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