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>
36 #include <rte_cycles.h>
38 #include <mlx5_glue.h>
39 #include <mlx5_devx_cmds.h>
40 #include <mlx5_common.h>
43 #include "mlx5_rxtx.h"
44 #include "mlx5_utils.h"
46 /* Supported speed values found in /usr/include/linux/ethtool.h */
47 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
48 #define SUPPORTED_40000baseKR4_Full (1 << 23)
50 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
51 #define SUPPORTED_40000baseCR4_Full (1 << 24)
53 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
54 #define SUPPORTED_40000baseSR4_Full (1 << 25)
56 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
57 #define SUPPORTED_40000baseLR4_Full (1 << 26)
59 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
60 #define SUPPORTED_56000baseKR4_Full (1 << 27)
62 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
63 #define SUPPORTED_56000baseCR4_Full (1 << 28)
65 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
66 #define SUPPORTED_56000baseSR4_Full (1 << 29)
68 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
69 #define SUPPORTED_56000baseLR4_Full (1 << 30)
72 /* Add defines in case the running kernel is not the same as user headers. */
73 #ifndef ETHTOOL_GLINKSETTINGS
74 struct ethtool_link_settings {
83 uint8_t eth_tp_mdix_ctrl;
84 int8_t link_mode_masks_nwords;
86 uint32_t link_mode_masks[];
89 #define ETHTOOL_GLINKSETTINGS 0x0000004c
90 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
91 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
92 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
93 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
94 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
95 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
96 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
97 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
98 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
99 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
100 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
101 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
102 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
103 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
104 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
105 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
107 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
108 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
109 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
110 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
112 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
113 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
114 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
116 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
117 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
118 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
119 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
120 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
124 * Get master interface name from private structure.
127 * Pointer to Ethernet device.
129 * Interface name output buffer.
132 * 0 on success, a negative errno value otherwise and rte_errno is set.
135 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
139 unsigned int dev_type = 0;
140 unsigned int dev_port_prev = ~0u;
141 char match[IF_NAMESIZE] = "";
143 MLX5_ASSERT(ibdev_path);
145 MKSTR(path, "%s/device/net", ibdev_path);
153 while ((dent = readdir(dir)) != NULL) {
154 char *name = dent->d_name;
156 unsigned int dev_port;
159 if ((name[0] == '.') &&
160 ((name[1] == '\0') ||
161 ((name[1] == '.') && (name[2] == '\0'))))
164 MKSTR(path, "%s/device/net/%s/%s",
166 (dev_type ? "dev_id" : "dev_port"));
168 file = fopen(path, "rb");
173 * Switch to dev_id when dev_port does not exist as
174 * is the case with Linux kernel versions < 3.15.
185 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
190 * Switch to dev_id when dev_port returns the same value for
191 * all ports. May happen when using a MOFED release older than
192 * 3.0 with a Linux kernel >= 3.15.
194 if (dev_port == dev_port_prev)
196 dev_port_prev = dev_port;
198 strlcpy(match, name, sizeof(match));
201 if (match[0] == '\0') {
205 strncpy(*ifname, match, sizeof(*ifname));
210 * Get interface name from private structure.
212 * This is a port representor-aware version of mlx5_get_master_ifname().
215 * Pointer to Ethernet device.
217 * Interface name output buffer.
220 * 0 on success, a negative errno value otherwise and rte_errno is set.
223 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
225 struct mlx5_priv *priv = dev->data->dev_private;
226 unsigned int ifindex;
229 MLX5_ASSERT(priv->sh);
230 ifindex = mlx5_ifindex(dev);
232 if (!priv->representor)
233 return mlx5_get_master_ifname(priv->sh->ibdev_path,
238 if (if_indextoname(ifindex, &(*ifname)[0]))
245 * Get the interface index from device name.
248 * Pointer to Ethernet device.
251 * Nonzero interface index on success, zero otherwise and rte_errno is set.
254 mlx5_ifindex(const struct rte_eth_dev *dev)
256 struct mlx5_priv *priv = dev->data->dev_private;
257 unsigned int ifindex;
260 MLX5_ASSERT(priv->if_index);
261 ifindex = priv->if_index;
268 * Perform ifreq ioctl() on associated Ethernet device.
271 * Pointer to Ethernet device.
273 * Request number to pass to ioctl().
275 * Interface request structure output buffer.
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)
283 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
290 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
293 ret = ioctl(sock, req, ifr);
309 * Pointer to Ethernet device.
311 * MTU value output buffer.
314 * 0 on success, a negative errno value otherwise and rte_errno is set.
317 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
319 struct ifreq request;
320 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
324 *mtu = request.ifr_mtu;
332 * Pointer to Ethernet device.
337 * 0 on success, a negative errno value otherwise and rte_errno is set.
340 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
342 struct ifreq request = { .ifr_mtu = mtu, };
344 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
351 * Pointer to Ethernet device.
353 * Bitmask for flags that must remain untouched.
355 * Bitmask for flags to modify.
358 * 0 on success, a negative errno value otherwise and rte_errno is set.
361 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
363 struct ifreq request;
364 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
368 request.ifr_flags &= keep;
369 request.ifr_flags |= flags & ~keep;
370 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
374 * DPDK callback for Ethernet device configuration.
377 * Pointer to Ethernet device structure.
380 * 0 on success, a negative errno value otherwise and rte_errno is set.
383 mlx5_dev_configure(struct rte_eth_dev *dev)
385 struct mlx5_priv *priv = dev->data->dev_private;
386 unsigned int rxqs_n = dev->data->nb_rx_queues;
387 unsigned int txqs_n = dev->data->nb_tx_queues;
388 const uint8_t use_app_rss_key =
389 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
392 if (use_app_rss_key &&
393 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
394 MLX5_RSS_HASH_KEY_LEN)) {
395 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
396 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
400 priv->rss_conf.rss_key =
401 rte_realloc(priv->rss_conf.rss_key,
402 MLX5_RSS_HASH_KEY_LEN, 0);
403 if (!priv->rss_conf.rss_key) {
404 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
405 dev->data->port_id, rxqs_n);
410 if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
411 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
413 memcpy(priv->rss_conf.rss_key,
415 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
416 rss_hash_default_key,
417 MLX5_RSS_HASH_KEY_LEN);
418 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
419 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
420 priv->rxqs = (void *)dev->data->rx_queues;
421 priv->txqs = (void *)dev->data->tx_queues;
422 if (txqs_n != priv->txqs_n) {
423 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
424 dev->data->port_id, priv->txqs_n, txqs_n);
425 priv->txqs_n = txqs_n;
427 if (rxqs_n > priv->config.ind_table_max_size) {
428 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
429 dev->data->port_id, rxqs_n);
433 if (rxqs_n != priv->rxqs_n) {
434 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
435 dev->data->port_id, priv->rxqs_n, rxqs_n);
436 priv->rxqs_n = rxqs_n;
438 priv->skip_default_rss_reta = 0;
439 ret = mlx5_proc_priv_init(dev);
446 * Configure default RSS reta.
449 * Pointer to Ethernet device structure.
452 * 0 on success, a negative errno value otherwise and rte_errno is set.
455 mlx5_dev_configure_rss_reta(struct rte_eth_dev *dev)
457 struct mlx5_priv *priv = dev->data->dev_private;
458 unsigned int rxqs_n = dev->data->nb_rx_queues;
461 unsigned int reta_idx_n;
463 unsigned int *rss_queue_arr = NULL;
464 unsigned int rss_queue_n = 0;
466 if (priv->skip_default_rss_reta)
468 rss_queue_arr = rte_malloc("", rxqs_n * sizeof(unsigned int), 0);
469 if (!rss_queue_arr) {
470 DRV_LOG(ERR, "port %u cannot allocate RSS queue list (%u)",
471 dev->data->port_id, rxqs_n);
475 for (i = 0, j = 0; i < rxqs_n; i++) {
476 struct mlx5_rxq_data *rxq_data;
477 struct mlx5_rxq_ctrl *rxq_ctrl;
479 rxq_data = (*priv->rxqs)[i];
480 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
481 if (rxq_ctrl && rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
482 rss_queue_arr[j++] = i;
485 if (rss_queue_n > priv->config.ind_table_max_size) {
486 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
487 dev->data->port_id, rss_queue_n);
489 rte_free(rss_queue_arr);
492 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
493 dev->data->port_id, priv->rxqs_n, rxqs_n);
494 priv->rxqs_n = rxqs_n;
496 * If the requested number of RX queues is not a power of two,
497 * use the maximum indirection table size for better balancing.
498 * The result is always rounded to the next power of two.
500 reta_idx_n = (1 << log2above((rss_queue_n & (rss_queue_n - 1)) ?
501 priv->config.ind_table_max_size :
503 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
505 rte_free(rss_queue_arr);
509 * When the number of RX queues is not a power of two,
510 * the remaining table entries are padded with reused WQs
511 * and hashes are not spread uniformly.
513 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
514 (*priv->reta_idx)[i] = rss_queue_arr[j];
515 if (++j == rss_queue_n)
518 rte_free(rss_queue_arr);
523 * Sets default tuning parameters.
526 * Pointer to Ethernet device.
528 * Info structure output buffer.
531 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
533 struct mlx5_priv *priv = dev->data->dev_private;
535 /* Minimum CPU utilization. */
536 info->default_rxportconf.ring_size = 256;
537 info->default_txportconf.ring_size = 256;
538 info->default_rxportconf.burst_size = MLX5_RX_DEFAULT_BURST;
539 info->default_txportconf.burst_size = MLX5_TX_DEFAULT_BURST;
540 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
541 info->default_rxportconf.nb_queues = 16;
542 info->default_txportconf.nb_queues = 16;
543 if (dev->data->nb_rx_queues > 2 ||
544 dev->data->nb_tx_queues > 2) {
545 /* Max Throughput. */
546 info->default_rxportconf.ring_size = 2048;
547 info->default_txportconf.ring_size = 2048;
550 info->default_rxportconf.nb_queues = 8;
551 info->default_txportconf.nb_queues = 8;
552 if (dev->data->nb_rx_queues > 2 ||
553 dev->data->nb_tx_queues > 2) {
554 /* Max Throughput. */
555 info->default_rxportconf.ring_size = 4096;
556 info->default_txportconf.ring_size = 4096;
562 * Sets tx mbuf limiting parameters.
565 * Pointer to Ethernet device.
567 * Info structure output buffer.
570 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
572 struct mlx5_priv *priv = dev->data->dev_private;
573 struct mlx5_dev_config *config = &priv->config;
577 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
578 MLX5_SEND_DEF_INLINE_LEN :
579 (unsigned int)config->txq_inline_max;
580 MLX5_ASSERT(config->txq_inline_min >= 0);
581 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
582 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
583 MLX5_ESEG_MIN_INLINE_SIZE -
586 MLX5_WQE_DSEG_SIZE * 2);
587 nb_max = (MLX5_WQE_SIZE_MAX +
588 MLX5_ESEG_MIN_INLINE_SIZE -
592 inlen) / MLX5_WSEG_SIZE;
593 info->tx_desc_lim.nb_seg_max = nb_max;
594 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
598 * DPDK callback to get information about the device.
601 * Pointer to Ethernet device structure.
603 * Info structure output buffer.
606 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
608 struct mlx5_priv *priv = dev->data->dev_private;
609 struct mlx5_dev_config *config = &priv->config;
612 /* FIXME: we should ask the device for these values. */
613 info->min_rx_bufsize = 32;
614 info->max_rx_pktlen = 65536;
615 info->max_lro_pkt_size = MLX5_MAX_LRO_SIZE;
617 * Since we need one CQ per QP, the limit is the minimum number
618 * between the two values.
620 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
621 priv->sh->device_attr.orig_attr.max_qp);
622 /* max_rx_queues is uint16_t. */
623 max = RTE_MIN(max, (unsigned int)UINT16_MAX);
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 MLX5_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 * Handle asynchronous removal event for entire multiport device.
1217 * Infiniband device shared context.
1220 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1224 for (i = 0; i < sh->max_port; ++i) {
1225 struct rte_eth_dev *dev;
1227 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1229 * Or not existing port either no
1230 * handler installed for this port.
1234 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1236 if (dev->data->dev_conf.intr_conf.rmv)
1237 _rte_eth_dev_callback_process
1238 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1243 * Handle shared asynchronous events the NIC (removal event
1244 * and link status change). Supports multiport IB device.
1247 * Callback argument.
1250 mlx5_dev_interrupt_handler(void *cb_arg)
1252 struct mlx5_ibv_shared *sh = cb_arg;
1253 struct ibv_async_event event;
1255 /* Read all message from the IB device and acknowledge them. */
1257 struct rte_eth_dev *dev;
1260 if (mlx5_glue->get_async_event(sh->ctx, &event))
1262 /* Retrieve and check IB port index. */
1263 tmp = (uint32_t)event.element.port_num;
1264 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1266 * The DEVICE_FATAL event is called once for
1267 * entire device without port specifying.
1268 * We should notify all existing ports.
1270 mlx5_glue->ack_async_event(&event);
1271 mlx5_dev_interrupt_device_fatal(sh);
1274 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
1276 /* Unsupported devive level event. */
1277 mlx5_glue->ack_async_event(&event);
1279 "unsupported common event (type %d)",
1283 if (tmp > sh->max_port) {
1284 /* Invalid IB port index. */
1285 mlx5_glue->ack_async_event(&event);
1287 "cannot handle an event (type %d)"
1288 "due to invalid IB port index (%u)",
1289 event.event_type, tmp);
1292 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1293 /* No handler installed. */
1294 mlx5_glue->ack_async_event(&event);
1296 "cannot handle an event (type %d)"
1297 "due to no handler installed for port %u",
1298 event.event_type, tmp);
1301 /* Retrieve ethernet device descriptor. */
1302 tmp = sh->port[tmp - 1].ih_port_id;
1303 dev = &rte_eth_devices[tmp];
1305 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1306 event.event_type == IBV_EVENT_PORT_ERR) &&
1307 dev->data->dev_conf.intr_conf.lsc) {
1308 mlx5_glue->ack_async_event(&event);
1309 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1313 _rte_eth_dev_callback_process
1314 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1318 "port %u cannot handle an unknown event (type %d)",
1319 dev->data->port_id, event.event_type);
1320 mlx5_glue->ack_async_event(&event);
1325 * Unregister callback handler safely. The handler may be active
1326 * while we are trying to unregister it, in this case code -EAGAIN
1327 * is returned by rte_intr_callback_unregister(). This routine checks
1328 * the return code and tries to unregister handler again.
1333 * pointer to callback routine
1335 * opaque callback parameter
1338 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1339 rte_intr_callback_fn cb_fn, void *cb_arg)
1342 * Try to reduce timeout management overhead by not calling
1343 * the timer related routines on the first iteration. If the
1344 * unregistering succeeds on first call there will be no
1345 * timer calls at all.
1353 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1356 if (ret != -EAGAIN) {
1357 DRV_LOG(INFO, "failed to unregister interrupt"
1358 " handler (error: %d)", ret);
1363 struct timespec onems;
1365 /* Wait one millisecond and try again. */
1367 onems.tv_nsec = NS_PER_S / MS_PER_S;
1368 nanosleep(&onems, 0);
1369 /* Check whether one second elapsed. */
1370 if ((rte_get_timer_cycles() - start) <= twait)
1374 * We get the amount of timer ticks for one second.
1375 * If this amount elapsed it means we spent one
1376 * second in waiting. This branch is executed once
1377 * on first iteration.
1379 twait = rte_get_timer_hz();
1383 * Timeout elapsed, show message (once a second) and retry.
1384 * We have no other acceptable option here, if we ignore
1385 * the unregistering return code the handler will not
1386 * be unregistered, fd will be closed and we may get the
1387 * crush. Hanging and messaging in the loop seems not to be
1390 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1391 start = rte_get_timer_cycles();
1396 * Handle DEVX interrupts from the NIC.
1397 * This function is probably called from the DPDK host thread.
1400 * Callback argument.
1403 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1405 #ifndef HAVE_IBV_DEVX_ASYNC
1409 struct mlx5_ibv_shared *sh = cb_arg;
1411 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1412 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1413 MLX5_ST_SZ_BYTES(traffic_counter) +
1414 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1416 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1418 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1421 mlx5_flow_async_pool_query_handle
1422 (sh, (uint64_t)out.cmd_resp.wr_id,
1423 mlx5_devx_get_out_command_status(buf));
1424 #endif /* HAVE_IBV_DEVX_ASYNC */
1428 * Uninstall shared asynchronous device events handler.
1429 * This function is implemented to support event sharing
1430 * between multiple ports of single IB device.
1433 * Pointer to Ethernet device.
1436 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1438 struct mlx5_priv *priv = dev->data->dev_private;
1439 struct mlx5_ibv_shared *sh = priv->sh;
1441 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1443 pthread_mutex_lock(&sh->intr_mutex);
1444 MLX5_ASSERT(priv->ibv_port);
1445 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1446 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1447 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1449 MLX5_ASSERT(sh->port[priv->ibv_port - 1].ih_port_id ==
1450 (uint32_t)dev->data->port_id);
1451 MLX5_ASSERT(sh->intr_cnt);
1452 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1453 if (!sh->intr_cnt || --sh->intr_cnt)
1455 mlx5_intr_callback_unregister(&sh->intr_handle,
1456 mlx5_dev_interrupt_handler, sh);
1457 sh->intr_handle.fd = 0;
1458 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1460 pthread_mutex_unlock(&sh->intr_mutex);
1464 * Uninstall devx shared asynchronous device events handler.
1465 * This function is implemeted to support event sharing
1466 * between multiple ports of single IB device.
1469 * Pointer to Ethernet device.
1472 mlx5_dev_shared_handler_devx_uninstall(struct rte_eth_dev *dev)
1474 struct mlx5_priv *priv = dev->data->dev_private;
1475 struct mlx5_ibv_shared *sh = priv->sh;
1477 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1479 pthread_mutex_lock(&sh->intr_mutex);
1480 MLX5_ASSERT(priv->ibv_port);
1481 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1482 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1483 if (sh->port[priv->ibv_port - 1].devx_ih_port_id >= RTE_MAX_ETHPORTS)
1485 MLX5_ASSERT(sh->port[priv->ibv_port - 1].devx_ih_port_id ==
1486 (uint32_t)dev->data->port_id);
1487 sh->port[priv->ibv_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
1488 if (!sh->devx_intr_cnt || --sh->devx_intr_cnt)
1490 if (sh->intr_handle_devx.fd) {
1491 rte_intr_callback_unregister(&sh->intr_handle_devx,
1492 mlx5_dev_interrupt_handler_devx,
1494 sh->intr_handle_devx.fd = 0;
1495 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1497 if (sh->devx_comp) {
1498 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1499 sh->devx_comp = NULL;
1502 pthread_mutex_unlock(&sh->intr_mutex);
1506 * Install shared asynchronous device events handler.
1507 * This function is implemented to support event sharing
1508 * between multiple ports of single IB device.
1511 * Pointer to Ethernet device.
1514 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1516 struct mlx5_priv *priv = dev->data->dev_private;
1517 struct mlx5_ibv_shared *sh = priv->sh;
1521 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1523 pthread_mutex_lock(&sh->intr_mutex);
1524 MLX5_ASSERT(priv->ibv_port);
1525 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1526 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1527 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1528 /* The handler is already installed for this port. */
1529 MLX5_ASSERT(sh->intr_cnt);
1533 sh->port[priv->ibv_port - 1].ih_port_id =
1534 (uint32_t)dev->data->port_id;
1538 /* No shared handler installed. */
1539 MLX5_ASSERT(sh->ctx->async_fd > 0);
1540 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1541 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1543 DRV_LOG(INFO, "failed to change file descriptor async event"
1545 /* Indicate there will be no interrupts. */
1546 dev->data->dev_conf.intr_conf.lsc = 0;
1547 dev->data->dev_conf.intr_conf.rmv = 0;
1549 sh->intr_handle.fd = sh->ctx->async_fd;
1550 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1551 rte_intr_callback_register(&sh->intr_handle,
1552 mlx5_dev_interrupt_handler, sh);
1554 sh->port[priv->ibv_port - 1].ih_port_id =
1555 (uint32_t)dev->data->port_id;
1558 pthread_mutex_unlock(&sh->intr_mutex);
1562 * Install devx shared asyncronous device events handler.
1563 * This function is implemeted to support event sharing
1564 * between multiple ports of single IB device.
1567 * Pointer to Ethernet device.
1570 mlx5_dev_shared_handler_devx_install(struct rte_eth_dev *dev)
1572 struct mlx5_priv *priv = dev->data->dev_private;
1573 struct mlx5_ibv_shared *sh = priv->sh;
1575 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1577 pthread_mutex_lock(&sh->intr_mutex);
1578 MLX5_ASSERT(priv->ibv_port);
1579 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1580 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1581 if (sh->port[priv->ibv_port - 1].devx_ih_port_id < RTE_MAX_ETHPORTS) {
1582 /* The handler is already installed for this port. */
1583 MLX5_ASSERT(sh->devx_intr_cnt);
1586 if (sh->devx_intr_cnt) {
1587 sh->devx_intr_cnt++;
1588 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1589 (uint32_t)dev->data->port_id;
1592 if (priv->config.devx) {
1593 #ifndef HAVE_IBV_DEVX_ASYNC
1596 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1597 if (sh->devx_comp) {
1598 int flags = fcntl(sh->devx_comp->fd, F_GETFL);
1599 int ret = fcntl(sh->devx_comp->fd, F_SETFL,
1600 flags | O_NONBLOCK);
1603 DRV_LOG(INFO, "failed to change file descriptor"
1604 " devx async event queue");
1606 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1607 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1608 rte_intr_callback_register
1609 (&sh->intr_handle_devx,
1610 mlx5_dev_interrupt_handler_devx, sh);
1611 sh->devx_intr_cnt++;
1612 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1613 (uint32_t)dev->data->port_id;
1616 #endif /* HAVE_IBV_DEVX_ASYNC */
1619 pthread_mutex_unlock(&sh->intr_mutex);
1623 * Uninstall interrupt handler.
1626 * Pointer to Ethernet device.
1629 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1631 mlx5_dev_shared_handler_uninstall(dev);
1635 * Install interrupt handler.
1638 * Pointer to Ethernet device.
1641 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1643 mlx5_dev_shared_handler_install(dev);
1647 * Devx uninstall interrupt handler.
1650 * Pointer to Ethernet device.
1653 mlx5_dev_interrupt_handler_devx_uninstall(struct rte_eth_dev *dev)
1655 mlx5_dev_shared_handler_devx_uninstall(dev);
1659 * Devx install interrupt handler.
1662 * Pointer to Ethernet device.
1665 mlx5_dev_interrupt_handler_devx_install(struct rte_eth_dev *dev)
1667 mlx5_dev_shared_handler_devx_install(dev);
1671 * DPDK callback to bring the link DOWN.
1674 * Pointer to Ethernet device structure.
1677 * 0 on success, a negative errno value otherwise and rte_errno is set.
1680 mlx5_set_link_down(struct rte_eth_dev *dev)
1682 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1686 * DPDK callback to bring the link UP.
1689 * Pointer to Ethernet device structure.
1692 * 0 on success, a negative errno value otherwise and rte_errno is set.
1695 mlx5_set_link_up(struct rte_eth_dev *dev)
1697 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1701 * Configure the RX function to use.
1704 * Pointer to private data structure.
1707 * Pointer to selected Rx burst function.
1710 mlx5_select_rx_function(struct rte_eth_dev *dev)
1712 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1714 MLX5_ASSERT(dev != NULL);
1715 if (mlx5_check_vec_rx_support(dev) > 0) {
1716 rx_pkt_burst = mlx5_rx_burst_vec;
1717 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1718 dev->data->port_id);
1719 } else if (mlx5_mprq_enabled(dev)) {
1720 rx_pkt_burst = mlx5_rx_burst_mprq;
1722 return rx_pkt_burst;
1726 * Check if mlx5 device was removed.
1729 * Pointer to Ethernet device structure.
1732 * 1 when device is removed, otherwise 0.
1735 mlx5_is_removed(struct rte_eth_dev *dev)
1737 struct ibv_device_attr device_attr;
1738 struct mlx5_priv *priv = dev->data->dev_private;
1740 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1746 * Get the E-Switch parameters by port id.
1751 * Device port id is valid, skip check. This flag is useful
1752 * when trials are performed from probing and device is not
1753 * flagged as valid yet (in attaching process).
1754 * @param[out] es_domain_id
1755 * E-Switch domain id.
1756 * @param[out] es_port_id
1757 * The port id of the port in the E-Switch.
1760 * pointer to device private data structure containing data needed
1761 * on success, NULL otherwise and rte_errno is set.
1764 mlx5_port_to_eswitch_info(uint16_t port, bool valid)
1766 struct rte_eth_dev *dev;
1767 struct mlx5_priv *priv;
1769 if (port >= RTE_MAX_ETHPORTS) {
1773 if (!valid && !rte_eth_dev_is_valid_port(port)) {
1777 dev = &rte_eth_devices[port];
1778 priv = dev->data->dev_private;
1779 if (!(priv->representor || priv->master)) {
1787 * Get the E-Switch parameters by device instance.
1791 * @param[out] es_domain_id
1792 * E-Switch domain id.
1793 * @param[out] es_port_id
1794 * The port id of the port in the E-Switch.
1797 * pointer to device private data structure containing data needed
1798 * on success, NULL otherwise and rte_errno is set.
1801 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
1803 struct mlx5_priv *priv;
1805 priv = dev->data->dev_private;
1806 if (!(priv->representor || priv->master)) {
1814 * Get switch information associated with network interface.
1817 * Network interface index.
1819 * Switch information object, populated in case of success.
1822 * 0 on success, a negative errno value otherwise and rte_errno is set.
1825 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1827 char ifname[IF_NAMESIZE];
1828 char port_name[IF_NAMESIZE];
1830 struct mlx5_switch_info data = {
1833 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1838 bool port_switch_id_set = false;
1839 bool device_dir = false;
1843 if (!if_indextoname(ifindex, ifname)) {
1848 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1850 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1852 MKSTR(pci_device, "/sys/class/net/%s/device",
1855 file = fopen(phys_port_name, "rb");
1857 ret = fscanf(file, "%s", port_name);
1860 mlx5_translate_port_name(port_name, &data);
1862 file = fopen(phys_switch_id, "rb");
1867 port_switch_id_set =
1868 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1871 dir = opendir(pci_device);
1876 if (port_switch_id_set) {
1877 /* We have some E-Switch configuration. */
1878 mlx5_sysfs_check_switch_info(device_dir, &data);
1881 MLX5_ASSERT(!(data.master && data.representor));
1882 if (data.master && data.representor) {
1883 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1884 " and as representor", ifindex);
1892 * Analyze gathered port parameters via sysfs to recognize master
1893 * and representor devices for E-Switch configuration.
1895 * @param[in] device_dir
1896 * flag of presence of "device" directory under port device key.
1897 * @param[inout] switch_info
1898 * Port information, including port name as a number and port name
1899 * type if recognized
1902 * master and representor flags are set in switch_info according to
1903 * recognized parameters (if any).
1906 mlx5_sysfs_check_switch_info(bool device_dir,
1907 struct mlx5_switch_info *switch_info)
1909 switch (switch_info->name_type) {
1910 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1912 * Name is not recognized, assume the master,
1913 * check the device directory presence.
1915 switch_info->master = device_dir;
1917 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1919 * Name is not set, this assumes the legacy naming
1920 * schema for master, just check if there is
1921 * a device directory.
1923 switch_info->master = device_dir;
1925 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1926 /* New uplink naming schema recognized. */
1927 switch_info->master = 1;
1929 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1930 /* Legacy representors naming schema. */
1931 switch_info->representor = !device_dir;
1933 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1934 /* New representors naming schema. */
1935 switch_info->representor = 1;
1941 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1944 * Pointer to Ethernet device structure.
1945 * @param[out] modinfo
1946 * Storage for plug-in module EEPROM information.
1949 * 0 on success, a negative errno value otherwise and rte_errno is set.
1952 mlx5_get_module_info(struct rte_eth_dev *dev,
1953 struct rte_eth_dev_module_info *modinfo)
1955 struct ethtool_modinfo info = {
1956 .cmd = ETHTOOL_GMODULEINFO,
1958 struct ifreq ifr = (struct ifreq) {
1959 .ifr_data = (void *)&info,
1963 if (!dev || !modinfo) {
1964 DRV_LOG(WARNING, "missing argument, cannot get module info");
1968 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1970 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1971 dev->data->port_id, strerror(rte_errno));
1974 modinfo->type = info.type;
1975 modinfo->eeprom_len = info.eeprom_len;
1980 * DPDK callback to retrieve plug-in module EEPROM data.
1983 * Pointer to Ethernet device structure.
1985 * Storage for plug-in module EEPROM data.
1988 * 0 on success, a negative errno value otherwise and rte_errno is set.
1990 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1991 struct rte_dev_eeprom_info *info)
1993 struct ethtool_eeprom *eeprom;
1997 if (!dev || !info) {
1998 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
2002 eeprom = rte_calloc(__func__, 1,
2003 (sizeof(struct ethtool_eeprom) + info->length), 0);
2005 DRV_LOG(WARNING, "port %u cannot allocate memory for "
2006 "eeprom data", dev->data->port_id);
2010 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2011 eeprom->offset = info->offset;
2012 eeprom->len = info->length;
2013 ifr = (struct ifreq) {
2014 .ifr_data = (void *)eeprom,
2016 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2018 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2019 dev->data->port_id, strerror(rte_errno));
2021 rte_memcpy(info->data, eeprom->data, info->length);
2027 * DPDK callback to retrieve hairpin capabilities.
2030 * Pointer to Ethernet device structure.
2032 * Storage for hairpin capability data.
2035 * 0 on success, a negative errno value otherwise and rte_errno is set.
2037 int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
2038 struct rte_eth_hairpin_cap *cap)
2040 struct mlx5_priv *priv = dev->data->dev_private;
2042 if (priv->sh->devx == 0) {
2043 rte_errno = ENOTSUP;
2046 cap->max_nb_queues = UINT16_MAX;
2047 cap->max_rx_2_tx = 1;
2048 cap->max_tx_2_rx = 1;
2049 cap->max_nb_desc = 8192;