1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2021 Xilinx, Inc.
4 * Copyright(c) 2019 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
13 #include <rte_ethdev.h>
14 #include <rte_malloc.h>
15 #include <ethdev_driver.h>
20 #include "sfc_debug.h"
22 #include "sfc_ethdev_state.h"
23 #include "sfc_repr_proxy_api.h"
24 #include "sfc_switch.h"
25 #include "sfc_dp_tx.h"
27 /** Multi-process shared representor private data */
28 struct sfc_repr_shared {
31 uint16_t switch_domain_id;
32 uint16_t switch_port_id;
36 /* Datapath members */
37 struct rte_ring *ring;
41 /* Datapath members */
42 struct rte_ring *ring;
43 efx_mport_id_t egress_mport;
46 /** Primary process representor private data */
49 * PMD setup and configuration is not thread safe. Since it is not
50 * performance sensitive, it is better to guarantee thread-safety
51 * and add device level lock. Adapter control operations which
52 * change its state should acquire the lock.
55 enum sfc_ethdev_state state;
58 #define sfcr_err(sr, ...) \
60 const struct sfc_repr *_sr = (sr); \
63 SFC_GENERIC_LOG(ERR, __VA_ARGS__); \
66 #define sfcr_warn(sr, ...) \
68 const struct sfc_repr *_sr = (sr); \
71 SFC_GENERIC_LOG(WARNING, __VA_ARGS__); \
74 #define sfcr_info(sr, ...) \
76 const struct sfc_repr *_sr = (sr); \
79 SFC_GENERIC_LOG(INFO, \
81 RTE_FMT_HEAD(__VA_ARGS__ ,), \
83 RTE_FMT_TAIL(__VA_ARGS__ ,))); \
86 static inline struct sfc_repr_shared *
87 sfc_repr_shared_by_eth_dev(struct rte_eth_dev *eth_dev)
89 struct sfc_repr_shared *srs = eth_dev->data->dev_private;
94 static inline struct sfc_repr *
95 sfc_repr_by_eth_dev(struct rte_eth_dev *eth_dev)
97 struct sfc_repr *sr = eth_dev->process_private;
103 * Add wrapper functions to acquire/release lock to be able to remove or
104 * change the lock in one place.
108 sfc_repr_lock_init(struct sfc_repr *sr)
110 rte_spinlock_init(&sr->lock);
113 #if defined(RTE_LIBRTE_SFC_EFX_DEBUG) || defined(RTE_ENABLE_ASSERT)
116 sfc_repr_lock_is_locked(struct sfc_repr *sr)
118 return rte_spinlock_is_locked(&sr->lock);
124 sfc_repr_lock(struct sfc_repr *sr)
126 rte_spinlock_lock(&sr->lock);
130 sfc_repr_unlock(struct sfc_repr *sr)
132 rte_spinlock_unlock(&sr->lock);
136 sfc_repr_lock_fini(__rte_unused struct sfc_repr *sr)
138 /* Just for symmetry of the API */
142 sfc_repr_rx_queue_stop(void *queue)
144 struct sfc_repr_rxq *rxq = queue;
149 rte_ring_reset(rxq->ring);
153 sfc_repr_tx_queue_stop(void *queue)
155 struct sfc_repr_txq *txq = queue;
160 rte_ring_reset(txq->ring);
164 sfc_repr_start(struct rte_eth_dev *dev)
166 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
167 struct sfc_repr_shared *srs;
170 sfcr_info(sr, "entry");
172 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
175 case SFC_ETHDEV_CONFIGURED:
177 case SFC_ETHDEV_STARTED:
178 sfcr_info(sr, "already started");
185 sr->state = SFC_ETHDEV_STARTING;
187 srs = sfc_repr_shared_by_eth_dev(dev);
188 ret = sfc_repr_proxy_start_repr(srs->pf_port_id, srs->repr_id);
195 sr->state = SFC_ETHDEV_STARTED;
197 sfcr_info(sr, "done");
202 sr->state = SFC_ETHDEV_CONFIGURED;
205 sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
210 sfc_repr_dev_start(struct rte_eth_dev *dev)
212 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
215 sfcr_info(sr, "entry");
218 ret = sfc_repr_start(dev);
224 sfcr_info(sr, "done");
229 sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
234 sfc_repr_stop(struct rte_eth_dev *dev)
236 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
237 struct sfc_repr_shared *srs;
241 sfcr_info(sr, "entry");
243 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
246 case SFC_ETHDEV_STARTED:
248 case SFC_ETHDEV_CONFIGURED:
249 sfcr_info(sr, "already stopped");
252 sfcr_err(sr, "stop in unexpected state %u", sr->state);
258 srs = sfc_repr_shared_by_eth_dev(dev);
259 ret = sfc_repr_proxy_stop_repr(srs->pf_port_id, srs->repr_id);
266 for (i = 0; i < dev->data->nb_rx_queues; i++)
267 sfc_repr_rx_queue_stop(dev->data->rx_queues[i]);
269 for (i = 0; i < dev->data->nb_tx_queues; i++)
270 sfc_repr_tx_queue_stop(dev->data->tx_queues[i]);
272 sr->state = SFC_ETHDEV_CONFIGURED;
273 sfcr_info(sr, "done");
279 sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
285 sfc_repr_dev_stop(struct rte_eth_dev *dev)
287 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
290 sfcr_info(sr, "entry");
294 ret = sfc_repr_stop(dev);
296 sfcr_err(sr, "%s() failed to stop representor", __func__);
302 sfcr_info(sr, "done");
309 sfcr_err(sr, "%s() failed %s", __func__, rte_strerror(-ret));
315 sfc_repr_check_conf(struct sfc_repr *sr, uint16_t nb_rx_queues,
316 const struct rte_eth_conf *conf)
318 const struct rte_eth_rss_conf *rss_conf;
321 sfcr_info(sr, "entry");
323 if (conf->link_speeds != 0) {
324 sfcr_err(sr, "specific link speeds not supported");
328 switch (conf->rxmode.mq_mode) {
330 if (nb_rx_queues != 1) {
331 sfcr_err(sr, "Rx RSS is not supported with %u queues",
337 rss_conf = &conf->rx_adv_conf.rss_conf;
338 if (rss_conf->rss_key != NULL || rss_conf->rss_key_len != 0 ||
339 rss_conf->rss_hf != 0) {
340 sfcr_err(sr, "Rx RSS configuration is not supported");
347 sfcr_err(sr, "Rx mode MQ modes other than RSS not supported");
352 if (conf->txmode.mq_mode != ETH_MQ_TX_NONE) {
353 sfcr_err(sr, "Tx mode MQ modes not supported");
357 if (conf->lpbk_mode != 0) {
358 sfcr_err(sr, "loopback not supported");
362 if (conf->dcb_capability_en != 0) {
363 sfcr_err(sr, "priority-based flow control not supported");
367 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
368 sfcr_err(sr, "Flow Director not supported");
372 if (conf->intr_conf.lsc != 0) {
373 sfcr_err(sr, "link status change interrupt not supported");
377 if (conf->intr_conf.rxq != 0) {
378 sfcr_err(sr, "receive queue interrupt not supported");
382 if (conf->intr_conf.rmv != 0) {
383 sfcr_err(sr, "remove interrupt not supported");
387 sfcr_info(sr, "done %d", ret);
394 sfc_repr_configure(struct sfc_repr *sr, uint16_t nb_rx_queues,
395 const struct rte_eth_conf *conf)
399 sfcr_info(sr, "entry");
401 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
403 ret = sfc_repr_check_conf(sr, nb_rx_queues, conf);
405 goto fail_check_conf;
407 sr->state = SFC_ETHDEV_CONFIGURED;
409 sfcr_info(sr, "done");
414 sfcr_info(sr, "failed %s", rte_strerror(-ret));
419 sfc_repr_dev_configure(struct rte_eth_dev *dev)
421 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
422 struct rte_eth_dev_data *dev_data = dev->data;
425 sfcr_info(sr, "entry n_rxq=%u n_txq=%u",
426 dev_data->nb_rx_queues, dev_data->nb_tx_queues);
430 case SFC_ETHDEV_CONFIGURED:
432 case SFC_ETHDEV_INITIALIZED:
433 ret = sfc_repr_configure(sr, dev_data->nb_rx_queues,
434 &dev_data->dev_conf);
437 sfcr_err(sr, "unexpected adapter state %u to configure",
444 sfcr_info(sr, "done %s", rte_strerror(-ret));
450 sfc_repr_dev_infos_get(struct rte_eth_dev *dev,
451 struct rte_eth_dev_info *dev_info)
453 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
455 dev_info->device = dev->device;
457 dev_info->max_rx_queues = SFC_REPR_RXQ_MAX;
458 dev_info->max_tx_queues = SFC_REPR_TXQ_MAX;
459 dev_info->default_rxconf.rx_drop_en = 1;
460 dev_info->switch_info.domain_id = srs->switch_domain_id;
461 dev_info->switch_info.port_id = srs->switch_port_id;
467 sfc_repr_dev_link_update(struct rte_eth_dev *dev,
468 __rte_unused int wait_to_complete)
470 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
471 struct rte_eth_link link;
473 if (sr->state != SFC_ETHDEV_STARTED) {
474 sfc_port_link_mode_to_info(EFX_LINK_UNKNOWN, &link);
476 memset(&link, 0, sizeof(link));
477 link.link_status = ETH_LINK_UP;
478 link.link_speed = ETH_SPEED_NUM_UNKNOWN;
481 return rte_eth_linkstatus_set(dev, &link);
485 sfc_repr_ring_create(uint16_t pf_port_id, uint16_t repr_id,
486 const char *type_name, uint16_t qid, uint16_t nb_desc,
487 unsigned int socket_id, struct rte_ring **ring)
489 char ring_name[RTE_RING_NAMESIZE];
492 ret = snprintf(ring_name, sizeof(ring_name), "sfc_%u_repr_%u_%sq%u",
493 pf_port_id, repr_id, type_name, qid);
494 if (ret >= (int)sizeof(ring_name))
495 return -ENAMETOOLONG;
498 * Single producer/consumer rings are used since the API for Tx/Rx
499 * packet burst for representors are guaranteed to be called from
500 * a single thread, and the user of the other end (representor proxy)
501 * is also single-threaded.
503 *ring = rte_ring_create(ring_name, nb_desc, socket_id,
504 RING_F_SP_ENQ | RING_F_SC_DEQ);
512 sfc_repr_rx_qcheck_conf(struct sfc_repr *sr,
513 const struct rte_eth_rxconf *rx_conf)
517 sfcr_info(sr, "entry");
519 if (rx_conf->rx_thresh.pthresh != 0 ||
520 rx_conf->rx_thresh.hthresh != 0 ||
521 rx_conf->rx_thresh.wthresh != 0) {
523 "RxQ prefetch/host/writeback thresholds are not supported");
526 if (rx_conf->rx_free_thresh != 0)
527 sfcr_warn(sr, "RxQ free threshold is not supported");
529 if (rx_conf->rx_drop_en == 0)
530 sfcr_warn(sr, "RxQ drop disable is not supported");
532 if (rx_conf->rx_deferred_start) {
533 sfcr_err(sr, "Deferred start is not supported");
537 sfcr_info(sr, "done: %s", rte_strerror(-ret));
543 sfc_repr_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
544 uint16_t nb_rx_desc, unsigned int socket_id,
545 __rte_unused const struct rte_eth_rxconf *rx_conf,
546 struct rte_mempool *mb_pool)
548 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
549 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
550 struct sfc_repr_rxq *rxq;
553 sfcr_info(sr, "entry");
555 ret = sfc_repr_rx_qcheck_conf(sr, rx_conf);
557 goto fail_check_conf;
560 rxq = rte_zmalloc_socket("sfc-repr-rxq", sizeof(*rxq),
561 RTE_CACHE_LINE_SIZE, socket_id);
563 sfcr_err(sr, "%s() failed to alloc RxQ", __func__);
567 ret = sfc_repr_ring_create(srs->pf_port_id, srs->repr_id,
568 "rx", rx_queue_id, nb_rx_desc,
569 socket_id, &rxq->ring);
571 sfcr_err(sr, "%s() failed to create ring", __func__);
572 goto fail_ring_create;
575 ret = sfc_repr_proxy_add_rxq(srs->pf_port_id, srs->repr_id,
576 rx_queue_id, rxq->ring, mb_pool);
580 sfcr_err(sr, "%s() failed to add proxy RxQ", __func__);
581 goto fail_proxy_add_rxq;
584 dev->data->rx_queues[rx_queue_id] = rxq;
586 sfcr_info(sr, "done");
591 rte_ring_free(rxq->ring);
598 sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
603 sfc_repr_rx_queue_release(struct rte_eth_dev *dev, uint16_t rx_queue_id)
605 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
606 struct sfc_repr_rxq *rxq = dev->data->rx_queues[rx_queue_id];
608 sfc_repr_proxy_del_rxq(srs->pf_port_id, srs->repr_id, rx_queue_id);
609 rte_ring_free(rxq->ring);
614 sfc_repr_tx_qcheck_conf(struct sfc_repr *sr,
615 const struct rte_eth_txconf *tx_conf)
619 sfcr_info(sr, "entry");
621 if (tx_conf->tx_rs_thresh != 0)
622 sfcr_warn(sr, "RS bit in transmit descriptor is not supported");
624 if (tx_conf->tx_free_thresh != 0)
625 sfcr_warn(sr, "TxQ free threshold is not supported");
627 if (tx_conf->tx_thresh.pthresh != 0 ||
628 tx_conf->tx_thresh.hthresh != 0 ||
629 tx_conf->tx_thresh.wthresh != 0) {
631 "prefetch/host/writeback thresholds are not supported");
634 if (tx_conf->tx_deferred_start) {
635 sfcr_err(sr, "Deferred start is not supported");
639 sfcr_info(sr, "done: %s", rte_strerror(-ret));
645 sfc_repr_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
646 uint16_t nb_tx_desc, unsigned int socket_id,
647 const struct rte_eth_txconf *tx_conf)
649 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
650 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
651 struct sfc_repr_txq *txq;
654 sfcr_info(sr, "entry");
656 ret = sfc_repr_tx_qcheck_conf(sr, tx_conf);
658 goto fail_check_conf;
661 txq = rte_zmalloc_socket("sfc-repr-txq", sizeof(*txq),
662 RTE_CACHE_LINE_SIZE, socket_id);
666 ret = sfc_repr_ring_create(srs->pf_port_id, srs->repr_id,
667 "tx", tx_queue_id, nb_tx_desc,
668 socket_id, &txq->ring);
670 goto fail_ring_create;
672 ret = sfc_repr_proxy_add_txq(srs->pf_port_id, srs->repr_id,
673 tx_queue_id, txq->ring,
676 goto fail_proxy_add_txq;
678 dev->data->tx_queues[tx_queue_id] = txq;
680 sfcr_info(sr, "done");
685 rte_ring_free(txq->ring);
692 sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
697 sfc_repr_tx_queue_release(struct rte_eth_dev *dev, uint16_t tx_queue_id)
699 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
700 struct sfc_repr_txq *txq = dev->data->tx_queues[tx_queue_id];
702 sfc_repr_proxy_del_txq(srs->pf_port_id, srs->repr_id, tx_queue_id);
703 rte_ring_free(txq->ring);
708 sfc_repr_close(struct sfc_repr *sr)
710 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
712 SFC_ASSERT(sr->state == SFC_ETHDEV_CONFIGURED);
713 sr->state = SFC_ETHDEV_CLOSING;
715 /* Put representor close actions here */
717 sr->state = SFC_ETHDEV_INITIALIZED;
721 sfc_repr_dev_close(struct rte_eth_dev *dev)
723 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
724 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
727 sfcr_info(sr, "entry");
731 case SFC_ETHDEV_STARTED:
733 SFC_ASSERT(sr->state == SFC_ETHDEV_CONFIGURED);
735 case SFC_ETHDEV_CONFIGURED:
737 SFC_ASSERT(sr->state == SFC_ETHDEV_INITIALIZED);
739 case SFC_ETHDEV_INITIALIZED:
742 sfcr_err(sr, "unexpected adapter state %u on close", sr->state);
746 for (i = 0; i < dev->data->nb_rx_queues; i++) {
747 sfc_repr_rx_queue_release(dev, i);
748 dev->data->rx_queues[i] = NULL;
751 for (i = 0; i < dev->data->nb_tx_queues; i++) {
752 sfc_repr_tx_queue_release(dev, i);
753 dev->data->tx_queues[i] = NULL;
757 * Cleanup all resources.
758 * Rollback primary process sfc_repr_eth_dev_init() below.
761 (void)sfc_repr_proxy_del_port(srs->pf_port_id, srs->repr_id);
766 sfc_repr_lock_fini(sr);
768 sfcr_info(sr, "done");
775 static const struct eth_dev_ops sfc_repr_dev_ops = {
776 .dev_configure = sfc_repr_dev_configure,
777 .dev_start = sfc_repr_dev_start,
778 .dev_stop = sfc_repr_dev_stop,
779 .dev_close = sfc_repr_dev_close,
780 .dev_infos_get = sfc_repr_dev_infos_get,
781 .link_update = sfc_repr_dev_link_update,
782 .rx_queue_setup = sfc_repr_rx_queue_setup,
783 .rx_queue_release = sfc_repr_rx_queue_release,
784 .tx_queue_setup = sfc_repr_tx_queue_setup,
785 .tx_queue_release = sfc_repr_tx_queue_release,
789 struct sfc_repr_init_data {
792 uint16_t switch_domain_id;
793 efx_mport_sel_t mport_sel;
797 sfc_repr_assign_mae_switch_port(uint16_t switch_domain_id,
798 const struct sfc_mae_switch_port_request *req,
799 uint16_t *switch_port_id)
803 rc = sfc_mae_assign_switch_port(switch_domain_id, req, switch_port_id);
810 sfc_repr_eth_dev_init(struct rte_eth_dev *dev, void *init_params)
812 const struct sfc_repr_init_data *repr_data = init_params;
813 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
814 struct sfc_mae_switch_port_request switch_port_request;
815 efx_mport_sel_t ethdev_mport_sel;
820 * Currently there is no mport we can use for representor's
821 * ethdev. Use an invalid one for now. This way representors
822 * can be instantiated.
824 efx_mae_mport_invalid(ðdev_mport_sel);
826 memset(&switch_port_request, 0, sizeof(switch_port_request));
827 switch_port_request.type = SFC_MAE_SWITCH_PORT_REPRESENTOR;
828 switch_port_request.ethdev_mportp = ðdev_mport_sel;
829 switch_port_request.entity_mportp = &repr_data->mport_sel;
830 switch_port_request.ethdev_port_id = dev->data->port_id;
832 ret = sfc_repr_assign_mae_switch_port(repr_data->switch_domain_id,
833 &switch_port_request,
834 &srs->switch_port_id);
837 "%s() failed to assign MAE switch port (domain id %u)",
838 __func__, repr_data->switch_domain_id);
839 goto fail_mae_assign_switch_port;
842 ret = sfc_repr_proxy_add_port(repr_data->pf_port_id,
845 &repr_data->mport_sel);
847 SFC_GENERIC_LOG(ERR, "%s() failed to add repr proxy port",
851 goto fail_create_port;
855 * Allocate process private data from heap, since it should not
856 * be located in shared memory allocated using rte_malloc() API.
858 sr = calloc(1, sizeof(*sr));
864 sfc_repr_lock_init(sr);
867 dev->process_private = sr;
869 srs->pf_port_id = repr_data->pf_port_id;
870 srs->repr_id = repr_data->repr_id;
871 srs->switch_domain_id = repr_data->switch_domain_id;
873 dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
874 dev->data->representor_id = srs->repr_id;
875 dev->data->backer_port_id = srs->pf_port_id;
877 dev->data->mac_addrs = rte_zmalloc("sfcr", RTE_ETHER_ADDR_LEN, 0);
878 if (dev->data->mac_addrs == NULL) {
883 dev->dev_ops = &sfc_repr_dev_ops;
885 sr->state = SFC_ETHDEV_INITIALIZED;
895 (void)sfc_repr_proxy_del_port(repr_data->pf_port_id,
899 fail_mae_assign_switch_port:
900 SFC_GENERIC_LOG(ERR, "%s() failed: %s", __func__, rte_strerror(-ret));
905 sfc_repr_create(struct rte_eth_dev *parent, uint16_t representor_id,
906 uint16_t switch_domain_id, const efx_mport_sel_t *mport_sel)
908 struct sfc_repr_init_data repr_data;
909 char name[RTE_ETH_NAME_MAX_LEN];
912 if (snprintf(name, sizeof(name), "net_%s_representor_%u",
913 parent->device->name, representor_id) >=
915 SFC_GENERIC_LOG(ERR, "%s() failed name too long", __func__);
916 return -ENAMETOOLONG;
919 memset(&repr_data, 0, sizeof(repr_data));
920 repr_data.pf_port_id = parent->data->port_id;
921 repr_data.repr_id = representor_id;
922 repr_data.switch_domain_id = switch_domain_id;
923 repr_data.mport_sel = *mport_sel;
925 ret = rte_eth_dev_create(parent->device, name,
926 sizeof(struct sfc_repr_shared),
928 sfc_repr_eth_dev_init, &repr_data);
930 SFC_GENERIC_LOG(ERR, "%s() failed to create device", __func__);
932 SFC_GENERIC_LOG(INFO, "%s() done: %s", __func__, rte_strerror(-ret));