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