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