1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/sockios.h>
27 #include <rte_atomic.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_bus_pci.h>
31 #include <rte_common.h>
32 #include <rte_interrupts.h>
33 #include <rte_malloc.h>
34 #include <rte_string_fns.h>
35 #include <rte_rwlock.h>
38 #include "mlx5_glue.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
42 /* Supported speed values found in /usr/include/linux/ethtool.h */
43 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
44 #define SUPPORTED_40000baseKR4_Full (1 << 23)
46 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
47 #define SUPPORTED_40000baseCR4_Full (1 << 24)
49 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
50 #define SUPPORTED_40000baseSR4_Full (1 << 25)
52 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
53 #define SUPPORTED_40000baseLR4_Full (1 << 26)
55 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
56 #define SUPPORTED_56000baseKR4_Full (1 << 27)
58 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
59 #define SUPPORTED_56000baseCR4_Full (1 << 28)
61 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
62 #define SUPPORTED_56000baseSR4_Full (1 << 29)
64 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
65 #define SUPPORTED_56000baseLR4_Full (1 << 30)
68 /* Add defines in case the running kernel is not the same as user headers. */
69 #ifndef ETHTOOL_GLINKSETTINGS
70 struct ethtool_link_settings {
79 uint8_t eth_tp_mdix_ctrl;
80 int8_t link_mode_masks_nwords;
82 uint32_t link_mode_masks[];
85 #define ETHTOOL_GLINKSETTINGS 0x0000004c
86 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
87 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
88 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
89 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
90 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
91 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
92 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
93 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
94 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
95 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
96 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
97 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
98 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
99 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
100 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
101 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
103 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
104 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
105 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
106 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
108 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
109 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
110 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
112 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
113 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
114 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
115 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
116 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
120 * Get master interface name from private structure.
123 * Pointer to Ethernet device.
125 * Interface name output buffer.
128 * 0 on success, a negative errno value otherwise and rte_errno is set.
131 mlx5_get_master_ifname(const struct rte_eth_dev *dev,
132 char (*ifname)[IF_NAMESIZE])
134 struct mlx5_priv *priv = dev->data->dev_private;
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
142 MKSTR(path, "%s/device/net", priv->ibdev_path);
150 while ((dent = readdir(dir)) != NULL) {
151 char *name = dent->d_name;
153 unsigned int dev_port;
156 if ((name[0] == '.') &&
157 ((name[1] == '\0') ||
158 ((name[1] == '.') && (name[2] == '\0'))))
161 MKSTR(path, "%s/device/net/%s/%s",
162 priv->ibdev_path, name,
163 (dev_type ? "dev_id" : "dev_port"));
165 file = fopen(path, "rb");
170 * Switch to dev_id when dev_port does not exist as
171 * is the case with Linux kernel versions < 3.15.
182 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
187 * Switch to dev_id when dev_port returns the same value for
188 * all ports. May happen when using a MOFED release older than
189 * 3.0 with a Linux kernel >= 3.15.
191 if (dev_port == dev_port_prev)
193 dev_port_prev = dev_port;
195 strlcpy(match, name, sizeof(match));
198 if (match[0] == '\0') {
202 strncpy(*ifname, match, sizeof(*ifname));
207 * Get interface name from private structure.
209 * This is a port representor-aware version of mlx5_get_master_ifname().
212 * Pointer to Ethernet device.
214 * Interface name output buffer.
217 * 0 on success, a negative errno value otherwise and rte_errno is set.
220 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
222 struct mlx5_priv *priv = dev->data->dev_private;
223 unsigned int ifindex =
224 priv->nl_socket_rdma >= 0 ?
225 mlx5_nl_ifindex(priv->nl_socket_rdma, priv->ibdev_name) : 0;
228 if (!priv->representor)
229 return mlx5_get_master_ifname(dev, ifname);
233 if (if_indextoname(ifindex, &(*ifname)[0]))
240 * Get the interface index from device name.
243 * Pointer to Ethernet device.
246 * Nonzero interface index on success, zero otherwise and rte_errno is set.
249 mlx5_ifindex(const struct rte_eth_dev *dev)
251 char ifname[IF_NAMESIZE];
252 unsigned int ifindex;
254 if (mlx5_get_ifname(dev, &ifname))
256 ifindex = if_nametoindex(ifname);
263 * Perform ifreq ioctl() on associated Ethernet device.
266 * Pointer to Ethernet device.
268 * Request number to pass to ioctl().
270 * Interface request structure output buffer.
273 * 0 on success, a negative errno value otherwise and rte_errno is set.
276 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
278 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
285 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
288 ret = ioctl(sock, req, ifr);
304 * Pointer to Ethernet device.
306 * MTU value output buffer.
309 * 0 on success, a negative errno value otherwise and rte_errno is set.
312 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
314 struct ifreq request;
315 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
319 *mtu = request.ifr_mtu;
327 * Pointer to Ethernet device.
332 * 0 on success, a negative errno value otherwise and rte_errno is set.
335 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
337 struct ifreq request = { .ifr_mtu = mtu, };
339 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
346 * Pointer to Ethernet device.
348 * Bitmask for flags that must remain untouched.
350 * Bitmask for flags to modify.
353 * 0 on success, a negative errno value otherwise and rte_errno is set.
356 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
358 struct ifreq request;
359 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
363 request.ifr_flags &= keep;
364 request.ifr_flags |= flags & ~keep;
365 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
369 * DPDK callback for Ethernet device configuration.
372 * Pointer to Ethernet device structure.
375 * 0 on success, a negative errno value otherwise and rte_errno is set.
378 mlx5_dev_configure(struct rte_eth_dev *dev)
380 struct mlx5_priv *priv = dev->data->dev_private;
381 unsigned int rxqs_n = dev->data->nb_rx_queues;
382 unsigned int txqs_n = dev->data->nb_tx_queues;
385 unsigned int reta_idx_n;
386 const uint8_t use_app_rss_key =
387 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
390 if (use_app_rss_key &&
391 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
392 MLX5_RSS_HASH_KEY_LEN)) {
393 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
394 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
398 priv->rss_conf.rss_key =
399 rte_realloc(priv->rss_conf.rss_key,
400 MLX5_RSS_HASH_KEY_LEN, 0);
401 if (!priv->rss_conf.rss_key) {
402 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
403 dev->data->port_id, rxqs_n);
407 memcpy(priv->rss_conf.rss_key,
409 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
410 rss_hash_default_key,
411 MLX5_RSS_HASH_KEY_LEN);
412 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
413 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
414 priv->rxqs = (void *)dev->data->rx_queues;
415 priv->txqs = (void *)dev->data->tx_queues;
416 if (txqs_n != priv->txqs_n) {
417 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
418 dev->data->port_id, priv->txqs_n, txqs_n);
419 priv->txqs_n = txqs_n;
421 if (rxqs_n > priv->config.ind_table_max_size) {
422 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
423 dev->data->port_id, rxqs_n);
427 if (rxqs_n == priv->rxqs_n)
429 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
430 dev->data->port_id, priv->rxqs_n, rxqs_n);
431 priv->rxqs_n = rxqs_n;
432 /* If the requested number of RX queues is not a power of two, use the
433 * maximum indirection table size for better balancing.
434 * The result is always rounded to the next power of two. */
435 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
436 priv->config.ind_table_max_size :
438 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
441 /* When the number of RX queues is not a power of two, the remaining
442 * table entries are padded with reused WQs and hashes are not spread
444 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
445 (*priv->reta_idx)[i] = j;
453 * Sets default tuning parameters.
456 * Pointer to Ethernet device.
458 * Info structure output buffer.
461 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
463 struct mlx5_priv *priv = dev->data->dev_private;
465 /* Minimum CPU utilization. */
466 info->default_rxportconf.ring_size = 256;
467 info->default_txportconf.ring_size = 256;
468 info->default_rxportconf.burst_size = 64;
469 info->default_txportconf.burst_size = 64;
470 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
471 info->default_rxportconf.nb_queues = 16;
472 info->default_txportconf.nb_queues = 16;
473 if (dev->data->nb_rx_queues > 2 ||
474 dev->data->nb_tx_queues > 2) {
475 /* Max Throughput. */
476 info->default_rxportconf.ring_size = 2048;
477 info->default_txportconf.ring_size = 2048;
480 info->default_rxportconf.nb_queues = 8;
481 info->default_txportconf.nb_queues = 8;
482 if (dev->data->nb_rx_queues > 2 ||
483 dev->data->nb_tx_queues > 2) {
484 /* Max Throughput. */
485 info->default_rxportconf.ring_size = 4096;
486 info->default_txportconf.ring_size = 4096;
492 * DPDK callback to get information about the device.
495 * Pointer to Ethernet device structure.
497 * Info structure output buffer.
500 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
502 struct mlx5_priv *priv = dev->data->dev_private;
503 struct mlx5_dev_config *config = &priv->config;
505 char ifname[IF_NAMESIZE];
507 /* FIXME: we should ask the device for these values. */
508 info->min_rx_bufsize = 32;
509 info->max_rx_pktlen = 65536;
511 * Since we need one CQ per QP, the limit is the minimum number
512 * between the two values.
514 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
515 priv->device_attr.orig_attr.max_qp);
516 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
519 info->max_rx_queues = max;
520 info->max_tx_queues = max;
521 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
522 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
523 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
524 info->rx_queue_offload_capa);
525 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
526 if (mlx5_get_ifname(dev, &ifname) == 0)
527 info->if_index = if_nametoindex(ifname);
528 info->reta_size = priv->reta_idx_n ?
529 priv->reta_idx_n : config->ind_table_max_size;
530 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
531 info->speed_capa = priv->link_speed_capa;
532 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
533 mlx5_set_default_params(dev, info);
534 info->switch_info.name = dev->data->name;
535 info->switch_info.domain_id = priv->domain_id;
536 info->switch_info.port_id = priv->representor_id;
537 if (priv->representor) {
538 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
541 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
543 struct mlx5_priv *opriv =
544 rte_eth_devices[port_id[i]].data->dev_private;
547 opriv->representor ||
548 opriv->domain_id != priv->domain_id)
551 * Override switch name with that of the master
554 info->switch_info.name = opriv->dev_data->name;
561 * Get firmware version of a device.
564 * Ethernet device port.
566 * String output allocated by caller.
568 * Size of the output string, including terminating null byte.
571 * 0 on success, or the size of the non truncated string if too big.
573 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
575 struct mlx5_priv *priv = dev->data->dev_private;
576 struct ibv_device_attr *attr = &priv->device_attr.orig_attr;
577 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
582 strlcpy(fw_ver, attr->fw_ver, fw_size);
587 * Get supported packet types.
590 * Pointer to Ethernet device structure.
593 * A pointer to the supported Packet types array.
596 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
598 static const uint32_t ptypes[] = {
599 /* refers to rxq_cq_to_pkt_type() */
601 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
602 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
603 RTE_PTYPE_L4_NONFRAG,
607 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
608 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
609 RTE_PTYPE_INNER_L4_NONFRAG,
610 RTE_PTYPE_INNER_L4_FRAG,
611 RTE_PTYPE_INNER_L4_TCP,
612 RTE_PTYPE_INNER_L4_UDP,
616 if (dev->rx_pkt_burst == mlx5_rx_burst ||
617 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
618 dev->rx_pkt_burst == mlx5_rx_burst_vec)
624 * DPDK callback to retrieve physical link information.
627 * Pointer to Ethernet device structure.
629 * Storage for current link status.
632 * 0 on success, a negative errno value otherwise and rte_errno is set.
635 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
636 struct rte_eth_link *link)
638 struct mlx5_priv *priv = dev->data->dev_private;
639 struct ethtool_cmd edata = {
640 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
643 struct rte_eth_link dev_link;
647 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
649 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
650 dev->data->port_id, strerror(rte_errno));
653 dev_link = (struct rte_eth_link) {
654 .link_status = ((ifr.ifr_flags & IFF_UP) &&
655 (ifr.ifr_flags & IFF_RUNNING)),
657 ifr = (struct ifreq) {
658 .ifr_data = (void *)&edata,
660 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
663 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
664 dev->data->port_id, strerror(rte_errno));
667 link_speed = ethtool_cmd_speed(&edata);
668 if (link_speed == -1)
669 dev_link.link_speed = ETH_SPEED_NUM_NONE;
671 dev_link.link_speed = link_speed;
672 priv->link_speed_capa = 0;
673 if (edata.supported & SUPPORTED_Autoneg)
674 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
675 if (edata.supported & (SUPPORTED_1000baseT_Full |
676 SUPPORTED_1000baseKX_Full))
677 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
678 if (edata.supported & SUPPORTED_10000baseKR_Full)
679 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
680 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
681 SUPPORTED_40000baseCR4_Full |
682 SUPPORTED_40000baseSR4_Full |
683 SUPPORTED_40000baseLR4_Full))
684 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
685 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
686 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
687 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
688 ETH_LINK_SPEED_FIXED);
689 if (((dev_link.link_speed && !dev_link.link_status) ||
690 (!dev_link.link_speed && dev_link.link_status))) {
699 * Retrieve physical link information (unlocked version using new ioctl).
702 * Pointer to Ethernet device structure.
704 * Storage for current link status.
707 * 0 on success, a negative errno value otherwise and rte_errno is set.
710 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
711 struct rte_eth_link *link)
714 struct mlx5_priv *priv = dev->data->dev_private;
715 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
717 struct rte_eth_link dev_link;
721 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
723 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
724 dev->data->port_id, strerror(rte_errno));
727 dev_link = (struct rte_eth_link) {
728 .link_status = ((ifr.ifr_flags & IFF_UP) &&
729 (ifr.ifr_flags & IFF_RUNNING)),
731 ifr = (struct ifreq) {
732 .ifr_data = (void *)&gcmd,
734 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
737 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
739 dev->data->port_id, strerror(rte_errno));
742 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
744 alignas(struct ethtool_link_settings)
745 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
746 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
747 struct ethtool_link_settings *ecmd = (void *)data;
750 ifr.ifr_data = (void *)ecmd;
751 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
754 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
756 dev->data->port_id, strerror(rte_errno));
759 dev_link.link_speed = ecmd->speed;
760 sc = ecmd->link_mode_masks[0] |
761 ((uint64_t)ecmd->link_mode_masks[1] << 32);
762 priv->link_speed_capa = 0;
763 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
764 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
765 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
766 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
767 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
768 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
769 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
770 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
771 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
772 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
773 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
774 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
775 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
776 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
777 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
778 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
779 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
780 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
781 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
782 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
783 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
784 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
785 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
786 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
787 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
788 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
789 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
790 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
791 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
792 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
793 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
794 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
795 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
796 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
797 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
798 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
799 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
800 ETH_LINK_SPEED_FIXED);
801 if (((dev_link.link_speed && !dev_link.link_status) ||
802 (!dev_link.link_speed && dev_link.link_status))) {
811 * DPDK callback to retrieve physical link information.
814 * Pointer to Ethernet device structure.
815 * @param wait_to_complete
816 * Wait for request completion.
819 * 0 if link status was not updated, positive if it was, a negative errno
820 * value otherwise and rte_errno is set.
823 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
826 struct rte_eth_link dev_link;
827 time_t start_time = time(NULL);
830 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
832 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
835 /* Handle wait to complete situation. */
836 if (wait_to_complete && ret == -EAGAIN) {
837 if (abs((int)difftime(time(NULL), start_time)) <
838 MLX5_LINK_STATUS_TIMEOUT) {
845 } else if (ret < 0) {
848 } while (wait_to_complete);
849 ret = !!memcmp(&dev->data->dev_link, &dev_link,
850 sizeof(struct rte_eth_link));
851 dev->data->dev_link = dev_link;
856 * DPDK callback to change the MTU.
859 * Pointer to Ethernet device structure.
864 * 0 on success, a negative errno value otherwise and rte_errno is set.
867 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
869 struct mlx5_priv *priv = dev->data->dev_private;
870 uint16_t kern_mtu = 0;
873 ret = mlx5_get_mtu(dev, &kern_mtu);
876 /* Set kernel interface MTU first. */
877 ret = mlx5_set_mtu(dev, mtu);
880 ret = mlx5_get_mtu(dev, &kern_mtu);
883 if (kern_mtu == mtu) {
885 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
886 dev->data->port_id, mtu);
894 * DPDK callback to get flow control status.
897 * Pointer to Ethernet device structure.
898 * @param[out] fc_conf
899 * Flow control output buffer.
902 * 0 on success, a negative errno value otherwise and rte_errno is set.
905 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
908 struct ethtool_pauseparam ethpause = {
909 .cmd = ETHTOOL_GPAUSEPARAM
913 ifr.ifr_data = (void *)ðpause;
914 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
917 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
919 dev->data->port_id, strerror(rte_errno));
922 fc_conf->autoneg = ethpause.autoneg;
923 if (ethpause.rx_pause && ethpause.tx_pause)
924 fc_conf->mode = RTE_FC_FULL;
925 else if (ethpause.rx_pause)
926 fc_conf->mode = RTE_FC_RX_PAUSE;
927 else if (ethpause.tx_pause)
928 fc_conf->mode = RTE_FC_TX_PAUSE;
930 fc_conf->mode = RTE_FC_NONE;
935 * DPDK callback to modify flow control parameters.
938 * Pointer to Ethernet device structure.
940 * Flow control parameters.
943 * 0 on success, a negative errno value otherwise and rte_errno is set.
946 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
949 struct ethtool_pauseparam ethpause = {
950 .cmd = ETHTOOL_SPAUSEPARAM
954 ifr.ifr_data = (void *)ðpause;
955 ethpause.autoneg = fc_conf->autoneg;
956 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
957 (fc_conf->mode & RTE_FC_RX_PAUSE))
958 ethpause.rx_pause = 1;
960 ethpause.rx_pause = 0;
962 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
963 (fc_conf->mode & RTE_FC_TX_PAUSE))
964 ethpause.tx_pause = 1;
966 ethpause.tx_pause = 0;
967 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
970 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
972 dev->data->port_id, strerror(rte_errno));
979 * Get PCI information from struct ibv_device.
982 * Pointer to Ethernet device structure.
983 * @param[out] pci_addr
984 * PCI bus address output buffer.
987 * 0 on success, a negative errno value otherwise and rte_errno is set.
990 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
991 struct rte_pci_addr *pci_addr)
995 MKSTR(path, "%s/device/uevent", device->ibdev_path);
997 file = fopen(path, "rb");
1002 while (fgets(line, sizeof(line), file) == line) {
1003 size_t len = strlen(line);
1006 /* Truncate long lines. */
1007 if (len == (sizeof(line) - 1))
1008 while (line[(len - 1)] != '\n') {
1012 line[(len - 1)] = ret;
1014 /* Extract information. */
1017 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1021 &pci_addr->function) == 4) {
1031 * Device status handler.
1034 * Pointer to Ethernet device.
1036 * Pointer to event flags holder.
1039 * Events bitmap of callback process which can be called immediately.
1042 mlx5_dev_status_handler(struct rte_eth_dev *dev)
1044 struct mlx5_priv *priv = dev->data->dev_private;
1045 struct ibv_async_event event;
1048 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1052 /* Read all message and acknowledge them. */
1054 if (mlx5_glue->get_async_event(priv->ctx, &event))
1056 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1057 event.event_type == IBV_EVENT_PORT_ERR) &&
1058 (dev->data->dev_conf.intr_conf.lsc == 1))
1059 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1060 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1061 dev->data->dev_conf.intr_conf.rmv == 1)
1062 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1065 "port %u event type %d on not handled",
1066 dev->data->port_id, event.event_type);
1067 mlx5_glue->ack_async_event(&event);
1073 * Handle interrupts from the NIC.
1075 * @param[in] intr_handle
1076 * Interrupt handler.
1078 * Callback argument.
1081 mlx5_dev_interrupt_handler(void *cb_arg)
1083 struct rte_eth_dev *dev = cb_arg;
1086 events = mlx5_dev_status_handler(dev);
1087 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1088 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1089 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1090 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1094 * Handle interrupts from the socket.
1097 * Callback argument.
1100 mlx5_dev_handler_socket(void *cb_arg)
1102 struct rte_eth_dev *dev = cb_arg;
1104 mlx5_socket_handle(dev);
1108 * Uninstall interrupt handler.
1111 * Pointer to Ethernet device.
1114 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1116 struct mlx5_priv *priv = dev->data->dev_private;
1118 if (dev->data->dev_conf.intr_conf.lsc ||
1119 dev->data->dev_conf.intr_conf.rmv)
1120 rte_intr_callback_unregister(&priv->intr_handle,
1121 mlx5_dev_interrupt_handler, dev);
1122 if (priv->primary_socket)
1123 rte_intr_callback_unregister(&priv->intr_handle_socket,
1124 mlx5_dev_handler_socket, dev);
1125 priv->intr_handle.fd = 0;
1126 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1127 priv->intr_handle_socket.fd = 0;
1128 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1132 * Install interrupt handler.
1135 * Pointer to Ethernet device.
1138 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1140 struct mlx5_priv *priv = dev->data->dev_private;
1144 assert(priv->ctx->async_fd > 0);
1145 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1146 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1149 "port %u failed to change file descriptor async event"
1151 dev->data->port_id);
1152 dev->data->dev_conf.intr_conf.lsc = 0;
1153 dev->data->dev_conf.intr_conf.rmv = 0;
1155 if (dev->data->dev_conf.intr_conf.lsc ||
1156 dev->data->dev_conf.intr_conf.rmv) {
1157 priv->intr_handle.fd = priv->ctx->async_fd;
1158 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1159 rte_intr_callback_register(&priv->intr_handle,
1160 mlx5_dev_interrupt_handler, dev);
1162 ret = mlx5_socket_init(dev);
1164 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1165 dev->data->port_id, strerror(rte_errno));
1166 else if (priv->primary_socket) {
1167 priv->intr_handle_socket.fd = priv->primary_socket;
1168 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1169 rte_intr_callback_register(&priv->intr_handle_socket,
1170 mlx5_dev_handler_socket, dev);
1175 * DPDK callback to bring the link DOWN.
1178 * Pointer to Ethernet device structure.
1181 * 0 on success, a negative errno value otherwise and rte_errno is set.
1184 mlx5_set_link_down(struct rte_eth_dev *dev)
1186 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1190 * DPDK callback to bring the link UP.
1193 * Pointer to Ethernet device structure.
1196 * 0 on success, a negative errno value otherwise and rte_errno is set.
1199 mlx5_set_link_up(struct rte_eth_dev *dev)
1201 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1205 * Configure the TX function to use.
1208 * Pointer to private data structure.
1211 * Pointer to selected Tx burst function.
1214 mlx5_select_tx_function(struct rte_eth_dev *dev)
1216 struct mlx5_priv *priv = dev->data->dev_private;
1217 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1218 struct mlx5_dev_config *config = &priv->config;
1219 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1220 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1221 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1222 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1223 DEV_TX_OFFLOAD_IP_TNL_TSO |
1224 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1225 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1226 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1227 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1228 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1230 assert(priv != NULL);
1231 /* Select appropriate TX function. */
1232 if (vlan_insert || tso || swp)
1233 return tx_pkt_burst;
1234 if (config->mps == MLX5_MPW_ENHANCED) {
1235 if (mlx5_check_vec_tx_support(dev) > 0) {
1236 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1237 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1239 tx_pkt_burst = mlx5_tx_burst_vec;
1241 "port %u selected enhanced MPW Tx vectorized"
1243 dev->data->port_id);
1245 tx_pkt_burst = mlx5_tx_burst_empw;
1247 "port %u selected enhanced MPW Tx function",
1248 dev->data->port_id);
1250 } else if (config->mps && (config->txq_inline > 0)) {
1251 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1252 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1253 dev->data->port_id);
1254 } else if (config->mps) {
1255 tx_pkt_burst = mlx5_tx_burst_mpw;
1256 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1257 dev->data->port_id);
1259 return tx_pkt_burst;
1263 * Configure the RX function to use.
1266 * Pointer to private data structure.
1269 * Pointer to selected Rx burst function.
1272 mlx5_select_rx_function(struct rte_eth_dev *dev)
1274 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1276 assert(dev != NULL);
1277 if (mlx5_check_vec_rx_support(dev) > 0) {
1278 rx_pkt_burst = mlx5_rx_burst_vec;
1279 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1280 dev->data->port_id);
1281 } else if (mlx5_mprq_enabled(dev)) {
1282 rx_pkt_burst = mlx5_rx_burst_mprq;
1284 return rx_pkt_burst;
1288 * Check if mlx5 device was removed.
1291 * Pointer to Ethernet device structure.
1294 * 1 when device is removed, otherwise 0.
1297 mlx5_is_removed(struct rte_eth_dev *dev)
1299 struct ibv_device_attr device_attr;
1300 struct mlx5_priv *priv = dev->data->dev_private;
1302 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1308 * Get port ID list of mlx5 instances sharing a common device.
1311 * Device to look for.
1312 * @param[out] port_list
1313 * Result buffer for collected port IDs.
1314 * @param port_list_n
1315 * Maximum number of entries in result buffer. If 0, @p port_list can be
1319 * Number of matching instances regardless of the @p port_list_n
1320 * parameter, 0 if none were found.
1323 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1324 unsigned int port_list_n)
1329 RTE_ETH_FOREACH_DEV(id) {
1330 struct rte_eth_dev *ldev = &rte_eth_devices[id];
1332 if (ldev->device != dev)
1334 if (n < port_list_n)
1342 * Get switch information associated with network interface.
1345 * Network interface index.
1347 * Switch information object, populated in case of success.
1350 * 0 on success, a negative errno value otherwise and rte_errno is set.
1353 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1355 char ifname[IF_NAMESIZE];
1356 char port_name[IF_NAMESIZE];
1358 struct mlx5_switch_info data = {
1365 bool port_name_set = false;
1366 bool port_switch_id_set = false;
1370 if (!if_indextoname(ifindex, ifname)) {
1375 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1377 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1380 file = fopen(phys_port_name, "rb");
1382 ret = fscanf(file, "%s", port_name);
1385 port_name_set = mlx5_translate_port_name(port_name,
1388 file = fopen(phys_switch_id, "rb");
1393 port_switch_id_set =
1394 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1397 data.master = port_switch_id_set && !port_name_set;
1398 data.representor = port_switch_id_set && port_name_set;
1404 * Extract port name, as a number, from sysfs or netlink information.
1406 * @param[in] port_name_in
1407 * String representing the port name.
1408 * @param[out] port_info_out
1409 * Port information, including port name as a number.
1412 * true on success, false otherwise.
1415 mlx5_translate_port_name(const char *port_name_in,
1416 struct mlx5_switch_info *port_info_out)
1418 char pf_c1, pf_c2, vf_c1, vf_c2;
1421 bool port_name_set = false;
1424 * Check for port-name as a string of the form pf0vf0
1425 * (support kernel ver >= 5.0)
1427 port_name_set = (sscanf(port_name_in, "%c%c%d%c%c%d", &pf_c1, &pf_c2,
1428 &pf_num, &vf_c1, &vf_c2,
1429 &port_info_out->port_name) == 6);
1430 if (port_name_set) {
1431 port_info_out->port_name_new = 1;
1433 /* Check for port-name as a number (support kernel ver < 5.0 */
1435 port_info_out->port_name = strtol(port_name_in, &end, 0);
1437 (size_t)(end - port_name_in) == strlen(port_name_in))
1438 port_name_set = true;
1440 return port_name_set;