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