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