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