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