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>
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 */
132 /* Get interface index from SubFunction device name. */
134 mlx5_auxiliary_get_ifindex(const char *sf_name)
136 char if_name[IF_NAMESIZE] = { 0 };
138 if (mlx5_auxiliary_get_child_name(sf_name, "/net",
139 if_name, sizeof(if_name)) != 0)
141 return if_nametoindex(if_name);
145 * Get interface name from private structure.
147 * This is a port representor-aware version of mlx5_get_ifname_sysfs().
150 * Pointer to Ethernet device.
152 * Interface name output buffer.
155 * 0 on success, a negative errno value otherwise and rte_errno is set.
158 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[MLX5_NAMESIZE])
160 struct mlx5_priv *priv = dev->data->dev_private;
161 unsigned int ifindex;
164 MLX5_ASSERT(priv->sh);
165 if (priv->master && priv->sh->bond.ifindex > 0) {
166 memcpy(ifname, priv->sh->bond.ifname, MLX5_NAMESIZE);
169 ifindex = mlx5_ifindex(dev);
171 if (!priv->representor)
172 return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
177 if (if_indextoname(ifindex, &(*ifname)[0]))
184 * Perform ifreq ioctl() on associated netdev ifname.
187 * Pointer to netdev name.
189 * Request number to pass to ioctl().
191 * Interface request structure output buffer.
194 * 0 on success, a negative errno value otherwise and rte_errno is set.
197 mlx5_ifreq_by_ifname(const char *ifname, int req, struct ifreq *ifr)
199 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
206 rte_strscpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
207 ret = ioctl(sock, req, ifr);
220 * Perform ifreq ioctl() on associated Ethernet device.
223 * Pointer to Ethernet device.
225 * Request number to pass to ioctl().
227 * Interface request structure output buffer.
230 * 0 on success, a negative errno value otherwise and rte_errno is set.
233 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
235 char ifname[sizeof(ifr->ifr_name)];
238 ret = mlx5_get_ifname(dev, &ifname);
241 return mlx5_ifreq_by_ifname(ifname, req, ifr);
248 * Pointer to Ethernet device.
250 * MTU value output buffer.
253 * 0 on success, a negative errno value otherwise and rte_errno is set.
256 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
258 struct ifreq request;
259 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
263 *mtu = request.ifr_mtu;
271 * Pointer to Ethernet device.
276 * 0 on success, a negative errno value otherwise and rte_errno is set.
279 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
281 struct ifreq request = { .ifr_mtu = mtu, };
283 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
290 * Pointer to Ethernet device.
292 * Bitmask for flags that must remain untouched.
294 * Bitmask for flags to modify.
297 * 0 on success, a negative errno value otherwise and rte_errno is set.
300 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
302 struct ifreq request;
303 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
307 request.ifr_flags &= keep;
308 request.ifr_flags |= flags & ~keep;
309 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
313 * Get device current raw clock counter
316 * Pointer to Ethernet device structure.
318 * Current raw clock counter of the device.
321 * 0 if the clock has correctly been read
322 * The value of errno in case of error
325 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
327 struct mlx5_priv *priv = dev->data->dev_private;
328 struct ibv_context *ctx = priv->sh->cdev->ctx;
329 struct ibv_values_ex values;
332 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
333 err = mlx5_glue->query_rt_values_ex(ctx, &values);
335 DRV_LOG(WARNING, "Could not query the clock !");
338 *clock = values.raw_clock.tv_nsec;
343 * Retrieve the master device for representor in the same switch domain.
346 * Pointer to representor Ethernet device structure.
349 * Master device structure on success, NULL otherwise.
351 static struct rte_eth_dev *
352 mlx5_find_master_dev(struct rte_eth_dev *dev)
354 struct mlx5_priv *priv;
358 priv = dev->data->dev_private;
359 domain_id = priv->domain_id;
360 MLX5_ASSERT(priv->representor);
361 MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
362 struct mlx5_priv *opriv =
363 rte_eth_devices[port_id].data->dev_private;
366 opriv->domain_id == domain_id &&
367 opriv->sh == priv->sh)
368 return &rte_eth_devices[port_id];
374 * DPDK callback to retrieve physical link information.
377 * Pointer to Ethernet device structure.
379 * Storage for current link status.
382 * 0 on success, a negative errno value otherwise and rte_errno is set.
385 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
386 struct rte_eth_link *link)
388 struct mlx5_priv *priv = dev->data->dev_private;
389 struct ethtool_cmd edata = {
390 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
393 struct rte_eth_link dev_link;
397 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
399 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
400 dev->data->port_id, strerror(rte_errno));
403 dev_link = (struct rte_eth_link) {
404 .link_status = ((ifr.ifr_flags & IFF_UP) &&
405 (ifr.ifr_flags & IFF_RUNNING)),
407 ifr = (struct ifreq) {
408 .ifr_data = (void *)&edata,
410 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
412 if (ret == -ENOTSUP && priv->representor) {
413 struct rte_eth_dev *master;
416 * For representors we can try to inherit link
417 * settings from the master device. Actually
418 * link settings do not make a lot of sense
419 * for representors due to missing physical
420 * link. The old kernel drivers supported
421 * emulated settings query for representors,
422 * the new ones do not, so we have to add
423 * this code for compatibility issues.
425 master = mlx5_find_master_dev(dev);
427 ifr = (struct ifreq) {
428 .ifr_data = (void *)&edata,
430 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
435 "port %u ioctl(SIOCETHTOOL,"
436 " ETHTOOL_GSET) failed: %s",
437 dev->data->port_id, strerror(rte_errno));
441 link_speed = ethtool_cmd_speed(&edata);
442 if (link_speed == -1)
443 dev_link.link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
445 dev_link.link_speed = link_speed;
446 priv->link_speed_capa = 0;
447 if (edata.supported & (SUPPORTED_1000baseT_Full |
448 SUPPORTED_1000baseKX_Full))
449 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G;
450 if (edata.supported & SUPPORTED_10000baseKR_Full)
451 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G;
452 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
453 SUPPORTED_40000baseCR4_Full |
454 SUPPORTED_40000baseSR4_Full |
455 SUPPORTED_40000baseLR4_Full))
456 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G;
457 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
458 RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX);
459 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
460 RTE_ETH_LINK_SPEED_FIXED);
466 * Retrieve physical link information (unlocked version using new ioctl).
469 * Pointer to Ethernet device structure.
471 * Storage for current link status.
474 * 0 on success, a negative errno value otherwise and rte_errno is set.
477 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
478 struct rte_eth_link *link)
481 struct mlx5_priv *priv = dev->data->dev_private;
482 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
484 struct rte_eth_link dev_link;
485 struct rte_eth_dev *master = NULL;
489 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
491 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
492 dev->data->port_id, strerror(rte_errno));
495 dev_link = (struct rte_eth_link) {
496 .link_status = ((ifr.ifr_flags & IFF_UP) &&
497 (ifr.ifr_flags & IFF_RUNNING)),
499 ifr = (struct ifreq) {
500 .ifr_data = (void *)&gcmd,
502 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
504 if (ret == -ENOTSUP && priv->representor) {
506 * For representors we can try to inherit link
507 * settings from the master device. Actually
508 * link settings do not make a lot of sense
509 * for representors due to missing physical
510 * link. The old kernel drivers supported
511 * emulated settings query for representors,
512 * the new ones do not, so we have to add
513 * this code for compatibility issues.
515 master = mlx5_find_master_dev(dev);
517 ifr = (struct ifreq) {
518 .ifr_data = (void *)&gcmd,
520 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
525 "port %u ioctl(SIOCETHTOOL,"
526 " ETHTOOL_GLINKSETTINGS) failed: %s",
527 dev->data->port_id, strerror(rte_errno));
531 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
533 alignas(struct ethtool_link_settings)
534 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
535 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
536 struct ethtool_link_settings *ecmd = (void *)data;
539 ifr.ifr_data = (void *)ecmd;
540 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
543 "port %u ioctl(SIOCETHTOOL,"
544 "ETHTOOL_GLINKSETTINGS) failed: %s",
545 dev->data->port_id, strerror(rte_errno));
548 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ?
549 RTE_ETH_SPEED_NUM_UNKNOWN : ecmd->speed;
550 sc = ecmd->link_mode_masks[0] |
551 ((uint64_t)ecmd->link_mode_masks[1] << 32);
552 priv->link_speed_capa = 0;
553 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
554 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
555 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G;
556 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
557 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
558 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
559 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G;
560 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
561 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
562 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_20G;
563 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
564 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
565 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
566 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
567 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G;
568 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
569 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
570 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
571 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
572 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_56G;
573 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
574 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
575 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
576 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_25G;
577 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
578 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
579 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_50G;
580 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
581 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
582 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
583 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
584 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_100G;
585 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
586 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
587 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G;
589 sc = ecmd->link_mode_masks[2] |
590 ((uint64_t)ecmd->link_mode_masks[3] << 32);
591 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
593 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
594 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
595 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G;
596 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
597 RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX);
598 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
599 RTE_ETH_LINK_SPEED_FIXED);
605 * DPDK callback to retrieve physical link information.
608 * Pointer to Ethernet device structure.
609 * @param wait_to_complete
610 * Wait for request completion.
613 * 0 if link status was not updated, positive if it was, a negative errno
614 * value otherwise and rte_errno is set.
617 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
620 struct rte_eth_link dev_link;
621 time_t start_time = time(NULL);
622 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
625 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
627 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
630 /* Handle wait to complete situation. */
631 if ((wait_to_complete || retry) && ret == -EAGAIN) {
632 if (abs((int)difftime(time(NULL), start_time)) <
633 MLX5_LINK_STATUS_TIMEOUT) {
640 } else if (ret < 0) {
643 } while (wait_to_complete || retry-- > 0);
644 ret = !!memcmp(&dev->data->dev_link, &dev_link,
645 sizeof(struct rte_eth_link));
646 dev->data->dev_link = dev_link;
651 * DPDK callback to get flow control status.
654 * Pointer to Ethernet device structure.
655 * @param[out] fc_conf
656 * Flow control output buffer.
659 * 0 on success, a negative errno value otherwise and rte_errno is set.
662 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
665 struct ethtool_pauseparam ethpause = {
666 .cmd = ETHTOOL_GPAUSEPARAM
670 ifr.ifr_data = (void *)ðpause;
671 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
674 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
676 dev->data->port_id, strerror(rte_errno));
679 fc_conf->autoneg = ethpause.autoneg;
680 if (ethpause.rx_pause && ethpause.tx_pause)
681 fc_conf->mode = RTE_ETH_FC_FULL;
682 else if (ethpause.rx_pause)
683 fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
684 else if (ethpause.tx_pause)
685 fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
687 fc_conf->mode = RTE_ETH_FC_NONE;
692 * DPDK callback to modify flow control parameters.
695 * Pointer to Ethernet device structure.
697 * Flow control parameters.
700 * 0 on success, a negative errno value otherwise and rte_errno is set.
703 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
706 struct ethtool_pauseparam ethpause = {
707 .cmd = ETHTOOL_SPAUSEPARAM
711 ifr.ifr_data = (void *)ðpause;
712 ethpause.autoneg = fc_conf->autoneg;
713 if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) ||
714 (fc_conf->mode & RTE_ETH_FC_RX_PAUSE))
715 ethpause.rx_pause = 1;
717 ethpause.rx_pause = 0;
719 if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) ||
720 (fc_conf->mode & RTE_ETH_FC_TX_PAUSE))
721 ethpause.tx_pause = 1;
723 ethpause.tx_pause = 0;
724 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
727 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
729 dev->data->port_id, strerror(rte_errno));
736 * Handle asynchronous removal event for entire multiport device.
739 * Infiniband device shared context.
742 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
746 for (i = 0; i < sh->max_port; ++i) {
747 struct rte_eth_dev *dev;
749 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
751 * Or not existing port either no
752 * handler installed for this port.
756 dev = &rte_eth_devices[sh->port[i].ih_port_id];
758 if (dev->data->dev_conf.intr_conf.rmv)
759 rte_eth_dev_callback_process
760 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
765 mlx5_dev_interrupt_nl_cb(struct nlmsghdr *hdr, void *cb_arg)
767 struct mlx5_dev_ctx_shared *sh = cb_arg;
771 if (mlx5_nl_parse_link_status_update(hdr, &if_index) < 0)
773 for (i = 0; i < sh->max_port; i++) {
774 struct mlx5_dev_shared_port *port = &sh->port[i];
775 struct rte_eth_dev *dev;
776 struct mlx5_priv *priv;
778 if (port->nl_ih_port_id >= RTE_MAX_ETHPORTS)
780 dev = &rte_eth_devices[port->nl_ih_port_id];
781 /* Probing may initiate an LSC before configuration is done. */
782 if (dev->data->dev_configured &&
783 !dev->data->dev_conf.intr_conf.lsc)
785 priv = dev->data->dev_private;
786 if (priv->if_index == if_index) {
787 /* Block logical LSC events. */
788 uint16_t prev_status = dev->data->dev_link.link_status;
790 if (mlx5_link_update(dev, 0) < 0)
791 DRV_LOG(ERR, "Failed to update link status: %s",
792 rte_strerror(rte_errno));
793 else if (prev_status != dev->data->dev_link.link_status)
794 rte_eth_dev_callback_process
795 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
802 mlx5_dev_interrupt_handler_nl(void *arg)
804 struct mlx5_dev_ctx_shared *sh = arg;
805 int nlsk_fd = rte_intr_fd_get(sh->intr_handle_nl);
809 if (mlx5_nl_read_events(nlsk_fd, mlx5_dev_interrupt_nl_cb, sh) < 0)
810 DRV_LOG(ERR, "Failed to process Netlink events: %s",
811 rte_strerror(rte_errno));
815 * Handle shared asynchronous events the NIC (removal event
816 * and link status change). Supports multiport IB device.
822 mlx5_dev_interrupt_handler(void *cb_arg)
824 struct mlx5_dev_ctx_shared *sh = cb_arg;
825 struct ibv_async_event event;
827 /* Read all message from the IB device and acknowledge them. */
829 struct rte_eth_dev *dev;
832 if (mlx5_glue->get_async_event(sh->cdev->ctx, &event))
834 /* Retrieve and check IB port index. */
835 tmp = (uint32_t)event.element.port_num;
836 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
838 * The DEVICE_FATAL event is called once for
839 * entire device without port specifying.
840 * We should notify all existing ports.
842 mlx5_glue->ack_async_event(&event);
843 mlx5_dev_interrupt_device_fatal(sh);
846 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
848 /* Unsupported device level event. */
849 mlx5_glue->ack_async_event(&event);
851 "unsupported common event (type %d)",
855 if (tmp > sh->max_port) {
856 /* Invalid IB port index. */
857 mlx5_glue->ack_async_event(&event);
859 "cannot handle an event (type %d)"
860 "due to invalid IB port index (%u)",
861 event.event_type, tmp);
864 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
865 /* No handler installed. */
866 mlx5_glue->ack_async_event(&event);
868 "cannot handle an event (type %d)"
869 "due to no handler installed for port %u",
870 event.event_type, tmp);
873 /* Retrieve ethernet device descriptor. */
874 tmp = sh->port[tmp - 1].ih_port_id;
875 dev = &rte_eth_devices[tmp];
878 "port %u cannot handle an unknown event (type %d)",
879 dev->data->port_id, event.event_type);
880 mlx5_glue->ack_async_event(&event);
885 * Unregister callback handler safely. The handler may be active
886 * while we are trying to unregister it, in this case code -EAGAIN
887 * is returned by rte_intr_callback_unregister(). This routine checks
888 * the return code and tries to unregister handler again.
893 * pointer to callback routine
895 * opaque callback parameter
898 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
899 rte_intr_callback_fn cb_fn, void *cb_arg)
902 * Try to reduce timeout management overhead by not calling
903 * the timer related routines on the first iteration. If the
904 * unregistering succeeds on first call there will be no
905 * timer calls at all.
913 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
916 if (ret != -EAGAIN) {
917 DRV_LOG(INFO, "failed to unregister interrupt"
918 " handler (error: %d)", ret);
923 struct timespec onems;
925 /* Wait one millisecond and try again. */
927 onems.tv_nsec = NS_PER_S / MS_PER_S;
928 nanosleep(&onems, 0);
929 /* Check whether one second elapsed. */
930 if ((rte_get_timer_cycles() - start) <= twait)
934 * We get the amount of timer ticks for one second.
935 * If this amount elapsed it means we spent one
936 * second in waiting. This branch is executed once
937 * on first iteration.
939 twait = rte_get_timer_hz();
943 * Timeout elapsed, show message (once a second) and retry.
944 * We have no other acceptable option here, if we ignore
945 * the unregistering return code the handler will not
946 * be unregistered, fd will be closed and we may get the
947 * crush. Hanging and messaging in the loop seems not to be
950 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
951 start = rte_get_timer_cycles();
956 * Handle DEVX interrupts from the NIC.
957 * This function is probably called from the DPDK host thread.
963 mlx5_dev_interrupt_handler_devx(void *cb_arg)
965 #ifndef HAVE_IBV_DEVX_ASYNC
969 struct mlx5_dev_ctx_shared *sh = cb_arg;
971 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
972 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
973 MLX5_ST_SZ_BYTES(traffic_counter) +
974 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
976 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
978 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
981 mlx5_flow_async_pool_query_handle
982 (sh, (uint64_t)out.cmd_resp.wr_id,
983 mlx5_devx_get_out_command_status(buf));
984 #endif /* HAVE_IBV_DEVX_ASYNC */
988 * DPDK callback to bring the link DOWN.
991 * Pointer to Ethernet device structure.
994 * 0 on success, a negative errno value otherwise and rte_errno is set.
997 mlx5_set_link_down(struct rte_eth_dev *dev)
999 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1003 * DPDK callback to bring the link UP.
1006 * Pointer to Ethernet device structure.
1009 * 0 on success, a negative errno value otherwise and rte_errno is set.
1012 mlx5_set_link_up(struct rte_eth_dev *dev)
1014 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1018 * Check if mlx5 device was removed.
1021 * Pointer to Ethernet device structure.
1024 * 1 when device is removed, otherwise 0.
1027 mlx5_is_removed(struct rte_eth_dev *dev)
1029 struct ibv_device_attr device_attr;
1030 struct mlx5_priv *priv = dev->data->dev_private;
1032 if (mlx5_glue->query_device(priv->sh->cdev->ctx, &device_attr) == EIO)
1038 * Analyze gathered port parameters via sysfs to recognize master
1039 * and representor devices for E-Switch configuration.
1041 * @param[in] device_dir
1042 * flag of presence of "device" directory under port device key.
1043 * @param[inout] switch_info
1044 * Port information, including port name as a number and port name
1045 * type if recognized
1048 * master and representor flags are set in switch_info according to
1049 * recognized parameters (if any).
1052 mlx5_sysfs_check_switch_info(bool device_dir,
1053 struct mlx5_switch_info *switch_info)
1055 switch (switch_info->name_type) {
1056 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1058 * Name is not recognized, assume the master,
1059 * check the device directory presence.
1061 switch_info->master = device_dir;
1063 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1065 * Name is not set, this assumes the legacy naming
1066 * schema for master, just check if there is
1067 * a device directory.
1069 switch_info->master = device_dir;
1071 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1072 /* New uplink naming schema recognized. */
1073 switch_info->master = 1;
1075 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1076 /* Legacy representors naming schema. */
1077 switch_info->representor = !device_dir;
1079 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1081 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1083 case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1084 /* New representors naming schema. */
1085 switch_info->representor = 1;
1088 switch_info->master = device_dir;
1094 * Get switch information associated with network interface.
1097 * Network interface index.
1099 * Switch information object, populated in case of success.
1102 * 0 on success, a negative errno value otherwise and rte_errno is set.
1105 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1107 char ifname[IF_NAMESIZE];
1108 char port_name[IF_NAMESIZE];
1110 struct mlx5_switch_info data = {
1113 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1118 bool port_switch_id_set = false;
1119 bool device_dir = false;
1123 if (!if_indextoname(ifindex, ifname)) {
1128 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1130 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1132 MKSTR(pci_device, "/sys/class/net/%s/device",
1135 file = fopen(phys_port_name, "rb");
1137 ret = fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", port_name);
1140 mlx5_translate_port_name(port_name, &data);
1142 file = fopen(phys_switch_id, "rb");
1147 port_switch_id_set =
1148 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1151 dir = opendir(pci_device);
1156 if (port_switch_id_set) {
1157 /* We have some E-Switch configuration. */
1158 mlx5_sysfs_check_switch_info(device_dir, &data);
1161 MLX5_ASSERT(!(data.master && data.representor));
1162 if (data.master && data.representor) {
1163 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1164 " and as representor", ifindex);
1172 * Get bond information associated with network interface.
1175 * Network interface index of bond slave interface
1176 * @param[out] ifindex
1177 * Pointer to bond ifindex.
1178 * @param[out] ifname
1179 * Pointer to bond ifname.
1182 * 0 on success, a negative errno value otherwise and rte_errno is set.
1185 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex,
1188 char name[IF_NAMESIZE];
1193 if (!if_indextoname(pf_ifindex, name) || !strlen(name)) {
1197 MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name);
1198 /* read bond ifindex */
1199 file = fopen(bond_if, "rb");
1204 ret = fscanf(file, "%u", &index);
1213 /* read bond device name from symbol link */
1215 if (!if_indextoname(index, ifname)) {
1224 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1227 * Pointer to Ethernet device structure.
1228 * @param[out] modinfo
1229 * Storage for plug-in module EEPROM information.
1232 * 0 on success, a negative errno value otherwise and rte_errno is set.
1235 mlx5_get_module_info(struct rte_eth_dev *dev,
1236 struct rte_eth_dev_module_info *modinfo)
1238 struct ethtool_modinfo info = {
1239 .cmd = ETHTOOL_GMODULEINFO,
1241 struct ifreq ifr = (struct ifreq) {
1242 .ifr_data = (void *)&info,
1247 DRV_LOG(WARNING, "missing argument, cannot get module info");
1251 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1253 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1254 dev->data->port_id, strerror(rte_errno));
1257 modinfo->type = info.type;
1258 modinfo->eeprom_len = info.eeprom_len;
1263 * DPDK callback to retrieve plug-in module EEPROM data.
1266 * Pointer to Ethernet device structure.
1268 * Storage for plug-in module EEPROM data.
1271 * 0 on success, a negative errno value otherwise and rte_errno is set.
1273 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1274 struct rte_dev_eeprom_info *info)
1276 struct ethtool_eeprom *eeprom;
1281 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1285 eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1286 (sizeof(struct ethtool_eeprom) + info->length), 0,
1289 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1290 "eeprom data", dev->data->port_id);
1294 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1295 eeprom->offset = info->offset;
1296 eeprom->len = info->length;
1297 ifr = (struct ifreq) {
1298 .ifr_data = (void *)eeprom,
1300 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1302 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1303 dev->data->port_id, strerror(rte_errno));
1305 rte_memcpy(info->data, eeprom->data, info->length);
1311 * Read device counters table.
1314 * Pointer to Ethernet device.
1316 * PF index in case of bonding device, -1 otherwise
1318 * Counters table output buffer.
1321 * 0 on success and stats is filled, negative errno value otherwise and
1325 _mlx5_os_read_dev_counters(struct rte_eth_dev *dev, int pf, uint64_t *stats)
1327 struct mlx5_priv *priv = dev->data->dev_private;
1328 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1331 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1332 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1333 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1336 et_stats->cmd = ETHTOOL_GSTATS;
1337 et_stats->n_stats = xstats_ctrl->stats_n;
1338 ifr.ifr_data = (caddr_t)et_stats;
1340 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[pf].ifname,
1343 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1346 "port %u unable to read statistic values from device",
1347 dev->data->port_id);
1350 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1351 if (xstats_ctrl->info[i].dev)
1353 stats[i] += (uint64_t)
1354 et_stats->data[xstats_ctrl->dev_table_idx[i]];
1360 * Read device counters.
1363 * Pointer to Ethernet device.
1365 * Counters table output buffer.
1368 * 0 on success and stats is filled, negative errno value otherwise and
1372 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1374 struct mlx5_priv *priv = dev->data->dev_private;
1375 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1378 memset(stats, 0, sizeof(*stats) * xstats_ctrl->mlx5_stats_n);
1379 /* Read ifreq counters. */
1380 if (priv->master && priv->pf_bond >= 0) {
1381 /* Sum xstats from bonding device member ports. */
1382 for (i = 0; i < priv->sh->bond.n_port; i++) {
1383 ret = _mlx5_os_read_dev_counters(dev, i, stats);
1388 ret = _mlx5_os_read_dev_counters(dev, -1, stats);
1390 /* Read IB counters. */
1391 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1392 if (!xstats_ctrl->info[i].dev)
1394 ret = mlx5_os_read_dev_stat(priv, xstats_ctrl->info[i].ctr_name,
1396 /* return last xstats counter if fail to read. */
1398 xstats_ctrl->xstats[i] = stats[i];
1400 stats[i] = xstats_ctrl->xstats[i];
1406 * Query the number of statistics provided by ETHTOOL.
1409 * Pointer to Ethernet device.
1412 * Number of statistics on success, negative errno value otherwise and
1416 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1418 struct mlx5_priv *priv = dev->data->dev_private;
1419 struct ethtool_drvinfo drvinfo;
1423 drvinfo.cmd = ETHTOOL_GDRVINFO;
1424 ifr.ifr_data = (caddr_t)&drvinfo;
1425 if (priv->master && priv->pf_bond >= 0)
1427 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1430 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1432 DRV_LOG(WARNING, "port %u unable to query number of statistics",
1433 dev->data->port_id);
1436 return drvinfo.n_stats;
1439 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1441 .dpdk_name = "rx_unicast_bytes",
1442 .ctr_name = "rx_vport_unicast_bytes",
1445 .dpdk_name = "rx_multicast_bytes",
1446 .ctr_name = "rx_vport_multicast_bytes",
1449 .dpdk_name = "rx_broadcast_bytes",
1450 .ctr_name = "rx_vport_broadcast_bytes",
1453 .dpdk_name = "rx_unicast_packets",
1454 .ctr_name = "rx_vport_unicast_packets",
1457 .dpdk_name = "rx_multicast_packets",
1458 .ctr_name = "rx_vport_multicast_packets",
1461 .dpdk_name = "rx_broadcast_packets",
1462 .ctr_name = "rx_vport_broadcast_packets",
1465 .dpdk_name = "tx_unicast_bytes",
1466 .ctr_name = "tx_vport_unicast_bytes",
1469 .dpdk_name = "tx_multicast_bytes",
1470 .ctr_name = "tx_vport_multicast_bytes",
1473 .dpdk_name = "tx_broadcast_bytes",
1474 .ctr_name = "tx_vport_broadcast_bytes",
1477 .dpdk_name = "tx_unicast_packets",
1478 .ctr_name = "tx_vport_unicast_packets",
1481 .dpdk_name = "tx_multicast_packets",
1482 .ctr_name = "tx_vport_multicast_packets",
1485 .dpdk_name = "tx_broadcast_packets",
1486 .ctr_name = "tx_vport_broadcast_packets",
1489 .dpdk_name = "rx_wqe_errors",
1490 .ctr_name = "rx_wqe_err",
1493 .dpdk_name = "rx_phy_crc_errors",
1494 .ctr_name = "rx_crc_errors_phy",
1497 .dpdk_name = "rx_phy_in_range_len_errors",
1498 .ctr_name = "rx_in_range_len_errors_phy",
1501 .dpdk_name = "rx_phy_symbol_errors",
1502 .ctr_name = "rx_symbol_err_phy",
1505 .dpdk_name = "tx_phy_errors",
1506 .ctr_name = "tx_errors_phy",
1509 .dpdk_name = "rx_out_of_buffer",
1510 .ctr_name = "out_of_buffer",
1514 .dpdk_name = "tx_phy_packets",
1515 .ctr_name = "tx_packets_phy",
1518 .dpdk_name = "rx_phy_packets",
1519 .ctr_name = "rx_packets_phy",
1522 .dpdk_name = "tx_phy_discard_packets",
1523 .ctr_name = "tx_discards_phy",
1526 .dpdk_name = "rx_phy_discard_packets",
1527 .ctr_name = "rx_discards_phy",
1530 .dpdk_name = "tx_phy_bytes",
1531 .ctr_name = "tx_bytes_phy",
1534 .dpdk_name = "rx_phy_bytes",
1535 .ctr_name = "rx_bytes_phy",
1537 /* Representor only */
1539 .dpdk_name = "rx_vport_packets",
1540 .ctr_name = "vport_rx_packets",
1543 .dpdk_name = "rx_vport_bytes",
1544 .ctr_name = "vport_rx_bytes",
1547 .dpdk_name = "tx_vport_packets",
1548 .ctr_name = "vport_tx_packets",
1551 .dpdk_name = "tx_vport_bytes",
1552 .ctr_name = "vport_tx_bytes",
1556 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1559 * Init the structures to read device counters.
1562 * Pointer to Ethernet device.
1565 mlx5_os_stats_init(struct rte_eth_dev *dev)
1567 struct mlx5_priv *priv = dev->data->dev_private;
1568 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1569 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1573 struct ethtool_gstrings *strings = NULL;
1574 unsigned int dev_stats_n;
1575 unsigned int str_sz;
1578 /* So that it won't aggregate for each init. */
1579 xstats_ctrl->mlx5_stats_n = 0;
1580 ret = mlx5_os_get_stats_n(dev);
1582 DRV_LOG(WARNING, "port %u no extended statistics available",
1583 dev->data->port_id);
1587 /* Allocate memory to grab stat names and values. */
1588 str_sz = dev_stats_n * ETH_GSTRING_LEN;
1589 strings = (struct ethtool_gstrings *)
1590 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1593 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1594 dev->data->port_id);
1597 strings->cmd = ETHTOOL_GSTRINGS;
1598 strings->string_set = ETH_SS_STATS;
1599 strings->len = dev_stats_n;
1600 ifr.ifr_data = (caddr_t)strings;
1601 if (priv->master && priv->pf_bond >= 0)
1602 /* Bonding master. */
1603 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1606 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1608 DRV_LOG(WARNING, "port %u unable to get statistic names",
1609 dev->data->port_id);
1612 for (i = 0; i != dev_stats_n; ++i) {
1613 const char *curr_string = (const char *)
1614 &strings->data[i * ETH_GSTRING_LEN];
1616 for (j = 0; j != xstats_n; ++j) {
1617 if (!strcmp(mlx5_counters_init[j].ctr_name,
1619 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1621 xstats_ctrl->dev_table_idx[idx] = i;
1622 xstats_ctrl->info[idx] = mlx5_counters_init[j];
1627 /* Add dev counters. */
1628 for (i = 0; i != xstats_n; ++i) {
1629 if (mlx5_counters_init[i].dev) {
1630 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1632 xstats_ctrl->info[idx] = mlx5_counters_init[i];
1633 xstats_ctrl->hw_stats[idx] = 0;
1636 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1637 xstats_ctrl->stats_n = dev_stats_n;
1638 /* Copy to base at first time. */
1639 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1641 DRV_LOG(ERR, "port %u cannot read device counters: %s",
1642 dev->data->port_id, strerror(rte_errno));
1643 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1644 stats_ctrl->imissed = 0;
1650 * Get MAC address by querying netdevice.
1653 * Pointer to Ethernet device.
1655 * MAC address output buffer.
1658 * 0 on success, a negative errno value otherwise and rte_errno is set.
1661 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1663 struct ifreq request;
1666 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1669 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1674 * Query dropless_rq private flag value provided by ETHTOOL.
1677 * Pointer to Ethernet device.
1680 * - 0 on success, flag is not set.
1681 * - 1 on success, flag is set.
1682 * - negative errno value otherwise and rte_errno is set.
1684 int mlx5_get_flag_dropless_rq(struct rte_eth_dev *dev)
1687 struct ethtool_sset_info hdr;
1690 struct ethtool_drvinfo drvinfo;
1692 struct ethtool_gstrings *strings = NULL;
1693 struct ethtool_value flags;
1694 const int32_t flag_len = sizeof(flags.data) * CHAR_BIT;
1700 sset_info.hdr.cmd = ETHTOOL_GSSET_INFO;
1701 sset_info.hdr.reserved = 0;
1702 sset_info.hdr.sset_mask = 1ULL << ETH_SS_PRIV_FLAGS;
1703 ifr.ifr_data = (caddr_t)&sset_info;
1704 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1706 const uint32_t *sset_lengths = sset_info.hdr.data;
1708 len = sset_info.hdr.sset_mask ? sset_lengths[0] : 0;
1709 } else if (ret == -EOPNOTSUPP) {
1710 drvinfo.cmd = ETHTOOL_GDRVINFO;
1711 ifr.ifr_data = (caddr_t)&drvinfo;
1712 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1714 DRV_LOG(WARNING, "port %u cannot get the driver info",
1715 dev->data->port_id);
1718 len = *(uint32_t *)((char *)&drvinfo +
1719 offsetof(struct ethtool_drvinfo, n_priv_flags));
1721 DRV_LOG(WARNING, "port %u cannot get the sset info",
1722 dev->data->port_id);
1726 DRV_LOG(WARNING, "port %u does not have private flag",
1727 dev->data->port_id);
1728 rte_errno = EOPNOTSUPP;
1731 } else if (len > flag_len) {
1732 DRV_LOG(WARNING, "port %u maximal private flags number is %d",
1733 dev->data->port_id, flag_len);
1736 str_sz = ETH_GSTRING_LEN * len;
1737 strings = (struct ethtool_gstrings *)
1738 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1741 DRV_LOG(WARNING, "port %u unable to allocate memory for"
1742 " private flags", dev->data->port_id);
1747 strings->cmd = ETHTOOL_GSTRINGS;
1748 strings->string_set = ETH_SS_PRIV_FLAGS;
1750 ifr.ifr_data = (caddr_t)strings;
1751 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1753 DRV_LOG(WARNING, "port %u unable to get private flags strings",
1754 dev->data->port_id);
1757 for (i = 0; i < len; i++) {
1758 strings->data[(i + 1) * ETH_GSTRING_LEN - 1] = 0;
1759 if (!strcmp((const char *)strings->data + i * ETH_GSTRING_LEN,
1764 DRV_LOG(WARNING, "port %u does not support dropless_rq",
1765 dev->data->port_id);
1766 rte_errno = EOPNOTSUPP;
1770 flags.cmd = ETHTOOL_GPFLAGS;
1771 ifr.ifr_data = (caddr_t)&flags;
1772 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1774 DRV_LOG(WARNING, "port %u unable to get private flags status",
1775 dev->data->port_id);
1778 ret = !!(flags.data & (1U << i));