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