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