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 <ethdev_driver.h>
28 #include <rte_bus_pci.h>
30 #include <rte_common.h>
31 #include <rte_interrupts.h>
32 #include <rte_malloc.h>
33 #include <rte_string_fns.h>
34 #include <rte_rwlock.h>
35 #include <rte_cycles.h>
37 #include <mlx5_glue.h>
38 #include <mlx5_devx_cmds.h>
39 #include <mlx5_common.h>
40 #include <mlx5_malloc.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 */
133 * Get interface name from private structure.
135 * This is a port representor-aware version of mlx5_get_ifname_sysfs().
138 * Pointer to Ethernet device.
140 * Interface name output buffer.
143 * 0 on success, a negative errno value otherwise and rte_errno is set.
146 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[MLX5_NAMESIZE])
148 struct mlx5_priv *priv = dev->data->dev_private;
149 unsigned int ifindex;
152 MLX5_ASSERT(priv->sh);
153 if (priv->master && priv->sh->bond.ifindex > 0) {
154 memcpy(ifname, priv->sh->bond.ifname, MLX5_NAMESIZE);
157 ifindex = mlx5_ifindex(dev);
159 if (!priv->representor)
160 return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
165 if (if_indextoname(ifindex, &(*ifname)[0]))
172 * Perform ifreq ioctl() on associated netdev ifname.
175 * Pointer to netdev name.
177 * Request number to pass to ioctl().
179 * Interface request structure output buffer.
182 * 0 on success, a negative errno value otherwise and rte_errno is set.
185 mlx5_ifreq_by_ifname(const char *ifname, int req, struct ifreq *ifr)
187 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
194 rte_strscpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
195 ret = ioctl(sock, req, ifr);
208 * Perform ifreq ioctl() on associated Ethernet device.
211 * Pointer to Ethernet device.
213 * Request number to pass to ioctl().
215 * Interface request structure output buffer.
218 * 0 on success, a negative errno value otherwise and rte_errno is set.
221 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
223 char ifname[sizeof(ifr->ifr_name)];
226 ret = mlx5_get_ifname(dev, &ifname);
229 return mlx5_ifreq_by_ifname(ifname, req, ifr);
236 * Pointer to Ethernet device.
238 * MTU value output buffer.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
246 struct ifreq request;
247 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
251 *mtu = request.ifr_mtu;
259 * Pointer to Ethernet device.
264 * 0 on success, a negative errno value otherwise and rte_errno is set.
267 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
269 struct ifreq request = { .ifr_mtu = mtu, };
271 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
278 * Pointer to Ethernet device.
280 * Bitmask for flags that must remain untouched.
282 * Bitmask for flags to modify.
285 * 0 on success, a negative errno value otherwise and rte_errno is set.
288 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
290 struct ifreq request;
291 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
295 request.ifr_flags &= keep;
296 request.ifr_flags |= flags & ~keep;
297 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
301 * Get device current raw clock counter
304 * Pointer to Ethernet device structure.
306 * Current raw clock counter of the device.
309 * 0 if the clock has correctly been read
310 * The value of errno in case of error
313 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
315 struct mlx5_priv *priv = dev->data->dev_private;
316 struct ibv_context *ctx = priv->sh->ctx;
317 struct ibv_values_ex values;
320 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
321 err = mlx5_glue->query_rt_values_ex(ctx, &values);
323 DRV_LOG(WARNING, "Could not query the clock !");
326 *clock = values.raw_clock.tv_nsec;
331 * Retrieve the master device for representor in the same switch domain.
334 * Pointer to representor Ethernet device structure.
337 * Master device structure on success, NULL otherwise.
339 static struct rte_eth_dev *
340 mlx5_find_master_dev(struct rte_eth_dev *dev)
342 struct mlx5_priv *priv;
346 priv = dev->data->dev_private;
347 domain_id = priv->domain_id;
348 MLX5_ASSERT(priv->representor);
349 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
350 struct mlx5_priv *opriv =
351 rte_eth_devices[port_id].data->dev_private;
354 opriv->domain_id == domain_id &&
355 opriv->sh == priv->sh)
356 return &rte_eth_devices[port_id];
362 * DPDK callback to retrieve physical link information.
365 * Pointer to Ethernet device structure.
367 * Storage for current link status.
370 * 0 on success, a negative errno value otherwise and rte_errno is set.
373 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
374 struct rte_eth_link *link)
376 struct mlx5_priv *priv = dev->data->dev_private;
377 struct ethtool_cmd edata = {
378 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
381 struct rte_eth_link dev_link;
385 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
387 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
388 dev->data->port_id, strerror(rte_errno));
391 dev_link = (struct rte_eth_link) {
392 .link_status = ((ifr.ifr_flags & IFF_UP) &&
393 (ifr.ifr_flags & IFF_RUNNING)),
395 ifr = (struct ifreq) {
396 .ifr_data = (void *)&edata,
398 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
400 if (ret == -ENOTSUP && priv->representor) {
401 struct rte_eth_dev *master;
404 * For representors we can try to inherit link
405 * settings from the master device. Actually
406 * link settings do not make a lot of sense
407 * for representors due to missing physical
408 * link. The old kernel drivers supported
409 * emulated settings query for representors,
410 * the new ones do not, so we have to add
411 * this code for compatibility issues.
413 master = mlx5_find_master_dev(dev);
415 ifr = (struct ifreq) {
416 .ifr_data = (void *)&edata,
418 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
423 "port %u ioctl(SIOCETHTOOL,"
424 " ETHTOOL_GSET) failed: %s",
425 dev->data->port_id, strerror(rte_errno));
429 link_speed = ethtool_cmd_speed(&edata);
430 if (link_speed == -1)
431 dev_link.link_speed = ETH_SPEED_NUM_UNKNOWN;
433 dev_link.link_speed = link_speed;
434 priv->link_speed_capa = 0;
435 if (edata.supported & (SUPPORTED_1000baseT_Full |
436 SUPPORTED_1000baseKX_Full))
437 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
438 if (edata.supported & SUPPORTED_10000baseKR_Full)
439 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
440 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
441 SUPPORTED_40000baseCR4_Full |
442 SUPPORTED_40000baseSR4_Full |
443 SUPPORTED_40000baseLR4_Full))
444 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
445 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
446 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
447 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
448 ETH_LINK_SPEED_FIXED);
454 * Retrieve physical link information (unlocked version using new ioctl).
457 * Pointer to Ethernet device structure.
459 * Storage for current link status.
462 * 0 on success, a negative errno value otherwise and rte_errno is set.
465 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
466 struct rte_eth_link *link)
469 struct mlx5_priv *priv = dev->data->dev_private;
470 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
472 struct rte_eth_link dev_link;
473 struct rte_eth_dev *master = NULL;
477 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
479 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
480 dev->data->port_id, strerror(rte_errno));
483 dev_link = (struct rte_eth_link) {
484 .link_status = ((ifr.ifr_flags & IFF_UP) &&
485 (ifr.ifr_flags & IFF_RUNNING)),
487 ifr = (struct ifreq) {
488 .ifr_data = (void *)&gcmd,
490 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
492 if (ret == -ENOTSUP && priv->representor) {
494 * For representors we can try to inherit link
495 * settings from the master device. Actually
496 * link settings do not make a lot of sense
497 * for representors due to missing physical
498 * link. The old kernel drivers supported
499 * emulated settings query for representors,
500 * the new ones do not, so we have to add
501 * this code for compatibility issues.
503 master = mlx5_find_master_dev(dev);
505 ifr = (struct ifreq) {
506 .ifr_data = (void *)&gcmd,
508 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
513 "port %u ioctl(SIOCETHTOOL,"
514 " ETHTOOL_GLINKSETTINGS) failed: %s",
515 dev->data->port_id, strerror(rte_errno));
519 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
521 alignas(struct ethtool_link_settings)
522 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
523 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
524 struct ethtool_link_settings *ecmd = (void *)data;
527 ifr.ifr_data = (void *)ecmd;
528 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
531 "port %u ioctl(SIOCETHTOOL,"
532 "ETHTOOL_GLINKSETTINGS) failed: %s",
533 dev->data->port_id, strerror(rte_errno));
536 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ?
537 ETH_SPEED_NUM_UNKNOWN : ecmd->speed;
538 sc = ecmd->link_mode_masks[0] |
539 ((uint64_t)ecmd->link_mode_masks[1] << 32);
540 priv->link_speed_capa = 0;
541 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
542 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
543 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
544 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
545 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
546 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
547 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
548 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
549 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
550 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
551 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
552 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
553 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
554 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
555 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
556 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
557 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
558 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
559 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
560 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
561 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
562 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
563 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
564 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
565 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
566 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
567 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
568 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
569 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
570 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
571 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
572 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
573 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
574 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
575 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
577 sc = ecmd->link_mode_masks[2] |
578 ((uint64_t)ecmd->link_mode_masks[3] << 32);
579 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
581 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
582 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
583 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
584 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
585 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
586 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
587 ETH_LINK_SPEED_FIXED);
593 * DPDK callback to retrieve physical link information.
596 * Pointer to Ethernet device structure.
597 * @param wait_to_complete
598 * Wait for request completion.
601 * 0 if link status was not updated, positive if it was, a negative errno
602 * value otherwise and rte_errno is set.
605 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
608 struct rte_eth_link dev_link;
609 time_t start_time = time(NULL);
610 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
613 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
615 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
618 /* Handle wait to complete situation. */
619 if ((wait_to_complete || retry) && ret == -EAGAIN) {
620 if (abs((int)difftime(time(NULL), start_time)) <
621 MLX5_LINK_STATUS_TIMEOUT) {
628 } else if (ret < 0) {
631 } while (wait_to_complete || retry-- > 0);
632 ret = !!memcmp(&dev->data->dev_link, &dev_link,
633 sizeof(struct rte_eth_link));
634 dev->data->dev_link = dev_link;
639 * DPDK callback to get flow control status.
642 * Pointer to Ethernet device structure.
643 * @param[out] fc_conf
644 * Flow control output buffer.
647 * 0 on success, a negative errno value otherwise and rte_errno is set.
650 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
653 struct ethtool_pauseparam ethpause = {
654 .cmd = ETHTOOL_GPAUSEPARAM
658 ifr.ifr_data = (void *)ðpause;
659 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
662 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
664 dev->data->port_id, strerror(rte_errno));
667 fc_conf->autoneg = ethpause.autoneg;
668 if (ethpause.rx_pause && ethpause.tx_pause)
669 fc_conf->mode = RTE_FC_FULL;
670 else if (ethpause.rx_pause)
671 fc_conf->mode = RTE_FC_RX_PAUSE;
672 else if (ethpause.tx_pause)
673 fc_conf->mode = RTE_FC_TX_PAUSE;
675 fc_conf->mode = RTE_FC_NONE;
680 * DPDK callback to modify flow control parameters.
683 * Pointer to Ethernet device structure.
685 * Flow control parameters.
688 * 0 on success, a negative errno value otherwise and rte_errno is set.
691 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
694 struct ethtool_pauseparam ethpause = {
695 .cmd = ETHTOOL_SPAUSEPARAM
699 ifr.ifr_data = (void *)ðpause;
700 ethpause.autoneg = fc_conf->autoneg;
701 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
702 (fc_conf->mode & RTE_FC_RX_PAUSE))
703 ethpause.rx_pause = 1;
705 ethpause.rx_pause = 0;
707 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
708 (fc_conf->mode & RTE_FC_TX_PAUSE))
709 ethpause.tx_pause = 1;
711 ethpause.tx_pause = 0;
712 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
715 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
717 dev->data->port_id, strerror(rte_errno));
724 * Handle asynchronous removal event for entire multiport device.
727 * Infiniband device shared context.
730 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
734 for (i = 0; i < sh->max_port; ++i) {
735 struct rte_eth_dev *dev;
737 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
739 * Or not existing port either no
740 * handler installed for this port.
744 dev = &rte_eth_devices[sh->port[i].ih_port_id];
746 if (dev->data->dev_conf.intr_conf.rmv)
747 rte_eth_dev_callback_process
748 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
753 * Handle shared asynchronous events the NIC (removal event
754 * and link status change). Supports multiport IB device.
760 mlx5_dev_interrupt_handler(void *cb_arg)
762 struct mlx5_dev_ctx_shared *sh = cb_arg;
763 struct ibv_async_event event;
765 /* Read all message from the IB device and acknowledge them. */
767 struct rte_eth_dev *dev;
770 if (mlx5_glue->get_async_event(sh->ctx, &event))
772 /* Retrieve and check IB port index. */
773 tmp = (uint32_t)event.element.port_num;
774 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
776 * The DEVICE_FATAL event is called once for
777 * entire device without port specifying.
778 * We should notify all existing ports.
780 mlx5_glue->ack_async_event(&event);
781 mlx5_dev_interrupt_device_fatal(sh);
784 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
786 /* Unsupported device level event. */
787 mlx5_glue->ack_async_event(&event);
789 "unsupported common event (type %d)",
793 if (tmp > sh->max_port) {
794 /* Invalid IB port index. */
795 mlx5_glue->ack_async_event(&event);
797 "cannot handle an event (type %d)"
798 "due to invalid IB port index (%u)",
799 event.event_type, tmp);
802 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
803 /* No handler installed. */
804 mlx5_glue->ack_async_event(&event);
806 "cannot handle an event (type %d)"
807 "due to no handler installed for port %u",
808 event.event_type, tmp);
811 /* Retrieve ethernet device descriptor. */
812 tmp = sh->port[tmp - 1].ih_port_id;
813 dev = &rte_eth_devices[tmp];
815 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
816 event.event_type == IBV_EVENT_PORT_ERR) &&
817 dev->data->dev_conf.intr_conf.lsc) {
818 mlx5_glue->ack_async_event(&event);
819 if (mlx5_link_update(dev, 0) == -EAGAIN) {
823 rte_eth_dev_callback_process
824 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
828 "port %u cannot handle an unknown event (type %d)",
829 dev->data->port_id, event.event_type);
830 mlx5_glue->ack_async_event(&event);
835 * Unregister callback handler safely. The handler may be active
836 * while we are trying to unregister it, in this case code -EAGAIN
837 * is returned by rte_intr_callback_unregister(). This routine checks
838 * the return code and tries to unregister handler again.
843 * pointer to callback routine
845 * opaque callback parameter
848 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
849 rte_intr_callback_fn cb_fn, void *cb_arg)
852 * Try to reduce timeout management overhead by not calling
853 * the timer related routines on the first iteration. If the
854 * unregistering succeeds on first call there will be no
855 * timer calls at all.
863 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
866 if (ret != -EAGAIN) {
867 DRV_LOG(INFO, "failed to unregister interrupt"
868 " handler (error: %d)", ret);
873 struct timespec onems;
875 /* Wait one millisecond and try again. */
877 onems.tv_nsec = NS_PER_S / MS_PER_S;
878 nanosleep(&onems, 0);
879 /* Check whether one second elapsed. */
880 if ((rte_get_timer_cycles() - start) <= twait)
884 * We get the amount of timer ticks for one second.
885 * If this amount elapsed it means we spent one
886 * second in waiting. This branch is executed once
887 * on first iteration.
889 twait = rte_get_timer_hz();
893 * Timeout elapsed, show message (once a second) and retry.
894 * We have no other acceptable option here, if we ignore
895 * the unregistering return code the handler will not
896 * be unregistered, fd will be closed and we may get the
897 * crush. Hanging and messaging in the loop seems not to be
900 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
901 start = rte_get_timer_cycles();
906 * Handle DEVX interrupts from the NIC.
907 * This function is probably called from the DPDK host thread.
913 mlx5_dev_interrupt_handler_devx(void *cb_arg)
915 #ifndef HAVE_IBV_DEVX_ASYNC
919 struct mlx5_dev_ctx_shared *sh = cb_arg;
921 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
922 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
923 MLX5_ST_SZ_BYTES(traffic_counter) +
924 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
926 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
928 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
931 mlx5_flow_async_pool_query_handle
932 (sh, (uint64_t)out.cmd_resp.wr_id,
933 mlx5_devx_get_out_command_status(buf));
934 #endif /* HAVE_IBV_DEVX_ASYNC */
938 * DPDK callback to bring the link DOWN.
941 * Pointer to Ethernet device structure.
944 * 0 on success, a negative errno value otherwise and rte_errno is set.
947 mlx5_set_link_down(struct rte_eth_dev *dev)
949 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
953 * DPDK callback to bring the link UP.
956 * Pointer to Ethernet device structure.
959 * 0 on success, a negative errno value otherwise and rte_errno is set.
962 mlx5_set_link_up(struct rte_eth_dev *dev)
964 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
968 * Check if mlx5 device was removed.
971 * Pointer to Ethernet device structure.
974 * 1 when device is removed, otherwise 0.
977 mlx5_is_removed(struct rte_eth_dev *dev)
979 struct ibv_device_attr device_attr;
980 struct mlx5_priv *priv = dev->data->dev_private;
982 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
988 * Analyze gathered port parameters via sysfs to recognize master
989 * and representor devices for E-Switch configuration.
991 * @param[in] device_dir
992 * flag of presence of "device" directory under port device key.
993 * @param[inout] switch_info
994 * Port information, including port name as a number and port name
998 * master and representor flags are set in switch_info according to
999 * recognized parameters (if any).
1002 mlx5_sysfs_check_switch_info(bool device_dir,
1003 struct mlx5_switch_info *switch_info)
1005 switch (switch_info->name_type) {
1006 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1008 * Name is not recognized, assume the master,
1009 * check the device directory presence.
1011 switch_info->master = device_dir;
1013 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1015 * Name is not set, this assumes the legacy naming
1016 * schema for master, just check if there is
1017 * a device directory.
1019 switch_info->master = device_dir;
1021 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1022 /* New uplink naming schema recognized. */
1023 switch_info->master = 1;
1025 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1026 /* Legacy representors naming schema. */
1027 switch_info->representor = !device_dir;
1029 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1031 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1033 case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1034 /* New representors naming schema. */
1035 switch_info->representor = 1;
1038 switch_info->master = device_dir;
1044 * Get switch information associated with network interface.
1047 * Network interface index.
1049 * Switch information object, populated in case of success.
1052 * 0 on success, a negative errno value otherwise and rte_errno is set.
1055 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1057 char ifname[IF_NAMESIZE];
1058 char port_name[IF_NAMESIZE];
1060 struct mlx5_switch_info data = {
1063 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1068 bool port_switch_id_set = false;
1069 bool device_dir = false;
1073 if (!if_indextoname(ifindex, ifname)) {
1078 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1080 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1082 MKSTR(pci_device, "/sys/class/net/%s/device",
1085 file = fopen(phys_port_name, "rb");
1087 ret = fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", port_name);
1090 mlx5_translate_port_name(port_name, &data);
1092 file = fopen(phys_switch_id, "rb");
1097 port_switch_id_set =
1098 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1101 dir = opendir(pci_device);
1106 if (port_switch_id_set) {
1107 /* We have some E-Switch configuration. */
1108 mlx5_sysfs_check_switch_info(device_dir, &data);
1111 MLX5_ASSERT(!(data.master && data.representor));
1112 if (data.master && data.representor) {
1113 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1114 " and as representor", ifindex);
1122 * Get bond information associated with network interface.
1125 * Network interface index of bond slave interface
1126 * @param[out] ifindex
1127 * Pointer to bond ifindex.
1128 * @param[out] ifname
1129 * Pointer to bond ifname.
1132 * 0 on success, a negative errno value otherwise and rte_errno is set.
1135 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex,
1138 char name[IF_NAMESIZE];
1143 if (!if_indextoname(pf_ifindex, name) || !strlen(name)) {
1147 MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name);
1148 /* read bond ifindex */
1149 file = fopen(bond_if, "rb");
1154 ret = fscanf(file, "%u", &index);
1163 /* read bond device name from symbol link */
1165 if (!if_indextoname(index, ifname)) {
1174 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1177 * Pointer to Ethernet device structure.
1178 * @param[out] modinfo
1179 * Storage for plug-in module EEPROM information.
1182 * 0 on success, a negative errno value otherwise and rte_errno is set.
1185 mlx5_get_module_info(struct rte_eth_dev *dev,
1186 struct rte_eth_dev_module_info *modinfo)
1188 struct ethtool_modinfo info = {
1189 .cmd = ETHTOOL_GMODULEINFO,
1191 struct ifreq ifr = (struct ifreq) {
1192 .ifr_data = (void *)&info,
1197 DRV_LOG(WARNING, "missing argument, cannot get module info");
1201 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1203 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1204 dev->data->port_id, strerror(rte_errno));
1207 modinfo->type = info.type;
1208 modinfo->eeprom_len = info.eeprom_len;
1213 * DPDK callback to retrieve plug-in module EEPROM data.
1216 * Pointer to Ethernet device structure.
1218 * Storage for plug-in module EEPROM data.
1221 * 0 on success, a negative errno value otherwise and rte_errno is set.
1223 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1224 struct rte_dev_eeprom_info *info)
1226 struct ethtool_eeprom *eeprom;
1231 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1235 eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1236 (sizeof(struct ethtool_eeprom) + info->length), 0,
1239 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1240 "eeprom data", dev->data->port_id);
1244 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1245 eeprom->offset = info->offset;
1246 eeprom->len = info->length;
1247 ifr = (struct ifreq) {
1248 .ifr_data = (void *)eeprom,
1250 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1252 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1253 dev->data->port_id, strerror(rte_errno));
1255 rte_memcpy(info->data, eeprom->data, info->length);
1261 * Read device counters table.
1264 * Pointer to Ethernet device.
1266 * PF index in case of bonding device, -1 otherwise
1268 * Counters table output buffer.
1271 * 0 on success and stats is filled, negative errno value otherwise and
1275 _mlx5_os_read_dev_counters(struct rte_eth_dev *dev, int pf, uint64_t *stats)
1277 struct mlx5_priv *priv = dev->data->dev_private;
1278 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1281 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1282 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1283 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1286 et_stats->cmd = ETHTOOL_GSTATS;
1287 et_stats->n_stats = xstats_ctrl->stats_n;
1288 ifr.ifr_data = (caddr_t)et_stats;
1290 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[pf].ifname,
1293 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1296 "port %u unable to read statistic values from device",
1297 dev->data->port_id);
1300 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1301 if (xstats_ctrl->info[i].dev)
1303 stats[i] += (uint64_t)
1304 et_stats->data[xstats_ctrl->dev_table_idx[i]];
1310 * Read device counters.
1313 * Pointer to Ethernet device.
1315 * Counters table output buffer.
1318 * 0 on success and stats is filled, negative errno value otherwise and
1322 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1324 struct mlx5_priv *priv = dev->data->dev_private;
1325 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1328 memset(stats, 0, sizeof(*stats) * xstats_ctrl->mlx5_stats_n);
1329 /* Read ifreq counters. */
1330 if (priv->master && priv->pf_bond >= 0) {
1331 /* Sum xstats from bonding device member ports. */
1332 for (i = 0; i < priv->sh->bond.n_port; i++) {
1333 ret = _mlx5_os_read_dev_counters(dev, i, stats);
1338 ret = _mlx5_os_read_dev_counters(dev, -1, stats);
1340 /* Read IB counters. */
1341 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1342 if (!xstats_ctrl->info[i].dev)
1344 ret = mlx5_os_read_dev_stat(priv, xstats_ctrl->info[i].ctr_name,
1346 /* return last xstats counter if fail to read. */
1348 xstats_ctrl->xstats[i] = stats[i];
1350 stats[i] = xstats_ctrl->xstats[i];
1356 * Query the number of statistics provided by ETHTOOL.
1359 * Pointer to Ethernet device.
1362 * Number of statistics on success, negative errno value otherwise and
1366 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1368 struct mlx5_priv *priv = dev->data->dev_private;
1369 struct ethtool_drvinfo drvinfo;
1373 drvinfo.cmd = ETHTOOL_GDRVINFO;
1374 ifr.ifr_data = (caddr_t)&drvinfo;
1375 if (priv->master && priv->pf_bond >= 0)
1377 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1380 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1382 DRV_LOG(WARNING, "port %u unable to query number of statistics",
1383 dev->data->port_id);
1386 return drvinfo.n_stats;
1389 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1391 .dpdk_name = "rx_unicast_bytes",
1392 .ctr_name = "rx_vport_unicast_bytes",
1395 .dpdk_name = "rx_multicast_bytes",
1396 .ctr_name = "rx_vport_multicast_bytes",
1399 .dpdk_name = "rx_broadcast_bytes",
1400 .ctr_name = "rx_vport_broadcast_bytes",
1403 .dpdk_name = "rx_unicast_packets",
1404 .ctr_name = "rx_vport_unicast_packets",
1407 .dpdk_name = "rx_multicast_packets",
1408 .ctr_name = "rx_vport_multicast_packets",
1411 .dpdk_name = "rx_broadcast_packets",
1412 .ctr_name = "rx_vport_broadcast_packets",
1415 .dpdk_name = "tx_unicast_bytes",
1416 .ctr_name = "tx_vport_unicast_bytes",
1419 .dpdk_name = "tx_multicast_bytes",
1420 .ctr_name = "tx_vport_multicast_bytes",
1423 .dpdk_name = "tx_broadcast_bytes",
1424 .ctr_name = "tx_vport_broadcast_bytes",
1427 .dpdk_name = "tx_unicast_packets",
1428 .ctr_name = "tx_vport_unicast_packets",
1431 .dpdk_name = "tx_multicast_packets",
1432 .ctr_name = "tx_vport_multicast_packets",
1435 .dpdk_name = "tx_broadcast_packets",
1436 .ctr_name = "tx_vport_broadcast_packets",
1439 .dpdk_name = "rx_wqe_errors",
1440 .ctr_name = "rx_wqe_err",
1443 .dpdk_name = "rx_phy_crc_errors",
1444 .ctr_name = "rx_crc_errors_phy",
1447 .dpdk_name = "rx_phy_in_range_len_errors",
1448 .ctr_name = "rx_in_range_len_errors_phy",
1451 .dpdk_name = "rx_phy_symbol_errors",
1452 .ctr_name = "rx_symbol_err_phy",
1455 .dpdk_name = "tx_phy_errors",
1456 .ctr_name = "tx_errors_phy",
1459 .dpdk_name = "rx_out_of_buffer",
1460 .ctr_name = "out_of_buffer",
1464 .dpdk_name = "tx_phy_packets",
1465 .ctr_name = "tx_packets_phy",
1468 .dpdk_name = "rx_phy_packets",
1469 .ctr_name = "rx_packets_phy",
1472 .dpdk_name = "tx_phy_discard_packets",
1473 .ctr_name = "tx_discards_phy",
1476 .dpdk_name = "rx_phy_discard_packets",
1477 .ctr_name = "rx_discards_phy",
1480 .dpdk_name = "tx_phy_bytes",
1481 .ctr_name = "tx_bytes_phy",
1484 .dpdk_name = "rx_phy_bytes",
1485 .ctr_name = "rx_bytes_phy",
1487 /* Representor only */
1489 .dpdk_name = "rx_vport_packets",
1490 .ctr_name = "vport_rx_packets",
1493 .dpdk_name = "rx_vport_bytes",
1494 .ctr_name = "vport_rx_bytes",
1497 .dpdk_name = "tx_vport_packets",
1498 .ctr_name = "vport_tx_packets",
1501 .dpdk_name = "tx_vport_bytes",
1502 .ctr_name = "vport_tx_bytes",
1506 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1509 * Init the structures to read device counters.
1512 * Pointer to Ethernet device.
1515 mlx5_os_stats_init(struct rte_eth_dev *dev)
1517 struct mlx5_priv *priv = dev->data->dev_private;
1518 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1519 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1523 struct ethtool_gstrings *strings = NULL;
1524 unsigned int dev_stats_n;
1525 unsigned int str_sz;
1528 /* So that it won't aggregate for each init. */
1529 xstats_ctrl->mlx5_stats_n = 0;
1530 ret = mlx5_os_get_stats_n(dev);
1532 DRV_LOG(WARNING, "port %u no extended statistics available",
1533 dev->data->port_id);
1537 /* Allocate memory to grab stat names and values. */
1538 str_sz = dev_stats_n * ETH_GSTRING_LEN;
1539 strings = (struct ethtool_gstrings *)
1540 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1543 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1544 dev->data->port_id);
1547 strings->cmd = ETHTOOL_GSTRINGS;
1548 strings->string_set = ETH_SS_STATS;
1549 strings->len = dev_stats_n;
1550 ifr.ifr_data = (caddr_t)strings;
1551 if (priv->master && priv->pf_bond >= 0)
1552 /* Bonding master. */
1553 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1556 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1558 DRV_LOG(WARNING, "port %u unable to get statistic names",
1559 dev->data->port_id);
1562 for (i = 0; i != dev_stats_n; ++i) {
1563 const char *curr_string = (const char *)
1564 &strings->data[i * ETH_GSTRING_LEN];
1566 for (j = 0; j != xstats_n; ++j) {
1567 if (!strcmp(mlx5_counters_init[j].ctr_name,
1569 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1571 xstats_ctrl->dev_table_idx[idx] = i;
1572 xstats_ctrl->info[idx] = mlx5_counters_init[j];
1577 /* Add dev counters. */
1578 for (i = 0; i != xstats_n; ++i) {
1579 if (mlx5_counters_init[i].dev) {
1580 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1582 xstats_ctrl->info[idx] = mlx5_counters_init[i];
1583 xstats_ctrl->hw_stats[idx] = 0;
1586 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1587 xstats_ctrl->stats_n = dev_stats_n;
1588 /* Copy to base at first time. */
1589 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1591 DRV_LOG(ERR, "port %u cannot read device counters: %s",
1592 dev->data->port_id, strerror(rte_errno));
1593 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1594 stats_ctrl->imissed = 0;
1600 * Get MAC address by querying netdevice.
1603 * Pointer to Ethernet device.
1605 * MAC address output buffer.
1608 * 0 on success, a negative errno value otherwise and rte_errno is set.
1611 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1613 struct ifreq request;
1616 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1619 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);