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>
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>
39 #include "mlx5_glue.h"
40 #include "mlx5_rxtx.h"
41 #include "mlx5_utils.h"
43 /* Add defines in case the running kernel is not the same as user headers. */
44 #ifndef ETHTOOL_GLINKSETTINGS
45 struct ethtool_link_settings {
54 uint8_t eth_tp_mdix_ctrl;
55 int8_t link_mode_masks_nwords;
57 uint32_t link_mode_masks[];
60 #define ETHTOOL_GLINKSETTINGS 0x0000004c
61 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
62 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
63 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
64 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
65 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
66 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
67 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
68 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
69 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
70 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
71 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
72 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
73 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
74 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
75 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
76 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
78 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
79 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
80 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
81 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
83 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
84 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
85 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
87 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
88 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
89 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
90 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
91 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
95 * Get interface name from private structure.
98 * Pointer to Ethernet device.
100 * Interface name output buffer.
103 * 0 on success, a negative errno value otherwise and rte_errno is set.
106 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
108 struct priv *priv = dev->data->dev_private;
111 unsigned int dev_type = 0;
112 unsigned int dev_port_prev = ~0u;
113 char match[IF_NAMESIZE] = "";
116 MKSTR(path, "%s/device/net", priv->ibdev_path);
124 while ((dent = readdir(dir)) != NULL) {
125 char *name = dent->d_name;
127 unsigned int dev_port;
130 if ((name[0] == '.') &&
131 ((name[1] == '\0') ||
132 ((name[1] == '.') && (name[2] == '\0'))))
135 MKSTR(path, "%s/device/net/%s/%s",
136 priv->ibdev_path, name,
137 (dev_type ? "dev_id" : "dev_port"));
139 file = fopen(path, "rb");
144 * Switch to dev_id when dev_port does not exist as
145 * is the case with Linux kernel versions < 3.15.
156 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
161 * Switch to dev_id when dev_port returns the same value for
162 * all ports. May happen when using a MOFED release older than
163 * 3.0 with a Linux kernel >= 3.15.
165 if (dev_port == dev_port_prev)
167 dev_port_prev = dev_port;
168 if (dev_port == (priv->port - 1u))
169 strlcpy(match, name, sizeof(match));
172 if (match[0] == '\0') {
176 strncpy(*ifname, match, sizeof(*ifname));
181 * Perform ifreq ioctl() on associated Ethernet device.
184 * Pointer to Ethernet device.
186 * Request number to pass to ioctl().
188 * Interface request structure output buffer.
191 * 0 on success, a negative errno value otherwise and rte_errno is set.
194 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
196 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
203 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
206 ret = ioctl(sock, req, ifr);
222 * Pointer to Ethernet device.
224 * MTU value output buffer.
227 * 0 on success, a negative errno value otherwise and rte_errno is set.
230 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
232 struct ifreq request;
233 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
237 *mtu = request.ifr_mtu;
245 * Pointer to Ethernet device.
250 * 0 on success, a negative errno value otherwise and rte_errno is set.
253 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
255 struct ifreq request = { .ifr_mtu = mtu, };
257 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
264 * Pointer to Ethernet device.
266 * Bitmask for flags that must remain untouched.
268 * Bitmask for flags to modify.
271 * 0 on success, a negative errno value otherwise and rte_errno is set.
274 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
276 struct ifreq request;
277 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
281 request.ifr_flags &= keep;
282 request.ifr_flags |= flags & ~keep;
283 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
287 * DPDK callback for Ethernet device configuration.
290 * Pointer to Ethernet device structure.
293 * 0 on success, a negative errno value otherwise and rte_errno is set.
296 mlx5_dev_configure(struct rte_eth_dev *dev)
298 struct priv *priv = dev->data->dev_private;
299 unsigned int rxqs_n = dev->data->nb_rx_queues;
300 unsigned int txqs_n = dev->data->nb_tx_queues;
303 unsigned int reta_idx_n;
304 const uint8_t use_app_rss_key =
305 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
306 uint64_t supp_tx_offloads = mlx5_get_tx_port_offloads(dev);
307 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
308 uint64_t supp_rx_offloads =
309 (mlx5_get_rx_port_offloads() |
310 mlx5_get_rx_queue_offloads(dev));
311 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
314 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
316 "port %u some Tx offloads are not supported requested"
317 " 0x%" PRIx64 " supported 0x%" PRIx64,
318 dev->data->port_id, tx_offloads, supp_tx_offloads);
322 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
324 "port %u some Rx offloads are not supported requested"
325 " 0x%" PRIx64 " supported 0x%" PRIx64,
326 dev->data->port_id, rx_offloads, supp_rx_offloads);
330 if (use_app_rss_key &&
331 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
332 rss_hash_default_key_len)) {
333 DRV_LOG(ERR, "port %u RSS key len must be %zu Bytes long",
334 dev->data->port_id, rss_hash_default_key_len);
338 priv->rss_conf.rss_key =
339 rte_realloc(priv->rss_conf.rss_key,
340 rss_hash_default_key_len, 0);
341 if (!priv->rss_conf.rss_key) {
342 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
343 dev->data->port_id, rxqs_n);
347 memcpy(priv->rss_conf.rss_key,
349 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
350 rss_hash_default_key,
351 rss_hash_default_key_len);
352 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
353 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
354 priv->rxqs = (void *)dev->data->rx_queues;
355 priv->txqs = (void *)dev->data->tx_queues;
356 if (txqs_n != priv->txqs_n) {
357 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
358 dev->data->port_id, priv->txqs_n, txqs_n);
359 priv->txqs_n = txqs_n;
361 if (rxqs_n > priv->config.ind_table_max_size) {
362 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
363 dev->data->port_id, rxqs_n);
367 if (rxqs_n == priv->rxqs_n)
369 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
370 dev->data->port_id, priv->rxqs_n, rxqs_n);
371 priv->rxqs_n = rxqs_n;
372 /* If the requested number of RX queues is not a power of two, use the
373 * maximum indirection table size for better balancing.
374 * The result is always rounded to the next power of two. */
375 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
376 priv->config.ind_table_max_size :
378 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
381 /* When the number of RX queues is not a power of two, the remaining
382 * table entries are padded with reused WQs and hashes are not spread
384 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
385 (*priv->reta_idx)[i] = j;
393 * DPDK callback to get information about the device.
396 * Pointer to Ethernet device structure.
398 * Info structure output buffer.
401 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
403 struct priv *priv = dev->data->dev_private;
404 struct mlx5_dev_config *config = &priv->config;
406 char ifname[IF_NAMESIZE];
408 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
409 /* FIXME: we should ask the device for these values. */
410 info->min_rx_bufsize = 32;
411 info->max_rx_pktlen = 65536;
413 * Since we need one CQ per QP, the limit is the minimum number
414 * between the two values.
416 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
417 priv->device_attr.orig_attr.max_qp);
418 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
421 info->max_rx_queues = max;
422 info->max_tx_queues = max;
423 info->max_mac_addrs = RTE_DIM(priv->mac);
424 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
425 info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
426 info->rx_queue_offload_capa);
427 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
428 if (mlx5_get_ifname(dev, &ifname) == 0)
429 info->if_index = if_nametoindex(ifname);
430 info->reta_size = priv->reta_idx_n ?
431 priv->reta_idx_n : config->ind_table_max_size;
432 info->hash_key_size = rss_hash_default_key_len;
433 info->speed_capa = priv->link_speed_capa;
434 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
438 * Get supported packet types.
441 * Pointer to Ethernet device structure.
444 * A pointer to the supported Packet types array.
447 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
449 static const uint32_t ptypes[] = {
450 /* refers to rxq_cq_to_pkt_type() */
452 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
453 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
454 RTE_PTYPE_L4_NONFRAG,
458 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
459 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
460 RTE_PTYPE_INNER_L4_NONFRAG,
461 RTE_PTYPE_INNER_L4_FRAG,
462 RTE_PTYPE_INNER_L4_TCP,
463 RTE_PTYPE_INNER_L4_UDP,
467 if (dev->rx_pkt_burst == mlx5_rx_burst ||
468 dev->rx_pkt_burst == mlx5_rx_burst_vec)
474 * DPDK callback to retrieve physical link information.
477 * Pointer to Ethernet device structure.
479 * Storage for current link status.
482 * 0 on success, a negative errno value otherwise and rte_errno is set.
485 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
486 struct rte_eth_link *link)
488 struct priv *priv = dev->data->dev_private;
489 struct ethtool_cmd edata = {
490 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
493 struct rte_eth_link dev_link;
497 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
499 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
500 dev->data->port_id, strerror(rte_errno));
503 memset(&dev_link, 0, sizeof(dev_link));
504 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
505 (ifr.ifr_flags & IFF_RUNNING));
506 ifr.ifr_data = (void *)&edata;
507 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
510 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
511 dev->data->port_id, strerror(rte_errno));
514 link_speed = ethtool_cmd_speed(&edata);
515 if (link_speed == -1)
516 dev_link.link_speed = 0;
518 dev_link.link_speed = link_speed;
519 priv->link_speed_capa = 0;
520 if (edata.supported & SUPPORTED_Autoneg)
521 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
522 if (edata.supported & (SUPPORTED_1000baseT_Full |
523 SUPPORTED_1000baseKX_Full))
524 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
525 if (edata.supported & SUPPORTED_10000baseKR_Full)
526 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
527 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
528 SUPPORTED_40000baseCR4_Full |
529 SUPPORTED_40000baseSR4_Full |
530 SUPPORTED_40000baseLR4_Full))
531 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
532 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
533 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
534 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
535 ETH_LINK_SPEED_FIXED);
536 if ((dev_link.link_speed && !dev_link.link_status) ||
537 (!dev_link.link_speed && dev_link.link_status)) {
546 * Retrieve physical link information (unlocked version using new ioctl).
549 * Pointer to Ethernet device structure.
551 * Storage for current link status.
554 * 0 on success, a negative errno value otherwise and rte_errno is set.
557 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
558 struct rte_eth_link *link)
561 struct priv *priv = dev->data->dev_private;
562 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
564 struct rte_eth_link dev_link;
568 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
570 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
571 dev->data->port_id, strerror(rte_errno));
574 memset(&dev_link, 0, sizeof(dev_link));
575 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
576 (ifr.ifr_flags & IFF_RUNNING));
577 ifr.ifr_data = (void *)&gcmd;
578 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
581 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
583 dev->data->port_id, strerror(rte_errno));
586 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
588 alignas(struct ethtool_link_settings)
589 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
590 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
591 struct ethtool_link_settings *ecmd = (void *)data;
594 ifr.ifr_data = (void *)ecmd;
595 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
598 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
600 dev->data->port_id, strerror(rte_errno));
603 dev_link.link_speed = ecmd->speed;
604 sc = ecmd->link_mode_masks[0] |
605 ((uint64_t)ecmd->link_mode_masks[1] << 32);
606 priv->link_speed_capa = 0;
607 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
608 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
609 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
610 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
611 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
612 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
613 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
614 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
615 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
616 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
617 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
618 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
619 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
620 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
621 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
622 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
623 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
624 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
625 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
626 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
627 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
628 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
629 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
630 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
631 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
632 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
633 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
634 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
635 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
636 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
637 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
638 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
639 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
640 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
641 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
642 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
643 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
644 ETH_LINK_SPEED_FIXED);
645 if ((dev_link.link_speed && !dev_link.link_status) ||
646 (!dev_link.link_speed && dev_link.link_status)) {
655 * DPDK callback to retrieve physical link information.
658 * Pointer to Ethernet device structure.
659 * @param wait_to_complete
660 * Wait for request completion.
663 * 0 if link status was not updated, positive if it was, a negative errno
664 * value otherwise and rte_errno is set.
667 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
670 struct rte_eth_link dev_link;
671 time_t start_time = time(NULL);
674 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
676 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
679 /* Handle wait to complete situation. */
680 if (wait_to_complete && ret == -EAGAIN) {
681 if (abs((int)difftime(time(NULL), start_time)) <
682 MLX5_LINK_STATUS_TIMEOUT) {
689 } else if (ret < 0) {
692 } while (wait_to_complete);
693 ret = !!memcmp(&dev->data->dev_link, &dev_link,
694 sizeof(struct rte_eth_link));
695 dev->data->dev_link = dev_link;
700 * DPDK callback to change the MTU.
703 * Pointer to Ethernet device structure.
708 * 0 on success, a negative errno value otherwise and rte_errno is set.
711 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
713 struct priv *priv = dev->data->dev_private;
714 uint16_t kern_mtu = 0;
717 ret = mlx5_get_mtu(dev, &kern_mtu);
720 /* Set kernel interface MTU first. */
721 ret = mlx5_set_mtu(dev, mtu);
724 ret = mlx5_get_mtu(dev, &kern_mtu);
727 if (kern_mtu == mtu) {
729 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
730 dev->data->port_id, mtu);
738 * DPDK callback to get flow control status.
741 * Pointer to Ethernet device structure.
742 * @param[out] fc_conf
743 * Flow control output buffer.
746 * 0 on success, a negative errno value otherwise and rte_errno is set.
749 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
752 struct ethtool_pauseparam ethpause = {
753 .cmd = ETHTOOL_GPAUSEPARAM
757 ifr.ifr_data = (void *)ðpause;
758 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
761 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
763 dev->data->port_id, strerror(rte_errno));
766 fc_conf->autoneg = ethpause.autoneg;
767 if (ethpause.rx_pause && ethpause.tx_pause)
768 fc_conf->mode = RTE_FC_FULL;
769 else if (ethpause.rx_pause)
770 fc_conf->mode = RTE_FC_RX_PAUSE;
771 else if (ethpause.tx_pause)
772 fc_conf->mode = RTE_FC_TX_PAUSE;
774 fc_conf->mode = RTE_FC_NONE;
779 * DPDK callback to modify flow control parameters.
782 * Pointer to Ethernet device structure.
784 * Flow control parameters.
787 * 0 on success, a negative errno value otherwise and rte_errno is set.
790 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
793 struct ethtool_pauseparam ethpause = {
794 .cmd = ETHTOOL_SPAUSEPARAM
798 ifr.ifr_data = (void *)ðpause;
799 ethpause.autoneg = fc_conf->autoneg;
800 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
801 (fc_conf->mode & RTE_FC_RX_PAUSE))
802 ethpause.rx_pause = 1;
804 ethpause.rx_pause = 0;
806 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
807 (fc_conf->mode & RTE_FC_TX_PAUSE))
808 ethpause.tx_pause = 1;
810 ethpause.tx_pause = 0;
811 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
814 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
816 dev->data->port_id, strerror(rte_errno));
823 * Get PCI information from struct ibv_device.
826 * Pointer to Ethernet device structure.
827 * @param[out] pci_addr
828 * PCI bus address output buffer.
831 * 0 on success, a negative errno value otherwise and rte_errno is set.
834 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
835 struct rte_pci_addr *pci_addr)
839 MKSTR(path, "%s/device/uevent", device->ibdev_path);
841 file = fopen(path, "rb");
846 while (fgets(line, sizeof(line), file) == line) {
847 size_t len = strlen(line);
850 /* Truncate long lines. */
851 if (len == (sizeof(line) - 1))
852 while (line[(len - 1)] != '\n') {
856 line[(len - 1)] = ret;
858 /* Extract information. */
861 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
865 &pci_addr->function) == 4) {
875 * Device status handler.
878 * Pointer to Ethernet device.
880 * Pointer to event flags holder.
883 * Events bitmap of callback process which can be called immediately.
886 mlx5_dev_status_handler(struct rte_eth_dev *dev)
888 struct priv *priv = dev->data->dev_private;
889 struct ibv_async_event event;
892 if (mlx5_link_update(dev, 0) == -EAGAIN) {
896 /* Read all message and acknowledge them. */
898 if (mlx5_glue->get_async_event(priv->ctx, &event))
900 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
901 event.event_type == IBV_EVENT_PORT_ERR) &&
902 (dev->data->dev_conf.intr_conf.lsc == 1))
903 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
904 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
905 dev->data->dev_conf.intr_conf.rmv == 1)
906 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
909 "port %u event type %d on not handled",
910 dev->data->port_id, event.event_type);
911 mlx5_glue->ack_async_event(&event);
917 * Handle interrupts from the NIC.
919 * @param[in] intr_handle
925 mlx5_dev_interrupt_handler(void *cb_arg)
927 struct rte_eth_dev *dev = cb_arg;
930 events = mlx5_dev_status_handler(dev);
931 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
932 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
933 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
934 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
938 * Handle interrupts from the socket.
944 mlx5_dev_handler_socket(void *cb_arg)
946 struct rte_eth_dev *dev = cb_arg;
948 mlx5_socket_handle(dev);
952 * Uninstall interrupt handler.
955 * Pointer to Ethernet device.
958 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
960 struct priv *priv = dev->data->dev_private;
962 if (dev->data->dev_conf.intr_conf.lsc ||
963 dev->data->dev_conf.intr_conf.rmv)
964 rte_intr_callback_unregister(&priv->intr_handle,
965 mlx5_dev_interrupt_handler, dev);
966 if (priv->primary_socket)
967 rte_intr_callback_unregister(&priv->intr_handle_socket,
968 mlx5_dev_handler_socket, dev);
969 priv->intr_handle.fd = 0;
970 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
971 priv->intr_handle_socket.fd = 0;
972 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
976 * Install interrupt handler.
979 * Pointer to Ethernet device.
982 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
984 struct priv *priv = dev->data->dev_private;
988 assert(priv->ctx->async_fd > 0);
989 flags = fcntl(priv->ctx->async_fd, F_GETFL);
990 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
993 "port %u failed to change file descriptor async event"
996 dev->data->dev_conf.intr_conf.lsc = 0;
997 dev->data->dev_conf.intr_conf.rmv = 0;
999 if (dev->data->dev_conf.intr_conf.lsc ||
1000 dev->data->dev_conf.intr_conf.rmv) {
1001 priv->intr_handle.fd = priv->ctx->async_fd;
1002 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1003 rte_intr_callback_register(&priv->intr_handle,
1004 mlx5_dev_interrupt_handler, dev);
1006 ret = mlx5_socket_init(dev);
1008 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1009 dev->data->port_id, strerror(rte_errno));
1010 else if (priv->primary_socket) {
1011 priv->intr_handle_socket.fd = priv->primary_socket;
1012 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1013 rte_intr_callback_register(&priv->intr_handle_socket,
1014 mlx5_dev_handler_socket, dev);
1019 * DPDK callback to bring the link DOWN.
1022 * Pointer to Ethernet device structure.
1025 * 0 on success, a negative errno value otherwise and rte_errno is set.
1028 mlx5_set_link_down(struct rte_eth_dev *dev)
1030 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1034 * DPDK callback to bring the link UP.
1037 * Pointer to Ethernet device structure.
1040 * 0 on success, a negative errno value otherwise and rte_errno is set.
1043 mlx5_set_link_up(struct rte_eth_dev *dev)
1045 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1049 * Configure the TX function to use.
1052 * Pointer to private data structure.
1055 * Pointer to selected Tx burst function.
1058 mlx5_select_tx_function(struct rte_eth_dev *dev)
1060 struct priv *priv = dev->data->dev_private;
1061 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1062 struct mlx5_dev_config *config = &priv->config;
1063 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1064 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1065 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1066 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1067 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1069 assert(priv != NULL);
1070 /* Select appropriate TX function. */
1071 if (vlan_insert || tso)
1072 return tx_pkt_burst;
1073 if (config->mps == MLX5_MPW_ENHANCED) {
1074 if (mlx5_check_vec_tx_support(dev) > 0) {
1075 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1076 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1078 tx_pkt_burst = mlx5_tx_burst_vec;
1080 "port %u selected enhanced MPW Tx vectorized"
1082 dev->data->port_id);
1084 tx_pkt_burst = mlx5_tx_burst_empw;
1086 "port %u selected enhanced MPW Tx function",
1087 dev->data->port_id);
1089 } else if (config->mps && (config->txq_inline > 0)) {
1090 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1091 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1092 dev->data->port_id);
1093 } else if (config->mps) {
1094 tx_pkt_burst = mlx5_tx_burst_mpw;
1095 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1096 dev->data->port_id);
1098 return tx_pkt_burst;
1102 * Configure the RX function to use.
1105 * Pointer to private data structure.
1108 * Pointer to selected Rx burst function.
1111 mlx5_select_rx_function(struct rte_eth_dev *dev)
1113 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1115 assert(dev != NULL);
1116 if (mlx5_check_vec_rx_support(dev) > 0) {
1117 rx_pkt_burst = mlx5_rx_burst_vec;
1118 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1119 dev->data->port_id);
1121 return rx_pkt_burst;
1125 * Check if mlx5 device was removed.
1128 * Pointer to Ethernet device structure.
1131 * 1 when device is removed, otherwise 0.
1134 mlx5_is_removed(struct rte_eth_dev *dev)
1136 struct ibv_device_attr device_attr;
1137 struct priv *priv = dev->data->dev_private;
1139 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)