4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <rte_debug.h>
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
41 #include "failsafe_private.h"
43 static struct rte_eth_dev_info default_infos = {
44 /* Max possible number of elements */
45 .max_rx_pktlen = UINT32_MAX,
46 .max_rx_queues = RTE_MAX_QUEUES_PER_PORT,
47 .max_tx_queues = RTE_MAX_QUEUES_PER_PORT,
48 .max_mac_addrs = FAILSAFE_MAX_ETHADDR,
49 .max_hash_mac_addrs = UINT32_MAX,
50 .max_vfs = UINT16_MAX,
51 .max_vmdq_pools = UINT16_MAX,
56 .nb_seg_max = UINT16_MAX,
57 .nb_mtu_seg_max = UINT16_MAX,
63 .nb_seg_max = UINT16_MAX,
64 .nb_mtu_seg_max = UINT16_MAX,
66 /* Set of understood capabilities */
67 .rx_offload_capa = 0x0,
68 .tx_offload_capa = 0x0,
69 .flow_type_rss_offloads = 0x0,
73 fs_dev_configure(struct rte_eth_dev *dev)
75 struct sub_device *sdev;
79 FOREACH_SUBDEV(sdev, i, dev) {
80 if (sdev->state != DEV_PROBED)
82 DEBUG("Configuring sub-device %d", i);
83 ret = rte_eth_dev_configure(PORT_ID(sdev),
84 dev->data->nb_rx_queues,
85 dev->data->nb_tx_queues,
86 &dev->data->dev_conf);
88 ERROR("Could not configure sub_device %d", i);
91 sdev->state = DEV_ACTIVE;
93 if (PRIV(dev)->state < DEV_ACTIVE)
94 PRIV(dev)->state = DEV_ACTIVE;
99 fs_dev_start(struct rte_eth_dev *dev)
101 struct sub_device *sdev;
105 FOREACH_SUBDEV(sdev, i, dev) {
106 if (sdev->state != DEV_ACTIVE)
108 DEBUG("Starting sub_device %d", i);
109 ret = rte_eth_dev_start(PORT_ID(sdev));
112 sdev->state = DEV_STARTED;
114 if (PRIV(dev)->state < DEV_STARTED)
115 PRIV(dev)->state = DEV_STARTED;
121 fs_dev_stop(struct rte_eth_dev *dev)
123 struct sub_device *sdev;
126 PRIV(dev)->state = DEV_STARTED - 1;
127 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_STARTED) {
128 rte_eth_dev_stop(PORT_ID(sdev));
129 sdev->state = DEV_STARTED - 1;
134 fs_dev_set_link_up(struct rte_eth_dev *dev)
136 struct sub_device *sdev;
140 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
141 DEBUG("Calling rte_eth_dev_set_link_up on sub_device %d", i);
142 ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
144 ERROR("Operation rte_eth_dev_set_link_up failed for sub_device %d"
145 " with error %d", i, ret);
153 fs_dev_set_link_down(struct rte_eth_dev *dev)
155 struct sub_device *sdev;
159 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
160 DEBUG("Calling rte_eth_dev_set_link_down on sub_device %d", i);
161 ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
163 ERROR("Operation rte_eth_dev_set_link_down failed for sub_device %d"
164 " with error %d", i, ret);
171 static void fs_dev_free_queues(struct rte_eth_dev *dev);
173 fs_dev_close(struct rte_eth_dev *dev)
175 struct sub_device *sdev;
178 failsafe_hotplug_alarm_cancel(dev);
179 if (PRIV(dev)->state == DEV_STARTED)
180 dev->dev_ops->dev_stop(dev);
181 PRIV(dev)->state = DEV_ACTIVE - 1;
182 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
183 DEBUG("Closing sub_device %d", i);
184 rte_eth_dev_close(PORT_ID(sdev));
185 sdev->state = DEV_ACTIVE - 1;
187 fs_dev_free_queues(dev);
191 fs_rx_queue_release(void *queue)
193 struct rte_eth_dev *dev;
194 struct sub_device *sdev;
201 dev = rxq->priv->dev;
202 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
203 SUBOPS(sdev, rx_queue_release)
204 (ETH(sdev)->data->rx_queues[rxq->qid]);
205 dev->data->rx_queues[rxq->qid] = NULL;
210 fs_rx_queue_setup(struct rte_eth_dev *dev,
211 uint16_t rx_queue_id,
213 unsigned int socket_id,
214 const struct rte_eth_rxconf *rx_conf,
215 struct rte_mempool *mb_pool)
217 struct sub_device *sdev;
222 rxq = dev->data->rx_queues[rx_queue_id];
224 fs_rx_queue_release(rxq);
225 dev->data->rx_queues[rx_queue_id] = NULL;
227 rxq = rte_zmalloc(NULL, sizeof(*rxq),
228 RTE_CACHE_LINE_SIZE);
231 rxq->qid = rx_queue_id;
232 rxq->socket_id = socket_id;
233 rxq->info.mp = mb_pool;
234 rxq->info.conf = *rx_conf;
235 rxq->info.nb_desc = nb_rx_desc;
236 rxq->priv = PRIV(dev);
237 dev->data->rx_queues[rx_queue_id] = rxq;
238 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
239 ret = rte_eth_rx_queue_setup(PORT_ID(sdev),
241 nb_rx_desc, socket_id,
244 ERROR("RX queue setup failed for sub_device %d", i);
250 fs_rx_queue_release(rxq);
255 fs_tx_queue_release(void *queue)
257 struct rte_eth_dev *dev;
258 struct sub_device *sdev;
265 dev = txq->priv->dev;
266 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
267 SUBOPS(sdev, tx_queue_release)
268 (ETH(sdev)->data->tx_queues[txq->qid]);
269 dev->data->tx_queues[txq->qid] = NULL;
274 fs_tx_queue_setup(struct rte_eth_dev *dev,
275 uint16_t tx_queue_id,
277 unsigned int socket_id,
278 const struct rte_eth_txconf *tx_conf)
280 struct sub_device *sdev;
285 txq = dev->data->tx_queues[tx_queue_id];
287 fs_tx_queue_release(txq);
288 dev->data->tx_queues[tx_queue_id] = NULL;
290 txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
291 RTE_CACHE_LINE_SIZE);
294 txq->qid = tx_queue_id;
295 txq->socket_id = socket_id;
296 txq->info.conf = *tx_conf;
297 txq->info.nb_desc = nb_tx_desc;
298 txq->priv = PRIV(dev);
299 dev->data->tx_queues[tx_queue_id] = txq;
300 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
301 ret = rte_eth_tx_queue_setup(PORT_ID(sdev),
303 nb_tx_desc, socket_id,
306 ERROR("TX queue setup failed for sub_device %d", i);
312 fs_tx_queue_release(txq);
317 fs_dev_free_queues(struct rte_eth_dev *dev)
321 for (i = 0; i < dev->data->nb_rx_queues; i++) {
322 fs_rx_queue_release(dev->data->rx_queues[i]);
323 dev->data->rx_queues[i] = NULL;
325 dev->data->nb_rx_queues = 0;
326 for (i = 0; i < dev->data->nb_tx_queues; i++) {
327 fs_tx_queue_release(dev->data->tx_queues[i]);
328 dev->data->tx_queues[i] = NULL;
330 dev->data->nb_tx_queues = 0;
334 fs_promiscuous_enable(struct rte_eth_dev *dev)
336 struct sub_device *sdev;
339 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
340 rte_eth_promiscuous_enable(PORT_ID(sdev));
344 fs_promiscuous_disable(struct rte_eth_dev *dev)
346 struct sub_device *sdev;
349 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
350 rte_eth_promiscuous_disable(PORT_ID(sdev));
354 fs_allmulticast_enable(struct rte_eth_dev *dev)
356 struct sub_device *sdev;
359 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
360 rte_eth_allmulticast_enable(PORT_ID(sdev));
364 fs_allmulticast_disable(struct rte_eth_dev *dev)
366 struct sub_device *sdev;
369 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
370 rte_eth_allmulticast_disable(PORT_ID(sdev));
374 fs_link_update(struct rte_eth_dev *dev,
375 int wait_to_complete)
377 struct sub_device *sdev;
381 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
382 DEBUG("Calling link_update on sub_device %d", i);
383 ret = (SUBOPS(sdev, link_update))(ETH(sdev), wait_to_complete);
384 if (ret && ret != -1) {
385 ERROR("Link update failed for sub_device %d with error %d",
390 if (TX_SUBDEV(dev)) {
391 struct rte_eth_link *l1;
392 struct rte_eth_link *l2;
394 l1 = &dev->data->dev_link;
395 l2 = Ð(TX_SUBDEV(dev))->data->dev_link;
396 if (memcmp(l1, l2, sizeof(*l1))) {
405 fs_stats_get(struct rte_eth_dev *dev,
406 struct rte_eth_stats *stats)
408 if (TX_SUBDEV(dev) == NULL)
410 rte_eth_stats_get(PORT_ID(TX_SUBDEV(dev)), stats);
414 fs_stats_reset(struct rte_eth_dev *dev)
416 struct sub_device *sdev;
419 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
420 rte_eth_stats_reset(PORT_ID(sdev));
424 * Fail-safe dev_infos_get rules:
428 * Use the maximum possible values for any field, so as not
429 * to impede any further configuration effort.
431 * Limits capabilities to those that are understood by the
432 * fail-safe PMD. This understanding stems from the fail-safe
433 * being capable of verifying that the related capability is
434 * expressed within the device configuration (struct rte_eth_conf).
436 * At least one probed sub_device:
438 * Uses values from the active probed sub_device
439 * The rationale here is that if any sub_device is less capable
440 * (for example concerning the number of queues) than the active
441 * sub_device, then its subsequent configuration will fail.
442 * It is impossible to foresee this failure when the failing sub_device
443 * is supposed to be plugged-in later on, so the configuration process
444 * is the single point of failure and error reporting.
446 * Uses a logical AND of RX capabilities among
447 * all sub_devices and the default capabilities.
448 * Uses a logical AND of TX capabilities among
449 * the active probed sub_device and the default capabilities.
453 fs_dev_infos_get(struct rte_eth_dev *dev,
454 struct rte_eth_dev_info *infos)
456 struct sub_device *sdev;
459 sdev = TX_SUBDEV(dev);
461 DEBUG("No probed device, using default infos");
462 rte_memcpy(&PRIV(dev)->infos, &default_infos,
463 sizeof(default_infos));
465 uint32_t rx_offload_capa;
467 rx_offload_capa = default_infos.rx_offload_capa;
468 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
469 rte_eth_dev_info_get(PORT_ID(sdev),
471 rx_offload_capa &= PRIV(dev)->infos.rx_offload_capa;
473 sdev = TX_SUBDEV(dev);
474 rte_eth_dev_info_get(PORT_ID(sdev), &PRIV(dev)->infos);
475 PRIV(dev)->infos.rx_offload_capa = rx_offload_capa;
476 PRIV(dev)->infos.tx_offload_capa &=
477 default_infos.tx_offload_capa;
478 PRIV(dev)->infos.flow_type_rss_offloads &=
479 default_infos.flow_type_rss_offloads;
481 rte_memcpy(infos, &PRIV(dev)->infos, sizeof(*infos));
484 static const uint32_t *
485 fs_dev_supported_ptypes_get(struct rte_eth_dev *dev)
487 struct sub_device *sdev;
488 struct rte_eth_dev *edev;
490 sdev = TX_SUBDEV(dev);
494 /* ENOTSUP: counts as no supported ptypes */
495 if (SUBOPS(sdev, dev_supported_ptypes_get) == NULL)
498 * The API does not permit to do a clean AND of all ptypes,
499 * It is also incomplete by design and we do not really care
500 * to have a best possible value in this context.
501 * We just return the ptypes of the device of highest
502 * priority, usually the PREFERRED device.
504 return SUBOPS(sdev, dev_supported_ptypes_get)(edev);
508 fs_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
510 struct sub_device *sdev;
514 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
515 DEBUG("Calling rte_eth_dev_set_mtu on sub_device %d", i);
516 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), mtu);
518 ERROR("Operation rte_eth_dev_set_mtu failed for sub_device %d with error %d",
527 fs_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
529 struct sub_device *sdev;
533 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
534 DEBUG("Calling rte_eth_dev_vlan_filter on sub_device %d", i);
535 ret = rte_eth_dev_vlan_filter(PORT_ID(sdev), vlan_id, on);
537 ERROR("Operation rte_eth_dev_vlan_filter failed for sub_device %d"
538 " with error %d", i, ret);
546 fs_flow_ctrl_get(struct rte_eth_dev *dev,
547 struct rte_eth_fc_conf *fc_conf)
549 struct sub_device *sdev;
551 sdev = TX_SUBDEV(dev);
554 if (SUBOPS(sdev, flow_ctrl_get) == NULL)
556 return SUBOPS(sdev, flow_ctrl_get)(ETH(sdev), fc_conf);
560 fs_flow_ctrl_set(struct rte_eth_dev *dev,
561 struct rte_eth_fc_conf *fc_conf)
563 struct sub_device *sdev;
567 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
568 DEBUG("Calling rte_eth_dev_flow_ctrl_set on sub_device %d", i);
569 ret = rte_eth_dev_flow_ctrl_set(PORT_ID(sdev), fc_conf);
571 ERROR("Operation rte_eth_dev_flow_ctrl_set failed for sub_device %d"
572 " with error %d", i, ret);
580 fs_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
582 struct sub_device *sdev;
585 /* No check: already done within the rte_eth_dev_mac_addr_remove
586 * call for the fail-safe device.
588 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
589 rte_eth_dev_mac_addr_remove(PORT_ID(sdev),
590 &dev->data->mac_addrs[index]);
591 PRIV(dev)->mac_addr_pool[index] = 0;
595 fs_mac_addr_add(struct rte_eth_dev *dev,
596 struct ether_addr *mac_addr,
600 struct sub_device *sdev;
604 RTE_ASSERT(index < FAILSAFE_MAX_ETHADDR);
605 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
606 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), mac_addr, vmdq);
608 ERROR("Operation rte_eth_dev_mac_addr_add failed for sub_device %"
609 PRIu8 " with error %d", i, ret);
613 if (index >= PRIV(dev)->nb_mac_addr) {
614 DEBUG("Growing mac_addrs array");
615 PRIV(dev)->nb_mac_addr = index;
617 PRIV(dev)->mac_addr_pool[index] = vmdq;
622 fs_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
624 struct sub_device *sdev;
627 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
628 rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), mac_addr);
632 fs_filter_ctrl(struct rte_eth_dev *dev,
633 enum rte_filter_type type,
634 enum rte_filter_op op,
637 struct sub_device *sdev;
641 if (type == RTE_ETH_FILTER_GENERIC &&
642 op == RTE_ETH_FILTER_GET) {
643 *(const void **)arg = &fs_flow_ops;
646 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
647 DEBUG("Calling rte_eth_dev_filter_ctrl on sub_device %d", i);
648 ret = rte_eth_dev_filter_ctrl(PORT_ID(sdev), type, op, arg);
650 ERROR("Operation rte_eth_dev_filter_ctrl failed for sub_device %d"
651 " with error %d", i, ret);
658 const struct eth_dev_ops failsafe_ops = {
659 .dev_configure = fs_dev_configure,
660 .dev_start = fs_dev_start,
661 .dev_stop = fs_dev_stop,
662 .dev_set_link_down = fs_dev_set_link_down,
663 .dev_set_link_up = fs_dev_set_link_up,
664 .dev_close = fs_dev_close,
665 .promiscuous_enable = fs_promiscuous_enable,
666 .promiscuous_disable = fs_promiscuous_disable,
667 .allmulticast_enable = fs_allmulticast_enable,
668 .allmulticast_disable = fs_allmulticast_disable,
669 .link_update = fs_link_update,
670 .stats_get = fs_stats_get,
671 .stats_reset = fs_stats_reset,
672 .dev_infos_get = fs_dev_infos_get,
673 .dev_supported_ptypes_get = fs_dev_supported_ptypes_get,
674 .mtu_set = fs_mtu_set,
675 .vlan_filter_set = fs_vlan_filter_set,
676 .rx_queue_setup = fs_rx_queue_setup,
677 .tx_queue_setup = fs_tx_queue_setup,
678 .rx_queue_release = fs_rx_queue_release,
679 .tx_queue_release = fs_tx_queue_release,
680 .flow_ctrl_get = fs_flow_ctrl_get,
681 .flow_ctrl_set = fs_flow_ctrl_set,
682 .mac_addr_remove = fs_mac_addr_remove,
683 .mac_addr_add = fs_mac_addr_add,
684 .mac_addr_set = fs_mac_addr_set,
685 .filter_ctrl = fs_filter_ctrl,