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