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, 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 snprintf(match, sizeof(match), "%s", name);
173 if (match[0] == '\0') {
177 strncpy(*ifname, match, sizeof(*ifname));
182 * Perform ifreq ioctl() on associated Ethernet device.
185 * Pointer to Ethernet device.
187 * Request number to pass to ioctl().
189 * Interface request structure output buffer.
192 * 0 on success, a negative errno value otherwise and rte_errno is set.
195 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
197 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
204 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
207 ret = ioctl(sock, req, ifr);
223 * Pointer to Ethernet device.
225 * MTU value output buffer.
228 * 0 on success, a negative errno value otherwise and rte_errno is set.
231 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
233 struct ifreq request;
234 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
238 *mtu = request.ifr_mtu;
246 * Pointer to Ethernet device.
251 * 0 on success, a negative errno value otherwise and rte_errno is set.
254 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
256 struct ifreq request = { .ifr_mtu = mtu, };
258 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
265 * Pointer to Ethernet device.
267 * Bitmask for flags that must remain untouched.
269 * Bitmask for flags to modify.
272 * 0 on success, a negative errno value otherwise and rte_errno is set.
275 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
277 struct ifreq request;
278 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
282 request.ifr_flags &= keep;
283 request.ifr_flags |= flags & ~keep;
284 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
288 * DPDK callback for Ethernet device configuration.
291 * Pointer to Ethernet device structure.
294 * 0 on success, a negative errno value otherwise and rte_errno is set.
297 mlx5_dev_configure(struct rte_eth_dev *dev)
299 struct priv *priv = dev->data->dev_private;
300 unsigned int rxqs_n = dev->data->nb_rx_queues;
301 unsigned int txqs_n = dev->data->nb_tx_queues;
304 unsigned int reta_idx_n;
305 const uint8_t use_app_rss_key =
306 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
307 uint64_t supp_tx_offloads = mlx5_get_tx_port_offloads(dev);
308 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
309 uint64_t supp_rx_offloads =
310 (mlx5_get_rx_port_offloads() |
311 mlx5_get_rx_queue_offloads(dev));
312 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
315 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
316 ERROR("Some Tx offloads are not supported "
317 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
318 tx_offloads, supp_tx_offloads);
322 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
323 ERROR("Some Rx offloads are not supported "
324 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
325 rx_offloads, supp_rx_offloads);
329 if (use_app_rss_key &&
330 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
331 rss_hash_default_key_len)) {
332 /* MLX5 RSS only support 40bytes key. */
336 priv->rss_conf.rss_key =
337 rte_realloc(priv->rss_conf.rss_key,
338 rss_hash_default_key_len, 0);
339 if (!priv->rss_conf.rss_key) {
340 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
344 memcpy(priv->rss_conf.rss_key,
346 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
347 rss_hash_default_key,
348 rss_hash_default_key_len);
349 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
350 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
351 priv->rxqs = (void *)dev->data->rx_queues;
352 priv->txqs = (void *)dev->data->tx_queues;
353 if (txqs_n != priv->txqs_n) {
354 INFO("%p: TX queues number update: %u -> %u",
355 (void *)dev, priv->txqs_n, txqs_n);
356 priv->txqs_n = txqs_n;
358 if (rxqs_n > priv->config.ind_table_max_size) {
359 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
363 if (rxqs_n == priv->rxqs_n)
365 INFO("%p: RX queues number update: %u -> %u",
366 (void *)dev, priv->rxqs_n, rxqs_n);
367 priv->rxqs_n = rxqs_n;
368 /* If the requested number of RX queues is not a power of two, use the
369 * maximum indirection table size for better balancing.
370 * The result is always rounded to the next power of two. */
371 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
372 priv->config.ind_table_max_size :
374 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
377 /* When the number of RX queues is not a power of two, the remaining
378 * table entries are padded with reused WQs and hashes are not spread
380 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
381 (*priv->reta_idx)[i] = j;
389 * DPDK callback to get information about the device.
392 * Pointer to Ethernet device structure.
394 * Info structure output buffer.
397 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
399 struct priv *priv = dev->data->dev_private;
400 struct mlx5_dev_config *config = &priv->config;
402 char ifname[IF_NAMESIZE];
404 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
405 /* FIXME: we should ask the device for these values. */
406 info->min_rx_bufsize = 32;
407 info->max_rx_pktlen = 65536;
409 * Since we need one CQ per QP, the limit is the minimum number
410 * between the two values.
412 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
413 priv->device_attr.orig_attr.max_qp);
414 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
417 info->max_rx_queues = max;
418 info->max_tx_queues = max;
419 info->max_mac_addrs = RTE_DIM(priv->mac);
420 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
421 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
422 info->rx_queue_offload_capa);
423 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
424 if (mlx5_get_ifname(dev, &ifname) == 0)
425 info->if_index = if_nametoindex(ifname);
426 info->reta_size = priv->reta_idx_n ?
427 priv->reta_idx_n : config->ind_table_max_size;
428 info->hash_key_size = priv->rss_conf.rss_key_len;
429 info->speed_capa = priv->link_speed_capa;
430 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
434 * Get supported packet types.
437 * Pointer to Ethernet device structure.
440 * A pointer to the supported Packet types array.
443 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
445 static const uint32_t ptypes[] = {
446 /* refers to rxq_cq_to_pkt_type() */
448 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
449 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
450 RTE_PTYPE_L4_NONFRAG,
454 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
455 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
456 RTE_PTYPE_INNER_L4_NONFRAG,
457 RTE_PTYPE_INNER_L4_FRAG,
458 RTE_PTYPE_INNER_L4_TCP,
459 RTE_PTYPE_INNER_L4_UDP,
463 if (dev->rx_pkt_burst == mlx5_rx_burst ||
464 dev->rx_pkt_burst == mlx5_rx_burst_vec)
470 * DPDK callback to retrieve physical link information.
473 * Pointer to Ethernet device structure.
476 * 0 on success, a negative errno value otherwise and rte_errno is set.
479 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev)
481 struct priv *priv = dev->data->dev_private;
482 struct ethtool_cmd edata = {
483 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
486 struct rte_eth_link dev_link;
490 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
492 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
495 memset(&dev_link, 0, sizeof(dev_link));
496 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
497 (ifr.ifr_flags & IFF_RUNNING));
498 ifr.ifr_data = (void *)&edata;
499 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
501 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
502 strerror(rte_errno));
505 link_speed = ethtool_cmd_speed(&edata);
506 if (link_speed == -1)
507 dev_link.link_speed = 0;
509 dev_link.link_speed = link_speed;
510 priv->link_speed_capa = 0;
511 if (edata.supported & SUPPORTED_Autoneg)
512 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
513 if (edata.supported & (SUPPORTED_1000baseT_Full |
514 SUPPORTED_1000baseKX_Full))
515 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
516 if (edata.supported & SUPPORTED_10000baseKR_Full)
517 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
518 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
519 SUPPORTED_40000baseCR4_Full |
520 SUPPORTED_40000baseSR4_Full |
521 SUPPORTED_40000baseLR4_Full))
522 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
523 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
524 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
525 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
526 ETH_LINK_SPEED_FIXED);
527 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
528 /* Link status changed. */
529 dev->data->dev_link = dev_link;
532 /* Link status is still the same. */
538 * Retrieve physical link information (unlocked version using new ioctl).
541 * Pointer to Ethernet device structure.
544 * 0 on success, a negative errno value otherwise and rte_errno is set.
547 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev)
549 struct priv *priv = dev->data->dev_private;
550 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
552 struct rte_eth_link dev_link;
556 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
558 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
561 memset(&dev_link, 0, sizeof(dev_link));
562 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
563 (ifr.ifr_flags & IFF_RUNNING));
564 ifr.ifr_data = (void *)&gcmd;
565 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
567 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
568 strerror(rte_errno));
571 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
573 alignas(struct ethtool_link_settings)
574 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
575 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
576 struct ethtool_link_settings *ecmd = (void *)data;
579 ifr.ifr_data = (void *)ecmd;
580 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
582 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
583 strerror(rte_errno));
586 dev_link.link_speed = ecmd->speed;
587 sc = ecmd->link_mode_masks[0] |
588 ((uint64_t)ecmd->link_mode_masks[1] << 32);
589 priv->link_speed_capa = 0;
590 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
591 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
592 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
593 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
594 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
595 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
596 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
597 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
598 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
599 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
600 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
601 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
602 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
603 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
604 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
605 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
606 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
607 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
608 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
609 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
610 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
611 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
612 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
613 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
614 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
615 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
616 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
617 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
618 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
619 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
620 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
621 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
622 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
623 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
624 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
625 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
626 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
627 ETH_LINK_SPEED_FIXED);
628 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
629 /* Link status changed. */
630 dev->data->dev_link = dev_link;
633 /* Link status is still the same. */
639 * Enable receiving and transmitting traffic.
642 * Pointer to Ethernet device.
645 mlx5_link_start(struct rte_eth_dev *dev)
647 struct priv *priv = dev->data->dev_private;
650 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
651 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
652 ret = mlx5_traffic_enable(dev);
654 ERROR("%p: error occurred while configuring control flows: %s",
655 (void *)dev, strerror(rte_errno));
658 ret = mlx5_flow_start(dev, &priv->flows);
660 ERROR("%p: error occurred while configuring flows: %s",
661 (void *)dev, strerror(rte_errno));
666 * Disable receiving and transmitting traffic.
669 * Pointer to Ethernet device.
672 mlx5_link_stop(struct rte_eth_dev *dev)
674 struct priv *priv = dev->data->dev_private;
676 mlx5_flow_stop(dev, &priv->flows);
677 mlx5_traffic_disable(dev);
678 dev->rx_pkt_burst = removed_rx_burst;
679 dev->tx_pkt_burst = removed_tx_burst;
683 * Querying the link status till it changes to the desired state.
684 * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS.
687 * Pointer to Ethernet device.
689 * Link desired status.
692 * 0 on success, a negative errno value otherwise and rte_errno is set.
695 mlx5_force_link_status_change(struct rte_eth_dev *dev, int status)
699 while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) {
700 mlx5_link_update(dev, 0);
701 if (dev->data->dev_link.link_status == status)
711 * DPDK callback to retrieve physical link information.
714 * Pointer to Ethernet device structure.
715 * @param wait_to_complete
716 * Wait for request completion (ignored).
719 * 0 on success, a negative errno value otherwise and rte_errno is set.
722 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
724 struct utsname utsname;
727 struct rte_eth_link dev_link = dev->data->dev_link;
729 if (uname(&utsname) == -1 ||
730 sscanf(utsname.release, "%d.%d.%d",
731 &ver[0], &ver[1], &ver[2]) != 3 ||
732 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
733 ret = mlx5_link_update_unlocked_gset(dev);
735 ret = mlx5_link_update_unlocked_gs(dev);
738 /* If lsc interrupt is disabled, should always be ready for traffic. */
739 if (!dev->data->dev_conf.intr_conf.lsc) {
740 mlx5_link_start(dev);
743 /* Re-select burst callbacks only if link status has been changed. */
744 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
745 if (dev->data->dev_link.link_status == ETH_LINK_UP)
746 mlx5_link_start(dev);
754 * DPDK callback to change the MTU.
757 * Pointer to Ethernet device structure.
762 * 0 on success, a negative errno value otherwise and rte_errno is set.
765 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
767 struct priv *priv = dev->data->dev_private;
768 uint16_t kern_mtu = 0;
771 ret = mlx5_get_mtu(dev, &kern_mtu);
774 /* Set kernel interface MTU first. */
775 ret = mlx5_set_mtu(dev, mtu);
778 ret = mlx5_get_mtu(dev, &kern_mtu);
781 if (kern_mtu == mtu) {
783 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
791 * DPDK callback to get flow control status.
794 * Pointer to Ethernet device structure.
795 * @param[out] fc_conf
796 * Flow control output buffer.
799 * 0 on success, a negative errno value otherwise and rte_errno is set.
802 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
805 struct ethtool_pauseparam ethpause = {
806 .cmd = ETHTOOL_GPAUSEPARAM
810 ifr.ifr_data = (void *)ðpause;
811 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
813 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed: %s",
814 strerror(rte_errno));
817 fc_conf->autoneg = ethpause.autoneg;
818 if (ethpause.rx_pause && ethpause.tx_pause)
819 fc_conf->mode = RTE_FC_FULL;
820 else if (ethpause.rx_pause)
821 fc_conf->mode = RTE_FC_RX_PAUSE;
822 else if (ethpause.tx_pause)
823 fc_conf->mode = RTE_FC_TX_PAUSE;
825 fc_conf->mode = RTE_FC_NONE;
830 * DPDK callback to modify flow control parameters.
833 * Pointer to Ethernet device structure.
835 * Flow control parameters.
838 * 0 on success, a negative errno value otherwise and rte_errno is set.
841 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
844 struct ethtool_pauseparam ethpause = {
845 .cmd = ETHTOOL_SPAUSEPARAM
849 ifr.ifr_data = (void *)ðpause;
850 ethpause.autoneg = fc_conf->autoneg;
851 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
852 (fc_conf->mode & RTE_FC_RX_PAUSE))
853 ethpause.rx_pause = 1;
855 ethpause.rx_pause = 0;
857 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
858 (fc_conf->mode & RTE_FC_TX_PAUSE))
859 ethpause.tx_pause = 1;
861 ethpause.tx_pause = 0;
862 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
864 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
866 strerror(rte_errno));
873 * Get PCI information from struct ibv_device.
876 * Pointer to Ethernet device structure.
877 * @param[out] pci_addr
878 * PCI bus address output buffer.
881 * 0 on success, a negative errno value otherwise and rte_errno is set.
884 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
885 struct rte_pci_addr *pci_addr)
889 MKSTR(path, "%s/device/uevent", device->ibdev_path);
891 file = fopen(path, "rb");
896 while (fgets(line, sizeof(line), file) == line) {
897 size_t len = strlen(line);
900 /* Truncate long lines. */
901 if (len == (sizeof(line) - 1))
902 while (line[(len - 1)] != '\n') {
906 line[(len - 1)] = ret;
908 /* Extract information. */
911 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
915 &pci_addr->function) == 4) {
925 * Update the link status.
928 * Pointer to Ethernet device.
931 * Zero if the callback process can be called immediately, negative errno
932 * value otherwise and rte_errno is set.
935 mlx5_link_status_update(struct rte_eth_dev *dev)
937 struct priv *priv = dev->data->dev_private;
938 struct rte_eth_link *link = &dev->data->dev_link;
941 ret = mlx5_link_update(dev, 0);
944 if (((link->link_speed == 0) && link->link_status) ||
945 ((link->link_speed != 0) && !link->link_status)) {
947 * Inconsistent status. Event likely occurred before the
948 * kernel netdevice exposes the new status.
950 if (!priv->pending_alarm) {
951 priv->pending_alarm = 1;
952 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
953 mlx5_dev_link_status_handler,
957 } else if (unlikely(priv->pending_alarm)) {
958 /* Link interrupt occurred while alarm is already scheduled. */
959 priv->pending_alarm = 0;
960 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
966 * Device status handler.
969 * Pointer to Ethernet device.
971 * Pointer to event flags holder.
974 * Events bitmap of callback process which can be called immediately.
977 mlx5_dev_status_handler(struct rte_eth_dev *dev)
979 struct priv *priv = dev->data->dev_private;
980 struct ibv_async_event event;
983 /* Read all message and acknowledge them. */
985 if (mlx5_glue->get_async_event(priv->ctx, &event))
987 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
988 event.event_type == IBV_EVENT_PORT_ERR) &&
989 (dev->data->dev_conf.intr_conf.lsc == 1))
990 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
991 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
992 dev->data->dev_conf.intr_conf.rmv == 1)
993 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
995 DEBUG("event type %d on port %d not handled",
996 event.event_type, event.element.port_num);
997 mlx5_glue->ack_async_event(&event);
999 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1000 if (mlx5_link_status_update(dev))
1001 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1006 * Handle delayed link status event.
1009 * Registered argument.
1012 mlx5_dev_link_status_handler(void *arg)
1014 struct rte_eth_dev *dev = arg;
1015 struct priv *priv = dev->data->dev_private;
1018 priv->pending_alarm = 0;
1019 ret = mlx5_link_status_update(dev);
1021 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1025 * Handle interrupts from the NIC.
1027 * @param[in] intr_handle
1028 * Interrupt handler.
1030 * Callback argument.
1033 mlx5_dev_interrupt_handler(void *cb_arg)
1035 struct rte_eth_dev *dev = cb_arg;
1038 events = mlx5_dev_status_handler(dev);
1039 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1040 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1041 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1042 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1046 * Handle interrupts from the socket.
1049 * Callback argument.
1052 mlx5_dev_handler_socket(void *cb_arg)
1054 struct rte_eth_dev *dev = cb_arg;
1056 mlx5_socket_handle(dev);
1060 * Uninstall interrupt handler.
1063 * Pointer to Ethernet device.
1066 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1068 struct priv *priv = dev->data->dev_private;
1070 if (dev->data->dev_conf.intr_conf.lsc ||
1071 dev->data->dev_conf.intr_conf.rmv)
1072 rte_intr_callback_unregister(&priv->intr_handle,
1073 mlx5_dev_interrupt_handler, dev);
1074 if (priv->primary_socket)
1075 rte_intr_callback_unregister(&priv->intr_handle_socket,
1076 mlx5_dev_handler_socket, dev);
1077 if (priv->pending_alarm) {
1078 priv->pending_alarm = 0;
1079 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1081 priv->intr_handle.fd = 0;
1082 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1083 priv->intr_handle_socket.fd = 0;
1084 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1088 * Install interrupt handler.
1091 * Pointer to Ethernet device.
1094 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1096 struct priv *priv = dev->data->dev_private;
1100 assert(priv->ctx->async_fd > 0);
1101 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1102 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1104 INFO("failed to change file descriptor async event queue");
1105 dev->data->dev_conf.intr_conf.lsc = 0;
1106 dev->data->dev_conf.intr_conf.rmv = 0;
1108 if (dev->data->dev_conf.intr_conf.lsc ||
1109 dev->data->dev_conf.intr_conf.rmv) {
1110 priv->intr_handle.fd = priv->ctx->async_fd;
1111 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1112 rte_intr_callback_register(&priv->intr_handle,
1113 mlx5_dev_interrupt_handler, dev);
1115 ret = mlx5_socket_init(dev);
1117 ERROR("cannot initialise socket: %s", strerror(rte_errno));
1118 else if (priv->primary_socket) {
1119 priv->intr_handle_socket.fd = priv->primary_socket;
1120 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1121 rte_intr_callback_register(&priv->intr_handle_socket,
1122 mlx5_dev_handler_socket, dev);
1127 * DPDK callback to bring the link DOWN.
1130 * Pointer to Ethernet device structure.
1133 * 0 on success, a negative errno value otherwise and rte_errno is set.
1136 mlx5_set_link_down(struct rte_eth_dev *dev)
1138 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1142 * DPDK callback to bring the link UP.
1145 * Pointer to Ethernet device structure.
1148 * 0 on success, a negative errno value otherwise and rte_errno is set.
1151 mlx5_set_link_up(struct rte_eth_dev *dev)
1153 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1157 * Configure the TX function to use.
1160 * Pointer to private data structure.
1163 * Pointer to selected Tx burst function.
1166 mlx5_select_tx_function(struct rte_eth_dev *dev)
1168 struct priv *priv = dev->data->dev_private;
1169 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1170 struct mlx5_dev_config *config = &priv->config;
1171 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1172 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1173 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1174 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1175 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1177 assert(priv != NULL);
1178 /* Select appropriate TX function. */
1179 if (vlan_insert || tso)
1180 return tx_pkt_burst;
1181 if (config->mps == MLX5_MPW_ENHANCED) {
1182 if (mlx5_check_vec_tx_support(dev) > 0) {
1183 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1184 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1186 tx_pkt_burst = mlx5_tx_burst_vec;
1187 DEBUG("selected Enhanced MPW TX vectorized function");
1189 tx_pkt_burst = mlx5_tx_burst_empw;
1190 DEBUG("selected Enhanced MPW TX function");
1192 } else if (config->mps && (config->txq_inline > 0)) {
1193 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1194 DEBUG("selected MPW inline TX function");
1195 } else if (config->mps) {
1196 tx_pkt_burst = mlx5_tx_burst_mpw;
1197 DEBUG("selected MPW TX function");
1199 return tx_pkt_burst;
1203 * Configure the RX function to use.
1206 * Pointer to private data structure.
1209 * Pointer to selected Rx burst function.
1212 mlx5_select_rx_function(struct rte_eth_dev *dev)
1214 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1216 assert(dev != NULL);
1217 if (mlx5_check_vec_rx_support(dev) > 0) {
1218 rx_pkt_burst = mlx5_rx_burst_vec;
1219 DEBUG("selected RX vectorized function");
1221 return rx_pkt_burst;
1225 * Check if mlx5 device was removed.
1228 * Pointer to Ethernet device structure.
1231 * 1 when device is removed, otherwise 0.
1234 mlx5_is_removed(struct rte_eth_dev *dev)
1236 struct ibv_device_attr device_attr;
1237 struct priv *priv = dev->data->dev_private;
1239 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)