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