]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_main.c
ice: update branding strings and supported device ids
[linux.git] / drivers / net / ethernet / intel / ice / ice_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include "ice.h"
9
10 #define DRV_VERSION     "0.7.1-k"
11 #define DRV_SUMMARY     "Intel(R) Ethernet Connection E800 Series Linux Driver"
12 const char ice_drv_ver[] = DRV_VERSION;
13 static const char ice_driver_string[] = DRV_SUMMARY;
14 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
15
16 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
17 MODULE_DESCRIPTION(DRV_SUMMARY);
18 MODULE_LICENSE("GPL v2");
19 MODULE_VERSION(DRV_VERSION);
20
21 static int debug = -1;
22 module_param(debug, int, 0644);
23 #ifndef CONFIG_DYNAMIC_DEBUG
24 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
25 #else
26 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
27 #endif /* !CONFIG_DYNAMIC_DEBUG */
28
29 static struct workqueue_struct *ice_wq;
30 static const struct net_device_ops ice_netdev_ops;
31
32 static void ice_pf_dis_all_vsi(struct ice_pf *pf);
33 static void ice_rebuild(struct ice_pf *pf);
34 static int ice_vsi_release(struct ice_vsi *vsi);
35 static void ice_vsi_release_all(struct ice_pf *pf);
36 static void ice_update_vsi_stats(struct ice_vsi *vsi);
37 static void ice_update_pf_stats(struct ice_pf *pf);
38
39 /**
40  * ice_get_tx_pending - returns number of Tx descriptors not processed
41  * @ring: the ring of descriptors
42  */
43 static u32 ice_get_tx_pending(struct ice_ring *ring)
44 {
45         u32 head, tail;
46
47         head = ring->next_to_clean;
48         tail = readl(ring->tail);
49
50         if (head != tail)
51                 return (head < tail) ?
52                         tail - head : (tail + ring->count - head);
53         return 0;
54 }
55
56 /**
57  * ice_check_for_hang_subtask - check for and recover hung queues
58  * @pf: pointer to PF struct
59  */
60 static void ice_check_for_hang_subtask(struct ice_pf *pf)
61 {
62         struct ice_vsi *vsi = NULL;
63         unsigned int i;
64         u32 v, v_idx;
65         int packets;
66
67         ice_for_each_vsi(pf, v)
68                 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
69                         vsi = pf->vsi[v];
70                         break;
71                 }
72
73         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
74                 return;
75
76         if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
77                 return;
78
79         for (i = 0; i < vsi->num_txq; i++) {
80                 struct ice_ring *tx_ring = vsi->tx_rings[i];
81
82                 if (tx_ring && tx_ring->desc) {
83                         int itr = ICE_ITR_NONE;
84
85                         /* If packet counter has not changed the queue is
86                          * likely stalled, so force an interrupt for this
87                          * queue.
88                          *
89                          * prev_pkt would be negative if there was no
90                          * pending work.
91                          */
92                         packets = tx_ring->stats.pkts & INT_MAX;
93                         if (tx_ring->tx_stats.prev_pkt == packets) {
94                                 /* Trigger sw interrupt to revive the queue */
95                                 v_idx = tx_ring->q_vector->v_idx;
96                                 wr32(&vsi->back->hw,
97                                      GLINT_DYN_CTL(vsi->base_vector + v_idx),
98                                      (itr << GLINT_DYN_CTL_ITR_INDX_S) |
99                                      GLINT_DYN_CTL_SWINT_TRIG_M |
100                                      GLINT_DYN_CTL_INTENA_MSK_M);
101                                 continue;
102                         }
103
104                         /* Memory barrier between read of packet count and call
105                          * to ice_get_tx_pending()
106                          */
107                         smp_rmb();
108                         tx_ring->tx_stats.prev_pkt =
109                             ice_get_tx_pending(tx_ring) ? packets : -1;
110                 }
111         }
112 }
113
114 /**
115  * ice_get_free_slot - get the next non-NULL location index in array
116  * @array: array to search
117  * @size: size of the array
118  * @curr: last known occupied index to be used as a search hint
119  *
120  * void * is being used to keep the functionality generic. This lets us use this
121  * function on any array of pointers.
122  */
123 static int ice_get_free_slot(void *array, int size, int curr)
124 {
125         int **tmp_array = (int **)array;
126         int next;
127
128         if (curr < (size - 1) && !tmp_array[curr + 1]) {
129                 next = curr + 1;
130         } else {
131                 int i = 0;
132
133                 while ((i < size) && (tmp_array[i]))
134                         i++;
135                 if (i == size)
136                         next = ICE_NO_VSI;
137                 else
138                         next = i;
139         }
140         return next;
141 }
142
143 /**
144  * ice_search_res - Search the tracker for a block of resources
145  * @res: pointer to the resource
146  * @needed: size of the block needed
147  * @id: identifier to track owner
148  * Returns the base item index of the block, or -ENOMEM for error
149  */
150 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
151 {
152         int start = res->search_hint;
153         int end = start;
154
155         id |= ICE_RES_VALID_BIT;
156
157         do {
158                 /* skip already allocated entries */
159                 if (res->list[end++] & ICE_RES_VALID_BIT) {
160                         start = end;
161                         if ((start + needed) > res->num_entries)
162                                 break;
163                 }
164
165                 if (end == (start + needed)) {
166                         int i = start;
167
168                         /* there was enough, so assign it to the requestor */
169                         while (i != end)
170                                 res->list[i++] = id;
171
172                         if (end == res->num_entries)
173                                 end = 0;
174
175                         res->search_hint = end;
176                         return start;
177                 }
178         } while (1);
179
180         return -ENOMEM;
181 }
182
183 /**
184  * ice_get_res - get a block of resources
185  * @pf: board private structure
186  * @res: pointer to the resource
187  * @needed: size of the block needed
188  * @id: identifier to track owner
189  *
190  * Returns the base item index of the block, or -ENOMEM for error
191  * The search_hint trick and lack of advanced fit-finding only works
192  * because we're highly likely to have all the same sized requests.
193  * Linear search time and any fragmentation should be minimal.
194  */
195 static int
196 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
197 {
198         int ret;
199
200         if (!res || !pf)
201                 return -EINVAL;
202
203         if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
204                 dev_err(&pf->pdev->dev,
205                         "param err: needed=%d, num_entries = %d id=0x%04x\n",
206                         needed, res->num_entries, id);
207                 return -EINVAL;
208         }
209
210         /* search based on search_hint */
211         ret = ice_search_res(res, needed, id);
212
213         if (ret < 0) {
214                 /* previous search failed. Reset search hint and try again */
215                 res->search_hint = 0;
216                 ret = ice_search_res(res, needed, id);
217         }
218
219         return ret;
220 }
221
222 /**
223  * ice_free_res - free a block of resources
224  * @res: pointer to the resource
225  * @index: starting index previously returned by ice_get_res
226  * @id: identifier to track owner
227  * Returns number of resources freed
228  */
229 static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
230 {
231         int count = 0;
232         int i;
233
234         if (!res || index >= res->num_entries)
235                 return -EINVAL;
236
237         id |= ICE_RES_VALID_BIT;
238         for (i = index; i < res->num_entries && res->list[i] == id; i++) {
239                 res->list[i] = 0;
240                 count++;
241         }
242
243         return count;
244 }
245
246 /**
247  * ice_add_mac_to_list - Add a mac address filter entry to the list
248  * @vsi: the VSI to be forwarded to
249  * @add_list: pointer to the list which contains MAC filter entries
250  * @macaddr: the MAC address to be added.
251  *
252  * Adds mac address filter entry to the temp list
253  *
254  * Returns 0 on success or ENOMEM on failure.
255  */
256 static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
257                                const u8 *macaddr)
258 {
259         struct ice_fltr_list_entry *tmp;
260         struct ice_pf *pf = vsi->back;
261
262         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
263         if (!tmp)
264                 return -ENOMEM;
265
266         tmp->fltr_info.flag = ICE_FLTR_TX;
267         tmp->fltr_info.src = vsi->vsi_num;
268         tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
269         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
270         tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
271         ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
272
273         INIT_LIST_HEAD(&tmp->list_entry);
274         list_add(&tmp->list_entry, add_list);
275
276         return 0;
277 }
278
279 /**
280  * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
281  * @netdev: the net device on which the sync is happening
282  * @addr: mac address to sync
283  *
284  * This is a callback function which is called by the in kernel device sync
285  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
286  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
287  * mac filters from the hardware.
288  */
289 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
290 {
291         struct ice_netdev_priv *np = netdev_priv(netdev);
292         struct ice_vsi *vsi = np->vsi;
293
294         if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
295                 return -EINVAL;
296
297         return 0;
298 }
299
300 /**
301  * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
302  * @netdev: the net device on which the unsync is happening
303  * @addr: mac address to unsync
304  *
305  * This is a callback function which is called by the in kernel device unsync
306  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
307  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
308  * delete the mac filters from the hardware.
309  */
310 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
311 {
312         struct ice_netdev_priv *np = netdev_priv(netdev);
313         struct ice_vsi *vsi = np->vsi;
314
315         if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
316                 return -EINVAL;
317
318         return 0;
319 }
320
321 /**
322  * ice_free_fltr_list - free filter lists helper
323  * @dev: pointer to the device struct
324  * @h: pointer to the list head to be freed
325  *
326  * Helper function to free filter lists previously created using
327  * ice_add_mac_to_list
328  */
329 static void ice_free_fltr_list(struct device *dev, struct list_head *h)
330 {
331         struct ice_fltr_list_entry *e, *tmp;
332
333         list_for_each_entry_safe(e, tmp, h, list_entry) {
334                 list_del(&e->list_entry);
335                 devm_kfree(dev, e);
336         }
337 }
338
339 /**
340  * ice_vsi_fltr_changed - check if filter state changed
341  * @vsi: VSI to be checked
342  *
343  * returns true if filter state has changed, false otherwise.
344  */
345 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
346 {
347         return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
348                test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
349                test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
350 }
351
352 /**
353  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
354  * @vsi: VSI to enable or disable VLAN pruning on
355  * @ena: set to true to enable VLAN pruning and false to disable it
356  *
357  * returns 0 if VSI is updated, negative otherwise
358  */
359 static int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
360 {
361         struct ice_vsi_ctx *ctxt;
362         struct device *dev;
363         int status;
364
365         if (!vsi)
366                 return -EINVAL;
367
368         dev = &vsi->back->pdev->dev;
369         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
370         if (!ctxt)
371                 return -ENOMEM;
372
373         ctxt->info = vsi->info;
374
375         if (ena) {
376                 ctxt->info.sec_flags |=
377                         ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
378                         ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
379                 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
380         } else {
381                 ctxt->info.sec_flags &=
382                         ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
383                           ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
384                 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
385         }
386
387         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
388                                                 ICE_AQ_VSI_PROP_SW_VALID);
389         ctxt->vsi_num = vsi->vsi_num;
390         status = ice_aq_update_vsi(&vsi->back->hw, ctxt, NULL);
391         if (status) {
392                 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI %d failed, err = %d, aq_err = %d\n",
393                            ena ? "Ena" : "Dis", vsi->vsi_num, status,
394                            vsi->back->hw.adminq.sq_last_status);
395                 goto err_out;
396         }
397
398         vsi->info.sec_flags = ctxt->info.sec_flags;
399         vsi->info.sw_flags2 = ctxt->info.sw_flags2;
400
401         devm_kfree(dev, ctxt);
402         return 0;
403
404 err_out:
405         devm_kfree(dev, ctxt);
406         return -EIO;
407 }
408
409 /**
410  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
411  * @vsi: ptr to the VSI
412  *
413  * Push any outstanding VSI filter changes through the AdminQ.
414  */
415 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
416 {
417         struct device *dev = &vsi->back->pdev->dev;
418         struct net_device *netdev = vsi->netdev;
419         bool promisc_forced_on = false;
420         struct ice_pf *pf = vsi->back;
421         struct ice_hw *hw = &pf->hw;
422         enum ice_status status = 0;
423         u32 changed_flags = 0;
424         int err = 0;
425
426         if (!vsi->netdev)
427                 return -EINVAL;
428
429         while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
430                 usleep_range(1000, 2000);
431
432         changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
433         vsi->current_netdev_flags = vsi->netdev->flags;
434
435         INIT_LIST_HEAD(&vsi->tmp_sync_list);
436         INIT_LIST_HEAD(&vsi->tmp_unsync_list);
437
438         if (ice_vsi_fltr_changed(vsi)) {
439                 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
440                 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
441                 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
442
443                 /* grab the netdev's addr_list_lock */
444                 netif_addr_lock_bh(netdev);
445                 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
446                               ice_add_mac_to_unsync_list);
447                 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
448                               ice_add_mac_to_unsync_list);
449                 /* our temp lists are populated. release lock */
450                 netif_addr_unlock_bh(netdev);
451         }
452
453         /* Remove mac addresses in the unsync list */
454         status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
455         ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
456         if (status) {
457                 netdev_err(netdev, "Failed to delete MAC filters\n");
458                 /* if we failed because of alloc failures, just bail */
459                 if (status == ICE_ERR_NO_MEMORY) {
460                         err = -ENOMEM;
461                         goto out;
462                 }
463         }
464
465         /* Add mac addresses in the sync list */
466         status = ice_add_mac(hw, &vsi->tmp_sync_list);
467         ice_free_fltr_list(dev, &vsi->tmp_sync_list);
468         if (status) {
469                 netdev_err(netdev, "Failed to add MAC filters\n");
470                 /* If there is no more space for new umac filters, vsi
471                  * should go into promiscuous mode. There should be some
472                  * space reserved for promiscuous filters.
473                  */
474                 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
475                     !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
476                                       vsi->state)) {
477                         promisc_forced_on = true;
478                         netdev_warn(netdev,
479                                     "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
480                                     vsi->vsi_num);
481                 } else {
482                         err = -EIO;
483                         goto out;
484                 }
485         }
486         /* check for changes in promiscuous modes */
487         if (changed_flags & IFF_ALLMULTI)
488                 netdev_warn(netdev, "Unsupported configuration\n");
489
490         if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
491             test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
492                 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
493                 if (vsi->current_netdev_flags & IFF_PROMISC) {
494                         /* Apply TX filter rule to get traffic from VMs */
495                         status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
496                                                   ICE_FLTR_TX);
497                         if (status) {
498                                 netdev_err(netdev, "Error setting default VSI %i tx rule\n",
499                                            vsi->vsi_num);
500                                 vsi->current_netdev_flags &= ~IFF_PROMISC;
501                                 err = -EIO;
502                                 goto out_promisc;
503                         }
504                         /* Apply RX filter rule to get traffic from wire */
505                         status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
506                                                   ICE_FLTR_RX);
507                         if (status) {
508                                 netdev_err(netdev, "Error setting default VSI %i rx rule\n",
509                                            vsi->vsi_num);
510                                 vsi->current_netdev_flags &= ~IFF_PROMISC;
511                                 err = -EIO;
512                                 goto out_promisc;
513                         }
514                 } else {
515                         /* Clear TX filter rule to stop traffic from VMs */
516                         status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
517                                                   ICE_FLTR_TX);
518                         if (status) {
519                                 netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
520                                            vsi->vsi_num);
521                                 vsi->current_netdev_flags |= IFF_PROMISC;
522                                 err = -EIO;
523                                 goto out_promisc;
524                         }
525                         /* Clear filter RX to remove traffic from wire */
526                         status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
527                                                   ICE_FLTR_RX);
528                         if (status) {
529                                 netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
530                                            vsi->vsi_num);
531                                 vsi->current_netdev_flags |= IFF_PROMISC;
532                                 err = -EIO;
533                                 goto out_promisc;
534                         }
535                 }
536         }
537         goto exit;
538
539 out_promisc:
540         set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
541         goto exit;
542 out:
543         /* if something went wrong then set the changed flag so we try again */
544         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
545         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
546 exit:
547         clear_bit(__ICE_CFG_BUSY, vsi->state);
548         return err;
549 }
550
551 /**
552  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
553  * @pf: board private structure
554  */
555 static void ice_sync_fltr_subtask(struct ice_pf *pf)
556 {
557         int v;
558
559         if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
560                 return;
561
562         clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
563
564         for (v = 0; v < pf->num_alloc_vsi; v++)
565                 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
566                     ice_vsi_sync_fltr(pf->vsi[v])) {
567                         /* come back and try again later */
568                         set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
569                         break;
570                 }
571 }
572
573 /**
574  * ice_is_reset_recovery_pending - schedule a reset
575  * @state: pf state field
576  */
577 static bool ice_is_reset_recovery_pending(unsigned long int *state)
578 {
579         return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
580 }
581
582 /**
583  * ice_prepare_for_reset - prep for the core to reset
584  * @pf: board private structure
585  *
586  * Inform or close all dependent features in prep for reset.
587  */
588 static void
589 ice_prepare_for_reset(struct ice_pf *pf)
590 {
591         struct ice_hw *hw = &pf->hw;
592
593         /* disable the VSIs and their queues that are not already DOWN */
594         ice_pf_dis_all_vsi(pf);
595
596         ice_shutdown_all_ctrlq(hw);
597
598         set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
599 }
600
601 /**
602  * ice_do_reset - Initiate one of many types of resets
603  * @pf: board private structure
604  * @reset_type: reset type requested
605  * before this function was called.
606  */
607 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
608 {
609         struct device *dev = &pf->pdev->dev;
610         struct ice_hw *hw = &pf->hw;
611
612         dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
613         WARN_ON(in_interrupt());
614
615         /* PFR is a bit of a special case because it doesn't result in an OICR
616          * interrupt. Set pending bit here which otherwise gets set in the
617          * OICR handler.
618          */
619         if (reset_type == ICE_RESET_PFR)
620                 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
621
622         ice_prepare_for_reset(pf);
623
624         /* trigger the reset */
625         if (ice_reset(hw, reset_type)) {
626                 dev_err(dev, "reset %d failed\n", reset_type);
627                 set_bit(__ICE_RESET_FAILED, pf->state);
628                 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
629                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
630                 return;
631         }
632
633         /* PFR is a bit of a special case because it doesn't result in an OICR
634          * interrupt. So for PFR, rebuild after the reset and clear the reset-
635          * associated state bits.
636          */
637         if (reset_type == ICE_RESET_PFR) {
638                 pf->pfr_count++;
639                 ice_rebuild(pf);
640                 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
641                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
642         }
643 }
644
645 /**
646  * ice_reset_subtask - Set up for resetting the device and driver
647  * @pf: board private structure
648  */
649 static void ice_reset_subtask(struct ice_pf *pf)
650 {
651         enum ice_reset_req reset_type = ICE_RESET_INVAL;
652
653         /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
654          * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
655          * of reset is pending and sets bits in pf->state indicating the reset
656          * type and __ICE_RESET_RECOVERY_PENDING.  So, if the latter bit is set
657          * prepare for pending reset if not already (for PF software-initiated
658          * global resets the software should already be prepared for it as
659          * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
660          * by firmware or software on other PFs, that bit is not set so prepare
661          * for the reset now), poll for reset done, rebuild and return.
662          */
663         if (ice_is_reset_recovery_pending(pf->state)) {
664                 clear_bit(__ICE_GLOBR_RECV, pf->state);
665                 clear_bit(__ICE_CORER_RECV, pf->state);
666                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
667                         ice_prepare_for_reset(pf);
668
669                 /* make sure we are ready to rebuild */
670                 if (ice_check_reset(&pf->hw)) {
671                         set_bit(__ICE_RESET_FAILED, pf->state);
672                 } else {
673                         /* done with reset. start rebuild */
674                         pf->hw.reset_ongoing = false;
675                         ice_rebuild(pf);
676                         /* clear bit to resume normal operations, but
677                          * ICE_NEEDS_RESTART bit is set incase rebuild failed
678                          */
679                         clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
680                         clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
681                 }
682
683                 return;
684         }
685
686         /* No pending resets to finish processing. Check for new resets */
687         if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
688                 reset_type = ICE_RESET_PFR;
689         if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
690                 reset_type = ICE_RESET_CORER;
691         if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
692                 reset_type = ICE_RESET_GLOBR;
693         /* If no valid reset type requested just return */
694         if (reset_type == ICE_RESET_INVAL)
695                 return;
696
697         /* reset if not already down or busy */
698         if (!test_bit(__ICE_DOWN, pf->state) &&
699             !test_bit(__ICE_CFG_BUSY, pf->state)) {
700                 ice_do_reset(pf, reset_type);
701         }
702 }
703
704 /**
705  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
706  * @pf: board private structure
707  */
708 static void ice_watchdog_subtask(struct ice_pf *pf)
709 {
710         int i;
711
712         /* if interface is down do nothing */
713         if (test_bit(__ICE_DOWN, pf->state) ||
714             test_bit(__ICE_CFG_BUSY, pf->state))
715                 return;
716
717         /* make sure we don't do these things too often */
718         if (time_before(jiffies,
719                         pf->serv_tmr_prev + pf->serv_tmr_period))
720                 return;
721
722         pf->serv_tmr_prev = jiffies;
723
724         /* Update the stats for active netdevs so the network stack
725          * can look at updated numbers whenever it cares to
726          */
727         ice_update_pf_stats(pf);
728         for (i = 0; i < pf->num_alloc_vsi; i++)
729                 if (pf->vsi[i] && pf->vsi[i]->netdev)
730                         ice_update_vsi_stats(pf->vsi[i]);
731 }
732
733 /**
734  * ice_print_link_msg - print link up or down message
735  * @vsi: the VSI whose link status is being queried
736  * @isup: boolean for if the link is now up or down
737  */
738 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
739 {
740         const char *speed;
741         const char *fc;
742
743         if (vsi->current_isup == isup)
744                 return;
745
746         vsi->current_isup = isup;
747
748         if (!isup) {
749                 netdev_info(vsi->netdev, "NIC Link is Down\n");
750                 return;
751         }
752
753         switch (vsi->port_info->phy.link_info.link_speed) {
754         case ICE_AQ_LINK_SPEED_40GB:
755                 speed = "40 G";
756                 break;
757         case ICE_AQ_LINK_SPEED_25GB:
758                 speed = "25 G";
759                 break;
760         case ICE_AQ_LINK_SPEED_20GB:
761                 speed = "20 G";
762                 break;
763         case ICE_AQ_LINK_SPEED_10GB:
764                 speed = "10 G";
765                 break;
766         case ICE_AQ_LINK_SPEED_5GB:
767                 speed = "5 G";
768                 break;
769         case ICE_AQ_LINK_SPEED_2500MB:
770                 speed = "2.5 G";
771                 break;
772         case ICE_AQ_LINK_SPEED_1000MB:
773                 speed = "1 G";
774                 break;
775         case ICE_AQ_LINK_SPEED_100MB:
776                 speed = "100 M";
777                 break;
778         default:
779                 speed = "Unknown";
780                 break;
781         }
782
783         switch (vsi->port_info->fc.current_mode) {
784         case ICE_FC_FULL:
785                 fc = "RX/TX";
786                 break;
787         case ICE_FC_TX_PAUSE:
788                 fc = "TX";
789                 break;
790         case ICE_FC_RX_PAUSE:
791                 fc = "RX";
792                 break;
793         default:
794                 fc = "Unknown";
795                 break;
796         }
797
798         netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
799                     speed, fc);
800 }
801
802 /**
803  * ice_init_link_events - enable/initialize link events
804  * @pi: pointer to the port_info instance
805  *
806  * Returns -EIO on failure, 0 on success
807  */
808 static int ice_init_link_events(struct ice_port_info *pi)
809 {
810         u16 mask;
811
812         mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
813                        ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
814
815         if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
816                 dev_dbg(ice_hw_to_dev(pi->hw),
817                         "Failed to set link event mask for port %d\n",
818                         pi->lport);
819                 return -EIO;
820         }
821
822         if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
823                 dev_dbg(ice_hw_to_dev(pi->hw),
824                         "Failed to enable link events for port %d\n",
825                         pi->lport);
826                 return -EIO;
827         }
828
829         return 0;
830 }
831
832 /**
833  * ice_vsi_link_event - update the vsi's netdev
834  * @vsi: the vsi on which the link event occurred
835  * @link_up: whether or not the vsi needs to be set up or down
836  */
837 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
838 {
839         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
840                 return;
841
842         if (vsi->type == ICE_VSI_PF) {
843                 if (!vsi->netdev) {
844                         dev_dbg(&vsi->back->pdev->dev,
845                                 "vsi->netdev is not initialized!\n");
846                         return;
847                 }
848                 if (link_up) {
849                         netif_carrier_on(vsi->netdev);
850                         netif_tx_wake_all_queues(vsi->netdev);
851                 } else {
852                         netif_carrier_off(vsi->netdev);
853                         netif_tx_stop_all_queues(vsi->netdev);
854                 }
855         }
856 }
857
858 /**
859  * ice_link_event - process the link event
860  * @pf: pf that the link event is associated with
861  * @pi: port_info for the port that the link event is associated with
862  *
863  * Returns -EIO if ice_get_link_status() fails
864  * Returns 0 on success
865  */
866 static int
867 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
868 {
869         u8 new_link_speed, old_link_speed;
870         struct ice_phy_info *phy_info;
871         bool new_link_same_as_old;
872         bool new_link, old_link;
873         u8 lport;
874         u16 v;
875
876         phy_info = &pi->phy;
877         phy_info->link_info_old = phy_info->link_info;
878         /* Force ice_get_link_status() to update link info */
879         phy_info->get_link_info = true;
880
881         old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
882         old_link_speed = phy_info->link_info_old.link_speed;
883
884         lport = pi->lport;
885         if (ice_get_link_status(pi, &new_link)) {
886                 dev_dbg(&pf->pdev->dev,
887                         "Could not get link status for port %d\n", lport);
888                 return -EIO;
889         }
890
891         new_link_speed = phy_info->link_info.link_speed;
892
893         new_link_same_as_old = (new_link == old_link &&
894                                 new_link_speed == old_link_speed);
895
896         ice_for_each_vsi(pf, v) {
897                 struct ice_vsi *vsi = pf->vsi[v];
898
899                 if (!vsi || !vsi->port_info)
900                         continue;
901
902                 if (new_link_same_as_old &&
903                     (test_bit(__ICE_DOWN, vsi->state) ||
904                     new_link == netif_carrier_ok(vsi->netdev)))
905                         continue;
906
907                 if (vsi->port_info->lport == lport) {
908                         ice_print_link_msg(vsi, new_link);
909                         ice_vsi_link_event(vsi, new_link);
910                 }
911         }
912
913         return 0;
914 }
915
916 /**
917  * ice_handle_link_event - handle link event via ARQ
918  * @pf: pf that the link event is associated with
919  *
920  * Return -EINVAL if port_info is null
921  * Return status on succes
922  */
923 static int ice_handle_link_event(struct ice_pf *pf)
924 {
925         struct ice_port_info *port_info;
926         int status;
927
928         port_info = pf->hw.port_info;
929         if (!port_info)
930                 return -EINVAL;
931
932         status = ice_link_event(pf, port_info);
933         if (status)
934                 dev_dbg(&pf->pdev->dev,
935                         "Could not process link event, error %d\n", status);
936
937         return status;
938 }
939
940 /**
941  * __ice_clean_ctrlq - helper function to clean controlq rings
942  * @pf: ptr to struct ice_pf
943  * @q_type: specific Control queue type
944  */
945 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
946 {
947         struct ice_rq_event_info event;
948         struct ice_hw *hw = &pf->hw;
949         struct ice_ctl_q_info *cq;
950         u16 pending, i = 0;
951         const char *qtype;
952         u32 oldval, val;
953
954         /* Do not clean control queue if/when PF reset fails */
955         if (test_bit(__ICE_RESET_FAILED, pf->state))
956                 return 0;
957
958         switch (q_type) {
959         case ICE_CTL_Q_ADMIN:
960                 cq = &hw->adminq;
961                 qtype = "Admin";
962                 break;
963         default:
964                 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
965                          q_type);
966                 return 0;
967         }
968
969         /* check for error indications - PF_xx_AxQLEN register layout for
970          * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
971          */
972         val = rd32(hw, cq->rq.len);
973         if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
974                    PF_FW_ARQLEN_ARQCRIT_M)) {
975                 oldval = val;
976                 if (val & PF_FW_ARQLEN_ARQVFE_M)
977                         dev_dbg(&pf->pdev->dev,
978                                 "%s Receive Queue VF Error detected\n", qtype);
979                 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
980                         dev_dbg(&pf->pdev->dev,
981                                 "%s Receive Queue Overflow Error detected\n",
982                                 qtype);
983                 }
984                 if (val & PF_FW_ARQLEN_ARQCRIT_M)
985                         dev_dbg(&pf->pdev->dev,
986                                 "%s Receive Queue Critical Error detected\n",
987                                 qtype);
988                 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
989                          PF_FW_ARQLEN_ARQCRIT_M);
990                 if (oldval != val)
991                         wr32(hw, cq->rq.len, val);
992         }
993
994         val = rd32(hw, cq->sq.len);
995         if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
996                    PF_FW_ATQLEN_ATQCRIT_M)) {
997                 oldval = val;
998                 if (val & PF_FW_ATQLEN_ATQVFE_M)
999                         dev_dbg(&pf->pdev->dev,
1000                                 "%s Send Queue VF Error detected\n", qtype);
1001                 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1002                         dev_dbg(&pf->pdev->dev,
1003                                 "%s Send Queue Overflow Error detected\n",
1004                                 qtype);
1005                 }
1006                 if (val & PF_FW_ATQLEN_ATQCRIT_M)
1007                         dev_dbg(&pf->pdev->dev,
1008                                 "%s Send Queue Critical Error detected\n",
1009                                 qtype);
1010                 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1011                          PF_FW_ATQLEN_ATQCRIT_M);
1012                 if (oldval != val)
1013                         wr32(hw, cq->sq.len, val);
1014         }
1015
1016         event.buf_len = cq->rq_buf_size;
1017         event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
1018                                      GFP_KERNEL);
1019         if (!event.msg_buf)
1020                 return 0;
1021
1022         do {
1023                 enum ice_status ret;
1024                 u16 opcode;
1025
1026                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1027                 if (ret == ICE_ERR_AQ_NO_WORK)
1028                         break;
1029                 if (ret) {
1030                         dev_err(&pf->pdev->dev,
1031                                 "%s Receive Queue event error %d\n", qtype,
1032                                 ret);
1033                         break;
1034                 }
1035
1036                 opcode = le16_to_cpu(event.desc.opcode);
1037
1038                 switch (opcode) {
1039                 case ice_aqc_opc_get_link_status:
1040                         if (ice_handle_link_event(pf))
1041                                 dev_err(&pf->pdev->dev,
1042                                         "Could not handle link event\n");
1043                         break;
1044                 case ice_aqc_opc_fw_logging:
1045                         ice_output_fw_log(hw, &event.desc, event.msg_buf);
1046                         break;
1047                 default:
1048                         dev_dbg(&pf->pdev->dev,
1049                                 "%s Receive Queue unknown event 0x%04x ignored\n",
1050                                 qtype, opcode);
1051                         break;
1052                 }
1053         } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1054
1055         devm_kfree(&pf->pdev->dev, event.msg_buf);
1056
1057         return pending && (i == ICE_DFLT_IRQ_WORK);
1058 }
1059
1060 /**
1061  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1062  * @hw: pointer to hardware info
1063  * @cq: control queue information
1064  *
1065  * returns true if there are pending messages in a queue, false if there aren't
1066  */
1067 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1068 {
1069         u16 ntu;
1070
1071         ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1072         return cq->rq.next_to_clean != ntu;
1073 }
1074
1075 /**
1076  * ice_clean_adminq_subtask - clean the AdminQ rings
1077  * @pf: board private structure
1078  */
1079 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1080 {
1081         struct ice_hw *hw = &pf->hw;
1082
1083         if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1084                 return;
1085
1086         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1087                 return;
1088
1089         clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1090
1091         /* There might be a situation where new messages arrive to a control
1092          * queue between processing the last message and clearing the
1093          * EVENT_PENDING bit. So before exiting, check queue head again (using
1094          * ice_ctrlq_pending) and process new messages if any.
1095          */
1096         if (ice_ctrlq_pending(hw, &hw->adminq))
1097                 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1098
1099         ice_flush(hw);
1100 }
1101
1102 /**
1103  * ice_service_task_schedule - schedule the service task to wake up
1104  * @pf: board private structure
1105  *
1106  * If not already scheduled, this puts the task into the work queue.
1107  */
1108 static void ice_service_task_schedule(struct ice_pf *pf)
1109 {
1110         if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1111             !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1112             !test_bit(__ICE_NEEDS_RESTART, pf->state))
1113                 queue_work(ice_wq, &pf->serv_task);
1114 }
1115
1116 /**
1117  * ice_service_task_complete - finish up the service task
1118  * @pf: board private structure
1119  */
1120 static void ice_service_task_complete(struct ice_pf *pf)
1121 {
1122         WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1123
1124         /* force memory (pf->state) to sync before next service task */
1125         smp_mb__before_atomic();
1126         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1127 }
1128
1129 /**
1130  * ice_service_task_stop - stop service task and cancel works
1131  * @pf: board private structure
1132  */
1133 static void ice_service_task_stop(struct ice_pf *pf)
1134 {
1135         set_bit(__ICE_SERVICE_DIS, pf->state);
1136
1137         if (pf->serv_tmr.function)
1138                 del_timer_sync(&pf->serv_tmr);
1139         if (pf->serv_task.func)
1140                 cancel_work_sync(&pf->serv_task);
1141
1142         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1143 }
1144
1145 /**
1146  * ice_service_timer - timer callback to schedule service task
1147  * @t: pointer to timer_list
1148  */
1149 static void ice_service_timer(struct timer_list *t)
1150 {
1151         struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1152
1153         mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1154         ice_service_task_schedule(pf);
1155 }
1156
1157 /**
1158  * ice_handle_mdd_event - handle malicious driver detect event
1159  * @pf: pointer to the PF structure
1160  *
1161  * Called from service task. OICR interrupt handler indicates MDD event
1162  */
1163 static void ice_handle_mdd_event(struct ice_pf *pf)
1164 {
1165         struct ice_hw *hw = &pf->hw;
1166         bool mdd_detected = false;
1167         u32 reg;
1168
1169         if (!test_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1170                 return;
1171
1172         /* find what triggered the MDD event */
1173         reg = rd32(hw, GL_MDET_TX_PQM);
1174         if (reg & GL_MDET_TX_PQM_VALID_M) {
1175                 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1176                                 GL_MDET_TX_PQM_PF_NUM_S;
1177                 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1178                                 GL_MDET_TX_PQM_VF_NUM_S;
1179                 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1180                                 GL_MDET_TX_PQM_MAL_TYPE_S;
1181                 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1182                                 GL_MDET_TX_PQM_QNUM_S);
1183
1184                 if (netif_msg_tx_err(pf))
1185                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1186                                  event, queue, pf_num, vf_num);
1187                 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1188                 mdd_detected = true;
1189         }
1190
1191         reg = rd32(hw, GL_MDET_TX_TCLAN);
1192         if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1193                 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1194                                 GL_MDET_TX_TCLAN_PF_NUM_S;
1195                 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1196                                 GL_MDET_TX_TCLAN_VF_NUM_S;
1197                 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1198                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1199                 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1200                                 GL_MDET_TX_TCLAN_QNUM_S);
1201
1202                 if (netif_msg_rx_err(pf))
1203                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1204                                  event, queue, pf_num, vf_num);
1205                 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1206                 mdd_detected = true;
1207         }
1208
1209         reg = rd32(hw, GL_MDET_RX);
1210         if (reg & GL_MDET_RX_VALID_M) {
1211                 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1212                                 GL_MDET_RX_PF_NUM_S;
1213                 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1214                                 GL_MDET_RX_VF_NUM_S;
1215                 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1216                                 GL_MDET_RX_MAL_TYPE_S;
1217                 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1218                                 GL_MDET_RX_QNUM_S);
1219
1220                 if (netif_msg_rx_err(pf))
1221                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1222                                  event, queue, pf_num, vf_num);
1223                 wr32(hw, GL_MDET_RX, 0xffffffff);
1224                 mdd_detected = true;
1225         }
1226
1227         if (mdd_detected) {
1228                 bool pf_mdd_detected = false;
1229
1230                 reg = rd32(hw, PF_MDET_TX_PQM);
1231                 if (reg & PF_MDET_TX_PQM_VALID_M) {
1232                         wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1233                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1234                         pf_mdd_detected = true;
1235                 }
1236
1237                 reg = rd32(hw, PF_MDET_TX_TCLAN);
1238                 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1239                         wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1240                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1241                         pf_mdd_detected = true;
1242                 }
1243
1244                 reg = rd32(hw, PF_MDET_RX);
1245                 if (reg & PF_MDET_RX_VALID_M) {
1246                         wr32(hw, PF_MDET_RX, 0xFFFF);
1247                         dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1248                         pf_mdd_detected = true;
1249                 }
1250                 /* Queue belongs to the PF initiate a reset */
1251                 if (pf_mdd_detected) {
1252                         set_bit(__ICE_NEEDS_RESTART, pf->state);
1253                         ice_service_task_schedule(pf);
1254                 }
1255         }
1256
1257         /* re-enable MDD interrupt cause */
1258         clear_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1259         reg = rd32(hw, PFINT_OICR_ENA);
1260         reg |= PFINT_OICR_MAL_DETECT_M;
1261         wr32(hw, PFINT_OICR_ENA, reg);
1262         ice_flush(hw);
1263 }
1264
1265 /**
1266  * ice_service_task - manage and run subtasks
1267  * @work: pointer to work_struct contained by the PF struct
1268  */
1269 static void ice_service_task(struct work_struct *work)
1270 {
1271         struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1272         unsigned long start_time = jiffies;
1273
1274         /* subtasks */
1275
1276         /* process reset requests first */
1277         ice_reset_subtask(pf);
1278
1279         /* bail if a reset/recovery cycle is pending or rebuild failed */
1280         if (ice_is_reset_recovery_pending(pf->state) ||
1281             test_bit(__ICE_SUSPENDED, pf->state) ||
1282             test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1283                 ice_service_task_complete(pf);
1284                 return;
1285         }
1286
1287         ice_check_for_hang_subtask(pf);
1288         ice_sync_fltr_subtask(pf);
1289         ice_handle_mdd_event(pf);
1290         ice_watchdog_subtask(pf);
1291         ice_clean_adminq_subtask(pf);
1292
1293         /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1294         ice_service_task_complete(pf);
1295
1296         /* If the tasks have taken longer than one service timer period
1297          * or there is more work to be done, reset the service timer to
1298          * schedule the service task now.
1299          */
1300         if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1301             test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1302             test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1303                 mod_timer(&pf->serv_tmr, jiffies);
1304 }
1305
1306 /**
1307  * ice_set_ctrlq_len - helper function to set controlq length
1308  * @hw: pointer to the hw instance
1309  */
1310 static void ice_set_ctrlq_len(struct ice_hw *hw)
1311 {
1312         hw->adminq.num_rq_entries = ICE_AQ_LEN;
1313         hw->adminq.num_sq_entries = ICE_AQ_LEN;
1314         hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1315         hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1316 }
1317
1318 /**
1319  * ice_irq_affinity_notify - Callback for affinity changes
1320  * @notify: context as to what irq was changed
1321  * @mask: the new affinity mask
1322  *
1323  * This is a callback function used by the irq_set_affinity_notifier function
1324  * so that we may register to receive changes to the irq affinity masks.
1325  */
1326 static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1327                                     const cpumask_t *mask)
1328 {
1329         struct ice_q_vector *q_vector =
1330                 container_of(notify, struct ice_q_vector, affinity_notify);
1331
1332         cpumask_copy(&q_vector->affinity_mask, mask);
1333 }
1334
1335 /**
1336  * ice_irq_affinity_release - Callback for affinity notifier release
1337  * @ref: internal core kernel usage
1338  *
1339  * This is a callback function used by the irq_set_affinity_notifier function
1340  * to inform the current notification subscriber that they will no longer
1341  * receive notifications.
1342  */
1343 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1344
1345 /**
1346  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
1347  * @vsi: the VSI being un-configured
1348  */
1349 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
1350 {
1351         struct ice_pf *pf = vsi->back;
1352         struct ice_hw *hw = &pf->hw;
1353         int base = vsi->base_vector;
1354         u32 val;
1355         int i;
1356
1357         /* disable interrupt causation from each queue */
1358         if (vsi->tx_rings) {
1359                 ice_for_each_txq(vsi, i) {
1360                         if (vsi->tx_rings[i]) {
1361                                 u16 reg;
1362
1363                                 reg = vsi->tx_rings[i]->reg_idx;
1364                                 val = rd32(hw, QINT_TQCTL(reg));
1365                                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1366                                 wr32(hw, QINT_TQCTL(reg), val);
1367                         }
1368                 }
1369         }
1370
1371         if (vsi->rx_rings) {
1372                 ice_for_each_rxq(vsi, i) {
1373                         if (vsi->rx_rings[i]) {
1374                                 u16 reg;
1375
1376                                 reg = vsi->rx_rings[i]->reg_idx;
1377                                 val = rd32(hw, QINT_RQCTL(reg));
1378                                 val &= ~QINT_RQCTL_CAUSE_ENA_M;
1379                                 wr32(hw, QINT_RQCTL(reg), val);
1380                         }
1381                 }
1382         }
1383
1384         /* disable each interrupt */
1385         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1386                 for (i = vsi->base_vector;
1387                      i < (vsi->num_q_vectors + vsi->base_vector); i++)
1388                         wr32(hw, GLINT_DYN_CTL(i), 0);
1389
1390                 ice_flush(hw);
1391                 for (i = 0; i < vsi->num_q_vectors; i++)
1392                         synchronize_irq(pf->msix_entries[i + base].vector);
1393         }
1394 }
1395
1396 /**
1397  * ice_vsi_ena_irq - Enable IRQ for the given VSI
1398  * @vsi: the VSI being configured
1399  */
1400 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1401 {
1402         struct ice_pf *pf = vsi->back;
1403         struct ice_hw *hw = &pf->hw;
1404
1405         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1406                 int i;
1407
1408                 for (i = 0; i < vsi->num_q_vectors; i++)
1409                         ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1410         }
1411
1412         ice_flush(hw);
1413         return 0;
1414 }
1415
1416 /**
1417  * ice_vsi_delete - delete a VSI from the switch
1418  * @vsi: pointer to VSI being removed
1419  */
1420 static void ice_vsi_delete(struct ice_vsi *vsi)
1421 {
1422         struct ice_pf *pf = vsi->back;
1423         struct ice_vsi_ctx ctxt;
1424         enum ice_status status;
1425
1426         ctxt.vsi_num = vsi->vsi_num;
1427
1428         memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
1429
1430         status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
1431         if (status)
1432                 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
1433                         vsi->vsi_num);
1434 }
1435
1436 /**
1437  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1438  * @vsi: the VSI being configured
1439  * @basename: name for the vector
1440  */
1441 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1442 {
1443         int q_vectors = vsi->num_q_vectors;
1444         struct ice_pf *pf = vsi->back;
1445         int base = vsi->base_vector;
1446         int rx_int_idx = 0;
1447         int tx_int_idx = 0;
1448         int vector, err;
1449         int irq_num;
1450
1451         for (vector = 0; vector < q_vectors; vector++) {
1452                 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1453
1454                 irq_num = pf->msix_entries[base + vector].vector;
1455
1456                 if (q_vector->tx.ring && q_vector->rx.ring) {
1457                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1458                                  "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1459                         tx_int_idx++;
1460                 } else if (q_vector->rx.ring) {
1461                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1462                                  "%s-%s-%d", basename, "rx", rx_int_idx++);
1463                 } else if (q_vector->tx.ring) {
1464                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1465                                  "%s-%s-%d", basename, "tx", tx_int_idx++);
1466                 } else {
1467                         /* skip this unused q_vector */
1468                         continue;
1469                 }
1470                 err = devm_request_irq(&pf->pdev->dev,
1471                                        pf->msix_entries[base + vector].vector,
1472                                        vsi->irq_handler, 0, q_vector->name,
1473                                        q_vector);
1474                 if (err) {
1475                         netdev_err(vsi->netdev,
1476                                    "MSIX request_irq failed, error: %d\n", err);
1477                         goto free_q_irqs;
1478                 }
1479
1480                 /* register for affinity change notifications */
1481                 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1482                 q_vector->affinity_notify.release = ice_irq_affinity_release;
1483                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1484
1485                 /* assign the mask for this irq */
1486                 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1487         }
1488
1489         vsi->irqs_ready = true;
1490         return 0;
1491
1492 free_q_irqs:
1493         while (vector) {
1494                 vector--;
1495                 irq_num = pf->msix_entries[base + vector].vector,
1496                 irq_set_affinity_notifier(irq_num, NULL);
1497                 irq_set_affinity_hint(irq_num, NULL);
1498                 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1499         }
1500         return err;
1501 }
1502
1503 /**
1504  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
1505  * @vsi: the VSI being configured
1506  */
1507 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
1508 {
1509         struct ice_hw_common_caps *cap;
1510         struct ice_pf *pf = vsi->back;
1511
1512         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1513                 vsi->rss_size = 1;
1514                 return;
1515         }
1516
1517         cap = &pf->hw.func_caps.common_cap;
1518         switch (vsi->type) {
1519         case ICE_VSI_PF:
1520                 /* PF VSI will inherit RSS instance of PF */
1521                 vsi->rss_table_size = cap->rss_table_size;
1522                 vsi->rss_size = min_t(int, num_online_cpus(),
1523                                       BIT(cap->rss_table_entry_width));
1524                 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
1525                 break;
1526         default:
1527                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1528                 break;
1529         }
1530 }
1531
1532 /**
1533  * ice_vsi_setup_q_map - Setup a VSI queue map
1534  * @vsi: the VSI being configured
1535  * @ctxt: VSI context structure
1536  */
1537 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1538 {
1539         u16 offset = 0, qmap = 0, numq_tc;
1540         u16 pow = 0, max_rss = 0, qcount;
1541         u16 qcount_tx = vsi->alloc_txq;
1542         u16 qcount_rx = vsi->alloc_rxq;
1543         bool ena_tc0 = false;
1544         int i;
1545
1546         /* at least TC0 should be enabled by default */
1547         if (vsi->tc_cfg.numtc) {
1548                 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
1549                         ena_tc0 =  true;
1550         } else {
1551                 ena_tc0 =  true;
1552         }
1553
1554         if (ena_tc0) {
1555                 vsi->tc_cfg.numtc++;
1556                 vsi->tc_cfg.ena_tc |= 1;
1557         }
1558
1559         numq_tc = qcount_rx / vsi->tc_cfg.numtc;
1560
1561         /* TC mapping is a function of the number of Rx queues assigned to the
1562          * VSI for each traffic class and the offset of these queues.
1563          * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1564          * queues allocated to TC0. No:of queues is a power-of-2.
1565          *
1566          * If TC is not enabled, the queue offset is set to 0, and allocate one
1567          * queue, this way, traffic for the given TC will be sent to the default
1568          * queue.
1569          *
1570          * Setup number and offset of Rx queues for all TCs for the VSI
1571          */
1572
1573         /* qcount will change if RSS is enabled */
1574         if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
1575                 if (vsi->type == ICE_VSI_PF)
1576                         max_rss = ICE_MAX_LG_RSS_QS;
1577                 else
1578                         max_rss = ICE_MAX_SMALL_RSS_QS;
1579
1580                 qcount = min_t(int, numq_tc, max_rss);
1581                 qcount = min_t(int, qcount, vsi->rss_size);
1582         } else {
1583                 qcount = numq_tc;
1584         }
1585
1586         /* find the (rounded up) power-of-2 of qcount */
1587         pow = order_base_2(qcount);
1588
1589         for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1590                 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1591                         /* TC is not enabled */
1592                         vsi->tc_cfg.tc_info[i].qoffset = 0;
1593                         vsi->tc_cfg.tc_info[i].qcount = 1;
1594                         ctxt->info.tc_mapping[i] = 0;
1595                         continue;
1596                 }
1597
1598                 /* TC is enabled */
1599                 vsi->tc_cfg.tc_info[i].qoffset = offset;
1600                 vsi->tc_cfg.tc_info[i].qcount = qcount;
1601
1602                 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1603                         ICE_AQ_VSI_TC_Q_OFFSET_M) |
1604                         ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1605                          ICE_AQ_VSI_TC_Q_NUM_M);
1606                 offset += qcount;
1607                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1608         }
1609
1610         vsi->num_txq = qcount_tx;
1611         vsi->num_rxq = offset;
1612
1613         /* Rx queue mapping */
1614         ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1615         /* q_mapping buffer holds the info for the first queue allocated for
1616          * this VSI in the PF space and also the number of queues associated
1617          * with this VSI.
1618          */
1619         ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1620         ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1621 }
1622
1623 /**
1624  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
1625  * @ctxt: the VSI context being set
1626  *
1627  * This initializes a default VSI context for all sections except the Queues.
1628  */
1629 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
1630 {
1631         u32 table = 0;
1632
1633         memset(&ctxt->info, 0, sizeof(ctxt->info));
1634         /* VSI's should be allocated from shared pool */
1635         ctxt->alloc_from_pool = true;
1636         /* Src pruning enabled by default */
1637         ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
1638         /* Traffic from VSI can be sent to LAN */
1639         ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1640
1641         /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
1642          * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
1643          * packets untagged/tagged.
1644          */
1645         ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
1646                                   ICE_AQ_VSI_VLAN_MODE_M) >>
1647                                  ICE_AQ_VSI_VLAN_MODE_S);
1648
1649         /* Have 1:1 UP mapping for both ingress/egress tables */
1650         table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1651         table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1652         table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1653         table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1654         table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1655         table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1656         table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1657         table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1658         ctxt->info.ingress_table = cpu_to_le32(table);
1659         ctxt->info.egress_table = cpu_to_le32(table);
1660         /* Have 1:1 UP mapping for outer to inner UP table */
1661         ctxt->info.outer_up_table = cpu_to_le32(table);
1662         /* No Outer tag support outer_tag_flags remains to zero */
1663 }
1664
1665 /**
1666  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1667  * @ctxt: the VSI context being set
1668  * @vsi: the VSI being configured
1669  */
1670 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1671 {
1672         u8 lut_type, hash_type;
1673
1674         switch (vsi->type) {
1675         case ICE_VSI_PF:
1676                 /* PF VSI will inherit RSS instance of PF */
1677                 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1678                 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1679                 break;
1680         default:
1681                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1682                          vsi->type);
1683                 return;
1684         }
1685
1686         ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1687                                 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1688                                 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1689                                  ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1690 }
1691
1692 /**
1693  * ice_vsi_init - Create and initialize a VSI
1694  * @vsi: the VSI being configured
1695  *
1696  * This initializes a VSI context depending on the VSI type to be added and
1697  * passes it down to the add_vsi aq command to create a new VSI.
1698  */
1699 static int ice_vsi_init(struct ice_vsi *vsi)
1700 {
1701         struct ice_vsi_ctx ctxt = { 0 };
1702         struct ice_pf *pf = vsi->back;
1703         struct ice_hw *hw = &pf->hw;
1704         int ret = 0;
1705
1706         switch (vsi->type) {
1707         case ICE_VSI_PF:
1708                 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
1709                 break;
1710         default:
1711                 return -ENODEV;
1712         }
1713
1714         ice_set_dflt_vsi_ctx(&ctxt);
1715         /* if the switch is in VEB mode, allow VSI loopback */
1716         if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1717                 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1718
1719         /* Set LUT type and HASH type if RSS is enabled */
1720         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1721                 ice_set_rss_vsi_ctx(&ctxt, vsi);
1722
1723         ctxt.info.sw_id = vsi->port_info->sw_id;
1724         ice_vsi_setup_q_map(vsi, &ctxt);
1725
1726         ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
1727         if (ret) {
1728                 dev_err(&pf->pdev->dev,
1729                         "Add VSI failed, err %d\n", ret);
1730                 return -EIO;
1731         }
1732
1733         /* keep context for update VSI operations */
1734         vsi->info = ctxt.info;
1735
1736         /* record VSI number returned */
1737         vsi->vsi_num = ctxt.vsi_num;
1738
1739         return ret;
1740 }
1741
1742 /**
1743  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1744  * @vsi: the VSI being cleaned up
1745  */
1746 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1747 {
1748         struct ice_pf *pf = vsi->back;
1749         u16 vector = vsi->base_vector;
1750         struct ice_hw *hw = &pf->hw;
1751         u32 txq = 0;
1752         u32 rxq = 0;
1753         int i, q;
1754
1755         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1756                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1757
1758                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1759                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1760                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1761                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1762                         txq++;
1763                 }
1764
1765                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1766                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1767                         rxq++;
1768                 }
1769         }
1770
1771         ice_flush(hw);
1772 }
1773
1774 /**
1775  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1776  * @vsi: the VSI having rings deallocated
1777  */
1778 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1779 {
1780         int i;
1781
1782         if (vsi->tx_rings) {
1783                 for (i = 0; i < vsi->alloc_txq; i++) {
1784                         if (vsi->tx_rings[i]) {
1785                                 kfree_rcu(vsi->tx_rings[i], rcu);
1786                                 vsi->tx_rings[i] = NULL;
1787                         }
1788                 }
1789         }
1790         if (vsi->rx_rings) {
1791                 for (i = 0; i < vsi->alloc_rxq; i++) {
1792                         if (vsi->rx_rings[i]) {
1793                                 kfree_rcu(vsi->rx_rings[i], rcu);
1794                                 vsi->rx_rings[i] = NULL;
1795                         }
1796                 }
1797         }
1798 }
1799
1800 /**
1801  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1802  * @vsi: VSI which is having rings allocated
1803  */
1804 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1805 {
1806         struct ice_pf *pf = vsi->back;
1807         int i;
1808
1809         /* Allocate tx_rings */
1810         for (i = 0; i < vsi->alloc_txq; i++) {
1811                 struct ice_ring *ring;
1812
1813                 /* allocate with kzalloc(), free with kfree_rcu() */
1814                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1815
1816                 if (!ring)
1817                         goto err_out;
1818
1819                 ring->q_index = i;
1820                 ring->reg_idx = vsi->txq_map[i];
1821                 ring->ring_active = false;
1822                 ring->vsi = vsi;
1823                 ring->netdev = vsi->netdev;
1824                 ring->dev = &pf->pdev->dev;
1825                 ring->count = vsi->num_desc;
1826
1827                 vsi->tx_rings[i] = ring;
1828         }
1829
1830         /* Allocate rx_rings */
1831         for (i = 0; i < vsi->alloc_rxq; i++) {
1832                 struct ice_ring *ring;
1833
1834                 /* allocate with kzalloc(), free with kfree_rcu() */
1835                 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1836                 if (!ring)
1837                         goto err_out;
1838
1839                 ring->q_index = i;
1840                 ring->reg_idx = vsi->rxq_map[i];
1841                 ring->ring_active = false;
1842                 ring->vsi = vsi;
1843                 ring->netdev = vsi->netdev;
1844                 ring->dev = &pf->pdev->dev;
1845                 ring->count = vsi->num_desc;
1846                 vsi->rx_rings[i] = ring;
1847         }
1848
1849         return 0;
1850
1851 err_out:
1852         ice_vsi_clear_rings(vsi);
1853         return -ENOMEM;
1854 }
1855
1856 /**
1857  * ice_vsi_free_irq - Free the irq association with the OS
1858  * @vsi: the VSI being configured
1859  */
1860 static void ice_vsi_free_irq(struct ice_vsi *vsi)
1861 {
1862         struct ice_pf *pf = vsi->back;
1863         int base = vsi->base_vector;
1864
1865         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1866                 int i;
1867
1868                 if (!vsi->q_vectors || !vsi->irqs_ready)
1869                         return;
1870
1871                 vsi->irqs_ready = false;
1872                 for (i = 0; i < vsi->num_q_vectors; i++) {
1873                         u16 vector = i + base;
1874                         int irq_num;
1875
1876                         irq_num = pf->msix_entries[vector].vector;
1877
1878                         /* free only the irqs that were actually requested */
1879                         if (!vsi->q_vectors[i] ||
1880                             !(vsi->q_vectors[i]->num_ring_tx ||
1881                               vsi->q_vectors[i]->num_ring_rx))
1882                                 continue;
1883
1884                         /* clear the affinity notifier in the IRQ descriptor */
1885                         irq_set_affinity_notifier(irq_num, NULL);
1886
1887                         /* clear the affinity_mask in the IRQ descriptor */
1888                         irq_set_affinity_hint(irq_num, NULL);
1889                         synchronize_irq(irq_num);
1890                         devm_free_irq(&pf->pdev->dev, irq_num,
1891                                       vsi->q_vectors[i]);
1892                 }
1893                 ice_vsi_release_msix(vsi);
1894         }
1895 }
1896
1897 /**
1898  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1899  * @vsi: the VSI being configured
1900  */
1901 static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1902 {
1903         struct ice_pf *pf = vsi->back;
1904         u16 vector = vsi->base_vector;
1905         struct ice_hw *hw = &pf->hw;
1906         u32 txq = 0, rxq = 0;
1907         int i, q, itr;
1908         u8 itr_gran;
1909
1910         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1911                 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1912
1913                 itr_gran = hw->itr_gran_200;
1914
1915                 if (q_vector->num_ring_rx) {
1916                         q_vector->rx.itr =
1917                                 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1918                                            itr_gran);
1919                         q_vector->rx.latency_range = ICE_LOW_LATENCY;
1920                 }
1921
1922                 if (q_vector->num_ring_tx) {
1923                         q_vector->tx.itr =
1924                                 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1925                                            itr_gran);
1926                         q_vector->tx.latency_range = ICE_LOW_LATENCY;
1927                 }
1928                 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1929                 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1930
1931                 /* Both Transmit Queue Interrupt Cause Control register
1932                  * and Receive Queue Interrupt Cause control register
1933                  * expects MSIX_INDX field to be the vector index
1934                  * within the function space and not the absolute
1935                  * vector index across PF or across device.
1936                  * For SR-IOV VF VSIs queue vector index always starts
1937                  * with 1 since first vector index(0) is used for OICR
1938                  * in VF space. Since VMDq and other PF VSIs are withtin
1939                  * the PF function space, use the vector index thats
1940                  * tracked for this PF.
1941                  */
1942                 for (q = 0; q < q_vector->num_ring_tx; q++) {
1943                         u32 val;
1944
1945                         itr = ICE_TX_ITR;
1946                         val = QINT_TQCTL_CAUSE_ENA_M |
1947                               (itr << QINT_TQCTL_ITR_INDX_S)  |
1948                               (vector << QINT_TQCTL_MSIX_INDX_S);
1949                         wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1950                         txq++;
1951                 }
1952
1953                 for (q = 0; q < q_vector->num_ring_rx; q++) {
1954                         u32 val;
1955
1956                         itr = ICE_RX_ITR;
1957                         val = QINT_RQCTL_CAUSE_ENA_M |
1958                               (itr << QINT_RQCTL_ITR_INDX_S)  |
1959                               (vector << QINT_RQCTL_MSIX_INDX_S);
1960                         wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1961                         rxq++;
1962                 }
1963         }
1964
1965         ice_flush(hw);
1966 }
1967
1968 /**
1969  * ice_ena_misc_vector - enable the non-queue interrupts
1970  * @pf: board private structure
1971  */
1972 static void ice_ena_misc_vector(struct ice_pf *pf)
1973 {
1974         struct ice_hw *hw = &pf->hw;
1975         u32 val;
1976
1977         /* clear things first */
1978         wr32(hw, PFINT_OICR_ENA, 0);    /* disable all */
1979         rd32(hw, PFINT_OICR);           /* read to clear */
1980
1981         val = (PFINT_OICR_ECC_ERR_M |
1982                PFINT_OICR_MAL_DETECT_M |
1983                PFINT_OICR_GRST_M |
1984                PFINT_OICR_PCI_EXCEPTION_M |
1985                PFINT_OICR_HMC_ERR_M |
1986                PFINT_OICR_PE_CRITERR_M);
1987
1988         wr32(hw, PFINT_OICR_ENA, val);
1989
1990         /* SW_ITR_IDX = 0, but don't change INTENA */
1991         wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1992              GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1993 }
1994
1995 /**
1996  * ice_misc_intr - misc interrupt handler
1997  * @irq: interrupt number
1998  * @data: pointer to a q_vector
1999  */
2000 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2001 {
2002         struct ice_pf *pf = (struct ice_pf *)data;
2003         struct ice_hw *hw = &pf->hw;
2004         irqreturn_t ret = IRQ_NONE;
2005         u32 oicr, ena_mask;
2006
2007         set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2008
2009         oicr = rd32(hw, PFINT_OICR);
2010         ena_mask = rd32(hw, PFINT_OICR_ENA);
2011
2012         if (oicr & PFINT_OICR_MAL_DETECT_M) {
2013                 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2014                 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2015         }
2016
2017         if (oicr & PFINT_OICR_GRST_M) {
2018                 u32 reset;
2019
2020                 /* we have a reset warning */
2021                 ena_mask &= ~PFINT_OICR_GRST_M;
2022                 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2023                         GLGEN_RSTAT_RESET_TYPE_S;
2024
2025                 if (reset == ICE_RESET_CORER)
2026                         pf->corer_count++;
2027                 else if (reset == ICE_RESET_GLOBR)
2028                         pf->globr_count++;
2029                 else
2030                         pf->empr_count++;
2031
2032                 /* If a reset cycle isn't already in progress, we set a bit in
2033                  * pf->state so that the service task can start a reset/rebuild.
2034                  * We also make note of which reset happened so that peer
2035                  * devices/drivers can be informed.
2036                  */
2037                 if (!test_and_set_bit(__ICE_RESET_RECOVERY_PENDING,
2038                                       pf->state)) {
2039                         if (reset == ICE_RESET_CORER)
2040                                 set_bit(__ICE_CORER_RECV, pf->state);
2041                         else if (reset == ICE_RESET_GLOBR)
2042                                 set_bit(__ICE_GLOBR_RECV, pf->state);
2043                         else
2044                                 set_bit(__ICE_EMPR_RECV, pf->state);
2045
2046                         /* There are couple of different bits at play here.
2047                          * hw->reset_ongoing indicates whether the hardware is
2048                          * in reset. This is set to true when a reset interrupt
2049                          * is received and set back to false after the driver
2050                          * has determined that the hardware is out of reset.
2051                          *
2052                          * __ICE_RESET_RECOVERY_PENDING in pf->state indicates
2053                          * that a post reset rebuild is required before the
2054                          * driver is operational again. This is set above.
2055                          *
2056                          * As this is the start of the reset/rebuild cycle, set
2057                          * both to indicate that.
2058                          */
2059                         hw->reset_ongoing = true;
2060                 }
2061         }
2062
2063         if (oicr & PFINT_OICR_HMC_ERR_M) {
2064                 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2065                 dev_dbg(&pf->pdev->dev,
2066                         "HMC Error interrupt - info 0x%x, data 0x%x\n",
2067                         rd32(hw, PFHMC_ERRORINFO),
2068                         rd32(hw, PFHMC_ERRORDATA));
2069         }
2070
2071         /* Report and mask off any remaining unexpected interrupts */
2072         oicr &= ena_mask;
2073         if (oicr) {
2074                 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
2075                         oicr);
2076                 /* If a critical error is pending there is no choice but to
2077                  * reset the device.
2078                  */
2079                 if (oicr & (PFINT_OICR_PE_CRITERR_M |
2080                             PFINT_OICR_PCI_EXCEPTION_M |
2081                             PFINT_OICR_ECC_ERR_M)) {
2082                         set_bit(__ICE_PFR_REQ, pf->state);
2083                         ice_service_task_schedule(pf);
2084                 }
2085                 ena_mask &= ~oicr;
2086         }
2087         ret = IRQ_HANDLED;
2088
2089         /* re-enable interrupt causes that are not handled during this pass */
2090         wr32(hw, PFINT_OICR_ENA, ena_mask);
2091         if (!test_bit(__ICE_DOWN, pf->state)) {
2092                 ice_service_task_schedule(pf);
2093                 ice_irq_dynamic_ena(hw, NULL, NULL);
2094         }
2095
2096         return ret;
2097 }
2098
2099 /**
2100  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
2101  * @vsi: the VSI being configured
2102  *
2103  * This function maps descriptor rings to the queue-specific vectors allotted
2104  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
2105  * and Rx rings to the vector as "efficiently" as possible.
2106  */
2107 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
2108 {
2109         int q_vectors = vsi->num_q_vectors;
2110         int tx_rings_rem, rx_rings_rem;
2111         int v_id;
2112
2113         /* initially assigning remaining rings count to VSIs num queue value */
2114         tx_rings_rem = vsi->num_txq;
2115         rx_rings_rem = vsi->num_rxq;
2116
2117         for (v_id = 0; v_id < q_vectors; v_id++) {
2118                 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
2119                 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
2120
2121                 /* Tx rings mapping to vector */
2122                 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
2123                 q_vector->num_ring_tx = tx_rings_per_v;
2124                 q_vector->tx.ring = NULL;
2125                 q_base = vsi->num_txq - tx_rings_rem;
2126
2127                 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
2128                         struct ice_ring *tx_ring = vsi->tx_rings[q_id];
2129
2130                         tx_ring->q_vector = q_vector;
2131                         tx_ring->next = q_vector->tx.ring;
2132                         q_vector->tx.ring = tx_ring;
2133                 }
2134                 tx_rings_rem -= tx_rings_per_v;
2135
2136                 /* Rx rings mapping to vector */
2137                 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
2138                 q_vector->num_ring_rx = rx_rings_per_v;
2139                 q_vector->rx.ring = NULL;
2140                 q_base = vsi->num_rxq - rx_rings_rem;
2141
2142                 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
2143                         struct ice_ring *rx_ring = vsi->rx_rings[q_id];
2144
2145                         rx_ring->q_vector = q_vector;
2146                         rx_ring->next = q_vector->rx.ring;
2147                         q_vector->rx.ring = rx_ring;
2148                 }
2149                 rx_rings_rem -= rx_rings_per_v;
2150         }
2151 }
2152
2153 /**
2154  * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
2155  * @vsi: the VSI being configured
2156  *
2157  * Return 0 on success and a negative value on error
2158  */
2159 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
2160 {
2161         struct ice_pf *pf = vsi->back;
2162
2163         switch (vsi->type) {
2164         case ICE_VSI_PF:
2165                 vsi->alloc_txq = pf->num_lan_tx;
2166                 vsi->alloc_rxq = pf->num_lan_rx;
2167                 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
2168                 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
2169                 break;
2170         default:
2171                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2172                          vsi->type);
2173                 break;
2174         }
2175 }
2176
2177 /**
2178  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
2179  * @vsi: VSI pointer
2180  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
2181  *
2182  * On error: returns error code (negative)
2183  * On success: returns 0
2184  */
2185 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
2186 {
2187         struct ice_pf *pf = vsi->back;
2188
2189         /* allocate memory for both Tx and Rx ring pointers */
2190         vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
2191                                      sizeof(struct ice_ring *), GFP_KERNEL);
2192         if (!vsi->tx_rings)
2193                 goto err_txrings;
2194
2195         vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
2196                                      sizeof(struct ice_ring *), GFP_KERNEL);
2197         if (!vsi->rx_rings)
2198                 goto err_rxrings;
2199
2200         if (alloc_qvectors) {
2201                 /* allocate memory for q_vector pointers */
2202                 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
2203                                               vsi->num_q_vectors,
2204                                               sizeof(struct ice_q_vector *),
2205                                               GFP_KERNEL);
2206                 if (!vsi->q_vectors)
2207                         goto err_vectors;
2208         }
2209
2210         return 0;
2211
2212 err_vectors:
2213         devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2214 err_rxrings:
2215         devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2216 err_txrings:
2217         return -ENOMEM;
2218 }
2219
2220 /**
2221  * ice_msix_clean_rings - MSIX mode Interrupt Handler
2222  * @irq: interrupt number
2223  * @data: pointer to a q_vector
2224  */
2225 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
2226 {
2227         struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
2228
2229         if (!q_vector->tx.ring && !q_vector->rx.ring)
2230                 return IRQ_HANDLED;
2231
2232         napi_schedule(&q_vector->napi);
2233
2234         return IRQ_HANDLED;
2235 }
2236
2237 /**
2238  * ice_vsi_alloc - Allocates the next available struct vsi in the PF
2239  * @pf: board private structure
2240  * @type: type of VSI
2241  *
2242  * returns a pointer to a VSI on success, NULL on failure.
2243  */
2244 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
2245 {
2246         struct ice_vsi *vsi = NULL;
2247
2248         /* Need to protect the allocation of the VSIs at the PF level */
2249         mutex_lock(&pf->sw_mutex);
2250
2251         /* If we have already allocated our maximum number of VSIs,
2252          * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
2253          * is available to be populated
2254          */
2255         if (pf->next_vsi == ICE_NO_VSI) {
2256                 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
2257                 goto unlock_pf;
2258         }
2259
2260         vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
2261         if (!vsi)
2262                 goto unlock_pf;
2263
2264         vsi->type = type;
2265         vsi->back = pf;
2266         set_bit(__ICE_DOWN, vsi->state);
2267         vsi->idx = pf->next_vsi;
2268         vsi->work_lmt = ICE_DFLT_IRQ_WORK;
2269
2270         ice_vsi_set_num_qs(vsi);
2271
2272         switch (vsi->type) {
2273         case ICE_VSI_PF:
2274                 if (ice_vsi_alloc_arrays(vsi, true))
2275                         goto err_rings;
2276
2277                 /* Setup default MSIX irq handler for VSI */
2278                 vsi->irq_handler = ice_msix_clean_rings;
2279                 break;
2280         default:
2281                 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
2282                 goto unlock_pf;
2283         }
2284
2285         /* fill VSI slot in the PF struct */
2286         pf->vsi[pf->next_vsi] = vsi;
2287
2288         /* prepare pf->next_vsi for next use */
2289         pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
2290                                          pf->next_vsi);
2291         goto unlock_pf;
2292
2293 err_rings:
2294         devm_kfree(&pf->pdev->dev, vsi);
2295         vsi = NULL;
2296 unlock_pf:
2297         mutex_unlock(&pf->sw_mutex);
2298         return vsi;
2299 }
2300
2301 /**
2302  * ice_free_irq_msix_misc - Unroll misc vector setup
2303  * @pf: board private structure
2304  */
2305 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2306 {
2307         /* disable OICR interrupt */
2308         wr32(&pf->hw, PFINT_OICR_ENA, 0);
2309         ice_flush(&pf->hw);
2310
2311         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
2312                 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2313                 devm_free_irq(&pf->pdev->dev,
2314                               pf->msix_entries[pf->oicr_idx].vector, pf);
2315         }
2316
2317         ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2318 }
2319
2320 /**
2321  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2322  * @pf: board private structure
2323  *
2324  * This sets up the handler for MSIX 0, which is used to manage the
2325  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
2326  * when in MSI or Legacy interrupt mode.
2327  */
2328 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2329 {
2330         struct ice_hw *hw = &pf->hw;
2331         int oicr_idx, err = 0;
2332         u8 itr_gran;
2333         u32 val;
2334
2335         if (!pf->int_name[0])
2336                 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2337                          dev_driver_string(&pf->pdev->dev),
2338                          dev_name(&pf->pdev->dev));
2339
2340         /* Do not request IRQ but do enable OICR interrupt since settings are
2341          * lost during reset. Note that this function is called only during
2342          * rebuild path and not while reset is in progress.
2343          */
2344         if (ice_is_reset_recovery_pending(pf->state))
2345                 goto skip_req_irq;
2346
2347         /* reserve one vector in irq_tracker for misc interrupts */
2348         oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2349         if (oicr_idx < 0)
2350                 return oicr_idx;
2351
2352         pf->oicr_idx = oicr_idx;
2353
2354         err = devm_request_irq(&pf->pdev->dev,
2355                                pf->msix_entries[pf->oicr_idx].vector,
2356                                ice_misc_intr, 0, pf->int_name, pf);
2357         if (err) {
2358                 dev_err(&pf->pdev->dev,
2359                         "devm_request_irq for %s failed: %d\n",
2360                         pf->int_name, err);
2361                 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2362                 return err;
2363         }
2364
2365 skip_req_irq:
2366         ice_ena_misc_vector(pf);
2367
2368         val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2369                PFINT_OICR_CTL_CAUSE_ENA_M);
2370         wr32(hw, PFINT_OICR_CTL, val);
2371
2372         /* This enables Admin queue Interrupt causes */
2373         val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2374                PFINT_FW_CTL_CAUSE_ENA_M);
2375         wr32(hw, PFINT_FW_CTL, val);
2376
2377         itr_gran = hw->itr_gran_200;
2378
2379         wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2380              ITR_TO_REG(ICE_ITR_8K, itr_gran));
2381
2382         ice_flush(hw);
2383         ice_irq_dynamic_ena(hw, NULL, NULL);
2384
2385         return 0;
2386 }
2387
2388 /**
2389  * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
2390  * @vsi: the VSI getting queues
2391  *
2392  * Return 0 on success and a negative value on error
2393  */
2394 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
2395 {
2396         struct ice_pf *pf = vsi->back;
2397         int offset, ret = 0;
2398
2399         mutex_lock(&pf->avail_q_mutex);
2400         /* look for contiguous block of queues for tx */
2401         offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
2402                                             0, vsi->alloc_txq, 0);
2403         if (offset < ICE_MAX_TXQS) {
2404                 int i;
2405
2406                 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
2407                 for (i = 0; i < vsi->alloc_txq; i++)
2408                         vsi->txq_map[i] = i + offset;
2409         } else {
2410                 ret = -ENOMEM;
2411                 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
2412         }
2413
2414         /* look for contiguous block of queues for rx */
2415         offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
2416                                             0, vsi->alloc_rxq, 0);
2417         if (offset < ICE_MAX_RXQS) {
2418                 int i;
2419
2420                 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
2421                 for (i = 0; i < vsi->alloc_rxq; i++)
2422                         vsi->rxq_map[i] = i + offset;
2423         } else {
2424                 ret = -ENOMEM;
2425                 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
2426         }
2427         mutex_unlock(&pf->avail_q_mutex);
2428
2429         return ret;
2430 }
2431
2432 /**
2433  * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
2434  * @vsi: the VSI getting queues
2435  *
2436  * Return 0 on success and a negative value on error
2437  */
2438 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
2439 {
2440         struct ice_pf *pf = vsi->back;
2441         int i, index = 0;
2442
2443         mutex_lock(&pf->avail_q_mutex);
2444
2445         if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2446                 for (i = 0; i < vsi->alloc_txq; i++) {
2447                         index = find_next_zero_bit(pf->avail_txqs,
2448                                                    ICE_MAX_TXQS, index);
2449                         if (index < ICE_MAX_TXQS) {
2450                                 set_bit(index, pf->avail_txqs);
2451                                 vsi->txq_map[i] = index;
2452                         } else {
2453                                 goto err_scatter_tx;
2454                         }
2455                 }
2456         }
2457
2458         if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2459                 for (i = 0; i < vsi->alloc_rxq; i++) {
2460                         index = find_next_zero_bit(pf->avail_rxqs,
2461                                                    ICE_MAX_RXQS, index);
2462                         if (index < ICE_MAX_RXQS) {
2463                                 set_bit(index, pf->avail_rxqs);
2464                                 vsi->rxq_map[i] = index;
2465                         } else {
2466                                 goto err_scatter_rx;
2467                         }
2468                 }
2469         }
2470
2471         mutex_unlock(&pf->avail_q_mutex);
2472         return 0;
2473
2474 err_scatter_rx:
2475         /* unflag any queues we have grabbed (i is failed position) */
2476         for (index = 0; index < i; index++) {
2477                 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
2478                 vsi->rxq_map[index] = 0;
2479         }
2480         i = vsi->alloc_txq;
2481 err_scatter_tx:
2482         /* i is either position of failed attempt or vsi->alloc_txq */
2483         for (index = 0; index < i; index++) {
2484                 clear_bit(vsi->txq_map[index], pf->avail_txqs);
2485                 vsi->txq_map[index] = 0;
2486         }
2487
2488         mutex_unlock(&pf->avail_q_mutex);
2489         return -ENOMEM;
2490 }
2491
2492 /**
2493  * ice_vsi_get_qs - Assign queues from PF to VSI
2494  * @vsi: the VSI to assign queues to
2495  *
2496  * Returns 0 on success and a negative value on error
2497  */
2498 static int ice_vsi_get_qs(struct ice_vsi *vsi)
2499 {
2500         int ret = 0;
2501
2502         vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
2503         vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
2504
2505         /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
2506          * modes individually to scatter if assigning contiguous queues
2507          * to rx or tx fails
2508          */
2509         ret = ice_vsi_get_qs_contig(vsi);
2510         if (ret < 0) {
2511                 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
2512                         vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
2513                                                ICE_MAX_SCATTER_TXQS);
2514                 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
2515                         vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
2516                                                ICE_MAX_SCATTER_RXQS);
2517                 ret = ice_vsi_get_qs_scatter(vsi);
2518         }
2519
2520         return ret;
2521 }
2522
2523 /**
2524  * ice_vsi_put_qs - Release queues from VSI to PF
2525  * @vsi: the VSI thats going to release queues
2526  */
2527 static void ice_vsi_put_qs(struct ice_vsi *vsi)
2528 {
2529         struct ice_pf *pf = vsi->back;
2530         int i;
2531
2532         mutex_lock(&pf->avail_q_mutex);
2533
2534         for (i = 0; i < vsi->alloc_txq; i++) {
2535                 clear_bit(vsi->txq_map[i], pf->avail_txqs);
2536                 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
2537         }
2538
2539         for (i = 0; i < vsi->alloc_rxq; i++) {
2540                 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
2541                 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
2542         }
2543
2544         mutex_unlock(&pf->avail_q_mutex);
2545 }
2546
2547 /**
2548  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
2549  * @vsi: VSI having the memory freed
2550  * @v_idx: index of the vector to be freed
2551  */
2552 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
2553 {
2554         struct ice_q_vector *q_vector;
2555         struct ice_ring *ring;
2556
2557         if (!vsi->q_vectors[v_idx]) {
2558                 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
2559                         v_idx);
2560                 return;
2561         }
2562         q_vector = vsi->q_vectors[v_idx];
2563
2564         ice_for_each_ring(ring, q_vector->tx)
2565                 ring->q_vector = NULL;
2566         ice_for_each_ring(ring, q_vector->rx)
2567                 ring->q_vector = NULL;
2568
2569         /* only VSI with an associated netdev is set up with NAPI */
2570         if (vsi->netdev)
2571                 netif_napi_del(&q_vector->napi);
2572
2573         devm_kfree(&vsi->back->pdev->dev, q_vector);
2574         vsi->q_vectors[v_idx] = NULL;
2575 }
2576
2577 /**
2578  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
2579  * @vsi: the VSI having memory freed
2580  */
2581 static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
2582 {
2583         int v_idx;
2584
2585         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
2586                 ice_free_q_vector(vsi, v_idx);
2587 }
2588
2589 /**
2590  * ice_cfg_netdev - Setup the netdev flags
2591  * @vsi: the VSI being configured
2592  *
2593  * Returns 0 on success, negative value on failure
2594  */
2595 static int ice_cfg_netdev(struct ice_vsi *vsi)
2596 {
2597         netdev_features_t csumo_features;
2598         netdev_features_t vlano_features;
2599         netdev_features_t dflt_features;
2600         netdev_features_t tso_features;
2601         struct ice_netdev_priv *np;
2602         struct net_device *netdev;
2603         u8 mac_addr[ETH_ALEN];
2604
2605         netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
2606                                     vsi->alloc_txq, vsi->alloc_rxq);
2607         if (!netdev)
2608                 return -ENOMEM;
2609
2610         vsi->netdev = netdev;
2611         np = netdev_priv(netdev);
2612         np->vsi = vsi;
2613
2614         dflt_features = NETIF_F_SG      |
2615                         NETIF_F_HIGHDMA |
2616                         NETIF_F_RXHASH;
2617
2618         csumo_features = NETIF_F_RXCSUM   |
2619                          NETIF_F_IP_CSUM  |
2620                          NETIF_F_IPV6_CSUM;
2621
2622         vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2623                          NETIF_F_HW_VLAN_CTAG_TX     |
2624                          NETIF_F_HW_VLAN_CTAG_RX;
2625
2626         tso_features = NETIF_F_TSO;
2627
2628         /* set features that user can change */
2629         netdev->hw_features = dflt_features | csumo_features |
2630                               vlano_features | tso_features;
2631
2632         /* enable features */
2633         netdev->features |= netdev->hw_features;
2634         /* encap and VLAN devices inherit default, csumo and tso features */
2635         netdev->hw_enc_features |= dflt_features | csumo_features |
2636                                    tso_features;
2637         netdev->vlan_features |= dflt_features | csumo_features |
2638                                  tso_features;
2639
2640         if (vsi->type == ICE_VSI_PF) {
2641                 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
2642                 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2643
2644                 ether_addr_copy(netdev->dev_addr, mac_addr);
2645                 ether_addr_copy(netdev->perm_addr, mac_addr);
2646         }
2647
2648         netdev->priv_flags |= IFF_UNICAST_FLT;
2649
2650         /* assign netdev_ops */
2651         netdev->netdev_ops = &ice_netdev_ops;
2652
2653         /* setup watchdog timeout value to be 5 second */
2654         netdev->watchdog_timeo = 5 * HZ;
2655
2656         ice_set_ethtool_ops(netdev);
2657
2658         netdev->min_mtu = ETH_MIN_MTU;
2659         netdev->max_mtu = ICE_MAX_MTU;
2660
2661         return 0;
2662 }
2663
2664 /**
2665  * ice_vsi_free_arrays - clean up vsi resources
2666  * @vsi: pointer to VSI being cleared
2667  * @free_qvectors: bool to specify if q_vectors should be deallocated
2668  */
2669 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
2670 {
2671         struct ice_pf *pf = vsi->back;
2672
2673         /* free the ring and vector containers */
2674         if (free_qvectors && vsi->q_vectors) {
2675                 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
2676                 vsi->q_vectors = NULL;
2677         }
2678         if (vsi->tx_rings) {
2679                 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2680                 vsi->tx_rings = NULL;
2681         }
2682         if (vsi->rx_rings) {
2683                 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2684                 vsi->rx_rings = NULL;
2685         }
2686 }
2687
2688 /**
2689  * ice_vsi_clear - clean up and deallocate the provided vsi
2690  * @vsi: pointer to VSI being cleared
2691  *
2692  * This deallocates the vsi's queue resources, removes it from the PF's
2693  * VSI array if necessary, and deallocates the VSI
2694  *
2695  * Returns 0 on success, negative on failure
2696  */
2697 static int ice_vsi_clear(struct ice_vsi *vsi)
2698 {
2699         struct ice_pf *pf = NULL;
2700
2701         if (!vsi)
2702                 return 0;
2703
2704         if (!vsi->back)
2705                 return -EINVAL;
2706
2707         pf = vsi->back;
2708
2709         if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
2710                 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
2711                         vsi->idx);
2712                 return -EINVAL;
2713         }
2714
2715         mutex_lock(&pf->sw_mutex);
2716         /* updates the PF for this cleared vsi */
2717
2718         pf->vsi[vsi->idx] = NULL;
2719         if (vsi->idx < pf->next_vsi)
2720                 pf->next_vsi = vsi->idx;
2721
2722         ice_vsi_free_arrays(vsi, true);
2723         mutex_unlock(&pf->sw_mutex);
2724         devm_kfree(&pf->pdev->dev, vsi);
2725
2726         return 0;
2727 }
2728
2729 /**
2730  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
2731  * @vsi: the VSI being configured
2732  * @v_idx: index of the vector in the vsi struct
2733  *
2734  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
2735  */
2736 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
2737 {
2738         struct ice_pf *pf = vsi->back;
2739         struct ice_q_vector *q_vector;
2740
2741         /* allocate q_vector */
2742         q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
2743         if (!q_vector)
2744                 return -ENOMEM;
2745
2746         q_vector->vsi = vsi;
2747         q_vector->v_idx = v_idx;
2748         /* only set affinity_mask if the CPU is online */
2749         if (cpu_online(v_idx))
2750                 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
2751
2752         if (vsi->netdev)
2753                 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
2754                                NAPI_POLL_WEIGHT);
2755         /* tie q_vector and vsi together */
2756         vsi->q_vectors[v_idx] = q_vector;
2757
2758         return 0;
2759 }
2760
2761 /**
2762  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
2763  * @vsi: the VSI being configured
2764  *
2765  * We allocate one q_vector per queue interrupt.  If allocation fails we
2766  * return -ENOMEM.
2767  */
2768 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
2769 {
2770         struct ice_pf *pf = vsi->back;
2771         int v_idx = 0, num_q_vectors;
2772         int err;
2773
2774         if (vsi->q_vectors[0]) {
2775                 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
2776                         vsi->vsi_num);
2777                 return -EEXIST;
2778         }
2779
2780         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2781                 num_q_vectors = vsi->num_q_vectors;
2782         } else {
2783                 err = -EINVAL;
2784                 goto err_out;
2785         }
2786
2787         for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
2788                 err = ice_vsi_alloc_q_vector(vsi, v_idx);
2789                 if (err)
2790                         goto err_out;
2791         }
2792
2793         return 0;
2794
2795 err_out:
2796         while (v_idx--)
2797                 ice_free_q_vector(vsi, v_idx);
2798
2799         dev_err(&pf->pdev->dev,
2800                 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
2801                 vsi->num_q_vectors, vsi->vsi_num, err);
2802         vsi->num_q_vectors = 0;
2803         return err;
2804 }
2805
2806 /**
2807  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
2808  * @vsi: ptr to the VSI
2809  *
2810  * This should only be called after ice_vsi_alloc() which allocates the
2811  * corresponding SW VSI structure and initializes num_queue_pairs for the
2812  * newly allocated VSI.
2813  *
2814  * Returns 0 on success or negative on failure
2815  */
2816 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
2817 {
2818         struct ice_pf *pf = vsi->back;
2819         int num_q_vectors = 0;
2820
2821         if (vsi->base_vector) {
2822                 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
2823                         vsi->vsi_num, vsi->base_vector);
2824                 return -EEXIST;
2825         }
2826
2827         if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2828                 return -ENOENT;
2829
2830         switch (vsi->type) {
2831         case ICE_VSI_PF:
2832                 num_q_vectors = vsi->num_q_vectors;
2833                 break;
2834         default:
2835                 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2836                          vsi->type);
2837                 break;
2838         }
2839
2840         if (num_q_vectors)
2841                 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
2842                                                num_q_vectors, vsi->idx);
2843
2844         if (vsi->base_vector < 0) {
2845                 dev_err(&pf->pdev->dev,
2846                         "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
2847                         num_q_vectors, vsi->vsi_num, vsi->base_vector);
2848                 return -ENOENT;
2849         }
2850
2851         return 0;
2852 }
2853
2854 /**
2855  * ice_fill_rss_lut - Fill the RSS lookup table with default values
2856  * @lut: Lookup table
2857  * @rss_table_size: Lookup table size
2858  * @rss_size: Range of queue number for hashing
2859  */
2860 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2861 {
2862         u16 i;
2863
2864         for (i = 0; i < rss_table_size; i++)
2865                 lut[i] = i % rss_size;
2866 }
2867
2868 /**
2869  * ice_vsi_cfg_rss - Configure RSS params for a VSI
2870  * @vsi: VSI to be configured
2871  */
2872 static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
2873 {
2874         u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
2875         struct ice_aqc_get_set_rss_keys *key;
2876         struct ice_pf *pf = vsi->back;
2877         enum ice_status status;
2878         int err = 0;
2879         u8 *lut;
2880
2881         vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
2882
2883         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2884         if (!lut)
2885                 return -ENOMEM;
2886
2887         if (vsi->rss_lut_user)
2888                 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
2889         else
2890                 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
2891
2892         status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
2893                                     lut, vsi->rss_table_size);
2894
2895         if (status) {
2896                 dev_err(&vsi->back->pdev->dev,
2897                         "set_rss_lut failed, error %d\n", status);
2898                 err = -EIO;
2899                 goto ice_vsi_cfg_rss_exit;
2900         }
2901
2902         key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
2903         if (!key) {
2904                 err = -ENOMEM;
2905                 goto ice_vsi_cfg_rss_exit;
2906         }
2907
2908         if (vsi->rss_hkey_user)
2909                 memcpy(seed, vsi->rss_hkey_user,
2910                        ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2911         else
2912                 netdev_rss_key_fill((void *)seed,
2913                                     ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2914         memcpy(&key->standard_rss_key, seed,
2915                ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2916
2917         status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
2918
2919         if (status) {
2920                 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
2921                         status);
2922                 err = -EIO;
2923         }
2924
2925         devm_kfree(&pf->pdev->dev, key);
2926 ice_vsi_cfg_rss_exit:
2927         devm_kfree(&pf->pdev->dev, lut);
2928         return err;
2929 }
2930
2931 /**
2932  * ice_vsi_rebuild - Rebuild VSI after reset
2933  * @vsi: vsi to be rebuild
2934  *
2935  * Returns 0 on success and negative value on failure
2936  */
2937 static int ice_vsi_rebuild(struct ice_vsi *vsi)
2938 {
2939         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2940         int ret, i;
2941
2942         if (!vsi)
2943                 return -EINVAL;
2944
2945         ice_vsi_free_q_vectors(vsi);
2946         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2947         vsi->base_vector = 0;
2948         ice_vsi_clear_rings(vsi);
2949         ice_vsi_free_arrays(vsi, false);
2950         ice_vsi_set_num_qs(vsi);
2951
2952         /* Initialize VSI struct elements and create VSI in FW */
2953         ret = ice_vsi_init(vsi);
2954         if (ret < 0)
2955                 goto err_vsi;
2956
2957         ret = ice_vsi_alloc_arrays(vsi, false);
2958         if (ret < 0)
2959                 goto err_vsi;
2960
2961         switch (vsi->type) {
2962         case ICE_VSI_PF:
2963                 /* fall through */
2964                 ret = ice_vsi_alloc_q_vectors(vsi);
2965                 if (ret)
2966                         goto err_rings;
2967
2968                 ret = ice_vsi_setup_vector_base(vsi);
2969                 if (ret)
2970                         goto err_vectors;
2971
2972                 ret = ice_vsi_alloc_rings(vsi);
2973                 if (ret)
2974                         goto err_vectors;
2975
2976                 ice_vsi_map_rings_to_vectors(vsi);
2977                 break;
2978         default:
2979                 break;
2980         }
2981
2982         ice_vsi_set_tc_cfg(vsi);
2983
2984         /* configure VSI nodes based on number of queues and TC's */
2985         for (i = 0; i < vsi->tc_cfg.numtc; i++)
2986                 max_txqs[i] = vsi->num_txq;
2987
2988         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2989                               vsi->tc_cfg.ena_tc, max_txqs);
2990         if (ret) {
2991                 dev_info(&vsi->back->pdev->dev,
2992                          "Failed VSI lan queue config\n");
2993                 goto err_vectors;
2994         }
2995         return 0;
2996
2997 err_vectors:
2998         ice_vsi_free_q_vectors(vsi);
2999 err_rings:
3000         if (vsi->netdev) {
3001                 vsi->current_netdev_flags = 0;
3002                 unregister_netdev(vsi->netdev);
3003                 free_netdev(vsi->netdev);
3004                 vsi->netdev = NULL;
3005         }
3006 err_vsi:
3007         ice_vsi_clear(vsi);
3008         set_bit(__ICE_RESET_FAILED, vsi->back->state);
3009         return ret;
3010 }
3011
3012 /**
3013  * ice_vsi_setup - Set up a VSI by a given type
3014  * @pf: board private structure
3015  * @pi: pointer to the port_info instance
3016  * @type: VSI type
3017  * @vf_id: defines VF id to which this VSI connects. This field is meant to be
3018  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
3019  *         fill-in ICE_INVAL_VFID as input.
3020  *
3021  * This allocates the sw VSI structure and its queue resources.
3022  *
3023  * Returns pointer to the successfully allocated and configured VSI sw struct on
3024  * success, NULL on failure.
3025  */
3026 static struct ice_vsi *
3027 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3028               enum ice_vsi_type type, u16 __always_unused vf_id)
3029 {
3030         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3031         struct device *dev = &pf->pdev->dev;
3032         struct ice_vsi *vsi;
3033         int ret, i;
3034
3035         vsi = ice_vsi_alloc(pf, type);
3036         if (!vsi) {
3037                 dev_err(dev, "could not allocate VSI\n");
3038                 return NULL;
3039         }
3040
3041         vsi->port_info = pi;
3042         vsi->vsw = pf->first_sw;
3043
3044         if (ice_vsi_get_qs(vsi)) {
3045                 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
3046                         vsi->idx);
3047                 goto err_get_qs;
3048         }
3049
3050         /* set RSS capabilities */
3051         ice_vsi_set_rss_params(vsi);
3052
3053         /* create the VSI */
3054         ret = ice_vsi_init(vsi);
3055         if (ret)
3056                 goto err_vsi;
3057
3058         switch (vsi->type) {
3059         case ICE_VSI_PF:
3060                 ret = ice_cfg_netdev(vsi);
3061                 if (ret)
3062                         goto err_cfg_netdev;
3063
3064                 ret = register_netdev(vsi->netdev);
3065                 if (ret)
3066                         goto err_register_netdev;
3067
3068                 netif_carrier_off(vsi->netdev);
3069
3070                 /* make sure transmit queues start off as stopped */
3071                 netif_tx_stop_all_queues(vsi->netdev);
3072                 ret = ice_vsi_alloc_q_vectors(vsi);
3073                 if (ret)
3074                         goto err_msix;
3075
3076                 ret = ice_vsi_setup_vector_base(vsi);
3077                 if (ret)
3078                         goto err_rings;
3079
3080                 ret = ice_vsi_alloc_rings(vsi);
3081                 if (ret)
3082                         goto err_rings;
3083
3084                 ice_vsi_map_rings_to_vectors(vsi);
3085
3086                 /* Do not exit if configuring RSS had an issue, at least
3087                  * receive traffic on first queue. Hence no need to capture
3088                  * return value
3089                  */
3090                 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3091                         ice_vsi_cfg_rss(vsi);
3092                 break;
3093         default:
3094                 /* if vsi type is not recognized, clean up the resources and
3095                  * exit
3096                  */
3097                 goto err_rings;
3098         }
3099
3100         ice_vsi_set_tc_cfg(vsi);
3101
3102         /* configure VSI nodes based on number of queues and TC's */
3103         for (i = 0; i < vsi->tc_cfg.numtc; i++)
3104                 max_txqs[i] = vsi->num_txq;
3105
3106         ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
3107                               vsi->tc_cfg.ena_tc, max_txqs);
3108         if (ret) {
3109                 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
3110                 goto err_rings;
3111         }
3112
3113         return vsi;
3114
3115 err_rings:
3116         ice_vsi_free_q_vectors(vsi);
3117 err_msix:
3118         if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3119                 unregister_netdev(vsi->netdev);
3120 err_register_netdev:
3121         if (vsi->netdev) {
3122                 free_netdev(vsi->netdev);
3123                 vsi->netdev = NULL;
3124         }
3125 err_cfg_netdev:
3126         ice_vsi_delete(vsi);
3127 err_vsi:
3128         ice_vsi_put_qs(vsi);
3129 err_get_qs:
3130         pf->q_left_tx += vsi->alloc_txq;
3131         pf->q_left_rx += vsi->alloc_rxq;
3132         ice_vsi_clear(vsi);
3133
3134         return NULL;
3135 }
3136
3137 /**
3138  * ice_pf_vsi_setup - Set up a PF VSI
3139  * @pf: board private structure
3140  * @pi: pointer to the port_info instance
3141  *
3142  * Returns pointer to the successfully allocated VSI sw struct on success,
3143  * otherwise returns NULL on failure.
3144  */
3145 static struct ice_vsi *
3146 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3147 {
3148         return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
3149 }
3150
3151 /**
3152  * ice_vsi_add_vlan - Add vsi membership for given vlan
3153  * @vsi: the vsi being configured
3154  * @vid: vlan id to be added
3155  */
3156 static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
3157 {
3158         struct ice_fltr_list_entry *tmp;
3159         struct ice_pf *pf = vsi->back;
3160         LIST_HEAD(tmp_add_list);
3161         enum ice_status status;
3162         int err = 0;
3163
3164         tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
3165         if (!tmp)
3166                 return -ENOMEM;
3167
3168         tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
3169         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3170         tmp->fltr_info.flag = ICE_FLTR_TX;
3171         tmp->fltr_info.src = vsi->vsi_num;
3172         tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
3173         tmp->fltr_info.l_data.vlan.vlan_id = vid;
3174
3175         INIT_LIST_HEAD(&tmp->list_entry);
3176         list_add(&tmp->list_entry, &tmp_add_list);
3177
3178         status = ice_add_vlan(&pf->hw, &tmp_add_list);
3179         if (status) {
3180                 err = -ENODEV;
3181                 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
3182                         vid, vsi->vsi_num);
3183         }
3184
3185         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3186         return err;
3187 }
3188
3189 /**
3190  * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
3191  * @netdev: network interface to be adjusted
3192  * @proto: unused protocol
3193  * @vid: vlan id to be added
3194  *
3195  * net_device_ops implementation for adding vlan ids
3196  */
3197 static int ice_vlan_rx_add_vid(struct net_device *netdev,
3198                                __always_unused __be16 proto, u16 vid)
3199 {
3200         struct ice_netdev_priv *np = netdev_priv(netdev);
3201         struct ice_vsi *vsi = np->vsi;
3202         int ret;
3203
3204         if (vid >= VLAN_N_VID) {
3205                 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
3206                            vid, VLAN_N_VID);
3207                 return -EINVAL;
3208         }
3209
3210         if (vsi->info.pvid)
3211                 return -EINVAL;
3212
3213         /* Enable VLAN pruning when VLAN 0 is added */
3214         if (unlikely(!vid)) {
3215                 ret = ice_cfg_vlan_pruning(vsi, true);
3216                 if (ret)
3217                         return ret;
3218         }
3219
3220         /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
3221          * needed to continue allowing all untagged packets since VLAN prune
3222          * list is applied to all packets by the switch
3223          */
3224         ret = ice_vsi_add_vlan(vsi, vid);
3225
3226         if (!ret)
3227                 set_bit(vid, vsi->active_vlans);
3228
3229         return ret;
3230 }
3231
3232 /**
3233  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
3234  * @vsi: the VSI being configured
3235  * @vid: VLAN id to be removed
3236  *
3237  * Returns 0 on success and negative on failure
3238  */
3239 static int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
3240 {
3241         struct ice_fltr_list_entry *list;
3242         struct ice_pf *pf = vsi->back;
3243         LIST_HEAD(tmp_add_list);
3244         int status = 0;
3245
3246         list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
3247         if (!list)
3248                 return -ENOMEM;
3249
3250         list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
3251         list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
3252         list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3253         list->fltr_info.l_data.vlan.vlan_id = vid;
3254         list->fltr_info.flag = ICE_FLTR_TX;
3255         list->fltr_info.src = vsi->vsi_num;
3256
3257         INIT_LIST_HEAD(&list->list_entry);
3258         list_add(&list->list_entry, &tmp_add_list);
3259
3260         if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
3261                 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
3262                         vid, vsi->vsi_num);
3263                 status = -EIO;
3264         }
3265
3266         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3267         return status;
3268 }
3269
3270 /**
3271  * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3272  * @netdev: network interface to be adjusted
3273  * @proto: unused protocol
3274  * @vid: vlan id to be removed
3275  *
3276  * net_device_ops implementation for removing vlan ids
3277  */
3278 static int ice_vlan_rx_kill_vid(struct net_device *netdev,
3279                                 __always_unused __be16 proto, u16 vid)
3280 {
3281         struct ice_netdev_priv *np = netdev_priv(netdev);
3282         struct ice_vsi *vsi = np->vsi;
3283         int status;
3284
3285         if (vsi->info.pvid)
3286                 return -EINVAL;
3287
3288         /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3289          * information
3290          */
3291         status = ice_vsi_kill_vlan(vsi, vid);
3292         if (status)
3293                 return status;
3294
3295         clear_bit(vid, vsi->active_vlans);
3296
3297         /* Disable VLAN pruning when VLAN 0 is removed */
3298         if (unlikely(!vid))
3299                 status = ice_cfg_vlan_pruning(vsi, false);
3300
3301         return status;
3302 }
3303
3304 /**
3305  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3306  * @pf: board private structure
3307  *
3308  * Returns 0 on success, negative value on failure
3309  */
3310 static int ice_setup_pf_sw(struct ice_pf *pf)
3311 {
3312         LIST_HEAD(tmp_add_list);
3313         u8 broadcast[ETH_ALEN];
3314         struct ice_vsi *vsi;
3315         int status = 0;
3316
3317         if (ice_is_reset_recovery_pending(pf->state))
3318                 return -EBUSY;
3319
3320         vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3321         if (!vsi) {
3322                 status = -ENOMEM;
3323                 goto unroll_vsi_setup;
3324         }
3325
3326         /* To add a MAC filter, first add the MAC to a list and then
3327          * pass the list to ice_add_mac.
3328          */
3329
3330          /* Add a unicast MAC filter so the VSI can get its packets */
3331         status = ice_add_mac_to_list(vsi, &tmp_add_list,
3332                                      vsi->port_info->mac.perm_addr);
3333         if (status)
3334                 goto unroll_vsi_setup;
3335
3336         /* VSI needs to receive broadcast traffic, so add the broadcast
3337          * MAC address to the list as well.
3338          */
3339         eth_broadcast_addr(broadcast);
3340         status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
3341         if (status)
3342                 goto free_mac_list;
3343
3344         /* program MAC filters for entries in tmp_add_list */
3345         status = ice_add_mac(&pf->hw, &tmp_add_list);
3346         if (status) {
3347                 dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
3348                 status = -ENOMEM;
3349                 goto free_mac_list;
3350         }
3351
3352         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3353         return status;
3354
3355 free_mac_list:
3356         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3357
3358 unroll_vsi_setup:
3359         if (vsi) {
3360                 ice_vsi_free_q_vectors(vsi);
3361                 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3362                         unregister_netdev(vsi->netdev);
3363                 if (vsi->netdev) {
3364                         free_netdev(vsi->netdev);
3365                         vsi->netdev = NULL;
3366                 }
3367
3368                 ice_vsi_delete(vsi);
3369                 ice_vsi_put_qs(vsi);
3370                 pf->q_left_tx += vsi->alloc_txq;
3371                 pf->q_left_rx += vsi->alloc_rxq;
3372                 ice_vsi_clear(vsi);
3373         }
3374         return status;
3375 }
3376
3377 /**
3378  * ice_determine_q_usage - Calculate queue distribution
3379  * @pf: board private structure
3380  *
3381  * Return -ENOMEM if we don't get enough queues for all ports
3382  */
3383 static void ice_determine_q_usage(struct ice_pf *pf)
3384 {
3385         u16 q_left_tx, q_left_rx;
3386
3387         q_left_tx = pf->hw.func_caps.common_cap.num_txq;
3388         q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
3389
3390         pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
3391
3392         /* only 1 rx queue unless RSS is enabled */
3393         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3394                 pf->num_lan_rx = 1;
3395         else
3396                 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
3397
3398         pf->q_left_tx = q_left_tx - pf->num_lan_tx;
3399         pf->q_left_rx = q_left_rx - pf->num_lan_rx;
3400 }
3401
3402 /**
3403  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3404  * @pf: board private structure to initialize
3405  */
3406 static void ice_deinit_pf(struct ice_pf *pf)
3407 {
3408         ice_service_task_stop(pf);
3409         mutex_destroy(&pf->sw_mutex);
3410         mutex_destroy(&pf->avail_q_mutex);
3411 }
3412
3413 /**
3414  * ice_init_pf - Initialize general software structures (struct ice_pf)
3415  * @pf: board private structure to initialize
3416  */
3417 static void ice_init_pf(struct ice_pf *pf)
3418 {
3419         bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
3420         set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3421
3422         mutex_init(&pf->sw_mutex);
3423         mutex_init(&pf->avail_q_mutex);
3424
3425         /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
3426         mutex_lock(&pf->avail_q_mutex);
3427         bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
3428         bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
3429         mutex_unlock(&pf->avail_q_mutex);
3430
3431         if (pf->hw.func_caps.common_cap.rss_table_size)
3432                 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3433
3434         /* setup service timer and periodic service task */
3435         timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3436         pf->serv_tmr_period = HZ;
3437         INIT_WORK(&pf->serv_task, ice_service_task);
3438         clear_bit(__ICE_SERVICE_SCHED, pf->state);
3439 }
3440
3441 /**
3442  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3443  * @pf: board private structure
3444  *
3445  * compute the number of MSIX vectors required (v_budget) and request from
3446  * the OS. Return the number of vectors reserved or negative on failure
3447  */
3448 static int ice_ena_msix_range(struct ice_pf *pf)
3449 {
3450         int v_left, v_actual, v_budget = 0;
3451         int needed, err, i;
3452
3453         v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3454
3455         /* reserve one vector for miscellaneous handler */
3456         needed = 1;
3457         v_budget += needed;
3458         v_left -= needed;
3459
3460         /* reserve vectors for LAN traffic */
3461         pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
3462         v_budget += pf->num_lan_msix;
3463
3464         pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
3465                                         sizeof(struct msix_entry), GFP_KERNEL);
3466
3467         if (!pf->msix_entries) {
3468                 err = -ENOMEM;
3469                 goto exit_err;
3470         }
3471
3472         for (i = 0; i < v_budget; i++)
3473                 pf->msix_entries[i].entry = i;
3474
3475         /* actually reserve the vectors */
3476         v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3477                                          ICE_MIN_MSIX, v_budget);
3478
3479         if (v_actual < 0) {
3480                 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
3481                 err = v_actual;
3482                 goto msix_err;
3483         }
3484
3485         if (v_actual < v_budget) {
3486                 dev_warn(&pf->pdev->dev,
3487                          "not enough vectors. requested = %d, obtained = %d\n",
3488                          v_budget, v_actual);
3489                 if (v_actual >= (pf->num_lan_msix + 1)) {
3490                         pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
3491                 } else if (v_actual >= 2) {
3492                         pf->num_lan_msix = 1;
3493                         pf->num_avail_msix = v_actual - 2;
3494                 } else {
3495                         pci_disable_msix(pf->pdev);
3496                         err = -ERANGE;
3497                         goto msix_err;
3498                 }
3499         }
3500
3501         return v_actual;
3502
3503 msix_err:
3504         devm_kfree(&pf->pdev->dev, pf->msix_entries);
3505         goto exit_err;
3506
3507 exit_err:
3508         pf->num_lan_msix = 0;
3509         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3510         return err;
3511 }
3512
3513 /**
3514  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3515  * @pf: board private structure
3516  */
3517 static void ice_dis_msix(struct ice_pf *pf)
3518 {
3519         pci_disable_msix(pf->pdev);
3520         devm_kfree(&pf->pdev->dev, pf->msix_entries);
3521         pf->msix_entries = NULL;
3522         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3523 }
3524
3525 /**
3526  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3527  * @pf: board private structure to initialize
3528  */
3529 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3530 {
3531         int vectors = 0;
3532         ssize_t size;
3533
3534         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3535                 vectors = ice_ena_msix_range(pf);
3536         else
3537                 return -ENODEV;
3538
3539         if (vectors < 0)
3540                 return vectors;
3541
3542         /* set up vector assignment tracking */
3543         size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
3544
3545         pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
3546         if (!pf->irq_tracker) {
3547                 ice_dis_msix(pf);
3548                 return -ENOMEM;
3549         }
3550
3551         pf->irq_tracker->num_entries = vectors;
3552
3553         return 0;
3554 }
3555
3556 /**
3557  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3558  * @pf: board private structure
3559  */
3560 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3561 {
3562         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3563                 ice_dis_msix(pf);
3564
3565         if (pf->irq_tracker) {
3566                 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
3567                 pf->irq_tracker = NULL;
3568         }
3569 }
3570
3571 /**
3572  * ice_probe - Device initialization routine
3573  * @pdev: PCI device information struct
3574  * @ent: entry in ice_pci_tbl
3575  *
3576  * Returns 0 on success, negative on failure
3577  */
3578 static int ice_probe(struct pci_dev *pdev,
3579                      const struct pci_device_id __always_unused *ent)
3580 {
3581         struct ice_pf *pf;
3582         struct ice_hw *hw;
3583         int err;
3584
3585         /* this driver uses devres, see Documentation/driver-model/devres.txt */
3586         err = pcim_enable_device(pdev);
3587         if (err)
3588                 return err;
3589
3590         err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3591         if (err) {
3592                 dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
3593                 return err;
3594         }
3595
3596         pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
3597         if (!pf)
3598                 return -ENOMEM;
3599
3600         /* set up for high or low dma */
3601         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3602         if (err)
3603                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3604         if (err) {
3605                 dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3606                 return err;
3607         }
3608
3609         pci_enable_pcie_error_reporting(pdev);
3610         pci_set_master(pdev);
3611
3612         pf->pdev = pdev;
3613         pci_set_drvdata(pdev, pf);
3614         set_bit(__ICE_DOWN, pf->state);
3615         /* Disable service task until DOWN bit is cleared */
3616         set_bit(__ICE_SERVICE_DIS, pf->state);
3617
3618         hw = &pf->hw;
3619         hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3620         hw->back = pf;
3621         hw->vendor_id = pdev->vendor;
3622         hw->device_id = pdev->device;
3623         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3624         hw->subsystem_vendor_id = pdev->subsystem_vendor;
3625         hw->subsystem_device_id = pdev->subsystem_device;
3626         hw->bus.device = PCI_SLOT(pdev->devfn);
3627         hw->bus.func = PCI_FUNC(pdev->devfn);
3628         ice_set_ctrlq_len(hw);
3629
3630         pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3631
3632 #ifndef CONFIG_DYNAMIC_DEBUG
3633         if (debug < -1)
3634                 hw->debug_mask = debug;
3635 #endif
3636
3637         err = ice_init_hw(hw);
3638         if (err) {
3639                 dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
3640                 err = -EIO;
3641                 goto err_exit_unroll;
3642         }
3643
3644         dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
3645                  hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
3646                  hw->api_maj_ver, hw->api_min_ver);
3647
3648         ice_init_pf(pf);
3649
3650         ice_determine_q_usage(pf);
3651
3652         pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
3653                                   hw->func_caps.guaranteed_num_vsi);
3654         if (!pf->num_alloc_vsi) {
3655                 err = -EIO;
3656                 goto err_init_pf_unroll;
3657         }
3658
3659         pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
3660                                sizeof(struct ice_vsi *), GFP_KERNEL);
3661         if (!pf->vsi) {
3662                 err = -ENOMEM;
3663                 goto err_init_pf_unroll;
3664         }
3665
3666         err = ice_init_interrupt_scheme(pf);
3667         if (err) {
3668                 dev_err(&pdev->dev,
3669                         "ice_init_interrupt_scheme failed: %d\n", err);
3670                 err = -EIO;
3671                 goto err_init_interrupt_unroll;
3672         }
3673
3674         /* Driver is mostly up */
3675         clear_bit(__ICE_DOWN, pf->state);
3676
3677         /* In case of MSIX we are going to setup the misc vector right here
3678          * to handle admin queue events etc. In case of legacy and MSI
3679          * the misc functionality and queue processing is combined in
3680          * the same vector and that gets setup at open.
3681          */
3682         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3683                 err = ice_req_irq_msix_misc(pf);
3684                 if (err) {
3685                         dev_err(&pdev->dev,
3686                                 "setup of misc vector failed: %d\n", err);
3687                         goto err_init_interrupt_unroll;
3688                 }
3689         }
3690
3691         /* create switch struct for the switch element created by FW on boot */
3692         pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
3693                                     GFP_KERNEL);
3694         if (!pf->first_sw) {
3695                 err = -ENOMEM;
3696                 goto err_msix_misc_unroll;
3697         }
3698
3699         if (hw->evb_veb)
3700                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3701         else
3702                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
3703
3704         pf->first_sw->pf = pf;
3705
3706         /* record the sw_id available for later use */
3707         pf->first_sw->sw_id = hw->port_info->sw_id;
3708
3709         err = ice_setup_pf_sw(pf);
3710         if (err) {
3711                 dev_err(&pdev->dev,
3712                         "probe failed due to setup pf switch:%d\n", err);
3713                 goto err_alloc_sw_unroll;
3714         }
3715
3716         clear_bit(__ICE_SERVICE_DIS, pf->state);
3717
3718         /* since everything is good, start the service timer */
3719         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3720
3721         err = ice_init_link_events(pf->hw.port_info);
3722         if (err) {
3723                 dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
3724                 goto err_alloc_sw_unroll;
3725         }
3726
3727         return 0;
3728
3729 err_alloc_sw_unroll:
3730         set_bit(__ICE_SERVICE_DIS, pf->state);
3731         set_bit(__ICE_DOWN, pf->state);
3732         devm_kfree(&pf->pdev->dev, pf->first_sw);
3733 err_msix_misc_unroll:
3734         ice_free_irq_msix_misc(pf);
3735 err_init_interrupt_unroll:
3736         ice_clear_interrupt_scheme(pf);
3737         devm_kfree(&pdev->dev, pf->vsi);
3738 err_init_pf_unroll:
3739         ice_deinit_pf(pf);
3740         ice_deinit_hw(hw);
3741 err_exit_unroll:
3742         pci_disable_pcie_error_reporting(pdev);
3743         return err;
3744 }
3745
3746 /**
3747  * ice_remove - Device removal routine
3748  * @pdev: PCI device information struct
3749  */
3750 static void ice_remove(struct pci_dev *pdev)
3751 {
3752         struct ice_pf *pf = pci_get_drvdata(pdev);
3753
3754         if (!pf)
3755                 return;
3756
3757         set_bit(__ICE_DOWN, pf->state);
3758         ice_service_task_stop(pf);
3759
3760         ice_vsi_release_all(pf);
3761         ice_free_irq_msix_misc(pf);
3762         ice_clear_interrupt_scheme(pf);
3763         ice_deinit_pf(pf);
3764         ice_deinit_hw(&pf->hw);
3765         pci_disable_pcie_error_reporting(pdev);
3766 }
3767
3768 /* ice_pci_tbl - PCI Device ID Table
3769  *
3770  * Wildcard entries (PCI_ANY_ID) should come last
3771  * Last entry must be all 0s
3772  *
3773  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3774  *   Class, Class Mask, private data (not used) }
3775  */
3776 static const struct pci_device_id ice_pci_tbl[] = {
3777         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
3778         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
3779         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
3780         /* required last entry */
3781         { 0, }
3782 };
3783 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3784
3785 static struct pci_driver ice_driver = {
3786         .name = KBUILD_MODNAME,
3787         .id_table = ice_pci_tbl,
3788         .probe = ice_probe,
3789         .remove = ice_remove,
3790 };
3791
3792 /**
3793  * ice_module_init - Driver registration routine
3794  *
3795  * ice_module_init is the first routine called when the driver is
3796  * loaded. All it does is register with the PCI subsystem.
3797  */
3798 static int __init ice_module_init(void)
3799 {
3800         int status;
3801
3802         pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3803         pr_info("%s\n", ice_copyright);
3804
3805         ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
3806         if (!ice_wq) {
3807                 pr_err("Failed to create workqueue\n");
3808                 return -ENOMEM;
3809         }
3810
3811         status = pci_register_driver(&ice_driver);
3812         if (status) {
3813                 pr_err("failed to register pci driver, err %d\n", status);
3814                 destroy_workqueue(ice_wq);
3815         }
3816
3817         return status;
3818 }
3819 module_init(ice_module_init);
3820
3821 /**
3822  * ice_module_exit - Driver exit cleanup routine
3823  *
3824  * ice_module_exit is called just before the driver is removed
3825  * from memory.
3826  */
3827 static void __exit ice_module_exit(void)
3828 {
3829         pci_unregister_driver(&ice_driver);
3830         destroy_workqueue(ice_wq);
3831         pr_info("module unloaded\n");
3832 }
3833 module_exit(ice_module_exit);
3834
3835 /**
3836  * ice_set_mac_address - NDO callback to set mac address
3837  * @netdev: network interface device structure
3838  * @pi: pointer to an address structure
3839  *
3840  * Returns 0 on success, negative on failure
3841  */
3842 static int ice_set_mac_address(struct net_device *netdev, void *pi)
3843 {
3844         struct ice_netdev_priv *np = netdev_priv(netdev);
3845         struct ice_vsi *vsi = np->vsi;
3846         struct ice_pf *pf = vsi->back;
3847         struct ice_hw *hw = &pf->hw;
3848         struct sockaddr *addr = pi;
3849         enum ice_status status;
3850         LIST_HEAD(a_mac_list);
3851         LIST_HEAD(r_mac_list);
3852         u8 flags = 0;
3853         int err;
3854         u8 *mac;
3855
3856         mac = (u8 *)addr->sa_data;
3857
3858         if (!is_valid_ether_addr(mac))
3859                 return -EADDRNOTAVAIL;
3860
3861         if (ether_addr_equal(netdev->dev_addr, mac)) {
3862                 netdev_warn(netdev, "already using mac %pM\n", mac);
3863                 return 0;
3864         }
3865
3866         if (test_bit(__ICE_DOWN, pf->state) ||
3867             ice_is_reset_recovery_pending(pf->state)) {
3868                 netdev_err(netdev, "can't set mac %pM. device not ready\n",
3869                            mac);
3870                 return -EBUSY;
3871         }
3872
3873         /* When we change the mac address we also have to change the mac address
3874          * based filter rules that were created previously for the old mac
3875          * address. So first, we remove the old filter rule using ice_remove_mac
3876          * and then create a new filter rule using ice_add_mac. Note that for
3877          * both these operations, we first need to form a "list" of mac
3878          * addresses (even though in this case, we have only 1 mac address to be
3879          * added/removed) and this done using ice_add_mac_to_list. Depending on
3880          * the ensuing operation this "list" of mac addresses is either to be
3881          * added or removed from the filter.
3882          */
3883         err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
3884         if (err) {
3885                 err = -EADDRNOTAVAIL;
3886                 goto free_lists;
3887         }
3888
3889         status = ice_remove_mac(hw, &r_mac_list);
3890         if (status) {
3891                 err = -EADDRNOTAVAIL;
3892                 goto free_lists;
3893         }
3894
3895         err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
3896         if (err) {
3897                 err = -EADDRNOTAVAIL;
3898                 goto free_lists;
3899         }
3900
3901         status = ice_add_mac(hw, &a_mac_list);
3902         if (status) {
3903                 err = -EADDRNOTAVAIL;
3904                 goto free_lists;
3905         }
3906
3907 free_lists:
3908         /* free list entries */
3909         ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
3910         ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
3911
3912         if (err) {
3913                 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
3914                            mac);
3915                 return err;
3916         }
3917
3918         /* change the netdev's mac address */
3919         memcpy(netdev->dev_addr, mac, netdev->addr_len);
3920         netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
3921                    netdev->dev_addr);
3922
3923         /* write new mac address to the firmware */
3924         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3925         status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3926         if (status) {
3927                 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
3928                            mac);
3929         }
3930         return 0;
3931 }
3932
3933 /**
3934  * ice_set_rx_mode - NDO callback to set the netdev filters
3935  * @netdev: network interface device structure
3936  */
3937 static void ice_set_rx_mode(struct net_device *netdev)
3938 {
3939         struct ice_netdev_priv *np = netdev_priv(netdev);
3940         struct ice_vsi *vsi = np->vsi;
3941
3942         if (!vsi)
3943                 return;
3944
3945         /* Set the flags to synchronize filters
3946          * ndo_set_rx_mode may be triggered even without a change in netdev
3947          * flags
3948          */
3949         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3950         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3951         set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3952
3953         /* schedule our worker thread which will take care of
3954          * applying the new filter changes
3955          */
3956         ice_service_task_schedule(vsi->back);
3957 }
3958
3959 /**
3960  * ice_fdb_add - add an entry to the hardware database
3961  * @ndm: the input from the stack
3962  * @tb: pointer to array of nladdr (unused)
3963  * @dev: the net device pointer
3964  * @addr: the MAC address entry being added
3965  * @vid: VLAN id
3966  * @flags: instructions from stack about fdb operation
3967  */
3968 static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3969                        struct net_device *dev, const unsigned char *addr,
3970                        u16 vid, u16 flags)
3971 {
3972         int err;
3973
3974         if (vid) {
3975                 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3976                 return -EINVAL;
3977         }
3978         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3979                 netdev_err(dev, "FDB only supports static addresses\n");
3980                 return -EINVAL;
3981         }
3982
3983         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3984                 err = dev_uc_add_excl(dev, addr);
3985         else if (is_multicast_ether_addr(addr))
3986                 err = dev_mc_add_excl(dev, addr);
3987         else
3988                 err = -EINVAL;
3989
3990         /* Only return duplicate errors if NLM_F_EXCL is set */
3991         if (err == -EEXIST && !(flags & NLM_F_EXCL))
3992                 err = 0;
3993
3994         return err;
3995 }
3996
3997 /**
3998  * ice_fdb_del - delete an entry from the hardware database
3999  * @ndm: the input from the stack
4000  * @tb: pointer to array of nladdr (unused)
4001  * @dev: the net device pointer
4002  * @addr: the MAC address entry being added
4003  * @vid: VLAN id
4004  */
4005 static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
4006                        struct net_device *dev, const unsigned char *addr,
4007                        __always_unused u16 vid)
4008 {
4009         int err;
4010
4011         if (ndm->ndm_state & NUD_PERMANENT) {
4012                 netdev_err(dev, "FDB only supports static addresses\n");
4013                 return -EINVAL;
4014         }
4015
4016         if (is_unicast_ether_addr(addr))
4017                 err = dev_uc_del(dev, addr);
4018         else if (is_multicast_ether_addr(addr))
4019                 err = dev_mc_del(dev, addr);
4020         else
4021                 err = -EINVAL;
4022
4023         return err;
4024 }
4025
4026 /**
4027  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
4028  * @vsi: the vsi being changed
4029  */
4030 static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
4031 {
4032         struct device *dev = &vsi->back->pdev->dev;
4033         struct ice_hw *hw = &vsi->back->hw;
4034         struct ice_vsi_ctx ctxt = { 0 };
4035         enum ice_status status;
4036
4037         /* Here we are configuring the VSI to let the driver add VLAN tags by
4038          * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
4039          * insertion happens in the Tx hot path, in ice_tx_map.
4040          */
4041         ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
4042
4043         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
4044         ctxt.vsi_num = vsi->vsi_num;
4045
4046         status = ice_aq_update_vsi(hw, &ctxt, NULL);
4047         if (status) {
4048                 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
4049                         status, hw->adminq.sq_last_status);
4050                 return -EIO;
4051         }
4052
4053         vsi->info.vlan_flags = ctxt.info.vlan_flags;
4054         return 0;
4055 }
4056
4057 /**
4058  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
4059  * @vsi: the vsi being changed
4060  * @ena: boolean value indicating if this is a enable or disable request
4061  */
4062 static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
4063 {
4064         struct device *dev = &vsi->back->pdev->dev;
4065         struct ice_hw *hw = &vsi->back->hw;
4066         struct ice_vsi_ctx ctxt = { 0 };
4067         enum ice_status status;
4068
4069         /* Here we are configuring what the VSI should do with the VLAN tag in
4070          * the Rx packet. We can either leave the tag in the packet or put it in
4071          * the Rx descriptor.
4072          */
4073         if (ena) {
4074                 /* Strip VLAN tag from Rx packet and put it in the desc */
4075                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
4076         } else {
4077                 /* Disable stripping. Leave tag in packet */
4078                 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
4079         }
4080
4081         /* Allow all packets untagged/tagged */
4082         ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
4083
4084         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
4085         ctxt.vsi_num = vsi->vsi_num;
4086
4087         status = ice_aq_update_vsi(hw, &ctxt, NULL);
4088         if (status) {
4089                 dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
4090                         ena, status, hw->adminq.sq_last_status);
4091                 return -EIO;
4092         }
4093
4094         vsi->info.vlan_flags = ctxt.info.vlan_flags;
4095         return 0;
4096 }
4097
4098 /**
4099  * ice_set_features - set the netdev feature flags
4100  * @netdev: ptr to the netdev being adjusted
4101  * @features: the feature set that the stack is suggesting
4102  */
4103 static int ice_set_features(struct net_device *netdev,
4104                             netdev_features_t features)
4105 {
4106         struct ice_netdev_priv *np = netdev_priv(netdev);
4107         struct ice_vsi *vsi = np->vsi;
4108         int ret = 0;
4109
4110         if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
4111             !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
4112                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
4113         else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
4114                  (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
4115                 ret = ice_vsi_manage_vlan_stripping(vsi, false);
4116         else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
4117                  !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
4118                 ret = ice_vsi_manage_vlan_insertion(vsi);
4119         else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
4120                  (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
4121                 ret = ice_vsi_manage_vlan_insertion(vsi);
4122
4123         return ret;
4124 }
4125
4126 /**
4127  * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
4128  * @vsi: VSI to setup vlan properties for
4129  */
4130 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
4131 {
4132         int ret = 0;
4133
4134         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
4135                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
4136         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
4137                 ret = ice_vsi_manage_vlan_insertion(vsi);
4138
4139         return ret;
4140 }
4141
4142 /**
4143  * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
4144  * @vsi: the VSI being brought back up
4145  */
4146 static int ice_restore_vlan(struct ice_vsi *vsi)
4147 {
4148         int err;
4149         u16 vid;
4150
4151         if (!vsi->netdev)
4152                 return -EINVAL;
4153
4154         err = ice_vsi_vlan_setup(vsi);
4155         if (err)
4156                 return err;
4157
4158         for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
4159                 err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
4160                 if (err)
4161                         break;
4162         }
4163
4164         return err;
4165 }
4166
4167 /**
4168  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
4169  * @ring: The Tx ring to configure
4170  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
4171  * @pf_q: queue index in the PF space
4172  *
4173  * Configure the Tx descriptor ring in TLAN context.
4174  */
4175 static void
4176 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
4177 {
4178         struct ice_vsi *vsi = ring->vsi;
4179         struct ice_hw *hw = &vsi->back->hw;
4180
4181         tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
4182
4183         tlan_ctx->port_num = vsi->port_info->lport;
4184
4185         /* Transmit Queue Length */
4186         tlan_ctx->qlen = ring->count;
4187
4188         /* PF number */
4189         tlan_ctx->pf_num = hw->pf_id;
4190
4191         /* queue belongs to a specific VSI type
4192          * VF / VM index should be programmed per vmvf_type setting:
4193          * for vmvf_type = VF, it is VF number between 0-256
4194          * for vmvf_type = VM, it is VM number between 0-767
4195          * for PF or EMP this field should be set to zero
4196          */
4197         switch (vsi->type) {
4198         case ICE_VSI_PF:
4199                 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
4200                 break;
4201         default:
4202                 return;
4203         }
4204
4205         /* make sure the context is associated with the right VSI */
4206         tlan_ctx->src_vsi = vsi->vsi_num;
4207
4208         tlan_ctx->tso_ena = ICE_TX_LEGACY;
4209         tlan_ctx->tso_qnum = pf_q;
4210
4211         /* Legacy or Advanced Host Interface:
4212          * 0: Advanced Host Interface
4213          * 1: Legacy Host Interface
4214          */
4215         tlan_ctx->legacy_int = ICE_TX_LEGACY;
4216 }
4217
4218 /**
4219  * ice_vsi_cfg_txqs - Configure the VSI for Tx
4220  * @vsi: the VSI being configured
4221  *
4222  * Return 0 on success and a negative value on error
4223  * Configure the Tx VSI for operation.
4224  */
4225 static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
4226 {
4227         struct ice_aqc_add_tx_qgrp *qg_buf;
4228         struct ice_aqc_add_txqs_perq *txq;
4229         struct ice_pf *pf = vsi->back;
4230         enum ice_status status;
4231         u16 buf_len, i, pf_q;
4232         int err = 0, tc = 0;
4233         u8 num_q_grps;
4234
4235         buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
4236         qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
4237         if (!qg_buf)
4238                 return -ENOMEM;
4239
4240         if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
4241                 err = -EINVAL;
4242                 goto err_cfg_txqs;
4243         }
4244         qg_buf->num_txqs = 1;
4245         num_q_grps = 1;
4246
4247         /* set up and configure the tx queues */
4248         ice_for_each_txq(vsi, i) {
4249                 struct ice_tlan_ctx tlan_ctx = { 0 };
4250
4251                 pf_q = vsi->txq_map[i];
4252                 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
4253                 /* copy context contents into the qg_buf */
4254                 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
4255                 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
4256                             ice_tlan_ctx_info);
4257
4258                 /* init queue specific tail reg. It is referred as transmit
4259                  * comm scheduler queue doorbell.
4260                  */
4261                 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
4262                 status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
4263                                          num_q_grps, qg_buf, buf_len, NULL);
4264                 if (status) {
4265                         dev_err(&vsi->back->pdev->dev,
4266                                 "Failed to set LAN Tx queue context, error: %d\n",
4267                                 status);
4268                         err = -ENODEV;
4269                         goto err_cfg_txqs;
4270                 }
4271
4272                 /* Add Tx Queue TEID into the VSI tx ring from the response
4273                  * This will complete configuring and enabling the queue.
4274                  */
4275                 txq = &qg_buf->txqs[0];
4276                 if (pf_q == le16_to_cpu(txq->txq_id))
4277                         vsi->tx_rings[i]->txq_teid =
4278                                 le32_to_cpu(txq->q_teid);
4279         }
4280 err_cfg_txqs:
4281         devm_kfree(&pf->pdev->dev, qg_buf);
4282         return err;
4283 }
4284
4285 /**
4286  * ice_setup_rx_ctx - Configure a receive ring context
4287  * @ring: The Rx ring to configure
4288  *
4289  * Configure the Rx descriptor ring in RLAN context.
4290  */
4291 static int ice_setup_rx_ctx(struct ice_ring *ring)
4292 {
4293         struct ice_vsi *vsi = ring->vsi;
4294         struct ice_hw *hw = &vsi->back->hw;
4295         u32 rxdid = ICE_RXDID_FLEX_NIC;
4296         struct ice_rlan_ctx rlan_ctx;
4297         u32 regval;
4298         u16 pf_q;
4299         int err;
4300
4301         /* what is RX queue number in global space of 2K rx queues */
4302         pf_q = vsi->rxq_map[ring->q_index];
4303
4304         /* clear the context structure first */
4305         memset(&rlan_ctx, 0, sizeof(rlan_ctx));
4306
4307         rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
4308
4309         rlan_ctx.qlen = ring->count;
4310
4311         /* Receive Packet Data Buffer Size.
4312          * The Packet Data Buffer Size is defined in 128 byte units.
4313          */
4314         rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
4315
4316         /* use 32 byte descriptors */
4317         rlan_ctx.dsize = 1;
4318
4319         /* Strip the Ethernet CRC bytes before the packet is posted to host
4320          * memory.
4321          */
4322         rlan_ctx.crcstrip = 1;
4323
4324         /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
4325         rlan_ctx.l2tsel = 1;
4326
4327         rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
4328         rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
4329         rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
4330
4331         /* This controls whether VLAN is stripped from inner headers
4332          * The VLAN in the inner L2 header is stripped to the receive
4333          * descriptor if enabled by this flag.
4334          */
4335         rlan_ctx.showiv = 0;
4336
4337         /* Max packet size for this queue - must not be set to a larger value
4338          * than 5 x DBUF
4339          */
4340         rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
4341                                ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
4342
4343         /* Rx queue threshold in units of 64 */
4344         rlan_ctx.lrxqthresh = 1;
4345
4346          /* Enable Flexible Descriptors in the queue context which
4347           * allows this driver to select a specific receive descriptor format
4348           */
4349         regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
4350         regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
4351                 QRXFLXP_CNTXT_RXDID_IDX_M;
4352
4353         /* increasing context priority to pick up profile id;
4354          * default is 0x01; setting to 0x03 to ensure profile
4355          * is programming if prev context is of same priority
4356          */
4357         regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
4358                 QRXFLXP_CNTXT_RXDID_PRIO_M;
4359
4360         wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
4361
4362         /* Absolute queue number out of 2K needs to be passed */
4363         err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
4364         if (err) {
4365                 dev_err(&vsi->back->pdev->dev,
4366                         "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
4367                         pf_q, err);
4368                 return -EIO;
4369         }
4370
4371         /* init queue specific tail register */
4372         ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
4373         writel(0, ring->tail);
4374         ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
4375
4376         return 0;
4377 }
4378
4379 /**
4380  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
4381  * @vsi: the VSI being configured
4382  *
4383  * Return 0 on success and a negative value on error
4384  * Configure the Rx VSI for operation.
4385  */
4386 static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
4387 {
4388         int err = 0;
4389         u16 i;
4390
4391         if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
4392                 vsi->max_frame = vsi->netdev->mtu +
4393                         ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4394         else
4395                 vsi->max_frame = ICE_RXBUF_2048;
4396
4397         vsi->rx_buf_len = ICE_RXBUF_2048;
4398         /* set up individual rings */
4399         for (i = 0; i < vsi->num_rxq && !err; i++)
4400                 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
4401
4402         if (err) {
4403                 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
4404                 return -EIO;
4405         }
4406         return err;
4407 }
4408
4409 /**
4410  * ice_vsi_cfg - Setup the VSI
4411  * @vsi: the VSI being configured
4412  *
4413  * Return 0 on success and negative value on error
4414  */
4415 static int ice_vsi_cfg(struct ice_vsi *vsi)
4416 {
4417         int err;
4418
4419         if (vsi->netdev) {
4420                 ice_set_rx_mode(vsi->netdev);
4421                 err = ice_restore_vlan(vsi);
4422                 if (err)
4423                         return err;
4424         }
4425
4426         err = ice_vsi_cfg_txqs(vsi);
4427         if (!err)
4428                 err = ice_vsi_cfg_rxqs(vsi);
4429
4430         return err;
4431 }
4432
4433 /**
4434  * ice_vsi_stop_tx_rings - Disable Tx rings
4435  * @vsi: the VSI being configured
4436  */
4437 static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
4438 {
4439         struct ice_pf *pf = vsi->back;
4440         struct ice_hw *hw = &pf->hw;
4441         enum ice_status status;
4442         u32 *q_teids, val;
4443         u16 *q_ids, i;
4444         int err = 0;
4445
4446         if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
4447                 return -EINVAL;
4448
4449         q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
4450                                GFP_KERNEL);
4451         if (!q_teids)
4452                 return -ENOMEM;
4453
4454         q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
4455                              GFP_KERNEL);
4456         if (!q_ids) {
4457                 err = -ENOMEM;
4458                 goto err_alloc_q_ids;
4459         }
4460
4461         /* set up the tx queue list to be disabled */
4462         ice_for_each_txq(vsi, i) {
4463                 u16 v_idx;
4464
4465                 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
4466                         err = -EINVAL;
4467                         goto err_out;
4468                 }
4469
4470                 q_ids[i] = vsi->txq_map[i];
4471                 q_teids[i] = vsi->tx_rings[i]->txq_teid;
4472
4473                 /* clear cause_ena bit for disabled queues */
4474                 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4475                 val &= ~QINT_TQCTL_CAUSE_ENA_M;
4476                 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4477
4478                 /* software is expected to wait for 100 ns */
4479                 ndelay(100);
4480
4481                 /* trigger a software interrupt for the vector associated to
4482                  * the queue to schedule napi handler
4483                  */
4484                 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
4485                 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
4486                      GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
4487         }
4488         status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
4489                                  NULL);
4490         /* if the disable queue command was exercised during an active reset
4491          * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
4492          * the reset operation disables queues at the hardware level anyway.
4493          */
4494         if (status == ICE_ERR_RESET_ONGOING) {
4495                 dev_dbg(&pf->pdev->dev,
4496                         "Reset in progress. LAN Tx queues already disabled\n");
4497         } else if (status) {
4498                 dev_err(&pf->pdev->dev,
4499                         "Failed to disable LAN Tx queues, error: %d\n",
4500                         status);
4501                 err = -ENODEV;
4502         }
4503
4504 err_out:
4505         devm_kfree(&pf->pdev->dev, q_ids);
4506
4507 err_alloc_q_ids:
4508         devm_kfree(&pf->pdev->dev, q_teids);
4509
4510         return err;
4511 }
4512
4513 /**
4514  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4515  * @pf: the PF being configured
4516  * @pf_q: the PF queue
4517  * @ena: enable or disable state of the queue
4518  *
4519  * This routine will wait for the given Rx queue of the PF to reach the
4520  * enabled or disabled state.
4521  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4522  * multiple retries; else will return 0 in case of success.
4523  */
4524 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
4525 {
4526         int i;
4527
4528         for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
4529                 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
4530
4531                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4532                         break;
4533
4534                 usleep_range(10, 20);
4535         }
4536         if (i >= ICE_Q_WAIT_RETRY_LIMIT)
4537                 return -ETIMEDOUT;
4538
4539         return 0;
4540 }
4541
4542 /**
4543  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
4544  * @vsi: the VSI being configured
4545  * @ena: start or stop the rx rings
4546  */
4547 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
4548 {
4549         struct ice_pf *pf = vsi->back;
4550         struct ice_hw *hw = &pf->hw;
4551         int i, j, ret = 0;
4552
4553         for (i = 0; i < vsi->num_rxq; i++) {
4554                 int pf_q = vsi->rxq_map[i];
4555                 u32 rx_reg;
4556
4557                 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
4558                         rx_reg = rd32(hw, QRX_CTRL(pf_q));
4559                         if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
4560                             ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
4561                                 break;
4562                         usleep_range(1000, 2000);
4563                 }
4564
4565                 /* Skip if the queue is already in the requested state */
4566                 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4567                         continue;
4568
4569                 /* turn on/off the queue */
4570                 if (ena)
4571                         rx_reg |= QRX_CTRL_QENA_REQ_M;
4572                 else
4573                         rx_reg &= ~QRX_CTRL_QENA_REQ_M;
4574                 wr32(hw, QRX_CTRL(pf_q), rx_reg);
4575
4576                 /* wait for the change to finish */
4577                 ret = ice_pf_rxq_wait(pf, pf_q, ena);
4578                 if (ret) {
4579                         dev_err(&pf->pdev->dev,
4580                                 "VSI idx %d Rx ring %d %sable timeout\n",
4581                                 vsi->idx, pf_q, (ena ? "en" : "dis"));
4582                         break;
4583                 }
4584         }
4585
4586         return ret;
4587 }
4588
4589 /**
4590  * ice_vsi_start_rx_rings - start VSI's rx rings
4591  * @vsi: the VSI whose rings are to be started
4592  *
4593  * Returns 0 on success and a negative value on error
4594  */
4595 static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
4596 {
4597         return ice_vsi_ctrl_rx_rings(vsi, true);
4598 }
4599
4600 /**
4601  * ice_vsi_stop_rx_rings - stop VSI's rx rings
4602  * @vsi: the VSI
4603  *
4604  * Returns 0 on success and a negative value on error
4605  */
4606 static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
4607 {
4608         return ice_vsi_ctrl_rx_rings(vsi, false);
4609 }
4610
4611 /**
4612  * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
4613  * @vsi: the VSI
4614  * Returns 0 on success and a negative value on error
4615  */
4616 static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
4617 {
4618         int err_tx, err_rx;
4619
4620         err_tx = ice_vsi_stop_tx_rings(vsi);
4621         if (err_tx)
4622                 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
4623
4624         err_rx = ice_vsi_stop_rx_rings(vsi);
4625         if (err_rx)
4626                 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
4627
4628         if (err_tx || err_rx)
4629                 return -EIO;
4630
4631         return 0;
4632 }
4633
4634 /**
4635  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4636  * @vsi: the VSI being configured
4637  */
4638 static void ice_napi_enable_all(struct ice_vsi *vsi)
4639 {
4640         int q_idx;
4641
4642         if (!vsi->netdev)
4643                 return;
4644
4645         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4646                 napi_enable(&vsi->q_vectors[q_idx]->napi);
4647 }
4648
4649 /**
4650  * ice_up_complete - Finish the last steps of bringing up a connection
4651  * @vsi: The VSI being configured
4652  *
4653  * Return 0 on success and negative value on error
4654  */
4655 static int ice_up_complete(struct ice_vsi *vsi)
4656 {
4657         struct ice_pf *pf = vsi->back;
4658         int err;
4659
4660         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4661                 ice_vsi_cfg_msix(vsi);
4662         else
4663                 return -ENOTSUPP;
4664
4665         /* Enable only Rx rings, Tx rings were enabled by the FW when the
4666          * Tx queue group list was configured and the context bits were
4667          * programmed using ice_vsi_cfg_txqs
4668          */
4669         err = ice_vsi_start_rx_rings(vsi);
4670         if (err)
4671                 return err;
4672
4673         clear_bit(__ICE_DOWN, vsi->state);
4674         ice_napi_enable_all(vsi);
4675         ice_vsi_ena_irq(vsi);
4676
4677         if (vsi->port_info &&
4678             (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
4679             vsi->netdev) {
4680                 ice_print_link_msg(vsi, true);
4681                 netif_tx_start_all_queues(vsi->netdev);
4682                 netif_carrier_on(vsi->netdev);
4683         }
4684
4685         ice_service_task_schedule(pf);
4686
4687         return err;
4688 }
4689
4690 /**
4691  * ice_up - Bring the connection back up after being down
4692  * @vsi: VSI being configured
4693  */
4694 int ice_up(struct ice_vsi *vsi)
4695 {
4696         int err;
4697
4698         err = ice_vsi_cfg(vsi);
4699         if (!err)
4700                 err = ice_up_complete(vsi);
4701
4702         return err;
4703 }
4704
4705 /**
4706  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
4707  * @ring: Tx or Rx ring to read stats from
4708  * @pkts: packets stats counter
4709  * @bytes: bytes stats counter
4710  *
4711  * This function fetches stats from the ring considering the atomic operations
4712  * that needs to be performed to read u64 values in 32 bit machine.
4713  */
4714 static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
4715                                          u64 *bytes)
4716 {
4717         unsigned int start;
4718         *pkts = 0;
4719         *bytes = 0;
4720
4721         if (!ring)
4722                 return;
4723         do {
4724                 start = u64_stats_fetch_begin_irq(&ring->syncp);
4725                 *pkts = ring->stats.pkts;
4726                 *bytes = ring->stats.bytes;
4727         } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4728 }
4729
4730 /**
4731  * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4732  * @hw: ptr to the hardware info
4733  * @hireg: high 32 bit HW register to read from
4734  * @loreg: low 32 bit HW register to read from
4735  * @prev_stat_loaded: bool to specify if previous stats are loaded
4736  * @prev_stat: ptr to previous loaded stat value
4737  * @cur_stat: ptr to current stat value
4738  */
4739 static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
4740                               bool prev_stat_loaded, u64 *prev_stat,
4741                               u64 *cur_stat)
4742 {
4743         u64 new_data;
4744
4745         new_data = rd32(hw, loreg);
4746         new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
4747
4748         /* device stats are not reset at PFR, they likely will not be zeroed
4749          * when the driver starts. So save the first values read and use them as
4750          * offsets to be subtracted from the raw values in order to report stats
4751          * that count from zero.
4752          */
4753         if (!prev_stat_loaded)
4754                 *prev_stat = new_data;
4755         if (likely(new_data >= *prev_stat))
4756                 *cur_stat = new_data - *prev_stat;
4757         else
4758                 /* to manage the potential roll-over */
4759                 *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
4760         *cur_stat &= 0xFFFFFFFFFFULL;
4761 }
4762
4763 /**
4764  * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4765  * @hw: ptr to the hardware info
4766  * @reg: HW register to read from
4767  * @prev_stat_loaded: bool to specify if previous stats are loaded
4768  * @prev_stat: ptr to previous loaded stat value
4769  * @cur_stat: ptr to current stat value
4770  */
4771 static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4772                               u64 *prev_stat, u64 *cur_stat)
4773 {
4774         u32 new_data;
4775
4776         new_data = rd32(hw, reg);
4777
4778         /* device stats are not reset at PFR, they likely will not be zeroed
4779          * when the driver starts. So save the first values read and use them as
4780          * offsets to be subtracted from the raw values in order to report stats
4781          * that count from zero.
4782          */
4783         if (!prev_stat_loaded)
4784                 *prev_stat = new_data;
4785         if (likely(new_data >= *prev_stat))
4786                 *cur_stat = new_data - *prev_stat;
4787         else
4788                 /* to manage the potential roll-over */
4789                 *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
4790 }
4791
4792 /**
4793  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
4794  * @vsi: the VSI to be updated
4795  */
4796 static void ice_update_eth_stats(struct ice_vsi *vsi)
4797 {
4798         struct ice_eth_stats *prev_es, *cur_es;
4799         struct ice_hw *hw = &vsi->back->hw;
4800         u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
4801
4802         prev_es = &vsi->eth_stats_prev;
4803         cur_es = &vsi->eth_stats;
4804
4805         ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
4806                           vsi->stat_offsets_loaded, &prev_es->rx_bytes,
4807                           &cur_es->rx_bytes);
4808
4809         ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
4810                           vsi->stat_offsets_loaded, &prev_es->rx_unicast,
4811                           &cur_es->rx_unicast);
4812
4813         ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
4814                           vsi->stat_offsets_loaded, &prev_es->rx_multicast,
4815                           &cur_es->rx_multicast);
4816
4817         ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
4818                           vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
4819                           &cur_es->rx_broadcast);
4820
4821         ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
4822                           &prev_es->rx_discards, &cur_es->rx_discards);
4823
4824         ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
4825                           vsi->stat_offsets_loaded, &prev_es->tx_bytes,
4826                           &cur_es->tx_bytes);
4827
4828         ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
4829                           vsi->stat_offsets_loaded, &prev_es->tx_unicast,
4830                           &cur_es->tx_unicast);
4831
4832         ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
4833                           vsi->stat_offsets_loaded, &prev_es->tx_multicast,
4834                           &cur_es->tx_multicast);
4835
4836         ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
4837                           vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
4838                           &cur_es->tx_broadcast);
4839
4840         ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
4841                           &prev_es->tx_errors, &cur_es->tx_errors);
4842
4843         vsi->stat_offsets_loaded = true;
4844 }
4845
4846 /**
4847  * ice_update_vsi_ring_stats - Update VSI stats counters
4848  * @vsi: the VSI to be updated
4849  */
4850 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
4851 {
4852         struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
4853         struct ice_ring *ring;
4854         u64 pkts, bytes;
4855         int i;
4856
4857         /* reset netdev stats */
4858         vsi_stats->tx_packets = 0;
4859         vsi_stats->tx_bytes = 0;
4860         vsi_stats->rx_packets = 0;
4861         vsi_stats->rx_bytes = 0;
4862
4863         /* reset non-netdev (extended) stats */
4864         vsi->tx_restart = 0;
4865         vsi->tx_busy = 0;
4866         vsi->tx_linearize = 0;
4867         vsi->rx_buf_failed = 0;
4868         vsi->rx_page_failed = 0;
4869
4870         rcu_read_lock();
4871
4872         /* update Tx rings counters */
4873         ice_for_each_txq(vsi, i) {
4874                 ring = READ_ONCE(vsi->tx_rings[i]);
4875                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4876                 vsi_stats->tx_packets += pkts;
4877                 vsi_stats->tx_bytes += bytes;
4878                 vsi->tx_restart += ring->tx_stats.restart_q;
4879                 vsi->tx_busy += ring->tx_stats.tx_busy;
4880                 vsi->tx_linearize += ring->tx_stats.tx_linearize;
4881         }
4882
4883         /* update Rx rings counters */
4884         ice_for_each_rxq(vsi, i) {
4885                 ring = READ_ONCE(vsi->rx_rings[i]);
4886                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4887                 vsi_stats->rx_packets += pkts;
4888                 vsi_stats->rx_bytes += bytes;
4889                 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4890                 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4891         }
4892
4893         rcu_read_unlock();
4894 }
4895
4896 /**
4897  * ice_update_vsi_stats - Update VSI stats counters
4898  * @vsi: the VSI to be updated
4899  */
4900 static void ice_update_vsi_stats(struct ice_vsi *vsi)
4901 {
4902         struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4903         struct ice_eth_stats *cur_es = &vsi->eth_stats;
4904         struct ice_pf *pf = vsi->back;
4905
4906         if (test_bit(__ICE_DOWN, vsi->state) ||
4907             test_bit(__ICE_CFG_BUSY, pf->state))
4908                 return;
4909
4910         /* get stats as recorded by Tx/Rx rings */
4911         ice_update_vsi_ring_stats(vsi);
4912
4913         /* get VSI stats as recorded by the hardware */
4914         ice_update_eth_stats(vsi);
4915
4916         cur_ns->tx_errors = cur_es->tx_errors;
4917         cur_ns->rx_dropped = cur_es->rx_discards;
4918         cur_ns->tx_dropped = cur_es->tx_discards;
4919         cur_ns->multicast = cur_es->rx_multicast;
4920
4921         /* update some more netdev stats if this is main VSI */
4922         if (vsi->type == ICE_VSI_PF) {
4923                 cur_ns->rx_crc_errors = pf->stats.crc_errors;
4924                 cur_ns->rx_errors = pf->stats.crc_errors +
4925                                     pf->stats.illegal_bytes;
4926                 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4927         }
4928 }
4929
4930 /**
4931  * ice_update_pf_stats - Update PF port stats counters
4932  * @pf: PF whose stats needs to be updated
4933  */
4934 static void ice_update_pf_stats(struct ice_pf *pf)
4935 {
4936         struct ice_hw_port_stats *prev_ps, *cur_ps;
4937         struct ice_hw *hw = &pf->hw;
4938         u8 pf_id;
4939
4940         prev_ps = &pf->stats_prev;
4941         cur_ps = &pf->stats;
4942         pf_id = hw->pf_id;
4943
4944         ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
4945                           pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
4946                           &cur_ps->eth.rx_bytes);
4947
4948         ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
4949                           pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
4950                           &cur_ps->eth.rx_unicast);
4951
4952         ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
4953                           pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
4954                           &cur_ps->eth.rx_multicast);
4955
4956         ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
4957                           pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
4958                           &cur_ps->eth.rx_broadcast);
4959
4960         ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
4961                           pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
4962                           &cur_ps->eth.tx_bytes);
4963
4964         ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
4965                           pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
4966                           &cur_ps->eth.tx_unicast);
4967
4968         ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
4969                           pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
4970                           &cur_ps->eth.tx_multicast);
4971
4972         ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
4973                           pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
4974                           &cur_ps->eth.tx_broadcast);
4975
4976         ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
4977                           &prev_ps->tx_dropped_link_down,
4978                           &cur_ps->tx_dropped_link_down);
4979
4980         ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
4981                           pf->stat_prev_loaded, &prev_ps->rx_size_64,
4982                           &cur_ps->rx_size_64);
4983
4984         ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
4985                           pf->stat_prev_loaded, &prev_ps->rx_size_127,
4986                           &cur_ps->rx_size_127);
4987
4988         ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
4989                           pf->stat_prev_loaded, &prev_ps->rx_size_255,
4990                           &cur_ps->rx_size_255);
4991
4992         ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
4993                           pf->stat_prev_loaded, &prev_ps->rx_size_511,
4994                           &cur_ps->rx_size_511);
4995
4996         ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
4997                           GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
4998                           &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
4999
5000         ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
5001                           GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
5002                           &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5003
5004         ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
5005                           GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
5006                           &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5007
5008         ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
5009                           pf->stat_prev_loaded, &prev_ps->tx_size_64,
5010                           &cur_ps->tx_size_64);
5011
5012         ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
5013                           pf->stat_prev_loaded, &prev_ps->tx_size_127,
5014                           &cur_ps->tx_size_127);
5015
5016         ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
5017                           pf->stat_prev_loaded, &prev_ps->tx_size_255,
5018                           &cur_ps->tx_size_255);
5019
5020         ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
5021                           pf->stat_prev_loaded, &prev_ps->tx_size_511,
5022                           &cur_ps->tx_size_511);
5023
5024         ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
5025                           GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
5026                           &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5027
5028         ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
5029                           GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
5030                           &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5031
5032         ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
5033                           GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
5034                           &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5035
5036         ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
5037                           &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5038
5039         ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
5040                           &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5041
5042         ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
5043                           &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5044
5045         ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
5046                           &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5047
5048         ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
5049                           &prev_ps->crc_errors, &cur_ps->crc_errors);
5050
5051         ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
5052                           &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5053
5054         ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
5055                           &prev_ps->mac_local_faults,
5056                           &cur_ps->mac_local_faults);
5057
5058         ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
5059                           &prev_ps->mac_remote_faults,
5060                           &cur_ps->mac_remote_faults);
5061
5062         ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
5063                           &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5064
5065         ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
5066                           &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5067
5068         ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
5069                           &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5070
5071         ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
5072                           &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5073
5074         ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
5075                           &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5076
5077         pf->stat_prev_loaded = true;
5078 }
5079
5080 /**
5081  * ice_get_stats64 - get statistics for network device structure
5082  * @netdev: network interface device structure
5083  * @stats: main device statistics structure
5084  */
5085 static
5086 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5087 {
5088         struct ice_netdev_priv *np = netdev_priv(netdev);
5089         struct rtnl_link_stats64 *vsi_stats;
5090         struct ice_vsi *vsi = np->vsi;
5091
5092         vsi_stats = &vsi->net_stats;
5093
5094         if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
5095                 return;
5096         /* netdev packet/byte stats come from ring counter. These are obtained
5097          * by summing up ring counters (done by ice_update_vsi_ring_stats).
5098          */
5099         ice_update_vsi_ring_stats(vsi);
5100         stats->tx_packets = vsi_stats->tx_packets;
5101         stats->tx_bytes = vsi_stats->tx_bytes;
5102         stats->rx_packets = vsi_stats->rx_packets;
5103         stats->rx_bytes = vsi_stats->rx_bytes;
5104
5105         /* The rest of the stats can be read from the hardware but instead we
5106          * just return values that the watchdog task has already obtained from
5107          * the hardware.
5108          */
5109         stats->multicast = vsi_stats->multicast;
5110         stats->tx_errors = vsi_stats->tx_errors;
5111         stats->tx_dropped = vsi_stats->tx_dropped;
5112         stats->rx_errors = vsi_stats->rx_errors;
5113         stats->rx_dropped = vsi_stats->rx_dropped;
5114         stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5115         stats->rx_length_errors = vsi_stats->rx_length_errors;
5116 }
5117
5118 /**
5119  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5120  * @vsi: VSI having NAPI disabled
5121  */
5122 static void ice_napi_disable_all(struct ice_vsi *vsi)
5123 {
5124         int q_idx;
5125
5126         if (!vsi->netdev)
5127                 return;
5128
5129         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
5130                 napi_disable(&vsi->q_vectors[q_idx]->napi);
5131 }
5132
5133 /**
5134  * ice_down - Shutdown the connection
5135  * @vsi: The VSI being stopped
5136  */
5137 int ice_down(struct ice_vsi *vsi)
5138 {
5139         int i, err;
5140
5141         /* Caller of this function is expected to set the
5142          * vsi->state __ICE_DOWN bit
5143          */
5144         if (vsi->netdev) {
5145                 netif_carrier_off(vsi->netdev);
5146                 netif_tx_disable(vsi->netdev);
5147         }
5148
5149         ice_vsi_dis_irq(vsi);
5150         err = ice_vsi_stop_tx_rx_rings(vsi);
5151         ice_napi_disable_all(vsi);
5152
5153         ice_for_each_txq(vsi, i)
5154                 ice_clean_tx_ring(vsi->tx_rings[i]);
5155
5156         ice_for_each_rxq(vsi, i)
5157                 ice_clean_rx_ring(vsi->rx_rings[i]);
5158
5159         if (err)
5160                 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5161                            vsi->vsi_num, vsi->vsw->sw_id);
5162         return err;
5163 }
5164
5165 /**
5166  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5167  * @vsi: VSI having resources allocated
5168  *
5169  * Return 0 on success, negative on failure
5170  */
5171 static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5172 {
5173         int i, err = 0;
5174
5175         if (!vsi->num_txq) {
5176                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
5177                         vsi->vsi_num);
5178                 return -EINVAL;
5179         }
5180
5181         ice_for_each_txq(vsi, i) {
5182                 err = ice_setup_tx_ring(vsi->tx_rings[i]);
5183                 if (err)
5184                         break;
5185         }
5186
5187         return err;
5188 }
5189
5190 /**
5191  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5192  * @vsi: VSI having resources allocated
5193  *
5194  * Return 0 on success, negative on failure
5195  */
5196 static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5197 {
5198         int i, err = 0;
5199
5200         if (!vsi->num_rxq) {
5201                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
5202                         vsi->vsi_num);
5203                 return -EINVAL;
5204         }
5205
5206         ice_for_each_rxq(vsi, i) {
5207                 err = ice_setup_rx_ring(vsi->rx_rings[i]);
5208                 if (err)
5209                         break;
5210         }
5211
5212         return err;
5213 }
5214
5215 /**
5216  * ice_vsi_req_irq - Request IRQ from the OS
5217  * @vsi: The VSI IRQ is being requested for
5218  * @basename: name for the vector
5219  *
5220  * Return 0 on success and a negative value on error
5221  */
5222 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
5223 {
5224         struct ice_pf *pf = vsi->back;
5225         int err = -EINVAL;
5226
5227         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
5228                 err = ice_vsi_req_irq_msix(vsi, basename);
5229
5230         return err;
5231 }
5232
5233 /**
5234  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
5235  * @vsi: the VSI having resources freed
5236  */
5237 static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
5238 {
5239         int i;
5240
5241         if (!vsi->tx_rings)
5242                 return;
5243
5244         ice_for_each_txq(vsi, i)
5245                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
5246                         ice_free_tx_ring(vsi->tx_rings[i]);
5247 }
5248
5249 /**
5250  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
5251  * @vsi: the VSI having resources freed
5252  */
5253 static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
5254 {
5255         int i;
5256
5257         if (!vsi->rx_rings)
5258                 return;
5259
5260         ice_for_each_rxq(vsi, i)
5261                 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
5262                         ice_free_rx_ring(vsi->rx_rings[i]);
5263 }
5264
5265 /**
5266  * ice_vsi_open - Called when a network interface is made active
5267  * @vsi: the VSI to open
5268  *
5269  * Initialization of the VSI
5270  *
5271  * Returns 0 on success, negative value on error
5272  */
5273 static int ice_vsi_open(struct ice_vsi *vsi)
5274 {
5275         char int_name[ICE_INT_NAME_STR_LEN];
5276         struct ice_pf *pf = vsi->back;
5277         int err;
5278
5279         /* allocate descriptors */
5280         err = ice_vsi_setup_tx_rings(vsi);
5281         if (err)
5282                 goto err_setup_tx;
5283
5284         err = ice_vsi_setup_rx_rings(vsi);
5285         if (err)
5286                 goto err_setup_rx;
5287
5288         err = ice_vsi_cfg(vsi);
5289         if (err)
5290                 goto err_setup_rx;
5291
5292         snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5293                  dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5294         err = ice_vsi_req_irq(vsi, int_name);
5295         if (err)
5296                 goto err_setup_rx;
5297
5298         /* Notify the stack of the actual queue counts. */
5299         err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5300         if (err)
5301                 goto err_set_qs;
5302
5303         err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5304         if (err)
5305                 goto err_set_qs;
5306
5307         err = ice_up_complete(vsi);
5308         if (err)
5309                 goto err_up_complete;
5310
5311         return 0;
5312
5313 err_up_complete:
5314         ice_down(vsi);
5315 err_set_qs:
5316         ice_vsi_free_irq(vsi);
5317 err_setup_rx:
5318         ice_vsi_free_rx_rings(vsi);
5319 err_setup_tx:
5320         ice_vsi_free_tx_rings(vsi);
5321
5322         return err;
5323 }
5324
5325 /**
5326  * ice_vsi_close - Shut down a VSI
5327  * @vsi: the VSI being shut down
5328  */
5329 static void ice_vsi_close(struct ice_vsi *vsi)
5330 {
5331         if (!test_and_set_bit(__ICE_DOWN, vsi->state))
5332                 ice_down(vsi);
5333
5334         ice_vsi_free_irq(vsi);
5335         ice_vsi_free_tx_rings(vsi);
5336         ice_vsi_free_rx_rings(vsi);
5337 }
5338
5339 /**
5340  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
5341  * @vsi: the VSI being removed
5342  */
5343 static void ice_rss_clean(struct ice_vsi *vsi)
5344 {
5345         struct ice_pf *pf;
5346
5347         pf = vsi->back;
5348
5349         if (vsi->rss_hkey_user)
5350                 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
5351         if (vsi->rss_lut_user)
5352                 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
5353 }
5354
5355 /**
5356  * ice_vsi_release - Delete a VSI and free its resources
5357  * @vsi: the VSI being removed
5358  *
5359  * Returns 0 on success or < 0 on error
5360  */
5361 static int ice_vsi_release(struct ice_vsi *vsi)
5362 {
5363         struct ice_pf *pf;
5364
5365         if (!vsi->back)
5366                 return -ENODEV;
5367         pf = vsi->back;
5368         /* do not unregister and free netdevs while driver is in the reset
5369          * recovery pending state. Since reset/rebuild happens through PF
5370          * service task workqueue, its not a good idea to unregister netdev
5371          * that is associated to the PF that is running the work queue items
5372          * currently. This is done to avoid check_flush_dependency() warning
5373          * on this wq
5374          */
5375         if (vsi->netdev && !ice_is_reset_recovery_pending(pf->state)) {
5376                 unregister_netdev(vsi->netdev);
5377                 free_netdev(vsi->netdev);
5378                 vsi->netdev = NULL;
5379         }
5380
5381         if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
5382                 ice_rss_clean(vsi);
5383
5384         /* Disable VSI and free resources */
5385         ice_vsi_dis_irq(vsi);
5386         ice_vsi_close(vsi);
5387
5388         /* reclaim interrupt vectors back to PF */
5389         ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
5390         pf->num_avail_msix += vsi->num_q_vectors;
5391
5392         ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
5393         ice_vsi_delete(vsi);
5394         ice_vsi_free_q_vectors(vsi);
5395         ice_vsi_clear_rings(vsi);
5396
5397         ice_vsi_put_qs(vsi);
5398         pf->q_left_tx += vsi->alloc_txq;
5399         pf->q_left_rx += vsi->alloc_rxq;
5400
5401         /* retain SW VSI data structure since it is needed to unregister and
5402          * free VSI netdev when PF is not in reset recovery pending state,\
5403          * for ex: during rmmod.
5404          */
5405         if (!ice_is_reset_recovery_pending(pf->state))
5406                 ice_vsi_clear(vsi);
5407
5408         return 0;
5409 }
5410
5411 /**
5412  * ice_vsi_release_all - Delete all VSIs
5413  * @pf: PF from which all VSIs are being removed
5414  */
5415 static void ice_vsi_release_all(struct ice_pf *pf)
5416 {
5417         int err, i;
5418
5419         if (!pf->vsi)
5420                 return;
5421
5422         for (i = 0; i < pf->num_alloc_vsi; i++) {
5423                 if (!pf->vsi[i])
5424                         continue;
5425
5426                 err = ice_vsi_release(pf->vsi[i]);
5427                 if (err)
5428                         dev_dbg(&pf->pdev->dev,
5429                                 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5430                                 i, err, pf->vsi[i]->vsi_num);
5431         }
5432 }
5433
5434 /**
5435  * ice_dis_vsi - pause a VSI
5436  * @vsi: the VSI being paused
5437  */
5438 static void ice_dis_vsi(struct ice_vsi *vsi)
5439 {
5440         if (test_bit(__ICE_DOWN, vsi->state))
5441                 return;
5442
5443         set_bit(__ICE_NEEDS_RESTART, vsi->state);
5444
5445         if (vsi->netdev && netif_running(vsi->netdev) &&
5446             vsi->type == ICE_VSI_PF) {
5447                 rtnl_lock();
5448                 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5449                 rtnl_unlock();
5450         } else {
5451                 ice_vsi_close(vsi);
5452         }
5453 }
5454
5455 /**
5456  * ice_ena_vsi - resume a VSI
5457  * @vsi: the VSI being resume
5458  */
5459 static int ice_ena_vsi(struct ice_vsi *vsi)
5460 {
5461         int err = 0;
5462
5463         if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
5464                 if (vsi->netdev && netif_running(vsi->netdev)) {
5465                         rtnl_lock();
5466                         err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5467                         rtnl_unlock();
5468                 }
5469
5470         return err;
5471 }
5472
5473 /**
5474  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
5475  * @pf: the PF
5476  */
5477 static void ice_pf_dis_all_vsi(struct ice_pf *pf)
5478 {
5479         int v;
5480
5481         ice_for_each_vsi(pf, v)
5482                 if (pf->vsi[v])
5483                         ice_dis_vsi(pf->vsi[v]);
5484 }
5485
5486 /**
5487  * ice_pf_ena_all_vsi - Resume all VSIs on a PF
5488  * @pf: the PF
5489  */
5490 static int ice_pf_ena_all_vsi(struct ice_pf *pf)
5491 {
5492         int v;
5493
5494         ice_for_each_vsi(pf, v)
5495                 if (pf->vsi[v])
5496                         if (ice_ena_vsi(pf->vsi[v]))
5497                                 return -EIO;
5498
5499         return 0;
5500 }
5501
5502 /**
5503  * ice_vsi_rebuild_all - rebuild all VSIs in pf
5504  * @pf: the PF
5505  */
5506 static int ice_vsi_rebuild_all(struct ice_pf *pf)
5507 {
5508         int i;
5509
5510         /* loop through pf->vsi array and reinit the VSI if found */
5511         for (i = 0; i < pf->num_alloc_vsi; i++) {
5512                 int err;
5513
5514                 if (!pf->vsi[i])
5515                         continue;
5516
5517                 err = ice_vsi_rebuild(pf->vsi[i]);
5518                 if (err) {
5519                         dev_err(&pf->pdev->dev,
5520                                 "VSI at index %d rebuild failed\n",
5521                                 pf->vsi[i]->idx);
5522                         return err;
5523                 }
5524
5525                 dev_info(&pf->pdev->dev,
5526                          "VSI at index %d rebuilt. vsi_num = 0x%x\n",
5527                          pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
5528         }
5529
5530         return 0;
5531 }
5532
5533 /**
5534  * ice_rebuild - rebuild after reset
5535  * @pf: pf to rebuild
5536  */
5537 static void ice_rebuild(struct ice_pf *pf)
5538 {
5539         struct device *dev = &pf->pdev->dev;
5540         struct ice_hw *hw = &pf->hw;
5541         enum ice_status ret;
5542         int err;
5543
5544         if (test_bit(__ICE_DOWN, pf->state))
5545                 goto clear_recovery;
5546
5547         dev_dbg(dev, "rebuilding pf\n");
5548
5549         ret = ice_init_all_ctrlq(hw);
5550         if (ret) {
5551                 dev_err(dev, "control queues init failed %d\n", ret);
5552                 goto err_init_ctrlq;
5553         }
5554
5555         ret = ice_clear_pf_cfg(hw);
5556         if (ret) {
5557                 dev_err(dev, "clear PF configuration failed %d\n", ret);
5558                 goto err_init_ctrlq;
5559         }
5560
5561         ice_clear_pxe_mode(hw);
5562
5563         ret = ice_get_caps(hw);
5564         if (ret) {
5565                 dev_err(dev, "ice_get_caps failed %d\n", ret);
5566                 goto err_init_ctrlq;
5567         }
5568
5569         err = ice_sched_init_port(hw->port_info);
5570         if (err)
5571                 goto err_sched_init_port;
5572
5573         err = ice_vsi_rebuild_all(pf);
5574         if (err) {
5575                 dev_err(dev, "ice_vsi_rebuild_all failed\n");
5576                 goto err_vsi_rebuild;
5577         }
5578
5579         ret = ice_replay_all_fltr(&pf->hw);
5580         if (ret) {
5581                 dev_err(&pf->pdev->dev,
5582                         "error replaying switch filter rules\n");
5583                 goto err_vsi_rebuild;
5584         }
5585
5586         /* start misc vector */
5587         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
5588                 err = ice_req_irq_msix_misc(pf);
5589                 if (err) {
5590                         dev_err(dev, "misc vector setup failed: %d\n", err);
5591                         goto err_vsi_rebuild;
5592                 }
5593         }
5594
5595         /* restart the VSIs that were rebuilt and running before the reset */
5596         err = ice_pf_ena_all_vsi(pf);
5597         if (err) {
5598                 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
5599                 /* no need to disable VSIs in tear down path in ice_rebuild()
5600                  * since its already taken care in ice_vsi_open()
5601                  */
5602                 goto err_vsi_rebuild;
5603         }
5604
5605         /* if we get here, reset flow is successful */
5606         clear_bit(__ICE_RESET_FAILED, pf->state);
5607         return;
5608
5609 err_vsi_rebuild:
5610         ice_vsi_release_all(pf);
5611 err_sched_init_port:
5612         ice_sched_cleanup_all(hw);
5613 err_init_ctrlq:
5614         ice_shutdown_all_ctrlq(hw);
5615         set_bit(__ICE_RESET_FAILED, pf->state);
5616 clear_recovery:
5617         /* set this bit in PF state to control service task scheduling */
5618         set_bit(__ICE_NEEDS_RESTART, pf->state);
5619         dev_err(dev, "Rebuild failed, unload and reload driver\n");
5620 }
5621
5622 /**
5623  * ice_change_mtu - NDO callback to change the MTU
5624  * @netdev: network interface device structure
5625  * @new_mtu: new value for maximum frame size
5626  *
5627  * Returns 0 on success, negative on failure
5628  */
5629 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
5630 {
5631         struct ice_netdev_priv *np = netdev_priv(netdev);
5632         struct ice_vsi *vsi = np->vsi;
5633         struct ice_pf *pf = vsi->back;
5634         u8 count = 0;
5635
5636         if (new_mtu == netdev->mtu) {
5637                 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
5638                 return 0;
5639         }
5640
5641         if (new_mtu < netdev->min_mtu) {
5642                 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
5643                            netdev->min_mtu);
5644                 return -EINVAL;
5645         } else if (new_mtu > netdev->max_mtu) {
5646                 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
5647                            netdev->min_mtu);
5648                 return -EINVAL;
5649         }
5650         /* if a reset is in progress, wait for some time for it to complete */
5651         do {
5652                 if (ice_is_reset_recovery_pending(pf->state)) {
5653                         count++;
5654                         usleep_range(1000, 2000);
5655                 } else {
5656                         break;
5657                 }
5658
5659         } while (count < 100);
5660
5661         if (count == 100) {
5662                 netdev_err(netdev, "can't change mtu. Device is busy\n");
5663                 return -EBUSY;
5664         }
5665
5666         netdev->mtu = new_mtu;
5667
5668         /* if VSI is up, bring it down and then back up */
5669         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
5670                 int err;
5671
5672                 err = ice_down(vsi);
5673                 if (err) {
5674                         netdev_err(netdev, "change mtu if_up err %d\n", err);
5675                         return err;
5676                 }
5677
5678                 err = ice_up(vsi);
5679                 if (err) {
5680                         netdev_err(netdev, "change mtu if_up err %d\n", err);
5681                         return err;
5682                 }
5683         }
5684
5685         netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
5686         return 0;
5687 }
5688
5689 /**
5690  * ice_set_rss - Set RSS keys and lut
5691  * @vsi: Pointer to VSI structure
5692  * @seed: RSS hash seed
5693  * @lut: Lookup table
5694  * @lut_size: Lookup table size
5695  *
5696  * Returns 0 on success, negative on failure
5697  */
5698 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5699 {
5700         struct ice_pf *pf = vsi->back;
5701         struct ice_hw *hw = &pf->hw;
5702         enum ice_status status;
5703
5704         if (seed) {
5705                 struct ice_aqc_get_set_rss_keys *buf =
5706                                   (struct ice_aqc_get_set_rss_keys *)seed;
5707
5708                 status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
5709
5710                 if (status) {
5711                         dev_err(&pf->pdev->dev,
5712                                 "Cannot set RSS key, err %d aq_err %d\n",
5713                                 status, hw->adminq.rq_last_status);
5714                         return -EIO;
5715                 }
5716         }
5717
5718         if (lut) {
5719                 status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
5720                                             vsi->rss_lut_type, lut, lut_size);
5721                 if (status) {
5722                         dev_err(&pf->pdev->dev,
5723                                 "Cannot set RSS lut, err %d aq_err %d\n",
5724                                 status, hw->adminq.rq_last_status);
5725                         return -EIO;
5726                 }
5727         }
5728
5729         return 0;
5730 }
5731
5732 /**
5733  * ice_get_rss - Get RSS keys and lut
5734  * @vsi: Pointer to VSI structure
5735  * @seed: Buffer to store the keys
5736  * @lut: Buffer to store the lookup table entries
5737  * @lut_size: Size of buffer to store the lookup table entries
5738  *
5739  * Returns 0 on success, negative on failure
5740  */
5741 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5742 {
5743         struct ice_pf *pf = vsi->back;
5744         struct ice_hw *hw = &pf->hw;
5745         enum ice_status status;
5746
5747         if (seed) {
5748                 struct ice_aqc_get_set_rss_keys *buf =
5749                                   (struct ice_aqc_get_set_rss_keys *)seed;
5750
5751                 status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
5752                 if (status) {
5753                         dev_err(&pf->pdev->dev,
5754                                 "Cannot get RSS key, err %d aq_err %d\n",
5755                                 status, hw->adminq.rq_last_status);
5756                         return -EIO;
5757                 }
5758         }
5759
5760         if (lut) {
5761                 status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
5762                                             vsi->rss_lut_type, lut, lut_size);
5763                 if (status) {
5764                         dev_err(&pf->pdev->dev,
5765                                 "Cannot get RSS lut, err %d aq_err %d\n",
5766                                 status, hw->adminq.rq_last_status);
5767                         return -EIO;
5768                 }
5769         }
5770
5771         return 0;
5772 }
5773
5774 /**
5775  * ice_bridge_getlink - Get the hardware bridge mode
5776  * @skb: skb buff
5777  * @pid: process id
5778  * @seq: RTNL message seq
5779  * @dev: the netdev being configured
5780  * @filter_mask: filter mask passed in
5781  * @nlflags: netlink flags passed in
5782  *
5783  * Return the bridge mode (VEB/VEPA)
5784  */
5785 static int
5786 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
5787                    struct net_device *dev, u32 filter_mask, int nlflags)
5788 {
5789         struct ice_netdev_priv *np = netdev_priv(dev);
5790         struct ice_vsi *vsi = np->vsi;
5791         struct ice_pf *pf = vsi->back;
5792         u16 bmode;
5793
5794         bmode = pf->first_sw->bridge_mode;
5795
5796         return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
5797                                        filter_mask, NULL);
5798 }
5799
5800 /**
5801  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
5802  * @vsi: Pointer to VSI structure
5803  * @bmode: Hardware bridge mode (VEB/VEPA)
5804  *
5805  * Returns 0 on success, negative on failure
5806  */
5807 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
5808 {
5809         struct device *dev = &vsi->back->pdev->dev;
5810         struct ice_aqc_vsi_props *vsi_props;
5811         struct ice_hw *hw = &vsi->back->hw;
5812         struct ice_vsi_ctx ctxt = { 0 };
5813         enum ice_status status;
5814
5815         vsi_props = &vsi->info;
5816         ctxt.info = vsi->info;
5817
5818         if (bmode == BRIDGE_MODE_VEB)
5819                 /* change from VEPA to VEB mode */
5820                 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
5821         else
5822                 /* change from VEB to VEPA mode */
5823                 ctxt.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
5824         ctxt.vsi_num = vsi->vsi_num;
5825         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
5826         status = ice_aq_update_vsi(hw, &ctxt, NULL);
5827         if (status) {
5828                 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
5829                         bmode, status, hw->adminq.sq_last_status);
5830                 return -EIO;
5831         }
5832         /* Update sw flags for book keeping */
5833         vsi_props->sw_flags = ctxt.info.sw_flags;
5834
5835         return 0;
5836 }
5837
5838 /**
5839  * ice_bridge_setlink - Set the hardware bridge mode
5840  * @dev: the netdev being configured
5841  * @nlh: RTNL message
5842  * @flags: bridge setlink flags
5843  *
5844  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
5845  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
5846  * not already set for all VSIs connected to this switch. And also update the
5847  * unicast switch filter rules for the corresponding switch of the netdev.
5848  */
5849 static int
5850 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
5851                    u16 __always_unused flags)
5852 {
5853         struct ice_netdev_priv *np = netdev_priv(dev);
5854         struct ice_pf *pf = np->vsi->back;
5855         struct nlattr *attr, *br_spec;
5856         struct ice_hw *hw = &pf->hw;
5857         enum ice_status status;
5858         struct ice_sw *pf_sw;
5859         int rem, v, err = 0;
5860
5861         pf_sw = pf->first_sw;
5862         /* find the attribute in the netlink message */
5863         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5864
5865         nla_for_each_nested(attr, br_spec, rem) {
5866                 __u16 mode;
5867
5868                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
5869                         continue;
5870                 mode = nla_get_u16(attr);
5871                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
5872                         return -EINVAL;
5873                 /* Continue  if bridge mode is not being flipped */
5874                 if (mode == pf_sw->bridge_mode)
5875                         continue;
5876                 /* Iterates through the PF VSI list and update the loopback
5877                  * mode of the VSI
5878                  */
5879                 ice_for_each_vsi(pf, v) {
5880                         if (!pf->vsi[v])
5881                                 continue;
5882                         err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
5883                         if (err)
5884                                 return err;
5885                 }
5886
5887                 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
5888                 /* Update the unicast switch filter rules for the corresponding
5889                  * switch of the netdev
5890                  */
5891                 status = ice_update_sw_rule_bridge_mode(hw);
5892                 if (status) {
5893                         netdev_err(dev, "update SW_RULE for bridge mode failed,  = %d err %d aq_err %d\n",
5894                                    mode, status, hw->adminq.sq_last_status);
5895                         /* revert hw->evb_veb */
5896                         hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
5897                         return -EIO;
5898                 }
5899
5900                 pf_sw->bridge_mode = mode;
5901         }
5902
5903         return 0;
5904 }
5905
5906 /**
5907  * ice_tx_timeout - Respond to a Tx Hang
5908  * @netdev: network interface device structure
5909  */
5910 static void ice_tx_timeout(struct net_device *netdev)
5911 {
5912         struct ice_netdev_priv *np = netdev_priv(netdev);
5913         struct ice_ring *tx_ring = NULL;
5914         struct ice_vsi *vsi = np->vsi;
5915         struct ice_pf *pf = vsi->back;
5916         u32 head, val = 0, i;
5917         int hung_queue = -1;
5918
5919         pf->tx_timeout_count++;
5920
5921         /* find the stopped queue the same way the stack does */
5922         for (i = 0; i < netdev->num_tx_queues; i++) {
5923                 struct netdev_queue *q;
5924                 unsigned long trans_start;
5925
5926                 q = netdev_get_tx_queue(netdev, i);
5927                 trans_start = q->trans_start;
5928                 if (netif_xmit_stopped(q) &&
5929                     time_after(jiffies,
5930                                (trans_start + netdev->watchdog_timeo))) {
5931                         hung_queue = i;
5932                         break;
5933                 }
5934         }
5935
5936         if (i == netdev->num_tx_queues) {
5937                 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
5938         } else {
5939                 /* now that we have an index, find the tx_ring struct */
5940                 for (i = 0; i < vsi->num_txq; i++) {
5941                         if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
5942                                 if (hung_queue ==
5943                                     vsi->tx_rings[i]->q_index) {
5944                                         tx_ring = vsi->tx_rings[i];
5945                                         break;
5946                                 }
5947                         }
5948                 }
5949         }
5950
5951         /* Reset recovery level if enough time has elapsed after last timeout.
5952          * Also ensure no new reset action happens before next timeout period.
5953          */
5954         if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
5955                 pf->tx_timeout_recovery_level = 1;
5956         else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
5957                                        netdev->watchdog_timeo)))
5958                 return;
5959
5960         if (tx_ring) {
5961                 head = tx_ring->next_to_clean;
5962                 /* Read interrupt register */
5963                 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
5964                         val = rd32(&pf->hw,
5965                                    GLINT_DYN_CTL(tx_ring->q_vector->v_idx +
5966                                                 tx_ring->vsi->base_vector - 1));
5967
5968                 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
5969                             vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
5970                             head, tx_ring->next_to_use,
5971                             readl(tx_ring->tail), val);
5972         }
5973
5974         pf->tx_timeout_last_recovery = jiffies;
5975         netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
5976                     pf->tx_timeout_recovery_level, hung_queue);
5977
5978         switch (pf->tx_timeout_recovery_level) {
5979         case 1:
5980                 set_bit(__ICE_PFR_REQ, pf->state);
5981                 break;
5982         case 2:
5983                 set_bit(__ICE_CORER_REQ, pf->state);
5984                 break;
5985         case 3:
5986                 set_bit(__ICE_GLOBR_REQ, pf->state);
5987                 break;
5988         default:
5989                 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
5990                 set_bit(__ICE_DOWN, pf->state);
5991                 set_bit(__ICE_NEEDS_RESTART, vsi->state);
5992                 set_bit(__ICE_SERVICE_DIS, pf->state);
5993                 break;
5994         }
5995
5996         ice_service_task_schedule(pf);
5997         pf->tx_timeout_recovery_level++;
5998 }
5999
6000 /**
6001  * ice_open - Called when a network interface becomes active
6002  * @netdev: network interface device structure
6003  *
6004  * The open entry point is called when a network interface is made
6005  * active by the system (IFF_UP).  At this point all resources needed
6006  * for transmit and receive operations are allocated, the interrupt
6007  * handler is registered with the OS, the netdev watchdog is enabled,
6008  * and the stack is notified that the interface is ready.
6009  *
6010  * Returns 0 on success, negative value on failure
6011  */
6012 static int ice_open(struct net_device *netdev)
6013 {
6014         struct ice_netdev_priv *np = netdev_priv(netdev);
6015         struct ice_vsi *vsi = np->vsi;
6016         int err;
6017
6018         if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
6019                 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6020                 return -EIO;
6021         }
6022
6023         netif_carrier_off(netdev);
6024
6025         err = ice_vsi_open(vsi);
6026
6027         if (err)
6028                 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6029                            vsi->vsi_num, vsi->vsw->sw_id);
6030         return err;
6031 }
6032
6033 /**
6034  * ice_stop - Disables a network interface
6035  * @netdev: network interface device structure
6036  *
6037  * The stop entry point is called when an interface is de-activated by the OS,
6038  * and the netdevice enters the DOWN state.  The hardware is still under the
6039  * driver's control, but the netdev interface is disabled.
6040  *
6041  * Returns success only - not allowed to fail
6042  */
6043 static int ice_stop(struct net_device *netdev)
6044 {
6045         struct ice_netdev_priv *np = netdev_priv(netdev);
6046         struct ice_vsi *vsi = np->vsi;
6047
6048         ice_vsi_close(vsi);
6049
6050         return 0;
6051 }
6052
6053 /**
6054  * ice_features_check - Validate encapsulated packet conforms to limits
6055  * @skb: skb buffer
6056  * @netdev: This port's netdev
6057  * @features: Offload features that the stack believes apply
6058  */
6059 static netdev_features_t
6060 ice_features_check(struct sk_buff *skb,
6061                    struct net_device __always_unused *netdev,
6062                    netdev_features_t features)
6063 {
6064         size_t len;
6065
6066         /* No point in doing any of this if neither checksum nor GSO are
6067          * being requested for this frame.  We can rule out both by just
6068          * checking for CHECKSUM_PARTIAL
6069          */
6070         if (skb->ip_summed != CHECKSUM_PARTIAL)
6071                 return features;
6072
6073         /* We cannot support GSO if the MSS is going to be less than
6074          * 64 bytes.  If it is then we need to drop support for GSO.
6075          */
6076         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6077                 features &= ~NETIF_F_GSO_MASK;
6078
6079         len = skb_network_header(skb) - skb->data;
6080         if (len & ~(ICE_TXD_MACLEN_MAX))
6081                 goto out_rm_features;
6082
6083         len = skb_transport_header(skb) - skb_network_header(skb);
6084         if (len & ~(ICE_TXD_IPLEN_MAX))
6085                 goto out_rm_features;
6086
6087         if (skb->encapsulation) {
6088                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
6089                 if (len & ~(ICE_TXD_L4LEN_MAX))
6090                         goto out_rm_features;
6091
6092                 len = skb_inner_transport_header(skb) -
6093                       skb_inner_network_header(skb);
6094                 if (len & ~(ICE_TXD_IPLEN_MAX))
6095                         goto out_rm_features;
6096         }
6097
6098         return features;
6099 out_rm_features:
6100         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6101 }
6102
6103 static const struct net_device_ops ice_netdev_ops = {
6104         .ndo_open = ice_open,
6105         .ndo_stop = ice_stop,
6106         .ndo_start_xmit = ice_start_xmit,
6107         .ndo_features_check = ice_features_check,
6108         .ndo_set_rx_mode = ice_set_rx_mode,
6109         .ndo_set_mac_address = ice_set_mac_address,
6110         .ndo_validate_addr = eth_validate_addr,
6111         .ndo_change_mtu = ice_change_mtu,
6112         .ndo_get_stats64 = ice_get_stats64,
6113         .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6114         .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6115         .ndo_set_features = ice_set_features,
6116         .ndo_bridge_getlink = ice_bridge_getlink,
6117         .ndo_bridge_setlink = ice_bridge_setlink,
6118         .ndo_fdb_add = ice_fdb_add,
6119         .ndo_fdb_del = ice_fdb_del,
6120         .ndo_tx_timeout = ice_tx_timeout,
6121 };