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 /* Add defines in case the running kernel is not the same as user headers. */
45 #ifndef ETHTOOL_GLINKSETTINGS
46 struct ethtool_link_settings {
55 uint8_t eth_tp_mdix_ctrl;
56 int8_t link_mode_masks_nwords;
58 uint32_t link_mode_masks[];
61 #define ETHTOOL_GLINKSETTINGS 0x0000004c
62 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
63 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
64 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
65 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
66 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
67 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
68 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
69 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
70 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
71 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
72 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
73 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
74 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
75 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
76 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
77 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
79 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
80 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
81 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
82 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
84 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
85 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
86 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
88 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
89 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
90 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
91 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
92 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
96 * Get interface name from private structure.
99 * Pointer to Ethernet device.
101 * Interface name output buffer.
104 * 0 on success, a negative errno value otherwise and rte_errno is set.
107 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
109 struct priv *priv = dev->data->dev_private;
112 unsigned int dev_type = 0;
113 unsigned int dev_port_prev = ~0u;
114 char match[IF_NAMESIZE] = "";
117 MKSTR(path, "%s/device/net", priv->ibdev_path);
125 while ((dent = readdir(dir)) != NULL) {
126 char *name = dent->d_name;
128 unsigned int dev_port;
131 if ((name[0] == '.') &&
132 ((name[1] == '\0') ||
133 ((name[1] == '.') && (name[2] == '\0'))))
136 MKSTR(path, "%s/device/net/%s/%s",
137 priv->ibdev_path, name,
138 (dev_type ? "dev_id" : "dev_port"));
140 file = fopen(path, "rb");
145 * Switch to dev_id when dev_port does not exist as
146 * is the case with Linux kernel versions < 3.15.
157 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
162 * Switch to dev_id when dev_port returns the same value for
163 * all ports. May happen when using a MOFED release older than
164 * 3.0 with a Linux kernel >= 3.15.
166 if (dev_port == dev_port_prev)
168 dev_port_prev = dev_port;
169 if (dev_port == (priv->port - 1u))
170 strlcpy(match, name, sizeof(match));
173 if (match[0] == '\0') {
177 strncpy(*ifname, match, sizeof(*ifname));
182 * Get the interface index from device name.
185 * Pointer to Ethernet device.
188 * Interface index on success, a negative errno value otherwise and
192 mlx5_ifindex(const struct rte_eth_dev *dev)
194 char ifname[IF_NAMESIZE];
197 ret = mlx5_get_ifname(dev, &ifname);
200 ret = if_nametoindex(ifname);
209 * Perform ifreq ioctl() on associated Ethernet device.
212 * Pointer to Ethernet device.
214 * Request number to pass to ioctl().
216 * Interface request structure output buffer.
219 * 0 on success, a negative errno value otherwise and rte_errno is set.
222 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
224 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
231 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
234 ret = ioctl(sock, req, ifr);
250 * Pointer to Ethernet device.
252 * MTU value output buffer.
255 * 0 on success, a negative errno value otherwise and rte_errno is set.
258 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
260 struct ifreq request;
261 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
265 *mtu = request.ifr_mtu;
273 * Pointer to Ethernet device.
278 * 0 on success, a negative errno value otherwise and rte_errno is set.
281 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
283 struct ifreq request = { .ifr_mtu = mtu, };
285 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
292 * Pointer to Ethernet device.
294 * Bitmask for flags that must remain untouched.
296 * Bitmask for flags to modify.
299 * 0 on success, a negative errno value otherwise and rte_errno is set.
302 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
304 struct ifreq request;
305 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
309 request.ifr_flags &= keep;
310 request.ifr_flags |= flags & ~keep;
311 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
315 * DPDK callback for Ethernet device configuration.
318 * Pointer to Ethernet device structure.
321 * 0 on success, a negative errno value otherwise and rte_errno is set.
324 mlx5_dev_configure(struct rte_eth_dev *dev)
326 struct priv *priv = dev->data->dev_private;
327 unsigned int rxqs_n = dev->data->nb_rx_queues;
328 unsigned int txqs_n = dev->data->nb_tx_queues;
331 unsigned int reta_idx_n;
332 const uint8_t use_app_rss_key =
333 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
336 if (use_app_rss_key &&
337 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
338 rss_hash_default_key_len)) {
339 DRV_LOG(ERR, "port %u RSS key len must be %zu Bytes long",
340 dev->data->port_id, rss_hash_default_key_len);
344 priv->rss_conf.rss_key =
345 rte_realloc(priv->rss_conf.rss_key,
346 rss_hash_default_key_len, 0);
347 if (!priv->rss_conf.rss_key) {
348 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
349 dev->data->port_id, rxqs_n);
353 memcpy(priv->rss_conf.rss_key,
355 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
356 rss_hash_default_key,
357 rss_hash_default_key_len);
358 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
359 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
360 priv->rxqs = (void *)dev->data->rx_queues;
361 priv->txqs = (void *)dev->data->tx_queues;
362 if (txqs_n != priv->txqs_n) {
363 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
364 dev->data->port_id, priv->txqs_n, txqs_n);
365 priv->txqs_n = txqs_n;
367 if (rxqs_n > priv->config.ind_table_max_size) {
368 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
369 dev->data->port_id, rxqs_n);
373 if (rxqs_n == priv->rxqs_n)
375 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
376 dev->data->port_id, priv->rxqs_n, rxqs_n);
377 priv->rxqs_n = rxqs_n;
378 /* If the requested number of RX queues is not a power of two, use the
379 * maximum indirection table size for better balancing.
380 * The result is always rounded to the next power of two. */
381 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
382 priv->config.ind_table_max_size :
384 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
387 /* When the number of RX queues is not a power of two, the remaining
388 * table entries are padded with reused WQs and hashes are not spread
390 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
391 (*priv->reta_idx)[i] = j;
396 * Once the device is added to the list of memory event callback, its
397 * global MR cache table cannot be expanded on the fly because of
398 * deadlock. If it overflows, lookup should be done by searching MR list
399 * linearly, which is slow.
401 if (mlx5_mr_btree_init(&priv->mr.cache, MLX5_MR_BTREE_CACHE_N * 2,
402 dev->device->numa_node)) {
403 /* rte_errno is already set. */
406 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
407 LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
409 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
414 * Sets default tuning parameters.
417 * Pointer to Ethernet device.
419 * Info structure output buffer.
422 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
424 struct priv *priv = dev->data->dev_private;
426 /* Minimum CPU utilization. */
427 info->default_rxportconf.ring_size = 256;
428 info->default_txportconf.ring_size = 256;
429 info->default_rxportconf.burst_size = 64;
430 info->default_txportconf.burst_size = 64;
431 if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
432 info->default_rxportconf.nb_queues = 16;
433 info->default_txportconf.nb_queues = 16;
434 if (dev->data->nb_rx_queues > 2 ||
435 dev->data->nb_tx_queues > 2) {
436 /* Max Throughput. */
437 info->default_rxportconf.ring_size = 2048;
438 info->default_txportconf.ring_size = 2048;
441 info->default_rxportconf.nb_queues = 8;
442 info->default_txportconf.nb_queues = 8;
443 if (dev->data->nb_rx_queues > 2 ||
444 dev->data->nb_tx_queues > 2) {
445 /* Max Throughput. */
446 info->default_rxportconf.ring_size = 4096;
447 info->default_txportconf.ring_size = 4096;
453 * DPDK callback to get information about the device.
456 * Pointer to Ethernet device structure.
458 * Info structure output buffer.
461 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
463 struct priv *priv = dev->data->dev_private;
464 struct mlx5_dev_config *config = &priv->config;
466 char ifname[IF_NAMESIZE];
468 /* FIXME: we should ask the device for these values. */
469 info->min_rx_bufsize = 32;
470 info->max_rx_pktlen = 65536;
472 * Since we need one CQ per QP, the limit is the minimum number
473 * between the two values.
475 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
476 priv->device_attr.orig_attr.max_qp);
477 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
480 info->max_rx_queues = max;
481 info->max_tx_queues = max;
482 info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
483 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
484 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
485 info->rx_queue_offload_capa);
486 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
487 if (mlx5_get_ifname(dev, &ifname) == 0)
488 info->if_index = if_nametoindex(ifname);
489 info->reta_size = priv->reta_idx_n ?
490 priv->reta_idx_n : config->ind_table_max_size;
491 info->hash_key_size = rss_hash_default_key_len;
492 info->speed_capa = priv->link_speed_capa;
493 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
494 mlx5_set_default_params(dev, info);
498 * Get supported packet types.
501 * Pointer to Ethernet device structure.
504 * A pointer to the supported Packet types array.
507 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
509 static const uint32_t ptypes[] = {
510 /* refers to rxq_cq_to_pkt_type() */
512 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
513 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
514 RTE_PTYPE_L4_NONFRAG,
518 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
519 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
520 RTE_PTYPE_INNER_L4_NONFRAG,
521 RTE_PTYPE_INNER_L4_FRAG,
522 RTE_PTYPE_INNER_L4_TCP,
523 RTE_PTYPE_INNER_L4_UDP,
527 if (dev->rx_pkt_burst == mlx5_rx_burst ||
528 dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
529 dev->rx_pkt_burst == mlx5_rx_burst_vec)
535 * DPDK callback to retrieve physical link information.
538 * Pointer to Ethernet device structure.
540 * Storage for current link status.
543 * 0 on success, a negative errno value otherwise and rte_errno is set.
546 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
547 struct rte_eth_link *link)
549 struct priv *priv = dev->data->dev_private;
550 struct ethtool_cmd edata = {
551 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
554 struct rte_eth_link dev_link;
558 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
560 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
561 dev->data->port_id, strerror(rte_errno));
564 memset(&dev_link, 0, sizeof(dev_link));
565 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
566 (ifr.ifr_flags & IFF_RUNNING));
567 ifr.ifr_data = (void *)&edata;
568 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
571 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
572 dev->data->port_id, strerror(rte_errno));
575 link_speed = ethtool_cmd_speed(&edata);
576 if (link_speed == -1)
577 dev_link.link_speed = ETH_SPEED_NUM_NONE;
579 dev_link.link_speed = link_speed;
580 priv->link_speed_capa = 0;
581 if (edata.supported & SUPPORTED_Autoneg)
582 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
583 if (edata.supported & (SUPPORTED_1000baseT_Full |
584 SUPPORTED_1000baseKX_Full))
585 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
586 if (edata.supported & SUPPORTED_10000baseKR_Full)
587 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
588 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
589 SUPPORTED_40000baseCR4_Full |
590 SUPPORTED_40000baseSR4_Full |
591 SUPPORTED_40000baseLR4_Full))
592 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
593 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
594 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
595 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
596 ETH_LINK_SPEED_FIXED);
597 if ((dev_link.link_speed && !dev_link.link_status) ||
598 (!dev_link.link_speed && dev_link.link_status)) {
607 * Retrieve physical link information (unlocked version using new ioctl).
610 * Pointer to Ethernet device structure.
612 * Storage for current link status.
615 * 0 on success, a negative errno value otherwise and rte_errno is set.
618 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
619 struct rte_eth_link *link)
622 struct priv *priv = dev->data->dev_private;
623 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
625 struct rte_eth_link dev_link;
629 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
631 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
632 dev->data->port_id, strerror(rte_errno));
635 memset(&dev_link, 0, sizeof(dev_link));
636 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
637 (ifr.ifr_flags & IFF_RUNNING));
638 ifr.ifr_data = (void *)&gcmd;
639 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
642 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
644 dev->data->port_id, strerror(rte_errno));
647 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
649 alignas(struct ethtool_link_settings)
650 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
651 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
652 struct ethtool_link_settings *ecmd = (void *)data;
655 ifr.ifr_data = (void *)ecmd;
656 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
659 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
661 dev->data->port_id, strerror(rte_errno));
664 dev_link.link_speed = ecmd->speed;
665 sc = ecmd->link_mode_masks[0] |
666 ((uint64_t)ecmd->link_mode_masks[1] << 32);
667 priv->link_speed_capa = 0;
668 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
669 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
670 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
671 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
672 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
673 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
674 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
675 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
676 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
677 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
678 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
679 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
680 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
681 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
682 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
683 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
684 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
685 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
686 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
687 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
688 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
689 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
690 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
691 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
692 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
693 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
694 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
695 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
696 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
697 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
698 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
699 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
700 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
701 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
702 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
703 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
704 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
705 ETH_LINK_SPEED_FIXED);
706 if ((dev_link.link_speed && !dev_link.link_status) ||
707 (!dev_link.link_speed && dev_link.link_status)) {
716 * DPDK callback to retrieve physical link information.
719 * Pointer to Ethernet device structure.
720 * @param wait_to_complete
721 * Wait for request completion.
724 * 0 if link status was not updated, positive if it was, a negative errno
725 * value otherwise and rte_errno is set.
728 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
731 struct rte_eth_link dev_link;
732 time_t start_time = time(NULL);
735 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
737 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
740 /* Handle wait to complete situation. */
741 if (wait_to_complete && ret == -EAGAIN) {
742 if (abs((int)difftime(time(NULL), start_time)) <
743 MLX5_LINK_STATUS_TIMEOUT) {
750 } else if (ret < 0) {
753 } while (wait_to_complete);
754 ret = !!memcmp(&dev->data->dev_link, &dev_link,
755 sizeof(struct rte_eth_link));
756 dev->data->dev_link = dev_link;
761 * DPDK callback to change the MTU.
764 * Pointer to Ethernet device structure.
769 * 0 on success, a negative errno value otherwise and rte_errno is set.
772 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
774 struct priv *priv = dev->data->dev_private;
775 uint16_t kern_mtu = 0;
778 ret = mlx5_get_mtu(dev, &kern_mtu);
781 /* Set kernel interface MTU first. */
782 ret = mlx5_set_mtu(dev, mtu);
785 ret = mlx5_get_mtu(dev, &kern_mtu);
788 if (kern_mtu == mtu) {
790 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
791 dev->data->port_id, mtu);
799 * DPDK callback to get flow control status.
802 * Pointer to Ethernet device structure.
803 * @param[out] fc_conf
804 * Flow control output buffer.
807 * 0 on success, a negative errno value otherwise and rte_errno is set.
810 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
813 struct ethtool_pauseparam ethpause = {
814 .cmd = ETHTOOL_GPAUSEPARAM
818 ifr.ifr_data = (void *)ðpause;
819 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
822 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
824 dev->data->port_id, strerror(rte_errno));
827 fc_conf->autoneg = ethpause.autoneg;
828 if (ethpause.rx_pause && ethpause.tx_pause)
829 fc_conf->mode = RTE_FC_FULL;
830 else if (ethpause.rx_pause)
831 fc_conf->mode = RTE_FC_RX_PAUSE;
832 else if (ethpause.tx_pause)
833 fc_conf->mode = RTE_FC_TX_PAUSE;
835 fc_conf->mode = RTE_FC_NONE;
840 * DPDK callback to modify flow control parameters.
843 * Pointer to Ethernet device structure.
845 * Flow control parameters.
848 * 0 on success, a negative errno value otherwise and rte_errno is set.
851 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
854 struct ethtool_pauseparam ethpause = {
855 .cmd = ETHTOOL_SPAUSEPARAM
859 ifr.ifr_data = (void *)ðpause;
860 ethpause.autoneg = fc_conf->autoneg;
861 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
862 (fc_conf->mode & RTE_FC_RX_PAUSE))
863 ethpause.rx_pause = 1;
865 ethpause.rx_pause = 0;
867 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
868 (fc_conf->mode & RTE_FC_TX_PAUSE))
869 ethpause.tx_pause = 1;
871 ethpause.tx_pause = 0;
872 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
875 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
877 dev->data->port_id, strerror(rte_errno));
884 * Get PCI information from struct ibv_device.
887 * Pointer to Ethernet device structure.
888 * @param[out] pci_addr
889 * PCI bus address output buffer.
892 * 0 on success, a negative errno value otherwise and rte_errno is set.
895 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
896 struct rte_pci_addr *pci_addr)
900 MKSTR(path, "%s/device/uevent", device->ibdev_path);
902 file = fopen(path, "rb");
907 while (fgets(line, sizeof(line), file) == line) {
908 size_t len = strlen(line);
911 /* Truncate long lines. */
912 if (len == (sizeof(line) - 1))
913 while (line[(len - 1)] != '\n') {
917 line[(len - 1)] = ret;
919 /* Extract information. */
922 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
926 &pci_addr->function) == 4) {
936 * Device status handler.
939 * Pointer to Ethernet device.
941 * Pointer to event flags holder.
944 * Events bitmap of callback process which can be called immediately.
947 mlx5_dev_status_handler(struct rte_eth_dev *dev)
949 struct priv *priv = dev->data->dev_private;
950 struct ibv_async_event event;
953 if (mlx5_link_update(dev, 0) == -EAGAIN) {
957 /* Read all message and acknowledge them. */
959 if (mlx5_glue->get_async_event(priv->ctx, &event))
961 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
962 event.event_type == IBV_EVENT_PORT_ERR) &&
963 (dev->data->dev_conf.intr_conf.lsc == 1))
964 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
965 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
966 dev->data->dev_conf.intr_conf.rmv == 1)
967 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
970 "port %u event type %d on not handled",
971 dev->data->port_id, event.event_type);
972 mlx5_glue->ack_async_event(&event);
978 * Handle interrupts from the NIC.
980 * @param[in] intr_handle
986 mlx5_dev_interrupt_handler(void *cb_arg)
988 struct rte_eth_dev *dev = cb_arg;
991 events = mlx5_dev_status_handler(dev);
992 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
993 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
994 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
995 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
999 * Handle interrupts from the socket.
1002 * Callback argument.
1005 mlx5_dev_handler_socket(void *cb_arg)
1007 struct rte_eth_dev *dev = cb_arg;
1009 mlx5_socket_handle(dev);
1013 * Uninstall interrupt handler.
1016 * Pointer to Ethernet device.
1019 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1021 struct priv *priv = dev->data->dev_private;
1023 if (dev->data->dev_conf.intr_conf.lsc ||
1024 dev->data->dev_conf.intr_conf.rmv)
1025 rte_intr_callback_unregister(&priv->intr_handle,
1026 mlx5_dev_interrupt_handler, dev);
1027 if (priv->primary_socket)
1028 rte_intr_callback_unregister(&priv->intr_handle_socket,
1029 mlx5_dev_handler_socket, dev);
1030 priv->intr_handle.fd = 0;
1031 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1032 priv->intr_handle_socket.fd = 0;
1033 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1037 * Install interrupt handler.
1040 * Pointer to Ethernet device.
1043 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1045 struct priv *priv = dev->data->dev_private;
1049 assert(priv->ctx->async_fd > 0);
1050 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1051 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1054 "port %u failed to change file descriptor async event"
1056 dev->data->port_id);
1057 dev->data->dev_conf.intr_conf.lsc = 0;
1058 dev->data->dev_conf.intr_conf.rmv = 0;
1060 if (dev->data->dev_conf.intr_conf.lsc ||
1061 dev->data->dev_conf.intr_conf.rmv) {
1062 priv->intr_handle.fd = priv->ctx->async_fd;
1063 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1064 rte_intr_callback_register(&priv->intr_handle,
1065 mlx5_dev_interrupt_handler, dev);
1067 ret = mlx5_socket_init(dev);
1069 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1070 dev->data->port_id, strerror(rte_errno));
1071 else if (priv->primary_socket) {
1072 priv->intr_handle_socket.fd = priv->primary_socket;
1073 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1074 rte_intr_callback_register(&priv->intr_handle_socket,
1075 mlx5_dev_handler_socket, dev);
1080 * DPDK callback to bring the link DOWN.
1083 * Pointer to Ethernet device structure.
1086 * 0 on success, a negative errno value otherwise and rte_errno is set.
1089 mlx5_set_link_down(struct rte_eth_dev *dev)
1091 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1095 * DPDK callback to bring the link UP.
1098 * Pointer to Ethernet device structure.
1101 * 0 on success, a negative errno value otherwise and rte_errno is set.
1104 mlx5_set_link_up(struct rte_eth_dev *dev)
1106 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1110 * Configure the TX function to use.
1113 * Pointer to private data structure.
1116 * Pointer to selected Tx burst function.
1119 mlx5_select_tx_function(struct rte_eth_dev *dev)
1121 struct priv *priv = dev->data->dev_private;
1122 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1123 struct mlx5_dev_config *config = &priv->config;
1124 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1125 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1126 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1127 DEV_TX_OFFLOAD_GRE_TNL_TSO |
1128 DEV_TX_OFFLOAD_IP_TNL_TSO |
1129 DEV_TX_OFFLOAD_UDP_TNL_TSO));
1130 int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1131 DEV_TX_OFFLOAD_UDP_TNL_TSO |
1132 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1133 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1135 assert(priv != NULL);
1136 /* Select appropriate TX function. */
1137 if (vlan_insert || tso || swp)
1138 return tx_pkt_burst;
1139 if (config->mps == MLX5_MPW_ENHANCED) {
1140 if (mlx5_check_vec_tx_support(dev) > 0) {
1141 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1142 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1144 tx_pkt_burst = mlx5_tx_burst_vec;
1146 "port %u selected enhanced MPW Tx vectorized"
1148 dev->data->port_id);
1150 tx_pkt_burst = mlx5_tx_burst_empw;
1152 "port %u selected enhanced MPW Tx function",
1153 dev->data->port_id);
1155 } else if (config->mps && (config->txq_inline > 0)) {
1156 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1157 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1158 dev->data->port_id);
1159 } else if (config->mps) {
1160 tx_pkt_burst = mlx5_tx_burst_mpw;
1161 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1162 dev->data->port_id);
1164 return tx_pkt_burst;
1168 * Configure the RX function to use.
1171 * Pointer to private data structure.
1174 * Pointer to selected Rx burst function.
1177 mlx5_select_rx_function(struct rte_eth_dev *dev)
1179 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1181 assert(dev != NULL);
1182 if (mlx5_check_vec_rx_support(dev) > 0) {
1183 rx_pkt_burst = mlx5_rx_burst_vec;
1184 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1185 dev->data->port_id);
1186 } else if (mlx5_mprq_enabled(dev)) {
1187 rx_pkt_burst = mlx5_rx_burst_mprq;
1189 return rx_pkt_burst;
1193 * Check if mlx5 device was removed.
1196 * Pointer to Ethernet device structure.
1199 * 1 when device is removed, otherwise 0.
1202 mlx5_is_removed(struct rte_eth_dev *dev)
1204 struct ibv_device_attr device_attr;
1205 struct priv *priv = dev->data->dev_private;
1207 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)