xref: /dpdk/drivers/net/ngbe/ngbe_rxtx.c (revision 7917b0d38e92e8b9ec5a870415b791420e10f11a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3  * Copyright(c) 2010-2017 Intel Corporation
4  */
5 
6 #include <sys/queue.h>
7 
8 #include <stdint.h>
9 #include <rte_ethdev.h>
10 #include <ethdev_driver.h>
11 #include <rte_malloc.h>
12 #include <rte_net.h>
13 #include <rte_vect.h>
14 
15 #include "ngbe_logs.h"
16 #include "base/ngbe.h"
17 #include "ngbe_ethdev.h"
18 #include "ngbe_rxtx.h"
19 
20 #ifdef RTE_LIBRTE_IEEE1588
21 #define NGBE_TX_IEEE1588_TMST RTE_MBUF_F_TX_IEEE1588_TMST
22 #else
23 #define NGBE_TX_IEEE1588_TMST 0
24 #endif
25 
26 /* Bit Mask to indicate what bits required for building Tx context */
27 static const u64 NGBE_TX_OFFLOAD_MASK = (RTE_MBUF_F_TX_IP_CKSUM |
28 		RTE_MBUF_F_TX_IPV6 |
29 		RTE_MBUF_F_TX_IPV4 |
30 		RTE_MBUF_F_TX_VLAN |
31 		RTE_MBUF_F_TX_L4_MASK |
32 		RTE_MBUF_F_TX_TCP_SEG |
33 		NGBE_TX_IEEE1588_TMST);
34 
35 #define NGBE_TX_OFFLOAD_NOTSUP_MASK \
36 		(RTE_MBUF_F_TX_OFFLOAD_MASK ^ NGBE_TX_OFFLOAD_MASK)
37 
38 /*
39  * Prefetch a cache line into all cache levels.
40  */
41 #define rte_ngbe_prefetch(p)   rte_prefetch0(p)
42 
43 /*********************************************************************
44  *
45  *  Tx functions
46  *
47  **********************************************************************/
48 
49 /*
50  * Check for descriptors with their DD bit set and free mbufs.
51  * Return the total number of buffers freed.
52  */
53 static __rte_always_inline int
54 ngbe_tx_free_bufs(struct ngbe_tx_queue *txq)
55 {
56 	struct ngbe_tx_entry *txep;
57 	uint32_t status;
58 	int i, nb_free = 0;
59 	struct rte_mbuf *m, *free[RTE_NGBE_TX_MAX_FREE_BUF_SZ];
60 
61 	/* check DD bit on threshold descriptor */
62 	status = txq->tx_ring[txq->tx_next_dd].dw3;
63 	if (!(status & rte_cpu_to_le_32(NGBE_TXD_DD))) {
64 		if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
65 			ngbe_set32_masked(txq->tdc_reg_addr,
66 				NGBE_TXCFG_FLUSH, NGBE_TXCFG_FLUSH);
67 		return 0;
68 	}
69 
70 	/*
71 	 * first buffer to free from S/W ring is at index
72 	 * tx_next_dd - (tx_free_thresh-1)
73 	 */
74 	txep = &txq->sw_ring[txq->tx_next_dd - (txq->tx_free_thresh - 1)];
75 	for (i = 0; i < txq->tx_free_thresh; ++i, ++txep) {
76 		/* free buffers one at a time */
77 		m = rte_pktmbuf_prefree_seg(txep->mbuf);
78 		txep->mbuf = NULL;
79 
80 		if (unlikely(m == NULL))
81 			continue;
82 
83 		if (nb_free >= RTE_NGBE_TX_MAX_FREE_BUF_SZ ||
84 		    (nb_free > 0 && m->pool != free[0]->pool)) {
85 			rte_mempool_put_bulk(free[0]->pool,
86 					     (void **)free, nb_free);
87 			nb_free = 0;
88 		}
89 
90 		free[nb_free++] = m;
91 	}
92 
93 	if (nb_free > 0)
94 		rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
95 
96 	/* buffers were freed, update counters */
97 	txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_free_thresh);
98 	txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_free_thresh);
99 	if (txq->tx_next_dd >= txq->nb_tx_desc)
100 		txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
101 
102 	return txq->tx_free_thresh;
103 }
104 
105 /* Populate 4 descriptors with data from 4 mbufs */
106 static inline void
107 tx4(volatile struct ngbe_tx_desc *txdp, struct rte_mbuf **pkts)
108 {
109 	uint64_t buf_dma_addr;
110 	uint32_t pkt_len;
111 	int i;
112 
113 	for (i = 0; i < 4; ++i, ++txdp, ++pkts) {
114 		buf_dma_addr = rte_mbuf_data_iova(*pkts);
115 		pkt_len = (*pkts)->data_len;
116 
117 		/* write data to descriptor */
118 		txdp->qw0 = rte_cpu_to_le_64(buf_dma_addr);
119 		txdp->dw2 = cpu_to_le32(NGBE_TXD_FLAGS |
120 					NGBE_TXD_DATLEN(pkt_len));
121 		txdp->dw3 = cpu_to_le32(NGBE_TXD_PAYLEN(pkt_len));
122 
123 		rte_prefetch0(&(*pkts)->pool);
124 	}
125 }
126 
127 /* Populate 1 descriptor with data from 1 mbuf */
128 static inline void
129 tx1(volatile struct ngbe_tx_desc *txdp, struct rte_mbuf **pkts)
130 {
131 	uint64_t buf_dma_addr;
132 	uint32_t pkt_len;
133 
134 	buf_dma_addr = rte_mbuf_data_iova(*pkts);
135 	pkt_len = (*pkts)->data_len;
136 
137 	/* write data to descriptor */
138 	txdp->qw0 = cpu_to_le64(buf_dma_addr);
139 	txdp->dw2 = cpu_to_le32(NGBE_TXD_FLAGS |
140 				NGBE_TXD_DATLEN(pkt_len));
141 	txdp->dw3 = cpu_to_le32(NGBE_TXD_PAYLEN(pkt_len));
142 
143 	rte_prefetch0(&(*pkts)->pool);
144 }
145 
146 /*
147  * Fill H/W descriptor ring with mbuf data.
148  * Copy mbuf pointers to the S/W ring.
149  */
150 static inline void
151 ngbe_tx_fill_hw_ring(struct ngbe_tx_queue *txq, struct rte_mbuf **pkts,
152 		      uint16_t nb_pkts)
153 {
154 	volatile struct ngbe_tx_desc *txdp = &txq->tx_ring[txq->tx_tail];
155 	struct ngbe_tx_entry *txep = &txq->sw_ring[txq->tx_tail];
156 	const int N_PER_LOOP = 4;
157 	const int N_PER_LOOP_MASK = N_PER_LOOP - 1;
158 	int mainpart, leftover;
159 	int i, j;
160 
161 	/*
162 	 * Process most of the packets in chunks of N pkts.  Any
163 	 * leftover packets will get processed one at a time.
164 	 */
165 	mainpart = (nb_pkts & ((uint32_t)~N_PER_LOOP_MASK));
166 	leftover = (nb_pkts & ((uint32_t)N_PER_LOOP_MASK));
167 	for (i = 0; i < mainpart; i += N_PER_LOOP) {
168 		/* Copy N mbuf pointers to the S/W ring */
169 		for (j = 0; j < N_PER_LOOP; ++j)
170 			(txep + i + j)->mbuf = *(pkts + i + j);
171 		tx4(txdp + i, pkts + i);
172 	}
173 
174 	if (unlikely(leftover > 0)) {
175 		for (i = 0; i < leftover; ++i) {
176 			(txep + mainpart + i)->mbuf = *(pkts + mainpart + i);
177 			tx1(txdp + mainpart + i, pkts + mainpart + i);
178 		}
179 	}
180 }
181 
182 static inline uint16_t
183 tx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
184 	     uint16_t nb_pkts)
185 {
186 	struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
187 	uint16_t n = 0;
188 
189 	/*
190 	 * Begin scanning the H/W ring for done descriptors when the
191 	 * number of available descriptors drops below tx_free_thresh.
192 	 * For each done descriptor, free the associated buffer.
193 	 */
194 	if (txq->nb_tx_free < txq->tx_free_thresh)
195 		ngbe_tx_free_bufs(txq);
196 
197 	/* Only use descriptors that are available */
198 	nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
199 	if (unlikely(nb_pkts == 0))
200 		return 0;
201 
202 	/* Use exactly nb_pkts descriptors */
203 	txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
204 
205 	/*
206 	 * At this point, we know there are enough descriptors in the
207 	 * ring to transmit all the packets.  This assumes that each
208 	 * mbuf contains a single segment, and that no new offloads
209 	 * are expected, which would require a new context descriptor.
210 	 */
211 
212 	/*
213 	 * See if we're going to wrap-around. If so, handle the top
214 	 * of the descriptor ring first, then do the bottom.  If not,
215 	 * the processing looks just like the "bottom" part anyway...
216 	 */
217 	if ((txq->tx_tail + nb_pkts) > txq->nb_tx_desc) {
218 		n = (uint16_t)(txq->nb_tx_desc - txq->tx_tail);
219 		ngbe_tx_fill_hw_ring(txq, tx_pkts, n);
220 		txq->tx_tail = 0;
221 	}
222 
223 	/* Fill H/W descriptor ring with mbuf data */
224 	ngbe_tx_fill_hw_ring(txq, tx_pkts + n, (uint16_t)(nb_pkts - n));
225 	txq->tx_tail = (uint16_t)(txq->tx_tail + (nb_pkts - n));
226 
227 	/*
228 	 * Check for wrap-around. This would only happen if we used
229 	 * up to the last descriptor in the ring, no more, no less.
230 	 */
231 	if (txq->tx_tail >= txq->nb_tx_desc)
232 		txq->tx_tail = 0;
233 
234 	PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
235 		   (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
236 		   (uint16_t)txq->tx_tail, (uint16_t)nb_pkts);
237 
238 	/* update tail pointer */
239 	rte_wmb();
240 	ngbe_set32_relaxed(txq->tdt_reg_addr, txq->tx_tail);
241 
242 	return nb_pkts;
243 }
244 
245 uint16_t
246 ngbe_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
247 		       uint16_t nb_pkts)
248 {
249 	uint16_t nb_tx;
250 
251 	/* Try to transmit at least chunks of TX_MAX_BURST pkts */
252 	if (likely(nb_pkts <= RTE_PMD_NGBE_TX_MAX_BURST))
253 		return tx_xmit_pkts(tx_queue, tx_pkts, nb_pkts);
254 
255 	/* transmit more than the max burst, in chunks of TX_MAX_BURST */
256 	nb_tx = 0;
257 	while (nb_pkts != 0) {
258 		uint16_t ret, n;
259 
260 		n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_NGBE_TX_MAX_BURST);
261 		ret = tx_xmit_pkts(tx_queue, &tx_pkts[nb_tx], n);
262 		nb_tx = (uint16_t)(nb_tx + ret);
263 		nb_pkts = (uint16_t)(nb_pkts - ret);
264 		if (ret < n)
265 			break;
266 	}
267 
268 	return nb_tx;
269 }
270 
271 static uint16_t
272 ngbe_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
273 		   uint16_t nb_pkts)
274 {
275 	struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
276 	uint16_t nb_tx = 0;
277 
278 	while (nb_pkts) {
279 		uint16_t ret, num;
280 
281 		num = (uint16_t)RTE_MIN(nb_pkts, txq->tx_free_thresh);
282 		ret = ngbe_xmit_fixed_burst_vec(tx_queue, &tx_pkts[nb_tx], num);
283 		nb_tx += ret;
284 		nb_pkts -= ret;
285 		if (ret < num)
286 			break;
287 	}
288 
289 	return nb_tx;
290 }
291 
292 static inline void
293 ngbe_set_xmit_ctx(struct ngbe_tx_queue *txq,
294 		volatile struct ngbe_tx_ctx_desc *ctx_txd,
295 		uint64_t ol_flags, union ngbe_tx_offload tx_offload)
296 {
297 	union ngbe_tx_offload tx_offload_mask;
298 	uint32_t type_tucmd_mlhl;
299 	uint32_t mss_l4len_idx;
300 	uint32_t ctx_idx;
301 	uint32_t vlan_macip_lens;
302 	uint32_t tunnel_seed;
303 
304 	ctx_idx = txq->ctx_curr;
305 	tx_offload_mask.data[0] = 0;
306 	tx_offload_mask.data[1] = 0;
307 
308 	/* Specify which HW CTX to upload. */
309 	mss_l4len_idx = NGBE_TXD_IDX(ctx_idx);
310 	type_tucmd_mlhl = NGBE_TXD_CTXT;
311 
312 	tx_offload_mask.ptid |= ~0;
313 	type_tucmd_mlhl |= NGBE_TXD_PTID(tx_offload.ptid);
314 
315 	/* check if TCP segmentation required for this packet */
316 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
317 		tx_offload_mask.l2_len |= ~0;
318 		tx_offload_mask.l3_len |= ~0;
319 		tx_offload_mask.l4_len |= ~0;
320 		tx_offload_mask.tso_segsz |= ~0;
321 		mss_l4len_idx |= NGBE_TXD_MSS(tx_offload.tso_segsz);
322 		mss_l4len_idx |= NGBE_TXD_L4LEN(tx_offload.l4_len);
323 	} else { /* no TSO, check if hardware checksum is needed */
324 		if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
325 			tx_offload_mask.l2_len |= ~0;
326 			tx_offload_mask.l3_len |= ~0;
327 		}
328 
329 		switch (ol_flags & RTE_MBUF_F_TX_L4_MASK) {
330 		case RTE_MBUF_F_TX_UDP_CKSUM:
331 			mss_l4len_idx |=
332 				NGBE_TXD_L4LEN(sizeof(struct rte_udp_hdr));
333 			tx_offload_mask.l2_len |= ~0;
334 			tx_offload_mask.l3_len |= ~0;
335 			break;
336 		case RTE_MBUF_F_TX_TCP_CKSUM:
337 			mss_l4len_idx |=
338 				NGBE_TXD_L4LEN(sizeof(struct rte_tcp_hdr));
339 			tx_offload_mask.l2_len |= ~0;
340 			tx_offload_mask.l3_len |= ~0;
341 			break;
342 		case RTE_MBUF_F_TX_SCTP_CKSUM:
343 			mss_l4len_idx |=
344 				NGBE_TXD_L4LEN(sizeof(struct rte_sctp_hdr));
345 			tx_offload_mask.l2_len |= ~0;
346 			tx_offload_mask.l3_len |= ~0;
347 			break;
348 		default:
349 			break;
350 		}
351 	}
352 
353 	vlan_macip_lens = NGBE_TXD_IPLEN(tx_offload.l3_len >> 1);
354 	vlan_macip_lens |= NGBE_TXD_MACLEN(tx_offload.l2_len);
355 
356 	if (ol_flags & RTE_MBUF_F_TX_VLAN) {
357 		tx_offload_mask.vlan_tci |= ~0;
358 		vlan_macip_lens |= NGBE_TXD_VLAN(tx_offload.vlan_tci);
359 	}
360 
361 	tunnel_seed = 0;
362 
363 	txq->ctx_cache[ctx_idx].flags = ol_flags;
364 	txq->ctx_cache[ctx_idx].tx_offload.data[0] =
365 		tx_offload_mask.data[0] & tx_offload.data[0];
366 	txq->ctx_cache[ctx_idx].tx_offload.data[1] =
367 		tx_offload_mask.data[1] & tx_offload.data[1];
368 	txq->ctx_cache[ctx_idx].tx_offload_mask = tx_offload_mask;
369 
370 	ctx_txd->dw0 = rte_cpu_to_le_32(vlan_macip_lens);
371 	ctx_txd->dw1 = rte_cpu_to_le_32(tunnel_seed);
372 	ctx_txd->dw2 = rte_cpu_to_le_32(type_tucmd_mlhl);
373 	ctx_txd->dw3 = rte_cpu_to_le_32(mss_l4len_idx);
374 }
375 
376 /*
377  * Check which hardware context can be used. Use the existing match
378  * or create a new context descriptor.
379  */
380 static inline uint32_t
381 what_ctx_update(struct ngbe_tx_queue *txq, uint64_t flags,
382 		   union ngbe_tx_offload tx_offload)
383 {
384 	/* If match with the current used context */
385 	if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
386 		   (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
387 		    (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
388 		     & tx_offload.data[0])) &&
389 		   (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
390 		    (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
391 		     & tx_offload.data[1]))))
392 		return txq->ctx_curr;
393 
394 	/* What if match with the next context  */
395 	txq->ctx_curr ^= 1;
396 	if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
397 		   (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
398 		    (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
399 		     & tx_offload.data[0])) &&
400 		   (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
401 		    (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
402 		     & tx_offload.data[1]))))
403 		return txq->ctx_curr;
404 
405 	/* Mismatch, use the previous context */
406 	return NGBE_CTX_NUM;
407 }
408 
409 static inline uint32_t
410 tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)
411 {
412 	uint32_t tmp = 0;
413 
414 	if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) != RTE_MBUF_F_TX_L4_NO_CKSUM) {
415 		tmp |= NGBE_TXD_CC;
416 		tmp |= NGBE_TXD_L4CS;
417 	}
418 	if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
419 		tmp |= NGBE_TXD_CC;
420 		tmp |= NGBE_TXD_IPCS;
421 	}
422 	if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) {
423 		tmp |= NGBE_TXD_CC;
424 		tmp |= NGBE_TXD_EIPCS;
425 	}
426 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
427 		tmp |= NGBE_TXD_CC;
428 		/* implies IPv4 cksum */
429 		if (ol_flags & RTE_MBUF_F_TX_IPV4)
430 			tmp |= NGBE_TXD_IPCS;
431 		tmp |= NGBE_TXD_L4CS;
432 	}
433 	if (ol_flags & RTE_MBUF_F_TX_VLAN)
434 		tmp |= NGBE_TXD_CC;
435 
436 	return tmp;
437 }
438 
439 static inline uint32_t
440 tx_desc_ol_flags_to_cmdtype(uint64_t ol_flags)
441 {
442 	uint32_t cmdtype = 0;
443 
444 	if (ol_flags & RTE_MBUF_F_TX_VLAN)
445 		cmdtype |= NGBE_TXD_VLE;
446 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
447 		cmdtype |= NGBE_TXD_TSE;
448 	return cmdtype;
449 }
450 
451 static inline uint32_t
452 tx_desc_ol_flags_to_ptype(uint64_t oflags)
453 {
454 	uint32_t ptype;
455 
456 	/* L2 level */
457 	ptype = RTE_PTYPE_L2_ETHER;
458 	if (oflags & RTE_MBUF_F_TX_VLAN)
459 		ptype |= RTE_PTYPE_L2_ETHER_VLAN;
460 
461 	/* L3 level */
462 	if (oflags & (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM))
463 		ptype |= RTE_PTYPE_L3_IPV4;
464 	else if (oflags & (RTE_MBUF_F_TX_IPV6))
465 		ptype |= RTE_PTYPE_L3_IPV6;
466 
467 	/* L4 level */
468 	switch (oflags & (RTE_MBUF_F_TX_L4_MASK)) {
469 	case RTE_MBUF_F_TX_TCP_CKSUM:
470 		ptype |= RTE_PTYPE_L4_TCP;
471 		break;
472 	case RTE_MBUF_F_TX_UDP_CKSUM:
473 		ptype |= RTE_PTYPE_L4_UDP;
474 		break;
475 	case RTE_MBUF_F_TX_SCTP_CKSUM:
476 		ptype |= RTE_PTYPE_L4_SCTP;
477 		break;
478 	}
479 
480 	if (oflags & RTE_MBUF_F_TX_TCP_SEG)
481 		ptype |= RTE_PTYPE_L4_TCP;
482 
483 	return ptype;
484 }
485 
486 static inline uint8_t
487 tx_desc_ol_flags_to_ptid(uint64_t oflags)
488 {
489 	uint32_t ptype;
490 
491 	ptype = tx_desc_ol_flags_to_ptype(oflags);
492 
493 	return ngbe_encode_ptype(ptype);
494 }
495 
496 /* Reset transmit descriptors after they have been used */
497 static inline int
498 ngbe_xmit_cleanup(struct ngbe_tx_queue *txq)
499 {
500 	struct ngbe_tx_entry *sw_ring = txq->sw_ring;
501 	volatile struct ngbe_tx_desc *txr = txq->tx_ring;
502 	uint16_t last_desc_cleaned = txq->last_desc_cleaned;
503 	uint16_t nb_tx_desc = txq->nb_tx_desc;
504 	uint16_t desc_to_clean_to;
505 	uint16_t nb_tx_to_clean;
506 	uint32_t status;
507 
508 	/* Determine the last descriptor needing to be cleaned */
509 	desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_free_thresh);
510 	if (desc_to_clean_to >= nb_tx_desc)
511 		desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
512 
513 	/* Check to make sure the last descriptor to clean is done */
514 	desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
515 	status = txr[desc_to_clean_to].dw3;
516 	if (!(status & rte_cpu_to_le_32(NGBE_TXD_DD))) {
517 		PMD_TX_LOG(DEBUG,
518 			"Tx descriptor %4u is not done"
519 			"(port=%d queue=%d)",
520 			desc_to_clean_to,
521 			txq->port_id, txq->queue_id);
522 		if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
523 			ngbe_set32_masked(txq->tdc_reg_addr,
524 				NGBE_TXCFG_FLUSH, NGBE_TXCFG_FLUSH);
525 		/* Failed to clean any descriptors, better luck next time */
526 		return -(1);
527 	}
528 
529 	/* Figure out how many descriptors will be cleaned */
530 	if (last_desc_cleaned > desc_to_clean_to)
531 		nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
532 							desc_to_clean_to);
533 	else
534 		nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
535 						last_desc_cleaned);
536 
537 	PMD_TX_LOG(DEBUG,
538 		"Cleaning %4u Tx descriptors: %4u to %4u (port=%d queue=%d)",
539 		nb_tx_to_clean, last_desc_cleaned, desc_to_clean_to,
540 		txq->port_id, txq->queue_id);
541 
542 	/*
543 	 * The last descriptor to clean is done, so that means all the
544 	 * descriptors from the last descriptor that was cleaned
545 	 * up to the last descriptor with the RS bit set
546 	 * are done. Only reset the threshold descriptor.
547 	 */
548 	txr[desc_to_clean_to].dw3 = 0;
549 
550 	/* Update the txq to reflect the last descriptor that was cleaned */
551 	txq->last_desc_cleaned = desc_to_clean_to;
552 	txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
553 
554 	/* No Error */
555 	return 0;
556 }
557 
558 uint16_t
559 ngbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
560 		uint16_t nb_pkts)
561 {
562 	struct ngbe_tx_queue *txq;
563 	struct ngbe_tx_entry *sw_ring;
564 	struct ngbe_tx_entry *txe, *txn;
565 	volatile struct ngbe_tx_desc *txr;
566 	volatile struct ngbe_tx_desc *txd;
567 	struct rte_mbuf     *tx_pkt;
568 	struct rte_mbuf     *m_seg;
569 	uint64_t buf_dma_addr;
570 	uint32_t olinfo_status;
571 	uint32_t cmd_type_len;
572 	uint32_t pkt_len;
573 	uint16_t slen;
574 	uint64_t ol_flags;
575 	uint16_t tx_id;
576 	uint16_t tx_last;
577 	uint16_t nb_tx;
578 	uint16_t nb_used;
579 	uint64_t tx_ol_req;
580 	uint32_t ctx = 0;
581 	uint32_t new_ctx;
582 	union ngbe_tx_offload tx_offload;
583 
584 	tx_offload.data[0] = 0;
585 	tx_offload.data[1] = 0;
586 	txq = tx_queue;
587 	sw_ring = txq->sw_ring;
588 	txr     = txq->tx_ring;
589 	tx_id   = txq->tx_tail;
590 	txe = &sw_ring[tx_id];
591 
592 	/* Determine if the descriptor ring needs to be cleaned. */
593 	if (txq->nb_tx_free < txq->tx_free_thresh)
594 		ngbe_xmit_cleanup(txq);
595 
596 	rte_prefetch0(&txe->mbuf->pool);
597 
598 	/* Tx loop */
599 	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
600 		new_ctx = 0;
601 		tx_pkt = *tx_pkts++;
602 		pkt_len = tx_pkt->pkt_len;
603 
604 		/*
605 		 * Determine how many (if any) context descriptors
606 		 * are needed for offload functionality.
607 		 */
608 		ol_flags = tx_pkt->ol_flags;
609 
610 		/* If hardware offload required */
611 		tx_ol_req = ol_flags & NGBE_TX_OFFLOAD_MASK;
612 		if (tx_ol_req) {
613 			tx_offload.ptid = tx_desc_ol_flags_to_ptid(tx_ol_req);
614 			tx_offload.l2_len = tx_pkt->l2_len;
615 			tx_offload.l3_len = tx_pkt->l3_len;
616 			tx_offload.l4_len = tx_pkt->l4_len;
617 			tx_offload.vlan_tci = tx_pkt->vlan_tci;
618 			tx_offload.tso_segsz = tx_pkt->tso_segsz;
619 
620 			/* If new context need be built or reuse the exist ctx*/
621 			ctx = what_ctx_update(txq, tx_ol_req, tx_offload);
622 			/* Only allocate context descriptor if required */
623 			new_ctx = (ctx == NGBE_CTX_NUM);
624 			ctx = txq->ctx_curr;
625 		}
626 
627 		/*
628 		 * Keep track of how many descriptors are used this loop
629 		 * This will always be the number of segments + the number of
630 		 * Context descriptors required to transmit the packet
631 		 */
632 		nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
633 
634 		/*
635 		 * The number of descriptors that must be allocated for a
636 		 * packet is the number of segments of that packet, plus 1
637 		 * Context Descriptor for the hardware offload, if any.
638 		 * Determine the last Tx descriptor to allocate in the Tx ring
639 		 * for the packet, starting from the current position (tx_id)
640 		 * in the ring.
641 		 */
642 		tx_last = (uint16_t)(tx_id + nb_used - 1);
643 
644 		/* Circular ring */
645 		if (tx_last >= txq->nb_tx_desc)
646 			tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
647 
648 		PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
649 			   " tx_first=%u tx_last=%u",
650 			   (uint16_t)txq->port_id,
651 			   (uint16_t)txq->queue_id,
652 			   (uint32_t)pkt_len,
653 			   (uint16_t)tx_id,
654 			   (uint16_t)tx_last);
655 
656 		/*
657 		 * Make sure there are enough Tx descriptors available to
658 		 * transmit the entire packet.
659 		 * nb_used better be less than or equal to txq->tx_free_thresh
660 		 */
661 		if (nb_used > txq->nb_tx_free) {
662 			PMD_TX_LOG(DEBUG,
663 				"Not enough free Tx descriptors "
664 				"nb_used=%4u nb_free=%4u "
665 				"(port=%d queue=%d)",
666 				nb_used, txq->nb_tx_free,
667 				txq->port_id, txq->queue_id);
668 
669 			if (ngbe_xmit_cleanup(txq) != 0) {
670 				/* Could not clean any descriptors */
671 				if (nb_tx == 0)
672 					return 0;
673 				goto end_of_tx;
674 			}
675 
676 			/* nb_used better be <= txq->tx_free_thresh */
677 			if (unlikely(nb_used > txq->tx_free_thresh)) {
678 				PMD_TX_LOG(DEBUG,
679 					"The number of descriptors needed to "
680 					"transmit the packet exceeds the "
681 					"RS bit threshold. This will impact "
682 					"performance."
683 					"nb_used=%4u nb_free=%4u "
684 					"tx_free_thresh=%4u. "
685 					"(port=%d queue=%d)",
686 					nb_used, txq->nb_tx_free,
687 					txq->tx_free_thresh,
688 					txq->port_id, txq->queue_id);
689 				/*
690 				 * Loop here until there are enough Tx
691 				 * descriptors or until the ring cannot be
692 				 * cleaned.
693 				 */
694 				while (nb_used > txq->nb_tx_free) {
695 					if (ngbe_xmit_cleanup(txq) != 0) {
696 						/*
697 						 * Could not clean any
698 						 * descriptors
699 						 */
700 						if (nb_tx == 0)
701 							return 0;
702 						goto end_of_tx;
703 					}
704 				}
705 			}
706 		}
707 
708 		/*
709 		 * By now there are enough free Tx descriptors to transmit
710 		 * the packet.
711 		 */
712 
713 		/*
714 		 * Set common flags of all Tx Data Descriptors.
715 		 *
716 		 * The following bits must be set in the first Data Descriptor
717 		 * and are ignored in the other ones:
718 		 *   - NGBE_TXD_FCS
719 		 *
720 		 * The following bits must only be set in the last Data
721 		 * Descriptor:
722 		 *   - NGBE_TXD_EOP
723 		 */
724 		cmd_type_len = NGBE_TXD_FCS;
725 
726 #ifdef RTE_LIBRTE_IEEE1588
727 		if (ol_flags & RTE_MBUF_F_TX_IEEE1588_TMST)
728 			cmd_type_len |= NGBE_TXD_1588;
729 #endif
730 
731 		olinfo_status = 0;
732 		if (tx_ol_req) {
733 			if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
734 				/* when TSO is on, paylen in descriptor is the
735 				 * not the packet len but the tcp payload len
736 				 */
737 				pkt_len -= (tx_offload.l2_len +
738 					tx_offload.l3_len + tx_offload.l4_len);
739 			}
740 
741 			/*
742 			 * Setup the Tx Context Descriptor if required
743 			 */
744 			if (new_ctx) {
745 				volatile struct ngbe_tx_ctx_desc *ctx_txd;
746 
747 				ctx_txd = (volatile struct ngbe_tx_ctx_desc *)
748 				    &txr[tx_id];
749 
750 				txn = &sw_ring[txe->next_id];
751 				rte_prefetch0(&txn->mbuf->pool);
752 
753 				if (txe->mbuf != NULL) {
754 					rte_pktmbuf_free_seg(txe->mbuf);
755 					txe->mbuf = NULL;
756 				}
757 
758 				ngbe_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
759 					tx_offload);
760 
761 				txe->last_id = tx_last;
762 				tx_id = txe->next_id;
763 				txe = txn;
764 			}
765 
766 			/*
767 			 * Setup the Tx Data Descriptor,
768 			 * This path will go through
769 			 * whatever new/reuse the context descriptor
770 			 */
771 			cmd_type_len  |= tx_desc_ol_flags_to_cmdtype(ol_flags);
772 			olinfo_status |=
773 				tx_desc_cksum_flags_to_olinfo(ol_flags);
774 			olinfo_status |= NGBE_TXD_IDX(ctx);
775 		}
776 
777 		olinfo_status |= NGBE_TXD_PAYLEN(pkt_len);
778 
779 		m_seg = tx_pkt;
780 		do {
781 			txd = &txr[tx_id];
782 			txn = &sw_ring[txe->next_id];
783 			rte_prefetch0(&txn->mbuf->pool);
784 
785 			if (txe->mbuf != NULL)
786 				rte_pktmbuf_free_seg(txe->mbuf);
787 			txe->mbuf = m_seg;
788 
789 			/*
790 			 * Set up Transmit Data Descriptor.
791 			 */
792 			slen = m_seg->data_len;
793 			buf_dma_addr = rte_mbuf_data_iova(m_seg);
794 			txd->qw0 = rte_cpu_to_le_64(buf_dma_addr);
795 			txd->dw2 = rte_cpu_to_le_32(cmd_type_len | slen);
796 			txd->dw3 = rte_cpu_to_le_32(olinfo_status);
797 			txe->last_id = tx_last;
798 			tx_id = txe->next_id;
799 			txe = txn;
800 			m_seg = m_seg->next;
801 		} while (m_seg != NULL);
802 
803 		/*
804 		 * The last packet data descriptor needs End Of Packet (EOP)
805 		 */
806 		cmd_type_len |= NGBE_TXD_EOP;
807 		txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
808 
809 		txd->dw2 |= rte_cpu_to_le_32(cmd_type_len);
810 	}
811 
812 end_of_tx:
813 
814 	rte_wmb();
815 
816 	/*
817 	 * Set the Transmit Descriptor Tail (TDT)
818 	 */
819 	PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
820 		   (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
821 		   (uint16_t)tx_id, (uint16_t)nb_tx);
822 	ngbe_set32_relaxed(txq->tdt_reg_addr, tx_id);
823 	txq->tx_tail = tx_id;
824 
825 	return nb_tx;
826 }
827 
828 /*********************************************************************
829  *
830  *  Tx prep functions
831  *
832  **********************************************************************/
833 uint16_t
834 ngbe_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
835 {
836 	int i, ret;
837 	uint64_t ol_flags;
838 	struct rte_mbuf *m;
839 	struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
840 
841 	for (i = 0; i < nb_pkts; i++) {
842 		m = tx_pkts[i];
843 		ol_flags = m->ol_flags;
844 
845 		/**
846 		 * Check if packet meets requirements for number of segments
847 		 *
848 		 * NOTE: for ngbe it's always (40 - WTHRESH) for both TSO and
849 		 *       non-TSO
850 		 */
851 
852 		if (m->nb_segs > NGBE_TX_MAX_SEG - txq->wthresh) {
853 			rte_errno = -EINVAL;
854 			return i;
855 		}
856 
857 		if (ol_flags & NGBE_TX_OFFLOAD_NOTSUP_MASK) {
858 			rte_errno = -ENOTSUP;
859 			return i;
860 		}
861 
862 #ifdef RTE_ETHDEV_DEBUG_TX
863 		ret = rte_validate_tx_offload(m);
864 		if (ret != 0) {
865 			rte_errno = ret;
866 			return i;
867 		}
868 #endif
869 		ret = rte_net_intel_cksum_prepare(m);
870 		if (ret != 0) {
871 			rte_errno = ret;
872 			return i;
873 		}
874 	}
875 
876 	return i;
877 }
878 
879 /*********************************************************************
880  *
881  *  Rx functions
882  *
883  **********************************************************************/
884 static inline uint32_t
885 ngbe_rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint16_t ptid_mask)
886 {
887 	uint16_t ptid = NGBE_RXD_PTID(pkt_info);
888 
889 	ptid &= ptid_mask;
890 
891 	return ngbe_decode_ptype(ptid);
892 }
893 
894 static inline uint64_t
895 ngbe_rxd_pkt_info_to_pkt_flags(uint32_t pkt_info)
896 {
897 	static alignas(RTE_CACHE_LINE_SIZE) uint64_t ip_rss_types_map[16] = {
898 		0, RTE_MBUF_F_RX_RSS_HASH, RTE_MBUF_F_RX_RSS_HASH, RTE_MBUF_F_RX_RSS_HASH,
899 		0, RTE_MBUF_F_RX_RSS_HASH, 0, RTE_MBUF_F_RX_RSS_HASH,
900 		RTE_MBUF_F_RX_RSS_HASH, 0, 0, 0,
901 		0, 0, 0,  RTE_MBUF_F_RX_FDIR,
902 	};
903 #ifdef RTE_LIBRTE_IEEE1588
904 	static uint64_t ip_pkt_etqf_map[8] = {
905 		0, 0, 0, RTE_MBUF_F_RX_IEEE1588_PTP,
906 		0, 0, 0, 0,
907 	};
908 	int etfid = ngbe_etflt_id(NGBE_RXD_PTID(pkt_info));
909 	if (likely(-1 != etfid))
910 		return ip_pkt_etqf_map[etfid] |
911 		       ip_rss_types_map[NGBE_RXD_RSSTYPE(pkt_info)];
912 	else
913 		return ip_rss_types_map[NGBE_RXD_RSSTYPE(pkt_info)];
914 #else
915 	return ip_rss_types_map[NGBE_RXD_RSSTYPE(pkt_info)];
916 #endif
917 }
918 
919 static inline uint64_t
920 rx_desc_status_to_pkt_flags(uint32_t rx_status, uint64_t vlan_flags)
921 {
922 	uint64_t pkt_flags;
923 
924 	/*
925 	 * Check if VLAN present only.
926 	 * Do not check whether L3/L4 rx checksum done by NIC or not,
927 	 * That can be found from rte_eth_rxmode.offloads flag
928 	 */
929 	pkt_flags = (rx_status & NGBE_RXD_STAT_VLAN &&
930 		     vlan_flags & RTE_MBUF_F_RX_VLAN_STRIPPED)
931 		    ? vlan_flags : 0;
932 
933 #ifdef RTE_LIBRTE_IEEE1588
934 	if (rx_status & NGBE_RXD_STAT_1588)
935 		pkt_flags = pkt_flags | RTE_MBUF_F_RX_IEEE1588_TMST;
936 #endif
937 	return pkt_flags;
938 }
939 
940 static inline uint64_t
941 rx_desc_error_to_pkt_flags(uint32_t rx_status)
942 {
943 	uint64_t pkt_flags = 0;
944 
945 	/* checksum offload can't be disabled */
946 	if (rx_status & NGBE_RXD_STAT_IPCS)
947 		pkt_flags |= (rx_status & NGBE_RXD_ERR_IPCS
948 				? RTE_MBUF_F_RX_IP_CKSUM_BAD : RTE_MBUF_F_RX_IP_CKSUM_GOOD);
949 
950 	if (rx_status & NGBE_RXD_STAT_L4CS)
951 		pkt_flags |= (rx_status & NGBE_RXD_ERR_L4CS
952 				? RTE_MBUF_F_RX_L4_CKSUM_BAD : RTE_MBUF_F_RX_L4_CKSUM_GOOD);
953 
954 	if (rx_status & NGBE_RXD_STAT_EIPCS &&
955 	    rx_status & NGBE_RXD_ERR_EIPCS)
956 		pkt_flags |= RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD;
957 
958 	return pkt_flags;
959 }
960 
961 /*
962  * LOOK_AHEAD defines how many desc statuses to check beyond the
963  * current descriptor.
964  * It must be a pound define for optimal performance.
965  * Do not change the value of LOOK_AHEAD, as the ngbe_rx_scan_hw_ring
966  * function only works with LOOK_AHEAD=8.
967  */
968 #define LOOK_AHEAD 8
969 #if (LOOK_AHEAD != 8)
970 #error "PMD NGBE: LOOK_AHEAD must be 8\n"
971 #endif
972 static inline int
973 ngbe_rx_scan_hw_ring(struct ngbe_rx_queue *rxq)
974 {
975 	volatile struct ngbe_rx_desc *rxdp;
976 	struct ngbe_rx_entry *rxep;
977 	struct rte_mbuf *mb;
978 	uint16_t pkt_len;
979 	uint64_t pkt_flags;
980 	int nb_dd;
981 	uint32_t s[LOOK_AHEAD];
982 	uint32_t pkt_info[LOOK_AHEAD];
983 	int i, j, nb_rx = 0;
984 	uint32_t status;
985 
986 	/* get references to current descriptor and S/W ring entry */
987 	rxdp = &rxq->rx_ring[rxq->rx_tail];
988 	rxep = &rxq->sw_ring[rxq->rx_tail];
989 
990 	status = rxdp->qw1.lo.status;
991 	/* check to make sure there is at least 1 packet to receive */
992 	if (!(status & rte_cpu_to_le_32(NGBE_RXD_STAT_DD)))
993 		return 0;
994 
995 	/*
996 	 * Scan LOOK_AHEAD descriptors at a time to determine which descriptors
997 	 * reference packets that are ready to be received.
998 	 */
999 	for (i = 0; i < RTE_PMD_NGBE_RX_MAX_BURST;
1000 	     i += LOOK_AHEAD, rxdp += LOOK_AHEAD, rxep += LOOK_AHEAD) {
1001 		/* Read desc statuses backwards to avoid race condition */
1002 		for (j = 0; j < LOOK_AHEAD; j++)
1003 			s[j] = rte_le_to_cpu_32(rxdp[j].qw1.lo.status);
1004 
1005 		rte_atomic_thread_fence(rte_memory_order_acquire);
1006 
1007 		/* Compute how many status bits were set */
1008 		for (nb_dd = 0; nb_dd < LOOK_AHEAD &&
1009 				(s[nb_dd] & NGBE_RXD_STAT_DD); nb_dd++)
1010 			;
1011 
1012 		for (j = 0; j < nb_dd; j++)
1013 			pkt_info[j] = rte_le_to_cpu_32(rxdp[j].qw0.dw0);
1014 
1015 		nb_rx += nb_dd;
1016 
1017 		/* Translate descriptor info to mbuf format */
1018 		for (j = 0; j < nb_dd; ++j) {
1019 			mb = rxep[j].mbuf;
1020 			pkt_len = rte_le_to_cpu_16(rxdp[j].qw1.hi.len) -
1021 				  rxq->crc_len;
1022 			mb->data_len = pkt_len;
1023 			mb->pkt_len = pkt_len;
1024 			mb->vlan_tci = rte_le_to_cpu_16(rxdp[j].qw1.hi.tag);
1025 
1026 			/* convert descriptor fields to rte mbuf flags */
1027 			pkt_flags = rx_desc_status_to_pkt_flags(s[j],
1028 					rxq->vlan_flags);
1029 			pkt_flags |= rx_desc_error_to_pkt_flags(s[j]);
1030 			pkt_flags |=
1031 				ngbe_rxd_pkt_info_to_pkt_flags(pkt_info[j]);
1032 			mb->ol_flags = pkt_flags;
1033 			mb->packet_type =
1034 				ngbe_rxd_pkt_info_to_pkt_type(pkt_info[j],
1035 				NGBE_PTID_MASK);
1036 
1037 			if (likely(pkt_flags & RTE_MBUF_F_RX_RSS_HASH))
1038 				mb->hash.rss =
1039 					rte_le_to_cpu_32(rxdp[j].qw0.dw1);
1040 		}
1041 
1042 		/* Move mbuf pointers from the S/W ring to the stage */
1043 		for (j = 0; j < LOOK_AHEAD; ++j)
1044 			rxq->rx_stage[i + j] = rxep[j].mbuf;
1045 
1046 		/* stop if all requested packets could not be received */
1047 		if (nb_dd != LOOK_AHEAD)
1048 			break;
1049 	}
1050 
1051 	/* clear software ring entries so we can cleanup correctly */
1052 	for (i = 0; i < nb_rx; ++i)
1053 		rxq->sw_ring[rxq->rx_tail + i].mbuf = NULL;
1054 
1055 	return nb_rx;
1056 }
1057 
1058 static inline int
1059 ngbe_rx_alloc_bufs(struct ngbe_rx_queue *rxq, bool reset_mbuf)
1060 {
1061 	volatile struct ngbe_rx_desc *rxdp;
1062 	struct ngbe_rx_entry *rxep;
1063 	struct rte_mbuf *mb;
1064 	uint16_t alloc_idx;
1065 	__le64 dma_addr;
1066 	int diag, i;
1067 
1068 	/* allocate buffers in bulk directly into the S/W ring */
1069 	alloc_idx = rxq->rx_free_trigger - (rxq->rx_free_thresh - 1);
1070 	rxep = &rxq->sw_ring[alloc_idx];
1071 	diag = rte_mempool_get_bulk(rxq->mb_pool, (void *)rxep,
1072 				    rxq->rx_free_thresh);
1073 	if (unlikely(diag != 0))
1074 		return -ENOMEM;
1075 
1076 	rxdp = &rxq->rx_ring[alloc_idx];
1077 	for (i = 0; i < rxq->rx_free_thresh; ++i) {
1078 		/* populate the static rte mbuf fields */
1079 		mb = rxep[i].mbuf;
1080 		if (reset_mbuf)
1081 			mb->port = rxq->port_id;
1082 
1083 		rte_mbuf_refcnt_set(mb, 1);
1084 		mb->data_off = RTE_PKTMBUF_HEADROOM;
1085 
1086 		/* populate the descriptors */
1087 		dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mb));
1088 		NGBE_RXD_HDRADDR(&rxdp[i], 0);
1089 		NGBE_RXD_PKTADDR(&rxdp[i], dma_addr);
1090 	}
1091 
1092 	/* update state of internal queue structure */
1093 	rxq->rx_free_trigger = rxq->rx_free_trigger + rxq->rx_free_thresh;
1094 	if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
1095 		rxq->rx_free_trigger = rxq->rx_free_thresh - 1;
1096 
1097 	/* no errors */
1098 	return 0;
1099 }
1100 
1101 static inline uint16_t
1102 ngbe_rx_fill_from_stage(struct ngbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
1103 			 uint16_t nb_pkts)
1104 {
1105 	struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
1106 	int i;
1107 
1108 	/* how many packets are ready to return? */
1109 	nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
1110 
1111 	/* copy mbuf pointers to the application's packet list */
1112 	for (i = 0; i < nb_pkts; ++i)
1113 		rx_pkts[i] = stage[i];
1114 
1115 	/* update internal queue state */
1116 	rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
1117 	rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
1118 
1119 	return nb_pkts;
1120 }
1121 
1122 static inline uint16_t
1123 ngbe_rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1124 	     uint16_t nb_pkts)
1125 {
1126 	struct ngbe_rx_queue *rxq = (struct ngbe_rx_queue *)rx_queue;
1127 	struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1128 	uint16_t nb_rx = 0;
1129 
1130 	/* Any previously recv'd pkts will be returned from the Rx stage */
1131 	if (rxq->rx_nb_avail)
1132 		return ngbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1133 
1134 	/* Scan the H/W ring for packets to receive */
1135 	nb_rx = (uint16_t)ngbe_rx_scan_hw_ring(rxq);
1136 
1137 	/* update internal queue state */
1138 	rxq->rx_next_avail = 0;
1139 	rxq->rx_nb_avail = nb_rx;
1140 	rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
1141 
1142 	/* if required, allocate new buffers to replenish descriptors */
1143 	if (rxq->rx_tail > rxq->rx_free_trigger) {
1144 		uint16_t cur_free_trigger = rxq->rx_free_trigger;
1145 
1146 		if (ngbe_rx_alloc_bufs(rxq, true) != 0) {
1147 			int i, j;
1148 
1149 			PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1150 				   "queue_id=%u", (uint16_t)rxq->port_id,
1151 				   (uint16_t)rxq->queue_id);
1152 
1153 			dev->data->rx_mbuf_alloc_failed +=
1154 				rxq->rx_free_thresh;
1155 
1156 			/*
1157 			 * Need to rewind any previous receives if we cannot
1158 			 * allocate new buffers to replenish the old ones.
1159 			 */
1160 			rxq->rx_nb_avail = 0;
1161 			rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
1162 			for (i = 0, j = rxq->rx_tail; i < nb_rx; ++i, ++j)
1163 				rxq->sw_ring[j].mbuf = rxq->rx_stage[i];
1164 
1165 			return 0;
1166 		}
1167 
1168 		/* update tail pointer */
1169 		rte_wmb();
1170 		ngbe_set32_relaxed(rxq->rdt_reg_addr, cur_free_trigger);
1171 	}
1172 
1173 	if (rxq->rx_tail >= rxq->nb_rx_desc)
1174 		rxq->rx_tail = 0;
1175 
1176 	/* received any packets this loop? */
1177 	if (rxq->rx_nb_avail)
1178 		return ngbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1179 
1180 	return 0;
1181 }
1182 
1183 /* split requests into chunks of size RTE_PMD_NGBE_RX_MAX_BURST */
1184 uint16_t
1185 ngbe_recv_pkts_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1186 			   uint16_t nb_pkts)
1187 {
1188 	uint16_t nb_rx;
1189 
1190 	if (unlikely(nb_pkts == 0))
1191 		return 0;
1192 
1193 	if (likely(nb_pkts <= RTE_PMD_NGBE_RX_MAX_BURST))
1194 		return ngbe_rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
1195 
1196 	/* request is relatively large, chunk it up */
1197 	nb_rx = 0;
1198 	while (nb_pkts) {
1199 		uint16_t ret, n;
1200 
1201 		n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_NGBE_RX_MAX_BURST);
1202 		ret = ngbe_rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
1203 		nb_rx = (uint16_t)(nb_rx + ret);
1204 		nb_pkts = (uint16_t)(nb_pkts - ret);
1205 		if (ret < n)
1206 			break;
1207 	}
1208 
1209 	return nb_rx;
1210 }
1211 
1212 uint16_t
1213 ngbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1214 		uint16_t nb_pkts)
1215 {
1216 	struct ngbe_rx_queue *rxq;
1217 	volatile struct ngbe_rx_desc *rx_ring;
1218 	volatile struct ngbe_rx_desc *rxdp;
1219 	struct ngbe_rx_entry *sw_ring;
1220 	struct ngbe_rx_entry *rxe;
1221 	struct rte_mbuf *rxm;
1222 	struct rte_mbuf *nmb;
1223 	struct ngbe_rx_desc rxd;
1224 	uint64_t dma_addr;
1225 	uint32_t staterr;
1226 	uint32_t pkt_info;
1227 	uint16_t pkt_len;
1228 	uint16_t rx_id;
1229 	uint16_t nb_rx;
1230 	uint16_t nb_hold;
1231 	uint64_t pkt_flags;
1232 
1233 	nb_rx = 0;
1234 	nb_hold = 0;
1235 	rxq = rx_queue;
1236 	rx_id = rxq->rx_tail;
1237 	rx_ring = rxq->rx_ring;
1238 	sw_ring = rxq->sw_ring;
1239 	struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1240 	while (nb_rx < nb_pkts) {
1241 		/*
1242 		 * The order of operations here is important as the DD status
1243 		 * bit must not be read after any other descriptor fields.
1244 		 * rx_ring and rxdp are pointing to volatile data so the order
1245 		 * of accesses cannot be reordered by the compiler. If they were
1246 		 * not volatile, they could be reordered which could lead to
1247 		 * using invalid descriptor fields when read from rxd.
1248 		 *
1249 		 * Meanwhile, to prevent the CPU from executing out of order, we
1250 		 * need to use a proper memory barrier to ensure the memory
1251 		 * ordering below.
1252 		 */
1253 		rxdp = &rx_ring[rx_id];
1254 		staterr = rxdp->qw1.lo.status;
1255 		if (!(staterr & rte_cpu_to_le_32(NGBE_RXD_STAT_DD)))
1256 			break;
1257 
1258 		/*
1259 		 * Use acquire fence to ensure that status_error which includes
1260 		 * DD bit is loaded before loading of other descriptor words.
1261 		 */
1262 		rte_atomic_thread_fence(rte_memory_order_acquire);
1263 
1264 		rxd = *rxdp;
1265 
1266 		/*
1267 		 * End of packet.
1268 		 *
1269 		 * If the NGBE_RXD_STAT_EOP flag is not set, the Rx packet
1270 		 * is likely to be invalid and to be dropped by the various
1271 		 * validation checks performed by the network stack.
1272 		 *
1273 		 * Allocate a new mbuf to replenish the RX ring descriptor.
1274 		 * If the allocation fails:
1275 		 *    - arrange for that Rx descriptor to be the first one
1276 		 *      being parsed the next time the receive function is
1277 		 *      invoked [on the same queue].
1278 		 *
1279 		 *    - Stop parsing the Rx ring and return immediately.
1280 		 *
1281 		 * This policy do not drop the packet received in the Rx
1282 		 * descriptor for which the allocation of a new mbuf failed.
1283 		 * Thus, it allows that packet to be later retrieved if
1284 		 * mbuf have been freed in the mean time.
1285 		 * As a side effect, holding Rx descriptors instead of
1286 		 * systematically giving them back to the NIC may lead to
1287 		 * Rx ring exhaustion situations.
1288 		 * However, the NIC can gracefully prevent such situations
1289 		 * to happen by sending specific "back-pressure" flow control
1290 		 * frames to its peer(s).
1291 		 */
1292 		PMD_RX_LOG(DEBUG,
1293 			   "port_id=%u queue_id=%u rx_id=%u ext_err_stat=0x%08x pkt_len=%u",
1294 			   (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1295 			   (uint16_t)rx_id, (uint32_t)staterr,
1296 			   (uint16_t)rte_le_to_cpu_16(rxd.qw1.hi.len));
1297 
1298 		nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1299 		if (nmb == NULL) {
1300 			PMD_RX_LOG(DEBUG,
1301 				   "Rx mbuf alloc failed port_id=%u queue_id=%u",
1302 				   (uint16_t)rxq->port_id,
1303 				   (uint16_t)rxq->queue_id);
1304 			dev->data->rx_mbuf_alloc_failed++;
1305 			break;
1306 		}
1307 
1308 		nb_hold++;
1309 		rxe = &sw_ring[rx_id];
1310 		rx_id++;
1311 		if (rx_id == rxq->nb_rx_desc)
1312 			rx_id = 0;
1313 
1314 		/* Prefetch next mbuf while processing current one. */
1315 		rte_ngbe_prefetch(sw_ring[rx_id].mbuf);
1316 
1317 		/*
1318 		 * When next Rx descriptor is on a cache-line boundary,
1319 		 * prefetch the next 4 Rx descriptors and the next 8 pointers
1320 		 * to mbufs.
1321 		 */
1322 		if ((rx_id & 0x3) == 0) {
1323 			rte_ngbe_prefetch(&rx_ring[rx_id]);
1324 			rte_ngbe_prefetch(&sw_ring[rx_id]);
1325 		}
1326 
1327 		rxm = rxe->mbuf;
1328 		rxe->mbuf = nmb;
1329 		dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1330 		NGBE_RXD_HDRADDR(rxdp, 0);
1331 		NGBE_RXD_PKTADDR(rxdp, dma_addr);
1332 
1333 		/*
1334 		 * Initialize the returned mbuf.
1335 		 * 1) setup generic mbuf fields:
1336 		 *    - number of segments,
1337 		 *    - next segment,
1338 		 *    - packet length,
1339 		 *    - Rx port identifier.
1340 		 * 2) integrate hardware offload data, if any:
1341 		 *    - RSS flag & hash,
1342 		 *    - IP checksum flag,
1343 		 *    - VLAN TCI, if any,
1344 		 *    - error flags.
1345 		 */
1346 		pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.qw1.hi.len) -
1347 				      rxq->crc_len);
1348 		rxm->data_off = RTE_PKTMBUF_HEADROOM;
1349 		rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
1350 		rxm->nb_segs = 1;
1351 		rxm->next = NULL;
1352 		rxm->pkt_len = pkt_len;
1353 		rxm->data_len = pkt_len;
1354 		rxm->port = rxq->port_id;
1355 
1356 		pkt_info = rte_le_to_cpu_32(rxd.qw0.dw0);
1357 		/* Only valid if RTE_MBUF_F_RX_VLAN set in pkt_flags */
1358 		rxm->vlan_tci = rte_le_to_cpu_16(rxd.qw1.hi.tag);
1359 
1360 		pkt_flags = rx_desc_status_to_pkt_flags(staterr,
1361 					rxq->vlan_flags);
1362 		pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1363 		pkt_flags |= ngbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1364 		rxm->ol_flags = pkt_flags;
1365 		rxm->packet_type = ngbe_rxd_pkt_info_to_pkt_type(pkt_info,
1366 						       NGBE_PTID_MASK);
1367 
1368 		if (likely(pkt_flags & RTE_MBUF_F_RX_RSS_HASH))
1369 			rxm->hash.rss = rte_le_to_cpu_32(rxd.qw0.dw1);
1370 
1371 		/*
1372 		 * Store the mbuf address into the next entry of the array
1373 		 * of returned packets.
1374 		 */
1375 		rx_pkts[nb_rx++] = rxm;
1376 	}
1377 	rxq->rx_tail = rx_id;
1378 
1379 	/*
1380 	 * If the number of free Rx descriptors is greater than the Rx free
1381 	 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1382 	 * register.
1383 	 * Update the RDT with the value of the last processed Rx descriptor
1384 	 * minus 1, to guarantee that the RDT register is never equal to the
1385 	 * RDH register, which creates a "full" ring situation from the
1386 	 * hardware point of view...
1387 	 */
1388 	nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
1389 	if (nb_hold > rxq->rx_free_thresh) {
1390 		PMD_RX_LOG(DEBUG,
1391 			   "port_id=%u queue_id=%u rx_tail=%u nb_hold=%u nb_rx=%u",
1392 			   (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1393 			   (uint16_t)rx_id, (uint16_t)nb_hold,
1394 			   (uint16_t)nb_rx);
1395 		rx_id = (uint16_t)((rx_id == 0) ?
1396 				(rxq->nb_rx_desc - 1) : (rx_id - 1));
1397 		ngbe_set32(rxq->rdt_reg_addr, rx_id);
1398 		nb_hold = 0;
1399 	}
1400 	rxq->nb_rx_hold = nb_hold;
1401 	return nb_rx;
1402 }
1403 
1404 /**
1405  * ngbe_fill_cluster_head_buf - fill the first mbuf of the returned packet
1406  *
1407  * Fill the following info in the HEAD buffer of the Rx cluster:
1408  *    - RX port identifier
1409  *    - hardware offload data, if any:
1410  *      - RSS flag & hash
1411  *      - IP checksum flag
1412  *      - VLAN TCI, if any
1413  *      - error flags
1414  * @head HEAD of the packet cluster
1415  * @desc HW descriptor to get data from
1416  * @rxq Pointer to the Rx queue
1417  */
1418 static inline void
1419 ngbe_fill_cluster_head_buf(struct rte_mbuf *head, struct ngbe_rx_desc *desc,
1420 		struct ngbe_rx_queue *rxq, uint32_t staterr)
1421 {
1422 	uint32_t pkt_info;
1423 	uint64_t pkt_flags;
1424 
1425 	head->port = rxq->port_id;
1426 
1427 	/* The vlan_tci field is only valid when RTE_MBUF_F_RX_VLAN is
1428 	 * set in the pkt_flags field.
1429 	 */
1430 	head->vlan_tci = rte_le_to_cpu_16(desc->qw1.hi.tag);
1431 	pkt_info = rte_le_to_cpu_32(desc->qw0.dw0);
1432 	pkt_flags = rx_desc_status_to_pkt_flags(staterr, rxq->vlan_flags);
1433 	pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1434 	pkt_flags |= ngbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1435 	head->ol_flags = pkt_flags;
1436 	head->packet_type = ngbe_rxd_pkt_info_to_pkt_type(pkt_info,
1437 						NGBE_PTID_MASK);
1438 
1439 	if (likely(pkt_flags & RTE_MBUF_F_RX_RSS_HASH))
1440 		head->hash.rss = rte_le_to_cpu_32(desc->qw0.dw1);
1441 }
1442 
1443 /**
1444  * ngbe_recv_pkts_sc - receive handler for scatter case.
1445  *
1446  * @rx_queue Rx queue handle
1447  * @rx_pkts table of received packets
1448  * @nb_pkts size of rx_pkts table
1449  * @bulk_alloc if TRUE bulk allocation is used for a HW ring refilling
1450  *
1451  * Returns the number of received packets/clusters (according to the "bulk
1452  * receive" interface).
1453  */
1454 static inline uint16_t
1455 ngbe_recv_pkts_sc(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts,
1456 		    bool bulk_alloc)
1457 {
1458 	struct ngbe_rx_queue *rxq = rx_queue;
1459 	struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1460 	volatile struct ngbe_rx_desc *rx_ring = rxq->rx_ring;
1461 	struct ngbe_rx_entry *sw_ring = rxq->sw_ring;
1462 	struct ngbe_scattered_rx_entry *sw_sc_ring = rxq->sw_sc_ring;
1463 	uint16_t rx_id = rxq->rx_tail;
1464 	uint16_t nb_rx = 0;
1465 	uint16_t nb_hold = rxq->nb_rx_hold;
1466 	uint16_t prev_id = rxq->rx_tail;
1467 
1468 	while (nb_rx < nb_pkts) {
1469 		bool eop;
1470 		struct ngbe_rx_entry *rxe;
1471 		struct ngbe_scattered_rx_entry *sc_entry;
1472 		struct ngbe_scattered_rx_entry *next_sc_entry = NULL;
1473 		struct ngbe_rx_entry *next_rxe = NULL;
1474 		struct rte_mbuf *first_seg;
1475 		struct rte_mbuf *rxm;
1476 		struct rte_mbuf *nmb = NULL;
1477 		struct ngbe_rx_desc rxd;
1478 		uint16_t data_len;
1479 		uint16_t next_id;
1480 		volatile struct ngbe_rx_desc *rxdp;
1481 		uint32_t staterr;
1482 
1483 next_desc:
1484 		rxdp = &rx_ring[rx_id];
1485 		staterr = rte_le_to_cpu_32(rxdp->qw1.lo.status);
1486 
1487 		if (!(staterr & NGBE_RXD_STAT_DD))
1488 			break;
1489 
1490 		/*
1491 		 * Use acquire fence to ensure that status_error which includes
1492 		 * DD bit is loaded before loading of other descriptor words.
1493 		 */
1494 		rte_atomic_thread_fence(rte_memory_order_acquire);
1495 
1496 		rxd = *rxdp;
1497 
1498 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1499 				  "staterr=0x%x data_len=%u",
1500 			   rxq->port_id, rxq->queue_id, rx_id, staterr,
1501 			   rte_le_to_cpu_16(rxd.qw1.hi.len));
1502 
1503 		if (!bulk_alloc) {
1504 			nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1505 			if (nmb == NULL) {
1506 				PMD_RX_LOG(DEBUG, "Rx mbuf alloc failed "
1507 						  "port_id=%u queue_id=%u",
1508 					   rxq->port_id, rxq->queue_id);
1509 
1510 				dev->data->rx_mbuf_alloc_failed++;
1511 				break;
1512 			}
1513 		} else if (nb_hold > rxq->rx_free_thresh) {
1514 			uint16_t next_rdt = rxq->rx_free_trigger;
1515 
1516 			if (!ngbe_rx_alloc_bufs(rxq, false)) {
1517 				rte_wmb();
1518 				ngbe_set32_relaxed(rxq->rdt_reg_addr,
1519 							    next_rdt);
1520 				nb_hold -= rxq->rx_free_thresh;
1521 			} else {
1522 				PMD_RX_LOG(DEBUG, "Rx bulk alloc failed "
1523 						  "port_id=%u queue_id=%u",
1524 					   rxq->port_id, rxq->queue_id);
1525 
1526 				dev->data->rx_mbuf_alloc_failed++;
1527 				break;
1528 			}
1529 		}
1530 
1531 		nb_hold++;
1532 		rxe = &sw_ring[rx_id];
1533 		eop = staterr & NGBE_RXD_STAT_EOP;
1534 
1535 		next_id = rx_id + 1;
1536 		if (next_id == rxq->nb_rx_desc)
1537 			next_id = 0;
1538 
1539 		/* Prefetch next mbuf while processing current one. */
1540 		rte_ngbe_prefetch(sw_ring[next_id].mbuf);
1541 
1542 		/*
1543 		 * When next Rx descriptor is on a cache-line boundary,
1544 		 * prefetch the next 4 RX descriptors and the next 4 pointers
1545 		 * to mbufs.
1546 		 */
1547 		if ((next_id & 0x3) == 0) {
1548 			rte_ngbe_prefetch(&rx_ring[next_id]);
1549 			rte_ngbe_prefetch(&sw_ring[next_id]);
1550 		}
1551 
1552 		rxm = rxe->mbuf;
1553 
1554 		if (!bulk_alloc) {
1555 			__le64 dma =
1556 			  rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1557 			/*
1558 			 * Update Rx descriptor with the physical address of the
1559 			 * new data buffer of the new allocated mbuf.
1560 			 */
1561 			rxe->mbuf = nmb;
1562 
1563 			rxm->data_off = RTE_PKTMBUF_HEADROOM;
1564 			NGBE_RXD_HDRADDR(rxdp, 0);
1565 			NGBE_RXD_PKTADDR(rxdp, dma);
1566 		} else {
1567 			rxe->mbuf = NULL;
1568 		}
1569 
1570 		/*
1571 		 * Set data length & data buffer address of mbuf.
1572 		 */
1573 		data_len = rte_le_to_cpu_16(rxd.qw1.hi.len);
1574 		rxm->data_len = data_len;
1575 
1576 		if (!eop) {
1577 			uint16_t nextp_id;
1578 
1579 			nextp_id = next_id;
1580 			next_sc_entry = &sw_sc_ring[nextp_id];
1581 			next_rxe = &sw_ring[nextp_id];
1582 			rte_ngbe_prefetch(next_rxe);
1583 		}
1584 
1585 		sc_entry = &sw_sc_ring[rx_id];
1586 		first_seg = sc_entry->fbuf;
1587 		sc_entry->fbuf = NULL;
1588 
1589 		/*
1590 		 * If this is the first buffer of the received packet,
1591 		 * set the pointer to the first mbuf of the packet and
1592 		 * initialize its context.
1593 		 * Otherwise, update the total length and the number of segments
1594 		 * of the current scattered packet, and update the pointer to
1595 		 * the last mbuf of the current packet.
1596 		 */
1597 		if (first_seg == NULL) {
1598 			first_seg = rxm;
1599 			first_seg->pkt_len = data_len;
1600 			first_seg->nb_segs = 1;
1601 		} else {
1602 			first_seg->pkt_len += data_len;
1603 			first_seg->nb_segs++;
1604 		}
1605 
1606 		prev_id = rx_id;
1607 		rx_id = next_id;
1608 
1609 		/*
1610 		 * If this is not the last buffer of the received packet, update
1611 		 * the pointer to the first mbuf at the NEXTP entry in the
1612 		 * sw_sc_ring and continue to parse the Rx ring.
1613 		 */
1614 		if (!eop && next_rxe) {
1615 			rxm->next = next_rxe->mbuf;
1616 			next_sc_entry->fbuf = first_seg;
1617 			goto next_desc;
1618 		}
1619 
1620 		/* Initialize the first mbuf of the returned packet */
1621 		ngbe_fill_cluster_head_buf(first_seg, &rxd, rxq, staterr);
1622 
1623 		/* Deal with the case, when HW CRC srip is disabled. */
1624 		first_seg->pkt_len -= rxq->crc_len;
1625 		if (unlikely(rxm->data_len <= rxq->crc_len)) {
1626 			struct rte_mbuf *lp;
1627 
1628 			for (lp = first_seg; lp->next != rxm; lp = lp->next)
1629 				;
1630 
1631 			first_seg->nb_segs--;
1632 			lp->data_len -= rxq->crc_len - rxm->data_len;
1633 			lp->next = NULL;
1634 			rte_pktmbuf_free_seg(rxm);
1635 		} else {
1636 			rxm->data_len -= rxq->crc_len;
1637 		}
1638 
1639 		/* Prefetch data of first segment, if configured to do so. */
1640 		rte_packet_prefetch((char *)first_seg->buf_addr +
1641 			first_seg->data_off);
1642 
1643 		/*
1644 		 * Store the mbuf address into the next entry of the array
1645 		 * of returned packets.
1646 		 */
1647 		rx_pkts[nb_rx++] = first_seg;
1648 	}
1649 
1650 	/*
1651 	 * Record index of the next Rx descriptor to probe.
1652 	 */
1653 	rxq->rx_tail = rx_id;
1654 
1655 	/*
1656 	 * If the number of free Rx descriptors is greater than the Rx free
1657 	 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1658 	 * register.
1659 	 * Update the RDT with the value of the last processed Rx descriptor
1660 	 * minus 1, to guarantee that the RDT register is never equal to the
1661 	 * RDH register, which creates a "full" ring situation from the
1662 	 * hardware point of view...
1663 	 */
1664 	if (!bulk_alloc && nb_hold > rxq->rx_free_thresh) {
1665 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1666 			   "nb_hold=%u nb_rx=%u",
1667 			   rxq->port_id, rxq->queue_id, rx_id, nb_hold, nb_rx);
1668 
1669 		rte_wmb();
1670 		ngbe_set32_relaxed(rxq->rdt_reg_addr, prev_id);
1671 		nb_hold = 0;
1672 	}
1673 
1674 	rxq->nb_rx_hold = nb_hold;
1675 	return nb_rx;
1676 }
1677 
1678 uint16_t
1679 ngbe_recv_pkts_sc_single_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1680 				 uint16_t nb_pkts)
1681 {
1682 	return ngbe_recv_pkts_sc(rx_queue, rx_pkts, nb_pkts, false);
1683 }
1684 
1685 uint16_t
1686 ngbe_recv_pkts_sc_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1687 			       uint16_t nb_pkts)
1688 {
1689 	return ngbe_recv_pkts_sc(rx_queue, rx_pkts, nb_pkts, true);
1690 }
1691 
1692 /*********************************************************************
1693  *
1694  *  Queue management functions
1695  *
1696  **********************************************************************/
1697 
1698 static void
1699 ngbe_tx_queue_release_mbufs(struct ngbe_tx_queue *txq)
1700 {
1701 	unsigned int i;
1702 
1703 	if (txq->sw_ring != NULL) {
1704 		for (i = 0; i < txq->nb_tx_desc; i++) {
1705 			if (txq->sw_ring[i].mbuf != NULL) {
1706 				rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1707 				txq->sw_ring[i].mbuf = NULL;
1708 			}
1709 		}
1710 	}
1711 }
1712 
1713 static int
1714 ngbe_tx_done_cleanup_full(struct ngbe_tx_queue *txq, uint32_t free_cnt)
1715 {
1716 	struct ngbe_tx_entry *swr_ring = txq->sw_ring;
1717 	uint16_t i, tx_last, tx_id;
1718 	uint16_t nb_tx_free_last;
1719 	uint16_t nb_tx_to_clean;
1720 	uint32_t pkt_cnt;
1721 
1722 	/* Start free mbuf from the next of tx_tail */
1723 	tx_last = txq->tx_tail;
1724 	tx_id  = swr_ring[tx_last].next_id;
1725 
1726 	if (txq->nb_tx_free == 0 && ngbe_xmit_cleanup(txq))
1727 		return 0;
1728 
1729 	nb_tx_to_clean = txq->nb_tx_free;
1730 	nb_tx_free_last = txq->nb_tx_free;
1731 	if (!free_cnt)
1732 		free_cnt = txq->nb_tx_desc;
1733 
1734 	/* Loop through swr_ring to count the amount of
1735 	 * freeable mubfs and packets.
1736 	 */
1737 	for (pkt_cnt = 0; pkt_cnt < free_cnt; ) {
1738 		for (i = 0; i < nb_tx_to_clean &&
1739 			pkt_cnt < free_cnt &&
1740 			tx_id != tx_last; i++) {
1741 			if (swr_ring[tx_id].mbuf != NULL) {
1742 				rte_pktmbuf_free_seg(swr_ring[tx_id].mbuf);
1743 				swr_ring[tx_id].mbuf = NULL;
1744 
1745 				/*
1746 				 * last segment in the packet,
1747 				 * increment packet count
1748 				 */
1749 				pkt_cnt += (swr_ring[tx_id].last_id == tx_id);
1750 			}
1751 
1752 			tx_id = swr_ring[tx_id].next_id;
1753 		}
1754 
1755 		if (pkt_cnt < free_cnt) {
1756 			if (ngbe_xmit_cleanup(txq))
1757 				break;
1758 
1759 			nb_tx_to_clean = txq->nb_tx_free - nb_tx_free_last;
1760 			nb_tx_free_last = txq->nb_tx_free;
1761 		}
1762 	}
1763 
1764 	return (int)pkt_cnt;
1765 }
1766 
1767 static int
1768 ngbe_tx_done_cleanup_simple(struct ngbe_tx_queue *txq,
1769 			uint32_t free_cnt)
1770 {
1771 	int i, n, cnt;
1772 
1773 	if (free_cnt == 0 || free_cnt > txq->nb_tx_desc)
1774 		free_cnt = txq->nb_tx_desc;
1775 
1776 	cnt = free_cnt - free_cnt % txq->tx_free_thresh;
1777 
1778 	for (i = 0; i < cnt; i += n) {
1779 		if (txq->nb_tx_desc - txq->nb_tx_free < txq->tx_free_thresh)
1780 			break;
1781 
1782 		n = ngbe_tx_free_bufs(txq);
1783 
1784 		if (n == 0)
1785 			break;
1786 	}
1787 
1788 	return i;
1789 }
1790 
1791 int
1792 ngbe_dev_tx_done_cleanup(void *tx_queue, uint32_t free_cnt)
1793 {
1794 	struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
1795 	if (txq->offloads == 0 &&
1796 		txq->tx_free_thresh >= RTE_PMD_NGBE_TX_MAX_BURST)
1797 		return ngbe_tx_done_cleanup_simple(txq, free_cnt);
1798 
1799 	return ngbe_tx_done_cleanup_full(txq, free_cnt);
1800 }
1801 
1802 static void
1803 ngbe_tx_free_swring(struct ngbe_tx_queue *txq)
1804 {
1805 	if (txq != NULL)
1806 		rte_free(txq->sw_ring);
1807 }
1808 
1809 static void
1810 ngbe_tx_queue_release(struct ngbe_tx_queue *txq)
1811 {
1812 	if (txq != NULL) {
1813 		if (txq->ops != NULL) {
1814 			txq->ops->release_mbufs(txq);
1815 			txq->ops->free_swring(txq);
1816 			rte_memzone_free(txq->mz);
1817 		}
1818 		rte_free(txq);
1819 	}
1820 }
1821 
1822 void
1823 ngbe_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1824 {
1825 	ngbe_tx_queue_release(dev->data->tx_queues[qid]);
1826 }
1827 
1828 /* (Re)set dynamic ngbe_tx_queue fields to defaults */
1829 static void
1830 ngbe_reset_tx_queue(struct ngbe_tx_queue *txq)
1831 {
1832 	static const struct ngbe_tx_desc zeroed_desc = {0};
1833 	struct ngbe_tx_entry *txe = txq->sw_ring;
1834 	uint16_t prev, i;
1835 
1836 	/* Zero out HW ring memory */
1837 	for (i = 0; i < txq->nb_tx_desc; i++)
1838 		txq->tx_ring[i] = zeroed_desc;
1839 
1840 	/* Initialize SW ring entries */
1841 	prev = (uint16_t)(txq->nb_tx_desc - 1);
1842 	for (i = 0; i < txq->nb_tx_desc; i++) {
1843 		/* the ring can also be modified by hardware */
1844 		volatile struct ngbe_tx_desc *txd = &txq->tx_ring[i];
1845 
1846 		txd->dw3 = rte_cpu_to_le_32(NGBE_TXD_DD);
1847 		txe[i].mbuf = NULL;
1848 		txe[i].last_id = i;
1849 		txe[prev].next_id = i;
1850 		prev = i;
1851 	}
1852 
1853 	txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
1854 	txq->tx_tail = 0;
1855 
1856 	/*
1857 	 * Always allow 1 descriptor to be un-allocated to avoid
1858 	 * a H/W race condition
1859 	 */
1860 	txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
1861 	txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
1862 	txq->ctx_curr = 0;
1863 	memset((void *)&txq->ctx_cache, 0,
1864 		NGBE_CTX_NUM * sizeof(struct ngbe_ctx_info));
1865 }
1866 
1867 static const struct ngbe_txq_ops def_txq_ops = {
1868 	.release_mbufs = ngbe_tx_queue_release_mbufs,
1869 	.free_swring = ngbe_tx_free_swring,
1870 	.reset = ngbe_reset_tx_queue,
1871 };
1872 
1873 /* Takes an ethdev and a queue and sets up the tx function to be used based on
1874  * the queue parameters. Used in tx_queue_setup by primary process and then
1875  * in dev_init by secondary process when attaching to an existing ethdev.
1876  */
1877 void
1878 ngbe_set_tx_function(struct rte_eth_dev *dev, struct ngbe_tx_queue *txq)
1879 {
1880 	/* Use a simple Tx queue (no offloads, no multi segs) if possible */
1881 	if (txq->offloads == 0 &&
1882 			txq->tx_free_thresh >= RTE_PMD_NGBE_TX_MAX_BURST) {
1883 		PMD_INIT_LOG(DEBUG, "Using simple tx code path");
1884 		dev->tx_pkt_prepare = NULL;
1885 		if (txq->tx_free_thresh <= RTE_NGBE_TX_MAX_FREE_BUF_SZ &&
1886 				rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128 &&
1887 				(rte_eal_process_type() != RTE_PROC_PRIMARY ||
1888 					ngbe_txq_vec_setup(txq) == 0)) {
1889 			PMD_INIT_LOG(DEBUG, "Vector tx enabled.");
1890 			dev->tx_pkt_burst = ngbe_xmit_pkts_vec;
1891 		} else {
1892 			dev->tx_pkt_burst = ngbe_xmit_pkts_simple;
1893 		}
1894 	} else {
1895 		PMD_INIT_LOG(DEBUG, "Using full-featured tx code path");
1896 		PMD_INIT_LOG(DEBUG,
1897 				" - offloads = 0x%" PRIx64,
1898 				txq->offloads);
1899 		PMD_INIT_LOG(DEBUG,
1900 				" - tx_free_thresh = %lu [RTE_PMD_NGBE_TX_MAX_BURST=%lu]",
1901 				(unsigned long)txq->tx_free_thresh,
1902 				(unsigned long)RTE_PMD_NGBE_TX_MAX_BURST);
1903 		dev->tx_pkt_burst = ngbe_xmit_pkts;
1904 		dev->tx_pkt_prepare = ngbe_prep_pkts;
1905 	}
1906 }
1907 
1908 static const struct {
1909 	eth_tx_burst_t pkt_burst;
1910 	const char *info;
1911 } ngbe_tx_burst_infos[] = {
1912 	{ ngbe_xmit_pkts_simple,   "Scalar Simple"},
1913 	{ ngbe_xmit_pkts,          "Scalar"},
1914 #ifdef RTE_ARCH_X86
1915 	{ ngbe_xmit_pkts_vec,      "Vector SSE" },
1916 #elif defined(RTE_ARCH_ARM)
1917 	{ ngbe_xmit_pkts_vec,      "Vector Neon" },
1918 #endif
1919 };
1920 
1921 int
1922 ngbe_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
1923 		      struct rte_eth_burst_mode *mode)
1924 {
1925 	eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
1926 	int ret = -EINVAL;
1927 	unsigned int i;
1928 
1929 	for (i = 0; i < RTE_DIM(ngbe_tx_burst_infos); ++i) {
1930 		if (pkt_burst == ngbe_tx_burst_infos[i].pkt_burst) {
1931 			snprintf(mode->info, sizeof(mode->info), "%s",
1932 				 ngbe_tx_burst_infos[i].info);
1933 			ret = 0;
1934 			break;
1935 		}
1936 	}
1937 
1938 	return ret;
1939 }
1940 
1941 uint64_t
1942 ngbe_get_tx_port_offloads(struct rte_eth_dev *dev)
1943 {
1944 	uint64_t tx_offload_capa;
1945 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
1946 
1947 	tx_offload_capa =
1948 		RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
1949 		RTE_ETH_TX_OFFLOAD_IPV4_CKSUM  |
1950 		RTE_ETH_TX_OFFLOAD_UDP_CKSUM   |
1951 		RTE_ETH_TX_OFFLOAD_TCP_CKSUM   |
1952 		RTE_ETH_TX_OFFLOAD_SCTP_CKSUM  |
1953 		RTE_ETH_TX_OFFLOAD_TCP_TSO     |
1954 		RTE_ETH_TX_OFFLOAD_UDP_TSO	   |
1955 		RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
1956 
1957 	if (hw->is_pf)
1958 		tx_offload_capa |= RTE_ETH_TX_OFFLOAD_QINQ_INSERT;
1959 
1960 	return tx_offload_capa;
1961 }
1962 
1963 int
1964 ngbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
1965 			 uint16_t queue_idx,
1966 			 uint16_t nb_desc,
1967 			 unsigned int socket_id,
1968 			 const struct rte_eth_txconf *tx_conf)
1969 {
1970 	const struct rte_memzone *tz;
1971 	struct ngbe_tx_queue *txq;
1972 	struct ngbe_hw     *hw;
1973 	uint16_t tx_free_thresh;
1974 	uint64_t offloads;
1975 
1976 	PMD_INIT_FUNC_TRACE();
1977 	hw = ngbe_dev_hw(dev);
1978 
1979 	offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1980 
1981 	/*
1982 	 * The Tx descriptor ring will be cleaned after txq->tx_free_thresh
1983 	 * descriptors are used or if the number of descriptors required
1984 	 * to transmit a packet is greater than the number of free Tx
1985 	 * descriptors.
1986 	 * One descriptor in the Tx ring is used as a sentinel to avoid a
1987 	 * H/W race condition, hence the maximum threshold constraints.
1988 	 * When set to zero use default values.
1989 	 */
1990 	tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
1991 			tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
1992 	if (tx_free_thresh >= (nb_desc - 3)) {
1993 		PMD_INIT_LOG(ERR,
1994 			     "tx_free_thresh must be less than the number of TX descriptors minus 3. (tx_free_thresh=%u port=%d queue=%d)",
1995 			     (unsigned int)tx_free_thresh,
1996 			     (int)dev->data->port_id, (int)queue_idx);
1997 		return -(EINVAL);
1998 	}
1999 
2000 	if (nb_desc % tx_free_thresh != 0) {
2001 		PMD_INIT_LOG(ERR,
2002 			     "tx_free_thresh must be a divisor of the number of Tx descriptors. (tx_free_thresh=%u port=%d queue=%d)",
2003 			     (unsigned int)tx_free_thresh,
2004 			     (int)dev->data->port_id, (int)queue_idx);
2005 		return -(EINVAL);
2006 	}
2007 
2008 	/* Free memory prior to re-allocation if needed... */
2009 	if (dev->data->tx_queues[queue_idx] != NULL) {
2010 		ngbe_tx_queue_release(dev->data->tx_queues[queue_idx]);
2011 		dev->data->tx_queues[queue_idx] = NULL;
2012 	}
2013 
2014 	/* First allocate the Tx queue data structure */
2015 	txq = rte_zmalloc_socket("ethdev Tx queue",
2016 				 sizeof(struct ngbe_tx_queue),
2017 				 RTE_CACHE_LINE_SIZE, socket_id);
2018 	if (txq == NULL)
2019 		return -ENOMEM;
2020 
2021 	/*
2022 	 * Allocate Tx ring hardware descriptors. A memzone large enough to
2023 	 * handle the maximum ring size is allocated in order to allow for
2024 	 * resizing in later calls to the queue setup function.
2025 	 */
2026 	tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
2027 			sizeof(struct ngbe_tx_desc) * NGBE_RING_DESC_MAX,
2028 			NGBE_ALIGN, socket_id);
2029 	if (tz == NULL) {
2030 		ngbe_tx_queue_release(txq);
2031 		return -ENOMEM;
2032 	}
2033 
2034 	txq->mz = tz;
2035 	txq->nb_tx_desc = nb_desc;
2036 	txq->tx_free_thresh = tx_free_thresh;
2037 	txq->pthresh = tx_conf->tx_thresh.pthresh;
2038 	txq->hthresh = tx_conf->tx_thresh.hthresh;
2039 	txq->wthresh = tx_conf->tx_thresh.wthresh;
2040 	txq->queue_id = queue_idx;
2041 	txq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2042 		queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2043 	txq->port_id = dev->data->port_id;
2044 	txq->offloads = offloads;
2045 	txq->ops = &def_txq_ops;
2046 	txq->tx_deferred_start = tx_conf->tx_deferred_start;
2047 
2048 	txq->tdt_reg_addr = NGBE_REG_ADDR(hw, NGBE_TXWP(txq->reg_idx));
2049 	txq->tdc_reg_addr = NGBE_REG_ADDR(hw, NGBE_TXCFG(txq->reg_idx));
2050 
2051 	txq->tx_ring_phys_addr = TMZ_PADDR(tz);
2052 	txq->tx_ring = (struct ngbe_tx_desc *)TMZ_VADDR(tz);
2053 
2054 	/* Allocate software ring */
2055 	txq->sw_ring = rte_zmalloc_socket("txq->sw_ring",
2056 				sizeof(struct ngbe_tx_entry) * nb_desc,
2057 				RTE_CACHE_LINE_SIZE, socket_id);
2058 	if (txq->sw_ring == NULL) {
2059 		ngbe_tx_queue_release(txq);
2060 		return -ENOMEM;
2061 	}
2062 	PMD_INIT_LOG(DEBUG,
2063 		     "sw_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
2064 		     txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
2065 
2066 	/* set up scalar Tx function as appropriate */
2067 	ngbe_set_tx_function(dev, txq);
2068 
2069 	txq->ops->reset(txq);
2070 
2071 	dev->data->tx_queues[queue_idx] = txq;
2072 
2073 	return 0;
2074 }
2075 
2076 /**
2077  * ngbe_free_sc_cluster - free the not-yet-completed scattered cluster
2078  *
2079  * The "next" pointer of the last segment of (not-yet-completed) RSC clusters
2080  * in the sw_sc_ring is not set to NULL but rather points to the next
2081  * mbuf of this RSC aggregation (that has not been completed yet and still
2082  * resides on the HW ring). So, instead of calling for rte_pktmbuf_free() we
2083  * will just free first "nb_segs" segments of the cluster explicitly by calling
2084  * an rte_pktmbuf_free_seg().
2085  *
2086  * @m scattered cluster head
2087  */
2088 static void
2089 ngbe_free_sc_cluster(struct rte_mbuf *m)
2090 {
2091 	uint16_t i, nb_segs = m->nb_segs;
2092 	struct rte_mbuf *next_seg;
2093 
2094 	for (i = 0; i < nb_segs; i++) {
2095 		next_seg = m->next;
2096 		rte_pktmbuf_free_seg(m);
2097 		m = next_seg;
2098 	}
2099 }
2100 
2101 static void
2102 ngbe_rx_queue_release_mbufs(struct ngbe_rx_queue *rxq)
2103 {
2104 	unsigned int i;
2105 
2106 	/* SSE Vector driver has a different way of releasing mbufs. */
2107 	if (rxq->rx_using_sse) {
2108 		ngbe_rx_queue_release_mbufs_vec(rxq);
2109 		return;
2110 	}
2111 
2112 	if (rxq->sw_ring != NULL) {
2113 		for (i = 0; i < rxq->nb_rx_desc; i++) {
2114 			if (rxq->sw_ring[i].mbuf != NULL) {
2115 				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
2116 				rxq->sw_ring[i].mbuf = NULL;
2117 			}
2118 		}
2119 		for (i = 0; i < rxq->rx_nb_avail; ++i) {
2120 			struct rte_mbuf *mb;
2121 
2122 			mb = rxq->rx_stage[rxq->rx_next_avail + i];
2123 			rte_pktmbuf_free_seg(mb);
2124 		}
2125 		rxq->rx_nb_avail = 0;
2126 	}
2127 
2128 	if (rxq->sw_sc_ring != NULL)
2129 		for (i = 0; i < rxq->nb_rx_desc; i++)
2130 			if (rxq->sw_sc_ring[i].fbuf != NULL) {
2131 				ngbe_free_sc_cluster(rxq->sw_sc_ring[i].fbuf);
2132 				rxq->sw_sc_ring[i].fbuf = NULL;
2133 			}
2134 }
2135 
2136 static void
2137 ngbe_rx_queue_release(struct ngbe_rx_queue *rxq)
2138 {
2139 	if (rxq != NULL) {
2140 		ngbe_rx_queue_release_mbufs(rxq);
2141 		rte_free(rxq->sw_ring);
2142 		rte_free(rxq->sw_sc_ring);
2143 		rte_memzone_free(rxq->mz);
2144 		rte_free(rxq);
2145 	}
2146 }
2147 
2148 void
2149 ngbe_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
2150 {
2151 	ngbe_rx_queue_release(dev->data->rx_queues[qid]);
2152 }
2153 
2154 /*
2155  * Check if Rx Burst Bulk Alloc function can be used.
2156  * Return
2157  *        0: the preconditions are satisfied and the bulk allocation function
2158  *           can be used.
2159  *  -EINVAL: the preconditions are NOT satisfied and the default Rx burst
2160  *           function must be used.
2161  */
2162 static inline int
2163 check_rx_burst_bulk_alloc_preconditions(struct ngbe_rx_queue *rxq)
2164 {
2165 	int ret = 0;
2166 
2167 	/*
2168 	 * Make sure the following pre-conditions are satisfied:
2169 	 *   rxq->rx_free_thresh >= RTE_PMD_NGBE_RX_MAX_BURST
2170 	 *   rxq->rx_free_thresh < rxq->nb_rx_desc
2171 	 *   (rxq->nb_rx_desc % rxq->rx_free_thresh) == 0
2172 	 * Scattered packets are not supported.  This should be checked
2173 	 * outside of this function.
2174 	 */
2175 	if (rxq->rx_free_thresh < RTE_PMD_NGBE_RX_MAX_BURST) {
2176 		PMD_INIT_LOG(DEBUG,
2177 			     "Rx Burst Bulk Alloc Preconditions: rxq->rx_free_thresh=%d, RTE_PMD_NGBE_RX_MAX_BURST=%d",
2178 			     rxq->rx_free_thresh, RTE_PMD_NGBE_RX_MAX_BURST);
2179 		ret = -EINVAL;
2180 	} else if (rxq->rx_free_thresh >= rxq->nb_rx_desc) {
2181 		PMD_INIT_LOG(DEBUG,
2182 			     "Rx Burst Bulk Alloc Preconditions: rxq->rx_free_thresh=%d, rxq->nb_rx_desc=%d",
2183 			     rxq->rx_free_thresh, rxq->nb_rx_desc);
2184 		ret = -EINVAL;
2185 	} else if ((rxq->nb_rx_desc % rxq->rx_free_thresh) != 0) {
2186 		PMD_INIT_LOG(DEBUG,
2187 			     "Rx Burst Bulk Alloc Preconditions: rxq->nb_rx_desc=%d, rxq->rx_free_thresh=%d",
2188 			     rxq->nb_rx_desc, rxq->rx_free_thresh);
2189 		ret = -EINVAL;
2190 	}
2191 
2192 	return ret;
2193 }
2194 
2195 /* Reset dynamic ngbe_rx_queue fields back to defaults */
2196 static void
2197 ngbe_reset_rx_queue(struct ngbe_adapter *adapter, struct ngbe_rx_queue *rxq)
2198 {
2199 	static const struct ngbe_rx_desc zeroed_desc = {
2200 						{{0}, {0} }, {{0}, {0} } };
2201 	unsigned int i;
2202 	uint16_t len = rxq->nb_rx_desc;
2203 
2204 	/*
2205 	 * By default, the Rx queue setup function allocates enough memory for
2206 	 * NGBE_RING_DESC_MAX.  The Rx Burst bulk allocation function requires
2207 	 * extra memory at the end of the descriptor ring to be zero'd out.
2208 	 */
2209 	if (adapter->rx_bulk_alloc_allowed)
2210 		/* zero out extra memory */
2211 		len += RTE_PMD_NGBE_RX_MAX_BURST;
2212 
2213 	/*
2214 	 * Zero out HW ring memory. Zero out extra memory at the end of
2215 	 * the H/W ring so look-ahead logic in Rx Burst bulk alloc function
2216 	 * reads extra memory as zeros.
2217 	 */
2218 	for (i = 0; i < len; i++)
2219 		rxq->rx_ring[i] = zeroed_desc;
2220 
2221 	/*
2222 	 * initialize extra software ring entries. Space for these extra
2223 	 * entries is always allocated
2224 	 */
2225 	memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
2226 	for (i = rxq->nb_rx_desc; i < len; ++i)
2227 		rxq->sw_ring[i].mbuf = &rxq->fake_mbuf;
2228 
2229 	rxq->rx_nb_avail = 0;
2230 	rxq->rx_next_avail = 0;
2231 	rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
2232 	rxq->rx_tail = 0;
2233 	rxq->nb_rx_hold = 0;
2234 	rte_pktmbuf_free(rxq->pkt_first_seg);
2235 	rxq->pkt_first_seg = NULL;
2236 	rxq->pkt_last_seg = NULL;
2237 
2238 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM)
2239 	rxq->rxrearm_start = 0;
2240 	rxq->rxrearm_nb = 0;
2241 #endif
2242 }
2243 
2244 uint64_t
2245 ngbe_get_rx_queue_offloads(struct rte_eth_dev *dev __rte_unused)
2246 {
2247 	return RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
2248 }
2249 
2250 uint64_t
2251 ngbe_get_rx_port_offloads(struct rte_eth_dev *dev)
2252 {
2253 	uint64_t offloads;
2254 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2255 
2256 	offloads = RTE_ETH_RX_OFFLOAD_IPV4_CKSUM  |
2257 		   RTE_ETH_RX_OFFLOAD_UDP_CKSUM   |
2258 		   RTE_ETH_RX_OFFLOAD_TCP_CKSUM   |
2259 		   RTE_ETH_RX_OFFLOAD_KEEP_CRC    |
2260 		   RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
2261 		   RTE_ETH_RX_OFFLOAD_RSS_HASH    |
2262 		   RTE_ETH_RX_OFFLOAD_SCATTER;
2263 
2264 	if (hw->is_pf)
2265 		offloads |= (RTE_ETH_RX_OFFLOAD_QINQ_STRIP |
2266 			     RTE_ETH_RX_OFFLOAD_VLAN_EXTEND);
2267 
2268 	return offloads;
2269 }
2270 
2271 int
2272 ngbe_dev_rx_queue_setup(struct rte_eth_dev *dev,
2273 			 uint16_t queue_idx,
2274 			 uint16_t nb_desc,
2275 			 unsigned int socket_id,
2276 			 const struct rte_eth_rxconf *rx_conf,
2277 			 struct rte_mempool *mp)
2278 {
2279 	const struct rte_memzone *rz;
2280 	struct ngbe_rx_queue *rxq;
2281 	struct ngbe_hw     *hw;
2282 	uint16_t len;
2283 	struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2284 	uint64_t offloads;
2285 
2286 	PMD_INIT_FUNC_TRACE();
2287 	hw = ngbe_dev_hw(dev);
2288 
2289 	offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
2290 
2291 	/* Free memory prior to re-allocation if needed... */
2292 	if (dev->data->rx_queues[queue_idx] != NULL) {
2293 		ngbe_rx_queue_release(dev->data->rx_queues[queue_idx]);
2294 		dev->data->rx_queues[queue_idx] = NULL;
2295 	}
2296 
2297 	/* First allocate the Rx queue data structure */
2298 	rxq = rte_zmalloc_socket("ethdev RX queue",
2299 				 sizeof(struct ngbe_rx_queue),
2300 				 RTE_CACHE_LINE_SIZE, socket_id);
2301 	if (rxq == NULL)
2302 		return -ENOMEM;
2303 	rxq->mb_pool = mp;
2304 	rxq->nb_rx_desc = nb_desc;
2305 	rxq->rx_free_thresh = rx_conf->rx_free_thresh;
2306 	rxq->queue_id = queue_idx;
2307 	rxq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2308 		queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2309 	rxq->port_id = dev->data->port_id;
2310 	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2311 		rxq->crc_len = RTE_ETHER_CRC_LEN;
2312 	else
2313 		rxq->crc_len = 0;
2314 	rxq->drop_en = rx_conf->rx_drop_en;
2315 	rxq->rx_deferred_start = rx_conf->rx_deferred_start;
2316 	rxq->offloads = offloads;
2317 
2318 	/*
2319 	 * Allocate Rx ring hardware descriptors. A memzone large enough to
2320 	 * handle the maximum ring size is allocated in order to allow for
2321 	 * resizing in later calls to the queue setup function.
2322 	 */
2323 	rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
2324 				      RX_RING_SZ, NGBE_ALIGN, socket_id);
2325 	if (rz == NULL) {
2326 		ngbe_rx_queue_release(rxq);
2327 		return -ENOMEM;
2328 	}
2329 
2330 	rxq->mz = rz;
2331 	/*
2332 	 * Zero init all the descriptors in the ring.
2333 	 */
2334 	memset(rz->addr, 0, RX_RING_SZ);
2335 
2336 	rxq->rdt_reg_addr = NGBE_REG_ADDR(hw, NGBE_RXWP(rxq->reg_idx));
2337 	rxq->rdh_reg_addr = NGBE_REG_ADDR(hw, NGBE_RXRP(rxq->reg_idx));
2338 
2339 	rxq->rx_ring_phys_addr = TMZ_PADDR(rz);
2340 	rxq->rx_ring = (struct ngbe_rx_desc *)TMZ_VADDR(rz);
2341 
2342 	/*
2343 	 * Certain constraints must be met in order to use the bulk buffer
2344 	 * allocation Rx burst function. If any of Rx queues doesn't meet them
2345 	 * the feature should be disabled for the whole port.
2346 	 */
2347 	if (check_rx_burst_bulk_alloc_preconditions(rxq)) {
2348 		PMD_INIT_LOG(DEBUG,
2349 			     "queue[%d] doesn't meet Rx Bulk Alloc preconditions - canceling the feature for the whole port[%d]",
2350 			     rxq->queue_id, rxq->port_id);
2351 		adapter->rx_bulk_alloc_allowed = false;
2352 	}
2353 
2354 	/*
2355 	 * Allocate software ring. Allow for space at the end of the
2356 	 * S/W ring to make sure look-ahead logic in bulk alloc Rx burst
2357 	 * function does not access an invalid memory region.
2358 	 */
2359 	len = nb_desc;
2360 	if (adapter->rx_bulk_alloc_allowed)
2361 		len += RTE_PMD_NGBE_RX_MAX_BURST;
2362 
2363 	rxq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
2364 					  sizeof(struct ngbe_rx_entry) * len,
2365 					  RTE_CACHE_LINE_SIZE, socket_id);
2366 	if (rxq->sw_ring == NULL) {
2367 		ngbe_rx_queue_release(rxq);
2368 		return -ENOMEM;
2369 	}
2370 
2371 	/*
2372 	 * Always allocate even if it's not going to be needed in order to
2373 	 * simplify the code.
2374 	 *
2375 	 * This ring is used in Scattered Rx cases and Scattered Rx may
2376 	 * be requested in ngbe_dev_rx_init(), which is called later from
2377 	 * dev_start() flow.
2378 	 */
2379 	rxq->sw_sc_ring =
2380 		rte_zmalloc_socket("rxq->sw_sc_ring",
2381 				  sizeof(struct ngbe_scattered_rx_entry) * len,
2382 				  RTE_CACHE_LINE_SIZE, socket_id);
2383 	if (rxq->sw_sc_ring == NULL) {
2384 		ngbe_rx_queue_release(rxq);
2385 		return -ENOMEM;
2386 	}
2387 
2388 	PMD_INIT_LOG(DEBUG,
2389 		     "sw_ring=%p sw_sc_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
2390 		     rxq->sw_ring, rxq->sw_sc_ring, rxq->rx_ring,
2391 		     rxq->rx_ring_phys_addr);
2392 
2393 	if (!rte_is_power_of_2(nb_desc)) {
2394 		PMD_INIT_LOG(DEBUG, "queue[%d] doesn't meet Vector Rx "
2395 				    "preconditions - canceling the feature for "
2396 				    "the whole port[%d]",
2397 			     rxq->queue_id, rxq->port_id);
2398 		adapter->rx_vec_allowed = false;
2399 	} else {
2400 		ngbe_rxq_vec_setup(rxq);
2401 	}
2402 
2403 	dev->data->rx_queues[queue_idx] = rxq;
2404 
2405 	ngbe_reset_rx_queue(adapter, rxq);
2406 
2407 	return 0;
2408 }
2409 
2410 uint32_t
2411 ngbe_dev_rx_queue_count(void *rx_queue)
2412 {
2413 #define NGBE_RXQ_SCAN_INTERVAL 4
2414 	volatile struct ngbe_rx_desc *rxdp;
2415 	struct ngbe_rx_queue *rxq = rx_queue;
2416 	uint32_t desc = 0;
2417 
2418 	rxdp = &rxq->rx_ring[rxq->rx_tail];
2419 
2420 	while ((desc < rxq->nb_rx_desc) &&
2421 		(rxdp->qw1.lo.status &
2422 			rte_cpu_to_le_32(NGBE_RXD_STAT_DD))) {
2423 		desc += NGBE_RXQ_SCAN_INTERVAL;
2424 		rxdp += NGBE_RXQ_SCAN_INTERVAL;
2425 		if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
2426 			rxdp = &(rxq->rx_ring[rxq->rx_tail +
2427 				desc - rxq->nb_rx_desc]);
2428 	}
2429 
2430 	return desc;
2431 }
2432 
2433 int
2434 ngbe_dev_rx_descriptor_status(void *rx_queue, uint16_t offset)
2435 {
2436 	struct ngbe_rx_queue *rxq = rx_queue;
2437 	volatile uint32_t *status;
2438 	uint32_t nb_hold, desc;
2439 
2440 	if (unlikely(offset >= rxq->nb_rx_desc))
2441 		return -EINVAL;
2442 
2443 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM)
2444 	if (rxq->rx_using_sse)
2445 		nb_hold = rxq->rxrearm_nb;
2446 	else
2447 #endif
2448 		nb_hold = rxq->nb_rx_hold;
2449 	if (offset >= rxq->nb_rx_desc - nb_hold)
2450 		return RTE_ETH_RX_DESC_UNAVAIL;
2451 
2452 	desc = rxq->rx_tail + offset;
2453 	if (desc >= rxq->nb_rx_desc)
2454 		desc -= rxq->nb_rx_desc;
2455 
2456 	status = &rxq->rx_ring[desc].qw1.lo.status;
2457 	if (*status & rte_cpu_to_le_32(NGBE_RXD_STAT_DD))
2458 		return RTE_ETH_RX_DESC_DONE;
2459 
2460 	return RTE_ETH_RX_DESC_AVAIL;
2461 }
2462 
2463 int
2464 ngbe_dev_tx_descriptor_status(void *tx_queue, uint16_t offset)
2465 {
2466 	struct ngbe_tx_queue *txq = tx_queue;
2467 	volatile uint32_t *status;
2468 	uint32_t desc;
2469 
2470 	if (unlikely(offset >= txq->nb_tx_desc))
2471 		return -EINVAL;
2472 
2473 	desc = txq->tx_tail + offset;
2474 	if (desc >= txq->nb_tx_desc) {
2475 		desc -= txq->nb_tx_desc;
2476 		if (desc >= txq->nb_tx_desc)
2477 			desc -= txq->nb_tx_desc;
2478 	}
2479 
2480 	status = &txq->tx_ring[desc].dw3;
2481 	if (*status & rte_cpu_to_le_32(NGBE_TXD_DD))
2482 		return RTE_ETH_TX_DESC_DONE;
2483 
2484 	return RTE_ETH_TX_DESC_FULL;
2485 }
2486 
2487 void
2488 ngbe_dev_clear_queues(struct rte_eth_dev *dev)
2489 {
2490 	unsigned int i;
2491 	struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2492 
2493 	PMD_INIT_FUNC_TRACE();
2494 
2495 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2496 		struct ngbe_tx_queue *txq = dev->data->tx_queues[i];
2497 
2498 		if (txq != NULL) {
2499 			txq->ops->release_mbufs(txq);
2500 			txq->ops->reset(txq);
2501 			dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
2502 		}
2503 	}
2504 
2505 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2506 		struct ngbe_rx_queue *rxq = dev->data->rx_queues[i];
2507 
2508 		if (rxq != NULL) {
2509 			ngbe_rx_queue_release_mbufs(rxq);
2510 			ngbe_reset_rx_queue(adapter, rxq);
2511 			dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
2512 		}
2513 	}
2514 }
2515 
2516 void
2517 ngbe_dev_free_queues(struct rte_eth_dev *dev)
2518 {
2519 	unsigned int i;
2520 
2521 	PMD_INIT_FUNC_TRACE();
2522 
2523 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2524 		ngbe_dev_rx_queue_release(dev, i);
2525 		dev->data->rx_queues[i] = NULL;
2526 	}
2527 	dev->data->nb_rx_queues = 0;
2528 
2529 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2530 		ngbe_dev_tx_queue_release(dev, i);
2531 		dev->data->tx_queues[i] = NULL;
2532 	}
2533 	dev->data->nb_tx_queues = 0;
2534 }
2535 
2536 /**
2537  * Receive Side Scaling (RSS)
2538  *
2539  * Principles:
2540  * The source and destination IP addresses of the IP header and the source
2541  * and destination ports of TCP/UDP headers, if any, of received packets are
2542  * hashed against a configurable random key to compute a 32-bit RSS hash result.
2543  * The seven (7) LSBs of the 32-bit hash result are used as an index into a
2544  * 128-entry redirection table (RETA).  Each entry of the RETA provides a 3-bit
2545  * RSS output index which is used as the Rx queue index where to store the
2546  * received packets.
2547  * The following output is supplied in the Rx write-back descriptor:
2548  *     - 32-bit result of the Microsoft RSS hash function,
2549  *     - 4-bit RSS type field.
2550  */
2551 
2552 /*
2553  * Used as the default key.
2554  */
2555 static uint8_t rss_intel_key[40] = {
2556 	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
2557 	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
2558 	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
2559 	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
2560 	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
2561 };
2562 
2563 static void
2564 ngbe_rss_disable(struct rte_eth_dev *dev)
2565 {
2566 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2567 
2568 	wr32m(hw, NGBE_RACTL, NGBE_RACTL_RSSENA, 0);
2569 }
2570 
2571 int
2572 ngbe_dev_rss_hash_update(struct rte_eth_dev *dev,
2573 			  struct rte_eth_rss_conf *rss_conf)
2574 {
2575 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2576 	uint8_t  *hash_key;
2577 	uint32_t mrqc;
2578 	uint32_t rss_key;
2579 	uint64_t rss_hf;
2580 	uint16_t i;
2581 
2582 	if (!hw->is_pf) {
2583 		PMD_DRV_LOG(ERR, "RSS hash update is not supported on this "
2584 			"NIC.");
2585 		return -ENOTSUP;
2586 	}
2587 
2588 	hash_key = rss_conf->rss_key;
2589 	if (hash_key) {
2590 		/* Fill in RSS hash key */
2591 		for (i = 0; i < 10; i++) {
2592 			rss_key  = LS32(hash_key[(i * 4) + 0], 0, 0xFF);
2593 			rss_key |= LS32(hash_key[(i * 4) + 1], 8, 0xFF);
2594 			rss_key |= LS32(hash_key[(i * 4) + 2], 16, 0xFF);
2595 			rss_key |= LS32(hash_key[(i * 4) + 3], 24, 0xFF);
2596 			wr32a(hw, NGBE_REG_RSSKEY, i, rss_key);
2597 		}
2598 	}
2599 
2600 	/* Set configured hashing protocols */
2601 	rss_hf = rss_conf->rss_hf & NGBE_RSS_OFFLOAD_ALL;
2602 
2603 	mrqc = rd32(hw, NGBE_RACTL);
2604 	mrqc &= ~NGBE_RACTL_RSSMASK;
2605 	if (rss_hf & RTE_ETH_RSS_IPV4)
2606 		mrqc |= NGBE_RACTL_RSSIPV4;
2607 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
2608 		mrqc |= NGBE_RACTL_RSSIPV4TCP;
2609 	if (rss_hf & RTE_ETH_RSS_IPV6 ||
2610 	    rss_hf & RTE_ETH_RSS_IPV6_EX)
2611 		mrqc |= NGBE_RACTL_RSSIPV6;
2612 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP ||
2613 	    rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
2614 		mrqc |= NGBE_RACTL_RSSIPV6TCP;
2615 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_UDP)
2616 		mrqc |= NGBE_RACTL_RSSIPV4UDP;
2617 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_UDP ||
2618 	    rss_hf & RTE_ETH_RSS_IPV6_UDP_EX)
2619 		mrqc |= NGBE_RACTL_RSSIPV6UDP;
2620 
2621 	if (rss_hf)
2622 		mrqc |= NGBE_RACTL_RSSENA;
2623 	else
2624 		mrqc &= ~NGBE_RACTL_RSSENA;
2625 
2626 	wr32(hw, NGBE_RACTL, mrqc);
2627 
2628 	return 0;
2629 }
2630 
2631 int
2632 ngbe_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
2633 			    struct rte_eth_rss_conf *rss_conf)
2634 {
2635 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2636 	uint8_t *hash_key;
2637 	uint32_t mrqc;
2638 	uint32_t rss_key;
2639 	uint64_t rss_hf;
2640 	uint16_t i;
2641 
2642 	hash_key = rss_conf->rss_key;
2643 	if (hash_key) {
2644 		/* Return RSS hash key */
2645 		for (i = 0; i < 10; i++) {
2646 			rss_key = rd32a(hw, NGBE_REG_RSSKEY, i);
2647 			hash_key[(i * 4) + 0] = RS32(rss_key, 0, 0xFF);
2648 			hash_key[(i * 4) + 1] = RS32(rss_key, 8, 0xFF);
2649 			hash_key[(i * 4) + 2] = RS32(rss_key, 16, 0xFF);
2650 			hash_key[(i * 4) + 3] = RS32(rss_key, 24, 0xFF);
2651 		}
2652 	}
2653 
2654 	rss_hf = 0;
2655 
2656 	mrqc = rd32(hw, NGBE_RACTL);
2657 	if (mrqc & NGBE_RACTL_RSSIPV4)
2658 		rss_hf |= RTE_ETH_RSS_IPV4;
2659 	if (mrqc & NGBE_RACTL_RSSIPV4TCP)
2660 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
2661 	if (mrqc & NGBE_RACTL_RSSIPV6)
2662 		rss_hf |= RTE_ETH_RSS_IPV6 |
2663 			  RTE_ETH_RSS_IPV6_EX;
2664 	if (mrqc & NGBE_RACTL_RSSIPV6TCP)
2665 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP |
2666 			  RTE_ETH_RSS_IPV6_TCP_EX;
2667 	if (mrqc & NGBE_RACTL_RSSIPV4UDP)
2668 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_UDP;
2669 	if (mrqc & NGBE_RACTL_RSSIPV6UDP)
2670 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_UDP |
2671 			  RTE_ETH_RSS_IPV6_UDP_EX;
2672 	if (!(mrqc & NGBE_RACTL_RSSENA))
2673 		rss_hf = 0;
2674 
2675 	rss_hf &= NGBE_RSS_OFFLOAD_ALL;
2676 
2677 	rss_conf->rss_hf = rss_hf;
2678 	return 0;
2679 }
2680 
2681 static void
2682 ngbe_rss_configure(struct rte_eth_dev *dev)
2683 {
2684 	struct rte_eth_rss_conf rss_conf;
2685 	struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2686 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2687 	uint32_t reta;
2688 	uint16_t i;
2689 	uint16_t j;
2690 
2691 	PMD_INIT_FUNC_TRACE();
2692 
2693 	/*
2694 	 * Fill in redirection table
2695 	 * The byte-swap is needed because NIC registers are in
2696 	 * little-endian order.
2697 	 */
2698 	if (adapter->rss_reta_updated == 0) {
2699 		reta = 0;
2700 		for (i = 0, j = 0; i < RTE_ETH_RSS_RETA_SIZE_128; i++, j++) {
2701 			if (j == dev->data->nb_rx_queues)
2702 				j = 0;
2703 			reta = (reta >> 8) | LS32(j, 24, 0xFF);
2704 			if ((i & 3) == 3)
2705 				wr32a(hw, NGBE_REG_RSSTBL, i >> 2, reta);
2706 		}
2707 	}
2708 	/*
2709 	 * Configure the RSS key and the RSS protocols used to compute
2710 	 * the RSS hash of input packets.
2711 	 */
2712 	rss_conf = dev->data->dev_conf.rx_adv_conf.rss_conf;
2713 	if (rss_conf.rss_key == NULL)
2714 		rss_conf.rss_key = rss_intel_key; /* Default hash key */
2715 	ngbe_dev_rss_hash_update(dev, &rss_conf);
2716 }
2717 
2718 void ngbe_configure_port(struct rte_eth_dev *dev)
2719 {
2720 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
2721 	int i = 0;
2722 	uint16_t tpids[8] = {RTE_ETHER_TYPE_VLAN, RTE_ETHER_TYPE_QINQ,
2723 				0x9100, 0x9200,
2724 				0x0000, 0x0000,
2725 				0x0000, 0x0000};
2726 
2727 	PMD_INIT_FUNC_TRACE();
2728 
2729 	/* default outer vlan tpid */
2730 	wr32(hw, NGBE_EXTAG,
2731 		NGBE_EXTAG_ETAG(RTE_ETHER_TYPE_ETAG) |
2732 		NGBE_EXTAG_VLAN(RTE_ETHER_TYPE_QINQ));
2733 
2734 	/* default inner vlan tpid */
2735 	wr32m(hw, NGBE_VLANCTL,
2736 		NGBE_VLANCTL_TPID_MASK,
2737 		NGBE_VLANCTL_TPID(RTE_ETHER_TYPE_VLAN));
2738 	wr32m(hw, NGBE_DMATXCTRL,
2739 		NGBE_DMATXCTRL_TPID_MASK,
2740 		NGBE_DMATXCTRL_TPID(RTE_ETHER_TYPE_VLAN));
2741 
2742 	/* default vlan tpid filters */
2743 	for (i = 0; i < 8; i++) {
2744 		wr32m(hw, NGBE_TAGTPID(i / 2),
2745 			(i % 2 ? NGBE_TAGTPID_MSB_MASK
2746 			       : NGBE_TAGTPID_LSB_MASK),
2747 			(i % 2 ? NGBE_TAGTPID_MSB(tpids[i])
2748 			       : NGBE_TAGTPID_LSB(tpids[i])));
2749 	}
2750 }
2751 
2752 static int
2753 ngbe_alloc_rx_queue_mbufs(struct ngbe_rx_queue *rxq)
2754 {
2755 	struct ngbe_rx_entry *rxe = rxq->sw_ring;
2756 	uint64_t dma_addr;
2757 	unsigned int i;
2758 
2759 	/* Initialize software ring entries */
2760 	for (i = 0; i < rxq->nb_rx_desc; i++) {
2761 		/* the ring can also be modified by hardware */
2762 		volatile struct ngbe_rx_desc *rxd;
2763 		struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
2764 
2765 		if (mbuf == NULL) {
2766 			PMD_INIT_LOG(ERR, "Rx mbuf alloc failed queue_id=%u port_id=%u",
2767 				     (unsigned int)rxq->queue_id,
2768 				     (unsigned int)rxq->port_id);
2769 			return -ENOMEM;
2770 		}
2771 
2772 		mbuf->data_off = RTE_PKTMBUF_HEADROOM;
2773 		mbuf->port = rxq->port_id;
2774 
2775 		dma_addr =
2776 			rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
2777 		rxd = &rxq->rx_ring[i];
2778 		NGBE_RXD_HDRADDR(rxd, 0);
2779 		NGBE_RXD_PKTADDR(rxd, dma_addr);
2780 		rxe[i].mbuf = mbuf;
2781 	}
2782 
2783 	return 0;
2784 }
2785 
2786 static int
2787 ngbe_dev_mq_rx_configure(struct rte_eth_dev *dev)
2788 {
2789 	if (RTE_ETH_DEV_SRIOV(dev).active == 0) {
2790 		switch (dev->data->dev_conf.rxmode.mq_mode) {
2791 		case RTE_ETH_MQ_RX_RSS:
2792 			ngbe_rss_configure(dev);
2793 			break;
2794 
2795 		case RTE_ETH_MQ_RX_NONE:
2796 		default:
2797 			/* if mq_mode is none, disable rss mode.*/
2798 			ngbe_rss_disable(dev);
2799 			break;
2800 		}
2801 	}
2802 
2803 	return 0;
2804 }
2805 
2806 void
2807 ngbe_set_rx_function(struct rte_eth_dev *dev)
2808 {
2809 	uint16_t i, rx_using_sse;
2810 	struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2811 
2812 	/*
2813 	 * In order to allow Vector Rx there are a few configuration
2814 	 * conditions to be met and Rx Bulk Allocation should be allowed.
2815 	 */
2816 	if (ngbe_rx_vec_dev_conf_condition_check(dev) ||
2817 	    !adapter->rx_bulk_alloc_allowed ||
2818 			rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128) {
2819 		PMD_INIT_LOG(DEBUG,
2820 			     "Port[%d] doesn't meet Vector Rx preconditions",
2821 			     dev->data->port_id);
2822 		adapter->rx_vec_allowed = false;
2823 	}
2824 
2825 	if (dev->data->scattered_rx) {
2826 		/*
2827 		 * Set the scattered callback: there are bulk and
2828 		 * single allocation versions.
2829 		 */
2830 		if (adapter->rx_vec_allowed) {
2831 			PMD_INIT_LOG(DEBUG,
2832 				     "Using Vector Scattered Rx callback (port=%d).",
2833 				     dev->data->port_id);
2834 			dev->rx_pkt_burst = ngbe_recv_scattered_pkts_vec;
2835 		} else if (adapter->rx_bulk_alloc_allowed) {
2836 			PMD_INIT_LOG(DEBUG, "Using a Scattered with bulk "
2837 					   "allocation callback (port=%d).",
2838 				     dev->data->port_id);
2839 			dev->rx_pkt_burst = ngbe_recv_pkts_sc_bulk_alloc;
2840 		} else {
2841 			PMD_INIT_LOG(DEBUG, "Using Regular (non-vector, "
2842 					    "single allocation) "
2843 					    "Scattered Rx callback "
2844 					    "(port=%d).",
2845 				     dev->data->port_id);
2846 
2847 			dev->rx_pkt_burst = ngbe_recv_pkts_sc_single_alloc;
2848 		}
2849 	/*
2850 	 * Below we set "simple" callbacks according to port/queues parameters.
2851 	 * If parameters allow we are going to choose between the following
2852 	 * callbacks:
2853 	 *    - Vector
2854 	 *    - Bulk Allocation
2855 	 *    - Single buffer allocation (the simplest one)
2856 	 */
2857 	} else if (adapter->rx_vec_allowed) {
2858 		PMD_INIT_LOG(DEBUG, "Vector rx enabled, please make sure Rx "
2859 				    "burst size no less than %d (port=%d).",
2860 			     RTE_NGBE_DESCS_PER_LOOP,
2861 			     dev->data->port_id);
2862 		dev->rx_pkt_burst = ngbe_recv_pkts_vec;
2863 	} else if (adapter->rx_bulk_alloc_allowed) {
2864 		PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
2865 				    "satisfied. Rx Burst Bulk Alloc function "
2866 				    "will be used on port=%d.",
2867 			     dev->data->port_id);
2868 
2869 		dev->rx_pkt_burst = ngbe_recv_pkts_bulk_alloc;
2870 	} else {
2871 		PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are not "
2872 				    "satisfied, or Scattered Rx is requested "
2873 				    "(port=%d).",
2874 			     dev->data->port_id);
2875 
2876 		dev->rx_pkt_burst = ngbe_recv_pkts;
2877 	}
2878 
2879 	rx_using_sse = (dev->rx_pkt_burst == ngbe_recv_scattered_pkts_vec ||
2880 			dev->rx_pkt_burst == ngbe_recv_pkts_vec);
2881 
2882 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2883 		struct ngbe_rx_queue *rxq = dev->data->rx_queues[i];
2884 
2885 		rxq->rx_using_sse = rx_using_sse;
2886 	}
2887 }
2888 
2889 static const struct {
2890 	eth_rx_burst_t pkt_burst;
2891 	const char *info;
2892 } ngbe_rx_burst_infos[] = {
2893 	{ ngbe_recv_pkts_sc_single_alloc,    "Scalar Scattered"},
2894 	{ ngbe_recv_pkts_sc_bulk_alloc,      "Scalar Scattered Bulk Alloc"},
2895 	{ ngbe_recv_pkts_bulk_alloc,         "Scalar Bulk Alloc"},
2896 	{ ngbe_recv_pkts,                    "Scalar"},
2897 #ifdef RTE_ARCH_X86
2898 	{ ngbe_recv_scattered_pkts_vec,      "Vector SSE Scattered" },
2899 	{ ngbe_recv_pkts_vec,                "Vector SSE" },
2900 #elif defined(RTE_ARCH_ARM64)
2901 	{ ngbe_recv_scattered_pkts_vec,      "Vector Neon Scattered" },
2902 	{ ngbe_recv_pkts_vec,                "Vector Neon" },
2903 #endif
2904 };
2905 
2906 int
2907 ngbe_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2908 		      struct rte_eth_burst_mode *mode)
2909 {
2910 	eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2911 	int ret = -EINVAL;
2912 	unsigned int i;
2913 
2914 	for (i = 0; i < RTE_DIM(ngbe_rx_burst_infos); ++i) {
2915 		if (pkt_burst == ngbe_rx_burst_infos[i].pkt_burst) {
2916 			snprintf(mode->info, sizeof(mode->info), "%s",
2917 				 ngbe_rx_burst_infos[i].info);
2918 			ret = 0;
2919 			break;
2920 		}
2921 	}
2922 
2923 	return ret;
2924 }
2925 
2926 /*
2927  * Initializes Receive Unit.
2928  */
2929 int
2930 ngbe_dev_rx_init(struct rte_eth_dev *dev)
2931 {
2932 	struct ngbe_hw *hw;
2933 	struct ngbe_rx_queue *rxq;
2934 	uint64_t bus_addr;
2935 	uint32_t fctrl;
2936 	uint32_t hlreg0;
2937 	uint32_t srrctl;
2938 	uint32_t rdrxctl;
2939 	uint32_t rxcsum;
2940 	uint16_t buf_size;
2941 	uint16_t i;
2942 	struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
2943 
2944 	PMD_INIT_FUNC_TRACE();
2945 	hw = ngbe_dev_hw(dev);
2946 
2947 	/*
2948 	 * Make sure receives are disabled while setting
2949 	 * up the Rx context (registers, descriptor rings, etc.).
2950 	 */
2951 
2952 	if (!(hw->ncsi_enabled || hw->wol_enabled))
2953 		wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, 0);
2954 
2955 	wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, 0);
2956 
2957 	/* Enable receipt of broadcasted frames */
2958 	fctrl = rd32(hw, NGBE_PSRCTL);
2959 	fctrl |= NGBE_PSRCTL_BCA;
2960 	wr32(hw, NGBE_PSRCTL, fctrl);
2961 
2962 	/*
2963 	 * Configure CRC stripping, if any.
2964 	 */
2965 	hlreg0 = rd32(hw, NGBE_SECRXCTL);
2966 	if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2967 		hlreg0 &= ~NGBE_SECRXCTL_CRCSTRIP;
2968 	else
2969 		hlreg0 |= NGBE_SECRXCTL_CRCSTRIP;
2970 	hlreg0 &= ~NGBE_SECRXCTL_XDSA;
2971 	wr32(hw, NGBE_SECRXCTL, hlreg0);
2972 
2973 	/*
2974 	 * Configure jumbo frame support, if any.
2975 	 */
2976 	wr32m(hw, NGBE_FRMSZ, NGBE_FRMSZ_MAX_MASK,
2977 		NGBE_FRMSZ_MAX(dev->data->mtu + NGBE_ETH_OVERHEAD));
2978 
2979 	/*
2980 	 * If loopback mode is configured, set LPBK bit.
2981 	 */
2982 	hlreg0 = rd32(hw, NGBE_PSRCTL);
2983 	if (hw->is_pf && dev->data->dev_conf.lpbk_mode)
2984 		hlreg0 |= NGBE_PSRCTL_LBENA;
2985 	else
2986 		hlreg0 &= ~NGBE_PSRCTL_LBENA;
2987 
2988 	wr32(hw, NGBE_PSRCTL, hlreg0);
2989 
2990 	/*
2991 	 * Assume no header split and no VLAN strip support
2992 	 * on any Rx queue first .
2993 	 */
2994 	rx_conf->offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
2995 
2996 	/* Setup Rx queues */
2997 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2998 		rxq = dev->data->rx_queues[i];
2999 
3000 		/*
3001 		 * Reset crc_len in case it was changed after queue setup by a
3002 		 * call to configure.
3003 		 */
3004 		if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
3005 			rxq->crc_len = RTE_ETHER_CRC_LEN;
3006 		else
3007 			rxq->crc_len = 0;
3008 
3009 		/* Setup the Base and Length of the Rx Descriptor Rings */
3010 		bus_addr = rxq->rx_ring_phys_addr;
3011 		wr32(hw, NGBE_RXBAL(rxq->reg_idx),
3012 				(uint32_t)(bus_addr & BIT_MASK32));
3013 		wr32(hw, NGBE_RXBAH(rxq->reg_idx),
3014 				(uint32_t)(bus_addr >> 32));
3015 		wr32(hw, NGBE_RXRP(rxq->reg_idx), 0);
3016 		wr32(hw, NGBE_RXWP(rxq->reg_idx), 0);
3017 
3018 		srrctl = NGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
3019 
3020 		/* Set if packets are dropped when no descriptors available */
3021 		if (rxq->drop_en)
3022 			srrctl |= NGBE_RXCFG_DROP;
3023 
3024 		/*
3025 		 * Configure the Rx buffer size in the PKTLEN field of
3026 		 * the RXCFG register of the queue.
3027 		 * The value is in 1 KB resolution. Valid values can be from
3028 		 * 1 KB to 16 KB.
3029 		 */
3030 		buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
3031 			RTE_PKTMBUF_HEADROOM);
3032 		buf_size = ROUND_DOWN(buf_size, 0x1 << 10);
3033 		srrctl |= NGBE_RXCFG_PKTLEN(buf_size);
3034 
3035 		wr32(hw, NGBE_RXCFG(rxq->reg_idx), srrctl);
3036 
3037 		/* It adds dual VLAN length for supporting dual VLAN */
3038 		if (dev->data->mtu + NGBE_ETH_OVERHEAD +
3039 				2 * RTE_VLAN_HLEN > buf_size)
3040 			dev->data->scattered_rx = 1;
3041 		if (rxq->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP)
3042 			rx_conf->offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
3043 	}
3044 
3045 	if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_SCATTER)
3046 		dev->data->scattered_rx = 1;
3047 
3048 	/*
3049 	 * Device configured with multiple RX queues.
3050 	 */
3051 	ngbe_dev_mq_rx_configure(dev);
3052 
3053 	/*
3054 	 * Setup the Checksum Register.
3055 	 * Disable Full-Packet Checksum which is mutually exclusive with RSS.
3056 	 * Enable IP/L4 checksum computation by hardware if requested to do so.
3057 	 */
3058 	rxcsum = rd32(hw, NGBE_PSRCTL);
3059 	rxcsum |= NGBE_PSRCTL_PCSD;
3060 	if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM)
3061 		rxcsum |= NGBE_PSRCTL_L4CSUM;
3062 	else
3063 		rxcsum &= ~NGBE_PSRCTL_L4CSUM;
3064 
3065 	wr32(hw, NGBE_PSRCTL, rxcsum);
3066 
3067 	if (hw->is_pf) {
3068 		rdrxctl = rd32(hw, NGBE_SECRXCTL);
3069 		if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
3070 			rdrxctl &= ~NGBE_SECRXCTL_CRCSTRIP;
3071 		else
3072 			rdrxctl |= NGBE_SECRXCTL_CRCSTRIP;
3073 		wr32(hw, NGBE_SECRXCTL, rdrxctl);
3074 	}
3075 
3076 	ngbe_set_rx_function(dev);
3077 
3078 	return 0;
3079 }
3080 
3081 /*
3082  * Initializes Transmit Unit.
3083  */
3084 void
3085 ngbe_dev_tx_init(struct rte_eth_dev *dev)
3086 {
3087 	struct ngbe_hw     *hw;
3088 	struct ngbe_tx_queue *txq;
3089 	uint64_t bus_addr;
3090 	uint16_t i;
3091 
3092 	PMD_INIT_FUNC_TRACE();
3093 	hw = ngbe_dev_hw(dev);
3094 
3095 	wr32m(hw, NGBE_SECTXCTL, NGBE_SECTXCTL_ODSA, NGBE_SECTXCTL_ODSA);
3096 	wr32m(hw, NGBE_SECTXCTL, NGBE_SECTXCTL_XDSA, 0);
3097 
3098 	/* Setup the Base and Length of the Tx Descriptor Rings */
3099 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
3100 		txq = dev->data->tx_queues[i];
3101 
3102 		bus_addr = txq->tx_ring_phys_addr;
3103 		wr32(hw, NGBE_TXBAL(txq->reg_idx),
3104 				(uint32_t)(bus_addr & BIT_MASK32));
3105 		wr32(hw, NGBE_TXBAH(txq->reg_idx),
3106 				(uint32_t)(bus_addr >> 32));
3107 		wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_BUFLEN_MASK,
3108 			NGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
3109 		/* Setup the HW Tx Head and TX Tail descriptor pointers */
3110 		wr32(hw, NGBE_TXRP(txq->reg_idx), 0);
3111 		wr32(hw, NGBE_TXWP(txq->reg_idx), 0);
3112 	}
3113 }
3114 
3115 /*
3116  * Set up link loopback mode Tx->Rx.
3117  */
3118 static inline void
3119 ngbe_setup_loopback_link(struct ngbe_hw *hw)
3120 {
3121 	PMD_INIT_FUNC_TRACE();
3122 
3123 	wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_LB, NGBE_MACRXCFG_LB);
3124 
3125 	msec_delay(50);
3126 }
3127 
3128 /*
3129  * Start Transmit and Receive Units.
3130  */
3131 int
3132 ngbe_dev_rxtx_start(struct rte_eth_dev *dev)
3133 {
3134 	struct ngbe_hw     *hw;
3135 	struct ngbe_tx_queue *txq;
3136 	struct ngbe_rx_queue *rxq;
3137 	uint32_t dmatxctl;
3138 	uint32_t rxctrl;
3139 	uint16_t i;
3140 	int ret = 0;
3141 
3142 	PMD_INIT_FUNC_TRACE();
3143 	hw = ngbe_dev_hw(dev);
3144 
3145 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
3146 		txq = dev->data->tx_queues[i];
3147 		/* Setup Transmit Threshold Registers */
3148 		wr32m(hw, NGBE_TXCFG(txq->reg_idx),
3149 		      NGBE_TXCFG_HTHRESH_MASK |
3150 		      NGBE_TXCFG_WTHRESH_MASK,
3151 		      NGBE_TXCFG_HTHRESH(txq->hthresh) |
3152 		      NGBE_TXCFG_WTHRESH(txq->wthresh));
3153 	}
3154 
3155 	dmatxctl = rd32(hw, NGBE_DMATXCTRL);
3156 	dmatxctl |= NGBE_DMATXCTRL_ENA;
3157 	wr32(hw, NGBE_DMATXCTRL, dmatxctl);
3158 
3159 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
3160 		txq = dev->data->tx_queues[i];
3161 		if (txq->tx_deferred_start == 0) {
3162 			ret = ngbe_dev_tx_queue_start(dev, i);
3163 			if (ret < 0)
3164 				return ret;
3165 		}
3166 	}
3167 
3168 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
3169 		rxq = dev->data->rx_queues[i];
3170 		if (rxq->rx_deferred_start == 0) {
3171 			ret = ngbe_dev_rx_queue_start(dev, i);
3172 			if (ret < 0)
3173 				return ret;
3174 		}
3175 	}
3176 
3177 	/* Enable Receive engine */
3178 	rxctrl = rd32(hw, NGBE_PBRXCTL);
3179 	rxctrl |= NGBE_PBRXCTL_ENA;
3180 	hw->mac.enable_rx_dma(hw, rxctrl);
3181 
3182 	/* If loopback mode is enabled, set up the link accordingly */
3183 	if (hw->is_pf && dev->data->dev_conf.lpbk_mode)
3184 		ngbe_setup_loopback_link(hw);
3185 
3186 	return 0;
3187 }
3188 
3189 void
3190 ngbe_dev_save_rx_queue(struct ngbe_hw *hw, uint16_t rx_queue_id)
3191 {
3192 	u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
3193 	*(reg++) = rd32(hw, NGBE_RXBAL(rx_queue_id));
3194 	*(reg++) = rd32(hw, NGBE_RXBAH(rx_queue_id));
3195 	*(reg++) = rd32(hw, NGBE_RXCFG(rx_queue_id));
3196 }
3197 
3198 void
3199 ngbe_dev_store_rx_queue(struct ngbe_hw *hw, uint16_t rx_queue_id)
3200 {
3201 	u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
3202 	wr32(hw, NGBE_RXBAL(rx_queue_id), *(reg++));
3203 	wr32(hw, NGBE_RXBAH(rx_queue_id), *(reg++));
3204 	wr32(hw, NGBE_RXCFG(rx_queue_id), *(reg++) & ~NGBE_RXCFG_ENA);
3205 }
3206 
3207 void
3208 ngbe_dev_save_tx_queue(struct ngbe_hw *hw, uint16_t tx_queue_id)
3209 {
3210 	u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
3211 	*(reg++) = rd32(hw, NGBE_TXBAL(tx_queue_id));
3212 	*(reg++) = rd32(hw, NGBE_TXBAH(tx_queue_id));
3213 	*(reg++) = rd32(hw, NGBE_TXCFG(tx_queue_id));
3214 }
3215 
3216 void
3217 ngbe_dev_store_tx_queue(struct ngbe_hw *hw, uint16_t tx_queue_id)
3218 {
3219 	u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
3220 	wr32(hw, NGBE_TXBAL(tx_queue_id), *(reg++));
3221 	wr32(hw, NGBE_TXBAH(tx_queue_id), *(reg++));
3222 	wr32(hw, NGBE_TXCFG(tx_queue_id), *(reg++) & ~NGBE_TXCFG_ENA);
3223 }
3224 
3225 /*
3226  * Start Receive Units for specified queue.
3227  */
3228 int
3229 ngbe_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
3230 {
3231 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
3232 	struct ngbe_rx_queue *rxq;
3233 	uint32_t rxdctl;
3234 	int poll_ms;
3235 
3236 	PMD_INIT_FUNC_TRACE();
3237 
3238 	rxq = dev->data->rx_queues[rx_queue_id];
3239 
3240 	/* Allocate buffers for descriptor rings */
3241 	if (ngbe_alloc_rx_queue_mbufs(rxq) != 0) {
3242 		PMD_INIT_LOG(ERR, "Could not alloc mbuf for queue:%d",
3243 			     rx_queue_id);
3244 		return -1;
3245 	}
3246 	rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
3247 	rxdctl |= NGBE_RXCFG_ENA;
3248 	wr32(hw, NGBE_RXCFG(rxq->reg_idx), rxdctl);
3249 
3250 	/* Wait until Rx Enable ready */
3251 	poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
3252 	do {
3253 		rte_delay_ms(1);
3254 		rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
3255 	} while (--poll_ms && !(rxdctl & NGBE_RXCFG_ENA));
3256 	if (poll_ms == 0)
3257 		PMD_INIT_LOG(ERR, "Could not enable Rx Queue %d", rx_queue_id);
3258 	rte_wmb();
3259 	wr32(hw, NGBE_RXRP(rxq->reg_idx), 0);
3260 	wr32(hw, NGBE_RXWP(rxq->reg_idx), rxq->nb_rx_desc - 1);
3261 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
3262 
3263 	return 0;
3264 }
3265 
3266 /*
3267  * Stop Receive Units for specified queue.
3268  */
3269 int
3270 ngbe_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
3271 {
3272 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
3273 	struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
3274 	struct ngbe_rx_queue *rxq;
3275 	uint32_t rxdctl;
3276 	int poll_ms;
3277 
3278 	PMD_INIT_FUNC_TRACE();
3279 
3280 	rxq = dev->data->rx_queues[rx_queue_id];
3281 
3282 	ngbe_dev_save_rx_queue(hw, rxq->reg_idx);
3283 	wr32m(hw, NGBE_RXCFG(rxq->reg_idx), NGBE_RXCFG_ENA, 0);
3284 
3285 	/* Wait until Rx Enable bit clear */
3286 	poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
3287 	do {
3288 		rte_delay_ms(1);
3289 		rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
3290 	} while (--poll_ms && (rxdctl & NGBE_RXCFG_ENA));
3291 	if (poll_ms == 0)
3292 		PMD_INIT_LOG(ERR, "Could not disable Rx Queue %d", rx_queue_id);
3293 
3294 	rte_delay_us(RTE_NGBE_WAIT_100_US);
3295 	ngbe_dev_store_rx_queue(hw, rxq->reg_idx);
3296 
3297 	ngbe_rx_queue_release_mbufs(rxq);
3298 	ngbe_reset_rx_queue(adapter, rxq);
3299 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
3300 
3301 	return 0;
3302 }
3303 
3304 /*
3305  * Start Transmit Units for specified queue.
3306  */
3307 int
3308 ngbe_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
3309 {
3310 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
3311 	struct ngbe_tx_queue *txq;
3312 	uint32_t txdctl;
3313 	int poll_ms;
3314 
3315 	PMD_INIT_FUNC_TRACE();
3316 
3317 	txq = dev->data->tx_queues[tx_queue_id];
3318 	wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_ENA, NGBE_TXCFG_ENA);
3319 
3320 	/* Wait until Tx Enable ready */
3321 	poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
3322 	do {
3323 		rte_delay_ms(1);
3324 		txdctl = rd32(hw, NGBE_TXCFG(txq->reg_idx));
3325 	} while (--poll_ms && !(txdctl & NGBE_TXCFG_ENA));
3326 	if (poll_ms == 0)
3327 		PMD_INIT_LOG(ERR, "Could not enable Tx Queue %d",
3328 			     tx_queue_id);
3329 
3330 	rte_wmb();
3331 	wr32(hw, NGBE_TXWP(txq->reg_idx), txq->tx_tail);
3332 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
3333 
3334 	return 0;
3335 }
3336 
3337 /*
3338  * Stop Transmit Units for specified queue.
3339  */
3340 int
3341 ngbe_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
3342 {
3343 	struct ngbe_hw *hw = ngbe_dev_hw(dev);
3344 	struct ngbe_tx_queue *txq;
3345 	uint32_t txdctl;
3346 	uint32_t txtdh, txtdt;
3347 	int poll_ms;
3348 
3349 	PMD_INIT_FUNC_TRACE();
3350 
3351 	txq = dev->data->tx_queues[tx_queue_id];
3352 
3353 	/* Wait until Tx queue is empty */
3354 	poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
3355 	do {
3356 		rte_delay_us(RTE_NGBE_WAIT_100_US);
3357 		txtdh = rd32(hw, NGBE_TXRP(txq->reg_idx));
3358 		txtdt = rd32(hw, NGBE_TXWP(txq->reg_idx));
3359 	} while (--poll_ms && (txtdh != txtdt));
3360 	if (poll_ms == 0)
3361 		PMD_INIT_LOG(ERR, "Tx Queue %d is not empty when stopping.",
3362 			     tx_queue_id);
3363 
3364 	ngbe_dev_save_tx_queue(hw, txq->reg_idx);
3365 	wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_ENA, 0);
3366 
3367 	/* Wait until Tx Enable bit clear */
3368 	poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
3369 	do {
3370 		rte_delay_ms(1);
3371 		txdctl = rd32(hw, NGBE_TXCFG(txq->reg_idx));
3372 	} while (--poll_ms && (txdctl & NGBE_TXCFG_ENA));
3373 	if (poll_ms == 0)
3374 		PMD_INIT_LOG(ERR, "Could not disable Tx Queue %d",
3375 			     tx_queue_id);
3376 
3377 	rte_delay_us(RTE_NGBE_WAIT_100_US);
3378 	ngbe_dev_store_tx_queue(hw, txq->reg_idx);
3379 
3380 	if (txq->ops != NULL) {
3381 		txq->ops->release_mbufs(txq);
3382 		txq->ops->reset(txq);
3383 	}
3384 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
3385 
3386 	return 0;
3387 }
3388 
3389 void
3390 ngbe_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3391 	struct rte_eth_rxq_info *qinfo)
3392 {
3393 	struct ngbe_rx_queue *rxq;
3394 
3395 	rxq = dev->data->rx_queues[queue_id];
3396 
3397 	qinfo->mp = rxq->mb_pool;
3398 	qinfo->scattered_rx = dev->data->scattered_rx;
3399 	qinfo->nb_desc = rxq->nb_rx_desc;
3400 
3401 	qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
3402 	qinfo->conf.rx_drop_en = rxq->drop_en;
3403 	qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
3404 	qinfo->conf.offloads = rxq->offloads;
3405 }
3406 
3407 void
3408 ngbe_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3409 	struct rte_eth_txq_info *qinfo)
3410 {
3411 	struct ngbe_tx_queue *txq;
3412 
3413 	txq = dev->data->tx_queues[queue_id];
3414 
3415 	qinfo->nb_desc = txq->nb_tx_desc;
3416 
3417 	qinfo->conf.tx_thresh.pthresh = txq->pthresh;
3418 	qinfo->conf.tx_thresh.hthresh = txq->hthresh;
3419 	qinfo->conf.tx_thresh.wthresh = txq->wthresh;
3420 
3421 	qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
3422 	qinfo->conf.offloads = txq->offloads;
3423 	qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
3424 }
3425 
3426 /* Stubs needed for linkage when RTE_ARCH_PPC_64, RTE_ARCH_RISCV or
3427  * RTE_ARCH_LOONGARCH is set.
3428  */
3429 #if defined(RTE_ARCH_PPC_64) || defined(RTE_ARCH_RISCV) || \
3430 	defined(RTE_ARCH_LOONGARCH)
3431 int
3432 ngbe_rx_vec_dev_conf_condition_check(__rte_unused struct rte_eth_dev *dev)
3433 {
3434 	return -1;
3435 }
3436 
3437 uint16_t
3438 ngbe_recv_pkts_vec(__rte_unused void *rx_queue,
3439 		   __rte_unused struct rte_mbuf **rx_pkts,
3440 		   __rte_unused uint16_t nb_pkts)
3441 {
3442 	return 0;
3443 }
3444 
3445 uint16_t
3446 ngbe_recv_scattered_pkts_vec(__rte_unused void *rx_queue,
3447 			     __rte_unused struct rte_mbuf **rx_pkts,
3448 			     __rte_unused uint16_t nb_pkts)
3449 {
3450 	return 0;
3451 }
3452 
3453 int
3454 ngbe_rxq_vec_setup(__rte_unused struct ngbe_rx_queue *rxq)
3455 {
3456 	return -1;
3457 }
3458 
3459 uint16_t
3460 ngbe_xmit_fixed_burst_vec(__rte_unused void *tx_queue,
3461 			  __rte_unused struct rte_mbuf **tx_pkts,
3462 			  __rte_unused uint16_t nb_pkts)
3463 {
3464 	return 0;
3465 }
3466 
3467 int
3468 ngbe_txq_vec_setup(__rte_unused struct ngbe_tx_queue *txq)
3469 {
3470 	return -1;
3471 }
3472 
3473 void
3474 ngbe_rx_queue_release_mbufs_vec(__rte_unused struct ngbe_rx_queue *rxq)
3475 {
3476 }
3477 #endif
3478