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