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