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