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.
12 #include <rte_ethdev.h>
13 #include <rte_malloc.h>
14 #include <ethdev_driver.h>
19 #include "sfc_debug.h"
21 #include "sfc_ethdev_state.h"
22 #include "sfc_switch.h"
24 /** Multi-process shared representor private data */
25 struct sfc_repr_shared {
28 uint16_t switch_domain_id;
29 uint16_t switch_port_id;
32 /** Primary process representor private data */
35 * PMD setup and configuration is not thread safe. Since it is not
36 * performance sensitive, it is better to guarantee thread-safety
37 * and add device level lock. Adapter control operations which
38 * change its state should acquire the lock.
41 enum sfc_ethdev_state state;
44 #define sfcr_err(sr, ...) \
46 const struct sfc_repr *_sr = (sr); \
49 SFC_GENERIC_LOG(ERR, __VA_ARGS__); \
52 #define sfcr_info(sr, ...) \
54 const struct sfc_repr *_sr = (sr); \
57 SFC_GENERIC_LOG(INFO, \
59 RTE_FMT_HEAD(__VA_ARGS__ ,), \
61 RTE_FMT_TAIL(__VA_ARGS__ ,))); \
64 static inline struct sfc_repr_shared *
65 sfc_repr_shared_by_eth_dev(struct rte_eth_dev *eth_dev)
67 struct sfc_repr_shared *srs = eth_dev->data->dev_private;
72 static inline struct sfc_repr *
73 sfc_repr_by_eth_dev(struct rte_eth_dev *eth_dev)
75 struct sfc_repr *sr = eth_dev->process_private;
81 * Add wrapper functions to acquire/release lock to be able to remove or
82 * change the lock in one place.
86 sfc_repr_lock_init(struct sfc_repr *sr)
88 rte_spinlock_init(&sr->lock);
91 #if defined(RTE_LIBRTE_SFC_EFX_DEBUG) || defined(RTE_ENABLE_ASSERT)
94 sfc_repr_lock_is_locked(struct sfc_repr *sr)
96 return rte_spinlock_is_locked(&sr->lock);
102 sfc_repr_lock(struct sfc_repr *sr)
104 rte_spinlock_lock(&sr->lock);
108 sfc_repr_unlock(struct sfc_repr *sr)
110 rte_spinlock_unlock(&sr->lock);
114 sfc_repr_lock_fini(__rte_unused struct sfc_repr *sr)
116 /* Just for symmetry of the API */
120 sfc_repr_check_conf(struct sfc_repr *sr, uint16_t nb_rx_queues,
121 const struct rte_eth_conf *conf)
123 const struct rte_eth_rss_conf *rss_conf;
126 sfcr_info(sr, "entry");
128 if (conf->link_speeds != 0) {
129 sfcr_err(sr, "specific link speeds not supported");
133 switch (conf->rxmode.mq_mode) {
135 if (nb_rx_queues != 1) {
136 sfcr_err(sr, "Rx RSS is not supported with %u queues",
142 rss_conf = &conf->rx_adv_conf.rss_conf;
143 if (rss_conf->rss_key != NULL || rss_conf->rss_key_len != 0 ||
144 rss_conf->rss_hf != 0) {
145 sfcr_err(sr, "Rx RSS configuration is not supported");
152 sfcr_err(sr, "Rx mode MQ modes other than RSS not supported");
157 if (conf->txmode.mq_mode != ETH_MQ_TX_NONE) {
158 sfcr_err(sr, "Tx mode MQ modes not supported");
162 if (conf->lpbk_mode != 0) {
163 sfcr_err(sr, "loopback not supported");
167 if (conf->dcb_capability_en != 0) {
168 sfcr_err(sr, "priority-based flow control not supported");
172 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
173 sfcr_err(sr, "Flow Director not supported");
177 if (conf->intr_conf.lsc != 0) {
178 sfcr_err(sr, "link status change interrupt not supported");
182 if (conf->intr_conf.rxq != 0) {
183 sfcr_err(sr, "receive queue interrupt not supported");
187 if (conf->intr_conf.rmv != 0) {
188 sfcr_err(sr, "remove interrupt not supported");
192 sfcr_info(sr, "done %d", ret);
199 sfc_repr_configure(struct sfc_repr *sr, uint16_t nb_rx_queues,
200 const struct rte_eth_conf *conf)
204 sfcr_info(sr, "entry");
206 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
208 ret = sfc_repr_check_conf(sr, nb_rx_queues, conf);
210 goto fail_check_conf;
212 sr->state = SFC_ETHDEV_CONFIGURED;
214 sfcr_info(sr, "done");
219 sfcr_info(sr, "failed %s", rte_strerror(-ret));
224 sfc_repr_dev_configure(struct rte_eth_dev *dev)
226 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
227 struct rte_eth_dev_data *dev_data = dev->data;
230 sfcr_info(sr, "entry n_rxq=%u n_txq=%u",
231 dev_data->nb_rx_queues, dev_data->nb_tx_queues);
235 case SFC_ETHDEV_CONFIGURED:
237 case SFC_ETHDEV_INITIALIZED:
238 ret = sfc_repr_configure(sr, dev_data->nb_rx_queues,
239 &dev_data->dev_conf);
242 sfcr_err(sr, "unexpected adapter state %u to configure",
249 sfcr_info(sr, "done %s", rte_strerror(-ret));
255 sfc_repr_dev_infos_get(struct rte_eth_dev *dev,
256 struct rte_eth_dev_info *dev_info)
258 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
260 dev_info->device = dev->device;
262 dev_info->max_rx_queues = SFC_REPR_RXQ_MAX;
263 dev_info->max_tx_queues = SFC_REPR_TXQ_MAX;
264 dev_info->default_rxconf.rx_drop_en = 1;
265 dev_info->switch_info.domain_id = srs->switch_domain_id;
266 dev_info->switch_info.port_id = srs->switch_port_id;
272 sfc_repr_close(struct sfc_repr *sr)
274 SFC_ASSERT(sfc_repr_lock_is_locked(sr));
276 SFC_ASSERT(sr->state == SFC_ETHDEV_CONFIGURED);
277 sr->state = SFC_ETHDEV_CLOSING;
279 /* Put representor close actions here */
281 sr->state = SFC_ETHDEV_INITIALIZED;
285 sfc_repr_dev_close(struct rte_eth_dev *dev)
287 struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
289 sfcr_info(sr, "entry");
293 case SFC_ETHDEV_CONFIGURED:
295 SFC_ASSERT(sr->state == SFC_ETHDEV_INITIALIZED);
297 case SFC_ETHDEV_INITIALIZED:
300 sfcr_err(sr, "unexpected adapter state %u on close", sr->state);
305 * Cleanup all resources.
306 * Rollback primary process sfc_repr_eth_dev_init() below.
312 sfc_repr_lock_fini(sr);
314 sfcr_info(sr, "done");
321 static const struct eth_dev_ops sfc_repr_dev_ops = {
322 .dev_configure = sfc_repr_dev_configure,
323 .dev_close = sfc_repr_dev_close,
324 .dev_infos_get = sfc_repr_dev_infos_get,
328 struct sfc_repr_init_data {
331 uint16_t switch_domain_id;
332 efx_mport_sel_t mport_sel;
336 sfc_repr_assign_mae_switch_port(uint16_t switch_domain_id,
337 const struct sfc_mae_switch_port_request *req,
338 uint16_t *switch_port_id)
342 rc = sfc_mae_assign_switch_port(switch_domain_id, req, switch_port_id);
349 sfc_repr_eth_dev_init(struct rte_eth_dev *dev, void *init_params)
351 const struct sfc_repr_init_data *repr_data = init_params;
352 struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
353 struct sfc_mae_switch_port_request switch_port_request;
354 efx_mport_sel_t ethdev_mport_sel;
359 * Currently there is no mport we can use for representor's
360 * ethdev. Use an invalid one for now. This way representors
361 * can be instantiated.
363 efx_mae_mport_invalid(ðdev_mport_sel);
365 memset(&switch_port_request, 0, sizeof(switch_port_request));
366 switch_port_request.type = SFC_MAE_SWITCH_PORT_REPRESENTOR;
367 switch_port_request.ethdev_mportp = ðdev_mport_sel;
368 switch_port_request.entity_mportp = &repr_data->mport_sel;
369 switch_port_request.ethdev_port_id = dev->data->port_id;
371 ret = sfc_repr_assign_mae_switch_port(repr_data->switch_domain_id,
372 &switch_port_request,
373 &srs->switch_port_id);
376 "%s() failed to assign MAE switch port (domain id %u)",
377 __func__, repr_data->switch_domain_id);
378 goto fail_mae_assign_switch_port;
382 * Allocate process private data from heap, since it should not
383 * be located in shared memory allocated using rte_malloc() API.
385 sr = calloc(1, sizeof(*sr));
391 sfc_repr_lock_init(sr);
394 dev->process_private = sr;
396 srs->pf_port_id = repr_data->pf_port_id;
397 srs->repr_id = repr_data->repr_id;
398 srs->switch_domain_id = repr_data->switch_domain_id;
400 dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
401 dev->data->representor_id = srs->repr_id;
402 dev->data->backer_port_id = srs->pf_port_id;
404 dev->data->mac_addrs = rte_zmalloc("sfcr", RTE_ETHER_ADDR_LEN, 0);
405 if (dev->data->mac_addrs == NULL) {
410 dev->dev_ops = &sfc_repr_dev_ops;
412 sr->state = SFC_ETHDEV_INITIALIZED;
422 fail_mae_assign_switch_port:
423 SFC_GENERIC_LOG(ERR, "%s() failed: %s", __func__, rte_strerror(-ret));
428 sfc_repr_create(struct rte_eth_dev *parent, uint16_t representor_id,
429 uint16_t switch_domain_id, const efx_mport_sel_t *mport_sel)
431 struct sfc_repr_init_data repr_data;
432 char name[RTE_ETH_NAME_MAX_LEN];
435 if (snprintf(name, sizeof(name), "net_%s_representor_%u",
436 parent->device->name, representor_id) >=
438 SFC_GENERIC_LOG(ERR, "%s() failed name too long", __func__);
439 return -ENAMETOOLONG;
442 memset(&repr_data, 0, sizeof(repr_data));
443 repr_data.pf_port_id = parent->data->port_id;
444 repr_data.repr_id = representor_id;
445 repr_data.switch_domain_id = switch_domain_id;
446 repr_data.mport_sel = *mport_sel;
448 ret = rte_eth_dev_create(parent->device, name,
449 sizeof(struct sfc_repr_shared),
451 sfc_repr_eth_dev_init, &repr_data);
453 SFC_GENERIC_LOG(ERR, "%s() failed to create device", __func__);
455 SFC_GENERIC_LOG(INFO, "%s() done: %s", __func__, rte_strerror(-ret));