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>
7 #include <net/netdev_queues.h>
8 #include <net/page_pool/helpers.h>
11 #include "fbnic_csr.h"
12 #include "fbnic_netdev.h"
13 #include "fbnic_txrx.h"
16 FBNIC_XMIT_CB_TS
= 0x01,
19 struct fbnic_xmit_cb
{
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
);
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
)
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.
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
);
114 res
= netif_txq_maybe_stop(txq
, fbnic_desc_unused(ring
), size
,
115 FBNIC_TX_DESC_WAKEUP
);
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
);
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
);
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
))
165 fbn
= netdev_priv(skb
->dev
);
166 if (fbn
->hwtstamp_config
.tx_type
== HWTSTAMP_TX_OFF
)
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;
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
))
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));
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
)))
208 if (FIELD_GET(FBNIC_RCD_META_L4_CSUM_UNNECESSARY
, rcd
)) {
209 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
211 u16 csum
= FIELD_GET(FBNIC_RCD_META_L2_CSUM_MASK
, rcd
);
213 skb
->ip_summed
= CHECKSUM_COMPLETE
;
214 skb
->csum
= (__force __wsum
)csum
;
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
;
228 ring
->tx_buf
[tail
] = skb
;
231 tail
&= ring
->size_mask
;
234 size
= skb_headlen(skb
);
235 data_len
= skb
->data_len
;
237 if (size
> FIELD_MAX(FBNIC_TWD_LEN_MASK
))
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
))
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
,
254 tail
&= ring
->size_mask
;
259 size
= skb_frag_size(frag
);
262 if (size
> FIELD_MAX(FBNIC_TWD_LEN_MASK
))
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
;
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 */
286 writel(tail
, ring
->doorbell
);
292 netdev_err(skb
->dev
, "TX DMA map failed\n");
294 while (tail
!= first
) {
296 tail
&= ring
->size_mask
;
297 twd
= &ring
->desc
[tail
];
299 fbnic_unmap_single_twd(dev
, twd
);
301 fbnic_unmap_page_twd(dev
, twd
);
307 #define FBNIC_MIN_FRAME_LEN 60
310 fbnic_xmit_frame_ring(struct sk_buff
*skb
, struct fbnic_ring
*ring
)
312 __le64
*meta
= &ring
->desc
[ring
->tail
];
315 if (skb_put_padto(skb
, FBNIC_MIN_FRAME_LEN
))
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
))
337 if (fbnic_tx_map(ring
, skb
, meta
))
343 dev_kfree_skb_any(skb
);
345 u64_stats_update_begin(&ring
->stats
.syncp
);
346 ring
->stats
.dropped
++;
347 u64_stats_update_end(&ring
->stats
.syncp
);
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
]);
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
))
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
;
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
;
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
)
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
))
410 ring
->tx_buf
[head
] = NULL
;
412 clean_desc
-= desc_cnt
;
414 while (!(ring
->desc
[head
] & FBNIC_TWD_TYPE(AL
))) {
416 head
&= ring
->size_mask
;
420 fbnic_unmap_single_twd(nv
->dev
, &ring
->desc
[head
]);
422 head
&= ring
->size_mask
;
426 fbnic_unmap_page_twd(nv
->dev
, &ring
->desc
[head
]);
428 head
&= ring
->size_mask
;
431 total_bytes
+= FBNIC_XMIT_CB(skb
)->bytecount
;
434 napi_consume_skb(skb
, napi_budget
);
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
);
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
;
474 head
= (*ts_head
< 0) ? ring
->head
: *ts_head
;
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");
486 skb
= ring
->tx_buf
[head
];
487 desc_cnt
= FBNIC_XMIT_CB(skb
)->desc_count
;
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
);
501 FBNIC_XMIT_CB(skb
)->flags
&= ~FBNIC_XMIT_CB_TS
;
503 head
= FBNIC_XMIT_CB(skb
)->hw_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
,
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
;
524 static struct page
*fbnic_page_pool_get(struct fbnic_ring
*ring
,
527 struct fbnic_rx_buf
*rx_buf
= &ring
->rx_buf
[idx
];
529 rx_buf
->pagecnt_bias
--;
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
);
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
)
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
);
556 fbnic_clean_tcq(struct fbnic_napi_vector
*nv
, struct fbnic_q_triad
*qt
,
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
) {
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
,
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.
585 case FBNIC_TCD_TYPE_1
:
586 if (WARN_ON_ONCE(tcd
& FBNIC_TCD_TWQ1
))
589 fbnic_clean_tsq(nv
, &qt
->sub0
, tcd
, &ts_head
, &head0
);
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
) {
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
;
622 fbnic_page_pool_drain(ring
, head
, nv
, napi_budget
);
625 head
&= ring
->size_mask
;
626 } while (head
!= hw_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.
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);
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
;
663 page
= page_pool_dev_alloc_pages(nv
->page_pool
);
667 fbnic_page_pool_init(bdq
, i
, page
);
668 fbnic_bd_prep(bdq
, i
, page
);
676 if (bdq
->tail
!= i
) {
679 /* Force DMA writes to flush before writing to tail */
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))
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
) *
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
,
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;
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
) *
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
;
790 if (!pkt
->buff
.data_hard_start
)
793 shinfo
= xdp_get_shared_info_from_buff(&pkt
->buff
);
794 nr_frags
= pkt
->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
;
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
);
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
);
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 */
847 skb_hwtstamps(skb
)->hwtstamp
= pkt
->hwtstamp
;
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
:
859 static void fbnic_rx_tstamp(struct fbnic_napi_vector
*nv
, u64 rcd
,
860 struct fbnic_pkt_buff
*pkt
)
862 struct fbnic_net
*fbn
;
865 if (!FIELD_GET(FBNIC_RCD_OPT_META_TS
, rcd
))
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
)
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
];
912 /* Walk the completion queue collecting the heads reported by NIC */
913 while (likely(packets
< budget
)) {
914 struct sk_buff
*skb
= ERR_PTR(-EINVAL
);
917 if ((*raw_rcd
& cpu_to_le64(FBNIC_RCD_DONE
)) == done
)
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
);
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
);
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
))
940 fbnic_rx_tstamp(nv
, rcd
, pkt
);
942 /* We currently ignore the action table index */
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
);
955 napi_gro_receive(&nv
->napi
, skb
);
958 fbnic_put_pkt_buff(nv
, pkt
, 1);
961 pkt
->buff
.data_hard_start
= NULL
;
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 */
984 fbnic_clean_bdq(nv
, budget
, &qt
->sub0
, head0
);
985 fbnic_fill_bdq(nv
, &qt
->sub0
);
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
) {
994 writel(head
& rcq
->size_mask
, rcq
->doorbell
);
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
,
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
)
1033 if (likely(napi_complete_done(napi
, work_done
)))
1034 fbnic_nv_irq_rearm(nv
);
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
);
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
))
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
))
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
;
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
);
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
;
1136 snprintf(nv
->name
, sizeof(nv
->name
), "%s-Tx-%u", dev_name
,
1137 nv
->v_idx
- FBNIC_NON_NAPI_VECTORS
);
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
= {
1151 .flags
= FBNIC_PAGE_POOL_FLAGS
,
1152 .pool_size
= (fbn
->hpq_size
+ fbn
->ppq_size
) * nv
->rxt_count
,
1153 .nid
= NUMA_NO_NODE
,
1155 .dma_dir
= DMA_BIDIRECTIONAL
,
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
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
);
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
;
1203 qt_count
= txt_count
+ rxq_count
;
1207 /* If MMIO has already failed there are no rings to initialize */
1211 /* Allocate NAPI vector and queue triads */
1212 nv
= kzalloc(struct_size(nv
, qt
, qt_count
), GFP_KERNEL
);
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 */
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 */
1235 /* Allocate page pool */
1237 err
= fbnic_alloc_nv_page_pool(fbn
, nv
);
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
);
1251 /* Initialize queue triads */
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
;
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 */
1278 /* Move to next queue triad */
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 */
1300 /* Move to next queue triad */
1307 page_pool_destroy(nv
->page_pool
);
1309 netif_napi_del(&nv
->napi
);
1310 list_del(&nv
->napis
);
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
;
1324 /* Allocate 1 Tx queue per napi vector */
1325 if (num_napi
< FBNIC_MAX_TXQS
&& num_napi
== num_tx
+ num_rx
) {
1327 err
= fbnic_alloc_napi_vector(fbd
, fbn
,
1333 /* Update counts and index */
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
);
1351 /* Update counts and index */
1364 fbnic_free_napi_vectors(fbn
);
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 */
1379 dma_free_coherent(dev
, ring
->size
, ring
->desc
, ring
->dma
);
1380 ring
->size_mask
= 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
;
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
);
1398 /* txq_size should be a power of 2, so mask is just that -1 */
1399 txr
->size_mask
= fbn
->txq_size
- 1;
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
;
1420 if (txr
->flags
& FBNIC_RING_F_DISABLED
)
1423 err
= fbnic_alloc_tx_ring_desc(fbn
, txr
);
1427 if (!(txr
->flags
& FBNIC_RING_F_CTX
))
1430 err
= fbnic_alloc_tx_ring_buffer(txr
);
1437 fbnic_free_ring_resources(dev
, txr
);
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
);
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
;
1454 case FBNIC_QUEUE_BDQ_PPQ_TAIL
:
1455 rxq_size
= fbn
->ppq_size
/ FBNIC_BD_FRAG_COUNT
;
1456 desc_size
*= FBNIC_BD_FRAG_COUNT
;
1458 case FBNIC_QUEUE_RCQ_HEAD
:
1459 rxq_size
= fbn
->rcq_size
;
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
);
1473 /* rxq_size should be a power of 2, so mask is just that -1 */
1474 rxr
->size_mask
= rxq_size
- 1;
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);
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
;
1500 err
= fbnic_alloc_rx_ring_desc(fbn
, rxr
);
1504 err
= fbnic_alloc_rx_ring_buffer(rxr
);
1511 fbnic_free_ring_resources(dev
, rxr
);
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
;
1531 err
= fbnic_alloc_tx_ring_resources(fbn
, &qt
->sub0
);
1535 err
= fbnic_alloc_tx_ring_resources(fbn
, &qt
->cmpl
);
1542 fbnic_free_ring_resources(dev
, &qt
->sub0
);
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
;
1552 err
= fbnic_alloc_rx_ring_resources(fbn
, &qt
->sub0
);
1556 err
= fbnic_alloc_rx_ring_resources(fbn
, &qt
->sub1
);
1560 err
= fbnic_alloc_rx_ring_resources(fbn
, &qt
->cmpl
);
1567 fbnic_free_ring_resources(dev
, &qt
->sub1
);
1569 fbnic_free_ring_resources(dev
, &qt
->sub0
);
1573 static void fbnic_free_nv_resources(struct fbnic_net
*fbn
,
1574 struct fbnic_napi_vector
*nv
)
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
)
1591 /* Allocate Tx Resources */
1592 for (i
= 0; i
< nv
->txt_count
; i
++) {
1593 err
= fbnic_alloc_tx_qt_resources(fbn
, &nv
->qt
[i
]);
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
]);
1602 goto free_resources
;
1609 fbnic_free_qt_resources(fbn
, &nv
->qt
[i
]);
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
;
1626 list_for_each_entry(nv
, &fbn
->napis
, napis
) {
1627 err
= fbnic_alloc_nv_resources(fbn
, nv
);
1629 goto free_resources
;
1635 list_for_each_entry_continue_reverse(nv
, &fbn
->napis
, napis
)
1636 fbnic_free_nv_resources(fbn
, nv
);
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
;
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
);
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
{
1727 static bool fbnic_all_idle(struct fbnic_dev
*fbd
,
1728 const struct fbnic_idle_regs
*regs
,
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)
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
)
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
, },
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
, },
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
,
1777 fbd
, tx
, ARRAY_SIZE(tx
));
1778 fbnic_tx_flush_off(fbd
);
1781 fbnic_idle_dump(fbd
, tx
, ARRAY_SIZE(tx
), "Tx", err
);
1786 err
= read_poll_timeout_atomic(fbnic_all_idle
, idle
, idle
, 2, 500000,
1787 false, fbd
, rx
, ARRAY_SIZE(rx
));
1789 fbnic_idle_dump(fbd
, rx
, ARRAY_SIZE(rx
), "Rx", err
);
1793 void fbnic_flush(struct fbnic_net
*fbn
)
1795 struct fbnic_napi_vector
*nv
;
1797 list_for_each_entry(nv
, &fbn
->napis
, napis
) {
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
)
1815 /* Reset BQL associated with Tx queue */
1816 tx_queue
= netdev_get_tx_queue(nv
->napi
.dev
,
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
) {
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
)
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
)
1890 /* Reset head/tail */
1891 fbnic_ring_wr32(twq
, FBNIC_QUEUE_TWQ0_CTL
, FBNIC_QUEUE_TWQ_CTL_RESET
);
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
)
1913 /* Reset head/tail */
1914 fbnic_ring_wr32(tcq
, FBNIC_QUEUE_TCQ_CTL
, FBNIC_QUEUE_TCQ_CTL_RESET
);
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);
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
;
1939 /* Reset head/tail */
1940 fbnic_ring_wr32(hpq
, FBNIC_QUEUE_BDQ_CTL
, FBNIC_QUEUE_BDQ_CTL_RESET
);
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
)
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);
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
);
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
,
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
);
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);
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
;
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
);
2053 static void fbnic_nv_irq_enable(struct fbnic_napi_vector
*nv
)
2055 struct fbnic_dev
*fbd
= nv
->fbd
;
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
;
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
++) {
2088 fbnic_wr32(fbd
, FBNIC_INTR_SET(i
), irqs
[i
]);
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
;
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
++) {
2117 fbnic_wr32(fbd
, FBNIC_INTR_MASK_CLEAR(i
), irqs
[i
]);
2118 fbnic_wr32(fbd
, FBNIC_INTR_SET(i
), irqs
[i
]);