93ebd09114b4aba1a4ad5abe406c2a0056588384
[dpdk.git] / drivers / net / failsafe / failsafe_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <unistd.h>
9
10 #include <rte_debug.h>
11 #include <rte_atomic.h>
12 #include <rte_ethdev_driver.h>
13 #include <rte_malloc.h>
14 #include <rte_flow.h>
15 #include <rte_cycles.h>
16 #include <rte_ethdev.h>
17 #include <rte_string_fns.h>
18
19 #include "failsafe_private.h"
20
21 static int
22 fs_dev_configure(struct rte_eth_dev *dev)
23 {
24         struct sub_device *sdev;
25         uint8_t i;
26         int ret;
27
28         fs_lock(dev, 0);
29         FOREACH_SUBDEV(sdev, i, dev) {
30                 int rmv_interrupt = 0;
31                 int lsc_interrupt = 0;
32                 int lsc_enabled;
33
34                 if (sdev->state != DEV_PROBED &&
35                     !(PRIV(dev)->alarm_lock == 0 && sdev->state == DEV_ACTIVE))
36                         continue;
37
38                 rmv_interrupt = ETH(sdev)->data->dev_flags &
39                                 RTE_ETH_DEV_INTR_RMV;
40                 if (rmv_interrupt) {
41                         DEBUG("Enabling RMV interrupts for sub_device %d", i);
42                         dev->data->dev_conf.intr_conf.rmv = 1;
43                 } else {
44                         DEBUG("sub_device %d does not support RMV event", i);
45                 }
46                 lsc_enabled = dev->data->dev_conf.intr_conf.lsc;
47                 lsc_interrupt = lsc_enabled &&
48                                 (ETH(sdev)->data->dev_flags &
49                                  RTE_ETH_DEV_INTR_LSC);
50                 if (lsc_interrupt) {
51                         DEBUG("Enabling LSC interrupts for sub_device %d", i);
52                         dev->data->dev_conf.intr_conf.lsc = 1;
53                 } else if (lsc_enabled && !lsc_interrupt) {
54                         DEBUG("Disabling LSC interrupts for sub_device %d", i);
55                         dev->data->dev_conf.intr_conf.lsc = 0;
56                 }
57                 DEBUG("Configuring sub-device %d", i);
58                 ret = rte_eth_dev_configure(PORT_ID(sdev),
59                                         dev->data->nb_rx_queues,
60                                         dev->data->nb_tx_queues,
61                                         &dev->data->dev_conf);
62                 if (ret) {
63                         if (!fs_err(sdev, ret))
64                                 continue;
65                         ERROR("Could not configure sub_device %d", i);
66                         fs_unlock(dev, 0);
67                         return ret;
68                 }
69                 if (rmv_interrupt && sdev->rmv_callback == 0) {
70                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
71                                         RTE_ETH_EVENT_INTR_RMV,
72                                         failsafe_eth_rmv_event_callback,
73                                         sdev);
74                         if (ret)
75                                 WARN("Failed to register RMV callback for sub_device %d",
76                                      SUB_ID(sdev));
77                         else
78                                 sdev->rmv_callback = 1;
79                 }
80                 dev->data->dev_conf.intr_conf.rmv = 0;
81                 if (lsc_interrupt && sdev->lsc_callback == 0) {
82                         ret = rte_eth_dev_callback_register(PORT_ID(sdev),
83                                                 RTE_ETH_EVENT_INTR_LSC,
84                                                 failsafe_eth_lsc_event_callback,
85                                                 dev);
86                         if (ret)
87                                 WARN("Failed to register LSC callback for sub_device %d",
88                                      SUB_ID(sdev));
89                         else
90                                 sdev->lsc_callback = 1;
91                 }
92                 dev->data->dev_conf.intr_conf.lsc = lsc_enabled;
93                 sdev->state = DEV_ACTIVE;
94         }
95         if (PRIV(dev)->state < DEV_ACTIVE)
96                 PRIV(dev)->state = DEV_ACTIVE;
97         fs_unlock(dev, 0);
98         return 0;
99 }
100
101 static void
102 fs_set_queues_state_start(struct rte_eth_dev *dev)
103 {
104         struct rxq *rxq;
105         struct txq *txq;
106         uint16_t i;
107
108         for (i = 0; i < dev->data->nb_rx_queues; i++) {
109                 rxq = dev->data->rx_queues[i];
110                 if (rxq != NULL && !rxq->info.conf.rx_deferred_start)
111                         dev->data->rx_queue_state[i] =
112                                                 RTE_ETH_QUEUE_STATE_STARTED;
113         }
114         for (i = 0; i < dev->data->nb_tx_queues; i++) {
115                 txq = dev->data->tx_queues[i];
116                 if (txq != NULL && !txq->info.conf.tx_deferred_start)
117                         dev->data->tx_queue_state[i] =
118                                                 RTE_ETH_QUEUE_STATE_STARTED;
119         }
120 }
121
122 static int
123 fs_dev_start(struct rte_eth_dev *dev)
124 {
125         struct sub_device *sdev;
126         uint8_t i;
127         int ret;
128
129         fs_lock(dev, 0);
130         ret = failsafe_rx_intr_install(dev);
131         if (ret) {
132                 fs_unlock(dev, 0);
133                 return ret;
134         }
135         FOREACH_SUBDEV(sdev, i, dev) {
136                 if (sdev->state != DEV_ACTIVE)
137                         continue;
138                 DEBUG("Starting sub_device %d", i);
139                 ret = rte_eth_dev_start(PORT_ID(sdev));
140                 if (ret) {
141                         if (!fs_err(sdev, ret))
142                                 continue;
143                         fs_unlock(dev, 0);
144                         return ret;
145                 }
146                 ret = failsafe_rx_intr_install_subdevice(sdev);
147                 if (ret) {
148                         if (!fs_err(sdev, ret))
149                                 continue;
150                         rte_eth_dev_stop(PORT_ID(sdev));
151                         fs_unlock(dev, 0);
152                         return ret;
153                 }
154                 sdev->state = DEV_STARTED;
155         }
156         if (PRIV(dev)->state < DEV_STARTED) {
157                 PRIV(dev)->state = DEV_STARTED;
158                 fs_set_queues_state_start(dev);
159         }
160         fs_switch_dev(dev, NULL);
161         fs_unlock(dev, 0);
162         return 0;
163 }
164
165 static void
166 fs_set_queues_state_stop(struct rte_eth_dev *dev)
167 {
168         uint16_t i;
169
170         for (i = 0; i < dev->data->nb_rx_queues; i++)
171                 if (dev->data->rx_queues[i] != NULL)
172                         dev->data->rx_queue_state[i] =
173                                                 RTE_ETH_QUEUE_STATE_STOPPED;
174         for (i = 0; i < dev->data->nb_tx_queues; i++)
175                 if (dev->data->tx_queues[i] != NULL)
176                         dev->data->tx_queue_state[i] =
177                                                 RTE_ETH_QUEUE_STATE_STOPPED;
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         fs_lock(dev, 0);
187         PRIV(dev)->state = DEV_STARTED - 1;
188         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_STARTED) {
189                 rte_eth_dev_stop(PORT_ID(sdev));
190                 failsafe_rx_intr_uninstall_subdevice(sdev);
191                 sdev->state = DEV_STARTED - 1;
192         }
193         failsafe_rx_intr_uninstall(dev);
194         fs_set_queues_state_stop(dev);
195         fs_unlock(dev, 0);
196 }
197
198 static int
199 fs_dev_set_link_up(struct rte_eth_dev *dev)
200 {
201         struct sub_device *sdev;
202         uint8_t i;
203         int ret;
204
205         fs_lock(dev, 0);
206         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
207                 DEBUG("Calling rte_eth_dev_set_link_up on sub_device %d", i);
208                 ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
209                 if ((ret = fs_err(sdev, ret))) {
210                         ERROR("Operation rte_eth_dev_set_link_up failed for sub_device %d"
211                               " with error %d", i, ret);
212                         fs_unlock(dev, 0);
213                         return ret;
214                 }
215         }
216         fs_unlock(dev, 0);
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         fs_lock(dev, 0);
228         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
229                 DEBUG("Calling rte_eth_dev_set_link_down on sub_device %d", i);
230                 ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
231                 if ((ret = fs_err(sdev, ret))) {
232                         ERROR("Operation rte_eth_dev_set_link_down failed for sub_device %d"
233                               " with error %d", i, ret);
234                         fs_unlock(dev, 0);
235                         return ret;
236                 }
237         }
238         fs_unlock(dev, 0);
239         return 0;
240 }
241
242 static void fs_dev_free_queues(struct rte_eth_dev *dev);
243 static int
244 fs_dev_close(struct rte_eth_dev *dev)
245 {
246         struct sub_device *sdev;
247         uint8_t i;
248
249         fs_lock(dev, 0);
250         failsafe_hotplug_alarm_cancel(dev);
251         if (PRIV(dev)->state == DEV_STARTED)
252                 dev->dev_ops->dev_stop(dev);
253         PRIV(dev)->state = DEV_ACTIVE - 1;
254         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
255                 DEBUG("Closing sub_device %d", i);
256                 failsafe_eth_dev_unregister_callbacks(sdev);
257                 rte_eth_dev_close(PORT_ID(sdev));
258                 sdev->state = DEV_ACTIVE - 1;
259         }
260         fs_dev_free_queues(dev);
261         fs_unlock(dev, 0);
262         return 0;
263 }
264
265 static int
266 fs_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
267 {
268         struct sub_device *sdev;
269         uint8_t i;
270         int ret;
271         int err = 0;
272         bool failure = true;
273
274         fs_lock(dev, 0);
275         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
276                 uint16_t port_id = ETH(sdev)->data->port_id;
277
278                 ret = rte_eth_dev_rx_queue_stop(port_id, rx_queue_id);
279                 ret = fs_err(sdev, ret);
280                 if (ret) {
281                         ERROR("Rx queue stop failed for subdevice %d", i);
282                         err = ret;
283                 } else {
284                         failure = false;
285                 }
286         }
287         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
288         fs_unlock(dev, 0);
289         /* Return 0 in case of at least one successful queue stop */
290         return (failure) ? err : 0;
291 }
292
293 static int
294 fs_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
295 {
296         struct sub_device *sdev;
297         uint8_t i;
298         int ret;
299
300         fs_lock(dev, 0);
301         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
302                 uint16_t port_id = ETH(sdev)->data->port_id;
303
304                 ret = rte_eth_dev_rx_queue_start(port_id, rx_queue_id);
305                 ret = fs_err(sdev, ret);
306                 if (ret) {
307                         ERROR("Rx queue start failed for subdevice %d", i);
308                         fs_rx_queue_stop(dev, rx_queue_id);
309                         fs_unlock(dev, 0);
310                         return ret;
311                 }
312         }
313         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
314         fs_unlock(dev, 0);
315         return 0;
316 }
317
318 static int
319 fs_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
320 {
321         struct sub_device *sdev;
322         uint8_t i;
323         int ret;
324         int err = 0;
325         bool failure = true;
326
327         fs_lock(dev, 0);
328         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
329                 uint16_t port_id = ETH(sdev)->data->port_id;
330
331                 ret = rte_eth_dev_tx_queue_stop(port_id, tx_queue_id);
332                 ret = fs_err(sdev, ret);
333                 if (ret) {
334                         ERROR("Tx queue stop failed for subdevice %d", i);
335                         err = ret;
336                 } else {
337                         failure = false;
338                 }
339         }
340         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
341         fs_unlock(dev, 0);
342         /* Return 0 in case of at least one successful queue stop */
343         return (failure) ? err : 0;
344 }
345
346 static int
347 fs_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
348 {
349         struct sub_device *sdev;
350         uint8_t i;
351         int ret;
352
353         fs_lock(dev, 0);
354         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
355                 uint16_t port_id = ETH(sdev)->data->port_id;
356
357                 ret = rte_eth_dev_tx_queue_start(port_id, tx_queue_id);
358                 ret = fs_err(sdev, ret);
359                 if (ret) {
360                         ERROR("Tx queue start failed for subdevice %d", i);
361                         fs_tx_queue_stop(dev, tx_queue_id);
362                         fs_unlock(dev, 0);
363                         return ret;
364                 }
365         }
366         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
367         fs_unlock(dev, 0);
368         return 0;
369 }
370
371 static void
372 fs_rx_queue_release(void *queue)
373 {
374         struct rte_eth_dev *dev;
375         struct sub_device *sdev;
376         uint8_t i;
377         struct rxq *rxq;
378
379         if (queue == NULL)
380                 return;
381         rxq = queue;
382         dev = &rte_eth_devices[rxq->priv->data->port_id];
383         fs_lock(dev, 0);
384         if (rxq->event_fd >= 0)
385                 close(rxq->event_fd);
386         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
387                 if (ETH(sdev)->data->rx_queues != NULL &&
388                     ETH(sdev)->data->rx_queues[rxq->qid] != NULL) {
389                         SUBOPS(sdev, rx_queue_release)
390                                 (ETH(sdev)->data->rx_queues[rxq->qid]);
391                 }
392         }
393         dev->data->rx_queues[rxq->qid] = NULL;
394         rte_free(rxq);
395         fs_unlock(dev, 0);
396 }
397
398 static int
399 fs_rx_queue_setup(struct rte_eth_dev *dev,
400                 uint16_t rx_queue_id,
401                 uint16_t nb_rx_desc,
402                 unsigned int socket_id,
403                 const struct rte_eth_rxconf *rx_conf,
404                 struct rte_mempool *mb_pool)
405 {
406         /*
407          * FIXME: Add a proper interface in rte_eal_interrupts for
408          * allocating eventfd as an interrupt vector.
409          * For the time being, fake as if we are using MSIX interrupts,
410          * this will cause rte_intr_efd_enable to allocate an eventfd for us.
411          */
412         struct rte_intr_handle intr_handle = {
413                 .type = RTE_INTR_HANDLE_VFIO_MSIX,
414                 .efds = { -1, },
415         };
416         struct sub_device *sdev;
417         struct rxq *rxq;
418         uint8_t i;
419         int ret;
420
421         fs_lock(dev, 0);
422         if (rx_conf->rx_deferred_start) {
423                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
424                         if (SUBOPS(sdev, rx_queue_start) == NULL) {
425                                 ERROR("Rx queue deferred start is not "
426                                         "supported for subdevice %d", i);
427                                 fs_unlock(dev, 0);
428                                 return -EINVAL;
429                         }
430                 }
431         }
432         rxq = dev->data->rx_queues[rx_queue_id];
433         if (rxq != NULL) {
434                 fs_rx_queue_release(rxq);
435                 dev->data->rx_queues[rx_queue_id] = NULL;
436         }
437         rxq = rte_zmalloc(NULL,
438                           sizeof(*rxq) +
439                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
440                           RTE_CACHE_LINE_SIZE);
441         if (rxq == NULL) {
442                 fs_unlock(dev, 0);
443                 return -ENOMEM;
444         }
445         FOREACH_SUBDEV(sdev, i, dev)
446                 rte_atomic64_init(&rxq->refcnt[i]);
447         rxq->qid = rx_queue_id;
448         rxq->socket_id = socket_id;
449         rxq->info.mp = mb_pool;
450         rxq->info.conf = *rx_conf;
451         rxq->info.nb_desc = nb_rx_desc;
452         rxq->priv = PRIV(dev);
453         rxq->sdev = PRIV(dev)->subs;
454         ret = rte_intr_efd_enable(&intr_handle, 1);
455         if (ret < 0) {
456                 fs_unlock(dev, 0);
457                 return ret;
458         }
459         rxq->event_fd = intr_handle.efds[0];
460         dev->data->rx_queues[rx_queue_id] = rxq;
461         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
462                 ret = rte_eth_rx_queue_setup(PORT_ID(sdev),
463                                 rx_queue_id,
464                                 nb_rx_desc, socket_id,
465                                 rx_conf, mb_pool);
466                 if ((ret = fs_err(sdev, ret))) {
467                         ERROR("RX queue setup failed for sub_device %d", i);
468                         goto free_rxq;
469                 }
470         }
471         fs_unlock(dev, 0);
472         return 0;
473 free_rxq:
474         fs_rx_queue_release(rxq);
475         fs_unlock(dev, 0);
476         return ret;
477 }
478
479 static int
480 fs_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
481 {
482         struct rxq *rxq;
483         struct sub_device *sdev;
484         uint8_t i;
485         int ret;
486         int rc = 0;
487
488         fs_lock(dev, 0);
489         if (idx >= dev->data->nb_rx_queues) {
490                 rc = -EINVAL;
491                 goto unlock;
492         }
493         rxq = dev->data->rx_queues[idx];
494         if (rxq == NULL || rxq->event_fd <= 0) {
495                 rc = -EINVAL;
496                 goto unlock;
497         }
498         /* Fail if proxy service is nor running. */
499         if (PRIV(dev)->rxp.sstate != SS_RUNNING) {
500                 ERROR("failsafe interrupt services are not running");
501                 rc = -EAGAIN;
502                 goto unlock;
503         }
504         rxq->enable_events = 1;
505         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
506                 ret = rte_eth_dev_rx_intr_enable(PORT_ID(sdev), idx);
507                 ret = fs_err(sdev, ret);
508                 if (ret)
509                         rc = ret;
510         }
511 unlock:
512         fs_unlock(dev, 0);
513         if (rc)
514                 rte_errno = -rc;
515         return rc;
516 }
517
518 static int
519 fs_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
520 {
521         struct rxq *rxq;
522         struct sub_device *sdev;
523         uint64_t u64;
524         uint8_t i;
525         int rc = 0;
526         int ret;
527
528         fs_lock(dev, 0);
529         if (idx >= dev->data->nb_rx_queues) {
530                 rc = -EINVAL;
531                 goto unlock;
532         }
533         rxq = dev->data->rx_queues[idx];
534         if (rxq == NULL || rxq->event_fd <= 0) {
535                 rc = -EINVAL;
536                 goto unlock;
537         }
538         rxq->enable_events = 0;
539         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
540                 ret = rte_eth_dev_rx_intr_disable(PORT_ID(sdev), idx);
541                 ret = fs_err(sdev, ret);
542                 if (ret)
543                         rc = ret;
544         }
545         /* Clear pending events */
546         while (read(rxq->event_fd, &u64, sizeof(uint64_t)) >  0)
547                 ;
548 unlock:
549         fs_unlock(dev, 0);
550         if (rc)
551                 rte_errno = -rc;
552         return rc;
553 }
554
555 static void
556 fs_tx_queue_release(void *queue)
557 {
558         struct rte_eth_dev *dev;
559         struct sub_device *sdev;
560         uint8_t i;
561         struct txq *txq;
562
563         if (queue == NULL)
564                 return;
565         txq = queue;
566         dev = &rte_eth_devices[txq->priv->data->port_id];
567         fs_lock(dev, 0);
568         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
569                 if (ETH(sdev)->data->tx_queues != NULL &&
570                     ETH(sdev)->data->tx_queues[txq->qid] != NULL) {
571                         SUBOPS(sdev, tx_queue_release)
572                                 (ETH(sdev)->data->tx_queues[txq->qid]);
573                 }
574         }
575         dev->data->tx_queues[txq->qid] = NULL;
576         rte_free(txq);
577         fs_unlock(dev, 0);
578 }
579
580 static int
581 fs_tx_queue_setup(struct rte_eth_dev *dev,
582                 uint16_t tx_queue_id,
583                 uint16_t nb_tx_desc,
584                 unsigned int socket_id,
585                 const struct rte_eth_txconf *tx_conf)
586 {
587         struct sub_device *sdev;
588         struct txq *txq;
589         uint8_t i;
590         int ret;
591
592         fs_lock(dev, 0);
593         if (tx_conf->tx_deferred_start) {
594                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
595                         if (SUBOPS(sdev, tx_queue_start) == NULL) {
596                                 ERROR("Tx queue deferred start is not "
597                                         "supported for subdevice %d", i);
598                                 fs_unlock(dev, 0);
599                                 return -EINVAL;
600                         }
601                 }
602         }
603         txq = dev->data->tx_queues[tx_queue_id];
604         if (txq != NULL) {
605                 fs_tx_queue_release(txq);
606                 dev->data->tx_queues[tx_queue_id] = NULL;
607         }
608         txq = rte_zmalloc("ethdev TX queue",
609                           sizeof(*txq) +
610                           sizeof(rte_atomic64_t) * PRIV(dev)->subs_tail,
611                           RTE_CACHE_LINE_SIZE);
612         if (txq == NULL) {
613                 fs_unlock(dev, 0);
614                 return -ENOMEM;
615         }
616         FOREACH_SUBDEV(sdev, i, dev)
617                 rte_atomic64_init(&txq->refcnt[i]);
618         txq->qid = tx_queue_id;
619         txq->socket_id = socket_id;
620         txq->info.conf = *tx_conf;
621         txq->info.nb_desc = nb_tx_desc;
622         txq->priv = PRIV(dev);
623         dev->data->tx_queues[tx_queue_id] = txq;
624         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
625                 ret = rte_eth_tx_queue_setup(PORT_ID(sdev),
626                                 tx_queue_id,
627                                 nb_tx_desc, socket_id,
628                                 tx_conf);
629                 if ((ret = fs_err(sdev, ret))) {
630                         ERROR("TX queue setup failed for sub_device %d", i);
631                         goto free_txq;
632                 }
633         }
634         fs_unlock(dev, 0);
635         return 0;
636 free_txq:
637         fs_tx_queue_release(txq);
638         fs_unlock(dev, 0);
639         return ret;
640 }
641
642 static void
643 fs_dev_free_queues(struct rte_eth_dev *dev)
644 {
645         uint16_t i;
646
647         for (i = 0; i < dev->data->nb_rx_queues; i++) {
648                 fs_rx_queue_release(dev->data->rx_queues[i]);
649                 dev->data->rx_queues[i] = NULL;
650         }
651         dev->data->nb_rx_queues = 0;
652         for (i = 0; i < dev->data->nb_tx_queues; i++) {
653                 fs_tx_queue_release(dev->data->tx_queues[i]);
654                 dev->data->tx_queues[i] = NULL;
655         }
656         dev->data->nb_tx_queues = 0;
657 }
658
659 static int
660 fs_promiscuous_enable(struct rte_eth_dev *dev)
661 {
662         struct sub_device *sdev;
663         uint8_t i;
664         int ret = 0;
665
666         fs_lock(dev, 0);
667         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
668                 ret = rte_eth_promiscuous_enable(PORT_ID(sdev));
669                 ret = fs_err(sdev, ret);
670                 if (ret != 0) {
671                         ERROR("Promiscuous mode enable failed for subdevice %d",
672                                 PORT_ID(sdev));
673                         break;
674                 }
675         }
676         if (ret != 0) {
677                 /* Rollback in the case of failure */
678                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
679                         ret = rte_eth_promiscuous_disable(PORT_ID(sdev));
680                         ret = fs_err(sdev, ret);
681                         if (ret != 0)
682                                 ERROR("Promiscuous mode disable during rollback failed for subdevice %d",
683                                         PORT_ID(sdev));
684                 }
685         }
686         fs_unlock(dev, 0);
687
688         return ret;
689 }
690
691 static int
692 fs_promiscuous_disable(struct rte_eth_dev *dev)
693 {
694         struct sub_device *sdev;
695         uint8_t i;
696         int ret = 0;
697
698         fs_lock(dev, 0);
699         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
700                 ret = rte_eth_promiscuous_disable(PORT_ID(sdev));
701                 ret = fs_err(sdev, ret);
702                 if (ret != 0) {
703                         ERROR("Promiscuous mode disable failed for subdevice %d",
704                                 PORT_ID(sdev));
705                         break;
706                 }
707         }
708         if (ret != 0) {
709                 /* Rollback in the case of failure */
710                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
711                         ret = rte_eth_promiscuous_enable(PORT_ID(sdev));
712                         ret = fs_err(sdev, ret);
713                         if (ret != 0)
714                                 ERROR("Promiscuous mode enable during rollback failed for subdevice %d",
715                                         PORT_ID(sdev));
716                 }
717         }
718         fs_unlock(dev, 0);
719
720         return ret;
721 }
722
723 static int
724 fs_allmulticast_enable(struct rte_eth_dev *dev)
725 {
726         struct sub_device *sdev;
727         uint8_t i;
728         int ret = 0;
729
730         fs_lock(dev, 0);
731         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
732                 ret = rte_eth_allmulticast_enable(PORT_ID(sdev));
733                 ret = fs_err(sdev, ret);
734                 if (ret != 0) {
735                         ERROR("All-multicast mode enable failed for subdevice %d",
736                                 PORT_ID(sdev));
737                         break;
738                 }
739         }
740         if (ret != 0) {
741                 /* Rollback in the case of failure */
742                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
743                         ret = rte_eth_allmulticast_disable(PORT_ID(sdev));
744                         ret = fs_err(sdev, ret);
745                         if (ret != 0)
746                                 ERROR("All-multicast mode disable during rollback failed for subdevice %d",
747                                         PORT_ID(sdev));
748                 }
749         }
750         fs_unlock(dev, 0);
751
752         return ret;
753 }
754
755 static int
756 fs_allmulticast_disable(struct rte_eth_dev *dev)
757 {
758         struct sub_device *sdev;
759         uint8_t i;
760         int ret = 0;
761
762         fs_lock(dev, 0);
763         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
764                 ret = rte_eth_allmulticast_disable(PORT_ID(sdev));
765                 ret = fs_err(sdev, ret);
766                 if (ret != 0) {
767                         ERROR("All-multicast mode disable failed for subdevice %d",
768                                 PORT_ID(sdev));
769                         break;
770                 }
771         }
772         if (ret != 0) {
773                 /* Rollback in the case of failure */
774                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
775                         ret = rte_eth_allmulticast_enable(PORT_ID(sdev));
776                         ret = fs_err(sdev, ret);
777                         if (ret != 0)
778                                 ERROR("All-multicast mode enable during rollback failed for subdevice %d",
779                                         PORT_ID(sdev));
780                 }
781         }
782         fs_unlock(dev, 0);
783
784         return ret;
785 }
786
787 static int
788 fs_link_update(struct rte_eth_dev *dev,
789                 int wait_to_complete)
790 {
791         struct sub_device *sdev;
792         uint8_t i;
793         int ret;
794
795         fs_lock(dev, 0);
796         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
797                 DEBUG("Calling link_update on sub_device %d", i);
798                 ret = (SUBOPS(sdev, link_update))(ETH(sdev), wait_to_complete);
799                 if (ret && ret != -1 && sdev->remove == 0 &&
800                     rte_eth_dev_is_removed(PORT_ID(sdev)) == 0) {
801                         ERROR("Link update failed for sub_device %d with error %d",
802                               i, ret);
803                         fs_unlock(dev, 0);
804                         return ret;
805                 }
806         }
807         if (TX_SUBDEV(dev)) {
808                 struct rte_eth_link *l1;
809                 struct rte_eth_link *l2;
810
811                 l1 = &dev->data->dev_link;
812                 l2 = &ETH(TX_SUBDEV(dev))->data->dev_link;
813                 if (memcmp(l1, l2, sizeof(*l1))) {
814                         *l1 = *l2;
815                         fs_unlock(dev, 0);
816                         return 0;
817                 }
818         }
819         fs_unlock(dev, 0);
820         return -1;
821 }
822
823 static int
824 fs_stats_get(struct rte_eth_dev *dev,
825              struct rte_eth_stats *stats)
826 {
827         struct rte_eth_stats backup;
828         struct sub_device *sdev;
829         uint8_t i;
830         int ret;
831
832         fs_lock(dev, 0);
833         rte_memcpy(stats, &PRIV(dev)->stats_accumulator, sizeof(*stats));
834         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
835                 struct rte_eth_stats *snapshot = &sdev->stats_snapshot.stats;
836                 uint64_t *timestamp = &sdev->stats_snapshot.timestamp;
837
838                 rte_memcpy(&backup, snapshot, sizeof(backup));
839                 ret = rte_eth_stats_get(PORT_ID(sdev), snapshot);
840                 if (ret) {
841                         if (!fs_err(sdev, ret)) {
842                                 rte_memcpy(snapshot, &backup, sizeof(backup));
843                                 goto inc;
844                         }
845                         ERROR("Operation rte_eth_stats_get failed for sub_device %d with error %d",
846                                   i, ret);
847                         *timestamp = 0;
848                         fs_unlock(dev, 0);
849                         return ret;
850                 }
851                 *timestamp = rte_rdtsc();
852 inc:
853                 failsafe_stats_increment(stats, snapshot);
854         }
855         fs_unlock(dev, 0);
856         return 0;
857 }
858
859 static int
860 fs_stats_reset(struct rte_eth_dev *dev)
861 {
862         struct sub_device *sdev;
863         uint8_t i;
864         int ret;
865
866         fs_lock(dev, 0);
867         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
868                 ret = rte_eth_stats_reset(PORT_ID(sdev));
869                 if (ret) {
870                         if (!fs_err(sdev, ret))
871                                 continue;
872
873                         ERROR("Operation rte_eth_stats_reset failed for sub_device %d with error %d",
874                               i, ret);
875                         fs_unlock(dev, 0);
876                         return ret;
877                 }
878                 memset(&sdev->stats_snapshot, 0, sizeof(struct rte_eth_stats));
879         }
880         memset(&PRIV(dev)->stats_accumulator, 0, sizeof(struct rte_eth_stats));
881         fs_unlock(dev, 0);
882
883         return 0;
884 }
885
886 static int
887 __fs_xstats_count(struct rte_eth_dev *dev)
888 {
889         struct sub_device *sdev;
890         int count = 0;
891         uint8_t i;
892         int ret;
893
894         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
895                 ret = rte_eth_xstats_get_names(PORT_ID(sdev), NULL, 0);
896                 if (ret < 0)
897                         return ret;
898                 count += ret;
899         }
900
901         return count;
902 }
903
904 static int
905 __fs_xstats_get_names(struct rte_eth_dev *dev,
906                     struct rte_eth_xstat_name *xstats_names,
907                     unsigned int limit)
908 {
909         struct sub_device *sdev;
910         unsigned int count = 0;
911         uint8_t i;
912
913         /* Caller only cares about count */
914         if (!xstats_names)
915                 return  __fs_xstats_count(dev);
916
917         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
918                 struct rte_eth_xstat_name *sub_names = xstats_names + count;
919                 int j, r;
920
921                 if (count >= limit)
922                         break;
923
924                 r = rte_eth_xstats_get_names(PORT_ID(sdev),
925                                              sub_names, limit - count);
926                 if (r < 0)
927                         return r;
928
929                 /* add subN_ prefix to names */
930                 for (j = 0; j < r; j++) {
931                         char *xname = sub_names[j].name;
932                         char tmp[RTE_ETH_XSTATS_NAME_SIZE];
933
934                         if ((xname[0] == 't' || xname[0] == 'r') &&
935                             xname[1] == 'x' && xname[2] == '_')
936                                 snprintf(tmp, sizeof(tmp), "%.3ssub%u_%s",
937                                          xname, i, xname + 3);
938                         else
939                                 snprintf(tmp, sizeof(tmp), "sub%u_%s",
940                                          i, xname);
941
942                         strlcpy(xname, tmp, RTE_ETH_XSTATS_NAME_SIZE);
943                 }
944                 count += r;
945         }
946         return count;
947 }
948
949 static int
950 fs_xstats_get_names(struct rte_eth_dev *dev,
951                     struct rte_eth_xstat_name *xstats_names,
952                     unsigned int limit)
953 {
954         int ret;
955
956         fs_lock(dev, 0);
957         ret = __fs_xstats_get_names(dev, xstats_names, limit);
958         fs_unlock(dev, 0);
959         return ret;
960 }
961
962 static int
963 __fs_xstats_get(struct rte_eth_dev *dev,
964               struct rte_eth_xstat *xstats,
965               unsigned int n)
966 {
967         unsigned int count = 0;
968         struct sub_device *sdev;
969         uint8_t i;
970         int j, ret;
971
972         ret = __fs_xstats_count(dev);
973         /*
974          * if error
975          * or caller did not give enough space
976          * or just querying
977          */
978         if (ret < 0 || ret > (int)n || xstats == NULL)
979                 return ret;
980
981         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
982                 ret = rte_eth_xstats_get(PORT_ID(sdev), xstats, n);
983                 if (ret < 0)
984                         return ret;
985
986                 if (ret > (int)n)
987                         return n + count;
988
989                 /* add offset to id's from sub-device */
990                 for (j = 0; j < ret; j++)
991                         xstats[j].id += count;
992
993                 xstats += ret;
994                 n -= ret;
995                 count += ret;
996         }
997
998         return count;
999 }
1000
1001 static int
1002 fs_xstats_get(struct rte_eth_dev *dev,
1003               struct rte_eth_xstat *xstats,
1004               unsigned int n)
1005 {
1006         int ret;
1007
1008         fs_lock(dev, 0);
1009         ret = __fs_xstats_get(dev, xstats, n);
1010         fs_unlock(dev, 0);
1011
1012         return ret;
1013 }
1014
1015
1016 static int
1017 fs_xstats_reset(struct rte_eth_dev *dev)
1018 {
1019         struct sub_device *sdev;
1020         uint8_t i;
1021         int r = 0;
1022
1023         fs_lock(dev, 0);
1024         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1025                 r = rte_eth_xstats_reset(PORT_ID(sdev));
1026                 if (r < 0)
1027                         break;
1028         }
1029         fs_unlock(dev, 0);
1030
1031         return r;
1032 }
1033
1034 static void
1035 fs_dev_merge_desc_lim(struct rte_eth_desc_lim *to,
1036                       const struct rte_eth_desc_lim *from)
1037 {
1038         to->nb_max = RTE_MIN(to->nb_max, from->nb_max);
1039         to->nb_min = RTE_MAX(to->nb_min, from->nb_min);
1040         to->nb_align = RTE_MAX(to->nb_align, from->nb_align);
1041
1042         to->nb_seg_max = RTE_MIN(to->nb_seg_max, from->nb_seg_max);
1043         to->nb_mtu_seg_max = RTE_MIN(to->nb_mtu_seg_max, from->nb_mtu_seg_max);
1044 }
1045
1046 /*
1047  * Merge the information from sub-devices.
1048  *
1049  * The reported values must be the common subset of all sub devices
1050  */
1051 static void
1052 fs_dev_merge_info(struct rte_eth_dev_info *info,
1053                   const struct rte_eth_dev_info *sinfo)
1054 {
1055         info->max_rx_pktlen = RTE_MIN(info->max_rx_pktlen, sinfo->max_rx_pktlen);
1056         info->max_rx_queues = RTE_MIN(info->max_rx_queues, sinfo->max_rx_queues);
1057         info->max_tx_queues = RTE_MIN(info->max_tx_queues, sinfo->max_tx_queues);
1058         info->max_mac_addrs = RTE_MIN(info->max_mac_addrs, sinfo->max_mac_addrs);
1059         info->max_hash_mac_addrs = RTE_MIN(info->max_hash_mac_addrs,
1060                                         sinfo->max_hash_mac_addrs);
1061         info->max_vmdq_pools = RTE_MIN(info->max_vmdq_pools, sinfo->max_vmdq_pools);
1062         info->max_vfs = RTE_MIN(info->max_vfs, sinfo->max_vfs);
1063
1064         fs_dev_merge_desc_lim(&info->rx_desc_lim, &sinfo->rx_desc_lim);
1065         fs_dev_merge_desc_lim(&info->tx_desc_lim, &sinfo->tx_desc_lim);
1066
1067         info->rx_offload_capa &= sinfo->rx_offload_capa;
1068         info->tx_offload_capa &= sinfo->tx_offload_capa;
1069         info->rx_queue_offload_capa &= sinfo->rx_queue_offload_capa;
1070         info->tx_queue_offload_capa &= sinfo->tx_queue_offload_capa;
1071         info->flow_type_rss_offloads &= sinfo->flow_type_rss_offloads;
1072
1073         /*
1074          * RETA size is a GCD of RETA sizes indicated by sub-devices.
1075          * Each of these sizes is a power of 2, so use the lower one.
1076          */
1077         info->reta_size = RTE_MIN(info->reta_size, sinfo->reta_size);
1078
1079         info->hash_key_size = RTE_MIN(info->hash_key_size,
1080                                       sinfo->hash_key_size);
1081 }
1082
1083 /**
1084  * Fail-safe dev_infos_get rules:
1085  *
1086  * No sub_device:
1087  *   Numerables:
1088  *      Use the maximum possible values for any field, so as not
1089  *      to impede any further configuration effort.
1090  *   Capabilities:
1091  *      Limits capabilities to those that are understood by the
1092  *      fail-safe PMD. This understanding stems from the fail-safe
1093  *      being capable of verifying that the related capability is
1094  *      expressed within the device configuration (struct rte_eth_conf).
1095  *
1096  * At least one probed sub_device:
1097  *   Numerables:
1098  *      Uses values from the active probed sub_device
1099  *      The rationale here is that if any sub_device is less capable
1100  *      (for example concerning the number of queues) than the active
1101  *      sub_device, then its subsequent configuration will fail.
1102  *      It is impossible to foresee this failure when the failing sub_device
1103  *      is supposed to be plugged-in later on, so the configuration process
1104  *      is the single point of failure and error reporting.
1105  *   Capabilities:
1106  *      Uses a logical AND of RX capabilities among
1107  *      all sub_devices and the default capabilities.
1108  *      Uses a logical AND of TX capabilities among
1109  *      the active probed sub_device and the default capabilities.
1110  *      Uses a logical AND of device capabilities among
1111  *      all sub_devices and the default capabilities.
1112  *
1113  */
1114 static int
1115 fs_dev_infos_get(struct rte_eth_dev *dev,
1116                   struct rte_eth_dev_info *infos)
1117 {
1118         struct sub_device *sdev;
1119         uint8_t i;
1120         int ret;
1121
1122         /* Use maximum upper bounds by default */
1123         infos->max_rx_pktlen = UINT32_MAX;
1124         infos->max_rx_queues = RTE_MAX_QUEUES_PER_PORT;
1125         infos->max_tx_queues = RTE_MAX_QUEUES_PER_PORT;
1126         infos->max_mac_addrs = FAILSAFE_MAX_ETHADDR;
1127         infos->max_hash_mac_addrs = UINT32_MAX;
1128         infos->max_vfs = UINT16_MAX;
1129         infos->max_vmdq_pools = UINT16_MAX;
1130         infos->reta_size = UINT16_MAX;
1131         infos->hash_key_size = UINT8_MAX;
1132
1133         /*
1134          * Set of capabilities that can be verified upon
1135          * configuring a sub-device.
1136          */
1137         infos->rx_offload_capa =
1138                 DEV_RX_OFFLOAD_VLAN_STRIP |
1139                 DEV_RX_OFFLOAD_IPV4_CKSUM |
1140                 DEV_RX_OFFLOAD_UDP_CKSUM |
1141                 DEV_RX_OFFLOAD_TCP_CKSUM |
1142                 DEV_RX_OFFLOAD_TCP_LRO |
1143                 DEV_RX_OFFLOAD_QINQ_STRIP |
1144                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1145                 DEV_RX_OFFLOAD_MACSEC_STRIP |
1146                 DEV_RX_OFFLOAD_HEADER_SPLIT |
1147                 DEV_RX_OFFLOAD_VLAN_FILTER |
1148                 DEV_RX_OFFLOAD_VLAN_EXTEND |
1149                 DEV_RX_OFFLOAD_JUMBO_FRAME |
1150                 DEV_RX_OFFLOAD_SCATTER |
1151                 DEV_RX_OFFLOAD_TIMESTAMP |
1152                 DEV_RX_OFFLOAD_SECURITY;
1153
1154         infos->rx_queue_offload_capa =
1155                 DEV_RX_OFFLOAD_VLAN_STRIP |
1156                 DEV_RX_OFFLOAD_IPV4_CKSUM |
1157                 DEV_RX_OFFLOAD_UDP_CKSUM |
1158                 DEV_RX_OFFLOAD_TCP_CKSUM |
1159                 DEV_RX_OFFLOAD_TCP_LRO |
1160                 DEV_RX_OFFLOAD_QINQ_STRIP |
1161                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1162                 DEV_RX_OFFLOAD_MACSEC_STRIP |
1163                 DEV_RX_OFFLOAD_HEADER_SPLIT |
1164                 DEV_RX_OFFLOAD_VLAN_FILTER |
1165                 DEV_RX_OFFLOAD_VLAN_EXTEND |
1166                 DEV_RX_OFFLOAD_JUMBO_FRAME |
1167                 DEV_RX_OFFLOAD_SCATTER |
1168                 DEV_RX_OFFLOAD_TIMESTAMP |
1169                 DEV_RX_OFFLOAD_SECURITY;
1170
1171         infos->tx_offload_capa =
1172                 DEV_TX_OFFLOAD_MULTI_SEGS |
1173                 DEV_TX_OFFLOAD_MBUF_FAST_FREE |
1174                 DEV_TX_OFFLOAD_IPV4_CKSUM |
1175                 DEV_TX_OFFLOAD_UDP_CKSUM |
1176                 DEV_TX_OFFLOAD_TCP_CKSUM |
1177                 DEV_TX_OFFLOAD_TCP_TSO;
1178
1179         infos->flow_type_rss_offloads =
1180                 ETH_RSS_IP |
1181                 ETH_RSS_UDP |
1182                 ETH_RSS_TCP;
1183         infos->dev_capa =
1184                 RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
1185                 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
1186
1187         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
1188                 struct rte_eth_dev_info sub_info;
1189
1190                 ret = rte_eth_dev_info_get(PORT_ID(sdev), &sub_info);
1191                 ret = fs_err(sdev, ret);
1192                 if (ret != 0)
1193                         return ret;
1194
1195                 fs_dev_merge_info(infos, &sub_info);
1196         }
1197
1198         return 0;
1199 }
1200
1201 static const uint32_t *
1202 fs_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1203 {
1204         struct sub_device *sdev;
1205         struct rte_eth_dev *edev;
1206         const uint32_t *ret;
1207
1208         fs_lock(dev, 0);
1209         sdev = TX_SUBDEV(dev);
1210         if (sdev == NULL) {
1211                 ret = NULL;
1212                 goto unlock;
1213         }
1214         edev = ETH(sdev);
1215         /* ENOTSUP: counts as no supported ptypes */
1216         if (SUBOPS(sdev, dev_supported_ptypes_get) == NULL) {
1217                 ret = NULL;
1218                 goto unlock;
1219         }
1220         /*
1221          * The API does not permit to do a clean AND of all ptypes,
1222          * It is also incomplete by design and we do not really care
1223          * to have a best possible value in this context.
1224          * We just return the ptypes of the device of highest
1225          * priority, usually the PREFERRED device.
1226          */
1227         ret = SUBOPS(sdev, dev_supported_ptypes_get)(edev);
1228 unlock:
1229         fs_unlock(dev, 0);
1230         return ret;
1231 }
1232
1233 static int
1234 fs_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1235 {
1236         struct sub_device *sdev;
1237         uint8_t i;
1238         int ret;
1239
1240         fs_lock(dev, 0);
1241         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1242                 DEBUG("Calling rte_eth_dev_set_mtu on sub_device %d", i);
1243                 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), mtu);
1244                 if ((ret = fs_err(sdev, ret))) {
1245                         ERROR("Operation rte_eth_dev_set_mtu failed for sub_device %d with error %d",
1246                               i, ret);
1247                         fs_unlock(dev, 0);
1248                         return ret;
1249                 }
1250         }
1251         fs_unlock(dev, 0);
1252         return 0;
1253 }
1254
1255 static int
1256 fs_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1257 {
1258         struct sub_device *sdev;
1259         uint8_t i;
1260         int ret;
1261
1262         fs_lock(dev, 0);
1263         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1264                 DEBUG("Calling rte_eth_dev_vlan_filter on sub_device %d", i);
1265                 ret = rte_eth_dev_vlan_filter(PORT_ID(sdev), vlan_id, on);
1266                 if ((ret = fs_err(sdev, ret))) {
1267                         ERROR("Operation rte_eth_dev_vlan_filter failed for sub_device %d"
1268                               " with error %d", i, ret);
1269                         fs_unlock(dev, 0);
1270                         return ret;
1271                 }
1272         }
1273         fs_unlock(dev, 0);
1274         return 0;
1275 }
1276
1277 static int
1278 fs_flow_ctrl_get(struct rte_eth_dev *dev,
1279                 struct rte_eth_fc_conf *fc_conf)
1280 {
1281         struct sub_device *sdev;
1282         int ret;
1283
1284         fs_lock(dev, 0);
1285         sdev = TX_SUBDEV(dev);
1286         if (sdev == NULL) {
1287                 ret = 0;
1288                 goto unlock;
1289         }
1290         if (SUBOPS(sdev, flow_ctrl_get) == NULL) {
1291                 ret = -ENOTSUP;
1292                 goto unlock;
1293         }
1294         ret = SUBOPS(sdev, flow_ctrl_get)(ETH(sdev), fc_conf);
1295 unlock:
1296         fs_unlock(dev, 0);
1297         return ret;
1298 }
1299
1300 static int
1301 fs_flow_ctrl_set(struct rte_eth_dev *dev,
1302                 struct rte_eth_fc_conf *fc_conf)
1303 {
1304         struct sub_device *sdev;
1305         uint8_t i;
1306         int ret;
1307
1308         fs_lock(dev, 0);
1309         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1310                 DEBUG("Calling rte_eth_dev_flow_ctrl_set on sub_device %d", i);
1311                 ret = rte_eth_dev_flow_ctrl_set(PORT_ID(sdev), fc_conf);
1312                 if ((ret = fs_err(sdev, ret))) {
1313                         ERROR("Operation rte_eth_dev_flow_ctrl_set failed for sub_device %d"
1314                               " with error %d", i, ret);
1315                         fs_unlock(dev, 0);
1316                         return ret;
1317                 }
1318         }
1319         fs_unlock(dev, 0);
1320         return 0;
1321 }
1322
1323 static void
1324 fs_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1325 {
1326         struct sub_device *sdev;
1327         uint8_t i;
1328
1329         fs_lock(dev, 0);
1330         /* No check: already done within the rte_eth_dev_mac_addr_remove
1331          * call for the fail-safe device.
1332          */
1333         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
1334                 rte_eth_dev_mac_addr_remove(PORT_ID(sdev),
1335                                 &dev->data->mac_addrs[index]);
1336         PRIV(dev)->mac_addr_pool[index] = 0;
1337         fs_unlock(dev, 0);
1338 }
1339
1340 static int
1341 fs_mac_addr_add(struct rte_eth_dev *dev,
1342                 struct rte_ether_addr *mac_addr,
1343                 uint32_t index,
1344                 uint32_t vmdq)
1345 {
1346         struct sub_device *sdev;
1347         int ret;
1348         uint8_t i;
1349
1350         RTE_ASSERT(index < FAILSAFE_MAX_ETHADDR);
1351         fs_lock(dev, 0);
1352         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1353                 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), mac_addr, vmdq);
1354                 if ((ret = fs_err(sdev, ret))) {
1355                         ERROR("Operation rte_eth_dev_mac_addr_add failed for sub_device %"
1356                               PRIu8 " with error %d", i, ret);
1357                         fs_unlock(dev, 0);
1358                         return ret;
1359                 }
1360         }
1361         if (index >= PRIV(dev)->nb_mac_addr) {
1362                 DEBUG("Growing mac_addrs array");
1363                 PRIV(dev)->nb_mac_addr = index;
1364         }
1365         PRIV(dev)->mac_addr_pool[index] = vmdq;
1366         fs_unlock(dev, 0);
1367         return 0;
1368 }
1369
1370 static int
1371 fs_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1372 {
1373         struct sub_device *sdev;
1374         uint8_t i;
1375         int ret;
1376
1377         fs_lock(dev, 0);
1378         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1379                 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), mac_addr);
1380                 ret = fs_err(sdev, ret);
1381                 if (ret) {
1382                         ERROR("Operation rte_eth_dev_mac_addr_set failed for sub_device %d with error %d",
1383                                 i, ret);
1384                         fs_unlock(dev, 0);
1385                         return ret;
1386                 }
1387         }
1388         fs_unlock(dev, 0);
1389
1390         return 0;
1391 }
1392
1393 static int
1394 fs_set_mc_addr_list(struct rte_eth_dev *dev,
1395                     struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
1396 {
1397         struct sub_device *sdev;
1398         uint8_t i;
1399         int ret;
1400         void *mcast_addrs;
1401
1402         fs_lock(dev, 0);
1403
1404         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1405                 ret = rte_eth_dev_set_mc_addr_list(PORT_ID(sdev),
1406                                                    mc_addr_set, nb_mc_addr);
1407                 if (ret != 0) {
1408                         ERROR("Operation rte_eth_dev_set_mc_addr_list failed for sub_device %d with error %d",
1409                               i, ret);
1410                         goto rollback;
1411                 }
1412         }
1413
1414         mcast_addrs = rte_realloc(PRIV(dev)->mcast_addrs,
1415                 nb_mc_addr * sizeof(PRIV(dev)->mcast_addrs[0]), 0);
1416         if (mcast_addrs == NULL && nb_mc_addr > 0) {
1417                 ret = -ENOMEM;
1418                 goto rollback;
1419         }
1420         rte_memcpy(mcast_addrs, mc_addr_set,
1421                    nb_mc_addr * sizeof(PRIV(dev)->mcast_addrs[0]));
1422         PRIV(dev)->nb_mcast_addr = nb_mc_addr;
1423         PRIV(dev)->mcast_addrs = mcast_addrs;
1424
1425         fs_unlock(dev, 0);
1426         return 0;
1427
1428 rollback:
1429         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1430                 int rc = rte_eth_dev_set_mc_addr_list(PORT_ID(sdev),
1431                         PRIV(dev)->mcast_addrs, PRIV(dev)->nb_mcast_addr);
1432                 if (rc != 0) {
1433                         ERROR("Multicast MAC address list rollback for sub_device %d failed with error %d",
1434                               i, rc);
1435                 }
1436         }
1437
1438         fs_unlock(dev, 0);
1439         return ret;
1440 }
1441
1442 static int
1443 fs_rss_hash_update(struct rte_eth_dev *dev,
1444                         struct rte_eth_rss_conf *rss_conf)
1445 {
1446         struct sub_device *sdev;
1447         uint8_t i;
1448         int ret;
1449
1450         fs_lock(dev, 0);
1451         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
1452                 ret = rte_eth_dev_rss_hash_update(PORT_ID(sdev), rss_conf);
1453                 ret = fs_err(sdev, ret);
1454                 if (ret) {
1455                         ERROR("Operation rte_eth_dev_rss_hash_update"
1456                                 " failed for sub_device %d with error %d",
1457                                 i, ret);
1458                         fs_unlock(dev, 0);
1459                         return ret;
1460                 }
1461         }
1462         fs_unlock(dev, 0);
1463
1464         return 0;
1465 }
1466
1467 static int
1468 fs_filter_ctrl(struct rte_eth_dev *dev __rte_unused,
1469                 enum rte_filter_type type,
1470                 enum rte_filter_op op,
1471                 void *arg)
1472 {
1473         if (type == RTE_ETH_FILTER_GENERIC &&
1474             op == RTE_ETH_FILTER_GET) {
1475                 *(const void **)arg = &fs_flow_ops;
1476                 return 0;
1477         }
1478         return -ENOTSUP;
1479 }
1480
1481 const struct eth_dev_ops failsafe_ops = {
1482         .dev_configure = fs_dev_configure,
1483         .dev_start = fs_dev_start,
1484         .dev_stop = fs_dev_stop,
1485         .dev_set_link_down = fs_dev_set_link_down,
1486         .dev_set_link_up = fs_dev_set_link_up,
1487         .dev_close = fs_dev_close,
1488         .promiscuous_enable = fs_promiscuous_enable,
1489         .promiscuous_disable = fs_promiscuous_disable,
1490         .allmulticast_enable = fs_allmulticast_enable,
1491         .allmulticast_disable = fs_allmulticast_disable,
1492         .link_update = fs_link_update,
1493         .stats_get = fs_stats_get,
1494         .stats_reset = fs_stats_reset,
1495         .xstats_get = fs_xstats_get,
1496         .xstats_get_names = fs_xstats_get_names,
1497         .xstats_reset = fs_xstats_reset,
1498         .dev_infos_get = fs_dev_infos_get,
1499         .dev_supported_ptypes_get = fs_dev_supported_ptypes_get,
1500         .mtu_set = fs_mtu_set,
1501         .vlan_filter_set = fs_vlan_filter_set,
1502         .rx_queue_start = fs_rx_queue_start,
1503         .rx_queue_stop = fs_rx_queue_stop,
1504         .tx_queue_start = fs_tx_queue_start,
1505         .tx_queue_stop = fs_tx_queue_stop,
1506         .rx_queue_setup = fs_rx_queue_setup,
1507         .tx_queue_setup = fs_tx_queue_setup,
1508         .rx_queue_release = fs_rx_queue_release,
1509         .tx_queue_release = fs_tx_queue_release,
1510         .rx_queue_intr_enable = fs_rx_intr_enable,
1511         .rx_queue_intr_disable = fs_rx_intr_disable,
1512         .flow_ctrl_get = fs_flow_ctrl_get,
1513         .flow_ctrl_set = fs_flow_ctrl_set,
1514         .mac_addr_remove = fs_mac_addr_remove,
1515         .mac_addr_add = fs_mac_addr_add,
1516         .mac_addr_set = fs_mac_addr_set,
1517         .set_mc_addr_list = fs_set_mc_addr_list,
1518         .rss_hash_update = fs_rss_hash_update,
1519         .filter_ctrl = fs_filter_ctrl,
1520 };