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.
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/utsname.h>
47 #include <netinet/in.h>
48 #include <linux/ethtool.h>
49 #include <linux/sockios.h>
50 #include <linux/version.h>
53 #include <rte_atomic.h>
54 #include <rte_ethdev.h>
56 #include <rte_common.h>
57 #include <rte_interrupts.h>
58 #include <rte_alarm.h>
59 #include <rte_malloc.h>
62 #include "mlx5_rxtx.h"
63 #include "mlx5_utils.h"
65 /* Add defines in case the running kernel is not the same as user headers. */
66 #ifndef ETHTOOL_GLINKSETTINGS
67 struct ethtool_link_settings {
76 uint8_t eth_tp_mdix_ctrl;
77 int8_t link_mode_masks_nwords;
79 uint32_t link_mode_masks[];
82 #define ETHTOOL_GLINKSETTINGS 0x0000004c
83 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
84 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
85 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
86 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
87 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
88 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
89 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
90 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
91 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
92 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
93 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
94 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
95 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
96 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
97 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
98 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
100 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
101 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
102 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
103 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
105 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
106 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
107 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
109 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
110 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
111 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
112 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
113 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
115 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32 (SCHAR_MAX)
118 * Return private structure associated with an Ethernet device.
121 * Pointer to Ethernet device structure.
124 * Pointer to private structure.
127 mlx5_get_priv(struct rte_eth_dev *dev)
129 struct mlx5_secondary_data *sd;
131 if (!mlx5_is_secondary())
132 return dev->data->dev_private;
133 sd = &mlx5_secondary_data[dev->data->port_id];
134 return sd->data.dev_private;
138 * Check if running as a secondary process.
141 * Nonzero if running as a secondary process.
144 mlx5_is_secondary(void)
146 return rte_eal_process_type() != RTE_PROC_PRIMARY;
150 * Get interface name from private structure.
153 * Pointer to private structure.
155 * Interface name output buffer.
158 * 0 on success, -1 on failure and errno is set.
161 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
165 unsigned int dev_type = 0;
166 unsigned int dev_port_prev = ~0u;
167 char match[IF_NAMESIZE] = "";
170 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
176 while ((dent = readdir(dir)) != NULL) {
177 char *name = dent->d_name;
179 unsigned int dev_port;
182 if ((name[0] == '.') &&
183 ((name[1] == '\0') ||
184 ((name[1] == '.') && (name[2] == '\0'))))
187 MKSTR(path, "%s/device/net/%s/%s",
188 priv->ctx->device->ibdev_path, name,
189 (dev_type ? "dev_id" : "dev_port"));
191 file = fopen(path, "rb");
196 * Switch to dev_id when dev_port does not exist as
197 * is the case with Linux kernel versions < 3.15.
208 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
213 * Switch to dev_id when dev_port returns the same value for
214 * all ports. May happen when using a MOFED release older than
215 * 3.0 with a Linux kernel >= 3.15.
217 if (dev_port == dev_port_prev)
219 dev_port_prev = dev_port;
220 if (dev_port == (priv->port - 1u))
221 snprintf(match, sizeof(match), "%s", name);
224 if (match[0] == '\0')
226 strncpy(*ifname, match, sizeof(*ifname));
231 * Check if the counter is located on ib counters file.
237 * 1 if counter is located on ib counters file , 0 otherwise.
240 priv_is_ib_cntr(const char *cntr)
242 if (!strcmp(cntr, "out_of_buffer"))
248 * Read from sysfs entry.
251 * Pointer to private structure.
253 * Entry name relative to sysfs path.
255 * Data output buffer.
260 * 0 on success, -1 on failure and errno is set.
263 priv_sysfs_read(const struct priv *priv, const char *entry,
264 char *buf, size_t size)
266 char ifname[IF_NAMESIZE];
271 if (priv_get_ifname(priv, &ifname))
274 if (priv_is_ib_cntr(entry)) {
275 MKSTR(path, "%s/ports/1/hw_counters/%s",
276 priv->ctx->device->ibdev_path, entry);
277 file = fopen(path, "rb");
279 MKSTR(path, "%s/device/net/%s/%s",
280 priv->ctx->device->ibdev_path, ifname, entry);
281 file = fopen(path, "rb");
285 ret = fread(buf, 1, size, file);
287 if (((size_t)ret < size) && (ferror(file)))
297 * Write to sysfs entry.
300 * Pointer to private structure.
302 * Entry name relative to sysfs path.
309 * 0 on success, -1 on failure and errno is set.
312 priv_sysfs_write(const struct priv *priv, const char *entry,
313 char *buf, size_t size)
315 char ifname[IF_NAMESIZE];
320 if (priv_get_ifname(priv, &ifname))
323 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
326 file = fopen(path, "wb");
329 ret = fwrite(buf, 1, size, file);
331 if (((size_t)ret < size) || (ferror(file)))
341 * Get unsigned long sysfs property.
344 * Pointer to private structure.
346 * Entry name relative to sysfs path.
348 * Value output buffer.
351 * 0 on success, -1 on failure and errno is set.
354 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
357 unsigned long value_ret;
360 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
362 DEBUG("cannot read %s value from sysfs: %s",
363 name, strerror(errno));
366 value_str[ret] = '\0';
368 value_ret = strtoul(value_str, NULL, 0);
370 DEBUG("invalid %s value `%s': %s", name, value_str,
379 * Set unsigned long sysfs property.
382 * Pointer to private structure.
384 * Entry name relative to sysfs path.
389 * 0 on success, -1 on failure and errno is set.
392 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
395 MKSTR(value_str, "%lu", value);
397 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
399 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
400 name, value_str, value, strerror(errno));
407 * Perform ifreq ioctl() on associated Ethernet device.
410 * Pointer to private structure.
412 * Request number to pass to ioctl().
414 * Interface request structure output buffer.
417 * 0 on success, -1 on failure and errno is set.
420 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
422 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
427 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
428 ret = ioctl(sock, req, ifr);
434 * Return the number of active VFs for the current device.
437 * Pointer to private structure.
438 * @param[out] num_vfs
439 * Number of active VFs.
442 * 0 on success, -1 on failure and errno is set.
445 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
447 /* The sysfs entry name depends on the operating system. */
448 const char **name = (const char *[]){
449 "device/sriov_numvfs",
450 "device/mlx5_num_vfs",
456 unsigned long ulong_num_vfs;
458 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
460 *num_vfs = ulong_num_vfs;
461 } while (*(++name) && ret);
469 * Pointer to private structure.
471 * MTU value output buffer.
474 * 0 on success, -1 on failure and errno is set.
477 priv_get_mtu(struct priv *priv, uint16_t *mtu)
479 unsigned long ulong_mtu;
481 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
488 * Read device counter from sysfs.
491 * Pointer to private structure.
495 * Counter output buffer.
498 * 0 on success, -1 on failure and errno is set.
501 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
503 unsigned long ulong_ctr;
505 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
515 * Pointer to private structure.
520 * 0 on success, -1 on failure and errno is set.
523 priv_set_mtu(struct priv *priv, uint16_t mtu)
527 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
528 priv_get_mtu(priv, &new_mtu))
540 * Pointer to private structure.
542 * Bitmask for flags that must remain untouched.
544 * Bitmask for flags to modify.
547 * 0 on success, -1 on failure and errno is set.
550 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
554 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
557 tmp |= (flags & (~keep));
558 return priv_set_sysfs_ulong(priv, "flags", tmp);
562 * Ethernet device configuration.
564 * Prepare the driver for a given number of TX and RX queues.
567 * Pointer to Ethernet device structure.
570 * 0 on success, errno value on failure.
573 dev_configure(struct rte_eth_dev *dev)
575 struct priv *priv = dev->data->dev_private;
576 unsigned int rxqs_n = dev->data->nb_rx_queues;
577 unsigned int txqs_n = dev->data->nb_tx_queues;
580 unsigned int reta_idx_n;
582 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
583 priv->rxqs = (void *)dev->data->rx_queues;
584 priv->txqs = (void *)dev->data->tx_queues;
585 if (txqs_n != priv->txqs_n) {
586 INFO("%p: TX queues number update: %u -> %u",
587 (void *)dev, priv->txqs_n, txqs_n);
588 priv->txqs_n = txqs_n;
590 if (rxqs_n > priv->ind_table_max_size) {
591 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
594 if (rxqs_n == priv->rxqs_n)
596 INFO("%p: RX queues number update: %u -> %u",
597 (void *)dev, priv->rxqs_n, rxqs_n);
598 priv->rxqs_n = rxqs_n;
599 /* If the requested number of RX queues is not a power of two, use the
600 * maximum indirection table size for better balancing.
601 * The result is always rounded to the next power of two. */
602 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
603 priv->ind_table_max_size :
605 if (priv_rss_reta_index_resize(priv, reta_idx_n))
607 /* When the number of RX queues is not a power of two, the remaining
608 * table entries are padded with reused WQs and hashes are not spread
610 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
611 (*priv->reta_idx)[i] = j;
619 * DPDK callback for Ethernet device configuration.
622 * Pointer to Ethernet device structure.
625 * 0 on success, negative errno value on failure.
628 mlx5_dev_configure(struct rte_eth_dev *dev)
630 struct priv *priv = dev->data->dev_private;
633 if (mlx5_is_secondary())
634 return -E_RTE_SECONDARY;
637 ret = dev_configure(dev);
644 * DPDK callback to get information about the device.
647 * Pointer to Ethernet device structure.
649 * Info structure output buffer.
652 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
654 struct priv *priv = mlx5_get_priv(dev);
656 char ifname[IF_NAMESIZE];
658 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
661 /* FIXME: we should ask the device for these values. */
662 info->min_rx_bufsize = 32;
663 info->max_rx_pktlen = 65536;
665 * Since we need one CQ per QP, the limit is the minimum number
666 * between the two values.
668 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
669 priv->device_attr.max_qp : priv->device_attr.max_cq);
670 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
673 info->max_rx_queues = max;
674 info->max_tx_queues = max;
675 info->max_mac_addrs = RTE_DIM(priv->mac);
676 info->rx_offload_capa =
678 (DEV_RX_OFFLOAD_IPV4_CKSUM |
679 DEV_RX_OFFLOAD_UDP_CKSUM |
680 DEV_RX_OFFLOAD_TCP_CKSUM) :
682 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
684 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
686 info->tx_offload_capa |=
687 (DEV_TX_OFFLOAD_IPV4_CKSUM |
688 DEV_TX_OFFLOAD_UDP_CKSUM |
689 DEV_TX_OFFLOAD_TCP_CKSUM);
691 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
693 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
694 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
695 DEV_TX_OFFLOAD_GRE_TNL_TSO);
696 if (priv_get_ifname(priv, &ifname) == 0)
697 info->if_index = if_nametoindex(ifname);
698 info->reta_size = priv->reta_idx_n ?
699 priv->reta_idx_n : priv->ind_table_max_size;
700 info->hash_key_size = ((*priv->rss_conf) ?
701 (*priv->rss_conf)[0]->rss_key_len :
703 info->speed_capa = priv->link_speed_capa;
708 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
710 static const uint32_t ptypes[] = {
711 /* refers to rxq_cq_to_pkt_type() */
713 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
714 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
715 RTE_PTYPE_L4_NONFRAG,
719 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
720 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
721 RTE_PTYPE_INNER_L4_NONFRAG,
722 RTE_PTYPE_INNER_L4_FRAG,
723 RTE_PTYPE_INNER_L4_TCP,
724 RTE_PTYPE_INNER_L4_UDP,
728 if (dev->rx_pkt_burst == mlx5_rx_burst ||
729 dev->rx_pkt_burst == mlx5_rx_burst_vec)
735 * DPDK callback to retrieve physical link information.
738 * Pointer to Ethernet device structure.
739 * @param wait_to_complete
740 * Wait for request completion (ignored).
743 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
745 struct priv *priv = mlx5_get_priv(dev);
746 struct ethtool_cmd edata = {
747 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
750 struct rte_eth_link dev_link;
753 /* priv_lock() is not taken to allow concurrent calls. */
755 (void)wait_to_complete;
756 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
757 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
760 memset(&dev_link, 0, sizeof(dev_link));
761 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
762 (ifr.ifr_flags & IFF_RUNNING));
763 ifr.ifr_data = (void *)&edata;
764 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
765 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
769 link_speed = ethtool_cmd_speed(&edata);
770 if (link_speed == -1)
771 dev_link.link_speed = 0;
773 dev_link.link_speed = link_speed;
774 priv->link_speed_capa = 0;
775 if (edata.supported & SUPPORTED_Autoneg)
776 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
777 if (edata.supported & (SUPPORTED_1000baseT_Full |
778 SUPPORTED_1000baseKX_Full))
779 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
780 if (edata.supported & SUPPORTED_10000baseKR_Full)
781 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
782 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
783 SUPPORTED_40000baseCR4_Full |
784 SUPPORTED_40000baseSR4_Full |
785 SUPPORTED_40000baseLR4_Full))
786 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
787 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
788 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
789 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
790 ETH_LINK_SPEED_FIXED);
791 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
792 /* Link status changed. */
793 dev->data->dev_link = dev_link;
796 /* Link status is still the same. */
801 * Retrieve physical link information (unlocked version using new ioctl).
804 * Pointer to Ethernet device structure.
805 * @param wait_to_complete
806 * Wait for request completion (ignored).
809 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
811 struct priv *priv = mlx5_get_priv(dev);
812 __extension__ struct {
813 struct ethtool_link_settings edata;
814 uint32_t link_mode_data[3 *
815 ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32];
819 struct rte_eth_link dev_link;
822 (void)wait_to_complete;
823 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
824 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
827 memset(&dev_link, 0, sizeof(dev_link));
828 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
829 (ifr.ifr_flags & IFF_RUNNING));
830 memset(&ecmd, 0, sizeof(ecmd));
831 ecmd.edata.cmd = ETHTOOL_GLINKSETTINGS;
832 ifr.ifr_data = (void *)&ecmd;
833 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
834 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
838 ecmd.edata.link_mode_masks_nwords = -ecmd.edata.link_mode_masks_nwords;
839 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
840 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
844 dev_link.link_speed = ecmd.edata.speed;
845 sc = ecmd.edata.link_mode_masks[0] |
846 ((uint64_t)ecmd.edata.link_mode_masks[1] << 32);
847 priv->link_speed_capa = 0;
848 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
849 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
850 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
851 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
852 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
853 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
854 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
855 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
856 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
857 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
858 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
859 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
860 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
861 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
862 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
863 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
864 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
865 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
866 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
867 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
868 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
869 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
870 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
871 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
872 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
873 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
874 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
875 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
876 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
877 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
878 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
879 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
880 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
881 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
882 dev_link.link_duplex = ((ecmd.edata.duplex == DUPLEX_HALF) ?
883 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
884 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
885 ETH_LINK_SPEED_FIXED);
886 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
887 /* Link status changed. */
888 dev->data->dev_link = dev_link;
891 /* Link status is still the same. */
896 * DPDK callback to retrieve physical link information.
899 * Pointer to Ethernet device structure.
900 * @param wait_to_complete
901 * Wait for request completion (ignored).
904 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
906 struct utsname utsname;
909 if (uname(&utsname) == -1 ||
910 sscanf(utsname.release, "%d.%d.%d",
911 &ver[0], &ver[1], &ver[2]) != 3 ||
912 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
913 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
914 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
918 * DPDK callback to change the MTU.
921 * Pointer to Ethernet device structure.
926 * 0 on success, negative errno value on failure.
929 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
931 struct priv *priv = dev->data->dev_private;
935 if (mlx5_is_secondary())
936 return -E_RTE_SECONDARY;
939 ret = priv_get_mtu(priv, &kern_mtu);
942 /* Set kernel interface MTU first. */
943 ret = priv_set_mtu(priv, mtu);
946 ret = priv_get_mtu(priv, &kern_mtu);
949 if (kern_mtu == mtu) {
951 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
957 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
965 * DPDK callback to get flow control status.
968 * Pointer to Ethernet device structure.
969 * @param[out] fc_conf
970 * Flow control output buffer.
973 * 0 on success, negative errno value on failure.
976 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
978 struct priv *priv = dev->data->dev_private;
980 struct ethtool_pauseparam ethpause = {
981 .cmd = ETHTOOL_GPAUSEPARAM
985 if (mlx5_is_secondary())
986 return -E_RTE_SECONDARY;
988 ifr.ifr_data = (void *)ðpause;
990 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
992 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
998 fc_conf->autoneg = ethpause.autoneg;
999 if (ethpause.rx_pause && ethpause.tx_pause)
1000 fc_conf->mode = RTE_FC_FULL;
1001 else if (ethpause.rx_pause)
1002 fc_conf->mode = RTE_FC_RX_PAUSE;
1003 else if (ethpause.tx_pause)
1004 fc_conf->mode = RTE_FC_TX_PAUSE;
1006 fc_conf->mode = RTE_FC_NONE;
1016 * DPDK callback to modify flow control parameters.
1019 * Pointer to Ethernet device structure.
1020 * @param[in] fc_conf
1021 * Flow control parameters.
1024 * 0 on success, negative errno value on failure.
1027 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1029 struct priv *priv = dev->data->dev_private;
1031 struct ethtool_pauseparam ethpause = {
1032 .cmd = ETHTOOL_SPAUSEPARAM
1036 if (mlx5_is_secondary())
1037 return -E_RTE_SECONDARY;
1039 ifr.ifr_data = (void *)ðpause;
1040 ethpause.autoneg = fc_conf->autoneg;
1041 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1042 (fc_conf->mode & RTE_FC_RX_PAUSE))
1043 ethpause.rx_pause = 1;
1045 ethpause.rx_pause = 0;
1047 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1048 (fc_conf->mode & RTE_FC_TX_PAUSE))
1049 ethpause.tx_pause = 1;
1051 ethpause.tx_pause = 0;
1054 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1056 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1070 * Get PCI information from struct ibv_device.
1073 * Pointer to Ethernet device structure.
1074 * @param[out] pci_addr
1075 * PCI bus address output buffer.
1078 * 0 on success, -1 on failure and errno is set.
1081 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1082 struct rte_pci_addr *pci_addr)
1086 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1088 file = fopen(path, "rb");
1091 while (fgets(line, sizeof(line), file) == line) {
1092 size_t len = strlen(line);
1095 /* Truncate long lines. */
1096 if (len == (sizeof(line) - 1))
1097 while (line[(len - 1)] != '\n') {
1101 line[(len - 1)] = ret;
1103 /* Extract information. */
1106 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1110 &pci_addr->function) == 4) {
1120 * Link status handler.
1123 * Pointer to private structure.
1125 * Pointer to the rte_eth_dev structure.
1128 * Nonzero if the callback process can be called immediately.
1131 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
1133 struct ibv_async_event event;
1134 struct rte_eth_link *link = &dev->data->dev_link;
1137 /* Read all message and acknowledge them. */
1139 if (ibv_get_async_event(priv->ctx, &event))
1142 if (event.event_type != IBV_EVENT_PORT_ACTIVE &&
1143 event.event_type != IBV_EVENT_PORT_ERR)
1144 DEBUG("event type %d on port %d not handled",
1145 event.event_type, event.element.port_num);
1146 ibv_ack_async_event(&event);
1148 mlx5_link_update(dev, 0);
1149 if (((link->link_speed == 0) && link->link_status) ||
1150 ((link->link_speed != 0) && !link->link_status)) {
1151 if (!priv->pending_alarm) {
1152 /* Inconsistent status, check again later. */
1153 priv->pending_alarm = 1;
1154 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1155 mlx5_dev_link_status_handler,
1165 * Handle delayed link status event.
1168 * Registered argument.
1171 mlx5_dev_link_status_handler(void *arg)
1173 struct rte_eth_dev *dev = arg;
1174 struct priv *priv = dev->data->dev_private;
1178 assert(priv->pending_alarm == 1);
1179 priv->pending_alarm = 0;
1180 ret = priv_dev_link_status_handler(priv, dev);
1183 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1188 * Handle interrupts from the NIC.
1190 * @param[in] intr_handle
1191 * Interrupt handler.
1193 * Callback argument.
1196 mlx5_dev_interrupt_handler(void *cb_arg)
1198 struct rte_eth_dev *dev = cb_arg;
1199 struct priv *priv = dev->data->dev_private;
1203 ret = priv_dev_link_status_handler(priv, dev);
1206 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1211 * Uninstall interrupt handler.
1214 * Pointer to private structure.
1216 * Pointer to the rte_eth_dev structure.
1219 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1221 if (!dev->data->dev_conf.intr_conf.lsc)
1223 rte_intr_callback_unregister(&priv->intr_handle,
1224 mlx5_dev_interrupt_handler,
1226 if (priv->pending_alarm)
1227 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1228 priv->pending_alarm = 0;
1229 priv->intr_handle.fd = 0;
1230 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1234 * Install interrupt handler.
1237 * Pointer to private structure.
1239 * Pointer to the rte_eth_dev structure.
1242 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1246 if (!dev->data->dev_conf.intr_conf.lsc)
1248 assert(priv->ctx->async_fd > 0);
1249 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1250 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1252 INFO("failed to change file descriptor async event queue");
1253 dev->data->dev_conf.intr_conf.lsc = 0;
1255 priv->intr_handle.fd = priv->ctx->async_fd;
1256 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1257 rte_intr_callback_register(&priv->intr_handle,
1258 mlx5_dev_interrupt_handler,
1264 * Change the link state (UP / DOWN).
1267 * Pointer to Ethernet device structure.
1269 * Nonzero for link up, otherwise link down.
1272 * 0 on success, errno value on failure.
1275 priv_set_link(struct priv *priv, int up)
1277 struct rte_eth_dev *dev = priv->dev;
1281 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1284 priv_select_tx_function(priv);
1285 priv_select_rx_function(priv);
1287 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1290 dev->rx_pkt_burst = removed_rx_burst;
1291 dev->tx_pkt_burst = removed_tx_burst;
1297 * DPDK callback to bring the link DOWN.
1300 * Pointer to Ethernet device structure.
1303 * 0 on success, errno value on failure.
1306 mlx5_set_link_down(struct rte_eth_dev *dev)
1308 struct priv *priv = dev->data->dev_private;
1312 err = priv_set_link(priv, 0);
1318 * DPDK callback to bring the link UP.
1321 * Pointer to Ethernet device structure.
1324 * 0 on success, errno value on failure.
1327 mlx5_set_link_up(struct rte_eth_dev *dev)
1329 struct priv *priv = dev->data->dev_private;
1333 err = priv_set_link(priv, 1);
1339 * Configure secondary process queues from a private data pointer (primary
1340 * or secondary) and update burst callbacks. Can take place only once.
1342 * All queues must have been previously created by the primary process to
1343 * avoid undefined behavior.
1346 * Private data pointer from either primary or secondary process.
1349 * Private data pointer from secondary process, NULL in case of error.
1352 mlx5_secondary_data_setup(struct priv *priv)
1354 unsigned int port_id = 0;
1355 struct mlx5_secondary_data *sd;
1358 unsigned int nb_tx_queues;
1359 unsigned int nb_rx_queues;
1362 /* priv must be valid at this point. */
1363 assert(priv != NULL);
1364 /* priv->dev must also be valid but may point to local memory from
1365 * another process, possibly with the same address and must not
1366 * be dereferenced yet. */
1367 assert(priv->dev != NULL);
1368 /* Determine port ID by finding out where priv comes from. */
1370 sd = &mlx5_secondary_data[port_id];
1371 rte_spinlock_lock(&sd->lock);
1372 /* Primary process? */
1373 if (sd->primary_priv == priv)
1375 /* Secondary process? */
1376 if (sd->data.dev_private == priv)
1378 rte_spinlock_unlock(&sd->lock);
1379 if (++port_id == RTE_DIM(mlx5_secondary_data))
1382 /* Switch to secondary private structure. If private data has already
1383 * been updated by another thread, there is nothing else to do. */
1384 priv = sd->data.dev_private;
1385 if (priv->dev->data == &sd->data)
1387 /* Sanity checks. Secondary private structure is supposed to point
1388 * to local eth_dev, itself still pointing to the shared device data
1389 * structure allocated by the primary process. */
1390 assert(sd->shared_dev_data != &sd->data);
1391 assert(sd->data.nb_tx_queues == 0);
1392 assert(sd->data.tx_queues == NULL);
1393 assert(sd->data.nb_rx_queues == 0);
1394 assert(sd->data.rx_queues == NULL);
1395 assert(priv != sd->primary_priv);
1396 assert(priv->dev->data == sd->shared_dev_data);
1397 assert(priv->txqs_n == 0);
1398 assert(priv->txqs == NULL);
1399 assert(priv->rxqs_n == 0);
1400 assert(priv->rxqs == NULL);
1401 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1402 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1403 /* Allocate local storage for queues. */
1404 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1405 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1406 RTE_CACHE_LINE_SIZE);
1407 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1408 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1409 RTE_CACHE_LINE_SIZE);
1410 if (tx_queues == NULL || rx_queues == NULL)
1412 /* Lock to prevent control operations during setup. */
1415 for (i = 0; i != nb_tx_queues; ++i) {
1416 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1417 struct txq_ctrl *primary_txq_ctrl;
1418 struct txq_ctrl *txq_ctrl;
1420 if (primary_txq == NULL)
1422 primary_txq_ctrl = container_of(primary_txq,
1423 struct txq_ctrl, txq);
1424 txq_ctrl = rte_calloc_socket("TXQ", 1, sizeof(*txq_ctrl) +
1425 (1 << primary_txq->elts_n) *
1426 sizeof(struct rte_mbuf *), 0,
1427 primary_txq_ctrl->socket);
1428 if (txq_ctrl != NULL) {
1429 if (txq_ctrl_setup(priv->dev,
1431 1 << primary_txq->elts_n,
1432 primary_txq_ctrl->socket,
1434 txq_ctrl->txq.stats.idx =
1435 primary_txq->stats.idx;
1436 tx_queues[i] = &txq_ctrl->txq;
1442 txq_ctrl = tx_queues[--i];
1443 txq_cleanup(txq_ctrl);
1449 for (i = 0; i != nb_rx_queues; ++i) {
1450 struct rxq_ctrl *primary_rxq =
1451 container_of((*sd->primary_priv->rxqs)[i],
1452 struct rxq_ctrl, rxq);
1454 if (primary_rxq == NULL)
1456 /* Not supported yet. */
1457 rx_queues[i] = NULL;
1459 /* Update everything. */
1460 priv->txqs = (void *)tx_queues;
1461 priv->txqs_n = nb_tx_queues;
1462 priv->rxqs = (void *)rx_queues;
1463 priv->rxqs_n = nb_rx_queues;
1464 sd->data.rx_queues = rx_queues;
1465 sd->data.tx_queues = tx_queues;
1466 sd->data.nb_rx_queues = nb_rx_queues;
1467 sd->data.nb_tx_queues = nb_tx_queues;
1468 sd->data.dev_link = sd->shared_dev_data->dev_link;
1469 sd->data.mtu = sd->shared_dev_data->mtu;
1470 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1471 sizeof(sd->data.rx_queue_state));
1472 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1473 sizeof(sd->data.tx_queue_state));
1474 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1475 /* Use local data from now on. */
1477 priv->dev->data = &sd->data;
1479 priv_select_tx_function(priv);
1480 priv_select_rx_function(priv);
1483 /* More sanity checks. */
1484 assert(priv->dev->data == &sd->data);
1485 rte_spinlock_unlock(&sd->lock);
1489 rte_free(tx_queues);
1490 rte_free(rx_queues);
1491 rte_spinlock_unlock(&sd->lock);
1496 * Configure the TX function to use.
1499 * Pointer to private structure.
1502 priv_select_tx_function(struct priv *priv)
1504 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1505 /* Select appropriate TX function. */
1506 if (priv->mps == MLX5_MPW_ENHANCED) {
1507 if (priv_check_vec_tx_support(priv) > 0) {
1508 if (priv_check_raw_vec_tx_support(priv) > 0)
1509 priv->dev->tx_pkt_burst = mlx5_tx_burst_raw_vec;
1511 priv->dev->tx_pkt_burst = mlx5_tx_burst_vec;
1512 DEBUG("selected Enhanced MPW TX vectorized function");
1514 priv->dev->tx_pkt_burst = mlx5_tx_burst_empw;
1515 DEBUG("selected Enhanced MPW TX function");
1517 } else if (priv->mps && priv->txq_inline) {
1518 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1519 DEBUG("selected MPW inline TX function");
1520 } else if (priv->mps) {
1521 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1522 DEBUG("selected MPW TX function");
1527 * Configure the RX function to use.
1530 * Pointer to private structure.
1533 priv_select_rx_function(struct priv *priv)
1535 if (priv_check_vec_rx_support(priv) > 0) {
1536 priv->dev->rx_pkt_burst = mlx5_rx_burst_vec;
1537 DEBUG("selected RX vectorized function");
1539 priv->dev->rx_pkt_burst = mlx5_rx_burst;