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>
60 #include <rte_common.h>
61 #include <rte_interrupts.h>
62 #include <rte_alarm.h>
63 #include <rte_malloc.h>
66 #include "mlx5_rxtx.h"
67 #include "mlx5_utils.h"
69 /* Add defines in case the running kernel is not the same as user headers. */
70 #ifndef ETHTOOL_GLINKSETTINGS
71 struct ethtool_link_settings {
80 uint8_t eth_tp_mdix_ctrl;
81 int8_t link_mode_masks_nwords;
83 uint32_t link_mode_masks[];
86 #define ETHTOOL_GLINKSETTINGS 0x0000004c
87 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
88 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
89 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
90 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
91 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
92 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
93 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
94 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
95 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
96 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
97 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
98 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
99 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
100 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
101 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
102 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
104 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
105 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
106 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
107 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
109 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
110 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
111 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
113 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
114 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
115 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
116 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
117 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
121 * Return private structure associated with an Ethernet device.
124 * Pointer to Ethernet device structure.
127 * Pointer to private structure.
130 mlx5_get_priv(struct rte_eth_dev *dev)
132 return dev->data->dev_private;
136 * Check if running as a secondary process.
139 * Nonzero if running as a secondary process.
142 mlx5_is_secondary(void)
144 return rte_eal_process_type() == RTE_PROC_SECONDARY;
148 * Get interface name from private structure.
151 * Pointer to private structure.
153 * Interface name output buffer.
156 * 0 on success, -1 on failure and errno is set.
159 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
163 unsigned int dev_type = 0;
164 unsigned int dev_port_prev = ~0u;
165 char match[IF_NAMESIZE] = "";
168 MKSTR(path, "%s/device/net", priv->ibdev_path);
174 while ((dent = readdir(dir)) != NULL) {
175 char *name = dent->d_name;
177 unsigned int dev_port;
180 if ((name[0] == '.') &&
181 ((name[1] == '\0') ||
182 ((name[1] == '.') && (name[2] == '\0'))))
185 MKSTR(path, "%s/device/net/%s/%s",
186 priv->ibdev_path, name,
187 (dev_type ? "dev_id" : "dev_port"));
189 file = fopen(path, "rb");
194 * Switch to dev_id when dev_port does not exist as
195 * is the case with Linux kernel versions < 3.15.
206 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
211 * Switch to dev_id when dev_port returns the same value for
212 * all ports. May happen when using a MOFED release older than
213 * 3.0 with a Linux kernel >= 3.15.
215 if (dev_port == dev_port_prev)
217 dev_port_prev = dev_port;
218 if (dev_port == (priv->port - 1u))
219 snprintf(match, sizeof(match), "%s", name);
222 if (match[0] == '\0')
224 strncpy(*ifname, match, sizeof(*ifname));
229 * Check if the counter is located on ib counters file.
235 * 1 if counter is located on ib counters file , 0 otherwise.
238 priv_is_ib_cntr(const char *cntr)
240 if (!strcmp(cntr, "out_of_buffer"))
246 * Read from sysfs entry.
249 * Pointer to private structure.
251 * Entry name relative to sysfs path.
253 * Data output buffer.
258 * 0 on success, -1 on failure and errno is set.
261 priv_sysfs_read(const struct priv *priv, const char *entry,
262 char *buf, size_t size)
264 char ifname[IF_NAMESIZE];
269 if (priv_get_ifname(priv, &ifname))
272 if (priv_is_ib_cntr(entry)) {
273 MKSTR(path, "%s/ports/1/hw_counters/%s",
274 priv->ibdev_path, entry);
275 file = fopen(path, "rb");
277 MKSTR(path, "%s/device/net/%s/%s",
278 priv->ibdev_path, ifname, entry);
279 file = fopen(path, "rb");
283 ret = fread(buf, 1, size, file);
285 if (((size_t)ret < size) && (ferror(file)))
295 * Write to sysfs entry.
298 * Pointer to private structure.
300 * Entry name relative to sysfs path.
307 * 0 on success, -1 on failure and errno is set.
310 priv_sysfs_write(const struct priv *priv, const char *entry,
311 char *buf, size_t size)
313 char ifname[IF_NAMESIZE];
318 if (priv_get_ifname(priv, &ifname))
321 MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
323 file = fopen(path, "wb");
326 ret = fwrite(buf, 1, size, file);
328 if (((size_t)ret < size) || (ferror(file)))
338 * Get unsigned long sysfs property.
341 * Pointer to private structure.
343 * Entry name relative to sysfs path.
345 * Value output buffer.
348 * 0 on success, -1 on failure and errno is set.
351 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
354 unsigned long value_ret;
357 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
359 DEBUG("cannot read %s value from sysfs: %s",
360 name, strerror(errno));
363 value_str[ret] = '\0';
365 value_ret = strtoul(value_str, NULL, 0);
367 DEBUG("invalid %s value `%s': %s", name, value_str,
376 * Set unsigned long sysfs property.
379 * Pointer to private structure.
381 * Entry name relative to sysfs path.
386 * 0 on success, -1 on failure and errno is set.
389 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
392 MKSTR(value_str, "%lu", value);
394 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
396 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
397 name, value_str, value, strerror(errno));
404 * Perform ifreq ioctl() on associated Ethernet device.
407 * Pointer to private structure.
409 * Request number to pass to ioctl().
411 * Interface request structure output buffer.
414 * 0 on success, -1 on failure and errno is set.
417 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
419 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
424 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
425 ret = ioctl(sock, req, ifr);
431 * Return the number of active VFs for the current device.
434 * Pointer to private structure.
435 * @param[out] num_vfs
436 * Number of active VFs.
439 * 0 on success, -1 on failure and errno is set.
442 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
444 /* The sysfs entry name depends on the operating system. */
445 const char **name = (const char *[]){
446 "device/sriov_numvfs",
447 "device/mlx5_num_vfs",
453 unsigned long ulong_num_vfs;
455 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
457 *num_vfs = ulong_num_vfs;
458 } while (*(++name) && ret);
466 * Pointer to private structure.
468 * MTU value output buffer.
471 * 0 on success, -1 on failure and errno is set.
474 priv_get_mtu(struct priv *priv, uint16_t *mtu)
476 unsigned long ulong_mtu;
478 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
485 * Read device counter from sysfs.
488 * Pointer to private structure.
492 * Counter output buffer.
495 * 0 on success, -1 on failure and errno is set.
498 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
500 unsigned long ulong_ctr;
502 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
512 * Pointer to private structure.
517 * 0 on success, -1 on failure and errno is set.
520 priv_set_mtu(struct priv *priv, uint16_t mtu)
524 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
525 priv_get_mtu(priv, &new_mtu))
537 * Pointer to private structure.
539 * Bitmask for flags that must remain untouched.
541 * Bitmask for flags to modify.
544 * 0 on success, -1 on failure and errno is set.
547 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
551 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
554 tmp |= (flags & (~keep));
555 return priv_set_sysfs_ulong(priv, "flags", tmp);
559 * Ethernet device configuration.
561 * Prepare the driver for a given number of TX and RX queues.
564 * Pointer to Ethernet device structure.
567 * 0 on success, errno value on failure.
570 dev_configure(struct rte_eth_dev *dev)
572 struct priv *priv = dev->data->dev_private;
573 unsigned int rxqs_n = dev->data->nb_rx_queues;
574 unsigned int txqs_n = dev->data->nb_tx_queues;
577 unsigned int reta_idx_n;
578 const uint8_t use_app_rss_key =
579 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len;
581 if (use_app_rss_key &&
582 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
583 rss_hash_default_key_len)) {
584 /* MLX5 RSS only support 40bytes key. */
587 priv->rss_conf.rss_key =
588 rte_realloc(priv->rss_conf.rss_key,
589 rss_hash_default_key_len, 0);
590 if (!priv->rss_conf.rss_key) {
591 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
594 memcpy(priv->rss_conf.rss_key,
596 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
597 rss_hash_default_key,
598 rss_hash_default_key_len);
599 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
600 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
601 priv->rxqs = (void *)dev->data->rx_queues;
602 priv->txqs = (void *)dev->data->tx_queues;
603 if (txqs_n != priv->txqs_n) {
604 INFO("%p: TX queues number update: %u -> %u",
605 (void *)dev, priv->txqs_n, txqs_n);
606 priv->txqs_n = txqs_n;
608 if (rxqs_n > priv->ind_table_max_size) {
609 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
612 if (rxqs_n == priv->rxqs_n)
614 INFO("%p: RX queues number update: %u -> %u",
615 (void *)dev, priv->rxqs_n, rxqs_n);
616 priv->rxqs_n = rxqs_n;
617 /* If the requested number of RX queues is not a power of two, use the
618 * maximum indirection table size for better balancing.
619 * The result is always rounded to the next power of two. */
620 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
621 priv->ind_table_max_size :
623 if (priv_rss_reta_index_resize(priv, reta_idx_n))
625 /* When the number of RX queues is not a power of two, the remaining
626 * table entries are padded with reused WQs and hashes are not spread
628 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
629 (*priv->reta_idx)[i] = j;
637 * DPDK callback for Ethernet device configuration.
640 * Pointer to Ethernet device structure.
643 * 0 on success, negative errno value on failure.
646 mlx5_dev_configure(struct rte_eth_dev *dev)
648 struct priv *priv = dev->data->dev_private;
651 if (mlx5_is_secondary())
652 return -E_RTE_SECONDARY;
655 ret = dev_configure(dev);
662 * DPDK callback to get information about the device.
665 * Pointer to Ethernet device structure.
667 * Info structure output buffer.
670 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
672 struct priv *priv = mlx5_get_priv(dev);
674 char ifname[IF_NAMESIZE];
676 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
679 /* FIXME: we should ask the device for these values. */
680 info->min_rx_bufsize = 32;
681 info->max_rx_pktlen = 65536;
683 * Since we need one CQ per QP, the limit is the minimum number
684 * between the two values.
686 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
687 priv->device_attr.orig_attr.max_qp);
688 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
691 info->max_rx_queues = max;
692 info->max_tx_queues = max;
693 info->max_mac_addrs = RTE_DIM(priv->mac);
694 info->rx_offload_capa =
696 (DEV_RX_OFFLOAD_IPV4_CKSUM |
697 DEV_RX_OFFLOAD_UDP_CKSUM |
698 DEV_RX_OFFLOAD_TCP_CKSUM) :
700 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0) |
701 DEV_RX_OFFLOAD_TIMESTAMP;
704 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
706 info->tx_offload_capa |=
707 (DEV_TX_OFFLOAD_IPV4_CKSUM |
708 DEV_TX_OFFLOAD_UDP_CKSUM |
709 DEV_TX_OFFLOAD_TCP_CKSUM);
711 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
713 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
714 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
715 DEV_TX_OFFLOAD_GRE_TNL_TSO);
716 if (priv_get_ifname(priv, &ifname) == 0)
717 info->if_index = if_nametoindex(ifname);
718 info->reta_size = priv->reta_idx_n ?
719 priv->reta_idx_n : priv->ind_table_max_size;
720 info->hash_key_size = priv->rss_conf.rss_key_len;
721 info->speed_capa = priv->link_speed_capa;
726 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
728 static const uint32_t ptypes[] = {
729 /* refers to rxq_cq_to_pkt_type() */
731 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
732 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
733 RTE_PTYPE_L4_NONFRAG,
737 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
738 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
739 RTE_PTYPE_INNER_L4_NONFRAG,
740 RTE_PTYPE_INNER_L4_FRAG,
741 RTE_PTYPE_INNER_L4_TCP,
742 RTE_PTYPE_INNER_L4_UDP,
746 if (dev->rx_pkt_burst == mlx5_rx_burst ||
747 dev->rx_pkt_burst == mlx5_rx_burst_vec)
753 * DPDK callback to retrieve physical link information.
756 * Pointer to Ethernet device structure.
757 * @param wait_to_complete
758 * Wait for request completion (ignored).
761 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
763 struct priv *priv = mlx5_get_priv(dev);
764 struct ethtool_cmd edata = {
765 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
768 struct rte_eth_link dev_link;
771 /* priv_lock() is not taken to allow concurrent calls. */
773 (void)wait_to_complete;
774 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
775 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
778 memset(&dev_link, 0, sizeof(dev_link));
779 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
780 (ifr.ifr_flags & IFF_RUNNING));
781 ifr.ifr_data = (void *)&edata;
782 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
783 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
787 link_speed = ethtool_cmd_speed(&edata);
788 if (link_speed == -1)
789 dev_link.link_speed = 0;
791 dev_link.link_speed = link_speed;
792 priv->link_speed_capa = 0;
793 if (edata.supported & SUPPORTED_Autoneg)
794 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
795 if (edata.supported & (SUPPORTED_1000baseT_Full |
796 SUPPORTED_1000baseKX_Full))
797 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
798 if (edata.supported & SUPPORTED_10000baseKR_Full)
799 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
800 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
801 SUPPORTED_40000baseCR4_Full |
802 SUPPORTED_40000baseSR4_Full |
803 SUPPORTED_40000baseLR4_Full))
804 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
805 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
806 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
807 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
808 ETH_LINK_SPEED_FIXED);
809 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
810 /* Link status changed. */
811 dev->data->dev_link = dev_link;
814 /* Link status is still the same. */
819 * Retrieve physical link information (unlocked version using new ioctl).
822 * Pointer to Ethernet device structure.
823 * @param wait_to_complete
824 * Wait for request completion (ignored).
827 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
829 struct priv *priv = mlx5_get_priv(dev);
830 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
832 struct rte_eth_link dev_link;
835 (void)wait_to_complete;
836 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
837 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
840 memset(&dev_link, 0, sizeof(dev_link));
841 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
842 (ifr.ifr_flags & IFF_RUNNING));
843 ifr.ifr_data = (void *)&gcmd;
844 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
845 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
849 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
851 alignas(struct ethtool_link_settings)
852 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
853 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
854 struct ethtool_link_settings *ecmd = (void *)data;
857 ifr.ifr_data = (void *)ecmd;
858 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
859 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
863 dev_link.link_speed = ecmd->speed;
864 sc = ecmd->link_mode_masks[0] |
865 ((uint64_t)ecmd->link_mode_masks[1] << 32);
866 priv->link_speed_capa = 0;
867 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
868 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
869 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
870 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
871 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
872 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
873 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
874 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
875 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
876 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
877 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
878 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
879 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
880 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
881 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
882 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
883 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
884 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
885 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
886 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
887 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
888 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
889 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
890 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
891 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
892 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
893 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
894 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
895 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
896 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
897 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
898 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
899 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
900 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
901 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
902 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
903 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
904 ETH_LINK_SPEED_FIXED);
905 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
906 /* Link status changed. */
907 dev->data->dev_link = dev_link;
910 /* Link status is still the same. */
915 * DPDK callback to retrieve physical link information.
918 * Pointer to Ethernet device structure.
919 * @param wait_to_complete
920 * Wait for request completion (ignored).
923 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
925 struct utsname utsname;
928 if (uname(&utsname) == -1 ||
929 sscanf(utsname.release, "%d.%d.%d",
930 &ver[0], &ver[1], &ver[2]) != 3 ||
931 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
932 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
933 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
937 * DPDK callback to change the MTU.
940 * Pointer to Ethernet device structure.
945 * 0 on success, negative errno value on failure.
948 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
950 struct priv *priv = dev->data->dev_private;
954 if (mlx5_is_secondary())
955 return -E_RTE_SECONDARY;
958 ret = priv_get_mtu(priv, &kern_mtu);
961 /* Set kernel interface MTU first. */
962 ret = priv_set_mtu(priv, mtu);
965 ret = priv_get_mtu(priv, &kern_mtu);
968 if (kern_mtu == mtu) {
970 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
976 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
984 * DPDK callback to get flow control status.
987 * Pointer to Ethernet device structure.
988 * @param[out] fc_conf
989 * Flow control output buffer.
992 * 0 on success, negative errno value on failure.
995 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
997 struct priv *priv = dev->data->dev_private;
999 struct ethtool_pauseparam ethpause = {
1000 .cmd = ETHTOOL_GPAUSEPARAM
1004 if (mlx5_is_secondary())
1005 return -E_RTE_SECONDARY;
1007 ifr.ifr_data = (void *)ðpause;
1009 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1011 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1017 fc_conf->autoneg = ethpause.autoneg;
1018 if (ethpause.rx_pause && ethpause.tx_pause)
1019 fc_conf->mode = RTE_FC_FULL;
1020 else if (ethpause.rx_pause)
1021 fc_conf->mode = RTE_FC_RX_PAUSE;
1022 else if (ethpause.tx_pause)
1023 fc_conf->mode = RTE_FC_TX_PAUSE;
1025 fc_conf->mode = RTE_FC_NONE;
1035 * DPDK callback to modify flow control parameters.
1038 * Pointer to Ethernet device structure.
1039 * @param[in] fc_conf
1040 * Flow control parameters.
1043 * 0 on success, negative errno value on failure.
1046 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1048 struct priv *priv = dev->data->dev_private;
1050 struct ethtool_pauseparam ethpause = {
1051 .cmd = ETHTOOL_SPAUSEPARAM
1055 if (mlx5_is_secondary())
1056 return -E_RTE_SECONDARY;
1058 ifr.ifr_data = (void *)ðpause;
1059 ethpause.autoneg = fc_conf->autoneg;
1060 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1061 (fc_conf->mode & RTE_FC_RX_PAUSE))
1062 ethpause.rx_pause = 1;
1064 ethpause.rx_pause = 0;
1066 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1067 (fc_conf->mode & RTE_FC_TX_PAUSE))
1068 ethpause.tx_pause = 1;
1070 ethpause.tx_pause = 0;
1073 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1075 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1089 * Get PCI information from struct ibv_device.
1092 * Pointer to Ethernet device structure.
1093 * @param[out] pci_addr
1094 * PCI bus address output buffer.
1097 * 0 on success, -1 on failure and errno is set.
1100 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1101 struct rte_pci_addr *pci_addr)
1105 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1107 file = fopen(path, "rb");
1110 while (fgets(line, sizeof(line), file) == line) {
1111 size_t len = strlen(line);
1114 /* Truncate long lines. */
1115 if (len == (sizeof(line) - 1))
1116 while (line[(len - 1)] != '\n') {
1120 line[(len - 1)] = ret;
1122 /* Extract information. */
1125 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1129 &pci_addr->function) == 4) {
1139 * Update the link status.
1142 * Pointer to private structure.
1145 * Zero if the callback process can be called immediately.
1148 priv_link_status_update(struct priv *priv)
1150 struct rte_eth_link *link = &priv->dev->data->dev_link;
1152 mlx5_link_update(priv->dev, 0);
1153 if (((link->link_speed == 0) && link->link_status) ||
1154 ((link->link_speed != 0) && !link->link_status)) {
1156 * Inconsistent status. Event likely occurred before the
1157 * kernel netdevice exposes the new status.
1159 if (!priv->pending_alarm) {
1160 priv->pending_alarm = 1;
1161 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1162 mlx5_dev_link_status_handler,
1166 } else if (unlikely(priv->pending_alarm)) {
1167 /* Link interrupt occurred while alarm is already scheduled. */
1168 priv->pending_alarm = 0;
1169 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1175 * Device status handler.
1178 * Pointer to private structure.
1180 * Pointer to event flags holder.
1183 * Events bitmap of callback process which can be called immediately.
1186 priv_dev_status_handler(struct priv *priv)
1188 struct ibv_async_event event;
1191 /* Read all message and acknowledge them. */
1193 if (ibv_get_async_event(priv->ctx, &event))
1195 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1196 event.event_type == IBV_EVENT_PORT_ERR) &&
1197 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1198 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1199 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1200 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1201 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1203 DEBUG("event type %d on port %d not handled",
1204 event.event_type, event.element.port_num);
1205 ibv_ack_async_event(&event);
1207 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1208 if (priv_link_status_update(priv))
1209 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1214 * Handle delayed link status event.
1217 * Registered argument.
1220 mlx5_dev_link_status_handler(void *arg)
1222 struct rte_eth_dev *dev = arg;
1223 struct priv *priv = dev->data->dev_private;
1227 assert(priv->pending_alarm == 1);
1228 priv->pending_alarm = 0;
1229 ret = priv_link_status_update(priv);
1232 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1237 * Handle interrupts from the NIC.
1239 * @param[in] intr_handle
1240 * Interrupt handler.
1242 * Callback argument.
1245 mlx5_dev_interrupt_handler(void *cb_arg)
1247 struct rte_eth_dev *dev = cb_arg;
1248 struct priv *priv = dev->data->dev_private;
1252 events = priv_dev_status_handler(priv);
1254 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1255 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1257 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1258 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL,
1263 * Handle interrupts from the socket.
1266 * Callback argument.
1269 mlx5_dev_handler_socket(void *cb_arg)
1271 struct rte_eth_dev *dev = cb_arg;
1272 struct priv *priv = dev->data->dev_private;
1275 priv_socket_handle(priv);
1280 * Uninstall interrupt handler.
1283 * Pointer to private structure.
1285 * Pointer to the rte_eth_dev structure.
1288 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1290 if (dev->data->dev_conf.intr_conf.lsc ||
1291 dev->data->dev_conf.intr_conf.rmv)
1292 rte_intr_callback_unregister(&priv->intr_handle,
1293 mlx5_dev_interrupt_handler, dev);
1294 if (priv->primary_socket)
1295 rte_intr_callback_unregister(&priv->intr_handle_socket,
1296 mlx5_dev_handler_socket, dev);
1297 if (priv->pending_alarm)
1298 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1299 priv->pending_alarm = 0;
1300 priv->intr_handle.fd = 0;
1301 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1302 priv->intr_handle_socket.fd = 0;
1303 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1307 * Install interrupt handler.
1310 * Pointer to private structure.
1312 * Pointer to the rte_eth_dev structure.
1315 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1319 assert(!mlx5_is_secondary());
1320 assert(priv->ctx->async_fd > 0);
1321 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1322 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1324 INFO("failed to change file descriptor async event queue");
1325 dev->data->dev_conf.intr_conf.lsc = 0;
1326 dev->data->dev_conf.intr_conf.rmv = 0;
1328 if (dev->data->dev_conf.intr_conf.lsc ||
1329 dev->data->dev_conf.intr_conf.rmv) {
1330 priv->intr_handle.fd = priv->ctx->async_fd;
1331 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1332 rte_intr_callback_register(&priv->intr_handle,
1333 mlx5_dev_interrupt_handler, dev);
1336 rc = priv_socket_init(priv);
1337 if (!rc && priv->primary_socket) {
1338 priv->intr_handle_socket.fd = priv->primary_socket;
1339 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1340 rte_intr_callback_register(&priv->intr_handle_socket,
1341 mlx5_dev_handler_socket, dev);
1346 * Change the link state (UP / DOWN).
1349 * Pointer to private data structure.
1351 * Pointer to rte_eth_dev structure.
1353 * Nonzero for link up, otherwise link down.
1356 * 0 on success, errno value on failure.
1359 priv_dev_set_link(struct priv *priv, struct rte_eth_dev *dev, int up)
1364 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1367 priv_dev_select_tx_function(priv, dev);
1368 priv_dev_select_rx_function(priv, dev);
1370 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1373 dev->rx_pkt_burst = removed_rx_burst;
1374 dev->tx_pkt_burst = removed_tx_burst;
1380 * DPDK callback to bring the link DOWN.
1383 * Pointer to Ethernet device structure.
1386 * 0 on success, errno value on failure.
1389 mlx5_set_link_down(struct rte_eth_dev *dev)
1391 struct priv *priv = dev->data->dev_private;
1395 err = priv_dev_set_link(priv, dev, 0);
1401 * DPDK callback to bring the link UP.
1404 * Pointer to Ethernet device structure.
1407 * 0 on success, errno value on failure.
1410 mlx5_set_link_up(struct rte_eth_dev *dev)
1412 struct priv *priv = dev->data->dev_private;
1416 err = priv_dev_set_link(priv, dev, 1);
1422 * Configure the TX function to use.
1425 * Pointer to private data structure.
1427 * Pointer to rte_eth_dev structure.
1430 priv_dev_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1432 assert(priv != NULL);
1433 assert(dev != NULL);
1434 dev->tx_pkt_burst = mlx5_tx_burst;
1435 /* Select appropriate TX function. */
1436 if (priv->mps == MLX5_MPW_ENHANCED) {
1437 if (priv_check_vec_tx_support(priv) > 0) {
1438 if (priv_check_raw_vec_tx_support(priv) > 0)
1439 dev->tx_pkt_burst = mlx5_tx_burst_raw_vec;
1441 dev->tx_pkt_burst = mlx5_tx_burst_vec;
1442 DEBUG("selected Enhanced MPW TX vectorized function");
1444 dev->tx_pkt_burst = mlx5_tx_burst_empw;
1445 DEBUG("selected Enhanced MPW TX function");
1447 } else if (priv->mps && priv->txq_inline) {
1448 dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1449 DEBUG("selected MPW inline TX function");
1450 } else if (priv->mps) {
1451 dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1452 DEBUG("selected MPW TX function");
1457 * Configure the RX function to use.
1460 * Pointer to private data structure.
1462 * Pointer to rte_eth_dev structure.
1465 priv_dev_select_rx_function(struct priv *priv, struct rte_eth_dev *dev)
1467 assert(priv != NULL);
1468 assert(dev != NULL);
1469 if (priv_check_vec_rx_support(priv) > 0) {
1470 dev->rx_pkt_burst = mlx5_rx_burst_vec;
1471 DEBUG("selected RX vectorized function");
1473 dev->rx_pkt_burst = mlx5_rx_burst;