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_update_eth_stats - Update VSI-specific ethernet statistics counters
1472  * @vsi: the VSI to be updated
1473  */
1474 void ice_update_eth_stats(struct ice_vsi *vsi)
1475 {
1476 	struct ice_eth_stats *prev_es, *cur_es;
1477 	struct ice_hw *hw = &vsi->back->hw;
1478 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1479 
1480 	prev_es = &vsi->eth_stats_prev;
1481 	cur_es = &vsi->eth_stats;
1482 
1483 	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1484 			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1485 
1486 	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1487 			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1488 
1489 	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1490 			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1491 
1492 	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1493 			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1494 
1495 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1496 			  &prev_es->rx_discards, &cur_es->rx_discards);
1497 
1498 	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1499 			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1500 
1501 	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1502 			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1503 
1504 	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1505 			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1506 
1507 	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1508 			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1509 
1510 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1511 			  &prev_es->tx_errors, &cur_es->tx_errors);
1512 
1513 	vsi->stat_offsets_loaded = true;
1514 }
1515 
1516 /**
1517  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1518  * @vsi: the VSI being configured
1519  * @vid: VLAN ID to be added
1520  * @action: filter action to be performed on match
1521  */
1522 int
1523 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1524 {
1525 	struct ice_pf *pf = vsi->back;
1526 	struct device *dev;
1527 	int err = 0;
1528 
1529 	dev = ice_pf_to_dev(pf);
1530 
1531 	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1532 		vsi->num_vlan++;
1533 	} else {
1534 		err = -ENODEV;
1535 		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1536 			vsi->vsi_num);
1537 	}
1538 
1539 	return err;
1540 }
1541 
1542 /**
1543  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1544  * @vsi: the VSI being configured
1545  * @vid: VLAN ID to be removed
1546  *
1547  * Returns 0 on success and negative on failure
1548  */
1549 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1550 {
1551 	struct ice_pf *pf = vsi->back;
1552 	enum ice_status status;
1553 	struct device *dev;
1554 	int err = 0;
1555 
1556 	dev = ice_pf_to_dev(pf);
1557 
1558 	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1559 	if (!status) {
1560 		vsi->num_vlan--;
1561 	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1562 		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1563 			vid, vsi->vsi_num, ice_stat_str(status));
1564 	} else {
1565 		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1566 			vid, vsi->vsi_num, ice_stat_str(status));
1567 		err = -EIO;
1568 	}
1569 
1570 	return err;
1571 }
1572 
1573 /**
1574  * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1575  * @vsi: VSI
1576  */
1577 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1578 {
1579 	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1580 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1581 		vsi->rx_buf_len = ICE_RXBUF_2048;
1582 #if (PAGE_SIZE < 8192)
1583 	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1584 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1585 		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1586 		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1587 #endif
1588 	} else {
1589 		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1590 #if (PAGE_SIZE < 8192)
1591 		vsi->rx_buf_len = ICE_RXBUF_3072;
1592 #else
1593 		vsi->rx_buf_len = ICE_RXBUF_2048;
1594 #endif
1595 	}
1596 }
1597 
1598 /**
1599  * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1600  * @hw: HW pointer
1601  * @pf_q: index of the Rx queue in the PF's queue space
1602  * @rxdid: flexible descriptor RXDID
1603  * @prio: priority for the RXDID for this queue
1604  */
1605 void
1606 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio)
1607 {
1608 	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1609 
1610 	/* clear any previous values */
1611 	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1612 		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1613 		    QRXFLXP_CNTXT_TS_M);
1614 
1615 	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1616 		QRXFLXP_CNTXT_RXDID_IDX_M;
1617 
1618 	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1619 		QRXFLXP_CNTXT_RXDID_PRIO_M;
1620 
1621 	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1622 }
1623 
1624 /**
1625  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1626  * @vsi: the VSI being configured
1627  *
1628  * Return 0 on success and a negative value on error
1629  * Configure the Rx VSI for operation.
1630  */
1631 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1632 {
1633 	u16 i;
1634 
1635 	if (vsi->type == ICE_VSI_VF)
1636 		goto setup_rings;
1637 
1638 	ice_vsi_cfg_frame_size(vsi);
1639 setup_rings:
1640 	/* set up individual rings */
1641 	for (i = 0; i < vsi->num_rxq; i++) {
1642 		int err;
1643 
1644 		err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1645 		if (err) {
1646 			dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1647 				i, err);
1648 			return err;
1649 		}
1650 	}
1651 
1652 	return 0;
1653 }
1654 
1655 /**
1656  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1657  * @vsi: the VSI being configured
1658  * @rings: Tx ring array to be configured
1659  *
1660  * Return 0 on success and a negative value on error
1661  * Configure the Tx VSI for operation.
1662  */
1663 static int
1664 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1665 {
1666 	struct ice_aqc_add_tx_qgrp *qg_buf;
1667 	u16 q_idx = 0;
1668 	int err = 0;
1669 
1670 	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1671 	if (!qg_buf)
1672 		return -ENOMEM;
1673 
1674 	qg_buf->num_txqs = 1;
1675 
1676 	for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1677 		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1678 		if (err)
1679 			goto err_cfg_txqs;
1680 	}
1681 
1682 err_cfg_txqs:
1683 	kfree(qg_buf);
1684 	return err;
1685 }
1686 
1687 /**
1688  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1689  * @vsi: the VSI being configured
1690  *
1691  * Return 0 on success and a negative value on error
1692  * Configure the Tx VSI for operation.
1693  */
1694 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1695 {
1696 	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1697 }
1698 
1699 /**
1700  * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1701  * @vsi: the VSI being configured
1702  *
1703  * Return 0 on success and a negative value on error
1704  * Configure the Tx queues dedicated for XDP in given VSI for operation.
1705  */
1706 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1707 {
1708 	int ret;
1709 	int i;
1710 
1711 	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1712 	if (ret)
1713 		return ret;
1714 
1715 	for (i = 0; i < vsi->num_xdp_txq; i++)
1716 		vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1717 
1718 	return ret;
1719 }
1720 
1721 /**
1722  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1723  * @intrl: interrupt rate limit in usecs
1724  * @gran: interrupt rate limit granularity in usecs
1725  *
1726  * This function converts a decimal interrupt rate limit in usecs to the format
1727  * expected by firmware.
1728  */
1729 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1730 {
1731 	u32 val = intrl / gran;
1732 
1733 	if (val)
1734 		return val | GLINT_RATE_INTRL_ENA_M;
1735 	return 0;
1736 }
1737 
1738 /**
1739  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1740  * @vsi: the VSI being configured
1741  *
1742  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1743  * for the VF VSI.
1744  */
1745 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1746 {
1747 	struct ice_pf *pf = vsi->back;
1748 	struct ice_hw *hw = &pf->hw;
1749 	u16 txq = 0, rxq = 0;
1750 	int i, q;
1751 
1752 	for (i = 0; i < vsi->num_q_vectors; i++) {
1753 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1754 		u16 reg_idx = q_vector->reg_idx;
1755 
1756 		ice_cfg_itr(hw, q_vector);
1757 
1758 		wr32(hw, GLINT_RATE(reg_idx),
1759 		     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1760 
1761 		/* Both Transmit Queue Interrupt Cause Control register
1762 		 * and Receive Queue Interrupt Cause control register
1763 		 * expects MSIX_INDX field to be the vector index
1764 		 * within the function space and not the absolute
1765 		 * vector index across PF or across device.
1766 		 * For SR-IOV VF VSIs queue vector index always starts
1767 		 * with 1 since first vector index(0) is used for OICR
1768 		 * in VF space. Since VMDq and other PF VSIs are within
1769 		 * the PF function space, use the vector index that is
1770 		 * tracked for this PF.
1771 		 */
1772 		for (q = 0; q < q_vector->num_ring_tx; q++) {
1773 			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1774 					      q_vector->tx.itr_idx);
1775 			txq++;
1776 		}
1777 
1778 		for (q = 0; q < q_vector->num_ring_rx; q++) {
1779 			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1780 					      q_vector->rx.itr_idx);
1781 			rxq++;
1782 		}
1783 	}
1784 }
1785 
1786 /**
1787  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1788  * @vsi: the VSI being changed
1789  */
1790 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1791 {
1792 	struct ice_hw *hw = &vsi->back->hw;
1793 	struct ice_vsi_ctx *ctxt;
1794 	enum ice_status status;
1795 	int ret = 0;
1796 
1797 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1798 	if (!ctxt)
1799 		return -ENOMEM;
1800 
1801 	/* Here we are configuring the VSI to let the driver add VLAN tags by
1802 	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1803 	 * insertion happens in the Tx hot path, in ice_tx_map.
1804 	 */
1805 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1806 
1807 	/* Preserve existing VLAN strip setting */
1808 	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1809 				  ICE_AQ_VSI_VLAN_EMOD_M);
1810 
1811 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1812 
1813 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1814 	if (status) {
1815 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1816 			ice_stat_str(status),
1817 			ice_aq_str(hw->adminq.sq_last_status));
1818 		ret = -EIO;
1819 		goto out;
1820 	}
1821 
1822 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
1823 out:
1824 	kfree(ctxt);
1825 	return ret;
1826 }
1827 
1828 /**
1829  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1830  * @vsi: the VSI being changed
1831  * @ena: boolean value indicating if this is a enable or disable request
1832  */
1833 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1834 {
1835 	struct ice_hw *hw = &vsi->back->hw;
1836 	struct ice_vsi_ctx *ctxt;
1837 	enum ice_status status;
1838 	int ret = 0;
1839 
1840 	/* do not allow modifying VLAN stripping when a port VLAN is configured
1841 	 * on this VSI
1842 	 */
1843 	if (vsi->info.pvid)
1844 		return 0;
1845 
1846 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1847 	if (!ctxt)
1848 		return -ENOMEM;
1849 
1850 	/* Here we are configuring what the VSI should do with the VLAN tag in
1851 	 * the Rx packet. We can either leave the tag in the packet or put it in
1852 	 * the Rx descriptor.
1853 	 */
1854 	if (ena)
1855 		/* Strip VLAN tag from Rx packet and put it in the desc */
1856 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1857 	else
1858 		/* Disable stripping. Leave tag in packet */
1859 		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1860 
1861 	/* Allow all packets untagged/tagged */
1862 	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1863 
1864 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1865 
1866 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1867 	if (status) {
1868 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1869 			ena, ice_stat_str(status),
1870 			ice_aq_str(hw->adminq.sq_last_status));
1871 		ret = -EIO;
1872 		goto out;
1873 	}
1874 
1875 	vsi->info.vlan_flags = ctxt->info.vlan_flags;
1876 out:
1877 	kfree(ctxt);
1878 	return ret;
1879 }
1880 
1881 /**
1882  * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1883  * @vsi: the VSI whose rings are to be enabled
1884  *
1885  * Returns 0 on success and a negative value on error
1886  */
1887 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1888 {
1889 	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1890 }
1891 
1892 /**
1893  * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1894  * @vsi: the VSI whose rings are to be disabled
1895  *
1896  * Returns 0 on success and a negative value on error
1897  */
1898 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1899 {
1900 	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1901 }
1902 
1903 /**
1904  * ice_vsi_stop_tx_rings - Disable Tx rings
1905  * @vsi: the VSI being configured
1906  * @rst_src: reset source
1907  * @rel_vmvf_num: Relative ID of VF/VM
1908  * @rings: Tx ring array to be stopped
1909  */
1910 static int
1911 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1912 		      u16 rel_vmvf_num, struct ice_ring **rings)
1913 {
1914 	u16 q_idx;
1915 
1916 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1917 		return -EINVAL;
1918 
1919 	for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1920 		struct ice_txq_meta txq_meta = { };
1921 		int status;
1922 
1923 		if (!rings || !rings[q_idx])
1924 			return -EINVAL;
1925 
1926 		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1927 		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1928 					      rings[q_idx], &txq_meta);
1929 
1930 		if (status)
1931 			return status;
1932 	}
1933 
1934 	return 0;
1935 }
1936 
1937 /**
1938  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1939  * @vsi: the VSI being configured
1940  * @rst_src: reset source
1941  * @rel_vmvf_num: Relative ID of VF/VM
1942  */
1943 int
1944 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1945 			  u16 rel_vmvf_num)
1946 {
1947 	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1948 }
1949 
1950 /**
1951  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1952  * @vsi: the VSI being configured
1953  */
1954 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1955 {
1956 	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1957 }
1958 
1959 /**
1960  * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
1961  * @vsi: VSI to check whether or not VLAN pruning is enabled.
1962  *
1963  * returns true if Rx VLAN pruning is enabled and false otherwise.
1964  */
1965 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
1966 {
1967 	if (!vsi)
1968 		return false;
1969 
1970 	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
1971 }
1972 
1973 /**
1974  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1975  * @vsi: VSI to enable or disable VLAN pruning on
1976  * @ena: set to true to enable VLAN pruning and false to disable it
1977  * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1978  *
1979  * returns 0 if VSI is updated, negative otherwise
1980  */
1981 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1982 {
1983 	struct ice_vsi_ctx *ctxt;
1984 	struct ice_pf *pf;
1985 	int status;
1986 
1987 	if (!vsi)
1988 		return -EINVAL;
1989 
1990 	pf = vsi->back;
1991 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1992 	if (!ctxt)
1993 		return -ENOMEM;
1994 
1995 	ctxt->info = vsi->info;
1996 
1997 	if (ena)
1998 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1999 	else
2000 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2001 
2002 	if (!vlan_promisc)
2003 		ctxt->info.valid_sections =
2004 			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2005 
2006 	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2007 	if (status) {
2008 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2009 			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2010 			   ice_stat_str(status),
2011 			   ice_aq_str(pf->hw.adminq.sq_last_status));
2012 		goto err_out;
2013 	}
2014 
2015 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2016 
2017 	kfree(ctxt);
2018 	return 0;
2019 
2020 err_out:
2021 	kfree(ctxt);
2022 	return -EIO;
2023 }
2024 
2025 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2026 {
2027 	struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
2028 
2029 	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2030 	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2031 }
2032 
2033 /**
2034  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2035  * @vsi: VSI to set the q_vectors register index on
2036  */
2037 static int
2038 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2039 {
2040 	u16 i;
2041 
2042 	if (!vsi || !vsi->q_vectors)
2043 		return -EINVAL;
2044 
2045 	ice_for_each_q_vector(vsi, i) {
2046 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2047 
2048 		if (!q_vector) {
2049 			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2050 				i, vsi->vsi_num);
2051 			goto clear_reg_idx;
2052 		}
2053 
2054 		if (vsi->type == ICE_VSI_VF) {
2055 			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2056 
2057 			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2058 		} else {
2059 			q_vector->reg_idx =
2060 				q_vector->v_idx + vsi->base_vector;
2061 		}
2062 	}
2063 
2064 	return 0;
2065 
2066 clear_reg_idx:
2067 	ice_for_each_q_vector(vsi, i) {
2068 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2069 
2070 		if (q_vector)
2071 			q_vector->reg_idx = 0;
2072 	}
2073 
2074 	return -EINVAL;
2075 }
2076 
2077 /**
2078  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2079  * @vsi: the VSI being configured
2080  * @tx: bool to determine Tx or Rx rule
2081  * @create: bool to determine create or remove Rule
2082  */
2083 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2084 {
2085 	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2086 				    enum ice_sw_fwd_act_type act);
2087 	struct ice_pf *pf = vsi->back;
2088 	enum ice_status status;
2089 	struct device *dev;
2090 
2091 	dev = ice_pf_to_dev(pf);
2092 	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2093 
2094 	if (tx)
2095 		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2096 				  ICE_DROP_PACKET);
2097 	else
2098 		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX, ICE_FWD_TO_VSI);
2099 
2100 	if (status)
2101 		dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2102 			create ? "adding" : "removing", tx ? "TX" : "RX",
2103 			vsi->vsi_num, ice_stat_str(status));
2104 }
2105 
2106 /**
2107  * ice_vsi_setup - Set up a VSI by a given type
2108  * @pf: board private structure
2109  * @pi: pointer to the port_info instance
2110  * @vsi_type: VSI type
2111  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2112  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2113  *         fill-in ICE_INVAL_VFID as input.
2114  *
2115  * This allocates the sw VSI structure and its queue resources.
2116  *
2117  * Returns pointer to the successfully allocated and configured VSI sw struct on
2118  * success, NULL on failure.
2119  */
2120 struct ice_vsi *
2121 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2122 	      enum ice_vsi_type vsi_type, u16 vf_id)
2123 {
2124 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2125 	struct device *dev = ice_pf_to_dev(pf);
2126 	enum ice_status status;
2127 	struct ice_vsi *vsi;
2128 	int ret, i;
2129 
2130 	if (vsi_type == ICE_VSI_VF)
2131 		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2132 	else
2133 		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2134 
2135 	if (!vsi) {
2136 		dev_err(dev, "could not allocate VSI\n");
2137 		return NULL;
2138 	}
2139 
2140 	vsi->port_info = pi;
2141 	vsi->vsw = pf->first_sw;
2142 	if (vsi->type == ICE_VSI_PF)
2143 		vsi->ethtype = ETH_P_PAUSE;
2144 
2145 	if (vsi->type == ICE_VSI_VF)
2146 		vsi->vf_id = vf_id;
2147 
2148 	ice_alloc_fd_res(vsi);
2149 
2150 	if (ice_vsi_get_qs(vsi)) {
2151 		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2152 			vsi->idx);
2153 		goto unroll_vsi_alloc;
2154 	}
2155 
2156 	/* set RSS capabilities */
2157 	ice_vsi_set_rss_params(vsi);
2158 
2159 	/* set TC configuration */
2160 	ice_vsi_set_tc_cfg(vsi);
2161 
2162 	/* create the VSI */
2163 	ret = ice_vsi_init(vsi, true);
2164 	if (ret)
2165 		goto unroll_get_qs;
2166 
2167 	switch (vsi->type) {
2168 	case ICE_VSI_CTRL:
2169 	case ICE_VSI_PF:
2170 		ret = ice_vsi_alloc_q_vectors(vsi);
2171 		if (ret)
2172 			goto unroll_vsi_init;
2173 
2174 		ret = ice_vsi_setup_vector_base(vsi);
2175 		if (ret)
2176 			goto unroll_alloc_q_vector;
2177 
2178 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2179 		if (ret)
2180 			goto unroll_vector_base;
2181 
2182 		ret = ice_vsi_alloc_rings(vsi);
2183 		if (ret)
2184 			goto unroll_vector_base;
2185 
2186 		/* Always add VLAN ID 0 switch rule by default. This is needed
2187 		 * in order to allow all untagged and 0 tagged priority traffic
2188 		 * if Rx VLAN pruning is enabled. Also there are cases where we
2189 		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2190 		 * so this handles those cases (i.e. adding the PF to a bridge
2191 		 * without the 8021q module loaded).
2192 		 */
2193 		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2194 		if (ret)
2195 			goto unroll_clear_rings;
2196 
2197 		ice_vsi_map_rings_to_vectors(vsi);
2198 
2199 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2200 		if (vsi->type != ICE_VSI_CTRL)
2201 			/* Do not exit if configuring RSS had an issue, at
2202 			 * least receive traffic on first queue. Hence no
2203 			 * need to capture return value
2204 			 */
2205 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2206 				ice_vsi_cfg_rss_lut_key(vsi);
2207 				ice_vsi_set_rss_flow_fld(vsi);
2208 			}
2209 		ice_init_arfs(vsi);
2210 		break;
2211 	case ICE_VSI_VF:
2212 		/* VF driver will take care of creating netdev for this type and
2213 		 * map queues to vectors through Virtchnl, PF driver only
2214 		 * creates a VSI and corresponding structures for bookkeeping
2215 		 * purpose
2216 		 */
2217 		ret = ice_vsi_alloc_q_vectors(vsi);
2218 		if (ret)
2219 			goto unroll_vsi_init;
2220 
2221 		ret = ice_vsi_alloc_rings(vsi);
2222 		if (ret)
2223 			goto unroll_alloc_q_vector;
2224 
2225 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2226 		if (ret)
2227 			goto unroll_vector_base;
2228 
2229 		/* Do not exit if configuring RSS had an issue, at least
2230 		 * receive traffic on first queue. Hence no need to capture
2231 		 * return value
2232 		 */
2233 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2234 			ice_vsi_cfg_rss_lut_key(vsi);
2235 			ice_vsi_set_vf_rss_flow_fld(vsi);
2236 		}
2237 		break;
2238 	case ICE_VSI_LB:
2239 		ret = ice_vsi_alloc_rings(vsi);
2240 		if (ret)
2241 			goto unroll_vsi_init;
2242 		break;
2243 	default:
2244 		/* clean up the resources and exit */
2245 		goto unroll_vsi_init;
2246 	}
2247 
2248 	/* configure VSI nodes based on number of queues and TC's */
2249 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2250 		max_txqs[i] = vsi->alloc_txq;
2251 
2252 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2253 				 max_txqs);
2254 	if (status) {
2255 		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2256 			vsi->vsi_num, ice_stat_str(status));
2257 		goto unroll_vector_base;
2258 	}
2259 
2260 	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2261 	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2262 	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2263 	 * The rule is added once for PF VSI in order to create appropriate
2264 	 * recipe, since VSI/VSI list is ignored with drop action...
2265 	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2266 	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2267 	 * settings in the HW.
2268 	 */
2269 	if (!ice_is_safe_mode(pf))
2270 		if (vsi->type == ICE_VSI_PF) {
2271 			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2272 					 ICE_DROP_PACKET);
2273 			ice_cfg_sw_lldp(vsi, true, true);
2274 		}
2275 
2276 	return vsi;
2277 
2278 unroll_clear_rings:
2279 	ice_vsi_clear_rings(vsi);
2280 unroll_vector_base:
2281 	/* reclaim SW interrupts back to the common pool */
2282 	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2283 	pf->num_avail_sw_msix += vsi->num_q_vectors;
2284 unroll_alloc_q_vector:
2285 	ice_vsi_free_q_vectors(vsi);
2286 unroll_vsi_init:
2287 	ice_vsi_delete(vsi);
2288 unroll_get_qs:
2289 	ice_vsi_put_qs(vsi);
2290 unroll_vsi_alloc:
2291 	ice_vsi_clear(vsi);
2292 
2293 	return NULL;
2294 }
2295 
2296 /**
2297  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2298  * @vsi: the VSI being cleaned up
2299  */
2300 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2301 {
2302 	struct ice_pf *pf = vsi->back;
2303 	struct ice_hw *hw = &pf->hw;
2304 	u32 txq = 0;
2305 	u32 rxq = 0;
2306 	int i, q;
2307 
2308 	for (i = 0; i < vsi->num_q_vectors; i++) {
2309 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2310 		u16 reg_idx = q_vector->reg_idx;
2311 
2312 		wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2313 		wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2314 		for (q = 0; q < q_vector->num_ring_tx; q++) {
2315 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2316 			if (ice_is_xdp_ena_vsi(vsi)) {
2317 				u32 xdp_txq = txq + vsi->num_xdp_txq;
2318 
2319 				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2320 			}
2321 			txq++;
2322 		}
2323 
2324 		for (q = 0; q < q_vector->num_ring_rx; q++) {
2325 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2326 			rxq++;
2327 		}
2328 	}
2329 
2330 	ice_flush(hw);
2331 }
2332 
2333 /**
2334  * ice_vsi_free_irq - Free the IRQ association with the OS
2335  * @vsi: the VSI being configured
2336  */
2337 void ice_vsi_free_irq(struct ice_vsi *vsi)
2338 {
2339 	struct ice_pf *pf = vsi->back;
2340 	int base = vsi->base_vector;
2341 	int i;
2342 
2343 	if (!vsi->q_vectors || !vsi->irqs_ready)
2344 		return;
2345 
2346 	ice_vsi_release_msix(vsi);
2347 	if (vsi->type == ICE_VSI_VF)
2348 		return;
2349 
2350 	vsi->irqs_ready = false;
2351 	ice_for_each_q_vector(vsi, i) {
2352 		u16 vector = i + base;
2353 		int irq_num;
2354 
2355 		irq_num = pf->msix_entries[vector].vector;
2356 
2357 		/* free only the irqs that were actually requested */
2358 		if (!vsi->q_vectors[i] ||
2359 		    !(vsi->q_vectors[i]->num_ring_tx ||
2360 		      vsi->q_vectors[i]->num_ring_rx))
2361 			continue;
2362 
2363 		/* clear the affinity notifier in the IRQ descriptor */
2364 		irq_set_affinity_notifier(irq_num, NULL);
2365 
2366 		/* clear the affinity_mask in the IRQ descriptor */
2367 		irq_set_affinity_hint(irq_num, NULL);
2368 		synchronize_irq(irq_num);
2369 		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2370 	}
2371 }
2372 
2373 /**
2374  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2375  * @vsi: the VSI having resources freed
2376  */
2377 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2378 {
2379 	int i;
2380 
2381 	if (!vsi->tx_rings)
2382 		return;
2383 
2384 	ice_for_each_txq(vsi, i)
2385 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2386 			ice_free_tx_ring(vsi->tx_rings[i]);
2387 }
2388 
2389 /**
2390  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2391  * @vsi: the VSI having resources freed
2392  */
2393 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2394 {
2395 	int i;
2396 
2397 	if (!vsi->rx_rings)
2398 		return;
2399 
2400 	ice_for_each_rxq(vsi, i)
2401 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2402 			ice_free_rx_ring(vsi->rx_rings[i]);
2403 }
2404 
2405 /**
2406  * ice_vsi_close - Shut down a VSI
2407  * @vsi: the VSI being shut down
2408  */
2409 void ice_vsi_close(struct ice_vsi *vsi)
2410 {
2411 	if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2412 		ice_down(vsi);
2413 
2414 	ice_vsi_free_irq(vsi);
2415 	ice_vsi_free_tx_rings(vsi);
2416 	ice_vsi_free_rx_rings(vsi);
2417 }
2418 
2419 /**
2420  * ice_ena_vsi - resume a VSI
2421  * @vsi: the VSI being resume
2422  * @locked: is the rtnl_lock already held
2423  */
2424 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2425 {
2426 	int err = 0;
2427 
2428 	if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2429 		return 0;
2430 
2431 	clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2432 
2433 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2434 		if (netif_running(vsi->netdev)) {
2435 			if (!locked)
2436 				rtnl_lock();
2437 
2438 			err = ice_open(vsi->netdev);
2439 
2440 			if (!locked)
2441 				rtnl_unlock();
2442 		}
2443 	} else if (vsi->type == ICE_VSI_CTRL) {
2444 		err = ice_vsi_open_ctrl(vsi);
2445 	}
2446 
2447 	return err;
2448 }
2449 
2450 /**
2451  * ice_dis_vsi - pause a VSI
2452  * @vsi: the VSI being paused
2453  * @locked: is the rtnl_lock already held
2454  */
2455 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2456 {
2457 	if (test_bit(__ICE_DOWN, vsi->state))
2458 		return;
2459 
2460 	set_bit(__ICE_NEEDS_RESTART, vsi->state);
2461 
2462 	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2463 		if (netif_running(vsi->netdev)) {
2464 			if (!locked)
2465 				rtnl_lock();
2466 
2467 			ice_stop(vsi->netdev);
2468 
2469 			if (!locked)
2470 				rtnl_unlock();
2471 		} else {
2472 			ice_vsi_close(vsi);
2473 		}
2474 	} else if (vsi->type == ICE_VSI_CTRL) {
2475 		ice_vsi_close(vsi);
2476 	}
2477 }
2478 
2479 /**
2480  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2481  * @vsi: the VSI being un-configured
2482  */
2483 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2484 {
2485 	int base = vsi->base_vector;
2486 	struct ice_pf *pf = vsi->back;
2487 	struct ice_hw *hw = &pf->hw;
2488 	u32 val;
2489 	int i;
2490 
2491 	/* disable interrupt causation from each queue */
2492 	if (vsi->tx_rings) {
2493 		ice_for_each_txq(vsi, i) {
2494 			if (vsi->tx_rings[i]) {
2495 				u16 reg;
2496 
2497 				reg = vsi->tx_rings[i]->reg_idx;
2498 				val = rd32(hw, QINT_TQCTL(reg));
2499 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2500 				wr32(hw, QINT_TQCTL(reg), val);
2501 			}
2502 		}
2503 	}
2504 
2505 	if (vsi->rx_rings) {
2506 		ice_for_each_rxq(vsi, i) {
2507 			if (vsi->rx_rings[i]) {
2508 				u16 reg;
2509 
2510 				reg = vsi->rx_rings[i]->reg_idx;
2511 				val = rd32(hw, QINT_RQCTL(reg));
2512 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2513 				wr32(hw, QINT_RQCTL(reg), val);
2514 			}
2515 		}
2516 	}
2517 
2518 	/* disable each interrupt */
2519 	ice_for_each_q_vector(vsi, i) {
2520 		if (!vsi->q_vectors[i])
2521 			continue;
2522 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2523 	}
2524 
2525 	ice_flush(hw);
2526 
2527 	/* don't call synchronize_irq() for VF's from the host */
2528 	if (vsi->type == ICE_VSI_VF)
2529 		return;
2530 
2531 	ice_for_each_q_vector(vsi, i)
2532 		synchronize_irq(pf->msix_entries[i + base].vector);
2533 }
2534 
2535 /**
2536  * ice_napi_del - Remove NAPI handler for the VSI
2537  * @vsi: VSI for which NAPI handler is to be removed
2538  */
2539 void ice_napi_del(struct ice_vsi *vsi)
2540 {
2541 	int v_idx;
2542 
2543 	if (!vsi->netdev)
2544 		return;
2545 
2546 	ice_for_each_q_vector(vsi, v_idx)
2547 		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2548 }
2549 
2550 /**
2551  * ice_vsi_release - Delete a VSI and free its resources
2552  * @vsi: the VSI being removed
2553  *
2554  * Returns 0 on success or < 0 on error
2555  */
2556 int ice_vsi_release(struct ice_vsi *vsi)
2557 {
2558 	struct ice_pf *pf;
2559 
2560 	if (!vsi->back)
2561 		return -ENODEV;
2562 	pf = vsi->back;
2563 
2564 	/* do not unregister while driver is in the reset recovery pending
2565 	 * state. Since reset/rebuild happens through PF service task workqueue,
2566 	 * it's not a good idea to unregister netdev that is associated to the
2567 	 * PF that is running the work queue items currently. This is done to
2568 	 * avoid check_flush_dependency() warning on this wq
2569 	 */
2570 	if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2571 		unregister_netdev(vsi->netdev);
2572 
2573 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2574 		ice_rss_clean(vsi);
2575 
2576 	/* Disable VSI and free resources */
2577 	if (vsi->type != ICE_VSI_LB)
2578 		ice_vsi_dis_irq(vsi);
2579 	ice_vsi_close(vsi);
2580 
2581 	/* SR-IOV determines needed MSIX resources all at once instead of per
2582 	 * VSI since when VFs are spawned we know how many VFs there are and how
2583 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2584 	 * cleared in the same manner.
2585 	 */
2586 	if (vsi->type != ICE_VSI_VF) {
2587 		/* reclaim SW interrupts back to the common pool */
2588 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2589 		pf->num_avail_sw_msix += vsi->num_q_vectors;
2590 	}
2591 
2592 	if (!ice_is_safe_mode(pf)) {
2593 		if (vsi->type == ICE_VSI_PF) {
2594 			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2595 					    ICE_DROP_PACKET);
2596 			ice_cfg_sw_lldp(vsi, true, false);
2597 			/* The Rx rule will only exist to remove if the LLDP FW
2598 			 * engine is currently stopped
2599 			 */
2600 			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2601 				ice_cfg_sw_lldp(vsi, false, false);
2602 		}
2603 	}
2604 
2605 	ice_fltr_remove_all(vsi);
2606 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2607 	ice_vsi_delete(vsi);
2608 	ice_vsi_free_q_vectors(vsi);
2609 
2610 	/* make sure unregister_netdev() was called by checking __ICE_DOWN */
2611 	if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2612 		free_netdev(vsi->netdev);
2613 		vsi->netdev = NULL;
2614 	}
2615 
2616 	ice_vsi_clear_rings(vsi);
2617 
2618 	ice_vsi_put_qs(vsi);
2619 
2620 	/* retain SW VSI data structure since it is needed to unregister and
2621 	 * free VSI netdev when PF is not in reset recovery pending state,\
2622 	 * for ex: during rmmod.
2623 	 */
2624 	if (!ice_is_reset_in_progress(pf->state))
2625 		ice_vsi_clear(vsi);
2626 
2627 	return 0;
2628 }
2629 
2630 /**
2631  * ice_vsi_rebuild_update_coalesce - set coalesce for a q_vector
2632  * @q_vector: pointer to q_vector which is being updated
2633  * @coalesce: pointer to array of struct with stored coalesce
2634  *
2635  * Set coalesce param in q_vector and update these parameters in HW.
2636  */
2637 static void
2638 ice_vsi_rebuild_update_coalesce(struct ice_q_vector *q_vector,
2639 				struct ice_coalesce_stored *coalesce)
2640 {
2641 	struct ice_ring_container *rx_rc = &q_vector->rx;
2642 	struct ice_ring_container *tx_rc = &q_vector->tx;
2643 	struct ice_hw *hw = &q_vector->vsi->back->hw;
2644 
2645 	tx_rc->itr_setting = coalesce->itr_tx;
2646 	rx_rc->itr_setting = coalesce->itr_rx;
2647 
2648 	/* dynamic ITR values will be updated during Tx/Rx */
2649 	if (!ITR_IS_DYNAMIC(tx_rc->itr_setting))
2650 		wr32(hw, GLINT_ITR(tx_rc->itr_idx, q_vector->reg_idx),
2651 		     ITR_REG_ALIGN(tx_rc->itr_setting) >>
2652 		     ICE_ITR_GRAN_S);
2653 	if (!ITR_IS_DYNAMIC(rx_rc->itr_setting))
2654 		wr32(hw, GLINT_ITR(rx_rc->itr_idx, q_vector->reg_idx),
2655 		     ITR_REG_ALIGN(rx_rc->itr_setting) >>
2656 		     ICE_ITR_GRAN_S);
2657 
2658 	q_vector->intrl = coalesce->intrl;
2659 	wr32(hw, GLINT_RATE(q_vector->reg_idx),
2660 	     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
2661 }
2662 
2663 /**
2664  * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2665  * @vsi: VSI connected with q_vectors
2666  * @coalesce: array of struct with stored coalesce
2667  *
2668  * Returns array size.
2669  */
2670 static int
2671 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2672 			     struct ice_coalesce_stored *coalesce)
2673 {
2674 	int i;
2675 
2676 	ice_for_each_q_vector(vsi, i) {
2677 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2678 
2679 		coalesce[i].itr_tx = q_vector->tx.itr_setting;
2680 		coalesce[i].itr_rx = q_vector->rx.itr_setting;
2681 		coalesce[i].intrl = q_vector->intrl;
2682 	}
2683 
2684 	return vsi->num_q_vectors;
2685 }
2686 
2687 /**
2688  * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2689  * @vsi: VSI connected with q_vectors
2690  * @coalesce: pointer to array of struct with stored coalesce
2691  * @size: size of coalesce array
2692  *
2693  * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2694  * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2695  * to default value.
2696  */
2697 static void
2698 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2699 			     struct ice_coalesce_stored *coalesce, int size)
2700 {
2701 	int i;
2702 
2703 	if ((size && !coalesce) || !vsi)
2704 		return;
2705 
2706 	for (i = 0; i < size && i < vsi->num_q_vectors; i++)
2707 		ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2708 						&coalesce[i]);
2709 
2710 	/* number of q_vectors increased, so assume coalesce settings were
2711 	 * changed globally (i.e. ethtool -C eth0 instead of per-queue) and use
2712 	 * the previous settings from q_vector 0 for all of the new q_vectors
2713 	 */
2714 	for (; i < vsi->num_q_vectors; i++)
2715 		ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2716 						&coalesce[0]);
2717 }
2718 
2719 /**
2720  * ice_vsi_rebuild - Rebuild VSI after reset
2721  * @vsi: VSI to be rebuild
2722  * @init_vsi: is this an initialization or a reconfigure of the VSI
2723  *
2724  * Returns 0 on success and negative value on failure
2725  */
2726 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2727 {
2728 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2729 	struct ice_coalesce_stored *coalesce;
2730 	int prev_num_q_vectors = 0;
2731 	struct ice_vf *vf = NULL;
2732 	enum ice_status status;
2733 	struct ice_pf *pf;
2734 	int ret, i;
2735 
2736 	if (!vsi)
2737 		return -EINVAL;
2738 
2739 	pf = vsi->back;
2740 	if (vsi->type == ICE_VSI_VF)
2741 		vf = &pf->vf[vsi->vf_id];
2742 
2743 	coalesce = kcalloc(vsi->num_q_vectors,
2744 			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
2745 	if (coalesce)
2746 		prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi,
2747 								  coalesce);
2748 	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2749 	ice_vsi_free_q_vectors(vsi);
2750 
2751 	/* SR-IOV determines needed MSIX resources all at once instead of per
2752 	 * VSI since when VFs are spawned we know how many VFs there are and how
2753 	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2754 	 * cleared in the same manner.
2755 	 */
2756 	if (vsi->type != ICE_VSI_VF) {
2757 		/* reclaim SW interrupts back to the common pool */
2758 		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2759 		pf->num_avail_sw_msix += vsi->num_q_vectors;
2760 		vsi->base_vector = 0;
2761 	}
2762 
2763 	if (ice_is_xdp_ena_vsi(vsi))
2764 		/* return value check can be skipped here, it always returns
2765 		 * 0 if reset is in progress
2766 		 */
2767 		ice_destroy_xdp_rings(vsi);
2768 	ice_vsi_put_qs(vsi);
2769 	ice_vsi_clear_rings(vsi);
2770 	ice_vsi_free_arrays(vsi);
2771 	if (vsi->type == ICE_VSI_VF)
2772 		ice_vsi_set_num_qs(vsi, vf->vf_id);
2773 	else
2774 		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2775 
2776 	ret = ice_vsi_alloc_arrays(vsi);
2777 	if (ret < 0)
2778 		goto err_vsi;
2779 
2780 	ice_vsi_get_qs(vsi);
2781 
2782 	ice_alloc_fd_res(vsi);
2783 	ice_vsi_set_tc_cfg(vsi);
2784 
2785 	/* Initialize VSI struct elements and create VSI in FW */
2786 	ret = ice_vsi_init(vsi, init_vsi);
2787 	if (ret < 0)
2788 		goto err_vsi;
2789 
2790 	switch (vsi->type) {
2791 	case ICE_VSI_CTRL:
2792 	case ICE_VSI_PF:
2793 		ret = ice_vsi_alloc_q_vectors(vsi);
2794 		if (ret)
2795 			goto err_rings;
2796 
2797 		ret = ice_vsi_setup_vector_base(vsi);
2798 		if (ret)
2799 			goto err_vectors;
2800 
2801 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2802 		if (ret)
2803 			goto err_vectors;
2804 
2805 		ret = ice_vsi_alloc_rings(vsi);
2806 		if (ret)
2807 			goto err_vectors;
2808 
2809 		ice_vsi_map_rings_to_vectors(vsi);
2810 		if (ice_is_xdp_ena_vsi(vsi)) {
2811 			vsi->num_xdp_txq = vsi->alloc_rxq;
2812 			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2813 			if (ret)
2814 				goto err_vectors;
2815 		}
2816 		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2817 		if (vsi->type != ICE_VSI_CTRL)
2818 			/* Do not exit if configuring RSS had an issue, at
2819 			 * least receive traffic on first queue. Hence no
2820 			 * need to capture return value
2821 			 */
2822 			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2823 				ice_vsi_cfg_rss_lut_key(vsi);
2824 		break;
2825 	case ICE_VSI_VF:
2826 		ret = ice_vsi_alloc_q_vectors(vsi);
2827 		if (ret)
2828 			goto err_rings;
2829 
2830 		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2831 		if (ret)
2832 			goto err_vectors;
2833 
2834 		ret = ice_vsi_alloc_rings(vsi);
2835 		if (ret)
2836 			goto err_vectors;
2837 
2838 		break;
2839 	default:
2840 		break;
2841 	}
2842 
2843 	/* configure VSI nodes based on number of queues and TC's */
2844 	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2845 		max_txqs[i] = vsi->alloc_txq;
2846 
2847 		if (ice_is_xdp_ena_vsi(vsi))
2848 			max_txqs[i] += vsi->num_xdp_txq;
2849 	}
2850 
2851 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2852 				 max_txqs);
2853 	if (status) {
2854 		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
2855 			vsi->vsi_num, ice_stat_str(status));
2856 		if (init_vsi) {
2857 			ret = -EIO;
2858 			goto err_vectors;
2859 		} else {
2860 			return ice_schedule_reset(pf, ICE_RESET_PFR);
2861 		}
2862 	}
2863 	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
2864 	kfree(coalesce);
2865 
2866 	return 0;
2867 
2868 err_vectors:
2869 	ice_vsi_free_q_vectors(vsi);
2870 err_rings:
2871 	if (vsi->netdev) {
2872 		vsi->current_netdev_flags = 0;
2873 		unregister_netdev(vsi->netdev);
2874 		free_netdev(vsi->netdev);
2875 		vsi->netdev = NULL;
2876 	}
2877 err_vsi:
2878 	ice_vsi_clear(vsi);
2879 	set_bit(__ICE_RESET_FAILED, pf->state);
2880 	kfree(coalesce);
2881 	return ret;
2882 }
2883 
2884 /**
2885  * ice_is_reset_in_progress - check for a reset in progress
2886  * @state: PF state field
2887  */
2888 bool ice_is_reset_in_progress(unsigned long *state)
2889 {
2890 	return test_bit(__ICE_RESET_OICR_RECV, state) ||
2891 	       test_bit(__ICE_DCBNL_DEVRESET, state) ||
2892 	       test_bit(__ICE_PFR_REQ, state) ||
2893 	       test_bit(__ICE_CORER_REQ, state) ||
2894 	       test_bit(__ICE_GLOBR_REQ, state);
2895 }
2896 
2897 #ifdef CONFIG_DCB
2898 /**
2899  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2900  * @vsi: VSI being configured
2901  * @ctx: the context buffer returned from AQ VSI update command
2902  */
2903 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2904 {
2905 	vsi->info.mapping_flags = ctx->info.mapping_flags;
2906 	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2907 	       sizeof(vsi->info.q_mapping));
2908 	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2909 	       sizeof(vsi->info.tc_mapping));
2910 }
2911 
2912 /**
2913  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2914  * @vsi: VSI to be configured
2915  * @ena_tc: TC bitmap
2916  *
2917  * VSI queues expected to be quiesced before calling this function
2918  */
2919 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2920 {
2921 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2922 	struct ice_pf *pf = vsi->back;
2923 	struct ice_vsi_ctx *ctx;
2924 	enum ice_status status;
2925 	struct device *dev;
2926 	int i, ret = 0;
2927 	u8 num_tc = 0;
2928 
2929 	dev = ice_pf_to_dev(pf);
2930 
2931 	ice_for_each_traffic_class(i) {
2932 		/* build bitmap of enabled TCs */
2933 		if (ena_tc & BIT(i))
2934 			num_tc++;
2935 		/* populate max_txqs per TC */
2936 		max_txqs[i] = vsi->alloc_txq;
2937 	}
2938 
2939 	vsi->tc_cfg.ena_tc = ena_tc;
2940 	vsi->tc_cfg.numtc = num_tc;
2941 
2942 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2943 	if (!ctx)
2944 		return -ENOMEM;
2945 
2946 	ctx->vf_num = 0;
2947 	ctx->info = vsi->info;
2948 
2949 	ice_vsi_setup_q_map(vsi, ctx);
2950 
2951 	/* must to indicate which section of VSI context are being modified */
2952 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2953 	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2954 	if (status) {
2955 		dev_info(dev, "Failed VSI Update\n");
2956 		ret = -EIO;
2957 		goto out;
2958 	}
2959 
2960 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2961 				 max_txqs);
2962 
2963 	if (status) {
2964 		dev_err(dev, "VSI %d failed TC config, error %s\n",
2965 			vsi->vsi_num, ice_stat_str(status));
2966 		ret = -EIO;
2967 		goto out;
2968 	}
2969 	ice_vsi_update_q_map(vsi, ctx);
2970 	vsi->info.valid_sections = 0;
2971 
2972 	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2973 out:
2974 	kfree(ctx);
2975 	return ret;
2976 }
2977 #endif /* CONFIG_DCB */
2978 
2979 /**
2980  * ice_update_ring_stats - Update ring statistics
2981  * @ring: ring to update
2982  * @cont: used to increment per-vector counters
2983  * @pkts: number of processed packets
2984  * @bytes: number of processed bytes
2985  *
2986  * This function assumes that caller has acquired a u64_stats_sync lock.
2987  */
2988 static void
2989 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2990 		      u64 pkts, u64 bytes)
2991 {
2992 	ring->stats.bytes += bytes;
2993 	ring->stats.pkts += pkts;
2994 	cont->total_bytes += bytes;
2995 	cont->total_pkts += pkts;
2996 }
2997 
2998 /**
2999  * ice_update_tx_ring_stats - Update Tx ring specific counters
3000  * @tx_ring: ring to update
3001  * @pkts: number of processed packets
3002  * @bytes: number of processed bytes
3003  */
3004 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3005 {
3006 	u64_stats_update_begin(&tx_ring->syncp);
3007 	ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
3008 	u64_stats_update_end(&tx_ring->syncp);
3009 }
3010 
3011 /**
3012  * ice_update_rx_ring_stats - Update Rx ring specific counters
3013  * @rx_ring: ring to update
3014  * @pkts: number of processed packets
3015  * @bytes: number of processed bytes
3016  */
3017 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3018 {
3019 	u64_stats_update_begin(&rx_ring->syncp);
3020 	ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
3021 	u64_stats_update_end(&rx_ring->syncp);
3022 }
3023 
3024 /**
3025  * ice_status_to_errno - convert from enum ice_status to Linux errno
3026  * @err: ice_status value to convert
3027  */
3028 int ice_status_to_errno(enum ice_status err)
3029 {
3030 	switch (err) {
3031 	case ICE_SUCCESS:
3032 		return 0;
3033 	case ICE_ERR_DOES_NOT_EXIST:
3034 		return -ENOENT;
3035 	case ICE_ERR_OUT_OF_RANGE:
3036 		return -ENOTTY;
3037 	case ICE_ERR_PARAM:
3038 		return -EINVAL;
3039 	case ICE_ERR_NO_MEMORY:
3040 		return -ENOMEM;
3041 	case ICE_ERR_MAX_LIMIT:
3042 		return -EAGAIN;
3043 	default:
3044 		return -EINVAL;
3045 	}
3046 }
3047 
3048 /**
3049  * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3050  * @sw: switch to check if its default forwarding VSI is free
3051  *
3052  * Return true if the default forwarding VSI is already being used, else returns
3053  * false signalling that it's available to use.
3054  */
3055 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3056 {
3057 	return (sw->dflt_vsi && sw->dflt_vsi_ena);
3058 }
3059 
3060 /**
3061  * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3062  * @sw: switch for the default forwarding VSI to compare against
3063  * @vsi: VSI to compare against default forwarding VSI
3064  *
3065  * If this VSI passed in is the default forwarding VSI then return true, else
3066  * return false
3067  */
3068 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3069 {
3070 	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3071 }
3072 
3073 /**
3074  * ice_set_dflt_vsi - set the default forwarding VSI
3075  * @sw: switch used to assign the default forwarding VSI
3076  * @vsi: VSI getting set as the default forwarding VSI on the switch
3077  *
3078  * If the VSI passed in is already the default VSI and it's enabled just return
3079  * success.
3080  *
3081  * If there is already a default VSI on the switch and it's enabled then return
3082  * -EEXIST since there can only be one default VSI per switch.
3083  *
3084  *  Otherwise try to set the VSI passed in as the switch's default VSI and
3085  *  return the result.
3086  */
3087 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3088 {
3089 	enum ice_status status;
3090 	struct device *dev;
3091 
3092 	if (!sw || !vsi)
3093 		return -EINVAL;
3094 
3095 	dev = ice_pf_to_dev(vsi->back);
3096 
3097 	/* the VSI passed in is already the default VSI */
3098 	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3099 		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3100 			vsi->vsi_num);
3101 		return 0;
3102 	}
3103 
3104 	/* another VSI is already the default VSI for this switch */
3105 	if (ice_is_dflt_vsi_in_use(sw)) {
3106 		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3107 			sw->dflt_vsi->vsi_num);
3108 		return -EEXIST;
3109 	}
3110 
3111 	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3112 	if (status) {
3113 		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3114 			vsi->vsi_num, ice_stat_str(status));
3115 		return -EIO;
3116 	}
3117 
3118 	sw->dflt_vsi = vsi;
3119 	sw->dflt_vsi_ena = true;
3120 
3121 	return 0;
3122 }
3123 
3124 /**
3125  * ice_clear_dflt_vsi - clear the default forwarding VSI
3126  * @sw: switch used to clear the default VSI
3127  *
3128  * If the switch has no default VSI or it's not enabled then return error.
3129  *
3130  * Otherwise try to clear the default VSI and return the result.
3131  */
3132 int ice_clear_dflt_vsi(struct ice_sw *sw)
3133 {
3134 	struct ice_vsi *dflt_vsi;
3135 	enum ice_status status;
3136 	struct device *dev;
3137 
3138 	if (!sw)
3139 		return -EINVAL;
3140 
3141 	dev = ice_pf_to_dev(sw->pf);
3142 
3143 	dflt_vsi = sw->dflt_vsi;
3144 
3145 	/* there is no default VSI configured */
3146 	if (!ice_is_dflt_vsi_in_use(sw))
3147 		return -ENODEV;
3148 
3149 	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3150 				  ICE_FLTR_RX);
3151 	if (status) {
3152 		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3153 			dflt_vsi->vsi_num, ice_stat_str(status));
3154 		return -EIO;
3155 	}
3156 
3157 	sw->dflt_vsi = NULL;
3158 	sw->dflt_vsi_ena = false;
3159 
3160 	return 0;
3161 }
3162