xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision 7fe24446e946c81235289d44236d08e22bff9fae)
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 
555 	if (use_app_rss_key &&
556 	    (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
557 	     rss_hash_default_key_len)) {
558 		/* MLX5 RSS only support 40bytes key. */
559 		return EINVAL;
560 	}
561 	priv->rss_conf.rss_key =
562 		rte_realloc(priv->rss_conf.rss_key,
563 			    rss_hash_default_key_len, 0);
564 	if (!priv->rss_conf.rss_key) {
565 		ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
566 		return ENOMEM;
567 	}
568 	memcpy(priv->rss_conf.rss_key,
569 	       use_app_rss_key ?
570 	       dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
571 	       rss_hash_default_key,
572 	       rss_hash_default_key_len);
573 	priv->rss_conf.rss_key_len = rss_hash_default_key_len;
574 	priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
575 	priv->rxqs = (void *)dev->data->rx_queues;
576 	priv->txqs = (void *)dev->data->tx_queues;
577 	if (txqs_n != priv->txqs_n) {
578 		INFO("%p: TX queues number update: %u -> %u",
579 		     (void *)dev, priv->txqs_n, txqs_n);
580 		priv->txqs_n = txqs_n;
581 	}
582 	if (rxqs_n > priv->config.ind_table_max_size) {
583 		ERROR("cannot handle this many RX queues (%u)", rxqs_n);
584 		return EINVAL;
585 	}
586 	if (rxqs_n == priv->rxqs_n)
587 		return 0;
588 	INFO("%p: RX queues number update: %u -> %u",
589 	     (void *)dev, priv->rxqs_n, rxqs_n);
590 	priv->rxqs_n = rxqs_n;
591 	/* If the requested number of RX queues is not a power of two, use the
592 	 * maximum indirection table size for better balancing.
593 	 * The result is always rounded to the next power of two. */
594 	reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
595 				     priv->config.ind_table_max_size :
596 				     rxqs_n));
597 	if (priv_rss_reta_index_resize(priv, reta_idx_n))
598 		return ENOMEM;
599 	/* When the number of RX queues is not a power of two, the remaining
600 	 * table entries are padded with reused WQs and hashes are not spread
601 	 * uniformly. */
602 	for (i = 0, j = 0; (i != reta_idx_n); ++i) {
603 		(*priv->reta_idx)[i] = j;
604 		if (++j == rxqs_n)
605 			j = 0;
606 	}
607 	return 0;
608 }
609 
610 /**
611  * DPDK callback for Ethernet device configuration.
612  *
613  * @param dev
614  *   Pointer to Ethernet device structure.
615  *
616  * @return
617  *   0 on success, negative errno value on failure.
618  */
619 int
620 mlx5_dev_configure(struct rte_eth_dev *dev)
621 {
622 	struct priv *priv = dev->data->dev_private;
623 	int ret;
624 
625 	priv_lock(priv);
626 	ret = dev_configure(dev);
627 	assert(ret >= 0);
628 	priv_unlock(priv);
629 	return -ret;
630 }
631 
632 /**
633  * DPDK callback to get information about the device.
634  *
635  * @param dev
636  *   Pointer to Ethernet device structure.
637  * @param[out] info
638  *   Info structure output buffer.
639  */
640 void
641 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
642 {
643 	struct priv *priv = dev->data->dev_private;
644 	struct mlx5_dev_config *config = &priv->config;
645 	unsigned int max;
646 	char ifname[IF_NAMESIZE];
647 
648 	info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
649 
650 	priv_lock(priv);
651 	/* FIXME: we should ask the device for these values. */
652 	info->min_rx_bufsize = 32;
653 	info->max_rx_pktlen = 65536;
654 	/*
655 	 * Since we need one CQ per QP, the limit is the minimum number
656 	 * between the two values.
657 	 */
658 	max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
659 		      priv->device_attr.orig_attr.max_qp);
660 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
661 	if (max >= 65535)
662 		max = 65535;
663 	info->max_rx_queues = max;
664 	info->max_tx_queues = max;
665 	info->max_mac_addrs = RTE_DIM(priv->mac);
666 	info->rx_offload_capa =
667 		(config->hw_csum ?
668 		 (DEV_RX_OFFLOAD_IPV4_CKSUM |
669 		  DEV_RX_OFFLOAD_UDP_CKSUM |
670 		  DEV_RX_OFFLOAD_TCP_CKSUM) :
671 		 0) |
672 		(priv->config.hw_vlan_strip ? DEV_RX_OFFLOAD_VLAN_STRIP : 0) |
673 		DEV_RX_OFFLOAD_TIMESTAMP;
674 
675 	if (!config->mps)
676 		info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
677 	if (config->hw_csum)
678 		info->tx_offload_capa |=
679 			(DEV_TX_OFFLOAD_IPV4_CKSUM |
680 			 DEV_TX_OFFLOAD_UDP_CKSUM |
681 			 DEV_TX_OFFLOAD_TCP_CKSUM);
682 	if (config->tso)
683 		info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
684 	if (config->tunnel_en)
685 		info->tx_offload_capa |= (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
686 					  DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
687 					  DEV_TX_OFFLOAD_GRE_TNL_TSO);
688 	if (priv_get_ifname(priv, &ifname) == 0)
689 		info->if_index = if_nametoindex(ifname);
690 	info->reta_size = priv->reta_idx_n ?
691 		priv->reta_idx_n : config->ind_table_max_size;
692 	info->hash_key_size = priv->rss_conf.rss_key_len;
693 	info->speed_capa = priv->link_speed_capa;
694 	priv_unlock(priv);
695 }
696 
697 const uint32_t *
698 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
699 {
700 	static const uint32_t ptypes[] = {
701 		/* refers to rxq_cq_to_pkt_type() */
702 		RTE_PTYPE_L2_ETHER,
703 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
704 		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
705 		RTE_PTYPE_L4_NONFRAG,
706 		RTE_PTYPE_L4_FRAG,
707 		RTE_PTYPE_L4_TCP,
708 		RTE_PTYPE_L4_UDP,
709 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
710 		RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
711 		RTE_PTYPE_INNER_L4_NONFRAG,
712 		RTE_PTYPE_INNER_L4_FRAG,
713 		RTE_PTYPE_INNER_L4_TCP,
714 		RTE_PTYPE_INNER_L4_UDP,
715 		RTE_PTYPE_UNKNOWN
716 	};
717 
718 	if (dev->rx_pkt_burst == mlx5_rx_burst ||
719 	    dev->rx_pkt_burst == mlx5_rx_burst_vec)
720 		return ptypes;
721 	return NULL;
722 }
723 
724 /**
725  * DPDK callback to retrieve physical link information.
726  *
727  * @param dev
728  *   Pointer to Ethernet device structure.
729  * @param wait_to_complete
730  *   Wait for request completion (ignored).
731  */
732 static int
733 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
734 {
735 	struct priv *priv = dev->data->dev_private;
736 	struct ethtool_cmd edata = {
737 		.cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
738 	};
739 	struct ifreq ifr;
740 	struct rte_eth_link dev_link;
741 	int link_speed = 0;
742 
743 	/* priv_lock() is not taken to allow concurrent calls. */
744 
745 	(void)wait_to_complete;
746 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
747 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
748 		return -1;
749 	}
750 	memset(&dev_link, 0, sizeof(dev_link));
751 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
752 				(ifr.ifr_flags & IFF_RUNNING));
753 	ifr.ifr_data = (void *)&edata;
754 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
755 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
756 		     strerror(errno));
757 		return -1;
758 	}
759 	link_speed = ethtool_cmd_speed(&edata);
760 	if (link_speed == -1)
761 		dev_link.link_speed = 0;
762 	else
763 		dev_link.link_speed = link_speed;
764 	priv->link_speed_capa = 0;
765 	if (edata.supported & SUPPORTED_Autoneg)
766 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
767 	if (edata.supported & (SUPPORTED_1000baseT_Full |
768 			       SUPPORTED_1000baseKX_Full))
769 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
770 	if (edata.supported & SUPPORTED_10000baseKR_Full)
771 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
772 	if (edata.supported & (SUPPORTED_40000baseKR4_Full |
773 			       SUPPORTED_40000baseCR4_Full |
774 			       SUPPORTED_40000baseSR4_Full |
775 			       SUPPORTED_40000baseLR4_Full))
776 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
777 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
778 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
779 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
780 			ETH_LINK_SPEED_FIXED);
781 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
782 		/* Link status changed. */
783 		dev->data->dev_link = dev_link;
784 		return 0;
785 	}
786 	/* Link status is still the same. */
787 	return -1;
788 }
789 
790 /**
791  * Retrieve physical link information (unlocked version using new ioctl).
792  *
793  * @param dev
794  *   Pointer to Ethernet device structure.
795  * @param wait_to_complete
796  *   Wait for request completion (ignored).
797  */
798 static int
799 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
800 {
801 	struct priv *priv = dev->data->dev_private;
802 	struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
803 	struct ifreq ifr;
804 	struct rte_eth_link dev_link;
805 	uint64_t sc;
806 
807 	(void)wait_to_complete;
808 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
809 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
810 		return -1;
811 	}
812 	memset(&dev_link, 0, sizeof(dev_link));
813 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
814 				(ifr.ifr_flags & IFF_RUNNING));
815 	ifr.ifr_data = (void *)&gcmd;
816 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
817 		DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
818 		      strerror(errno));
819 		return -1;
820 	}
821 	gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
822 
823 	alignas(struct ethtool_link_settings)
824 	uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
825 		     sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
826 	struct ethtool_link_settings *ecmd = (void *)data;
827 
828 	*ecmd = gcmd;
829 	ifr.ifr_data = (void *)ecmd;
830 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
831 		DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
832 		      strerror(errno));
833 		return -1;
834 	}
835 	dev_link.link_speed = ecmd->speed;
836 	sc = ecmd->link_mode_masks[0] |
837 		((uint64_t)ecmd->link_mode_masks[1] << 32);
838 	priv->link_speed_capa = 0;
839 	if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
840 		priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
841 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
842 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
843 		priv->link_speed_capa |= ETH_LINK_SPEED_1G;
844 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
845 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
846 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
847 		priv->link_speed_capa |= ETH_LINK_SPEED_10G;
848 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
849 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
850 		priv->link_speed_capa |= ETH_LINK_SPEED_20G;
851 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
852 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
853 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
854 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
855 		priv->link_speed_capa |= ETH_LINK_SPEED_40G;
856 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
857 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
858 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
859 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
860 		priv->link_speed_capa |= ETH_LINK_SPEED_56G;
861 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
862 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
863 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
864 		priv->link_speed_capa |= ETH_LINK_SPEED_25G;
865 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
866 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
867 		priv->link_speed_capa |= ETH_LINK_SPEED_50G;
868 	if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
869 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
870 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
871 		  MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
872 		priv->link_speed_capa |= ETH_LINK_SPEED_100G;
873 	dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
874 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
875 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
876 				  ETH_LINK_SPEED_FIXED);
877 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
878 		/* Link status changed. */
879 		dev->data->dev_link = dev_link;
880 		return 0;
881 	}
882 	/* Link status is still the same. */
883 	return -1;
884 }
885 
886 /**
887  * DPDK callback to retrieve physical link information.
888  *
889  * @param dev
890  *   Pointer to Ethernet device structure.
891  * @param wait_to_complete
892  *   Wait for request completion (ignored).
893  */
894 int
895 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
896 {
897 	struct utsname utsname;
898 	int ver[3];
899 
900 	if (uname(&utsname) == -1 ||
901 	    sscanf(utsname.release, "%d.%d.%d",
902 		   &ver[0], &ver[1], &ver[2]) != 3 ||
903 	    KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
904 		return mlx5_link_update_unlocked_gset(dev, wait_to_complete);
905 	return mlx5_link_update_unlocked_gs(dev, wait_to_complete);
906 }
907 
908 /**
909  * DPDK callback to change the MTU.
910  *
911  * @param dev
912  *   Pointer to Ethernet device structure.
913  * @param in_mtu
914  *   New MTU.
915  *
916  * @return
917  *   0 on success, negative errno value on failure.
918  */
919 int
920 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
921 {
922 	struct priv *priv = dev->data->dev_private;
923 	uint16_t kern_mtu;
924 	int ret = 0;
925 
926 	priv_lock(priv);
927 	ret = priv_get_mtu(priv, &kern_mtu);
928 	if (ret)
929 		goto out;
930 	/* Set kernel interface MTU first. */
931 	ret = priv_set_mtu(priv, mtu);
932 	if (ret)
933 		goto out;
934 	ret = priv_get_mtu(priv, &kern_mtu);
935 	if (ret)
936 		goto out;
937 	if (kern_mtu == mtu) {
938 		priv->mtu = mtu;
939 		DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
940 	}
941 	priv_unlock(priv);
942 	return 0;
943 out:
944 	ret = errno;
945 	WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
946 	     strerror(ret));
947 	priv_unlock(priv);
948 	assert(ret >= 0);
949 	return -ret;
950 }
951 
952 /**
953  * DPDK callback to get flow control status.
954  *
955  * @param dev
956  *   Pointer to Ethernet device structure.
957  * @param[out] fc_conf
958  *   Flow control output buffer.
959  *
960  * @return
961  *   0 on success, negative errno value on failure.
962  */
963 int
964 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
965 {
966 	struct priv *priv = dev->data->dev_private;
967 	struct ifreq ifr;
968 	struct ethtool_pauseparam ethpause = {
969 		.cmd = ETHTOOL_GPAUSEPARAM
970 	};
971 	int ret;
972 
973 	ifr.ifr_data = (void *)&ethpause;
974 	priv_lock(priv);
975 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
976 		ret = errno;
977 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
978 		     " failed: %s",
979 		     strerror(ret));
980 		goto out;
981 	}
982 
983 	fc_conf->autoneg = ethpause.autoneg;
984 	if (ethpause.rx_pause && ethpause.tx_pause)
985 		fc_conf->mode = RTE_FC_FULL;
986 	else if (ethpause.rx_pause)
987 		fc_conf->mode = RTE_FC_RX_PAUSE;
988 	else if (ethpause.tx_pause)
989 		fc_conf->mode = RTE_FC_TX_PAUSE;
990 	else
991 		fc_conf->mode = RTE_FC_NONE;
992 	ret = 0;
993 
994 out:
995 	priv_unlock(priv);
996 	assert(ret >= 0);
997 	return -ret;
998 }
999 
1000 /**
1001  * DPDK callback to modify flow control parameters.
1002  *
1003  * @param dev
1004  *   Pointer to Ethernet device structure.
1005  * @param[in] fc_conf
1006  *   Flow control parameters.
1007  *
1008  * @return
1009  *   0 on success, negative errno value on failure.
1010  */
1011 int
1012 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1013 {
1014 	struct priv *priv = dev->data->dev_private;
1015 	struct ifreq ifr;
1016 	struct ethtool_pauseparam ethpause = {
1017 		.cmd = ETHTOOL_SPAUSEPARAM
1018 	};
1019 	int ret;
1020 
1021 	ifr.ifr_data = (void *)&ethpause;
1022 	ethpause.autoneg = fc_conf->autoneg;
1023 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1024 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
1025 		ethpause.rx_pause = 1;
1026 	else
1027 		ethpause.rx_pause = 0;
1028 
1029 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1030 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
1031 		ethpause.tx_pause = 1;
1032 	else
1033 		ethpause.tx_pause = 0;
1034 
1035 	priv_lock(priv);
1036 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1037 		ret = errno;
1038 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1039 		     " failed: %s",
1040 		     strerror(ret));
1041 		goto out;
1042 	}
1043 	ret = 0;
1044 
1045 out:
1046 	priv_unlock(priv);
1047 	assert(ret >= 0);
1048 	return -ret;
1049 }
1050 
1051 /**
1052  * Get PCI information from struct ibv_device.
1053  *
1054  * @param device
1055  *   Pointer to Ethernet device structure.
1056  * @param[out] pci_addr
1057  *   PCI bus address output buffer.
1058  *
1059  * @return
1060  *   0 on success, -1 on failure and errno is set.
1061  */
1062 int
1063 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1064 			    struct rte_pci_addr *pci_addr)
1065 {
1066 	FILE *file;
1067 	char line[32];
1068 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
1069 
1070 	file = fopen(path, "rb");
1071 	if (file == NULL)
1072 		return -1;
1073 	while (fgets(line, sizeof(line), file) == line) {
1074 		size_t len = strlen(line);
1075 		int ret;
1076 
1077 		/* Truncate long lines. */
1078 		if (len == (sizeof(line) - 1))
1079 			while (line[(len - 1)] != '\n') {
1080 				ret = fgetc(file);
1081 				if (ret == EOF)
1082 					break;
1083 				line[(len - 1)] = ret;
1084 			}
1085 		/* Extract information. */
1086 		if (sscanf(line,
1087 			   "PCI_SLOT_NAME="
1088 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1089 			   &pci_addr->domain,
1090 			   &pci_addr->bus,
1091 			   &pci_addr->devid,
1092 			   &pci_addr->function) == 4) {
1093 			ret = 0;
1094 			break;
1095 		}
1096 	}
1097 	fclose(file);
1098 	return 0;
1099 }
1100 
1101 /**
1102  * Update the link status.
1103  *
1104  * @param priv
1105  *   Pointer to private structure.
1106  *
1107  * @return
1108  *   Zero if the callback process can be called immediately.
1109  */
1110 static int
1111 priv_link_status_update(struct priv *priv)
1112 {
1113 	struct rte_eth_link *link = &priv->dev->data->dev_link;
1114 
1115 	mlx5_link_update(priv->dev, 0);
1116 	if (((link->link_speed == 0) && link->link_status) ||
1117 		((link->link_speed != 0) && !link->link_status)) {
1118 		/*
1119 		 * Inconsistent status. Event likely occurred before the
1120 		 * kernel netdevice exposes the new status.
1121 		 */
1122 		if (!priv->pending_alarm) {
1123 			priv->pending_alarm = 1;
1124 			rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1125 					  mlx5_dev_link_status_handler,
1126 					  priv->dev);
1127 		}
1128 		return 1;
1129 	} else if (unlikely(priv->pending_alarm)) {
1130 		/* Link interrupt occurred while alarm is already scheduled. */
1131 		priv->pending_alarm = 0;
1132 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1133 	}
1134 	return 0;
1135 }
1136 
1137 /**
1138  * Device status handler.
1139  *
1140  * @param priv
1141  *   Pointer to private structure.
1142  * @param events
1143  *   Pointer to event flags holder.
1144  *
1145  * @return
1146  *   Events bitmap of callback process which can be called immediately.
1147  */
1148 static uint32_t
1149 priv_dev_status_handler(struct priv *priv)
1150 {
1151 	struct ibv_async_event event;
1152 	uint32_t ret = 0;
1153 
1154 	/* Read all message and acknowledge them. */
1155 	for (;;) {
1156 		if (ibv_get_async_event(priv->ctx, &event))
1157 			break;
1158 		if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1159 			event.event_type == IBV_EVENT_PORT_ERR) &&
1160 			(priv->dev->data->dev_conf.intr_conf.lsc == 1))
1161 			ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1162 		else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1163 			priv->dev->data->dev_conf.intr_conf.rmv == 1)
1164 			ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1165 		else
1166 			DEBUG("event type %d on port %d not handled",
1167 			      event.event_type, event.element.port_num);
1168 		ibv_ack_async_event(&event);
1169 	}
1170 	if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1171 		if (priv_link_status_update(priv))
1172 			ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1173 	return ret;
1174 }
1175 
1176 /**
1177  * Handle delayed link status event.
1178  *
1179  * @param arg
1180  *   Registered argument.
1181  */
1182 void
1183 mlx5_dev_link_status_handler(void *arg)
1184 {
1185 	struct rte_eth_dev *dev = arg;
1186 	struct priv *priv = dev->data->dev_private;
1187 	int ret;
1188 
1189 	priv_lock(priv);
1190 	assert(priv->pending_alarm == 1);
1191 	priv->pending_alarm = 0;
1192 	ret = priv_link_status_update(priv);
1193 	priv_unlock(priv);
1194 	if (!ret)
1195 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1196 					      NULL);
1197 }
1198 
1199 /**
1200  * Handle interrupts from the NIC.
1201  *
1202  * @param[in] intr_handle
1203  *   Interrupt handler.
1204  * @param cb_arg
1205  *   Callback argument.
1206  */
1207 void
1208 mlx5_dev_interrupt_handler(void *cb_arg)
1209 {
1210 	struct rte_eth_dev *dev = cb_arg;
1211 	struct priv *priv = dev->data->dev_private;
1212 	uint32_t events;
1213 
1214 	priv_lock(priv);
1215 	events = priv_dev_status_handler(priv);
1216 	priv_unlock(priv);
1217 	if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1218 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
1219 					      NULL);
1220 	if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1221 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL,
1222 					      NULL);
1223 }
1224 
1225 /**
1226  * Handle interrupts from the socket.
1227  *
1228  * @param cb_arg
1229  *   Callback argument.
1230  */
1231 static void
1232 mlx5_dev_handler_socket(void *cb_arg)
1233 {
1234 	struct rte_eth_dev *dev = cb_arg;
1235 	struct priv *priv = dev->data->dev_private;
1236 
1237 	priv_lock(priv);
1238 	priv_socket_handle(priv);
1239 	priv_unlock(priv);
1240 }
1241 
1242 /**
1243  * Uninstall interrupt handler.
1244  *
1245  * @param priv
1246  *   Pointer to private structure.
1247  * @param dev
1248  *   Pointer to the rte_eth_dev structure.
1249  */
1250 void
1251 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1252 {
1253 	if (dev->data->dev_conf.intr_conf.lsc ||
1254 	    dev->data->dev_conf.intr_conf.rmv)
1255 		rte_intr_callback_unregister(&priv->intr_handle,
1256 					     mlx5_dev_interrupt_handler, dev);
1257 	if (priv->primary_socket)
1258 		rte_intr_callback_unregister(&priv->intr_handle_socket,
1259 					     mlx5_dev_handler_socket, dev);
1260 	if (priv->pending_alarm)
1261 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1262 	priv->pending_alarm = 0;
1263 	priv->intr_handle.fd = 0;
1264 	priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1265 	priv->intr_handle_socket.fd = 0;
1266 	priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1267 }
1268 
1269 /**
1270  * Install interrupt handler.
1271  *
1272  * @param priv
1273  *   Pointer to private structure.
1274  * @param dev
1275  *   Pointer to the rte_eth_dev structure.
1276  */
1277 void
1278 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1279 {
1280 	int rc, flags;
1281 
1282 	assert(priv->ctx->async_fd > 0);
1283 	flags = fcntl(priv->ctx->async_fd, F_GETFL);
1284 	rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1285 	if (rc < 0) {
1286 		INFO("failed to change file descriptor async event queue");
1287 		dev->data->dev_conf.intr_conf.lsc = 0;
1288 		dev->data->dev_conf.intr_conf.rmv = 0;
1289 	}
1290 	if (dev->data->dev_conf.intr_conf.lsc ||
1291 	    dev->data->dev_conf.intr_conf.rmv) {
1292 		priv->intr_handle.fd = priv->ctx->async_fd;
1293 		priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1294 		rte_intr_callback_register(&priv->intr_handle,
1295 					   mlx5_dev_interrupt_handler, dev);
1296 	}
1297 
1298 	rc = priv_socket_init(priv);
1299 	if (!rc && priv->primary_socket) {
1300 		priv->intr_handle_socket.fd = priv->primary_socket;
1301 		priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1302 		rte_intr_callback_register(&priv->intr_handle_socket,
1303 					   mlx5_dev_handler_socket, dev);
1304 	}
1305 }
1306 
1307 /**
1308  * Change the link state (UP / DOWN).
1309  *
1310  * @param priv
1311  *   Pointer to private data structure.
1312  * @param dev
1313  *   Pointer to rte_eth_dev structure.
1314  * @param up
1315  *   Nonzero for link up, otherwise link down.
1316  *
1317  * @return
1318  *   0 on success, errno value on failure.
1319  */
1320 static int
1321 priv_dev_set_link(struct priv *priv, struct rte_eth_dev *dev, int up)
1322 {
1323 	int err;
1324 
1325 	if (up) {
1326 		err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1327 		if (err)
1328 			return err;
1329 		dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
1330 		dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
1331 	} else {
1332 		err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1333 		if (err)
1334 			return err;
1335 		dev->rx_pkt_burst = removed_rx_burst;
1336 		dev->tx_pkt_burst = removed_tx_burst;
1337 	}
1338 	return 0;
1339 }
1340 
1341 /**
1342  * DPDK callback to bring the link DOWN.
1343  *
1344  * @param dev
1345  *   Pointer to Ethernet device structure.
1346  *
1347  * @return
1348  *   0 on success, errno value on failure.
1349  */
1350 int
1351 mlx5_set_link_down(struct rte_eth_dev *dev)
1352 {
1353 	struct priv *priv = dev->data->dev_private;
1354 	int err;
1355 
1356 	priv_lock(priv);
1357 	err = priv_dev_set_link(priv, dev, 0);
1358 	priv_unlock(priv);
1359 	return err;
1360 }
1361 
1362 /**
1363  * DPDK callback to bring the link UP.
1364  *
1365  * @param dev
1366  *   Pointer to Ethernet device structure.
1367  *
1368  * @return
1369  *   0 on success, errno value on failure.
1370  */
1371 int
1372 mlx5_set_link_up(struct rte_eth_dev *dev)
1373 {
1374 	struct priv *priv = dev->data->dev_private;
1375 	int err;
1376 
1377 	priv_lock(priv);
1378 	err = priv_dev_set_link(priv, dev, 1);
1379 	priv_unlock(priv);
1380 	return err;
1381 }
1382 
1383 /**
1384  * Configure the TX function to use.
1385  *
1386  * @param priv
1387  *   Pointer to private data structure.
1388  * @param dev
1389  *   Pointer to rte_eth_dev structure.
1390  *
1391  * @return
1392  *   Pointer to selected Tx burst function.
1393  */
1394 eth_tx_burst_t
1395 priv_select_tx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1396 {
1397 	eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1398 	struct mlx5_dev_config *config = &priv->config;
1399 
1400 	assert(priv != NULL);
1401 	/* Select appropriate TX function. */
1402 	if (config->mps == MLX5_MPW_ENHANCED) {
1403 		if (priv_check_vec_tx_support(priv) > 0) {
1404 			if (priv_check_raw_vec_tx_support(priv) > 0)
1405 				tx_pkt_burst = mlx5_tx_burst_raw_vec;
1406 			else
1407 				tx_pkt_burst = mlx5_tx_burst_vec;
1408 			DEBUG("selected Enhanced MPW TX vectorized function");
1409 		} else {
1410 			tx_pkt_burst = mlx5_tx_burst_empw;
1411 			DEBUG("selected Enhanced MPW TX function");
1412 		}
1413 	} else if (config->mps && (config->txq_inline > 0)) {
1414 		tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1415 		DEBUG("selected MPW inline TX function");
1416 	} else if (config->mps) {
1417 		tx_pkt_burst = mlx5_tx_burst_mpw;
1418 		DEBUG("selected MPW TX function");
1419 	}
1420 	return tx_pkt_burst;
1421 }
1422 
1423 /**
1424  * Configure the RX function to use.
1425  *
1426  * @param priv
1427  *   Pointer to private data structure.
1428  * @param dev
1429  *   Pointer to rte_eth_dev structure.
1430  *
1431  * @return
1432  *   Pointer to selected Rx burst function.
1433  */
1434 eth_rx_burst_t
1435 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1436 {
1437 	eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1438 
1439 	assert(priv != NULL);
1440 	if (priv_check_vec_rx_support(priv) > 0) {
1441 		rx_pkt_burst = mlx5_rx_burst_vec;
1442 		DEBUG("selected RX vectorized function");
1443 	}
1444 	return rx_pkt_burst;
1445 }
1446