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