4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <linux/ethtool.h>
51 #include <linux/sockios.h>
57 #include <rte_atomic.h>
58 #include <rte_ethdev.h>
59 #include <rte_bus_pci.h>
61 #include <rte_common.h>
62 #include <rte_interrupts.h>
63 #include <rte_malloc.h>
66 #include "mlx5_rxtx.h"
67 #include "mlx5_utils.h"
69 /* Add defines in case the running kernel is not the same as user headers. */
70 #ifndef ETHTOOL_GLINKSETTINGS
71 struct ethtool_link_settings {
80 uint8_t eth_tp_mdix_ctrl;
81 int8_t link_mode_masks_nwords;
83 uint32_t link_mode_masks[];
86 #define ETHTOOL_GLINKSETTINGS 0x0000004c
87 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
88 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
89 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
90 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
91 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
92 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
93 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
94 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
95 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
96 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
97 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
98 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
99 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
100 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
101 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
102 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
104 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
105 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
106 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
107 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
109 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
110 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
111 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
113 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
114 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
115 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
116 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
117 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
121 * Get interface name from private structure.
124 * Pointer to Ethernet device.
126 * Interface name output buffer.
129 * 0 on success, a negative errno value otherwise and rte_errno is set.
132 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
134 struct priv *priv = dev->data->dev_private;
137 unsigned int dev_type = 0;
138 unsigned int dev_port_prev = ~0u;
139 char match[IF_NAMESIZE] = "";
142 MKSTR(path, "%s/device/net", priv->ibdev_path);
150 while ((dent = readdir(dir)) != NULL) {
151 char *name = dent->d_name;
153 unsigned int dev_port;
156 if ((name[0] == '.') &&
157 ((name[1] == '\0') ||
158 ((name[1] == '.') && (name[2] == '\0'))))
161 MKSTR(path, "%s/device/net/%s/%s",
162 priv->ibdev_path, name,
163 (dev_type ? "dev_id" : "dev_port"));
165 file = fopen(path, "rb");
170 * Switch to dev_id when dev_port does not exist as
171 * is the case with Linux kernel versions < 3.15.
182 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
187 * Switch to dev_id when dev_port returns the same value for
188 * all ports. May happen when using a MOFED release older than
189 * 3.0 with a Linux kernel >= 3.15.
191 if (dev_port == dev_port_prev)
193 dev_port_prev = dev_port;
194 if (dev_port == (priv->port - 1u))
195 snprintf(match, sizeof(match), "%s", name);
198 if (match[0] == '\0') {
202 strncpy(*ifname, match, sizeof(*ifname));
207 * Perform ifreq ioctl() on associated Ethernet device.
210 * Pointer to Ethernet device.
212 * Request number to pass to ioctl().
214 * Interface request structure output buffer.
217 * 0 on success, a negative errno value otherwise and rte_errno is set.
220 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
222 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
229 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
232 ret = ioctl(sock, req, ifr);
248 * Pointer to Ethernet device.
250 * MTU value output buffer.
253 * 0 on success, a negative errno value otherwise and rte_errno is set.
256 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
258 struct ifreq request;
259 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
263 *mtu = request.ifr_mtu;
271 * Pointer to Ethernet device.
276 * 0 on success, a negative errno value otherwise and rte_errno is set.
279 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
281 struct ifreq request = { .ifr_mtu = mtu, };
283 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
290 * Pointer to Ethernet device.
292 * Bitmask for flags that must remain untouched.
294 * Bitmask for flags to modify.
297 * 0 on success, a negative errno value otherwise and rte_errno is set.
300 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
302 struct ifreq request;
303 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
307 request.ifr_flags &= keep;
308 request.ifr_flags |= flags & ~keep;
309 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
313 * DPDK callback for Ethernet device configuration.
316 * Pointer to Ethernet device structure.
319 * 0 on success, a negative errno value otherwise and rte_errno is set.
322 mlx5_dev_configure(struct rte_eth_dev *dev)
324 struct priv *priv = dev->data->dev_private;
325 unsigned int rxqs_n = dev->data->nb_rx_queues;
326 unsigned int txqs_n = dev->data->nb_tx_queues;
329 unsigned int reta_idx_n;
330 const uint8_t use_app_rss_key =
331 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
334 if (use_app_rss_key &&
335 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
336 rss_hash_default_key_len)) {
337 DRV_LOG(ERR, "port %u RSS key len must be %zu Bytes long",
338 dev->data->port_id, rss_hash_default_key_len);
342 priv->rss_conf.rss_key =
343 rte_realloc(priv->rss_conf.rss_key,
344 rss_hash_default_key_len, 0);
345 if (!priv->rss_conf.rss_key) {
346 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
347 dev->data->port_id, rxqs_n);
351 memcpy(priv->rss_conf.rss_key,
353 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
354 rss_hash_default_key,
355 rss_hash_default_key_len);
356 priv->rss_conf.rss_key_len = rss_hash_default_key_len;
357 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
358 priv->rxqs = (void *)dev->data->rx_queues;
359 priv->txqs = (void *)dev->data->tx_queues;
360 if (txqs_n != priv->txqs_n) {
361 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
362 dev->data->port_id, priv->txqs_n, txqs_n);
363 priv->txqs_n = txqs_n;
365 if (rxqs_n > priv->ind_table_max_size) {
366 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
367 dev->data->port_id, rxqs_n);
371 if (rxqs_n == priv->rxqs_n)
373 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
374 dev->data->port_id, priv->rxqs_n, rxqs_n);
375 priv->rxqs_n = rxqs_n;
376 /* If the requested number of RX queues is not a power of two, use the
377 * maximum indirection table size for better balancing.
378 * The result is always rounded to the next power of two. */
379 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
380 priv->ind_table_max_size :
382 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
385 /* When the number of RX queues is not a power of two, the remaining
386 * table entries are padded with reused WQs and hashes are not spread
388 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
389 (*priv->reta_idx)[i] = j;
397 * DPDK callback to get information about the device.
400 * Pointer to Ethernet device structure.
402 * Info structure output buffer.
405 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
407 struct priv *priv = dev->data->dev_private;
409 char ifname[IF_NAMESIZE];
411 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
412 /* FIXME: we should ask the device for these values. */
413 info->min_rx_bufsize = 32;
414 info->max_rx_pktlen = 65536;
416 * Since we need one CQ per QP, the limit is the minimum number
417 * between the two values.
419 max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
420 priv->device_attr.orig_attr.max_qp);
421 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
424 info->max_rx_queues = max;
425 info->max_tx_queues = max;
426 info->max_mac_addrs = RTE_DIM(priv->mac);
427 info->rx_offload_capa =
429 (DEV_RX_OFFLOAD_IPV4_CKSUM |
430 DEV_RX_OFFLOAD_UDP_CKSUM |
431 DEV_RX_OFFLOAD_TCP_CKSUM) :
433 (priv->hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0) |
434 DEV_RX_OFFLOAD_TIMESTAMP;
437 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
439 info->tx_offload_capa |=
440 (DEV_TX_OFFLOAD_IPV4_CKSUM |
441 DEV_TX_OFFLOAD_UDP_CKSUM |
442 DEV_TX_OFFLOAD_TCP_CKSUM);
444 info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
446 info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
447 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
448 DEV_TX_OFFLOAD_GRE_TNL_TSO);
449 if (mlx5_get_ifname(dev, &ifname) == 0)
450 info->if_index = if_nametoindex(ifname);
451 info->reta_size = priv->reta_idx_n ?
452 priv->reta_idx_n : priv->ind_table_max_size;
453 info->hash_key_size = rss_hash_default_key_len;
454 info->speed_capa = priv->link_speed_capa;
455 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
459 * Get supported packet types.
462 * Pointer to Ethernet device structure.
465 * A pointer to the supported Packet types array.
468 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
470 static const uint32_t ptypes[] = {
471 /* refers to rxq_cq_to_pkt_type() */
473 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
474 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
475 RTE_PTYPE_L4_NONFRAG,
479 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
480 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
481 RTE_PTYPE_INNER_L4_NONFRAG,
482 RTE_PTYPE_INNER_L4_FRAG,
483 RTE_PTYPE_INNER_L4_TCP,
484 RTE_PTYPE_INNER_L4_UDP,
488 if (dev->rx_pkt_burst == mlx5_rx_burst ||
489 dev->rx_pkt_burst == mlx5_rx_burst_vec)
495 * DPDK callback to retrieve physical link information.
498 * Pointer to Ethernet device structure.
500 * Storage for current link status.
503 * 0 on success, a negative errno value otherwise and rte_errno is set.
506 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
507 struct rte_eth_link *link)
509 struct priv *priv = dev->data->dev_private;
510 struct ethtool_cmd edata = {
511 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
514 struct rte_eth_link dev_link;
518 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
520 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
521 dev->data->port_id, strerror(rte_errno));
524 memset(&dev_link, 0, sizeof(dev_link));
525 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
526 (ifr.ifr_flags & IFF_RUNNING));
527 ifr.ifr_data = (void *)&edata;
528 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
531 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
532 dev->data->port_id, strerror(rte_errno));
535 link_speed = ethtool_cmd_speed(&edata);
536 if (link_speed == -1)
537 dev_link.link_speed = 0;
539 dev_link.link_speed = link_speed;
540 priv->link_speed_capa = 0;
541 if (edata.supported & SUPPORTED_Autoneg)
542 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
543 if (edata.supported & (SUPPORTED_1000baseT_Full |
544 SUPPORTED_1000baseKX_Full))
545 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
546 if (edata.supported & SUPPORTED_10000baseKR_Full)
547 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
548 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
549 SUPPORTED_40000baseCR4_Full |
550 SUPPORTED_40000baseSR4_Full |
551 SUPPORTED_40000baseLR4_Full))
552 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
553 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
554 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
555 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
556 ETH_LINK_SPEED_FIXED);
557 if ((dev_link.link_speed && !dev_link.link_status) ||
558 (!dev_link.link_speed && dev_link.link_status)) {
567 * Retrieve physical link information (unlocked version using new ioctl).
570 * Pointer to Ethernet device structure.
572 * Storage for current link status.
575 * 0 on success, a negative errno value otherwise and rte_errno is set.
578 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
579 struct rte_eth_link *link)
582 struct priv *priv = dev->data->dev_private;
583 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
585 struct rte_eth_link dev_link;
589 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
591 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
592 dev->data->port_id, strerror(rte_errno));
595 memset(&dev_link, 0, sizeof(dev_link));
596 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
597 (ifr.ifr_flags & IFF_RUNNING));
598 ifr.ifr_data = (void *)&gcmd;
599 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
602 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
604 dev->data->port_id, strerror(rte_errno));
607 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
609 alignas(struct ethtool_link_settings)
610 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
611 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
612 struct ethtool_link_settings *ecmd = (void *)data;
615 ifr.ifr_data = (void *)ecmd;
616 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
619 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
621 dev->data->port_id, strerror(rte_errno));
624 dev_link.link_speed = ecmd->speed;
625 sc = ecmd->link_mode_masks[0] |
626 ((uint64_t)ecmd->link_mode_masks[1] << 32);
627 priv->link_speed_capa = 0;
628 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
629 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
630 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
631 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
632 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
633 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
634 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
635 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
636 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
637 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
638 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
639 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
640 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
641 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
642 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
643 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
644 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
645 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
646 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
647 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
648 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
649 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
650 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
651 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
652 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
653 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
654 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
655 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
656 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
657 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
658 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
659 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
660 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
661 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
662 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
663 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
664 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
665 ETH_LINK_SPEED_FIXED);
666 if ((dev_link.link_speed && !dev_link.link_status) ||
667 (!dev_link.link_speed && dev_link.link_status)) {
676 * DPDK callback to retrieve physical link information.
679 * Pointer to Ethernet device structure.
680 * @param wait_to_complete
681 * Wait for request completion.
684 * 0 if link status was not updated, positive if it was, a negative errno
685 * value otherwise and rte_errno is set.
688 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
691 struct rte_eth_link dev_link;
692 time_t start_time = time(NULL);
695 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
697 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
700 /* Handle wait to complete situation. */
701 if (wait_to_complete && ret == -EAGAIN) {
702 if (abs((int)difftime(time(NULL), start_time)) <
703 MLX5_LINK_STATUS_TIMEOUT) {
710 } else if (ret < 0) {
713 } while (wait_to_complete);
714 ret = !!memcmp(&dev->data->dev_link, &dev_link,
715 sizeof(struct rte_eth_link));
716 dev->data->dev_link = dev_link;
721 * DPDK callback to change the MTU.
724 * Pointer to Ethernet device structure.
729 * 0 on success, a negative errno value otherwise and rte_errno is set.
732 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
734 struct priv *priv = dev->data->dev_private;
735 uint16_t kern_mtu = 0;
738 ret = mlx5_get_mtu(dev, &kern_mtu);
741 /* Set kernel interface MTU first. */
742 ret = mlx5_set_mtu(dev, mtu);
745 ret = mlx5_get_mtu(dev, &kern_mtu);
748 if (kern_mtu == mtu) {
750 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
751 dev->data->port_id, mtu);
759 * DPDK callback to get flow control status.
762 * Pointer to Ethernet device structure.
763 * @param[out] fc_conf
764 * Flow control output buffer.
767 * 0 on success, a negative errno value otherwise and rte_errno is set.
770 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
773 struct ethtool_pauseparam ethpause = {
774 .cmd = ETHTOOL_GPAUSEPARAM
778 ifr.ifr_data = (void *)ðpause;
779 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
782 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
784 dev->data->port_id, strerror(rte_errno));
787 fc_conf->autoneg = ethpause.autoneg;
788 if (ethpause.rx_pause && ethpause.tx_pause)
789 fc_conf->mode = RTE_FC_FULL;
790 else if (ethpause.rx_pause)
791 fc_conf->mode = RTE_FC_RX_PAUSE;
792 else if (ethpause.tx_pause)
793 fc_conf->mode = RTE_FC_TX_PAUSE;
795 fc_conf->mode = RTE_FC_NONE;
800 * DPDK callback to modify flow control parameters.
803 * Pointer to Ethernet device structure.
805 * Flow control parameters.
808 * 0 on success, a negative errno value otherwise and rte_errno is set.
811 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
814 struct ethtool_pauseparam ethpause = {
815 .cmd = ETHTOOL_SPAUSEPARAM
819 ifr.ifr_data = (void *)ðpause;
820 ethpause.autoneg = fc_conf->autoneg;
821 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
822 (fc_conf->mode & RTE_FC_RX_PAUSE))
823 ethpause.rx_pause = 1;
825 ethpause.rx_pause = 0;
827 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
828 (fc_conf->mode & RTE_FC_TX_PAUSE))
829 ethpause.tx_pause = 1;
831 ethpause.tx_pause = 0;
832 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
835 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
837 dev->data->port_id, strerror(rte_errno));
844 * Get PCI information from struct ibv_device.
847 * Pointer to Ethernet device structure.
848 * @param[out] pci_addr
849 * PCI bus address output buffer.
852 * 0 on success, a negative errno value otherwise and rte_errno is set.
855 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
856 struct rte_pci_addr *pci_addr)
860 MKSTR(path, "%s/device/uevent", device->ibdev_path);
862 file = fopen(path, "rb");
867 while (fgets(line, sizeof(line), file) == line) {
868 size_t len = strlen(line);
871 /* Truncate long lines. */
872 if (len == (sizeof(line) - 1))
873 while (line[(len - 1)] != '\n') {
877 line[(len - 1)] = ret;
879 /* Extract information. */
882 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
886 &pci_addr->function) == 4) {
896 * Device status handler.
899 * Pointer to Ethernet device.
901 * Pointer to event flags holder.
904 * Events bitmap of callback process which can be called immediately.
907 mlx5_dev_status_handler(struct rte_eth_dev *dev)
909 struct priv *priv = dev->data->dev_private;
910 struct ibv_async_event event;
913 if (mlx5_link_update(dev, 0) == -EAGAIN) {
917 /* Read all message and acknowledge them. */
919 if (ibv_get_async_event(priv->ctx, &event))
921 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
922 event.event_type == IBV_EVENT_PORT_ERR) &&
923 (dev->data->dev_conf.intr_conf.lsc == 1))
924 ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
925 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
926 dev->data->dev_conf.intr_conf.rmv == 1)
927 ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
930 "port %u event type %d on not handled",
931 dev->data->port_id, event.event_type);
932 ibv_ack_async_event(&event);
938 * Handle interrupts from the NIC.
940 * @param[in] intr_handle
946 mlx5_dev_interrupt_handler(void *cb_arg)
948 struct rte_eth_dev *dev = cb_arg;
951 events = mlx5_dev_status_handler(dev);
952 if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
953 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
955 if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
956 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL,
961 * Handle interrupts from the socket.
967 mlx5_dev_handler_socket(void *cb_arg)
969 struct rte_eth_dev *dev = cb_arg;
971 mlx5_socket_handle(dev);
975 * Uninstall interrupt handler.
978 * Pointer to Ethernet device.
981 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
983 struct priv *priv = dev->data->dev_private;
985 if (dev->data->dev_conf.intr_conf.lsc ||
986 dev->data->dev_conf.intr_conf.rmv)
987 rte_intr_callback_unregister(&priv->intr_handle,
988 mlx5_dev_interrupt_handler, dev);
989 if (priv->primary_socket)
990 rte_intr_callback_unregister(&priv->intr_handle_socket,
991 mlx5_dev_handler_socket, dev);
992 priv->intr_handle.fd = 0;
993 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
994 priv->intr_handle_socket.fd = 0;
995 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
999 * Install interrupt handler.
1002 * Pointer to Ethernet device.
1005 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1007 struct priv *priv = dev->data->dev_private;
1011 assert(priv->ctx->async_fd > 0);
1012 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1013 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1016 "port %u failed to change file descriptor async event"
1018 dev->data->port_id);
1019 dev->data->dev_conf.intr_conf.lsc = 0;
1020 dev->data->dev_conf.intr_conf.rmv = 0;
1022 if (dev->data->dev_conf.intr_conf.lsc ||
1023 dev->data->dev_conf.intr_conf.rmv) {
1024 priv->intr_handle.fd = priv->ctx->async_fd;
1025 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1026 rte_intr_callback_register(&priv->intr_handle,
1027 mlx5_dev_interrupt_handler, dev);
1029 ret = mlx5_socket_init(dev);
1031 DRV_LOG(ERR, "port %u cannot initialise socket: %s",
1032 dev->data->port_id, strerror(rte_errno));
1033 else if (priv->primary_socket) {
1034 priv->intr_handle_socket.fd = priv->primary_socket;
1035 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1036 rte_intr_callback_register(&priv->intr_handle_socket,
1037 mlx5_dev_handler_socket, dev);
1042 * DPDK callback to bring the link DOWN.
1045 * Pointer to Ethernet device structure.
1048 * 0 on success, a negative errno value otherwise and rte_errno is set.
1051 mlx5_set_link_down(struct rte_eth_dev *dev)
1053 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1057 * DPDK callback to bring the link UP.
1060 * Pointer to Ethernet device structure.
1063 * 0 on success, a negative errno value otherwise and rte_errno is set.
1066 mlx5_set_link_up(struct rte_eth_dev *dev)
1068 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1072 * Configure the TX function to use.
1075 * Pointer to rte_eth_dev structure.
1078 * Pointer to selected Tx burst function.
1081 mlx5_select_tx_function(struct rte_eth_dev *dev)
1083 struct priv *priv = dev->data->dev_private;
1084 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1086 /* Select appropriate TX function. */
1087 if (priv->mps == MLX5_MPW_ENHANCED) {
1088 if (mlx5_check_vec_tx_support(dev) > 0) {
1089 if (mlx5_check_raw_vec_tx_support(dev) > 0)
1090 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1092 tx_pkt_burst = mlx5_tx_burst_vec;
1094 "port %u selected enhanced MPW Tx vectorized"
1096 dev->data->port_id);
1098 tx_pkt_burst = mlx5_tx_burst_empw;
1100 "port %u selected enhanced MPW Tx function",
1101 dev->data->port_id);
1103 } else if (priv->mps && priv->txq_inline) {
1104 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1105 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1106 dev->data->port_id);
1107 } else if (priv->mps) {
1108 tx_pkt_burst = mlx5_tx_burst_mpw;
1109 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1110 dev->data->port_id);
1112 return tx_pkt_burst;
1116 * Configure the RX function to use.
1119 * Pointer to rte_eth_dev structure.
1122 * Pointer to selected Rx burst function.
1125 mlx5_select_rx_function(struct rte_eth_dev *dev)
1127 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1129 assert(dev != NULL);
1130 if (mlx5_check_vec_rx_support(dev) > 0) {
1131 rx_pkt_burst = mlx5_rx_burst_vec;
1132 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1133 dev->data->port_id);
1135 return rx_pkt_burst;