1
2
3
4
5
6#include <linux/prefetch.h>
7#include <linux/mm.h>
8#include <linux/bpf_trace.h>
9#include <net/xdp.h>
10#include "ice_txrx_lib.h"
11#include "ice_lib.h"
12#include "ice.h"
13#include "ice_dcb_lib.h"
14#include "ice_xsk.h"
15
16#define ICE_RX_HDR_SIZE 256
17
18
19
20
21
22
23static void
24ice_unmap_and_free_tx_buf(struct ice_ring *ring, struct ice_tx_buf *tx_buf)
25{
26 if (tx_buf->skb) {
27 if (ice_ring_is_xdp(ring))
28 page_frag_free(tx_buf->raw_buf);
29 else
30 dev_kfree_skb_any(tx_buf->skb);
31 if (dma_unmap_len(tx_buf, len))
32 dma_unmap_single(ring->dev,
33 dma_unmap_addr(tx_buf, dma),
34 dma_unmap_len(tx_buf, len),
35 DMA_TO_DEVICE);
36 } else if (dma_unmap_len(tx_buf, len)) {
37 dma_unmap_page(ring->dev,
38 dma_unmap_addr(tx_buf, dma),
39 dma_unmap_len(tx_buf, len),
40 DMA_TO_DEVICE);
41 }
42
43 tx_buf->next_to_watch = NULL;
44 tx_buf->skb = NULL;
45 dma_unmap_len_set(tx_buf, len, 0);
46
47}
48
49static struct netdev_queue *txring_txq(const struct ice_ring *ring)
50{
51 return netdev_get_tx_queue(ring->netdev, ring->q_index);
52}
53
54
55
56
57
58void ice_clean_tx_ring(struct ice_ring *tx_ring)
59{
60 u16 i;
61
62 if (ice_ring_is_xdp(tx_ring) && tx_ring->xsk_umem) {
63 ice_xsk_clean_xdp_ring(tx_ring);
64 goto tx_skip_free;
65 }
66
67
68 if (!tx_ring->tx_buf)
69 return;
70
71
72 for (i = 0; i < tx_ring->count; i++)
73 ice_unmap_and_free_tx_buf(tx_ring, &tx_ring->tx_buf[i]);
74
75tx_skip_free:
76 memset(tx_ring->tx_buf, 0, sizeof(*tx_ring->tx_buf) * tx_ring->count);
77
78
79 memset(tx_ring->desc, 0, tx_ring->size);
80
81 tx_ring->next_to_use = 0;
82 tx_ring->next_to_clean = 0;
83
84 if (!tx_ring->netdev)
85 return;
86
87
88 netdev_tx_reset_queue(txring_txq(tx_ring));
89}
90
91
92
93
94
95
96
97void ice_free_tx_ring(struct ice_ring *tx_ring)
98{
99 ice_clean_tx_ring(tx_ring);
100 devm_kfree(tx_ring->dev, tx_ring->tx_buf);
101 tx_ring->tx_buf = NULL;
102
103 if (tx_ring->desc) {
104 dmam_free_coherent(tx_ring->dev, tx_ring->size,
105 tx_ring->desc, tx_ring->dma);
106 tx_ring->desc = NULL;
107 }
108}
109
110
111
112
113
114
115
116
117static bool ice_clean_tx_irq(struct ice_ring *tx_ring, int napi_budget)
118{
119 unsigned int total_bytes = 0, total_pkts = 0;
120 unsigned int budget = ICE_DFLT_IRQ_WORK;
121 struct ice_vsi *vsi = tx_ring->vsi;
122 s16 i = tx_ring->next_to_clean;
123 struct ice_tx_desc *tx_desc;
124 struct ice_tx_buf *tx_buf;
125
126 tx_buf = &tx_ring->tx_buf[i];
127 tx_desc = ICE_TX_DESC(tx_ring, i);
128 i -= tx_ring->count;
129
130 prefetch(&vsi->state);
131
132 do {
133 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
134
135
136 if (!eop_desc)
137 break;
138
139 smp_rmb();
140
141
142 if (!(eop_desc->cmd_type_offset_bsz &
143 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
144 break;
145
146
147 tx_buf->next_to_watch = NULL;
148
149
150 total_bytes += tx_buf->bytecount;
151 total_pkts += tx_buf->gso_segs;
152
153 if (ice_ring_is_xdp(tx_ring))
154 page_frag_free(tx_buf->raw_buf);
155 else
156
157 napi_consume_skb(tx_buf->skb, napi_budget);
158
159
160 dma_unmap_single(tx_ring->dev,
161 dma_unmap_addr(tx_buf, dma),
162 dma_unmap_len(tx_buf, len),
163 DMA_TO_DEVICE);
164
165
166 tx_buf->skb = NULL;
167 dma_unmap_len_set(tx_buf, len, 0);
168
169
170 while (tx_desc != eop_desc) {
171 tx_buf++;
172 tx_desc++;
173 i++;
174 if (unlikely(!i)) {
175 i -= tx_ring->count;
176 tx_buf = tx_ring->tx_buf;
177 tx_desc = ICE_TX_DESC(tx_ring, 0);
178 }
179
180
181 if (dma_unmap_len(tx_buf, len)) {
182 dma_unmap_page(tx_ring->dev,
183 dma_unmap_addr(tx_buf, dma),
184 dma_unmap_len(tx_buf, len),
185 DMA_TO_DEVICE);
186 dma_unmap_len_set(tx_buf, len, 0);
187 }
188 }
189
190
191 tx_buf++;
192 tx_desc++;
193 i++;
194 if (unlikely(!i)) {
195 i -= tx_ring->count;
196 tx_buf = tx_ring->tx_buf;
197 tx_desc = ICE_TX_DESC(tx_ring, 0);
198 }
199
200 prefetch(tx_desc);
201
202
203 budget--;
204 } while (likely(budget));
205
206 i += tx_ring->count;
207 tx_ring->next_to_clean = i;
208
209 ice_update_tx_ring_stats(tx_ring, total_pkts, total_bytes);
210
211 if (ice_ring_is_xdp(tx_ring))
212 return !!budget;
213
214 netdev_tx_completed_queue(txring_txq(tx_ring), total_pkts,
215 total_bytes);
216
217#define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
218 if (unlikely(total_pkts && netif_carrier_ok(tx_ring->netdev) &&
219 (ICE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
220
221
222
223 smp_mb();
224 if (__netif_subqueue_stopped(tx_ring->netdev,
225 tx_ring->q_index) &&
226 !test_bit(__ICE_DOWN, vsi->state)) {
227 netif_wake_subqueue(tx_ring->netdev,
228 tx_ring->q_index);
229 ++tx_ring->tx_stats.restart_q;
230 }
231 }
232
233 return !!budget;
234}
235
236
237
238
239
240
241
242int ice_setup_tx_ring(struct ice_ring *tx_ring)
243{
244 struct device *dev = tx_ring->dev;
245
246 if (!dev)
247 return -ENOMEM;
248
249
250 WARN_ON(tx_ring->tx_buf);
251 tx_ring->tx_buf =
252 devm_kzalloc(dev, sizeof(*tx_ring->tx_buf) * tx_ring->count,
253 GFP_KERNEL);
254 if (!tx_ring->tx_buf)
255 return -ENOMEM;
256
257
258 tx_ring->size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
259 PAGE_SIZE);
260 tx_ring->desc = dmam_alloc_coherent(dev, tx_ring->size, &tx_ring->dma,
261 GFP_KERNEL);
262 if (!tx_ring->desc) {
263 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
264 tx_ring->size);
265 goto err;
266 }
267
268 tx_ring->next_to_use = 0;
269 tx_ring->next_to_clean = 0;
270 tx_ring->tx_stats.prev_pkt = -1;
271 return 0;
272
273err:
274 devm_kfree(dev, tx_ring->tx_buf);
275 tx_ring->tx_buf = NULL;
276 return -ENOMEM;
277}
278
279
280
281
282
283void ice_clean_rx_ring(struct ice_ring *rx_ring)
284{
285 struct device *dev = rx_ring->dev;
286 u16 i;
287
288
289 if (!rx_ring->rx_buf)
290 return;
291
292 if (rx_ring->xsk_umem) {
293 ice_xsk_clean_rx_ring(rx_ring);
294 goto rx_skip_free;
295 }
296
297
298 for (i = 0; i < rx_ring->count; i++) {
299 struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
300
301 if (rx_buf->skb) {
302 dev_kfree_skb(rx_buf->skb);
303 rx_buf->skb = NULL;
304 }
305 if (!rx_buf->page)
306 continue;
307
308
309
310
311 dma_sync_single_range_for_cpu(dev, rx_buf->dma,
312 rx_buf->page_offset,
313 rx_ring->rx_buf_len,
314 DMA_FROM_DEVICE);
315
316
317 dma_unmap_page_attrs(dev, rx_buf->dma, ice_rx_pg_size(rx_ring),
318 DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
319 __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
320
321 rx_buf->page = NULL;
322 rx_buf->page_offset = 0;
323 }
324
325rx_skip_free:
326 memset(rx_ring->rx_buf, 0, sizeof(*rx_ring->rx_buf) * rx_ring->count);
327
328
329 memset(rx_ring->desc, 0, rx_ring->size);
330
331 rx_ring->next_to_alloc = 0;
332 rx_ring->next_to_clean = 0;
333 rx_ring->next_to_use = 0;
334}
335
336
337
338
339
340
341
342void ice_free_rx_ring(struct ice_ring *rx_ring)
343{
344 ice_clean_rx_ring(rx_ring);
345 if (rx_ring->vsi->type == ICE_VSI_PF)
346 if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
347 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
348 rx_ring->xdp_prog = NULL;
349 devm_kfree(rx_ring->dev, rx_ring->rx_buf);
350 rx_ring->rx_buf = NULL;
351
352 if (rx_ring->desc) {
353 dmam_free_coherent(rx_ring->dev, rx_ring->size,
354 rx_ring->desc, rx_ring->dma);
355 rx_ring->desc = NULL;
356 }
357}
358
359
360
361
362
363
364
365int ice_setup_rx_ring(struct ice_ring *rx_ring)
366{
367 struct device *dev = rx_ring->dev;
368
369 if (!dev)
370 return -ENOMEM;
371
372
373 WARN_ON(rx_ring->rx_buf);
374 rx_ring->rx_buf =
375 devm_kzalloc(dev, sizeof(*rx_ring->rx_buf) * rx_ring->count,
376 GFP_KERNEL);
377 if (!rx_ring->rx_buf)
378 return -ENOMEM;
379
380
381 rx_ring->size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
382 PAGE_SIZE);
383 rx_ring->desc = dmam_alloc_coherent(dev, rx_ring->size, &rx_ring->dma,
384 GFP_KERNEL);
385 if (!rx_ring->desc) {
386 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
387 rx_ring->size);
388 goto err;
389 }
390
391 rx_ring->next_to_use = 0;
392 rx_ring->next_to_clean = 0;
393
394 if (ice_is_xdp_ena_vsi(rx_ring->vsi))
395 WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog);
396
397 if (rx_ring->vsi->type == ICE_VSI_PF &&
398 !xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
399 if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
400 rx_ring->q_index))
401 goto err;
402 return 0;
403
404err:
405 devm_kfree(dev, rx_ring->rx_buf);
406 rx_ring->rx_buf = NULL;
407 return -ENOMEM;
408}
409
410
411
412
413
414
415
416static unsigned int ice_rx_offset(struct ice_ring *rx_ring)
417{
418 if (ice_ring_uses_build_skb(rx_ring))
419 return ICE_SKB_PAD;
420 else if (ice_is_xdp_ena_vsi(rx_ring->vsi))
421 return XDP_PACKET_HEADROOM;
422
423 return 0;
424}
425
426
427
428
429
430
431
432
433
434static int
435ice_run_xdp(struct ice_ring *rx_ring, struct xdp_buff *xdp,
436 struct bpf_prog *xdp_prog)
437{
438 int err, result = ICE_XDP_PASS;
439 struct ice_ring *xdp_ring;
440 u32 act;
441
442 act = bpf_prog_run_xdp(xdp_prog, xdp);
443 switch (act) {
444 case XDP_PASS:
445 break;
446 case XDP_TX:
447 xdp_ring = rx_ring->vsi->xdp_rings[smp_processor_id()];
448 result = ice_xmit_xdp_buff(xdp, xdp_ring);
449 break;
450 case XDP_REDIRECT:
451 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
452 result = !err ? ICE_XDP_REDIR : ICE_XDP_CONSUMED;
453 break;
454 default:
455 bpf_warn_invalid_xdp_action(act);
456
457 case XDP_ABORTED:
458 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
459
460 case XDP_DROP:
461 result = ICE_XDP_CONSUMED;
462 break;
463 }
464
465 return result;
466}
467
468
469
470
471
472
473
474
475
476
477
478
479
480int
481ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
482 u32 flags)
483{
484 struct ice_netdev_priv *np = netdev_priv(dev);
485 unsigned int queue_index = smp_processor_id();
486 struct ice_vsi *vsi = np->vsi;
487 struct ice_ring *xdp_ring;
488 int drops = 0, i;
489
490 if (test_bit(__ICE_DOWN, vsi->state))
491 return -ENETDOWN;
492
493 if (!ice_is_xdp_ena_vsi(vsi) || queue_index >= vsi->num_xdp_txq)
494 return -ENXIO;
495
496 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
497 return -EINVAL;
498
499 xdp_ring = vsi->xdp_rings[queue_index];
500 for (i = 0; i < n; i++) {
501 struct xdp_frame *xdpf = frames[i];
502 int err;
503
504 err = ice_xmit_xdp_ring(xdpf->data, xdpf->len, xdp_ring);
505 if (err != ICE_XDP_TX) {
506 xdp_return_frame_rx_napi(xdpf);
507 drops++;
508 }
509 }
510
511 if (unlikely(flags & XDP_XMIT_FLUSH))
512 ice_xdp_ring_update_tail(xdp_ring);
513
514 return n - drops;
515}
516
517
518
519
520
521
522
523
524
525static bool
526ice_alloc_mapped_page(struct ice_ring *rx_ring, struct ice_rx_buf *bi)
527{
528 struct page *page = bi->page;
529 dma_addr_t dma;
530
531
532 if (likely(page)) {
533 rx_ring->rx_stats.page_reuse_count++;
534 return true;
535 }
536
537
538 page = dev_alloc_pages(ice_rx_pg_order(rx_ring));
539 if (unlikely(!page)) {
540 rx_ring->rx_stats.alloc_page_failed++;
541 return false;
542 }
543
544
545 dma = dma_map_page_attrs(rx_ring->dev, page, 0, ice_rx_pg_size(rx_ring),
546 DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
547
548
549
550
551 if (dma_mapping_error(rx_ring->dev, dma)) {
552 __free_pages(page, ice_rx_pg_order(rx_ring));
553 rx_ring->rx_stats.alloc_page_failed++;
554 return false;
555 }
556
557 bi->dma = dma;
558 bi->page = page;
559 bi->page_offset = ice_rx_offset(rx_ring);
560 page_ref_add(page, USHRT_MAX - 1);
561 bi->pagecnt_bias = USHRT_MAX;
562
563 return true;
564}
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579bool ice_alloc_rx_bufs(struct ice_ring *rx_ring, u16 cleaned_count)
580{
581 union ice_32b_rx_flex_desc *rx_desc;
582 u16 ntu = rx_ring->next_to_use;
583 struct ice_rx_buf *bi;
584
585
586 if (!rx_ring->netdev || !cleaned_count)
587 return false;
588
589
590 rx_desc = ICE_RX_DESC(rx_ring, ntu);
591 bi = &rx_ring->rx_buf[ntu];
592
593 do {
594
595 if (!ice_alloc_mapped_page(rx_ring, bi))
596 break;
597
598
599 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
600 bi->page_offset,
601 rx_ring->rx_buf_len,
602 DMA_FROM_DEVICE);
603
604
605
606
607 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
608
609 rx_desc++;
610 bi++;
611 ntu++;
612 if (unlikely(ntu == rx_ring->count)) {
613 rx_desc = ICE_RX_DESC(rx_ring, 0);
614 bi = rx_ring->rx_buf;
615 ntu = 0;
616 }
617
618
619 rx_desc->wb.status_error0 = 0;
620
621 cleaned_count--;
622 } while (cleaned_count);
623
624 if (rx_ring->next_to_use != ntu)
625 ice_release_rx_desc(rx_ring, ntu);
626
627 return !!cleaned_count;
628}
629
630
631
632
633
634static bool ice_page_is_reserved(struct page *page)
635{
636 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
637}
638
639
640
641
642
643
644
645
646
647
648
649static void
650ice_rx_buf_adjust_pg_offset(struct ice_rx_buf *rx_buf, unsigned int size)
651{
652#if (PAGE_SIZE < 8192)
653
654 rx_buf->page_offset ^= size;
655#else
656
657 rx_buf->page_offset += size;
658#endif
659}
660
661
662
663
664
665
666
667
668
669
670static bool ice_can_reuse_rx_page(struct ice_rx_buf *rx_buf)
671{
672 unsigned int pagecnt_bias = rx_buf->pagecnt_bias;
673 struct page *page = rx_buf->page;
674
675
676 if (unlikely(ice_page_is_reserved(page)))
677 return false;
678
679#if (PAGE_SIZE < 8192)
680
681 if (unlikely((page_count(page) - pagecnt_bias) > 1))
682 return false;
683#else
684#define ICE_LAST_OFFSET \
685 (SKB_WITH_OVERHEAD(PAGE_SIZE) - ICE_RXBUF_2048)
686 if (rx_buf->page_offset > ICE_LAST_OFFSET)
687 return false;
688#endif
689
690
691
692
693
694 if (unlikely(pagecnt_bias == 1)) {
695 page_ref_add(page, USHRT_MAX - 1);
696 rx_buf->pagecnt_bias = USHRT_MAX;
697 }
698
699 return true;
700}
701
702
703
704
705
706
707
708
709
710
711
712
713static void
714ice_add_rx_frag(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
715 struct sk_buff *skb, unsigned int size)
716{
717#if (PAGE_SIZE >= 8192)
718 unsigned int truesize = SKB_DATA_ALIGN(size + ice_rx_offset(rx_ring));
719#else
720 unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
721#endif
722
723 if (!size)
724 return;
725 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buf->page,
726 rx_buf->page_offset, size, truesize);
727
728
729 ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
730}
731
732
733
734
735
736
737
738
739static void
740ice_reuse_rx_page(struct ice_ring *rx_ring, struct ice_rx_buf *old_buf)
741{
742 u16 nta = rx_ring->next_to_alloc;
743 struct ice_rx_buf *new_buf;
744
745 new_buf = &rx_ring->rx_buf[nta];
746
747
748 nta++;
749 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
750
751
752
753
754
755 new_buf->dma = old_buf->dma;
756 new_buf->page = old_buf->page;
757 new_buf->page_offset = old_buf->page_offset;
758 new_buf->pagecnt_bias = old_buf->pagecnt_bias;
759}
760
761
762
763
764
765
766
767
768
769
770static struct ice_rx_buf *
771ice_get_rx_buf(struct ice_ring *rx_ring, struct sk_buff **skb,
772 const unsigned int size)
773{
774 struct ice_rx_buf *rx_buf;
775
776 rx_buf = &rx_ring->rx_buf[rx_ring->next_to_clean];
777 prefetchw(rx_buf->page);
778 *skb = rx_buf->skb;
779
780 if (!size)
781 return rx_buf;
782
783 dma_sync_single_range_for_cpu(rx_ring->dev, rx_buf->dma,
784 rx_buf->page_offset, size,
785 DMA_FROM_DEVICE);
786
787
788 rx_buf->pagecnt_bias--;
789
790 return rx_buf;
791}
792
793
794
795
796
797
798
799
800
801
802static struct sk_buff *
803ice_build_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
804 struct xdp_buff *xdp)
805{
806 unsigned int metasize = xdp->data - xdp->data_meta;
807#if (PAGE_SIZE < 8192)
808 unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
809#else
810 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
811 SKB_DATA_ALIGN(xdp->data_end -
812 xdp->data_hard_start);
813#endif
814 struct sk_buff *skb;
815
816
817
818
819
820
821 prefetch(xdp->data_meta);
822#if L1_CACHE_BYTES < 128
823 prefetch((void *)(xdp->data + L1_CACHE_BYTES));
824#endif
825
826 skb = build_skb(xdp->data_hard_start, truesize);
827 if (unlikely(!skb))
828 return NULL;
829
830
831
832
833 skb_record_rx_queue(skb, rx_ring->q_index);
834
835
836 skb_reserve(skb, xdp->data - xdp->data_hard_start);
837 __skb_put(skb, xdp->data_end - xdp->data);
838 if (metasize)
839 skb_metadata_set(skb, metasize);
840
841
842 ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
843
844 return skb;
845}
846
847
848
849
850
851
852
853
854
855
856
857static struct sk_buff *
858ice_construct_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
859 struct xdp_buff *xdp)
860{
861 unsigned int size = xdp->data_end - xdp->data;
862 unsigned int headlen;
863 struct sk_buff *skb;
864
865
866 prefetch(xdp->data);
867#if L1_CACHE_BYTES < 128
868 prefetch((void *)(xdp->data + L1_CACHE_BYTES));
869#endif
870
871
872 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, ICE_RX_HDR_SIZE,
873 GFP_ATOMIC | __GFP_NOWARN);
874 if (unlikely(!skb))
875 return NULL;
876
877 skb_record_rx_queue(skb, rx_ring->q_index);
878
879 headlen = size;
880 if (headlen > ICE_RX_HDR_SIZE)
881 headlen = eth_get_headlen(skb->dev, xdp->data, ICE_RX_HDR_SIZE);
882
883
884 memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen,
885 sizeof(long)));
886
887
888 size -= headlen;
889 if (size) {
890#if (PAGE_SIZE >= 8192)
891 unsigned int truesize = SKB_DATA_ALIGN(size);
892#else
893 unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
894#endif
895 skb_add_rx_frag(skb, 0, rx_buf->page,
896 rx_buf->page_offset + headlen, size, truesize);
897
898 ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
899 } else {
900
901
902
903
904 rx_buf->pagecnt_bias++;
905 }
906
907 return skb;
908}
909
910
911
912
913
914
915
916
917
918
919static void ice_put_rx_buf(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf)
920{
921 u32 ntc = rx_ring->next_to_clean + 1;
922
923
924 ntc = (ntc < rx_ring->count) ? ntc : 0;
925 rx_ring->next_to_clean = ntc;
926
927 if (!rx_buf)
928 return;
929
930 if (ice_can_reuse_rx_page(rx_buf)) {
931
932 ice_reuse_rx_page(rx_ring, rx_buf);
933 rx_ring->rx_stats.page_reuse_count++;
934 } else {
935
936 dma_unmap_page_attrs(rx_ring->dev, rx_buf->dma,
937 ice_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
938 ICE_RX_DMA_ATTR);
939 __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
940 }
941
942
943 rx_buf->page = NULL;
944 rx_buf->skb = NULL;
945}
946
947
948
949
950
951
952
953
954
955
956static bool
957ice_is_non_eop(struct ice_ring *rx_ring, union ice_32b_rx_flex_desc *rx_desc,
958 struct sk_buff *skb)
959{
960
961#define ICE_RXD_EOF BIT(ICE_RX_FLEX_DESC_STATUS0_EOF_S)
962 if (likely(ice_test_staterr(rx_desc, ICE_RXD_EOF)))
963 return false;
964
965
966 rx_ring->rx_buf[rx_ring->next_to_clean].skb = skb;
967 rx_ring->rx_stats.non_eop_descs++;
968
969 return true;
970}
971
972
973
974
975
976
977
978
979
980
981
982
983
984static int ice_clean_rx_irq(struct ice_ring *rx_ring, int budget)
985{
986 unsigned int total_rx_bytes = 0, total_rx_pkts = 0;
987 u16 cleaned_count = ICE_DESC_UNUSED(rx_ring);
988 unsigned int xdp_res, xdp_xmit = 0;
989 struct bpf_prog *xdp_prog = NULL;
990 struct xdp_buff xdp;
991 bool failure;
992
993 xdp.rxq = &rx_ring->xdp_rxq;
994
995
996 while (likely(total_rx_pkts < (unsigned int)budget)) {
997 union ice_32b_rx_flex_desc *rx_desc;
998 struct ice_rx_buf *rx_buf;
999 struct sk_buff *skb;
1000 unsigned int size;
1001 u16 stat_err_bits;
1002 u16 vlan_tag = 0;
1003 u8 rx_ptype;
1004
1005
1006 rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
1007
1008
1009
1010
1011
1012
1013 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
1014 if (!ice_test_staterr(rx_desc, stat_err_bits))
1015 break;
1016
1017
1018
1019
1020
1021 dma_rmb();
1022
1023 size = le16_to_cpu(rx_desc->wb.pkt_len) &
1024 ICE_RX_FLX_DESC_PKT_LEN_M;
1025
1026
1027 rx_buf = ice_get_rx_buf(rx_ring, &skb, size);
1028
1029 if (!size) {
1030 xdp.data = NULL;
1031 xdp.data_end = NULL;
1032 xdp.data_hard_start = NULL;
1033 xdp.data_meta = NULL;
1034 goto construct_skb;
1035 }
1036
1037 xdp.data = page_address(rx_buf->page) + rx_buf->page_offset;
1038 xdp.data_hard_start = xdp.data - ice_rx_offset(rx_ring);
1039 xdp.data_meta = xdp.data;
1040 xdp.data_end = xdp.data + size;
1041
1042 rcu_read_lock();
1043 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1044 if (!xdp_prog) {
1045 rcu_read_unlock();
1046 goto construct_skb;
1047 }
1048
1049 xdp_res = ice_run_xdp(rx_ring, &xdp, xdp_prog);
1050 rcu_read_unlock();
1051 if (!xdp_res)
1052 goto construct_skb;
1053 if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR)) {
1054 unsigned int truesize;
1055
1056#if (PAGE_SIZE < 8192)
1057 truesize = ice_rx_pg_size(rx_ring) / 2;
1058#else
1059 truesize = SKB_DATA_ALIGN(ice_rx_offset(rx_ring) +
1060 size);
1061#endif
1062 xdp_xmit |= xdp_res;
1063 ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
1064 } else {
1065 rx_buf->pagecnt_bias++;
1066 }
1067 total_rx_bytes += size;
1068 total_rx_pkts++;
1069
1070 cleaned_count++;
1071 ice_put_rx_buf(rx_ring, rx_buf);
1072 continue;
1073construct_skb:
1074 if (skb) {
1075 ice_add_rx_frag(rx_ring, rx_buf, skb, size);
1076 } else if (likely(xdp.data)) {
1077 if (ice_ring_uses_build_skb(rx_ring))
1078 skb = ice_build_skb(rx_ring, rx_buf, &xdp);
1079 else
1080 skb = ice_construct_skb(rx_ring, rx_buf, &xdp);
1081 }
1082
1083 if (!skb) {
1084 rx_ring->rx_stats.alloc_buf_failed++;
1085 if (rx_buf)
1086 rx_buf->pagecnt_bias++;
1087 break;
1088 }
1089
1090 ice_put_rx_buf(rx_ring, rx_buf);
1091 cleaned_count++;
1092
1093
1094 if (ice_is_non_eop(rx_ring, rx_desc, skb))
1095 continue;
1096
1097 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S);
1098 if (unlikely(ice_test_staterr(rx_desc, stat_err_bits))) {
1099 dev_kfree_skb_any(skb);
1100 continue;
1101 }
1102
1103 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S);
1104 if (ice_test_staterr(rx_desc, stat_err_bits))
1105 vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1);
1106
1107
1108 if (eth_skb_pad(skb)) {
1109 skb = NULL;
1110 continue;
1111 }
1112
1113
1114 total_rx_bytes += skb->len;
1115
1116
1117 rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
1118 ICE_RX_FLEX_DESC_PTYPE_M;
1119
1120 ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
1121
1122
1123 ice_receive_skb(rx_ring, skb, vlan_tag);
1124
1125
1126 total_rx_pkts++;
1127 }
1128
1129
1130 failure = ice_alloc_rx_bufs(rx_ring, cleaned_count);
1131
1132 if (xdp_prog)
1133 ice_finalize_xdp_rx(rx_ring, xdp_xmit);
1134
1135 ice_update_rx_ring_stats(rx_ring, total_rx_pkts, total_rx_bytes);
1136
1137
1138 return failure ? budget : (int)total_rx_pkts;
1139}
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164static unsigned int
1165ice_adjust_itr_by_size_and_speed(struct ice_port_info *port_info,
1166 unsigned int avg_pkt_size,
1167 unsigned int itr)
1168{
1169 switch (port_info->phy.link_info.link_speed) {
1170 case ICE_AQ_LINK_SPEED_100GB:
1171 itr += DIV_ROUND_UP(17 * (avg_pkt_size + 24),
1172 avg_pkt_size + 640);
1173 break;
1174 case ICE_AQ_LINK_SPEED_50GB:
1175 itr += DIV_ROUND_UP(34 * (avg_pkt_size + 24),
1176 avg_pkt_size + 640);
1177 break;
1178 case ICE_AQ_LINK_SPEED_40GB:
1179 itr += DIV_ROUND_UP(43 * (avg_pkt_size + 24),
1180 avg_pkt_size + 640);
1181 break;
1182 case ICE_AQ_LINK_SPEED_25GB:
1183 itr += DIV_ROUND_UP(68 * (avg_pkt_size + 24),
1184 avg_pkt_size + 640);
1185 break;
1186 case ICE_AQ_LINK_SPEED_20GB:
1187 itr += DIV_ROUND_UP(85 * (avg_pkt_size + 24),
1188 avg_pkt_size + 640);
1189 break;
1190 case ICE_AQ_LINK_SPEED_10GB:
1191
1192 default:
1193 itr += DIV_ROUND_UP(170 * (avg_pkt_size + 24),
1194 avg_pkt_size + 640);
1195 break;
1196 }
1197
1198 if ((itr & ICE_ITR_MASK) > ICE_ITR_ADAPTIVE_MAX_USECS) {
1199 itr &= ICE_ITR_ADAPTIVE_LATENCY;
1200 itr += ICE_ITR_ADAPTIVE_MAX_USECS;
1201 }
1202
1203 return itr;
1204}
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219static void
1220ice_update_itr(struct ice_q_vector *q_vector, struct ice_ring_container *rc)
1221{
1222 unsigned long next_update = jiffies;
1223 unsigned int packets, bytes, itr;
1224 bool container_is_rx;
1225
1226 if (!rc->ring || !ITR_IS_DYNAMIC(rc->itr_setting))
1227 return;
1228
1229
1230
1231
1232
1233
1234
1235 if (q_vector->itr_countdown) {
1236 itr = rc->target_itr;
1237 goto clear_counts;
1238 }
1239
1240 container_is_rx = (&q_vector->rx == rc);
1241
1242
1243
1244 itr = container_is_rx ?
1245 ICE_ITR_ADAPTIVE_MIN_USECS | ICE_ITR_ADAPTIVE_LATENCY :
1246 ICE_ITR_ADAPTIVE_MAX_USECS | ICE_ITR_ADAPTIVE_LATENCY;
1247
1248
1249
1250
1251
1252
1253 if (time_after(next_update, rc->next_update))
1254 goto clear_counts;
1255
1256 prefetch(q_vector->vsi->port_info);
1257
1258 packets = rc->total_pkts;
1259 bytes = rc->total_bytes;
1260
1261 if (container_is_rx) {
1262
1263
1264
1265
1266
1267 if (packets && packets < 4 && bytes < 9000 &&
1268 (q_vector->tx.target_itr & ICE_ITR_ADAPTIVE_LATENCY)) {
1269 itr = ICE_ITR_ADAPTIVE_LATENCY;
1270 goto adjust_by_size_and_speed;
1271 }
1272 } else if (packets < 4) {
1273
1274
1275
1276
1277
1278 if (rc->target_itr == ICE_ITR_ADAPTIVE_MAX_USECS &&
1279 (q_vector->rx.target_itr & ICE_ITR_MASK) ==
1280 ICE_ITR_ADAPTIVE_MAX_USECS)
1281 goto clear_counts;
1282 } else if (packets > 32) {
1283
1284
1285
1286 rc->target_itr &= ~ICE_ITR_ADAPTIVE_LATENCY;
1287 }
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297 if (packets < 56) {
1298 itr = rc->target_itr + ICE_ITR_ADAPTIVE_MIN_INC;
1299 if ((itr & ICE_ITR_MASK) > ICE_ITR_ADAPTIVE_MAX_USECS) {
1300 itr &= ICE_ITR_ADAPTIVE_LATENCY;
1301 itr += ICE_ITR_ADAPTIVE_MAX_USECS;
1302 }
1303 goto clear_counts;
1304 }
1305
1306 if (packets <= 256) {
1307 itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
1308 itr &= ICE_ITR_MASK;
1309
1310
1311
1312
1313
1314 if (packets <= 112)
1315 goto clear_counts;
1316
1317
1318
1319
1320
1321
1322 itr >>= 1;
1323 itr &= ICE_ITR_MASK;
1324 if (itr < ICE_ITR_ADAPTIVE_MIN_USECS)
1325 itr = ICE_ITR_ADAPTIVE_MIN_USECS;
1326
1327 goto clear_counts;
1328 }
1329
1330
1331
1332
1333
1334
1335
1336 itr = ICE_ITR_ADAPTIVE_BULK;
1337
1338adjust_by_size_and_speed:
1339
1340
1341 itr = ice_adjust_itr_by_size_and_speed(q_vector->vsi->port_info,
1342 bytes / packets, itr);
1343
1344clear_counts:
1345
1346 rc->target_itr = itr;
1347
1348
1349 rc->next_update = next_update + 1;
1350
1351 rc->total_bytes = 0;
1352 rc->total_pkts = 0;
1353}
1354
1355
1356
1357
1358
1359
1360static u32 ice_buildreg_itr(u16 itr_idx, u16 itr)
1361{
1362
1363
1364
1365
1366
1367
1368
1369 itr &= ICE_ITR_MASK;
1370
1371 return GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
1372 (itr_idx << GLINT_DYN_CTL_ITR_INDX_S) |
1373 (itr << (GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S));
1374}
1375
1376
1377
1378
1379
1380
1381
1382
1383#define ITR_COUNTDOWN_START 3
1384
1385
1386
1387
1388
1389static void ice_update_ena_itr(struct ice_q_vector *q_vector)
1390{
1391 struct ice_ring_container *tx = &q_vector->tx;
1392 struct ice_ring_container *rx = &q_vector->rx;
1393 struct ice_vsi *vsi = q_vector->vsi;
1394 u32 itr_val;
1395
1396
1397
1398
1399
1400 if (q_vector->itr_countdown == ICE_IN_WB_ON_ITR_MODE) {
1401 itr_val = ice_buildreg_itr(rx->itr_idx, ICE_WB_ON_ITR_USECS);
1402 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), itr_val);
1403
1404 rx->target_itr = rx->itr_setting;
1405
1406 rx->current_itr = ICE_WB_ON_ITR_USECS |
1407 (rx->itr_setting & ICE_ITR_DYNAMIC);
1408
1409 q_vector->itr_countdown = 0;
1410 return;
1411 }
1412
1413
1414 ice_update_itr(q_vector, tx);
1415 ice_update_itr(q_vector, rx);
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425 if (rx->target_itr < rx->current_itr) {
1426
1427 itr_val = ice_buildreg_itr(rx->itr_idx, rx->target_itr);
1428 rx->current_itr = rx->target_itr;
1429 q_vector->itr_countdown = ITR_COUNTDOWN_START;
1430 } else if ((tx->target_itr < tx->current_itr) ||
1431 ((rx->target_itr - rx->current_itr) <
1432 (tx->target_itr - tx->current_itr))) {
1433
1434
1435
1436 itr_val = ice_buildreg_itr(tx->itr_idx, tx->target_itr);
1437 tx->current_itr = tx->target_itr;
1438 q_vector->itr_countdown = ITR_COUNTDOWN_START;
1439 } else if (rx->current_itr != rx->target_itr) {
1440
1441 itr_val = ice_buildreg_itr(rx->itr_idx, rx->target_itr);
1442 rx->current_itr = rx->target_itr;
1443 q_vector->itr_countdown = ITR_COUNTDOWN_START;
1444 } else {
1445
1446 itr_val = ice_buildreg_itr(ICE_ITR_NONE, 0);
1447 if (q_vector->itr_countdown)
1448 q_vector->itr_countdown--;
1449 }
1450
1451 if (!test_bit(__ICE_DOWN, q_vector->vsi->state))
1452 wr32(&q_vector->vsi->back->hw,
1453 GLINT_DYN_CTL(q_vector->reg_idx),
1454 itr_val);
1455}
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471static void ice_set_wb_on_itr(struct ice_q_vector *q_vector)
1472{
1473 struct ice_vsi *vsi = q_vector->vsi;
1474
1475
1476 if (q_vector->itr_countdown == ICE_IN_WB_ON_ITR_MODE)
1477 return;
1478
1479 if (q_vector->num_ring_rx)
1480 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
1481 ICE_GLINT_DYN_CTL_WB_ON_ITR(ICE_WB_ON_ITR_USECS,
1482 ICE_RX_ITR));
1483
1484 if (q_vector->num_ring_tx)
1485 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
1486 ICE_GLINT_DYN_CTL_WB_ON_ITR(ICE_WB_ON_ITR_USECS,
1487 ICE_TX_ITR));
1488
1489 q_vector->itr_countdown = ICE_IN_WB_ON_ITR_MODE;
1490}
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501int ice_napi_poll(struct napi_struct *napi, int budget)
1502{
1503 struct ice_q_vector *q_vector =
1504 container_of(napi, struct ice_q_vector, napi);
1505 bool clean_complete = true;
1506 struct ice_ring *ring;
1507 int budget_per_ring;
1508 int work_done = 0;
1509
1510
1511
1512
1513 ice_for_each_ring(ring, q_vector->tx) {
1514 bool wd = ring->xsk_umem ?
1515 ice_clean_tx_irq_zc(ring, budget) :
1516 ice_clean_tx_irq(ring, budget);
1517
1518 if (!wd)
1519 clean_complete = false;
1520 }
1521
1522
1523 if (unlikely(budget <= 0))
1524 return budget;
1525
1526
1527 if (unlikely(q_vector->num_ring_rx > 1))
1528
1529
1530
1531
1532 budget_per_ring = max(budget / q_vector->num_ring_rx, 1);
1533 else
1534
1535 budget_per_ring = budget;
1536
1537 ice_for_each_ring(ring, q_vector->rx) {
1538 int cleaned;
1539
1540
1541
1542
1543
1544 cleaned = ring->xsk_umem ?
1545 ice_clean_rx_irq_zc(ring, budget_per_ring) :
1546 ice_clean_rx_irq(ring, budget_per_ring);
1547 work_done += cleaned;
1548
1549 if (cleaned >= budget_per_ring)
1550 clean_complete = false;
1551 }
1552
1553
1554 if (!clean_complete)
1555 return budget;
1556
1557
1558
1559
1560 if (likely(napi_complete_done(napi, work_done)))
1561 ice_update_ena_itr(q_vector);
1562 else
1563 ice_set_wb_on_itr(q_vector);
1564
1565 return min_t(int, work_done, budget - 1);
1566}
1567
1568
1569
1570
1571
1572
1573
1574
1575static int __ice_maybe_stop_tx(struct ice_ring *tx_ring, unsigned int size)
1576{
1577 netif_stop_subqueue(tx_ring->netdev, tx_ring->q_index);
1578
1579 smp_mb();
1580
1581
1582 if (likely(ICE_DESC_UNUSED(tx_ring) < size))
1583 return -EBUSY;
1584
1585
1586 netif_start_subqueue(tx_ring->netdev, tx_ring->q_index);
1587 ++tx_ring->tx_stats.restart_q;
1588 return 0;
1589}
1590
1591
1592
1593
1594
1595
1596
1597
1598static int ice_maybe_stop_tx(struct ice_ring *tx_ring, unsigned int size)
1599{
1600 if (likely(ICE_DESC_UNUSED(tx_ring) >= size))
1601 return 0;
1602
1603 return __ice_maybe_stop_tx(tx_ring, size);
1604}
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616static void
1617ice_tx_map(struct ice_ring *tx_ring, struct ice_tx_buf *first,
1618 struct ice_tx_offload_params *off)
1619{
1620 u64 td_offset, td_tag, td_cmd;
1621 u16 i = tx_ring->next_to_use;
1622 unsigned int data_len, size;
1623 struct ice_tx_desc *tx_desc;
1624 struct ice_tx_buf *tx_buf;
1625 struct sk_buff *skb;
1626 skb_frag_t *frag;
1627 dma_addr_t dma;
1628
1629 td_tag = off->td_l2tag1;
1630 td_cmd = off->td_cmd;
1631 td_offset = off->td_offset;
1632 skb = first->skb;
1633
1634 data_len = skb->data_len;
1635 size = skb_headlen(skb);
1636
1637 tx_desc = ICE_TX_DESC(tx_ring, i);
1638
1639 if (first->tx_flags & ICE_TX_FLAGS_HW_VLAN) {
1640 td_cmd |= (u64)ICE_TX_DESC_CMD_IL2TAG1;
1641 td_tag = (first->tx_flags & ICE_TX_FLAGS_VLAN_M) >>
1642 ICE_TX_FLAGS_VLAN_S;
1643 }
1644
1645 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1646
1647 tx_buf = first;
1648
1649 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1650 unsigned int max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1651
1652 if (dma_mapping_error(tx_ring->dev, dma))
1653 goto dma_error;
1654
1655
1656 dma_unmap_len_set(tx_buf, len, size);
1657 dma_unmap_addr_set(tx_buf, dma, dma);
1658
1659
1660 max_data += -dma & (ICE_MAX_READ_REQ_SIZE - 1);
1661 tx_desc->buf_addr = cpu_to_le64(dma);
1662
1663
1664
1665
1666 while (unlikely(size > ICE_MAX_DATA_PER_TXD)) {
1667 tx_desc->cmd_type_offset_bsz =
1668 build_ctob(td_cmd, td_offset, max_data, td_tag);
1669
1670 tx_desc++;
1671 i++;
1672
1673 if (i == tx_ring->count) {
1674 tx_desc = ICE_TX_DESC(tx_ring, 0);
1675 i = 0;
1676 }
1677
1678 dma += max_data;
1679 size -= max_data;
1680
1681 max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1682 tx_desc->buf_addr = cpu_to_le64(dma);
1683 }
1684
1685 if (likely(!data_len))
1686 break;
1687
1688 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1689 size, td_tag);
1690
1691 tx_desc++;
1692 i++;
1693
1694 if (i == tx_ring->count) {
1695 tx_desc = ICE_TX_DESC(tx_ring, 0);
1696 i = 0;
1697 }
1698
1699 size = skb_frag_size(frag);
1700 data_len -= size;
1701
1702 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1703 DMA_TO_DEVICE);
1704
1705 tx_buf = &tx_ring->tx_buf[i];
1706 }
1707
1708
1709 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1710
1711
1712 skb_tx_timestamp(first->skb);
1713
1714 i++;
1715 if (i == tx_ring->count)
1716 i = 0;
1717
1718
1719 td_cmd |= (u64)ICE_TXD_LAST_DESC_CMD;
1720 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset, size,
1721 td_tag);
1722
1723
1724
1725
1726
1727
1728
1729 wmb();
1730
1731
1732 first->next_to_watch = tx_desc;
1733
1734 tx_ring->next_to_use = i;
1735
1736 ice_maybe_stop_tx(tx_ring, DESC_NEEDED);
1737
1738
1739 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more())
1740 writel(i, tx_ring->tail);
1741
1742 return;
1743
1744dma_error:
1745
1746 for (;;) {
1747 tx_buf = &tx_ring->tx_buf[i];
1748 ice_unmap_and_free_tx_buf(tx_ring, tx_buf);
1749 if (tx_buf == first)
1750 break;
1751 if (i == 0)
1752 i = tx_ring->count;
1753 i--;
1754 }
1755
1756 tx_ring->next_to_use = i;
1757}
1758
1759
1760
1761
1762
1763
1764
1765
1766static
1767int ice_tx_csum(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
1768{
1769 u32 l4_len = 0, l3_len = 0, l2_len = 0;
1770 struct sk_buff *skb = first->skb;
1771 union {
1772 struct iphdr *v4;
1773 struct ipv6hdr *v6;
1774 unsigned char *hdr;
1775 } ip;
1776 union {
1777 struct tcphdr *tcp;
1778 unsigned char *hdr;
1779 } l4;
1780 __be16 frag_off, protocol;
1781 unsigned char *exthdr;
1782 u32 offset, cmd = 0;
1783 u8 l4_proto = 0;
1784
1785 if (skb->ip_summed != CHECKSUM_PARTIAL)
1786 return 0;
1787
1788 ip.hdr = skb_network_header(skb);
1789 l4.hdr = skb_transport_header(skb);
1790
1791
1792 l2_len = ip.hdr - skb->data;
1793 offset = (l2_len / 2) << ICE_TX_DESC_LEN_MACLEN_S;
1794
1795 if (skb->encapsulation)
1796 return -1;
1797
1798
1799 protocol = vlan_get_protocol(skb);
1800 if (protocol == htons(ETH_P_IP)) {
1801 l4_proto = ip.v4->protocol;
1802
1803
1804
1805 if (first->tx_flags & ICE_TX_FLAGS_TSO)
1806 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4_CSUM;
1807 else
1808 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4;
1809
1810 } else if (protocol == htons(ETH_P_IPV6)) {
1811 cmd |= ICE_TX_DESC_CMD_IIPT_IPV6;
1812 exthdr = ip.hdr + sizeof(*ip.v6);
1813 l4_proto = ip.v6->nexthdr;
1814 if (l4.hdr != exthdr)
1815 ipv6_skip_exthdr(skb, exthdr - skb->data, &l4_proto,
1816 &frag_off);
1817 } else {
1818 return -1;
1819 }
1820
1821
1822 l3_len = l4.hdr - ip.hdr;
1823 offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;
1824
1825
1826 switch (l4_proto) {
1827 case IPPROTO_TCP:
1828
1829 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
1830 l4_len = l4.tcp->doff;
1831 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
1832 break;
1833 case IPPROTO_UDP:
1834
1835 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_UDP;
1836 l4_len = (sizeof(struct udphdr) >> 2);
1837 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
1838 break;
1839 case IPPROTO_SCTP:
1840
1841 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_SCTP;
1842 l4_len = sizeof(struct sctphdr) >> 2;
1843 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
1844 break;
1845
1846 default:
1847 if (first->tx_flags & ICE_TX_FLAGS_TSO)
1848 return -1;
1849 skb_checksum_help(skb);
1850 return 0;
1851 }
1852
1853 off->td_cmd |= cmd;
1854 off->td_offset |= offset;
1855 return 1;
1856}
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869static int
1870ice_tx_prepare_vlan_flags(struct ice_ring *tx_ring, struct ice_tx_buf *first)
1871{
1872 struct sk_buff *skb = first->skb;
1873 __be16 protocol = skb->protocol;
1874
1875 if (protocol == htons(ETH_P_8021Q) &&
1876 !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
1877
1878
1879
1880
1881
1882
1883
1884 skb->protocol = vlan_get_protocol(skb);
1885 return 0;
1886 }
1887
1888
1889 if (skb_vlan_tag_present(skb)) {
1890 first->tx_flags |= skb_vlan_tag_get(skb) << ICE_TX_FLAGS_VLAN_S;
1891 first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
1892 } else if (protocol == htons(ETH_P_8021Q)) {
1893 struct vlan_hdr *vhdr, _vhdr;
1894
1895
1896 vhdr = (struct vlan_hdr *)skb_header_pointer(skb, ETH_HLEN,
1897 sizeof(_vhdr),
1898 &_vhdr);
1899 if (!vhdr)
1900 return -EINVAL;
1901
1902 first->tx_flags |= ntohs(vhdr->h_vlan_TCI) <<
1903 ICE_TX_FLAGS_VLAN_S;
1904 first->tx_flags |= ICE_TX_FLAGS_SW_VLAN;
1905 }
1906
1907 return ice_tx_prepare_vlan_flags_dcb(tx_ring, first);
1908}
1909
1910
1911
1912
1913
1914
1915
1916
1917static
1918int ice_tso(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
1919{
1920 struct sk_buff *skb = first->skb;
1921 union {
1922 struct iphdr *v4;
1923 struct ipv6hdr *v6;
1924 unsigned char *hdr;
1925 } ip;
1926 union {
1927 struct tcphdr *tcp;
1928 struct udphdr *udp;
1929 unsigned char *hdr;
1930 } l4;
1931 u64 cd_mss, cd_tso_len;
1932 u32 paylen, l4_start;
1933 int err;
1934
1935 if (skb->ip_summed != CHECKSUM_PARTIAL)
1936 return 0;
1937
1938 if (!skb_is_gso(skb))
1939 return 0;
1940
1941 err = skb_cow_head(skb, 0);
1942 if (err < 0)
1943 return err;
1944
1945
1946 ip.hdr = skb_network_header(skb);
1947 l4.hdr = skb_transport_header(skb);
1948
1949
1950 if (ip.v4->version == 4) {
1951 ip.v4->tot_len = 0;
1952 ip.v4->check = 0;
1953 } else {
1954 ip.v6->payload_len = 0;
1955 }
1956
1957
1958 l4_start = l4.hdr - skb->data;
1959
1960
1961 paylen = skb->len - l4_start;
1962
1963 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
1964 csum_replace_by_diff(&l4.udp->check,
1965 (__force __wsum)htonl(paylen));
1966
1967 off->header_len = sizeof(l4.udp) + l4_start;
1968 } else {
1969 csum_replace_by_diff(&l4.tcp->check,
1970 (__force __wsum)htonl(paylen));
1971
1972 off->header_len = (l4.tcp->doff * 4) + l4_start;
1973 }
1974
1975
1976 first->gso_segs = skb_shinfo(skb)->gso_segs;
1977 first->bytecount += (first->gso_segs - 1) * off->header_len;
1978
1979 cd_tso_len = skb->len - off->header_len;
1980 cd_mss = skb_shinfo(skb)->gso_size;
1981
1982
1983 off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
1984 (ICE_TX_CTX_DESC_TSO << ICE_TXD_CTX_QW1_CMD_S) |
1985 (cd_tso_len << ICE_TXD_CTX_QW1_TSO_LEN_S) |
1986 (cd_mss << ICE_TXD_CTX_QW1_MSS_S));
1987 first->tx_flags |= ICE_TX_FLAGS_TSO;
1988 return 1;
1989}
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019static unsigned int ice_txd_use_count(unsigned int size)
2020{
2021 return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
2022}
2023
2024
2025
2026
2027
2028
2029
2030static unsigned int ice_xmit_desc_count(struct sk_buff *skb)
2031{
2032 const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
2033 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2034 unsigned int count = 0, size = skb_headlen(skb);
2035
2036 for (;;) {
2037 count += ice_txd_use_count(size);
2038
2039 if (!nr_frags--)
2040 break;
2041
2042 size = skb_frag_size(frag++);
2043 }
2044
2045 return count;
2046}
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061static bool __ice_chk_linearize(struct sk_buff *skb)
2062{
2063 const skb_frag_t *frag, *stale;
2064 int nr_frags, sum;
2065
2066
2067 nr_frags = skb_shinfo(skb)->nr_frags;
2068 if (nr_frags < (ICE_MAX_BUF_TXD - 1))
2069 return false;
2070
2071
2072
2073
2074 nr_frags -= ICE_MAX_BUF_TXD - 2;
2075 frag = &skb_shinfo(skb)->frags[0];
2076
2077
2078
2079
2080
2081
2082
2083 sum = 1 - skb_shinfo(skb)->gso_size;
2084
2085
2086 sum += skb_frag_size(frag++);
2087 sum += skb_frag_size(frag++);
2088 sum += skb_frag_size(frag++);
2089 sum += skb_frag_size(frag++);
2090 sum += skb_frag_size(frag++);
2091
2092
2093
2094
2095 stale = &skb_shinfo(skb)->frags[0];
2096 for (;;) {
2097 sum += skb_frag_size(frag++);
2098
2099
2100 if (sum < 0)
2101 return true;
2102
2103 if (!nr_frags--)
2104 break;
2105
2106 sum -= skb_frag_size(stale++);
2107 }
2108
2109 return false;
2110}
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121static bool ice_chk_linearize(struct sk_buff *skb, unsigned int count)
2122{
2123
2124 if (likely(count < ICE_MAX_BUF_TXD))
2125 return false;
2126
2127 if (skb_is_gso(skb))
2128 return __ice_chk_linearize(skb);
2129
2130
2131 return count != ICE_MAX_BUF_TXD;
2132}
2133
2134
2135
2136
2137
2138
2139
2140
2141static netdev_tx_t
2142ice_xmit_frame_ring(struct sk_buff *skb, struct ice_ring *tx_ring)
2143{
2144 struct ice_tx_offload_params offload = { 0 };
2145 struct ice_vsi *vsi = tx_ring->vsi;
2146 struct ice_tx_buf *first;
2147 unsigned int count;
2148 int tso, csum;
2149
2150 count = ice_xmit_desc_count(skb);
2151 if (ice_chk_linearize(skb, count)) {
2152 if (__skb_linearize(skb))
2153 goto out_drop;
2154 count = ice_txd_use_count(skb->len);
2155 tx_ring->tx_stats.tx_linearize++;
2156 }
2157
2158
2159
2160
2161
2162
2163
2164 if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE +
2165 ICE_DESCS_FOR_CTX_DESC)) {
2166 tx_ring->tx_stats.tx_busy++;
2167 return NETDEV_TX_BUSY;
2168 }
2169
2170 offload.tx_ring = tx_ring;
2171
2172
2173 first = &tx_ring->tx_buf[tx_ring->next_to_use];
2174 first->skb = skb;
2175 first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
2176 first->gso_segs = 1;
2177 first->tx_flags = 0;
2178
2179
2180 if (ice_tx_prepare_vlan_flags(tx_ring, first))
2181 goto out_drop;
2182
2183
2184 tso = ice_tso(first, &offload);
2185 if (tso < 0)
2186 goto out_drop;
2187
2188
2189 csum = ice_tx_csum(first, &offload);
2190 if (csum < 0)
2191 goto out_drop;
2192
2193
2194 if (unlikely(skb->priority == TC_PRIO_CONTROL &&
2195 vsi->type == ICE_VSI_PF &&
2196 vsi->port_info->is_sw_lldp))
2197 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2198 ICE_TX_CTX_DESC_SWTCH_UPLINK <<
2199 ICE_TXD_CTX_QW1_CMD_S);
2200
2201 if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) {
2202 struct ice_tx_ctx_desc *cdesc;
2203 int i = tx_ring->next_to_use;
2204
2205
2206 cdesc = ICE_TX_CTX_DESC(tx_ring, i);
2207 i++;
2208 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2209
2210
2211 cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params);
2212 cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2);
2213 cdesc->rsvd = cpu_to_le16(0);
2214 cdesc->qw1 = cpu_to_le64(offload.cd_qw1);
2215 }
2216
2217 ice_tx_map(tx_ring, first, &offload);
2218 return NETDEV_TX_OK;
2219
2220out_drop:
2221 dev_kfree_skb_any(skb);
2222 return NETDEV_TX_OK;
2223}
2224
2225
2226
2227
2228
2229
2230
2231
2232netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2233{
2234 struct ice_netdev_priv *np = netdev_priv(netdev);
2235 struct ice_vsi *vsi = np->vsi;
2236 struct ice_ring *tx_ring;
2237
2238 tx_ring = vsi->tx_rings[skb->queue_mapping];
2239
2240
2241
2242
2243 if (skb_put_padto(skb, ICE_MIN_TX_LEN))
2244 return NETDEV_TX_OK;
2245
2246 return ice_xmit_frame_ring(skb, tx_ring);
2247}
2248