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 /* DPDK headers don't like -pedantic. */
55 #pragma GCC diagnostic ignored "-Wpedantic"
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>
65 #pragma GCC diagnostic error "-Wpedantic"
69 #include "mlx5_rxtx.h"
70 #include "mlx5_utils.h"
72 /* Add defines in case the running kernel is not the same as user headers. */
73 #ifndef ETHTOOL_GLINKSETTINGS
74 struct ethtool_link_settings {
83 uint8_t eth_tp_mdix_ctrl;
84 int8_t link_mode_masks_nwords;
86 uint32_t link_mode_masks[];
89 #define ETHTOOL_GLINKSETTINGS 0x0000004c
90 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
91 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
92 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
93 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
94 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
95 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
96 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
97 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
98 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
99 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
100 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
101 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
102 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
103 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
104 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
105 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
107 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
108 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
109 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
110 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
112 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
113 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
114 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
116 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
117 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
118 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
119 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
120 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
124 * Return private structure associated with an Ethernet device.
127 * Pointer to Ethernet device structure.
130 * Pointer to private structure.
133 mlx5_get_priv(struct rte_eth_dev *dev)
135 struct mlx5_secondary_data *sd;
137 if (!mlx5_is_secondary())
138 return dev->data->dev_private;
139 sd = &mlx5_secondary_data[dev->data->port_id];
140 return sd->data.dev_private;
144 * Check if running as a secondary process.
147 * Nonzero if running as a secondary process.
150 mlx5_is_secondary(void)
152 return rte_eal_process_type() != RTE_PROC_PRIMARY;
156 * Get interface name from private structure.
159 * Pointer to private structure.
161 * Interface name output buffer.
164 * 0 on success, -1 on failure and errno is set.
167 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
171 unsigned int dev_type = 0;
172 unsigned int dev_port_prev = ~0u;
173 char match[IF_NAMESIZE] = "";
176 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
182 while ((dent = readdir(dir)) != NULL) {
183 char *name = dent->d_name;
185 unsigned int dev_port;
188 if ((name[0] == '.') &&
189 ((name[1] == '\0') ||
190 ((name[1] == '.') && (name[2] == '\0'))))
193 MKSTR(path, "%s/device/net/%s/%s",
194 priv->ctx->device->ibdev_path, name,
195 (dev_type ? "dev_id" : "dev_port"));
197 file = fopen(path, "rb");
202 * Switch to dev_id when dev_port does not exist as
203 * is the case with Linux kernel versions < 3.15.
214 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
219 * Switch to dev_id when dev_port returns the same value for
220 * all ports. May happen when using a MOFED release older than
221 * 3.0 with a Linux kernel >= 3.15.
223 if (dev_port == dev_port_prev)
225 dev_port_prev = dev_port;
226 if (dev_port == (priv->port - 1u))
227 snprintf(match, sizeof(match), "%s", name);
230 if (match[0] == '\0')
232 strncpy(*ifname, match, sizeof(*ifname));
237 * Read from sysfs entry.
240 * Pointer to private structure.
242 * Entry name relative to sysfs path.
244 * Data output buffer.
249 * 0 on success, -1 on failure and errno is set.
252 priv_sysfs_read(const struct priv *priv, const char *entry,
253 char *buf, size_t size)
255 char ifname[IF_NAMESIZE];
260 if (priv_get_ifname(priv, &ifname))
263 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
266 file = fopen(path, "rb");
269 ret = fread(buf, 1, size, file);
271 if (((size_t)ret < size) && (ferror(file)))
281 * Write to sysfs entry.
284 * Pointer to private structure.
286 * Entry name relative to sysfs path.
293 * 0 on success, -1 on failure and errno is set.
296 priv_sysfs_write(const struct priv *priv, const char *entry,
297 char *buf, size_t size)
299 char ifname[IF_NAMESIZE];
304 if (priv_get_ifname(priv, &ifname))
307 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
310 file = fopen(path, "wb");
313 ret = fwrite(buf, 1, size, file);
315 if (((size_t)ret < size) || (ferror(file)))
325 * Get unsigned long sysfs property.
328 * Pointer to private structure.
330 * Entry name relative to sysfs path.
332 * Value output buffer.
335 * 0 on success, -1 on failure and errno is set.
338 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
341 unsigned long value_ret;
344 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
346 DEBUG("cannot read %s value from sysfs: %s",
347 name, strerror(errno));
350 value_str[ret] = '\0';
352 value_ret = strtoul(value_str, NULL, 0);
354 DEBUG("invalid %s value `%s': %s", name, value_str,
363 * Set unsigned long sysfs property.
366 * Pointer to private structure.
368 * Entry name relative to sysfs path.
373 * 0 on success, -1 on failure and errno is set.
376 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
379 MKSTR(value_str, "%lu", value);
381 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
383 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
384 name, value_str, value, strerror(errno));
391 * Perform ifreq ioctl() on associated Ethernet device.
394 * Pointer to private structure.
396 * Request number to pass to ioctl().
398 * Interface request structure output buffer.
401 * 0 on success, -1 on failure and errno is set.
404 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
406 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
411 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
412 ret = ioctl(sock, req, ifr);
418 * Return the number of active VFs for the current device.
421 * Pointer to private structure.
422 * @param[out] num_vfs
423 * Number of active VFs.
426 * 0 on success, -1 on failure and errno is set.
429 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
431 /* The sysfs entry name depends on the operating system. */
432 const char **name = (const char *[]){
433 "device/sriov_numvfs",
434 "device/mlx5_num_vfs",
440 unsigned long ulong_num_vfs;
442 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
444 *num_vfs = ulong_num_vfs;
445 } while (*(++name) && ret);
453 * Pointer to private structure.
455 * MTU value output buffer.
458 * 0 on success, -1 on failure and errno is set.
461 priv_get_mtu(struct priv *priv, uint16_t *mtu)
463 unsigned long ulong_mtu;
465 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
475 * Pointer to private structure.
480 * 0 on success, -1 on failure and errno is set.
483 priv_set_mtu(struct priv *priv, uint16_t mtu)
487 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
488 priv_get_mtu(priv, &new_mtu))
500 * Pointer to private structure.
502 * Bitmask for flags that must remain untouched.
504 * Bitmask for flags to modify.
507 * 0 on success, -1 on failure and errno is set.
510 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
514 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
517 tmp |= (flags & (~keep));
518 return priv_set_sysfs_ulong(priv, "flags", tmp);
522 * Ethernet device configuration.
524 * Prepare the driver for a given number of TX and RX queues.
527 * Pointer to Ethernet device structure.
530 * 0 on success, errno value on failure.
533 dev_configure(struct rte_eth_dev *dev)
535 struct priv *priv = dev->data->dev_private;
536 unsigned int rxqs_n = dev->data->nb_rx_queues;
537 unsigned int txqs_n = dev->data->nb_tx_queues;
540 unsigned int reta_idx_n;
542 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
543 priv->rxqs = (void *)dev->data->rx_queues;
544 priv->txqs = (void *)dev->data->tx_queues;
545 if (txqs_n != priv->txqs_n) {
546 INFO("%p: TX queues number update: %u -> %u",
547 (void *)dev, priv->txqs_n, txqs_n);
548 priv->txqs_n = txqs_n;
550 if (rxqs_n > priv->ind_table_max_size) {
551 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
554 if (rxqs_n == priv->rxqs_n)
556 INFO("%p: RX queues number update: %u -> %u",
557 (void *)dev, priv->rxqs_n, rxqs_n);
558 priv->rxqs_n = rxqs_n;
559 /* If the requested number of RX queues is not a power of two, use the
560 * maximum indirection table size for better balancing.
561 * The result is always rounded to the next power of two. */
562 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
563 priv->ind_table_max_size :
565 if (priv_rss_reta_index_resize(priv, reta_idx_n))
567 /* When the number of RX queues is not a power of two, the remaining
568 * table entries are padded with reused WQs and hashes are not spread
570 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
571 (*priv->reta_idx)[i] = j;
579 * DPDK callback for Ethernet device configuration.
582 * Pointer to Ethernet device structure.
585 * 0 on success, negative errno value on failure.
588 mlx5_dev_configure(struct rte_eth_dev *dev)
590 struct priv *priv = dev->data->dev_private;
593 if (mlx5_is_secondary())
594 return -E_RTE_SECONDARY;
597 ret = dev_configure(dev);
604 * DPDK callback to get information about the device.
607 * Pointer to Ethernet device structure.
609 * Info structure output buffer.
612 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
614 struct priv *priv = mlx5_get_priv(dev);
616 char ifname[IF_NAMESIZE];
618 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
621 /* FIXME: we should ask the device for these values. */
622 info->min_rx_bufsize = 32;
623 info->max_rx_pktlen = 65536;
625 * Since we need one CQ per QP, the limit is the minimum number
626 * between the two values.
628 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
629 priv->device_attr.max_qp : priv->device_attr.max_cq);
630 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
633 info->max_rx_queues = max;
634 info->max_tx_queues = max;
635 info->max_mac_addrs = RTE_DIM(priv->mac);
636 info->rx_offload_capa =
638 (DEV_RX_OFFLOAD_IPV4_CKSUM |
639 DEV_RX_OFFLOAD_UDP_CKSUM |
640 DEV_RX_OFFLOAD_TCP_CKSUM) :
642 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
644 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
646 info->tx_offload_capa |=
647 (DEV_TX_OFFLOAD_IPV4_CKSUM |
648 DEV_TX_OFFLOAD_UDP_CKSUM |
649 DEV_TX_OFFLOAD_TCP_CKSUM);
650 if (priv_get_ifname(priv, &ifname) == 0)
651 info->if_index = if_nametoindex(ifname);
652 /* FIXME: RETA update/query API expects the callee to know the size of
653 * the indirection table, for this PMD the size varies depending on
654 * the number of RX queues, it becomes impossible to find the correct
655 * size if it is not fixed.
656 * The API should be updated to solve this problem. */
657 info->reta_size = priv->ind_table_max_size;
658 info->hash_key_size = ((*priv->rss_conf) ?
659 (*priv->rss_conf)[0]->rss_key_len :
661 info->speed_capa = priv->link_speed_capa;
666 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
668 static const uint32_t ptypes[] = {
669 /* refers to rxq_cq_to_pkt_type() */
672 RTE_PTYPE_INNER_L3_IPV4,
673 RTE_PTYPE_INNER_L3_IPV6,
678 if (dev->rx_pkt_burst == mlx5_rx_burst)
684 * DPDK callback to retrieve physical link information.
687 * Pointer to Ethernet device structure.
688 * @param wait_to_complete
689 * Wait for request completion (ignored).
692 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
694 struct priv *priv = mlx5_get_priv(dev);
695 struct ethtool_cmd edata = {
696 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
699 struct rte_eth_link dev_link;
702 /* priv_lock() is not taken to allow concurrent calls. */
704 (void)wait_to_complete;
705 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
706 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
709 memset(&dev_link, 0, sizeof(dev_link));
710 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
711 (ifr.ifr_flags & IFF_RUNNING));
712 ifr.ifr_data = (void *)&edata;
713 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
714 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
718 link_speed = ethtool_cmd_speed(&edata);
719 if (link_speed == -1)
720 dev_link.link_speed = 0;
722 dev_link.link_speed = link_speed;
723 priv->link_speed_capa = 0;
724 if (edata.supported & SUPPORTED_Autoneg)
725 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
726 if (edata.supported & (SUPPORTED_1000baseT_Full |
727 SUPPORTED_1000baseKX_Full))
728 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
729 if (edata.supported & SUPPORTED_10000baseKR_Full)
730 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
731 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
732 SUPPORTED_40000baseCR4_Full |
733 SUPPORTED_40000baseSR4_Full |
734 SUPPORTED_40000baseLR4_Full))
735 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
736 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
737 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
738 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
739 ETH_LINK_SPEED_FIXED);
740 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
741 /* Link status changed. */
742 dev->data->dev_link = dev_link;
745 /* Link status is still the same. */
750 * Retrieve physical link information (unlocked version using new ioctl).
753 * Pointer to Ethernet device structure.
754 * @param wait_to_complete
755 * Wait for request completion (ignored).
758 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
760 struct priv *priv = mlx5_get_priv(dev);
761 struct ethtool_link_settings edata = {
762 .cmd = ETHTOOL_GLINKSETTINGS,
765 struct rte_eth_link dev_link;
768 (void)wait_to_complete;
769 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
770 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
773 memset(&dev_link, 0, sizeof(dev_link));
774 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
775 (ifr.ifr_flags & IFF_RUNNING));
776 ifr.ifr_data = (void *)&edata;
777 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
778 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
782 dev_link.link_speed = edata.speed;
783 sc = edata.link_mode_masks[0] |
784 ((uint64_t)edata.link_mode_masks[1] << 32);
785 priv->link_speed_capa = 0;
786 if (sc & ETHTOOL_LINK_MODE_Autoneg_BIT)
787 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
788 if (sc & (ETHTOOL_LINK_MODE_1000baseT_Full_BIT |
789 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))
790 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
791 if (sc & (ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT |
792 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT |
793 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))
794 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
795 if (sc & (ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT |
796 ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))
797 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
798 if (sc & (ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT |
799 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT |
800 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT |
801 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))
802 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
803 if (sc & (ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT |
804 ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT |
805 ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT |
806 ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))
807 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
808 if (sc & (ETHTOOL_LINK_MODE_25000baseCR_Full_BIT |
809 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT |
810 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))
811 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
812 if (sc & (ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT |
813 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))
814 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
815 if (sc & (ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT |
816 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT |
817 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT |
818 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))
819 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
820 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
821 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
822 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
823 ETH_LINK_SPEED_FIXED);
824 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
825 /* Link status changed. */
826 dev->data->dev_link = dev_link;
829 /* Link status is still the same. */
834 * DPDK callback to retrieve physical link information.
837 * Pointer to Ethernet device structure.
838 * @param wait_to_complete
839 * Wait for request completion (ignored).
842 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
844 struct utsname utsname;
847 if (uname(&utsname) == -1 ||
848 sscanf(utsname.release, "%d.%d.%d",
849 &ver[0], &ver[1], &ver[2]) != 3 ||
850 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
851 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
852 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
856 * DPDK callback to change the MTU.
858 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
859 * received). Use this as a hint to enable/disable scattered packets support
860 * and improve performance when not needed.
861 * Since failure is not an option, reconfiguring queues on the fly is not
865 * Pointer to Ethernet device structure.
870 * 0 on success, negative errno value on failure.
873 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
875 struct priv *priv = dev->data->dev_private;
878 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
880 unsigned int max_frame_len;
882 int restart = priv->started;
884 if (mlx5_is_secondary())
885 return -E_RTE_SECONDARY;
888 /* Set kernel interface MTU first. */
889 if (priv_set_mtu(priv, mtu)) {
891 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
895 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
896 /* Temporarily replace RX handler with a fake one, assuming it has not
897 * been copied elsewhere. */
898 dev->rx_pkt_burst = removed_rx_burst;
899 /* Make sure everyone has left mlx5_rx_burst() and uses
900 * removed_rx_burst() instead. */
903 /* MTU does not include header and CRC. */
904 max_frame_len = ETHER_HDR_LEN + mtu + ETHER_CRC_LEN;
905 /* Check if at least one queue is going to need a SGE update. */
906 for (i = 0; i != priv->rxqs_n; ++i) {
907 struct rxq *rxq = (*priv->rxqs)[i];
909 unsigned int size = RTE_PKTMBUF_HEADROOM + max_frame_len;
914 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
915 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
917 * Determine the number of SGEs needed for a full packet
918 * and round it to the next power of two.
920 sges_n = log2above((size / mb_len) + !!(size % mb_len));
921 if (sges_n != rxq->sges_n)
925 * If all queues have the right number of SGEs, a simple rehash
926 * of their buffers is enough, otherwise SGE information can only
927 * be updated in a queue by recreating it. All resources that depend
928 * on queues (flows, indirection tables) must be recreated as well in
931 rehash = (i == priv->rxqs_n);
933 /* Clean up everything as with mlx5_dev_stop(). */
934 priv_special_flow_disable_all(priv);
935 priv_mac_addrs_disable(priv);
936 priv_destroy_hash_rxqs(priv);
937 priv_fdir_disable(priv);
938 priv_dev_interrupt_handler_uninstall(priv, dev);
941 /* Reconfigure each RX queue. */
942 for (i = 0; (i != priv->rxqs_n); ++i) {
943 struct rxq *rxq = (*priv->rxqs)[i];
944 struct rxq_ctrl *rxq_ctrl =
945 container_of(rxq, struct rxq_ctrl, rxq);
952 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
953 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
954 /* Toggle scattered support (sp) if necessary. */
955 sp = (max_frame_len > (mb_len - RTE_PKTMBUF_HEADROOM));
956 /* Provide new values to rxq_setup(). */
957 dev->data->dev_conf.rxmode.jumbo_frame = sp;
958 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
960 ret = rxq_rehash(dev, rxq_ctrl);
962 ret = rxq_ctrl_setup(dev, rxq_ctrl, 1 << rxq->elts_n,
963 rxq_ctrl->socket, NULL, rxq->mp);
966 /* Attempt to roll back in case of error. */
967 tmp = (mb_len << rxq->sges_n) - RTE_PKTMBUF_HEADROOM;
968 if (max_frame_len != tmp) {
972 /* Double fault, disable RX. */
976 * Use a safe RX burst function in case of error, otherwise mimic
980 ERROR("unable to reconfigure RX queues, RX disabled");
981 rx_func = removed_rx_burst;
982 } else if (restart &&
984 !priv_create_hash_rxqs(priv) &&
985 !priv_rehash_flows(priv)) {
986 if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_NONE)
987 priv_fdir_enable(priv);
988 priv_dev_interrupt_handler_install(priv, dev);
991 /* Burst functions can now be called again. */
993 dev->rx_pkt_burst = rx_func;
1001 * DPDK callback to get flow control status.
1004 * Pointer to Ethernet device structure.
1005 * @param[out] fc_conf
1006 * Flow control output buffer.
1009 * 0 on success, negative errno value on failure.
1012 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1014 struct priv *priv = dev->data->dev_private;
1016 struct ethtool_pauseparam ethpause = {
1017 .cmd = ETHTOOL_GPAUSEPARAM
1021 if (mlx5_is_secondary())
1022 return -E_RTE_SECONDARY;
1024 ifr.ifr_data = (void *)ðpause;
1026 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1028 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1034 fc_conf->autoneg = ethpause.autoneg;
1035 if (ethpause.rx_pause && ethpause.tx_pause)
1036 fc_conf->mode = RTE_FC_FULL;
1037 else if (ethpause.rx_pause)
1038 fc_conf->mode = RTE_FC_RX_PAUSE;
1039 else if (ethpause.tx_pause)
1040 fc_conf->mode = RTE_FC_TX_PAUSE;
1042 fc_conf->mode = RTE_FC_NONE;
1052 * DPDK callback to modify flow control parameters.
1055 * Pointer to Ethernet device structure.
1056 * @param[in] fc_conf
1057 * Flow control parameters.
1060 * 0 on success, negative errno value on failure.
1063 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1065 struct priv *priv = dev->data->dev_private;
1067 struct ethtool_pauseparam ethpause = {
1068 .cmd = ETHTOOL_SPAUSEPARAM
1072 if (mlx5_is_secondary())
1073 return -E_RTE_SECONDARY;
1075 ifr.ifr_data = (void *)ðpause;
1076 ethpause.autoneg = fc_conf->autoneg;
1077 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1078 (fc_conf->mode & RTE_FC_RX_PAUSE))
1079 ethpause.rx_pause = 1;
1081 ethpause.rx_pause = 0;
1083 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1084 (fc_conf->mode & RTE_FC_TX_PAUSE))
1085 ethpause.tx_pause = 1;
1087 ethpause.tx_pause = 0;
1090 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1092 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1106 * Get PCI information from struct ibv_device.
1109 * Pointer to Ethernet device structure.
1110 * @param[out] pci_addr
1111 * PCI bus address output buffer.
1114 * 0 on success, -1 on failure and errno is set.
1117 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1118 struct rte_pci_addr *pci_addr)
1122 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1124 file = fopen(path, "rb");
1127 while (fgets(line, sizeof(line), file) == line) {
1128 size_t len = strlen(line);
1131 /* Truncate long lines. */
1132 if (len == (sizeof(line) - 1))
1133 while (line[(len - 1)] != '\n') {
1137 line[(len - 1)] = ret;
1139 /* Extract information. */
1142 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1146 &pci_addr->function) == 4) {
1156 * Link status handler.
1159 * Pointer to private structure.
1161 * Pointer to the rte_eth_dev structure.
1164 * Nonzero if the callback process can be called immediately.
1167 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
1169 struct ibv_async_event event;
1170 struct rte_eth_link *link = &dev->data->dev_link;
1173 /* Read all message and acknowledge them. */
1175 if (ibv_get_async_event(priv->ctx, &event))
1178 if (event.event_type != IBV_EVENT_PORT_ACTIVE &&
1179 event.event_type != IBV_EVENT_PORT_ERR)
1180 DEBUG("event type %d on port %d not handled",
1181 event.event_type, event.element.port_num);
1182 ibv_ack_async_event(&event);
1184 mlx5_link_update(dev, 0);
1185 if (((link->link_speed == 0) && link->link_status) ||
1186 ((link->link_speed != 0) && !link->link_status)) {
1187 if (!priv->pending_alarm) {
1188 /* Inconsistent status, check again later. */
1189 priv->pending_alarm = 1;
1190 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1191 mlx5_dev_link_status_handler,
1201 * Handle delayed link status event.
1204 * Registered argument.
1207 mlx5_dev_link_status_handler(void *arg)
1209 struct rte_eth_dev *dev = arg;
1210 struct priv *priv = dev->data->dev_private;
1214 assert(priv->pending_alarm == 1);
1215 priv->pending_alarm = 0;
1216 ret = priv_dev_link_status_handler(priv, dev);
1219 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1223 * Handle interrupts from the NIC.
1225 * @param[in] intr_handle
1226 * Interrupt handler.
1228 * Callback argument.
1231 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
1233 struct rte_eth_dev *dev = cb_arg;
1234 struct priv *priv = dev->data->dev_private;
1239 ret = priv_dev_link_status_handler(priv, dev);
1242 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1246 * Uninstall interrupt handler.
1249 * Pointer to private structure.
1251 * Pointer to the rte_eth_dev structure.
1254 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1256 if (!dev->data->dev_conf.intr_conf.lsc)
1258 rte_intr_callback_unregister(&priv->intr_handle,
1259 mlx5_dev_interrupt_handler,
1261 if (priv->pending_alarm)
1262 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1263 priv->pending_alarm = 0;
1264 priv->intr_handle.fd = 0;
1265 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1269 * Install interrupt handler.
1272 * Pointer to private structure.
1274 * Pointer to the rte_eth_dev structure.
1277 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1281 if (!dev->data->dev_conf.intr_conf.lsc)
1283 assert(priv->ctx->async_fd > 0);
1284 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1285 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1287 INFO("failed to change file descriptor async event queue");
1288 dev->data->dev_conf.intr_conf.lsc = 0;
1290 priv->intr_handle.fd = priv->ctx->async_fd;
1291 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1292 rte_intr_callback_register(&priv->intr_handle,
1293 mlx5_dev_interrupt_handler,
1299 * Change the link state (UP / DOWN).
1302 * Pointer to Ethernet device structure.
1304 * Nonzero for link up, otherwise link down.
1307 * 0 on success, errno value on failure.
1310 priv_set_link(struct priv *priv, int up)
1312 struct rte_eth_dev *dev = priv->dev;
1316 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1319 priv_select_tx_function(priv);
1320 priv_select_rx_function(priv);
1322 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1325 dev->rx_pkt_burst = removed_rx_burst;
1326 dev->tx_pkt_burst = removed_tx_burst;
1332 * DPDK callback to bring the link DOWN.
1335 * Pointer to Ethernet device structure.
1338 * 0 on success, errno value on failure.
1341 mlx5_set_link_down(struct rte_eth_dev *dev)
1343 struct priv *priv = dev->data->dev_private;
1347 err = priv_set_link(priv, 0);
1353 * DPDK callback to bring the link UP.
1356 * Pointer to Ethernet device structure.
1359 * 0 on success, errno value on failure.
1362 mlx5_set_link_up(struct rte_eth_dev *dev)
1364 struct priv *priv = dev->data->dev_private;
1368 err = priv_set_link(priv, 1);
1374 * Configure secondary process queues from a private data pointer (primary
1375 * or secondary) and update burst callbacks. Can take place only once.
1377 * All queues must have been previously created by the primary process to
1378 * avoid undefined behavior.
1381 * Private data pointer from either primary or secondary process.
1384 * Private data pointer from secondary process, NULL in case of error.
1387 mlx5_secondary_data_setup(struct priv *priv)
1389 unsigned int port_id = 0;
1390 struct mlx5_secondary_data *sd;
1393 unsigned int nb_tx_queues;
1394 unsigned int nb_rx_queues;
1397 /* priv must be valid at this point. */
1398 assert(priv != NULL);
1399 /* priv->dev must also be valid but may point to local memory from
1400 * another process, possibly with the same address and must not
1401 * be dereferenced yet. */
1402 assert(priv->dev != NULL);
1403 /* Determine port ID by finding out where priv comes from. */
1405 sd = &mlx5_secondary_data[port_id];
1406 rte_spinlock_lock(&sd->lock);
1407 /* Primary process? */
1408 if (sd->primary_priv == priv)
1410 /* Secondary process? */
1411 if (sd->data.dev_private == priv)
1413 rte_spinlock_unlock(&sd->lock);
1414 if (++port_id == RTE_DIM(mlx5_secondary_data))
1417 /* Switch to secondary private structure. If private data has already
1418 * been updated by another thread, there is nothing else to do. */
1419 priv = sd->data.dev_private;
1420 if (priv->dev->data == &sd->data)
1422 /* Sanity checks. Secondary private structure is supposed to point
1423 * to local eth_dev, itself still pointing to the shared device data
1424 * structure allocated by the primary process. */
1425 assert(sd->shared_dev_data != &sd->data);
1426 assert(sd->data.nb_tx_queues == 0);
1427 assert(sd->data.tx_queues == NULL);
1428 assert(sd->data.nb_rx_queues == 0);
1429 assert(sd->data.rx_queues == NULL);
1430 assert(priv != sd->primary_priv);
1431 assert(priv->dev->data == sd->shared_dev_data);
1432 assert(priv->txqs_n == 0);
1433 assert(priv->txqs == NULL);
1434 assert(priv->rxqs_n == 0);
1435 assert(priv->rxqs == NULL);
1436 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1437 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1438 /* Allocate local storage for queues. */
1439 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1440 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1441 RTE_CACHE_LINE_SIZE);
1442 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1443 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1444 RTE_CACHE_LINE_SIZE);
1445 if (tx_queues == NULL || rx_queues == NULL)
1447 /* Lock to prevent control operations during setup. */
1450 for (i = 0; i != nb_tx_queues; ++i) {
1451 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1452 struct txq_ctrl *primary_txq_ctrl;
1453 struct txq_ctrl *txq_ctrl;
1455 if (primary_txq == NULL)
1457 primary_txq_ctrl = container_of(primary_txq,
1458 struct txq_ctrl, txq);
1459 txq_ctrl = rte_calloc_socket("TXQ", 1, sizeof(*txq_ctrl) +
1460 (1 << primary_txq->elts_n) *
1461 sizeof(struct rte_mbuf *), 0,
1462 primary_txq_ctrl->socket);
1463 if (txq_ctrl != NULL) {
1464 if (txq_ctrl_setup(priv->dev,
1466 1 << primary_txq->elts_n,
1467 primary_txq_ctrl->socket,
1469 txq_ctrl->txq.stats.idx =
1470 primary_txq->stats.idx;
1471 tx_queues[i] = &txq_ctrl->txq;
1477 txq_ctrl = tx_queues[--i];
1478 txq_cleanup(txq_ctrl);
1484 for (i = 0; i != nb_rx_queues; ++i) {
1485 struct rxq_ctrl *primary_rxq =
1486 container_of((*sd->primary_priv->rxqs)[i],
1487 struct rxq_ctrl, rxq);
1489 if (primary_rxq == NULL)
1491 /* Not supported yet. */
1492 rx_queues[i] = NULL;
1494 /* Update everything. */
1495 priv->txqs = (void *)tx_queues;
1496 priv->txqs_n = nb_tx_queues;
1497 priv->rxqs = (void *)rx_queues;
1498 priv->rxqs_n = nb_rx_queues;
1499 sd->data.rx_queues = rx_queues;
1500 sd->data.tx_queues = tx_queues;
1501 sd->data.nb_rx_queues = nb_rx_queues;
1502 sd->data.nb_tx_queues = nb_tx_queues;
1503 sd->data.dev_link = sd->shared_dev_data->dev_link;
1504 sd->data.mtu = sd->shared_dev_data->mtu;
1505 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1506 sizeof(sd->data.rx_queue_state));
1507 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1508 sizeof(sd->data.tx_queue_state));
1509 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1510 /* Use local data from now on. */
1512 priv->dev->data = &sd->data;
1514 priv_select_tx_function(priv);
1515 priv_select_rx_function(priv);
1518 /* More sanity checks. */
1519 assert(priv->dev->data == &sd->data);
1520 rte_spinlock_unlock(&sd->lock);
1524 rte_free(tx_queues);
1525 rte_free(rx_queues);
1526 rte_spinlock_unlock(&sd->lock);
1531 * Configure the TX function to use.
1534 * Pointer to private structure.
1537 priv_select_tx_function(struct priv *priv)
1539 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1540 /* Select appropriate TX function. */
1541 if (priv->mps && priv->txq_inline) {
1542 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1543 DEBUG("selected MPW inline TX function");
1544 } else if (priv->mps) {
1545 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1546 DEBUG("selected MPW TX function");
1551 * Configure the RX function to use.
1554 * Pointer to private structure.
1557 priv_select_rx_function(struct priv *priv)
1559 priv->dev->rx_pkt_burst = mlx5_rx_burst;