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