1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_malloc.h>
35 #include <rte_string_fns.h>
36 #include <rte_rwlock.h>
37 #include <rte_cycles.h>
40 #include "mlx5_glue.h"
41 #include "mlx5_devx_cmds.h"
42 #include "mlx5_rxtx.h"
43 #include "mlx5_utils.h"
45 /* Supported speed values found in /usr/include/linux/ethtool.h */
46 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
47 #define SUPPORTED_40000baseKR4_Full (1 << 23)
49 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
50 #define SUPPORTED_40000baseCR4_Full (1 << 24)
52 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
53 #define SUPPORTED_40000baseSR4_Full (1 << 25)
55 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
56 #define SUPPORTED_40000baseLR4_Full (1 << 26)
58 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
59 #define SUPPORTED_56000baseKR4_Full (1 << 27)
61 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
62 #define SUPPORTED_56000baseCR4_Full (1 << 28)
64 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
65 #define SUPPORTED_56000baseSR4_Full (1 << 29)
67 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
68 #define SUPPORTED_56000baseLR4_Full (1 << 30)
71 /* Add defines in case the running kernel is not the same as user headers. */
72 #ifndef ETHTOOL_GLINKSETTINGS
73 struct ethtool_link_settings {
82 uint8_t eth_tp_mdix_ctrl;
83 int8_t link_mode_masks_nwords;
85 uint32_t link_mode_masks[];
88 #define ETHTOOL_GLINKSETTINGS 0x0000004c
89 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
90 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
91 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
92 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
93 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
94 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
95 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
96 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
97 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
98 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
99 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
100 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
101 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
102 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
103 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
104 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
106 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
107 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
108 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
109 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
111 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
112 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
113 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
115 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
116 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
117 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
118 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
119 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
123 * Get master interface name from private structure.
126 * Pointer to Ethernet device.
128 * Interface name output buffer.
131 * 0 on success, a negative errno value otherwise and rte_errno is set.
134 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
138 unsigned int dev_type = 0;
139 unsigned int dev_port_prev = ~0u;
140 char match[IF_NAMESIZE] = "";
144 MKSTR(path, "%s/device/net", 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",
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 mlx5_priv *priv = dev->data->dev_private;
225 unsigned int ifindex;
229 ifindex = mlx5_ifindex(dev);
231 if (!priv->representor)
232 return mlx5_get_master_ifname(priv->sh->ibdev_path,
237 if (if_indextoname(ifindex, &(*ifname)[0]))
244 * Get the interface index from device name.
247 * Pointer to Ethernet device.
250 * Nonzero interface index on success, zero otherwise and rte_errno is set.
253 mlx5_ifindex(const struct rte_eth_dev *dev)
255 struct mlx5_priv *priv = dev->data->dev_private;
256 unsigned int ifindex;
259 assert(priv->if_index);
260 ifindex = priv->if_index;
267 * Perform ifreq ioctl() on associated Ethernet device.
270 * Pointer to Ethernet device.
272 * Request number to pass to ioctl().
274 * Interface request structure output buffer.
277 * 0 on success, a negative errno value otherwise and rte_errno is set.
280 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
282 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
289 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
292 ret = ioctl(sock, req, ifr);
308 * Pointer to Ethernet device.
310 * MTU value output buffer.
313 * 0 on success, a negative errno value otherwise and rte_errno is set.
316 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
318 struct ifreq request;
319 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
323 *mtu = request.ifr_mtu;
331 * Pointer to Ethernet device.
336 * 0 on success, a negative errno value otherwise and rte_errno is set.
339 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
341 struct ifreq request = { .ifr_mtu = mtu, };
343 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
350 * Pointer to Ethernet device.
352 * Bitmask for flags that must remain untouched.
354 * Bitmask for flags to modify.
357 * 0 on success, a negative errno value otherwise and rte_errno is set.
360 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
362 struct ifreq request;
363 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
367 request.ifr_flags &= keep;
368 request.ifr_flags |= flags & ~keep;
369 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
373 * DPDK callback for Ethernet device configuration.
376 * Pointer to Ethernet device structure.
379 * 0 on success, a negative errno value otherwise and rte_errno is set.
382 mlx5_dev_configure(struct rte_eth_dev *dev)
384 struct mlx5_priv *priv = dev->data->dev_private;
385 unsigned int rxqs_n = dev->data->nb_rx_queues;
386 unsigned int txqs_n = dev->data->nb_tx_queues;
387 const uint8_t use_app_rss_key =
388 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
391 if (use_app_rss_key &&
392 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
393 MLX5_RSS_HASH_KEY_LEN)) {
394 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
395 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
399 priv->rss_conf.rss_key =
400 rte_realloc(priv->rss_conf.rss_key,
401 MLX5_RSS_HASH_KEY_LEN, 0);
402 if (!priv->rss_conf.rss_key) {
403 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
404 dev->data->port_id, rxqs_n);
409 if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
410 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
412 memcpy(priv->rss_conf.rss_key,
414 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
415 rss_hash_default_key,
416 MLX5_RSS_HASH_KEY_LEN);
417 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
418 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
419 priv->rxqs = (void *)dev->data->rx_queues;
420 priv->txqs = (void *)dev->data->tx_queues;
421 if (txqs_n != priv->txqs_n) {
422 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
423 dev->data->port_id, priv->txqs_n, txqs_n);
424 priv->txqs_n = txqs_n;
426 if (rxqs_n > priv->config.ind_table_max_size) {
427 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
428 dev->data->port_id, rxqs_n);
432 if (rxqs_n != priv->rxqs_n) {
433 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
434 dev->data->port_id, priv->rxqs_n, rxqs_n);
435 priv->rxqs_n = rxqs_n;
437 priv->skip_default_rss_reta = 0;
438 ret = mlx5_proc_priv_init(dev);
445 * Configure default RSS reta.
448 * Pointer to Ethernet device structure.
451 * 0 on success, a negative errno value otherwise and rte_errno is set.
454 mlx5_dev_configure_rss_reta(struct rte_eth_dev *dev)
456 struct mlx5_priv *priv = dev->data->dev_private;
457 unsigned int rxqs_n = dev->data->nb_rx_queues;
460 unsigned int reta_idx_n;
462 unsigned int *rss_queue_arr = NULL;
463 unsigned int rss_queue_n = 0;
465 if (priv->skip_default_rss_reta)
467 rss_queue_arr = rte_malloc("", rxqs_n * sizeof(unsigned int), 0);
468 if (!rss_queue_arr) {
469 DRV_LOG(ERR, "port %u cannot allocate RSS queue list (%u)",
470 dev->data->port_id, rxqs_n);
474 for (i = 0, j = 0; i < rxqs_n; i++) {
475 struct mlx5_rxq_data *rxq_data;
476 struct mlx5_rxq_ctrl *rxq_ctrl;
478 rxq_data = (*priv->rxqs)[i];
479 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
480 if (rxq_ctrl && rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
481 rss_queue_arr[j++] = i;
484 if (rss_queue_n > priv->config.ind_table_max_size) {
485 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
486 dev->data->port_id, rss_queue_n);
488 rte_free(rss_queue_arr);
491 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
492 dev->data->port_id, priv->rxqs_n, rxqs_n);
493 priv->rxqs_n = rxqs_n;
495 * If the requested number of RX queues is not a power of two,
496 * use the maximum indirection table size for better balancing.
497 * The result is always rounded to the next power of two.
499 reta_idx_n = (1 << log2above((rss_queue_n & (rss_queue_n - 1)) ?
500 priv->config.ind_table_max_size :
502 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
504 rte_free(rss_queue_arr);
508 * When the number of RX queues is not a power of two,
509 * the remaining table entries are padded with reused WQs
510 * and hashes are not spread uniformly.
512 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
513 (*priv->reta_idx)[i] = rss_queue_arr[j];
514 if (++j == rss_queue_n)
517 rte_free(rss_queue_arr);
522 * Sets default tuning parameters.
525 * Pointer to Ethernet device.
527 * Info structure output buffer.
530 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
532 struct mlx5_priv *priv = dev->data->dev_private;
534 /* Minimum CPU utilization. */
535 info->default_rxportconf.ring_size = 256;
536 info->default_txportconf.ring_size = 256;
537 info->default_rxportconf.burst_size = MLX5_RX_DEFAULT_BURST;
538 info->default_txportconf.burst_size = MLX5_TX_DEFAULT_BURST;
539 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
540 info->default_rxportconf.nb_queues = 16;
541 info->default_txportconf.nb_queues = 16;
542 if (dev->data->nb_rx_queues > 2 ||
543 dev->data->nb_tx_queues > 2) {
544 /* Max Throughput. */
545 info->default_rxportconf.ring_size = 2048;
546 info->default_txportconf.ring_size = 2048;
549 info->default_rxportconf.nb_queues = 8;
550 info->default_txportconf.nb_queues = 8;
551 if (dev->data->nb_rx_queues > 2 ||
552 dev->data->nb_tx_queues > 2) {
553 /* Max Throughput. */
554 info->default_rxportconf.ring_size = 4096;
555 info->default_txportconf.ring_size = 4096;
561 * Sets tx mbuf limiting parameters.
564 * Pointer to Ethernet device.
566 * Info structure output buffer.
569 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
571 struct mlx5_priv *priv = dev->data->dev_private;
572 struct mlx5_dev_config *config = &priv->config;
576 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
577 MLX5_SEND_DEF_INLINE_LEN :
578 (unsigned int)config->txq_inline_max;
579 assert(config->txq_inline_min >= 0);
580 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
581 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
582 MLX5_ESEG_MIN_INLINE_SIZE -
585 MLX5_WQE_DSEG_SIZE * 2);
586 nb_max = (MLX5_WQE_SIZE_MAX +
587 MLX5_ESEG_MIN_INLINE_SIZE -
591 inlen) / MLX5_WSEG_SIZE;
592 info->tx_desc_lim.nb_seg_max = nb_max;
593 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
597 * DPDK callback to get information about the device.
600 * Pointer to Ethernet device structure.
602 * Info structure output buffer.
605 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
607 struct mlx5_priv *priv = dev->data->dev_private;
608 struct mlx5_dev_config *config = &priv->config;
611 /* FIXME: we should ask the device for these values. */
612 info->min_rx_bufsize = 32;
613 info->max_rx_pktlen = 65536;
614 info->max_lro_pkt_size = MLX5_MAX_LRO_SIZE;
616 * Since we need one CQ per QP, the limit is the minimum number
617 * between the two values.
619 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
620 priv->sh->device_attr.orig_attr.max_qp);
621 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
624 info->max_rx_queues = max;
625 info->max_tx_queues = max;
626 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
627 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
628 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
629 info->rx_queue_offload_capa);
630 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
631 info->if_index = mlx5_ifindex(dev);
632 info->reta_size = priv->reta_idx_n ?
633 priv->reta_idx_n : config->ind_table_max_size;
634 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
635 info->speed_capa = priv->link_speed_capa;
636 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
637 mlx5_set_default_params(dev, info);
638 mlx5_set_txlimit_params(dev, info);
639 info->switch_info.name = dev->data->name;
640 info->switch_info.domain_id = priv->domain_id;
641 info->switch_info.port_id = priv->representor_id;
642 if (priv->representor) {
645 if (priv->pf_bond >= 0) {
647 * Switch port ID is opaque value with driver defined
648 * format. Push the PF index in bonding configurations
649 * in upper four bits of port ID. If we get too many
650 * representors (more than 4K) or PFs (more than 15)
651 * this approach must be reconsidered.
653 if ((info->switch_info.port_id >>
654 MLX5_PORT_ID_BONDING_PF_SHIFT) ||
655 priv->pf_bond > MLX5_PORT_ID_BONDING_PF_MASK) {
656 DRV_LOG(ERR, "can't update switch port ID"
657 " for bonding device");
661 info->switch_info.port_id |=
662 priv->pf_bond << MLX5_PORT_ID_BONDING_PF_SHIFT;
664 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
665 struct mlx5_priv *opriv =
666 rte_eth_devices[port_id].data->dev_private;
669 opriv->representor ||
670 opriv->sh != priv->sh ||
671 opriv->domain_id != priv->domain_id)
674 * Override switch name with that of the master
677 info->switch_info.name = opriv->dev_data->name;
685 * Get device current raw clock counter
688 * Pointer to Ethernet device structure.
690 * Current raw clock counter of the device.
693 * 0 if the clock has correctly been read
694 * The value of errno in case of error
697 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
699 struct mlx5_priv *priv = dev->data->dev_private;
700 struct ibv_context *ctx = priv->sh->ctx;
701 struct ibv_values_ex values;
704 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
705 err = mlx5_glue->query_rt_values_ex(ctx, &values);
707 DRV_LOG(WARNING, "Could not query the clock !");
710 *clock = values.raw_clock.tv_nsec;
715 * Get firmware version of a device.
718 * Ethernet device port.
720 * String output allocated by caller.
722 * Size of the output string, including terminating null byte.
725 * 0 on success, or the size of the non truncated string if too big.
727 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
729 struct mlx5_priv *priv = dev->data->dev_private;
730 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
731 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
736 strlcpy(fw_ver, attr->fw_ver, fw_size);
741 * Get supported packet types.
744 * Pointer to Ethernet device structure.
747 * A pointer to the supported Packet types array.
750 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
752 static const uint32_t ptypes[] = {
753 /* refers to rxq_cq_to_pkt_type() */
755 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
756 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
757 RTE_PTYPE_L4_NONFRAG,
761 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
762 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
763 RTE_PTYPE_INNER_L4_NONFRAG,
764 RTE_PTYPE_INNER_L4_FRAG,
765 RTE_PTYPE_INNER_L4_TCP,
766 RTE_PTYPE_INNER_L4_UDP,
770 if (dev->rx_pkt_burst == mlx5_rx_burst ||
771 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
772 dev->rx_pkt_burst == mlx5_rx_burst_vec)
778 * Retrieve the master device for representor in the same switch domain.
781 * Pointer to representor Ethernet device structure.
784 * Master device structure on success, NULL otherwise.
787 static struct rte_eth_dev *
788 mlx5_find_master_dev(struct rte_eth_dev *dev)
790 struct mlx5_priv *priv;
794 priv = dev->data->dev_private;
795 domain_id = priv->domain_id;
796 assert(priv->representor);
797 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
798 struct mlx5_priv *opriv =
799 rte_eth_devices[port_id].data->dev_private;
802 opriv->domain_id == domain_id &&
803 opriv->sh == priv->sh)
804 return &rte_eth_devices[port_id];
810 * DPDK callback to retrieve physical link information.
813 * Pointer to Ethernet device structure.
815 * Storage for current link status.
818 * 0 on success, a negative errno value otherwise and rte_errno is set.
821 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
822 struct rte_eth_link *link)
824 struct mlx5_priv *priv = dev->data->dev_private;
825 struct ethtool_cmd edata = {
826 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
829 struct rte_eth_link dev_link;
833 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
835 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
836 dev->data->port_id, strerror(rte_errno));
839 dev_link = (struct rte_eth_link) {
840 .link_status = ((ifr.ifr_flags & IFF_UP) &&
841 (ifr.ifr_flags & IFF_RUNNING)),
843 ifr = (struct ifreq) {
844 .ifr_data = (void *)&edata,
846 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
848 if (ret == -ENOTSUP && priv->representor) {
849 struct rte_eth_dev *master;
852 * For representors we can try to inherit link
853 * settings from the master device. Actually
854 * link settings do not make a lot of sense
855 * for representors due to missing physical
856 * link. The old kernel drivers supported
857 * emulated settings query for representors,
858 * the new ones do not, so we have to add
859 * this code for compatibility issues.
861 master = mlx5_find_master_dev(dev);
863 ifr = (struct ifreq) {
864 .ifr_data = (void *)&edata,
866 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
871 "port %u ioctl(SIOCETHTOOL,"
872 " ETHTOOL_GSET) failed: %s",
873 dev->data->port_id, strerror(rte_errno));
877 link_speed = ethtool_cmd_speed(&edata);
878 if (link_speed == -1)
879 dev_link.link_speed = ETH_SPEED_NUM_NONE;
881 dev_link.link_speed = link_speed;
882 priv->link_speed_capa = 0;
883 if (edata.supported & SUPPORTED_Autoneg)
884 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
885 if (edata.supported & (SUPPORTED_1000baseT_Full |
886 SUPPORTED_1000baseKX_Full))
887 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
888 if (edata.supported & SUPPORTED_10000baseKR_Full)
889 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
890 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
891 SUPPORTED_40000baseCR4_Full |
892 SUPPORTED_40000baseSR4_Full |
893 SUPPORTED_40000baseLR4_Full))
894 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
895 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
896 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
897 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
898 ETH_LINK_SPEED_FIXED);
899 if (((dev_link.link_speed && !dev_link.link_status) ||
900 (!dev_link.link_speed && dev_link.link_status))) {
909 * Retrieve physical link information (unlocked version using new ioctl).
912 * Pointer to Ethernet device structure.
914 * Storage for current link status.
917 * 0 on success, a negative errno value otherwise and rte_errno is set.
920 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
921 struct rte_eth_link *link)
924 struct mlx5_priv *priv = dev->data->dev_private;
925 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
927 struct rte_eth_link dev_link;
928 struct rte_eth_dev *master = NULL;
932 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
934 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
935 dev->data->port_id, strerror(rte_errno));
938 dev_link = (struct rte_eth_link) {
939 .link_status = ((ifr.ifr_flags & IFF_UP) &&
940 (ifr.ifr_flags & IFF_RUNNING)),
942 ifr = (struct ifreq) {
943 .ifr_data = (void *)&gcmd,
945 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
947 if (ret == -ENOTSUP && priv->representor) {
949 * For representors we can try to inherit link
950 * settings from the master device. Actually
951 * link settings do not make a lot of sense
952 * for representors due to missing physical
953 * link. The old kernel drivers supported
954 * emulated settings query for representors,
955 * the new ones do not, so we have to add
956 * this code for compatibility issues.
958 master = mlx5_find_master_dev(dev);
960 ifr = (struct ifreq) {
961 .ifr_data = (void *)&gcmd,
963 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
968 "port %u ioctl(SIOCETHTOOL,"
969 " ETHTOOL_GLINKSETTINGS) failed: %s",
970 dev->data->port_id, strerror(rte_errno));
975 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
977 alignas(struct ethtool_link_settings)
978 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
979 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
980 struct ethtool_link_settings *ecmd = (void *)data;
983 ifr.ifr_data = (void *)ecmd;
984 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
987 "port %u ioctl(SIOCETHTOOL,"
988 "ETHTOOL_GLINKSETTINGS) failed: %s",
989 dev->data->port_id, strerror(rte_errno));
992 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
994 sc = ecmd->link_mode_masks[0] |
995 ((uint64_t)ecmd->link_mode_masks[1] << 32);
996 priv->link_speed_capa = 0;
997 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
998 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
999 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
1000 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
1001 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
1002 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
1003 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
1004 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
1005 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
1006 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
1007 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
1008 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
1009 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
1010 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1011 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1012 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1013 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1014 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1015 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1016 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1017 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1018 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1019 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1020 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1021 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1022 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1023 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1024 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1025 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1026 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1027 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1028 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1029 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1030 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1031 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1032 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1033 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1034 ETH_LINK_SPEED_FIXED);
1035 if (((dev_link.link_speed && !dev_link.link_status) ||
1036 (!dev_link.link_speed && dev_link.link_status))) {
1045 * DPDK callback to retrieve physical link information.
1048 * Pointer to Ethernet device structure.
1049 * @param wait_to_complete
1050 * Wait for request completion.
1053 * 0 if link status was not updated, positive if it was, a negative errno
1054 * value otherwise and rte_errno is set.
1057 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1060 struct rte_eth_link dev_link;
1061 time_t start_time = time(NULL);
1062 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
1065 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1066 if (ret == -ENOTSUP)
1067 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1070 /* Handle wait to complete situation. */
1071 if ((wait_to_complete || retry) && ret == -EAGAIN) {
1072 if (abs((int)difftime(time(NULL), start_time)) <
1073 MLX5_LINK_STATUS_TIMEOUT) {
1080 } else if (ret < 0) {
1083 } while (wait_to_complete || retry-- > 0);
1084 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1085 sizeof(struct rte_eth_link));
1086 dev->data->dev_link = dev_link;
1091 * DPDK callback to change the MTU.
1094 * Pointer to Ethernet device structure.
1099 * 0 on success, a negative errno value otherwise and rte_errno is set.
1102 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1104 struct mlx5_priv *priv = dev->data->dev_private;
1105 uint16_t kern_mtu = 0;
1108 ret = mlx5_get_mtu(dev, &kern_mtu);
1111 /* Set kernel interface MTU first. */
1112 ret = mlx5_set_mtu(dev, mtu);
1115 ret = mlx5_get_mtu(dev, &kern_mtu);
1118 if (kern_mtu == mtu) {
1120 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1121 dev->data->port_id, mtu);
1129 * DPDK callback to get flow control status.
1132 * Pointer to Ethernet device structure.
1133 * @param[out] fc_conf
1134 * Flow control output buffer.
1137 * 0 on success, a negative errno value otherwise and rte_errno is set.
1140 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1143 struct ethtool_pauseparam ethpause = {
1144 .cmd = ETHTOOL_GPAUSEPARAM
1148 ifr.ifr_data = (void *)ðpause;
1149 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1152 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1154 dev->data->port_id, strerror(rte_errno));
1157 fc_conf->autoneg = ethpause.autoneg;
1158 if (ethpause.rx_pause && ethpause.tx_pause)
1159 fc_conf->mode = RTE_FC_FULL;
1160 else if (ethpause.rx_pause)
1161 fc_conf->mode = RTE_FC_RX_PAUSE;
1162 else if (ethpause.tx_pause)
1163 fc_conf->mode = RTE_FC_TX_PAUSE;
1165 fc_conf->mode = RTE_FC_NONE;
1170 * DPDK callback to modify flow control parameters.
1173 * Pointer to Ethernet device structure.
1174 * @param[in] fc_conf
1175 * Flow control parameters.
1178 * 0 on success, a negative errno value otherwise and rte_errno is set.
1181 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1184 struct ethtool_pauseparam ethpause = {
1185 .cmd = ETHTOOL_SPAUSEPARAM
1189 ifr.ifr_data = (void *)ðpause;
1190 ethpause.autoneg = fc_conf->autoneg;
1191 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1192 (fc_conf->mode & RTE_FC_RX_PAUSE))
1193 ethpause.rx_pause = 1;
1195 ethpause.rx_pause = 0;
1197 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1198 (fc_conf->mode & RTE_FC_TX_PAUSE))
1199 ethpause.tx_pause = 1;
1201 ethpause.tx_pause = 0;
1202 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1205 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1207 dev->data->port_id, strerror(rte_errno));
1214 * Get PCI information by sysfs device path.
1217 * Pointer to device sysfs folder name.
1218 * @param[out] pci_addr
1219 * PCI bus address output buffer.
1222 * 0 on success, a negative errno value otherwise and rte_errno is set.
1225 mlx5_dev_to_pci_addr(const char *dev_path,
1226 struct rte_pci_addr *pci_addr)
1230 MKSTR(path, "%s/device/uevent", dev_path);
1232 file = fopen(path, "rb");
1237 while (fgets(line, sizeof(line), file) == line) {
1238 size_t len = strlen(line);
1241 /* Truncate long lines. */
1242 if (len == (sizeof(line) - 1))
1243 while (line[(len - 1)] != '\n') {
1247 line[(len - 1)] = ret;
1249 /* Extract information. */
1252 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1256 &pci_addr->function) == 4) {
1266 * Handle asynchronous removal event for entire multiport device.
1269 * Infiniband device shared context.
1272 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1276 for (i = 0; i < sh->max_port; ++i) {
1277 struct rte_eth_dev *dev;
1279 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1281 * Or not existing port either no
1282 * handler installed for this port.
1286 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1288 if (dev->data->dev_conf.intr_conf.rmv)
1289 _rte_eth_dev_callback_process
1290 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1295 * Handle shared asynchronous events the NIC (removal event
1296 * and link status change). Supports multiport IB device.
1299 * Callback argument.
1302 mlx5_dev_interrupt_handler(void *cb_arg)
1304 struct mlx5_ibv_shared *sh = cb_arg;
1305 struct ibv_async_event event;
1307 /* Read all message from the IB device and acknowledge them. */
1309 struct rte_eth_dev *dev;
1312 if (mlx5_glue->get_async_event(sh->ctx, &event))
1314 /* Retrieve and check IB port index. */
1315 tmp = (uint32_t)event.element.port_num;
1316 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1318 * The DEVICE_FATAL event is called once for
1319 * entire device without port specifying.
1320 * We should notify all existing ports.
1322 mlx5_glue->ack_async_event(&event);
1323 mlx5_dev_interrupt_device_fatal(sh);
1326 assert(tmp && (tmp <= sh->max_port));
1328 /* Unsupported devive level event. */
1329 mlx5_glue->ack_async_event(&event);
1331 "unsupported common event (type %d)",
1335 if (tmp > sh->max_port) {
1336 /* Invalid IB port index. */
1337 mlx5_glue->ack_async_event(&event);
1339 "cannot handle an event (type %d)"
1340 "due to invalid IB port index (%u)",
1341 event.event_type, tmp);
1344 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1345 /* No handler installed. */
1346 mlx5_glue->ack_async_event(&event);
1348 "cannot handle an event (type %d)"
1349 "due to no handler installed for port %u",
1350 event.event_type, tmp);
1353 /* Retrieve ethernet device descriptor. */
1354 tmp = sh->port[tmp - 1].ih_port_id;
1355 dev = &rte_eth_devices[tmp];
1357 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1358 event.event_type == IBV_EVENT_PORT_ERR) &&
1359 dev->data->dev_conf.intr_conf.lsc) {
1360 mlx5_glue->ack_async_event(&event);
1361 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1365 _rte_eth_dev_callback_process
1366 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1370 "port %u cannot handle an unknown event (type %d)",
1371 dev->data->port_id, event.event_type);
1372 mlx5_glue->ack_async_event(&event);
1377 * Unregister callback handler safely. The handler may be active
1378 * while we are trying to unregister it, in this case code -EAGAIN
1379 * is returned by rte_intr_callback_unregister(). This routine checks
1380 * the return code and tries to unregister handler again.
1385 * pointer to callback routine
1387 * opaque callback parameter
1390 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1391 rte_intr_callback_fn cb_fn, void *cb_arg)
1394 * Try to reduce timeout management overhead by not calling
1395 * the timer related routines on the first iteration. If the
1396 * unregistering succeeds on first call there will be no
1397 * timer calls at all.
1405 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1408 if (ret != -EAGAIN) {
1409 DRV_LOG(INFO, "failed to unregister interrupt"
1410 " handler (error: %d)", ret);
1415 struct timespec onems;
1417 /* Wait one millisecond and try again. */
1419 onems.tv_nsec = NS_PER_S / MS_PER_S;
1420 nanosleep(&onems, 0);
1421 /* Check whether one second elapsed. */
1422 if ((rte_get_timer_cycles() - start) <= twait)
1426 * We get the amount of timer ticks for one second.
1427 * If this amount elapsed it means we spent one
1428 * second in waiting. This branch is executed once
1429 * on first iteration.
1431 twait = rte_get_timer_hz();
1435 * Timeout elapsed, show message (once a second) and retry.
1436 * We have no other acceptable option here, if we ignore
1437 * the unregistering return code the handler will not
1438 * be unregistered, fd will be closed and we may get the
1439 * crush. Hanging and messaging in the loop seems not to be
1442 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1443 start = rte_get_timer_cycles();
1448 * Handle DEVX interrupts from the NIC.
1449 * This function is probably called from the DPDK host thread.
1452 * Callback argument.
1455 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1457 #ifndef HAVE_IBV_DEVX_ASYNC
1461 struct mlx5_ibv_shared *sh = cb_arg;
1463 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1464 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1465 MLX5_ST_SZ_BYTES(traffic_counter) +
1466 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1468 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1470 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1473 mlx5_flow_async_pool_query_handle
1474 (sh, (uint64_t)out.cmd_resp.wr_id,
1475 mlx5_devx_get_out_command_status(buf));
1476 #endif /* HAVE_IBV_DEVX_ASYNC */
1480 * Uninstall shared asynchronous device events handler.
1481 * This function is implemented to support event sharing
1482 * between multiple ports of single IB device.
1485 * Pointer to Ethernet device.
1488 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1490 struct mlx5_priv *priv = dev->data->dev_private;
1491 struct mlx5_ibv_shared *sh = priv->sh;
1493 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1495 pthread_mutex_lock(&sh->intr_mutex);
1496 assert(priv->ibv_port);
1497 assert(priv->ibv_port <= sh->max_port);
1498 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1499 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1501 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1502 (uint32_t)dev->data->port_id);
1503 assert(sh->intr_cnt);
1504 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1505 if (!sh->intr_cnt || --sh->intr_cnt)
1507 mlx5_intr_callback_unregister(&sh->intr_handle,
1508 mlx5_dev_interrupt_handler, sh);
1509 sh->intr_handle.fd = 0;
1510 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1512 pthread_mutex_unlock(&sh->intr_mutex);
1516 * Uninstall devx shared asynchronous device events handler.
1517 * This function is implemeted to support event sharing
1518 * between multiple ports of single IB device.
1521 * Pointer to Ethernet device.
1524 mlx5_dev_shared_handler_devx_uninstall(struct rte_eth_dev *dev)
1526 struct mlx5_priv *priv = dev->data->dev_private;
1527 struct mlx5_ibv_shared *sh = priv->sh;
1529 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1531 pthread_mutex_lock(&sh->intr_mutex);
1532 assert(priv->ibv_port);
1533 assert(priv->ibv_port <= sh->max_port);
1534 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1535 if (sh->port[priv->ibv_port - 1].devx_ih_port_id >= RTE_MAX_ETHPORTS)
1537 assert(sh->port[priv->ibv_port - 1].devx_ih_port_id ==
1538 (uint32_t)dev->data->port_id);
1539 sh->port[priv->ibv_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
1540 if (!sh->devx_intr_cnt || --sh->devx_intr_cnt)
1542 if (sh->intr_handle_devx.fd) {
1543 rte_intr_callback_unregister(&sh->intr_handle_devx,
1544 mlx5_dev_interrupt_handler_devx,
1546 sh->intr_handle_devx.fd = 0;
1547 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1549 if (sh->devx_comp) {
1550 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1551 sh->devx_comp = NULL;
1554 pthread_mutex_unlock(&sh->intr_mutex);
1558 * Install shared asynchronous device events handler.
1559 * This function is implemented to support event sharing
1560 * between multiple ports of single IB device.
1563 * Pointer to Ethernet device.
1566 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1568 struct mlx5_priv *priv = dev->data->dev_private;
1569 struct mlx5_ibv_shared *sh = priv->sh;
1573 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1575 pthread_mutex_lock(&sh->intr_mutex);
1576 assert(priv->ibv_port);
1577 assert(priv->ibv_port <= sh->max_port);
1578 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1579 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1580 /* The handler is already installed for this port. */
1581 assert(sh->intr_cnt);
1585 sh->port[priv->ibv_port - 1].ih_port_id =
1586 (uint32_t)dev->data->port_id;
1590 /* No shared handler installed. */
1591 assert(sh->ctx->async_fd > 0);
1592 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1593 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1595 DRV_LOG(INFO, "failed to change file descriptor async event"
1597 /* Indicate there will be no interrupts. */
1598 dev->data->dev_conf.intr_conf.lsc = 0;
1599 dev->data->dev_conf.intr_conf.rmv = 0;
1601 sh->intr_handle.fd = sh->ctx->async_fd;
1602 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1603 rte_intr_callback_register(&sh->intr_handle,
1604 mlx5_dev_interrupt_handler, sh);
1606 sh->port[priv->ibv_port - 1].ih_port_id =
1607 (uint32_t)dev->data->port_id;
1610 pthread_mutex_unlock(&sh->intr_mutex);
1614 * Install devx shared asyncronous device events handler.
1615 * This function is implemeted to support event sharing
1616 * between multiple ports of single IB device.
1619 * Pointer to Ethernet device.
1622 mlx5_dev_shared_handler_devx_install(struct rte_eth_dev *dev)
1624 struct mlx5_priv *priv = dev->data->dev_private;
1625 struct mlx5_ibv_shared *sh = priv->sh;
1627 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1629 pthread_mutex_lock(&sh->intr_mutex);
1630 assert(priv->ibv_port);
1631 assert(priv->ibv_port <= sh->max_port);
1632 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1633 if (sh->port[priv->ibv_port - 1].devx_ih_port_id < RTE_MAX_ETHPORTS) {
1634 /* The handler is already installed for this port. */
1635 assert(sh->devx_intr_cnt);
1638 if (sh->devx_intr_cnt) {
1639 sh->devx_intr_cnt++;
1640 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1641 (uint32_t)dev->data->port_id;
1644 if (priv->config.devx) {
1645 #ifndef HAVE_IBV_DEVX_ASYNC
1648 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1649 if (sh->devx_comp) {
1650 int flags = fcntl(sh->devx_comp->fd, F_GETFL);
1651 int ret = fcntl(sh->devx_comp->fd, F_SETFL,
1652 flags | O_NONBLOCK);
1655 DRV_LOG(INFO, "failed to change file descriptor"
1656 " devx async event queue");
1658 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1659 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1660 rte_intr_callback_register
1661 (&sh->intr_handle_devx,
1662 mlx5_dev_interrupt_handler_devx, sh);
1663 sh->devx_intr_cnt++;
1664 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1665 (uint32_t)dev->data->port_id;
1668 #endif /* HAVE_IBV_DEVX_ASYNC */
1671 pthread_mutex_unlock(&sh->intr_mutex);
1675 * Uninstall interrupt handler.
1678 * Pointer to Ethernet device.
1681 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1683 mlx5_dev_shared_handler_uninstall(dev);
1687 * Install interrupt handler.
1690 * Pointer to Ethernet device.
1693 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1695 mlx5_dev_shared_handler_install(dev);
1699 * Devx uninstall interrupt handler.
1702 * Pointer to Ethernet device.
1705 mlx5_dev_interrupt_handler_devx_uninstall(struct rte_eth_dev *dev)
1707 mlx5_dev_shared_handler_devx_uninstall(dev);
1711 * Devx install interrupt handler.
1714 * Pointer to Ethernet device.
1717 mlx5_dev_interrupt_handler_devx_install(struct rte_eth_dev *dev)
1719 mlx5_dev_shared_handler_devx_install(dev);
1723 * DPDK callback to bring the link DOWN.
1726 * Pointer to Ethernet device structure.
1729 * 0 on success, a negative errno value otherwise and rte_errno is set.
1732 mlx5_set_link_down(struct rte_eth_dev *dev)
1734 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1738 * DPDK callback to bring the link UP.
1741 * Pointer to Ethernet device structure.
1744 * 0 on success, a negative errno value otherwise and rte_errno is set.
1747 mlx5_set_link_up(struct rte_eth_dev *dev)
1749 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1753 * Configure the RX function to use.
1756 * Pointer to private data structure.
1759 * Pointer to selected Rx burst function.
1762 mlx5_select_rx_function(struct rte_eth_dev *dev)
1764 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1766 assert(dev != NULL);
1767 if (mlx5_check_vec_rx_support(dev) > 0) {
1768 rx_pkt_burst = mlx5_rx_burst_vec;
1769 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1770 dev->data->port_id);
1771 } else if (mlx5_mprq_enabled(dev)) {
1772 rx_pkt_burst = mlx5_rx_burst_mprq;
1774 return rx_pkt_burst;
1778 * Check if mlx5 device was removed.
1781 * Pointer to Ethernet device structure.
1784 * 1 when device is removed, otherwise 0.
1787 mlx5_is_removed(struct rte_eth_dev *dev)
1789 struct ibv_device_attr device_attr;
1790 struct mlx5_priv *priv = dev->data->dev_private;
1792 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1798 * Get the E-Switch parameters by port id.
1803 * Device port id is valid, skip check. This flag is useful
1804 * when trials are performed from probing and device is not
1805 * flagged as valid yet (in attaching process).
1806 * @param[out] es_domain_id
1807 * E-Switch domain id.
1808 * @param[out] es_port_id
1809 * The port id of the port in the E-Switch.
1812 * pointer to device private data structure containing data needed
1813 * on success, NULL otherwise and rte_errno is set.
1816 mlx5_port_to_eswitch_info(uint16_t port, bool valid)
1818 struct rte_eth_dev *dev;
1819 struct mlx5_priv *priv;
1821 if (port >= RTE_MAX_ETHPORTS) {
1825 if (!valid && !rte_eth_dev_is_valid_port(port)) {
1829 dev = &rte_eth_devices[port];
1830 priv = dev->data->dev_private;
1831 if (!(priv->representor || priv->master)) {
1839 * Get the E-Switch parameters by device instance.
1843 * @param[out] es_domain_id
1844 * E-Switch domain id.
1845 * @param[out] es_port_id
1846 * The port id of the port in the E-Switch.
1849 * pointer to device private data structure containing data needed
1850 * on success, NULL otherwise and rte_errno is set.
1853 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
1855 struct mlx5_priv *priv;
1857 priv = dev->data->dev_private;
1858 if (!(priv->representor || priv->master)) {
1866 * Get switch information associated with network interface.
1869 * Network interface index.
1871 * Switch information object, populated in case of success.
1874 * 0 on success, a negative errno value otherwise and rte_errno is set.
1877 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1879 char ifname[IF_NAMESIZE];
1880 char port_name[IF_NAMESIZE];
1882 struct mlx5_switch_info data = {
1885 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1890 bool port_switch_id_set = false;
1891 bool device_dir = false;
1895 if (!if_indextoname(ifindex, ifname)) {
1900 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1902 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1904 MKSTR(pci_device, "/sys/class/net/%s/device",
1907 file = fopen(phys_port_name, "rb");
1909 ret = fscanf(file, "%s", port_name);
1912 mlx5_translate_port_name(port_name, &data);
1914 file = fopen(phys_switch_id, "rb");
1919 port_switch_id_set =
1920 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1923 dir = opendir(pci_device);
1928 if (port_switch_id_set) {
1929 /* We have some E-Switch configuration. */
1930 mlx5_sysfs_check_switch_info(device_dir, &data);
1933 assert(!(data.master && data.representor));
1934 if (data.master && data.representor) {
1935 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1936 " and as representor", ifindex);
1944 * Analyze gathered port parameters via Netlink to recognize master
1945 * and representor devices for E-Switch configuration.
1947 * @param[in] num_vf_set
1948 * flag of presence of number of VFs port attribute.
1949 * @param[inout] switch_info
1950 * Port information, including port name as a number and port name
1951 * type if recognized
1954 * master and representor flags are set in switch_info according to
1955 * recognized parameters (if any).
1958 mlx5_nl_check_switch_info(bool num_vf_set,
1959 struct mlx5_switch_info *switch_info)
1961 switch (switch_info->name_type) {
1962 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1964 * Name is not recognized, assume the master,
1965 * check the number of VFs key presence.
1967 switch_info->master = num_vf_set;
1969 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1971 * Name is not set, this assumes the legacy naming
1972 * schema for master, just check if there is a
1973 * number of VFs key.
1975 switch_info->master = num_vf_set;
1977 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1978 /* New uplink naming schema recognized. */
1979 switch_info->master = 1;
1981 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1982 /* Legacy representors naming schema. */
1983 switch_info->representor = !num_vf_set;
1985 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1986 /* New representors naming schema. */
1987 switch_info->representor = 1;
1993 * Analyze gathered port parameters via sysfs to recognize master
1994 * and representor devices for E-Switch configuration.
1996 * @param[in] device_dir
1997 * flag of presence of "device" directory under port device key.
1998 * @param[inout] switch_info
1999 * Port information, including port name as a number and port name
2000 * type if recognized
2003 * master and representor flags are set in switch_info according to
2004 * recognized parameters (if any).
2007 mlx5_sysfs_check_switch_info(bool device_dir,
2008 struct mlx5_switch_info *switch_info)
2010 switch (switch_info->name_type) {
2011 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
2013 * Name is not recognized, assume the master,
2014 * check the device directory presence.
2016 switch_info->master = device_dir;
2018 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
2020 * Name is not set, this assumes the legacy naming
2021 * schema for master, just check if there is
2022 * a device directory.
2024 switch_info->master = device_dir;
2026 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
2027 /* New uplink naming schema recognized. */
2028 switch_info->master = 1;
2030 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
2031 /* Legacy representors naming schema. */
2032 switch_info->representor = !device_dir;
2034 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2035 /* New representors naming schema. */
2036 switch_info->representor = 1;
2042 * Extract port name, as a number, from sysfs or netlink information.
2044 * @param[in] port_name_in
2045 * String representing the port name.
2046 * @param[out] port_info_out
2047 * Port information, including port name as a number and port name
2048 * type if recognized
2051 * port_name field set according to recognized name format.
2054 mlx5_translate_port_name(const char *port_name_in,
2055 struct mlx5_switch_info *port_info_out)
2057 char pf_c1, pf_c2, vf_c1, vf_c2;
2062 * Check for port-name as a string of the form pf0vf0
2063 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
2065 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
2066 &pf_c1, &pf_c2, &port_info_out->pf_num,
2067 &vf_c1, &vf_c2, &port_info_out->port_name);
2068 if (sc_items == 6 &&
2069 pf_c1 == 'p' && pf_c2 == 'f' &&
2070 vf_c1 == 'v' && vf_c2 == 'f') {
2071 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
2075 * Check for port-name as a string of the form p0
2076 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
2078 sc_items = sscanf(port_name_in, "%c%d",
2079 &pf_c1, &port_info_out->port_name);
2080 if (sc_items == 2 && pf_c1 == 'p') {
2081 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2084 /* Check for port-name as a number (support kernel ver < 5.0 */
2086 port_info_out->port_name = strtol(port_name_in, &end, 0);
2088 (size_t)(end - port_name_in) == strlen(port_name_in)) {
2089 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
2092 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
2097 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
2100 * Pointer to Ethernet device structure.
2101 * @param[out] modinfo
2102 * Storage for plug-in module EEPROM information.
2105 * 0 on success, a negative errno value otherwise and rte_errno is set.
2108 mlx5_get_module_info(struct rte_eth_dev *dev,
2109 struct rte_eth_dev_module_info *modinfo)
2111 struct ethtool_modinfo info = {
2112 .cmd = ETHTOOL_GMODULEINFO,
2114 struct ifreq ifr = (struct ifreq) {
2115 .ifr_data = (void *)&info,
2119 if (!dev || !modinfo) {
2120 DRV_LOG(WARNING, "missing argument, cannot get module info");
2124 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2126 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2127 dev->data->port_id, strerror(rte_errno));
2130 modinfo->type = info.type;
2131 modinfo->eeprom_len = info.eeprom_len;
2136 * DPDK callback to retrieve plug-in module EEPROM data.
2139 * Pointer to Ethernet device structure.
2141 * Storage for plug-in module EEPROM data.
2144 * 0 on success, a negative errno value otherwise and rte_errno is set.
2146 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
2147 struct rte_dev_eeprom_info *info)
2149 struct ethtool_eeprom *eeprom;
2153 if (!dev || !info) {
2154 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
2158 eeprom = rte_calloc(__func__, 1,
2159 (sizeof(struct ethtool_eeprom) + info->length), 0);
2161 DRV_LOG(WARNING, "port %u cannot allocate memory for "
2162 "eeprom data", dev->data->port_id);
2166 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2167 eeprom->offset = info->offset;
2168 eeprom->len = info->length;
2169 ifr = (struct ifreq) {
2170 .ifr_data = (void *)eeprom,
2172 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2174 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2175 dev->data->port_id, strerror(rte_errno));
2177 rte_memcpy(info->data, eeprom->data, info->length);
2183 * DPDK callback to retrieve hairpin capabilities.
2186 * Pointer to Ethernet device structure.
2188 * Storage for hairpin capability data.
2191 * 0 on success, a negative errno value otherwise and rte_errno is set.
2193 int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
2194 struct rte_eth_hairpin_cap *cap)
2196 struct mlx5_priv *priv = dev->data->dev_private;
2198 if (priv->sh->devx == 0) {
2199 rte_errno = ENOTSUP;
2202 cap->max_nb_queues = UINT16_MAX;
2203 cap->max_rx_2_tx = 1;
2204 cap->max_tx_2_rx = 1;
2205 cap->max_nb_desc = 8192;