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>
54 /* DPDK headers don't like -pedantic. */
56 #pragma GCC diagnostic ignored "-Wpedantic"
58 #include <rte_atomic.h>
59 #include <rte_ethdev.h>
61 #include <rte_common.h>
62 #include <rte_interrupts.h>
63 #include <rte_alarm.h>
64 #include <rte_malloc.h>
66 #pragma GCC diagnostic error "-Wpedantic"
70 #include "mlx5_rxtx.h"
71 #include "mlx5_utils.h"
73 /* Add defines in case the running kernel is not the same as user headers. */
74 #ifndef ETHTOOL_GLINKSETTINGS
75 struct ethtool_link_settings {
84 uint8_t eth_tp_mdix_ctrl;
85 int8_t link_mode_masks_nwords;
87 uint32_t link_mode_masks[];
90 #define ETHTOOL_GLINKSETTINGS 0x0000004c
91 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
92 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
93 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
94 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
95 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
96 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
97 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
98 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
99 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
100 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
101 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
102 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
103 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
104 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
105 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
106 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
108 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
109 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
110 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
111 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
113 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
114 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
115 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
117 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
118 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
119 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
120 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
121 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
125 * Return private structure associated with an Ethernet device.
128 * Pointer to Ethernet device structure.
131 * Pointer to private structure.
134 mlx5_get_priv(struct rte_eth_dev *dev)
136 struct mlx5_secondary_data *sd;
138 if (!mlx5_is_secondary())
139 return dev->data->dev_private;
140 sd = &mlx5_secondary_data[dev->data->port_id];
141 return sd->data.dev_private;
145 * Check if running as a secondary process.
148 * Nonzero if running as a secondary process.
151 mlx5_is_secondary(void)
153 return rte_eal_process_type() != RTE_PROC_PRIMARY;
157 * Get interface name from private structure.
160 * Pointer to private structure.
162 * Interface name output buffer.
165 * 0 on success, -1 on failure and errno is set.
168 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
172 unsigned int dev_type = 0;
173 unsigned int dev_port_prev = ~0u;
174 char match[IF_NAMESIZE] = "";
177 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
183 while ((dent = readdir(dir)) != NULL) {
184 char *name = dent->d_name;
186 unsigned int dev_port;
189 if ((name[0] == '.') &&
190 ((name[1] == '\0') ||
191 ((name[1] == '.') && (name[2] == '\0'))))
194 MKSTR(path, "%s/device/net/%s/%s",
195 priv->ctx->device->ibdev_path, name,
196 (dev_type ? "dev_id" : "dev_port"));
198 file = fopen(path, "rb");
203 * Switch to dev_id when dev_port does not exist as
204 * is the case with Linux kernel versions < 3.15.
215 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
220 * Switch to dev_id when dev_port returns the same value for
221 * all ports. May happen when using a MOFED release older than
222 * 3.0 with a Linux kernel >= 3.15.
224 if (dev_port == dev_port_prev)
226 dev_port_prev = dev_port;
227 if (dev_port == (priv->port - 1u))
228 snprintf(match, sizeof(match), "%s", name);
231 if (match[0] == '\0')
233 strncpy(*ifname, match, sizeof(*ifname));
238 * Read from sysfs entry.
241 * Pointer to private structure.
243 * Entry name relative to sysfs path.
245 * Data output buffer.
250 * 0 on success, -1 on failure and errno is set.
253 priv_sysfs_read(const struct priv *priv, const char *entry,
254 char *buf, size_t size)
256 char ifname[IF_NAMESIZE];
261 if (priv_get_ifname(priv, &ifname))
264 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
267 file = fopen(path, "rb");
270 ret = fread(buf, 1, size, file);
272 if (((size_t)ret < size) && (ferror(file)))
282 * Write to sysfs entry.
285 * Pointer to private structure.
287 * Entry name relative to sysfs path.
294 * 0 on success, -1 on failure and errno is set.
297 priv_sysfs_write(const struct priv *priv, const char *entry,
298 char *buf, size_t size)
300 char ifname[IF_NAMESIZE];
305 if (priv_get_ifname(priv, &ifname))
308 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
311 file = fopen(path, "wb");
314 ret = fwrite(buf, 1, size, file);
316 if (((size_t)ret < size) || (ferror(file)))
326 * Get unsigned long sysfs property.
329 * Pointer to private structure.
331 * Entry name relative to sysfs path.
333 * Value output buffer.
336 * 0 on success, -1 on failure and errno is set.
339 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
342 unsigned long value_ret;
345 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
347 DEBUG("cannot read %s value from sysfs: %s",
348 name, strerror(errno));
351 value_str[ret] = '\0';
353 value_ret = strtoul(value_str, NULL, 0);
355 DEBUG("invalid %s value `%s': %s", name, value_str,
364 * Set unsigned long sysfs property.
367 * Pointer to private structure.
369 * Entry name relative to sysfs path.
374 * 0 on success, -1 on failure and errno is set.
377 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
380 MKSTR(value_str, "%lu", value);
382 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
384 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
385 name, value_str, value, strerror(errno));
392 * Perform ifreq ioctl() on associated Ethernet device.
395 * Pointer to private structure.
397 * Request number to pass to ioctl().
399 * Interface request structure output buffer.
402 * 0 on success, -1 on failure and errno is set.
405 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
407 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
412 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
413 ret = ioctl(sock, req, ifr);
419 * Return the number of active VFs for the current device.
422 * Pointer to private structure.
423 * @param[out] num_vfs
424 * Number of active VFs.
427 * 0 on success, -1 on failure and errno is set.
430 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
432 /* The sysfs entry name depends on the operating system. */
433 const char **name = (const char *[]){
434 "device/sriov_numvfs",
435 "device/mlx5_num_vfs",
441 unsigned long ulong_num_vfs;
443 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
445 *num_vfs = ulong_num_vfs;
446 } while (*(++name) && ret);
454 * Pointer to private structure.
456 * MTU value output buffer.
459 * 0 on success, -1 on failure and errno is set.
462 priv_get_mtu(struct priv *priv, uint16_t *mtu)
464 unsigned long ulong_mtu;
466 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
476 * Pointer to private structure.
481 * 0 on success, -1 on failure and errno is set.
484 priv_set_mtu(struct priv *priv, uint16_t mtu)
488 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
489 priv_get_mtu(priv, &new_mtu))
501 * Pointer to private structure.
503 * Bitmask for flags that must remain untouched.
505 * Bitmask for flags to modify.
508 * 0 on success, -1 on failure and errno is set.
511 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
515 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
518 tmp |= (flags & (~keep));
519 return priv_set_sysfs_ulong(priv, "flags", tmp);
523 * Ethernet device configuration.
525 * Prepare the driver for a given number of TX and RX queues.
528 * Pointer to Ethernet device structure.
531 * 0 on success, errno value on failure.
534 dev_configure(struct rte_eth_dev *dev)
536 struct priv *priv = dev->data->dev_private;
537 unsigned int rxqs_n = dev->data->nb_rx_queues;
538 unsigned int txqs_n = dev->data->nb_tx_queues;
541 unsigned int reta_idx_n;
543 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
544 priv->rxqs = (void *)dev->data->rx_queues;
545 priv->txqs = (void *)dev->data->tx_queues;
546 if (txqs_n != priv->txqs_n) {
547 INFO("%p: TX queues number update: %u -> %u",
548 (void *)dev, priv->txqs_n, txqs_n);
549 priv->txqs_n = txqs_n;
551 if (rxqs_n > priv->ind_table_max_size) {
552 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
555 if (rxqs_n == priv->rxqs_n)
557 INFO("%p: RX queues number update: %u -> %u",
558 (void *)dev, priv->rxqs_n, rxqs_n);
559 priv->rxqs_n = rxqs_n;
560 /* If the requested number of RX queues is not a power of two, use the
561 * maximum indirection table size for better balancing.
562 * The result is always rounded to the next power of two. */
563 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
564 priv->ind_table_max_size :
566 if (priv_rss_reta_index_resize(priv, reta_idx_n))
568 /* When the number of RX queues is not a power of two, the remaining
569 * table entries are padded with reused WQs and hashes are not spread
571 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
572 (*priv->reta_idx)[i] = j;
580 * DPDK callback for Ethernet device configuration.
583 * Pointer to Ethernet device structure.
586 * 0 on success, negative errno value on failure.
589 mlx5_dev_configure(struct rte_eth_dev *dev)
591 struct priv *priv = dev->data->dev_private;
594 if (mlx5_is_secondary())
595 return -E_RTE_SECONDARY;
598 ret = dev_configure(dev);
605 * DPDK callback to get information about the device.
608 * Pointer to Ethernet device structure.
610 * Info structure output buffer.
613 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
615 struct priv *priv = mlx5_get_priv(dev);
617 char ifname[IF_NAMESIZE];
620 /* FIXME: we should ask the device for these values. */
621 info->min_rx_bufsize = 32;
622 info->max_rx_pktlen = 65536;
624 * Since we need one CQ per QP, the limit is the minimum number
625 * between the two values.
627 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
628 priv->device_attr.max_qp : priv->device_attr.max_cq);
629 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
632 info->max_rx_queues = max;
633 info->max_tx_queues = max;
634 info->max_mac_addrs = RTE_DIM(priv->mac);
635 info->rx_offload_capa =
637 (DEV_RX_OFFLOAD_IPV4_CKSUM |
638 DEV_RX_OFFLOAD_UDP_CKSUM |
639 DEV_RX_OFFLOAD_TCP_CKSUM) :
641 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0);
643 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
645 info->tx_offload_capa |=
646 (DEV_TX_OFFLOAD_IPV4_CKSUM |
647 DEV_TX_OFFLOAD_UDP_CKSUM |
648 DEV_TX_OFFLOAD_TCP_CKSUM);
649 if (priv_get_ifname(priv, &ifname) == 0)
650 info->if_index = if_nametoindex(ifname);
651 /* FIXME: RETA update/query API expects the callee to know the size of
652 * the indirection table, for this PMD the size varies depending on
653 * the number of RX queues, it becomes impossible to find the correct
654 * size if it is not fixed.
655 * The API should be updated to solve this problem. */
656 info->reta_size = priv->ind_table_max_size;
657 info->hash_key_size = ((*priv->rss_conf) ?
658 (*priv->rss_conf)[0]->rss_key_len :
660 info->speed_capa = priv->link_speed_capa;
665 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
667 static const uint32_t ptypes[] = {
668 /* refers to rxq_cq_to_pkt_type() */
669 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
670 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
671 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
672 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
677 if (dev->rx_pkt_burst == mlx5_rx_burst)
683 * Retrieve physical link information (unlocked version using legacy ioctl).
686 * Pointer to Ethernet device structure.
687 * @param wait_to_complete
688 * Wait for request completion (ignored).
691 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
693 struct priv *priv = mlx5_get_priv(dev);
694 struct ethtool_cmd edata = {
695 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
698 struct rte_eth_link dev_link;
701 (void)wait_to_complete;
702 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
703 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
706 memset(&dev_link, 0, sizeof(dev_link));
707 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
708 (ifr.ifr_flags & IFF_RUNNING));
709 ifr.ifr_data = (void *)&edata;
710 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
711 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
715 link_speed = ethtool_cmd_speed(&edata);
716 if (link_speed == -1)
717 dev_link.link_speed = 0;
719 dev_link.link_speed = link_speed;
720 priv->link_speed_capa = 0;
721 if (edata.supported & SUPPORTED_Autoneg)
722 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
723 if (edata.supported & (SUPPORTED_1000baseT_Full |
724 SUPPORTED_1000baseKX_Full))
725 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
726 if (edata.supported & SUPPORTED_10000baseKR_Full)
727 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
728 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
729 SUPPORTED_40000baseCR4_Full |
730 SUPPORTED_40000baseSR4_Full |
731 SUPPORTED_40000baseLR4_Full))
732 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
733 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
734 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
735 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
736 ETH_LINK_SPEED_FIXED);
737 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
738 /* Link status changed. */
739 dev->data->dev_link = dev_link;
742 /* Link status is still the same. */
747 * Retrieve physical link information (unlocked version using new ioctl).
750 * Pointer to Ethernet device structure.
751 * @param wait_to_complete
752 * Wait for request completion (ignored).
755 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
757 struct priv *priv = mlx5_get_priv(dev);
758 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
760 struct rte_eth_link dev_link;
763 (void)wait_to_complete;
764 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
765 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
768 memset(&dev_link, 0, sizeof(dev_link));
769 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
770 (ifr.ifr_flags & IFF_RUNNING));
771 ifr.ifr_data = (void *)&gcmd;
772 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
773 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
777 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
779 alignas(struct ethtool_link_settings)
780 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
781 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
782 struct ethtool_link_settings *ecmd = (void *)data;
785 ifr.ifr_data = (void *)ecmd;
786 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
787 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
791 dev_link.link_speed = ecmd->speed;
792 sc = ecmd->link_mode_masks[0] |
793 ((uint64_t)ecmd->link_mode_masks[1] << 32);
794 priv->link_speed_capa = 0;
795 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
796 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
797 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
798 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
799 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
800 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
801 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
802 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
803 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
804 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
805 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
806 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
807 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
808 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
809 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
810 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
811 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
812 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
813 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
814 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
815 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
816 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
817 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
818 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
819 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
820 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
821 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
822 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
823 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
824 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
825 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
826 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
827 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
828 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
829 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
830 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
831 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
832 ETH_LINK_SPEED_FIXED);
833 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
834 /* Link status changed. */
835 dev->data->dev_link = dev_link;
838 /* Link status is still the same. */
843 * DPDK callback to retrieve physical link information (unlocked version).
846 * Pointer to Ethernet device structure.
847 * @param wait_to_complete
848 * Wait for request completion (ignored).
851 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
853 struct utsname utsname;
856 if (uname(&utsname) == -1 ||
857 sscanf(utsname.release, "%d.%d.%d",
858 &ver[0], &ver[1], &ver[2]) != 3 ||
859 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
860 return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
861 return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
865 * DPDK callback to retrieve physical link information.
868 * Pointer to Ethernet device structure.
869 * @param wait_to_complete
870 * Wait for request completion (ignored).
873 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
875 struct priv *priv = mlx5_get_priv(dev);
879 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
885 * DPDK callback to change the MTU.
887 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
888 * received). Use this as a hint to enable/disable scattered packets support
889 * and improve performance when not needed.
890 * Since failure is not an option, reconfiguring queues on the fly is not
894 * Pointer to Ethernet device structure.
899 * 0 on success, negative errno value on failure.
902 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
904 struct priv *priv = dev->data->dev_private;
907 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
909 unsigned int max_frame_len;
911 int restart = priv->started;
913 if (mlx5_is_secondary())
914 return -E_RTE_SECONDARY;
917 /* Set kernel interface MTU first. */
918 if (priv_set_mtu(priv, mtu)) {
920 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
924 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
925 /* Temporarily replace RX handler with a fake one, assuming it has not
926 * been copied elsewhere. */
927 dev->rx_pkt_burst = removed_rx_burst;
928 /* Make sure everyone has left mlx5_rx_burst() and uses
929 * removed_rx_burst() instead. */
932 /* MTU does not include header and CRC. */
933 max_frame_len = ETHER_HDR_LEN + mtu + ETHER_CRC_LEN;
934 /* Check if at least one queue is going to need a SGE update. */
935 for (i = 0; i != priv->rxqs_n; ++i) {
936 struct rxq *rxq = (*priv->rxqs)[i];
938 unsigned int size = RTE_PKTMBUF_HEADROOM + max_frame_len;
943 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
944 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
946 * Determine the number of SGEs needed for a full packet
947 * and round it to the next power of two.
949 sges_n = log2above((size / mb_len) + !!(size % mb_len));
950 if (sges_n != rxq->sges_n)
954 * If all queues have the right number of SGEs, a simple rehash
955 * of their buffers is enough, otherwise SGE information can only
956 * be updated in a queue by recreating it. All resources that depend
957 * on queues (flows, indirection tables) must be recreated as well in
960 rehash = (i == priv->rxqs_n);
962 /* Clean up everything as with mlx5_dev_stop(). */
963 priv_special_flow_disable_all(priv);
964 priv_mac_addrs_disable(priv);
965 priv_destroy_hash_rxqs(priv);
966 priv_fdir_disable(priv);
967 priv_dev_interrupt_handler_uninstall(priv, dev);
970 /* Reconfigure each RX queue. */
971 for (i = 0; (i != priv->rxqs_n); ++i) {
972 struct rxq *rxq = (*priv->rxqs)[i];
973 struct rxq_ctrl *rxq_ctrl =
974 container_of(rxq, struct rxq_ctrl, rxq);
981 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
982 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
983 /* Toggle scattered support (sp) if necessary. */
984 sp = (max_frame_len > (mb_len - RTE_PKTMBUF_HEADROOM));
985 /* Provide new values to rxq_setup(). */
986 dev->data->dev_conf.rxmode.jumbo_frame = sp;
987 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
989 ret = rxq_rehash(dev, rxq_ctrl);
991 ret = rxq_ctrl_setup(dev, rxq_ctrl, 1 << rxq->elts_n,
992 rxq_ctrl->socket, NULL, rxq->mp);
995 /* Attempt to roll back in case of error. */
996 tmp = (mb_len << rxq->sges_n) - RTE_PKTMBUF_HEADROOM;
997 if (max_frame_len != tmp) {
1001 /* Double fault, disable RX. */
1005 * Use a safe RX burst function in case of error, otherwise mimic
1009 ERROR("unable to reconfigure RX queues, RX disabled");
1010 rx_func = removed_rx_burst;
1011 } else if (restart &&
1013 !priv_create_hash_rxqs(priv) &&
1014 !priv_rehash_flows(priv)) {
1015 if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_NONE)
1016 priv_fdir_enable(priv);
1017 priv_dev_interrupt_handler_install(priv, dev);
1020 /* Burst functions can now be called again. */
1022 dev->rx_pkt_burst = rx_func;
1030 * DPDK callback to get flow control status.
1033 * Pointer to Ethernet device structure.
1034 * @param[out] fc_conf
1035 * Flow control output buffer.
1038 * 0 on success, negative errno value on failure.
1041 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1043 struct priv *priv = dev->data->dev_private;
1045 struct ethtool_pauseparam ethpause = {
1046 .cmd = ETHTOOL_GPAUSEPARAM
1050 if (mlx5_is_secondary())
1051 return -E_RTE_SECONDARY;
1053 ifr.ifr_data = (void *)ðpause;
1055 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1057 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1063 fc_conf->autoneg = ethpause.autoneg;
1064 if (ethpause.rx_pause && ethpause.tx_pause)
1065 fc_conf->mode = RTE_FC_FULL;
1066 else if (ethpause.rx_pause)
1067 fc_conf->mode = RTE_FC_RX_PAUSE;
1068 else if (ethpause.tx_pause)
1069 fc_conf->mode = RTE_FC_TX_PAUSE;
1071 fc_conf->mode = RTE_FC_NONE;
1081 * DPDK callback to modify flow control parameters.
1084 * Pointer to Ethernet device structure.
1085 * @param[in] fc_conf
1086 * Flow control parameters.
1089 * 0 on success, negative errno value on failure.
1092 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1094 struct priv *priv = dev->data->dev_private;
1096 struct ethtool_pauseparam ethpause = {
1097 .cmd = ETHTOOL_SPAUSEPARAM
1101 if (mlx5_is_secondary())
1102 return -E_RTE_SECONDARY;
1104 ifr.ifr_data = (void *)ðpause;
1105 ethpause.autoneg = fc_conf->autoneg;
1106 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1107 (fc_conf->mode & RTE_FC_RX_PAUSE))
1108 ethpause.rx_pause = 1;
1110 ethpause.rx_pause = 0;
1112 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1113 (fc_conf->mode & RTE_FC_TX_PAUSE))
1114 ethpause.tx_pause = 1;
1116 ethpause.tx_pause = 0;
1119 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1121 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1135 * Get PCI information from struct ibv_device.
1138 * Pointer to Ethernet device structure.
1139 * @param[out] pci_addr
1140 * PCI bus address output buffer.
1143 * 0 on success, -1 on failure and errno is set.
1146 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1147 struct rte_pci_addr *pci_addr)
1151 MKSTR(path, "%s/device/uevent", device->ibdev_path);
1153 file = fopen(path, "rb");
1156 while (fgets(line, sizeof(line), file) == line) {
1157 size_t len = strlen(line);
1160 /* Truncate long lines. */
1161 if (len == (sizeof(line) - 1))
1162 while (line[(len - 1)] != '\n') {
1166 line[(len - 1)] = ret;
1168 /* Extract information. */
1171 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1175 &pci_addr->function) == 4) {
1185 * Link status handler.
1188 * Pointer to private structure.
1190 * Pointer to the rte_eth_dev structure.
1193 * Nonzero if the callback process can be called immediately.
1196 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
1198 struct ibv_async_event event;
1199 struct rte_eth_link *link = &dev->data->dev_link;
1202 /* Read all message and acknowledge them. */
1204 if (ibv_get_async_event(priv->ctx, &event))
1207 if (event.event_type != IBV_EVENT_PORT_ACTIVE &&
1208 event.event_type != IBV_EVENT_PORT_ERR)
1209 DEBUG("event type %d on port %d not handled",
1210 event.event_type, event.element.port_num);
1211 ibv_ack_async_event(&event);
1213 mlx5_link_update(dev, 0);
1214 if (((link->link_speed == 0) && link->link_status) ||
1215 ((link->link_speed != 0) && !link->link_status)) {
1216 if (!priv->pending_alarm) {
1217 /* Inconsistent status, check again later. */
1218 priv->pending_alarm = 1;
1219 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1220 mlx5_dev_link_status_handler,
1230 * Handle delayed link status event.
1233 * Registered argument.
1236 mlx5_dev_link_status_handler(void *arg)
1238 struct rte_eth_dev *dev = arg;
1239 struct priv *priv = dev->data->dev_private;
1243 assert(priv->pending_alarm == 1);
1244 priv->pending_alarm = 0;
1245 ret = priv_dev_link_status_handler(priv, dev);
1248 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1252 * Handle interrupts from the NIC.
1254 * @param[in] intr_handle
1255 * Interrupt handler.
1257 * Callback argument.
1260 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
1262 struct rte_eth_dev *dev = cb_arg;
1263 struct priv *priv = dev->data->dev_private;
1268 ret = priv_dev_link_status_handler(priv, dev);
1271 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1275 * Uninstall interrupt handler.
1278 * Pointer to private structure.
1280 * Pointer to the rte_eth_dev structure.
1283 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1285 if (!dev->data->dev_conf.intr_conf.lsc)
1287 rte_intr_callback_unregister(&priv->intr_handle,
1288 mlx5_dev_interrupt_handler,
1290 if (priv->pending_alarm)
1291 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1292 priv->pending_alarm = 0;
1293 priv->intr_handle.fd = 0;
1294 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1298 * Install interrupt handler.
1301 * Pointer to private structure.
1303 * Pointer to the rte_eth_dev structure.
1306 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1310 if (!dev->data->dev_conf.intr_conf.lsc)
1312 assert(priv->ctx->async_fd > 0);
1313 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1314 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1316 INFO("failed to change file descriptor async event queue");
1317 dev->data->dev_conf.intr_conf.lsc = 0;
1319 priv->intr_handle.fd = priv->ctx->async_fd;
1320 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1321 rte_intr_callback_register(&priv->intr_handle,
1322 mlx5_dev_interrupt_handler,
1328 * Change the link state (UP / DOWN).
1331 * Pointer to Ethernet device structure.
1333 * Nonzero for link up, otherwise link down.
1336 * 0 on success, errno value on failure.
1339 priv_set_link(struct priv *priv, int up)
1341 struct rte_eth_dev *dev = priv->dev;
1345 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1348 priv_select_tx_function(priv);
1349 priv_select_rx_function(priv);
1351 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1354 dev->rx_pkt_burst = removed_rx_burst;
1355 dev->tx_pkt_burst = removed_tx_burst;
1361 * DPDK callback to bring the link DOWN.
1364 * Pointer to Ethernet device structure.
1367 * 0 on success, errno value on failure.
1370 mlx5_set_link_down(struct rte_eth_dev *dev)
1372 struct priv *priv = dev->data->dev_private;
1376 err = priv_set_link(priv, 0);
1382 * DPDK callback to bring the link UP.
1385 * Pointer to Ethernet device structure.
1388 * 0 on success, errno value on failure.
1391 mlx5_set_link_up(struct rte_eth_dev *dev)
1393 struct priv *priv = dev->data->dev_private;
1397 err = priv_set_link(priv, 1);
1403 * Configure secondary process queues from a private data pointer (primary
1404 * or secondary) and update burst callbacks. Can take place only once.
1406 * All queues must have been previously created by the primary process to
1407 * avoid undefined behavior.
1410 * Private data pointer from either primary or secondary process.
1413 * Private data pointer from secondary process, NULL in case of error.
1416 mlx5_secondary_data_setup(struct priv *priv)
1418 unsigned int port_id = 0;
1419 struct mlx5_secondary_data *sd;
1422 unsigned int nb_tx_queues;
1423 unsigned int nb_rx_queues;
1426 /* priv must be valid at this point. */
1427 assert(priv != NULL);
1428 /* priv->dev must also be valid but may point to local memory from
1429 * another process, possibly with the same address and must not
1430 * be dereferenced yet. */
1431 assert(priv->dev != NULL);
1432 /* Determine port ID by finding out where priv comes from. */
1434 sd = &mlx5_secondary_data[port_id];
1435 rte_spinlock_lock(&sd->lock);
1436 /* Primary process? */
1437 if (sd->primary_priv == priv)
1439 /* Secondary process? */
1440 if (sd->data.dev_private == priv)
1442 rte_spinlock_unlock(&sd->lock);
1443 if (++port_id == RTE_DIM(mlx5_secondary_data))
1446 /* Switch to secondary private structure. If private data has already
1447 * been updated by another thread, there is nothing else to do. */
1448 priv = sd->data.dev_private;
1449 if (priv->dev->data == &sd->data)
1451 /* Sanity checks. Secondary private structure is supposed to point
1452 * to local eth_dev, itself still pointing to the shared device data
1453 * structure allocated by the primary process. */
1454 assert(sd->shared_dev_data != &sd->data);
1455 assert(sd->data.nb_tx_queues == 0);
1456 assert(sd->data.tx_queues == NULL);
1457 assert(sd->data.nb_rx_queues == 0);
1458 assert(sd->data.rx_queues == NULL);
1459 assert(priv != sd->primary_priv);
1460 assert(priv->dev->data == sd->shared_dev_data);
1461 assert(priv->txqs_n == 0);
1462 assert(priv->txqs == NULL);
1463 assert(priv->rxqs_n == 0);
1464 assert(priv->rxqs == NULL);
1465 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1466 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1467 /* Allocate local storage for queues. */
1468 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1469 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1470 RTE_CACHE_LINE_SIZE);
1471 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1472 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1473 RTE_CACHE_LINE_SIZE);
1474 if (tx_queues == NULL || rx_queues == NULL)
1476 /* Lock to prevent control operations during setup. */
1479 for (i = 0; i != nb_tx_queues; ++i) {
1480 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1481 struct txq_ctrl *primary_txq_ctrl;
1482 struct txq_ctrl *txq_ctrl;
1484 if (primary_txq == NULL)
1486 primary_txq_ctrl = container_of(primary_txq,
1487 struct txq_ctrl, txq);
1488 txq_ctrl = rte_calloc_socket("TXQ", 1, sizeof(*txq_ctrl) +
1489 (1 << primary_txq->elts_n) *
1490 sizeof(struct rte_mbuf *), 0,
1491 primary_txq_ctrl->socket);
1492 if (txq_ctrl != NULL) {
1493 if (txq_ctrl_setup(priv->dev,
1495 1 << primary_txq->elts_n,
1496 primary_txq_ctrl->socket,
1498 txq_ctrl->txq.stats.idx =
1499 primary_txq->stats.idx;
1500 tx_queues[i] = &txq_ctrl->txq;
1506 txq_ctrl = tx_queues[--i];
1507 txq_cleanup(txq_ctrl);
1513 for (i = 0; i != nb_rx_queues; ++i) {
1514 struct rxq_ctrl *primary_rxq =
1515 container_of((*sd->primary_priv->rxqs)[i],
1516 struct rxq_ctrl, rxq);
1518 if (primary_rxq == NULL)
1520 /* Not supported yet. */
1521 rx_queues[i] = NULL;
1523 /* Update everything. */
1524 priv->txqs = (void *)tx_queues;
1525 priv->txqs_n = nb_tx_queues;
1526 priv->rxqs = (void *)rx_queues;
1527 priv->rxqs_n = nb_rx_queues;
1528 sd->data.rx_queues = rx_queues;
1529 sd->data.tx_queues = tx_queues;
1530 sd->data.nb_rx_queues = nb_rx_queues;
1531 sd->data.nb_tx_queues = nb_tx_queues;
1532 sd->data.dev_link = sd->shared_dev_data->dev_link;
1533 sd->data.mtu = sd->shared_dev_data->mtu;
1534 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1535 sizeof(sd->data.rx_queue_state));
1536 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1537 sizeof(sd->data.tx_queue_state));
1538 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1539 /* Use local data from now on. */
1541 priv->dev->data = &sd->data;
1543 priv_select_tx_function(priv);
1544 priv_select_rx_function(priv);
1547 /* More sanity checks. */
1548 assert(priv->dev->data == &sd->data);
1549 rte_spinlock_unlock(&sd->lock);
1553 rte_free(tx_queues);
1554 rte_free(rx_queues);
1555 rte_spinlock_unlock(&sd->lock);
1560 * Configure the TX function to use.
1563 * Pointer to private structure.
1566 priv_select_tx_function(struct priv *priv)
1568 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1569 /* Display warning for unsupported configurations. */
1570 if (priv->sriov && priv->mps)
1571 WARN("multi-packet send WQE cannot be used on a SR-IOV setup");
1572 /* Select appropriate TX function. */
1573 if ((priv->sriov == 0) && priv->mps && priv->txq_inline) {
1574 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1575 DEBUG("selected MPW inline TX function");
1576 } else if ((priv->sriov == 0) && priv->mps) {
1577 priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1578 DEBUG("selected MPW TX function");
1583 * Configure the RX function to use.
1586 * Pointer to private structure.
1589 priv_select_rx_function(struct priv *priv)
1591 priv->dev->rx_pkt_burst = mlx5_rx_burst;