1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Atlantic Network Driver
3 *
4 * Copyright (C) 2014-2019 aQuantia Corporation
5 * Copyright (C) 2019-2020 Marvell International Ltd.
6 */
7
8 /* File aq_ring.c: Definition of functions for Rx/Tx rings. */
9
10 #include "aq_nic.h"
11 #include "aq_hw.h"
12 #include "aq_hw_utils.h"
13 #include "aq_ptp.h"
14 #include "aq_vec.h"
15 #include "aq_main.h"
16
17 #include <net/xdp.h>
18 #include <linux/filter.h>
19 #include <linux/bpf_trace.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22
aq_get_rxpages_xdp(struct aq_ring_buff_s * buff,struct xdp_buff * xdp)23 static void aq_get_rxpages_xdp(struct aq_ring_buff_s *buff,
24 struct xdp_buff *xdp)
25 {
26 struct skb_shared_info *sinfo;
27 int i;
28
29 if (xdp_buff_has_frags(xdp)) {
30 sinfo = xdp_get_shared_info_from_buff(xdp);
31
32 for (i = 0; i < sinfo->nr_frags; i++) {
33 skb_frag_t *frag = &sinfo->frags[i];
34
35 page_ref_inc(skb_frag_page(frag));
36 }
37 }
38 page_ref_inc(buff->rxdata.page);
39 }
40
aq_free_rxpage(struct aq_rxpage * rxpage,struct device * dev)41 static inline void aq_free_rxpage(struct aq_rxpage *rxpage, struct device *dev)
42 {
43 unsigned int len = PAGE_SIZE << rxpage->order;
44
45 dma_unmap_page(dev, rxpage->daddr, len, DMA_FROM_DEVICE);
46
47 /* Drop the ref for being in the ring. */
48 __free_pages(rxpage->page, rxpage->order);
49 rxpage->page = NULL;
50 }
51
aq_alloc_rxpages(struct aq_rxpage * rxpage,struct aq_ring_s * rx_ring)52 static int aq_alloc_rxpages(struct aq_rxpage *rxpage, struct aq_ring_s *rx_ring)
53 {
54 struct device *dev = aq_nic_get_dev(rx_ring->aq_nic);
55 unsigned int order = rx_ring->page_order;
56 struct page *page;
57 int ret = -ENOMEM;
58 dma_addr_t daddr;
59
60 page = dev_alloc_pages(order);
61 if (unlikely(!page))
62 goto err_exit;
63
64 daddr = dma_map_page(dev, page, 0, PAGE_SIZE << order,
65 DMA_FROM_DEVICE);
66
67 if (unlikely(dma_mapping_error(dev, daddr)))
68 goto free_page;
69
70 rxpage->page = page;
71 rxpage->daddr = daddr;
72 rxpage->order = order;
73 rxpage->pg_off = rx_ring->page_offset;
74
75 return 0;
76
77 free_page:
78 __free_pages(page, order);
79
80 err_exit:
81 return ret;
82 }
83
aq_get_rxpages(struct aq_ring_s * self,struct aq_ring_buff_s * rxbuf)84 static int aq_get_rxpages(struct aq_ring_s *self, struct aq_ring_buff_s *rxbuf)
85 {
86 unsigned int order = self->page_order;
87 u16 page_offset = self->page_offset;
88 u16 frame_max = self->frame_max;
89 u16 tail_size = self->tail_size;
90 int ret;
91
92 if (rxbuf->rxdata.page) {
93 /* One means ring is the only user and can reuse */
94 if (page_ref_count(rxbuf->rxdata.page) > 1) {
95 /* Try reuse buffer */
96 rxbuf->rxdata.pg_off += frame_max + page_offset +
97 tail_size;
98 if (rxbuf->rxdata.pg_off + frame_max + tail_size <=
99 (PAGE_SIZE << order)) {
100 u64_stats_update_begin(&self->stats.rx.syncp);
101 self->stats.rx.pg_flips++;
102 u64_stats_update_end(&self->stats.rx.syncp);
103
104 } else {
105 /* Buffer exhausted. We have other users and
106 * should release this page and realloc
107 */
108 aq_free_rxpage(&rxbuf->rxdata,
109 aq_nic_get_dev(self->aq_nic));
110 u64_stats_update_begin(&self->stats.rx.syncp);
111 self->stats.rx.pg_losts++;
112 u64_stats_update_end(&self->stats.rx.syncp);
113 }
114 } else {
115 rxbuf->rxdata.pg_off = page_offset;
116 u64_stats_update_begin(&self->stats.rx.syncp);
117 self->stats.rx.pg_reuses++;
118 u64_stats_update_end(&self->stats.rx.syncp);
119 }
120 }
121
122 if (!rxbuf->rxdata.page) {
123 ret = aq_alloc_rxpages(&rxbuf->rxdata, self);
124 if (ret) {
125 u64_stats_update_begin(&self->stats.rx.syncp);
126 self->stats.rx.alloc_fails++;
127 u64_stats_update_end(&self->stats.rx.syncp);
128 }
129 return ret;
130 }
131
132 return 0;
133 }
134
aq_ring_alloc(struct aq_ring_s * self,struct aq_nic_s * aq_nic)135 static int aq_ring_alloc(struct aq_ring_s *self,
136 struct aq_nic_s *aq_nic)
137 {
138 int err = 0;
139
140 self->buff_ring =
141 kcalloc(self->size, sizeof(struct aq_ring_buff_s), GFP_KERNEL);
142
143 if (!self->buff_ring) {
144 err = -ENOMEM;
145 goto err_exit;
146 }
147
148 self->dx_ring = dma_alloc_coherent(aq_nic_get_dev(aq_nic),
149 self->size * self->dx_size,
150 &self->dx_ring_pa, GFP_KERNEL);
151 if (!self->dx_ring) {
152 err = -ENOMEM;
153 goto err_exit;
154 }
155
156 err_exit:
157 if (err < 0) {
158 aq_ring_free(self);
159 }
160
161 return err;
162 }
163
aq_ring_tx_alloc(struct aq_ring_s * self,struct aq_nic_s * aq_nic,unsigned int idx,struct aq_nic_cfg_s * aq_nic_cfg)164 int aq_ring_tx_alloc(struct aq_ring_s *self,
165 struct aq_nic_s *aq_nic,
166 unsigned int idx,
167 struct aq_nic_cfg_s *aq_nic_cfg)
168 {
169 self->aq_nic = aq_nic;
170 self->idx = idx;
171 self->size = aq_nic_cfg->txds;
172 self->dx_size = aq_nic_cfg->aq_hw_caps->txd_size;
173
174 return aq_ring_alloc(self, aq_nic);
175 }
176
aq_ring_rx_alloc(struct aq_ring_s * self,struct aq_nic_s * aq_nic,unsigned int idx,struct aq_nic_cfg_s * aq_nic_cfg)177 int aq_ring_rx_alloc(struct aq_ring_s *self,
178 struct aq_nic_s *aq_nic,
179 unsigned int idx,
180 struct aq_nic_cfg_s *aq_nic_cfg)
181 {
182 self->aq_nic = aq_nic;
183 self->idx = idx;
184 self->size = aq_nic_cfg->rxds;
185 self->dx_size = aq_nic_cfg->aq_hw_caps->rxd_size;
186 self->xdp_prog = aq_nic->xdp_prog;
187 self->frame_max = AQ_CFG_RX_FRAME_MAX;
188
189 /* Only order-2 is allowed if XDP is enabled */
190 if (READ_ONCE(self->xdp_prog)) {
191 self->page_offset = AQ_XDP_HEADROOM;
192 self->page_order = AQ_CFG_XDP_PAGEORDER;
193 self->tail_size = AQ_XDP_TAILROOM;
194 } else {
195 self->page_offset = 0;
196 self->page_order = fls(self->frame_max / PAGE_SIZE +
197 (self->frame_max % PAGE_SIZE ? 1 : 0)) - 1;
198 if (aq_nic_cfg->rxpageorder > self->page_order)
199 self->page_order = aq_nic_cfg->rxpageorder;
200 self->tail_size = 0;
201 }
202
203 return aq_ring_alloc(self, aq_nic);
204 }
205
206 int
aq_ring_hwts_rx_alloc(struct aq_ring_s * self,struct aq_nic_s * aq_nic,unsigned int idx,unsigned int size,unsigned int dx_size)207 aq_ring_hwts_rx_alloc(struct aq_ring_s *self, struct aq_nic_s *aq_nic,
208 unsigned int idx, unsigned int size, unsigned int dx_size)
209 {
210 struct device *dev = aq_nic_get_dev(aq_nic);
211 size_t sz = size * dx_size + AQ_CFG_RXDS_DEF;
212
213 memset(self, 0, sizeof(*self));
214
215 self->aq_nic = aq_nic;
216 self->idx = idx;
217 self->size = size;
218 self->dx_size = dx_size;
219
220 self->dx_ring = dma_alloc_coherent(dev, sz, &self->dx_ring_pa,
221 GFP_KERNEL);
222 if (!self->dx_ring) {
223 aq_ring_free(self);
224 return -ENOMEM;
225 }
226
227 return 0;
228 }
229
aq_ring_init(struct aq_ring_s * self,const enum atl_ring_type ring_type)230 int aq_ring_init(struct aq_ring_s *self, const enum atl_ring_type ring_type)
231 {
232 self->hw_head = 0;
233 self->sw_head = 0;
234 self->sw_tail = 0;
235 self->ring_type = ring_type;
236
237 if (self->ring_type == ATL_RING_RX)
238 u64_stats_init(&self->stats.rx.syncp);
239 else
240 u64_stats_init(&self->stats.tx.syncp);
241
242 return 0;
243 }
244
aq_ring_dx_in_range(unsigned int h,unsigned int i,unsigned int t)245 static inline bool aq_ring_dx_in_range(unsigned int h, unsigned int i,
246 unsigned int t)
247 {
248 return (h < t) ? ((h < i) && (i < t)) : ((h < i) || (i < t));
249 }
250
aq_ring_update_queue_state(struct aq_ring_s * ring)251 void aq_ring_update_queue_state(struct aq_ring_s *ring)
252 {
253 if (aq_ring_avail_dx(ring) <= AQ_CFG_SKB_FRAGS_MAX)
254 aq_ring_queue_stop(ring);
255 else if (aq_ring_avail_dx(ring) > AQ_CFG_RESTART_DESC_THRES)
256 aq_ring_queue_wake(ring);
257 }
258
aq_ring_queue_wake(struct aq_ring_s * ring)259 void aq_ring_queue_wake(struct aq_ring_s *ring)
260 {
261 struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);
262
263 if (__netif_subqueue_stopped(ndev,
264 AQ_NIC_RING2QMAP(ring->aq_nic,
265 ring->idx))) {
266 netif_wake_subqueue(ndev,
267 AQ_NIC_RING2QMAP(ring->aq_nic, ring->idx));
268 u64_stats_update_begin(&ring->stats.tx.syncp);
269 ring->stats.tx.queue_restarts++;
270 u64_stats_update_end(&ring->stats.tx.syncp);
271 }
272 }
273
aq_ring_queue_stop(struct aq_ring_s * ring)274 void aq_ring_queue_stop(struct aq_ring_s *ring)
275 {
276 struct net_device *ndev = aq_nic_get_ndev(ring->aq_nic);
277
278 if (!__netif_subqueue_stopped(ndev,
279 AQ_NIC_RING2QMAP(ring->aq_nic,
280 ring->idx)))
281 netif_stop_subqueue(ndev,
282 AQ_NIC_RING2QMAP(ring->aq_nic, ring->idx));
283 }
284
aq_ring_tx_clean(struct aq_ring_s * self)285 bool aq_ring_tx_clean(struct aq_ring_s *self)
286 {
287 struct device *dev = aq_nic_get_dev(self->aq_nic);
288 unsigned int budget;
289
290 for (budget = AQ_CFG_TX_CLEAN_BUDGET;
291 budget && self->sw_head != self->hw_head; budget--) {
292 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
293
294 if (likely(buff->is_mapped)) {
295 if (unlikely(buff->is_sop)) {
296 if (!buff->is_eop &&
297 buff->eop_index != 0xffffU &&
298 (!aq_ring_dx_in_range(self->sw_head,
299 buff->eop_index,
300 self->hw_head)))
301 break;
302
303 dma_unmap_single(dev, buff->pa, buff->len,
304 DMA_TO_DEVICE);
305 } else {
306 dma_unmap_page(dev, buff->pa, buff->len,
307 DMA_TO_DEVICE);
308 }
309 }
310
311 if (likely(!buff->is_eop))
312 goto out;
313
314 if (buff->skb) {
315 u64_stats_update_begin(&self->stats.tx.syncp);
316 ++self->stats.tx.packets;
317 self->stats.tx.bytes += buff->skb->len;
318 u64_stats_update_end(&self->stats.tx.syncp);
319 dev_kfree_skb_any(buff->skb);
320 } else if (buff->xdpf) {
321 u64_stats_update_begin(&self->stats.tx.syncp);
322 ++self->stats.tx.packets;
323 self->stats.tx.bytes += xdp_get_frame_len(buff->xdpf);
324 u64_stats_update_end(&self->stats.tx.syncp);
325 xdp_return_frame_rx_napi(buff->xdpf);
326 }
327
328 out:
329 buff->skb = NULL;
330 buff->xdpf = NULL;
331 buff->pa = 0U;
332 buff->eop_index = 0xffffU;
333 self->sw_head = aq_ring_next_dx(self, self->sw_head);
334 }
335
336 return !!budget;
337 }
338
aq_rx_checksum(struct aq_ring_s * self,struct aq_ring_buff_s * buff,struct sk_buff * skb)339 static void aq_rx_checksum(struct aq_ring_s *self,
340 struct aq_ring_buff_s *buff,
341 struct sk_buff *skb)
342 {
343 if (!(self->aq_nic->ndev->features & NETIF_F_RXCSUM))
344 return;
345
346 if (unlikely(buff->is_cso_err)) {
347 u64_stats_update_begin(&self->stats.rx.syncp);
348 ++self->stats.rx.errors;
349 u64_stats_update_end(&self->stats.rx.syncp);
350 skb->ip_summed = CHECKSUM_NONE;
351 return;
352 }
353 if (buff->is_ip_cso) {
354 __skb_incr_checksum_unnecessary(skb);
355 } else {
356 skb->ip_summed = CHECKSUM_NONE;
357 }
358
359 if (buff->is_udp_cso || buff->is_tcp_cso)
360 __skb_incr_checksum_unnecessary(skb);
361 }
362
aq_xdp_xmit(struct net_device * dev,int num_frames,struct xdp_frame ** frames,u32 flags)363 int aq_xdp_xmit(struct net_device *dev, int num_frames,
364 struct xdp_frame **frames, u32 flags)
365 {
366 struct aq_nic_s *aq_nic = netdev_priv(dev);
367 unsigned int vec, i, drop = 0;
368 int cpu = smp_processor_id();
369 struct aq_nic_cfg_s *aq_cfg;
370 struct aq_ring_s *ring;
371
372 aq_cfg = aq_nic_get_cfg(aq_nic);
373 vec = cpu % aq_cfg->vecs;
374 ring = aq_nic->aq_ring_tx[AQ_NIC_CFG_TCVEC2RING(aq_cfg, 0, vec)];
375
376 for (i = 0; i < num_frames; i++) {
377 struct xdp_frame *xdpf = frames[i];
378
379 if (aq_nic_xmit_xdpf(aq_nic, ring, xdpf) == NETDEV_TX_BUSY)
380 drop++;
381 }
382
383 return num_frames - drop;
384 }
385
aq_xdp_build_skb(struct xdp_buff * xdp,struct net_device * dev,struct aq_ring_buff_s * buff)386 static struct sk_buff *aq_xdp_build_skb(struct xdp_buff *xdp,
387 struct net_device *dev,
388 struct aq_ring_buff_s *buff)
389 {
390 struct xdp_frame *xdpf;
391 struct sk_buff *skb;
392
393 xdpf = xdp_convert_buff_to_frame(xdp);
394 if (unlikely(!xdpf))
395 return NULL;
396
397 skb = xdp_build_skb_from_frame(xdpf, dev);
398 if (!skb)
399 return NULL;
400
401 aq_get_rxpages_xdp(buff, xdp);
402 return skb;
403 }
404
aq_xdp_run_prog(struct aq_nic_s * aq_nic,struct xdp_buff * xdp,struct aq_ring_s * rx_ring,struct aq_ring_buff_s * buff)405 static struct sk_buff *aq_xdp_run_prog(struct aq_nic_s *aq_nic,
406 struct xdp_buff *xdp,
407 struct aq_ring_s *rx_ring,
408 struct aq_ring_buff_s *buff)
409 {
410 int result = NETDEV_TX_BUSY;
411 struct aq_ring_s *tx_ring;
412 struct xdp_frame *xdpf;
413 struct bpf_prog *prog;
414 u32 act = XDP_ABORTED;
415 struct sk_buff *skb;
416
417 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
418 ++rx_ring->stats.rx.packets;
419 rx_ring->stats.rx.bytes += xdp_get_buff_len(xdp);
420 u64_stats_update_end(&rx_ring->stats.rx.syncp);
421
422 prog = READ_ONCE(rx_ring->xdp_prog);
423 if (!prog)
424 return aq_xdp_build_skb(xdp, aq_nic->ndev, buff);
425
426 prefetchw(xdp->data_hard_start); /* xdp_frame write */
427
428 /* single buffer XDP program, but packet is multi buffer, aborted */
429 if (xdp_buff_has_frags(xdp) && !prog->aux->xdp_has_frags)
430 goto out_aborted;
431
432 act = bpf_prog_run_xdp(prog, xdp);
433 switch (act) {
434 case XDP_PASS:
435 skb = aq_xdp_build_skb(xdp, aq_nic->ndev, buff);
436 if (!skb)
437 goto out_aborted;
438 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
439 ++rx_ring->stats.rx.xdp_pass;
440 u64_stats_update_end(&rx_ring->stats.rx.syncp);
441 return skb;
442 case XDP_TX:
443 xdpf = xdp_convert_buff_to_frame(xdp);
444 if (unlikely(!xdpf))
445 goto out_aborted;
446 tx_ring = aq_nic->aq_ring_tx[rx_ring->idx];
447 result = aq_nic_xmit_xdpf(aq_nic, tx_ring, xdpf);
448 if (result == NETDEV_TX_BUSY)
449 goto out_aborted;
450 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
451 ++rx_ring->stats.rx.xdp_tx;
452 u64_stats_update_end(&rx_ring->stats.rx.syncp);
453 aq_get_rxpages_xdp(buff, xdp);
454 break;
455 case XDP_REDIRECT:
456 if (xdp_do_redirect(aq_nic->ndev, xdp, prog) < 0)
457 goto out_aborted;
458 xdp_do_flush();
459 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
460 ++rx_ring->stats.rx.xdp_redirect;
461 u64_stats_update_end(&rx_ring->stats.rx.syncp);
462 aq_get_rxpages_xdp(buff, xdp);
463 break;
464 default:
465 fallthrough;
466 case XDP_ABORTED:
467 out_aborted:
468 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
469 ++rx_ring->stats.rx.xdp_aborted;
470 u64_stats_update_end(&rx_ring->stats.rx.syncp);
471 trace_xdp_exception(aq_nic->ndev, prog, act);
472 bpf_warn_invalid_xdp_action(aq_nic->ndev, prog, act);
473 break;
474 case XDP_DROP:
475 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
476 ++rx_ring->stats.rx.xdp_drop;
477 u64_stats_update_end(&rx_ring->stats.rx.syncp);
478 break;
479 }
480
481 return ERR_PTR(-result);
482 }
483
aq_add_rx_fragment(struct device * dev,struct aq_ring_s * ring,struct aq_ring_buff_s * buff,struct xdp_buff * xdp)484 static bool aq_add_rx_fragment(struct device *dev,
485 struct aq_ring_s *ring,
486 struct aq_ring_buff_s *buff,
487 struct xdp_buff *xdp)
488 {
489 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
490 struct aq_ring_buff_s *buff_ = buff;
491
492 memset(sinfo, 0, sizeof(*sinfo));
493 do {
494 skb_frag_t *frag;
495
496 if (unlikely(sinfo->nr_frags >= MAX_SKB_FRAGS))
497 return true;
498
499 frag = &sinfo->frags[sinfo->nr_frags++];
500 buff_ = &ring->buff_ring[buff_->next];
501 dma_sync_single_range_for_cpu(dev,
502 buff_->rxdata.daddr,
503 buff_->rxdata.pg_off,
504 buff_->len,
505 DMA_FROM_DEVICE);
506 sinfo->xdp_frags_size += buff_->len;
507 skb_frag_fill_page_desc(frag, buff_->rxdata.page,
508 buff_->rxdata.pg_off,
509 buff_->len);
510
511 buff_->is_cleaned = 1;
512
513 buff->is_ip_cso &= buff_->is_ip_cso;
514 buff->is_udp_cso &= buff_->is_udp_cso;
515 buff->is_tcp_cso &= buff_->is_tcp_cso;
516 buff->is_cso_err |= buff_->is_cso_err;
517
518 if (page_is_pfmemalloc(buff_->rxdata.page))
519 xdp_buff_set_frag_pfmemalloc(xdp);
520
521 } while (!buff_->is_eop);
522
523 xdp_buff_set_frags_flag(xdp);
524
525 return false;
526 }
527
__aq_ring_rx_clean(struct aq_ring_s * self,struct napi_struct * napi,int * work_done,int budget)528 static int __aq_ring_rx_clean(struct aq_ring_s *self, struct napi_struct *napi,
529 int *work_done, int budget)
530 {
531 struct net_device *ndev = aq_nic_get_ndev(self->aq_nic);
532 int err = 0;
533
534 for (; (self->sw_head != self->hw_head) && budget;
535 self->sw_head = aq_ring_next_dx(self, self->sw_head),
536 --budget, ++(*work_done)) {
537 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
538 bool is_ptp_ring = aq_ptp_ring(self->aq_nic, self);
539 struct aq_ring_buff_s *buff_ = NULL;
540 struct sk_buff *skb = NULL;
541 unsigned int next_ = 0U;
542 unsigned int i = 0U;
543 u16 hdr_len;
544
545 if (buff->is_cleaned)
546 continue;
547
548 if (!buff->is_eop) {
549 unsigned int frag_cnt = 0U;
550
551 /* There will be an extra fragment */
552 if (buff->len > AQ_CFG_RX_HDR_SIZE)
553 frag_cnt++;
554
555 buff_ = buff;
556 do {
557 bool is_rsc_completed = true;
558
559 if (buff_->next >= self->size) {
560 err = -EIO;
561 goto err_exit;
562 }
563
564 frag_cnt++;
565 next_ = buff_->next;
566 buff_ = &self->buff_ring[next_];
567 is_rsc_completed =
568 aq_ring_dx_in_range(self->sw_head,
569 next_,
570 self->hw_head);
571
572 if (unlikely(!is_rsc_completed) ||
573 frag_cnt > MAX_SKB_FRAGS) {
574 err = 0;
575 goto err_exit;
576 }
577
578 buff->is_error |= buff_->is_error;
579 buff->is_cso_err |= buff_->is_cso_err;
580
581 } while (!buff_->is_eop);
582
583 if (buff->is_error ||
584 (buff->is_lro && buff->is_cso_err)) {
585 buff_ = buff;
586 do {
587 if (buff_->next >= self->size) {
588 err = -EIO;
589 goto err_exit;
590 }
591 next_ = buff_->next;
592 buff_ = &self->buff_ring[next_];
593
594 buff_->is_cleaned = true;
595 } while (!buff_->is_eop);
596
597 u64_stats_update_begin(&self->stats.rx.syncp);
598 ++self->stats.rx.errors;
599 u64_stats_update_end(&self->stats.rx.syncp);
600 continue;
601 }
602 }
603
604 if (buff->is_error) {
605 u64_stats_update_begin(&self->stats.rx.syncp);
606 ++self->stats.rx.errors;
607 u64_stats_update_end(&self->stats.rx.syncp);
608 continue;
609 }
610
611 dma_sync_single_range_for_cpu(aq_nic_get_dev(self->aq_nic),
612 buff->rxdata.daddr,
613 buff->rxdata.pg_off,
614 buff->len, DMA_FROM_DEVICE);
615
616 skb = napi_alloc_skb(napi, AQ_CFG_RX_HDR_SIZE);
617 if (unlikely(!skb)) {
618 u64_stats_update_begin(&self->stats.rx.syncp);
619 self->stats.rx.skb_alloc_fails++;
620 u64_stats_update_end(&self->stats.rx.syncp);
621 err = -ENOMEM;
622 goto err_exit;
623 }
624 if (is_ptp_ring)
625 buff->len -=
626 aq_ptp_extract_ts(self->aq_nic, skb_hwtstamps(skb),
627 aq_buf_vaddr(&buff->rxdata),
628 buff->len);
629
630 hdr_len = buff->len;
631 if (hdr_len > AQ_CFG_RX_HDR_SIZE)
632 hdr_len = eth_get_headlen(skb->dev,
633 aq_buf_vaddr(&buff->rxdata),
634 AQ_CFG_RX_HDR_SIZE);
635
636 memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
637 ALIGN(hdr_len, sizeof(long)));
638
639 if (buff->len - hdr_len > 0) {
640 skb_add_rx_frag(skb, i++, buff->rxdata.page,
641 buff->rxdata.pg_off + hdr_len,
642 buff->len - hdr_len,
643 self->frame_max);
644 page_ref_inc(buff->rxdata.page);
645 }
646
647 if (!buff->is_eop) {
648 buff_ = buff;
649 do {
650 next_ = buff_->next;
651 buff_ = &self->buff_ring[next_];
652
653 dma_sync_single_range_for_cpu(aq_nic_get_dev(self->aq_nic),
654 buff_->rxdata.daddr,
655 buff_->rxdata.pg_off,
656 buff_->len,
657 DMA_FROM_DEVICE);
658 skb_add_rx_frag(skb, i++,
659 buff_->rxdata.page,
660 buff_->rxdata.pg_off,
661 buff_->len,
662 self->frame_max);
663 page_ref_inc(buff_->rxdata.page);
664 buff_->is_cleaned = 1;
665
666 buff->is_ip_cso &= buff_->is_ip_cso;
667 buff->is_udp_cso &= buff_->is_udp_cso;
668 buff->is_tcp_cso &= buff_->is_tcp_cso;
669 buff->is_cso_err |= buff_->is_cso_err;
670
671 } while (!buff_->is_eop);
672 }
673
674 if (buff->is_vlan)
675 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
676 buff->vlan_rx_tag);
677
678 skb->protocol = eth_type_trans(skb, ndev);
679
680 aq_rx_checksum(self, buff, skb);
681
682 skb_set_hash(skb, buff->rss_hash,
683 buff->is_hash_l4 ? PKT_HASH_TYPE_L4 :
684 PKT_HASH_TYPE_NONE);
685 /* Send all PTP traffic to 0 queue */
686 skb_record_rx_queue(skb,
687 is_ptp_ring ? 0
688 : AQ_NIC_RING2QMAP(self->aq_nic,
689 self->idx));
690
691 u64_stats_update_begin(&self->stats.rx.syncp);
692 ++self->stats.rx.packets;
693 self->stats.rx.bytes += skb->len;
694 u64_stats_update_end(&self->stats.rx.syncp);
695
696 napi_gro_receive(napi, skb);
697 }
698
699 err_exit:
700 return err;
701 }
702
__aq_ring_xdp_clean(struct aq_ring_s * rx_ring,struct napi_struct * napi,int * work_done,int budget)703 static int __aq_ring_xdp_clean(struct aq_ring_s *rx_ring,
704 struct napi_struct *napi, int *work_done,
705 int budget)
706 {
707 int frame_sz = rx_ring->page_offset + rx_ring->frame_max +
708 rx_ring->tail_size;
709 struct aq_nic_s *aq_nic = rx_ring->aq_nic;
710 bool is_rsc_completed = true;
711 struct device *dev;
712 int err = 0;
713
714 dev = aq_nic_get_dev(aq_nic);
715 for (; (rx_ring->sw_head != rx_ring->hw_head) && budget;
716 rx_ring->sw_head = aq_ring_next_dx(rx_ring, rx_ring->sw_head),
717 --budget, ++(*work_done)) {
718 struct aq_ring_buff_s *buff = &rx_ring->buff_ring[rx_ring->sw_head];
719 bool is_ptp_ring = aq_ptp_ring(rx_ring->aq_nic, rx_ring);
720 struct aq_ring_buff_s *buff_ = NULL;
721 u16 ptp_hwtstamp_len = 0;
722 struct skb_shared_hwtstamps shhwtstamps;
723 struct sk_buff *skb = NULL;
724 unsigned int next_ = 0U;
725 struct xdp_buff xdp;
726 void *hard_start;
727
728 if (buff->is_cleaned)
729 continue;
730
731 if (!buff->is_eop) {
732 buff_ = buff;
733 do {
734 if (buff_->next >= rx_ring->size) {
735 err = -EIO;
736 goto err_exit;
737 }
738 next_ = buff_->next;
739 buff_ = &rx_ring->buff_ring[next_];
740 is_rsc_completed =
741 aq_ring_dx_in_range(rx_ring->sw_head,
742 next_,
743 rx_ring->hw_head);
744
745 if (unlikely(!is_rsc_completed))
746 break;
747
748 buff->is_error |= buff_->is_error;
749 buff->is_cso_err |= buff_->is_cso_err;
750 } while (!buff_->is_eop);
751
752 if (!is_rsc_completed) {
753 err = 0;
754 goto err_exit;
755 }
756 if (buff->is_error ||
757 (buff->is_lro && buff->is_cso_err)) {
758 buff_ = buff;
759 do {
760 if (buff_->next >= rx_ring->size) {
761 err = -EIO;
762 goto err_exit;
763 }
764 next_ = buff_->next;
765 buff_ = &rx_ring->buff_ring[next_];
766
767 buff_->is_cleaned = true;
768 } while (!buff_->is_eop);
769
770 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
771 ++rx_ring->stats.rx.errors;
772 u64_stats_update_end(&rx_ring->stats.rx.syncp);
773 continue;
774 }
775 }
776
777 if (buff->is_error) {
778 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
779 ++rx_ring->stats.rx.errors;
780 u64_stats_update_end(&rx_ring->stats.rx.syncp);
781 continue;
782 }
783
784 dma_sync_single_range_for_cpu(dev,
785 buff->rxdata.daddr,
786 buff->rxdata.pg_off,
787 buff->len, DMA_FROM_DEVICE);
788 hard_start = page_address(buff->rxdata.page) +
789 buff->rxdata.pg_off - rx_ring->page_offset;
790
791 if (is_ptp_ring) {
792 ptp_hwtstamp_len = aq_ptp_extract_ts(rx_ring->aq_nic, &shhwtstamps,
793 aq_buf_vaddr(&buff->rxdata),
794 buff->len);
795 buff->len -= ptp_hwtstamp_len;
796 }
797
798 xdp_init_buff(&xdp, frame_sz, &rx_ring->xdp_rxq);
799 xdp_prepare_buff(&xdp, hard_start, rx_ring->page_offset,
800 buff->len, false);
801 if (!buff->is_eop) {
802 if (aq_add_rx_fragment(dev, rx_ring, buff, &xdp)) {
803 u64_stats_update_begin(&rx_ring->stats.rx.syncp);
804 ++rx_ring->stats.rx.packets;
805 rx_ring->stats.rx.bytes += xdp_get_buff_len(&xdp);
806 ++rx_ring->stats.rx.xdp_aborted;
807 u64_stats_update_end(&rx_ring->stats.rx.syncp);
808 continue;
809 }
810 }
811
812 skb = aq_xdp_run_prog(aq_nic, &xdp, rx_ring, buff);
813 if (IS_ERR(skb) || !skb)
814 continue;
815
816 if (ptp_hwtstamp_len > 0)
817 *skb_hwtstamps(skb) = shhwtstamps;
818
819 if (buff->is_vlan)
820 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
821 buff->vlan_rx_tag);
822
823 aq_rx_checksum(rx_ring, buff, skb);
824
825 skb_set_hash(skb, buff->rss_hash,
826 buff->is_hash_l4 ? PKT_HASH_TYPE_L4 :
827 PKT_HASH_TYPE_NONE);
828 /* Send all PTP traffic to 0 queue */
829 skb_record_rx_queue(skb,
830 is_ptp_ring ? 0
831 : AQ_NIC_RING2QMAP(rx_ring->aq_nic,
832 rx_ring->idx));
833
834 napi_gro_receive(napi, skb);
835 }
836
837 err_exit:
838 return err;
839 }
840
aq_ring_rx_clean(struct aq_ring_s * self,struct napi_struct * napi,int * work_done,int budget)841 int aq_ring_rx_clean(struct aq_ring_s *self,
842 struct napi_struct *napi,
843 int *work_done,
844 int budget)
845 {
846 if (static_branch_unlikely(&aq_xdp_locking_key))
847 return __aq_ring_xdp_clean(self, napi, work_done, budget);
848 else
849 return __aq_ring_rx_clean(self, napi, work_done, budget);
850 }
851
aq_ring_hwts_rx_clean(struct aq_ring_s * self,struct aq_nic_s * aq_nic)852 void aq_ring_hwts_rx_clean(struct aq_ring_s *self, struct aq_nic_s *aq_nic)
853 {
854 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
855 while (self->sw_head != self->hw_head) {
856 u64 ns;
857
858 aq_nic->aq_hw_ops->extract_hwts(aq_nic->aq_hw,
859 self->dx_ring +
860 (self->sw_head * self->dx_size),
861 self->dx_size, &ns);
862 aq_ptp_tx_hwtstamp(aq_nic, ns);
863
864 self->sw_head = aq_ring_next_dx(self, self->sw_head);
865 }
866 #endif
867 }
868
aq_ring_rx_fill(struct aq_ring_s * self)869 int aq_ring_rx_fill(struct aq_ring_s *self)
870 {
871 struct aq_ring_buff_s *buff = NULL;
872 int err = 0;
873 int i = 0;
874
875 if (aq_ring_avail_dx(self) < min_t(unsigned int, AQ_CFG_RX_REFILL_THRES,
876 self->size / 2))
877 return err;
878
879 for (i = aq_ring_avail_dx(self); i--;
880 self->sw_tail = aq_ring_next_dx(self, self->sw_tail)) {
881 buff = &self->buff_ring[self->sw_tail];
882
883 buff->flags = 0U;
884 buff->len = self->frame_max;
885
886 err = aq_get_rxpages(self, buff);
887 if (err)
888 goto err_exit;
889
890 buff->pa = aq_buf_daddr(&buff->rxdata);
891 buff = NULL;
892 }
893
894 err_exit:
895 return err;
896 }
897
aq_ring_rx_deinit(struct aq_ring_s * self)898 void aq_ring_rx_deinit(struct aq_ring_s *self)
899 {
900 if (!self)
901 return;
902
903 for (; self->sw_head != self->sw_tail;
904 self->sw_head = aq_ring_next_dx(self, self->sw_head)) {
905 struct aq_ring_buff_s *buff = &self->buff_ring[self->sw_head];
906
907 aq_free_rxpage(&buff->rxdata, aq_nic_get_dev(self->aq_nic));
908 }
909 }
910
aq_ring_free(struct aq_ring_s * self)911 void aq_ring_free(struct aq_ring_s *self)
912 {
913 if (!self)
914 return;
915
916 kfree(self->buff_ring);
917 self->buff_ring = NULL;
918
919 if (self->dx_ring) {
920 dma_free_coherent(aq_nic_get_dev(self->aq_nic),
921 self->size * self->dx_size, self->dx_ring,
922 self->dx_ring_pa);
923 self->dx_ring = NULL;
924 }
925 }
926
aq_ring_hwts_rx_free(struct aq_ring_s * self)927 void aq_ring_hwts_rx_free(struct aq_ring_s *self)
928 {
929 if (!self)
930 return;
931
932 if (self->dx_ring) {
933 dma_free_coherent(aq_nic_get_dev(self->aq_nic),
934 self->size * self->dx_size + AQ_CFG_RXDS_DEF,
935 self->dx_ring, self->dx_ring_pa);
936 self->dx_ring = NULL;
937 }
938 }
939
aq_ring_fill_stats_data(struct aq_ring_s * self,u64 * data)940 unsigned int aq_ring_fill_stats_data(struct aq_ring_s *self, u64 *data)
941 {
942 unsigned int count;
943 unsigned int start;
944
945 if (self->ring_type == ATL_RING_RX) {
946 /* This data should mimic aq_ethtool_queue_rx_stat_names structure */
947 do {
948 count = 0;
949 start = u64_stats_fetch_begin(&self->stats.rx.syncp);
950 data[count] = self->stats.rx.packets;
951 data[++count] = self->stats.rx.jumbo_packets;
952 data[++count] = self->stats.rx.lro_packets;
953 data[++count] = self->stats.rx.errors;
954 data[++count] = self->stats.rx.alloc_fails;
955 data[++count] = self->stats.rx.skb_alloc_fails;
956 data[++count] = self->stats.rx.polls;
957 data[++count] = self->stats.rx.pg_flips;
958 data[++count] = self->stats.rx.pg_reuses;
959 data[++count] = self->stats.rx.pg_losts;
960 data[++count] = self->stats.rx.xdp_aborted;
961 data[++count] = self->stats.rx.xdp_drop;
962 data[++count] = self->stats.rx.xdp_pass;
963 data[++count] = self->stats.rx.xdp_tx;
964 data[++count] = self->stats.rx.xdp_invalid;
965 data[++count] = self->stats.rx.xdp_redirect;
966 } while (u64_stats_fetch_retry(&self->stats.rx.syncp, start));
967 } else {
968 /* This data should mimic aq_ethtool_queue_tx_stat_names structure */
969 do {
970 count = 0;
971 start = u64_stats_fetch_begin(&self->stats.tx.syncp);
972 data[count] = self->stats.tx.packets;
973 data[++count] = self->stats.tx.queue_restarts;
974 } while (u64_stats_fetch_retry(&self->stats.tx.syncp, start));
975 }
976
977 return ++count;
978 }
979