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