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