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