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