net/failsafe: register slaves Rx interrupts
[dpdk.git] / drivers / net / failsafe / failsafe_ops.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <unistd.h>
37
38 #include <rte_debug.h>
39 #include <rte_atomic.h>
40 #include <rte_ethdev_driver.h>
41 #include <rte_malloc.h>
42 #include <rte_flow.h>
43 #include <rte_cycles.h>
44
45 #include "failsafe_private.h"
46
47 static struct rte_eth_dev_info default_infos = {
48         /* Max possible number of elements */
49         .max_rx_pktlen = UINT32_MAX,
50         .max_rx_queues = RTE_MAX_QUEUES_PER_PORT,
51         .max_tx_queues = RTE_MAX_QUEUES_PER_PORT,
52         .max_mac_addrs = FAILSAFE_MAX_ETHADDR,
53         .max_hash_mac_addrs = UINT32_MAX,
54         .max_vfs = UINT16_MAX,
55         .max_vmdq_pools = UINT16_MAX,
56         .rx_desc_lim = {
57                 .nb_max = UINT16_MAX,
58                 .nb_min = 0,
59                 .nb_align = 1,
60                 .nb_seg_max = UINT16_MAX,
61                 .nb_mtu_seg_max = UINT16_MAX,
62         },
63         .tx_desc_lim = {
64                 .nb_max = UINT16_MAX,
65                 .nb_min = 0,
66                 .nb_align = 1,
67                 .nb_seg_max = UINT16_MAX,
68                 .nb_mtu_seg_max = UINT16_MAX,
69         },
70         /*
71          * Set of capabilities that can be verified upon
72          * configuring a sub-device.
73          */
74         .rx_offload_capa =
75                 DEV_RX_OFFLOAD_VLAN_STRIP |
76                 DEV_RX_OFFLOAD_IPV4_CKSUM |
77                 DEV_RX_OFFLOAD_UDP_CKSUM |
78                 DEV_RX_OFFLOAD_TCP_CKSUM |
79                 DEV_RX_OFFLOAD_TCP_LRO |
80                 DEV_RX_OFFLOAD_QINQ_STRIP |
81                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
82                 DEV_RX_OFFLOAD_MACSEC_STRIP |
83                 DEV_RX_OFFLOAD_HEADER_SPLIT |
84                 DEV_RX_OFFLOAD_VLAN_FILTER |
85                 DEV_RX_OFFLOAD_VLAN_EXTEND |
86                 DEV_RX_OFFLOAD_JUMBO_FRAME |
87                 DEV_RX_OFFLOAD_CRC_STRIP |
88                 DEV_RX_OFFLOAD_SCATTER |
89                 DEV_RX_OFFLOAD_TIMESTAMP |
90                 DEV_RX_OFFLOAD_SECURITY,
91         .rx_queue_offload_capa =
92                 DEV_RX_OFFLOAD_VLAN_STRIP |
93                 DEV_RX_OFFLOAD_IPV4_CKSUM |
94                 DEV_RX_OFFLOAD_UDP_CKSUM |
95                 DEV_RX_OFFLOAD_TCP_CKSUM |
96                 DEV_RX_OFFLOAD_TCP_LRO |
97                 DEV_RX_OFFLOAD_QINQ_STRIP |
98                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
99                 DEV_RX_OFFLOAD_MACSEC_STRIP |
100                 DEV_RX_OFFLOAD_HEADER_SPLIT |
101                 DEV_RX_OFFLOAD_VLAN_FILTER |
102                 DEV_RX_OFFLOAD_VLAN_EXTEND |
103                 DEV_RX_OFFLOAD_JUMBO_FRAME |
104                 DEV_RX_OFFLOAD_CRC_STRIP |
105                 DEV_RX_OFFLOAD_SCATTER |
106                 DEV_RX_OFFLOAD_TIMESTAMP |
107                 DEV_RX_OFFLOAD_SECURITY,
108         .tx_offload_capa = 0x0,
109         .flow_type_rss_offloads = 0x0,
110 };
111
112 static int
113 fs_dev_configure(struct rte_eth_dev *dev)
114 {
115         struct sub_device *sdev;
116         uint64_t supp_tx_offloads;
117         uint64_t tx_offloads;
118         uint8_t i;
119         int ret;
120
121         supp_tx_offloads = PRIV(dev)->infos.tx_offload_capa;
122         tx_offloads = dev->data->dev_conf.txmode.offloads;
123         if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
124                 rte_errno = ENOTSUP;
125                 ERROR("Some Tx offloads are not supported, "
126                       "requested 0x%" PRIx64 " supported 0x%" PRIx64,
127                       tx_offloads, supp_tx_offloads);
128                 return -rte_errno;
129         }
130         FOREACH_SUBDEV(sdev, i, dev) {
131                 int rmv_interrupt = 0;
132                 int lsc_interrupt = 0;
133                 int lsc_enabled;
134
135                 if (sdev->state != DEV_PROBED)
136                         continue;
137
138                 rmv_interrupt = ETH(sdev)->data->dev_flags &
139                                 RTE_ETH_DEV_INTR_RMV;
140                 if (rmv_interrupt) {
141                         DEBUG("Enabling RMV interrupts for sub_device %d", i);
142                         dev->data->dev_conf.intr_conf.rmv = 1;
143                 } else {
144                         DEBUG("sub_device %d does not support RMV event", i);
145                 }
146                 lsc_enabled = dev->data->dev_conf.intr_conf.lsc;
147                 lsc_interrupt = lsc_enabled &&
148                                 (ETH(sdev)->data->dev_flags &
149                                  RTE_ETH_DEV_INTR_LSC);
150                 if (lsc_interrupt) {
151                         DEBUG("Enabling LSC interrupts for sub_device %d", i);
152                         dev->data->dev_conf.intr_conf.lsc = 1;
153                 } else if (lsc_enabled && !lsc_interrupt) {
154                         DEBUG("Disabling LSC interrupts for sub_device %d", i);
155                         dev->data->dev_conf.intr_conf.lsc = 0;
156                 }
157                 DEBUG("Configuring sub-device %d", i);
158                 sdev->remove = 0;
159                 ret = rte_eth_dev_configure(PORT_ID(sdev),
160                                         dev->data->nb_rx_queues,
161                                         dev->data->nb_tx_queues,
162                                         &dev->data->dev_conf);
163                 if (ret) {
164                         if (!fs_err(sdev, ret))
165                                 continue;
166                         ERROR("Could not configure sub_device %d", i);
167                         return ret;
168                 }
169                 if (rmv_interrupt) {
170                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
171                                         RTE_ETH_EVENT_INTR_RMV,
172                                         failsafe_eth_rmv_event_callback,
173                                         sdev);
174                         if (ret)
175                                 WARN("Failed to register RMV callback for sub_device %d",
176                                      SUB_ID(sdev));
177                 }
178                 dev->data->dev_conf.intr_conf.rmv = 0;
179                 if (lsc_interrupt) {
180                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
181                                                 RTE_ETH_EVENT_INTR_LSC,
182                                                 failsafe_eth_lsc_event_callback,
183                                                 dev);
184                         if (ret)
185                                 WARN("Failed to register LSC callback for sub_device %d",
186                                      SUB_ID(sdev));
187                 }
188                 dev->data->dev_conf.intr_conf.lsc = lsc_enabled;
189                 sdev->state = DEV_ACTIVE;
190         }
191         if (PRIV(dev)->state < DEV_ACTIVE)
192                 PRIV(dev)->state = DEV_ACTIVE;
193         return 0;
194 }
195
196 static int
197 fs_dev_start(struct rte_eth_dev *dev)
198 {
199         struct sub_device *sdev;
200         uint8_t i;
201         int ret;
202
203         ret = failsafe_rx_intr_install(dev);
204         if (ret)
205                 return ret;
206         FOREACH_SUBDEV(sdev, i, dev) {
207                 if (sdev->state != DEV_ACTIVE)
208                         continue;
209                 DEBUG("Starting sub_device %d", i);
210                 ret = rte_eth_dev_start(PORT_ID(sdev));
211                 if (ret) {
212                         if (!fs_err(sdev, ret))
213                                 continue;
214                         return ret;
215                 }
216                 ret = failsafe_rx_intr_install_subdevice(sdev);
217                 if (ret) {
218                         if (!fs_err(sdev, ret))
219                                 continue;
220                         rte_eth_dev_stop(PORT_ID(sdev));
221                         return ret;
222                 }
223                 sdev->state = DEV_STARTED;
224         }
225         if (PRIV(dev)->state < DEV_STARTED)
226                 PRIV(dev)->state = DEV_STARTED;
227         fs_switch_dev(dev, NULL);
228         return 0;
229 }
230
231 static void
232 fs_dev_stop(struct rte_eth_dev *dev)
233 {
234         struct sub_device *sdev;
235         uint8_t i;
236
237         PRIV(dev)->state = DEV_STARTED - 1;
238         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_STARTED) {
239                 rte_eth_dev_stop(PORT_ID(sdev));
240                 failsafe_rx_intr_uninstall_subdevice(sdev);
241                 sdev->state = DEV_STARTED - 1;
242         }
243         failsafe_rx_intr_uninstall(dev);
244 }
245
246 static int
247 fs_dev_set_link_up(struct rte_eth_dev *dev)
248 {
249         struct sub_device *sdev;
250         uint8_t i;
251         int ret;
252
253         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
254                 DEBUG("Calling rte_eth_dev_set_link_up on sub_device %d", i);
255                 ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
256                 if ((ret = fs_err(sdev, ret))) {
257                         ERROR("Operation rte_eth_dev_set_link_up failed for sub_device %d"
258                               " with error %d", i, ret);
259                         return ret;
260                 }
261         }
262         return 0;
263 }
264
265 static int
266 fs_dev_set_link_down(struct rte_eth_dev *dev)
267 {
268         struct sub_device *sdev;
269         uint8_t i;
270         int ret;
271
272         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
273                 DEBUG("Calling rte_eth_dev_set_link_down on sub_device %d", i);
274                 ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
275                 if ((ret = fs_err(sdev, ret))) {
276                         ERROR("Operation rte_eth_dev_set_link_down failed for sub_device %d"
277                               " with error %d", i, ret);
278                         return ret;
279                 }
280         }
281         return 0;
282 }
283
284 static void fs_dev_free_queues(struct rte_eth_dev *dev);
285 static void
286 fs_dev_close(struct rte_eth_dev *dev)
287 {
288         struct sub_device *sdev;
289         uint8_t i;
290
291         failsafe_hotplug_alarm_cancel(dev);
292         if (PRIV(dev)->state == DEV_STARTED)
293                 dev->dev_ops->dev_stop(dev);
294         PRIV(dev)->state = DEV_ACTIVE - 1;
295         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
296                 DEBUG("Closing sub_device %d", i);
297                 rte_eth_dev_close(PORT_ID(sdev));
298                 sdev->state = DEV_ACTIVE - 1;
299         }
300         fs_dev_free_queues(dev);
301 }
302
303 static bool
304 fs_rxq_offloads_valid(struct rte_eth_dev *dev, uint64_t offloads)
305 {
306         uint64_t port_offloads;
307         uint64_t queue_supp_offloads;
308         uint64_t port_supp_offloads;
309
310         port_offloads = dev->data->dev_conf.rxmode.offloads;
311         queue_supp_offloads = PRIV(dev)->infos.rx_queue_offload_capa;
312         port_supp_offloads = PRIV(dev)->infos.rx_offload_capa;
313         if ((offloads & (queue_supp_offloads | port_supp_offloads)) !=
314              offloads)
315                 return false;
316         /* Verify we have no conflict with port offloads */
317         if ((port_offloads ^ offloads) & port_supp_offloads)
318                 return false;
319         return true;
320 }
321
322 static void
323 fs_rx_queue_release(void *queue)
324 {
325         struct rte_eth_dev *dev;
326         struct sub_device *sdev;
327         uint8_t i;
328         struct rxq *rxq;
329
330         if (queue == NULL)
331                 return;
332         rxq = queue;
333         if (rxq->event_fd > 0)
334                 close(rxq->event_fd);
335         dev = rxq->priv->dev;
336         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
337                 SUBOPS(sdev, rx_queue_release)
338                         (ETH(sdev)->data->rx_queues[rxq->qid]);
339         dev->data->rx_queues[rxq->qid] = NULL;
340         rte_free(rxq);
341 }
342
343 static int
344 fs_rx_queue_setup(struct rte_eth_dev *dev,
345                 uint16_t rx_queue_id,
346                 uint16_t nb_rx_desc,
347                 unsigned int socket_id,
348                 const struct rte_eth_rxconf *rx_conf,
349                 struct rte_mempool *mb_pool)
350 {
351         /*
352          * FIXME: Add a proper interface in rte_eal_interrupts for
353          * allocating eventfd as an interrupt vector.
354          * For the time being, fake as if we are using MSIX interrupts,
355          * this will cause rte_intr_efd_enable to allocate an eventfd for us.
356          */
357         struct rte_intr_handle intr_handle = {
358                 .type = RTE_INTR_HANDLE_VFIO_MSIX,
359                 .efds = { -1, },
360         };
361         struct sub_device *sdev;
362         struct rxq *rxq;
363         uint8_t i;
364         int ret;
365
366         rxq = dev->data->rx_queues[rx_queue_id];
367         if (rxq != NULL) {
368                 fs_rx_queue_release(rxq);
369                 dev->data->rx_queues[rx_queue_id] = NULL;
370         }
371         /* Verify application offloads are valid for our port and queue. */
372         if (fs_rxq_offloads_valid(dev, rx_conf->offloads) == false) {
373                 rte_errno = ENOTSUP;
374                 ERROR("Rx queue offloads 0x%" PRIx64
375                       " don't match port offloads 0x%" PRIx64
376                       " or supported offloads 0x%" PRIx64,
377                       rx_conf->offloads,
378                       dev->data->dev_conf.rxmode.offloads,
379                       PRIV(dev)->infos.rx_offload_capa |
380                       PRIV(dev)->infos.rx_queue_offload_capa);
381                 return -rte_errno;
382         }
383         rxq = rte_zmalloc(NULL,
384                           sizeof(*rxq) +
385                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
386                           RTE_CACHE_LINE_SIZE);
387         if (rxq == NULL)
388                 return -ENOMEM;
389         FOREACH_SUBDEV(sdev, i, dev)
390                 rte_atomic64_init(&rxq->refcnt[i]);
391         rxq->qid = rx_queue_id;
392         rxq->socket_id = socket_id;
393         rxq->info.mp = mb_pool;
394         rxq->info.conf = *rx_conf;
395         rxq->info.nb_desc = nb_rx_desc;
396         rxq->priv = PRIV(dev);
397         rxq->sdev = PRIV(dev)->subs;
398         ret = rte_intr_efd_enable(&intr_handle, 1);
399         if (ret < 0)
400                 return ret;
401         rxq->event_fd = intr_handle.efds[0];
402         dev->data->rx_queues[rx_queue_id] = rxq;
403         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
404                 ret = rte_eth_rx_queue_setup(PORT_ID(sdev),
405                                 rx_queue_id,
406                                 nb_rx_desc, socket_id,
407                                 rx_conf, mb_pool);
408                 if ((ret = fs_err(sdev, ret))) {
409                         ERROR("RX queue setup failed for sub_device %d", i);
410                         goto free_rxq;
411                 }
412         }
413         return 0;
414 free_rxq:
415         fs_rx_queue_release(rxq);
416         return ret;
417 }
418
419 static int
420 fs_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
421 {
422         struct rxq *rxq;
423         struct sub_device *sdev;
424         uint8_t i;
425         int ret;
426         int rc = 0;
427
428         if (idx >= dev->data->nb_rx_queues) {
429                 rte_errno = EINVAL;
430                 return -rte_errno;
431         }
432         rxq = dev->data->rx_queues[idx];
433         if (rxq == NULL || rxq->event_fd <= 0) {
434                 rte_errno = EINVAL;
435                 return -rte_errno;
436         }
437         rxq->enable_events = 1;
438         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
439                 ret = rte_eth_dev_rx_intr_enable(PORT_ID(sdev), idx);
440                 ret = fs_err(sdev, ret);
441                 if (ret)
442                         rc = ret;
443         }
444         if (rc)
445                 rte_errno = -rc;
446         return rc;
447 }
448
449 static int
450 fs_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
451 {
452         struct rxq *rxq;
453         struct sub_device *sdev;
454         uint64_t u64;
455         uint8_t i;
456         int rc = 0;
457         int ret;
458
459         if (idx >= dev->data->nb_rx_queues) {
460                 rte_errno = EINVAL;
461                 return -rte_errno;
462         }
463         rxq = dev->data->rx_queues[idx];
464         if (rxq == NULL || rxq->event_fd <= 0) {
465                 rte_errno = EINVAL;
466                 return -rte_errno;
467         }
468         rxq->enable_events = 0;
469         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
470                 ret = rte_eth_dev_rx_intr_disable(PORT_ID(sdev), idx);
471                 ret = fs_err(sdev, ret);
472                 if (ret)
473                         rc = ret;
474         }
475         /* Clear pending events */
476         while (read(rxq->event_fd, &u64, sizeof(uint64_t)) >  0)
477                 ;
478         if (rc)
479                 rte_errno = -rc;
480         return rc;
481 }
482
483 static bool
484 fs_txq_offloads_valid(struct rte_eth_dev *dev, uint64_t offloads)
485 {
486         uint64_t port_offloads;
487         uint64_t queue_supp_offloads;
488         uint64_t port_supp_offloads;
489
490         port_offloads = dev->data->dev_conf.txmode.offloads;
491         queue_supp_offloads = PRIV(dev)->infos.tx_queue_offload_capa;
492         port_supp_offloads = PRIV(dev)->infos.tx_offload_capa;
493         if ((offloads & (queue_supp_offloads | port_supp_offloads)) !=
494              offloads)
495                 return false;
496         /* Verify we have no conflict with port offloads */
497         if ((port_offloads ^ offloads) & port_supp_offloads)
498                 return false;
499         return true;
500 }
501
502 static void
503 fs_tx_queue_release(void *queue)
504 {
505         struct rte_eth_dev *dev;
506         struct sub_device *sdev;
507         uint8_t i;
508         struct txq *txq;
509
510         if (queue == NULL)
511                 return;
512         txq = queue;
513         dev = txq->priv->dev;
514         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
515                 SUBOPS(sdev, tx_queue_release)
516                         (ETH(sdev)->data->tx_queues[txq->qid]);
517         dev->data->tx_queues[txq->qid] = NULL;
518         rte_free(txq);
519 }
520
521 static int
522 fs_tx_queue_setup(struct rte_eth_dev *dev,
523                 uint16_t tx_queue_id,
524                 uint16_t nb_tx_desc,
525                 unsigned int socket_id,
526                 const struct rte_eth_txconf *tx_conf)
527 {
528         struct sub_device *sdev;
529         struct txq *txq;
530         uint8_t i;
531         int ret;
532
533         txq = dev->data->tx_queues[tx_queue_id];
534         if (txq != NULL) {
535                 fs_tx_queue_release(txq);
536                 dev->data->tx_queues[tx_queue_id] = NULL;
537         }
538         /*
539          * Don't verify queue offloads for applications which
540          * use the old API.
541          */
542         if (tx_conf != NULL &&
543             (tx_conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) &&
544             fs_txq_offloads_valid(dev, tx_conf->offloads) == false) {
545                 rte_errno = ENOTSUP;
546                 ERROR("Tx queue offloads 0x%" PRIx64
547                       " don't match port offloads 0x%" PRIx64
548                       " or supported offloads 0x%" PRIx64,
549                       tx_conf->offloads,
550                       dev->data->dev_conf.txmode.offloads,
551                       PRIV(dev)->infos.tx_offload_capa |
552                       PRIV(dev)->infos.tx_queue_offload_capa);
553                 return -rte_errno;
554         }
555         txq = rte_zmalloc("ethdev TX queue",
556                           sizeof(*txq) +
557                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
558                           RTE_CACHE_LINE_SIZE);
559         if (txq == NULL)
560                 return -ENOMEM;
561         FOREACH_SUBDEV(sdev, i, dev)
562                 rte_atomic64_init(&txq->refcnt[i]);
563         txq->qid = tx_queue_id;
564         txq->socket_id = socket_id;
565         txq->info.conf = *tx_conf;
566         txq->info.nb_desc = nb_tx_desc;
567         txq->priv = PRIV(dev);
568         dev->data->tx_queues[tx_queue_id] = txq;
569         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
570                 ret = rte_eth_tx_queue_setup(PORT_ID(sdev),
571                                 tx_queue_id,
572                                 nb_tx_desc, socket_id,
573                                 tx_conf);
574                 if ((ret = fs_err(sdev, ret))) {
575                         ERROR("TX queue setup failed for sub_device %d", i);
576                         goto free_txq;
577                 }
578         }
579         return 0;
580 free_txq:
581         fs_tx_queue_release(txq);
582         return ret;
583 }
584
585 static void
586 fs_dev_free_queues(struct rte_eth_dev *dev)
587 {
588         uint16_t i;
589
590         for (i = 0; i < dev->data->nb_rx_queues; i++) {
591                 fs_rx_queue_release(dev->data->rx_queues[i]);
592                 dev->data->rx_queues[i] = NULL;
593         }
594         dev->data->nb_rx_queues = 0;
595         for (i = 0; i < dev->data->nb_tx_queues; i++) {
596                 fs_tx_queue_release(dev->data->tx_queues[i]);
597                 dev->data->tx_queues[i] = NULL;
598         }
599         dev->data->nb_tx_queues = 0;
600 }
601
602 static void
603 fs_promiscuous_enable(struct rte_eth_dev *dev)
604 {
605         struct sub_device *sdev;
606         uint8_t i;
607
608         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
609                 rte_eth_promiscuous_enable(PORT_ID(sdev));
610 }
611
612 static void
613 fs_promiscuous_disable(struct rte_eth_dev *dev)
614 {
615         struct sub_device *sdev;
616         uint8_t i;
617
618         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
619                 rte_eth_promiscuous_disable(PORT_ID(sdev));
620 }
621
622 static void
623 fs_allmulticast_enable(struct rte_eth_dev *dev)
624 {
625         struct sub_device *sdev;
626         uint8_t i;
627
628         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
629                 rte_eth_allmulticast_enable(PORT_ID(sdev));
630 }
631
632 static void
633 fs_allmulticast_disable(struct rte_eth_dev *dev)
634 {
635         struct sub_device *sdev;
636         uint8_t i;
637
638         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
639                 rte_eth_allmulticast_disable(PORT_ID(sdev));
640 }
641
642 static int
643 fs_link_update(struct rte_eth_dev *dev,
644                 int wait_to_complete)
645 {
646         struct sub_device *sdev;
647         uint8_t i;
648         int ret;
649
650         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
651                 DEBUG("Calling link_update on sub_device %d", i);
652                 ret = (SUBOPS(sdev, link_update))(ETH(sdev), wait_to_complete);
653                 if (ret && ret != -1 && sdev->remove == 0 &&
654                     rte_eth_dev_is_removed(PORT_ID(sdev)) == 0) {
655                         ERROR("Link update failed for sub_device %d with error %d",
656                               i, ret);
657                         return ret;
658                 }
659         }
660         if (TX_SUBDEV(dev)) {
661                 struct rte_eth_link *l1;
662                 struct rte_eth_link *l2;
663
664                 l1 = &dev->data->dev_link;
665                 l2 = &ETH(TX_SUBDEV(dev))->data->dev_link;
666                 if (memcmp(l1, l2, sizeof(*l1))) {
667                         *l1 = *l2;
668                         return 0;
669                 }
670         }
671         return -1;
672 }
673
674 static int
675 fs_stats_get(struct rte_eth_dev *dev,
676              struct rte_eth_stats *stats)
677 {
678         struct rte_eth_stats backup;
679         struct sub_device *sdev;
680         uint8_t i;
681         int ret;
682
683         rte_memcpy(stats, &PRIV(dev)->stats_accumulator, sizeof(*stats));
684         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
685                 struct rte_eth_stats *snapshot = &sdev->stats_snapshot.stats;
686                 uint64_t *timestamp = &sdev->stats_snapshot.timestamp;
687
688                 rte_memcpy(&backup, snapshot, sizeof(backup));
689                 ret = rte_eth_stats_get(PORT_ID(sdev), snapshot);
690                 if (ret) {
691                         if (!fs_err(sdev, ret)) {
692                                 rte_memcpy(snapshot, &backup, sizeof(backup));
693                                 goto inc;
694                         }
695                         ERROR("Operation rte_eth_stats_get failed for sub_device %d with error %d",
696                                   i, ret);
697                         *timestamp = 0;
698                         return ret;
699                 }
700                 *timestamp = rte_rdtsc();
701 inc:
702                 failsafe_stats_increment(stats, snapshot);
703         }
704         return 0;
705 }
706
707 static void
708 fs_stats_reset(struct rte_eth_dev *dev)
709 {
710         struct sub_device *sdev;
711         uint8_t i;
712
713         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
714                 rte_eth_stats_reset(PORT_ID(sdev));
715                 memset(&sdev->stats_snapshot, 0, sizeof(struct rte_eth_stats));
716         }
717         memset(&PRIV(dev)->stats_accumulator, 0, sizeof(struct rte_eth_stats));
718 }
719
720 /**
721  * Fail-safe dev_infos_get rules:
722  *
723  * No sub_device:
724  *   Numerables:
725  *      Use the maximum possible values for any field, so as not
726  *      to impede any further configuration effort.
727  *   Capabilities:
728  *      Limits capabilities to those that are understood by the
729  *      fail-safe PMD. This understanding stems from the fail-safe
730  *      being capable of verifying that the related capability is
731  *      expressed within the device configuration (struct rte_eth_conf).
732  *
733  * At least one probed sub_device:
734  *   Numerables:
735  *      Uses values from the active probed sub_device
736  *      The rationale here is that if any sub_device is less capable
737  *      (for example concerning the number of queues) than the active
738  *      sub_device, then its subsequent configuration will fail.
739  *      It is impossible to foresee this failure when the failing sub_device
740  *      is supposed to be plugged-in later on, so the configuration process
741  *      is the single point of failure and error reporting.
742  *   Capabilities:
743  *      Uses a logical AND of RX capabilities among
744  *      all sub_devices and the default capabilities.
745  *      Uses a logical AND of TX capabilities among
746  *      the active probed sub_device and the default capabilities.
747  *
748  */
749 static void
750 fs_dev_infos_get(struct rte_eth_dev *dev,
751                   struct rte_eth_dev_info *infos)
752 {
753         struct sub_device *sdev;
754         uint8_t i;
755
756         sdev = TX_SUBDEV(dev);
757         if (sdev == NULL) {
758                 DEBUG("No probed device, using default infos");
759                 rte_memcpy(&PRIV(dev)->infos, &default_infos,
760                            sizeof(default_infos));
761         } else {
762                 uint64_t rx_offload_capa;
763                 uint64_t rxq_offload_capa;
764
765                 rx_offload_capa = default_infos.rx_offload_capa;
766                 rxq_offload_capa = default_infos.rx_queue_offload_capa;
767                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
768                         rte_eth_dev_info_get(PORT_ID(sdev),
769                                         &PRIV(dev)->infos);
770                         rx_offload_capa &= PRIV(dev)->infos.rx_offload_capa;
771                         rxq_offload_capa &=
772                                         PRIV(dev)->infos.rx_queue_offload_capa;
773                 }
774                 sdev = TX_SUBDEV(dev);
775                 rte_eth_dev_info_get(PORT_ID(sdev), &PRIV(dev)->infos);
776                 PRIV(dev)->infos.rx_offload_capa = rx_offload_capa;
777                 PRIV(dev)->infos.rx_queue_offload_capa = rxq_offload_capa;
778                 PRIV(dev)->infos.tx_offload_capa &=
779                                         default_infos.tx_offload_capa;
780                 PRIV(dev)->infos.tx_queue_offload_capa &=
781                                         default_infos.tx_queue_offload_capa;
782                 PRIV(dev)->infos.flow_type_rss_offloads &=
783                                         default_infos.flow_type_rss_offloads;
784         }
785         rte_memcpy(infos, &PRIV(dev)->infos, sizeof(*infos));
786 }
787
788 static const uint32_t *
789 fs_dev_supported_ptypes_get(struct rte_eth_dev *dev)
790 {
791         struct sub_device *sdev;
792         struct rte_eth_dev *edev;
793
794         sdev = TX_SUBDEV(dev);
795         if (sdev == NULL)
796                 return NULL;
797         edev = ETH(sdev);
798         /* ENOTSUP: counts as no supported ptypes */
799         if (SUBOPS(sdev, dev_supported_ptypes_get) == NULL)
800                 return NULL;
801         /*
802          * The API does not permit to do a clean AND of all ptypes,
803          * It is also incomplete by design and we do not really care
804          * to have a best possible value in this context.
805          * We just return the ptypes of the device of highest
806          * priority, usually the PREFERRED device.
807          */
808         return SUBOPS(sdev, dev_supported_ptypes_get)(edev);
809 }
810
811 static int
812 fs_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
813 {
814         struct sub_device *sdev;
815         uint8_t i;
816         int ret;
817
818         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
819                 DEBUG("Calling rte_eth_dev_set_mtu on sub_device %d", i);
820                 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), mtu);
821                 if ((ret = fs_err(sdev, ret))) {
822                         ERROR("Operation rte_eth_dev_set_mtu failed for sub_device %d with error %d",
823                               i, ret);
824                         return ret;
825                 }
826         }
827         return 0;
828 }
829
830 static int
831 fs_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
832 {
833         struct sub_device *sdev;
834         uint8_t i;
835         int ret;
836
837         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
838                 DEBUG("Calling rte_eth_dev_vlan_filter on sub_device %d", i);
839                 ret = rte_eth_dev_vlan_filter(PORT_ID(sdev), vlan_id, on);
840                 if ((ret = fs_err(sdev, ret))) {
841                         ERROR("Operation rte_eth_dev_vlan_filter failed for sub_device %d"
842                               " with error %d", i, ret);
843                         return ret;
844                 }
845         }
846         return 0;
847 }
848
849 static int
850 fs_flow_ctrl_get(struct rte_eth_dev *dev,
851                 struct rte_eth_fc_conf *fc_conf)
852 {
853         struct sub_device *sdev;
854
855         sdev = TX_SUBDEV(dev);
856         if (sdev == NULL)
857                 return 0;
858         if (SUBOPS(sdev, flow_ctrl_get) == NULL)
859                 return -ENOTSUP;
860         return SUBOPS(sdev, flow_ctrl_get)(ETH(sdev), fc_conf);
861 }
862
863 static int
864 fs_flow_ctrl_set(struct rte_eth_dev *dev,
865                 struct rte_eth_fc_conf *fc_conf)
866 {
867         struct sub_device *sdev;
868         uint8_t i;
869         int ret;
870
871         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
872                 DEBUG("Calling rte_eth_dev_flow_ctrl_set on sub_device %d", i);
873                 ret = rte_eth_dev_flow_ctrl_set(PORT_ID(sdev), fc_conf);
874                 if ((ret = fs_err(sdev, ret))) {
875                         ERROR("Operation rte_eth_dev_flow_ctrl_set failed for sub_device %d"
876                               " with error %d", i, ret);
877                         return ret;
878                 }
879         }
880         return 0;
881 }
882
883 static void
884 fs_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
885 {
886         struct sub_device *sdev;
887         uint8_t i;
888
889         /* No check: already done within the rte_eth_dev_mac_addr_remove
890          * call for the fail-safe device.
891          */
892         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
893                 rte_eth_dev_mac_addr_remove(PORT_ID(sdev),
894                                 &dev->data->mac_addrs[index]);
895         PRIV(dev)->mac_addr_pool[index] = 0;
896 }
897
898 static int
899 fs_mac_addr_add(struct rte_eth_dev *dev,
900                 struct ether_addr *mac_addr,
901                 uint32_t index,
902                 uint32_t vmdq)
903 {
904         struct sub_device *sdev;
905         int ret;
906         uint8_t i;
907
908         RTE_ASSERT(index < FAILSAFE_MAX_ETHADDR);
909         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
910                 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), mac_addr, vmdq);
911                 if ((ret = fs_err(sdev, ret))) {
912                         ERROR("Operation rte_eth_dev_mac_addr_add failed for sub_device %"
913                               PRIu8 " with error %d", i, ret);
914                         return ret;
915                 }
916         }
917         if (index >= PRIV(dev)->nb_mac_addr) {
918                 DEBUG("Growing mac_addrs array");
919                 PRIV(dev)->nb_mac_addr = index;
920         }
921         PRIV(dev)->mac_addr_pool[index] = vmdq;
922         return 0;
923 }
924
925 static void
926 fs_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
927 {
928         struct sub_device *sdev;
929         uint8_t i;
930
931         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
932                 rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), mac_addr);
933 }
934
935 static int
936 fs_filter_ctrl(struct rte_eth_dev *dev,
937                 enum rte_filter_type type,
938                 enum rte_filter_op op,
939                 void *arg)
940 {
941         struct sub_device *sdev;
942         uint8_t i;
943         int ret;
944
945         if (type == RTE_ETH_FILTER_GENERIC &&
946             op == RTE_ETH_FILTER_GET) {
947                 *(const void **)arg = &fs_flow_ops;
948                 return 0;
949         }
950         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
951                 DEBUG("Calling rte_eth_dev_filter_ctrl on sub_device %d", i);
952                 ret = rte_eth_dev_filter_ctrl(PORT_ID(sdev), type, op, arg);
953                 if ((ret = fs_err(sdev, ret))) {
954                         ERROR("Operation rte_eth_dev_filter_ctrl failed for sub_device %d"
955                               " with error %d", i, ret);
956                         return ret;
957                 }
958         }
959         return 0;
960 }
961
962 const struct eth_dev_ops failsafe_ops = {
963         .dev_configure = fs_dev_configure,
964         .dev_start = fs_dev_start,
965         .dev_stop = fs_dev_stop,
966         .dev_set_link_down = fs_dev_set_link_down,
967         .dev_set_link_up = fs_dev_set_link_up,
968         .dev_close = fs_dev_close,
969         .promiscuous_enable = fs_promiscuous_enable,
970         .promiscuous_disable = fs_promiscuous_disable,
971         .allmulticast_enable = fs_allmulticast_enable,
972         .allmulticast_disable = fs_allmulticast_disable,
973         .link_update = fs_link_update,
974         .stats_get = fs_stats_get,
975         .stats_reset = fs_stats_reset,
976         .dev_infos_get = fs_dev_infos_get,
977         .dev_supported_ptypes_get = fs_dev_supported_ptypes_get,
978         .mtu_set = fs_mtu_set,
979         .vlan_filter_set = fs_vlan_filter_set,
980         .rx_queue_setup = fs_rx_queue_setup,
981         .tx_queue_setup = fs_tx_queue_setup,
982         .rx_queue_release = fs_rx_queue_release,
983         .tx_queue_release = fs_tx_queue_release,
984         .rx_queue_intr_enable = fs_rx_intr_enable,
985         .rx_queue_intr_disable = fs_rx_intr_disable,
986         .flow_ctrl_get = fs_flow_ctrl_get,
987         .flow_ctrl_set = fs_flow_ctrl_set,
988         .mac_addr_remove = fs_mac_addr_remove,
989         .mac_addr_add = fs_mac_addr_add,
990         .mac_addr_set = fs_mac_addr_set,
991         .filter_ctrl = fs_filter_ctrl,
992 };