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 = priv->nl_socket_rdma >= 0 ?
229 mlx5_nl_ifindex(priv->nl_socket_rdma,
230 priv->sh->ibdev_name,
233 if (!priv->representor)
234 return mlx5_get_master_ifname(priv->sh->ibdev_path,
239 if (if_indextoname(ifindex, &(*ifname)[0]))
246 * Get interface name for the specified device, uses the extra base
247 * device resources to perform Netlink requests.
249 * This is a port representor-aware version of mlx5_get_master_ifname().
252 * Pointer to Ethernet device to use Netlink socket from
253 * to perfrom requests.
255 * Pointer to Ethernet device.
257 * Interface name output buffer.
260 * 0 on success, a negative errno value otherwise and rte_errno is set.
263 mlx5_get_ifname_base(const struct rte_eth_dev *base,
264 const struct rte_eth_dev *dev,
265 char (*ifname)[IF_NAMESIZE])
267 struct mlx5_priv *priv = dev->data->dev_private;
268 struct mlx5_priv *priv_base = base->data->dev_private;
269 unsigned int ifindex;
274 ifindex = priv_base->nl_socket_rdma >= 0 ?
275 mlx5_nl_ifindex(priv_base->nl_socket_rdma,
276 priv->sh->ibdev_name,
279 if (!priv->representor)
280 return mlx5_get_master_ifname(priv->sh->ibdev_path,
285 if (if_indextoname(ifindex, &(*ifname)[0]))
291 * Get the interface index from device name.
294 * Pointer to Ethernet device.
297 * Nonzero interface index on success, zero otherwise and rte_errno is set.
300 mlx5_ifindex(const struct rte_eth_dev *dev)
302 char ifname[IF_NAMESIZE];
303 unsigned int ifindex;
305 if (mlx5_get_ifname(dev, &ifname))
307 ifindex = if_nametoindex(ifname);
314 * Perform ifreq ioctl() on associated Ethernet device.
317 * Pointer to Ethernet device.
319 * Request number to pass to ioctl().
321 * Interface request structure output buffer.
324 * 0 on success, a negative errno value otherwise and rte_errno is set.
327 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
329 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
336 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
339 ret = ioctl(sock, req, ifr);
352 * Perform ifreq ioctl() on specified Ethernet device,
353 * ifindex, name and other attributes are requested
354 * on the base device to avoid specified device Netlink
355 * socket sharing (this is not thread-safe).
358 * Pointer to Ethernet device to get dev attributes.
360 * Pointer to Ethernet device to perform ioctl.
362 * Request number to pass to ioctl().
364 * Interface request structure output buffer.
367 * 0 on success, a negative errno value otherwise and rte_errno is set.
370 mlx5_ifreq_base(const struct rte_eth_dev *base,
371 const struct rte_eth_dev *dev,
372 int req, struct ifreq *ifr)
374 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
381 ret = mlx5_get_ifname_base(base, dev, &ifr->ifr_name);
384 ret = ioctl(sock, req, ifr);
400 * Pointer to Ethernet device.
402 * MTU value output buffer.
405 * 0 on success, a negative errno value otherwise and rte_errno is set.
408 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
410 struct ifreq request;
411 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
415 *mtu = request.ifr_mtu;
423 * Pointer to Ethernet device.
428 * 0 on success, a negative errno value otherwise and rte_errno is set.
431 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
433 struct ifreq request = { .ifr_mtu = mtu, };
435 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
442 * Pointer to Ethernet device.
444 * Bitmask for flags that must remain untouched.
446 * Bitmask for flags to modify.
449 * 0 on success, a negative errno value otherwise and rte_errno is set.
452 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
454 struct ifreq request;
455 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
459 request.ifr_flags &= keep;
460 request.ifr_flags |= flags & ~keep;
461 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
465 * DPDK callback for Ethernet device configuration.
468 * Pointer to Ethernet device structure.
471 * 0 on success, a negative errno value otherwise and rte_errno is set.
474 mlx5_dev_configure(struct rte_eth_dev *dev)
476 struct mlx5_priv *priv = dev->data->dev_private;
477 unsigned int rxqs_n = dev->data->nb_rx_queues;
478 unsigned int txqs_n = dev->data->nb_tx_queues;
481 unsigned int reta_idx_n;
482 const uint8_t use_app_rss_key =
483 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
486 if (use_app_rss_key &&
487 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
488 MLX5_RSS_HASH_KEY_LEN)) {
489 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
490 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
494 priv->rss_conf.rss_key =
495 rte_realloc(priv->rss_conf.rss_key,
496 MLX5_RSS_HASH_KEY_LEN, 0);
497 if (!priv->rss_conf.rss_key) {
498 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
499 dev->data->port_id, rxqs_n);
503 memcpy(priv->rss_conf.rss_key,
505 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
506 rss_hash_default_key,
507 MLX5_RSS_HASH_KEY_LEN);
508 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
509 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
510 priv->rxqs = (void *)dev->data->rx_queues;
511 priv->txqs = (void *)dev->data->tx_queues;
512 if (txqs_n != priv->txqs_n) {
513 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
514 dev->data->port_id, priv->txqs_n, txqs_n);
515 priv->txqs_n = txqs_n;
517 if (rxqs_n > priv->config.ind_table_max_size) {
518 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
519 dev->data->port_id, rxqs_n);
523 if (rxqs_n != priv->rxqs_n) {
524 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
525 dev->data->port_id, priv->rxqs_n, rxqs_n);
526 priv->rxqs_n = rxqs_n;
528 * If the requested number of RX queues is not a power of two,
529 * use the maximum indirection table size for better balancing.
530 * The result is always rounded to the next power of two.
532 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
533 priv->config.ind_table_max_size :
535 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
539 * When the number of RX queues is not a power of two,
540 * the remaining table entries are padded with reused WQs
541 * and hashes are not spread uniformly.
543 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
544 (*priv->reta_idx)[i] = j;
549 ret = mlx5_proc_priv_init(dev);
556 * Sets default tuning parameters.
559 * Pointer to Ethernet device.
561 * Info structure output buffer.
564 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
566 struct mlx5_priv *priv = dev->data->dev_private;
568 /* Minimum CPU utilization. */
569 info->default_rxportconf.ring_size = 256;
570 info->default_txportconf.ring_size = 256;
571 info->default_rxportconf.burst_size = 64;
572 info->default_txportconf.burst_size = 64;
573 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
574 info->default_rxportconf.nb_queues = 16;
575 info->default_txportconf.nb_queues = 16;
576 if (dev->data->nb_rx_queues > 2 ||
577 dev->data->nb_tx_queues > 2) {
578 /* Max Throughput. */
579 info->default_rxportconf.ring_size = 2048;
580 info->default_txportconf.ring_size = 2048;
583 info->default_rxportconf.nb_queues = 8;
584 info->default_txportconf.nb_queues = 8;
585 if (dev->data->nb_rx_queues > 2 ||
586 dev->data->nb_tx_queues > 2) {
587 /* Max Throughput. */
588 info->default_rxportconf.ring_size = 4096;
589 info->default_txportconf.ring_size = 4096;
595 * Sets tx mbuf limiting parameters.
598 * Pointer to Ethernet device.
600 * Info structure output buffer.
603 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
605 struct mlx5_priv *priv = dev->data->dev_private;
606 struct mlx5_dev_config *config = &priv->config;
610 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
611 MLX5_SEND_DEF_INLINE_LEN :
612 (unsigned int)config->txq_inline_max;
613 assert(config->txq_inline_min >= 0);
614 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
615 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
616 MLX5_ESEG_MIN_INLINE_SIZE -
619 MLX5_WQE_DSEG_SIZE * 2);
620 nb_max = (MLX5_WQE_SIZE_MAX +
621 MLX5_ESEG_MIN_INLINE_SIZE -
625 inlen) / MLX5_WSEG_SIZE;
626 info->tx_desc_lim.nb_seg_max = nb_max;
627 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
631 * DPDK callback to get information about the device.
634 * Pointer to Ethernet device structure.
636 * Info structure output buffer.
639 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
641 struct mlx5_priv *priv = dev->data->dev_private;
642 struct mlx5_dev_config *config = &priv->config;
644 char ifname[IF_NAMESIZE];
646 /* FIXME: we should ask the device for these values. */
647 info->min_rx_bufsize = 32;
648 info->max_rx_pktlen = 65536;
650 * Since we need one CQ per QP, the limit is the minimum number
651 * between the two values.
653 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
654 priv->sh->device_attr.orig_attr.max_qp);
655 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
658 info->max_rx_queues = max;
659 info->max_tx_queues = max;
660 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
661 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
662 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
663 info->rx_queue_offload_capa);
664 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
665 if (mlx5_get_ifname(dev, &ifname) == 0)
666 info->if_index = if_nametoindex(ifname);
667 info->reta_size = priv->reta_idx_n ?
668 priv->reta_idx_n : config->ind_table_max_size;
669 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
670 info->speed_capa = priv->link_speed_capa;
671 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
672 mlx5_set_default_params(dev, info);
673 mlx5_set_txlimit_params(dev, info);
674 info->switch_info.name = dev->data->name;
675 info->switch_info.domain_id = priv->domain_id;
676 info->switch_info.port_id = priv->representor_id;
677 if (priv->representor) {
678 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
681 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
683 struct mlx5_priv *opriv =
684 rte_eth_devices[port_id[i]].data->dev_private;
687 opriv->representor ||
688 opriv->domain_id != priv->domain_id)
691 * Override switch name with that of the master
694 info->switch_info.name = opriv->dev_data->name;
701 * Get device current raw clock counter
704 * Pointer to Ethernet device structure.
706 * Current raw clock counter of the device.
709 * 0 if the clock has correctly been read
710 * The value of errno in case of error
713 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
715 struct mlx5_priv *priv = dev->data->dev_private;
716 struct ibv_context *ctx = priv->sh->ctx;
717 struct ibv_values_ex values;
720 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
721 err = mlx5_glue->query_rt_values_ex(ctx, &values);
723 DRV_LOG(WARNING, "Could not query the clock !");
726 *clock = values.raw_clock.tv_nsec;
731 * Get firmware version of a device.
734 * Ethernet device port.
736 * String output allocated by caller.
738 * Size of the output string, including terminating null byte.
741 * 0 on success, or the size of the non truncated string if too big.
743 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
745 struct mlx5_priv *priv = dev->data->dev_private;
746 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
747 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
752 strlcpy(fw_ver, attr->fw_ver, fw_size);
757 * Get supported packet types.
760 * Pointer to Ethernet device structure.
763 * A pointer to the supported Packet types array.
766 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
768 static const uint32_t ptypes[] = {
769 /* refers to rxq_cq_to_pkt_type() */
771 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
772 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
773 RTE_PTYPE_L4_NONFRAG,
777 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
778 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
779 RTE_PTYPE_INNER_L4_NONFRAG,
780 RTE_PTYPE_INNER_L4_FRAG,
781 RTE_PTYPE_INNER_L4_TCP,
782 RTE_PTYPE_INNER_L4_UDP,
786 if (dev->rx_pkt_burst == mlx5_rx_burst ||
787 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
788 dev->rx_pkt_burst == mlx5_rx_burst_vec)
794 * Retrieve the master device for representor in the same switch domain.
797 * Pointer to representor Ethernet device structure.
800 * Master device structure on success, NULL otherwise.
803 static struct rte_eth_dev *
804 mlx5_find_master_dev(struct rte_eth_dev *dev)
806 struct mlx5_priv *priv;
810 priv = dev->data->dev_private;
811 domain_id = priv->domain_id;
812 assert(priv->representor);
813 RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
814 priv = rte_eth_devices[port_id].data->dev_private;
817 priv->domain_id == domain_id)
818 return &rte_eth_devices[port_id];
824 * DPDK callback to retrieve physical link information.
827 * Pointer to Ethernet device structure.
829 * Storage for current link status.
832 * 0 on success, a negative errno value otherwise and rte_errno is set.
835 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
836 struct rte_eth_link *link)
838 struct mlx5_priv *priv = dev->data->dev_private;
839 struct ethtool_cmd edata = {
840 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
843 struct rte_eth_link dev_link;
847 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
849 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
850 dev->data->port_id, strerror(rte_errno));
853 dev_link = (struct rte_eth_link) {
854 .link_status = ((ifr.ifr_flags & IFF_UP) &&
855 (ifr.ifr_flags & IFF_RUNNING)),
857 ifr = (struct ifreq) {
858 .ifr_data = (void *)&edata,
860 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
862 if (ret == -ENOTSUP && priv->representor) {
863 struct rte_eth_dev *master;
866 * For representors we can try to inherit link
867 * settings from the master device. Actually
868 * link settings do not make a lot of sense
869 * for representors due to missing physical
870 * link. The old kernel drivers supported
871 * emulated settings query for representors,
872 * the new ones do not, so we have to add
873 * this code for compatibility issues.
875 master = mlx5_find_master_dev(dev);
877 ifr = (struct ifreq) {
878 .ifr_data = (void *)&edata,
881 * Use special version of mlx5_ifreq()
882 * to get master device name with local
883 * device Netlink socket. Using master
884 * device Netlink socket is not thread
887 ret = mlx5_ifreq_base(dev, master,
893 "port %u ioctl(SIOCETHTOOL,"
894 " ETHTOOL_GSET) failed: %s",
895 dev->data->port_id, strerror(rte_errno));
899 link_speed = ethtool_cmd_speed(&edata);
900 if (link_speed == -1)
901 dev_link.link_speed = ETH_SPEED_NUM_NONE;
903 dev_link.link_speed = link_speed;
904 priv->link_speed_capa = 0;
905 if (edata.supported & SUPPORTED_Autoneg)
906 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
907 if (edata.supported & (SUPPORTED_1000baseT_Full |
908 SUPPORTED_1000baseKX_Full))
909 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
910 if (edata.supported & SUPPORTED_10000baseKR_Full)
911 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
912 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
913 SUPPORTED_40000baseCR4_Full |
914 SUPPORTED_40000baseSR4_Full |
915 SUPPORTED_40000baseLR4_Full))
916 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
917 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
918 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
919 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
920 ETH_LINK_SPEED_FIXED);
921 if (((dev_link.link_speed && !dev_link.link_status) ||
922 (!dev_link.link_speed && dev_link.link_status))) {
931 * Retrieve physical link information (unlocked version using new ioctl).
934 * Pointer to Ethernet device structure.
936 * Storage for current link status.
939 * 0 on success, a negative errno value otherwise and rte_errno is set.
942 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
943 struct rte_eth_link *link)
946 struct mlx5_priv *priv = dev->data->dev_private;
947 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
949 struct rte_eth_link dev_link;
950 struct rte_eth_dev *master = NULL;
954 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
956 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
957 dev->data->port_id, strerror(rte_errno));
960 dev_link = (struct rte_eth_link) {
961 .link_status = ((ifr.ifr_flags & IFF_UP) &&
962 (ifr.ifr_flags & IFF_RUNNING)),
964 ifr = (struct ifreq) {
965 .ifr_data = (void *)&gcmd,
967 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
969 if (ret == -ENOTSUP && priv->representor) {
971 * For representors we can try to inherit link
972 * settings from the master device. Actually
973 * link settings do not make a lot of sense
974 * for representors due to missing physical
975 * link. The old kernel drivers supported
976 * emulated settings query for representors,
977 * the new ones do not, so we have to add
978 * this code for compatibility issues.
980 master = mlx5_find_master_dev(dev);
982 ifr = (struct ifreq) {
983 .ifr_data = (void *)&gcmd,
986 * Avoid using master Netlink socket.
987 * This is not thread-safe.
989 ret = mlx5_ifreq_base(dev, master,
995 "port %u ioctl(SIOCETHTOOL,"
996 " ETHTOOL_GLINKSETTINGS) failed: %s",
997 dev->data->port_id, strerror(rte_errno));
1002 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
1004 alignas(struct ethtool_link_settings)
1005 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
1006 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
1007 struct ethtool_link_settings *ecmd = (void *)data;
1010 ifr.ifr_data = (void *)ecmd;
1011 ret = mlx5_ifreq_base(dev, master ? master : dev, SIOCETHTOOL, &ifr);
1014 "port %u ioctl(SIOCETHTOOL,"
1015 "ETHTOOL_GLINKSETTINGS) failed: %s",
1016 dev->data->port_id, strerror(rte_errno));
1019 dev_link.link_speed = ecmd->speed;
1020 sc = ecmd->link_mode_masks[0] |
1021 ((uint64_t)ecmd->link_mode_masks[1] << 32);
1022 priv->link_speed_capa = 0;
1023 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
1024 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
1025 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
1026 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
1027 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
1028 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
1029 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
1030 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
1031 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
1032 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
1033 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
1034 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
1035 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
1036 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1037 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1038 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1039 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1040 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1041 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1042 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1043 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1044 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1045 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1046 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1047 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1048 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1049 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1050 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1051 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1052 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1053 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1054 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1055 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1056 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1057 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1058 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1059 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1060 ETH_LINK_SPEED_FIXED);
1061 if (((dev_link.link_speed && !dev_link.link_status) ||
1062 (!dev_link.link_speed && dev_link.link_status))) {
1071 * DPDK callback to retrieve physical link information.
1074 * Pointer to Ethernet device structure.
1075 * @param wait_to_complete
1076 * Wait for request completion.
1079 * 0 if link status was not updated, positive if it was, a negative errno
1080 * value otherwise and rte_errno is set.
1083 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1086 struct rte_eth_link dev_link;
1087 time_t start_time = time(NULL);
1090 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1091 if (ret == -ENOTSUP)
1092 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1095 /* Handle wait to complete situation. */
1096 if (wait_to_complete && ret == -EAGAIN) {
1097 if (abs((int)difftime(time(NULL), start_time)) <
1098 MLX5_LINK_STATUS_TIMEOUT) {
1105 } else if (ret < 0) {
1108 } while (wait_to_complete);
1109 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1110 sizeof(struct rte_eth_link));
1111 dev->data->dev_link = dev_link;
1116 * DPDK callback to change the MTU.
1119 * Pointer to Ethernet device structure.
1124 * 0 on success, a negative errno value otherwise and rte_errno is set.
1127 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1129 struct mlx5_priv *priv = dev->data->dev_private;
1130 uint16_t kern_mtu = 0;
1133 ret = mlx5_get_mtu(dev, &kern_mtu);
1136 /* Set kernel interface MTU first. */
1137 ret = mlx5_set_mtu(dev, mtu);
1140 ret = mlx5_get_mtu(dev, &kern_mtu);
1143 if (kern_mtu == mtu) {
1145 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1146 dev->data->port_id, mtu);
1154 * DPDK callback to get flow control status.
1157 * Pointer to Ethernet device structure.
1158 * @param[out] fc_conf
1159 * Flow control output buffer.
1162 * 0 on success, a negative errno value otherwise and rte_errno is set.
1165 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1168 struct ethtool_pauseparam ethpause = {
1169 .cmd = ETHTOOL_GPAUSEPARAM
1173 ifr.ifr_data = (void *)ðpause;
1174 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1177 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1179 dev->data->port_id, strerror(rte_errno));
1182 fc_conf->autoneg = ethpause.autoneg;
1183 if (ethpause.rx_pause && ethpause.tx_pause)
1184 fc_conf->mode = RTE_FC_FULL;
1185 else if (ethpause.rx_pause)
1186 fc_conf->mode = RTE_FC_RX_PAUSE;
1187 else if (ethpause.tx_pause)
1188 fc_conf->mode = RTE_FC_TX_PAUSE;
1190 fc_conf->mode = RTE_FC_NONE;
1195 * DPDK callback to modify flow control parameters.
1198 * Pointer to Ethernet device structure.
1199 * @param[in] fc_conf
1200 * Flow control parameters.
1203 * 0 on success, a negative errno value otherwise and rte_errno is set.
1206 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1209 struct ethtool_pauseparam ethpause = {
1210 .cmd = ETHTOOL_SPAUSEPARAM
1214 ifr.ifr_data = (void *)ðpause;
1215 ethpause.autoneg = fc_conf->autoneg;
1216 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1217 (fc_conf->mode & RTE_FC_RX_PAUSE))
1218 ethpause.rx_pause = 1;
1220 ethpause.rx_pause = 0;
1222 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1223 (fc_conf->mode & RTE_FC_TX_PAUSE))
1224 ethpause.tx_pause = 1;
1226 ethpause.tx_pause = 0;
1227 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1230 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1232 dev->data->port_id, strerror(rte_errno));
1239 * Get PCI information from struct ibv_device.
1242 * Pointer to Ethernet device structure.
1243 * @param[out] pci_addr
1244 * PCI bus address output buffer.
1247 * 0 on success, a negative errno value otherwise and rte_errno is set.
1250 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1251 struct rte_pci_addr *pci_addr)
1255 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1257 file = fopen(path, "rb");
1262 while (fgets(line, sizeof(line), file) == line) {
1263 size_t len = strlen(line);
1266 /* Truncate long lines. */
1267 if (len == (sizeof(line) - 1))
1268 while (line[(len - 1)] != '\n') {
1272 line[(len - 1)] = ret;
1274 /* Extract information. */
1277 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1281 &pci_addr->function) == 4) {
1291 * Handle asynchronous removal event for entire multiport device.
1294 * Infiniband device shared context.
1297 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1301 for (i = 0; i < sh->max_port; ++i) {
1302 struct rte_eth_dev *dev;
1304 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1306 * Or not existing port either no
1307 * handler installed for this port.
1311 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1313 if (dev->data->dev_conf.intr_conf.rmv)
1314 _rte_eth_dev_callback_process
1315 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1320 * Handle shared asynchronous events the NIC (removal event
1321 * and link status change). Supports multiport IB device.
1324 * Callback argument.
1327 mlx5_dev_interrupt_handler(void *cb_arg)
1329 struct mlx5_ibv_shared *sh = cb_arg;
1330 struct ibv_async_event event;
1332 /* Read all message from the IB device and acknowledge them. */
1334 struct rte_eth_dev *dev;
1337 if (mlx5_glue->get_async_event(sh->ctx, &event))
1339 /* Retrieve and check IB port index. */
1340 tmp = (uint32_t)event.element.port_num;
1341 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1343 * The DEVICE_FATAL event is called once for
1344 * entire device without port specifying.
1345 * We should notify all existing ports.
1347 mlx5_glue->ack_async_event(&event);
1348 mlx5_dev_interrupt_device_fatal(sh);
1351 assert(tmp && (tmp <= sh->max_port));
1353 /* Unsupported devive level event. */
1354 mlx5_glue->ack_async_event(&event);
1356 "unsupported common event (type %d)",
1360 if (tmp > sh->max_port) {
1361 /* Invalid IB port index. */
1362 mlx5_glue->ack_async_event(&event);
1364 "cannot handle an event (type %d)"
1365 "due to invalid IB port index (%u)",
1366 event.event_type, tmp);
1369 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1370 /* No handler installed. */
1371 mlx5_glue->ack_async_event(&event);
1373 "cannot handle an event (type %d)"
1374 "due to no handler installed for port %u",
1375 event.event_type, tmp);
1378 /* Retrieve ethernet device descriptor. */
1379 tmp = sh->port[tmp - 1].ih_port_id;
1380 dev = &rte_eth_devices[tmp];
1382 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1383 event.event_type == IBV_EVENT_PORT_ERR) &&
1384 dev->data->dev_conf.intr_conf.lsc) {
1385 mlx5_glue->ack_async_event(&event);
1386 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1390 _rte_eth_dev_callback_process
1391 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1395 "port %u cannot handle an unknown event (type %d)",
1396 dev->data->port_id, event.event_type);
1397 mlx5_glue->ack_async_event(&event);
1402 * Unregister callback handler safely. The handler may be active
1403 * while we are trying to unregister it, in this case code -EAGAIN
1404 * is returned by rte_intr_callback_unregister(). This routine checks
1405 * the return code and tries to unregister handler again.
1410 * pointer to callback routine
1412 * opaque callback parameter
1415 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1416 rte_intr_callback_fn cb_fn, void *cb_arg)
1419 * Try to reduce timeout management overhead by not calling
1420 * the timer related routines on the first iteration. If the
1421 * unregistering succeeds on first call there will be no
1422 * timer calls at all.
1430 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1433 if (ret != -EAGAIN) {
1434 DRV_LOG(INFO, "failed to unregister interrupt"
1435 " handler (error: %d)", ret);
1440 struct timespec onems;
1442 /* Wait one millisecond and try again. */
1444 onems.tv_nsec = NS_PER_S / MS_PER_S;
1445 nanosleep(&onems, 0);
1446 /* Check whether one second elapsed. */
1447 if ((rte_get_timer_cycles() - start) <= twait)
1451 * We get the amount of timer ticks for one second.
1452 * If this amount elapsed it means we spent one
1453 * second in waiting. This branch is executed once
1454 * on first iteration.
1456 twait = rte_get_timer_hz();
1460 * Timeout elapsed, show message (once a second) and retry.
1461 * We have no other acceptable option here, if we ignore
1462 * the unregistering return code the handler will not
1463 * be unregistered, fd will be closed and we may get the
1464 * crush. Hanging and messaging in the loop seems not to be
1467 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1468 start = rte_get_timer_cycles();
1473 * Handle DEVX interrupts from the NIC.
1474 * This function is probably called from the DPDK host thread.
1477 * Callback argument.
1480 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1482 #ifndef HAVE_IBV_DEVX_ASYNC
1486 struct mlx5_ibv_shared *sh = cb_arg;
1488 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1489 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1490 MLX5_ST_SZ_BYTES(traffic_counter) +
1491 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1493 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1495 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1498 mlx5_flow_async_pool_query_handle
1499 (sh, (uint64_t)out.cmd_resp.wr_id,
1500 mlx5_devx_get_out_command_status(buf));
1501 #endif /* HAVE_IBV_DEVX_ASYNC */
1505 * Uninstall shared asynchronous device events handler.
1506 * This function is implemented to support event sharing
1507 * between multiple ports of single IB device.
1510 * Pointer to Ethernet device.
1513 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1515 struct mlx5_priv *priv = dev->data->dev_private;
1516 struct mlx5_ibv_shared *sh = priv->sh;
1518 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1520 pthread_mutex_lock(&sh->intr_mutex);
1521 assert(priv->ibv_port);
1522 assert(priv->ibv_port <= sh->max_port);
1523 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1524 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1526 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1527 (uint32_t)dev->data->port_id);
1528 assert(sh->intr_cnt);
1529 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1530 if (!sh->intr_cnt || --sh->intr_cnt)
1532 mlx5_intr_callback_unregister(&sh->intr_handle,
1533 mlx5_dev_interrupt_handler, sh);
1534 sh->intr_handle.fd = 0;
1535 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1536 if (sh->intr_handle_devx.fd) {
1537 rte_intr_callback_unregister(&sh->intr_handle_devx,
1538 mlx5_dev_interrupt_handler_devx,
1540 sh->intr_handle_devx.fd = 0;
1541 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1543 if (sh->devx_comp) {
1544 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1545 sh->devx_comp = NULL;
1548 pthread_mutex_unlock(&sh->intr_mutex);
1552 * Install shared asynchronous device events handler.
1553 * This function is implemented to support event sharing
1554 * between multiple ports of single IB device.
1557 * Pointer to Ethernet device.
1560 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1562 struct mlx5_priv *priv = dev->data->dev_private;
1563 struct mlx5_ibv_shared *sh = priv->sh;
1567 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1569 pthread_mutex_lock(&sh->intr_mutex);
1570 assert(priv->ibv_port);
1571 assert(priv->ibv_port <= sh->max_port);
1572 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1573 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1574 /* The handler is already installed for this port. */
1575 assert(sh->intr_cnt);
1578 sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1583 /* No shared handler installed. */
1584 assert(sh->ctx->async_fd > 0);
1585 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1586 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1588 DRV_LOG(INFO, "failed to change file descriptor"
1589 " async event queue");
1592 sh->intr_handle.fd = sh->ctx->async_fd;
1593 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1594 rte_intr_callback_register(&sh->intr_handle,
1595 mlx5_dev_interrupt_handler, sh);
1596 if (priv->config.devx) {
1597 #ifndef HAVE_IBV_DEVX_ASYNC
1598 goto error_unregister;
1600 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1601 if (sh->devx_comp) {
1602 flags = fcntl(sh->devx_comp->fd, F_GETFL);
1603 ret = fcntl(sh->devx_comp->fd, F_SETFL,
1604 flags | O_NONBLOCK);
1606 DRV_LOG(INFO, "failed to change file descriptor"
1607 " devx async event queue");
1608 goto error_unregister;
1610 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1611 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1612 rte_intr_callback_register
1613 (&sh->intr_handle_devx,
1614 mlx5_dev_interrupt_handler_devx, sh);
1616 DRV_LOG(INFO, "failed to create devx async command "
1618 goto error_unregister;
1620 #endif /* HAVE_IBV_DEVX_ASYNC */
1625 rte_intr_callback_unregister(&sh->intr_handle,
1626 mlx5_dev_interrupt_handler, sh);
1628 /* Indicate there will be no interrupts. */
1629 dev->data->dev_conf.intr_conf.lsc = 0;
1630 dev->data->dev_conf.intr_conf.rmv = 0;
1631 sh->intr_handle.fd = 0;
1632 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1633 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1635 pthread_mutex_unlock(&sh->intr_mutex);
1639 * Uninstall interrupt handler.
1642 * Pointer to Ethernet device.
1645 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1647 mlx5_dev_shared_handler_uninstall(dev);
1651 * Install interrupt handler.
1654 * Pointer to Ethernet device.
1657 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1659 mlx5_dev_shared_handler_install(dev);
1663 * DPDK callback to bring the link DOWN.
1666 * Pointer to Ethernet device structure.
1669 * 0 on success, a negative errno value otherwise and rte_errno is set.
1672 mlx5_set_link_down(struct rte_eth_dev *dev)
1674 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1678 * DPDK callback to bring the link UP.
1681 * Pointer to Ethernet device structure.
1684 * 0 on success, a negative errno value otherwise and rte_errno is set.
1687 mlx5_set_link_up(struct rte_eth_dev *dev)
1689 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1693 * Configure the RX function to use.
1696 * Pointer to private data structure.
1699 * Pointer to selected Rx burst function.
1702 mlx5_select_rx_function(struct rte_eth_dev *dev)
1704 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1706 assert(dev != NULL);
1707 if (mlx5_check_vec_rx_support(dev) > 0) {
1708 rx_pkt_burst = mlx5_rx_burst_vec;
1709 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1710 dev->data->port_id);
1711 } else if (mlx5_mprq_enabled(dev)) {
1712 rx_pkt_burst = mlx5_rx_burst_mprq;
1714 return rx_pkt_burst;
1718 * Check if mlx5 device was removed.
1721 * Pointer to Ethernet device structure.
1724 * 1 when device is removed, otherwise 0.
1727 mlx5_is_removed(struct rte_eth_dev *dev)
1729 struct ibv_device_attr device_attr;
1730 struct mlx5_priv *priv = dev->data->dev_private;
1732 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1738 * Get port ID list of mlx5 instances sharing a common device.
1741 * Device to look for.
1742 * @param[out] port_list
1743 * Result buffer for collected port IDs.
1744 * @param port_list_n
1745 * Maximum number of entries in result buffer. If 0, @p port_list can be
1749 * Number of matching instances regardless of the @p port_list_n
1750 * parameter, 0 if none were found.
1753 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1754 unsigned int port_list_n)
1759 RTE_ETH_FOREACH_DEV_OF(id, dev) {
1760 if (n < port_list_n)
1768 * Get the E-Switch domain id this port belongs to.
1772 * @param[out] es_domain_id
1773 * E-Switch domain id.
1774 * @param[out] es_port_id
1775 * The port id of the port in the E-Switch.
1778 * 0 on success, a negative errno value otherwise and rte_errno is set.
1781 mlx5_port_to_eswitch_info(uint16_t port,
1782 uint16_t *es_domain_id, uint16_t *es_port_id)
1784 struct rte_eth_dev *dev;
1785 struct mlx5_priv *priv;
1787 if (port >= RTE_MAX_ETHPORTS) {
1791 if (!rte_eth_dev_is_valid_port(port)) {
1795 dev = &rte_eth_devices[port];
1796 priv = dev->data->dev_private;
1797 if (!(priv->representor || priv->master)) {
1802 *es_domain_id = priv->domain_id;
1804 *es_port_id = priv->vport_id;
1809 * Get switch information associated with network interface.
1812 * Network interface index.
1814 * Switch information object, populated in case of success.
1817 * 0 on success, a negative errno value otherwise and rte_errno is set.
1820 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1822 char ifname[IF_NAMESIZE];
1823 char port_name[IF_NAMESIZE];
1825 struct mlx5_switch_info data = {
1828 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1833 bool port_switch_id_set = false;
1834 bool device_dir = false;
1838 if (!if_indextoname(ifindex, ifname)) {
1843 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1845 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1847 MKSTR(pci_device, "/sys/class/net/%s/device",
1850 file = fopen(phys_port_name, "rb");
1852 ret = fscanf(file, "%s", port_name);
1855 mlx5_translate_port_name(port_name, &data);
1857 file = fopen(phys_switch_id, "rb");
1862 port_switch_id_set =
1863 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1866 dir = opendir(pci_device);
1871 if (port_switch_id_set) {
1872 /* We have some E-Switch configuration. */
1873 mlx5_sysfs_check_switch_info(device_dir, &data);
1876 assert(!(data.master && data.representor));
1877 if (data.master && data.representor) {
1878 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1879 " and as representor", ifindex);
1887 * Analyze gathered port parameters via Netlink to recognize master
1888 * and representor devices for E-Switch configuration.
1890 * @param[in] num_vf_set
1891 * flag of presence of number of VFs port attribute.
1892 * @param[inout] switch_info
1893 * Port information, including port name as a number and port name
1894 * type if recognized
1897 * master and representor flags are set in switch_info according to
1898 * recognized parameters (if any).
1901 mlx5_nl_check_switch_info(bool num_vf_set,
1902 struct mlx5_switch_info *switch_info)
1904 switch (switch_info->name_type) {
1905 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1907 * Name is not recognized, assume the master,
1908 * check the number of VFs key presence.
1910 switch_info->master = num_vf_set;
1912 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1914 * Name is not set, this assumes the legacy naming
1915 * schema for master, just check if there is a
1916 * number of VFs key.
1918 switch_info->master = num_vf_set;
1920 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1921 /* New uplink naming schema recognized. */
1922 switch_info->master = 1;
1924 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1925 /* Legacy representors naming schema. */
1926 switch_info->representor = !num_vf_set;
1928 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1929 /* New representors naming schema. */
1930 switch_info->representor = 1;
1936 * Analyze gathered port parameters via sysfs to recognize master
1937 * and representor devices for E-Switch configuration.
1939 * @param[in] device_dir
1940 * flag of presence of "device" directory under port device key.
1941 * @param[inout] switch_info
1942 * Port information, including port name as a number and port name
1943 * type if recognized
1946 * master and representor flags are set in switch_info according to
1947 * recognized parameters (if any).
1950 mlx5_sysfs_check_switch_info(bool device_dir,
1951 struct mlx5_switch_info *switch_info)
1953 switch (switch_info->name_type) {
1954 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1956 * Name is not recognized, assume the master,
1957 * check the device directory presence.
1959 switch_info->master = device_dir;
1961 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1963 * Name is not set, this assumes the legacy naming
1964 * schema for master, just check if there is
1965 * a device directory.
1967 switch_info->master = device_dir;
1969 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1970 /* New uplink naming schema recognized. */
1971 switch_info->master = 1;
1973 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1974 /* Legacy representors naming schema. */
1975 switch_info->representor = !device_dir;
1977 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1978 /* New representors naming schema. */
1979 switch_info->representor = 1;
1985 * Extract port name, as a number, from sysfs or netlink information.
1987 * @param[in] port_name_in
1988 * String representing the port name.
1989 * @param[out] port_info_out
1990 * Port information, including port name as a number and port name
1991 * type if recognized
1994 * port_name field set according to recognized name format.
1997 mlx5_translate_port_name(const char *port_name_in,
1998 struct mlx5_switch_info *port_info_out)
2000 char pf_c1, pf_c2, vf_c1, vf_c2;
2005 * Check for port-name as a string of the form pf0vf0
2006 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
2008 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
2009 &pf_c1, &pf_c2, &port_info_out->pf_num,
2010 &vf_c1, &vf_c2, &port_info_out->port_name);
2011 if (sc_items == 6 &&
2012 pf_c1 == 'p' && pf_c2 == 'f' &&
2013 vf_c1 == 'v' && vf_c2 == 'f') {
2014 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
2018 * Check for port-name as a string of the form p0
2019 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
2021 sc_items = sscanf(port_name_in, "%c%d",
2022 &pf_c1, &port_info_out->port_name);
2023 if (sc_items == 2 && pf_c1 == 'p') {
2024 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2027 /* Check for port-name as a number (support kernel ver < 5.0 */
2029 port_info_out->port_name = strtol(port_name_in, &end, 0);
2031 (size_t)(end - port_name_in) == strlen(port_name_in)) {
2032 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
2035 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;