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 * DPDK callback to get information about the device.
598 * Pointer to Ethernet device structure.
600 * Info structure output buffer.
603 mlx5_dev_infos_get(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;
608 char ifname[IF_NAMESIZE];
610 /* FIXME: we should ask the device for these values. */
611 info->min_rx_bufsize = 32;
612 info->max_rx_pktlen = 65536;
614 * Since we need one CQ per QP, the limit is the minimum number
615 * between the two values.
617 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
618 priv->sh->device_attr.orig_attr.max_qp);
619 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
622 info->max_rx_queues = max;
623 info->max_tx_queues = max;
624 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
625 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
626 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
627 info->rx_queue_offload_capa);
628 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
629 if (mlx5_get_ifname(dev, &ifname) == 0)
630 info->if_index = if_nametoindex(ifname);
631 info->reta_size = priv->reta_idx_n ?
632 priv->reta_idx_n : config->ind_table_max_size;
633 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
634 info->speed_capa = priv->link_speed_capa;
635 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
636 mlx5_set_default_params(dev, info);
637 info->switch_info.name = dev->data->name;
638 info->switch_info.domain_id = priv->domain_id;
639 info->switch_info.port_id = priv->representor_id;
640 if (priv->representor) {
641 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
644 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
646 struct mlx5_priv *opriv =
647 rte_eth_devices[port_id[i]].data->dev_private;
650 opriv->representor ||
651 opriv->domain_id != priv->domain_id)
654 * Override switch name with that of the master
657 info->switch_info.name = opriv->dev_data->name;
664 * Get device current raw clock counter
667 * Pointer to Ethernet device structure.
669 * Current raw clock counter of the device.
672 * 0 if the clock has correctly been read
673 * The value of errno in case of error
676 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
678 struct mlx5_priv *priv = dev->data->dev_private;
679 struct ibv_context *ctx = priv->sh->ctx;
680 struct ibv_values_ex values;
683 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
684 err = mlx5_glue->query_rt_values_ex(ctx, &values);
686 DRV_LOG(WARNING, "Could not query the clock !");
689 *clock = values.raw_clock.tv_nsec;
694 * Get firmware version of a device.
697 * Ethernet device port.
699 * String output allocated by caller.
701 * Size of the output string, including terminating null byte.
704 * 0 on success, or the size of the non truncated string if too big.
706 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
708 struct mlx5_priv *priv = dev->data->dev_private;
709 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
710 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
715 strlcpy(fw_ver, attr->fw_ver, fw_size);
720 * Get supported packet types.
723 * Pointer to Ethernet device structure.
726 * A pointer to the supported Packet types array.
729 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
731 static const uint32_t ptypes[] = {
732 /* refers to rxq_cq_to_pkt_type() */
734 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
735 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
736 RTE_PTYPE_L4_NONFRAG,
740 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
741 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
742 RTE_PTYPE_INNER_L4_NONFRAG,
743 RTE_PTYPE_INNER_L4_FRAG,
744 RTE_PTYPE_INNER_L4_TCP,
745 RTE_PTYPE_INNER_L4_UDP,
749 if (dev->rx_pkt_burst == mlx5_rx_burst ||
750 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
751 dev->rx_pkt_burst == mlx5_rx_burst_vec)
757 * Retrieve the master device for representor in the same switch domain.
760 * Pointer to representor Ethernet device structure.
763 * Master device structure on success, NULL otherwise.
766 static struct rte_eth_dev *
767 mlx5_find_master_dev(struct rte_eth_dev *dev)
769 struct mlx5_priv *priv;
773 priv = dev->data->dev_private;
774 domain_id = priv->domain_id;
775 assert(priv->representor);
776 RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
777 priv = rte_eth_devices[port_id].data->dev_private;
780 priv->domain_id == domain_id)
781 return &rte_eth_devices[port_id];
787 * DPDK callback to retrieve physical link information.
790 * Pointer to Ethernet device structure.
792 * Storage for current link status.
795 * 0 on success, a negative errno value otherwise and rte_errno is set.
798 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
799 struct rte_eth_link *link)
801 struct mlx5_priv *priv = dev->data->dev_private;
802 struct ethtool_cmd edata = {
803 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
806 struct rte_eth_link dev_link;
810 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
812 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
813 dev->data->port_id, strerror(rte_errno));
816 dev_link = (struct rte_eth_link) {
817 .link_status = ((ifr.ifr_flags & IFF_UP) &&
818 (ifr.ifr_flags & IFF_RUNNING)),
820 ifr = (struct ifreq) {
821 .ifr_data = (void *)&edata,
823 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
825 if (ret == -ENOTSUP && priv->representor) {
826 struct rte_eth_dev *master;
829 * For representors we can try to inherit link
830 * settings from the master device. Actually
831 * link settings do not make a lot of sense
832 * for representors due to missing physical
833 * link. The old kernel drivers supported
834 * emulated settings query for representors,
835 * the new ones do not, so we have to add
836 * this code for compatibility issues.
838 master = mlx5_find_master_dev(dev);
840 ifr = (struct ifreq) {
841 .ifr_data = (void *)&edata,
844 * Use special version of mlx5_ifreq()
845 * to get master device name with local
846 * device Netlink socket. Using master
847 * device Netlink socket is not thread
850 ret = mlx5_ifreq_base(dev, master,
856 "port %u ioctl(SIOCETHTOOL,"
857 " ETHTOOL_GSET) failed: %s",
858 dev->data->port_id, strerror(rte_errno));
862 link_speed = ethtool_cmd_speed(&edata);
863 if (link_speed == -1)
864 dev_link.link_speed = ETH_SPEED_NUM_NONE;
866 dev_link.link_speed = link_speed;
867 priv->link_speed_capa = 0;
868 if (edata.supported & SUPPORTED_Autoneg)
869 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
870 if (edata.supported & (SUPPORTED_1000baseT_Full |
871 SUPPORTED_1000baseKX_Full))
872 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
873 if (edata.supported & SUPPORTED_10000baseKR_Full)
874 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
875 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
876 SUPPORTED_40000baseCR4_Full |
877 SUPPORTED_40000baseSR4_Full |
878 SUPPORTED_40000baseLR4_Full))
879 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
880 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
881 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
882 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
883 ETH_LINK_SPEED_FIXED);
884 if (((dev_link.link_speed && !dev_link.link_status) ||
885 (!dev_link.link_speed && dev_link.link_status))) {
894 * Retrieve physical link information (unlocked version using new ioctl).
897 * Pointer to Ethernet device structure.
899 * Storage for current link status.
902 * 0 on success, a negative errno value otherwise and rte_errno is set.
905 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
906 struct rte_eth_link *link)
909 struct mlx5_priv *priv = dev->data->dev_private;
910 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
912 struct rte_eth_link dev_link;
913 struct rte_eth_dev *master = NULL;
917 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
919 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
920 dev->data->port_id, strerror(rte_errno));
923 dev_link = (struct rte_eth_link) {
924 .link_status = ((ifr.ifr_flags & IFF_UP) &&
925 (ifr.ifr_flags & IFF_RUNNING)),
927 ifr = (struct ifreq) {
928 .ifr_data = (void *)&gcmd,
930 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
932 if (ret == -ENOTSUP && priv->representor) {
934 * For representors we can try to inherit link
935 * settings from the master device. Actually
936 * link settings do not make a lot of sense
937 * for representors due to missing physical
938 * link. The old kernel drivers supported
939 * emulated settings query for representors,
940 * the new ones do not, so we have to add
941 * this code for compatibility issues.
943 master = mlx5_find_master_dev(dev);
945 ifr = (struct ifreq) {
946 .ifr_data = (void *)&gcmd,
949 * Avoid using master Netlink socket.
950 * This is not thread-safe.
952 ret = mlx5_ifreq_base(dev, master,
958 "port %u ioctl(SIOCETHTOOL,"
959 " ETHTOOL_GLINKSETTINGS) failed: %s",
960 dev->data->port_id, strerror(rte_errno));
965 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
967 alignas(struct ethtool_link_settings)
968 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
969 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
970 struct ethtool_link_settings *ecmd = (void *)data;
973 ifr.ifr_data = (void *)ecmd;
974 ret = mlx5_ifreq_base(dev, master ? master : dev, SIOCETHTOOL, &ifr);
977 "port %u ioctl(SIOCETHTOOL,"
978 "ETHTOOL_GLINKSETTINGS) failed: %s",
979 dev->data->port_id, strerror(rte_errno));
982 dev_link.link_speed = ecmd->speed;
983 sc = ecmd->link_mode_masks[0] |
984 ((uint64_t)ecmd->link_mode_masks[1] << 32);
985 priv->link_speed_capa = 0;
986 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
987 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
988 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
989 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
990 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
991 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
992 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
993 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
994 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
995 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
996 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
997 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
998 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
999 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1000 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1001 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1002 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1003 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1004 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1005 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1006 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1007 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1008 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1009 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1010 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1011 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1012 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1013 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1014 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1015 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1016 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1017 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1018 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1019 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1020 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1021 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1022 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1023 ETH_LINK_SPEED_FIXED);
1024 if (((dev_link.link_speed && !dev_link.link_status) ||
1025 (!dev_link.link_speed && dev_link.link_status))) {
1034 * DPDK callback to retrieve physical link information.
1037 * Pointer to Ethernet device structure.
1038 * @param wait_to_complete
1039 * Wait for request completion.
1042 * 0 if link status was not updated, positive if it was, a negative errno
1043 * value otherwise and rte_errno is set.
1046 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1049 struct rte_eth_link dev_link;
1050 time_t start_time = time(NULL);
1053 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1054 if (ret == -ENOTSUP)
1055 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1058 /* Handle wait to complete situation. */
1059 if (wait_to_complete && ret == -EAGAIN) {
1060 if (abs((int)difftime(time(NULL), start_time)) <
1061 MLX5_LINK_STATUS_TIMEOUT) {
1068 } else if (ret < 0) {
1071 } while (wait_to_complete);
1072 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1073 sizeof(struct rte_eth_link));
1074 dev->data->dev_link = dev_link;
1079 * DPDK callback to change the MTU.
1082 * Pointer to Ethernet device structure.
1087 * 0 on success, a negative errno value otherwise and rte_errno is set.
1090 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1092 struct mlx5_priv *priv = dev->data->dev_private;
1093 uint16_t kern_mtu = 0;
1096 ret = mlx5_get_mtu(dev, &kern_mtu);
1099 /* Set kernel interface MTU first. */
1100 ret = mlx5_set_mtu(dev, mtu);
1103 ret = mlx5_get_mtu(dev, &kern_mtu);
1106 if (kern_mtu == mtu) {
1108 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1109 dev->data->port_id, mtu);
1117 * DPDK callback to get flow control status.
1120 * Pointer to Ethernet device structure.
1121 * @param[out] fc_conf
1122 * Flow control output buffer.
1125 * 0 on success, a negative errno value otherwise and rte_errno is set.
1128 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1131 struct ethtool_pauseparam ethpause = {
1132 .cmd = ETHTOOL_GPAUSEPARAM
1136 ifr.ifr_data = (void *)ðpause;
1137 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1140 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1142 dev->data->port_id, strerror(rte_errno));
1145 fc_conf->autoneg = ethpause.autoneg;
1146 if (ethpause.rx_pause && ethpause.tx_pause)
1147 fc_conf->mode = RTE_FC_FULL;
1148 else if (ethpause.rx_pause)
1149 fc_conf->mode = RTE_FC_RX_PAUSE;
1150 else if (ethpause.tx_pause)
1151 fc_conf->mode = RTE_FC_TX_PAUSE;
1153 fc_conf->mode = RTE_FC_NONE;
1158 * DPDK callback to modify flow control parameters.
1161 * Pointer to Ethernet device structure.
1162 * @param[in] fc_conf
1163 * Flow control parameters.
1166 * 0 on success, a negative errno value otherwise and rte_errno is set.
1169 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1172 struct ethtool_pauseparam ethpause = {
1173 .cmd = ETHTOOL_SPAUSEPARAM
1177 ifr.ifr_data = (void *)ðpause;
1178 ethpause.autoneg = fc_conf->autoneg;
1179 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1180 (fc_conf->mode & RTE_FC_RX_PAUSE))
1181 ethpause.rx_pause = 1;
1183 ethpause.rx_pause = 0;
1185 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1186 (fc_conf->mode & RTE_FC_TX_PAUSE))
1187 ethpause.tx_pause = 1;
1189 ethpause.tx_pause = 0;
1190 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1193 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1195 dev->data->port_id, strerror(rte_errno));
1202 * Get PCI information from struct ibv_device.
1205 * Pointer to Ethernet device structure.
1206 * @param[out] pci_addr
1207 * PCI bus address output buffer.
1210 * 0 on success, a negative errno value otherwise and rte_errno is set.
1213 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1214 struct rte_pci_addr *pci_addr)
1218 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1220 file = fopen(path, "rb");
1225 while (fgets(line, sizeof(line), file) == line) {
1226 size_t len = strlen(line);
1229 /* Truncate long lines. */
1230 if (len == (sizeof(line) - 1))
1231 while (line[(len - 1)] != '\n') {
1235 line[(len - 1)] = ret;
1237 /* Extract information. */
1240 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1244 &pci_addr->function) == 4) {
1254 * Handle asynchronous removal event for entire multiport device.
1257 * Infiniband device shared context.
1260 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1264 for (i = 0; i < sh->max_port; ++i) {
1265 struct rte_eth_dev *dev;
1267 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1269 * Or not existing port either no
1270 * handler installed for this port.
1274 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1276 if (dev->data->dev_conf.intr_conf.rmv)
1277 _rte_eth_dev_callback_process
1278 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1283 * Handle shared asynchronous events the NIC (removal event
1284 * and link status change). Supports multiport IB device.
1287 * Callback argument.
1290 mlx5_dev_interrupt_handler(void *cb_arg)
1292 struct mlx5_ibv_shared *sh = cb_arg;
1293 struct ibv_async_event event;
1295 /* Read all message from the IB device and acknowledge them. */
1297 struct rte_eth_dev *dev;
1300 if (mlx5_glue->get_async_event(sh->ctx, &event))
1302 /* Retrieve and check IB port index. */
1303 tmp = (uint32_t)event.element.port_num;
1304 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1306 * The DEVICE_FATAL event is called once for
1307 * entire device without port specifying.
1308 * We should notify all existing ports.
1310 mlx5_glue->ack_async_event(&event);
1311 mlx5_dev_interrupt_device_fatal(sh);
1314 assert(tmp && (tmp <= sh->max_port));
1316 /* Unsupported devive level event. */
1317 mlx5_glue->ack_async_event(&event);
1319 "unsupported common event (type %d)",
1323 if (tmp > sh->max_port) {
1324 /* Invalid IB port index. */
1325 mlx5_glue->ack_async_event(&event);
1327 "cannot handle an event (type %d)"
1328 "due to invalid IB port index (%u)",
1329 event.event_type, tmp);
1332 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1333 /* No handler installed. */
1334 mlx5_glue->ack_async_event(&event);
1336 "cannot handle an event (type %d)"
1337 "due to no handler installed for port %u",
1338 event.event_type, tmp);
1341 /* Retrieve ethernet device descriptor. */
1342 tmp = sh->port[tmp - 1].ih_port_id;
1343 dev = &rte_eth_devices[tmp];
1345 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1346 event.event_type == IBV_EVENT_PORT_ERR) &&
1347 dev->data->dev_conf.intr_conf.lsc) {
1348 mlx5_glue->ack_async_event(&event);
1349 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1353 _rte_eth_dev_callback_process
1354 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1358 "port %u cannot handle an unknown event (type %d)",
1359 dev->data->port_id, event.event_type);
1360 mlx5_glue->ack_async_event(&event);
1365 * Unregister callback handler safely. The handler may be active
1366 * while we are trying to unregister it, in this case code -EAGAIN
1367 * is returned by rte_intr_callback_unregister(). This routine checks
1368 * the return code and tries to unregister handler again.
1373 * pointer to callback routine
1375 * opaque callback parameter
1378 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1379 rte_intr_callback_fn cb_fn, void *cb_arg)
1382 * Try to reduce timeout management overhead by not calling
1383 * the timer related routines on the first iteration. If the
1384 * unregistering succeeds on first call there will be no
1385 * timer calls at all.
1393 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1396 if (ret != -EAGAIN) {
1397 DRV_LOG(INFO, "failed to unregister interrupt"
1398 " handler (error: %d)", ret);
1403 struct timespec onems;
1405 /* Wait one millisecond and try again. */
1407 onems.tv_nsec = NS_PER_S / MS_PER_S;
1408 nanosleep(&onems, 0);
1409 /* Check whether one second elapsed. */
1410 if ((rte_get_timer_cycles() - start) <= twait)
1414 * We get the amount of timer ticks for one second.
1415 * If this amount elapsed it means we spent one
1416 * second in waiting. This branch is executed once
1417 * on first iteration.
1419 twait = rte_get_timer_hz();
1423 * Timeout elapsed, show message (once a second) and retry.
1424 * We have no other acceptable option here, if we ignore
1425 * the unregistering return code the handler will not
1426 * be unregistered, fd will be closed and we may get the
1427 * crush. Hanging and messaging in the loop seems not to be
1430 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1431 start = rte_get_timer_cycles();
1436 * Uninstall shared asynchronous device events handler.
1437 * This function is implemented to support event sharing
1438 * between multiple ports of single IB device.
1441 * Pointer to Ethernet device.
1444 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1446 struct mlx5_priv *priv = dev->data->dev_private;
1447 struct mlx5_ibv_shared *sh = priv->sh;
1449 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1451 pthread_mutex_lock(&sh->intr_mutex);
1452 assert(priv->ibv_port);
1453 assert(priv->ibv_port <= sh->max_port);
1454 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1455 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1457 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1458 (uint32_t)dev->data->port_id);
1459 assert(sh->intr_cnt);
1460 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1461 if (!sh->intr_cnt || --sh->intr_cnt)
1463 mlx5_intr_callback_unregister(&sh->intr_handle,
1464 mlx5_dev_interrupt_handler, sh);
1465 sh->intr_handle.fd = 0;
1466 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1468 pthread_mutex_unlock(&sh->intr_mutex);
1472 * Install shared asynchronous device events handler.
1473 * This function is implemented to support event sharing
1474 * between multiple ports of single IB device.
1477 * Pointer to Ethernet device.
1480 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1482 struct mlx5_priv *priv = dev->data->dev_private;
1483 struct mlx5_ibv_shared *sh = priv->sh;
1487 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1489 pthread_mutex_lock(&sh->intr_mutex);
1490 assert(priv->ibv_port);
1491 assert(priv->ibv_port <= sh->max_port);
1492 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1493 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1494 /* The handler is already installed for this port. */
1495 assert(sh->intr_cnt);
1498 sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1503 /* No shared handler installed. */
1504 assert(sh->ctx->async_fd > 0);
1505 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1506 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1508 DRV_LOG(INFO, "failed to change file descriptor"
1509 " async event queue");
1510 /* Indicate there will be no interrupts. */
1511 dev->data->dev_conf.intr_conf.lsc = 0;
1512 dev->data->dev_conf.intr_conf.rmv = 0;
1513 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1516 sh->intr_handle.fd = sh->ctx->async_fd;
1517 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1518 rte_intr_callback_register(&sh->intr_handle,
1519 mlx5_dev_interrupt_handler, sh);
1522 pthread_mutex_unlock(&sh->intr_mutex);
1526 * Uninstall interrupt handler.
1529 * Pointer to Ethernet device.
1532 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1534 mlx5_dev_shared_handler_uninstall(dev);
1538 * Install interrupt handler.
1541 * Pointer to Ethernet device.
1544 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1546 mlx5_dev_shared_handler_install(dev);
1550 * DPDK callback to bring the link DOWN.
1553 * Pointer to Ethernet device structure.
1556 * 0 on success, a negative errno value otherwise and rte_errno is set.
1559 mlx5_set_link_down(struct rte_eth_dev *dev)
1561 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1565 * DPDK callback to bring the link UP.
1568 * Pointer to Ethernet device structure.
1571 * 0 on success, a negative errno value otherwise and rte_errno is set.
1574 mlx5_set_link_up(struct rte_eth_dev *dev)
1576 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1580 * Configure the TX function to use.
1583 * Pointer to private data structure.
1586 * Pointer to selected Tx burst function.
1589 mlx5_select_tx_function(struct rte_eth_dev *dev)
1591 struct mlx5_priv *priv = dev->data->dev_private;
1592 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1593 struct mlx5_dev_config *config = &priv->config;
1594 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1595 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1596 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1597 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1598 DEV_TX_OFFLOAD_IP_TNL_TSO |
1599 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1600 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1601 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1602 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1603 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1605 assert(priv != NULL);
1606 /* Select appropriate TX function. */
1607 if (vlan_insert || tso || swp)
1608 return tx_pkt_burst;
1609 if (config->mps == MLX5_MPW_ENHANCED) {
1610 if (mlx5_check_vec_tx_support(dev) > 0) {
1611 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1612 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1614 tx_pkt_burst = mlx5_tx_burst_vec;
1616 "port %u selected enhanced MPW Tx vectorized"
1618 dev->data->port_id);
1620 tx_pkt_burst = mlx5_tx_burst_empw;
1622 "port %u selected enhanced MPW Tx function",
1623 dev->data->port_id);
1625 } else if (config->mps && (config->txq_inline > 0)) {
1626 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1627 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1628 dev->data->port_id);
1629 } else if (config->mps) {
1630 tx_pkt_burst = mlx5_tx_burst_mpw;
1631 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1632 dev->data->port_id);
1634 return tx_pkt_burst;
1638 * Configure the RX function to use.
1641 * Pointer to private data structure.
1644 * Pointer to selected Rx burst function.
1647 mlx5_select_rx_function(struct rte_eth_dev *dev)
1649 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1651 assert(dev != NULL);
1652 if (mlx5_check_vec_rx_support(dev) > 0) {
1653 rx_pkt_burst = mlx5_rx_burst_vec;
1654 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1655 dev->data->port_id);
1656 } else if (mlx5_mprq_enabled(dev)) {
1657 rx_pkt_burst = mlx5_rx_burst_mprq;
1659 return rx_pkt_burst;
1663 * Check if mlx5 device was removed.
1666 * Pointer to Ethernet device structure.
1669 * 1 when device is removed, otherwise 0.
1672 mlx5_is_removed(struct rte_eth_dev *dev)
1674 struct ibv_device_attr device_attr;
1675 struct mlx5_priv *priv = dev->data->dev_private;
1677 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1683 * Get port ID list of mlx5 instances sharing a common device.
1686 * Device to look for.
1687 * @param[out] port_list
1688 * Result buffer for collected port IDs.
1689 * @param port_list_n
1690 * Maximum number of entries in result buffer. If 0, @p port_list can be
1694 * Number of matching instances regardless of the @p port_list_n
1695 * parameter, 0 if none were found.
1698 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1699 unsigned int port_list_n)
1704 RTE_ETH_FOREACH_DEV_OF(id, dev) {
1705 if (n < port_list_n)
1713 * Get the E-Switch domain id this port belongs to.
1717 * @param[out] es_domain_id
1718 * E-Switch domain id.
1719 * @param[out] es_port_id
1720 * The port id of the port in the E-Switch.
1723 * 0 on success, a negative errno value otherwise and rte_errno is set.
1726 mlx5_port_to_eswitch_info(uint16_t port,
1727 uint16_t *es_domain_id, uint16_t *es_port_id)
1729 struct rte_eth_dev *dev;
1730 struct mlx5_priv *priv;
1732 if (port >= RTE_MAX_ETHPORTS) {
1736 if (!rte_eth_dev_is_valid_port(port)) {
1740 dev = &rte_eth_devices[port];
1741 priv = dev->data->dev_private;
1742 if (!(priv->representor || priv->master)) {
1747 *es_domain_id = priv->domain_id;
1749 *es_port_id = priv->vport_id;
1754 * Get switch information associated with network interface.
1757 * Network interface index.
1759 * Switch information object, populated in case of success.
1762 * 0 on success, a negative errno value otherwise and rte_errno is set.
1765 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1767 char ifname[IF_NAMESIZE];
1768 char port_name[IF_NAMESIZE];
1770 struct mlx5_switch_info data = {
1773 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1778 bool port_switch_id_set = false;
1779 bool device_dir = false;
1783 if (!if_indextoname(ifindex, ifname)) {
1788 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1790 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1792 MKSTR(pci_device, "/sys/class/net/%s/device",
1795 file = fopen(phys_port_name, "rb");
1797 ret = fscanf(file, "%s", port_name);
1800 mlx5_translate_port_name(port_name, &data);
1802 file = fopen(phys_switch_id, "rb");
1807 port_switch_id_set =
1808 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1811 dir = opendir(pci_device);
1816 if (port_switch_id_set) {
1817 /* We have some E-Switch configuration. */
1818 mlx5_sysfs_check_switch_info(device_dir, &data);
1821 assert(!(data.master && data.representor));
1822 if (data.master && data.representor) {
1823 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1824 " and as representor", ifindex);
1832 * Analyze gathered port parameters via Netlink to recognize master
1833 * and representor devices for E-Switch configuration.
1835 * @param[in] num_vf_set
1836 * flag of presence of number of VFs port attribute.
1837 * @param[inout] switch_info
1838 * Port information, including port name as a number and port name
1839 * type if recognized
1842 * master and representor flags are set in switch_info according to
1843 * recognized parameters (if any).
1846 mlx5_nl_check_switch_info(bool num_vf_set,
1847 struct mlx5_switch_info *switch_info)
1849 switch (switch_info->name_type) {
1850 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1852 * Name is not recognized, assume the master,
1853 * check the number of VFs key presence.
1855 switch_info->master = num_vf_set;
1857 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1859 * Name is not set, this assumes the legacy naming
1860 * schema for master, just check if there is a
1861 * number of VFs key.
1863 switch_info->master = num_vf_set;
1865 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1866 /* New uplink naming schema recognized. */
1867 switch_info->master = 1;
1869 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1870 /* Legacy representors naming schema. */
1871 switch_info->representor = !num_vf_set;
1873 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1874 /* New representors naming schema. */
1875 switch_info->representor = 1;
1881 * Analyze gathered port parameters via sysfs to recognize master
1882 * and representor devices for E-Switch configuration.
1884 * @param[in] device_dir
1885 * flag of presence of "device" directory under port device key.
1886 * @param[inout] switch_info
1887 * Port information, including port name as a number and port name
1888 * type if recognized
1891 * master and representor flags are set in switch_info according to
1892 * recognized parameters (if any).
1895 mlx5_sysfs_check_switch_info(bool device_dir,
1896 struct mlx5_switch_info *switch_info)
1898 switch (switch_info->name_type) {
1899 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1901 * Name is not recognized, assume the master,
1902 * check the device directory presence.
1904 switch_info->master = device_dir;
1906 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1908 * Name is not set, this assumes the legacy naming
1909 * schema for master, just check if there is
1910 * a device directory.
1912 switch_info->master = device_dir;
1914 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1915 /* New uplink naming schema recognized. */
1916 switch_info->master = 1;
1918 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1919 /* Legacy representors naming schema. */
1920 switch_info->representor = !device_dir;
1922 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1923 /* New representors naming schema. */
1924 switch_info->representor = 1;
1930 * Extract port name, as a number, from sysfs or netlink information.
1932 * @param[in] port_name_in
1933 * String representing the port name.
1934 * @param[out] port_info_out
1935 * Port information, including port name as a number and port name
1936 * type if recognized
1939 * port_name field set according to recognized name format.
1942 mlx5_translate_port_name(const char *port_name_in,
1943 struct mlx5_switch_info *port_info_out)
1945 char pf_c1, pf_c2, vf_c1, vf_c2;
1950 * Check for port-name as a string of the form pf0vf0
1951 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1953 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1954 &pf_c1, &pf_c2, &port_info_out->pf_num,
1955 &vf_c1, &vf_c2, &port_info_out->port_name);
1956 if (sc_items == 6 &&
1957 pf_c1 == 'p' && pf_c2 == 'f' &&
1958 vf_c1 == 'v' && vf_c2 == 'f') {
1959 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1963 * Check for port-name as a string of the form p0
1964 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1966 sc_items = sscanf(port_name_in, "%c%d",
1967 &pf_c1, &port_info_out->port_name);
1968 if (sc_items == 2 && pf_c1 == 'p') {
1969 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1972 /* Check for port-name as a number (support kernel ver < 5.0 */
1974 port_info_out->port_name = strtol(port_name_in, &end, 0);
1976 (size_t)(end - port_name_in) == strlen(port_name_in)) {
1977 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1980 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;