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