xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision a61888c8f297eac7e917850e2a4f66601bb715c1)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5 
6 #define _GNU_SOURCE
7 
8 #include <stddef.h>
9 #include <assert.h>
10 #include <inttypes.h>
11 #include <unistd.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <dirent.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/utsname.h>
22 #include <netinet/in.h>
23 #include <linux/ethtool.h>
24 #include <linux/sockios.h>
25 #include <linux/version.h>
26 #include <fcntl.h>
27 #include <stdalign.h>
28 #include <sys/un.h>
29 
30 #include <rte_atomic.h>
31 #include <rte_ethdev_driver.h>
32 #include <rte_bus_pci.h>
33 #include <rte_mbuf.h>
34 #include <rte_common.h>
35 #include <rte_interrupts.h>
36 #include <rte_alarm.h>
37 #include <rte_malloc.h>
38 
39 #include "mlx5.h"
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
43 
44 /* Add defines in case the running kernel is not the same as user headers. */
45 #ifndef ETHTOOL_GLINKSETTINGS
46 struct ethtool_link_settings {
47 	uint32_t cmd;
48 	uint32_t speed;
49 	uint8_t duplex;
50 	uint8_t port;
51 	uint8_t phy_address;
52 	uint8_t autoneg;
53 	uint8_t mdio_support;
54 	uint8_t eth_to_mdix;
55 	uint8_t eth_tp_mdix_ctrl;
56 	int8_t link_mode_masks_nwords;
57 	uint32_t reserved[8];
58 	uint32_t link_mode_masks[];
59 };
60 
61 #define ETHTOOL_GLINKSETTINGS 0x0000004c
62 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
63 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
64 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
65 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
66 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
67 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
68 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
69 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
70 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
71 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
72 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
73 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
74 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
75 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
76 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
77 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
78 #endif
79 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
80 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
81 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
82 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
83 #endif
84 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
85 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
86 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
87 #endif
88 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
89 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
90 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
91 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
92 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
93 #endif
94 
95 /**
96  * Get interface name from private structure.
97  *
98  * @param[in] priv
99  *   Pointer to private structure.
100  * @param[out] ifname
101  *   Interface name output buffer.
102  *
103  * @return
104  *   0 on success, -1 on failure and errno is set.
105  */
106 int
107 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
108 {
109 	DIR *dir;
110 	struct dirent *dent;
111 	unsigned int dev_type = 0;
112 	unsigned int dev_port_prev = ~0u;
113 	char match[IF_NAMESIZE] = "";
114 
115 	{
116 		MKSTR(path, "%s/device/net", priv->ibdev_path);
117 
118 		dir = opendir(path);
119 		if (dir == NULL)
120 			return -1;
121 	}
122 	while ((dent = readdir(dir)) != NULL) {
123 		char *name = dent->d_name;
124 		FILE *file;
125 		unsigned int dev_port;
126 		int r;
127 
128 		if ((name[0] == '.') &&
129 		    ((name[1] == '\0') ||
130 		     ((name[1] == '.') && (name[2] == '\0'))))
131 			continue;
132 
133 		MKSTR(path, "%s/device/net/%s/%s",
134 		      priv->ibdev_path, name,
135 		      (dev_type ? "dev_id" : "dev_port"));
136 
137 		file = fopen(path, "rb");
138 		if (file == NULL) {
139 			if (errno != ENOENT)
140 				continue;
141 			/*
142 			 * Switch to dev_id when dev_port does not exist as
143 			 * is the case with Linux kernel versions < 3.15.
144 			 */
145 try_dev_id:
146 			match[0] = '\0';
147 			if (dev_type)
148 				break;
149 			dev_type = 1;
150 			dev_port_prev = ~0u;
151 			rewinddir(dir);
152 			continue;
153 		}
154 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
155 		fclose(file);
156 		if (r != 1)
157 			continue;
158 		/*
159 		 * Switch to dev_id when dev_port returns the same value for
160 		 * all ports. May happen when using a MOFED release older than
161 		 * 3.0 with a Linux kernel >= 3.15.
162 		 */
163 		if (dev_port == dev_port_prev)
164 			goto try_dev_id;
165 		dev_port_prev = dev_port;
166 		if (dev_port == (priv->port - 1u))
167 			snprintf(match, sizeof(match), "%s", name);
168 	}
169 	closedir(dir);
170 	if (match[0] == '\0')
171 		return -1;
172 	strncpy(*ifname, match, sizeof(*ifname));
173 	return 0;
174 }
175 
176 /**
177  * Perform ifreq ioctl() on associated Ethernet device.
178  *
179  * @param[in] priv
180  *   Pointer to private structure.
181  * @param req
182  *   Request number to pass to ioctl().
183  * @param[out] ifr
184  *   Interface request structure output buffer.
185  *
186  * @return
187  *   0 on success, -1 on failure and errno is set.
188  */
189 int
190 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
191 {
192 	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
193 	int ret = -1;
194 
195 	if (sock == -1)
196 		return ret;
197 	if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
198 		ret = ioctl(sock, req, ifr);
199 	close(sock);
200 	return ret;
201 }
202 
203 /**
204  * Get device MTU.
205  *
206  * @param priv
207  *   Pointer to private structure.
208  * @param[out] mtu
209  *   MTU value output buffer.
210  *
211  * @return
212  *   0 on success, -1 on failure and errno is set.
213  */
214 int
215 priv_get_mtu(struct priv *priv, uint16_t *mtu)
216 {
217 	struct ifreq request;
218 	int ret = priv_ifreq(priv, SIOCGIFMTU, &request);
219 
220 	if (ret)
221 		return ret;
222 	*mtu = request.ifr_mtu;
223 	return 0;
224 }
225 
226 /**
227  * Set device MTU.
228  *
229  * @param priv
230  *   Pointer to private structure.
231  * @param mtu
232  *   MTU value to set.
233  *
234  * @return
235  *   0 on success, -1 on failure and errno is set.
236  */
237 static int
238 priv_set_mtu(struct priv *priv, uint16_t mtu)
239 {
240 	struct ifreq request = { .ifr_mtu = mtu, };
241 
242 	return priv_ifreq(priv, SIOCSIFMTU, &request);
243 }
244 
245 /**
246  * Set device flags.
247  *
248  * @param priv
249  *   Pointer to private structure.
250  * @param keep
251  *   Bitmask for flags that must remain untouched.
252  * @param flags
253  *   Bitmask for flags to modify.
254  *
255  * @return
256  *   0 on success, -1 on failure and errno is set.
257  */
258 int
259 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
260 {
261 	struct ifreq request;
262 	int ret = priv_ifreq(priv, SIOCGIFFLAGS, &request);
263 
264 	if (ret)
265 		return ret;
266 	request.ifr_flags &= keep;
267 	request.ifr_flags |= flags & ~keep;
268 	return priv_ifreq(priv, SIOCSIFFLAGS, &request);
269 }
270 
271 /**
272  * Ethernet device configuration.
273  *
274  * Prepare the driver for a given number of TX and RX queues.
275  *
276  * @param dev
277  *   Pointer to Ethernet device structure.
278  *
279  * @return
280  *   0 on success, errno value on failure.
281  */
282 static int
283 dev_configure(struct rte_eth_dev *dev)
284 {
285 	struct priv *priv = dev->data->dev_private;
286 	unsigned int rxqs_n = dev->data->nb_rx_queues;
287 	unsigned int txqs_n = dev->data->nb_tx_queues;
288 	unsigned int i;
289 	unsigned int j;
290 	unsigned int reta_idx_n;
291 	const uint8_t use_app_rss_key =
292 		!!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
293 	uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
294 	uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
295 	uint64_t supp_rx_offloads =
296 		(mlx5_priv_get_rx_port_offloads(priv) |
297 		 mlx5_priv_get_rx_queue_offloads(priv));
298 	uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
299 
300 	if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
301 		ERROR("Some Tx offloads are not supported "
302 		      "requested 0x%" PRIx64 " supported 0x%" PRIx64,
303 		      tx_offloads, supp_tx_offloads);
304 		return ENOTSUP;
305 	}
306 	if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
307 		ERROR("Some Rx offloads are not supported "
308 		      "requested 0x%" PRIx64 " supported 0x%" PRIx64,
309 		      rx_offloads, supp_rx_offloads);
310 		return ENOTSUP;
311 	}
312 	if (use_app_rss_key &&
313 	    (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
314 	     rss_hash_default_key_len)) {
315 		/* MLX5 RSS only support 40bytes key. */
316 		return EINVAL;
317 	}
318 	priv->rss_conf.rss_key =
319 		rte_realloc(priv->rss_conf.rss_key,
320 			    rss_hash_default_key_len, 0);
321 	if (!priv->rss_conf.rss_key) {
322 		ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
323 		return ENOMEM;
324 	}
325 	memcpy(priv->rss_conf.rss_key,
326 	       use_app_rss_key ?
327 	       dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
328 	       rss_hash_default_key,
329 	       rss_hash_default_key_len);
330 	priv->rss_conf.rss_key_len = rss_hash_default_key_len;
331 	priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
332 	priv->rxqs = (void *)dev->data->rx_queues;
333 	priv->txqs = (void *)dev->data->tx_queues;
334 	if (txqs_n != priv->txqs_n) {
335 		INFO("%p: TX queues number update: %u -> %u",
336 		     (void *)dev, priv->txqs_n, txqs_n);
337 		priv->txqs_n = txqs_n;
338 	}
339 	if (rxqs_n > priv->config.ind_table_max_size) {
340 		ERROR("cannot handle this many RX queues (%u)", rxqs_n);
341 		return EINVAL;
342 	}
343 	if (rxqs_n == priv->rxqs_n)
344 		return 0;
345 	INFO("%p: RX queues number update: %u -> %u",
346 	     (void *)dev, priv->rxqs_n, rxqs_n);
347 	priv->rxqs_n = rxqs_n;
348 	/* If the requested number of RX queues is not a power of two, use the
349 	 * maximum indirection table size for better balancing.
350 	 * The result is always rounded to the next power of two. */
351 	reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
352 				     priv->config.ind_table_max_size :
353 				     rxqs_n));
354 	if (priv_rss_reta_index_resize(priv, reta_idx_n))
355 		return ENOMEM;
356 	/* When the number of RX queues is not a power of two, the remaining
357 	 * table entries are padded with reused WQs and hashes are not spread
358 	 * uniformly. */
359 	for (i = 0, j = 0; (i != reta_idx_n); ++i) {
360 		(*priv->reta_idx)[i] = j;
361 		if (++j == rxqs_n)
362 			j = 0;
363 	}
364 	return 0;
365 }
366 
367 /**
368  * DPDK callback for Ethernet device configuration.
369  *
370  * @param dev
371  *   Pointer to Ethernet device structure.
372  *
373  * @return
374  *   0 on success, negative errno value on failure.
375  */
376 int
377 mlx5_dev_configure(struct rte_eth_dev *dev)
378 {
379 	struct priv *priv = dev->data->dev_private;
380 	int ret;
381 
382 	priv_lock(priv);
383 	ret = dev_configure(dev);
384 	assert(ret >= 0);
385 	priv_unlock(priv);
386 	return -ret;
387 }
388 
389 /**
390  * DPDK callback to get information about the device.
391  *
392  * @param dev
393  *   Pointer to Ethernet device structure.
394  * @param[out] info
395  *   Info structure output buffer.
396  */
397 void
398 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
399 {
400 	struct priv *priv = dev->data->dev_private;
401 	struct mlx5_dev_config *config = &priv->config;
402 	unsigned int max;
403 	char ifname[IF_NAMESIZE];
404 
405 	info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
406 
407 	priv_lock(priv);
408 	/* FIXME: we should ask the device for these values. */
409 	info->min_rx_bufsize = 32;
410 	info->max_rx_pktlen = 65536;
411 	/*
412 	 * Since we need one CQ per QP, the limit is the minimum number
413 	 * between the two values.
414 	 */
415 	max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
416 		      priv->device_attr.orig_attr.max_qp);
417 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
418 	if (max >= 65535)
419 		max = 65535;
420 	info->max_rx_queues = max;
421 	info->max_tx_queues = max;
422 	info->max_mac_addrs = RTE_DIM(priv->mac);
423 	info->rx_queue_offload_capa =
424 		mlx5_priv_get_rx_queue_offloads(priv);
425 	info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
426 				 info->rx_queue_offload_capa);
427 	info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
428 	if (priv_get_ifname(priv, &ifname) == 0)
429 		info->if_index = if_nametoindex(ifname);
430 	info->reta_size = priv->reta_idx_n ?
431 		priv->reta_idx_n : config->ind_table_max_size;
432 	info->hash_key_size = priv->rss_conf.rss_key_len;
433 	info->speed_capa = priv->link_speed_capa;
434 	info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
435 	priv_unlock(priv);
436 }
437 
438 const uint32_t *
439 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
440 {
441 	static const uint32_t ptypes[] = {
442 		/* refers to rxq_cq_to_pkt_type() */
443 		RTE_PTYPE_L2_ETHER,
444 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
445 		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
446 		RTE_PTYPE_L4_NONFRAG,
447 		RTE_PTYPE_L4_FRAG,
448 		RTE_PTYPE_L4_TCP,
449 		RTE_PTYPE_L4_UDP,
450 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
451 		RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
452 		RTE_PTYPE_INNER_L4_NONFRAG,
453 		RTE_PTYPE_INNER_L4_FRAG,
454 		RTE_PTYPE_INNER_L4_TCP,
455 		RTE_PTYPE_INNER_L4_UDP,
456 		RTE_PTYPE_UNKNOWN
457 	};
458 
459 	if (dev->rx_pkt_burst == mlx5_rx_burst ||
460 	    dev->rx_pkt_burst == mlx5_rx_burst_vec)
461 		return ptypes;
462 	return NULL;
463 }
464 
465 /**
466  * DPDK callback to retrieve physical link information.
467  *
468  * @param dev
469  *   Pointer to Ethernet device structure.
470  * @param wait_to_complete
471  *   Wait for request completion (ignored).
472  */
473 static int
474 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
475 {
476 	struct priv *priv = dev->data->dev_private;
477 	struct ethtool_cmd edata = {
478 		.cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
479 	};
480 	struct ifreq ifr;
481 	struct rte_eth_link dev_link;
482 	int link_speed = 0;
483 
484 	/* priv_lock() is not taken to allow concurrent calls. */
485 
486 	(void)wait_to_complete;
487 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
488 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
489 		return -1;
490 	}
491 	memset(&dev_link, 0, sizeof(dev_link));
492 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
493 				(ifr.ifr_flags & IFF_RUNNING));
494 	ifr.ifr_data = (void *)&edata;
495 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
496 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
497 		     strerror(errno));
498 		return -1;
499 	}
500 	link_speed = ethtool_cmd_speed(&edata);
501 	if (link_speed == -1)
502 		dev_link.link_speed = 0;
503 	else
504 		dev_link.link_speed = link_speed;
505 	priv->link_speed_capa = 0;
506 	if (edata.supported & SUPPORTED_Autoneg)
507 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
508 	if (edata.supported & (SUPPORTED_1000baseT_Full |
509 			       SUPPORTED_1000baseKX_Full))
510 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
511 	if (edata.supported & SUPPORTED_10000baseKR_Full)
512 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
513 	if (edata.supported & (SUPPORTED_40000baseKR4_Full |
514 			       SUPPORTED_40000baseCR4_Full |
515 			       SUPPORTED_40000baseSR4_Full |
516 			       SUPPORTED_40000baseLR4_Full))
517 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
518 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
519 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
520 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
521 			ETH_LINK_SPEED_FIXED);
522 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
523 		/* Link status changed. */
524 		dev->data->dev_link = dev_link;
525 		return 0;
526 	}
527 	/* Link status is still the same. */
528 	return -1;
529 }
530 
531 /**
532  * Retrieve physical link information (unlocked version using new ioctl).
533  *
534  * @param dev
535  *   Pointer to Ethernet device structure.
536  * @param wait_to_complete
537  *   Wait for request completion (ignored).
538  */
539 static int
540 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
541 {
542 	struct priv *priv = dev->data->dev_private;
543 	struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
544 	struct ifreq ifr;
545 	struct rte_eth_link dev_link;
546 	uint64_t sc;
547 
548 	(void)wait_to_complete;
549 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
550 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
551 		return -1;
552 	}
553 	memset(&dev_link, 0, sizeof(dev_link));
554 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
555 				(ifr.ifr_flags & IFF_RUNNING));
556 	ifr.ifr_data = (void *)&gcmd;
557 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
558 		DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
559 		      strerror(errno));
560 		return -1;
561 	}
562 	gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
563 
564 	alignas(struct ethtool_link_settings)
565 	uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
566 		     sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
567 	struct ethtool_link_settings *ecmd = (void *)data;
568 
569 	*ecmd = gcmd;
570 	ifr.ifr_data = (void *)ecmd;
571 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
572 		DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
573 		      strerror(errno));
574 		return -1;
575 	}
576 	dev_link.link_speed = ecmd->speed;
577 	sc = ecmd->link_mode_masks[0] |
578 		((uint64_t)ecmd->link_mode_masks[1] << 32);
579 	priv->link_speed_capa = 0;
580 	if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
581 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
582 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
583 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
584 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
585 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
586 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
587 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
588 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
589 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
590 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
591 		priv->link_speed_capa |= ETH_LINK_SPEED_20G;
592 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
593 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
594 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
595 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
596 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
597 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
598 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
599 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
600 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
601 		priv->link_speed_capa |= ETH_LINK_SPEED_56G;
602 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
603 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
604 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
605 		priv->link_speed_capa |= ETH_LINK_SPEED_25G;
606 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
607 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
608 		priv->link_speed_capa |= ETH_LINK_SPEED_50G;
609 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
610 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
611 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
612 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
613 		priv->link_speed_capa |= ETH_LINK_SPEED_100G;
614 	dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
615 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
616 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
617 				  ETH_LINK_SPEED_FIXED);
618 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
619 		/* Link status changed. */
620 		dev->data->dev_link = dev_link;
621 		return 0;
622 	}
623 	/* Link status is still the same. */
624 	return -1;
625 }
626 
627 /**
628  * Enable receiving and transmitting traffic.
629  *
630  * @param priv
631  *   Pointer to private structure.
632  */
633 static void
634 priv_link_start(struct priv *priv)
635 {
636 	struct rte_eth_dev *dev = priv->dev;
637 	int err;
638 
639 	dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
640 	dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
641 	err = priv_dev_traffic_enable(priv, dev);
642 	if (err)
643 		ERROR("%p: error occurred while configuring control flows: %s",
644 		      (void *)priv, strerror(err));
645 	err = priv_flow_start(priv, &priv->flows);
646 	if (err)
647 		ERROR("%p: error occurred while configuring flows: %s",
648 		      (void *)priv, strerror(err));
649 }
650 
651 /**
652  * Disable receiving and transmitting traffic.
653  *
654  * @param priv
655  *   Pointer to private structure.
656  */
657 static void
658 priv_link_stop(struct priv *priv)
659 {
660 	struct rte_eth_dev *dev = priv->dev;
661 
662 	priv_flow_stop(priv, &priv->flows);
663 	priv_dev_traffic_disable(priv, dev);
664 	dev->rx_pkt_burst = removed_rx_burst;
665 	dev->tx_pkt_burst = removed_tx_burst;
666 }
667 
668 /**
669  * Retrieve physical link information and update rx/tx_pkt_burst callbacks
670  * accordingly.
671  *
672  * @param priv
673  *   Pointer to private structure.
674  * @param wait_to_complete
675  *   Wait for request completion (ignored).
676  */
677 int
678 priv_link_update(struct priv *priv, int wait_to_complete)
679 {
680 	struct rte_eth_dev *dev = priv->dev;
681 	struct utsname utsname;
682 	int ver[3];
683 	int ret;
684 	struct rte_eth_link dev_link = dev->data->dev_link;
685 
686 	if (uname(&utsname) == -1 ||
687 	    sscanf(utsname.release, "%d.%d.%d",
688 		   &ver[0], &ver[1], &ver[2]) != 3 ||
689 	    KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
690 		ret = mlx5_link_update_unlocked_gset(dev, wait_to_complete);
691 	else
692 		ret = mlx5_link_update_unlocked_gs(dev, wait_to_complete);
693 	/* If lsc interrupt is disabled, should always be ready for traffic. */
694 	if (!dev->data->dev_conf.intr_conf.lsc) {
695 		priv_link_start(priv);
696 		return ret;
697 	}
698 	/* Re-select burst callbacks only if link status has been changed. */
699 	if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
700 		if (dev->data->dev_link.link_status == ETH_LINK_UP)
701 			priv_link_start(priv);
702 		else
703 			priv_link_stop(priv);
704 	}
705 	return ret;
706 }
707 
708 /**
709  * Querying the link status till it changes to the desired state.
710  * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS.
711  *
712  * @param priv
713  *   Pointer to private structure.
714  * @param status
715  *   Link desired status.
716  *
717  * @return
718  *   0 on success, negative errno value on failure.
719  */
720 int
721 priv_force_link_status_change(struct priv *priv, int status)
722 {
723 	int try = 0;
724 
725 	while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) {
726 		priv_link_update(priv, 0);
727 		if (priv->dev->data->dev_link.link_status == status)
728 			return 0;
729 		try++;
730 		sleep(1);
731 	}
732 	return -EAGAIN;
733 }
734 
735 /**
736  * DPDK callback to retrieve physical link information.
737  *
738  * @param dev
739  *   Pointer to Ethernet device structure.
740  * @param wait_to_complete
741  *   Wait for request completion (ignored).
742  */
743 int
744 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
745 {
746 	struct priv *priv = dev->data->dev_private;
747 	int ret;
748 
749 	priv_lock(priv);
750 	ret = priv_link_update(priv, wait_to_complete);
751 	priv_unlock(priv);
752 	return ret;
753 }
754 
755 /**
756  * DPDK callback to change the MTU.
757  *
758  * @param dev
759  *   Pointer to Ethernet device structure.
760  * @param in_mtu
761  *   New MTU.
762  *
763  * @return
764  *   0 on success, negative errno value on failure.
765  */
766 int
767 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
768 {
769 	struct priv *priv = dev->data->dev_private;
770 	uint16_t kern_mtu;
771 	int ret = 0;
772 
773 	priv_lock(priv);
774 	ret = priv_get_mtu(priv, &kern_mtu);
775 	if (ret)
776 		goto out;
777 	/* Set kernel interface MTU first. */
778 	ret = priv_set_mtu(priv, mtu);
779 	if (ret)
780 		goto out;
781 	ret = priv_get_mtu(priv, &kern_mtu);
782 	if (ret)
783 		goto out;
784 	if (kern_mtu == mtu) {
785 		priv->mtu = mtu;
786 		DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
787 	}
788 	priv_unlock(priv);
789 	return 0;
790 out:
791 	ret = errno;
792 	WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
793 	     strerror(ret));
794 	priv_unlock(priv);
795 	assert(ret >= 0);
796 	return -ret;
797 }
798 
799 /**
800  * DPDK callback to get flow control status.
801  *
802  * @param dev
803  *   Pointer to Ethernet device structure.
804  * @param[out] fc_conf
805  *   Flow control output buffer.
806  *
807  * @return
808  *   0 on success, negative errno value on failure.
809  */
810 int
811 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
812 {
813 	struct priv *priv = dev->data->dev_private;
814 	struct ifreq ifr;
815 	struct ethtool_pauseparam ethpause = {
816 		.cmd = ETHTOOL_GPAUSEPARAM
817 	};
818 	int ret;
819 
820 	ifr.ifr_data = (void *)&ethpause;
821 	priv_lock(priv);
822 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
823 		ret = errno;
824 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
825 		     " failed: %s",
826 		     strerror(ret));
827 		goto out;
828 	}
829 
830 	fc_conf->autoneg = ethpause.autoneg;
831 	if (ethpause.rx_pause && ethpause.tx_pause)
832 		fc_conf->mode = RTE_FC_FULL;
833 	else if (ethpause.rx_pause)
834 		fc_conf->mode = RTE_FC_RX_PAUSE;
835 	else if (ethpause.tx_pause)
836 		fc_conf->mode = RTE_FC_TX_PAUSE;
837 	else
838 		fc_conf->mode = RTE_FC_NONE;
839 	ret = 0;
840 
841 out:
842 	priv_unlock(priv);
843 	assert(ret >= 0);
844 	return -ret;
845 }
846 
847 /**
848  * DPDK callback to modify flow control parameters.
849  *
850  * @param dev
851  *   Pointer to Ethernet device structure.
852  * @param[in] fc_conf
853  *   Flow control parameters.
854  *
855  * @return
856  *   0 on success, negative errno value on failure.
857  */
858 int
859 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
860 {
861 	struct priv *priv = dev->data->dev_private;
862 	struct ifreq ifr;
863 	struct ethtool_pauseparam ethpause = {
864 		.cmd = ETHTOOL_SPAUSEPARAM
865 	};
866 	int ret;
867 
868 	ifr.ifr_data = (void *)&ethpause;
869 	ethpause.autoneg = fc_conf->autoneg;
870 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
871 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
872 		ethpause.rx_pause = 1;
873 	else
874 		ethpause.rx_pause = 0;
875 
876 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
877 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
878 		ethpause.tx_pause = 1;
879 	else
880 		ethpause.tx_pause = 0;
881 
882 	priv_lock(priv);
883 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
884 		ret = errno;
885 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
886 		     " failed: %s",
887 		     strerror(ret));
888 		goto out;
889 	}
890 	ret = 0;
891 
892 out:
893 	priv_unlock(priv);
894 	assert(ret >= 0);
895 	return -ret;
896 }
897 
898 /**
899  * Get PCI information from struct ibv_device.
900  *
901  * @param device
902  *   Pointer to Ethernet device structure.
903  * @param[out] pci_addr
904  *   PCI bus address output buffer.
905  *
906  * @return
907  *   0 on success, -1 on failure and errno is set.
908  */
909 int
910 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
911 			    struct rte_pci_addr *pci_addr)
912 {
913 	FILE *file;
914 	char line[32];
915 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
916 
917 	file = fopen(path, "rb");
918 	if (file == NULL)
919 		return -1;
920 	while (fgets(line, sizeof(line), file) == line) {
921 		size_t len = strlen(line);
922 		int ret;
923 
924 		/* Truncate long lines. */
925 		if (len == (sizeof(line) - 1))
926 			while (line[(len - 1)] != '\n') {
927 				ret = fgetc(file);
928 				if (ret == EOF)
929 					break;
930 				line[(len - 1)] = ret;
931 			}
932 		/* Extract information. */
933 		if (sscanf(line,
934 			   "PCI_SLOT_NAME="
935 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
936 			   &pci_addr->domain,
937 			   &pci_addr->bus,
938 			   &pci_addr->devid,
939 			   &pci_addr->function) == 4) {
940 			ret = 0;
941 			break;
942 		}
943 	}
944 	fclose(file);
945 	return 0;
946 }
947 
948 /**
949  * Update the link status.
950  *
951  * @param priv
952  *   Pointer to private structure.
953  *
954  * @return
955  *   Zero if the callback process can be called immediately.
956  */
957 static int
958 priv_link_status_update(struct priv *priv)
959 {
960 	struct rte_eth_link *link = &priv->dev->data->dev_link;
961 
962 	priv_link_update(priv, 0);
963 	if (((link->link_speed == 0) && link->link_status) ||
964 		((link->link_speed != 0) && !link->link_status)) {
965 		/*
966 		 * Inconsistent status. Event likely occurred before the
967 		 * kernel netdevice exposes the new status.
968 		 */
969 		if (!priv->pending_alarm) {
970 			priv->pending_alarm = 1;
971 			rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
972 					  mlx5_dev_link_status_handler,
973 					  priv->dev);
974 		}
975 		return 1;
976 	} else if (unlikely(priv->pending_alarm)) {
977 		/* Link interrupt occurred while alarm is already scheduled. */
978 		priv->pending_alarm = 0;
979 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
980 	}
981 	return 0;
982 }
983 
984 /**
985  * Device status handler.
986  *
987  * @param priv
988  *   Pointer to private structure.
989  * @param events
990  *   Pointer to event flags holder.
991  *
992  * @return
993  *   Events bitmap of callback process which can be called immediately.
994  */
995 static uint32_t
996 priv_dev_status_handler(struct priv *priv)
997 {
998 	struct ibv_async_event event;
999 	uint32_t ret = 0;
1000 
1001 	/* Read all message and acknowledge them. */
1002 	for (;;) {
1003 		if (mlx5_glue->get_async_event(priv->ctx, &event))
1004 			break;
1005 		if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1006 			event.event_type == IBV_EVENT_PORT_ERR) &&
1007 			(priv->dev->data->dev_conf.intr_conf.lsc == 1))
1008 			ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1009 		else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1010 			priv->dev->data->dev_conf.intr_conf.rmv == 1)
1011 			ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1012 		else
1013 			DEBUG("event type %d on port %d not handled",
1014 			      event.event_type, event.element.port_num);
1015 		mlx5_glue->ack_async_event(&event);
1016 	}
1017 	if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1018 		if (priv_link_status_update(priv))
1019 			ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1020 	return ret;
1021 }
1022 
1023 /**
1024  * Handle delayed link status event.
1025  *
1026  * @param arg
1027  *   Registered argument.
1028  */
1029 void
1030 mlx5_dev_link_status_handler(void *arg)
1031 {
1032 	struct rte_eth_dev *dev = arg;
1033 	struct priv *priv = dev->data->dev_private;
1034 	int ret;
1035 
1036 	while (!priv_trylock(priv)) {
1037 		/* Alarm is being canceled. */
1038 		if (priv->pending_alarm == 0)
1039 			return;
1040 		rte_pause();
1041 	}
1042 	priv->pending_alarm = 0;
1043 	ret = priv_link_status_update(priv);
1044 	priv_unlock(priv);
1045 	if (!ret)
1046 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1047 }
1048 
1049 /**
1050  * Handle interrupts from the NIC.
1051  *
1052  * @param[in] intr_handle
1053  *   Interrupt handler.
1054  * @param cb_arg
1055  *   Callback argument.
1056  */
1057 void
1058 mlx5_dev_interrupt_handler(void *cb_arg)
1059 {
1060 	struct rte_eth_dev *dev = cb_arg;
1061 	struct priv *priv = dev->data->dev_private;
1062 	uint32_t events;
1063 
1064 	priv_lock(priv);
1065 	events = priv_dev_status_handler(priv);
1066 	priv_unlock(priv);
1067 	if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1068 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1069 	if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1070 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1071 }
1072 
1073 /**
1074  * Handle interrupts from the socket.
1075  *
1076  * @param cb_arg
1077  *   Callback argument.
1078  */
1079 static void
1080 mlx5_dev_handler_socket(void *cb_arg)
1081 {
1082 	struct rte_eth_dev *dev = cb_arg;
1083 	struct priv *priv = dev->data->dev_private;
1084 
1085 	priv_lock(priv);
1086 	priv_socket_handle(priv);
1087 	priv_unlock(priv);
1088 }
1089 
1090 /**
1091  * Uninstall interrupt handler.
1092  *
1093  * @param priv
1094  *   Pointer to private structure.
1095  * @param dev
1096  *   Pointer to the rte_eth_dev structure.
1097  */
1098 void
1099 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1100 {
1101 	if (dev->data->dev_conf.intr_conf.lsc ||
1102 	    dev->data->dev_conf.intr_conf.rmv)
1103 		rte_intr_callback_unregister(&priv->intr_handle,
1104 					     mlx5_dev_interrupt_handler, dev);
1105 	if (priv->primary_socket)
1106 		rte_intr_callback_unregister(&priv->intr_handle_socket,
1107 					     mlx5_dev_handler_socket, dev);
1108 	if (priv->pending_alarm) {
1109 		priv->pending_alarm = 0;
1110 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1111 	}
1112 	priv->intr_handle.fd = 0;
1113 	priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1114 	priv->intr_handle_socket.fd = 0;
1115 	priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1116 }
1117 
1118 /**
1119  * Install interrupt handler.
1120  *
1121  * @param priv
1122  *   Pointer to private structure.
1123  * @param dev
1124  *   Pointer to the rte_eth_dev structure.
1125  */
1126 void
1127 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1128 {
1129 	int rc, flags;
1130 
1131 	assert(priv->ctx->async_fd > 0);
1132 	flags = fcntl(priv->ctx->async_fd, F_GETFL);
1133 	rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1134 	if (rc < 0) {
1135 		INFO("failed to change file descriptor async event queue");
1136 		dev->data->dev_conf.intr_conf.lsc = 0;
1137 		dev->data->dev_conf.intr_conf.rmv = 0;
1138 	}
1139 	if (dev->data->dev_conf.intr_conf.lsc ||
1140 	    dev->data->dev_conf.intr_conf.rmv) {
1141 		priv->intr_handle.fd = priv->ctx->async_fd;
1142 		priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1143 		rte_intr_callback_register(&priv->intr_handle,
1144 					   mlx5_dev_interrupt_handler, dev);
1145 	}
1146 
1147 	rc = priv_socket_init(priv);
1148 	if (!rc && priv->primary_socket) {
1149 		priv->intr_handle_socket.fd = priv->primary_socket;
1150 		priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1151 		rte_intr_callback_register(&priv->intr_handle_socket,
1152 					   mlx5_dev_handler_socket, dev);
1153 	}
1154 }
1155 
1156 /**
1157  * Change the link state (UP / DOWN).
1158  *
1159  * @param priv
1160  *   Pointer to private data structure.
1161  * @param up
1162  *   Nonzero for link up, otherwise link down.
1163  *
1164  * @return
1165  *   0 on success, errno value on failure.
1166  */
1167 static int
1168 priv_dev_set_link(struct priv *priv, int up)
1169 {
1170 	return priv_set_flags(priv, ~IFF_UP, up ? IFF_UP : ~IFF_UP);
1171 }
1172 
1173 /**
1174  * DPDK callback to bring the link DOWN.
1175  *
1176  * @param dev
1177  *   Pointer to Ethernet device structure.
1178  *
1179  * @return
1180  *   0 on success, errno value on failure.
1181  */
1182 int
1183 mlx5_set_link_down(struct rte_eth_dev *dev)
1184 {
1185 	struct priv *priv = dev->data->dev_private;
1186 	int err;
1187 
1188 	priv_lock(priv);
1189 	err = priv_dev_set_link(priv, 0);
1190 	priv_unlock(priv);
1191 	return err;
1192 }
1193 
1194 /**
1195  * DPDK callback to bring the link UP.
1196  *
1197  * @param dev
1198  *   Pointer to Ethernet device structure.
1199  *
1200  * @return
1201  *   0 on success, errno value on failure.
1202  */
1203 int
1204 mlx5_set_link_up(struct rte_eth_dev *dev)
1205 {
1206 	struct priv *priv = dev->data->dev_private;
1207 	int err;
1208 
1209 	priv_lock(priv);
1210 	err = priv_dev_set_link(priv, 1);
1211 	priv_unlock(priv);
1212 	return err;
1213 }
1214 
1215 /**
1216  * Configure the TX function to use.
1217  *
1218  * @param priv
1219  *   Pointer to private data structure.
1220  * @param dev
1221  *   Pointer to rte_eth_dev structure.
1222  *
1223  * @return
1224  *   Pointer to selected Tx burst function.
1225  */
1226 eth_tx_burst_t
1227 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1228 {
1229 	eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1230 	struct mlx5_dev_config *config = &priv->config;
1231 	uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1232 	int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1233 				    DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1234 				    DEV_TX_OFFLOAD_GRE_TNL_TSO));
1235 	int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1236 
1237 	assert(priv != NULL);
1238 	/* Select appropriate TX function. */
1239 	if (vlan_insert || tso)
1240 		return tx_pkt_burst;
1241 	if (config->mps == MLX5_MPW_ENHANCED) {
1242 		if (priv_check_vec_tx_support(priv, dev) > 0) {
1243 			if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1244 				tx_pkt_burst = mlx5_tx_burst_raw_vec;
1245 			else
1246 				tx_pkt_burst = mlx5_tx_burst_vec;
1247 			DEBUG("selected Enhanced MPW TX vectorized function");
1248 		} else {
1249 			tx_pkt_burst = mlx5_tx_burst_empw;
1250 			DEBUG("selected Enhanced MPW TX function");
1251 		}
1252 	} else if (config->mps && (config->txq_inline > 0)) {
1253 		tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1254 		DEBUG("selected MPW inline TX function");
1255 	} else if (config->mps) {
1256 		tx_pkt_burst = mlx5_tx_burst_mpw;
1257 		DEBUG("selected MPW TX function");
1258 	}
1259 	return tx_pkt_burst;
1260 }
1261 
1262 /**
1263  * Configure the RX function to use.
1264  *
1265  * @param priv
1266  *   Pointer to private data structure.
1267  * @param dev
1268  *   Pointer to rte_eth_dev structure.
1269  *
1270  * @return
1271  *   Pointer to selected Rx burst function.
1272  */
1273 eth_rx_burst_t
1274 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1275 {
1276 	eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1277 
1278 	assert(priv != NULL);
1279 	if (priv_check_vec_rx_support(priv) > 0) {
1280 		rx_pkt_burst = mlx5_rx_burst_vec;
1281 		DEBUG("selected RX vectorized function");
1282 	}
1283 	return rx_pkt_burst;
1284 }
1285 
1286 /**
1287  * Check if mlx5 device was removed.
1288  *
1289  * @param dev
1290  *   Pointer to Ethernet device structure.
1291  *
1292  * @return
1293  *   1 when device is removed, otherwise 0.
1294  */
1295 int
1296 mlx5_is_removed(struct rte_eth_dev *dev)
1297 {
1298 	struct ibv_device_attr device_attr;
1299 	struct priv *priv = dev->data->dev_private;
1300 
1301 	if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO)
1302 		return 1;
1303 	return 0;
1304 }
1305