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