xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_lib.c (revision 8bf3cbe32b180836720f735e6de5dee700052317)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_dcb_lib.h"
7 
8 /**
9  * ice_setup_rx_ctx - Configure a receive ring context
10  * @ring: The Rx ring to configure
11  *
12  * Configure the Rx descriptor ring in RLAN context.
13  */
14 static int ice_setup_rx_ctx(struct ice_ring *ring)
15 {
16 	struct ice_vsi *vsi = ring->vsi;
17 	struct ice_hw *hw = &vsi->back->hw;
18 	u32 rxdid = ICE_RXDID_FLEX_NIC;
19 	struct ice_rlan_ctx rlan_ctx;
20 	u32 regval;
21 	u16 pf_q;
22 	int err;
23 
24 	/* what is Rx queue number in global space of 2K Rx queues */
25 	pf_q = vsi->rxq_map[ring->q_index];
26 
27 	/* clear the context structure first */
28 	memset(&rlan_ctx, 0, sizeof(rlan_ctx));
29 
30 	rlan_ctx.base = ring->dma >> 7;
31 
32 	rlan_ctx.qlen = ring->count;
33 
34 	/* Receive Packet Data Buffer Size.
35 	 * The Packet Data Buffer Size is defined in 128 byte units.
36 	 */
37 	rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
38 
39 	/* use 32 byte descriptors */
40 	rlan_ctx.dsize = 1;
41 
42 	/* Strip the Ethernet CRC bytes before the packet is posted to host
43 	 * memory.
44 	 */
45 	rlan_ctx.crcstrip = 1;
46 
47 	/* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
48 	rlan_ctx.l2tsel = 1;
49 
50 	rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
51 	rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
52 	rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
53 
54 	/* This controls whether VLAN is stripped from inner headers
55 	 * The VLAN in the inner L2 header is stripped to the receive
56 	 * descriptor if enabled by this flag.
57 	 */
58 	rlan_ctx.showiv = 0;
59 
60 	/* Max packet size for this queue - must not be set to a larger value
61 	 * than 5 x DBUF
62 	 */
63 	rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
64 			       ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
65 
66 	/* Rx queue threshold in units of 64 */
67 	rlan_ctx.lrxqthresh = 1;
68 
69 	 /* Enable Flexible Descriptors in the queue context which
70 	  * allows this driver to select a specific receive descriptor format
71 	  */
72 	if (vsi->type != ICE_VSI_VF) {
73 		regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
74 		regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
75 			QRXFLXP_CNTXT_RXDID_IDX_M;
76 
77 		/* increasing context priority to pick up profile ID;
78 		 * default is 0x01; setting to 0x03 to ensure profile
79 		 * is programming if prev context is of same priority
80 		 */
81 		regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
82 			QRXFLXP_CNTXT_RXDID_PRIO_M;
83 
84 		wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
85 	}
86 
87 	/* Absolute queue number out of 2K needs to be passed */
88 	err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
89 	if (err) {
90 		dev_err(&vsi->back->pdev->dev,
91 			"Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
92 			pf_q, err);
93 		return -EIO;
94 	}
95 
96 	if (vsi->type == ICE_VSI_VF)
97 		return 0;
98 
99 	/* init queue specific tail register */
100 	ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
101 	writel(0, ring->tail);
102 	ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
103 
104 	return 0;
105 }
106 
107 /**
108  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
109  * @ring: The Tx ring to configure
110  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
111  * @pf_q: queue index in the PF space
112  *
113  * Configure the Tx descriptor ring in TLAN context.
114  */
115 static void
116 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
117 {
118 	struct ice_vsi *vsi = ring->vsi;
119 	struct ice_hw *hw = &vsi->back->hw;
120 
121 	tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
122 
123 	tlan_ctx->port_num = vsi->port_info->lport;
124 
125 	/* Transmit Queue Length */
126 	tlan_ctx->qlen = ring->count;
127 
128 	ice_set_cgd_num(tlan_ctx, ring);
129 
130 	/* PF number */
131 	tlan_ctx->pf_num = hw->pf_id;
132 
133 	/* queue belongs to a specific VSI type
134 	 * VF / VM index should be programmed per vmvf_type setting:
135 	 * for vmvf_type = VF, it is VF number between 0-256
136 	 * for vmvf_type = VM, it is VM number between 0-767
137 	 * for PF or EMP this field should be set to zero
138 	 */
139 	switch (vsi->type) {
140 	case ICE_VSI_LB:
141 		/* fall through */
142 	case ICE_VSI_PF:
143 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
144 		break;
145 	case ICE_VSI_VF:
146 		/* Firmware expects vmvf_num to be absolute VF ID */
147 		tlan_ctx->vmvf_num = hw->func_caps.vf_base_id + vsi->vf_id;
148 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VF;
149 		break;
150 	default:
151 		return;
152 	}
153 
154 	/* make sure the context is associated with the right VSI */
155 	tlan_ctx->src_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
156 
157 	tlan_ctx->tso_ena = ICE_TX_LEGACY;
158 	tlan_ctx->tso_qnum = pf_q;
159 
160 	/* Legacy or Advanced Host Interface:
161 	 * 0: Advanced Host Interface
162 	 * 1: Legacy Host Interface
163 	 */
164 	tlan_ctx->legacy_int = ICE_TX_LEGACY;
165 }
166 
167 /**
168  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
169  * @pf: the PF being configured
170  * @pf_q: the PF queue
171  * @ena: enable or disable state of the queue
172  *
173  * This routine will wait for the given Rx queue of the PF to reach the
174  * enabled or disabled state.
175  * Returns -ETIMEDOUT in case of failing to reach the requested state after
176  * multiple retries; else will return 0 in case of success.
177  */
178 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
179 {
180 	int i;
181 
182 	for (i = 0; i < ICE_Q_WAIT_MAX_RETRY; i++) {
183 		if (ena == !!(rd32(&pf->hw, QRX_CTRL(pf_q)) &
184 			      QRX_CTRL_QENA_STAT_M))
185 			return 0;
186 
187 		usleep_range(20, 40);
188 	}
189 
190 	return -ETIMEDOUT;
191 }
192 
193 /**
194  * ice_vsi_ctrl_rx_ring - Start or stop a VSI's Rx ring
195  * @vsi: the VSI being configured
196  * @ena: start or stop the Rx rings
197  * @rxq_idx: Rx queue index
198  */
199 #ifndef CONFIG_PCI_IOV
200 static
201 #endif /* !CONFIG_PCI_IOV */
202 int ice_vsi_ctrl_rx_ring(struct ice_vsi *vsi, bool ena, u16 rxq_idx)
203 {
204 	int pf_q = vsi->rxq_map[rxq_idx];
205 	struct ice_pf *pf = vsi->back;
206 	struct ice_hw *hw = &pf->hw;
207 	int ret = 0;
208 	u32 rx_reg;
209 
210 	rx_reg = rd32(hw, QRX_CTRL(pf_q));
211 
212 	/* Skip if the queue is already in the requested state */
213 	if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
214 		return 0;
215 
216 	/* turn on/off the queue */
217 	if (ena)
218 		rx_reg |= QRX_CTRL_QENA_REQ_M;
219 	else
220 		rx_reg &= ~QRX_CTRL_QENA_REQ_M;
221 	wr32(hw, QRX_CTRL(pf_q), rx_reg);
222 
223 	/* wait for the change to finish */
224 	ret = ice_pf_rxq_wait(pf, pf_q, ena);
225 	if (ret)
226 		dev_err(&pf->pdev->dev,
227 			"VSI idx %d Rx ring %d %sable timeout\n",
228 			vsi->idx, pf_q, (ena ? "en" : "dis"));
229 
230 	return ret;
231 }
232 
233 /**
234  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
235  * @vsi: the VSI being configured
236  * @ena: start or stop the Rx rings
237  */
238 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
239 {
240 	int i, ret = 0;
241 
242 	for (i = 0; i < vsi->num_rxq; i++) {
243 		ret = ice_vsi_ctrl_rx_ring(vsi, ena, i);
244 		if (ret)
245 			break;
246 	}
247 
248 	return ret;
249 }
250 
251 /**
252  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
253  * @vsi: VSI pointer
254  *
255  * On error: returns error code (negative)
256  * On success: returns 0
257  */
258 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
259 {
260 	struct ice_pf *pf = vsi->back;
261 
262 	/* allocate memory for both Tx and Rx ring pointers */
263 	vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
264 				     sizeof(*vsi->tx_rings), GFP_KERNEL);
265 	if (!vsi->tx_rings)
266 		return -ENOMEM;
267 
268 	vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
269 				     sizeof(*vsi->rx_rings), GFP_KERNEL);
270 	if (!vsi->rx_rings)
271 		goto err_rings;
272 
273 	vsi->txq_map = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
274 				    sizeof(*vsi->txq_map), GFP_KERNEL);
275 
276 	if (!vsi->txq_map)
277 		goto err_txq_map;
278 
279 	vsi->rxq_map = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
280 				    sizeof(*vsi->rxq_map), GFP_KERNEL);
281 	if (!vsi->rxq_map)
282 		goto err_rxq_map;
283 
284 
285 	/* There is no need to allocate q_vectors for a loopback VSI. */
286 	if (vsi->type == ICE_VSI_LB)
287 		return 0;
288 
289 	/* allocate memory for q_vector pointers */
290 	vsi->q_vectors = devm_kcalloc(&pf->pdev->dev, vsi->num_q_vectors,
291 				      sizeof(*vsi->q_vectors), GFP_KERNEL);
292 	if (!vsi->q_vectors)
293 		goto err_vectors;
294 
295 	return 0;
296 
297 err_vectors:
298 	devm_kfree(&pf->pdev->dev, vsi->rxq_map);
299 err_rxq_map:
300 	devm_kfree(&pf->pdev->dev, vsi->txq_map);
301 err_txq_map:
302 	devm_kfree(&pf->pdev->dev, vsi->rx_rings);
303 err_rings:
304 	devm_kfree(&pf->pdev->dev, vsi->tx_rings);
305 	return -ENOMEM;
306 }
307 
308 /**
309  * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
310  * @vsi: the VSI being configured
311  */
312 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
313 {
314 	switch (vsi->type) {
315 	case ICE_VSI_PF:
316 		/* fall through */
317 	case ICE_VSI_LB:
318 		vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
319 		vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
320 		break;
321 	default:
322 		dev_dbg(&vsi->back->pdev->dev,
323 			"Not setting number of Tx/Rx descriptors for VSI type %d\n",
324 			vsi->type);
325 		break;
326 	}
327 }
328 
329 /**
330  * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
331  * @vsi: the VSI being configured
332  * @vf_id: ID of the VF being configured
333  *
334  * Return 0 on success and a negative value on error
335  */
336 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
337 {
338 	struct ice_pf *pf = vsi->back;
339 	struct ice_vf *vf = NULL;
340 
341 	if (vsi->type == ICE_VSI_VF)
342 		vsi->vf_id = vf_id;
343 
344 	switch (vsi->type) {
345 	case ICE_VSI_PF:
346 		vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
347 				       num_online_cpus());
348 
349 		pf->num_lan_tx = vsi->alloc_txq;
350 
351 		/* only 1 Rx queue unless RSS is enabled */
352 		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
353 			vsi->alloc_rxq = 1;
354 		else
355 			vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
356 					       num_online_cpus());
357 
358 		pf->num_lan_rx = vsi->alloc_rxq;
359 
360 		vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
361 		break;
362 	case ICE_VSI_VF:
363 		vf = &pf->vf[vsi->vf_id];
364 		vsi->alloc_txq = vf->num_vf_qs;
365 		vsi->alloc_rxq = vf->num_vf_qs;
366 		/* pf->num_vf_msix includes (VF miscellaneous vector +
367 		 * data queue interrupts). Since vsi->num_q_vectors is number
368 		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
369 		 * original vector count
370 		 */
371 		vsi->num_q_vectors = pf->num_vf_msix - ICE_NONQ_VECS_VF;
372 		break;
373 	case ICE_VSI_LB:
374 		vsi->alloc_txq = 1;
375 		vsi->alloc_rxq = 1;
376 		break;
377 	default:
378 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
379 		break;
380 	}
381 
382 	ice_vsi_set_num_desc(vsi);
383 }
384 
385 /**
386  * ice_get_free_slot - get the next non-NULL location index in array
387  * @array: array to search
388  * @size: size of the array
389  * @curr: last known occupied index to be used as a search hint
390  *
391  * void * is being used to keep the functionality generic. This lets us use this
392  * function on any array of pointers.
393  */
394 static int ice_get_free_slot(void *array, int size, int curr)
395 {
396 	int **tmp_array = (int **)array;
397 	int next;
398 
399 	if (curr < (size - 1) && !tmp_array[curr + 1]) {
400 		next = curr + 1;
401 	} else {
402 		int i = 0;
403 
404 		while ((i < size) && (tmp_array[i]))
405 			i++;
406 		if (i == size)
407 			next = ICE_NO_VSI;
408 		else
409 			next = i;
410 	}
411 	return next;
412 }
413 
414 /**
415  * ice_vsi_delete - delete a VSI from the switch
416  * @vsi: pointer to VSI being removed
417  */
418 void ice_vsi_delete(struct ice_vsi *vsi)
419 {
420 	struct ice_pf *pf = vsi->back;
421 	struct ice_vsi_ctx *ctxt;
422 	enum ice_status status;
423 
424 	ctxt = devm_kzalloc(&pf->pdev->dev, sizeof(*ctxt), GFP_KERNEL);
425 	if (!ctxt)
426 		return;
427 
428 	if (vsi->type == ICE_VSI_VF)
429 		ctxt->vf_num = vsi->vf_id;
430 	ctxt->vsi_num = vsi->vsi_num;
431 
432 	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
433 
434 	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
435 	if (status)
436 		dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
437 			vsi->vsi_num);
438 
439 	devm_kfree(&pf->pdev->dev, ctxt);
440 }
441 
442 /**
443  * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
444  * @vsi: pointer to VSI being cleared
445  */
446 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
447 {
448 	struct ice_pf *pf = vsi->back;
449 
450 	/* free the ring and vector containers */
451 	if (vsi->q_vectors) {
452 		devm_kfree(&pf->pdev->dev, vsi->q_vectors);
453 		vsi->q_vectors = NULL;
454 	}
455 	if (vsi->tx_rings) {
456 		devm_kfree(&pf->pdev->dev, vsi->tx_rings);
457 		vsi->tx_rings = NULL;
458 	}
459 	if (vsi->rx_rings) {
460 		devm_kfree(&pf->pdev->dev, vsi->rx_rings);
461 		vsi->rx_rings = NULL;
462 	}
463 	if (vsi->txq_map) {
464 		devm_kfree(&pf->pdev->dev, vsi->txq_map);
465 		vsi->txq_map = NULL;
466 	}
467 	if (vsi->rxq_map) {
468 		devm_kfree(&pf->pdev->dev, vsi->rxq_map);
469 		vsi->rxq_map = NULL;
470 	}
471 }
472 
473 /**
474  * ice_vsi_clear - clean up and deallocate the provided VSI
475  * @vsi: pointer to VSI being cleared
476  *
477  * This deallocates the VSI's queue resources, removes it from the PF's
478  * VSI array if necessary, and deallocates the VSI
479  *
480  * Returns 0 on success, negative on failure
481  */
482 int ice_vsi_clear(struct ice_vsi *vsi)
483 {
484 	struct ice_pf *pf = NULL;
485 
486 	if (!vsi)
487 		return 0;
488 
489 	if (!vsi->back)
490 		return -EINVAL;
491 
492 	pf = vsi->back;
493 
494 	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
495 		dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
496 			vsi->idx);
497 		return -EINVAL;
498 	}
499 
500 	mutex_lock(&pf->sw_mutex);
501 	/* updates the PF for this cleared VSI */
502 
503 	pf->vsi[vsi->idx] = NULL;
504 	if (vsi->idx < pf->next_vsi)
505 		pf->next_vsi = vsi->idx;
506 
507 	ice_vsi_free_arrays(vsi);
508 	mutex_unlock(&pf->sw_mutex);
509 	devm_kfree(&pf->pdev->dev, vsi);
510 
511 	return 0;
512 }
513 
514 /**
515  * ice_msix_clean_rings - MSIX mode Interrupt Handler
516  * @irq: interrupt number
517  * @data: pointer to a q_vector
518  */
519 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
520 {
521 	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
522 
523 	if (!q_vector->tx.ring && !q_vector->rx.ring)
524 		return IRQ_HANDLED;
525 
526 	napi_schedule(&q_vector->napi);
527 
528 	return IRQ_HANDLED;
529 }
530 
531 /**
532  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
533  * @pf: board private structure
534  * @type: type of VSI
535  * @vf_id: ID of the VF being configured
536  *
537  * returns a pointer to a VSI on success, NULL on failure.
538  */
539 static struct ice_vsi *
540 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type, u16 vf_id)
541 {
542 	struct ice_vsi *vsi = NULL;
543 
544 	/* Need to protect the allocation of the VSIs at the PF level */
545 	mutex_lock(&pf->sw_mutex);
546 
547 	/* If we have already allocated our maximum number of VSIs,
548 	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
549 	 * is available to be populated
550 	 */
551 	if (pf->next_vsi == ICE_NO_VSI) {
552 		dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
553 		goto unlock_pf;
554 	}
555 
556 	vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
557 	if (!vsi)
558 		goto unlock_pf;
559 
560 	vsi->type = type;
561 	vsi->back = pf;
562 	set_bit(__ICE_DOWN, vsi->state);
563 
564 	vsi->idx = pf->next_vsi;
565 
566 	if (type == ICE_VSI_VF)
567 		ice_vsi_set_num_qs(vsi, vf_id);
568 	else
569 		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
570 
571 	switch (vsi->type) {
572 	case ICE_VSI_PF:
573 		if (ice_vsi_alloc_arrays(vsi))
574 			goto err_rings;
575 
576 		/* Setup default MSIX irq handler for VSI */
577 		vsi->irq_handler = ice_msix_clean_rings;
578 		break;
579 	case ICE_VSI_VF:
580 		if (ice_vsi_alloc_arrays(vsi))
581 			goto err_rings;
582 		break;
583 	case ICE_VSI_LB:
584 		if (ice_vsi_alloc_arrays(vsi))
585 			goto err_rings;
586 		break;
587 	default:
588 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
589 		goto unlock_pf;
590 	}
591 
592 	/* fill VSI slot in the PF struct */
593 	pf->vsi[pf->next_vsi] = vsi;
594 
595 	/* prepare pf->next_vsi for next use */
596 	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
597 					 pf->next_vsi);
598 	goto unlock_pf;
599 
600 err_rings:
601 	devm_kfree(&pf->pdev->dev, vsi);
602 	vsi = NULL;
603 unlock_pf:
604 	mutex_unlock(&pf->sw_mutex);
605 	return vsi;
606 }
607 
608 /**
609  * __ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
610  * @qs_cfg: gathered variables needed for PF->VSI queues assignment
611  *
612  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
613  */
614 static int __ice_vsi_get_qs_contig(struct ice_qs_cfg *qs_cfg)
615 {
616 	int offset, i;
617 
618 	mutex_lock(qs_cfg->qs_mutex);
619 	offset = bitmap_find_next_zero_area(qs_cfg->pf_map, qs_cfg->pf_map_size,
620 					    0, qs_cfg->q_count, 0);
621 	if (offset >= qs_cfg->pf_map_size) {
622 		mutex_unlock(qs_cfg->qs_mutex);
623 		return -ENOMEM;
624 	}
625 
626 	bitmap_set(qs_cfg->pf_map, offset, qs_cfg->q_count);
627 	for (i = 0; i < qs_cfg->q_count; i++)
628 		qs_cfg->vsi_map[i + qs_cfg->vsi_map_offset] = i + offset;
629 	mutex_unlock(qs_cfg->qs_mutex);
630 
631 	return 0;
632 }
633 
634 /**
635  * __ice_vsi_get_qs_sc - Assign a scattered queues from PF to VSI
636  * @qs_cfg: gathered variables needed for pf->vsi queues assignment
637  *
638  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
639  */
640 static int __ice_vsi_get_qs_sc(struct ice_qs_cfg *qs_cfg)
641 {
642 	int i, index = 0;
643 
644 	mutex_lock(qs_cfg->qs_mutex);
645 	for (i = 0; i < qs_cfg->q_count; i++) {
646 		index = find_next_zero_bit(qs_cfg->pf_map,
647 					   qs_cfg->pf_map_size, index);
648 		if (index >= qs_cfg->pf_map_size)
649 			goto err_scatter;
650 		set_bit(index, qs_cfg->pf_map);
651 		qs_cfg->vsi_map[i + qs_cfg->vsi_map_offset] = index;
652 	}
653 	mutex_unlock(qs_cfg->qs_mutex);
654 
655 	return 0;
656 err_scatter:
657 	for (index = 0; index < i; index++) {
658 		clear_bit(qs_cfg->vsi_map[index], qs_cfg->pf_map);
659 		qs_cfg->vsi_map[index + qs_cfg->vsi_map_offset] = 0;
660 	}
661 	mutex_unlock(qs_cfg->qs_mutex);
662 
663 	return -ENOMEM;
664 }
665 
666 /**
667  * __ice_vsi_get_qs - helper function for assigning queues from PF to VSI
668  * @qs_cfg: gathered variables needed for pf->vsi queues assignment
669  *
670  * This function first tries to find contiguous space. If it is not successful,
671  * it tries with the scatter approach.
672  *
673  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
674  */
675 static int __ice_vsi_get_qs(struct ice_qs_cfg *qs_cfg)
676 {
677 	int ret = 0;
678 
679 	ret = __ice_vsi_get_qs_contig(qs_cfg);
680 	if (ret) {
681 		/* contig failed, so try with scatter approach */
682 		qs_cfg->mapping_mode = ICE_VSI_MAP_SCATTER;
683 		qs_cfg->q_count = min_t(u16, qs_cfg->q_count,
684 					qs_cfg->scatter_count);
685 		ret = __ice_vsi_get_qs_sc(qs_cfg);
686 	}
687 	return ret;
688 }
689 
690 /**
691  * ice_vsi_get_qs - Assign queues from PF to VSI
692  * @vsi: the VSI to assign queues to
693  *
694  * Returns 0 on success and a negative value on error
695  */
696 static int ice_vsi_get_qs(struct ice_vsi *vsi)
697 {
698 	struct ice_pf *pf = vsi->back;
699 	struct ice_qs_cfg tx_qs_cfg = {
700 		.qs_mutex = &pf->avail_q_mutex,
701 		.pf_map = pf->avail_txqs,
702 		.pf_map_size = pf->max_pf_txqs,
703 		.q_count = vsi->alloc_txq,
704 		.scatter_count = ICE_MAX_SCATTER_TXQS,
705 		.vsi_map = vsi->txq_map,
706 		.vsi_map_offset = 0,
707 		.mapping_mode = vsi->tx_mapping_mode
708 	};
709 	struct ice_qs_cfg rx_qs_cfg = {
710 		.qs_mutex = &pf->avail_q_mutex,
711 		.pf_map = pf->avail_rxqs,
712 		.pf_map_size = pf->max_pf_rxqs,
713 		.q_count = vsi->alloc_rxq,
714 		.scatter_count = ICE_MAX_SCATTER_RXQS,
715 		.vsi_map = vsi->rxq_map,
716 		.vsi_map_offset = 0,
717 		.mapping_mode = vsi->rx_mapping_mode
718 	};
719 	int ret = 0;
720 
721 	vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
722 	vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
723 
724 	ret = __ice_vsi_get_qs(&tx_qs_cfg);
725 	if (!ret)
726 		ret = __ice_vsi_get_qs(&rx_qs_cfg);
727 
728 	return ret;
729 }
730 
731 /**
732  * ice_vsi_put_qs - Release queues from VSI to PF
733  * @vsi: the VSI that is going to release queues
734  */
735 void ice_vsi_put_qs(struct ice_vsi *vsi)
736 {
737 	struct ice_pf *pf = vsi->back;
738 	int i;
739 
740 	mutex_lock(&pf->avail_q_mutex);
741 
742 	for (i = 0; i < vsi->alloc_txq; i++) {
743 		clear_bit(vsi->txq_map[i], pf->avail_txqs);
744 		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
745 	}
746 
747 	for (i = 0; i < vsi->alloc_rxq; i++) {
748 		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
749 		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
750 	}
751 
752 	mutex_unlock(&pf->avail_q_mutex);
753 }
754 
755 /**
756  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
757  * @vsi: the VSI being removed
758  */
759 static void ice_rss_clean(struct ice_vsi *vsi)
760 {
761 	struct ice_pf *pf;
762 
763 	pf = vsi->back;
764 
765 	if (vsi->rss_hkey_user)
766 		devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
767 	if (vsi->rss_lut_user)
768 		devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
769 }
770 
771 /**
772  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
773  * @vsi: the VSI being configured
774  */
775 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
776 {
777 	struct ice_hw_common_caps *cap;
778 	struct ice_pf *pf = vsi->back;
779 
780 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
781 		vsi->rss_size = 1;
782 		return;
783 	}
784 
785 	cap = &pf->hw.func_caps.common_cap;
786 	switch (vsi->type) {
787 	case ICE_VSI_PF:
788 		/* PF VSI will inherit RSS instance of PF */
789 		vsi->rss_table_size = cap->rss_table_size;
790 		vsi->rss_size = min_t(int, num_online_cpus(),
791 				      BIT(cap->rss_table_entry_width));
792 		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
793 		break;
794 	case ICE_VSI_VF:
795 		/* VF VSI will gets a small RSS table
796 		 * For VSI_LUT, LUT size should be set to 64 bytes
797 		 */
798 		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
799 		vsi->rss_size = min_t(int, num_online_cpus(),
800 				      BIT(cap->rss_table_entry_width));
801 		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
802 		break;
803 	case ICE_VSI_LB:
804 		break;
805 	default:
806 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n",
807 			 vsi->type);
808 		break;
809 	}
810 }
811 
812 /**
813  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
814  * @ctxt: the VSI context being set
815  *
816  * This initializes a default VSI context for all sections except the Queues.
817  */
818 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
819 {
820 	u32 table = 0;
821 
822 	memset(&ctxt->info, 0, sizeof(ctxt->info));
823 	/* VSI's should be allocated from shared pool */
824 	ctxt->alloc_from_pool = true;
825 	/* Src pruning enabled by default */
826 	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
827 	/* Traffic from VSI can be sent to LAN */
828 	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
829 	/* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
830 	 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
831 	 * packets untagged/tagged.
832 	 */
833 	ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
834 				  ICE_AQ_VSI_VLAN_MODE_M) >>
835 				 ICE_AQ_VSI_VLAN_MODE_S);
836 	/* Have 1:1 UP mapping for both ingress/egress tables */
837 	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
838 	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
839 	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
840 	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
841 	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
842 	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
843 	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
844 	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
845 	ctxt->info.ingress_table = cpu_to_le32(table);
846 	ctxt->info.egress_table = cpu_to_le32(table);
847 	/* Have 1:1 UP mapping for outer to inner UP table */
848 	ctxt->info.outer_up_table = cpu_to_le32(table);
849 	/* No Outer tag support outer_tag_flags remains to zero */
850 }
851 
852 /**
853  * ice_vsi_setup_q_map - Setup a VSI queue map
854  * @vsi: the VSI being configured
855  * @ctxt: VSI context structure
856  */
857 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
858 {
859 	u16 offset = 0, qmap = 0, tx_count = 0;
860 	u16 qcount_tx = vsi->alloc_txq;
861 	u16 qcount_rx = vsi->alloc_rxq;
862 	u16 tx_numq_tc, rx_numq_tc;
863 	u16 pow = 0, max_rss = 0;
864 	bool ena_tc0 = false;
865 	u8 netdev_tc = 0;
866 	int i;
867 
868 	/* at least TC0 should be enabled by default */
869 	if (vsi->tc_cfg.numtc) {
870 		if (!(vsi->tc_cfg.ena_tc & BIT(0)))
871 			ena_tc0 = true;
872 	} else {
873 		ena_tc0 = true;
874 	}
875 
876 	if (ena_tc0) {
877 		vsi->tc_cfg.numtc++;
878 		vsi->tc_cfg.ena_tc |= 1;
879 	}
880 
881 	rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
882 	if (!rx_numq_tc)
883 		rx_numq_tc = 1;
884 	tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
885 	if (!tx_numq_tc)
886 		tx_numq_tc = 1;
887 
888 	/* TC mapping is a function of the number of Rx queues assigned to the
889 	 * VSI for each traffic class and the offset of these queues.
890 	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
891 	 * queues allocated to TC0. No:of queues is a power-of-2.
892 	 *
893 	 * If TC is not enabled, the queue offset is set to 0, and allocate one
894 	 * queue, this way, traffic for the given TC will be sent to the default
895 	 * queue.
896 	 *
897 	 * Setup number and offset of Rx queues for all TCs for the VSI
898 	 */
899 
900 	qcount_rx = rx_numq_tc;
901 
902 	/* qcount will change if RSS is enabled */
903 	if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
904 		if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
905 			if (vsi->type == ICE_VSI_PF)
906 				max_rss = ICE_MAX_LG_RSS_QS;
907 			else
908 				max_rss = ICE_MAX_SMALL_RSS_QS;
909 			qcount_rx = min_t(int, rx_numq_tc, max_rss);
910 			qcount_rx = min_t(int, qcount_rx, vsi->rss_size);
911 		}
912 	}
913 
914 	/* find the (rounded up) power-of-2 of qcount */
915 	pow = order_base_2(qcount_rx);
916 
917 	ice_for_each_traffic_class(i) {
918 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
919 			/* TC is not enabled */
920 			vsi->tc_cfg.tc_info[i].qoffset = 0;
921 			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
922 			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
923 			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
924 			ctxt->info.tc_mapping[i] = 0;
925 			continue;
926 		}
927 
928 		/* TC is enabled */
929 		vsi->tc_cfg.tc_info[i].qoffset = offset;
930 		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
931 		vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
932 		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
933 
934 		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
935 			ICE_AQ_VSI_TC_Q_OFFSET_M) |
936 			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
937 			 ICE_AQ_VSI_TC_Q_NUM_M);
938 		offset += qcount_rx;
939 		tx_count += tx_numq_tc;
940 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
941 	}
942 
943 	/* if offset is non-zero, means it is calculated correctly based on
944 	 * enabled TCs for a given VSI otherwise qcount_rx will always
945 	 * be correct and non-zero because it is based off - VSI's
946 	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
947 	 * at least 1)
948 	 */
949 	if (offset)
950 		vsi->num_rxq = offset;
951 	else
952 		vsi->num_rxq = qcount_rx;
953 
954 	vsi->num_txq = tx_count;
955 
956 	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
957 		dev_dbg(&vsi->back->pdev->dev, "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
958 		/* since there is a chance that num_rxq could have been changed
959 		 * in the above for loop, make num_txq equal to num_rxq.
960 		 */
961 		vsi->num_txq = vsi->num_rxq;
962 	}
963 
964 	/* Rx queue mapping */
965 	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
966 	/* q_mapping buffer holds the info for the first queue allocated for
967 	 * this VSI in the PF space and also the number of queues associated
968 	 * with this VSI.
969 	 */
970 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
971 	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
972 }
973 
974 /**
975  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
976  * @ctxt: the VSI context being set
977  * @vsi: the VSI being configured
978  */
979 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
980 {
981 	u8 lut_type, hash_type;
982 	struct ice_pf *pf;
983 
984 	pf = vsi->back;
985 
986 	switch (vsi->type) {
987 	case ICE_VSI_PF:
988 		/* PF VSI will inherit RSS instance of PF */
989 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
990 		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
991 		break;
992 	case ICE_VSI_VF:
993 		/* VF VSI will gets a small RSS table which is a VSI LUT type */
994 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
995 		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
996 		break;
997 	case ICE_VSI_LB:
998 		dev_dbg(&pf->pdev->dev, "Unsupported VSI type %d\n", vsi->type);
999 		return;
1000 	default:
1001 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1002 		return;
1003 	}
1004 
1005 	ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1006 				ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1007 				((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1008 				 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1009 }
1010 
1011 /**
1012  * ice_vsi_init - Create and initialize a VSI
1013  * @vsi: the VSI being configured
1014  *
1015  * This initializes a VSI context depending on the VSI type to be added and
1016  * passes it down to the add_vsi aq command to create a new VSI.
1017  */
1018 static int ice_vsi_init(struct ice_vsi *vsi)
1019 {
1020 	struct ice_pf *pf = vsi->back;
1021 	struct ice_hw *hw = &pf->hw;
1022 	struct ice_vsi_ctx *ctxt;
1023 	int ret = 0;
1024 
1025 	ctxt = devm_kzalloc(&pf->pdev->dev, sizeof(*ctxt), GFP_KERNEL);
1026 	if (!ctxt)
1027 		return -ENOMEM;
1028 
1029 	ctxt->info = vsi->info;
1030 	switch (vsi->type) {
1031 	case ICE_VSI_LB:
1032 		/* fall through */
1033 	case ICE_VSI_PF:
1034 		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1035 		break;
1036 	case ICE_VSI_VF:
1037 		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1038 		/* VF number here is the absolute VF number (0-255) */
1039 		ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
1040 		break;
1041 	default:
1042 		return -ENODEV;
1043 	}
1044 
1045 	ice_set_dflt_vsi_ctx(ctxt);
1046 	/* if the switch is in VEB mode, allow VSI loopback */
1047 	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1048 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1049 
1050 	/* Set LUT type and HASH type if RSS is enabled */
1051 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1052 		ice_set_rss_vsi_ctx(ctxt, vsi);
1053 
1054 	ctxt->info.sw_id = vsi->port_info->sw_id;
1055 	ice_vsi_setup_q_map(vsi, ctxt);
1056 
1057 	/* Enable MAC Antispoof with new VSI being initialized or updated */
1058 	if (vsi->type == ICE_VSI_VF && pf->vf[vsi->vf_id].spoofchk) {
1059 		ctxt->info.valid_sections |=
1060 			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1061 		ctxt->info.sec_flags |=
1062 			ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
1063 	}
1064 
1065 	/* Allow control frames out of main VSI */
1066 	if (vsi->type == ICE_VSI_PF) {
1067 		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1068 		ctxt->info.valid_sections |=
1069 			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1070 	}
1071 
1072 	ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1073 	if (ret) {
1074 		dev_err(&pf->pdev->dev,
1075 			"Add VSI failed, err %d\n", ret);
1076 		return -EIO;
1077 	}
1078 
1079 	/* keep context for update VSI operations */
1080 	vsi->info = ctxt->info;
1081 
1082 	/* record VSI number returned */
1083 	vsi->vsi_num = ctxt->vsi_num;
1084 
1085 	devm_kfree(&pf->pdev->dev, ctxt);
1086 	return ret;
1087 }
1088 
1089 /**
1090  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
1091  * @vsi: VSI having the memory freed
1092  * @v_idx: index of the vector to be freed
1093  */
1094 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
1095 {
1096 	struct ice_q_vector *q_vector;
1097 	struct ice_pf *pf = vsi->back;
1098 	struct ice_ring *ring;
1099 
1100 	if (!vsi->q_vectors[v_idx]) {
1101 		dev_dbg(&pf->pdev->dev, "Queue vector at index %d not found\n",
1102 			v_idx);
1103 		return;
1104 	}
1105 	q_vector = vsi->q_vectors[v_idx];
1106 
1107 	ice_for_each_ring(ring, q_vector->tx)
1108 		ring->q_vector = NULL;
1109 	ice_for_each_ring(ring, q_vector->rx)
1110 		ring->q_vector = NULL;
1111 
1112 	/* only VSI with an associated netdev is set up with NAPI */
1113 	if (vsi->netdev)
1114 		netif_napi_del(&q_vector->napi);
1115 
1116 	devm_kfree(&pf->pdev->dev, q_vector);
1117 	vsi->q_vectors[v_idx] = NULL;
1118 }
1119 
1120 /**
1121  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
1122  * @vsi: the VSI having memory freed
1123  */
1124 void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
1125 {
1126 	int v_idx;
1127 
1128 	ice_for_each_q_vector(vsi, v_idx)
1129 		ice_free_q_vector(vsi, v_idx);
1130 }
1131 
1132 /**
1133  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
1134  * @vsi: the VSI being configured
1135  * @v_idx: index of the vector in the VSI struct
1136  *
1137  * We allocate one q_vector. If allocation fails we return -ENOMEM.
1138  */
1139 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
1140 {
1141 	struct ice_pf *pf = vsi->back;
1142 	struct ice_q_vector *q_vector;
1143 
1144 	/* allocate q_vector */
1145 	q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
1146 	if (!q_vector)
1147 		return -ENOMEM;
1148 
1149 	q_vector->vsi = vsi;
1150 	q_vector->v_idx = v_idx;
1151 	if (vsi->type == ICE_VSI_VF)
1152 		goto out;
1153 	/* only set affinity_mask if the CPU is online */
1154 	if (cpu_online(v_idx))
1155 		cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
1156 
1157 	/* This will not be called in the driver load path because the netdev
1158 	 * will not be created yet. All other cases with register the NAPI
1159 	 * handler here (i.e. resume, reset/rebuild, etc.)
1160 	 */
1161 	if (vsi->netdev)
1162 		netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
1163 			       NAPI_POLL_WEIGHT);
1164 
1165 out:
1166 	/* tie q_vector and VSI together */
1167 	vsi->q_vectors[v_idx] = q_vector;
1168 
1169 	return 0;
1170 }
1171 
1172 /**
1173  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
1174  * @vsi: the VSI being configured
1175  *
1176  * We allocate one q_vector per queue interrupt. If allocation fails we
1177  * return -ENOMEM.
1178  */
1179 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
1180 {
1181 	struct ice_pf *pf = vsi->back;
1182 	int v_idx = 0, num_q_vectors;
1183 	int err;
1184 
1185 	if (vsi->q_vectors[0]) {
1186 		dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
1187 			vsi->vsi_num);
1188 		return -EEXIST;
1189 	}
1190 
1191 	num_q_vectors = vsi->num_q_vectors;
1192 
1193 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
1194 		err = ice_vsi_alloc_q_vector(vsi, v_idx);
1195 		if (err)
1196 			goto err_out;
1197 	}
1198 
1199 	return 0;
1200 
1201 err_out:
1202 	while (v_idx--)
1203 		ice_free_q_vector(vsi, v_idx);
1204 
1205 	dev_err(&pf->pdev->dev,
1206 		"Failed to allocate %d q_vector for VSI %d, ret=%d\n",
1207 		vsi->num_q_vectors, vsi->vsi_num, err);
1208 	vsi->num_q_vectors = 0;
1209 	return err;
1210 }
1211 
1212 /**
1213  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1214  * @vsi: ptr to the VSI
1215  *
1216  * This should only be called after ice_vsi_alloc() which allocates the
1217  * corresponding SW VSI structure and initializes num_queue_pairs for the
1218  * newly allocated VSI.
1219  *
1220  * Returns 0 on success or negative on failure
1221  */
1222 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1223 {
1224 	struct ice_pf *pf = vsi->back;
1225 	u16 num_q_vectors;
1226 
1227 	/* SRIOV doesn't grab irq_tracker entries for each VSI */
1228 	if (vsi->type == ICE_VSI_VF)
1229 		return 0;
1230 
1231 	if (vsi->base_vector) {
1232 		dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
1233 			vsi->vsi_num, vsi->base_vector);
1234 		return -EEXIST;
1235 	}
1236 
1237 	num_q_vectors = vsi->num_q_vectors;
1238 	/* reserve slots from OS requested IRQs */
1239 	vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1240 				       vsi->idx);
1241 	if (vsi->base_vector < 0) {
1242 		dev_err(&pf->pdev->dev,
1243 			"Failed to get tracking for %d vectors for VSI %d, err=%d\n",
1244 			num_q_vectors, vsi->vsi_num, vsi->base_vector);
1245 		return -ENOENT;
1246 	}
1247 	pf->num_avail_sw_msix -= num_q_vectors;
1248 
1249 	return 0;
1250 }
1251 
1252 /**
1253  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1254  * @vsi: the VSI having rings deallocated
1255  */
1256 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1257 {
1258 	int i;
1259 
1260 	if (vsi->tx_rings) {
1261 		for (i = 0; i < vsi->alloc_txq; i++) {
1262 			if (vsi->tx_rings[i]) {
1263 				kfree_rcu(vsi->tx_rings[i], rcu);
1264 				vsi->tx_rings[i] = NULL;
1265 			}
1266 		}
1267 	}
1268 	if (vsi->rx_rings) {
1269 		for (i = 0; i < vsi->alloc_rxq; i++) {
1270 			if (vsi->rx_rings[i]) {
1271 				kfree_rcu(vsi->rx_rings[i], rcu);
1272 				vsi->rx_rings[i] = NULL;
1273 			}
1274 		}
1275 	}
1276 }
1277 
1278 /**
1279  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1280  * @vsi: VSI which is having rings allocated
1281  */
1282 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1283 {
1284 	struct ice_pf *pf = vsi->back;
1285 	int i;
1286 
1287 	/* Allocate Tx rings */
1288 	for (i = 0; i < vsi->alloc_txq; i++) {
1289 		struct ice_ring *ring;
1290 
1291 		/* allocate with kzalloc(), free with kfree_rcu() */
1292 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1293 
1294 		if (!ring)
1295 			goto err_out;
1296 
1297 		ring->q_index = i;
1298 		ring->reg_idx = vsi->txq_map[i];
1299 		ring->ring_active = false;
1300 		ring->vsi = vsi;
1301 		ring->dev = &pf->pdev->dev;
1302 		ring->count = vsi->num_tx_desc;
1303 		vsi->tx_rings[i] = ring;
1304 	}
1305 
1306 	/* Allocate Rx rings */
1307 	for (i = 0; i < vsi->alloc_rxq; i++) {
1308 		struct ice_ring *ring;
1309 
1310 		/* allocate with kzalloc(), free with kfree_rcu() */
1311 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1312 		if (!ring)
1313 			goto err_out;
1314 
1315 		ring->q_index = i;
1316 		ring->reg_idx = vsi->rxq_map[i];
1317 		ring->ring_active = false;
1318 		ring->vsi = vsi;
1319 		ring->netdev = vsi->netdev;
1320 		ring->dev = &pf->pdev->dev;
1321 		ring->count = vsi->num_rx_desc;
1322 		vsi->rx_rings[i] = ring;
1323 	}
1324 
1325 	return 0;
1326 
1327 err_out:
1328 	ice_vsi_clear_rings(vsi);
1329 	return -ENOMEM;
1330 }
1331 
1332 /**
1333  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1334  * @vsi: the VSI being configured
1335  *
1336  * This function maps descriptor rings to the queue-specific vectors allotted
1337  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1338  * and Rx rings to the vector as "efficiently" as possible.
1339  */
1340 #ifdef CONFIG_DCB
1341 void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1342 #else
1343 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1344 #endif /* CONFIG_DCB */
1345 {
1346 	int q_vectors = vsi->num_q_vectors;
1347 	int tx_rings_rem, rx_rings_rem;
1348 	int v_id;
1349 
1350 	/* initially assigning remaining rings count to VSIs num queue value */
1351 	tx_rings_rem = vsi->num_txq;
1352 	rx_rings_rem = vsi->num_rxq;
1353 
1354 	for (v_id = 0; v_id < q_vectors; v_id++) {
1355 		struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1356 		int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1357 
1358 		/* Tx rings mapping to vector */
1359 		tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1360 		q_vector->num_ring_tx = tx_rings_per_v;
1361 		q_vector->tx.ring = NULL;
1362 		q_vector->tx.itr_idx = ICE_TX_ITR;
1363 		q_base = vsi->num_txq - tx_rings_rem;
1364 
1365 		for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1366 			struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1367 
1368 			tx_ring->q_vector = q_vector;
1369 			tx_ring->next = q_vector->tx.ring;
1370 			q_vector->tx.ring = tx_ring;
1371 		}
1372 		tx_rings_rem -= tx_rings_per_v;
1373 
1374 		/* Rx rings mapping to vector */
1375 		rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1376 		q_vector->num_ring_rx = rx_rings_per_v;
1377 		q_vector->rx.ring = NULL;
1378 		q_vector->rx.itr_idx = ICE_RX_ITR;
1379 		q_base = vsi->num_rxq - rx_rings_rem;
1380 
1381 		for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1382 			struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1383 
1384 			rx_ring->q_vector = q_vector;
1385 			rx_ring->next = q_vector->rx.ring;
1386 			q_vector->rx.ring = rx_ring;
1387 		}
1388 		rx_rings_rem -= rx_rings_per_v;
1389 	}
1390 }
1391 
1392 /**
1393  * ice_vsi_manage_rss_lut - disable/enable RSS
1394  * @vsi: the VSI being changed
1395  * @ena: boolean value indicating if this is an enable or disable request
1396  *
1397  * In the event of disable request for RSS, this function will zero out RSS
1398  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1399  * LUT.
1400  */
1401 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1402 {
1403 	int err = 0;
1404 	u8 *lut;
1405 
1406 	lut = devm_kzalloc(&vsi->back->pdev->dev, vsi->rss_table_size,
1407 			   GFP_KERNEL);
1408 	if (!lut)
1409 		return -ENOMEM;
1410 
1411 	if (ena) {
1412 		if (vsi->rss_lut_user)
1413 			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1414 		else
1415 			ice_fill_rss_lut(lut, vsi->rss_table_size,
1416 					 vsi->rss_size);
1417 	}
1418 
1419 	err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1420 	devm_kfree(&vsi->back->pdev->dev, lut);
1421 	return err;
1422 }
1423 
1424 /**
1425  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1426  * @vsi: VSI to be configured
1427  */
1428 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1429 {
1430 	struct ice_aqc_get_set_rss_keys *key;
1431 	struct ice_pf *pf = vsi->back;
1432 	enum ice_status status;
1433 	int err = 0;
1434 	u8 *lut;
1435 
1436 	vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1437 
1438 	lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
1439 	if (!lut)
1440 		return -ENOMEM;
1441 
1442 	if (vsi->rss_lut_user)
1443 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1444 	else
1445 		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1446 
1447 	status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1448 				    vsi->rss_table_size);
1449 
1450 	if (status) {
1451 		dev_err(&pf->pdev->dev,
1452 			"set_rss_lut failed, error %d\n", status);
1453 		err = -EIO;
1454 		goto ice_vsi_cfg_rss_exit;
1455 	}
1456 
1457 	key = devm_kzalloc(&pf->pdev->dev, sizeof(*key), GFP_KERNEL);
1458 	if (!key) {
1459 		err = -ENOMEM;
1460 		goto ice_vsi_cfg_rss_exit;
1461 	}
1462 
1463 	if (vsi->rss_hkey_user)
1464 		memcpy(key,
1465 		       (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1466 		       ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1467 	else
1468 		netdev_rss_key_fill((void *)key,
1469 				    ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1470 
1471 	status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1472 
1473 	if (status) {
1474 		dev_err(&pf->pdev->dev, "set_rss_key failed, error %d\n",
1475 			status);
1476 		err = -EIO;
1477 	}
1478 
1479 	devm_kfree(&pf->pdev->dev, key);
1480 ice_vsi_cfg_rss_exit:
1481 	devm_kfree(&pf->pdev->dev, lut);
1482 	return err;
1483 }
1484 
1485 /**
1486  * ice_add_mac_to_list - Add a MAC address filter entry to the list
1487  * @vsi: the VSI to be forwarded to
1488  * @add_list: pointer to the list which contains MAC filter entries
1489  * @macaddr: the MAC address to be added.
1490  *
1491  * Adds MAC address filter entry to the temp list
1492  *
1493  * Returns 0 on success or ENOMEM on failure.
1494  */
1495 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1496 			const u8 *macaddr)
1497 {
1498 	struct ice_fltr_list_entry *tmp;
1499 	struct ice_pf *pf = vsi->back;
1500 
1501 	tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
1502 	if (!tmp)
1503 		return -ENOMEM;
1504 
1505 	tmp->fltr_info.flag = ICE_FLTR_TX;
1506 	tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1507 	tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1508 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1509 	tmp->fltr_info.vsi_handle = vsi->idx;
1510 	ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1511 
1512 	INIT_LIST_HEAD(&tmp->list_entry);
1513 	list_add(&tmp->list_entry, add_list);
1514 
1515 	return 0;
1516 }
1517 
1518 /**
1519  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1520  * @vsi: the VSI to be updated
1521  */
1522 void ice_update_eth_stats(struct ice_vsi *vsi)
1523 {
1524 	struct ice_eth_stats *prev_es, *cur_es;
1525 	struct ice_hw *hw = &vsi->back->hw;
1526 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1527 
1528 	prev_es = &vsi->eth_stats_prev;
1529 	cur_es = &vsi->eth_stats;
1530 
1531 	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1532 			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1533 
1534 	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1535 			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1536 
1537 	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1538 			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1539 
1540 	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1541 			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1542 
1543 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1544 			  &prev_es->rx_discards, &cur_es->rx_discards);
1545 
1546 	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1547 			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1548 
1549 	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1550 			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1551 
1552 	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1553 			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1554 
1555 	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1556 			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1557 
1558 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1559 			  &prev_es->tx_errors, &cur_es->tx_errors);
1560 
1561 	vsi->stat_offsets_loaded = true;
1562 }
1563 
1564 /**
1565  * ice_free_fltr_list - free filter lists helper
1566  * @dev: pointer to the device struct
1567  * @h: pointer to the list head to be freed
1568  *
1569  * Helper function to free filter lists previously created using
1570  * ice_add_mac_to_list
1571  */
1572 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1573 {
1574 	struct ice_fltr_list_entry *e, *tmp;
1575 
1576 	list_for_each_entry_safe(e, tmp, h, list_entry) {
1577 		list_del(&e->list_entry);
1578 		devm_kfree(dev, e);
1579 	}
1580 }
1581 
1582 /**
1583  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1584  * @vsi: the VSI being configured
1585  * @vid: VLAN ID to be added
1586  */
1587 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1588 {
1589 	struct ice_fltr_list_entry *tmp;
1590 	struct ice_pf *pf = vsi->back;
1591 	LIST_HEAD(tmp_add_list);
1592 	enum ice_status status;
1593 	int err = 0;
1594 
1595 	tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
1596 	if (!tmp)
1597 		return -ENOMEM;
1598 
1599 	tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1600 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1601 	tmp->fltr_info.flag = ICE_FLTR_TX;
1602 	tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1603 	tmp->fltr_info.vsi_handle = vsi->idx;
1604 	tmp->fltr_info.l_data.vlan.vlan_id = vid;
1605 
1606 	INIT_LIST_HEAD(&tmp->list_entry);
1607 	list_add(&tmp->list_entry, &tmp_add_list);
1608 
1609 	status = ice_add_vlan(&pf->hw, &tmp_add_list);
1610 	if (status) {
1611 		err = -ENODEV;
1612 		dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
1613 			vid, vsi->vsi_num);
1614 	}
1615 
1616 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1617 	return err;
1618 }
1619 
1620 /**
1621  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1622  * @vsi: the VSI being configured
1623  * @vid: VLAN ID to be removed
1624  *
1625  * Returns 0 on success and negative on failure
1626  */
1627 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1628 {
1629 	struct ice_fltr_list_entry *list;
1630 	struct ice_pf *pf = vsi->back;
1631 	LIST_HEAD(tmp_add_list);
1632 	enum ice_status status;
1633 	int err = 0;
1634 
1635 	list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1636 	if (!list)
1637 		return -ENOMEM;
1638 
1639 	list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1640 	list->fltr_info.vsi_handle = vsi->idx;
1641 	list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1642 	list->fltr_info.l_data.vlan.vlan_id = vid;
1643 	list->fltr_info.flag = ICE_FLTR_TX;
1644 	list->fltr_info.src_id = ICE_SRC_ID_VSI;
1645 
1646 	INIT_LIST_HEAD(&list->list_entry);
1647 	list_add(&list->list_entry, &tmp_add_list);
1648 
1649 	status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1650 	if (status == ICE_ERR_DOES_NOT_EXIST) {
1651 		dev_dbg(&pf->pdev->dev,
1652 			"Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1653 			vid, vsi->vsi_num, status);
1654 	} else if (status) {
1655 		dev_err(&pf->pdev->dev,
1656 			"Error removing VLAN %d on vsi %i error: %d\n",
1657 			vid, vsi->vsi_num, status);
1658 		err = -EIO;
1659 	}
1660 
1661 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1662 	return err;
1663 }
1664 
1665 /**
1666  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1667  * @vsi: the VSI being configured
1668  *
1669  * Return 0 on success and a negative value on error
1670  * Configure the Rx VSI for operation.
1671  */
1672 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1673 {
1674 	u16 i;
1675 
1676 	if (vsi->type == ICE_VSI_VF)
1677 		goto setup_rings;
1678 
1679 	if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
1680 		vsi->max_frame = vsi->netdev->mtu +
1681 			ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1682 	else
1683 		vsi->max_frame = ICE_RXBUF_2048;
1684 
1685 	vsi->rx_buf_len = ICE_RXBUF_2048;
1686 setup_rings:
1687 	/* set up individual rings */
1688 	for (i = 0; i < vsi->num_rxq; i++) {
1689 		int err;
1690 
1691 		err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1692 		if (err) {
1693 			dev_err(&vsi->back->pdev->dev,
1694 				"ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1695 				i, err);
1696 			return err;
1697 		}
1698 	}
1699 
1700 	return 0;
1701 }
1702 
1703 /**
1704  * ice_vsi_cfg_txq - Configure single Tx queue
1705  * @vsi: the VSI that queue belongs to
1706  * @ring: Tx ring to be configured
1707  * @tc_q_idx: queue index within given TC
1708  * @qg_buf: queue group buffer
1709  * @tc: TC that Tx ring belongs to
1710  */
1711 static int
1712 ice_vsi_cfg_txq(struct ice_vsi *vsi, struct ice_ring *ring, u16 tc_q_idx,
1713 		struct ice_aqc_add_tx_qgrp *qg_buf, u8 tc)
1714 {
1715 	struct ice_tlan_ctx tlan_ctx = { 0 };
1716 	struct ice_aqc_add_txqs_perq *txq;
1717 	struct ice_pf *pf = vsi->back;
1718 	u8 buf_len = sizeof(*qg_buf);
1719 	enum ice_status status;
1720 	u16 pf_q;
1721 
1722 	pf_q = ring->reg_idx;
1723 	ice_setup_tx_ctx(ring, &tlan_ctx, pf_q);
1724 	/* copy context contents into the qg_buf */
1725 	qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
1726 	ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
1727 		    ice_tlan_ctx_info);
1728 
1729 	/* init queue specific tail reg. It is referred as
1730 	 * transmit comm scheduler queue doorbell.
1731 	 */
1732 	ring->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
1733 
1734 	/* Add unique software queue handle of the Tx queue per
1735 	 * TC into the VSI Tx ring
1736 	 */
1737 	ring->q_handle = tc_q_idx;
1738 
1739 	status = ice_ena_vsi_txq(vsi->port_info, vsi->idx, tc, ring->q_handle,
1740 				 1, qg_buf, buf_len, NULL);
1741 	if (status) {
1742 		dev_err(&pf->pdev->dev,
1743 			"Failed to set LAN Tx queue context, error: %d\n",
1744 			status);
1745 		return -ENODEV;
1746 	}
1747 
1748 	/* Add Tx Queue TEID into the VSI Tx ring from the
1749 	 * response. This will complete configuring and
1750 	 * enabling the queue.
1751 	 */
1752 	txq = &qg_buf->txqs[0];
1753 	if (pf_q == le16_to_cpu(txq->txq_id))
1754 		ring->txq_teid = le32_to_cpu(txq->q_teid);
1755 
1756 	return 0;
1757 }
1758 
1759 /**
1760  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1761  * @vsi: the VSI being configured
1762  * @rings: Tx ring array to be configured
1763  * @offset: offset within vsi->txq_map
1764  *
1765  * Return 0 on success and a negative value on error
1766  * Configure the Tx VSI for operation.
1767  */
1768 static int
1769 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, int offset)
1770 {
1771 	struct ice_aqc_add_tx_qgrp *qg_buf;
1772 	struct ice_pf *pf = vsi->back;
1773 	u16 q_idx = 0, i;
1774 	int err = 0;
1775 	u8 tc;
1776 
1777 	qg_buf = devm_kzalloc(&pf->pdev->dev, sizeof(*qg_buf), GFP_KERNEL);
1778 	if (!qg_buf)
1779 		return -ENOMEM;
1780 
1781 	qg_buf->num_txqs = 1;
1782 
1783 	/* set up and configure the Tx queues for each enabled TC */
1784 	ice_for_each_traffic_class(tc) {
1785 		if (!(vsi->tc_cfg.ena_tc & BIT(tc)))
1786 			break;
1787 
1788 		for (i = 0; i < vsi->tc_cfg.tc_info[tc].qcount_tx; i++) {
1789 			err = ice_vsi_cfg_txq(vsi, rings[q_idx], i + offset,
1790 					      qg_buf, tc);
1791 			if (err)
1792 				goto err_cfg_txqs;
1793 
1794 			q_idx++;
1795 		}
1796 	}
1797 err_cfg_txqs:
1798 	devm_kfree(&pf->pdev->dev, qg_buf);
1799 	return err;
1800 }
1801 
1802 /**
1803  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1804  * @vsi: the VSI being configured
1805  *
1806  * Return 0 on success and a negative value on error
1807  * Configure the Tx VSI for operation.
1808  */
1809 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1810 {
1811 	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, 0);
1812 }
1813 
1814 /**
1815  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1816  * @intrl: interrupt rate limit in usecs
1817  * @gran: interrupt rate limit granularity in usecs
1818  *
1819  * This function converts a decimal interrupt rate limit in usecs to the format
1820  * expected by firmware.
1821  */
1822 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1823 {
1824 	u32 val = intrl / gran;
1825 
1826 	if (val)
1827 		return val | GLINT_RATE_INTRL_ENA_M;
1828 	return 0;
1829 }
1830 
1831 /**
1832  * ice_cfg_itr_gran - set the ITR granularity to 2 usecs if not already set
1833  * @hw: board specific structure
1834  */
1835 static void ice_cfg_itr_gran(struct ice_hw *hw)
1836 {
1837 	u32 regval = rd32(hw, GLINT_CTL);
1838 
1839 	/* no need to update global register if ITR gran is already set */
1840 	if (!(regval & GLINT_CTL_DIS_AUTOMASK_M) &&
1841 	    (((regval & GLINT_CTL_ITR_GRAN_200_M) >>
1842 	     GLINT_CTL_ITR_GRAN_200_S) == ICE_ITR_GRAN_US) &&
1843 	    (((regval & GLINT_CTL_ITR_GRAN_100_M) >>
1844 	     GLINT_CTL_ITR_GRAN_100_S) == ICE_ITR_GRAN_US) &&
1845 	    (((regval & GLINT_CTL_ITR_GRAN_50_M) >>
1846 	     GLINT_CTL_ITR_GRAN_50_S) == ICE_ITR_GRAN_US) &&
1847 	    (((regval & GLINT_CTL_ITR_GRAN_25_M) >>
1848 	      GLINT_CTL_ITR_GRAN_25_S) == ICE_ITR_GRAN_US))
1849 		return;
1850 
1851 	regval = ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_200_S) &
1852 		  GLINT_CTL_ITR_GRAN_200_M) |
1853 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_100_S) &
1854 		  GLINT_CTL_ITR_GRAN_100_M) |
1855 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_50_S) &
1856 		  GLINT_CTL_ITR_GRAN_50_M) |
1857 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_25_S) &
1858 		  GLINT_CTL_ITR_GRAN_25_M);
1859 	wr32(hw, GLINT_CTL, regval);
1860 }
1861 
1862 /**
1863  * ice_cfg_itr - configure the initial interrupt throttle values
1864  * @hw: pointer to the HW structure
1865  * @q_vector: interrupt vector that's being configured
1866  *
1867  * Configure interrupt throttling values for the ring containers that are
1868  * associated with the interrupt vector passed in.
1869  */
1870 static void
1871 ice_cfg_itr(struct ice_hw *hw, struct ice_q_vector *q_vector)
1872 {
1873 	ice_cfg_itr_gran(hw);
1874 
1875 	if (q_vector->num_ring_rx) {
1876 		struct ice_ring_container *rc = &q_vector->rx;
1877 
1878 		/* if this value is set then don't overwrite with default */
1879 		if (!rc->itr_setting)
1880 			rc->itr_setting = ICE_DFLT_RX_ITR;
1881 
1882 		rc->target_itr = ITR_TO_REG(rc->itr_setting);
1883 		rc->next_update = jiffies + 1;
1884 		rc->current_itr = rc->target_itr;
1885 		wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1886 		     ITR_REG_ALIGN(rc->current_itr) >> ICE_ITR_GRAN_S);
1887 	}
1888 
1889 	if (q_vector->num_ring_tx) {
1890 		struct ice_ring_container *rc = &q_vector->tx;
1891 
1892 		/* if this value is set then don't overwrite with default */
1893 		if (!rc->itr_setting)
1894 			rc->itr_setting = ICE_DFLT_TX_ITR;
1895 
1896 		rc->target_itr = ITR_TO_REG(rc->itr_setting);
1897 		rc->next_update = jiffies + 1;
1898 		rc->current_itr = rc->target_itr;
1899 		wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1900 		     ITR_REG_ALIGN(rc->current_itr) >> ICE_ITR_GRAN_S);
1901 	}
1902 }
1903 
1904 /**
1905  * ice_cfg_txq_interrupt - configure interrupt on Tx queue
1906  * @vsi: the VSI being configured
1907  * @txq: Tx queue being mapped to MSI-X vector
1908  * @msix_idx: MSI-X vector index within the function
1909  * @itr_idx: ITR index of the interrupt cause
1910  *
1911  * Configure interrupt on Tx queue by associating Tx queue to MSI-X vector
1912  * within the function space.
1913  */
1914 #ifdef CONFIG_PCI_IOV
1915 void
1916 ice_cfg_txq_interrupt(struct ice_vsi *vsi, u16 txq, u16 msix_idx, u16 itr_idx)
1917 #else
1918 static void
1919 ice_cfg_txq_interrupt(struct ice_vsi *vsi, u16 txq, u16 msix_idx, u16 itr_idx)
1920 #endif /* CONFIG_PCI_IOV */
1921 {
1922 	struct ice_pf *pf = vsi->back;
1923 	struct ice_hw *hw = &pf->hw;
1924 	u32 val;
1925 
1926 	itr_idx = (itr_idx << QINT_TQCTL_ITR_INDX_S) & QINT_TQCTL_ITR_INDX_M;
1927 
1928 	val = QINT_TQCTL_CAUSE_ENA_M | itr_idx |
1929 	      ((msix_idx << QINT_TQCTL_MSIX_INDX_S) & QINT_TQCTL_MSIX_INDX_M);
1930 
1931 	wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1932 }
1933 
1934 /**
1935  * ice_cfg_rxq_interrupt - configure interrupt on Rx queue
1936  * @vsi: the VSI being configured
1937  * @rxq: Rx queue being mapped to MSI-X vector
1938  * @msix_idx: MSI-X vector index within the function
1939  * @itr_idx: ITR index of the interrupt cause
1940  *
1941  * Configure interrupt on Rx queue by associating Rx queue to MSI-X vector
1942  * within the function space.
1943  */
1944 #ifdef CONFIG_PCI_IOV
1945 void
1946 ice_cfg_rxq_interrupt(struct ice_vsi *vsi, u16 rxq, u16 msix_idx, u16 itr_idx)
1947 #else
1948 static void
1949 ice_cfg_rxq_interrupt(struct ice_vsi *vsi, u16 rxq, u16 msix_idx, u16 itr_idx)
1950 #endif /* CONFIG_PCI_IOV */
1951 {
1952 	struct ice_pf *pf = vsi->back;
1953 	struct ice_hw *hw = &pf->hw;
1954 	u32 val;
1955 
1956 	itr_idx = (itr_idx << QINT_RQCTL_ITR_INDX_S) & QINT_RQCTL_ITR_INDX_M;
1957 
1958 	val = QINT_RQCTL_CAUSE_ENA_M | itr_idx |
1959 	      ((msix_idx << QINT_RQCTL_MSIX_INDX_S) & QINT_RQCTL_MSIX_INDX_M);
1960 
1961 	wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1962 
1963 	ice_flush(hw);
1964 }
1965 
1966 /**
1967  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1968  * @vsi: the VSI being configured
1969  *
1970  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1971  * for the VF VSI.
1972  */
1973 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1974 {
1975 	struct ice_pf *pf = vsi->back;
1976 	struct ice_hw *hw = &pf->hw;
1977 	u32 txq = 0, rxq = 0;
1978 	int i, q;
1979 
1980 	for (i = 0; i < vsi->num_q_vectors; i++) {
1981 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1982 		u16 reg_idx = q_vector->reg_idx;
1983 
1984 		ice_cfg_itr(hw, q_vector);
1985 
1986 		wr32(hw, GLINT_RATE(reg_idx),
1987 		     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1988 
1989 		/* Both Transmit Queue Interrupt Cause Control register
1990 		 * and Receive Queue Interrupt Cause control register
1991 		 * expects MSIX_INDX field to be the vector index
1992 		 * within the function space and not the absolute
1993 		 * vector index across PF or across device.
1994 		 * For SR-IOV VF VSIs queue vector index always starts
1995 		 * with 1 since first vector index(0) is used for OICR
1996 		 * in VF space. Since VMDq and other PF VSIs are within
1997 		 * the PF function space, use the vector index that is
1998 		 * tracked for this PF.
1999 		 */
2000 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2001 			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2002 					      q_vector->tx.itr_idx);
2003 			txq++;
2004 		}
2005 
2006 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2007 			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2008 					      q_vector->rx.itr_idx);
2009 			rxq++;
2010 		}
2011 	}
2012 }
2013 
2014 /**
2015  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
2016  * @vsi: the VSI being changed
2017  */
2018 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
2019 {
2020 	struct device *dev = &vsi->back->pdev->dev;
2021 	struct ice_hw *hw = &vsi->back->hw;
2022 	struct ice_vsi_ctx *ctxt;
2023 	enum ice_status status;
2024 	int ret = 0;
2025 
2026 	ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
2027 	if (!ctxt)
2028 		return -ENOMEM;
2029 
2030 	/* Here we are configuring the VSI to let the driver add VLAN tags by
2031 	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
2032 	 * insertion happens in the Tx hot path, in ice_tx_map.
2033 	 */
2034 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
2035 
2036 	/* Preserve existing VLAN strip setting */
2037 	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
2038 				  ICE_AQ_VSI_VLAN_EMOD_M);
2039 
2040 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2041 
2042 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2043 	if (status) {
2044 		dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
2045 			status, hw->adminq.sq_last_status);
2046 		ret = -EIO;
2047 		goto out;
2048 	}
2049 
2050 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2051 out:
2052 	devm_kfree(dev, ctxt);
2053 	return ret;
2054 }
2055 
2056 /**
2057  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
2058  * @vsi: the VSI being changed
2059  * @ena: boolean value indicating if this is a enable or disable request
2060  */
2061 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
2062 {
2063 	struct device *dev = &vsi->back->pdev->dev;
2064 	struct ice_hw *hw = &vsi->back->hw;
2065 	struct ice_vsi_ctx *ctxt;
2066 	enum ice_status status;
2067 	int ret = 0;
2068 
2069 	ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
2070 	if (!ctxt)
2071 		return -ENOMEM;
2072 
2073 	/* Here we are configuring what the VSI should do with the VLAN tag in
2074 	 * the Rx packet. We can either leave the tag in the packet or put it in
2075 	 * the Rx descriptor.
2076 	 */
2077 	if (ena)
2078 		/* Strip VLAN tag from Rx packet and put it in the desc */
2079 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
2080 	else
2081 		/* Disable stripping. Leave tag in packet */
2082 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2083 
2084 	/* Allow all packets untagged/tagged */
2085 	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
2086 
2087 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2088 
2089 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2090 	if (status) {
2091 		dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
2092 			ena, status, hw->adminq.sq_last_status);
2093 		ret = -EIO;
2094 		goto out;
2095 	}
2096 
2097 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2098 out:
2099 	devm_kfree(dev, ctxt);
2100 	return ret;
2101 }
2102 
2103 /**
2104  * ice_vsi_start_rx_rings - start VSI's Rx rings
2105  * @vsi: the VSI whose rings are to be started
2106  *
2107  * Returns 0 on success and a negative value on error
2108  */
2109 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
2110 {
2111 	return ice_vsi_ctrl_rx_rings(vsi, true);
2112 }
2113 
2114 /**
2115  * ice_vsi_stop_rx_rings - stop VSI's Rx rings
2116  * @vsi: the VSI
2117  *
2118  * Returns 0 on success and a negative value on error
2119  */
2120 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
2121 {
2122 	return ice_vsi_ctrl_rx_rings(vsi, false);
2123 }
2124 
2125 /**
2126  * ice_trigger_sw_intr - trigger a software interrupt
2127  * @hw: pointer to the HW structure
2128  * @q_vector: interrupt vector to trigger the software interrupt for
2129  */
2130 void ice_trigger_sw_intr(struct ice_hw *hw, struct ice_q_vector *q_vector)
2131 {
2132 	wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx),
2133 	     (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S) |
2134 	     GLINT_DYN_CTL_SWINT_TRIG_M |
2135 	     GLINT_DYN_CTL_INTENA_M);
2136 }
2137 
2138 /**
2139  * ice_vsi_stop_tx_ring - Disable single Tx ring
2140  * @vsi: the VSI being configured
2141  * @rst_src: reset source
2142  * @rel_vmvf_num: Relative ID of VF/VM
2143  * @ring: Tx ring to be stopped
2144  * @txq_meta: Meta data of Tx ring to be stopped
2145  */
2146 #ifndef CONFIG_PCI_IOV
2147 static
2148 #endif /* !CONFIG_PCI_IOV */
2149 int
2150 ice_vsi_stop_tx_ring(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2151 		     u16 rel_vmvf_num, struct ice_ring *ring,
2152 		     struct ice_txq_meta *txq_meta)
2153 {
2154 	struct ice_pf *pf = vsi->back;
2155 	struct ice_q_vector *q_vector;
2156 	struct ice_hw *hw = &pf->hw;
2157 	enum ice_status status;
2158 	u32 val;
2159 
2160 	/* clear cause_ena bit for disabled queues */
2161 	val = rd32(hw, QINT_TQCTL(ring->reg_idx));
2162 	val &= ~QINT_TQCTL_CAUSE_ENA_M;
2163 	wr32(hw, QINT_TQCTL(ring->reg_idx), val);
2164 
2165 	/* software is expected to wait for 100 ns */
2166 	ndelay(100);
2167 
2168 	/* trigger a software interrupt for the vector
2169 	 * associated to the queue to schedule NAPI handler
2170 	 */
2171 	q_vector = ring->q_vector;
2172 	if (q_vector)
2173 		ice_trigger_sw_intr(hw, q_vector);
2174 
2175 	status = ice_dis_vsi_txq(vsi->port_info, txq_meta->vsi_idx,
2176 				 txq_meta->tc, 1, &txq_meta->q_handle,
2177 				 &txq_meta->q_id, &txq_meta->q_teid, rst_src,
2178 				 rel_vmvf_num, NULL);
2179 
2180 	/* if the disable queue command was exercised during an
2181 	 * active reset flow, ICE_ERR_RESET_ONGOING is returned.
2182 	 * This is not an error as the reset operation disables
2183 	 * queues at the hardware level anyway.
2184 	 */
2185 	if (status == ICE_ERR_RESET_ONGOING) {
2186 		dev_dbg(&vsi->back->pdev->dev,
2187 			"Reset in progress. LAN Tx queues already disabled\n");
2188 	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
2189 		dev_dbg(&vsi->back->pdev->dev,
2190 			"LAN Tx queues do not exist, nothing to disable\n");
2191 	} else if (status) {
2192 		dev_err(&vsi->back->pdev->dev,
2193 			"Failed to disable LAN Tx queues, error: %d\n", status);
2194 		return -ENODEV;
2195 	}
2196 
2197 	return 0;
2198 }
2199 
2200 /**
2201  * ice_fill_txq_meta - Prepare the Tx queue's meta data
2202  * @vsi: VSI that ring belongs to
2203  * @ring: ring that txq_meta will be based on
2204  * @txq_meta: a helper struct that wraps Tx queue's information
2205  *
2206  * Set up a helper struct that will contain all the necessary fields that
2207  * are needed for stopping Tx queue
2208  */
2209 #ifndef CONFIG_PCI_IOV
2210 static
2211 #endif /* !CONFIG_PCI_IOV */
2212 void
2213 ice_fill_txq_meta(struct ice_vsi *vsi, struct ice_ring *ring,
2214 		  struct ice_txq_meta *txq_meta)
2215 {
2216 	u8 tc = 0;
2217 
2218 #ifdef CONFIG_DCB
2219 	tc = ring->dcb_tc;
2220 #endif /* CONFIG_DCB */
2221 	txq_meta->q_id = ring->reg_idx;
2222 	txq_meta->q_teid = ring->txq_teid;
2223 	txq_meta->q_handle = ring->q_handle;
2224 	txq_meta->vsi_idx = vsi->idx;
2225 	txq_meta->tc = tc;
2226 }
2227 
2228 /**
2229  * ice_vsi_stop_tx_rings - Disable Tx rings
2230  * @vsi: the VSI being configured
2231  * @rst_src: reset source
2232  * @rel_vmvf_num: Relative ID of VF/VM
2233  * @rings: Tx ring array to be stopped
2234  */
2235 static int
2236 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2237 		      u16 rel_vmvf_num, struct ice_ring **rings)
2238 {
2239 	u16 i, q_idx = 0;
2240 	int status;
2241 	u8 tc;
2242 
2243 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2244 		return -EINVAL;
2245 
2246 	/* set up the Tx queue list to be disabled for each enabled TC */
2247 	ice_for_each_traffic_class(tc) {
2248 		if (!(vsi->tc_cfg.ena_tc & BIT(tc)))
2249 			break;
2250 
2251 		for (i = 0; i < vsi->tc_cfg.tc_info[tc].qcount_tx; i++) {
2252 			struct ice_txq_meta txq_meta = { };
2253 
2254 			if (!rings || !rings[q_idx])
2255 				return -EINVAL;
2256 
2257 			ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2258 			status = ice_vsi_stop_tx_ring(vsi, rst_src,
2259 						      rel_vmvf_num,
2260 						      rings[q_idx], &txq_meta);
2261 
2262 			if (status)
2263 				return status;
2264 
2265 			q_idx++;
2266 		}
2267 	}
2268 
2269 	return 0;
2270 }
2271 
2272 /**
2273  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2274  * @vsi: the VSI being configured
2275  * @rst_src: reset source
2276  * @rel_vmvf_num: Relative ID of VF/VM
2277  */
2278 int
2279 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2280 			  u16 rel_vmvf_num)
2281 {
2282 	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
2283 }
2284 
2285 /**
2286  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2287  * @vsi: VSI to enable or disable VLAN pruning on
2288  * @ena: set to true to enable VLAN pruning and false to disable it
2289  * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2290  *
2291  * returns 0 if VSI is updated, negative otherwise
2292  */
2293 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2294 {
2295 	struct ice_vsi_ctx *ctxt;
2296 	struct device *dev;
2297 	struct ice_pf *pf;
2298 	int status;
2299 
2300 	if (!vsi)
2301 		return -EINVAL;
2302 
2303 	pf = vsi->back;
2304 	dev = &pf->pdev->dev;
2305 	ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
2306 	if (!ctxt)
2307 		return -ENOMEM;
2308 
2309 	ctxt->info = vsi->info;
2310 
2311 	if (ena) {
2312 		ctxt->info.sec_flags |=
2313 			ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2314 			ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
2315 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2316 	} else {
2317 		ctxt->info.sec_flags &=
2318 			~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2319 			  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2320 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2321 	}
2322 
2323 	if (!vlan_promisc)
2324 		ctxt->info.valid_sections =
2325 			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
2326 				    ICE_AQ_VSI_PROP_SW_VALID);
2327 
2328 	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2329 	if (status) {
2330 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
2331 			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
2332 			   pf->hw.adminq.sq_last_status);
2333 		goto err_out;
2334 	}
2335 
2336 	vsi->info.sec_flags = ctxt->info.sec_flags;
2337 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2338 
2339 	devm_kfree(dev, ctxt);
2340 	return 0;
2341 
2342 err_out:
2343 	devm_kfree(dev, ctxt);
2344 	return -EIO;
2345 }
2346 
2347 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2348 {
2349 	struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
2350 
2351 	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2352 	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2353 }
2354 
2355 /**
2356  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2357  * @vsi: VSI to set the q_vectors register index on
2358  */
2359 static int
2360 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2361 {
2362 	u16 i;
2363 
2364 	if (!vsi || !vsi->q_vectors)
2365 		return -EINVAL;
2366 
2367 	ice_for_each_q_vector(vsi, i) {
2368 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2369 
2370 		if (!q_vector) {
2371 			dev_err(&vsi->back->pdev->dev,
2372 				"Failed to set reg_idx on q_vector %d VSI %d\n",
2373 				i, vsi->vsi_num);
2374 			goto clear_reg_idx;
2375 		}
2376 
2377 		if (vsi->type == ICE_VSI_VF) {
2378 			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2379 
2380 			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2381 		} else {
2382 			q_vector->reg_idx =
2383 				q_vector->v_idx + vsi->base_vector;
2384 		}
2385 	}
2386 
2387 	return 0;
2388 
2389 clear_reg_idx:
2390 	ice_for_each_q_vector(vsi, i) {
2391 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2392 
2393 		if (q_vector)
2394 			q_vector->reg_idx = 0;
2395 	}
2396 
2397 	return -EINVAL;
2398 }
2399 
2400 /**
2401  * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
2402  * @vsi: the VSI being configured
2403  * @add_rule: boolean value to add or remove ethertype filter rule
2404  */
2405 static void
2406 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
2407 {
2408 	struct ice_fltr_list_entry *list;
2409 	struct ice_pf *pf = vsi->back;
2410 	LIST_HEAD(tmp_add_list);
2411 	enum ice_status status;
2412 
2413 	list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
2414 	if (!list)
2415 		return;
2416 
2417 	list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
2418 	list->fltr_info.fltr_act = ICE_DROP_PACKET;
2419 	list->fltr_info.flag = ICE_FLTR_TX;
2420 	list->fltr_info.src_id = ICE_SRC_ID_VSI;
2421 	list->fltr_info.vsi_handle = vsi->idx;
2422 	list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
2423 
2424 	INIT_LIST_HEAD(&list->list_entry);
2425 	list_add(&list->list_entry, &tmp_add_list);
2426 
2427 	if (add_rule)
2428 		status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
2429 	else
2430 		status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
2431 
2432 	if (status)
2433 		dev_err(&pf->pdev->dev,
2434 			"Failure Adding or Removing Ethertype on VSI %i error: %d\n",
2435 			vsi->vsi_num, status);
2436 
2437 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2438 }
2439 
2440 /**
2441  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2442  * @vsi: the VSI being configured
2443  * @tx: bool to determine Tx or Rx rule
2444  * @create: bool to determine create or remove Rule
2445  */
2446 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2447 {
2448 	struct ice_fltr_list_entry *list;
2449 	struct ice_pf *pf = vsi->back;
2450 	LIST_HEAD(tmp_add_list);
2451 	enum ice_status status;
2452 
2453 	list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
2454 	if (!list)
2455 		return;
2456 
2457 	list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
2458 	list->fltr_info.vsi_handle = vsi->idx;
2459 	list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
2460 
2461 	if (tx) {
2462 		list->fltr_info.fltr_act = ICE_DROP_PACKET;
2463 		list->fltr_info.flag = ICE_FLTR_TX;
2464 		list->fltr_info.src_id = ICE_SRC_ID_VSI;
2465 	} else {
2466 		list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2467 		list->fltr_info.flag = ICE_FLTR_RX;
2468 		list->fltr_info.src_id = ICE_SRC_ID_LPORT;
2469 	}
2470 
2471 	INIT_LIST_HEAD(&list->list_entry);
2472 	list_add(&list->list_entry, &tmp_add_list);
2473 
2474 	if (create)
2475 		status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
2476 	else
2477 		status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
2478 
2479 	if (status)
2480 		dev_err(&pf->pdev->dev,
2481 			"Fail %s %s LLDP rule on VSI %i error: %d\n",
2482 			create ? "adding" : "removing", tx ? "TX" : "RX",
2483 			vsi->vsi_num, status);
2484 
2485 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2486 }
2487 
2488 /**
2489  * ice_vsi_setup - Set up a VSI by a given type
2490  * @pf: board private structure
2491  * @pi: pointer to the port_info instance
2492  * @type: VSI type
2493  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2494  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2495  *         fill-in ICE_INVAL_VFID as input.
2496  *
2497  * This allocates the sw VSI structure and its queue resources.
2498  *
2499  * Returns pointer to the successfully allocated and configured VSI sw struct on
2500  * success, NULL on failure.
2501  */
2502 struct ice_vsi *
2503 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2504 	      enum ice_vsi_type type, u16 vf_id)
2505 {
2506 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2507 	struct device *dev = &pf->pdev->dev;
2508 	enum ice_status status;
2509 	struct ice_vsi *vsi;
2510 	int ret, i;
2511 
2512 	if (type == ICE_VSI_VF)
2513 		vsi = ice_vsi_alloc(pf, type, vf_id);
2514 	else
2515 		vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
2516 
2517 	if (!vsi) {
2518 		dev_err(dev, "could not allocate VSI\n");
2519 		return NULL;
2520 	}
2521 
2522 	vsi->port_info = pi;
2523 	vsi->vsw = pf->first_sw;
2524 	if (vsi->type == ICE_VSI_PF)
2525 		vsi->ethtype = ETH_P_PAUSE;
2526 
2527 	if (vsi->type == ICE_VSI_VF)
2528 		vsi->vf_id = vf_id;
2529 
2530 	if (ice_vsi_get_qs(vsi)) {
2531 		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2532 			vsi->idx);
2533 		goto unroll_get_qs;
2534 	}
2535 
2536 	/* set RSS capabilities */
2537 	ice_vsi_set_rss_params(vsi);
2538 
2539 	/* set TC configuration */
2540 	ice_vsi_set_tc_cfg(vsi);
2541 
2542 	/* create the VSI */
2543 	ret = ice_vsi_init(vsi);
2544 	if (ret)
2545 		goto unroll_get_qs;
2546 
2547 	switch (vsi->type) {
2548 	case ICE_VSI_PF:
2549 		ret = ice_vsi_alloc_q_vectors(vsi);
2550 		if (ret)
2551 			goto unroll_vsi_init;
2552 
2553 		ret = ice_vsi_setup_vector_base(vsi);
2554 		if (ret)
2555 			goto unroll_alloc_q_vector;
2556 
2557 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2558 		if (ret)
2559 			goto unroll_vector_base;
2560 
2561 		ret = ice_vsi_alloc_rings(vsi);
2562 		if (ret)
2563 			goto unroll_vector_base;
2564 
2565 		ice_vsi_map_rings_to_vectors(vsi);
2566 
2567 		/* Do not exit if configuring RSS had an issue, at least
2568 		 * receive traffic on first queue. Hence no need to capture
2569 		 * return value
2570 		 */
2571 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2572 			ice_vsi_cfg_rss_lut_key(vsi);
2573 		break;
2574 	case ICE_VSI_VF:
2575 		/* VF driver will take care of creating netdev for this type and
2576 		 * map queues to vectors through Virtchnl, PF driver only
2577 		 * creates a VSI and corresponding structures for bookkeeping
2578 		 * purpose
2579 		 */
2580 		ret = ice_vsi_alloc_q_vectors(vsi);
2581 		if (ret)
2582 			goto unroll_vsi_init;
2583 
2584 		ret = ice_vsi_alloc_rings(vsi);
2585 		if (ret)
2586 			goto unroll_alloc_q_vector;
2587 
2588 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2589 		if (ret)
2590 			goto unroll_vector_base;
2591 
2592 		/* Do not exit if configuring RSS had an issue, at least
2593 		 * receive traffic on first queue. Hence no need to capture
2594 		 * return value
2595 		 */
2596 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2597 			ice_vsi_cfg_rss_lut_key(vsi);
2598 		break;
2599 	case ICE_VSI_LB:
2600 		ret = ice_vsi_alloc_rings(vsi);
2601 		if (ret)
2602 			goto unroll_vsi_init;
2603 		break;
2604 	default:
2605 		/* clean up the resources and exit */
2606 		goto unroll_vsi_init;
2607 	}
2608 
2609 	/* configure VSI nodes based on number of queues and TC's */
2610 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2611 		max_txqs[i] = vsi->alloc_txq;
2612 
2613 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2614 				 max_txqs);
2615 	if (status) {
2616 		dev_err(&pf->pdev->dev,
2617 			"VSI %d failed lan queue config, error %d\n",
2618 			vsi->vsi_num, status);
2619 		goto unroll_vector_base;
2620 	}
2621 
2622 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2623 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2624 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2625 	 * The rule is added once for PF VSI in order to create appropriate
2626 	 * recipe, since VSI/VSI list is ignored with drop action...
2627 	 * Also add rules to handle LLDP Tx and Rx packets.  Tx LLDP packets
2628 	 * need to be dropped so that VFs cannot send LLDP packets to reconfig
2629 	 * DCB settings in the HW.  Also, if the FW DCBX engine is not running
2630 	 * then Rx LLDP packets need to be redirected up the stack.
2631 	 */
2632 	if (vsi->type == ICE_VSI_PF) {
2633 		ice_vsi_add_rem_eth_mac(vsi, true);
2634 
2635 		/* Tx LLDP packets */
2636 		ice_cfg_sw_lldp(vsi, true, true);
2637 
2638 		/* Rx LLDP packets */
2639 		if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2640 			ice_cfg_sw_lldp(vsi, false, true);
2641 	}
2642 
2643 	return vsi;
2644 
2645 unroll_vector_base:
2646 	/* reclaim SW interrupts back to the common pool */
2647 	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2648 	pf->num_avail_sw_msix += vsi->num_q_vectors;
2649 unroll_alloc_q_vector:
2650 	ice_vsi_free_q_vectors(vsi);
2651 unroll_vsi_init:
2652 	ice_vsi_delete(vsi);
2653 unroll_get_qs:
2654 	ice_vsi_put_qs(vsi);
2655 	ice_vsi_clear(vsi);
2656 
2657 	return NULL;
2658 }
2659 
2660 /**
2661  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2662  * @vsi: the VSI being cleaned up
2663  */
2664 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2665 {
2666 	struct ice_pf *pf = vsi->back;
2667 	struct ice_hw *hw = &pf->hw;
2668 	u32 txq = 0;
2669 	u32 rxq = 0;
2670 	int i, q;
2671 
2672 	for (i = 0; i < vsi->num_q_vectors; i++) {
2673 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2674 		u16 reg_idx = q_vector->reg_idx;
2675 
2676 		wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2677 		wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2678 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2679 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2680 			txq++;
2681 		}
2682 
2683 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2684 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2685 			rxq++;
2686 		}
2687 	}
2688 
2689 	ice_flush(hw);
2690 }
2691 
2692 /**
2693  * ice_vsi_free_irq - Free the IRQ association with the OS
2694  * @vsi: the VSI being configured
2695  */
2696 void ice_vsi_free_irq(struct ice_vsi *vsi)
2697 {
2698 	struct ice_pf *pf = vsi->back;
2699 	int base = vsi->base_vector;
2700 	int i;
2701 
2702 	if (!vsi->q_vectors || !vsi->irqs_ready)
2703 		return;
2704 
2705 	ice_vsi_release_msix(vsi);
2706 	if (vsi->type == ICE_VSI_VF)
2707 		return;
2708 
2709 	vsi->irqs_ready = false;
2710 	ice_for_each_q_vector(vsi, i) {
2711 		u16 vector = i + base;
2712 		int irq_num;
2713 
2714 		irq_num = pf->msix_entries[vector].vector;
2715 
2716 		/* free only the irqs that were actually requested */
2717 		if (!vsi->q_vectors[i] ||
2718 		    !(vsi->q_vectors[i]->num_ring_tx ||
2719 		      vsi->q_vectors[i]->num_ring_rx))
2720 			continue;
2721 
2722 		/* clear the affinity notifier in the IRQ descriptor */
2723 		irq_set_affinity_notifier(irq_num, NULL);
2724 
2725 		/* clear the affinity_mask in the IRQ descriptor */
2726 		irq_set_affinity_hint(irq_num, NULL);
2727 		synchronize_irq(irq_num);
2728 		devm_free_irq(&pf->pdev->dev, irq_num,
2729 			      vsi->q_vectors[i]);
2730 	}
2731 }
2732 
2733 /**
2734  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2735  * @vsi: the VSI having resources freed
2736  */
2737 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2738 {
2739 	int i;
2740 
2741 	if (!vsi->tx_rings)
2742 		return;
2743 
2744 	ice_for_each_txq(vsi, i)
2745 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2746 			ice_free_tx_ring(vsi->tx_rings[i]);
2747 }
2748 
2749 /**
2750  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2751  * @vsi: the VSI having resources freed
2752  */
2753 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2754 {
2755 	int i;
2756 
2757 	if (!vsi->rx_rings)
2758 		return;
2759 
2760 	ice_for_each_rxq(vsi, i)
2761 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2762 			ice_free_rx_ring(vsi->rx_rings[i]);
2763 }
2764 
2765 /**
2766  * ice_vsi_close - Shut down a VSI
2767  * @vsi: the VSI being shut down
2768  */
2769 void ice_vsi_close(struct ice_vsi *vsi)
2770 {
2771 	if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2772 		ice_down(vsi);
2773 
2774 	ice_vsi_free_irq(vsi);
2775 	ice_vsi_free_tx_rings(vsi);
2776 	ice_vsi_free_rx_rings(vsi);
2777 }
2778 
2779 /**
2780  * ice_free_res - free a block of resources
2781  * @res: pointer to the resource
2782  * @index: starting index previously returned by ice_get_res
2783  * @id: identifier to track owner
2784  *
2785  * Returns number of resources freed
2786  */
2787 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2788 {
2789 	int count = 0;
2790 	int i;
2791 
2792 	if (!res || index >= res->end)
2793 		return -EINVAL;
2794 
2795 	id |= ICE_RES_VALID_BIT;
2796 	for (i = index; i < res->end && res->list[i] == id; i++) {
2797 		res->list[i] = 0;
2798 		count++;
2799 	}
2800 
2801 	return count;
2802 }
2803 
2804 /**
2805  * ice_search_res - Search the tracker for a block of resources
2806  * @res: pointer to the resource
2807  * @needed: size of the block needed
2808  * @id: identifier to track owner
2809  *
2810  * Returns the base item index of the block, or -ENOMEM for error
2811  */
2812 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2813 {
2814 	int start = 0, end = 0;
2815 
2816 	if (needed > res->end)
2817 		return -ENOMEM;
2818 
2819 	id |= ICE_RES_VALID_BIT;
2820 
2821 	do {
2822 		/* skip already allocated entries */
2823 		if (res->list[end++] & ICE_RES_VALID_BIT) {
2824 			start = end;
2825 			if ((start + needed) > res->end)
2826 				break;
2827 		}
2828 
2829 		if (end == (start + needed)) {
2830 			int i = start;
2831 
2832 			/* there was enough, so assign it to the requestor */
2833 			while (i != end)
2834 				res->list[i++] = id;
2835 
2836 			return start;
2837 		}
2838 	} while (end < res->end);
2839 
2840 	return -ENOMEM;
2841 }
2842 
2843 /**
2844  * ice_get_res - get a block of resources
2845  * @pf: board private structure
2846  * @res: pointer to the resource
2847  * @needed: size of the block needed
2848  * @id: identifier to track owner
2849  *
2850  * Returns the base item index of the block, or negative for error
2851  */
2852 int
2853 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2854 {
2855 	if (!res || !pf)
2856 		return -EINVAL;
2857 
2858 	if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2859 		dev_err(&pf->pdev->dev,
2860 			"param err: needed=%d, num_entries = %d id=0x%04x\n",
2861 			needed, res->num_entries, id);
2862 		return -EINVAL;
2863 	}
2864 
2865 	return ice_search_res(res, needed, id);
2866 }
2867 
2868 /**
2869  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2870  * @vsi: the VSI being un-configured
2871  */
2872 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2873 {
2874 	int base = vsi->base_vector;
2875 	struct ice_pf *pf = vsi->back;
2876 	struct ice_hw *hw = &pf->hw;
2877 	u32 val;
2878 	int i;
2879 
2880 	/* disable interrupt causation from each queue */
2881 	if (vsi->tx_rings) {
2882 		ice_for_each_txq(vsi, i) {
2883 			if (vsi->tx_rings[i]) {
2884 				u16 reg;
2885 
2886 				reg = vsi->tx_rings[i]->reg_idx;
2887 				val = rd32(hw, QINT_TQCTL(reg));
2888 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2889 				wr32(hw, QINT_TQCTL(reg), val);
2890 			}
2891 		}
2892 	}
2893 
2894 	if (vsi->rx_rings) {
2895 		ice_for_each_rxq(vsi, i) {
2896 			if (vsi->rx_rings[i]) {
2897 				u16 reg;
2898 
2899 				reg = vsi->rx_rings[i]->reg_idx;
2900 				val = rd32(hw, QINT_RQCTL(reg));
2901 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2902 				wr32(hw, QINT_RQCTL(reg), val);
2903 			}
2904 		}
2905 	}
2906 
2907 	/* disable each interrupt */
2908 	ice_for_each_q_vector(vsi, i)
2909 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2910 
2911 	ice_flush(hw);
2912 
2913 	/* don't call synchronize_irq() for VF's from the host */
2914 	if (vsi->type == ICE_VSI_VF)
2915 		return;
2916 
2917 	ice_for_each_q_vector(vsi, i)
2918 		synchronize_irq(pf->msix_entries[i + base].vector);
2919 }
2920 
2921 /**
2922  * ice_napi_del - Remove NAPI handler for the VSI
2923  * @vsi: VSI for which NAPI handler is to be removed
2924  */
2925 void ice_napi_del(struct ice_vsi *vsi)
2926 {
2927 	int v_idx;
2928 
2929 	if (!vsi->netdev)
2930 		return;
2931 
2932 	ice_for_each_q_vector(vsi, v_idx)
2933 		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2934 }
2935 
2936 /**
2937  * ice_vsi_release - Delete a VSI and free its resources
2938  * @vsi: the VSI being removed
2939  *
2940  * Returns 0 on success or < 0 on error
2941  */
2942 int ice_vsi_release(struct ice_vsi *vsi)
2943 {
2944 	struct ice_pf *pf;
2945 
2946 	if (!vsi->back)
2947 		return -ENODEV;
2948 	pf = vsi->back;
2949 
2950 	/* do not unregister while driver is in the reset recovery pending
2951 	 * state. Since reset/rebuild happens through PF service task workqueue,
2952 	 * it's not a good idea to unregister netdev that is associated to the
2953 	 * PF that is running the work queue items currently. This is done to
2954 	 * avoid check_flush_dependency() warning on this wq
2955 	 */
2956 	if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2957 		unregister_netdev(vsi->netdev);
2958 
2959 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2960 		ice_rss_clean(vsi);
2961 
2962 	/* Disable VSI and free resources */
2963 	if (vsi->type != ICE_VSI_LB)
2964 		ice_vsi_dis_irq(vsi);
2965 	ice_vsi_close(vsi);
2966 
2967 	/* SR-IOV determines needed MSIX resources all at once instead of per
2968 	 * VSI since when VFs are spawned we know how many VFs there are and how
2969 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2970 	 * cleared in the same manner.
2971 	 */
2972 	if (vsi->type != ICE_VSI_VF) {
2973 		/* reclaim SW interrupts back to the common pool */
2974 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2975 		pf->num_avail_sw_msix += vsi->num_q_vectors;
2976 	}
2977 
2978 	if (vsi->type == ICE_VSI_PF) {
2979 		ice_vsi_add_rem_eth_mac(vsi, false);
2980 		ice_cfg_sw_lldp(vsi, true, false);
2981 		/* The Rx rule will only exist to remove if the LLDP FW
2982 		 * engine is currently stopped
2983 		 */
2984 		if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2985 			ice_cfg_sw_lldp(vsi, false, false);
2986 	}
2987 
2988 	ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2989 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2990 	ice_vsi_delete(vsi);
2991 	ice_vsi_free_q_vectors(vsi);
2992 
2993 	/* make sure unregister_netdev() was called by checking __ICE_DOWN */
2994 	if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2995 		free_netdev(vsi->netdev);
2996 		vsi->netdev = NULL;
2997 	}
2998 
2999 	ice_vsi_clear_rings(vsi);
3000 
3001 	ice_vsi_put_qs(vsi);
3002 
3003 	/* retain SW VSI data structure since it is needed to unregister and
3004 	 * free VSI netdev when PF is not in reset recovery pending state,\
3005 	 * for ex: during rmmod.
3006 	 */
3007 	if (!ice_is_reset_in_progress(pf->state))
3008 		ice_vsi_clear(vsi);
3009 
3010 	return 0;
3011 }
3012 
3013 /**
3014  * ice_vsi_rebuild - Rebuild VSI after reset
3015  * @vsi: VSI to be rebuild
3016  *
3017  * Returns 0 on success and negative value on failure
3018  */
3019 int ice_vsi_rebuild(struct ice_vsi *vsi)
3020 {
3021 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3022 	struct ice_vf *vf = NULL;
3023 	enum ice_status status;
3024 	struct ice_pf *pf;
3025 	int ret, i;
3026 
3027 	if (!vsi)
3028 		return -EINVAL;
3029 
3030 	pf = vsi->back;
3031 	if (vsi->type == ICE_VSI_VF)
3032 		vf = &pf->vf[vsi->vf_id];
3033 
3034 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3035 	ice_vsi_free_q_vectors(vsi);
3036 
3037 	/* SR-IOV determines needed MSIX resources all at once instead of per
3038 	 * VSI since when VFs are spawned we know how many VFs there are and how
3039 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
3040 	 * cleared in the same manner.
3041 	 */
3042 	if (vsi->type != ICE_VSI_VF) {
3043 		/* reclaim SW interrupts back to the common pool */
3044 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3045 		pf->num_avail_sw_msix += vsi->num_q_vectors;
3046 		vsi->base_vector = 0;
3047 	}
3048 
3049 	ice_vsi_put_qs(vsi);
3050 	ice_vsi_clear_rings(vsi);
3051 	ice_vsi_free_arrays(vsi);
3052 	ice_dev_onetime_setup(&pf->hw);
3053 	if (vsi->type == ICE_VSI_VF)
3054 		ice_vsi_set_num_qs(vsi, vf->vf_id);
3055 	else
3056 		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3057 
3058 	ret = ice_vsi_alloc_arrays(vsi);
3059 	if (ret < 0)
3060 		goto err_vsi;
3061 
3062 	ice_vsi_get_qs(vsi);
3063 	ice_vsi_set_tc_cfg(vsi);
3064 
3065 	/* Initialize VSI struct elements and create VSI in FW */
3066 	ret = ice_vsi_init(vsi);
3067 	if (ret < 0)
3068 		goto err_vsi;
3069 
3070 
3071 	switch (vsi->type) {
3072 	case ICE_VSI_PF:
3073 		ret = ice_vsi_alloc_q_vectors(vsi);
3074 		if (ret)
3075 			goto err_rings;
3076 
3077 		ret = ice_vsi_setup_vector_base(vsi);
3078 		if (ret)
3079 			goto err_vectors;
3080 
3081 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3082 		if (ret)
3083 			goto err_vectors;
3084 
3085 		ret = ice_vsi_alloc_rings(vsi);
3086 		if (ret)
3087 			goto err_vectors;
3088 
3089 		ice_vsi_map_rings_to_vectors(vsi);
3090 		/* Do not exit if configuring RSS had an issue, at least
3091 		 * receive traffic on first queue. Hence no need to capture
3092 		 * return value
3093 		 */
3094 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3095 			ice_vsi_cfg_rss_lut_key(vsi);
3096 		break;
3097 	case ICE_VSI_VF:
3098 		ret = ice_vsi_alloc_q_vectors(vsi);
3099 		if (ret)
3100 			goto err_rings;
3101 
3102 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3103 		if (ret)
3104 			goto err_vectors;
3105 
3106 		ret = ice_vsi_alloc_rings(vsi);
3107 		if (ret)
3108 			goto err_vectors;
3109 
3110 		break;
3111 	default:
3112 		break;
3113 	}
3114 
3115 	/* configure VSI nodes based on number of queues and TC's */
3116 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
3117 		max_txqs[i] = vsi->alloc_txq;
3118 
3119 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3120 				 max_txqs);
3121 	if (status) {
3122 		dev_err(&pf->pdev->dev,
3123 			"VSI %d failed lan queue config, error %d\n",
3124 			vsi->vsi_num, status);
3125 		goto err_vectors;
3126 	}
3127 	return 0;
3128 
3129 err_vectors:
3130 	ice_vsi_free_q_vectors(vsi);
3131 err_rings:
3132 	if (vsi->netdev) {
3133 		vsi->current_netdev_flags = 0;
3134 		unregister_netdev(vsi->netdev);
3135 		free_netdev(vsi->netdev);
3136 		vsi->netdev = NULL;
3137 	}
3138 err_vsi:
3139 	ice_vsi_clear(vsi);
3140 	set_bit(__ICE_RESET_FAILED, pf->state);
3141 	return ret;
3142 }
3143 
3144 /**
3145  * ice_is_reset_in_progress - check for a reset in progress
3146  * @state: PF state field
3147  */
3148 bool ice_is_reset_in_progress(unsigned long *state)
3149 {
3150 	return test_bit(__ICE_RESET_OICR_RECV, state) ||
3151 	       test_bit(__ICE_PFR_REQ, state) ||
3152 	       test_bit(__ICE_CORER_REQ, state) ||
3153 	       test_bit(__ICE_GLOBR_REQ, state);
3154 }
3155 
3156 #ifdef CONFIG_DCB
3157 /**
3158  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3159  * @vsi: VSI being configured
3160  * @ctx: the context buffer returned from AQ VSI update command
3161  */
3162 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3163 {
3164 	vsi->info.mapping_flags = ctx->info.mapping_flags;
3165 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3166 	       sizeof(vsi->info.q_mapping));
3167 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3168 	       sizeof(vsi->info.tc_mapping));
3169 }
3170 
3171 /**
3172  * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3173  * @vsi: the VSI being configured
3174  * @ena_tc: TC map to be enabled
3175  */
3176 static void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3177 {
3178 	struct net_device *netdev = vsi->netdev;
3179 	struct ice_pf *pf = vsi->back;
3180 	struct ice_dcbx_cfg *dcbcfg;
3181 	u8 netdev_tc;
3182 	int i;
3183 
3184 	if (!netdev)
3185 		return;
3186 
3187 	if (!ena_tc) {
3188 		netdev_reset_tc(netdev);
3189 		return;
3190 	}
3191 
3192 	if (netdev_set_num_tc(netdev, vsi->tc_cfg.numtc))
3193 		return;
3194 
3195 	dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
3196 
3197 	ice_for_each_traffic_class(i)
3198 		if (vsi->tc_cfg.ena_tc & BIT(i))
3199 			netdev_set_tc_queue(netdev,
3200 					    vsi->tc_cfg.tc_info[i].netdev_tc,
3201 					    vsi->tc_cfg.tc_info[i].qcount_tx,
3202 					    vsi->tc_cfg.tc_info[i].qoffset);
3203 
3204 	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3205 		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3206 
3207 		/* Get the mapped netdev TC# for the UP */
3208 		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3209 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3210 	}
3211 }
3212 
3213 /**
3214  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3215  * @vsi: VSI to be configured
3216  * @ena_tc: TC bitmap
3217  *
3218  * VSI queues expected to be quiesced before calling this function
3219  */
3220 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3221 {
3222 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3223 	struct ice_vsi_ctx *ctx;
3224 	struct ice_pf *pf = vsi->back;
3225 	enum ice_status status;
3226 	int i, ret = 0;
3227 	u8 num_tc = 0;
3228 
3229 	ice_for_each_traffic_class(i) {
3230 		/* build bitmap of enabled TCs */
3231 		if (ena_tc & BIT(i))
3232 			num_tc++;
3233 		/* populate max_txqs per TC */
3234 		max_txqs[i] = vsi->alloc_txq;
3235 	}
3236 
3237 	vsi->tc_cfg.ena_tc = ena_tc;
3238 	vsi->tc_cfg.numtc = num_tc;
3239 
3240 	ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
3241 	if (!ctx)
3242 		return -ENOMEM;
3243 
3244 	ctx->vf_num = 0;
3245 	ctx->info = vsi->info;
3246 
3247 	ice_vsi_setup_q_map(vsi, ctx);
3248 
3249 	/* must to indicate which section of VSI context are being modified */
3250 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3251 	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3252 	if (status) {
3253 		dev_info(&pf->pdev->dev, "Failed VSI Update\n");
3254 		ret = -EIO;
3255 		goto out;
3256 	}
3257 
3258 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3259 				 max_txqs);
3260 
3261 	if (status) {
3262 		dev_err(&pf->pdev->dev,
3263 			"VSI %d failed TC config, error %d\n",
3264 			vsi->vsi_num, status);
3265 		ret = -EIO;
3266 		goto out;
3267 	}
3268 	ice_vsi_update_q_map(vsi, ctx);
3269 	vsi->info.valid_sections = 0;
3270 
3271 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3272 out:
3273 	devm_kfree(&pf->pdev->dev, ctx);
3274 	return ret;
3275 }
3276 #endif /* CONFIG_DCB */
3277 
3278 /**
3279  * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
3280  * @vsi: the VSI being configured MAC filter
3281  * @macaddr: the MAC address to be added.
3282  * @set: Add or delete a MAC filter
3283  *
3284  * Adds or removes MAC address filter entry for VF VSI
3285  */
3286 enum ice_status
3287 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
3288 {
3289 	LIST_HEAD(tmp_add_list);
3290 	enum ice_status status;
3291 
3292 	 /* Update MAC filter list to be added or removed for a VSI */
3293 	if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
3294 		status = ICE_ERR_NO_MEMORY;
3295 		goto cfg_mac_fltr_exit;
3296 	}
3297 
3298 	if (set)
3299 		status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
3300 	else
3301 		status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
3302 
3303 cfg_mac_fltr_exit:
3304 	ice_free_fltr_list(&vsi->back->pdev->dev, &tmp_add_list);
3305 	return status;
3306 }
3307