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 <netinet/in.h>
22 #include <linux/ethtool.h>
23 #include <linux/sockios.h>
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_alarm.h>
35 #include <rte_malloc.h>
38 #include "mlx5_glue.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
42 /* Add defines in case the running kernel is not the same as user headers. */
43 #ifndef ETHTOOL_GLINKSETTINGS
44 struct ethtool_link_settings {
53 uint8_t eth_tp_mdix_ctrl;
54 int8_t link_mode_masks_nwords;
56 uint32_t link_mode_masks[];
59 #define ETHTOOL_GLINKSETTINGS 0x0000004c
60 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
61 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
62 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
63 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
64 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
65 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
66 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
67 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
68 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
69 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
70 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
71 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
72 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
73 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
74 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
75 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
77 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
78 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
79 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
80 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
82 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
83 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
84 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
86 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
87 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
88 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
89 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
90 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
94 * Get interface name from private structure.
97 * Pointer to Ethernet device.
99 * Interface name output buffer.
102 * 0 on success, a negative errno value otherwise and rte_errno is set.
105 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
107 struct priv *priv = dev->data->dev_private;
110 unsigned int dev_type = 0;
111 unsigned int dev_port_prev = ~0u;
112 char match[IF_NAMESIZE] = "";
115 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') {
175 strncpy(*ifname, match, sizeof(*ifname));
180 * Perform ifreq ioctl() on associated Ethernet device.
183 * Pointer to Ethernet device.
185 * Request number to pass to ioctl().
187 * Interface request structure output buffer.
190 * 0 on success, a negative errno value otherwise and rte_errno is set.
193 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
195 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
202 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
205 ret = ioctl(sock, req, ifr);
221 * Pointer to Ethernet device.
223 * MTU value output buffer.
226 * 0 on success, a negative errno value otherwise and rte_errno is set.
229 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
231 struct ifreq request;
232 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
236 *mtu = request.ifr_mtu;
244 * Pointer to Ethernet device.
249 * 0 on success, a negative errno value otherwise and rte_errno is set.
252 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
254 struct ifreq request = { .ifr_mtu = mtu, };
256 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
263 * Pointer to Ethernet device.
265 * Bitmask for flags that must remain untouched.
267 * Bitmask for flags to modify.
270 * 0 on success, a negative errno value otherwise and rte_errno is set.
273 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
275 struct ifreq request;
276 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
280 request.ifr_flags &= keep;
281 request.ifr_flags |= flags & ~keep;
282 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
286 * DPDK callback for Ethernet device configuration.
289 * Pointer to Ethernet device structure.
292 * 0 on success, a negative errno value otherwise and rte_errno is set.
295 mlx5_dev_configure(struct rte_eth_dev *dev)
297 struct priv *priv = dev->data->dev_private;
298 unsigned int rxqs_n = dev->data->nb_rx_queues;
299 unsigned int txqs_n = dev->data->nb_tx_queues;
302 unsigned int reta_idx_n;
303 const uint8_t use_app_rss_key =
304 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
305 uint64_t supp_tx_offloads = mlx5_get_tx_port_offloads(dev);
306 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
307 uint64_t supp_rx_offloads =
308 (mlx5_get_rx_port_offloads() |
309 mlx5_get_rx_queue_offloads(dev));
310 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
313 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
315 "port %u some Tx offloads are not supported requested"
316 " 0x%" PRIx64 " supported 0x%" PRIx64,
317 dev->data->port_id, tx_offloads, supp_tx_offloads);
321 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
323 "port %u some Rx offloads are not supported requested"
324 " 0x%" PRIx64 " supported 0x%" PRIx64,
325 dev->data->port_id, 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 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
341 dev->data->port_id, rxqs_n);
345 memcpy(priv->rss_conf.rss_key,
347 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
348 rss_hash_default_key,
349 rss_hash_default_key_len);
350 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
351 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
352 priv->rxqs = (void *)dev->data->rx_queues;
353 priv->txqs = (void *)dev->data->tx_queues;
354 if (txqs_n != priv->txqs_n) {
355 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
356 dev->data->port_id, priv->txqs_n, txqs_n);
357 priv->txqs_n = txqs_n;
359 if (rxqs_n > priv->config.ind_table_max_size) {
360 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
361 dev->data->port_id, rxqs_n);
365 if (rxqs_n == priv->rxqs_n)
367 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
368 dev->data->port_id, priv->rxqs_n, rxqs_n);
369 priv->rxqs_n = rxqs_n;
370 /* If the requested number of RX queues is not a power of two, use the
371 * maximum indirection table size for better balancing.
372 * The result is always rounded to the next power of two. */
373 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
374 priv->config.ind_table_max_size :
376 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
379 /* When the number of RX queues is not a power of two, the remaining
380 * table entries are padded with reused WQs and hashes are not spread
382 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
383 (*priv->reta_idx)[i] = j;
391 * DPDK callback to get information about the device.
394 * Pointer to Ethernet device structure.
396 * Info structure output buffer.
399 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
401 struct priv *priv = dev->data->dev_private;
402 struct mlx5_dev_config *config = &priv->config;
404 char ifname[IF_NAMESIZE];
406 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
407 /* FIXME: we should ask the device for these values. */
408 info->min_rx_bufsize = 32;
409 info->max_rx_pktlen = 65536;
411 * Since we need one CQ per QP, the limit is the minimum number
412 * between the two values.
414 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
415 priv->device_attr.orig_attr.max_qp);
416 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
419 info->max_rx_queues = max;
420 info->max_tx_queues = max;
421 info->max_mac_addrs = RTE_DIM(priv->mac);
422 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
423 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
424 info->rx_queue_offload_capa);
425 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
426 if (mlx5_get_ifname(dev, &ifname) == 0)
427 info->if_index = if_nametoindex(ifname);
428 info->reta_size = priv->reta_idx_n ?
429 priv->reta_idx_n : config->ind_table_max_size;
430 info->hash_key_size = priv->rss_conf.rss_key_len;
431 info->speed_capa = priv->link_speed_capa;
432 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
436 * Get supported packet types.
439 * Pointer to Ethernet device structure.
442 * A pointer to the supported Packet types array.
445 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
447 static const uint32_t ptypes[] = {
448 /* refers to rxq_cq_to_pkt_type() */
450 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
451 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
452 RTE_PTYPE_L4_NONFRAG,
456 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
457 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
458 RTE_PTYPE_INNER_L4_NONFRAG,
459 RTE_PTYPE_INNER_L4_FRAG,
460 RTE_PTYPE_INNER_L4_TCP,
461 RTE_PTYPE_INNER_L4_UDP,
465 if (dev->rx_pkt_burst == mlx5_rx_burst ||
466 dev->rx_pkt_burst == mlx5_rx_burst_vec)
472 * DPDK callback to retrieve physical link information.
475 * Pointer to Ethernet device structure.
478 * 0 on success, a negative errno value otherwise and rte_errno is set.
481 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev)
483 struct priv *priv = dev->data->dev_private;
484 struct ethtool_cmd edata = {
485 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
488 struct rte_eth_link dev_link;
492 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
494 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
495 dev->data->port_id, strerror(rte_errno));
498 memset(&dev_link, 0, sizeof(dev_link));
499 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
500 (ifr.ifr_flags & IFF_RUNNING));
501 ifr.ifr_data = (void *)&edata;
502 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
505 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
506 dev->data->port_id, strerror(rte_errno));
509 link_speed = ethtool_cmd_speed(&edata);
510 if (link_speed == -1)
511 dev_link.link_speed = 0;
513 dev_link.link_speed = link_speed;
514 priv->link_speed_capa = 0;
515 if (edata.supported & SUPPORTED_Autoneg)
516 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
517 if (edata.supported & (SUPPORTED_1000baseT_Full |
518 SUPPORTED_1000baseKX_Full))
519 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
520 if (edata.supported & SUPPORTED_10000baseKR_Full)
521 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
522 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
523 SUPPORTED_40000baseCR4_Full |
524 SUPPORTED_40000baseSR4_Full |
525 SUPPORTED_40000baseLR4_Full))
526 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
527 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
528 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
529 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
530 ETH_LINK_SPEED_FIXED);
531 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
532 /* Link status changed. */
533 dev->data->dev_link = dev_link;
536 /* Link status is still the same. */
542 * Retrieve physical link information (unlocked version using new ioctl).
545 * Pointer to Ethernet device structure.
548 * 0 on success, a negative errno value otherwise and rte_errno is set.
551 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev)
553 struct priv *priv = dev->data->dev_private;
554 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
556 struct rte_eth_link dev_link;
560 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
562 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
563 dev->data->port_id, strerror(rte_errno));
566 memset(&dev_link, 0, sizeof(dev_link));
567 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
568 (ifr.ifr_flags & IFF_RUNNING));
569 ifr.ifr_data = (void *)&gcmd;
570 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
573 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
575 dev->data->port_id, strerror(rte_errno));
578 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
580 alignas(struct ethtool_link_settings)
581 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
582 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
583 struct ethtool_link_settings *ecmd = (void *)data;
586 ifr.ifr_data = (void *)ecmd;
587 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
590 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
592 dev->data->port_id, strerror(rte_errno));
595 dev_link.link_speed = ecmd->speed;
596 sc = ecmd->link_mode_masks[0] |
597 ((uint64_t)ecmd->link_mode_masks[1] << 32);
598 priv->link_speed_capa = 0;
599 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
600 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
601 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
602 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
603 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
604 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
605 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
606 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
607 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
608 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
609 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
610 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
611 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
612 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
613 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
614 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
615 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
616 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
617 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
618 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
619 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
620 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
621 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
622 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
623 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
624 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
625 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
626 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
627 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
628 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
629 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
630 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
631 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
632 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
633 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
634 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
635 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
636 ETH_LINK_SPEED_FIXED);
637 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
638 /* Link status changed. */
639 dev->data->dev_link = dev_link;
642 /* Link status is still the same. */
648 * DPDK callback to retrieve physical link information.
651 * Pointer to Ethernet device structure.
652 * @param wait_to_complete
653 * Wait for request completion (ignored).
656 * 0 on success, a negative errno value otherwise and rte_errno is set.
659 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
663 ret = mlx5_link_update_unlocked_gset(dev);
665 ret = mlx5_link_update_unlocked_gs(dev);
670 * DPDK callback to change the MTU.
673 * Pointer to Ethernet device structure.
678 * 0 on success, a negative errno value otherwise and rte_errno is set.
681 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
683 struct priv *priv = dev->data->dev_private;
684 uint16_t kern_mtu = 0;
687 ret = mlx5_get_mtu(dev, &kern_mtu);
690 /* Set kernel interface MTU first. */
691 ret = mlx5_set_mtu(dev, mtu);
694 ret = mlx5_get_mtu(dev, &kern_mtu);
697 if (kern_mtu == mtu) {
699 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
700 dev->data->port_id, mtu);
708 * DPDK callback to get flow control status.
711 * Pointer to Ethernet device structure.
712 * @param[out] fc_conf
713 * Flow control output buffer.
716 * 0 on success, a negative errno value otherwise and rte_errno is set.
719 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
722 struct ethtool_pauseparam ethpause = {
723 .cmd = ETHTOOL_GPAUSEPARAM
727 ifr.ifr_data = (void *)ðpause;
728 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
731 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
733 dev->data->port_id, strerror(rte_errno));
736 fc_conf->autoneg = ethpause.autoneg;
737 if (ethpause.rx_pause && ethpause.tx_pause)
738 fc_conf->mode = RTE_FC_FULL;
739 else if (ethpause.rx_pause)
740 fc_conf->mode = RTE_FC_RX_PAUSE;
741 else if (ethpause.tx_pause)
742 fc_conf->mode = RTE_FC_TX_PAUSE;
744 fc_conf->mode = RTE_FC_NONE;
749 * DPDK callback to modify flow control parameters.
752 * Pointer to Ethernet device structure.
754 * Flow control parameters.
757 * 0 on success, a negative errno value otherwise and rte_errno is set.
760 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
763 struct ethtool_pauseparam ethpause = {
764 .cmd = ETHTOOL_SPAUSEPARAM
768 ifr.ifr_data = (void *)ðpause;
769 ethpause.autoneg = fc_conf->autoneg;
770 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
771 (fc_conf->mode & RTE_FC_RX_PAUSE))
772 ethpause.rx_pause = 1;
774 ethpause.rx_pause = 0;
776 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
777 (fc_conf->mode & RTE_FC_TX_PAUSE))
778 ethpause.tx_pause = 1;
780 ethpause.tx_pause = 0;
781 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
784 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
786 dev->data->port_id, strerror(rte_errno));
793 * Get PCI information from struct ibv_device.
796 * Pointer to Ethernet device structure.
797 * @param[out] pci_addr
798 * PCI bus address output buffer.
801 * 0 on success, a negative errno value otherwise and rte_errno is set.
804 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
805 struct rte_pci_addr *pci_addr)
809 MKSTR(path, "%s/device/uevent", device->ibdev_path);
811 file = fopen(path, "rb");
816 while (fgets(line, sizeof(line), file) == line) {
817 size_t len = strlen(line);
820 /* Truncate long lines. */
821 if (len == (sizeof(line) - 1))
822 while (line[(len - 1)] != '\n') {
826 line[(len - 1)] = ret;
828 /* Extract information. */
831 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
835 &pci_addr->function) == 4) {
845 * Update the link status.
848 * Pointer to Ethernet device.
851 * Zero if the callback process can be called immediately, negative errno
852 * value otherwise and rte_errno is set.
855 mlx5_link_status_update(struct rte_eth_dev *dev)
857 struct priv *priv = dev->data->dev_private;
858 struct rte_eth_link *link = &dev->data->dev_link;
861 ret = mlx5_link_update(dev, 0);
864 if (((link->link_speed == 0) && link->link_status) ||
865 ((link->link_speed != 0) && !link->link_status)) {
867 * Inconsistent status. Event likely occurred before the
868 * kernel netdevice exposes the new status.
870 if (!priv->pending_alarm) {
871 priv->pending_alarm = 1;
872 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
873 mlx5_dev_link_status_handler,
877 } else if (unlikely(priv->pending_alarm)) {
878 /* Link interrupt occurred while alarm is already scheduled. */
879 priv->pending_alarm = 0;
880 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
886 * Device status handler.
889 * Pointer to Ethernet device.
891 * Pointer to event flags holder.
894 * Events bitmap of callback process which can be called immediately.
897 mlx5_dev_status_handler(struct rte_eth_dev *dev)
899 struct priv *priv = dev->data->dev_private;
900 struct ibv_async_event event;
903 /* Read all message and acknowledge them. */
905 if (mlx5_glue->get_async_event(priv->ctx, &event))
907 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
908 event.event_type == IBV_EVENT_PORT_ERR) &&
909 (dev->data->dev_conf.intr_conf.lsc == 1))
910 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
911 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
912 dev->data->dev_conf.intr_conf.rmv == 1)
913 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
916 "port %u event type %d on not handled",
917 dev->data->port_id, event.event_type);
918 mlx5_glue->ack_async_event(&event);
920 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
921 if (mlx5_link_status_update(dev))
922 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
927 * Handle delayed link status event.
930 * Registered argument.
933 mlx5_dev_link_status_handler(void *arg)
935 struct rte_eth_dev *dev = arg;
936 struct priv *priv = dev->data->dev_private;
939 priv->pending_alarm = 0;
940 ret = mlx5_link_status_update(dev);
942 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
946 * Handle interrupts from the NIC.
948 * @param[in] intr_handle
954 mlx5_dev_interrupt_handler(void *cb_arg)
956 struct rte_eth_dev *dev = cb_arg;
959 events = mlx5_dev_status_handler(dev);
960 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
961 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
962 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
963 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
967 * Handle interrupts from the socket.
973 mlx5_dev_handler_socket(void *cb_arg)
975 struct rte_eth_dev *dev = cb_arg;
977 mlx5_socket_handle(dev);
981 * Uninstall interrupt handler.
984 * Pointer to Ethernet device.
987 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
989 struct priv *priv = dev->data->dev_private;
991 if (dev->data->dev_conf.intr_conf.lsc ||
992 dev->data->dev_conf.intr_conf.rmv)
993 rte_intr_callback_unregister(&priv->intr_handle,
994 mlx5_dev_interrupt_handler, dev);
995 if (priv->primary_socket)
996 rte_intr_callback_unregister(&priv->intr_handle_socket,
997 mlx5_dev_handler_socket, dev);
998 if (priv->pending_alarm) {
999 priv->pending_alarm = 0;
1000 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1002 priv->intr_handle.fd = 0;
1003 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1004 priv->intr_handle_socket.fd = 0;
1005 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1009 * Install interrupt handler.
1012 * Pointer to Ethernet device.
1015 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1017 struct priv *priv = dev->data->dev_private;
1021 assert(priv->ctx->async_fd > 0);
1022 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1023 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1026 "port %u failed to change file descriptor async event"
1028 dev->data->port_id);
1029 dev->data->dev_conf.intr_conf.lsc = 0;
1030 dev->data->dev_conf.intr_conf.rmv = 0;
1032 if (dev->data->dev_conf.intr_conf.lsc ||
1033 dev->data->dev_conf.intr_conf.rmv) {
1034 priv->intr_handle.fd = priv->ctx->async_fd;
1035 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1036 rte_intr_callback_register(&priv->intr_handle,
1037 mlx5_dev_interrupt_handler, dev);
1039 ret = mlx5_socket_init(dev);
1041 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1042 dev->data->port_id, strerror(rte_errno));
1043 else if (priv->primary_socket) {
1044 priv->intr_handle_socket.fd = priv->primary_socket;
1045 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1046 rte_intr_callback_register(&priv->intr_handle_socket,
1047 mlx5_dev_handler_socket, dev);
1052 * DPDK callback to bring the link DOWN.
1055 * Pointer to Ethernet device structure.
1058 * 0 on success, a negative errno value otherwise and rte_errno is set.
1061 mlx5_set_link_down(struct rte_eth_dev *dev)
1063 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1067 * DPDK callback to bring the link UP.
1070 * Pointer to Ethernet device structure.
1073 * 0 on success, a negative errno value otherwise and rte_errno is set.
1076 mlx5_set_link_up(struct rte_eth_dev *dev)
1078 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1082 * Configure the TX function to use.
1085 * Pointer to private data structure.
1088 * Pointer to selected Tx burst function.
1091 mlx5_select_tx_function(struct rte_eth_dev *dev)
1093 struct priv *priv = dev->data->dev_private;
1094 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1095 struct mlx5_dev_config *config = &priv->config;
1096 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1097 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1098 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1099 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1100 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1102 assert(priv != NULL);
1103 /* Select appropriate TX function. */
1104 if (vlan_insert || tso)
1105 return tx_pkt_burst;
1106 if (config->mps == MLX5_MPW_ENHANCED) {
1107 if (mlx5_check_vec_tx_support(dev) > 0) {
1108 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1109 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1111 tx_pkt_burst = mlx5_tx_burst_vec;
1113 "port %u selected enhanced MPW Tx vectorized"
1115 dev->data->port_id);
1117 tx_pkt_burst = mlx5_tx_burst_empw;
1119 "port %u selected enhanced MPW Tx function",
1120 dev->data->port_id);
1122 } else if (config->mps && (config->txq_inline > 0)) {
1123 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1124 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1125 dev->data->port_id);
1126 } else if (config->mps) {
1127 tx_pkt_burst = mlx5_tx_burst_mpw;
1128 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1129 dev->data->port_id);
1131 return tx_pkt_burst;
1135 * Configure the RX function to use.
1138 * Pointer to private data structure.
1141 * Pointer to selected Rx burst function.
1144 mlx5_select_rx_function(struct rte_eth_dev *dev)
1146 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1148 assert(dev != NULL);
1149 if (mlx5_check_vec_rx_support(dev) > 0) {
1150 rx_pkt_burst = mlx5_rx_burst_vec;
1151 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1152 dev->data->port_id);
1154 return rx_pkt_burst;
1158 * Check if mlx5 device was removed.
1161 * Pointer to Ethernet device structure.
1164 * 1 when device is removed, otherwise 0.
1167 mlx5_is_removed(struct rte_eth_dev *dev)
1169 struct ibv_device_attr device_attr;
1170 struct priv *priv = dev->data->dev_private;
1172 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)