Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / meta / fbnic / fbnic_txrx.c
blobb5050fabe8fe82e95cbfc5e2e902131b8cbd2976
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
4 #include <linux/bitfield.h>
5 #include <linux/iopoll.h>
6 #include <linux/pci.h>
7 #include <net/netdev_queues.h>
8 #include <net/page_pool/helpers.h>
10 #include "fbnic.h"
11 #include "fbnic_csr.h"
12 #include "fbnic_netdev.h"
13 #include "fbnic_txrx.h"
15 enum {
16 FBNIC_XMIT_CB_TS = 0x01,
19 struct fbnic_xmit_cb {
20 u32 bytecount;
21 u8 desc_count;
22 u8 flags;
23 int hw_head;
26 #define FBNIC_XMIT_CB(__skb) ((struct fbnic_xmit_cb *)((__skb)->cb))
28 static u32 __iomem *fbnic_ring_csr_base(const struct fbnic_ring *ring)
30 unsigned long csr_base = (unsigned long)ring->doorbell;
32 csr_base &= ~(FBNIC_QUEUE_STRIDE * sizeof(u32) - 1);
34 return (u32 __iomem *)csr_base;
37 static u32 fbnic_ring_rd32(struct fbnic_ring *ring, unsigned int csr)
39 u32 __iomem *csr_base = fbnic_ring_csr_base(ring);
41 return readl(csr_base + csr);
44 static void fbnic_ring_wr32(struct fbnic_ring *ring, unsigned int csr, u32 val)
46 u32 __iomem *csr_base = fbnic_ring_csr_base(ring);
48 writel(val, csr_base + csr);
51 /**
52 * fbnic_ts40_to_ns() - convert descriptor timestamp to PHC time
53 * @fbn: netdev priv of the FB NIC
54 * @ts40: timestamp read from a descriptor
56 * Return: u64 value of PHC time in nanoseconds
58 * Convert truncated 40 bit device timestamp as read from a descriptor
59 * to the full PHC time in nanoseconds.
61 static __maybe_unused u64 fbnic_ts40_to_ns(struct fbnic_net *fbn, u64 ts40)
63 unsigned int s;
64 u64 time_ns;
65 s64 offset;
66 u8 ts_top;
67 u32 high;
69 do {
70 s = u64_stats_fetch_begin(&fbn->time_seq);
71 offset = READ_ONCE(fbn->time_offset);
72 } while (u64_stats_fetch_retry(&fbn->time_seq, s));
74 high = READ_ONCE(fbn->time_high);
76 /* Bits 63..40 from periodic clock reads, 39..0 from ts40 */
77 time_ns = (u64)(high >> 8) << 40 | ts40;
79 /* Compare bits 32-39 between periodic reads and ts40,
80 * see if HW clock may have wrapped since last read. We are sure
81 * that periodic reads are always at least ~1 minute behind, so
82 * this logic works perfectly fine.
84 ts_top = ts40 >> 32;
85 if (ts_top < (u8)high && (u8)high - ts_top > U8_MAX / 2)
86 time_ns += 1ULL << 40;
88 return time_ns + offset;
91 static unsigned int fbnic_desc_unused(struct fbnic_ring *ring)
93 return (ring->head - ring->tail - 1) & ring->size_mask;
96 static unsigned int fbnic_desc_used(struct fbnic_ring *ring)
98 return (ring->tail - ring->head) & ring->size_mask;
101 static struct netdev_queue *txring_txq(const struct net_device *dev,
102 const struct fbnic_ring *ring)
104 return netdev_get_tx_queue(dev, ring->q_idx);
107 static int fbnic_maybe_stop_tx(const struct net_device *dev,
108 struct fbnic_ring *ring,
109 const unsigned int size)
111 struct netdev_queue *txq = txring_txq(dev, ring);
112 int res;
114 res = netif_txq_maybe_stop(txq, fbnic_desc_unused(ring), size,
115 FBNIC_TX_DESC_WAKEUP);
117 return !res;
120 static bool fbnic_tx_sent_queue(struct sk_buff *skb, struct fbnic_ring *ring)
122 struct netdev_queue *dev_queue = txring_txq(skb->dev, ring);
123 unsigned int bytecount = FBNIC_XMIT_CB(skb)->bytecount;
124 bool xmit_more = netdev_xmit_more();
126 /* TBD: Request completion more often if xmit_more becomes large */
128 return __netdev_tx_sent_queue(dev_queue, bytecount, xmit_more);
131 static void fbnic_unmap_single_twd(struct device *dev, __le64 *twd)
133 u64 raw_twd = le64_to_cpu(*twd);
134 unsigned int len;
135 dma_addr_t dma;
137 dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd);
138 len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd);
140 dma_unmap_single(dev, dma, len, DMA_TO_DEVICE);
143 static void fbnic_unmap_page_twd(struct device *dev, __le64 *twd)
145 u64 raw_twd = le64_to_cpu(*twd);
146 unsigned int len;
147 dma_addr_t dma;
149 dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd);
150 len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd);
152 dma_unmap_page(dev, dma, len, DMA_TO_DEVICE);
155 #define FBNIC_TWD_TYPE(_type) \
156 cpu_to_le64(FIELD_PREP(FBNIC_TWD_TYPE_MASK, FBNIC_TWD_TYPE_##_type))
158 static bool fbnic_tx_tstamp(struct sk_buff *skb)
160 struct fbnic_net *fbn;
162 if (!unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
163 return false;
165 fbn = netdev_priv(skb->dev);
166 if (fbn->hwtstamp_config.tx_type == HWTSTAMP_TX_OFF)
167 return false;
169 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
170 FBNIC_XMIT_CB(skb)->flags |= FBNIC_XMIT_CB_TS;
171 FBNIC_XMIT_CB(skb)->hw_head = -1;
173 return true;
176 static bool
177 fbnic_tx_offloads(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta)
179 unsigned int l2len, i3len;
181 if (fbnic_tx_tstamp(skb))
182 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_TS);
184 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
185 return false;
187 l2len = skb_mac_header_len(skb);
188 i3len = skb_checksum_start(skb) - skb_network_header(skb);
190 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_CSUM_OFFSET_MASK,
191 skb->csum_offset / 2));
193 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_CSO);
195 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) |
196 FIELD_PREP(FBNIC_TWD_L3_IHLEN_MASK, i3len / 2));
197 return false;
200 static void
201 fbnic_rx_csum(u64 rcd, struct sk_buff *skb, struct fbnic_ring *rcq)
203 skb_checksum_none_assert(skb);
205 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
206 return;
208 if (FIELD_GET(FBNIC_RCD_META_L4_CSUM_UNNECESSARY, rcd)) {
209 skb->ip_summed = CHECKSUM_UNNECESSARY;
210 } else {
211 u16 csum = FIELD_GET(FBNIC_RCD_META_L2_CSUM_MASK, rcd);
213 skb->ip_summed = CHECKSUM_COMPLETE;
214 skb->csum = (__force __wsum)csum;
218 static bool
219 fbnic_tx_map(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta)
221 struct device *dev = skb->dev->dev.parent;
222 unsigned int tail = ring->tail, first;
223 unsigned int size, data_len;
224 skb_frag_t *frag;
225 dma_addr_t dma;
226 __le64 *twd;
228 ring->tx_buf[tail] = skb;
230 tail++;
231 tail &= ring->size_mask;
232 first = tail;
234 size = skb_headlen(skb);
235 data_len = skb->data_len;
237 if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK))
238 goto dma_error;
240 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
242 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
243 twd = &ring->desc[tail];
245 if (dma_mapping_error(dev, dma))
246 goto dma_error;
248 *twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) |
249 FIELD_PREP(FBNIC_TWD_LEN_MASK, size) |
250 FIELD_PREP(FBNIC_TWD_TYPE_MASK,
251 FBNIC_TWD_TYPE_AL));
253 tail++;
254 tail &= ring->size_mask;
256 if (!data_len)
257 break;
259 size = skb_frag_size(frag);
260 data_len -= size;
262 if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK))
263 goto dma_error;
265 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
268 *twd |= FBNIC_TWD_TYPE(LAST_AL);
270 FBNIC_XMIT_CB(skb)->desc_count = ((twd - meta) + 1) & ring->size_mask;
272 ring->tail = tail;
274 /* Record SW timestamp */
275 skb_tx_timestamp(skb);
277 /* Verify there is room for another packet */
278 fbnic_maybe_stop_tx(skb->dev, ring, FBNIC_MAX_SKB_DESC);
280 if (fbnic_tx_sent_queue(skb, ring)) {
281 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_COMPLETION);
283 /* Force DMA writes to flush before writing to tail */
284 dma_wmb();
286 writel(tail, ring->doorbell);
289 return false;
290 dma_error:
291 if (net_ratelimit())
292 netdev_err(skb->dev, "TX DMA map failed\n");
294 while (tail != first) {
295 tail--;
296 tail &= ring->size_mask;
297 twd = &ring->desc[tail];
298 if (tail == first)
299 fbnic_unmap_single_twd(dev, twd);
300 else
301 fbnic_unmap_page_twd(dev, twd);
304 return true;
307 #define FBNIC_MIN_FRAME_LEN 60
309 static netdev_tx_t
310 fbnic_xmit_frame_ring(struct sk_buff *skb, struct fbnic_ring *ring)
312 __le64 *meta = &ring->desc[ring->tail];
313 u16 desc_needed;
315 if (skb_put_padto(skb, FBNIC_MIN_FRAME_LEN))
316 goto err_count;
318 /* Need: 1 descriptor per page,
319 * + 1 desc for skb_head,
320 * + 2 desc for metadata and timestamp metadata
321 * + 7 desc gap to keep tail from touching head
322 * otherwise try next time
324 desc_needed = skb_shinfo(skb)->nr_frags + 10;
325 if (fbnic_maybe_stop_tx(skb->dev, ring, desc_needed))
326 return NETDEV_TX_BUSY;
328 *meta = cpu_to_le64(FBNIC_TWD_FLAG_DEST_MAC);
330 /* Write all members within DWORD to condense this into 2 4B writes */
331 FBNIC_XMIT_CB(skb)->bytecount = skb->len;
332 FBNIC_XMIT_CB(skb)->desc_count = 0;
334 if (fbnic_tx_offloads(ring, skb, meta))
335 goto err_free;
337 if (fbnic_tx_map(ring, skb, meta))
338 goto err_free;
340 return NETDEV_TX_OK;
342 err_free:
343 dev_kfree_skb_any(skb);
344 err_count:
345 u64_stats_update_begin(&ring->stats.syncp);
346 ring->stats.dropped++;
347 u64_stats_update_end(&ring->stats.syncp);
348 return NETDEV_TX_OK;
351 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev)
353 struct fbnic_net *fbn = netdev_priv(dev);
354 unsigned int q_map = skb->queue_mapping;
356 return fbnic_xmit_frame_ring(skb, fbn->tx[q_map]);
359 netdev_features_t
360 fbnic_features_check(struct sk_buff *skb, struct net_device *dev,
361 netdev_features_t features)
363 unsigned int l2len, l3len;
365 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
366 return features;
368 l2len = skb_mac_header_len(skb);
369 l3len = skb_checksum_start(skb) - skb_network_header(skb);
371 /* Check header lengths are multiple of 2.
372 * In case of 6in6 we support longer headers (IHLEN + OHLEN)
373 * but keep things simple for now, 512B is plenty.
375 if ((l2len | l3len | skb->csum_offset) % 2 ||
376 !FIELD_FIT(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) ||
377 !FIELD_FIT(FBNIC_TWD_L3_IHLEN_MASK, l3len / 2) ||
378 !FIELD_FIT(FBNIC_TWD_CSUM_OFFSET_MASK, skb->csum_offset / 2))
379 return features & ~NETIF_F_CSUM_MASK;
381 return features;
384 static void fbnic_clean_twq0(struct fbnic_napi_vector *nv, int napi_budget,
385 struct fbnic_ring *ring, bool discard,
386 unsigned int hw_head)
388 u64 total_bytes = 0, total_packets = 0, ts_lost = 0;
389 unsigned int head = ring->head;
390 struct netdev_queue *txq;
391 unsigned int clean_desc;
393 clean_desc = (hw_head - head) & ring->size_mask;
395 while (clean_desc) {
396 struct sk_buff *skb = ring->tx_buf[head];
397 unsigned int desc_cnt;
399 desc_cnt = FBNIC_XMIT_CB(skb)->desc_count;
400 if (desc_cnt > clean_desc)
401 break;
403 if (unlikely(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS)) {
404 FBNIC_XMIT_CB(skb)->hw_head = hw_head;
405 if (likely(!discard))
406 break;
407 ts_lost++;
410 ring->tx_buf[head] = NULL;
412 clean_desc -= desc_cnt;
414 while (!(ring->desc[head] & FBNIC_TWD_TYPE(AL))) {
415 head++;
416 head &= ring->size_mask;
417 desc_cnt--;
420 fbnic_unmap_single_twd(nv->dev, &ring->desc[head]);
421 head++;
422 head &= ring->size_mask;
423 desc_cnt--;
425 while (desc_cnt--) {
426 fbnic_unmap_page_twd(nv->dev, &ring->desc[head]);
427 head++;
428 head &= ring->size_mask;
431 total_bytes += FBNIC_XMIT_CB(skb)->bytecount;
432 total_packets += 1;
434 napi_consume_skb(skb, napi_budget);
437 if (!total_bytes)
438 return;
440 ring->head = head;
442 txq = txring_txq(nv->napi.dev, ring);
444 if (unlikely(discard)) {
445 u64_stats_update_begin(&ring->stats.syncp);
446 ring->stats.dropped += total_packets;
447 ring->stats.ts_lost += ts_lost;
448 u64_stats_update_end(&ring->stats.syncp);
450 netdev_tx_completed_queue(txq, total_packets, total_bytes);
451 return;
454 u64_stats_update_begin(&ring->stats.syncp);
455 ring->stats.bytes += total_bytes;
456 ring->stats.packets += total_packets;
457 u64_stats_update_end(&ring->stats.syncp);
459 netif_txq_completed_wake(txq, total_packets, total_bytes,
460 fbnic_desc_unused(ring),
461 FBNIC_TX_DESC_WAKEUP);
464 static void fbnic_clean_tsq(struct fbnic_napi_vector *nv,
465 struct fbnic_ring *ring,
466 u64 tcd, int *ts_head, int *head0)
468 struct skb_shared_hwtstamps hwtstamp;
469 struct fbnic_net *fbn;
470 struct sk_buff *skb;
471 int head;
472 u64 ns;
474 head = (*ts_head < 0) ? ring->head : *ts_head;
476 do {
477 unsigned int desc_cnt;
479 if (head == ring->tail) {
480 if (unlikely(net_ratelimit()))
481 netdev_err(nv->napi.dev,
482 "Tx timestamp without matching packet\n");
483 return;
486 skb = ring->tx_buf[head];
487 desc_cnt = FBNIC_XMIT_CB(skb)->desc_count;
489 head += desc_cnt;
490 head &= ring->size_mask;
491 } while (!(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS));
493 fbn = netdev_priv(nv->napi.dev);
494 ns = fbnic_ts40_to_ns(fbn, FIELD_GET(FBNIC_TCD_TYPE1_TS_MASK, tcd));
496 memset(&hwtstamp, 0, sizeof(hwtstamp));
497 hwtstamp.hwtstamp = ns_to_ktime(ns);
499 *ts_head = head;
501 FBNIC_XMIT_CB(skb)->flags &= ~FBNIC_XMIT_CB_TS;
502 if (*head0 < 0) {
503 head = FBNIC_XMIT_CB(skb)->hw_head;
504 if (head >= 0)
505 *head0 = head;
508 skb_tstamp_tx(skb, &hwtstamp);
509 u64_stats_update_begin(&ring->stats.syncp);
510 ring->stats.ts_packets++;
511 u64_stats_update_end(&ring->stats.syncp);
514 static void fbnic_page_pool_init(struct fbnic_ring *ring, unsigned int idx,
515 struct page *page)
517 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx];
519 page_pool_fragment_page(page, PAGECNT_BIAS_MAX);
520 rx_buf->pagecnt_bias = PAGECNT_BIAS_MAX;
521 rx_buf->page = page;
524 static struct page *fbnic_page_pool_get(struct fbnic_ring *ring,
525 unsigned int idx)
527 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx];
529 rx_buf->pagecnt_bias--;
531 return rx_buf->page;
534 static void fbnic_page_pool_drain(struct fbnic_ring *ring, unsigned int idx,
535 struct fbnic_napi_vector *nv, int budget)
537 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx];
538 struct page *page = rx_buf->page;
540 if (!page_pool_unref_page(page, rx_buf->pagecnt_bias))
541 page_pool_put_unrefed_page(nv->page_pool, page, -1, !!budget);
543 rx_buf->page = NULL;
546 static void fbnic_clean_twq(struct fbnic_napi_vector *nv, int napi_budget,
547 struct fbnic_q_triad *qt, s32 ts_head, s32 head0)
549 if (head0 >= 0)
550 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, head0);
551 else if (ts_head >= 0)
552 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, ts_head);
555 static void
556 fbnic_clean_tcq(struct fbnic_napi_vector *nv, struct fbnic_q_triad *qt,
557 int napi_budget)
559 struct fbnic_ring *cmpl = &qt->cmpl;
560 s32 head0 = -1, ts_head = -1;
561 __le64 *raw_tcd, done;
562 u32 head = cmpl->head;
564 done = (head & (cmpl->size_mask + 1)) ? 0 : cpu_to_le64(FBNIC_TCD_DONE);
565 raw_tcd = &cmpl->desc[head & cmpl->size_mask];
567 /* Walk the completion queue collecting the heads reported by NIC */
568 while ((*raw_tcd & cpu_to_le64(FBNIC_TCD_DONE)) == done) {
569 u64 tcd;
571 dma_rmb();
573 tcd = le64_to_cpu(*raw_tcd);
575 switch (FIELD_GET(FBNIC_TCD_TYPE_MASK, tcd)) {
576 case FBNIC_TCD_TYPE_0:
577 if (!(tcd & FBNIC_TCD_TWQ1))
578 head0 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD0_MASK,
579 tcd);
580 /* Currently all err status bits are related to
581 * timestamps and as those have yet to be added
582 * they are skipped for now.
584 break;
585 case FBNIC_TCD_TYPE_1:
586 if (WARN_ON_ONCE(tcd & FBNIC_TCD_TWQ1))
587 break;
589 fbnic_clean_tsq(nv, &qt->sub0, tcd, &ts_head, &head0);
590 break;
591 default:
592 break;
595 raw_tcd++;
596 head++;
597 if (!(head & cmpl->size_mask)) {
598 done ^= cpu_to_le64(FBNIC_TCD_DONE);
599 raw_tcd = &cmpl->desc[0];
603 /* Record the current head/tail of the queue */
604 if (cmpl->head != head) {
605 cmpl->head = head;
606 writel(head & cmpl->size_mask, cmpl->doorbell);
609 /* Unmap and free processed buffers */
610 fbnic_clean_twq(nv, napi_budget, qt, ts_head, head0);
613 static void fbnic_clean_bdq(struct fbnic_napi_vector *nv, int napi_budget,
614 struct fbnic_ring *ring, unsigned int hw_head)
616 unsigned int head = ring->head;
618 if (head == hw_head)
619 return;
621 do {
622 fbnic_page_pool_drain(ring, head, nv, napi_budget);
624 head++;
625 head &= ring->size_mask;
626 } while (head != hw_head);
628 ring->head = head;
631 static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, struct page *page)
633 __le64 *bdq_desc = &bdq->desc[id * FBNIC_BD_FRAG_COUNT];
634 dma_addr_t dma = page_pool_get_dma_addr(page);
635 u64 bd, i = FBNIC_BD_FRAG_COUNT;
637 bd = (FBNIC_BD_PAGE_ADDR_MASK & dma) |
638 FIELD_PREP(FBNIC_BD_PAGE_ID_MASK, id);
640 /* In the case that a page size is larger than 4K we will map a
641 * single page to multiple fragments. The fragments will be
642 * FBNIC_BD_FRAG_COUNT in size and the lower n bits will be use
643 * to indicate the individual fragment IDs.
645 do {
646 *bdq_desc = cpu_to_le64(bd);
647 bd += FIELD_PREP(FBNIC_BD_DESC_ADDR_MASK, 1) |
648 FIELD_PREP(FBNIC_BD_DESC_ID_MASK, 1);
649 } while (--i);
652 static void fbnic_fill_bdq(struct fbnic_napi_vector *nv, struct fbnic_ring *bdq)
654 unsigned int count = fbnic_desc_unused(bdq);
655 unsigned int i = bdq->tail;
657 if (!count)
658 return;
660 do {
661 struct page *page;
663 page = page_pool_dev_alloc_pages(nv->page_pool);
664 if (!page)
665 break;
667 fbnic_page_pool_init(bdq, i, page);
668 fbnic_bd_prep(bdq, i, page);
670 i++;
671 i &= bdq->size_mask;
673 count--;
674 } while (count);
676 if (bdq->tail != i) {
677 bdq->tail = i;
679 /* Force DMA writes to flush before writing to tail */
680 dma_wmb();
682 writel(i, bdq->doorbell);
686 static unsigned int fbnic_hdr_pg_start(unsigned int pg_off)
688 /* The headroom of the first header may be larger than FBNIC_RX_HROOM
689 * due to alignment. So account for that by just making the page
690 * offset 0 if we are starting at the first header.
692 if (ALIGN(FBNIC_RX_HROOM, 128) > FBNIC_RX_HROOM &&
693 pg_off == ALIGN(FBNIC_RX_HROOM, 128))
694 return 0;
696 return pg_off - FBNIC_RX_HROOM;
699 static unsigned int fbnic_hdr_pg_end(unsigned int pg_off, unsigned int len)
701 /* Determine the end of the buffer by finding the start of the next
702 * and then subtracting the headroom from that frame.
704 pg_off += len + FBNIC_RX_TROOM + FBNIC_RX_HROOM;
706 return ALIGN(pg_off, 128) - FBNIC_RX_HROOM;
709 static void fbnic_pkt_prepare(struct fbnic_napi_vector *nv, u64 rcd,
710 struct fbnic_pkt_buff *pkt,
711 struct fbnic_q_triad *qt)
713 unsigned int hdr_pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
714 unsigned int hdr_pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd);
715 struct page *page = fbnic_page_pool_get(&qt->sub0, hdr_pg_idx);
716 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd);
717 unsigned int frame_sz, hdr_pg_start, hdr_pg_end, headroom;
718 unsigned char *hdr_start;
720 /* data_hard_start should always be NULL when this is called */
721 WARN_ON_ONCE(pkt->buff.data_hard_start);
723 /* Short-cut the end calculation if we know page is fully consumed */
724 hdr_pg_end = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ?
725 FBNIC_BD_FRAG_SIZE : fbnic_hdr_pg_end(hdr_pg_off, len);
726 hdr_pg_start = fbnic_hdr_pg_start(hdr_pg_off);
728 headroom = hdr_pg_off - hdr_pg_start + FBNIC_RX_PAD;
729 frame_sz = hdr_pg_end - hdr_pg_start;
730 xdp_init_buff(&pkt->buff, frame_sz, NULL);
731 hdr_pg_start += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) *
732 FBNIC_BD_FRAG_SIZE;
734 /* Sync DMA buffer */
735 dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page),
736 hdr_pg_start, frame_sz,
737 DMA_BIDIRECTIONAL);
739 /* Build frame around buffer */
740 hdr_start = page_address(page) + hdr_pg_start;
742 xdp_prepare_buff(&pkt->buff, hdr_start, headroom,
743 len - FBNIC_RX_PAD, true);
745 pkt->data_truesize = 0;
746 pkt->data_len = 0;
747 pkt->nr_frags = 0;
750 static void fbnic_add_rx_frag(struct fbnic_napi_vector *nv, u64 rcd,
751 struct fbnic_pkt_buff *pkt,
752 struct fbnic_q_triad *qt)
754 unsigned int pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
755 unsigned int pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd);
756 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd);
757 struct page *page = fbnic_page_pool_get(&qt->sub1, pg_idx);
758 struct skb_shared_info *shinfo;
759 unsigned int truesize;
761 truesize = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ?
762 FBNIC_BD_FRAG_SIZE - pg_off : ALIGN(len, 128);
764 pg_off += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) *
765 FBNIC_BD_FRAG_SIZE;
767 /* Sync DMA buffer */
768 dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page),
769 pg_off, truesize, DMA_BIDIRECTIONAL);
771 /* Add page to xdp shared info */
772 shinfo = xdp_get_shared_info_from_buff(&pkt->buff);
774 /* We use gso_segs to store truesize */
775 pkt->data_truesize += truesize;
777 __skb_fill_page_desc_noacc(shinfo, pkt->nr_frags++, page, pg_off, len);
779 /* Store data_len in gso_size */
780 pkt->data_len += len;
783 static void fbnic_put_pkt_buff(struct fbnic_napi_vector *nv,
784 struct fbnic_pkt_buff *pkt, int budget)
786 struct skb_shared_info *shinfo;
787 struct page *page;
788 int nr_frags;
790 if (!pkt->buff.data_hard_start)
791 return;
793 shinfo = xdp_get_shared_info_from_buff(&pkt->buff);
794 nr_frags = pkt->nr_frags;
796 while (nr_frags--) {
797 page = skb_frag_page(&shinfo->frags[nr_frags]);
798 page_pool_put_full_page(nv->page_pool, page, !!budget);
801 page = virt_to_page(pkt->buff.data_hard_start);
802 page_pool_put_full_page(nv->page_pool, page, !!budget);
805 static struct sk_buff *fbnic_build_skb(struct fbnic_napi_vector *nv,
806 struct fbnic_pkt_buff *pkt)
808 unsigned int nr_frags = pkt->nr_frags;
809 struct skb_shared_info *shinfo;
810 unsigned int truesize;
811 struct sk_buff *skb;
813 truesize = xdp_data_hard_end(&pkt->buff) + FBNIC_RX_TROOM -
814 pkt->buff.data_hard_start;
816 /* Build frame around buffer */
817 skb = napi_build_skb(pkt->buff.data_hard_start, truesize);
818 if (unlikely(!skb))
819 return NULL;
821 /* Push data pointer to start of data, put tail to end of data */
822 skb_reserve(skb, pkt->buff.data - pkt->buff.data_hard_start);
823 __skb_put(skb, pkt->buff.data_end - pkt->buff.data);
825 /* Add tracking for metadata at the start of the frame */
826 skb_metadata_set(skb, pkt->buff.data - pkt->buff.data_meta);
828 /* Add Rx frags */
829 if (nr_frags) {
830 /* Verify that shared info didn't move */
831 shinfo = xdp_get_shared_info_from_buff(&pkt->buff);
832 WARN_ON(skb_shinfo(skb) != shinfo);
834 skb->truesize += pkt->data_truesize;
835 skb->data_len += pkt->data_len;
836 shinfo->nr_frags = nr_frags;
837 skb->len += pkt->data_len;
840 skb_mark_for_recycle(skb);
842 /* Set MAC header specific fields */
843 skb->protocol = eth_type_trans(skb, nv->napi.dev);
845 /* Add timestamp if present */
846 if (pkt->hwtstamp)
847 skb_hwtstamps(skb)->hwtstamp = pkt->hwtstamp;
849 return skb;
852 static enum pkt_hash_types fbnic_skb_hash_type(u64 rcd)
854 return (FBNIC_RCD_META_L4_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L4 :
855 (FBNIC_RCD_META_L3_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L3 :
856 PKT_HASH_TYPE_L2;
859 static void fbnic_rx_tstamp(struct fbnic_napi_vector *nv, u64 rcd,
860 struct fbnic_pkt_buff *pkt)
862 struct fbnic_net *fbn;
863 u64 ns, ts;
865 if (!FIELD_GET(FBNIC_RCD_OPT_META_TS, rcd))
866 return;
868 fbn = netdev_priv(nv->napi.dev);
869 ts = FIELD_GET(FBNIC_RCD_OPT_META_TS_MASK, rcd);
870 ns = fbnic_ts40_to_ns(fbn, ts);
872 /* Add timestamp to shared info */
873 pkt->hwtstamp = ns_to_ktime(ns);
876 static void fbnic_populate_skb_fields(struct fbnic_napi_vector *nv,
877 u64 rcd, struct sk_buff *skb,
878 struct fbnic_q_triad *qt)
880 struct net_device *netdev = nv->napi.dev;
881 struct fbnic_ring *rcq = &qt->cmpl;
883 fbnic_rx_csum(rcd, skb, rcq);
885 if (netdev->features & NETIF_F_RXHASH)
886 skb_set_hash(skb,
887 FIELD_GET(FBNIC_RCD_META_RSS_HASH_MASK, rcd),
888 fbnic_skb_hash_type(rcd));
890 skb_record_rx_queue(skb, rcq->q_idx);
893 static bool fbnic_rcd_metadata_err(u64 rcd)
895 return !!(FBNIC_RCD_META_UNCORRECTABLE_ERR_MASK & rcd);
898 static int fbnic_clean_rcq(struct fbnic_napi_vector *nv,
899 struct fbnic_q_triad *qt, int budget)
901 unsigned int packets = 0, bytes = 0, dropped = 0;
902 struct fbnic_ring *rcq = &qt->cmpl;
903 struct fbnic_pkt_buff *pkt;
904 s32 head0 = -1, head1 = -1;
905 __le64 *raw_rcd, done;
906 u32 head = rcq->head;
908 done = (head & (rcq->size_mask + 1)) ? cpu_to_le64(FBNIC_RCD_DONE) : 0;
909 raw_rcd = &rcq->desc[head & rcq->size_mask];
910 pkt = rcq->pkt;
912 /* Walk the completion queue collecting the heads reported by NIC */
913 while (likely(packets < budget)) {
914 struct sk_buff *skb = ERR_PTR(-EINVAL);
915 u64 rcd;
917 if ((*raw_rcd & cpu_to_le64(FBNIC_RCD_DONE)) == done)
918 break;
920 dma_rmb();
922 rcd = le64_to_cpu(*raw_rcd);
924 switch (FIELD_GET(FBNIC_RCD_TYPE_MASK, rcd)) {
925 case FBNIC_RCD_TYPE_HDR_AL:
926 head0 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
927 fbnic_pkt_prepare(nv, rcd, pkt, qt);
929 break;
930 case FBNIC_RCD_TYPE_PAY_AL:
931 head1 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd);
932 fbnic_add_rx_frag(nv, rcd, pkt, qt);
934 break;
935 case FBNIC_RCD_TYPE_OPT_META:
936 /* Only type 0 is currently supported */
937 if (FIELD_GET(FBNIC_RCD_OPT_META_TYPE_MASK, rcd))
938 break;
940 fbnic_rx_tstamp(nv, rcd, pkt);
942 /* We currently ignore the action table index */
943 break;
944 case FBNIC_RCD_TYPE_META:
945 if (likely(!fbnic_rcd_metadata_err(rcd)))
946 skb = fbnic_build_skb(nv, pkt);
948 /* Populate skb and invalidate XDP */
949 if (!IS_ERR_OR_NULL(skb)) {
950 fbnic_populate_skb_fields(nv, rcd, skb, qt);
952 packets++;
953 bytes += skb->len;
955 napi_gro_receive(&nv->napi, skb);
956 } else {
957 dropped++;
958 fbnic_put_pkt_buff(nv, pkt, 1);
961 pkt->buff.data_hard_start = NULL;
963 break;
966 raw_rcd++;
967 head++;
968 if (!(head & rcq->size_mask)) {
969 done ^= cpu_to_le64(FBNIC_RCD_DONE);
970 raw_rcd = &rcq->desc[0];
974 u64_stats_update_begin(&rcq->stats.syncp);
975 rcq->stats.packets += packets;
976 rcq->stats.bytes += bytes;
977 /* Re-add ethernet header length (removed in fbnic_build_skb) */
978 rcq->stats.bytes += ETH_HLEN * packets;
979 rcq->stats.dropped += dropped;
980 u64_stats_update_end(&rcq->stats.syncp);
982 /* Unmap and free processed buffers */
983 if (head0 >= 0)
984 fbnic_clean_bdq(nv, budget, &qt->sub0, head0);
985 fbnic_fill_bdq(nv, &qt->sub0);
987 if (head1 >= 0)
988 fbnic_clean_bdq(nv, budget, &qt->sub1, head1);
989 fbnic_fill_bdq(nv, &qt->sub1);
991 /* Record the current head/tail of the queue */
992 if (rcq->head != head) {
993 rcq->head = head;
994 writel(head & rcq->size_mask, rcq->doorbell);
997 return packets;
1000 static void fbnic_nv_irq_disable(struct fbnic_napi_vector *nv)
1002 struct fbnic_dev *fbd = nv->fbd;
1003 u32 v_idx = nv->v_idx;
1005 fbnic_wr32(fbd, FBNIC_INTR_MASK_SET(v_idx / 32), 1 << (v_idx % 32));
1008 static void fbnic_nv_irq_rearm(struct fbnic_napi_vector *nv)
1010 struct fbnic_dev *fbd = nv->fbd;
1011 u32 v_idx = nv->v_idx;
1013 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(v_idx),
1014 FBNIC_INTR_CQ_REARM_INTR_UNMASK);
1017 static int fbnic_poll(struct napi_struct *napi, int budget)
1019 struct fbnic_napi_vector *nv = container_of(napi,
1020 struct fbnic_napi_vector,
1021 napi);
1022 int i, j, work_done = 0;
1024 for (i = 0; i < nv->txt_count; i++)
1025 fbnic_clean_tcq(nv, &nv->qt[i], budget);
1027 for (j = 0; j < nv->rxt_count; j++, i++)
1028 work_done += fbnic_clean_rcq(nv, &nv->qt[i], budget);
1030 if (work_done >= budget)
1031 return budget;
1033 if (likely(napi_complete_done(napi, work_done)))
1034 fbnic_nv_irq_rearm(nv);
1036 return 0;
1039 static irqreturn_t fbnic_msix_clean_rings(int __always_unused irq, void *data)
1041 struct fbnic_napi_vector *nv = data;
1043 napi_schedule_irqoff(&nv->napi);
1045 return IRQ_HANDLED;
1048 static void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn,
1049 struct fbnic_ring *rxr)
1051 struct fbnic_queue_stats *stats = &rxr->stats;
1053 /* Capture stats from queues before dissasociating them */
1054 fbn->rx_stats.bytes += stats->bytes;
1055 fbn->rx_stats.packets += stats->packets;
1056 fbn->rx_stats.dropped += stats->dropped;
1059 static void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn,
1060 struct fbnic_ring *txr)
1062 struct fbnic_queue_stats *stats = &txr->stats;
1064 /* Capture stats from queues before dissasociating them */
1065 fbn->tx_stats.bytes += stats->bytes;
1066 fbn->tx_stats.packets += stats->packets;
1067 fbn->tx_stats.dropped += stats->dropped;
1068 fbn->tx_stats.ts_lost += stats->ts_lost;
1069 fbn->tx_stats.ts_packets += stats->ts_packets;
1072 static void fbnic_remove_tx_ring(struct fbnic_net *fbn,
1073 struct fbnic_ring *txr)
1075 if (!(txr->flags & FBNIC_RING_F_STATS))
1076 return;
1078 fbnic_aggregate_ring_tx_counters(fbn, txr);
1080 /* Remove pointer to the Tx ring */
1081 WARN_ON(fbn->tx[txr->q_idx] && fbn->tx[txr->q_idx] != txr);
1082 fbn->tx[txr->q_idx] = NULL;
1085 static void fbnic_remove_rx_ring(struct fbnic_net *fbn,
1086 struct fbnic_ring *rxr)
1088 if (!(rxr->flags & FBNIC_RING_F_STATS))
1089 return;
1091 fbnic_aggregate_ring_rx_counters(fbn, rxr);
1093 /* Remove pointer to the Rx ring */
1094 WARN_ON(fbn->rx[rxr->q_idx] && fbn->rx[rxr->q_idx] != rxr);
1095 fbn->rx[rxr->q_idx] = NULL;
1098 static void fbnic_free_napi_vector(struct fbnic_net *fbn,
1099 struct fbnic_napi_vector *nv)
1101 struct fbnic_dev *fbd = nv->fbd;
1102 u32 v_idx = nv->v_idx;
1103 int i, j;
1105 for (i = 0; i < nv->txt_count; i++) {
1106 fbnic_remove_tx_ring(fbn, &nv->qt[i].sub0);
1107 fbnic_remove_tx_ring(fbn, &nv->qt[i].cmpl);
1110 for (j = 0; j < nv->rxt_count; j++, i++) {
1111 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub0);
1112 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub1);
1113 fbnic_remove_rx_ring(fbn, &nv->qt[i].cmpl);
1116 fbnic_free_irq(fbd, v_idx, nv);
1117 page_pool_destroy(nv->page_pool);
1118 netif_napi_del(&nv->napi);
1119 list_del(&nv->napis);
1120 kfree(nv);
1123 void fbnic_free_napi_vectors(struct fbnic_net *fbn)
1125 struct fbnic_napi_vector *nv, *temp;
1127 list_for_each_entry_safe(nv, temp, &fbn->napis, napis)
1128 fbnic_free_napi_vector(fbn, nv);
1131 static void fbnic_name_napi_vector(struct fbnic_napi_vector *nv)
1133 unsigned char *dev_name = nv->napi.dev->name;
1135 if (!nv->rxt_count)
1136 snprintf(nv->name, sizeof(nv->name), "%s-Tx-%u", dev_name,
1137 nv->v_idx - FBNIC_NON_NAPI_VECTORS);
1138 else
1139 snprintf(nv->name, sizeof(nv->name), "%s-TxRx-%u", dev_name,
1140 nv->v_idx - FBNIC_NON_NAPI_VECTORS);
1143 #define FBNIC_PAGE_POOL_FLAGS \
1144 (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV)
1146 static int fbnic_alloc_nv_page_pool(struct fbnic_net *fbn,
1147 struct fbnic_napi_vector *nv)
1149 struct page_pool_params pp_params = {
1150 .order = 0,
1151 .flags = FBNIC_PAGE_POOL_FLAGS,
1152 .pool_size = (fbn->hpq_size + fbn->ppq_size) * nv->rxt_count,
1153 .nid = NUMA_NO_NODE,
1154 .dev = nv->dev,
1155 .dma_dir = DMA_BIDIRECTIONAL,
1156 .offset = 0,
1157 .max_len = PAGE_SIZE
1159 struct page_pool *pp;
1161 /* Page pool cannot exceed a size of 32768. This doesn't limit the
1162 * pages on the ring but the number we can have cached waiting on
1163 * the next use.
1165 * TBD: Can this be reduced further? Would a multiple of
1166 * NAPI_POLL_WEIGHT possibly make more sense? The question is how
1167 * may pages do we need to hold in reserve to get the best return
1168 * without hogging too much system memory.
1170 if (pp_params.pool_size > 32768)
1171 pp_params.pool_size = 32768;
1173 pp = page_pool_create(&pp_params);
1174 if (IS_ERR(pp))
1175 return PTR_ERR(pp);
1177 nv->page_pool = pp;
1179 return 0;
1182 static void fbnic_ring_init(struct fbnic_ring *ring, u32 __iomem *doorbell,
1183 int q_idx, u8 flags)
1185 u64_stats_init(&ring->stats.syncp);
1186 ring->doorbell = doorbell;
1187 ring->q_idx = q_idx;
1188 ring->flags = flags;
1191 static int fbnic_alloc_napi_vector(struct fbnic_dev *fbd, struct fbnic_net *fbn,
1192 unsigned int v_count, unsigned int v_idx,
1193 unsigned int txq_count, unsigned int txq_idx,
1194 unsigned int rxq_count, unsigned int rxq_idx)
1196 int txt_count = txq_count, rxt_count = rxq_count;
1197 u32 __iomem *uc_addr = fbd->uc_addr0;
1198 struct fbnic_napi_vector *nv;
1199 struct fbnic_q_triad *qt;
1200 int qt_count, err;
1201 u32 __iomem *db;
1203 qt_count = txt_count + rxq_count;
1204 if (!qt_count)
1205 return -EINVAL;
1207 /* If MMIO has already failed there are no rings to initialize */
1208 if (!uc_addr)
1209 return -EIO;
1211 /* Allocate NAPI vector and queue triads */
1212 nv = kzalloc(struct_size(nv, qt, qt_count), GFP_KERNEL);
1213 if (!nv)
1214 return -ENOMEM;
1216 /* Record queue triad counts */
1217 nv->txt_count = txt_count;
1218 nv->rxt_count = rxt_count;
1220 /* Provide pointer back to fbnic and MSI-X vectors */
1221 nv->fbd = fbd;
1222 nv->v_idx = v_idx;
1224 /* Tie napi to netdev */
1225 list_add(&nv->napis, &fbn->napis);
1226 netif_napi_add(fbn->netdev, &nv->napi, fbnic_poll);
1228 /* Record IRQ to NAPI struct */
1229 netif_napi_set_irq(&nv->napi,
1230 pci_irq_vector(to_pci_dev(fbd->dev), nv->v_idx));
1232 /* Tie nv back to PCIe dev */
1233 nv->dev = fbd->dev;
1235 /* Allocate page pool */
1236 if (rxq_count) {
1237 err = fbnic_alloc_nv_page_pool(fbn, nv);
1238 if (err)
1239 goto napi_del;
1242 /* Initialize vector name */
1243 fbnic_name_napi_vector(nv);
1245 /* Request the IRQ for napi vector */
1246 err = fbnic_request_irq(fbd, v_idx, &fbnic_msix_clean_rings,
1247 IRQF_SHARED, nv->name, nv);
1248 if (err)
1249 goto pp_destroy;
1251 /* Initialize queue triads */
1252 qt = nv->qt;
1254 while (txt_count) {
1255 /* Configure Tx queue */
1256 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ0_TAIL];
1258 /* Assign Tx queue to netdev if applicable */
1259 if (txq_count > 0) {
1260 u8 flags = FBNIC_RING_F_CTX | FBNIC_RING_F_STATS;
1262 fbnic_ring_init(&qt->sub0, db, txq_idx, flags);
1263 fbn->tx[txq_idx] = &qt->sub0;
1264 txq_count--;
1265 } else {
1266 fbnic_ring_init(&qt->sub0, db, 0,
1267 FBNIC_RING_F_DISABLED);
1270 /* Configure Tx completion queue */
1271 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TCQ_HEAD];
1272 fbnic_ring_init(&qt->cmpl, db, 0, 0);
1274 /* Update Tx queue index */
1275 txt_count--;
1276 txq_idx += v_count;
1278 /* Move to next queue triad */
1279 qt++;
1282 while (rxt_count) {
1283 /* Configure header queue */
1284 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_HPQ_TAIL];
1285 fbnic_ring_init(&qt->sub0, db, 0, FBNIC_RING_F_CTX);
1287 /* Configure payload queue */
1288 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_PPQ_TAIL];
1289 fbnic_ring_init(&qt->sub1, db, 0, FBNIC_RING_F_CTX);
1291 /* Configure Rx completion queue */
1292 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_RCQ_HEAD];
1293 fbnic_ring_init(&qt->cmpl, db, rxq_idx, FBNIC_RING_F_STATS);
1294 fbn->rx[rxq_idx] = &qt->cmpl;
1296 /* Update Rx queue index */
1297 rxt_count--;
1298 rxq_idx += v_count;
1300 /* Move to next queue triad */
1301 qt++;
1304 return 0;
1306 pp_destroy:
1307 page_pool_destroy(nv->page_pool);
1308 napi_del:
1309 netif_napi_del(&nv->napi);
1310 list_del(&nv->napis);
1311 kfree(nv);
1312 return err;
1315 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn)
1317 unsigned int txq_idx = 0, rxq_idx = 0, v_idx = FBNIC_NON_NAPI_VECTORS;
1318 unsigned int num_tx = fbn->num_tx_queues;
1319 unsigned int num_rx = fbn->num_rx_queues;
1320 unsigned int num_napi = fbn->num_napi;
1321 struct fbnic_dev *fbd = fbn->fbd;
1322 int err;
1324 /* Allocate 1 Tx queue per napi vector */
1325 if (num_napi < FBNIC_MAX_TXQS && num_napi == num_tx + num_rx) {
1326 while (num_tx) {
1327 err = fbnic_alloc_napi_vector(fbd, fbn,
1328 num_napi, v_idx,
1329 1, txq_idx, 0, 0);
1330 if (err)
1331 goto free_vectors;
1333 /* Update counts and index */
1334 num_tx--;
1335 txq_idx++;
1337 v_idx++;
1341 /* Allocate Tx/Rx queue pairs per vector, or allocate remaining Rx */
1342 while (num_rx | num_tx) {
1343 int tqpv = DIV_ROUND_UP(num_tx, num_napi - txq_idx);
1344 int rqpv = DIV_ROUND_UP(num_rx, num_napi - rxq_idx);
1346 err = fbnic_alloc_napi_vector(fbd, fbn, num_napi, v_idx,
1347 tqpv, txq_idx, rqpv, rxq_idx);
1348 if (err)
1349 goto free_vectors;
1351 /* Update counts and index */
1352 num_tx -= tqpv;
1353 txq_idx++;
1355 num_rx -= rqpv;
1356 rxq_idx++;
1358 v_idx++;
1361 return 0;
1363 free_vectors:
1364 fbnic_free_napi_vectors(fbn);
1366 return -ENOMEM;
1369 static void fbnic_free_ring_resources(struct device *dev,
1370 struct fbnic_ring *ring)
1372 kvfree(ring->buffer);
1373 ring->buffer = NULL;
1375 /* If size is not set there are no descriptors present */
1376 if (!ring->size)
1377 return;
1379 dma_free_coherent(dev, ring->size, ring->desc, ring->dma);
1380 ring->size_mask = 0;
1381 ring->size = 0;
1384 static int fbnic_alloc_tx_ring_desc(struct fbnic_net *fbn,
1385 struct fbnic_ring *txr)
1387 struct device *dev = fbn->netdev->dev.parent;
1388 size_t size;
1390 /* Round size up to nearest 4K */
1391 size = ALIGN(array_size(sizeof(*txr->desc), fbn->txq_size), 4096);
1393 txr->desc = dma_alloc_coherent(dev, size, &txr->dma,
1394 GFP_KERNEL | __GFP_NOWARN);
1395 if (!txr->desc)
1396 return -ENOMEM;
1398 /* txq_size should be a power of 2, so mask is just that -1 */
1399 txr->size_mask = fbn->txq_size - 1;
1400 txr->size = size;
1402 return 0;
1405 static int fbnic_alloc_tx_ring_buffer(struct fbnic_ring *txr)
1407 size_t size = array_size(sizeof(*txr->tx_buf), txr->size_mask + 1);
1409 txr->tx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1411 return txr->tx_buf ? 0 : -ENOMEM;
1414 static int fbnic_alloc_tx_ring_resources(struct fbnic_net *fbn,
1415 struct fbnic_ring *txr)
1417 struct device *dev = fbn->netdev->dev.parent;
1418 int err;
1420 if (txr->flags & FBNIC_RING_F_DISABLED)
1421 return 0;
1423 err = fbnic_alloc_tx_ring_desc(fbn, txr);
1424 if (err)
1425 return err;
1427 if (!(txr->flags & FBNIC_RING_F_CTX))
1428 return 0;
1430 err = fbnic_alloc_tx_ring_buffer(txr);
1431 if (err)
1432 goto free_desc;
1434 return 0;
1436 free_desc:
1437 fbnic_free_ring_resources(dev, txr);
1438 return err;
1441 static int fbnic_alloc_rx_ring_desc(struct fbnic_net *fbn,
1442 struct fbnic_ring *rxr)
1444 struct device *dev = fbn->netdev->dev.parent;
1445 size_t desc_size = sizeof(*rxr->desc);
1446 u32 rxq_size;
1447 size_t size;
1449 switch (rxr->doorbell - fbnic_ring_csr_base(rxr)) {
1450 case FBNIC_QUEUE_BDQ_HPQ_TAIL:
1451 rxq_size = fbn->hpq_size / FBNIC_BD_FRAG_COUNT;
1452 desc_size *= FBNIC_BD_FRAG_COUNT;
1453 break;
1454 case FBNIC_QUEUE_BDQ_PPQ_TAIL:
1455 rxq_size = fbn->ppq_size / FBNIC_BD_FRAG_COUNT;
1456 desc_size *= FBNIC_BD_FRAG_COUNT;
1457 break;
1458 case FBNIC_QUEUE_RCQ_HEAD:
1459 rxq_size = fbn->rcq_size;
1460 break;
1461 default:
1462 return -EINVAL;
1465 /* Round size up to nearest 4K */
1466 size = ALIGN(array_size(desc_size, rxq_size), 4096);
1468 rxr->desc = dma_alloc_coherent(dev, size, &rxr->dma,
1469 GFP_KERNEL | __GFP_NOWARN);
1470 if (!rxr->desc)
1471 return -ENOMEM;
1473 /* rxq_size should be a power of 2, so mask is just that -1 */
1474 rxr->size_mask = rxq_size - 1;
1475 rxr->size = size;
1477 return 0;
1480 static int fbnic_alloc_rx_ring_buffer(struct fbnic_ring *rxr)
1482 size_t size = array_size(sizeof(*rxr->rx_buf), rxr->size_mask + 1);
1484 if (rxr->flags & FBNIC_RING_F_CTX)
1485 size = sizeof(*rxr->rx_buf) * (rxr->size_mask + 1);
1486 else
1487 size = sizeof(*rxr->pkt);
1489 rxr->rx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1491 return rxr->rx_buf ? 0 : -ENOMEM;
1494 static int fbnic_alloc_rx_ring_resources(struct fbnic_net *fbn,
1495 struct fbnic_ring *rxr)
1497 struct device *dev = fbn->netdev->dev.parent;
1498 int err;
1500 err = fbnic_alloc_rx_ring_desc(fbn, rxr);
1501 if (err)
1502 return err;
1504 err = fbnic_alloc_rx_ring_buffer(rxr);
1505 if (err)
1506 goto free_desc;
1508 return 0;
1510 free_desc:
1511 fbnic_free_ring_resources(dev, rxr);
1512 return err;
1515 static void fbnic_free_qt_resources(struct fbnic_net *fbn,
1516 struct fbnic_q_triad *qt)
1518 struct device *dev = fbn->netdev->dev.parent;
1520 fbnic_free_ring_resources(dev, &qt->cmpl);
1521 fbnic_free_ring_resources(dev, &qt->sub1);
1522 fbnic_free_ring_resources(dev, &qt->sub0);
1525 static int fbnic_alloc_tx_qt_resources(struct fbnic_net *fbn,
1526 struct fbnic_q_triad *qt)
1528 struct device *dev = fbn->netdev->dev.parent;
1529 int err;
1531 err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub0);
1532 if (err)
1533 return err;
1535 err = fbnic_alloc_tx_ring_resources(fbn, &qt->cmpl);
1536 if (err)
1537 goto free_sub1;
1539 return 0;
1541 free_sub1:
1542 fbnic_free_ring_resources(dev, &qt->sub0);
1543 return err;
1546 static int fbnic_alloc_rx_qt_resources(struct fbnic_net *fbn,
1547 struct fbnic_q_triad *qt)
1549 struct device *dev = fbn->netdev->dev.parent;
1550 int err;
1552 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub0);
1553 if (err)
1554 return err;
1556 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub1);
1557 if (err)
1558 goto free_sub0;
1560 err = fbnic_alloc_rx_ring_resources(fbn, &qt->cmpl);
1561 if (err)
1562 goto free_sub1;
1564 return 0;
1566 free_sub1:
1567 fbnic_free_ring_resources(dev, &qt->sub1);
1568 free_sub0:
1569 fbnic_free_ring_resources(dev, &qt->sub0);
1570 return err;
1573 static void fbnic_free_nv_resources(struct fbnic_net *fbn,
1574 struct fbnic_napi_vector *nv)
1576 int i, j;
1578 /* Free Tx Resources */
1579 for (i = 0; i < nv->txt_count; i++)
1580 fbnic_free_qt_resources(fbn, &nv->qt[i]);
1582 for (j = 0; j < nv->rxt_count; j++, i++)
1583 fbnic_free_qt_resources(fbn, &nv->qt[i]);
1586 static int fbnic_alloc_nv_resources(struct fbnic_net *fbn,
1587 struct fbnic_napi_vector *nv)
1589 int i, j, err;
1591 /* Allocate Tx Resources */
1592 for (i = 0; i < nv->txt_count; i++) {
1593 err = fbnic_alloc_tx_qt_resources(fbn, &nv->qt[i]);
1594 if (err)
1595 goto free_resources;
1598 /* Allocate Rx Resources */
1599 for (j = 0; j < nv->rxt_count; j++, i++) {
1600 err = fbnic_alloc_rx_qt_resources(fbn, &nv->qt[i]);
1601 if (err)
1602 goto free_resources;
1605 return 0;
1607 free_resources:
1608 while (i--)
1609 fbnic_free_qt_resources(fbn, &nv->qt[i]);
1610 return err;
1613 void fbnic_free_resources(struct fbnic_net *fbn)
1615 struct fbnic_napi_vector *nv;
1617 list_for_each_entry(nv, &fbn->napis, napis)
1618 fbnic_free_nv_resources(fbn, nv);
1621 int fbnic_alloc_resources(struct fbnic_net *fbn)
1623 struct fbnic_napi_vector *nv;
1624 int err = -ENODEV;
1626 list_for_each_entry(nv, &fbn->napis, napis) {
1627 err = fbnic_alloc_nv_resources(fbn, nv);
1628 if (err)
1629 goto free_resources;
1632 return 0;
1634 free_resources:
1635 list_for_each_entry_continue_reverse(nv, &fbn->napis, napis)
1636 fbnic_free_nv_resources(fbn, nv);
1638 return err;
1641 static void fbnic_disable_twq0(struct fbnic_ring *txr)
1643 u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ0_CTL);
1645 twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE;
1647 fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ0_CTL, twq_ctl);
1650 static void fbnic_disable_tcq(struct fbnic_ring *txr)
1652 fbnic_ring_wr32(txr, FBNIC_QUEUE_TCQ_CTL, 0);
1653 fbnic_ring_wr32(txr, FBNIC_QUEUE_TIM_MASK, FBNIC_QUEUE_TIM_MASK_MASK);
1656 static void fbnic_disable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq)
1658 u32 bdq_ctl = fbnic_ring_rd32(hpq, FBNIC_QUEUE_BDQ_CTL);
1660 bdq_ctl &= ~FBNIC_QUEUE_BDQ_CTL_ENABLE;
1662 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl);
1665 static void fbnic_disable_rcq(struct fbnic_ring *rxr)
1667 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RCQ_CTL, 0);
1668 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RIM_MASK, FBNIC_QUEUE_RIM_MASK_MASK);
1671 void fbnic_napi_disable(struct fbnic_net *fbn)
1673 struct fbnic_napi_vector *nv;
1675 list_for_each_entry(nv, &fbn->napis, napis) {
1676 napi_disable(&nv->napi);
1678 fbnic_nv_irq_disable(nv);
1682 void fbnic_disable(struct fbnic_net *fbn)
1684 struct fbnic_dev *fbd = fbn->fbd;
1685 struct fbnic_napi_vector *nv;
1686 int i, j;
1688 list_for_each_entry(nv, &fbn->napis, napis) {
1689 /* Disable Tx queue triads */
1690 for (i = 0; i < nv->txt_count; i++) {
1691 struct fbnic_q_triad *qt = &nv->qt[i];
1693 fbnic_disable_twq0(&qt->sub0);
1694 fbnic_disable_tcq(&qt->cmpl);
1697 /* Disable Rx queue triads */
1698 for (j = 0; j < nv->rxt_count; j++, i++) {
1699 struct fbnic_q_triad *qt = &nv->qt[i];
1701 fbnic_disable_bdq(&qt->sub0, &qt->sub1);
1702 fbnic_disable_rcq(&qt->cmpl);
1706 fbnic_wrfl(fbd);
1709 static void fbnic_tx_flush(struct fbnic_dev *fbd)
1711 netdev_warn(fbd->netdev, "triggering Tx flush\n");
1713 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN,
1714 FBNIC_TMI_DROP_CTRL_EN);
1717 static void fbnic_tx_flush_off(struct fbnic_dev *fbd)
1719 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 0);
1722 struct fbnic_idle_regs {
1723 u32 reg_base;
1724 u8 reg_cnt;
1727 static bool fbnic_all_idle(struct fbnic_dev *fbd,
1728 const struct fbnic_idle_regs *regs,
1729 unsigned int nregs)
1731 unsigned int i, j;
1733 for (i = 0; i < nregs; i++) {
1734 for (j = 0; j < regs[i].reg_cnt; j++) {
1735 if (fbnic_rd32(fbd, regs[i].reg_base + j) != ~0U)
1736 return false;
1739 return true;
1742 static void fbnic_idle_dump(struct fbnic_dev *fbd,
1743 const struct fbnic_idle_regs *regs,
1744 unsigned int nregs, const char *dir, int err)
1746 unsigned int i, j;
1748 netdev_err(fbd->netdev, "error waiting for %s idle %d\n", dir, err);
1749 for (i = 0; i < nregs; i++)
1750 for (j = 0; j < regs[i].reg_cnt; j++)
1751 netdev_err(fbd->netdev, "0x%04x: %08x\n",
1752 regs[i].reg_base + j,
1753 fbnic_rd32(fbd, regs[i].reg_base + j));
1756 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail)
1758 static const struct fbnic_idle_regs tx[] = {
1759 { FBNIC_QM_TWQ_IDLE(0), FBNIC_QM_TWQ_IDLE_CNT, },
1760 { FBNIC_QM_TQS_IDLE(0), FBNIC_QM_TQS_IDLE_CNT, },
1761 { FBNIC_QM_TDE_IDLE(0), FBNIC_QM_TDE_IDLE_CNT, },
1762 { FBNIC_QM_TCQ_IDLE(0), FBNIC_QM_TCQ_IDLE_CNT, },
1763 }, rx[] = {
1764 { FBNIC_QM_HPQ_IDLE(0), FBNIC_QM_HPQ_IDLE_CNT, },
1765 { FBNIC_QM_PPQ_IDLE(0), FBNIC_QM_PPQ_IDLE_CNT, },
1766 { FBNIC_QM_RCQ_IDLE(0), FBNIC_QM_RCQ_IDLE_CNT, },
1768 bool idle;
1769 int err;
1771 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000,
1772 false, fbd, tx, ARRAY_SIZE(tx));
1773 if (err == -ETIMEDOUT) {
1774 fbnic_tx_flush(fbd);
1775 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle,
1776 2, 500000, false,
1777 fbd, tx, ARRAY_SIZE(tx));
1778 fbnic_tx_flush_off(fbd);
1780 if (err) {
1781 fbnic_idle_dump(fbd, tx, ARRAY_SIZE(tx), "Tx", err);
1782 if (may_fail)
1783 return err;
1786 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000,
1787 false, fbd, rx, ARRAY_SIZE(rx));
1788 if (err)
1789 fbnic_idle_dump(fbd, rx, ARRAY_SIZE(rx), "Rx", err);
1790 return err;
1793 void fbnic_flush(struct fbnic_net *fbn)
1795 struct fbnic_napi_vector *nv;
1797 list_for_each_entry(nv, &fbn->napis, napis) {
1798 int i, j;
1800 /* Flush any processed Tx Queue Triads and drop the rest */
1801 for (i = 0; i < nv->txt_count; i++) {
1802 struct fbnic_q_triad *qt = &nv->qt[i];
1803 struct netdev_queue *tx_queue;
1805 /* Clean the work queues of unprocessed work */
1806 fbnic_clean_twq0(nv, 0, &qt->sub0, true, qt->sub0.tail);
1808 /* Reset completion queue descriptor ring */
1809 memset(qt->cmpl.desc, 0, qt->cmpl.size);
1811 /* Nothing else to do if Tx queue is disabled */
1812 if (qt->sub0.flags & FBNIC_RING_F_DISABLED)
1813 continue;
1815 /* Reset BQL associated with Tx queue */
1816 tx_queue = netdev_get_tx_queue(nv->napi.dev,
1817 qt->sub0.q_idx);
1818 netdev_tx_reset_queue(tx_queue);
1820 /* Disassociate Tx queue from NAPI */
1821 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx,
1822 NETDEV_QUEUE_TYPE_TX, NULL);
1825 /* Flush any processed Rx Queue Triads and drop the rest */
1826 for (j = 0; j < nv->rxt_count; j++, i++) {
1827 struct fbnic_q_triad *qt = &nv->qt[i];
1829 /* Clean the work queues of unprocessed work */
1830 fbnic_clean_bdq(nv, 0, &qt->sub0, qt->sub0.tail);
1831 fbnic_clean_bdq(nv, 0, &qt->sub1, qt->sub1.tail);
1833 /* Reset completion queue descriptor ring */
1834 memset(qt->cmpl.desc, 0, qt->cmpl.size);
1836 fbnic_put_pkt_buff(nv, qt->cmpl.pkt, 0);
1837 qt->cmpl.pkt->buff.data_hard_start = NULL;
1839 /* Disassociate Rx queue from NAPI */
1840 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx,
1841 NETDEV_QUEUE_TYPE_RX, NULL);
1846 void fbnic_fill(struct fbnic_net *fbn)
1848 struct fbnic_napi_vector *nv;
1850 list_for_each_entry(nv, &fbn->napis, napis) {
1851 int i, j;
1853 /* Configure NAPI mapping for Tx */
1854 for (i = 0; i < nv->txt_count; i++) {
1855 struct fbnic_q_triad *qt = &nv->qt[i];
1857 /* Nothing to do if Tx queue is disabled */
1858 if (qt->sub0.flags & FBNIC_RING_F_DISABLED)
1859 continue;
1861 /* Associate Tx queue with NAPI */
1862 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx,
1863 NETDEV_QUEUE_TYPE_TX, &nv->napi);
1866 /* Configure NAPI mapping and populate pages
1867 * in the BDQ rings to use for Rx
1869 for (j = 0; j < nv->rxt_count; j++, i++) {
1870 struct fbnic_q_triad *qt = &nv->qt[i];
1872 /* Associate Rx queue with NAPI */
1873 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx,
1874 NETDEV_QUEUE_TYPE_RX, &nv->napi);
1876 /* Populate the header and payload BDQs */
1877 fbnic_fill_bdq(nv, &qt->sub0);
1878 fbnic_fill_bdq(nv, &qt->sub1);
1883 static void fbnic_enable_twq0(struct fbnic_ring *twq)
1885 u32 log_size = fls(twq->size_mask);
1887 if (!twq->size_mask)
1888 return;
1890 /* Reset head/tail */
1891 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_RESET);
1892 twq->tail = 0;
1893 twq->head = 0;
1895 /* Store descriptor ring address and size */
1896 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAL, lower_32_bits(twq->dma));
1897 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAH, upper_32_bits(twq->dma));
1899 /* Write lower 4 bits of log size as 64K ring size is 0 */
1900 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_SIZE, log_size & 0xf);
1902 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE);
1905 static void fbnic_enable_tcq(struct fbnic_napi_vector *nv,
1906 struct fbnic_ring *tcq)
1908 u32 log_size = fls(tcq->size_mask);
1910 if (!tcq->size_mask)
1911 return;
1913 /* Reset head/tail */
1914 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_RESET);
1915 tcq->tail = 0;
1916 tcq->head = 0;
1918 /* Store descriptor ring address and size */
1919 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAL, lower_32_bits(tcq->dma));
1920 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAH, upper_32_bits(tcq->dma));
1922 /* Write lower 4 bits of log size as 64K ring size is 0 */
1923 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_SIZE, log_size & 0xf);
1925 /* Store interrupt information for the completion queue */
1926 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_CTL, nv->v_idx);
1927 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_THRESHOLD, tcq->size_mask / 2);
1928 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_MASK, 0);
1930 /* Enable queue */
1931 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_ENABLE);
1934 static void fbnic_enable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq)
1936 u32 bdq_ctl = FBNIC_QUEUE_BDQ_CTL_ENABLE;
1937 u32 log_size;
1939 /* Reset head/tail */
1940 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, FBNIC_QUEUE_BDQ_CTL_RESET);
1941 ppq->tail = 0;
1942 ppq->head = 0;
1943 hpq->tail = 0;
1944 hpq->head = 0;
1946 log_size = fls(hpq->size_mask);
1948 /* Store descriptor ring address and size */
1949 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAL, lower_32_bits(hpq->dma));
1950 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAH, upper_32_bits(hpq->dma));
1952 /* Write lower 4 bits of log size as 64K ring size is 0 */
1953 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_SIZE, log_size & 0xf);
1955 if (!ppq->size_mask)
1956 goto write_ctl;
1958 log_size = fls(ppq->size_mask);
1960 /* Add enabling of PPQ to BDQ control */
1961 bdq_ctl |= FBNIC_QUEUE_BDQ_CTL_PPQ_ENABLE;
1963 /* Store descriptor ring address and size */
1964 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAL, lower_32_bits(ppq->dma));
1965 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAH, upper_32_bits(ppq->dma));
1966 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_SIZE, log_size & 0xf);
1968 write_ctl:
1969 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl);
1972 static void fbnic_config_drop_mode_rcq(struct fbnic_napi_vector *nv,
1973 struct fbnic_ring *rcq)
1975 u32 drop_mode, rcq_ctl;
1977 drop_mode = FBNIC_QUEUE_RDE_CTL0_DROP_IMMEDIATE;
1979 /* Specify packet layout */
1980 rcq_ctl = FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_DROP_MODE_MASK, drop_mode) |
1981 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_HROOM_MASK, FBNIC_RX_HROOM) |
1982 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_TROOM_MASK, FBNIC_RX_TROOM);
1984 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL0, rcq_ctl);
1987 static void fbnic_enable_rcq(struct fbnic_napi_vector *nv,
1988 struct fbnic_ring *rcq)
1990 u32 log_size = fls(rcq->size_mask);
1991 u32 rcq_ctl;
1993 fbnic_config_drop_mode_rcq(nv, rcq);
1995 rcq_ctl = FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PADLEN_MASK, FBNIC_RX_PAD) |
1996 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_MAX_HDR_MASK,
1997 FBNIC_RX_MAX_HDR) |
1998 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_OFF_MASK,
1999 FBNIC_RX_PAYLD_OFFSET) |
2000 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_PG_CL_MASK,
2001 FBNIC_RX_PAYLD_PG_CL);
2002 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL1, rcq_ctl);
2004 /* Reset head/tail */
2005 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_RESET);
2006 rcq->head = 0;
2007 rcq->tail = 0;
2009 /* Store descriptor ring address and size */
2010 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAL, lower_32_bits(rcq->dma));
2011 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAH, upper_32_bits(rcq->dma));
2013 /* Write lower 4 bits of log size as 64K ring size is 0 */
2014 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_SIZE, log_size & 0xf);
2016 /* Store interrupt information for the completion queue */
2017 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_CTL, nv->v_idx);
2018 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_THRESHOLD, rcq->size_mask / 2);
2019 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_MASK, 0);
2021 /* Enable queue */
2022 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_ENABLE);
2025 void fbnic_enable(struct fbnic_net *fbn)
2027 struct fbnic_dev *fbd = fbn->fbd;
2028 struct fbnic_napi_vector *nv;
2029 int i, j;
2031 list_for_each_entry(nv, &fbn->napis, napis) {
2032 /* Setup Tx Queue Triads */
2033 for (i = 0; i < nv->txt_count; i++) {
2034 struct fbnic_q_triad *qt = &nv->qt[i];
2036 fbnic_enable_twq0(&qt->sub0);
2037 fbnic_enable_tcq(nv, &qt->cmpl);
2040 /* Setup Rx Queue Triads */
2041 for (j = 0; j < nv->rxt_count; j++, i++) {
2042 struct fbnic_q_triad *qt = &nv->qt[i];
2044 fbnic_enable_bdq(&qt->sub0, &qt->sub1);
2045 fbnic_config_drop_mode_rcq(nv, &qt->cmpl);
2046 fbnic_enable_rcq(nv, &qt->cmpl);
2050 fbnic_wrfl(fbd);
2053 static void fbnic_nv_irq_enable(struct fbnic_napi_vector *nv)
2055 struct fbnic_dev *fbd = nv->fbd;
2056 u32 val;
2058 val = FBNIC_INTR_CQ_REARM_INTR_UNMASK;
2060 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(nv->v_idx), val);
2063 void fbnic_napi_enable(struct fbnic_net *fbn)
2065 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {};
2066 struct fbnic_dev *fbd = fbn->fbd;
2067 struct fbnic_napi_vector *nv;
2068 int i;
2070 list_for_each_entry(nv, &fbn->napis, napis) {
2071 napi_enable(&nv->napi);
2073 fbnic_nv_irq_enable(nv);
2075 /* Record bit used for NAPI IRQs so we can
2076 * set the mask appropriately
2078 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32);
2081 /* Force the first interrupt on the device to guarantee
2082 * that any packets that may have been enqueued during the
2083 * bringup are processed.
2085 for (i = 0; i < ARRAY_SIZE(irqs); i++) {
2086 if (!irqs[i])
2087 continue;
2088 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]);
2091 fbnic_wrfl(fbd);
2094 void fbnic_napi_depletion_check(struct net_device *netdev)
2096 struct fbnic_net *fbn = netdev_priv(netdev);
2097 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {};
2098 struct fbnic_dev *fbd = fbn->fbd;
2099 struct fbnic_napi_vector *nv;
2100 int i, j;
2102 list_for_each_entry(nv, &fbn->napis, napis) {
2103 /* Find RQs which are completely out of pages */
2104 for (i = nv->txt_count, j = 0; j < nv->rxt_count; j++, i++) {
2105 /* Assume 4 pages is always enough to fit a packet
2106 * and therefore generate a completion and an IRQ.
2108 if (fbnic_desc_used(&nv->qt[i].sub0) < 4 ||
2109 fbnic_desc_used(&nv->qt[i].sub1) < 4)
2110 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32);
2114 for (i = 0; i < ARRAY_SIZE(irqs); i++) {
2115 if (!irqs[i])
2116 continue;
2117 fbnic_wr32(fbd, FBNIC_INTR_MASK_CLEAR(i), irqs[i]);
2118 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]);
2121 fbnic_wrfl(fbd);