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