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