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