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 return dev->data->dev_private;
133 * Check if running as a secondary process.
136 * Nonzero if running as a secondary process.
139 mlx5_is_secondary(void)
141 return rte_eal_process_type() == RTE_PROC_SECONDARY;
145 * Get interface name from private structure.
148 * Pointer to private structure.
150 * Interface name output buffer.
153 * 0 on success, -1 on failure and errno is set.
156 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
160 unsigned int dev_type = 0;
161 unsigned int dev_port_prev = ~0u;
162 char match[IF_NAMESIZE] = "";
165 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
171 while ((dent = readdir(dir)) != NULL) {
172 char *name = dent->d_name;
174 unsigned int dev_port;
177 if ((name[0] == '.') &&
178 ((name[1] == '\0') ||
179 ((name[1] == '.') && (name[2] == '\0'))))
182 MKSTR(path, "%s/device/net/%s/%s",
183 priv->ctx->device->ibdev_path, name,
184 (dev_type ? "dev_id" : "dev_port"));
186 file = fopen(path, "rb");
191 * Switch to dev_id when dev_port does not exist as
192 * is the case with Linux kernel versions < 3.15.
203 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
208 * Switch to dev_id when dev_port returns the same value for
209 * all ports. May happen when using a MOFED release older than
210 * 3.0 with a Linux kernel >= 3.15.
212 if (dev_port == dev_port_prev)
214 dev_port_prev = dev_port;
215 if (dev_port == (priv->port - 1u))
216 snprintf(match, sizeof(match), "%s", name);
219 if (match[0] == '\0')
221 strncpy(*ifname, match, sizeof(*ifname));
226 * Check if the counter is located on ib counters file.
232 * 1 if counter is located on ib counters file , 0 otherwise.
235 priv_is_ib_cntr(const char *cntr)
237 if (!strcmp(cntr, "out_of_buffer"))
243 * Read from sysfs entry.
246 * Pointer to private structure.
248 * Entry name relative to sysfs path.
250 * Data output buffer.
255 * 0 on success, -1 on failure and errno is set.
258 priv_sysfs_read(const struct priv *priv, const char *entry,
259 char *buf, size_t size)
261 char ifname[IF_NAMESIZE];
266 if (priv_get_ifname(priv, &ifname))
269 if (priv_is_ib_cntr(entry)) {
270 MKSTR(path, "%s/ports/1/hw_counters/%s",
271 priv->ctx->device->ibdev_path, entry);
272 file = fopen(path, "rb");
274 MKSTR(path, "%s/device/net/%s/%s",
275 priv->ctx->device->ibdev_path, ifname, entry);
276 file = fopen(path, "rb");
280 ret = fread(buf, 1, size, file);
282 if (((size_t)ret < size) && (ferror(file)))
292 * Write to sysfs entry.
295 * Pointer to private structure.
297 * Entry name relative to sysfs path.
304 * 0 on success, -1 on failure and errno is set.
307 priv_sysfs_write(const struct priv *priv, const char *entry,
308 char *buf, size_t size)
310 char ifname[IF_NAMESIZE];
315 if (priv_get_ifname(priv, &ifname))
318 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
321 file = fopen(path, "wb");
324 ret = fwrite(buf, 1, size, file);
326 if (((size_t)ret < size) || (ferror(file)))
336 * Get unsigned long sysfs property.
339 * Pointer to private structure.
341 * Entry name relative to sysfs path.
343 * Value output buffer.
346 * 0 on success, -1 on failure and errno is set.
349 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
352 unsigned long value_ret;
355 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
357 DEBUG("cannot read %s value from sysfs: %s",
358 name, strerror(errno));
361 value_str[ret] = '\0';
363 value_ret = strtoul(value_str, NULL, 0);
365 DEBUG("invalid %s value `%s': %s", name, value_str,
374 * Set unsigned long sysfs property.
377 * Pointer to private structure.
379 * Entry name relative to sysfs path.
384 * 0 on success, -1 on failure and errno is set.
387 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
390 MKSTR(value_str, "%lu", value);
392 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
394 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
395 name, value_str, value, strerror(errno));
402 * Perform ifreq ioctl() on associated Ethernet device.
405 * Pointer to private structure.
407 * Request number to pass to ioctl().
409 * Interface request structure output buffer.
412 * 0 on success, -1 on failure and errno is set.
415 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
417 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
422 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
423 ret = ioctl(sock, req, ifr);
429 * Return the number of active VFs for the current device.
432 * Pointer to private structure.
433 * @param[out] num_vfs
434 * Number of active VFs.
437 * 0 on success, -1 on failure and errno is set.
440 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
442 /* The sysfs entry name depends on the operating system. */
443 const char **name = (const char *[]){
444 "device/sriov_numvfs",
445 "device/mlx5_num_vfs",
451 unsigned long ulong_num_vfs;
453 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
455 *num_vfs = ulong_num_vfs;
456 } while (*(++name) && ret);
464 * Pointer to private structure.
466 * MTU value output buffer.
469 * 0 on success, -1 on failure and errno is set.
472 priv_get_mtu(struct priv *priv, uint16_t *mtu)
474 unsigned long ulong_mtu;
476 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
483 * Read device counter from sysfs.
486 * Pointer to private structure.
490 * Counter output buffer.
493 * 0 on success, -1 on failure and errno is set.
496 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
498 unsigned long ulong_ctr;
500 if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
510 * Pointer to private structure.
515 * 0 on success, -1 on failure and errno is set.
518 priv_set_mtu(struct priv *priv, uint16_t mtu)
522 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
523 priv_get_mtu(priv, &new_mtu))
535 * Pointer to private structure.
537 * Bitmask for flags that must remain untouched.
539 * Bitmask for flags to modify.
542 * 0 on success, -1 on failure and errno is set.
545 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
549 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
552 tmp |= (flags & (~keep));
553 return priv_set_sysfs_ulong(priv, "flags", tmp);
557 * Ethernet device configuration.
559 * Prepare the driver for a given number of TX and RX queues.
562 * Pointer to Ethernet device structure.
565 * 0 on success, errno value on failure.
568 dev_configure(struct rte_eth_dev *dev)
570 struct priv *priv = dev->data->dev_private;
571 unsigned int rxqs_n = dev->data->nb_rx_queues;
572 unsigned int txqs_n = dev->data->nb_tx_queues;
575 unsigned int reta_idx_n;
577 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
578 priv->rxqs = (void *)dev->data->rx_queues;
579 priv->txqs = (void *)dev->data->tx_queues;
580 if (txqs_n != priv->txqs_n) {
581 INFO("%p: TX queues number update: %u -> %u",
582 (void *)dev, priv->txqs_n, txqs_n);
583 priv->txqs_n = txqs_n;
585 if (rxqs_n > priv->ind_table_max_size) {
586 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
589 if (rxqs_n == priv->rxqs_n)
591 INFO("%p: RX queues number update: %u -> %u",
592 (void *)dev, priv->rxqs_n, rxqs_n);
593 priv->rxqs_n = rxqs_n;
594 /* If the requested number of RX queues is not a power of two, use the
595 * maximum indirection table size for better balancing.
596 * The result is always rounded to the next power of two. */
597 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
598 priv->ind_table_max_size :
600 if (priv_rss_reta_index_resize(priv, reta_idx_n))
602 /* When the number of RX queues is not a power of two, the remaining
603 * table entries are padded with reused WQs and hashes are not spread
605 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
606 (*priv->reta_idx)[i] = j;
614 * DPDK callback for Ethernet device configuration.
617 * Pointer to Ethernet device structure.
620 * 0 on success, negative errno value on failure.
623 mlx5_dev_configure(struct rte_eth_dev *dev)
625 struct priv *priv = dev->data->dev_private;
628 if (mlx5_is_secondary())
629 return -E_RTE_SECONDARY;
632 ret = dev_configure(dev);
639 * DPDK callback to get information about the device.
642 * Pointer to Ethernet device structure.
644 * Info structure output buffer.
647 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
649 struct priv *priv = mlx5_get_priv(dev);
651 char ifname[IF_NAMESIZE];
653 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
656 /* FIXME: we should ask the device for these values. */
657 info->min_rx_bufsize = 32;
658 info->max_rx_pktlen = 65536;
660 * Since we need one CQ per QP, the limit is the minimum number
661 * between the two values.
663 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
664 priv->device_attr.max_qp : priv->device_attr.max_cq);
665 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
668 info->max_rx_queues = max;
669 info->max_tx_queues = max;
670 info->max_mac_addrs = RTE_DIM(priv->mac);
671 info->rx_offload_capa =
673 (DEV_RX_OFFLOAD_IPV4_CKSUM |
674 DEV_RX_OFFLOAD_UDP_CKSUM |
675 DEV_RX_OFFLOAD_TCP_CKSUM) :
677 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
679 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
681 info->tx_offload_capa |=
682 (DEV_TX_OFFLOAD_IPV4_CKSUM |
683 DEV_TX_OFFLOAD_UDP_CKSUM |
684 DEV_TX_OFFLOAD_TCP_CKSUM);
686 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
688 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
689 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
690 DEV_TX_OFFLOAD_GRE_TNL_TSO);
691 if (priv_get_ifname(priv, &ifname) == 0)
692 info->if_index = if_nametoindex(ifname);
693 info->reta_size = priv->reta_idx_n ?
694 priv->reta_idx_n : priv->ind_table_max_size;
695 info->hash_key_size = ((*priv->rss_conf) ?
696 (*priv->rss_conf)[0]->rss_key_len :
698 info->speed_capa = priv->link_speed_capa;
703 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
705 static const uint32_t ptypes[] = {
706 /* refers to rxq_cq_to_pkt_type() */
708 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
709 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
710 RTE_PTYPE_L4_NONFRAG,
714 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
715 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
716 RTE_PTYPE_INNER_L4_NONFRAG,
717 RTE_PTYPE_INNER_L4_FRAG,
718 RTE_PTYPE_INNER_L4_TCP,
719 RTE_PTYPE_INNER_L4_UDP,
723 if (dev->rx_pkt_burst == mlx5_rx_burst ||
724 dev->rx_pkt_burst == mlx5_rx_burst_vec)
730 * DPDK callback to retrieve physical link information.
733 * Pointer to Ethernet device structure.
734 * @param wait_to_complete
735 * Wait for request completion (ignored).
738 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
740 struct priv *priv = mlx5_get_priv(dev);
741 struct ethtool_cmd edata = {
742 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
745 struct rte_eth_link dev_link;
748 /* priv_lock() is not taken to allow concurrent calls. */
750 (void)wait_to_complete;
751 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
752 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
755 memset(&dev_link, 0, sizeof(dev_link));
756 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
757 (ifr.ifr_flags & IFF_RUNNING));
758 ifr.ifr_data = (void *)&edata;
759 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
760 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
764 link_speed = ethtool_cmd_speed(&edata);
765 if (link_speed == -1)
766 dev_link.link_speed = 0;
768 dev_link.link_speed = link_speed;
769 priv->link_speed_capa = 0;
770 if (edata.supported & SUPPORTED_Autoneg)
771 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
772 if (edata.supported & (SUPPORTED_1000baseT_Full |
773 SUPPORTED_1000baseKX_Full))
774 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
775 if (edata.supported & SUPPORTED_10000baseKR_Full)
776 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
777 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
778 SUPPORTED_40000baseCR4_Full |
779 SUPPORTED_40000baseSR4_Full |
780 SUPPORTED_40000baseLR4_Full))
781 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
782 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
783 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
784 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
785 ETH_LINK_SPEED_FIXED);
786 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
787 /* Link status changed. */
788 dev->data->dev_link = dev_link;
791 /* Link status is still the same. */
796 * Retrieve physical link information (unlocked version using new ioctl).
799 * Pointer to Ethernet device structure.
800 * @param wait_to_complete
801 * Wait for request completion (ignored).
804 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
806 struct priv *priv = mlx5_get_priv(dev);
807 __extension__ struct {
808 struct ethtool_link_settings edata;
809 uint32_t link_mode_data[3 *
810 ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32];
814 struct rte_eth_link dev_link;
817 (void)wait_to_complete;
818 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
819 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
822 memset(&dev_link, 0, sizeof(dev_link));
823 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
824 (ifr.ifr_flags & IFF_RUNNING));
825 memset(&ecmd, 0, sizeof(ecmd));
826 ecmd.edata.cmd = ETHTOOL_GLINKSETTINGS;
827 ifr.ifr_data = (void *)&ecmd;
828 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
829 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
833 ecmd.edata.link_mode_masks_nwords = -ecmd.edata.link_mode_masks_nwords;
834 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
835 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
839 dev_link.link_speed = ecmd.edata.speed;
840 sc = ecmd.edata.link_mode_masks[0] |
841 ((uint64_t)ecmd.edata.link_mode_masks[1] << 32);
842 priv->link_speed_capa = 0;
843 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
844 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
845 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
846 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
847 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
848 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
849 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
850 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
851 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
852 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
853 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
854 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
855 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
856 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
857 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
858 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
859 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
860 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
861 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
862 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
863 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
864 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
865 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
866 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
867 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
868 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
869 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
870 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
871 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
872 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
873 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
874 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
875 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
876 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
877 dev_link.link_duplex = ((ecmd.edata.duplex == DUPLEX_HALF) ?
878 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
879 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
880 ETH_LINK_SPEED_FIXED);
881 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
882 /* Link status changed. */
883 dev->data->dev_link = dev_link;
886 /* Link status is still the same. */
891 * DPDK callback to retrieve physical link information.
894 * Pointer to Ethernet device structure.
895 * @param wait_to_complete
896 * Wait for request completion (ignored).
899 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
901 struct utsname utsname;
904 if (uname(&utsname) == -1 ||
905 sscanf(utsname.release, "%d.%d.%d",
906 &ver[0], &ver[1], &ver[2]) != 3 ||
907 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
908 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
909 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
913 * DPDK callback to change the MTU.
916 * Pointer to Ethernet device structure.
921 * 0 on success, negative errno value on failure.
924 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
926 struct priv *priv = dev->data->dev_private;
930 if (mlx5_is_secondary())
931 return -E_RTE_SECONDARY;
934 ret = priv_get_mtu(priv, &kern_mtu);
937 /* Set kernel interface MTU first. */
938 ret = priv_set_mtu(priv, mtu);
941 ret = priv_get_mtu(priv, &kern_mtu);
944 if (kern_mtu == mtu) {
946 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
952 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
960 * DPDK callback to get flow control status.
963 * Pointer to Ethernet device structure.
964 * @param[out] fc_conf
965 * Flow control output buffer.
968 * 0 on success, negative errno value on failure.
971 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
973 struct priv *priv = dev->data->dev_private;
975 struct ethtool_pauseparam ethpause = {
976 .cmd = ETHTOOL_GPAUSEPARAM
980 if (mlx5_is_secondary())
981 return -E_RTE_SECONDARY;
983 ifr.ifr_data = (void *)ðpause;
985 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
987 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
993 fc_conf->autoneg = ethpause.autoneg;
994 if (ethpause.rx_pause && ethpause.tx_pause)
995 fc_conf->mode = RTE_FC_FULL;
996 else if (ethpause.rx_pause)
997 fc_conf->mode = RTE_FC_RX_PAUSE;
998 else if (ethpause.tx_pause)
999 fc_conf->mode = RTE_FC_TX_PAUSE;
1001 fc_conf->mode = RTE_FC_NONE;
1011 * DPDK callback to modify flow control parameters.
1014 * Pointer to Ethernet device structure.
1015 * @param[in] fc_conf
1016 * Flow control parameters.
1019 * 0 on success, negative errno value on failure.
1022 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1024 struct priv *priv = dev->data->dev_private;
1026 struct ethtool_pauseparam ethpause = {
1027 .cmd = ETHTOOL_SPAUSEPARAM
1031 if (mlx5_is_secondary())
1032 return -E_RTE_SECONDARY;
1034 ifr.ifr_data = (void *)ðpause;
1035 ethpause.autoneg = fc_conf->autoneg;
1036 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1037 (fc_conf->mode & RTE_FC_RX_PAUSE))
1038 ethpause.rx_pause = 1;
1040 ethpause.rx_pause = 0;
1042 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1043 (fc_conf->mode & RTE_FC_TX_PAUSE))
1044 ethpause.tx_pause = 1;
1046 ethpause.tx_pause = 0;
1049 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1051 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1065 * Get PCI information from struct ibv_device.
1068 * Pointer to Ethernet device structure.
1069 * @param[out] pci_addr
1070 * PCI bus address output buffer.
1073 * 0 on success, -1 on failure and errno is set.
1076 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1077 struct rte_pci_addr *pci_addr)
1081 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1083 file = fopen(path, "rb");
1086 while (fgets(line, sizeof(line), file) == line) {
1087 size_t len = strlen(line);
1090 /* Truncate long lines. */
1091 if (len == (sizeof(line) - 1))
1092 while (line[(len - 1)] != '\n') {
1096 line[(len - 1)] = ret;
1098 /* Extract information. */
1101 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1105 &pci_addr->function) == 4) {
1115 * Link status handler.
1118 * Pointer to private structure.
1120 * Pointer to the rte_eth_dev structure.
1123 * Nonzero if the callback process can be called immediately.
1126 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
1128 struct ibv_async_event event;
1129 struct rte_eth_link *link = &dev->data->dev_link;
1132 /* Read all message and acknowledge them. */
1134 if (ibv_get_async_event(priv->ctx, &event))
1137 if (event.event_type != IBV_EVENT_PORT_ACTIVE &&
1138 event.event_type != IBV_EVENT_PORT_ERR)
1139 DEBUG("event type %d on port %d not handled",
1140 event.event_type, event.element.port_num);
1141 ibv_ack_async_event(&event);
1143 mlx5_link_update(dev, 0);
1144 if (((link->link_speed == 0) && link->link_status) ||
1145 ((link->link_speed != 0) && !link->link_status)) {
1146 if (!priv->pending_alarm) {
1147 /* Inconsistent status, check again later. */
1148 priv->pending_alarm = 1;
1149 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1150 mlx5_dev_link_status_handler,
1160 * Handle delayed link status event.
1163 * Registered argument.
1166 mlx5_dev_link_status_handler(void *arg)
1168 struct rte_eth_dev *dev = arg;
1169 struct priv *priv = dev->data->dev_private;
1173 assert(priv->pending_alarm == 1);
1174 priv->pending_alarm = 0;
1175 ret = priv_dev_link_status_handler(priv, dev);
1178 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1183 * Handle interrupts from the NIC.
1185 * @param[in] intr_handle
1186 * Interrupt handler.
1188 * Callback argument.
1191 mlx5_dev_interrupt_handler(void *cb_arg)
1193 struct rte_eth_dev *dev = cb_arg;
1194 struct priv *priv = dev->data->dev_private;
1198 ret = priv_dev_link_status_handler(priv, dev);
1201 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1206 * Uninstall interrupt handler.
1209 * Pointer to private structure.
1211 * Pointer to the rte_eth_dev structure.
1214 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1216 if (!dev->data->dev_conf.intr_conf.lsc)
1218 rte_intr_callback_unregister(&priv->intr_handle,
1219 mlx5_dev_interrupt_handler,
1221 if (priv->pending_alarm)
1222 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1223 priv->pending_alarm = 0;
1224 priv->intr_handle.fd = 0;
1225 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1229 * Install interrupt handler.
1232 * Pointer to private structure.
1234 * Pointer to the rte_eth_dev structure.
1237 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1241 if (!dev->data->dev_conf.intr_conf.lsc)
1243 assert(priv->ctx->async_fd > 0);
1244 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1245 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1247 INFO("failed to change file descriptor async event queue");
1248 dev->data->dev_conf.intr_conf.lsc = 0;
1250 priv->intr_handle.fd = priv->ctx->async_fd;
1251 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1252 rte_intr_callback_register(&priv->intr_handle,
1253 mlx5_dev_interrupt_handler,
1259 * Change the link state (UP / DOWN).
1262 * Pointer to Ethernet device structure.
1264 * Nonzero for link up, otherwise link down.
1267 * 0 on success, errno value on failure.
1270 priv_set_link(struct priv *priv, int up)
1272 struct rte_eth_dev *dev = priv->dev;
1276 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1279 priv_select_tx_function(priv);
1280 priv_select_rx_function(priv);
1282 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1285 dev->rx_pkt_burst = removed_rx_burst;
1286 dev->tx_pkt_burst = removed_tx_burst;
1292 * DPDK callback to bring the link DOWN.
1295 * Pointer to Ethernet device structure.
1298 * 0 on success, errno value on failure.
1301 mlx5_set_link_down(struct rte_eth_dev *dev)
1303 struct priv *priv = dev->data->dev_private;
1307 err = priv_set_link(priv, 0);
1313 * DPDK callback to bring the link UP.
1316 * Pointer to Ethernet device structure.
1319 * 0 on success, errno value on failure.
1322 mlx5_set_link_up(struct rte_eth_dev *dev)
1324 struct priv *priv = dev->data->dev_private;
1328 err = priv_set_link(priv, 1);
1334 * Configure the TX function to use.
1337 * Pointer to private structure.
1340 priv_select_tx_function(struct priv *priv)
1342 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1343 /* Select appropriate TX function. */
1344 if (priv->mps == MLX5_MPW_ENHANCED) {
1345 if (priv_check_vec_tx_support(priv) > 0) {
1346 if (priv_check_raw_vec_tx_support(priv) > 0)
1347 priv->dev->tx_pkt_burst = mlx5_tx_burst_raw_vec;
1349 priv->dev->tx_pkt_burst = mlx5_tx_burst_vec;
1350 DEBUG("selected Enhanced MPW TX vectorized function");
1352 priv->dev->tx_pkt_burst = mlx5_tx_burst_empw;
1353 DEBUG("selected Enhanced MPW TX function");
1355 } else if (priv->mps && priv->txq_inline) {
1356 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1357 DEBUG("selected MPW inline TX function");
1358 } else if (priv->mps) {
1359 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1360 DEBUG("selected MPW TX function");
1365 * Configure the RX function to use.
1368 * Pointer to private structure.
1371 priv_select_rx_function(struct priv *priv)
1373 if (priv_check_vec_rx_support(priv) > 0) {
1374 priv->dev->rx_pkt_burst = mlx5_rx_burst_vec;
1375 DEBUG("selected RX vectorized function");
1377 priv->dev->rx_pkt_burst = mlx5_rx_burst;