xref: /dpdk/drivers/net/mlx5/linux/mlx5_ethdev_os.c (revision 5d52418fa4b9a7f28eaedc1d88ec5cf330381c0e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5 
6 #include <stddef.h>
7 #include <inttypes.h>
8 #include <unistd.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <dirent.h>
16 #include <net/if.h>
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>
22 #include <fcntl.h>
23 #include <stdalign.h>
24 #include <sys/un.h>
25 #include <time.h>
26 
27 #include <ethdev_driver.h>
28 #include <bus_pci_driver.h>
29 #include <rte_mbuf.h>
30 #include <rte_common.h>
31 #include <rte_eal_paging.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>
37 
38 #include <mlx5_glue.h>
39 #include <mlx5_devx_cmds.h>
40 #include <mlx5_common.h>
41 #include <mlx5_malloc.h>
42 #include <mlx5_nl.h>
43 
44 #include "mlx5.h"
45 #include "mlx5_rxtx.h"
46 #include "mlx5_utils.h"
47 
48 /* Supported speed values found in /usr/include/linux/ethtool.h */
49 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
50 #define SUPPORTED_40000baseKR4_Full (1 << 23)
51 #endif
52 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
53 #define SUPPORTED_40000baseCR4_Full (1 << 24)
54 #endif
55 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
56 #define SUPPORTED_40000baseSR4_Full (1 << 25)
57 #endif
58 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
59 #define SUPPORTED_40000baseLR4_Full (1 << 26)
60 #endif
61 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
62 #define SUPPORTED_56000baseKR4_Full (1 << 27)
63 #endif
64 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
65 #define SUPPORTED_56000baseCR4_Full (1 << 28)
66 #endif
67 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
68 #define SUPPORTED_56000baseSR4_Full (1 << 29)
69 #endif
70 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
71 #define SUPPORTED_56000baseLR4_Full (1 << 30)
72 #endif
73 
74 /* Add defines in case the running kernel is not the same as user headers. */
75 #ifndef ETHTOOL_GLINKSETTINGS
76 struct ethtool_link_settings {
77 	uint32_t cmd;
78 	uint32_t speed;
79 	uint8_t duplex;
80 	uint8_t port;
81 	uint8_t phy_address;
82 	uint8_t autoneg;
83 	uint8_t mdio_support;
84 	uint8_t eth_to_mdix;
85 	uint8_t eth_tp_mdix_ctrl;
86 	int8_t link_mode_masks_nwords;
87 	uint32_t reserved[8];
88 	uint32_t link_mode_masks[];
89 };
90 
91 /* The kernel values can be found in /include/uapi/linux/ethtool.h */
92 #define ETHTOOL_GLINKSETTINGS 0x0000004c
93 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
94 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
95 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
96 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
97 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
98 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
99 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
100 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
101 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
102 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
103 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
104 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
105 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
106 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
107 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
108 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
109 #endif
110 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
111 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
112 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
113 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
114 #endif
115 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
116 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
117 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
118 #endif
119 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
120 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
121 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
122 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
123 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
124 #endif
125 #ifndef HAVE_ETHTOOL_LINK_MODE_200G
126 #define ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT 62
127 #define ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT 63
128 #define ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT 0 /* 64 - 64 */
129 #define ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT 1 /* 65 - 64 */
130 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */
131 #endif
132 
133 /* Get interface index from SubFunction device name. */
134 int
135 mlx5_auxiliary_get_ifindex(const char *sf_name)
136 {
137 	char if_name[IF_NAMESIZE] = { 0 };
138 
139 	if (mlx5_auxiliary_get_child_name(sf_name, "/net",
140 					  if_name, sizeof(if_name)) != 0)
141 		return -rte_errno;
142 	return if_nametoindex(if_name);
143 }
144 
145 /**
146  * Get interface name from private structure.
147  *
148  * This is a port representor-aware version of mlx5_get_ifname_sysfs().
149  *
150  * @param[in] dev
151  *   Pointer to Ethernet device.
152  * @param[out] ifname
153  *   Interface name output buffer.
154  *
155  * @return
156  *   0 on success, a negative errno value otherwise and rte_errno is set.
157  */
158 int
159 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[MLX5_NAMESIZE])
160 {
161 	struct mlx5_priv *priv = dev->data->dev_private;
162 	unsigned int ifindex;
163 
164 	MLX5_ASSERT(priv);
165 	MLX5_ASSERT(priv->sh);
166 	if (priv->master && priv->sh->bond.ifindex > 0) {
167 		memcpy(ifname, priv->sh->bond.ifname, MLX5_NAMESIZE);
168 		return 0;
169 	}
170 	ifindex = mlx5_ifindex(dev);
171 	if (!ifindex) {
172 		if (!priv->representor)
173 			return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
174 						     *ifname);
175 		rte_errno = ENXIO;
176 		return -rte_errno;
177 	}
178 	if (if_indextoname(ifindex, &(*ifname)[0]))
179 		return 0;
180 	rte_errno = errno;
181 	return -rte_errno;
182 }
183 
184 /**
185  * Perform ifreq ioctl() on associated netdev ifname.
186  *
187  * @param[in] ifname
188  *   Pointer to netdev name.
189  * @param req
190  *   Request number to pass to ioctl().
191  * @param[out] ifr
192  *   Interface request structure output buffer.
193  *
194  * @return
195  *   0 on success, a negative errno value otherwise and rte_errno is set.
196  */
197 static int
198 mlx5_ifreq_by_ifname(const char *ifname, int req, struct ifreq *ifr)
199 {
200 	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
201 	int ret = 0;
202 
203 	if (sock == -1) {
204 		rte_errno = errno;
205 		return -rte_errno;
206 	}
207 	rte_strscpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
208 	ret = ioctl(sock, req, ifr);
209 	if (ret == -1) {
210 		rte_errno = errno;
211 		goto error;
212 	}
213 	close(sock);
214 	return 0;
215 error:
216 	close(sock);
217 	return -rte_errno;
218 }
219 
220 /**
221  * Perform ifreq ioctl() on associated Ethernet device.
222  *
223  * @param[in] dev
224  *   Pointer to Ethernet device.
225  * @param req
226  *   Request number to pass to ioctl().
227  * @param[out] ifr
228  *   Interface request structure output buffer.
229  *
230  * @return
231  *   0 on success, a negative errno value otherwise and rte_errno is set.
232  */
233 static int
234 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
235 {
236 	char ifname[sizeof(ifr->ifr_name)];
237 	int ret;
238 
239 	ret = mlx5_get_ifname(dev, &ifname);
240 	if (ret)
241 		return -rte_errno;
242 	return mlx5_ifreq_by_ifname(ifname, req, ifr);
243 }
244 
245 /**
246  * Get device MTU.
247  *
248  * @param dev
249  *   Pointer to Ethernet device.
250  * @param[out] mtu
251  *   MTU value output buffer.
252  *
253  * @return
254  *   0 on success, a negative errno value otherwise and rte_errno is set.
255  */
256 int
257 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
258 {
259 	struct ifreq request;
260 	int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
261 
262 	if (ret)
263 		return ret;
264 	*mtu = request.ifr_mtu;
265 	return 0;
266 }
267 
268 /**
269  * Set device MTU.
270  *
271  * @param dev
272  *   Pointer to Ethernet device.
273  * @param mtu
274  *   MTU value to set.
275  *
276  * @return
277  *   0 on success, a negative errno value otherwise and rte_errno is set.
278  */
279 int
280 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
281 {
282 	struct ifreq request = { .ifr_mtu = mtu, };
283 
284 	return mlx5_ifreq(dev, SIOCSIFMTU, &request);
285 }
286 
287 /**
288  * Set device flags.
289  *
290  * @param dev
291  *   Pointer to Ethernet device.
292  * @param keep
293  *   Bitmask for flags that must remain untouched.
294  * @param flags
295  *   Bitmask for flags to modify.
296  *
297  * @return
298  *   0 on success, a negative errno value otherwise and rte_errno is set.
299  */
300 static int
301 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
302 {
303 	struct ifreq request;
304 	int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
305 
306 	if (ret)
307 		return ret;
308 	request.ifr_flags &= keep;
309 	request.ifr_flags |= flags & ~keep;
310 	return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
311 }
312 
313 /**
314  * Get device current raw clock counter
315  *
316  * @param dev
317  *   Pointer to Ethernet device structure.
318  * @param[out] time
319  *   Current raw clock counter of the device.
320  *
321  * @return
322  *   0 if the clock has correctly been read
323  *   The value of errno in case of error
324  */
325 int
326 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
327 {
328 	struct mlx5_priv *priv = dev->data->dev_private;
329 	struct ibv_context *ctx = priv->sh->cdev->ctx;
330 	struct ibv_values_ex values;
331 	int err = 0;
332 
333 	values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
334 	err = mlx5_glue->query_rt_values_ex(ctx, &values);
335 	if (err != 0) {
336 		DRV_LOG(WARNING, "Could not query the clock !");
337 		return err;
338 	}
339 	*clock = values.raw_clock.tv_nsec;
340 	return 0;
341 }
342 
343 /**
344  * Retrieve the master device for representor in the same switch domain.
345  *
346  * @param dev
347  *   Pointer to representor Ethernet device structure.
348  *
349  * @return
350  *   Master device structure  on success, NULL otherwise.
351  */
352 static struct rte_eth_dev *
353 mlx5_find_master_dev(struct rte_eth_dev *dev)
354 {
355 	struct mlx5_priv *priv;
356 	uint16_t port_id;
357 	uint16_t domain_id;
358 
359 	priv = dev->data->dev_private;
360 	domain_id = priv->domain_id;
361 	MLX5_ASSERT(priv->representor);
362 	MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
363 		struct mlx5_priv *opriv =
364 			rte_eth_devices[port_id].data->dev_private;
365 		if (opriv &&
366 		    opriv->master &&
367 		    opriv->domain_id == domain_id &&
368 		    opriv->sh == priv->sh)
369 			return &rte_eth_devices[port_id];
370 	}
371 	return NULL;
372 }
373 
374 /**
375  * DPDK callback to retrieve physical link information.
376  *
377  * @param dev
378  *   Pointer to Ethernet device structure.
379  * @param[out] link
380  *   Storage for current link status.
381  *
382  * @return
383  *   0 on success, a negative errno value otherwise and rte_errno is set.
384  */
385 static int
386 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
387 			       struct rte_eth_link *link)
388 {
389 	struct mlx5_priv *priv = dev->data->dev_private;
390 	struct ethtool_cmd edata = {
391 		.cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
392 	};
393 	struct ifreq ifr;
394 	struct rte_eth_link dev_link;
395 	int link_speed = 0;
396 	int ret;
397 
398 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
399 	if (ret) {
400 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
401 			dev->data->port_id, strerror(rte_errno));
402 		return ret;
403 	}
404 	dev_link = (struct rte_eth_link) {
405 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
406 				(ifr.ifr_flags & IFF_RUNNING)),
407 	};
408 	ifr = (struct ifreq) {
409 		.ifr_data = (void *)&edata,
410 	};
411 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
412 	if (ret) {
413 		if (ret == -ENOTSUP && priv->representor) {
414 			struct rte_eth_dev *master;
415 
416 			/*
417 			 * For representors we can try to inherit link
418 			 * settings from the master device. Actually
419 			 * link settings do not make a lot of sense
420 			 * for representors due to missing physical
421 			 * link. The old kernel drivers supported
422 			 * emulated settings query for representors,
423 			 * the new ones do not, so we have to add
424 			 * this code for compatibility issues.
425 			 */
426 			master = mlx5_find_master_dev(dev);
427 			if (master) {
428 				ifr = (struct ifreq) {
429 					.ifr_data = (void *)&edata,
430 				};
431 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
432 			}
433 		}
434 		if (ret) {
435 			DRV_LOG(WARNING,
436 				"port %u ioctl(SIOCETHTOOL,"
437 				" ETHTOOL_GSET) failed: %s",
438 				dev->data->port_id, strerror(rte_errno));
439 			return ret;
440 		}
441 	}
442 	link_speed = ethtool_cmd_speed(&edata);
443 	if (link_speed == -1)
444 		dev_link.link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
445 	else
446 		dev_link.link_speed = link_speed;
447 	priv->link_speed_capa = 0;
448 	if (edata.supported & (SUPPORTED_1000baseT_Full |
449 			       SUPPORTED_1000baseKX_Full))
450 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G;
451 	if (edata.supported & SUPPORTED_10000baseKR_Full)
452 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G;
453 	if (edata.supported & (SUPPORTED_40000baseKR4_Full |
454 			       SUPPORTED_40000baseCR4_Full |
455 			       SUPPORTED_40000baseSR4_Full |
456 			       SUPPORTED_40000baseLR4_Full))
457 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G;
458 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
459 				RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX);
460 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
461 			RTE_ETH_LINK_SPEED_FIXED);
462 	*link = dev_link;
463 	return 0;
464 }
465 
466 /**
467  * Retrieve physical link information (unlocked version using new ioctl).
468  *
469  * @param dev
470  *   Pointer to Ethernet device structure.
471  * @param[out] link
472  *   Storage for current link status.
473  *
474  * @return
475  *   0 on success, a negative errno value otherwise and rte_errno is set.
476  */
477 static int
478 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
479 			     struct rte_eth_link *link)
480 
481 {
482 	struct mlx5_priv *priv = dev->data->dev_private;
483 	struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
484 	struct ifreq ifr;
485 	struct rte_eth_link dev_link;
486 	struct rte_eth_dev *master = NULL;
487 	uint64_t sc;
488 	int ret;
489 
490 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
491 	if (ret) {
492 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
493 			dev->data->port_id, strerror(rte_errno));
494 		return ret;
495 	}
496 	dev_link = (struct rte_eth_link) {
497 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
498 				(ifr.ifr_flags & IFF_RUNNING)),
499 	};
500 	ifr = (struct ifreq) {
501 		.ifr_data = (void *)&gcmd,
502 	};
503 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
504 	if (ret) {
505 		if (ret == -ENOTSUP && priv->representor) {
506 			/*
507 			 * For representors we can try to inherit link
508 			 * settings from the master device. Actually
509 			 * link settings do not make a lot of sense
510 			 * for representors due to missing physical
511 			 * link. The old kernel drivers supported
512 			 * emulated settings query for representors,
513 			 * the new ones do not, so we have to add
514 			 * this code for compatibility issues.
515 			 */
516 			master = mlx5_find_master_dev(dev);
517 			if (master) {
518 				ifr = (struct ifreq) {
519 					.ifr_data = (void *)&gcmd,
520 				};
521 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
522 			}
523 		}
524 		if (ret) {
525 			DRV_LOG(DEBUG,
526 				"port %u ioctl(SIOCETHTOOL,"
527 				" ETHTOOL_GLINKSETTINGS) failed: %s",
528 				dev->data->port_id, strerror(rte_errno));
529 			return ret;
530 		}
531 	}
532 	gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
533 
534 	alignas(struct ethtool_link_settings)
535 	uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
536 		     sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
537 	struct ethtool_link_settings *ecmd = (void *)data;
538 
539 	*ecmd = gcmd;
540 	ifr.ifr_data = (void *)ecmd;
541 	ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
542 	if (ret) {
543 		DRV_LOG(DEBUG,
544 			"port %u ioctl(SIOCETHTOOL,"
545 			"ETHTOOL_GLINKSETTINGS) failed: %s",
546 			dev->data->port_id, strerror(rte_errno));
547 		return ret;
548 	}
549 	dev_link.link_speed = (ecmd->speed == UINT32_MAX) ?
550 				RTE_ETH_SPEED_NUM_UNKNOWN : ecmd->speed;
551 	sc = ecmd->link_mode_masks[0] |
552 		((uint64_t)ecmd->link_mode_masks[1] << 32);
553 	priv->link_speed_capa = 0;
554 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
555 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
556 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G;
557 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
558 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
559 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
560 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G;
561 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
562 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
563 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_20G;
564 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
565 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
566 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
567 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
568 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G;
569 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
570 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
571 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
572 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
573 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_56G;
574 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
575 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
576 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
577 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_25G;
578 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
579 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
580 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_50G;
581 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
582 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
583 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
584 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
585 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_100G;
586 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
587 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
588 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G;
589 
590 	sc = ecmd->link_mode_masks[2] |
591 		((uint64_t)ecmd->link_mode_masks[3] << 32);
592 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
593 		  MLX5_BITSHIFT
594 		       (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
595 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
596 		priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G;
597 	dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
598 				RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX);
599 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
600 				  RTE_ETH_LINK_SPEED_FIXED);
601 	*link = dev_link;
602 	return 0;
603 }
604 
605 /**
606  * DPDK callback to retrieve physical link information.
607  *
608  * @param dev
609  *   Pointer to Ethernet device structure.
610  * @param wait_to_complete
611  *   Wait for request completion.
612  *
613  * @return
614  *   0 if link status was not updated, positive if it was, a negative errno
615  *   value otherwise and rte_errno is set.
616  */
617 int
618 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
619 {
620 	int ret;
621 	struct rte_eth_link dev_link;
622 	time_t start_time = time(NULL);
623 	int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
624 
625 	do {
626 		ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
627 		if (ret == -ENOTSUP)
628 			ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
629 		if (ret == 0)
630 			break;
631 		/* Handle wait to complete situation. */
632 		if ((wait_to_complete || retry) && ret == -EAGAIN) {
633 			if (abs((int)difftime(time(NULL), start_time)) <
634 			    MLX5_LINK_STATUS_TIMEOUT) {
635 				usleep(0);
636 				continue;
637 			} else {
638 				rte_errno = EBUSY;
639 				return -rte_errno;
640 			}
641 		} else if (ret < 0) {
642 			return ret;
643 		}
644 	} while (wait_to_complete || retry-- > 0);
645 	ret = !!memcmp(&dev->data->dev_link, &dev_link,
646 		       sizeof(struct rte_eth_link));
647 	dev->data->dev_link = dev_link;
648 	return ret;
649 }
650 
651 /**
652  * DPDK callback to get flow control status.
653  *
654  * @param dev
655  *   Pointer to Ethernet device structure.
656  * @param[out] fc_conf
657  *   Flow control output buffer.
658  *
659  * @return
660  *   0 on success, a negative errno value otherwise and rte_errno is set.
661  */
662 int
663 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
664 {
665 	struct ifreq ifr;
666 	struct ethtool_pauseparam ethpause = {
667 		.cmd = ETHTOOL_GPAUSEPARAM
668 	};
669 	int ret;
670 
671 	ifr.ifr_data = (void *)&ethpause;
672 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
673 	if (ret) {
674 		DRV_LOG(WARNING,
675 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
676 			" %s",
677 			dev->data->port_id, strerror(rte_errno));
678 		return ret;
679 	}
680 	fc_conf->autoneg = ethpause.autoneg;
681 	if (ethpause.rx_pause && ethpause.tx_pause)
682 		fc_conf->mode = RTE_ETH_FC_FULL;
683 	else if (ethpause.rx_pause)
684 		fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
685 	else if (ethpause.tx_pause)
686 		fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
687 	else
688 		fc_conf->mode = RTE_ETH_FC_NONE;
689 	return 0;
690 }
691 
692 /**
693  * DPDK callback to modify flow control parameters.
694  *
695  * @param dev
696  *   Pointer to Ethernet device structure.
697  * @param[in] fc_conf
698  *   Flow control parameters.
699  *
700  * @return
701  *   0 on success, a negative errno value otherwise and rte_errno is set.
702  */
703 int
704 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
705 {
706 	struct ifreq ifr;
707 	struct ethtool_pauseparam ethpause = {
708 		.cmd = ETHTOOL_SPAUSEPARAM
709 	};
710 	int ret;
711 
712 	ifr.ifr_data = (void *)&ethpause;
713 	ethpause.autoneg = fc_conf->autoneg;
714 	if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) ||
715 	    (fc_conf->mode & RTE_ETH_FC_RX_PAUSE))
716 		ethpause.rx_pause = 1;
717 	else
718 		ethpause.rx_pause = 0;
719 
720 	if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) ||
721 	    (fc_conf->mode & RTE_ETH_FC_TX_PAUSE))
722 		ethpause.tx_pause = 1;
723 	else
724 		ethpause.tx_pause = 0;
725 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
726 	if (ret) {
727 		DRV_LOG(WARNING,
728 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
729 			" failed: %s",
730 			dev->data->port_id, strerror(rte_errno));
731 		return ret;
732 	}
733 	return 0;
734 }
735 
736 /**
737  * Handle asynchronous removal event for entire multiport device.
738  *
739  * @param sh
740  *   Infiniband device shared context.
741  */
742 static void
743 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
744 {
745 	uint32_t i;
746 
747 	for (i = 0; i < sh->max_port; ++i) {
748 		struct rte_eth_dev *dev;
749 		struct mlx5_priv *priv;
750 
751 		if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
752 			/*
753 			 * Or not existing port either no
754 			 * handler installed for this port.
755 			 */
756 			continue;
757 		}
758 		dev = &rte_eth_devices[sh->port[i].ih_port_id];
759 		MLX5_ASSERT(dev);
760 		priv = dev->data->dev_private;
761 		MLX5_ASSERT(priv);
762 		if (!priv->rmv_notified && dev->data->dev_conf.intr_conf.rmv) {
763 			/* Notify driver about removal only once. */
764 			priv->rmv_notified = 1;
765 			rte_eth_dev_callback_process
766 				(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
767 		}
768 	}
769 }
770 
771 static void
772 mlx5_dev_interrupt_nl_cb(struct nlmsghdr *hdr, void *cb_arg)
773 {
774 	struct mlx5_dev_ctx_shared *sh = cb_arg;
775 	uint32_t i;
776 	uint32_t if_index;
777 
778 	if (mlx5_nl_parse_link_status_update(hdr, &if_index) < 0)
779 		return;
780 	for (i = 0; i < sh->max_port; i++) {
781 		struct mlx5_dev_shared_port *port = &sh->port[i];
782 		struct rte_eth_dev *dev;
783 		struct mlx5_priv *priv;
784 
785 		if (port->nl_ih_port_id >= RTE_MAX_ETHPORTS)
786 			continue;
787 		dev = &rte_eth_devices[port->nl_ih_port_id];
788 		/* Probing may initiate an LSC before configuration is done. */
789 		if (dev->data->dev_configured &&
790 		    !dev->data->dev_conf.intr_conf.lsc)
791 			break;
792 		priv = dev->data->dev_private;
793 		if (priv->if_index == if_index) {
794 			/* Block logical LSC events. */
795 			uint16_t prev_status = dev->data->dev_link.link_status;
796 
797 			if (mlx5_link_update(dev, 0) < 0)
798 				DRV_LOG(ERR, "Failed to update link status: %s",
799 					rte_strerror(rte_errno));
800 			else if (prev_status != dev->data->dev_link.link_status)
801 				rte_eth_dev_callback_process
802 					(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
803 			break;
804 		}
805 	}
806 }
807 
808 void
809 mlx5_dev_interrupt_handler_nl(void *arg)
810 {
811 	struct mlx5_dev_ctx_shared *sh = arg;
812 	int nlsk_fd = rte_intr_fd_get(sh->intr_handle_nl);
813 
814 	if (nlsk_fd < 0)
815 		return;
816 	if (mlx5_nl_read_events(nlsk_fd, mlx5_dev_interrupt_nl_cb, sh) < 0)
817 		DRV_LOG(ERR, "Failed to process Netlink events: %s",
818 			rte_strerror(rte_errno));
819 }
820 
821 /**
822  * Handle shared asynchronous events the NIC (removal event
823  * and link status change). Supports multiport IB device.
824  *
825  * @param cb_arg
826  *   Callback argument.
827  */
828 void
829 mlx5_dev_interrupt_handler(void *cb_arg)
830 {
831 	struct mlx5_dev_ctx_shared *sh = cb_arg;
832 	struct ibv_async_event event;
833 
834 	/* Read all message from the IB device and acknowledge them. */
835 	for (;;) {
836 		struct rte_eth_dev *dev;
837 		uint32_t tmp;
838 
839 		if (mlx5_glue->get_async_event(sh->cdev->ctx, &event)) {
840 			if (errno == EIO) {
841 				DRV_LOG(DEBUG,
842 					"IBV async event queue closed on: %s",
843 					sh->ibdev_name);
844 				mlx5_dev_interrupt_device_fatal(sh);
845 			}
846 			break;
847 		}
848 		if (event.event_type == IBV_EVENT_DEVICE_FATAL) {
849 			/*
850 			 * The DEVICE_FATAL event can be called by kernel
851 			 * twice - from mlx5 and uverbs layers, and port
852 			 * index is not applicable. We should notify all
853 			 * existing ports.
854 			 */
855 			mlx5_dev_interrupt_device_fatal(sh);
856 			mlx5_glue->ack_async_event(&event);
857 			continue;
858 		}
859 		/* Retrieve and check IB port index. */
860 		tmp = (uint32_t)event.element.port_num;
861 		MLX5_ASSERT(tmp <= sh->max_port);
862 		if (!tmp) {
863 			/* Unsupported device level event. */
864 			mlx5_glue->ack_async_event(&event);
865 			DRV_LOG(DEBUG,
866 				"unsupported common event (type %d)",
867 				event.event_type);
868 			continue;
869 		}
870 		if (tmp > sh->max_port) {
871 			/* Invalid IB port index. */
872 			mlx5_glue->ack_async_event(&event);
873 			DRV_LOG(DEBUG,
874 				"cannot handle an event (type %d)"
875 				"due to invalid IB port index (%u)",
876 				event.event_type, tmp);
877 			continue;
878 		}
879 		if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
880 			/* No handler installed. */
881 			mlx5_glue->ack_async_event(&event);
882 			DRV_LOG(DEBUG,
883 				"cannot handle an event (type %d)"
884 				"due to no handler installed for port %u",
885 				event.event_type, tmp);
886 			continue;
887 		}
888 		/* Retrieve ethernet device descriptor. */
889 		tmp = sh->port[tmp - 1].ih_port_id;
890 		dev = &rte_eth_devices[tmp];
891 		MLX5_ASSERT(dev);
892 		DRV_LOG(DEBUG,
893 			"port %u cannot handle an unknown event (type %d)",
894 			dev->data->port_id, event.event_type);
895 		mlx5_glue->ack_async_event(&event);
896 	}
897 }
898 
899 /**
900  * Handle DEVX interrupts from the NIC.
901  * This function is probably called from the DPDK host thread.
902  *
903  * @param cb_arg
904  *   Callback argument.
905  */
906 void
907 mlx5_dev_interrupt_handler_devx(void *cb_arg)
908 {
909 #ifndef HAVE_IBV_DEVX_ASYNC
910 	(void)cb_arg;
911 	return;
912 #else
913 	struct mlx5_dev_ctx_shared *sh = cb_arg;
914 	union {
915 		struct mlx5dv_devx_async_cmd_hdr cmd_resp;
916 		uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
917 			    MLX5_ST_SZ_BYTES(traffic_counter) +
918 			    sizeof(struct mlx5dv_devx_async_cmd_hdr)];
919 	} out;
920 	uint8_t *buf = out.buf + sizeof(out.cmd_resp);
921 
922 	while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
923 						   &out.cmd_resp,
924 						   sizeof(out.buf)))
925 		mlx5_flow_async_pool_query_handle
926 			(sh, (uint64_t)out.cmd_resp.wr_id,
927 			 mlx5_devx_get_out_command_status(buf));
928 #endif /* HAVE_IBV_DEVX_ASYNC */
929 }
930 
931 /**
932  * DPDK callback to bring the link DOWN.
933  *
934  * @param dev
935  *   Pointer to Ethernet device structure.
936  *
937  * @return
938  *   0 on success, a negative errno value otherwise and rte_errno is set.
939  */
940 int
941 mlx5_set_link_down(struct rte_eth_dev *dev)
942 {
943 	return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
944 }
945 
946 /**
947  * DPDK callback to bring the link UP.
948  *
949  * @param dev
950  *   Pointer to Ethernet device structure.
951  *
952  * @return
953  *   0 on success, a negative errno value otherwise and rte_errno is set.
954  */
955 int
956 mlx5_set_link_up(struct rte_eth_dev *dev)
957 {
958 	return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
959 }
960 
961 /**
962  * Check if mlx5 device was removed.
963  *
964  * @param dev
965  *   Pointer to Ethernet device structure.
966  *
967  * @return
968  *   1 when device is removed, otherwise 0.
969  */
970 int
971 mlx5_is_removed(struct rte_eth_dev *dev)
972 {
973 	struct ibv_device_attr device_attr;
974 	struct mlx5_priv *priv = dev->data->dev_private;
975 
976 	if (mlx5_glue->query_device(priv->sh->cdev->ctx, &device_attr) == EIO)
977 		return 1;
978 	return 0;
979 }
980 
981 /**
982  * Analyze gathered port parameters via sysfs to recognize master
983  * and representor devices for E-Switch configuration.
984  *
985  * @param[in] device_dir
986  *   flag of presence of "device" directory under port device key.
987  * @param[inout] switch_info
988  *   Port information, including port name as a number and port name
989  *   type if recognized
990  *
991  * @return
992  *   master and representor flags are set in switch_info according to
993  *   recognized parameters (if any).
994  */
995 static void
996 mlx5_sysfs_check_switch_info(bool device_dir,
997 			     struct mlx5_switch_info *switch_info)
998 {
999 	switch (switch_info->name_type) {
1000 	case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1001 		/*
1002 		 * Name is not recognized, assume the master,
1003 		 * check the device directory presence.
1004 		 */
1005 		switch_info->master = device_dir;
1006 		break;
1007 	case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1008 		/*
1009 		 * Name is not set, this assumes the legacy naming
1010 		 * schema for master, just check if there is
1011 		 * a device directory.
1012 		 */
1013 		switch_info->master = device_dir;
1014 		break;
1015 	case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1016 		/* New uplink naming schema recognized. */
1017 		switch_info->master = 1;
1018 		break;
1019 	case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1020 		/* Legacy representors naming schema. */
1021 		switch_info->representor = !device_dir;
1022 		break;
1023 	case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1024 		/* Fallthrough */
1025 	case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1026 		/* Fallthrough */
1027 	case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
1028 		/* New representors naming schema. */
1029 		switch_info->representor = 1;
1030 		break;
1031 	default:
1032 		switch_info->master = device_dir;
1033 		break;
1034 	}
1035 }
1036 
1037 /**
1038  * Get switch information associated with network interface.
1039  *
1040  * @param ifindex
1041  *   Network interface index.
1042  * @param[out] info
1043  *   Switch information object, populated in case of success.
1044  *
1045  * @return
1046  *   0 on success, a negative errno value otherwise and rte_errno is set.
1047  */
1048 int
1049 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1050 {
1051 	char ifname[IF_NAMESIZE];
1052 	char *port_name = NULL;
1053 	size_t port_name_size = 0;
1054 	FILE *file;
1055 	struct mlx5_switch_info data = {
1056 		.master = 0,
1057 		.representor = 0,
1058 		.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1059 		.port_name = 0,
1060 		.switch_id = 0,
1061 	};
1062 	DIR *dir;
1063 	bool port_switch_id_set = false;
1064 	bool device_dir = false;
1065 	char c;
1066 	ssize_t line_size;
1067 
1068 	if (!if_indextoname(ifindex, ifname)) {
1069 		rte_errno = errno;
1070 		return -rte_errno;
1071 	}
1072 
1073 	MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1074 	      ifname);
1075 	MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1076 	      ifname);
1077 	MKSTR(pci_device, "/sys/class/net/%s/device",
1078 	      ifname);
1079 
1080 	file = fopen(phys_port_name, "rb");
1081 	if (file != NULL) {
1082 		char *tail_nl;
1083 
1084 		line_size = getline(&port_name, &port_name_size, file);
1085 		if (line_size < 0) {
1086 			fclose(file);
1087 			rte_errno = errno;
1088 			return -rte_errno;
1089 		} else if (line_size > 0) {
1090 			/* Remove tailing newline character. */
1091 			tail_nl = strchr(port_name, '\n');
1092 			if (tail_nl)
1093 				*tail_nl = '\0';
1094 			mlx5_translate_port_name(port_name, &data);
1095 		}
1096 		free(port_name);
1097 		fclose(file);
1098 	}
1099 	file = fopen(phys_switch_id, "rb");
1100 	if (file == NULL) {
1101 		rte_errno = errno;
1102 		return -rte_errno;
1103 	}
1104 	port_switch_id_set =
1105 		fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1106 		c == '\n';
1107 	fclose(file);
1108 	dir = opendir(pci_device);
1109 	if (dir != NULL) {
1110 		closedir(dir);
1111 		device_dir = true;
1112 	}
1113 	if (port_switch_id_set) {
1114 		/* We have some E-Switch configuration. */
1115 		mlx5_sysfs_check_switch_info(device_dir, &data);
1116 	}
1117 	*info = data;
1118 	MLX5_ASSERT(!(data.master && data.representor));
1119 	if (data.master && data.representor) {
1120 		DRV_LOG(ERR, "ifindex %u device is recognized as master"
1121 			     " and as representor", ifindex);
1122 		rte_errno = ENODEV;
1123 		return -rte_errno;
1124 	}
1125 	return 0;
1126 }
1127 
1128 /**
1129  * Get bond information associated with network interface.
1130  *
1131  * @param pf_ifindex
1132  *   Network interface index of bond slave interface
1133  * @param[out] ifindex
1134  *   Pointer to bond ifindex.
1135  * @param[out] ifname
1136  *   Pointer to bond ifname.
1137  *
1138  * @return
1139  *   0 on success, a negative errno value otherwise and rte_errno is set.
1140  */
1141 int
1142 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex,
1143 		     char *ifname)
1144 {
1145 	char name[IF_NAMESIZE];
1146 	FILE *file;
1147 	unsigned int index;
1148 	int ret;
1149 
1150 	if (!if_indextoname(pf_ifindex, name) || !strlen(name)) {
1151 		rte_errno = errno;
1152 		return -rte_errno;
1153 	}
1154 	MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name);
1155 	/* read bond ifindex */
1156 	file = fopen(bond_if, "rb");
1157 	if (file == NULL) {
1158 		rte_errno = errno;
1159 		return -rte_errno;
1160 	}
1161 	ret = fscanf(file, "%u", &index);
1162 	fclose(file);
1163 	if (ret <= 0) {
1164 		rte_errno = errno;
1165 		return -rte_errno;
1166 	}
1167 	if (ifindex)
1168 		*ifindex = index;
1169 
1170 	/* read bond device name from symbol link */
1171 	if (ifname) {
1172 		if (!if_indextoname(index, ifname)) {
1173 			rte_errno = errno;
1174 			return -rte_errno;
1175 		}
1176 	}
1177 	return 0;
1178 }
1179 
1180 /**
1181  * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1182  *
1183  * @param dev
1184  *   Pointer to Ethernet device structure.
1185  * @param[out] modinfo
1186  *   Storage for plug-in module EEPROM information.
1187  *
1188  * @return
1189  *   0 on success, a negative errno value otherwise and rte_errno is set.
1190  */
1191 int
1192 mlx5_get_module_info(struct rte_eth_dev *dev,
1193 		     struct rte_eth_dev_module_info *modinfo)
1194 {
1195 	struct ethtool_modinfo info = {
1196 		.cmd = ETHTOOL_GMODULEINFO,
1197 	};
1198 	struct ifreq ifr = (struct ifreq) {
1199 		.ifr_data = (void *)&info,
1200 	};
1201 	int ret = 0;
1202 
1203 	if (!dev) {
1204 		DRV_LOG(WARNING, "missing argument, cannot get module info");
1205 		rte_errno = EINVAL;
1206 		return -rte_errno;
1207 	}
1208 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1209 	if (ret) {
1210 		DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1211 			dev->data->port_id, strerror(rte_errno));
1212 		return ret;
1213 	}
1214 	modinfo->type = info.type;
1215 	modinfo->eeprom_len = info.eeprom_len;
1216 	return ret;
1217 }
1218 
1219 /**
1220  * DPDK callback to retrieve plug-in module EEPROM data.
1221  *
1222  * @param dev
1223  *   Pointer to Ethernet device structure.
1224  * @param[out] info
1225  *   Storage for plug-in module EEPROM data.
1226  *
1227  * @return
1228  *   0 on success, a negative errno value otherwise and rte_errno is set.
1229  */
1230 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1231 			   struct rte_dev_eeprom_info *info)
1232 {
1233 	struct ethtool_eeprom *eeprom;
1234 	struct ifreq ifr;
1235 	int ret = 0;
1236 
1237 	if (!dev) {
1238 		DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1239 		rte_errno = EINVAL;
1240 		return -rte_errno;
1241 	}
1242 	eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1243 			     (sizeof(struct ethtool_eeprom) + info->length), 0,
1244 			     SOCKET_ID_ANY);
1245 	if (!eeprom) {
1246 		DRV_LOG(WARNING, "port %u cannot allocate memory for "
1247 			"eeprom data", dev->data->port_id);
1248 		rte_errno = ENOMEM;
1249 		return -rte_errno;
1250 	}
1251 	eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1252 	eeprom->offset = info->offset;
1253 	eeprom->len = info->length;
1254 	ifr = (struct ifreq) {
1255 		.ifr_data = (void *)eeprom,
1256 	};
1257 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1258 	if (ret)
1259 		DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1260 			dev->data->port_id, strerror(rte_errno));
1261 	else
1262 		rte_memcpy(info->data, eeprom->data, info->length);
1263 	mlx5_free(eeprom);
1264 	return ret;
1265 }
1266 
1267 /**
1268  * Read device counters table.
1269  *
1270  * @param dev
1271  *   Pointer to Ethernet device.
1272  * @param[in] pf
1273  *   PF index in case of bonding device, -1 otherwise
1274  * @param[out] stats
1275  *   Counters table output buffer.
1276  *
1277  * @return
1278  *   0 on success and stats is filled, negative errno value otherwise and
1279  *   rte_errno is set.
1280  */
1281 static int
1282 _mlx5_os_read_dev_counters(struct rte_eth_dev *dev, int pf, uint64_t *stats)
1283 {
1284 	struct mlx5_priv *priv = dev->data->dev_private;
1285 	struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1286 	unsigned int i;
1287 	struct ifreq ifr;
1288 	unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1289 	unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1290 	struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1291 	int ret;
1292 
1293 	et_stats->cmd = ETHTOOL_GSTATS;
1294 	et_stats->n_stats = xstats_ctrl->stats_n;
1295 	ifr.ifr_data = (caddr_t)et_stats;
1296 	if (pf >= 0)
1297 		ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[pf].ifname,
1298 					   SIOCETHTOOL, &ifr);
1299 	else
1300 		ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1301 	if (ret) {
1302 		DRV_LOG(WARNING,
1303 			"port %u unable to read statistic values from device",
1304 			dev->data->port_id);
1305 		return ret;
1306 	}
1307 	for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1308 		if (xstats_ctrl->info[i].dev)
1309 			continue;
1310 		stats[i] += (uint64_t)
1311 			    et_stats->data[xstats_ctrl->dev_table_idx[i]];
1312 	}
1313 	return 0;
1314 }
1315 
1316 /**
1317  * Read device counters.
1318  *
1319  * @param dev
1320  *   Pointer to Ethernet device.
1321  * @param[out] stats
1322  *   Counters table output buffer.
1323  *
1324  * @return
1325  *   0 on success and stats is filled, negative errno value otherwise and
1326  *   rte_errno is set.
1327  */
1328 int
1329 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1330 {
1331 	struct mlx5_priv *priv = dev->data->dev_private;
1332 	struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1333 	int ret = 0, i;
1334 
1335 	memset(stats, 0, sizeof(*stats) * xstats_ctrl->mlx5_stats_n);
1336 	/* Read ifreq counters. */
1337 	if (priv->master && priv->pf_bond >= 0) {
1338 		/* Sum xstats from bonding device member ports. */
1339 		for (i = 0; i < priv->sh->bond.n_port; i++) {
1340 			ret = _mlx5_os_read_dev_counters(dev, i, stats);
1341 			if (ret)
1342 				return ret;
1343 		}
1344 	} else {
1345 		ret = _mlx5_os_read_dev_counters(dev, -1, stats);
1346 		if (ret)
1347 			return ret;
1348 	}
1349 	/* Read IB counters. */
1350 	for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1351 		if (!xstats_ctrl->info[i].dev)
1352 			continue;
1353 		/* return last xstats counter if fail to read. */
1354 		if (mlx5_os_read_dev_stat(priv, xstats_ctrl->info[i].ctr_name,
1355 			    &stats[i]) == 0)
1356 			xstats_ctrl->xstats[i] = stats[i];
1357 		else
1358 			stats[i] = xstats_ctrl->xstats[i];
1359 	}
1360 	return ret;
1361 }
1362 
1363 /**
1364  * Query the number of statistics provided by ETHTOOL.
1365  *
1366  * @param dev
1367  *   Pointer to Ethernet device.
1368  *
1369  * @return
1370  *   Number of statistics on success, negative errno value otherwise and
1371  *   rte_errno is set.
1372  */
1373 int
1374 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1375 {
1376 	struct mlx5_priv *priv = dev->data->dev_private;
1377 	struct ethtool_drvinfo drvinfo;
1378 	struct ifreq ifr;
1379 	int ret;
1380 
1381 	drvinfo.cmd = ETHTOOL_GDRVINFO;
1382 	ifr.ifr_data = (caddr_t)&drvinfo;
1383 	if (priv->master && priv->pf_bond >= 0)
1384 		/* Bonding PF. */
1385 		ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1386 					   SIOCETHTOOL, &ifr);
1387 	else
1388 		ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1389 	if (ret) {
1390 		DRV_LOG(WARNING, "port %u unable to query number of statistics",
1391 			dev->data->port_id);
1392 		return ret;
1393 	}
1394 	return drvinfo.n_stats;
1395 }
1396 
1397 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1398 	{
1399 		.dpdk_name = "rx_unicast_bytes",
1400 		.ctr_name = "rx_vport_unicast_bytes",
1401 	},
1402 	{
1403 		.dpdk_name = "rx_multicast_bytes",
1404 		.ctr_name = "rx_vport_multicast_bytes",
1405 	},
1406 	{
1407 		.dpdk_name = "rx_broadcast_bytes",
1408 		.ctr_name = "rx_vport_broadcast_bytes",
1409 	},
1410 	{
1411 		.dpdk_name = "rx_unicast_packets",
1412 		.ctr_name = "rx_vport_unicast_packets",
1413 	},
1414 	{
1415 		.dpdk_name = "rx_multicast_packets",
1416 		.ctr_name = "rx_vport_multicast_packets",
1417 	},
1418 	{
1419 		.dpdk_name = "rx_broadcast_packets",
1420 		.ctr_name = "rx_vport_broadcast_packets",
1421 	},
1422 	{
1423 		.dpdk_name = "tx_unicast_bytes",
1424 		.ctr_name = "tx_vport_unicast_bytes",
1425 	},
1426 	{
1427 		.dpdk_name = "tx_multicast_bytes",
1428 		.ctr_name = "tx_vport_multicast_bytes",
1429 	},
1430 	{
1431 		.dpdk_name = "tx_broadcast_bytes",
1432 		.ctr_name = "tx_vport_broadcast_bytes",
1433 	},
1434 	{
1435 		.dpdk_name = "tx_unicast_packets",
1436 		.ctr_name = "tx_vport_unicast_packets",
1437 	},
1438 	{
1439 		.dpdk_name = "tx_multicast_packets",
1440 		.ctr_name = "tx_vport_multicast_packets",
1441 	},
1442 	{
1443 		.dpdk_name = "tx_broadcast_packets",
1444 		.ctr_name = "tx_vport_broadcast_packets",
1445 	},
1446 	{
1447 		.dpdk_name = "rx_wqe_errors",
1448 		.ctr_name = "rx_wqe_err",
1449 	},
1450 	{
1451 		.dpdk_name = "rx_phy_crc_errors",
1452 		.ctr_name = "rx_crc_errors_phy",
1453 	},
1454 	{
1455 		.dpdk_name = "rx_phy_in_range_len_errors",
1456 		.ctr_name = "rx_in_range_len_errors_phy",
1457 	},
1458 	{
1459 		.dpdk_name = "rx_phy_symbol_errors",
1460 		.ctr_name = "rx_symbol_err_phy",
1461 	},
1462 	{
1463 		.dpdk_name = "tx_phy_errors",
1464 		.ctr_name = "tx_errors_phy",
1465 	},
1466 	{
1467 		.dpdk_name = "rx_out_of_buffer",
1468 		.ctr_name = "out_of_buffer",
1469 		.dev = 1,
1470 	},
1471 	{
1472 		.dpdk_name = "tx_phy_packets",
1473 		.ctr_name = "tx_packets_phy",
1474 	},
1475 	{
1476 		.dpdk_name = "rx_phy_packets",
1477 		.ctr_name = "rx_packets_phy",
1478 	},
1479 	{
1480 		.dpdk_name = "tx_phy_discard_packets",
1481 		.ctr_name = "tx_discards_phy",
1482 	},
1483 	{
1484 		.dpdk_name = "rx_phy_discard_packets",
1485 		.ctr_name = "rx_discards_phy",
1486 	},
1487 	{
1488 		.dpdk_name = "rx_prio0_buf_discard_packets",
1489 		.ctr_name = "rx_prio0_buf_discard",
1490 	},
1491 	{
1492 		.dpdk_name = "rx_prio1_buf_discard_packets",
1493 		.ctr_name = "rx_prio1_buf_discard",
1494 	},
1495 	{
1496 		.dpdk_name = "rx_prio2_buf_discard_packets",
1497 		.ctr_name = "rx_prio2_buf_discard",
1498 	},
1499 	{
1500 		.dpdk_name = "rx_prio3_buf_discard_packets",
1501 		.ctr_name = "rx_prio3_buf_discard",
1502 	},
1503 	{
1504 		.dpdk_name = "rx_prio4_buf_discard_packets",
1505 		.ctr_name = "rx_prio4_buf_discard",
1506 	},
1507 	{
1508 		.dpdk_name = "rx_prio5_buf_discard_packets",
1509 		.ctr_name = "rx_prio5_buf_discard",
1510 	},
1511 	{
1512 		.dpdk_name = "rx_prio6_buf_discard_packets",
1513 		.ctr_name = "rx_prio6_buf_discard",
1514 	},
1515 	{
1516 		.dpdk_name = "rx_prio7_buf_discard_packets",
1517 		.ctr_name = "rx_prio7_buf_discard",
1518 	},
1519 	{
1520 		.dpdk_name = "rx_prio0_cong_discard_packets",
1521 		.ctr_name = "rx_prio0_cong_discard",
1522 	},
1523 	{
1524 		.dpdk_name = "rx_prio1_cong_discard_packets",
1525 		.ctr_name = "rx_prio1_cong_discard",
1526 	},
1527 	{
1528 		.dpdk_name = "rx_prio2_cong_discard_packets",
1529 		.ctr_name = "rx_prio2_cong_discard",
1530 	},
1531 	{
1532 		.dpdk_name = "rx_prio3_cong_discard_packets",
1533 		.ctr_name = "rx_prio3_cong_discard",
1534 	},
1535 	{
1536 		.dpdk_name = "rx_prio4_cong_discard_packets",
1537 		.ctr_name = "rx_prio4_cong_discard",
1538 	},
1539 	{
1540 		.dpdk_name = "rx_prio5_cong_discard_packets",
1541 		.ctr_name = "rx_prio5_cong_discard",
1542 	},
1543 	{
1544 		.dpdk_name = "rx_prio6_cong_discard_packets",
1545 		.ctr_name = "rx_prio6_cong_discard",
1546 	},
1547 	{
1548 		.dpdk_name = "rx_prio7_cong_discard_packets",
1549 		.ctr_name = "rx_prio7_cong_discard",
1550 	},
1551 	{
1552 		.dpdk_name = "tx_phy_bytes",
1553 		.ctr_name = "tx_bytes_phy",
1554 	},
1555 	{
1556 		.dpdk_name = "rx_phy_bytes",
1557 		.ctr_name = "rx_bytes_phy",
1558 	},
1559 	/* Representor only */
1560 	{
1561 		.dpdk_name = "rx_vport_packets",
1562 		.ctr_name = "vport_rx_packets",
1563 	},
1564 	{
1565 		.dpdk_name = "rx_vport_bytes",
1566 		.ctr_name = "vport_rx_bytes",
1567 	},
1568 	{
1569 		.dpdk_name = "tx_vport_packets",
1570 		.ctr_name = "vport_tx_packets",
1571 	},
1572 	{
1573 		.dpdk_name = "tx_vport_bytes",
1574 		.ctr_name = "vport_tx_bytes",
1575 	},
1576 };
1577 
1578 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1579 
1580 /**
1581  * Init the structures to read device counters.
1582  *
1583  * @param dev
1584  *   Pointer to Ethernet device.
1585  */
1586 void
1587 mlx5_os_stats_init(struct rte_eth_dev *dev)
1588 {
1589 	struct mlx5_priv *priv = dev->data->dev_private;
1590 	struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1591 	struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1592 	unsigned int i;
1593 	unsigned int j;
1594 	struct ifreq ifr;
1595 	struct ethtool_gstrings *strings = NULL;
1596 	unsigned int dev_stats_n;
1597 	unsigned int str_sz;
1598 	int ret;
1599 
1600 	/* So that it won't aggregate for each init. */
1601 	xstats_ctrl->mlx5_stats_n = 0;
1602 	ret = mlx5_os_get_stats_n(dev);
1603 	if (ret < 0) {
1604 		DRV_LOG(WARNING, "port %u no extended statistics available",
1605 			dev->data->port_id);
1606 		return;
1607 	}
1608 	dev_stats_n = ret;
1609 	/* Allocate memory to grab stat names and values. */
1610 	str_sz = dev_stats_n * ETH_GSTRING_LEN;
1611 	strings = (struct ethtool_gstrings *)
1612 		  mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1613 			      SOCKET_ID_ANY);
1614 	if (!strings) {
1615 		DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1616 		     dev->data->port_id);
1617 		return;
1618 	}
1619 	strings->cmd = ETHTOOL_GSTRINGS;
1620 	strings->string_set = ETH_SS_STATS;
1621 	strings->len = dev_stats_n;
1622 	ifr.ifr_data = (caddr_t)strings;
1623 	if (priv->master && priv->pf_bond >= 0)
1624 		/* Bonding master. */
1625 		ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname,
1626 					   SIOCETHTOOL, &ifr);
1627 	else
1628 		ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1629 	if (ret) {
1630 		DRV_LOG(WARNING, "port %u unable to get statistic names",
1631 			dev->data->port_id);
1632 		goto free;
1633 	}
1634 	for (i = 0; i != dev_stats_n; ++i) {
1635 		const char *curr_string = (const char *)
1636 			&strings->data[i * ETH_GSTRING_LEN];
1637 
1638 		for (j = 0; j != xstats_n; ++j) {
1639 			if (!strcmp(mlx5_counters_init[j].ctr_name,
1640 				    curr_string)) {
1641 				unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1642 
1643 				xstats_ctrl->dev_table_idx[idx] = i;
1644 				xstats_ctrl->info[idx] = mlx5_counters_init[j];
1645 				break;
1646 			}
1647 		}
1648 	}
1649 	/* Add dev counters. */
1650 	MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1651 	for (i = 0; i != xstats_n; ++i) {
1652 		if (mlx5_counters_init[i].dev) {
1653 			unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1654 
1655 			xstats_ctrl->info[idx] = mlx5_counters_init[i];
1656 			xstats_ctrl->hw_stats[idx] = 0;
1657 		}
1658 	}
1659 	xstats_ctrl->stats_n = dev_stats_n;
1660 	/* Copy to base at first time. */
1661 	ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1662 	if (ret)
1663 		DRV_LOG(ERR, "port %u cannot read device counters: %s",
1664 			dev->data->port_id, strerror(rte_errno));
1665 	mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1666 	stats_ctrl->imissed = 0;
1667 free:
1668 	mlx5_free(strings);
1669 }
1670 
1671 /**
1672  * Get MAC address by querying netdevice.
1673  *
1674  * @param[in] dev
1675  *   Pointer to Ethernet device.
1676  * @param[out] mac
1677  *   MAC address output buffer.
1678  *
1679  * @return
1680  *   0 on success, a negative errno value otherwise and rte_errno is set.
1681  */
1682 int
1683 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1684 {
1685 	struct ifreq request;
1686 	int ret;
1687 
1688 	ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1689 	if (ret)
1690 		return ret;
1691 	memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1692 	return 0;
1693 }
1694 
1695 /*
1696  * Query dropless_rq private flag value provided by ETHTOOL.
1697  *
1698  * @param dev
1699  *   Pointer to Ethernet device.
1700  *
1701  * @return
1702  *   - 0 on success, flag is not set.
1703  *   - 1 on success, flag is set.
1704  *   - negative errno value otherwise and rte_errno is set.
1705  */
1706 int mlx5_get_flag_dropless_rq(struct rte_eth_dev *dev)
1707 {
1708 	struct ethtool_sset_info *sset_info = NULL;
1709 	struct ethtool_drvinfo drvinfo;
1710 	struct ifreq ifr;
1711 	struct ethtool_gstrings *strings = NULL;
1712 	struct ethtool_value flags;
1713 	const int32_t flag_len = sizeof(flags.data) * CHAR_BIT;
1714 	int32_t str_sz;
1715 	int32_t len;
1716 	int32_t i;
1717 	int ret;
1718 
1719 	sset_info = mlx5_malloc(0, sizeof(struct ethtool_sset_info) +
1720 			sizeof(uint32_t), 0, SOCKET_ID_ANY);
1721 	if (sset_info == NULL) {
1722 		rte_errno = ENOMEM;
1723 		return -rte_errno;
1724 	}
1725 	sset_info->cmd = ETHTOOL_GSSET_INFO;
1726 	sset_info->reserved = 0;
1727 	sset_info->sset_mask = 1ULL << ETH_SS_PRIV_FLAGS;
1728 	ifr.ifr_data = (caddr_t)&sset_info;
1729 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1730 	if (!ret) {
1731 		const uint32_t *sset_lengths = sset_info->data;
1732 
1733 		len = sset_info->sset_mask ? sset_lengths[0] : 0;
1734 	} else if (ret == -EOPNOTSUPP) {
1735 		drvinfo.cmd = ETHTOOL_GDRVINFO;
1736 		ifr.ifr_data = (caddr_t)&drvinfo;
1737 		ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1738 		if (ret) {
1739 			DRV_LOG(WARNING, "port %u cannot get the driver info",
1740 				dev->data->port_id);
1741 			goto exit;
1742 		}
1743 		len = *(uint32_t *)((char *)&drvinfo +
1744 			offsetof(struct ethtool_drvinfo, n_priv_flags));
1745 	} else {
1746 		DRV_LOG(WARNING, "port %u cannot get the sset info",
1747 			dev->data->port_id);
1748 		goto exit;
1749 	}
1750 	if (!len) {
1751 		DRV_LOG(WARNING, "port %u does not have private flag",
1752 			dev->data->port_id);
1753 		rte_errno = EOPNOTSUPP;
1754 		ret = -rte_errno;
1755 		goto exit;
1756 	} else if (len > flag_len) {
1757 		DRV_LOG(WARNING, "port %u maximal private flags number is %d",
1758 			dev->data->port_id, flag_len);
1759 		len = flag_len;
1760 	}
1761 	str_sz = ETH_GSTRING_LEN * len;
1762 	strings = (struct ethtool_gstrings *)
1763 		  mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1764 			      SOCKET_ID_ANY);
1765 	if (!strings) {
1766 		DRV_LOG(WARNING, "port %u unable to allocate memory for"
1767 			" private flags", dev->data->port_id);
1768 		rte_errno = ENOMEM;
1769 		ret = -rte_errno;
1770 		goto exit;
1771 	}
1772 	strings->cmd = ETHTOOL_GSTRINGS;
1773 	strings->string_set = ETH_SS_PRIV_FLAGS;
1774 	strings->len = len;
1775 	ifr.ifr_data = (caddr_t)strings;
1776 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1777 	if (ret) {
1778 		DRV_LOG(WARNING, "port %u unable to get private flags strings",
1779 			dev->data->port_id);
1780 		goto exit;
1781 	}
1782 	for (i = 0; i < len; i++) {
1783 		strings->data[(i + 1) * ETH_GSTRING_LEN - 1] = 0;
1784 		if (!strcmp((const char *)strings->data + i * ETH_GSTRING_LEN,
1785 			     "dropless_rq"))
1786 			break;
1787 	}
1788 	if (i == len) {
1789 		DRV_LOG(WARNING, "port %u does not support dropless_rq",
1790 			dev->data->port_id);
1791 		rte_errno = EOPNOTSUPP;
1792 		ret = -rte_errno;
1793 		goto exit;
1794 	}
1795 	flags.cmd = ETHTOOL_GPFLAGS;
1796 	ifr.ifr_data = (caddr_t)&flags;
1797 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1798 	if (ret) {
1799 		DRV_LOG(WARNING, "port %u unable to get private flags status",
1800 			dev->data->port_id);
1801 		goto exit;
1802 	}
1803 	ret = !!(flags.data & (1U << i));
1804 exit:
1805 	mlx5_free(strings);
1806 	mlx5_free(sset_info);
1807 	return ret;
1808 }
1809 
1810 /**
1811  * Unmaps HCA PCI BAR from the current process address space.
1812  *
1813  * @param dev
1814  *   Pointer to Ethernet device structure.
1815  */
1816 void mlx5_txpp_unmap_hca_bar(struct rte_eth_dev *dev)
1817 {
1818 	struct mlx5_proc_priv *ppriv = dev->process_private;
1819 
1820 	if (ppriv && ppriv->hca_bar) {
1821 		rte_mem_unmap(ppriv->hca_bar, MLX5_ST_SZ_BYTES(initial_seg));
1822 		ppriv->hca_bar = NULL;
1823 	}
1824 }
1825 
1826 /**
1827  * Maps HCA PCI BAR to the current process address space.
1828  * Stores pointer in the process private structure allowing
1829  * to read internal and real time counter directly from the HW.
1830  *
1831  * @param dev
1832  *   Pointer to Ethernet device structure.
1833  *
1834  * @return
1835  *   0 on success and not NULL pointer to mapped area in process structure.
1836  *   negative otherwise and NULL pointer
1837  */
1838 int mlx5_txpp_map_hca_bar(struct rte_eth_dev *dev)
1839 {
1840 	struct mlx5_proc_priv *ppriv = dev->process_private;
1841 	char pci_addr[PCI_PRI_STR_SIZE] = { 0 };
1842 	void *base, *expected = NULL;
1843 	int fd, ret;
1844 
1845 	if (!ppriv) {
1846 		rte_errno = ENOMEM;
1847 		return -rte_errno;
1848 	}
1849 	if (ppriv->hca_bar)
1850 		return 0;
1851 	ret = mlx5_dev_to_pci_str(dev->device, pci_addr, sizeof(pci_addr));
1852 	if (ret < 0)
1853 		return -rte_errno;
1854 	/* Open PCI device resource 0 - HCA initialize segment */
1855 	MKSTR(name, "/sys/bus/pci/devices/%s/resource0", pci_addr);
1856 	fd = open(name, O_RDWR | O_SYNC);
1857 	if (fd == -1) {
1858 		rte_errno = ENOTSUP;
1859 		return -ENOTSUP;
1860 	}
1861 	base = rte_mem_map(NULL, MLX5_ST_SZ_BYTES(initial_seg),
1862 			   RTE_PROT_READ, RTE_MAP_SHARED, fd, 0);
1863 	close(fd);
1864 	if (!base) {
1865 		rte_errno = ENOTSUP;
1866 		return -ENOTSUP;
1867 	}
1868 	/* Check there is no concurrent mapping in other thread. */
1869 	if (!__atomic_compare_exchange_n(&ppriv->hca_bar, &expected,
1870 					 base, false,
1871 					 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
1872 		rte_mem_unmap(base, MLX5_ST_SZ_BYTES(initial_seg));
1873 	return 0;
1874 }
1875 
1876