1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/sockios.h>
27 #include <rte_atomic.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_bus_pci.h>
31 #include <rte_common.h>
32 #include <rte_interrupts.h>
33 #include <rte_malloc.h>
34 #include <rte_string_fns.h>
35 #include <rte_rwlock.h>
36 #include <rte_cycles.h>
38 #include <mlx5_glue.h>
39 #include <mlx5_devx_cmds.h>
40 #include <mlx5_common.h>
43 #include "mlx5_rxtx.h"
44 #include "mlx5_utils.h"
46 /* Supported speed values found in /usr/include/linux/ethtool.h */
47 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
48 #define SUPPORTED_40000baseKR4_Full (1 << 23)
50 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
51 #define SUPPORTED_40000baseCR4_Full (1 << 24)
53 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
54 #define SUPPORTED_40000baseSR4_Full (1 << 25)
56 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
57 #define SUPPORTED_40000baseLR4_Full (1 << 26)
59 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
60 #define SUPPORTED_56000baseKR4_Full (1 << 27)
62 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
63 #define SUPPORTED_56000baseCR4_Full (1 << 28)
65 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
66 #define SUPPORTED_56000baseSR4_Full (1 << 29)
68 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
69 #define SUPPORTED_56000baseLR4_Full (1 << 30)
72 /* Add defines in case the running kernel is not the same as user headers. */
73 #ifndef ETHTOOL_GLINKSETTINGS
74 struct ethtool_link_settings {
83 uint8_t eth_tp_mdix_ctrl;
84 int8_t link_mode_masks_nwords;
86 uint32_t link_mode_masks[];
89 /* The kernel values can be found in /include/uapi/linux/ethtool.h */
90 #define ETHTOOL_GLINKSETTINGS 0x0000004c
91 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
92 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
93 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
94 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
95 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
96 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
97 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
98 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
99 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
100 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
101 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
102 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
103 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
104 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
105 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
106 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
108 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
109 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
110 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
111 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
113 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
114 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
115 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
117 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
118 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
119 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
120 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
121 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
123 #ifndef HAVE_ETHTOOL_LINK_MODE_200G
124 #define ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT 62
125 #define ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT 63
126 #define ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT 0 /* 64 - 64 */
127 #define ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT 1 /* 65 - 64 */
128 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */
132 * Get master interface name from private structure.
135 * Pointer to Ethernet device.
137 * Interface name output buffer.
140 * 0 on success, a negative errno value otherwise and rte_errno is set.
143 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
147 unsigned int dev_type = 0;
148 unsigned int dev_port_prev = ~0u;
149 char match[IF_NAMESIZE] = "";
151 MLX5_ASSERT(ibdev_path);
153 MKSTR(path, "%s/device/net", ibdev_path);
161 while ((dent = readdir(dir)) != NULL) {
162 char *name = dent->d_name;
164 unsigned int dev_port;
167 if ((name[0] == '.') &&
168 ((name[1] == '\0') ||
169 ((name[1] == '.') && (name[2] == '\0'))))
172 MKSTR(path, "%s/device/net/%s/%s",
174 (dev_type ? "dev_id" : "dev_port"));
176 file = fopen(path, "rb");
181 * Switch to dev_id when dev_port does not exist as
182 * is the case with Linux kernel versions < 3.15.
193 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
198 * Switch to dev_id when dev_port returns the same value for
199 * all ports. May happen when using a MOFED release older than
200 * 3.0 with a Linux kernel >= 3.15.
202 if (dev_port == dev_port_prev)
204 dev_port_prev = dev_port;
206 strlcpy(match, name, sizeof(match));
209 if (match[0] == '\0') {
213 strncpy(*ifname, match, sizeof(*ifname));
218 * Get interface name from private structure.
220 * This is a port representor-aware version of mlx5_get_master_ifname().
223 * Pointer to Ethernet device.
225 * Interface name output buffer.
228 * 0 on success, a negative errno value otherwise and rte_errno is set.
231 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
233 struct mlx5_priv *priv = dev->data->dev_private;
234 unsigned int ifindex;
237 MLX5_ASSERT(priv->sh);
238 ifindex = mlx5_ifindex(dev);
240 if (!priv->representor)
241 return mlx5_get_master_ifname(priv->sh->ibdev_path,
246 if (if_indextoname(ifindex, &(*ifname)[0]))
253 * Perform ifreq ioctl() on associated Ethernet device.
256 * Pointer to Ethernet device.
258 * Request number to pass to ioctl().
260 * Interface request structure output buffer.
263 * 0 on success, a negative errno value otherwise and rte_errno is set.
266 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
268 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
275 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
278 ret = ioctl(sock, req, ifr);
294 * Pointer to Ethernet device.
296 * MTU value output buffer.
299 * 0 on success, a negative errno value otherwise and rte_errno is set.
302 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
304 struct ifreq request;
305 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
309 *mtu = request.ifr_mtu;
317 * Pointer to Ethernet device.
322 * 0 on success, a negative errno value otherwise and rte_errno is set.
325 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
327 struct ifreq request = { .ifr_mtu = mtu, };
329 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
336 * Pointer to Ethernet device.
338 * Bitmask for flags that must remain untouched.
340 * Bitmask for flags to modify.
343 * 0 on success, a negative errno value otherwise and rte_errno is set.
346 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
348 struct ifreq request;
349 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
353 request.ifr_flags &= keep;
354 request.ifr_flags |= flags & ~keep;
355 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
359 * Get device current raw clock counter
362 * Pointer to Ethernet device structure.
364 * Current raw clock counter of the device.
367 * 0 if the clock has correctly been read
368 * The value of errno in case of error
371 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
373 struct mlx5_priv *priv = dev->data->dev_private;
374 struct ibv_context *ctx = priv->sh->ctx;
375 struct ibv_values_ex values;
378 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
379 err = mlx5_glue->query_rt_values_ex(ctx, &values);
381 DRV_LOG(WARNING, "Could not query the clock !");
384 *clock = values.raw_clock.tv_nsec;
389 * Retrieve the master device for representor in the same switch domain.
392 * Pointer to representor Ethernet device structure.
395 * Master device structure on success, NULL otherwise.
397 static struct rte_eth_dev *
398 mlx5_find_master_dev(struct rte_eth_dev *dev)
400 struct mlx5_priv *priv;
404 priv = dev->data->dev_private;
405 domain_id = priv->domain_id;
406 MLX5_ASSERT(priv->representor);
407 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
408 struct mlx5_priv *opriv =
409 rte_eth_devices[port_id].data->dev_private;
412 opriv->domain_id == domain_id &&
413 opriv->sh == priv->sh)
414 return &rte_eth_devices[port_id];
420 * DPDK callback to retrieve physical link information.
423 * Pointer to Ethernet device structure.
425 * Storage for current link status.
428 * 0 on success, a negative errno value otherwise and rte_errno is set.
431 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
432 struct rte_eth_link *link)
434 struct mlx5_priv *priv = dev->data->dev_private;
435 struct ethtool_cmd edata = {
436 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
439 struct rte_eth_link dev_link;
443 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
445 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
446 dev->data->port_id, strerror(rte_errno));
449 dev_link = (struct rte_eth_link) {
450 .link_status = ((ifr.ifr_flags & IFF_UP) &&
451 (ifr.ifr_flags & IFF_RUNNING)),
453 ifr = (struct ifreq) {
454 .ifr_data = (void *)&edata,
456 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
458 if (ret == -ENOTSUP && priv->representor) {
459 struct rte_eth_dev *master;
462 * For representors we can try to inherit link
463 * settings from the master device. Actually
464 * link settings do not make a lot of sense
465 * for representors due to missing physical
466 * link. The old kernel drivers supported
467 * emulated settings query for representors,
468 * the new ones do not, so we have to add
469 * this code for compatibility issues.
471 master = mlx5_find_master_dev(dev);
473 ifr = (struct ifreq) {
474 .ifr_data = (void *)&edata,
476 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
481 "port %u ioctl(SIOCETHTOOL,"
482 " ETHTOOL_GSET) failed: %s",
483 dev->data->port_id, strerror(rte_errno));
487 link_speed = ethtool_cmd_speed(&edata);
488 if (link_speed == -1)
489 dev_link.link_speed = ETH_SPEED_NUM_NONE;
491 dev_link.link_speed = link_speed;
492 priv->link_speed_capa = 0;
493 if (edata.supported & SUPPORTED_Autoneg)
494 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
495 if (edata.supported & (SUPPORTED_1000baseT_Full |
496 SUPPORTED_1000baseKX_Full))
497 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
498 if (edata.supported & SUPPORTED_10000baseKR_Full)
499 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
500 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
501 SUPPORTED_40000baseCR4_Full |
502 SUPPORTED_40000baseSR4_Full |
503 SUPPORTED_40000baseLR4_Full))
504 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
505 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
506 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
507 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
508 ETH_LINK_SPEED_FIXED);
509 if (((dev_link.link_speed && !dev_link.link_status) ||
510 (!dev_link.link_speed && dev_link.link_status))) {
519 * Retrieve physical link information (unlocked version using new ioctl).
522 * Pointer to Ethernet device structure.
524 * Storage for current link status.
527 * 0 on success, a negative errno value otherwise and rte_errno is set.
530 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
531 struct rte_eth_link *link)
534 struct mlx5_priv *priv = dev->data->dev_private;
535 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
537 struct rte_eth_link dev_link;
538 struct rte_eth_dev *master = NULL;
542 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
544 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
545 dev->data->port_id, strerror(rte_errno));
548 dev_link = (struct rte_eth_link) {
549 .link_status = ((ifr.ifr_flags & IFF_UP) &&
550 (ifr.ifr_flags & IFF_RUNNING)),
552 ifr = (struct ifreq) {
553 .ifr_data = (void *)&gcmd,
555 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
557 if (ret == -ENOTSUP && priv->representor) {
559 * For representors we can try to inherit link
560 * settings from the master device. Actually
561 * link settings do not make a lot of sense
562 * for representors due to missing physical
563 * link. The old kernel drivers supported
564 * emulated settings query for representors,
565 * the new ones do not, so we have to add
566 * this code for compatibility issues.
568 master = mlx5_find_master_dev(dev);
570 ifr = (struct ifreq) {
571 .ifr_data = (void *)&gcmd,
573 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
578 "port %u ioctl(SIOCETHTOOL,"
579 " ETHTOOL_GLINKSETTINGS) failed: %s",
580 dev->data->port_id, strerror(rte_errno));
584 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
586 alignas(struct ethtool_link_settings)
587 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
588 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
589 struct ethtool_link_settings *ecmd = (void *)data;
592 ifr.ifr_data = (void *)ecmd;
593 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
596 "port %u ioctl(SIOCETHTOOL,"
597 "ETHTOOL_GLINKSETTINGS) failed: %s",
598 dev->data->port_id, strerror(rte_errno));
601 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
603 sc = ecmd->link_mode_masks[0] |
604 ((uint64_t)ecmd->link_mode_masks[1] << 32);
605 priv->link_speed_capa = 0;
606 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
607 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
608 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
609 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
610 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
611 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
612 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
613 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
614 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
615 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
616 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
617 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
618 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
619 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
620 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
621 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
622 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
623 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
624 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
625 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
626 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
627 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
628 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
629 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
630 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
631 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
632 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
633 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
634 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
635 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
636 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
637 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
638 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
639 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
640 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
641 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
642 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
644 sc = ecmd->link_mode_masks[2] |
645 ((uint64_t)ecmd->link_mode_masks[3] << 32);
646 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
648 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
649 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
650 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
651 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
652 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
653 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
654 ETH_LINK_SPEED_FIXED);
655 if (((dev_link.link_speed && !dev_link.link_status) ||
656 (!dev_link.link_speed && dev_link.link_status))) {
665 * DPDK callback to retrieve physical link information.
668 * Pointer to Ethernet device structure.
669 * @param wait_to_complete
670 * Wait for request completion.
673 * 0 if link status was not updated, positive if it was, a negative errno
674 * value otherwise and rte_errno is set.
677 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
680 struct rte_eth_link dev_link;
681 time_t start_time = time(NULL);
682 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
685 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
687 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
690 /* Handle wait to complete situation. */
691 if ((wait_to_complete || retry) && ret == -EAGAIN) {
692 if (abs((int)difftime(time(NULL), start_time)) <
693 MLX5_LINK_STATUS_TIMEOUT) {
700 } else if (ret < 0) {
703 } while (wait_to_complete || retry-- > 0);
704 ret = !!memcmp(&dev->data->dev_link, &dev_link,
705 sizeof(struct rte_eth_link));
706 dev->data->dev_link = dev_link;
711 * DPDK callback to get flow control status.
714 * Pointer to Ethernet device structure.
715 * @param[out] fc_conf
716 * Flow control output buffer.
719 * 0 on success, a negative errno value otherwise and rte_errno is set.
722 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
725 struct ethtool_pauseparam ethpause = {
726 .cmd = ETHTOOL_GPAUSEPARAM
730 ifr.ifr_data = (void *)ðpause;
731 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
734 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
736 dev->data->port_id, strerror(rte_errno));
739 fc_conf->autoneg = ethpause.autoneg;
740 if (ethpause.rx_pause && ethpause.tx_pause)
741 fc_conf->mode = RTE_FC_FULL;
742 else if (ethpause.rx_pause)
743 fc_conf->mode = RTE_FC_RX_PAUSE;
744 else if (ethpause.tx_pause)
745 fc_conf->mode = RTE_FC_TX_PAUSE;
747 fc_conf->mode = RTE_FC_NONE;
752 * DPDK callback to modify flow control parameters.
755 * Pointer to Ethernet device structure.
757 * Flow control parameters.
760 * 0 on success, a negative errno value otherwise and rte_errno is set.
763 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
766 struct ethtool_pauseparam ethpause = {
767 .cmd = ETHTOOL_SPAUSEPARAM
771 ifr.ifr_data = (void *)ðpause;
772 ethpause.autoneg = fc_conf->autoneg;
773 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
774 (fc_conf->mode & RTE_FC_RX_PAUSE))
775 ethpause.rx_pause = 1;
777 ethpause.rx_pause = 0;
779 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
780 (fc_conf->mode & RTE_FC_TX_PAUSE))
781 ethpause.tx_pause = 1;
783 ethpause.tx_pause = 0;
784 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
787 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
789 dev->data->port_id, strerror(rte_errno));
796 * Handle asynchronous removal event for entire multiport device.
799 * Infiniband device shared context.
802 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
806 for (i = 0; i < sh->max_port; ++i) {
807 struct rte_eth_dev *dev;
809 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
811 * Or not existing port either no
812 * handler installed for this port.
816 dev = &rte_eth_devices[sh->port[i].ih_port_id];
818 if (dev->data->dev_conf.intr_conf.rmv)
819 _rte_eth_dev_callback_process
820 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
825 * Handle shared asynchronous events the NIC (removal event
826 * and link status change). Supports multiport IB device.
832 mlx5_dev_interrupt_handler(void *cb_arg)
834 struct mlx5_dev_ctx_shared *sh = cb_arg;
835 struct ibv_async_event event;
837 /* Read all message from the IB device and acknowledge them. */
839 struct rte_eth_dev *dev;
842 if (mlx5_glue->get_async_event(sh->ctx, &event))
844 /* Retrieve and check IB port index. */
845 tmp = (uint32_t)event.element.port_num;
846 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
848 * The DEVICE_FATAL event is called once for
849 * entire device without port specifying.
850 * We should notify all existing ports.
852 mlx5_glue->ack_async_event(&event);
853 mlx5_dev_interrupt_device_fatal(sh);
856 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
858 /* Unsupported device level event. */
859 mlx5_glue->ack_async_event(&event);
861 "unsupported common event (type %d)",
865 if (tmp > sh->max_port) {
866 /* Invalid IB port index. */
867 mlx5_glue->ack_async_event(&event);
869 "cannot handle an event (type %d)"
870 "due to invalid IB port index (%u)",
871 event.event_type, tmp);
874 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
875 /* No handler installed. */
876 mlx5_glue->ack_async_event(&event);
878 "cannot handle an event (type %d)"
879 "due to no handler installed for port %u",
880 event.event_type, tmp);
883 /* Retrieve ethernet device descriptor. */
884 tmp = sh->port[tmp - 1].ih_port_id;
885 dev = &rte_eth_devices[tmp];
887 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
888 event.event_type == IBV_EVENT_PORT_ERR) &&
889 dev->data->dev_conf.intr_conf.lsc) {
890 mlx5_glue->ack_async_event(&event);
891 if (mlx5_link_update(dev, 0) == -EAGAIN) {
895 _rte_eth_dev_callback_process
896 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
900 "port %u cannot handle an unknown event (type %d)",
901 dev->data->port_id, event.event_type);
902 mlx5_glue->ack_async_event(&event);
907 * Unregister callback handler safely. The handler may be active
908 * while we are trying to unregister it, in this case code -EAGAIN
909 * is returned by rte_intr_callback_unregister(). This routine checks
910 * the return code and tries to unregister handler again.
915 * pointer to callback routine
917 * opaque callback parameter
920 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
921 rte_intr_callback_fn cb_fn, void *cb_arg)
924 * Try to reduce timeout management overhead by not calling
925 * the timer related routines on the first iteration. If the
926 * unregistering succeeds on first call there will be no
927 * timer calls at all.
935 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
938 if (ret != -EAGAIN) {
939 DRV_LOG(INFO, "failed to unregister interrupt"
940 " handler (error: %d)", ret);
945 struct timespec onems;
947 /* Wait one millisecond and try again. */
949 onems.tv_nsec = NS_PER_S / MS_PER_S;
950 nanosleep(&onems, 0);
951 /* Check whether one second elapsed. */
952 if ((rte_get_timer_cycles() - start) <= twait)
956 * We get the amount of timer ticks for one second.
957 * If this amount elapsed it means we spent one
958 * second in waiting. This branch is executed once
959 * on first iteration.
961 twait = rte_get_timer_hz();
965 * Timeout elapsed, show message (once a second) and retry.
966 * We have no other acceptable option here, if we ignore
967 * the unregistering return code the handler will not
968 * be unregistered, fd will be closed and we may get the
969 * crush. Hanging and messaging in the loop seems not to be
972 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
973 start = rte_get_timer_cycles();
978 * Handle DEVX interrupts from the NIC.
979 * This function is probably called from the DPDK host thread.
985 mlx5_dev_interrupt_handler_devx(void *cb_arg)
987 #ifndef HAVE_IBV_DEVX_ASYNC
991 struct mlx5_dev_ctx_shared *sh = cb_arg;
993 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
994 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
995 MLX5_ST_SZ_BYTES(traffic_counter) +
996 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
998 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1000 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1003 mlx5_flow_async_pool_query_handle
1004 (sh, (uint64_t)out.cmd_resp.wr_id,
1005 mlx5_devx_get_out_command_status(buf));
1006 #endif /* HAVE_IBV_DEVX_ASYNC */
1010 * DPDK callback to bring the link DOWN.
1013 * Pointer to Ethernet device structure.
1016 * 0 on success, a negative errno value otherwise and rte_errno is set.
1019 mlx5_set_link_down(struct rte_eth_dev *dev)
1021 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1025 * DPDK callback to bring the link UP.
1028 * Pointer to Ethernet device structure.
1031 * 0 on success, a negative errno value otherwise and rte_errno is set.
1034 mlx5_set_link_up(struct rte_eth_dev *dev)
1036 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1040 * Check if mlx5 device was removed.
1043 * Pointer to Ethernet device structure.
1046 * 1 when device is removed, otherwise 0.
1049 mlx5_is_removed(struct rte_eth_dev *dev)
1051 struct ibv_device_attr device_attr;
1052 struct mlx5_priv *priv = dev->data->dev_private;
1054 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1060 * Get switch information associated with network interface.
1063 * Network interface index.
1065 * Switch information object, populated in case of success.
1068 * 0 on success, a negative errno value otherwise and rte_errno is set.
1071 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1073 char ifname[IF_NAMESIZE];
1074 char port_name[IF_NAMESIZE];
1076 struct mlx5_switch_info data = {
1079 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1084 bool port_switch_id_set = false;
1085 bool device_dir = false;
1089 if (!if_indextoname(ifindex, ifname)) {
1094 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1096 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1098 MKSTR(pci_device, "/sys/class/net/%s/device",
1101 file = fopen(phys_port_name, "rb");
1103 ret = fscanf(file, "%s", port_name);
1106 mlx5_translate_port_name(port_name, &data);
1108 file = fopen(phys_switch_id, "rb");
1113 port_switch_id_set =
1114 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1117 dir = opendir(pci_device);
1122 if (port_switch_id_set) {
1123 /* We have some E-Switch configuration. */
1124 mlx5_sysfs_check_switch_info(device_dir, &data);
1127 MLX5_ASSERT(!(data.master && data.representor));
1128 if (data.master && data.representor) {
1129 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1130 " and as representor", ifindex);
1138 * Analyze gathered port parameters via sysfs to recognize master
1139 * and representor devices for E-Switch configuration.
1141 * @param[in] device_dir
1142 * flag of presence of "device" directory under port device key.
1143 * @param[inout] switch_info
1144 * Port information, including port name as a number and port name
1145 * type if recognized
1148 * master and representor flags are set in switch_info according to
1149 * recognized parameters (if any).
1152 mlx5_sysfs_check_switch_info(bool device_dir,
1153 struct mlx5_switch_info *switch_info)
1155 switch (switch_info->name_type) {
1156 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1158 * Name is not recognized, assume the master,
1159 * check the device directory presence.
1161 switch_info->master = device_dir;
1163 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1165 * Name is not set, this assumes the legacy naming
1166 * schema for master, just check if there is
1167 * a device directory.
1169 switch_info->master = device_dir;
1171 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1172 /* New uplink naming schema recognized. */
1173 switch_info->master = 1;
1175 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1176 /* Legacy representors naming schema. */
1177 switch_info->representor = !device_dir;
1179 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1180 /* New representors naming schema. */
1181 switch_info->representor = 1;
1187 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1190 * Pointer to Ethernet device structure.
1191 * @param[out] modinfo
1192 * Storage for plug-in module EEPROM information.
1195 * 0 on success, a negative errno value otherwise and rte_errno is set.
1198 mlx5_get_module_info(struct rte_eth_dev *dev,
1199 struct rte_eth_dev_module_info *modinfo)
1201 struct ethtool_modinfo info = {
1202 .cmd = ETHTOOL_GMODULEINFO,
1204 struct ifreq ifr = (struct ifreq) {
1205 .ifr_data = (void *)&info,
1209 if (!dev || !modinfo) {
1210 DRV_LOG(WARNING, "missing argument, cannot get module info");
1214 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1216 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1217 dev->data->port_id, strerror(rte_errno));
1220 modinfo->type = info.type;
1221 modinfo->eeprom_len = info.eeprom_len;
1226 * DPDK callback to retrieve plug-in module EEPROM data.
1229 * Pointer to Ethernet device structure.
1231 * Storage for plug-in module EEPROM data.
1234 * 0 on success, a negative errno value otherwise and rte_errno is set.
1236 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1237 struct rte_dev_eeprom_info *info)
1239 struct ethtool_eeprom *eeprom;
1243 if (!dev || !info) {
1244 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1248 eeprom = rte_calloc(__func__, 1,
1249 (sizeof(struct ethtool_eeprom) + info->length), 0);
1251 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1252 "eeprom data", dev->data->port_id);
1256 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1257 eeprom->offset = info->offset;
1258 eeprom->len = info->length;
1259 ifr = (struct ifreq) {
1260 .ifr_data = (void *)eeprom,
1262 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1264 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1265 dev->data->port_id, strerror(rte_errno));
1267 rte_memcpy(info->data, eeprom->data, info->length);