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 * Get interface name from private structure.
125 * Pointer to private structure.
127 * Interface name output buffer.
130 * 0 on success, -1 on failure and errno is set.
133 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
142 MKSTR(path, "%s/device/net", priv->ibdev_path);
148 while ((dent = readdir(dir)) != NULL) {
149 char *name = dent->d_name;
151 unsigned int dev_port;
154 if ((name[0] == '.') &&
155 ((name[1] == '\0') ||
156 ((name[1] == '.') && (name[2] == '\0'))))
159 MKSTR(path, "%s/device/net/%s/%s",
160 priv->ibdev_path, name,
161 (dev_type ? "dev_id" : "dev_port"));
163 file = fopen(path, "rb");
168 * Switch to dev_id when dev_port does not exist as
169 * is the case with Linux kernel versions < 3.15.
180 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
185 * Switch to dev_id when dev_port returns the same value for
186 * all ports. May happen when using a MOFED release older than
187 * 3.0 with a Linux kernel >= 3.15.
189 if (dev_port == dev_port_prev)
191 dev_port_prev = dev_port;
192 if (dev_port == (priv->port - 1u))
193 snprintf(match, sizeof(match), "%s", name);
196 if (match[0] == '\0')
198 strncpy(*ifname, match, sizeof(*ifname));
203 * Check if the counter is located on ib counters file.
209 * 1 if counter is located on ib counters file , 0 otherwise.
212 priv_is_ib_cntr(const char *cntr)
214 if (!strcmp(cntr, "out_of_buffer"))
220 * Read from sysfs entry.
223 * Pointer to private structure.
225 * Entry name relative to sysfs path.
227 * Data output buffer.
232 * 0 on success, -1 on failure and errno is set.
235 priv_sysfs_read(const struct priv *priv, const char *entry,
236 char *buf, size_t size)
238 char ifname[IF_NAMESIZE];
243 if (priv_get_ifname(priv, &ifname))
246 if (priv_is_ib_cntr(entry)) {
247 MKSTR(path, "%s/ports/1/hw_counters/%s",
248 priv->ibdev_path, entry);
249 file = fopen(path, "rb");
251 MKSTR(path, "%s/device/net/%s/%s",
252 priv->ibdev_path, ifname, entry);
253 file = fopen(path, "rb");
257 ret = fread(buf, 1, size, file);
259 if (((size_t)ret < size) && (ferror(file)))
269 * Write to sysfs entry.
272 * Pointer to private structure.
274 * Entry name relative to sysfs path.
281 * 0 on success, -1 on failure and errno is set.
284 priv_sysfs_write(const struct priv *priv, const char *entry,
285 char *buf, size_t size)
287 char ifname[IF_NAMESIZE];
292 if (priv_get_ifname(priv, &ifname))
295 MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
297 file = fopen(path, "wb");
300 ret = fwrite(buf, 1, size, file);
302 if (((size_t)ret < size) || (ferror(file)))
312 * Get unsigned long sysfs property.
315 * Pointer to private structure.
317 * Entry name relative to sysfs path.
319 * Value output buffer.
322 * 0 on success, -1 on failure and errno is set.
325 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
328 unsigned long value_ret;
331 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
333 DEBUG("cannot read %s value from sysfs: %s",
334 name, strerror(errno));
337 value_str[ret] = '\0';
339 value_ret = strtoul(value_str, NULL, 0);
341 DEBUG("invalid %s value `%s': %s", name, value_str,
350 * Set unsigned long sysfs property.
353 * Pointer to private structure.
355 * Entry name relative to sysfs path.
360 * 0 on success, -1 on failure and errno is set.
363 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
366 MKSTR(value_str, "%lu", value);
368 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
370 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
371 name, value_str, value, strerror(errno));
378 * Perform ifreq ioctl() on associated Ethernet device.
381 * Pointer to private structure.
383 * Request number to pass to ioctl().
385 * Interface request structure output buffer.
388 * 0 on success, -1 on failure and errno is set.
391 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
393 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
398 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
399 ret = ioctl(sock, req, ifr);
405 * Return the number of active VFs for the current device.
408 * Pointer to private structure.
409 * @param[out] num_vfs
410 * Number of active VFs.
413 * 0 on success, -1 on failure and errno is set.
416 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
418 /* The sysfs entry name depends on the operating system. */
419 const char **name = (const char *[]){
420 "device/sriov_numvfs",
421 "device/mlx5_num_vfs",
427 unsigned long ulong_num_vfs;
429 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
431 *num_vfs = ulong_num_vfs;
432 } while (*(++name) && ret);
440 * Pointer to private structure.
442 * MTU value output buffer.
445 * 0 on success, -1 on failure and errno is set.
448 priv_get_mtu(struct priv *priv, uint16_t *mtu)
450 unsigned long ulong_mtu;
452 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
459 * Read device counter from sysfs.
462 * Pointer to private structure.
466 * Counter output buffer.
469 * 0 on success, -1 on failure and errno is set.
472 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
474 unsigned long ulong_ctr;
476 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
486 * Pointer to private structure.
491 * 0 on success, -1 on failure and errno is set.
494 priv_set_mtu(struct priv *priv, uint16_t mtu)
498 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
499 priv_get_mtu(priv, &new_mtu))
511 * Pointer to private structure.
513 * Bitmask for flags that must remain untouched.
515 * Bitmask for flags to modify.
518 * 0 on success, -1 on failure and errno is set.
521 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
525 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
528 tmp |= (flags & (~keep));
529 return priv_set_sysfs_ulong(priv, "flags", tmp);
533 * Ethernet device configuration.
535 * Prepare the driver for a given number of TX and RX queues.
538 * Pointer to Ethernet device structure.
541 * 0 on success, errno value on failure.
544 dev_configure(struct rte_eth_dev *dev)
546 struct priv *priv = dev->data->dev_private;
547 unsigned int rxqs_n = dev->data->nb_rx_queues;
548 unsigned int txqs_n = dev->data->nb_tx_queues;
551 unsigned int reta_idx_n;
552 const uint8_t use_app_rss_key =
553 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
554 uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
555 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
556 uint64_t supp_rx_offloads =
557 (mlx5_priv_get_rx_port_offloads(priv) |
558 mlx5_priv_get_rx_queue_offloads(priv));
559 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
561 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
562 ERROR("Some Tx offloads are not supported "
563 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
564 tx_offloads, supp_tx_offloads);
567 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
568 ERROR("Some Rx offloads are not supported "
569 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
570 rx_offloads, supp_rx_offloads);
573 if (use_app_rss_key &&
574 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
575 rss_hash_default_key_len)) {
576 /* MLX5 RSS only support 40bytes key. */
579 priv->rss_conf.rss_key =
580 rte_realloc(priv->rss_conf.rss_key,
581 rss_hash_default_key_len, 0);
582 if (!priv->rss_conf.rss_key) {
583 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
586 memcpy(priv->rss_conf.rss_key,
588 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
589 rss_hash_default_key,
590 rss_hash_default_key_len);
591 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
592 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
593 priv->rxqs = (void *)dev->data->rx_queues;
594 priv->txqs = (void *)dev->data->tx_queues;
595 if (txqs_n != priv->txqs_n) {
596 INFO("%p: TX queues number update: %u -> %u",
597 (void *)dev, priv->txqs_n, txqs_n);
598 priv->txqs_n = txqs_n;
600 if (rxqs_n > priv->config.ind_table_max_size) {
601 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
604 if (rxqs_n == priv->rxqs_n)
606 INFO("%p: RX queues number update: %u -> %u",
607 (void *)dev, priv->rxqs_n, rxqs_n);
608 priv->rxqs_n = rxqs_n;
609 /* If the requested number of RX queues is not a power of two, use the
610 * maximum indirection table size for better balancing.
611 * The result is always rounded to the next power of two. */
612 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
613 priv->config.ind_table_max_size :
615 if (priv_rss_reta_index_resize(priv, reta_idx_n))
617 /* When the number of RX queues is not a power of two, the remaining
618 * table entries are padded with reused WQs and hashes are not spread
620 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
621 (*priv->reta_idx)[i] = j;
629 * DPDK callback for Ethernet device configuration.
632 * Pointer to Ethernet device structure.
635 * 0 on success, negative errno value on failure.
638 mlx5_dev_configure(struct rte_eth_dev *dev)
640 struct priv *priv = dev->data->dev_private;
644 ret = dev_configure(dev);
651 * DPDK callback to get information about the device.
654 * Pointer to Ethernet device structure.
656 * Info structure output buffer.
659 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
661 struct priv *priv = dev->data->dev_private;
662 struct mlx5_dev_config *config = &priv->config;
664 char ifname[IF_NAMESIZE];
666 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
669 /* FIXME: we should ask the device for these values. */
670 info->min_rx_bufsize = 32;
671 info->max_rx_pktlen = 65536;
673 * Since we need one CQ per QP, the limit is the minimum number
674 * between the two values.
676 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
677 priv->device_attr.orig_attr.max_qp);
678 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
681 info->max_rx_queues = max;
682 info->max_tx_queues = max;
683 info->max_mac_addrs = RTE_DIM(priv->mac);
684 info->rx_queue_offload_capa =
685 mlx5_priv_get_rx_queue_offloads(priv);
686 info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
687 info->rx_queue_offload_capa);
688 info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
689 if (priv_get_ifname(priv, &ifname) == 0)
690 info->if_index = if_nametoindex(ifname);
691 info->reta_size = priv->reta_idx_n ?
692 priv->reta_idx_n : config->ind_table_max_size;
693 info->hash_key_size = priv->rss_conf.rss_key_len;
694 info->speed_capa = priv->link_speed_capa;
699 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
701 static const uint32_t ptypes[] = {
702 /* refers to rxq_cq_to_pkt_type() */
704 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
705 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
706 RTE_PTYPE_L4_NONFRAG,
710 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
711 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
712 RTE_PTYPE_INNER_L4_NONFRAG,
713 RTE_PTYPE_INNER_L4_FRAG,
714 RTE_PTYPE_INNER_L4_TCP,
715 RTE_PTYPE_INNER_L4_UDP,
719 if (dev->rx_pkt_burst == mlx5_rx_burst ||
720 dev->rx_pkt_burst == mlx5_rx_burst_vec)
726 * DPDK callback to retrieve physical link information.
729 * Pointer to Ethernet device structure.
730 * @param wait_to_complete
731 * Wait for request completion (ignored).
734 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
736 struct priv *priv = dev->data->dev_private;
737 struct ethtool_cmd edata = {
738 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
741 struct rte_eth_link dev_link;
744 /* priv_lock() is not taken to allow concurrent calls. */
746 (void)wait_to_complete;
747 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
748 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
751 memset(&dev_link, 0, sizeof(dev_link));
752 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
753 (ifr.ifr_flags & IFF_RUNNING));
754 ifr.ifr_data = (void *)&edata;
755 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
756 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
760 link_speed = ethtool_cmd_speed(&edata);
761 if (link_speed == -1)
762 dev_link.link_speed = 0;
764 dev_link.link_speed = link_speed;
765 priv->link_speed_capa = 0;
766 if (edata.supported & SUPPORTED_Autoneg)
767 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
768 if (edata.supported & (SUPPORTED_1000baseT_Full |
769 SUPPORTED_1000baseKX_Full))
770 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
771 if (edata.supported & SUPPORTED_10000baseKR_Full)
772 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
773 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
774 SUPPORTED_40000baseCR4_Full |
775 SUPPORTED_40000baseSR4_Full |
776 SUPPORTED_40000baseLR4_Full))
777 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
778 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
779 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
780 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
781 ETH_LINK_SPEED_FIXED);
782 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
783 /* Link status changed. */
784 dev->data->dev_link = dev_link;
787 /* Link status is still the same. */
792 * Retrieve physical link information (unlocked version using new ioctl).
795 * Pointer to Ethernet device structure.
796 * @param wait_to_complete
797 * Wait for request completion (ignored).
800 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
802 struct priv *priv = dev->data->dev_private;
803 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
805 struct rte_eth_link dev_link;
808 (void)wait_to_complete;
809 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
810 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
813 memset(&dev_link, 0, sizeof(dev_link));
814 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
815 (ifr.ifr_flags & IFF_RUNNING));
816 ifr.ifr_data = (void *)&gcmd;
817 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
818 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
822 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
824 alignas(struct ethtool_link_settings)
825 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
826 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
827 struct ethtool_link_settings *ecmd = (void *)data;
830 ifr.ifr_data = (void *)ecmd;
831 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
832 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
836 dev_link.link_speed = ecmd->speed;
837 sc = ecmd->link_mode_masks[0] |
838 ((uint64_t)ecmd->link_mode_masks[1] << 32);
839 priv->link_speed_capa = 0;
840 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
841 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
842 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
843 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
844 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
845 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
846 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
847 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
848 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
849 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
850 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
851 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
852 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
853 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
854 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
855 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
856 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
857 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
858 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
859 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
860 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
861 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
862 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
863 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
864 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
865 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
866 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
867 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
868 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
869 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
870 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
871 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
872 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
873 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
874 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
875 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
876 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
877 ETH_LINK_SPEED_FIXED);
878 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
879 /* Link status changed. */
880 dev->data->dev_link = dev_link;
883 /* Link status is still the same. */
888 * DPDK callback to retrieve physical link information.
891 * Pointer to Ethernet device structure.
892 * @param wait_to_complete
893 * Wait for request completion (ignored).
896 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
898 struct utsname utsname;
901 if (uname(&utsname) == -1 ||
902 sscanf(utsname.release, "%d.%d.%d",
903 &ver[0], &ver[1], &ver[2]) != 3 ||
904 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
905 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
906 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
910 * DPDK callback to change the MTU.
913 * Pointer to Ethernet device structure.
918 * 0 on success, negative errno value on failure.
921 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
923 struct priv *priv = dev->data->dev_private;
928 ret = priv_get_mtu(priv, &kern_mtu);
931 /* Set kernel interface MTU first. */
932 ret = priv_set_mtu(priv, mtu);
935 ret = priv_get_mtu(priv, &kern_mtu);
938 if (kern_mtu == mtu) {
940 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
946 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
954 * DPDK callback to get flow control status.
957 * Pointer to Ethernet device structure.
958 * @param[out] fc_conf
959 * Flow control output buffer.
962 * 0 on success, negative errno value on failure.
965 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
967 struct priv *priv = dev->data->dev_private;
969 struct ethtool_pauseparam ethpause = {
970 .cmd = ETHTOOL_GPAUSEPARAM
974 ifr.ifr_data = (void *)ðpause;
976 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
978 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
984 fc_conf->autoneg = ethpause.autoneg;
985 if (ethpause.rx_pause && ethpause.tx_pause)
986 fc_conf->mode = RTE_FC_FULL;
987 else if (ethpause.rx_pause)
988 fc_conf->mode = RTE_FC_RX_PAUSE;
989 else if (ethpause.tx_pause)
990 fc_conf->mode = RTE_FC_TX_PAUSE;
992 fc_conf->mode = RTE_FC_NONE;
1002 * DPDK callback to modify flow control parameters.
1005 * Pointer to Ethernet device structure.
1006 * @param[in] fc_conf
1007 * Flow control parameters.
1010 * 0 on success, negative errno value on failure.
1013 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1015 struct priv *priv = dev->data->dev_private;
1017 struct ethtool_pauseparam ethpause = {
1018 .cmd = ETHTOOL_SPAUSEPARAM
1022 ifr.ifr_data = (void *)ðpause;
1023 ethpause.autoneg = fc_conf->autoneg;
1024 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1025 (fc_conf->mode & RTE_FC_RX_PAUSE))
1026 ethpause.rx_pause = 1;
1028 ethpause.rx_pause = 0;
1030 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1031 (fc_conf->mode & RTE_FC_TX_PAUSE))
1032 ethpause.tx_pause = 1;
1034 ethpause.tx_pause = 0;
1037 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1039 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1053 * Get PCI information from struct ibv_device.
1056 * Pointer to Ethernet device structure.
1057 * @param[out] pci_addr
1058 * PCI bus address output buffer.
1061 * 0 on success, -1 on failure and errno is set.
1064 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1065 struct rte_pci_addr *pci_addr)
1069 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1071 file = fopen(path, "rb");
1074 while (fgets(line, sizeof(line), file) == line) {
1075 size_t len = strlen(line);
1078 /* Truncate long lines. */
1079 if (len == (sizeof(line) - 1))
1080 while (line[(len - 1)] != '\n') {
1084 line[(len - 1)] = ret;
1086 /* Extract information. */
1089 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1093 &pci_addr->function) == 4) {
1103 * Update the link status.
1106 * Pointer to private structure.
1109 * Zero if the callback process can be called immediately.
1112 priv_link_status_update(struct priv *priv)
1114 struct rte_eth_link *link = &priv->dev->data->dev_link;
1116 mlx5_link_update(priv->dev, 0);
1117 if (((link->link_speed == 0) && link->link_status) ||
1118 ((link->link_speed != 0) && !link->link_status)) {
1120 * Inconsistent status. Event likely occurred before the
1121 * kernel netdevice exposes the new status.
1123 if (!priv->pending_alarm) {
1124 priv->pending_alarm = 1;
1125 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1126 mlx5_dev_link_status_handler,
1130 } else if (unlikely(priv->pending_alarm)) {
1131 /* Link interrupt occurred while alarm is already scheduled. */
1132 priv->pending_alarm = 0;
1133 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1139 * Device status handler.
1142 * Pointer to private structure.
1144 * Pointer to event flags holder.
1147 * Events bitmap of callback process which can be called immediately.
1150 priv_dev_status_handler(struct priv *priv)
1152 struct ibv_async_event event;
1155 /* Read all message and acknowledge them. */
1157 if (ibv_get_async_event(priv->ctx, &event))
1159 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1160 event.event_type == IBV_EVENT_PORT_ERR) &&
1161 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1162 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1163 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1164 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1165 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1167 DEBUG("event type %d on port %d not handled",
1168 event.event_type, event.element.port_num);
1169 ibv_ack_async_event(&event);
1171 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1172 if (priv_link_status_update(priv))
1173 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1178 * Handle delayed link status event.
1181 * Registered argument.
1184 mlx5_dev_link_status_handler(void *arg)
1186 struct rte_eth_dev *dev = arg;
1187 struct priv *priv = dev->data->dev_private;
1190 while (!priv_trylock(priv)) {
1191 /* Alarm is being canceled. */
1192 if (priv->pending_alarm == 0)
1196 priv->pending_alarm = 0;
1197 ret = priv_link_status_update(priv);
1200 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1204 * Handle interrupts from the NIC.
1206 * @param[in] intr_handle
1207 * Interrupt handler.
1209 * Callback argument.
1212 mlx5_dev_interrupt_handler(void *cb_arg)
1214 struct rte_eth_dev *dev = cb_arg;
1215 struct priv *priv = dev->data->dev_private;
1219 events = priv_dev_status_handler(priv);
1221 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1222 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1223 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1224 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1228 * Handle interrupts from the socket.
1231 * Callback argument.
1234 mlx5_dev_handler_socket(void *cb_arg)
1236 struct rte_eth_dev *dev = cb_arg;
1237 struct priv *priv = dev->data->dev_private;
1240 priv_socket_handle(priv);
1245 * Uninstall interrupt handler.
1248 * Pointer to private structure.
1250 * Pointer to the rte_eth_dev structure.
1253 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1255 if (dev->data->dev_conf.intr_conf.lsc ||
1256 dev->data->dev_conf.intr_conf.rmv)
1257 rte_intr_callback_unregister(&priv->intr_handle,
1258 mlx5_dev_interrupt_handler, dev);
1259 if (priv->primary_socket)
1260 rte_intr_callback_unregister(&priv->intr_handle_socket,
1261 mlx5_dev_handler_socket, dev);
1262 if (priv->pending_alarm) {
1263 priv->pending_alarm = 0;
1264 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1266 priv->intr_handle.fd = 0;
1267 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1268 priv->intr_handle_socket.fd = 0;
1269 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1273 * Install interrupt handler.
1276 * Pointer to private structure.
1278 * Pointer to the rte_eth_dev structure.
1281 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1285 assert(priv->ctx->async_fd > 0);
1286 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1287 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1289 INFO("failed to change file descriptor async event queue");
1290 dev->data->dev_conf.intr_conf.lsc = 0;
1291 dev->data->dev_conf.intr_conf.rmv = 0;
1293 if (dev->data->dev_conf.intr_conf.lsc ||
1294 dev->data->dev_conf.intr_conf.rmv) {
1295 priv->intr_handle.fd = priv->ctx->async_fd;
1296 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1297 rte_intr_callback_register(&priv->intr_handle,
1298 mlx5_dev_interrupt_handler, dev);
1301 rc = priv_socket_init(priv);
1302 if (!rc && priv->primary_socket) {
1303 priv->intr_handle_socket.fd = priv->primary_socket;
1304 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1305 rte_intr_callback_register(&priv->intr_handle_socket,
1306 mlx5_dev_handler_socket, dev);
1311 * Change the link state (UP / DOWN).
1314 * Pointer to private data structure.
1316 * Pointer to rte_eth_dev structure.
1318 * Nonzero for link up, otherwise link down.
1321 * 0 on success, errno value on failure.
1324 priv_dev_set_link(struct priv *priv, struct rte_eth_dev *dev, int up)
1329 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1332 dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
1333 dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
1335 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1338 dev->rx_pkt_burst = removed_rx_burst;
1339 dev->tx_pkt_burst = removed_tx_burst;
1345 * DPDK callback to bring the link DOWN.
1348 * Pointer to Ethernet device structure.
1351 * 0 on success, errno value on failure.
1354 mlx5_set_link_down(struct rte_eth_dev *dev)
1356 struct priv *priv = dev->data->dev_private;
1360 err = priv_dev_set_link(priv, dev, 0);
1366 * DPDK callback to bring the link UP.
1369 * Pointer to Ethernet device structure.
1372 * 0 on success, errno value on failure.
1375 mlx5_set_link_up(struct rte_eth_dev *dev)
1377 struct priv *priv = dev->data->dev_private;
1381 err = priv_dev_set_link(priv, dev, 1);
1387 * Configure the TX function to use.
1390 * Pointer to private data structure.
1392 * Pointer to rte_eth_dev structure.
1395 * Pointer to selected Tx burst function.
1398 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1400 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1401 struct mlx5_dev_config *config = &priv->config;
1402 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1403 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1404 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1405 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1406 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1408 assert(priv != NULL);
1409 /* Select appropriate TX function. */
1410 if (vlan_insert || tso)
1411 return tx_pkt_burst;
1412 if (config->mps == MLX5_MPW_ENHANCED) {
1413 if (priv_check_vec_tx_support(priv, dev) > 0) {
1414 if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1415 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1417 tx_pkt_burst = mlx5_tx_burst_vec;
1418 DEBUG("selected Enhanced MPW TX vectorized function");
1420 tx_pkt_burst = mlx5_tx_burst_empw;
1421 DEBUG("selected Enhanced MPW TX function");
1423 } else if (config->mps && (config->txq_inline > 0)) {
1424 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1425 DEBUG("selected MPW inline TX function");
1426 } else if (config->mps) {
1427 tx_pkt_burst = mlx5_tx_burst_mpw;
1428 DEBUG("selected MPW TX function");
1430 return tx_pkt_burst;
1434 * Configure the RX function to use.
1437 * Pointer to private data structure.
1439 * Pointer to rte_eth_dev structure.
1442 * Pointer to selected Rx burst function.
1445 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1447 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1449 assert(priv != NULL);
1450 if (priv_check_vec_rx_support(priv) > 0) {
1451 rx_pkt_burst = mlx5_rx_burst_vec;
1452 DEBUG("selected RX vectorized function");
1454 return rx_pkt_burst;