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