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