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 char *ibdev_path, char (*ifname)[IF_NAMESIZE])
135 unsigned int dev_type = 0;
136 unsigned int dev_port_prev = ~0u;
137 char match[IF_NAMESIZE] = "";
141 MKSTR(path, "%s/device/net", ibdev_path);
149 while ((dent = readdir(dir)) != NULL) {
150 char *name = dent->d_name;
152 unsigned int dev_port;
155 if ((name[0] == '.') &&
156 ((name[1] == '\0') ||
157 ((name[1] == '.') && (name[2] == '\0'))))
160 MKSTR(path, "%s/device/net/%s/%s",
162 (dev_type ? "dev_id" : "dev_port"));
164 file = fopen(path, "rb");
169 * Switch to dev_id when dev_port does not exist as
170 * is the case with Linux kernel versions < 3.15.
181 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
186 * Switch to dev_id when dev_port returns the same value for
187 * all ports. May happen when using a MOFED release older than
188 * 3.0 with a Linux kernel >= 3.15.
190 if (dev_port == dev_port_prev)
192 dev_port_prev = dev_port;
194 strlcpy(match, name, sizeof(match));
197 if (match[0] == '\0') {
201 strncpy(*ifname, match, sizeof(*ifname));
206 * Get interface name from private structure.
208 * This is a port representor-aware version of mlx5_get_master_ifname().
211 * Pointer to Ethernet device.
213 * Interface name output buffer.
216 * 0 on success, a negative errno value otherwise and rte_errno is set.
219 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
221 struct mlx5_priv *priv = dev->data->dev_private;
222 unsigned int ifindex;
226 ifindex = priv->nl_socket_rdma >= 0 ?
227 mlx5_nl_ifindex(priv->nl_socket_rdma,
228 priv->sh->ibdev_name,
231 if (!priv->representor)
232 return mlx5_get_master_ifname(priv->sh->ibdev_path,
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) {
432 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
433 dev->data->port_id, priv->rxqs_n, rxqs_n);
434 priv->rxqs_n = rxqs_n;
436 * If the requested number of RX queues is not a power of two,
437 * use the maximum indirection table size for better balancing.
438 * The result is always rounded to the next power of two.
440 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
441 priv->config.ind_table_max_size :
443 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
447 * When the number of RX queues is not a power of two,
448 * the remaining table entries are padded with reused WQs
449 * and hashes are not spread uniformly.
451 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
452 (*priv->reta_idx)[i] = j;
457 ret = mlx5_proc_priv_init(dev);
464 * Sets default tuning parameters.
467 * Pointer to Ethernet device.
469 * Info structure output buffer.
472 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
474 struct mlx5_priv *priv = dev->data->dev_private;
476 /* Minimum CPU utilization. */
477 info->default_rxportconf.ring_size = 256;
478 info->default_txportconf.ring_size = 256;
479 info->default_rxportconf.burst_size = 64;
480 info->default_txportconf.burst_size = 64;
481 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
482 info->default_rxportconf.nb_queues = 16;
483 info->default_txportconf.nb_queues = 16;
484 if (dev->data->nb_rx_queues > 2 ||
485 dev->data->nb_tx_queues > 2) {
486 /* Max Throughput. */
487 info->default_rxportconf.ring_size = 2048;
488 info->default_txportconf.ring_size = 2048;
491 info->default_rxportconf.nb_queues = 8;
492 info->default_txportconf.nb_queues = 8;
493 if (dev->data->nb_rx_queues > 2 ||
494 dev->data->nb_tx_queues > 2) {
495 /* Max Throughput. */
496 info->default_rxportconf.ring_size = 4096;
497 info->default_txportconf.ring_size = 4096;
503 * DPDK callback to get information about the device.
506 * Pointer to Ethernet device structure.
508 * Info structure output buffer.
511 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
513 struct mlx5_priv *priv = dev->data->dev_private;
514 struct mlx5_dev_config *config = &priv->config;
516 char ifname[IF_NAMESIZE];
518 /* FIXME: we should ask the device for these values. */
519 info->min_rx_bufsize = 32;
520 info->max_rx_pktlen = 65536;
522 * Since we need one CQ per QP, the limit is the minimum number
523 * between the two values.
525 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
526 priv->sh->device_attr.orig_attr.max_qp);
527 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
530 info->max_rx_queues = max;
531 info->max_tx_queues = max;
532 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
533 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
534 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
535 info->rx_queue_offload_capa);
536 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
537 if (mlx5_get_ifname(dev, &ifname) == 0)
538 info->if_index = if_nametoindex(ifname);
539 info->reta_size = priv->reta_idx_n ?
540 priv->reta_idx_n : config->ind_table_max_size;
541 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
542 info->speed_capa = priv->link_speed_capa;
543 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
544 mlx5_set_default_params(dev, info);
545 info->switch_info.name = dev->data->name;
546 info->switch_info.domain_id = priv->domain_id;
547 info->switch_info.port_id = priv->representor_id;
548 if (priv->representor) {
549 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
552 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
554 struct mlx5_priv *opriv =
555 rte_eth_devices[port_id[i]].data->dev_private;
558 opriv->representor ||
559 opriv->domain_id != priv->domain_id)
562 * Override switch name with that of the master
565 info->switch_info.name = opriv->dev_data->name;
572 * Get firmware version of a device.
575 * Ethernet device port.
577 * String output allocated by caller.
579 * Size of the output string, including terminating null byte.
582 * 0 on success, or the size of the non truncated string if too big.
584 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
586 struct mlx5_priv *priv = dev->data->dev_private;
587 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
588 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
593 strlcpy(fw_ver, attr->fw_ver, fw_size);
598 * Get supported packet types.
601 * Pointer to Ethernet device structure.
604 * A pointer to the supported Packet types array.
607 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
609 static const uint32_t ptypes[] = {
610 /* refers to rxq_cq_to_pkt_type() */
612 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
613 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
614 RTE_PTYPE_L4_NONFRAG,
618 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
619 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
620 RTE_PTYPE_INNER_L4_NONFRAG,
621 RTE_PTYPE_INNER_L4_FRAG,
622 RTE_PTYPE_INNER_L4_TCP,
623 RTE_PTYPE_INNER_L4_UDP,
627 if (dev->rx_pkt_burst == mlx5_rx_burst ||
628 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
629 dev->rx_pkt_burst == mlx5_rx_burst_vec)
635 * Retrieve the master device for representor in the same switch domain.
638 * Pointer to representor Ethernet device structure.
641 * Master device structure on success, NULL otherwise.
644 static struct rte_eth_dev *
645 mlx5_find_master_dev(struct rte_eth_dev *dev)
647 struct mlx5_priv *priv;
651 priv = dev->data->dev_private;
652 domain_id = priv->domain_id;
653 assert(priv->representor);
654 RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
655 priv = rte_eth_devices[port_id].data->dev_private;
658 priv->domain_id == domain_id)
659 return &rte_eth_devices[port_id];
665 * DPDK callback to retrieve physical link information.
668 * Pointer to Ethernet device structure.
670 * Storage for current link status.
673 * 0 on success, a negative errno value otherwise and rte_errno is set.
676 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
677 struct rte_eth_link *link)
679 struct mlx5_priv *priv = dev->data->dev_private;
680 struct ethtool_cmd edata = {
681 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
684 struct rte_eth_link dev_link;
688 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
690 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
691 dev->data->port_id, strerror(rte_errno));
694 dev_link = (struct rte_eth_link) {
695 .link_status = ((ifr.ifr_flags & IFF_UP) &&
696 (ifr.ifr_flags & IFF_RUNNING)),
698 ifr = (struct ifreq) {
699 .ifr_data = (void *)&edata,
701 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
703 if (ret == -ENOTSUP && priv->representor) {
704 struct rte_eth_dev *master;
707 * For representors we can try to inherit link
708 * settings from the master device. Actually
709 * link settings do not make a lot of sense
710 * for representors due to missing physical
711 * link. The old kernel drivers supported
712 * emulated settings query for representors,
713 * the new ones do not, so we have to add
714 * this code for compatibility issues.
716 master = mlx5_find_master_dev(dev);
718 ifr = (struct ifreq) {
719 .ifr_data = (void *)&edata,
721 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
726 "port %u ioctl(SIOCETHTOOL,"
727 " ETHTOOL_GSET) failed: %s",
728 dev->data->port_id, strerror(rte_errno));
732 link_speed = ethtool_cmd_speed(&edata);
733 if (link_speed == -1)
734 dev_link.link_speed = ETH_SPEED_NUM_NONE;
736 dev_link.link_speed = link_speed;
737 priv->link_speed_capa = 0;
738 if (edata.supported & SUPPORTED_Autoneg)
739 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
740 if (edata.supported & (SUPPORTED_1000baseT_Full |
741 SUPPORTED_1000baseKX_Full))
742 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
743 if (edata.supported & SUPPORTED_10000baseKR_Full)
744 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
745 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
746 SUPPORTED_40000baseCR4_Full |
747 SUPPORTED_40000baseSR4_Full |
748 SUPPORTED_40000baseLR4_Full))
749 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
750 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
751 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
752 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
753 ETH_LINK_SPEED_FIXED);
754 if (((dev_link.link_speed && !dev_link.link_status) ||
755 (!dev_link.link_speed && dev_link.link_status))) {
764 * Retrieve physical link information (unlocked version using new ioctl).
767 * Pointer to Ethernet device structure.
769 * Storage for current link status.
772 * 0 on success, a negative errno value otherwise and rte_errno is set.
775 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
776 struct rte_eth_link *link)
779 struct mlx5_priv *priv = dev->data->dev_private;
780 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
782 struct rte_eth_link dev_link;
783 struct rte_eth_dev *master = NULL;
787 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
789 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
790 dev->data->port_id, strerror(rte_errno));
793 dev_link = (struct rte_eth_link) {
794 .link_status = ((ifr.ifr_flags & IFF_UP) &&
795 (ifr.ifr_flags & IFF_RUNNING)),
797 ifr = (struct ifreq) {
798 .ifr_data = (void *)&gcmd,
800 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
802 if (ret == -ENOTSUP && priv->representor) {
804 * For representors we can try to inherit link
805 * settings from the master device. Actually
806 * link settings do not make a lot of sense
807 * for representors due to missing physical
808 * link. The old kernel drivers supported
809 * emulated settings query for representors,
810 * the new ones do not, so we have to add
811 * this code for compatibility issues.
813 master = mlx5_find_master_dev(dev);
815 ifr = (struct ifreq) {
816 .ifr_data = (void *)&gcmd,
818 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
823 "port %u ioctl(SIOCETHTOOL,"
824 " ETHTOOL_GLINKSETTINGS) failed: %s",
825 dev->data->port_id, strerror(rte_errno));
830 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
832 alignas(struct ethtool_link_settings)
833 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
834 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
835 struct ethtool_link_settings *ecmd = (void *)data;
838 ifr.ifr_data = (void *)ecmd;
839 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
842 "port %u ioctl(SIOCETHTOOL,"
843 "ETHTOOL_GLINKSETTINGS) failed: %s",
844 dev->data->port_id, strerror(rte_errno));
847 dev_link.link_speed = ecmd->speed;
848 sc = ecmd->link_mode_masks[0] |
849 ((uint64_t)ecmd->link_mode_masks[1] << 32);
850 priv->link_speed_capa = 0;
851 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
852 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
853 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
854 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
855 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
856 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
857 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
858 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
859 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
860 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
861 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
862 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
863 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
864 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
865 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
866 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
867 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
868 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
869 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
870 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
871 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
872 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
873 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
874 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
875 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
876 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
877 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
878 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
879 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
880 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
881 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
882 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
883 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
884 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
885 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
886 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
887 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
888 ETH_LINK_SPEED_FIXED);
889 if (((dev_link.link_speed && !dev_link.link_status) ||
890 (!dev_link.link_speed && dev_link.link_status))) {
899 * DPDK callback to retrieve physical link information.
902 * Pointer to Ethernet device structure.
903 * @param wait_to_complete
904 * Wait for request completion.
907 * 0 if link status was not updated, positive if it was, a negative errno
908 * value otherwise and rte_errno is set.
911 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
914 struct rte_eth_link dev_link;
915 time_t start_time = time(NULL);
918 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
920 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
923 /* Handle wait to complete situation. */
924 if (wait_to_complete && ret == -EAGAIN) {
925 if (abs((int)difftime(time(NULL), start_time)) <
926 MLX5_LINK_STATUS_TIMEOUT) {
933 } else if (ret < 0) {
936 } while (wait_to_complete);
937 ret = !!memcmp(&dev->data->dev_link, &dev_link,
938 sizeof(struct rte_eth_link));
939 dev->data->dev_link = dev_link;
944 * DPDK callback to change the MTU.
947 * Pointer to Ethernet device structure.
952 * 0 on success, a negative errno value otherwise and rte_errno is set.
955 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
957 struct mlx5_priv *priv = dev->data->dev_private;
958 uint16_t kern_mtu = 0;
961 ret = mlx5_get_mtu(dev, &kern_mtu);
964 /* Set kernel interface MTU first. */
965 ret = mlx5_set_mtu(dev, mtu);
968 ret = mlx5_get_mtu(dev, &kern_mtu);
971 if (kern_mtu == mtu) {
973 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
974 dev->data->port_id, mtu);
982 * DPDK callback to get flow control status.
985 * Pointer to Ethernet device structure.
986 * @param[out] fc_conf
987 * Flow control output buffer.
990 * 0 on success, a negative errno value otherwise and rte_errno is set.
993 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
996 struct ethtool_pauseparam ethpause = {
997 .cmd = ETHTOOL_GPAUSEPARAM
1001 ifr.ifr_data = (void *)ðpause;
1002 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1005 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1007 dev->data->port_id, strerror(rte_errno));
1010 fc_conf->autoneg = ethpause.autoneg;
1011 if (ethpause.rx_pause && ethpause.tx_pause)
1012 fc_conf->mode = RTE_FC_FULL;
1013 else if (ethpause.rx_pause)
1014 fc_conf->mode = RTE_FC_RX_PAUSE;
1015 else if (ethpause.tx_pause)
1016 fc_conf->mode = RTE_FC_TX_PAUSE;
1018 fc_conf->mode = RTE_FC_NONE;
1023 * DPDK callback to modify flow control parameters.
1026 * Pointer to Ethernet device structure.
1027 * @param[in] fc_conf
1028 * Flow control parameters.
1031 * 0 on success, a negative errno value otherwise and rte_errno is set.
1034 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1037 struct ethtool_pauseparam ethpause = {
1038 .cmd = ETHTOOL_SPAUSEPARAM
1042 ifr.ifr_data = (void *)ðpause;
1043 ethpause.autoneg = fc_conf->autoneg;
1044 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1045 (fc_conf->mode & RTE_FC_RX_PAUSE))
1046 ethpause.rx_pause = 1;
1048 ethpause.rx_pause = 0;
1050 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1051 (fc_conf->mode & RTE_FC_TX_PAUSE))
1052 ethpause.tx_pause = 1;
1054 ethpause.tx_pause = 0;
1055 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1058 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1060 dev->data->port_id, strerror(rte_errno));
1067 * Get PCI information from struct ibv_device.
1070 * Pointer to Ethernet device structure.
1071 * @param[out] pci_addr
1072 * PCI bus address output buffer.
1075 * 0 on success, a negative errno value otherwise and rte_errno is set.
1078 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1079 struct rte_pci_addr *pci_addr)
1083 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1085 file = fopen(path, "rb");
1090 while (fgets(line, sizeof(line), file) == line) {
1091 size_t len = strlen(line);
1094 /* Truncate long lines. */
1095 if (len == (sizeof(line) - 1))
1096 while (line[(len - 1)] != '\n') {
1100 line[(len - 1)] = ret;
1102 /* Extract information. */
1105 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1109 &pci_addr->function) == 4) {
1119 * Handle asynchronous removal event for entire multiport device.
1122 * Infiniband device shared context.
1125 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1129 for (i = 0; i < sh->max_port; ++i) {
1130 struct rte_eth_dev *dev;
1132 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1134 * Or not existing port either no
1135 * handler installed for this port.
1139 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1141 if (dev->data->dev_conf.intr_conf.rmv)
1142 _rte_eth_dev_callback_process
1143 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1148 * Handle shared asynchronous events the NIC (removal event
1149 * and link status change). Supports multiport IB device.
1152 * Callback argument.
1155 mlx5_dev_interrupt_handler(void *cb_arg)
1157 struct mlx5_ibv_shared *sh = cb_arg;
1158 struct ibv_async_event event;
1160 /* Read all message from the IB device and acknowledge them. */
1162 struct rte_eth_dev *dev;
1165 if (mlx5_glue->get_async_event(sh->ctx, &event))
1167 /* Retrieve and check IB port index. */
1168 tmp = (uint32_t)event.element.port_num;
1169 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1171 * The DEVICE_FATAL event is called once for
1172 * entire device without port specifying.
1173 * We should notify all existing ports.
1175 mlx5_glue->ack_async_event(&event);
1176 mlx5_dev_interrupt_device_fatal(sh);
1179 assert(tmp && (tmp <= sh->max_port));
1181 /* Unsupported devive level event. */
1182 mlx5_glue->ack_async_event(&event);
1184 "unsupported common event (type %d)",
1188 if (tmp > sh->max_port) {
1189 /* Invalid IB port index. */
1190 mlx5_glue->ack_async_event(&event);
1192 "cannot handle an event (type %d)"
1193 "due to invalid IB port index (%u)",
1194 event.event_type, tmp);
1197 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1198 /* No handler installed. */
1199 mlx5_glue->ack_async_event(&event);
1201 "cannot handle an event (type %d)"
1202 "due to no handler installed for port %u",
1203 event.event_type, tmp);
1206 /* Retrieve ethernet device descriptor. */
1207 tmp = sh->port[tmp - 1].ih_port_id;
1208 dev = &rte_eth_devices[tmp];
1210 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1211 event.event_type == IBV_EVENT_PORT_ERR) &&
1212 dev->data->dev_conf.intr_conf.lsc) {
1213 mlx5_glue->ack_async_event(&event);
1214 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1218 _rte_eth_dev_callback_process
1219 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1223 "port %u cannot handle an unknown event (type %d)",
1224 dev->data->port_id, event.event_type);
1225 mlx5_glue->ack_async_event(&event);
1230 * Uninstall shared asynchronous device events handler.
1231 * This function is implemeted to support event sharing
1232 * between multiple ports of single IB device.
1235 * Pointer to Ethernet device.
1238 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1240 struct mlx5_priv *priv = dev->data->dev_private;
1241 struct mlx5_ibv_shared *sh = priv->sh;
1243 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1245 pthread_mutex_lock(&sh->intr_mutex);
1246 assert(priv->ibv_port);
1247 assert(priv->ibv_port <= sh->max_port);
1248 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1249 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1251 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1252 (uint32_t)dev->data->port_id);
1253 assert(sh->intr_cnt);
1254 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1255 if (!sh->intr_cnt || --sh->intr_cnt)
1257 rte_intr_callback_unregister(&sh->intr_handle,
1258 mlx5_dev_interrupt_handler, sh);
1259 sh->intr_handle.fd = 0;
1260 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1262 pthread_mutex_unlock(&sh->intr_mutex);
1266 * Install shared asyncronous device events handler.
1267 * This function is implemeted to support event sharing
1268 * between multiple ports of single IB device.
1271 * Pointer to Ethernet device.
1274 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1276 struct mlx5_priv *priv = dev->data->dev_private;
1277 struct mlx5_ibv_shared *sh = priv->sh;
1281 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1283 pthread_mutex_lock(&sh->intr_mutex);
1284 assert(priv->ibv_port);
1285 assert(priv->ibv_port <= sh->max_port);
1286 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1287 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1288 /* The handler is already installed for this port. */
1289 assert(sh->intr_cnt);
1292 sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1297 /* No shared handler installed. */
1298 assert(sh->ctx->async_fd > 0);
1299 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1300 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1302 DRV_LOG(INFO, "failed to change file descriptor"
1303 " async event queue");
1304 /* Indicate there will be no interrupts. */
1305 dev->data->dev_conf.intr_conf.lsc = 0;
1306 dev->data->dev_conf.intr_conf.rmv = 0;
1307 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1310 sh->intr_handle.fd = sh->ctx->async_fd;
1311 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1312 rte_intr_callback_register(&sh->intr_handle,
1313 mlx5_dev_interrupt_handler, sh);
1316 pthread_mutex_unlock(&sh->intr_mutex);
1320 * Uninstall interrupt handler.
1323 * Pointer to Ethernet device.
1326 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1328 mlx5_dev_shared_handler_uninstall(dev);
1332 * Install interrupt handler.
1335 * Pointer to Ethernet device.
1338 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1340 mlx5_dev_shared_handler_install(dev);
1344 * DPDK callback to bring the link DOWN.
1347 * Pointer to Ethernet device structure.
1350 * 0 on success, a negative errno value otherwise and rte_errno is set.
1353 mlx5_set_link_down(struct rte_eth_dev *dev)
1355 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1359 * DPDK callback to bring the link UP.
1362 * Pointer to Ethernet device structure.
1365 * 0 on success, a negative errno value otherwise and rte_errno is set.
1368 mlx5_set_link_up(struct rte_eth_dev *dev)
1370 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1374 * Configure the TX function to use.
1377 * Pointer to private data structure.
1380 * Pointer to selected Tx burst function.
1383 mlx5_select_tx_function(struct rte_eth_dev *dev)
1385 struct mlx5_priv *priv = dev->data->dev_private;
1386 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1387 struct mlx5_dev_config *config = &priv->config;
1388 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1389 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1390 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1391 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1392 DEV_TX_OFFLOAD_IP_TNL_TSO |
1393 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1394 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1395 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1396 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1397 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1399 assert(priv != NULL);
1400 /* Select appropriate TX function. */
1401 if (vlan_insert || tso || swp)
1402 return tx_pkt_burst;
1403 if (config->mps == MLX5_MPW_ENHANCED) {
1404 if (mlx5_check_vec_tx_support(dev) > 0) {
1405 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1406 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1408 tx_pkt_burst = mlx5_tx_burst_vec;
1410 "port %u selected enhanced MPW Tx vectorized"
1412 dev->data->port_id);
1414 tx_pkt_burst = mlx5_tx_burst_empw;
1416 "port %u selected enhanced MPW Tx function",
1417 dev->data->port_id);
1419 } else if (config->mps && (config->txq_inline > 0)) {
1420 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1421 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1422 dev->data->port_id);
1423 } else if (config->mps) {
1424 tx_pkt_burst = mlx5_tx_burst_mpw;
1425 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1426 dev->data->port_id);
1428 return tx_pkt_burst;
1432 * Configure the RX function to use.
1435 * Pointer to private data structure.
1438 * Pointer to selected Rx burst function.
1441 mlx5_select_rx_function(struct rte_eth_dev *dev)
1443 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1445 assert(dev != NULL);
1446 if (mlx5_check_vec_rx_support(dev) > 0) {
1447 rx_pkt_burst = mlx5_rx_burst_vec;
1448 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1449 dev->data->port_id);
1450 } else if (mlx5_mprq_enabled(dev)) {
1451 rx_pkt_burst = mlx5_rx_burst_mprq;
1453 return rx_pkt_burst;
1457 * Check if mlx5 device was removed.
1460 * Pointer to Ethernet device structure.
1463 * 1 when device is removed, otherwise 0.
1466 mlx5_is_removed(struct rte_eth_dev *dev)
1468 struct ibv_device_attr device_attr;
1469 struct mlx5_priv *priv = dev->data->dev_private;
1471 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1477 * Get port ID list of mlx5 instances sharing a common device.
1480 * Device to look for.
1481 * @param[out] port_list
1482 * Result buffer for collected port IDs.
1483 * @param port_list_n
1484 * Maximum number of entries in result buffer. If 0, @p port_list can be
1488 * Number of matching instances regardless of the @p port_list_n
1489 * parameter, 0 if none were found.
1492 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1493 unsigned int port_list_n)
1498 RTE_ETH_FOREACH_DEV_OF(id, dev) {
1499 if (n < port_list_n)
1507 * Get the E-Switch domain id this port belongs to.
1511 * @param[out] es_domain_id
1512 * E-Switch domain id.
1513 * @param[out] es_port_id
1514 * The port id of the port in the E-Switch.
1517 * 0 on success, a negative errno value otherwise and rte_errno is set.
1520 mlx5_port_to_eswitch_info(uint16_t port,
1521 uint16_t *es_domain_id, uint16_t *es_port_id)
1523 struct rte_eth_dev *dev;
1524 struct mlx5_priv *priv;
1526 if (port >= RTE_MAX_ETHPORTS) {
1530 if (!rte_eth_dev_is_valid_port(port)) {
1534 dev = &rte_eth_devices[port];
1535 priv = dev->data->dev_private;
1536 if (!(priv->representor || priv->master)) {
1541 *es_domain_id = priv->domain_id;
1543 *es_port_id = priv->vport_id;
1548 * Get switch information associated with network interface.
1551 * Network interface index.
1553 * Switch information object, populated in case of success.
1556 * 0 on success, a negative errno value otherwise and rte_errno is set.
1559 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1561 char ifname[IF_NAMESIZE];
1562 char port_name[IF_NAMESIZE];
1564 struct mlx5_switch_info data = {
1567 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1572 bool port_switch_id_set = false;
1573 bool device_dir = false;
1577 if (!if_indextoname(ifindex, ifname)) {
1582 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1584 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1586 MKSTR(pci_device, "/sys/class/net/%s/device",
1589 file = fopen(phys_port_name, "rb");
1591 ret = fscanf(file, "%s", port_name);
1594 mlx5_translate_port_name(port_name, &data);
1596 file = fopen(phys_switch_id, "rb");
1601 port_switch_id_set =
1602 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1605 dir = opendir(pci_device);
1610 if (port_switch_id_set) {
1611 /* We have some E-Switch configuration. */
1612 mlx5_sysfs_check_switch_info(device_dir, &data);
1615 assert(!(data.master && data.representor));
1616 if (data.master && data.representor) {
1617 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1618 " and as representor", ifindex);
1626 * Analyze gathered port parameters via Netlink to recognize master
1627 * and representor devices for E-Switch configuration.
1629 * @param[in] num_vf_set
1630 * flag of presence of number of VFs port attribute.
1631 * @param[inout] switch_info
1632 * Port information, including port name as a number and port name
1633 * type if recognized
1636 * master and representor flags are set in switch_info according to
1637 * recognized parameters (if any).
1640 mlx5_nl_check_switch_info(bool num_vf_set,
1641 struct mlx5_switch_info *switch_info)
1643 switch (switch_info->name_type) {
1644 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1646 * Name is not recognized, assume the master,
1647 * check the number of VFs key presence.
1649 switch_info->master = num_vf_set;
1651 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1653 * Name is not set, this assumes the legacy naming
1654 * schema for master, just check if there is a
1655 * number of VFs key.
1657 switch_info->master = num_vf_set;
1659 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1660 /* New uplink naming schema recognized. */
1661 switch_info->master = 1;
1663 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1664 /* Legacy representors naming schema. */
1665 switch_info->representor = !num_vf_set;
1667 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1668 /* New representors naming schema. */
1669 switch_info->representor = 1;
1675 * Analyze gathered port parameters via sysfs to recognize master
1676 * and representor devices for E-Switch configuration.
1678 * @param[in] device_dir
1679 * flag of presence of "device" directory under port device key.
1680 * @param[inout] switch_info
1681 * Port information, including port name as a number and port name
1682 * type if recognized
1685 * master and representor flags are set in switch_info according to
1686 * recognized parameters (if any).
1689 mlx5_sysfs_check_switch_info(bool device_dir,
1690 struct mlx5_switch_info *switch_info)
1692 switch (switch_info->name_type) {
1693 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1695 * Name is not recognized, assume the master,
1696 * check the device directory presence.
1698 switch_info->master = device_dir;
1700 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1702 * Name is not set, this assumes the legacy naming
1703 * schema for master, just check if there is
1704 * a device directory.
1706 switch_info->master = device_dir;
1708 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1709 /* New uplink naming schema recognized. */
1710 switch_info->master = 1;
1712 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1713 /* Legacy representors naming schema. */
1714 switch_info->representor = !device_dir;
1716 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1717 /* New representors naming schema. */
1718 switch_info->representor = 1;
1724 * Extract port name, as a number, from sysfs or netlink information.
1726 * @param[in] port_name_in
1727 * String representing the port name.
1728 * @param[out] port_info_out
1729 * Port information, including port name as a number and port name
1730 * type if recognized
1733 * port_name field set according to recognized name format.
1736 mlx5_translate_port_name(const char *port_name_in,
1737 struct mlx5_switch_info *port_info_out)
1739 char pf_c1, pf_c2, vf_c1, vf_c2;
1744 * Check for port-name as a string of the form pf0vf0
1745 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1747 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1748 &pf_c1, &pf_c2, &port_info_out->pf_num,
1749 &vf_c1, &vf_c2, &port_info_out->port_name);
1750 if (sc_items == 6 &&
1751 pf_c1 == 'p' && pf_c2 == 'f' &&
1752 vf_c1 == 'v' && vf_c2 == 'f') {
1753 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1757 * Check for port-name as a string of the form p0
1758 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1760 sc_items = sscanf(port_name_in, "%c%d",
1761 &pf_c1, &port_info_out->port_name);
1762 if (sc_items == 2 && pf_c1 == 'p') {
1763 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1766 /* Check for port-name as a number (support kernel ver < 5.0 */
1768 port_info_out->port_name = strtol(port_name_in, &end, 0);
1770 (size_t)(end - port_name_in) == strlen(port_name_in)) {
1771 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1774 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;