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_driver.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_glue.h"
68 #include "mlx5_rxtx.h"
69 #include "mlx5_utils.h"
71 /* Add defines in case the running kernel is not the same as user headers. */
72 #ifndef ETHTOOL_GLINKSETTINGS
73 struct ethtool_link_settings {
82 uint8_t eth_tp_mdix_ctrl;
83 int8_t link_mode_masks_nwords;
85 uint32_t link_mode_masks[];
88 #define ETHTOOL_GLINKSETTINGS 0x0000004c
89 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
90 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
91 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
92 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
93 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
94 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
95 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
96 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
97 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
98 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
99 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
100 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
101 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
102 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
103 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
104 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
106 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
107 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
108 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
109 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
111 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
112 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
113 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
115 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
116 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
117 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
118 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
119 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
123 * Get interface name from private structure.
126 * Pointer to private structure.
128 * Interface name output buffer.
131 * 0 on success, -1 on failure and errno is set.
134 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
138 unsigned int dev_type = 0;
139 unsigned int dev_port_prev = ~0u;
140 char match[IF_NAMESIZE] = "";
143 MKSTR(path, "%s/device/net", priv->ibdev_path);
149 while ((dent = readdir(dir)) != NULL) {
150 char *name = dent->d_name;
152 unsigned int dev_port;
155 if ((name[0] == '.') &&
156 ((name[1] == '\0') ||
157 ((name[1] == '.') && (name[2] == '\0'))))
160 MKSTR(path, "%s/device/net/%s/%s",
161 priv->ibdev_path, name,
162 (dev_type ? "dev_id" : "dev_port"));
164 file = fopen(path, "rb");
169 * Switch to dev_id when dev_port does not exist as
170 * is the case with Linux kernel versions < 3.15.
181 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
186 * Switch to dev_id when dev_port returns the same value for
187 * all ports. May happen when using a MOFED release older than
188 * 3.0 with a Linux kernel >= 3.15.
190 if (dev_port == dev_port_prev)
192 dev_port_prev = dev_port;
193 if (dev_port == (priv->port - 1u))
194 snprintf(match, sizeof(match), "%s", name);
197 if (match[0] == '\0')
199 strncpy(*ifname, match, sizeof(*ifname));
204 * Check if the counter is located on ib counters file.
210 * 1 if counter is located on ib counters file , 0 otherwise.
213 priv_is_ib_cntr(const char *cntr)
215 if (!strcmp(cntr, "out_of_buffer"))
221 * Read from sysfs entry.
224 * Pointer to private structure.
226 * Entry name relative to sysfs path.
228 * Data output buffer.
233 * 0 on success, -1 on failure and errno is set.
236 priv_sysfs_read(const struct priv *priv, const char *entry,
237 char *buf, size_t size)
239 char ifname[IF_NAMESIZE];
244 if (priv_get_ifname(priv, &ifname))
247 if (priv_is_ib_cntr(entry)) {
248 MKSTR(path, "%s/ports/1/hw_counters/%s",
249 priv->ibdev_path, entry);
250 file = fopen(path, "rb");
252 MKSTR(path, "%s/device/net/%s/%s",
253 priv->ibdev_path, ifname, entry);
254 file = fopen(path, "rb");
258 ret = fread(buf, 1, size, file);
260 if (((size_t)ret < size) && (ferror(file)))
270 * Write to sysfs entry.
273 * Pointer to private structure.
275 * Entry name relative to sysfs path.
282 * 0 on success, -1 on failure and errno is set.
285 priv_sysfs_write(const struct priv *priv, const char *entry,
286 char *buf, size_t size)
288 char ifname[IF_NAMESIZE];
293 if (priv_get_ifname(priv, &ifname))
296 MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
298 file = fopen(path, "wb");
301 ret = fwrite(buf, 1, size, file);
303 if (((size_t)ret < size) || (ferror(file)))
313 * Get unsigned long sysfs property.
316 * Pointer to private structure.
318 * Entry name relative to sysfs path.
320 * Value output buffer.
323 * 0 on success, -1 on failure and errno is set.
326 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
329 unsigned long value_ret;
332 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
334 DEBUG("cannot read %s value from sysfs: %s",
335 name, strerror(errno));
338 value_str[ret] = '\0';
340 value_ret = strtoul(value_str, NULL, 0);
342 DEBUG("invalid %s value `%s': %s", name, value_str,
351 * Set unsigned long sysfs property.
354 * Pointer to private structure.
356 * Entry name relative to sysfs path.
361 * 0 on success, -1 on failure and errno is set.
364 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
367 MKSTR(value_str, "%lu", value);
369 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
371 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
372 name, value_str, value, strerror(errno));
379 * Perform ifreq ioctl() on associated Ethernet device.
382 * Pointer to private structure.
384 * Request number to pass to ioctl().
386 * Interface request structure output buffer.
389 * 0 on success, -1 on failure and errno is set.
392 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
394 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
399 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
400 ret = ioctl(sock, req, ifr);
406 * Return the number of active VFs for the current device.
409 * Pointer to private structure.
410 * @param[out] num_vfs
411 * Number of active VFs.
414 * 0 on success, -1 on failure and errno is set.
417 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
419 /* The sysfs entry name depends on the operating system. */
420 const char **name = (const char *[]){
421 "device/sriov_numvfs",
422 "device/mlx5_num_vfs",
428 unsigned long ulong_num_vfs;
430 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
432 *num_vfs = ulong_num_vfs;
433 } while (*(++name) && ret);
441 * Pointer to private structure.
443 * MTU value output buffer.
446 * 0 on success, -1 on failure and errno is set.
449 priv_get_mtu(struct priv *priv, uint16_t *mtu)
451 unsigned long ulong_mtu;
453 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
460 * Read device counter from sysfs.
463 * Pointer to private structure.
467 * Counter output buffer.
470 * 0 on success, -1 on failure and errno is set.
473 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
475 unsigned long ulong_ctr;
477 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
487 * Pointer to private structure.
492 * 0 on success, -1 on failure and errno is set.
495 priv_set_mtu(struct priv *priv, uint16_t mtu)
499 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
500 priv_get_mtu(priv, &new_mtu))
512 * Pointer to private structure.
514 * Bitmask for flags that must remain untouched.
516 * Bitmask for flags to modify.
519 * 0 on success, -1 on failure and errno is set.
522 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
526 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
529 tmp |= (flags & (~keep));
530 return priv_set_sysfs_ulong(priv, "flags", tmp);
534 * Ethernet device configuration.
536 * Prepare the driver for a given number of TX and RX queues.
539 * Pointer to Ethernet device structure.
542 * 0 on success, errno value on failure.
545 dev_configure(struct rte_eth_dev *dev)
547 struct priv *priv = dev->data->dev_private;
548 unsigned int rxqs_n = dev->data->nb_rx_queues;
549 unsigned int txqs_n = dev->data->nb_tx_queues;
552 unsigned int reta_idx_n;
553 const uint8_t use_app_rss_key =
554 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
555 uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
556 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
557 uint64_t supp_rx_offloads =
558 (mlx5_priv_get_rx_port_offloads(priv) |
559 mlx5_priv_get_rx_queue_offloads(priv));
560 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
562 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
563 ERROR("Some Tx offloads are not supported "
564 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
565 tx_offloads, supp_tx_offloads);
568 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
569 ERROR("Some Rx offloads are not supported "
570 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
571 rx_offloads, supp_rx_offloads);
574 if (use_app_rss_key &&
575 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
576 rss_hash_default_key_len)) {
577 /* MLX5 RSS only support 40bytes key. */
580 priv->rss_conf.rss_key =
581 rte_realloc(priv->rss_conf.rss_key,
582 rss_hash_default_key_len, 0);
583 if (!priv->rss_conf.rss_key) {
584 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
587 memcpy(priv->rss_conf.rss_key,
589 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
590 rss_hash_default_key,
591 rss_hash_default_key_len);
592 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
593 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
594 priv->rxqs = (void *)dev->data->rx_queues;
595 priv->txqs = (void *)dev->data->tx_queues;
596 if (txqs_n != priv->txqs_n) {
597 INFO("%p: TX queues number update: %u -> %u",
598 (void *)dev, priv->txqs_n, txqs_n);
599 priv->txqs_n = txqs_n;
601 if (rxqs_n > priv->config.ind_table_max_size) {
602 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
605 if (rxqs_n == priv->rxqs_n)
607 INFO("%p: RX queues number update: %u -> %u",
608 (void *)dev, priv->rxqs_n, rxqs_n);
609 priv->rxqs_n = rxqs_n;
610 /* If the requested number of RX queues is not a power of two, use the
611 * maximum indirection table size for better balancing.
612 * The result is always rounded to the next power of two. */
613 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
614 priv->config.ind_table_max_size :
616 if (priv_rss_reta_index_resize(priv, reta_idx_n))
618 /* When the number of RX queues is not a power of two, the remaining
619 * table entries are padded with reused WQs and hashes are not spread
621 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
622 (*priv->reta_idx)[i] = j;
630 * DPDK callback for Ethernet device configuration.
633 * Pointer to Ethernet device structure.
636 * 0 on success, negative errno value on failure.
639 mlx5_dev_configure(struct rte_eth_dev *dev)
641 struct priv *priv = dev->data->dev_private;
645 ret = dev_configure(dev);
652 * DPDK callback to get information about the device.
655 * Pointer to Ethernet device structure.
657 * Info structure output buffer.
660 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
662 struct priv *priv = dev->data->dev_private;
663 struct mlx5_dev_config *config = &priv->config;
665 char ifname[IF_NAMESIZE];
667 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
670 /* FIXME: we should ask the device for these values. */
671 info->min_rx_bufsize = 32;
672 info->max_rx_pktlen = 65536;
674 * Since we need one CQ per QP, the limit is the minimum number
675 * between the two values.
677 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
678 priv->device_attr.orig_attr.max_qp);
679 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
682 info->max_rx_queues = max;
683 info->max_tx_queues = max;
684 info->max_mac_addrs = RTE_DIM(priv->mac);
685 info->rx_queue_offload_capa =
686 mlx5_priv_get_rx_queue_offloads(priv);
687 info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
688 info->rx_queue_offload_capa);
689 info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
690 if (priv_get_ifname(priv, &ifname) == 0)
691 info->if_index = if_nametoindex(ifname);
692 info->reta_size = priv->reta_idx_n ?
693 priv->reta_idx_n : config->ind_table_max_size;
694 info->hash_key_size = priv->rss_conf.rss_key_len;
695 info->speed_capa = priv->link_speed_capa;
696 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
701 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
703 static const uint32_t ptypes[] = {
704 /* refers to rxq_cq_to_pkt_type() */
706 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
707 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
708 RTE_PTYPE_L4_NONFRAG,
712 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
713 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
714 RTE_PTYPE_INNER_L4_NONFRAG,
715 RTE_PTYPE_INNER_L4_FRAG,
716 RTE_PTYPE_INNER_L4_TCP,
717 RTE_PTYPE_INNER_L4_UDP,
721 if (dev->rx_pkt_burst == mlx5_rx_burst ||
722 dev->rx_pkt_burst == mlx5_rx_burst_vec)
728 * DPDK callback to retrieve physical link information.
731 * Pointer to Ethernet device structure.
732 * @param wait_to_complete
733 * Wait for request completion (ignored).
736 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
738 struct priv *priv = dev->data->dev_private;
739 struct ethtool_cmd edata = {
740 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
743 struct rte_eth_link dev_link;
746 /* priv_lock() is not taken to allow concurrent calls. */
748 (void)wait_to_complete;
749 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
750 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
753 memset(&dev_link, 0, sizeof(dev_link));
754 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
755 (ifr.ifr_flags & IFF_RUNNING));
756 ifr.ifr_data = (void *)&edata;
757 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
758 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
762 link_speed = ethtool_cmd_speed(&edata);
763 if (link_speed == -1)
764 dev_link.link_speed = 0;
766 dev_link.link_speed = link_speed;
767 priv->link_speed_capa = 0;
768 if (edata.supported & SUPPORTED_Autoneg)
769 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
770 if (edata.supported & (SUPPORTED_1000baseT_Full |
771 SUPPORTED_1000baseKX_Full))
772 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
773 if (edata.supported & SUPPORTED_10000baseKR_Full)
774 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
775 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
776 SUPPORTED_40000baseCR4_Full |
777 SUPPORTED_40000baseSR4_Full |
778 SUPPORTED_40000baseLR4_Full))
779 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
780 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
781 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
782 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
783 ETH_LINK_SPEED_FIXED);
784 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
785 /* Link status changed. */
786 dev->data->dev_link = dev_link;
789 /* Link status is still the same. */
794 * Retrieve physical link information (unlocked version using new ioctl).
797 * Pointer to Ethernet device structure.
798 * @param wait_to_complete
799 * Wait for request completion (ignored).
802 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
804 struct priv *priv = dev->data->dev_private;
805 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
807 struct rte_eth_link dev_link;
810 (void)wait_to_complete;
811 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
812 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
815 memset(&dev_link, 0, sizeof(dev_link));
816 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
817 (ifr.ifr_flags & IFF_RUNNING));
818 ifr.ifr_data = (void *)&gcmd;
819 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
820 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
824 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
826 alignas(struct ethtool_link_settings)
827 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
828 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
829 struct ethtool_link_settings *ecmd = (void *)data;
832 ifr.ifr_data = (void *)ecmd;
833 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
834 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
838 dev_link.link_speed = ecmd->speed;
839 sc = ecmd->link_mode_masks[0] |
840 ((uint64_t)ecmd->link_mode_masks[1] << 32);
841 priv->link_speed_capa = 0;
842 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
843 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
844 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
845 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
846 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
847 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
848 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
849 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
850 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
851 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
852 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
853 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
854 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
855 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
856 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
857 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
858 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
859 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
860 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
861 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
862 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
863 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
864 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
865 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
866 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
867 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
868 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
869 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
870 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
871 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
872 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
873 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
874 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
875 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
876 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
877 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
878 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
879 ETH_LINK_SPEED_FIXED);
880 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
881 /* Link status changed. */
882 dev->data->dev_link = dev_link;
885 /* Link status is still the same. */
890 * Enable receiving and transmitting traffic.
893 * Pointer to private structure.
896 priv_link_start(struct priv *priv)
898 struct rte_eth_dev *dev = priv->dev;
901 dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
902 dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
903 err = priv_dev_traffic_enable(priv, dev);
905 ERROR("%p: error occurred while configuring control flows: %s",
906 (void *)priv, strerror(err));
907 err = priv_flow_start(priv, &priv->flows);
909 ERROR("%p: error occurred while configuring flows: %s",
910 (void *)priv, strerror(err));
914 * Disable receiving and transmitting traffic.
917 * Pointer to private structure.
920 priv_link_stop(struct priv *priv)
922 struct rte_eth_dev *dev = priv->dev;
924 priv_flow_stop(priv, &priv->flows);
925 priv_dev_traffic_disable(priv, dev);
926 dev->rx_pkt_burst = removed_rx_burst;
927 dev->tx_pkt_burst = removed_tx_burst;
931 * Retrieve physical link information and update rx/tx_pkt_burst callbacks
935 * Pointer to private structure.
936 * @param wait_to_complete
937 * Wait for request completion (ignored).
940 priv_link_update(struct priv *priv, int wait_to_complete)
942 struct rte_eth_dev *dev = priv->dev;
943 struct utsname utsname;
946 struct rte_eth_link dev_link = dev->data->dev_link;
948 if (uname(&utsname) == -1 ||
949 sscanf(utsname.release, "%d.%d.%d",
950 &ver[0], &ver[1], &ver[2]) != 3 ||
951 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
952 ret = mlx5_link_update_unlocked_gset(dev, wait_to_complete);
954 ret = mlx5_link_update_unlocked_gs(dev, wait_to_complete);
955 /* If lsc interrupt is disabled, should always be ready for traffic. */
956 if (!dev->data->dev_conf.intr_conf.lsc) {
957 priv_link_start(priv);
960 /* Re-select burst callbacks only if link status has been changed. */
961 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
962 if (dev->data->dev_link.link_status == ETH_LINK_UP)
963 priv_link_start(priv);
965 priv_link_stop(priv);
971 * Querying the link status till it changes to the desired state.
972 * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS.
975 * Pointer to private structure.
977 * Link desired status.
980 * 0 on success, negative errno value on failure.
983 priv_force_link_status_change(struct priv *priv, int status)
987 while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) {
988 priv_link_update(priv, 0);
989 if (priv->dev->data->dev_link.link_status == status)
998 * DPDK callback to retrieve physical link information.
1001 * Pointer to Ethernet device structure.
1002 * @param wait_to_complete
1003 * Wait for request completion (ignored).
1006 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1008 struct priv *priv = dev->data->dev_private;
1012 ret = priv_link_update(priv, wait_to_complete);
1018 * DPDK callback to change the MTU.
1021 * Pointer to Ethernet device structure.
1026 * 0 on success, negative errno value on failure.
1029 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1031 struct priv *priv = dev->data->dev_private;
1036 ret = priv_get_mtu(priv, &kern_mtu);
1039 /* Set kernel interface MTU first. */
1040 ret = priv_set_mtu(priv, mtu);
1043 ret = priv_get_mtu(priv, &kern_mtu);
1046 if (kern_mtu == mtu) {
1048 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
1054 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
1062 * DPDK callback to get flow control status.
1065 * Pointer to Ethernet device structure.
1066 * @param[out] fc_conf
1067 * Flow control output buffer.
1070 * 0 on success, negative errno value on failure.
1073 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1075 struct priv *priv = dev->data->dev_private;
1077 struct ethtool_pauseparam ethpause = {
1078 .cmd = ETHTOOL_GPAUSEPARAM
1082 ifr.ifr_data = (void *)ðpause;
1084 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1086 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1092 fc_conf->autoneg = ethpause.autoneg;
1093 if (ethpause.rx_pause && ethpause.tx_pause)
1094 fc_conf->mode = RTE_FC_FULL;
1095 else if (ethpause.rx_pause)
1096 fc_conf->mode = RTE_FC_RX_PAUSE;
1097 else if (ethpause.tx_pause)
1098 fc_conf->mode = RTE_FC_TX_PAUSE;
1100 fc_conf->mode = RTE_FC_NONE;
1110 * DPDK callback to modify flow control parameters.
1113 * Pointer to Ethernet device structure.
1114 * @param[in] fc_conf
1115 * Flow control parameters.
1118 * 0 on success, negative errno value on failure.
1121 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1123 struct priv *priv = dev->data->dev_private;
1125 struct ethtool_pauseparam ethpause = {
1126 .cmd = ETHTOOL_SPAUSEPARAM
1130 ifr.ifr_data = (void *)ðpause;
1131 ethpause.autoneg = fc_conf->autoneg;
1132 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1133 (fc_conf->mode & RTE_FC_RX_PAUSE))
1134 ethpause.rx_pause = 1;
1136 ethpause.rx_pause = 0;
1138 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1139 (fc_conf->mode & RTE_FC_TX_PAUSE))
1140 ethpause.tx_pause = 1;
1142 ethpause.tx_pause = 0;
1145 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1147 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1161 * Get PCI information from struct ibv_device.
1164 * Pointer to Ethernet device structure.
1165 * @param[out] pci_addr
1166 * PCI bus address output buffer.
1169 * 0 on success, -1 on failure and errno is set.
1172 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1173 struct rte_pci_addr *pci_addr)
1177 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1179 file = fopen(path, "rb");
1182 while (fgets(line, sizeof(line), file) == line) {
1183 size_t len = strlen(line);
1186 /* Truncate long lines. */
1187 if (len == (sizeof(line) - 1))
1188 while (line[(len - 1)] != '\n') {
1192 line[(len - 1)] = ret;
1194 /* Extract information. */
1197 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1201 &pci_addr->function) == 4) {
1211 * Update the link status.
1214 * Pointer to private structure.
1217 * Zero if the callback process can be called immediately.
1220 priv_link_status_update(struct priv *priv)
1222 struct rte_eth_link *link = &priv->dev->data->dev_link;
1224 priv_link_update(priv, 0);
1225 if (((link->link_speed == 0) && link->link_status) ||
1226 ((link->link_speed != 0) && !link->link_status)) {
1228 * Inconsistent status. Event likely occurred before the
1229 * kernel netdevice exposes the new status.
1231 if (!priv->pending_alarm) {
1232 priv->pending_alarm = 1;
1233 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1234 mlx5_dev_link_status_handler,
1238 } else if (unlikely(priv->pending_alarm)) {
1239 /* Link interrupt occurred while alarm is already scheduled. */
1240 priv->pending_alarm = 0;
1241 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1247 * Device status handler.
1250 * Pointer to private structure.
1252 * Pointer to event flags holder.
1255 * Events bitmap of callback process which can be called immediately.
1258 priv_dev_status_handler(struct priv *priv)
1260 struct ibv_async_event event;
1263 /* Read all message and acknowledge them. */
1265 if (mlx5_glue->get_async_event(priv->ctx, &event))
1267 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1268 event.event_type == IBV_EVENT_PORT_ERR) &&
1269 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1270 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1271 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1272 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1273 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1275 DEBUG("event type %d on port %d not handled",
1276 event.event_type, event.element.port_num);
1277 mlx5_glue->ack_async_event(&event);
1279 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1280 if (priv_link_status_update(priv))
1281 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1286 * Handle delayed link status event.
1289 * Registered argument.
1292 mlx5_dev_link_status_handler(void *arg)
1294 struct rte_eth_dev *dev = arg;
1295 struct priv *priv = dev->data->dev_private;
1298 while (!priv_trylock(priv)) {
1299 /* Alarm is being canceled. */
1300 if (priv->pending_alarm == 0)
1304 priv->pending_alarm = 0;
1305 ret = priv_link_status_update(priv);
1308 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1312 * Handle interrupts from the NIC.
1314 * @param[in] intr_handle
1315 * Interrupt handler.
1317 * Callback argument.
1320 mlx5_dev_interrupt_handler(void *cb_arg)
1322 struct rte_eth_dev *dev = cb_arg;
1323 struct priv *priv = dev->data->dev_private;
1327 events = priv_dev_status_handler(priv);
1329 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1330 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1331 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1332 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1336 * Handle interrupts from the socket.
1339 * Callback argument.
1342 mlx5_dev_handler_socket(void *cb_arg)
1344 struct rte_eth_dev *dev = cb_arg;
1345 struct priv *priv = dev->data->dev_private;
1348 priv_socket_handle(priv);
1353 * Uninstall interrupt handler.
1356 * Pointer to private structure.
1358 * Pointer to the rte_eth_dev structure.
1361 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1363 if (dev->data->dev_conf.intr_conf.lsc ||
1364 dev->data->dev_conf.intr_conf.rmv)
1365 rte_intr_callback_unregister(&priv->intr_handle,
1366 mlx5_dev_interrupt_handler, dev);
1367 if (priv->primary_socket)
1368 rte_intr_callback_unregister(&priv->intr_handle_socket,
1369 mlx5_dev_handler_socket, dev);
1370 if (priv->pending_alarm) {
1371 priv->pending_alarm = 0;
1372 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1374 priv->intr_handle.fd = 0;
1375 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1376 priv->intr_handle_socket.fd = 0;
1377 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1381 * Install interrupt handler.
1384 * Pointer to private structure.
1386 * Pointer to the rte_eth_dev structure.
1389 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1393 assert(priv->ctx->async_fd > 0);
1394 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1395 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1397 INFO("failed to change file descriptor async event queue");
1398 dev->data->dev_conf.intr_conf.lsc = 0;
1399 dev->data->dev_conf.intr_conf.rmv = 0;
1401 if (dev->data->dev_conf.intr_conf.lsc ||
1402 dev->data->dev_conf.intr_conf.rmv) {
1403 priv->intr_handle.fd = priv->ctx->async_fd;
1404 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1405 rte_intr_callback_register(&priv->intr_handle,
1406 mlx5_dev_interrupt_handler, dev);
1409 rc = priv_socket_init(priv);
1410 if (!rc && priv->primary_socket) {
1411 priv->intr_handle_socket.fd = priv->primary_socket;
1412 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1413 rte_intr_callback_register(&priv->intr_handle_socket,
1414 mlx5_dev_handler_socket, dev);
1419 * Change the link state (UP / DOWN).
1422 * Pointer to private data structure.
1424 * Nonzero for link up, otherwise link down.
1427 * 0 on success, errno value on failure.
1430 priv_dev_set_link(struct priv *priv, int up)
1432 return priv_set_flags(priv, ~IFF_UP, up ? IFF_UP : ~IFF_UP);
1436 * DPDK callback to bring the link DOWN.
1439 * Pointer to Ethernet device structure.
1442 * 0 on success, errno value on failure.
1445 mlx5_set_link_down(struct rte_eth_dev *dev)
1447 struct priv *priv = dev->data->dev_private;
1451 err = priv_dev_set_link(priv, 0);
1457 * DPDK callback to bring the link UP.
1460 * Pointer to Ethernet device structure.
1463 * 0 on success, errno value on failure.
1466 mlx5_set_link_up(struct rte_eth_dev *dev)
1468 struct priv *priv = dev->data->dev_private;
1472 err = priv_dev_set_link(priv, 1);
1478 * Configure the TX function to use.
1481 * Pointer to private data structure.
1483 * Pointer to rte_eth_dev structure.
1486 * Pointer to selected Tx burst function.
1489 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1491 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1492 struct mlx5_dev_config *config = &priv->config;
1493 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1494 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1495 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1496 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1497 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1499 assert(priv != NULL);
1500 /* Select appropriate TX function. */
1501 if (vlan_insert || tso)
1502 return tx_pkt_burst;
1503 if (config->mps == MLX5_MPW_ENHANCED) {
1504 if (priv_check_vec_tx_support(priv, dev) > 0) {
1505 if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1506 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1508 tx_pkt_burst = mlx5_tx_burst_vec;
1509 DEBUG("selected Enhanced MPW TX vectorized function");
1511 tx_pkt_burst = mlx5_tx_burst_empw;
1512 DEBUG("selected Enhanced MPW TX function");
1514 } else if (config->mps && (config->txq_inline > 0)) {
1515 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1516 DEBUG("selected MPW inline TX function");
1517 } else if (config->mps) {
1518 tx_pkt_burst = mlx5_tx_burst_mpw;
1519 DEBUG("selected MPW TX function");
1521 return tx_pkt_burst;
1525 * Configure the RX function to use.
1528 * Pointer to private data structure.
1530 * Pointer to rte_eth_dev structure.
1533 * Pointer to selected Rx burst function.
1536 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1538 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1540 assert(priv != NULL);
1541 if (priv_check_vec_rx_support(priv) > 0) {
1542 rx_pkt_burst = mlx5_rx_burst_vec;
1543 DEBUG("selected RX vectorized function");
1545 return rx_pkt_burst;
1549 * Check if mlx5 device was removed.
1552 * Pointer to Ethernet device structure.
1555 * 1 when device is removed, otherwise 0.
1558 mlx5_is_removed(struct rte_eth_dev *dev)
1560 struct ibv_device_attr device_attr;
1561 struct priv *priv = dev->data->dev_private;
1563 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)