1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* The driver transmit and receive code */
5
6 #include <linux/mm.h>
7 #include <linux/netdevice.h>
8 #include <linux/prefetch.h>
9 #include <linux/bpf_trace.h>
10 #include <net/dsfield.h>
11 #include <net/mpls.h>
12 #include <net/xdp.h>
13 #include "ice_txrx_lib.h"
14 #include "ice_lib.h"
15 #include "ice.h"
16 #include "ice_trace.h"
17 #include "ice_dcb_lib.h"
18 #include "ice_xsk.h"
19 #include "ice_eswitch.h"
20
21 #define ICE_RX_HDR_SIZE 256
22
23 #define ICE_FDIR_CLEAN_DELAY 10
24
25 /**
26 * ice_prgm_fdir_fltr - Program a Flow Director filter
27 * @vsi: VSI to send dummy packet
28 * @fdir_desc: flow director descriptor
29 * @raw_packet: allocated buffer for flow director
30 */
31 int
ice_prgm_fdir_fltr(struct ice_vsi * vsi,struct ice_fltr_desc * fdir_desc,u8 * raw_packet)32 ice_prgm_fdir_fltr(struct ice_vsi *vsi, struct ice_fltr_desc *fdir_desc,
33 u8 *raw_packet)
34 {
35 struct ice_tx_buf *tx_buf, *first;
36 struct ice_fltr_desc *f_desc;
37 struct ice_tx_desc *tx_desc;
38 struct ice_tx_ring *tx_ring;
39 struct device *dev;
40 dma_addr_t dma;
41 u32 td_cmd;
42 u16 i;
43
44 /* VSI and Tx ring */
45 if (!vsi)
46 return -ENOENT;
47 tx_ring = vsi->tx_rings[0];
48 if (!tx_ring || !tx_ring->desc)
49 return -ENOENT;
50 dev = tx_ring->dev;
51
52 /* we are using two descriptors to add/del a filter and we can wait */
53 for (i = ICE_FDIR_CLEAN_DELAY; ICE_DESC_UNUSED(tx_ring) < 2; i--) {
54 if (!i)
55 return -EAGAIN;
56 msleep_interruptible(1);
57 }
58
59 dma = dma_map_single(dev, raw_packet, ICE_FDIR_MAX_RAW_PKT_SIZE,
60 DMA_TO_DEVICE);
61
62 if (dma_mapping_error(dev, dma))
63 return -EINVAL;
64
65 /* grab the next descriptor */
66 i = tx_ring->next_to_use;
67 first = &tx_ring->tx_buf[i];
68 f_desc = ICE_TX_FDIRDESC(tx_ring, i);
69 memcpy(f_desc, fdir_desc, sizeof(*f_desc));
70
71 i++;
72 i = (i < tx_ring->count) ? i : 0;
73 tx_desc = ICE_TX_DESC(tx_ring, i);
74 tx_buf = &tx_ring->tx_buf[i];
75
76 i++;
77 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
78
79 memset(tx_buf, 0, sizeof(*tx_buf));
80 dma_unmap_len_set(tx_buf, len, ICE_FDIR_MAX_RAW_PKT_SIZE);
81 dma_unmap_addr_set(tx_buf, dma, dma);
82
83 tx_desc->buf_addr = cpu_to_le64(dma);
84 td_cmd = ICE_TXD_LAST_DESC_CMD | ICE_TX_DESC_CMD_DUMMY |
85 ICE_TX_DESC_CMD_RE;
86
87 tx_buf->type = ICE_TX_BUF_DUMMY;
88 tx_buf->raw_buf = raw_packet;
89
90 tx_desc->cmd_type_offset_bsz =
91 ice_build_ctob(td_cmd, 0, ICE_FDIR_MAX_RAW_PKT_SIZE, 0);
92
93 /* Force memory write to complete before letting h/w know
94 * there are new descriptors to fetch.
95 */
96 wmb();
97
98 /* mark the data descriptor to be watched */
99 first->next_to_watch = tx_desc;
100
101 writel(tx_ring->next_to_use, tx_ring->tail);
102
103 return 0;
104 }
105
106 /**
107 * ice_unmap_and_free_tx_buf - Release a Tx buffer
108 * @ring: the ring that owns the buffer
109 * @tx_buf: the buffer to free
110 */
111 static void
ice_unmap_and_free_tx_buf(struct ice_tx_ring * ring,struct ice_tx_buf * tx_buf)112 ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf)
113 {
114 if (dma_unmap_len(tx_buf, len))
115 dma_unmap_page(ring->dev,
116 dma_unmap_addr(tx_buf, dma),
117 dma_unmap_len(tx_buf, len),
118 DMA_TO_DEVICE);
119
120 switch (tx_buf->type) {
121 case ICE_TX_BUF_DUMMY:
122 devm_kfree(ring->dev, tx_buf->raw_buf);
123 break;
124 case ICE_TX_BUF_SKB:
125 dev_kfree_skb_any(tx_buf->skb);
126 break;
127 case ICE_TX_BUF_XDP_TX:
128 page_frag_free(tx_buf->raw_buf);
129 break;
130 case ICE_TX_BUF_XDP_XMIT:
131 xdp_return_frame(tx_buf->xdpf);
132 break;
133 }
134
135 tx_buf->next_to_watch = NULL;
136 tx_buf->type = ICE_TX_BUF_EMPTY;
137 dma_unmap_len_set(tx_buf, len, 0);
138 /* tx_buf must be completely set up in the transmit path */
139 }
140
txring_txq(const struct ice_tx_ring * ring)141 static struct netdev_queue *txring_txq(const struct ice_tx_ring *ring)
142 {
143 return netdev_get_tx_queue(ring->netdev, ring->q_index);
144 }
145
146 /**
147 * ice_clean_tstamp_ring - clean time stamp ring
148 * @tx_ring: Tx ring to clean the Time Stamp ring for
149 */
ice_clean_tstamp_ring(struct ice_tx_ring * tx_ring)150 static void ice_clean_tstamp_ring(struct ice_tx_ring *tx_ring)
151 {
152 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring;
153 u32 size;
154
155 if (!tstamp_ring->desc)
156 return;
157
158 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc),
159 PAGE_SIZE);
160 memset(tstamp_ring->desc, 0, size);
161 tstamp_ring->next_to_use = 0;
162 }
163
164 /**
165 * ice_free_tstamp_ring - free time stamp resources per queue
166 * @tx_ring: Tx ring to free the Time Stamp ring for
167 */
ice_free_tstamp_ring(struct ice_tx_ring * tx_ring)168 void ice_free_tstamp_ring(struct ice_tx_ring *tx_ring)
169 {
170 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring;
171 u32 size;
172
173 if (!tstamp_ring->desc)
174 return;
175
176 ice_clean_tstamp_ring(tx_ring);
177 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc),
178 PAGE_SIZE);
179 dmam_free_coherent(tx_ring->dev, size, tstamp_ring->desc,
180 tstamp_ring->dma);
181 tstamp_ring->desc = NULL;
182 }
183
184 /**
185 * ice_free_tx_tstamp_ring - free time stamp resources per Tx ring
186 * @tx_ring: Tx ring to free the Time Stamp ring for
187 */
ice_free_tx_tstamp_ring(struct ice_tx_ring * tx_ring)188 void ice_free_tx_tstamp_ring(struct ice_tx_ring *tx_ring)
189 {
190 ice_free_tstamp_ring(tx_ring);
191 kfree_rcu(tx_ring->tstamp_ring, rcu);
192 tx_ring->tstamp_ring = NULL;
193 tx_ring->flags &= ~ICE_TX_FLAGS_TXTIME;
194 }
195
196 /**
197 * ice_clean_tx_ring - Free any empty Tx buffers
198 * @tx_ring: ring to be cleaned
199 */
ice_clean_tx_ring(struct ice_tx_ring * tx_ring)200 void ice_clean_tx_ring(struct ice_tx_ring *tx_ring)
201 {
202 u32 size;
203 u16 i;
204
205 if (ice_ring_is_xdp(tx_ring) && tx_ring->xsk_pool) {
206 ice_xsk_clean_xdp_ring(tx_ring);
207 goto tx_skip_free;
208 }
209
210 /* ring already cleared, nothing to do */
211 if (!tx_ring->tx_buf)
212 return;
213
214 /* Free all the Tx ring sk_buffs */
215 for (i = 0; i < tx_ring->count; i++)
216 ice_unmap_and_free_tx_buf(tx_ring, &tx_ring->tx_buf[i]);
217
218 tx_skip_free:
219 memset(tx_ring->tx_buf, 0, sizeof(*tx_ring->tx_buf) * tx_ring->count);
220
221 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
222 PAGE_SIZE);
223 /* Zero out the descriptor ring */
224 memset(tx_ring->desc, 0, size);
225
226 tx_ring->next_to_use = 0;
227 tx_ring->next_to_clean = 0;
228
229 if (!tx_ring->netdev)
230 return;
231
232 /* cleanup Tx queue statistics */
233 netdev_tx_reset_queue(txring_txq(tx_ring));
234
235 if (ice_is_txtime_cfg(tx_ring))
236 ice_free_tx_tstamp_ring(tx_ring);
237 }
238
239 /**
240 * ice_free_tx_ring - Free Tx resources per queue
241 * @tx_ring: Tx descriptor ring for a specific queue
242 *
243 * Free all transmit software resources
244 */
ice_free_tx_ring(struct ice_tx_ring * tx_ring)245 void ice_free_tx_ring(struct ice_tx_ring *tx_ring)
246 {
247 u32 size;
248
249 ice_clean_tx_ring(tx_ring);
250 devm_kfree(tx_ring->dev, tx_ring->tx_buf);
251 tx_ring->tx_buf = NULL;
252
253 if (tx_ring->desc) {
254 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
255 PAGE_SIZE);
256 dmam_free_coherent(tx_ring->dev, size,
257 tx_ring->desc, tx_ring->dma);
258 tx_ring->desc = NULL;
259 }
260 }
261
262 /**
263 * ice_clean_tx_irq - Reclaim resources after transmit completes
264 * @tx_ring: Tx ring to clean
265 * @napi_budget: Used to determine if we are in netpoll
266 *
267 * Returns true if there's any budget left (e.g. the clean is finished)
268 */
ice_clean_tx_irq(struct ice_tx_ring * tx_ring,int napi_budget)269 static bool ice_clean_tx_irq(struct ice_tx_ring *tx_ring, int napi_budget)
270 {
271 unsigned int total_bytes = 0, total_pkts = 0;
272 unsigned int budget = ICE_DFLT_IRQ_WORK;
273 struct ice_vsi *vsi = tx_ring->vsi;
274 s16 i = tx_ring->next_to_clean;
275 struct ice_tx_desc *tx_desc;
276 struct ice_tx_buf *tx_buf;
277
278 /* get the bql data ready */
279 netdev_txq_bql_complete_prefetchw(txring_txq(tx_ring));
280
281 tx_buf = &tx_ring->tx_buf[i];
282 tx_desc = ICE_TX_DESC(tx_ring, i);
283 i -= tx_ring->count;
284
285 prefetch(&vsi->state);
286
287 do {
288 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
289
290 /* if next_to_watch is not set then there is no work pending */
291 if (!eop_desc)
292 break;
293
294 /* follow the guidelines of other drivers */
295 prefetchw(&tx_buf->skb->users);
296
297 smp_rmb(); /* prevent any other reads prior to eop_desc */
298
299 ice_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
300 /* if the descriptor isn't done, no work yet to do */
301 if (!(eop_desc->cmd_type_offset_bsz &
302 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
303 break;
304
305 /* clear next_to_watch to prevent false hangs */
306 tx_buf->next_to_watch = NULL;
307
308 /* update the statistics for this packet */
309 total_bytes += tx_buf->bytecount;
310 total_pkts += tx_buf->gso_segs;
311
312 /* free the skb */
313 napi_consume_skb(tx_buf->skb, napi_budget);
314
315 /* unmap skb header data */
316 dma_unmap_single(tx_ring->dev,
317 dma_unmap_addr(tx_buf, dma),
318 dma_unmap_len(tx_buf, len),
319 DMA_TO_DEVICE);
320
321 /* clear tx_buf data */
322 tx_buf->type = ICE_TX_BUF_EMPTY;
323 dma_unmap_len_set(tx_buf, len, 0);
324
325 /* unmap remaining buffers */
326 while (tx_desc != eop_desc) {
327 ice_trace(clean_tx_irq_unmap, tx_ring, tx_desc, tx_buf);
328 tx_buf++;
329 tx_desc++;
330 i++;
331 if (unlikely(!i)) {
332 i -= tx_ring->count;
333 tx_buf = tx_ring->tx_buf;
334 tx_desc = ICE_TX_DESC(tx_ring, 0);
335 }
336
337 /* unmap any remaining paged data */
338 if (dma_unmap_len(tx_buf, len)) {
339 dma_unmap_page(tx_ring->dev,
340 dma_unmap_addr(tx_buf, dma),
341 dma_unmap_len(tx_buf, len),
342 DMA_TO_DEVICE);
343 dma_unmap_len_set(tx_buf, len, 0);
344 }
345 }
346 ice_trace(clean_tx_irq_unmap_eop, tx_ring, tx_desc, tx_buf);
347
348 /* move us one more past the eop_desc for start of next pkt */
349 tx_buf++;
350 tx_desc++;
351 i++;
352 if (unlikely(!i)) {
353 i -= tx_ring->count;
354 tx_buf = tx_ring->tx_buf;
355 tx_desc = ICE_TX_DESC(tx_ring, 0);
356 }
357
358 prefetch(tx_desc);
359
360 /* update budget accounting */
361 budget--;
362 } while (likely(budget));
363
364 i += tx_ring->count;
365 tx_ring->next_to_clean = i;
366
367 ice_update_tx_ring_stats(tx_ring, total_pkts, total_bytes);
368 netdev_tx_completed_queue(txring_txq(tx_ring), total_pkts, total_bytes);
369
370 #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
371 if (unlikely(total_pkts && netif_carrier_ok(tx_ring->netdev) &&
372 (ICE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
373 /* Make sure that anybody stopping the queue after this
374 * sees the new next_to_clean.
375 */
376 smp_mb();
377 if (netif_tx_queue_stopped(txring_txq(tx_ring)) &&
378 !test_bit(ICE_VSI_DOWN, vsi->state)) {
379 netif_tx_wake_queue(txring_txq(tx_ring));
380 ++tx_ring->ring_stats->tx_stats.restart_q;
381 }
382 }
383
384 return !!budget;
385 }
386
387 /**
388 * ice_alloc_tstamp_ring - allocate the Time Stamp ring
389 * @tx_ring: Tx ring to allocate the Time Stamp ring for
390 *
391 * Return: 0 on success, negative on error
392 */
ice_alloc_tstamp_ring(struct ice_tx_ring * tx_ring)393 static int ice_alloc_tstamp_ring(struct ice_tx_ring *tx_ring)
394 {
395 struct ice_tstamp_ring *tstamp_ring;
396
397 /* allocate with kzalloc(), free with kfree_rcu() */
398 tstamp_ring = kzalloc(sizeof(*tstamp_ring), GFP_KERNEL);
399 if (!tstamp_ring)
400 return -ENOMEM;
401
402 tstamp_ring->tx_ring = tx_ring;
403 tx_ring->tstamp_ring = tstamp_ring;
404 tstamp_ring->desc = NULL;
405 tstamp_ring->count = ice_calc_ts_ring_count(tx_ring);
406 tx_ring->flags |= ICE_TX_FLAGS_TXTIME;
407 return 0;
408 }
409
410 /**
411 * ice_setup_tstamp_ring - allocate the Time Stamp ring
412 * @tx_ring: Tx ring to set up the Time Stamp ring for
413 *
414 * Return: 0 on success, negative on error
415 */
ice_setup_tstamp_ring(struct ice_tx_ring * tx_ring)416 static int ice_setup_tstamp_ring(struct ice_tx_ring *tx_ring)
417 {
418 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring;
419 struct device *dev = tx_ring->dev;
420 u32 size;
421
422 /* round up to nearest page */
423 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc),
424 PAGE_SIZE);
425 tstamp_ring->desc = dmam_alloc_coherent(dev, size, &tstamp_ring->dma,
426 GFP_KERNEL);
427 if (!tstamp_ring->desc) {
428 dev_err(dev, "Unable to allocate memory for Time stamp Ring, size=%d\n",
429 size);
430 return -ENOMEM;
431 }
432
433 tstamp_ring->next_to_use = 0;
434 return 0;
435 }
436
437 /**
438 * ice_alloc_setup_tstamp_ring - Allocate and setup the Time Stamp ring
439 * @tx_ring: Tx ring to allocate and setup the Time Stamp ring for
440 *
441 * Return: 0 on success, negative on error
442 */
ice_alloc_setup_tstamp_ring(struct ice_tx_ring * tx_ring)443 int ice_alloc_setup_tstamp_ring(struct ice_tx_ring *tx_ring)
444 {
445 struct device *dev = tx_ring->dev;
446 int err;
447
448 err = ice_alloc_tstamp_ring(tx_ring);
449 if (err) {
450 dev_err(dev, "Unable to allocate Time stamp ring for Tx ring %d\n",
451 tx_ring->q_index);
452 return err;
453 }
454
455 err = ice_setup_tstamp_ring(tx_ring);
456 if (err) {
457 dev_err(dev, "Unable to setup Time stamp ring for Tx ring %d\n",
458 tx_ring->q_index);
459 ice_free_tx_tstamp_ring(tx_ring);
460 return err;
461 }
462 return 0;
463 }
464
465 /**
466 * ice_setup_tx_ring - Allocate the Tx descriptors
467 * @tx_ring: the Tx ring to set up
468 *
469 * Return 0 on success, negative on error
470 */
ice_setup_tx_ring(struct ice_tx_ring * tx_ring)471 int ice_setup_tx_ring(struct ice_tx_ring *tx_ring)
472 {
473 struct device *dev = tx_ring->dev;
474 u32 size;
475
476 if (!dev)
477 return -ENOMEM;
478
479 /* warn if we are about to overwrite the pointer */
480 WARN_ON(tx_ring->tx_buf);
481 tx_ring->tx_buf =
482 devm_kcalloc(dev, sizeof(*tx_ring->tx_buf), tx_ring->count,
483 GFP_KERNEL);
484 if (!tx_ring->tx_buf)
485 return -ENOMEM;
486
487 /* round up to nearest page */
488 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
489 PAGE_SIZE);
490 tx_ring->desc = dmam_alloc_coherent(dev, size, &tx_ring->dma,
491 GFP_KERNEL);
492 if (!tx_ring->desc) {
493 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
494 size);
495 goto err;
496 }
497
498 tx_ring->next_to_use = 0;
499 tx_ring->next_to_clean = 0;
500 tx_ring->ring_stats->tx_stats.prev_pkt = -1;
501 return 0;
502
503 err:
504 devm_kfree(dev, tx_ring->tx_buf);
505 tx_ring->tx_buf = NULL;
506 return -ENOMEM;
507 }
508
509 /**
510 * ice_clean_rx_ring - Free Rx buffers
511 * @rx_ring: ring to be cleaned
512 */
ice_clean_rx_ring(struct ice_rx_ring * rx_ring)513 void ice_clean_rx_ring(struct ice_rx_ring *rx_ring)
514 {
515 struct xdp_buff *xdp = &rx_ring->xdp;
516 struct device *dev = rx_ring->dev;
517 u32 size;
518 u16 i;
519
520 /* ring already cleared, nothing to do */
521 if (!rx_ring->rx_buf)
522 return;
523
524 if (rx_ring->xsk_pool) {
525 ice_xsk_clean_rx_ring(rx_ring);
526 goto rx_skip_free;
527 }
528
529 if (xdp->data) {
530 xdp_return_buff(xdp);
531 xdp->data = NULL;
532 }
533
534 /* Free all the Rx ring sk_buffs */
535 for (i = 0; i < rx_ring->count; i++) {
536 struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
537
538 if (!rx_buf->page)
539 continue;
540
541 /* Invalidate cache lines that may have been written to by
542 * device so that we avoid corrupting memory.
543 */
544 dma_sync_single_range_for_cpu(dev, rx_buf->dma,
545 rx_buf->page_offset,
546 rx_ring->rx_buf_len,
547 DMA_FROM_DEVICE);
548
549 /* free resources associated with mapping */
550 dma_unmap_page_attrs(dev, rx_buf->dma, ice_rx_pg_size(rx_ring),
551 DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
552 __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
553
554 rx_buf->page = NULL;
555 rx_buf->page_offset = 0;
556 }
557
558 rx_skip_free:
559 if (rx_ring->xsk_pool)
560 memset(rx_ring->xdp_buf, 0, array_size(rx_ring->count, sizeof(*rx_ring->xdp_buf)));
561 else
562 memset(rx_ring->rx_buf, 0, array_size(rx_ring->count, sizeof(*rx_ring->rx_buf)));
563
564 /* Zero out the descriptor ring */
565 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
566 PAGE_SIZE);
567 memset(rx_ring->desc, 0, size);
568
569 rx_ring->next_to_alloc = 0;
570 rx_ring->next_to_clean = 0;
571 rx_ring->first_desc = 0;
572 rx_ring->next_to_use = 0;
573 }
574
575 /**
576 * ice_free_rx_ring - Free Rx resources
577 * @rx_ring: ring to clean the resources from
578 *
579 * Free all receive software resources
580 */
ice_free_rx_ring(struct ice_rx_ring * rx_ring)581 void ice_free_rx_ring(struct ice_rx_ring *rx_ring)
582 {
583 u32 size;
584
585 ice_clean_rx_ring(rx_ring);
586 if (rx_ring->vsi->type == ICE_VSI_PF)
587 if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
588 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
589 WRITE_ONCE(rx_ring->xdp_prog, NULL);
590 if (rx_ring->xsk_pool) {
591 kfree(rx_ring->xdp_buf);
592 rx_ring->xdp_buf = NULL;
593 } else {
594 kfree(rx_ring->rx_buf);
595 rx_ring->rx_buf = NULL;
596 }
597
598 if (rx_ring->desc) {
599 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
600 PAGE_SIZE);
601 dmam_free_coherent(rx_ring->dev, size,
602 rx_ring->desc, rx_ring->dma);
603 rx_ring->desc = NULL;
604 }
605 }
606
607 /**
608 * ice_setup_rx_ring - Allocate the Rx descriptors
609 * @rx_ring: the Rx ring to set up
610 *
611 * Return 0 on success, negative on error
612 */
ice_setup_rx_ring(struct ice_rx_ring * rx_ring)613 int ice_setup_rx_ring(struct ice_rx_ring *rx_ring)
614 {
615 struct device *dev = rx_ring->dev;
616 u32 size;
617
618 if (!dev)
619 return -ENOMEM;
620
621 /* warn if we are about to overwrite the pointer */
622 WARN_ON(rx_ring->rx_buf);
623 rx_ring->rx_buf =
624 kcalloc(rx_ring->count, sizeof(*rx_ring->rx_buf), GFP_KERNEL);
625 if (!rx_ring->rx_buf)
626 return -ENOMEM;
627
628 /* round up to nearest page */
629 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
630 PAGE_SIZE);
631 rx_ring->desc = dmam_alloc_coherent(dev, size, &rx_ring->dma,
632 GFP_KERNEL);
633 if (!rx_ring->desc) {
634 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
635 size);
636 goto err;
637 }
638
639 rx_ring->next_to_use = 0;
640 rx_ring->next_to_clean = 0;
641 rx_ring->first_desc = 0;
642
643 if (ice_is_xdp_ena_vsi(rx_ring->vsi))
644 WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog);
645
646 return 0;
647
648 err:
649 kfree(rx_ring->rx_buf);
650 rx_ring->rx_buf = NULL;
651 return -ENOMEM;
652 }
653
654 /**
655 * ice_run_xdp - Executes an XDP program on initialized xdp_buff
656 * @rx_ring: Rx ring
657 * @xdp: xdp_buff used as input to the XDP program
658 * @xdp_prog: XDP program to run
659 * @xdp_ring: ring to be used for XDP_TX action
660 * @eop_desc: Last descriptor in packet to read metadata from
661 *
662 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
663 */
664 static u32
ice_run_xdp(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp,struct bpf_prog * xdp_prog,struct ice_tx_ring * xdp_ring,union ice_32b_rx_flex_desc * eop_desc)665 ice_run_xdp(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
666 struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring,
667 union ice_32b_rx_flex_desc *eop_desc)
668 {
669 unsigned int ret = ICE_XDP_PASS;
670 u32 act;
671
672 if (!xdp_prog)
673 goto exit;
674
675 ice_xdp_meta_set_desc(xdp, eop_desc);
676
677 act = bpf_prog_run_xdp(xdp_prog, xdp);
678 switch (act) {
679 case XDP_PASS:
680 break;
681 case XDP_TX:
682 if (static_branch_unlikely(&ice_xdp_locking_key))
683 spin_lock(&xdp_ring->tx_lock);
684 ret = __ice_xmit_xdp_ring(xdp, xdp_ring, false);
685 if (static_branch_unlikely(&ice_xdp_locking_key))
686 spin_unlock(&xdp_ring->tx_lock);
687 if (ret == ICE_XDP_CONSUMED)
688 goto out_failure;
689 break;
690 case XDP_REDIRECT:
691 if (xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog))
692 goto out_failure;
693 ret = ICE_XDP_REDIR;
694 break;
695 default:
696 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
697 fallthrough;
698 case XDP_ABORTED:
699 out_failure:
700 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
701 fallthrough;
702 case XDP_DROP:
703 ret = ICE_XDP_CONSUMED;
704 }
705 exit:
706 return ret;
707 }
708
709 /**
710 * ice_xmit_xdp_ring - submit frame to XDP ring for transmission
711 * @xdpf: XDP frame that will be converted to XDP buff
712 * @xdp_ring: XDP ring for transmission
713 */
ice_xmit_xdp_ring(const struct xdp_frame * xdpf,struct ice_tx_ring * xdp_ring)714 static int ice_xmit_xdp_ring(const struct xdp_frame *xdpf,
715 struct ice_tx_ring *xdp_ring)
716 {
717 struct xdp_buff xdp;
718
719 xdp.data_hard_start = (void *)xdpf;
720 xdp.data = xdpf->data;
721 xdp.data_end = xdp.data + xdpf->len;
722 xdp.frame_sz = xdpf->frame_sz;
723 xdp.flags = xdpf->flags;
724
725 return __ice_xmit_xdp_ring(&xdp, xdp_ring, true);
726 }
727
728 /**
729 * ice_xdp_xmit - submit packets to XDP ring for transmission
730 * @dev: netdev
731 * @n: number of XDP frames to be transmitted
732 * @frames: XDP frames to be transmitted
733 * @flags: transmit flags
734 *
735 * Returns number of frames successfully sent. Failed frames
736 * will be free'ed by XDP core.
737 * For error cases, a negative errno code is returned and no-frames
738 * are transmitted (caller must handle freeing frames).
739 */
740 int
ice_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)741 ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
742 u32 flags)
743 {
744 struct ice_netdev_priv *np = netdev_priv(dev);
745 unsigned int queue_index = smp_processor_id();
746 struct ice_vsi *vsi = np->vsi;
747 struct ice_tx_ring *xdp_ring;
748 struct ice_tx_buf *tx_buf;
749 int nxmit = 0, i;
750
751 if (test_bit(ICE_VSI_DOWN, vsi->state))
752 return -ENETDOWN;
753
754 if (!ice_is_xdp_ena_vsi(vsi))
755 return -ENXIO;
756
757 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
758 return -EINVAL;
759
760 if (static_branch_unlikely(&ice_xdp_locking_key)) {
761 queue_index %= vsi->num_xdp_txq;
762 xdp_ring = vsi->xdp_rings[queue_index];
763 spin_lock(&xdp_ring->tx_lock);
764 } else {
765 /* Generally, should not happen */
766 if (unlikely(queue_index >= vsi->num_xdp_txq))
767 return -ENXIO;
768 xdp_ring = vsi->xdp_rings[queue_index];
769 }
770
771 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use];
772 for (i = 0; i < n; i++) {
773 const struct xdp_frame *xdpf = frames[i];
774 int err;
775
776 err = ice_xmit_xdp_ring(xdpf, xdp_ring);
777 if (err != ICE_XDP_TX)
778 break;
779 nxmit++;
780 }
781
782 tx_buf->rs_idx = ice_set_rs_bit(xdp_ring);
783 if (unlikely(flags & XDP_XMIT_FLUSH))
784 ice_xdp_ring_update_tail(xdp_ring);
785
786 if (static_branch_unlikely(&ice_xdp_locking_key))
787 spin_unlock(&xdp_ring->tx_lock);
788
789 return nxmit;
790 }
791
792 /**
793 * ice_alloc_mapped_page - recycle or make a new page
794 * @rx_ring: ring to use
795 * @bi: rx_buf struct to modify
796 *
797 * Returns true if the page was successfully allocated or
798 * reused.
799 */
800 static bool
ice_alloc_mapped_page(struct ice_rx_ring * rx_ring,struct ice_rx_buf * bi)801 ice_alloc_mapped_page(struct ice_rx_ring *rx_ring, struct ice_rx_buf *bi)
802 {
803 struct page *page = bi->page;
804 dma_addr_t dma;
805
806 /* since we are recycling buffers we should seldom need to alloc */
807 if (likely(page))
808 return true;
809
810 /* alloc new page for storage */
811 page = dev_alloc_pages(ice_rx_pg_order(rx_ring));
812 if (unlikely(!page)) {
813 rx_ring->ring_stats->rx_stats.alloc_page_failed++;
814 return false;
815 }
816
817 /* map page for use */
818 dma = dma_map_page_attrs(rx_ring->dev, page, 0, ice_rx_pg_size(rx_ring),
819 DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
820
821 /* if mapping failed free memory back to system since
822 * there isn't much point in holding memory we can't use
823 */
824 if (dma_mapping_error(rx_ring->dev, dma)) {
825 __free_pages(page, ice_rx_pg_order(rx_ring));
826 rx_ring->ring_stats->rx_stats.alloc_page_failed++;
827 return false;
828 }
829
830 bi->dma = dma;
831 bi->page = page;
832 bi->page_offset = rx_ring->rx_offset;
833 page_ref_add(page, USHRT_MAX - 1);
834 bi->pagecnt_bias = USHRT_MAX;
835
836 return true;
837 }
838
839 /**
840 * ice_init_ctrl_rx_descs - Initialize Rx descriptors for control vsi.
841 * @rx_ring: ring to init descriptors on
842 * @count: number of descriptors to initialize
843 */
ice_init_ctrl_rx_descs(struct ice_rx_ring * rx_ring,u32 count)844 void ice_init_ctrl_rx_descs(struct ice_rx_ring *rx_ring, u32 count)
845 {
846 union ice_32b_rx_flex_desc *rx_desc;
847 u32 ntu = rx_ring->next_to_use;
848
849 if (!count)
850 return;
851
852 rx_desc = ICE_RX_DESC(rx_ring, ntu);
853
854 do {
855 rx_desc++;
856 ntu++;
857 if (unlikely(ntu == rx_ring->count)) {
858 rx_desc = ICE_RX_DESC(rx_ring, 0);
859 ntu = 0;
860 }
861
862 rx_desc->wb.status_error0 = 0;
863 count--;
864 } while (count);
865
866 if (rx_ring->next_to_use != ntu)
867 ice_release_rx_desc(rx_ring, ntu);
868 }
869
870 /**
871 * ice_alloc_rx_bufs - Replace used receive buffers
872 * @rx_ring: ring to place buffers on
873 * @cleaned_count: number of buffers to replace
874 *
875 * Returns false if all allocations were successful, true if any fail. Returning
876 * true signals to the caller that we didn't replace cleaned_count buffers and
877 * there is more work to do.
878 *
879 * First, try to clean "cleaned_count" Rx buffers. Then refill the cleaned Rx
880 * buffers. Then bump tail at most one time. Grouping like this lets us avoid
881 * multiple tail writes per call.
882 */
ice_alloc_rx_bufs(struct ice_rx_ring * rx_ring,unsigned int cleaned_count)883 bool ice_alloc_rx_bufs(struct ice_rx_ring *rx_ring, unsigned int cleaned_count)
884 {
885 union ice_32b_rx_flex_desc *rx_desc;
886 u16 ntu = rx_ring->next_to_use;
887 struct ice_rx_buf *bi;
888
889 /* do nothing if no valid netdev defined */
890 if (!rx_ring->netdev || !cleaned_count)
891 return false;
892
893 /* get the Rx descriptor and buffer based on next_to_use */
894 rx_desc = ICE_RX_DESC(rx_ring, ntu);
895 bi = &rx_ring->rx_buf[ntu];
896
897 do {
898 /* if we fail here, we have work remaining */
899 if (!ice_alloc_mapped_page(rx_ring, bi))
900 break;
901
902 /* sync the buffer for use by the device */
903 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
904 bi->page_offset,
905 rx_ring->rx_buf_len,
906 DMA_FROM_DEVICE);
907
908 /* Refresh the desc even if buffer_addrs didn't change
909 * because each write-back erases this info.
910 */
911 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
912
913 rx_desc++;
914 bi++;
915 ntu++;
916 if (unlikely(ntu == rx_ring->count)) {
917 rx_desc = ICE_RX_DESC(rx_ring, 0);
918 bi = rx_ring->rx_buf;
919 ntu = 0;
920 }
921
922 /* clear the status bits for the next_to_use descriptor */
923 rx_desc->wb.status_error0 = 0;
924
925 cleaned_count--;
926 } while (cleaned_count);
927
928 if (rx_ring->next_to_use != ntu)
929 ice_release_rx_desc(rx_ring, ntu);
930
931 return !!cleaned_count;
932 }
933
934 /**
935 * ice_rx_buf_adjust_pg_offset - Prepare Rx buffer for reuse
936 * @rx_buf: Rx buffer to adjust
937 * @size: Size of adjustment
938 *
939 * Update the offset within page so that Rx buf will be ready to be reused.
940 * For systems with PAGE_SIZE < 8192 this function will flip the page offset
941 * so the second half of page assigned to Rx buffer will be used, otherwise
942 * the offset is moved by "size" bytes
943 */
944 static void
ice_rx_buf_adjust_pg_offset(struct ice_rx_buf * rx_buf,unsigned int size)945 ice_rx_buf_adjust_pg_offset(struct ice_rx_buf *rx_buf, unsigned int size)
946 {
947 #if (PAGE_SIZE < 8192)
948 /* flip page offset to other buffer */
949 rx_buf->page_offset ^= size;
950 #else
951 /* move offset up to the next cache line */
952 rx_buf->page_offset += size;
953 #endif
954 }
955
956 /**
957 * ice_can_reuse_rx_page - Determine if page can be reused for another Rx
958 * @rx_buf: buffer containing the page
959 *
960 * If page is reusable, we have a green light for calling ice_reuse_rx_page,
961 * which will assign the current buffer to the buffer that next_to_alloc is
962 * pointing to; otherwise, the DMA mapping needs to be destroyed and
963 * page freed
964 */
965 static bool
ice_can_reuse_rx_page(struct ice_rx_buf * rx_buf)966 ice_can_reuse_rx_page(struct ice_rx_buf *rx_buf)
967 {
968 unsigned int pagecnt_bias = rx_buf->pagecnt_bias;
969 struct page *page = rx_buf->page;
970
971 /* avoid re-using remote and pfmemalloc pages */
972 if (!dev_page_is_reusable(page))
973 return false;
974
975 /* if we are only owner of page we can reuse it */
976 if (unlikely(rx_buf->pgcnt - pagecnt_bias > 1))
977 return false;
978 #if (PAGE_SIZE >= 8192)
979 #define ICE_LAST_OFFSET \
980 (SKB_WITH_OVERHEAD(PAGE_SIZE) - ICE_RXBUF_3072)
981 if (rx_buf->page_offset > ICE_LAST_OFFSET)
982 return false;
983 #endif /* PAGE_SIZE >= 8192) */
984
985 /* If we have drained the page fragment pool we need to update
986 * the pagecnt_bias and page count so that we fully restock the
987 * number of references the driver holds.
988 */
989 if (unlikely(pagecnt_bias == 1)) {
990 page_ref_add(page, USHRT_MAX - 1);
991 rx_buf->pagecnt_bias = USHRT_MAX;
992 }
993
994 return true;
995 }
996
997 /**
998 * ice_add_xdp_frag - Add contents of Rx buffer to xdp buf as a frag
999 * @rx_ring: Rx descriptor ring to transact packets on
1000 * @xdp: xdp buff to place the data into
1001 * @rx_buf: buffer containing page to add
1002 * @size: packet length from rx_desc
1003 *
1004 * This function will add the data contained in rx_buf->page to the xdp buf.
1005 * It will just attach the page as a frag.
1006 */
1007 static int
ice_add_xdp_frag(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp,struct ice_rx_buf * rx_buf,const unsigned int size)1008 ice_add_xdp_frag(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
1009 struct ice_rx_buf *rx_buf, const unsigned int size)
1010 {
1011 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1012
1013 if (!size)
1014 return 0;
1015
1016 if (!xdp_buff_has_frags(xdp)) {
1017 sinfo->nr_frags = 0;
1018 sinfo->xdp_frags_size = 0;
1019 xdp_buff_set_frags_flag(xdp);
1020 }
1021
1022 if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS))
1023 return -ENOMEM;
1024
1025 __skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++, rx_buf->page,
1026 rx_buf->page_offset, size);
1027 sinfo->xdp_frags_size += size;
1028
1029 if (page_is_pfmemalloc(rx_buf->page))
1030 xdp_buff_set_frag_pfmemalloc(xdp);
1031
1032 return 0;
1033 }
1034
1035 /**
1036 * ice_reuse_rx_page - page flip buffer and store it back on the ring
1037 * @rx_ring: Rx descriptor ring to store buffers on
1038 * @old_buf: donor buffer to have page reused
1039 *
1040 * Synchronizes page for reuse by the adapter
1041 */
1042 static void
ice_reuse_rx_page(struct ice_rx_ring * rx_ring,struct ice_rx_buf * old_buf)1043 ice_reuse_rx_page(struct ice_rx_ring *rx_ring, struct ice_rx_buf *old_buf)
1044 {
1045 u16 nta = rx_ring->next_to_alloc;
1046 struct ice_rx_buf *new_buf;
1047
1048 new_buf = &rx_ring->rx_buf[nta];
1049
1050 /* update, and store next to alloc */
1051 nta++;
1052 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1053
1054 /* Transfer page from old buffer to new buffer.
1055 * Move each member individually to avoid possible store
1056 * forwarding stalls and unnecessary copy of skb.
1057 */
1058 new_buf->dma = old_buf->dma;
1059 new_buf->page = old_buf->page;
1060 new_buf->page_offset = old_buf->page_offset;
1061 new_buf->pagecnt_bias = old_buf->pagecnt_bias;
1062 }
1063
1064 /**
1065 * ice_get_rx_buf - Fetch Rx buffer and synchronize data for use
1066 * @rx_ring: Rx descriptor ring to transact packets on
1067 * @size: size of buffer to add to skb
1068 * @ntc: index of next to clean element
1069 *
1070 * This function will pull an Rx buffer from the ring and synchronize it
1071 * for use by the CPU.
1072 */
1073 static struct ice_rx_buf *
ice_get_rx_buf(struct ice_rx_ring * rx_ring,const unsigned int size,const unsigned int ntc)1074 ice_get_rx_buf(struct ice_rx_ring *rx_ring, const unsigned int size,
1075 const unsigned int ntc)
1076 {
1077 struct ice_rx_buf *rx_buf;
1078
1079 rx_buf = &rx_ring->rx_buf[ntc];
1080 prefetchw(rx_buf->page);
1081
1082 if (!size)
1083 return rx_buf;
1084 /* we are reusing so sync this buffer for CPU use */
1085 dma_sync_single_range_for_cpu(rx_ring->dev, rx_buf->dma,
1086 rx_buf->page_offset, size,
1087 DMA_FROM_DEVICE);
1088
1089 /* We have pulled a buffer for use, so decrement pagecnt_bias */
1090 rx_buf->pagecnt_bias--;
1091
1092 return rx_buf;
1093 }
1094
1095 /**
1096 * ice_get_pgcnts - grab page_count() for gathered fragments
1097 * @rx_ring: Rx descriptor ring to store the page counts on
1098 * @ntc: the next to clean element (not included in this frame!)
1099 *
1100 * This function is intended to be called right before running XDP
1101 * program so that the page recycling mechanism will be able to take
1102 * a correct decision regarding underlying pages; this is done in such
1103 * way as XDP program can change the refcount of page
1104 */
ice_get_pgcnts(struct ice_rx_ring * rx_ring,unsigned int ntc)1105 static void ice_get_pgcnts(struct ice_rx_ring *rx_ring, unsigned int ntc)
1106 {
1107 u32 idx = rx_ring->first_desc;
1108 struct ice_rx_buf *rx_buf;
1109 u32 cnt = rx_ring->count;
1110
1111 while (idx != ntc) {
1112 rx_buf = &rx_ring->rx_buf[idx];
1113 rx_buf->pgcnt = page_count(rx_buf->page);
1114
1115 if (++idx == cnt)
1116 idx = 0;
1117 }
1118 }
1119
1120 /**
1121 * ice_build_skb - Build skb around an existing buffer
1122 * @rx_ring: Rx descriptor ring to transact packets on
1123 * @xdp: xdp_buff pointing to the data
1124 *
1125 * This function builds an skb around an existing XDP buffer, taking care
1126 * to set up the skb correctly and avoid any memcpy overhead. Driver has
1127 * already combined frags (if any) to skb_shared_info.
1128 */
1129 static struct sk_buff *
ice_build_skb(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp)1130 ice_build_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
1131 {
1132 u8 metasize = xdp->data - xdp->data_meta;
1133 struct skb_shared_info *sinfo = NULL;
1134 unsigned int nr_frags;
1135 struct sk_buff *skb;
1136
1137 if (unlikely(xdp_buff_has_frags(xdp))) {
1138 sinfo = xdp_get_shared_info_from_buff(xdp);
1139 nr_frags = sinfo->nr_frags;
1140 }
1141
1142 /* Prefetch first cache line of first page. If xdp->data_meta
1143 * is unused, this points exactly as xdp->data, otherwise we
1144 * likely have a consumer accessing first few bytes of meta
1145 * data, and then actual data.
1146 */
1147 net_prefetch(xdp->data_meta);
1148 /* build an skb around the page buffer */
1149 skb = napi_build_skb(xdp->data_hard_start, xdp->frame_sz);
1150 if (unlikely(!skb))
1151 return NULL;
1152
1153 /* must to record Rx queue, otherwise OS features such as
1154 * symmetric queue won't work
1155 */
1156 skb_record_rx_queue(skb, rx_ring->q_index);
1157
1158 /* update pointers within the skb to store the data */
1159 skb_reserve(skb, xdp->data - xdp->data_hard_start);
1160 __skb_put(skb, xdp->data_end - xdp->data);
1161 if (metasize)
1162 skb_metadata_set(skb, metasize);
1163
1164 if (unlikely(xdp_buff_has_frags(xdp)))
1165 xdp_update_skb_frags_info(skb, nr_frags, sinfo->xdp_frags_size,
1166 nr_frags * xdp->frame_sz,
1167 xdp_buff_get_skb_flags(xdp));
1168
1169 return skb;
1170 }
1171
1172 /**
1173 * ice_construct_skb - Allocate skb and populate it
1174 * @rx_ring: Rx descriptor ring to transact packets on
1175 * @xdp: xdp_buff pointing to the data
1176 *
1177 * This function allocates an skb. It then populates it with the page
1178 * data from the current receive descriptor, taking care to set up the
1179 * skb correctly.
1180 */
1181 static struct sk_buff *
ice_construct_skb(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp)1182 ice_construct_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
1183 {
1184 unsigned int size = xdp->data_end - xdp->data;
1185 struct skb_shared_info *sinfo = NULL;
1186 struct ice_rx_buf *rx_buf;
1187 unsigned int nr_frags = 0;
1188 unsigned int headlen;
1189 struct sk_buff *skb;
1190
1191 /* prefetch first cache line of first page */
1192 net_prefetch(xdp->data);
1193
1194 if (unlikely(xdp_buff_has_frags(xdp))) {
1195 sinfo = xdp_get_shared_info_from_buff(xdp);
1196 nr_frags = sinfo->nr_frags;
1197 }
1198
1199 /* allocate a skb to store the frags */
1200 skb = napi_alloc_skb(&rx_ring->q_vector->napi, ICE_RX_HDR_SIZE);
1201 if (unlikely(!skb))
1202 return NULL;
1203
1204 rx_buf = &rx_ring->rx_buf[rx_ring->first_desc];
1205 skb_record_rx_queue(skb, rx_ring->q_index);
1206 /* Determine available headroom for copy */
1207 headlen = size;
1208 if (headlen > ICE_RX_HDR_SIZE)
1209 headlen = eth_get_headlen(skb->dev, xdp->data, ICE_RX_HDR_SIZE);
1210
1211 /* align pull length to size of long to optimize memcpy performance */
1212 memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen,
1213 sizeof(long)));
1214
1215 /* if we exhaust the linear part then add what is left as a frag */
1216 size -= headlen;
1217 if (size) {
1218 /* besides adding here a partial frag, we are going to add
1219 * frags from xdp_buff, make sure there is enough space for
1220 * them
1221 */
1222 if (unlikely(nr_frags >= MAX_SKB_FRAGS - 1)) {
1223 dev_kfree_skb(skb);
1224 return NULL;
1225 }
1226 skb_add_rx_frag(skb, 0, rx_buf->page,
1227 rx_buf->page_offset + headlen, size,
1228 xdp->frame_sz);
1229 } else {
1230 /* buffer is unused, restore biased page count in Rx buffer;
1231 * data was copied onto skb's linear part so there's no
1232 * need for adjusting page offset and we can reuse this buffer
1233 * as-is
1234 */
1235 rx_buf->pagecnt_bias++;
1236 }
1237
1238 if (unlikely(xdp_buff_has_frags(xdp))) {
1239 struct skb_shared_info *skinfo = skb_shinfo(skb);
1240
1241 memcpy(&skinfo->frags[skinfo->nr_frags], &sinfo->frags[0],
1242 sizeof(skb_frag_t) * nr_frags);
1243
1244 xdp_update_skb_frags_info(skb, skinfo->nr_frags + nr_frags,
1245 sinfo->xdp_frags_size,
1246 nr_frags * xdp->frame_sz,
1247 xdp_buff_get_skb_flags(xdp));
1248 }
1249
1250 return skb;
1251 }
1252
1253 /**
1254 * ice_put_rx_buf - Clean up used buffer and either recycle or free
1255 * @rx_ring: Rx descriptor ring to transact packets on
1256 * @rx_buf: Rx buffer to pull data from
1257 *
1258 * This function will clean up the contents of the rx_buf. It will either
1259 * recycle the buffer or unmap it and free the associated resources.
1260 */
1261 static void
ice_put_rx_buf(struct ice_rx_ring * rx_ring,struct ice_rx_buf * rx_buf)1262 ice_put_rx_buf(struct ice_rx_ring *rx_ring, struct ice_rx_buf *rx_buf)
1263 {
1264 if (!rx_buf)
1265 return;
1266
1267 if (ice_can_reuse_rx_page(rx_buf)) {
1268 /* hand second half of page back to the ring */
1269 ice_reuse_rx_page(rx_ring, rx_buf);
1270 } else {
1271 /* we are not reusing the buffer so unmap it */
1272 dma_unmap_page_attrs(rx_ring->dev, rx_buf->dma,
1273 ice_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1274 ICE_RX_DMA_ATTR);
1275 __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
1276 }
1277
1278 /* clear contents of buffer_info */
1279 rx_buf->page = NULL;
1280 }
1281
1282 /**
1283 * ice_put_rx_mbuf - ice_put_rx_buf() caller, for all buffers in frame
1284 * @rx_ring: Rx ring with all the auxiliary data
1285 * @xdp: XDP buffer carrying linear + frags part
1286 * @ntc: the next to clean element (not included in this frame!)
1287 * @verdict: return code from XDP program execution
1288 *
1289 * Called after XDP program is completed, or on error with verdict set to
1290 * ICE_XDP_CONSUMED.
1291 *
1292 * Walk through buffers from first_desc to the end of the frame, releasing
1293 * buffers and satisfying internal page recycle mechanism. The action depends
1294 * on verdict from XDP program.
1295 */
ice_put_rx_mbuf(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp,u32 ntc,u32 verdict)1296 static void ice_put_rx_mbuf(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
1297 u32 ntc, u32 verdict)
1298 {
1299 u32 idx = rx_ring->first_desc;
1300 u32 cnt = rx_ring->count;
1301 struct ice_rx_buf *buf;
1302 u32 xdp_frags = 0;
1303 int i = 0;
1304
1305 if (unlikely(xdp_buff_has_frags(xdp)))
1306 xdp_frags = xdp_get_shared_info_from_buff(xdp)->nr_frags;
1307
1308 while (idx != ntc) {
1309 buf = &rx_ring->rx_buf[idx];
1310 if (++idx == cnt)
1311 idx = 0;
1312
1313 /* An XDP program could release fragments from the end of the
1314 * buffer. For these, we need to keep the pagecnt_bias as-is.
1315 * To do this, only adjust pagecnt_bias for fragments up to
1316 * the total remaining after the XDP program has run.
1317 */
1318 if (verdict != ICE_XDP_CONSUMED)
1319 ice_rx_buf_adjust_pg_offset(buf, xdp->frame_sz);
1320 else if (i++ <= xdp_frags)
1321 buf->pagecnt_bias++;
1322
1323 ice_put_rx_buf(rx_ring, buf);
1324 }
1325
1326 xdp->data = NULL;
1327 rx_ring->first_desc = ntc;
1328 }
1329
1330 /**
1331 * ice_clean_ctrl_rx_irq - Clean descriptors from flow director Rx ring
1332 * @rx_ring: Rx descriptor ring for ctrl_vsi to transact packets on
1333 *
1334 * This function cleans Rx descriptors from the ctrl_vsi Rx ring used
1335 * to set flow director rules on VFs.
1336 */
ice_clean_ctrl_rx_irq(struct ice_rx_ring * rx_ring)1337 void ice_clean_ctrl_rx_irq(struct ice_rx_ring *rx_ring)
1338 {
1339 u32 ntc = rx_ring->next_to_clean;
1340 unsigned int total_rx_pkts = 0;
1341 u32 cnt = rx_ring->count;
1342
1343 while (likely(total_rx_pkts < ICE_DFLT_IRQ_WORK)) {
1344 struct ice_vsi *ctrl_vsi = rx_ring->vsi;
1345 union ice_32b_rx_flex_desc *rx_desc;
1346 u16 stat_err_bits;
1347
1348 rx_desc = ICE_RX_DESC(rx_ring, ntc);
1349
1350 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
1351 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits))
1352 break;
1353
1354 dma_rmb();
1355
1356 if (ctrl_vsi->vf)
1357 ice_vc_fdir_irq_handler(ctrl_vsi, rx_desc);
1358
1359 if (++ntc == cnt)
1360 ntc = 0;
1361 total_rx_pkts++;
1362 }
1363
1364 rx_ring->first_desc = ntc;
1365 rx_ring->next_to_clean = ntc;
1366 ice_init_ctrl_rx_descs(rx_ring, ICE_RX_DESC_UNUSED(rx_ring));
1367 }
1368
1369 /**
1370 * ice_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1371 * @rx_ring: Rx descriptor ring to transact packets on
1372 * @budget: Total limit on number of packets to process
1373 *
1374 * This function provides a "bounce buffer" approach to Rx interrupt
1375 * processing. The advantage to this is that on systems that have
1376 * expensive overhead for IOMMU access this provides a means of avoiding
1377 * it by maintaining the mapping of the page to the system.
1378 *
1379 * Returns amount of work completed
1380 */
ice_clean_rx_irq(struct ice_rx_ring * rx_ring,int budget)1381 static int ice_clean_rx_irq(struct ice_rx_ring *rx_ring, int budget)
1382 {
1383 unsigned int total_rx_bytes = 0, total_rx_pkts = 0;
1384 unsigned int offset = rx_ring->rx_offset;
1385 struct xdp_buff *xdp = &rx_ring->xdp;
1386 struct ice_tx_ring *xdp_ring = NULL;
1387 struct bpf_prog *xdp_prog = NULL;
1388 u32 ntc = rx_ring->next_to_clean;
1389 u32 cached_ntu, xdp_verdict;
1390 u32 cnt = rx_ring->count;
1391 u32 xdp_xmit = 0;
1392 bool failure;
1393
1394 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1395 if (xdp_prog) {
1396 xdp_ring = rx_ring->xdp_ring;
1397 cached_ntu = xdp_ring->next_to_use;
1398 }
1399
1400 /* start the loop to process Rx packets bounded by 'budget' */
1401 while (likely(total_rx_pkts < (unsigned int)budget)) {
1402 union ice_32b_rx_flex_desc *rx_desc;
1403 struct ice_rx_buf *rx_buf;
1404 struct sk_buff *skb;
1405 unsigned int size;
1406 u16 stat_err_bits;
1407 u16 vlan_tci;
1408
1409 /* get the Rx desc from Rx ring based on 'next_to_clean' */
1410 rx_desc = ICE_RX_DESC(rx_ring, ntc);
1411
1412 /* status_error_len will always be zero for unused descriptors
1413 * because it's cleared in cleanup, and overlaps with hdr_addr
1414 * which is always zero because packet split isn't used, if the
1415 * hardware wrote DD then it will be non-zero
1416 */
1417 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
1418 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits))
1419 break;
1420
1421 /* This memory barrier is needed to keep us from reading
1422 * any other fields out of the rx_desc until we know the
1423 * DD bit is set.
1424 */
1425 dma_rmb();
1426
1427 ice_trace(clean_rx_irq, rx_ring, rx_desc);
1428
1429 size = le16_to_cpu(rx_desc->wb.pkt_len) &
1430 ICE_RX_FLX_DESC_PKT_LEN_M;
1431
1432 /* retrieve a buffer from the ring */
1433 rx_buf = ice_get_rx_buf(rx_ring, size, ntc);
1434
1435 /* Increment ntc before calls to ice_put_rx_mbuf() */
1436 if (++ntc == cnt)
1437 ntc = 0;
1438
1439 if (!xdp->data) {
1440 void *hard_start;
1441
1442 hard_start = page_address(rx_buf->page) + rx_buf->page_offset -
1443 offset;
1444 xdp_prepare_buff(xdp, hard_start, offset, size, !!offset);
1445 xdp_buff_clear_frags_flag(xdp);
1446 } else if (ice_add_xdp_frag(rx_ring, xdp, rx_buf, size)) {
1447 ice_put_rx_mbuf(rx_ring, xdp, ntc, ICE_XDP_CONSUMED);
1448 break;
1449 }
1450
1451 /* skip if it is NOP desc */
1452 if (ice_is_non_eop(rx_ring, rx_desc))
1453 continue;
1454
1455 ice_get_pgcnts(rx_ring, ntc);
1456 xdp_verdict = ice_run_xdp(rx_ring, xdp, xdp_prog, xdp_ring, rx_desc);
1457 if (xdp_verdict == ICE_XDP_PASS)
1458 goto construct_skb;
1459 total_rx_bytes += xdp_get_buff_len(xdp);
1460 total_rx_pkts++;
1461
1462 ice_put_rx_mbuf(rx_ring, xdp, ntc, xdp_verdict);
1463 xdp_xmit |= xdp_verdict & (ICE_XDP_TX | ICE_XDP_REDIR);
1464
1465 continue;
1466 construct_skb:
1467 if (likely(ice_ring_uses_build_skb(rx_ring)))
1468 skb = ice_build_skb(rx_ring, xdp);
1469 else
1470 skb = ice_construct_skb(rx_ring, xdp);
1471 /* exit if we failed to retrieve a buffer */
1472 if (!skb) {
1473 rx_ring->ring_stats->rx_stats.alloc_buf_failed++;
1474 xdp_verdict = ICE_XDP_CONSUMED;
1475 }
1476 ice_put_rx_mbuf(rx_ring, xdp, ntc, xdp_verdict);
1477
1478 if (!skb)
1479 break;
1480
1481 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S);
1482 if (unlikely(ice_test_staterr(rx_desc->wb.status_error0,
1483 stat_err_bits))) {
1484 dev_kfree_skb_any(skb);
1485 continue;
1486 }
1487
1488 vlan_tci = ice_get_vlan_tci(rx_desc);
1489
1490 /* pad the skb if needed, to make a valid ethernet frame */
1491 if (eth_skb_pad(skb))
1492 continue;
1493
1494 /* probably a little skewed due to removing CRC */
1495 total_rx_bytes += skb->len;
1496
1497 /* populate checksum, VLAN, and protocol */
1498 ice_process_skb_fields(rx_ring, rx_desc, skb);
1499
1500 ice_trace(clean_rx_irq_indicate, rx_ring, rx_desc, skb);
1501 /* send completed skb up the stack */
1502 ice_receive_skb(rx_ring, skb, vlan_tci);
1503
1504 /* update budget accounting */
1505 total_rx_pkts++;
1506 }
1507
1508 rx_ring->next_to_clean = ntc;
1509 /* return up to cleaned_count buffers to hardware */
1510 failure = ice_alloc_rx_bufs(rx_ring, ICE_RX_DESC_UNUSED(rx_ring));
1511
1512 if (xdp_xmit)
1513 ice_finalize_xdp_rx(xdp_ring, xdp_xmit, cached_ntu);
1514
1515 if (rx_ring->ring_stats)
1516 ice_update_rx_ring_stats(rx_ring, total_rx_pkts,
1517 total_rx_bytes);
1518
1519 /* guarantee a trip back through this routine if there was a failure */
1520 return failure ? budget : (int)total_rx_pkts;
1521 }
1522
__ice_update_sample(struct ice_q_vector * q_vector,struct ice_ring_container * rc,struct dim_sample * sample,bool is_tx)1523 static void __ice_update_sample(struct ice_q_vector *q_vector,
1524 struct ice_ring_container *rc,
1525 struct dim_sample *sample,
1526 bool is_tx)
1527 {
1528 u64 packets = 0, bytes = 0;
1529
1530 if (is_tx) {
1531 struct ice_tx_ring *tx_ring;
1532
1533 ice_for_each_tx_ring(tx_ring, *rc) {
1534 struct ice_ring_stats *ring_stats;
1535
1536 ring_stats = tx_ring->ring_stats;
1537 if (!ring_stats)
1538 continue;
1539 packets += ring_stats->stats.pkts;
1540 bytes += ring_stats->stats.bytes;
1541 }
1542 } else {
1543 struct ice_rx_ring *rx_ring;
1544
1545 ice_for_each_rx_ring(rx_ring, *rc) {
1546 struct ice_ring_stats *ring_stats;
1547
1548 ring_stats = rx_ring->ring_stats;
1549 if (!ring_stats)
1550 continue;
1551 packets += ring_stats->stats.pkts;
1552 bytes += ring_stats->stats.bytes;
1553 }
1554 }
1555
1556 dim_update_sample(q_vector->total_events, packets, bytes, sample);
1557 sample->comp_ctr = 0;
1558
1559 /* if dim settings get stale, like when not updated for 1
1560 * second or longer, force it to start again. This addresses the
1561 * frequent case of an idle queue being switched to by the
1562 * scheduler. The 1,000 here means 1,000 milliseconds.
1563 */
1564 if (ktime_ms_delta(sample->time, rc->dim.start_sample.time) >= 1000)
1565 rc->dim.state = DIM_START_MEASURE;
1566 }
1567
1568 /**
1569 * ice_net_dim - Update net DIM algorithm
1570 * @q_vector: the vector associated with the interrupt
1571 *
1572 * Create a DIM sample and notify net_dim() so that it can possibly decide
1573 * a new ITR value based on incoming packets, bytes, and interrupts.
1574 *
1575 * This function is a no-op if the ring is not configured to dynamic ITR.
1576 */
ice_net_dim(struct ice_q_vector * q_vector)1577 static void ice_net_dim(struct ice_q_vector *q_vector)
1578 {
1579 struct ice_ring_container *tx = &q_vector->tx;
1580 struct ice_ring_container *rx = &q_vector->rx;
1581
1582 if (ITR_IS_DYNAMIC(tx)) {
1583 struct dim_sample dim_sample;
1584
1585 __ice_update_sample(q_vector, tx, &dim_sample, true);
1586 net_dim(&tx->dim, &dim_sample);
1587 }
1588
1589 if (ITR_IS_DYNAMIC(rx)) {
1590 struct dim_sample dim_sample;
1591
1592 __ice_update_sample(q_vector, rx, &dim_sample, false);
1593 net_dim(&rx->dim, &dim_sample);
1594 }
1595 }
1596
1597 /**
1598 * ice_buildreg_itr - build value for writing to the GLINT_DYN_CTL register
1599 * @itr_idx: interrupt throttling index
1600 * @itr: interrupt throttling value in usecs
1601 */
ice_buildreg_itr(u16 itr_idx,u16 itr)1602 static u32 ice_buildreg_itr(u16 itr_idx, u16 itr)
1603 {
1604 /* The ITR value is reported in microseconds, and the register value is
1605 * recorded in 2 microsecond units. For this reason we only need to
1606 * shift by the GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S to apply this
1607 * granularity as a shift instead of division. The mask makes sure the
1608 * ITR value is never odd so we don't accidentally write into the field
1609 * prior to the ITR field.
1610 */
1611 itr &= ICE_ITR_MASK;
1612
1613 return GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
1614 (itr_idx << GLINT_DYN_CTL_ITR_INDX_S) |
1615 (itr << (GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S));
1616 }
1617
1618 /**
1619 * ice_enable_interrupt - re-enable MSI-X interrupt
1620 * @q_vector: the vector associated with the interrupt to enable
1621 *
1622 * If the VSI is down, the interrupt will not be re-enabled. Also,
1623 * when enabling the interrupt always reset the wb_on_itr to false
1624 * and trigger a software interrupt to clean out internal state.
1625 */
ice_enable_interrupt(struct ice_q_vector * q_vector)1626 static void ice_enable_interrupt(struct ice_q_vector *q_vector)
1627 {
1628 struct ice_vsi *vsi = q_vector->vsi;
1629 bool wb_en = q_vector->wb_on_itr;
1630 u32 itr_val;
1631
1632 if (test_bit(ICE_DOWN, vsi->state))
1633 return;
1634
1635 /* trigger an ITR delayed software interrupt when exiting busy poll, to
1636 * make sure to catch any pending cleanups that might have been missed
1637 * due to interrupt state transition. If busy poll or poll isn't
1638 * enabled, then don't update ITR, and just enable the interrupt.
1639 */
1640 if (!wb_en) {
1641 itr_val = ice_buildreg_itr(ICE_ITR_NONE, 0);
1642 } else {
1643 q_vector->wb_on_itr = false;
1644
1645 /* do two things here with a single write. Set up the third ITR
1646 * index to be used for software interrupt moderation, and then
1647 * trigger a software interrupt with a rate limit of 20K on
1648 * software interrupts, this will help avoid high interrupt
1649 * loads due to frequently polling and exiting polling.
1650 */
1651 itr_val = ice_buildreg_itr(ICE_IDX_ITR2, ICE_ITR_20K);
1652 itr_val |= GLINT_DYN_CTL_SWINT_TRIG_M |
1653 ICE_IDX_ITR2 << GLINT_DYN_CTL_SW_ITR_INDX_S |
1654 GLINT_DYN_CTL_SW_ITR_INDX_ENA_M;
1655 }
1656 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), itr_val);
1657 }
1658
1659 /**
1660 * ice_set_wb_on_itr - set WB_ON_ITR for this q_vector
1661 * @q_vector: q_vector to set WB_ON_ITR on
1662 *
1663 * We need to tell hardware to write-back completed descriptors even when
1664 * interrupts are disabled. Descriptors will be written back on cache line
1665 * boundaries without WB_ON_ITR enabled, but if we don't enable WB_ON_ITR
1666 * descriptors may not be written back if they don't fill a cache line until
1667 * the next interrupt.
1668 *
1669 * This sets the write-back frequency to whatever was set previously for the
1670 * ITR indices. Also, set the INTENA_MSK bit to make sure hardware knows we
1671 * aren't meddling with the INTENA_M bit.
1672 */
ice_set_wb_on_itr(struct ice_q_vector * q_vector)1673 static void ice_set_wb_on_itr(struct ice_q_vector *q_vector)
1674 {
1675 struct ice_vsi *vsi = q_vector->vsi;
1676
1677 /* already in wb_on_itr mode no need to change it */
1678 if (q_vector->wb_on_itr)
1679 return;
1680
1681 /* use previously set ITR values for all of the ITR indices by
1682 * specifying ICE_ITR_NONE, which will vary in adaptive (AIM) mode and
1683 * be static in non-adaptive mode (user configured)
1684 */
1685 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
1686 FIELD_PREP(GLINT_DYN_CTL_ITR_INDX_M, ICE_ITR_NONE) |
1687 FIELD_PREP(GLINT_DYN_CTL_INTENA_MSK_M, 1) |
1688 FIELD_PREP(GLINT_DYN_CTL_WB_ON_ITR_M, 1));
1689
1690 q_vector->wb_on_itr = true;
1691 }
1692
1693 /**
1694 * ice_napi_poll - NAPI polling Rx/Tx cleanup routine
1695 * @napi: napi struct with our devices info in it
1696 * @budget: amount of work driver is allowed to do this pass, in packets
1697 *
1698 * This function will clean all queues associated with a q_vector.
1699 *
1700 * Returns the amount of work done
1701 */
ice_napi_poll(struct napi_struct * napi,int budget)1702 int ice_napi_poll(struct napi_struct *napi, int budget)
1703 {
1704 struct ice_q_vector *q_vector =
1705 container_of(napi, struct ice_q_vector, napi);
1706 struct ice_tx_ring *tx_ring;
1707 struct ice_rx_ring *rx_ring;
1708 bool clean_complete = true;
1709 int budget_per_ring;
1710 int work_done = 0;
1711
1712 /* Since the actual Tx work is minimal, we can give the Tx a larger
1713 * budget and be more aggressive about cleaning up the Tx descriptors.
1714 */
1715 ice_for_each_tx_ring(tx_ring, q_vector->tx) {
1716 struct xsk_buff_pool *xsk_pool = READ_ONCE(tx_ring->xsk_pool);
1717 bool wd;
1718
1719 if (xsk_pool)
1720 wd = ice_xmit_zc(tx_ring, xsk_pool);
1721 else if (ice_ring_is_xdp(tx_ring))
1722 wd = true;
1723 else
1724 wd = ice_clean_tx_irq(tx_ring, budget);
1725
1726 if (!wd)
1727 clean_complete = false;
1728 }
1729
1730 /* Handle case where we are called by netpoll with a budget of 0 */
1731 if (unlikely(budget <= 0))
1732 return budget;
1733
1734 /* normally we have 1 Rx ring per q_vector */
1735 if (unlikely(q_vector->num_ring_rx > 1))
1736 /* We attempt to distribute budget to each Rx queue fairly, but
1737 * don't allow the budget to go below 1 because that would exit
1738 * polling early.
1739 */
1740 budget_per_ring = max_t(int, budget / q_vector->num_ring_rx, 1);
1741 else
1742 /* Max of 1 Rx ring in this q_vector so give it the budget */
1743 budget_per_ring = budget;
1744
1745 ice_for_each_rx_ring(rx_ring, q_vector->rx) {
1746 struct xsk_buff_pool *xsk_pool = READ_ONCE(rx_ring->xsk_pool);
1747 int cleaned;
1748
1749 /* A dedicated path for zero-copy allows making a single
1750 * comparison in the irq context instead of many inside the
1751 * ice_clean_rx_irq function and makes the codebase cleaner.
1752 */
1753 cleaned = rx_ring->xsk_pool ?
1754 ice_clean_rx_irq_zc(rx_ring, xsk_pool, budget_per_ring) :
1755 ice_clean_rx_irq(rx_ring, budget_per_ring);
1756 work_done += cleaned;
1757 /* if we clean as many as budgeted, we must not be done */
1758 if (cleaned >= budget_per_ring)
1759 clean_complete = false;
1760 }
1761
1762 /* If work not completed, return budget and polling will return */
1763 if (!clean_complete) {
1764 /* Set the writeback on ITR so partial completions of
1765 * cache-lines will still continue even if we're polling.
1766 */
1767 ice_set_wb_on_itr(q_vector);
1768 return budget;
1769 }
1770
1771 /* Exit the polling mode, but don't re-enable interrupts if stack might
1772 * poll us due to busy-polling
1773 */
1774 if (napi_complete_done(napi, work_done)) {
1775 ice_net_dim(q_vector);
1776 ice_enable_interrupt(q_vector);
1777 } else {
1778 ice_set_wb_on_itr(q_vector);
1779 }
1780
1781 return min_t(int, work_done, budget - 1);
1782 }
1783
1784 /**
1785 * __ice_maybe_stop_tx - 2nd level check for Tx stop conditions
1786 * @tx_ring: the ring to be checked
1787 * @size: the size buffer we want to assure is available
1788 *
1789 * Returns -EBUSY if a stop is needed, else 0
1790 */
__ice_maybe_stop_tx(struct ice_tx_ring * tx_ring,unsigned int size)1791 static int __ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size)
1792 {
1793 netif_tx_stop_queue(txring_txq(tx_ring));
1794 /* Memory barrier before checking head and tail */
1795 smp_mb();
1796
1797 /* Check again in a case another CPU has just made room available. */
1798 if (likely(ICE_DESC_UNUSED(tx_ring) < size))
1799 return -EBUSY;
1800
1801 /* A reprieve! - use start_queue because it doesn't call schedule */
1802 netif_tx_start_queue(txring_txq(tx_ring));
1803 ++tx_ring->ring_stats->tx_stats.restart_q;
1804 return 0;
1805 }
1806
1807 /**
1808 * ice_maybe_stop_tx - 1st level check for Tx stop conditions
1809 * @tx_ring: the ring to be checked
1810 * @size: the size buffer we want to assure is available
1811 *
1812 * Returns 0 if stop is not needed
1813 */
ice_maybe_stop_tx(struct ice_tx_ring * tx_ring,unsigned int size)1814 static int ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size)
1815 {
1816 if (likely(ICE_DESC_UNUSED(tx_ring) >= size))
1817 return 0;
1818
1819 return __ice_maybe_stop_tx(tx_ring, size);
1820 }
1821
1822 /**
1823 * ice_tx_map - Build the Tx descriptor
1824 * @tx_ring: ring to send buffer on
1825 * @first: first buffer info buffer to use
1826 * @off: pointer to struct that holds offload parameters
1827 *
1828 * This function loops over the skb data pointed to by *first
1829 * and gets a physical address for each memory location and programs
1830 * it and the length into the transmit descriptor.
1831 */
1832 static void
ice_tx_map(struct ice_tx_ring * tx_ring,struct ice_tx_buf * first,struct ice_tx_offload_params * off)1833 ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first,
1834 struct ice_tx_offload_params *off)
1835 {
1836 u64 td_offset, td_tag, td_cmd;
1837 u16 i = tx_ring->next_to_use;
1838 unsigned int data_len, size;
1839 struct ice_tx_desc *tx_desc;
1840 struct ice_tx_buf *tx_buf;
1841 struct sk_buff *skb;
1842 skb_frag_t *frag;
1843 dma_addr_t dma;
1844 bool kick;
1845
1846 td_tag = off->td_l2tag1;
1847 td_cmd = off->td_cmd;
1848 td_offset = off->td_offset;
1849 skb = first->skb;
1850
1851 data_len = skb->data_len;
1852 size = skb_headlen(skb);
1853
1854 tx_desc = ICE_TX_DESC(tx_ring, i);
1855
1856 if (first->tx_flags & ICE_TX_FLAGS_HW_VLAN) {
1857 td_cmd |= (u64)ICE_TX_DESC_CMD_IL2TAG1;
1858 td_tag = first->vid;
1859 }
1860
1861 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1862
1863 tx_buf = first;
1864
1865 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1866 unsigned int max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1867
1868 if (dma_mapping_error(tx_ring->dev, dma))
1869 goto dma_error;
1870
1871 /* record length, and DMA address */
1872 dma_unmap_len_set(tx_buf, len, size);
1873 dma_unmap_addr_set(tx_buf, dma, dma);
1874
1875 /* align size to end of page */
1876 max_data += -dma & (ICE_MAX_READ_REQ_SIZE - 1);
1877 tx_desc->buf_addr = cpu_to_le64(dma);
1878
1879 /* account for data chunks larger than the hardware
1880 * can handle
1881 */
1882 while (unlikely(size > ICE_MAX_DATA_PER_TXD)) {
1883 tx_desc->cmd_type_offset_bsz =
1884 ice_build_ctob(td_cmd, td_offset, max_data,
1885 td_tag);
1886
1887 tx_desc++;
1888 i++;
1889
1890 if (i == tx_ring->count) {
1891 tx_desc = ICE_TX_DESC(tx_ring, 0);
1892 i = 0;
1893 }
1894
1895 dma += max_data;
1896 size -= max_data;
1897
1898 max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1899 tx_desc->buf_addr = cpu_to_le64(dma);
1900 }
1901
1902 if (likely(!data_len))
1903 break;
1904
1905 tx_desc->cmd_type_offset_bsz = ice_build_ctob(td_cmd, td_offset,
1906 size, td_tag);
1907
1908 tx_desc++;
1909 i++;
1910
1911 if (i == tx_ring->count) {
1912 tx_desc = ICE_TX_DESC(tx_ring, 0);
1913 i = 0;
1914 }
1915
1916 size = skb_frag_size(frag);
1917 data_len -= size;
1918
1919 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1920 DMA_TO_DEVICE);
1921
1922 tx_buf = &tx_ring->tx_buf[i];
1923 tx_buf->type = ICE_TX_BUF_FRAG;
1924 }
1925
1926 /* record SW timestamp if HW timestamp is not available */
1927 skb_tx_timestamp(first->skb);
1928
1929 i++;
1930 if (i == tx_ring->count)
1931 i = 0;
1932
1933 /* write last descriptor with RS and EOP bits */
1934 td_cmd |= (u64)ICE_TXD_LAST_DESC_CMD;
1935 tx_desc->cmd_type_offset_bsz =
1936 ice_build_ctob(td_cmd, td_offset, size, td_tag);
1937
1938 /* Force memory writes to complete before letting h/w know there
1939 * are new descriptors to fetch.
1940 *
1941 * We also use this memory barrier to make certain all of the
1942 * status bits have been updated before next_to_watch is written.
1943 */
1944 wmb();
1945
1946 /* set next_to_watch value indicating a packet is present */
1947 first->next_to_watch = tx_desc;
1948
1949 tx_ring->next_to_use = i;
1950
1951 ice_maybe_stop_tx(tx_ring, DESC_NEEDED);
1952
1953 /* notify HW of packet */
1954 kick = __netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount,
1955 netdev_xmit_more());
1956 if (!kick)
1957 return;
1958
1959 if (ice_is_txtime_cfg(tx_ring)) {
1960 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring;
1961 u32 tstamp_count = tstamp_ring->count;
1962 u32 j = tstamp_ring->next_to_use;
1963 struct ice_ts_desc *ts_desc;
1964 struct timespec64 ts;
1965 u32 tstamp;
1966
1967 ts = ktime_to_timespec64(first->skb->tstamp);
1968 tstamp = ts.tv_nsec >> ICE_TXTIME_CTX_RESOLUTION_128NS;
1969
1970 ts_desc = ICE_TS_DESC(tstamp_ring, j);
1971 ts_desc->tx_desc_idx_tstamp = ice_build_tstamp_desc(i, tstamp);
1972
1973 j++;
1974 if (j == tstamp_count) {
1975 u32 fetch = tstamp_count - tx_ring->count;
1976
1977 j = 0;
1978
1979 /* To prevent an MDD, when wrapping the tstamp ring
1980 * create additional TS descriptors equal to the number
1981 * of the fetch TS descriptors value. HW will merge the
1982 * TS descriptors with the same timestamp value into a
1983 * single descriptor.
1984 */
1985 for (; j < fetch; j++) {
1986 ts_desc = ICE_TS_DESC(tstamp_ring, j);
1987 ts_desc->tx_desc_idx_tstamp =
1988 ice_build_tstamp_desc(i, tstamp);
1989 }
1990 }
1991 tstamp_ring->next_to_use = j;
1992 writel_relaxed(j, tstamp_ring->tail);
1993 } else {
1994 writel_relaxed(i, tx_ring->tail);
1995 }
1996 return;
1997
1998 dma_error:
1999 /* clear DMA mappings for failed tx_buf map */
2000 for (;;) {
2001 tx_buf = &tx_ring->tx_buf[i];
2002 ice_unmap_and_free_tx_buf(tx_ring, tx_buf);
2003 if (tx_buf == first)
2004 break;
2005 if (i == 0)
2006 i = tx_ring->count;
2007 i--;
2008 }
2009
2010 tx_ring->next_to_use = i;
2011 }
2012
2013 /**
2014 * ice_tx_csum - Enable Tx checksum offloads
2015 * @first: pointer to the first descriptor
2016 * @off: pointer to struct that holds offload parameters
2017 *
2018 * Returns 0 or error (negative) if checksum offload can't happen, 1 otherwise.
2019 */
2020 static
ice_tx_csum(struct ice_tx_buf * first,struct ice_tx_offload_params * off)2021 int ice_tx_csum(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
2022 {
2023 const struct ice_tx_ring *tx_ring = off->tx_ring;
2024 u32 l4_len = 0, l3_len = 0, l2_len = 0;
2025 struct sk_buff *skb = first->skb;
2026 union {
2027 struct iphdr *v4;
2028 struct ipv6hdr *v6;
2029 unsigned char *hdr;
2030 } ip;
2031 union {
2032 struct tcphdr *tcp;
2033 unsigned char *hdr;
2034 } l4;
2035 __be16 frag_off, protocol;
2036 unsigned char *exthdr;
2037 u32 offset, cmd = 0;
2038 u8 l4_proto = 0;
2039
2040 if (skb->ip_summed != CHECKSUM_PARTIAL)
2041 return 0;
2042
2043 protocol = vlan_get_protocol(skb);
2044
2045 if (eth_p_mpls(protocol)) {
2046 ip.hdr = skb_inner_network_header(skb);
2047 l4.hdr = skb_checksum_start(skb);
2048 } else {
2049 ip.hdr = skb_network_header(skb);
2050 l4.hdr = skb_transport_header(skb);
2051 }
2052
2053 /* compute outer L2 header size */
2054 l2_len = ip.hdr - skb->data;
2055 offset = (l2_len / 2) << ICE_TX_DESC_LEN_MACLEN_S;
2056
2057 /* set the tx_flags to indicate the IP protocol type. this is
2058 * required so that checksum header computation below is accurate.
2059 */
2060 if (ip.v4->version == 4)
2061 first->tx_flags |= ICE_TX_FLAGS_IPV4;
2062 else if (ip.v6->version == 6)
2063 first->tx_flags |= ICE_TX_FLAGS_IPV6;
2064
2065 if (skb->encapsulation) {
2066 bool gso_ena = false;
2067 u32 tunnel = 0;
2068
2069 /* define outer network header type */
2070 if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
2071 tunnel |= (first->tx_flags & ICE_TX_FLAGS_TSO) ?
2072 ICE_TX_CTX_EIPT_IPV4 :
2073 ICE_TX_CTX_EIPT_IPV4_NO_CSUM;
2074 l4_proto = ip.v4->protocol;
2075 } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
2076 int ret;
2077
2078 tunnel |= ICE_TX_CTX_EIPT_IPV6;
2079 exthdr = ip.hdr + sizeof(*ip.v6);
2080 l4_proto = ip.v6->nexthdr;
2081 ret = ipv6_skip_exthdr(skb, exthdr - skb->data,
2082 &l4_proto, &frag_off);
2083 if (ret < 0)
2084 return -1;
2085 }
2086
2087 /* define outer transport */
2088 switch (l4_proto) {
2089 case IPPROTO_UDP:
2090 tunnel |= ICE_TXD_CTX_UDP_TUNNELING;
2091 first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
2092 break;
2093 case IPPROTO_GRE:
2094 tunnel |= ICE_TXD_CTX_GRE_TUNNELING;
2095 first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
2096 break;
2097 case IPPROTO_IPIP:
2098 case IPPROTO_IPV6:
2099 first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
2100 l4.hdr = skb_inner_network_header(skb);
2101 break;
2102 default:
2103 if (first->tx_flags & ICE_TX_FLAGS_TSO)
2104 return -1;
2105
2106 skb_checksum_help(skb);
2107 return 0;
2108 }
2109
2110 /* compute outer L3 header size */
2111 tunnel |= ((l4.hdr - ip.hdr) / 4) <<
2112 ICE_TXD_CTX_QW0_EIPLEN_S;
2113
2114 /* switch IP header pointer from outer to inner header */
2115 ip.hdr = skb_inner_network_header(skb);
2116
2117 /* compute tunnel header size */
2118 tunnel |= ((ip.hdr - l4.hdr) / 2) <<
2119 ICE_TXD_CTX_QW0_NATLEN_S;
2120
2121 gso_ena = skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL;
2122 /* indicate if we need to offload outer UDP header */
2123 if ((first->tx_flags & ICE_TX_FLAGS_TSO) && !gso_ena &&
2124 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
2125 tunnel |= ICE_TXD_CTX_QW0_L4T_CS_M;
2126
2127 /* record tunnel offload values */
2128 off->cd_tunnel_params |= tunnel;
2129
2130 /* set DTYP=1 to indicate that it's an Tx context descriptor
2131 * in IPsec tunnel mode with Tx offloads in Quad word 1
2132 */
2133 off->cd_qw1 |= (u64)ICE_TX_DESC_DTYPE_CTX;
2134
2135 /* switch L4 header pointer from outer to inner */
2136 l4.hdr = skb_inner_transport_header(skb);
2137 l4_proto = 0;
2138
2139 /* reset type as we transition from outer to inner headers */
2140 first->tx_flags &= ~(ICE_TX_FLAGS_IPV4 | ICE_TX_FLAGS_IPV6);
2141 if (ip.v4->version == 4)
2142 first->tx_flags |= ICE_TX_FLAGS_IPV4;
2143 if (ip.v6->version == 6)
2144 first->tx_flags |= ICE_TX_FLAGS_IPV6;
2145 }
2146
2147 /* Enable IP checksum offloads */
2148 if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
2149 l4_proto = ip.v4->protocol;
2150 /* the stack computes the IP header already, the only time we
2151 * need the hardware to recompute it is in the case of TSO.
2152 */
2153 if (first->tx_flags & ICE_TX_FLAGS_TSO)
2154 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4_CSUM;
2155 else
2156 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4;
2157
2158 } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
2159 cmd |= ICE_TX_DESC_CMD_IIPT_IPV6;
2160 exthdr = ip.hdr + sizeof(*ip.v6);
2161 l4_proto = ip.v6->nexthdr;
2162 if (l4.hdr != exthdr)
2163 ipv6_skip_exthdr(skb, exthdr - skb->data, &l4_proto,
2164 &frag_off);
2165 } else {
2166 return -1;
2167 }
2168
2169 /* compute inner L3 header size */
2170 l3_len = l4.hdr - ip.hdr;
2171 offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;
2172
2173 if ((tx_ring->netdev->features & NETIF_F_HW_CSUM) &&
2174 !(first->tx_flags & ICE_TX_FLAGS_TSO) &&
2175 !skb_csum_is_sctp(skb)) {
2176 /* Set GCS */
2177 u16 csum_start = (skb->csum_start - skb->mac_header) / 2;
2178 u16 csum_offset = skb->csum_offset / 2;
2179 u16 gcs_params;
2180
2181 gcs_params = FIELD_PREP(ICE_TX_GCS_DESC_START_M, csum_start) |
2182 FIELD_PREP(ICE_TX_GCS_DESC_OFFSET_M, csum_offset) |
2183 FIELD_PREP(ICE_TX_GCS_DESC_TYPE_M,
2184 ICE_TX_GCS_DESC_CSUM_PSH);
2185
2186 /* Unlike legacy HW checksums, GCS requires a context
2187 * descriptor.
2188 */
2189 off->cd_qw1 |= ICE_TX_DESC_DTYPE_CTX;
2190 off->cd_gcs_params = gcs_params;
2191 /* Fill out CSO info in data descriptors */
2192 off->td_offset |= offset;
2193 off->td_cmd |= cmd;
2194 return 1;
2195 }
2196
2197 /* Enable L4 checksum offloads */
2198 switch (l4_proto) {
2199 case IPPROTO_TCP:
2200 /* enable checksum offloads */
2201 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
2202 l4_len = l4.tcp->doff;
2203 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2204 break;
2205 case IPPROTO_UDP:
2206 /* enable UDP checksum offload */
2207 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_UDP;
2208 l4_len = (sizeof(struct udphdr) >> 2);
2209 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2210 break;
2211 case IPPROTO_SCTP:
2212 /* enable SCTP checksum offload */
2213 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_SCTP;
2214 l4_len = sizeof(struct sctphdr) >> 2;
2215 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2216 break;
2217
2218 default:
2219 if (first->tx_flags & ICE_TX_FLAGS_TSO)
2220 return -1;
2221 skb_checksum_help(skb);
2222 return 0;
2223 }
2224
2225 off->td_cmd |= cmd;
2226 off->td_offset |= offset;
2227 return 1;
2228 }
2229
2230 /**
2231 * ice_tx_prepare_vlan_flags - prepare generic Tx VLAN tagging flags for HW
2232 * @tx_ring: ring to send buffer on
2233 * @first: pointer to struct ice_tx_buf
2234 *
2235 * Checks the skb and set up correspondingly several generic transmit flags
2236 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
2237 */
2238 static void
ice_tx_prepare_vlan_flags(struct ice_tx_ring * tx_ring,struct ice_tx_buf * first)2239 ice_tx_prepare_vlan_flags(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first)
2240 {
2241 struct sk_buff *skb = first->skb;
2242
2243 /* nothing left to do, software offloaded VLAN */
2244 if (!skb_vlan_tag_present(skb) && eth_type_vlan(skb->protocol))
2245 return;
2246
2247 /* the VLAN ethertype/tpid is determined by VSI configuration and netdev
2248 * feature flags, which the driver only allows either 802.1Q or 802.1ad
2249 * VLAN offloads exclusively so we only care about the VLAN ID here
2250 */
2251 if (skb_vlan_tag_present(skb)) {
2252 first->vid = skb_vlan_tag_get(skb);
2253 if (tx_ring->flags & ICE_TX_FLAGS_RING_VLAN_L2TAG2)
2254 first->tx_flags |= ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN;
2255 else
2256 first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
2257 }
2258
2259 ice_tx_prepare_vlan_flags_dcb(tx_ring, first);
2260 }
2261
2262 /**
2263 * ice_tso - computes mss and TSO length to prepare for TSO
2264 * @first: pointer to struct ice_tx_buf
2265 * @off: pointer to struct that holds offload parameters
2266 *
2267 * Returns 0 or error (negative) if TSO can't happen, 1 otherwise.
2268 */
2269 static
ice_tso(struct ice_tx_buf * first,struct ice_tx_offload_params * off)2270 int ice_tso(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
2271 {
2272 struct sk_buff *skb = first->skb;
2273 union {
2274 struct iphdr *v4;
2275 struct ipv6hdr *v6;
2276 unsigned char *hdr;
2277 } ip;
2278 union {
2279 struct tcphdr *tcp;
2280 struct udphdr *udp;
2281 unsigned char *hdr;
2282 } l4;
2283 u64 cd_mss, cd_tso_len;
2284 __be16 protocol;
2285 u32 paylen;
2286 u8 l4_start;
2287 int err;
2288
2289 if (skb->ip_summed != CHECKSUM_PARTIAL)
2290 return 0;
2291
2292 if (!skb_is_gso(skb))
2293 return 0;
2294
2295 err = skb_cow_head(skb, 0);
2296 if (err < 0)
2297 return err;
2298
2299 protocol = vlan_get_protocol(skb);
2300
2301 if (eth_p_mpls(protocol))
2302 ip.hdr = skb_inner_network_header(skb);
2303 else
2304 ip.hdr = skb_network_header(skb);
2305 l4.hdr = skb_checksum_start(skb);
2306
2307 /* initialize outer IP header fields */
2308 if (ip.v4->version == 4) {
2309 ip.v4->tot_len = 0;
2310 ip.v4->check = 0;
2311 } else {
2312 ip.v6->payload_len = 0;
2313 }
2314
2315 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
2316 SKB_GSO_GRE_CSUM |
2317 SKB_GSO_IPXIP4 |
2318 SKB_GSO_IPXIP6 |
2319 SKB_GSO_UDP_TUNNEL |
2320 SKB_GSO_UDP_TUNNEL_CSUM)) {
2321 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
2322 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
2323 l4.udp->len = 0;
2324
2325 /* determine offset of outer transport header */
2326 l4_start = (u8)(l4.hdr - skb->data);
2327
2328 /* remove payload length from outer checksum */
2329 paylen = skb->len - l4_start;
2330 csum_replace_by_diff(&l4.udp->check,
2331 (__force __wsum)htonl(paylen));
2332 }
2333
2334 /* reset pointers to inner headers */
2335 ip.hdr = skb_inner_network_header(skb);
2336 l4.hdr = skb_inner_transport_header(skb);
2337
2338 /* initialize inner IP header fields */
2339 if (ip.v4->version == 4) {
2340 ip.v4->tot_len = 0;
2341 ip.v4->check = 0;
2342 } else {
2343 ip.v6->payload_len = 0;
2344 }
2345 }
2346
2347 /* determine offset of transport header */
2348 l4_start = (u8)(l4.hdr - skb->data);
2349
2350 /* remove payload length from checksum */
2351 paylen = skb->len - l4_start;
2352
2353 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
2354 csum_replace_by_diff(&l4.udp->check,
2355 (__force __wsum)htonl(paylen));
2356 /* compute length of UDP segmentation header */
2357 off->header_len = (u8)sizeof(l4.udp) + l4_start;
2358 } else {
2359 csum_replace_by_diff(&l4.tcp->check,
2360 (__force __wsum)htonl(paylen));
2361 /* compute length of TCP segmentation header */
2362 off->header_len = (u8)((l4.tcp->doff * 4) + l4_start);
2363 }
2364
2365 /* update gso_segs and bytecount */
2366 first->gso_segs = skb_shinfo(skb)->gso_segs;
2367 first->bytecount += (first->gso_segs - 1) * off->header_len;
2368
2369 cd_tso_len = skb->len - off->header_len;
2370 cd_mss = skb_shinfo(skb)->gso_size;
2371
2372 /* record cdesc_qw1 with TSO parameters */
2373 off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2374 (ICE_TX_CTX_DESC_TSO << ICE_TXD_CTX_QW1_CMD_S) |
2375 (cd_tso_len << ICE_TXD_CTX_QW1_TSO_LEN_S) |
2376 (cd_mss << ICE_TXD_CTX_QW1_MSS_S));
2377 first->tx_flags |= ICE_TX_FLAGS_TSO;
2378 return 1;
2379 }
2380
2381 /**
2382 * ice_txd_use_count - estimate the number of descriptors needed for Tx
2383 * @size: transmit request size in bytes
2384 *
2385 * Due to hardware alignment restrictions (4K alignment), we need to
2386 * assume that we can have no more than 12K of data per descriptor, even
2387 * though each descriptor can take up to 16K - 1 bytes of aligned memory.
2388 * Thus, we need to divide by 12K. But division is slow! Instead,
2389 * we decompose the operation into shifts and one relatively cheap
2390 * multiply operation.
2391 *
2392 * To divide by 12K, we first divide by 4K, then divide by 3:
2393 * To divide by 4K, shift right by 12 bits
2394 * To divide by 3, multiply by 85, then divide by 256
2395 * (Divide by 256 is done by shifting right by 8 bits)
2396 * Finally, we add one to round up. Because 256 isn't an exact multiple of
2397 * 3, we'll underestimate near each multiple of 12K. This is actually more
2398 * accurate as we have 4K - 1 of wiggle room that we can fit into the last
2399 * segment. For our purposes this is accurate out to 1M which is orders of
2400 * magnitude greater than our largest possible GSO size.
2401 *
2402 * This would then be implemented as:
2403 * return (((size >> 12) * 85) >> 8) + ICE_DESCS_FOR_SKB_DATA_PTR;
2404 *
2405 * Since multiplication and division are commutative, we can reorder
2406 * operations into:
2407 * return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
2408 */
ice_txd_use_count(unsigned int size)2409 static unsigned int ice_txd_use_count(unsigned int size)
2410 {
2411 return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
2412 }
2413
2414 /**
2415 * ice_xmit_desc_count - calculate number of Tx descriptors needed
2416 * @skb: send buffer
2417 *
2418 * Returns number of data descriptors needed for this skb.
2419 */
ice_xmit_desc_count(struct sk_buff * skb)2420 static unsigned int ice_xmit_desc_count(struct sk_buff *skb)
2421 {
2422 const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
2423 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2424 unsigned int count = 0, size = skb_headlen(skb);
2425
2426 for (;;) {
2427 count += ice_txd_use_count(size);
2428
2429 if (!nr_frags--)
2430 break;
2431
2432 size = skb_frag_size(frag++);
2433 }
2434
2435 return count;
2436 }
2437
2438 /**
2439 * __ice_chk_linearize - Check if there are more than 8 buffers per packet
2440 * @skb: send buffer
2441 *
2442 * Note: This HW can't DMA more than 8 buffers to build a packet on the wire
2443 * and so we need to figure out the cases where we need to linearize the skb.
2444 *
2445 * For TSO we need to count the TSO header and segment payload separately.
2446 * As such we need to check cases where we have 7 fragments or more as we
2447 * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
2448 * the segment payload in the first descriptor, and another 7 for the
2449 * fragments.
2450 */
__ice_chk_linearize(struct sk_buff * skb)2451 static bool __ice_chk_linearize(struct sk_buff *skb)
2452 {
2453 const skb_frag_t *frag, *stale;
2454 int nr_frags, sum;
2455
2456 /* no need to check if number of frags is less than 7 */
2457 nr_frags = skb_shinfo(skb)->nr_frags;
2458 if (nr_frags < (ICE_MAX_BUF_TXD - 1))
2459 return false;
2460
2461 /* We need to walk through the list and validate that each group
2462 * of 6 fragments totals at least gso_size.
2463 */
2464 nr_frags -= ICE_MAX_BUF_TXD - 2;
2465 frag = &skb_shinfo(skb)->frags[0];
2466
2467 /* Initialize size to the negative value of gso_size minus 1. We
2468 * use this as the worst case scenario in which the frag ahead
2469 * of us only provides one byte which is why we are limited to 6
2470 * descriptors for a single transmit as the header and previous
2471 * fragment are already consuming 2 descriptors.
2472 */
2473 sum = 1 - skb_shinfo(skb)->gso_size;
2474
2475 /* Add size of frags 0 through 4 to create our initial sum */
2476 sum += skb_frag_size(frag++);
2477 sum += skb_frag_size(frag++);
2478 sum += skb_frag_size(frag++);
2479 sum += skb_frag_size(frag++);
2480 sum += skb_frag_size(frag++);
2481
2482 /* Walk through fragments adding latest fragment, testing it, and
2483 * then removing stale fragments from the sum.
2484 */
2485 for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
2486 int stale_size = skb_frag_size(stale);
2487
2488 sum += skb_frag_size(frag++);
2489
2490 /* The stale fragment may present us with a smaller
2491 * descriptor than the actual fragment size. To account
2492 * for that we need to remove all the data on the front and
2493 * figure out what the remainder would be in the last
2494 * descriptor associated with the fragment.
2495 */
2496 if (stale_size > ICE_MAX_DATA_PER_TXD) {
2497 int align_pad = -(skb_frag_off(stale)) &
2498 (ICE_MAX_READ_REQ_SIZE - 1);
2499
2500 sum -= align_pad;
2501 stale_size -= align_pad;
2502
2503 do {
2504 sum -= ICE_MAX_DATA_PER_TXD_ALIGNED;
2505 stale_size -= ICE_MAX_DATA_PER_TXD_ALIGNED;
2506 } while (stale_size > ICE_MAX_DATA_PER_TXD);
2507 }
2508
2509 /* if sum is negative we failed to make sufficient progress */
2510 if (sum < 0)
2511 return true;
2512
2513 if (!nr_frags--)
2514 break;
2515
2516 sum -= stale_size;
2517 }
2518
2519 return false;
2520 }
2521
2522 /**
2523 * ice_chk_linearize - Check if there are more than 8 fragments per packet
2524 * @skb: send buffer
2525 * @count: number of buffers used
2526 *
2527 * Note: Our HW can't scatter-gather more than 8 fragments to build
2528 * a packet on the wire and so we need to figure out the cases where we
2529 * need to linearize the skb.
2530 */
ice_chk_linearize(struct sk_buff * skb,unsigned int count)2531 static bool ice_chk_linearize(struct sk_buff *skb, unsigned int count)
2532 {
2533 /* Both TSO and single send will work if count is less than 8 */
2534 if (likely(count < ICE_MAX_BUF_TXD))
2535 return false;
2536
2537 if (skb_is_gso(skb))
2538 return __ice_chk_linearize(skb);
2539
2540 /* we can support up to 8 data buffers for a single send */
2541 return count != ICE_MAX_BUF_TXD;
2542 }
2543
2544 /**
2545 * ice_tstamp - set up context descriptor for hardware timestamp
2546 * @tx_ring: pointer to the Tx ring to send buffer on
2547 * @skb: pointer to the SKB we're sending
2548 * @first: Tx buffer
2549 * @off: Tx offload parameters
2550 */
2551 static void
ice_tstamp(struct ice_tx_ring * tx_ring,struct sk_buff * skb,struct ice_tx_buf * first,struct ice_tx_offload_params * off)2552 ice_tstamp(struct ice_tx_ring *tx_ring, struct sk_buff *skb,
2553 struct ice_tx_buf *first, struct ice_tx_offload_params *off)
2554 {
2555 s8 idx;
2556
2557 /* only timestamp the outbound packet if the user has requested it */
2558 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
2559 return;
2560
2561 /* Tx timestamps cannot be sampled when doing TSO */
2562 if (first->tx_flags & ICE_TX_FLAGS_TSO)
2563 return;
2564
2565 /* Grab an open timestamp slot */
2566 idx = ice_ptp_request_ts(tx_ring->tx_tstamps, skb);
2567 if (idx < 0) {
2568 tx_ring->vsi->back->ptp.tx_hwtstamp_skipped++;
2569 return;
2570 }
2571
2572 off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2573 (ICE_TX_CTX_DESC_TSYN << ICE_TXD_CTX_QW1_CMD_S) |
2574 ((u64)idx << ICE_TXD_CTX_QW1_TSO_LEN_S));
2575 first->tx_flags |= ICE_TX_FLAGS_TSYN;
2576 }
2577
2578 /**
2579 * ice_xmit_frame_ring - Sends buffer on Tx ring
2580 * @skb: send buffer
2581 * @tx_ring: ring to send buffer on
2582 *
2583 * Returns NETDEV_TX_OK if sent, else an error code
2584 */
2585 static netdev_tx_t
ice_xmit_frame_ring(struct sk_buff * skb,struct ice_tx_ring * tx_ring)2586 ice_xmit_frame_ring(struct sk_buff *skb, struct ice_tx_ring *tx_ring)
2587 {
2588 struct ice_tx_offload_params offload = { 0 };
2589 struct ice_vsi *vsi = tx_ring->vsi;
2590 struct ice_tx_buf *first;
2591 struct ethhdr *eth;
2592 unsigned int count;
2593 int tso, csum;
2594
2595 ice_trace(xmit_frame_ring, tx_ring, skb);
2596
2597 if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
2598 goto out_drop;
2599
2600 count = ice_xmit_desc_count(skb);
2601 if (ice_chk_linearize(skb, count)) {
2602 if (__skb_linearize(skb))
2603 goto out_drop;
2604 count = ice_txd_use_count(skb->len);
2605 tx_ring->ring_stats->tx_stats.tx_linearize++;
2606 }
2607
2608 /* need: 1 descriptor per page * PAGE_SIZE/ICE_MAX_DATA_PER_TXD,
2609 * + 1 desc for skb_head_len/ICE_MAX_DATA_PER_TXD,
2610 * + 4 desc gap to avoid the cache line where head is,
2611 * + 1 desc for context descriptor,
2612 * otherwise try next time
2613 */
2614 if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE +
2615 ICE_DESCS_FOR_CTX_DESC)) {
2616 tx_ring->ring_stats->tx_stats.tx_busy++;
2617 return NETDEV_TX_BUSY;
2618 }
2619
2620 /* prefetch for bql data which is infrequently used */
2621 netdev_txq_bql_enqueue_prefetchw(txring_txq(tx_ring));
2622
2623 offload.tx_ring = tx_ring;
2624
2625 /* record the location of the first descriptor for this packet */
2626 first = &tx_ring->tx_buf[tx_ring->next_to_use];
2627 first->skb = skb;
2628 first->type = ICE_TX_BUF_SKB;
2629 first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
2630 first->gso_segs = 1;
2631 first->tx_flags = 0;
2632
2633 /* prepare the VLAN tagging flags for Tx */
2634 ice_tx_prepare_vlan_flags(tx_ring, first);
2635 if (first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) {
2636 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2637 (ICE_TX_CTX_DESC_IL2TAG2 <<
2638 ICE_TXD_CTX_QW1_CMD_S));
2639 offload.cd_l2tag2 = first->vid;
2640 }
2641
2642 /* set up TSO offload */
2643 tso = ice_tso(first, &offload);
2644 if (tso < 0)
2645 goto out_drop;
2646
2647 /* always set up Tx checksum offload */
2648 csum = ice_tx_csum(first, &offload);
2649 if (csum < 0)
2650 goto out_drop;
2651
2652 /* allow CONTROL frames egress from main VSI if FW LLDP disabled */
2653 eth = (struct ethhdr *)skb_mac_header(skb);
2654
2655 if ((ice_is_switchdev_running(vsi->back) ||
2656 ice_lag_is_switchdev_running(vsi->back)) &&
2657 vsi->type != ICE_VSI_SF)
2658 ice_eswitch_set_target_vsi(skb, &offload);
2659 else if (unlikely((skb->priority == TC_PRIO_CONTROL ||
2660 eth->h_proto == htons(ETH_P_LLDP)) &&
2661 vsi->type == ICE_VSI_PF &&
2662 vsi->port_info->qos_cfg.is_sw_lldp))
2663 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2664 ICE_TX_CTX_DESC_SWTCH_UPLINK <<
2665 ICE_TXD_CTX_QW1_CMD_S);
2666
2667 ice_tstamp(tx_ring, skb, first, &offload);
2668
2669 if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) {
2670 struct ice_tx_ctx_desc *cdesc;
2671 u16 i = tx_ring->next_to_use;
2672
2673 /* grab the next descriptor */
2674 cdesc = ICE_TX_CTX_DESC(tx_ring, i);
2675 i++;
2676 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2677
2678 /* setup context descriptor */
2679 cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params);
2680 cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2);
2681 cdesc->gcs = cpu_to_le16(offload.cd_gcs_params);
2682 cdesc->qw1 = cpu_to_le64(offload.cd_qw1);
2683 }
2684
2685 ice_tx_map(tx_ring, first, &offload);
2686 return NETDEV_TX_OK;
2687
2688 out_drop:
2689 ice_trace(xmit_frame_ring_drop, tx_ring, skb);
2690 dev_kfree_skb_any(skb);
2691 return NETDEV_TX_OK;
2692 }
2693
2694 /**
2695 * ice_start_xmit - Selects the correct VSI and Tx queue to send buffer
2696 * @skb: send buffer
2697 * @netdev: network interface device structure
2698 *
2699 * Returns NETDEV_TX_OK if sent, else an error code
2700 */
ice_start_xmit(struct sk_buff * skb,struct net_device * netdev)2701 netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2702 {
2703 struct ice_netdev_priv *np = netdev_priv(netdev);
2704 struct ice_vsi *vsi = np->vsi;
2705 struct ice_tx_ring *tx_ring;
2706
2707 tx_ring = vsi->tx_rings[skb->queue_mapping];
2708
2709 /* hardware can't handle really short frames, hardware padding works
2710 * beyond this point
2711 */
2712 if (skb_put_padto(skb, ICE_MIN_TX_LEN))
2713 return NETDEV_TX_OK;
2714
2715 return ice_xmit_frame_ring(skb, tx_ring);
2716 }
2717
2718 /**
2719 * ice_get_dscp_up - return the UP/TC value for a SKB
2720 * @dcbcfg: DCB config that contains DSCP to UP/TC mapping
2721 * @skb: SKB to query for info to determine UP/TC
2722 *
2723 * This function is to only be called when the PF is in L3 DSCP PFC mode
2724 */
ice_get_dscp_up(struct ice_dcbx_cfg * dcbcfg,struct sk_buff * skb)2725 static u8 ice_get_dscp_up(struct ice_dcbx_cfg *dcbcfg, struct sk_buff *skb)
2726 {
2727 u8 dscp = 0;
2728
2729 if (skb->protocol == htons(ETH_P_IP))
2730 dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
2731 else if (skb->protocol == htons(ETH_P_IPV6))
2732 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
2733
2734 return dcbcfg->dscp_map[dscp];
2735 }
2736
2737 u16
ice_select_queue(struct net_device * netdev,struct sk_buff * skb,struct net_device * sb_dev)2738 ice_select_queue(struct net_device *netdev, struct sk_buff *skb,
2739 struct net_device *sb_dev)
2740 {
2741 struct ice_pf *pf = ice_netdev_to_pf(netdev);
2742 struct ice_dcbx_cfg *dcbcfg;
2743
2744 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
2745 if (dcbcfg->pfc_mode == ICE_QOS_MODE_DSCP)
2746 skb->priority = ice_get_dscp_up(dcbcfg, skb);
2747
2748 return netdev_pick_tx(netdev, skb, sb_dev);
2749 }
2750
2751 /**
2752 * ice_clean_ctrl_tx_irq - interrupt handler for flow director Tx queue
2753 * @tx_ring: tx_ring to clean
2754 */
ice_clean_ctrl_tx_irq(struct ice_tx_ring * tx_ring)2755 void ice_clean_ctrl_tx_irq(struct ice_tx_ring *tx_ring)
2756 {
2757 struct ice_vsi *vsi = tx_ring->vsi;
2758 s16 i = tx_ring->next_to_clean;
2759 int budget = ICE_DFLT_IRQ_WORK;
2760 struct ice_tx_desc *tx_desc;
2761 struct ice_tx_buf *tx_buf;
2762
2763 tx_buf = &tx_ring->tx_buf[i];
2764 tx_desc = ICE_TX_DESC(tx_ring, i);
2765 i -= tx_ring->count;
2766
2767 do {
2768 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
2769
2770 /* if next_to_watch is not set then there is no pending work */
2771 if (!eop_desc)
2772 break;
2773
2774 /* prevent any other reads prior to eop_desc */
2775 smp_rmb();
2776
2777 /* if the descriptor isn't done, no work to do */
2778 if (!(eop_desc->cmd_type_offset_bsz &
2779 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
2780 break;
2781
2782 /* clear next_to_watch to prevent false hangs */
2783 tx_buf->next_to_watch = NULL;
2784 tx_desc->buf_addr = 0;
2785 tx_desc->cmd_type_offset_bsz = 0;
2786
2787 /* move past filter desc */
2788 tx_buf++;
2789 tx_desc++;
2790 i++;
2791 if (unlikely(!i)) {
2792 i -= tx_ring->count;
2793 tx_buf = tx_ring->tx_buf;
2794 tx_desc = ICE_TX_DESC(tx_ring, 0);
2795 }
2796
2797 /* unmap the data header */
2798 if (dma_unmap_len(tx_buf, len))
2799 dma_unmap_single(tx_ring->dev,
2800 dma_unmap_addr(tx_buf, dma),
2801 dma_unmap_len(tx_buf, len),
2802 DMA_TO_DEVICE);
2803 if (tx_buf->type == ICE_TX_BUF_DUMMY)
2804 devm_kfree(tx_ring->dev, tx_buf->raw_buf);
2805
2806 /* clear next_to_watch to prevent false hangs */
2807 tx_buf->type = ICE_TX_BUF_EMPTY;
2808 tx_buf->tx_flags = 0;
2809 tx_buf->next_to_watch = NULL;
2810 dma_unmap_len_set(tx_buf, len, 0);
2811 tx_desc->buf_addr = 0;
2812 tx_desc->cmd_type_offset_bsz = 0;
2813
2814 /* move past eop_desc for start of next FD desc */
2815 tx_buf++;
2816 tx_desc++;
2817 i++;
2818 if (unlikely(!i)) {
2819 i -= tx_ring->count;
2820 tx_buf = tx_ring->tx_buf;
2821 tx_desc = ICE_TX_DESC(tx_ring, 0);
2822 }
2823
2824 budget--;
2825 } while (likely(budget));
2826
2827 i += tx_ring->count;
2828 tx_ring->next_to_clean = i;
2829
2830 /* re-enable interrupt if needed */
2831 ice_irq_dynamic_ena(&vsi->back->hw, vsi, vsi->q_vectors[0]);
2832 }
2833