]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_lib.c
ice: configure software LLDP in ice_init_pf_dcb
[linux.git] / drivers / net / ethernet / intel / ice / ice_lib.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_base.h"
6 #include "ice_lib.h"
7 #include "ice_dcb_lib.h"
8
9 /**
10  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
11  * @vsi: the VSI being configured
12  * @ena: start or stop the Rx rings
13  */
14 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
15 {
16         int i, ret = 0;
17
18         for (i = 0; i < vsi->num_rxq; i++) {
19                 ret = ice_vsi_ctrl_rx_ring(vsi, ena, i);
20                 if (ret)
21                         break;
22         }
23
24         return ret;
25 }
26
27 /**
28  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
29  * @vsi: VSI pointer
30  *
31  * On error: returns error code (negative)
32  * On success: returns 0
33  */
34 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
35 {
36         struct ice_pf *pf = vsi->back;
37
38         /* allocate memory for both Tx and Rx ring pointers */
39         vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
40                                      sizeof(*vsi->tx_rings), GFP_KERNEL);
41         if (!vsi->tx_rings)
42                 return -ENOMEM;
43
44         vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
45                                      sizeof(*vsi->rx_rings), GFP_KERNEL);
46         if (!vsi->rx_rings)
47                 goto err_rings;
48
49         /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
50         vsi->txq_map = devm_kcalloc(&pf->pdev->dev, (2 * vsi->alloc_txq),
51                                     sizeof(*vsi->txq_map), GFP_KERNEL);
52
53         if (!vsi->txq_map)
54                 goto err_txq_map;
55
56         vsi->rxq_map = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
57                                     sizeof(*vsi->rxq_map), GFP_KERNEL);
58         if (!vsi->rxq_map)
59                 goto err_rxq_map;
60
61         /* There is no need to allocate q_vectors for a loopback VSI. */
62         if (vsi->type == ICE_VSI_LB)
63                 return 0;
64
65         /* allocate memory for q_vector pointers */
66         vsi->q_vectors = devm_kcalloc(&pf->pdev->dev, vsi->num_q_vectors,
67                                       sizeof(*vsi->q_vectors), GFP_KERNEL);
68         if (!vsi->q_vectors)
69                 goto err_vectors;
70
71         return 0;
72
73 err_vectors:
74         devm_kfree(&pf->pdev->dev, vsi->rxq_map);
75 err_rxq_map:
76         devm_kfree(&pf->pdev->dev, vsi->txq_map);
77 err_txq_map:
78         devm_kfree(&pf->pdev->dev, vsi->rx_rings);
79 err_rings:
80         devm_kfree(&pf->pdev->dev, vsi->tx_rings);
81         return -ENOMEM;
82 }
83
84 /**
85  * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
86  * @vsi: the VSI being configured
87  */
88 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
89 {
90         switch (vsi->type) {
91         case ICE_VSI_PF:
92                 /* fall through */
93         case ICE_VSI_LB:
94                 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
95                 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
96                 break;
97         default:
98                 dev_dbg(&vsi->back->pdev->dev,
99                         "Not setting number of Tx/Rx descriptors for VSI type %d\n",
100                         vsi->type);
101                 break;
102         }
103 }
104
105 /**
106  * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
107  * @vsi: the VSI being configured
108  * @vf_id: ID of the VF being configured
109  *
110  * Return 0 on success and a negative value on error
111  */
112 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
113 {
114         struct ice_pf *pf = vsi->back;
115         struct ice_vf *vf = NULL;
116
117         if (vsi->type == ICE_VSI_VF)
118                 vsi->vf_id = vf_id;
119
120         switch (vsi->type) {
121         case ICE_VSI_PF:
122                 vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
123                                        num_online_cpus());
124
125                 pf->num_lan_tx = vsi->alloc_txq;
126
127                 /* only 1 Rx queue unless RSS is enabled */
128                 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
129                         vsi->alloc_rxq = 1;
130                 else
131                         vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
132                                                num_online_cpus());
133
134                 pf->num_lan_rx = vsi->alloc_rxq;
135
136                 vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
137                 break;
138         case ICE_VSI_VF:
139                 vf = &pf->vf[vsi->vf_id];
140                 vsi->alloc_txq = vf->num_vf_qs;
141                 vsi->alloc_rxq = vf->num_vf_qs;
142                 /* pf->num_vf_msix includes (VF miscellaneous vector +
143                  * data queue interrupts). Since vsi->num_q_vectors is number
144                  * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
145                  * original vector count
146                  */
147                 vsi->num_q_vectors = pf->num_vf_msix - ICE_NONQ_VECS_VF;
148                 break;
149         case ICE_VSI_LB:
150                 vsi->alloc_txq = 1;
151                 vsi->alloc_rxq = 1;
152                 break;
153         default:
154                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
155                 break;
156         }
157
158         ice_vsi_set_num_desc(vsi);
159 }
160
161 /**
162  * ice_get_free_slot - get the next non-NULL location index in array
163  * @array: array to search
164  * @size: size of the array
165  * @curr: last known occupied index to be used as a search hint
166  *
167  * void * is being used to keep the functionality generic. This lets us use this
168  * function on any array of pointers.
169  */
170 static int ice_get_free_slot(void *array, int size, int curr)
171 {
172         int **tmp_array = (int **)array;
173         int next;
174
175         if (curr < (size - 1) && !tmp_array[curr + 1]) {
176                 next = curr + 1;
177         } else {
178                 int i = 0;
179
180                 while ((i < size) && (tmp_array[i]))
181                         i++;
182                 if (i == size)
183                         next = ICE_NO_VSI;
184                 else
185                         next = i;
186         }
187         return next;
188 }
189
190 /**
191  * ice_vsi_delete - delete a VSI from the switch
192  * @vsi: pointer to VSI being removed
193  */
194 void ice_vsi_delete(struct ice_vsi *vsi)
195 {
196         struct ice_pf *pf = vsi->back;
197         struct ice_vsi_ctx *ctxt;
198         enum ice_status status;
199
200         ctxt = devm_kzalloc(&pf->pdev->dev, sizeof(*ctxt), GFP_KERNEL);
201         if (!ctxt)
202                 return;
203
204         if (vsi->type == ICE_VSI_VF)
205                 ctxt->vf_num = vsi->vf_id;
206         ctxt->vsi_num = vsi->vsi_num;
207
208         memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
209
210         status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
211         if (status)
212                 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
213                         vsi->vsi_num);
214
215         devm_kfree(&pf->pdev->dev, ctxt);
216 }
217
218 /**
219  * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
220  * @vsi: pointer to VSI being cleared
221  */
222 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
223 {
224         struct ice_pf *pf = vsi->back;
225
226         /* free the ring and vector containers */
227         if (vsi->q_vectors) {
228                 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
229                 vsi->q_vectors = NULL;
230         }
231         if (vsi->tx_rings) {
232                 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
233                 vsi->tx_rings = NULL;
234         }
235         if (vsi->rx_rings) {
236                 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
237                 vsi->rx_rings = NULL;
238         }
239         if (vsi->txq_map) {
240                 devm_kfree(&pf->pdev->dev, vsi->txq_map);
241                 vsi->txq_map = NULL;
242         }
243         if (vsi->rxq_map) {
244                 devm_kfree(&pf->pdev->dev, vsi->rxq_map);
245                 vsi->rxq_map = NULL;
246         }
247 }
248
249 /**
250  * ice_vsi_clear - clean up and deallocate the provided VSI
251  * @vsi: pointer to VSI being cleared
252  *
253  * This deallocates the VSI's queue resources, removes it from the PF's
254  * VSI array if necessary, and deallocates the VSI
255  *
256  * Returns 0 on success, negative on failure
257  */
258 int ice_vsi_clear(struct ice_vsi *vsi)
259 {
260         struct ice_pf *pf = NULL;
261
262         if (!vsi)
263                 return 0;
264
265         if (!vsi->back)
266                 return -EINVAL;
267
268         pf = vsi->back;
269
270         if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
271                 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
272                         vsi->idx);
273                 return -EINVAL;
274         }
275
276         mutex_lock(&pf->sw_mutex);
277         /* updates the PF for this cleared VSI */
278
279         pf->vsi[vsi->idx] = NULL;
280         if (vsi->idx < pf->next_vsi)
281                 pf->next_vsi = vsi->idx;
282
283         ice_vsi_free_arrays(vsi);
284         mutex_unlock(&pf->sw_mutex);
285         devm_kfree(&pf->pdev->dev, vsi);
286
287         return 0;
288 }
289
290 /**
291  * ice_msix_clean_rings - MSIX mode Interrupt Handler
292  * @irq: interrupt number
293  * @data: pointer to a q_vector
294  */
295 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
296 {
297         struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
298
299         if (!q_vector->tx.ring && !q_vector->rx.ring)
300                 return IRQ_HANDLED;
301
302         napi_schedule(&q_vector->napi);
303
304         return IRQ_HANDLED;
305 }
306
307 /**
308  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
309  * @pf: board private structure
310  * @type: type of VSI
311  * @vf_id: ID of the VF being configured
312  *
313  * returns a pointer to a VSI on success, NULL on failure.
314  */
315 static struct ice_vsi *
316 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type, u16 vf_id)
317 {
318         struct ice_vsi *vsi = NULL;
319
320         /* Need to protect the allocation of the VSIs at the PF level */
321         mutex_lock(&pf->sw_mutex);
322
323         /* If we have already allocated our maximum number of VSIs,
324          * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
325          * is available to be populated
326          */
327         if (pf->next_vsi == ICE_NO_VSI) {
328                 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
329                 goto unlock_pf;
330         }
331
332         vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
333         if (!vsi)
334                 goto unlock_pf;
335
336         vsi->type = type;
337         vsi->back = pf;
338         set_bit(__ICE_DOWN, vsi->state);
339
340         vsi->idx = pf->next_vsi;
341
342         if (type == ICE_VSI_VF)
343                 ice_vsi_set_num_qs(vsi, vf_id);
344         else
345                 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
346
347         switch (vsi->type) {
348         case ICE_VSI_PF:
349                 if (ice_vsi_alloc_arrays(vsi))
350                         goto err_rings;
351
352                 /* Setup default MSIX irq handler for VSI */
353                 vsi->irq_handler = ice_msix_clean_rings;
354                 break;
355         case ICE_VSI_VF:
356                 if (ice_vsi_alloc_arrays(vsi))
357                         goto err_rings;
358                 break;
359         case ICE_VSI_LB:
360                 if (ice_vsi_alloc_arrays(vsi))
361                         goto err_rings;
362                 break;
363         default:
364                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
365                 goto unlock_pf;
366         }
367
368         /* fill VSI slot in the PF struct */
369         pf->vsi[pf->next_vsi] = vsi;
370
371         /* prepare pf->next_vsi for next use */
372         pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
373                                          pf->next_vsi);
374         goto unlock_pf;
375
376 err_rings:
377         devm_kfree(&pf->pdev->dev, vsi);
378         vsi = NULL;
379 unlock_pf:
380         mutex_unlock(&pf->sw_mutex);
381         return vsi;
382 }
383
384 /**
385  * ice_vsi_get_qs - Assign queues from PF to VSI
386  * @vsi: the VSI to assign queues to
387  *
388  * Returns 0 on success and a negative value on error
389  */
390 static int ice_vsi_get_qs(struct ice_vsi *vsi)
391 {
392         struct ice_pf *pf = vsi->back;
393         struct ice_qs_cfg tx_qs_cfg = {
394                 .qs_mutex = &pf->avail_q_mutex,
395                 .pf_map = pf->avail_txqs,
396                 .pf_map_size = pf->max_pf_txqs,
397                 .q_count = vsi->alloc_txq,
398                 .scatter_count = ICE_MAX_SCATTER_TXQS,
399                 .vsi_map = vsi->txq_map,
400                 .vsi_map_offset = 0,
401                 .mapping_mode = vsi->tx_mapping_mode
402         };
403         struct ice_qs_cfg rx_qs_cfg = {
404                 .qs_mutex = &pf->avail_q_mutex,
405                 .pf_map = pf->avail_rxqs,
406                 .pf_map_size = pf->max_pf_rxqs,
407                 .q_count = vsi->alloc_rxq,
408                 .scatter_count = ICE_MAX_SCATTER_RXQS,
409                 .vsi_map = vsi->rxq_map,
410                 .vsi_map_offset = 0,
411                 .mapping_mode = vsi->rx_mapping_mode
412         };
413         int ret = 0;
414
415         vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
416         vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
417
418         ret = __ice_vsi_get_qs(&tx_qs_cfg);
419         if (!ret)
420                 ret = __ice_vsi_get_qs(&rx_qs_cfg);
421
422         return ret;
423 }
424
425 /**
426  * ice_vsi_put_qs - Release queues from VSI to PF
427  * @vsi: the VSI that is going to release queues
428  */
429 void ice_vsi_put_qs(struct ice_vsi *vsi)
430 {
431         struct ice_pf *pf = vsi->back;
432         int i;
433
434         mutex_lock(&pf->avail_q_mutex);
435
436         for (i = 0; i < vsi->alloc_txq; i++) {
437                 clear_bit(vsi->txq_map[i], pf->avail_txqs);
438                 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
439         }
440
441         for (i = 0; i < vsi->alloc_rxq; i++) {
442                 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
443                 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
444         }
445
446         mutex_unlock(&pf->avail_q_mutex);
447 }
448
449 /**
450  * ice_is_safe_mode
451  * @pf: pointer to the PF struct
452  *
453  * returns true if driver is in safe mode, false otherwise
454  */
455 bool ice_is_safe_mode(struct ice_pf *pf)
456 {
457         return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
458 }
459
460 /**
461  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
462  * @vsi: the VSI being removed
463  */
464 static void ice_rss_clean(struct ice_vsi *vsi)
465 {
466         struct ice_pf *pf;
467
468         pf = vsi->back;
469
470         if (vsi->rss_hkey_user)
471                 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
472         if (vsi->rss_lut_user)
473                 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
474 }
475
476 /**
477  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
478  * @vsi: the VSI being configured
479  */
480 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
481 {
482         struct ice_hw_common_caps *cap;
483         struct ice_pf *pf = vsi->back;
484
485         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
486                 vsi->rss_size = 1;
487                 return;
488         }
489
490         cap = &pf->hw.func_caps.common_cap;
491         switch (vsi->type) {
492         case ICE_VSI_PF:
493                 /* PF VSI will inherit RSS instance of PF */
494                 vsi->rss_table_size = cap->rss_table_size;
495                 vsi->rss_size = min_t(int, num_online_cpus(),
496                                       BIT(cap->rss_table_entry_width));
497                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
498                 break;
499         case ICE_VSI_VF:
500                 /* VF VSI will gets a small RSS table
501                  * For VSI_LUT, LUT size should be set to 64 bytes
502                  */
503                 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
504                 vsi->rss_size = min_t(int, num_online_cpus(),
505                                       BIT(cap->rss_table_entry_width));
506                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
507                 break;
508         case ICE_VSI_LB:
509                 break;
510         default:
511                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n",
512                          vsi->type);
513                 break;
514         }
515 }
516
517 /**
518  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
519  * @ctxt: the VSI context being set
520  *
521  * This initializes a default VSI context for all sections except the Queues.
522  */
523 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
524 {
525         u32 table = 0;
526
527         memset(&ctxt->info, 0, sizeof(ctxt->info));
528         /* VSI's should be allocated from shared pool */
529         ctxt->alloc_from_pool = true;
530         /* Src pruning enabled by default */
531         ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
532         /* Traffic from VSI can be sent to LAN */
533         ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
534         /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
535          * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
536          * packets untagged/tagged.
537          */
538         ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
539                                   ICE_AQ_VSI_VLAN_MODE_M) >>
540                                  ICE_AQ_VSI_VLAN_MODE_S);
541         /* Have 1:1 UP mapping for both ingress/egress tables */
542         table |= ICE_UP_TABLE_TRANSLATE(0, 0);
543         table |= ICE_UP_TABLE_TRANSLATE(1, 1);
544         table |= ICE_UP_TABLE_TRANSLATE(2, 2);
545         table |= ICE_UP_TABLE_TRANSLATE(3, 3);
546         table |= ICE_UP_TABLE_TRANSLATE(4, 4);
547         table |= ICE_UP_TABLE_TRANSLATE(5, 5);
548         table |= ICE_UP_TABLE_TRANSLATE(6, 6);
549         table |= ICE_UP_TABLE_TRANSLATE(7, 7);
550         ctxt->info.ingress_table = cpu_to_le32(table);
551         ctxt->info.egress_table = cpu_to_le32(table);
552         /* Have 1:1 UP mapping for outer to inner UP table */
553         ctxt->info.outer_up_table = cpu_to_le32(table);
554         /* No Outer tag support outer_tag_flags remains to zero */
555 }
556
557 /**
558  * ice_vsi_setup_q_map - Setup a VSI queue map
559  * @vsi: the VSI being configured
560  * @ctxt: VSI context structure
561  */
562 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
563 {
564         u16 offset = 0, qmap = 0, tx_count = 0;
565         u16 qcount_tx = vsi->alloc_txq;
566         u16 qcount_rx = vsi->alloc_rxq;
567         u16 tx_numq_tc, rx_numq_tc;
568         u16 pow = 0, max_rss = 0;
569         bool ena_tc0 = false;
570         u8 netdev_tc = 0;
571         int i;
572
573         /* at least TC0 should be enabled by default */
574         if (vsi->tc_cfg.numtc) {
575                 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
576                         ena_tc0 = true;
577         } else {
578                 ena_tc0 = true;
579         }
580
581         if (ena_tc0) {
582                 vsi->tc_cfg.numtc++;
583                 vsi->tc_cfg.ena_tc |= 1;
584         }
585
586         rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
587         if (!rx_numq_tc)
588                 rx_numq_tc = 1;
589         tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
590         if (!tx_numq_tc)
591                 tx_numq_tc = 1;
592
593         /* TC mapping is a function of the number of Rx queues assigned to the
594          * VSI for each traffic class and the offset of these queues.
595          * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
596          * queues allocated to TC0. No:of queues is a power-of-2.
597          *
598          * If TC is not enabled, the queue offset is set to 0, and allocate one
599          * queue, this way, traffic for the given TC will be sent to the default
600          * queue.
601          *
602          * Setup number and offset of Rx queues for all TCs for the VSI
603          */
604
605         qcount_rx = rx_numq_tc;
606
607         /* qcount will change if RSS is enabled */
608         if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
609                 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
610                         if (vsi->type == ICE_VSI_PF)
611                                 max_rss = ICE_MAX_LG_RSS_QS;
612                         else
613                                 max_rss = ICE_MAX_SMALL_RSS_QS;
614                         qcount_rx = min_t(int, rx_numq_tc, max_rss);
615                         qcount_rx = min_t(int, qcount_rx, vsi->rss_size);
616                 }
617         }
618
619         /* find the (rounded up) power-of-2 of qcount */
620         pow = order_base_2(qcount_rx);
621
622         ice_for_each_traffic_class(i) {
623                 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
624                         /* TC is not enabled */
625                         vsi->tc_cfg.tc_info[i].qoffset = 0;
626                         vsi->tc_cfg.tc_info[i].qcount_rx = 1;
627                         vsi->tc_cfg.tc_info[i].qcount_tx = 1;
628                         vsi->tc_cfg.tc_info[i].netdev_tc = 0;
629                         ctxt->info.tc_mapping[i] = 0;
630                         continue;
631                 }
632
633                 /* TC is enabled */
634                 vsi->tc_cfg.tc_info[i].qoffset = offset;
635                 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
636                 vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
637                 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
638
639                 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
640                         ICE_AQ_VSI_TC_Q_OFFSET_M) |
641                         ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
642                          ICE_AQ_VSI_TC_Q_NUM_M);
643                 offset += qcount_rx;
644                 tx_count += tx_numq_tc;
645                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
646         }
647
648         /* if offset is non-zero, means it is calculated correctly based on
649          * enabled TCs for a given VSI otherwise qcount_rx will always
650          * be correct and non-zero because it is based off - VSI's
651          * allocated Rx queues which is at least 1 (hence qcount_tx will be
652          * at least 1)
653          */
654         if (offset)
655                 vsi->num_rxq = offset;
656         else
657                 vsi->num_rxq = qcount_rx;
658
659         vsi->num_txq = tx_count;
660
661         if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
662                 dev_dbg(&vsi->back->pdev->dev, "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
663                 /* since there is a chance that num_rxq could have been changed
664                  * in the above for loop, make num_txq equal to num_rxq.
665                  */
666                 vsi->num_txq = vsi->num_rxq;
667         }
668
669         /* Rx queue mapping */
670         ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
671         /* q_mapping buffer holds the info for the first queue allocated for
672          * this VSI in the PF space and also the number of queues associated
673          * with this VSI.
674          */
675         ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
676         ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
677 }
678
679 /**
680  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
681  * @ctxt: the VSI context being set
682  * @vsi: the VSI being configured
683  */
684 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
685 {
686         u8 lut_type, hash_type;
687         struct ice_pf *pf;
688
689         pf = vsi->back;
690
691         switch (vsi->type) {
692         case ICE_VSI_PF:
693                 /* PF VSI will inherit RSS instance of PF */
694                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
695                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
696                 break;
697         case ICE_VSI_VF:
698                 /* VF VSI will gets a small RSS table which is a VSI LUT type */
699                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
700                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
701                 break;
702         case ICE_VSI_LB:
703                 dev_dbg(&pf->pdev->dev, "Unsupported VSI type %d\n", vsi->type);
704                 return;
705         default:
706                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
707                 return;
708         }
709
710         ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
711                                 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
712                                 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
713                                  ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
714 }
715
716 /**
717  * ice_vsi_init - Create and initialize a VSI
718  * @vsi: the VSI being configured
719  *
720  * This initializes a VSI context depending on the VSI type to be added and
721  * passes it down to the add_vsi aq command to create a new VSI.
722  */
723 static int ice_vsi_init(struct ice_vsi *vsi)
724 {
725         struct ice_pf *pf = vsi->back;
726         struct ice_hw *hw = &pf->hw;
727         struct ice_vsi_ctx *ctxt;
728         int ret = 0;
729
730         ctxt = devm_kzalloc(&pf->pdev->dev, sizeof(*ctxt), GFP_KERNEL);
731         if (!ctxt)
732                 return -ENOMEM;
733
734         ctxt->info = vsi->info;
735         switch (vsi->type) {
736         case ICE_VSI_LB:
737                 /* fall through */
738         case ICE_VSI_PF:
739                 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
740                 break;
741         case ICE_VSI_VF:
742                 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
743                 /* VF number here is the absolute VF number (0-255) */
744                 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
745                 break;
746         default:
747                 return -ENODEV;
748         }
749
750         ice_set_dflt_vsi_ctx(ctxt);
751         /* if the switch is in VEB mode, allow VSI loopback */
752         if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
753                 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
754
755         /* Set LUT type and HASH type if RSS is enabled */
756         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
757                 ice_set_rss_vsi_ctx(ctxt, vsi);
758
759         ctxt->info.sw_id = vsi->port_info->sw_id;
760         ice_vsi_setup_q_map(vsi, ctxt);
761
762         /* Enable MAC Antispoof with new VSI being initialized or updated */
763         if (vsi->type == ICE_VSI_VF && pf->vf[vsi->vf_id].spoofchk) {
764                 ctxt->info.valid_sections |=
765                         cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
766                 ctxt->info.sec_flags |=
767                         ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
768         }
769
770         /* Allow control frames out of main VSI */
771         if (vsi->type == ICE_VSI_PF) {
772                 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
773                 ctxt->info.valid_sections |=
774                         cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
775         }
776
777         ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
778         if (ret) {
779                 dev_err(&pf->pdev->dev,
780                         "Add VSI failed, err %d\n", ret);
781                 return -EIO;
782         }
783
784         /* keep context for update VSI operations */
785         vsi->info = ctxt->info;
786
787         /* record VSI number returned */
788         vsi->vsi_num = ctxt->vsi_num;
789
790         devm_kfree(&pf->pdev->dev, ctxt);
791         return ret;
792 }
793
794 /**
795  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
796  * @vsi: ptr to the VSI
797  *
798  * This should only be called after ice_vsi_alloc() which allocates the
799  * corresponding SW VSI structure and initializes num_queue_pairs for the
800  * newly allocated VSI.
801  *
802  * Returns 0 on success or negative on failure
803  */
804 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
805 {
806         struct ice_pf *pf = vsi->back;
807         u16 num_q_vectors;
808
809         /* SRIOV doesn't grab irq_tracker entries for each VSI */
810         if (vsi->type == ICE_VSI_VF)
811                 return 0;
812
813         if (vsi->base_vector) {
814                 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
815                         vsi->vsi_num, vsi->base_vector);
816                 return -EEXIST;
817         }
818
819         num_q_vectors = vsi->num_q_vectors;
820         /* reserve slots from OS requested IRQs */
821         vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
822                                        vsi->idx);
823         if (vsi->base_vector < 0) {
824                 dev_err(&pf->pdev->dev,
825                         "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
826                         num_q_vectors, vsi->vsi_num, vsi->base_vector);
827                 return -ENOENT;
828         }
829         pf->num_avail_sw_msix -= num_q_vectors;
830
831         return 0;
832 }
833
834 /**
835  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
836  * @vsi: the VSI having rings deallocated
837  */
838 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
839 {
840         int i;
841
842         if (vsi->tx_rings) {
843                 for (i = 0; i < vsi->alloc_txq; i++) {
844                         if (vsi->tx_rings[i]) {
845                                 kfree_rcu(vsi->tx_rings[i], rcu);
846                                 vsi->tx_rings[i] = NULL;
847                         }
848                 }
849         }
850         if (vsi->rx_rings) {
851                 for (i = 0; i < vsi->alloc_rxq; i++) {
852                         if (vsi->rx_rings[i]) {
853                                 kfree_rcu(vsi->rx_rings[i], rcu);
854                                 vsi->rx_rings[i] = NULL;
855                         }
856                 }
857         }
858 }
859
860 /**
861  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
862  * @vsi: VSI which is having rings allocated
863  */
864 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
865 {
866         struct ice_pf *pf = vsi->back;
867         int i;
868
869         /* Allocate Tx rings */
870         for (i = 0; i < vsi->alloc_txq; i++) {
871                 struct ice_ring *ring;
872
873                 /* allocate with kzalloc(), free with kfree_rcu() */
874                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
875
876                 if (!ring)
877                         goto err_out;
878
879                 ring->q_index = i;
880                 ring->reg_idx = vsi->txq_map[i];
881                 ring->ring_active = false;
882                 ring->vsi = vsi;
883                 ring->dev = &pf->pdev->dev;
884                 ring->count = vsi->num_tx_desc;
885                 vsi->tx_rings[i] = ring;
886         }
887
888         /* Allocate Rx rings */
889         for (i = 0; i < vsi->alloc_rxq; i++) {
890                 struct ice_ring *ring;
891
892                 /* allocate with kzalloc(), free with kfree_rcu() */
893                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
894                 if (!ring)
895                         goto err_out;
896
897                 ring->q_index = i;
898                 ring->reg_idx = vsi->rxq_map[i];
899                 ring->ring_active = false;
900                 ring->vsi = vsi;
901                 ring->netdev = vsi->netdev;
902                 ring->dev = &pf->pdev->dev;
903                 ring->count = vsi->num_rx_desc;
904                 vsi->rx_rings[i] = ring;
905         }
906
907         return 0;
908
909 err_out:
910         ice_vsi_clear_rings(vsi);
911         return -ENOMEM;
912 }
913
914 /**
915  * ice_vsi_manage_rss_lut - disable/enable RSS
916  * @vsi: the VSI being changed
917  * @ena: boolean value indicating if this is an enable or disable request
918  *
919  * In the event of disable request for RSS, this function will zero out RSS
920  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
921  * LUT.
922  */
923 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
924 {
925         int err = 0;
926         u8 *lut;
927
928         lut = devm_kzalloc(&vsi->back->pdev->dev, vsi->rss_table_size,
929                            GFP_KERNEL);
930         if (!lut)
931                 return -ENOMEM;
932
933         if (ena) {
934                 if (vsi->rss_lut_user)
935                         memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
936                 else
937                         ice_fill_rss_lut(lut, vsi->rss_table_size,
938                                          vsi->rss_size);
939         }
940
941         err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
942         devm_kfree(&vsi->back->pdev->dev, lut);
943         return err;
944 }
945
946 /**
947  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
948  * @vsi: VSI to be configured
949  */
950 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
951 {
952         struct ice_aqc_get_set_rss_keys *key;
953         struct ice_pf *pf = vsi->back;
954         enum ice_status status;
955         int err = 0;
956         u8 *lut;
957
958         vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
959
960         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
961         if (!lut)
962                 return -ENOMEM;
963
964         if (vsi->rss_lut_user)
965                 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
966         else
967                 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
968
969         status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
970                                     vsi->rss_table_size);
971
972         if (status) {
973                 dev_err(&pf->pdev->dev,
974                         "set_rss_lut failed, error %d\n", status);
975                 err = -EIO;
976                 goto ice_vsi_cfg_rss_exit;
977         }
978
979         key = devm_kzalloc(&pf->pdev->dev, sizeof(*key), GFP_KERNEL);
980         if (!key) {
981                 err = -ENOMEM;
982                 goto ice_vsi_cfg_rss_exit;
983         }
984
985         if (vsi->rss_hkey_user)
986                 memcpy(key,
987                        (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
988                        ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
989         else
990                 netdev_rss_key_fill((void *)key,
991                                     ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
992
993         status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
994
995         if (status) {
996                 dev_err(&pf->pdev->dev, "set_rss_key failed, error %d\n",
997                         status);
998                 err = -EIO;
999         }
1000
1001         devm_kfree(&pf->pdev->dev, key);
1002 ice_vsi_cfg_rss_exit:
1003         devm_kfree(&pf->pdev->dev, lut);
1004         return err;
1005 }
1006
1007 /**
1008  * ice_add_mac_to_list - Add a MAC address filter entry to the list
1009  * @vsi: the VSI to be forwarded to
1010  * @add_list: pointer to the list which contains MAC filter entries
1011  * @macaddr: the MAC address to be added.
1012  *
1013  * Adds MAC address filter entry to the temp list
1014  *
1015  * Returns 0 on success or ENOMEM on failure.
1016  */
1017 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1018                         const u8 *macaddr)
1019 {
1020         struct ice_fltr_list_entry *tmp;
1021         struct ice_pf *pf = vsi->back;
1022
1023         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
1024         if (!tmp)
1025                 return -ENOMEM;
1026
1027         tmp->fltr_info.flag = ICE_FLTR_TX;
1028         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1029         tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1030         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1031         tmp->fltr_info.vsi_handle = vsi->idx;
1032         ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1033
1034         INIT_LIST_HEAD(&tmp->list_entry);
1035         list_add(&tmp->list_entry, add_list);
1036
1037         return 0;
1038 }
1039
1040 /**
1041  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1042  * @vsi: the VSI to be updated
1043  */
1044 void ice_update_eth_stats(struct ice_vsi *vsi)
1045 {
1046         struct ice_eth_stats *prev_es, *cur_es;
1047         struct ice_hw *hw = &vsi->back->hw;
1048         u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1049
1050         prev_es = &vsi->eth_stats_prev;
1051         cur_es = &vsi->eth_stats;
1052
1053         ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1054                           &prev_es->rx_bytes, &cur_es->rx_bytes);
1055
1056         ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1057                           &prev_es->rx_unicast, &cur_es->rx_unicast);
1058
1059         ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1060                           &prev_es->rx_multicast, &cur_es->rx_multicast);
1061
1062         ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1063                           &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1064
1065         ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1066                           &prev_es->rx_discards, &cur_es->rx_discards);
1067
1068         ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1069                           &prev_es->tx_bytes, &cur_es->tx_bytes);
1070
1071         ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1072                           &prev_es->tx_unicast, &cur_es->tx_unicast);
1073
1074         ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1075                           &prev_es->tx_multicast, &cur_es->tx_multicast);
1076
1077         ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1078                           &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1079
1080         ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1081                           &prev_es->tx_errors, &cur_es->tx_errors);
1082
1083         vsi->stat_offsets_loaded = true;
1084 }
1085
1086 /**
1087  * ice_free_fltr_list - free filter lists helper
1088  * @dev: pointer to the device struct
1089  * @h: pointer to the list head to be freed
1090  *
1091  * Helper function to free filter lists previously created using
1092  * ice_add_mac_to_list
1093  */
1094 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1095 {
1096         struct ice_fltr_list_entry *e, *tmp;
1097
1098         list_for_each_entry_safe(e, tmp, h, list_entry) {
1099                 list_del(&e->list_entry);
1100                 devm_kfree(dev, e);
1101         }
1102 }
1103
1104 /**
1105  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1106  * @vsi: the VSI being configured
1107  * @vid: VLAN ID to be added
1108  */
1109 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1110 {
1111         struct ice_fltr_list_entry *tmp;
1112         struct ice_pf *pf = vsi->back;
1113         LIST_HEAD(tmp_add_list);
1114         enum ice_status status;
1115         int err = 0;
1116
1117         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
1118         if (!tmp)
1119                 return -ENOMEM;
1120
1121         tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1122         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1123         tmp->fltr_info.flag = ICE_FLTR_TX;
1124         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1125         tmp->fltr_info.vsi_handle = vsi->idx;
1126         tmp->fltr_info.l_data.vlan.vlan_id = vid;
1127
1128         INIT_LIST_HEAD(&tmp->list_entry);
1129         list_add(&tmp->list_entry, &tmp_add_list);
1130
1131         status = ice_add_vlan(&pf->hw, &tmp_add_list);
1132         if (status) {
1133                 err = -ENODEV;
1134                 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
1135                         vid, vsi->vsi_num);
1136         }
1137
1138         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1139         return err;
1140 }
1141
1142 /**
1143  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1144  * @vsi: the VSI being configured
1145  * @vid: VLAN ID to be removed
1146  *
1147  * Returns 0 on success and negative on failure
1148  */
1149 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1150 {
1151         struct ice_fltr_list_entry *list;
1152         struct ice_pf *pf = vsi->back;
1153         LIST_HEAD(tmp_add_list);
1154         enum ice_status status;
1155         int err = 0;
1156
1157         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1158         if (!list)
1159                 return -ENOMEM;
1160
1161         list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1162         list->fltr_info.vsi_handle = vsi->idx;
1163         list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1164         list->fltr_info.l_data.vlan.vlan_id = vid;
1165         list->fltr_info.flag = ICE_FLTR_TX;
1166         list->fltr_info.src_id = ICE_SRC_ID_VSI;
1167
1168         INIT_LIST_HEAD(&list->list_entry);
1169         list_add(&list->list_entry, &tmp_add_list);
1170
1171         status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1172         if (status == ICE_ERR_DOES_NOT_EXIST) {
1173                 dev_dbg(&pf->pdev->dev,
1174                         "Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1175                         vid, vsi->vsi_num, status);
1176         } else if (status) {
1177                 dev_err(&pf->pdev->dev,
1178                         "Error removing VLAN %d on vsi %i error: %d\n",
1179                         vid, vsi->vsi_num, status);
1180                 err = -EIO;
1181         }
1182
1183         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1184         return err;
1185 }
1186
1187 /**
1188  * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1189  * @vsi: VSI
1190  */
1191 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1192 {
1193         if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1194                 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1195                 vsi->rx_buf_len = ICE_RXBUF_2048;
1196 #if (PAGE_SIZE < 8192)
1197         } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1198                    (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1199                 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1200                 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1201 #endif
1202         } else {
1203                 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1204 #if (PAGE_SIZE < 8192)
1205                 vsi->rx_buf_len = ICE_RXBUF_3072;
1206 #else
1207                 vsi->rx_buf_len = ICE_RXBUF_2048;
1208 #endif
1209         }
1210 }
1211
1212 /**
1213  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1214  * @vsi: the VSI being configured
1215  *
1216  * Return 0 on success and a negative value on error
1217  * Configure the Rx VSI for operation.
1218  */
1219 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1220 {
1221         u16 i;
1222
1223         if (vsi->type == ICE_VSI_VF)
1224                 goto setup_rings;
1225
1226         ice_vsi_cfg_frame_size(vsi);
1227 setup_rings:
1228         /* set up individual rings */
1229         for (i = 0; i < vsi->num_rxq; i++) {
1230                 int err;
1231
1232                 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1233                 if (err) {
1234                         dev_err(&vsi->back->pdev->dev,
1235                                 "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1236                                 i, err);
1237                         return err;
1238                 }
1239         }
1240
1241         return 0;
1242 }
1243
1244 /**
1245  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1246  * @vsi: the VSI being configured
1247  * @rings: Tx ring array to be configured
1248  *
1249  * Return 0 on success and a negative value on error
1250  * Configure the Tx VSI for operation.
1251  */
1252 static int
1253 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1254 {
1255         struct ice_aqc_add_tx_qgrp *qg_buf;
1256         u16 q_idx = 0;
1257         int err = 0;
1258
1259         qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1260         if (!qg_buf)
1261                 return -ENOMEM;
1262
1263         qg_buf->num_txqs = 1;
1264
1265         for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1266                 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1267                 if (err)
1268                         goto err_cfg_txqs;
1269         }
1270
1271 err_cfg_txqs:
1272         kfree(qg_buf);
1273         return err;
1274 }
1275
1276 /**
1277  * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1278  * @vsi: the VSI being configured
1279  *
1280  * Return 0 on success and a negative value on error
1281  * Configure the Tx VSI for operation.
1282  */
1283 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1284 {
1285         return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1286 }
1287
1288 /**
1289  * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1290  * @vsi: the VSI being configured
1291  *
1292  * Return 0 on success and a negative value on error
1293  * Configure the Tx queues dedicated for XDP in given VSI for operation.
1294  */
1295 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1296 {
1297         int ret;
1298         int i;
1299
1300         ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1301         if (ret)
1302                 return ret;
1303
1304         for (i = 0; i < vsi->num_xdp_txq; i++)
1305                 vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1306
1307         return ret;
1308 }
1309
1310 /**
1311  * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1312  * @intrl: interrupt rate limit in usecs
1313  * @gran: interrupt rate limit granularity in usecs
1314  *
1315  * This function converts a decimal interrupt rate limit in usecs to the format
1316  * expected by firmware.
1317  */
1318 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1319 {
1320         u32 val = intrl / gran;
1321
1322         if (val)
1323                 return val | GLINT_RATE_INTRL_ENA_M;
1324         return 0;
1325 }
1326
1327 /**
1328  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1329  * @vsi: the VSI being configured
1330  *
1331  * This configures MSIX mode interrupts for the PF VSI, and should not be used
1332  * for the VF VSI.
1333  */
1334 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1335 {
1336         struct ice_pf *pf = vsi->back;
1337         struct ice_hw *hw = &pf->hw;
1338         u32 txq = 0, rxq = 0;
1339         int i, q;
1340
1341         for (i = 0; i < vsi->num_q_vectors; i++) {
1342                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1343                 u16 reg_idx = q_vector->reg_idx;
1344
1345                 ice_cfg_itr(hw, q_vector);
1346
1347                 wr32(hw, GLINT_RATE(reg_idx),
1348                      ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1349
1350                 /* Both Transmit Queue Interrupt Cause Control register
1351                  * and Receive Queue Interrupt Cause control register
1352                  * expects MSIX_INDX field to be the vector index
1353                  * within the function space and not the absolute
1354                  * vector index across PF or across device.
1355                  * For SR-IOV VF VSIs queue vector index always starts
1356                  * with 1 since first vector index(0) is used for OICR
1357                  * in VF space. Since VMDq and other PF VSIs are within
1358                  * the PF function space, use the vector index that is
1359                  * tracked for this PF.
1360                  */
1361                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1362                         ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1363                                               q_vector->tx.itr_idx);
1364                         txq++;
1365                 }
1366
1367                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1368                         ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1369                                               q_vector->rx.itr_idx);
1370                         rxq++;
1371                 }
1372         }
1373 }
1374
1375 /**
1376  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1377  * @vsi: the VSI being changed
1378  */
1379 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1380 {
1381         struct device *dev = &vsi->back->pdev->dev;
1382         struct ice_hw *hw = &vsi->back->hw;
1383         struct ice_vsi_ctx *ctxt;
1384         enum ice_status status;
1385         int ret = 0;
1386
1387         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
1388         if (!ctxt)
1389                 return -ENOMEM;
1390
1391         /* Here we are configuring the VSI to let the driver add VLAN tags by
1392          * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1393          * insertion happens in the Tx hot path, in ice_tx_map.
1394          */
1395         ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1396
1397         /* Preserve existing VLAN strip setting */
1398         ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1399                                   ICE_AQ_VSI_VLAN_EMOD_M);
1400
1401         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1402
1403         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1404         if (status) {
1405                 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1406                         status, hw->adminq.sq_last_status);
1407                 ret = -EIO;
1408                 goto out;
1409         }
1410
1411         vsi->info.vlan_flags = ctxt->info.vlan_flags;
1412 out:
1413         devm_kfree(dev, ctxt);
1414         return ret;
1415 }
1416
1417 /**
1418  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1419  * @vsi: the VSI being changed
1420  * @ena: boolean value indicating if this is a enable or disable request
1421  */
1422 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1423 {
1424         struct device *dev = &vsi->back->pdev->dev;
1425         struct ice_hw *hw = &vsi->back->hw;
1426         struct ice_vsi_ctx *ctxt;
1427         enum ice_status status;
1428         int ret = 0;
1429
1430         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
1431         if (!ctxt)
1432                 return -ENOMEM;
1433
1434         /* Here we are configuring what the VSI should do with the VLAN tag in
1435          * the Rx packet. We can either leave the tag in the packet or put it in
1436          * the Rx descriptor.
1437          */
1438         if (ena)
1439                 /* Strip VLAN tag from Rx packet and put it in the desc */
1440                 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1441         else
1442                 /* Disable stripping. Leave tag in packet */
1443                 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1444
1445         /* Allow all packets untagged/tagged */
1446         ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1447
1448         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1449
1450         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1451         if (status) {
1452                 dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1453                         ena, status, hw->adminq.sq_last_status);
1454                 ret = -EIO;
1455                 goto out;
1456         }
1457
1458         vsi->info.vlan_flags = ctxt->info.vlan_flags;
1459 out:
1460         devm_kfree(dev, ctxt);
1461         return ret;
1462 }
1463
1464 /**
1465  * ice_vsi_start_rx_rings - start VSI's Rx rings
1466  * @vsi: the VSI whose rings are to be started
1467  *
1468  * Returns 0 on success and a negative value on error
1469  */
1470 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1471 {
1472         return ice_vsi_ctrl_rx_rings(vsi, true);
1473 }
1474
1475 /**
1476  * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1477  * @vsi: the VSI
1478  *
1479  * Returns 0 on success and a negative value on error
1480  */
1481 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1482 {
1483         return ice_vsi_ctrl_rx_rings(vsi, false);
1484 }
1485
1486 /**
1487  * ice_vsi_stop_tx_rings - Disable Tx rings
1488  * @vsi: the VSI being configured
1489  * @rst_src: reset source
1490  * @rel_vmvf_num: Relative ID of VF/VM
1491  * @rings: Tx ring array to be stopped
1492  */
1493 static int
1494 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1495                       u16 rel_vmvf_num, struct ice_ring **rings)
1496 {
1497         u16 q_idx;
1498
1499         if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1500                 return -EINVAL;
1501
1502         for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1503                 struct ice_txq_meta txq_meta = { };
1504                 int status;
1505
1506                 if (!rings || !rings[q_idx])
1507                         return -EINVAL;
1508
1509                 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1510                 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1511                                               rings[q_idx], &txq_meta);
1512
1513                 if (status)
1514                         return status;
1515         }
1516
1517         return 0;
1518 }
1519
1520 /**
1521  * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1522  * @vsi: the VSI being configured
1523  * @rst_src: reset source
1524  * @rel_vmvf_num: Relative ID of VF/VM
1525  */
1526 int
1527 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1528                           u16 rel_vmvf_num)
1529 {
1530         return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1531 }
1532
1533 /**
1534  * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1535  * @vsi: the VSI being configured
1536  */
1537 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1538 {
1539         return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1540 }
1541
1542 /**
1543  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1544  * @vsi: VSI to enable or disable VLAN pruning on
1545  * @ena: set to true to enable VLAN pruning and false to disable it
1546  * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1547  *
1548  * returns 0 if VSI is updated, negative otherwise
1549  */
1550 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1551 {
1552         struct ice_vsi_ctx *ctxt;
1553         struct device *dev;
1554         struct ice_pf *pf;
1555         int status;
1556
1557         if (!vsi)
1558                 return -EINVAL;
1559
1560         pf = vsi->back;
1561         dev = &pf->pdev->dev;
1562         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
1563         if (!ctxt)
1564                 return -ENOMEM;
1565
1566         ctxt->info = vsi->info;
1567
1568         if (ena) {
1569                 ctxt->info.sec_flags |=
1570                         ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1571                         ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
1572                 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1573         } else {
1574                 ctxt->info.sec_flags &=
1575                         ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1576                           ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1577                 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1578         }
1579
1580         if (!vlan_promisc)
1581                 ctxt->info.valid_sections =
1582                         cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
1583                                     ICE_AQ_VSI_PROP_SW_VALID);
1584
1585         status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1586         if (status) {
1587                 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1588                            ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
1589                            pf->hw.adminq.sq_last_status);
1590                 goto err_out;
1591         }
1592
1593         vsi->info.sec_flags = ctxt->info.sec_flags;
1594         vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1595
1596         devm_kfree(dev, ctxt);
1597         return 0;
1598
1599 err_out:
1600         devm_kfree(dev, ctxt);
1601         return -EIO;
1602 }
1603
1604 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
1605 {
1606         struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
1607
1608         vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
1609         vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
1610 }
1611
1612 /**
1613  * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
1614  * @vsi: VSI to set the q_vectors register index on
1615  */
1616 static int
1617 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
1618 {
1619         u16 i;
1620
1621         if (!vsi || !vsi->q_vectors)
1622                 return -EINVAL;
1623
1624         ice_for_each_q_vector(vsi, i) {
1625                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1626
1627                 if (!q_vector) {
1628                         dev_err(&vsi->back->pdev->dev,
1629                                 "Failed to set reg_idx on q_vector %d VSI %d\n",
1630                                 i, vsi->vsi_num);
1631                         goto clear_reg_idx;
1632                 }
1633
1634                 if (vsi->type == ICE_VSI_VF) {
1635                         struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
1636
1637                         q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
1638                 } else {
1639                         q_vector->reg_idx =
1640                                 q_vector->v_idx + vsi->base_vector;
1641                 }
1642         }
1643
1644         return 0;
1645
1646 clear_reg_idx:
1647         ice_for_each_q_vector(vsi, i) {
1648                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1649
1650                 if (q_vector)
1651                         q_vector->reg_idx = 0;
1652         }
1653
1654         return -EINVAL;
1655 }
1656
1657 /**
1658  * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
1659  * @vsi: the VSI being configured
1660  * @add_rule: boolean value to add or remove ethertype filter rule
1661  */
1662 static void
1663 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
1664 {
1665         struct ice_fltr_list_entry *list;
1666         struct ice_pf *pf = vsi->back;
1667         LIST_HEAD(tmp_add_list);
1668         enum ice_status status;
1669
1670         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1671         if (!list)
1672                 return;
1673
1674         list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1675         list->fltr_info.fltr_act = ICE_DROP_PACKET;
1676         list->fltr_info.flag = ICE_FLTR_TX;
1677         list->fltr_info.src_id = ICE_SRC_ID_VSI;
1678         list->fltr_info.vsi_handle = vsi->idx;
1679         list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
1680
1681         INIT_LIST_HEAD(&list->list_entry);
1682         list_add(&list->list_entry, &tmp_add_list);
1683
1684         if (add_rule)
1685                 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1686         else
1687                 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1688
1689         if (status)
1690                 dev_err(&pf->pdev->dev,
1691                         "Failure Adding or Removing Ethertype on VSI %i error: %d\n",
1692                         vsi->vsi_num, status);
1693
1694         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1695 }
1696
1697 /**
1698  * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
1699  * @vsi: the VSI being configured
1700  * @tx: bool to determine Tx or Rx rule
1701  * @create: bool to determine create or remove Rule
1702  */
1703 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
1704 {
1705         struct ice_fltr_list_entry *list;
1706         struct ice_pf *pf = vsi->back;
1707         LIST_HEAD(tmp_add_list);
1708         enum ice_status status;
1709
1710         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1711         if (!list)
1712                 return;
1713
1714         list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1715         list->fltr_info.vsi_handle = vsi->idx;
1716         list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
1717
1718         if (tx) {
1719                 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1720                 list->fltr_info.flag = ICE_FLTR_TX;
1721                 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1722         } else {
1723                 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1724                 list->fltr_info.flag = ICE_FLTR_RX;
1725                 list->fltr_info.src_id = ICE_SRC_ID_LPORT;
1726         }
1727
1728         INIT_LIST_HEAD(&list->list_entry);
1729         list_add(&list->list_entry, &tmp_add_list);
1730
1731         if (create)
1732                 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1733         else
1734                 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1735
1736         if (status)
1737                 dev_err(&pf->pdev->dev,
1738                         "Fail %s %s LLDP rule on VSI %i error: %d\n",
1739                         create ? "adding" : "removing", tx ? "TX" : "RX",
1740                         vsi->vsi_num, status);
1741
1742         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1743 }
1744
1745 /**
1746  * ice_vsi_setup - Set up a VSI by a given type
1747  * @pf: board private structure
1748  * @pi: pointer to the port_info instance
1749  * @type: VSI type
1750  * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1751  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
1752  *         fill-in ICE_INVAL_VFID as input.
1753  *
1754  * This allocates the sw VSI structure and its queue resources.
1755  *
1756  * Returns pointer to the successfully allocated and configured VSI sw struct on
1757  * success, NULL on failure.
1758  */
1759 struct ice_vsi *
1760 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1761               enum ice_vsi_type type, u16 vf_id)
1762 {
1763         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1764         struct device *dev = &pf->pdev->dev;
1765         enum ice_status status;
1766         struct ice_vsi *vsi;
1767         int ret, i;
1768
1769         if (type == ICE_VSI_VF)
1770                 vsi = ice_vsi_alloc(pf, type, vf_id);
1771         else
1772                 vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
1773
1774         if (!vsi) {
1775                 dev_err(dev, "could not allocate VSI\n");
1776                 return NULL;
1777         }
1778
1779         vsi->port_info = pi;
1780         vsi->vsw = pf->first_sw;
1781         if (vsi->type == ICE_VSI_PF)
1782                 vsi->ethtype = ETH_P_PAUSE;
1783
1784         if (vsi->type == ICE_VSI_VF)
1785                 vsi->vf_id = vf_id;
1786
1787         if (ice_vsi_get_qs(vsi)) {
1788                 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1789                         vsi->idx);
1790                 goto unroll_get_qs;
1791         }
1792
1793         /* set RSS capabilities */
1794         ice_vsi_set_rss_params(vsi);
1795
1796         /* set TC configuration */
1797         ice_vsi_set_tc_cfg(vsi);
1798
1799         /* create the VSI */
1800         ret = ice_vsi_init(vsi);
1801         if (ret)
1802                 goto unroll_get_qs;
1803
1804         switch (vsi->type) {
1805         case ICE_VSI_PF:
1806                 ret = ice_vsi_alloc_q_vectors(vsi);
1807                 if (ret)
1808                         goto unroll_vsi_init;
1809
1810                 ret = ice_vsi_setup_vector_base(vsi);
1811                 if (ret)
1812                         goto unroll_alloc_q_vector;
1813
1814                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1815                 if (ret)
1816                         goto unroll_vector_base;
1817
1818                 ret = ice_vsi_alloc_rings(vsi);
1819                 if (ret)
1820                         goto unroll_vector_base;
1821
1822                 ice_vsi_map_rings_to_vectors(vsi);
1823
1824                 /* Do not exit if configuring RSS had an issue, at least
1825                  * receive traffic on first queue. Hence no need to capture
1826                  * return value
1827                  */
1828                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1829                         ice_vsi_cfg_rss_lut_key(vsi);
1830                 break;
1831         case ICE_VSI_VF:
1832                 /* VF driver will take care of creating netdev for this type and
1833                  * map queues to vectors through Virtchnl, PF driver only
1834                  * creates a VSI and corresponding structures for bookkeeping
1835                  * purpose
1836                  */
1837                 ret = ice_vsi_alloc_q_vectors(vsi);
1838                 if (ret)
1839                         goto unroll_vsi_init;
1840
1841                 ret = ice_vsi_alloc_rings(vsi);
1842                 if (ret)
1843                         goto unroll_alloc_q_vector;
1844
1845                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1846                 if (ret)
1847                         goto unroll_vector_base;
1848
1849                 /* Do not exit if configuring RSS had an issue, at least
1850                  * receive traffic on first queue. Hence no need to capture
1851                  * return value
1852                  */
1853                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1854                         ice_vsi_cfg_rss_lut_key(vsi);
1855                 break;
1856         case ICE_VSI_LB:
1857                 ret = ice_vsi_alloc_rings(vsi);
1858                 if (ret)
1859                         goto unroll_vsi_init;
1860                 break;
1861         default:
1862                 /* clean up the resources and exit */
1863                 goto unroll_vsi_init;
1864         }
1865
1866         /* configure VSI nodes based on number of queues and TC's */
1867         for (i = 0; i < vsi->tc_cfg.numtc; i++)
1868                 max_txqs[i] = vsi->alloc_txq;
1869
1870         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1871                                  max_txqs);
1872         if (status) {
1873                 dev_err(&pf->pdev->dev,
1874                         "VSI %d failed lan queue config, error %d\n",
1875                         vsi->vsi_num, status);
1876                 goto unroll_vector_base;
1877         }
1878
1879         /* Add switch rule to drop all Tx Flow Control Frames, of look up
1880          * type ETHERTYPE from VSIs, and restrict malicious VF from sending
1881          * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
1882          * The rule is added once for PF VSI in order to create appropriate
1883          * recipe, since VSI/VSI list is ignored with drop action...
1884          * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
1885          * be dropped so that VFs cannot send LLDP packets to reconfig DCB
1886          * settings in the HW.
1887          */
1888         if (!ice_is_safe_mode(pf))
1889                 if (vsi->type == ICE_VSI_PF) {
1890                         ice_vsi_add_rem_eth_mac(vsi, true);
1891
1892                         /* Tx LLDP packets */
1893                         ice_cfg_sw_lldp(vsi, true, true);
1894                 }
1895
1896         return vsi;
1897
1898 unroll_vector_base:
1899         /* reclaim SW interrupts back to the common pool */
1900         ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
1901         pf->num_avail_sw_msix += vsi->num_q_vectors;
1902 unroll_alloc_q_vector:
1903         ice_vsi_free_q_vectors(vsi);
1904 unroll_vsi_init:
1905         ice_vsi_delete(vsi);
1906 unroll_get_qs:
1907         ice_vsi_put_qs(vsi);
1908         ice_vsi_clear(vsi);
1909
1910         return NULL;
1911 }
1912
1913 /**
1914  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1915  * @vsi: the VSI being cleaned up
1916  */
1917 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1918 {
1919         struct ice_pf *pf = vsi->back;
1920         struct ice_hw *hw = &pf->hw;
1921         u32 txq = 0;
1922         u32 rxq = 0;
1923         int i, q;
1924
1925         for (i = 0; i < vsi->num_q_vectors; i++) {
1926                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1927                 u16 reg_idx = q_vector->reg_idx;
1928
1929                 wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
1930                 wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
1931                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1932                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1933                         if (ice_is_xdp_ena_vsi(vsi)) {
1934                                 u32 xdp_txq = txq + vsi->num_xdp_txq;
1935
1936                                 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
1937                         }
1938                         txq++;
1939                 }
1940
1941                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1942                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1943                         rxq++;
1944                 }
1945         }
1946
1947         ice_flush(hw);
1948 }
1949
1950 /**
1951  * ice_vsi_free_irq - Free the IRQ association with the OS
1952  * @vsi: the VSI being configured
1953  */
1954 void ice_vsi_free_irq(struct ice_vsi *vsi)
1955 {
1956         struct ice_pf *pf = vsi->back;
1957         int base = vsi->base_vector;
1958         int i;
1959
1960         if (!vsi->q_vectors || !vsi->irqs_ready)
1961                 return;
1962
1963         ice_vsi_release_msix(vsi);
1964         if (vsi->type == ICE_VSI_VF)
1965                 return;
1966
1967         vsi->irqs_ready = false;
1968         ice_for_each_q_vector(vsi, i) {
1969                 u16 vector = i + base;
1970                 int irq_num;
1971
1972                 irq_num = pf->msix_entries[vector].vector;
1973
1974                 /* free only the irqs that were actually requested */
1975                 if (!vsi->q_vectors[i] ||
1976                     !(vsi->q_vectors[i]->num_ring_tx ||
1977                       vsi->q_vectors[i]->num_ring_rx))
1978                         continue;
1979
1980                 /* clear the affinity notifier in the IRQ descriptor */
1981                 irq_set_affinity_notifier(irq_num, NULL);
1982
1983                 /* clear the affinity_mask in the IRQ descriptor */
1984                 irq_set_affinity_hint(irq_num, NULL);
1985                 synchronize_irq(irq_num);
1986                 devm_free_irq(&pf->pdev->dev, irq_num,
1987                               vsi->q_vectors[i]);
1988         }
1989 }
1990
1991 /**
1992  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
1993  * @vsi: the VSI having resources freed
1994  */
1995 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
1996 {
1997         int i;
1998
1999         if (!vsi->tx_rings)
2000                 return;
2001
2002         ice_for_each_txq(vsi, i)
2003                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2004                         ice_free_tx_ring(vsi->tx_rings[i]);
2005 }
2006
2007 /**
2008  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2009  * @vsi: the VSI having resources freed
2010  */
2011 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2012 {
2013         int i;
2014
2015         if (!vsi->rx_rings)
2016                 return;
2017
2018         ice_for_each_rxq(vsi, i)
2019                 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2020                         ice_free_rx_ring(vsi->rx_rings[i]);
2021 }
2022
2023 /**
2024  * ice_vsi_close - Shut down a VSI
2025  * @vsi: the VSI being shut down
2026  */
2027 void ice_vsi_close(struct ice_vsi *vsi)
2028 {
2029         if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2030                 ice_down(vsi);
2031
2032         ice_vsi_free_irq(vsi);
2033         ice_vsi_free_tx_rings(vsi);
2034         ice_vsi_free_rx_rings(vsi);
2035 }
2036
2037 /**
2038  * ice_ena_vsi - resume a VSI
2039  * @vsi: the VSI being resume
2040  * @locked: is the rtnl_lock already held
2041  */
2042 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2043 {
2044         int err = 0;
2045
2046         if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2047                 return 0;
2048
2049         clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2050
2051         if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2052                 if (netif_running(vsi->netdev)) {
2053                         if (!locked)
2054                                 rtnl_lock();
2055
2056                         err = ice_open(vsi->netdev);
2057
2058                         if (!locked)
2059                                 rtnl_unlock();
2060                 }
2061         }
2062
2063         return err;
2064 }
2065
2066 /**
2067  * ice_dis_vsi - pause a VSI
2068  * @vsi: the VSI being paused
2069  * @locked: is the rtnl_lock already held
2070  */
2071 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2072 {
2073         if (test_bit(__ICE_DOWN, vsi->state))
2074                 return;
2075
2076         set_bit(__ICE_NEEDS_RESTART, vsi->state);
2077
2078         if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2079                 if (netif_running(vsi->netdev)) {
2080                         if (!locked)
2081                                 rtnl_lock();
2082
2083                         ice_stop(vsi->netdev);
2084
2085                         if (!locked)
2086                                 rtnl_unlock();
2087                 } else {
2088                         ice_vsi_close(vsi);
2089                 }
2090         }
2091 }
2092
2093 /**
2094  * ice_free_res - free a block of resources
2095  * @res: pointer to the resource
2096  * @index: starting index previously returned by ice_get_res
2097  * @id: identifier to track owner
2098  *
2099  * Returns number of resources freed
2100  */
2101 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2102 {
2103         int count = 0;
2104         int i;
2105
2106         if (!res || index >= res->end)
2107                 return -EINVAL;
2108
2109         id |= ICE_RES_VALID_BIT;
2110         for (i = index; i < res->end && res->list[i] == id; i++) {
2111                 res->list[i] = 0;
2112                 count++;
2113         }
2114
2115         return count;
2116 }
2117
2118 /**
2119  * ice_search_res - Search the tracker for a block of resources
2120  * @res: pointer to the resource
2121  * @needed: size of the block needed
2122  * @id: identifier to track owner
2123  *
2124  * Returns the base item index of the block, or -ENOMEM for error
2125  */
2126 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2127 {
2128         int start = 0, end = 0;
2129
2130         if (needed > res->end)
2131                 return -ENOMEM;
2132
2133         id |= ICE_RES_VALID_BIT;
2134
2135         do {
2136                 /* skip already allocated entries */
2137                 if (res->list[end++] & ICE_RES_VALID_BIT) {
2138                         start = end;
2139                         if ((start + needed) > res->end)
2140                                 break;
2141                 }
2142
2143                 if (end == (start + needed)) {
2144                         int i = start;
2145
2146                         /* there was enough, so assign it to the requestor */
2147                         while (i != end)
2148                                 res->list[i++] = id;
2149
2150                         return start;
2151                 }
2152         } while (end < res->end);
2153
2154         return -ENOMEM;
2155 }
2156
2157 /**
2158  * ice_get_res - get a block of resources
2159  * @pf: board private structure
2160  * @res: pointer to the resource
2161  * @needed: size of the block needed
2162  * @id: identifier to track owner
2163  *
2164  * Returns the base item index of the block, or negative for error
2165  */
2166 int
2167 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2168 {
2169         if (!res || !pf)
2170                 return -EINVAL;
2171
2172         if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2173                 dev_err(&pf->pdev->dev,
2174                         "param err: needed=%d, num_entries = %d id=0x%04x\n",
2175                         needed, res->num_entries, id);
2176                 return -EINVAL;
2177         }
2178
2179         return ice_search_res(res, needed, id);
2180 }
2181
2182 /**
2183  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2184  * @vsi: the VSI being un-configured
2185  */
2186 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2187 {
2188         int base = vsi->base_vector;
2189         struct ice_pf *pf = vsi->back;
2190         struct ice_hw *hw = &pf->hw;
2191         u32 val;
2192         int i;
2193
2194         /* disable interrupt causation from each queue */
2195         if (vsi->tx_rings) {
2196                 ice_for_each_txq(vsi, i) {
2197                         if (vsi->tx_rings[i]) {
2198                                 u16 reg;
2199
2200                                 reg = vsi->tx_rings[i]->reg_idx;
2201                                 val = rd32(hw, QINT_TQCTL(reg));
2202                                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2203                                 wr32(hw, QINT_TQCTL(reg), val);
2204                         }
2205                 }
2206         }
2207
2208         if (vsi->rx_rings) {
2209                 ice_for_each_rxq(vsi, i) {
2210                         if (vsi->rx_rings[i]) {
2211                                 u16 reg;
2212
2213                                 reg = vsi->rx_rings[i]->reg_idx;
2214                                 val = rd32(hw, QINT_RQCTL(reg));
2215                                 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2216                                 wr32(hw, QINT_RQCTL(reg), val);
2217                         }
2218                 }
2219         }
2220
2221         /* disable each interrupt */
2222         ice_for_each_q_vector(vsi, i) {
2223                 if (!vsi->q_vectors[i])
2224                         continue;
2225                 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2226         }
2227
2228         ice_flush(hw);
2229
2230         /* don't call synchronize_irq() for VF's from the host */
2231         if (vsi->type == ICE_VSI_VF)
2232                 return;
2233
2234         ice_for_each_q_vector(vsi, i)
2235                 synchronize_irq(pf->msix_entries[i + base].vector);
2236 }
2237
2238 /**
2239  * ice_napi_del - Remove NAPI handler for the VSI
2240  * @vsi: VSI for which NAPI handler is to be removed
2241  */
2242 void ice_napi_del(struct ice_vsi *vsi)
2243 {
2244         int v_idx;
2245
2246         if (!vsi->netdev)
2247                 return;
2248
2249         ice_for_each_q_vector(vsi, v_idx)
2250                 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2251 }
2252
2253 /**
2254  * ice_vsi_release - Delete a VSI and free its resources
2255  * @vsi: the VSI being removed
2256  *
2257  * Returns 0 on success or < 0 on error
2258  */
2259 int ice_vsi_release(struct ice_vsi *vsi)
2260 {
2261         struct ice_pf *pf;
2262
2263         if (!vsi->back)
2264                 return -ENODEV;
2265         pf = vsi->back;
2266
2267         /* do not unregister while driver is in the reset recovery pending
2268          * state. Since reset/rebuild happens through PF service task workqueue,
2269          * it's not a good idea to unregister netdev that is associated to the
2270          * PF that is running the work queue items currently. This is done to
2271          * avoid check_flush_dependency() warning on this wq
2272          */
2273         if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2274                 unregister_netdev(vsi->netdev);
2275
2276         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2277                 ice_rss_clean(vsi);
2278
2279         /* Disable VSI and free resources */
2280         if (vsi->type != ICE_VSI_LB)
2281                 ice_vsi_dis_irq(vsi);
2282         ice_vsi_close(vsi);
2283
2284         /* SR-IOV determines needed MSIX resources all at once instead of per
2285          * VSI since when VFs are spawned we know how many VFs there are and how
2286          * many interrupts each VF needs. SR-IOV MSIX resources are also
2287          * cleared in the same manner.
2288          */
2289         if (vsi->type != ICE_VSI_VF) {
2290                 /* reclaim SW interrupts back to the common pool */
2291                 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2292                 pf->num_avail_sw_msix += vsi->num_q_vectors;
2293         }
2294
2295         if (!ice_is_safe_mode(pf)) {
2296                 if (vsi->type == ICE_VSI_PF) {
2297                         ice_vsi_add_rem_eth_mac(vsi, false);
2298                         ice_cfg_sw_lldp(vsi, true, false);
2299                         /* The Rx rule will only exist to remove if the LLDP FW
2300                          * engine is currently stopped
2301                          */
2302                         if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2303                                 ice_cfg_sw_lldp(vsi, false, false);
2304                 }
2305         }
2306
2307         ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2308         ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2309         ice_vsi_delete(vsi);
2310         ice_vsi_free_q_vectors(vsi);
2311
2312         /* make sure unregister_netdev() was called by checking __ICE_DOWN */
2313         if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2314                 free_netdev(vsi->netdev);
2315                 vsi->netdev = NULL;
2316         }
2317
2318         ice_vsi_clear_rings(vsi);
2319
2320         ice_vsi_put_qs(vsi);
2321
2322         /* retain SW VSI data structure since it is needed to unregister and
2323          * free VSI netdev when PF is not in reset recovery pending state,\
2324          * for ex: during rmmod.
2325          */
2326         if (!ice_is_reset_in_progress(pf->state))
2327                 ice_vsi_clear(vsi);
2328
2329         return 0;
2330 }
2331
2332 /**
2333  * ice_vsi_rebuild - Rebuild VSI after reset
2334  * @vsi: VSI to be rebuild
2335  *
2336  * Returns 0 on success and negative value on failure
2337  */
2338 int ice_vsi_rebuild(struct ice_vsi *vsi)
2339 {
2340         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2341         struct ice_vf *vf = NULL;
2342         enum ice_status status;
2343         struct ice_pf *pf;
2344         int ret, i;
2345
2346         if (!vsi)
2347                 return -EINVAL;
2348
2349         pf = vsi->back;
2350         if (vsi->type == ICE_VSI_VF)
2351                 vf = &pf->vf[vsi->vf_id];
2352
2353         ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2354         ice_vsi_free_q_vectors(vsi);
2355
2356         /* SR-IOV determines needed MSIX resources all at once instead of per
2357          * VSI since when VFs are spawned we know how many VFs there are and how
2358          * many interrupts each VF needs. SR-IOV MSIX resources are also
2359          * cleared in the same manner.
2360          */
2361         if (vsi->type != ICE_VSI_VF) {
2362                 /* reclaim SW interrupts back to the common pool */
2363                 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2364                 pf->num_avail_sw_msix += vsi->num_q_vectors;
2365                 vsi->base_vector = 0;
2366         }
2367
2368         if (ice_is_xdp_ena_vsi(vsi))
2369                 /* return value check can be skipped here, it always returns
2370                  * 0 if reset is in progress
2371                  */
2372                 ice_destroy_xdp_rings(vsi);
2373         ice_vsi_put_qs(vsi);
2374         ice_vsi_clear_rings(vsi);
2375         ice_vsi_free_arrays(vsi);
2376         ice_dev_onetime_setup(&pf->hw);
2377         if (vsi->type == ICE_VSI_VF)
2378                 ice_vsi_set_num_qs(vsi, vf->vf_id);
2379         else
2380                 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2381
2382         ret = ice_vsi_alloc_arrays(vsi);
2383         if (ret < 0)
2384                 goto err_vsi;
2385
2386         ice_vsi_get_qs(vsi);
2387         ice_vsi_set_tc_cfg(vsi);
2388
2389         /* Initialize VSI struct elements and create VSI in FW */
2390         ret = ice_vsi_init(vsi);
2391         if (ret < 0)
2392                 goto err_vsi;
2393
2394         switch (vsi->type) {
2395         case ICE_VSI_PF:
2396                 ret = ice_vsi_alloc_q_vectors(vsi);
2397                 if (ret)
2398                         goto err_rings;
2399
2400                 ret = ice_vsi_setup_vector_base(vsi);
2401                 if (ret)
2402                         goto err_vectors;
2403
2404                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2405                 if (ret)
2406                         goto err_vectors;
2407
2408                 ret = ice_vsi_alloc_rings(vsi);
2409                 if (ret)
2410                         goto err_vectors;
2411
2412                 ice_vsi_map_rings_to_vectors(vsi);
2413                 if (ice_is_xdp_ena_vsi(vsi)) {
2414                         vsi->num_xdp_txq = vsi->alloc_txq;
2415                         ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2416                         if (ret)
2417                                 goto err_vectors;
2418                 }
2419                 /* Do not exit if configuring RSS had an issue, at least
2420                  * receive traffic on first queue. Hence no need to capture
2421                  * return value
2422                  */
2423                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2424                         ice_vsi_cfg_rss_lut_key(vsi);
2425                 break;
2426         case ICE_VSI_VF:
2427                 ret = ice_vsi_alloc_q_vectors(vsi);
2428                 if (ret)
2429                         goto err_rings;
2430
2431                 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2432                 if (ret)
2433                         goto err_vectors;
2434
2435                 ret = ice_vsi_alloc_rings(vsi);
2436                 if (ret)
2437                         goto err_vectors;
2438
2439                 break;
2440         default:
2441                 break;
2442         }
2443
2444         /* configure VSI nodes based on number of queues and TC's */
2445         for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2446                 max_txqs[i] = vsi->alloc_txq;
2447
2448                 if (ice_is_xdp_ena_vsi(vsi))
2449                         max_txqs[i] += vsi->num_xdp_txq;
2450         }
2451
2452         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2453                                  max_txqs);
2454         if (status) {
2455                 dev_err(&pf->pdev->dev,
2456                         "VSI %d failed lan queue config, error %d\n",
2457                         vsi->vsi_num, status);
2458                 goto err_vectors;
2459         }
2460         return 0;
2461
2462 err_vectors:
2463         ice_vsi_free_q_vectors(vsi);
2464 err_rings:
2465         if (vsi->netdev) {
2466                 vsi->current_netdev_flags = 0;
2467                 unregister_netdev(vsi->netdev);
2468                 free_netdev(vsi->netdev);
2469                 vsi->netdev = NULL;
2470         }
2471 err_vsi:
2472         ice_vsi_clear(vsi);
2473         set_bit(__ICE_RESET_FAILED, pf->state);
2474         return ret;
2475 }
2476
2477 /**
2478  * ice_is_reset_in_progress - check for a reset in progress
2479  * @state: PF state field
2480  */
2481 bool ice_is_reset_in_progress(unsigned long *state)
2482 {
2483         return test_bit(__ICE_RESET_OICR_RECV, state) ||
2484                test_bit(__ICE_DCBNL_DEVRESET, state) ||
2485                test_bit(__ICE_PFR_REQ, state) ||
2486                test_bit(__ICE_CORER_REQ, state) ||
2487                test_bit(__ICE_GLOBR_REQ, state);
2488 }
2489
2490 #ifdef CONFIG_DCB
2491 /**
2492  * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2493  * @vsi: VSI being configured
2494  * @ctx: the context buffer returned from AQ VSI update command
2495  */
2496 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2497 {
2498         vsi->info.mapping_flags = ctx->info.mapping_flags;
2499         memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2500                sizeof(vsi->info.q_mapping));
2501         memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2502                sizeof(vsi->info.tc_mapping));
2503 }
2504
2505 /**
2506  * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2507  * @vsi: VSI to be configured
2508  * @ena_tc: TC bitmap
2509  *
2510  * VSI queues expected to be quiesced before calling this function
2511  */
2512 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2513 {
2514         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2515         struct ice_vsi_ctx *ctx;
2516         struct ice_pf *pf = vsi->back;
2517         enum ice_status status;
2518         int i, ret = 0;
2519         u8 num_tc = 0;
2520
2521         ice_for_each_traffic_class(i) {
2522                 /* build bitmap of enabled TCs */
2523                 if (ena_tc & BIT(i))
2524                         num_tc++;
2525                 /* populate max_txqs per TC */
2526                 max_txqs[i] = vsi->alloc_txq;
2527         }
2528
2529         vsi->tc_cfg.ena_tc = ena_tc;
2530         vsi->tc_cfg.numtc = num_tc;
2531
2532         ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
2533         if (!ctx)
2534                 return -ENOMEM;
2535
2536         ctx->vf_num = 0;
2537         ctx->info = vsi->info;
2538
2539         ice_vsi_setup_q_map(vsi, ctx);
2540
2541         /* must to indicate which section of VSI context are being modified */
2542         ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2543         status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2544         if (status) {
2545                 dev_info(&pf->pdev->dev, "Failed VSI Update\n");
2546                 ret = -EIO;
2547                 goto out;
2548         }
2549
2550         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2551                                  max_txqs);
2552
2553         if (status) {
2554                 dev_err(&pf->pdev->dev,
2555                         "VSI %d failed TC config, error %d\n",
2556                         vsi->vsi_num, status);
2557                 ret = -EIO;
2558                 goto out;
2559         }
2560         ice_vsi_update_q_map(vsi, ctx);
2561         vsi->info.valid_sections = 0;
2562
2563         ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2564 out:
2565         devm_kfree(&pf->pdev->dev, ctx);
2566         return ret;
2567 }
2568 #endif /* CONFIG_DCB */
2569
2570 /**
2571  * ice_nvm_version_str - format the NVM version strings
2572  * @hw: ptr to the hardware info
2573  */
2574 char *ice_nvm_version_str(struct ice_hw *hw)
2575 {
2576         u8 oem_ver, oem_patch, ver_hi, ver_lo;
2577         static char buf[ICE_NVM_VER_LEN];
2578         u16 oem_build;
2579
2580         ice_get_nvm_version(hw, &oem_ver, &oem_build, &oem_patch, &ver_hi,
2581                             &ver_lo);
2582
2583         snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d", ver_hi, ver_lo,
2584                  hw->nvm.eetrack, oem_ver, oem_build, oem_patch);
2585
2586         return buf;
2587 }
2588
2589 /**
2590  * ice_update_ring_stats - Update ring statistics
2591  * @ring: ring to update
2592  * @cont: used to increment per-vector counters
2593  * @pkts: number of processed packets
2594  * @bytes: number of processed bytes
2595  *
2596  * This function assumes that caller has acquired a u64_stats_sync lock.
2597  */
2598 static void
2599 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2600                       u64 pkts, u64 bytes)
2601 {
2602         ring->stats.bytes += bytes;
2603         ring->stats.pkts += pkts;
2604         cont->total_bytes += bytes;
2605         cont->total_pkts += pkts;
2606 }
2607
2608 /**
2609  * ice_update_tx_ring_stats - Update Tx ring specific counters
2610  * @tx_ring: ring to update
2611  * @pkts: number of processed packets
2612  * @bytes: number of processed bytes
2613  */
2614 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
2615 {
2616         u64_stats_update_begin(&tx_ring->syncp);
2617         ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
2618         u64_stats_update_end(&tx_ring->syncp);
2619 }
2620
2621 /**
2622  * ice_update_rx_ring_stats - Update Rx ring specific counters
2623  * @rx_ring: ring to update
2624  * @pkts: number of processed packets
2625  * @bytes: number of processed bytes
2626  */
2627 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
2628 {
2629         u64_stats_update_begin(&rx_ring->syncp);
2630         ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
2631         u64_stats_update_end(&rx_ring->syncp);
2632 }
2633
2634 /**
2635  * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
2636  * @vsi: the VSI being configured MAC filter
2637  * @macaddr: the MAC address to be added.
2638  * @set: Add or delete a MAC filter
2639  *
2640  * Adds or removes MAC address filter entry for VF VSI
2641  */
2642 enum ice_status
2643 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
2644 {
2645         LIST_HEAD(tmp_add_list);
2646         enum ice_status status;
2647
2648          /* Update MAC filter list to be added or removed for a VSI */
2649         if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
2650                 status = ICE_ERR_NO_MEMORY;
2651                 goto cfg_mac_fltr_exit;
2652         }
2653
2654         if (set)
2655                 status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
2656         else
2657                 status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
2658
2659 cfg_mac_fltr_exit:
2660         ice_free_fltr_list(&vsi->back->pdev->dev, &tmp_add_list);
2661         return status;
2662 }