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_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->bond_ifindex > 0) {
154 memcpy(ifname, priv->bond_name, 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 Ethernet device.
175 * Pointer to Ethernet device.
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(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
187 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
194 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
197 ret = ioctl(sock, req, ifr);
213 * Pointer to Ethernet device.
215 * MTU value output buffer.
218 * 0 on success, a negative errno value otherwise and rte_errno is set.
221 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
223 struct ifreq request;
224 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
228 *mtu = request.ifr_mtu;
236 * Pointer to Ethernet device.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
246 struct ifreq request = { .ifr_mtu = mtu, };
248 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
255 * Pointer to Ethernet device.
257 * Bitmask for flags that must remain untouched.
259 * Bitmask for flags to modify.
262 * 0 on success, a negative errno value otherwise and rte_errno is set.
265 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
267 struct ifreq request;
268 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
272 request.ifr_flags &= keep;
273 request.ifr_flags |= flags & ~keep;
274 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
278 * Get device current raw clock counter
281 * Pointer to Ethernet device structure.
283 * Current raw clock counter of the device.
286 * 0 if the clock has correctly been read
287 * The value of errno in case of error
290 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
292 struct mlx5_priv *priv = dev->data->dev_private;
293 struct ibv_context *ctx = priv->sh->ctx;
294 struct ibv_values_ex values;
297 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
298 err = mlx5_glue->query_rt_values_ex(ctx, &values);
300 DRV_LOG(WARNING, "Could not query the clock !");
303 *clock = values.raw_clock.tv_nsec;
308 * Retrieve the master device for representor in the same switch domain.
311 * Pointer to representor Ethernet device structure.
314 * Master device structure on success, NULL otherwise.
316 static struct rte_eth_dev *
317 mlx5_find_master_dev(struct rte_eth_dev *dev)
319 struct mlx5_priv *priv;
323 priv = dev->data->dev_private;
324 domain_id = priv->domain_id;
325 MLX5_ASSERT(priv->representor);
326 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
327 struct mlx5_priv *opriv =
328 rte_eth_devices[port_id].data->dev_private;
331 opriv->domain_id == domain_id &&
332 opriv->sh == priv->sh)
333 return &rte_eth_devices[port_id];
339 * DPDK callback to retrieve physical link information.
342 * Pointer to Ethernet device structure.
344 * Storage for current link status.
347 * 0 on success, a negative errno value otherwise and rte_errno is set.
350 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
351 struct rte_eth_link *link)
353 struct mlx5_priv *priv = dev->data->dev_private;
354 struct ethtool_cmd edata = {
355 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
358 struct rte_eth_link dev_link;
362 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
364 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
365 dev->data->port_id, strerror(rte_errno));
368 dev_link = (struct rte_eth_link) {
369 .link_status = ((ifr.ifr_flags & IFF_UP) &&
370 (ifr.ifr_flags & IFF_RUNNING)),
372 ifr = (struct ifreq) {
373 .ifr_data = (void *)&edata,
375 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
377 if (ret == -ENOTSUP && priv->representor) {
378 struct rte_eth_dev *master;
381 * For representors we can try to inherit link
382 * settings from the master device. Actually
383 * link settings do not make a lot of sense
384 * for representors due to missing physical
385 * link. The old kernel drivers supported
386 * emulated settings query for representors,
387 * the new ones do not, so we have to add
388 * this code for compatibility issues.
390 master = mlx5_find_master_dev(dev);
392 ifr = (struct ifreq) {
393 .ifr_data = (void *)&edata,
395 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
400 "port %u ioctl(SIOCETHTOOL,"
401 " ETHTOOL_GSET) failed: %s",
402 dev->data->port_id, strerror(rte_errno));
406 link_speed = ethtool_cmd_speed(&edata);
407 if (link_speed == -1)
408 dev_link.link_speed = ETH_SPEED_NUM_UNKNOWN;
410 dev_link.link_speed = link_speed;
411 priv->link_speed_capa = 0;
412 if (edata.supported & SUPPORTED_Autoneg)
413 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
414 if (edata.supported & (SUPPORTED_1000baseT_Full |
415 SUPPORTED_1000baseKX_Full))
416 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
417 if (edata.supported & SUPPORTED_10000baseKR_Full)
418 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
419 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
420 SUPPORTED_40000baseCR4_Full |
421 SUPPORTED_40000baseSR4_Full |
422 SUPPORTED_40000baseLR4_Full))
423 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
424 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
425 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
426 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
427 ETH_LINK_SPEED_FIXED);
433 * Retrieve physical link information (unlocked version using new ioctl).
436 * Pointer to Ethernet device structure.
438 * Storage for current link status.
441 * 0 on success, a negative errno value otherwise and rte_errno is set.
444 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
445 struct rte_eth_link *link)
448 struct mlx5_priv *priv = dev->data->dev_private;
449 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
451 struct rte_eth_link dev_link;
452 struct rte_eth_dev *master = NULL;
456 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
458 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
459 dev->data->port_id, strerror(rte_errno));
462 dev_link = (struct rte_eth_link) {
463 .link_status = ((ifr.ifr_flags & IFF_UP) &&
464 (ifr.ifr_flags & IFF_RUNNING)),
466 ifr = (struct ifreq) {
467 .ifr_data = (void *)&gcmd,
469 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
471 if (ret == -ENOTSUP && priv->representor) {
473 * For representors we can try to inherit link
474 * settings from the master device. Actually
475 * link settings do not make a lot of sense
476 * for representors due to missing physical
477 * link. The old kernel drivers supported
478 * emulated settings query for representors,
479 * the new ones do not, so we have to add
480 * this code for compatibility issues.
482 master = mlx5_find_master_dev(dev);
484 ifr = (struct ifreq) {
485 .ifr_data = (void *)&gcmd,
487 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
492 "port %u ioctl(SIOCETHTOOL,"
493 " ETHTOOL_GLINKSETTINGS) failed: %s",
494 dev->data->port_id, strerror(rte_errno));
498 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
500 alignas(struct ethtool_link_settings)
501 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
502 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
503 struct ethtool_link_settings *ecmd = (void *)data;
506 ifr.ifr_data = (void *)ecmd;
507 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
510 "port %u ioctl(SIOCETHTOOL,"
511 "ETHTOOL_GLINKSETTINGS) failed: %s",
512 dev->data->port_id, strerror(rte_errno));
515 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ?
516 ETH_SPEED_NUM_UNKNOWN : ecmd->speed;
517 sc = ecmd->link_mode_masks[0] |
518 ((uint64_t)ecmd->link_mode_masks[1] << 32);
519 priv->link_speed_capa = 0;
520 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
521 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
522 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
523 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
524 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
525 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
526 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
527 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
528 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
529 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
530 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
531 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
532 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
533 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
534 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
535 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
536 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
537 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
538 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
539 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
540 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
541 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
542 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
543 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
544 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
545 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
546 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
547 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
548 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
549 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
550 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
551 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
552 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
553 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
554 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
555 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
556 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
558 sc = ecmd->link_mode_masks[2] |
559 ((uint64_t)ecmd->link_mode_masks[3] << 32);
560 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
562 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
563 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
564 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
565 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
566 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
567 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
568 ETH_LINK_SPEED_FIXED);
574 * DPDK callback to retrieve physical link information.
577 * Pointer to Ethernet device structure.
578 * @param wait_to_complete
579 * Wait for request completion.
582 * 0 if link status was not updated, positive if it was, a negative errno
583 * value otherwise and rte_errno is set.
586 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
589 struct rte_eth_link dev_link;
590 time_t start_time = time(NULL);
591 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
594 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
596 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
599 /* Handle wait to complete situation. */
600 if ((wait_to_complete || retry) && ret == -EAGAIN) {
601 if (abs((int)difftime(time(NULL), start_time)) <
602 MLX5_LINK_STATUS_TIMEOUT) {
609 } else if (ret < 0) {
612 } while (wait_to_complete || retry-- > 0);
613 ret = !!memcmp(&dev->data->dev_link, &dev_link,
614 sizeof(struct rte_eth_link));
615 dev->data->dev_link = dev_link;
620 * DPDK callback to get flow control status.
623 * Pointer to Ethernet device structure.
624 * @param[out] fc_conf
625 * Flow control output buffer.
628 * 0 on success, a negative errno value otherwise and rte_errno is set.
631 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
634 struct ethtool_pauseparam ethpause = {
635 .cmd = ETHTOOL_GPAUSEPARAM
639 ifr.ifr_data = (void *)ðpause;
640 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
643 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
645 dev->data->port_id, strerror(rte_errno));
648 fc_conf->autoneg = ethpause.autoneg;
649 if (ethpause.rx_pause && ethpause.tx_pause)
650 fc_conf->mode = RTE_FC_FULL;
651 else if (ethpause.rx_pause)
652 fc_conf->mode = RTE_FC_RX_PAUSE;
653 else if (ethpause.tx_pause)
654 fc_conf->mode = RTE_FC_TX_PAUSE;
656 fc_conf->mode = RTE_FC_NONE;
661 * DPDK callback to modify flow control parameters.
664 * Pointer to Ethernet device structure.
666 * Flow control parameters.
669 * 0 on success, a negative errno value otherwise and rte_errno is set.
672 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
675 struct ethtool_pauseparam ethpause = {
676 .cmd = ETHTOOL_SPAUSEPARAM
680 ifr.ifr_data = (void *)ðpause;
681 ethpause.autoneg = fc_conf->autoneg;
682 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
683 (fc_conf->mode & RTE_FC_RX_PAUSE))
684 ethpause.rx_pause = 1;
686 ethpause.rx_pause = 0;
688 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
689 (fc_conf->mode & RTE_FC_TX_PAUSE))
690 ethpause.tx_pause = 1;
692 ethpause.tx_pause = 0;
693 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
696 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
698 dev->data->port_id, strerror(rte_errno));
705 * Handle asynchronous removal event for entire multiport device.
708 * Infiniband device shared context.
711 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
715 for (i = 0; i < sh->max_port; ++i) {
716 struct rte_eth_dev *dev;
718 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
720 * Or not existing port either no
721 * handler installed for this port.
725 dev = &rte_eth_devices[sh->port[i].ih_port_id];
727 if (dev->data->dev_conf.intr_conf.rmv)
728 rte_eth_dev_callback_process
729 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
734 * Handle shared asynchronous events the NIC (removal event
735 * and link status change). Supports multiport IB device.
741 mlx5_dev_interrupt_handler(void *cb_arg)
743 struct mlx5_dev_ctx_shared *sh = cb_arg;
744 struct ibv_async_event event;
746 /* Read all message from the IB device and acknowledge them. */
748 struct rte_eth_dev *dev;
751 if (mlx5_glue->get_async_event(sh->ctx, &event))
753 /* Retrieve and check IB port index. */
754 tmp = (uint32_t)event.element.port_num;
755 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
757 * The DEVICE_FATAL event is called once for
758 * entire device without port specifying.
759 * We should notify all existing ports.
761 mlx5_glue->ack_async_event(&event);
762 mlx5_dev_interrupt_device_fatal(sh);
765 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
767 /* Unsupported device level event. */
768 mlx5_glue->ack_async_event(&event);
770 "unsupported common event (type %d)",
774 if (tmp > sh->max_port) {
775 /* Invalid IB port index. */
776 mlx5_glue->ack_async_event(&event);
778 "cannot handle an event (type %d)"
779 "due to invalid IB port index (%u)",
780 event.event_type, tmp);
783 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
784 /* No handler installed. */
785 mlx5_glue->ack_async_event(&event);
787 "cannot handle an event (type %d)"
788 "due to no handler installed for port %u",
789 event.event_type, tmp);
792 /* Retrieve ethernet device descriptor. */
793 tmp = sh->port[tmp - 1].ih_port_id;
794 dev = &rte_eth_devices[tmp];
796 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
797 event.event_type == IBV_EVENT_PORT_ERR) &&
798 dev->data->dev_conf.intr_conf.lsc) {
799 mlx5_glue->ack_async_event(&event);
800 if (mlx5_link_update(dev, 0) == -EAGAIN) {
804 rte_eth_dev_callback_process
805 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
809 "port %u cannot handle an unknown event (type %d)",
810 dev->data->port_id, event.event_type);
811 mlx5_glue->ack_async_event(&event);
816 * Unregister callback handler safely. The handler may be active
817 * while we are trying to unregister it, in this case code -EAGAIN
818 * is returned by rte_intr_callback_unregister(). This routine checks
819 * the return code and tries to unregister handler again.
824 * pointer to callback routine
826 * opaque callback parameter
829 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
830 rte_intr_callback_fn cb_fn, void *cb_arg)
833 * Try to reduce timeout management overhead by not calling
834 * the timer related routines on the first iteration. If the
835 * unregistering succeeds on first call there will be no
836 * timer calls at all.
844 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
847 if (ret != -EAGAIN) {
848 DRV_LOG(INFO, "failed to unregister interrupt"
849 " handler (error: %d)", ret);
854 struct timespec onems;
856 /* Wait one millisecond and try again. */
858 onems.tv_nsec = NS_PER_S / MS_PER_S;
859 nanosleep(&onems, 0);
860 /* Check whether one second elapsed. */
861 if ((rte_get_timer_cycles() - start) <= twait)
865 * We get the amount of timer ticks for one second.
866 * If this amount elapsed it means we spent one
867 * second in waiting. This branch is executed once
868 * on first iteration.
870 twait = rte_get_timer_hz();
874 * Timeout elapsed, show message (once a second) and retry.
875 * We have no other acceptable option here, if we ignore
876 * the unregistering return code the handler will not
877 * be unregistered, fd will be closed and we may get the
878 * crush. Hanging and messaging in the loop seems not to be
881 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
882 start = rte_get_timer_cycles();
887 * Handle DEVX interrupts from the NIC.
888 * This function is probably called from the DPDK host thread.
894 mlx5_dev_interrupt_handler_devx(void *cb_arg)
896 #ifndef HAVE_IBV_DEVX_ASYNC
900 struct mlx5_dev_ctx_shared *sh = cb_arg;
902 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
903 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
904 MLX5_ST_SZ_BYTES(traffic_counter) +
905 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
907 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
909 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
912 mlx5_flow_async_pool_query_handle
913 (sh, (uint64_t)out.cmd_resp.wr_id,
914 mlx5_devx_get_out_command_status(buf));
915 #endif /* HAVE_IBV_DEVX_ASYNC */
919 * DPDK callback to bring the link DOWN.
922 * Pointer to Ethernet device structure.
925 * 0 on success, a negative errno value otherwise and rte_errno is set.
928 mlx5_set_link_down(struct rte_eth_dev *dev)
930 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
934 * DPDK callback to bring the link UP.
937 * Pointer to Ethernet device structure.
940 * 0 on success, a negative errno value otherwise and rte_errno is set.
943 mlx5_set_link_up(struct rte_eth_dev *dev)
945 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
949 * Check if mlx5 device was removed.
952 * Pointer to Ethernet device structure.
955 * 1 when device is removed, otherwise 0.
958 mlx5_is_removed(struct rte_eth_dev *dev)
960 struct ibv_device_attr device_attr;
961 struct mlx5_priv *priv = dev->data->dev_private;
963 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
969 * Analyze gathered port parameters via sysfs to recognize master
970 * and representor devices for E-Switch configuration.
972 * @param[in] device_dir
973 * flag of presence of "device" directory under port device key.
974 * @param[inout] switch_info
975 * Port information, including port name as a number and port name
979 * master and representor flags are set in switch_info according to
980 * recognized parameters (if any).
983 mlx5_sysfs_check_switch_info(bool device_dir,
984 struct mlx5_switch_info *switch_info)
986 switch (switch_info->name_type) {
987 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
989 * Name is not recognized, assume the master,
990 * check the device directory presence.
992 switch_info->master = device_dir;
994 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
996 * Name is not set, this assumes the legacy naming
997 * schema for master, just check if there is
998 * a device directory.
1000 switch_info->master = device_dir;
1002 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1003 /* New uplink naming schema recognized. */
1004 switch_info->master = 1;
1006 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1007 /* Legacy representors naming schema. */
1008 switch_info->representor = !device_dir;
1010 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1012 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1013 /* New representors naming schema. */
1014 switch_info->representor = 1;
1020 * Get switch information associated with network interface.
1023 * Network interface index.
1025 * Switch information object, populated in case of success.
1028 * 0 on success, a negative errno value otherwise and rte_errno is set.
1031 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1033 char ifname[IF_NAMESIZE];
1034 char port_name[IF_NAMESIZE];
1036 struct mlx5_switch_info data = {
1039 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1044 bool port_switch_id_set = false;
1045 bool device_dir = false;
1049 if (!if_indextoname(ifindex, ifname)) {
1054 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1056 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1058 MKSTR(pci_device, "/sys/class/net/%s/device",
1061 file = fopen(phys_port_name, "rb");
1063 ret = fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", port_name);
1066 mlx5_translate_port_name(port_name, &data);
1068 file = fopen(phys_switch_id, "rb");
1073 port_switch_id_set =
1074 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1077 dir = opendir(pci_device);
1082 if (port_switch_id_set) {
1083 /* We have some E-Switch configuration. */
1084 mlx5_sysfs_check_switch_info(device_dir, &data);
1087 MLX5_ASSERT(!(data.master && data.representor));
1088 if (data.master && data.representor) {
1089 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1090 " and as representor", ifindex);
1098 * Get bond information associated with network interface.
1101 * Network interface index of bond slave interface
1102 * @param[out] ifindex
1103 * Pointer to bond ifindex.
1104 * @param[out] ifname
1105 * Pointer to bond ifname.
1108 * 0 on success, a negative errno value otherwise and rte_errno is set.
1111 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex,
1114 char name[IF_NAMESIZE];
1119 if (!if_indextoname(pf_ifindex, name) || !strlen(name)) {
1123 MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name);
1124 /* read bond ifindex */
1125 file = fopen(bond_if, "rb");
1130 ret = fscanf(file, "%u", &index);
1139 /* read bond device name from symbol link */
1141 if (!if_indextoname(index, ifname)) {
1150 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1153 * Pointer to Ethernet device structure.
1154 * @param[out] modinfo
1155 * Storage for plug-in module EEPROM information.
1158 * 0 on success, a negative errno value otherwise and rte_errno is set.
1161 mlx5_get_module_info(struct rte_eth_dev *dev,
1162 struct rte_eth_dev_module_info *modinfo)
1164 struct ethtool_modinfo info = {
1165 .cmd = ETHTOOL_GMODULEINFO,
1167 struct ifreq ifr = (struct ifreq) {
1168 .ifr_data = (void *)&info,
1172 if (!dev || !modinfo) {
1173 DRV_LOG(WARNING, "missing argument, cannot get module info");
1177 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1179 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1180 dev->data->port_id, strerror(rte_errno));
1183 modinfo->type = info.type;
1184 modinfo->eeprom_len = info.eeprom_len;
1189 * DPDK callback to retrieve plug-in module EEPROM data.
1192 * Pointer to Ethernet device structure.
1194 * Storage for plug-in module EEPROM data.
1197 * 0 on success, a negative errno value otherwise and rte_errno is set.
1199 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1200 struct rte_dev_eeprom_info *info)
1202 struct ethtool_eeprom *eeprom;
1206 if (!dev || !info) {
1207 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1211 eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1212 (sizeof(struct ethtool_eeprom) + info->length), 0,
1215 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1216 "eeprom data", dev->data->port_id);
1220 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1221 eeprom->offset = info->offset;
1222 eeprom->len = info->length;
1223 ifr = (struct ifreq) {
1224 .ifr_data = (void *)eeprom,
1226 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1228 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1229 dev->data->port_id, strerror(rte_errno));
1231 rte_memcpy(info->data, eeprom->data, info->length);
1237 * Read device counters table.
1240 * Pointer to Ethernet device.
1242 * Counters table output buffer.
1245 * 0 on success and stats is filled, negative errno value otherwise and
1249 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1251 struct mlx5_priv *priv = dev->data->dev_private;
1252 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1255 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1256 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1257 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1260 et_stats->cmd = ETHTOOL_GSTATS;
1261 et_stats->n_stats = xstats_ctrl->stats_n;
1262 ifr.ifr_data = (caddr_t)et_stats;
1263 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1266 "port %u unable to read statistic values from device",
1267 dev->data->port_id);
1270 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1271 if (xstats_ctrl->info[i].dev) {
1272 ret = mlx5_os_read_dev_stat(priv,
1273 xstats_ctrl->info[i].ctr_name,
1275 /* return last xstats counter if fail to read. */
1277 xstats_ctrl->xstats[i] = stats[i];
1279 stats[i] = xstats_ctrl->xstats[i];
1281 stats[i] = (uint64_t)
1282 et_stats->data[xstats_ctrl->dev_table_idx[i]];
1289 * Query the number of statistics provided by ETHTOOL.
1292 * Pointer to Ethernet device.
1295 * Number of statistics on success, negative errno value otherwise and
1299 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1301 struct ethtool_drvinfo drvinfo;
1305 drvinfo.cmd = ETHTOOL_GDRVINFO;
1306 ifr.ifr_data = (caddr_t)&drvinfo;
1307 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1309 DRV_LOG(WARNING, "port %u unable to query number of statistics",
1310 dev->data->port_id);
1313 return drvinfo.n_stats;
1316 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1318 .dpdk_name = "rx_unicast_bytes",
1319 .ctr_name = "rx_vport_unicast_bytes",
1322 .dpdk_name = "rx_multicast_bytes",
1323 .ctr_name = "rx_vport_multicast_bytes",
1326 .dpdk_name = "rx_broadcast_bytes",
1327 .ctr_name = "rx_vport_broadcast_bytes",
1330 .dpdk_name = "rx_unicast_packets",
1331 .ctr_name = "rx_vport_unicast_packets",
1334 .dpdk_name = "rx_multicast_packets",
1335 .ctr_name = "rx_vport_multicast_packets",
1338 .dpdk_name = "rx_broadcast_packets",
1339 .ctr_name = "rx_vport_broadcast_packets",
1342 .dpdk_name = "tx_unicast_bytes",
1343 .ctr_name = "tx_vport_unicast_bytes",
1346 .dpdk_name = "tx_multicast_bytes",
1347 .ctr_name = "tx_vport_multicast_bytes",
1350 .dpdk_name = "tx_broadcast_bytes",
1351 .ctr_name = "tx_vport_broadcast_bytes",
1354 .dpdk_name = "tx_unicast_packets",
1355 .ctr_name = "tx_vport_unicast_packets",
1358 .dpdk_name = "tx_multicast_packets",
1359 .ctr_name = "tx_vport_multicast_packets",
1362 .dpdk_name = "tx_broadcast_packets",
1363 .ctr_name = "tx_vport_broadcast_packets",
1366 .dpdk_name = "rx_wqe_errors",
1367 .ctr_name = "rx_wqe_err",
1370 .dpdk_name = "rx_phy_crc_errors",
1371 .ctr_name = "rx_crc_errors_phy",
1374 .dpdk_name = "rx_phy_in_range_len_errors",
1375 .ctr_name = "rx_in_range_len_errors_phy",
1378 .dpdk_name = "rx_phy_symbol_errors",
1379 .ctr_name = "rx_symbol_err_phy",
1382 .dpdk_name = "tx_phy_errors",
1383 .ctr_name = "tx_errors_phy",
1386 .dpdk_name = "rx_out_of_buffer",
1387 .ctr_name = "out_of_buffer",
1391 .dpdk_name = "tx_phy_packets",
1392 .ctr_name = "tx_packets_phy",
1395 .dpdk_name = "rx_phy_packets",
1396 .ctr_name = "rx_packets_phy",
1399 .dpdk_name = "tx_phy_discard_packets",
1400 .ctr_name = "tx_discards_phy",
1403 .dpdk_name = "rx_phy_discard_packets",
1404 .ctr_name = "rx_discards_phy",
1407 .dpdk_name = "tx_phy_bytes",
1408 .ctr_name = "tx_bytes_phy",
1411 .dpdk_name = "rx_phy_bytes",
1412 .ctr_name = "rx_bytes_phy",
1414 /* Representor only */
1416 .dpdk_name = "rx_vport_packets",
1417 .ctr_name = "vport_rx_packets",
1420 .dpdk_name = "rx_vport_bytes",
1421 .ctr_name = "vport_rx_bytes",
1424 .dpdk_name = "tx_vport_packets",
1425 .ctr_name = "vport_tx_packets",
1428 .dpdk_name = "tx_vport_bytes",
1429 .ctr_name = "vport_tx_bytes",
1433 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1436 * Init the structures to read device counters.
1439 * Pointer to Ethernet device.
1442 mlx5_os_stats_init(struct rte_eth_dev *dev)
1444 struct mlx5_priv *priv = dev->data->dev_private;
1445 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1446 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1450 struct ethtool_gstrings *strings = NULL;
1451 unsigned int dev_stats_n;
1452 unsigned int str_sz;
1455 /* So that it won't aggregate for each init. */
1456 xstats_ctrl->mlx5_stats_n = 0;
1457 ret = mlx5_os_get_stats_n(dev);
1459 DRV_LOG(WARNING, "port %u no extended statistics available",
1460 dev->data->port_id);
1464 /* Allocate memory to grab stat names and values. */
1465 str_sz = dev_stats_n * ETH_GSTRING_LEN;
1466 strings = (struct ethtool_gstrings *)
1467 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1470 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1471 dev->data->port_id);
1474 strings->cmd = ETHTOOL_GSTRINGS;
1475 strings->string_set = ETH_SS_STATS;
1476 strings->len = dev_stats_n;
1477 ifr.ifr_data = (caddr_t)strings;
1478 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1480 DRV_LOG(WARNING, "port %u unable to get statistic names",
1481 dev->data->port_id);
1484 for (i = 0; i != dev_stats_n; ++i) {
1485 const char *curr_string = (const char *)
1486 &strings->data[i * ETH_GSTRING_LEN];
1488 for (j = 0; j != xstats_n; ++j) {
1489 if (!strcmp(mlx5_counters_init[j].ctr_name,
1491 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1493 xstats_ctrl->dev_table_idx[idx] = i;
1494 xstats_ctrl->info[idx] = mlx5_counters_init[j];
1499 /* Add dev counters. */
1500 for (i = 0; i != xstats_n; ++i) {
1501 if (mlx5_counters_init[i].dev) {
1502 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1504 xstats_ctrl->info[idx] = mlx5_counters_init[i];
1505 xstats_ctrl->hw_stats[idx] = 0;
1508 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1509 xstats_ctrl->stats_n = dev_stats_n;
1510 /* Copy to base at first time. */
1511 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1513 DRV_LOG(ERR, "port %u cannot read device counters: %s",
1514 dev->data->port_id, strerror(rte_errno));
1515 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1516 stats_ctrl->imissed = 0;
1522 * Get MAC address by querying netdevice.
1525 * Pointer to Ethernet device.
1527 * MAC address output buffer.
1530 * 0 on success, a negative errno value otherwise and rte_errno is set.
1533 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1535 struct ifreq request;
1538 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1541 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);