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