1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox.
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/utsname.h>
22 #include <netinet/in.h>
23 #include <linux/ethtool.h>
24 #include <linux/sockios.h>
25 #include <linux/version.h>
30 #include <rte_atomic.h>
31 #include <rte_ethdev_driver.h>
32 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_interrupts.h>
36 #include <rte_alarm.h>
37 #include <rte_malloc.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, -1 on failure and 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);
123 while ((dent = readdir(dir)) != NULL) {
124 char *name = dent->d_name;
126 unsigned int dev_port;
129 if ((name[0] == '.') &&
130 ((name[1] == '\0') ||
131 ((name[1] == '.') && (name[2] == '\0'))))
134 MKSTR(path, "%s/device/net/%s/%s",
135 priv->ibdev_path, name,
136 (dev_type ? "dev_id" : "dev_port"));
138 file = fopen(path, "rb");
143 * Switch to dev_id when dev_port does not exist as
144 * is the case with Linux kernel versions < 3.15.
155 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
160 * Switch to dev_id when dev_port returns the same value for
161 * all ports. May happen when using a MOFED release older than
162 * 3.0 with a Linux kernel >= 3.15.
164 if (dev_port == dev_port_prev)
166 dev_port_prev = dev_port;
167 if (dev_port == (priv->port - 1u))
168 snprintf(match, sizeof(match), "%s", name);
171 if (match[0] == '\0')
173 strncpy(*ifname, match, sizeof(*ifname));
178 * Perform ifreq ioctl() on associated Ethernet device.
181 * Pointer to Ethernet device.
183 * Request number to pass to ioctl().
185 * Interface request structure output buffer.
188 * 0 on success, -1 on failure and errno is set.
191 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
193 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
198 if (mlx5_get_ifname(dev, &ifr->ifr_name) == 0)
199 ret = ioctl(sock, req, ifr);
208 * Pointer to Ethernet device.
210 * MTU value output buffer.
213 * 0 on success, -1 on failure and errno is set.
216 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
218 struct ifreq request;
219 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
223 *mtu = request.ifr_mtu;
231 * Pointer to Ethernet device.
236 * 0 on success, -1 on failure and errno is set.
239 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
241 struct ifreq request = { .ifr_mtu = mtu, };
243 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
250 * Pointer to Ethernet device.
252 * Bitmask for flags that must remain untouched.
254 * Bitmask for flags to modify.
257 * 0 on success, -1 on failure and errno is set.
260 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
262 struct ifreq request;
263 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
267 request.ifr_flags &= keep;
268 request.ifr_flags |= flags & ~keep;
269 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
273 * DPDK callback for Ethernet device configuration.
276 * Pointer to Ethernet device structure.
279 * 0 on success, negative errno value on failure.
282 mlx5_dev_configure(struct rte_eth_dev *dev)
284 struct priv *priv = dev->data->dev_private;
285 unsigned int rxqs_n = dev->data->nb_rx_queues;
286 unsigned int txqs_n = dev->data->nb_tx_queues;
289 unsigned int reta_idx_n;
290 const uint8_t use_app_rss_key =
291 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
292 uint64_t supp_tx_offloads = mlx5_get_tx_port_offloads(dev);
293 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
294 uint64_t supp_rx_offloads =
295 (mlx5_get_rx_port_offloads() |
296 mlx5_get_rx_queue_offloads(dev));
297 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
299 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
300 ERROR("Some Tx offloads are not supported "
301 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
302 tx_offloads, supp_tx_offloads);
305 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
306 ERROR("Some Rx offloads are not supported "
307 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
308 rx_offloads, supp_rx_offloads);
311 if (use_app_rss_key &&
312 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
313 rss_hash_default_key_len)) {
314 /* MLX5 RSS only support 40bytes key. */
317 priv->rss_conf.rss_key =
318 rte_realloc(priv->rss_conf.rss_key,
319 rss_hash_default_key_len, 0);
320 if (!priv->rss_conf.rss_key) {
321 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
324 memcpy(priv->rss_conf.rss_key,
326 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
327 rss_hash_default_key,
328 rss_hash_default_key_len);
329 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
330 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
331 priv->rxqs = (void *)dev->data->rx_queues;
332 priv->txqs = (void *)dev->data->tx_queues;
333 if (txqs_n != priv->txqs_n) {
334 INFO("%p: TX queues number update: %u -> %u",
335 (void *)dev, priv->txqs_n, txqs_n);
336 priv->txqs_n = txqs_n;
338 if (rxqs_n > priv->config.ind_table_max_size) {
339 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
342 if (rxqs_n == priv->rxqs_n)
344 INFO("%p: RX queues number update: %u -> %u",
345 (void *)dev, priv->rxqs_n, rxqs_n);
346 priv->rxqs_n = rxqs_n;
347 /* If the requested number of RX queues is not a power of two, use the
348 * maximum indirection table size for better balancing.
349 * The result is always rounded to the next power of two. */
350 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
351 priv->config.ind_table_max_size :
353 if (mlx5_rss_reta_index_resize(dev, reta_idx_n))
355 /* When the number of RX queues is not a power of two, the remaining
356 * table entries are padded with reused WQs and hashes are not spread
358 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
359 (*priv->reta_idx)[i] = j;
368 * DPDK callback to get information about the device.
371 * Pointer to Ethernet device structure.
373 * Info structure output buffer.
376 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
378 struct priv *priv = dev->data->dev_private;
379 struct mlx5_dev_config *config = &priv->config;
381 char ifname[IF_NAMESIZE];
383 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
384 /* FIXME: we should ask the device for these values. */
385 info->min_rx_bufsize = 32;
386 info->max_rx_pktlen = 65536;
388 * Since we need one CQ per QP, the limit is the minimum number
389 * between the two values.
391 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
392 priv->device_attr.orig_attr.max_qp);
393 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
396 info->max_rx_queues = max;
397 info->max_tx_queues = max;
398 info->max_mac_addrs = RTE_DIM(priv->mac);
399 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
400 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
401 info->rx_queue_offload_capa);
402 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
403 if (mlx5_get_ifname(dev, &ifname) == 0)
404 info->if_index = if_nametoindex(ifname);
405 info->reta_size = priv->reta_idx_n ?
406 priv->reta_idx_n : config->ind_table_max_size;
407 info->hash_key_size = priv->rss_conf.rss_key_len;
408 info->speed_capa = priv->link_speed_capa;
409 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
413 * Get supported packet types.
416 * Pointer to Ethernet device structure.
419 * A pointer to the supported Packet types array.
422 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
424 static const uint32_t ptypes[] = {
425 /* refers to rxq_cq_to_pkt_type() */
427 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
428 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
429 RTE_PTYPE_L4_NONFRAG,
433 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
434 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
435 RTE_PTYPE_INNER_L4_NONFRAG,
436 RTE_PTYPE_INNER_L4_FRAG,
437 RTE_PTYPE_INNER_L4_TCP,
438 RTE_PTYPE_INNER_L4_UDP,
442 if (dev->rx_pkt_burst == mlx5_rx_burst ||
443 dev->rx_pkt_burst == mlx5_rx_burst_vec)
449 * DPDK callback to retrieve physical link information.
452 * Pointer to Ethernet device structure.
455 * 0 on success, -1 on error.
458 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev)
460 struct priv *priv = dev->data->dev_private;
461 struct ethtool_cmd edata = {
462 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
465 struct rte_eth_link dev_link;
468 if (mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr)) {
469 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
472 memset(&dev_link, 0, sizeof(dev_link));
473 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
474 (ifr.ifr_flags & IFF_RUNNING));
475 ifr.ifr_data = (void *)&edata;
476 if (mlx5_ifreq(dev, SIOCETHTOOL, &ifr)) {
477 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
481 link_speed = ethtool_cmd_speed(&edata);
482 if (link_speed == -1)
483 dev_link.link_speed = 0;
485 dev_link.link_speed = link_speed;
486 priv->link_speed_capa = 0;
487 if (edata.supported & SUPPORTED_Autoneg)
488 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
489 if (edata.supported & (SUPPORTED_1000baseT_Full |
490 SUPPORTED_1000baseKX_Full))
491 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
492 if (edata.supported & SUPPORTED_10000baseKR_Full)
493 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
494 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
495 SUPPORTED_40000baseCR4_Full |
496 SUPPORTED_40000baseSR4_Full |
497 SUPPORTED_40000baseLR4_Full))
498 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
499 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
500 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
501 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
502 ETH_LINK_SPEED_FIXED);
503 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
504 /* Link status changed. */
505 dev->data->dev_link = dev_link;
508 /* Link status is still the same. */
513 * Retrieve physical link information (unlocked version using new ioctl).
516 * Pointer to Ethernet device structure.
519 * 0 on success, -1 on error.
522 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev)
524 struct priv *priv = dev->data->dev_private;
525 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
527 struct rte_eth_link dev_link;
530 if (mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr)) {
531 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
534 memset(&dev_link, 0, sizeof(dev_link));
535 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
536 (ifr.ifr_flags & IFF_RUNNING));
537 ifr.ifr_data = (void *)&gcmd;
538 if (mlx5_ifreq(dev, SIOCETHTOOL, &ifr)) {
539 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
543 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
545 alignas(struct ethtool_link_settings)
546 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
547 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
548 struct ethtool_link_settings *ecmd = (void *)data;
551 ifr.ifr_data = (void *)ecmd;
552 if (mlx5_ifreq(dev, SIOCETHTOOL, &ifr)) {
553 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
557 dev_link.link_speed = ecmd->speed;
558 sc = ecmd->link_mode_masks[0] |
559 ((uint64_t)ecmd->link_mode_masks[1] << 32);
560 priv->link_speed_capa = 0;
561 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
562 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
563 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
564 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
565 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
566 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
567 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
568 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
569 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
570 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
571 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
572 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
573 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
574 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
575 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
576 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
577 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
578 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
579 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
580 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
581 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
582 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
583 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
584 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
585 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
586 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
587 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
588 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
589 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
590 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
591 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
592 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
593 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
594 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
595 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
596 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
597 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
598 ETH_LINK_SPEED_FIXED);
599 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
600 /* Link status changed. */
601 dev->data->dev_link = dev_link;
604 /* Link status is still the same. */
609 * Enable receiving and transmitting traffic.
612 * Pointer to Ethernet device.
615 mlx5_link_start(struct rte_eth_dev *dev)
617 struct priv *priv = dev->data->dev_private;
620 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
621 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
622 err = mlx5_traffic_enable(dev);
624 ERROR("%p: error occurred while configuring control flows: %s",
625 (void *)dev, strerror(err));
626 err = mlx5_flow_start(dev, &priv->flows);
628 ERROR("%p: error occurred while configuring flows: %s",
629 (void *)dev, strerror(err));
633 * Disable receiving and transmitting traffic.
636 * Pointer to Ethernet device.
639 mlx5_link_stop(struct rte_eth_dev *dev)
641 struct priv *priv = dev->data->dev_private;
643 mlx5_flow_stop(dev, &priv->flows);
644 mlx5_traffic_disable(dev);
645 dev->rx_pkt_burst = removed_rx_burst;
646 dev->tx_pkt_burst = removed_tx_burst;
650 * Querying the link status till it changes to the desired state.
651 * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS.
654 * Pointer to Ethernet device.
656 * Link desired status.
659 * 0 on success, negative errno value on failure.
662 mlx5_force_link_status_change(struct rte_eth_dev *dev, int status)
666 while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) {
667 mlx5_link_update(dev, 0);
668 if (dev->data->dev_link.link_status == status)
677 * DPDK callback to retrieve physical link information.
680 * Pointer to Ethernet device structure.
681 * @param wait_to_complete
682 * Wait for request completion (ignored).
685 * 0 on success, -1 on error.
688 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
690 struct utsname utsname;
693 struct rte_eth_link dev_link = dev->data->dev_link;
695 if (uname(&utsname) == -1 ||
696 sscanf(utsname.release, "%d.%d.%d",
697 &ver[0], &ver[1], &ver[2]) != 3 ||
698 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
699 ret = mlx5_link_update_unlocked_gset(dev);
701 ret = mlx5_link_update_unlocked_gs(dev);
702 /* If lsc interrupt is disabled, should always be ready for traffic. */
703 if (!dev->data->dev_conf.intr_conf.lsc) {
704 mlx5_link_start(dev);
707 /* Re-select burst callbacks only if link status has been changed. */
708 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
709 if (dev->data->dev_link.link_status == ETH_LINK_UP)
710 mlx5_link_start(dev);
718 * DPDK callback to change the MTU.
721 * Pointer to Ethernet device structure.
726 * 0 on success, negative errno value on failure.
729 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
731 struct priv *priv = dev->data->dev_private;
735 ret = mlx5_get_mtu(dev, &kern_mtu);
738 /* Set kernel interface MTU first. */
739 ret = mlx5_set_mtu(dev, mtu);
742 ret = mlx5_get_mtu(dev, &kern_mtu);
745 if (kern_mtu == mtu) {
747 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
752 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
759 * DPDK callback to get flow control status.
762 * Pointer to Ethernet device structure.
763 * @param[out] fc_conf
764 * Flow control output buffer.
767 * 0 on success, negative errno value on failure.
770 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
773 struct ethtool_pauseparam ethpause = {
774 .cmd = ETHTOOL_GPAUSEPARAM
778 ifr.ifr_data = (void *)ðpause;
779 if (mlx5_ifreq(dev, SIOCETHTOOL, &ifr)) {
781 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed: %s",
785 fc_conf->autoneg = ethpause.autoneg;
786 if (ethpause.rx_pause && ethpause.tx_pause)
787 fc_conf->mode = RTE_FC_FULL;
788 else if (ethpause.rx_pause)
789 fc_conf->mode = RTE_FC_RX_PAUSE;
790 else if (ethpause.tx_pause)
791 fc_conf->mode = RTE_FC_TX_PAUSE;
793 fc_conf->mode = RTE_FC_NONE;
801 * DPDK callback to modify flow control parameters.
804 * Pointer to Ethernet device structure.
806 * Flow control parameters.
809 * 0 on success, negative errno value on failure.
812 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
815 struct ethtool_pauseparam ethpause = {
816 .cmd = ETHTOOL_SPAUSEPARAM
820 ifr.ifr_data = (void *)ðpause;
821 ethpause.autoneg = fc_conf->autoneg;
822 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
823 (fc_conf->mode & RTE_FC_RX_PAUSE))
824 ethpause.rx_pause = 1;
826 ethpause.rx_pause = 0;
828 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
829 (fc_conf->mode & RTE_FC_TX_PAUSE))
830 ethpause.tx_pause = 1;
832 ethpause.tx_pause = 0;
833 if (mlx5_ifreq(dev, SIOCETHTOOL, &ifr)) {
835 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
847 * Get PCI information from struct ibv_device.
850 * Pointer to Ethernet device structure.
851 * @param[out] pci_addr
852 * PCI bus address output buffer.
855 * 0 on success, -1 on failure and errno is set.
858 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
859 struct rte_pci_addr *pci_addr)
863 MKSTR(path, "%s/device/uevent", device->ibdev_path);
865 file = fopen(path, "rb");
868 while (fgets(line, sizeof(line), file) == line) {
869 size_t len = strlen(line);
872 /* Truncate long lines. */
873 if (len == (sizeof(line) - 1))
874 while (line[(len - 1)] != '\n') {
878 line[(len - 1)] = ret;
880 /* Extract information. */
883 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
887 &pci_addr->function) == 4) {
897 * Update the link status.
900 * Pointer to Ethernet device.
903 * Zero if the callback process can be called immediately.
906 mlx5_link_status_update(struct rte_eth_dev *dev)
908 struct priv *priv = dev->data->dev_private;
909 struct rte_eth_link *link = &dev->data->dev_link;
911 mlx5_link_update(dev, 0);
912 if (((link->link_speed == 0) && link->link_status) ||
913 ((link->link_speed != 0) && !link->link_status)) {
915 * Inconsistent status. Event likely occurred before the
916 * kernel netdevice exposes the new status.
918 if (!priv->pending_alarm) {
919 priv->pending_alarm = 1;
920 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
921 mlx5_dev_link_status_handler,
925 } else if (unlikely(priv->pending_alarm)) {
926 /* Link interrupt occurred while alarm is already scheduled. */
927 priv->pending_alarm = 0;
928 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
934 * Device status handler.
937 * Pointer to Ethernet device.
939 * Pointer to event flags holder.
942 * Events bitmap of callback process which can be called immediately.
945 mlx5_dev_status_handler(struct rte_eth_dev *dev)
947 struct priv *priv = dev->data->dev_private;
948 struct ibv_async_event event;
951 /* Read all message and acknowledge them. */
953 if (mlx5_glue->get_async_event(priv->ctx, &event))
955 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
956 event.event_type == IBV_EVENT_PORT_ERR) &&
957 (dev->data->dev_conf.intr_conf.lsc == 1))
958 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
959 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
960 dev->data->dev_conf.intr_conf.rmv == 1)
961 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
963 DEBUG("event type %d on port %d not handled",
964 event.event_type, event.element.port_num);
965 mlx5_glue->ack_async_event(&event);
967 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
968 if (mlx5_link_status_update(dev))
969 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
974 * Handle delayed link status event.
977 * Registered argument.
980 mlx5_dev_link_status_handler(void *arg)
982 struct rte_eth_dev *dev = arg;
983 struct priv *priv = dev->data->dev_private;
986 priv->pending_alarm = 0;
987 ret = mlx5_link_status_update(dev);
989 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
993 * Handle interrupts from the NIC.
995 * @param[in] intr_handle
1001 mlx5_dev_interrupt_handler(void *cb_arg)
1003 struct rte_eth_dev *dev = cb_arg;
1006 events = mlx5_dev_status_handler(dev);
1007 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1008 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1009 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1010 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1014 * Handle interrupts from the socket.
1017 * Callback argument.
1020 mlx5_dev_handler_socket(void *cb_arg)
1022 struct rte_eth_dev *dev = cb_arg;
1024 mlx5_socket_handle(dev);
1028 * Uninstall interrupt handler.
1031 * Pointer to Ethernet device.
1034 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1036 struct priv *priv = dev->data->dev_private;
1038 if (dev->data->dev_conf.intr_conf.lsc ||
1039 dev->data->dev_conf.intr_conf.rmv)
1040 rte_intr_callback_unregister(&priv->intr_handle,
1041 mlx5_dev_interrupt_handler, dev);
1042 if (priv->primary_socket)
1043 rte_intr_callback_unregister(&priv->intr_handle_socket,
1044 mlx5_dev_handler_socket, dev);
1045 if (priv->pending_alarm) {
1046 priv->pending_alarm = 0;
1047 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1049 priv->intr_handle.fd = 0;
1050 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1051 priv->intr_handle_socket.fd = 0;
1052 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1056 * Install interrupt handler.
1059 * Pointer to Ethernet device.
1062 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1064 struct priv *priv = dev->data->dev_private;
1067 assert(priv->ctx->async_fd > 0);
1068 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1069 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1071 INFO("failed to change file descriptor async event queue");
1072 dev->data->dev_conf.intr_conf.lsc = 0;
1073 dev->data->dev_conf.intr_conf.rmv = 0;
1075 if (dev->data->dev_conf.intr_conf.lsc ||
1076 dev->data->dev_conf.intr_conf.rmv) {
1077 priv->intr_handle.fd = priv->ctx->async_fd;
1078 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1079 rte_intr_callback_register(&priv->intr_handle,
1080 mlx5_dev_interrupt_handler, dev);
1082 rc = mlx5_socket_init(dev);
1083 if (!rc && priv->primary_socket) {
1084 priv->intr_handle_socket.fd = priv->primary_socket;
1085 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1086 rte_intr_callback_register(&priv->intr_handle_socket,
1087 mlx5_dev_handler_socket, dev);
1092 * DPDK callback to bring the link DOWN.
1095 * Pointer to Ethernet device structure.
1098 * 0 on success, errno value on failure.
1101 mlx5_set_link_down(struct rte_eth_dev *dev)
1103 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1107 * DPDK callback to bring the link UP.
1110 * Pointer to Ethernet device structure.
1113 * 0 on success, errno value on failure.
1116 mlx5_set_link_up(struct rte_eth_dev *dev)
1118 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1122 * Configure the TX function to use.
1125 * Pointer to private data structure.
1128 * Pointer to selected Tx burst function.
1131 mlx5_select_tx_function(struct rte_eth_dev *dev)
1133 struct priv *priv = dev->data->dev_private;
1134 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1135 struct mlx5_dev_config *config = &priv->config;
1136 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1137 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1138 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1139 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1140 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1142 assert(priv != NULL);
1143 /* Select appropriate TX function. */
1144 if (vlan_insert || tso)
1145 return tx_pkt_burst;
1146 if (config->mps == MLX5_MPW_ENHANCED) {
1147 if (mlx5_check_vec_tx_support(dev) > 0) {
1148 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1149 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1151 tx_pkt_burst = mlx5_tx_burst_vec;
1152 DEBUG("selected Enhanced MPW TX vectorized function");
1154 tx_pkt_burst = mlx5_tx_burst_empw;
1155 DEBUG("selected Enhanced MPW TX function");
1157 } else if (config->mps && (config->txq_inline > 0)) {
1158 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1159 DEBUG("selected MPW inline TX function");
1160 } else if (config->mps) {
1161 tx_pkt_burst = mlx5_tx_burst_mpw;
1162 DEBUG("selected MPW TX function");
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 DEBUG("selected RX vectorized function");
1186 return rx_pkt_burst;
1190 * Check if mlx5 device was removed.
1193 * Pointer to Ethernet device structure.
1196 * 1 when device is removed, otherwise 0.
1199 mlx5_is_removed(struct rte_eth_dev *dev)
1201 struct ibv_device_attr device_attr;
1202 struct priv *priv = dev->data->dev_private;
1204 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)