1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <linux/ethtool.h>
23 #include <linux/sockios.h>
29 #include <rte_atomic.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_bus_pci.h>
33 #include <rte_common.h>
34 #include <rte_interrupts.h>
35 #include <rte_malloc.h>
36 #include <rte_string_fns.h>
37 #include <rte_rwlock.h>
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
81 uint8_t eth_tp_mdix_ctrl;
82 int8_t link_mode_masks_nwords;
84 uint32_t link_mode_masks[];
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
122 * Get master interface name from private structure.
125 * Pointer to Ethernet device.
127 * Interface name output buffer.
130 * 0 on success, a negative errno value otherwise and rte_errno is set.
133 mlx5_get_master_ifname(const struct rte_eth_dev *dev,
134 char (*ifname)[IF_NAMESIZE])
136 struct priv *priv = dev->data->dev_private;
139 unsigned int dev_type = 0;
140 unsigned int dev_port_prev = ~0u;
141 char match[IF_NAMESIZE] = "";
144 MKSTR(path, "%s/device/net", priv->ibdev_path);
152 while ((dent = readdir(dir)) != NULL) {
153 char *name = dent->d_name;
155 unsigned int dev_port;
158 if ((name[0] == '.') &&
159 ((name[1] == '\0') ||
160 ((name[1] == '.') && (name[2] == '\0'))))
163 MKSTR(path, "%s/device/net/%s/%s",
164 priv->ibdev_path, name,
165 (dev_type ? "dev_id" : "dev_port"));
167 file = fopen(path, "rb");
172 * Switch to dev_id when dev_port does not exist as
173 * is the case with Linux kernel versions < 3.15.
184 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
189 * Switch to dev_id when dev_port returns the same value for
190 * all ports. May happen when using a MOFED release older than
191 * 3.0 with a Linux kernel >= 3.15.
193 if (dev_port == dev_port_prev)
195 dev_port_prev = dev_port;
197 strlcpy(match, name, sizeof(match));
200 if (match[0] == '\0') {
204 strncpy(*ifname, match, sizeof(*ifname));
209 * Get interface name from private structure.
211 * This is a port representor-aware version of mlx5_get_master_ifname().
214 * Pointer to Ethernet device.
216 * Interface name output buffer.
219 * 0 on success, a negative errno value otherwise and rte_errno is set.
222 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
224 struct priv *priv = dev->data->dev_private;
225 unsigned int ifindex =
226 priv->nl_socket_rdma >= 0 ?
227 mlx5_nl_ifindex(priv->nl_socket_rdma, priv->ibdev_name) : 0;
230 if (!priv->representor)
231 return mlx5_get_master_ifname(dev, ifname);
235 if (if_indextoname(ifindex, &(*ifname)[0]))
242 * Get the interface index from device name.
245 * Pointer to Ethernet device.
248 * Nonzero interface index on success, zero otherwise and rte_errno is set.
251 mlx5_ifindex(const struct rte_eth_dev *dev)
253 char ifname[IF_NAMESIZE];
254 unsigned int ifindex;
256 if (mlx5_get_ifname(dev, &ifname))
258 ifindex = if_nametoindex(ifname);
265 * Perform ifreq ioctl() on associated Ethernet device.
268 * Pointer to Ethernet device.
270 * Request number to pass to ioctl().
272 * Interface request structure output buffer.
274 * When device is a port representor, perform request on master device
278 * 0 on success, a negative errno value otherwise and rte_errno is set.
281 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr,
284 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
292 ret = mlx5_get_master_ifname(dev, &ifr->ifr_name);
294 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
297 ret = ioctl(sock, req, ifr);
313 * Pointer to Ethernet device.
315 * MTU value output buffer.
318 * 0 on success, a negative errno value otherwise and rte_errno is set.
321 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
323 struct ifreq request;
324 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request, 0);
328 *mtu = request.ifr_mtu;
336 * Pointer to Ethernet device.
341 * 0 on success, a negative errno value otherwise and rte_errno is set.
344 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
346 struct ifreq request = { .ifr_mtu = mtu, };
348 return mlx5_ifreq(dev, SIOCSIFMTU, &request, 0);
355 * Pointer to Ethernet device.
357 * Bitmask for flags that must remain untouched.
359 * Bitmask for flags to modify.
362 * 0 on success, a negative errno value otherwise and rte_errno is set.
365 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
367 struct ifreq request;
368 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request, 0);
372 request.ifr_flags &= keep;
373 request.ifr_flags |= flags & ~keep;
374 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request, 0);
378 * DPDK callback for Ethernet device configuration.
381 * Pointer to Ethernet device structure.
384 * 0 on success, a negative errno value otherwise and rte_errno is set.
387 mlx5_dev_configure(struct rte_eth_dev *dev)
389 struct priv *priv = dev->data->dev_private;
390 unsigned int rxqs_n = dev->data->nb_rx_queues;
391 unsigned int txqs_n = dev->data->nb_tx_queues;
394 unsigned int reta_idx_n;
395 const uint8_t use_app_rss_key =
396 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
399 if (use_app_rss_key &&
400 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
401 MLX5_RSS_HASH_KEY_LEN)) {
402 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
403 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
407 priv->rss_conf.rss_key =
408 rte_realloc(priv->rss_conf.rss_key,
409 MLX5_RSS_HASH_KEY_LEN, 0);
410 if (!priv->rss_conf.rss_key) {
411 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
412 dev->data->port_id, rxqs_n);
416 memcpy(priv->rss_conf.rss_key,
418 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
419 rss_hash_default_key,
420 MLX5_RSS_HASH_KEY_LEN);
421 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
422 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
423 priv->rxqs = (void *)dev->data->rx_queues;
424 priv->txqs = (void *)dev->data->tx_queues;
425 if (txqs_n != priv->txqs_n) {
426 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
427 dev->data->port_id, priv->txqs_n, txqs_n);
428 priv->txqs_n = txqs_n;
430 if (rxqs_n > priv->config.ind_table_max_size) {
431 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
432 dev->data->port_id, rxqs_n);
436 if (rxqs_n == priv->rxqs_n)
438 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
439 dev->data->port_id, priv->rxqs_n, rxqs_n);
440 priv->rxqs_n = rxqs_n;
441 /* If the requested number of RX queues is not a power of two, use the
442 * maximum indirection table size for better balancing.
443 * The result is always rounded to the next power of two. */
444 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
445 priv->config.ind_table_max_size :
447 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
450 /* When the number of RX queues is not a power of two, the remaining
451 * table entries are padded with reused WQs and hashes are not spread
453 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
454 (*priv->reta_idx)[i] = j;
462 * Sets default tuning parameters.
465 * Pointer to Ethernet device.
467 * Info structure output buffer.
470 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
472 struct priv *priv = dev->data->dev_private;
474 /* Minimum CPU utilization. */
475 info->default_rxportconf.ring_size = 256;
476 info->default_txportconf.ring_size = 256;
477 info->default_rxportconf.burst_size = 64;
478 info->default_txportconf.burst_size = 64;
479 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
480 info->default_rxportconf.nb_queues = 16;
481 info->default_txportconf.nb_queues = 16;
482 if (dev->data->nb_rx_queues > 2 ||
483 dev->data->nb_tx_queues > 2) {
484 /* Max Throughput. */
485 info->default_rxportconf.ring_size = 2048;
486 info->default_txportconf.ring_size = 2048;
489 info->default_rxportconf.nb_queues = 8;
490 info->default_txportconf.nb_queues = 8;
491 if (dev->data->nb_rx_queues > 2 ||
492 dev->data->nb_tx_queues > 2) {
493 /* Max Throughput. */
494 info->default_rxportconf.ring_size = 4096;
495 info->default_txportconf.ring_size = 4096;
501 * DPDK callback to get information about the device.
504 * Pointer to Ethernet device structure.
506 * Info structure output buffer.
509 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
511 struct priv *priv = dev->data->dev_private;
512 struct mlx5_dev_config *config = &priv->config;
514 char ifname[IF_NAMESIZE];
516 /* FIXME: we should ask the device for these values. */
517 info->min_rx_bufsize = 32;
518 info->max_rx_pktlen = 65536;
520 * Since we need one CQ per QP, the limit is the minimum number
521 * between the two values.
523 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
524 priv->device_attr.orig_attr.max_qp);
525 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
528 info->max_rx_queues = max;
529 info->max_tx_queues = max;
530 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
531 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
532 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
533 info->rx_queue_offload_capa);
534 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
535 if (mlx5_get_ifname(dev, &ifname) == 0)
536 info->if_index = if_nametoindex(ifname);
537 info->reta_size = priv->reta_idx_n ?
538 priv->reta_idx_n : config->ind_table_max_size;
539 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
540 info->speed_capa = priv->link_speed_capa;
541 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
542 mlx5_set_default_params(dev, info);
543 info->switch_info.name = dev->data->name;
544 info->switch_info.domain_id = priv->domain_id;
545 info->switch_info.port_id = priv->representor_id;
546 if (priv->representor) {
547 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
550 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
553 rte_eth_devices[port_id[i]].data->dev_private;
556 opriv->representor ||
557 opriv->domain_id != priv->domain_id)
560 * Override switch name with that of the master
563 info->switch_info.name = opriv->dev_data->name;
570 * Get supported packet types.
573 * Pointer to Ethernet device structure.
576 * A pointer to the supported Packet types array.
579 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
581 static const uint32_t ptypes[] = {
582 /* refers to rxq_cq_to_pkt_type() */
584 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
585 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
586 RTE_PTYPE_L4_NONFRAG,
590 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
591 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
592 RTE_PTYPE_INNER_L4_NONFRAG,
593 RTE_PTYPE_INNER_L4_FRAG,
594 RTE_PTYPE_INNER_L4_TCP,
595 RTE_PTYPE_INNER_L4_UDP,
599 if (dev->rx_pkt_burst == mlx5_rx_burst ||
600 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
601 dev->rx_pkt_burst == mlx5_rx_burst_vec)
607 * DPDK callback to retrieve physical link information.
610 * Pointer to Ethernet device structure.
612 * Storage for current link status.
615 * 0 on success, a negative errno value otherwise and rte_errno is set.
618 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
619 struct rte_eth_link *link)
621 struct priv *priv = dev->data->dev_private;
622 struct ethtool_cmd edata = {
623 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
626 struct rte_eth_link dev_link;
630 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr, 1);
632 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
633 dev->data->port_id, strerror(rte_errno));
636 memset(&dev_link, 0, sizeof(dev_link));
637 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
638 (ifr.ifr_flags & IFF_RUNNING));
639 ifr.ifr_data = (void *)&edata;
640 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
643 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
644 dev->data->port_id, strerror(rte_errno));
647 link_speed = ethtool_cmd_speed(&edata);
648 if (link_speed == -1)
649 dev_link.link_speed = ETH_SPEED_NUM_NONE;
651 dev_link.link_speed = link_speed;
652 priv->link_speed_capa = 0;
653 if (edata.supported & SUPPORTED_Autoneg)
654 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
655 if (edata.supported & (SUPPORTED_1000baseT_Full |
656 SUPPORTED_1000baseKX_Full))
657 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
658 if (edata.supported & SUPPORTED_10000baseKR_Full)
659 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
660 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
661 SUPPORTED_40000baseCR4_Full |
662 SUPPORTED_40000baseSR4_Full |
663 SUPPORTED_40000baseLR4_Full))
664 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
665 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
666 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
667 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
668 ETH_LINK_SPEED_FIXED);
669 if ((dev_link.link_speed && !dev_link.link_status) ||
670 (!dev_link.link_speed && dev_link.link_status)) {
679 * Retrieve physical link information (unlocked version using new ioctl).
682 * Pointer to Ethernet device structure.
684 * Storage for current link status.
687 * 0 on success, a negative errno value otherwise and rte_errno is set.
690 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
691 struct rte_eth_link *link)
694 struct priv *priv = dev->data->dev_private;
695 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
697 struct rte_eth_link dev_link;
701 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr, 1);
703 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
704 dev->data->port_id, strerror(rte_errno));
707 memset(&dev_link, 0, sizeof(dev_link));
708 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
709 (ifr.ifr_flags & IFF_RUNNING));
710 ifr.ifr_data = (void *)&gcmd;
711 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
714 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
716 dev->data->port_id, strerror(rte_errno));
719 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
721 alignas(struct ethtool_link_settings)
722 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
723 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
724 struct ethtool_link_settings *ecmd = (void *)data;
727 ifr.ifr_data = (void *)ecmd;
728 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
731 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
733 dev->data->port_id, strerror(rte_errno));
736 dev_link.link_speed = ecmd->speed;
737 sc = ecmd->link_mode_masks[0] |
738 ((uint64_t)ecmd->link_mode_masks[1] << 32);
739 priv->link_speed_capa = 0;
740 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
741 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
742 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
743 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
744 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
745 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
746 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
747 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
748 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
749 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
750 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
751 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
752 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
753 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
754 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
755 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
756 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
757 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
758 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
759 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
760 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
761 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
762 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
763 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
764 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
765 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
766 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
767 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
768 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
769 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
770 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
771 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
772 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
773 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
774 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
775 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
776 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
777 ETH_LINK_SPEED_FIXED);
778 if ((dev_link.link_speed && !dev_link.link_status) ||
779 (!dev_link.link_speed && dev_link.link_status)) {
788 * DPDK callback to retrieve physical link information.
791 * Pointer to Ethernet device structure.
792 * @param wait_to_complete
793 * Wait for request completion.
796 * 0 if link status was not updated, positive if it was, a negative errno
797 * value otherwise and rte_errno is set.
800 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
803 struct rte_eth_link dev_link;
804 time_t start_time = time(NULL);
807 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
809 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
812 /* Handle wait to complete situation. */
813 if (wait_to_complete && ret == -EAGAIN) {
814 if (abs((int)difftime(time(NULL), start_time)) <
815 MLX5_LINK_STATUS_TIMEOUT) {
822 } else if (ret < 0) {
825 } while (wait_to_complete);
826 ret = !!memcmp(&dev->data->dev_link, &dev_link,
827 sizeof(struct rte_eth_link));
828 dev->data->dev_link = dev_link;
833 * DPDK callback to change the MTU.
836 * Pointer to Ethernet device structure.
841 * 0 on success, a negative errno value otherwise and rte_errno is set.
844 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
846 struct priv *priv = dev->data->dev_private;
847 uint16_t kern_mtu = 0;
850 ret = mlx5_get_mtu(dev, &kern_mtu);
853 /* Set kernel interface MTU first. */
854 ret = mlx5_set_mtu(dev, mtu);
857 ret = mlx5_get_mtu(dev, &kern_mtu);
860 if (kern_mtu == mtu) {
862 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
863 dev->data->port_id, mtu);
871 * DPDK callback to get flow control status.
874 * Pointer to Ethernet device structure.
875 * @param[out] fc_conf
876 * Flow control output buffer.
879 * 0 on success, a negative errno value otherwise and rte_errno is set.
882 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
885 struct ethtool_pauseparam ethpause = {
886 .cmd = ETHTOOL_GPAUSEPARAM
890 ifr.ifr_data = (void *)ðpause;
891 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
894 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
896 dev->data->port_id, strerror(rte_errno));
899 fc_conf->autoneg = ethpause.autoneg;
900 if (ethpause.rx_pause && ethpause.tx_pause)
901 fc_conf->mode = RTE_FC_FULL;
902 else if (ethpause.rx_pause)
903 fc_conf->mode = RTE_FC_RX_PAUSE;
904 else if (ethpause.tx_pause)
905 fc_conf->mode = RTE_FC_TX_PAUSE;
907 fc_conf->mode = RTE_FC_NONE;
912 * DPDK callback to modify flow control parameters.
915 * Pointer to Ethernet device structure.
917 * Flow control parameters.
920 * 0 on success, a negative errno value otherwise and rte_errno is set.
923 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
926 struct ethtool_pauseparam ethpause = {
927 .cmd = ETHTOOL_SPAUSEPARAM
931 ifr.ifr_data = (void *)ðpause;
932 ethpause.autoneg = fc_conf->autoneg;
933 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
934 (fc_conf->mode & RTE_FC_RX_PAUSE))
935 ethpause.rx_pause = 1;
937 ethpause.rx_pause = 0;
939 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
940 (fc_conf->mode & RTE_FC_TX_PAUSE))
941 ethpause.tx_pause = 1;
943 ethpause.tx_pause = 0;
944 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
947 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
949 dev->data->port_id, strerror(rte_errno));
956 * Get PCI information from struct ibv_device.
959 * Pointer to Ethernet device structure.
960 * @param[out] pci_addr
961 * PCI bus address output buffer.
964 * 0 on success, a negative errno value otherwise and rte_errno is set.
967 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
968 struct rte_pci_addr *pci_addr)
972 MKSTR(path, "%s/device/uevent", device->ibdev_path);
974 file = fopen(path, "rb");
979 while (fgets(line, sizeof(line), file) == line) {
980 size_t len = strlen(line);
983 /* Truncate long lines. */
984 if (len == (sizeof(line) - 1))
985 while (line[(len - 1)] != '\n') {
989 line[(len - 1)] = ret;
991 /* Extract information. */
994 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
998 &pci_addr->function) == 4) {
1008 * Device status handler.
1011 * Pointer to Ethernet device.
1013 * Pointer to event flags holder.
1016 * Events bitmap of callback process which can be called immediately.
1019 mlx5_dev_status_handler(struct rte_eth_dev *dev)
1021 struct priv *priv = dev->data->dev_private;
1022 struct ibv_async_event event;
1025 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1029 /* Read all message and acknowledge them. */
1031 if (mlx5_glue->get_async_event(priv->ctx, &event))
1033 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1034 event.event_type == IBV_EVENT_PORT_ERR) &&
1035 (dev->data->dev_conf.intr_conf.lsc == 1))
1036 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1037 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1038 dev->data->dev_conf.intr_conf.rmv == 1)
1039 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1042 "port %u event type %d on not handled",
1043 dev->data->port_id, event.event_type);
1044 mlx5_glue->ack_async_event(&event);
1050 * Handle interrupts from the NIC.
1052 * @param[in] intr_handle
1053 * Interrupt handler.
1055 * Callback argument.
1058 mlx5_dev_interrupt_handler(void *cb_arg)
1060 struct rte_eth_dev *dev = cb_arg;
1063 events = mlx5_dev_status_handler(dev);
1064 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1065 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1066 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1067 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1071 * Handle interrupts from the socket.
1074 * Callback argument.
1077 mlx5_dev_handler_socket(void *cb_arg)
1079 struct rte_eth_dev *dev = cb_arg;
1081 mlx5_socket_handle(dev);
1085 * Uninstall interrupt handler.
1088 * Pointer to Ethernet device.
1091 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1093 struct priv *priv = dev->data->dev_private;
1095 if (dev->data->dev_conf.intr_conf.lsc ||
1096 dev->data->dev_conf.intr_conf.rmv)
1097 rte_intr_callback_unregister(&priv->intr_handle,
1098 mlx5_dev_interrupt_handler, dev);
1099 if (priv->primary_socket)
1100 rte_intr_callback_unregister(&priv->intr_handle_socket,
1101 mlx5_dev_handler_socket, dev);
1102 priv->intr_handle.fd = 0;
1103 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1104 priv->intr_handle_socket.fd = 0;
1105 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1109 * Install interrupt handler.
1112 * Pointer to Ethernet device.
1115 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1117 struct priv *priv = dev->data->dev_private;
1121 assert(priv->ctx->async_fd > 0);
1122 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1123 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1126 "port %u failed to change file descriptor async event"
1128 dev->data->port_id);
1129 dev->data->dev_conf.intr_conf.lsc = 0;
1130 dev->data->dev_conf.intr_conf.rmv = 0;
1132 if (dev->data->dev_conf.intr_conf.lsc ||
1133 dev->data->dev_conf.intr_conf.rmv) {
1134 priv->intr_handle.fd = priv->ctx->async_fd;
1135 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1136 rte_intr_callback_register(&priv->intr_handle,
1137 mlx5_dev_interrupt_handler, dev);
1139 ret = mlx5_socket_init(dev);
1141 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1142 dev->data->port_id, strerror(rte_errno));
1143 else if (priv->primary_socket) {
1144 priv->intr_handle_socket.fd = priv->primary_socket;
1145 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1146 rte_intr_callback_register(&priv->intr_handle_socket,
1147 mlx5_dev_handler_socket, dev);
1152 * DPDK callback to bring the link DOWN.
1155 * Pointer to Ethernet device structure.
1158 * 0 on success, a negative errno value otherwise and rte_errno is set.
1161 mlx5_set_link_down(struct rte_eth_dev *dev)
1163 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1167 * DPDK callback to bring the link UP.
1170 * Pointer to Ethernet device structure.
1173 * 0 on success, a negative errno value otherwise and rte_errno is set.
1176 mlx5_set_link_up(struct rte_eth_dev *dev)
1178 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1182 * Configure the TX function to use.
1185 * Pointer to private data structure.
1188 * Pointer to selected Tx burst function.
1191 mlx5_select_tx_function(struct rte_eth_dev *dev)
1193 struct priv *priv = dev->data->dev_private;
1194 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1195 struct mlx5_dev_config *config = &priv->config;
1196 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1197 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1198 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1199 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1200 DEV_TX_OFFLOAD_IP_TNL_TSO |
1201 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1202 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1203 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1204 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1205 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1207 assert(priv != NULL);
1208 /* Select appropriate TX function. */
1209 if (vlan_insert || tso || swp)
1210 return tx_pkt_burst;
1211 if (config->mps == MLX5_MPW_ENHANCED) {
1212 if (mlx5_check_vec_tx_support(dev) > 0) {
1213 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1214 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1216 tx_pkt_burst = mlx5_tx_burst_vec;
1218 "port %u selected enhanced MPW Tx vectorized"
1220 dev->data->port_id);
1222 tx_pkt_burst = mlx5_tx_burst_empw;
1224 "port %u selected enhanced MPW Tx function",
1225 dev->data->port_id);
1227 } else if (config->mps && (config->txq_inline > 0)) {
1228 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1229 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1230 dev->data->port_id);
1231 } else if (config->mps) {
1232 tx_pkt_burst = mlx5_tx_burst_mpw;
1233 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1234 dev->data->port_id);
1236 return tx_pkt_burst;
1240 * Configure the RX function to use.
1243 * Pointer to private data structure.
1246 * Pointer to selected Rx burst function.
1249 mlx5_select_rx_function(struct rte_eth_dev *dev)
1251 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1253 assert(dev != NULL);
1254 if (mlx5_check_vec_rx_support(dev) > 0) {
1255 rx_pkt_burst = mlx5_rx_burst_vec;
1256 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1257 dev->data->port_id);
1258 } else if (mlx5_mprq_enabled(dev)) {
1259 rx_pkt_burst = mlx5_rx_burst_mprq;
1261 return rx_pkt_burst;
1265 * Check if mlx5 device was removed.
1268 * Pointer to Ethernet device structure.
1271 * 1 when device is removed, otherwise 0.
1274 mlx5_is_removed(struct rte_eth_dev *dev)
1276 struct ibv_device_attr device_attr;
1277 struct priv *priv = dev->data->dev_private;
1279 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1285 * Get port ID list of mlx5 instances sharing a common device.
1288 * Device to look for.
1289 * @param[out] port_list
1290 * Result buffer for collected port IDs.
1291 * @param port_list_n
1292 * Maximum number of entries in result buffer. If 0, @p port_list can be
1296 * Number of matching instances regardless of the @p port_list_n
1297 * parameter, 0 if none were found.
1300 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1301 unsigned int port_list_n)
1306 RTE_ETH_FOREACH_DEV(id) {
1307 struct rte_eth_dev *ldev = &rte_eth_devices[id];
1309 if (!ldev->device ||
1310 !ldev->device->driver ||
1311 strcmp(ldev->device->driver->name, MLX5_DRIVER_NAME) ||
1312 ldev->device != dev)
1314 if (n < port_list_n)
1322 * Get switch information associated with network interface.
1325 * Network interface index.
1327 * Switch information object, populated in case of success.
1330 * 0 on success, a negative errno value otherwise and rte_errno is set.
1333 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1335 char ifname[IF_NAMESIZE];
1337 struct mlx5_switch_info data = { .master = 0, };
1338 bool port_name_set = false;
1339 bool port_switch_id_set = false;
1342 if (!if_indextoname(ifindex, ifname)) {
1347 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1349 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1352 file = fopen(phys_port_name, "rb");
1355 fscanf(file, "%d%c", &data.port_name, &c) == 2 &&
1359 file = fopen(phys_switch_id, "rb");
1364 port_switch_id_set =
1365 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1368 data.master = port_switch_id_set && !port_name_set;
1369 data.representor = port_switch_id_set && port_name_set;