4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <sys/utsname.h>
49 #include <netinet/in.h>
50 #include <linux/ethtool.h>
51 #include <linux/sockios.h>
52 #include <linux/version.h>
57 #include <rte_atomic.h>
58 #include <rte_ethdev.h>
59 #include <rte_bus_pci.h>
61 #include <rte_common.h>
62 #include <rte_interrupts.h>
63 #include <rte_alarm.h>
64 #include <rte_malloc.h>
67 #include "mlx5_rxtx.h"
68 #include "mlx5_utils.h"
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 * Return private structure associated with an Ethernet device.
125 * Pointer to Ethernet device structure.
128 * Pointer to private structure.
131 mlx5_get_priv(struct rte_eth_dev *dev)
133 return dev->data->dev_private;
137 * Check if running as a secondary process.
140 * Nonzero if running as a secondary process.
143 mlx5_is_secondary(void)
145 return rte_eal_process_type() == RTE_PROC_SECONDARY;
149 * Get interface name from private structure.
152 * Pointer to private structure.
154 * Interface name output buffer.
157 * 0 on success, -1 on failure and errno is set.
160 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
164 unsigned int dev_type = 0;
165 unsigned int dev_port_prev = ~0u;
166 char match[IF_NAMESIZE] = "";
169 MKSTR(path, "%s/device/net", priv->ibdev_path);
175 while ((dent = readdir(dir)) != NULL) {
176 char *name = dent->d_name;
178 unsigned int dev_port;
181 if ((name[0] == '.') &&
182 ((name[1] == '\0') ||
183 ((name[1] == '.') && (name[2] == '\0'))))
186 MKSTR(path, "%s/device/net/%s/%s",
187 priv->ibdev_path, name,
188 (dev_type ? "dev_id" : "dev_port"));
190 file = fopen(path, "rb");
195 * Switch to dev_id when dev_port does not exist as
196 * is the case with Linux kernel versions < 3.15.
207 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
212 * Switch to dev_id when dev_port returns the same value for
213 * all ports. May happen when using a MOFED release older than
214 * 3.0 with a Linux kernel >= 3.15.
216 if (dev_port == dev_port_prev)
218 dev_port_prev = dev_port;
219 if (dev_port == (priv->port - 1u))
220 snprintf(match, sizeof(match), "%s", name);
223 if (match[0] == '\0')
225 strncpy(*ifname, match, sizeof(*ifname));
230 * Check if the counter is located on ib counters file.
236 * 1 if counter is located on ib counters file , 0 otherwise.
239 priv_is_ib_cntr(const char *cntr)
241 if (!strcmp(cntr, "out_of_buffer"))
247 * Read from sysfs entry.
250 * Pointer to private structure.
252 * Entry name relative to sysfs path.
254 * Data output buffer.
259 * 0 on success, -1 on failure and errno is set.
262 priv_sysfs_read(const struct priv *priv, const char *entry,
263 char *buf, size_t size)
265 char ifname[IF_NAMESIZE];
270 if (priv_get_ifname(priv, &ifname))
273 if (priv_is_ib_cntr(entry)) {
274 MKSTR(path, "%s/ports/1/hw_counters/%s",
275 priv->ibdev_path, entry);
276 file = fopen(path, "rb");
278 MKSTR(path, "%s/device/net/%s/%s",
279 priv->ibdev_path, ifname, entry);
280 file = fopen(path, "rb");
284 ret = fread(buf, 1, size, file);
286 if (((size_t)ret < size) && (ferror(file)))
296 * Write to sysfs entry.
299 * Pointer to private structure.
301 * Entry name relative to sysfs path.
308 * 0 on success, -1 on failure and errno is set.
311 priv_sysfs_write(const struct priv *priv, const char *entry,
312 char *buf, size_t size)
314 char ifname[IF_NAMESIZE];
319 if (priv_get_ifname(priv, &ifname))
322 MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
324 file = fopen(path, "wb");
327 ret = fwrite(buf, 1, size, file);
329 if (((size_t)ret < size) || (ferror(file)))
339 * Get unsigned long sysfs property.
342 * Pointer to private structure.
344 * Entry name relative to sysfs path.
346 * Value output buffer.
349 * 0 on success, -1 on failure and errno is set.
352 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
355 unsigned long value_ret;
358 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
360 DEBUG("cannot read %s value from sysfs: %s",
361 name, strerror(errno));
364 value_str[ret] = '\0';
366 value_ret = strtoul(value_str, NULL, 0);
368 DEBUG("invalid %s value `%s': %s", name, value_str,
377 * Set unsigned long sysfs property.
380 * Pointer to private structure.
382 * Entry name relative to sysfs path.
387 * 0 on success, -1 on failure and errno is set.
390 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
393 MKSTR(value_str, "%lu", value);
395 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
397 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
398 name, value_str, value, strerror(errno));
405 * Perform ifreq ioctl() on associated Ethernet device.
408 * Pointer to private structure.
410 * Request number to pass to ioctl().
412 * Interface request structure output buffer.
415 * 0 on success, -1 on failure and errno is set.
418 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
420 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
425 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
426 ret = ioctl(sock, req, ifr);
432 * Return the number of active VFs for the current device.
435 * Pointer to private structure.
436 * @param[out] num_vfs
437 * Number of active VFs.
440 * 0 on success, -1 on failure and errno is set.
443 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
445 /* The sysfs entry name depends on the operating system. */
446 const char **name = (const char *[]){
447 "device/sriov_numvfs",
448 "device/mlx5_num_vfs",
454 unsigned long ulong_num_vfs;
456 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
458 *num_vfs = ulong_num_vfs;
459 } while (*(++name) && ret);
467 * Pointer to private structure.
469 * MTU value output buffer.
472 * 0 on success, -1 on failure and errno is set.
475 priv_get_mtu(struct priv *priv, uint16_t *mtu)
477 unsigned long ulong_mtu;
479 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
486 * Read device counter from sysfs.
489 * Pointer to private structure.
493 * Counter output buffer.
496 * 0 on success, -1 on failure and errno is set.
499 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
501 unsigned long ulong_ctr;
503 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
513 * Pointer to private structure.
518 * 0 on success, -1 on failure and errno is set.
521 priv_set_mtu(struct priv *priv, uint16_t mtu)
525 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
526 priv_get_mtu(priv, &new_mtu))
538 * Pointer to private structure.
540 * Bitmask for flags that must remain untouched.
542 * Bitmask for flags to modify.
545 * 0 on success, -1 on failure and errno is set.
548 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
552 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
555 tmp |= (flags & (~keep));
556 return priv_set_sysfs_ulong(priv, "flags", tmp);
560 * Ethernet device configuration.
562 * Prepare the driver for a given number of TX and RX queues.
565 * Pointer to Ethernet device structure.
568 * 0 on success, errno value on failure.
571 dev_configure(struct rte_eth_dev *dev)
573 struct priv *priv = dev->data->dev_private;
574 unsigned int rxqs_n = dev->data->nb_rx_queues;
575 unsigned int txqs_n = dev->data->nb_tx_queues;
578 unsigned int reta_idx_n;
579 const uint8_t use_app_rss_key =
580 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len;
582 if (use_app_rss_key &&
583 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
584 rss_hash_default_key_len)) {
585 /* MLX5 RSS only support 40bytes key. */
588 priv->rss_conf.rss_key =
589 rte_realloc(priv->rss_conf.rss_key,
590 rss_hash_default_key_len, 0);
591 if (!priv->rss_conf.rss_key) {
592 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
595 memcpy(priv->rss_conf.rss_key,
597 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
598 rss_hash_default_key,
599 rss_hash_default_key_len);
600 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
601 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
602 priv->rxqs = (void *)dev->data->rx_queues;
603 priv->txqs = (void *)dev->data->tx_queues;
604 if (txqs_n != priv->txqs_n) {
605 INFO("%p: TX queues number update: %u -> %u",
606 (void *)dev, priv->txqs_n, txqs_n);
607 priv->txqs_n = txqs_n;
609 if (rxqs_n > priv->ind_table_max_size) {
610 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
613 if (rxqs_n == priv->rxqs_n)
615 INFO("%p: RX queues number update: %u -> %u",
616 (void *)dev, priv->rxqs_n, rxqs_n);
617 priv->rxqs_n = rxqs_n;
618 /* If the requested number of RX queues is not a power of two, use the
619 * maximum indirection table size for better balancing.
620 * The result is always rounded to the next power of two. */
621 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
622 priv->ind_table_max_size :
624 if (priv_rss_reta_index_resize(priv, reta_idx_n))
626 /* When the number of RX queues is not a power of two, the remaining
627 * table entries are padded with reused WQs and hashes are not spread
629 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
630 (*priv->reta_idx)[i] = j;
638 * DPDK callback for Ethernet device configuration.
641 * Pointer to Ethernet device structure.
644 * 0 on success, negative errno value on failure.
647 mlx5_dev_configure(struct rte_eth_dev *dev)
649 struct priv *priv = dev->data->dev_private;
652 if (mlx5_is_secondary())
653 return -E_RTE_SECONDARY;
656 ret = dev_configure(dev);
663 * DPDK callback to get information about the device.
666 * Pointer to Ethernet device structure.
668 * Info structure output buffer.
671 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
673 struct priv *priv = mlx5_get_priv(dev);
675 char ifname[IF_NAMESIZE];
677 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
680 /* FIXME: we should ask the device for these values. */
681 info->min_rx_bufsize = 32;
682 info->max_rx_pktlen = 65536;
684 * Since we need one CQ per QP, the limit is the minimum number
685 * between the two values.
687 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
688 priv->device_attr.orig_attr.max_qp);
689 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
692 info->max_rx_queues = max;
693 info->max_tx_queues = max;
694 info->max_mac_addrs = RTE_DIM(priv->mac);
695 info->rx_offload_capa =
697 (DEV_RX_OFFLOAD_IPV4_CKSUM |
698 DEV_RX_OFFLOAD_UDP_CKSUM |
699 DEV_RX_OFFLOAD_TCP_CKSUM) :
701 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0) |
702 DEV_RX_OFFLOAD_TIMESTAMP;
705 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
707 info->tx_offload_capa |=
708 (DEV_TX_OFFLOAD_IPV4_CKSUM |
709 DEV_TX_OFFLOAD_UDP_CKSUM |
710 DEV_TX_OFFLOAD_TCP_CKSUM);
712 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
714 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
715 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
716 DEV_TX_OFFLOAD_GRE_TNL_TSO);
717 if (priv_get_ifname(priv, &ifname) == 0)
718 info->if_index = if_nametoindex(ifname);
719 info->reta_size = priv->reta_idx_n ?
720 priv->reta_idx_n : priv->ind_table_max_size;
721 info->hash_key_size = priv->rss_conf.rss_key_len;
722 info->speed_capa = priv->link_speed_capa;
727 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
729 static const uint32_t ptypes[] = {
730 /* refers to rxq_cq_to_pkt_type() */
732 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
733 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
734 RTE_PTYPE_L4_NONFRAG,
738 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
739 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
740 RTE_PTYPE_INNER_L4_NONFRAG,
741 RTE_PTYPE_INNER_L4_FRAG,
742 RTE_PTYPE_INNER_L4_TCP,
743 RTE_PTYPE_INNER_L4_UDP,
747 if (dev->rx_pkt_burst == mlx5_rx_burst ||
748 dev->rx_pkt_burst == mlx5_rx_burst_vec)
754 * DPDK callback to retrieve physical link information.
757 * Pointer to Ethernet device structure.
758 * @param wait_to_complete
759 * Wait for request completion (ignored).
762 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
764 struct priv *priv = mlx5_get_priv(dev);
765 struct ethtool_cmd edata = {
766 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
769 struct rte_eth_link dev_link;
772 /* priv_lock() is not taken to allow concurrent calls. */
774 (void)wait_to_complete;
775 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
776 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
779 memset(&dev_link, 0, sizeof(dev_link));
780 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
781 (ifr.ifr_flags & IFF_RUNNING));
782 ifr.ifr_data = (void *)&edata;
783 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
784 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
788 link_speed = ethtool_cmd_speed(&edata);
789 if (link_speed == -1)
790 dev_link.link_speed = 0;
792 dev_link.link_speed = link_speed;
793 priv->link_speed_capa = 0;
794 if (edata.supported & SUPPORTED_Autoneg)
795 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
796 if (edata.supported & (SUPPORTED_1000baseT_Full |
797 SUPPORTED_1000baseKX_Full))
798 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
799 if (edata.supported & SUPPORTED_10000baseKR_Full)
800 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
801 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
802 SUPPORTED_40000baseCR4_Full |
803 SUPPORTED_40000baseSR4_Full |
804 SUPPORTED_40000baseLR4_Full))
805 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
806 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
807 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
808 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
809 ETH_LINK_SPEED_FIXED);
810 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
811 /* Link status changed. */
812 dev->data->dev_link = dev_link;
815 /* Link status is still the same. */
820 * Retrieve physical link information (unlocked version using new ioctl).
823 * Pointer to Ethernet device structure.
824 * @param wait_to_complete
825 * Wait for request completion (ignored).
828 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
830 struct priv *priv = mlx5_get_priv(dev);
831 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
833 struct rte_eth_link dev_link;
836 (void)wait_to_complete;
837 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
838 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
841 memset(&dev_link, 0, sizeof(dev_link));
842 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
843 (ifr.ifr_flags & IFF_RUNNING));
844 ifr.ifr_data = (void *)&gcmd;
845 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
846 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
850 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
852 alignas(struct ethtool_link_settings)
853 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
854 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
855 struct ethtool_link_settings *ecmd = (void *)data;
858 ifr.ifr_data = (void *)ecmd;
859 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
860 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
864 dev_link.link_speed = ecmd->speed;
865 sc = ecmd->link_mode_masks[0] |
866 ((uint64_t)ecmd->link_mode_masks[1] << 32);
867 priv->link_speed_capa = 0;
868 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
869 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
870 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
871 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
872 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
873 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
874 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
875 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
876 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
877 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
878 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
879 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
880 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
881 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
882 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
883 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
884 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
885 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
886 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
887 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
888 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
889 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
890 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
891 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
892 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
893 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
894 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
895 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
896 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
897 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
898 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
899 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
900 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
901 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
902 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
903 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
904 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
905 ETH_LINK_SPEED_FIXED);
906 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
907 /* Link status changed. */
908 dev->data->dev_link = dev_link;
911 /* Link status is still the same. */
916 * DPDK callback to retrieve physical link information.
919 * Pointer to Ethernet device structure.
920 * @param wait_to_complete
921 * Wait for request completion (ignored).
924 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
926 struct utsname utsname;
929 if (uname(&utsname) == -1 ||
930 sscanf(utsname.release, "%d.%d.%d",
931 &ver[0], &ver[1], &ver[2]) != 3 ||
932 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
933 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
934 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
938 * DPDK callback to change the MTU.
941 * Pointer to Ethernet device structure.
946 * 0 on success, negative errno value on failure.
949 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
951 struct priv *priv = dev->data->dev_private;
955 if (mlx5_is_secondary())
956 return -E_RTE_SECONDARY;
959 ret = priv_get_mtu(priv, &kern_mtu);
962 /* Set kernel interface MTU first. */
963 ret = priv_set_mtu(priv, mtu);
966 ret = priv_get_mtu(priv, &kern_mtu);
969 if (kern_mtu == mtu) {
971 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
977 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
985 * DPDK callback to get flow control status.
988 * Pointer to Ethernet device structure.
989 * @param[out] fc_conf
990 * Flow control output buffer.
993 * 0 on success, negative errno value on failure.
996 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
998 struct priv *priv = dev->data->dev_private;
1000 struct ethtool_pauseparam ethpause = {
1001 .cmd = ETHTOOL_GPAUSEPARAM
1005 if (mlx5_is_secondary())
1006 return -E_RTE_SECONDARY;
1008 ifr.ifr_data = (void *)ðpause;
1010 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1012 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1018 fc_conf->autoneg = ethpause.autoneg;
1019 if (ethpause.rx_pause && ethpause.tx_pause)
1020 fc_conf->mode = RTE_FC_FULL;
1021 else if (ethpause.rx_pause)
1022 fc_conf->mode = RTE_FC_RX_PAUSE;
1023 else if (ethpause.tx_pause)
1024 fc_conf->mode = RTE_FC_TX_PAUSE;
1026 fc_conf->mode = RTE_FC_NONE;
1036 * DPDK callback to modify flow control parameters.
1039 * Pointer to Ethernet device structure.
1040 * @param[in] fc_conf
1041 * Flow control parameters.
1044 * 0 on success, negative errno value on failure.
1047 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1049 struct priv *priv = dev->data->dev_private;
1051 struct ethtool_pauseparam ethpause = {
1052 .cmd = ETHTOOL_SPAUSEPARAM
1056 if (mlx5_is_secondary())
1057 return -E_RTE_SECONDARY;
1059 ifr.ifr_data = (void *)ðpause;
1060 ethpause.autoneg = fc_conf->autoneg;
1061 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1062 (fc_conf->mode & RTE_FC_RX_PAUSE))
1063 ethpause.rx_pause = 1;
1065 ethpause.rx_pause = 0;
1067 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1068 (fc_conf->mode & RTE_FC_TX_PAUSE))
1069 ethpause.tx_pause = 1;
1071 ethpause.tx_pause = 0;
1074 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1076 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1090 * Get PCI information from struct ibv_device.
1093 * Pointer to Ethernet device structure.
1094 * @param[out] pci_addr
1095 * PCI bus address output buffer.
1098 * 0 on success, -1 on failure and errno is set.
1101 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1102 struct rte_pci_addr *pci_addr)
1106 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1108 file = fopen(path, "rb");
1111 while (fgets(line, sizeof(line), file) == line) {
1112 size_t len = strlen(line);
1115 /* Truncate long lines. */
1116 if (len == (sizeof(line) - 1))
1117 while (line[(len - 1)] != '\n') {
1121 line[(len - 1)] = ret;
1123 /* Extract information. */
1126 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1130 &pci_addr->function) == 4) {
1140 * Update the link status.
1143 * Pointer to private structure.
1146 * Zero if the callback process can be called immediately.
1149 priv_link_status_update(struct priv *priv)
1151 struct rte_eth_link *link = &priv->dev->data->dev_link;
1153 mlx5_link_update(priv->dev, 0);
1154 if (((link->link_speed == 0) && link->link_status) ||
1155 ((link->link_speed != 0) && !link->link_status)) {
1157 * Inconsistent status. Event likely occurred before the
1158 * kernel netdevice exposes the new status.
1160 if (!priv->pending_alarm) {
1161 priv->pending_alarm = 1;
1162 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1163 mlx5_dev_link_status_handler,
1167 } else if (unlikely(priv->pending_alarm)) {
1168 /* Link interrupt occurred while alarm is already scheduled. */
1169 priv->pending_alarm = 0;
1170 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1176 * Device status handler.
1179 * Pointer to private structure.
1181 * Pointer to event flags holder.
1184 * Events bitmap of callback process which can be called immediately.
1187 priv_dev_status_handler(struct priv *priv)
1189 struct ibv_async_event event;
1192 /* Read all message and acknowledge them. */
1194 if (ibv_get_async_event(priv->ctx, &event))
1196 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1197 event.event_type == IBV_EVENT_PORT_ERR) &&
1198 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1199 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1200 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1201 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1202 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1204 DEBUG("event type %d on port %d not handled",
1205 event.event_type, event.element.port_num);
1206 ibv_ack_async_event(&event);
1208 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1209 if (priv_link_status_update(priv))
1210 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1215 * Handle delayed link status event.
1218 * Registered argument.
1221 mlx5_dev_link_status_handler(void *arg)
1223 struct rte_eth_dev *dev = arg;
1224 struct priv *priv = dev->data->dev_private;
1228 assert(priv->pending_alarm == 1);
1229 priv->pending_alarm = 0;
1230 ret = priv_link_status_update(priv);
1233 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1238 * Handle interrupts from the NIC.
1240 * @param[in] intr_handle
1241 * Interrupt handler.
1243 * Callback argument.
1246 mlx5_dev_interrupt_handler(void *cb_arg)
1248 struct rte_eth_dev *dev = cb_arg;
1249 struct priv *priv = dev->data->dev_private;
1253 events = priv_dev_status_handler(priv);
1255 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1256 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1258 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1259 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL,
1264 * Handle interrupts from the socket.
1267 * Callback argument.
1270 mlx5_dev_handler_socket(void *cb_arg)
1272 struct rte_eth_dev *dev = cb_arg;
1273 struct priv *priv = dev->data->dev_private;
1276 priv_socket_handle(priv);
1281 * Uninstall interrupt handler.
1284 * Pointer to private structure.
1286 * Pointer to the rte_eth_dev structure.
1289 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1291 if (dev->data->dev_conf.intr_conf.lsc ||
1292 dev->data->dev_conf.intr_conf.rmv)
1293 rte_intr_callback_unregister(&priv->intr_handle,
1294 mlx5_dev_interrupt_handler, dev);
1295 if (priv->primary_socket)
1296 rte_intr_callback_unregister(&priv->intr_handle_socket,
1297 mlx5_dev_handler_socket, dev);
1298 if (priv->pending_alarm)
1299 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1300 priv->pending_alarm = 0;
1301 priv->intr_handle.fd = 0;
1302 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1303 priv->intr_handle_socket.fd = 0;
1304 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1308 * Install interrupt handler.
1311 * Pointer to private structure.
1313 * Pointer to the rte_eth_dev structure.
1316 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1320 assert(!mlx5_is_secondary());
1321 assert(priv->ctx->async_fd > 0);
1322 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1323 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1325 INFO("failed to change file descriptor async event queue");
1326 dev->data->dev_conf.intr_conf.lsc = 0;
1327 dev->data->dev_conf.intr_conf.rmv = 0;
1329 if (dev->data->dev_conf.intr_conf.lsc ||
1330 dev->data->dev_conf.intr_conf.rmv) {
1331 priv->intr_handle.fd = priv->ctx->async_fd;
1332 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1333 rte_intr_callback_register(&priv->intr_handle,
1334 mlx5_dev_interrupt_handler, dev);
1337 rc = priv_socket_init(priv);
1338 if (!rc && priv->primary_socket) {
1339 priv->intr_handle_socket.fd = priv->primary_socket;
1340 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1341 rte_intr_callback_register(&priv->intr_handle_socket,
1342 mlx5_dev_handler_socket, dev);
1347 * Change the link state (UP / DOWN).
1350 * Pointer to private data structure.
1352 * Pointer to rte_eth_dev structure.
1354 * Nonzero for link up, otherwise link down.
1357 * 0 on success, errno value on failure.
1360 priv_dev_set_link(struct priv *priv, struct rte_eth_dev *dev, int up)
1365 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1368 priv_dev_select_tx_function(priv, dev);
1369 priv_dev_select_rx_function(priv, dev);
1371 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1374 dev->rx_pkt_burst = removed_rx_burst;
1375 dev->tx_pkt_burst = removed_tx_burst;
1381 * DPDK callback to bring the link DOWN.
1384 * Pointer to Ethernet device structure.
1387 * 0 on success, errno value on failure.
1390 mlx5_set_link_down(struct rte_eth_dev *dev)
1392 struct priv *priv = dev->data->dev_private;
1396 err = priv_dev_set_link(priv, dev, 0);
1402 * DPDK callback to bring the link UP.
1405 * Pointer to Ethernet device structure.
1408 * 0 on success, errno value on failure.
1411 mlx5_set_link_up(struct rte_eth_dev *dev)
1413 struct priv *priv = dev->data->dev_private;
1417 err = priv_dev_set_link(priv, dev, 1);
1423 * Configure the TX function to use.
1426 * Pointer to private data structure.
1428 * Pointer to rte_eth_dev structure.
1431 priv_dev_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1433 assert(priv != NULL);
1434 assert(dev != NULL);
1435 dev->tx_pkt_burst = mlx5_tx_burst;
1436 /* Select appropriate TX function. */
1437 if (priv->mps == MLX5_MPW_ENHANCED) {
1438 if (priv_check_vec_tx_support(priv) > 0) {
1439 if (priv_check_raw_vec_tx_support(priv) > 0)
1440 dev->tx_pkt_burst = mlx5_tx_burst_raw_vec;
1442 dev->tx_pkt_burst = mlx5_tx_burst_vec;
1443 DEBUG("selected Enhanced MPW TX vectorized function");
1445 dev->tx_pkt_burst = mlx5_tx_burst_empw;
1446 DEBUG("selected Enhanced MPW TX function");
1448 } else if (priv->mps && priv->txq_inline) {
1449 dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1450 DEBUG("selected MPW inline TX function");
1451 } else if (priv->mps) {
1452 dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1453 DEBUG("selected MPW TX function");
1458 * Configure the RX function to use.
1461 * Pointer to private data structure.
1463 * Pointer to rte_eth_dev structure.
1466 priv_dev_select_rx_function(struct priv *priv, struct rte_eth_dev *dev)
1468 assert(priv != NULL);
1469 assert(dev != NULL);
1470 if (priv_check_vec_rx_support(priv) > 0) {
1471 dev->rx_pkt_burst = mlx5_rx_burst_vec;
1472 DEBUG("selected RX vectorized function");
1474 dev->rx_pkt_burst = mlx5_rx_burst;