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