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