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