xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision 63bd16292c3a519029a1b451e734b8f4489c2fd3)
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 	const uint8_t use_app_rss_key =
387 		!!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
388 	int ret = 0;
389 
390 	if (use_app_rss_key &&
391 	    (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
392 	     MLX5_RSS_HASH_KEY_LEN)) {
393 		DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
394 			dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
395 		rte_errno = EINVAL;
396 		return -rte_errno;
397 	}
398 	priv->rss_conf.rss_key =
399 		rte_realloc(priv->rss_conf.rss_key,
400 			    MLX5_RSS_HASH_KEY_LEN, 0);
401 	if (!priv->rss_conf.rss_key) {
402 		DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
403 			dev->data->port_id, rxqs_n);
404 		rte_errno = ENOMEM;
405 		return -rte_errno;
406 	}
407 	memcpy(priv->rss_conf.rss_key,
408 	       use_app_rss_key ?
409 	       dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
410 	       rss_hash_default_key,
411 	       MLX5_RSS_HASH_KEY_LEN);
412 	priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
413 	priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
414 	priv->rxqs = (void *)dev->data->rx_queues;
415 	priv->txqs = (void *)dev->data->tx_queues;
416 	if (txqs_n != priv->txqs_n) {
417 		DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
418 			dev->data->port_id, priv->txqs_n, txqs_n);
419 		priv->txqs_n = txqs_n;
420 	}
421 	if (rxqs_n > priv->config.ind_table_max_size) {
422 		DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
423 			dev->data->port_id, rxqs_n);
424 		rte_errno = EINVAL;
425 		return -rte_errno;
426 	}
427 	if (rxqs_n != priv->rxqs_n) {
428 		DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
429 			dev->data->port_id, priv->rxqs_n, rxqs_n);
430 		priv->rxqs_n = rxqs_n;
431 	}
432 	priv->skip_default_rss_reta = 0;
433 	ret = mlx5_proc_priv_init(dev);
434 	if (ret)
435 		return ret;
436 	return 0;
437 }
438 
439 /**
440  * Configure default RSS reta.
441  *
442  * @param dev
443  *   Pointer to Ethernet device structure.
444  *
445  * @return
446  *   0 on success, a negative errno value otherwise and rte_errno is set.
447  */
448 int
449 mlx5_dev_configure_rss_reta(struct rte_eth_dev *dev)
450 {
451 	struct mlx5_priv *priv = dev->data->dev_private;
452 	unsigned int rxqs_n = dev->data->nb_rx_queues;
453 	unsigned int i;
454 	unsigned int j;
455 	unsigned int reta_idx_n;
456 	int ret = 0;
457 	unsigned int *rss_queue_arr = NULL;
458 	unsigned int rss_queue_n = 0;
459 
460 	if (priv->skip_default_rss_reta)
461 		return ret;
462 	rss_queue_arr = rte_malloc("", rxqs_n * sizeof(unsigned int), 0);
463 	if (!rss_queue_arr) {
464 		DRV_LOG(ERR, "port %u cannot allocate RSS queue list (%u)",
465 			dev->data->port_id, rxqs_n);
466 		rte_errno = ENOMEM;
467 		return -rte_errno;
468 	}
469 	for (i = 0, j = 0; i < rxqs_n; i++) {
470 		struct mlx5_rxq_data *rxq_data;
471 		struct mlx5_rxq_ctrl *rxq_ctrl;
472 
473 		rxq_data = (*priv->rxqs)[i];
474 		rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
475 		if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
476 			rss_queue_arr[j++] = i;
477 	}
478 	rss_queue_n = j;
479 	if (rss_queue_n > priv->config.ind_table_max_size) {
480 		DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
481 			dev->data->port_id, rss_queue_n);
482 		rte_errno = EINVAL;
483 		rte_free(rss_queue_arr);
484 		return -rte_errno;
485 	}
486 	DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
487 		dev->data->port_id, priv->rxqs_n, rxqs_n);
488 	priv->rxqs_n = rxqs_n;
489 	/*
490 	 * If the requested number of RX queues is not a power of two,
491 	 * use the maximum indirection table size for better balancing.
492 	 * The result is always rounded to the next power of two.
493 	 */
494 	reta_idx_n = (1 << log2above((rss_queue_n & (rss_queue_n - 1)) ?
495 				priv->config.ind_table_max_size :
496 				rss_queue_n));
497 	ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
498 	if (ret) {
499 		rte_free(rss_queue_arr);
500 		return ret;
501 	}
502 	/*
503 	 * When the number of RX queues is not a power of two,
504 	 * the remaining table entries are padded with reused WQs
505 	 * and hashes are not spread uniformly.
506 	 */
507 	for (i = 0, j = 0; (i != reta_idx_n); ++i) {
508 		(*priv->reta_idx)[i] = rss_queue_arr[j];
509 		if (++j == rss_queue_n)
510 			j = 0;
511 	}
512 	rte_free(rss_queue_arr);
513 	return ret;
514 }
515 
516 /**
517  * Sets default tuning parameters.
518  *
519  * @param dev
520  *   Pointer to Ethernet device.
521  * @param[out] info
522  *   Info structure output buffer.
523  */
524 static void
525 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
526 {
527 	struct mlx5_priv *priv = dev->data->dev_private;
528 
529 	/* Minimum CPU utilization. */
530 	info->default_rxportconf.ring_size = 256;
531 	info->default_txportconf.ring_size = 256;
532 	info->default_rxportconf.burst_size = 64;
533 	info->default_txportconf.burst_size = 64;
534 	if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
535 		info->default_rxportconf.nb_queues = 16;
536 		info->default_txportconf.nb_queues = 16;
537 		if (dev->data->nb_rx_queues > 2 ||
538 		    dev->data->nb_tx_queues > 2) {
539 			/* Max Throughput. */
540 			info->default_rxportconf.ring_size = 2048;
541 			info->default_txportconf.ring_size = 2048;
542 		}
543 	} else {
544 		info->default_rxportconf.nb_queues = 8;
545 		info->default_txportconf.nb_queues = 8;
546 		if (dev->data->nb_rx_queues > 2 ||
547 		    dev->data->nb_tx_queues > 2) {
548 			/* Max Throughput. */
549 			info->default_rxportconf.ring_size = 4096;
550 			info->default_txportconf.ring_size = 4096;
551 		}
552 	}
553 }
554 
555 /**
556  * Sets tx mbuf limiting parameters.
557  *
558  * @param dev
559  *   Pointer to Ethernet device.
560  * @param[out] info
561  *   Info structure output buffer.
562  */
563 static void
564 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
565 {
566 	struct mlx5_priv *priv = dev->data->dev_private;
567 	struct mlx5_dev_config *config = &priv->config;
568 	unsigned int inlen;
569 	uint16_t nb_max;
570 
571 	inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
572 		MLX5_SEND_DEF_INLINE_LEN :
573 		(unsigned int)config->txq_inline_max;
574 	assert(config->txq_inline_min >= 0);
575 	inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
576 	inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
577 			       MLX5_ESEG_MIN_INLINE_SIZE -
578 			       MLX5_WQE_CSEG_SIZE -
579 			       MLX5_WQE_ESEG_SIZE -
580 			       MLX5_WQE_DSEG_SIZE * 2);
581 	nb_max = (MLX5_WQE_SIZE_MAX +
582 		  MLX5_ESEG_MIN_INLINE_SIZE -
583 		  MLX5_WQE_CSEG_SIZE -
584 		  MLX5_WQE_ESEG_SIZE -
585 		  MLX5_WQE_DSEG_SIZE -
586 		  inlen) / MLX5_WSEG_SIZE;
587 	info->tx_desc_lim.nb_seg_max = nb_max;
588 	info->tx_desc_lim.nb_mtu_seg_max = nb_max;
589 }
590 
591 /**
592  * DPDK callback to get information about the device.
593  *
594  * @param dev
595  *   Pointer to Ethernet device structure.
596  * @param[out] info
597  *   Info structure output buffer.
598  */
599 int
600 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
601 {
602 	struct mlx5_priv *priv = dev->data->dev_private;
603 	struct mlx5_dev_config *config = &priv->config;
604 	unsigned int max;
605 
606 	/* FIXME: we should ask the device for these values. */
607 	info->min_rx_bufsize = 32;
608 	info->max_rx_pktlen = 65536;
609 	/*
610 	 * Since we need one CQ per QP, the limit is the minimum number
611 	 * between the two values.
612 	 */
613 	max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
614 		      priv->sh->device_attr.orig_attr.max_qp);
615 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
616 	if (max >= 65535)
617 		max = 65535;
618 	info->max_rx_queues = max;
619 	info->max_tx_queues = max;
620 	info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
621 	info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
622 	info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
623 				 info->rx_queue_offload_capa);
624 	info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
625 	info->if_index = mlx5_ifindex(dev);
626 	info->reta_size = priv->reta_idx_n ?
627 		priv->reta_idx_n : config->ind_table_max_size;
628 	info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
629 	info->speed_capa = priv->link_speed_capa;
630 	info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
631 	mlx5_set_default_params(dev, info);
632 	mlx5_set_txlimit_params(dev, info);
633 	info->switch_info.name = dev->data->name;
634 	info->switch_info.domain_id = priv->domain_id;
635 	info->switch_info.port_id = priv->representor_id;
636 	if (priv->representor) {
637 		uint16_t port_id;
638 
639 		if (priv->pf_bond >= 0) {
640 			/*
641 			 * Switch port ID is opaque value with driver defined
642 			 * format. Push the PF index in bonding configurations
643 			 * in upper four bits of port ID. If we get too many
644 			 * representors (more than 4K) or PFs (more than 15)
645 			 * this approach must be reconsidered.
646 			 */
647 			if ((info->switch_info.port_id >>
648 				MLX5_PORT_ID_BONDING_PF_SHIFT) ||
649 			    priv->pf_bond > MLX5_PORT_ID_BONDING_PF_MASK) {
650 				DRV_LOG(ERR, "can't update switch port ID"
651 					     " for bonding device");
652 				assert(false);
653 				return -ENODEV;
654 			}
655 			info->switch_info.port_id |=
656 				priv->pf_bond << MLX5_PORT_ID_BONDING_PF_SHIFT;
657 		}
658 		MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
659 			struct mlx5_priv *opriv =
660 				rte_eth_devices[port_id].data->dev_private;
661 
662 			if (!opriv ||
663 			    opriv->representor ||
664 			    opriv->sh != priv->sh ||
665 			    opriv->domain_id != priv->domain_id)
666 				continue;
667 			/*
668 			 * Override switch name with that of the master
669 			 * device.
670 			 */
671 			info->switch_info.name = opriv->dev_data->name;
672 			break;
673 		}
674 	}
675 	return 0;
676 }
677 
678 /**
679  * Get device current raw clock counter
680  *
681  * @param dev
682  *   Pointer to Ethernet device structure.
683  * @param[out] time
684  *   Current raw clock counter of the device.
685  *
686  * @return
687  *   0 if the clock has correctly been read
688  *   The value of errno in case of error
689  */
690 int
691 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
692 {
693 	struct mlx5_priv *priv = dev->data->dev_private;
694 	struct ibv_context *ctx = priv->sh->ctx;
695 	struct ibv_values_ex values;
696 	int err = 0;
697 
698 	values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
699 	err = mlx5_glue->query_rt_values_ex(ctx, &values);
700 	if (err != 0) {
701 		DRV_LOG(WARNING, "Could not query the clock !");
702 		return err;
703 	}
704 	*clock = values.raw_clock.tv_nsec;
705 	return 0;
706 }
707 
708 /**
709  * Get firmware version of a device.
710  *
711  * @param dev
712  *   Ethernet device port.
713  * @param fw_ver
714  *   String output allocated by caller.
715  * @param fw_size
716  *   Size of the output string, including terminating null byte.
717  *
718  * @return
719  *   0 on success, or the size of the non truncated string if too big.
720  */
721 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
722 {
723 	struct mlx5_priv *priv = dev->data->dev_private;
724 	struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
725 	size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
726 
727 	if (fw_size < size)
728 		return size;
729 	if (fw_ver != NULL)
730 		strlcpy(fw_ver, attr->fw_ver, fw_size);
731 	return 0;
732 }
733 
734 /**
735  * Get supported packet types.
736  *
737  * @param dev
738  *   Pointer to Ethernet device structure.
739  *
740  * @return
741  *   A pointer to the supported Packet types array.
742  */
743 const uint32_t *
744 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
745 {
746 	static const uint32_t ptypes[] = {
747 		/* refers to rxq_cq_to_pkt_type() */
748 		RTE_PTYPE_L2_ETHER,
749 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
750 		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
751 		RTE_PTYPE_L4_NONFRAG,
752 		RTE_PTYPE_L4_FRAG,
753 		RTE_PTYPE_L4_TCP,
754 		RTE_PTYPE_L4_UDP,
755 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
756 		RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
757 		RTE_PTYPE_INNER_L4_NONFRAG,
758 		RTE_PTYPE_INNER_L4_FRAG,
759 		RTE_PTYPE_INNER_L4_TCP,
760 		RTE_PTYPE_INNER_L4_UDP,
761 		RTE_PTYPE_UNKNOWN
762 	};
763 
764 	if (dev->rx_pkt_burst == mlx5_rx_burst ||
765 	    dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
766 	    dev->rx_pkt_burst == mlx5_rx_burst_vec)
767 		return ptypes;
768 	return NULL;
769 }
770 
771 /**
772  * Retrieve the master device for representor in the same switch domain.
773  *
774  * @param dev
775  *   Pointer to representor Ethernet device structure.
776  *
777  * @return
778  *   Master device structure  on success, NULL otherwise.
779  */
780 
781 static struct rte_eth_dev *
782 mlx5_find_master_dev(struct rte_eth_dev *dev)
783 {
784 	struct mlx5_priv *priv;
785 	uint16_t port_id;
786 	uint16_t domain_id;
787 
788 	priv = dev->data->dev_private;
789 	domain_id = priv->domain_id;
790 	assert(priv->representor);
791 	MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
792 		struct mlx5_priv *opriv =
793 			rte_eth_devices[port_id].data->dev_private;
794 		if (opriv &&
795 		    opriv->master &&
796 		    opriv->domain_id == domain_id &&
797 		    opriv->sh == priv->sh)
798 			return &rte_eth_devices[port_id];
799 	}
800 	return NULL;
801 }
802 
803 /**
804  * DPDK callback to retrieve physical link information.
805  *
806  * @param dev
807  *   Pointer to Ethernet device structure.
808  * @param[out] link
809  *   Storage for current link status.
810  *
811  * @return
812  *   0 on success, a negative errno value otherwise and rte_errno is set.
813  */
814 static int
815 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
816 			       struct rte_eth_link *link)
817 {
818 	struct mlx5_priv *priv = dev->data->dev_private;
819 	struct ethtool_cmd edata = {
820 		.cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
821 	};
822 	struct ifreq ifr;
823 	struct rte_eth_link dev_link;
824 	int link_speed = 0;
825 	int ret;
826 
827 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
828 	if (ret) {
829 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
830 			dev->data->port_id, strerror(rte_errno));
831 		return ret;
832 	}
833 	dev_link = (struct rte_eth_link) {
834 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
835 				(ifr.ifr_flags & IFF_RUNNING)),
836 	};
837 	ifr = (struct ifreq) {
838 		.ifr_data = (void *)&edata,
839 	};
840 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
841 	if (ret) {
842 		if (ret == -ENOTSUP && priv->representor) {
843 			struct rte_eth_dev *master;
844 
845 			/*
846 			 * For representors we can try to inherit link
847 			 * settings from the master device. Actually
848 			 * link settings do not make a lot of sense
849 			 * for representors due to missing physical
850 			 * link. The old kernel drivers supported
851 			 * emulated settings query for representors,
852 			 * the new ones do not, so we have to add
853 			 * this code for compatibility issues.
854 			 */
855 			master = mlx5_find_master_dev(dev);
856 			if (master) {
857 				ifr = (struct ifreq) {
858 					.ifr_data = (void *)&edata,
859 				};
860 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
861 			}
862 		}
863 		if (ret) {
864 			DRV_LOG(WARNING,
865 				"port %u ioctl(SIOCETHTOOL,"
866 				" ETHTOOL_GSET) failed: %s",
867 				dev->data->port_id, strerror(rte_errno));
868 			return ret;
869 		}
870 	}
871 	link_speed = ethtool_cmd_speed(&edata);
872 	if (link_speed == -1)
873 		dev_link.link_speed = ETH_SPEED_NUM_NONE;
874 	else
875 		dev_link.link_speed = link_speed;
876 	priv->link_speed_capa = 0;
877 	if (edata.supported & SUPPORTED_Autoneg)
878 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
879 	if (edata.supported & (SUPPORTED_1000baseT_Full |
880 			       SUPPORTED_1000baseKX_Full))
881 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
882 	if (edata.supported & SUPPORTED_10000baseKR_Full)
883 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
884 	if (edata.supported & (SUPPORTED_40000baseKR4_Full |
885 			       SUPPORTED_40000baseCR4_Full |
886 			       SUPPORTED_40000baseSR4_Full |
887 			       SUPPORTED_40000baseLR4_Full))
888 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
889 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
890 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
891 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
892 			ETH_LINK_SPEED_FIXED);
893 	if (((dev_link.link_speed && !dev_link.link_status) ||
894 	     (!dev_link.link_speed && dev_link.link_status))) {
895 		rte_errno = EAGAIN;
896 		return -rte_errno;
897 	}
898 	*link = dev_link;
899 	return 0;
900 }
901 
902 /**
903  * Retrieve physical link information (unlocked version using new ioctl).
904  *
905  * @param dev
906  *   Pointer to Ethernet device structure.
907  * @param[out] link
908  *   Storage for current link status.
909  *
910  * @return
911  *   0 on success, a negative errno value otherwise and rte_errno is set.
912  */
913 static int
914 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
915 			     struct rte_eth_link *link)
916 
917 {
918 	struct mlx5_priv *priv = dev->data->dev_private;
919 	struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
920 	struct ifreq ifr;
921 	struct rte_eth_link dev_link;
922 	struct rte_eth_dev *master = NULL;
923 	uint64_t sc;
924 	int ret;
925 
926 	ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
927 	if (ret) {
928 		DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
929 			dev->data->port_id, strerror(rte_errno));
930 		return ret;
931 	}
932 	dev_link = (struct rte_eth_link) {
933 		.link_status = ((ifr.ifr_flags & IFF_UP) &&
934 				(ifr.ifr_flags & IFF_RUNNING)),
935 	};
936 	ifr = (struct ifreq) {
937 		.ifr_data = (void *)&gcmd,
938 	};
939 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
940 	if (ret) {
941 		if (ret == -ENOTSUP && priv->representor) {
942 			/*
943 			 * For representors we can try to inherit link
944 			 * settings from the master device. Actually
945 			 * link settings do not make a lot of sense
946 			 * for representors due to missing physical
947 			 * link. The old kernel drivers supported
948 			 * emulated settings query for representors,
949 			 * the new ones do not, so we have to add
950 			 * this code for compatibility issues.
951 			 */
952 			master = mlx5_find_master_dev(dev);
953 			if (master) {
954 				ifr = (struct ifreq) {
955 					.ifr_data = (void *)&gcmd,
956 				};
957 				ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
958 			}
959 		}
960 		if (ret) {
961 			DRV_LOG(DEBUG,
962 				"port %u ioctl(SIOCETHTOOL,"
963 				" ETHTOOL_GLINKSETTINGS) failed: %s",
964 				dev->data->port_id, strerror(rte_errno));
965 			return ret;
966 		}
967 
968 	}
969 	gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
970 
971 	alignas(struct ethtool_link_settings)
972 	uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
973 		     sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
974 	struct ethtool_link_settings *ecmd = (void *)data;
975 
976 	*ecmd = gcmd;
977 	ifr.ifr_data = (void *)ecmd;
978 	ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
979 	if (ret) {
980 		DRV_LOG(DEBUG,
981 			"port %u ioctl(SIOCETHTOOL,"
982 			"ETHTOOL_GLINKSETTINGS) failed: %s",
983 			dev->data->port_id, strerror(rte_errno));
984 		return ret;
985 	}
986 	dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
987 							    ecmd->speed;
988 	sc = ecmd->link_mode_masks[0] |
989 		((uint64_t)ecmd->link_mode_masks[1] << 32);
990 	priv->link_speed_capa = 0;
991 	if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
992 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
993 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
994 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
995 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
996 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
997 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
998 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
999 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
1000 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
1001 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
1002 		priv->link_speed_capa |= ETH_LINK_SPEED_20G;
1003 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
1004 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
1005 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
1006 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
1007 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
1008 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
1009 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
1010 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
1011 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
1012 		priv->link_speed_capa |= ETH_LINK_SPEED_56G;
1013 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
1014 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
1015 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
1016 		priv->link_speed_capa |= ETH_LINK_SPEED_25G;
1017 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
1018 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
1019 		priv->link_speed_capa |= ETH_LINK_SPEED_50G;
1020 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
1021 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
1022 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
1023 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
1024 		priv->link_speed_capa |= ETH_LINK_SPEED_100G;
1025 	dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
1026 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1027 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1028 				  ETH_LINK_SPEED_FIXED);
1029 	if (((dev_link.link_speed && !dev_link.link_status) ||
1030 	     (!dev_link.link_speed && dev_link.link_status))) {
1031 		rte_errno = EAGAIN;
1032 		return -rte_errno;
1033 	}
1034 	*link = dev_link;
1035 	return 0;
1036 }
1037 
1038 /**
1039  * DPDK callback to retrieve physical link information.
1040  *
1041  * @param dev
1042  *   Pointer to Ethernet device structure.
1043  * @param wait_to_complete
1044  *   Wait for request completion.
1045  *
1046  * @return
1047  *   0 if link status was not updated, positive if it was, a negative errno
1048  *   value otherwise and rte_errno is set.
1049  */
1050 int
1051 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1052 {
1053 	int ret;
1054 	struct rte_eth_link dev_link;
1055 	time_t start_time = time(NULL);
1056 	int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
1057 
1058 	do {
1059 		ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
1060 		if (ret == -ENOTSUP)
1061 			ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
1062 		if (ret == 0)
1063 			break;
1064 		/* Handle wait to complete situation. */
1065 		if ((wait_to_complete || retry) && ret == -EAGAIN) {
1066 			if (abs((int)difftime(time(NULL), start_time)) <
1067 			    MLX5_LINK_STATUS_TIMEOUT) {
1068 				usleep(0);
1069 				continue;
1070 			} else {
1071 				rte_errno = EBUSY;
1072 				return -rte_errno;
1073 			}
1074 		} else if (ret < 0) {
1075 			return ret;
1076 		}
1077 	} while (wait_to_complete || retry-- > 0);
1078 	ret = !!memcmp(&dev->data->dev_link, &dev_link,
1079 		       sizeof(struct rte_eth_link));
1080 	dev->data->dev_link = dev_link;
1081 	return ret;
1082 }
1083 
1084 /**
1085  * DPDK callback to change the MTU.
1086  *
1087  * @param dev
1088  *   Pointer to Ethernet device structure.
1089  * @param in_mtu
1090  *   New MTU.
1091  *
1092  * @return
1093  *   0 on success, a negative errno value otherwise and rte_errno is set.
1094  */
1095 int
1096 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1097 {
1098 	struct mlx5_priv *priv = dev->data->dev_private;
1099 	uint16_t kern_mtu = 0;
1100 	int ret;
1101 
1102 	ret = mlx5_get_mtu(dev, &kern_mtu);
1103 	if (ret)
1104 		return ret;
1105 	/* Set kernel interface MTU first. */
1106 	ret = mlx5_set_mtu(dev, mtu);
1107 	if (ret)
1108 		return ret;
1109 	ret = mlx5_get_mtu(dev, &kern_mtu);
1110 	if (ret)
1111 		return ret;
1112 	if (kern_mtu == mtu) {
1113 		priv->mtu = mtu;
1114 		DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1115 			dev->data->port_id, mtu);
1116 		return 0;
1117 	}
1118 	rte_errno = EAGAIN;
1119 	return -rte_errno;
1120 }
1121 
1122 /**
1123  * DPDK callback to get flow control status.
1124  *
1125  * @param dev
1126  *   Pointer to Ethernet device structure.
1127  * @param[out] fc_conf
1128  *   Flow control output buffer.
1129  *
1130  * @return
1131  *   0 on success, a negative errno value otherwise and rte_errno is set.
1132  */
1133 int
1134 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1135 {
1136 	struct ifreq ifr;
1137 	struct ethtool_pauseparam ethpause = {
1138 		.cmd = ETHTOOL_GPAUSEPARAM
1139 	};
1140 	int ret;
1141 
1142 	ifr.ifr_data = (void *)&ethpause;
1143 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1144 	if (ret) {
1145 		DRV_LOG(WARNING,
1146 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1147 			" %s",
1148 			dev->data->port_id, strerror(rte_errno));
1149 		return ret;
1150 	}
1151 	fc_conf->autoneg = ethpause.autoneg;
1152 	if (ethpause.rx_pause && ethpause.tx_pause)
1153 		fc_conf->mode = RTE_FC_FULL;
1154 	else if (ethpause.rx_pause)
1155 		fc_conf->mode = RTE_FC_RX_PAUSE;
1156 	else if (ethpause.tx_pause)
1157 		fc_conf->mode = RTE_FC_TX_PAUSE;
1158 	else
1159 		fc_conf->mode = RTE_FC_NONE;
1160 	return 0;
1161 }
1162 
1163 /**
1164  * DPDK callback to modify flow control parameters.
1165  *
1166  * @param dev
1167  *   Pointer to Ethernet device structure.
1168  * @param[in] fc_conf
1169  *   Flow control parameters.
1170  *
1171  * @return
1172  *   0 on success, a negative errno value otherwise and rte_errno is set.
1173  */
1174 int
1175 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1176 {
1177 	struct ifreq ifr;
1178 	struct ethtool_pauseparam ethpause = {
1179 		.cmd = ETHTOOL_SPAUSEPARAM
1180 	};
1181 	int ret;
1182 
1183 	ifr.ifr_data = (void *)&ethpause;
1184 	ethpause.autoneg = fc_conf->autoneg;
1185 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1186 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
1187 		ethpause.rx_pause = 1;
1188 	else
1189 		ethpause.rx_pause = 0;
1190 
1191 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1192 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
1193 		ethpause.tx_pause = 1;
1194 	else
1195 		ethpause.tx_pause = 0;
1196 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1197 	if (ret) {
1198 		DRV_LOG(WARNING,
1199 			"port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1200 			" failed: %s",
1201 			dev->data->port_id, strerror(rte_errno));
1202 		return ret;
1203 	}
1204 	return 0;
1205 }
1206 
1207 /**
1208  * Get PCI information by sysfs device path.
1209  *
1210  * @param dev_path
1211  *   Pointer to device sysfs folder name.
1212  * @param[out] pci_addr
1213  *   PCI bus address output buffer.
1214  *
1215  * @return
1216  *   0 on success, a negative errno value otherwise and rte_errno is set.
1217  */
1218 int
1219 mlx5_dev_to_pci_addr(const char *dev_path,
1220 		     struct rte_pci_addr *pci_addr)
1221 {
1222 	FILE *file;
1223 	char line[32];
1224 	MKSTR(path, "%s/device/uevent", dev_path);
1225 
1226 	file = fopen(path, "rb");
1227 	if (file == NULL) {
1228 		rte_errno = errno;
1229 		return -rte_errno;
1230 	}
1231 	while (fgets(line, sizeof(line), file) == line) {
1232 		size_t len = strlen(line);
1233 		int ret;
1234 
1235 		/* Truncate long lines. */
1236 		if (len == (sizeof(line) - 1))
1237 			while (line[(len - 1)] != '\n') {
1238 				ret = fgetc(file);
1239 				if (ret == EOF)
1240 					break;
1241 				line[(len - 1)] = ret;
1242 			}
1243 		/* Extract information. */
1244 		if (sscanf(line,
1245 			   "PCI_SLOT_NAME="
1246 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1247 			   &pci_addr->domain,
1248 			   &pci_addr->bus,
1249 			   &pci_addr->devid,
1250 			   &pci_addr->function) == 4) {
1251 			ret = 0;
1252 			break;
1253 		}
1254 	}
1255 	fclose(file);
1256 	return 0;
1257 }
1258 
1259 /**
1260  * Handle asynchronous removal event for entire multiport device.
1261  *
1262  * @param sh
1263  *   Infiniband device shared context.
1264  */
1265 static void
1266 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1267 {
1268 	uint32_t i;
1269 
1270 	for (i = 0; i < sh->max_port; ++i) {
1271 		struct rte_eth_dev *dev;
1272 
1273 		if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1274 			/*
1275 			 * Or not existing port either no
1276 			 * handler installed for this port.
1277 			 */
1278 			continue;
1279 		}
1280 		dev = &rte_eth_devices[sh->port[i].ih_port_id];
1281 		assert(dev);
1282 		if (dev->data->dev_conf.intr_conf.rmv)
1283 			_rte_eth_dev_callback_process
1284 				(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1285 	}
1286 }
1287 
1288 /**
1289  * Handle shared asynchronous events the NIC (removal event
1290  * and link status change). Supports multiport IB device.
1291  *
1292  * @param cb_arg
1293  *   Callback argument.
1294  */
1295 void
1296 mlx5_dev_interrupt_handler(void *cb_arg)
1297 {
1298 	struct mlx5_ibv_shared *sh = cb_arg;
1299 	struct ibv_async_event event;
1300 
1301 	/* Read all message from the IB device and acknowledge them. */
1302 	for (;;) {
1303 		struct rte_eth_dev *dev;
1304 		uint32_t tmp;
1305 
1306 		if (mlx5_glue->get_async_event(sh->ctx, &event))
1307 			break;
1308 		/* Retrieve and check IB port index. */
1309 		tmp = (uint32_t)event.element.port_num;
1310 		if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1311 			/*
1312 			 * The DEVICE_FATAL event is called once for
1313 			 * entire device without port specifying.
1314 			 * We should notify all existing ports.
1315 			 */
1316 			mlx5_glue->ack_async_event(&event);
1317 			mlx5_dev_interrupt_device_fatal(sh);
1318 			continue;
1319 		}
1320 		assert(tmp && (tmp <= sh->max_port));
1321 		if (!tmp) {
1322 			/* Unsupported devive level event. */
1323 			mlx5_glue->ack_async_event(&event);
1324 			DRV_LOG(DEBUG,
1325 				"unsupported common event (type %d)",
1326 				event.event_type);
1327 			continue;
1328 		}
1329 		if (tmp > sh->max_port) {
1330 			/* Invalid IB port index. */
1331 			mlx5_glue->ack_async_event(&event);
1332 			DRV_LOG(DEBUG,
1333 				"cannot handle an event (type %d)"
1334 				"due to invalid IB port index (%u)",
1335 				event.event_type, tmp);
1336 			continue;
1337 		}
1338 		if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1339 			/* No handler installed. */
1340 			mlx5_glue->ack_async_event(&event);
1341 			DRV_LOG(DEBUG,
1342 				"cannot handle an event (type %d)"
1343 				"due to no handler installed for port %u",
1344 				event.event_type, tmp);
1345 			continue;
1346 		}
1347 		/* Retrieve ethernet device descriptor. */
1348 		tmp = sh->port[tmp - 1].ih_port_id;
1349 		dev = &rte_eth_devices[tmp];
1350 		assert(dev);
1351 		if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1352 		     event.event_type == IBV_EVENT_PORT_ERR) &&
1353 			dev->data->dev_conf.intr_conf.lsc) {
1354 			mlx5_glue->ack_async_event(&event);
1355 			if (mlx5_link_update(dev, 0) == -EAGAIN) {
1356 				usleep(0);
1357 				continue;
1358 			}
1359 			_rte_eth_dev_callback_process
1360 				(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1361 			continue;
1362 		}
1363 		DRV_LOG(DEBUG,
1364 			"port %u cannot handle an unknown event (type %d)",
1365 			dev->data->port_id, event.event_type);
1366 		mlx5_glue->ack_async_event(&event);
1367 	}
1368 }
1369 
1370 /*
1371  * Unregister callback handler safely. The handler may be active
1372  * while we are trying to unregister it, in this case code -EAGAIN
1373  * is returned by rte_intr_callback_unregister(). This routine checks
1374  * the return code and tries to unregister handler again.
1375  *
1376  * @param handle
1377  *   interrupt handle
1378  * @param cb_fn
1379  *   pointer to callback routine
1380  * @cb_arg
1381  *   opaque callback parameter
1382  */
1383 void
1384 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1385 			      rte_intr_callback_fn cb_fn, void *cb_arg)
1386 {
1387 	/*
1388 	 * Try to reduce timeout management overhead by not calling
1389 	 * the timer related routines on the first iteration. If the
1390 	 * unregistering succeeds on first call there will be no
1391 	 * timer calls at all.
1392 	 */
1393 	uint64_t twait = 0;
1394 	uint64_t start = 0;
1395 
1396 	do {
1397 		int ret;
1398 
1399 		ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1400 		if (ret >= 0)
1401 			return;
1402 		if (ret != -EAGAIN) {
1403 			DRV_LOG(INFO, "failed to unregister interrupt"
1404 				      " handler (error: %d)", ret);
1405 			assert(false);
1406 			return;
1407 		}
1408 		if (twait) {
1409 			struct timespec onems;
1410 
1411 			/* Wait one millisecond and try again. */
1412 			onems.tv_sec = 0;
1413 			onems.tv_nsec = NS_PER_S / MS_PER_S;
1414 			nanosleep(&onems, 0);
1415 			/* Check whether one second elapsed. */
1416 			if ((rte_get_timer_cycles() - start) <= twait)
1417 				continue;
1418 		} else {
1419 			/*
1420 			 * We get the amount of timer ticks for one second.
1421 			 * If this amount elapsed it means we spent one
1422 			 * second in waiting. This branch is executed once
1423 			 * on first iteration.
1424 			 */
1425 			twait = rte_get_timer_hz();
1426 			assert(twait);
1427 		}
1428 		/*
1429 		 * Timeout elapsed, show message (once a second) and retry.
1430 		 * We have no other acceptable option here, if we ignore
1431 		 * the unregistering return code the handler will not
1432 		 * be unregistered, fd will be closed and we may get the
1433 		 * crush. Hanging and messaging in the loop seems not to be
1434 		 * the worst choice.
1435 		 */
1436 		DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1437 		start = rte_get_timer_cycles();
1438 	} while (true);
1439 }
1440 
1441 /**
1442  * Handle DEVX interrupts from the NIC.
1443  * This function is probably called from the DPDK host thread.
1444  *
1445  * @param cb_arg
1446  *   Callback argument.
1447  */
1448 void
1449 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1450 {
1451 #ifndef HAVE_IBV_DEVX_ASYNC
1452 	(void)cb_arg;
1453 	return;
1454 #else
1455 	struct mlx5_ibv_shared *sh = cb_arg;
1456 	union {
1457 		struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1458 		uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1459 			    MLX5_ST_SZ_BYTES(traffic_counter) +
1460 			    sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1461 	} out;
1462 	uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1463 
1464 	while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1465 						   &out.cmd_resp,
1466 						   sizeof(out.buf)))
1467 		mlx5_flow_async_pool_query_handle
1468 			(sh, (uint64_t)out.cmd_resp.wr_id,
1469 			 mlx5_devx_get_out_command_status(buf));
1470 #endif /* HAVE_IBV_DEVX_ASYNC */
1471 }
1472 
1473 /**
1474  * Uninstall shared asynchronous device events handler.
1475  * This function is implemented to support event sharing
1476  * between multiple ports of single IB device.
1477  *
1478  * @param dev
1479  *   Pointer to Ethernet device.
1480  */
1481 static void
1482 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1483 {
1484 	struct mlx5_priv *priv = dev->data->dev_private;
1485 	struct mlx5_ibv_shared *sh = priv->sh;
1486 
1487 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1488 		return;
1489 	pthread_mutex_lock(&sh->intr_mutex);
1490 	assert(priv->ibv_port);
1491 	assert(priv->ibv_port <= sh->max_port);
1492 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1493 	if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1494 		goto exit;
1495 	assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1496 					(uint32_t)dev->data->port_id);
1497 	assert(sh->intr_cnt);
1498 	sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1499 	if (!sh->intr_cnt || --sh->intr_cnt)
1500 		goto exit;
1501 	mlx5_intr_callback_unregister(&sh->intr_handle,
1502 				     mlx5_dev_interrupt_handler, sh);
1503 	sh->intr_handle.fd = 0;
1504 	sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1505 exit:
1506 	pthread_mutex_unlock(&sh->intr_mutex);
1507 }
1508 
1509 /**
1510  * Uninstall devx shared asynchronous device events handler.
1511  * This function is implemeted to support event sharing
1512  * between multiple ports of single IB device.
1513  *
1514  * @param dev
1515  *   Pointer to Ethernet device.
1516  */
1517 static void
1518 mlx5_dev_shared_handler_devx_uninstall(struct rte_eth_dev *dev)
1519 {
1520 	struct mlx5_priv *priv = dev->data->dev_private;
1521 	struct mlx5_ibv_shared *sh = priv->sh;
1522 
1523 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1524 		return;
1525 	pthread_mutex_lock(&sh->intr_mutex);
1526 	assert(priv->ibv_port);
1527 	assert(priv->ibv_port <= sh->max_port);
1528 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1529 	if (sh->port[priv->ibv_port - 1].devx_ih_port_id >= RTE_MAX_ETHPORTS)
1530 		goto exit;
1531 	assert(sh->port[priv->ibv_port - 1].devx_ih_port_id ==
1532 					(uint32_t)dev->data->port_id);
1533 	sh->port[priv->ibv_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
1534 	if (!sh->devx_intr_cnt || --sh->devx_intr_cnt)
1535 		goto exit;
1536 	if (sh->intr_handle_devx.fd) {
1537 		rte_intr_callback_unregister(&sh->intr_handle_devx,
1538 					     mlx5_dev_interrupt_handler_devx,
1539 					     sh);
1540 		sh->intr_handle_devx.fd = 0;
1541 		sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1542 	}
1543 	if (sh->devx_comp) {
1544 		mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1545 		sh->devx_comp = NULL;
1546 	}
1547 exit:
1548 	pthread_mutex_unlock(&sh->intr_mutex);
1549 }
1550 
1551 /**
1552  * Install shared asynchronous device events handler.
1553  * This function is implemented to support event sharing
1554  * between multiple ports of single IB device.
1555  *
1556  * @param dev
1557  *   Pointer to Ethernet device.
1558  */
1559 static void
1560 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1561 {
1562 	struct mlx5_priv *priv = dev->data->dev_private;
1563 	struct mlx5_ibv_shared *sh = priv->sh;
1564 	int ret;
1565 	int flags;
1566 
1567 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1568 		return;
1569 	pthread_mutex_lock(&sh->intr_mutex);
1570 	assert(priv->ibv_port);
1571 	assert(priv->ibv_port <= sh->max_port);
1572 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1573 	if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1574 		/* The handler is already installed for this port. */
1575 		assert(sh->intr_cnt);
1576 		goto exit;
1577 	}
1578 	if (sh->intr_cnt) {
1579 		sh->port[priv->ibv_port - 1].ih_port_id =
1580 						(uint32_t)dev->data->port_id;
1581 		sh->intr_cnt++;
1582 		goto exit;
1583 	}
1584 	/* No shared handler installed. */
1585 	assert(sh->ctx->async_fd > 0);
1586 	flags = fcntl(sh->ctx->async_fd, F_GETFL);
1587 	ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1588 	if (ret) {
1589 		DRV_LOG(INFO, "failed to change file descriptor async event"
1590 			" queue");
1591 		/* Indicate there will be no interrupts. */
1592 		dev->data->dev_conf.intr_conf.lsc = 0;
1593 		dev->data->dev_conf.intr_conf.rmv = 0;
1594 	} else {
1595 		sh->intr_handle.fd = sh->ctx->async_fd;
1596 		sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1597 		rte_intr_callback_register(&sh->intr_handle,
1598 					   mlx5_dev_interrupt_handler, sh);
1599 		sh->intr_cnt++;
1600 		sh->port[priv->ibv_port - 1].ih_port_id =
1601 						(uint32_t)dev->data->port_id;
1602 	}
1603 exit:
1604 	pthread_mutex_unlock(&sh->intr_mutex);
1605 }
1606 
1607 /**
1608  * Install devx shared asyncronous device events handler.
1609  * This function is implemeted to support event sharing
1610  * between multiple ports of single IB device.
1611  *
1612  * @param dev
1613  *   Pointer to Ethernet device.
1614  */
1615 static void
1616 mlx5_dev_shared_handler_devx_install(struct rte_eth_dev *dev)
1617 {
1618 	struct mlx5_priv *priv = dev->data->dev_private;
1619 	struct mlx5_ibv_shared *sh = priv->sh;
1620 
1621 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1622 		return;
1623 	pthread_mutex_lock(&sh->intr_mutex);
1624 	assert(priv->ibv_port);
1625 	assert(priv->ibv_port <= sh->max_port);
1626 	assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1627 	if (sh->port[priv->ibv_port - 1].devx_ih_port_id < RTE_MAX_ETHPORTS) {
1628 		/* The handler is already installed for this port. */
1629 		assert(sh->devx_intr_cnt);
1630 		goto exit;
1631 	}
1632 	if (sh->devx_intr_cnt) {
1633 		sh->devx_intr_cnt++;
1634 		sh->port[priv->ibv_port - 1].devx_ih_port_id =
1635 					(uint32_t)dev->data->port_id;
1636 		goto exit;
1637 	}
1638 	if (priv->config.devx) {
1639 #ifndef HAVE_IBV_DEVX_ASYNC
1640 		goto exit;
1641 #else
1642 		sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1643 		if (sh->devx_comp) {
1644 			int flags = fcntl(sh->devx_comp->fd, F_GETFL);
1645 			int ret = fcntl(sh->devx_comp->fd, F_SETFL,
1646 				    flags | O_NONBLOCK);
1647 
1648 			if (ret) {
1649 				DRV_LOG(INFO, "failed to change file descriptor"
1650 					" devx async event queue");
1651 			} else {
1652 				sh->intr_handle_devx.fd = sh->devx_comp->fd;
1653 				sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1654 				rte_intr_callback_register
1655 					(&sh->intr_handle_devx,
1656 					 mlx5_dev_interrupt_handler_devx, sh);
1657 				sh->devx_intr_cnt++;
1658 				sh->port[priv->ibv_port - 1].devx_ih_port_id =
1659 						(uint32_t)dev->data->port_id;
1660 			}
1661 		}
1662 #endif /* HAVE_IBV_DEVX_ASYNC */
1663 	}
1664 exit:
1665 	pthread_mutex_unlock(&sh->intr_mutex);
1666 }
1667 
1668 /**
1669  * Uninstall interrupt handler.
1670  *
1671  * @param dev
1672  *   Pointer to Ethernet device.
1673  */
1674 void
1675 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1676 {
1677 	mlx5_dev_shared_handler_uninstall(dev);
1678 }
1679 
1680 /**
1681  * Install interrupt handler.
1682  *
1683  * @param dev
1684  *   Pointer to Ethernet device.
1685  */
1686 void
1687 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1688 {
1689 	mlx5_dev_shared_handler_install(dev);
1690 }
1691 
1692 /**
1693  * Devx uninstall interrupt handler.
1694  *
1695  * @param dev
1696  *   Pointer to Ethernet device.
1697  */
1698 void
1699 mlx5_dev_interrupt_handler_devx_uninstall(struct rte_eth_dev *dev)
1700 {
1701 	mlx5_dev_shared_handler_devx_uninstall(dev);
1702 }
1703 
1704 /**
1705  * Devx install interrupt handler.
1706  *
1707  * @param dev
1708  *   Pointer to Ethernet device.
1709  */
1710 void
1711 mlx5_dev_interrupt_handler_devx_install(struct rte_eth_dev *dev)
1712 {
1713 	mlx5_dev_shared_handler_devx_install(dev);
1714 }
1715 
1716 /**
1717  * DPDK callback to bring the link DOWN.
1718  *
1719  * @param dev
1720  *   Pointer to Ethernet device structure.
1721  *
1722  * @return
1723  *   0 on success, a negative errno value otherwise and rte_errno is set.
1724  */
1725 int
1726 mlx5_set_link_down(struct rte_eth_dev *dev)
1727 {
1728 	return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1729 }
1730 
1731 /**
1732  * DPDK callback to bring the link UP.
1733  *
1734  * @param dev
1735  *   Pointer to Ethernet device structure.
1736  *
1737  * @return
1738  *   0 on success, a negative errno value otherwise and rte_errno is set.
1739  */
1740 int
1741 mlx5_set_link_up(struct rte_eth_dev *dev)
1742 {
1743 	return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1744 }
1745 
1746 /**
1747  * Configure the RX function to use.
1748  *
1749  * @param dev
1750  *   Pointer to private data structure.
1751  *
1752  * @return
1753  *   Pointer to selected Rx burst function.
1754  */
1755 eth_rx_burst_t
1756 mlx5_select_rx_function(struct rte_eth_dev *dev)
1757 {
1758 	eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1759 
1760 	assert(dev != NULL);
1761 	if (mlx5_check_vec_rx_support(dev) > 0) {
1762 		rx_pkt_burst = mlx5_rx_burst_vec;
1763 		DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1764 			dev->data->port_id);
1765 	} else if (mlx5_mprq_enabled(dev)) {
1766 		rx_pkt_burst = mlx5_rx_burst_mprq;
1767 	}
1768 	return rx_pkt_burst;
1769 }
1770 
1771 /**
1772  * Check if mlx5 device was removed.
1773  *
1774  * @param dev
1775  *   Pointer to Ethernet device structure.
1776  *
1777  * @return
1778  *   1 when device is removed, otherwise 0.
1779  */
1780 int
1781 mlx5_is_removed(struct rte_eth_dev *dev)
1782 {
1783 	struct ibv_device_attr device_attr;
1784 	struct mlx5_priv *priv = dev->data->dev_private;
1785 
1786 	if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1787 		return 1;
1788 	return 0;
1789 }
1790 
1791 /**
1792  * Get the E-Switch parameters by port id.
1793  *
1794  * @param[in] port
1795  *   Device port id.
1796  * @param[out] es_domain_id
1797  *   E-Switch domain id.
1798  * @param[out] es_port_id
1799  *   The port id of the port in the E-Switch.
1800  *
1801  * @return
1802  *   pointer to device private data structure containing data needed
1803  *   on success, NULL otherwise and rte_errno is set.
1804  */
1805 struct mlx5_priv *
1806 mlx5_port_to_eswitch_info(uint16_t port)
1807 {
1808 	struct rte_eth_dev *dev;
1809 	struct mlx5_priv *priv;
1810 
1811 	if (port >= RTE_MAX_ETHPORTS) {
1812 		rte_errno = EINVAL;
1813 		return NULL;
1814 	}
1815 	if (!rte_eth_dev_is_valid_port(port)) {
1816 		rte_errno = ENODEV;
1817 		return NULL;
1818 	}
1819 	dev = &rte_eth_devices[port];
1820 	priv = dev->data->dev_private;
1821 	if (!(priv->representor || priv->master)) {
1822 		rte_errno = EINVAL;
1823 		return NULL;
1824 	}
1825 	return priv;
1826 }
1827 
1828 /**
1829  * Get the E-Switch parameters by device instance.
1830  *
1831  * @param[in] port
1832  *   Device port id.
1833  * @param[out] es_domain_id
1834  *   E-Switch domain id.
1835  * @param[out] es_port_id
1836  *   The port id of the port in the E-Switch.
1837  *
1838  * @return
1839  *   pointer to device private data structure containing data needed
1840  *   on success, NULL otherwise and rte_errno is set.
1841  */
1842 struct mlx5_priv *
1843 mlx5_dev_to_eswitch_info(struct rte_eth_dev *dev)
1844 {
1845 	struct mlx5_priv *priv;
1846 
1847 	priv = dev->data->dev_private;
1848 	if (!(priv->representor || priv->master)) {
1849 		rte_errno = EINVAL;
1850 		return NULL;
1851 	}
1852 	return priv;
1853 }
1854 
1855 /**
1856  * Get switch information associated with network interface.
1857  *
1858  * @param ifindex
1859  *   Network interface index.
1860  * @param[out] info
1861  *   Switch information object, populated in case of success.
1862  *
1863  * @return
1864  *   0 on success, a negative errno value otherwise and rte_errno is set.
1865  */
1866 int
1867 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1868 {
1869 	char ifname[IF_NAMESIZE];
1870 	char port_name[IF_NAMESIZE];
1871 	FILE *file;
1872 	struct mlx5_switch_info data = {
1873 		.master = 0,
1874 		.representor = 0,
1875 		.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1876 		.port_name = 0,
1877 		.switch_id = 0,
1878 	};
1879 	DIR *dir;
1880 	bool port_switch_id_set = false;
1881 	bool device_dir = false;
1882 	char c;
1883 	int ret;
1884 
1885 	if (!if_indextoname(ifindex, ifname)) {
1886 		rte_errno = errno;
1887 		return -rte_errno;
1888 	}
1889 
1890 	MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1891 	      ifname);
1892 	MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1893 	      ifname);
1894 	MKSTR(pci_device, "/sys/class/net/%s/device",
1895 	      ifname);
1896 
1897 	file = fopen(phys_port_name, "rb");
1898 	if (file != NULL) {
1899 		ret = fscanf(file, "%s", port_name);
1900 		fclose(file);
1901 		if (ret == 1)
1902 			mlx5_translate_port_name(port_name, &data);
1903 	}
1904 	file = fopen(phys_switch_id, "rb");
1905 	if (file == NULL) {
1906 		rte_errno = errno;
1907 		return -rte_errno;
1908 	}
1909 	port_switch_id_set =
1910 		fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1911 		c == '\n';
1912 	fclose(file);
1913 	dir = opendir(pci_device);
1914 	if (dir != NULL) {
1915 		closedir(dir);
1916 		device_dir = true;
1917 	}
1918 	if (port_switch_id_set) {
1919 		/* We have some E-Switch configuration. */
1920 		mlx5_sysfs_check_switch_info(device_dir, &data);
1921 	}
1922 	*info = data;
1923 	assert(!(data.master && data.representor));
1924 	if (data.master && data.representor) {
1925 		DRV_LOG(ERR, "ifindex %u device is recognized as master"
1926 			     " and as representor", ifindex);
1927 		rte_errno = ENODEV;
1928 		return -rte_errno;
1929 	}
1930 	return 0;
1931 }
1932 
1933 /**
1934  * Analyze gathered port parameters via Netlink to recognize master
1935  * and representor devices for E-Switch configuration.
1936  *
1937  * @param[in] num_vf_set
1938  *   flag of presence of number of VFs port attribute.
1939  * @param[inout] switch_info
1940  *   Port information, including port name as a number and port name
1941  *   type if recognized
1942  *
1943  * @return
1944  *   master and representor flags are set in switch_info according to
1945  *   recognized parameters (if any).
1946  */
1947 void
1948 mlx5_nl_check_switch_info(bool num_vf_set,
1949 			  struct mlx5_switch_info *switch_info)
1950 {
1951 	switch (switch_info->name_type) {
1952 	case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1953 		/*
1954 		 * Name is not recognized, assume the master,
1955 		 * check the number of VFs key presence.
1956 		 */
1957 		switch_info->master = num_vf_set;
1958 		break;
1959 	case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1960 		/*
1961 		 * Name is not set, this assumes the legacy naming
1962 		 * schema for master, just check if there is a
1963 		 * number of VFs key.
1964 		 */
1965 		switch_info->master = num_vf_set;
1966 		break;
1967 	case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1968 		/* New uplink naming schema recognized. */
1969 		switch_info->master = 1;
1970 		break;
1971 	case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1972 		/* Legacy representors naming schema. */
1973 		switch_info->representor = !num_vf_set;
1974 		break;
1975 	case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1976 		/* New representors naming schema. */
1977 		switch_info->representor = 1;
1978 		break;
1979 	}
1980 }
1981 
1982 /**
1983  * Analyze gathered port parameters via sysfs to recognize master
1984  * and representor devices for E-Switch configuration.
1985  *
1986  * @param[in] device_dir
1987  *   flag of presence of "device" directory under port device key.
1988  * @param[inout] switch_info
1989  *   Port information, including port name as a number and port name
1990  *   type if recognized
1991  *
1992  * @return
1993  *   master and representor flags are set in switch_info according to
1994  *   recognized parameters (if any).
1995  */
1996 void
1997 mlx5_sysfs_check_switch_info(bool device_dir,
1998 			     struct mlx5_switch_info *switch_info)
1999 {
2000 	switch (switch_info->name_type) {
2001 	case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
2002 		/*
2003 		 * Name is not recognized, assume the master,
2004 		 * check the device directory presence.
2005 		 */
2006 		switch_info->master = device_dir;
2007 		break;
2008 	case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
2009 		/*
2010 		 * Name is not set, this assumes the legacy naming
2011 		 * schema for master, just check if there is
2012 		 * a device directory.
2013 		 */
2014 		switch_info->master = device_dir;
2015 		break;
2016 	case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
2017 		/* New uplink naming schema recognized. */
2018 		switch_info->master = 1;
2019 		break;
2020 	case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
2021 		/* Legacy representors naming schema. */
2022 		switch_info->representor = !device_dir;
2023 		break;
2024 	case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2025 		/* New representors naming schema. */
2026 		switch_info->representor = 1;
2027 		break;
2028 	}
2029 }
2030 
2031 /**
2032  * Extract port name, as a number, from sysfs or netlink information.
2033  *
2034  * @param[in] port_name_in
2035  *   String representing the port name.
2036  * @param[out] port_info_out
2037  *   Port information, including port name as a number and port name
2038  *   type if recognized
2039  *
2040  * @return
2041  *   port_name field set according to recognized name format.
2042  */
2043 void
2044 mlx5_translate_port_name(const char *port_name_in,
2045 			 struct mlx5_switch_info *port_info_out)
2046 {
2047 	char pf_c1, pf_c2, vf_c1, vf_c2;
2048 	char *end;
2049 	int sc_items;
2050 
2051 	/*
2052 	 * Check for port-name as a string of the form pf0vf0
2053 	 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
2054 	 */
2055 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
2056 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
2057 			  &vf_c1, &vf_c2, &port_info_out->port_name);
2058 	if (sc_items == 6 &&
2059 	    pf_c1 == 'p' && pf_c2 == 'f' &&
2060 	    vf_c1 == 'v' && vf_c2 == 'f') {
2061 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
2062 		return;
2063 	}
2064 	/*
2065 	 * Check for port-name as a string of the form p0
2066 	 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
2067 	 */
2068 	sc_items = sscanf(port_name_in, "%c%d",
2069 			  &pf_c1, &port_info_out->port_name);
2070 	if (sc_items == 2 && pf_c1 == 'p') {
2071 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2072 		return;
2073 	}
2074 	/* Check for port-name as a number (support kernel ver < 5.0 */
2075 	errno = 0;
2076 	port_info_out->port_name = strtol(port_name_in, &end, 0);
2077 	if (!errno &&
2078 	    (size_t)(end - port_name_in) == strlen(port_name_in)) {
2079 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
2080 		return;
2081 	}
2082 	port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
2083 	return;
2084 }
2085 
2086 /**
2087  * DPDK callback to retrieve plug-in module EEPROM information (type and size).
2088  *
2089  * @param dev
2090  *   Pointer to Ethernet device structure.
2091  * @param[out] modinfo
2092  *   Storage for plug-in module EEPROM information.
2093  *
2094  * @return
2095  *   0 on success, a negative errno value otherwise and rte_errno is set.
2096  */
2097 int
2098 mlx5_get_module_info(struct rte_eth_dev *dev,
2099 		     struct rte_eth_dev_module_info *modinfo)
2100 {
2101 	struct ethtool_modinfo info = {
2102 		.cmd = ETHTOOL_GMODULEINFO,
2103 	};
2104 	struct ifreq ifr = (struct ifreq) {
2105 		.ifr_data = (void *)&info,
2106 	};
2107 	int ret = 0;
2108 
2109 	if (!dev || !modinfo) {
2110 		DRV_LOG(WARNING, "missing argument, cannot get module info");
2111 		rte_errno = EINVAL;
2112 		return -rte_errno;
2113 	}
2114 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2115 	if (ret) {
2116 		DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2117 			dev->data->port_id, strerror(rte_errno));
2118 		return ret;
2119 	}
2120 	modinfo->type = info.type;
2121 	modinfo->eeprom_len = info.eeprom_len;
2122 	return ret;
2123 }
2124 
2125 /**
2126  * DPDK callback to retrieve plug-in module EEPROM data.
2127  *
2128  * @param dev
2129  *   Pointer to Ethernet device structure.
2130  * @param[out] info
2131  *   Storage for plug-in module EEPROM data.
2132  *
2133  * @return
2134  *   0 on success, a negative errno value otherwise and rte_errno is set.
2135  */
2136 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
2137 			   struct rte_dev_eeprom_info *info)
2138 {
2139 	struct ethtool_eeprom *eeprom;
2140 	struct ifreq ifr;
2141 	int ret = 0;
2142 
2143 	if (!dev || !info) {
2144 		DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
2145 		rte_errno = EINVAL;
2146 		return -rte_errno;
2147 	}
2148 	eeprom = rte_calloc(__func__, 1,
2149 			    (sizeof(struct ethtool_eeprom) + info->length), 0);
2150 	if (!eeprom) {
2151 		DRV_LOG(WARNING, "port %u cannot allocate memory for "
2152 			"eeprom data", dev->data->port_id);
2153 		rte_errno = ENOMEM;
2154 		return -rte_errno;
2155 	}
2156 	eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2157 	eeprom->offset = info->offset;
2158 	eeprom->len = info->length;
2159 	ifr = (struct ifreq) {
2160 		.ifr_data = (void *)eeprom,
2161 	};
2162 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2163 	if (ret)
2164 		DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2165 			dev->data->port_id, strerror(rte_errno));
2166 	else
2167 		rte_memcpy(info->data, eeprom->data, info->length);
2168 	rte_free(eeprom);
2169 	return ret;
2170 }
2171 
2172 /**
2173  * DPDK callback to retrieve hairpin capabilities.
2174  *
2175  * @param dev
2176  *   Pointer to Ethernet device structure.
2177  * @param[out] cap
2178  *   Storage for hairpin capability data.
2179  *
2180  * @return
2181  *   0 on success, a negative errno value otherwise and rte_errno is set.
2182  */
2183 int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
2184 			 struct rte_eth_hairpin_cap *cap)
2185 {
2186 	struct mlx5_priv *priv = dev->data->dev_private;
2187 
2188 	if (priv->sh->devx == 0) {
2189 		rte_errno = ENOTSUP;
2190 		return -rte_errno;
2191 	}
2192 	cap->max_nb_queues = UINT16_MAX;
2193 	cap->max_rx_2_tx = 1;
2194 	cap->max_tx_2_rx = 1;
2195 	cap->max_nb_desc = 8192;
2196 	return 0;
2197 }
2198