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