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 private structure.
101 * Interface name output buffer.
104 * 0 on success, -1 on failure and errno is set.
107 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
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);
122 while ((dent = readdir(dir)) != NULL) {
123 char *name = dent->d_name;
125 unsigned int dev_port;
128 if ((name[0] == '.') &&
129 ((name[1] == '\0') ||
130 ((name[1] == '.') && (name[2] == '\0'))))
133 MKSTR(path, "%s/device/net/%s/%s",
134 priv->ibdev_path, name,
135 (dev_type ? "dev_id" : "dev_port"));
137 file = fopen(path, "rb");
142 * Switch to dev_id when dev_port does not exist as
143 * is the case with Linux kernel versions < 3.15.
154 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
159 * Switch to dev_id when dev_port returns the same value for
160 * all ports. May happen when using a MOFED release older than
161 * 3.0 with a Linux kernel >= 3.15.
163 if (dev_port == dev_port_prev)
165 dev_port_prev = dev_port;
166 if (dev_port == (priv->port - 1u))
167 snprintf(match, sizeof(match), "%s", name);
170 if (match[0] == '\0')
172 strncpy(*ifname, match, sizeof(*ifname));
177 * Perform ifreq ioctl() on associated Ethernet device.
180 * Pointer to private structure.
182 * Request number to pass to ioctl().
184 * Interface request structure output buffer.
187 * 0 on success, -1 on failure and errno is set.
190 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
192 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
197 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
198 ret = ioctl(sock, req, ifr);
207 * Pointer to private structure.
209 * MTU value output buffer.
212 * 0 on success, -1 on failure and errno is set.
215 priv_get_mtu(struct priv *priv, uint16_t *mtu)
217 struct ifreq request;
218 int ret = priv_ifreq(priv, SIOCGIFMTU, &request);
222 *mtu = request.ifr_mtu;
230 * Pointer to private structure.
235 * 0 on success, -1 on failure and errno is set.
238 priv_set_mtu(struct priv *priv, uint16_t mtu)
240 struct ifreq request = { .ifr_mtu = mtu, };
242 return priv_ifreq(priv, SIOCSIFMTU, &request);
249 * Pointer to private structure.
251 * Bitmask for flags that must remain untouched.
253 * Bitmask for flags to modify.
256 * 0 on success, -1 on failure and errno is set.
259 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
261 struct ifreq request;
262 int ret = priv_ifreq(priv, SIOCGIFFLAGS, &request);
266 request.ifr_flags &= keep;
267 request.ifr_flags |= flags & ~keep;
268 return priv_ifreq(priv, SIOCSIFFLAGS, &request);
272 * Ethernet device configuration.
274 * Prepare the driver for a given number of TX and RX queues.
277 * Pointer to Ethernet device structure.
280 * 0 on success, errno value on failure.
283 dev_configure(struct rte_eth_dev *dev)
285 struct priv *priv = dev->data->dev_private;
286 unsigned int rxqs_n = dev->data->nb_rx_queues;
287 unsigned int txqs_n = dev->data->nb_tx_queues;
290 unsigned int reta_idx_n;
291 const uint8_t use_app_rss_key =
292 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
293 uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
294 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
295 uint64_t supp_rx_offloads =
296 (mlx5_priv_get_rx_port_offloads(priv) |
297 mlx5_priv_get_rx_queue_offloads(priv));
298 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
300 if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
301 ERROR("Some Tx offloads are not supported "
302 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
303 tx_offloads, supp_tx_offloads);
306 if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
307 ERROR("Some Rx offloads are not supported "
308 "requested 0x%" PRIx64 " supported 0x%" PRIx64,
309 rx_offloads, supp_rx_offloads);
312 if (use_app_rss_key &&
313 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
314 rss_hash_default_key_len)) {
315 /* MLX5 RSS only support 40bytes key. */
318 priv->rss_conf.rss_key =
319 rte_realloc(priv->rss_conf.rss_key,
320 rss_hash_default_key_len, 0);
321 if (!priv->rss_conf.rss_key) {
322 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
325 memcpy(priv->rss_conf.rss_key,
327 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
328 rss_hash_default_key,
329 rss_hash_default_key_len);
330 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
331 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
332 priv->rxqs = (void *)dev->data->rx_queues;
333 priv->txqs = (void *)dev->data->tx_queues;
334 if (txqs_n != priv->txqs_n) {
335 INFO("%p: TX queues number update: %u -> %u",
336 (void *)dev, priv->txqs_n, txqs_n);
337 priv->txqs_n = txqs_n;
339 if (rxqs_n > priv->config.ind_table_max_size) {
340 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
343 if (rxqs_n == priv->rxqs_n)
345 INFO("%p: RX queues number update: %u -> %u",
346 (void *)dev, priv->rxqs_n, rxqs_n);
347 priv->rxqs_n = rxqs_n;
348 /* If the requested number of RX queues is not a power of two, use the
349 * maximum indirection table size for better balancing.
350 * The result is always rounded to the next power of two. */
351 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
352 priv->config.ind_table_max_size :
354 if (priv_rss_reta_index_resize(priv, reta_idx_n))
356 /* When the number of RX queues is not a power of two, the remaining
357 * table entries are padded with reused WQs and hashes are not spread
359 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
360 (*priv->reta_idx)[i] = j;
368 * DPDK callback for Ethernet device configuration.
371 * Pointer to Ethernet device structure.
374 * 0 on success, negative errno value on failure.
377 mlx5_dev_configure(struct rte_eth_dev *dev)
379 struct priv *priv = dev->data->dev_private;
383 ret = dev_configure(dev);
390 * DPDK callback to get information about the device.
393 * Pointer to Ethernet device structure.
395 * Info structure output buffer.
398 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
400 struct priv *priv = dev->data->dev_private;
401 struct mlx5_dev_config *config = &priv->config;
403 char ifname[IF_NAMESIZE];
405 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
408 /* FIXME: we should ask the device for these values. */
409 info->min_rx_bufsize = 32;
410 info->max_rx_pktlen = 65536;
412 * Since we need one CQ per QP, the limit is the minimum number
413 * between the two values.
415 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
416 priv->device_attr.orig_attr.max_qp);
417 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
420 info->max_rx_queues = max;
421 info->max_tx_queues = max;
422 info->max_mac_addrs = RTE_DIM(priv->mac);
423 info->rx_queue_offload_capa =
424 mlx5_priv_get_rx_queue_offloads(priv);
425 info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
426 info->rx_queue_offload_capa);
427 info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
428 if (priv_get_ifname(priv, &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 = priv->rss_conf.rss_key_len;
433 info->speed_capa = priv->link_speed_capa;
434 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
439 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
441 static const uint32_t ptypes[] = {
442 /* refers to rxq_cq_to_pkt_type() */
444 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
445 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
446 RTE_PTYPE_L4_NONFRAG,
450 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
451 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
452 RTE_PTYPE_INNER_L4_NONFRAG,
453 RTE_PTYPE_INNER_L4_FRAG,
454 RTE_PTYPE_INNER_L4_TCP,
455 RTE_PTYPE_INNER_L4_UDP,
459 if (dev->rx_pkt_burst == mlx5_rx_burst ||
460 dev->rx_pkt_burst == mlx5_rx_burst_vec)
466 * DPDK callback to retrieve physical link information.
469 * Pointer to Ethernet device structure.
472 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev)
474 struct priv *priv = dev->data->dev_private;
475 struct ethtool_cmd edata = {
476 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
479 struct rte_eth_link dev_link;
482 /* priv_lock() is not taken to allow concurrent calls. */
484 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
485 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
488 memset(&dev_link, 0, sizeof(dev_link));
489 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
490 (ifr.ifr_flags & IFF_RUNNING));
491 ifr.ifr_data = (void *)&edata;
492 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
493 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
497 link_speed = ethtool_cmd_speed(&edata);
498 if (link_speed == -1)
499 dev_link.link_speed = 0;
501 dev_link.link_speed = link_speed;
502 priv->link_speed_capa = 0;
503 if (edata.supported & SUPPORTED_Autoneg)
504 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
505 if (edata.supported & (SUPPORTED_1000baseT_Full |
506 SUPPORTED_1000baseKX_Full))
507 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
508 if (edata.supported & SUPPORTED_10000baseKR_Full)
509 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
510 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
511 SUPPORTED_40000baseCR4_Full |
512 SUPPORTED_40000baseSR4_Full |
513 SUPPORTED_40000baseLR4_Full))
514 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
515 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
516 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
517 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
518 ETH_LINK_SPEED_FIXED);
519 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
520 /* Link status changed. */
521 dev->data->dev_link = dev_link;
524 /* Link status is still the same. */
529 * Retrieve physical link information (unlocked version using new ioctl).
532 * Pointer to Ethernet device structure.
535 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev)
537 struct priv *priv = dev->data->dev_private;
538 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
540 struct rte_eth_link dev_link;
543 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
544 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
547 memset(&dev_link, 0, sizeof(dev_link));
548 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
549 (ifr.ifr_flags & IFF_RUNNING));
550 ifr.ifr_data = (void *)&gcmd;
551 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
552 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
556 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
558 alignas(struct ethtool_link_settings)
559 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
560 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
561 struct ethtool_link_settings *ecmd = (void *)data;
564 ifr.ifr_data = (void *)ecmd;
565 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
566 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
570 dev_link.link_speed = ecmd->speed;
571 sc = ecmd->link_mode_masks[0] |
572 ((uint64_t)ecmd->link_mode_masks[1] << 32);
573 priv->link_speed_capa = 0;
574 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
575 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
576 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
577 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
578 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
579 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
580 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
581 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
582 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
583 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
584 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
585 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
586 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
587 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
588 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
589 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
590 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
591 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
592 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
593 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
594 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
595 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
596 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
597 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
598 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
599 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
600 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
601 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
602 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
603 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
604 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
605 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
606 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
607 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
608 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
609 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
610 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
611 ETH_LINK_SPEED_FIXED);
612 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
613 /* Link status changed. */
614 dev->data->dev_link = dev_link;
617 /* Link status is still the same. */
622 * Enable receiving and transmitting traffic.
625 * Pointer to private structure.
628 priv_link_start(struct priv *priv)
630 struct rte_eth_dev *dev = priv->dev;
633 dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
634 dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
635 err = priv_dev_traffic_enable(priv, dev);
637 ERROR("%p: error occurred while configuring control flows: %s",
638 (void *)priv, strerror(err));
639 err = priv_flow_start(priv, &priv->flows);
641 ERROR("%p: error occurred while configuring flows: %s",
642 (void *)priv, strerror(err));
646 * Disable receiving and transmitting traffic.
649 * Pointer to private structure.
652 priv_link_stop(struct priv *priv)
654 struct rte_eth_dev *dev = priv->dev;
656 priv_flow_stop(priv, &priv->flows);
657 priv_dev_traffic_disable(priv, dev);
658 dev->rx_pkt_burst = removed_rx_burst;
659 dev->tx_pkt_burst = removed_tx_burst;
663 * Retrieve physical link information and update rx/tx_pkt_burst callbacks
667 * Pointer to private structure.
668 * @param wait_to_complete
669 * Wait for request completion (ignored).
672 priv_link_update(struct priv *priv, int wait_to_complete __rte_unused)
674 struct rte_eth_dev *dev = priv->dev;
675 struct utsname utsname;
678 struct rte_eth_link dev_link = dev->data->dev_link;
680 if (uname(&utsname) == -1 ||
681 sscanf(utsname.release, "%d.%d.%d",
682 &ver[0], &ver[1], &ver[2]) != 3 ||
683 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
684 ret = mlx5_link_update_unlocked_gset(dev);
686 ret = mlx5_link_update_unlocked_gs(dev);
687 /* If lsc interrupt is disabled, should always be ready for traffic. */
688 if (!dev->data->dev_conf.intr_conf.lsc) {
689 priv_link_start(priv);
692 /* Re-select burst callbacks only if link status has been changed. */
693 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
694 if (dev->data->dev_link.link_status == ETH_LINK_UP)
695 priv_link_start(priv);
697 priv_link_stop(priv);
703 * Querying the link status till it changes to the desired state.
704 * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS.
707 * Pointer to private structure.
709 * Link desired status.
712 * 0 on success, negative errno value on failure.
715 priv_force_link_status_change(struct priv *priv, int status)
719 while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) {
720 priv_link_update(priv, 0);
721 if (priv->dev->data->dev_link.link_status == status)
730 * DPDK callback to retrieve physical link information.
733 * Pointer to Ethernet device structure.
734 * @param wait_to_complete
735 * Wait for request completion (ignored).
738 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
740 struct priv *priv = dev->data->dev_private;
744 ret = priv_link_update(priv, wait_to_complete);
750 * DPDK callback to change the MTU.
753 * Pointer to Ethernet device structure.
758 * 0 on success, negative errno value on failure.
761 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
763 struct priv *priv = dev->data->dev_private;
768 ret = priv_get_mtu(priv, &kern_mtu);
771 /* Set kernel interface MTU first. */
772 ret = priv_set_mtu(priv, mtu);
775 ret = priv_get_mtu(priv, &kern_mtu);
778 if (kern_mtu == mtu) {
780 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
786 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
794 * DPDK callback to get flow control status.
797 * Pointer to Ethernet device structure.
798 * @param[out] fc_conf
799 * Flow control output buffer.
802 * 0 on success, negative errno value on failure.
805 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
807 struct priv *priv = dev->data->dev_private;
809 struct ethtool_pauseparam ethpause = {
810 .cmd = ETHTOOL_GPAUSEPARAM
814 ifr.ifr_data = (void *)ðpause;
816 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
818 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
824 fc_conf->autoneg = ethpause.autoneg;
825 if (ethpause.rx_pause && ethpause.tx_pause)
826 fc_conf->mode = RTE_FC_FULL;
827 else if (ethpause.rx_pause)
828 fc_conf->mode = RTE_FC_RX_PAUSE;
829 else if (ethpause.tx_pause)
830 fc_conf->mode = RTE_FC_TX_PAUSE;
832 fc_conf->mode = RTE_FC_NONE;
842 * DPDK callback to modify flow control parameters.
845 * Pointer to Ethernet device structure.
847 * Flow control parameters.
850 * 0 on success, negative errno value on failure.
853 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
855 struct priv *priv = dev->data->dev_private;
857 struct ethtool_pauseparam ethpause = {
858 .cmd = ETHTOOL_SPAUSEPARAM
862 ifr.ifr_data = (void *)ðpause;
863 ethpause.autoneg = fc_conf->autoneg;
864 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
865 (fc_conf->mode & RTE_FC_RX_PAUSE))
866 ethpause.rx_pause = 1;
868 ethpause.rx_pause = 0;
870 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
871 (fc_conf->mode & RTE_FC_TX_PAUSE))
872 ethpause.tx_pause = 1;
874 ethpause.tx_pause = 0;
877 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
879 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
893 * Get PCI information from struct ibv_device.
896 * Pointer to Ethernet device structure.
897 * @param[out] pci_addr
898 * PCI bus address output buffer.
901 * 0 on success, -1 on failure and errno is set.
904 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
905 struct rte_pci_addr *pci_addr)
909 MKSTR(path, "%s/device/uevent", device->ibdev_path);
911 file = fopen(path, "rb");
914 while (fgets(line, sizeof(line), file) == line) {
915 size_t len = strlen(line);
918 /* Truncate long lines. */
919 if (len == (sizeof(line) - 1))
920 while (line[(len - 1)] != '\n') {
924 line[(len - 1)] = ret;
926 /* Extract information. */
929 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
933 &pci_addr->function) == 4) {
943 * Update the link status.
946 * Pointer to private structure.
949 * Zero if the callback process can be called immediately.
952 priv_link_status_update(struct priv *priv)
954 struct rte_eth_link *link = &priv->dev->data->dev_link;
956 priv_link_update(priv, 0);
957 if (((link->link_speed == 0) && link->link_status) ||
958 ((link->link_speed != 0) && !link->link_status)) {
960 * Inconsistent status. Event likely occurred before the
961 * kernel netdevice exposes the new status.
963 if (!priv->pending_alarm) {
964 priv->pending_alarm = 1;
965 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
966 mlx5_dev_link_status_handler,
970 } else if (unlikely(priv->pending_alarm)) {
971 /* Link interrupt occurred while alarm is already scheduled. */
972 priv->pending_alarm = 0;
973 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
979 * Device status handler.
982 * Pointer to private structure.
984 * Pointer to event flags holder.
987 * Events bitmap of callback process which can be called immediately.
990 priv_dev_status_handler(struct priv *priv)
992 struct ibv_async_event event;
995 /* Read all message and acknowledge them. */
997 if (mlx5_glue->get_async_event(priv->ctx, &event))
999 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1000 event.event_type == IBV_EVENT_PORT_ERR) &&
1001 (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1002 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1003 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1004 priv->dev->data->dev_conf.intr_conf.rmv == 1)
1005 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1007 DEBUG("event type %d on port %d not handled",
1008 event.event_type, event.element.port_num);
1009 mlx5_glue->ack_async_event(&event);
1011 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1012 if (priv_link_status_update(priv))
1013 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1018 * Handle delayed link status event.
1021 * Registered argument.
1024 mlx5_dev_link_status_handler(void *arg)
1026 struct rte_eth_dev *dev = arg;
1027 struct priv *priv = dev->data->dev_private;
1030 while (!priv_trylock(priv)) {
1031 /* Alarm is being canceled. */
1032 if (priv->pending_alarm == 0)
1036 priv->pending_alarm = 0;
1037 ret = priv_link_status_update(priv);
1040 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1044 * Handle interrupts from the NIC.
1046 * @param[in] intr_handle
1047 * Interrupt handler.
1049 * Callback argument.
1052 mlx5_dev_interrupt_handler(void *cb_arg)
1054 struct rte_eth_dev *dev = cb_arg;
1055 struct priv *priv = dev->data->dev_private;
1059 events = priv_dev_status_handler(priv);
1061 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1062 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1063 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1064 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1068 * Handle interrupts from the socket.
1071 * Callback argument.
1074 mlx5_dev_handler_socket(void *cb_arg)
1076 struct rte_eth_dev *dev = cb_arg;
1077 struct priv *priv = dev->data->dev_private;
1080 priv_socket_handle(priv);
1085 * Uninstall interrupt handler.
1088 * Pointer to private structure.
1090 * Pointer to the rte_eth_dev structure.
1093 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1095 if (dev->data->dev_conf.intr_conf.lsc ||
1096 dev->data->dev_conf.intr_conf.rmv)
1097 rte_intr_callback_unregister(&priv->intr_handle,
1098 mlx5_dev_interrupt_handler, dev);
1099 if (priv->primary_socket)
1100 rte_intr_callback_unregister(&priv->intr_handle_socket,
1101 mlx5_dev_handler_socket, dev);
1102 if (priv->pending_alarm) {
1103 priv->pending_alarm = 0;
1104 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1106 priv->intr_handle.fd = 0;
1107 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1108 priv->intr_handle_socket.fd = 0;
1109 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1113 * Install interrupt handler.
1116 * Pointer to private structure.
1118 * Pointer to the rte_eth_dev structure.
1121 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1125 assert(priv->ctx->async_fd > 0);
1126 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1127 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1129 INFO("failed to change file descriptor async event queue");
1130 dev->data->dev_conf.intr_conf.lsc = 0;
1131 dev->data->dev_conf.intr_conf.rmv = 0;
1133 if (dev->data->dev_conf.intr_conf.lsc ||
1134 dev->data->dev_conf.intr_conf.rmv) {
1135 priv->intr_handle.fd = priv->ctx->async_fd;
1136 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1137 rte_intr_callback_register(&priv->intr_handle,
1138 mlx5_dev_interrupt_handler, dev);
1141 rc = priv_socket_init(priv);
1142 if (!rc && priv->primary_socket) {
1143 priv->intr_handle_socket.fd = priv->primary_socket;
1144 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1145 rte_intr_callback_register(&priv->intr_handle_socket,
1146 mlx5_dev_handler_socket, dev);
1151 * Change the link state (UP / DOWN).
1154 * Pointer to private data structure.
1156 * Nonzero for link up, otherwise link down.
1159 * 0 on success, errno value on failure.
1162 priv_dev_set_link(struct priv *priv, int up)
1164 return priv_set_flags(priv, ~IFF_UP, up ? IFF_UP : ~IFF_UP);
1168 * DPDK callback to bring the link DOWN.
1171 * Pointer to Ethernet device structure.
1174 * 0 on success, errno value on failure.
1177 mlx5_set_link_down(struct rte_eth_dev *dev)
1179 struct priv *priv = dev->data->dev_private;
1183 err = priv_dev_set_link(priv, 0);
1189 * DPDK callback to bring the link UP.
1192 * Pointer to Ethernet device structure.
1195 * 0 on success, errno value on failure.
1198 mlx5_set_link_up(struct rte_eth_dev *dev)
1200 struct priv *priv = dev->data->dev_private;
1204 err = priv_dev_set_link(priv, 1);
1210 * Configure the TX function to use.
1213 * Pointer to private data structure.
1215 * Pointer to rte_eth_dev structure.
1218 * Pointer to selected Tx burst function.
1221 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1223 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1224 struct mlx5_dev_config *config = &priv->config;
1225 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1226 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1227 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1228 DEV_TX_OFFLOAD_GRE_TNL_TSO));
1229 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1231 assert(priv != NULL);
1232 /* Select appropriate TX function. */
1233 if (vlan_insert || tso)
1234 return tx_pkt_burst;
1235 if (config->mps == MLX5_MPW_ENHANCED) {
1236 if (priv_check_vec_tx_support(priv, dev) > 0) {
1237 if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1238 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1240 tx_pkt_burst = mlx5_tx_burst_vec;
1241 DEBUG("selected Enhanced MPW TX vectorized function");
1243 tx_pkt_burst = mlx5_tx_burst_empw;
1244 DEBUG("selected Enhanced MPW TX function");
1246 } else if (config->mps && (config->txq_inline > 0)) {
1247 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1248 DEBUG("selected MPW inline TX function");
1249 } else if (config->mps) {
1250 tx_pkt_burst = mlx5_tx_burst_mpw;
1251 DEBUG("selected MPW TX function");
1253 return tx_pkt_burst;
1257 * Configure the RX function to use.
1260 * Pointer to private data structure.
1262 * Pointer to rte_eth_dev structure.
1265 * Pointer to selected Rx burst function.
1268 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1270 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1272 assert(priv != NULL);
1273 if (priv_check_vec_rx_support(priv) > 0) {
1274 rx_pkt_burst = mlx5_rx_burst_vec;
1275 DEBUG("selected RX vectorized function");
1277 return rx_pkt_burst;
1281 * Check if mlx5 device was removed.
1284 * Pointer to Ethernet device structure.
1287 * 1 when device is removed, otherwise 0.
1290 mlx5_is_removed(struct rte_eth_dev *dev)
1292 struct ibv_device_attr device_attr;
1293 struct priv *priv = dev->data->dev_private;
1295 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)