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->ctx->device->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->ctx->device->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->ctx->device->ibdev_path, entry);
275 file = fopen(path, "rb");
277 MKSTR(path, "%s/device/net/%s/%s",
278 priv->ctx->device->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->ctx->device->ibdev_path,
324 file = fopen(path, "wb");
327 ret = fwrite(buf, 1, size, file);
329 if (((size_t)ret < size) || (ferror(file)))
339 * Get unsigned long sysfs property.
342 * Pointer to private structure.
344 * Entry name relative to sysfs path.
346 * Value output buffer.
349 * 0 on success, -1 on failure and errno is set.
352 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
355 unsigned long value_ret;
358 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
360 DEBUG("cannot read %s value from sysfs: %s",
361 name, strerror(errno));
364 value_str[ret] = '\0';
366 value_ret = strtoul(value_str, NULL, 0);
368 DEBUG("invalid %s value `%s': %s", name, value_str,
377 * Set unsigned long sysfs property.
380 * Pointer to private structure.
382 * Entry name relative to sysfs path.
387 * 0 on success, -1 on failure and errno is set.
390 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
393 MKSTR(value_str, "%lu", value);
395 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
397 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
398 name, value_str, value, strerror(errno));
405 * Perform ifreq ioctl() on associated Ethernet device.
408 * Pointer to private structure.
410 * Request number to pass to ioctl().
412 * Interface request structure output buffer.
415 * 0 on success, -1 on failure and errno is set.
418 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
420 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
425 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
426 ret = ioctl(sock, req, ifr);
432 * Return the number of active VFs for the current device.
435 * Pointer to private structure.
436 * @param[out] num_vfs
437 * Number of active VFs.
440 * 0 on success, -1 on failure and errno is set.
443 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
445 /* The sysfs entry name depends on the operating system. */
446 const char **name = (const char *[]){
447 "device/sriov_numvfs",
448 "device/mlx5_num_vfs",
454 unsigned long ulong_num_vfs;
456 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
458 *num_vfs = ulong_num_vfs;
459 } while (*(++name) && ret);
467 * Pointer to private structure.
469 * MTU value output buffer.
472 * 0 on success, -1 on failure and errno is set.
475 priv_get_mtu(struct priv *priv, uint16_t *mtu)
477 unsigned long ulong_mtu;
479 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
486 * Read device counter from sysfs.
489 * Pointer to private structure.
493 * Counter output buffer.
496 * 0 on success, -1 on failure and errno is set.
499 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
501 unsigned long ulong_ctr;
503 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
513 * Pointer to private structure.
518 * 0 on success, -1 on failure and errno is set.
521 priv_set_mtu(struct priv *priv, uint16_t mtu)
525 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
526 priv_get_mtu(priv, &new_mtu))
538 * Pointer to private structure.
540 * Bitmask for flags that must remain untouched.
542 * Bitmask for flags to modify.
545 * 0 on success, -1 on failure and errno is set.
548 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
552 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
555 tmp |= (flags & (~keep));
556 return priv_set_sysfs_ulong(priv, "flags", tmp);
560 * Ethernet device configuration.
562 * Prepare the driver for a given number of TX and RX queues.
565 * Pointer to Ethernet device structure.
568 * 0 on success, errno value on failure.
571 dev_configure(struct rte_eth_dev *dev)
573 struct priv *priv = dev->data->dev_private;
574 unsigned int rxqs_n = dev->data->nb_rx_queues;
575 unsigned int txqs_n = dev->data->nb_tx_queues;
578 unsigned int reta_idx_n;
580 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
581 priv->rxqs = (void *)dev->data->rx_queues;
582 priv->txqs = (void *)dev->data->tx_queues;
583 if (txqs_n != priv->txqs_n) {
584 INFO("%p: TX queues number update: %u -> %u",
585 (void *)dev, priv->txqs_n, txqs_n);
586 priv->txqs_n = txqs_n;
588 if (rxqs_n > priv->ind_table_max_size) {
589 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
592 if (rxqs_n == priv->rxqs_n)
594 INFO("%p: RX queues number update: %u -> %u",
595 (void *)dev, priv->rxqs_n, rxqs_n);
596 priv->rxqs_n = rxqs_n;
597 /* If the requested number of RX queues is not a power of two, use the
598 * maximum indirection table size for better balancing.
599 * The result is always rounded to the next power of two. */
600 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
601 priv->ind_table_max_size :
603 if (priv_rss_reta_index_resize(priv, reta_idx_n))
605 /* When the number of RX queues is not a power of two, the remaining
606 * table entries are padded with reused WQs and hashes are not spread
608 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
609 (*priv->reta_idx)[i] = j;
617 * DPDK callback for Ethernet device configuration.
620 * Pointer to Ethernet device structure.
623 * 0 on success, negative errno value on failure.
626 mlx5_dev_configure(struct rte_eth_dev *dev)
628 struct priv *priv = dev->data->dev_private;
631 if (mlx5_is_secondary())
632 return -E_RTE_SECONDARY;
635 ret = dev_configure(dev);
642 * DPDK callback to get information about the device.
645 * Pointer to Ethernet device structure.
647 * Info structure output buffer.
650 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
652 struct priv *priv = mlx5_get_priv(dev);
654 char ifname[IF_NAMESIZE];
656 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
659 /* FIXME: we should ask the device for these values. */
660 info->min_rx_bufsize = 32;
661 info->max_rx_pktlen = 65536;
663 * Since we need one CQ per QP, the limit is the minimum number
664 * between the two values.
666 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
667 priv->device_attr.orig_attr.max_qp);
668 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
671 info->max_rx_queues = max;
672 info->max_tx_queues = max;
673 info->max_mac_addrs = RTE_DIM(priv->mac);
674 info->rx_offload_capa =
676 (DEV_RX_OFFLOAD_IPV4_CKSUM |
677 DEV_RX_OFFLOAD_UDP_CKSUM |
678 DEV_RX_OFFLOAD_TCP_CKSUM) :
680 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
682 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
684 info->tx_offload_capa |=
685 (DEV_TX_OFFLOAD_IPV4_CKSUM |
686 DEV_TX_OFFLOAD_UDP_CKSUM |
687 DEV_TX_OFFLOAD_TCP_CKSUM);
689 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
691 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
692 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
693 DEV_TX_OFFLOAD_GRE_TNL_TSO);
694 if (priv_get_ifname(priv, &ifname) == 0)
695 info->if_index = if_nametoindex(ifname);
696 info->reta_size = priv->reta_idx_n ?
697 priv->reta_idx_n : priv->ind_table_max_size;
698 info->hash_key_size = ((*priv->rss_conf) ?
699 (*priv->rss_conf)[0]->rss_key_len :
701 info->speed_capa = priv->link_speed_capa;
706 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
708 static const uint32_t ptypes[] = {
709 /* refers to rxq_cq_to_pkt_type() */
711 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
712 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
713 RTE_PTYPE_L4_NONFRAG,
717 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
718 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
719 RTE_PTYPE_INNER_L4_NONFRAG,
720 RTE_PTYPE_INNER_L4_FRAG,
721 RTE_PTYPE_INNER_L4_TCP,
722 RTE_PTYPE_INNER_L4_UDP,
726 if (dev->rx_pkt_burst == mlx5_rx_burst ||
727 dev->rx_pkt_burst == mlx5_rx_burst_vec)
733 * DPDK callback to retrieve physical link information.
736 * Pointer to Ethernet device structure.
737 * @param wait_to_complete
738 * Wait for request completion (ignored).
741 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
743 struct priv *priv = mlx5_get_priv(dev);
744 struct ethtool_cmd edata = {
745 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
748 struct rte_eth_link dev_link;
751 /* priv_lock() is not taken to allow concurrent calls. */
753 (void)wait_to_complete;
754 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
755 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
758 memset(&dev_link, 0, sizeof(dev_link));
759 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
760 (ifr.ifr_flags & IFF_RUNNING));
761 ifr.ifr_data = (void *)&edata;
762 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
763 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
767 link_speed = ethtool_cmd_speed(&edata);
768 if (link_speed == -1)
769 dev_link.link_speed = 0;
771 dev_link.link_speed = link_speed;
772 priv->link_speed_capa = 0;
773 if (edata.supported & SUPPORTED_Autoneg)
774 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
775 if (edata.supported & (SUPPORTED_1000baseT_Full |
776 SUPPORTED_1000baseKX_Full))
777 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
778 if (edata.supported & SUPPORTED_10000baseKR_Full)
779 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
780 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
781 SUPPORTED_40000baseCR4_Full |
782 SUPPORTED_40000baseSR4_Full |
783 SUPPORTED_40000baseLR4_Full))
784 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
785 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
786 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
787 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
788 ETH_LINK_SPEED_FIXED);
789 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
790 /* Link status changed. */
791 dev->data->dev_link = dev_link;
794 /* Link status is still the same. */
799 * Retrieve physical link information (unlocked version using new ioctl).
802 * Pointer to Ethernet device structure.
803 * @param wait_to_complete
804 * Wait for request completion (ignored).
807 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
809 struct priv *priv = mlx5_get_priv(dev);
810 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
812 struct rte_eth_link dev_link;
815 (void)wait_to_complete;
816 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
817 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
820 memset(&dev_link, 0, sizeof(dev_link));
821 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
822 (ifr.ifr_flags & IFF_RUNNING));
823 ifr.ifr_data = (void *)&gcmd;
824 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
825 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
829 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
831 alignas(struct ethtool_link_settings)
832 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
833 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
834 struct ethtool_link_settings *ecmd = (void *)data;
837 ifr.ifr_data = (void *)ecmd;
838 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
839 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
843 dev_link.link_speed = ecmd->speed;
844 sc = ecmd->link_mode_masks[0] |
845 ((uint64_t)ecmd->link_mode_masks[1] << 32);
846 priv->link_speed_capa = 0;
847 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
848 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
849 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
850 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
851 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
852 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
853 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
854 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
855 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
856 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
857 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
858 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
859 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
860 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
861 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
862 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
863 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
864 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
865 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
866 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
867 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
868 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
869 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
870 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
871 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
872 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
873 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
874 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
875 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
876 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
877 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
878 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
879 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
880 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
881 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
882 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
883 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
884 ETH_LINK_SPEED_FIXED);
885 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
886 /* Link status changed. */
887 dev->data->dev_link = dev_link;
890 /* Link status is still the same. */
895 * DPDK callback to retrieve physical link information.
898 * Pointer to Ethernet device structure.
899 * @param wait_to_complete
900 * Wait for request completion (ignored).
903 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
905 struct utsname utsname;
908 if (uname(&utsname) == -1 ||
909 sscanf(utsname.release, "%d.%d.%d",
910 &ver[0], &ver[1], &ver[2]) != 3 ||
911 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
912 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
913 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
917 * DPDK callback to change the MTU.
920 * Pointer to Ethernet device structure.
925 * 0 on success, negative errno value on failure.
928 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
930 struct priv *priv = dev->data->dev_private;
934 if (mlx5_is_secondary())
935 return -E_RTE_SECONDARY;
938 ret = priv_get_mtu(priv, &kern_mtu);
941 /* Set kernel interface MTU first. */
942 ret = priv_set_mtu(priv, mtu);
945 ret = priv_get_mtu(priv, &kern_mtu);
948 if (kern_mtu == mtu) {
950 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
956 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
964 * DPDK callback to get flow control status.
967 * Pointer to Ethernet device structure.
968 * @param[out] fc_conf
969 * Flow control output buffer.
972 * 0 on success, negative errno value on failure.
975 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
977 struct priv *priv = dev->data->dev_private;
979 struct ethtool_pauseparam ethpause = {
980 .cmd = ETHTOOL_GPAUSEPARAM
984 if (mlx5_is_secondary())
985 return -E_RTE_SECONDARY;
987 ifr.ifr_data = (void *)ðpause;
989 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
991 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
997 fc_conf->autoneg = ethpause.autoneg;
998 if (ethpause.rx_pause && ethpause.tx_pause)
999 fc_conf->mode = RTE_FC_FULL;
1000 else if (ethpause.rx_pause)
1001 fc_conf->mode = RTE_FC_RX_PAUSE;
1002 else if (ethpause.tx_pause)
1003 fc_conf->mode = RTE_FC_TX_PAUSE;
1005 fc_conf->mode = RTE_FC_NONE;
1015 * DPDK callback to modify flow control parameters.
1018 * Pointer to Ethernet device structure.
1019 * @param[in] fc_conf
1020 * Flow control parameters.
1023 * 0 on success, negative errno value on failure.
1026 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1028 struct priv *priv = dev->data->dev_private;
1030 struct ethtool_pauseparam ethpause = {
1031 .cmd = ETHTOOL_SPAUSEPARAM
1035 if (mlx5_is_secondary())
1036 return -E_RTE_SECONDARY;
1038 ifr.ifr_data = (void *)ðpause;
1039 ethpause.autoneg = fc_conf->autoneg;
1040 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1041 (fc_conf->mode & RTE_FC_RX_PAUSE))
1042 ethpause.rx_pause = 1;
1044 ethpause.rx_pause = 0;
1046 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1047 (fc_conf->mode & RTE_FC_TX_PAUSE))
1048 ethpause.tx_pause = 1;
1050 ethpause.tx_pause = 0;
1053 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1055 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1069 * Get PCI information from struct ibv_device.
1072 * Pointer to Ethernet device structure.
1073 * @param[out] pci_addr
1074 * PCI bus address output buffer.
1077 * 0 on success, -1 on failure and errno is set.
1080 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1081 struct rte_pci_addr *pci_addr)
1085 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1087 file = fopen(path, "rb");
1090 while (fgets(line, sizeof(line), file) == line) {
1091 size_t len = strlen(line);
1094 /* Truncate long lines. */
1095 if (len == (sizeof(line) - 1))
1096 while (line[(len - 1)] != '\n') {
1100 line[(len - 1)] = ret;
1102 /* Extract information. */
1105 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1109 &pci_addr->function) == 4) {
1119 * Update the link status.
1122 * Pointer to private structure.
1125 * Zero if the callback process can be called immediately.
1128 priv_link_status_update(struct priv *priv)
1130 struct rte_eth_link *link = &priv->dev->data->dev_link;
1132 mlx5_link_update(priv->dev, 0);
1133 if (((link->link_speed == 0) && link->link_status) ||
1134 ((link->link_speed != 0) && !link->link_status)) {
1136 * Inconsistent status. Event likely occurred before the
1137 * kernel netdevice exposes the new status.
1139 if (!priv->pending_alarm) {
1140 priv->pending_alarm = 1;
1141 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1142 mlx5_dev_link_status_handler,
1146 } else if (unlikely(priv->pending_alarm)) {
1147 /* Link interrupt occurred while alarm is already scheduled. */
1148 priv->pending_alarm = 0;
1149 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1155 * Device status handler.
1158 * Pointer to private structure.
1160 * Pointer to event flags holder.
1163 * Events bitmap of callback process which can be called immediately.
1166 priv_dev_status_handler(struct priv *priv)
1168 struct ibv_async_event event;
1171 /* Read all message and acknowledge them. */
1173 if (ibv_get_async_event(priv->ctx, &event))
1175 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1176 event.event_type == IBV_EVENT_PORT_ERR) &&
1177 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1178 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1179 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1180 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1181 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1183 DEBUG("event type %d on port %d not handled",
1184 event.event_type, event.element.port_num);
1185 ibv_ack_async_event(&event);
1187 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1188 if (priv_link_status_update(priv))
1189 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1194 * Handle delayed link status event.
1197 * Registered argument.
1200 mlx5_dev_link_status_handler(void *arg)
1202 struct rte_eth_dev *dev = arg;
1203 struct priv *priv = dev->data->dev_private;
1207 assert(priv->pending_alarm == 1);
1208 priv->pending_alarm = 0;
1209 ret = priv_link_status_update(priv);
1212 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1217 * Handle interrupts from the NIC.
1219 * @param[in] intr_handle
1220 * Interrupt handler.
1222 * Callback argument.
1225 mlx5_dev_interrupt_handler(void *cb_arg)
1227 struct rte_eth_dev *dev = cb_arg;
1228 struct priv *priv = dev->data->dev_private;
1232 events = priv_dev_status_handler(priv);
1234 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1235 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1237 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1238 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL,
1243 * Handle interrupts from the socket.
1246 * Callback argument.
1249 mlx5_dev_handler_socket(void *cb_arg)
1251 struct rte_eth_dev *dev = cb_arg;
1252 struct priv *priv = dev->data->dev_private;
1255 priv_socket_handle(priv);
1260 * Uninstall interrupt handler.
1263 * Pointer to private structure.
1265 * Pointer to the rte_eth_dev structure.
1268 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1270 if (dev->data->dev_conf.intr_conf.lsc ||
1271 dev->data->dev_conf.intr_conf.rmv)
1272 rte_intr_callback_unregister(&priv->intr_handle,
1273 mlx5_dev_interrupt_handler, dev);
1274 if (priv->primary_socket)
1275 rte_intr_callback_unregister(&priv->intr_handle_socket,
1276 mlx5_dev_handler_socket, dev);
1277 if (priv->pending_alarm)
1278 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1279 priv->pending_alarm = 0;
1280 priv->intr_handle.fd = 0;
1281 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1282 priv->intr_handle_socket.fd = 0;
1283 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1287 * Install interrupt handler.
1290 * Pointer to private structure.
1292 * Pointer to the rte_eth_dev structure.
1295 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1299 assert(!mlx5_is_secondary());
1300 assert(priv->ctx->async_fd > 0);
1301 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1302 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1304 INFO("failed to change file descriptor async event queue");
1305 dev->data->dev_conf.intr_conf.lsc = 0;
1306 dev->data->dev_conf.intr_conf.rmv = 0;
1308 if (dev->data->dev_conf.intr_conf.lsc ||
1309 dev->data->dev_conf.intr_conf.rmv) {
1310 priv->intr_handle.fd = priv->ctx->async_fd;
1311 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1312 rte_intr_callback_register(&priv->intr_handle,
1313 mlx5_dev_interrupt_handler, dev);
1316 rc = priv_socket_init(priv);
1317 if (!rc && priv->primary_socket) {
1318 priv->intr_handle_socket.fd = priv->primary_socket;
1319 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1320 rte_intr_callback_register(&priv->intr_handle_socket,
1321 mlx5_dev_handler_socket, dev);
1326 * Change the link state (UP / DOWN).
1329 * Pointer to private data structure.
1331 * Pointer to rte_eth_dev structure.
1333 * Nonzero for link up, otherwise link down.
1336 * 0 on success, errno value on failure.
1339 priv_dev_set_link(struct priv *priv, struct rte_eth_dev *dev, int up)
1344 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1347 priv_dev_select_tx_function(priv, dev);
1348 priv_dev_select_rx_function(priv, dev);
1350 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1353 dev->rx_pkt_burst = removed_rx_burst;
1354 dev->tx_pkt_burst = removed_tx_burst;
1360 * DPDK callback to bring the link DOWN.
1363 * Pointer to Ethernet device structure.
1366 * 0 on success, errno value on failure.
1369 mlx5_set_link_down(struct rte_eth_dev *dev)
1371 struct priv *priv = dev->data->dev_private;
1375 err = priv_dev_set_link(priv, dev, 0);
1381 * DPDK callback to bring the link UP.
1384 * Pointer to Ethernet device structure.
1387 * 0 on success, errno value on failure.
1390 mlx5_set_link_up(struct rte_eth_dev *dev)
1392 struct priv *priv = dev->data->dev_private;
1396 err = priv_dev_set_link(priv, dev, 1);
1402 * Configure the TX function to use.
1405 * Pointer to private data structure.
1407 * Pointer to rte_eth_dev structure.
1410 priv_dev_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1412 assert(priv != NULL);
1413 assert(dev != NULL);
1414 dev->tx_pkt_burst = mlx5_tx_burst;
1415 /* Select appropriate TX function. */
1416 if (priv->mps == MLX5_MPW_ENHANCED) {
1417 if (priv_check_vec_tx_support(priv) > 0) {
1418 if (priv_check_raw_vec_tx_support(priv) > 0)
1419 dev->tx_pkt_burst = mlx5_tx_burst_raw_vec;
1421 dev->tx_pkt_burst = mlx5_tx_burst_vec;
1422 DEBUG("selected Enhanced MPW TX vectorized function");
1424 dev->tx_pkt_burst = mlx5_tx_burst_empw;
1425 DEBUG("selected Enhanced MPW TX function");
1427 } else if (priv->mps && priv->txq_inline) {
1428 dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1429 DEBUG("selected MPW inline TX function");
1430 } else if (priv->mps) {
1431 dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1432 DEBUG("selected MPW TX function");
1437 * Configure the RX function to use.
1440 * Pointer to private data structure.
1442 * Pointer to rte_eth_dev structure.
1445 priv_dev_select_rx_function(struct priv *priv, struct rte_eth_dev *dev)
1447 assert(priv != NULL);
1448 assert(dev != NULL);
1449 if (priv_check_vec_rx_support(priv) > 0) {
1450 dev->rx_pkt_burst = mlx5_rx_burst_vec;
1451 DEBUG("selected RX vectorized function");
1453 dev->rx_pkt_burst = mlx5_rx_burst;