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>
36 #include <rte_cycles.h>
39 #include "mlx5_glue.h"
40 #include "mlx5_rxtx.h"
41 #include "mlx5_utils.h"
43 /* Supported speed values found in /usr/include/linux/ethtool.h */
44 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
45 #define SUPPORTED_40000baseKR4_Full (1 << 23)
47 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
48 #define SUPPORTED_40000baseCR4_Full (1 << 24)
50 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
51 #define SUPPORTED_40000baseSR4_Full (1 << 25)
53 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
54 #define SUPPORTED_40000baseLR4_Full (1 << 26)
56 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
57 #define SUPPORTED_56000baseKR4_Full (1 << 27)
59 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
60 #define SUPPORTED_56000baseCR4_Full (1 << 28)
62 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
63 #define SUPPORTED_56000baseSR4_Full (1 << 29)
65 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
66 #define SUPPORTED_56000baseLR4_Full (1 << 30)
69 /* Add defines in case the running kernel is not the same as user headers. */
70 #ifndef ETHTOOL_GLINKSETTINGS
71 struct ethtool_link_settings {
80 uint8_t eth_tp_mdix_ctrl;
81 int8_t link_mode_masks_nwords;
83 uint32_t link_mode_masks[];
86 #define ETHTOOL_GLINKSETTINGS 0x0000004c
87 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
88 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
89 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
90 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
91 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
92 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
93 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
94 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
95 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
96 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
97 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
98 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
99 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
100 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
101 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
102 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
104 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
105 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
106 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
107 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
109 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
110 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
111 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
113 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
114 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
115 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
116 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
117 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
121 * Get master interface name from private structure.
124 * Pointer to Ethernet device.
126 * Interface name output buffer.
129 * 0 on success, a negative errno value otherwise and rte_errno is set.
132 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
136 unsigned int dev_type = 0;
137 unsigned int dev_port_prev = ~0u;
138 char match[IF_NAMESIZE] = "";
142 MKSTR(path, "%s/device/net", ibdev_path);
150 while ((dent = readdir(dir)) != NULL) {
151 char *name = dent->d_name;
153 unsigned int dev_port;
156 if ((name[0] == '.') &&
157 ((name[1] == '\0') ||
158 ((name[1] == '.') && (name[2] == '\0'))))
161 MKSTR(path, "%s/device/net/%s/%s",
163 (dev_type ? "dev_id" : "dev_port"));
165 file = fopen(path, "rb");
170 * Switch to dev_id when dev_port does not exist as
171 * is the case with Linux kernel versions < 3.15.
182 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
187 * Switch to dev_id when dev_port returns the same value for
188 * all ports. May happen when using a MOFED release older than
189 * 3.0 with a Linux kernel >= 3.15.
191 if (dev_port == dev_port_prev)
193 dev_port_prev = dev_port;
195 strlcpy(match, name, sizeof(match));
198 if (match[0] == '\0') {
202 strncpy(*ifname, match, sizeof(*ifname));
207 * Get interface name from private structure.
209 * This is a port representor-aware version of mlx5_get_master_ifname().
212 * Pointer to Ethernet device.
214 * Interface name output buffer.
217 * 0 on success, a negative errno value otherwise and rte_errno is set.
220 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
222 struct mlx5_priv *priv = dev->data->dev_private;
223 unsigned int ifindex;
227 ifindex = priv->nl_socket_rdma >= 0 ?
228 mlx5_nl_ifindex(priv->nl_socket_rdma,
229 priv->sh->ibdev_name,
232 if (!priv->representor)
233 return mlx5_get_master_ifname(priv->sh->ibdev_path,
238 if (if_indextoname(ifindex, &(*ifname)[0]))
245 * Get the interface index from device name.
248 * Pointer to Ethernet device.
251 * Nonzero interface index on success, zero otherwise and rte_errno is set.
254 mlx5_ifindex(const struct rte_eth_dev *dev)
256 char ifname[IF_NAMESIZE];
257 unsigned int ifindex;
259 if (mlx5_get_ifname(dev, &ifname))
261 ifindex = if_nametoindex(ifname);
268 * Perform ifreq ioctl() on associated Ethernet device.
271 * Pointer to Ethernet device.
273 * Request number to pass to ioctl().
275 * Interface request structure output buffer.
278 * 0 on success, a negative errno value otherwise and rte_errno is set.
281 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
283 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
290 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
293 ret = ioctl(sock, req, ifr);
309 * Pointer to Ethernet device.
311 * MTU value output buffer.
314 * 0 on success, a negative errno value otherwise and rte_errno is set.
317 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
319 struct ifreq request;
320 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
324 *mtu = request.ifr_mtu;
332 * Pointer to Ethernet device.
337 * 0 on success, a negative errno value otherwise and rte_errno is set.
340 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
342 struct ifreq request = { .ifr_mtu = mtu, };
344 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
351 * Pointer to Ethernet device.
353 * Bitmask for flags that must remain untouched.
355 * Bitmask for flags to modify.
358 * 0 on success, a negative errno value otherwise and rte_errno is set.
361 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
363 struct ifreq request;
364 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
368 request.ifr_flags &= keep;
369 request.ifr_flags |= flags & ~keep;
370 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
374 * DPDK callback for Ethernet device configuration.
377 * Pointer to Ethernet device structure.
380 * 0 on success, a negative errno value otherwise and rte_errno is set.
383 mlx5_dev_configure(struct rte_eth_dev *dev)
385 struct mlx5_priv *priv = dev->data->dev_private;
386 unsigned int rxqs_n = dev->data->nb_rx_queues;
387 unsigned int txqs_n = dev->data->nb_tx_queues;
390 unsigned int reta_idx_n;
391 const uint8_t use_app_rss_key =
392 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
395 if (use_app_rss_key &&
396 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
397 MLX5_RSS_HASH_KEY_LEN)) {
398 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
399 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
403 priv->rss_conf.rss_key =
404 rte_realloc(priv->rss_conf.rss_key,
405 MLX5_RSS_HASH_KEY_LEN, 0);
406 if (!priv->rss_conf.rss_key) {
407 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
408 dev->data->port_id, rxqs_n);
412 memcpy(priv->rss_conf.rss_key,
414 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
415 rss_hash_default_key,
416 MLX5_RSS_HASH_KEY_LEN);
417 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
418 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
419 priv->rxqs = (void *)dev->data->rx_queues;
420 priv->txqs = (void *)dev->data->tx_queues;
421 if (txqs_n != priv->txqs_n) {
422 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
423 dev->data->port_id, priv->txqs_n, txqs_n);
424 priv->txqs_n = txqs_n;
426 if (rxqs_n > priv->config.ind_table_max_size) {
427 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
428 dev->data->port_id, rxqs_n);
432 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;
437 * If the requested number of RX queues is not a power of two,
438 * use the maximum indirection table size for better balancing.
439 * The result is always rounded to the next power of two.
441 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
442 priv->config.ind_table_max_size :
444 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
448 * When the number of RX queues is not a power of two,
449 * the remaining table entries are padded with reused WQs
450 * and hashes are not spread uniformly.
452 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
453 (*priv->reta_idx)[i] = j;
458 ret = mlx5_proc_priv_init(dev);
465 * Sets default tuning parameters.
468 * Pointer to Ethernet device.
470 * Info structure output buffer.
473 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
475 struct mlx5_priv *priv = dev->data->dev_private;
477 /* Minimum CPU utilization. */
478 info->default_rxportconf.ring_size = 256;
479 info->default_txportconf.ring_size = 256;
480 info->default_rxportconf.burst_size = 64;
481 info->default_txportconf.burst_size = 64;
482 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
483 info->default_rxportconf.nb_queues = 16;
484 info->default_txportconf.nb_queues = 16;
485 if (dev->data->nb_rx_queues > 2 ||
486 dev->data->nb_tx_queues > 2) {
487 /* Max Throughput. */
488 info->default_rxportconf.ring_size = 2048;
489 info->default_txportconf.ring_size = 2048;
492 info->default_rxportconf.nb_queues = 8;
493 info->default_txportconf.nb_queues = 8;
494 if (dev->data->nb_rx_queues > 2 ||
495 dev->data->nb_tx_queues > 2) {
496 /* Max Throughput. */
497 info->default_rxportconf.ring_size = 4096;
498 info->default_txportconf.ring_size = 4096;
504 * DPDK callback to get information about the device.
507 * Pointer to Ethernet device structure.
509 * Info structure output buffer.
512 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
514 struct mlx5_priv *priv = dev->data->dev_private;
515 struct mlx5_dev_config *config = &priv->config;
517 char ifname[IF_NAMESIZE];
519 /* FIXME: we should ask the device for these values. */
520 info->min_rx_bufsize = 32;
521 info->max_rx_pktlen = 65536;
523 * Since we need one CQ per QP, the limit is the minimum number
524 * between the two values.
526 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
527 priv->sh->device_attr.orig_attr.max_qp);
528 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
531 info->max_rx_queues = max;
532 info->max_tx_queues = max;
533 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
534 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
535 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
536 info->rx_queue_offload_capa);
537 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
538 if (mlx5_get_ifname(dev, &ifname) == 0)
539 info->if_index = if_nametoindex(ifname);
540 info->reta_size = priv->reta_idx_n ?
541 priv->reta_idx_n : config->ind_table_max_size;
542 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
543 info->speed_capa = priv->link_speed_capa;
544 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
545 mlx5_set_default_params(dev, info);
546 info->switch_info.name = dev->data->name;
547 info->switch_info.domain_id = priv->domain_id;
548 info->switch_info.port_id = priv->representor_id;
549 if (priv->representor) {
550 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
553 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
555 struct mlx5_priv *opriv =
556 rte_eth_devices[port_id[i]].data->dev_private;
559 opriv->representor ||
560 opriv->domain_id != priv->domain_id)
563 * Override switch name with that of the master
566 info->switch_info.name = opriv->dev_data->name;
573 * Get device current raw clock counter
576 * Pointer to Ethernet device structure.
578 * Current raw clock counter of the device.
581 * 0 if the clock has correctly been read
582 * The value of errno in case of error
585 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
587 struct mlx5_priv *priv = dev->data->dev_private;
588 struct ibv_context *ctx = priv->sh->ctx;
589 struct ibv_values_ex values;
592 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
593 err = mlx5_glue->query_rt_values_ex(ctx, &values);
595 DRV_LOG(WARNING, "Could not query the clock !");
598 *clock = values.raw_clock.tv_nsec;
603 * Get firmware version of a device.
606 * Ethernet device port.
608 * String output allocated by caller.
610 * Size of the output string, including terminating null byte.
613 * 0 on success, or the size of the non truncated string if too big.
615 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
617 struct mlx5_priv *priv = dev->data->dev_private;
618 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
619 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
624 strlcpy(fw_ver, attr->fw_ver, fw_size);
629 * Get supported packet types.
632 * Pointer to Ethernet device structure.
635 * A pointer to the supported Packet types array.
638 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
640 static const uint32_t ptypes[] = {
641 /* refers to rxq_cq_to_pkt_type() */
643 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
644 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
645 RTE_PTYPE_L4_NONFRAG,
649 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
650 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
651 RTE_PTYPE_INNER_L4_NONFRAG,
652 RTE_PTYPE_INNER_L4_FRAG,
653 RTE_PTYPE_INNER_L4_TCP,
654 RTE_PTYPE_INNER_L4_UDP,
658 if (dev->rx_pkt_burst == mlx5_rx_burst ||
659 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
660 dev->rx_pkt_burst == mlx5_rx_burst_vec)
666 * Retrieve the master device for representor in the same switch domain.
669 * Pointer to representor Ethernet device structure.
672 * Master device structure on success, NULL otherwise.
675 static struct rte_eth_dev *
676 mlx5_find_master_dev(struct rte_eth_dev *dev)
678 struct mlx5_priv *priv;
682 priv = dev->data->dev_private;
683 domain_id = priv->domain_id;
684 assert(priv->representor);
685 RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
686 priv = rte_eth_devices[port_id].data->dev_private;
689 priv->domain_id == domain_id)
690 return &rte_eth_devices[port_id];
696 * DPDK callback to retrieve physical link information.
699 * Pointer to Ethernet device structure.
701 * Storage for current link status.
704 * 0 on success, a negative errno value otherwise and rte_errno is set.
707 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
708 struct rte_eth_link *link)
710 struct mlx5_priv *priv = dev->data->dev_private;
711 struct ethtool_cmd edata = {
712 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
715 struct rte_eth_link dev_link;
719 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
721 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
722 dev->data->port_id, strerror(rte_errno));
725 dev_link = (struct rte_eth_link) {
726 .link_status = ((ifr.ifr_flags & IFF_UP) &&
727 (ifr.ifr_flags & IFF_RUNNING)),
729 ifr = (struct ifreq) {
730 .ifr_data = (void *)&edata,
732 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
734 if (ret == -ENOTSUP && priv->representor) {
735 struct rte_eth_dev *master;
738 * For representors we can try to inherit link
739 * settings from the master device. Actually
740 * link settings do not make a lot of sense
741 * for representors due to missing physical
742 * link. The old kernel drivers supported
743 * emulated settings query for representors,
744 * the new ones do not, so we have to add
745 * this code for compatibility issues.
747 master = mlx5_find_master_dev(dev);
749 ifr = (struct ifreq) {
750 .ifr_data = (void *)&edata,
752 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
757 "port %u ioctl(SIOCETHTOOL,"
758 " ETHTOOL_GSET) failed: %s",
759 dev->data->port_id, strerror(rte_errno));
763 link_speed = ethtool_cmd_speed(&edata);
764 if (link_speed == -1)
765 dev_link.link_speed = ETH_SPEED_NUM_NONE;
767 dev_link.link_speed = link_speed;
768 priv->link_speed_capa = 0;
769 if (edata.supported & SUPPORTED_Autoneg)
770 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
771 if (edata.supported & (SUPPORTED_1000baseT_Full |
772 SUPPORTED_1000baseKX_Full))
773 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
774 if (edata.supported & SUPPORTED_10000baseKR_Full)
775 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
776 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
777 SUPPORTED_40000baseCR4_Full |
778 SUPPORTED_40000baseSR4_Full |
779 SUPPORTED_40000baseLR4_Full))
780 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
781 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
782 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
783 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
784 ETH_LINK_SPEED_FIXED);
785 if (((dev_link.link_speed && !dev_link.link_status) ||
786 (!dev_link.link_speed && dev_link.link_status))) {
795 * Retrieve physical link information (unlocked version using new ioctl).
798 * Pointer to Ethernet device structure.
800 * Storage for current link status.
803 * 0 on success, a negative errno value otherwise and rte_errno is set.
806 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
807 struct rte_eth_link *link)
810 struct mlx5_priv *priv = dev->data->dev_private;
811 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
813 struct rte_eth_link dev_link;
814 struct rte_eth_dev *master = NULL;
818 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
820 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
821 dev->data->port_id, strerror(rte_errno));
824 dev_link = (struct rte_eth_link) {
825 .link_status = ((ifr.ifr_flags & IFF_UP) &&
826 (ifr.ifr_flags & IFF_RUNNING)),
828 ifr = (struct ifreq) {
829 .ifr_data = (void *)&gcmd,
831 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
833 if (ret == -ENOTSUP && priv->representor) {
835 * For representors we can try to inherit link
836 * settings from the master device. Actually
837 * link settings do not make a lot of sense
838 * for representors due to missing physical
839 * link. The old kernel drivers supported
840 * emulated settings query for representors,
841 * the new ones do not, so we have to add
842 * this code for compatibility issues.
844 master = mlx5_find_master_dev(dev);
846 ifr = (struct ifreq) {
847 .ifr_data = (void *)&gcmd,
849 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
854 "port %u ioctl(SIOCETHTOOL,"
855 " ETHTOOL_GLINKSETTINGS) failed: %s",
856 dev->data->port_id, strerror(rte_errno));
861 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
863 alignas(struct ethtool_link_settings)
864 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
865 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
866 struct ethtool_link_settings *ecmd = (void *)data;
869 ifr.ifr_data = (void *)ecmd;
870 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
873 "port %u ioctl(SIOCETHTOOL,"
874 "ETHTOOL_GLINKSETTINGS) failed: %s",
875 dev->data->port_id, strerror(rte_errno));
878 dev_link.link_speed = ecmd->speed;
879 sc = ecmd->link_mode_masks[0] |
880 ((uint64_t)ecmd->link_mode_masks[1] << 32);
881 priv->link_speed_capa = 0;
882 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
883 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
884 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
885 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
886 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
887 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
888 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
889 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
890 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
891 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
892 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
893 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
894 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
895 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
896 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
897 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
898 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
899 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
900 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
901 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
902 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
903 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
904 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
905 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
906 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
907 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
908 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
909 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
910 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
911 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
912 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
913 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
914 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
915 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
916 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
917 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
918 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
919 ETH_LINK_SPEED_FIXED);
920 if (((dev_link.link_speed && !dev_link.link_status) ||
921 (!dev_link.link_speed && dev_link.link_status))) {
930 * DPDK callback to retrieve physical link information.
933 * Pointer to Ethernet device structure.
934 * @param wait_to_complete
935 * Wait for request completion.
938 * 0 if link status was not updated, positive if it was, a negative errno
939 * value otherwise and rte_errno is set.
942 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
945 struct rte_eth_link dev_link;
946 time_t start_time = time(NULL);
949 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
951 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
954 /* Handle wait to complete situation. */
955 if (wait_to_complete && ret == -EAGAIN) {
956 if (abs((int)difftime(time(NULL), start_time)) <
957 MLX5_LINK_STATUS_TIMEOUT) {
964 } else if (ret < 0) {
967 } while (wait_to_complete);
968 ret = !!memcmp(&dev->data->dev_link, &dev_link,
969 sizeof(struct rte_eth_link));
970 dev->data->dev_link = dev_link;
975 * DPDK callback to change the MTU.
978 * Pointer to Ethernet device structure.
983 * 0 on success, a negative errno value otherwise and rte_errno is set.
986 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
988 struct mlx5_priv *priv = dev->data->dev_private;
989 uint16_t kern_mtu = 0;
992 ret = mlx5_get_mtu(dev, &kern_mtu);
995 /* Set kernel interface MTU first. */
996 ret = mlx5_set_mtu(dev, mtu);
999 ret = mlx5_get_mtu(dev, &kern_mtu);
1002 if (kern_mtu == mtu) {
1004 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1005 dev->data->port_id, mtu);
1013 * DPDK callback to get flow control status.
1016 * Pointer to Ethernet device structure.
1017 * @param[out] fc_conf
1018 * Flow control output buffer.
1021 * 0 on success, a negative errno value otherwise and rte_errno is set.
1024 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1027 struct ethtool_pauseparam ethpause = {
1028 .cmd = ETHTOOL_GPAUSEPARAM
1032 ifr.ifr_data = (void *)ðpause;
1033 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1036 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1038 dev->data->port_id, strerror(rte_errno));
1041 fc_conf->autoneg = ethpause.autoneg;
1042 if (ethpause.rx_pause && ethpause.tx_pause)
1043 fc_conf->mode = RTE_FC_FULL;
1044 else if (ethpause.rx_pause)
1045 fc_conf->mode = RTE_FC_RX_PAUSE;
1046 else if (ethpause.tx_pause)
1047 fc_conf->mode = RTE_FC_TX_PAUSE;
1049 fc_conf->mode = RTE_FC_NONE;
1054 * DPDK callback to modify flow control parameters.
1057 * Pointer to Ethernet device structure.
1058 * @param[in] fc_conf
1059 * Flow control parameters.
1062 * 0 on success, a negative errno value otherwise and rte_errno is set.
1065 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1068 struct ethtool_pauseparam ethpause = {
1069 .cmd = ETHTOOL_SPAUSEPARAM
1073 ifr.ifr_data = (void *)ðpause;
1074 ethpause.autoneg = fc_conf->autoneg;
1075 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1076 (fc_conf->mode & RTE_FC_RX_PAUSE))
1077 ethpause.rx_pause = 1;
1079 ethpause.rx_pause = 0;
1081 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1082 (fc_conf->mode & RTE_FC_TX_PAUSE))
1083 ethpause.tx_pause = 1;
1085 ethpause.tx_pause = 0;
1086 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1089 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1091 dev->data->port_id, strerror(rte_errno));
1098 * Get PCI information from struct ibv_device.
1101 * Pointer to Ethernet device structure.
1102 * @param[out] pci_addr
1103 * PCI bus address output buffer.
1106 * 0 on success, a negative errno value otherwise and rte_errno is set.
1109 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1110 struct rte_pci_addr *pci_addr)
1114 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1116 file = fopen(path, "rb");
1121 while (fgets(line, sizeof(line), file) == line) {
1122 size_t len = strlen(line);
1125 /* Truncate long lines. */
1126 if (len == (sizeof(line) - 1))
1127 while (line[(len - 1)] != '\n') {
1131 line[(len - 1)] = ret;
1133 /* Extract information. */
1136 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1140 &pci_addr->function) == 4) {
1150 * Handle asynchronous removal event for entire multiport device.
1153 * Infiniband device shared context.
1156 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1160 for (i = 0; i < sh->max_port; ++i) {
1161 struct rte_eth_dev *dev;
1163 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1165 * Or not existing port either no
1166 * handler installed for this port.
1170 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1172 if (dev->data->dev_conf.intr_conf.rmv)
1173 _rte_eth_dev_callback_process
1174 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1179 * Handle shared asynchronous events the NIC (removal event
1180 * and link status change). Supports multiport IB device.
1183 * Callback argument.
1186 mlx5_dev_interrupt_handler(void *cb_arg)
1188 struct mlx5_ibv_shared *sh = cb_arg;
1189 struct ibv_async_event event;
1191 /* Read all message from the IB device and acknowledge them. */
1193 struct rte_eth_dev *dev;
1196 if (mlx5_glue->get_async_event(sh->ctx, &event))
1198 /* Retrieve and check IB port index. */
1199 tmp = (uint32_t)event.element.port_num;
1200 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1202 * The DEVICE_FATAL event is called once for
1203 * entire device without port specifying.
1204 * We should notify all existing ports.
1206 mlx5_glue->ack_async_event(&event);
1207 mlx5_dev_interrupt_device_fatal(sh);
1210 assert(tmp && (tmp <= sh->max_port));
1212 /* Unsupported devive level event. */
1213 mlx5_glue->ack_async_event(&event);
1215 "unsupported common event (type %d)",
1219 if (tmp > sh->max_port) {
1220 /* Invalid IB port index. */
1221 mlx5_glue->ack_async_event(&event);
1223 "cannot handle an event (type %d)"
1224 "due to invalid IB port index (%u)",
1225 event.event_type, tmp);
1228 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1229 /* No handler installed. */
1230 mlx5_glue->ack_async_event(&event);
1232 "cannot handle an event (type %d)"
1233 "due to no handler installed for port %u",
1234 event.event_type, tmp);
1237 /* Retrieve ethernet device descriptor. */
1238 tmp = sh->port[tmp - 1].ih_port_id;
1239 dev = &rte_eth_devices[tmp];
1241 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1242 event.event_type == IBV_EVENT_PORT_ERR) &&
1243 dev->data->dev_conf.intr_conf.lsc) {
1244 mlx5_glue->ack_async_event(&event);
1245 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1249 _rte_eth_dev_callback_process
1250 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1254 "port %u cannot handle an unknown event (type %d)",
1255 dev->data->port_id, event.event_type);
1256 mlx5_glue->ack_async_event(&event);
1261 * Unregister callback handler safely. The handler may be active
1262 * while we are trying to unregister it, in this case code -EAGAIN
1263 * is returned by rte_intr_callback_unregister(). This routine checks
1264 * the return code and tries to unregister handler again.
1269 * pointer to callback routine
1271 * opaque callback parameter
1274 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1275 rte_intr_callback_fn cb_fn, void *cb_arg)
1278 * Try to reduce timeout management overhead by not calling
1279 * the timer related routines on the first iteration. If the
1280 * unregistering succeeds on first call there will be no
1281 * timer calls at all.
1289 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1292 if (ret != -EAGAIN) {
1293 DRV_LOG(INFO, "failed to unregister interrupt"
1294 " handler (error: %d)", ret);
1299 struct timespec onems;
1301 /* Wait one millisecond and try again. */
1303 onems.tv_nsec = NS_PER_S / MS_PER_S;
1304 nanosleep(&onems, 0);
1305 /* Check whether one second elapsed. */
1306 if ((rte_get_timer_cycles() - start) <= twait)
1310 * We get the amount of timer ticks for one second.
1311 * If this amount elapsed it means we spent one
1312 * second in waiting. This branch is executed once
1313 * on first iteration.
1315 twait = rte_get_timer_hz();
1319 * Timeout elapsed, show message (once a second) and retry.
1320 * We have no other acceptable option here, if we ignore
1321 * the unregistering return code the handler will not
1322 * be unregistered, fd will be closed and we may get the
1323 * crush. Hanging and messaging in the loop seems not to be
1326 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1327 start = rte_get_timer_cycles();
1332 * Uninstall shared asynchronous device events handler.
1333 * This function is implemented to support event sharing
1334 * between multiple ports of single IB device.
1337 * Pointer to Ethernet device.
1340 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1342 struct mlx5_priv *priv = dev->data->dev_private;
1343 struct mlx5_ibv_shared *sh = priv->sh;
1345 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1347 pthread_mutex_lock(&sh->intr_mutex);
1348 assert(priv->ibv_port);
1349 assert(priv->ibv_port <= sh->max_port);
1350 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1351 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1353 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1354 (uint32_t)dev->data->port_id);
1355 assert(sh->intr_cnt);
1356 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1357 if (!sh->intr_cnt || --sh->intr_cnt)
1359 mlx5_intr_callback_unregister(&sh->intr_handle,
1360 mlx5_dev_interrupt_handler, sh);
1361 sh->intr_handle.fd = 0;
1362 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1364 pthread_mutex_unlock(&sh->intr_mutex);
1368 * Install shared asynchronous device events handler.
1369 * This function is implemented to support event sharing
1370 * between multiple ports of single IB device.
1373 * Pointer to Ethernet device.
1376 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1378 struct mlx5_priv *priv = dev->data->dev_private;
1379 struct mlx5_ibv_shared *sh = priv->sh;
1383 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1385 pthread_mutex_lock(&sh->intr_mutex);
1386 assert(priv->ibv_port);
1387 assert(priv->ibv_port <= sh->max_port);
1388 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1389 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1390 /* The handler is already installed for this port. */
1391 assert(sh->intr_cnt);
1394 sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1399 /* No shared handler installed. */
1400 assert(sh->ctx->async_fd > 0);
1401 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1402 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1404 DRV_LOG(INFO, "failed to change file descriptor"
1405 " async event queue");
1406 /* Indicate there will be no interrupts. */
1407 dev->data->dev_conf.intr_conf.lsc = 0;
1408 dev->data->dev_conf.intr_conf.rmv = 0;
1409 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1412 sh->intr_handle.fd = sh->ctx->async_fd;
1413 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1414 rte_intr_callback_register(&sh->intr_handle,
1415 mlx5_dev_interrupt_handler, sh);
1418 pthread_mutex_unlock(&sh->intr_mutex);
1422 * Uninstall interrupt handler.
1425 * Pointer to Ethernet device.
1428 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1430 mlx5_dev_shared_handler_uninstall(dev);
1434 * Install interrupt handler.
1437 * Pointer to Ethernet device.
1440 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1442 mlx5_dev_shared_handler_install(dev);
1446 * DPDK callback to bring the link DOWN.
1449 * Pointer to Ethernet device structure.
1452 * 0 on success, a negative errno value otherwise and rte_errno is set.
1455 mlx5_set_link_down(struct rte_eth_dev *dev)
1457 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1461 * DPDK callback to bring the link UP.
1464 * Pointer to Ethernet device structure.
1467 * 0 on success, a negative errno value otherwise and rte_errno is set.
1470 mlx5_set_link_up(struct rte_eth_dev *dev)
1472 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1476 * Configure the TX function to use.
1479 * Pointer to private data structure.
1482 * Pointer to selected Tx burst function.
1485 mlx5_select_tx_function(struct rte_eth_dev *dev)
1487 struct mlx5_priv *priv = dev->data->dev_private;
1488 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1489 struct mlx5_dev_config *config = &priv->config;
1490 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1491 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1492 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1493 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1494 DEV_TX_OFFLOAD_IP_TNL_TSO |
1495 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1496 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1497 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1498 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1499 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1501 assert(priv != NULL);
1502 /* Select appropriate TX function. */
1503 if (vlan_insert || tso || swp)
1504 return tx_pkt_burst;
1505 if (config->mps == MLX5_MPW_ENHANCED) {
1506 if (mlx5_check_vec_tx_support(dev) > 0) {
1507 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1508 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1510 tx_pkt_burst = mlx5_tx_burst_vec;
1512 "port %u selected enhanced MPW Tx vectorized"
1514 dev->data->port_id);
1516 tx_pkt_burst = mlx5_tx_burst_empw;
1518 "port %u selected enhanced MPW Tx function",
1519 dev->data->port_id);
1521 } else if (config->mps && (config->txq_inline > 0)) {
1522 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1523 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1524 dev->data->port_id);
1525 } else if (config->mps) {
1526 tx_pkt_burst = mlx5_tx_burst_mpw;
1527 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1528 dev->data->port_id);
1530 return tx_pkt_burst;
1534 * Configure the RX function to use.
1537 * Pointer to private data structure.
1540 * Pointer to selected Rx burst function.
1543 mlx5_select_rx_function(struct rte_eth_dev *dev)
1545 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1547 assert(dev != NULL);
1548 if (mlx5_check_vec_rx_support(dev) > 0) {
1549 rx_pkt_burst = mlx5_rx_burst_vec;
1550 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1551 dev->data->port_id);
1552 } else if (mlx5_mprq_enabled(dev)) {
1553 rx_pkt_burst = mlx5_rx_burst_mprq;
1555 return rx_pkt_burst;
1559 * Check if mlx5 device was removed.
1562 * Pointer to Ethernet device structure.
1565 * 1 when device is removed, otherwise 0.
1568 mlx5_is_removed(struct rte_eth_dev *dev)
1570 struct ibv_device_attr device_attr;
1571 struct mlx5_priv *priv = dev->data->dev_private;
1573 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1579 * Get port ID list of mlx5 instances sharing a common device.
1582 * Device to look for.
1583 * @param[out] port_list
1584 * Result buffer for collected port IDs.
1585 * @param port_list_n
1586 * Maximum number of entries in result buffer. If 0, @p port_list can be
1590 * Number of matching instances regardless of the @p port_list_n
1591 * parameter, 0 if none were found.
1594 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1595 unsigned int port_list_n)
1600 RTE_ETH_FOREACH_DEV_OF(id, dev) {
1601 if (n < port_list_n)
1609 * Get the E-Switch domain id this port belongs to.
1613 * @param[out] es_domain_id
1614 * E-Switch domain id.
1615 * @param[out] es_port_id
1616 * The port id of the port in the E-Switch.
1619 * 0 on success, a negative errno value otherwise and rte_errno is set.
1622 mlx5_port_to_eswitch_info(uint16_t port,
1623 uint16_t *es_domain_id, uint16_t *es_port_id)
1625 struct rte_eth_dev *dev;
1626 struct mlx5_priv *priv;
1628 if (port >= RTE_MAX_ETHPORTS) {
1632 if (!rte_eth_dev_is_valid_port(port)) {
1636 dev = &rte_eth_devices[port];
1637 priv = dev->data->dev_private;
1638 if (!(priv->representor || priv->master)) {
1643 *es_domain_id = priv->domain_id;
1645 *es_port_id = priv->vport_id;
1650 * Get switch information associated with network interface.
1653 * Network interface index.
1655 * Switch information object, populated in case of success.
1658 * 0 on success, a negative errno value otherwise and rte_errno is set.
1661 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1663 char ifname[IF_NAMESIZE];
1664 char port_name[IF_NAMESIZE];
1666 struct mlx5_switch_info data = {
1669 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1674 bool port_switch_id_set = false;
1675 bool device_dir = false;
1679 if (!if_indextoname(ifindex, ifname)) {
1684 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1686 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1688 MKSTR(pci_device, "/sys/class/net/%s/device",
1691 file = fopen(phys_port_name, "rb");
1693 ret = fscanf(file, "%s", port_name);
1696 mlx5_translate_port_name(port_name, &data);
1698 file = fopen(phys_switch_id, "rb");
1703 port_switch_id_set =
1704 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1707 dir = opendir(pci_device);
1712 if (port_switch_id_set) {
1713 /* We have some E-Switch configuration. */
1714 mlx5_sysfs_check_switch_info(device_dir, &data);
1717 assert(!(data.master && data.representor));
1718 if (data.master && data.representor) {
1719 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1720 " and as representor", ifindex);
1728 * Analyze gathered port parameters via Netlink to recognize master
1729 * and representor devices for E-Switch configuration.
1731 * @param[in] num_vf_set
1732 * flag of presence of number of VFs port attribute.
1733 * @param[inout] switch_info
1734 * Port information, including port name as a number and port name
1735 * type if recognized
1738 * master and representor flags are set in switch_info according to
1739 * recognized parameters (if any).
1742 mlx5_nl_check_switch_info(bool num_vf_set,
1743 struct mlx5_switch_info *switch_info)
1745 switch (switch_info->name_type) {
1746 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1748 * Name is not recognized, assume the master,
1749 * check the number of VFs key presence.
1751 switch_info->master = num_vf_set;
1753 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1755 * Name is not set, this assumes the legacy naming
1756 * schema for master, just check if there is a
1757 * number of VFs key.
1759 switch_info->master = num_vf_set;
1761 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1762 /* New uplink naming schema recognized. */
1763 switch_info->master = 1;
1765 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1766 /* Legacy representors naming schema. */
1767 switch_info->representor = !num_vf_set;
1769 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1770 /* New representors naming schema. */
1771 switch_info->representor = 1;
1777 * Analyze gathered port parameters via sysfs to recognize master
1778 * and representor devices for E-Switch configuration.
1780 * @param[in] device_dir
1781 * flag of presence of "device" directory under port device key.
1782 * @param[inout] switch_info
1783 * Port information, including port name as a number and port name
1784 * type if recognized
1787 * master and representor flags are set in switch_info according to
1788 * recognized parameters (if any).
1791 mlx5_sysfs_check_switch_info(bool device_dir,
1792 struct mlx5_switch_info *switch_info)
1794 switch (switch_info->name_type) {
1795 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1797 * Name is not recognized, assume the master,
1798 * check the device directory presence.
1800 switch_info->master = device_dir;
1802 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1804 * Name is not set, this assumes the legacy naming
1805 * schema for master, just check if there is
1806 * a device directory.
1808 switch_info->master = device_dir;
1810 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1811 /* New uplink naming schema recognized. */
1812 switch_info->master = 1;
1814 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1815 /* Legacy representors naming schema. */
1816 switch_info->representor = !device_dir;
1818 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1819 /* New representors naming schema. */
1820 switch_info->representor = 1;
1826 * Extract port name, as a number, from sysfs or netlink information.
1828 * @param[in] port_name_in
1829 * String representing the port name.
1830 * @param[out] port_info_out
1831 * Port information, including port name as a number and port name
1832 * type if recognized
1835 * port_name field set according to recognized name format.
1838 mlx5_translate_port_name(const char *port_name_in,
1839 struct mlx5_switch_info *port_info_out)
1841 char pf_c1, pf_c2, vf_c1, vf_c2;
1846 * Check for port-name as a string of the form pf0vf0
1847 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1849 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1850 &pf_c1, &pf_c2, &port_info_out->pf_num,
1851 &vf_c1, &vf_c2, &port_info_out->port_name);
1852 if (sc_items == 6 &&
1853 pf_c1 == 'p' && pf_c2 == 'f' &&
1854 vf_c1 == 'v' && vf_c2 == 'f') {
1855 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1859 * Check for port-name as a string of the form p0
1860 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1862 sc_items = sscanf(port_name_in, "%c%d",
1863 &pf_c1, &port_info_out->port_name);
1864 if (sc_items == 2 && pf_c1 == 'p') {
1865 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1868 /* Check for port-name as a number (support kernel ver < 5.0 */
1870 port_info_out->port_name = strtol(port_name_in, &end, 0);
1872 (size_t)(end - port_name_in) == strlen(port_name_in)) {
1873 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1876 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;