xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision 175f1c21d0334e96d959fbe88a614a4bcc1b1e99)
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 <assert.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <dirent.h>
17 #include <net/if.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
23 #include <fcntl.h>
24 #include <stdalign.h>
25 #include <sys/un.h>
26 #include <time.h>
27 
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
31 #include <rte_mbuf.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_malloc.h>
35 #include <rte_string_fns.h>
36 #include <rte_rwlock.h>
37 #include <rte_cycles.h>
38 
39 #include "mlx5.h"
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
43 
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
47 #endif
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
50 #endif
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
53 #endif
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
56 #endif
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
59 #endif
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
62 #endif
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
65 #endif
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
68 #endif
69 
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
73 	uint32_t cmd;
74 	uint32_t speed;
75 	uint8_t duplex;
76 	uint8_t port;
77 	uint8_t phy_address;
78 	uint8_t autoneg;
79 	uint8_t mdio_support;
80 	uint8_t eth_to_mdix;
81 	uint8_t eth_tp_mdix_ctrl;
82 	int8_t link_mode_masks_nwords;
83 	uint32_t reserved[8];
84 	uint32_t link_mode_masks[];
85 };
86 
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
104 #endif
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
109 #endif
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
113 #endif
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
119 #endif
120 
121 /**
122  * Get master interface name from private structure.
123  *
124  * @param[in] dev
125  *   Pointer to Ethernet device.
126  * @param[out] ifname
127  *   Interface name output buffer.
128  *
129  * @return
130  *   0 on success, a negative errno value otherwise and rte_errno is set.
131  */
132 int
133 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
134 {
135 	DIR *dir;
136 	struct dirent *dent;
137 	unsigned int dev_type = 0;
138 	unsigned int dev_port_prev = ~0u;
139 	char match[IF_NAMESIZE] = "";
140 
141 	assert(ibdev_path);
142 	{
143 		MKSTR(path, "%s/device/net", ibdev_path);
144 
145 		dir = opendir(path);
146 		if (dir == NULL) {
147 			rte_errno = errno;
148 			return -rte_errno;
149 		}
150 	}
151 	while ((dent = readdir(dir)) != NULL) {
152 		char *name = dent->d_name;
153 		FILE *file;
154 		unsigned int dev_port;
155 		int r;
156 
157 		if ((name[0] == '.') &&
158 		    ((name[1] == '\0') ||
159 		     ((name[1] == '.') && (name[2] == '\0'))))
160 			continue;
161 
162 		MKSTR(path, "%s/device/net/%s/%s",
163 		      ibdev_path, name,
164 		      (dev_type ? "dev_id" : "dev_port"));
165 
166 		file = fopen(path, "rb");
167 		if (file == NULL) {
168 			if (errno != ENOENT)
169 				continue;
170 			/*
171 			 * Switch to dev_id when dev_port does not exist as
172 			 * is the case with Linux kernel versions < 3.15.
173 			 */
174 try_dev_id:
175 			match[0] = '\0';
176 			if (dev_type)
177 				break;
178 			dev_type = 1;
179 			dev_port_prev = ~0u;
180 			rewinddir(dir);
181 			continue;
182 		}
183 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
184 		fclose(file);
185 		if (r != 1)
186 			continue;
187 		/*
188 		 * Switch to dev_id when dev_port returns the same value for
189 		 * all ports. May happen when using a MOFED release older than
190 		 * 3.0 with a Linux kernel >= 3.15.
191 		 */
192 		if (dev_port == dev_port_prev)
193 			goto try_dev_id;
194 		dev_port_prev = dev_port;
195 		if (dev_port == 0)
196 			strlcpy(match, name, sizeof(match));
197 	}
198 	closedir(dir);
199 	if (match[0] == '\0') {
200 		rte_errno = ENOENT;
201 		return -rte_errno;
202 	}
203 	strncpy(*ifname, match, sizeof(*ifname));
204 	return 0;
205 }
206 
207 /**
208  * Get interface name from private structure.
209  *
210  * This is a port representor-aware version of mlx5_get_master_ifname().
211  *
212  * @param[in] dev
213  *   Pointer to Ethernet device.
214  * @param[out] ifname
215  *   Interface name output buffer.
216  *
217  * @return
218  *   0 on success, a negative errno value otherwise and rte_errno is set.
219  */
220 int
221 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
222 {
223 	struct mlx5_priv *priv = dev->data->dev_private;
224 	unsigned int ifindex;
225 
226 	assert(priv);
227 	assert(priv->sh);
228 	ifindex = mlx5_ifindex(dev);
229 	if (!ifindex) {
230 		if (!priv->representor)
231 			return mlx5_get_master_ifname(priv->sh->ibdev_path,
232 						      ifname);
233 		rte_errno = ENXIO;
234 		return -rte_errno;
235 	}
236 	if (if_indextoname(ifindex, &(*ifname)[0]))
237 		return 0;
238 	rte_errno = errno;
239 	return -rte_errno;
240 }
241 
242 /**
243  * Get the interface index from device name.
244  *
245  * @param[in] dev
246  *   Pointer to Ethernet device.
247  *
248  * @return
249  *   Nonzero interface index on success, zero otherwise and rte_errno is set.
250  */
251 unsigned int
252 mlx5_ifindex(const struct rte_eth_dev *dev)
253 {
254 	struct mlx5_priv *priv = dev->data->dev_private;
255 	unsigned int ifindex;
256 
257 	assert(priv);
258 	assert(priv->if_index);
259 	ifindex = priv->if_index;
260 	if (!ifindex)
261 		rte_errno = ENXIO;
262 	return ifindex;
263 }
264 
265 /**
266  * Perform ifreq ioctl() on associated Ethernet device.
267  *
268  * @param[in] dev
269  *   Pointer to Ethernet device.
270  * @param req
271  *   Request number to pass to ioctl().
272  * @param[out] ifr
273  *   Interface request structure output buffer.
274  *
275  * @return
276  *   0 on success, a negative errno value otherwise and rte_errno is set.
277  */
278 int
279 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
280 {
281 	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
282 	int ret = 0;
283 
284 	if (sock == -1) {
285 		rte_errno = errno;
286 		return -rte_errno;
287 	}
288 	ret = mlx5_get_ifname(dev, &ifr->ifr_name);
289 	if (ret)
290 		goto error;
291 	ret = ioctl(sock, req, ifr);
292 	if (ret == -1) {
293 		rte_errno = errno;
294 		goto error;
295 	}
296 	close(sock);
297 	return 0;
298 error:
299 	close(sock);
300 	return -rte_errno;
301 }
302 
303 /**
304  * Get device MTU.
305  *
306  * @param dev
307  *   Pointer to Ethernet device.
308  * @param[out] mtu
309  *   MTU value output buffer.
310  *
311  * @return
312  *   0 on success, a negative errno value otherwise and rte_errno is set.
313  */
314 int
315 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
316 {
317 	struct ifreq request;
318 	int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
319 
320 	if (ret)
321 		return ret;
322 	*mtu = request.ifr_mtu;
323 	return 0;
324 }
325 
326 /**
327  * Set device MTU.
328  *
329  * @param dev
330  *   Pointer to Ethernet device.
331  * @param mtu
332  *   MTU value to set.
333  *
334  * @return
335  *   0 on success, a negative errno value otherwise and rte_errno is set.
336  */
337 static int
338 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
339 {
340 	struct ifreq request = { .ifr_mtu = mtu, };
341 
342 	return mlx5_ifreq(dev, SIOCSIFMTU, &request);
343 }
344 
345 /**
346  * Set device flags.
347  *
348  * @param dev
349  *   Pointer to Ethernet device.
350  * @param keep
351  *   Bitmask for flags that must remain untouched.
352  * @param flags
353  *   Bitmask for flags to modify.
354  *
355  * @return
356  *   0 on success, a negative errno value otherwise and rte_errno is set.
357  */
358 int
359 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
360 {
361 	struct ifreq request;
362 	int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
363 
364 	if (ret)
365 		return ret;
366 	request.ifr_flags &= keep;
367 	request.ifr_flags |= flags & ~keep;
368 	return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
369 }
370 
371 /**
372  * DPDK callback for Ethernet device configuration.
373  *
374  * @param dev
375  *   Pointer to Ethernet device structure.
376  *
377  * @return
378  *   0 on success, a negative errno value otherwise and rte_errno is set.
379  */
380 int
381 mlx5_dev_configure(struct rte_eth_dev *dev)
382 {
383 	struct mlx5_priv *priv = dev->data->dev_private;
384 	unsigned int rxqs_n = dev->data->nb_rx_queues;
385 	unsigned int txqs_n = dev->data->nb_tx_queues;
386 	unsigned int i;
387 	unsigned int j;
388 	unsigned int reta_idx_n;
389 	const uint8_t use_app_rss_key =
390 		!!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
391 	int ret = 0;
392 	unsigned int lro_on = mlx5_lro_on(dev);
393 
394 	if (use_app_rss_key &&
395 	    (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
396 	     MLX5_RSS_HASH_KEY_LEN)) {
397 		DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
398 			dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
399 		rte_errno = EINVAL;
400 		return -rte_errno;
401 	}
402 	priv->rss_conf.rss_key =
403 		rte_realloc(priv->rss_conf.rss_key,
404 			    MLX5_RSS_HASH_KEY_LEN, 0);
405 	if (!priv->rss_conf.rss_key) {
406 		DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
407 			dev->data->port_id, rxqs_n);
408 		rte_errno = ENOMEM;
409 		return -rte_errno;
410 	}
411 	memcpy(priv->rss_conf.rss_key,
412 	       use_app_rss_key ?
413 	       dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
414 	       rss_hash_default_key,
415 	       MLX5_RSS_HASH_KEY_LEN);
416 	priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
417 	priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
418 	priv->rxqs = (void *)dev->data->rx_queues;
419 	priv->txqs = (void *)dev->data->tx_queues;
420 	if (txqs_n != priv->txqs_n) {
421 		DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
422 			dev->data->port_id, priv->txqs_n, txqs_n);
423 		priv->txqs_n = txqs_n;
424 	}
425 	if (rxqs_n > priv->config.ind_table_max_size) {
426 		DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
427 			dev->data->port_id, rxqs_n);
428 		rte_errno = EINVAL;
429 		return -rte_errno;
430 	}
431 	if (rxqs_n != priv->rxqs_n) {
432 		DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
433 			dev->data->port_id, priv->rxqs_n, rxqs_n);
434 		priv->rxqs_n = rxqs_n;
435 		/*
436 		 * WHen using LRO, MPRQ is implicitly enabled.
437 		 * Adjust threshold value to ensure MPRQ can be enabled.
438 		 */
439 		if (lro_on && priv->config.mprq.min_rxqs_num > priv->rxqs_n)
440 			priv->config.mprq.min_rxqs_num = priv->rxqs_n;
441 		/*
442 		 * If the requested number of RX queues is not a power of two,
443 		 * use the maximum indirection table size for better balancing.
444 		 * The result is always rounded to the next power of two.
445 		 */
446 		reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
447 					     priv->config.ind_table_max_size :
448 					     rxqs_n));
449 		ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
450 		if (ret)
451 			return ret;
452 		/*
453 		 * When the number of RX queues is not a power of two,
454 		 * the remaining table entries are padded with reused WQs
455 		 * and hashes are not spread uniformly.
456 		 */
457 		for (i = 0, j = 0; (i != reta_idx_n); ++i) {
458 			(*priv->reta_idx)[i] = j;
459 			if (++j == rxqs_n)
460 				j = 0;
461 		}
462 	}
463 	if (lro_on && priv->config.cqe_comp) {
464 		/* CQE compressing is not supported for LRO CQEs. */
465 		DRV_LOG(WARNING, "Rx CQE compression isn't supported with LRO");
466 		priv->config.cqe_comp = 0;
467 	}
468 	ret = mlx5_proc_priv_init(dev);
469 	if (ret)
470 		return ret;
471 	return 0;
472 }
473 
474 /**
475  * Sets default tuning parameters.
476  *
477  * @param dev
478  *   Pointer to Ethernet device.
479  * @param[out] info
480  *   Info structure output buffer.
481  */
482 static void
483 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
484 {
485 	struct mlx5_priv *priv = dev->data->dev_private;
486 
487 	/* Minimum CPU utilization. */
488 	info->default_rxportconf.ring_size = 256;
489 	info->default_txportconf.ring_size = 256;
490 	info->default_rxportconf.burst_size = 64;
491 	info->default_txportconf.burst_size = 64;
492 	if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
493 		info->default_rxportconf.nb_queues = 16;
494 		info->default_txportconf.nb_queues = 16;
495 		if (dev->data->nb_rx_queues > 2 ||
496 		    dev->data->nb_tx_queues > 2) {
497 			/* Max Throughput. */
498 			info->default_rxportconf.ring_size = 2048;
499 			info->default_txportconf.ring_size = 2048;
500 		}
501 	} else {
502 		info->default_rxportconf.nb_queues = 8;
503 		info->default_txportconf.nb_queues = 8;
504 		if (dev->data->nb_rx_queues > 2 ||
505 		    dev->data->nb_tx_queues > 2) {
506 			/* Max Throughput. */
507 			info->default_rxportconf.ring_size = 4096;
508 			info->default_txportconf.ring_size = 4096;
509 		}
510 	}
511 }
512 
513 /**
514  * Sets tx mbuf limiting parameters.
515  *
516  * @param dev
517  *   Pointer to Ethernet device.
518  * @param[out] info
519  *   Info structure output buffer.
520  */
521 static void
522 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
523 {
524 	struct mlx5_priv *priv = dev->data->dev_private;
525 	struct mlx5_dev_config *config = &priv->config;
526 	unsigned int inlen;
527 	uint16_t nb_max;
528 
529 	inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
530 		MLX5_SEND_DEF_INLINE_LEN :
531 		(unsigned int)config->txq_inline_max;
532 	assert(config->txq_inline_min >= 0);
533 	inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
534 	inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
535 			       MLX5_ESEG_MIN_INLINE_SIZE -
536 			       MLX5_WQE_CSEG_SIZE -
537 			       MLX5_WQE_ESEG_SIZE -
538 			       MLX5_WQE_DSEG_SIZE * 2);
539 	nb_max = (MLX5_WQE_SIZE_MAX +
540 		  MLX5_ESEG_MIN_INLINE_SIZE -
541 		  MLX5_WQE_CSEG_SIZE -
542 		  MLX5_WQE_ESEG_SIZE -
543 		  MLX5_WQE_DSEG_SIZE -
544 		  inlen) / MLX5_WSEG_SIZE;
545 	info->tx_desc_lim.nb_seg_max = nb_max;
546 	info->tx_desc_lim.nb_mtu_seg_max = nb_max;
547 }
548 
549 /**
550  * DPDK callback to get information about the device.
551  *
552  * @param dev
553  *   Pointer to Ethernet device structure.
554  * @param[out] info
555  *   Info structure output buffer.
556  */
557 void
558 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
559 {
560 	struct mlx5_priv *priv = dev->data->dev_private;
561 	struct mlx5_dev_config *config = &priv->config;
562 	unsigned int max;
563 
564 	/* FIXME: we should ask the device for these values. */
565 	info->min_rx_bufsize = 32;
566 	info->max_rx_pktlen = 65536;
567 	/*
568 	 * Since we need one CQ per QP, the limit is the minimum number
569 	 * between the two values.
570 	 */
571 	max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
572 		      priv->sh->device_attr.orig_attr.max_qp);
573 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
574 	if (max >= 65535)
575 		max = 65535;
576 	info->max_rx_queues = max;
577 	info->max_tx_queues = max;
578 	info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
579 	info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
580 	info->rx_offload_capa = (mlx5_get_rx_port_offloads(dev) |
581 				 info->rx_queue_offload_capa);
582 	info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
583 	info->if_index = mlx5_ifindex(dev);
584 	info->reta_size = priv->reta_idx_n ?
585 		priv->reta_idx_n : config->ind_table_max_size;
586 	info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
587 	info->speed_capa = priv->link_speed_capa;
588 	info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
589 	mlx5_set_default_params(dev, info);
590 	mlx5_set_txlimit_params(dev, info);
591 	info->switch_info.name = dev->data->name;
592 	info->switch_info.domain_id = priv->domain_id;
593 	info->switch_info.port_id = priv->representor_id;
594 	if (priv->representor) {
595 		unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
596 		uint16_t port_id[i];
597 
598 		i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
599 		while (i--) {
600 			struct mlx5_priv *opriv =
601 				rte_eth_devices[port_id[i]].data->dev_private;
602 
603 			if (!opriv ||
604 			    opriv->representor ||
605 			    opriv->domain_id != priv->domain_id)
606 				continue;
607 			/*
608 			 * Override switch name with that of the master
609 			 * device.
610 			 */
611 			info->switch_info.name = opriv->dev_data->name;
612 			break;
613 		}
614 	}
615 }
616 
617 /**
618  * Get device current raw clock counter
619  *
620  * @param dev
621  *   Pointer to Ethernet device structure.
622  * @param[out] time
623  *   Current raw clock counter of the device.
624  *
625  * @return
626  *   0 if the clock has correctly been read
627  *   The value of errno in case of error
628  */
629 int
630 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
631 {
632 	struct mlx5_priv *priv = dev->data->dev_private;
633 	struct ibv_context *ctx = priv->sh->ctx;
634 	struct ibv_values_ex values;
635 	int err = 0;
636 
637 	values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
638 	err = mlx5_glue->query_rt_values_ex(ctx, &values);
639 	if (err != 0) {
640 		DRV_LOG(WARNING, "Could not query the clock !");
641 		return err;
642 	}
643 	*clock = values.raw_clock.tv_nsec;
644 	return 0;
645 }
646 
647 /**
648  * Get firmware version of a device.
649  *
650  * @param dev
651  *   Ethernet device port.
652  * @param fw_ver
653  *   String output allocated by caller.
654  * @param fw_size
655  *   Size of the output string, including terminating null byte.
656  *
657  * @return
658  *   0 on success, or the size of the non truncated string if too big.
659  */
660 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
661 {
662 	struct mlx5_priv *priv = dev->data->dev_private;
663 	struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
664 	size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
665 
666 	if (fw_size < size)
667 		return size;
668 	if (fw_ver != NULL)
669 		strlcpy(fw_ver, attr->fw_ver, fw_size);
670 	return 0;
671 }
672 
673 /**
674  * Get supported packet types.
675  *
676  * @param dev
677  *   Pointer to Ethernet device structure.
678  *
679  * @return
680  *   A pointer to the supported Packet types array.
681  */
682 const uint32_t *
683 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
684 {
685 	static const uint32_t ptypes[] = {
686 		/* refers to rxq_cq_to_pkt_type() */
687 		RTE_PTYPE_L2_ETHER,
688 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
689 		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
690 		RTE_PTYPE_L4_NONFRAG,
691 		RTE_PTYPE_L4_FRAG,
692 		RTE_PTYPE_L4_TCP,
693 		RTE_PTYPE_L4_UDP,
694 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
695 		RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
696 		RTE_PTYPE_INNER_L4_NONFRAG,
697 		RTE_PTYPE_INNER_L4_FRAG,
698 		RTE_PTYPE_INNER_L4_TCP,
699 		RTE_PTYPE_INNER_L4_UDP,
700 		RTE_PTYPE_UNKNOWN
701 	};
702 
703 	if (dev->rx_pkt_burst == mlx5_rx_burst ||
704 	    dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
705 	    dev->rx_pkt_burst == mlx5_rx_burst_vec)
706 		return ptypes;
707 	return NULL;
708 }
709 
710 /**
711  * Retrieve the master device for representor in the same switch domain.
712  *
713  * @param dev
714  *   Pointer to representor Ethernet device structure.
715  *
716  * @return
717  *   Master device structure  on success, NULL otherwise.
718  */
719 
720 static struct rte_eth_dev *
721 mlx5_find_master_dev(struct rte_eth_dev *dev)
722 {
723 	struct mlx5_priv *priv;
724 	uint16_t port_id;
725 	uint16_t domain_id;
726 
727 	priv = dev->data->dev_private;
728 	domain_id = priv->domain_id;
729 	assert(priv->representor);
730 	RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
731 		priv = rte_eth_devices[port_id].data->dev_private;
732 		if (priv &&
733 		    priv->master &&
734 		    priv->domain_id == domain_id)
735 			return &rte_eth_devices[port_id];
736 	}
737 	return NULL;
738 }
739 
740 /**
741  * DPDK callback to retrieve physical link information.
742  *
743  * @param dev
744  *   Pointer to Ethernet device structure.
745  * @param[out] link
746  *   Storage for current link status.
747  *
748  * @return
749  *   0 on success, a negative errno value otherwise and rte_errno is set.
750  */
751 static int
752 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
753 			       struct rte_eth_link *link)
754 {
755 	struct mlx5_priv *priv = dev->data->dev_private;
756 	struct ethtool_cmd edata = {
757 		.cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
758 	};
759 	struct ifreq ifr;
760 	struct rte_eth_link dev_link;
761 	int link_speed = 0;
762 	int ret;
763 
764 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
765 	if (ret) {
766 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
767 			dev->data->port_id, strerror(rte_errno));
768 		return ret;
769 	}
770 	dev_link = (struct rte_eth_link) {
771 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
772 				(ifr.ifr_flags & IFF_RUNNING)),
773 	};
774 	ifr = (struct ifreq) {
775 		.ifr_data = (void *)&edata,
776 	};
777 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
778 	if (ret) {
779 		if (ret == -ENOTSUP && priv->representor) {
780 			struct rte_eth_dev *master;
781 
782 			/*
783 			 * For representors we can try to inherit link
784 			 * settings from the master device. Actually
785 			 * link settings do not make a lot of sense
786 			 * for representors due to missing physical
787 			 * link. The old kernel drivers supported
788 			 * emulated settings query for representors,
789 			 * the new ones do not, so we have to add
790 			 * this code for compatibility issues.
791 			 */
792 			master = mlx5_find_master_dev(dev);
793 			if (master) {
794 				ifr = (struct ifreq) {
795 					.ifr_data = (void *)&edata,
796 				};
797 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
798 			}
799 		}
800 		if (ret) {
801 			DRV_LOG(WARNING,
802 				"port %u ioctl(SIOCETHTOOL,"
803 				" ETHTOOL_GSET) failed: %s",
804 				dev->data->port_id, strerror(rte_errno));
805 			return ret;
806 		}
807 	}
808 	link_speed = ethtool_cmd_speed(&edata);
809 	if (link_speed == -1)
810 		dev_link.link_speed = ETH_SPEED_NUM_NONE;
811 	else
812 		dev_link.link_speed = link_speed;
813 	priv->link_speed_capa = 0;
814 	if (edata.supported & SUPPORTED_Autoneg)
815 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
816 	if (edata.supported & (SUPPORTED_1000baseT_Full |
817 			       SUPPORTED_1000baseKX_Full))
818 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
819 	if (edata.supported & SUPPORTED_10000baseKR_Full)
820 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
821 	if (edata.supported & (SUPPORTED_40000baseKR4_Full |
822 			       SUPPORTED_40000baseCR4_Full |
823 			       SUPPORTED_40000baseSR4_Full |
824 			       SUPPORTED_40000baseLR4_Full))
825 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
826 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
827 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
828 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
829 			ETH_LINK_SPEED_FIXED);
830 	if (((dev_link.link_speed && !dev_link.link_status) ||
831 	     (!dev_link.link_speed && dev_link.link_status))) {
832 		rte_errno = EAGAIN;
833 		return -rte_errno;
834 	}
835 	*link = dev_link;
836 	return 0;
837 }
838 
839 /**
840  * Retrieve physical link information (unlocked version using new ioctl).
841  *
842  * @param dev
843  *   Pointer to Ethernet device structure.
844  * @param[out] link
845  *   Storage for current link status.
846  *
847  * @return
848  *   0 on success, a negative errno value otherwise and rte_errno is set.
849  */
850 static int
851 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
852 			     struct rte_eth_link *link)
853 
854 {
855 	struct mlx5_priv *priv = dev->data->dev_private;
856 	struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
857 	struct ifreq ifr;
858 	struct rte_eth_link dev_link;
859 	struct rte_eth_dev *master = NULL;
860 	uint64_t sc;
861 	int ret;
862 
863 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
864 	if (ret) {
865 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
866 			dev->data->port_id, strerror(rte_errno));
867 		return ret;
868 	}
869 	dev_link = (struct rte_eth_link) {
870 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
871 				(ifr.ifr_flags & IFF_RUNNING)),
872 	};
873 	ifr = (struct ifreq) {
874 		.ifr_data = (void *)&gcmd,
875 	};
876 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
877 	if (ret) {
878 		if (ret == -ENOTSUP && priv->representor) {
879 			/*
880 			 * For representors we can try to inherit link
881 			 * settings from the master device. Actually
882 			 * link settings do not make a lot of sense
883 			 * for representors due to missing physical
884 			 * link. The old kernel drivers supported
885 			 * emulated settings query for representors,
886 			 * the new ones do not, so we have to add
887 			 * this code for compatibility issues.
888 			 */
889 			master = mlx5_find_master_dev(dev);
890 			if (master) {
891 				ifr = (struct ifreq) {
892 					.ifr_data = (void *)&gcmd,
893 				};
894 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
895 			}
896 		}
897 		if (ret) {
898 			DRV_LOG(DEBUG,
899 				"port %u ioctl(SIOCETHTOOL,"
900 				" ETHTOOL_GLINKSETTINGS) failed: %s",
901 				dev->data->port_id, strerror(rte_errno));
902 			return ret;
903 		}
904 
905 	}
906 	gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
907 
908 	alignas(struct ethtool_link_settings)
909 	uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
910 		     sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
911 	struct ethtool_link_settings *ecmd = (void *)data;
912 
913 	*ecmd = gcmd;
914 	ifr.ifr_data = (void *)ecmd;
915 	ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
916 	if (ret) {
917 		DRV_LOG(DEBUG,
918 			"port %u ioctl(SIOCETHTOOL,"
919 			"ETHTOOL_GLINKSETTINGS) failed: %s",
920 			dev->data->port_id, strerror(rte_errno));
921 		return ret;
922 	}
923 	dev_link.link_speed = ecmd->speed;
924 	sc = ecmd->link_mode_masks[0] |
925 		((uint64_t)ecmd->link_mode_masks[1] << 32);
926 	priv->link_speed_capa = 0;
927 	if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
928 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
929 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
930 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
931 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
932 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
933 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
934 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
935 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
936 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
937 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
938 		priv->link_speed_capa |= ETH_LINK_SPEED_20G;
939 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
940 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
941 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
942 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
943 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
944 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
945 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
946 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
947 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
948 		priv->link_speed_capa |= ETH_LINK_SPEED_56G;
949 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
950 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
951 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
952 		priv->link_speed_capa |= ETH_LINK_SPEED_25G;
953 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
954 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
955 		priv->link_speed_capa |= ETH_LINK_SPEED_50G;
956 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
957 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
958 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
959 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
960 		priv->link_speed_capa |= ETH_LINK_SPEED_100G;
961 	dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
962 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
963 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
964 				  ETH_LINK_SPEED_FIXED);
965 	if (((dev_link.link_speed && !dev_link.link_status) ||
966 	     (!dev_link.link_speed && dev_link.link_status))) {
967 		rte_errno = EAGAIN;
968 		return -rte_errno;
969 	}
970 	*link = dev_link;
971 	return 0;
972 }
973 
974 /**
975  * DPDK callback to retrieve physical link information.
976  *
977  * @param dev
978  *   Pointer to Ethernet device structure.
979  * @param wait_to_complete
980  *   Wait for request completion.
981  *
982  * @return
983  *   0 if link status was not updated, positive if it was, a negative errno
984  *   value otherwise and rte_errno is set.
985  */
986 int
987 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
988 {
989 	int ret;
990 	struct rte_eth_link dev_link;
991 	time_t start_time = time(NULL);
992 
993 	do {
994 		ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
995 		if (ret == -ENOTSUP)
996 			ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
997 		if (ret == 0)
998 			break;
999 		/* Handle wait to complete situation. */
1000 		if (wait_to_complete && ret == -EAGAIN) {
1001 			if (abs((int)difftime(time(NULL), start_time)) <
1002 			    MLX5_LINK_STATUS_TIMEOUT) {
1003 				usleep(0);
1004 				continue;
1005 			} else {
1006 				rte_errno = EBUSY;
1007 				return -rte_errno;
1008 			}
1009 		} else if (ret < 0) {
1010 			return ret;
1011 		}
1012 	} while (wait_to_complete);
1013 	ret = !!memcmp(&dev->data->dev_link, &dev_link,
1014 		       sizeof(struct rte_eth_link));
1015 	dev->data->dev_link = dev_link;
1016 	return ret;
1017 }
1018 
1019 /**
1020  * DPDK callback to change the MTU.
1021  *
1022  * @param dev
1023  *   Pointer to Ethernet device structure.
1024  * @param in_mtu
1025  *   New MTU.
1026  *
1027  * @return
1028  *   0 on success, a negative errno value otherwise and rte_errno is set.
1029  */
1030 int
1031 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1032 {
1033 	struct mlx5_priv *priv = dev->data->dev_private;
1034 	uint16_t kern_mtu = 0;
1035 	int ret;
1036 
1037 	ret = mlx5_get_mtu(dev, &kern_mtu);
1038 	if (ret)
1039 		return ret;
1040 	/* Set kernel interface MTU first. */
1041 	ret = mlx5_set_mtu(dev, mtu);
1042 	if (ret)
1043 		return ret;
1044 	ret = mlx5_get_mtu(dev, &kern_mtu);
1045 	if (ret)
1046 		return ret;
1047 	if (kern_mtu == mtu) {
1048 		priv->mtu = mtu;
1049 		DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1050 			dev->data->port_id, mtu);
1051 		return 0;
1052 	}
1053 	rte_errno = EAGAIN;
1054 	return -rte_errno;
1055 }
1056 
1057 /**
1058  * DPDK callback to get flow control status.
1059  *
1060  * @param dev
1061  *   Pointer to Ethernet device structure.
1062  * @param[out] fc_conf
1063  *   Flow control output buffer.
1064  *
1065  * @return
1066  *   0 on success, a negative errno value otherwise and rte_errno is set.
1067  */
1068 int
1069 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1070 {
1071 	struct ifreq ifr;
1072 	struct ethtool_pauseparam ethpause = {
1073 		.cmd = ETHTOOL_GPAUSEPARAM
1074 	};
1075 	int ret;
1076 
1077 	ifr.ifr_data = (void *)&ethpause;
1078 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1079 	if (ret) {
1080 		DRV_LOG(WARNING,
1081 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1082 			" %s",
1083 			dev->data->port_id, strerror(rte_errno));
1084 		return ret;
1085 	}
1086 	fc_conf->autoneg = ethpause.autoneg;
1087 	if (ethpause.rx_pause && ethpause.tx_pause)
1088 		fc_conf->mode = RTE_FC_FULL;
1089 	else if (ethpause.rx_pause)
1090 		fc_conf->mode = RTE_FC_RX_PAUSE;
1091 	else if (ethpause.tx_pause)
1092 		fc_conf->mode = RTE_FC_TX_PAUSE;
1093 	else
1094 		fc_conf->mode = RTE_FC_NONE;
1095 	return 0;
1096 }
1097 
1098 /**
1099  * DPDK callback to modify flow control parameters.
1100  *
1101  * @param dev
1102  *   Pointer to Ethernet device structure.
1103  * @param[in] fc_conf
1104  *   Flow control parameters.
1105  *
1106  * @return
1107  *   0 on success, a negative errno value otherwise and rte_errno is set.
1108  */
1109 int
1110 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1111 {
1112 	struct ifreq ifr;
1113 	struct ethtool_pauseparam ethpause = {
1114 		.cmd = ETHTOOL_SPAUSEPARAM
1115 	};
1116 	int ret;
1117 
1118 	ifr.ifr_data = (void *)&ethpause;
1119 	ethpause.autoneg = fc_conf->autoneg;
1120 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1121 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
1122 		ethpause.rx_pause = 1;
1123 	else
1124 		ethpause.rx_pause = 0;
1125 
1126 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1127 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
1128 		ethpause.tx_pause = 1;
1129 	else
1130 		ethpause.tx_pause = 0;
1131 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1132 	if (ret) {
1133 		DRV_LOG(WARNING,
1134 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1135 			" failed: %s",
1136 			dev->data->port_id, strerror(rte_errno));
1137 		return ret;
1138 	}
1139 	return 0;
1140 }
1141 
1142 /**
1143  * Get PCI information from struct ibv_device.
1144  *
1145  * @param device
1146  *   Pointer to Ethernet device structure.
1147  * @param[out] pci_addr
1148  *   PCI bus address output buffer.
1149  *
1150  * @return
1151  *   0 on success, a negative errno value otherwise and rte_errno is set.
1152  */
1153 int
1154 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1155 			    struct rte_pci_addr *pci_addr)
1156 {
1157 	FILE *file;
1158 	char line[32];
1159 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
1160 
1161 	file = fopen(path, "rb");
1162 	if (file == NULL) {
1163 		rte_errno = errno;
1164 		return -rte_errno;
1165 	}
1166 	while (fgets(line, sizeof(line), file) == line) {
1167 		size_t len = strlen(line);
1168 		int ret;
1169 
1170 		/* Truncate long lines. */
1171 		if (len == (sizeof(line) - 1))
1172 			while (line[(len - 1)] != '\n') {
1173 				ret = fgetc(file);
1174 				if (ret == EOF)
1175 					break;
1176 				line[(len - 1)] = ret;
1177 			}
1178 		/* Extract information. */
1179 		if (sscanf(line,
1180 			   "PCI_SLOT_NAME="
1181 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1182 			   &pci_addr->domain,
1183 			   &pci_addr->bus,
1184 			   &pci_addr->devid,
1185 			   &pci_addr->function) == 4) {
1186 			ret = 0;
1187 			break;
1188 		}
1189 	}
1190 	fclose(file);
1191 	return 0;
1192 }
1193 
1194 /**
1195  * Handle asynchronous removal event for entire multiport device.
1196  *
1197  * @param sh
1198  *   Infiniband device shared context.
1199  */
1200 static void
1201 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1202 {
1203 	uint32_t i;
1204 
1205 	for (i = 0; i < sh->max_port; ++i) {
1206 		struct rte_eth_dev *dev;
1207 
1208 		if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1209 			/*
1210 			 * Or not existing port either no
1211 			 * handler installed for this port.
1212 			 */
1213 			continue;
1214 		}
1215 		dev = &rte_eth_devices[sh->port[i].ih_port_id];
1216 		assert(dev);
1217 		if (dev->data->dev_conf.intr_conf.rmv)
1218 			_rte_eth_dev_callback_process
1219 				(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1220 	}
1221 }
1222 
1223 /**
1224  * Handle shared asynchronous events the NIC (removal event
1225  * and link status change). Supports multiport IB device.
1226  *
1227  * @param cb_arg
1228  *   Callback argument.
1229  */
1230 void
1231 mlx5_dev_interrupt_handler(void *cb_arg)
1232 {
1233 	struct mlx5_ibv_shared *sh = cb_arg;
1234 	struct ibv_async_event event;
1235 
1236 	/* Read all message from the IB device and acknowledge them. */
1237 	for (;;) {
1238 		struct rte_eth_dev *dev;
1239 		uint32_t tmp;
1240 
1241 		if (mlx5_glue->get_async_event(sh->ctx, &event))
1242 			break;
1243 		/* Retrieve and check IB port index. */
1244 		tmp = (uint32_t)event.element.port_num;
1245 		if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1246 			/*
1247 			 * The DEVICE_FATAL event is called once for
1248 			 * entire device without port specifying.
1249 			 * We should notify all existing ports.
1250 			 */
1251 			mlx5_glue->ack_async_event(&event);
1252 			mlx5_dev_interrupt_device_fatal(sh);
1253 			continue;
1254 		}
1255 		assert(tmp && (tmp <= sh->max_port));
1256 		if (!tmp) {
1257 			/* Unsupported devive level event. */
1258 			mlx5_glue->ack_async_event(&event);
1259 			DRV_LOG(DEBUG,
1260 				"unsupported common event (type %d)",
1261 				event.event_type);
1262 			continue;
1263 		}
1264 		if (tmp > sh->max_port) {
1265 			/* Invalid IB port index. */
1266 			mlx5_glue->ack_async_event(&event);
1267 			DRV_LOG(DEBUG,
1268 				"cannot handle an event (type %d)"
1269 				"due to invalid IB port index (%u)",
1270 				event.event_type, tmp);
1271 			continue;
1272 		}
1273 		if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1274 			/* No handler installed. */
1275 			mlx5_glue->ack_async_event(&event);
1276 			DRV_LOG(DEBUG,
1277 				"cannot handle an event (type %d)"
1278 				"due to no handler installed for port %u",
1279 				event.event_type, tmp);
1280 			continue;
1281 		}
1282 		/* Retrieve ethernet device descriptor. */
1283 		tmp = sh->port[tmp - 1].ih_port_id;
1284 		dev = &rte_eth_devices[tmp];
1285 		assert(dev);
1286 		if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1287 		     event.event_type == IBV_EVENT_PORT_ERR) &&
1288 			dev->data->dev_conf.intr_conf.lsc) {
1289 			mlx5_glue->ack_async_event(&event);
1290 			if (mlx5_link_update(dev, 0) == -EAGAIN) {
1291 				usleep(0);
1292 				continue;
1293 			}
1294 			_rte_eth_dev_callback_process
1295 				(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1296 			continue;
1297 		}
1298 		DRV_LOG(DEBUG,
1299 			"port %u cannot handle an unknown event (type %d)",
1300 			dev->data->port_id, event.event_type);
1301 		mlx5_glue->ack_async_event(&event);
1302 	}
1303 }
1304 
1305 /*
1306  * Unregister callback handler safely. The handler may be active
1307  * while we are trying to unregister it, in this case code -EAGAIN
1308  * is returned by rte_intr_callback_unregister(). This routine checks
1309  * the return code and tries to unregister handler again.
1310  *
1311  * @param handle
1312  *   interrupt handle
1313  * @param cb_fn
1314  *   pointer to callback routine
1315  * @cb_arg
1316  *   opaque callback parameter
1317  */
1318 void
1319 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1320 			      rte_intr_callback_fn cb_fn, void *cb_arg)
1321 {
1322 	/*
1323 	 * Try to reduce timeout management overhead by not calling
1324 	 * the timer related routines on the first iteration. If the
1325 	 * unregistering succeeds on first call there will be no
1326 	 * timer calls at all.
1327 	 */
1328 	uint64_t twait = 0;
1329 	uint64_t start = 0;
1330 
1331 	do {
1332 		int ret;
1333 
1334 		ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1335 		if (ret >= 0)
1336 			return;
1337 		if (ret != -EAGAIN) {
1338 			DRV_LOG(INFO, "failed to unregister interrupt"
1339 				      " handler (error: %d)", ret);
1340 			assert(false);
1341 			return;
1342 		}
1343 		if (twait) {
1344 			struct timespec onems;
1345 
1346 			/* Wait one millisecond and try again. */
1347 			onems.tv_sec = 0;
1348 			onems.tv_nsec = NS_PER_S / MS_PER_S;
1349 			nanosleep(&onems, 0);
1350 			/* Check whether one second elapsed. */
1351 			if ((rte_get_timer_cycles() - start) <= twait)
1352 				continue;
1353 		} else {
1354 			/*
1355 			 * We get the amount of timer ticks for one second.
1356 			 * If this amount elapsed it means we spent one
1357 			 * second in waiting. This branch is executed once
1358 			 * on first iteration.
1359 			 */
1360 			twait = rte_get_timer_hz();
1361 			assert(twait);
1362 		}
1363 		/*
1364 		 * Timeout elapsed, show message (once a second) and retry.
1365 		 * We have no other acceptable option here, if we ignore
1366 		 * the unregistering return code the handler will not
1367 		 * be unregistered, fd will be closed and we may get the
1368 		 * crush. Hanging and messaging in the loop seems not to be
1369 		 * the worst choice.
1370 		 */
1371 		DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1372 		start = rte_get_timer_cycles();
1373 	} while (true);
1374 }
1375 
1376 /**
1377  * Handle DEVX interrupts from the NIC.
1378  * This function is probably called from the DPDK host thread.
1379  *
1380  * @param cb_arg
1381  *   Callback argument.
1382  */
1383 void
1384 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1385 {
1386 #ifndef HAVE_IBV_DEVX_ASYNC
1387 	(void)cb_arg;
1388 	return;
1389 #else
1390 	struct mlx5_ibv_shared *sh = cb_arg;
1391 	union {
1392 		struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1393 		uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1394 			    MLX5_ST_SZ_BYTES(traffic_counter) +
1395 			    sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1396 	} out;
1397 	uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1398 
1399 	while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1400 						   &out.cmd_resp,
1401 						   sizeof(out.buf)))
1402 		mlx5_flow_async_pool_query_handle
1403 			(sh, (uint64_t)out.cmd_resp.wr_id,
1404 			 mlx5_devx_get_out_command_status(buf));
1405 #endif /* HAVE_IBV_DEVX_ASYNC */
1406 }
1407 
1408 /**
1409  * Uninstall shared asynchronous device events handler.
1410  * This function is implemented to support event sharing
1411  * between multiple ports of single IB device.
1412  *
1413  * @param dev
1414  *   Pointer to Ethernet device.
1415  */
1416 static void
1417 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1418 {
1419 	struct mlx5_priv *priv = dev->data->dev_private;
1420 	struct mlx5_ibv_shared *sh = priv->sh;
1421 
1422 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1423 		return;
1424 	pthread_mutex_lock(&sh->intr_mutex);
1425 	assert(priv->ibv_port);
1426 	assert(priv->ibv_port <= sh->max_port);
1427 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1428 	if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1429 		goto exit;
1430 	assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1431 					(uint32_t)dev->data->port_id);
1432 	assert(sh->intr_cnt);
1433 	sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1434 	if (!sh->intr_cnt || --sh->intr_cnt)
1435 		goto exit;
1436 	mlx5_intr_callback_unregister(&sh->intr_handle,
1437 				     mlx5_dev_interrupt_handler, sh);
1438 	sh->intr_handle.fd = 0;
1439 	sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1440 	if (sh->intr_handle_devx.fd) {
1441 		rte_intr_callback_unregister(&sh->intr_handle_devx,
1442 					     mlx5_dev_interrupt_handler_devx,
1443 					     sh);
1444 		sh->intr_handle_devx.fd = 0;
1445 		sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1446 	}
1447 	if (sh->devx_comp) {
1448 		mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1449 		sh->devx_comp = NULL;
1450 	}
1451 exit:
1452 	pthread_mutex_unlock(&sh->intr_mutex);
1453 }
1454 
1455 /**
1456  * Install shared asynchronous device events handler.
1457  * This function is implemented to support event sharing
1458  * between multiple ports of single IB device.
1459  *
1460  * @param dev
1461  *   Pointer to Ethernet device.
1462  */
1463 static void
1464 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1465 {
1466 	struct mlx5_priv *priv = dev->data->dev_private;
1467 	struct mlx5_ibv_shared *sh = priv->sh;
1468 	int ret;
1469 	int flags;
1470 
1471 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1472 		return;
1473 	pthread_mutex_lock(&sh->intr_mutex);
1474 	assert(priv->ibv_port);
1475 	assert(priv->ibv_port <= sh->max_port);
1476 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1477 	if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1478 		/* The handler is already installed for this port. */
1479 		assert(sh->intr_cnt);
1480 		goto exit;
1481 	}
1482 	sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1483 	if (sh->intr_cnt) {
1484 		sh->intr_cnt++;
1485 		goto exit;
1486 	}
1487 	/* No shared handler installed. */
1488 	assert(sh->ctx->async_fd > 0);
1489 	flags = fcntl(sh->ctx->async_fd, F_GETFL);
1490 	ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1491 	if (ret) {
1492 		DRV_LOG(INFO, "failed to change file descriptor"
1493 			      " async event queue");
1494 		goto error;
1495 	}
1496 	sh->intr_handle.fd = sh->ctx->async_fd;
1497 	sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1498 	rte_intr_callback_register(&sh->intr_handle,
1499 				   mlx5_dev_interrupt_handler, sh);
1500 	if (priv->config.devx) {
1501 #ifndef HAVE_IBV_DEVX_ASYNC
1502 		goto error_unregister;
1503 #else
1504 		sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1505 		if (sh->devx_comp) {
1506 			flags = fcntl(sh->devx_comp->fd, F_GETFL);
1507 			ret = fcntl(sh->devx_comp->fd, F_SETFL,
1508 				    flags | O_NONBLOCK);
1509 			if (ret) {
1510 				DRV_LOG(INFO, "failed to change file descriptor"
1511 					      " devx async event queue");
1512 				goto error_unregister;
1513 			}
1514 			sh->intr_handle_devx.fd = sh->devx_comp->fd;
1515 			sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1516 			rte_intr_callback_register
1517 				(&sh->intr_handle_devx,
1518 				 mlx5_dev_interrupt_handler_devx, sh);
1519 		} else {
1520 			DRV_LOG(INFO, "failed to create devx async command "
1521 				"completion");
1522 			goto error_unregister;
1523 		}
1524 #endif /* HAVE_IBV_DEVX_ASYNC */
1525 	}
1526 	sh->intr_cnt++;
1527 	goto exit;
1528 error_unregister:
1529 	rte_intr_callback_unregister(&sh->intr_handle,
1530 				     mlx5_dev_interrupt_handler, sh);
1531 error:
1532 	/* Indicate there will be no interrupts. */
1533 	dev->data->dev_conf.intr_conf.lsc = 0;
1534 	dev->data->dev_conf.intr_conf.rmv = 0;
1535 	sh->intr_handle.fd = 0;
1536 	sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1537 	sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1538 exit:
1539 	pthread_mutex_unlock(&sh->intr_mutex);
1540 }
1541 
1542 /**
1543  * Uninstall interrupt handler.
1544  *
1545  * @param dev
1546  *   Pointer to Ethernet device.
1547  */
1548 void
1549 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1550 {
1551 	mlx5_dev_shared_handler_uninstall(dev);
1552 }
1553 
1554 /**
1555  * Install interrupt handler.
1556  *
1557  * @param dev
1558  *   Pointer to Ethernet device.
1559  */
1560 void
1561 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1562 {
1563 	mlx5_dev_shared_handler_install(dev);
1564 }
1565 
1566 /**
1567  * DPDK callback to bring the link DOWN.
1568  *
1569  * @param dev
1570  *   Pointer to Ethernet device structure.
1571  *
1572  * @return
1573  *   0 on success, a negative errno value otherwise and rte_errno is set.
1574  */
1575 int
1576 mlx5_set_link_down(struct rte_eth_dev *dev)
1577 {
1578 	return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1579 }
1580 
1581 /**
1582  * DPDK callback to bring the link UP.
1583  *
1584  * @param dev
1585  *   Pointer to Ethernet device structure.
1586  *
1587  * @return
1588  *   0 on success, a negative errno value otherwise and rte_errno is set.
1589  */
1590 int
1591 mlx5_set_link_up(struct rte_eth_dev *dev)
1592 {
1593 	return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1594 }
1595 
1596 /**
1597  * Configure the RX function to use.
1598  *
1599  * @param dev
1600  *   Pointer to private data structure.
1601  *
1602  * @return
1603  *   Pointer to selected Rx burst function.
1604  */
1605 eth_rx_burst_t
1606 mlx5_select_rx_function(struct rte_eth_dev *dev)
1607 {
1608 	eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1609 
1610 	assert(dev != NULL);
1611 	if (mlx5_check_vec_rx_support(dev) > 0) {
1612 		rx_pkt_burst = mlx5_rx_burst_vec;
1613 		DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1614 			dev->data->port_id);
1615 	} else if (mlx5_mprq_enabled(dev)) {
1616 		rx_pkt_burst = mlx5_rx_burst_mprq;
1617 	}
1618 	return rx_pkt_burst;
1619 }
1620 
1621 /**
1622  * Check if mlx5 device was removed.
1623  *
1624  * @param dev
1625  *   Pointer to Ethernet device structure.
1626  *
1627  * @return
1628  *   1 when device is removed, otherwise 0.
1629  */
1630 int
1631 mlx5_is_removed(struct rte_eth_dev *dev)
1632 {
1633 	struct ibv_device_attr device_attr;
1634 	struct mlx5_priv *priv = dev->data->dev_private;
1635 
1636 	if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1637 		return 1;
1638 	return 0;
1639 }
1640 
1641 /**
1642  * Get port ID list of mlx5 instances sharing a common device.
1643  *
1644  * @param[in] dev
1645  *   Device to look for.
1646  * @param[out] port_list
1647  *   Result buffer for collected port IDs.
1648  * @param port_list_n
1649  *   Maximum number of entries in result buffer. If 0, @p port_list can be
1650  *   NULL.
1651  *
1652  * @return
1653  *   Number of matching instances regardless of the @p port_list_n
1654  *   parameter, 0 if none were found.
1655  */
1656 unsigned int
1657 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1658 		    unsigned int port_list_n)
1659 {
1660 	uint16_t id;
1661 	unsigned int n = 0;
1662 
1663 	RTE_ETH_FOREACH_DEV_OF(id, dev) {
1664 		if (n < port_list_n)
1665 			port_list[n] = id;
1666 		n++;
1667 	}
1668 	return n;
1669 }
1670 
1671 /**
1672  * Get the E-Switch domain id this port belongs to.
1673  *
1674  * @param[in] port
1675  *   Device port id.
1676  * @param[out] es_domain_id
1677  *   E-Switch domain id.
1678  * @param[out] es_port_id
1679  *   The port id of the port in the E-Switch.
1680  *
1681  * @return
1682  *   0 on success, a negative errno value otherwise and rte_errno is set.
1683  */
1684 int
1685 mlx5_port_to_eswitch_info(uint16_t port,
1686 			  uint16_t *es_domain_id, uint16_t *es_port_id)
1687 {
1688 	struct rte_eth_dev *dev;
1689 	struct mlx5_priv *priv;
1690 
1691 	if (port >= RTE_MAX_ETHPORTS) {
1692 		rte_errno = EINVAL;
1693 		return -rte_errno;
1694 	}
1695 	if (!rte_eth_dev_is_valid_port(port)) {
1696 		rte_errno = ENODEV;
1697 		return -rte_errno;
1698 	}
1699 	dev = &rte_eth_devices[port];
1700 	priv = dev->data->dev_private;
1701 	if (!(priv->representor || priv->master)) {
1702 		rte_errno = EINVAL;
1703 		return -rte_errno;
1704 	}
1705 	if (es_domain_id)
1706 		*es_domain_id = priv->domain_id;
1707 	if (es_port_id)
1708 		*es_port_id = priv->vport_id;
1709 	return 0;
1710 }
1711 
1712 /**
1713  * Get switch information associated with network interface.
1714  *
1715  * @param ifindex
1716  *   Network interface index.
1717  * @param[out] info
1718  *   Switch information object, populated in case of success.
1719  *
1720  * @return
1721  *   0 on success, a negative errno value otherwise and rte_errno is set.
1722  */
1723 int
1724 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1725 {
1726 	char ifname[IF_NAMESIZE];
1727 	char port_name[IF_NAMESIZE];
1728 	FILE *file;
1729 	struct mlx5_switch_info data = {
1730 		.master = 0,
1731 		.representor = 0,
1732 		.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1733 		.port_name = 0,
1734 		.switch_id = 0,
1735 	};
1736 	DIR *dir;
1737 	bool port_switch_id_set = false;
1738 	bool device_dir = false;
1739 	char c;
1740 	int ret;
1741 
1742 	if (!if_indextoname(ifindex, ifname)) {
1743 		rte_errno = errno;
1744 		return -rte_errno;
1745 	}
1746 
1747 	MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1748 	      ifname);
1749 	MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1750 	      ifname);
1751 	MKSTR(pci_device, "/sys/class/net/%s/device",
1752 	      ifname);
1753 
1754 	file = fopen(phys_port_name, "rb");
1755 	if (file != NULL) {
1756 		ret = fscanf(file, "%s", port_name);
1757 		fclose(file);
1758 		if (ret == 1)
1759 			mlx5_translate_port_name(port_name, &data);
1760 	}
1761 	file = fopen(phys_switch_id, "rb");
1762 	if (file == NULL) {
1763 		rte_errno = errno;
1764 		return -rte_errno;
1765 	}
1766 	port_switch_id_set =
1767 		fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1768 		c == '\n';
1769 	fclose(file);
1770 	dir = opendir(pci_device);
1771 	if (dir != NULL) {
1772 		closedir(dir);
1773 		device_dir = true;
1774 	}
1775 	if (port_switch_id_set) {
1776 		/* We have some E-Switch configuration. */
1777 		mlx5_sysfs_check_switch_info(device_dir, &data);
1778 	}
1779 	*info = data;
1780 	assert(!(data.master && data.representor));
1781 	if (data.master && data.representor) {
1782 		DRV_LOG(ERR, "ifindex %u device is recognized as master"
1783 			     " and as representor", ifindex);
1784 		rte_errno = ENODEV;
1785 		return -rte_errno;
1786 	}
1787 	return 0;
1788 }
1789 
1790 /**
1791  * Analyze gathered port parameters via Netlink to recognize master
1792  * and representor devices for E-Switch configuration.
1793  *
1794  * @param[in] num_vf_set
1795  *   flag of presence of number of VFs port attribute.
1796  * @param[inout] switch_info
1797  *   Port information, including port name as a number and port name
1798  *   type if recognized
1799  *
1800  * @return
1801  *   master and representor flags are set in switch_info according to
1802  *   recognized parameters (if any).
1803  */
1804 void
1805 mlx5_nl_check_switch_info(bool num_vf_set,
1806 			  struct mlx5_switch_info *switch_info)
1807 {
1808 	switch (switch_info->name_type) {
1809 	case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1810 		/*
1811 		 * Name is not recognized, assume the master,
1812 		 * check the number of VFs key presence.
1813 		 */
1814 		switch_info->master = num_vf_set;
1815 		break;
1816 	case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1817 		/*
1818 		 * Name is not set, this assumes the legacy naming
1819 		 * schema for master, just check if there is a
1820 		 * number of VFs key.
1821 		 */
1822 		switch_info->master = num_vf_set;
1823 		break;
1824 	case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1825 		/* New uplink naming schema recognized. */
1826 		switch_info->master = 1;
1827 		break;
1828 	case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1829 		/* Legacy representors naming schema. */
1830 		switch_info->representor = !num_vf_set;
1831 		break;
1832 	case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1833 		/* New representors naming schema. */
1834 		switch_info->representor = 1;
1835 		break;
1836 	}
1837 }
1838 
1839 /**
1840  * Analyze gathered port parameters via sysfs to recognize master
1841  * and representor devices for E-Switch configuration.
1842  *
1843  * @param[in] device_dir
1844  *   flag of presence of "device" directory under port device key.
1845  * @param[inout] switch_info
1846  *   Port information, including port name as a number and port name
1847  *   type if recognized
1848  *
1849  * @return
1850  *   master and representor flags are set in switch_info according to
1851  *   recognized parameters (if any).
1852  */
1853 void
1854 mlx5_sysfs_check_switch_info(bool device_dir,
1855 			     struct mlx5_switch_info *switch_info)
1856 {
1857 	switch (switch_info->name_type) {
1858 	case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1859 		/*
1860 		 * Name is not recognized, assume the master,
1861 		 * check the device directory presence.
1862 		 */
1863 		switch_info->master = device_dir;
1864 		break;
1865 	case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1866 		/*
1867 		 * Name is not set, this assumes the legacy naming
1868 		 * schema for master, just check if there is
1869 		 * a device directory.
1870 		 */
1871 		switch_info->master = device_dir;
1872 		break;
1873 	case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1874 		/* New uplink naming schema recognized. */
1875 		switch_info->master = 1;
1876 		break;
1877 	case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1878 		/* Legacy representors naming schema. */
1879 		switch_info->representor = !device_dir;
1880 		break;
1881 	case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1882 		/* New representors naming schema. */
1883 		switch_info->representor = 1;
1884 		break;
1885 	}
1886 }
1887 
1888 /**
1889  * Extract port name, as a number, from sysfs or netlink information.
1890  *
1891  * @param[in] port_name_in
1892  *   String representing the port name.
1893  * @param[out] port_info_out
1894  *   Port information, including port name as a number and port name
1895  *   type if recognized
1896  *
1897  * @return
1898  *   port_name field set according to recognized name format.
1899  */
1900 void
1901 mlx5_translate_port_name(const char *port_name_in,
1902 			 struct mlx5_switch_info *port_info_out)
1903 {
1904 	char pf_c1, pf_c2, vf_c1, vf_c2;
1905 	char *end;
1906 	int sc_items;
1907 
1908 	/*
1909 	 * Check for port-name as a string of the form pf0vf0
1910 	 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1911 	 */
1912 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1913 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
1914 			  &vf_c1, &vf_c2, &port_info_out->port_name);
1915 	if (sc_items == 6 &&
1916 	    pf_c1 == 'p' && pf_c2 == 'f' &&
1917 	    vf_c1 == 'v' && vf_c2 == 'f') {
1918 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1919 		return;
1920 	}
1921 	/*
1922 	 * Check for port-name as a string of the form p0
1923 	 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1924 	 */
1925 	sc_items = sscanf(port_name_in, "%c%d",
1926 			  &pf_c1, &port_info_out->port_name);
1927 	if (sc_items == 2 && pf_c1 == 'p') {
1928 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1929 		return;
1930 	}
1931 	/* Check for port-name as a number (support kernel ver < 5.0 */
1932 	errno = 0;
1933 	port_info_out->port_name = strtol(port_name_in, &end, 0);
1934 	if (!errno &&
1935 	    (size_t)(end - port_name_in) == strlen(port_name_in)) {
1936 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1937 		return;
1938 	}
1939 	port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1940 	return;
1941 }
1942