xref: /dpdk/drivers/net/mlx5/mlx5_ethdev.c (revision 230189d9ff22660ba147eda7933ebb21bb882bdc)
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  * Return the number of active VFs for the current device.
366  *
367  * @param[in] priv
368  *   Pointer to private structure.
369  * @param[out] num_vfs
370  *   Number of active VFs.
371  *
372  * @return
373  *   0 on success, -1 on failure and errno is set.
374  */
375 int
376 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
377 {
378 	/* The sysfs entry name depends on the operating system. */
379 	const char **name = (const char *[]){
380 		"device/sriov_numvfs",
381 		"device/mlx5_num_vfs",
382 		NULL,
383 	};
384 	int ret;
385 
386 	do {
387 		unsigned long ulong_num_vfs;
388 
389 		ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
390 		if (!ret)
391 			*num_vfs = ulong_num_vfs;
392 	} while (*(++name) && ret);
393 	return ret;
394 }
395 
396 /**
397  * Get device MTU.
398  *
399  * @param priv
400  *   Pointer to private structure.
401  * @param[out] mtu
402  *   MTU value output buffer.
403  *
404  * @return
405  *   0 on success, -1 on failure and errno is set.
406  */
407 int
408 priv_get_mtu(struct priv *priv, uint16_t *mtu)
409 {
410 	unsigned long ulong_mtu;
411 
412 	if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
413 		return -1;
414 	*mtu = ulong_mtu;
415 	return 0;
416 }
417 
418 /**
419  * Set device MTU.
420  *
421  * @param priv
422  *   Pointer to private structure.
423  * @param mtu
424  *   MTU value to set.
425  *
426  * @return
427  *   0 on success, -1 on failure and errno is set.
428  */
429 static int
430 priv_set_mtu(struct priv *priv, uint16_t mtu)
431 {
432 	uint16_t new_mtu;
433 
434 	if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
435 	    priv_get_mtu(priv, &new_mtu))
436 		return -1;
437 	if (new_mtu == mtu)
438 		return 0;
439 	errno = EINVAL;
440 	return -1;
441 }
442 
443 /**
444  * Set device flags.
445  *
446  * @param priv
447  *   Pointer to private structure.
448  * @param keep
449  *   Bitmask for flags that must remain untouched.
450  * @param flags
451  *   Bitmask for flags to modify.
452  *
453  * @return
454  *   0 on success, -1 on failure and errno is set.
455  */
456 int
457 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
458 {
459 	unsigned long tmp;
460 
461 	if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
462 		return -1;
463 	tmp &= keep;
464 	tmp |= flags;
465 	return priv_set_sysfs_ulong(priv, "flags", tmp);
466 }
467 
468 /**
469  * Ethernet device configuration.
470  *
471  * Prepare the driver for a given number of TX and RX queues.
472  *
473  * @param dev
474  *   Pointer to Ethernet device structure.
475  *
476  * @return
477  *   0 on success, errno value on failure.
478  */
479 static int
480 dev_configure(struct rte_eth_dev *dev)
481 {
482 	struct priv *priv = dev->data->dev_private;
483 	unsigned int rxqs_n = dev->data->nb_rx_queues;
484 	unsigned int txqs_n = dev->data->nb_tx_queues;
485 	unsigned int i;
486 	unsigned int j;
487 	unsigned int reta_idx_n;
488 
489 	priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
490 	priv->rxqs = (void *)dev->data->rx_queues;
491 	priv->txqs = (void *)dev->data->tx_queues;
492 	if (txqs_n != priv->txqs_n) {
493 		INFO("%p: TX queues number update: %u -> %u",
494 		     (void *)dev, priv->txqs_n, txqs_n);
495 		priv->txqs_n = txqs_n;
496 	}
497 	if (rxqs_n > priv->ind_table_max_size) {
498 		ERROR("cannot handle this many RX queues (%u)", rxqs_n);
499 		return EINVAL;
500 	}
501 	if (rxqs_n == priv->rxqs_n)
502 		return 0;
503 	INFO("%p: RX queues number update: %u -> %u",
504 	     (void *)dev, priv->rxqs_n, rxqs_n);
505 	priv->rxqs_n = rxqs_n;
506 	/* If the requested number of RX queues is not a power of two, use the
507 	 * maximum indirection table size for better balancing.
508 	 * The result is always rounded to the next power of two. */
509 	reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
510 				     priv->ind_table_max_size :
511 				     rxqs_n));
512 	if (priv_rss_reta_index_resize(priv, reta_idx_n))
513 		return ENOMEM;
514 	/* When the number of RX queues is not a power of two, the remaining
515 	 * table entries are padded with reused WQs and hashes are not spread
516 	 * uniformly. */
517 	for (i = 0, j = 0; (i != reta_idx_n); ++i) {
518 		(*priv->reta_idx)[i] = j;
519 		if (++j == rxqs_n)
520 			j = 0;
521 	}
522 	return 0;
523 }
524 
525 /**
526  * DPDK callback for Ethernet device configuration.
527  *
528  * @param dev
529  *   Pointer to Ethernet device structure.
530  *
531  * @return
532  *   0 on success, negative errno value on failure.
533  */
534 int
535 mlx5_dev_configure(struct rte_eth_dev *dev)
536 {
537 	struct priv *priv = dev->data->dev_private;
538 	int ret;
539 
540 	if (mlx5_is_secondary())
541 		return -E_RTE_SECONDARY;
542 
543 	priv_lock(priv);
544 	ret = dev_configure(dev);
545 	assert(ret >= 0);
546 	priv_unlock(priv);
547 	return -ret;
548 }
549 
550 /**
551  * DPDK callback to get information about the device.
552  *
553  * @param dev
554  *   Pointer to Ethernet device structure.
555  * @param[out] info
556  *   Info structure output buffer.
557  */
558 void
559 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
560 {
561 	struct priv *priv = mlx5_get_priv(dev);
562 	unsigned int max;
563 	char ifname[IF_NAMESIZE];
564 
565 	priv_lock(priv);
566 	/* FIXME: we should ask the device for these values. */
567 	info->min_rx_bufsize = 32;
568 	info->max_rx_pktlen = 65536;
569 	/*
570 	 * Since we need one CQ per QP, the limit is the minimum number
571 	 * between the two values.
572 	 */
573 	max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
574 	       priv->device_attr.max_qp : priv->device_attr.max_cq);
575 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
576 	if (max >= 65535)
577 		max = 65535;
578 	info->max_rx_queues = max;
579 	info->max_tx_queues = max;
580 	info->max_mac_addrs = RTE_DIM(priv->mac);
581 	info->rx_offload_capa =
582 		(priv->hw_csum ?
583 		 (DEV_RX_OFFLOAD_IPV4_CKSUM |
584 		  DEV_RX_OFFLOAD_UDP_CKSUM |
585 		  DEV_RX_OFFLOAD_TCP_CKSUM) :
586 		 0);
587 	if (!priv->mps)
588 		info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
589 	if (priv->hw_csum)
590 		info->tx_offload_capa |=
591 			(DEV_TX_OFFLOAD_IPV4_CKSUM |
592 			 DEV_TX_OFFLOAD_UDP_CKSUM |
593 			 DEV_TX_OFFLOAD_TCP_CKSUM);
594 	if (priv_get_ifname(priv, &ifname) == 0)
595 		info->if_index = if_nametoindex(ifname);
596 	/* FIXME: RETA update/query API expects the callee to know the size of
597 	 * the indirection table, for this PMD the size varies depending on
598 	 * the number of RX queues, it becomes impossible to find the correct
599 	 * size if it is not fixed.
600 	 * The API should be updated to solve this problem. */
601 	info->reta_size = priv->ind_table_max_size;
602 	info->speed_capa =
603 			ETH_LINK_SPEED_1G |
604 			ETH_LINK_SPEED_10G |
605 			ETH_LINK_SPEED_20G |
606 			ETH_LINK_SPEED_25G |
607 			ETH_LINK_SPEED_40G |
608 			ETH_LINK_SPEED_50G |
609 			ETH_LINK_SPEED_56G |
610 			ETH_LINK_SPEED_100G;
611 	priv_unlock(priv);
612 }
613 
614 const uint32_t *
615 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
616 {
617 	static const uint32_t ptypes[] = {
618 		/* refers to rxq_cq_to_pkt_type() */
619 		RTE_PTYPE_L3_IPV4,
620 		RTE_PTYPE_L3_IPV6,
621 		RTE_PTYPE_INNER_L3_IPV4,
622 		RTE_PTYPE_INNER_L3_IPV6,
623 		RTE_PTYPE_UNKNOWN
624 
625 	};
626 
627 	if (dev->rx_pkt_burst == mlx5_rx_burst)
628 		return ptypes;
629 	return NULL;
630 }
631 
632 /**
633  * DPDK callback to retrieve physical link information (unlocked version).
634  *
635  * @param dev
636  *   Pointer to Ethernet device structure.
637  * @param wait_to_complete
638  *   Wait for request completion (ignored).
639  */
640 static int
641 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
642 {
643 	struct priv *priv = mlx5_get_priv(dev);
644 	struct ethtool_cmd edata = {
645 		.cmd = ETHTOOL_GSET
646 	};
647 	struct ifreq ifr;
648 	struct rte_eth_link dev_link;
649 	int link_speed = 0;
650 
651 	(void)wait_to_complete;
652 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
653 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
654 		return -1;
655 	}
656 	memset(&dev_link, 0, sizeof(dev_link));
657 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
658 				(ifr.ifr_flags & IFF_RUNNING));
659 	ifr.ifr_data = (void *)&edata;
660 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
661 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
662 		     strerror(errno));
663 		return -1;
664 	}
665 	link_speed = ethtool_cmd_speed(&edata);
666 	if (link_speed == -1)
667 		dev_link.link_speed = 0;
668 	else
669 		dev_link.link_speed = link_speed;
670 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
671 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
672 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
673 			ETH_LINK_SPEED_FIXED);
674 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
675 		/* Link status changed. */
676 		dev->data->dev_link = dev_link;
677 		return 0;
678 	}
679 	/* Link status is still the same. */
680 	return -1;
681 }
682 
683 /**
684  * DPDK callback to retrieve physical link information.
685  *
686  * @param dev
687  *   Pointer to Ethernet device structure.
688  * @param wait_to_complete
689  *   Wait for request completion (ignored).
690  */
691 int
692 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
693 {
694 	struct priv *priv = mlx5_get_priv(dev);
695 	int ret;
696 
697 	priv_lock(priv);
698 	ret = mlx5_link_update_unlocked(dev, wait_to_complete);
699 	priv_unlock(priv);
700 	return ret;
701 }
702 
703 /**
704  * DPDK callback to change the MTU.
705  *
706  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
707  * received). Use this as a hint to enable/disable scattered packets support
708  * and improve performance when not needed.
709  * Since failure is not an option, reconfiguring queues on the fly is not
710  * recommended.
711  *
712  * @param dev
713  *   Pointer to Ethernet device structure.
714  * @param in_mtu
715  *   New MTU.
716  *
717  * @return
718  *   0 on success, negative errno value on failure.
719  */
720 int
721 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
722 {
723 	struct priv *priv = dev->data->dev_private;
724 	int ret = 0;
725 	unsigned int i;
726 	uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
727 		mlx5_rx_burst;
728 
729 	if (mlx5_is_secondary())
730 		return -E_RTE_SECONDARY;
731 
732 	priv_lock(priv);
733 	/* Set kernel interface MTU first. */
734 	if (priv_set_mtu(priv, mtu)) {
735 		ret = errno;
736 		WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
737 		     strerror(ret));
738 		goto out;
739 	} else
740 		DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
741 	priv->mtu = mtu;
742 	/* Temporarily replace RX handler with a fake one, assuming it has not
743 	 * been copied elsewhere. */
744 	dev->rx_pkt_burst = removed_rx_burst;
745 	/* Make sure everyone has left mlx5_rx_burst() and uses
746 	 * removed_rx_burst() instead. */
747 	rte_wmb();
748 	usleep(1000);
749 	/* Reconfigure each RX queue. */
750 	for (i = 0; (i != priv->rxqs_n); ++i) {
751 		struct rxq *rxq = (*priv->rxqs)[i];
752 		unsigned int mb_len;
753 		unsigned int max_frame_len;
754 		int sp;
755 
756 		if (rxq == NULL)
757 			continue;
758 		/* Calculate new maximum frame length according to MTU and
759 		 * toggle scattered support (sp) if necessary. */
760 		max_frame_len = (priv->mtu + ETHER_HDR_LEN +
761 				 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
762 		mb_len = rte_pktmbuf_data_room_size(rxq->mp);
763 		assert(mb_len >= RTE_PKTMBUF_HEADROOM);
764 		sp = (max_frame_len > (mb_len - RTE_PKTMBUF_HEADROOM));
765 		if (sp) {
766 			ERROR("%p: RX scatter is not supported", (void *)dev);
767 			ret = ENOTSUP;
768 			goto out;
769 		}
770 	}
771 	/* Burst functions can now be called again. */
772 	rte_wmb();
773 	dev->rx_pkt_burst = rx_func;
774 out:
775 	priv_unlock(priv);
776 	assert(ret >= 0);
777 	return -ret;
778 }
779 
780 /**
781  * DPDK callback to get flow control status.
782  *
783  * @param dev
784  *   Pointer to Ethernet device structure.
785  * @param[out] fc_conf
786  *   Flow control output buffer.
787  *
788  * @return
789  *   0 on success, negative errno value on failure.
790  */
791 int
792 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
793 {
794 	struct priv *priv = dev->data->dev_private;
795 	struct ifreq ifr;
796 	struct ethtool_pauseparam ethpause = {
797 		.cmd = ETHTOOL_GPAUSEPARAM
798 	};
799 	int ret;
800 
801 	if (mlx5_is_secondary())
802 		return -E_RTE_SECONDARY;
803 
804 	ifr.ifr_data = (void *)&ethpause;
805 	priv_lock(priv);
806 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
807 		ret = errno;
808 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
809 		     " failed: %s",
810 		     strerror(ret));
811 		goto out;
812 	}
813 
814 	fc_conf->autoneg = ethpause.autoneg;
815 	if (ethpause.rx_pause && ethpause.tx_pause)
816 		fc_conf->mode = RTE_FC_FULL;
817 	else if (ethpause.rx_pause)
818 		fc_conf->mode = RTE_FC_RX_PAUSE;
819 	else if (ethpause.tx_pause)
820 		fc_conf->mode = RTE_FC_TX_PAUSE;
821 	else
822 		fc_conf->mode = RTE_FC_NONE;
823 	ret = 0;
824 
825 out:
826 	priv_unlock(priv);
827 	assert(ret >= 0);
828 	return -ret;
829 }
830 
831 /**
832  * DPDK callback to modify flow control parameters.
833  *
834  * @param dev
835  *   Pointer to Ethernet device structure.
836  * @param[in] fc_conf
837  *   Flow control parameters.
838  *
839  * @return
840  *   0 on success, negative errno value on failure.
841  */
842 int
843 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
844 {
845 	struct priv *priv = dev->data->dev_private;
846 	struct ifreq ifr;
847 	struct ethtool_pauseparam ethpause = {
848 		.cmd = ETHTOOL_SPAUSEPARAM
849 	};
850 	int ret;
851 
852 	if (mlx5_is_secondary())
853 		return -E_RTE_SECONDARY;
854 
855 	ifr.ifr_data = (void *)&ethpause;
856 	ethpause.autoneg = fc_conf->autoneg;
857 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
858 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
859 		ethpause.rx_pause = 1;
860 	else
861 		ethpause.rx_pause = 0;
862 
863 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
864 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
865 		ethpause.tx_pause = 1;
866 	else
867 		ethpause.tx_pause = 0;
868 
869 	priv_lock(priv);
870 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
871 		ret = errno;
872 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
873 		     " failed: %s",
874 		     strerror(ret));
875 		goto out;
876 	}
877 	ret = 0;
878 
879 out:
880 	priv_unlock(priv);
881 	assert(ret >= 0);
882 	return -ret;
883 }
884 
885 /**
886  * Get PCI information from struct ibv_device.
887  *
888  * @param device
889  *   Pointer to Ethernet device structure.
890  * @param[out] pci_addr
891  *   PCI bus address output buffer.
892  *
893  * @return
894  *   0 on success, -1 on failure and errno is set.
895  */
896 int
897 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
898 			    struct rte_pci_addr *pci_addr)
899 {
900 	FILE *file;
901 	char line[32];
902 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
903 
904 	file = fopen(path, "rb");
905 	if (file == NULL)
906 		return -1;
907 	while (fgets(line, sizeof(line), file) == line) {
908 		size_t len = strlen(line);
909 		int ret;
910 
911 		/* Truncate long lines. */
912 		if (len == (sizeof(line) - 1))
913 			while (line[(len - 1)] != '\n') {
914 				ret = fgetc(file);
915 				if (ret == EOF)
916 					break;
917 				line[(len - 1)] = ret;
918 			}
919 		/* Extract information. */
920 		if (sscanf(line,
921 			   "PCI_SLOT_NAME="
922 			   "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
923 			   &pci_addr->domain,
924 			   &pci_addr->bus,
925 			   &pci_addr->devid,
926 			   &pci_addr->function) == 4) {
927 			ret = 0;
928 			break;
929 		}
930 	}
931 	fclose(file);
932 	return 0;
933 }
934 
935 /**
936  * Link status handler.
937  *
938  * @param priv
939  *   Pointer to private structure.
940  * @param dev
941  *   Pointer to the rte_eth_dev structure.
942  *
943  * @return
944  *   Nonzero if the callback process can be called immediately.
945  */
946 static int
947 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
948 {
949 	struct ibv_async_event event;
950 	int port_change = 0;
951 	int ret = 0;
952 
953 	/* Read all message and acknowledge them. */
954 	for (;;) {
955 		if (ibv_get_async_event(priv->ctx, &event))
956 			break;
957 
958 		if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
959 		    event.event_type == IBV_EVENT_PORT_ERR)
960 			port_change = 1;
961 		else
962 			DEBUG("event type %d on port %d not handled",
963 			      event.event_type, event.element.port_num);
964 		ibv_ack_async_event(&event);
965 	}
966 
967 	if (port_change ^ priv->pending_alarm) {
968 		struct rte_eth_link *link = &dev->data->dev_link;
969 
970 		priv->pending_alarm = 0;
971 		mlx5_link_update_unlocked(dev, 0);
972 		if (((link->link_speed == 0) && link->link_status) ||
973 		    ((link->link_speed != 0) && !link->link_status)) {
974 			/* Inconsistent status, check again later. */
975 			priv->pending_alarm = 1;
976 			rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
977 					  mlx5_dev_link_status_handler,
978 					  dev);
979 		} else
980 			ret = 1;
981 	}
982 	return ret;
983 }
984 
985 /**
986  * Handle delayed link status event.
987  *
988  * @param arg
989  *   Registered argument.
990  */
991 void
992 mlx5_dev_link_status_handler(void *arg)
993 {
994 	struct rte_eth_dev *dev = arg;
995 	struct priv *priv = dev->data->dev_private;
996 	int ret;
997 
998 	priv_lock(priv);
999 	assert(priv->pending_alarm == 1);
1000 	ret = priv_dev_link_status_handler(priv, dev);
1001 	priv_unlock(priv);
1002 	if (ret)
1003 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
1004 }
1005 
1006 /**
1007  * Handle interrupts from the NIC.
1008  *
1009  * @param[in] intr_handle
1010  *   Interrupt handler.
1011  * @param cb_arg
1012  *   Callback argument.
1013  */
1014 void
1015 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
1016 {
1017 	struct rte_eth_dev *dev = cb_arg;
1018 	struct priv *priv = dev->data->dev_private;
1019 	int ret;
1020 
1021 	(void)intr_handle;
1022 	priv_lock(priv);
1023 	ret = priv_dev_link_status_handler(priv, dev);
1024 	priv_unlock(priv);
1025 	if (ret)
1026 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
1027 }
1028 
1029 /**
1030  * Uninstall interrupt handler.
1031  *
1032  * @param priv
1033  *   Pointer to private structure.
1034  * @param dev
1035  *   Pointer to the rte_eth_dev structure.
1036  */
1037 void
1038 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1039 {
1040 	if (!dev->data->dev_conf.intr_conf.lsc)
1041 		return;
1042 	rte_intr_callback_unregister(&priv->intr_handle,
1043 				     mlx5_dev_interrupt_handler,
1044 				     dev);
1045 	if (priv->pending_alarm)
1046 		rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1047 	priv->pending_alarm = 0;
1048 	priv->intr_handle.fd = 0;
1049 	priv->intr_handle.type = 0;
1050 }
1051 
1052 /**
1053  * Install interrupt handler.
1054  *
1055  * @param priv
1056  *   Pointer to private structure.
1057  * @param dev
1058  *   Pointer to the rte_eth_dev structure.
1059  */
1060 void
1061 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1062 {
1063 	int rc, flags;
1064 
1065 	if (!dev->data->dev_conf.intr_conf.lsc)
1066 		return;
1067 	assert(priv->ctx->async_fd > 0);
1068 	flags = fcntl(priv->ctx->async_fd, F_GETFL);
1069 	rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1070 	if (rc < 0) {
1071 		INFO("failed to change file descriptor async event queue");
1072 		dev->data->dev_conf.intr_conf.lsc = 0;
1073 	} else {
1074 		priv->intr_handle.fd = priv->ctx->async_fd;
1075 		priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1076 		rte_intr_callback_register(&priv->intr_handle,
1077 					   mlx5_dev_interrupt_handler,
1078 					   dev);
1079 	}
1080 }
1081 
1082 /**
1083  * Change the link state (UP / DOWN).
1084  *
1085  * @param dev
1086  *   Pointer to Ethernet device structure.
1087  * @param up
1088  *   Nonzero for link up, otherwise link down.
1089  *
1090  * @return
1091  *   0 on success, errno value on failure.
1092  */
1093 static int
1094 priv_set_link(struct priv *priv, int up)
1095 {
1096 	struct rte_eth_dev *dev = priv->dev;
1097 	int err;
1098 
1099 	if (up) {
1100 		err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1101 		if (err)
1102 			return err;
1103 		priv_select_tx_function(priv);
1104 		priv_select_rx_function(priv);
1105 	} else {
1106 		err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1107 		if (err)
1108 			return err;
1109 		dev->rx_pkt_burst = removed_rx_burst;
1110 		dev->tx_pkt_burst = removed_tx_burst;
1111 	}
1112 	return 0;
1113 }
1114 
1115 /**
1116  * DPDK callback to bring the link DOWN.
1117  *
1118  * @param dev
1119  *   Pointer to Ethernet device structure.
1120  *
1121  * @return
1122  *   0 on success, errno value on failure.
1123  */
1124 int
1125 mlx5_set_link_down(struct rte_eth_dev *dev)
1126 {
1127 	struct priv *priv = dev->data->dev_private;
1128 	int err;
1129 
1130 	priv_lock(priv);
1131 	err = priv_set_link(priv, 0);
1132 	priv_unlock(priv);
1133 	return err;
1134 }
1135 
1136 /**
1137  * DPDK callback to bring the link UP.
1138  *
1139  * @param dev
1140  *   Pointer to Ethernet device structure.
1141  *
1142  * @return
1143  *   0 on success, errno value on failure.
1144  */
1145 int
1146 mlx5_set_link_up(struct rte_eth_dev *dev)
1147 {
1148 	struct priv *priv = dev->data->dev_private;
1149 	int err;
1150 
1151 	priv_lock(priv);
1152 	err = priv_set_link(priv, 1);
1153 	priv_unlock(priv);
1154 	return err;
1155 }
1156 
1157 /**
1158  * Configure secondary process queues from a private data pointer (primary
1159  * or secondary) and update burst callbacks. Can take place only once.
1160  *
1161  * All queues must have been previously created by the primary process to
1162  * avoid undefined behavior.
1163  *
1164  * @param priv
1165  *   Private data pointer from either primary or secondary process.
1166  *
1167  * @return
1168  *   Private data pointer from secondary process, NULL in case of error.
1169  */
1170 struct priv *
1171 mlx5_secondary_data_setup(struct priv *priv)
1172 {
1173 	unsigned int port_id = 0;
1174 	struct mlx5_secondary_data *sd;
1175 	void **tx_queues;
1176 	void **rx_queues;
1177 	unsigned int nb_tx_queues;
1178 	unsigned int nb_rx_queues;
1179 	unsigned int i;
1180 
1181 	/* priv must be valid at this point. */
1182 	assert(priv != NULL);
1183 	/* priv->dev must also be valid but may point to local memory from
1184 	 * another process, possibly with the same address and must not
1185 	 * be dereferenced yet. */
1186 	assert(priv->dev != NULL);
1187 	/* Determine port ID by finding out where priv comes from. */
1188 	while (1) {
1189 		sd = &mlx5_secondary_data[port_id];
1190 		rte_spinlock_lock(&sd->lock);
1191 		/* Primary process? */
1192 		if (sd->primary_priv == priv)
1193 			break;
1194 		/* Secondary process? */
1195 		if (sd->data.dev_private == priv)
1196 			break;
1197 		rte_spinlock_unlock(&sd->lock);
1198 		if (++port_id == RTE_DIM(mlx5_secondary_data))
1199 			port_id = 0;
1200 	}
1201 	/* Switch to secondary private structure. If private data has already
1202 	 * been updated by another thread, there is nothing else to do. */
1203 	priv = sd->data.dev_private;
1204 	if (priv->dev->data == &sd->data)
1205 		goto end;
1206 	/* Sanity checks. Secondary private structure is supposed to point
1207 	 * to local eth_dev, itself still pointing to the shared device data
1208 	 * structure allocated by the primary process. */
1209 	assert(sd->shared_dev_data != &sd->data);
1210 	assert(sd->data.nb_tx_queues == 0);
1211 	assert(sd->data.tx_queues == NULL);
1212 	assert(sd->data.nb_rx_queues == 0);
1213 	assert(sd->data.rx_queues == NULL);
1214 	assert(priv != sd->primary_priv);
1215 	assert(priv->dev->data == sd->shared_dev_data);
1216 	assert(priv->txqs_n == 0);
1217 	assert(priv->txqs == NULL);
1218 	assert(priv->rxqs_n == 0);
1219 	assert(priv->rxqs == NULL);
1220 	nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1221 	nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1222 	/* Allocate local storage for queues. */
1223 	tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1224 				sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1225 				RTE_CACHE_LINE_SIZE);
1226 	rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1227 				sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1228 				RTE_CACHE_LINE_SIZE);
1229 	if (tx_queues == NULL || rx_queues == NULL)
1230 		goto error;
1231 	/* Lock to prevent control operations during setup. */
1232 	priv_lock(priv);
1233 	/* TX queues. */
1234 	for (i = 0; i != nb_tx_queues; ++i) {
1235 		struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1236 		struct txq_ctrl *primary_txq_ctrl;
1237 		struct txq_ctrl *txq_ctrl;
1238 
1239 		if (primary_txq == NULL)
1240 			continue;
1241 		primary_txq_ctrl = container_of(primary_txq,
1242 						struct txq_ctrl, txq);
1243 		txq_ctrl = rte_calloc_socket("TXQ", 1, sizeof(*txq_ctrl), 0,
1244 					     primary_txq_ctrl->socket);
1245 		if (txq_ctrl != NULL) {
1246 			if (txq_ctrl_setup(priv->dev,
1247 					   primary_txq_ctrl,
1248 					   primary_txq->elts_n,
1249 					   primary_txq_ctrl->socket,
1250 					   NULL) == 0) {
1251 				txq_ctrl->txq.stats.idx =
1252 					primary_txq->stats.idx;
1253 				tx_queues[i] = &txq_ctrl->txq;
1254 				continue;
1255 			}
1256 			rte_free(txq_ctrl);
1257 		}
1258 		while (i) {
1259 			txq_ctrl = tx_queues[--i];
1260 			txq_cleanup(txq_ctrl);
1261 			rte_free(txq_ctrl);
1262 		}
1263 		goto error;
1264 	}
1265 	/* RX queues. */
1266 	for (i = 0; i != nb_rx_queues; ++i) {
1267 		struct rxq_ctrl *primary_rxq =
1268 			container_of((*sd->primary_priv->rxqs)[i],
1269 				     struct rxq_ctrl, rxq);
1270 
1271 		if (primary_rxq == NULL)
1272 			continue;
1273 		/* Not supported yet. */
1274 		rx_queues[i] = NULL;
1275 	}
1276 	/* Update everything. */
1277 	priv->txqs = (void *)tx_queues;
1278 	priv->txqs_n = nb_tx_queues;
1279 	priv->rxqs = (void *)rx_queues;
1280 	priv->rxqs_n = nb_rx_queues;
1281 	sd->data.rx_queues = rx_queues;
1282 	sd->data.tx_queues = tx_queues;
1283 	sd->data.nb_rx_queues = nb_rx_queues;
1284 	sd->data.nb_tx_queues = nb_tx_queues;
1285 	sd->data.dev_link = sd->shared_dev_data->dev_link;
1286 	sd->data.mtu = sd->shared_dev_data->mtu;
1287 	memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1288 	       sizeof(sd->data.rx_queue_state));
1289 	memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1290 	       sizeof(sd->data.tx_queue_state));
1291 	sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1292 	/* Use local data from now on. */
1293 	rte_mb();
1294 	priv->dev->data = &sd->data;
1295 	rte_mb();
1296 	priv_select_tx_function(priv);
1297 	priv_select_rx_function(priv);
1298 	priv_unlock(priv);
1299 end:
1300 	/* More sanity checks. */
1301 	assert(priv->dev->data == &sd->data);
1302 	rte_spinlock_unlock(&sd->lock);
1303 	return priv;
1304 error:
1305 	priv_unlock(priv);
1306 	rte_free(tx_queues);
1307 	rte_free(rx_queues);
1308 	rte_spinlock_unlock(&sd->lock);
1309 	return NULL;
1310 }
1311 
1312 /**
1313  * Configure the TX function to use.
1314  *
1315  * @param priv
1316  *   Pointer to private structure.
1317  */
1318 void
1319 priv_select_tx_function(struct priv *priv)
1320 {
1321 	priv->dev->tx_pkt_burst = mlx5_tx_burst;
1322 	/* Display warning for unsupported configurations. */
1323 	if (priv->sriov && priv->mps)
1324 		WARN("multi-packet send WQE cannot be used on a SR-IOV setup");
1325 	/* Select appropriate TX function. */
1326 	if ((priv->sriov == 0) && priv->mps && priv->txq_inline) {
1327 		priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1328 		DEBUG("selected MPW inline TX function");
1329 	} else if ((priv->sriov == 0) && priv->mps) {
1330 		priv->dev->tx_pkt_burst = mlx5_tx_burst_mpw;
1331 		DEBUG("selected MPW TX function");
1332 	} else if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
1333 		priv->dev->tx_pkt_burst = mlx5_tx_burst_inline;
1334 		DEBUG("selected inline TX function (%u >= %u queues)",
1335 		      priv->txqs_n, priv->txqs_inline);
1336 	}
1337 }
1338 
1339 /**
1340  * Configure the RX function to use.
1341  *
1342  * @param priv
1343  *   Pointer to private structure.
1344  */
1345 void
1346 priv_select_rx_function(struct priv *priv)
1347 {
1348 	priv->dev->rx_pkt_burst = mlx5_rx_burst;
1349 }
1350