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, 0);
632 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
633 dev->data->port_id, strerror(rte_errno));
636 dev_link = (struct rte_eth_link) {
637 .link_status = ((ifr.ifr_flags & IFF_UP) &&
638 (ifr.ifr_flags & IFF_RUNNING)),
640 ifr = (struct ifreq) {
641 .ifr_data = (void *)&edata,
643 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
646 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
647 dev->data->port_id, strerror(rte_errno));
650 link_speed = ethtool_cmd_speed(&edata);
651 if (link_speed == -1)
652 dev_link.link_speed = ETH_SPEED_NUM_NONE;
654 dev_link.link_speed = link_speed;
655 priv->link_speed_capa = 0;
656 if (edata.supported & SUPPORTED_Autoneg)
657 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
658 if (edata.supported & (SUPPORTED_1000baseT_Full |
659 SUPPORTED_1000baseKX_Full))
660 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
661 if (edata.supported & SUPPORTED_10000baseKR_Full)
662 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
663 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
664 SUPPORTED_40000baseCR4_Full |
665 SUPPORTED_40000baseSR4_Full |
666 SUPPORTED_40000baseLR4_Full))
667 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
668 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
669 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
670 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
671 ETH_LINK_SPEED_FIXED);
672 if (!priv->representor &&
673 ((dev_link.link_speed && !dev_link.link_status) ||
674 (!dev_link.link_speed && dev_link.link_status))) {
683 * Retrieve physical link information (unlocked version using new ioctl).
686 * Pointer to Ethernet device structure.
688 * Storage for current link status.
691 * 0 on success, a negative errno value otherwise and rte_errno is set.
694 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
695 struct rte_eth_link *link)
698 struct priv *priv = dev->data->dev_private;
699 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
701 struct rte_eth_link dev_link;
705 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr, 0);
707 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
708 dev->data->port_id, strerror(rte_errno));
711 dev_link = (struct rte_eth_link) {
712 .link_status = ((ifr.ifr_flags & IFF_UP) &&
713 (ifr.ifr_flags & IFF_RUNNING)),
715 ifr = (struct ifreq) {
716 .ifr_data = (void *)&gcmd,
718 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
721 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
723 dev->data->port_id, strerror(rte_errno));
726 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
728 alignas(struct ethtool_link_settings)
729 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
730 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
731 struct ethtool_link_settings *ecmd = (void *)data;
734 ifr.ifr_data = (void *)ecmd;
735 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
738 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
740 dev->data->port_id, strerror(rte_errno));
743 dev_link.link_speed = ecmd->speed;
744 sc = ecmd->link_mode_masks[0] |
745 ((uint64_t)ecmd->link_mode_masks[1] << 32);
746 priv->link_speed_capa = 0;
747 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
748 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
749 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
750 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
751 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
752 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
753 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
754 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
755 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
756 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
757 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
758 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
759 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
760 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
761 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
762 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
763 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
764 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
765 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
766 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
767 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
768 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
769 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
770 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
771 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
772 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
773 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
774 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
775 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
776 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
777 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
778 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
779 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
780 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
781 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
782 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
783 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
784 ETH_LINK_SPEED_FIXED);
785 if (!priv->representor &&
786 ((dev_link.link_speed && !dev_link.link_status) ||
787 (!dev_link.link_speed && dev_link.link_status))) {
796 * DPDK callback to retrieve physical link information.
799 * Pointer to Ethernet device structure.
800 * @param wait_to_complete
801 * Wait for request completion.
804 * 0 if link status was not updated, positive if it was, a negative errno
805 * value otherwise and rte_errno is set.
808 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
811 struct rte_eth_link dev_link;
812 time_t start_time = time(NULL);
815 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
817 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
820 /* Handle wait to complete situation. */
821 if (wait_to_complete && ret == -EAGAIN) {
822 if (abs((int)difftime(time(NULL), start_time)) <
823 MLX5_LINK_STATUS_TIMEOUT) {
830 } else if (ret < 0) {
833 } while (wait_to_complete);
834 ret = !!memcmp(&dev->data->dev_link, &dev_link,
835 sizeof(struct rte_eth_link));
836 dev->data->dev_link = dev_link;
841 * DPDK callback to change the MTU.
844 * Pointer to Ethernet device structure.
849 * 0 on success, a negative errno value otherwise and rte_errno is set.
852 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
854 struct priv *priv = dev->data->dev_private;
855 uint16_t kern_mtu = 0;
858 ret = mlx5_get_mtu(dev, &kern_mtu);
861 /* Set kernel interface MTU first. */
862 ret = mlx5_set_mtu(dev, mtu);
865 ret = mlx5_get_mtu(dev, &kern_mtu);
868 if (kern_mtu == mtu) {
870 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
871 dev->data->port_id, mtu);
879 * DPDK callback to get flow control status.
882 * Pointer to Ethernet device structure.
883 * @param[out] fc_conf
884 * Flow control output buffer.
887 * 0 on success, a negative errno value otherwise and rte_errno is set.
890 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
893 struct ethtool_pauseparam ethpause = {
894 .cmd = ETHTOOL_GPAUSEPARAM
898 ifr.ifr_data = (void *)ðpause;
899 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 1);
902 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
904 dev->data->port_id, strerror(rte_errno));
907 fc_conf->autoneg = ethpause.autoneg;
908 if (ethpause.rx_pause && ethpause.tx_pause)
909 fc_conf->mode = RTE_FC_FULL;
910 else if (ethpause.rx_pause)
911 fc_conf->mode = RTE_FC_RX_PAUSE;
912 else if (ethpause.tx_pause)
913 fc_conf->mode = RTE_FC_TX_PAUSE;
915 fc_conf->mode = RTE_FC_NONE;
920 * DPDK callback to modify flow control parameters.
923 * Pointer to Ethernet device structure.
925 * Flow control parameters.
928 * 0 on success, a negative errno value otherwise and rte_errno is set.
931 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
934 struct ethtool_pauseparam ethpause = {
935 .cmd = ETHTOOL_SPAUSEPARAM
939 ifr.ifr_data = (void *)ðpause;
940 ethpause.autoneg = fc_conf->autoneg;
941 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
942 (fc_conf->mode & RTE_FC_RX_PAUSE))
943 ethpause.rx_pause = 1;
945 ethpause.rx_pause = 0;
947 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
948 (fc_conf->mode & RTE_FC_TX_PAUSE))
949 ethpause.tx_pause = 1;
951 ethpause.tx_pause = 0;
952 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
955 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
957 dev->data->port_id, strerror(rte_errno));
964 * Get PCI information from struct ibv_device.
967 * Pointer to Ethernet device structure.
968 * @param[out] pci_addr
969 * PCI bus address output buffer.
972 * 0 on success, a negative errno value otherwise and rte_errno is set.
975 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
976 struct rte_pci_addr *pci_addr)
980 MKSTR(path, "%s/device/uevent", device->ibdev_path);
982 file = fopen(path, "rb");
987 while (fgets(line, sizeof(line), file) == line) {
988 size_t len = strlen(line);
991 /* Truncate long lines. */
992 if (len == (sizeof(line) - 1))
993 while (line[(len - 1)] != '\n') {
997 line[(len - 1)] = ret;
999 /* Extract information. */
1002 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1006 &pci_addr->function) == 4) {
1016 * Device status handler.
1019 * Pointer to Ethernet device.
1021 * Pointer to event flags holder.
1024 * Events bitmap of callback process which can be called immediately.
1027 mlx5_dev_status_handler(struct rte_eth_dev *dev)
1029 struct priv *priv = dev->data->dev_private;
1030 struct ibv_async_event event;
1033 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1037 /* Read all message and acknowledge them. */
1039 if (mlx5_glue->get_async_event(priv->ctx, &event))
1041 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1042 event.event_type == IBV_EVENT_PORT_ERR) &&
1043 (dev->data->dev_conf.intr_conf.lsc == 1))
1044 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1045 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1046 dev->data->dev_conf.intr_conf.rmv == 1)
1047 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1050 "port %u event type %d on not handled",
1051 dev->data->port_id, event.event_type);
1052 mlx5_glue->ack_async_event(&event);
1058 * Handle interrupts from the NIC.
1060 * @param[in] intr_handle
1061 * Interrupt handler.
1063 * Callback argument.
1066 mlx5_dev_interrupt_handler(void *cb_arg)
1068 struct rte_eth_dev *dev = cb_arg;
1071 events = mlx5_dev_status_handler(dev);
1072 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1073 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1074 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1075 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1079 * Handle interrupts from the socket.
1082 * Callback argument.
1085 mlx5_dev_handler_socket(void *cb_arg)
1087 struct rte_eth_dev *dev = cb_arg;
1089 mlx5_socket_handle(dev);
1093 * Uninstall interrupt handler.
1096 * Pointer to Ethernet device.
1099 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1101 struct priv *priv = dev->data->dev_private;
1103 if (dev->data->dev_conf.intr_conf.lsc ||
1104 dev->data->dev_conf.intr_conf.rmv)
1105 rte_intr_callback_unregister(&priv->intr_handle,
1106 mlx5_dev_interrupt_handler, dev);
1107 if (priv->primary_socket)
1108 rte_intr_callback_unregister(&priv->intr_handle_socket,
1109 mlx5_dev_handler_socket, dev);
1110 priv->intr_handle.fd = 0;
1111 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1112 priv->intr_handle_socket.fd = 0;
1113 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1117 * Install interrupt handler.
1120 * Pointer to Ethernet device.
1123 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1125 struct priv *priv = dev->data->dev_private;
1129 assert(priv->ctx->async_fd > 0);
1130 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1131 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1134 "port %u failed to change file descriptor async event"
1136 dev->data->port_id);
1137 dev->data->dev_conf.intr_conf.lsc = 0;
1138 dev->data->dev_conf.intr_conf.rmv = 0;
1140 if (dev->data->dev_conf.intr_conf.lsc ||
1141 dev->data->dev_conf.intr_conf.rmv) {
1142 priv->intr_handle.fd = priv->ctx->async_fd;
1143 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1144 rte_intr_callback_register(&priv->intr_handle,
1145 mlx5_dev_interrupt_handler, dev);
1147 ret = mlx5_socket_init(dev);
1149 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1150 dev->data->port_id, strerror(rte_errno));
1151 else if (priv->primary_socket) {
1152 priv->intr_handle_socket.fd = priv->primary_socket;
1153 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1154 rte_intr_callback_register(&priv->intr_handle_socket,
1155 mlx5_dev_handler_socket, dev);
1160 * DPDK callback to bring the link DOWN.
1163 * Pointer to Ethernet device structure.
1166 * 0 on success, a negative errno value otherwise and rte_errno is set.
1169 mlx5_set_link_down(struct rte_eth_dev *dev)
1171 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1175 * DPDK callback to bring the link UP.
1178 * Pointer to Ethernet device structure.
1181 * 0 on success, a negative errno value otherwise and rte_errno is set.
1184 mlx5_set_link_up(struct rte_eth_dev *dev)
1186 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1190 * Configure the TX function to use.
1193 * Pointer to private data structure.
1196 * Pointer to selected Tx burst function.
1199 mlx5_select_tx_function(struct rte_eth_dev *dev)
1201 struct priv *priv = dev->data->dev_private;
1202 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1203 struct mlx5_dev_config *config = &priv->config;
1204 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1205 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1206 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1207 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1208 DEV_TX_OFFLOAD_IP_TNL_TSO |
1209 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1210 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1211 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1212 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1213 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1215 assert(priv != NULL);
1216 /* Select appropriate TX function. */
1217 if (vlan_insert || tso || swp)
1218 return tx_pkt_burst;
1219 if (config->mps == MLX5_MPW_ENHANCED) {
1220 if (mlx5_check_vec_tx_support(dev) > 0) {
1221 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1222 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1224 tx_pkt_burst = mlx5_tx_burst_vec;
1226 "port %u selected enhanced MPW Tx vectorized"
1228 dev->data->port_id);
1230 tx_pkt_burst = mlx5_tx_burst_empw;
1232 "port %u selected enhanced MPW Tx function",
1233 dev->data->port_id);
1235 } else if (config->mps && (config->txq_inline > 0)) {
1236 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1237 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1238 dev->data->port_id);
1239 } else if (config->mps) {
1240 tx_pkt_burst = mlx5_tx_burst_mpw;
1241 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1242 dev->data->port_id);
1244 return tx_pkt_burst;
1248 * Configure the RX function to use.
1251 * Pointer to private data structure.
1254 * Pointer to selected Rx burst function.
1257 mlx5_select_rx_function(struct rte_eth_dev *dev)
1259 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1261 assert(dev != NULL);
1262 if (mlx5_check_vec_rx_support(dev) > 0) {
1263 rx_pkt_burst = mlx5_rx_burst_vec;
1264 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1265 dev->data->port_id);
1266 } else if (mlx5_mprq_enabled(dev)) {
1267 rx_pkt_burst = mlx5_rx_burst_mprq;
1269 return rx_pkt_burst;
1273 * Check if mlx5 device was removed.
1276 * Pointer to Ethernet device structure.
1279 * 1 when device is removed, otherwise 0.
1282 mlx5_is_removed(struct rte_eth_dev *dev)
1284 struct ibv_device_attr device_attr;
1285 struct priv *priv = dev->data->dev_private;
1287 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1293 * Get port ID list of mlx5 instances sharing a common device.
1296 * Device to look for.
1297 * @param[out] port_list
1298 * Result buffer for collected port IDs.
1299 * @param port_list_n
1300 * Maximum number of entries in result buffer. If 0, @p port_list can be
1304 * Number of matching instances regardless of the @p port_list_n
1305 * parameter, 0 if none were found.
1308 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1309 unsigned int port_list_n)
1314 RTE_ETH_FOREACH_DEV(id) {
1315 struct rte_eth_dev *ldev = &rte_eth_devices[id];
1317 if (!ldev->device ||
1318 !ldev->device->driver ||
1319 strcmp(ldev->device->driver->name, MLX5_DRIVER_NAME) ||
1320 ldev->device != dev)
1322 if (n < port_list_n)
1330 * Get switch information associated with network interface.
1333 * Network interface index.
1335 * Switch information object, populated in case of success.
1338 * 0 on success, a negative errno value otherwise and rte_errno is set.
1341 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1343 char ifname[IF_NAMESIZE];
1345 struct mlx5_switch_info data = { .master = 0, };
1346 bool port_name_set = false;
1347 bool port_switch_id_set = false;
1350 if (!if_indextoname(ifindex, ifname)) {
1355 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1357 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1360 file = fopen(phys_port_name, "rb");
1363 fscanf(file, "%d%c", &data.port_name, &c) == 2 &&
1367 file = fopen(phys_switch_id, "rb");
1372 port_switch_id_set =
1373 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1376 data.master = port_switch_id_set && !port_name_set;
1377 data.representor = port_switch_id_set && port_name_set;