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