1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_malloc.h>
35 #include <rte_string_fns.h>
36 #include <rte_rwlock.h>
37 #include <rte_cycles.h>
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
81 uint8_t eth_tp_mdix_ctrl;
82 int8_t link_mode_masks_nwords;
84 uint32_t link_mode_masks[];
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
122 * Get master interface name from private structure.
125 * Pointer to Ethernet device.
127 * Interface name output buffer.
130 * 0 on success, a negative errno value otherwise and rte_errno is set.
133 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
143 MKSTR(path, "%s/device/net", ibdev_path);
151 while ((dent = readdir(dir)) != NULL) {
152 char *name = dent->d_name;
154 unsigned int dev_port;
157 if ((name[0] == '.') &&
158 ((name[1] == '\0') ||
159 ((name[1] == '.') && (name[2] == '\0'))))
162 MKSTR(path, "%s/device/net/%s/%s",
164 (dev_type ? "dev_id" : "dev_port"));
166 file = fopen(path, "rb");
171 * Switch to dev_id when dev_port does not exist as
172 * is the case with Linux kernel versions < 3.15.
183 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
188 * Switch to dev_id when dev_port returns the same value for
189 * all ports. May happen when using a MOFED release older than
190 * 3.0 with a Linux kernel >= 3.15.
192 if (dev_port == dev_port_prev)
194 dev_port_prev = dev_port;
196 strlcpy(match, name, sizeof(match));
199 if (match[0] == '\0') {
203 strncpy(*ifname, match, sizeof(*ifname));
208 * Get interface name from private structure.
210 * This is a port representor-aware version of mlx5_get_master_ifname().
213 * Pointer to Ethernet device.
215 * Interface name output buffer.
218 * 0 on success, a negative errno value otherwise and rte_errno is set.
221 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
223 struct mlx5_priv *priv = dev->data->dev_private;
224 unsigned int ifindex;
228 ifindex = mlx5_ifindex(dev);
230 if (!priv->representor)
231 return mlx5_get_master_ifname(priv->sh->ibdev_path,
236 if (if_indextoname(ifindex, &(*ifname)[0]))
243 * Get the interface index from device name.
246 * Pointer to Ethernet device.
249 * Nonzero interface index on success, zero otherwise and rte_errno is set.
252 mlx5_ifindex(const struct rte_eth_dev *dev)
254 struct mlx5_priv *priv = dev->data->dev_private;
255 unsigned int ifindex;
258 assert(priv->if_index);
259 ifindex = priv->if_index;
266 * Perform ifreq ioctl() on associated Ethernet device.
269 * Pointer to Ethernet device.
271 * Request number to pass to ioctl().
273 * Interface request structure output buffer.
276 * 0 on success, a negative errno value otherwise and rte_errno is set.
279 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
281 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
288 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
291 ret = ioctl(sock, req, ifr);
307 * Pointer to Ethernet device.
309 * MTU value output buffer.
312 * 0 on success, a negative errno value otherwise and rte_errno is set.
315 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
317 struct ifreq request;
318 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
322 *mtu = request.ifr_mtu;
330 * Pointer to Ethernet device.
335 * 0 on success, a negative errno value otherwise and rte_errno is set.
338 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
340 struct ifreq request = { .ifr_mtu = mtu, };
342 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
349 * Pointer to Ethernet device.
351 * Bitmask for flags that must remain untouched.
353 * Bitmask for flags to modify.
356 * 0 on success, a negative errno value otherwise and rte_errno is set.
359 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
361 struct ifreq request;
362 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
366 request.ifr_flags &= keep;
367 request.ifr_flags |= flags & ~keep;
368 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
372 * DPDK callback for Ethernet device configuration.
375 * Pointer to Ethernet device structure.
378 * 0 on success, a negative errno value otherwise and rte_errno is set.
381 mlx5_dev_configure(struct rte_eth_dev *dev)
383 struct mlx5_priv *priv = dev->data->dev_private;
384 unsigned int rxqs_n = dev->data->nb_rx_queues;
385 unsigned int txqs_n = dev->data->nb_tx_queues;
386 const uint8_t use_app_rss_key =
387 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
390 if (use_app_rss_key &&
391 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
392 MLX5_RSS_HASH_KEY_LEN)) {
393 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
394 dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
398 priv->rss_conf.rss_key =
399 rte_realloc(priv->rss_conf.rss_key,
400 MLX5_RSS_HASH_KEY_LEN, 0);
401 if (!priv->rss_conf.rss_key) {
402 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
403 dev->data->port_id, rxqs_n);
408 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
410 memcpy(priv->rss_conf.rss_key,
412 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
413 rss_hash_default_key,
414 MLX5_RSS_HASH_KEY_LEN);
415 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
416 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
417 priv->rxqs = (void *)dev->data->rx_queues;
418 priv->txqs = (void *)dev->data->tx_queues;
419 if (txqs_n != priv->txqs_n) {
420 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
421 dev->data->port_id, priv->txqs_n, txqs_n);
422 priv->txqs_n = txqs_n;
424 if (rxqs_n > priv->config.ind_table_max_size) {
425 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
426 dev->data->port_id, rxqs_n);
430 if (rxqs_n != priv->rxqs_n) {
431 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
432 dev->data->port_id, priv->rxqs_n, rxqs_n);
433 priv->rxqs_n = rxqs_n;
435 priv->skip_default_rss_reta = 0;
436 ret = mlx5_proc_priv_init(dev);
443 * Configure default RSS reta.
446 * Pointer to Ethernet device structure.
449 * 0 on success, a negative errno value otherwise and rte_errno is set.
452 mlx5_dev_configure_rss_reta(struct rte_eth_dev *dev)
454 struct mlx5_priv *priv = dev->data->dev_private;
455 unsigned int rxqs_n = dev->data->nb_rx_queues;
458 unsigned int reta_idx_n;
460 unsigned int *rss_queue_arr = NULL;
461 unsigned int rss_queue_n = 0;
463 if (priv->skip_default_rss_reta)
465 rss_queue_arr = rte_malloc("", rxqs_n * sizeof(unsigned int), 0);
466 if (!rss_queue_arr) {
467 DRV_LOG(ERR, "port %u cannot allocate RSS queue list (%u)",
468 dev->data->port_id, rxqs_n);
472 for (i = 0, j = 0; i < rxqs_n; i++) {
473 struct mlx5_rxq_data *rxq_data;
474 struct mlx5_rxq_ctrl *rxq_ctrl;
476 rxq_data = (*priv->rxqs)[i];
477 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
478 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
479 rss_queue_arr[j++] = i;
482 if (rss_queue_n > priv->config.ind_table_max_size) {
483 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
484 dev->data->port_id, rss_queue_n);
486 rte_free(rss_queue_arr);
489 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
490 dev->data->port_id, priv->rxqs_n, rxqs_n);
491 priv->rxqs_n = rxqs_n;
493 * If the requested number of RX queues is not a power of two,
494 * use the maximum indirection table size for better balancing.
495 * The result is always rounded to the next power of two.
497 reta_idx_n = (1 << log2above((rss_queue_n & (rss_queue_n - 1)) ?
498 priv->config.ind_table_max_size :
500 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
502 rte_free(rss_queue_arr);
506 * When the number of RX queues is not a power of two,
507 * the remaining table entries are padded with reused WQs
508 * and hashes are not spread uniformly.
510 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
511 (*priv->reta_idx)[i] = rss_queue_arr[j];
512 if (++j == rss_queue_n)
515 rte_free(rss_queue_arr);
520 * Sets default tuning parameters.
523 * Pointer to Ethernet device.
525 * Info structure output buffer.
528 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
530 struct mlx5_priv *priv = dev->data->dev_private;
532 /* Minimum CPU utilization. */
533 info->default_rxportconf.ring_size = 256;
534 info->default_txportconf.ring_size = 256;
535 info->default_rxportconf.burst_size = 64;
536 info->default_txportconf.burst_size = 64;
537 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
538 info->default_rxportconf.nb_queues = 16;
539 info->default_txportconf.nb_queues = 16;
540 if (dev->data->nb_rx_queues > 2 ||
541 dev->data->nb_tx_queues > 2) {
542 /* Max Throughput. */
543 info->default_rxportconf.ring_size = 2048;
544 info->default_txportconf.ring_size = 2048;
547 info->default_rxportconf.nb_queues = 8;
548 info->default_txportconf.nb_queues = 8;
549 if (dev->data->nb_rx_queues > 2 ||
550 dev->data->nb_tx_queues > 2) {
551 /* Max Throughput. */
552 info->default_rxportconf.ring_size = 4096;
553 info->default_txportconf.ring_size = 4096;
559 * Sets tx mbuf limiting parameters.
562 * Pointer to Ethernet device.
564 * Info structure output buffer.
567 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
569 struct mlx5_priv *priv = dev->data->dev_private;
570 struct mlx5_dev_config *config = &priv->config;
574 inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
575 MLX5_SEND_DEF_INLINE_LEN :
576 (unsigned int)config->txq_inline_max;
577 assert(config->txq_inline_min >= 0);
578 inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
579 inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
580 MLX5_ESEG_MIN_INLINE_SIZE -
583 MLX5_WQE_DSEG_SIZE * 2);
584 nb_max = (MLX5_WQE_SIZE_MAX +
585 MLX5_ESEG_MIN_INLINE_SIZE -
589 inlen) / MLX5_WSEG_SIZE;
590 info->tx_desc_lim.nb_seg_max = nb_max;
591 info->tx_desc_lim.nb_mtu_seg_max = nb_max;
595 * DPDK callback to get information about the device.
598 * Pointer to Ethernet device structure.
600 * Info structure output buffer.
603 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
605 struct mlx5_priv *priv = dev->data->dev_private;
606 struct mlx5_dev_config *config = &priv->config;
609 /* FIXME: we should ask the device for these values. */
610 info->min_rx_bufsize = 32;
611 info->max_rx_pktlen = 65536;
612 info->max_lro_pkt_size = MLX5_MAX_LRO_SIZE;
614 * Since we need one CQ per QP, the limit is the minimum number
615 * between the two values.
617 max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
618 priv->sh->device_attr.orig_attr.max_qp);
619 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
622 info->max_rx_queues = max;
623 info->max_tx_queues = max;
624 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
625 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
626 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
627 info->rx_queue_offload_capa);
628 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
629 info->if_index = mlx5_ifindex(dev);
630 info->reta_size = priv->reta_idx_n ?
631 priv->reta_idx_n : config->ind_table_max_size;
632 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
633 info->speed_capa = priv->link_speed_capa;
634 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
635 mlx5_set_default_params(dev, info);
636 mlx5_set_txlimit_params(dev, info);
637 info->switch_info.name = dev->data->name;
638 info->switch_info.domain_id = priv->domain_id;
639 info->switch_info.port_id = priv->representor_id;
640 if (priv->representor) {
643 if (priv->pf_bond >= 0) {
645 * Switch port ID is opaque value with driver defined
646 * format. Push the PF index in bonding configurations
647 * in upper four bits of port ID. If we get too many
648 * representors (more than 4K) or PFs (more than 15)
649 * this approach must be reconsidered.
651 if ((info->switch_info.port_id >>
652 MLX5_PORT_ID_BONDING_PF_SHIFT) ||
653 priv->pf_bond > MLX5_PORT_ID_BONDING_PF_MASK) {
654 DRV_LOG(ERR, "can't update switch port ID"
655 " for bonding device");
659 info->switch_info.port_id |=
660 priv->pf_bond << MLX5_PORT_ID_BONDING_PF_SHIFT;
662 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
663 struct mlx5_priv *opriv =
664 rte_eth_devices[port_id].data->dev_private;
667 opriv->representor ||
668 opriv->sh != priv->sh ||
669 opriv->domain_id != priv->domain_id)
672 * Override switch name with that of the master
675 info->switch_info.name = opriv->dev_data->name;
683 * Get device current raw clock counter
686 * Pointer to Ethernet device structure.
688 * Current raw clock counter of the device.
691 * 0 if the clock has correctly been read
692 * The value of errno in case of error
695 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
697 struct mlx5_priv *priv = dev->data->dev_private;
698 struct ibv_context *ctx = priv->sh->ctx;
699 struct ibv_values_ex values;
702 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
703 err = mlx5_glue->query_rt_values_ex(ctx, &values);
705 DRV_LOG(WARNING, "Could not query the clock !");
708 *clock = values.raw_clock.tv_nsec;
713 * Get firmware version of a device.
716 * Ethernet device port.
718 * String output allocated by caller.
720 * Size of the output string, including terminating null byte.
723 * 0 on success, or the size of the non truncated string if too big.
725 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
727 struct mlx5_priv *priv = dev->data->dev_private;
728 struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
729 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
734 strlcpy(fw_ver, attr->fw_ver, fw_size);
739 * Get supported packet types.
742 * Pointer to Ethernet device structure.
745 * A pointer to the supported Packet types array.
748 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
750 static const uint32_t ptypes[] = {
751 /* refers to rxq_cq_to_pkt_type() */
753 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
754 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
755 RTE_PTYPE_L4_NONFRAG,
759 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
760 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
761 RTE_PTYPE_INNER_L4_NONFRAG,
762 RTE_PTYPE_INNER_L4_FRAG,
763 RTE_PTYPE_INNER_L4_TCP,
764 RTE_PTYPE_INNER_L4_UDP,
768 if (dev->rx_pkt_burst == mlx5_rx_burst ||
769 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
770 dev->rx_pkt_burst == mlx5_rx_burst_vec)
776 * Retrieve the master device for representor in the same switch domain.
779 * Pointer to representor Ethernet device structure.
782 * Master device structure on success, NULL otherwise.
785 static struct rte_eth_dev *
786 mlx5_find_master_dev(struct rte_eth_dev *dev)
788 struct mlx5_priv *priv;
792 priv = dev->data->dev_private;
793 domain_id = priv->domain_id;
794 assert(priv->representor);
795 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
796 struct mlx5_priv *opriv =
797 rte_eth_devices[port_id].data->dev_private;
800 opriv->domain_id == domain_id &&
801 opriv->sh == priv->sh)
802 return &rte_eth_devices[port_id];
808 * DPDK callback to retrieve physical link information.
811 * Pointer to Ethernet device structure.
813 * Storage for current link status.
816 * 0 on success, a negative errno value otherwise and rte_errno is set.
819 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
820 struct rte_eth_link *link)
822 struct mlx5_priv *priv = dev->data->dev_private;
823 struct ethtool_cmd edata = {
824 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
827 struct rte_eth_link dev_link;
831 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
833 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
834 dev->data->port_id, strerror(rte_errno));
837 dev_link = (struct rte_eth_link) {
838 .link_status = ((ifr.ifr_flags & IFF_UP) &&
839 (ifr.ifr_flags & IFF_RUNNING)),
841 ifr = (struct ifreq) {
842 .ifr_data = (void *)&edata,
844 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
846 if (ret == -ENOTSUP && priv->representor) {
847 struct rte_eth_dev *master;
850 * For representors we can try to inherit link
851 * settings from the master device. Actually
852 * link settings do not make a lot of sense
853 * for representors due to missing physical
854 * link. The old kernel drivers supported
855 * emulated settings query for representors,
856 * the new ones do not, so we have to add
857 * this code for compatibility issues.
859 master = mlx5_find_master_dev(dev);
861 ifr = (struct ifreq) {
862 .ifr_data = (void *)&edata,
864 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
869 "port %u ioctl(SIOCETHTOOL,"
870 " ETHTOOL_GSET) failed: %s",
871 dev->data->port_id, strerror(rte_errno));
875 link_speed = ethtool_cmd_speed(&edata);
876 if (link_speed == -1)
877 dev_link.link_speed = ETH_SPEED_NUM_NONE;
879 dev_link.link_speed = link_speed;
880 priv->link_speed_capa = 0;
881 if (edata.supported & SUPPORTED_Autoneg)
882 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
883 if (edata.supported & (SUPPORTED_1000baseT_Full |
884 SUPPORTED_1000baseKX_Full))
885 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
886 if (edata.supported & SUPPORTED_10000baseKR_Full)
887 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
888 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
889 SUPPORTED_40000baseCR4_Full |
890 SUPPORTED_40000baseSR4_Full |
891 SUPPORTED_40000baseLR4_Full))
892 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
893 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
894 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
895 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
896 ETH_LINK_SPEED_FIXED);
897 if (((dev_link.link_speed && !dev_link.link_status) ||
898 (!dev_link.link_speed && dev_link.link_status))) {
907 * Retrieve physical link information (unlocked version using new ioctl).
910 * Pointer to Ethernet device structure.
912 * Storage for current link status.
915 * 0 on success, a negative errno value otherwise and rte_errno is set.
918 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
919 struct rte_eth_link *link)
922 struct mlx5_priv *priv = dev->data->dev_private;
923 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
925 struct rte_eth_link dev_link;
926 struct rte_eth_dev *master = NULL;
930 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
932 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
933 dev->data->port_id, strerror(rte_errno));
936 dev_link = (struct rte_eth_link) {
937 .link_status = ((ifr.ifr_flags & IFF_UP) &&
938 (ifr.ifr_flags & IFF_RUNNING)),
940 ifr = (struct ifreq) {
941 .ifr_data = (void *)&gcmd,
943 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
945 if (ret == -ENOTSUP && priv->representor) {
947 * For representors we can try to inherit link
948 * settings from the master device. Actually
949 * link settings do not make a lot of sense
950 * for representors due to missing physical
951 * link. The old kernel drivers supported
952 * emulated settings query for representors,
953 * the new ones do not, so we have to add
954 * this code for compatibility issues.
956 master = mlx5_find_master_dev(dev);
958 ifr = (struct ifreq) {
959 .ifr_data = (void *)&gcmd,
961 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
966 "port %u ioctl(SIOCETHTOOL,"
967 " ETHTOOL_GLINKSETTINGS) failed: %s",
968 dev->data->port_id, strerror(rte_errno));
973 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
975 alignas(struct ethtool_link_settings)
976 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
977 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
978 struct ethtool_link_settings *ecmd = (void *)data;
981 ifr.ifr_data = (void *)ecmd;
982 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
985 "port %u ioctl(SIOCETHTOOL,"
986 "ETHTOOL_GLINKSETTINGS) failed: %s",
987 dev->data->port_id, strerror(rte_errno));
990 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
992 sc = ecmd->link_mode_masks[0] |
993 ((uint64_t)ecmd->link_mode_masks[1] << 32);
994 priv->link_speed_capa = 0;
995 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
996 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
997 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
998 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
999 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
1000 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
1001 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
1002 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
1003 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
1004 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
1005 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
1006 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
1007 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
1008 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1009 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1010 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1011 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1012 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1013 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1014 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1015 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1016 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1017 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1018 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1019 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1020 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1021 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1022 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1023 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1024 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1025 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1026 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1027 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1028 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1029 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1030 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1031 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1032 ETH_LINK_SPEED_FIXED);
1033 if (((dev_link.link_speed && !dev_link.link_status) ||
1034 (!dev_link.link_speed && dev_link.link_status))) {
1043 * DPDK callback to retrieve physical link information.
1046 * Pointer to Ethernet device structure.
1047 * @param wait_to_complete
1048 * Wait for request completion.
1051 * 0 if link status was not updated, positive if it was, a negative errno
1052 * value otherwise and rte_errno is set.
1055 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1058 struct rte_eth_link dev_link;
1059 time_t start_time = time(NULL);
1060 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
1063 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1064 if (ret == -ENOTSUP)
1065 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1068 /* Handle wait to complete situation. */
1069 if ((wait_to_complete || retry) && ret == -EAGAIN) {
1070 if (abs((int)difftime(time(NULL), start_time)) <
1071 MLX5_LINK_STATUS_TIMEOUT) {
1078 } else if (ret < 0) {
1081 } while (wait_to_complete || retry-- > 0);
1082 ret = !!memcmp(&dev->data->dev_link, &dev_link,
1083 sizeof(struct rte_eth_link));
1084 dev->data->dev_link = dev_link;
1089 * DPDK callback to change the MTU.
1092 * Pointer to Ethernet device structure.
1097 * 0 on success, a negative errno value otherwise and rte_errno is set.
1100 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1102 struct mlx5_priv *priv = dev->data->dev_private;
1103 uint16_t kern_mtu = 0;
1106 ret = mlx5_get_mtu(dev, &kern_mtu);
1109 /* Set kernel interface MTU first. */
1110 ret = mlx5_set_mtu(dev, mtu);
1113 ret = mlx5_get_mtu(dev, &kern_mtu);
1116 if (kern_mtu == mtu) {
1118 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1119 dev->data->port_id, mtu);
1127 * DPDK callback to get flow control status.
1130 * Pointer to Ethernet device structure.
1131 * @param[out] fc_conf
1132 * Flow control output buffer.
1135 * 0 on success, a negative errno value otherwise and rte_errno is set.
1138 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1141 struct ethtool_pauseparam ethpause = {
1142 .cmd = ETHTOOL_GPAUSEPARAM
1146 ifr.ifr_data = (void *)ðpause;
1147 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1150 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1152 dev->data->port_id, strerror(rte_errno));
1155 fc_conf->autoneg = ethpause.autoneg;
1156 if (ethpause.rx_pause && ethpause.tx_pause)
1157 fc_conf->mode = RTE_FC_FULL;
1158 else if (ethpause.rx_pause)
1159 fc_conf->mode = RTE_FC_RX_PAUSE;
1160 else if (ethpause.tx_pause)
1161 fc_conf->mode = RTE_FC_TX_PAUSE;
1163 fc_conf->mode = RTE_FC_NONE;
1168 * DPDK callback to modify flow control parameters.
1171 * Pointer to Ethernet device structure.
1172 * @param[in] fc_conf
1173 * Flow control parameters.
1176 * 0 on success, a negative errno value otherwise and rte_errno is set.
1179 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1182 struct ethtool_pauseparam ethpause = {
1183 .cmd = ETHTOOL_SPAUSEPARAM
1187 ifr.ifr_data = (void *)ðpause;
1188 ethpause.autoneg = fc_conf->autoneg;
1189 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1190 (fc_conf->mode & RTE_FC_RX_PAUSE))
1191 ethpause.rx_pause = 1;
1193 ethpause.rx_pause = 0;
1195 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1196 (fc_conf->mode & RTE_FC_TX_PAUSE))
1197 ethpause.tx_pause = 1;
1199 ethpause.tx_pause = 0;
1200 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1203 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1205 dev->data->port_id, strerror(rte_errno));
1212 * Get PCI information by sysfs device path.
1215 * Pointer to device sysfs folder name.
1216 * @param[out] pci_addr
1217 * PCI bus address output buffer.
1220 * 0 on success, a negative errno value otherwise and rte_errno is set.
1223 mlx5_dev_to_pci_addr(const char *dev_path,
1224 struct rte_pci_addr *pci_addr)
1228 MKSTR(path, "%s/device/uevent", dev_path);
1230 file = fopen(path, "rb");
1235 while (fgets(line, sizeof(line), file) == line) {
1236 size_t len = strlen(line);
1239 /* Truncate long lines. */
1240 if (len == (sizeof(line) - 1))
1241 while (line[(len - 1)] != '\n') {
1245 line[(len - 1)] = ret;
1247 /* Extract information. */
1250 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1254 &pci_addr->function) == 4) {
1264 * Handle asynchronous removal event for entire multiport device.
1267 * Infiniband device shared context.
1270 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1274 for (i = 0; i < sh->max_port; ++i) {
1275 struct rte_eth_dev *dev;
1277 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1279 * Or not existing port either no
1280 * handler installed for this port.
1284 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1286 if (dev->data->dev_conf.intr_conf.rmv)
1287 _rte_eth_dev_callback_process
1288 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1293 * Handle shared asynchronous events the NIC (removal event
1294 * and link status change). Supports multiport IB device.
1297 * Callback argument.
1300 mlx5_dev_interrupt_handler(void *cb_arg)
1302 struct mlx5_ibv_shared *sh = cb_arg;
1303 struct ibv_async_event event;
1305 /* Read all message from the IB device and acknowledge them. */
1307 struct rte_eth_dev *dev;
1310 if (mlx5_glue->get_async_event(sh->ctx, &event))
1312 /* Retrieve and check IB port index. */
1313 tmp = (uint32_t)event.element.port_num;
1314 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1316 * The DEVICE_FATAL event is called once for
1317 * entire device without port specifying.
1318 * We should notify all existing ports.
1320 mlx5_glue->ack_async_event(&event);
1321 mlx5_dev_interrupt_device_fatal(sh);
1324 assert(tmp && (tmp <= sh->max_port));
1326 /* Unsupported devive level event. */
1327 mlx5_glue->ack_async_event(&event);
1329 "unsupported common event (type %d)",
1333 if (tmp > sh->max_port) {
1334 /* Invalid IB port index. */
1335 mlx5_glue->ack_async_event(&event);
1337 "cannot handle an event (type %d)"
1338 "due to invalid IB port index (%u)",
1339 event.event_type, tmp);
1342 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1343 /* No handler installed. */
1344 mlx5_glue->ack_async_event(&event);
1346 "cannot handle an event (type %d)"
1347 "due to no handler installed for port %u",
1348 event.event_type, tmp);
1351 /* Retrieve ethernet device descriptor. */
1352 tmp = sh->port[tmp - 1].ih_port_id;
1353 dev = &rte_eth_devices[tmp];
1355 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1356 event.event_type == IBV_EVENT_PORT_ERR) &&
1357 dev->data->dev_conf.intr_conf.lsc) {
1358 mlx5_glue->ack_async_event(&event);
1359 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1363 _rte_eth_dev_callback_process
1364 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1368 "port %u cannot handle an unknown event (type %d)",
1369 dev->data->port_id, event.event_type);
1370 mlx5_glue->ack_async_event(&event);
1375 * Unregister callback handler safely. The handler may be active
1376 * while we are trying to unregister it, in this case code -EAGAIN
1377 * is returned by rte_intr_callback_unregister(). This routine checks
1378 * the return code and tries to unregister handler again.
1383 * pointer to callback routine
1385 * opaque callback parameter
1388 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1389 rte_intr_callback_fn cb_fn, void *cb_arg)
1392 * Try to reduce timeout management overhead by not calling
1393 * the timer related routines on the first iteration. If the
1394 * unregistering succeeds on first call there will be no
1395 * timer calls at all.
1403 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1406 if (ret != -EAGAIN) {
1407 DRV_LOG(INFO, "failed to unregister interrupt"
1408 " handler (error: %d)", ret);
1413 struct timespec onems;
1415 /* Wait one millisecond and try again. */
1417 onems.tv_nsec = NS_PER_S / MS_PER_S;
1418 nanosleep(&onems, 0);
1419 /* Check whether one second elapsed. */
1420 if ((rte_get_timer_cycles() - start) <= twait)
1424 * We get the amount of timer ticks for one second.
1425 * If this amount elapsed it means we spent one
1426 * second in waiting. This branch is executed once
1427 * on first iteration.
1429 twait = rte_get_timer_hz();
1433 * Timeout elapsed, show message (once a second) and retry.
1434 * We have no other acceptable option here, if we ignore
1435 * the unregistering return code the handler will not
1436 * be unregistered, fd will be closed and we may get the
1437 * crush. Hanging and messaging in the loop seems not to be
1440 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1441 start = rte_get_timer_cycles();
1446 * Handle DEVX interrupts from the NIC.
1447 * This function is probably called from the DPDK host thread.
1450 * Callback argument.
1453 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1455 #ifndef HAVE_IBV_DEVX_ASYNC
1459 struct mlx5_ibv_shared *sh = cb_arg;
1461 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1462 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1463 MLX5_ST_SZ_BYTES(traffic_counter) +
1464 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1466 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1468 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1471 mlx5_flow_async_pool_query_handle
1472 (sh, (uint64_t)out.cmd_resp.wr_id,
1473 mlx5_devx_get_out_command_status(buf));
1474 #endif /* HAVE_IBV_DEVX_ASYNC */
1478 * Uninstall shared asynchronous device events handler.
1479 * This function is implemented to support event sharing
1480 * between multiple ports of single IB device.
1483 * Pointer to Ethernet device.
1486 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1488 struct mlx5_priv *priv = dev->data->dev_private;
1489 struct mlx5_ibv_shared *sh = priv->sh;
1491 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1493 pthread_mutex_lock(&sh->intr_mutex);
1494 assert(priv->ibv_port);
1495 assert(priv->ibv_port <= sh->max_port);
1496 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1497 if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1499 assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1500 (uint32_t)dev->data->port_id);
1501 assert(sh->intr_cnt);
1502 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1503 if (!sh->intr_cnt || --sh->intr_cnt)
1505 mlx5_intr_callback_unregister(&sh->intr_handle,
1506 mlx5_dev_interrupt_handler, sh);
1507 sh->intr_handle.fd = 0;
1508 sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1510 pthread_mutex_unlock(&sh->intr_mutex);
1514 * Uninstall devx shared asynchronous device events handler.
1515 * This function is implemeted to support event sharing
1516 * between multiple ports of single IB device.
1519 * Pointer to Ethernet device.
1522 mlx5_dev_shared_handler_devx_uninstall(struct rte_eth_dev *dev)
1524 struct mlx5_priv *priv = dev->data->dev_private;
1525 struct mlx5_ibv_shared *sh = priv->sh;
1527 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1529 pthread_mutex_lock(&sh->intr_mutex);
1530 assert(priv->ibv_port);
1531 assert(priv->ibv_port <= sh->max_port);
1532 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1533 if (sh->port[priv->ibv_port - 1].devx_ih_port_id >= RTE_MAX_ETHPORTS)
1535 assert(sh->port[priv->ibv_port - 1].devx_ih_port_id ==
1536 (uint32_t)dev->data->port_id);
1537 sh->port[priv->ibv_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
1538 if (!sh->devx_intr_cnt || --sh->devx_intr_cnt)
1540 if (sh->intr_handle_devx.fd) {
1541 rte_intr_callback_unregister(&sh->intr_handle_devx,
1542 mlx5_dev_interrupt_handler_devx,
1544 sh->intr_handle_devx.fd = 0;
1545 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1547 if (sh->devx_comp) {
1548 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1549 sh->devx_comp = NULL;
1552 pthread_mutex_unlock(&sh->intr_mutex);
1556 * Install shared asynchronous device events handler.
1557 * This function is implemented to support event sharing
1558 * between multiple ports of single IB device.
1561 * Pointer to Ethernet device.
1564 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1566 struct mlx5_priv *priv = dev->data->dev_private;
1567 struct mlx5_ibv_shared *sh = priv->sh;
1571 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1573 pthread_mutex_lock(&sh->intr_mutex);
1574 assert(priv->ibv_port);
1575 assert(priv->ibv_port <= sh->max_port);
1576 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1577 if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1578 /* The handler is already installed for this port. */
1579 assert(sh->intr_cnt);
1583 sh->port[priv->ibv_port - 1].ih_port_id =
1584 (uint32_t)dev->data->port_id;
1588 /* No shared handler installed. */
1589 assert(sh->ctx->async_fd > 0);
1590 flags = fcntl(sh->ctx->async_fd, F_GETFL);
1591 ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1593 DRV_LOG(INFO, "failed to change file descriptor async event"
1595 /* Indicate there will be no interrupts. */
1596 dev->data->dev_conf.intr_conf.lsc = 0;
1597 dev->data->dev_conf.intr_conf.rmv = 0;
1599 sh->intr_handle.fd = sh->ctx->async_fd;
1600 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1601 rte_intr_callback_register(&sh->intr_handle,
1602 mlx5_dev_interrupt_handler, sh);
1604 sh->port[priv->ibv_port - 1].ih_port_id =
1605 (uint32_t)dev->data->port_id;
1608 pthread_mutex_unlock(&sh->intr_mutex);
1612 * Install devx shared asyncronous device events handler.
1613 * This function is implemeted to support event sharing
1614 * between multiple ports of single IB device.
1617 * Pointer to Ethernet device.
1620 mlx5_dev_shared_handler_devx_install(struct rte_eth_dev *dev)
1622 struct mlx5_priv *priv = dev->data->dev_private;
1623 struct mlx5_ibv_shared *sh = priv->sh;
1625 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1627 pthread_mutex_lock(&sh->intr_mutex);
1628 assert(priv->ibv_port);
1629 assert(priv->ibv_port <= sh->max_port);
1630 assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1631 if (sh->port[priv->ibv_port - 1].devx_ih_port_id < RTE_MAX_ETHPORTS) {
1632 /* The handler is already installed for this port. */
1633 assert(sh->devx_intr_cnt);
1636 if (sh->devx_intr_cnt) {
1637 sh->devx_intr_cnt++;
1638 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1639 (uint32_t)dev->data->port_id;
1642 if (priv->config.devx) {
1643 #ifndef HAVE_IBV_DEVX_ASYNC
1646 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1647 if (sh->devx_comp) {
1648 int flags = fcntl(sh->devx_comp->fd, F_GETFL);
1649 int ret = fcntl(sh->devx_comp->fd, F_SETFL,
1650 flags | O_NONBLOCK);
1653 DRV_LOG(INFO, "failed to change file descriptor"
1654 " devx async event queue");
1656 sh->intr_handle_devx.fd = sh->devx_comp->fd;
1657 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1658 rte_intr_callback_register
1659 (&sh->intr_handle_devx,
1660 mlx5_dev_interrupt_handler_devx, sh);
1661 sh->devx_intr_cnt++;
1662 sh->port[priv->ibv_port - 1].devx_ih_port_id =
1663 (uint32_t)dev->data->port_id;
1666 #endif /* HAVE_IBV_DEVX_ASYNC */
1669 pthread_mutex_unlock(&sh->intr_mutex);
1673 * Uninstall interrupt handler.
1676 * Pointer to Ethernet device.
1679 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1681 mlx5_dev_shared_handler_uninstall(dev);
1685 * Install interrupt handler.
1688 * Pointer to Ethernet device.
1691 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1693 mlx5_dev_shared_handler_install(dev);
1697 * Devx uninstall interrupt handler.
1700 * Pointer to Ethernet device.
1703 mlx5_dev_interrupt_handler_devx_uninstall(struct rte_eth_dev *dev)
1705 mlx5_dev_shared_handler_devx_uninstall(dev);
1709 * Devx install interrupt handler.
1712 * Pointer to Ethernet device.
1715 mlx5_dev_interrupt_handler_devx_install(struct rte_eth_dev *dev)
1717 mlx5_dev_shared_handler_devx_install(dev);
1721 * DPDK callback to bring the link DOWN.
1724 * Pointer to Ethernet device structure.
1727 * 0 on success, a negative errno value otherwise and rte_errno is set.
1730 mlx5_set_link_down(struct rte_eth_dev *dev)
1732 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1736 * DPDK callback to bring the link UP.
1739 * Pointer to Ethernet device structure.
1742 * 0 on success, a negative errno value otherwise and rte_errno is set.
1745 mlx5_set_link_up(struct rte_eth_dev *dev)
1747 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1751 * Configure the RX function to use.
1754 * Pointer to private data structure.
1757 * Pointer to selected Rx burst function.
1760 mlx5_select_rx_function(struct rte_eth_dev *dev)
1762 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1764 assert(dev != NULL);
1765 if (mlx5_check_vec_rx_support(dev) > 0) {
1766 rx_pkt_burst = mlx5_rx_burst_vec;
1767 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1768 dev->data->port_id);
1769 } else if (mlx5_mprq_enabled(dev)) {
1770 rx_pkt_burst = mlx5_rx_burst_mprq;
1772 return rx_pkt_burst;
1776 * Check if mlx5 device was removed.
1779 * Pointer to Ethernet device structure.
1782 * 1 when device is removed, otherwise 0.
1785 mlx5_is_removed(struct rte_eth_dev *dev)
1787 struct ibv_device_attr device_attr;
1788 struct mlx5_priv *priv = dev->data->dev_private;
1790 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1796 * Get the E-Switch parameters by port id.
1801 * Device port id is valid, skip check. This flag is useful
1802 * when trials are performed from probing and device is not
1803 * flagged as valid yet (in attaching process).
1804 * @param[out] es_domain_id
1805 * E-Switch domain id.
1806 * @param[out] es_port_id
1807 * The port id of the port in the E-Switch.
1810 * pointer to device private data structure containing data needed
1811 * on success, NULL otherwise and rte_errno is set.
1814 mlx5_port_to_eswitch_info(uint16_t port, bool valid)
1816 struct rte_eth_dev *dev;
1817 struct mlx5_priv *priv;
1819 if (port >= RTE_MAX_ETHPORTS) {
1823 if (!valid && !rte_eth_dev_is_valid_port(port)) {
1827 dev = &rte_eth_devices[port];
1828 priv = dev->data->dev_private;
1829 if (!(priv->representor || priv->master)) {
1837 * Get the E-Switch parameters by device instance.
1841 * @param[out] es_domain_id
1842 * E-Switch domain id.
1843 * @param[out] es_port_id
1844 * The port id of the port in the E-Switch.
1847 * pointer to device private data structure containing data needed
1848 * on success, NULL otherwise and rte_errno is set.
1851 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
1853 struct mlx5_priv *priv;
1855 priv = dev->data->dev_private;
1856 if (!(priv->representor || priv->master)) {
1864 * Get switch information associated with network interface.
1867 * Network interface index.
1869 * Switch information object, populated in case of success.
1872 * 0 on success, a negative errno value otherwise and rte_errno is set.
1875 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1877 char ifname[IF_NAMESIZE];
1878 char port_name[IF_NAMESIZE];
1880 struct mlx5_switch_info data = {
1883 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1888 bool port_switch_id_set = false;
1889 bool device_dir = false;
1893 if (!if_indextoname(ifindex, ifname)) {
1898 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1900 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1902 MKSTR(pci_device, "/sys/class/net/%s/device",
1905 file = fopen(phys_port_name, "rb");
1907 ret = fscanf(file, "%s", port_name);
1910 mlx5_translate_port_name(port_name, &data);
1912 file = fopen(phys_switch_id, "rb");
1917 port_switch_id_set =
1918 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1921 dir = opendir(pci_device);
1926 if (port_switch_id_set) {
1927 /* We have some E-Switch configuration. */
1928 mlx5_sysfs_check_switch_info(device_dir, &data);
1931 assert(!(data.master && data.representor));
1932 if (data.master && data.representor) {
1933 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1934 " and as representor", ifindex);
1942 * Analyze gathered port parameters via Netlink to recognize master
1943 * and representor devices for E-Switch configuration.
1945 * @param[in] num_vf_set
1946 * flag of presence of number of VFs port attribute.
1947 * @param[inout] switch_info
1948 * Port information, including port name as a number and port name
1949 * type if recognized
1952 * master and representor flags are set in switch_info according to
1953 * recognized parameters (if any).
1956 mlx5_nl_check_switch_info(bool num_vf_set,
1957 struct mlx5_switch_info *switch_info)
1959 switch (switch_info->name_type) {
1960 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1962 * Name is not recognized, assume the master,
1963 * check the number of VFs key presence.
1965 switch_info->master = num_vf_set;
1967 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1969 * Name is not set, this assumes the legacy naming
1970 * schema for master, just check if there is a
1971 * number of VFs key.
1973 switch_info->master = num_vf_set;
1975 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1976 /* New uplink naming schema recognized. */
1977 switch_info->master = 1;
1979 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1980 /* Legacy representors naming schema. */
1981 switch_info->representor = !num_vf_set;
1983 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1984 /* New representors naming schema. */
1985 switch_info->representor = 1;
1991 * Analyze gathered port parameters via sysfs to recognize master
1992 * and representor devices for E-Switch configuration.
1994 * @param[in] device_dir
1995 * flag of presence of "device" directory under port device key.
1996 * @param[inout] switch_info
1997 * Port information, including port name as a number and port name
1998 * type if recognized
2001 * master and representor flags are set in switch_info according to
2002 * recognized parameters (if any).
2005 mlx5_sysfs_check_switch_info(bool device_dir,
2006 struct mlx5_switch_info *switch_info)
2008 switch (switch_info->name_type) {
2009 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
2011 * Name is not recognized, assume the master,
2012 * check the device directory presence.
2014 switch_info->master = device_dir;
2016 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
2018 * Name is not set, this assumes the legacy naming
2019 * schema for master, just check if there is
2020 * a device directory.
2022 switch_info->master = device_dir;
2024 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
2025 /* New uplink naming schema recognized. */
2026 switch_info->master = 1;
2028 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
2029 /* Legacy representors naming schema. */
2030 switch_info->representor = !device_dir;
2032 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2033 /* New representors naming schema. */
2034 switch_info->representor = 1;
2040 * Extract port name, as a number, from sysfs or netlink information.
2042 * @param[in] port_name_in
2043 * String representing the port name.
2044 * @param[out] port_info_out
2045 * Port information, including port name as a number and port name
2046 * type if recognized
2049 * port_name field set according to recognized name format.
2052 mlx5_translate_port_name(const char *port_name_in,
2053 struct mlx5_switch_info *port_info_out)
2055 char pf_c1, pf_c2, vf_c1, vf_c2;
2060 * Check for port-name as a string of the form pf0vf0
2061 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
2063 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
2064 &pf_c1, &pf_c2, &port_info_out->pf_num,
2065 &vf_c1, &vf_c2, &port_info_out->port_name);
2066 if (sc_items == 6 &&
2067 pf_c1 == 'p' && pf_c2 == 'f' &&
2068 vf_c1 == 'v' && vf_c2 == 'f') {
2069 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
2073 * Check for port-name as a string of the form p0
2074 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
2076 sc_items = sscanf(port_name_in, "%c%d",
2077 &pf_c1, &port_info_out->port_name);
2078 if (sc_items == 2 && pf_c1 == 'p') {
2079 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2082 /* Check for port-name as a number (support kernel ver < 5.0 */
2084 port_info_out->port_name = strtol(port_name_in, &end, 0);
2086 (size_t)(end - port_name_in) == strlen(port_name_in)) {
2087 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
2090 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
2095 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
2098 * Pointer to Ethernet device structure.
2099 * @param[out] modinfo
2100 * Storage for plug-in module EEPROM information.
2103 * 0 on success, a negative errno value otherwise and rte_errno is set.
2106 mlx5_get_module_info(struct rte_eth_dev *dev,
2107 struct rte_eth_dev_module_info *modinfo)
2109 struct ethtool_modinfo info = {
2110 .cmd = ETHTOOL_GMODULEINFO,
2112 struct ifreq ifr = (struct ifreq) {
2113 .ifr_data = (void *)&info,
2117 if (!dev || !modinfo) {
2118 DRV_LOG(WARNING, "missing argument, cannot get module info");
2122 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2124 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2125 dev->data->port_id, strerror(rte_errno));
2128 modinfo->type = info.type;
2129 modinfo->eeprom_len = info.eeprom_len;
2134 * DPDK callback to retrieve plug-in module EEPROM data.
2137 * Pointer to Ethernet device structure.
2139 * Storage for plug-in module EEPROM data.
2142 * 0 on success, a negative errno value otherwise and rte_errno is set.
2144 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
2145 struct rte_dev_eeprom_info *info)
2147 struct ethtool_eeprom *eeprom;
2151 if (!dev || !info) {
2152 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
2156 eeprom = rte_calloc(__func__, 1,
2157 (sizeof(struct ethtool_eeprom) + info->length), 0);
2159 DRV_LOG(WARNING, "port %u cannot allocate memory for "
2160 "eeprom data", dev->data->port_id);
2164 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2165 eeprom->offset = info->offset;
2166 eeprom->len = info->length;
2167 ifr = (struct ifreq) {
2168 .ifr_data = (void *)eeprom,
2170 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2172 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2173 dev->data->port_id, strerror(rte_errno));
2175 rte_memcpy(info->data, eeprom->data, info->length);
2181 * DPDK callback to retrieve hairpin capabilities.
2184 * Pointer to Ethernet device structure.
2186 * Storage for hairpin capability data.
2189 * 0 on success, a negative errno value otherwise and rte_errno is set.
2191 int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
2192 struct rte_eth_hairpin_cap *cap)
2194 struct mlx5_priv *priv = dev->data->dev_private;
2196 if (priv->sh->devx == 0) {
2197 rte_errno = ENOTSUP;
2200 cap->max_nb_queues = UINT16_MAX;
2201 cap->max_rx_2_tx = 1;
2202 cap->max_tx_2_rx = 1;
2203 cap->max_nb_desc = 8192;