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