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