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