New upstream version 17.11.5
[deb_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 <stdint.h>
35
36 #include <rte_debug.h>
37 #include <rte_atomic.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_flow.h>
41 #include <rte_cycles.h>
42
43 #include "failsafe_private.h"
44
45 static struct rte_eth_dev_info default_infos = {
46         /* Max possible number of elements */
47         .max_rx_pktlen = UINT32_MAX,
48         .max_rx_queues = RTE_MAX_QUEUES_PER_PORT,
49         .max_tx_queues = RTE_MAX_QUEUES_PER_PORT,
50         .max_mac_addrs = FAILSAFE_MAX_ETHADDR,
51         .max_hash_mac_addrs = UINT32_MAX,
52         .max_vfs = UINT16_MAX,
53         .max_vmdq_pools = UINT16_MAX,
54         .rx_desc_lim = {
55                 .nb_max = UINT16_MAX,
56                 .nb_min = 0,
57                 .nb_align = 1,
58                 .nb_seg_max = UINT16_MAX,
59                 .nb_mtu_seg_max = UINT16_MAX,
60         },
61         .tx_desc_lim = {
62                 .nb_max = UINT16_MAX,
63                 .nb_min = 0,
64                 .nb_align = 1,
65                 .nb_seg_max = UINT16_MAX,
66                 .nb_mtu_seg_max = UINT16_MAX,
67         },
68         /*
69          * Set of capabilities that can be verified upon
70          * configuring a sub-device.
71          */
72         .rx_offload_capa =
73                 DEV_RX_OFFLOAD_VLAN_STRIP |
74                 DEV_RX_OFFLOAD_QINQ_STRIP |
75                 DEV_RX_OFFLOAD_IPV4_CKSUM |
76                 DEV_RX_OFFLOAD_UDP_CKSUM |
77                 DEV_RX_OFFLOAD_TCP_CKSUM |
78                 DEV_RX_OFFLOAD_TCP_LRO,
79         .tx_offload_capa = 0x0,
80         .flow_type_rss_offloads = 0x0,
81 };
82
83 static int
84 fs_dev_configure(struct rte_eth_dev *dev)
85 {
86         struct sub_device *sdev;
87         uint8_t i;
88         int ret;
89
90         FOREACH_SUBDEV(sdev, i, dev) {
91                 int rmv_interrupt = 0;
92                 int lsc_interrupt = 0;
93                 int lsc_enabled;
94
95                 if (sdev->state != DEV_PROBED)
96                         continue;
97
98                 rmv_interrupt = ETH(sdev)->data->dev_flags &
99                                 RTE_ETH_DEV_INTR_RMV;
100                 if (rmv_interrupt) {
101                         DEBUG("Enabling RMV interrupts for sub_device %d", i);
102                         dev->data->dev_conf.intr_conf.rmv = 1;
103                 } else {
104                         DEBUG("sub_device %d does not support RMV event", i);
105                 }
106                 lsc_enabled = dev->data->dev_conf.intr_conf.lsc;
107                 lsc_interrupt = lsc_enabled &&
108                                 (ETH(sdev)->data->dev_flags &
109                                  RTE_ETH_DEV_INTR_LSC);
110                 if (lsc_interrupt) {
111                         DEBUG("Enabling LSC interrupts for sub_device %d", i);
112                         dev->data->dev_conf.intr_conf.lsc = 1;
113                 } else if (lsc_enabled && !lsc_interrupt) {
114                         DEBUG("Disabling LSC interrupts for sub_device %d", i);
115                         dev->data->dev_conf.intr_conf.lsc = 0;
116                 }
117                 DEBUG("Configuring sub-device %d", i);
118                 sdev->remove = 0;
119                 ret = rte_eth_dev_configure(PORT_ID(sdev),
120                                         dev->data->nb_rx_queues,
121                                         dev->data->nb_tx_queues,
122                                         &dev->data->dev_conf);
123                 if (ret) {
124                         ERROR("Could not configure sub_device %d", i);
125                         return ret;
126                 }
127                 if (rmv_interrupt && sdev->rmv_callback == 0) {
128                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
129                                         RTE_ETH_EVENT_INTR_RMV,
130                                         failsafe_eth_rmv_event_callback,
131                                         sdev);
132                         if (ret)
133                                 WARN("Failed to register RMV callback for sub_device %d",
134                                      SUB_ID(sdev));
135                         else
136                                 sdev->rmv_callback = 1;
137                 }
138                 dev->data->dev_conf.intr_conf.rmv = 0;
139                 if (lsc_interrupt && sdev->lsc_callback == 0) {
140                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
141                                                 RTE_ETH_EVENT_INTR_LSC,
142                                                 failsafe_eth_lsc_event_callback,
143                                                 dev);
144                         if (ret)
145                                 WARN("Failed to register LSC callback for sub_device %d",
146                                      SUB_ID(sdev));
147                         else
148                                 sdev->lsc_callback = 1;
149                 }
150                 dev->data->dev_conf.intr_conf.lsc = lsc_enabled;
151                 sdev->state = DEV_ACTIVE;
152         }
153         if (PRIV(dev)->state < DEV_ACTIVE)
154                 PRIV(dev)->state = DEV_ACTIVE;
155         return 0;
156 }
157
158 static int
159 fs_dev_start(struct rte_eth_dev *dev)
160 {
161         struct sub_device *sdev;
162         uint8_t i;
163         int ret;
164
165         FOREACH_SUBDEV(sdev, i, dev) {
166                 if (sdev->state != DEV_ACTIVE)
167                         continue;
168                 DEBUG("Starting sub_device %d", i);
169                 ret = rte_eth_dev_start(PORT_ID(sdev));
170                 if (ret)
171                         return ret;
172                 sdev->state = DEV_STARTED;
173         }
174         if (PRIV(dev)->state < DEV_STARTED)
175                 PRIV(dev)->state = DEV_STARTED;
176         fs_switch_dev(dev, NULL);
177         return 0;
178 }
179
180 static void
181 fs_dev_stop(struct rte_eth_dev *dev)
182 {
183         struct sub_device *sdev;
184         uint8_t i;
185
186         PRIV(dev)->state = DEV_STARTED - 1;
187         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_STARTED) {
188                 rte_eth_dev_stop(PORT_ID(sdev));
189                 sdev->state = DEV_STARTED - 1;
190         }
191 }
192
193 static int
194 fs_dev_set_link_up(struct rte_eth_dev *dev)
195 {
196         struct sub_device *sdev;
197         uint8_t i;
198         int ret;
199
200         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
201                 DEBUG("Calling rte_eth_dev_set_link_up on sub_device %d", i);
202                 ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
203                 if (ret) {
204                         ERROR("Operation rte_eth_dev_set_link_up failed for sub_device %d"
205                               " with error %d", i, ret);
206                         return ret;
207                 }
208         }
209         return 0;
210 }
211
212 static int
213 fs_dev_set_link_down(struct rte_eth_dev *dev)
214 {
215         struct sub_device *sdev;
216         uint8_t i;
217         int ret;
218
219         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
220                 DEBUG("Calling rte_eth_dev_set_link_down on sub_device %d", i);
221                 ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
222                 if (ret) {
223                         ERROR("Operation rte_eth_dev_set_link_down failed for sub_device %d"
224                               " with error %d", i, ret);
225                         return ret;
226                 }
227         }
228         return 0;
229 }
230
231 static void fs_dev_free_queues(struct rte_eth_dev *dev);
232 static void
233 fs_dev_close(struct rte_eth_dev *dev)
234 {
235         struct sub_device *sdev;
236         uint8_t i;
237
238         failsafe_hotplug_alarm_cancel(dev);
239         if (PRIV(dev)->state == DEV_STARTED)
240                 dev->dev_ops->dev_stop(dev);
241         PRIV(dev)->state = DEV_ACTIVE - 1;
242         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
243                 DEBUG("Closing sub_device %d", i);
244                 failsafe_eth_dev_unregister_callbacks(sdev);
245                 rte_eth_dev_close(PORT_ID(sdev));
246                 sdev->state = DEV_ACTIVE - 1;
247         }
248         fs_dev_free_queues(dev);
249 }
250
251 static void
252 fs_rx_queue_release(void *queue)
253 {
254         struct rte_eth_dev *dev;
255         struct sub_device *sdev;
256         uint8_t i;
257         struct rxq *rxq;
258
259         if (queue == NULL)
260                 return;
261         rxq = queue;
262         dev = rxq->priv->dev;
263         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
264                 if (ETH(sdev)->data->rx_queues != NULL &&
265                     ETH(sdev)->data->rx_queues[rxq->qid] != NULL) {
266                         SUBOPS(sdev, rx_queue_release)
267                                 (ETH(sdev)->data->rx_queues[rxq->qid]);
268                 }
269         }
270         dev->data->rx_queues[rxq->qid] = NULL;
271         rte_free(rxq);
272 }
273
274 static int
275 fs_rx_queue_setup(struct rte_eth_dev *dev,
276                 uint16_t rx_queue_id,
277                 uint16_t nb_rx_desc,
278                 unsigned int socket_id,
279                 const struct rte_eth_rxconf *rx_conf,
280                 struct rte_mempool *mb_pool)
281 {
282         struct sub_device *sdev;
283         struct rxq *rxq;
284         uint8_t i;
285         int ret;
286
287         if (rx_conf->rx_deferred_start) {
288                 ERROR("Rx queue deferred start is not supported");
289                 return -EINVAL;
290         }
291
292         rxq = dev->data->rx_queues[rx_queue_id];
293         if (rxq != NULL) {
294                 fs_rx_queue_release(rxq);
295                 dev->data->rx_queues[rx_queue_id] = NULL;
296         }
297         rxq = rte_zmalloc(NULL,
298                           sizeof(*rxq) +
299                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
300                           RTE_CACHE_LINE_SIZE);
301         if (rxq == NULL)
302                 return -ENOMEM;
303         FOREACH_SUBDEV(sdev, i, dev)
304                 rte_atomic64_init(&rxq->refcnt[i]);
305         rxq->qid = rx_queue_id;
306         rxq->socket_id = socket_id;
307         rxq->info.mp = mb_pool;
308         rxq->info.conf = *rx_conf;
309         rxq->info.nb_desc = nb_rx_desc;
310         rxq->priv = PRIV(dev);
311         dev->data->rx_queues[rx_queue_id] = rxq;
312         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
313                 ret = rte_eth_rx_queue_setup(PORT_ID(sdev),
314                                 rx_queue_id,
315                                 nb_rx_desc, socket_id,
316                                 rx_conf, mb_pool);
317                 if (ret) {
318                         ERROR("RX queue setup failed for sub_device %d", i);
319                         goto free_rxq;
320                 }
321         }
322         return 0;
323 free_rxq:
324         fs_rx_queue_release(rxq);
325         return ret;
326 }
327
328 static void
329 fs_tx_queue_release(void *queue)
330 {
331         struct rte_eth_dev *dev;
332         struct sub_device *sdev;
333         uint8_t i;
334         struct txq *txq;
335
336         if (queue == NULL)
337                 return;
338         txq = queue;
339         dev = txq->priv->dev;
340         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
341                 if (ETH(sdev)->data->tx_queues != NULL &&
342                     ETH(sdev)->data->tx_queues[txq->qid] != NULL) {
343                         SUBOPS(sdev, tx_queue_release)
344                                 (ETH(sdev)->data->tx_queues[txq->qid]);
345                 }
346         }
347         dev->data->tx_queues[txq->qid] = NULL;
348         rte_free(txq);
349 }
350
351 static int
352 fs_tx_queue_setup(struct rte_eth_dev *dev,
353                 uint16_t tx_queue_id,
354                 uint16_t nb_tx_desc,
355                 unsigned int socket_id,
356                 const struct rte_eth_txconf *tx_conf)
357 {
358         struct sub_device *sdev;
359         struct txq *txq;
360         uint8_t i;
361         int ret;
362
363         if (tx_conf->tx_deferred_start) {
364                 ERROR("Tx queue deferred start is not supported");
365                 return -EINVAL;
366         }
367
368         txq = dev->data->tx_queues[tx_queue_id];
369         if (txq != NULL) {
370                 fs_tx_queue_release(txq);
371                 dev->data->tx_queues[tx_queue_id] = NULL;
372         }
373         txq = rte_zmalloc("ethdev TX queue",
374                           sizeof(*txq) +
375                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
376                           RTE_CACHE_LINE_SIZE);
377         if (txq == NULL)
378                 return -ENOMEM;
379         FOREACH_SUBDEV(sdev, i, dev)
380                 rte_atomic64_init(&txq->refcnt[i]);
381         txq->qid = tx_queue_id;
382         txq->socket_id = socket_id;
383         txq->info.conf = *tx_conf;
384         txq->info.nb_desc = nb_tx_desc;
385         txq->priv = PRIV(dev);
386         dev->data->tx_queues[tx_queue_id] = txq;
387         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
388                 ret = rte_eth_tx_queue_setup(PORT_ID(sdev),
389                                 tx_queue_id,
390                                 nb_tx_desc, socket_id,
391                                 tx_conf);
392                 if (ret) {
393                         ERROR("TX queue setup failed for sub_device %d", i);
394                         goto free_txq;
395                 }
396         }
397         return 0;
398 free_txq:
399         fs_tx_queue_release(txq);
400         return ret;
401 }
402
403 static void
404 fs_dev_free_queues(struct rte_eth_dev *dev)
405 {
406         uint16_t i;
407
408         for (i = 0; i < dev->data->nb_rx_queues; i++) {
409                 fs_rx_queue_release(dev->data->rx_queues[i]);
410                 dev->data->rx_queues[i] = NULL;
411         }
412         dev->data->nb_rx_queues = 0;
413         for (i = 0; i < dev->data->nb_tx_queues; i++) {
414                 fs_tx_queue_release(dev->data->tx_queues[i]);
415                 dev->data->tx_queues[i] = NULL;
416         }
417         dev->data->nb_tx_queues = 0;
418 }
419
420 static void
421 fs_promiscuous_enable(struct rte_eth_dev *dev)
422 {
423         struct sub_device *sdev;
424         uint8_t i;
425
426         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
427                 rte_eth_promiscuous_enable(PORT_ID(sdev));
428 }
429
430 static void
431 fs_promiscuous_disable(struct rte_eth_dev *dev)
432 {
433         struct sub_device *sdev;
434         uint8_t i;
435
436         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
437                 rte_eth_promiscuous_disable(PORT_ID(sdev));
438 }
439
440 static void
441 fs_allmulticast_enable(struct rte_eth_dev *dev)
442 {
443         struct sub_device *sdev;
444         uint8_t i;
445
446         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
447                 rte_eth_allmulticast_enable(PORT_ID(sdev));
448 }
449
450 static void
451 fs_allmulticast_disable(struct rte_eth_dev *dev)
452 {
453         struct sub_device *sdev;
454         uint8_t i;
455
456         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
457                 rte_eth_allmulticast_disable(PORT_ID(sdev));
458 }
459
460 static int
461 fs_link_update(struct rte_eth_dev *dev,
462                 int wait_to_complete)
463 {
464         struct sub_device *sdev;
465         uint8_t i;
466         int ret;
467
468         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
469                 DEBUG("Calling link_update on sub_device %d", i);
470                 ret = (SUBOPS(sdev, link_update))(ETH(sdev), wait_to_complete);
471                 if (ret && ret != -1) {
472                         ERROR("Link update failed for sub_device %d with error %d",
473                               i, ret);
474                         return ret;
475                 }
476         }
477         if (TX_SUBDEV(dev)) {
478                 struct rte_eth_link *l1;
479                 struct rte_eth_link *l2;
480
481                 l1 = &dev->data->dev_link;
482                 l2 = &ETH(TX_SUBDEV(dev))->data->dev_link;
483                 if (memcmp(l1, l2, sizeof(*l1))) {
484                         *l1 = *l2;
485                         return 0;
486                 }
487         }
488         return -1;
489 }
490
491 static int
492 fs_stats_get(struct rte_eth_dev *dev,
493              struct rte_eth_stats *stats)
494 {
495         struct sub_device *sdev;
496         uint8_t i;
497         int ret;
498
499         rte_memcpy(stats, &PRIV(dev)->stats_accumulator, sizeof(*stats));
500         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
501                 struct rte_eth_stats *snapshot = &sdev->stats_snapshot.stats;
502                 uint64_t *timestamp = &sdev->stats_snapshot.timestamp;
503
504                 ret = rte_eth_stats_get(PORT_ID(sdev), snapshot);
505                 if (ret) {
506                         ERROR("Operation rte_eth_stats_get failed for sub_device %d with error %d",
507                                   i, ret);
508                         *timestamp = 0;
509                         return ret;
510                 }
511                 *timestamp = rte_rdtsc();
512                 failsafe_stats_increment(stats, snapshot);
513         }
514         return 0;
515 }
516
517 static void
518 fs_stats_reset(struct rte_eth_dev *dev)
519 {
520         struct sub_device *sdev;
521         uint8_t i;
522
523         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
524                 rte_eth_stats_reset(PORT_ID(sdev));
525                 memset(&sdev->stats_snapshot, 0, sizeof(struct rte_eth_stats));
526         }
527         memset(&PRIV(dev)->stats_accumulator, 0, sizeof(struct rte_eth_stats));
528 }
529
530 /**
531  * Fail-safe dev_infos_get rules:
532  *
533  * No sub_device:
534  *   Numerables:
535  *      Use the maximum possible values for any field, so as not
536  *      to impede any further configuration effort.
537  *   Capabilities:
538  *      Limits capabilities to those that are understood by the
539  *      fail-safe PMD. This understanding stems from the fail-safe
540  *      being capable of verifying that the related capability is
541  *      expressed within the device configuration (struct rte_eth_conf).
542  *
543  * At least one probed sub_device:
544  *   Numerables:
545  *      Uses values from the active probed sub_device
546  *      The rationale here is that if any sub_device is less capable
547  *      (for example concerning the number of queues) than the active
548  *      sub_device, then its subsequent configuration will fail.
549  *      It is impossible to foresee this failure when the failing sub_device
550  *      is supposed to be plugged-in later on, so the configuration process
551  *      is the single point of failure and error reporting.
552  *   Capabilities:
553  *      Uses a logical AND of RX capabilities among
554  *      all sub_devices and the default capabilities.
555  *      Uses a logical AND of TX capabilities among
556  *      the active probed sub_device and the default capabilities.
557  *
558  */
559 static void
560 fs_dev_infos_get(struct rte_eth_dev *dev,
561                   struct rte_eth_dev_info *infos)
562 {
563         struct sub_device *sdev;
564         uint8_t i;
565
566         sdev = TX_SUBDEV(dev);
567         if (sdev == NULL) {
568                 DEBUG("No probed device, using default infos");
569                 rte_memcpy(&PRIV(dev)->infos, &default_infos,
570                            sizeof(default_infos));
571         } else {
572                 uint32_t rx_offload_capa;
573
574                 rx_offload_capa = default_infos.rx_offload_capa;
575                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
576                         rte_eth_dev_info_get(PORT_ID(sdev),
577                                         &PRIV(dev)->infos);
578                         rx_offload_capa &= PRIV(dev)->infos.rx_offload_capa;
579                 }
580                 sdev = TX_SUBDEV(dev);
581                 rte_eth_dev_info_get(PORT_ID(sdev), &PRIV(dev)->infos);
582                 PRIV(dev)->infos.rx_offload_capa = rx_offload_capa;
583                 PRIV(dev)->infos.tx_offload_capa &=
584                                         default_infos.tx_offload_capa;
585                 PRIV(dev)->infos.flow_type_rss_offloads &=
586                                         default_infos.flow_type_rss_offloads;
587         }
588         rte_memcpy(infos, &PRIV(dev)->infos, sizeof(*infos));
589 }
590
591 static const uint32_t *
592 fs_dev_supported_ptypes_get(struct rte_eth_dev *dev)
593 {
594         struct sub_device *sdev;
595         struct rte_eth_dev *edev;
596
597         sdev = TX_SUBDEV(dev);
598         if (sdev == NULL)
599                 return NULL;
600         edev = ETH(sdev);
601         /* ENOTSUP: counts as no supported ptypes */
602         if (SUBOPS(sdev, dev_supported_ptypes_get) == NULL)
603                 return NULL;
604         /*
605          * The API does not permit to do a clean AND of all ptypes,
606          * It is also incomplete by design and we do not really care
607          * to have a best possible value in this context.
608          * We just return the ptypes of the device of highest
609          * priority, usually the PREFERRED device.
610          */
611         return SUBOPS(sdev, dev_supported_ptypes_get)(edev);
612 }
613
614 static int
615 fs_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
616 {
617         struct sub_device *sdev;
618         uint8_t i;
619         int ret;
620
621         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
622                 DEBUG("Calling rte_eth_dev_set_mtu on sub_device %d", i);
623                 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), mtu);
624                 if (ret) {
625                         ERROR("Operation rte_eth_dev_set_mtu failed for sub_device %d with error %d",
626                               i, ret);
627                         return ret;
628                 }
629         }
630         return 0;
631 }
632
633 static int
634 fs_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
635 {
636         struct sub_device *sdev;
637         uint8_t i;
638         int ret;
639
640         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
641                 DEBUG("Calling rte_eth_dev_vlan_filter on sub_device %d", i);
642                 ret = rte_eth_dev_vlan_filter(PORT_ID(sdev), vlan_id, on);
643                 if (ret) {
644                         ERROR("Operation rte_eth_dev_vlan_filter failed for sub_device %d"
645                               " with error %d", i, ret);
646                         return ret;
647                 }
648         }
649         return 0;
650 }
651
652 static int
653 fs_flow_ctrl_get(struct rte_eth_dev *dev,
654                 struct rte_eth_fc_conf *fc_conf)
655 {
656         struct sub_device *sdev;
657
658         sdev = TX_SUBDEV(dev);
659         if (sdev == NULL)
660                 return 0;
661         if (SUBOPS(sdev, flow_ctrl_get) == NULL)
662                 return -ENOTSUP;
663         return SUBOPS(sdev, flow_ctrl_get)(ETH(sdev), fc_conf);
664 }
665
666 static int
667 fs_flow_ctrl_set(struct rte_eth_dev *dev,
668                 struct rte_eth_fc_conf *fc_conf)
669 {
670         struct sub_device *sdev;
671         uint8_t i;
672         int ret;
673
674         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
675                 DEBUG("Calling rte_eth_dev_flow_ctrl_set on sub_device %d", i);
676                 ret = rte_eth_dev_flow_ctrl_set(PORT_ID(sdev), fc_conf);
677                 if (ret) {
678                         ERROR("Operation rte_eth_dev_flow_ctrl_set failed for sub_device %d"
679                               " with error %d", i, ret);
680                         return ret;
681                 }
682         }
683         return 0;
684 }
685
686 static void
687 fs_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
688 {
689         struct sub_device *sdev;
690         uint8_t i;
691
692         /* No check: already done within the rte_eth_dev_mac_addr_remove
693          * call for the fail-safe device.
694          */
695         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
696                 rte_eth_dev_mac_addr_remove(PORT_ID(sdev),
697                                 &dev->data->mac_addrs[index]);
698         PRIV(dev)->mac_addr_pool[index] = 0;
699 }
700
701 static int
702 fs_mac_addr_add(struct rte_eth_dev *dev,
703                 struct ether_addr *mac_addr,
704                 uint32_t index,
705                 uint32_t vmdq)
706 {
707         struct sub_device *sdev;
708         int ret;
709         uint8_t i;
710
711         RTE_ASSERT(index < FAILSAFE_MAX_ETHADDR);
712         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
713                 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), mac_addr, vmdq);
714                 if (ret) {
715                         ERROR("Operation rte_eth_dev_mac_addr_add failed for sub_device %"
716                               PRIu8 " with error %d", i, ret);
717                         return ret;
718                 }
719         }
720         if (index >= PRIV(dev)->nb_mac_addr) {
721                 DEBUG("Growing mac_addrs array");
722                 PRIV(dev)->nb_mac_addr = index;
723         }
724         PRIV(dev)->mac_addr_pool[index] = vmdq;
725         return 0;
726 }
727
728 static void
729 fs_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
730 {
731         struct sub_device *sdev;
732         uint8_t i;
733
734         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
735                 rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), mac_addr);
736 }
737
738 static int
739 fs_filter_ctrl(struct rte_eth_dev *dev,
740                 enum rte_filter_type type,
741                 enum rte_filter_op op,
742                 void *arg)
743 {
744         struct sub_device *sdev;
745         uint8_t i;
746         int ret;
747
748         if (type == RTE_ETH_FILTER_GENERIC &&
749             op == RTE_ETH_FILTER_GET) {
750                 *(const void **)arg = &fs_flow_ops;
751                 return 0;
752         }
753         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
754                 DEBUG("Calling rte_eth_dev_filter_ctrl on sub_device %d", i);
755                 ret = rte_eth_dev_filter_ctrl(PORT_ID(sdev), type, op, arg);
756                 if (ret) {
757                         ERROR("Operation rte_eth_dev_filter_ctrl failed for sub_device %d"
758                               " with error %d", i, ret);
759                         return ret;
760                 }
761         }
762         return 0;
763 }
764
765 const struct eth_dev_ops failsafe_ops = {
766         .dev_configure = fs_dev_configure,
767         .dev_start = fs_dev_start,
768         .dev_stop = fs_dev_stop,
769         .dev_set_link_down = fs_dev_set_link_down,
770         .dev_set_link_up = fs_dev_set_link_up,
771         .dev_close = fs_dev_close,
772         .promiscuous_enable = fs_promiscuous_enable,
773         .promiscuous_disable = fs_promiscuous_disable,
774         .allmulticast_enable = fs_allmulticast_enable,
775         .allmulticast_disable = fs_allmulticast_disable,
776         .link_update = fs_link_update,
777         .stats_get = fs_stats_get,
778         .stats_reset = fs_stats_reset,
779         .dev_infos_get = fs_dev_infos_get,
780         .dev_supported_ptypes_get = fs_dev_supported_ptypes_get,
781         .mtu_set = fs_mtu_set,
782         .vlan_filter_set = fs_vlan_filter_set,
783         .rx_queue_setup = fs_rx_queue_setup,
784         .tx_queue_setup = fs_tx_queue_setup,
785         .rx_queue_release = fs_rx_queue_release,
786         .tx_queue_release = fs_tx_queue_release,
787         .flow_ctrl_get = fs_flow_ctrl_get,
788         .flow_ctrl_set = fs_flow_ctrl_set,
789         .mac_addr_remove = fs_mac_addr_remove,
790         .mac_addr_add = fs_mac_addr_add,
791         .mac_addr_set = fs_mac_addr_set,
792         .filter_ctrl = fs_filter_ctrl,
793 };