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