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