xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision d06c608c013c36711e7a693b3fece68a93ae4369)
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 #include <stddef.h>
35 #include <assert.h>
36 #include <unistd.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <dirent.h>
43 #include <net/if.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <linux/ethtool.h>
48 #include <linux/sockios.h>
49 #include <fcntl.h>
50 
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-pedantic"
54 #endif
55 #include <rte_atomic.h>
56 #include <rte_ethdev.h>
57 #include <rte_mbuf.h>
58 #include <rte_common.h>
59 #include <rte_interrupts.h>
60 #include <rte_alarm.h>
61 #include <rte_malloc.h>
62 #ifdef PEDANTIC
63 #pragma GCC diagnostic error "-pedantic"
64 #endif
65 
66 #include "mlx5.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_utils.h"
69 
70 /**
71  * Return private structure associated with an Ethernet device.
72  *
73  * @param dev
74  *   Pointer to Ethernet device structure.
75  *
76  * @return
77  *   Pointer to private structure.
78  */
79 struct priv *
80 mlx5_get_priv(struct rte_eth_dev *dev)
81 {
82 	struct mlx5_secondary_data *sd;
83 
84 	if (!mlx5_is_secondary())
85 		return dev->data->dev_private;
86 	sd = &mlx5_secondary_data[dev->data->port_id];
87 	return sd->data.dev_private;
88 }
89 
90 /**
91  * Check if running as a secondary process.
92  *
93  * @return
94  *   Nonzero if running as a secondary process.
95  */
96 inline int
97 mlx5_is_secondary(void)
98 {
99 	return rte_eal_process_type() != RTE_PROC_PRIMARY;
100 }
101 
102 /**
103  * Get interface name from private structure.
104  *
105  * @param[in] priv
106  *   Pointer to private structure.
107  * @param[out] ifname
108  *   Interface name output buffer.
109  *
110  * @return
111  *   0 on success, -1 on failure and errno is set.
112  */
113 int
114 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
115 {
116 	DIR *dir;
117 	struct dirent *dent;
118 	unsigned int dev_type = 0;
119 	unsigned int dev_port_prev = ~0u;
120 	char match[IF_NAMESIZE] = "";
121 
122 	{
123 		MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
124 
125 		dir = opendir(path);
126 		if (dir == NULL)
127 			return -1;
128 	}
129 	while ((dent = readdir(dir)) != NULL) {
130 		char *name = dent->d_name;
131 		FILE *file;
132 		unsigned int dev_port;
133 		int r;
134 
135 		if ((name[0] == '.') &&
136 		    ((name[1] == '\0') ||
137 		     ((name[1] == '.') && (name[2] == '\0'))))
138 			continue;
139 
140 		MKSTR(path, "%s/device/net/%s/%s",
141 		      priv->ctx->device->ibdev_path, name,
142 		      (dev_type ? "dev_id" : "dev_port"));
143 
144 		file = fopen(path, "rb");
145 		if (file == NULL) {
146 			if (errno != ENOENT)
147 				continue;
148 			/*
149 			 * Switch to dev_id when dev_port does not exist as
150 			 * is the case with Linux kernel versions < 3.15.
151 			 */
152 try_dev_id:
153 			match[0] = '\0';
154 			if (dev_type)
155 				break;
156 			dev_type = 1;
157 			dev_port_prev = ~0u;
158 			rewinddir(dir);
159 			continue;
160 		}
161 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
162 		fclose(file);
163 		if (r != 1)
164 			continue;
165 		/*
166 		 * Switch to dev_id when dev_port returns the same value for
167 		 * all ports. May happen when using a MOFED release older than
168 		 * 3.0 with a Linux kernel >= 3.15.
169 		 */
170 		if (dev_port == dev_port_prev)
171 			goto try_dev_id;
172 		dev_port_prev = dev_port;
173 		if (dev_port == (priv->port - 1u))
174 			snprintf(match, sizeof(match), "%s", name);
175 	}
176 	closedir(dir);
177 	if (match[0] == '\0')
178 		return -1;
179 	strncpy(*ifname, match, sizeof(*ifname));
180 	return 0;
181 }
182 
183 /**
184  * Read from sysfs entry.
185  *
186  * @param[in] priv
187  *   Pointer to private structure.
188  * @param[in] entry
189  *   Entry name relative to sysfs path.
190  * @param[out] buf
191  *   Data output buffer.
192  * @param size
193  *   Buffer size.
194  *
195  * @return
196  *   0 on success, -1 on failure and errno is set.
197  */
198 static int
199 priv_sysfs_read(const struct priv *priv, const char *entry,
200 		char *buf, size_t size)
201 {
202 	char ifname[IF_NAMESIZE];
203 	FILE *file;
204 	int ret;
205 	int err;
206 
207 	if (priv_get_ifname(priv, &ifname))
208 		return -1;
209 
210 	MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
211 	      ifname, entry);
212 
213 	file = fopen(path, "rb");
214 	if (file == NULL)
215 		return -1;
216 	ret = fread(buf, 1, size, file);
217 	err = errno;
218 	if (((size_t)ret < size) && (ferror(file)))
219 		ret = -1;
220 	else
221 		ret = size;
222 	fclose(file);
223 	errno = err;
224 	return ret;
225 }
226 
227 /**
228  * Write to sysfs entry.
229  *
230  * @param[in] priv
231  *   Pointer to private structure.
232  * @param[in] entry
233  *   Entry name relative to sysfs path.
234  * @param[in] buf
235  *   Data buffer.
236  * @param size
237  *   Buffer size.
238  *
239  * @return
240  *   0 on success, -1 on failure and errno is set.
241  */
242 static int
243 priv_sysfs_write(const struct priv *priv, const char *entry,
244 		 char *buf, size_t size)
245 {
246 	char ifname[IF_NAMESIZE];
247 	FILE *file;
248 	int ret;
249 	int err;
250 
251 	if (priv_get_ifname(priv, &ifname))
252 		return -1;
253 
254 	MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
255 	      ifname, entry);
256 
257 	file = fopen(path, "wb");
258 	if (file == NULL)
259 		return -1;
260 	ret = fwrite(buf, 1, size, file);
261 	err = errno;
262 	if (((size_t)ret < size) || (ferror(file)))
263 		ret = -1;
264 	else
265 		ret = size;
266 	fclose(file);
267 	errno = err;
268 	return ret;
269 }
270 
271 /**
272  * Get unsigned long sysfs property.
273  *
274  * @param priv
275  *   Pointer to private structure.
276  * @param[in] name
277  *   Entry name relative to sysfs path.
278  * @param[out] value
279  *   Value output buffer.
280  *
281  * @return
282  *   0 on success, -1 on failure and errno is set.
283  */
284 static int
285 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
286 {
287 	int ret;
288 	unsigned long value_ret;
289 	char value_str[32];
290 
291 	ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
292 	if (ret == -1) {
293 		DEBUG("cannot read %s value from sysfs: %s",
294 		      name, strerror(errno));
295 		return -1;
296 	}
297 	value_str[ret] = '\0';
298 	errno = 0;
299 	value_ret = strtoul(value_str, NULL, 0);
300 	if (errno) {
301 		DEBUG("invalid %s value `%s': %s", name, value_str,
302 		      strerror(errno));
303 		return -1;
304 	}
305 	*value = value_ret;
306 	return 0;
307 }
308 
309 /**
310  * Set unsigned long sysfs property.
311  *
312  * @param priv
313  *   Pointer to private structure.
314  * @param[in] name
315  *   Entry name relative to sysfs path.
316  * @param value
317  *   Value to set.
318  *
319  * @return
320  *   0 on success, -1 on failure and errno is set.
321  */
322 static int
323 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
324 {
325 	int ret;
326 	MKSTR(value_str, "%lu", value);
327 
328 	ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
329 	if (ret == -1) {
330 		DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
331 		      name, value_str, value, strerror(errno));
332 		return -1;
333 	}
334 	return 0;
335 }
336 
337 /**
338  * Perform ifreq ioctl() on associated Ethernet device.
339  *
340  * @param[in] priv
341  *   Pointer to private structure.
342  * @param req
343  *   Request number to pass to ioctl().
344  * @param[out] ifr
345  *   Interface request structure output buffer.
346  *
347  * @return
348  *   0 on success, -1 on failure and errno is set.
349  */
350 int
351 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
352 {
353 	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
354 	int ret = -1;
355 
356 	if (sock == -1)
357 		return ret;
358 	if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
359 		ret = ioctl(sock, req, ifr);
360 	close(sock);
361 	return ret;
362 }
363 
364 /**
365  * Get device MTU.
366  *
367  * @param priv
368  *   Pointer to private structure.
369  * @param[out] mtu
370  *   MTU value output buffer.
371  *
372  * @return
373  *   0 on success, -1 on failure and errno is set.
374  */
375 int
376 priv_get_mtu(struct priv *priv, uint16_t *mtu)
377 {
378 	unsigned long ulong_mtu;
379 
380 	if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
381 		return -1;
382 	*mtu = ulong_mtu;
383 	return 0;
384 }
385 
386 /**
387  * Set device MTU.
388  *
389  * @param priv
390  *   Pointer to private structure.
391  * @param mtu
392  *   MTU value to set.
393  *
394  * @return
395  *   0 on success, -1 on failure and errno is set.
396  */
397 static int
398 priv_set_mtu(struct priv *priv, uint16_t mtu)
399 {
400 	return priv_set_sysfs_ulong(priv, "mtu", mtu);
401 }
402 
403 /**
404  * Set device flags.
405  *
406  * @param priv
407  *   Pointer to private structure.
408  * @param keep
409  *   Bitmask for flags that must remain untouched.
410  * @param flags
411  *   Bitmask for flags to modify.
412  *
413  * @return
414  *   0 on success, -1 on failure and errno is set.
415  */
416 int
417 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
418 {
419 	unsigned long tmp;
420 
421 	if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
422 		return -1;
423 	tmp &= keep;
424 	tmp |= flags;
425 	return priv_set_sysfs_ulong(priv, "flags", tmp);
426 }
427 
428 /**
429  * Ethernet device configuration.
430  *
431  * Prepare the driver for a given number of TX and RX queues.
432  *
433  * @param dev
434  *   Pointer to Ethernet device structure.
435  *
436  * @return
437  *   0 on success, errno value on failure.
438  */
439 static int
440 dev_configure(struct rte_eth_dev *dev)
441 {
442 	struct priv *priv = dev->data->dev_private;
443 	unsigned int rxqs_n = dev->data->nb_rx_queues;
444 	unsigned int txqs_n = dev->data->nb_tx_queues;
445 	unsigned int i;
446 	unsigned int j;
447 	unsigned int reta_idx_n;
448 
449 	priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
450 	priv->rxqs = (void *)dev->data->rx_queues;
451 	priv->txqs = (void *)dev->data->tx_queues;
452 	if (txqs_n != priv->txqs_n) {
453 		INFO("%p: TX queues number update: %u -> %u",
454 		     (void *)dev, priv->txqs_n, txqs_n);
455 		priv->txqs_n = txqs_n;
456 	}
457 	if (rxqs_n > priv->ind_table_max_size) {
458 		ERROR("cannot handle this many RX queues (%u)", rxqs_n);
459 		return EINVAL;
460 	}
461 	if (rxqs_n == priv->rxqs_n)
462 		return 0;
463 	INFO("%p: RX queues number update: %u -> %u",
464 	     (void *)dev, priv->rxqs_n, rxqs_n);
465 	priv->rxqs_n = rxqs_n;
466 	/* If the requested number of RX queues is not a power of two, use the
467 	 * maximum indirection table size for better balancing.
468 	 * The result is always rounded to the next power of two. */
469 	reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
470 				     priv->ind_table_max_size :
471 				     rxqs_n));
472 	if (priv_rss_reta_index_resize(priv, reta_idx_n))
473 		return ENOMEM;
474 	/* When the number of RX queues is not a power of two, the remaining
475 	 * table entries are padded with reused WQs and hashes are not spread
476 	 * uniformly. */
477 	for (i = 0, j = 0; (i != reta_idx_n); ++i) {
478 		(*priv->reta_idx)[i] = j;
479 		if (++j == rxqs_n)
480 			j = 0;
481 	}
482 	return 0;
483 }
484 
485 /**
486  * DPDK callback for Ethernet device configuration.
487  *
488  * @param dev
489  *   Pointer to Ethernet device structure.
490  *
491  * @return
492  *   0 on success, negative errno value on failure.
493  */
494 int
495 mlx5_dev_configure(struct rte_eth_dev *dev)
496 {
497 	struct priv *priv = dev->data->dev_private;
498 	int ret;
499 
500 	if (mlx5_is_secondary())
501 		return -E_RTE_SECONDARY;
502 
503 	priv_lock(priv);
504 	ret = dev_configure(dev);
505 	assert(ret >= 0);
506 	priv_unlock(priv);
507 	return -ret;
508 }
509 
510 /**
511  * DPDK callback to get information about the device.
512  *
513  * @param dev
514  *   Pointer to Ethernet device structure.
515  * @param[out] info
516  *   Info structure output buffer.
517  */
518 void
519 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
520 {
521 	struct priv *priv = mlx5_get_priv(dev);
522 	unsigned int max;
523 	char ifname[IF_NAMESIZE];
524 
525 	priv_lock(priv);
526 	/* FIXME: we should ask the device for these values. */
527 	info->min_rx_bufsize = 32;
528 	info->max_rx_pktlen = 65536;
529 	/*
530 	 * Since we need one CQ per QP, the limit is the minimum number
531 	 * between the two values.
532 	 */
533 	max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
534 	       priv->device_attr.max_qp : priv->device_attr.max_cq);
535 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
536 	if (max >= 65535)
537 		max = 65535;
538 	info->max_rx_queues = max;
539 	info->max_tx_queues = max;
540 	info->max_mac_addrs = RTE_DIM(priv->mac);
541 	info->rx_offload_capa =
542 		(priv->hw_csum ?
543 		 (DEV_RX_OFFLOAD_IPV4_CKSUM |
544 		  DEV_RX_OFFLOAD_UDP_CKSUM |
545 		  DEV_RX_OFFLOAD_TCP_CKSUM) :
546 		 0);
547 	info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
548 	if (priv->hw_csum)
549 		info->tx_offload_capa |=
550 			(DEV_TX_OFFLOAD_IPV4_CKSUM |
551 			 DEV_TX_OFFLOAD_UDP_CKSUM |
552 			 DEV_TX_OFFLOAD_TCP_CKSUM);
553 	if (priv_get_ifname(priv, &ifname) == 0)
554 		info->if_index = if_nametoindex(ifname);
555 	/* FIXME: RETA update/query API expects the callee to know the size of
556 	 * the indirection table, for this PMD the size varies depending on
557 	 * the number of RX queues, it becomes impossible to find the correct
558 	 * size if it is not fixed.
559 	 * The API should be updated to solve this problem. */
560 	info->reta_size = priv->ind_table_max_size;
561 	info->speed_capa =
562 			ETH_LINK_SPEED_1G |
563 			ETH_LINK_SPEED_10G |
564 			ETH_LINK_SPEED_20G |
565 			ETH_LINK_SPEED_25G |
566 			ETH_LINK_SPEED_40G |
567 			ETH_LINK_SPEED_50G |
568 			ETH_LINK_SPEED_56G |
569 			ETH_LINK_SPEED_100G;
570 	priv_unlock(priv);
571 }
572 
573 const uint32_t *
574 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
575 {
576 	static const uint32_t ptypes[] = {
577 		/* refers to rxq_cq_to_pkt_type() */
578 		RTE_PTYPE_L3_IPV4,
579 		RTE_PTYPE_L3_IPV6,
580 		RTE_PTYPE_INNER_L3_IPV4,
581 		RTE_PTYPE_INNER_L3_IPV6,
582 		RTE_PTYPE_UNKNOWN
583 
584 	};
585 
586 	if (dev->rx_pkt_burst == mlx5_rx_burst ||
587 	    dev->rx_pkt_burst == mlx5_rx_burst_sp)
588 		return ptypes;
589 	return NULL;
590 }
591 
592 /**
593  * DPDK callback to retrieve physical link information (unlocked version).
594  *
595  * @param dev
596  *   Pointer to Ethernet device structure.
597  * @param wait_to_complete
598  *   Wait for request completion (ignored).
599  */
600 static int
601 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
602 {
603 	struct priv *priv = mlx5_get_priv(dev);
604 	struct ethtool_cmd edata = {
605 		.cmd = ETHTOOL_GSET
606 	};
607 	struct ifreq ifr;
608 	struct rte_eth_link dev_link;
609 	int link_speed = 0;
610 
611 	(void)wait_to_complete;
612 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
613 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
614 		return -1;
615 	}
616 	memset(&dev_link, 0, sizeof(dev_link));
617 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
618 				(ifr.ifr_flags & IFF_RUNNING));
619 	ifr.ifr_data = (void *)&edata;
620 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
621 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
622 		     strerror(errno));
623 		return -1;
624 	}
625 	link_speed = ethtool_cmd_speed(&edata);
626 	if (link_speed == -1)
627 		dev_link.link_speed = 0;
628 	else
629 		dev_link.link_speed = link_speed;
630 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
631 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
632 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
633 			ETH_LINK_SPEED_FIXED);
634 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
635 		/* Link status changed. */
636 		dev->data->dev_link = dev_link;
637 		return 0;
638 	}
639 	/* Link status is still the same. */
640 	return -1;
641 }
642 
643 /**
644  * DPDK callback to retrieve physical link information.
645  *
646  * @param dev
647  *   Pointer to Ethernet device structure.
648  * @param wait_to_complete
649  *   Wait for request completion (ignored).
650  */
651 int
652 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
653 {
654 	struct priv *priv = mlx5_get_priv(dev);
655 	int ret;
656 
657 	priv_lock(priv);
658 	ret = mlx5_link_update_unlocked(dev, wait_to_complete);
659 	priv_unlock(priv);
660 	return ret;
661 }
662 
663 /**
664  * DPDK callback to change the MTU.
665  *
666  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
667  * received). Use this as a hint to enable/disable scattered packets support
668  * and improve performance when not needed.
669  * Since failure is not an option, reconfiguring queues on the fly is not
670  * recommended.
671  *
672  * @param dev
673  *   Pointer to Ethernet device structure.
674  * @param in_mtu
675  *   New MTU.
676  *
677  * @return
678  *   0 on success, negative errno value on failure.
679  */
680 int
681 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
682 {
683 	struct priv *priv = dev->data->dev_private;
684 	int ret = 0;
685 	unsigned int i;
686 	uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
687 		mlx5_rx_burst;
688 
689 	if (mlx5_is_secondary())
690 		return -E_RTE_SECONDARY;
691 
692 	priv_lock(priv);
693 	/* Set kernel interface MTU first. */
694 	if (priv_set_mtu(priv, mtu)) {
695 		ret = errno;
696 		WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
697 		     strerror(ret));
698 		goto out;
699 	} else
700 		DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
701 	priv->mtu = mtu;
702 	/* Temporarily replace RX handler with a fake one, assuming it has not
703 	 * been copied elsewhere. */
704 	dev->rx_pkt_burst = removed_rx_burst;
705 	/* Make sure everyone has left mlx5_rx_burst() and uses
706 	 * removed_rx_burst() instead. */
707 	rte_wmb();
708 	usleep(1000);
709 	/* Reconfigure each RX queue. */
710 	for (i = 0; (i != priv->rxqs_n); ++i) {
711 		struct rxq *rxq = (*priv->rxqs)[i];
712 		unsigned int max_frame_len;
713 		int sp;
714 
715 		if (rxq == NULL)
716 			continue;
717 		/* Calculate new maximum frame length according to MTU and
718 		 * toggle scattered support (sp) if necessary. */
719 		max_frame_len = (priv->mtu + ETHER_HDR_LEN +
720 				 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
721 		sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
722 		/* Provide new values to rxq_setup(). */
723 		dev->data->dev_conf.rxmode.jumbo_frame = sp;
724 		dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
725 		ret = rxq_rehash(dev, rxq);
726 		if (ret) {
727 			/* Force SP RX if that queue requires it and abort. */
728 			if (rxq->sp)
729 				rx_func = mlx5_rx_burst_sp;
730 			break;
731 		}
732 		/* Scattered burst function takes priority. */
733 		if (rxq->sp)
734 			rx_func = mlx5_rx_burst_sp;
735 	}
736 	/* Burst functions can now be called again. */
737 	rte_wmb();
738 	dev->rx_pkt_burst = rx_func;
739 out:
740 	priv_unlock(priv);
741 	assert(ret >= 0);
742 	return -ret;
743 }
744 
745 /**
746  * DPDK callback to get flow control status.
747  *
748  * @param dev
749  *   Pointer to Ethernet device structure.
750  * @param[out] fc_conf
751  *   Flow control output buffer.
752  *
753  * @return
754  *   0 on success, negative errno value on failure.
755  */
756 int
757 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
758 {
759 	struct priv *priv = dev->data->dev_private;
760 	struct ifreq ifr;
761 	struct ethtool_pauseparam ethpause = {
762 		.cmd = ETHTOOL_GPAUSEPARAM
763 	};
764 	int ret;
765 
766 	if (mlx5_is_secondary())
767 		return -E_RTE_SECONDARY;
768 
769 	ifr.ifr_data = (void *)&ethpause;
770 	priv_lock(priv);
771 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
772 		ret = errno;
773 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
774 		     " failed: %s",
775 		     strerror(ret));
776 		goto out;
777 	}
778 
779 	fc_conf->autoneg = ethpause.autoneg;
780 	if (ethpause.rx_pause && ethpause.tx_pause)
781 		fc_conf->mode = RTE_FC_FULL;
782 	else if (ethpause.rx_pause)
783 		fc_conf->mode = RTE_FC_RX_PAUSE;
784 	else if (ethpause.tx_pause)
785 		fc_conf->mode = RTE_FC_TX_PAUSE;
786 	else
787 		fc_conf->mode = RTE_FC_NONE;
788 	ret = 0;
789 
790 out:
791 	priv_unlock(priv);
792 	assert(ret >= 0);
793 	return -ret;
794 }
795 
796 /**
797  * DPDK callback to modify flow control parameters.
798  *
799  * @param dev
800  *   Pointer to Ethernet device structure.
801  * @param[in] fc_conf
802  *   Flow control parameters.
803  *
804  * @return
805  *   0 on success, negative errno value on failure.
806  */
807 int
808 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
809 {
810 	struct priv *priv = dev->data->dev_private;
811 	struct ifreq ifr;
812 	struct ethtool_pauseparam ethpause = {
813 		.cmd = ETHTOOL_SPAUSEPARAM
814 	};
815 	int ret;
816 
817 	if (mlx5_is_secondary())
818 		return -E_RTE_SECONDARY;
819 
820 	ifr.ifr_data = (void *)&ethpause;
821 	ethpause.autoneg = fc_conf->autoneg;
822 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
823 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
824 		ethpause.rx_pause = 1;
825 	else
826 		ethpause.rx_pause = 0;
827 
828 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
829 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
830 		ethpause.tx_pause = 1;
831 	else
832 		ethpause.tx_pause = 0;
833 
834 	priv_lock(priv);
835 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
836 		ret = errno;
837 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
838 		     " failed: %s",
839 		     strerror(ret));
840 		goto out;
841 	}
842 	ret = 0;
843 
844 out:
845 	priv_unlock(priv);
846 	assert(ret >= 0);
847 	return -ret;
848 }
849 
850 /**
851  * Get PCI information from struct ibv_device.
852  *
853  * @param device
854  *   Pointer to Ethernet device structure.
855  * @param[out] pci_addr
856  *   PCI bus address output buffer.
857  *
858  * @return
859  *   0 on success, -1 on failure and errno is set.
860  */
861 int
862 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
863 			    struct rte_pci_addr *pci_addr)
864 {
865 	FILE *file;
866 	char line[32];
867 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
868 
869 	file = fopen(path, "rb");
870 	if (file == NULL)
871 		return -1;
872 	while (fgets(line, sizeof(line), file) == line) {
873 		size_t len = strlen(line);
874 		int ret;
875 
876 		/* Truncate long lines. */
877 		if (len == (sizeof(line) - 1))
878 			while (line[(len - 1)] != '\n') {
879 				ret = fgetc(file);
880 				if (ret == EOF)
881 					break;
882 				line[(len - 1)] = ret;
883 			}
884 		/* Extract information. */
885 		if (sscanf(line,
886 			   "PCI_SLOT_NAME="
887 			   "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
888 			   &pci_addr->domain,
889 			   &pci_addr->bus,
890 			   &pci_addr->devid,
891 			   &pci_addr->function) == 4) {
892 			ret = 0;
893 			break;
894 		}
895 	}
896 	fclose(file);
897 	return 0;
898 }
899 
900 /**
901  * Link status handler.
902  *
903  * @param priv
904  *   Pointer to private structure.
905  * @param dev
906  *   Pointer to the rte_eth_dev structure.
907  *
908  * @return
909  *   Nonzero if the callback process can be called immediately.
910  */
911 static int
912 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
913 {
914 	struct ibv_async_event event;
915 	int port_change = 0;
916 	int ret = 0;
917 
918 	/* Read all message and acknowledge them. */
919 	for (;;) {
920 		if (ibv_get_async_event(priv->ctx, &event))
921 			break;
922 
923 		if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
924 		    event.event_type == IBV_EVENT_PORT_ERR)
925 			port_change = 1;
926 		else
927 			DEBUG("event type %d on port %d not handled",
928 			      event.event_type, event.element.port_num);
929 		ibv_ack_async_event(&event);
930 	}
931 
932 	if (port_change ^ priv->pending_alarm) {
933 		struct rte_eth_link *link = &dev->data->dev_link;
934 
935 		priv->pending_alarm = 0;
936 		mlx5_link_update_unlocked(dev, 0);
937 		if (((link->link_speed == 0) && link->link_status) ||
938 		    ((link->link_speed != 0) && !link->link_status)) {
939 			/* Inconsistent status, check again later. */
940 			priv->pending_alarm = 1;
941 			rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
942 					  mlx5_dev_link_status_handler,
943 					  dev);
944 		} else
945 			ret = 1;
946 	}
947 	return ret;
948 }
949 
950 /**
951  * Handle delayed link status event.
952  *
953  * @param arg
954  *   Registered argument.
955  */
956 void
957 mlx5_dev_link_status_handler(void *arg)
958 {
959 	struct rte_eth_dev *dev = arg;
960 	struct priv *priv = dev->data->dev_private;
961 	int ret;
962 
963 	priv_lock(priv);
964 	assert(priv->pending_alarm == 1);
965 	ret = priv_dev_link_status_handler(priv, dev);
966 	priv_unlock(priv);
967 	if (ret)
968 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
969 }
970 
971 /**
972  * Handle interrupts from the NIC.
973  *
974  * @param[in] intr_handle
975  *   Interrupt handler.
976  * @param cb_arg
977  *   Callback argument.
978  */
979 void
980 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
981 {
982 	struct rte_eth_dev *dev = cb_arg;
983 	struct priv *priv = dev->data->dev_private;
984 	int ret;
985 
986 	(void)intr_handle;
987 	priv_lock(priv);
988 	ret = priv_dev_link_status_handler(priv, dev);
989 	priv_unlock(priv);
990 	if (ret)
991 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
992 }
993 
994 /**
995  * Uninstall interrupt handler.
996  *
997  * @param priv
998  *   Pointer to private structure.
999  * @param dev
1000  *   Pointer to the rte_eth_dev structure.
1001  */
1002 void
1003 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1004 {
1005 	if (!dev->data->dev_conf.intr_conf.lsc)
1006 		return;
1007 	rte_intr_callback_unregister(&priv->intr_handle,
1008 				     mlx5_dev_interrupt_handler,
1009 				     dev);
1010 	if (priv->pending_alarm)
1011 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1012 	priv->pending_alarm = 0;
1013 	priv->intr_handle.fd = 0;
1014 	priv->intr_handle.type = 0;
1015 }
1016 
1017 /**
1018  * Install interrupt handler.
1019  *
1020  * @param priv
1021  *   Pointer to private structure.
1022  * @param dev
1023  *   Pointer to the rte_eth_dev structure.
1024  */
1025 void
1026 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1027 {
1028 	int rc, flags;
1029 
1030 	if (!dev->data->dev_conf.intr_conf.lsc)
1031 		return;
1032 	assert(priv->ctx->async_fd > 0);
1033 	flags = fcntl(priv->ctx->async_fd, F_GETFL);
1034 	rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1035 	if (rc < 0) {
1036 		INFO("failed to change file descriptor async event queue");
1037 		dev->data->dev_conf.intr_conf.lsc = 0;
1038 	} else {
1039 		priv->intr_handle.fd = priv->ctx->async_fd;
1040 		priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1041 		rte_intr_callback_register(&priv->intr_handle,
1042 					   mlx5_dev_interrupt_handler,
1043 					   dev);
1044 	}
1045 }
1046 
1047 /**
1048  * Change the link state (UP / DOWN).
1049  *
1050  * @param dev
1051  *   Pointer to Ethernet device structure.
1052  * @param up
1053  *   Nonzero for link up, otherwise link down.
1054  *
1055  * @return
1056  *   0 on success, errno value on failure.
1057  */
1058 static int
1059 priv_set_link(struct priv *priv, int up)
1060 {
1061 	struct rte_eth_dev *dev = priv->dev;
1062 	int err;
1063 	unsigned int i;
1064 
1065 	if (up) {
1066 		err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1067 		if (err)
1068 			return err;
1069 		for (i = 0; i < priv->rxqs_n; i++)
1070 			if ((*priv->rxqs)[i]->sp)
1071 				break;
1072 		/* Check if an sp queue exists.
1073 		 * Note: Some old frames might be received.
1074 		 */
1075 		if (i == priv->rxqs_n)
1076 			dev->rx_pkt_burst = mlx5_rx_burst;
1077 		else
1078 			dev->rx_pkt_burst = mlx5_rx_burst_sp;
1079 		dev->tx_pkt_burst = mlx5_tx_burst;
1080 	} else {
1081 		err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1082 		if (err)
1083 			return err;
1084 		dev->rx_pkt_burst = removed_rx_burst;
1085 		dev->tx_pkt_burst = removed_tx_burst;
1086 	}
1087 	return 0;
1088 }
1089 
1090 /**
1091  * DPDK callback to bring the link DOWN.
1092  *
1093  * @param dev
1094  *   Pointer to Ethernet device structure.
1095  *
1096  * @return
1097  *   0 on success, errno value on failure.
1098  */
1099 int
1100 mlx5_set_link_down(struct rte_eth_dev *dev)
1101 {
1102 	struct priv *priv = dev->data->dev_private;
1103 	int err;
1104 
1105 	priv_lock(priv);
1106 	err = priv_set_link(priv, 0);
1107 	priv_unlock(priv);
1108 	return err;
1109 }
1110 
1111 /**
1112  * DPDK callback to bring the link UP.
1113  *
1114  * @param dev
1115  *   Pointer to Ethernet device structure.
1116  *
1117  * @return
1118  *   0 on success, errno value on failure.
1119  */
1120 int
1121 mlx5_set_link_up(struct rte_eth_dev *dev)
1122 {
1123 	struct priv *priv = dev->data->dev_private;
1124 	int err;
1125 
1126 	priv_lock(priv);
1127 	err = priv_set_link(priv, 1);
1128 	priv_unlock(priv);
1129 	return err;
1130 }
1131 
1132 /**
1133  * Configure secondary process queues from a private data pointer (primary
1134  * or secondary) and update burst callbacks. Can take place only once.
1135  *
1136  * All queues must have been previously created by the primary process to
1137  * avoid undefined behavior.
1138  *
1139  * @param priv
1140  *   Private data pointer from either primary or secondary process.
1141  *
1142  * @return
1143  *   Private data pointer from secondary process, NULL in case of error.
1144  */
1145 struct priv *
1146 mlx5_secondary_data_setup(struct priv *priv)
1147 {
1148 	unsigned int port_id = 0;
1149 	struct mlx5_secondary_data *sd;
1150 	void **tx_queues;
1151 	void **rx_queues;
1152 	unsigned int nb_tx_queues;
1153 	unsigned int nb_rx_queues;
1154 	unsigned int i;
1155 
1156 	/* priv must be valid at this point. */
1157 	assert(priv != NULL);
1158 	/* priv->dev must also be valid but may point to local memory from
1159 	 * another process, possibly with the same address and must not
1160 	 * be dereferenced yet. */
1161 	assert(priv->dev != NULL);
1162 	/* Determine port ID by finding out where priv comes from. */
1163 	while (1) {
1164 		sd = &mlx5_secondary_data[port_id];
1165 		rte_spinlock_lock(&sd->lock);
1166 		/* Primary process? */
1167 		if (sd->primary_priv == priv)
1168 			break;
1169 		/* Secondary process? */
1170 		if (sd->data.dev_private == priv)
1171 			break;
1172 		rte_spinlock_unlock(&sd->lock);
1173 		if (++port_id == RTE_DIM(mlx5_secondary_data))
1174 			port_id = 0;
1175 	}
1176 	/* Switch to secondary private structure. If private data has already
1177 	 * been updated by another thread, there is nothing else to do. */
1178 	priv = sd->data.dev_private;
1179 	if (priv->dev->data == &sd->data)
1180 		goto end;
1181 	/* Sanity checks. Secondary private structure is supposed to point
1182 	 * to local eth_dev, itself still pointing to the shared device data
1183 	 * structure allocated by the primary process. */
1184 	assert(sd->shared_dev_data != &sd->data);
1185 	assert(sd->data.nb_tx_queues == 0);
1186 	assert(sd->data.tx_queues == NULL);
1187 	assert(sd->data.nb_rx_queues == 0);
1188 	assert(sd->data.rx_queues == NULL);
1189 	assert(priv != sd->primary_priv);
1190 	assert(priv->dev->data == sd->shared_dev_data);
1191 	assert(priv->txqs_n == 0);
1192 	assert(priv->txqs == NULL);
1193 	assert(priv->rxqs_n == 0);
1194 	assert(priv->rxqs == NULL);
1195 	nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1196 	nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1197 	/* Allocate local storage for queues. */
1198 	tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1199 				sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1200 				RTE_CACHE_LINE_SIZE);
1201 	rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1202 				sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1203 				RTE_CACHE_LINE_SIZE);
1204 	if (tx_queues == NULL || rx_queues == NULL)
1205 		goto error;
1206 	/* Lock to prevent control operations during setup. */
1207 	priv_lock(priv);
1208 	/* TX queues. */
1209 	for (i = 0; i != nb_tx_queues; ++i) {
1210 		struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1211 		struct txq *txq;
1212 
1213 		if (primary_txq == NULL)
1214 			continue;
1215 		txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
1216 					primary_txq->socket);
1217 		if (txq != NULL) {
1218 			if (txq_setup(priv->dev,
1219 				      txq,
1220 				      primary_txq->elts_n * MLX5_PMD_SGE_WR_N,
1221 				      primary_txq->socket,
1222 				      NULL) == 0) {
1223 				txq->stats.idx = primary_txq->stats.idx;
1224 				tx_queues[i] = txq;
1225 				continue;
1226 			}
1227 			rte_free(txq);
1228 		}
1229 		while (i) {
1230 			txq = tx_queues[--i];
1231 			txq_cleanup(txq);
1232 			rte_free(txq);
1233 		}
1234 		goto error;
1235 	}
1236 	/* RX queues. */
1237 	for (i = 0; i != nb_rx_queues; ++i) {
1238 		struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
1239 
1240 		if (primary_rxq == NULL)
1241 			continue;
1242 		/* Not supported yet. */
1243 		rx_queues[i] = NULL;
1244 	}
1245 	/* Update everything. */
1246 	priv->txqs = (void *)tx_queues;
1247 	priv->txqs_n = nb_tx_queues;
1248 	priv->rxqs = (void *)rx_queues;
1249 	priv->rxqs_n = nb_rx_queues;
1250 	sd->data.rx_queues = rx_queues;
1251 	sd->data.tx_queues = tx_queues;
1252 	sd->data.nb_rx_queues = nb_rx_queues;
1253 	sd->data.nb_tx_queues = nb_tx_queues;
1254 	sd->data.dev_link = sd->shared_dev_data->dev_link;
1255 	sd->data.mtu = sd->shared_dev_data->mtu;
1256 	memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1257 	       sizeof(sd->data.rx_queue_state));
1258 	memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1259 	       sizeof(sd->data.tx_queue_state));
1260 	sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1261 	/* Use local data from now on. */
1262 	rte_mb();
1263 	priv->dev->data = &sd->data;
1264 	rte_mb();
1265 	priv->dev->tx_pkt_burst = mlx5_tx_burst;
1266 	priv->dev->rx_pkt_burst = removed_rx_burst;
1267 	priv_unlock(priv);
1268 end:
1269 	/* More sanity checks. */
1270 	assert(priv->dev->tx_pkt_burst == mlx5_tx_burst);
1271 	assert(priv->dev->rx_pkt_burst == removed_rx_burst);
1272 	assert(priv->dev->data == &sd->data);
1273 	rte_spinlock_unlock(&sd->lock);
1274 	return priv;
1275 error:
1276 	priv_unlock(priv);
1277 	rte_free(tx_queues);
1278 	rte_free(rx_queues);
1279 	rte_spinlock_unlock(&sd->lock);
1280 	return NULL;
1281 }
1282