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