1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <linux/ethtool.h>
23 #include <linux/sockios.h>
29 #include <rte_atomic.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_bus_pci.h>
33 #include <rte_common.h>
34 #include <rte_interrupts.h>
35 #include <rte_malloc.h>
36 #include <rte_string_fns.h>
37 #include <rte_rwlock.h>
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
81 uint8_t eth_tp_mdix_ctrl;
82 int8_t link_mode_masks_nwords;
84 uint32_t link_mode_masks[];
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
122 * Get master interface name from private structure.
125 * Pointer to Ethernet device.
127 * Interface name output buffer.
130 * 0 on success, a negative errno value otherwise and rte_errno is set.
133 mlx5_get_master_ifname(const struct rte_eth_dev *dev,
134 char (*ifname)[IF_NAMESIZE])
136 struct priv *priv = dev->data->dev_private;
139 unsigned int dev_type = 0;
140 unsigned int dev_port_prev = ~0u;
141 char match[IF_NAMESIZE] = "";
144 MKSTR(path, "%s/device/net", priv->ibdev_path);
152 while ((dent = readdir(dir)) != NULL) {
153 char *name = dent->d_name;
155 unsigned int dev_port;
158 if ((name[0] == '.') &&
159 ((name[1] == '\0') ||
160 ((name[1] == '.') && (name[2] == '\0'))))
163 MKSTR(path, "%s/device/net/%s/%s",
164 priv->ibdev_path, name,
165 (dev_type ? "dev_id" : "dev_port"));
167 file = fopen(path, "rb");
172 * Switch to dev_id when dev_port does not exist as
173 * is the case with Linux kernel versions < 3.15.
184 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
189 * Switch to dev_id when dev_port returns the same value for
190 * all ports. May happen when using a MOFED release older than
191 * 3.0 with a Linux kernel >= 3.15.
193 if (dev_port == dev_port_prev)
195 dev_port_prev = dev_port;
197 strlcpy(match, name, sizeof(match));
200 if (match[0] == '\0') {
204 strncpy(*ifname, match, sizeof(*ifname));
209 * Get interface name from private structure.
211 * This is a port representor-aware version of mlx5_get_master_ifname().
214 * Pointer to Ethernet device.
216 * Interface name output buffer.
219 * 0 on success, a negative errno value otherwise and rte_errno is set.
222 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
224 struct priv *priv = dev->data->dev_private;
225 unsigned int ifindex =
226 priv->nl_socket_rdma >= 0 ?
227 mlx5_nl_ifindex(priv->nl_socket_rdma, priv->ibdev_name) : 0;
230 if (!priv->representor)
231 return mlx5_get_master_ifname(dev, ifname);
235 if (if_indextoname(ifindex, &(*ifname)[0]))
242 * Get the interface index from device name.
245 * Pointer to Ethernet device.
248 * Nonzero interface index on success, zero otherwise and rte_errno is set.
251 mlx5_ifindex(const struct rte_eth_dev *dev)
253 char ifname[IF_NAMESIZE];
254 unsigned int ifindex;
256 if (mlx5_get_ifname(dev, &ifname))
258 ifindex = if_nametoindex(ifname);
265 * Perform ifreq ioctl() on associated Ethernet device.
268 * Pointer to Ethernet device.
270 * Request number to pass to ioctl().
272 * Interface request structure output buffer.
275 * 0 on success, a negative errno value otherwise and rte_errno is set.
278 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
280 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
287 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
290 ret = ioctl(sock, req, ifr);
306 * Pointer to Ethernet device.
308 * MTU value output buffer.
311 * 0 on success, a negative errno value otherwise and rte_errno is set.
314 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
316 struct ifreq request;
317 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
321 *mtu = request.ifr_mtu;
329 * Pointer to Ethernet device.
334 * 0 on success, a negative errno value otherwise and rte_errno is set.
337 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
339 struct ifreq request = { .ifr_mtu = mtu, };
341 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
348 * Pointer to Ethernet device.
350 * Bitmask for flags that must remain untouched.
352 * Bitmask for flags to modify.
355 * 0 on success, a negative errno value otherwise and rte_errno is set.
358 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
360 struct ifreq request;
361 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
365 request.ifr_flags &= keep;
366 request.ifr_flags |= flags & ~keep;
367 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
371 * DPDK callback for Ethernet device configuration.
374 * Pointer to Ethernet device structure.
377 * 0 on success, a negative errno value otherwise and rte_errno is set.
380 mlx5_dev_configure(struct rte_eth_dev *dev)
382 struct priv *priv = dev->data->dev_private;
383 unsigned int rxqs_n = dev->data->nb_rx_queues;
384 unsigned int txqs_n = dev->data->nb_tx_queues;
387 unsigned int reta_idx_n;
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);
409 memcpy(priv->rss_conf.rss_key,
411 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
412 rss_hash_default_key,
413 MLX5_RSS_HASH_KEY_LEN);
414 priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
415 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
416 priv->rxqs = (void *)dev->data->rx_queues;
417 priv->txqs = (void *)dev->data->tx_queues;
418 if (txqs_n != priv->txqs_n) {
419 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
420 dev->data->port_id, priv->txqs_n, txqs_n);
421 priv->txqs_n = txqs_n;
423 if (rxqs_n > priv->config.ind_table_max_size) {
424 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
425 dev->data->port_id, rxqs_n);
429 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;
434 /* If the requested number of RX queues is not a power of two, use the
435 * maximum indirection table size for better balancing.
436 * The result is always rounded to the next power of two. */
437 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
438 priv->config.ind_table_max_size :
440 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
443 /* When the number of RX queues is not a power of two, the remaining
444 * table entries are padded with reused WQs and hashes are not spread
446 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
447 (*priv->reta_idx)[i] = j;
455 * Sets default tuning parameters.
458 * Pointer to Ethernet device.
460 * Info structure output buffer.
463 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
465 struct priv *priv = dev->data->dev_private;
467 /* Minimum CPU utilization. */
468 info->default_rxportconf.ring_size = 256;
469 info->default_txportconf.ring_size = 256;
470 info->default_rxportconf.burst_size = 64;
471 info->default_txportconf.burst_size = 64;
472 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
473 info->default_rxportconf.nb_queues = 16;
474 info->default_txportconf.nb_queues = 16;
475 if (dev->data->nb_rx_queues > 2 ||
476 dev->data->nb_tx_queues > 2) {
477 /* Max Throughput. */
478 info->default_rxportconf.ring_size = 2048;
479 info->default_txportconf.ring_size = 2048;
482 info->default_rxportconf.nb_queues = 8;
483 info->default_txportconf.nb_queues = 8;
484 if (dev->data->nb_rx_queues > 2 ||
485 dev->data->nb_tx_queues > 2) {
486 /* Max Throughput. */
487 info->default_rxportconf.ring_size = 4096;
488 info->default_txportconf.ring_size = 4096;
494 * DPDK callback to get information about the device.
497 * Pointer to Ethernet device structure.
499 * Info structure output buffer.
502 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
504 struct priv *priv = dev->data->dev_private;
505 struct mlx5_dev_config *config = &priv->config;
507 char ifname[IF_NAMESIZE];
509 /* FIXME: we should ask the device for these values. */
510 info->min_rx_bufsize = 32;
511 info->max_rx_pktlen = 65536;
513 * Since we need one CQ per QP, the limit is the minimum number
514 * between the two values.
516 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
517 priv->device_attr.orig_attr.max_qp);
518 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
521 info->max_rx_queues = max;
522 info->max_tx_queues = max;
523 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
524 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
525 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
526 info->rx_queue_offload_capa);
527 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
528 if (mlx5_get_ifname(dev, &ifname) == 0)
529 info->if_index = if_nametoindex(ifname);
530 info->reta_size = priv->reta_idx_n ?
531 priv->reta_idx_n : config->ind_table_max_size;
532 info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
533 info->speed_capa = priv->link_speed_capa;
534 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
535 mlx5_set_default_params(dev, info);
536 info->switch_info.name = dev->data->name;
537 info->switch_info.domain_id = priv->domain_id;
538 info->switch_info.port_id = priv->representor_id;
539 if (priv->representor) {
540 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
543 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
546 rte_eth_devices[port_id[i]].data->dev_private;
549 opriv->representor ||
550 opriv->domain_id != priv->domain_id)
553 * Override switch name with that of the master
556 info->switch_info.name = opriv->dev_data->name;
563 * Get supported packet types.
566 * Pointer to Ethernet device structure.
569 * A pointer to the supported Packet types array.
572 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
574 static const uint32_t ptypes[] = {
575 /* refers to rxq_cq_to_pkt_type() */
577 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
578 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
579 RTE_PTYPE_L4_NONFRAG,
583 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
584 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
585 RTE_PTYPE_INNER_L4_NONFRAG,
586 RTE_PTYPE_INNER_L4_FRAG,
587 RTE_PTYPE_INNER_L4_TCP,
588 RTE_PTYPE_INNER_L4_UDP,
592 if (dev->rx_pkt_burst == mlx5_rx_burst ||
593 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
594 dev->rx_pkt_burst == mlx5_rx_burst_vec)
600 * DPDK callback to retrieve physical link information.
603 * Pointer to Ethernet device structure.
605 * Storage for current link status.
608 * 0 on success, a negative errno value otherwise and rte_errno is set.
611 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
612 struct rte_eth_link *link)
614 struct priv *priv = dev->data->dev_private;
615 struct ethtool_cmd edata = {
616 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
619 struct rte_eth_link dev_link;
623 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
625 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
626 dev->data->port_id, strerror(rte_errno));
629 dev_link = (struct rte_eth_link) {
630 .link_status = ((ifr.ifr_flags & IFF_UP) &&
631 (ifr.ifr_flags & IFF_RUNNING)),
633 ifr = (struct ifreq) {
634 .ifr_data = (void *)&edata,
636 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
639 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
640 dev->data->port_id, strerror(rte_errno));
643 link_speed = ethtool_cmd_speed(&edata);
644 if (link_speed == -1)
645 dev_link.link_speed = ETH_SPEED_NUM_NONE;
647 dev_link.link_speed = link_speed;
648 priv->link_speed_capa = 0;
649 if (edata.supported & SUPPORTED_Autoneg)
650 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
651 if (edata.supported & (SUPPORTED_1000baseT_Full |
652 SUPPORTED_1000baseKX_Full))
653 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
654 if (edata.supported & SUPPORTED_10000baseKR_Full)
655 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
656 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
657 SUPPORTED_40000baseCR4_Full |
658 SUPPORTED_40000baseSR4_Full |
659 SUPPORTED_40000baseLR4_Full))
660 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
661 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
662 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
663 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
664 ETH_LINK_SPEED_FIXED);
665 if (((dev_link.link_speed && !dev_link.link_status) ||
666 (!dev_link.link_speed && dev_link.link_status))) {
675 * Retrieve physical link information (unlocked version using new ioctl).
678 * Pointer to Ethernet device structure.
680 * Storage for current link status.
683 * 0 on success, a negative errno value otherwise and rte_errno is set.
686 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
687 struct rte_eth_link *link)
690 struct priv *priv = dev->data->dev_private;
691 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
693 struct rte_eth_link dev_link;
697 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
699 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
700 dev->data->port_id, strerror(rte_errno));
703 dev_link = (struct rte_eth_link) {
704 .link_status = ((ifr.ifr_flags & IFF_UP) &&
705 (ifr.ifr_flags & IFF_RUNNING)),
707 ifr = (struct ifreq) {
708 .ifr_data = (void *)&gcmd,
710 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
713 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
715 dev->data->port_id, strerror(rte_errno));
718 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
720 alignas(struct ethtool_link_settings)
721 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
722 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
723 struct ethtool_link_settings *ecmd = (void *)data;
726 ifr.ifr_data = (void *)ecmd;
727 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
730 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
732 dev->data->port_id, strerror(rte_errno));
735 dev_link.link_speed = ecmd->speed;
736 sc = ecmd->link_mode_masks[0] |
737 ((uint64_t)ecmd->link_mode_masks[1] << 32);
738 priv->link_speed_capa = 0;
739 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
740 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
741 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
742 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
743 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
744 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
745 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
746 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
747 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
748 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
749 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
750 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
751 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
752 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
753 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
754 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
755 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
756 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
757 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
758 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
759 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
760 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
761 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
762 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
763 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
764 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
765 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
766 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
767 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
768 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
769 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
770 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
771 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
772 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
773 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
774 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
775 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
776 ETH_LINK_SPEED_FIXED);
777 if (((dev_link.link_speed && !dev_link.link_status) ||
778 (!dev_link.link_speed && dev_link.link_status))) {
787 * DPDK callback to retrieve physical link information.
790 * Pointer to Ethernet device structure.
791 * @param wait_to_complete
792 * Wait for request completion.
795 * 0 if link status was not updated, positive if it was, a negative errno
796 * value otherwise and rte_errno is set.
799 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
802 struct rte_eth_link dev_link;
803 time_t start_time = time(NULL);
806 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
808 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
811 /* Handle wait to complete situation. */
812 if (wait_to_complete && ret == -EAGAIN) {
813 if (abs((int)difftime(time(NULL), start_time)) <
814 MLX5_LINK_STATUS_TIMEOUT) {
821 } else if (ret < 0) {
824 } while (wait_to_complete);
825 ret = !!memcmp(&dev->data->dev_link, &dev_link,
826 sizeof(struct rte_eth_link));
827 dev->data->dev_link = dev_link;
832 * DPDK callback to change the MTU.
835 * Pointer to Ethernet device structure.
840 * 0 on success, a negative errno value otherwise and rte_errno is set.
843 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
845 struct priv *priv = dev->data->dev_private;
846 uint16_t kern_mtu = 0;
849 ret = mlx5_get_mtu(dev, &kern_mtu);
852 /* Set kernel interface MTU first. */
853 ret = mlx5_set_mtu(dev, mtu);
856 ret = mlx5_get_mtu(dev, &kern_mtu);
859 if (kern_mtu == mtu) {
861 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
862 dev->data->port_id, mtu);
870 * DPDK callback to get flow control status.
873 * Pointer to Ethernet device structure.
874 * @param[out] fc_conf
875 * Flow control output buffer.
878 * 0 on success, a negative errno value otherwise and rte_errno is set.
881 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
884 struct ethtool_pauseparam ethpause = {
885 .cmd = ETHTOOL_GPAUSEPARAM
889 ifr.ifr_data = (void *)ðpause;
890 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
893 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
895 dev->data->port_id, strerror(rte_errno));
898 fc_conf->autoneg = ethpause.autoneg;
899 if (ethpause.rx_pause && ethpause.tx_pause)
900 fc_conf->mode = RTE_FC_FULL;
901 else if (ethpause.rx_pause)
902 fc_conf->mode = RTE_FC_RX_PAUSE;
903 else if (ethpause.tx_pause)
904 fc_conf->mode = RTE_FC_TX_PAUSE;
906 fc_conf->mode = RTE_FC_NONE;
911 * DPDK callback to modify flow control parameters.
914 * Pointer to Ethernet device structure.
916 * Flow control parameters.
919 * 0 on success, a negative errno value otherwise and rte_errno is set.
922 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
925 struct ethtool_pauseparam ethpause = {
926 .cmd = ETHTOOL_SPAUSEPARAM
930 ifr.ifr_data = (void *)ðpause;
931 ethpause.autoneg = fc_conf->autoneg;
932 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
933 (fc_conf->mode & RTE_FC_RX_PAUSE))
934 ethpause.rx_pause = 1;
936 ethpause.rx_pause = 0;
938 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
939 (fc_conf->mode & RTE_FC_TX_PAUSE))
940 ethpause.tx_pause = 1;
942 ethpause.tx_pause = 0;
943 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
946 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
948 dev->data->port_id, strerror(rte_errno));
955 * Get PCI information from struct ibv_device.
958 * Pointer to Ethernet device structure.
959 * @param[out] pci_addr
960 * PCI bus address output buffer.
963 * 0 on success, a negative errno value otherwise and rte_errno is set.
966 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
967 struct rte_pci_addr *pci_addr)
971 MKSTR(path, "%s/device/uevent", device->ibdev_path);
973 file = fopen(path, "rb");
978 while (fgets(line, sizeof(line), file) == line) {
979 size_t len = strlen(line);
982 /* Truncate long lines. */
983 if (len == (sizeof(line) - 1))
984 while (line[(len - 1)] != '\n') {
988 line[(len - 1)] = ret;
990 /* Extract information. */
993 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
997 &pci_addr->function) == 4) {
1007 * Device status handler.
1010 * Pointer to Ethernet device.
1012 * Pointer to event flags holder.
1015 * Events bitmap of callback process which can be called immediately.
1018 mlx5_dev_status_handler(struct rte_eth_dev *dev)
1020 struct priv *priv = dev->data->dev_private;
1021 struct ibv_async_event event;
1024 if (mlx5_link_update(dev, 0) == -EAGAIN) {
1028 /* Read all message and acknowledge them. */
1030 if (mlx5_glue->get_async_event(priv->ctx, &event))
1032 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1033 event.event_type == IBV_EVENT_PORT_ERR) &&
1034 (dev->data->dev_conf.intr_conf.lsc == 1))
1035 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1036 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1037 dev->data->dev_conf.intr_conf.rmv == 1)
1038 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1041 "port %u event type %d on not handled",
1042 dev->data->port_id, event.event_type);
1043 mlx5_glue->ack_async_event(&event);
1049 * Handle interrupts from the NIC.
1051 * @param[in] intr_handle
1052 * Interrupt handler.
1054 * Callback argument.
1057 mlx5_dev_interrupt_handler(void *cb_arg)
1059 struct rte_eth_dev *dev = cb_arg;
1062 events = mlx5_dev_status_handler(dev);
1063 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1064 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1065 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1066 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1070 * Handle interrupts from the socket.
1073 * Callback argument.
1076 mlx5_dev_handler_socket(void *cb_arg)
1078 struct rte_eth_dev *dev = cb_arg;
1080 mlx5_socket_handle(dev);
1084 * Uninstall interrupt handler.
1087 * Pointer to Ethernet device.
1090 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1092 struct priv *priv = dev->data->dev_private;
1094 if (dev->data->dev_conf.intr_conf.lsc ||
1095 dev->data->dev_conf.intr_conf.rmv)
1096 rte_intr_callback_unregister(&priv->intr_handle,
1097 mlx5_dev_interrupt_handler, dev);
1098 if (priv->primary_socket)
1099 rte_intr_callback_unregister(&priv->intr_handle_socket,
1100 mlx5_dev_handler_socket, dev);
1101 priv->intr_handle.fd = 0;
1102 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1103 priv->intr_handle_socket.fd = 0;
1104 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1108 * Install interrupt handler.
1111 * Pointer to Ethernet device.
1114 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1116 struct priv *priv = dev->data->dev_private;
1120 assert(priv->ctx->async_fd > 0);
1121 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1122 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1125 "port %u failed to change file descriptor async event"
1127 dev->data->port_id);
1128 dev->data->dev_conf.intr_conf.lsc = 0;
1129 dev->data->dev_conf.intr_conf.rmv = 0;
1131 if (dev->data->dev_conf.intr_conf.lsc ||
1132 dev->data->dev_conf.intr_conf.rmv) {
1133 priv->intr_handle.fd = priv->ctx->async_fd;
1134 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1135 rte_intr_callback_register(&priv->intr_handle,
1136 mlx5_dev_interrupt_handler, dev);
1138 ret = mlx5_socket_init(dev);
1140 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1141 dev->data->port_id, strerror(rte_errno));
1142 else if (priv->primary_socket) {
1143 priv->intr_handle_socket.fd = priv->primary_socket;
1144 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1145 rte_intr_callback_register(&priv->intr_handle_socket,
1146 mlx5_dev_handler_socket, dev);
1151 * DPDK callback to bring the link DOWN.
1154 * Pointer to Ethernet device structure.
1157 * 0 on success, a negative errno value otherwise and rte_errno is set.
1160 mlx5_set_link_down(struct rte_eth_dev *dev)
1162 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1166 * DPDK callback to bring the link UP.
1169 * Pointer to Ethernet device structure.
1172 * 0 on success, a negative errno value otherwise and rte_errno is set.
1175 mlx5_set_link_up(struct rte_eth_dev *dev)
1177 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1181 * Configure the TX function to use.
1184 * Pointer to private data structure.
1187 * Pointer to selected Tx burst function.
1190 mlx5_select_tx_function(struct rte_eth_dev *dev)
1192 struct priv *priv = dev->data->dev_private;
1193 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1194 struct mlx5_dev_config *config = &priv->config;
1195 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1196 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1197 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1198 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1199 DEV_TX_OFFLOAD_IP_TNL_TSO |
1200 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1201 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1202 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1203 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1204 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1206 assert(priv != NULL);
1207 /* Select appropriate TX function. */
1208 if (vlan_insert || tso || swp)
1209 return tx_pkt_burst;
1210 if (config->mps == MLX5_MPW_ENHANCED) {
1211 if (mlx5_check_vec_tx_support(dev) > 0) {
1212 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1213 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1215 tx_pkt_burst = mlx5_tx_burst_vec;
1217 "port %u selected enhanced MPW Tx vectorized"
1219 dev->data->port_id);
1221 tx_pkt_burst = mlx5_tx_burst_empw;
1223 "port %u selected enhanced MPW Tx function",
1224 dev->data->port_id);
1226 } else if (config->mps && (config->txq_inline > 0)) {
1227 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1228 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1229 dev->data->port_id);
1230 } else if (config->mps) {
1231 tx_pkt_burst = mlx5_tx_burst_mpw;
1232 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1233 dev->data->port_id);
1235 return tx_pkt_burst;
1239 * Configure the RX function to use.
1242 * Pointer to private data structure.
1245 * Pointer to selected Rx burst function.
1248 mlx5_select_rx_function(struct rte_eth_dev *dev)
1250 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1252 assert(dev != NULL);
1253 if (mlx5_check_vec_rx_support(dev) > 0) {
1254 rx_pkt_burst = mlx5_rx_burst_vec;
1255 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1256 dev->data->port_id);
1257 } else if (mlx5_mprq_enabled(dev)) {
1258 rx_pkt_burst = mlx5_rx_burst_mprq;
1260 return rx_pkt_burst;
1264 * Check if mlx5 device was removed.
1267 * Pointer to Ethernet device structure.
1270 * 1 when device is removed, otherwise 0.
1273 mlx5_is_removed(struct rte_eth_dev *dev)
1275 struct ibv_device_attr device_attr;
1276 struct priv *priv = dev->data->dev_private;
1278 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1284 * Get port ID list of mlx5 instances sharing a common device.
1287 * Device to look for.
1288 * @param[out] port_list
1289 * Result buffer for collected port IDs.
1290 * @param port_list_n
1291 * Maximum number of entries in result buffer. If 0, @p port_list can be
1295 * Number of matching instances regardless of the @p port_list_n
1296 * parameter, 0 if none were found.
1299 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1300 unsigned int port_list_n)
1305 RTE_ETH_FOREACH_DEV(id) {
1306 struct rte_eth_dev *ldev = &rte_eth_devices[id];
1308 if (ldev->device != dev)
1310 if (n < port_list_n)
1318 * Get switch information associated with network interface.
1321 * Network interface index.
1323 * Switch information object, populated in case of success.
1326 * 0 on success, a negative errno value otherwise and rte_errno is set.
1329 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1331 char ifname[IF_NAMESIZE];
1333 struct mlx5_switch_info data = { .master = 0, };
1334 bool port_name_set = false;
1335 bool port_switch_id_set = false;
1338 if (!if_indextoname(ifindex, ifname)) {
1343 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1345 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1348 file = fopen(phys_port_name, "rb");
1351 fscanf(file, "%d%c", &data.port_name, &c) == 2 &&
1355 file = fopen(phys_switch_id, "rb");
1360 port_switch_id_set =
1361 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1364 data.master = port_switch_id_set && !port_name_set;
1365 data.representor = port_switch_id_set && port_name_set;