1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/sockios.h>
27 #include <rte_atomic.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_bus_pci.h>
31 #include <rte_common.h>
32 #include <rte_interrupts.h>
33 #include <rte_malloc.h>
34 #include <rte_string_fns.h>
35 #include <rte_rwlock.h>
38 #include "mlx5_glue.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
42 /* Supported speed values found in /usr/include/linux/ethtool.h */
43 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
44 #define SUPPORTED_40000baseKR4_Full (1 << 23)
46 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
47 #define SUPPORTED_40000baseCR4_Full (1 << 24)
49 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
50 #define SUPPORTED_40000baseSR4_Full (1 << 25)
52 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
53 #define SUPPORTED_40000baseLR4_Full (1 << 26)
55 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
56 #define SUPPORTED_56000baseKR4_Full (1 << 27)
58 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
59 #define SUPPORTED_56000baseCR4_Full (1 << 28)
61 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
62 #define SUPPORTED_56000baseSR4_Full (1 << 29)
64 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
65 #define SUPPORTED_56000baseLR4_Full (1 << 30)
68 /* Add defines in case the running kernel is not the same as user headers. */
69 #ifndef ETHTOOL_GLINKSETTINGS
70 struct ethtool_link_settings {
79 uint8_t eth_tp_mdix_ctrl;
80 int8_t link_mode_masks_nwords;
82 uint32_t link_mode_masks[];
85 #define ETHTOOL_GLINKSETTINGS 0x0000004c
86 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
87 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
88 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
89 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
90 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
91 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
92 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
93 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
94 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
95 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
96 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
97 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
98 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
99 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
100 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
101 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
103 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
104 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
105 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
106 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
108 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
109 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
110 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
112 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
113 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
114 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
115 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
116 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
120 * Get master interface name from private structure.
123 * Pointer to Ethernet device.
125 * Interface name output buffer.
128 * 0 on success, a negative errno value otherwise and rte_errno is set.
131 mlx5_get_master_ifname(const struct rte_eth_dev *dev,
132 char (*ifname)[IF_NAMESIZE])
134 struct mlx5_priv *priv = dev->data->dev_private;
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
144 MKSTR(path, "%s/device/net", priv->sh->ibdev_path);
152 while ((dent = readdir(dir)) != NULL) {
153 char *name = dent->d_name;
155 unsigned int dev_port;
158 if ((name[0] == '.') &&
159 ((name[1] == '\0') ||
160 ((name[1] == '.') && (name[2] == '\0'))))
163 MKSTR(path, "%s/device/net/%s/%s",
164 priv->sh->ibdev_path, name,
165 (dev_type ? "dev_id" : "dev_port"));
167 file = fopen(path, "rb");
172 * Switch to dev_id when dev_port does not exist as
173 * is the case with Linux kernel versions < 3.15.
184 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
189 * Switch to dev_id when dev_port returns the same value for
190 * all ports. May happen when using a MOFED release older than
191 * 3.0 with a Linux kernel >= 3.15.
193 if (dev_port == dev_port_prev)
195 dev_port_prev = dev_port;
197 strlcpy(match, name, sizeof(match));
200 if (match[0] == '\0') {
204 strncpy(*ifname, match, sizeof(*ifname));
209 * Get interface name from private structure.
211 * This is a port representor-aware version of mlx5_get_master_ifname().
214 * Pointer to Ethernet device.
216 * Interface name output buffer.
219 * 0 on success, a negative errno value otherwise and rte_errno is set.
222 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
224 struct mlx5_priv *priv = dev->data->dev_private;
225 unsigned int ifindex =
226 priv->nl_socket_rdma >= 0 ?
227 mlx5_nl_ifindex(priv->nl_socket_rdma,
228 priv->sh->ibdev_name,
232 if (!priv->representor)
233 return mlx5_get_master_ifname(dev, ifname);
237 if (if_indextoname(ifindex, &(*ifname)[0]))
244 * Get the interface index from device name.
247 * Pointer to Ethernet device.
250 * Nonzero interface index on success, zero otherwise and rte_errno is set.
253 mlx5_ifindex(const struct rte_eth_dev *dev)
255 char ifname[IF_NAMESIZE];
256 unsigned int ifindex;
258 if (mlx5_get_ifname(dev, &ifname))
260 ifindex = if_nametoindex(ifname);
267 * Perform ifreq ioctl() on associated Ethernet device.
270 * Pointer to Ethernet device.
272 * Request number to pass to ioctl().
274 * Interface request structure output buffer.
277 * 0 on success, a negative errno value otherwise and rte_errno is set.
280 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
282 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
289 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
292 ret = ioctl(sock, req, ifr);
308 * Pointer to Ethernet device.
310 * MTU value output buffer.
313 * 0 on success, a negative errno value otherwise and rte_errno is set.
316 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
318 struct ifreq request;
319 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
323 *mtu = request.ifr_mtu;
331 * Pointer to Ethernet device.
336 * 0 on success, a negative errno value otherwise and rte_errno is set.
339 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
341 struct ifreq request = { .ifr_mtu = mtu, };
343 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
350 * Pointer to Ethernet device.
352 * Bitmask for flags that must remain untouched.
354 * Bitmask for flags to modify.
357 * 0 on success, a negative errno value otherwise and rte_errno is set.
360 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
362 struct ifreq request;
363 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
367 request.ifr_flags &= keep;
368 request.ifr_flags |= flags & ~keep;
369 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
373 * DPDK callback for Ethernet device configuration.
376 * Pointer to Ethernet device structure.
379 * 0 on success, a negative errno value otherwise and rte_errno is set.
382 mlx5_dev_configure(struct rte_eth_dev *dev)
384 struct mlx5_priv *priv = dev->data->dev_private;
385 unsigned int rxqs_n = dev->data->nb_rx_queues;
386 unsigned int txqs_n = dev->data->nb_tx_queues;
389 unsigned int reta_idx_n;
390 const uint8_t use_app_rss_key =
391 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
394 if (use_app_rss_key &&
395 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
396 MLX5_RSS_HASH_KEY_LEN)) {
397 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
398 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
402 priv->rss_conf.rss_key =
403 rte_realloc(priv->rss_conf.rss_key,
404 MLX5_RSS_HASH_KEY_LEN, 0);
405 if (!priv->rss_conf.rss_key) {
406 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
407 dev->data->port_id, rxqs_n);
411 memcpy(priv->rss_conf.rss_key,
413 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
414 rss_hash_default_key,
415 MLX5_RSS_HASH_KEY_LEN);
416 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
417 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
418 priv->rxqs = (void *)dev->data->rx_queues;
419 priv->txqs = (void *)dev->data->tx_queues;
420 if (txqs_n != priv->txqs_n) {
421 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
422 dev->data->port_id, priv->txqs_n, txqs_n);
423 priv->txqs_n = txqs_n;
425 if (rxqs_n > priv->config.ind_table_max_size) {
426 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
427 dev->data->port_id, rxqs_n);
431 if (rxqs_n == priv->rxqs_n)
433 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
434 dev->data->port_id, priv->rxqs_n, rxqs_n);
435 priv->rxqs_n = rxqs_n;
436 /* If the requested number of RX queues is not a power of two, use the
437 * maximum indirection table size for better balancing.
438 * The result is always rounded to the next power of two. */
439 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
440 priv->config.ind_table_max_size :
442 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
445 /* When the number of RX queues is not a power of two, the remaining
446 * table entries are padded with reused WQs and hashes are not spread
448 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
449 (*priv->reta_idx)[i] = j;
457 * Sets default tuning parameters.
460 * Pointer to Ethernet device.
462 * Info structure output buffer.
465 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
467 struct mlx5_priv *priv = dev->data->dev_private;
469 /* Minimum CPU utilization. */
470 info->default_rxportconf.ring_size = 256;
471 info->default_txportconf.ring_size = 256;
472 info->default_rxportconf.burst_size = 64;
473 info->default_txportconf.burst_size = 64;
474 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
475 info->default_rxportconf.nb_queues = 16;
476 info->default_txportconf.nb_queues = 16;
477 if (dev->data->nb_rx_queues > 2 ||
478 dev->data->nb_tx_queues > 2) {
479 /* Max Throughput. */
480 info->default_rxportconf.ring_size = 2048;
481 info->default_txportconf.ring_size = 2048;
484 info->default_rxportconf.nb_queues = 8;
485 info->default_txportconf.nb_queues = 8;
486 if (dev->data->nb_rx_queues > 2 ||
487 dev->data->nb_tx_queues > 2) {
488 /* Max Throughput. */
489 info->default_rxportconf.ring_size = 4096;
490 info->default_txportconf.ring_size = 4096;
496 * DPDK callback to get information about the device.
499 * Pointer to Ethernet device structure.
501 * Info structure output buffer.
504 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
506 struct mlx5_priv *priv = dev->data->dev_private;
507 struct mlx5_dev_config *config = &priv->config;
509 char ifname[IF_NAMESIZE];
511 /* FIXME: we should ask the device for these values. */
512 info->min_rx_bufsize = 32;
513 info->max_rx_pktlen = 65536;
515 * Since we need one CQ per QP, the limit is the minimum number
516 * between the two values.
518 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
519 priv->device_attr.orig_attr.max_qp);
520 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
523 info->max_rx_queues = max;
524 info->max_tx_queues = max;
525 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
526 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
527 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
528 info->rx_queue_offload_capa);
529 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
530 if (mlx5_get_ifname(dev, &ifname) == 0)
531 info->if_index = if_nametoindex(ifname);
532 info->reta_size = priv->reta_idx_n ?
533 priv->reta_idx_n : config->ind_table_max_size;
534 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
535 info->speed_capa = priv->link_speed_capa;
536 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
537 mlx5_set_default_params(dev, info);
538 info->switch_info.name = dev->data->name;
539 info->switch_info.domain_id = priv->domain_id;
540 info->switch_info.port_id = priv->representor_id;
541 if (priv->representor) {
542 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
545 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
547 struct mlx5_priv *opriv =
548 rte_eth_devices[port_id[i]].data->dev_private;
551 opriv->representor ||
552 opriv->domain_id != priv->domain_id)
555 * Override switch name with that of the master
558 info->switch_info.name = opriv->dev_data->name;
565 * Get firmware version of a device.
568 * Ethernet device port.
570 * String output allocated by caller.
572 * Size of the output string, including terminating null byte.
575 * 0 on success, or the size of the non truncated string if too big.
577 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
579 struct mlx5_priv *priv = dev->data->dev_private;
580 struct ibv_device_attr *attr = &priv->device_attr.orig_attr;
581 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
586 strlcpy(fw_ver, attr->fw_ver, fw_size);
591 * Get supported packet types.
594 * Pointer to Ethernet device structure.
597 * A pointer to the supported Packet types array.
600 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
602 static const uint32_t ptypes[] = {
603 /* refers to rxq_cq_to_pkt_type() */
605 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
606 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
607 RTE_PTYPE_L4_NONFRAG,
611 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
612 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
613 RTE_PTYPE_INNER_L4_NONFRAG,
614 RTE_PTYPE_INNER_L4_FRAG,
615 RTE_PTYPE_INNER_L4_TCP,
616 RTE_PTYPE_INNER_L4_UDP,
620 if (dev->rx_pkt_burst == mlx5_rx_burst ||
621 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
622 dev->rx_pkt_burst == mlx5_rx_burst_vec)
628 * DPDK callback to retrieve physical link information.
631 * Pointer to Ethernet device structure.
633 * Storage for current link status.
636 * 0 on success, a negative errno value otherwise and rte_errno is set.
639 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
640 struct rte_eth_link *link)
642 struct mlx5_priv *priv = dev->data->dev_private;
643 struct ethtool_cmd edata = {
644 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
647 struct rte_eth_link dev_link;
651 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
653 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
654 dev->data->port_id, strerror(rte_errno));
657 dev_link = (struct rte_eth_link) {
658 .link_status = ((ifr.ifr_flags & IFF_UP) &&
659 (ifr.ifr_flags & IFF_RUNNING)),
661 ifr = (struct ifreq) {
662 .ifr_data = (void *)&edata,
664 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
667 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
668 dev->data->port_id, strerror(rte_errno));
671 link_speed = ethtool_cmd_speed(&edata);
672 if (link_speed == -1)
673 dev_link.link_speed = ETH_SPEED_NUM_NONE;
675 dev_link.link_speed = link_speed;
676 priv->link_speed_capa = 0;
677 if (edata.supported & SUPPORTED_Autoneg)
678 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
679 if (edata.supported & (SUPPORTED_1000baseT_Full |
680 SUPPORTED_1000baseKX_Full))
681 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
682 if (edata.supported & SUPPORTED_10000baseKR_Full)
683 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
684 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
685 SUPPORTED_40000baseCR4_Full |
686 SUPPORTED_40000baseSR4_Full |
687 SUPPORTED_40000baseLR4_Full))
688 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
689 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
690 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
691 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
692 ETH_LINK_SPEED_FIXED);
693 if (((dev_link.link_speed && !dev_link.link_status) ||
694 (!dev_link.link_speed && dev_link.link_status))) {
703 * Retrieve physical link information (unlocked version using new ioctl).
706 * Pointer to Ethernet device structure.
708 * Storage for current link status.
711 * 0 on success, a negative errno value otherwise and rte_errno is set.
714 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
715 struct rte_eth_link *link)
718 struct mlx5_priv *priv = dev->data->dev_private;
719 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
721 struct rte_eth_link dev_link;
725 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
727 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
728 dev->data->port_id, strerror(rte_errno));
731 dev_link = (struct rte_eth_link) {
732 .link_status = ((ifr.ifr_flags & IFF_UP) &&
733 (ifr.ifr_flags & IFF_RUNNING)),
735 ifr = (struct ifreq) {
736 .ifr_data = (void *)&gcmd,
738 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
741 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
743 dev->data->port_id, strerror(rte_errno));
746 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
748 alignas(struct ethtool_link_settings)
749 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
750 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
751 struct ethtool_link_settings *ecmd = (void *)data;
754 ifr.ifr_data = (void *)ecmd;
755 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
758 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
760 dev->data->port_id, strerror(rte_errno));
763 dev_link.link_speed = ecmd->speed;
764 sc = ecmd->link_mode_masks[0] |
765 ((uint64_t)ecmd->link_mode_masks[1] << 32);
766 priv->link_speed_capa = 0;
767 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
768 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
769 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
770 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
771 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
772 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
773 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
774 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
775 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
776 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
777 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
778 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
779 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
780 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
781 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
782 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
783 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
784 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
785 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
786 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
787 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
788 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
789 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
790 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
791 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
792 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
793 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
794 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
795 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
796 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
797 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
798 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
799 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
800 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
801 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
802 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
803 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
804 ETH_LINK_SPEED_FIXED);
805 if (((dev_link.link_speed && !dev_link.link_status) ||
806 (!dev_link.link_speed && dev_link.link_status))) {
815 * DPDK callback to retrieve physical link information.
818 * Pointer to Ethernet device structure.
819 * @param wait_to_complete
820 * Wait for request completion.
823 * 0 if link status was not updated, positive if it was, a negative errno
824 * value otherwise and rte_errno is set.
827 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
830 struct rte_eth_link dev_link;
831 time_t start_time = time(NULL);
834 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
836 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
839 /* Handle wait to complete situation. */
840 if (wait_to_complete && ret == -EAGAIN) {
841 if (abs((int)difftime(time(NULL), start_time)) <
842 MLX5_LINK_STATUS_TIMEOUT) {
849 } else if (ret < 0) {
852 } while (wait_to_complete);
853 ret = !!memcmp(&dev->data->dev_link, &dev_link,
854 sizeof(struct rte_eth_link));
855 dev->data->dev_link = dev_link;
860 * DPDK callback to change the MTU.
863 * Pointer to Ethernet device structure.
868 * 0 on success, a negative errno value otherwise and rte_errno is set.
871 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
873 struct mlx5_priv *priv = dev->data->dev_private;
874 uint16_t kern_mtu = 0;
877 ret = mlx5_get_mtu(dev, &kern_mtu);
880 /* Set kernel interface MTU first. */
881 ret = mlx5_set_mtu(dev, mtu);
884 ret = mlx5_get_mtu(dev, &kern_mtu);
887 if (kern_mtu == mtu) {
889 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
890 dev->data->port_id, mtu);
898 * DPDK callback to get flow control status.
901 * Pointer to Ethernet device structure.
902 * @param[out] fc_conf
903 * Flow control output buffer.
906 * 0 on success, a negative errno value otherwise and rte_errno is set.
909 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
912 struct ethtool_pauseparam ethpause = {
913 .cmd = ETHTOOL_GPAUSEPARAM
917 ifr.ifr_data = (void *)ðpause;
918 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
921 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
923 dev->data->port_id, strerror(rte_errno));
926 fc_conf->autoneg = ethpause.autoneg;
927 if (ethpause.rx_pause && ethpause.tx_pause)
928 fc_conf->mode = RTE_FC_FULL;
929 else if (ethpause.rx_pause)
930 fc_conf->mode = RTE_FC_RX_PAUSE;
931 else if (ethpause.tx_pause)
932 fc_conf->mode = RTE_FC_TX_PAUSE;
934 fc_conf->mode = RTE_FC_NONE;
939 * DPDK callback to modify flow control parameters.
942 * Pointer to Ethernet device structure.
944 * Flow control parameters.
947 * 0 on success, a negative errno value otherwise and rte_errno is set.
950 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
953 struct ethtool_pauseparam ethpause = {
954 .cmd = ETHTOOL_SPAUSEPARAM
958 ifr.ifr_data = (void *)ðpause;
959 ethpause.autoneg = fc_conf->autoneg;
960 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
961 (fc_conf->mode & RTE_FC_RX_PAUSE))
962 ethpause.rx_pause = 1;
964 ethpause.rx_pause = 0;
966 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
967 (fc_conf->mode & RTE_FC_TX_PAUSE))
968 ethpause.tx_pause = 1;
970 ethpause.tx_pause = 0;
971 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
974 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
976 dev->data->port_id, strerror(rte_errno));
983 * Get PCI information from struct ibv_device.
986 * Pointer to Ethernet device structure.
987 * @param[out] pci_addr
988 * PCI bus address output buffer.
991 * 0 on success, a negative errno value otherwise and rte_errno is set.
994 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
995 struct rte_pci_addr *pci_addr)
999 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1001 file = fopen(path, "rb");
1006 while (fgets(line, sizeof(line), file) == line) {
1007 size_t len = strlen(line);
1010 /* Truncate long lines. */
1011 if (len == (sizeof(line) - 1))
1012 while (line[(len - 1)] != '\n') {
1016 line[(len - 1)] = ret;
1018 /* Extract information. */
1021 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1025 &pci_addr->function) == 4) {
1035 * Device status handler.
1038 * Pointer to Ethernet device.
1040 * Pointer to event flags holder.
1043 * Events bitmap of callback process which can be called immediately.
1046 mlx5_dev_status_handler(struct rte_eth_dev *dev)
1048 struct mlx5_priv *priv = dev->data->dev_private;
1049 struct ibv_async_event event;
1052 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1056 /* Read all message and acknowledge them. */
1058 if (mlx5_glue->get_async_event(priv->ctx, &event))
1060 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1061 event.event_type == IBV_EVENT_PORT_ERR) &&
1062 (dev->data->dev_conf.intr_conf.lsc == 1))
1063 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1064 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1065 dev->data->dev_conf.intr_conf.rmv == 1)
1066 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1069 "port %u event type %d on not handled",
1070 dev->data->port_id, event.event_type);
1071 mlx5_glue->ack_async_event(&event);
1077 * Handle interrupts from the NIC.
1079 * @param[in] intr_handle
1080 * Interrupt handler.
1082 * Callback argument.
1085 mlx5_dev_interrupt_handler(void *cb_arg)
1087 struct rte_eth_dev *dev = cb_arg;
1090 events = mlx5_dev_status_handler(dev);
1091 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1092 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1093 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1094 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1098 * Handle interrupts from the socket.
1101 * Callback argument.
1104 mlx5_dev_handler_socket(void *cb_arg)
1106 struct rte_eth_dev *dev = cb_arg;
1108 mlx5_socket_handle(dev);
1112 * Uninstall interrupt handler.
1115 * Pointer to Ethernet device.
1118 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1120 struct mlx5_priv *priv = dev->data->dev_private;
1122 if (dev->data->dev_conf.intr_conf.lsc ||
1123 dev->data->dev_conf.intr_conf.rmv)
1124 rte_intr_callback_unregister(&priv->intr_handle,
1125 mlx5_dev_interrupt_handler, dev);
1126 if (priv->primary_socket)
1127 rte_intr_callback_unregister(&priv->intr_handle_socket,
1128 mlx5_dev_handler_socket, dev);
1129 priv->intr_handle.fd = 0;
1130 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1131 priv->intr_handle_socket.fd = 0;
1132 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1136 * Install interrupt handler.
1139 * Pointer to Ethernet device.
1142 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1144 struct mlx5_priv *priv = dev->data->dev_private;
1148 assert(priv->ctx->async_fd > 0);
1149 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1150 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1153 "port %u failed to change file descriptor async event"
1155 dev->data->port_id);
1156 dev->data->dev_conf.intr_conf.lsc = 0;
1157 dev->data->dev_conf.intr_conf.rmv = 0;
1159 if (dev->data->dev_conf.intr_conf.lsc ||
1160 dev->data->dev_conf.intr_conf.rmv) {
1161 priv->intr_handle.fd = priv->ctx->async_fd;
1162 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1163 rte_intr_callback_register(&priv->intr_handle,
1164 mlx5_dev_interrupt_handler, dev);
1166 ret = mlx5_socket_init(dev);
1168 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1169 dev->data->port_id, strerror(rte_errno));
1170 else if (priv->primary_socket) {
1171 priv->intr_handle_socket.fd = priv->primary_socket;
1172 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1173 rte_intr_callback_register(&priv->intr_handle_socket,
1174 mlx5_dev_handler_socket, dev);
1179 * DPDK callback to bring the link DOWN.
1182 * Pointer to Ethernet device structure.
1185 * 0 on success, a negative errno value otherwise and rte_errno is set.
1188 mlx5_set_link_down(struct rte_eth_dev *dev)
1190 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1194 * DPDK callback to bring the link UP.
1197 * Pointer to Ethernet device structure.
1200 * 0 on success, a negative errno value otherwise and rte_errno is set.
1203 mlx5_set_link_up(struct rte_eth_dev *dev)
1205 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1209 * Configure the TX function to use.
1212 * Pointer to private data structure.
1215 * Pointer to selected Tx burst function.
1218 mlx5_select_tx_function(struct rte_eth_dev *dev)
1220 struct mlx5_priv *priv = dev->data->dev_private;
1221 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1222 struct mlx5_dev_config *config = &priv->config;
1223 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1224 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1225 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1226 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1227 DEV_TX_OFFLOAD_IP_TNL_TSO |
1228 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1229 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1230 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1231 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1232 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1234 assert(priv != NULL);
1235 /* Select appropriate TX function. */
1236 if (vlan_insert || tso || swp)
1237 return tx_pkt_burst;
1238 if (config->mps == MLX5_MPW_ENHANCED) {
1239 if (mlx5_check_vec_tx_support(dev) > 0) {
1240 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1241 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1243 tx_pkt_burst = mlx5_tx_burst_vec;
1245 "port %u selected enhanced MPW Tx vectorized"
1247 dev->data->port_id);
1249 tx_pkt_burst = mlx5_tx_burst_empw;
1251 "port %u selected enhanced MPW Tx function",
1252 dev->data->port_id);
1254 } else if (config->mps && (config->txq_inline > 0)) {
1255 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1256 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1257 dev->data->port_id);
1258 } else if (config->mps) {
1259 tx_pkt_burst = mlx5_tx_burst_mpw;
1260 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1261 dev->data->port_id);
1263 return tx_pkt_burst;
1267 * Configure the RX function to use.
1270 * Pointer to private data structure.
1273 * Pointer to selected Rx burst function.
1276 mlx5_select_rx_function(struct rte_eth_dev *dev)
1278 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1280 assert(dev != NULL);
1281 if (mlx5_check_vec_rx_support(dev) > 0) {
1282 rx_pkt_burst = mlx5_rx_burst_vec;
1283 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1284 dev->data->port_id);
1285 } else if (mlx5_mprq_enabled(dev)) {
1286 rx_pkt_burst = mlx5_rx_burst_mprq;
1288 return rx_pkt_burst;
1292 * Check if mlx5 device was removed.
1295 * Pointer to Ethernet device structure.
1298 * 1 when device is removed, otherwise 0.
1301 mlx5_is_removed(struct rte_eth_dev *dev)
1303 struct ibv_device_attr device_attr;
1304 struct mlx5_priv *priv = dev->data->dev_private;
1306 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1312 * Get port ID list of mlx5 instances sharing a common device.
1315 * Device to look for.
1316 * @param[out] port_list
1317 * Result buffer for collected port IDs.
1318 * @param port_list_n
1319 * Maximum number of entries in result buffer. If 0, @p port_list can be
1323 * Number of matching instances regardless of the @p port_list_n
1324 * parameter, 0 if none were found.
1327 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1328 unsigned int port_list_n)
1333 RTE_ETH_FOREACH_DEV(id) {
1334 struct rte_eth_dev *ldev = &rte_eth_devices[id];
1336 if (ldev->device != dev)
1338 if (n < port_list_n)
1346 * Get switch information associated with network interface.
1349 * Network interface index.
1351 * Switch information object, populated in case of success.
1354 * 0 on success, a negative errno value otherwise and rte_errno is set.
1357 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1359 char ifname[IF_NAMESIZE];
1360 char port_name[IF_NAMESIZE];
1362 struct mlx5_switch_info data = {
1370 bool port_name_set = false;
1371 bool port_switch_id_set = false;
1372 bool device_dir = false;
1376 if (!if_indextoname(ifindex, ifname)) {
1381 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1383 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1385 MKSTR(pci_device, "/sys/class/net/%s/device",
1388 file = fopen(phys_port_name, "rb");
1390 ret = fscanf(file, "%s", port_name);
1393 port_name_set = mlx5_translate_port_name(port_name,
1396 file = fopen(phys_switch_id, "rb");
1401 port_switch_id_set =
1402 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1405 dir = opendir(pci_device);
1410 data.master = port_switch_id_set && (!port_name_set || device_dir);
1411 data.representor = port_switch_id_set && port_name_set && !device_dir;
1413 assert(!(data.master && data.representor));
1414 if (data.master && data.representor) {
1415 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1416 " and as representor", ifindex);
1424 * Extract port name, as a number, from sysfs or netlink information.
1426 * @param[in] port_name_in
1427 * String representing the port name.
1428 * @param[out] port_info_out
1429 * Port information, including port name as a number.
1432 * true on success, false otherwise.
1435 mlx5_translate_port_name(const char *port_name_in,
1436 struct mlx5_switch_info *port_info_out)
1438 char pf_c1, pf_c2, vf_c1, vf_c2;
1441 bool port_name_set = false;
1444 * Check for port-name as a string of the form pf0vf0
1445 * (support kernel ver >= 5.0)
1447 port_name_set = (sscanf(port_name_in, "%c%c%d%c%c%d", &pf_c1, &pf_c2,
1448 &pf_num, &vf_c1, &vf_c2,
1449 &port_info_out->port_name) == 6);
1450 if (port_name_set) {
1451 port_info_out->port_name_new = 1;
1453 /* Check for port-name as a number (support kernel ver < 5.0 */
1455 port_info_out->port_name = strtol(port_name_in, &end, 0);
1457 (size_t)(end - port_name_in) == strlen(port_name_in))
1458 port_name_set = true;
1460 return port_name_set;