1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_malloc.h>
35 #include <rte_string_fns.h>
36 #include <rte_rwlock.h>
37 #include <rte_cycles.h>
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
81 uint8_t eth_tp_mdix_ctrl;
82 int8_t link_mode_masks_nwords;
84 uint32_t link_mode_masks[];
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
122 * Get master interface name from private structure.
125 * Pointer to Ethernet device.
127 * Interface name output buffer.
130 * 0 on success, a negative errno value otherwise and rte_errno is set.
133 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
143 MKSTR(path, "%s/device/net", ibdev_path);
151 while ((dent = readdir(dir)) != NULL) {
152 char *name = dent->d_name;
154 unsigned int dev_port;
157 if ((name[0] == '.') &&
158 ((name[1] == '\0') ||
159 ((name[1] == '.') && (name[2] == '\0'))))
162 MKSTR(path, "%s/device/net/%s/%s",
164 (dev_type ? "dev_id" : "dev_port"));
166 file = fopen(path, "rb");
171 * Switch to dev_id when dev_port does not exist as
172 * is the case with Linux kernel versions < 3.15.
183 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
188 * Switch to dev_id when dev_port returns the same value for
189 * all ports. May happen when using a MOFED release older than
190 * 3.0 with a Linux kernel >= 3.15.
192 if (dev_port == dev_port_prev)
194 dev_port_prev = dev_port;
196 strlcpy(match, name, sizeof(match));
199 if (match[0] == '\0') {
203 strncpy(*ifname, match, sizeof(*ifname));
208 * Get interface name from private structure.
210 * This is a port representor-aware version of mlx5_get_master_ifname().
213 * Pointer to Ethernet device.
215 * Interface name output buffer.
218 * 0 on success, a negative errno value otherwise and rte_errno is set.
221 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
223 struct mlx5_priv *priv = dev->data->dev_private;
224 unsigned int ifindex;
228 ifindex = mlx5_ifindex(dev);
230 if (!priv->representor)
231 return mlx5_get_master_ifname(priv->sh->ibdev_path,
236 if (if_indextoname(ifindex, &(*ifname)[0]))
243 * Get the interface index from device name.
246 * Pointer to Ethernet device.
249 * Nonzero interface index on success, zero otherwise and rte_errno is set.
252 mlx5_ifindex(const struct rte_eth_dev *dev)
254 struct mlx5_priv *priv = dev->data->dev_private;
255 unsigned int ifindex;
258 assert(priv->if_index);
259 ifindex = priv->if_index;
266 * Perform ifreq ioctl() on associated Ethernet device.
269 * Pointer to Ethernet device.
271 * Request number to pass to ioctl().
273 * Interface request structure output buffer.
276 * 0 on success, a negative errno value otherwise and rte_errno is set.
279 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
281 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
288 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
291 ret = ioctl(sock, req, ifr);
307 * Pointer to Ethernet device.
309 * MTU value output buffer.
312 * 0 on success, a negative errno value otherwise and rte_errno is set.
315 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
317 struct ifreq request;
318 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
322 *mtu = request.ifr_mtu;
330 * Pointer to Ethernet device.
335 * 0 on success, a negative errno value otherwise and rte_errno is set.
338 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
340 struct ifreq request = { .ifr_mtu = mtu, };
342 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
349 * Pointer to Ethernet device.
351 * Bitmask for flags that must remain untouched.
353 * Bitmask for flags to modify.
356 * 0 on success, a negative errno value otherwise and rte_errno is set.
359 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
361 struct ifreq request;
362 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
366 request.ifr_flags &= keep;
367 request.ifr_flags |= flags & ~keep;
368 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
372 * DPDK callback for Ethernet device configuration.
375 * Pointer to Ethernet device structure.
378 * 0 on success, a negative errno value otherwise and rte_errno is set.
381 mlx5_dev_configure(struct rte_eth_dev *dev)
383 struct mlx5_priv *priv = dev->data->dev_private;
384 unsigned int rxqs_n = dev->data->nb_rx_queues;
385 unsigned int txqs_n = dev->data->nb_tx_queues;
388 unsigned int reta_idx_n;
389 const uint8_t use_app_rss_key =
390 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
393 if (use_app_rss_key &&
394 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
395 MLX5_RSS_HASH_KEY_LEN)) {
396 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
397 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
401 priv->rss_conf.rss_key =
402 rte_realloc(priv->rss_conf.rss_key,
403 MLX5_RSS_HASH_KEY_LEN, 0);
404 if (!priv->rss_conf.rss_key) {
405 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
406 dev->data->port_id, rxqs_n);
410 memcpy(priv->rss_conf.rss_key,
412 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
413 rss_hash_default_key,
414 MLX5_RSS_HASH_KEY_LEN);
415 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
416 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
417 priv->rxqs = (void *)dev->data->rx_queues;
418 priv->txqs = (void *)dev->data->tx_queues;
419 if (txqs_n != priv->txqs_n) {
420 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
421 dev->data->port_id, priv->txqs_n, txqs_n);
422 priv->txqs_n = txqs_n;
424 if (rxqs_n > priv->config.ind_table_max_size) {
425 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
426 dev->data->port_id, rxqs_n);
430 if (rxqs_n != priv->rxqs_n) {
431 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
432 dev->data->port_id, priv->rxqs_n, rxqs_n);
433 priv->rxqs_n = rxqs_n;
435 * If the requested number of RX queues is not a power of two,
436 * use the maximum indirection table size for better balancing.
437 * 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);
446 * When the number of RX queues is not a power of two,
447 * the remaining table entries are padded with reused WQs
448 * and hashes are not spread uniformly.
450 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
451 (*priv->reta_idx)[i] = j;
456 ret = mlx5_proc_priv_init(dev);
463 * Sets default tuning parameters.
466 * Pointer to Ethernet device.
468 * Info structure output buffer.
471 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
473 struct mlx5_priv *priv = dev->data->dev_private;
475 /* Minimum CPU utilization. */
476 info->default_rxportconf.ring_size = 256;
477 info->default_txportconf.ring_size = 256;
478 info->default_rxportconf.burst_size = 64;
479 info->default_txportconf.burst_size = 64;
480 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
481 info->default_rxportconf.nb_queues = 16;
482 info->default_txportconf.nb_queues = 16;
483 if (dev->data->nb_rx_queues > 2 ||
484 dev->data->nb_tx_queues > 2) {
485 /* Max Throughput. */
486 info->default_rxportconf.ring_size = 2048;
487 info->default_txportconf.ring_size = 2048;
490 info->default_rxportconf.nb_queues = 8;
491 info->default_txportconf.nb_queues = 8;
492 if (dev->data->nb_rx_queues > 2 ||
493 dev->data->nb_tx_queues > 2) {
494 /* Max Throughput. */
495 info->default_rxportconf.ring_size = 4096;
496 info->default_txportconf.ring_size = 4096;
502 * Sets tx mbuf limiting parameters.
505 * Pointer to Ethernet device.
507 * Info structure output buffer.
510 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
512 struct mlx5_priv *priv = dev->data->dev_private;
513 struct mlx5_dev_config *config = &priv->config;
517 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
518 MLX5_SEND_DEF_INLINE_LEN :
519 (unsigned int)config->txq_inline_max;
520 assert(config->txq_inline_min >= 0);
521 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
522 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
523 MLX5_ESEG_MIN_INLINE_SIZE -
526 MLX5_WQE_DSEG_SIZE * 2);
527 nb_max = (MLX5_WQE_SIZE_MAX +
528 MLX5_ESEG_MIN_INLINE_SIZE -
532 inlen) / MLX5_WSEG_SIZE;
533 info->tx_desc_lim.nb_seg_max = nb_max;
534 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
538 * DPDK callback to get information about the device.
541 * Pointer to Ethernet device structure.
543 * Info structure output buffer.
546 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
548 struct mlx5_priv *priv = dev->data->dev_private;
549 struct mlx5_dev_config *config = &priv->config;
552 /* FIXME: we should ask the device for these values. */
553 info->min_rx_bufsize = 32;
554 info->max_rx_pktlen = 65536;
556 * Since we need one CQ per QP, the limit is the minimum number
557 * between the two values.
559 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
560 priv->sh->device_attr.orig_attr.max_qp);
561 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
564 info->max_rx_queues = max;
565 info->max_tx_queues = max;
566 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
567 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
568 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
569 info->rx_queue_offload_capa);
570 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
571 info->if_index = mlx5_ifindex(dev);
572 info->reta_size = priv->reta_idx_n ?
573 priv->reta_idx_n : config->ind_table_max_size;
574 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
575 info->speed_capa = priv->link_speed_capa;
576 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
577 mlx5_set_default_params(dev, info);
578 mlx5_set_txlimit_params(dev, info);
579 info->switch_info.name = dev->data->name;
580 info->switch_info.domain_id = priv->domain_id;
581 info->switch_info.port_id = priv->representor_id;
582 if (priv->representor) {
583 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
586 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
588 struct mlx5_priv *opriv =
589 rte_eth_devices[port_id[i]].data->dev_private;
592 opriv->representor ||
593 opriv->domain_id != priv->domain_id)
596 * Override switch name with that of the master
599 info->switch_info.name = opriv->dev_data->name;
606 * Get device current raw clock counter
609 * Pointer to Ethernet device structure.
611 * Current raw clock counter of the device.
614 * 0 if the clock has correctly been read
615 * The value of errno in case of error
618 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
620 struct mlx5_priv *priv = dev->data->dev_private;
621 struct ibv_context *ctx = priv->sh->ctx;
622 struct ibv_values_ex values;
625 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
626 err = mlx5_glue->query_rt_values_ex(ctx, &values);
628 DRV_LOG(WARNING, "Could not query the clock !");
631 *clock = values.raw_clock.tv_nsec;
636 * Get firmware version of a device.
639 * Ethernet device port.
641 * String output allocated by caller.
643 * Size of the output string, including terminating null byte.
646 * 0 on success, or the size of the non truncated string if too big.
648 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
650 struct mlx5_priv *priv = dev->data->dev_private;
651 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
652 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
657 strlcpy(fw_ver, attr->fw_ver, fw_size);
662 * Get supported packet types.
665 * Pointer to Ethernet device structure.
668 * A pointer to the supported Packet types array.
671 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
673 static const uint32_t ptypes[] = {
674 /* refers to rxq_cq_to_pkt_type() */
676 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
677 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
678 RTE_PTYPE_L4_NONFRAG,
682 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
683 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
684 RTE_PTYPE_INNER_L4_NONFRAG,
685 RTE_PTYPE_INNER_L4_FRAG,
686 RTE_PTYPE_INNER_L4_TCP,
687 RTE_PTYPE_INNER_L4_UDP,
691 if (dev->rx_pkt_burst == mlx5_rx_burst ||
692 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
693 dev->rx_pkt_burst == mlx5_rx_burst_vec)
699 * Retrieve the master device for representor in the same switch domain.
702 * Pointer to representor Ethernet device structure.
705 * Master device structure on success, NULL otherwise.
708 static struct rte_eth_dev *
709 mlx5_find_master_dev(struct rte_eth_dev *dev)
711 struct mlx5_priv *priv;
715 priv = dev->data->dev_private;
716 domain_id = priv->domain_id;
717 assert(priv->representor);
718 RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
719 priv = rte_eth_devices[port_id].data->dev_private;
722 priv->domain_id == domain_id)
723 return &rte_eth_devices[port_id];
729 * DPDK callback to retrieve physical link information.
732 * Pointer to Ethernet device structure.
734 * Storage for current link status.
737 * 0 on success, a negative errno value otherwise and rte_errno is set.
740 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
741 struct rte_eth_link *link)
743 struct mlx5_priv *priv = dev->data->dev_private;
744 struct ethtool_cmd edata = {
745 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
748 struct rte_eth_link dev_link;
752 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
754 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
755 dev->data->port_id, strerror(rte_errno));
758 dev_link = (struct rte_eth_link) {
759 .link_status = ((ifr.ifr_flags & IFF_UP) &&
760 (ifr.ifr_flags & IFF_RUNNING)),
762 ifr = (struct ifreq) {
763 .ifr_data = (void *)&edata,
765 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
767 if (ret == -ENOTSUP && priv->representor) {
768 struct rte_eth_dev *master;
771 * For representors we can try to inherit link
772 * settings from the master device. Actually
773 * link settings do not make a lot of sense
774 * for representors due to missing physical
775 * link. The old kernel drivers supported
776 * emulated settings query for representors,
777 * the new ones do not, so we have to add
778 * this code for compatibility issues.
780 master = mlx5_find_master_dev(dev);
782 ifr = (struct ifreq) {
783 .ifr_data = (void *)&edata,
785 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
790 "port %u ioctl(SIOCETHTOOL,"
791 " ETHTOOL_GSET) failed: %s",
792 dev->data->port_id, strerror(rte_errno));
796 link_speed = ethtool_cmd_speed(&edata);
797 if (link_speed == -1)
798 dev_link.link_speed = ETH_SPEED_NUM_NONE;
800 dev_link.link_speed = link_speed;
801 priv->link_speed_capa = 0;
802 if (edata.supported & SUPPORTED_Autoneg)
803 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
804 if (edata.supported & (SUPPORTED_1000baseT_Full |
805 SUPPORTED_1000baseKX_Full))
806 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
807 if (edata.supported & SUPPORTED_10000baseKR_Full)
808 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
809 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
810 SUPPORTED_40000baseCR4_Full |
811 SUPPORTED_40000baseSR4_Full |
812 SUPPORTED_40000baseLR4_Full))
813 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
814 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
815 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
816 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
817 ETH_LINK_SPEED_FIXED);
818 if (((dev_link.link_speed && !dev_link.link_status) ||
819 (!dev_link.link_speed && dev_link.link_status))) {
828 * Retrieve physical link information (unlocked version using new ioctl).
831 * Pointer to Ethernet device structure.
833 * Storage for current link status.
836 * 0 on success, a negative errno value otherwise and rte_errno is set.
839 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
840 struct rte_eth_link *link)
843 struct mlx5_priv *priv = dev->data->dev_private;
844 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
846 struct rte_eth_link dev_link;
847 struct rte_eth_dev *master = NULL;
851 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
853 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
854 dev->data->port_id, strerror(rte_errno));
857 dev_link = (struct rte_eth_link) {
858 .link_status = ((ifr.ifr_flags & IFF_UP) &&
859 (ifr.ifr_flags & IFF_RUNNING)),
861 ifr = (struct ifreq) {
862 .ifr_data = (void *)&gcmd,
864 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
866 if (ret == -ENOTSUP && priv->representor) {
868 * For representors we can try to inherit link
869 * settings from the master device. Actually
870 * link settings do not make a lot of sense
871 * for representors due to missing physical
872 * link. The old kernel drivers supported
873 * emulated settings query for representors,
874 * the new ones do not, so we have to add
875 * this code for compatibility issues.
877 master = mlx5_find_master_dev(dev);
879 ifr = (struct ifreq) {
880 .ifr_data = (void *)&gcmd,
882 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
887 "port %u ioctl(SIOCETHTOOL,"
888 " ETHTOOL_GLINKSETTINGS) failed: %s",
889 dev->data->port_id, strerror(rte_errno));
894 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
896 alignas(struct ethtool_link_settings)
897 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
898 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
899 struct ethtool_link_settings *ecmd = (void *)data;
902 ifr.ifr_data = (void *)ecmd;
903 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
906 "port %u ioctl(SIOCETHTOOL,"
907 "ETHTOOL_GLINKSETTINGS) failed: %s",
908 dev->data->port_id, strerror(rte_errno));
911 dev_link.link_speed = ecmd->speed;
912 sc = ecmd->link_mode_masks[0] |
913 ((uint64_t)ecmd->link_mode_masks[1] << 32);
914 priv->link_speed_capa = 0;
915 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
916 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
917 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
918 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
919 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
920 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
921 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
922 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
923 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
924 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
925 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
926 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
927 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
928 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
929 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
930 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
931 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
932 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
933 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
934 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
935 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
936 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
937 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
938 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
939 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
940 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
941 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
942 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
943 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
944 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
945 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
946 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
947 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
948 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
949 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
950 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
951 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
952 ETH_LINK_SPEED_FIXED);
953 if (((dev_link.link_speed && !dev_link.link_status) ||
954 (!dev_link.link_speed && dev_link.link_status))) {
963 * DPDK callback to retrieve physical link information.
966 * Pointer to Ethernet device structure.
967 * @param wait_to_complete
968 * Wait for request completion.
971 * 0 if link status was not updated, positive if it was, a negative errno
972 * value otherwise and rte_errno is set.
975 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
978 struct rte_eth_link dev_link;
979 time_t start_time = time(NULL);
982 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
984 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
987 /* Handle wait to complete situation. */
988 if (wait_to_complete && ret == -EAGAIN) {
989 if (abs((int)difftime(time(NULL), start_time)) <
990 MLX5_LINK_STATUS_TIMEOUT) {
997 } else if (ret < 0) {
1000 } while (wait_to_complete);
1001 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1002 sizeof(struct rte_eth_link));
1003 dev->data->dev_link = dev_link;
1008 * DPDK callback to change the MTU.
1011 * Pointer to Ethernet device structure.
1016 * 0 on success, a negative errno value otherwise and rte_errno is set.
1019 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1021 struct mlx5_priv *priv = dev->data->dev_private;
1022 uint16_t kern_mtu = 0;
1025 ret = mlx5_get_mtu(dev, &kern_mtu);
1028 /* Set kernel interface MTU first. */
1029 ret = mlx5_set_mtu(dev, mtu);
1032 ret = mlx5_get_mtu(dev, &kern_mtu);
1035 if (kern_mtu == mtu) {
1037 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1038 dev->data->port_id, mtu);
1046 * DPDK callback to get flow control status.
1049 * Pointer to Ethernet device structure.
1050 * @param[out] fc_conf
1051 * Flow control output buffer.
1054 * 0 on success, a negative errno value otherwise and rte_errno is set.
1057 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1060 struct ethtool_pauseparam ethpause = {
1061 .cmd = ETHTOOL_GPAUSEPARAM
1065 ifr.ifr_data = (void *)ðpause;
1066 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1069 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1071 dev->data->port_id, strerror(rte_errno));
1074 fc_conf->autoneg = ethpause.autoneg;
1075 if (ethpause.rx_pause && ethpause.tx_pause)
1076 fc_conf->mode = RTE_FC_FULL;
1077 else if (ethpause.rx_pause)
1078 fc_conf->mode = RTE_FC_RX_PAUSE;
1079 else if (ethpause.tx_pause)
1080 fc_conf->mode = RTE_FC_TX_PAUSE;
1082 fc_conf->mode = RTE_FC_NONE;
1087 * DPDK callback to modify flow control parameters.
1090 * Pointer to Ethernet device structure.
1091 * @param[in] fc_conf
1092 * Flow control parameters.
1095 * 0 on success, a negative errno value otherwise and rte_errno is set.
1098 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1101 struct ethtool_pauseparam ethpause = {
1102 .cmd = ETHTOOL_SPAUSEPARAM
1106 ifr.ifr_data = (void *)ðpause;
1107 ethpause.autoneg = fc_conf->autoneg;
1108 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1109 (fc_conf->mode & RTE_FC_RX_PAUSE))
1110 ethpause.rx_pause = 1;
1112 ethpause.rx_pause = 0;
1114 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1115 (fc_conf->mode & RTE_FC_TX_PAUSE))
1116 ethpause.tx_pause = 1;
1118 ethpause.tx_pause = 0;
1119 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1122 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1124 dev->data->port_id, strerror(rte_errno));
1131 * Get PCI information from struct ibv_device.
1134 * Pointer to Ethernet device structure.
1135 * @param[out] pci_addr
1136 * PCI bus address output buffer.
1139 * 0 on success, a negative errno value otherwise and rte_errno is set.
1142 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1143 struct rte_pci_addr *pci_addr)
1147 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1149 file = fopen(path, "rb");
1154 while (fgets(line, sizeof(line), file) == line) {
1155 size_t len = strlen(line);
1158 /* Truncate long lines. */
1159 if (len == (sizeof(line) - 1))
1160 while (line[(len - 1)] != '\n') {
1164 line[(len - 1)] = ret;
1166 /* Extract information. */
1169 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1173 &pci_addr->function) == 4) {
1183 * Handle asynchronous removal event for entire multiport device.
1186 * Infiniband device shared context.
1189 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1193 for (i = 0; i < sh->max_port; ++i) {
1194 struct rte_eth_dev *dev;
1196 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1198 * Or not existing port either no
1199 * handler installed for this port.
1203 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1205 if (dev->data->dev_conf.intr_conf.rmv)
1206 _rte_eth_dev_callback_process
1207 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1212 * Handle shared asynchronous events the NIC (removal event
1213 * and link status change). Supports multiport IB device.
1216 * Callback argument.
1219 mlx5_dev_interrupt_handler(void *cb_arg)
1221 struct mlx5_ibv_shared *sh = cb_arg;
1222 struct ibv_async_event event;
1224 /* Read all message from the IB device and acknowledge them. */
1226 struct rte_eth_dev *dev;
1229 if (mlx5_glue->get_async_event(sh->ctx, &event))
1231 /* Retrieve and check IB port index. */
1232 tmp = (uint32_t)event.element.port_num;
1233 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1235 * The DEVICE_FATAL event is called once for
1236 * entire device without port specifying.
1237 * We should notify all existing ports.
1239 mlx5_glue->ack_async_event(&event);
1240 mlx5_dev_interrupt_device_fatal(sh);
1243 assert(tmp && (tmp <= sh->max_port));
1245 /* Unsupported devive level event. */
1246 mlx5_glue->ack_async_event(&event);
1248 "unsupported common event (type %d)",
1252 if (tmp > sh->max_port) {
1253 /* Invalid IB port index. */
1254 mlx5_glue->ack_async_event(&event);
1256 "cannot handle an event (type %d)"
1257 "due to invalid IB port index (%u)",
1258 event.event_type, tmp);
1261 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1262 /* No handler installed. */
1263 mlx5_glue->ack_async_event(&event);
1265 "cannot handle an event (type %d)"
1266 "due to no handler installed for port %u",
1267 event.event_type, tmp);
1270 /* Retrieve ethernet device descriptor. */
1271 tmp = sh->port[tmp - 1].ih_port_id;
1272 dev = &rte_eth_devices[tmp];
1274 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1275 event.event_type == IBV_EVENT_PORT_ERR) &&
1276 dev->data->dev_conf.intr_conf.lsc) {
1277 mlx5_glue->ack_async_event(&event);
1278 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1282 _rte_eth_dev_callback_process
1283 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1287 "port %u cannot handle an unknown event (type %d)",
1288 dev->data->port_id, event.event_type);
1289 mlx5_glue->ack_async_event(&event);
1294 * Unregister callback handler safely. The handler may be active
1295 * while we are trying to unregister it, in this case code -EAGAIN
1296 * is returned by rte_intr_callback_unregister(). This routine checks
1297 * the return code and tries to unregister handler again.
1302 * pointer to callback routine
1304 * opaque callback parameter
1307 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1308 rte_intr_callback_fn cb_fn, void *cb_arg)
1311 * Try to reduce timeout management overhead by not calling
1312 * the timer related routines on the first iteration. If the
1313 * unregistering succeeds on first call there will be no
1314 * timer calls at all.
1322 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1325 if (ret != -EAGAIN) {
1326 DRV_LOG(INFO, "failed to unregister interrupt"
1327 " handler (error: %d)", ret);
1332 struct timespec onems;
1334 /* Wait one millisecond and try again. */
1336 onems.tv_nsec = NS_PER_S / MS_PER_S;
1337 nanosleep(&onems, 0);
1338 /* Check whether one second elapsed. */
1339 if ((rte_get_timer_cycles() - start) <= twait)
1343 * We get the amount of timer ticks for one second.
1344 * If this amount elapsed it means we spent one
1345 * second in waiting. This branch is executed once
1346 * on first iteration.
1348 twait = rte_get_timer_hz();
1352 * Timeout elapsed, show message (once a second) and retry.
1353 * We have no other acceptable option here, if we ignore
1354 * the unregistering return code the handler will not
1355 * be unregistered, fd will be closed and we may get the
1356 * crush. Hanging and messaging in the loop seems not to be
1359 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1360 start = rte_get_timer_cycles();
1365 * Handle DEVX interrupts from the NIC.
1366 * This function is probably called from the DPDK host thread.
1369 * Callback argument.
1372 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1374 #ifndef HAVE_IBV_DEVX_ASYNC
1378 struct mlx5_ibv_shared *sh = cb_arg;
1380 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1381 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1382 MLX5_ST_SZ_BYTES(traffic_counter) +
1383 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1385 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1387 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1390 mlx5_flow_async_pool_query_handle
1391 (sh, (uint64_t)out.cmd_resp.wr_id,
1392 mlx5_devx_get_out_command_status(buf));
1393 #endif /* HAVE_IBV_DEVX_ASYNC */
1397 * Uninstall shared asynchronous device events handler.
1398 * This function is implemented to support event sharing
1399 * between multiple ports of single IB device.
1402 * Pointer to Ethernet device.
1405 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1407 struct mlx5_priv *priv = dev->data->dev_private;
1408 struct mlx5_ibv_shared *sh = priv->sh;
1410 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1412 pthread_mutex_lock(&sh->intr_mutex);
1413 assert(priv->ibv_port);
1414 assert(priv->ibv_port <= sh->max_port);
1415 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1416 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1418 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1419 (uint32_t)dev->data->port_id);
1420 assert(sh->intr_cnt);
1421 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1422 if (!sh->intr_cnt || --sh->intr_cnt)
1424 mlx5_intr_callback_unregister(&sh->intr_handle,
1425 mlx5_dev_interrupt_handler, sh);
1426 sh->intr_handle.fd = 0;
1427 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1428 if (sh->intr_handle_devx.fd) {
1429 rte_intr_callback_unregister(&sh->intr_handle_devx,
1430 mlx5_dev_interrupt_handler_devx,
1432 sh->intr_handle_devx.fd = 0;
1433 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1435 if (sh->devx_comp) {
1436 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1437 sh->devx_comp = NULL;
1440 pthread_mutex_unlock(&sh->intr_mutex);
1444 * Install shared asynchronous device events handler.
1445 * This function is implemented to support event sharing
1446 * between multiple ports of single IB device.
1449 * Pointer to Ethernet device.
1452 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1454 struct mlx5_priv *priv = dev->data->dev_private;
1455 struct mlx5_ibv_shared *sh = priv->sh;
1459 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1461 pthread_mutex_lock(&sh->intr_mutex);
1462 assert(priv->ibv_port);
1463 assert(priv->ibv_port <= sh->max_port);
1464 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1465 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1466 /* The handler is already installed for this port. */
1467 assert(sh->intr_cnt);
1470 sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1475 /* No shared handler installed. */
1476 assert(sh->ctx->async_fd > 0);
1477 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1478 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1480 DRV_LOG(INFO, "failed to change file descriptor"
1481 " async event queue");
1484 sh->intr_handle.fd = sh->ctx->async_fd;
1485 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1486 rte_intr_callback_register(&sh->intr_handle,
1487 mlx5_dev_interrupt_handler, sh);
1488 if (priv->config.devx) {
1489 #ifndef HAVE_IBV_DEVX_ASYNC
1490 goto error_unregister;
1492 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1493 if (sh->devx_comp) {
1494 flags = fcntl(sh->devx_comp->fd, F_GETFL);
1495 ret = fcntl(sh->devx_comp->fd, F_SETFL,
1496 flags | O_NONBLOCK);
1498 DRV_LOG(INFO, "failed to change file descriptor"
1499 " devx async event queue");
1500 goto error_unregister;
1502 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1503 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1504 rte_intr_callback_register
1505 (&sh->intr_handle_devx,
1506 mlx5_dev_interrupt_handler_devx, sh);
1508 DRV_LOG(INFO, "failed to create devx async command "
1510 goto error_unregister;
1512 #endif /* HAVE_IBV_DEVX_ASYNC */
1517 rte_intr_callback_unregister(&sh->intr_handle,
1518 mlx5_dev_interrupt_handler, sh);
1520 /* Indicate there will be no interrupts. */
1521 dev->data->dev_conf.intr_conf.lsc = 0;
1522 dev->data->dev_conf.intr_conf.rmv = 0;
1523 sh->intr_handle.fd = 0;
1524 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1525 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1527 pthread_mutex_unlock(&sh->intr_mutex);
1531 * Uninstall interrupt handler.
1534 * Pointer to Ethernet device.
1537 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1539 mlx5_dev_shared_handler_uninstall(dev);
1543 * Install interrupt handler.
1546 * Pointer to Ethernet device.
1549 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1551 mlx5_dev_shared_handler_install(dev);
1555 * DPDK callback to bring the link DOWN.
1558 * Pointer to Ethernet device structure.
1561 * 0 on success, a negative errno value otherwise and rte_errno is set.
1564 mlx5_set_link_down(struct rte_eth_dev *dev)
1566 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1570 * DPDK callback to bring the link UP.
1573 * Pointer to Ethernet device structure.
1576 * 0 on success, a negative errno value otherwise and rte_errno is set.
1579 mlx5_set_link_up(struct rte_eth_dev *dev)
1581 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1585 * Configure the RX function to use.
1588 * Pointer to private data structure.
1591 * Pointer to selected Rx burst function.
1594 mlx5_select_rx_function(struct rte_eth_dev *dev)
1596 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1598 assert(dev != NULL);
1599 if (mlx5_check_vec_rx_support(dev) > 0) {
1600 rx_pkt_burst = mlx5_rx_burst_vec;
1601 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1602 dev->data->port_id);
1603 } else if (mlx5_mprq_enabled(dev)) {
1604 rx_pkt_burst = mlx5_rx_burst_mprq;
1606 return rx_pkt_burst;
1610 * Check if mlx5 device was removed.
1613 * Pointer to Ethernet device structure.
1616 * 1 when device is removed, otherwise 0.
1619 mlx5_is_removed(struct rte_eth_dev *dev)
1621 struct ibv_device_attr device_attr;
1622 struct mlx5_priv *priv = dev->data->dev_private;
1624 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1630 * Get port ID list of mlx5 instances sharing a common device.
1633 * Device to look for.
1634 * @param[out] port_list
1635 * Result buffer for collected port IDs.
1636 * @param port_list_n
1637 * Maximum number of entries in result buffer. If 0, @p port_list can be
1641 * Number of matching instances regardless of the @p port_list_n
1642 * parameter, 0 if none were found.
1645 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1646 unsigned int port_list_n)
1651 RTE_ETH_FOREACH_DEV_OF(id, dev) {
1652 if (n < port_list_n)
1660 * Get the E-Switch domain id this port belongs to.
1664 * @param[out] es_domain_id
1665 * E-Switch domain id.
1666 * @param[out] es_port_id
1667 * The port id of the port in the E-Switch.
1670 * 0 on success, a negative errno value otherwise and rte_errno is set.
1673 mlx5_port_to_eswitch_info(uint16_t port,
1674 uint16_t *es_domain_id, uint16_t *es_port_id)
1676 struct rte_eth_dev *dev;
1677 struct mlx5_priv *priv;
1679 if (port >= RTE_MAX_ETHPORTS) {
1683 if (!rte_eth_dev_is_valid_port(port)) {
1687 dev = &rte_eth_devices[port];
1688 priv = dev->data->dev_private;
1689 if (!(priv->representor || priv->master)) {
1694 *es_domain_id = priv->domain_id;
1696 *es_port_id = priv->vport_id;
1701 * Get switch information associated with network interface.
1704 * Network interface index.
1706 * Switch information object, populated in case of success.
1709 * 0 on success, a negative errno value otherwise and rte_errno is set.
1712 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1714 char ifname[IF_NAMESIZE];
1715 char port_name[IF_NAMESIZE];
1717 struct mlx5_switch_info data = {
1720 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1725 bool port_switch_id_set = false;
1726 bool device_dir = false;
1730 if (!if_indextoname(ifindex, ifname)) {
1735 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1737 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1739 MKSTR(pci_device, "/sys/class/net/%s/device",
1742 file = fopen(phys_port_name, "rb");
1744 ret = fscanf(file, "%s", port_name);
1747 mlx5_translate_port_name(port_name, &data);
1749 file = fopen(phys_switch_id, "rb");
1754 port_switch_id_set =
1755 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1758 dir = opendir(pci_device);
1763 if (port_switch_id_set) {
1764 /* We have some E-Switch configuration. */
1765 mlx5_sysfs_check_switch_info(device_dir, &data);
1768 assert(!(data.master && data.representor));
1769 if (data.master && data.representor) {
1770 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1771 " and as representor", ifindex);
1779 * Analyze gathered port parameters via Netlink to recognize master
1780 * and representor devices for E-Switch configuration.
1782 * @param[in] num_vf_set
1783 * flag of presence of number of VFs port attribute.
1784 * @param[inout] switch_info
1785 * Port information, including port name as a number and port name
1786 * type if recognized
1789 * master and representor flags are set in switch_info according to
1790 * recognized parameters (if any).
1793 mlx5_nl_check_switch_info(bool num_vf_set,
1794 struct mlx5_switch_info *switch_info)
1796 switch (switch_info->name_type) {
1797 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1799 * Name is not recognized, assume the master,
1800 * check the number of VFs key presence.
1802 switch_info->master = num_vf_set;
1804 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1806 * Name is not set, this assumes the legacy naming
1807 * schema for master, just check if there is a
1808 * number of VFs key.
1810 switch_info->master = num_vf_set;
1812 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1813 /* New uplink naming schema recognized. */
1814 switch_info->master = 1;
1816 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1817 /* Legacy representors naming schema. */
1818 switch_info->representor = !num_vf_set;
1820 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1821 /* New representors naming schema. */
1822 switch_info->representor = 1;
1828 * Analyze gathered port parameters via sysfs to recognize master
1829 * and representor devices for E-Switch configuration.
1831 * @param[in] device_dir
1832 * flag of presence of "device" directory under port device key.
1833 * @param[inout] switch_info
1834 * Port information, including port name as a number and port name
1835 * type if recognized
1838 * master and representor flags are set in switch_info according to
1839 * recognized parameters (if any).
1842 mlx5_sysfs_check_switch_info(bool device_dir,
1843 struct mlx5_switch_info *switch_info)
1845 switch (switch_info->name_type) {
1846 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1848 * Name is not recognized, assume the master,
1849 * check the device directory presence.
1851 switch_info->master = device_dir;
1853 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1855 * Name is not set, this assumes the legacy naming
1856 * schema for master, just check if there is
1857 * a device directory.
1859 switch_info->master = device_dir;
1861 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1862 /* New uplink naming schema recognized. */
1863 switch_info->master = 1;
1865 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1866 /* Legacy representors naming schema. */
1867 switch_info->representor = !device_dir;
1869 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1870 /* New representors naming schema. */
1871 switch_info->representor = 1;
1877 * Extract port name, as a number, from sysfs or netlink information.
1879 * @param[in] port_name_in
1880 * String representing the port name.
1881 * @param[out] port_info_out
1882 * Port information, including port name as a number and port name
1883 * type if recognized
1886 * port_name field set according to recognized name format.
1889 mlx5_translate_port_name(const char *port_name_in,
1890 struct mlx5_switch_info *port_info_out)
1892 char pf_c1, pf_c2, vf_c1, vf_c2;
1897 * Check for port-name as a string of the form pf0vf0
1898 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1900 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1901 &pf_c1, &pf_c2, &port_info_out->pf_num,
1902 &vf_c1, &vf_c2, &port_info_out->port_name);
1903 if (sc_items == 6 &&
1904 pf_c1 == 'p' && pf_c2 == 'f' &&
1905 vf_c1 == 'v' && vf_c2 == 'f') {
1906 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1910 * Check for port-name as a string of the form p0
1911 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1913 sc_items = sscanf(port_name_in, "%c%d",
1914 &pf_c1, &port_info_out->port_name);
1915 if (sc_items == 2 && pf_c1 == 'p') {
1916 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1919 /* Check for port-name as a number (support kernel ver < 5.0 */
1921 port_info_out->port_name = strtol(port_name_in, &end, 0);
1923 (size_t)(end - port_name_in) == strlen(port_name_in)) {
1924 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1927 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;