1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
13 #include <ethdev_driver.h>
14 #include <rte_bus_pci.h>
16 #include <rte_common.h>
17 #include <rte_interrupts.h>
18 #include <rte_malloc.h>
19 #include <rte_string_fns.h>
20 #include <rte_rwlock.h>
21 #include <rte_cycles.h>
23 #include <mlx5_malloc.h>
25 #include "mlx5_rxtx.h"
28 #include "mlx5_autoconf.h"
31 * Get the interface index from device name.
34 * Pointer to Ethernet device.
37 * Nonzero interface index on success, zero otherwise and rte_errno is set.
40 mlx5_ifindex(const struct rte_eth_dev *dev)
42 struct mlx5_priv *priv = dev->data->dev_private;
46 MLX5_ASSERT(priv->if_index);
47 if (priv->master && priv->sh->bond.ifindex > 0)
48 ifindex = priv->sh->bond.ifindex;
50 ifindex = priv->if_index;
57 * DPDK callback for Ethernet device configuration.
60 * Pointer to Ethernet device structure.
63 * 0 on success, a negative errno value otherwise and rte_errno is set.
66 mlx5_dev_configure(struct rte_eth_dev *dev)
68 struct mlx5_priv *priv = dev->data->dev_private;
69 unsigned int rxqs_n = dev->data->nb_rx_queues;
70 unsigned int txqs_n = dev->data->nb_tx_queues;
71 const uint8_t use_app_rss_key =
72 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
75 if (use_app_rss_key &&
76 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
77 MLX5_RSS_HASH_KEY_LEN)) {
78 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
79 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
83 priv->rss_conf.rss_key =
84 mlx5_realloc(priv->rss_conf.rss_key, MLX5_MEM_RTE,
85 MLX5_RSS_HASH_KEY_LEN, 0, SOCKET_ID_ANY);
86 if (!priv->rss_conf.rss_key) {
87 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
88 dev->data->port_id, rxqs_n);
93 if ((dev->data->dev_conf.txmode.offloads &
94 DEV_TX_OFFLOAD_SEND_ON_TIMESTAMP) &&
95 rte_mbuf_dyn_tx_timestamp_register(NULL, NULL) != 0) {
96 DRV_LOG(ERR, "port %u cannot register Tx timestamp field/flag",
100 memcpy(priv->rss_conf.rss_key,
102 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
103 rss_hash_default_key,
104 MLX5_RSS_HASH_KEY_LEN);
105 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
106 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
107 priv->rxqs = (void *)dev->data->rx_queues;
108 priv->txqs = (void *)dev->data->tx_queues;
109 if (txqs_n != priv->txqs_n) {
110 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
111 dev->data->port_id, priv->txqs_n, txqs_n);
112 priv->txqs_n = txqs_n;
114 if (rxqs_n > priv->config.ind_table_max_size) {
115 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
116 dev->data->port_id, rxqs_n);
120 if (rxqs_n != priv->rxqs_n) {
121 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
122 dev->data->port_id, priv->rxqs_n, rxqs_n);
123 priv->rxqs_n = rxqs_n;
125 priv->skip_default_rss_reta = 0;
126 ret = mlx5_proc_priv_init(dev);
133 * Configure default RSS reta.
136 * Pointer to Ethernet device structure.
139 * 0 on success, a negative errno value otherwise and rte_errno is set.
142 mlx5_dev_configure_rss_reta(struct rte_eth_dev *dev)
144 struct mlx5_priv *priv = dev->data->dev_private;
145 unsigned int rxqs_n = dev->data->nb_rx_queues;
148 unsigned int reta_idx_n;
150 unsigned int *rss_queue_arr = NULL;
151 unsigned int rss_queue_n = 0;
153 if (priv->skip_default_rss_reta)
155 rss_queue_arr = mlx5_malloc(0, rxqs_n * sizeof(unsigned int), 0,
157 if (!rss_queue_arr) {
158 DRV_LOG(ERR, "port %u cannot allocate RSS queue list (%u)",
159 dev->data->port_id, rxqs_n);
163 for (i = 0, j = 0; i < rxqs_n; i++) {
164 struct mlx5_rxq_data *rxq_data;
165 struct mlx5_rxq_ctrl *rxq_ctrl;
167 rxq_data = (*priv->rxqs)[i];
168 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
169 if (rxq_ctrl && rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
170 rss_queue_arr[j++] = i;
173 if (rss_queue_n > priv->config.ind_table_max_size) {
174 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
175 dev->data->port_id, rss_queue_n);
177 mlx5_free(rss_queue_arr);
180 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
181 dev->data->port_id, priv->rxqs_n, rxqs_n);
182 priv->rxqs_n = rxqs_n;
184 * If the requested number of RX queues is not a power of two,
185 * use the maximum indirection table size for better balancing.
186 * The result is always rounded to the next power of two.
188 reta_idx_n = (1 << log2above((rss_queue_n & (rss_queue_n - 1)) ?
189 priv->config.ind_table_max_size :
191 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
193 mlx5_free(rss_queue_arr);
197 * When the number of RX queues is not a power of two,
198 * the remaining table entries are padded with reused WQs
199 * and hashes are not spread uniformly.
201 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
202 (*priv->reta_idx)[i] = rss_queue_arr[j];
203 if (++j == rss_queue_n)
206 mlx5_free(rss_queue_arr);
211 * Sets default tuning parameters.
214 * Pointer to Ethernet device.
216 * Info structure output buffer.
219 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
221 struct mlx5_priv *priv = dev->data->dev_private;
223 /* Minimum CPU utilization. */
224 info->default_rxportconf.ring_size = 256;
225 info->default_txportconf.ring_size = 256;
226 info->default_rxportconf.burst_size = MLX5_RX_DEFAULT_BURST;
227 info->default_txportconf.burst_size = MLX5_TX_DEFAULT_BURST;
228 if ((priv->link_speed_capa & ETH_LINK_SPEED_200G) |
229 (priv->link_speed_capa & ETH_LINK_SPEED_100G)) {
230 info->default_rxportconf.nb_queues = 16;
231 info->default_txportconf.nb_queues = 16;
232 if (dev->data->nb_rx_queues > 2 ||
233 dev->data->nb_tx_queues > 2) {
234 /* Max Throughput. */
235 info->default_rxportconf.ring_size = 2048;
236 info->default_txportconf.ring_size = 2048;
239 info->default_rxportconf.nb_queues = 8;
240 info->default_txportconf.nb_queues = 8;
241 if (dev->data->nb_rx_queues > 2 ||
242 dev->data->nb_tx_queues > 2) {
243 /* Max Throughput. */
244 info->default_rxportconf.ring_size = 4096;
245 info->default_txportconf.ring_size = 4096;
251 * Sets tx mbuf limiting parameters.
254 * Pointer to Ethernet device.
256 * Info structure output buffer.
259 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
261 struct mlx5_priv *priv = dev->data->dev_private;
262 struct mlx5_dev_config *config = &priv->config;
266 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
267 MLX5_SEND_DEF_INLINE_LEN :
268 (unsigned int)config->txq_inline_max;
269 MLX5_ASSERT(config->txq_inline_min >= 0);
270 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
271 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
272 MLX5_ESEG_MIN_INLINE_SIZE -
275 MLX5_WQE_DSEG_SIZE * 2);
276 nb_max = (MLX5_WQE_SIZE_MAX +
277 MLX5_ESEG_MIN_INLINE_SIZE -
281 inlen) / MLX5_WSEG_SIZE;
282 info->tx_desc_lim.nb_seg_max = nb_max;
283 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
287 * DPDK callback to get information about the device.
290 * Pointer to Ethernet device structure.
292 * Info structure output buffer.
295 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
297 struct mlx5_priv *priv = dev->data->dev_private;
298 struct mlx5_dev_config *config = &priv->config;
301 /* FIXME: we should ask the device for these values. */
302 info->min_rx_bufsize = 32;
303 info->max_rx_pktlen = 65536;
304 info->max_lro_pkt_size = MLX5_MAX_LRO_SIZE;
306 * Since we need one CQ per QP, the limit is the minimum number
307 * between the two values.
309 max = RTE_MIN(priv->sh->device_attr.max_cq,
310 priv->sh->device_attr.max_qp);
311 /* max_rx_queues is uint16_t. */
312 max = RTE_MIN(max, (unsigned int)UINT16_MAX);
313 info->max_rx_queues = max;
314 info->max_tx_queues = max;
315 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
316 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
317 info->rx_seg_capa.max_nseg = MLX5_MAX_RXQ_NSEG;
318 info->rx_seg_capa.multi_pools = !config->mprq.enabled;
319 info->rx_seg_capa.offset_allowed = !config->mprq.enabled;
320 info->rx_seg_capa.offset_align_log2 = 0;
321 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
322 info->rx_queue_offload_capa);
323 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
324 info->if_index = mlx5_ifindex(dev);
325 info->reta_size = priv->reta_idx_n ?
326 priv->reta_idx_n : config->ind_table_max_size;
327 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
328 info->speed_capa = priv->link_speed_capa;
329 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
330 mlx5_set_default_params(dev, info);
331 mlx5_set_txlimit_params(dev, info);
332 info->switch_info.name = dev->data->name;
333 info->switch_info.domain_id = priv->domain_id;
334 info->switch_info.port_id = priv->representor_id;
335 if (priv->representor) {
338 MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
339 struct mlx5_priv *opriv =
340 rte_eth_devices[port_id].data->dev_private;
343 opriv->representor ||
344 opriv->sh != priv->sh ||
345 opriv->domain_id != priv->domain_id)
348 * Override switch name with that of the master
351 info->switch_info.name = opriv->dev_data->name;
359 * Calculate representor ID from port switch info.
361 * Uint16 representor ID bits definition:
369 * Use this type if port is HPF.
372 * Encoded representor ID.
375 mlx5_representor_id_encode(const struct mlx5_switch_info *info,
376 enum rte_eth_representor_type hpf_type)
378 enum rte_eth_representor_type type = RTE_ETH_REPRESENTOR_VF;
379 uint16_t repr = info->port_name;
381 if (info->representor == 0)
383 if (info->name_type == MLX5_PHYS_PORT_NAME_TYPE_PFSF)
384 type = RTE_ETH_REPRESENTOR_SF;
385 if (info->name_type == MLX5_PHYS_PORT_NAME_TYPE_PFHPF) {
389 return MLX5_REPRESENTOR_ID(info->pf_num, type, repr);
393 * DPDK callback to get information about representor.
395 * Representor ID bits definition:
401 * Pointer to Ethernet device structure.
403 * Nullable info structure output buffer.
406 * negative on error, or the number of representor ranges.
409 mlx5_representor_info_get(struct rte_eth_dev *dev,
410 struct rte_eth_representor_info *info)
412 struct mlx5_priv *priv = dev->data->dev_private;
413 int n_type = 4; /* Representor types, VF, HPF@VF, SF and HPF@SF. */
414 int n_pf = 2; /* Number of PFs. */
421 n_entries = n_type * n_pf;
422 if ((uint32_t)n_entries > info->nb_ranges_alloc)
423 n_entries = info->nb_ranges_alloc;
425 info->controller = 0;
426 info->pf = priv->pf_bond >= 0 ? priv->pf_bond : 0;
427 for (pf = 0; pf < n_pf; ++pf) {
429 info->ranges[i].type = RTE_ETH_REPRESENTOR_VF;
430 info->ranges[i].controller = 0;
431 info->ranges[i].pf = pf;
432 info->ranges[i].vf = 0;
433 info->ranges[i].id_base =
434 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, 0);
435 info->ranges[i].id_end =
436 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
437 snprintf(info->ranges[i].name,
438 sizeof(info->ranges[i].name), "pf%dvf", pf);
442 /* HPF range of VF type. */
443 info->ranges[i].type = RTE_ETH_REPRESENTOR_VF;
444 info->ranges[i].controller = 0;
445 info->ranges[i].pf = pf;
446 info->ranges[i].vf = UINT16_MAX;
447 info->ranges[i].id_base =
448 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
449 info->ranges[i].id_end =
450 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
451 snprintf(info->ranges[i].name,
452 sizeof(info->ranges[i].name), "pf%dvf", pf);
457 info->ranges[i].type = RTE_ETH_REPRESENTOR_SF;
458 info->ranges[i].controller = 0;
459 info->ranges[i].pf = pf;
460 info->ranges[i].vf = 0;
461 info->ranges[i].id_base =
462 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, 0);
463 info->ranges[i].id_end =
464 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
465 snprintf(info->ranges[i].name,
466 sizeof(info->ranges[i].name), "pf%dsf", pf);
470 /* HPF range of SF type. */
471 info->ranges[i].type = RTE_ETH_REPRESENTOR_SF;
472 info->ranges[i].controller = 0;
473 info->ranges[i].pf = pf;
474 info->ranges[i].vf = UINT16_MAX;
475 info->ranges[i].id_base =
476 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
477 info->ranges[i].id_end =
478 MLX5_REPRESENTOR_ID(pf, info->ranges[i].type, -1);
479 snprintf(info->ranges[i].name,
480 sizeof(info->ranges[i].name), "pf%dsf", pf);
487 return n_type * n_pf;
491 * Get firmware version of a device.
494 * Ethernet device port.
496 * String output allocated by caller.
498 * Size of the output string, including terminating null byte.
501 * 0 on success, or the size of the non truncated string if too big.
504 mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
506 struct mlx5_priv *priv = dev->data->dev_private;
507 struct mlx5_dev_attr *attr = &priv->sh->device_attr;
508 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
513 strlcpy(fw_ver, attr->fw_ver, fw_size);
518 * Get supported packet types.
521 * Pointer to Ethernet device structure.
524 * A pointer to the supported Packet types array.
527 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
529 static const uint32_t ptypes[] = {
530 /* refers to rxq_cq_to_pkt_type() */
532 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
533 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
534 RTE_PTYPE_L4_NONFRAG,
538 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
539 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
540 RTE_PTYPE_INNER_L4_NONFRAG,
541 RTE_PTYPE_INNER_L4_FRAG,
542 RTE_PTYPE_INNER_L4_TCP,
543 RTE_PTYPE_INNER_L4_UDP,
547 if (dev->rx_pkt_burst == mlx5_rx_burst ||
548 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
549 dev->rx_pkt_burst == mlx5_rx_burst_vec ||
550 dev->rx_pkt_burst == mlx5_rx_burst_mprq_vec)
556 * DPDK callback to change the MTU.
559 * Pointer to Ethernet device structure.
564 * 0 on success, a negative errno value otherwise and rte_errno is set.
567 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
569 struct mlx5_priv *priv = dev->data->dev_private;
570 uint16_t kern_mtu = 0;
573 ret = mlx5_get_mtu(dev, &kern_mtu);
576 /* Set kernel interface MTU first. */
577 ret = mlx5_set_mtu(dev, mtu);
580 ret = mlx5_get_mtu(dev, &kern_mtu);
583 if (kern_mtu == mtu) {
585 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
586 dev->data->port_id, mtu);
594 * Configure the RX function to use.
597 * Pointer to private data structure.
600 * Pointer to selected Rx burst function.
603 mlx5_select_rx_function(struct rte_eth_dev *dev)
605 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
607 MLX5_ASSERT(dev != NULL);
608 if (mlx5_check_vec_rx_support(dev) > 0) {
609 if (mlx5_mprq_enabled(dev)) {
610 rx_pkt_burst = mlx5_rx_burst_mprq_vec;
611 DRV_LOG(DEBUG, "port %u selected vectorized"
612 " MPRQ Rx function", dev->data->port_id);
614 rx_pkt_burst = mlx5_rx_burst_vec;
615 DRV_LOG(DEBUG, "port %u selected vectorized"
616 " SPRQ Rx function", dev->data->port_id);
618 } else if (mlx5_mprq_enabled(dev)) {
619 rx_pkt_burst = mlx5_rx_burst_mprq;
620 DRV_LOG(DEBUG, "port %u selected MPRQ Rx function",
623 DRV_LOG(DEBUG, "port %u selected SPRQ Rx function",
630 * Get the E-Switch parameters by port id.
635 * Device port id is valid, skip check. This flag is useful
636 * when trials are performed from probing and device is not
637 * flagged as valid yet (in attaching process).
638 * @param[out] es_domain_id
639 * E-Switch domain id.
640 * @param[out] es_port_id
641 * The port id of the port in the E-Switch.
644 * pointer to device private data structure containing data needed
645 * on success, NULL otherwise and rte_errno is set.
648 mlx5_port_to_eswitch_info(uint16_t port, bool valid)
650 struct rte_eth_dev *dev;
651 struct mlx5_priv *priv;
653 if (port >= RTE_MAX_ETHPORTS) {
657 if (!valid && !rte_eth_dev_is_valid_port(port)) {
661 dev = &rte_eth_devices[port];
662 priv = dev->data->dev_private;
663 if (!(priv->representor || priv->master)) {
671 * Get the E-Switch parameters by device instance.
675 * @param[out] es_domain_id
676 * E-Switch domain id.
677 * @param[out] es_port_id
678 * The port id of the port in the E-Switch.
681 * pointer to device private data structure containing data needed
682 * on success, NULL otherwise and rte_errno is set.
685 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
687 struct mlx5_priv *priv;
689 priv = dev->data->dev_private;
690 if (!(priv->representor || priv->master)) {
698 * DPDK callback to retrieve hairpin capabilities.
701 * Pointer to Ethernet device structure.
703 * Storage for hairpin capability data.
706 * 0 on success, a negative errno value otherwise and rte_errno is set.
709 mlx5_hairpin_cap_get(struct rte_eth_dev *dev, struct rte_eth_hairpin_cap *cap)
711 struct mlx5_priv *priv = dev->data->dev_private;
712 struct mlx5_dev_config *config = &priv->config;
714 if (!priv->sh->devx || !config->dest_tir || !config->dv_flow_en) {
718 cap->max_nb_queues = UINT16_MAX;
719 cap->max_rx_2_tx = 1;
720 cap->max_tx_2_rx = 1;
721 cap->max_nb_desc = 8192;