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