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