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