]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_lib.c
a4dfdf35ceab521802b7e73855d9062a75d5e62d
[linux.git] / drivers / net / ethernet / intel / ice / ice_lib.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_lib.h"
6
7 /**
8  * ice_setup_rx_ctx - Configure a receive ring context
9  * @ring: The Rx ring to configure
10  *
11  * Configure the Rx descriptor ring in RLAN context.
12  */
13 static int ice_setup_rx_ctx(struct ice_ring *ring)
14 {
15         struct ice_vsi *vsi = ring->vsi;
16         struct ice_hw *hw = &vsi->back->hw;
17         u32 rxdid = ICE_RXDID_FLEX_NIC;
18         struct ice_rlan_ctx rlan_ctx;
19         u32 regval;
20         u16 pf_q;
21         int err;
22
23         /* what is RX queue number in global space of 2K Rx queues */
24         pf_q = vsi->rxq_map[ring->q_index];
25
26         /* clear the context structure first */
27         memset(&rlan_ctx, 0, sizeof(rlan_ctx));
28
29         rlan_ctx.base = ring->dma >> 7;
30
31         rlan_ctx.qlen = ring->count;
32
33         /* Receive Packet Data Buffer Size.
34          * The Packet Data Buffer Size is defined in 128 byte units.
35          */
36         rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
37
38         /* use 32 byte descriptors */
39         rlan_ctx.dsize = 1;
40
41         /* Strip the Ethernet CRC bytes before the packet is posted to host
42          * memory.
43          */
44         rlan_ctx.crcstrip = 1;
45
46         /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
47         rlan_ctx.l2tsel = 1;
48
49         rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
50         rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
51         rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
52
53         /* This controls whether VLAN is stripped from inner headers
54          * The VLAN in the inner L2 header is stripped to the receive
55          * descriptor if enabled by this flag.
56          */
57         rlan_ctx.showiv = 0;
58
59         /* Max packet size for this queue - must not be set to a larger value
60          * than 5 x DBUF
61          */
62         rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
63                                ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
64
65         /* Rx queue threshold in units of 64 */
66         rlan_ctx.lrxqthresh = 1;
67
68          /* Enable Flexible Descriptors in the queue context which
69           * allows this driver to select a specific receive descriptor format
70           */
71         regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
72         regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
73                 QRXFLXP_CNTXT_RXDID_IDX_M;
74
75         /* increasing context priority to pick up profile id;
76          * default is 0x01; setting to 0x03 to ensure profile
77          * is programming if prev context is of same priority
78          */
79         regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
80                 QRXFLXP_CNTXT_RXDID_PRIO_M;
81
82         wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
83
84         /* Absolute queue number out of 2K needs to be passed */
85         err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
86         if (err) {
87                 dev_err(&vsi->back->pdev->dev,
88                         "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
89                         pf_q, err);
90                 return -EIO;
91         }
92
93         /* init queue specific tail register */
94         ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
95         writel(0, ring->tail);
96         ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
97
98         return 0;
99 }
100
101 /**
102  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
103  * @ring: The Tx ring to configure
104  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
105  * @pf_q: queue index in the PF space
106  *
107  * Configure the Tx descriptor ring in TLAN context.
108  */
109 static void
110 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
111 {
112         struct ice_vsi *vsi = ring->vsi;
113         struct ice_hw *hw = &vsi->back->hw;
114
115         tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
116
117         tlan_ctx->port_num = vsi->port_info->lport;
118
119         /* Transmit Queue Length */
120         tlan_ctx->qlen = ring->count;
121
122         /* PF number */
123         tlan_ctx->pf_num = hw->pf_id;
124
125         /* queue belongs to a specific VSI type
126          * VF / VM index should be programmed per vmvf_type setting:
127          * for vmvf_type = VF, it is VF number between 0-256
128          * for vmvf_type = VM, it is VM number between 0-767
129          * for PF or EMP this field should be set to zero
130          */
131         switch (vsi->type) {
132         case ICE_VSI_PF:
133                 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
134                 break;
135         default:
136                 return;
137         }
138
139         /* make sure the context is associated with the right VSI */
140         tlan_ctx->src_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
141
142         tlan_ctx->tso_ena = ICE_TX_LEGACY;
143         tlan_ctx->tso_qnum = pf_q;
144
145         /* Legacy or Advanced Host Interface:
146          * 0: Advanced Host Interface
147          * 1: Legacy Host Interface
148          */
149         tlan_ctx->legacy_int = ICE_TX_LEGACY;
150 }
151
152 /**
153  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
154  * @pf: the PF being configured
155  * @pf_q: the PF queue
156  * @ena: enable or disable state of the queue
157  *
158  * This routine will wait for the given Rx queue of the PF to reach the
159  * enabled or disabled state.
160  * Returns -ETIMEDOUT in case of failing to reach the requested state after
161  * multiple retries; else will return 0 in case of success.
162  */
163 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
164 {
165         int i;
166
167         for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
168                 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
169
170                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
171                         break;
172
173                 usleep_range(10, 20);
174         }
175         if (i >= ICE_Q_WAIT_RETRY_LIMIT)
176                 return -ETIMEDOUT;
177
178         return 0;
179 }
180
181 /**
182  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
183  * @vsi: the VSI being configured
184  * @ena: start or stop the Rx rings
185  */
186 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
187 {
188         struct ice_pf *pf = vsi->back;
189         struct ice_hw *hw = &pf->hw;
190         int i, j, ret = 0;
191
192         for (i = 0; i < vsi->num_rxq; i++) {
193                 int pf_q = vsi->rxq_map[i];
194                 u32 rx_reg;
195
196                 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
197                         rx_reg = rd32(hw, QRX_CTRL(pf_q));
198                         if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
199                             ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
200                                 break;
201                         usleep_range(1000, 2000);
202                 }
203
204                 /* Skip if the queue is already in the requested state */
205                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
206                         continue;
207
208                 /* turn on/off the queue */
209                 if (ena)
210                         rx_reg |= QRX_CTRL_QENA_REQ_M;
211                 else
212                         rx_reg &= ~QRX_CTRL_QENA_REQ_M;
213                 wr32(hw, QRX_CTRL(pf_q), rx_reg);
214
215                 /* wait for the change to finish */
216                 ret = ice_pf_rxq_wait(pf, pf_q, ena);
217                 if (ret) {
218                         dev_err(&pf->pdev->dev,
219                                 "VSI idx %d Rx ring %d %sable timeout\n",
220                                 vsi->idx, pf_q, (ena ? "en" : "dis"));
221                         break;
222                 }
223         }
224
225         return ret;
226 }
227
228 /**
229  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
230  * @vsi: VSI pointer
231  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
232  *
233  * On error: returns error code (negative)
234  * On success: returns 0
235  */
236 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
237 {
238         struct ice_pf *pf = vsi->back;
239
240         /* allocate memory for both Tx and Rx ring pointers */
241         vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
242                                      sizeof(struct ice_ring *), GFP_KERNEL);
243         if (!vsi->tx_rings)
244                 goto err_txrings;
245
246         vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
247                                      sizeof(struct ice_ring *), GFP_KERNEL);
248         if (!vsi->rx_rings)
249                 goto err_rxrings;
250
251         if (alloc_qvectors) {
252                 /* allocate memory for q_vector pointers */
253                 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
254                                               vsi->num_q_vectors,
255                                               sizeof(struct ice_q_vector *),
256                                               GFP_KERNEL);
257                 if (!vsi->q_vectors)
258                         goto err_vectors;
259         }
260
261         return 0;
262
263 err_vectors:
264         devm_kfree(&pf->pdev->dev, vsi->rx_rings);
265 err_rxrings:
266         devm_kfree(&pf->pdev->dev, vsi->tx_rings);
267 err_txrings:
268         return -ENOMEM;
269 }
270
271 /**
272  * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
273  * @vsi: the VSI being configured
274  *
275  * Return 0 on success and a negative value on error
276  */
277 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
278 {
279         struct ice_pf *pf = vsi->back;
280
281         switch (vsi->type) {
282         case ICE_VSI_PF:
283                 vsi->alloc_txq = pf->num_lan_tx;
284                 vsi->alloc_rxq = pf->num_lan_rx;
285                 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
286                 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
287                 break;
288         default:
289                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
290                          vsi->type);
291                 break;
292         }
293 }
294
295 /**
296  * ice_get_free_slot - get the next non-NULL location index in array
297  * @array: array to search
298  * @size: size of the array
299  * @curr: last known occupied index to be used as a search hint
300  *
301  * void * is being used to keep the functionality generic. This lets us use this
302  * function on any array of pointers.
303  */
304 static int ice_get_free_slot(void *array, int size, int curr)
305 {
306         int **tmp_array = (int **)array;
307         int next;
308
309         if (curr < (size - 1) && !tmp_array[curr + 1]) {
310                 next = curr + 1;
311         } else {
312                 int i = 0;
313
314                 while ((i < size) && (tmp_array[i]))
315                         i++;
316                 if (i == size)
317                         next = ICE_NO_VSI;
318                 else
319                         next = i;
320         }
321         return next;
322 }
323
324 /**
325  * ice_vsi_delete - delete a VSI from the switch
326  * @vsi: pointer to VSI being removed
327  */
328 void ice_vsi_delete(struct ice_vsi *vsi)
329 {
330         struct ice_pf *pf = vsi->back;
331         struct ice_vsi_ctx ctxt;
332         enum ice_status status;
333
334         ctxt.vsi_num = vsi->vsi_num;
335
336         memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
337
338         status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
339         if (status)
340                 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
341                         vsi->vsi_num);
342 }
343
344 /**
345  * ice_vsi_free_arrays - clean up VSI resources
346  * @vsi: pointer to VSI being cleared
347  * @free_qvectors: bool to specify if q_vectors should be deallocated
348  */
349 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
350 {
351         struct ice_pf *pf = vsi->back;
352
353         /* free the ring and vector containers */
354         if (free_qvectors && vsi->q_vectors) {
355                 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
356                 vsi->q_vectors = NULL;
357         }
358         if (vsi->tx_rings) {
359                 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
360                 vsi->tx_rings = NULL;
361         }
362         if (vsi->rx_rings) {
363                 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
364                 vsi->rx_rings = NULL;
365         }
366 }
367
368 /**
369  * ice_vsi_clear - clean up and deallocate the provided VSI
370  * @vsi: pointer to VSI being cleared
371  *
372  * This deallocates the VSI's queue resources, removes it from the PF's
373  * VSI array if necessary, and deallocates the VSI
374  *
375  * Returns 0 on success, negative on failure
376  */
377 int ice_vsi_clear(struct ice_vsi *vsi)
378 {
379         struct ice_pf *pf = NULL;
380
381         if (!vsi)
382                 return 0;
383
384         if (!vsi->back)
385                 return -EINVAL;
386
387         pf = vsi->back;
388
389         if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
390                 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
391                         vsi->idx);
392                 return -EINVAL;
393         }
394
395         mutex_lock(&pf->sw_mutex);
396         /* updates the PF for this cleared VSI */
397
398         pf->vsi[vsi->idx] = NULL;
399         if (vsi->idx < pf->next_vsi)
400                 pf->next_vsi = vsi->idx;
401
402         ice_vsi_free_arrays(vsi, true);
403         mutex_unlock(&pf->sw_mutex);
404         devm_kfree(&pf->pdev->dev, vsi);
405
406         return 0;
407 }
408
409 /**
410  * ice_msix_clean_rings - MSIX mode Interrupt Handler
411  * @irq: interrupt number
412  * @data: pointer to a q_vector
413  */
414 irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
415 {
416         struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
417
418         if (!q_vector->tx.ring && !q_vector->rx.ring)
419                 return IRQ_HANDLED;
420
421         napi_schedule(&q_vector->napi);
422
423         return IRQ_HANDLED;
424 }
425
426 /**
427  * ice_vsi_alloc - Allocates the next available struct VSI in the PF
428  * @pf: board private structure
429  * @type: type of VSI
430  *
431  * returns a pointer to a VSI on success, NULL on failure.
432  */
433 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
434 {
435         struct ice_vsi *vsi = NULL;
436
437         /* Need to protect the allocation of the VSIs at the PF level */
438         mutex_lock(&pf->sw_mutex);
439
440         /* If we have already allocated our maximum number of VSIs,
441          * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
442          * is available to be populated
443          */
444         if (pf->next_vsi == ICE_NO_VSI) {
445                 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
446                 goto unlock_pf;
447         }
448
449         vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
450         if (!vsi)
451                 goto unlock_pf;
452
453         vsi->type = type;
454         vsi->back = pf;
455         set_bit(__ICE_DOWN, vsi->state);
456         vsi->idx = pf->next_vsi;
457         vsi->work_lmt = ICE_DFLT_IRQ_WORK;
458
459         ice_vsi_set_num_qs(vsi);
460
461         switch (vsi->type) {
462         case ICE_VSI_PF:
463                 if (ice_vsi_alloc_arrays(vsi, true))
464                         goto err_rings;
465
466                 /* Setup default MSIX irq handler for VSI */
467                 vsi->irq_handler = ice_msix_clean_rings;
468                 break;
469         default:
470                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
471                 goto unlock_pf;
472         }
473
474         /* fill VSI slot in the PF struct */
475         pf->vsi[pf->next_vsi] = vsi;
476
477         /* prepare pf->next_vsi for next use */
478         pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
479                                          pf->next_vsi);
480         goto unlock_pf;
481
482 err_rings:
483         devm_kfree(&pf->pdev->dev, vsi);
484         vsi = NULL;
485 unlock_pf:
486         mutex_unlock(&pf->sw_mutex);
487         return vsi;
488 }
489
490 /**
491  * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
492  * @vsi: the VSI getting queues
493  *
494  * Return 0 on success and a negative value on error
495  */
496 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
497 {
498         struct ice_pf *pf = vsi->back;
499         int offset, ret = 0;
500
501         mutex_lock(&pf->avail_q_mutex);
502         /* look for contiguous block of queues for Tx */
503         offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
504                                             0, vsi->alloc_txq, 0);
505         if (offset < ICE_MAX_TXQS) {
506                 int i;
507
508                 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
509                 for (i = 0; i < vsi->alloc_txq; i++)
510                         vsi->txq_map[i] = i + offset;
511         } else {
512                 ret = -ENOMEM;
513                 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
514         }
515
516         /* look for contiguous block of queues for Rx */
517         offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
518                                             0, vsi->alloc_rxq, 0);
519         if (offset < ICE_MAX_RXQS) {
520                 int i;
521
522                 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
523                 for (i = 0; i < vsi->alloc_rxq; i++)
524                         vsi->rxq_map[i] = i + offset;
525         } else {
526                 ret = -ENOMEM;
527                 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
528         }
529         mutex_unlock(&pf->avail_q_mutex);
530
531         return ret;
532 }
533
534 /**
535  * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
536  * @vsi: the VSI getting queues
537  *
538  * Return 0 on success and a negative value on error
539  */
540 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
541 {
542         struct ice_pf *pf = vsi->back;
543         int i, index = 0;
544
545         mutex_lock(&pf->avail_q_mutex);
546
547         if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
548                 for (i = 0; i < vsi->alloc_txq; i++) {
549                         index = find_next_zero_bit(pf->avail_txqs,
550                                                    ICE_MAX_TXQS, index);
551                         if (index < ICE_MAX_TXQS) {
552                                 set_bit(index, pf->avail_txqs);
553                                 vsi->txq_map[i] = index;
554                         } else {
555                                 goto err_scatter_tx;
556                         }
557                 }
558         }
559
560         if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
561                 for (i = 0; i < vsi->alloc_rxq; i++) {
562                         index = find_next_zero_bit(pf->avail_rxqs,
563                                                    ICE_MAX_RXQS, index);
564                         if (index < ICE_MAX_RXQS) {
565                                 set_bit(index, pf->avail_rxqs);
566                                 vsi->rxq_map[i] = index;
567                         } else {
568                                 goto err_scatter_rx;
569                         }
570                 }
571         }
572
573         mutex_unlock(&pf->avail_q_mutex);
574         return 0;
575
576 err_scatter_rx:
577         /* unflag any queues we have grabbed (i is failed position) */
578         for (index = 0; index < i; index++) {
579                 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
580                 vsi->rxq_map[index] = 0;
581         }
582         i = vsi->alloc_txq;
583 err_scatter_tx:
584         /* i is either position of failed attempt or vsi->alloc_txq */
585         for (index = 0; index < i; index++) {
586                 clear_bit(vsi->txq_map[index], pf->avail_txqs);
587                 vsi->txq_map[index] = 0;
588         }
589
590         mutex_unlock(&pf->avail_q_mutex);
591         return -ENOMEM;
592 }
593
594 /**
595  * ice_vsi_get_qs - Assign queues from PF to VSI
596  * @vsi: the VSI to assign queues to
597  *
598  * Returns 0 on success and a negative value on error
599  */
600 static int ice_vsi_get_qs(struct ice_vsi *vsi)
601 {
602         int ret = 0;
603
604         vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
605         vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
606
607         /* NOTE: ice_vsi_get_qs_contig() will set the Rx/Tx mapping
608          * modes individually to scatter if assigning contiguous queues
609          * to Rx or Tx fails
610          */
611         ret = ice_vsi_get_qs_contig(vsi);
612         if (ret < 0) {
613                 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
614                         vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
615                                                ICE_MAX_SCATTER_TXQS);
616                 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
617                         vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
618                                                ICE_MAX_SCATTER_RXQS);
619                 ret = ice_vsi_get_qs_scatter(vsi);
620         }
621
622         return ret;
623 }
624
625 /**
626  * ice_vsi_put_qs - Release queues from VSI to PF
627  * @vsi: the VSI that is going to release queues
628  */
629 void ice_vsi_put_qs(struct ice_vsi *vsi)
630 {
631         struct ice_pf *pf = vsi->back;
632         int i;
633
634         mutex_lock(&pf->avail_q_mutex);
635
636         for (i = 0; i < vsi->alloc_txq; i++) {
637                 clear_bit(vsi->txq_map[i], pf->avail_txqs);
638                 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
639         }
640
641         for (i = 0; i < vsi->alloc_rxq; i++) {
642                 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
643                 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
644         }
645
646         mutex_unlock(&pf->avail_q_mutex);
647 }
648
649 /**
650  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
651  * @vsi: the VSI being removed
652  */
653 static void ice_rss_clean(struct ice_vsi *vsi)
654 {
655         struct ice_pf *pf;
656
657         pf = vsi->back;
658
659         if (vsi->rss_hkey_user)
660                 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
661         if (vsi->rss_lut_user)
662                 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
663 }
664
665 /**
666  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
667  * @vsi: the VSI being configured
668  */
669 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
670 {
671         struct ice_hw_common_caps *cap;
672         struct ice_pf *pf = vsi->back;
673
674         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
675                 vsi->rss_size = 1;
676                 return;
677         }
678
679         cap = &pf->hw.func_caps.common_cap;
680         switch (vsi->type) {
681         case ICE_VSI_PF:
682                 /* PF VSI will inherit RSS instance of PF */
683                 vsi->rss_table_size = cap->rss_table_size;
684                 vsi->rss_size = min_t(int, num_online_cpus(),
685                                       BIT(cap->rss_table_entry_width));
686                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
687                 break;
688         default:
689                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n",
690                          vsi->type);
691                 break;
692         }
693 }
694
695 /**
696  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
697  * @ctxt: the VSI context being set
698  *
699  * This initializes a default VSI context for all sections except the Queues.
700  */
701 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
702 {
703         u32 table = 0;
704
705         memset(&ctxt->info, 0, sizeof(ctxt->info));
706         /* VSI's should be allocated from shared pool */
707         ctxt->alloc_from_pool = true;
708         /* Src pruning enabled by default */
709         ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
710         /* Traffic from VSI can be sent to LAN */
711         ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
712         /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
713          * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
714          * packets untagged/tagged.
715          */
716         ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
717                                   ICE_AQ_VSI_VLAN_MODE_M) >>
718                                  ICE_AQ_VSI_VLAN_MODE_S);
719         /* Have 1:1 UP mapping for both ingress/egress tables */
720         table |= ICE_UP_TABLE_TRANSLATE(0, 0);
721         table |= ICE_UP_TABLE_TRANSLATE(1, 1);
722         table |= ICE_UP_TABLE_TRANSLATE(2, 2);
723         table |= ICE_UP_TABLE_TRANSLATE(3, 3);
724         table |= ICE_UP_TABLE_TRANSLATE(4, 4);
725         table |= ICE_UP_TABLE_TRANSLATE(5, 5);
726         table |= ICE_UP_TABLE_TRANSLATE(6, 6);
727         table |= ICE_UP_TABLE_TRANSLATE(7, 7);
728         ctxt->info.ingress_table = cpu_to_le32(table);
729         ctxt->info.egress_table = cpu_to_le32(table);
730         /* Have 1:1 UP mapping for outer to inner UP table */
731         ctxt->info.outer_up_table = cpu_to_le32(table);
732         /* No Outer tag support outer_tag_flags remains to zero */
733 }
734
735 /**
736  * ice_vsi_setup_q_map - Setup a VSI queue map
737  * @vsi: the VSI being configured
738  * @ctxt: VSI context structure
739  */
740 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
741 {
742         u16 offset = 0, qmap = 0, numq_tc;
743         u16 pow = 0, max_rss = 0, qcount;
744         u16 qcount_tx = vsi->alloc_txq;
745         u16 qcount_rx = vsi->alloc_rxq;
746         bool ena_tc0 = false;
747         int i;
748
749         /* at least TC0 should be enabled by default */
750         if (vsi->tc_cfg.numtc) {
751                 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
752                         ena_tc0 = true;
753         } else {
754                 ena_tc0 = true;
755         }
756
757         if (ena_tc0) {
758                 vsi->tc_cfg.numtc++;
759                 vsi->tc_cfg.ena_tc |= 1;
760         }
761
762         numq_tc = qcount_rx / vsi->tc_cfg.numtc;
763
764         /* TC mapping is a function of the number of Rx queues assigned to the
765          * VSI for each traffic class and the offset of these queues.
766          * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
767          * queues allocated to TC0. No:of queues is a power-of-2.
768          *
769          * If TC is not enabled, the queue offset is set to 0, and allocate one
770          * queue, this way, traffic for the given TC will be sent to the default
771          * queue.
772          *
773          * Setup number and offset of Rx queues for all TCs for the VSI
774          */
775
776         /* qcount will change if RSS is enabled */
777         if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
778                 if (vsi->type == ICE_VSI_PF)
779                         max_rss = ICE_MAX_LG_RSS_QS;
780                 else
781                         max_rss = ICE_MAX_SMALL_RSS_QS;
782
783                 qcount = min_t(int, numq_tc, max_rss);
784                 qcount = min_t(int, qcount, vsi->rss_size);
785         } else {
786                 qcount = numq_tc;
787         }
788
789         /* find the (rounded up) power-of-2 of qcount */
790         pow = order_base_2(qcount);
791
792         for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
793                 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
794                         /* TC is not enabled */
795                         vsi->tc_cfg.tc_info[i].qoffset = 0;
796                         vsi->tc_cfg.tc_info[i].qcount = 1;
797                         ctxt->info.tc_mapping[i] = 0;
798                         continue;
799                 }
800
801                 /* TC is enabled */
802                 vsi->tc_cfg.tc_info[i].qoffset = offset;
803                 vsi->tc_cfg.tc_info[i].qcount = qcount;
804
805                 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
806                         ICE_AQ_VSI_TC_Q_OFFSET_M) |
807                         ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
808                          ICE_AQ_VSI_TC_Q_NUM_M);
809                 offset += qcount;
810                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
811         }
812
813         vsi->num_txq = qcount_tx;
814         vsi->num_rxq = offset;
815
816         /* Rx queue mapping */
817         ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
818         /* q_mapping buffer holds the info for the first queue allocated for
819          * this VSI in the PF space and also the number of queues associated
820          * with this VSI.
821          */
822         ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
823         ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
824 }
825
826 /**
827  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
828  * @ctxt: the VSI context being set
829  * @vsi: the VSI being configured
830  */
831 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
832 {
833         u8 lut_type, hash_type;
834
835         switch (vsi->type) {
836         case ICE_VSI_PF:
837                 /* PF VSI will inherit RSS instance of PF */
838                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
839                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
840                 break;
841         default:
842                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
843                          vsi->type);
844                 return;
845         }
846
847         ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
848                                 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
849                                 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
850                                  ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
851 }
852
853 /**
854  * ice_vsi_init - Create and initialize a VSI
855  * @vsi: the VSI being configured
856  *
857  * This initializes a VSI context depending on the VSI type to be added and
858  * passes it down to the add_vsi aq command to create a new VSI.
859  */
860 static int ice_vsi_init(struct ice_vsi *vsi)
861 {
862         struct ice_vsi_ctx ctxt = { 0 };
863         struct ice_pf *pf = vsi->back;
864         struct ice_hw *hw = &pf->hw;
865         int ret = 0;
866
867         switch (vsi->type) {
868         case ICE_VSI_PF:
869                 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
870                 break;
871         default:
872                 return -ENODEV;
873         }
874
875         ice_set_dflt_vsi_ctx(&ctxt);
876         /* if the switch is in VEB mode, allow VSI loopback */
877         if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
878                 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
879
880         /* Set LUT type and HASH type if RSS is enabled */
881         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
882                 ice_set_rss_vsi_ctx(&ctxt, vsi);
883
884         ctxt.info.sw_id = vsi->port_info->sw_id;
885         ice_vsi_setup_q_map(vsi, &ctxt);
886
887         ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
888         if (ret) {
889                 dev_err(&pf->pdev->dev,
890                         "Add VSI failed, err %d\n", ret);
891                 return -EIO;
892         }
893
894         /* keep context for update VSI operations */
895         vsi->info = ctxt.info;
896
897         /* record VSI number returned */
898         vsi->vsi_num = ctxt.vsi_num;
899
900         return ret;
901 }
902
903 /**
904  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
905  * @vsi: VSI having the memory freed
906  * @v_idx: index of the vector to be freed
907  */
908 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
909 {
910         struct ice_q_vector *q_vector;
911         struct ice_ring *ring;
912
913         if (!vsi->q_vectors[v_idx]) {
914                 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
915                         v_idx);
916                 return;
917         }
918         q_vector = vsi->q_vectors[v_idx];
919
920         ice_for_each_ring(ring, q_vector->tx)
921                 ring->q_vector = NULL;
922         ice_for_each_ring(ring, q_vector->rx)
923                 ring->q_vector = NULL;
924
925         /* only VSI with an associated netdev is set up with NAPI */
926         if (vsi->netdev)
927                 netif_napi_del(&q_vector->napi);
928
929         devm_kfree(&vsi->back->pdev->dev, q_vector);
930         vsi->q_vectors[v_idx] = NULL;
931 }
932
933 /**
934  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
935  * @vsi: the VSI having memory freed
936  */
937 void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
938 {
939         int v_idx;
940
941         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
942                 ice_free_q_vector(vsi, v_idx);
943 }
944
945 /**
946  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
947  * @vsi: the VSI being configured
948  * @v_idx: index of the vector in the VSI struct
949  *
950  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
951  */
952 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
953 {
954         struct ice_pf *pf = vsi->back;
955         struct ice_q_vector *q_vector;
956
957         /* allocate q_vector */
958         q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
959         if (!q_vector)
960                 return -ENOMEM;
961
962         q_vector->vsi = vsi;
963         q_vector->v_idx = v_idx;
964         /* only set affinity_mask if the CPU is online */
965         if (cpu_online(v_idx))
966                 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
967
968         /* This will not be called in the driver load path because the netdev
969          * will not be created yet. All other cases with register the NAPI
970          * handler here (i.e. resume, reset/rebuild, etc.)
971          */
972         if (vsi->netdev)
973                 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
974                                NAPI_POLL_WEIGHT);
975
976         /* tie q_vector and VSI together */
977         vsi->q_vectors[v_idx] = q_vector;
978
979         return 0;
980 }
981
982 /**
983  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
984  * @vsi: the VSI being configured
985  *
986  * We allocate one q_vector per queue interrupt.  If allocation fails we
987  * return -ENOMEM.
988  */
989 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
990 {
991         struct ice_pf *pf = vsi->back;
992         int v_idx = 0, num_q_vectors;
993         int err;
994
995         if (vsi->q_vectors[0]) {
996                 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
997                         vsi->vsi_num);
998                 return -EEXIST;
999         }
1000
1001         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1002                 num_q_vectors = vsi->num_q_vectors;
1003         } else {
1004                 err = -EINVAL;
1005                 goto err_out;
1006         }
1007
1008         for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
1009                 err = ice_vsi_alloc_q_vector(vsi, v_idx);
1010                 if (err)
1011                         goto err_out;
1012         }
1013
1014         return 0;
1015
1016 err_out:
1017         while (v_idx--)
1018                 ice_free_q_vector(vsi, v_idx);
1019
1020         dev_err(&pf->pdev->dev,
1021                 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
1022                 vsi->num_q_vectors, vsi->vsi_num, err);
1023         vsi->num_q_vectors = 0;
1024         return err;
1025 }
1026
1027 /**
1028  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1029  * @vsi: ptr to the VSI
1030  *
1031  * This should only be called after ice_vsi_alloc() which allocates the
1032  * corresponding SW VSI structure and initializes num_queue_pairs for the
1033  * newly allocated VSI.
1034  *
1035  * Returns 0 on success or negative on failure
1036  */
1037 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1038 {
1039         struct ice_pf *pf = vsi->back;
1040         int num_q_vectors = 0;
1041
1042         if (vsi->base_vector) {
1043                 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
1044                         vsi->vsi_num, vsi->base_vector);
1045                 return -EEXIST;
1046         }
1047
1048         if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
1049                 return -ENOENT;
1050
1051         switch (vsi->type) {
1052         case ICE_VSI_PF:
1053                 num_q_vectors = vsi->num_q_vectors;
1054                 break;
1055         default:
1056                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1057                          vsi->type);
1058                 break;
1059         }
1060
1061         if (num_q_vectors)
1062                 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
1063                                                num_q_vectors, vsi->idx);
1064
1065         if (vsi->base_vector < 0) {
1066                 dev_err(&pf->pdev->dev,
1067                         "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
1068                         num_q_vectors, vsi->vsi_num, vsi->base_vector);
1069                 return -ENOENT;
1070         }
1071
1072         return 0;
1073 }
1074
1075 /**
1076  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1077  * @vsi: the VSI having rings deallocated
1078  */
1079 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1080 {
1081         int i;
1082
1083         if (vsi->tx_rings) {
1084                 for (i = 0; i < vsi->alloc_txq; i++) {
1085                         if (vsi->tx_rings[i]) {
1086                                 kfree_rcu(vsi->tx_rings[i], rcu);
1087                                 vsi->tx_rings[i] = NULL;
1088                         }
1089                 }
1090         }
1091         if (vsi->rx_rings) {
1092                 for (i = 0; i < vsi->alloc_rxq; i++) {
1093                         if (vsi->rx_rings[i]) {
1094                                 kfree_rcu(vsi->rx_rings[i], rcu);
1095                                 vsi->rx_rings[i] = NULL;
1096                         }
1097                 }
1098         }
1099 }
1100
1101 /**
1102  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1103  * @vsi: VSI which is having rings allocated
1104  */
1105 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1106 {
1107         struct ice_pf *pf = vsi->back;
1108         int i;
1109
1110         /* Allocate tx_rings */
1111         for (i = 0; i < vsi->alloc_txq; i++) {
1112                 struct ice_ring *ring;
1113
1114                 /* allocate with kzalloc(), free with kfree_rcu() */
1115                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1116
1117                 if (!ring)
1118                         goto err_out;
1119
1120                 ring->q_index = i;
1121                 ring->reg_idx = vsi->txq_map[i];
1122                 ring->ring_active = false;
1123                 ring->vsi = vsi;
1124                 ring->dev = &pf->pdev->dev;
1125                 ring->count = vsi->num_desc;
1126                 vsi->tx_rings[i] = ring;
1127         }
1128
1129         /* Allocate rx_rings */
1130         for (i = 0; i < vsi->alloc_rxq; i++) {
1131                 struct ice_ring *ring;
1132
1133                 /* allocate with kzalloc(), free with kfree_rcu() */
1134                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1135                 if (!ring)
1136                         goto err_out;
1137
1138                 ring->q_index = i;
1139                 ring->reg_idx = vsi->rxq_map[i];
1140                 ring->ring_active = false;
1141                 ring->vsi = vsi;
1142                 ring->netdev = vsi->netdev;
1143                 ring->dev = &pf->pdev->dev;
1144                 ring->count = vsi->num_desc;
1145                 vsi->rx_rings[i] = ring;
1146         }
1147
1148         return 0;
1149
1150 err_out:
1151         ice_vsi_clear_rings(vsi);
1152         return -ENOMEM;
1153 }
1154
1155 /**
1156  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1157  * @vsi: the VSI being configured
1158  *
1159  * This function maps descriptor rings to the queue-specific vectors allotted
1160  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1161  * and Rx rings to the vector as "efficiently" as possible.
1162  */
1163 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1164 {
1165         int q_vectors = vsi->num_q_vectors;
1166         int tx_rings_rem, rx_rings_rem;
1167         int v_id;
1168
1169         /* initially assigning remaining rings count to VSIs num queue value */
1170         tx_rings_rem = vsi->num_txq;
1171         rx_rings_rem = vsi->num_rxq;
1172
1173         for (v_id = 0; v_id < q_vectors; v_id++) {
1174                 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1175                 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1176
1177                 /* Tx rings mapping to vector */
1178                 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1179                 q_vector->num_ring_tx = tx_rings_per_v;
1180                 q_vector->tx.ring = NULL;
1181                 q_base = vsi->num_txq - tx_rings_rem;
1182
1183                 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1184                         struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1185
1186                         tx_ring->q_vector = q_vector;
1187                         tx_ring->next = q_vector->tx.ring;
1188                         q_vector->tx.ring = tx_ring;
1189                 }
1190                 tx_rings_rem -= tx_rings_per_v;
1191
1192                 /* Rx rings mapping to vector */
1193                 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1194                 q_vector->num_ring_rx = rx_rings_per_v;
1195                 q_vector->rx.ring = NULL;
1196                 q_base = vsi->num_rxq - rx_rings_rem;
1197
1198                 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1199                         struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1200
1201                         rx_ring->q_vector = q_vector;
1202                         rx_ring->next = q_vector->rx.ring;
1203                         q_vector->rx.ring = rx_ring;
1204                 }
1205                 rx_rings_rem -= rx_rings_per_v;
1206         }
1207 }
1208
1209 /**
1210  * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1211  * @vsi: VSI to be configured
1212  */
1213 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1214 {
1215         u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
1216         struct ice_aqc_get_set_rss_keys *key;
1217         struct ice_pf *pf = vsi->back;
1218         enum ice_status status;
1219         int err = 0;
1220         u8 *lut;
1221
1222         vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1223
1224         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
1225         if (!lut)
1226                 return -ENOMEM;
1227
1228         if (vsi->rss_lut_user)
1229                 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1230         else
1231                 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1232
1233         status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1234                                     vsi->rss_table_size);
1235
1236         if (status) {
1237                 dev_err(&vsi->back->pdev->dev,
1238                         "set_rss_lut failed, error %d\n", status);
1239                 err = -EIO;
1240                 goto ice_vsi_cfg_rss_exit;
1241         }
1242
1243         key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
1244         if (!key) {
1245                 err = -ENOMEM;
1246                 goto ice_vsi_cfg_rss_exit;
1247         }
1248
1249         if (vsi->rss_hkey_user)
1250                 memcpy(seed, vsi->rss_hkey_user,
1251                        ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1252         else
1253                 netdev_rss_key_fill((void *)seed,
1254                                     ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1255         memcpy(&key->standard_rss_key, seed,
1256                ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
1257
1258         status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1259
1260         if (status) {
1261                 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
1262                         status);
1263                 err = -EIO;
1264         }
1265
1266         devm_kfree(&pf->pdev->dev, key);
1267 ice_vsi_cfg_rss_exit:
1268         devm_kfree(&pf->pdev->dev, lut);
1269         return err;
1270 }
1271
1272 /**
1273  * ice_add_mac_to_list - Add a mac address filter entry to the list
1274  * @vsi: the VSI to be forwarded to
1275  * @add_list: pointer to the list which contains MAC filter entries
1276  * @macaddr: the MAC address to be added.
1277  *
1278  * Adds mac address filter entry to the temp list
1279  *
1280  * Returns 0 on success or ENOMEM on failure.
1281  */
1282 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1283                         const u8 *macaddr)
1284 {
1285         struct ice_fltr_list_entry *tmp;
1286         struct ice_pf *pf = vsi->back;
1287
1288         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
1289         if (!tmp)
1290                 return -ENOMEM;
1291
1292         tmp->fltr_info.flag = ICE_FLTR_TX;
1293         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1294         tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1295         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1296         tmp->fltr_info.vsi_handle = vsi->idx;
1297         ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1298
1299         INIT_LIST_HEAD(&tmp->list_entry);
1300         list_add(&tmp->list_entry, add_list);
1301
1302         return 0;
1303 }
1304
1305 /**
1306  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1307  * @vsi: the VSI to be updated
1308  */
1309 void ice_update_eth_stats(struct ice_vsi *vsi)
1310 {
1311         struct ice_eth_stats *prev_es, *cur_es;
1312         struct ice_hw *hw = &vsi->back->hw;
1313         u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1314
1315         prev_es = &vsi->eth_stats_prev;
1316         cur_es = &vsi->eth_stats;
1317
1318         ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
1319                           vsi->stat_offsets_loaded, &prev_es->rx_bytes,
1320                           &cur_es->rx_bytes);
1321
1322         ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
1323                           vsi->stat_offsets_loaded, &prev_es->rx_unicast,
1324                           &cur_es->rx_unicast);
1325
1326         ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
1327                           vsi->stat_offsets_loaded, &prev_es->rx_multicast,
1328                           &cur_es->rx_multicast);
1329
1330         ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
1331                           vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
1332                           &cur_es->rx_broadcast);
1333
1334         ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1335                           &prev_es->rx_discards, &cur_es->rx_discards);
1336
1337         ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
1338                           vsi->stat_offsets_loaded, &prev_es->tx_bytes,
1339                           &cur_es->tx_bytes);
1340
1341         ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
1342                           vsi->stat_offsets_loaded, &prev_es->tx_unicast,
1343                           &cur_es->tx_unicast);
1344
1345         ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
1346                           vsi->stat_offsets_loaded, &prev_es->tx_multicast,
1347                           &cur_es->tx_multicast);
1348
1349         ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
1350                           vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
1351                           &cur_es->tx_broadcast);
1352
1353         ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1354                           &prev_es->tx_errors, &cur_es->tx_errors);
1355
1356         vsi->stat_offsets_loaded = true;
1357 }
1358
1359 /**
1360  * ice_free_fltr_list - free filter lists helper
1361  * @dev: pointer to the device struct
1362  * @h: pointer to the list head to be freed
1363  *
1364  * Helper function to free filter lists previously created using
1365  * ice_add_mac_to_list
1366  */
1367 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1368 {
1369         struct ice_fltr_list_entry *e, *tmp;
1370
1371         list_for_each_entry_safe(e, tmp, h, list_entry) {
1372                 list_del(&e->list_entry);
1373                 devm_kfree(dev, e);
1374         }
1375 }
1376
1377 /**
1378  * ice_vsi_add_vlan - Add VSI membership for given VLAN
1379  * @vsi: the VSI being configured
1380  * @vid: VLAN id to be added
1381  */
1382 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1383 {
1384         struct ice_fltr_list_entry *tmp;
1385         struct ice_pf *pf = vsi->back;
1386         LIST_HEAD(tmp_add_list);
1387         enum ice_status status;
1388         int err = 0;
1389
1390         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
1391         if (!tmp)
1392                 return -ENOMEM;
1393
1394         tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1395         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1396         tmp->fltr_info.flag = ICE_FLTR_TX;
1397         tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1398         tmp->fltr_info.vsi_handle = vsi->idx;
1399         tmp->fltr_info.l_data.vlan.vlan_id = vid;
1400
1401         INIT_LIST_HEAD(&tmp->list_entry);
1402         list_add(&tmp->list_entry, &tmp_add_list);
1403
1404         status = ice_add_vlan(&pf->hw, &tmp_add_list);
1405         if (status) {
1406                 err = -ENODEV;
1407                 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
1408                         vid, vsi->vsi_num);
1409         }
1410
1411         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1412         return err;
1413 }
1414
1415 /**
1416  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1417  * @vsi: the VSI being configured
1418  * @vid: VLAN id to be removed
1419  *
1420  * Returns 0 on success and negative on failure
1421  */
1422 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1423 {
1424         struct ice_fltr_list_entry *list;
1425         struct ice_pf *pf = vsi->back;
1426         LIST_HEAD(tmp_add_list);
1427         int status = 0;
1428
1429         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
1430         if (!list)
1431                 return -ENOMEM;
1432
1433         list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1434         list->fltr_info.vsi_handle = vsi->idx;
1435         list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1436         list->fltr_info.l_data.vlan.vlan_id = vid;
1437         list->fltr_info.flag = ICE_FLTR_TX;
1438         list->fltr_info.src_id = ICE_SRC_ID_VSI;
1439
1440         INIT_LIST_HEAD(&list->list_entry);
1441         list_add(&list->list_entry, &tmp_add_list);
1442
1443         if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
1444                 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
1445                         vid, vsi->vsi_num);
1446                 status = -EIO;
1447         }
1448
1449         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1450         return status;
1451 }
1452
1453 /**
1454  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1455  * @vsi: the VSI being configured
1456  *
1457  * Return 0 on success and a negative value on error
1458  * Configure the Rx VSI for operation.
1459  */
1460 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1461 {
1462         int err = 0;
1463         u16 i;
1464
1465         if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
1466                 vsi->max_frame = vsi->netdev->mtu +
1467                         ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1468         else
1469                 vsi->max_frame = ICE_RXBUF_2048;
1470
1471         vsi->rx_buf_len = ICE_RXBUF_2048;
1472         /* set up individual rings */
1473         for (i = 0; i < vsi->num_rxq && !err; i++)
1474                 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1475
1476         if (err) {
1477                 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
1478                 return -EIO;
1479         }
1480         return err;
1481 }
1482
1483 /**
1484  * ice_vsi_cfg_txqs - Configure the VSI for Tx
1485  * @vsi: the VSI being configured
1486  *
1487  * Return 0 on success and a negative value on error
1488  * Configure the Tx VSI for operation.
1489  */
1490 int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
1491 {
1492         struct ice_aqc_add_tx_qgrp *qg_buf;
1493         struct ice_aqc_add_txqs_perq *txq;
1494         struct ice_pf *pf = vsi->back;
1495         enum ice_status status;
1496         u16 buf_len, i, pf_q;
1497         int err = 0, tc = 0;
1498         u8 num_q_grps;
1499
1500         buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
1501         qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
1502         if (!qg_buf)
1503                 return -ENOMEM;
1504
1505         if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
1506                 err = -EINVAL;
1507                 goto err_cfg_txqs;
1508         }
1509         qg_buf->num_txqs = 1;
1510         num_q_grps = 1;
1511
1512         /* set up and configure the Tx queues */
1513         ice_for_each_txq(vsi, i) {
1514                 struct ice_tlan_ctx tlan_ctx = { 0 };
1515
1516                 pf_q = vsi->txq_map[i];
1517                 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
1518                 /* copy context contents into the qg_buf */
1519                 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
1520                 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
1521                             ice_tlan_ctx_info);
1522
1523                 /* init queue specific tail reg. It is referred as transmit
1524                  * comm scheduler queue doorbell.
1525                  */
1526                 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
1527                 status = ice_ena_vsi_txq(vsi->port_info, vsi->idx, tc,
1528                                          num_q_grps, qg_buf, buf_len, NULL);
1529                 if (status) {
1530                         dev_err(&vsi->back->pdev->dev,
1531                                 "Failed to set LAN Tx queue context, error: %d\n",
1532                                 status);
1533                         err = -ENODEV;
1534                         goto err_cfg_txqs;
1535                 }
1536
1537                 /* Add Tx Queue TEID into the VSI Tx ring from the response
1538                  * This will complete configuring and enabling the queue.
1539                  */
1540                 txq = &qg_buf->txqs[0];
1541                 if (pf_q == le16_to_cpu(txq->txq_id))
1542                         vsi->tx_rings[i]->txq_teid =
1543                                 le32_to_cpu(txq->q_teid);
1544         }
1545 err_cfg_txqs:
1546         devm_kfree(&pf->pdev->dev, qg_buf);
1547         return err;
1548 }
1549
1550 /**
1551  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1552  * @vsi: the VSI being configured
1553  */
1554 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1555 {
1556         struct ice_pf *pf = vsi->back;
1557         u16 vector = vsi->base_vector;
1558         struct ice_hw *hw = &pf->hw;
1559         u32 txq = 0, rxq = 0;
1560         int i, q, itr;
1561         u8 itr_gran;
1562
1563         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1564                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1565
1566                 itr_gran = hw->itr_gran_200;
1567
1568                 if (q_vector->num_ring_rx) {
1569                         q_vector->rx.itr =
1570                                 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1571                                            itr_gran);
1572                         q_vector->rx.latency_range = ICE_LOW_LATENCY;
1573                 }
1574
1575                 if (q_vector->num_ring_tx) {
1576                         q_vector->tx.itr =
1577                                 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1578                                            itr_gran);
1579                         q_vector->tx.latency_range = ICE_LOW_LATENCY;
1580                 }
1581                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1582                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1583
1584                 /* Both Transmit Queue Interrupt Cause Control register
1585                  * and Receive Queue Interrupt Cause control register
1586                  * expects MSIX_INDX field to be the vector index
1587                  * within the function space and not the absolute
1588                  * vector index across PF or across device.
1589                  * For SR-IOV VF VSIs queue vector index always starts
1590                  * with 1 since first vector index(0) is used for OICR
1591                  * in VF space. Since VMDq and other PF VSIs are within
1592                  * the PF function space, use the vector index that is
1593                  * tracked for this PF.
1594                  */
1595                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1596                         u32 val;
1597
1598                         itr = ICE_ITR_NONE;
1599                         val = QINT_TQCTL_CAUSE_ENA_M |
1600                               (itr << QINT_TQCTL_ITR_INDX_S)  |
1601                               (vector << QINT_TQCTL_MSIX_INDX_S);
1602                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1603                         txq++;
1604                 }
1605
1606                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1607                         u32 val;
1608
1609                         itr = ICE_ITR_NONE;
1610                         val = QINT_RQCTL_CAUSE_ENA_M |
1611                               (itr << QINT_RQCTL_ITR_INDX_S)  |
1612                               (vector << QINT_RQCTL_MSIX_INDX_S);
1613                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1614                         rxq++;
1615                 }
1616         }
1617
1618         ice_flush(hw);
1619 }
1620
1621 /**
1622  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1623  * @vsi: the VSI being changed
1624  */
1625 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1626 {
1627         struct device *dev = &vsi->back->pdev->dev;
1628         struct ice_hw *hw = &vsi->back->hw;
1629         struct ice_vsi_ctx ctxt = { 0 };
1630         enum ice_status status;
1631
1632         /* Here we are configuring the VSI to let the driver add VLAN tags by
1633          * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1634          * insertion happens in the Tx hot path, in ice_tx_map.
1635          */
1636         ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1637
1638         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1639
1640         status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1641         if (status) {
1642                 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1643                         status, hw->adminq.sq_last_status);
1644                 return -EIO;
1645         }
1646
1647         vsi->info.vlan_flags = ctxt.info.vlan_flags;
1648         return 0;
1649 }
1650
1651 /**
1652  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1653  * @vsi: the VSI being changed
1654  * @ena: boolean value indicating if this is a enable or disable request
1655  */
1656 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1657 {
1658         struct device *dev = &vsi->back->pdev->dev;
1659         struct ice_hw *hw = &vsi->back->hw;
1660         struct ice_vsi_ctx ctxt = { 0 };
1661         enum ice_status status;
1662
1663         /* Here we are configuring what the VSI should do with the VLAN tag in
1664          * the Rx packet. We can either leave the tag in the packet or put it in
1665          * the Rx descriptor.
1666          */
1667         if (ena) {
1668                 /* Strip VLAN tag from Rx packet and put it in the desc */
1669                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1670         } else {
1671                 /* Disable stripping. Leave tag in packet */
1672                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1673         }
1674
1675         /* Allow all packets untagged/tagged */
1676         ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1677
1678         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1679
1680         status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1681         if (status) {
1682                 dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1683                         ena, status, hw->adminq.sq_last_status);
1684                 return -EIO;
1685         }
1686
1687         vsi->info.vlan_flags = ctxt.info.vlan_flags;
1688         return 0;
1689 }
1690
1691 /**
1692  * ice_vsi_start_rx_rings - start VSI's Rx rings
1693  * @vsi: the VSI whose rings are to be started
1694  *
1695  * Returns 0 on success and a negative value on error
1696  */
1697 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1698 {
1699         return ice_vsi_ctrl_rx_rings(vsi, true);
1700 }
1701
1702 /**
1703  * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1704  * @vsi: the VSI
1705  *
1706  * Returns 0 on success and a negative value on error
1707  */
1708 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1709 {
1710         return ice_vsi_ctrl_rx_rings(vsi, false);
1711 }
1712
1713 /**
1714  * ice_vsi_stop_tx_rings - Disable Tx rings
1715  * @vsi: the VSI being configured
1716  */
1717 int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
1718 {
1719         struct ice_pf *pf = vsi->back;
1720         struct ice_hw *hw = &pf->hw;
1721         enum ice_status status;
1722         u32 *q_teids, val;
1723         u16 *q_ids, i;
1724         int err = 0;
1725
1726         if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1727                 return -EINVAL;
1728
1729         q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
1730                                GFP_KERNEL);
1731         if (!q_teids)
1732                 return -ENOMEM;
1733
1734         q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
1735                              GFP_KERNEL);
1736         if (!q_ids) {
1737                 err = -ENOMEM;
1738                 goto err_alloc_q_ids;
1739         }
1740
1741         /* set up the Tx queue list to be disabled */
1742         ice_for_each_txq(vsi, i) {
1743                 u16 v_idx;
1744
1745                 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
1746                         err = -EINVAL;
1747                         goto err_out;
1748                 }
1749
1750                 q_ids[i] = vsi->txq_map[i];
1751                 q_teids[i] = vsi->tx_rings[i]->txq_teid;
1752
1753                 /* clear cause_ena bit for disabled queues */
1754                 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
1755                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1756                 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
1757
1758                 /* software is expected to wait for 100 ns */
1759                 ndelay(100);
1760
1761                 /* trigger a software interrupt for the vector associated to
1762                  * the queue to schedule NAPI handler
1763                  */
1764                 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
1765                 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
1766                      GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
1767         }
1768         status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
1769                                  NULL);
1770         /* if the disable queue command was exercised during an active reset
1771          * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
1772          * the reset operation disables queues at the hardware level anyway.
1773          */
1774         if (status == ICE_ERR_RESET_ONGOING) {
1775                 dev_info(&pf->pdev->dev,
1776                          "Reset in progress. LAN Tx queues already disabled\n");
1777         } else if (status) {
1778                 dev_err(&pf->pdev->dev,
1779                         "Failed to disable LAN Tx queues, error: %d\n",
1780                         status);
1781                 err = -ENODEV;
1782         }
1783
1784 err_out:
1785         devm_kfree(&pf->pdev->dev, q_ids);
1786
1787 err_alloc_q_ids:
1788         devm_kfree(&pf->pdev->dev, q_teids);
1789
1790         return err;
1791 }
1792
1793 /**
1794  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1795  * @vsi: VSI to enable or disable VLAN pruning on
1796  * @ena: set to true to enable VLAN pruning and false to disable it
1797  *
1798  * returns 0 if VSI is updated, negative otherwise
1799  */
1800 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
1801 {
1802         struct ice_vsi_ctx *ctxt;
1803         struct device *dev;
1804         int status;
1805
1806         if (!vsi)
1807                 return -EINVAL;
1808
1809         dev = &vsi->back->pdev->dev;
1810         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
1811         if (!ctxt)
1812                 return -ENOMEM;
1813
1814         ctxt->info = vsi->info;
1815
1816         if (ena) {
1817                 ctxt->info.sec_flags |=
1818                         ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1819                         ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
1820                 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1821         } else {
1822                 ctxt->info.sec_flags &=
1823                         ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1824                           ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1825                 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1826         }
1827
1828         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
1829                                                 ICE_AQ_VSI_PROP_SW_VALID);
1830
1831         status = ice_update_vsi(&vsi->back->hw, vsi->idx, ctxt, NULL);
1832         if (status) {
1833                 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1834                            ena ? "Ena" : "Dis", vsi->idx, vsi->vsi_num, status,
1835                            vsi->back->hw.adminq.sq_last_status);
1836                 goto err_out;
1837         }
1838
1839         vsi->info.sec_flags = ctxt->info.sec_flags;
1840         vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1841
1842         devm_kfree(dev, ctxt);
1843         return 0;
1844
1845 err_out:
1846         devm_kfree(dev, ctxt);
1847         return -EIO;
1848 }
1849
1850 /**
1851  * ice_vsi_setup - Set up a VSI by a given type
1852  * @pf: board private structure
1853  * @pi: pointer to the port_info instance
1854  * @type: VSI type
1855  * @vf_id: defines VF id to which this VSI connects. This field is meant to be
1856  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
1857  *         fill-in ICE_INVAL_VFID as input.
1858  *
1859  * This allocates the sw VSI structure and its queue resources.
1860  *
1861  * Returns pointer to the successfully allocated and configured VSI sw struct on
1862  * success, NULL on failure.
1863  */
1864 struct ice_vsi *
1865 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1866               enum ice_vsi_type type, u16 __always_unused vf_id)
1867 {
1868         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1869         struct device *dev = &pf->pdev->dev;
1870         struct ice_vsi *vsi;
1871         int ret, i;
1872
1873         vsi = ice_vsi_alloc(pf, type);
1874         if (!vsi) {
1875                 dev_err(dev, "could not allocate VSI\n");
1876                 return NULL;
1877         }
1878
1879         vsi->port_info = pi;
1880         vsi->vsw = pf->first_sw;
1881
1882         if (ice_vsi_get_qs(vsi)) {
1883                 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1884                         vsi->idx);
1885                 goto unroll_get_qs;
1886         }
1887
1888         /* set RSS capabilities */
1889         ice_vsi_set_rss_params(vsi);
1890
1891         /* create the VSI */
1892         ret = ice_vsi_init(vsi);
1893         if (ret)
1894                 goto unroll_get_qs;
1895
1896         switch (vsi->type) {
1897         case ICE_VSI_PF:
1898                 ret = ice_vsi_alloc_q_vectors(vsi);
1899                 if (ret)
1900                         goto unroll_vsi_init;
1901
1902                 ret = ice_vsi_setup_vector_base(vsi);
1903                 if (ret)
1904                         goto unroll_alloc_q_vector;
1905
1906                 ret = ice_vsi_alloc_rings(vsi);
1907                 if (ret)
1908                         goto unroll_vector_base;
1909
1910                 ice_vsi_map_rings_to_vectors(vsi);
1911
1912                 /* Do not exit if configuring RSS had an issue, at least
1913                  * receive traffic on first queue. Hence no need to capture
1914                  * return value
1915                  */
1916                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1917                         ice_vsi_cfg_rss_lut_key(vsi);
1918                 break;
1919         default:
1920                 /* if VSI type is not recognized, clean up the resources and
1921                  * exit
1922                  */
1923                 goto unroll_vsi_init;
1924         }
1925
1926         ice_vsi_set_tc_cfg(vsi);
1927
1928         /* configure VSI nodes based on number of queues and TC's */
1929         for (i = 0; i < vsi->tc_cfg.numtc; i++)
1930                 max_txqs[i] = vsi->num_txq;
1931
1932         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1933                               max_txqs);
1934         if (ret) {
1935                 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
1936                 goto unroll_vector_base;
1937         }
1938
1939         return vsi;
1940
1941 unroll_vector_base:
1942         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
1943 unroll_alloc_q_vector:
1944         ice_vsi_free_q_vectors(vsi);
1945 unroll_vsi_init:
1946         ice_vsi_delete(vsi);
1947 unroll_get_qs:
1948         ice_vsi_put_qs(vsi);
1949         pf->q_left_tx += vsi->alloc_txq;
1950         pf->q_left_rx += vsi->alloc_rxq;
1951         ice_vsi_clear(vsi);
1952
1953         return NULL;
1954 }
1955
1956 /**
1957  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1958  * @vsi: the VSI being cleaned up
1959  */
1960 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1961 {
1962         struct ice_pf *pf = vsi->back;
1963         u16 vector = vsi->base_vector;
1964         struct ice_hw *hw = &pf->hw;
1965         u32 txq = 0;
1966         u32 rxq = 0;
1967         int i, q;
1968
1969         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1970                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1971
1972                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1973                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1974                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1975                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1976                         txq++;
1977                 }
1978
1979                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1980                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1981                         rxq++;
1982                 }
1983         }
1984
1985         ice_flush(hw);
1986 }
1987
1988 /**
1989  * ice_vsi_free_irq - Free the IRQ association with the OS
1990  * @vsi: the VSI being configured
1991  */
1992 void ice_vsi_free_irq(struct ice_vsi *vsi)
1993 {
1994         struct ice_pf *pf = vsi->back;
1995         int base = vsi->base_vector;
1996
1997         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1998                 int i;
1999
2000                 if (!vsi->q_vectors || !vsi->irqs_ready)
2001                         return;
2002
2003                 vsi->irqs_ready = false;
2004                 for (i = 0; i < vsi->num_q_vectors; i++) {
2005                         u16 vector = i + base;
2006                         int irq_num;
2007
2008                         irq_num = pf->msix_entries[vector].vector;
2009
2010                         /* free only the irqs that were actually requested */
2011                         if (!vsi->q_vectors[i] ||
2012                             !(vsi->q_vectors[i]->num_ring_tx ||
2013                               vsi->q_vectors[i]->num_ring_rx))
2014                                 continue;
2015
2016                         /* clear the affinity notifier in the IRQ descriptor */
2017                         irq_set_affinity_notifier(irq_num, NULL);
2018
2019                         /* clear the affinity_mask in the IRQ descriptor */
2020                         irq_set_affinity_hint(irq_num, NULL);
2021                         synchronize_irq(irq_num);
2022                         devm_free_irq(&pf->pdev->dev, irq_num,
2023                                       vsi->q_vectors[i]);
2024                 }
2025                 ice_vsi_release_msix(vsi);
2026         }
2027 }
2028
2029 /**
2030  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2031  * @vsi: the VSI having resources freed
2032  */
2033 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2034 {
2035         int i;
2036
2037         if (!vsi->tx_rings)
2038                 return;
2039
2040         ice_for_each_txq(vsi, i)
2041                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2042                         ice_free_tx_ring(vsi->tx_rings[i]);
2043 }
2044
2045 /**
2046  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2047  * @vsi: the VSI having resources freed
2048  */
2049 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2050 {
2051         int i;
2052
2053         if (!vsi->rx_rings)
2054                 return;
2055
2056         ice_for_each_rxq(vsi, i)
2057                 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2058                         ice_free_rx_ring(vsi->rx_rings[i]);
2059 }
2060
2061 /**
2062  * ice_vsi_close - Shut down a VSI
2063  * @vsi: the VSI being shut down
2064  */
2065 void ice_vsi_close(struct ice_vsi *vsi)
2066 {
2067         if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2068                 ice_down(vsi);
2069
2070         ice_vsi_free_irq(vsi);
2071         ice_vsi_free_tx_rings(vsi);
2072         ice_vsi_free_rx_rings(vsi);
2073 }
2074
2075 /**
2076  * ice_free_res - free a block of resources
2077  * @res: pointer to the resource
2078  * @index: starting index previously returned by ice_get_res
2079  * @id: identifier to track owner
2080  *
2081  * Returns number of resources freed
2082  */
2083 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2084 {
2085         int count = 0;
2086         int i;
2087
2088         if (!res || index >= res->num_entries)
2089                 return -EINVAL;
2090
2091         id |= ICE_RES_VALID_BIT;
2092         for (i = index; i < res->num_entries && res->list[i] == id; i++) {
2093                 res->list[i] = 0;
2094                 count++;
2095         }
2096
2097         return count;
2098 }
2099
2100 /**
2101  * ice_search_res - Search the tracker for a block of resources
2102  * @res: pointer to the resource
2103  * @needed: size of the block needed
2104  * @id: identifier to track owner
2105  *
2106  * Returns the base item index of the block, or -ENOMEM for error
2107  */
2108 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2109 {
2110         int start = res->search_hint;
2111         int end = start;
2112
2113         id |= ICE_RES_VALID_BIT;
2114
2115         do {
2116                 /* skip already allocated entries */
2117                 if (res->list[end++] & ICE_RES_VALID_BIT) {
2118                         start = end;
2119                         if ((start + needed) > res->num_entries)
2120                                 break;
2121                 }
2122
2123                 if (end == (start + needed)) {
2124                         int i = start;
2125
2126                         /* there was enough, so assign it to the requestor */
2127                         while (i != end)
2128                                 res->list[i++] = id;
2129
2130                         if (end == res->num_entries)
2131                                 end = 0;
2132
2133                         res->search_hint = end;
2134                         return start;
2135                 }
2136         } while (1);
2137
2138         return -ENOMEM;
2139 }
2140
2141 /**
2142  * ice_get_res - get a block of resources
2143  * @pf: board private structure
2144  * @res: pointer to the resource
2145  * @needed: size of the block needed
2146  * @id: identifier to track owner
2147  *
2148  * Returns the base item index of the block, or -ENOMEM for error
2149  * The search_hint trick and lack of advanced fit-finding only works
2150  * because we're highly likely to have all the same sized requests.
2151  * Linear search time and any fragmentation should be minimal.
2152  */
2153 int
2154 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2155 {
2156         int ret;
2157
2158         if (!res || !pf)
2159                 return -EINVAL;
2160
2161         if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2162                 dev_err(&pf->pdev->dev,
2163                         "param err: needed=%d, num_entries = %d id=0x%04x\n",
2164                         needed, res->num_entries, id);
2165                 return -EINVAL;
2166         }
2167
2168         /* search based on search_hint */
2169         ret = ice_search_res(res, needed, id);
2170
2171         if (ret < 0) {
2172                 /* previous search failed. Reset search hint and try again */
2173                 res->search_hint = 0;
2174                 ret = ice_search_res(res, needed, id);
2175         }
2176
2177         return ret;
2178 }
2179
2180 /**
2181  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2182  * @vsi: the VSI being un-configured
2183  */
2184 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2185 {
2186         struct ice_pf *pf = vsi->back;
2187         struct ice_hw *hw = &pf->hw;
2188         int base = vsi->base_vector;
2189         u32 val;
2190         int i;
2191
2192         /* disable interrupt causation from each queue */
2193         if (vsi->tx_rings) {
2194                 ice_for_each_txq(vsi, i) {
2195                         if (vsi->tx_rings[i]) {
2196                                 u16 reg;
2197
2198                                 reg = vsi->tx_rings[i]->reg_idx;
2199                                 val = rd32(hw, QINT_TQCTL(reg));
2200                                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2201                                 wr32(hw, QINT_TQCTL(reg), val);
2202                         }
2203                 }
2204         }
2205
2206         if (vsi->rx_rings) {
2207                 ice_for_each_rxq(vsi, i) {
2208                         if (vsi->rx_rings[i]) {
2209                                 u16 reg;
2210
2211                                 reg = vsi->rx_rings[i]->reg_idx;
2212                                 val = rd32(hw, QINT_RQCTL(reg));
2213                                 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2214                                 wr32(hw, QINT_RQCTL(reg), val);
2215                         }
2216                 }
2217         }
2218
2219         /* disable each interrupt */
2220         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2221                 for (i = vsi->base_vector;
2222                      i < (vsi->num_q_vectors + vsi->base_vector); i++)
2223                         wr32(hw, GLINT_DYN_CTL(i), 0);
2224
2225                 ice_flush(hw);
2226                 for (i = 0; i < vsi->num_q_vectors; i++)
2227                         synchronize_irq(pf->msix_entries[i + base].vector);
2228         }
2229 }
2230
2231 /**
2232  * ice_vsi_release - Delete a VSI and free its resources
2233  * @vsi: the VSI being removed
2234  *
2235  * Returns 0 on success or < 0 on error
2236  */
2237 int ice_vsi_release(struct ice_vsi *vsi)
2238 {
2239         struct ice_pf *pf;
2240
2241         if (!vsi->back)
2242                 return -ENODEV;
2243         pf = vsi->back;
2244         /* do not unregister and free netdevs while driver is in the reset
2245          * recovery pending state. Since reset/rebuild happens through PF
2246          * service task workqueue, its not a good idea to unregister netdev
2247          * that is associated to the PF that is running the work queue items
2248          * currently. This is done to avoid check_flush_dependency() warning
2249          * on this wq
2250          */
2251         if (vsi->netdev && !ice_is_reset_in_progress(pf->state)) {
2252                 unregister_netdev(vsi->netdev);
2253                 free_netdev(vsi->netdev);
2254                 vsi->netdev = NULL;
2255         }
2256
2257         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2258                 ice_rss_clean(vsi);
2259
2260         /* Disable VSI and free resources */
2261         ice_vsi_dis_irq(vsi);
2262         ice_vsi_close(vsi);
2263
2264         /* reclaim interrupt vectors back to PF */
2265         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2266         pf->num_avail_msix += vsi->num_q_vectors;
2267
2268         ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2269         ice_vsi_delete(vsi);
2270         ice_vsi_free_q_vectors(vsi);
2271         ice_vsi_clear_rings(vsi);
2272
2273         ice_vsi_put_qs(vsi);
2274         pf->q_left_tx += vsi->alloc_txq;
2275         pf->q_left_rx += vsi->alloc_rxq;
2276
2277         /* retain SW VSI data structure since it is needed to unregister and
2278          * free VSI netdev when PF is not in reset recovery pending state,\
2279          * for ex: during rmmod.
2280          */
2281         if (!ice_is_reset_in_progress(pf->state))
2282                 ice_vsi_clear(vsi);
2283
2284         return 0;
2285 }
2286
2287 /**
2288  * ice_vsi_rebuild - Rebuild VSI after reset
2289  * @vsi: VSI to be rebuild
2290  *
2291  * Returns 0 on success and negative value on failure
2292  */
2293 int ice_vsi_rebuild(struct ice_vsi *vsi)
2294 {
2295         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2296         int ret, i;
2297
2298         if (!vsi)
2299                 return -EINVAL;
2300
2301         ice_vsi_free_q_vectors(vsi);
2302         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2303         vsi->base_vector = 0;
2304         ice_vsi_clear_rings(vsi);
2305         ice_vsi_free_arrays(vsi, false);
2306         ice_vsi_set_num_qs(vsi);
2307
2308         /* Initialize VSI struct elements and create VSI in FW */
2309         ret = ice_vsi_init(vsi);
2310         if (ret < 0)
2311                 goto err_vsi;
2312
2313         ret = ice_vsi_alloc_arrays(vsi, false);
2314         if (ret < 0)
2315                 goto err_vsi;
2316
2317         switch (vsi->type) {
2318         case ICE_VSI_PF:
2319                 ret = ice_vsi_alloc_q_vectors(vsi);
2320                 if (ret)
2321                         goto err_rings;
2322
2323                 ret = ice_vsi_setup_vector_base(vsi);
2324                 if (ret)
2325                         goto err_vectors;
2326
2327                 ret = ice_vsi_alloc_rings(vsi);
2328                 if (ret)
2329                         goto err_vectors;
2330
2331                 ice_vsi_map_rings_to_vectors(vsi);
2332                 break;
2333         default:
2334                 break;
2335         }
2336
2337         ice_vsi_set_tc_cfg(vsi);
2338
2339         /* configure VSI nodes based on number of queues and TC's */
2340         for (i = 0; i < vsi->tc_cfg.numtc; i++)
2341                 max_txqs[i] = vsi->num_txq;
2342
2343         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2344                               max_txqs);
2345         if (ret) {
2346                 dev_info(&vsi->back->pdev->dev,
2347                          "Failed VSI lan queue config\n");
2348                 goto err_vectors;
2349         }
2350         return 0;
2351
2352 err_vectors:
2353         ice_vsi_free_q_vectors(vsi);
2354 err_rings:
2355         if (vsi->netdev) {
2356                 vsi->current_netdev_flags = 0;
2357                 unregister_netdev(vsi->netdev);
2358                 free_netdev(vsi->netdev);
2359                 vsi->netdev = NULL;
2360         }
2361 err_vsi:
2362         ice_vsi_clear(vsi);
2363         set_bit(__ICE_RESET_FAILED, vsi->back->state);
2364         return ret;
2365 }
2366
2367 /**
2368  * ice_is_reset_in_progress - check for a reset in progress
2369  * @state: pf state field
2370  */
2371 bool ice_is_reset_in_progress(unsigned long *state)
2372 {
2373         return test_bit(__ICE_RESET_OICR_RECV, state) ||
2374                test_bit(__ICE_PFR_REQ, state) ||
2375                test_bit(__ICE_CORER_REQ, state) ||
2376                test_bit(__ICE_GLOBR_REQ, state);
2377 }