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>
41 #include <mlx5_malloc.h>
44 #include "mlx5_rxtx.h"
45 #include "mlx5_utils.h"
47 /* Supported speed values found in /usr/include/linux/ethtool.h */
48 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
49 #define SUPPORTED_40000baseKR4_Full (1 << 23)
51 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
52 #define SUPPORTED_40000baseCR4_Full (1 << 24)
54 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
55 #define SUPPORTED_40000baseSR4_Full (1 << 25)
57 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
58 #define SUPPORTED_40000baseLR4_Full (1 << 26)
60 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
61 #define SUPPORTED_56000baseKR4_Full (1 << 27)
63 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
64 #define SUPPORTED_56000baseCR4_Full (1 << 28)
66 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
67 #define SUPPORTED_56000baseSR4_Full (1 << 29)
69 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
70 #define SUPPORTED_56000baseLR4_Full (1 << 30)
73 /* Add defines in case the running kernel is not the same as user headers. */
74 #ifndef ETHTOOL_GLINKSETTINGS
75 struct ethtool_link_settings {
84 uint8_t eth_tp_mdix_ctrl;
85 int8_t link_mode_masks_nwords;
87 uint32_t link_mode_masks[];
90 /* The kernel values can be found in /include/uapi/linux/ethtool.h */
91 #define ETHTOOL_GLINKSETTINGS 0x0000004c
92 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
93 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
94 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
95 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
96 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
97 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
98 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
99 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
100 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
101 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
102 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
103 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
104 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
105 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
106 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
107 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
109 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
110 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
111 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
112 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
114 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
115 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
116 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
118 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
119 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
120 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
121 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
122 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
124 #ifndef HAVE_ETHTOOL_LINK_MODE_200G
125 #define ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT 62
126 #define ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT 63
127 #define ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT 0 /* 64 - 64 */
128 #define ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT 1 /* 65 - 64 */
129 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */
134 * Get interface name from private structure.
136 * This is a port representor-aware version of mlx5_get_ifname_sysfs().
139 * Pointer to Ethernet device.
141 * Interface name output buffer.
144 * 0 on success, a negative errno value otherwise and rte_errno is set.
147 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
149 struct mlx5_priv *priv = dev->data->dev_private;
150 unsigned int ifindex;
153 MLX5_ASSERT(priv->sh);
154 ifindex = mlx5_ifindex(dev);
156 if (!priv->representor)
157 return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
162 if (if_indextoname(ifindex, &(*ifname)[0]))
169 * Perform ifreq ioctl() on associated Ethernet device.
172 * Pointer to Ethernet device.
174 * Request number to pass to ioctl().
176 * Interface request structure output buffer.
179 * 0 on success, a negative errno value otherwise and rte_errno is set.
182 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
184 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
191 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
194 ret = ioctl(sock, req, ifr);
210 * Pointer to Ethernet device.
212 * MTU value output buffer.
215 * 0 on success, a negative errno value otherwise and rte_errno is set.
218 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
220 struct ifreq request;
221 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
225 *mtu = request.ifr_mtu;
233 * Pointer to Ethernet device.
238 * 0 on success, a negative errno value otherwise and rte_errno is set.
241 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
243 struct ifreq request = { .ifr_mtu = mtu, };
245 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
252 * Pointer to Ethernet device.
254 * Bitmask for flags that must remain untouched.
256 * Bitmask for flags to modify.
259 * 0 on success, a negative errno value otherwise and rte_errno is set.
262 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
264 struct ifreq request;
265 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
269 request.ifr_flags &= keep;
270 request.ifr_flags |= flags & ~keep;
271 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
275 * Get device current raw clock counter
278 * Pointer to Ethernet device structure.
280 * Current raw clock counter of the device.
283 * 0 if the clock has correctly been read
284 * The value of errno in case of error
287 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
289 struct mlx5_priv *priv = dev->data->dev_private;
290 struct ibv_context *ctx = priv->sh->ctx;
291 struct ibv_values_ex values;
294 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
295 err = mlx5_glue->query_rt_values_ex(ctx, &values);
297 DRV_LOG(WARNING, "Could not query the clock !");
300 *clock = values.raw_clock.tv_nsec;
305 * Retrieve the master device for representor in the same switch domain.
308 * Pointer to representor Ethernet device structure.
311 * Master device structure on success, NULL otherwise.
313 static struct rte_eth_dev *
314 mlx5_find_master_dev(struct rte_eth_dev *dev)
316 struct mlx5_priv *priv;
320 priv = dev->data->dev_private;
321 domain_id = priv->domain_id;
322 MLX5_ASSERT(priv->representor);
323 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
324 struct mlx5_priv *opriv =
325 rte_eth_devices[port_id].data->dev_private;
328 opriv->domain_id == domain_id &&
329 opriv->sh == priv->sh)
330 return &rte_eth_devices[port_id];
336 * DPDK callback to retrieve physical link information.
339 * Pointer to Ethernet device structure.
341 * Storage for current link status.
344 * 0 on success, a negative errno value otherwise and rte_errno is set.
347 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
348 struct rte_eth_link *link)
350 struct mlx5_priv *priv = dev->data->dev_private;
351 struct ethtool_cmd edata = {
352 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
355 struct rte_eth_link dev_link;
359 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
361 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
362 dev->data->port_id, strerror(rte_errno));
365 dev_link = (struct rte_eth_link) {
366 .link_status = ((ifr.ifr_flags & IFF_UP) &&
367 (ifr.ifr_flags & IFF_RUNNING)),
369 ifr = (struct ifreq) {
370 .ifr_data = (void *)&edata,
372 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
374 if (ret == -ENOTSUP && priv->representor) {
375 struct rte_eth_dev *master;
378 * For representors we can try to inherit link
379 * settings from the master device. Actually
380 * link settings do not make a lot of sense
381 * for representors due to missing physical
382 * link. The old kernel drivers supported
383 * emulated settings query for representors,
384 * the new ones do not, so we have to add
385 * this code for compatibility issues.
387 master = mlx5_find_master_dev(dev);
389 ifr = (struct ifreq) {
390 .ifr_data = (void *)&edata,
392 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
397 "port %u ioctl(SIOCETHTOOL,"
398 " ETHTOOL_GSET) failed: %s",
399 dev->data->port_id, strerror(rte_errno));
403 link_speed = ethtool_cmd_speed(&edata);
404 if (link_speed == -1)
405 dev_link.link_speed = ETH_SPEED_NUM_NONE;
407 dev_link.link_speed = link_speed;
408 priv->link_speed_capa = 0;
409 if (edata.supported & SUPPORTED_Autoneg)
410 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
411 if (edata.supported & (SUPPORTED_1000baseT_Full |
412 SUPPORTED_1000baseKX_Full))
413 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
414 if (edata.supported & SUPPORTED_10000baseKR_Full)
415 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
416 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
417 SUPPORTED_40000baseCR4_Full |
418 SUPPORTED_40000baseSR4_Full |
419 SUPPORTED_40000baseLR4_Full))
420 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
421 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
422 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
423 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
424 ETH_LINK_SPEED_FIXED);
425 if (((dev_link.link_speed && !dev_link.link_status) ||
426 (!dev_link.link_speed && dev_link.link_status))) {
435 * Retrieve physical link information (unlocked version using new ioctl).
438 * Pointer to Ethernet device structure.
440 * Storage for current link status.
443 * 0 on success, a negative errno value otherwise and rte_errno is set.
446 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
447 struct rte_eth_link *link)
450 struct mlx5_priv *priv = dev->data->dev_private;
451 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
453 struct rte_eth_link dev_link;
454 struct rte_eth_dev *master = NULL;
458 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
460 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
461 dev->data->port_id, strerror(rte_errno));
464 dev_link = (struct rte_eth_link) {
465 .link_status = ((ifr.ifr_flags & IFF_UP) &&
466 (ifr.ifr_flags & IFF_RUNNING)),
468 ifr = (struct ifreq) {
469 .ifr_data = (void *)&gcmd,
471 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
473 if (ret == -ENOTSUP && priv->representor) {
475 * For representors we can try to inherit link
476 * settings from the master device. Actually
477 * link settings do not make a lot of sense
478 * for representors due to missing physical
479 * link. The old kernel drivers supported
480 * emulated settings query for representors,
481 * the new ones do not, so we have to add
482 * this code for compatibility issues.
484 master = mlx5_find_master_dev(dev);
486 ifr = (struct ifreq) {
487 .ifr_data = (void *)&gcmd,
489 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
494 "port %u ioctl(SIOCETHTOOL,"
495 " ETHTOOL_GLINKSETTINGS) failed: %s",
496 dev->data->port_id, strerror(rte_errno));
500 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
502 alignas(struct ethtool_link_settings)
503 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
504 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
505 struct ethtool_link_settings *ecmd = (void *)data;
508 ifr.ifr_data = (void *)ecmd;
509 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
512 "port %u ioctl(SIOCETHTOOL,"
513 "ETHTOOL_GLINKSETTINGS) failed: %s",
514 dev->data->port_id, strerror(rte_errno));
517 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
519 sc = ecmd->link_mode_masks[0] |
520 ((uint64_t)ecmd->link_mode_masks[1] << 32);
521 priv->link_speed_capa = 0;
522 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
523 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
524 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
525 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
526 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
527 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
528 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
529 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
530 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
531 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
532 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
533 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
534 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
535 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
536 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
537 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
538 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
539 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
540 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
541 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
542 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
543 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
544 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
545 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
546 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
547 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
548 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
549 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
550 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
551 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
552 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
553 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
554 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
555 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
556 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
557 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
558 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
560 sc = ecmd->link_mode_masks[2] |
561 ((uint64_t)ecmd->link_mode_masks[3] << 32);
562 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
564 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
565 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
566 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
567 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
568 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
569 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
570 ETH_LINK_SPEED_FIXED);
571 if (((dev_link.link_speed && !dev_link.link_status) ||
572 (!dev_link.link_speed && dev_link.link_status))) {
581 * DPDK callback to retrieve physical link information.
584 * Pointer to Ethernet device structure.
585 * @param wait_to_complete
586 * Wait for request completion.
589 * 0 if link status was not updated, positive if it was, a negative errno
590 * value otherwise and rte_errno is set.
593 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
596 struct rte_eth_link dev_link;
597 time_t start_time = time(NULL);
598 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
601 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
603 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
606 /* Handle wait to complete situation. */
607 if ((wait_to_complete || retry) && ret == -EAGAIN) {
608 if (abs((int)difftime(time(NULL), start_time)) <
609 MLX5_LINK_STATUS_TIMEOUT) {
616 } else if (ret < 0) {
619 } while (wait_to_complete || retry-- > 0);
620 ret = !!memcmp(&dev->data->dev_link, &dev_link,
621 sizeof(struct rte_eth_link));
622 dev->data->dev_link = dev_link;
627 * DPDK callback to get flow control status.
630 * Pointer to Ethernet device structure.
631 * @param[out] fc_conf
632 * Flow control output buffer.
635 * 0 on success, a negative errno value otherwise and rte_errno is set.
638 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
641 struct ethtool_pauseparam ethpause = {
642 .cmd = ETHTOOL_GPAUSEPARAM
646 ifr.ifr_data = (void *)ðpause;
647 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
650 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
652 dev->data->port_id, strerror(rte_errno));
655 fc_conf->autoneg = ethpause.autoneg;
656 if (ethpause.rx_pause && ethpause.tx_pause)
657 fc_conf->mode = RTE_FC_FULL;
658 else if (ethpause.rx_pause)
659 fc_conf->mode = RTE_FC_RX_PAUSE;
660 else if (ethpause.tx_pause)
661 fc_conf->mode = RTE_FC_TX_PAUSE;
663 fc_conf->mode = RTE_FC_NONE;
668 * DPDK callback to modify flow control parameters.
671 * Pointer to Ethernet device structure.
673 * Flow control parameters.
676 * 0 on success, a negative errno value otherwise and rte_errno is set.
679 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
682 struct ethtool_pauseparam ethpause = {
683 .cmd = ETHTOOL_SPAUSEPARAM
687 ifr.ifr_data = (void *)ðpause;
688 ethpause.autoneg = fc_conf->autoneg;
689 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
690 (fc_conf->mode & RTE_FC_RX_PAUSE))
691 ethpause.rx_pause = 1;
693 ethpause.rx_pause = 0;
695 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
696 (fc_conf->mode & RTE_FC_TX_PAUSE))
697 ethpause.tx_pause = 1;
699 ethpause.tx_pause = 0;
700 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
703 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
705 dev->data->port_id, strerror(rte_errno));
712 * Handle asynchronous removal event for entire multiport device.
715 * Infiniband device shared context.
718 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
722 for (i = 0; i < sh->max_port; ++i) {
723 struct rte_eth_dev *dev;
725 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
727 * Or not existing port either no
728 * handler installed for this port.
732 dev = &rte_eth_devices[sh->port[i].ih_port_id];
734 if (dev->data->dev_conf.intr_conf.rmv)
735 rte_eth_dev_callback_process
736 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
741 * Handle shared asynchronous events the NIC (removal event
742 * and link status change). Supports multiport IB device.
748 mlx5_dev_interrupt_handler(void *cb_arg)
750 struct mlx5_dev_ctx_shared *sh = cb_arg;
751 struct ibv_async_event event;
753 /* Read all message from the IB device and acknowledge them. */
755 struct rte_eth_dev *dev;
758 if (mlx5_glue->get_async_event(sh->ctx, &event))
760 /* Retrieve and check IB port index. */
761 tmp = (uint32_t)event.element.port_num;
762 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
764 * The DEVICE_FATAL event is called once for
765 * entire device without port specifying.
766 * We should notify all existing ports.
768 mlx5_glue->ack_async_event(&event);
769 mlx5_dev_interrupt_device_fatal(sh);
772 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
774 /* Unsupported device level event. */
775 mlx5_glue->ack_async_event(&event);
777 "unsupported common event (type %d)",
781 if (tmp > sh->max_port) {
782 /* Invalid IB port index. */
783 mlx5_glue->ack_async_event(&event);
785 "cannot handle an event (type %d)"
786 "due to invalid IB port index (%u)",
787 event.event_type, tmp);
790 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
791 /* No handler installed. */
792 mlx5_glue->ack_async_event(&event);
794 "cannot handle an event (type %d)"
795 "due to no handler installed for port %u",
796 event.event_type, tmp);
799 /* Retrieve ethernet device descriptor. */
800 tmp = sh->port[tmp - 1].ih_port_id;
801 dev = &rte_eth_devices[tmp];
803 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
804 event.event_type == IBV_EVENT_PORT_ERR) &&
805 dev->data->dev_conf.intr_conf.lsc) {
806 mlx5_glue->ack_async_event(&event);
807 if (mlx5_link_update(dev, 0) == -EAGAIN) {
811 rte_eth_dev_callback_process
812 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
816 "port %u cannot handle an unknown event (type %d)",
817 dev->data->port_id, event.event_type);
818 mlx5_glue->ack_async_event(&event);
823 * Unregister callback handler safely. The handler may be active
824 * while we are trying to unregister it, in this case code -EAGAIN
825 * is returned by rte_intr_callback_unregister(). This routine checks
826 * the return code and tries to unregister handler again.
831 * pointer to callback routine
833 * opaque callback parameter
836 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
837 rte_intr_callback_fn cb_fn, void *cb_arg)
840 * Try to reduce timeout management overhead by not calling
841 * the timer related routines on the first iteration. If the
842 * unregistering succeeds on first call there will be no
843 * timer calls at all.
851 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
854 if (ret != -EAGAIN) {
855 DRV_LOG(INFO, "failed to unregister interrupt"
856 " handler (error: %d)", ret);
861 struct timespec onems;
863 /* Wait one millisecond and try again. */
865 onems.tv_nsec = NS_PER_S / MS_PER_S;
866 nanosleep(&onems, 0);
867 /* Check whether one second elapsed. */
868 if ((rte_get_timer_cycles() - start) <= twait)
872 * We get the amount of timer ticks for one second.
873 * If this amount elapsed it means we spent one
874 * second in waiting. This branch is executed once
875 * on first iteration.
877 twait = rte_get_timer_hz();
881 * Timeout elapsed, show message (once a second) and retry.
882 * We have no other acceptable option here, if we ignore
883 * the unregistering return code the handler will not
884 * be unregistered, fd will be closed and we may get the
885 * crush. Hanging and messaging in the loop seems not to be
888 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
889 start = rte_get_timer_cycles();
894 * Handle DEVX interrupts from the NIC.
895 * This function is probably called from the DPDK host thread.
901 mlx5_dev_interrupt_handler_devx(void *cb_arg)
903 #ifndef HAVE_IBV_DEVX_ASYNC
907 struct mlx5_dev_ctx_shared *sh = cb_arg;
909 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
910 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
911 MLX5_ST_SZ_BYTES(traffic_counter) +
912 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
914 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
916 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
919 mlx5_flow_async_pool_query_handle
920 (sh, (uint64_t)out.cmd_resp.wr_id,
921 mlx5_devx_get_out_command_status(buf));
922 #endif /* HAVE_IBV_DEVX_ASYNC */
926 * DPDK callback to bring the link DOWN.
929 * Pointer to Ethernet device structure.
932 * 0 on success, a negative errno value otherwise and rte_errno is set.
935 mlx5_set_link_down(struct rte_eth_dev *dev)
937 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
941 * DPDK callback to bring the link UP.
944 * Pointer to Ethernet device structure.
947 * 0 on success, a negative errno value otherwise and rte_errno is set.
950 mlx5_set_link_up(struct rte_eth_dev *dev)
952 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
956 * Check if mlx5 device was removed.
959 * Pointer to Ethernet device structure.
962 * 1 when device is removed, otherwise 0.
965 mlx5_is_removed(struct rte_eth_dev *dev)
967 struct ibv_device_attr device_attr;
968 struct mlx5_priv *priv = dev->data->dev_private;
970 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
976 * Analyze gathered port parameters via sysfs to recognize master
977 * and representor devices for E-Switch configuration.
979 * @param[in] device_dir
980 * flag of presence of "device" directory under port device key.
981 * @param[inout] switch_info
982 * Port information, including port name as a number and port name
986 * master and representor flags are set in switch_info according to
987 * recognized parameters (if any).
990 mlx5_sysfs_check_switch_info(bool device_dir,
991 struct mlx5_switch_info *switch_info)
993 switch (switch_info->name_type) {
994 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
996 * Name is not recognized, assume the master,
997 * check the device directory presence.
999 switch_info->master = device_dir;
1001 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1003 * Name is not set, this assumes the legacy naming
1004 * schema for master, just check if there is
1005 * a device directory.
1007 switch_info->master = device_dir;
1009 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1010 /* New uplink naming schema recognized. */
1011 switch_info->master = 1;
1013 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1014 /* Legacy representors naming schema. */
1015 switch_info->representor = !device_dir;
1017 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1019 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1020 /* New representors naming schema. */
1021 switch_info->representor = 1;
1027 * Get switch information associated with network interface.
1030 * Network interface index.
1032 * Switch information object, populated in case of success.
1035 * 0 on success, a negative errno value otherwise and rte_errno is set.
1038 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1040 char ifname[IF_NAMESIZE];
1041 char port_name[IF_NAMESIZE];
1043 struct mlx5_switch_info data = {
1046 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1051 bool port_switch_id_set = false;
1052 bool device_dir = false;
1056 if (!if_indextoname(ifindex, ifname)) {
1061 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1063 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1065 MKSTR(pci_device, "/sys/class/net/%s/device",
1068 file = fopen(phys_port_name, "rb");
1070 ret = fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", port_name);
1073 mlx5_translate_port_name(port_name, &data);
1075 file = fopen(phys_switch_id, "rb");
1080 port_switch_id_set =
1081 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1084 dir = opendir(pci_device);
1089 if (port_switch_id_set) {
1090 /* We have some E-Switch configuration. */
1091 mlx5_sysfs_check_switch_info(device_dir, &data);
1094 MLX5_ASSERT(!(data.master && data.representor));
1095 if (data.master && data.representor) {
1096 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1097 " and as representor", ifindex);
1105 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1108 * Pointer to Ethernet device structure.
1109 * @param[out] modinfo
1110 * Storage for plug-in module EEPROM information.
1113 * 0 on success, a negative errno value otherwise and rte_errno is set.
1116 mlx5_get_module_info(struct rte_eth_dev *dev,
1117 struct rte_eth_dev_module_info *modinfo)
1119 struct ethtool_modinfo info = {
1120 .cmd = ETHTOOL_GMODULEINFO,
1122 struct ifreq ifr = (struct ifreq) {
1123 .ifr_data = (void *)&info,
1127 if (!dev || !modinfo) {
1128 DRV_LOG(WARNING, "missing argument, cannot get module info");
1132 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1134 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1135 dev->data->port_id, strerror(rte_errno));
1138 modinfo->type = info.type;
1139 modinfo->eeprom_len = info.eeprom_len;
1144 * DPDK callback to retrieve plug-in module EEPROM data.
1147 * Pointer to Ethernet device structure.
1149 * Storage for plug-in module EEPROM data.
1152 * 0 on success, a negative errno value otherwise and rte_errno is set.
1154 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1155 struct rte_dev_eeprom_info *info)
1157 struct ethtool_eeprom *eeprom;
1161 if (!dev || !info) {
1162 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1166 eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1167 (sizeof(struct ethtool_eeprom) + info->length), 0,
1170 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1171 "eeprom data", dev->data->port_id);
1175 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1176 eeprom->offset = info->offset;
1177 eeprom->len = info->length;
1178 ifr = (struct ifreq) {
1179 .ifr_data = (void *)eeprom,
1181 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1183 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1184 dev->data->port_id, strerror(rte_errno));
1186 rte_memcpy(info->data, eeprom->data, info->length);
1192 * Read device counters table.
1195 * Pointer to Ethernet device.
1197 * Counters table output buffer.
1200 * 0 on success and stats is filled, negative errno value otherwise and
1204 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1206 struct mlx5_priv *priv = dev->data->dev_private;
1207 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1210 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1211 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1212 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1215 et_stats->cmd = ETHTOOL_GSTATS;
1216 et_stats->n_stats = xstats_ctrl->stats_n;
1217 ifr.ifr_data = (caddr_t)et_stats;
1218 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1221 "port %u unable to read statistic values from device",
1222 dev->data->port_id);
1225 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1226 if (xstats_ctrl->info[i].dev) {
1227 ret = mlx5_os_read_dev_stat(priv,
1228 xstats_ctrl->info[i].ctr_name,
1230 /* return last xstats counter if fail to read. */
1232 xstats_ctrl->xstats[i] = stats[i];
1234 stats[i] = xstats_ctrl->xstats[i];
1236 stats[i] = (uint64_t)
1237 et_stats->data[xstats_ctrl->dev_table_idx[i]];
1244 * Query the number of statistics provided by ETHTOOL.
1247 * Pointer to Ethernet device.
1250 * Number of statistics on success, negative errno value otherwise and
1254 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1256 struct ethtool_drvinfo drvinfo;
1260 drvinfo.cmd = ETHTOOL_GDRVINFO;
1261 ifr.ifr_data = (caddr_t)&drvinfo;
1262 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1264 DRV_LOG(WARNING, "port %u unable to query number of statistics",
1265 dev->data->port_id);
1268 return drvinfo.n_stats;
1271 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1273 .dpdk_name = "rx_port_unicast_bytes",
1274 .ctr_name = "rx_vport_unicast_bytes",
1277 .dpdk_name = "rx_port_multicast_bytes",
1278 .ctr_name = "rx_vport_multicast_bytes",
1281 .dpdk_name = "rx_port_broadcast_bytes",
1282 .ctr_name = "rx_vport_broadcast_bytes",
1285 .dpdk_name = "rx_port_unicast_packets",
1286 .ctr_name = "rx_vport_unicast_packets",
1289 .dpdk_name = "rx_port_multicast_packets",
1290 .ctr_name = "rx_vport_multicast_packets",
1293 .dpdk_name = "rx_port_broadcast_packets",
1294 .ctr_name = "rx_vport_broadcast_packets",
1297 .dpdk_name = "tx_port_unicast_bytes",
1298 .ctr_name = "tx_vport_unicast_bytes",
1301 .dpdk_name = "tx_port_multicast_bytes",
1302 .ctr_name = "tx_vport_multicast_bytes",
1305 .dpdk_name = "tx_port_broadcast_bytes",
1306 .ctr_name = "tx_vport_broadcast_bytes",
1309 .dpdk_name = "tx_port_unicast_packets",
1310 .ctr_name = "tx_vport_unicast_packets",
1313 .dpdk_name = "tx_port_multicast_packets",
1314 .ctr_name = "tx_vport_multicast_packets",
1317 .dpdk_name = "tx_port_broadcast_packets",
1318 .ctr_name = "tx_vport_broadcast_packets",
1321 .dpdk_name = "rx_wqe_err",
1322 .ctr_name = "rx_wqe_err",
1325 .dpdk_name = "rx_crc_errors_phy",
1326 .ctr_name = "rx_crc_errors_phy",
1329 .dpdk_name = "rx_in_range_len_errors_phy",
1330 .ctr_name = "rx_in_range_len_errors_phy",
1333 .dpdk_name = "rx_symbol_err_phy",
1334 .ctr_name = "rx_symbol_err_phy",
1337 .dpdk_name = "tx_errors_phy",
1338 .ctr_name = "tx_errors_phy",
1341 .dpdk_name = "rx_out_of_buffer",
1342 .ctr_name = "out_of_buffer",
1346 .dpdk_name = "tx_packets_phy",
1347 .ctr_name = "tx_packets_phy",
1350 .dpdk_name = "rx_packets_phy",
1351 .ctr_name = "rx_packets_phy",
1354 .dpdk_name = "tx_discards_phy",
1355 .ctr_name = "tx_discards_phy",
1358 .dpdk_name = "rx_discards_phy",
1359 .ctr_name = "rx_discards_phy",
1362 .dpdk_name = "tx_bytes_phy",
1363 .ctr_name = "tx_bytes_phy",
1366 .dpdk_name = "rx_bytes_phy",
1367 .ctr_name = "rx_bytes_phy",
1369 /* Representor only */
1371 .dpdk_name = "rx_packets",
1372 .ctr_name = "vport_rx_packets",
1375 .dpdk_name = "rx_bytes",
1376 .ctr_name = "vport_rx_bytes",
1379 .dpdk_name = "tx_packets",
1380 .ctr_name = "vport_tx_packets",
1383 .dpdk_name = "tx_bytes",
1384 .ctr_name = "vport_tx_bytes",
1388 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1391 * Init the structures to read device counters.
1394 * Pointer to Ethernet device.
1397 mlx5_os_stats_init(struct rte_eth_dev *dev)
1399 struct mlx5_priv *priv = dev->data->dev_private;
1400 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1401 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1405 struct ethtool_gstrings *strings = NULL;
1406 unsigned int dev_stats_n;
1407 unsigned int str_sz;
1410 /* So that it won't aggregate for each init. */
1411 xstats_ctrl->mlx5_stats_n = 0;
1412 ret = mlx5_os_get_stats_n(dev);
1414 DRV_LOG(WARNING, "port %u no extended statistics available",
1415 dev->data->port_id);
1419 /* Allocate memory to grab stat names and values. */
1420 str_sz = dev_stats_n * ETH_GSTRING_LEN;
1421 strings = (struct ethtool_gstrings *)
1422 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1425 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1426 dev->data->port_id);
1429 strings->cmd = ETHTOOL_GSTRINGS;
1430 strings->string_set = ETH_SS_STATS;
1431 strings->len = dev_stats_n;
1432 ifr.ifr_data = (caddr_t)strings;
1433 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1435 DRV_LOG(WARNING, "port %u unable to get statistic names",
1436 dev->data->port_id);
1439 for (i = 0; i != dev_stats_n; ++i) {
1440 const char *curr_string = (const char *)
1441 &strings->data[i * ETH_GSTRING_LEN];
1443 for (j = 0; j != xstats_n; ++j) {
1444 if (!strcmp(mlx5_counters_init[j].ctr_name,
1446 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1448 xstats_ctrl->dev_table_idx[idx] = i;
1449 xstats_ctrl->info[idx] = mlx5_counters_init[j];
1454 /* Add dev counters. */
1455 for (i = 0; i != xstats_n; ++i) {
1456 if (mlx5_counters_init[i].dev) {
1457 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1459 xstats_ctrl->info[idx] = mlx5_counters_init[i];
1460 xstats_ctrl->hw_stats[idx] = 0;
1463 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1464 xstats_ctrl->stats_n = dev_stats_n;
1465 /* Copy to base at first time. */
1466 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1468 DRV_LOG(ERR, "port %u cannot read device counters: %s",
1469 dev->data->port_id, strerror(rte_errno));
1470 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1471 stats_ctrl->imissed = 0;
1477 * Get MAC address by querying netdevice.
1480 * Pointer to Ethernet device.
1482 * MAC address output buffer.
1485 * 0 on success, a negative errno value otherwise and rte_errno is set.
1488 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1490 struct ifreq request;
1493 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1496 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);