xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_txrx.c (revision 3ceb08838b576b20108d7facf6baa3dbf792afe9)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/bitfield.h>
5 #include <linux/bpf.h>
6 #include <linux/bpf_trace.h>
7 #include <linux/iopoll.h>
8 #include <linux/pci.h>
9 #include <net/netdev_queues.h>
10 #include <net/page_pool/helpers.h>
11 #include <net/tcp.h>
12 #include <net/xdp.h>
13 
14 #include "fbnic.h"
15 #include "fbnic_csr.h"
16 #include "fbnic_netdev.h"
17 #include "fbnic_txrx.h"
18 
19 enum {
20 	FBNIC_XDP_PASS = 0,
21 	FBNIC_XDP_CONSUME,
22 	FBNIC_XDP_TX,
23 	FBNIC_XDP_LEN_ERR,
24 };
25 
26 enum {
27 	FBNIC_XMIT_CB_TS	= 0x01,
28 };
29 
30 struct fbnic_xmit_cb {
31 	u32 bytecount;
32 	u16 gso_segs;
33 	u8 desc_count;
34 	u8 flags;
35 	int hw_head;
36 };
37 
38 #define FBNIC_XMIT_CB(__skb) ((struct fbnic_xmit_cb *)((__skb)->cb))
39 
40 static u32 __iomem *fbnic_ring_csr_base(const struct fbnic_ring *ring)
41 {
42 	unsigned long csr_base = (unsigned long)ring->doorbell;
43 
44 	csr_base &= ~(FBNIC_QUEUE_STRIDE * sizeof(u32) - 1);
45 
46 	return (u32 __iomem *)csr_base;
47 }
48 
49 static u32 fbnic_ring_rd32(struct fbnic_ring *ring, unsigned int csr)
50 {
51 	u32 __iomem *csr_base = fbnic_ring_csr_base(ring);
52 
53 	return readl(csr_base + csr);
54 }
55 
56 static void fbnic_ring_wr32(struct fbnic_ring *ring, unsigned int csr, u32 val)
57 {
58 	u32 __iomem *csr_base = fbnic_ring_csr_base(ring);
59 
60 	writel(val, csr_base + csr);
61 }
62 
63 /**
64  * fbnic_ts40_to_ns() - convert descriptor timestamp to PHC time
65  * @fbn: netdev priv of the FB NIC
66  * @ts40: timestamp read from a descriptor
67  *
68  * Return: u64 value of PHC time in nanoseconds
69  *
70  * Convert truncated 40 bit device timestamp as read from a descriptor
71  * to the full PHC time in nanoseconds.
72  */
73 static __maybe_unused u64 fbnic_ts40_to_ns(struct fbnic_net *fbn, u64 ts40)
74 {
75 	unsigned int s;
76 	u64 time_ns;
77 	s64 offset;
78 	u8 ts_top;
79 	u32 high;
80 
81 	do {
82 		s = u64_stats_fetch_begin(&fbn->time_seq);
83 		offset = READ_ONCE(fbn->time_offset);
84 	} while (u64_stats_fetch_retry(&fbn->time_seq, s));
85 
86 	high = READ_ONCE(fbn->time_high);
87 
88 	/* Bits 63..40 from periodic clock reads, 39..0 from ts40 */
89 	time_ns = (u64)(high >> 8) << 40 | ts40;
90 
91 	/* Compare bits 32-39 between periodic reads and ts40,
92 	 * see if HW clock may have wrapped since last read. We are sure
93 	 * that periodic reads are always at least ~1 minute behind, so
94 	 * this logic works perfectly fine.
95 	 */
96 	ts_top = ts40 >> 32;
97 	if (ts_top < (u8)high && (u8)high - ts_top > U8_MAX / 2)
98 		time_ns += 1ULL << 40;
99 
100 	return time_ns + offset;
101 }
102 
103 static unsigned int fbnic_desc_unused(struct fbnic_ring *ring)
104 {
105 	return (ring->head - ring->tail - 1) & ring->size_mask;
106 }
107 
108 static unsigned int fbnic_desc_used(struct fbnic_ring *ring)
109 {
110 	return (ring->tail - ring->head) & ring->size_mask;
111 }
112 
113 static struct netdev_queue *txring_txq(const struct net_device *dev,
114 				       const struct fbnic_ring *ring)
115 {
116 	return netdev_get_tx_queue(dev, ring->q_idx);
117 }
118 
119 static int fbnic_maybe_stop_tx(const struct net_device *dev,
120 			       struct fbnic_ring *ring,
121 			       const unsigned int size)
122 {
123 	struct netdev_queue *txq = txring_txq(dev, ring);
124 	int res;
125 
126 	res = netif_txq_maybe_stop(txq, fbnic_desc_unused(ring), size,
127 				   FBNIC_TX_DESC_WAKEUP);
128 	if (!res) {
129 		u64_stats_update_begin(&ring->stats.syncp);
130 		ring->stats.twq.stop++;
131 		u64_stats_update_end(&ring->stats.syncp);
132 	}
133 
134 	return !res;
135 }
136 
137 static bool fbnic_tx_sent_queue(struct sk_buff *skb, struct fbnic_ring *ring)
138 {
139 	struct netdev_queue *dev_queue = txring_txq(skb->dev, ring);
140 	unsigned int bytecount = FBNIC_XMIT_CB(skb)->bytecount;
141 	bool xmit_more = netdev_xmit_more();
142 
143 	/* TBD: Request completion more often if xmit_more becomes large */
144 
145 	return __netdev_tx_sent_queue(dev_queue, bytecount, xmit_more);
146 }
147 
148 static void fbnic_unmap_single_twd(struct device *dev, __le64 *twd)
149 {
150 	u64 raw_twd = le64_to_cpu(*twd);
151 	unsigned int len;
152 	dma_addr_t dma;
153 
154 	dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd);
155 	len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd);
156 
157 	dma_unmap_single(dev, dma, len, DMA_TO_DEVICE);
158 }
159 
160 static void fbnic_unmap_page_twd(struct device *dev, __le64 *twd)
161 {
162 	u64 raw_twd = le64_to_cpu(*twd);
163 	unsigned int len;
164 	dma_addr_t dma;
165 
166 	dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd);
167 	len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd);
168 
169 	dma_unmap_page(dev, dma, len, DMA_TO_DEVICE);
170 }
171 
172 #define FBNIC_TWD_TYPE(_type) \
173 	cpu_to_le64(FIELD_PREP(FBNIC_TWD_TYPE_MASK, FBNIC_TWD_TYPE_##_type))
174 
175 static bool fbnic_tx_tstamp(struct sk_buff *skb)
176 {
177 	struct fbnic_net *fbn;
178 
179 	if (!unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
180 		return false;
181 
182 	fbn = netdev_priv(skb->dev);
183 	if (fbn->hwtstamp_config.tx_type == HWTSTAMP_TX_OFF)
184 		return false;
185 
186 	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
187 	FBNIC_XMIT_CB(skb)->flags |= FBNIC_XMIT_CB_TS;
188 	FBNIC_XMIT_CB(skb)->hw_head = -1;
189 
190 	return true;
191 }
192 
193 static bool
194 fbnic_tx_lso(struct fbnic_ring *ring, struct sk_buff *skb,
195 	     struct skb_shared_info *shinfo, __le64 *meta,
196 	     unsigned int *l2len, unsigned int *i3len)
197 {
198 	unsigned int l3_type, l4_type, l4len, hdrlen;
199 	unsigned char *l4hdr;
200 	__be16 payload_len;
201 
202 	if (unlikely(skb_cow_head(skb, 0)))
203 		return true;
204 
205 	if (shinfo->gso_type & SKB_GSO_PARTIAL) {
206 		l3_type = FBNIC_TWD_L3_TYPE_OTHER;
207 	} else if (!skb->encapsulation) {
208 		if (ip_hdr(skb)->version == 4)
209 			l3_type = FBNIC_TWD_L3_TYPE_IPV4;
210 		else
211 			l3_type = FBNIC_TWD_L3_TYPE_IPV6;
212 	} else {
213 		unsigned int o3len;
214 
215 		o3len = skb_inner_network_header(skb) - skb_network_header(skb);
216 		*i3len -= o3len;
217 		*meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L3_OHLEN_MASK,
218 						o3len / 2));
219 		l3_type = FBNIC_TWD_L3_TYPE_V6V6;
220 	}
221 
222 	l4hdr = skb_checksum_start(skb);
223 	payload_len = cpu_to_be16(skb->len - (l4hdr - skb->data));
224 
225 	if (shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
226 		struct tcphdr *tcph = (struct tcphdr *)l4hdr;
227 
228 		l4_type = FBNIC_TWD_L4_TYPE_TCP;
229 		l4len = __tcp_hdrlen((struct tcphdr *)l4hdr);
230 		csum_replace_by_diff(&tcph->check, (__force __wsum)payload_len);
231 	} else {
232 		struct udphdr *udph = (struct udphdr *)l4hdr;
233 
234 		l4_type = FBNIC_TWD_L4_TYPE_UDP;
235 		l4len = sizeof(struct udphdr);
236 		csum_replace_by_diff(&udph->check, (__force __wsum)payload_len);
237 	}
238 
239 	hdrlen = (l4hdr - skb->data) + l4len;
240 	*meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L3_TYPE_MASK, l3_type) |
241 			     FIELD_PREP(FBNIC_TWD_L4_TYPE_MASK, l4_type) |
242 			     FIELD_PREP(FBNIC_TWD_L4_HLEN_MASK, l4len / 4) |
243 			     FIELD_PREP(FBNIC_TWD_MSS_MASK, shinfo->gso_size) |
244 			     FBNIC_TWD_FLAG_REQ_LSO);
245 
246 	FBNIC_XMIT_CB(skb)->bytecount += (shinfo->gso_segs - 1) * hdrlen;
247 	FBNIC_XMIT_CB(skb)->gso_segs = shinfo->gso_segs;
248 
249 	u64_stats_update_begin(&ring->stats.syncp);
250 	ring->stats.twq.lso += shinfo->gso_segs;
251 	u64_stats_update_end(&ring->stats.syncp);
252 
253 	return false;
254 }
255 
256 static bool
257 fbnic_tx_offloads(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta)
258 {
259 	struct skb_shared_info *shinfo = skb_shinfo(skb);
260 	unsigned int l2len, i3len;
261 
262 	if (fbnic_tx_tstamp(skb))
263 		*meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_TS);
264 
265 	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
266 		return false;
267 
268 	l2len = skb_mac_header_len(skb);
269 	i3len = skb_checksum_start(skb) - skb_network_header(skb);
270 
271 	*meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_CSUM_OFFSET_MASK,
272 					skb->csum_offset / 2));
273 
274 	if (shinfo->gso_size) {
275 		if (fbnic_tx_lso(ring, skb, shinfo, meta, &l2len, &i3len))
276 			return true;
277 	} else {
278 		*meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_CSO);
279 		u64_stats_update_begin(&ring->stats.syncp);
280 		ring->stats.twq.csum_partial++;
281 		u64_stats_update_end(&ring->stats.syncp);
282 	}
283 
284 	*meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) |
285 			     FIELD_PREP(FBNIC_TWD_L3_IHLEN_MASK, i3len / 2));
286 	return false;
287 }
288 
289 static void
290 fbnic_rx_csum(u64 rcd, struct sk_buff *skb, struct fbnic_ring *rcq,
291 	      u64 *csum_cmpl, u64 *csum_none)
292 {
293 	skb_checksum_none_assert(skb);
294 
295 	if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
296 		(*csum_none)++;
297 		return;
298 	}
299 
300 	if (FIELD_GET(FBNIC_RCD_META_L4_CSUM_UNNECESSARY, rcd)) {
301 		skb->ip_summed = CHECKSUM_UNNECESSARY;
302 	} else {
303 		u16 csum = FIELD_GET(FBNIC_RCD_META_L2_CSUM_MASK, rcd);
304 
305 		skb->ip_summed = CHECKSUM_COMPLETE;
306 		skb->csum = (__force __wsum)csum;
307 		(*csum_cmpl)++;
308 	}
309 }
310 
311 static bool
312 fbnic_tx_map(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta)
313 {
314 	struct device *dev = skb->dev->dev.parent;
315 	unsigned int tail = ring->tail, first;
316 	unsigned int size, data_len;
317 	skb_frag_t *frag;
318 	dma_addr_t dma;
319 	__le64 *twd;
320 
321 	ring->tx_buf[tail] = skb;
322 
323 	tail++;
324 	tail &= ring->size_mask;
325 	first = tail;
326 
327 	size = skb_headlen(skb);
328 	data_len = skb->data_len;
329 
330 	if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK))
331 		goto dma_error;
332 
333 	dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
334 
335 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
336 		twd = &ring->desc[tail];
337 
338 		if (dma_mapping_error(dev, dma))
339 			goto dma_error;
340 
341 		*twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) |
342 				   FIELD_PREP(FBNIC_TWD_LEN_MASK, size) |
343 				   FIELD_PREP(FBNIC_TWD_TYPE_MASK,
344 					      FBNIC_TWD_TYPE_AL));
345 
346 		tail++;
347 		tail &= ring->size_mask;
348 
349 		if (!data_len)
350 			break;
351 
352 		size = skb_frag_size(frag);
353 		data_len -= size;
354 
355 		if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK))
356 			goto dma_error;
357 
358 		dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
359 	}
360 
361 	*twd |= FBNIC_TWD_TYPE(LAST_AL);
362 
363 	FBNIC_XMIT_CB(skb)->desc_count = ((twd - meta) + 1) & ring->size_mask;
364 
365 	ring->tail = tail;
366 
367 	/* Record SW timestamp */
368 	skb_tx_timestamp(skb);
369 
370 	/* Verify there is room for another packet */
371 	fbnic_maybe_stop_tx(skb->dev, ring, FBNIC_MAX_SKB_DESC);
372 
373 	if (fbnic_tx_sent_queue(skb, ring)) {
374 		*meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_COMPLETION);
375 
376 		/* Force DMA writes to flush before writing to tail */
377 		dma_wmb();
378 
379 		writel(tail, ring->doorbell);
380 	}
381 
382 	return false;
383 dma_error:
384 	if (net_ratelimit())
385 		netdev_err(skb->dev, "TX DMA map failed\n");
386 
387 	while (tail != first) {
388 		tail--;
389 		tail &= ring->size_mask;
390 		twd = &ring->desc[tail];
391 		if (tail == first)
392 			fbnic_unmap_single_twd(dev, twd);
393 		else
394 			fbnic_unmap_page_twd(dev, twd);
395 	}
396 
397 	return true;
398 }
399 
400 #define FBNIC_MIN_FRAME_LEN	60
401 
402 static netdev_tx_t
403 fbnic_xmit_frame_ring(struct sk_buff *skb, struct fbnic_ring *ring)
404 {
405 	__le64 *meta = &ring->desc[ring->tail];
406 	u16 desc_needed;
407 
408 	if (skb_put_padto(skb, FBNIC_MIN_FRAME_LEN))
409 		goto err_count;
410 
411 	/* Need: 1 descriptor per page,
412 	 *       + 1 desc for skb_head,
413 	 *       + 2 desc for metadata and timestamp metadata
414 	 *       + 7 desc gap to keep tail from touching head
415 	 * otherwise try next time
416 	 */
417 	desc_needed = skb_shinfo(skb)->nr_frags + 10;
418 	if (fbnic_maybe_stop_tx(skb->dev, ring, desc_needed))
419 		return NETDEV_TX_BUSY;
420 
421 	*meta = cpu_to_le64(FBNIC_TWD_FLAG_DEST_MAC);
422 
423 	/* Write all members within DWORD to condense this into 2 4B writes */
424 	FBNIC_XMIT_CB(skb)->bytecount = skb->len;
425 	FBNIC_XMIT_CB(skb)->gso_segs = 1;
426 	FBNIC_XMIT_CB(skb)->desc_count = 0;
427 	FBNIC_XMIT_CB(skb)->flags = 0;
428 
429 	if (fbnic_tx_offloads(ring, skb, meta))
430 		goto err_free;
431 
432 	if (fbnic_tx_map(ring, skb, meta))
433 		goto err_free;
434 
435 	return NETDEV_TX_OK;
436 
437 err_free:
438 	dev_kfree_skb_any(skb);
439 err_count:
440 	u64_stats_update_begin(&ring->stats.syncp);
441 	ring->stats.dropped++;
442 	u64_stats_update_end(&ring->stats.syncp);
443 	return NETDEV_TX_OK;
444 }
445 
446 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev)
447 {
448 	struct fbnic_net *fbn = netdev_priv(dev);
449 	unsigned int q_map = skb->queue_mapping;
450 
451 	return fbnic_xmit_frame_ring(skb, fbn->tx[q_map]);
452 }
453 
454 static netdev_features_t
455 fbnic_features_check_encap_gso(struct sk_buff *skb, struct net_device *dev,
456 			       netdev_features_t features, unsigned int l3len)
457 {
458 	netdev_features_t skb_gso_features;
459 	struct ipv6hdr *ip6_hdr;
460 	unsigned char l4_hdr;
461 	unsigned int start;
462 	__be16 frag_off;
463 
464 	/* Require MANGLEID for GSO_PARTIAL of IPv4.
465 	 * In theory we could support TSO with single, innermost v4 header
466 	 * by pretending everything before it is L2, but that needs to be
467 	 * parsed case by case.. so leaving it for when the need arises.
468 	 */
469 	if (!(features & NETIF_F_TSO_MANGLEID))
470 		features &= ~NETIF_F_TSO;
471 
472 	skb_gso_features = skb_shinfo(skb)->gso_type;
473 	skb_gso_features <<= NETIF_F_GSO_SHIFT;
474 
475 	/* We'd only clear the native GSO features, so don't bother validating
476 	 * if the match can only be on those supported thru GSO_PARTIAL.
477 	 */
478 	if (!(skb_gso_features & FBNIC_TUN_GSO_FEATURES))
479 		return features;
480 
481 	/* We can only do IPv6-in-IPv6, not v4-in-v6. It'd be nice
482 	 * to fall back to partial for this, or any failure below.
483 	 * This is just an optimization, UDPv4 will be caught later on.
484 	 */
485 	if (skb_gso_features & NETIF_F_TSO)
486 		return features & ~FBNIC_TUN_GSO_FEATURES;
487 
488 	/* Inner headers multiple of 2 */
489 	if ((skb_inner_network_header(skb) - skb_network_header(skb)) % 2)
490 		return features & ~FBNIC_TUN_GSO_FEATURES;
491 
492 	/* Encapsulated GSO packet, make 100% sure it's IPv6-in-IPv6. */
493 	ip6_hdr = ipv6_hdr(skb);
494 	if (ip6_hdr->version != 6)
495 		return features & ~FBNIC_TUN_GSO_FEATURES;
496 
497 	l4_hdr = ip6_hdr->nexthdr;
498 	start = (unsigned char *)ip6_hdr - skb->data + sizeof(struct ipv6hdr);
499 	start = ipv6_skip_exthdr(skb, start, &l4_hdr, &frag_off);
500 	if (frag_off || l4_hdr != IPPROTO_IPV6 ||
501 	    skb->data + start != skb_inner_network_header(skb))
502 		return features & ~FBNIC_TUN_GSO_FEATURES;
503 
504 	return features;
505 }
506 
507 netdev_features_t
508 fbnic_features_check(struct sk_buff *skb, struct net_device *dev,
509 		     netdev_features_t features)
510 {
511 	unsigned int l2len, l3len;
512 
513 	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
514 		return features;
515 
516 	l2len = skb_mac_header_len(skb);
517 	l3len = skb_checksum_start(skb) - skb_network_header(skb);
518 
519 	/* Check header lengths are multiple of 2.
520 	 * In case of 6in6 we support longer headers (IHLEN + OHLEN)
521 	 * but keep things simple for now, 512B is plenty.
522 	 */
523 	if ((l2len | l3len | skb->csum_offset) % 2 ||
524 	    !FIELD_FIT(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) ||
525 	    !FIELD_FIT(FBNIC_TWD_L3_IHLEN_MASK, l3len / 2) ||
526 	    !FIELD_FIT(FBNIC_TWD_CSUM_OFFSET_MASK, skb->csum_offset / 2))
527 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
528 
529 	if (likely(!skb->encapsulation) || !skb_is_gso(skb))
530 		return features;
531 
532 	return fbnic_features_check_encap_gso(skb, dev, features, l3len);
533 }
534 
535 static void fbnic_clean_twq0(struct fbnic_napi_vector *nv, int napi_budget,
536 			     struct fbnic_ring *ring, bool discard,
537 			     unsigned int hw_head)
538 {
539 	u64 total_bytes = 0, total_packets = 0, ts_lost = 0;
540 	unsigned int head = ring->head;
541 	struct netdev_queue *txq;
542 	unsigned int clean_desc;
543 
544 	clean_desc = (hw_head - head) & ring->size_mask;
545 
546 	while (clean_desc) {
547 		struct sk_buff *skb = ring->tx_buf[head];
548 		unsigned int desc_cnt;
549 
550 		desc_cnt = FBNIC_XMIT_CB(skb)->desc_count;
551 		if (desc_cnt > clean_desc)
552 			break;
553 
554 		if (unlikely(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS)) {
555 			FBNIC_XMIT_CB(skb)->hw_head = hw_head;
556 			if (likely(!discard))
557 				break;
558 			ts_lost++;
559 		}
560 
561 		ring->tx_buf[head] = NULL;
562 
563 		clean_desc -= desc_cnt;
564 
565 		while (!(ring->desc[head] & FBNIC_TWD_TYPE(AL))) {
566 			head++;
567 			head &= ring->size_mask;
568 			desc_cnt--;
569 		}
570 
571 		fbnic_unmap_single_twd(nv->dev, &ring->desc[head]);
572 		head++;
573 		head &= ring->size_mask;
574 		desc_cnt--;
575 
576 		while (desc_cnt--) {
577 			fbnic_unmap_page_twd(nv->dev, &ring->desc[head]);
578 			head++;
579 			head &= ring->size_mask;
580 		}
581 
582 		total_bytes += FBNIC_XMIT_CB(skb)->bytecount;
583 		total_packets += FBNIC_XMIT_CB(skb)->gso_segs;
584 
585 		napi_consume_skb(skb, napi_budget);
586 	}
587 
588 	if (!total_bytes)
589 		return;
590 
591 	ring->head = head;
592 
593 	txq = txring_txq(nv->napi.dev, ring);
594 
595 	if (unlikely(discard)) {
596 		u64_stats_update_begin(&ring->stats.syncp);
597 		ring->stats.dropped += total_packets;
598 		ring->stats.twq.ts_lost += ts_lost;
599 		u64_stats_update_end(&ring->stats.syncp);
600 
601 		netdev_tx_completed_queue(txq, total_packets, total_bytes);
602 		return;
603 	}
604 
605 	u64_stats_update_begin(&ring->stats.syncp);
606 	ring->stats.bytes += total_bytes;
607 	ring->stats.packets += total_packets;
608 	u64_stats_update_end(&ring->stats.syncp);
609 
610 	if (!netif_txq_completed_wake(txq, total_packets, total_bytes,
611 				      fbnic_desc_unused(ring),
612 				      FBNIC_TX_DESC_WAKEUP)) {
613 		u64_stats_update_begin(&ring->stats.syncp);
614 		ring->stats.twq.wake++;
615 		u64_stats_update_end(&ring->stats.syncp);
616 	}
617 }
618 
619 static void fbnic_clean_twq1(struct fbnic_napi_vector *nv, bool pp_allow_direct,
620 			     struct fbnic_ring *ring, bool discard,
621 			     unsigned int hw_head)
622 {
623 	u64 total_bytes = 0, total_packets = 0;
624 	unsigned int head = ring->head;
625 
626 	while (hw_head != head) {
627 		struct page *page;
628 		u64 twd;
629 
630 		if (unlikely(!(ring->desc[head] & FBNIC_TWD_TYPE(AL))))
631 			goto next_desc;
632 
633 		twd = le64_to_cpu(ring->desc[head]);
634 		page = ring->tx_buf[head];
635 
636 		/* TYPE_AL is 2, TYPE_LAST_AL is 3. So this trick gives
637 		 * us one increment per packet, with no branches.
638 		 */
639 		total_packets += FIELD_GET(FBNIC_TWD_TYPE_MASK, twd) -
640 				 FBNIC_TWD_TYPE_AL;
641 		total_bytes += FIELD_GET(FBNIC_TWD_LEN_MASK, twd);
642 
643 		page_pool_put_page(page->pp, page, -1, pp_allow_direct);
644 next_desc:
645 		head++;
646 		head &= ring->size_mask;
647 	}
648 
649 	if (!total_bytes)
650 		return;
651 
652 	ring->head = head;
653 
654 	if (discard) {
655 		u64_stats_update_begin(&ring->stats.syncp);
656 		ring->stats.dropped += total_packets;
657 		u64_stats_update_end(&ring->stats.syncp);
658 		return;
659 	}
660 
661 	u64_stats_update_begin(&ring->stats.syncp);
662 	ring->stats.bytes += total_bytes;
663 	ring->stats.packets += total_packets;
664 	u64_stats_update_end(&ring->stats.syncp);
665 }
666 
667 static void fbnic_clean_tsq(struct fbnic_napi_vector *nv,
668 			    struct fbnic_ring *ring,
669 			    u64 tcd, int *ts_head, int *head0)
670 {
671 	struct skb_shared_hwtstamps hwtstamp;
672 	struct fbnic_net *fbn;
673 	struct sk_buff *skb;
674 	int head;
675 	u64 ns;
676 
677 	head = (*ts_head < 0) ? ring->head : *ts_head;
678 
679 	do {
680 		unsigned int desc_cnt;
681 
682 		if (head == ring->tail) {
683 			if (unlikely(net_ratelimit()))
684 				netdev_err(nv->napi.dev,
685 					   "Tx timestamp without matching packet\n");
686 			return;
687 		}
688 
689 		skb = ring->tx_buf[head];
690 		desc_cnt = FBNIC_XMIT_CB(skb)->desc_count;
691 
692 		head += desc_cnt;
693 		head &= ring->size_mask;
694 	} while (!(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS));
695 
696 	fbn = netdev_priv(nv->napi.dev);
697 	ns = fbnic_ts40_to_ns(fbn, FIELD_GET(FBNIC_TCD_TYPE1_TS_MASK, tcd));
698 
699 	memset(&hwtstamp, 0, sizeof(hwtstamp));
700 	hwtstamp.hwtstamp = ns_to_ktime(ns);
701 
702 	*ts_head = head;
703 
704 	FBNIC_XMIT_CB(skb)->flags &= ~FBNIC_XMIT_CB_TS;
705 	if (*head0 < 0) {
706 		head = FBNIC_XMIT_CB(skb)->hw_head;
707 		if (head >= 0)
708 			*head0 = head;
709 	}
710 
711 	skb_tstamp_tx(skb, &hwtstamp);
712 	u64_stats_update_begin(&ring->stats.syncp);
713 	ring->stats.twq.ts_packets++;
714 	u64_stats_update_end(&ring->stats.syncp);
715 }
716 
717 static void fbnic_page_pool_init(struct fbnic_ring *ring, unsigned int idx,
718 				 netmem_ref netmem)
719 {
720 	struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx];
721 
722 	page_pool_fragment_netmem(netmem, FBNIC_PAGECNT_BIAS_MAX);
723 	rx_buf->pagecnt_bias = FBNIC_PAGECNT_BIAS_MAX;
724 	rx_buf->netmem = netmem;
725 }
726 
727 static struct page *
728 fbnic_page_pool_get_head(struct fbnic_q_triad *qt, unsigned int idx)
729 {
730 	struct fbnic_rx_buf *rx_buf = &qt->sub0.rx_buf[idx];
731 
732 	rx_buf->pagecnt_bias--;
733 
734 	/* sub0 is always fed system pages, from the NAPI-level page_pool */
735 	return netmem_to_page(rx_buf->netmem);
736 }
737 
738 static netmem_ref
739 fbnic_page_pool_get_data(struct fbnic_q_triad *qt, unsigned int idx)
740 {
741 	struct fbnic_rx_buf *rx_buf = &qt->sub1.rx_buf[idx];
742 
743 	rx_buf->pagecnt_bias--;
744 
745 	return rx_buf->netmem;
746 }
747 
748 static void fbnic_page_pool_drain(struct fbnic_ring *ring, unsigned int idx,
749 				  int budget)
750 {
751 	struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx];
752 	netmem_ref netmem = rx_buf->netmem;
753 
754 	if (!page_pool_unref_netmem(netmem, rx_buf->pagecnt_bias))
755 		page_pool_put_unrefed_netmem(ring->page_pool, netmem, -1,
756 					     !!budget);
757 
758 	rx_buf->netmem = 0;
759 }
760 
761 static void fbnic_clean_twq(struct fbnic_napi_vector *nv, int napi_budget,
762 			    struct fbnic_q_triad *qt, s32 ts_head, s32 head0,
763 			    s32 head1)
764 {
765 	if (head0 >= 0)
766 		fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, head0);
767 	else if (ts_head >= 0)
768 		fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, ts_head);
769 
770 	if (head1 >= 0) {
771 		qt->cmpl.deferred_head = -1;
772 		if (napi_budget)
773 			fbnic_clean_twq1(nv, true, &qt->sub1, false, head1);
774 		else
775 			qt->cmpl.deferred_head = head1;
776 	}
777 }
778 
779 static void
780 fbnic_clean_tcq(struct fbnic_napi_vector *nv, struct fbnic_q_triad *qt,
781 		int napi_budget)
782 {
783 	struct fbnic_ring *cmpl = &qt->cmpl;
784 	s32 head1 = cmpl->deferred_head;
785 	s32 head0 = -1, ts_head = -1;
786 	__le64 *raw_tcd, done;
787 	u32 head = cmpl->head;
788 
789 	done = (head & (cmpl->size_mask + 1)) ? 0 : cpu_to_le64(FBNIC_TCD_DONE);
790 	raw_tcd = &cmpl->desc[head & cmpl->size_mask];
791 
792 	/* Walk the completion queue collecting the heads reported by NIC */
793 	while ((*raw_tcd & cpu_to_le64(FBNIC_TCD_DONE)) == done) {
794 		u64 tcd;
795 
796 		dma_rmb();
797 
798 		tcd = le64_to_cpu(*raw_tcd);
799 
800 		switch (FIELD_GET(FBNIC_TCD_TYPE_MASK, tcd)) {
801 		case FBNIC_TCD_TYPE_0:
802 			if (tcd & FBNIC_TCD_TWQ1)
803 				head1 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD1_MASK,
804 						  tcd);
805 			else
806 				head0 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD0_MASK,
807 						  tcd);
808 			/* Currently all err status bits are related to
809 			 * timestamps and as those have yet to be added
810 			 * they are skipped for now.
811 			 */
812 			break;
813 		case FBNIC_TCD_TYPE_1:
814 			if (WARN_ON_ONCE(tcd & FBNIC_TCD_TWQ1))
815 				break;
816 
817 			fbnic_clean_tsq(nv, &qt->sub0, tcd, &ts_head, &head0);
818 			break;
819 		default:
820 			break;
821 		}
822 
823 		raw_tcd++;
824 		head++;
825 		if (!(head & cmpl->size_mask)) {
826 			done ^= cpu_to_le64(FBNIC_TCD_DONE);
827 			raw_tcd = &cmpl->desc[0];
828 		}
829 	}
830 
831 	/* Record the current head/tail of the queue */
832 	if (cmpl->head != head) {
833 		cmpl->head = head;
834 		writel(head & cmpl->size_mask, cmpl->doorbell);
835 	}
836 
837 	/* Unmap and free processed buffers */
838 	fbnic_clean_twq(nv, napi_budget, qt, ts_head, head0, head1);
839 }
840 
841 static void fbnic_clean_bdq(struct fbnic_ring *ring, unsigned int hw_head,
842 			    int napi_budget)
843 {
844 	unsigned int head = ring->head;
845 
846 	if (head == hw_head)
847 		return;
848 
849 	do {
850 		fbnic_page_pool_drain(ring, head, napi_budget);
851 
852 		head++;
853 		head &= ring->size_mask;
854 	} while (head != hw_head);
855 
856 	ring->head = head;
857 }
858 
859 static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, netmem_ref netmem)
860 {
861 	__le64 *bdq_desc = &bdq->desc[id * FBNIC_BD_FRAG_COUNT];
862 	dma_addr_t dma = page_pool_get_dma_addr_netmem(netmem);
863 	u64 bd, i = FBNIC_BD_FRAG_COUNT;
864 
865 	bd = (FBNIC_BD_PAGE_ADDR_MASK & dma) |
866 	     FIELD_PREP(FBNIC_BD_PAGE_ID_MASK, id);
867 
868 	/* In the case that a page size is larger than 4K we will map a
869 	 * single page to multiple fragments. The fragments will be
870 	 * FBNIC_BD_FRAG_COUNT in size and the lower n bits will be use
871 	 * to indicate the individual fragment IDs.
872 	 */
873 	do {
874 		*bdq_desc = cpu_to_le64(bd);
875 		bd += FIELD_PREP(FBNIC_BD_DESC_ADDR_MASK, 1) |
876 		      FIELD_PREP(FBNIC_BD_DESC_ID_MASK, 1);
877 	} while (--i);
878 }
879 
880 static void fbnic_fill_bdq(struct fbnic_ring *bdq)
881 {
882 	unsigned int count = fbnic_desc_unused(bdq);
883 	unsigned int i = bdq->tail;
884 
885 	if (!count)
886 		return;
887 
888 	do {
889 		netmem_ref netmem;
890 
891 		netmem = page_pool_dev_alloc_netmems(bdq->page_pool);
892 		if (!netmem) {
893 			u64_stats_update_begin(&bdq->stats.syncp);
894 			bdq->stats.rx.alloc_failed++;
895 			u64_stats_update_end(&bdq->stats.syncp);
896 
897 			break;
898 		}
899 
900 		fbnic_page_pool_init(bdq, i, netmem);
901 		fbnic_bd_prep(bdq, i, netmem);
902 
903 		i++;
904 		i &= bdq->size_mask;
905 
906 		count--;
907 	} while (count);
908 
909 	if (bdq->tail != i) {
910 		bdq->tail = i;
911 
912 		/* Force DMA writes to flush before writing to tail */
913 		dma_wmb();
914 
915 		writel(i, bdq->doorbell);
916 	}
917 }
918 
919 static unsigned int fbnic_hdr_pg_start(unsigned int pg_off)
920 {
921 	/* The headroom of the first header may be larger than FBNIC_RX_HROOM
922 	 * due to alignment. So account for that by just making the page
923 	 * offset 0 if we are starting at the first header.
924 	 */
925 	if (ALIGN(FBNIC_RX_HROOM, 128) > FBNIC_RX_HROOM &&
926 	    pg_off == ALIGN(FBNIC_RX_HROOM, 128))
927 		return 0;
928 
929 	return pg_off - FBNIC_RX_HROOM;
930 }
931 
932 static unsigned int fbnic_hdr_pg_end(unsigned int pg_off, unsigned int len)
933 {
934 	/* Determine the end of the buffer by finding the start of the next
935 	 * and then subtracting the headroom from that frame.
936 	 */
937 	pg_off += len + FBNIC_RX_TROOM + FBNIC_RX_HROOM;
938 
939 	return ALIGN(pg_off, 128) - FBNIC_RX_HROOM;
940 }
941 
942 static void fbnic_pkt_prepare(struct fbnic_napi_vector *nv, u64 rcd,
943 			      struct fbnic_pkt_buff *pkt,
944 			      struct fbnic_q_triad *qt)
945 {
946 	unsigned int hdr_pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
947 	unsigned int hdr_pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd);
948 	struct page *page = fbnic_page_pool_get_head(qt, hdr_pg_idx);
949 	unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd);
950 	unsigned int frame_sz, hdr_pg_start, hdr_pg_end, headroom;
951 	unsigned char *hdr_start;
952 
953 	/* data_hard_start should always be NULL when this is called */
954 	WARN_ON_ONCE(pkt->buff.data_hard_start);
955 
956 	/* Short-cut the end calculation if we know page is fully consumed */
957 	hdr_pg_end = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ?
958 		     FBNIC_BD_FRAG_SIZE : fbnic_hdr_pg_end(hdr_pg_off, len);
959 	hdr_pg_start = fbnic_hdr_pg_start(hdr_pg_off);
960 
961 	headroom = hdr_pg_off - hdr_pg_start + FBNIC_RX_PAD;
962 	frame_sz = hdr_pg_end - hdr_pg_start;
963 	xdp_init_buff(&pkt->buff, frame_sz, &qt->xdp_rxq);
964 	hdr_pg_start += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) *
965 			FBNIC_BD_FRAG_SIZE;
966 
967 	/* Sync DMA buffer */
968 	dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page),
969 				      hdr_pg_start, frame_sz,
970 				      DMA_BIDIRECTIONAL);
971 
972 	/* Build frame around buffer */
973 	hdr_start = page_address(page) + hdr_pg_start;
974 	net_prefetch(pkt->buff.data);
975 	xdp_prepare_buff(&pkt->buff, hdr_start, headroom,
976 			 len - FBNIC_RX_PAD, true);
977 
978 	pkt->hwtstamp = 0;
979 	pkt->add_frag_failed = false;
980 }
981 
982 static void fbnic_add_rx_frag(struct fbnic_napi_vector *nv, u64 rcd,
983 			      struct fbnic_pkt_buff *pkt,
984 			      struct fbnic_q_triad *qt)
985 {
986 	unsigned int pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
987 	unsigned int pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd);
988 	unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd);
989 	netmem_ref netmem = fbnic_page_pool_get_data(qt, pg_idx);
990 	unsigned int truesize;
991 	bool added;
992 
993 	truesize = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ?
994 		   FBNIC_BD_FRAG_SIZE - pg_off : ALIGN(len, 128);
995 
996 	pg_off += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) *
997 		  FBNIC_BD_FRAG_SIZE;
998 
999 	/* Sync DMA buffer */
1000 	dma_sync_single_range_for_cpu(nv->dev,
1001 				      page_pool_get_dma_addr_netmem(netmem),
1002 				      pg_off, truesize, DMA_BIDIRECTIONAL);
1003 
1004 	added = xdp_buff_add_frag(&pkt->buff, netmem, pg_off, len, truesize);
1005 	if (unlikely(!added)) {
1006 		pkt->add_frag_failed = true;
1007 		netdev_err_once(nv->napi.dev,
1008 				"Failed to add fragment to xdp_buff\n");
1009 	}
1010 }
1011 
1012 static void fbnic_put_pkt_buff(struct fbnic_q_triad *qt,
1013 			       struct fbnic_pkt_buff *pkt, int budget)
1014 {
1015 	struct page *page;
1016 
1017 	if (!pkt->buff.data_hard_start)
1018 		return;
1019 
1020 	if (xdp_buff_has_frags(&pkt->buff)) {
1021 		struct skb_shared_info *shinfo;
1022 		netmem_ref netmem;
1023 		int nr_frags;
1024 
1025 		shinfo = xdp_get_shared_info_from_buff(&pkt->buff);
1026 		nr_frags = shinfo->nr_frags;
1027 
1028 		while (nr_frags--) {
1029 			netmem = skb_frag_netmem(&shinfo->frags[nr_frags]);
1030 			page_pool_put_full_netmem(qt->sub1.page_pool, netmem,
1031 						  !!budget);
1032 		}
1033 	}
1034 
1035 	page = virt_to_page(pkt->buff.data_hard_start);
1036 	page_pool_put_full_page(qt->sub0.page_pool, page, !!budget);
1037 }
1038 
1039 static struct sk_buff *fbnic_build_skb(struct fbnic_napi_vector *nv,
1040 				       struct fbnic_pkt_buff *pkt)
1041 {
1042 	struct sk_buff *skb;
1043 
1044 	skb = xdp_build_skb_from_buff(&pkt->buff);
1045 	if (!skb)
1046 		return NULL;
1047 
1048 	/* Add timestamp if present */
1049 	if (pkt->hwtstamp)
1050 		skb_hwtstamps(skb)->hwtstamp = pkt->hwtstamp;
1051 
1052 	return skb;
1053 }
1054 
1055 static long fbnic_pkt_tx(struct fbnic_napi_vector *nv,
1056 			 struct fbnic_pkt_buff *pkt)
1057 {
1058 	struct fbnic_ring *ring = &nv->qt[0].sub1;
1059 	int size, offset, nsegs = 1, data_len = 0;
1060 	unsigned int tail = ring->tail;
1061 	struct skb_shared_info *shinfo;
1062 	skb_frag_t *frag = NULL;
1063 	struct page *page;
1064 	dma_addr_t dma;
1065 	__le64 *twd;
1066 
1067 	if (unlikely(xdp_buff_has_frags(&pkt->buff))) {
1068 		shinfo = xdp_get_shared_info_from_buff(&pkt->buff);
1069 		nsegs += shinfo->nr_frags;
1070 		data_len = shinfo->xdp_frags_size;
1071 		frag = &shinfo->frags[0];
1072 	}
1073 
1074 	if (fbnic_desc_unused(ring) < nsegs) {
1075 		u64_stats_update_begin(&ring->stats.syncp);
1076 		ring->stats.dropped++;
1077 		u64_stats_update_end(&ring->stats.syncp);
1078 		return -FBNIC_XDP_CONSUME;
1079 	}
1080 
1081 	page = virt_to_page(pkt->buff.data_hard_start);
1082 	offset = offset_in_page(pkt->buff.data);
1083 	dma = page_pool_get_dma_addr(page);
1084 
1085 	size = pkt->buff.data_end - pkt->buff.data;
1086 
1087 	while (nsegs--) {
1088 		dma_sync_single_range_for_device(nv->dev, dma, offset, size,
1089 						 DMA_BIDIRECTIONAL);
1090 		dma += offset;
1091 
1092 		ring->tx_buf[tail] = page;
1093 
1094 		twd = &ring->desc[tail];
1095 		*twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) |
1096 				   FIELD_PREP(FBNIC_TWD_LEN_MASK, size) |
1097 				   FIELD_PREP(FBNIC_TWD_TYPE_MASK,
1098 					      FBNIC_TWD_TYPE_AL));
1099 
1100 		tail++;
1101 		tail &= ring->size_mask;
1102 
1103 		if (!data_len)
1104 			break;
1105 
1106 		offset = skb_frag_off(frag);
1107 		page = skb_frag_page(frag);
1108 		dma = page_pool_get_dma_addr(page);
1109 
1110 		size = skb_frag_size(frag);
1111 		data_len -= size;
1112 		frag++;
1113 	}
1114 
1115 	*twd |= FBNIC_TWD_TYPE(LAST_AL);
1116 
1117 	ring->tail = tail;
1118 
1119 	return -FBNIC_XDP_TX;
1120 }
1121 
1122 static void fbnic_pkt_commit_tail(struct fbnic_napi_vector *nv,
1123 				  unsigned int pkt_tail)
1124 {
1125 	struct fbnic_ring *ring = &nv->qt[0].sub1;
1126 
1127 	/* Force DMA writes to flush before writing to tail */
1128 	dma_wmb();
1129 
1130 	writel(pkt_tail, ring->doorbell);
1131 }
1132 
1133 static struct sk_buff *fbnic_run_xdp(struct fbnic_napi_vector *nv,
1134 				     struct fbnic_pkt_buff *pkt)
1135 {
1136 	struct fbnic_net *fbn = netdev_priv(nv->napi.dev);
1137 	struct bpf_prog *xdp_prog;
1138 	int act;
1139 
1140 	xdp_prog = READ_ONCE(fbn->xdp_prog);
1141 	if (!xdp_prog)
1142 		goto xdp_pass;
1143 
1144 	/* Should never happen, config paths enforce HDS threshold > MTU */
1145 	if (xdp_buff_has_frags(&pkt->buff) && !xdp_prog->aux->xdp_has_frags)
1146 		return ERR_PTR(-FBNIC_XDP_LEN_ERR);
1147 
1148 	act = bpf_prog_run_xdp(xdp_prog, &pkt->buff);
1149 	switch (act) {
1150 	case XDP_PASS:
1151 xdp_pass:
1152 		return fbnic_build_skb(nv, pkt);
1153 	case XDP_TX:
1154 		return ERR_PTR(fbnic_pkt_tx(nv, pkt));
1155 	default:
1156 		bpf_warn_invalid_xdp_action(nv->napi.dev, xdp_prog, act);
1157 		fallthrough;
1158 	case XDP_ABORTED:
1159 		trace_xdp_exception(nv->napi.dev, xdp_prog, act);
1160 		fallthrough;
1161 	case XDP_DROP:
1162 		break;
1163 	}
1164 
1165 	return ERR_PTR(-FBNIC_XDP_CONSUME);
1166 }
1167 
1168 static enum pkt_hash_types fbnic_skb_hash_type(u64 rcd)
1169 {
1170 	return (FBNIC_RCD_META_L4_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L4 :
1171 	       (FBNIC_RCD_META_L3_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L3 :
1172 						     PKT_HASH_TYPE_L2;
1173 }
1174 
1175 static void fbnic_rx_tstamp(struct fbnic_napi_vector *nv, u64 rcd,
1176 			    struct fbnic_pkt_buff *pkt)
1177 {
1178 	struct fbnic_net *fbn;
1179 	u64 ns, ts;
1180 
1181 	if (!FIELD_GET(FBNIC_RCD_OPT_META_TS, rcd))
1182 		return;
1183 
1184 	fbn = netdev_priv(nv->napi.dev);
1185 	ts = FIELD_GET(FBNIC_RCD_OPT_META_TS_MASK, rcd);
1186 	ns = fbnic_ts40_to_ns(fbn, ts);
1187 
1188 	/* Add timestamp to shared info */
1189 	pkt->hwtstamp = ns_to_ktime(ns);
1190 }
1191 
1192 static void fbnic_populate_skb_fields(struct fbnic_napi_vector *nv,
1193 				      u64 rcd, struct sk_buff *skb,
1194 				      struct fbnic_q_triad *qt,
1195 				      u64 *csum_cmpl, u64 *csum_none)
1196 {
1197 	struct net_device *netdev = nv->napi.dev;
1198 	struct fbnic_ring *rcq = &qt->cmpl;
1199 
1200 	fbnic_rx_csum(rcd, skb, rcq, csum_cmpl, csum_none);
1201 
1202 	if (netdev->features & NETIF_F_RXHASH)
1203 		skb_set_hash(skb,
1204 			     FIELD_GET(FBNIC_RCD_META_RSS_HASH_MASK, rcd),
1205 			     fbnic_skb_hash_type(rcd));
1206 
1207 	skb_record_rx_queue(skb, rcq->q_idx);
1208 }
1209 
1210 static bool fbnic_rcd_metadata_err(u64 rcd)
1211 {
1212 	return !!(FBNIC_RCD_META_UNCORRECTABLE_ERR_MASK & rcd);
1213 }
1214 
1215 static int fbnic_clean_rcq(struct fbnic_napi_vector *nv,
1216 			   struct fbnic_q_triad *qt, int budget)
1217 {
1218 	unsigned int packets = 0, bytes = 0, dropped = 0, alloc_failed = 0;
1219 	u64 csum_complete = 0, csum_none = 0, length_errors = 0;
1220 	s32 head0 = -1, head1 = -1, pkt_tail = -1;
1221 	struct fbnic_ring *rcq = &qt->cmpl;
1222 	struct fbnic_pkt_buff *pkt;
1223 	__le64 *raw_rcd, done;
1224 	u32 head = rcq->head;
1225 
1226 	done = (head & (rcq->size_mask + 1)) ? cpu_to_le64(FBNIC_RCD_DONE) : 0;
1227 	raw_rcd = &rcq->desc[head & rcq->size_mask];
1228 	pkt = rcq->pkt;
1229 
1230 	/* Walk the completion queue collecting the heads reported by NIC */
1231 	while (likely(packets < budget)) {
1232 		struct sk_buff *skb = ERR_PTR(-EINVAL);
1233 		u64 rcd;
1234 
1235 		if ((*raw_rcd & cpu_to_le64(FBNIC_RCD_DONE)) == done)
1236 			break;
1237 
1238 		dma_rmb();
1239 
1240 		rcd = le64_to_cpu(*raw_rcd);
1241 
1242 		switch (FIELD_GET(FBNIC_RCD_TYPE_MASK, rcd)) {
1243 		case FBNIC_RCD_TYPE_HDR_AL:
1244 			head0 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
1245 			fbnic_pkt_prepare(nv, rcd, pkt, qt);
1246 
1247 			break;
1248 		case FBNIC_RCD_TYPE_PAY_AL:
1249 			head1 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
1250 			fbnic_add_rx_frag(nv, rcd, pkt, qt);
1251 
1252 			break;
1253 		case FBNIC_RCD_TYPE_OPT_META:
1254 			/* Only type 0 is currently supported */
1255 			if (FIELD_GET(FBNIC_RCD_OPT_META_TYPE_MASK, rcd))
1256 				break;
1257 
1258 			fbnic_rx_tstamp(nv, rcd, pkt);
1259 
1260 			/* We currently ignore the action table index */
1261 			break;
1262 		case FBNIC_RCD_TYPE_META:
1263 			if (unlikely(pkt->add_frag_failed))
1264 				skb = NULL;
1265 			else if (likely(!fbnic_rcd_metadata_err(rcd)))
1266 				skb = fbnic_run_xdp(nv, pkt);
1267 
1268 			/* Populate skb and invalidate XDP */
1269 			if (!IS_ERR_OR_NULL(skb)) {
1270 				fbnic_populate_skb_fields(nv, rcd, skb, qt,
1271 							  &csum_complete,
1272 							  &csum_none);
1273 
1274 				packets++;
1275 				bytes += skb->len;
1276 
1277 				napi_gro_receive(&nv->napi, skb);
1278 			} else if (skb == ERR_PTR(-FBNIC_XDP_TX)) {
1279 				pkt_tail = nv->qt[0].sub1.tail;
1280 				bytes += xdp_get_buff_len(&pkt->buff);
1281 			} else {
1282 				if (!skb) {
1283 					alloc_failed++;
1284 					dropped++;
1285 				} else if (skb == ERR_PTR(-FBNIC_XDP_LEN_ERR)) {
1286 					length_errors++;
1287 				} else {
1288 					dropped++;
1289 				}
1290 
1291 				fbnic_put_pkt_buff(qt, pkt, 1);
1292 			}
1293 
1294 			pkt->buff.data_hard_start = NULL;
1295 
1296 			break;
1297 		}
1298 
1299 		raw_rcd++;
1300 		head++;
1301 		if (!(head & rcq->size_mask)) {
1302 			done ^= cpu_to_le64(FBNIC_RCD_DONE);
1303 			raw_rcd = &rcq->desc[0];
1304 		}
1305 	}
1306 
1307 	u64_stats_update_begin(&rcq->stats.syncp);
1308 	rcq->stats.packets += packets;
1309 	rcq->stats.bytes += bytes;
1310 	/* Re-add ethernet header length (removed in fbnic_build_skb) */
1311 	rcq->stats.bytes += ETH_HLEN * packets;
1312 	rcq->stats.dropped += dropped;
1313 	rcq->stats.rx.alloc_failed += alloc_failed;
1314 	rcq->stats.rx.csum_complete += csum_complete;
1315 	rcq->stats.rx.csum_none += csum_none;
1316 	rcq->stats.rx.length_errors += length_errors;
1317 	u64_stats_update_end(&rcq->stats.syncp);
1318 
1319 	if (pkt_tail >= 0)
1320 		fbnic_pkt_commit_tail(nv, pkt_tail);
1321 
1322 	/* Unmap and free processed buffers */
1323 	if (head0 >= 0)
1324 		fbnic_clean_bdq(&qt->sub0, head0, budget);
1325 	fbnic_fill_bdq(&qt->sub0);
1326 
1327 	if (head1 >= 0)
1328 		fbnic_clean_bdq(&qt->sub1, head1, budget);
1329 	fbnic_fill_bdq(&qt->sub1);
1330 
1331 	/* Record the current head/tail of the queue */
1332 	if (rcq->head != head) {
1333 		rcq->head = head;
1334 		writel(head & rcq->size_mask, rcq->doorbell);
1335 	}
1336 
1337 	return packets;
1338 }
1339 
1340 static void fbnic_nv_irq_disable(struct fbnic_napi_vector *nv)
1341 {
1342 	struct fbnic_dev *fbd = nv->fbd;
1343 	u32 v_idx = nv->v_idx;
1344 
1345 	fbnic_wr32(fbd, FBNIC_INTR_MASK_SET(v_idx / 32), 1 << (v_idx % 32));
1346 }
1347 
1348 static void fbnic_nv_irq_rearm(struct fbnic_napi_vector *nv)
1349 {
1350 	struct fbnic_dev *fbd = nv->fbd;
1351 	u32 v_idx = nv->v_idx;
1352 
1353 	fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(v_idx),
1354 		   FBNIC_INTR_CQ_REARM_INTR_UNMASK);
1355 }
1356 
1357 static int fbnic_poll(struct napi_struct *napi, int budget)
1358 {
1359 	struct fbnic_napi_vector *nv = container_of(napi,
1360 						    struct fbnic_napi_vector,
1361 						    napi);
1362 	int i, j, work_done = 0;
1363 
1364 	for (i = 0; i < nv->txt_count; i++)
1365 		fbnic_clean_tcq(nv, &nv->qt[i], budget);
1366 
1367 	for (j = 0; j < nv->rxt_count; j++, i++)
1368 		work_done += fbnic_clean_rcq(nv, &nv->qt[i], budget);
1369 
1370 	if (work_done >= budget)
1371 		return budget;
1372 
1373 	if (likely(napi_complete_done(napi, work_done)))
1374 		fbnic_nv_irq_rearm(nv);
1375 
1376 	return work_done;
1377 }
1378 
1379 irqreturn_t fbnic_msix_clean_rings(int __always_unused irq, void *data)
1380 {
1381 	struct fbnic_napi_vector *nv = *(void **)data;
1382 
1383 	napi_schedule_irqoff(&nv->napi);
1384 
1385 	return IRQ_HANDLED;
1386 }
1387 
1388 void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn,
1389 				      struct fbnic_ring *rxr)
1390 {
1391 	struct fbnic_queue_stats *stats = &rxr->stats;
1392 
1393 	/* Capture stats from queues before dissasociating them */
1394 	fbn->rx_stats.bytes += stats->bytes;
1395 	fbn->rx_stats.packets += stats->packets;
1396 	fbn->rx_stats.dropped += stats->dropped;
1397 	fbn->rx_stats.rx.alloc_failed += stats->rx.alloc_failed;
1398 	fbn->rx_stats.rx.csum_complete += stats->rx.csum_complete;
1399 	fbn->rx_stats.rx.csum_none += stats->rx.csum_none;
1400 	fbn->rx_stats.rx.length_errors += stats->rx.length_errors;
1401 	/* Remember to add new stats here */
1402 	BUILD_BUG_ON(sizeof(fbn->rx_stats.rx) / 8 != 4);
1403 }
1404 
1405 void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn,
1406 				      struct fbnic_ring *txr)
1407 {
1408 	struct fbnic_queue_stats *stats = &txr->stats;
1409 
1410 	/* Capture stats from queues before dissasociating them */
1411 	fbn->tx_stats.bytes += stats->bytes;
1412 	fbn->tx_stats.packets += stats->packets;
1413 	fbn->tx_stats.dropped += stats->dropped;
1414 	fbn->tx_stats.twq.csum_partial += stats->twq.csum_partial;
1415 	fbn->tx_stats.twq.lso += stats->twq.lso;
1416 	fbn->tx_stats.twq.ts_lost += stats->twq.ts_lost;
1417 	fbn->tx_stats.twq.ts_packets += stats->twq.ts_packets;
1418 	fbn->tx_stats.twq.stop += stats->twq.stop;
1419 	fbn->tx_stats.twq.wake += stats->twq.wake;
1420 	/* Remember to add new stats here */
1421 	BUILD_BUG_ON(sizeof(fbn->tx_stats.twq) / 8 != 6);
1422 }
1423 
1424 static void fbnic_aggregate_ring_xdp_counters(struct fbnic_net *fbn,
1425 					      struct fbnic_ring *xdpr)
1426 {
1427 	struct fbnic_queue_stats *stats = &xdpr->stats;
1428 
1429 	if (!(xdpr->flags & FBNIC_RING_F_STATS))
1430 		return;
1431 
1432 	/* Capture stats from queues before dissasociating them */
1433 	fbn->rx_stats.bytes += stats->bytes;
1434 	fbn->rx_stats.packets += stats->packets;
1435 	fbn->rx_stats.dropped += stats->dropped;
1436 	fbn->tx_stats.bytes += stats->bytes;
1437 	fbn->tx_stats.packets += stats->packets;
1438 }
1439 
1440 static void fbnic_remove_tx_ring(struct fbnic_net *fbn,
1441 				 struct fbnic_ring *txr)
1442 {
1443 	if (!(txr->flags & FBNIC_RING_F_STATS))
1444 		return;
1445 
1446 	fbnic_aggregate_ring_tx_counters(fbn, txr);
1447 
1448 	/* Remove pointer to the Tx ring */
1449 	WARN_ON(fbn->tx[txr->q_idx] && fbn->tx[txr->q_idx] != txr);
1450 	fbn->tx[txr->q_idx] = NULL;
1451 }
1452 
1453 static void fbnic_remove_xdp_ring(struct fbnic_net *fbn,
1454 				  struct fbnic_ring *xdpr)
1455 {
1456 	if (!(xdpr->flags & FBNIC_RING_F_STATS))
1457 		return;
1458 
1459 	fbnic_aggregate_ring_xdp_counters(fbn, xdpr);
1460 
1461 	/* Remove pointer to the Tx ring */
1462 	WARN_ON(fbn->tx[xdpr->q_idx] && fbn->tx[xdpr->q_idx] != xdpr);
1463 	fbn->tx[xdpr->q_idx] = NULL;
1464 }
1465 
1466 static void fbnic_remove_rx_ring(struct fbnic_net *fbn,
1467 				 struct fbnic_ring *rxr)
1468 {
1469 	if (!(rxr->flags & FBNIC_RING_F_STATS))
1470 		return;
1471 
1472 	fbnic_aggregate_ring_rx_counters(fbn, rxr);
1473 
1474 	/* Remove pointer to the Rx ring */
1475 	WARN_ON(fbn->rx[rxr->q_idx] && fbn->rx[rxr->q_idx] != rxr);
1476 	fbn->rx[rxr->q_idx] = NULL;
1477 }
1478 
1479 static void fbnic_free_qt_page_pools(struct fbnic_q_triad *qt)
1480 {
1481 	page_pool_destroy(qt->sub0.page_pool);
1482 	page_pool_destroy(qt->sub1.page_pool);
1483 }
1484 
1485 static void fbnic_free_napi_vector(struct fbnic_net *fbn,
1486 				   struct fbnic_napi_vector *nv)
1487 {
1488 	struct fbnic_dev *fbd = nv->fbd;
1489 	int i, j;
1490 
1491 	for (i = 0; i < nv->txt_count; i++) {
1492 		fbnic_remove_tx_ring(fbn, &nv->qt[i].sub0);
1493 		fbnic_remove_xdp_ring(fbn, &nv->qt[i].sub1);
1494 		fbnic_remove_tx_ring(fbn, &nv->qt[i].cmpl);
1495 	}
1496 
1497 	for (j = 0; j < nv->rxt_count; j++, i++) {
1498 		fbnic_remove_rx_ring(fbn, &nv->qt[i].sub0);
1499 		fbnic_remove_rx_ring(fbn, &nv->qt[i].sub1);
1500 		fbnic_remove_rx_ring(fbn, &nv->qt[i].cmpl);
1501 	}
1502 
1503 	fbnic_napi_free_irq(fbd, nv);
1504 	netif_napi_del_locked(&nv->napi);
1505 	fbn->napi[fbnic_napi_idx(nv)] = NULL;
1506 	kfree(nv);
1507 }
1508 
1509 void fbnic_free_napi_vectors(struct fbnic_net *fbn)
1510 {
1511 	int i;
1512 
1513 	for (i = 0; i < fbn->num_napi; i++)
1514 		if (fbn->napi[i])
1515 			fbnic_free_napi_vector(fbn, fbn->napi[i]);
1516 }
1517 
1518 #define FBNIC_PAGE_POOL_FLAGS \
1519 	(PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV)
1520 
1521 static int
1522 fbnic_alloc_qt_page_pools(struct fbnic_net *fbn, struct fbnic_napi_vector *nv,
1523 			  struct fbnic_q_triad *qt)
1524 {
1525 	struct page_pool_params pp_params = {
1526 		.order = 0,
1527 		.flags = FBNIC_PAGE_POOL_FLAGS,
1528 		.pool_size = fbn->hpq_size + fbn->ppq_size,
1529 		.nid = NUMA_NO_NODE,
1530 		.dev = nv->dev,
1531 		.dma_dir = DMA_BIDIRECTIONAL,
1532 		.offset = 0,
1533 		.max_len = PAGE_SIZE,
1534 		.napi	= &nv->napi,
1535 		.netdev	= fbn->netdev,
1536 	};
1537 	struct page_pool *pp;
1538 
1539 	/* Page pool cannot exceed a size of 32768. This doesn't limit the
1540 	 * pages on the ring but the number we can have cached waiting on
1541 	 * the next use.
1542 	 *
1543 	 * TBD: Can this be reduced further? Would a multiple of
1544 	 * NAPI_POLL_WEIGHT possibly make more sense? The question is how
1545 	 * may pages do we need to hold in reserve to get the best return
1546 	 * without hogging too much system memory.
1547 	 */
1548 	if (pp_params.pool_size > 32768)
1549 		pp_params.pool_size = 32768;
1550 
1551 	pp = page_pool_create(&pp_params);
1552 	if (IS_ERR(pp))
1553 		return PTR_ERR(pp);
1554 
1555 	qt->sub0.page_pool = pp;
1556 	page_pool_get(pp);
1557 	qt->sub1.page_pool = pp;
1558 
1559 	return 0;
1560 }
1561 
1562 static void fbnic_ring_init(struct fbnic_ring *ring, u32 __iomem *doorbell,
1563 			    int q_idx, u8 flags)
1564 {
1565 	u64_stats_init(&ring->stats.syncp);
1566 	ring->doorbell = doorbell;
1567 	ring->q_idx = q_idx;
1568 	ring->flags = flags;
1569 	ring->deferred_head = -1;
1570 }
1571 
1572 static int fbnic_alloc_napi_vector(struct fbnic_dev *fbd, struct fbnic_net *fbn,
1573 				   unsigned int v_count, unsigned int v_idx,
1574 				   unsigned int txq_count, unsigned int txq_idx,
1575 				   unsigned int rxq_count, unsigned int rxq_idx)
1576 {
1577 	int txt_count = txq_count, rxt_count = rxq_count;
1578 	u32 __iomem *uc_addr = fbd->uc_addr0;
1579 	int xdp_count = 0, qt_count, err;
1580 	struct fbnic_napi_vector *nv;
1581 	struct fbnic_q_triad *qt;
1582 	u32 __iomem *db;
1583 
1584 	/* We need to reserve at least one Tx Queue Triad for an XDP ring */
1585 	if (rxq_count) {
1586 		xdp_count = 1;
1587 		if (!txt_count)
1588 			txt_count = 1;
1589 	}
1590 
1591 	qt_count = txt_count + rxq_count;
1592 	if (!qt_count)
1593 		return -EINVAL;
1594 
1595 	/* If MMIO has already failed there are no rings to initialize */
1596 	if (!uc_addr)
1597 		return -EIO;
1598 
1599 	/* Allocate NAPI vector and queue triads */
1600 	nv = kzalloc(struct_size(nv, qt, qt_count), GFP_KERNEL);
1601 	if (!nv)
1602 		return -ENOMEM;
1603 
1604 	/* Record queue triad counts */
1605 	nv->txt_count = txt_count;
1606 	nv->rxt_count = rxt_count;
1607 
1608 	/* Provide pointer back to fbnic and MSI-X vectors */
1609 	nv->fbd = fbd;
1610 	nv->v_idx = v_idx;
1611 
1612 	/* Tie napi to netdev */
1613 	fbn->napi[fbnic_napi_idx(nv)] = nv;
1614 	netif_napi_add_locked(fbn->netdev, &nv->napi, fbnic_poll);
1615 
1616 	/* Record IRQ to NAPI struct */
1617 	netif_napi_set_irq_locked(&nv->napi,
1618 				  pci_irq_vector(to_pci_dev(fbd->dev),
1619 						 nv->v_idx));
1620 
1621 	/* Tie nv back to PCIe dev */
1622 	nv->dev = fbd->dev;
1623 
1624 	/* Request the IRQ for napi vector */
1625 	err = fbnic_napi_request_irq(fbd, nv);
1626 	if (err)
1627 		goto napi_del;
1628 
1629 	/* Initialize queue triads */
1630 	qt = nv->qt;
1631 
1632 	while (txt_count) {
1633 		u8 flags = FBNIC_RING_F_CTX | FBNIC_RING_F_STATS;
1634 
1635 		/* Configure Tx queue */
1636 		db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ0_TAIL];
1637 
1638 		/* Assign Tx queue to netdev if applicable */
1639 		if (txq_count > 0) {
1640 
1641 			fbnic_ring_init(&qt->sub0, db, txq_idx, flags);
1642 			fbn->tx[txq_idx] = &qt->sub0;
1643 			txq_count--;
1644 		} else {
1645 			fbnic_ring_init(&qt->sub0, db, 0,
1646 					FBNIC_RING_F_DISABLED);
1647 		}
1648 
1649 		/* Configure XDP queue */
1650 		db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ1_TAIL];
1651 
1652 		/* Assign XDP queue to netdev if applicable
1653 		 *
1654 		 * The setup for this is in itself a bit different.
1655 		 * 1. We only need one XDP Tx queue per NAPI vector.
1656 		 * 2. We associate it to the first Rx queue index.
1657 		 * 3. The hardware side is associated based on the Tx Queue.
1658 		 * 4. The netdev queue is offset by FBNIC_MAX_TXQs.
1659 		 */
1660 		if (xdp_count > 0) {
1661 			unsigned int xdp_idx = FBNIC_MAX_TXQS + rxq_idx;
1662 
1663 			fbnic_ring_init(&qt->sub1, db, xdp_idx, flags);
1664 			fbn->tx[xdp_idx] = &qt->sub1;
1665 			xdp_count--;
1666 		} else {
1667 			fbnic_ring_init(&qt->sub1, db, 0,
1668 					FBNIC_RING_F_DISABLED);
1669 		}
1670 
1671 		/* Configure Tx completion queue */
1672 		db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TCQ_HEAD];
1673 		fbnic_ring_init(&qt->cmpl, db, 0, 0);
1674 
1675 		/* Update Tx queue index */
1676 		txt_count--;
1677 		txq_idx += v_count;
1678 
1679 		/* Move to next queue triad */
1680 		qt++;
1681 	}
1682 
1683 	while (rxt_count) {
1684 		/* Configure header queue */
1685 		db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_HPQ_TAIL];
1686 		fbnic_ring_init(&qt->sub0, db, 0, FBNIC_RING_F_CTX);
1687 
1688 		/* Configure payload queue */
1689 		db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_PPQ_TAIL];
1690 		fbnic_ring_init(&qt->sub1, db, 0, FBNIC_RING_F_CTX);
1691 
1692 		/* Configure Rx completion queue */
1693 		db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_RCQ_HEAD];
1694 		fbnic_ring_init(&qt->cmpl, db, rxq_idx, FBNIC_RING_F_STATS);
1695 		fbn->rx[rxq_idx] = &qt->cmpl;
1696 
1697 		/* Update Rx queue index */
1698 		rxt_count--;
1699 		rxq_idx += v_count;
1700 
1701 		/* Move to next queue triad */
1702 		qt++;
1703 	}
1704 
1705 	return 0;
1706 
1707 napi_del:
1708 	netif_napi_del_locked(&nv->napi);
1709 	fbn->napi[fbnic_napi_idx(nv)] = NULL;
1710 	kfree(nv);
1711 	return err;
1712 }
1713 
1714 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn)
1715 {
1716 	unsigned int txq_idx = 0, rxq_idx = 0, v_idx = FBNIC_NON_NAPI_VECTORS;
1717 	unsigned int num_tx = fbn->num_tx_queues;
1718 	unsigned int num_rx = fbn->num_rx_queues;
1719 	unsigned int num_napi = fbn->num_napi;
1720 	struct fbnic_dev *fbd = fbn->fbd;
1721 	int err;
1722 
1723 	/* Allocate 1 Tx queue per napi vector */
1724 	if (num_napi < FBNIC_MAX_TXQS && num_napi == num_tx + num_rx) {
1725 		while (num_tx) {
1726 			err = fbnic_alloc_napi_vector(fbd, fbn,
1727 						      num_napi, v_idx,
1728 						      1, txq_idx, 0, 0);
1729 			if (err)
1730 				goto free_vectors;
1731 
1732 			/* Update counts and index */
1733 			num_tx--;
1734 			txq_idx++;
1735 
1736 			v_idx++;
1737 		}
1738 	}
1739 
1740 	/* Allocate Tx/Rx queue pairs per vector, or allocate remaining Rx */
1741 	while (num_rx | num_tx) {
1742 		int tqpv = DIV_ROUND_UP(num_tx, num_napi - txq_idx);
1743 		int rqpv = DIV_ROUND_UP(num_rx, num_napi - rxq_idx);
1744 
1745 		err = fbnic_alloc_napi_vector(fbd, fbn, num_napi, v_idx,
1746 					      tqpv, txq_idx, rqpv, rxq_idx);
1747 		if (err)
1748 			goto free_vectors;
1749 
1750 		/* Update counts and index */
1751 		num_tx -= tqpv;
1752 		txq_idx++;
1753 
1754 		num_rx -= rqpv;
1755 		rxq_idx++;
1756 
1757 		v_idx++;
1758 	}
1759 
1760 	return 0;
1761 
1762 free_vectors:
1763 	fbnic_free_napi_vectors(fbn);
1764 
1765 	return -ENOMEM;
1766 }
1767 
1768 static void fbnic_free_ring_resources(struct device *dev,
1769 				      struct fbnic_ring *ring)
1770 {
1771 	kvfree(ring->buffer);
1772 	ring->buffer = NULL;
1773 
1774 	/* If size is not set there are no descriptors present */
1775 	if (!ring->size)
1776 		return;
1777 
1778 	dma_free_coherent(dev, ring->size, ring->desc, ring->dma);
1779 	ring->size_mask = 0;
1780 	ring->size = 0;
1781 }
1782 
1783 static int fbnic_alloc_tx_ring_desc(struct fbnic_net *fbn,
1784 				    struct fbnic_ring *txr)
1785 {
1786 	struct device *dev = fbn->netdev->dev.parent;
1787 	size_t size;
1788 
1789 	/* Round size up to nearest 4K */
1790 	size = ALIGN(array_size(sizeof(*txr->desc), fbn->txq_size), 4096);
1791 
1792 	txr->desc = dma_alloc_coherent(dev, size, &txr->dma,
1793 				       GFP_KERNEL | __GFP_NOWARN);
1794 	if (!txr->desc)
1795 		return -ENOMEM;
1796 
1797 	/* txq_size should be a power of 2, so mask is just that -1 */
1798 	txr->size_mask = fbn->txq_size - 1;
1799 	txr->size = size;
1800 
1801 	return 0;
1802 }
1803 
1804 static int fbnic_alloc_tx_ring_buffer(struct fbnic_ring *txr)
1805 {
1806 	size_t size = array_size(sizeof(*txr->tx_buf), txr->size_mask + 1);
1807 
1808 	txr->tx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1809 
1810 	return txr->tx_buf ? 0 : -ENOMEM;
1811 }
1812 
1813 static int fbnic_alloc_tx_ring_resources(struct fbnic_net *fbn,
1814 					 struct fbnic_ring *txr)
1815 {
1816 	struct device *dev = fbn->netdev->dev.parent;
1817 	int err;
1818 
1819 	if (txr->flags & FBNIC_RING_F_DISABLED)
1820 		return 0;
1821 
1822 	err = fbnic_alloc_tx_ring_desc(fbn, txr);
1823 	if (err)
1824 		return err;
1825 
1826 	if (!(txr->flags & FBNIC_RING_F_CTX))
1827 		return 0;
1828 
1829 	err = fbnic_alloc_tx_ring_buffer(txr);
1830 	if (err)
1831 		goto free_desc;
1832 
1833 	return 0;
1834 
1835 free_desc:
1836 	fbnic_free_ring_resources(dev, txr);
1837 	return err;
1838 }
1839 
1840 static int fbnic_alloc_rx_ring_desc(struct fbnic_net *fbn,
1841 				    struct fbnic_ring *rxr)
1842 {
1843 	struct device *dev = fbn->netdev->dev.parent;
1844 	size_t desc_size = sizeof(*rxr->desc);
1845 	u32 rxq_size;
1846 	size_t size;
1847 
1848 	switch (rxr->doorbell - fbnic_ring_csr_base(rxr)) {
1849 	case FBNIC_QUEUE_BDQ_HPQ_TAIL:
1850 		rxq_size = fbn->hpq_size / FBNIC_BD_FRAG_COUNT;
1851 		desc_size *= FBNIC_BD_FRAG_COUNT;
1852 		break;
1853 	case FBNIC_QUEUE_BDQ_PPQ_TAIL:
1854 		rxq_size = fbn->ppq_size / FBNIC_BD_FRAG_COUNT;
1855 		desc_size *= FBNIC_BD_FRAG_COUNT;
1856 		break;
1857 	case FBNIC_QUEUE_RCQ_HEAD:
1858 		rxq_size = fbn->rcq_size;
1859 		break;
1860 	default:
1861 		return -EINVAL;
1862 	}
1863 
1864 	/* Round size up to nearest 4K */
1865 	size = ALIGN(array_size(desc_size, rxq_size), 4096);
1866 
1867 	rxr->desc = dma_alloc_coherent(dev, size, &rxr->dma,
1868 				       GFP_KERNEL | __GFP_NOWARN);
1869 	if (!rxr->desc)
1870 		return -ENOMEM;
1871 
1872 	/* rxq_size should be a power of 2, so mask is just that -1 */
1873 	rxr->size_mask = rxq_size - 1;
1874 	rxr->size = size;
1875 
1876 	return 0;
1877 }
1878 
1879 static int fbnic_alloc_rx_ring_buffer(struct fbnic_ring *rxr)
1880 {
1881 	size_t size = array_size(sizeof(*rxr->rx_buf), rxr->size_mask + 1);
1882 
1883 	if (rxr->flags & FBNIC_RING_F_CTX)
1884 		size = sizeof(*rxr->rx_buf) * (rxr->size_mask + 1);
1885 	else
1886 		size = sizeof(*rxr->pkt);
1887 
1888 	rxr->rx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1889 
1890 	return rxr->rx_buf ? 0 : -ENOMEM;
1891 }
1892 
1893 static int fbnic_alloc_rx_ring_resources(struct fbnic_net *fbn,
1894 					 struct fbnic_ring *rxr)
1895 {
1896 	struct device *dev = fbn->netdev->dev.parent;
1897 	int err;
1898 
1899 	err = fbnic_alloc_rx_ring_desc(fbn, rxr);
1900 	if (err)
1901 		return err;
1902 
1903 	err = fbnic_alloc_rx_ring_buffer(rxr);
1904 	if (err)
1905 		goto free_desc;
1906 
1907 	return 0;
1908 
1909 free_desc:
1910 	fbnic_free_ring_resources(dev, rxr);
1911 	return err;
1912 }
1913 
1914 static void fbnic_free_qt_resources(struct fbnic_net *fbn,
1915 				    struct fbnic_q_triad *qt)
1916 {
1917 	struct device *dev = fbn->netdev->dev.parent;
1918 
1919 	fbnic_free_ring_resources(dev, &qt->cmpl);
1920 	fbnic_free_ring_resources(dev, &qt->sub1);
1921 	fbnic_free_ring_resources(dev, &qt->sub0);
1922 
1923 	if (xdp_rxq_info_is_reg(&qt->xdp_rxq)) {
1924 		xdp_rxq_info_unreg_mem_model(&qt->xdp_rxq);
1925 		xdp_rxq_info_unreg(&qt->xdp_rxq);
1926 		fbnic_free_qt_page_pools(qt);
1927 	}
1928 }
1929 
1930 static int fbnic_alloc_tx_qt_resources(struct fbnic_net *fbn,
1931 				       struct fbnic_q_triad *qt)
1932 {
1933 	struct device *dev = fbn->netdev->dev.parent;
1934 	int err;
1935 
1936 	err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub0);
1937 	if (err)
1938 		return err;
1939 
1940 	err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub1);
1941 	if (err)
1942 		goto free_sub0;
1943 
1944 	err = fbnic_alloc_tx_ring_resources(fbn, &qt->cmpl);
1945 	if (err)
1946 		goto free_sub1;
1947 
1948 	return 0;
1949 
1950 free_sub1:
1951 	fbnic_free_ring_resources(dev, &qt->sub1);
1952 free_sub0:
1953 	fbnic_free_ring_resources(dev, &qt->sub0);
1954 	return err;
1955 }
1956 
1957 static int fbnic_alloc_rx_qt_resources(struct fbnic_net *fbn,
1958 				       struct fbnic_napi_vector *nv,
1959 				       struct fbnic_q_triad *qt)
1960 {
1961 	struct device *dev = fbn->netdev->dev.parent;
1962 	int err;
1963 
1964 	err = fbnic_alloc_qt_page_pools(fbn, nv, qt);
1965 	if (err)
1966 		return err;
1967 
1968 	err = xdp_rxq_info_reg(&qt->xdp_rxq, fbn->netdev, qt->sub0.q_idx,
1969 			       nv->napi.napi_id);
1970 	if (err)
1971 		goto free_page_pools;
1972 
1973 	err = xdp_rxq_info_reg_mem_model(&qt->xdp_rxq, MEM_TYPE_PAGE_POOL,
1974 					 qt->sub0.page_pool);
1975 	if (err)
1976 		goto unreg_rxq;
1977 
1978 	err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub0);
1979 	if (err)
1980 		goto unreg_mm;
1981 
1982 	err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub1);
1983 	if (err)
1984 		goto free_sub0;
1985 
1986 	err = fbnic_alloc_rx_ring_resources(fbn, &qt->cmpl);
1987 	if (err)
1988 		goto free_sub1;
1989 
1990 	return 0;
1991 
1992 free_sub1:
1993 	fbnic_free_ring_resources(dev, &qt->sub1);
1994 free_sub0:
1995 	fbnic_free_ring_resources(dev, &qt->sub0);
1996 unreg_mm:
1997 	xdp_rxq_info_unreg_mem_model(&qt->xdp_rxq);
1998 unreg_rxq:
1999 	xdp_rxq_info_unreg(&qt->xdp_rxq);
2000 free_page_pools:
2001 	fbnic_free_qt_page_pools(qt);
2002 	return err;
2003 }
2004 
2005 static void fbnic_free_nv_resources(struct fbnic_net *fbn,
2006 				    struct fbnic_napi_vector *nv)
2007 {
2008 	int i;
2009 
2010 	for (i = 0; i < nv->txt_count + nv->rxt_count; i++)
2011 		fbnic_free_qt_resources(fbn, &nv->qt[i]);
2012 }
2013 
2014 static int fbnic_alloc_nv_resources(struct fbnic_net *fbn,
2015 				    struct fbnic_napi_vector *nv)
2016 {
2017 	int i, j, err;
2018 
2019 	/* Allocate Tx Resources */
2020 	for (i = 0; i < nv->txt_count; i++) {
2021 		err = fbnic_alloc_tx_qt_resources(fbn, &nv->qt[i]);
2022 		if (err)
2023 			goto free_qt_resources;
2024 	}
2025 
2026 	/* Allocate Rx Resources */
2027 	for (j = 0; j < nv->rxt_count; j++, i++) {
2028 		err = fbnic_alloc_rx_qt_resources(fbn, nv, &nv->qt[i]);
2029 		if (err)
2030 			goto free_qt_resources;
2031 	}
2032 
2033 	return 0;
2034 
2035 free_qt_resources:
2036 	while (i--)
2037 		fbnic_free_qt_resources(fbn, &nv->qt[i]);
2038 	return err;
2039 }
2040 
2041 void fbnic_free_resources(struct fbnic_net *fbn)
2042 {
2043 	int i;
2044 
2045 	for (i = 0; i < fbn->num_napi; i++)
2046 		fbnic_free_nv_resources(fbn, fbn->napi[i]);
2047 }
2048 
2049 int fbnic_alloc_resources(struct fbnic_net *fbn)
2050 {
2051 	int i, err = -ENODEV;
2052 
2053 	for (i = 0; i < fbn->num_napi; i++) {
2054 		err = fbnic_alloc_nv_resources(fbn, fbn->napi[i]);
2055 		if (err)
2056 			goto free_resources;
2057 	}
2058 
2059 	return 0;
2060 
2061 free_resources:
2062 	while (i--)
2063 		fbnic_free_nv_resources(fbn, fbn->napi[i]);
2064 
2065 	return err;
2066 }
2067 
2068 static void fbnic_set_netif_napi(struct fbnic_napi_vector *nv)
2069 {
2070 	int i, j;
2071 
2072 	/* Associate Tx queue with NAPI */
2073 	for (i = 0; i < nv->txt_count; i++) {
2074 		struct fbnic_q_triad *qt = &nv->qt[i];
2075 
2076 		netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx,
2077 				     NETDEV_QUEUE_TYPE_TX, &nv->napi);
2078 	}
2079 
2080 	/* Associate Rx queue with NAPI */
2081 	for (j = 0; j < nv->rxt_count; j++, i++) {
2082 		struct fbnic_q_triad *qt = &nv->qt[i];
2083 
2084 		netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx,
2085 				     NETDEV_QUEUE_TYPE_RX, &nv->napi);
2086 	}
2087 }
2088 
2089 static void fbnic_reset_netif_napi(struct fbnic_napi_vector *nv)
2090 {
2091 	int i, j;
2092 
2093 	/* Disassociate Tx queue from NAPI */
2094 	for (i = 0; i < nv->txt_count; i++) {
2095 		struct fbnic_q_triad *qt = &nv->qt[i];
2096 
2097 		netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx,
2098 				     NETDEV_QUEUE_TYPE_TX, NULL);
2099 	}
2100 
2101 	/* Disassociate Rx queue from NAPI */
2102 	for (j = 0; j < nv->rxt_count; j++, i++) {
2103 		struct fbnic_q_triad *qt = &nv->qt[i];
2104 
2105 		netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx,
2106 				     NETDEV_QUEUE_TYPE_RX, NULL);
2107 	}
2108 }
2109 
2110 int fbnic_set_netif_queues(struct fbnic_net *fbn)
2111 {
2112 	int i, err;
2113 
2114 	err = netif_set_real_num_queues(fbn->netdev, fbn->num_tx_queues,
2115 					fbn->num_rx_queues);
2116 	if (err)
2117 		return err;
2118 
2119 	for (i = 0; i < fbn->num_napi; i++)
2120 		fbnic_set_netif_napi(fbn->napi[i]);
2121 
2122 	return 0;
2123 }
2124 
2125 void fbnic_reset_netif_queues(struct fbnic_net *fbn)
2126 {
2127 	int i;
2128 
2129 	for (i = 0; i < fbn->num_napi; i++)
2130 		fbnic_reset_netif_napi(fbn->napi[i]);
2131 }
2132 
2133 static void fbnic_disable_twq0(struct fbnic_ring *txr)
2134 {
2135 	u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ0_CTL);
2136 
2137 	twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE;
2138 
2139 	fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ0_CTL, twq_ctl);
2140 }
2141 
2142 static void fbnic_disable_twq1(struct fbnic_ring *txr)
2143 {
2144 	u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ1_CTL);
2145 
2146 	twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE;
2147 
2148 	fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ1_CTL, twq_ctl);
2149 }
2150 
2151 static void fbnic_disable_tcq(struct fbnic_ring *txr)
2152 {
2153 	fbnic_ring_wr32(txr, FBNIC_QUEUE_TCQ_CTL, 0);
2154 	fbnic_ring_wr32(txr, FBNIC_QUEUE_TIM_MASK, FBNIC_QUEUE_TIM_MASK_MASK);
2155 }
2156 
2157 static void fbnic_disable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq)
2158 {
2159 	u32 bdq_ctl = fbnic_ring_rd32(hpq, FBNIC_QUEUE_BDQ_CTL);
2160 
2161 	bdq_ctl &= ~FBNIC_QUEUE_BDQ_CTL_ENABLE;
2162 
2163 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl);
2164 }
2165 
2166 static void fbnic_disable_rcq(struct fbnic_ring *rxr)
2167 {
2168 	fbnic_ring_wr32(rxr, FBNIC_QUEUE_RCQ_CTL, 0);
2169 	fbnic_ring_wr32(rxr, FBNIC_QUEUE_RIM_MASK, FBNIC_QUEUE_RIM_MASK_MASK);
2170 }
2171 
2172 void fbnic_napi_disable(struct fbnic_net *fbn)
2173 {
2174 	int i;
2175 
2176 	for (i = 0; i < fbn->num_napi; i++) {
2177 		napi_disable_locked(&fbn->napi[i]->napi);
2178 
2179 		fbnic_nv_irq_disable(fbn->napi[i]);
2180 	}
2181 }
2182 
2183 static void __fbnic_nv_disable(struct fbnic_napi_vector *nv)
2184 {
2185 	int i, t;
2186 
2187 	/* Disable Tx queue triads */
2188 	for (t = 0; t < nv->txt_count; t++) {
2189 		struct fbnic_q_triad *qt = &nv->qt[t];
2190 
2191 		fbnic_disable_twq0(&qt->sub0);
2192 		fbnic_disable_twq1(&qt->sub1);
2193 		fbnic_disable_tcq(&qt->cmpl);
2194 	}
2195 
2196 	/* Disable Rx queue triads */
2197 	for (i = 0; i < nv->rxt_count; i++, t++) {
2198 		struct fbnic_q_triad *qt = &nv->qt[t];
2199 
2200 		fbnic_disable_bdq(&qt->sub0, &qt->sub1);
2201 		fbnic_disable_rcq(&qt->cmpl);
2202 	}
2203 }
2204 
2205 void fbnic_disable(struct fbnic_net *fbn)
2206 {
2207 	struct fbnic_dev *fbd = fbn->fbd;
2208 	int i;
2209 
2210 	for (i = 0; i < fbn->num_napi; i++)
2211 		__fbnic_nv_disable(fbn->napi[i]);
2212 
2213 	fbnic_wrfl(fbd);
2214 }
2215 
2216 static void fbnic_tx_flush(struct fbnic_dev *fbd)
2217 {
2218 	netdev_warn(fbd->netdev, "triggering Tx flush\n");
2219 
2220 	fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN,
2221 		    FBNIC_TMI_DROP_CTRL_EN);
2222 }
2223 
2224 static void fbnic_tx_flush_off(struct fbnic_dev *fbd)
2225 {
2226 	fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 0);
2227 }
2228 
2229 struct fbnic_idle_regs {
2230 	u32 reg_base;
2231 	u8 reg_cnt;
2232 };
2233 
2234 static bool fbnic_all_idle(struct fbnic_dev *fbd,
2235 			   const struct fbnic_idle_regs *regs,
2236 			   unsigned int nregs)
2237 {
2238 	unsigned int i, j;
2239 
2240 	for (i = 0; i < nregs; i++) {
2241 		for (j = 0; j < regs[i].reg_cnt; j++) {
2242 			if (fbnic_rd32(fbd, regs[i].reg_base + j) != ~0U)
2243 				return false;
2244 		}
2245 	}
2246 	return true;
2247 }
2248 
2249 static void fbnic_idle_dump(struct fbnic_dev *fbd,
2250 			    const struct fbnic_idle_regs *regs,
2251 			    unsigned int nregs, const char *dir, int err)
2252 {
2253 	unsigned int i, j;
2254 
2255 	netdev_err(fbd->netdev, "error waiting for %s idle %d\n", dir, err);
2256 	for (i = 0; i < nregs; i++)
2257 		for (j = 0; j < regs[i].reg_cnt; j++)
2258 			netdev_err(fbd->netdev, "0x%04x: %08x\n",
2259 				   regs[i].reg_base + j,
2260 				   fbnic_rd32(fbd, regs[i].reg_base + j));
2261 }
2262 
2263 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail)
2264 {
2265 	static const struct fbnic_idle_regs tx[] = {
2266 		{ FBNIC_QM_TWQ_IDLE(0),	FBNIC_QM_TWQ_IDLE_CNT, },
2267 		{ FBNIC_QM_TQS_IDLE(0),	FBNIC_QM_TQS_IDLE_CNT, },
2268 		{ FBNIC_QM_TDE_IDLE(0),	FBNIC_QM_TDE_IDLE_CNT, },
2269 		{ FBNIC_QM_TCQ_IDLE(0),	FBNIC_QM_TCQ_IDLE_CNT, },
2270 	}, rx[] = {
2271 		{ FBNIC_QM_HPQ_IDLE(0),	FBNIC_QM_HPQ_IDLE_CNT, },
2272 		{ FBNIC_QM_PPQ_IDLE(0),	FBNIC_QM_PPQ_IDLE_CNT, },
2273 		{ FBNIC_QM_RCQ_IDLE(0),	FBNIC_QM_RCQ_IDLE_CNT, },
2274 	};
2275 	bool idle;
2276 	int err;
2277 
2278 	err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000,
2279 				       false, fbd, tx, ARRAY_SIZE(tx));
2280 	if (err == -ETIMEDOUT) {
2281 		fbnic_tx_flush(fbd);
2282 		err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle,
2283 					       2, 500000, false,
2284 					       fbd, tx, ARRAY_SIZE(tx));
2285 		fbnic_tx_flush_off(fbd);
2286 	}
2287 	if (err) {
2288 		fbnic_idle_dump(fbd, tx, ARRAY_SIZE(tx), "Tx", err);
2289 		if (may_fail)
2290 			return err;
2291 	}
2292 
2293 	err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000,
2294 				       false, fbd, rx, ARRAY_SIZE(rx));
2295 	if (err)
2296 		fbnic_idle_dump(fbd, rx, ARRAY_SIZE(rx), "Rx", err);
2297 	return err;
2298 }
2299 
2300 static void fbnic_nv_flush(struct fbnic_napi_vector *nv)
2301 {
2302 	int j, t;
2303 
2304 	/* Flush any processed Tx Queue Triads and drop the rest */
2305 	for (t = 0; t < nv->txt_count; t++) {
2306 		struct fbnic_q_triad *qt = &nv->qt[t];
2307 		struct netdev_queue *tx_queue;
2308 
2309 		/* Clean the work queues of unprocessed work */
2310 		fbnic_clean_twq0(nv, 0, &qt->sub0, true, qt->sub0.tail);
2311 		fbnic_clean_twq1(nv, false, &qt->sub1, true,
2312 				 qt->sub1.tail);
2313 
2314 		/* Reset completion queue descriptor ring */
2315 		memset(qt->cmpl.desc, 0, qt->cmpl.size);
2316 
2317 		/* Nothing else to do if Tx queue is disabled */
2318 		if (qt->sub0.flags & FBNIC_RING_F_DISABLED)
2319 			continue;
2320 
2321 		/* Reset BQL associated with Tx queue */
2322 		tx_queue = netdev_get_tx_queue(nv->napi.dev,
2323 					       qt->sub0.q_idx);
2324 		netdev_tx_reset_queue(tx_queue);
2325 	}
2326 
2327 	/* Flush any processed Rx Queue Triads and drop the rest */
2328 	for (j = 0; j < nv->rxt_count; j++, t++) {
2329 		struct fbnic_q_triad *qt = &nv->qt[t];
2330 
2331 		/* Clean the work queues of unprocessed work */
2332 		fbnic_clean_bdq(&qt->sub0, qt->sub0.tail, 0);
2333 		fbnic_clean_bdq(&qt->sub1, qt->sub1.tail, 0);
2334 
2335 		/* Reset completion queue descriptor ring */
2336 		memset(qt->cmpl.desc, 0, qt->cmpl.size);
2337 
2338 		fbnic_put_pkt_buff(qt, qt->cmpl.pkt, 0);
2339 		memset(qt->cmpl.pkt, 0, sizeof(struct fbnic_pkt_buff));
2340 	}
2341 }
2342 
2343 void fbnic_flush(struct fbnic_net *fbn)
2344 {
2345 	int i;
2346 
2347 	for (i = 0; i < fbn->num_napi; i++)
2348 		fbnic_nv_flush(fbn->napi[i]);
2349 }
2350 
2351 static void fbnic_nv_fill(struct fbnic_napi_vector *nv)
2352 {
2353 	int j, t;
2354 
2355 	/* Configure NAPI mapping and populate pages
2356 	 * in the BDQ rings to use for Rx
2357 	 */
2358 	for (j = 0, t = nv->txt_count; j < nv->rxt_count; j++, t++) {
2359 		struct fbnic_q_triad *qt = &nv->qt[t];
2360 
2361 		/* Populate the header and payload BDQs */
2362 		fbnic_fill_bdq(&qt->sub0);
2363 		fbnic_fill_bdq(&qt->sub1);
2364 	}
2365 }
2366 
2367 void fbnic_fill(struct fbnic_net *fbn)
2368 {
2369 	int i;
2370 
2371 	for (i = 0; i < fbn->num_napi; i++)
2372 		fbnic_nv_fill(fbn->napi[i]);
2373 }
2374 
2375 static void fbnic_enable_twq0(struct fbnic_ring *twq)
2376 {
2377 	u32 log_size = fls(twq->size_mask);
2378 
2379 	if (!twq->size_mask)
2380 		return;
2381 
2382 	/* Reset head/tail */
2383 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_RESET);
2384 	twq->tail = 0;
2385 	twq->head = 0;
2386 
2387 	/* Store descriptor ring address and size */
2388 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAL, lower_32_bits(twq->dma));
2389 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAH, upper_32_bits(twq->dma));
2390 
2391 	/* Write lower 4 bits of log size as 64K ring size is 0 */
2392 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_SIZE, log_size & 0xf);
2393 
2394 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE);
2395 }
2396 
2397 static void fbnic_enable_twq1(struct fbnic_ring *twq)
2398 {
2399 	u32 log_size = fls(twq->size_mask);
2400 
2401 	if (!twq->size_mask)
2402 		return;
2403 
2404 	/* Reset head/tail */
2405 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_RESET);
2406 	twq->tail = 0;
2407 	twq->head = 0;
2408 
2409 	/* Store descriptor ring address and size */
2410 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAL, lower_32_bits(twq->dma));
2411 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAH, upper_32_bits(twq->dma));
2412 
2413 	/* Write lower 4 bits of log size as 64K ring size is 0 */
2414 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_SIZE, log_size & 0xf);
2415 
2416 	fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE);
2417 }
2418 
2419 static void fbnic_enable_tcq(struct fbnic_napi_vector *nv,
2420 			     struct fbnic_ring *tcq)
2421 {
2422 	u32 log_size = fls(tcq->size_mask);
2423 
2424 	if (!tcq->size_mask)
2425 		return;
2426 
2427 	/* Reset head/tail */
2428 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_RESET);
2429 	tcq->tail = 0;
2430 	tcq->head = 0;
2431 
2432 	/* Store descriptor ring address and size */
2433 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAL, lower_32_bits(tcq->dma));
2434 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAH, upper_32_bits(tcq->dma));
2435 
2436 	/* Write lower 4 bits of log size as 64K ring size is 0 */
2437 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_SIZE, log_size & 0xf);
2438 
2439 	/* Store interrupt information for the completion queue */
2440 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_CTL, nv->v_idx);
2441 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_THRESHOLD, tcq->size_mask / 2);
2442 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_MASK, 0);
2443 
2444 	/* Enable queue */
2445 	fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_ENABLE);
2446 }
2447 
2448 static void fbnic_enable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq)
2449 {
2450 	u32 bdq_ctl = FBNIC_QUEUE_BDQ_CTL_ENABLE;
2451 	u32 log_size;
2452 
2453 	/* Reset head/tail */
2454 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, FBNIC_QUEUE_BDQ_CTL_RESET);
2455 	ppq->tail = 0;
2456 	ppq->head = 0;
2457 	hpq->tail = 0;
2458 	hpq->head = 0;
2459 
2460 	log_size = fls(hpq->size_mask);
2461 
2462 	/* Store descriptor ring address and size */
2463 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAL, lower_32_bits(hpq->dma));
2464 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAH, upper_32_bits(hpq->dma));
2465 
2466 	/* Write lower 4 bits of log size as 64K ring size is 0 */
2467 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_SIZE, log_size & 0xf);
2468 
2469 	if (!ppq->size_mask)
2470 		goto write_ctl;
2471 
2472 	log_size = fls(ppq->size_mask);
2473 
2474 	/* Add enabling of PPQ to BDQ control */
2475 	bdq_ctl |= FBNIC_QUEUE_BDQ_CTL_PPQ_ENABLE;
2476 
2477 	/* Store descriptor ring address and size */
2478 	fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAL, lower_32_bits(ppq->dma));
2479 	fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAH, upper_32_bits(ppq->dma));
2480 	fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_SIZE, log_size & 0xf);
2481 
2482 write_ctl:
2483 	fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl);
2484 }
2485 
2486 static void fbnic_config_drop_mode_rcq(struct fbnic_napi_vector *nv,
2487 				       struct fbnic_ring *rcq)
2488 {
2489 	u32 drop_mode, rcq_ctl;
2490 
2491 	drop_mode = FBNIC_QUEUE_RDE_CTL0_DROP_IMMEDIATE;
2492 
2493 	/* Specify packet layout */
2494 	rcq_ctl = FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_DROP_MODE_MASK, drop_mode) |
2495 	    FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_HROOM_MASK, FBNIC_RX_HROOM) |
2496 	    FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_TROOM_MASK, FBNIC_RX_TROOM);
2497 
2498 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL0, rcq_ctl);
2499 }
2500 
2501 static void fbnic_config_rim_threshold(struct fbnic_ring *rcq, u16 nv_idx, u32 rx_desc)
2502 {
2503 	u32 threshold;
2504 
2505 	/* Set the threhsold to half the ring size if rx_frames
2506 	 * is not configured
2507 	 */
2508 	threshold = rx_desc ? : rcq->size_mask / 2;
2509 
2510 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_CTL, nv_idx);
2511 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_THRESHOLD, threshold);
2512 }
2513 
2514 void fbnic_config_txrx_usecs(struct fbnic_napi_vector *nv, u32 arm)
2515 {
2516 	struct fbnic_net *fbn = netdev_priv(nv->napi.dev);
2517 	struct fbnic_dev *fbd = nv->fbd;
2518 	u32 val = arm;
2519 
2520 	val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT, fbn->rx_usecs) |
2521 	       FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT_UPD_EN;
2522 	val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT, fbn->tx_usecs) |
2523 	       FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT_UPD_EN;
2524 
2525 	fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(nv->v_idx), val);
2526 }
2527 
2528 void fbnic_config_rx_frames(struct fbnic_napi_vector *nv)
2529 {
2530 	struct fbnic_net *fbn = netdev_priv(nv->napi.dev);
2531 	int i;
2532 
2533 	for (i = nv->txt_count; i < nv->rxt_count + nv->txt_count; i++) {
2534 		struct fbnic_q_triad *qt = &nv->qt[i];
2535 
2536 		fbnic_config_rim_threshold(&qt->cmpl, nv->v_idx,
2537 					   fbn->rx_max_frames *
2538 					   FBNIC_MIN_RXD_PER_FRAME);
2539 	}
2540 }
2541 
2542 static void fbnic_enable_rcq(struct fbnic_napi_vector *nv,
2543 			     struct fbnic_ring *rcq)
2544 {
2545 	struct fbnic_net *fbn = netdev_priv(nv->napi.dev);
2546 	u32 log_size = fls(rcq->size_mask);
2547 	u32 hds_thresh = fbn->hds_thresh;
2548 	u32 rcq_ctl = 0;
2549 
2550 	fbnic_config_drop_mode_rcq(nv, rcq);
2551 
2552 	/* Force lower bound on MAX_HEADER_BYTES. Below this, all frames should
2553 	 * be split at L4. It would also result in the frames being split at
2554 	 * L2/L3 depending on the frame size.
2555 	 */
2556 	if (fbn->hds_thresh < FBNIC_HDR_BYTES_MIN) {
2557 		rcq_ctl = FBNIC_QUEUE_RDE_CTL0_EN_HDR_SPLIT;
2558 		hds_thresh = FBNIC_HDR_BYTES_MIN;
2559 	}
2560 
2561 	rcq_ctl |= FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PADLEN_MASK, FBNIC_RX_PAD) |
2562 		   FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_MAX_HDR_MASK, hds_thresh) |
2563 		   FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_OFF_MASK,
2564 			      FBNIC_RX_PAYLD_OFFSET) |
2565 		   FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_PG_CL_MASK,
2566 			      FBNIC_RX_PAYLD_PG_CL);
2567 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL1, rcq_ctl);
2568 
2569 	/* Reset head/tail */
2570 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_RESET);
2571 	rcq->head = 0;
2572 	rcq->tail = 0;
2573 
2574 	/* Store descriptor ring address and size */
2575 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAL, lower_32_bits(rcq->dma));
2576 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAH, upper_32_bits(rcq->dma));
2577 
2578 	/* Write lower 4 bits of log size as 64K ring size is 0 */
2579 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_SIZE, log_size & 0xf);
2580 
2581 	/* Store interrupt information for the completion queue */
2582 	fbnic_config_rim_threshold(rcq, nv->v_idx, fbn->rx_max_frames *
2583 						   FBNIC_MIN_RXD_PER_FRAME);
2584 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_MASK, 0);
2585 
2586 	/* Enable queue */
2587 	fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_ENABLE);
2588 }
2589 
2590 static void __fbnic_nv_enable(struct fbnic_napi_vector *nv)
2591 {
2592 	int j, t;
2593 
2594 	/* Setup Tx Queue Triads */
2595 	for (t = 0; t < nv->txt_count; t++) {
2596 		struct fbnic_q_triad *qt = &nv->qt[t];
2597 
2598 		fbnic_enable_twq0(&qt->sub0);
2599 		fbnic_enable_twq1(&qt->sub1);
2600 		fbnic_enable_tcq(nv, &qt->cmpl);
2601 	}
2602 
2603 	/* Setup Rx Queue Triads */
2604 	for (j = 0; j < nv->rxt_count; j++, t++) {
2605 		struct fbnic_q_triad *qt = &nv->qt[t];
2606 
2607 		fbnic_enable_bdq(&qt->sub0, &qt->sub1);
2608 		fbnic_config_drop_mode_rcq(nv, &qt->cmpl);
2609 		fbnic_enable_rcq(nv, &qt->cmpl);
2610 	}
2611 }
2612 
2613 void fbnic_enable(struct fbnic_net *fbn)
2614 {
2615 	struct fbnic_dev *fbd = fbn->fbd;
2616 	int i;
2617 
2618 	for (i = 0; i < fbn->num_napi; i++)
2619 		__fbnic_nv_enable(fbn->napi[i]);
2620 
2621 	fbnic_wrfl(fbd);
2622 }
2623 
2624 static void fbnic_nv_irq_enable(struct fbnic_napi_vector *nv)
2625 {
2626 	fbnic_config_txrx_usecs(nv, FBNIC_INTR_CQ_REARM_INTR_UNMASK);
2627 }
2628 
2629 void fbnic_napi_enable(struct fbnic_net *fbn)
2630 {
2631 	u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {};
2632 	struct fbnic_dev *fbd = fbn->fbd;
2633 	int i;
2634 
2635 	for (i = 0; i < fbn->num_napi; i++) {
2636 		struct fbnic_napi_vector *nv = fbn->napi[i];
2637 
2638 		napi_enable_locked(&nv->napi);
2639 
2640 		fbnic_nv_irq_enable(nv);
2641 
2642 		/* Record bit used for NAPI IRQs so we can
2643 		 * set the mask appropriately
2644 		 */
2645 		irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32);
2646 	}
2647 
2648 	/* Force the first interrupt on the device to guarantee
2649 	 * that any packets that may have been enqueued during the
2650 	 * bringup are processed.
2651 	 */
2652 	for (i = 0; i < ARRAY_SIZE(irqs); i++) {
2653 		if (!irqs[i])
2654 			continue;
2655 		fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]);
2656 	}
2657 
2658 	fbnic_wrfl(fbd);
2659 }
2660 
2661 void fbnic_napi_depletion_check(struct net_device *netdev)
2662 {
2663 	struct fbnic_net *fbn = netdev_priv(netdev);
2664 	u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {};
2665 	struct fbnic_dev *fbd = fbn->fbd;
2666 	int i, j, t;
2667 
2668 	for (i = 0; i < fbn->num_napi; i++) {
2669 		struct fbnic_napi_vector *nv = fbn->napi[i];
2670 
2671 		/* Find RQs which are completely out of pages */
2672 		for (t = nv->txt_count, j = 0; j < nv->rxt_count; j++, t++) {
2673 			/* Assume 4 pages is always enough to fit a packet
2674 			 * and therefore generate a completion and an IRQ.
2675 			 */
2676 			if (fbnic_desc_used(&nv->qt[t].sub0) < 4 ||
2677 			    fbnic_desc_used(&nv->qt[t].sub1) < 4)
2678 				irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32);
2679 		}
2680 	}
2681 
2682 	for (i = 0; i < ARRAY_SIZE(irqs); i++) {
2683 		if (!irqs[i])
2684 			continue;
2685 		fbnic_wr32(fbd, FBNIC_INTR_MASK_CLEAR(i), irqs[i]);
2686 		fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]);
2687 	}
2688 
2689 	fbnic_wrfl(fbd);
2690 }
2691