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 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
625 info->max_rx_queues = max;
626 info->max_tx_queues = max;
627 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
628 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
629 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
630 info->rx_queue_offload_capa);
631 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
632 info->if_index = mlx5_ifindex(dev);
633 info->reta_size = priv->reta_idx_n ?
634 priv->reta_idx_n : config->ind_table_max_size;
635 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
636 info->speed_capa = priv->link_speed_capa;
637 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
638 mlx5_set_default_params(dev, info);
639 mlx5_set_txlimit_params(dev, info);
640 info->switch_info.name = dev->data->name;
641 info->switch_info.domain_id = priv->domain_id;
642 info->switch_info.port_id = priv->representor_id;
643 if (priv->representor) {
646 if (priv->pf_bond >= 0) {
648 * Switch port ID is opaque value with driver defined
649 * format. Push the PF index in bonding configurations
650 * in upper four bits of port ID. If we get too many
651 * representors (more than 4K) or PFs (more than 15)
652 * this approach must be reconsidered.
654 if ((info->switch_info.port_id >>
655 MLX5_PORT_ID_BONDING_PF_SHIFT) ||
656 priv->pf_bond > MLX5_PORT_ID_BONDING_PF_MASK) {
657 DRV_LOG(ERR, "can't update switch port ID"
658 " for bonding device");
662 info->switch_info.port_id |=
663 priv->pf_bond << MLX5_PORT_ID_BONDING_PF_SHIFT;
665 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
666 struct mlx5_priv *opriv =
667 rte_eth_devices[port_id].data->dev_private;
670 opriv->representor ||
671 opriv->sh != priv->sh ||
672 opriv->domain_id != priv->domain_id)
675 * Override switch name with that of the master
678 info->switch_info.name = opriv->dev_data->name;
686 * Get device current raw clock counter
689 * Pointer to Ethernet device structure.
691 * Current raw clock counter of the device.
694 * 0 if the clock has correctly been read
695 * The value of errno in case of error
698 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
700 struct mlx5_priv *priv = dev->data->dev_private;
701 struct ibv_context *ctx = priv->sh->ctx;
702 struct ibv_values_ex values;
705 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
706 err = mlx5_glue->query_rt_values_ex(ctx, &values);
708 DRV_LOG(WARNING, "Could not query the clock !");
711 *clock = values.raw_clock.tv_nsec;
716 * Get firmware version of a device.
719 * Ethernet device port.
721 * String output allocated by caller.
723 * Size of the output string, including terminating null byte.
726 * 0 on success, or the size of the non truncated string if too big.
728 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
730 struct mlx5_priv *priv = dev->data->dev_private;
731 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
732 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
737 strlcpy(fw_ver, attr->fw_ver, fw_size);
742 * Get supported packet types.
745 * Pointer to Ethernet device structure.
748 * A pointer to the supported Packet types array.
751 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
753 static const uint32_t ptypes[] = {
754 /* refers to rxq_cq_to_pkt_type() */
756 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
757 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
758 RTE_PTYPE_L4_NONFRAG,
762 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
763 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
764 RTE_PTYPE_INNER_L4_NONFRAG,
765 RTE_PTYPE_INNER_L4_FRAG,
766 RTE_PTYPE_INNER_L4_TCP,
767 RTE_PTYPE_INNER_L4_UDP,
771 if (dev->rx_pkt_burst == mlx5_rx_burst ||
772 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
773 dev->rx_pkt_burst == mlx5_rx_burst_vec)
779 * Retrieve the master device for representor in the same switch domain.
782 * Pointer to representor Ethernet device structure.
785 * Master device structure on success, NULL otherwise.
788 static struct rte_eth_dev *
789 mlx5_find_master_dev(struct rte_eth_dev *dev)
791 struct mlx5_priv *priv;
795 priv = dev->data->dev_private;
796 domain_id = priv->domain_id;
797 MLX5_ASSERT(priv->representor);
798 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
799 struct mlx5_priv *opriv =
800 rte_eth_devices[port_id].data->dev_private;
803 opriv->domain_id == domain_id &&
804 opriv->sh == priv->sh)
805 return &rte_eth_devices[port_id];
811 * DPDK callback to retrieve physical link information.
814 * Pointer to Ethernet device structure.
816 * Storage for current link status.
819 * 0 on success, a negative errno value otherwise and rte_errno is set.
822 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
823 struct rte_eth_link *link)
825 struct mlx5_priv *priv = dev->data->dev_private;
826 struct ethtool_cmd edata = {
827 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
830 struct rte_eth_link dev_link;
834 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
836 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
837 dev->data->port_id, strerror(rte_errno));
840 dev_link = (struct rte_eth_link) {
841 .link_status = ((ifr.ifr_flags & IFF_UP) &&
842 (ifr.ifr_flags & IFF_RUNNING)),
844 ifr = (struct ifreq) {
845 .ifr_data = (void *)&edata,
847 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
849 if (ret == -ENOTSUP && priv->representor) {
850 struct rte_eth_dev *master;
853 * For representors we can try to inherit link
854 * settings from the master device. Actually
855 * link settings do not make a lot of sense
856 * for representors due to missing physical
857 * link. The old kernel drivers supported
858 * emulated settings query for representors,
859 * the new ones do not, so we have to add
860 * this code for compatibility issues.
862 master = mlx5_find_master_dev(dev);
864 ifr = (struct ifreq) {
865 .ifr_data = (void *)&edata,
867 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
872 "port %u ioctl(SIOCETHTOOL,"
873 " ETHTOOL_GSET) failed: %s",
874 dev->data->port_id, strerror(rte_errno));
878 link_speed = ethtool_cmd_speed(&edata);
879 if (link_speed == -1)
880 dev_link.link_speed = ETH_SPEED_NUM_NONE;
882 dev_link.link_speed = link_speed;
883 priv->link_speed_capa = 0;
884 if (edata.supported & SUPPORTED_Autoneg)
885 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
886 if (edata.supported & (SUPPORTED_1000baseT_Full |
887 SUPPORTED_1000baseKX_Full))
888 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
889 if (edata.supported & SUPPORTED_10000baseKR_Full)
890 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
891 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
892 SUPPORTED_40000baseCR4_Full |
893 SUPPORTED_40000baseSR4_Full |
894 SUPPORTED_40000baseLR4_Full))
895 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
896 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
897 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
898 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
899 ETH_LINK_SPEED_FIXED);
900 if (((dev_link.link_speed && !dev_link.link_status) ||
901 (!dev_link.link_speed && dev_link.link_status))) {
910 * Retrieve physical link information (unlocked version using new ioctl).
913 * Pointer to Ethernet device structure.
915 * Storage for current link status.
918 * 0 on success, a negative errno value otherwise and rte_errno is set.
921 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
922 struct rte_eth_link *link)
925 struct mlx5_priv *priv = dev->data->dev_private;
926 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
928 struct rte_eth_link dev_link;
929 struct rte_eth_dev *master = NULL;
933 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
935 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
936 dev->data->port_id, strerror(rte_errno));
939 dev_link = (struct rte_eth_link) {
940 .link_status = ((ifr.ifr_flags & IFF_UP) &&
941 (ifr.ifr_flags & IFF_RUNNING)),
943 ifr = (struct ifreq) {
944 .ifr_data = (void *)&gcmd,
946 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
948 if (ret == -ENOTSUP && priv->representor) {
950 * For representors we can try to inherit link
951 * settings from the master device. Actually
952 * link settings do not make a lot of sense
953 * for representors due to missing physical
954 * link. The old kernel drivers supported
955 * emulated settings query for representors,
956 * the new ones do not, so we have to add
957 * this code for compatibility issues.
959 master = mlx5_find_master_dev(dev);
961 ifr = (struct ifreq) {
962 .ifr_data = (void *)&gcmd,
964 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
969 "port %u ioctl(SIOCETHTOOL,"
970 " ETHTOOL_GLINKSETTINGS) failed: %s",
971 dev->data->port_id, strerror(rte_errno));
976 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
978 alignas(struct ethtool_link_settings)
979 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
980 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
981 struct ethtool_link_settings *ecmd = (void *)data;
984 ifr.ifr_data = (void *)ecmd;
985 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
988 "port %u ioctl(SIOCETHTOOL,"
989 "ETHTOOL_GLINKSETTINGS) failed: %s",
990 dev->data->port_id, strerror(rte_errno));
993 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
995 sc = ecmd->link_mode_masks[0] |
996 ((uint64_t)ecmd->link_mode_masks[1] << 32);
997 priv->link_speed_capa = 0;
998 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
999 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
1000 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
1001 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
1002 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
1003 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
1004 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
1005 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
1006 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
1007 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
1008 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
1009 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
1010 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
1011 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1012 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1013 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1014 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1015 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1016 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1017 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1018 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1019 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1020 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1021 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1022 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1023 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1024 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1025 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1026 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1027 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1028 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1029 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1030 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1031 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1032 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1033 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1034 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1035 ETH_LINK_SPEED_FIXED);
1036 if (((dev_link.link_speed && !dev_link.link_status) ||
1037 (!dev_link.link_speed && dev_link.link_status))) {
1046 * DPDK callback to retrieve physical link information.
1049 * Pointer to Ethernet device structure.
1050 * @param wait_to_complete
1051 * Wait for request completion.
1054 * 0 if link status was not updated, positive if it was, a negative errno
1055 * value otherwise and rte_errno is set.
1058 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1061 struct rte_eth_link dev_link;
1062 time_t start_time = time(NULL);
1063 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
1066 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1067 if (ret == -ENOTSUP)
1068 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1071 /* Handle wait to complete situation. */
1072 if ((wait_to_complete || retry) && ret == -EAGAIN) {
1073 if (abs((int)difftime(time(NULL), start_time)) <
1074 MLX5_LINK_STATUS_TIMEOUT) {
1081 } else if (ret < 0) {
1084 } while (wait_to_complete || retry-- > 0);
1085 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1086 sizeof(struct rte_eth_link));
1087 dev->data->dev_link = dev_link;
1092 * DPDK callback to change the MTU.
1095 * Pointer to Ethernet device structure.
1100 * 0 on success, a negative errno value otherwise and rte_errno is set.
1103 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1105 struct mlx5_priv *priv = dev->data->dev_private;
1106 uint16_t kern_mtu = 0;
1109 ret = mlx5_get_mtu(dev, &kern_mtu);
1112 /* Set kernel interface MTU first. */
1113 ret = mlx5_set_mtu(dev, mtu);
1116 ret = mlx5_get_mtu(dev, &kern_mtu);
1119 if (kern_mtu == mtu) {
1121 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1122 dev->data->port_id, mtu);
1130 * DPDK callback to get flow control status.
1133 * Pointer to Ethernet device structure.
1134 * @param[out] fc_conf
1135 * Flow control output buffer.
1138 * 0 on success, a negative errno value otherwise and rte_errno is set.
1141 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1144 struct ethtool_pauseparam ethpause = {
1145 .cmd = ETHTOOL_GPAUSEPARAM
1149 ifr.ifr_data = (void *)ðpause;
1150 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1153 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1155 dev->data->port_id, strerror(rte_errno));
1158 fc_conf->autoneg = ethpause.autoneg;
1159 if (ethpause.rx_pause && ethpause.tx_pause)
1160 fc_conf->mode = RTE_FC_FULL;
1161 else if (ethpause.rx_pause)
1162 fc_conf->mode = RTE_FC_RX_PAUSE;
1163 else if (ethpause.tx_pause)
1164 fc_conf->mode = RTE_FC_TX_PAUSE;
1166 fc_conf->mode = RTE_FC_NONE;
1171 * DPDK callback to modify flow control parameters.
1174 * Pointer to Ethernet device structure.
1175 * @param[in] fc_conf
1176 * Flow control parameters.
1179 * 0 on success, a negative errno value otherwise and rte_errno is set.
1182 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1185 struct ethtool_pauseparam ethpause = {
1186 .cmd = ETHTOOL_SPAUSEPARAM
1190 ifr.ifr_data = (void *)ðpause;
1191 ethpause.autoneg = fc_conf->autoneg;
1192 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1193 (fc_conf->mode & RTE_FC_RX_PAUSE))
1194 ethpause.rx_pause = 1;
1196 ethpause.rx_pause = 0;
1198 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1199 (fc_conf->mode & RTE_FC_TX_PAUSE))
1200 ethpause.tx_pause = 1;
1202 ethpause.tx_pause = 0;
1203 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1206 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1208 dev->data->port_id, strerror(rte_errno));
1215 * Handle asynchronous removal event for entire multiport device.
1218 * Infiniband device shared context.
1221 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1225 for (i = 0; i < sh->max_port; ++i) {
1226 struct rte_eth_dev *dev;
1228 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1230 * Or not existing port either no
1231 * handler installed for this port.
1235 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1237 if (dev->data->dev_conf.intr_conf.rmv)
1238 _rte_eth_dev_callback_process
1239 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1244 * Handle shared asynchronous events the NIC (removal event
1245 * and link status change). Supports multiport IB device.
1248 * Callback argument.
1251 mlx5_dev_interrupt_handler(void *cb_arg)
1253 struct mlx5_ibv_shared *sh = cb_arg;
1254 struct ibv_async_event event;
1256 /* Read all message from the IB device and acknowledge them. */
1258 struct rte_eth_dev *dev;
1261 if (mlx5_glue->get_async_event(sh->ctx, &event))
1263 /* Retrieve and check IB port index. */
1264 tmp = (uint32_t)event.element.port_num;
1265 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1267 * The DEVICE_FATAL event is called once for
1268 * entire device without port specifying.
1269 * We should notify all existing ports.
1271 mlx5_glue->ack_async_event(&event);
1272 mlx5_dev_interrupt_device_fatal(sh);
1275 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
1277 /* Unsupported devive level event. */
1278 mlx5_glue->ack_async_event(&event);
1280 "unsupported common event (type %d)",
1284 if (tmp > sh->max_port) {
1285 /* Invalid IB port index. */
1286 mlx5_glue->ack_async_event(&event);
1288 "cannot handle an event (type %d)"
1289 "due to invalid IB port index (%u)",
1290 event.event_type, tmp);
1293 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1294 /* No handler installed. */
1295 mlx5_glue->ack_async_event(&event);
1297 "cannot handle an event (type %d)"
1298 "due to no handler installed for port %u",
1299 event.event_type, tmp);
1302 /* Retrieve ethernet device descriptor. */
1303 tmp = sh->port[tmp - 1].ih_port_id;
1304 dev = &rte_eth_devices[tmp];
1306 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1307 event.event_type == IBV_EVENT_PORT_ERR) &&
1308 dev->data->dev_conf.intr_conf.lsc) {
1309 mlx5_glue->ack_async_event(&event);
1310 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1314 _rte_eth_dev_callback_process
1315 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1319 "port %u cannot handle an unknown event (type %d)",
1320 dev->data->port_id, event.event_type);
1321 mlx5_glue->ack_async_event(&event);
1326 * Unregister callback handler safely. The handler may be active
1327 * while we are trying to unregister it, in this case code -EAGAIN
1328 * is returned by rte_intr_callback_unregister(). This routine checks
1329 * the return code and tries to unregister handler again.
1334 * pointer to callback routine
1336 * opaque callback parameter
1339 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1340 rte_intr_callback_fn cb_fn, void *cb_arg)
1343 * Try to reduce timeout management overhead by not calling
1344 * the timer related routines on the first iteration. If the
1345 * unregistering succeeds on first call there will be no
1346 * timer calls at all.
1354 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1357 if (ret != -EAGAIN) {
1358 DRV_LOG(INFO, "failed to unregister interrupt"
1359 " handler (error: %d)", ret);
1364 struct timespec onems;
1366 /* Wait one millisecond and try again. */
1368 onems.tv_nsec = NS_PER_S / MS_PER_S;
1369 nanosleep(&onems, 0);
1370 /* Check whether one second elapsed. */
1371 if ((rte_get_timer_cycles() - start) <= twait)
1375 * We get the amount of timer ticks for one second.
1376 * If this amount elapsed it means we spent one
1377 * second in waiting. This branch is executed once
1378 * on first iteration.
1380 twait = rte_get_timer_hz();
1384 * Timeout elapsed, show message (once a second) and retry.
1385 * We have no other acceptable option here, if we ignore
1386 * the unregistering return code the handler will not
1387 * be unregistered, fd will be closed and we may get the
1388 * crush. Hanging and messaging in the loop seems not to be
1391 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1392 start = rte_get_timer_cycles();
1397 * Handle DEVX interrupts from the NIC.
1398 * This function is probably called from the DPDK host thread.
1401 * Callback argument.
1404 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1406 #ifndef HAVE_IBV_DEVX_ASYNC
1410 struct mlx5_ibv_shared *sh = cb_arg;
1412 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1413 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1414 MLX5_ST_SZ_BYTES(traffic_counter) +
1415 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1417 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1419 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1422 mlx5_flow_async_pool_query_handle
1423 (sh, (uint64_t)out.cmd_resp.wr_id,
1424 mlx5_devx_get_out_command_status(buf));
1425 #endif /* HAVE_IBV_DEVX_ASYNC */
1429 * Uninstall shared asynchronous device events handler.
1430 * This function is implemented to support event sharing
1431 * between multiple ports of single IB device.
1434 * Pointer to Ethernet device.
1437 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1439 struct mlx5_priv *priv = dev->data->dev_private;
1440 struct mlx5_ibv_shared *sh = priv->sh;
1442 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1444 pthread_mutex_lock(&sh->intr_mutex);
1445 MLX5_ASSERT(priv->ibv_port);
1446 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1447 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1448 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1450 MLX5_ASSERT(sh->port[priv->ibv_port - 1].ih_port_id ==
1451 (uint32_t)dev->data->port_id);
1452 MLX5_ASSERT(sh->intr_cnt);
1453 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1454 if (!sh->intr_cnt || --sh->intr_cnt)
1456 mlx5_intr_callback_unregister(&sh->intr_handle,
1457 mlx5_dev_interrupt_handler, sh);
1458 sh->intr_handle.fd = 0;
1459 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1461 pthread_mutex_unlock(&sh->intr_mutex);
1465 * Uninstall devx shared asynchronous device events handler.
1466 * This function is implemeted to support event sharing
1467 * between multiple ports of single IB device.
1470 * Pointer to Ethernet device.
1473 mlx5_dev_shared_handler_devx_uninstall(struct rte_eth_dev *dev)
1475 struct mlx5_priv *priv = dev->data->dev_private;
1476 struct mlx5_ibv_shared *sh = priv->sh;
1478 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1480 pthread_mutex_lock(&sh->intr_mutex);
1481 MLX5_ASSERT(priv->ibv_port);
1482 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1483 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1484 if (sh->port[priv->ibv_port - 1].devx_ih_port_id >= RTE_MAX_ETHPORTS)
1486 MLX5_ASSERT(sh->port[priv->ibv_port - 1].devx_ih_port_id ==
1487 (uint32_t)dev->data->port_id);
1488 sh->port[priv->ibv_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
1489 if (!sh->devx_intr_cnt || --sh->devx_intr_cnt)
1491 if (sh->intr_handle_devx.fd) {
1492 rte_intr_callback_unregister(&sh->intr_handle_devx,
1493 mlx5_dev_interrupt_handler_devx,
1495 sh->intr_handle_devx.fd = 0;
1496 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1498 if (sh->devx_comp) {
1499 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1500 sh->devx_comp = NULL;
1503 pthread_mutex_unlock(&sh->intr_mutex);
1507 * Install shared asynchronous device events handler.
1508 * This function is implemented to support event sharing
1509 * between multiple ports of single IB device.
1512 * Pointer to Ethernet device.
1515 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1517 struct mlx5_priv *priv = dev->data->dev_private;
1518 struct mlx5_ibv_shared *sh = priv->sh;
1522 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1524 pthread_mutex_lock(&sh->intr_mutex);
1525 MLX5_ASSERT(priv->ibv_port);
1526 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1527 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1528 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1529 /* The handler is already installed for this port. */
1530 MLX5_ASSERT(sh->intr_cnt);
1534 sh->port[priv->ibv_port - 1].ih_port_id =
1535 (uint32_t)dev->data->port_id;
1539 /* No shared handler installed. */
1540 MLX5_ASSERT(sh->ctx->async_fd > 0);
1541 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1542 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1544 DRV_LOG(INFO, "failed to change file descriptor async event"
1546 /* Indicate there will be no interrupts. */
1547 dev->data->dev_conf.intr_conf.lsc = 0;
1548 dev->data->dev_conf.intr_conf.rmv = 0;
1550 sh->intr_handle.fd = sh->ctx->async_fd;
1551 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1552 rte_intr_callback_register(&sh->intr_handle,
1553 mlx5_dev_interrupt_handler, sh);
1555 sh->port[priv->ibv_port - 1].ih_port_id =
1556 (uint32_t)dev->data->port_id;
1559 pthread_mutex_unlock(&sh->intr_mutex);
1563 * Install devx shared asyncronous device events handler.
1564 * This function is implemeted to support event sharing
1565 * between multiple ports of single IB device.
1568 * Pointer to Ethernet device.
1571 mlx5_dev_shared_handler_devx_install(struct rte_eth_dev *dev)
1573 struct mlx5_priv *priv = dev->data->dev_private;
1574 struct mlx5_ibv_shared *sh = priv->sh;
1576 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1578 pthread_mutex_lock(&sh->intr_mutex);
1579 MLX5_ASSERT(priv->ibv_port);
1580 MLX5_ASSERT(priv->ibv_port <= sh->max_port);
1581 MLX5_ASSERT(dev->data->port_id < RTE_MAX_ETHPORTS);
1582 if (sh->port[priv->ibv_port - 1].devx_ih_port_id < RTE_MAX_ETHPORTS) {
1583 /* The handler is already installed for this port. */
1584 MLX5_ASSERT(sh->devx_intr_cnt);
1587 if (sh->devx_intr_cnt) {
1588 sh->devx_intr_cnt++;
1589 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1590 (uint32_t)dev->data->port_id;
1593 if (priv->config.devx) {
1594 #ifndef HAVE_IBV_DEVX_ASYNC
1597 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1598 if (sh->devx_comp) {
1599 int flags = fcntl(sh->devx_comp->fd, F_GETFL);
1600 int ret = fcntl(sh->devx_comp->fd, F_SETFL,
1601 flags | O_NONBLOCK);
1604 DRV_LOG(INFO, "failed to change file descriptor"
1605 " devx async event queue");
1607 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1608 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1609 rte_intr_callback_register
1610 (&sh->intr_handle_devx,
1611 mlx5_dev_interrupt_handler_devx, sh);
1612 sh->devx_intr_cnt++;
1613 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1614 (uint32_t)dev->data->port_id;
1617 #endif /* HAVE_IBV_DEVX_ASYNC */
1620 pthread_mutex_unlock(&sh->intr_mutex);
1624 * Uninstall interrupt handler.
1627 * Pointer to Ethernet device.
1630 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1632 mlx5_dev_shared_handler_uninstall(dev);
1636 * Install interrupt handler.
1639 * Pointer to Ethernet device.
1642 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1644 mlx5_dev_shared_handler_install(dev);
1648 * Devx uninstall interrupt handler.
1651 * Pointer to Ethernet device.
1654 mlx5_dev_interrupt_handler_devx_uninstall(struct rte_eth_dev *dev)
1656 mlx5_dev_shared_handler_devx_uninstall(dev);
1660 * Devx install interrupt handler.
1663 * Pointer to Ethernet device.
1666 mlx5_dev_interrupt_handler_devx_install(struct rte_eth_dev *dev)
1668 mlx5_dev_shared_handler_devx_install(dev);
1672 * DPDK callback to bring the link DOWN.
1675 * Pointer to Ethernet device structure.
1678 * 0 on success, a negative errno value otherwise and rte_errno is set.
1681 mlx5_set_link_down(struct rte_eth_dev *dev)
1683 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1687 * DPDK callback to bring the link UP.
1690 * Pointer to Ethernet device structure.
1693 * 0 on success, a negative errno value otherwise and rte_errno is set.
1696 mlx5_set_link_up(struct rte_eth_dev *dev)
1698 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1702 * Configure the RX function to use.
1705 * Pointer to private data structure.
1708 * Pointer to selected Rx burst function.
1711 mlx5_select_rx_function(struct rte_eth_dev *dev)
1713 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1715 MLX5_ASSERT(dev != NULL);
1716 if (mlx5_check_vec_rx_support(dev) > 0) {
1717 rx_pkt_burst = mlx5_rx_burst_vec;
1718 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1719 dev->data->port_id);
1720 } else if (mlx5_mprq_enabled(dev)) {
1721 rx_pkt_burst = mlx5_rx_burst_mprq;
1723 return rx_pkt_burst;
1727 * Check if mlx5 device was removed.
1730 * Pointer to Ethernet device structure.
1733 * 1 when device is removed, otherwise 0.
1736 mlx5_is_removed(struct rte_eth_dev *dev)
1738 struct ibv_device_attr device_attr;
1739 struct mlx5_priv *priv = dev->data->dev_private;
1741 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1747 * Get the E-Switch parameters by port id.
1752 * Device port id is valid, skip check. This flag is useful
1753 * when trials are performed from probing and device is not
1754 * flagged as valid yet (in attaching process).
1755 * @param[out] es_domain_id
1756 * E-Switch domain id.
1757 * @param[out] es_port_id
1758 * The port id of the port in the E-Switch.
1761 * pointer to device private data structure containing data needed
1762 * on success, NULL otherwise and rte_errno is set.
1765 mlx5_port_to_eswitch_info(uint16_t port, bool valid)
1767 struct rte_eth_dev *dev;
1768 struct mlx5_priv *priv;
1770 if (port >= RTE_MAX_ETHPORTS) {
1774 if (!valid && !rte_eth_dev_is_valid_port(port)) {
1778 dev = &rte_eth_devices[port];
1779 priv = dev->data->dev_private;
1780 if (!(priv->representor || priv->master)) {
1788 * Get the E-Switch parameters by device instance.
1792 * @param[out] es_domain_id
1793 * E-Switch domain id.
1794 * @param[out] es_port_id
1795 * The port id of the port in the E-Switch.
1798 * pointer to device private data structure containing data needed
1799 * on success, NULL otherwise and rte_errno is set.
1802 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
1804 struct mlx5_priv *priv;
1806 priv = dev->data->dev_private;
1807 if (!(priv->representor || priv->master)) {
1815 * Get switch information associated with network interface.
1818 * Network interface index.
1820 * Switch information object, populated in case of success.
1823 * 0 on success, a negative errno value otherwise and rte_errno is set.
1826 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1828 char ifname[IF_NAMESIZE];
1829 char port_name[IF_NAMESIZE];
1831 struct mlx5_switch_info data = {
1834 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1839 bool port_switch_id_set = false;
1840 bool device_dir = false;
1844 if (!if_indextoname(ifindex, ifname)) {
1849 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1851 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1853 MKSTR(pci_device, "/sys/class/net/%s/device",
1856 file = fopen(phys_port_name, "rb");
1858 ret = fscanf(file, "%s", port_name);
1861 mlx5_translate_port_name(port_name, &data);
1863 file = fopen(phys_switch_id, "rb");
1868 port_switch_id_set =
1869 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1872 dir = opendir(pci_device);
1877 if (port_switch_id_set) {
1878 /* We have some E-Switch configuration. */
1879 mlx5_sysfs_check_switch_info(device_dir, &data);
1882 MLX5_ASSERT(!(data.master && data.representor));
1883 if (data.master && data.representor) {
1884 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1885 " and as representor", ifindex);
1893 * Analyze gathered port parameters via sysfs to recognize master
1894 * and representor devices for E-Switch configuration.
1896 * @param[in] device_dir
1897 * flag of presence of "device" directory under port device key.
1898 * @param[inout] switch_info
1899 * Port information, including port name as a number and port name
1900 * type if recognized
1903 * master and representor flags are set in switch_info according to
1904 * recognized parameters (if any).
1907 mlx5_sysfs_check_switch_info(bool device_dir,
1908 struct mlx5_switch_info *switch_info)
1910 switch (switch_info->name_type) {
1911 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1913 * Name is not recognized, assume the master,
1914 * check the device directory presence.
1916 switch_info->master = device_dir;
1918 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1920 * Name is not set, this assumes the legacy naming
1921 * schema for master, just check if there is
1922 * a device directory.
1924 switch_info->master = device_dir;
1926 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1927 /* New uplink naming schema recognized. */
1928 switch_info->master = 1;
1930 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1931 /* Legacy representors naming schema. */
1932 switch_info->representor = !device_dir;
1934 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1935 /* New representors naming schema. */
1936 switch_info->representor = 1;
1942 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1945 * Pointer to Ethernet device structure.
1946 * @param[out] modinfo
1947 * Storage for plug-in module EEPROM information.
1950 * 0 on success, a negative errno value otherwise and rte_errno is set.
1953 mlx5_get_module_info(struct rte_eth_dev *dev,
1954 struct rte_eth_dev_module_info *modinfo)
1956 struct ethtool_modinfo info = {
1957 .cmd = ETHTOOL_GMODULEINFO,
1959 struct ifreq ifr = (struct ifreq) {
1960 .ifr_data = (void *)&info,
1964 if (!dev || !modinfo) {
1965 DRV_LOG(WARNING, "missing argument, cannot get module info");
1969 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1971 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1972 dev->data->port_id, strerror(rte_errno));
1975 modinfo->type = info.type;
1976 modinfo->eeprom_len = info.eeprom_len;
1981 * DPDK callback to retrieve plug-in module EEPROM data.
1984 * Pointer to Ethernet device structure.
1986 * Storage for plug-in module EEPROM data.
1989 * 0 on success, a negative errno value otherwise and rte_errno is set.
1991 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1992 struct rte_dev_eeprom_info *info)
1994 struct ethtool_eeprom *eeprom;
1998 if (!dev || !info) {
1999 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
2003 eeprom = rte_calloc(__func__, 1,
2004 (sizeof(struct ethtool_eeprom) + info->length), 0);
2006 DRV_LOG(WARNING, "port %u cannot allocate memory for "
2007 "eeprom data", dev->data->port_id);
2011 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2012 eeprom->offset = info->offset;
2013 eeprom->len = info->length;
2014 ifr = (struct ifreq) {
2015 .ifr_data = (void *)eeprom,
2017 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2019 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2020 dev->data->port_id, strerror(rte_errno));
2022 rte_memcpy(info->data, eeprom->data, info->length);
2028 * DPDK callback to retrieve hairpin capabilities.
2031 * Pointer to Ethernet device structure.
2033 * Storage for hairpin capability data.
2036 * 0 on success, a negative errno value otherwise and rte_errno is set.
2038 int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
2039 struct rte_eth_hairpin_cap *cap)
2041 struct mlx5_priv *priv = dev->data->dev_private;
2043 if (priv->sh->devx == 0) {
2044 rte_errno = ENOTSUP;
2047 cap->max_nb_queues = UINT16_MAX;
2048 cap->max_rx_2_tx = 1;
2049 cap->max_tx_2_rx = 1;
2050 cap->max_nb_desc = 8192;