xref: /linux/drivers/net/ethernet/intel/idpf/idpf_txrx.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_ptp.h"
6 #include "idpf_virtchnl.h"
7 #include "xdp.h"
8 #include "xsk.h"
9 
10 #define idpf_tx_buf_next(buf)		(*(u32 *)&(buf)->priv)
11 LIBETH_SQE_CHECK_PRIV(u32);
12 
13 /**
14  * idpf_chk_linearize - Check if skb exceeds max descriptors per packet
15  * @skb: send buffer
16  * @max_bufs: maximum scatter gather buffers for single packet
17  * @count: number of buffers this packet needs
18  *
19  * Make sure we don't exceed maximum scatter gather buffers for a single
20  * packet.
21  * TSO case has been handled earlier from idpf_features_check().
22  */
23 static bool idpf_chk_linearize(const struct sk_buff *skb,
24 			       unsigned int max_bufs,
25 			       unsigned int count)
26 {
27 	if (likely(count <= max_bufs))
28 		return false;
29 
30 	if (skb_is_gso(skb))
31 		return false;
32 
33 	return true;
34 }
35 
36 /**
37  * idpf_tx_timeout - Respond to a Tx Hang
38  * @netdev: network interface device structure
39  * @txqueue: TX queue
40  */
41 void idpf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
42 {
43 	struct idpf_adapter *adapter = idpf_netdev_to_adapter(netdev);
44 
45 	adapter->tx_timeout_count++;
46 
47 	netdev_err(netdev, "Detected Tx timeout: Count %d, Queue %d\n",
48 		   adapter->tx_timeout_count, txqueue);
49 	if (!idpf_is_reset_in_prog(adapter)) {
50 		set_bit(IDPF_HR_FUNC_RESET, adapter->flags);
51 		queue_delayed_work(adapter->vc_event_wq,
52 				   &adapter->vc_event_task,
53 				   msecs_to_jiffies(10));
54 	}
55 }
56 
57 static void idpf_tx_buf_clean(struct idpf_tx_queue *txq)
58 {
59 	struct libeth_sq_napi_stats ss = { };
60 	struct xdp_frame_bulk bq;
61 	struct libeth_cq_pp cp = {
62 		.dev	= txq->dev,
63 		.bq	= &bq,
64 		.ss	= &ss,
65 	};
66 
67 	xdp_frame_bulk_init(&bq);
68 
69 	/* Free all the Tx buffer sk_buffs */
70 	for (u32 i = 0; i < txq->buf_pool_size; i++)
71 		libeth_tx_complete_any(&txq->tx_buf[i], &cp);
72 
73 	xdp_flush_frame_bulk(&bq);
74 }
75 
76 /**
77  * idpf_tx_buf_rel_all - Free any empty Tx buffers
78  * @txq: queue to be cleaned
79  */
80 static void idpf_tx_buf_rel_all(struct idpf_tx_queue *txq)
81 {
82 	/* Buffers already cleared, nothing to do */
83 	if (!txq->tx_buf)
84 		return;
85 
86 	if (idpf_queue_has(XSK, txq))
87 		idpf_xsksq_clean(txq);
88 	else
89 		idpf_tx_buf_clean(txq);
90 
91 	kfree(txq->tx_buf);
92 	txq->tx_buf = NULL;
93 }
94 
95 /**
96  * idpf_tx_desc_rel - Free Tx resources per queue
97  * @txq: Tx descriptor ring for a specific queue
98  *
99  * Free all transmit software resources
100  */
101 static void idpf_tx_desc_rel(struct idpf_tx_queue *txq)
102 {
103 	bool xdp = idpf_queue_has(XDP, txq);
104 
105 	if (xdp)
106 		libeth_xdpsq_deinit_timer(txq->timer);
107 
108 	idpf_tx_buf_rel_all(txq);
109 
110 	if (!xdp)
111 		netdev_tx_reset_subqueue(txq->netdev, txq->idx);
112 
113 	idpf_xsk_clear_queue(txq, VIRTCHNL2_QUEUE_TYPE_TX);
114 
115 	if (!txq->desc_ring)
116 		return;
117 
118 	if (!xdp && txq->refillq)
119 		kfree(txq->refillq->ring);
120 
121 	dmam_free_coherent(txq->dev, txq->size, txq->desc_ring, txq->dma);
122 	txq->desc_ring = NULL;
123 	txq->next_to_use = 0;
124 	txq->next_to_clean = 0;
125 }
126 
127 /**
128  * idpf_compl_desc_rel - Free completion resources per queue
129  * @complq: completion queue
130  *
131  * Free all completion software resources.
132  */
133 static void idpf_compl_desc_rel(struct idpf_compl_queue *complq)
134 {
135 	idpf_xsk_clear_queue(complq, VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION);
136 
137 	if (!complq->comp)
138 		return;
139 
140 	dma_free_coherent(complq->netdev->dev.parent, complq->size,
141 			  complq->desc_ring, complq->dma);
142 	complq->desc_ring = NULL;
143 	complq->next_to_use = 0;
144 	complq->next_to_clean = 0;
145 }
146 
147 /**
148  * idpf_tx_desc_rel_all - Free Tx Resources for All Queues
149  * @vport: virtual port structure
150  *
151  * Free all transmit software resources
152  */
153 static void idpf_tx_desc_rel_all(struct idpf_vport *vport)
154 {
155 	int i, j;
156 
157 	if (!vport->txq_grps)
158 		return;
159 
160 	for (i = 0; i < vport->num_txq_grp; i++) {
161 		struct idpf_txq_group *txq_grp = &vport->txq_grps[i];
162 
163 		for (j = 0; j < txq_grp->num_txq; j++)
164 			idpf_tx_desc_rel(txq_grp->txqs[j]);
165 
166 		if (idpf_is_queue_model_split(vport->txq_model))
167 			idpf_compl_desc_rel(txq_grp->complq);
168 	}
169 }
170 
171 /**
172  * idpf_tx_buf_alloc_all - Allocate memory for all buffer resources
173  * @tx_q: queue for which the buffers are allocated
174  *
175  * Returns 0 on success, negative on failure
176  */
177 static int idpf_tx_buf_alloc_all(struct idpf_tx_queue *tx_q)
178 {
179 	/* Allocate book keeping buffers only. Buffers to be supplied to HW
180 	 * are allocated by kernel network stack and received as part of skb
181 	 */
182 	if (idpf_queue_has(FLOW_SCH_EN, tx_q))
183 		tx_q->buf_pool_size = U16_MAX;
184 	else
185 		tx_q->buf_pool_size = tx_q->desc_count;
186 	tx_q->tx_buf = kcalloc(tx_q->buf_pool_size, sizeof(*tx_q->tx_buf),
187 			       GFP_KERNEL);
188 	if (!tx_q->tx_buf)
189 		return -ENOMEM;
190 
191 	return 0;
192 }
193 
194 /**
195  * idpf_tx_desc_alloc - Allocate the Tx descriptors
196  * @vport: vport to allocate resources for
197  * @tx_q: the tx ring to set up
198  *
199  * Returns 0 on success, negative on failure
200  */
201 static int idpf_tx_desc_alloc(const struct idpf_vport *vport,
202 			      struct idpf_tx_queue *tx_q)
203 {
204 	struct device *dev = tx_q->dev;
205 	struct idpf_sw_queue *refillq;
206 	int err;
207 
208 	err = idpf_tx_buf_alloc_all(tx_q);
209 	if (err)
210 		goto err_alloc;
211 
212 	tx_q->size = tx_q->desc_count * sizeof(*tx_q->base_tx);
213 
214 	/* Allocate descriptors also round up to nearest 4K */
215 	tx_q->size = ALIGN(tx_q->size, 4096);
216 	tx_q->desc_ring = dmam_alloc_coherent(dev, tx_q->size, &tx_q->dma,
217 					      GFP_KERNEL);
218 	if (!tx_q->desc_ring) {
219 		dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
220 			tx_q->size);
221 		err = -ENOMEM;
222 		goto err_alloc;
223 	}
224 
225 	tx_q->next_to_use = 0;
226 	tx_q->next_to_clean = 0;
227 	idpf_queue_set(GEN_CHK, tx_q);
228 
229 	idpf_xsk_setup_queue(vport, tx_q, VIRTCHNL2_QUEUE_TYPE_TX);
230 
231 	if (!idpf_queue_has(FLOW_SCH_EN, tx_q))
232 		return 0;
233 
234 	refillq = tx_q->refillq;
235 	refillq->desc_count = tx_q->buf_pool_size;
236 	refillq->ring = kcalloc(refillq->desc_count, sizeof(u32),
237 				GFP_KERNEL);
238 	if (!refillq->ring) {
239 		err = -ENOMEM;
240 		goto err_alloc;
241 	}
242 
243 	for (unsigned int i = 0; i < refillq->desc_count; i++)
244 		refillq->ring[i] =
245 			FIELD_PREP(IDPF_RFL_BI_BUFID_M, i) |
246 			FIELD_PREP(IDPF_RFL_BI_GEN_M,
247 				   idpf_queue_has(GEN_CHK, refillq));
248 
249 	/* Go ahead and flip the GEN bit since this counts as filling
250 	 * up the ring, i.e. we already ring wrapped.
251 	 */
252 	idpf_queue_change(GEN_CHK, refillq);
253 
254 	tx_q->last_re = tx_q->desc_count - IDPF_TX_SPLITQ_RE_MIN_GAP;
255 
256 	return 0;
257 
258 err_alloc:
259 	idpf_tx_desc_rel(tx_q);
260 
261 	return err;
262 }
263 
264 /**
265  * idpf_compl_desc_alloc - allocate completion descriptors
266  * @vport: vport to allocate resources for
267  * @complq: completion queue to set up
268  *
269  * Return: 0 on success, -errno on failure.
270  */
271 static int idpf_compl_desc_alloc(const struct idpf_vport *vport,
272 				 struct idpf_compl_queue *complq)
273 {
274 	u32 desc_size;
275 
276 	desc_size = idpf_queue_has(FLOW_SCH_EN, complq) ?
277 		    sizeof(*complq->comp) : sizeof(*complq->comp_4b);
278 	complq->size = array_size(complq->desc_count, desc_size);
279 
280 	complq->desc_ring = dma_alloc_coherent(complq->netdev->dev.parent,
281 					       complq->size, &complq->dma,
282 					       GFP_KERNEL);
283 	if (!complq->desc_ring)
284 		return -ENOMEM;
285 
286 	complq->next_to_use = 0;
287 	complq->next_to_clean = 0;
288 	idpf_queue_set(GEN_CHK, complq);
289 
290 	idpf_xsk_setup_queue(vport, complq,
291 			     VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION);
292 
293 	return 0;
294 }
295 
296 /**
297  * idpf_tx_desc_alloc_all - allocate all queues Tx resources
298  * @vport: virtual port private structure
299  *
300  * Returns 0 on success, negative on failure
301  */
302 static int idpf_tx_desc_alloc_all(struct idpf_vport *vport)
303 {
304 	int err = 0;
305 	int i, j;
306 
307 	/* Setup buffer queues. In single queue model buffer queues and
308 	 * completion queues will be same
309 	 */
310 	for (i = 0; i < vport->num_txq_grp; i++) {
311 		for (j = 0; j < vport->txq_grps[i].num_txq; j++) {
312 			struct idpf_tx_queue *txq = vport->txq_grps[i].txqs[j];
313 
314 			err = idpf_tx_desc_alloc(vport, txq);
315 			if (err) {
316 				pci_err(vport->adapter->pdev,
317 					"Allocation for Tx Queue %u failed\n",
318 					i);
319 				goto err_out;
320 			}
321 		}
322 
323 		if (!idpf_is_queue_model_split(vport->txq_model))
324 			continue;
325 
326 		/* Setup completion queues */
327 		err = idpf_compl_desc_alloc(vport, vport->txq_grps[i].complq);
328 		if (err) {
329 			pci_err(vport->adapter->pdev,
330 				"Allocation for Tx Completion Queue %u failed\n",
331 				i);
332 			goto err_out;
333 		}
334 	}
335 
336 err_out:
337 	if (err)
338 		idpf_tx_desc_rel_all(vport);
339 
340 	return err;
341 }
342 
343 /**
344  * idpf_rx_page_rel - Release an rx buffer page
345  * @rx_buf: the buffer to free
346  */
347 static void idpf_rx_page_rel(struct libeth_fqe *rx_buf)
348 {
349 	if (unlikely(!rx_buf->netmem))
350 		return;
351 
352 	libeth_rx_recycle_slow(rx_buf->netmem);
353 
354 	rx_buf->netmem = 0;
355 	rx_buf->offset = 0;
356 }
357 
358 /**
359  * idpf_rx_hdr_buf_rel_all - Release header buffer memory
360  * @bufq: queue to use
361  */
362 static void idpf_rx_hdr_buf_rel_all(struct idpf_buf_queue *bufq)
363 {
364 	struct libeth_fq fq = {
365 		.fqes	= bufq->hdr_buf,
366 		.pp	= bufq->hdr_pp,
367 	};
368 
369 	for (u32 i = 0; i < bufq->desc_count; i++)
370 		idpf_rx_page_rel(&bufq->hdr_buf[i]);
371 
372 	libeth_rx_fq_destroy(&fq);
373 	bufq->hdr_buf = NULL;
374 	bufq->hdr_pp = NULL;
375 }
376 
377 /**
378  * idpf_rx_buf_rel_bufq - Free all Rx buffer resources for a buffer queue
379  * @bufq: queue to be cleaned
380  */
381 static void idpf_rx_buf_rel_bufq(struct idpf_buf_queue *bufq)
382 {
383 	struct libeth_fq fq = {
384 		.fqes	= bufq->buf,
385 		.pp	= bufq->pp,
386 	};
387 
388 	/* queue already cleared, nothing to do */
389 	if (!bufq->buf)
390 		return;
391 
392 	if (idpf_queue_has(XSK, bufq)) {
393 		idpf_xskfq_rel(bufq);
394 		return;
395 	}
396 
397 	/* Free all the bufs allocated and given to hw on Rx queue */
398 	for (u32 i = 0; i < bufq->desc_count; i++)
399 		idpf_rx_page_rel(&bufq->buf[i]);
400 
401 	if (idpf_queue_has(HSPLIT_EN, bufq))
402 		idpf_rx_hdr_buf_rel_all(bufq);
403 
404 	libeth_rx_fq_destroy(&fq);
405 	bufq->buf = NULL;
406 	bufq->pp = NULL;
407 }
408 
409 /**
410  * idpf_rx_buf_rel_all - Free all Rx buffer resources for a receive queue
411  * @rxq: queue to be cleaned
412  */
413 static void idpf_rx_buf_rel_all(struct idpf_rx_queue *rxq)
414 {
415 	struct libeth_fq fq = {
416 		.fqes	= rxq->rx_buf,
417 		.pp	= rxq->pp,
418 	};
419 
420 	if (!rxq->rx_buf)
421 		return;
422 
423 	for (u32 i = 0; i < rxq->desc_count; i++)
424 		idpf_rx_page_rel(&rxq->rx_buf[i]);
425 
426 	libeth_rx_fq_destroy(&fq);
427 	rxq->rx_buf = NULL;
428 	rxq->pp = NULL;
429 }
430 
431 /**
432  * idpf_rx_desc_rel - Free a specific Rx q resources
433  * @rxq: queue to clean the resources from
434  * @dev: device to free DMA memory
435  * @model: single or split queue model
436  *
437  * Free a specific rx queue resources
438  */
439 static void idpf_rx_desc_rel(struct idpf_rx_queue *rxq, struct device *dev,
440 			     u32 model)
441 {
442 	if (!rxq)
443 		return;
444 
445 	if (!idpf_queue_has(XSK, rxq))
446 		libeth_xdp_return_stash(&rxq->xdp);
447 
448 	if (!idpf_is_queue_model_split(model))
449 		idpf_rx_buf_rel_all(rxq);
450 
451 	idpf_xsk_clear_queue(rxq, VIRTCHNL2_QUEUE_TYPE_RX);
452 
453 	rxq->next_to_alloc = 0;
454 	rxq->next_to_clean = 0;
455 	rxq->next_to_use = 0;
456 	if (!rxq->desc_ring)
457 		return;
458 
459 	dmam_free_coherent(dev, rxq->size, rxq->desc_ring, rxq->dma);
460 	rxq->desc_ring = NULL;
461 }
462 
463 /**
464  * idpf_rx_desc_rel_bufq - free buffer queue resources
465  * @bufq: buffer queue to clean the resources from
466  * @dev: device to free DMA memory
467  */
468 static void idpf_rx_desc_rel_bufq(struct idpf_buf_queue *bufq,
469 				  struct device *dev)
470 {
471 	if (!bufq)
472 		return;
473 
474 	idpf_rx_buf_rel_bufq(bufq);
475 	idpf_xsk_clear_queue(bufq, VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
476 
477 	bufq->next_to_alloc = 0;
478 	bufq->next_to_clean = 0;
479 	bufq->next_to_use = 0;
480 
481 	if (!bufq->split_buf)
482 		return;
483 
484 	dma_free_coherent(dev, bufq->size, bufq->split_buf, bufq->dma);
485 	bufq->split_buf = NULL;
486 }
487 
488 /**
489  * idpf_rx_desc_rel_all - Free Rx Resources for All Queues
490  * @vport: virtual port structure
491  *
492  * Free all rx queues resources
493  */
494 static void idpf_rx_desc_rel_all(struct idpf_vport *vport)
495 {
496 	struct device *dev = &vport->adapter->pdev->dev;
497 	struct idpf_rxq_group *rx_qgrp;
498 	u16 num_rxq;
499 	int i, j;
500 
501 	if (!vport->rxq_grps)
502 		return;
503 
504 	for (i = 0; i < vport->num_rxq_grp; i++) {
505 		rx_qgrp = &vport->rxq_grps[i];
506 
507 		if (!idpf_is_queue_model_split(vport->rxq_model)) {
508 			for (j = 0; j < rx_qgrp->singleq.num_rxq; j++)
509 				idpf_rx_desc_rel(rx_qgrp->singleq.rxqs[j], dev,
510 						 VIRTCHNL2_QUEUE_MODEL_SINGLE);
511 			continue;
512 		}
513 
514 		num_rxq = rx_qgrp->splitq.num_rxq_sets;
515 		for (j = 0; j < num_rxq; j++)
516 			idpf_rx_desc_rel(&rx_qgrp->splitq.rxq_sets[j]->rxq,
517 					 dev, VIRTCHNL2_QUEUE_MODEL_SPLIT);
518 
519 		if (!rx_qgrp->splitq.bufq_sets)
520 			continue;
521 
522 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
523 			struct idpf_bufq_set *bufq_set =
524 				&rx_qgrp->splitq.bufq_sets[j];
525 
526 			idpf_rx_desc_rel_bufq(&bufq_set->bufq, dev);
527 		}
528 	}
529 }
530 
531 /**
532  * idpf_rx_buf_hw_update - Store the new tail and head values
533  * @bufq: queue to bump
534  * @val: new head index
535  */
536 static void idpf_rx_buf_hw_update(struct idpf_buf_queue *bufq, u32 val)
537 {
538 	bufq->next_to_use = val;
539 
540 	if (unlikely(!bufq->tail))
541 		return;
542 
543 	/* writel has an implicit memory barrier */
544 	writel(val, bufq->tail);
545 }
546 
547 /**
548  * idpf_rx_hdr_buf_alloc_all - Allocate memory for header buffers
549  * @bufq: ring to use
550  *
551  * Returns 0 on success, negative on failure.
552  */
553 static int idpf_rx_hdr_buf_alloc_all(struct idpf_buf_queue *bufq)
554 {
555 	struct libeth_fq fq = {
556 		.count	= bufq->desc_count,
557 		.type	= LIBETH_FQE_HDR,
558 		.xdp	= idpf_xdp_enabled(bufq->q_vector->vport),
559 		.nid	= idpf_q_vector_to_mem(bufq->q_vector),
560 	};
561 	int ret;
562 
563 	ret = libeth_rx_fq_create(&fq, &bufq->q_vector->napi);
564 	if (ret)
565 		return ret;
566 
567 	bufq->hdr_pp = fq.pp;
568 	bufq->hdr_buf = fq.fqes;
569 	bufq->hdr_truesize = fq.truesize;
570 	bufq->rx_hbuf_size = fq.buf_len;
571 
572 	return 0;
573 }
574 
575 /**
576  * idpf_post_buf_refill - Post buffer id to refill queue
577  * @refillq: refill queue to post to
578  * @buf_id: buffer id to post
579  */
580 static void idpf_post_buf_refill(struct idpf_sw_queue *refillq, u16 buf_id)
581 {
582 	u32 nta = refillq->next_to_use;
583 
584 	/* store the buffer ID and the SW maintained GEN bit to the refillq */
585 	refillq->ring[nta] =
586 		FIELD_PREP(IDPF_RFL_BI_BUFID_M, buf_id) |
587 		FIELD_PREP(IDPF_RFL_BI_GEN_M,
588 			   idpf_queue_has(GEN_CHK, refillq));
589 
590 	if (unlikely(++nta == refillq->desc_count)) {
591 		nta = 0;
592 		idpf_queue_change(GEN_CHK, refillq);
593 	}
594 
595 	refillq->next_to_use = nta;
596 }
597 
598 /**
599  * idpf_rx_post_buf_desc - Post buffer to bufq descriptor ring
600  * @bufq: buffer queue to post to
601  * @buf_id: buffer id to post
602  *
603  * Returns false if buffer could not be allocated, true otherwise.
604  */
605 static bool idpf_rx_post_buf_desc(struct idpf_buf_queue *bufq, u16 buf_id)
606 {
607 	struct virtchnl2_splitq_rx_buf_desc *splitq_rx_desc = NULL;
608 	struct libeth_fq_fp fq = {
609 		.count	= bufq->desc_count,
610 	};
611 	u16 nta = bufq->next_to_alloc;
612 	dma_addr_t addr;
613 
614 	splitq_rx_desc = &bufq->split_buf[nta];
615 
616 	if (idpf_queue_has(HSPLIT_EN, bufq)) {
617 		fq.pp = bufq->hdr_pp;
618 		fq.fqes = bufq->hdr_buf;
619 		fq.truesize = bufq->hdr_truesize;
620 
621 		addr = libeth_rx_alloc(&fq, buf_id);
622 		if (addr == DMA_MAPPING_ERROR)
623 			return false;
624 
625 		splitq_rx_desc->hdr_addr = cpu_to_le64(addr);
626 	}
627 
628 	fq.pp = bufq->pp;
629 	fq.fqes = bufq->buf;
630 	fq.truesize = bufq->truesize;
631 
632 	addr = libeth_rx_alloc(&fq, buf_id);
633 	if (addr == DMA_MAPPING_ERROR)
634 		return false;
635 
636 	splitq_rx_desc->pkt_addr = cpu_to_le64(addr);
637 	splitq_rx_desc->qword0.buf_id = cpu_to_le16(buf_id);
638 
639 	nta++;
640 	if (unlikely(nta == bufq->desc_count))
641 		nta = 0;
642 	bufq->next_to_alloc = nta;
643 
644 	return true;
645 }
646 
647 /**
648  * idpf_rx_post_init_bufs - Post initial buffers to bufq
649  * @bufq: buffer queue to post working set to
650  * @working_set: number of buffers to put in working set
651  *
652  * Returns true if @working_set bufs were posted successfully, false otherwise.
653  */
654 static bool idpf_rx_post_init_bufs(struct idpf_buf_queue *bufq,
655 				   u16 working_set)
656 {
657 	int i;
658 
659 	for (i = 0; i < working_set; i++) {
660 		if (!idpf_rx_post_buf_desc(bufq, i))
661 			return false;
662 	}
663 
664 	idpf_rx_buf_hw_update(bufq, ALIGN_DOWN(bufq->next_to_alloc,
665 					       IDPF_RX_BUF_STRIDE));
666 
667 	return true;
668 }
669 
670 /**
671  * idpf_rx_buf_alloc_singleq - Allocate memory for all buffer resources
672  * @rxq: queue for which the buffers are allocated
673  *
674  * Return: 0 on success, -ENOMEM on failure.
675  */
676 static int idpf_rx_buf_alloc_singleq(struct idpf_rx_queue *rxq)
677 {
678 	if (idpf_rx_singleq_buf_hw_alloc_all(rxq, rxq->desc_count - 1))
679 		goto err;
680 
681 	return 0;
682 
683 err:
684 	idpf_rx_buf_rel_all(rxq);
685 
686 	return -ENOMEM;
687 }
688 
689 /**
690  * idpf_rx_bufs_init_singleq - Initialize page pool and allocate Rx bufs
691  * @rxq: buffer queue to create page pool for
692  *
693  * Return: 0 on success, -errno on failure.
694  */
695 static int idpf_rx_bufs_init_singleq(struct idpf_rx_queue *rxq)
696 {
697 	struct libeth_fq fq = {
698 		.count	= rxq->desc_count,
699 		.type	= LIBETH_FQE_MTU,
700 		.nid	= idpf_q_vector_to_mem(rxq->q_vector),
701 	};
702 	int ret;
703 
704 	ret = libeth_rx_fq_create(&fq, &rxq->q_vector->napi);
705 	if (ret)
706 		return ret;
707 
708 	rxq->pp = fq.pp;
709 	rxq->rx_buf = fq.fqes;
710 	rxq->truesize = fq.truesize;
711 	rxq->rx_buf_size = fq.buf_len;
712 
713 	return idpf_rx_buf_alloc_singleq(rxq);
714 }
715 
716 /**
717  * idpf_rx_buf_alloc_all - Allocate memory for all buffer resources
718  * @rxbufq: queue for which the buffers are allocated
719  *
720  * Returns 0 on success, negative on failure
721  */
722 static int idpf_rx_buf_alloc_all(struct idpf_buf_queue *rxbufq)
723 {
724 	int err = 0;
725 
726 	if (idpf_queue_has(HSPLIT_EN, rxbufq)) {
727 		err = idpf_rx_hdr_buf_alloc_all(rxbufq);
728 		if (err)
729 			goto rx_buf_alloc_all_out;
730 	}
731 
732 	/* Allocate buffers to be given to HW.	 */
733 	if (!idpf_rx_post_init_bufs(rxbufq, IDPF_RX_BUFQ_WORKING_SET(rxbufq)))
734 		err = -ENOMEM;
735 
736 rx_buf_alloc_all_out:
737 	if (err)
738 		idpf_rx_buf_rel_bufq(rxbufq);
739 
740 	return err;
741 }
742 
743 /**
744  * idpf_rx_bufs_init - Initialize page pool, allocate rx bufs, and post to HW
745  * @bufq: buffer queue to create page pool for
746  * @type: type of Rx buffers to allocate
747  *
748  * Returns 0 on success, negative on failure
749  */
750 static int idpf_rx_bufs_init(struct idpf_buf_queue *bufq,
751 			     enum libeth_fqe_type type)
752 {
753 	struct libeth_fq fq = {
754 		.truesize	= bufq->truesize,
755 		.count		= bufq->desc_count,
756 		.type		= type,
757 		.hsplit		= idpf_queue_has(HSPLIT_EN, bufq),
758 		.xdp		= idpf_xdp_enabled(bufq->q_vector->vport),
759 		.nid		= idpf_q_vector_to_mem(bufq->q_vector),
760 	};
761 	int ret;
762 
763 	if (idpf_queue_has(XSK, bufq))
764 		return idpf_xskfq_init(bufq);
765 
766 	ret = libeth_rx_fq_create(&fq, &bufq->q_vector->napi);
767 	if (ret)
768 		return ret;
769 
770 	bufq->pp = fq.pp;
771 	bufq->buf = fq.fqes;
772 	bufq->truesize = fq.truesize;
773 	bufq->rx_buf_size = fq.buf_len;
774 
775 	return idpf_rx_buf_alloc_all(bufq);
776 }
777 
778 /**
779  * idpf_rx_bufs_init_all - Initialize all RX bufs
780  * @vport: virtual port struct
781  *
782  * Returns 0 on success, negative on failure
783  */
784 int idpf_rx_bufs_init_all(struct idpf_vport *vport)
785 {
786 	bool split = idpf_is_queue_model_split(vport->rxq_model);
787 	int i, j, err;
788 
789 	idpf_xdp_copy_prog_to_rqs(vport, vport->xdp_prog);
790 
791 	for (i = 0; i < vport->num_rxq_grp; i++) {
792 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
793 		u32 truesize = 0;
794 
795 		/* Allocate bufs for the rxq itself in singleq */
796 		if (!split) {
797 			int num_rxq = rx_qgrp->singleq.num_rxq;
798 
799 			for (j = 0; j < num_rxq; j++) {
800 				struct idpf_rx_queue *q;
801 
802 				q = rx_qgrp->singleq.rxqs[j];
803 				err = idpf_rx_bufs_init_singleq(q);
804 				if (err)
805 					return err;
806 			}
807 
808 			continue;
809 		}
810 
811 		/* Otherwise, allocate bufs for the buffer queues */
812 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
813 			enum libeth_fqe_type type;
814 			struct idpf_buf_queue *q;
815 
816 			q = &rx_qgrp->splitq.bufq_sets[j].bufq;
817 			q->truesize = truesize;
818 
819 			type = truesize ? LIBETH_FQE_SHORT : LIBETH_FQE_MTU;
820 
821 			err = idpf_rx_bufs_init(q, type);
822 			if (err)
823 				return err;
824 
825 			truesize = q->truesize >> 1;
826 		}
827 	}
828 
829 	return 0;
830 }
831 
832 /**
833  * idpf_rx_desc_alloc - Allocate queue Rx resources
834  * @vport: vport to allocate resources for
835  * @rxq: Rx queue for which the resources are setup
836  *
837  * Returns 0 on success, negative on failure
838  */
839 static int idpf_rx_desc_alloc(const struct idpf_vport *vport,
840 			      struct idpf_rx_queue *rxq)
841 {
842 	struct device *dev = &vport->adapter->pdev->dev;
843 
844 	rxq->size = rxq->desc_count * sizeof(union virtchnl2_rx_desc);
845 
846 	/* Allocate descriptors and also round up to nearest 4K */
847 	rxq->size = ALIGN(rxq->size, 4096);
848 	rxq->desc_ring = dmam_alloc_coherent(dev, rxq->size,
849 					     &rxq->dma, GFP_KERNEL);
850 	if (!rxq->desc_ring) {
851 		dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
852 			rxq->size);
853 		return -ENOMEM;
854 	}
855 
856 	rxq->next_to_alloc = 0;
857 	rxq->next_to_clean = 0;
858 	rxq->next_to_use = 0;
859 	idpf_queue_set(GEN_CHK, rxq);
860 
861 	idpf_xsk_setup_queue(vport, rxq, VIRTCHNL2_QUEUE_TYPE_RX);
862 
863 	return 0;
864 }
865 
866 /**
867  * idpf_bufq_desc_alloc - Allocate buffer queue descriptor ring
868  * @vport: vport to allocate resources for
869  * @bufq: buffer queue for which the resources are set up
870  *
871  * Return: 0 on success, -ENOMEM on failure.
872  */
873 static int idpf_bufq_desc_alloc(const struct idpf_vport *vport,
874 				struct idpf_buf_queue *bufq)
875 {
876 	struct device *dev = &vport->adapter->pdev->dev;
877 
878 	bufq->size = array_size(bufq->desc_count, sizeof(*bufq->split_buf));
879 
880 	bufq->split_buf = dma_alloc_coherent(dev, bufq->size, &bufq->dma,
881 					     GFP_KERNEL);
882 	if (!bufq->split_buf)
883 		return -ENOMEM;
884 
885 	bufq->next_to_alloc = 0;
886 	bufq->next_to_clean = 0;
887 	bufq->next_to_use = 0;
888 	idpf_queue_set(GEN_CHK, bufq);
889 
890 	idpf_xsk_setup_queue(vport, bufq, VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
891 
892 	return 0;
893 }
894 
895 /**
896  * idpf_rx_desc_alloc_all - allocate all RX queues resources
897  * @vport: virtual port structure
898  *
899  * Returns 0 on success, negative on failure
900  */
901 static int idpf_rx_desc_alloc_all(struct idpf_vport *vport)
902 {
903 	struct idpf_rxq_group *rx_qgrp;
904 	int i, j, err;
905 	u16 num_rxq;
906 
907 	for (i = 0; i < vport->num_rxq_grp; i++) {
908 		rx_qgrp = &vport->rxq_grps[i];
909 		if (idpf_is_queue_model_split(vport->rxq_model))
910 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
911 		else
912 			num_rxq = rx_qgrp->singleq.num_rxq;
913 
914 		for (j = 0; j < num_rxq; j++) {
915 			struct idpf_rx_queue *q;
916 
917 			if (idpf_is_queue_model_split(vport->rxq_model))
918 				q = &rx_qgrp->splitq.rxq_sets[j]->rxq;
919 			else
920 				q = rx_qgrp->singleq.rxqs[j];
921 
922 			err = idpf_rx_desc_alloc(vport, q);
923 			if (err) {
924 				pci_err(vport->adapter->pdev,
925 					"Memory allocation for Rx Queue %u failed\n",
926 					i);
927 				goto err_out;
928 			}
929 		}
930 
931 		if (!idpf_is_queue_model_split(vport->rxq_model))
932 			continue;
933 
934 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
935 			struct idpf_buf_queue *q;
936 
937 			q = &rx_qgrp->splitq.bufq_sets[j].bufq;
938 
939 			err = idpf_bufq_desc_alloc(vport, q);
940 			if (err) {
941 				pci_err(vport->adapter->pdev,
942 					"Memory allocation for Rx Buffer Queue %u failed\n",
943 					i);
944 				goto err_out;
945 			}
946 		}
947 	}
948 
949 	return 0;
950 
951 err_out:
952 	idpf_rx_desc_rel_all(vport);
953 
954 	return err;
955 }
956 
957 static int idpf_init_queue_set(const struct idpf_queue_set *qs)
958 {
959 	const struct idpf_vport *vport = qs->vport;
960 	bool splitq;
961 	int err;
962 
963 	splitq = idpf_is_queue_model_split(vport->rxq_model);
964 
965 	for (u32 i = 0; i < qs->num; i++) {
966 		const struct idpf_queue_ptr *q = &qs->qs[i];
967 		struct idpf_buf_queue *bufq;
968 
969 		switch (q->type) {
970 		case VIRTCHNL2_QUEUE_TYPE_RX:
971 			err = idpf_rx_desc_alloc(vport, q->rxq);
972 			if (err)
973 				break;
974 
975 			err = idpf_xdp_rxq_info_init(q->rxq);
976 			if (err)
977 				break;
978 
979 			if (!splitq)
980 				err = idpf_rx_bufs_init_singleq(q->rxq);
981 
982 			break;
983 		case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
984 			bufq = q->bufq;
985 
986 			err = idpf_bufq_desc_alloc(vport, bufq);
987 			if (err)
988 				break;
989 
990 			for (u32 j = 0; j < bufq->q_vector->num_bufq; j++) {
991 				struct idpf_buf_queue * const *bufqs;
992 				enum libeth_fqe_type type;
993 				u32 ts;
994 
995 				bufqs = bufq->q_vector->bufq;
996 				if (bufqs[j] != bufq)
997 					continue;
998 
999 				if (j) {
1000 					type = LIBETH_FQE_SHORT;
1001 					ts = bufqs[j - 1]->truesize >> 1;
1002 				} else {
1003 					type = LIBETH_FQE_MTU;
1004 					ts = 0;
1005 				}
1006 
1007 				bufq->truesize = ts;
1008 
1009 				err = idpf_rx_bufs_init(bufq, type);
1010 				break;
1011 			}
1012 
1013 			break;
1014 		case VIRTCHNL2_QUEUE_TYPE_TX:
1015 			err = idpf_tx_desc_alloc(vport, q->txq);
1016 			break;
1017 		case VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION:
1018 			err = idpf_compl_desc_alloc(vport, q->complq);
1019 			break;
1020 		default:
1021 			continue;
1022 		}
1023 
1024 		if (err)
1025 			return err;
1026 	}
1027 
1028 	return 0;
1029 }
1030 
1031 static void idpf_clean_queue_set(const struct idpf_queue_set *qs)
1032 {
1033 	const struct idpf_vport *vport = qs->vport;
1034 	struct device *dev = vport->netdev->dev.parent;
1035 
1036 	for (u32 i = 0; i < qs->num; i++) {
1037 		const struct idpf_queue_ptr *q = &qs->qs[i];
1038 
1039 		switch (q->type) {
1040 		case VIRTCHNL2_QUEUE_TYPE_RX:
1041 			idpf_xdp_rxq_info_deinit(q->rxq, vport->rxq_model);
1042 			idpf_rx_desc_rel(q->rxq, dev, vport->rxq_model);
1043 			break;
1044 		case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
1045 			idpf_rx_desc_rel_bufq(q->bufq, dev);
1046 			break;
1047 		case VIRTCHNL2_QUEUE_TYPE_TX:
1048 			idpf_tx_desc_rel(q->txq);
1049 
1050 			if (idpf_queue_has(XDP, q->txq)) {
1051 				q->txq->pending = 0;
1052 				q->txq->xdp_tx = 0;
1053 			} else {
1054 				q->txq->txq_grp->num_completions_pending = 0;
1055 			}
1056 
1057 			writel(q->txq->next_to_use, q->txq->tail);
1058 			break;
1059 		case VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION:
1060 			idpf_compl_desc_rel(q->complq);
1061 			q->complq->num_completions = 0;
1062 			break;
1063 		default:
1064 			break;
1065 		}
1066 	}
1067 }
1068 
1069 static void idpf_qvec_ena_irq(struct idpf_q_vector *qv)
1070 {
1071 	if (qv->num_txq) {
1072 		u32 itr;
1073 
1074 		if (IDPF_ITR_IS_DYNAMIC(qv->tx_intr_mode))
1075 			itr = qv->vport->tx_itr_profile[qv->tx_dim.profile_ix];
1076 		else
1077 			itr = qv->tx_itr_value;
1078 
1079 		idpf_vport_intr_write_itr(qv, itr, true);
1080 	}
1081 
1082 	if (qv->num_rxq) {
1083 		u32 itr;
1084 
1085 		if (IDPF_ITR_IS_DYNAMIC(qv->rx_intr_mode))
1086 			itr = qv->vport->rx_itr_profile[qv->rx_dim.profile_ix];
1087 		else
1088 			itr = qv->rx_itr_value;
1089 
1090 		idpf_vport_intr_write_itr(qv, itr, false);
1091 	}
1092 
1093 	if (qv->num_txq || qv->num_rxq)
1094 		idpf_vport_intr_update_itr_ena_irq(qv);
1095 }
1096 
1097 /**
1098  * idpf_vector_to_queue_set - create a queue set associated with the given
1099  *			      queue vector
1100  * @qv: queue vector corresponding to the queue pair
1101  *
1102  * Returns a pointer to a dynamically allocated array of pointers to all
1103  * queues associated with a given queue vector (@qv).
1104  * Please note that the caller is responsible to free the memory allocated
1105  * by this function using kfree().
1106  *
1107  * Return: &idpf_queue_set on success, %NULL in case of error.
1108  */
1109 static struct idpf_queue_set *
1110 idpf_vector_to_queue_set(struct idpf_q_vector *qv)
1111 {
1112 	bool xdp = qv->vport->xdp_txq_offset && !qv->num_xsksq;
1113 	struct idpf_vport *vport = qv->vport;
1114 	struct idpf_queue_set *qs;
1115 	u32 num;
1116 
1117 	num = qv->num_rxq + qv->num_bufq + qv->num_txq + qv->num_complq;
1118 	num += xdp ? qv->num_rxq * 2 : qv->num_xsksq * 2;
1119 	if (!num)
1120 		return NULL;
1121 
1122 	qs = idpf_alloc_queue_set(vport, num);
1123 	if (!qs)
1124 		return NULL;
1125 
1126 	num = 0;
1127 
1128 	for (u32 i = 0; i < qv->num_bufq; i++) {
1129 		qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_RX_BUFFER;
1130 		qs->qs[num++].bufq = qv->bufq[i];
1131 	}
1132 
1133 	for (u32 i = 0; i < qv->num_rxq; i++) {
1134 		qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_RX;
1135 		qs->qs[num++].rxq = qv->rx[i];
1136 	}
1137 
1138 	for (u32 i = 0; i < qv->num_txq; i++) {
1139 		qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX;
1140 		qs->qs[num++].txq = qv->tx[i];
1141 	}
1142 
1143 	for (u32 i = 0; i < qv->num_complq; i++) {
1144 		qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
1145 		qs->qs[num++].complq = qv->complq[i];
1146 	}
1147 
1148 	if (!vport->xdp_txq_offset)
1149 		goto finalize;
1150 
1151 	if (xdp) {
1152 		for (u32 i = 0; i < qv->num_rxq; i++) {
1153 			u32 idx = vport->xdp_txq_offset + qv->rx[i]->idx;
1154 
1155 			qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX;
1156 			qs->qs[num++].txq = vport->txqs[idx];
1157 
1158 			qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
1159 			qs->qs[num++].complq = vport->txqs[idx]->complq;
1160 		}
1161 	} else {
1162 		for (u32 i = 0; i < qv->num_xsksq; i++) {
1163 			qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX;
1164 			qs->qs[num++].txq = qv->xsksq[i];
1165 
1166 			qs->qs[num].type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
1167 			qs->qs[num++].complq = qv->xsksq[i]->complq;
1168 		}
1169 	}
1170 
1171 finalize:
1172 	if (num != qs->num) {
1173 		kfree(qs);
1174 		return NULL;
1175 	}
1176 
1177 	return qs;
1178 }
1179 
1180 static int idpf_qp_enable(const struct idpf_queue_set *qs, u32 qid)
1181 {
1182 	struct idpf_vport *vport = qs->vport;
1183 	struct idpf_q_vector *q_vector;
1184 	int err;
1185 
1186 	q_vector = idpf_find_rxq_vec(vport, qid);
1187 
1188 	err = idpf_init_queue_set(qs);
1189 	if (err) {
1190 		netdev_err(vport->netdev, "Could not initialize queues in pair %u: %pe\n",
1191 			   qid, ERR_PTR(err));
1192 		return err;
1193 	}
1194 
1195 	if (!vport->xdp_txq_offset)
1196 		goto config;
1197 
1198 	q_vector->xsksq = kcalloc(DIV_ROUND_UP(vport->num_rxq_grp,
1199 					       vport->num_q_vectors),
1200 				  sizeof(*q_vector->xsksq), GFP_KERNEL);
1201 	if (!q_vector->xsksq)
1202 		return -ENOMEM;
1203 
1204 	for (u32 i = 0; i < qs->num; i++) {
1205 		const struct idpf_queue_ptr *q = &qs->qs[i];
1206 
1207 		if (q->type != VIRTCHNL2_QUEUE_TYPE_TX)
1208 			continue;
1209 
1210 		if (!idpf_queue_has(XSK, q->txq))
1211 			continue;
1212 
1213 		idpf_xsk_init_wakeup(q_vector);
1214 
1215 		q->txq->q_vector = q_vector;
1216 		q_vector->xsksq[q_vector->num_xsksq++] = q->txq;
1217 	}
1218 
1219 config:
1220 	err = idpf_send_config_queue_set_msg(qs);
1221 	if (err) {
1222 		netdev_err(vport->netdev, "Could not configure queues in pair %u: %pe\n",
1223 			   qid, ERR_PTR(err));
1224 		return err;
1225 	}
1226 
1227 	err = idpf_send_enable_queue_set_msg(qs);
1228 	if (err) {
1229 		netdev_err(vport->netdev, "Could not enable queues in pair %u: %pe\n",
1230 			   qid, ERR_PTR(err));
1231 		return err;
1232 	}
1233 
1234 	napi_enable(&q_vector->napi);
1235 	idpf_qvec_ena_irq(q_vector);
1236 
1237 	netif_start_subqueue(vport->netdev, qid);
1238 
1239 	return 0;
1240 }
1241 
1242 static int idpf_qp_disable(const struct idpf_queue_set *qs, u32 qid)
1243 {
1244 	struct idpf_vport *vport = qs->vport;
1245 	struct idpf_q_vector *q_vector;
1246 	int err;
1247 
1248 	q_vector = idpf_find_rxq_vec(vport, qid);
1249 	netif_stop_subqueue(vport->netdev, qid);
1250 
1251 	writel(0, q_vector->intr_reg.dyn_ctl);
1252 	napi_disable(&q_vector->napi);
1253 
1254 	err = idpf_send_disable_queue_set_msg(qs);
1255 	if (err) {
1256 		netdev_err(vport->netdev, "Could not disable queues in pair %u: %pe\n",
1257 			   qid, ERR_PTR(err));
1258 		return err;
1259 	}
1260 
1261 	idpf_clean_queue_set(qs);
1262 
1263 	kfree(q_vector->xsksq);
1264 	q_vector->num_xsksq = 0;
1265 
1266 	return 0;
1267 }
1268 
1269 /**
1270  * idpf_qp_switch - enable or disable queues associated with queue pair
1271  * @vport: vport to switch the pair for
1272  * @qid: index of the queue pair to switch
1273  * @en: whether to enable or disable the pair
1274  *
1275  * Return: 0 on success, -errno on failure.
1276  */
1277 int idpf_qp_switch(struct idpf_vport *vport, u32 qid, bool en)
1278 {
1279 	struct idpf_q_vector *q_vector = idpf_find_rxq_vec(vport, qid);
1280 	struct idpf_queue_set *qs __free(kfree) = NULL;
1281 
1282 	if (idpf_find_txq_vec(vport, qid) != q_vector)
1283 		return -EINVAL;
1284 
1285 	qs = idpf_vector_to_queue_set(q_vector);
1286 	if (!qs)
1287 		return -ENOMEM;
1288 
1289 	return en ? idpf_qp_enable(qs, qid) : idpf_qp_disable(qs, qid);
1290 }
1291 
1292 /**
1293  * idpf_txq_group_rel - Release all resources for txq groups
1294  * @vport: vport to release txq groups on
1295  */
1296 static void idpf_txq_group_rel(struct idpf_vport *vport)
1297 {
1298 	bool split, flow_sch_en;
1299 	int i, j;
1300 
1301 	if (!vport->txq_grps)
1302 		return;
1303 
1304 	split = idpf_is_queue_model_split(vport->txq_model);
1305 	flow_sch_en = !idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS,
1306 				       VIRTCHNL2_CAP_SPLITQ_QSCHED);
1307 
1308 	for (i = 0; i < vport->num_txq_grp; i++) {
1309 		struct idpf_txq_group *txq_grp = &vport->txq_grps[i];
1310 
1311 		for (j = 0; j < txq_grp->num_txq; j++) {
1312 			if (flow_sch_en) {
1313 				kfree(txq_grp->txqs[j]->refillq);
1314 				txq_grp->txqs[j]->refillq = NULL;
1315 			}
1316 
1317 			kfree(txq_grp->txqs[j]);
1318 			txq_grp->txqs[j] = NULL;
1319 		}
1320 
1321 		if (!split)
1322 			continue;
1323 
1324 		kfree(txq_grp->complq);
1325 		txq_grp->complq = NULL;
1326 	}
1327 	kfree(vport->txq_grps);
1328 	vport->txq_grps = NULL;
1329 }
1330 
1331 /**
1332  * idpf_rxq_sw_queue_rel - Release software queue resources
1333  * @rx_qgrp: rx queue group with software queues
1334  */
1335 static void idpf_rxq_sw_queue_rel(struct idpf_rxq_group *rx_qgrp)
1336 {
1337 	int i, j;
1338 
1339 	for (i = 0; i < rx_qgrp->vport->num_bufqs_per_qgrp; i++) {
1340 		struct idpf_bufq_set *bufq_set = &rx_qgrp->splitq.bufq_sets[i];
1341 
1342 		for (j = 0; j < bufq_set->num_refillqs; j++) {
1343 			kfree(bufq_set->refillqs[j].ring);
1344 			bufq_set->refillqs[j].ring = NULL;
1345 		}
1346 		kfree(bufq_set->refillqs);
1347 		bufq_set->refillqs = NULL;
1348 	}
1349 }
1350 
1351 /**
1352  * idpf_rxq_group_rel - Release all resources for rxq groups
1353  * @vport: vport to release rxq groups on
1354  */
1355 static void idpf_rxq_group_rel(struct idpf_vport *vport)
1356 {
1357 	int i;
1358 
1359 	if (!vport->rxq_grps)
1360 		return;
1361 
1362 	for (i = 0; i < vport->num_rxq_grp; i++) {
1363 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1364 		u16 num_rxq;
1365 		int j;
1366 
1367 		if (idpf_is_queue_model_split(vport->rxq_model)) {
1368 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
1369 			for (j = 0; j < num_rxq; j++) {
1370 				kfree(rx_qgrp->splitq.rxq_sets[j]);
1371 				rx_qgrp->splitq.rxq_sets[j] = NULL;
1372 			}
1373 
1374 			idpf_rxq_sw_queue_rel(rx_qgrp);
1375 			kfree(rx_qgrp->splitq.bufq_sets);
1376 			rx_qgrp->splitq.bufq_sets = NULL;
1377 		} else {
1378 			num_rxq = rx_qgrp->singleq.num_rxq;
1379 			for (j = 0; j < num_rxq; j++) {
1380 				kfree(rx_qgrp->singleq.rxqs[j]);
1381 				rx_qgrp->singleq.rxqs[j] = NULL;
1382 			}
1383 		}
1384 	}
1385 	kfree(vport->rxq_grps);
1386 	vport->rxq_grps = NULL;
1387 }
1388 
1389 /**
1390  * idpf_vport_queue_grp_rel_all - Release all queue groups
1391  * @vport: vport to release queue groups for
1392  */
1393 static void idpf_vport_queue_grp_rel_all(struct idpf_vport *vport)
1394 {
1395 	idpf_txq_group_rel(vport);
1396 	idpf_rxq_group_rel(vport);
1397 }
1398 
1399 /**
1400  * idpf_vport_queues_rel - Free memory for all queues
1401  * @vport: virtual port
1402  *
1403  * Free the memory allocated for queues associated to a vport
1404  */
1405 void idpf_vport_queues_rel(struct idpf_vport *vport)
1406 {
1407 	idpf_xdp_copy_prog_to_rqs(vport, NULL);
1408 
1409 	idpf_tx_desc_rel_all(vport);
1410 	idpf_rx_desc_rel_all(vport);
1411 
1412 	idpf_xdpsqs_put(vport);
1413 	idpf_vport_queue_grp_rel_all(vport);
1414 
1415 	kfree(vport->txqs);
1416 	vport->txqs = NULL;
1417 }
1418 
1419 /**
1420  * idpf_vport_init_fast_path_txqs - Initialize fast path txq array
1421  * @vport: vport to init txqs on
1422  *
1423  * We get a queue index from skb->queue_mapping and we need a fast way to
1424  * dereference the queue from queue groups.  This allows us to quickly pull a
1425  * txq based on a queue index.
1426  *
1427  * Returns 0 on success, negative on failure
1428  */
1429 static int idpf_vport_init_fast_path_txqs(struct idpf_vport *vport)
1430 {
1431 	struct idpf_ptp_vport_tx_tstamp_caps *caps = vport->tx_tstamp_caps;
1432 	struct work_struct *tstamp_task = &vport->tstamp_task;
1433 	int i, j, k = 0;
1434 
1435 	vport->txqs = kcalloc(vport->num_txq, sizeof(*vport->txqs),
1436 			      GFP_KERNEL);
1437 
1438 	if (!vport->txqs)
1439 		return -ENOMEM;
1440 
1441 	for (i = 0; i < vport->num_txq_grp; i++) {
1442 		struct idpf_txq_group *tx_grp = &vport->txq_grps[i];
1443 
1444 		for (j = 0; j < tx_grp->num_txq; j++, k++) {
1445 			vport->txqs[k] = tx_grp->txqs[j];
1446 			vport->txqs[k]->idx = k;
1447 
1448 			if (!caps)
1449 				continue;
1450 
1451 			vport->txqs[k]->cached_tstamp_caps = caps;
1452 			vport->txqs[k]->tstamp_task = tstamp_task;
1453 		}
1454 	}
1455 
1456 	return 0;
1457 }
1458 
1459 /**
1460  * idpf_vport_init_num_qs - Initialize number of queues
1461  * @vport: vport to initialize queues
1462  * @vport_msg: data to be filled into vport
1463  */
1464 void idpf_vport_init_num_qs(struct idpf_vport *vport,
1465 			    struct virtchnl2_create_vport *vport_msg)
1466 {
1467 	struct idpf_vport_user_config_data *config_data;
1468 	u16 idx = vport->idx;
1469 
1470 	config_data = &vport->adapter->vport_config[idx]->user_config;
1471 	vport->num_txq = le16_to_cpu(vport_msg->num_tx_q);
1472 	vport->num_rxq = le16_to_cpu(vport_msg->num_rx_q);
1473 	/* number of txqs and rxqs in config data will be zeros only in the
1474 	 * driver load path and we dont update them there after
1475 	 */
1476 	if (!config_data->num_req_tx_qs && !config_data->num_req_rx_qs) {
1477 		config_data->num_req_tx_qs = le16_to_cpu(vport_msg->num_tx_q);
1478 		config_data->num_req_rx_qs = le16_to_cpu(vport_msg->num_rx_q);
1479 	}
1480 
1481 	if (idpf_is_queue_model_split(vport->txq_model))
1482 		vport->num_complq = le16_to_cpu(vport_msg->num_tx_complq);
1483 	if (idpf_is_queue_model_split(vport->rxq_model))
1484 		vport->num_bufq = le16_to_cpu(vport_msg->num_rx_bufq);
1485 
1486 	vport->xdp_prog = config_data->xdp_prog;
1487 	if (idpf_xdp_enabled(vport)) {
1488 		vport->xdp_txq_offset = config_data->num_req_tx_qs;
1489 		vport->num_xdp_txq = le16_to_cpu(vport_msg->num_tx_q) -
1490 				     vport->xdp_txq_offset;
1491 		vport->xdpsq_share = libeth_xdpsq_shared(vport->num_xdp_txq);
1492 	} else {
1493 		vport->xdp_txq_offset = 0;
1494 		vport->num_xdp_txq = 0;
1495 		vport->xdpsq_share = false;
1496 	}
1497 
1498 	/* Adjust number of buffer queues per Rx queue group. */
1499 	if (!idpf_is_queue_model_split(vport->rxq_model)) {
1500 		vport->num_bufqs_per_qgrp = 0;
1501 
1502 		return;
1503 	}
1504 
1505 	vport->num_bufqs_per_qgrp = IDPF_MAX_BUFQS_PER_RXQ_GRP;
1506 }
1507 
1508 /**
1509  * idpf_vport_calc_num_q_desc - Calculate number of queue groups
1510  * @vport: vport to calculate q groups for
1511  */
1512 void idpf_vport_calc_num_q_desc(struct idpf_vport *vport)
1513 {
1514 	struct idpf_vport_user_config_data *config_data;
1515 	int num_bufqs = vport->num_bufqs_per_qgrp;
1516 	u32 num_req_txq_desc, num_req_rxq_desc;
1517 	u16 idx = vport->idx;
1518 	int i;
1519 
1520 	config_data =  &vport->adapter->vport_config[idx]->user_config;
1521 	num_req_txq_desc = config_data->num_req_txq_desc;
1522 	num_req_rxq_desc = config_data->num_req_rxq_desc;
1523 
1524 	vport->complq_desc_count = 0;
1525 	if (num_req_txq_desc) {
1526 		vport->txq_desc_count = num_req_txq_desc;
1527 		if (idpf_is_queue_model_split(vport->txq_model)) {
1528 			vport->complq_desc_count = num_req_txq_desc;
1529 			if (vport->complq_desc_count < IDPF_MIN_TXQ_COMPLQ_DESC)
1530 				vport->complq_desc_count =
1531 					IDPF_MIN_TXQ_COMPLQ_DESC;
1532 		}
1533 	} else {
1534 		vport->txq_desc_count =	IDPF_DFLT_TX_Q_DESC_COUNT;
1535 		if (idpf_is_queue_model_split(vport->txq_model))
1536 			vport->complq_desc_count =
1537 				IDPF_DFLT_TX_COMPLQ_DESC_COUNT;
1538 	}
1539 
1540 	if (num_req_rxq_desc)
1541 		vport->rxq_desc_count = num_req_rxq_desc;
1542 	else
1543 		vport->rxq_desc_count = IDPF_DFLT_RX_Q_DESC_COUNT;
1544 
1545 	for (i = 0; i < num_bufqs; i++) {
1546 		if (!vport->bufq_desc_count[i])
1547 			vport->bufq_desc_count[i] =
1548 				IDPF_RX_BUFQ_DESC_COUNT(vport->rxq_desc_count,
1549 							num_bufqs);
1550 	}
1551 }
1552 
1553 /**
1554  * idpf_vport_calc_total_qs - Calculate total number of queues
1555  * @adapter: private data struct
1556  * @vport_idx: vport idx to retrieve vport pointer
1557  * @vport_msg: message to fill with data
1558  * @max_q: vport max queue info
1559  *
1560  * Return 0 on success, error value on failure.
1561  */
1562 int idpf_vport_calc_total_qs(struct idpf_adapter *adapter, u16 vport_idx,
1563 			     struct virtchnl2_create_vport *vport_msg,
1564 			     struct idpf_vport_max_q *max_q)
1565 {
1566 	int dflt_splitq_txq_grps = 0, dflt_singleq_txqs = 0;
1567 	int dflt_splitq_rxq_grps = 0, dflt_singleq_rxqs = 0;
1568 	u16 num_req_tx_qs = 0, num_req_rx_qs = 0;
1569 	struct idpf_vport_user_config_data *user;
1570 	struct idpf_vport_config *vport_config;
1571 	u16 num_txq_grps, num_rxq_grps;
1572 	u32 num_qs, num_xdpsq;
1573 
1574 	vport_config = adapter->vport_config[vport_idx];
1575 	if (vport_config) {
1576 		num_req_tx_qs = vport_config->user_config.num_req_tx_qs;
1577 		num_req_rx_qs = vport_config->user_config.num_req_rx_qs;
1578 	} else {
1579 		u32 num_cpus = netif_get_num_default_rss_queues();
1580 
1581 		dflt_splitq_txq_grps = min_t(int, max_q->max_txq, num_cpus);
1582 		dflt_singleq_txqs = min_t(int, max_q->max_txq, num_cpus);
1583 		dflt_splitq_rxq_grps = min_t(int, max_q->max_rxq, num_cpus);
1584 		dflt_singleq_rxqs = min_t(int, max_q->max_rxq, num_cpus);
1585 	}
1586 
1587 	if (idpf_is_queue_model_split(le16_to_cpu(vport_msg->txq_model))) {
1588 		num_txq_grps = num_req_tx_qs ? num_req_tx_qs : dflt_splitq_txq_grps;
1589 		vport_msg->num_tx_complq = cpu_to_le16(num_txq_grps *
1590 						       IDPF_COMPLQ_PER_GROUP);
1591 		vport_msg->num_tx_q = cpu_to_le16(num_txq_grps *
1592 						  IDPF_DFLT_SPLITQ_TXQ_PER_GROUP);
1593 	} else {
1594 		num_txq_grps = IDPF_DFLT_SINGLEQ_TX_Q_GROUPS;
1595 		num_qs = num_txq_grps * (num_req_tx_qs ? num_req_tx_qs :
1596 					 dflt_singleq_txqs);
1597 		vport_msg->num_tx_q = cpu_to_le16(num_qs);
1598 		vport_msg->num_tx_complq = 0;
1599 	}
1600 	if (idpf_is_queue_model_split(le16_to_cpu(vport_msg->rxq_model))) {
1601 		num_rxq_grps = num_req_rx_qs ? num_req_rx_qs : dflt_splitq_rxq_grps;
1602 		vport_msg->num_rx_bufq = cpu_to_le16(num_rxq_grps *
1603 						     IDPF_MAX_BUFQS_PER_RXQ_GRP);
1604 		vport_msg->num_rx_q = cpu_to_le16(num_rxq_grps *
1605 						  IDPF_DFLT_SPLITQ_RXQ_PER_GROUP);
1606 	} else {
1607 		num_rxq_grps = IDPF_DFLT_SINGLEQ_RX_Q_GROUPS;
1608 		num_qs = num_rxq_grps * (num_req_rx_qs ? num_req_rx_qs :
1609 					 dflt_singleq_rxqs);
1610 		vport_msg->num_rx_q = cpu_to_le16(num_qs);
1611 		vport_msg->num_rx_bufq = 0;
1612 	}
1613 
1614 	if (!vport_config)
1615 		return 0;
1616 
1617 	user = &vport_config->user_config;
1618 	user->num_req_rx_qs = le16_to_cpu(vport_msg->num_rx_q);
1619 	user->num_req_tx_qs = le16_to_cpu(vport_msg->num_tx_q);
1620 
1621 	if (vport_config->user_config.xdp_prog)
1622 		num_xdpsq = libeth_xdpsq_num(user->num_req_rx_qs,
1623 					     user->num_req_tx_qs,
1624 					     vport_config->max_q.max_txq);
1625 	else
1626 		num_xdpsq = 0;
1627 
1628 	vport_msg->num_tx_q = cpu_to_le16(user->num_req_tx_qs + num_xdpsq);
1629 	if (idpf_is_queue_model_split(le16_to_cpu(vport_msg->txq_model)))
1630 		vport_msg->num_tx_complq = vport_msg->num_tx_q;
1631 
1632 	return 0;
1633 }
1634 
1635 /**
1636  * idpf_vport_calc_num_q_groups - Calculate number of queue groups
1637  * @vport: vport to calculate q groups for
1638  */
1639 void idpf_vport_calc_num_q_groups(struct idpf_vport *vport)
1640 {
1641 	if (idpf_is_queue_model_split(vport->txq_model))
1642 		vport->num_txq_grp = vport->num_txq;
1643 	else
1644 		vport->num_txq_grp = IDPF_DFLT_SINGLEQ_TX_Q_GROUPS;
1645 
1646 	if (idpf_is_queue_model_split(vport->rxq_model))
1647 		vport->num_rxq_grp = vport->num_rxq;
1648 	else
1649 		vport->num_rxq_grp = IDPF_DFLT_SINGLEQ_RX_Q_GROUPS;
1650 }
1651 
1652 /**
1653  * idpf_vport_calc_numq_per_grp - Calculate number of queues per group
1654  * @vport: vport to calculate queues for
1655  * @num_txq: return parameter for number of TX queues
1656  * @num_rxq: return parameter for number of RX queues
1657  */
1658 static void idpf_vport_calc_numq_per_grp(struct idpf_vport *vport,
1659 					 u16 *num_txq, u16 *num_rxq)
1660 {
1661 	if (idpf_is_queue_model_split(vport->txq_model))
1662 		*num_txq = IDPF_DFLT_SPLITQ_TXQ_PER_GROUP;
1663 	else
1664 		*num_txq = vport->num_txq;
1665 
1666 	if (idpf_is_queue_model_split(vport->rxq_model))
1667 		*num_rxq = IDPF_DFLT_SPLITQ_RXQ_PER_GROUP;
1668 	else
1669 		*num_rxq = vport->num_rxq;
1670 }
1671 
1672 /**
1673  * idpf_rxq_set_descids - set the descids supported by this queue
1674  * @vport: virtual port data structure
1675  * @q: rx queue for which descids are set
1676  *
1677  */
1678 static void idpf_rxq_set_descids(const struct idpf_vport *vport,
1679 				 struct idpf_rx_queue *q)
1680 {
1681 	if (idpf_is_queue_model_split(vport->rxq_model))
1682 		return;
1683 
1684 	if (vport->base_rxd)
1685 		q->rxdids = VIRTCHNL2_RXDID_1_32B_BASE_M;
1686 	else
1687 		q->rxdids = VIRTCHNL2_RXDID_2_FLEX_SQ_NIC_M;
1688 }
1689 
1690 /**
1691  * idpf_txq_group_alloc - Allocate all txq group resources
1692  * @vport: vport to allocate txq groups for
1693  * @num_txq: number of txqs to allocate for each group
1694  *
1695  * Returns 0 on success, negative on failure
1696  */
1697 static int idpf_txq_group_alloc(struct idpf_vport *vport, u16 num_txq)
1698 {
1699 	bool split, flow_sch_en;
1700 	int i;
1701 
1702 	vport->txq_grps = kcalloc(vport->num_txq_grp,
1703 				  sizeof(*vport->txq_grps), GFP_KERNEL);
1704 	if (!vport->txq_grps)
1705 		return -ENOMEM;
1706 
1707 	split = idpf_is_queue_model_split(vport->txq_model);
1708 	flow_sch_en = !idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS,
1709 				       VIRTCHNL2_CAP_SPLITQ_QSCHED);
1710 
1711 	for (i = 0; i < vport->num_txq_grp; i++) {
1712 		struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1713 		struct idpf_adapter *adapter = vport->adapter;
1714 		int j;
1715 
1716 		tx_qgrp->vport = vport;
1717 		tx_qgrp->num_txq = num_txq;
1718 
1719 		for (j = 0; j < tx_qgrp->num_txq; j++) {
1720 			tx_qgrp->txqs[j] = kzalloc(sizeof(*tx_qgrp->txqs[j]),
1721 						   GFP_KERNEL);
1722 			if (!tx_qgrp->txqs[j])
1723 				goto err_alloc;
1724 		}
1725 
1726 		for (j = 0; j < tx_qgrp->num_txq; j++) {
1727 			struct idpf_tx_queue *q = tx_qgrp->txqs[j];
1728 
1729 			q->dev = &adapter->pdev->dev;
1730 			q->desc_count = vport->txq_desc_count;
1731 			q->tx_max_bufs = idpf_get_max_tx_bufs(adapter);
1732 			q->tx_min_pkt_len = idpf_get_min_tx_pkt_len(adapter);
1733 			q->netdev = vport->netdev;
1734 			q->txq_grp = tx_qgrp;
1735 			q->rel_q_id = j;
1736 
1737 			if (!split) {
1738 				q->clean_budget = vport->compln_clean_budget;
1739 				idpf_queue_assign(CRC_EN, q,
1740 						  vport->crc_enable);
1741 			}
1742 
1743 			if (!flow_sch_en)
1744 				continue;
1745 
1746 			idpf_queue_set(FLOW_SCH_EN, q);
1747 
1748 			q->refillq = kzalloc(sizeof(*q->refillq), GFP_KERNEL);
1749 			if (!q->refillq)
1750 				goto err_alloc;
1751 
1752 			idpf_queue_set(GEN_CHK, q->refillq);
1753 			idpf_queue_set(RFL_GEN_CHK, q->refillq);
1754 		}
1755 
1756 		if (!split)
1757 			continue;
1758 
1759 		tx_qgrp->complq = kcalloc(IDPF_COMPLQ_PER_GROUP,
1760 					  sizeof(*tx_qgrp->complq),
1761 					  GFP_KERNEL);
1762 		if (!tx_qgrp->complq)
1763 			goto err_alloc;
1764 
1765 		tx_qgrp->complq->desc_count = vport->complq_desc_count;
1766 		tx_qgrp->complq->txq_grp = tx_qgrp;
1767 		tx_qgrp->complq->netdev = vport->netdev;
1768 		tx_qgrp->complq->clean_budget = vport->compln_clean_budget;
1769 
1770 		if (flow_sch_en)
1771 			idpf_queue_set(FLOW_SCH_EN, tx_qgrp->complq);
1772 	}
1773 
1774 	return 0;
1775 
1776 err_alloc:
1777 	idpf_txq_group_rel(vport);
1778 
1779 	return -ENOMEM;
1780 }
1781 
1782 /**
1783  * idpf_rxq_group_alloc - Allocate all rxq group resources
1784  * @vport: vport to allocate rxq groups for
1785  * @num_rxq: number of rxqs to allocate for each group
1786  *
1787  * Returns 0 on success, negative on failure
1788  */
1789 static int idpf_rxq_group_alloc(struct idpf_vport *vport, u16 num_rxq)
1790 {
1791 	int i, k, err = 0;
1792 	bool hs;
1793 
1794 	vport->rxq_grps = kcalloc(vport->num_rxq_grp,
1795 				  sizeof(struct idpf_rxq_group), GFP_KERNEL);
1796 	if (!vport->rxq_grps)
1797 		return -ENOMEM;
1798 
1799 	hs = idpf_vport_get_hsplit(vport) == ETHTOOL_TCP_DATA_SPLIT_ENABLED;
1800 
1801 	for (i = 0; i < vport->num_rxq_grp; i++) {
1802 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1803 		int j;
1804 
1805 		rx_qgrp->vport = vport;
1806 		if (!idpf_is_queue_model_split(vport->rxq_model)) {
1807 			rx_qgrp->singleq.num_rxq = num_rxq;
1808 			for (j = 0; j < num_rxq; j++) {
1809 				rx_qgrp->singleq.rxqs[j] =
1810 						kzalloc(sizeof(*rx_qgrp->singleq.rxqs[j]),
1811 							GFP_KERNEL);
1812 				if (!rx_qgrp->singleq.rxqs[j]) {
1813 					err = -ENOMEM;
1814 					goto err_alloc;
1815 				}
1816 			}
1817 			goto skip_splitq_rx_init;
1818 		}
1819 		rx_qgrp->splitq.num_rxq_sets = num_rxq;
1820 
1821 		for (j = 0; j < num_rxq; j++) {
1822 			rx_qgrp->splitq.rxq_sets[j] =
1823 				kzalloc(sizeof(struct idpf_rxq_set),
1824 					GFP_KERNEL);
1825 			if (!rx_qgrp->splitq.rxq_sets[j]) {
1826 				err = -ENOMEM;
1827 				goto err_alloc;
1828 			}
1829 		}
1830 
1831 		rx_qgrp->splitq.bufq_sets = kcalloc(vport->num_bufqs_per_qgrp,
1832 						    sizeof(struct idpf_bufq_set),
1833 						    GFP_KERNEL);
1834 		if (!rx_qgrp->splitq.bufq_sets) {
1835 			err = -ENOMEM;
1836 			goto err_alloc;
1837 		}
1838 
1839 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
1840 			struct idpf_bufq_set *bufq_set =
1841 				&rx_qgrp->splitq.bufq_sets[j];
1842 			int swq_size = sizeof(struct idpf_sw_queue);
1843 			struct idpf_buf_queue *q;
1844 
1845 			q = &rx_qgrp->splitq.bufq_sets[j].bufq;
1846 			q->desc_count = vport->bufq_desc_count[j];
1847 			q->rx_buffer_low_watermark = IDPF_LOW_WATERMARK;
1848 
1849 			idpf_queue_assign(HSPLIT_EN, q, hs);
1850 
1851 			bufq_set->num_refillqs = num_rxq;
1852 			bufq_set->refillqs = kcalloc(num_rxq, swq_size,
1853 						     GFP_KERNEL);
1854 			if (!bufq_set->refillqs) {
1855 				err = -ENOMEM;
1856 				goto err_alloc;
1857 			}
1858 			for (k = 0; k < bufq_set->num_refillqs; k++) {
1859 				struct idpf_sw_queue *refillq =
1860 					&bufq_set->refillqs[k];
1861 
1862 				refillq->desc_count =
1863 					vport->bufq_desc_count[j];
1864 				idpf_queue_set(GEN_CHK, refillq);
1865 				idpf_queue_set(RFL_GEN_CHK, refillq);
1866 				refillq->ring = kcalloc(refillq->desc_count,
1867 							sizeof(*refillq->ring),
1868 							GFP_KERNEL);
1869 				if (!refillq->ring) {
1870 					err = -ENOMEM;
1871 					goto err_alloc;
1872 				}
1873 			}
1874 		}
1875 
1876 skip_splitq_rx_init:
1877 		for (j = 0; j < num_rxq; j++) {
1878 			struct idpf_rx_queue *q;
1879 
1880 			if (!idpf_is_queue_model_split(vport->rxq_model)) {
1881 				q = rx_qgrp->singleq.rxqs[j];
1882 				goto setup_rxq;
1883 			}
1884 			q = &rx_qgrp->splitq.rxq_sets[j]->rxq;
1885 			rx_qgrp->splitq.rxq_sets[j]->refillq[0] =
1886 			      &rx_qgrp->splitq.bufq_sets[0].refillqs[j];
1887 			if (vport->num_bufqs_per_qgrp > IDPF_SINGLE_BUFQ_PER_RXQ_GRP)
1888 				rx_qgrp->splitq.rxq_sets[j]->refillq[1] =
1889 				      &rx_qgrp->splitq.bufq_sets[1].refillqs[j];
1890 
1891 			idpf_queue_assign(HSPLIT_EN, q, hs);
1892 
1893 setup_rxq:
1894 			q->desc_count = vport->rxq_desc_count;
1895 			q->rx_ptype_lkup = vport->rx_ptype_lkup;
1896 			q->bufq_sets = rx_qgrp->splitq.bufq_sets;
1897 			q->idx = (i * num_rxq) + j;
1898 			q->rx_buffer_low_watermark = IDPF_LOW_WATERMARK;
1899 			q->rx_max_pkt_size = vport->netdev->mtu +
1900 							LIBETH_RX_LL_LEN;
1901 			idpf_rxq_set_descids(vport, q);
1902 		}
1903 	}
1904 
1905 err_alloc:
1906 	if (err)
1907 		idpf_rxq_group_rel(vport);
1908 
1909 	return err;
1910 }
1911 
1912 /**
1913  * idpf_vport_queue_grp_alloc_all - Allocate all queue groups/resources
1914  * @vport: vport with qgrps to allocate
1915  *
1916  * Returns 0 on success, negative on failure
1917  */
1918 static int idpf_vport_queue_grp_alloc_all(struct idpf_vport *vport)
1919 {
1920 	u16 num_txq, num_rxq;
1921 	int err;
1922 
1923 	idpf_vport_calc_numq_per_grp(vport, &num_txq, &num_rxq);
1924 
1925 	err = idpf_txq_group_alloc(vport, num_txq);
1926 	if (err)
1927 		goto err_out;
1928 
1929 	err = idpf_rxq_group_alloc(vport, num_rxq);
1930 	if (err)
1931 		goto err_out;
1932 
1933 	return 0;
1934 
1935 err_out:
1936 	idpf_vport_queue_grp_rel_all(vport);
1937 
1938 	return err;
1939 }
1940 
1941 /**
1942  * idpf_vport_queues_alloc - Allocate memory for all queues
1943  * @vport: virtual port
1944  *
1945  * Allocate memory for queues associated with a vport.  Returns 0 on success,
1946  * negative on failure.
1947  */
1948 int idpf_vport_queues_alloc(struct idpf_vport *vport)
1949 {
1950 	int err;
1951 
1952 	err = idpf_vport_queue_grp_alloc_all(vport);
1953 	if (err)
1954 		goto err_out;
1955 
1956 	err = idpf_vport_init_fast_path_txqs(vport);
1957 	if (err)
1958 		goto err_out;
1959 
1960 	err = idpf_xdpsqs_get(vport);
1961 	if (err)
1962 		goto err_out;
1963 
1964 	err = idpf_tx_desc_alloc_all(vport);
1965 	if (err)
1966 		goto err_out;
1967 
1968 	err = idpf_rx_desc_alloc_all(vport);
1969 	if (err)
1970 		goto err_out;
1971 
1972 	return 0;
1973 
1974 err_out:
1975 	idpf_vport_queues_rel(vport);
1976 
1977 	return err;
1978 }
1979 
1980 /**
1981  * idpf_tx_read_tstamp - schedule a work to read Tx timestamp value
1982  * @txq: queue to read the timestamp from
1983  * @skb: socket buffer to provide Tx timestamp value
1984  *
1985  * Schedule a work to read Tx timestamp value generated once the packet is
1986  * transmitted.
1987  */
1988 static void idpf_tx_read_tstamp(struct idpf_tx_queue *txq, struct sk_buff *skb)
1989 {
1990 	struct idpf_ptp_vport_tx_tstamp_caps *tx_tstamp_caps;
1991 	struct idpf_ptp_tx_tstamp_status *tx_tstamp_status;
1992 
1993 	tx_tstamp_caps = txq->cached_tstamp_caps;
1994 	spin_lock_bh(&tx_tstamp_caps->status_lock);
1995 
1996 	for (u32 i = 0; i < tx_tstamp_caps->num_entries; i++) {
1997 		tx_tstamp_status = &tx_tstamp_caps->tx_tstamp_status[i];
1998 		if (tx_tstamp_status->state != IDPF_PTP_FREE)
1999 			continue;
2000 
2001 		tx_tstamp_status->skb = skb;
2002 		tx_tstamp_status->state = IDPF_PTP_REQUEST;
2003 
2004 		/* Fetch timestamp from completion descriptor through
2005 		 * virtchnl msg to report to stack.
2006 		 */
2007 		queue_work(system_unbound_wq, txq->tstamp_task);
2008 		break;
2009 	}
2010 
2011 	spin_unlock_bh(&tx_tstamp_caps->status_lock);
2012 }
2013 
2014 #define idpf_tx_splitq_clean_bump_ntc(txq, ntc, desc, buf)	\
2015 do {								\
2016 	if (unlikely(++(ntc) == (txq)->desc_count)) {		\
2017 		ntc = 0;					\
2018 		buf = (txq)->tx_buf;				\
2019 		desc = &(txq)->flex_tx[0];			\
2020 	} else {						\
2021 		(buf)++;					\
2022 		(desc)++;					\
2023 	}							\
2024 } while (0)
2025 
2026 /**
2027  * idpf_tx_splitq_clean - Reclaim resources from buffer queue
2028  * @tx_q: Tx queue to clean
2029  * @end: queue index until which it should be cleaned
2030  * @napi_budget: Used to determine if we are in netpoll
2031  * @cleaned: pointer to stats struct to track cleaned packets/bytes
2032  * @descs_only: true if queue is using flow-based scheduling and should
2033  * not clean buffers at this time
2034  *
2035  * Cleans the queue descriptor ring. If the queue is using queue-based
2036  * scheduling, the buffers will be cleaned as well. If the queue is using
2037  * flow-based scheduling, only the descriptors are cleaned at this time.
2038  * Separate packet completion events will be reported on the completion queue,
2039  * and the buffers will be cleaned separately. The stats are not updated from
2040  * this function when using flow-based scheduling.
2041  */
2042 static void idpf_tx_splitq_clean(struct idpf_tx_queue *tx_q, u16 end,
2043 				 int napi_budget,
2044 				 struct libeth_sq_napi_stats *cleaned,
2045 				 bool descs_only)
2046 {
2047 	union idpf_tx_flex_desc *next_pending_desc = NULL;
2048 	union idpf_tx_flex_desc *tx_desc;
2049 	u32 ntc = tx_q->next_to_clean;
2050 	struct libeth_cq_pp cp = {
2051 		.dev	= tx_q->dev,
2052 		.ss	= cleaned,
2053 		.napi	= napi_budget,
2054 	};
2055 	struct idpf_tx_buf *tx_buf;
2056 
2057 	if (descs_only) {
2058 		/* Bump ring index to mark as cleaned. */
2059 		tx_q->next_to_clean = end;
2060 		return;
2061 	}
2062 
2063 	tx_desc = &tx_q->flex_tx[ntc];
2064 	next_pending_desc = &tx_q->flex_tx[end];
2065 	tx_buf = &tx_q->tx_buf[ntc];
2066 
2067 	while (tx_desc != next_pending_desc) {
2068 		u32 eop_idx;
2069 
2070 		/* If this entry in the ring was used as a context descriptor,
2071 		 * it's corresponding entry in the buffer ring is reserved. We
2072 		 * can skip this descriptor since there is no buffer to clean.
2073 		 */
2074 		if (tx_buf->type <= LIBETH_SQE_CTX)
2075 			goto fetch_next_txq_desc;
2076 
2077 		if (unlikely(tx_buf->type != LIBETH_SQE_SKB))
2078 			break;
2079 
2080 		eop_idx = tx_buf->rs_idx;
2081 		libeth_tx_complete(tx_buf, &cp);
2082 
2083 		/* unmap remaining buffers */
2084 		while (ntc != eop_idx) {
2085 			idpf_tx_splitq_clean_bump_ntc(tx_q, ntc,
2086 						      tx_desc, tx_buf);
2087 
2088 			/* unmap any remaining paged data */
2089 			libeth_tx_complete(tx_buf, &cp);
2090 		}
2091 
2092 fetch_next_txq_desc:
2093 		idpf_tx_splitq_clean_bump_ntc(tx_q, ntc, tx_desc, tx_buf);
2094 	}
2095 
2096 	tx_q->next_to_clean = ntc;
2097 }
2098 
2099 /**
2100  * idpf_tx_clean_bufs - clean flow scheduling TX queue buffers
2101  * @txq: queue to clean
2102  * @buf_id: packet's starting buffer ID, from completion descriptor
2103  * @cleaned: pointer to stats struct to track cleaned packets/bytes
2104  * @budget: Used to determine if we are in netpoll
2105  *
2106  * Clean all buffers associated with the packet starting at buf_id. Returns the
2107  * byte/segment count for the cleaned packet.
2108  */
2109 static void idpf_tx_clean_bufs(struct idpf_tx_queue *txq, u32 buf_id,
2110 			       struct libeth_sq_napi_stats *cleaned,
2111 			       int budget)
2112 {
2113 	struct idpf_tx_buf *tx_buf = NULL;
2114 	struct libeth_cq_pp cp = {
2115 		.dev	= txq->dev,
2116 		.ss	= cleaned,
2117 		.napi	= budget,
2118 	};
2119 
2120 	tx_buf = &txq->tx_buf[buf_id];
2121 	if (tx_buf->type == LIBETH_SQE_SKB) {
2122 		if (skb_shinfo(tx_buf->skb)->tx_flags & SKBTX_IN_PROGRESS)
2123 			idpf_tx_read_tstamp(txq, tx_buf->skb);
2124 
2125 		libeth_tx_complete(tx_buf, &cp);
2126 		idpf_post_buf_refill(txq->refillq, buf_id);
2127 	}
2128 
2129 	while (idpf_tx_buf_next(tx_buf) != IDPF_TXBUF_NULL) {
2130 		buf_id = idpf_tx_buf_next(tx_buf);
2131 
2132 		tx_buf = &txq->tx_buf[buf_id];
2133 		libeth_tx_complete(tx_buf, &cp);
2134 		idpf_post_buf_refill(txq->refillq, buf_id);
2135 	}
2136 }
2137 
2138 /**
2139  * idpf_tx_handle_rs_completion - clean a single packet and all of its buffers
2140  * whether on the buffer ring or in the hash table
2141  * @txq: Tx ring to clean
2142  * @desc: pointer to completion queue descriptor to extract completion
2143  * information from
2144  * @cleaned: pointer to stats struct to track cleaned packets/bytes
2145  * @budget: Used to determine if we are in netpoll
2146  *
2147  * Returns bytes/packets cleaned
2148  */
2149 static void idpf_tx_handle_rs_completion(struct idpf_tx_queue *txq,
2150 					 struct idpf_splitq_tx_compl_desc *desc,
2151 					 struct libeth_sq_napi_stats *cleaned,
2152 					 int budget)
2153 {
2154 	/* RS completion contains queue head for queue based scheduling or
2155 	 * completion tag for flow based scheduling.
2156 	 */
2157 	u16 rs_compl_val = le16_to_cpu(desc->common.q_head_compl_tag.q_head);
2158 
2159 	if (!idpf_queue_has(FLOW_SCH_EN, txq)) {
2160 		idpf_tx_splitq_clean(txq, rs_compl_val, budget, cleaned, false);
2161 		return;
2162 	}
2163 
2164 	idpf_tx_clean_bufs(txq, rs_compl_val, cleaned, budget);
2165 }
2166 
2167 /**
2168  * idpf_tx_clean_complq - Reclaim resources on completion queue
2169  * @complq: Tx ring to clean
2170  * @budget: Used to determine if we are in netpoll
2171  * @cleaned: returns number of packets cleaned
2172  *
2173  * Returns true if there's any budget left (e.g. the clean is finished)
2174  */
2175 static bool idpf_tx_clean_complq(struct idpf_compl_queue *complq, int budget,
2176 				 int *cleaned)
2177 {
2178 	struct idpf_splitq_tx_compl_desc *tx_desc;
2179 	s16 ntc = complq->next_to_clean;
2180 	struct idpf_netdev_priv *np;
2181 	unsigned int complq_budget;
2182 	bool complq_ok = true;
2183 	int i;
2184 
2185 	complq_budget = complq->clean_budget;
2186 	tx_desc = &complq->comp[ntc];
2187 	ntc -= complq->desc_count;
2188 
2189 	do {
2190 		struct libeth_sq_napi_stats cleaned_stats = { };
2191 		struct idpf_tx_queue *tx_q;
2192 		__le16 hw_head;
2193 		int rel_tx_qid;
2194 		u8 ctype;	/* completion type */
2195 		u16 gen;
2196 
2197 		/* if the descriptor isn't done, no work yet to do */
2198 		gen = le16_get_bits(tx_desc->common.qid_comptype_gen,
2199 				    IDPF_TXD_COMPLQ_GEN_M);
2200 		if (idpf_queue_has(GEN_CHK, complq) != gen)
2201 			break;
2202 
2203 		/* Find necessary info of TX queue to clean buffers */
2204 		rel_tx_qid = le16_get_bits(tx_desc->common.qid_comptype_gen,
2205 					   IDPF_TXD_COMPLQ_QID_M);
2206 		if (rel_tx_qid >= complq->txq_grp->num_txq ||
2207 		    !complq->txq_grp->txqs[rel_tx_qid]) {
2208 			netdev_err(complq->netdev, "TxQ not found\n");
2209 			goto fetch_next_desc;
2210 		}
2211 		tx_q = complq->txq_grp->txqs[rel_tx_qid];
2212 
2213 		/* Determine completion type */
2214 		ctype = le16_get_bits(tx_desc->common.qid_comptype_gen,
2215 				      IDPF_TXD_COMPLQ_COMPL_TYPE_M);
2216 		switch (ctype) {
2217 		case IDPF_TXD_COMPLT_RE:
2218 			hw_head = tx_desc->common.q_head_compl_tag.q_head;
2219 
2220 			idpf_tx_splitq_clean(tx_q, le16_to_cpu(hw_head),
2221 					     budget, &cleaned_stats, true);
2222 			break;
2223 		case IDPF_TXD_COMPLT_RS:
2224 			idpf_tx_handle_rs_completion(tx_q, tx_desc,
2225 						     &cleaned_stats, budget);
2226 			break;
2227 		default:
2228 			netdev_err(tx_q->netdev,
2229 				   "Unknown TX completion type: %d\n", ctype);
2230 			goto fetch_next_desc;
2231 		}
2232 
2233 		u64_stats_update_begin(&tx_q->stats_sync);
2234 		u64_stats_add(&tx_q->q_stats.packets, cleaned_stats.packets);
2235 		u64_stats_add(&tx_q->q_stats.bytes, cleaned_stats.bytes);
2236 		tx_q->cleaned_pkts += cleaned_stats.packets;
2237 		tx_q->cleaned_bytes += cleaned_stats.bytes;
2238 		complq->num_completions++;
2239 		u64_stats_update_end(&tx_q->stats_sync);
2240 
2241 fetch_next_desc:
2242 		tx_desc++;
2243 		ntc++;
2244 		if (unlikely(!ntc)) {
2245 			ntc -= complq->desc_count;
2246 			tx_desc = &complq->comp[0];
2247 			idpf_queue_change(GEN_CHK, complq);
2248 		}
2249 
2250 		prefetch(tx_desc);
2251 
2252 		/* update budget accounting */
2253 		complq_budget--;
2254 	} while (likely(complq_budget));
2255 
2256 	/* Store the state of the complq to be used later in deciding if a
2257 	 * TXQ can be started again
2258 	 */
2259 	if (unlikely(IDPF_TX_COMPLQ_PENDING(complq->txq_grp) >
2260 		     IDPF_TX_COMPLQ_OVERFLOW_THRESH(complq)))
2261 		complq_ok = false;
2262 
2263 	np = netdev_priv(complq->netdev);
2264 	for (i = 0; i < complq->txq_grp->num_txq; ++i) {
2265 		struct idpf_tx_queue *tx_q = complq->txq_grp->txqs[i];
2266 		struct netdev_queue *nq;
2267 		bool dont_wake;
2268 
2269 		/* We didn't clean anything on this queue, move along */
2270 		if (!tx_q->cleaned_bytes)
2271 			continue;
2272 
2273 		*cleaned += tx_q->cleaned_pkts;
2274 
2275 		/* Update BQL */
2276 		nq = netdev_get_tx_queue(tx_q->netdev, tx_q->idx);
2277 
2278 		dont_wake = !complq_ok || np->state != __IDPF_VPORT_UP ||
2279 			    !netif_carrier_ok(tx_q->netdev);
2280 		/* Check if the TXQ needs to and can be restarted */
2281 		__netif_txq_completed_wake(nq, tx_q->cleaned_pkts, tx_q->cleaned_bytes,
2282 					   IDPF_DESC_UNUSED(tx_q), IDPF_TX_WAKE_THRESH,
2283 					   dont_wake);
2284 
2285 		/* Reset cleaned stats for the next time this queue is
2286 		 * cleaned
2287 		 */
2288 		tx_q->cleaned_bytes = 0;
2289 		tx_q->cleaned_pkts = 0;
2290 	}
2291 
2292 	ntc += complq->desc_count;
2293 	complq->next_to_clean = ntc;
2294 
2295 	return !!complq_budget;
2296 }
2297 
2298 /**
2299  * idpf_wait_for_sw_marker_completion - wait for SW marker of disabled Tx queue
2300  * @txq: disabled Tx queue
2301  *
2302  * When Tx queue is requested for disabling, the CP sends a special completion
2303  * descriptor called "SW marker", meaning the queue is ready to be destroyed.
2304  * If, for some reason, the marker is not received within 500 ms, break the
2305  * polling to not hang the driver.
2306  */
2307 void idpf_wait_for_sw_marker_completion(const struct idpf_tx_queue *txq)
2308 {
2309 	struct idpf_compl_queue *complq;
2310 	unsigned long timeout;
2311 	bool flow, gen_flag;
2312 	u32 ntc;
2313 
2314 	if (!idpf_queue_has(SW_MARKER, txq))
2315 		return;
2316 
2317 	complq = idpf_queue_has(XDP, txq) ? txq->complq : txq->txq_grp->complq;
2318 	ntc = complq->next_to_clean;
2319 
2320 	flow = idpf_queue_has(FLOW_SCH_EN, complq);
2321 	gen_flag = idpf_queue_has(GEN_CHK, complq);
2322 
2323 	timeout = jiffies + msecs_to_jiffies(IDPF_WAIT_FOR_MARKER_TIMEO);
2324 
2325 	do {
2326 		struct idpf_splitq_4b_tx_compl_desc *tx_desc;
2327 		struct idpf_tx_queue *target;
2328 		u32 ctype_gen, id;
2329 
2330 		tx_desc = flow ? &complq->comp[ntc].common :
2331 			  &complq->comp_4b[ntc];
2332 		ctype_gen = le16_to_cpu(tx_desc->qid_comptype_gen);
2333 
2334 		if (!!(ctype_gen & IDPF_TXD_COMPLQ_GEN_M) != gen_flag) {
2335 			usleep_range(500, 1000);
2336 			continue;
2337 		}
2338 
2339 		if (FIELD_GET(IDPF_TXD_COMPLQ_COMPL_TYPE_M, ctype_gen) !=
2340 		    IDPF_TXD_COMPLT_SW_MARKER)
2341 			goto next;
2342 
2343 		id = FIELD_GET(IDPF_TXD_COMPLQ_QID_M, ctype_gen);
2344 		target = complq->txq_grp->txqs[id];
2345 
2346 		idpf_queue_clear(SW_MARKER, target);
2347 		if (target == txq)
2348 			break;
2349 
2350 next:
2351 		if (unlikely(++ntc == complq->desc_count)) {
2352 			ntc = 0;
2353 			gen_flag = !gen_flag;
2354 		}
2355 	} while (time_before(jiffies, timeout));
2356 
2357 	idpf_queue_assign(GEN_CHK, complq, gen_flag);
2358 	complq->next_to_clean = ntc;
2359 }
2360 
2361 /**
2362  * idpf_tx_splitq_build_ctb - populate command tag and size for queue
2363  * based scheduling descriptors
2364  * @desc: descriptor to populate
2365  * @params: pointer to tx params struct
2366  * @td_cmd: command to be filled in desc
2367  * @size: size of buffer
2368  */
2369 void idpf_tx_splitq_build_ctb(union idpf_tx_flex_desc *desc,
2370 			      struct idpf_tx_splitq_params *params,
2371 			      u16 td_cmd, u16 size)
2372 {
2373 	desc->q.qw1.cmd_dtype =
2374 		le16_encode_bits(params->dtype, IDPF_FLEX_TXD_QW1_DTYPE_M);
2375 	desc->q.qw1.cmd_dtype |=
2376 		le16_encode_bits(td_cmd, IDPF_FLEX_TXD_QW1_CMD_M);
2377 	desc->q.qw1.buf_size = cpu_to_le16(size);
2378 	desc->q.qw1.l2tags.l2tag1 = cpu_to_le16(params->td_tag);
2379 }
2380 
2381 /**
2382  * idpf_tx_splitq_build_flow_desc - populate command tag and size for flow
2383  * scheduling descriptors
2384  * @desc: descriptor to populate
2385  * @params: pointer to tx params struct
2386  * @td_cmd: command to be filled in desc
2387  * @size: size of buffer
2388  */
2389 void idpf_tx_splitq_build_flow_desc(union idpf_tx_flex_desc *desc,
2390 				    struct idpf_tx_splitq_params *params,
2391 				    u16 td_cmd, u16 size)
2392 {
2393 	*(u32 *)&desc->flow.qw1.cmd_dtype = (u8)(params->dtype | td_cmd);
2394 	desc->flow.qw1.rxr_bufsize = cpu_to_le16((u16)size);
2395 	desc->flow.qw1.compl_tag = cpu_to_le16(params->compl_tag);
2396 }
2397 
2398 /**
2399  * idpf_tx_splitq_has_room - check if enough Tx splitq resources are available
2400  * @tx_q: the queue to be checked
2401  * @descs_needed: number of descriptors required for this packet
2402  * @bufs_needed: number of Tx buffers required for this packet
2403  *
2404  * Return: 0 if no room available, 1 otherwise
2405  */
2406 static int idpf_txq_has_room(struct idpf_tx_queue *tx_q, u32 descs_needed,
2407 			     u32 bufs_needed)
2408 {
2409 	if (IDPF_DESC_UNUSED(tx_q) < descs_needed ||
2410 	    IDPF_TX_COMPLQ_PENDING(tx_q->txq_grp) >
2411 		IDPF_TX_COMPLQ_OVERFLOW_THRESH(tx_q->txq_grp->complq) ||
2412 	    idpf_tx_splitq_get_free_bufs(tx_q->refillq) < bufs_needed)
2413 		return 0;
2414 	return 1;
2415 }
2416 
2417 /**
2418  * idpf_tx_maybe_stop_splitq - 1st level check for Tx splitq stop conditions
2419  * @tx_q: the queue to be checked
2420  * @descs_needed: number of descriptors required for this packet
2421  * @bufs_needed: number of buffers needed for this packet
2422  *
2423  * Return: 0 if stop is not needed
2424  */
2425 static int idpf_tx_maybe_stop_splitq(struct idpf_tx_queue *tx_q,
2426 				     u32 descs_needed,
2427 				     u32 bufs_needed)
2428 {
2429 	/* Since we have multiple resources to check for splitq, our
2430 	 * start,stop_thrs becomes a boolean check instead of a count
2431 	 * threshold.
2432 	 */
2433 	if (netif_subqueue_maybe_stop(tx_q->netdev, tx_q->idx,
2434 				      idpf_txq_has_room(tx_q, descs_needed,
2435 							bufs_needed),
2436 				      1, 1))
2437 		return 0;
2438 
2439 	u64_stats_update_begin(&tx_q->stats_sync);
2440 	u64_stats_inc(&tx_q->q_stats.q_busy);
2441 	u64_stats_update_end(&tx_q->stats_sync);
2442 
2443 	return -EBUSY;
2444 }
2445 
2446 /**
2447  * idpf_tx_buf_hw_update - Store the new tail value
2448  * @tx_q: queue to bump
2449  * @val: new tail index
2450  * @xmit_more: more skb's pending
2451  *
2452  * The naming here is special in that 'hw' signals that this function is about
2453  * to do a register write to update our queue status. We know this can only
2454  * mean tail here as HW should be owning head for TX.
2455  */
2456 void idpf_tx_buf_hw_update(struct idpf_tx_queue *tx_q, u32 val,
2457 			   bool xmit_more)
2458 {
2459 	struct netdev_queue *nq;
2460 
2461 	nq = netdev_get_tx_queue(tx_q->netdev, tx_q->idx);
2462 	tx_q->next_to_use = val;
2463 
2464 	/* Force memory writes to complete before letting h/w
2465 	 * know there are new descriptors to fetch.  (Only
2466 	 * applicable for weak-ordered memory model archs,
2467 	 * such as IA-64).
2468 	 */
2469 	wmb();
2470 
2471 	/* notify HW of packet */
2472 	if (netif_xmit_stopped(nq) || !xmit_more)
2473 		writel(val, tx_q->tail);
2474 }
2475 
2476 /**
2477  * idpf_tx_res_count_required - get number of Tx resources needed for this pkt
2478  * @txq: queue to send buffer on
2479  * @skb: send buffer
2480  * @bufs_needed: (output) number of buffers needed for this skb.
2481  *
2482  * Return: number of data descriptors and buffers needed for this skb.
2483  */
2484 unsigned int idpf_tx_res_count_required(struct idpf_tx_queue *txq,
2485 					struct sk_buff *skb,
2486 					u32 *bufs_needed)
2487 {
2488 	const struct skb_shared_info *shinfo;
2489 	unsigned int count = 0, i;
2490 
2491 	count += !!skb_headlen(skb);
2492 
2493 	if (!skb_is_nonlinear(skb))
2494 		return count;
2495 
2496 	shinfo = skb_shinfo(skb);
2497 	*bufs_needed += shinfo->nr_frags;
2498 	for (i = 0; i < shinfo->nr_frags; i++) {
2499 		unsigned int size;
2500 
2501 		size = skb_frag_size(&shinfo->frags[i]);
2502 
2503 		/* We only need to use the idpf_size_to_txd_count check if the
2504 		 * fragment is going to span multiple descriptors,
2505 		 * i.e. size >= 16K.
2506 		 */
2507 		if (size >= SZ_16K)
2508 			count += idpf_size_to_txd_count(size);
2509 		else
2510 			count++;
2511 	}
2512 
2513 	if (idpf_chk_linearize(skb, txq->tx_max_bufs, count)) {
2514 		if (__skb_linearize(skb))
2515 			return 0;
2516 
2517 		count = idpf_size_to_txd_count(skb->len);
2518 		u64_stats_update_begin(&txq->stats_sync);
2519 		u64_stats_inc(&txq->q_stats.linearize);
2520 		u64_stats_update_end(&txq->stats_sync);
2521 	}
2522 
2523 	return count;
2524 }
2525 
2526 /**
2527  * idpf_tx_splitq_bump_ntu - adjust NTU and generation
2528  * @txq: the tx ring to wrap
2529  * @ntu: ring index to bump
2530  */
2531 static unsigned int idpf_tx_splitq_bump_ntu(struct idpf_tx_queue *txq, u16 ntu)
2532 {
2533 	ntu++;
2534 
2535 	if (ntu == txq->desc_count)
2536 		ntu = 0;
2537 
2538 	return ntu;
2539 }
2540 
2541 /**
2542  * idpf_tx_get_free_buf_id - get a free buffer ID from the refill queue
2543  * @refillq: refill queue to get buffer ID from
2544  * @buf_id: return buffer ID
2545  *
2546  * Return: true if a buffer ID was found, false if not
2547  */
2548 static bool idpf_tx_get_free_buf_id(struct idpf_sw_queue *refillq,
2549 				    u32 *buf_id)
2550 {
2551 	u32 ntc = refillq->next_to_clean;
2552 	u32 refill_desc;
2553 
2554 	refill_desc = refillq->ring[ntc];
2555 
2556 	if (unlikely(idpf_queue_has(RFL_GEN_CHK, refillq) !=
2557 		     !!(refill_desc & IDPF_RFL_BI_GEN_M)))
2558 		return false;
2559 
2560 	*buf_id = FIELD_GET(IDPF_RFL_BI_BUFID_M, refill_desc);
2561 
2562 	if (unlikely(++ntc == refillq->desc_count)) {
2563 		idpf_queue_change(RFL_GEN_CHK, refillq);
2564 		ntc = 0;
2565 	}
2566 
2567 	refillq->next_to_clean = ntc;
2568 
2569 	return true;
2570 }
2571 
2572 /**
2573  * idpf_tx_splitq_pkt_err_unmap - Unmap buffers and bump tail in case of error
2574  * @txq: Tx queue to unwind
2575  * @params: pointer to splitq params struct
2576  * @first: starting buffer for packet to unmap
2577  */
2578 static void idpf_tx_splitq_pkt_err_unmap(struct idpf_tx_queue *txq,
2579 					 struct idpf_tx_splitq_params *params,
2580 					 struct idpf_tx_buf *first)
2581 {
2582 	struct idpf_sw_queue *refillq = txq->refillq;
2583 	struct libeth_sq_napi_stats ss = { };
2584 	struct idpf_tx_buf *tx_buf = first;
2585 	struct libeth_cq_pp cp = {
2586 		.dev    = txq->dev,
2587 		.ss     = &ss,
2588 	};
2589 
2590 	u64_stats_update_begin(&txq->stats_sync);
2591 	u64_stats_inc(&txq->q_stats.dma_map_errs);
2592 	u64_stats_update_end(&txq->stats_sync);
2593 
2594 	libeth_tx_complete(tx_buf, &cp);
2595 	while (idpf_tx_buf_next(tx_buf) != IDPF_TXBUF_NULL) {
2596 		tx_buf = &txq->tx_buf[idpf_tx_buf_next(tx_buf)];
2597 		libeth_tx_complete(tx_buf, &cp);
2598 	}
2599 
2600 	/* Update tail in case netdev_xmit_more was previously true. */
2601 	idpf_tx_buf_hw_update(txq, params->prev_ntu, false);
2602 
2603 	if (!refillq)
2604 		return;
2605 
2606 	/* Restore refillq state to avoid leaking tags. */
2607 	if (params->prev_refill_gen != idpf_queue_has(RFL_GEN_CHK, refillq))
2608 		idpf_queue_change(RFL_GEN_CHK, refillq);
2609 	refillq->next_to_clean = params->prev_refill_ntc;
2610 }
2611 
2612 /**
2613  * idpf_tx_splitq_map - Build the Tx flex descriptor
2614  * @tx_q: queue to send buffer on
2615  * @params: pointer to splitq params struct
2616  * @first: first buffer info buffer to use
2617  *
2618  * This function loops over the skb data pointed to by *first
2619  * and gets a physical address for each memory location and programs
2620  * it and the length into the transmit flex descriptor.
2621  */
2622 static void idpf_tx_splitq_map(struct idpf_tx_queue *tx_q,
2623 			       struct idpf_tx_splitq_params *params,
2624 			       struct idpf_tx_buf *first)
2625 {
2626 	union idpf_tx_flex_desc *tx_desc;
2627 	unsigned int data_len, size;
2628 	struct idpf_tx_buf *tx_buf;
2629 	u16 i = tx_q->next_to_use;
2630 	struct netdev_queue *nq;
2631 	struct sk_buff *skb;
2632 	skb_frag_t *frag;
2633 	u32 next_buf_id;
2634 	u16 td_cmd = 0;
2635 	dma_addr_t dma;
2636 
2637 	skb = first->skb;
2638 
2639 	td_cmd = params->offload.td_cmd;
2640 
2641 	data_len = skb->data_len;
2642 	size = skb_headlen(skb);
2643 
2644 	tx_desc = &tx_q->flex_tx[i];
2645 
2646 	dma = dma_map_single(tx_q->dev, skb->data, size, DMA_TO_DEVICE);
2647 
2648 	tx_buf = first;
2649 	first->nr_frags = 0;
2650 
2651 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
2652 		unsigned int max_data = IDPF_TX_MAX_DESC_DATA_ALIGNED;
2653 
2654 		if (unlikely(dma_mapping_error(tx_q->dev, dma))) {
2655 			idpf_tx_buf_next(tx_buf) = IDPF_TXBUF_NULL;
2656 			return idpf_tx_splitq_pkt_err_unmap(tx_q, params,
2657 							    first);
2658 		}
2659 
2660 		first->nr_frags++;
2661 		tx_buf->type = LIBETH_SQE_FRAG;
2662 
2663 		/* record length, and DMA address */
2664 		dma_unmap_len_set(tx_buf, len, size);
2665 		dma_unmap_addr_set(tx_buf, dma, dma);
2666 
2667 		/* buf_addr is in same location for both desc types */
2668 		tx_desc->q.buf_addr = cpu_to_le64(dma);
2669 
2670 		/* The stack can send us fragments that are too large for a
2671 		 * single descriptor i.e. frag size > 16K-1. We will need to
2672 		 * split the fragment across multiple descriptors in this case.
2673 		 * To adhere to HW alignment restrictions, the fragment needs
2674 		 * to be split such that the first chunk ends on a 4K boundary
2675 		 * and all subsequent chunks start on a 4K boundary. We still
2676 		 * want to send as much data as possible though, so our
2677 		 * intermediate descriptor chunk size will be 12K.
2678 		 *
2679 		 * For example, consider a 32K fragment mapped to DMA addr 2600.
2680 		 * ------------------------------------------------------------
2681 		 * |                    frag_size = 32K                       |
2682 		 * ------------------------------------------------------------
2683 		 * |2600		  |16384	    |28672
2684 		 *
2685 		 * 3 descriptors will be used for this fragment. The HW expects
2686 		 * the descriptors to contain the following:
2687 		 * ------------------------------------------------------------
2688 		 * | size = 13784         | size = 12K      | size = 6696     |
2689 		 * | dma = 2600           | dma = 16384     | dma = 28672     |
2690 		 * ------------------------------------------------------------
2691 		 *
2692 		 * We need to first adjust the max_data for the first chunk so
2693 		 * that it ends on a 4K boundary. By negating the value of the
2694 		 * DMA address and taking only the low order bits, we're
2695 		 * effectively calculating
2696 		 *	4K - (DMA addr lower order bits) =
2697 		 *				bytes to next boundary.
2698 		 *
2699 		 * Add that to our base aligned max_data (12K) and we have
2700 		 * our first chunk size. In the example above,
2701 		 *	13784 = 12K + (4096-2600)
2702 		 *
2703 		 * After guaranteeing the first chunk ends on a 4K boundary, we
2704 		 * will give the intermediate descriptors 12K chunks and
2705 		 * whatever is left to the final descriptor. This ensures that
2706 		 * all descriptors used for the remaining chunks of the
2707 		 * fragment start on a 4K boundary and we use as few
2708 		 * descriptors as possible.
2709 		 */
2710 		max_data += -dma & (IDPF_TX_MAX_READ_REQ_SIZE - 1);
2711 		while (unlikely(size > IDPF_TX_MAX_DESC_DATA)) {
2712 			idpf_tx_splitq_build_desc(tx_desc, params, td_cmd,
2713 						  max_data);
2714 
2715 			if (unlikely(++i == tx_q->desc_count)) {
2716 				tx_desc = &tx_q->flex_tx[0];
2717 				i = 0;
2718 			} else {
2719 				tx_desc++;
2720 			}
2721 
2722 			/* Adjust the DMA offset and the remaining size of the
2723 			 * fragment.  On the first iteration of this loop,
2724 			 * max_data will be >= 12K and <= 16K-1.  On any
2725 			 * subsequent iteration of this loop, max_data will
2726 			 * always be 12K.
2727 			 */
2728 			dma += max_data;
2729 			size -= max_data;
2730 
2731 			/* Reset max_data since remaining chunks will be 12K
2732 			 * at most
2733 			 */
2734 			max_data = IDPF_TX_MAX_DESC_DATA_ALIGNED;
2735 
2736 			/* buf_addr is in same location for both desc types */
2737 			tx_desc->q.buf_addr = cpu_to_le64(dma);
2738 		}
2739 
2740 		if (!data_len)
2741 			break;
2742 
2743 		idpf_tx_splitq_build_desc(tx_desc, params, td_cmd, size);
2744 
2745 		if (unlikely(++i == tx_q->desc_count)) {
2746 			tx_desc = &tx_q->flex_tx[0];
2747 			i = 0;
2748 		} else {
2749 			tx_desc++;
2750 		}
2751 
2752 		if (idpf_queue_has(FLOW_SCH_EN, tx_q)) {
2753 			if (unlikely(!idpf_tx_get_free_buf_id(tx_q->refillq,
2754 							      &next_buf_id))) {
2755 				idpf_tx_buf_next(tx_buf) = IDPF_TXBUF_NULL;
2756 				return idpf_tx_splitq_pkt_err_unmap(tx_q, params,
2757 								    first);
2758 			}
2759 		} else {
2760 			next_buf_id = i;
2761 		}
2762 		idpf_tx_buf_next(tx_buf) = next_buf_id;
2763 		tx_buf = &tx_q->tx_buf[next_buf_id];
2764 
2765 		size = skb_frag_size(frag);
2766 		data_len -= size;
2767 
2768 		dma = skb_frag_dma_map(tx_q->dev, frag, 0, size,
2769 				       DMA_TO_DEVICE);
2770 	}
2771 
2772 	/* record SW timestamp if HW timestamp is not available */
2773 	skb_tx_timestamp(skb);
2774 
2775 	first->type = LIBETH_SQE_SKB;
2776 
2777 	/* write last descriptor with RS and EOP bits */
2778 	first->rs_idx = i;
2779 	idpf_tx_buf_next(tx_buf) = IDPF_TXBUF_NULL;
2780 	td_cmd |= params->eop_cmd;
2781 	idpf_tx_splitq_build_desc(tx_desc, params, td_cmd, size);
2782 	i = idpf_tx_splitq_bump_ntu(tx_q, i);
2783 
2784 	tx_q->txq_grp->num_completions_pending++;
2785 
2786 	/* record bytecount for BQL */
2787 	nq = netdev_get_tx_queue(tx_q->netdev, tx_q->idx);
2788 	netdev_tx_sent_queue(nq, first->bytes);
2789 
2790 	idpf_tx_buf_hw_update(tx_q, i, netdev_xmit_more());
2791 }
2792 
2793 /**
2794  * idpf_tso - computes mss and TSO length to prepare for TSO
2795  * @skb: pointer to skb
2796  * @off: pointer to struct that holds offload parameters
2797  *
2798  * Returns error (negative) if TSO was requested but cannot be applied to the
2799  * given skb, 0 if TSO does not apply to the given skb, or 1 otherwise.
2800  */
2801 int idpf_tso(struct sk_buff *skb, struct idpf_tx_offload_params *off)
2802 {
2803 	const struct skb_shared_info *shinfo;
2804 	union {
2805 		struct iphdr *v4;
2806 		struct ipv6hdr *v6;
2807 		unsigned char *hdr;
2808 	} ip;
2809 	union {
2810 		struct tcphdr *tcp;
2811 		struct udphdr *udp;
2812 		unsigned char *hdr;
2813 	} l4;
2814 	u32 paylen, l4_start;
2815 	int err;
2816 
2817 	if (!skb_is_gso(skb))
2818 		return 0;
2819 
2820 	err = skb_cow_head(skb, 0);
2821 	if (err < 0)
2822 		return err;
2823 
2824 	shinfo = skb_shinfo(skb);
2825 
2826 	ip.hdr = skb_network_header(skb);
2827 	l4.hdr = skb_transport_header(skb);
2828 
2829 	/* initialize outer IP header fields */
2830 	if (ip.v4->version == 4) {
2831 		ip.v4->tot_len = 0;
2832 		ip.v4->check = 0;
2833 	} else if (ip.v6->version == 6) {
2834 		ip.v6->payload_len = 0;
2835 	}
2836 
2837 	l4_start = skb_transport_offset(skb);
2838 
2839 	/* remove payload length from checksum */
2840 	paylen = skb->len - l4_start;
2841 
2842 	switch (shinfo->gso_type & ~SKB_GSO_DODGY) {
2843 	case SKB_GSO_TCPV4:
2844 	case SKB_GSO_TCPV6:
2845 		csum_replace_by_diff(&l4.tcp->check,
2846 				     (__force __wsum)htonl(paylen));
2847 		off->tso_hdr_len = __tcp_hdrlen(l4.tcp) + l4_start;
2848 		break;
2849 	case SKB_GSO_UDP_L4:
2850 		csum_replace_by_diff(&l4.udp->check,
2851 				     (__force __wsum)htonl(paylen));
2852 		/* compute length of segmentation header */
2853 		off->tso_hdr_len = sizeof(struct udphdr) + l4_start;
2854 		l4.udp->len = htons(shinfo->gso_size + sizeof(struct udphdr));
2855 		break;
2856 	default:
2857 		return -EINVAL;
2858 	}
2859 
2860 	off->tso_len = skb->len - off->tso_hdr_len;
2861 	off->mss = shinfo->gso_size;
2862 	off->tso_segs = shinfo->gso_segs;
2863 
2864 	off->tx_flags |= IDPF_TX_FLAGS_TSO;
2865 
2866 	return 1;
2867 }
2868 
2869 
2870 /**
2871  * idpf_tx_splitq_get_ctx_desc - grab next desc and update buffer ring
2872  * @txq: queue to put context descriptor on
2873  *
2874  * Since the TX buffer rings mimics the descriptor ring, update the tx buffer
2875  * ring entry to reflect that this index is a context descriptor
2876  */
2877 static union idpf_flex_tx_ctx_desc *
2878 idpf_tx_splitq_get_ctx_desc(struct idpf_tx_queue *txq)
2879 {
2880 	union idpf_flex_tx_ctx_desc *desc;
2881 	int i = txq->next_to_use;
2882 
2883 	/* grab the next descriptor */
2884 	desc = &txq->flex_ctx[i];
2885 	txq->next_to_use = idpf_tx_splitq_bump_ntu(txq, i);
2886 
2887 	return desc;
2888 }
2889 
2890 /**
2891  * idpf_tx_drop_skb - free the SKB and bump tail if necessary
2892  * @tx_q: queue to send buffer on
2893  * @skb: pointer to skb
2894  */
2895 netdev_tx_t idpf_tx_drop_skb(struct idpf_tx_queue *tx_q, struct sk_buff *skb)
2896 {
2897 	u64_stats_update_begin(&tx_q->stats_sync);
2898 	u64_stats_inc(&tx_q->q_stats.skb_drops);
2899 	u64_stats_update_end(&tx_q->stats_sync);
2900 
2901 	idpf_tx_buf_hw_update(tx_q, tx_q->next_to_use, false);
2902 
2903 	dev_kfree_skb(skb);
2904 
2905 	return NETDEV_TX_OK;
2906 }
2907 
2908 #if (IS_ENABLED(CONFIG_PTP_1588_CLOCK))
2909 /**
2910  * idpf_tx_tstamp - set up context descriptor for hardware timestamp
2911  * @tx_q: queue to send buffer on
2912  * @skb: pointer to the SKB we're sending
2913  * @off: pointer to the offload struct
2914  *
2915  * Return: Positive index number on success, negative otherwise.
2916  */
2917 static int idpf_tx_tstamp(struct idpf_tx_queue *tx_q, struct sk_buff *skb,
2918 			  struct idpf_tx_offload_params *off)
2919 {
2920 	int err, idx;
2921 
2922 	/* only timestamp the outbound packet if the user has requested it */
2923 	if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
2924 		return -1;
2925 
2926 	if (!idpf_ptp_get_txq_tstamp_capability(tx_q))
2927 		return -1;
2928 
2929 	/* Tx timestamps cannot be sampled when doing TSO */
2930 	if (off->tx_flags & IDPF_TX_FLAGS_TSO)
2931 		return -1;
2932 
2933 	/* Grab an open timestamp slot */
2934 	err = idpf_ptp_request_ts(tx_q, skb, &idx);
2935 	if (err) {
2936 		u64_stats_update_begin(&tx_q->stats_sync);
2937 		u64_stats_inc(&tx_q->q_stats.tstamp_skipped);
2938 		u64_stats_update_end(&tx_q->stats_sync);
2939 
2940 		return -1;
2941 	}
2942 
2943 	off->tx_flags |= IDPF_TX_FLAGS_TSYN;
2944 
2945 	return idx;
2946 }
2947 
2948 /**
2949  * idpf_tx_set_tstamp_desc - Set the Tx descriptor fields needed to generate
2950  *			     PHY Tx timestamp
2951  * @ctx_desc: Context descriptor
2952  * @idx: Index of the Tx timestamp latch
2953  */
2954 static void idpf_tx_set_tstamp_desc(union idpf_flex_tx_ctx_desc *ctx_desc,
2955 				    u32 idx)
2956 {
2957 	ctx_desc->tsyn.qw1 = le64_encode_bits(IDPF_TX_DESC_DTYPE_CTX,
2958 					      IDPF_TX_CTX_DTYPE_M) |
2959 			     le64_encode_bits(IDPF_TX_CTX_DESC_TSYN,
2960 					      IDPF_TX_CTX_CMD_M) |
2961 			     le64_encode_bits(idx, IDPF_TX_CTX_TSYN_REG_M);
2962 }
2963 #else /* CONFIG_PTP_1588_CLOCK */
2964 static int idpf_tx_tstamp(struct idpf_tx_queue *tx_q, struct sk_buff *skb,
2965 			  struct idpf_tx_offload_params *off)
2966 {
2967 	return -1;
2968 }
2969 
2970 static void idpf_tx_set_tstamp_desc(union idpf_flex_tx_ctx_desc *ctx_desc,
2971 				    u32 idx)
2972 { }
2973 #endif /* CONFIG_PTP_1588_CLOCK */
2974 
2975 /**
2976  * idpf_tx_splitq_need_re - check whether RE bit needs to be set
2977  * @tx_q: pointer to Tx queue
2978  *
2979  * Return: true if RE bit needs to be set, false otherwise
2980  */
2981 static bool idpf_tx_splitq_need_re(struct idpf_tx_queue *tx_q)
2982 {
2983 	int gap = tx_q->next_to_use - tx_q->last_re;
2984 
2985 	gap += (gap < 0) ? tx_q->desc_count : 0;
2986 
2987 	return gap >= IDPF_TX_SPLITQ_RE_MIN_GAP;
2988 }
2989 
2990 /**
2991  * idpf_tx_splitq_frame - Sends buffer on Tx ring using flex descriptors
2992  * @skb: send buffer
2993  * @tx_q: queue to send buffer on
2994  *
2995  * Returns NETDEV_TX_OK if sent, else an error code
2996  */
2997 static netdev_tx_t idpf_tx_splitq_frame(struct sk_buff *skb,
2998 					struct idpf_tx_queue *tx_q)
2999 {
3000 	struct idpf_tx_splitq_params tx_params = {
3001 		.prev_ntu = tx_q->next_to_use,
3002 	};
3003 	union idpf_flex_tx_ctx_desc *ctx_desc;
3004 	struct idpf_tx_buf *first;
3005 	u32 count, buf_count = 1;
3006 	int tso, idx;
3007 	u32 buf_id;
3008 
3009 	count = idpf_tx_res_count_required(tx_q, skb, &buf_count);
3010 	if (unlikely(!count))
3011 		return idpf_tx_drop_skb(tx_q, skb);
3012 
3013 	tso = idpf_tso(skb, &tx_params.offload);
3014 	if (unlikely(tso < 0))
3015 		return idpf_tx_drop_skb(tx_q, skb);
3016 
3017 	/* Check for splitq specific TX resources */
3018 	count += (IDPF_TX_DESCS_PER_CACHE_LINE + tso);
3019 	if (idpf_tx_maybe_stop_splitq(tx_q, count, buf_count)) {
3020 		idpf_tx_buf_hw_update(tx_q, tx_q->next_to_use, false);
3021 
3022 		return NETDEV_TX_BUSY;
3023 	}
3024 
3025 	if (tso) {
3026 		/* If tso is needed, set up context desc */
3027 		ctx_desc = idpf_tx_splitq_get_ctx_desc(tx_q);
3028 
3029 		ctx_desc->tso.qw1.cmd_dtype =
3030 				cpu_to_le16(IDPF_TX_DESC_DTYPE_FLEX_TSO_CTX |
3031 					    IDPF_TX_FLEX_CTX_DESC_CMD_TSO);
3032 		ctx_desc->tso.qw0.flex_tlen =
3033 				cpu_to_le32(tx_params.offload.tso_len &
3034 					    IDPF_TXD_FLEX_CTX_TLEN_M);
3035 		ctx_desc->tso.qw0.mss_rt =
3036 				cpu_to_le16(tx_params.offload.mss &
3037 					    IDPF_TXD_FLEX_CTX_MSS_RT_M);
3038 		ctx_desc->tso.qw0.hdr_len = tx_params.offload.tso_hdr_len;
3039 
3040 		u64_stats_update_begin(&tx_q->stats_sync);
3041 		u64_stats_inc(&tx_q->q_stats.lso_pkts);
3042 		u64_stats_update_end(&tx_q->stats_sync);
3043 	}
3044 
3045 	idx = idpf_tx_tstamp(tx_q, skb, &tx_params.offload);
3046 	if (idx != -1) {
3047 		ctx_desc = idpf_tx_splitq_get_ctx_desc(tx_q);
3048 		idpf_tx_set_tstamp_desc(ctx_desc, idx);
3049 	}
3050 
3051 	if (idpf_queue_has(FLOW_SCH_EN, tx_q)) {
3052 		struct idpf_sw_queue *refillq = tx_q->refillq;
3053 
3054 		/* Save refillq state in case of a packet rollback.  Otherwise,
3055 		 * the tags will be leaked since they will be popped from the
3056 		 * refillq but never reposted during cleaning.
3057 		 */
3058 		tx_params.prev_refill_gen =
3059 			idpf_queue_has(RFL_GEN_CHK, refillq);
3060 		tx_params.prev_refill_ntc = refillq->next_to_clean;
3061 
3062 		if (unlikely(!idpf_tx_get_free_buf_id(tx_q->refillq,
3063 						      &buf_id))) {
3064 			if (tx_params.prev_refill_gen !=
3065 			    idpf_queue_has(RFL_GEN_CHK, refillq))
3066 				idpf_queue_change(RFL_GEN_CHK, refillq);
3067 			refillq->next_to_clean = tx_params.prev_refill_ntc;
3068 
3069 			tx_q->next_to_use = tx_params.prev_ntu;
3070 			return idpf_tx_drop_skb(tx_q, skb);
3071 		}
3072 		tx_params.compl_tag = buf_id;
3073 
3074 		tx_params.dtype = IDPF_TX_DESC_DTYPE_FLEX_FLOW_SCHE;
3075 		tx_params.eop_cmd = IDPF_TXD_FLEX_FLOW_CMD_EOP;
3076 		/* Set the RE bit to periodically "clean" the descriptor ring.
3077 		 * MIN_GAP is set to MIN_RING size to ensure it will be set at
3078 		 * least once each time around the ring.
3079 		 */
3080 		if (idpf_tx_splitq_need_re(tx_q)) {
3081 			tx_params.eop_cmd |= IDPF_TXD_FLEX_FLOW_CMD_RE;
3082 			tx_q->txq_grp->num_completions_pending++;
3083 			tx_q->last_re = tx_q->next_to_use;
3084 		}
3085 
3086 		if (skb->ip_summed == CHECKSUM_PARTIAL)
3087 			tx_params.offload.td_cmd |= IDPF_TXD_FLEX_FLOW_CMD_CS_EN;
3088 
3089 	} else {
3090 		buf_id = tx_q->next_to_use;
3091 
3092 		tx_params.dtype = IDPF_TX_DESC_DTYPE_FLEX_L2TAG1_L2TAG2;
3093 		tx_params.eop_cmd = IDPF_TXD_LAST_DESC_CMD;
3094 
3095 		if (skb->ip_summed == CHECKSUM_PARTIAL)
3096 			tx_params.offload.td_cmd |= IDPF_TX_FLEX_DESC_CMD_CS_EN;
3097 	}
3098 
3099 	first = &tx_q->tx_buf[buf_id];
3100 	first->skb = skb;
3101 
3102 	if (tso) {
3103 		first->packets = tx_params.offload.tso_segs;
3104 		first->bytes = skb->len +
3105 			((first->packets - 1) * tx_params.offload.tso_hdr_len);
3106 	} else {
3107 		first->packets = 1;
3108 		first->bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
3109 	}
3110 
3111 	idpf_tx_splitq_map(tx_q, &tx_params, first);
3112 
3113 	return NETDEV_TX_OK;
3114 }
3115 
3116 /**
3117  * idpf_tx_start - Selects the right Tx queue to send buffer
3118  * @skb: send buffer
3119  * @netdev: network interface device structure
3120  *
3121  * Returns NETDEV_TX_OK if sent, else an error code
3122  */
3123 netdev_tx_t idpf_tx_start(struct sk_buff *skb, struct net_device *netdev)
3124 {
3125 	const struct idpf_vport *vport = idpf_netdev_to_vport(netdev);
3126 	struct idpf_tx_queue *tx_q;
3127 
3128 	if (unlikely(skb_get_queue_mapping(skb) >=
3129 		     vport->num_txq - vport->num_xdp_txq)) {
3130 		dev_kfree_skb_any(skb);
3131 
3132 		return NETDEV_TX_OK;
3133 	}
3134 
3135 	tx_q = vport->txqs[skb_get_queue_mapping(skb)];
3136 
3137 	/* hardware can't handle really short frames, hardware padding works
3138 	 * beyond this point
3139 	 */
3140 	if (skb_put_padto(skb, tx_q->tx_min_pkt_len)) {
3141 		idpf_tx_buf_hw_update(tx_q, tx_q->next_to_use, false);
3142 
3143 		return NETDEV_TX_OK;
3144 	}
3145 
3146 	if (idpf_is_queue_model_split(vport->txq_model))
3147 		return idpf_tx_splitq_frame(skb, tx_q);
3148 	else
3149 		return idpf_tx_singleq_frame(skb, tx_q);
3150 }
3151 
3152 /**
3153  * idpf_rx_hash - set the hash value in the skb
3154  * @rxq: Rx descriptor ring packet is being transacted on
3155  * @skb: pointer to current skb being populated
3156  * @rx_desc: Receive descriptor
3157  * @decoded: Decoded Rx packet type related fields
3158  */
3159 static void
3160 idpf_rx_hash(const struct idpf_rx_queue *rxq, struct sk_buff *skb,
3161 	     const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc,
3162 	     struct libeth_rx_pt decoded)
3163 {
3164 	u32 hash;
3165 
3166 	if (!libeth_rx_pt_has_hash(rxq->xdp_rxq.dev, decoded))
3167 		return;
3168 
3169 	hash = le16_to_cpu(rx_desc->hash1) |
3170 	       (rx_desc->ff2_mirrid_hash2.hash2 << 16) |
3171 	       (rx_desc->hash3 << 24);
3172 
3173 	libeth_rx_pt_set_hash(skb, hash, decoded);
3174 }
3175 
3176 /**
3177  * idpf_rx_csum - Indicate in skb if checksum is good
3178  * @rxq: Rx descriptor ring packet is being transacted on
3179  * @skb: pointer to current skb being populated
3180  * @csum_bits: checksum fields extracted from the descriptor
3181  * @decoded: Decoded Rx packet type related fields
3182  *
3183  * skb->protocol must be set before this function is called
3184  */
3185 static void idpf_rx_csum(struct idpf_rx_queue *rxq, struct sk_buff *skb,
3186 			 struct libeth_rx_csum csum_bits,
3187 			 struct libeth_rx_pt decoded)
3188 {
3189 	bool ipv4, ipv6;
3190 
3191 	/* check if Rx checksum is enabled */
3192 	if (!libeth_rx_pt_has_checksum(rxq->xdp_rxq.dev, decoded))
3193 		return;
3194 
3195 	/* check if HW has decoded the packet and checksum */
3196 	if (unlikely(!csum_bits.l3l4p))
3197 		return;
3198 
3199 	ipv4 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV4;
3200 	ipv6 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV6;
3201 
3202 	if (unlikely(ipv4 && (csum_bits.ipe || csum_bits.eipe)))
3203 		goto checksum_fail;
3204 
3205 	if (unlikely(ipv6 && csum_bits.ipv6exadd))
3206 		return;
3207 
3208 	/* check for L4 errors and handle packets that were not able to be
3209 	 * checksummed
3210 	 */
3211 	if (unlikely(csum_bits.l4e))
3212 		goto checksum_fail;
3213 
3214 	if (!csum_bits.raw_csum_valid ||
3215 	    decoded.inner_prot == LIBETH_RX_PT_INNER_SCTP) {
3216 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3217 		return;
3218 	}
3219 
3220 	skb->csum = csum_unfold((__force __sum16)~swab16(csum_bits.raw_csum));
3221 	skb->ip_summed = CHECKSUM_COMPLETE;
3222 
3223 	return;
3224 
3225 checksum_fail:
3226 	u64_stats_update_begin(&rxq->stats_sync);
3227 	u64_stats_inc(&rxq->q_stats.hw_csum_err);
3228 	u64_stats_update_end(&rxq->stats_sync);
3229 }
3230 
3231 /**
3232  * idpf_rx_splitq_extract_csum_bits - Extract checksum bits from descriptor
3233  * @rx_desc: receive descriptor
3234  *
3235  * Return: parsed checksum status.
3236  **/
3237 static struct libeth_rx_csum
3238 idpf_rx_splitq_extract_csum_bits(const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc)
3239 {
3240 	struct libeth_rx_csum csum = { };
3241 	u8 qword0, qword1;
3242 
3243 	qword0 = rx_desc->status_err0_qw0;
3244 	qword1 = rx_desc->status_err0_qw1;
3245 
3246 	csum.ipe = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_XSUM_IPE_M,
3247 			     qword1);
3248 	csum.eipe = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_XSUM_EIPE_M,
3249 			      qword1);
3250 	csum.l4e = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_XSUM_L4E_M,
3251 			     qword1);
3252 	csum.l3l4p = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_L3L4P_M,
3253 			       qword1);
3254 	csum.ipv6exadd = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_IPV6EXADD_M,
3255 				   qword0);
3256 	csum.raw_csum_valid =
3257 		!le16_get_bits(rx_desc->ptype_err_fflags0,
3258 			       VIRTCHNL2_RX_FLEX_DESC_ADV_RAW_CSUM_INV_M);
3259 	csum.raw_csum = le16_to_cpu(rx_desc->misc.raw_cs);
3260 
3261 	return csum;
3262 }
3263 
3264 /**
3265  * idpf_rx_rsc - Set the RSC fields in the skb
3266  * @rxq : Rx descriptor ring packet is being transacted on
3267  * @skb : pointer to current skb being populated
3268  * @rx_desc: Receive descriptor
3269  * @decoded: Decoded Rx packet type related fields
3270  *
3271  * Return 0 on success and error code on failure
3272  *
3273  * Populate the skb fields with the total number of RSC segments, RSC payload
3274  * length and packet type.
3275  */
3276 static int idpf_rx_rsc(struct idpf_rx_queue *rxq, struct sk_buff *skb,
3277 		       const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc,
3278 		       struct libeth_rx_pt decoded)
3279 {
3280 	u16 rsc_segments, rsc_seg_len;
3281 	bool ipv4, ipv6;
3282 	int len;
3283 
3284 	if (unlikely(libeth_rx_pt_get_ip_ver(decoded) ==
3285 		     LIBETH_RX_PT_OUTER_L2))
3286 		return -EINVAL;
3287 
3288 	rsc_seg_len = le16_to_cpu(rx_desc->misc.rscseglen);
3289 	if (unlikely(!rsc_seg_len))
3290 		return -EINVAL;
3291 
3292 	ipv4 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV4;
3293 	ipv6 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV6;
3294 
3295 	if (unlikely(!(ipv4 ^ ipv6)))
3296 		return -EINVAL;
3297 
3298 	rsc_segments = DIV_ROUND_UP(skb->data_len, rsc_seg_len);
3299 
3300 	NAPI_GRO_CB(skb)->count = rsc_segments;
3301 	skb_shinfo(skb)->gso_size = rsc_seg_len;
3302 
3303 	skb_reset_network_header(skb);
3304 
3305 	if (ipv4) {
3306 		struct iphdr *ipv4h = ip_hdr(skb);
3307 
3308 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
3309 
3310 		/* Reset and set transport header offset in skb */
3311 		skb_set_transport_header(skb, sizeof(struct iphdr));
3312 		len = skb->len - skb_transport_offset(skb);
3313 
3314 		/* Compute the TCP pseudo header checksum*/
3315 		tcp_hdr(skb)->check =
3316 			~tcp_v4_check(len, ipv4h->saddr, ipv4h->daddr, 0);
3317 	} else {
3318 		struct ipv6hdr *ipv6h = ipv6_hdr(skb);
3319 
3320 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
3321 		skb_set_transport_header(skb, sizeof(struct ipv6hdr));
3322 		len = skb->len - skb_transport_offset(skb);
3323 		tcp_hdr(skb)->check =
3324 			~tcp_v6_check(len, &ipv6h->saddr, &ipv6h->daddr, 0);
3325 	}
3326 
3327 	tcp_gro_complete(skb);
3328 
3329 	u64_stats_update_begin(&rxq->stats_sync);
3330 	u64_stats_inc(&rxq->q_stats.rsc_pkts);
3331 	u64_stats_update_end(&rxq->stats_sync);
3332 
3333 	return 0;
3334 }
3335 
3336 /**
3337  * idpf_rx_hwtstamp - check for an RX timestamp and pass up the stack
3338  * @rxq: pointer to the rx queue that receives the timestamp
3339  * @rx_desc: pointer to rx descriptor containing timestamp
3340  * @skb: skb to put timestamp in
3341  */
3342 static void
3343 idpf_rx_hwtstamp(const struct idpf_rx_queue *rxq,
3344 		 const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc,
3345 		 struct sk_buff *skb)
3346 {
3347 	u64 cached_time, ts_ns;
3348 	u32 ts_high;
3349 
3350 	if (!(rx_desc->ts_low & VIRTCHNL2_RX_FLEX_TSTAMP_VALID))
3351 		return;
3352 
3353 	cached_time = READ_ONCE(rxq->cached_phc_time);
3354 
3355 	ts_high = le32_to_cpu(rx_desc->ts_high);
3356 	ts_ns = idpf_ptp_tstamp_extend_32b_to_64b(cached_time, ts_high);
3357 
3358 	*skb_hwtstamps(skb) = (struct skb_shared_hwtstamps) {
3359 		.hwtstamp = ns_to_ktime(ts_ns),
3360 	};
3361 }
3362 
3363 /**
3364  * __idpf_rx_process_skb_fields - Populate skb header fields from Rx descriptor
3365  * @rxq: Rx descriptor ring packet is being transacted on
3366  * @skb: pointer to current skb being populated
3367  * @rx_desc: Receive descriptor
3368  *
3369  * This function checks the ring, descriptor, and packet information in
3370  * order to populate the hash, checksum, protocol, and
3371  * other fields within the skb.
3372  */
3373 static int
3374 __idpf_rx_process_skb_fields(struct idpf_rx_queue *rxq, struct sk_buff *skb,
3375 			     const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc)
3376 {
3377 	struct libeth_rx_csum csum_bits;
3378 	struct libeth_rx_pt decoded;
3379 	u16 rx_ptype;
3380 
3381 	rx_ptype = le16_get_bits(rx_desc->ptype_err_fflags0,
3382 				 VIRTCHNL2_RX_FLEX_DESC_ADV_PTYPE_M);
3383 	decoded = rxq->rx_ptype_lkup[rx_ptype];
3384 
3385 	/* process RSS/hash */
3386 	idpf_rx_hash(rxq, skb, rx_desc, decoded);
3387 
3388 	if (idpf_queue_has(PTP, rxq))
3389 		idpf_rx_hwtstamp(rxq, rx_desc, skb);
3390 
3391 	if (le16_get_bits(rx_desc->hdrlen_flags,
3392 			  VIRTCHNL2_RX_FLEX_DESC_ADV_RSC_M))
3393 		return idpf_rx_rsc(rxq, skb, rx_desc, decoded);
3394 
3395 	csum_bits = idpf_rx_splitq_extract_csum_bits(rx_desc);
3396 	idpf_rx_csum(rxq, skb, csum_bits, decoded);
3397 
3398 	return 0;
3399 }
3400 
3401 bool idpf_rx_process_skb_fields(struct sk_buff *skb,
3402 				const struct libeth_xdp_buff *xdp,
3403 				struct libeth_rq_napi_stats *rs)
3404 {
3405 	struct idpf_rx_queue *rxq;
3406 
3407 	rxq = libeth_xdp_buff_to_rq(xdp, typeof(*rxq), xdp_rxq);
3408 
3409 	return !__idpf_rx_process_skb_fields(rxq, skb, xdp->desc);
3410 }
3411 
3412 LIBETH_XDP_DEFINE_START();
3413 LIBETH_XDP_DEFINE_RUN(static idpf_xdp_run_pass, idpf_xdp_run_prog,
3414 		      idpf_xdp_tx_flush_bulk, idpf_rx_process_skb_fields);
3415 LIBETH_XDP_DEFINE_FINALIZE(static idpf_xdp_finalize_rx, idpf_xdp_tx_flush_bulk,
3416 			   idpf_xdp_tx_finalize);
3417 LIBETH_XDP_DEFINE_END();
3418 
3419 /**
3420  * idpf_rx_hsplit_wa - handle header buffer overflows and split errors
3421  * @hdr: Rx buffer for the headers
3422  * @buf: Rx buffer for the payload
3423  * @data_len: number of bytes received to the payload buffer
3424  *
3425  * When a header buffer overflow occurs or the HW was unable do parse the
3426  * packet type to perform header split, the whole frame gets placed to the
3427  * payload buffer. We can't build a valid skb around a payload buffer when
3428  * the header split is active since it doesn't reserve any head- or tailroom.
3429  * In that case, copy either the whole frame when it's short or just the
3430  * Ethernet header to the header buffer to be able to build an skb and adjust
3431  * the data offset in the payload buffer, IOW emulate the header split.
3432  *
3433  * Return: number of bytes copied to the header buffer.
3434  */
3435 static u32 idpf_rx_hsplit_wa(const struct libeth_fqe *hdr,
3436 			     struct libeth_fqe *buf, u32 data_len)
3437 {
3438 	u32 copy = data_len <= L1_CACHE_BYTES ? data_len : ETH_HLEN;
3439 	struct page *hdr_page, *buf_page;
3440 	const void *src;
3441 	void *dst;
3442 
3443 	if (unlikely(netmem_is_net_iov(buf->netmem)) ||
3444 	    !libeth_rx_sync_for_cpu(buf, copy))
3445 		return 0;
3446 
3447 	hdr_page = __netmem_to_page(hdr->netmem);
3448 	buf_page = __netmem_to_page(buf->netmem);
3449 	dst = page_address(hdr_page) + hdr->offset +
3450 		pp_page_to_nmdesc(hdr_page)->pp->p.offset;
3451 	src = page_address(buf_page) + buf->offset +
3452 		pp_page_to_nmdesc(buf_page)->pp->p.offset;
3453 
3454 	memcpy(dst, src, LARGEST_ALIGN(copy));
3455 	buf->offset += copy;
3456 
3457 	return copy;
3458 }
3459 
3460 /**
3461  * idpf_rx_splitq_test_staterr - tests bits in Rx descriptor
3462  * status and error fields
3463  * @stat_err_field: field from descriptor to test bits in
3464  * @stat_err_bits: value to mask
3465  *
3466  */
3467 static bool idpf_rx_splitq_test_staterr(const u8 stat_err_field,
3468 					const u8 stat_err_bits)
3469 {
3470 	return !!(stat_err_field & stat_err_bits);
3471 }
3472 
3473 /**
3474  * idpf_rx_splitq_is_eop - process handling of EOP buffers
3475  * @rx_desc: Rx descriptor for current buffer
3476  *
3477  * If the buffer is an EOP buffer, this function exits returning true,
3478  * otherwise return false indicating that this is in fact a non-EOP buffer.
3479  */
3480 static bool idpf_rx_splitq_is_eop(struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc)
3481 {
3482 	/* if we are the last buffer then there is nothing else to do */
3483 	return likely(idpf_rx_splitq_test_staterr(rx_desc->status_err0_qw1,
3484 						  IDPF_RXD_EOF_SPLITQ));
3485 }
3486 
3487 /**
3488  * idpf_rx_splitq_clean - Clean completed descriptors from Rx queue
3489  * @rxq: Rx descriptor queue to retrieve receive buffer queue
3490  * @budget: Total limit on number of packets to process
3491  *
3492  * This function provides a "bounce buffer" approach to Rx interrupt
3493  * processing. The advantage to this is that on systems that have
3494  * expensive overhead for IOMMU access this provides a means of avoiding
3495  * it by maintaining the mapping of the page to the system.
3496  *
3497  * Returns amount of work completed
3498  */
3499 static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
3500 {
3501 	struct idpf_buf_queue *rx_bufq = NULL;
3502 	struct libeth_rq_napi_stats rs = { };
3503 	u16 ntc = rxq->next_to_clean;
3504 	LIBETH_XDP_ONSTACK_BUFF(xdp);
3505 	LIBETH_XDP_ONSTACK_BULK(bq);
3506 
3507 	libeth_xdp_tx_init_bulk(&bq, rxq->xdp_prog, rxq->xdp_rxq.dev,
3508 				rxq->xdpsqs, rxq->num_xdp_txq);
3509 	libeth_xdp_init_buff(xdp, &rxq->xdp, &rxq->xdp_rxq);
3510 
3511 	/* Process Rx packets bounded by budget */
3512 	while (likely(rs.packets < budget)) {
3513 		struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc;
3514 		struct libeth_fqe *hdr, *rx_buf = NULL;
3515 		struct idpf_sw_queue *refillq = NULL;
3516 		struct idpf_rxq_set *rxq_set = NULL;
3517 		unsigned int pkt_len = 0;
3518 		unsigned int hdr_len = 0;
3519 		u16 gen_id, buf_id = 0;
3520 		int bufq_id;
3521 		u8 rxdid;
3522 
3523 		/* get the Rx desc from Rx queue based on 'next_to_clean' */
3524 		rx_desc = &rxq->rx[ntc].flex_adv_nic_3_wb;
3525 
3526 		/* if the descriptor isn't done, no work yet to do */
3527 		gen_id = le16_get_bits(rx_desc->pktlen_gen_bufq_id,
3528 				       VIRTCHNL2_RX_FLEX_DESC_ADV_GEN_M);
3529 		if (idpf_queue_has(GEN_CHK, rxq) != gen_id)
3530 			break;
3531 
3532 		dma_rmb();
3533 
3534 		rxdid = FIELD_GET(VIRTCHNL2_RX_FLEX_DESC_ADV_RXDID_M,
3535 				  rx_desc->rxdid_ucast);
3536 		if (rxdid != VIRTCHNL2_RXDID_2_FLEX_SPLITQ) {
3537 			IDPF_RX_BUMP_NTC(rxq, ntc);
3538 			u64_stats_update_begin(&rxq->stats_sync);
3539 			u64_stats_inc(&rxq->q_stats.bad_descs);
3540 			u64_stats_update_end(&rxq->stats_sync);
3541 			continue;
3542 		}
3543 
3544 		pkt_len = le16_get_bits(rx_desc->pktlen_gen_bufq_id,
3545 					VIRTCHNL2_RX_FLEX_DESC_ADV_LEN_PBUF_M);
3546 
3547 		bufq_id = le16_get_bits(rx_desc->pktlen_gen_bufq_id,
3548 					VIRTCHNL2_RX_FLEX_DESC_ADV_BUFQ_ID_M);
3549 
3550 		rxq_set = container_of(rxq, struct idpf_rxq_set, rxq);
3551 		refillq = rxq_set->refillq[bufq_id];
3552 
3553 		/* retrieve buffer from the rxq */
3554 		rx_bufq = &rxq->bufq_sets[bufq_id].bufq;
3555 
3556 		buf_id = le16_to_cpu(rx_desc->buf_id);
3557 
3558 		rx_buf = &rx_bufq->buf[buf_id];
3559 
3560 		if (!rx_bufq->hdr_pp)
3561 			goto payload;
3562 
3563 #define __HBO_BIT	VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_HBO_M
3564 #define __HDR_LEN_MASK	VIRTCHNL2_RX_FLEX_DESC_ADV_LEN_HDR_M
3565 		if (likely(!(rx_desc->status_err0_qw1 & __HBO_BIT)))
3566 			/* If a header buffer overflow, occurs, i.e. header is
3567 			 * too large to fit in the header split buffer, HW will
3568 			 * put the entire packet, including headers, in the
3569 			 * data/payload buffer.
3570 			 */
3571 			hdr_len = le16_get_bits(rx_desc->hdrlen_flags,
3572 						__HDR_LEN_MASK);
3573 #undef __HDR_LEN_MASK
3574 #undef __HBO_BIT
3575 
3576 		hdr = &rx_bufq->hdr_buf[buf_id];
3577 
3578 		if (unlikely(!hdr_len && !xdp->data)) {
3579 			hdr_len = idpf_rx_hsplit_wa(hdr, rx_buf, pkt_len);
3580 			/* If failed, drop both buffers by setting len to 0 */
3581 			pkt_len -= hdr_len ? : pkt_len;
3582 
3583 			u64_stats_update_begin(&rxq->stats_sync);
3584 			u64_stats_inc(&rxq->q_stats.hsplit_buf_ovf);
3585 			u64_stats_update_end(&rxq->stats_sync);
3586 		}
3587 
3588 		if (libeth_xdp_process_buff(xdp, hdr, hdr_len))
3589 			rs.hsplit++;
3590 
3591 		hdr->netmem = 0;
3592 
3593 payload:
3594 		libeth_xdp_process_buff(xdp, rx_buf, pkt_len);
3595 		rx_buf->netmem = 0;
3596 
3597 		idpf_post_buf_refill(refillq, buf_id);
3598 		IDPF_RX_BUMP_NTC(rxq, ntc);
3599 
3600 		/* skip if it is non EOP desc */
3601 		if (!idpf_rx_splitq_is_eop(rx_desc) || unlikely(!xdp->data))
3602 			continue;
3603 
3604 		idpf_xdp_run_pass(xdp, &bq, rxq->napi, &rs, rx_desc);
3605 	}
3606 
3607 	idpf_xdp_finalize_rx(&bq);
3608 
3609 	rxq->next_to_clean = ntc;
3610 	libeth_xdp_save_buff(&rxq->xdp, xdp);
3611 
3612 	u64_stats_update_begin(&rxq->stats_sync);
3613 	u64_stats_add(&rxq->q_stats.packets, rs.packets);
3614 	u64_stats_add(&rxq->q_stats.bytes, rs.bytes);
3615 	u64_stats_add(&rxq->q_stats.hsplit_pkts, rs.hsplit);
3616 	u64_stats_update_end(&rxq->stats_sync);
3617 
3618 	return rs.packets;
3619 }
3620 
3621 /**
3622  * idpf_rx_update_bufq_desc - Update buffer queue descriptor
3623  * @bufq: Pointer to the buffer queue
3624  * @buf_id: buffer ID
3625  * @buf_desc: Buffer queue descriptor
3626  *
3627  * Return 0 on success and negative on failure.
3628  */
3629 static int idpf_rx_update_bufq_desc(struct idpf_buf_queue *bufq, u32 buf_id,
3630 				    struct virtchnl2_splitq_rx_buf_desc *buf_desc)
3631 {
3632 	struct libeth_fq_fp fq = {
3633 		.pp		= bufq->pp,
3634 		.fqes		= bufq->buf,
3635 		.truesize	= bufq->truesize,
3636 		.count		= bufq->desc_count,
3637 	};
3638 	dma_addr_t addr;
3639 
3640 	addr = libeth_rx_alloc(&fq, buf_id);
3641 	if (addr == DMA_MAPPING_ERROR)
3642 		return -ENOMEM;
3643 
3644 	buf_desc->pkt_addr = cpu_to_le64(addr);
3645 	buf_desc->qword0.buf_id = cpu_to_le16(buf_id);
3646 
3647 	if (!idpf_queue_has(HSPLIT_EN, bufq))
3648 		return 0;
3649 
3650 	fq.pp = bufq->hdr_pp;
3651 	fq.fqes = bufq->hdr_buf;
3652 	fq.truesize = bufq->hdr_truesize;
3653 
3654 	addr = libeth_rx_alloc(&fq, buf_id);
3655 	if (addr == DMA_MAPPING_ERROR)
3656 		return -ENOMEM;
3657 
3658 	buf_desc->hdr_addr = cpu_to_le64(addr);
3659 
3660 	return 0;
3661 }
3662 
3663 /**
3664  * idpf_rx_clean_refillq - Clean refill queue buffers
3665  * @bufq: buffer queue to post buffers back to
3666  * @refillq: refill queue to clean
3667  *
3668  * This function takes care of the buffer refill management
3669  */
3670 static void idpf_rx_clean_refillq(struct idpf_buf_queue *bufq,
3671 				  struct idpf_sw_queue *refillq)
3672 {
3673 	struct virtchnl2_splitq_rx_buf_desc *buf_desc;
3674 	u16 bufq_nta = bufq->next_to_alloc;
3675 	u16 ntc = refillq->next_to_clean;
3676 	int cleaned = 0;
3677 
3678 	buf_desc = &bufq->split_buf[bufq_nta];
3679 
3680 	/* make sure we stop at ring wrap in the unlikely case ring is full */
3681 	while (likely(cleaned < refillq->desc_count)) {
3682 		u32 buf_id, refill_desc = refillq->ring[ntc];
3683 		bool failure;
3684 
3685 		if (idpf_queue_has(RFL_GEN_CHK, refillq) !=
3686 		    !!(refill_desc & IDPF_RFL_BI_GEN_M))
3687 			break;
3688 
3689 		buf_id = FIELD_GET(IDPF_RFL_BI_BUFID_M, refill_desc);
3690 		failure = idpf_rx_update_bufq_desc(bufq, buf_id, buf_desc);
3691 		if (failure)
3692 			break;
3693 
3694 		if (unlikely(++ntc == refillq->desc_count)) {
3695 			idpf_queue_change(RFL_GEN_CHK, refillq);
3696 			ntc = 0;
3697 		}
3698 
3699 		if (unlikely(++bufq_nta == bufq->desc_count)) {
3700 			buf_desc = &bufq->split_buf[0];
3701 			bufq_nta = 0;
3702 		} else {
3703 			buf_desc++;
3704 		}
3705 
3706 		cleaned++;
3707 	}
3708 
3709 	if (!cleaned)
3710 		return;
3711 
3712 	/* We want to limit how many transactions on the bus we trigger with
3713 	 * tail writes so we only do it in strides. It's also important we
3714 	 * align the write to a multiple of 8 as required by HW.
3715 	 */
3716 	if (((bufq->next_to_use <= bufq_nta ? 0 : bufq->desc_count) +
3717 	    bufq_nta - bufq->next_to_use) >= IDPF_RX_BUF_POST_STRIDE)
3718 		idpf_rx_buf_hw_update(bufq, ALIGN_DOWN(bufq_nta,
3719 						       IDPF_RX_BUF_POST_STRIDE));
3720 
3721 	/* update next to alloc since we have filled the ring */
3722 	refillq->next_to_clean = ntc;
3723 	bufq->next_to_alloc = bufq_nta;
3724 }
3725 
3726 /**
3727  * idpf_rx_clean_refillq_all - Clean all refill queues
3728  * @bufq: buffer queue with refill queues
3729  * @nid: ID of the closest NUMA node with memory
3730  *
3731  * Iterates through all refill queues assigned to the buffer queue assigned to
3732  * this vector.  Returns true if clean is complete within budget, false
3733  * otherwise.
3734  */
3735 static void idpf_rx_clean_refillq_all(struct idpf_buf_queue *bufq, int nid)
3736 {
3737 	struct idpf_bufq_set *bufq_set;
3738 	int i;
3739 
3740 	page_pool_nid_changed(bufq->pp, nid);
3741 	if (bufq->hdr_pp)
3742 		page_pool_nid_changed(bufq->hdr_pp, nid);
3743 
3744 	bufq_set = container_of(bufq, struct idpf_bufq_set, bufq);
3745 	for (i = 0; i < bufq_set->num_refillqs; i++)
3746 		idpf_rx_clean_refillq(bufq, &bufq_set->refillqs[i]);
3747 }
3748 
3749 /**
3750  * idpf_vport_intr_clean_queues - MSIX mode Interrupt Handler
3751  * @irq: interrupt number
3752  * @data: pointer to a q_vector
3753  *
3754  */
3755 static irqreturn_t idpf_vport_intr_clean_queues(int __always_unused irq,
3756 						void *data)
3757 {
3758 	struct idpf_q_vector *q_vector = (struct idpf_q_vector *)data;
3759 
3760 	q_vector->total_events++;
3761 	napi_schedule_irqoff(&q_vector->napi);
3762 
3763 	return IRQ_HANDLED;
3764 }
3765 
3766 /**
3767  * idpf_vport_intr_napi_del_all - Unregister napi for all q_vectors in vport
3768  * @vport: virtual port structure
3769  *
3770  */
3771 static void idpf_vport_intr_napi_del_all(struct idpf_vport *vport)
3772 {
3773 	u16 v_idx;
3774 
3775 	for (v_idx = 0; v_idx < vport->num_q_vectors; v_idx++)
3776 		netif_napi_del(&vport->q_vectors[v_idx].napi);
3777 }
3778 
3779 /**
3780  * idpf_vport_intr_napi_dis_all - Disable NAPI for all q_vectors in the vport
3781  * @vport: main vport structure
3782  */
3783 static void idpf_vport_intr_napi_dis_all(struct idpf_vport *vport)
3784 {
3785 	int v_idx;
3786 
3787 	for (v_idx = 0; v_idx < vport->num_q_vectors; v_idx++)
3788 		napi_disable(&vport->q_vectors[v_idx].napi);
3789 }
3790 
3791 /**
3792  * idpf_vport_intr_rel - Free memory allocated for interrupt vectors
3793  * @vport: virtual port
3794  *
3795  * Free the memory allocated for interrupt vectors  associated to a vport
3796  */
3797 void idpf_vport_intr_rel(struct idpf_vport *vport)
3798 {
3799 	for (u32 v_idx = 0; v_idx < vport->num_q_vectors; v_idx++) {
3800 		struct idpf_q_vector *q_vector = &vport->q_vectors[v_idx];
3801 
3802 		kfree(q_vector->xsksq);
3803 		q_vector->xsksq = NULL;
3804 		kfree(q_vector->complq);
3805 		q_vector->complq = NULL;
3806 		kfree(q_vector->bufq);
3807 		q_vector->bufq = NULL;
3808 		kfree(q_vector->tx);
3809 		q_vector->tx = NULL;
3810 		kfree(q_vector->rx);
3811 		q_vector->rx = NULL;
3812 	}
3813 
3814 	kfree(vport->q_vectors);
3815 	vport->q_vectors = NULL;
3816 }
3817 
3818 static void idpf_q_vector_set_napi(struct idpf_q_vector *q_vector, bool link)
3819 {
3820 	struct napi_struct *napi = link ? &q_vector->napi : NULL;
3821 	struct net_device *dev = q_vector->vport->netdev;
3822 
3823 	for (u32 i = 0; i < q_vector->num_rxq; i++)
3824 		netif_queue_set_napi(dev, q_vector->rx[i]->idx,
3825 				     NETDEV_QUEUE_TYPE_RX, napi);
3826 
3827 	for (u32 i = 0; i < q_vector->num_txq; i++)
3828 		netif_queue_set_napi(dev, q_vector->tx[i]->idx,
3829 				     NETDEV_QUEUE_TYPE_TX, napi);
3830 }
3831 
3832 /**
3833  * idpf_vport_intr_rel_irq - Free the IRQ association with the OS
3834  * @vport: main vport structure
3835  */
3836 static void idpf_vport_intr_rel_irq(struct idpf_vport *vport)
3837 {
3838 	struct idpf_adapter *adapter = vport->adapter;
3839 	int vector;
3840 
3841 	for (vector = 0; vector < vport->num_q_vectors; vector++) {
3842 		struct idpf_q_vector *q_vector = &vport->q_vectors[vector];
3843 		int irq_num, vidx;
3844 
3845 		/* free only the irqs that were actually requested */
3846 		if (!q_vector)
3847 			continue;
3848 
3849 		vidx = vport->q_vector_idxs[vector];
3850 		irq_num = adapter->msix_entries[vidx].vector;
3851 
3852 		idpf_q_vector_set_napi(q_vector, false);
3853 		kfree(free_irq(irq_num, q_vector));
3854 	}
3855 }
3856 
3857 /**
3858  * idpf_vport_intr_dis_irq_all - Disable all interrupt
3859  * @vport: main vport structure
3860  */
3861 static void idpf_vport_intr_dis_irq_all(struct idpf_vport *vport)
3862 {
3863 	struct idpf_q_vector *q_vector = vport->q_vectors;
3864 	int q_idx;
3865 
3866 	writel(0, vport->noirq_dyn_ctl);
3867 
3868 	for (q_idx = 0; q_idx < vport->num_q_vectors; q_idx++)
3869 		writel(0, q_vector[q_idx].intr_reg.dyn_ctl);
3870 }
3871 
3872 /**
3873  * idpf_vport_intr_buildreg_itr - Enable default interrupt generation settings
3874  * @q_vector: pointer to q_vector
3875  */
3876 static u32 idpf_vport_intr_buildreg_itr(struct idpf_q_vector *q_vector)
3877 {
3878 	u32 itr_val = q_vector->intr_reg.dyn_ctl_intena_m;
3879 	int type = IDPF_NO_ITR_UPDATE_IDX;
3880 	u16 itr = 0;
3881 
3882 	if (q_vector->wb_on_itr) {
3883 		/*
3884 		 * Trigger a software interrupt when exiting wb_on_itr, to make
3885 		 * sure we catch any pending write backs that might have been
3886 		 * missed due to interrupt state transition.
3887 		 */
3888 		itr_val |= q_vector->intr_reg.dyn_ctl_swint_trig_m |
3889 			   q_vector->intr_reg.dyn_ctl_sw_itridx_ena_m;
3890 		type = IDPF_SW_ITR_UPDATE_IDX;
3891 		itr = IDPF_ITR_20K;
3892 	}
3893 
3894 	itr &= IDPF_ITR_MASK;
3895 	/* Don't clear PBA because that can cause lost interrupts that
3896 	 * came in while we were cleaning/polling
3897 	 */
3898 	itr_val |= (type << q_vector->intr_reg.dyn_ctl_itridx_s) |
3899 		   (itr << (q_vector->intr_reg.dyn_ctl_intrvl_s - 1));
3900 
3901 	return itr_val;
3902 }
3903 
3904 /**
3905  * idpf_update_dim_sample - Update dim sample with packets and bytes
3906  * @q_vector: the vector associated with the interrupt
3907  * @dim_sample: dim sample to update
3908  * @dim: dim instance structure
3909  * @packets: total packets
3910  * @bytes: total bytes
3911  *
3912  * Update the dim sample with the packets and bytes which are passed to this
3913  * function. Set the dim state appropriately if the dim settings gets stale.
3914  */
3915 static void idpf_update_dim_sample(struct idpf_q_vector *q_vector,
3916 				   struct dim_sample *dim_sample,
3917 				   struct dim *dim, u64 packets, u64 bytes)
3918 {
3919 	dim_update_sample(q_vector->total_events, packets, bytes, dim_sample);
3920 	dim_sample->comp_ctr = 0;
3921 
3922 	/* if dim settings get stale, like when not updated for 1 second or
3923 	 * longer, force it to start again. This addresses the frequent case
3924 	 * of an idle queue being switched to by the scheduler.
3925 	 */
3926 	if (ktime_ms_delta(dim_sample->time, dim->start_sample.time) >= HZ)
3927 		dim->state = DIM_START_MEASURE;
3928 }
3929 
3930 /**
3931  * idpf_net_dim - Update net DIM algorithm
3932  * @q_vector: the vector associated with the interrupt
3933  *
3934  * Create a DIM sample and notify net_dim() so that it can possibly decide
3935  * a new ITR value based on incoming packets, bytes, and interrupts.
3936  *
3937  * This function is a no-op if the queue is not configured to dynamic ITR.
3938  */
3939 static void idpf_net_dim(struct idpf_q_vector *q_vector)
3940 {
3941 	struct dim_sample dim_sample = { };
3942 	u64 packets, bytes;
3943 	u32 i;
3944 
3945 	if (!IDPF_ITR_IS_DYNAMIC(q_vector->tx_intr_mode))
3946 		goto check_rx_itr;
3947 
3948 	for (i = 0, packets = 0, bytes = 0; i < q_vector->num_txq; i++) {
3949 		struct idpf_tx_queue *txq = q_vector->tx[i];
3950 		unsigned int start;
3951 
3952 		do {
3953 			start = u64_stats_fetch_begin(&txq->stats_sync);
3954 			packets += u64_stats_read(&txq->q_stats.packets);
3955 			bytes += u64_stats_read(&txq->q_stats.bytes);
3956 		} while (u64_stats_fetch_retry(&txq->stats_sync, start));
3957 	}
3958 
3959 	idpf_update_dim_sample(q_vector, &dim_sample, &q_vector->tx_dim,
3960 			       packets, bytes);
3961 	net_dim(&q_vector->tx_dim, &dim_sample);
3962 
3963 check_rx_itr:
3964 	if (!IDPF_ITR_IS_DYNAMIC(q_vector->rx_intr_mode))
3965 		return;
3966 
3967 	for (i = 0, packets = 0, bytes = 0; i < q_vector->num_rxq; i++) {
3968 		struct idpf_rx_queue *rxq = q_vector->rx[i];
3969 		unsigned int start;
3970 
3971 		do {
3972 			start = u64_stats_fetch_begin(&rxq->stats_sync);
3973 			packets += u64_stats_read(&rxq->q_stats.packets);
3974 			bytes += u64_stats_read(&rxq->q_stats.bytes);
3975 		} while (u64_stats_fetch_retry(&rxq->stats_sync, start));
3976 	}
3977 
3978 	idpf_update_dim_sample(q_vector, &dim_sample, &q_vector->rx_dim,
3979 			       packets, bytes);
3980 	net_dim(&q_vector->rx_dim, &dim_sample);
3981 }
3982 
3983 /**
3984  * idpf_vport_intr_update_itr_ena_irq - Update itr and re-enable MSIX interrupt
3985  * @q_vector: q_vector for which itr is being updated and interrupt enabled
3986  *
3987  * Update the net_dim() algorithm and re-enable the interrupt associated with
3988  * this vector.
3989  */
3990 void idpf_vport_intr_update_itr_ena_irq(struct idpf_q_vector *q_vector)
3991 {
3992 	u32 intval;
3993 
3994 	/* net_dim() updates ITR out-of-band using a work item */
3995 	idpf_net_dim(q_vector);
3996 
3997 	intval = idpf_vport_intr_buildreg_itr(q_vector);
3998 	q_vector->wb_on_itr = false;
3999 
4000 	writel(intval, q_vector->intr_reg.dyn_ctl);
4001 }
4002 
4003 /**
4004  * idpf_vport_intr_req_irq - get MSI-X vectors from the OS for the vport
4005  * @vport: main vport structure
4006  */
4007 static int idpf_vport_intr_req_irq(struct idpf_vport *vport)
4008 {
4009 	struct idpf_adapter *adapter = vport->adapter;
4010 	const char *drv_name, *if_name, *vec_name;
4011 	int vector, err, irq_num, vidx;
4012 
4013 	drv_name = dev_driver_string(&adapter->pdev->dev);
4014 	if_name = netdev_name(vport->netdev);
4015 
4016 	for (vector = 0; vector < vport->num_q_vectors; vector++) {
4017 		struct idpf_q_vector *q_vector = &vport->q_vectors[vector];
4018 		char *name;
4019 
4020 		vidx = vport->q_vector_idxs[vector];
4021 		irq_num = adapter->msix_entries[vidx].vector;
4022 
4023 		if (q_vector->num_rxq && q_vector->num_txq)
4024 			vec_name = "TxRx";
4025 		else if (q_vector->num_rxq)
4026 			vec_name = "Rx";
4027 		else if (q_vector->num_txq)
4028 			vec_name = "Tx";
4029 		else
4030 			continue;
4031 
4032 		name = kasprintf(GFP_KERNEL, "%s-%s-%s-%d", drv_name, if_name,
4033 				 vec_name, vidx);
4034 
4035 		err = request_irq(irq_num, idpf_vport_intr_clean_queues, 0,
4036 				  name, q_vector);
4037 		if (err) {
4038 			netdev_err(vport->netdev,
4039 				   "Request_irq failed, error: %d\n", err);
4040 			goto free_q_irqs;
4041 		}
4042 
4043 		idpf_q_vector_set_napi(q_vector, true);
4044 	}
4045 
4046 	return 0;
4047 
4048 free_q_irqs:
4049 	while (--vector >= 0) {
4050 		vidx = vport->q_vector_idxs[vector];
4051 		irq_num = adapter->msix_entries[vidx].vector;
4052 		kfree(free_irq(irq_num, &vport->q_vectors[vector]));
4053 	}
4054 
4055 	return err;
4056 }
4057 
4058 /**
4059  * idpf_vport_intr_write_itr - Write ITR value to the ITR register
4060  * @q_vector: q_vector structure
4061  * @itr: Interrupt throttling rate
4062  * @tx: Tx or Rx ITR
4063  */
4064 void idpf_vport_intr_write_itr(struct idpf_q_vector *q_vector, u16 itr, bool tx)
4065 {
4066 	struct idpf_intr_reg *intr_reg;
4067 
4068 	if (tx && !q_vector->tx)
4069 		return;
4070 	else if (!tx && !q_vector->rx)
4071 		return;
4072 
4073 	intr_reg = &q_vector->intr_reg;
4074 	writel(ITR_REG_ALIGN(itr) >> IDPF_ITR_GRAN_S,
4075 	       tx ? intr_reg->tx_itr : intr_reg->rx_itr);
4076 }
4077 
4078 /**
4079  * idpf_vport_intr_ena_irq_all - Enable IRQ for the given vport
4080  * @vport: main vport structure
4081  */
4082 static void idpf_vport_intr_ena_irq_all(struct idpf_vport *vport)
4083 {
4084 	bool dynamic;
4085 	int q_idx;
4086 	u16 itr;
4087 
4088 	for (q_idx = 0; q_idx < vport->num_q_vectors; q_idx++) {
4089 		struct idpf_q_vector *qv = &vport->q_vectors[q_idx];
4090 
4091 		/* Set the initial ITR values */
4092 		if (qv->num_txq) {
4093 			dynamic = IDPF_ITR_IS_DYNAMIC(qv->tx_intr_mode);
4094 			itr = vport->tx_itr_profile[qv->tx_dim.profile_ix];
4095 			idpf_vport_intr_write_itr(qv, dynamic ?
4096 						  itr : qv->tx_itr_value,
4097 						  true);
4098 		}
4099 
4100 		if (qv->num_rxq) {
4101 			dynamic = IDPF_ITR_IS_DYNAMIC(qv->rx_intr_mode);
4102 			itr = vport->rx_itr_profile[qv->rx_dim.profile_ix];
4103 			idpf_vport_intr_write_itr(qv, dynamic ?
4104 						  itr : qv->rx_itr_value,
4105 						  false);
4106 		}
4107 
4108 		if (qv->num_txq || qv->num_rxq)
4109 			idpf_vport_intr_update_itr_ena_irq(qv);
4110 	}
4111 
4112 	writel(vport->noirq_dyn_ctl_ena, vport->noirq_dyn_ctl);
4113 }
4114 
4115 /**
4116  * idpf_vport_intr_deinit - Release all vector associations for the vport
4117  * @vport: main vport structure
4118  */
4119 void idpf_vport_intr_deinit(struct idpf_vport *vport)
4120 {
4121 	idpf_vport_intr_dis_irq_all(vport);
4122 	idpf_vport_intr_napi_dis_all(vport);
4123 	idpf_vport_intr_napi_del_all(vport);
4124 	idpf_vport_intr_rel_irq(vport);
4125 }
4126 
4127 /**
4128  * idpf_tx_dim_work - Call back from the stack
4129  * @work: work queue structure
4130  */
4131 static void idpf_tx_dim_work(struct work_struct *work)
4132 {
4133 	struct idpf_q_vector *q_vector;
4134 	struct idpf_vport *vport;
4135 	struct dim *dim;
4136 	u16 itr;
4137 
4138 	dim = container_of(work, struct dim, work);
4139 	q_vector = container_of(dim, struct idpf_q_vector, tx_dim);
4140 	vport = q_vector->vport;
4141 
4142 	if (dim->profile_ix >= ARRAY_SIZE(vport->tx_itr_profile))
4143 		dim->profile_ix = ARRAY_SIZE(vport->tx_itr_profile) - 1;
4144 
4145 	/* look up the values in our local table */
4146 	itr = vport->tx_itr_profile[dim->profile_ix];
4147 
4148 	idpf_vport_intr_write_itr(q_vector, itr, true);
4149 
4150 	dim->state = DIM_START_MEASURE;
4151 }
4152 
4153 /**
4154  * idpf_rx_dim_work - Call back from the stack
4155  * @work: work queue structure
4156  */
4157 static void idpf_rx_dim_work(struct work_struct *work)
4158 {
4159 	struct idpf_q_vector *q_vector;
4160 	struct idpf_vport *vport;
4161 	struct dim *dim;
4162 	u16 itr;
4163 
4164 	dim = container_of(work, struct dim, work);
4165 	q_vector = container_of(dim, struct idpf_q_vector, rx_dim);
4166 	vport = q_vector->vport;
4167 
4168 	if (dim->profile_ix >= ARRAY_SIZE(vport->rx_itr_profile))
4169 		dim->profile_ix = ARRAY_SIZE(vport->rx_itr_profile) - 1;
4170 
4171 	/* look up the values in our local table */
4172 	itr = vport->rx_itr_profile[dim->profile_ix];
4173 
4174 	idpf_vport_intr_write_itr(q_vector, itr, false);
4175 
4176 	dim->state = DIM_START_MEASURE;
4177 }
4178 
4179 /**
4180  * idpf_init_dim - Set up dynamic interrupt moderation
4181  * @qv: q_vector structure
4182  */
4183 static void idpf_init_dim(struct idpf_q_vector *qv)
4184 {
4185 	INIT_WORK(&qv->tx_dim.work, idpf_tx_dim_work);
4186 	qv->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4187 	qv->tx_dim.profile_ix = IDPF_DIM_DEFAULT_PROFILE_IX;
4188 
4189 	INIT_WORK(&qv->rx_dim.work, idpf_rx_dim_work);
4190 	qv->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4191 	qv->rx_dim.profile_ix = IDPF_DIM_DEFAULT_PROFILE_IX;
4192 }
4193 
4194 /**
4195  * idpf_vport_intr_napi_ena_all - Enable NAPI for all q_vectors in the vport
4196  * @vport: main vport structure
4197  */
4198 static void idpf_vport_intr_napi_ena_all(struct idpf_vport *vport)
4199 {
4200 	int q_idx;
4201 
4202 	for (q_idx = 0; q_idx < vport->num_q_vectors; q_idx++) {
4203 		struct idpf_q_vector *q_vector = &vport->q_vectors[q_idx];
4204 
4205 		idpf_init_dim(q_vector);
4206 		napi_enable(&q_vector->napi);
4207 	}
4208 }
4209 
4210 /**
4211  * idpf_tx_splitq_clean_all- Clean completion queues
4212  * @q_vec: queue vector
4213  * @budget: Used to determine if we are in netpoll
4214  * @cleaned: returns number of packets cleaned
4215  *
4216  * Returns false if clean is not complete else returns true
4217  */
4218 static bool idpf_tx_splitq_clean_all(struct idpf_q_vector *q_vec,
4219 				     int budget, int *cleaned)
4220 {
4221 	u16 num_complq = q_vec->num_complq;
4222 	bool clean_complete = true;
4223 	int i, budget_per_q;
4224 
4225 	if (unlikely(!num_complq))
4226 		return true;
4227 
4228 	budget_per_q = DIV_ROUND_UP(budget, num_complq);
4229 
4230 	for (i = 0; i < num_complq; i++)
4231 		clean_complete &= idpf_tx_clean_complq(q_vec->complq[i],
4232 						       budget_per_q, cleaned);
4233 
4234 	return clean_complete;
4235 }
4236 
4237 /**
4238  * idpf_rx_splitq_clean_all- Clean completion queues
4239  * @q_vec: queue vector
4240  * @budget: Used to determine if we are in netpoll
4241  * @cleaned: returns number of packets cleaned
4242  *
4243  * Returns false if clean is not complete else returns true
4244  */
4245 static bool idpf_rx_splitq_clean_all(struct idpf_q_vector *q_vec, int budget,
4246 				     int *cleaned)
4247 {
4248 	u16 num_rxq = q_vec->num_rxq;
4249 	bool clean_complete = true;
4250 	int pkts_cleaned = 0;
4251 	int i, budget_per_q;
4252 	int nid;
4253 
4254 	/* We attempt to distribute budget to each Rx queue fairly, but don't
4255 	 * allow the budget to go below 1 because that would exit polling early.
4256 	 */
4257 	budget_per_q = num_rxq ? max(budget / num_rxq, 1) : 0;
4258 	for (i = 0; i < num_rxq; i++) {
4259 		struct idpf_rx_queue *rxq = q_vec->rx[i];
4260 		int pkts_cleaned_per_q;
4261 
4262 		pkts_cleaned_per_q = idpf_queue_has(XSK, rxq) ?
4263 				     idpf_xskrq_poll(rxq, budget_per_q) :
4264 				     idpf_rx_splitq_clean(rxq, budget_per_q);
4265 		/* if we clean as many as budgeted, we must not be done */
4266 		if (pkts_cleaned_per_q >= budget_per_q)
4267 			clean_complete = false;
4268 		pkts_cleaned += pkts_cleaned_per_q;
4269 	}
4270 	*cleaned = pkts_cleaned;
4271 
4272 	nid = numa_mem_id();
4273 
4274 	for (i = 0; i < q_vec->num_bufq; i++) {
4275 		if (!idpf_queue_has(XSK, q_vec->bufq[i]))
4276 			idpf_rx_clean_refillq_all(q_vec->bufq[i], nid);
4277 	}
4278 
4279 	return clean_complete;
4280 }
4281 
4282 /**
4283  * idpf_vport_splitq_napi_poll - NAPI handler
4284  * @napi: struct from which you get q_vector
4285  * @budget: budget provided by stack
4286  */
4287 static int idpf_vport_splitq_napi_poll(struct napi_struct *napi, int budget)
4288 {
4289 	struct idpf_q_vector *q_vector =
4290 				container_of(napi, struct idpf_q_vector, napi);
4291 	bool clean_complete = true;
4292 	int work_done = 0;
4293 
4294 	/* Handle case where we are called by netpoll with a budget of 0 */
4295 	if (unlikely(!budget)) {
4296 		idpf_tx_splitq_clean_all(q_vector, budget, &work_done);
4297 
4298 		return 0;
4299 	}
4300 
4301 	for (u32 i = 0; i < q_vector->num_xsksq; i++)
4302 		clean_complete &= idpf_xsk_xmit(q_vector->xsksq[i]);
4303 
4304 	clean_complete &= idpf_tx_splitq_clean_all(q_vector, budget,
4305 						   &work_done);
4306 	clean_complete &= idpf_rx_splitq_clean_all(q_vector, budget,
4307 						   &work_done);
4308 
4309 	/* If work not completed, return budget and polling will return */
4310 	if (!clean_complete) {
4311 		idpf_vport_intr_set_wb_on_itr(q_vector);
4312 		return budget;
4313 	}
4314 
4315 	work_done = min_t(int, work_done, budget - 1);
4316 
4317 	/* Exit the polling mode, but don't re-enable interrupts if stack might
4318 	 * poll us due to busy-polling
4319 	 */
4320 	if (napi_complete_done(napi, work_done))
4321 		idpf_vport_intr_update_itr_ena_irq(q_vector);
4322 	else
4323 		idpf_vport_intr_set_wb_on_itr(q_vector);
4324 
4325 	return work_done;
4326 }
4327 
4328 /**
4329  * idpf_vport_intr_map_vector_to_qs - Map vectors to queues
4330  * @vport: virtual port
4331  *
4332  * Mapping for vectors to queues
4333  */
4334 static void idpf_vport_intr_map_vector_to_qs(struct idpf_vport *vport)
4335 {
4336 	u16 num_txq_grp = vport->num_txq_grp - vport->num_xdp_txq;
4337 	bool split = idpf_is_queue_model_split(vport->rxq_model);
4338 	struct idpf_rxq_group *rx_qgrp;
4339 	struct idpf_txq_group *tx_qgrp;
4340 	u32 i, qv_idx, q_index;
4341 
4342 	for (i = 0, qv_idx = 0; i < vport->num_rxq_grp; i++) {
4343 		u16 num_rxq;
4344 
4345 		if (qv_idx >= vport->num_q_vectors)
4346 			qv_idx = 0;
4347 
4348 		rx_qgrp = &vport->rxq_grps[i];
4349 		if (split)
4350 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
4351 		else
4352 			num_rxq = rx_qgrp->singleq.num_rxq;
4353 
4354 		for (u32 j = 0; j < num_rxq; j++) {
4355 			struct idpf_rx_queue *q;
4356 
4357 			if (split)
4358 				q = &rx_qgrp->splitq.rxq_sets[j]->rxq;
4359 			else
4360 				q = rx_qgrp->singleq.rxqs[j];
4361 			q->q_vector = &vport->q_vectors[qv_idx];
4362 			q_index = q->q_vector->num_rxq;
4363 			q->q_vector->rx[q_index] = q;
4364 			q->q_vector->num_rxq++;
4365 
4366 			if (split)
4367 				q->napi = &q->q_vector->napi;
4368 		}
4369 
4370 		if (split) {
4371 			for (u32 j = 0; j < vport->num_bufqs_per_qgrp; j++) {
4372 				struct idpf_buf_queue *bufq;
4373 
4374 				bufq = &rx_qgrp->splitq.bufq_sets[j].bufq;
4375 				bufq->q_vector = &vport->q_vectors[qv_idx];
4376 				q_index = bufq->q_vector->num_bufq;
4377 				bufq->q_vector->bufq[q_index] = bufq;
4378 				bufq->q_vector->num_bufq++;
4379 			}
4380 		}
4381 
4382 		qv_idx++;
4383 	}
4384 
4385 	split = idpf_is_queue_model_split(vport->txq_model);
4386 
4387 	for (i = 0, qv_idx = 0; i < num_txq_grp; i++) {
4388 		u16 num_txq;
4389 
4390 		if (qv_idx >= vport->num_q_vectors)
4391 			qv_idx = 0;
4392 
4393 		tx_qgrp = &vport->txq_grps[i];
4394 		num_txq = tx_qgrp->num_txq;
4395 
4396 		for (u32 j = 0; j < num_txq; j++) {
4397 			struct idpf_tx_queue *q;
4398 
4399 			q = tx_qgrp->txqs[j];
4400 			q->q_vector = &vport->q_vectors[qv_idx];
4401 			q->q_vector->tx[q->q_vector->num_txq++] = q;
4402 		}
4403 
4404 		if (split) {
4405 			struct idpf_compl_queue *q = tx_qgrp->complq;
4406 
4407 			q->q_vector = &vport->q_vectors[qv_idx];
4408 			q->q_vector->complq[q->q_vector->num_complq++] = q;
4409 		}
4410 
4411 		qv_idx++;
4412 	}
4413 
4414 	for (i = 0; i < vport->num_xdp_txq; i++) {
4415 		struct idpf_tx_queue *xdpsq;
4416 		struct idpf_q_vector *qv;
4417 
4418 		xdpsq = vport->txqs[vport->xdp_txq_offset + i];
4419 		if (!idpf_queue_has(XSK, xdpsq))
4420 			continue;
4421 
4422 		qv = idpf_find_rxq_vec(vport, i);
4423 		idpf_xsk_init_wakeup(qv);
4424 
4425 		xdpsq->q_vector = qv;
4426 		qv->xsksq[qv->num_xsksq++] = xdpsq;
4427 	}
4428 }
4429 
4430 /**
4431  * idpf_vport_intr_init_vec_idx - Initialize the vector indexes
4432  * @vport: virtual port
4433  *
4434  * Initialize vector indexes with values returened over mailbox
4435  */
4436 static int idpf_vport_intr_init_vec_idx(struct idpf_vport *vport)
4437 {
4438 	struct idpf_adapter *adapter = vport->adapter;
4439 	struct virtchnl2_alloc_vectors *ac;
4440 	u16 *vecids, total_vecs;
4441 	int i;
4442 
4443 	ac = adapter->req_vec_chunks;
4444 	if (!ac) {
4445 		for (i = 0; i < vport->num_q_vectors; i++)
4446 			vport->q_vectors[i].v_idx = vport->q_vector_idxs[i];
4447 
4448 		vport->noirq_v_idx = vport->q_vector_idxs[i];
4449 
4450 		return 0;
4451 	}
4452 
4453 	total_vecs = idpf_get_reserved_vecs(adapter);
4454 	vecids = kcalloc(total_vecs, sizeof(u16), GFP_KERNEL);
4455 	if (!vecids)
4456 		return -ENOMEM;
4457 
4458 	idpf_get_vec_ids(adapter, vecids, total_vecs, &ac->vchunks);
4459 
4460 	for (i = 0; i < vport->num_q_vectors; i++)
4461 		vport->q_vectors[i].v_idx = vecids[vport->q_vector_idxs[i]];
4462 
4463 	vport->noirq_v_idx = vecids[vport->q_vector_idxs[i]];
4464 
4465 	kfree(vecids);
4466 
4467 	return 0;
4468 }
4469 
4470 /**
4471  * idpf_vport_intr_napi_add_all- Register napi handler for all qvectors
4472  * @vport: virtual port structure
4473  */
4474 static void idpf_vport_intr_napi_add_all(struct idpf_vport *vport)
4475 {
4476 	int (*napi_poll)(struct napi_struct *napi, int budget);
4477 	u16 v_idx, qv_idx;
4478 	int irq_num;
4479 
4480 	if (idpf_is_queue_model_split(vport->txq_model))
4481 		napi_poll = idpf_vport_splitq_napi_poll;
4482 	else
4483 		napi_poll = idpf_vport_singleq_napi_poll;
4484 
4485 	for (v_idx = 0; v_idx < vport->num_q_vectors; v_idx++) {
4486 		struct idpf_q_vector *q_vector = &vport->q_vectors[v_idx];
4487 		qv_idx = vport->q_vector_idxs[v_idx];
4488 		irq_num = vport->adapter->msix_entries[qv_idx].vector;
4489 
4490 		netif_napi_add_config(vport->netdev, &q_vector->napi,
4491 				      napi_poll, v_idx);
4492 		netif_napi_set_irq(&q_vector->napi, irq_num);
4493 	}
4494 }
4495 
4496 /**
4497  * idpf_vport_intr_alloc - Allocate memory for interrupt vectors
4498  * @vport: virtual port
4499  *
4500  * We allocate one q_vector per queue interrupt. If allocation fails we
4501  * return -ENOMEM.
4502  */
4503 int idpf_vport_intr_alloc(struct idpf_vport *vport)
4504 {
4505 	u16 txqs_per_vector, rxqs_per_vector, bufqs_per_vector;
4506 	struct idpf_vport_user_config_data *user_config;
4507 	struct idpf_q_vector *q_vector;
4508 	struct idpf_q_coalesce *q_coal;
4509 	u32 complqs_per_vector, v_idx;
4510 	u16 idx = vport->idx;
4511 
4512 	user_config = &vport->adapter->vport_config[idx]->user_config;
4513 	vport->q_vectors = kcalloc(vport->num_q_vectors,
4514 				   sizeof(struct idpf_q_vector), GFP_KERNEL);
4515 	if (!vport->q_vectors)
4516 		return -ENOMEM;
4517 
4518 	txqs_per_vector = DIV_ROUND_UP(vport->num_txq_grp,
4519 				       vport->num_q_vectors);
4520 	rxqs_per_vector = DIV_ROUND_UP(vport->num_rxq_grp,
4521 				       vport->num_q_vectors);
4522 	bufqs_per_vector = vport->num_bufqs_per_qgrp *
4523 			   DIV_ROUND_UP(vport->num_rxq_grp,
4524 					vport->num_q_vectors);
4525 	complqs_per_vector = DIV_ROUND_UP(vport->num_txq_grp,
4526 					  vport->num_q_vectors);
4527 
4528 	for (v_idx = 0; v_idx < vport->num_q_vectors; v_idx++) {
4529 		q_vector = &vport->q_vectors[v_idx];
4530 		q_coal = &user_config->q_coalesce[v_idx];
4531 		q_vector->vport = vport;
4532 
4533 		q_vector->tx_itr_value = q_coal->tx_coalesce_usecs;
4534 		q_vector->tx_intr_mode = q_coal->tx_intr_mode;
4535 		q_vector->tx_itr_idx = VIRTCHNL2_ITR_IDX_1;
4536 
4537 		q_vector->rx_itr_value = q_coal->rx_coalesce_usecs;
4538 		q_vector->rx_intr_mode = q_coal->rx_intr_mode;
4539 		q_vector->rx_itr_idx = VIRTCHNL2_ITR_IDX_0;
4540 
4541 		q_vector->tx = kcalloc(txqs_per_vector, sizeof(*q_vector->tx),
4542 				       GFP_KERNEL);
4543 		if (!q_vector->tx)
4544 			goto error;
4545 
4546 		q_vector->rx = kcalloc(rxqs_per_vector, sizeof(*q_vector->rx),
4547 				       GFP_KERNEL);
4548 		if (!q_vector->rx)
4549 			goto error;
4550 
4551 		if (!idpf_is_queue_model_split(vport->rxq_model))
4552 			continue;
4553 
4554 		q_vector->bufq = kcalloc(bufqs_per_vector,
4555 					 sizeof(*q_vector->bufq),
4556 					 GFP_KERNEL);
4557 		if (!q_vector->bufq)
4558 			goto error;
4559 
4560 		q_vector->complq = kcalloc(complqs_per_vector,
4561 					   sizeof(*q_vector->complq),
4562 					   GFP_KERNEL);
4563 		if (!q_vector->complq)
4564 			goto error;
4565 
4566 		if (!vport->xdp_txq_offset)
4567 			continue;
4568 
4569 		q_vector->xsksq = kcalloc(rxqs_per_vector,
4570 					  sizeof(*q_vector->xsksq),
4571 					  GFP_KERNEL);
4572 		if (!q_vector->xsksq)
4573 			goto error;
4574 	}
4575 
4576 	return 0;
4577 
4578 error:
4579 	idpf_vport_intr_rel(vport);
4580 
4581 	return -ENOMEM;
4582 }
4583 
4584 /**
4585  * idpf_vport_intr_init - Setup all vectors for the given vport
4586  * @vport: virtual port
4587  *
4588  * Returns 0 on success or negative on failure
4589  */
4590 int idpf_vport_intr_init(struct idpf_vport *vport)
4591 {
4592 	int err;
4593 
4594 	err = idpf_vport_intr_init_vec_idx(vport);
4595 	if (err)
4596 		return err;
4597 
4598 	idpf_vport_intr_map_vector_to_qs(vport);
4599 	idpf_vport_intr_napi_add_all(vport);
4600 
4601 	err = vport->adapter->dev_ops.reg_ops.intr_reg_init(vport);
4602 	if (err)
4603 		goto unroll_vectors_alloc;
4604 
4605 	err = idpf_vport_intr_req_irq(vport);
4606 	if (err)
4607 		goto unroll_vectors_alloc;
4608 
4609 	return 0;
4610 
4611 unroll_vectors_alloc:
4612 	idpf_vport_intr_napi_del_all(vport);
4613 
4614 	return err;
4615 }
4616 
4617 void idpf_vport_intr_ena(struct idpf_vport *vport)
4618 {
4619 	idpf_vport_intr_napi_ena_all(vport);
4620 	idpf_vport_intr_ena_irq_all(vport);
4621 }
4622 
4623 /**
4624  * idpf_config_rss - Send virtchnl messages to configure RSS
4625  * @vport: virtual port
4626  *
4627  * Return 0 on success, negative on failure
4628  */
4629 int idpf_config_rss(struct idpf_vport *vport)
4630 {
4631 	int err;
4632 
4633 	err = idpf_send_get_set_rss_key_msg(vport, false);
4634 	if (err)
4635 		return err;
4636 
4637 	return idpf_send_get_set_rss_lut_msg(vport, false);
4638 }
4639 
4640 /**
4641  * idpf_fill_dflt_rss_lut - Fill the indirection table with the default values
4642  * @vport: virtual port structure
4643  */
4644 static void idpf_fill_dflt_rss_lut(struct idpf_vport *vport)
4645 {
4646 	struct idpf_adapter *adapter = vport->adapter;
4647 	u16 num_active_rxq = vport->num_rxq;
4648 	struct idpf_rss_data *rss_data;
4649 	int i;
4650 
4651 	rss_data = &adapter->vport_config[vport->idx]->user_config.rss_data;
4652 
4653 	for (i = 0; i < rss_data->rss_lut_size; i++) {
4654 		rss_data->rss_lut[i] = i % num_active_rxq;
4655 		rss_data->cached_lut[i] = rss_data->rss_lut[i];
4656 	}
4657 }
4658 
4659 /**
4660  * idpf_init_rss - Allocate and initialize RSS resources
4661  * @vport: virtual port
4662  *
4663  * Return 0 on success, negative on failure
4664  */
4665 int idpf_init_rss(struct idpf_vport *vport)
4666 {
4667 	struct idpf_adapter *adapter = vport->adapter;
4668 	struct idpf_rss_data *rss_data;
4669 	u32 lut_size;
4670 
4671 	rss_data = &adapter->vport_config[vport->idx]->user_config.rss_data;
4672 
4673 	lut_size = rss_data->rss_lut_size * sizeof(u32);
4674 	rss_data->rss_lut = kzalloc(lut_size, GFP_KERNEL);
4675 	if (!rss_data->rss_lut)
4676 		return -ENOMEM;
4677 
4678 	rss_data->cached_lut = kzalloc(lut_size, GFP_KERNEL);
4679 	if (!rss_data->cached_lut) {
4680 		kfree(rss_data->rss_lut);
4681 		rss_data->rss_lut = NULL;
4682 
4683 		return -ENOMEM;
4684 	}
4685 
4686 	/* Fill the default RSS lut values */
4687 	idpf_fill_dflt_rss_lut(vport);
4688 
4689 	return idpf_config_rss(vport);
4690 }
4691 
4692 /**
4693  * idpf_deinit_rss - Release RSS resources
4694  * @vport: virtual port
4695  */
4696 void idpf_deinit_rss(struct idpf_vport *vport)
4697 {
4698 	struct idpf_adapter *adapter = vport->adapter;
4699 	struct idpf_rss_data *rss_data;
4700 
4701 	rss_data = &adapter->vport_config[vport->idx]->user_config.rss_data;
4702 	kfree(rss_data->cached_lut);
4703 	rss_data->cached_lut = NULL;
4704 	kfree(rss_data->rss_lut);
4705 	rss_data->rss_lut = NULL;
4706 }
4707