xref: /dpdk/drivers/net/mlx4/mlx4.c (revision df2b82ca)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012-2015 6WIND S.A.
5  *   Copyright 2012 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 /*
35  * Known limitations:
36  * - RSS hash key and options cannot be modified.
37  * - Hardware counters aren't implemented.
38  */
39 
40 /* System headers. */
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <inttypes.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <limits.h>
50 #include <assert.h>
51 #include <arpa/inet.h>
52 #include <net/if.h>
53 #include <dirent.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <linux/ethtool.h>
58 #include <linux/sockios.h>
59 #include <fcntl.h>
60 
61 /* Verbs header. */
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
63 #ifdef PEDANTIC
64 #pragma GCC diagnostic ignored "-Wpedantic"
65 #endif
66 #include <infiniband/verbs.h>
67 #ifdef PEDANTIC
68 #pragma GCC diagnostic error "-Wpedantic"
69 #endif
70 
71 /* DPDK headers don't like -pedantic. */
72 #ifdef PEDANTIC
73 #pragma GCC diagnostic ignored "-Wpedantic"
74 #endif
75 #include <rte_ether.h>
76 #include <rte_ethdev.h>
77 #include <rte_dev.h>
78 #include <rte_mbuf.h>
79 #include <rte_errno.h>
80 #include <rte_mempool.h>
81 #include <rte_prefetch.h>
82 #include <rte_malloc.h>
83 #include <rte_spinlock.h>
84 #include <rte_atomic.h>
85 #include <rte_version.h>
86 #include <rte_log.h>
87 #include <rte_alarm.h>
88 #include <rte_memory.h>
89 #ifdef PEDANTIC
90 #pragma GCC diagnostic error "-Wpedantic"
91 #endif
92 
93 /* Generated configuration header. */
94 #include "mlx4_autoconf.h"
95 
96 /* PMD header. */
97 #include "mlx4.h"
98 
99 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
100  * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #ifndef NDEBUG
102 #define INFO(...) DEBUG(__VA_ARGS__)
103 #define WARN(...) DEBUG(__VA_ARGS__)
104 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #else
106 #define LOG__(level, m, ...) \
107 	RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
108 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
109 #define INFO(...) LOG_(INFO, __VA_ARGS__)
110 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
111 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
112 #endif
113 
114 /* Convenience macros for accessing mbuf fields. */
115 #define NEXT(m) ((m)->next)
116 #define DATA_LEN(m) ((m)->data_len)
117 #define PKT_LEN(m) ((m)->pkt_len)
118 #define DATA_OFF(m) ((m)->data_off)
119 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
120 #define NB_SEGS(m) ((m)->nb_segs)
121 #define PORT(m) ((m)->port)
122 
123 /* Work Request ID data type (64 bit). */
124 typedef union {
125 	struct {
126 		uint32_t id;
127 		uint16_t offset;
128 	} data;
129 	uint64_t raw;
130 } wr_id_t;
131 
132 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 
134 /* Transpose flags. Useful to convert IBV to DPDK flags. */
135 #define TRANSPOSE(val, from, to) \
136 	(((from) >= (to)) ? \
137 	 (((val) & (from)) / ((from) / (to))) : \
138 	 (((val) & (from)) * ((to) / (from))))
139 
140 struct mlx4_rxq_stats {
141 	unsigned int idx; /**< Mapping index. */
142 #ifdef MLX4_PMD_SOFT_COUNTERS
143 	uint64_t ipackets;  /**< Total of successfully received packets. */
144 	uint64_t ibytes;    /**< Total of successfully received bytes. */
145 #endif
146 	uint64_t idropped;  /**< Total of packets dropped when RX ring full. */
147 	uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
148 };
149 
150 struct mlx4_txq_stats {
151 	unsigned int idx; /**< Mapping index. */
152 #ifdef MLX4_PMD_SOFT_COUNTERS
153 	uint64_t opackets; /**< Total of successfully sent packets. */
154 	uint64_t obytes;   /**< Total of successfully sent bytes. */
155 #endif
156 	uint64_t odropped; /**< Total of packets not sent when TX ring full. */
157 };
158 
159 /* RX element (scattered packets). */
160 struct rxq_elt_sp {
161 	struct ibv_recv_wr wr; /* Work Request. */
162 	struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
163 	struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
164 };
165 
166 /* RX element. */
167 struct rxq_elt {
168 	struct ibv_recv_wr wr; /* Work Request. */
169 	struct ibv_sge sge; /* Scatter/Gather Element. */
170 	/* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
171 };
172 
173 /* RX queue descriptor. */
174 struct rxq {
175 	struct priv *priv; /* Back pointer to private data. */
176 	struct rte_mempool *mp; /* Memory Pool for allocations. */
177 	struct ibv_mr *mr; /* Memory Region (for mp). */
178 	struct ibv_cq *cq; /* Completion Queue. */
179 	struct ibv_qp *qp; /* Queue Pair. */
180 	struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
181 	struct ibv_exp_cq_family *if_cq; /* CQ interface. */
182 	/*
183 	 * Each VLAN ID requires a separate flow steering rule.
184 	 */
185 	BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
186 	struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
187 	struct ibv_flow *promisc_flow; /* Promiscuous flow. */
188 	struct ibv_flow *allmulti_flow; /* Multicast flow. */
189 	unsigned int port_id; /* Port ID for incoming packets. */
190 	unsigned int elts_n; /* (*elts)[] length. */
191 	unsigned int elts_head; /* Current index in (*elts)[]. */
192 	union {
193 		struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
194 		struct rxq_elt (*no_sp)[]; /* RX elements. */
195 	} elts;
196 	unsigned int sp:1; /* Use scattered RX elements. */
197 	unsigned int csum:1; /* Enable checksum offloading. */
198 	unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
199 	struct mlx4_rxq_stats stats; /* RX queue counters. */
200 	unsigned int socket; /* CPU socket ID for allocations. */
201 	struct ibv_exp_res_domain *rd; /* Resource Domain. */
202 };
203 
204 /* TX element. */
205 struct txq_elt {
206 	struct rte_mbuf *buf;
207 };
208 
209 /* Linear buffer type. It is used when transmitting buffers with too many
210  * segments that do not fit the hardware queue (see max_send_sge).
211  * Extra segments are copied (linearized) in such buffers, replacing the
212  * last SGE during TX.
213  * The size is arbitrary but large enough to hold a jumbo frame with
214  * 8 segments considering mbuf.buf_len is about 2048 bytes. */
215 typedef uint8_t linear_t[16384];
216 
217 /* TX queue descriptor. */
218 struct txq {
219 	struct priv *priv; /* Back pointer to private data. */
220 	struct {
221 		const struct rte_mempool *mp; /* Cached Memory Pool. */
222 		struct ibv_mr *mr; /* Memory Region (for mp). */
223 		uint32_t lkey; /* mr->lkey */
224 	} mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
225 	struct ibv_cq *cq; /* Completion Queue. */
226 	struct ibv_qp *qp; /* Queue Pair. */
227 	struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
228 	struct ibv_exp_cq_family *if_cq; /* CQ interface. */
229 #if MLX4_PMD_MAX_INLINE > 0
230 	uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
231 #endif
232 	unsigned int elts_n; /* (*elts)[] length. */
233 	struct txq_elt (*elts)[]; /* TX elements. */
234 	unsigned int elts_head; /* Current index in (*elts)[]. */
235 	unsigned int elts_tail; /* First element awaiting completion. */
236 	unsigned int elts_comp; /* Number of completion requests. */
237 	unsigned int elts_comp_cd; /* Countdown for next completion request. */
238 	unsigned int elts_comp_cd_init; /* Initial value for countdown. */
239 	struct mlx4_txq_stats stats; /* TX queue counters. */
240 	linear_t (*elts_linear)[]; /* Linearized buffers. */
241 	struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
242 	unsigned int socket; /* CPU socket ID for allocations. */
243 	struct ibv_exp_res_domain *rd; /* Resource Domain. */
244 };
245 
246 struct priv {
247 	struct rte_eth_dev *dev; /* Ethernet device. */
248 	struct ibv_context *ctx; /* Verbs context. */
249 	struct ibv_device_attr device_attr; /* Device properties. */
250 	struct ibv_pd *pd; /* Protection Domain. */
251 	/*
252 	 * MAC addresses array and configuration bit-field.
253 	 * An extra entry that cannot be modified by the DPDK is reserved
254 	 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
255 	 */
256 	struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
257 	BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
258 	/* VLAN filters. */
259 	struct {
260 		unsigned int enabled:1; /* If enabled. */
261 		unsigned int id:12; /* VLAN ID (0-4095). */
262 	} vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
263 	/* Device properties. */
264 	uint16_t mtu; /* Configured MTU. */
265 	uint8_t port; /* Physical port number. */
266 	unsigned int started:1; /* Device started, flows enabled. */
267 	unsigned int promisc:1; /* Device in promiscuous mode. */
268 	unsigned int allmulti:1; /* Device receives all multicast packets. */
269 	unsigned int hw_qpg:1; /* QP groups are supported. */
270 	unsigned int hw_tss:1; /* TSS is supported. */
271 	unsigned int hw_rss:1; /* RSS is supported. */
272 	unsigned int hw_csum:1; /* Checksum offload is supported. */
273 	unsigned int hw_csum_l2tun:1; /* Same for L2 tunnels. */
274 	unsigned int rss:1; /* RSS is enabled. */
275 	unsigned int vf:1; /* This is a VF device. */
276 	unsigned int pending_alarm:1; /* An alarm is pending. */
277 #ifdef INLINE_RECV
278 	unsigned int inl_recv_size; /* Inline recv size */
279 #endif
280 	unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
281 	/* RX/TX queues. */
282 	struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
283 	unsigned int rxqs_n; /* RX queues array size. */
284 	unsigned int txqs_n; /* TX queues array size. */
285 	struct rxq *(*rxqs)[]; /* RX queues. */
286 	struct txq *(*txqs)[]; /* TX queues. */
287 	struct rte_intr_handle intr_handle; /* Interrupt handler. */
288 	rte_spinlock_t lock; /* Lock for control functions. */
289 };
290 
291 /* Local storage for secondary process data. */
292 struct mlx4_secondary_data {
293 	struct rte_eth_dev_data data; /* Local device data. */
294 	struct priv *primary_priv; /* Private structure from primary. */
295 	struct rte_eth_dev_data *shared_dev_data; /* Shared device data. */
296 	rte_spinlock_t lock; /* Port configuration lock. */
297 } mlx4_secondary_data[RTE_MAX_ETHPORTS];
298 
299 /**
300  * Check if running as a secondary process.
301  *
302  * @return
303  *   Nonzero if running as a secondary process.
304  */
305 static inline int
306 mlx4_is_secondary(void)
307 {
308 	return rte_eal_process_type() != RTE_PROC_PRIMARY;
309 }
310 
311 /**
312  * Return private structure associated with an Ethernet device.
313  *
314  * @param dev
315  *   Pointer to Ethernet device structure.
316  *
317  * @return
318  *   Pointer to private structure.
319  */
320 static struct priv *
321 mlx4_get_priv(struct rte_eth_dev *dev)
322 {
323 	struct mlx4_secondary_data *sd;
324 
325 	if (!mlx4_is_secondary())
326 		return dev->data->dev_private;
327 	sd = &mlx4_secondary_data[dev->data->port_id];
328 	return sd->data.dev_private;
329 }
330 
331 /**
332  * Lock private structure to protect it from concurrent access in the
333  * control path.
334  *
335  * @param priv
336  *   Pointer to private structure.
337  */
338 static void
339 priv_lock(struct priv *priv)
340 {
341 	rte_spinlock_lock(&priv->lock);
342 }
343 
344 /**
345  * Unlock private structure.
346  *
347  * @param priv
348  *   Pointer to private structure.
349  */
350 static void
351 priv_unlock(struct priv *priv)
352 {
353 	rte_spinlock_unlock(&priv->lock);
354 }
355 
356 /* Allocate a buffer on the stack and fill it with a printf format string. */
357 #define MKSTR(name, ...) \
358 	char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
359 	\
360 	snprintf(name, sizeof(name), __VA_ARGS__)
361 
362 /**
363  * Get interface name from private structure.
364  *
365  * @param[in] priv
366  *   Pointer to private structure.
367  * @param[out] ifname
368  *   Interface name output buffer.
369  *
370  * @return
371  *   0 on success, -1 on failure and errno is set.
372  */
373 static int
374 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
375 {
376 	DIR *dir;
377 	struct dirent *dent;
378 	unsigned int dev_type = 0;
379 	unsigned int dev_port_prev = ~0u;
380 	char match[IF_NAMESIZE] = "";
381 
382 	{
383 		MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
384 
385 		dir = opendir(path);
386 		if (dir == NULL)
387 			return -1;
388 	}
389 	while ((dent = readdir(dir)) != NULL) {
390 		char *name = dent->d_name;
391 		FILE *file;
392 		unsigned int dev_port;
393 		int r;
394 
395 		if ((name[0] == '.') &&
396 		    ((name[1] == '\0') ||
397 		     ((name[1] == '.') && (name[2] == '\0'))))
398 			continue;
399 
400 		MKSTR(path, "%s/device/net/%s/%s",
401 		      priv->ctx->device->ibdev_path, name,
402 		      (dev_type ? "dev_id" : "dev_port"));
403 
404 		file = fopen(path, "rb");
405 		if (file == NULL) {
406 			if (errno != ENOENT)
407 				continue;
408 			/*
409 			 * Switch to dev_id when dev_port does not exist as
410 			 * is the case with Linux kernel versions < 3.15.
411 			 */
412 try_dev_id:
413 			match[0] = '\0';
414 			if (dev_type)
415 				break;
416 			dev_type = 1;
417 			dev_port_prev = ~0u;
418 			rewinddir(dir);
419 			continue;
420 		}
421 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
422 		fclose(file);
423 		if (r != 1)
424 			continue;
425 		/*
426 		 * Switch to dev_id when dev_port returns the same value for
427 		 * all ports. May happen when using a MOFED release older than
428 		 * 3.0 with a Linux kernel >= 3.15.
429 		 */
430 		if (dev_port == dev_port_prev)
431 			goto try_dev_id;
432 		dev_port_prev = dev_port;
433 		if (dev_port == (priv->port - 1u))
434 			snprintf(match, sizeof(match), "%s", name);
435 	}
436 	closedir(dir);
437 	if (match[0] == '\0')
438 		return -1;
439 	strncpy(*ifname, match, sizeof(*ifname));
440 	return 0;
441 }
442 
443 /**
444  * Read from sysfs entry.
445  *
446  * @param[in] priv
447  *   Pointer to private structure.
448  * @param[in] entry
449  *   Entry name relative to sysfs path.
450  * @param[out] buf
451  *   Data output buffer.
452  * @param size
453  *   Buffer size.
454  *
455  * @return
456  *   0 on success, -1 on failure and errno is set.
457  */
458 static int
459 priv_sysfs_read(const struct priv *priv, const char *entry,
460 		char *buf, size_t size)
461 {
462 	char ifname[IF_NAMESIZE];
463 	FILE *file;
464 	int ret;
465 	int err;
466 
467 	if (priv_get_ifname(priv, &ifname))
468 		return -1;
469 
470 	MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
471 	      ifname, entry);
472 
473 	file = fopen(path, "rb");
474 	if (file == NULL)
475 		return -1;
476 	ret = fread(buf, 1, size, file);
477 	err = errno;
478 	if (((size_t)ret < size) && (ferror(file)))
479 		ret = -1;
480 	else
481 		ret = size;
482 	fclose(file);
483 	errno = err;
484 	return ret;
485 }
486 
487 /**
488  * Write to sysfs entry.
489  *
490  * @param[in] priv
491  *   Pointer to private structure.
492  * @param[in] entry
493  *   Entry name relative to sysfs path.
494  * @param[in] buf
495  *   Data buffer.
496  * @param size
497  *   Buffer size.
498  *
499  * @return
500  *   0 on success, -1 on failure and errno is set.
501  */
502 static int
503 priv_sysfs_write(const struct priv *priv, const char *entry,
504 		 char *buf, size_t size)
505 {
506 	char ifname[IF_NAMESIZE];
507 	FILE *file;
508 	int ret;
509 	int err;
510 
511 	if (priv_get_ifname(priv, &ifname))
512 		return -1;
513 
514 	MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
515 	      ifname, entry);
516 
517 	file = fopen(path, "wb");
518 	if (file == NULL)
519 		return -1;
520 	ret = fwrite(buf, 1, size, file);
521 	err = errno;
522 	if (((size_t)ret < size) || (ferror(file)))
523 		ret = -1;
524 	else
525 		ret = size;
526 	fclose(file);
527 	errno = err;
528 	return ret;
529 }
530 
531 /**
532  * Get unsigned long sysfs property.
533  *
534  * @param priv
535  *   Pointer to private structure.
536  * @param[in] name
537  *   Entry name relative to sysfs path.
538  * @param[out] value
539  *   Value output buffer.
540  *
541  * @return
542  *   0 on success, -1 on failure and errno is set.
543  */
544 static int
545 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
546 {
547 	int ret;
548 	unsigned long value_ret;
549 	char value_str[32];
550 
551 	ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
552 	if (ret == -1) {
553 		DEBUG("cannot read %s value from sysfs: %s",
554 		      name, strerror(errno));
555 		return -1;
556 	}
557 	value_str[ret] = '\0';
558 	errno = 0;
559 	value_ret = strtoul(value_str, NULL, 0);
560 	if (errno) {
561 		DEBUG("invalid %s value `%s': %s", name, value_str,
562 		      strerror(errno));
563 		return -1;
564 	}
565 	*value = value_ret;
566 	return 0;
567 }
568 
569 /**
570  * Set unsigned long sysfs property.
571  *
572  * @param priv
573  *   Pointer to private structure.
574  * @param[in] name
575  *   Entry name relative to sysfs path.
576  * @param value
577  *   Value to set.
578  *
579  * @return
580  *   0 on success, -1 on failure and errno is set.
581  */
582 static int
583 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
584 {
585 	int ret;
586 	MKSTR(value_str, "%lu", value);
587 
588 	ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
589 	if (ret == -1) {
590 		DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
591 		      name, value_str, value, strerror(errno));
592 		return -1;
593 	}
594 	return 0;
595 }
596 
597 /**
598  * Perform ifreq ioctl() on associated Ethernet device.
599  *
600  * @param[in] priv
601  *   Pointer to private structure.
602  * @param req
603  *   Request number to pass to ioctl().
604  * @param[out] ifr
605  *   Interface request structure output buffer.
606  *
607  * @return
608  *   0 on success, -1 on failure and errno is set.
609  */
610 static int
611 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
612 {
613 	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
614 	int ret = -1;
615 
616 	if (sock == -1)
617 		return ret;
618 	if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
619 		ret = ioctl(sock, req, ifr);
620 	close(sock);
621 	return ret;
622 }
623 
624 /**
625  * Get device MTU.
626  *
627  * @param priv
628  *   Pointer to private structure.
629  * @param[out] mtu
630  *   MTU value output buffer.
631  *
632  * @return
633  *   0 on success, -1 on failure and errno is set.
634  */
635 static int
636 priv_get_mtu(struct priv *priv, uint16_t *mtu)
637 {
638 	unsigned long ulong_mtu;
639 
640 	if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
641 		return -1;
642 	*mtu = ulong_mtu;
643 	return 0;
644 }
645 
646 /**
647  * Set device MTU.
648  *
649  * @param priv
650  *   Pointer to private structure.
651  * @param mtu
652  *   MTU value to set.
653  *
654  * @return
655  *   0 on success, -1 on failure and errno is set.
656  */
657 static int
658 priv_set_mtu(struct priv *priv, uint16_t mtu)
659 {
660 	uint16_t new_mtu;
661 
662 	if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
663 	    priv_get_mtu(priv, &new_mtu))
664 		return -1;
665 	if (new_mtu == mtu)
666 		return 0;
667 	errno = EINVAL;
668 	return -1;
669 }
670 
671 /**
672  * Set device flags.
673  *
674  * @param priv
675  *   Pointer to private structure.
676  * @param keep
677  *   Bitmask for flags that must remain untouched.
678  * @param flags
679  *   Bitmask for flags to modify.
680  *
681  * @return
682  *   0 on success, -1 on failure and errno is set.
683  */
684 static int
685 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
686 {
687 	unsigned long tmp;
688 
689 	if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
690 		return -1;
691 	tmp &= keep;
692 	tmp |= (flags & (~keep));
693 	return priv_set_sysfs_ulong(priv, "flags", tmp);
694 }
695 
696 /* Device configuration. */
697 
698 static int
699 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
700 	  unsigned int socket, const struct rte_eth_txconf *conf);
701 
702 static void
703 txq_cleanup(struct txq *txq);
704 
705 static int
706 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
707 	  unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
708 	  struct rte_mempool *mp);
709 
710 static void
711 rxq_cleanup(struct rxq *rxq);
712 
713 /**
714  * Ethernet device configuration.
715  *
716  * Prepare the driver for a given number of TX and RX queues.
717  * Allocate parent RSS queue when several RX queues are requested.
718  *
719  * @param dev
720  *   Pointer to Ethernet device structure.
721  *
722  * @return
723  *   0 on success, errno value on failure.
724  */
725 static int
726 dev_configure(struct rte_eth_dev *dev)
727 {
728 	struct priv *priv = dev->data->dev_private;
729 	unsigned int rxqs_n = dev->data->nb_rx_queues;
730 	unsigned int txqs_n = dev->data->nb_tx_queues;
731 	unsigned int tmp;
732 	int ret;
733 
734 	priv->rxqs = (void *)dev->data->rx_queues;
735 	priv->txqs = (void *)dev->data->tx_queues;
736 	if (txqs_n != priv->txqs_n) {
737 		INFO("%p: TX queues number update: %u -> %u",
738 		     (void *)dev, priv->txqs_n, txqs_n);
739 		priv->txqs_n = txqs_n;
740 	}
741 	if (rxqs_n == priv->rxqs_n)
742 		return 0;
743 	if (!rte_is_power_of_2(rxqs_n)) {
744 		unsigned n_active;
745 
746 		n_active = rte_align32pow2(rxqs_n + 1) >> 1;
747 		WARN("%p: number of RX queues must be a power"
748 			" of 2: %u queues among %u will be active",
749 			(void *)dev, n_active, rxqs_n);
750 	}
751 
752 	INFO("%p: RX queues number update: %u -> %u",
753 	     (void *)dev, priv->rxqs_n, rxqs_n);
754 	/* If RSS is enabled, disable it first. */
755 	if (priv->rss) {
756 		unsigned int i;
757 
758 		/* Only if there are no remaining child RX queues. */
759 		for (i = 0; (i != priv->rxqs_n); ++i)
760 			if ((*priv->rxqs)[i] != NULL)
761 				return EINVAL;
762 		rxq_cleanup(&priv->rxq_parent);
763 		priv->rss = 0;
764 		priv->rxqs_n = 0;
765 	}
766 	if (rxqs_n <= 1) {
767 		/* Nothing else to do. */
768 		priv->rxqs_n = rxqs_n;
769 		return 0;
770 	}
771 	/* Allocate a new RSS parent queue if supported by hardware. */
772 	if (!priv->hw_rss) {
773 		ERROR("%p: only a single RX queue can be configured when"
774 		      " hardware doesn't support RSS",
775 		      (void *)dev);
776 		return EINVAL;
777 	}
778 	/* Fail if hardware doesn't support that many RSS queues. */
779 	if (rxqs_n >= priv->max_rss_tbl_sz) {
780 		ERROR("%p: only %u RX queues can be configured for RSS",
781 		      (void *)dev, priv->max_rss_tbl_sz);
782 		return EINVAL;
783 	}
784 	priv->rss = 1;
785 	tmp = priv->rxqs_n;
786 	priv->rxqs_n = rxqs_n;
787 	ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, 0, NULL, NULL);
788 	if (!ret)
789 		return 0;
790 	/* Failure, rollback. */
791 	priv->rss = 0;
792 	priv->rxqs_n = tmp;
793 	assert(ret > 0);
794 	return ret;
795 }
796 
797 /**
798  * DPDK callback for Ethernet device configuration.
799  *
800  * @param dev
801  *   Pointer to Ethernet device structure.
802  *
803  * @return
804  *   0 on success, negative errno value on failure.
805  */
806 static int
807 mlx4_dev_configure(struct rte_eth_dev *dev)
808 {
809 	struct priv *priv = dev->data->dev_private;
810 	int ret;
811 
812 	if (mlx4_is_secondary())
813 		return -E_RTE_SECONDARY;
814 	priv_lock(priv);
815 	ret = dev_configure(dev);
816 	assert(ret >= 0);
817 	priv_unlock(priv);
818 	return -ret;
819 }
820 
821 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
822 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
823 
824 /**
825  * Configure secondary process queues from a private data pointer (primary
826  * or secondary) and update burst callbacks. Can take place only once.
827  *
828  * All queues must have been previously created by the primary process to
829  * avoid undefined behavior.
830  *
831  * @param priv
832  *   Private data pointer from either primary or secondary process.
833  *
834  * @return
835  *   Private data pointer from secondary process, NULL in case of error.
836  */
837 static struct priv *
838 mlx4_secondary_data_setup(struct priv *priv)
839 {
840 	unsigned int port_id = 0;
841 	struct mlx4_secondary_data *sd;
842 	void **tx_queues;
843 	void **rx_queues;
844 	unsigned int nb_tx_queues;
845 	unsigned int nb_rx_queues;
846 	unsigned int i;
847 
848 	/* priv must be valid at this point. */
849 	assert(priv != NULL);
850 	/* priv->dev must also be valid but may point to local memory from
851 	 * another process, possibly with the same address and must not
852 	 * be dereferenced yet. */
853 	assert(priv->dev != NULL);
854 	/* Determine port ID by finding out where priv comes from. */
855 	while (1) {
856 		sd = &mlx4_secondary_data[port_id];
857 		rte_spinlock_lock(&sd->lock);
858 		/* Primary process? */
859 		if (sd->primary_priv == priv)
860 			break;
861 		/* Secondary process? */
862 		if (sd->data.dev_private == priv)
863 			break;
864 		rte_spinlock_unlock(&sd->lock);
865 		if (++port_id == RTE_DIM(mlx4_secondary_data))
866 			port_id = 0;
867 	}
868 	/* Switch to secondary private structure. If private data has already
869 	 * been updated by another thread, there is nothing else to do. */
870 	priv = sd->data.dev_private;
871 	if (priv->dev->data == &sd->data)
872 		goto end;
873 	/* Sanity checks. Secondary private structure is supposed to point
874 	 * to local eth_dev, itself still pointing to the shared device data
875 	 * structure allocated by the primary process. */
876 	assert(sd->shared_dev_data != &sd->data);
877 	assert(sd->data.nb_tx_queues == 0);
878 	assert(sd->data.tx_queues == NULL);
879 	assert(sd->data.nb_rx_queues == 0);
880 	assert(sd->data.rx_queues == NULL);
881 	assert(priv != sd->primary_priv);
882 	assert(priv->dev->data == sd->shared_dev_data);
883 	assert(priv->txqs_n == 0);
884 	assert(priv->txqs == NULL);
885 	assert(priv->rxqs_n == 0);
886 	assert(priv->rxqs == NULL);
887 	nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
888 	nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
889 	/* Allocate local storage for queues. */
890 	tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
891 				sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
892 				RTE_CACHE_LINE_SIZE);
893 	rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
894 				sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
895 				RTE_CACHE_LINE_SIZE);
896 	if (tx_queues == NULL || rx_queues == NULL)
897 		goto error;
898 	/* Lock to prevent control operations during setup. */
899 	priv_lock(priv);
900 	/* TX queues. */
901 	for (i = 0; i != nb_tx_queues; ++i) {
902 		struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
903 		struct txq *txq;
904 
905 		if (primary_txq == NULL)
906 			continue;
907 		txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
908 					primary_txq->socket);
909 		if (txq != NULL) {
910 			if (txq_setup(priv->dev,
911 				      txq,
912 				      primary_txq->elts_n * MLX4_PMD_SGE_WR_N,
913 				      primary_txq->socket,
914 				      NULL) == 0) {
915 				txq->stats.idx = primary_txq->stats.idx;
916 				tx_queues[i] = txq;
917 				continue;
918 			}
919 			rte_free(txq);
920 		}
921 		while (i) {
922 			txq = tx_queues[--i];
923 			txq_cleanup(txq);
924 			rte_free(txq);
925 		}
926 		goto error;
927 	}
928 	/* RX queues. */
929 	for (i = 0; i != nb_rx_queues; ++i) {
930 		struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
931 
932 		if (primary_rxq == NULL)
933 			continue;
934 		/* Not supported yet. */
935 		rx_queues[i] = NULL;
936 	}
937 	/* Update everything. */
938 	priv->txqs = (void *)tx_queues;
939 	priv->txqs_n = nb_tx_queues;
940 	priv->rxqs = (void *)rx_queues;
941 	priv->rxqs_n = nb_rx_queues;
942 	sd->data.rx_queues = rx_queues;
943 	sd->data.tx_queues = tx_queues;
944 	sd->data.nb_rx_queues = nb_rx_queues;
945 	sd->data.nb_tx_queues = nb_tx_queues;
946 	sd->data.dev_link = sd->shared_dev_data->dev_link;
947 	sd->data.mtu = sd->shared_dev_data->mtu;
948 	memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
949 	       sizeof(sd->data.rx_queue_state));
950 	memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
951 	       sizeof(sd->data.tx_queue_state));
952 	sd->data.dev_flags = sd->shared_dev_data->dev_flags;
953 	/* Use local data from now on. */
954 	rte_mb();
955 	priv->dev->data = &sd->data;
956 	rte_mb();
957 	priv->dev->tx_pkt_burst = mlx4_tx_burst;
958 	priv->dev->rx_pkt_burst = removed_rx_burst;
959 	priv_unlock(priv);
960 end:
961 	/* More sanity checks. */
962 	assert(priv->dev->tx_pkt_burst == mlx4_tx_burst);
963 	assert(priv->dev->rx_pkt_burst == removed_rx_burst);
964 	assert(priv->dev->data == &sd->data);
965 	rte_spinlock_unlock(&sd->lock);
966 	return priv;
967 error:
968 	priv_unlock(priv);
969 	rte_free(tx_queues);
970 	rte_free(rx_queues);
971 	rte_spinlock_unlock(&sd->lock);
972 	return NULL;
973 }
974 
975 /* TX queues handling. */
976 
977 /**
978  * Allocate TX queue elements.
979  *
980  * @param txq
981  *   Pointer to TX queue structure.
982  * @param elts_n
983  *   Number of elements to allocate.
984  *
985  * @return
986  *   0 on success, errno value on failure.
987  */
988 static int
989 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
990 {
991 	unsigned int i;
992 	struct txq_elt (*elts)[elts_n] =
993 		rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
994 	linear_t (*elts_linear)[elts_n] =
995 		rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
996 				  txq->socket);
997 	struct ibv_mr *mr_linear = NULL;
998 	int ret = 0;
999 
1000 	if ((elts == NULL) || (elts_linear == NULL)) {
1001 		ERROR("%p: can't allocate packets array", (void *)txq);
1002 		ret = ENOMEM;
1003 		goto error;
1004 	}
1005 	mr_linear =
1006 		ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
1007 			   IBV_ACCESS_LOCAL_WRITE);
1008 	if (mr_linear == NULL) {
1009 		ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
1010 		      (void *)txq);
1011 		ret = EINVAL;
1012 		goto error;
1013 	}
1014 	for (i = 0; (i != elts_n); ++i) {
1015 		struct txq_elt *elt = &(*elts)[i];
1016 
1017 		elt->buf = NULL;
1018 	}
1019 	DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
1020 	txq->elts_n = elts_n;
1021 	txq->elts = elts;
1022 	txq->elts_head = 0;
1023 	txq->elts_tail = 0;
1024 	txq->elts_comp = 0;
1025 	/* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
1026 	 * at least 4 times per ring. */
1027 	txq->elts_comp_cd_init =
1028 		((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
1029 		 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
1030 	txq->elts_comp_cd = txq->elts_comp_cd_init;
1031 	txq->elts_linear = elts_linear;
1032 	txq->mr_linear = mr_linear;
1033 	assert(ret == 0);
1034 	return 0;
1035 error:
1036 	if (mr_linear != NULL)
1037 		claim_zero(ibv_dereg_mr(mr_linear));
1038 
1039 	rte_free(elts_linear);
1040 	rte_free(elts);
1041 
1042 	DEBUG("%p: failed, freed everything", (void *)txq);
1043 	assert(ret > 0);
1044 	return ret;
1045 }
1046 
1047 /**
1048  * Free TX queue elements.
1049  *
1050  * @param txq
1051  *   Pointer to TX queue structure.
1052  */
1053 static void
1054 txq_free_elts(struct txq *txq)
1055 {
1056 	unsigned int elts_n = txq->elts_n;
1057 	unsigned int elts_head = txq->elts_head;
1058 	unsigned int elts_tail = txq->elts_tail;
1059 	struct txq_elt (*elts)[elts_n] = txq->elts;
1060 	linear_t (*elts_linear)[elts_n] = txq->elts_linear;
1061 	struct ibv_mr *mr_linear = txq->mr_linear;
1062 
1063 	DEBUG("%p: freeing WRs", (void *)txq);
1064 	txq->elts_n = 0;
1065 	txq->elts_head = 0;
1066 	txq->elts_tail = 0;
1067 	txq->elts_comp = 0;
1068 	txq->elts_comp_cd = 0;
1069 	txq->elts_comp_cd_init = 0;
1070 	txq->elts = NULL;
1071 	txq->elts_linear = NULL;
1072 	txq->mr_linear = NULL;
1073 	if (mr_linear != NULL)
1074 		claim_zero(ibv_dereg_mr(mr_linear));
1075 
1076 	rte_free(elts_linear);
1077 	if (elts == NULL)
1078 		return;
1079 	while (elts_tail != elts_head) {
1080 		struct txq_elt *elt = &(*elts)[elts_tail];
1081 
1082 		assert(elt->buf != NULL);
1083 		rte_pktmbuf_free(elt->buf);
1084 #ifndef NDEBUG
1085 		/* Poisoning. */
1086 		memset(elt, 0x77, sizeof(*elt));
1087 #endif
1088 		if (++elts_tail == elts_n)
1089 			elts_tail = 0;
1090 	}
1091 	rte_free(elts);
1092 }
1093 
1094 
1095 /**
1096  * Clean up a TX queue.
1097  *
1098  * Destroy objects, free allocated memory and reset the structure for reuse.
1099  *
1100  * @param txq
1101  *   Pointer to TX queue structure.
1102  */
1103 static void
1104 txq_cleanup(struct txq *txq)
1105 {
1106 	struct ibv_exp_release_intf_params params;
1107 	size_t i;
1108 
1109 	DEBUG("cleaning up %p", (void *)txq);
1110 	txq_free_elts(txq);
1111 	if (txq->if_qp != NULL) {
1112 		assert(txq->priv != NULL);
1113 		assert(txq->priv->ctx != NULL);
1114 		assert(txq->qp != NULL);
1115 		params = (struct ibv_exp_release_intf_params){
1116 			.comp_mask = 0,
1117 		};
1118 		claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1119 						txq->if_qp,
1120 						&params));
1121 	}
1122 	if (txq->if_cq != NULL) {
1123 		assert(txq->priv != NULL);
1124 		assert(txq->priv->ctx != NULL);
1125 		assert(txq->cq != NULL);
1126 		params = (struct ibv_exp_release_intf_params){
1127 			.comp_mask = 0,
1128 		};
1129 		claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1130 						txq->if_cq,
1131 						&params));
1132 	}
1133 	if (txq->qp != NULL)
1134 		claim_zero(ibv_destroy_qp(txq->qp));
1135 	if (txq->cq != NULL)
1136 		claim_zero(ibv_destroy_cq(txq->cq));
1137 	if (txq->rd != NULL) {
1138 		struct ibv_exp_destroy_res_domain_attr attr = {
1139 			.comp_mask = 0,
1140 		};
1141 
1142 		assert(txq->priv != NULL);
1143 		assert(txq->priv->ctx != NULL);
1144 		claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
1145 						      txq->rd,
1146 						      &attr));
1147 	}
1148 	for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1149 		if (txq->mp2mr[i].mp == NULL)
1150 			break;
1151 		assert(txq->mp2mr[i].mr != NULL);
1152 		claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1153 	}
1154 	memset(txq, 0, sizeof(*txq));
1155 }
1156 
1157 /**
1158  * Manage TX completions.
1159  *
1160  * When sending a burst, mlx4_tx_burst() posts several WRs.
1161  * To improve performance, a completion event is only required once every
1162  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
1163  * for other WRs, but this information would not be used anyway.
1164  *
1165  * @param txq
1166  *   Pointer to TX queue structure.
1167  *
1168  * @return
1169  *   0 on success, -1 on failure.
1170  */
1171 static int
1172 txq_complete(struct txq *txq)
1173 {
1174 	unsigned int elts_comp = txq->elts_comp;
1175 	unsigned int elts_tail = txq->elts_tail;
1176 	const unsigned int elts_n = txq->elts_n;
1177 	int wcs_n;
1178 
1179 	if (unlikely(elts_comp == 0))
1180 		return 0;
1181 #ifdef DEBUG_SEND
1182 	DEBUG("%p: processing %u work requests completions",
1183 	      (void *)txq, elts_comp);
1184 #endif
1185 	wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
1186 	if (unlikely(wcs_n == 0))
1187 		return 0;
1188 	if (unlikely(wcs_n < 0)) {
1189 		DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
1190 		      (void *)txq, wcs_n);
1191 		return -1;
1192 	}
1193 	elts_comp -= wcs_n;
1194 	assert(elts_comp <= txq->elts_comp);
1195 	/*
1196 	 * Assume WC status is successful as nothing can be done about it
1197 	 * anyway.
1198 	 */
1199 	elts_tail += wcs_n * txq->elts_comp_cd_init;
1200 	if (elts_tail >= elts_n)
1201 		elts_tail -= elts_n;
1202 	txq->elts_tail = elts_tail;
1203 	txq->elts_comp = elts_comp;
1204 	return 0;
1205 }
1206 
1207 struct mlx4_check_mempool_data {
1208 	int ret;
1209 	char *start;
1210 	char *end;
1211 };
1212 
1213 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
1214 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
1215 	void *opaque, struct rte_mempool_memhdr *memhdr,
1216 	unsigned mem_idx)
1217 {
1218 	struct mlx4_check_mempool_data *data = opaque;
1219 
1220 	(void)mp;
1221 	(void)mem_idx;
1222 
1223 	/* It already failed, skip the next chunks. */
1224 	if (data->ret != 0)
1225 		return;
1226 	/* It is the first chunk. */
1227 	if (data->start == NULL && data->end == NULL) {
1228 		data->start = memhdr->addr;
1229 		data->end = data->start + memhdr->len;
1230 		return;
1231 	}
1232 	if (data->end == memhdr->addr) {
1233 		data->end += memhdr->len;
1234 		return;
1235 	}
1236 	if (data->start == (char *)memhdr->addr + memhdr->len) {
1237 		data->start -= memhdr->len;
1238 		return;
1239 	}
1240 	/* Error, mempool is not virtually contigous. */
1241 	data->ret = -1;
1242 }
1243 
1244 /**
1245  * Check if a mempool can be used: it must be virtually contiguous.
1246  *
1247  * @param[in] mp
1248  *   Pointer to memory pool.
1249  * @param[out] start
1250  *   Pointer to the start address of the mempool virtual memory area
1251  * @param[out] end
1252  *   Pointer to the end address of the mempool virtual memory area
1253  *
1254  * @return
1255  *   0 on success (mempool is virtually contiguous), -1 on error.
1256  */
1257 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
1258 	uintptr_t *end)
1259 {
1260 	struct mlx4_check_mempool_data data;
1261 
1262 	memset(&data, 0, sizeof(data));
1263 	rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
1264 	*start = (uintptr_t)data.start;
1265 	*end = (uintptr_t)data.end;
1266 
1267 	return data.ret;
1268 }
1269 
1270 /* For best performance, this function should not be inlined. */
1271 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1272 	__attribute__((noinline));
1273 
1274 /**
1275  * Register mempool as a memory region.
1276  *
1277  * @param pd
1278  *   Pointer to protection domain.
1279  * @param mp
1280  *   Pointer to memory pool.
1281  *
1282  * @return
1283  *   Memory region pointer, NULL in case of error.
1284  */
1285 static struct ibv_mr *
1286 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1287 {
1288 	const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1289 	uintptr_t start;
1290 	uintptr_t end;
1291 	unsigned int i;
1292 
1293 	if (mlx4_check_mempool(mp, &start, &end) != 0) {
1294 		ERROR("mempool %p: not virtually contiguous",
1295 			(void *)mp);
1296 		return NULL;
1297 	}
1298 
1299 	DEBUG("mempool %p area start=%p end=%p size=%zu",
1300 	      (void *)mp, (void *)start, (void *)end,
1301 	      (size_t)(end - start));
1302 	/* Round start and end to page boundary if found in memory segments. */
1303 	for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1304 		uintptr_t addr = (uintptr_t)ms[i].addr;
1305 		size_t len = ms[i].len;
1306 		unsigned int align = ms[i].hugepage_sz;
1307 
1308 		if ((start > addr) && (start < addr + len))
1309 			start = RTE_ALIGN_FLOOR(start, align);
1310 		if ((end > addr) && (end < addr + len))
1311 			end = RTE_ALIGN_CEIL(end, align);
1312 	}
1313 	DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1314 	      (void *)mp, (void *)start, (void *)end,
1315 	      (size_t)(end - start));
1316 	return ibv_reg_mr(pd,
1317 			  (void *)start,
1318 			  end - start,
1319 			  IBV_ACCESS_LOCAL_WRITE);
1320 }
1321 
1322 /**
1323  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1324  * the cloned mbuf is allocated is returned instead.
1325  *
1326  * @param buf
1327  *   Pointer to mbuf.
1328  *
1329  * @return
1330  *   Memory pool where data is located for given mbuf.
1331  */
1332 static struct rte_mempool *
1333 txq_mb2mp(struct rte_mbuf *buf)
1334 {
1335 	if (unlikely(RTE_MBUF_INDIRECT(buf)))
1336 		return rte_mbuf_from_indirect(buf)->pool;
1337 	return buf->pool;
1338 }
1339 
1340 /**
1341  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1342  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1343  * remove an entry first.
1344  *
1345  * @param txq
1346  *   Pointer to TX queue structure.
1347  * @param[in] mp
1348  *   Memory Pool for which a Memory Region lkey must be returned.
1349  *
1350  * @return
1351  *   mr->lkey on success, (uint32_t)-1 on failure.
1352  */
1353 static uint32_t
1354 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1355 {
1356 	unsigned int i;
1357 	struct ibv_mr *mr;
1358 
1359 	for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1360 		if (unlikely(txq->mp2mr[i].mp == NULL)) {
1361 			/* Unknown MP, add a new MR for it. */
1362 			break;
1363 		}
1364 		if (txq->mp2mr[i].mp == mp) {
1365 			assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1366 			assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1367 			return txq->mp2mr[i].lkey;
1368 		}
1369 	}
1370 	/* Add a new entry, register MR first. */
1371 	DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1372 	      (void *)txq, mp->name, (void *)mp);
1373 	mr = mlx4_mp2mr(txq->priv->pd, mp);
1374 	if (unlikely(mr == NULL)) {
1375 		DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1376 		      (void *)txq);
1377 		return (uint32_t)-1;
1378 	}
1379 	if (unlikely(i == elemof(txq->mp2mr))) {
1380 		/* Table is full, remove oldest entry. */
1381 		DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1382 		      (void *)txq);
1383 		--i;
1384 		claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1385 		memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1386 			(sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1387 	}
1388 	/* Store the new entry. */
1389 	txq->mp2mr[i].mp = mp;
1390 	txq->mp2mr[i].mr = mr;
1391 	txq->mp2mr[i].lkey = mr->lkey;
1392 	DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1393 	      (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1394 	return txq->mp2mr[i].lkey;
1395 }
1396 
1397 struct txq_mp2mr_mbuf_check_data {
1398 	int ret;
1399 };
1400 
1401 /**
1402  * Callback function for rte_mempool_obj_iter() to check whether a given
1403  * mempool object looks like a mbuf.
1404  *
1405  * @param[in] mp
1406  *   The mempool pointer
1407  * @param[in] arg
1408  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1409  *   return value.
1410  * @param[in] obj
1411  *   Object address.
1412  * @param index
1413  *   Object index, unused.
1414  */
1415 static void
1416 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1417 	uint32_t index __rte_unused)
1418 {
1419 	struct txq_mp2mr_mbuf_check_data *data = arg;
1420 	struct rte_mbuf *buf = obj;
1421 
1422 	/* Check whether mbuf structure fits element size and whether mempool
1423 	 * pointer is valid. */
1424 	if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1425 		data->ret = -1;
1426 }
1427 
1428 /**
1429  * Iterator function for rte_mempool_walk() to register existing mempools and
1430  * fill the MP to MR cache of a TX queue.
1431  *
1432  * @param[in] mp
1433  *   Memory Pool to register.
1434  * @param *arg
1435  *   Pointer to TX queue structure.
1436  */
1437 static void
1438 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1439 {
1440 	struct txq *txq = arg;
1441 	struct txq_mp2mr_mbuf_check_data data = {
1442 		.ret = 0,
1443 	};
1444 
1445 	/* Register mempool only if the first element looks like a mbuf. */
1446 	if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1447 			data.ret == -1)
1448 		return;
1449 	txq_mp2mr(txq, mp);
1450 }
1451 
1452 #if MLX4_PMD_SGE_WR_N > 1
1453 
1454 /**
1455  * Copy scattered mbuf contents to a single linear buffer.
1456  *
1457  * @param[out] linear
1458  *   Linear output buffer.
1459  * @param[in] buf
1460  *   Scattered input buffer.
1461  *
1462  * @return
1463  *   Number of bytes copied to the output buffer or 0 if not large enough.
1464  */
1465 static unsigned int
1466 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1467 {
1468 	unsigned int size = 0;
1469 	unsigned int offset;
1470 
1471 	do {
1472 		unsigned int len = DATA_LEN(buf);
1473 
1474 		offset = size;
1475 		size += len;
1476 		if (unlikely(size > sizeof(*linear)))
1477 			return 0;
1478 		memcpy(&(*linear)[offset],
1479 		       rte_pktmbuf_mtod(buf, uint8_t *),
1480 		       len);
1481 		buf = NEXT(buf);
1482 	} while (buf != NULL);
1483 	return size;
1484 }
1485 
1486 /**
1487  * Handle scattered buffers for mlx4_tx_burst().
1488  *
1489  * @param txq
1490  *   TX queue structure.
1491  * @param segs
1492  *   Number of segments in buf.
1493  * @param elt
1494  *   TX queue element to fill.
1495  * @param[in] buf
1496  *   Buffer to process.
1497  * @param elts_head
1498  *   Index of the linear buffer to use if necessary (normally txq->elts_head).
1499  * @param[out] sges
1500  *   Array filled with SGEs on success.
1501  *
1502  * @return
1503  *   A structure containing the processed packet size in bytes and the
1504  *   number of SGEs. Both fields are set to (unsigned int)-1 in case of
1505  *   failure.
1506  */
1507 static struct tx_burst_sg_ret {
1508 	unsigned int length;
1509 	unsigned int num;
1510 }
1511 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1512 	    struct rte_mbuf *buf, unsigned int elts_head,
1513 	    struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1514 {
1515 	unsigned int sent_size = 0;
1516 	unsigned int j;
1517 	int linearize = 0;
1518 
1519 	/* When there are too many segments, extra segments are
1520 	 * linearized in the last SGE. */
1521 	if (unlikely(segs > elemof(*sges))) {
1522 		segs = (elemof(*sges) - 1);
1523 		linearize = 1;
1524 	}
1525 	/* Update element. */
1526 	elt->buf = buf;
1527 	/* Register segments as SGEs. */
1528 	for (j = 0; (j != segs); ++j) {
1529 		struct ibv_sge *sge = &(*sges)[j];
1530 		uint32_t lkey;
1531 
1532 		/* Retrieve Memory Region key for this memory pool. */
1533 		lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1534 		if (unlikely(lkey == (uint32_t)-1)) {
1535 			/* MR does not exist. */
1536 			DEBUG("%p: unable to get MP <-> MR association",
1537 			      (void *)txq);
1538 			/* Clean up TX element. */
1539 			elt->buf = NULL;
1540 			goto stop;
1541 		}
1542 		/* Update SGE. */
1543 		sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1544 		if (txq->priv->vf)
1545 			rte_prefetch0((volatile void *)
1546 				      (uintptr_t)sge->addr);
1547 		sge->length = DATA_LEN(buf);
1548 		sge->lkey = lkey;
1549 		sent_size += sge->length;
1550 		buf = NEXT(buf);
1551 	}
1552 	/* If buf is not NULL here and is not going to be linearized,
1553 	 * nb_segs is not valid. */
1554 	assert(j == segs);
1555 	assert((buf == NULL) || (linearize));
1556 	/* Linearize extra segments. */
1557 	if (linearize) {
1558 		struct ibv_sge *sge = &(*sges)[segs];
1559 		linear_t *linear = &(*txq->elts_linear)[elts_head];
1560 		unsigned int size = linearize_mbuf(linear, buf);
1561 
1562 		assert(segs == (elemof(*sges) - 1));
1563 		if (size == 0) {
1564 			/* Invalid packet. */
1565 			DEBUG("%p: packet too large to be linearized.",
1566 			      (void *)txq);
1567 			/* Clean up TX element. */
1568 			elt->buf = NULL;
1569 			goto stop;
1570 		}
1571 		/* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1572 		if (elemof(*sges) == 1) {
1573 			do {
1574 				struct rte_mbuf *next = NEXT(buf);
1575 
1576 				rte_pktmbuf_free_seg(buf);
1577 				buf = next;
1578 			} while (buf != NULL);
1579 			elt->buf = NULL;
1580 		}
1581 		/* Update SGE. */
1582 		sge->addr = (uintptr_t)&(*linear)[0];
1583 		sge->length = size;
1584 		sge->lkey = txq->mr_linear->lkey;
1585 		sent_size += size;
1586 		/* Include last segment. */
1587 		segs++;
1588 	}
1589 	return (struct tx_burst_sg_ret){
1590 		.length = sent_size,
1591 		.num = segs,
1592 	};
1593 stop:
1594 	return (struct tx_burst_sg_ret){
1595 		.length = -1,
1596 		.num = -1,
1597 	};
1598 }
1599 
1600 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1601 
1602 /**
1603  * DPDK callback for TX.
1604  *
1605  * @param dpdk_txq
1606  *   Generic pointer to TX queue structure.
1607  * @param[in] pkts
1608  *   Packets to transmit.
1609  * @param pkts_n
1610  *   Number of packets in array.
1611  *
1612  * @return
1613  *   Number of packets successfully transmitted (<= pkts_n).
1614  */
1615 static uint16_t
1616 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1617 {
1618 	struct txq *txq = (struct txq *)dpdk_txq;
1619 	unsigned int elts_head = txq->elts_head;
1620 	const unsigned int elts_n = txq->elts_n;
1621 	unsigned int elts_comp_cd = txq->elts_comp_cd;
1622 	unsigned int elts_comp = 0;
1623 	unsigned int i;
1624 	unsigned int max;
1625 	int err;
1626 
1627 	assert(elts_comp_cd != 0);
1628 	txq_complete(txq);
1629 	max = (elts_n - (elts_head - txq->elts_tail));
1630 	if (max > elts_n)
1631 		max -= elts_n;
1632 	assert(max >= 1);
1633 	assert(max <= elts_n);
1634 	/* Always leave one free entry in the ring. */
1635 	--max;
1636 	if (max == 0)
1637 		return 0;
1638 	if (max > pkts_n)
1639 		max = pkts_n;
1640 	for (i = 0; (i != max); ++i) {
1641 		struct rte_mbuf *buf = pkts[i];
1642 		unsigned int elts_head_next =
1643 			(((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1644 		struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1645 		struct txq_elt *elt = &(*txq->elts)[elts_head];
1646 		unsigned int segs = NB_SEGS(buf);
1647 #ifdef MLX4_PMD_SOFT_COUNTERS
1648 		unsigned int sent_size = 0;
1649 #endif
1650 		uint32_t send_flags = 0;
1651 
1652 		/* Clean up old buffer. */
1653 		if (likely(elt->buf != NULL)) {
1654 			struct rte_mbuf *tmp = elt->buf;
1655 
1656 #ifndef NDEBUG
1657 			/* Poisoning. */
1658 			memset(elt, 0x66, sizeof(*elt));
1659 #endif
1660 			/* Faster than rte_pktmbuf_free(). */
1661 			do {
1662 				struct rte_mbuf *next = NEXT(tmp);
1663 
1664 				rte_pktmbuf_free_seg(tmp);
1665 				tmp = next;
1666 			} while (tmp != NULL);
1667 		}
1668 		/* Request TX completion. */
1669 		if (unlikely(--elts_comp_cd == 0)) {
1670 			elts_comp_cd = txq->elts_comp_cd_init;
1671 			++elts_comp;
1672 			send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1673 		}
1674 		/* Should we enable HW CKSUM offload */
1675 		if (buf->ol_flags &
1676 		    (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1677 			send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1678 			/* HW does not support checksum offloads at arbitrary
1679 			 * offsets but automatically recognizes the packet
1680 			 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1681 			 * tunnels are currently supported. */
1682 			if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1683 				send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1684 		}
1685 		if (likely(segs == 1)) {
1686 			uintptr_t addr;
1687 			uint32_t length;
1688 			uint32_t lkey;
1689 
1690 			/* Retrieve buffer information. */
1691 			addr = rte_pktmbuf_mtod(buf, uintptr_t);
1692 			length = DATA_LEN(buf);
1693 			/* Retrieve Memory Region key for this memory pool. */
1694 			lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1695 			if (unlikely(lkey == (uint32_t)-1)) {
1696 				/* MR does not exist. */
1697 				DEBUG("%p: unable to get MP <-> MR"
1698 				      " association", (void *)txq);
1699 				/* Clean up TX element. */
1700 				elt->buf = NULL;
1701 				goto stop;
1702 			}
1703 			/* Update element. */
1704 			elt->buf = buf;
1705 			if (txq->priv->vf)
1706 				rte_prefetch0((volatile void *)
1707 					      (uintptr_t)addr);
1708 			RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1709 			/* Put packet into send queue. */
1710 #if MLX4_PMD_MAX_INLINE > 0
1711 			if (length <= txq->max_inline)
1712 				err = txq->if_qp->send_pending_inline
1713 					(txq->qp,
1714 					 (void *)addr,
1715 					 length,
1716 					 send_flags);
1717 			else
1718 #endif
1719 				err = txq->if_qp->send_pending
1720 					(txq->qp,
1721 					 addr,
1722 					 length,
1723 					 lkey,
1724 					 send_flags);
1725 			if (unlikely(err))
1726 				goto stop;
1727 #ifdef MLX4_PMD_SOFT_COUNTERS
1728 			sent_size += length;
1729 #endif
1730 		} else {
1731 #if MLX4_PMD_SGE_WR_N > 1
1732 			struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1733 			struct tx_burst_sg_ret ret;
1734 
1735 			ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1736 					  &sges);
1737 			if (ret.length == (unsigned int)-1)
1738 				goto stop;
1739 			RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1740 			/* Put SG list into send queue. */
1741 			err = txq->if_qp->send_pending_sg_list
1742 				(txq->qp,
1743 				 sges,
1744 				 ret.num,
1745 				 send_flags);
1746 			if (unlikely(err))
1747 				goto stop;
1748 #ifdef MLX4_PMD_SOFT_COUNTERS
1749 			sent_size += ret.length;
1750 #endif
1751 #else /* MLX4_PMD_SGE_WR_N > 1 */
1752 			DEBUG("%p: TX scattered buffers support not"
1753 			      " compiled in", (void *)txq);
1754 			goto stop;
1755 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1756 		}
1757 		elts_head = elts_head_next;
1758 #ifdef MLX4_PMD_SOFT_COUNTERS
1759 		/* Increment sent bytes counter. */
1760 		txq->stats.obytes += sent_size;
1761 #endif
1762 	}
1763 stop:
1764 	/* Take a shortcut if nothing must be sent. */
1765 	if (unlikely(i == 0))
1766 		return 0;
1767 #ifdef MLX4_PMD_SOFT_COUNTERS
1768 	/* Increment sent packets counter. */
1769 	txq->stats.opackets += i;
1770 #endif
1771 	/* Ring QP doorbell. */
1772 	err = txq->if_qp->send_flush(txq->qp);
1773 	if (unlikely(err)) {
1774 		/* A nonzero value is not supposed to be returned.
1775 		 * Nothing can be done about it. */
1776 		DEBUG("%p: send_flush() failed with error %d",
1777 		      (void *)txq, err);
1778 	}
1779 	txq->elts_head = elts_head;
1780 	txq->elts_comp += elts_comp;
1781 	txq->elts_comp_cd = elts_comp_cd;
1782 	return i;
1783 }
1784 
1785 /**
1786  * DPDK callback for TX in secondary processes.
1787  *
1788  * This function configures all queues from primary process information
1789  * if necessary before reverting to the normal TX burst callback.
1790  *
1791  * @param dpdk_txq
1792  *   Generic pointer to TX queue structure.
1793  * @param[in] pkts
1794  *   Packets to transmit.
1795  * @param pkts_n
1796  *   Number of packets in array.
1797  *
1798  * @return
1799  *   Number of packets successfully transmitted (<= pkts_n).
1800  */
1801 static uint16_t
1802 mlx4_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
1803 			      uint16_t pkts_n)
1804 {
1805 	struct txq *txq = dpdk_txq;
1806 	struct priv *priv = mlx4_secondary_data_setup(txq->priv);
1807 	struct priv *primary_priv;
1808 	unsigned int index;
1809 
1810 	if (priv == NULL)
1811 		return 0;
1812 	primary_priv =
1813 		mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
1814 	/* Look for queue index in both private structures. */
1815 	for (index = 0; index != priv->txqs_n; ++index)
1816 		if (((*primary_priv->txqs)[index] == txq) ||
1817 		    ((*priv->txqs)[index] == txq))
1818 			break;
1819 	if (index == priv->txqs_n)
1820 		return 0;
1821 	txq = (*priv->txqs)[index];
1822 	return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
1823 }
1824 
1825 /**
1826  * Configure a TX queue.
1827  *
1828  * @param dev
1829  *   Pointer to Ethernet device structure.
1830  * @param txq
1831  *   Pointer to TX queue structure.
1832  * @param desc
1833  *   Number of descriptors to configure in queue.
1834  * @param socket
1835  *   NUMA socket on which memory must be allocated.
1836  * @param[in] conf
1837  *   Thresholds parameters.
1838  *
1839  * @return
1840  *   0 on success, errno value on failure.
1841  */
1842 static int
1843 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1844 	  unsigned int socket, const struct rte_eth_txconf *conf)
1845 {
1846 	struct priv *priv = mlx4_get_priv(dev);
1847 	struct txq tmpl = {
1848 		.priv = priv,
1849 		.socket = socket
1850 	};
1851 	union {
1852 		struct ibv_exp_query_intf_params params;
1853 		struct ibv_exp_qp_init_attr init;
1854 		struct ibv_exp_res_domain_init_attr rd;
1855 		struct ibv_exp_cq_init_attr cq;
1856 		struct ibv_exp_qp_attr mod;
1857 	} attr;
1858 	enum ibv_exp_query_intf_status status;
1859 	int ret = 0;
1860 
1861 	(void)conf; /* Thresholds configuration (ignored). */
1862 	if (priv == NULL)
1863 		return EINVAL;
1864 	if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1865 		ERROR("%p: invalid number of TX descriptors (must be a"
1866 		      " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1867 		return EINVAL;
1868 	}
1869 	desc /= MLX4_PMD_SGE_WR_N;
1870 	/* MRs will be registered in mp2mr[] later. */
1871 	attr.rd = (struct ibv_exp_res_domain_init_attr){
1872 		.comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1873 			      IBV_EXP_RES_DOMAIN_MSG_MODEL),
1874 		.thread_model = IBV_EXP_THREAD_SINGLE,
1875 		.msg_model = IBV_EXP_MSG_HIGH_BW,
1876 	};
1877 	tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1878 	if (tmpl.rd == NULL) {
1879 		ret = ENOMEM;
1880 		ERROR("%p: RD creation failure: %s",
1881 		      (void *)dev, strerror(ret));
1882 		goto error;
1883 	}
1884 	attr.cq = (struct ibv_exp_cq_init_attr){
1885 		.comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1886 		.res_domain = tmpl.rd,
1887 	};
1888 	tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1889 	if (tmpl.cq == NULL) {
1890 		ret = ENOMEM;
1891 		ERROR("%p: CQ creation failure: %s",
1892 		      (void *)dev, strerror(ret));
1893 		goto error;
1894 	}
1895 	DEBUG("priv->device_attr.max_qp_wr is %d",
1896 	      priv->device_attr.max_qp_wr);
1897 	DEBUG("priv->device_attr.max_sge is %d",
1898 	      priv->device_attr.max_sge);
1899 	attr.init = (struct ibv_exp_qp_init_attr){
1900 		/* CQ to be associated with the send queue. */
1901 		.send_cq = tmpl.cq,
1902 		/* CQ to be associated with the receive queue. */
1903 		.recv_cq = tmpl.cq,
1904 		.cap = {
1905 			/* Max number of outstanding WRs. */
1906 			.max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1907 					priv->device_attr.max_qp_wr :
1908 					desc),
1909 			/* Max number of scatter/gather elements in a WR. */
1910 			.max_send_sge = ((priv->device_attr.max_sge <
1911 					  MLX4_PMD_SGE_WR_N) ?
1912 					 priv->device_attr.max_sge :
1913 					 MLX4_PMD_SGE_WR_N),
1914 #if MLX4_PMD_MAX_INLINE > 0
1915 			.max_inline_data = MLX4_PMD_MAX_INLINE,
1916 #endif
1917 		},
1918 		.qp_type = IBV_QPT_RAW_PACKET,
1919 		/* Do *NOT* enable this, completions events are managed per
1920 		 * TX burst. */
1921 		.sq_sig_all = 0,
1922 		.pd = priv->pd,
1923 		.res_domain = tmpl.rd,
1924 		.comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1925 			      IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1926 	};
1927 	tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1928 	if (tmpl.qp == NULL) {
1929 		ret = (errno ? errno : EINVAL);
1930 		ERROR("%p: QP creation failure: %s",
1931 		      (void *)dev, strerror(ret));
1932 		goto error;
1933 	}
1934 #if MLX4_PMD_MAX_INLINE > 0
1935 	/* ibv_create_qp() updates this value. */
1936 	tmpl.max_inline = attr.init.cap.max_inline_data;
1937 #endif
1938 	attr.mod = (struct ibv_exp_qp_attr){
1939 		/* Move the QP to this state. */
1940 		.qp_state = IBV_QPS_INIT,
1941 		/* Primary port number. */
1942 		.port_num = priv->port
1943 	};
1944 	ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1945 				(IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1946 	if (ret) {
1947 		ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1948 		      (void *)dev, strerror(ret));
1949 		goto error;
1950 	}
1951 	ret = txq_alloc_elts(&tmpl, desc);
1952 	if (ret) {
1953 		ERROR("%p: TXQ allocation failed: %s",
1954 		      (void *)dev, strerror(ret));
1955 		goto error;
1956 	}
1957 	attr.mod = (struct ibv_exp_qp_attr){
1958 		.qp_state = IBV_QPS_RTR
1959 	};
1960 	ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1961 	if (ret) {
1962 		ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1963 		      (void *)dev, strerror(ret));
1964 		goto error;
1965 	}
1966 	attr.mod.qp_state = IBV_QPS_RTS;
1967 	ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1968 	if (ret) {
1969 		ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1970 		      (void *)dev, strerror(ret));
1971 		goto error;
1972 	}
1973 	attr.params = (struct ibv_exp_query_intf_params){
1974 		.intf_scope = IBV_EXP_INTF_GLOBAL,
1975 		.intf = IBV_EXP_INTF_CQ,
1976 		.obj = tmpl.cq,
1977 	};
1978 	tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1979 	if (tmpl.if_cq == NULL) {
1980 		ERROR("%p: CQ interface family query failed with status %d",
1981 		      (void *)dev, status);
1982 		goto error;
1983 	}
1984 	attr.params = (struct ibv_exp_query_intf_params){
1985 		.intf_scope = IBV_EXP_INTF_GLOBAL,
1986 		.intf = IBV_EXP_INTF_QP_BURST,
1987 		.obj = tmpl.qp,
1988 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1989 		/* MC loopback must be disabled when not using a VF. */
1990 		.family_flags =
1991 			(!priv->vf ?
1992 			 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1993 			 0),
1994 #endif
1995 	};
1996 	tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1997 	if (tmpl.if_qp == NULL) {
1998 		ERROR("%p: QP interface family query failed with status %d",
1999 		      (void *)dev, status);
2000 		goto error;
2001 	}
2002 	/* Clean up txq in case we're reinitializing it. */
2003 	DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
2004 	txq_cleanup(txq);
2005 	*txq = tmpl;
2006 	DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
2007 	/* Pre-register known mempools. */
2008 	rte_mempool_walk(txq_mp2mr_iter, txq);
2009 	assert(ret == 0);
2010 	return 0;
2011 error:
2012 	txq_cleanup(&tmpl);
2013 	assert(ret > 0);
2014 	return ret;
2015 }
2016 
2017 /**
2018  * DPDK callback to configure a TX queue.
2019  *
2020  * @param dev
2021  *   Pointer to Ethernet device structure.
2022  * @param idx
2023  *   TX queue index.
2024  * @param desc
2025  *   Number of descriptors to configure in queue.
2026  * @param socket
2027  *   NUMA socket on which memory must be allocated.
2028  * @param[in] conf
2029  *   Thresholds parameters.
2030  *
2031  * @return
2032  *   0 on success, negative errno value on failure.
2033  */
2034 static int
2035 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2036 		    unsigned int socket, const struct rte_eth_txconf *conf)
2037 {
2038 	struct priv *priv = dev->data->dev_private;
2039 	struct txq *txq = (*priv->txqs)[idx];
2040 	int ret;
2041 
2042 	if (mlx4_is_secondary())
2043 		return -E_RTE_SECONDARY;
2044 	priv_lock(priv);
2045 	DEBUG("%p: configuring queue %u for %u descriptors",
2046 	      (void *)dev, idx, desc);
2047 	if (idx >= priv->txqs_n) {
2048 		ERROR("%p: queue index out of range (%u >= %u)",
2049 		      (void *)dev, idx, priv->txqs_n);
2050 		priv_unlock(priv);
2051 		return -EOVERFLOW;
2052 	}
2053 	if (txq != NULL) {
2054 		DEBUG("%p: reusing already allocated queue index %u (%p)",
2055 		      (void *)dev, idx, (void *)txq);
2056 		if (priv->started) {
2057 			priv_unlock(priv);
2058 			return -EEXIST;
2059 		}
2060 		(*priv->txqs)[idx] = NULL;
2061 		txq_cleanup(txq);
2062 	} else {
2063 		txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
2064 		if (txq == NULL) {
2065 			ERROR("%p: unable to allocate queue index %u",
2066 			      (void *)dev, idx);
2067 			priv_unlock(priv);
2068 			return -ENOMEM;
2069 		}
2070 	}
2071 	ret = txq_setup(dev, txq, desc, socket, conf);
2072 	if (ret)
2073 		rte_free(txq);
2074 	else {
2075 		txq->stats.idx = idx;
2076 		DEBUG("%p: adding TX queue %p to list",
2077 		      (void *)dev, (void *)txq);
2078 		(*priv->txqs)[idx] = txq;
2079 		/* Update send callback. */
2080 		dev->tx_pkt_burst = mlx4_tx_burst;
2081 	}
2082 	priv_unlock(priv);
2083 	return -ret;
2084 }
2085 
2086 /**
2087  * DPDK callback to release a TX queue.
2088  *
2089  * @param dpdk_txq
2090  *   Generic TX queue pointer.
2091  */
2092 static void
2093 mlx4_tx_queue_release(void *dpdk_txq)
2094 {
2095 	struct txq *txq = (struct txq *)dpdk_txq;
2096 	struct priv *priv;
2097 	unsigned int i;
2098 
2099 	if (mlx4_is_secondary())
2100 		return;
2101 	if (txq == NULL)
2102 		return;
2103 	priv = txq->priv;
2104 	priv_lock(priv);
2105 	for (i = 0; (i != priv->txqs_n); ++i)
2106 		if ((*priv->txqs)[i] == txq) {
2107 			DEBUG("%p: removing TX queue %p from list",
2108 			      (void *)priv->dev, (void *)txq);
2109 			(*priv->txqs)[i] = NULL;
2110 			break;
2111 		}
2112 	txq_cleanup(txq);
2113 	rte_free(txq);
2114 	priv_unlock(priv);
2115 }
2116 
2117 /* RX queues handling. */
2118 
2119 /**
2120  * Allocate RX queue elements with scattered packets support.
2121  *
2122  * @param rxq
2123  *   Pointer to RX queue structure.
2124  * @param elts_n
2125  *   Number of elements to allocate.
2126  * @param[in] pool
2127  *   If not NULL, fetch buffers from this array instead of allocating them
2128  *   with rte_pktmbuf_alloc().
2129  *
2130  * @return
2131  *   0 on success, errno value on failure.
2132  */
2133 static int
2134 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
2135 		  struct rte_mbuf **pool)
2136 {
2137 	unsigned int i;
2138 	struct rxq_elt_sp (*elts)[elts_n] =
2139 		rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2140 				  rxq->socket);
2141 	int ret = 0;
2142 
2143 	if (elts == NULL) {
2144 		ERROR("%p: can't allocate packets array", (void *)rxq);
2145 		ret = ENOMEM;
2146 		goto error;
2147 	}
2148 	/* For each WR (packet). */
2149 	for (i = 0; (i != elts_n); ++i) {
2150 		unsigned int j;
2151 		struct rxq_elt_sp *elt = &(*elts)[i];
2152 		struct ibv_recv_wr *wr = &elt->wr;
2153 		struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
2154 
2155 		/* These two arrays must have the same size. */
2156 		assert(elemof(elt->sges) == elemof(elt->bufs));
2157 		/* Configure WR. */
2158 		wr->wr_id = i;
2159 		wr->next = &(*elts)[(i + 1)].wr;
2160 		wr->sg_list = &(*sges)[0];
2161 		wr->num_sge = elemof(*sges);
2162 		/* For each SGE (segment). */
2163 		for (j = 0; (j != elemof(elt->bufs)); ++j) {
2164 			struct ibv_sge *sge = &(*sges)[j];
2165 			struct rte_mbuf *buf;
2166 
2167 			if (pool != NULL) {
2168 				buf = *(pool++);
2169 				assert(buf != NULL);
2170 				rte_pktmbuf_reset(buf);
2171 			} else
2172 				buf = rte_pktmbuf_alloc(rxq->mp);
2173 			if (buf == NULL) {
2174 				assert(pool == NULL);
2175 				ERROR("%p: empty mbuf pool", (void *)rxq);
2176 				ret = ENOMEM;
2177 				goto error;
2178 			}
2179 			elt->bufs[j] = buf;
2180 			/* Headroom is reserved by rte_pktmbuf_alloc(). */
2181 			assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2182 			/* Buffer is supposed to be empty. */
2183 			assert(rte_pktmbuf_data_len(buf) == 0);
2184 			assert(rte_pktmbuf_pkt_len(buf) == 0);
2185 			/* sge->addr must be able to store a pointer. */
2186 			assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2187 			if (j == 0) {
2188 				/* The first SGE keeps its headroom. */
2189 				sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
2190 				sge->length = (buf->buf_len -
2191 					       RTE_PKTMBUF_HEADROOM);
2192 			} else {
2193 				/* Subsequent SGEs lose theirs. */
2194 				assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2195 				SET_DATA_OFF(buf, 0);
2196 				sge->addr = (uintptr_t)buf->buf_addr;
2197 				sge->length = buf->buf_len;
2198 			}
2199 			sge->lkey = rxq->mr->lkey;
2200 			/* Redundant check for tailroom. */
2201 			assert(sge->length == rte_pktmbuf_tailroom(buf));
2202 		}
2203 	}
2204 	/* The last WR pointer must be NULL. */
2205 	(*elts)[(i - 1)].wr.next = NULL;
2206 	DEBUG("%p: allocated and configured %u WRs (%zu segments)",
2207 	      (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
2208 	rxq->elts_n = elts_n;
2209 	rxq->elts_head = 0;
2210 	rxq->elts.sp = elts;
2211 	assert(ret == 0);
2212 	return 0;
2213 error:
2214 	if (elts != NULL) {
2215 		assert(pool == NULL);
2216 		for (i = 0; (i != elemof(*elts)); ++i) {
2217 			unsigned int j;
2218 			struct rxq_elt_sp *elt = &(*elts)[i];
2219 
2220 			for (j = 0; (j != elemof(elt->bufs)); ++j) {
2221 				struct rte_mbuf *buf = elt->bufs[j];
2222 
2223 				if (buf != NULL)
2224 					rte_pktmbuf_free_seg(buf);
2225 			}
2226 		}
2227 		rte_free(elts);
2228 	}
2229 	DEBUG("%p: failed, freed everything", (void *)rxq);
2230 	assert(ret > 0);
2231 	return ret;
2232 }
2233 
2234 /**
2235  * Free RX queue elements with scattered packets support.
2236  *
2237  * @param rxq
2238  *   Pointer to RX queue structure.
2239  */
2240 static void
2241 rxq_free_elts_sp(struct rxq *rxq)
2242 {
2243 	unsigned int i;
2244 	unsigned int elts_n = rxq->elts_n;
2245 	struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
2246 
2247 	DEBUG("%p: freeing WRs", (void *)rxq);
2248 	rxq->elts_n = 0;
2249 	rxq->elts.sp = NULL;
2250 	if (elts == NULL)
2251 		return;
2252 	for (i = 0; (i != elemof(*elts)); ++i) {
2253 		unsigned int j;
2254 		struct rxq_elt_sp *elt = &(*elts)[i];
2255 
2256 		for (j = 0; (j != elemof(elt->bufs)); ++j) {
2257 			struct rte_mbuf *buf = elt->bufs[j];
2258 
2259 			if (buf != NULL)
2260 				rte_pktmbuf_free_seg(buf);
2261 		}
2262 	}
2263 	rte_free(elts);
2264 }
2265 
2266 /**
2267  * Allocate RX queue elements.
2268  *
2269  * @param rxq
2270  *   Pointer to RX queue structure.
2271  * @param elts_n
2272  *   Number of elements to allocate.
2273  * @param[in] pool
2274  *   If not NULL, fetch buffers from this array instead of allocating them
2275  *   with rte_pktmbuf_alloc().
2276  *
2277  * @return
2278  *   0 on success, errno value on failure.
2279  */
2280 static int
2281 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
2282 {
2283 	unsigned int i;
2284 	struct rxq_elt (*elts)[elts_n] =
2285 		rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2286 				  rxq->socket);
2287 	int ret = 0;
2288 
2289 	if (elts == NULL) {
2290 		ERROR("%p: can't allocate packets array", (void *)rxq);
2291 		ret = ENOMEM;
2292 		goto error;
2293 	}
2294 	/* For each WR (packet). */
2295 	for (i = 0; (i != elts_n); ++i) {
2296 		struct rxq_elt *elt = &(*elts)[i];
2297 		struct ibv_recv_wr *wr = &elt->wr;
2298 		struct ibv_sge *sge = &(*elts)[i].sge;
2299 		struct rte_mbuf *buf;
2300 
2301 		if (pool != NULL) {
2302 			buf = *(pool++);
2303 			assert(buf != NULL);
2304 			rte_pktmbuf_reset(buf);
2305 		} else
2306 			buf = rte_pktmbuf_alloc(rxq->mp);
2307 		if (buf == NULL) {
2308 			assert(pool == NULL);
2309 			ERROR("%p: empty mbuf pool", (void *)rxq);
2310 			ret = ENOMEM;
2311 			goto error;
2312 		}
2313 		/* Configure WR. Work request ID contains its own index in
2314 		 * the elts array and the offset between SGE buffer header and
2315 		 * its data. */
2316 		WR_ID(wr->wr_id).id = i;
2317 		WR_ID(wr->wr_id).offset =
2318 			(((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
2319 			 (uintptr_t)buf);
2320 		wr->next = &(*elts)[(i + 1)].wr;
2321 		wr->sg_list = sge;
2322 		wr->num_sge = 1;
2323 		/* Headroom is reserved by rte_pktmbuf_alloc(). */
2324 		assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2325 		/* Buffer is supposed to be empty. */
2326 		assert(rte_pktmbuf_data_len(buf) == 0);
2327 		assert(rte_pktmbuf_pkt_len(buf) == 0);
2328 		/* sge->addr must be able to store a pointer. */
2329 		assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2330 		/* SGE keeps its headroom. */
2331 		sge->addr = (uintptr_t)
2332 			((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
2333 		sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
2334 		sge->lkey = rxq->mr->lkey;
2335 		/* Redundant check for tailroom. */
2336 		assert(sge->length == rte_pktmbuf_tailroom(buf));
2337 		/* Make sure elts index and SGE mbuf pointer can be deduced
2338 		 * from WR ID. */
2339 		if ((WR_ID(wr->wr_id).id != i) ||
2340 		    ((void *)((uintptr_t)sge->addr -
2341 			WR_ID(wr->wr_id).offset) != buf)) {
2342 			ERROR("%p: cannot store index and offset in WR ID",
2343 			      (void *)rxq);
2344 			sge->addr = 0;
2345 			rte_pktmbuf_free(buf);
2346 			ret = EOVERFLOW;
2347 			goto error;
2348 		}
2349 	}
2350 	/* The last WR pointer must be NULL. */
2351 	(*elts)[(i - 1)].wr.next = NULL;
2352 	DEBUG("%p: allocated and configured %u single-segment WRs",
2353 	      (void *)rxq, elts_n);
2354 	rxq->elts_n = elts_n;
2355 	rxq->elts_head = 0;
2356 	rxq->elts.no_sp = elts;
2357 	assert(ret == 0);
2358 	return 0;
2359 error:
2360 	if (elts != NULL) {
2361 		assert(pool == NULL);
2362 		for (i = 0; (i != elemof(*elts)); ++i) {
2363 			struct rxq_elt *elt = &(*elts)[i];
2364 			struct rte_mbuf *buf;
2365 
2366 			if (elt->sge.addr == 0)
2367 				continue;
2368 			assert(WR_ID(elt->wr.wr_id).id == i);
2369 			buf = (void *)((uintptr_t)elt->sge.addr -
2370 				WR_ID(elt->wr.wr_id).offset);
2371 			rte_pktmbuf_free_seg(buf);
2372 		}
2373 		rte_free(elts);
2374 	}
2375 	DEBUG("%p: failed, freed everything", (void *)rxq);
2376 	assert(ret > 0);
2377 	return ret;
2378 }
2379 
2380 /**
2381  * Free RX queue elements.
2382  *
2383  * @param rxq
2384  *   Pointer to RX queue structure.
2385  */
2386 static void
2387 rxq_free_elts(struct rxq *rxq)
2388 {
2389 	unsigned int i;
2390 	unsigned int elts_n = rxq->elts_n;
2391 	struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2392 
2393 	DEBUG("%p: freeing WRs", (void *)rxq);
2394 	rxq->elts_n = 0;
2395 	rxq->elts.no_sp = NULL;
2396 	if (elts == NULL)
2397 		return;
2398 	for (i = 0; (i != elemof(*elts)); ++i) {
2399 		struct rxq_elt *elt = &(*elts)[i];
2400 		struct rte_mbuf *buf;
2401 
2402 		if (elt->sge.addr == 0)
2403 			continue;
2404 		assert(WR_ID(elt->wr.wr_id).id == i);
2405 		buf = (void *)((uintptr_t)elt->sge.addr -
2406 			WR_ID(elt->wr.wr_id).offset);
2407 		rte_pktmbuf_free_seg(buf);
2408 	}
2409 	rte_free(elts);
2410 }
2411 
2412 /**
2413  * Delete flow steering rule.
2414  *
2415  * @param rxq
2416  *   Pointer to RX queue structure.
2417  * @param mac_index
2418  *   MAC address index.
2419  * @param vlan_index
2420  *   VLAN index.
2421  */
2422 static void
2423 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2424 {
2425 #ifndef NDEBUG
2426 	struct priv *priv = rxq->priv;
2427 	const uint8_t (*mac)[ETHER_ADDR_LEN] =
2428 		(const uint8_t (*)[ETHER_ADDR_LEN])
2429 		priv->mac[mac_index].addr_bytes;
2430 #endif
2431 	assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2432 	DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2433 	      " (VLAN ID %" PRIu16 ")",
2434 	      (void *)rxq,
2435 	      (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2436 	      mac_index, priv->vlan_filter[vlan_index].id);
2437 	claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2438 	rxq->mac_flow[mac_index][vlan_index] = NULL;
2439 }
2440 
2441 /**
2442  * Unregister a MAC address from a RX queue.
2443  *
2444  * @param rxq
2445  *   Pointer to RX queue structure.
2446  * @param mac_index
2447  *   MAC address index.
2448  */
2449 static void
2450 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2451 {
2452 	struct priv *priv = rxq->priv;
2453 	unsigned int i;
2454 	unsigned int vlans = 0;
2455 
2456 	assert(mac_index < elemof(priv->mac));
2457 	if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2458 		return;
2459 	for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2460 		if (!priv->vlan_filter[i].enabled)
2461 			continue;
2462 		rxq_del_flow(rxq, mac_index, i);
2463 		vlans++;
2464 	}
2465 	if (!vlans) {
2466 		rxq_del_flow(rxq, mac_index, 0);
2467 	}
2468 	BITFIELD_RESET(rxq->mac_configured, mac_index);
2469 }
2470 
2471 /**
2472  * Unregister all MAC addresses from a RX queue.
2473  *
2474  * @param rxq
2475  *   Pointer to RX queue structure.
2476  */
2477 static void
2478 rxq_mac_addrs_del(struct rxq *rxq)
2479 {
2480 	struct priv *priv = rxq->priv;
2481 	unsigned int i;
2482 
2483 	for (i = 0; (i != elemof(priv->mac)); ++i)
2484 		rxq_mac_addr_del(rxq, i);
2485 }
2486 
2487 static int rxq_promiscuous_enable(struct rxq *);
2488 static void rxq_promiscuous_disable(struct rxq *);
2489 
2490 /**
2491  * Add single flow steering rule.
2492  *
2493  * @param rxq
2494  *   Pointer to RX queue structure.
2495  * @param mac_index
2496  *   MAC address index to register.
2497  * @param vlan_index
2498  *   VLAN index. Use -1 for a flow without VLAN.
2499  *
2500  * @return
2501  *   0 on success, errno value on failure.
2502  */
2503 static int
2504 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2505 {
2506 	struct ibv_flow *flow;
2507 	struct priv *priv = rxq->priv;
2508 	const uint8_t (*mac)[ETHER_ADDR_LEN] =
2509 			(const uint8_t (*)[ETHER_ADDR_LEN])
2510 			priv->mac[mac_index].addr_bytes;
2511 
2512 	/* Allocate flow specification on the stack. */
2513 	struct __attribute__((packed)) {
2514 		struct ibv_flow_attr attr;
2515 		struct ibv_flow_spec_eth spec;
2516 	} data;
2517 	struct ibv_flow_attr *attr = &data.attr;
2518 	struct ibv_flow_spec_eth *spec = &data.spec;
2519 
2520 	assert(mac_index < elemof(priv->mac));
2521 	assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2522 	/*
2523 	 * No padding must be inserted by the compiler between attr and spec.
2524 	 * This layout is expected by libibverbs.
2525 	 */
2526 	assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2527 	*attr = (struct ibv_flow_attr){
2528 		.type = IBV_FLOW_ATTR_NORMAL,
2529 		.num_of_specs = 1,
2530 		.port = priv->port,
2531 		.flags = 0
2532 	};
2533 	*spec = (struct ibv_flow_spec_eth){
2534 		.type = IBV_FLOW_SPEC_ETH,
2535 		.size = sizeof(*spec),
2536 		.val = {
2537 			.dst_mac = {
2538 				(*mac)[0], (*mac)[1], (*mac)[2],
2539 				(*mac)[3], (*mac)[4], (*mac)[5]
2540 			},
2541 			.vlan_tag = ((vlan_index != -1u) ?
2542 				     htons(priv->vlan_filter[vlan_index].id) :
2543 				     0),
2544 		},
2545 		.mask = {
2546 			.dst_mac = "\xff\xff\xff\xff\xff\xff",
2547 			.vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2548 		}
2549 	};
2550 	DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2551 	      " (VLAN %s %" PRIu16 ")",
2552 	      (void *)rxq,
2553 	      (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2554 	      mac_index,
2555 	      ((vlan_index != -1u) ? "ID" : "index"),
2556 	      ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2557 	/* Create related flow. */
2558 	errno = 0;
2559 	flow = ibv_create_flow(rxq->qp, attr);
2560 	if (flow == NULL) {
2561 		/* It's not clear whether errno is always set in this case. */
2562 		ERROR("%p: flow configuration failed, errno=%d: %s",
2563 		      (void *)rxq, errno,
2564 		      (errno ? strerror(errno) : "Unknown error"));
2565 		if (errno)
2566 			return errno;
2567 		return EINVAL;
2568 	}
2569 	if (vlan_index == -1u)
2570 		vlan_index = 0;
2571 	assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2572 	rxq->mac_flow[mac_index][vlan_index] = flow;
2573 	return 0;
2574 }
2575 
2576 /**
2577  * Register a MAC address in a RX queue.
2578  *
2579  * @param rxq
2580  *   Pointer to RX queue structure.
2581  * @param mac_index
2582  *   MAC address index to register.
2583  *
2584  * @return
2585  *   0 on success, errno value on failure.
2586  */
2587 static int
2588 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2589 {
2590 	struct priv *priv = rxq->priv;
2591 	unsigned int i;
2592 	unsigned int vlans = 0;
2593 	int ret;
2594 
2595 	assert(mac_index < elemof(priv->mac));
2596 	if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2597 		rxq_mac_addr_del(rxq, mac_index);
2598 	/* Fill VLAN specifications. */
2599 	for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2600 		if (!priv->vlan_filter[i].enabled)
2601 			continue;
2602 		/* Create related flow. */
2603 		ret = rxq_add_flow(rxq, mac_index, i);
2604 		if (!ret) {
2605 			vlans++;
2606 			continue;
2607 		}
2608 		/* Failure, rollback. */
2609 		while (i != 0)
2610 			if (priv->vlan_filter[--i].enabled)
2611 				rxq_del_flow(rxq, mac_index, i);
2612 		assert(ret > 0);
2613 		return ret;
2614 	}
2615 	/* In case there is no VLAN filter. */
2616 	if (!vlans) {
2617 		ret = rxq_add_flow(rxq, mac_index, -1);
2618 		if (ret)
2619 			return ret;
2620 	}
2621 	BITFIELD_SET(rxq->mac_configured, mac_index);
2622 	return 0;
2623 }
2624 
2625 /**
2626  * Register all MAC addresses in a RX queue.
2627  *
2628  * @param rxq
2629  *   Pointer to RX queue structure.
2630  *
2631  * @return
2632  *   0 on success, errno value on failure.
2633  */
2634 static int
2635 rxq_mac_addrs_add(struct rxq *rxq)
2636 {
2637 	struct priv *priv = rxq->priv;
2638 	unsigned int i;
2639 	int ret;
2640 
2641 	for (i = 0; (i != elemof(priv->mac)); ++i) {
2642 		if (!BITFIELD_ISSET(priv->mac_configured, i))
2643 			continue;
2644 		ret = rxq_mac_addr_add(rxq, i);
2645 		if (!ret)
2646 			continue;
2647 		/* Failure, rollback. */
2648 		while (i != 0)
2649 			rxq_mac_addr_del(rxq, --i);
2650 		assert(ret > 0);
2651 		return ret;
2652 	}
2653 	return 0;
2654 }
2655 
2656 /**
2657  * Unregister a MAC address.
2658  *
2659  * In RSS mode, the MAC address is unregistered from the parent queue,
2660  * otherwise it is unregistered from each queue directly.
2661  *
2662  * @param priv
2663  *   Pointer to private structure.
2664  * @param mac_index
2665  *   MAC address index.
2666  */
2667 static void
2668 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2669 {
2670 	unsigned int i;
2671 
2672 	assert(mac_index < elemof(priv->mac));
2673 	if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2674 		return;
2675 	if (priv->rss) {
2676 		rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2677 		goto end;
2678 	}
2679 	for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2680 		rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2681 end:
2682 	BITFIELD_RESET(priv->mac_configured, mac_index);
2683 }
2684 
2685 /**
2686  * Register a MAC address.
2687  *
2688  * In RSS mode, the MAC address is registered in the parent queue,
2689  * otherwise it is registered in each queue directly.
2690  *
2691  * @param priv
2692  *   Pointer to private structure.
2693  * @param mac_index
2694  *   MAC address index to use.
2695  * @param mac
2696  *   MAC address to register.
2697  *
2698  * @return
2699  *   0 on success, errno value on failure.
2700  */
2701 static int
2702 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2703 		  const uint8_t (*mac)[ETHER_ADDR_LEN])
2704 {
2705 	unsigned int i;
2706 	int ret;
2707 
2708 	assert(mac_index < elemof(priv->mac));
2709 	/* First, make sure this address isn't already configured. */
2710 	for (i = 0; (i != elemof(priv->mac)); ++i) {
2711 		/* Skip this index, it's going to be reconfigured. */
2712 		if (i == mac_index)
2713 			continue;
2714 		if (!BITFIELD_ISSET(priv->mac_configured, i))
2715 			continue;
2716 		if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2717 			continue;
2718 		/* Address already configured elsewhere, return with error. */
2719 		return EADDRINUSE;
2720 	}
2721 	if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2722 		priv_mac_addr_del(priv, mac_index);
2723 	priv->mac[mac_index] = (struct ether_addr){
2724 		{
2725 			(*mac)[0], (*mac)[1], (*mac)[2],
2726 			(*mac)[3], (*mac)[4], (*mac)[5]
2727 		}
2728 	};
2729 	/* If device isn't started, this is all we need to do. */
2730 	if (!priv->started) {
2731 #ifndef NDEBUG
2732 		/* Verify that all queues have this index disabled. */
2733 		for (i = 0; (i != priv->rxqs_n); ++i) {
2734 			if ((*priv->rxqs)[i] == NULL)
2735 				continue;
2736 			assert(!BITFIELD_ISSET
2737 			       ((*priv->rxqs)[i]->mac_configured, mac_index));
2738 		}
2739 #endif
2740 		goto end;
2741 	}
2742 	if (priv->rss) {
2743 		ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2744 		if (ret)
2745 			return ret;
2746 		goto end;
2747 	}
2748 	for (i = 0; (i != priv->rxqs_n); ++i) {
2749 		if ((*priv->rxqs)[i] == NULL)
2750 			continue;
2751 		ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2752 		if (!ret)
2753 			continue;
2754 		/* Failure, rollback. */
2755 		while (i != 0)
2756 			if ((*priv->rxqs)[(--i)] != NULL)
2757 				rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2758 		return ret;
2759 	}
2760 end:
2761 	BITFIELD_SET(priv->mac_configured, mac_index);
2762 	return 0;
2763 }
2764 
2765 /**
2766  * Enable allmulti mode in a RX queue.
2767  *
2768  * @param rxq
2769  *   Pointer to RX queue structure.
2770  *
2771  * @return
2772  *   0 on success, errno value on failure.
2773  */
2774 static int
2775 rxq_allmulticast_enable(struct rxq *rxq)
2776 {
2777 	struct ibv_flow *flow;
2778 	struct ibv_flow_attr attr = {
2779 		.type = IBV_FLOW_ATTR_MC_DEFAULT,
2780 		.num_of_specs = 0,
2781 		.port = rxq->priv->port,
2782 		.flags = 0
2783 	};
2784 
2785 	DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2786 	if (rxq->allmulti_flow != NULL)
2787 		return EBUSY;
2788 	errno = 0;
2789 	flow = ibv_create_flow(rxq->qp, &attr);
2790 	if (flow == NULL) {
2791 		/* It's not clear whether errno is always set in this case. */
2792 		ERROR("%p: flow configuration failed, errno=%d: %s",
2793 		      (void *)rxq, errno,
2794 		      (errno ? strerror(errno) : "Unknown error"));
2795 		if (errno)
2796 			return errno;
2797 		return EINVAL;
2798 	}
2799 	rxq->allmulti_flow = flow;
2800 	DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2801 	return 0;
2802 }
2803 
2804 /**
2805  * Disable allmulti mode in a RX queue.
2806  *
2807  * @param rxq
2808  *   Pointer to RX queue structure.
2809  */
2810 static void
2811 rxq_allmulticast_disable(struct rxq *rxq)
2812 {
2813 	DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2814 	if (rxq->allmulti_flow == NULL)
2815 		return;
2816 	claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2817 	rxq->allmulti_flow = NULL;
2818 	DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2819 }
2820 
2821 /**
2822  * Enable promiscuous mode in a RX queue.
2823  *
2824  * @param rxq
2825  *   Pointer to RX queue structure.
2826  *
2827  * @return
2828  *   0 on success, errno value on failure.
2829  */
2830 static int
2831 rxq_promiscuous_enable(struct rxq *rxq)
2832 {
2833 	struct ibv_flow *flow;
2834 	struct ibv_flow_attr attr = {
2835 		.type = IBV_FLOW_ATTR_ALL_DEFAULT,
2836 		.num_of_specs = 0,
2837 		.port = rxq->priv->port,
2838 		.flags = 0
2839 	};
2840 
2841 	if (rxq->priv->vf)
2842 		return 0;
2843 	DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2844 	if (rxq->promisc_flow != NULL)
2845 		return EBUSY;
2846 	errno = 0;
2847 	flow = ibv_create_flow(rxq->qp, &attr);
2848 	if (flow == NULL) {
2849 		/* It's not clear whether errno is always set in this case. */
2850 		ERROR("%p: flow configuration failed, errno=%d: %s",
2851 		      (void *)rxq, errno,
2852 		      (errno ? strerror(errno) : "Unknown error"));
2853 		if (errno)
2854 			return errno;
2855 		return EINVAL;
2856 	}
2857 	rxq->promisc_flow = flow;
2858 	DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2859 	return 0;
2860 }
2861 
2862 /**
2863  * Disable promiscuous mode in a RX queue.
2864  *
2865  * @param rxq
2866  *   Pointer to RX queue structure.
2867  */
2868 static void
2869 rxq_promiscuous_disable(struct rxq *rxq)
2870 {
2871 	if (rxq->priv->vf)
2872 		return;
2873 	DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2874 	if (rxq->promisc_flow == NULL)
2875 		return;
2876 	claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2877 	rxq->promisc_flow = NULL;
2878 	DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2879 }
2880 
2881 /**
2882  * Clean up a RX queue.
2883  *
2884  * Destroy objects, free allocated memory and reset the structure for reuse.
2885  *
2886  * @param rxq
2887  *   Pointer to RX queue structure.
2888  */
2889 static void
2890 rxq_cleanup(struct rxq *rxq)
2891 {
2892 	struct ibv_exp_release_intf_params params;
2893 
2894 	DEBUG("cleaning up %p", (void *)rxq);
2895 	if (rxq->sp)
2896 		rxq_free_elts_sp(rxq);
2897 	else
2898 		rxq_free_elts(rxq);
2899 	if (rxq->if_qp != NULL) {
2900 		assert(rxq->priv != NULL);
2901 		assert(rxq->priv->ctx != NULL);
2902 		assert(rxq->qp != NULL);
2903 		params = (struct ibv_exp_release_intf_params){
2904 			.comp_mask = 0,
2905 		};
2906 		claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2907 						rxq->if_qp,
2908 						&params));
2909 	}
2910 	if (rxq->if_cq != NULL) {
2911 		assert(rxq->priv != NULL);
2912 		assert(rxq->priv->ctx != NULL);
2913 		assert(rxq->cq != NULL);
2914 		params = (struct ibv_exp_release_intf_params){
2915 			.comp_mask = 0,
2916 		};
2917 		claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2918 						rxq->if_cq,
2919 						&params));
2920 	}
2921 	if (rxq->qp != NULL) {
2922 		rxq_promiscuous_disable(rxq);
2923 		rxq_allmulticast_disable(rxq);
2924 		rxq_mac_addrs_del(rxq);
2925 		claim_zero(ibv_destroy_qp(rxq->qp));
2926 	}
2927 	if (rxq->cq != NULL)
2928 		claim_zero(ibv_destroy_cq(rxq->cq));
2929 	if (rxq->rd != NULL) {
2930 		struct ibv_exp_destroy_res_domain_attr attr = {
2931 			.comp_mask = 0,
2932 		};
2933 
2934 		assert(rxq->priv != NULL);
2935 		assert(rxq->priv->ctx != NULL);
2936 		claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2937 						      rxq->rd,
2938 						      &attr));
2939 	}
2940 	if (rxq->mr != NULL)
2941 		claim_zero(ibv_dereg_mr(rxq->mr));
2942 	memset(rxq, 0, sizeof(*rxq));
2943 }
2944 
2945 /**
2946  * Translate RX completion flags to packet type.
2947  *
2948  * @param flags
2949  *   RX completion flags returned by poll_length_flags().
2950  *
2951  * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2952  *
2953  * @return
2954  *   Packet type for struct rte_mbuf.
2955  */
2956 static inline uint32_t
2957 rxq_cq_to_pkt_type(uint32_t flags)
2958 {
2959 	uint32_t pkt_type;
2960 
2961 	if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2962 		pkt_type =
2963 			TRANSPOSE(flags,
2964 				  IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2965 				  RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2966 			TRANSPOSE(flags,
2967 				  IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2968 				  RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2969 			TRANSPOSE(flags,
2970 				  IBV_EXP_CQ_RX_IPV4_PACKET,
2971 				  RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2972 			TRANSPOSE(flags,
2973 				  IBV_EXP_CQ_RX_IPV6_PACKET,
2974 				  RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2975 	else
2976 		pkt_type =
2977 			TRANSPOSE(flags,
2978 				  IBV_EXP_CQ_RX_IPV4_PACKET,
2979 				  RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2980 			TRANSPOSE(flags,
2981 				  IBV_EXP_CQ_RX_IPV6_PACKET,
2982 				  RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2983 	return pkt_type;
2984 }
2985 
2986 /**
2987  * Translate RX completion flags to offload flags.
2988  *
2989  * @param[in] rxq
2990  *   Pointer to RX queue structure.
2991  * @param flags
2992  *   RX completion flags returned by poll_length_flags().
2993  *
2994  * @return
2995  *   Offload flags (ol_flags) for struct rte_mbuf.
2996  */
2997 static inline uint32_t
2998 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2999 {
3000 	uint32_t ol_flags = 0;
3001 
3002 	if (rxq->csum)
3003 		ol_flags |=
3004 			TRANSPOSE(flags,
3005 				  IBV_EXP_CQ_RX_IP_CSUM_OK,
3006 				  PKT_RX_IP_CKSUM_GOOD) |
3007 			TRANSPOSE(flags,
3008 				  IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
3009 				  PKT_RX_L4_CKSUM_GOOD);
3010 	if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
3011 		ol_flags |=
3012 			TRANSPOSE(flags,
3013 				  IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
3014 				  PKT_RX_IP_CKSUM_GOOD) |
3015 			TRANSPOSE(flags,
3016 				  IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
3017 				  PKT_RX_L4_CKSUM_GOOD);
3018 	return ol_flags;
3019 }
3020 
3021 static uint16_t
3022 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
3023 
3024 /**
3025  * DPDK callback for RX with scattered packets support.
3026  *
3027  * @param dpdk_rxq
3028  *   Generic pointer to RX queue structure.
3029  * @param[out] pkts
3030  *   Array to store received packets.
3031  * @param pkts_n
3032  *   Maximum number of packets in array.
3033  *
3034  * @return
3035  *   Number of packets successfully received (<= pkts_n).
3036  */
3037 static uint16_t
3038 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3039 {
3040 	struct rxq *rxq = (struct rxq *)dpdk_rxq;
3041 	struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3042 	const unsigned int elts_n = rxq->elts_n;
3043 	unsigned int elts_head = rxq->elts_head;
3044 	struct ibv_recv_wr head;
3045 	struct ibv_recv_wr **next = &head.next;
3046 	struct ibv_recv_wr *bad_wr;
3047 	unsigned int i;
3048 	unsigned int pkts_ret = 0;
3049 	int ret;
3050 
3051 	if (unlikely(!rxq->sp))
3052 		return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
3053 	if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
3054 		return 0;
3055 	for (i = 0; (i != pkts_n); ++i) {
3056 		struct rxq_elt_sp *elt = &(*elts)[elts_head];
3057 		struct ibv_recv_wr *wr = &elt->wr;
3058 		uint64_t wr_id = wr->wr_id;
3059 		unsigned int len;
3060 		unsigned int pkt_buf_len;
3061 		struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
3062 		struct rte_mbuf **pkt_buf_next = &pkt_buf;
3063 		unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
3064 		unsigned int j = 0;
3065 		uint32_t flags;
3066 
3067 		/* Sanity checks. */
3068 #ifdef NDEBUG
3069 		(void)wr_id;
3070 #endif
3071 		assert(wr_id < rxq->elts_n);
3072 		assert(wr->sg_list == elt->sges);
3073 		assert(wr->num_sge == elemof(elt->sges));
3074 		assert(elts_head < rxq->elts_n);
3075 		assert(rxq->elts_head < rxq->elts_n);
3076 		ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3077 						    &flags);
3078 		if (unlikely(ret < 0)) {
3079 			struct ibv_wc wc;
3080 			int wcs_n;
3081 
3082 			DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3083 			      (void *)rxq, ret);
3084 			/* ibv_poll_cq() must be used in case of failure. */
3085 			wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3086 			if (unlikely(wcs_n == 0))
3087 				break;
3088 			if (unlikely(wcs_n < 0)) {
3089 				DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3090 				      (void *)rxq, wcs_n);
3091 				break;
3092 			}
3093 			assert(wcs_n == 1);
3094 			if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3095 				/* Whatever, just repost the offending WR. */
3096 				DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3097 				      " completion status (%d): %s",
3098 				      (void *)rxq, wc.wr_id, wc.status,
3099 				      ibv_wc_status_str(wc.status));
3100 #ifdef MLX4_PMD_SOFT_COUNTERS
3101 				/* Increment dropped packets counter. */
3102 				++rxq->stats.idropped;
3103 #endif
3104 				/* Link completed WRs together for repost. */
3105 				*next = wr;
3106 				next = &wr->next;
3107 				goto repost;
3108 			}
3109 			ret = wc.byte_len;
3110 		}
3111 		if (ret == 0)
3112 			break;
3113 		len = ret;
3114 		pkt_buf_len = len;
3115 		/* Link completed WRs together for repost. */
3116 		*next = wr;
3117 		next = &wr->next;
3118 		/*
3119 		 * Replace spent segments with new ones, concatenate and
3120 		 * return them as pkt_buf.
3121 		 */
3122 		while (1) {
3123 			struct ibv_sge *sge = &elt->sges[j];
3124 			struct rte_mbuf *seg = elt->bufs[j];
3125 			struct rte_mbuf *rep;
3126 			unsigned int seg_tailroom;
3127 
3128 			/*
3129 			 * Fetch initial bytes of packet descriptor into a
3130 			 * cacheline while allocating rep.
3131 			 */
3132 			rte_prefetch0(seg);
3133 			rep = rte_mbuf_raw_alloc(rxq->mp);
3134 			if (unlikely(rep == NULL)) {
3135 				/*
3136 				 * Unable to allocate a replacement mbuf,
3137 				 * repost WR.
3138 				 */
3139 				DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
3140 				      " can't allocate a new mbuf",
3141 				      (void *)rxq, wr_id);
3142 				if (pkt_buf != NULL) {
3143 					*pkt_buf_next = NULL;
3144 					rte_pktmbuf_free(pkt_buf);
3145 				}
3146 				/* Increase out of memory counters. */
3147 				++rxq->stats.rx_nombuf;
3148 				++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3149 				goto repost;
3150 			}
3151 #ifndef NDEBUG
3152 			/* Poison user-modifiable fields in rep. */
3153 			NEXT(rep) = (void *)((uintptr_t)-1);
3154 			SET_DATA_OFF(rep, 0xdead);
3155 			DATA_LEN(rep) = 0xd00d;
3156 			PKT_LEN(rep) = 0xdeadd00d;
3157 			NB_SEGS(rep) = 0x2a;
3158 			PORT(rep) = 0x2a;
3159 			rep->ol_flags = -1;
3160 #endif
3161 			assert(rep->buf_len == seg->buf_len);
3162 			/* Reconfigure sge to use rep instead of seg. */
3163 			assert(sge->lkey == rxq->mr->lkey);
3164 			sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
3165 			elt->bufs[j] = rep;
3166 			++j;
3167 			/* Update pkt_buf if it's the first segment, or link
3168 			 * seg to the previous one and update pkt_buf_next. */
3169 			*pkt_buf_next = seg;
3170 			pkt_buf_next = &NEXT(seg);
3171 			/* Update seg information. */
3172 			seg_tailroom = (seg->buf_len - seg_headroom);
3173 			assert(sge->length == seg_tailroom);
3174 			SET_DATA_OFF(seg, seg_headroom);
3175 			if (likely(len <= seg_tailroom)) {
3176 				/* Last segment. */
3177 				DATA_LEN(seg) = len;
3178 				PKT_LEN(seg) = len;
3179 				/* Sanity check. */
3180 				assert(rte_pktmbuf_headroom(seg) ==
3181 				       seg_headroom);
3182 				assert(rte_pktmbuf_tailroom(seg) ==
3183 				       (seg_tailroom - len));
3184 				break;
3185 			}
3186 			DATA_LEN(seg) = seg_tailroom;
3187 			PKT_LEN(seg) = seg_tailroom;
3188 			/* Sanity check. */
3189 			assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3190 			assert(rte_pktmbuf_tailroom(seg) == 0);
3191 			/* Fix len and clear headroom for next segments. */
3192 			len -= seg_tailroom;
3193 			seg_headroom = 0;
3194 		}
3195 		/* Update head and tail segments. */
3196 		*pkt_buf_next = NULL;
3197 		assert(pkt_buf != NULL);
3198 		assert(j != 0);
3199 		NB_SEGS(pkt_buf) = j;
3200 		PORT(pkt_buf) = rxq->port_id;
3201 		PKT_LEN(pkt_buf) = pkt_buf_len;
3202 		pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3203 		pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3204 
3205 		/* Return packet. */
3206 		*(pkts++) = pkt_buf;
3207 		++pkts_ret;
3208 #ifdef MLX4_PMD_SOFT_COUNTERS
3209 		/* Increase bytes counter. */
3210 		rxq->stats.ibytes += pkt_buf_len;
3211 #endif
3212 repost:
3213 		if (++elts_head >= elts_n)
3214 			elts_head = 0;
3215 		continue;
3216 	}
3217 	if (unlikely(i == 0))
3218 		return 0;
3219 	*next = NULL;
3220 	/* Repost WRs. */
3221 #ifdef DEBUG_RECV
3222 	DEBUG("%p: reposting %d WRs", (void *)rxq, i);
3223 #endif
3224 	ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3225 	if (unlikely(ret)) {
3226 		/* Inability to repost WRs is fatal. */
3227 		DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3228 		      (void *)rxq->priv,
3229 		      (void *)bad_wr,
3230 		      strerror(ret));
3231 		abort();
3232 	}
3233 	rxq->elts_head = elts_head;
3234 #ifdef MLX4_PMD_SOFT_COUNTERS
3235 	/* Increase packets counter. */
3236 	rxq->stats.ipackets += pkts_ret;
3237 #endif
3238 	return pkts_ret;
3239 }
3240 
3241 /**
3242  * DPDK callback for RX.
3243  *
3244  * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3245  * manage scattered packets. Improves performance when MRU is lower than the
3246  * size of the first segment.
3247  *
3248  * @param dpdk_rxq
3249  *   Generic pointer to RX queue structure.
3250  * @param[out] pkts
3251  *   Array to store received packets.
3252  * @param pkts_n
3253  *   Maximum number of packets in array.
3254  *
3255  * @return
3256  *   Number of packets successfully received (<= pkts_n).
3257  */
3258 static uint16_t
3259 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3260 {
3261 	struct rxq *rxq = (struct rxq *)dpdk_rxq;
3262 	struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3263 	const unsigned int elts_n = rxq->elts_n;
3264 	unsigned int elts_head = rxq->elts_head;
3265 	struct ibv_sge sges[pkts_n];
3266 	unsigned int i;
3267 	unsigned int pkts_ret = 0;
3268 	int ret;
3269 
3270 	if (unlikely(rxq->sp))
3271 		return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3272 	for (i = 0; (i != pkts_n); ++i) {
3273 		struct rxq_elt *elt = &(*elts)[elts_head];
3274 		struct ibv_recv_wr *wr = &elt->wr;
3275 		uint64_t wr_id = wr->wr_id;
3276 		unsigned int len;
3277 		struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3278 			WR_ID(wr_id).offset);
3279 		struct rte_mbuf *rep;
3280 		uint32_t flags;
3281 
3282 		/* Sanity checks. */
3283 		assert(WR_ID(wr_id).id < rxq->elts_n);
3284 		assert(wr->sg_list == &elt->sge);
3285 		assert(wr->num_sge == 1);
3286 		assert(elts_head < rxq->elts_n);
3287 		assert(rxq->elts_head < rxq->elts_n);
3288 		/*
3289 		 * Fetch initial bytes of packet descriptor into a
3290 		 * cacheline while allocating rep.
3291 		 */
3292 		rte_mbuf_prefetch_part1(seg);
3293 		rte_mbuf_prefetch_part2(seg);
3294 		ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3295 						    &flags);
3296 		if (unlikely(ret < 0)) {
3297 			struct ibv_wc wc;
3298 			int wcs_n;
3299 
3300 			DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3301 			      (void *)rxq, ret);
3302 			/* ibv_poll_cq() must be used in case of failure. */
3303 			wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3304 			if (unlikely(wcs_n == 0))
3305 				break;
3306 			if (unlikely(wcs_n < 0)) {
3307 				DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3308 				      (void *)rxq, wcs_n);
3309 				break;
3310 			}
3311 			assert(wcs_n == 1);
3312 			if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3313 				/* Whatever, just repost the offending WR. */
3314 				DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3315 				      " completion status (%d): %s",
3316 				      (void *)rxq, wc.wr_id, wc.status,
3317 				      ibv_wc_status_str(wc.status));
3318 #ifdef MLX4_PMD_SOFT_COUNTERS
3319 				/* Increment dropped packets counter. */
3320 				++rxq->stats.idropped;
3321 #endif
3322 				/* Add SGE to array for repost. */
3323 				sges[i] = elt->sge;
3324 				goto repost;
3325 			}
3326 			ret = wc.byte_len;
3327 		}
3328 		if (ret == 0)
3329 			break;
3330 		len = ret;
3331 		rep = rte_mbuf_raw_alloc(rxq->mp);
3332 		if (unlikely(rep == NULL)) {
3333 			/*
3334 			 * Unable to allocate a replacement mbuf,
3335 			 * repost WR.
3336 			 */
3337 			DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3338 			      " can't allocate a new mbuf",
3339 			      (void *)rxq, WR_ID(wr_id).id);
3340 			/* Increase out of memory counters. */
3341 			++rxq->stats.rx_nombuf;
3342 			++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3343 			goto repost;
3344 		}
3345 
3346 		/* Reconfigure sge to use rep instead of seg. */
3347 		elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3348 		assert(elt->sge.lkey == rxq->mr->lkey);
3349 		WR_ID(wr->wr_id).offset =
3350 			(((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3351 			 (uintptr_t)rep);
3352 		assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3353 
3354 		/* Add SGE to array for repost. */
3355 		sges[i] = elt->sge;
3356 
3357 		/* Update seg information. */
3358 		SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3359 		NB_SEGS(seg) = 1;
3360 		PORT(seg) = rxq->port_id;
3361 		NEXT(seg) = NULL;
3362 		PKT_LEN(seg) = len;
3363 		DATA_LEN(seg) = len;
3364 		seg->packet_type = rxq_cq_to_pkt_type(flags);
3365 		seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3366 
3367 		/* Return packet. */
3368 		*(pkts++) = seg;
3369 		++pkts_ret;
3370 #ifdef MLX4_PMD_SOFT_COUNTERS
3371 		/* Increase bytes counter. */
3372 		rxq->stats.ibytes += len;
3373 #endif
3374 repost:
3375 		if (++elts_head >= elts_n)
3376 			elts_head = 0;
3377 		continue;
3378 	}
3379 	if (unlikely(i == 0))
3380 		return 0;
3381 	/* Repost WRs. */
3382 #ifdef DEBUG_RECV
3383 	DEBUG("%p: reposting %u WRs", (void *)rxq, i);
3384 #endif
3385 	ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3386 	if (unlikely(ret)) {
3387 		/* Inability to repost WRs is fatal. */
3388 		DEBUG("%p: recv_burst(): failed (ret=%d)",
3389 		      (void *)rxq->priv,
3390 		      ret);
3391 		abort();
3392 	}
3393 	rxq->elts_head = elts_head;
3394 #ifdef MLX4_PMD_SOFT_COUNTERS
3395 	/* Increase packets counter. */
3396 	rxq->stats.ipackets += pkts_ret;
3397 #endif
3398 	return pkts_ret;
3399 }
3400 
3401 /**
3402  * DPDK callback for RX in secondary processes.
3403  *
3404  * This function configures all queues from primary process information
3405  * if necessary before reverting to the normal RX burst callback.
3406  *
3407  * @param dpdk_rxq
3408  *   Generic pointer to RX queue structure.
3409  * @param[out] pkts
3410  *   Array to store received packets.
3411  * @param pkts_n
3412  *   Maximum number of packets in array.
3413  *
3414  * @return
3415  *   Number of packets successfully received (<= pkts_n).
3416  */
3417 static uint16_t
3418 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3419 			      uint16_t pkts_n)
3420 {
3421 	struct rxq *rxq = dpdk_rxq;
3422 	struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3423 	struct priv *primary_priv;
3424 	unsigned int index;
3425 
3426 	if (priv == NULL)
3427 		return 0;
3428 	primary_priv =
3429 		mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3430 	/* Look for queue index in both private structures. */
3431 	for (index = 0; index != priv->rxqs_n; ++index)
3432 		if (((*primary_priv->rxqs)[index] == rxq) ||
3433 		    ((*priv->rxqs)[index] == rxq))
3434 			break;
3435 	if (index == priv->rxqs_n)
3436 		return 0;
3437 	rxq = (*priv->rxqs)[index];
3438 	return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3439 }
3440 
3441 /**
3442  * Allocate a Queue Pair.
3443  * Optionally setup inline receive if supported.
3444  *
3445  * @param priv
3446  *   Pointer to private structure.
3447  * @param cq
3448  *   Completion queue to associate with QP.
3449  * @param desc
3450  *   Number of descriptors in QP (hint only).
3451  *
3452  * @return
3453  *   QP pointer or NULL in case of error.
3454  */
3455 static struct ibv_qp *
3456 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3457 	     struct ibv_exp_res_domain *rd)
3458 {
3459 	struct ibv_exp_qp_init_attr attr = {
3460 		/* CQ to be associated with the send queue. */
3461 		.send_cq = cq,
3462 		/* CQ to be associated with the receive queue. */
3463 		.recv_cq = cq,
3464 		.cap = {
3465 			/* Max number of outstanding WRs. */
3466 			.max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3467 					priv->device_attr.max_qp_wr :
3468 					desc),
3469 			/* Max number of scatter/gather elements in a WR. */
3470 			.max_recv_sge = ((priv->device_attr.max_sge <
3471 					  MLX4_PMD_SGE_WR_N) ?
3472 					 priv->device_attr.max_sge :
3473 					 MLX4_PMD_SGE_WR_N),
3474 		},
3475 		.qp_type = IBV_QPT_RAW_PACKET,
3476 		.comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3477 			      IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3478 		.pd = priv->pd,
3479 		.res_domain = rd,
3480 	};
3481 
3482 #ifdef INLINE_RECV
3483 	attr.max_inl_recv = priv->inl_recv_size;
3484 	attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3485 #endif
3486 	return ibv_exp_create_qp(priv->ctx, &attr);
3487 }
3488 
3489 #ifdef RSS_SUPPORT
3490 
3491 /**
3492  * Allocate a RSS Queue Pair.
3493  * Optionally setup inline receive if supported.
3494  *
3495  * @param priv
3496  *   Pointer to private structure.
3497  * @param cq
3498  *   Completion queue to associate with QP.
3499  * @param desc
3500  *   Number of descriptors in QP (hint only).
3501  * @param parent
3502  *   If nonzero, create a parent QP, otherwise a child.
3503  *
3504  * @return
3505  *   QP pointer or NULL in case of error.
3506  */
3507 static struct ibv_qp *
3508 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3509 		 int parent, struct ibv_exp_res_domain *rd)
3510 {
3511 	struct ibv_exp_qp_init_attr attr = {
3512 		/* CQ to be associated with the send queue. */
3513 		.send_cq = cq,
3514 		/* CQ to be associated with the receive queue. */
3515 		.recv_cq = cq,
3516 		.cap = {
3517 			/* Max number of outstanding WRs. */
3518 			.max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3519 					priv->device_attr.max_qp_wr :
3520 					desc),
3521 			/* Max number of scatter/gather elements in a WR. */
3522 			.max_recv_sge = ((priv->device_attr.max_sge <
3523 					  MLX4_PMD_SGE_WR_N) ?
3524 					 priv->device_attr.max_sge :
3525 					 MLX4_PMD_SGE_WR_N),
3526 		},
3527 		.qp_type = IBV_QPT_RAW_PACKET,
3528 		.comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3529 			      IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3530 			      IBV_EXP_QP_INIT_ATTR_QPG),
3531 		.pd = priv->pd,
3532 		.res_domain = rd,
3533 	};
3534 
3535 #ifdef INLINE_RECV
3536 	attr.max_inl_recv = priv->inl_recv_size,
3537 	attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3538 #endif
3539 	if (parent) {
3540 		attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3541 		/* TSS isn't necessary. */
3542 		attr.qpg.parent_attrib.tss_child_count = 0;
3543 		attr.qpg.parent_attrib.rss_child_count =
3544 			rte_align32pow2(priv->rxqs_n + 1) >> 1;
3545 		DEBUG("initializing parent RSS queue");
3546 	} else {
3547 		attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3548 		attr.qpg.qpg_parent = priv->rxq_parent.qp;
3549 		DEBUG("initializing child RSS queue");
3550 	}
3551 	return ibv_exp_create_qp(priv->ctx, &attr);
3552 }
3553 
3554 #endif /* RSS_SUPPORT */
3555 
3556 /**
3557  * Reconfigure a RX queue with new parameters.
3558  *
3559  * rxq_rehash() does not allocate mbufs, which, if not done from the right
3560  * thread (such as a control thread), may corrupt the pool.
3561  * In case of failure, the queue is left untouched.
3562  *
3563  * @param dev
3564  *   Pointer to Ethernet device structure.
3565  * @param rxq
3566  *   RX queue pointer.
3567  *
3568  * @return
3569  *   0 on success, errno value on failure.
3570  */
3571 static int
3572 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3573 {
3574 	struct priv *priv = rxq->priv;
3575 	struct rxq tmpl = *rxq;
3576 	unsigned int mbuf_n;
3577 	unsigned int desc_n;
3578 	struct rte_mbuf **pool;
3579 	unsigned int i, k;
3580 	struct ibv_exp_qp_attr mod;
3581 	struct ibv_recv_wr *bad_wr;
3582 	unsigned int mb_len;
3583 	int err;
3584 	int parent = (rxq == &priv->rxq_parent);
3585 
3586 	if (parent) {
3587 		ERROR("%p: cannot rehash parent queue %p",
3588 		      (void *)dev, (void *)rxq);
3589 		return EINVAL;
3590 	}
3591 	mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3592 	DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3593 	/* Number of descriptors and mbufs currently allocated. */
3594 	desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3595 	mbuf_n = desc_n;
3596 	/* Toggle RX checksum offload if hardware supports it. */
3597 	if (priv->hw_csum) {
3598 		tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3599 		rxq->csum = tmpl.csum;
3600 	}
3601 	if (priv->hw_csum_l2tun) {
3602 		tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3603 		rxq->csum_l2tun = tmpl.csum_l2tun;
3604 	}
3605 	/* Enable scattered packets support for this queue if necessary. */
3606 	assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3607 	if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3608 	    (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3609 	     (mb_len - RTE_PKTMBUF_HEADROOM))) {
3610 		tmpl.sp = 1;
3611 		desc_n /= MLX4_PMD_SGE_WR_N;
3612 	} else
3613 		tmpl.sp = 0;
3614 	DEBUG("%p: %s scattered packets support (%u WRs)",
3615 	      (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3616 	/* If scatter mode is the same as before, nothing to do. */
3617 	if (tmpl.sp == rxq->sp) {
3618 		DEBUG("%p: nothing to do", (void *)dev);
3619 		return 0;
3620 	}
3621 	/* Remove attached flows if RSS is disabled (no parent queue). */
3622 	if (!priv->rss) {
3623 		rxq_allmulticast_disable(&tmpl);
3624 		rxq_promiscuous_disable(&tmpl);
3625 		rxq_mac_addrs_del(&tmpl);
3626 		/* Update original queue in case of failure. */
3627 		rxq->allmulti_flow = tmpl.allmulti_flow;
3628 		rxq->promisc_flow = tmpl.promisc_flow;
3629 		memcpy(rxq->mac_configured, tmpl.mac_configured,
3630 		       sizeof(rxq->mac_configured));
3631 		memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3632 	}
3633 	/* From now on, any failure will render the queue unusable.
3634 	 * Reinitialize QP. */
3635 	mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3636 	err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3637 	if (err) {
3638 		ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3639 		assert(err > 0);
3640 		return err;
3641 	}
3642 	err = ibv_resize_cq(tmpl.cq, desc_n);
3643 	if (err) {
3644 		ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3645 		assert(err > 0);
3646 		return err;
3647 	}
3648 	mod = (struct ibv_exp_qp_attr){
3649 		/* Move the QP to this state. */
3650 		.qp_state = IBV_QPS_INIT,
3651 		/* Primary port number. */
3652 		.port_num = priv->port
3653 	};
3654 	err = ibv_exp_modify_qp(tmpl.qp, &mod,
3655 				(IBV_EXP_QP_STATE |
3656 #ifdef RSS_SUPPORT
3657 				 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3658 #endif /* RSS_SUPPORT */
3659 				 IBV_EXP_QP_PORT));
3660 	if (err) {
3661 		ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3662 		      (void *)dev, strerror(err));
3663 		assert(err > 0);
3664 		return err;
3665 	};
3666 	/* Reconfigure flows. Do not care for errors. */
3667 	if (!priv->rss) {
3668 		rxq_mac_addrs_add(&tmpl);
3669 		if (priv->promisc)
3670 			rxq_promiscuous_enable(&tmpl);
3671 		if (priv->allmulti)
3672 			rxq_allmulticast_enable(&tmpl);
3673 		/* Update original queue in case of failure. */
3674 		rxq->allmulti_flow = tmpl.allmulti_flow;
3675 		rxq->promisc_flow = tmpl.promisc_flow;
3676 		memcpy(rxq->mac_configured, tmpl.mac_configured,
3677 		       sizeof(rxq->mac_configured));
3678 		memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3679 	}
3680 	/* Allocate pool. */
3681 	pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3682 	if (pool == NULL) {
3683 		ERROR("%p: cannot allocate memory", (void *)dev);
3684 		return ENOBUFS;
3685 	}
3686 	/* Snatch mbufs from original queue. */
3687 	k = 0;
3688 	if (rxq->sp) {
3689 		struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3690 
3691 		for (i = 0; (i != elemof(*elts)); ++i) {
3692 			struct rxq_elt_sp *elt = &(*elts)[i];
3693 			unsigned int j;
3694 
3695 			for (j = 0; (j != elemof(elt->bufs)); ++j) {
3696 				assert(elt->bufs[j] != NULL);
3697 				pool[k++] = elt->bufs[j];
3698 			}
3699 		}
3700 	} else {
3701 		struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3702 
3703 		for (i = 0; (i != elemof(*elts)); ++i) {
3704 			struct rxq_elt *elt = &(*elts)[i];
3705 			struct rte_mbuf *buf = (void *)
3706 				((uintptr_t)elt->sge.addr -
3707 				 WR_ID(elt->wr.wr_id).offset);
3708 
3709 			assert(WR_ID(elt->wr.wr_id).id == i);
3710 			pool[k++] = buf;
3711 		}
3712 	}
3713 	assert(k == mbuf_n);
3714 	tmpl.elts_n = 0;
3715 	tmpl.elts.sp = NULL;
3716 	assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3717 	err = ((tmpl.sp) ?
3718 	       rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3719 	       rxq_alloc_elts(&tmpl, desc_n, pool));
3720 	if (err) {
3721 		ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3722 		rte_free(pool);
3723 		assert(err > 0);
3724 		return err;
3725 	}
3726 	assert(tmpl.elts_n == desc_n);
3727 	assert(tmpl.elts.sp != NULL);
3728 	rte_free(pool);
3729 	/* Clean up original data. */
3730 	rxq->elts_n = 0;
3731 	rte_free(rxq->elts.sp);
3732 	rxq->elts.sp = NULL;
3733 	/* Post WRs. */
3734 	err = ibv_post_recv(tmpl.qp,
3735 			    (tmpl.sp ?
3736 			     &(*tmpl.elts.sp)[0].wr :
3737 			     &(*tmpl.elts.no_sp)[0].wr),
3738 			    &bad_wr);
3739 	if (err) {
3740 		ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3741 		      (void *)dev,
3742 		      (void *)bad_wr,
3743 		      strerror(err));
3744 		goto skip_rtr;
3745 	}
3746 	mod = (struct ibv_exp_qp_attr){
3747 		.qp_state = IBV_QPS_RTR
3748 	};
3749 	err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3750 	if (err)
3751 		ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3752 		      (void *)dev, strerror(err));
3753 skip_rtr:
3754 	*rxq = tmpl;
3755 	assert(err >= 0);
3756 	return err;
3757 }
3758 
3759 /**
3760  * Configure a RX queue.
3761  *
3762  * @param dev
3763  *   Pointer to Ethernet device structure.
3764  * @param rxq
3765  *   Pointer to RX queue structure.
3766  * @param desc
3767  *   Number of descriptors to configure in queue.
3768  * @param socket
3769  *   NUMA socket on which memory must be allocated.
3770  * @param inactive
3771  *   If true, the queue is disabled because its index is higher or
3772  *   equal to the real number of queues, which must be a power of 2.
3773  * @param[in] conf
3774  *   Thresholds parameters.
3775  * @param mp
3776  *   Memory pool for buffer allocations.
3777  *
3778  * @return
3779  *   0 on success, errno value on failure.
3780  */
3781 static int
3782 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3783 	  unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
3784 	  struct rte_mempool *mp)
3785 {
3786 	struct priv *priv = dev->data->dev_private;
3787 	struct rxq tmpl = {
3788 		.priv = priv,
3789 		.mp = mp,
3790 		.socket = socket
3791 	};
3792 	struct ibv_exp_qp_attr mod;
3793 	union {
3794 		struct ibv_exp_query_intf_params params;
3795 		struct ibv_exp_cq_init_attr cq;
3796 		struct ibv_exp_res_domain_init_attr rd;
3797 	} attr;
3798 	enum ibv_exp_query_intf_status status;
3799 	struct ibv_recv_wr *bad_wr;
3800 	unsigned int mb_len;
3801 	int ret = 0;
3802 	int parent = (rxq == &priv->rxq_parent);
3803 
3804 	(void)conf; /* Thresholds configuration (ignored). */
3805 	/*
3806 	 * If this is a parent queue, hardware must support RSS and
3807 	 * RSS must be enabled.
3808 	 */
3809 	assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3810 	if (parent) {
3811 		/* Even if unused, ibv_create_cq() requires at least one
3812 		 * descriptor. */
3813 		desc = 1;
3814 		goto skip_mr;
3815 	}
3816 	mb_len = rte_pktmbuf_data_room_size(mp);
3817 	if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3818 		ERROR("%p: invalid number of RX descriptors (must be a"
3819 		      " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3820 		return EINVAL;
3821 	}
3822 	/* Toggle RX checksum offload if hardware supports it. */
3823 	if (priv->hw_csum)
3824 		tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3825 	if (priv->hw_csum_l2tun)
3826 		tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3827 	/* Enable scattered packets support for this queue if necessary. */
3828 	assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3829 	if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3830 	    (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3831 	     (mb_len - RTE_PKTMBUF_HEADROOM))) {
3832 		tmpl.sp = 1;
3833 		desc /= MLX4_PMD_SGE_WR_N;
3834 	}
3835 	DEBUG("%p: %s scattered packets support (%u WRs)",
3836 	      (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3837 	/* Use the entire RX mempool as the memory region. */
3838 	tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3839 	if (tmpl.mr == NULL) {
3840 		ret = EINVAL;
3841 		ERROR("%p: MR creation failure: %s",
3842 		      (void *)dev, strerror(ret));
3843 		goto error;
3844 	}
3845 skip_mr:
3846 	attr.rd = (struct ibv_exp_res_domain_init_attr){
3847 		.comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3848 			      IBV_EXP_RES_DOMAIN_MSG_MODEL),
3849 		.thread_model = IBV_EXP_THREAD_SINGLE,
3850 		.msg_model = IBV_EXP_MSG_HIGH_BW,
3851 	};
3852 	tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3853 	if (tmpl.rd == NULL) {
3854 		ret = ENOMEM;
3855 		ERROR("%p: RD creation failure: %s",
3856 		      (void *)dev, strerror(ret));
3857 		goto error;
3858 	}
3859 	attr.cq = (struct ibv_exp_cq_init_attr){
3860 		.comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3861 		.res_domain = tmpl.rd,
3862 	};
3863 	tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
3864 	if (tmpl.cq == NULL) {
3865 		ret = ENOMEM;
3866 		ERROR("%p: CQ creation failure: %s",
3867 		      (void *)dev, strerror(ret));
3868 		goto error;
3869 	}
3870 	DEBUG("priv->device_attr.max_qp_wr is %d",
3871 	      priv->device_attr.max_qp_wr);
3872 	DEBUG("priv->device_attr.max_sge is %d",
3873 	      priv->device_attr.max_sge);
3874 #ifdef RSS_SUPPORT
3875 	if (priv->rss && !inactive)
3876 		tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3877 					   tmpl.rd);
3878 	else
3879 #endif /* RSS_SUPPORT */
3880 		tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3881 	if (tmpl.qp == NULL) {
3882 		ret = (errno ? errno : EINVAL);
3883 		ERROR("%p: QP creation failure: %s",
3884 		      (void *)dev, strerror(ret));
3885 		goto error;
3886 	}
3887 	mod = (struct ibv_exp_qp_attr){
3888 		/* Move the QP to this state. */
3889 		.qp_state = IBV_QPS_INIT,
3890 		/* Primary port number. */
3891 		.port_num = priv->port
3892 	};
3893 	ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3894 				(IBV_EXP_QP_STATE |
3895 #ifdef RSS_SUPPORT
3896 				 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3897 #endif /* RSS_SUPPORT */
3898 				 IBV_EXP_QP_PORT));
3899 	if (ret) {
3900 		ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3901 		      (void *)dev, strerror(ret));
3902 		goto error;
3903 	}
3904 	if ((parent) || (!priv->rss))  {
3905 		/* Configure MAC and broadcast addresses. */
3906 		ret = rxq_mac_addrs_add(&tmpl);
3907 		if (ret) {
3908 			ERROR("%p: QP flow attachment failed: %s",
3909 			      (void *)dev, strerror(ret));
3910 			goto error;
3911 		}
3912 	}
3913 	/* Allocate descriptors for RX queues, except for the RSS parent. */
3914 	if (parent)
3915 		goto skip_alloc;
3916 	if (tmpl.sp)
3917 		ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3918 	else
3919 		ret = rxq_alloc_elts(&tmpl, desc, NULL);
3920 	if (ret) {
3921 		ERROR("%p: RXQ allocation failed: %s",
3922 		      (void *)dev, strerror(ret));
3923 		goto error;
3924 	}
3925 	ret = ibv_post_recv(tmpl.qp,
3926 			    (tmpl.sp ?
3927 			     &(*tmpl.elts.sp)[0].wr :
3928 			     &(*tmpl.elts.no_sp)[0].wr),
3929 			    &bad_wr);
3930 	if (ret) {
3931 		ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3932 		      (void *)dev,
3933 		      (void *)bad_wr,
3934 		      strerror(ret));
3935 		goto error;
3936 	}
3937 skip_alloc:
3938 	mod = (struct ibv_exp_qp_attr){
3939 		.qp_state = IBV_QPS_RTR
3940 	};
3941 	ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3942 	if (ret) {
3943 		ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3944 		      (void *)dev, strerror(ret));
3945 		goto error;
3946 	}
3947 	/* Save port ID. */
3948 	tmpl.port_id = dev->data->port_id;
3949 	DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3950 	attr.params = (struct ibv_exp_query_intf_params){
3951 		.intf_scope = IBV_EXP_INTF_GLOBAL,
3952 		.intf = IBV_EXP_INTF_CQ,
3953 		.obj = tmpl.cq,
3954 	};
3955 	tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3956 	if (tmpl.if_cq == NULL) {
3957 		ERROR("%p: CQ interface family query failed with status %d",
3958 		      (void *)dev, status);
3959 		goto error;
3960 	}
3961 	attr.params = (struct ibv_exp_query_intf_params){
3962 		.intf_scope = IBV_EXP_INTF_GLOBAL,
3963 		.intf = IBV_EXP_INTF_QP_BURST,
3964 		.obj = tmpl.qp,
3965 	};
3966 	tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3967 	if (tmpl.if_qp == NULL) {
3968 		ERROR("%p: QP interface family query failed with status %d",
3969 		      (void *)dev, status);
3970 		goto error;
3971 	}
3972 	/* Clean up rxq in case we're reinitializing it. */
3973 	DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3974 	rxq_cleanup(rxq);
3975 	*rxq = tmpl;
3976 	DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3977 	assert(ret == 0);
3978 	return 0;
3979 error:
3980 	rxq_cleanup(&tmpl);
3981 	assert(ret > 0);
3982 	return ret;
3983 }
3984 
3985 /**
3986  * DPDK callback to configure a RX queue.
3987  *
3988  * @param dev
3989  *   Pointer to Ethernet device structure.
3990  * @param idx
3991  *   RX queue index.
3992  * @param desc
3993  *   Number of descriptors to configure in queue.
3994  * @param socket
3995  *   NUMA socket on which memory must be allocated.
3996  * @param[in] conf
3997  *   Thresholds parameters.
3998  * @param mp
3999  *   Memory pool for buffer allocations.
4000  *
4001  * @return
4002  *   0 on success, negative errno value on failure.
4003  */
4004 static int
4005 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
4006 		    unsigned int socket, const struct rte_eth_rxconf *conf,
4007 		    struct rte_mempool *mp)
4008 {
4009 	struct priv *priv = dev->data->dev_private;
4010 	struct rxq *rxq = (*priv->rxqs)[idx];
4011 	int inactive = 0;
4012 	int ret;
4013 
4014 	if (mlx4_is_secondary())
4015 		return -E_RTE_SECONDARY;
4016 	priv_lock(priv);
4017 	DEBUG("%p: configuring queue %u for %u descriptors",
4018 	      (void *)dev, idx, desc);
4019 	if (idx >= priv->rxqs_n) {
4020 		ERROR("%p: queue index out of range (%u >= %u)",
4021 		      (void *)dev, idx, priv->rxqs_n);
4022 		priv_unlock(priv);
4023 		return -EOVERFLOW;
4024 	}
4025 	if (rxq != NULL) {
4026 		DEBUG("%p: reusing already allocated queue index %u (%p)",
4027 		      (void *)dev, idx, (void *)rxq);
4028 		if (priv->started) {
4029 			priv_unlock(priv);
4030 			return -EEXIST;
4031 		}
4032 		(*priv->rxqs)[idx] = NULL;
4033 		rxq_cleanup(rxq);
4034 	} else {
4035 		rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
4036 		if (rxq == NULL) {
4037 			ERROR("%p: unable to allocate queue index %u",
4038 			      (void *)dev, idx);
4039 			priv_unlock(priv);
4040 			return -ENOMEM;
4041 		}
4042 	}
4043 	if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
4044 		inactive = 1;
4045 	ret = rxq_setup(dev, rxq, desc, socket, inactive, conf, mp);
4046 	if (ret)
4047 		rte_free(rxq);
4048 	else {
4049 		rxq->stats.idx = idx;
4050 		DEBUG("%p: adding RX queue %p to list",
4051 		      (void *)dev, (void *)rxq);
4052 		(*priv->rxqs)[idx] = rxq;
4053 		/* Update receive callback. */
4054 		if (rxq->sp)
4055 			dev->rx_pkt_burst = mlx4_rx_burst_sp;
4056 		else
4057 			dev->rx_pkt_burst = mlx4_rx_burst;
4058 	}
4059 	priv_unlock(priv);
4060 	return -ret;
4061 }
4062 
4063 /**
4064  * DPDK callback to release a RX queue.
4065  *
4066  * @param dpdk_rxq
4067  *   Generic RX queue pointer.
4068  */
4069 static void
4070 mlx4_rx_queue_release(void *dpdk_rxq)
4071 {
4072 	struct rxq *rxq = (struct rxq *)dpdk_rxq;
4073 	struct priv *priv;
4074 	unsigned int i;
4075 
4076 	if (mlx4_is_secondary())
4077 		return;
4078 	if (rxq == NULL)
4079 		return;
4080 	priv = rxq->priv;
4081 	priv_lock(priv);
4082 	assert(rxq != &priv->rxq_parent);
4083 	for (i = 0; (i != priv->rxqs_n); ++i)
4084 		if ((*priv->rxqs)[i] == rxq) {
4085 			DEBUG("%p: removing RX queue %p from list",
4086 			      (void *)priv->dev, (void *)rxq);
4087 			(*priv->rxqs)[i] = NULL;
4088 			break;
4089 		}
4090 	rxq_cleanup(rxq);
4091 	rte_free(rxq);
4092 	priv_unlock(priv);
4093 }
4094 
4095 static void
4096 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4097 
4098 /**
4099  * DPDK callback to start the device.
4100  *
4101  * Simulate device start by attaching all configured flows.
4102  *
4103  * @param dev
4104  *   Pointer to Ethernet device structure.
4105  *
4106  * @return
4107  *   0 on success, negative errno value on failure.
4108  */
4109 static int
4110 mlx4_dev_start(struct rte_eth_dev *dev)
4111 {
4112 	struct priv *priv = dev->data->dev_private;
4113 	unsigned int i = 0;
4114 	unsigned int r;
4115 	struct rxq *rxq;
4116 
4117 	if (mlx4_is_secondary())
4118 		return -E_RTE_SECONDARY;
4119 	priv_lock(priv);
4120 	if (priv->started) {
4121 		priv_unlock(priv);
4122 		return 0;
4123 	}
4124 	DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
4125 	priv->started = 1;
4126 	if (priv->rss) {
4127 		rxq = &priv->rxq_parent;
4128 		r = 1;
4129 	} else {
4130 		rxq = (*priv->rxqs)[0];
4131 		r = priv->rxqs_n;
4132 	}
4133 	/* Iterate only once when RSS is enabled. */
4134 	do {
4135 		int ret;
4136 
4137 		/* Ignore nonexistent RX queues. */
4138 		if (rxq == NULL)
4139 			continue;
4140 		ret = rxq_mac_addrs_add(rxq);
4141 		if (!ret && priv->promisc)
4142 			ret = rxq_promiscuous_enable(rxq);
4143 		if (!ret && priv->allmulti)
4144 			ret = rxq_allmulticast_enable(rxq);
4145 		if (!ret)
4146 			continue;
4147 		WARN("%p: QP flow attachment failed: %s",
4148 		     (void *)dev, strerror(ret));
4149 		/* Rollback. */
4150 		while (i != 0) {
4151 			rxq = (*priv->rxqs)[--i];
4152 			if (rxq != NULL) {
4153 				rxq_allmulticast_disable(rxq);
4154 				rxq_promiscuous_disable(rxq);
4155 				rxq_mac_addrs_del(rxq);
4156 			}
4157 		}
4158 		priv->started = 0;
4159 		priv_unlock(priv);
4160 		return -ret;
4161 	} while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4162 	priv_dev_interrupt_handler_install(priv, dev);
4163 	priv_unlock(priv);
4164 	return 0;
4165 }
4166 
4167 /**
4168  * DPDK callback to stop the device.
4169  *
4170  * Simulate device stop by detaching all configured flows.
4171  *
4172  * @param dev
4173  *   Pointer to Ethernet device structure.
4174  */
4175 static void
4176 mlx4_dev_stop(struct rte_eth_dev *dev)
4177 {
4178 	struct priv *priv = dev->data->dev_private;
4179 	unsigned int i = 0;
4180 	unsigned int r;
4181 	struct rxq *rxq;
4182 
4183 	if (mlx4_is_secondary())
4184 		return;
4185 	priv_lock(priv);
4186 	if (!priv->started) {
4187 		priv_unlock(priv);
4188 		return;
4189 	}
4190 	DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4191 	priv->started = 0;
4192 	if (priv->rss) {
4193 		rxq = &priv->rxq_parent;
4194 		r = 1;
4195 	} else {
4196 		rxq = (*priv->rxqs)[0];
4197 		r = priv->rxqs_n;
4198 	}
4199 	/* Iterate only once when RSS is enabled. */
4200 	do {
4201 		/* Ignore nonexistent RX queues. */
4202 		if (rxq == NULL)
4203 			continue;
4204 		rxq_allmulticast_disable(rxq);
4205 		rxq_promiscuous_disable(rxq);
4206 		rxq_mac_addrs_del(rxq);
4207 	} while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4208 	priv_unlock(priv);
4209 }
4210 
4211 /**
4212  * Dummy DPDK callback for TX.
4213  *
4214  * This function is used to temporarily replace the real callback during
4215  * unsafe control operations on the queue, or in case of error.
4216  *
4217  * @param dpdk_txq
4218  *   Generic pointer to TX queue structure.
4219  * @param[in] pkts
4220  *   Packets to transmit.
4221  * @param pkts_n
4222  *   Number of packets in array.
4223  *
4224  * @return
4225  *   Number of packets successfully transmitted (<= pkts_n).
4226  */
4227 static uint16_t
4228 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4229 {
4230 	(void)dpdk_txq;
4231 	(void)pkts;
4232 	(void)pkts_n;
4233 	return 0;
4234 }
4235 
4236 /**
4237  * Dummy DPDK callback for RX.
4238  *
4239  * This function is used to temporarily replace the real callback during
4240  * unsafe control operations on the queue, or in case of error.
4241  *
4242  * @param dpdk_rxq
4243  *   Generic pointer to RX queue structure.
4244  * @param[out] pkts
4245  *   Array to store received packets.
4246  * @param pkts_n
4247  *   Maximum number of packets in array.
4248  *
4249  * @return
4250  *   Number of packets successfully received (<= pkts_n).
4251  */
4252 static uint16_t
4253 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4254 {
4255 	(void)dpdk_rxq;
4256 	(void)pkts;
4257 	(void)pkts_n;
4258 	return 0;
4259 }
4260 
4261 static void
4262 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4263 
4264 /**
4265  * DPDK callback to close the device.
4266  *
4267  * Destroy all queues and objects, free memory.
4268  *
4269  * @param dev
4270  *   Pointer to Ethernet device structure.
4271  */
4272 static void
4273 mlx4_dev_close(struct rte_eth_dev *dev)
4274 {
4275 	struct priv *priv = mlx4_get_priv(dev);
4276 	void *tmp;
4277 	unsigned int i;
4278 
4279 	if (priv == NULL)
4280 		return;
4281 	priv_lock(priv);
4282 	DEBUG("%p: closing device \"%s\"",
4283 	      (void *)dev,
4284 	      ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4285 	/* Prevent crashes when queues are still in use. This is unfortunately
4286 	 * still required for DPDK 1.3 because some programs (such as testpmd)
4287 	 * never release them before closing the device. */
4288 	dev->rx_pkt_burst = removed_rx_burst;
4289 	dev->tx_pkt_burst = removed_tx_burst;
4290 	if (priv->rxqs != NULL) {
4291 		/* XXX race condition if mlx4_rx_burst() is still running. */
4292 		usleep(1000);
4293 		for (i = 0; (i != priv->rxqs_n); ++i) {
4294 			tmp = (*priv->rxqs)[i];
4295 			if (tmp == NULL)
4296 				continue;
4297 			(*priv->rxqs)[i] = NULL;
4298 			rxq_cleanup(tmp);
4299 			rte_free(tmp);
4300 		}
4301 		priv->rxqs_n = 0;
4302 		priv->rxqs = NULL;
4303 	}
4304 	if (priv->txqs != NULL) {
4305 		/* XXX race condition if mlx4_tx_burst() is still running. */
4306 		usleep(1000);
4307 		for (i = 0; (i != priv->txqs_n); ++i) {
4308 			tmp = (*priv->txqs)[i];
4309 			if (tmp == NULL)
4310 				continue;
4311 			(*priv->txqs)[i] = NULL;
4312 			txq_cleanup(tmp);
4313 			rte_free(tmp);
4314 		}
4315 		priv->txqs_n = 0;
4316 		priv->txqs = NULL;
4317 	}
4318 	if (priv->rss)
4319 		rxq_cleanup(&priv->rxq_parent);
4320 	if (priv->pd != NULL) {
4321 		assert(priv->ctx != NULL);
4322 		claim_zero(ibv_dealloc_pd(priv->pd));
4323 		claim_zero(ibv_close_device(priv->ctx));
4324 	} else
4325 		assert(priv->ctx == NULL);
4326 	priv_dev_interrupt_handler_uninstall(priv, dev);
4327 	priv_unlock(priv);
4328 	memset(priv, 0, sizeof(*priv));
4329 }
4330 
4331 /**
4332  * Change the link state (UP / DOWN).
4333  *
4334  * @param priv
4335  *   Pointer to Ethernet device private data.
4336  * @param up
4337  *   Nonzero for link up, otherwise link down.
4338  *
4339  * @return
4340  *   0 on success, errno value on failure.
4341  */
4342 static int
4343 priv_set_link(struct priv *priv, int up)
4344 {
4345 	struct rte_eth_dev *dev = priv->dev;
4346 	int err;
4347 	unsigned int i;
4348 
4349 	if (up) {
4350 		err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4351 		if (err)
4352 			return err;
4353 		for (i = 0; i < priv->rxqs_n; i++)
4354 			if ((*priv->rxqs)[i]->sp)
4355 				break;
4356 		/* Check if an sp queue exists.
4357 		 * Note: Some old frames might be received.
4358 		 */
4359 		if (i == priv->rxqs_n)
4360 			dev->rx_pkt_burst = mlx4_rx_burst;
4361 		else
4362 			dev->rx_pkt_burst = mlx4_rx_burst_sp;
4363 		dev->tx_pkt_burst = mlx4_tx_burst;
4364 	} else {
4365 		err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4366 		if (err)
4367 			return err;
4368 		dev->rx_pkt_burst = removed_rx_burst;
4369 		dev->tx_pkt_burst = removed_tx_burst;
4370 	}
4371 	return 0;
4372 }
4373 
4374 /**
4375  * DPDK callback to bring the link DOWN.
4376  *
4377  * @param dev
4378  *   Pointer to Ethernet device structure.
4379  *
4380  * @return
4381  *   0 on success, errno value on failure.
4382  */
4383 static int
4384 mlx4_set_link_down(struct rte_eth_dev *dev)
4385 {
4386 	struct priv *priv = dev->data->dev_private;
4387 	int err;
4388 
4389 	priv_lock(priv);
4390 	err = priv_set_link(priv, 0);
4391 	priv_unlock(priv);
4392 	return err;
4393 }
4394 
4395 /**
4396  * DPDK callback to bring the link UP.
4397  *
4398  * @param dev
4399  *   Pointer to Ethernet device structure.
4400  *
4401  * @return
4402  *   0 on success, errno value on failure.
4403  */
4404 static int
4405 mlx4_set_link_up(struct rte_eth_dev *dev)
4406 {
4407 	struct priv *priv = dev->data->dev_private;
4408 	int err;
4409 
4410 	priv_lock(priv);
4411 	err = priv_set_link(priv, 1);
4412 	priv_unlock(priv);
4413 	return err;
4414 }
4415 /**
4416  * DPDK callback to get information about the device.
4417  *
4418  * @param dev
4419  *   Pointer to Ethernet device structure.
4420  * @param[out] info
4421  *   Info structure output buffer.
4422  */
4423 static void
4424 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4425 {
4426 	struct priv *priv = mlx4_get_priv(dev);
4427 	unsigned int max;
4428 	char ifname[IF_NAMESIZE];
4429 
4430 	info->pci_dev = RTE_DEV_TO_PCI(dev->device);
4431 
4432 	if (priv == NULL)
4433 		return;
4434 	priv_lock(priv);
4435 	/* FIXME: we should ask the device for these values. */
4436 	info->min_rx_bufsize = 32;
4437 	info->max_rx_pktlen = 65536;
4438 	/*
4439 	 * Since we need one CQ per QP, the limit is the minimum number
4440 	 * between the two values.
4441 	 */
4442 	max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4443 	       priv->device_attr.max_qp : priv->device_attr.max_cq);
4444 	/* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4445 	if (max >= 65535)
4446 		max = 65535;
4447 	info->max_rx_queues = max;
4448 	info->max_tx_queues = max;
4449 	/* Last array entry is reserved for broadcast. */
4450 	info->max_mac_addrs = (elemof(priv->mac) - 1);
4451 	info->rx_offload_capa =
4452 		(priv->hw_csum ?
4453 		 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4454 		  DEV_RX_OFFLOAD_UDP_CKSUM |
4455 		  DEV_RX_OFFLOAD_TCP_CKSUM) :
4456 		 0);
4457 	info->tx_offload_capa =
4458 		(priv->hw_csum ?
4459 		 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4460 		  DEV_TX_OFFLOAD_UDP_CKSUM |
4461 		  DEV_TX_OFFLOAD_TCP_CKSUM) :
4462 		 0);
4463 	if (priv_get_ifname(priv, &ifname) == 0)
4464 		info->if_index = if_nametoindex(ifname);
4465 	info->speed_capa =
4466 			ETH_LINK_SPEED_1G |
4467 			ETH_LINK_SPEED_10G |
4468 			ETH_LINK_SPEED_20G |
4469 			ETH_LINK_SPEED_40G |
4470 			ETH_LINK_SPEED_56G;
4471 	priv_unlock(priv);
4472 }
4473 
4474 static const uint32_t *
4475 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4476 {
4477 	static const uint32_t ptypes[] = {
4478 		/* refers to rxq_cq_to_pkt_type() */
4479 		RTE_PTYPE_L3_IPV4,
4480 		RTE_PTYPE_L3_IPV6,
4481 		RTE_PTYPE_INNER_L3_IPV4,
4482 		RTE_PTYPE_INNER_L3_IPV6,
4483 		RTE_PTYPE_UNKNOWN
4484 	};
4485 
4486 	if (dev->rx_pkt_burst == mlx4_rx_burst ||
4487 	    dev->rx_pkt_burst == mlx4_rx_burst_sp)
4488 		return ptypes;
4489 	return NULL;
4490 }
4491 
4492 /**
4493  * DPDK callback to get device statistics.
4494  *
4495  * @param dev
4496  *   Pointer to Ethernet device structure.
4497  * @param[out] stats
4498  *   Stats structure output buffer.
4499  */
4500 static void
4501 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4502 {
4503 	struct priv *priv = mlx4_get_priv(dev);
4504 	struct rte_eth_stats tmp = {0};
4505 	unsigned int i;
4506 	unsigned int idx;
4507 
4508 	if (priv == NULL)
4509 		return;
4510 	priv_lock(priv);
4511 	/* Add software counters. */
4512 	for (i = 0; (i != priv->rxqs_n); ++i) {
4513 		struct rxq *rxq = (*priv->rxqs)[i];
4514 
4515 		if (rxq == NULL)
4516 			continue;
4517 		idx = rxq->stats.idx;
4518 		if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4519 #ifdef MLX4_PMD_SOFT_COUNTERS
4520 			tmp.q_ipackets[idx] += rxq->stats.ipackets;
4521 			tmp.q_ibytes[idx] += rxq->stats.ibytes;
4522 #endif
4523 			tmp.q_errors[idx] += (rxq->stats.idropped +
4524 					      rxq->stats.rx_nombuf);
4525 		}
4526 #ifdef MLX4_PMD_SOFT_COUNTERS
4527 		tmp.ipackets += rxq->stats.ipackets;
4528 		tmp.ibytes += rxq->stats.ibytes;
4529 #endif
4530 		tmp.ierrors += rxq->stats.idropped;
4531 		tmp.rx_nombuf += rxq->stats.rx_nombuf;
4532 	}
4533 	for (i = 0; (i != priv->txqs_n); ++i) {
4534 		struct txq *txq = (*priv->txqs)[i];
4535 
4536 		if (txq == NULL)
4537 			continue;
4538 		idx = txq->stats.idx;
4539 		if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4540 #ifdef MLX4_PMD_SOFT_COUNTERS
4541 			tmp.q_opackets[idx] += txq->stats.opackets;
4542 			tmp.q_obytes[idx] += txq->stats.obytes;
4543 #endif
4544 			tmp.q_errors[idx] += txq->stats.odropped;
4545 		}
4546 #ifdef MLX4_PMD_SOFT_COUNTERS
4547 		tmp.opackets += txq->stats.opackets;
4548 		tmp.obytes += txq->stats.obytes;
4549 #endif
4550 		tmp.oerrors += txq->stats.odropped;
4551 	}
4552 #ifndef MLX4_PMD_SOFT_COUNTERS
4553 	/* FIXME: retrieve and add hardware counters. */
4554 #endif
4555 	*stats = tmp;
4556 	priv_unlock(priv);
4557 }
4558 
4559 /**
4560  * DPDK callback to clear device statistics.
4561  *
4562  * @param dev
4563  *   Pointer to Ethernet device structure.
4564  */
4565 static void
4566 mlx4_stats_reset(struct rte_eth_dev *dev)
4567 {
4568 	struct priv *priv = mlx4_get_priv(dev);
4569 	unsigned int i;
4570 	unsigned int idx;
4571 
4572 	if (priv == NULL)
4573 		return;
4574 	priv_lock(priv);
4575 	for (i = 0; (i != priv->rxqs_n); ++i) {
4576 		if ((*priv->rxqs)[i] == NULL)
4577 			continue;
4578 		idx = (*priv->rxqs)[i]->stats.idx;
4579 		(*priv->rxqs)[i]->stats =
4580 			(struct mlx4_rxq_stats){ .idx = idx };
4581 	}
4582 	for (i = 0; (i != priv->txqs_n); ++i) {
4583 		if ((*priv->txqs)[i] == NULL)
4584 			continue;
4585 		idx = (*priv->txqs)[i]->stats.idx;
4586 		(*priv->txqs)[i]->stats =
4587 			(struct mlx4_txq_stats){ .idx = idx };
4588 	}
4589 #ifndef MLX4_PMD_SOFT_COUNTERS
4590 	/* FIXME: reset hardware counters. */
4591 #endif
4592 	priv_unlock(priv);
4593 }
4594 
4595 /**
4596  * DPDK callback to remove a MAC address.
4597  *
4598  * @param dev
4599  *   Pointer to Ethernet device structure.
4600  * @param index
4601  *   MAC address index.
4602  */
4603 static void
4604 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4605 {
4606 	struct priv *priv = dev->data->dev_private;
4607 
4608 	if (mlx4_is_secondary())
4609 		return;
4610 	priv_lock(priv);
4611 	DEBUG("%p: removing MAC address from index %" PRIu32,
4612 	      (void *)dev, index);
4613 	/* Last array entry is reserved for broadcast. */
4614 	if (index >= (elemof(priv->mac) - 1))
4615 		goto end;
4616 	priv_mac_addr_del(priv, index);
4617 end:
4618 	priv_unlock(priv);
4619 }
4620 
4621 /**
4622  * DPDK callback to add a MAC address.
4623  *
4624  * @param dev
4625  *   Pointer to Ethernet device structure.
4626  * @param mac_addr
4627  *   MAC address to register.
4628  * @param index
4629  *   MAC address index.
4630  * @param vmdq
4631  *   VMDq pool index to associate address with (ignored).
4632  */
4633 static void
4634 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4635 		  uint32_t index, uint32_t vmdq)
4636 {
4637 	struct priv *priv = dev->data->dev_private;
4638 
4639 	if (mlx4_is_secondary())
4640 		return;
4641 	(void)vmdq;
4642 	priv_lock(priv);
4643 	DEBUG("%p: adding MAC address at index %" PRIu32,
4644 	      (void *)dev, index);
4645 	/* Last array entry is reserved for broadcast. */
4646 	if (index >= (elemof(priv->mac) - 1))
4647 		goto end;
4648 	priv_mac_addr_add(priv, index,
4649 			  (const uint8_t (*)[ETHER_ADDR_LEN])
4650 			  mac_addr->addr_bytes);
4651 end:
4652 	priv_unlock(priv);
4653 }
4654 
4655 /**
4656  * DPDK callback to set the primary MAC address.
4657  *
4658  * @param dev
4659  *   Pointer to Ethernet device structure.
4660  * @param mac_addr
4661  *   MAC address to register.
4662  */
4663 static void
4664 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4665 {
4666 	DEBUG("%p: setting primary MAC address", (void *)dev);
4667 	mlx4_mac_addr_remove(dev, 0);
4668 	mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4669 }
4670 
4671 /**
4672  * DPDK callback to enable promiscuous mode.
4673  *
4674  * @param dev
4675  *   Pointer to Ethernet device structure.
4676  */
4677 static void
4678 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4679 {
4680 	struct priv *priv = dev->data->dev_private;
4681 	unsigned int i;
4682 	int ret;
4683 
4684 	if (mlx4_is_secondary())
4685 		return;
4686 	priv_lock(priv);
4687 	if (priv->promisc) {
4688 		priv_unlock(priv);
4689 		return;
4690 	}
4691 	/* If device isn't started, this is all we need to do. */
4692 	if (!priv->started)
4693 		goto end;
4694 	if (priv->rss) {
4695 		ret = rxq_promiscuous_enable(&priv->rxq_parent);
4696 		if (ret) {
4697 			priv_unlock(priv);
4698 			return;
4699 		}
4700 		goto end;
4701 	}
4702 	for (i = 0; (i != priv->rxqs_n); ++i) {
4703 		if ((*priv->rxqs)[i] == NULL)
4704 			continue;
4705 		ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4706 		if (!ret)
4707 			continue;
4708 		/* Failure, rollback. */
4709 		while (i != 0)
4710 			if ((*priv->rxqs)[--i] != NULL)
4711 				rxq_promiscuous_disable((*priv->rxqs)[i]);
4712 		priv_unlock(priv);
4713 		return;
4714 	}
4715 end:
4716 	priv->promisc = 1;
4717 	priv_unlock(priv);
4718 }
4719 
4720 /**
4721  * DPDK callback to disable promiscuous mode.
4722  *
4723  * @param dev
4724  *   Pointer to Ethernet device structure.
4725  */
4726 static void
4727 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4728 {
4729 	struct priv *priv = dev->data->dev_private;
4730 	unsigned int i;
4731 
4732 	if (mlx4_is_secondary())
4733 		return;
4734 	priv_lock(priv);
4735 	if (!priv->promisc) {
4736 		priv_unlock(priv);
4737 		return;
4738 	}
4739 	if (priv->rss) {
4740 		rxq_promiscuous_disable(&priv->rxq_parent);
4741 		goto end;
4742 	}
4743 	for (i = 0; (i != priv->rxqs_n); ++i)
4744 		if ((*priv->rxqs)[i] != NULL)
4745 			rxq_promiscuous_disable((*priv->rxqs)[i]);
4746 end:
4747 	priv->promisc = 0;
4748 	priv_unlock(priv);
4749 }
4750 
4751 /**
4752  * DPDK callback to enable allmulti mode.
4753  *
4754  * @param dev
4755  *   Pointer to Ethernet device structure.
4756  */
4757 static void
4758 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4759 {
4760 	struct priv *priv = dev->data->dev_private;
4761 	unsigned int i;
4762 	int ret;
4763 
4764 	if (mlx4_is_secondary())
4765 		return;
4766 	priv_lock(priv);
4767 	if (priv->allmulti) {
4768 		priv_unlock(priv);
4769 		return;
4770 	}
4771 	/* If device isn't started, this is all we need to do. */
4772 	if (!priv->started)
4773 		goto end;
4774 	if (priv->rss) {
4775 		ret = rxq_allmulticast_enable(&priv->rxq_parent);
4776 		if (ret) {
4777 			priv_unlock(priv);
4778 			return;
4779 		}
4780 		goto end;
4781 	}
4782 	for (i = 0; (i != priv->rxqs_n); ++i) {
4783 		if ((*priv->rxqs)[i] == NULL)
4784 			continue;
4785 		ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4786 		if (!ret)
4787 			continue;
4788 		/* Failure, rollback. */
4789 		while (i != 0)
4790 			if ((*priv->rxqs)[--i] != NULL)
4791 				rxq_allmulticast_disable((*priv->rxqs)[i]);
4792 		priv_unlock(priv);
4793 		return;
4794 	}
4795 end:
4796 	priv->allmulti = 1;
4797 	priv_unlock(priv);
4798 }
4799 
4800 /**
4801  * DPDK callback to disable allmulti mode.
4802  *
4803  * @param dev
4804  *   Pointer to Ethernet device structure.
4805  */
4806 static void
4807 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4808 {
4809 	struct priv *priv = dev->data->dev_private;
4810 	unsigned int i;
4811 
4812 	if (mlx4_is_secondary())
4813 		return;
4814 	priv_lock(priv);
4815 	if (!priv->allmulti) {
4816 		priv_unlock(priv);
4817 		return;
4818 	}
4819 	if (priv->rss) {
4820 		rxq_allmulticast_disable(&priv->rxq_parent);
4821 		goto end;
4822 	}
4823 	for (i = 0; (i != priv->rxqs_n); ++i)
4824 		if ((*priv->rxqs)[i] != NULL)
4825 			rxq_allmulticast_disable((*priv->rxqs)[i]);
4826 end:
4827 	priv->allmulti = 0;
4828 	priv_unlock(priv);
4829 }
4830 
4831 /**
4832  * DPDK callback to retrieve physical link information.
4833  *
4834  * @param dev
4835  *   Pointer to Ethernet device structure.
4836  * @param wait_to_complete
4837  *   Wait for request completion (ignored).
4838  */
4839 static int
4840 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4841 {
4842 	const struct priv *priv = mlx4_get_priv(dev);
4843 	struct ethtool_cmd edata = {
4844 		.cmd = ETHTOOL_GSET
4845 	};
4846 	struct ifreq ifr;
4847 	struct rte_eth_link dev_link;
4848 	int link_speed = 0;
4849 
4850 	/* priv_lock() is not taken to allow concurrent calls. */
4851 
4852 	if (priv == NULL)
4853 		return -EINVAL;
4854 	(void)wait_to_complete;
4855 	if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4856 		WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4857 		return -1;
4858 	}
4859 	memset(&dev_link, 0, sizeof(dev_link));
4860 	dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4861 				(ifr.ifr_flags & IFF_RUNNING));
4862 	ifr.ifr_data = (void *)&edata;
4863 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4864 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4865 		     strerror(errno));
4866 		return -1;
4867 	}
4868 	link_speed = ethtool_cmd_speed(&edata);
4869 	if (link_speed == -1)
4870 		dev_link.link_speed = 0;
4871 	else
4872 		dev_link.link_speed = link_speed;
4873 	dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4874 				ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4875 	dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4876 			ETH_LINK_SPEED_FIXED);
4877 	if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4878 		/* Link status changed. */
4879 		dev->data->dev_link = dev_link;
4880 		return 0;
4881 	}
4882 	/* Link status is still the same. */
4883 	return -1;
4884 }
4885 
4886 /**
4887  * DPDK callback to change the MTU.
4888  *
4889  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4890  * received). Use this as a hint to enable/disable scattered packets support
4891  * and improve performance when not needed.
4892  * Since failure is not an option, reconfiguring queues on the fly is not
4893  * recommended.
4894  *
4895  * @param dev
4896  *   Pointer to Ethernet device structure.
4897  * @param in_mtu
4898  *   New MTU.
4899  *
4900  * @return
4901  *   0 on success, negative errno value on failure.
4902  */
4903 static int
4904 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4905 {
4906 	struct priv *priv = dev->data->dev_private;
4907 	int ret = 0;
4908 	unsigned int i;
4909 	uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4910 		mlx4_rx_burst;
4911 
4912 	if (mlx4_is_secondary())
4913 		return -E_RTE_SECONDARY;
4914 	priv_lock(priv);
4915 	/* Set kernel interface MTU first. */
4916 	if (priv_set_mtu(priv, mtu)) {
4917 		ret = errno;
4918 		WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4919 		     strerror(ret));
4920 		goto out;
4921 	} else
4922 		DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4923 	priv->mtu = mtu;
4924 	/* Temporarily replace RX handler with a fake one, assuming it has not
4925 	 * been copied elsewhere. */
4926 	dev->rx_pkt_burst = removed_rx_burst;
4927 	/* Make sure everyone has left mlx4_rx_burst() and uses
4928 	 * removed_rx_burst() instead. */
4929 	rte_wmb();
4930 	usleep(1000);
4931 	/* Reconfigure each RX queue. */
4932 	for (i = 0; (i != priv->rxqs_n); ++i) {
4933 		struct rxq *rxq = (*priv->rxqs)[i];
4934 		unsigned int mb_len;
4935 		unsigned int max_frame_len;
4936 		int sp;
4937 
4938 		if (rxq == NULL)
4939 			continue;
4940 		/* Calculate new maximum frame length according to MTU and
4941 		 * toggle scattered support (sp) if necessary. */
4942 		max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4943 				 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4944 		mb_len = rte_pktmbuf_data_room_size(rxq->mp);
4945 		assert(mb_len >= RTE_PKTMBUF_HEADROOM);
4946 		sp = (max_frame_len > (mb_len - RTE_PKTMBUF_HEADROOM));
4947 		/* Provide new values to rxq_setup(). */
4948 		dev->data->dev_conf.rxmode.jumbo_frame = sp;
4949 		dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4950 		ret = rxq_rehash(dev, rxq);
4951 		if (ret) {
4952 			/* Force SP RX if that queue requires it and abort. */
4953 			if (rxq->sp)
4954 				rx_func = mlx4_rx_burst_sp;
4955 			break;
4956 		}
4957 		/* Reenable non-RSS queue attributes. No need to check
4958 		 * for errors at this stage. */
4959 		if (!priv->rss) {
4960 			rxq_mac_addrs_add(rxq);
4961 			if (priv->promisc)
4962 				rxq_promiscuous_enable(rxq);
4963 			if (priv->allmulti)
4964 				rxq_allmulticast_enable(rxq);
4965 		}
4966 		/* Scattered burst function takes priority. */
4967 		if (rxq->sp)
4968 			rx_func = mlx4_rx_burst_sp;
4969 	}
4970 	/* Burst functions can now be called again. */
4971 	rte_wmb();
4972 	dev->rx_pkt_burst = rx_func;
4973 out:
4974 	priv_unlock(priv);
4975 	assert(ret >= 0);
4976 	return -ret;
4977 }
4978 
4979 /**
4980  * DPDK callback to get flow control status.
4981  *
4982  * @param dev
4983  *   Pointer to Ethernet device structure.
4984  * @param[out] fc_conf
4985  *   Flow control output buffer.
4986  *
4987  * @return
4988  *   0 on success, negative errno value on failure.
4989  */
4990 static int
4991 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4992 {
4993 	struct priv *priv = dev->data->dev_private;
4994 	struct ifreq ifr;
4995 	struct ethtool_pauseparam ethpause = {
4996 		.cmd = ETHTOOL_GPAUSEPARAM
4997 	};
4998 	int ret;
4999 
5000 	if (mlx4_is_secondary())
5001 		return -E_RTE_SECONDARY;
5002 	ifr.ifr_data = (void *)&ethpause;
5003 	priv_lock(priv);
5004 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5005 		ret = errno;
5006 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
5007 		     " failed: %s",
5008 		     strerror(ret));
5009 		goto out;
5010 	}
5011 
5012 	fc_conf->autoneg = ethpause.autoneg;
5013 	if (ethpause.rx_pause && ethpause.tx_pause)
5014 		fc_conf->mode = RTE_FC_FULL;
5015 	else if (ethpause.rx_pause)
5016 		fc_conf->mode = RTE_FC_RX_PAUSE;
5017 	else if (ethpause.tx_pause)
5018 		fc_conf->mode = RTE_FC_TX_PAUSE;
5019 	else
5020 		fc_conf->mode = RTE_FC_NONE;
5021 	ret = 0;
5022 
5023 out:
5024 	priv_unlock(priv);
5025 	assert(ret >= 0);
5026 	return -ret;
5027 }
5028 
5029 /**
5030  * DPDK callback to modify flow control parameters.
5031  *
5032  * @param dev
5033  *   Pointer to Ethernet device structure.
5034  * @param[in] fc_conf
5035  *   Flow control parameters.
5036  *
5037  * @return
5038  *   0 on success, negative errno value on failure.
5039  */
5040 static int
5041 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5042 {
5043 	struct priv *priv = dev->data->dev_private;
5044 	struct ifreq ifr;
5045 	struct ethtool_pauseparam ethpause = {
5046 		.cmd = ETHTOOL_SPAUSEPARAM
5047 	};
5048 	int ret;
5049 
5050 	if (mlx4_is_secondary())
5051 		return -E_RTE_SECONDARY;
5052 	ifr.ifr_data = (void *)&ethpause;
5053 	ethpause.autoneg = fc_conf->autoneg;
5054 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5055 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
5056 		ethpause.rx_pause = 1;
5057 	else
5058 		ethpause.rx_pause = 0;
5059 
5060 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5061 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
5062 		ethpause.tx_pause = 1;
5063 	else
5064 		ethpause.tx_pause = 0;
5065 
5066 	priv_lock(priv);
5067 	if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5068 		ret = errno;
5069 		WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
5070 		     " failed: %s",
5071 		     strerror(ret));
5072 		goto out;
5073 	}
5074 	ret = 0;
5075 
5076 out:
5077 	priv_unlock(priv);
5078 	assert(ret >= 0);
5079 	return -ret;
5080 }
5081 
5082 /**
5083  * Configure a VLAN filter.
5084  *
5085  * @param dev
5086  *   Pointer to Ethernet device structure.
5087  * @param vlan_id
5088  *   VLAN ID to filter.
5089  * @param on
5090  *   Toggle filter.
5091  *
5092  * @return
5093  *   0 on success, errno value on failure.
5094  */
5095 static int
5096 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5097 {
5098 	struct priv *priv = dev->data->dev_private;
5099 	unsigned int i;
5100 	unsigned int j = -1;
5101 
5102 	DEBUG("%p: %s VLAN filter ID %" PRIu16,
5103 	      (void *)dev, (on ? "enable" : "disable"), vlan_id);
5104 	for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
5105 		if (!priv->vlan_filter[i].enabled) {
5106 			/* Unused index, remember it. */
5107 			j = i;
5108 			continue;
5109 		}
5110 		if (priv->vlan_filter[i].id != vlan_id)
5111 			continue;
5112 		/* This VLAN ID is already known, use its index. */
5113 		j = i;
5114 		break;
5115 	}
5116 	/* Check if there's room for another VLAN filter. */
5117 	if (j == (unsigned int)-1)
5118 		return ENOMEM;
5119 	/*
5120 	 * VLAN filters apply to all configured MAC addresses, flow
5121 	 * specifications must be reconfigured accordingly.
5122 	 */
5123 	priv->vlan_filter[j].id = vlan_id;
5124 	if ((on) && (!priv->vlan_filter[j].enabled)) {
5125 		/*
5126 		 * Filter is disabled, enable it.
5127 		 * Rehashing flows in all RX queues is necessary.
5128 		 */
5129 		if (priv->rss)
5130 			rxq_mac_addrs_del(&priv->rxq_parent);
5131 		else
5132 			for (i = 0; (i != priv->rxqs_n); ++i)
5133 				if ((*priv->rxqs)[i] != NULL)
5134 					rxq_mac_addrs_del((*priv->rxqs)[i]);
5135 		priv->vlan_filter[j].enabled = 1;
5136 		if (priv->started) {
5137 			if (priv->rss)
5138 				rxq_mac_addrs_add(&priv->rxq_parent);
5139 			else
5140 				for (i = 0; (i != priv->rxqs_n); ++i) {
5141 					if ((*priv->rxqs)[i] == NULL)
5142 						continue;
5143 					rxq_mac_addrs_add((*priv->rxqs)[i]);
5144 				}
5145 		}
5146 	} else if ((!on) && (priv->vlan_filter[j].enabled)) {
5147 		/*
5148 		 * Filter is enabled, disable it.
5149 		 * Rehashing flows in all RX queues is necessary.
5150 		 */
5151 		if (priv->rss)
5152 			rxq_mac_addrs_del(&priv->rxq_parent);
5153 		else
5154 			for (i = 0; (i != priv->rxqs_n); ++i)
5155 				if ((*priv->rxqs)[i] != NULL)
5156 					rxq_mac_addrs_del((*priv->rxqs)[i]);
5157 		priv->vlan_filter[j].enabled = 0;
5158 		if (priv->started) {
5159 			if (priv->rss)
5160 				rxq_mac_addrs_add(&priv->rxq_parent);
5161 			else
5162 				for (i = 0; (i != priv->rxqs_n); ++i) {
5163 					if ((*priv->rxqs)[i] == NULL)
5164 						continue;
5165 					rxq_mac_addrs_add((*priv->rxqs)[i]);
5166 				}
5167 		}
5168 	}
5169 	return 0;
5170 }
5171 
5172 /**
5173  * DPDK callback to configure a VLAN filter.
5174  *
5175  * @param dev
5176  *   Pointer to Ethernet device structure.
5177  * @param vlan_id
5178  *   VLAN ID to filter.
5179  * @param on
5180  *   Toggle filter.
5181  *
5182  * @return
5183  *   0 on success, negative errno value on failure.
5184  */
5185 static int
5186 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5187 {
5188 	struct priv *priv = dev->data->dev_private;
5189 	int ret;
5190 
5191 	if (mlx4_is_secondary())
5192 		return -E_RTE_SECONDARY;
5193 	priv_lock(priv);
5194 	ret = vlan_filter_set(dev, vlan_id, on);
5195 	priv_unlock(priv);
5196 	assert(ret >= 0);
5197 	return -ret;
5198 }
5199 
5200 static const struct eth_dev_ops mlx4_dev_ops = {
5201 	.dev_configure = mlx4_dev_configure,
5202 	.dev_start = mlx4_dev_start,
5203 	.dev_stop = mlx4_dev_stop,
5204 	.dev_set_link_down = mlx4_set_link_down,
5205 	.dev_set_link_up = mlx4_set_link_up,
5206 	.dev_close = mlx4_dev_close,
5207 	.promiscuous_enable = mlx4_promiscuous_enable,
5208 	.promiscuous_disable = mlx4_promiscuous_disable,
5209 	.allmulticast_enable = mlx4_allmulticast_enable,
5210 	.allmulticast_disable = mlx4_allmulticast_disable,
5211 	.link_update = mlx4_link_update,
5212 	.stats_get = mlx4_stats_get,
5213 	.stats_reset = mlx4_stats_reset,
5214 	.queue_stats_mapping_set = NULL,
5215 	.dev_infos_get = mlx4_dev_infos_get,
5216 	.dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5217 	.vlan_filter_set = mlx4_vlan_filter_set,
5218 	.vlan_tpid_set = NULL,
5219 	.vlan_strip_queue_set = NULL,
5220 	.vlan_offload_set = NULL,
5221 	.rx_queue_setup = mlx4_rx_queue_setup,
5222 	.tx_queue_setup = mlx4_tx_queue_setup,
5223 	.rx_queue_release = mlx4_rx_queue_release,
5224 	.tx_queue_release = mlx4_tx_queue_release,
5225 	.dev_led_on = NULL,
5226 	.dev_led_off = NULL,
5227 	.flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5228 	.flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5229 	.priority_flow_ctrl_set = NULL,
5230 	.mac_addr_remove = mlx4_mac_addr_remove,
5231 	.mac_addr_add = mlx4_mac_addr_add,
5232 	.mac_addr_set = mlx4_mac_addr_set,
5233 	.mtu_set = mlx4_dev_set_mtu,
5234 };
5235 
5236 /**
5237  * Get PCI information from struct ibv_device.
5238  *
5239  * @param device
5240  *   Pointer to Ethernet device structure.
5241  * @param[out] pci_addr
5242  *   PCI bus address output buffer.
5243  *
5244  * @return
5245  *   0 on success, -1 on failure and errno is set.
5246  */
5247 static int
5248 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5249 			    struct rte_pci_addr *pci_addr)
5250 {
5251 	FILE *file;
5252 	char line[32];
5253 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
5254 
5255 	file = fopen(path, "rb");
5256 	if (file == NULL)
5257 		return -1;
5258 	while (fgets(line, sizeof(line), file) == line) {
5259 		size_t len = strlen(line);
5260 		int ret;
5261 
5262 		/* Truncate long lines. */
5263 		if (len == (sizeof(line) - 1))
5264 			while (line[(len - 1)] != '\n') {
5265 				ret = fgetc(file);
5266 				if (ret == EOF)
5267 					break;
5268 				line[(len - 1)] = ret;
5269 			}
5270 		/* Extract information. */
5271 		if (sscanf(line,
5272 			   "PCI_SLOT_NAME="
5273 			   "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5274 			   &pci_addr->domain,
5275 			   &pci_addr->bus,
5276 			   &pci_addr->devid,
5277 			   &pci_addr->function) == 4) {
5278 			ret = 0;
5279 			break;
5280 		}
5281 	}
5282 	fclose(file);
5283 	return 0;
5284 }
5285 
5286 /**
5287  * Get MAC address by querying netdevice.
5288  *
5289  * @param[in] priv
5290  *   struct priv for the requested device.
5291  * @param[out] mac
5292  *   MAC address output buffer.
5293  *
5294  * @return
5295  *   0 on success, -1 on failure and errno is set.
5296  */
5297 static int
5298 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5299 {
5300 	struct ifreq request;
5301 
5302 	if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5303 		return -1;
5304 	memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5305 	return 0;
5306 }
5307 
5308 /* Support up to 32 adapters. */
5309 static struct {
5310 	struct rte_pci_addr pci_addr; /* associated PCI address */
5311 	uint32_t ports; /* physical ports bitfield. */
5312 } mlx4_dev[32];
5313 
5314 /**
5315  * Get device index in mlx4_dev[] from PCI bus address.
5316  *
5317  * @param[in] pci_addr
5318  *   PCI bus address to look for.
5319  *
5320  * @return
5321  *   mlx4_dev[] index on success, -1 on failure.
5322  */
5323 static int
5324 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
5325 {
5326 	unsigned int i;
5327 	int ret = -1;
5328 
5329 	assert(pci_addr != NULL);
5330 	for (i = 0; (i != elemof(mlx4_dev)); ++i) {
5331 		if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
5332 		    (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
5333 		    (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
5334 		    (mlx4_dev[i].pci_addr.function == pci_addr->function))
5335 			return i;
5336 		if ((mlx4_dev[i].ports == 0) && (ret == -1))
5337 			ret = i;
5338 	}
5339 	return ret;
5340 }
5341 
5342 /**
5343  * Retrieve integer value from environment variable.
5344  *
5345  * @param[in] name
5346  *   Environment variable name.
5347  *
5348  * @return
5349  *   Integer value, 0 if the variable is not set.
5350  */
5351 static int
5352 mlx4_getenv_int(const char *name)
5353 {
5354 	const char *val = getenv(name);
5355 
5356 	if (val == NULL)
5357 		return 0;
5358 	return atoi(val);
5359 }
5360 
5361 static void
5362 mlx4_dev_link_status_handler(void *);
5363 static void
5364 mlx4_dev_interrupt_handler(struct rte_intr_handle *, void *);
5365 
5366 /**
5367  * Link status handler.
5368  *
5369  * @param priv
5370  *   Pointer to private structure.
5371  * @param dev
5372  *   Pointer to the rte_eth_dev structure.
5373  *
5374  * @return
5375  *   Nonzero if the callback process can be called immediately.
5376  */
5377 static int
5378 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
5379 {
5380 	struct ibv_async_event event;
5381 	int port_change = 0;
5382 	int ret = 0;
5383 
5384 	/* Read all message and acknowledge them. */
5385 	for (;;) {
5386 		if (ibv_get_async_event(priv->ctx, &event))
5387 			break;
5388 
5389 		if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
5390 		    event.event_type == IBV_EVENT_PORT_ERR)
5391 			port_change = 1;
5392 		else
5393 			DEBUG("event type %d on port %d not handled",
5394 			      event.event_type, event.element.port_num);
5395 		ibv_ack_async_event(&event);
5396 	}
5397 
5398 	if (port_change ^ priv->pending_alarm) {
5399 		struct rte_eth_link *link = &dev->data->dev_link;
5400 
5401 		priv->pending_alarm = 0;
5402 		mlx4_link_update(dev, 0);
5403 		if (((link->link_speed == 0) && link->link_status) ||
5404 		    ((link->link_speed != 0) && !link->link_status)) {
5405 			/* Inconsistent status, check again later. */
5406 			priv->pending_alarm = 1;
5407 			rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5408 					  mlx4_dev_link_status_handler,
5409 					  dev);
5410 		} else
5411 			ret = 1;
5412 	}
5413 	return ret;
5414 }
5415 
5416 /**
5417  * Handle delayed link status event.
5418  *
5419  * @param arg
5420  *   Registered argument.
5421  */
5422 static void
5423 mlx4_dev_link_status_handler(void *arg)
5424 {
5425 	struct rte_eth_dev *dev = arg;
5426 	struct priv *priv = dev->data->dev_private;
5427 	int ret;
5428 
5429 	priv_lock(priv);
5430 	assert(priv->pending_alarm == 1);
5431 	ret = priv_dev_link_status_handler(priv, dev);
5432 	priv_unlock(priv);
5433 	if (ret)
5434 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5435 }
5436 
5437 /**
5438  * Handle interrupts from the NIC.
5439  *
5440  * @param[in] intr_handle
5441  *   Interrupt handler.
5442  * @param cb_arg
5443  *   Callback argument.
5444  */
5445 static void
5446 mlx4_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
5447 {
5448 	struct rte_eth_dev *dev = cb_arg;
5449 	struct priv *priv = dev->data->dev_private;
5450 	int ret;
5451 
5452 	(void)intr_handle;
5453 	priv_lock(priv);
5454 	ret = priv_dev_link_status_handler(priv, dev);
5455 	priv_unlock(priv);
5456 	if (ret)
5457 		_rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5458 }
5459 
5460 /**
5461  * Uninstall interrupt handler.
5462  *
5463  * @param priv
5464  *   Pointer to private structure.
5465  * @param dev
5466  *   Pointer to the rte_eth_dev structure.
5467  */
5468 static void
5469 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5470 {
5471 	if (!dev->data->dev_conf.intr_conf.lsc)
5472 		return;
5473 	rte_intr_callback_unregister(&priv->intr_handle,
5474 				     mlx4_dev_interrupt_handler,
5475 				     dev);
5476 	if (priv->pending_alarm)
5477 		rte_eal_alarm_cancel(mlx4_dev_link_status_handler, dev);
5478 	priv->pending_alarm = 0;
5479 	priv->intr_handle.fd = 0;
5480 	priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5481 }
5482 
5483 /**
5484  * Install interrupt handler.
5485  *
5486  * @param priv
5487  *   Pointer to private structure.
5488  * @param dev
5489  *   Pointer to the rte_eth_dev structure.
5490  */
5491 static void
5492 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
5493 {
5494 	int rc, flags;
5495 
5496 	if (!dev->data->dev_conf.intr_conf.lsc)
5497 		return;
5498 	assert(priv->ctx->async_fd > 0);
5499 	flags = fcntl(priv->ctx->async_fd, F_GETFL);
5500 	rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5501 	if (rc < 0) {
5502 		INFO("failed to change file descriptor async event queue");
5503 		dev->data->dev_conf.intr_conf.lsc = 0;
5504 	} else {
5505 		priv->intr_handle.fd = priv->ctx->async_fd;
5506 		priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5507 		rte_intr_callback_register(&priv->intr_handle,
5508 					   mlx4_dev_interrupt_handler,
5509 					   dev);
5510 	}
5511 }
5512 
5513 static struct eth_driver mlx4_driver;
5514 
5515 /**
5516  * DPDK callback to register a PCI device.
5517  *
5518  * This function creates an Ethernet device for each port of a given
5519  * PCI device.
5520  *
5521  * @param[in] pci_drv
5522  *   PCI driver structure (mlx4_driver).
5523  * @param[in] pci_dev
5524  *   PCI device information.
5525  *
5526  * @return
5527  *   0 on success, negative errno value on failure.
5528  */
5529 static int
5530 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5531 {
5532 	struct ibv_device **list;
5533 	struct ibv_device *ibv_dev;
5534 	int err = 0;
5535 	struct ibv_context *attr_ctx = NULL;
5536 	struct ibv_device_attr device_attr;
5537 	unsigned int vf;
5538 	int idx;
5539 	int i;
5540 
5541 	(void)pci_drv;
5542 	assert(pci_drv == &mlx4_driver.pci_drv);
5543 	/* Get mlx4_dev[] index. */
5544 	idx = mlx4_dev_idx(&pci_dev->addr);
5545 	if (idx == -1) {
5546 		ERROR("this driver cannot support any more adapters");
5547 		return -ENOMEM;
5548 	}
5549 	DEBUG("using driver device index %d", idx);
5550 
5551 	/* Save PCI address. */
5552 	mlx4_dev[idx].pci_addr = pci_dev->addr;
5553 	list = ibv_get_device_list(&i);
5554 	if (list == NULL) {
5555 		assert(errno);
5556 		if (errno == ENOSYS) {
5557 			WARN("cannot list devices, is ib_uverbs loaded?");
5558 			return 0;
5559 		}
5560 		return -errno;
5561 	}
5562 	assert(i >= 0);
5563 	/*
5564 	 * For each listed device, check related sysfs entry against
5565 	 * the provided PCI ID.
5566 	 */
5567 	while (i != 0) {
5568 		struct rte_pci_addr pci_addr;
5569 
5570 		--i;
5571 		DEBUG("checking device \"%s\"", list[i]->name);
5572 		if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5573 			continue;
5574 		if ((pci_dev->addr.domain != pci_addr.domain) ||
5575 		    (pci_dev->addr.bus != pci_addr.bus) ||
5576 		    (pci_dev->addr.devid != pci_addr.devid) ||
5577 		    (pci_dev->addr.function != pci_addr.function))
5578 			continue;
5579 		vf = (pci_dev->id.device_id ==
5580 		      PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5581 		INFO("PCI information matches, using device \"%s\" (VF: %s)",
5582 		     list[i]->name, (vf ? "true" : "false"));
5583 		attr_ctx = ibv_open_device(list[i]);
5584 		err = errno;
5585 		break;
5586 	}
5587 	if (attr_ctx == NULL) {
5588 		ibv_free_device_list(list);
5589 		switch (err) {
5590 		case 0:
5591 			WARN("cannot access device, is mlx4_ib loaded?");
5592 			return 0;
5593 		case EINVAL:
5594 			WARN("cannot use device, are drivers up to date?");
5595 			return 0;
5596 		}
5597 		assert(err > 0);
5598 		return -err;
5599 	}
5600 	ibv_dev = list[i];
5601 
5602 	DEBUG("device opened");
5603 	if (ibv_query_device(attr_ctx, &device_attr))
5604 		goto error;
5605 	INFO("%u port(s) detected", device_attr.phys_port_cnt);
5606 
5607 	for (i = 0; i < device_attr.phys_port_cnt; i++) {
5608 		uint32_t port = i + 1; /* ports are indexed from one */
5609 		uint32_t test = (1 << i);
5610 		struct ibv_context *ctx = NULL;
5611 		struct ibv_port_attr port_attr;
5612 		struct ibv_pd *pd = NULL;
5613 		struct priv *priv = NULL;
5614 		struct rte_eth_dev *eth_dev = NULL;
5615 #ifdef HAVE_EXP_QUERY_DEVICE
5616 		struct ibv_exp_device_attr exp_device_attr;
5617 #endif /* HAVE_EXP_QUERY_DEVICE */
5618 		struct ether_addr mac;
5619 
5620 #ifdef HAVE_EXP_QUERY_DEVICE
5621 		exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
5622 #ifdef RSS_SUPPORT
5623 		exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
5624 #endif /* RSS_SUPPORT */
5625 #endif /* HAVE_EXP_QUERY_DEVICE */
5626 
5627 		DEBUG("using port %u (%08" PRIx32 ")", port, test);
5628 
5629 		ctx = ibv_open_device(ibv_dev);
5630 		if (ctx == NULL)
5631 			goto port_error;
5632 
5633 		/* Check port status. */
5634 		err = ibv_query_port(ctx, port, &port_attr);
5635 		if (err) {
5636 			ERROR("port query failed: %s", strerror(err));
5637 			goto port_error;
5638 		}
5639 
5640 		if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
5641 			ERROR("port %d is not configured in Ethernet mode",
5642 			      port);
5643 			goto port_error;
5644 		}
5645 
5646 		if (port_attr.state != IBV_PORT_ACTIVE)
5647 			DEBUG("port %d is not active: \"%s\" (%d)",
5648 			      port, ibv_port_state_str(port_attr.state),
5649 			      port_attr.state);
5650 
5651 		/* Allocate protection domain. */
5652 		pd = ibv_alloc_pd(ctx);
5653 		if (pd == NULL) {
5654 			ERROR("PD allocation failure");
5655 			err = ENOMEM;
5656 			goto port_error;
5657 		}
5658 
5659 		mlx4_dev[idx].ports |= test;
5660 
5661 		/* from rte_ethdev.c */
5662 		priv = rte_zmalloc("ethdev private structure",
5663 				   sizeof(*priv),
5664 				   RTE_CACHE_LINE_SIZE);
5665 		if (priv == NULL) {
5666 			ERROR("priv allocation failure");
5667 			err = ENOMEM;
5668 			goto port_error;
5669 		}
5670 
5671 		priv->ctx = ctx;
5672 		priv->device_attr = device_attr;
5673 		priv->port = port;
5674 		priv->pd = pd;
5675 		priv->mtu = ETHER_MTU;
5676 #ifdef HAVE_EXP_QUERY_DEVICE
5677 		if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5678 			ERROR("ibv_exp_query_device() failed");
5679 			goto port_error;
5680 		}
5681 #ifdef RSS_SUPPORT
5682 		if ((exp_device_attr.exp_device_cap_flags &
5683 		     IBV_EXP_DEVICE_QPG) &&
5684 		    (exp_device_attr.exp_device_cap_flags &
5685 		     IBV_EXP_DEVICE_UD_RSS) &&
5686 		    (exp_device_attr.comp_mask &
5687 		     IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
5688 		    (exp_device_attr.max_rss_tbl_sz > 0)) {
5689 			priv->hw_qpg = 1;
5690 			priv->hw_rss = 1;
5691 			priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
5692 		} else {
5693 			priv->hw_qpg = 0;
5694 			priv->hw_rss = 0;
5695 			priv->max_rss_tbl_sz = 0;
5696 		}
5697 		priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
5698 				  IBV_EXP_DEVICE_UD_TSS);
5699 		DEBUG("device flags: %s%s%s",
5700 		      (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
5701 		      (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
5702 		      (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
5703 		if (priv->hw_rss)
5704 			DEBUG("maximum RSS indirection table size: %u",
5705 			      exp_device_attr.max_rss_tbl_sz);
5706 #endif /* RSS_SUPPORT */
5707 
5708 		priv->hw_csum =
5709 			((exp_device_attr.exp_device_cap_flags &
5710 			  IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
5711 			 (exp_device_attr.exp_device_cap_flags &
5712 			  IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
5713 		DEBUG("checksum offloading is %ssupported",
5714 		      (priv->hw_csum ? "" : "not "));
5715 
5716 		priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
5717 					 IBV_EXP_DEVICE_VXLAN_SUPPORT);
5718 		DEBUG("L2 tunnel checksum offloads are %ssupported",
5719 		      (priv->hw_csum_l2tun ? "" : "not "));
5720 
5721 #ifdef INLINE_RECV
5722 		priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
5723 
5724 		if (priv->inl_recv_size) {
5725 			exp_device_attr.comp_mask =
5726 				IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
5727 			if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5728 				INFO("Couldn't query device for inline-receive"
5729 				     " capabilities.");
5730 				priv->inl_recv_size = 0;
5731 			} else {
5732 				if ((unsigned)exp_device_attr.inline_recv_sz <
5733 				    priv->inl_recv_size) {
5734 					INFO("Max inline-receive (%d) <"
5735 					     " requested inline-receive (%u)",
5736 					     exp_device_attr.inline_recv_sz,
5737 					     priv->inl_recv_size);
5738 					priv->inl_recv_size =
5739 						exp_device_attr.inline_recv_sz;
5740 				}
5741 			}
5742 			INFO("Set inline receive size to %u",
5743 			     priv->inl_recv_size);
5744 		}
5745 #endif /* INLINE_RECV */
5746 #endif /* HAVE_EXP_QUERY_DEVICE */
5747 
5748 		(void)mlx4_getenv_int;
5749 		priv->vf = vf;
5750 		/* Configure the first MAC address by default. */
5751 		if (priv_get_mac(priv, &mac.addr_bytes)) {
5752 			ERROR("cannot get MAC address, is mlx4_en loaded?"
5753 			      " (errno: %s)", strerror(errno));
5754 			goto port_error;
5755 		}
5756 		INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
5757 		     priv->port,
5758 		     mac.addr_bytes[0], mac.addr_bytes[1],
5759 		     mac.addr_bytes[2], mac.addr_bytes[3],
5760 		     mac.addr_bytes[4], mac.addr_bytes[5]);
5761 		/* Register MAC and broadcast addresses. */
5762 		claim_zero(priv_mac_addr_add(priv, 0,
5763 					     (const uint8_t (*)[ETHER_ADDR_LEN])
5764 					     mac.addr_bytes));
5765 		claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
5766 					     &(const uint8_t [ETHER_ADDR_LEN])
5767 					     { "\xff\xff\xff\xff\xff\xff" }));
5768 #ifndef NDEBUG
5769 		{
5770 			char ifname[IF_NAMESIZE];
5771 
5772 			if (priv_get_ifname(priv, &ifname) == 0)
5773 				DEBUG("port %u ifname is \"%s\"",
5774 				      priv->port, ifname);
5775 			else
5776 				DEBUG("port %u ifname is unknown", priv->port);
5777 		}
5778 #endif
5779 		/* Get actual MTU if possible. */
5780 		priv_get_mtu(priv, &priv->mtu);
5781 		DEBUG("port %u MTU is %u", priv->port, priv->mtu);
5782 
5783 		/* from rte_ethdev.c */
5784 		{
5785 			char name[RTE_ETH_NAME_MAX_LEN];
5786 
5787 			snprintf(name, sizeof(name), "%s port %u",
5788 				 ibv_get_device_name(ibv_dev), port);
5789 			eth_dev = rte_eth_dev_allocate(name);
5790 		}
5791 		if (eth_dev == NULL) {
5792 			ERROR("can not allocate rte ethdev");
5793 			err = ENOMEM;
5794 			goto port_error;
5795 		}
5796 
5797 		/* Secondary processes have to use local storage for their
5798 		 * private data as well as a copy of eth_dev->data, but this
5799 		 * pointer must not be modified before burst functions are
5800 		 * actually called. */
5801 		if (mlx4_is_secondary()) {
5802 			struct mlx4_secondary_data *sd =
5803 				&mlx4_secondary_data[eth_dev->data->port_id];
5804 
5805 			sd->primary_priv = eth_dev->data->dev_private;
5806 			if (sd->primary_priv == NULL) {
5807 				ERROR("no private data for port %u",
5808 				      eth_dev->data->port_id);
5809 				err = EINVAL;
5810 				goto port_error;
5811 			}
5812 			sd->shared_dev_data = eth_dev->data;
5813 			rte_spinlock_init(&sd->lock);
5814 			memcpy(sd->data.name, sd->shared_dev_data->name,
5815 			       sizeof(sd->data.name));
5816 			sd->data.dev_private = priv;
5817 			sd->data.rx_mbuf_alloc_failed = 0;
5818 			sd->data.mtu = ETHER_MTU;
5819 			sd->data.port_id = sd->shared_dev_data->port_id;
5820 			sd->data.mac_addrs = priv->mac;
5821 			eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
5822 			eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
5823 		} else {
5824 			eth_dev->data->dev_private = priv;
5825 			eth_dev->data->mac_addrs = priv->mac;
5826 		}
5827 		eth_dev->device = &pci_dev->device;
5828 
5829 		rte_eth_copy_pci_info(eth_dev, pci_dev);
5830 
5831 		eth_dev->driver = &mlx4_driver;
5832 
5833 		priv->dev = eth_dev;
5834 		eth_dev->dev_ops = &mlx4_dev_ops;
5835 
5836 		/* Bring Ethernet device up. */
5837 		DEBUG("forcing Ethernet interface up");
5838 		priv_set_flags(priv, ~IFF_UP, IFF_UP);
5839 		continue;
5840 
5841 port_error:
5842 		rte_free(priv);
5843 		if (pd)
5844 			claim_zero(ibv_dealloc_pd(pd));
5845 		if (ctx)
5846 			claim_zero(ibv_close_device(ctx));
5847 		if (eth_dev)
5848 			rte_eth_dev_release_port(eth_dev);
5849 		break;
5850 	}
5851 
5852 	/*
5853 	 * XXX if something went wrong in the loop above, there is a resource
5854 	 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
5855 	 * long as the dpdk does not provide a way to deallocate a ethdev and a
5856 	 * way to enumerate the registered ethdevs to free the previous ones.
5857 	 */
5858 
5859 	/* no port found, complain */
5860 	if (!mlx4_dev[idx].ports) {
5861 		err = ENODEV;
5862 		goto error;
5863 	}
5864 
5865 error:
5866 	if (attr_ctx)
5867 		claim_zero(ibv_close_device(attr_ctx));
5868 	if (list)
5869 		ibv_free_device_list(list);
5870 	assert(err >= 0);
5871 	return -err;
5872 }
5873 
5874 static const struct rte_pci_id mlx4_pci_id_map[] = {
5875 	{
5876 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5877 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3)
5878 	},
5879 	{
5880 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5881 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
5882 	},
5883 	{
5884 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5885 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
5886 	},
5887 	{
5888 		.vendor_id = 0
5889 	}
5890 };
5891 
5892 static struct eth_driver mlx4_driver = {
5893 	.pci_drv = {
5894 		.driver = {
5895 			.name = MLX4_DRIVER_NAME
5896 		},
5897 		.id_table = mlx4_pci_id_map,
5898 		.probe = mlx4_pci_probe,
5899 		.drv_flags = RTE_PCI_DRV_INTR_LSC,
5900 	},
5901 	.dev_private_size = sizeof(struct priv)
5902 };
5903 
5904 /**
5905  * Driver initialization routine.
5906  */
5907 RTE_INIT(rte_mlx4_pmd_init);
5908 static void
5909 rte_mlx4_pmd_init(void)
5910 {
5911 	RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
5912 	/*
5913 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
5914 	 * huge pages. Calling ibv_fork_init() during init allows
5915 	 * applications to use fork() safely for purposes other than
5916 	 * using this PMD, which is not supported in forked processes.
5917 	 */
5918 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
5919 	ibv_fork_init();
5920 	rte_eal_pci_register(&mlx4_driver.pci_drv);
5921 }
5922 
5923 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
5924 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
5925 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
5926 	"* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");
5927