1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox.
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/utsname.h>
21 #include <netinet/in.h>
22 #include <linux/ethtool.h>
23 #include <linux/sockios.h>
24 #include <linux/version.h>
29 #include <rte_atomic.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_bus_pci.h>
33 #include <rte_common.h>
34 #include <rte_interrupts.h>
35 #include <rte_alarm.h>
36 #include <rte_malloc.h>
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
42 /* Add defines in case the running kernel is not the same as user headers. */
43 #ifndef ETHTOOL_GLINKSETTINGS
44 struct ethtool_link_settings {
53 uint8_t eth_tp_mdix_ctrl;
54 int8_t link_mode_masks_nwords;
56 uint32_t link_mode_masks[];
59 #define ETHTOOL_GLINKSETTINGS 0x0000004c
60 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
61 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
62 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
63 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
64 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
65 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
66 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
67 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
68 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
69 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
70 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
71 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
72 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
73 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
74 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
75 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
77 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
78 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
79 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
80 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
82 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
83 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
84 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
86 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
87 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
88 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
89 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
90 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
94 * Get interface name from private structure.
97 * Pointer to private structure.
99 * Interface name output buffer.
102 * 0 on success, -1 on failure and errno is set.
105 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
109 unsigned int dev_type = 0;
110 unsigned int dev_port_prev = ~0u;
111 char match[IF_NAMESIZE] = "";
114 MKSTR(path, "%s/device/net", priv->ibdev_path);
120 while ((dent = readdir(dir)) != NULL) {
121 char *name = dent->d_name;
123 unsigned int dev_port;
126 if ((name[0] == '.') &&
127 ((name[1] == '\0') ||
128 ((name[1] == '.') && (name[2] == '\0'))))
131 MKSTR(path, "%s/device/net/%s/%s",
132 priv->ibdev_path, name,
133 (dev_type ? "dev_id" : "dev_port"));
135 file = fopen(path, "rb");
140 * Switch to dev_id when dev_port does not exist as
141 * is the case with Linux kernel versions < 3.15.
152 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
157 * Switch to dev_id when dev_port returns the same value for
158 * all ports. May happen when using a MOFED release older than
159 * 3.0 with a Linux kernel >= 3.15.
161 if (dev_port == dev_port_prev)
163 dev_port_prev = dev_port;
164 if (dev_port == (priv->port - 1u))
165 snprintf(match, sizeof(match), "%s", name);
168 if (match[0] == '\0')
170 strncpy(*ifname, match, sizeof(*ifname));
175 * Check if the counter is located on ib counters file.
181 * 1 if counter is located on ib counters file , 0 otherwise.
184 priv_is_ib_cntr(const char *cntr)
186 if (!strcmp(cntr, "out_of_buffer"))
192 * Read from sysfs entry.
195 * Pointer to private structure.
197 * Entry name relative to sysfs path.
199 * Data output buffer.
204 * 0 on success, -1 on failure and errno is set.
207 priv_sysfs_read(const struct priv *priv, const char *entry,
208 char *buf, size_t size)
210 char ifname[IF_NAMESIZE];
215 if (priv_get_ifname(priv, &ifname))
218 if (priv_is_ib_cntr(entry)) {
219 MKSTR(path, "%s/ports/1/hw_counters/%s",
220 priv->ibdev_path, entry);
221 file = fopen(path, "rb");
223 MKSTR(path, "%s/device/net/%s/%s",
224 priv->ibdev_path, ifname, entry);
225 file = fopen(path, "rb");
229 ret = fread(buf, 1, size, file);
231 if (((size_t)ret < size) && (ferror(file)))
241 * Write to sysfs entry.
244 * Pointer to private structure.
246 * Entry name relative to sysfs path.
253 * 0 on success, -1 on failure and errno is set.
256 priv_sysfs_write(const struct priv *priv, const char *entry,
257 char *buf, size_t size)
259 char ifname[IF_NAMESIZE];
264 if (priv_get_ifname(priv, &ifname))
267 MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
269 file = fopen(path, "wb");
272 ret = fwrite(buf, 1, size, file);
274 if (((size_t)ret < size) || (ferror(file)))
284 * Get unsigned long sysfs property.
287 * Pointer to private structure.
289 * Entry name relative to sysfs path.
291 * Value output buffer.
294 * 0 on success, -1 on failure and errno is set.
297 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
300 unsigned long value_ret;
303 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
305 DEBUG("cannot read %s value from sysfs: %s",
306 name, strerror(errno));
309 value_str[ret] = '\0';
311 value_ret = strtoul(value_str, NULL, 0);
313 DEBUG("invalid %s value `%s': %s", name, value_str,
322 * Set unsigned long sysfs property.
325 * Pointer to private structure.
327 * Entry name relative to sysfs path.
332 * 0 on success, -1 on failure and errno is set.
335 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
338 MKSTR(value_str, "%lu", value);
340 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
342 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
343 name, value_str, value, strerror(errno));
350 * Perform ifreq ioctl() on associated Ethernet device.
353 * Pointer to private structure.
355 * Request number to pass to ioctl().
357 * Interface request structure output buffer.
360 * 0 on success, -1 on failure and errno is set.
363 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
365 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
370 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
371 ret = ioctl(sock, req, ifr);
377 * Return the number of active VFs for the current device.
380 * Pointer to private structure.
381 * @param[out] num_vfs
382 * Number of active VFs.
385 * 0 on success, -1 on failure and errno is set.
388 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
390 /* The sysfs entry name depends on the operating system. */
391 const char **name = (const char *[]){
392 "device/sriov_numvfs",
393 "device/mlx5_num_vfs",
399 unsigned long ulong_num_vfs;
401 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
403 *num_vfs = ulong_num_vfs;
404 } while (*(++name) && ret);
412 * Pointer to private structure.
414 * MTU value output buffer.
417 * 0 on success, -1 on failure and errno is set.
420 priv_get_mtu(struct priv *priv, uint16_t *mtu)
422 unsigned long ulong_mtu;
424 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
431 * Read device counter from sysfs.
434 * Pointer to private structure.
438 * Counter output buffer.
441 * 0 on success, -1 on failure and errno is set.
444 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
446 unsigned long ulong_ctr;
448 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
458 * Pointer to private structure.
463 * 0 on success, -1 on failure and errno is set.
466 priv_set_mtu(struct priv *priv, uint16_t mtu)
470 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
471 priv_get_mtu(priv, &new_mtu))
483 * Pointer to private structure.
485 * Bitmask for flags that must remain untouched.
487 * Bitmask for flags to modify.
490 * 0 on success, -1 on failure and errno is set.
493 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
497 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
500 tmp |= (flags & (~keep));
501 return priv_set_sysfs_ulong(priv, "flags", tmp);
505 * Ethernet device configuration.
507 * Prepare the driver for a given number of TX and RX queues.
510 * Pointer to Ethernet device structure.
513 * 0 on success, errno value on failure.
516 dev_configure(struct rte_eth_dev *dev)
518 struct priv *priv = dev->data->dev_private;
519 unsigned int rxqs_n = dev->data->nb_rx_queues;
520 unsigned int txqs_n = dev->data->nb_tx_queues;
523 unsigned int reta_idx_n;
524 const uint8_t use_app_rss_key =
525 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
526 uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
527 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
528 uint64_t supp_rx_offloads =
529 (mlx5_priv_get_rx_port_offloads(priv) |
530 mlx5_priv_get_rx_queue_offloads(priv));
531 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
533 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
534 ERROR("Some Tx offloads are not supported "
535 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
536 tx_offloads, supp_tx_offloads);
539 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
540 ERROR("Some Rx offloads are not supported "
541 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
542 rx_offloads, supp_rx_offloads);
545 if (use_app_rss_key &&
546 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
547 rss_hash_default_key_len)) {
548 /* MLX5 RSS only support 40bytes key. */
551 priv->rss_conf.rss_key =
552 rte_realloc(priv->rss_conf.rss_key,
553 rss_hash_default_key_len, 0);
554 if (!priv->rss_conf.rss_key) {
555 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
558 memcpy(priv->rss_conf.rss_key,
560 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
561 rss_hash_default_key,
562 rss_hash_default_key_len);
563 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
564 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
565 priv->rxqs = (void *)dev->data->rx_queues;
566 priv->txqs = (void *)dev->data->tx_queues;
567 if (txqs_n != priv->txqs_n) {
568 INFO("%p: TX queues number update: %u -> %u",
569 (void *)dev, priv->txqs_n, txqs_n);
570 priv->txqs_n = txqs_n;
572 if (rxqs_n > priv->config.ind_table_max_size) {
573 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
576 if (rxqs_n == priv->rxqs_n)
578 INFO("%p: RX queues number update: %u -> %u",
579 (void *)dev, priv->rxqs_n, rxqs_n);
580 priv->rxqs_n = rxqs_n;
581 /* If the requested number of RX queues is not a power of two, use the
582 * maximum indirection table size for better balancing.
583 * The result is always rounded to the next power of two. */
584 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
585 priv->config.ind_table_max_size :
587 if (priv_rss_reta_index_resize(priv, reta_idx_n))
589 /* When the number of RX queues is not a power of two, the remaining
590 * table entries are padded with reused WQs and hashes are not spread
592 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
593 (*priv->reta_idx)[i] = j;
601 * DPDK callback for Ethernet device configuration.
604 * Pointer to Ethernet device structure.
607 * 0 on success, negative errno value on failure.
610 mlx5_dev_configure(struct rte_eth_dev *dev)
612 struct priv *priv = dev->data->dev_private;
616 ret = dev_configure(dev);
623 * DPDK callback to get information about the device.
626 * Pointer to Ethernet device structure.
628 * Info structure output buffer.
631 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
633 struct priv *priv = dev->data->dev_private;
634 struct mlx5_dev_config *config = &priv->config;
636 char ifname[IF_NAMESIZE];
638 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
641 /* FIXME: we should ask the device for these values. */
642 info->min_rx_bufsize = 32;
643 info->max_rx_pktlen = 65536;
645 * Since we need one CQ per QP, the limit is the minimum number
646 * between the two values.
648 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
649 priv->device_attr.orig_attr.max_qp);
650 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
653 info->max_rx_queues = max;
654 info->max_tx_queues = max;
655 info->max_mac_addrs = RTE_DIM(priv->mac);
656 info->rx_queue_offload_capa =
657 mlx5_priv_get_rx_queue_offloads(priv);
658 info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
659 info->rx_queue_offload_capa);
660 info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
661 if (priv_get_ifname(priv, &ifname) == 0)
662 info->if_index = if_nametoindex(ifname);
663 info->reta_size = priv->reta_idx_n ?
664 priv->reta_idx_n : config->ind_table_max_size;
665 info->hash_key_size = priv->rss_conf.rss_key_len;
666 info->speed_capa = priv->link_speed_capa;
671 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
673 static const uint32_t ptypes[] = {
674 /* refers to rxq_cq_to_pkt_type() */
676 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
677 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
678 RTE_PTYPE_L4_NONFRAG,
682 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
683 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
684 RTE_PTYPE_INNER_L4_NONFRAG,
685 RTE_PTYPE_INNER_L4_FRAG,
686 RTE_PTYPE_INNER_L4_TCP,
687 RTE_PTYPE_INNER_L4_UDP,
691 if (dev->rx_pkt_burst == mlx5_rx_burst ||
692 dev->rx_pkt_burst == mlx5_rx_burst_vec)
698 * DPDK callback to retrieve physical link information.
701 * Pointer to Ethernet device structure.
702 * @param wait_to_complete
703 * Wait for request completion (ignored).
706 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
708 struct priv *priv = dev->data->dev_private;
709 struct ethtool_cmd edata = {
710 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
713 struct rte_eth_link dev_link;
716 /* priv_lock() is not taken to allow concurrent calls. */
718 (void)wait_to_complete;
719 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
720 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
723 memset(&dev_link, 0, sizeof(dev_link));
724 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
725 (ifr.ifr_flags & IFF_RUNNING));
726 ifr.ifr_data = (void *)&edata;
727 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
728 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
732 link_speed = ethtool_cmd_speed(&edata);
733 if (link_speed == -1)
734 dev_link.link_speed = 0;
736 dev_link.link_speed = link_speed;
737 priv->link_speed_capa = 0;
738 if (edata.supported & SUPPORTED_Autoneg)
739 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
740 if (edata.supported & (SUPPORTED_1000baseT_Full |
741 SUPPORTED_1000baseKX_Full))
742 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
743 if (edata.supported & SUPPORTED_10000baseKR_Full)
744 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
745 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
746 SUPPORTED_40000baseCR4_Full |
747 SUPPORTED_40000baseSR4_Full |
748 SUPPORTED_40000baseLR4_Full))
749 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
750 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
751 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
752 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
753 ETH_LINK_SPEED_FIXED);
754 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
755 /* Link status changed. */
756 dev->data->dev_link = dev_link;
759 /* Link status is still the same. */
764 * Retrieve physical link information (unlocked version using new ioctl).
767 * Pointer to Ethernet device structure.
768 * @param wait_to_complete
769 * Wait for request completion (ignored).
772 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
774 struct priv *priv = dev->data->dev_private;
775 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
777 struct rte_eth_link dev_link;
780 (void)wait_to_complete;
781 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
782 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
785 memset(&dev_link, 0, sizeof(dev_link));
786 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
787 (ifr.ifr_flags & IFF_RUNNING));
788 ifr.ifr_data = (void *)&gcmd;
789 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
790 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
794 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
796 alignas(struct ethtool_link_settings)
797 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
798 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
799 struct ethtool_link_settings *ecmd = (void *)data;
802 ifr.ifr_data = (void *)ecmd;
803 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
804 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
808 dev_link.link_speed = ecmd->speed;
809 sc = ecmd->link_mode_masks[0] |
810 ((uint64_t)ecmd->link_mode_masks[1] << 32);
811 priv->link_speed_capa = 0;
812 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
813 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
814 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
815 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
816 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
817 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
818 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
819 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
820 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
821 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
822 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
823 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
824 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
825 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
826 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
827 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
828 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
829 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
830 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
831 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
832 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
833 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
834 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
835 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
836 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
837 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
838 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
839 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
840 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
841 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
842 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
843 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
844 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
845 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
846 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
847 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
848 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
849 ETH_LINK_SPEED_FIXED);
850 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
851 /* Link status changed. */
852 dev->data->dev_link = dev_link;
855 /* Link status is still the same. */
860 * Enable receiving and transmitting traffic.
863 * Pointer to private structure.
866 priv_link_start(struct priv *priv)
868 struct rte_eth_dev *dev = priv->dev;
871 dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
872 dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
873 err = priv_dev_traffic_enable(priv, dev);
875 ERROR("%p: error occurred while configuring control flows: %s",
876 (void *)priv, strerror(err));
877 err = priv_flow_start(priv, &priv->flows);
879 ERROR("%p: error occurred while configuring flows: %s",
880 (void *)priv, strerror(err));
884 * Disable receiving and transmitting traffic.
887 * Pointer to private structure.
890 priv_link_stop(struct priv *priv)
892 struct rte_eth_dev *dev = priv->dev;
894 priv_flow_stop(priv, &priv->flows);
895 priv_dev_traffic_disable(priv, dev);
896 dev->rx_pkt_burst = removed_rx_burst;
897 dev->tx_pkt_burst = removed_tx_burst;
901 * Retrieve physical link information and update rx/tx_pkt_burst callbacks
905 * Pointer to private structure.
906 * @param wait_to_complete
907 * Wait for request completion (ignored).
910 priv_link_update(struct priv *priv, int wait_to_complete)
912 struct rte_eth_dev *dev = priv->dev;
913 struct utsname utsname;
916 struct rte_eth_link dev_link = dev->data->dev_link;
918 if (uname(&utsname) == -1 ||
919 sscanf(utsname.release, "%d.%d.%d",
920 &ver[0], &ver[1], &ver[2]) != 3 ||
921 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
922 ret = mlx5_link_update_unlocked_gset(dev, wait_to_complete);
924 ret = mlx5_link_update_unlocked_gs(dev, wait_to_complete);
925 /* If lsc interrupt is disabled, should always be ready for traffic. */
926 if (!dev->data->dev_conf.intr_conf.lsc) {
927 priv_link_start(priv);
930 /* Re-select burst callbacks only if link status has been changed. */
931 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
932 if (dev->data->dev_link.link_status == ETH_LINK_UP)
933 priv_link_start(priv);
935 priv_link_stop(priv);
941 * DPDK callback to retrieve physical link information.
944 * Pointer to Ethernet device structure.
945 * @param wait_to_complete
946 * Wait for request completion (ignored).
949 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
951 struct priv *priv = dev->data->dev_private;
955 ret = priv_link_update(priv, wait_to_complete);
961 * DPDK callback to change the MTU.
964 * Pointer to Ethernet device structure.
969 * 0 on success, negative errno value on failure.
972 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
974 struct priv *priv = dev->data->dev_private;
979 ret = priv_get_mtu(priv, &kern_mtu);
982 /* Set kernel interface MTU first. */
983 ret = priv_set_mtu(priv, mtu);
986 ret = priv_get_mtu(priv, &kern_mtu);
989 if (kern_mtu == mtu) {
991 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
997 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
1005 * DPDK callback to get flow control status.
1008 * Pointer to Ethernet device structure.
1009 * @param[out] fc_conf
1010 * Flow control output buffer.
1013 * 0 on success, negative errno value on failure.
1016 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1018 struct priv *priv = dev->data->dev_private;
1020 struct ethtool_pauseparam ethpause = {
1021 .cmd = ETHTOOL_GPAUSEPARAM
1025 ifr.ifr_data = (void *)ðpause;
1027 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1029 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1035 fc_conf->autoneg = ethpause.autoneg;
1036 if (ethpause.rx_pause && ethpause.tx_pause)
1037 fc_conf->mode = RTE_FC_FULL;
1038 else if (ethpause.rx_pause)
1039 fc_conf->mode = RTE_FC_RX_PAUSE;
1040 else if (ethpause.tx_pause)
1041 fc_conf->mode = RTE_FC_TX_PAUSE;
1043 fc_conf->mode = RTE_FC_NONE;
1053 * DPDK callback to modify flow control parameters.
1056 * Pointer to Ethernet device structure.
1057 * @param[in] fc_conf
1058 * Flow control parameters.
1061 * 0 on success, negative errno value on failure.
1064 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1066 struct priv *priv = dev->data->dev_private;
1068 struct ethtool_pauseparam ethpause = {
1069 .cmd = ETHTOOL_SPAUSEPARAM
1073 ifr.ifr_data = (void *)ðpause;
1074 ethpause.autoneg = fc_conf->autoneg;
1075 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1076 (fc_conf->mode & RTE_FC_RX_PAUSE))
1077 ethpause.rx_pause = 1;
1079 ethpause.rx_pause = 0;
1081 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1082 (fc_conf->mode & RTE_FC_TX_PAUSE))
1083 ethpause.tx_pause = 1;
1085 ethpause.tx_pause = 0;
1088 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1090 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1104 * Get PCI information from struct ibv_device.
1107 * Pointer to Ethernet device structure.
1108 * @param[out] pci_addr
1109 * PCI bus address output buffer.
1112 * 0 on success, -1 on failure and errno is set.
1115 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1116 struct rte_pci_addr *pci_addr)
1120 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1122 file = fopen(path, "rb");
1125 while (fgets(line, sizeof(line), file) == line) {
1126 size_t len = strlen(line);
1129 /* Truncate long lines. */
1130 if (len == (sizeof(line) - 1))
1131 while (line[(len - 1)] != '\n') {
1135 line[(len - 1)] = ret;
1137 /* Extract information. */
1140 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1144 &pci_addr->function) == 4) {
1154 * Update the link status.
1157 * Pointer to private structure.
1160 * Zero if the callback process can be called immediately.
1163 priv_link_status_update(struct priv *priv)
1165 struct rte_eth_link *link = &priv->dev->data->dev_link;
1167 priv_link_update(priv, 0);
1168 if (((link->link_speed == 0) && link->link_status) ||
1169 ((link->link_speed != 0) && !link->link_status)) {
1171 * Inconsistent status. Event likely occurred before the
1172 * kernel netdevice exposes the new status.
1174 if (!priv->pending_alarm) {
1175 priv->pending_alarm = 1;
1176 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1177 mlx5_dev_link_status_handler,
1181 } else if (unlikely(priv->pending_alarm)) {
1182 /* Link interrupt occurred while alarm is already scheduled. */
1183 priv->pending_alarm = 0;
1184 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1190 * Device status handler.
1193 * Pointer to private structure.
1195 * Pointer to event flags holder.
1198 * Events bitmap of callback process which can be called immediately.
1201 priv_dev_status_handler(struct priv *priv)
1203 struct ibv_async_event event;
1206 /* Read all message and acknowledge them. */
1208 if (ibv_get_async_event(priv->ctx, &event))
1210 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1211 event.event_type == IBV_EVENT_PORT_ERR) &&
1212 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1213 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1214 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1215 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1216 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1218 DEBUG("event type %d on port %d not handled",
1219 event.event_type, event.element.port_num);
1220 ibv_ack_async_event(&event);
1222 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1223 if (priv_link_status_update(priv))
1224 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1229 * Handle delayed link status event.
1232 * Registered argument.
1235 mlx5_dev_link_status_handler(void *arg)
1237 struct rte_eth_dev *dev = arg;
1238 struct priv *priv = dev->data->dev_private;
1241 while (!priv_trylock(priv)) {
1242 /* Alarm is being canceled. */
1243 if (priv->pending_alarm == 0)
1247 priv->pending_alarm = 0;
1248 ret = priv_link_status_update(priv);
1251 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1255 * Handle interrupts from the NIC.
1257 * @param[in] intr_handle
1258 * Interrupt handler.
1260 * Callback argument.
1263 mlx5_dev_interrupt_handler(void *cb_arg)
1265 struct rte_eth_dev *dev = cb_arg;
1266 struct priv *priv = dev->data->dev_private;
1270 events = priv_dev_status_handler(priv);
1272 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1273 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1274 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1275 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1279 * Handle interrupts from the socket.
1282 * Callback argument.
1285 mlx5_dev_handler_socket(void *cb_arg)
1287 struct rte_eth_dev *dev = cb_arg;
1288 struct priv *priv = dev->data->dev_private;
1291 priv_socket_handle(priv);
1296 * Uninstall interrupt handler.
1299 * Pointer to private structure.
1301 * Pointer to the rte_eth_dev structure.
1304 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1306 if (dev->data->dev_conf.intr_conf.lsc ||
1307 dev->data->dev_conf.intr_conf.rmv)
1308 rte_intr_callback_unregister(&priv->intr_handle,
1309 mlx5_dev_interrupt_handler, dev);
1310 if (priv->primary_socket)
1311 rte_intr_callback_unregister(&priv->intr_handle_socket,
1312 mlx5_dev_handler_socket, dev);
1313 if (priv->pending_alarm) {
1314 priv->pending_alarm = 0;
1315 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1317 priv->intr_handle.fd = 0;
1318 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1319 priv->intr_handle_socket.fd = 0;
1320 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1324 * Install interrupt handler.
1327 * Pointer to private structure.
1329 * Pointer to the rte_eth_dev structure.
1332 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1336 assert(priv->ctx->async_fd > 0);
1337 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1338 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1340 INFO("failed to change file descriptor async event queue");
1341 dev->data->dev_conf.intr_conf.lsc = 0;
1342 dev->data->dev_conf.intr_conf.rmv = 0;
1344 if (dev->data->dev_conf.intr_conf.lsc ||
1345 dev->data->dev_conf.intr_conf.rmv) {
1346 priv->intr_handle.fd = priv->ctx->async_fd;
1347 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1348 rte_intr_callback_register(&priv->intr_handle,
1349 mlx5_dev_interrupt_handler, dev);
1352 rc = priv_socket_init(priv);
1353 if (!rc && priv->primary_socket) {
1354 priv->intr_handle_socket.fd = priv->primary_socket;
1355 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1356 rte_intr_callback_register(&priv->intr_handle_socket,
1357 mlx5_dev_handler_socket, dev);
1362 * Change the link state (UP / DOWN).
1365 * Pointer to private data structure.
1367 * Nonzero for link up, otherwise link down.
1370 * 0 on success, errno value on failure.
1373 priv_dev_set_link(struct priv *priv, int up)
1375 return priv_set_flags(priv, ~IFF_UP, up ? IFF_UP : ~IFF_UP);
1379 * DPDK callback to bring the link DOWN.
1382 * Pointer to Ethernet device structure.
1385 * 0 on success, errno value on failure.
1388 mlx5_set_link_down(struct rte_eth_dev *dev)
1390 struct priv *priv = dev->data->dev_private;
1394 err = priv_dev_set_link(priv, 0);
1400 * DPDK callback to bring the link UP.
1403 * Pointer to Ethernet device structure.
1406 * 0 on success, errno value on failure.
1409 mlx5_set_link_up(struct rte_eth_dev *dev)
1411 struct priv *priv = dev->data->dev_private;
1415 err = priv_dev_set_link(priv, 1);
1421 * Configure the TX function to use.
1424 * Pointer to private data structure.
1426 * Pointer to rte_eth_dev structure.
1429 * Pointer to selected Tx burst function.
1432 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1434 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1435 struct mlx5_dev_config *config = &priv->config;
1436 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1437 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1438 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1439 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1440 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1442 assert(priv != NULL);
1443 /* Select appropriate TX function. */
1444 if (vlan_insert || tso)
1445 return tx_pkt_burst;
1446 if (config->mps == MLX5_MPW_ENHANCED) {
1447 if (priv_check_vec_tx_support(priv, dev) > 0) {
1448 if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1449 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1451 tx_pkt_burst = mlx5_tx_burst_vec;
1452 DEBUG("selected Enhanced MPW TX vectorized function");
1454 tx_pkt_burst = mlx5_tx_burst_empw;
1455 DEBUG("selected Enhanced MPW TX function");
1457 } else if (config->mps && (config->txq_inline > 0)) {
1458 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1459 DEBUG("selected MPW inline TX function");
1460 } else if (config->mps) {
1461 tx_pkt_burst = mlx5_tx_burst_mpw;
1462 DEBUG("selected MPW TX function");
1464 return tx_pkt_burst;
1468 * Configure the RX function to use.
1471 * Pointer to private data structure.
1473 * Pointer to rte_eth_dev structure.
1476 * Pointer to selected Rx burst function.
1479 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1481 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1483 assert(priv != NULL);
1484 if (priv_check_vec_rx_support(priv) > 0) {
1485 rx_pkt_burst = mlx5_rx_burst_vec;
1486 DEBUG("selected RX vectorized function");
1488 return rx_pkt_burst;
1492 * Check if mlx5 device was removed.
1495 * Pointer to Ethernet device structure.
1498 * 1 when device is removed, otherwise 0.
1501 mlx5_is_removed(struct rte_eth_dev *dev)
1503 struct ibv_device_attr device_attr;
1504 struct priv *priv = dev->data->dev_private;
1506 if (ibv_query_device(priv->ctx, &device_attr) == EIO)