]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_main.c
c8cf2c35ecbb0c55105f061741e5086e7483e94b
[linux.git] / drivers / net / ethernet / intel / ice / ice_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include "ice.h"
9 #include "ice_lib.h"
10 #include "ice_dcb_lib.h"
11
12 #define DRV_VERSION     "0.7.4-k"
13 #define DRV_SUMMARY     "Intel(R) Ethernet Connection E800 Series Linux Driver"
14 const char ice_drv_ver[] = DRV_VERSION;
15 static const char ice_driver_string[] = DRV_SUMMARY;
16 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
17
18 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
19 MODULE_DESCRIPTION(DRV_SUMMARY);
20 MODULE_LICENSE("GPL v2");
21 MODULE_VERSION(DRV_VERSION);
22
23 static int debug = -1;
24 module_param(debug, int, 0644);
25 #ifndef CONFIG_DYNAMIC_DEBUG
26 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
27 #else
28 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
29 #endif /* !CONFIG_DYNAMIC_DEBUG */
30
31 static struct workqueue_struct *ice_wq;
32 static const struct net_device_ops ice_netdev_ops;
33
34 static void ice_rebuild(struct ice_pf *pf);
35
36 static void ice_vsi_release_all(struct ice_pf *pf);
37 static void ice_update_vsi_stats(struct ice_vsi *vsi);
38 static void ice_update_pf_stats(struct ice_pf *pf);
39
40 /**
41  * ice_get_tx_pending - returns number of Tx descriptors not processed
42  * @ring: the ring of descriptors
43  */
44 static u32 ice_get_tx_pending(struct ice_ring *ring)
45 {
46         u32 head, tail;
47
48         head = ring->next_to_clean;
49         tail = readl(ring->tail);
50
51         if (head != tail)
52                 return (head < tail) ?
53                         tail - head : (tail + ring->count - head);
54         return 0;
55 }
56
57 /**
58  * ice_check_for_hang_subtask - check for and recover hung queues
59  * @pf: pointer to PF struct
60  */
61 static void ice_check_for_hang_subtask(struct ice_pf *pf)
62 {
63         struct ice_vsi *vsi = NULL;
64         struct ice_hw *hw;
65         unsigned int i;
66         int packets;
67         u32 v;
68
69         ice_for_each_vsi(pf, v)
70                 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
71                         vsi = pf->vsi[v];
72                         break;
73                 }
74
75         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
76                 return;
77
78         if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
79                 return;
80
81         hw = &vsi->back->hw;
82
83         for (i = 0; i < vsi->num_txq; i++) {
84                 struct ice_ring *tx_ring = vsi->tx_rings[i];
85
86                 if (tx_ring && tx_ring->desc) {
87                         /* If packet counter has not changed the queue is
88                          * likely stalled, so force an interrupt for this
89                          * queue.
90                          *
91                          * prev_pkt would be negative if there was no
92                          * pending work.
93                          */
94                         packets = tx_ring->stats.pkts & INT_MAX;
95                         if (tx_ring->tx_stats.prev_pkt == packets) {
96                                 /* Trigger sw interrupt to revive the queue */
97                                 ice_trigger_sw_intr(hw, tx_ring->q_vector);
98                                 continue;
99                         }
100
101                         /* Memory barrier between read of packet count and call
102                          * to ice_get_tx_pending()
103                          */
104                         smp_rmb();
105                         tx_ring->tx_stats.prev_pkt =
106                             ice_get_tx_pending(tx_ring) ? packets : -1;
107                 }
108         }
109 }
110
111 /**
112  * ice_init_mac_fltr - Set initial MAC filters
113  * @pf: board private structure
114  *
115  * Set initial set of mac filters for PF VSI; configure filters for permanent
116  * address and broadcast address. If an error is encountered, netdevice will be
117  * unregistered.
118  */
119 static int ice_init_mac_fltr(struct ice_pf *pf)
120 {
121         LIST_HEAD(tmp_add_list);
122         u8 broadcast[ETH_ALEN];
123         struct ice_vsi *vsi;
124         int status;
125
126         vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
127         if (!vsi)
128                 return -EINVAL;
129
130         /* To add a MAC filter, first add the MAC to a list and then
131          * pass the list to ice_add_mac.
132          */
133
134          /* Add a unicast MAC filter so the VSI can get its packets */
135         status = ice_add_mac_to_list(vsi, &tmp_add_list,
136                                      vsi->port_info->mac.perm_addr);
137         if (status)
138                 goto unregister;
139
140         /* VSI needs to receive broadcast traffic, so add the broadcast
141          * MAC address to the list as well.
142          */
143         eth_broadcast_addr(broadcast);
144         status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
145         if (status)
146                 goto free_mac_list;
147
148         /* Program MAC filters for entries in tmp_add_list */
149         status = ice_add_mac(&pf->hw, &tmp_add_list);
150         if (status)
151                 status = -ENOMEM;
152
153 free_mac_list:
154         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
155
156 unregister:
157         /* We aren't useful with no MAC filters, so unregister if we
158          * had an error
159          */
160         if (status && vsi->netdev->reg_state == NETREG_REGISTERED) {
161                 dev_err(&pf->pdev->dev,
162                         "Could not add MAC filters error %d. Unregistering device\n",
163                         status);
164                 unregister_netdev(vsi->netdev);
165                 free_netdev(vsi->netdev);
166                 vsi->netdev = NULL;
167         }
168
169         return status;
170 }
171
172 /**
173  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
174  * @netdev: the net device on which the sync is happening
175  * @addr: MAC address to sync
176  *
177  * This is a callback function which is called by the in kernel device sync
178  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
179  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
180  * MAC filters from the hardware.
181  */
182 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
183 {
184         struct ice_netdev_priv *np = netdev_priv(netdev);
185         struct ice_vsi *vsi = np->vsi;
186
187         if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
188                 return -EINVAL;
189
190         return 0;
191 }
192
193 /**
194  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
195  * @netdev: the net device on which the unsync is happening
196  * @addr: MAC address to unsync
197  *
198  * This is a callback function which is called by the in kernel device unsync
199  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
200  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
201  * delete the MAC filters from the hardware.
202  */
203 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
204 {
205         struct ice_netdev_priv *np = netdev_priv(netdev);
206         struct ice_vsi *vsi = np->vsi;
207
208         if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
209                 return -EINVAL;
210
211         return 0;
212 }
213
214 /**
215  * ice_vsi_fltr_changed - check if filter state changed
216  * @vsi: VSI to be checked
217  *
218  * returns true if filter state has changed, false otherwise.
219  */
220 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
221 {
222         return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
223                test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
224                test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
225 }
226
227 /**
228  * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
229  * @vsi: the VSI being configured
230  * @promisc_m: mask of promiscuous config bits
231  * @set_promisc: enable or disable promisc flag request
232  *
233  */
234 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
235 {
236         struct ice_hw *hw = &vsi->back->hw;
237         enum ice_status status = 0;
238
239         if (vsi->type != ICE_VSI_PF)
240                 return 0;
241
242         if (vsi->vlan_ena) {
243                 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
244                                                   set_promisc);
245         } else {
246                 if (set_promisc)
247                         status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
248                                                      0);
249                 else
250                         status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
251                                                        0);
252         }
253
254         if (status)
255                 return -EIO;
256
257         return 0;
258 }
259
260 /**
261  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
262  * @vsi: ptr to the VSI
263  *
264  * Push any outstanding VSI filter changes through the AdminQ.
265  */
266 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
267 {
268         struct device *dev = &vsi->back->pdev->dev;
269         struct net_device *netdev = vsi->netdev;
270         bool promisc_forced_on = false;
271         struct ice_pf *pf = vsi->back;
272         struct ice_hw *hw = &pf->hw;
273         enum ice_status status = 0;
274         u32 changed_flags = 0;
275         u8 promisc_m;
276         int err = 0;
277
278         if (!vsi->netdev)
279                 return -EINVAL;
280
281         while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
282                 usleep_range(1000, 2000);
283
284         changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
285         vsi->current_netdev_flags = vsi->netdev->flags;
286
287         INIT_LIST_HEAD(&vsi->tmp_sync_list);
288         INIT_LIST_HEAD(&vsi->tmp_unsync_list);
289
290         if (ice_vsi_fltr_changed(vsi)) {
291                 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
292                 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
293                 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
294
295                 /* grab the netdev's addr_list_lock */
296                 netif_addr_lock_bh(netdev);
297                 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
298                               ice_add_mac_to_unsync_list);
299                 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
300                               ice_add_mac_to_unsync_list);
301                 /* our temp lists are populated. release lock */
302                 netif_addr_unlock_bh(netdev);
303         }
304
305         /* Remove MAC addresses in the unsync list */
306         status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
307         ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
308         if (status) {
309                 netdev_err(netdev, "Failed to delete MAC filters\n");
310                 /* if we failed because of alloc failures, just bail */
311                 if (status == ICE_ERR_NO_MEMORY) {
312                         err = -ENOMEM;
313                         goto out;
314                 }
315         }
316
317         /* Add MAC addresses in the sync list */
318         status = ice_add_mac(hw, &vsi->tmp_sync_list);
319         ice_free_fltr_list(dev, &vsi->tmp_sync_list);
320         /* If filter is added successfully or already exists, do not go into
321          * 'if' condition and report it as error. Instead continue processing
322          * rest of the function.
323          */
324         if (status && status != ICE_ERR_ALREADY_EXISTS) {
325                 netdev_err(netdev, "Failed to add MAC filters\n");
326                 /* If there is no more space for new umac filters, VSI
327                  * should go into promiscuous mode. There should be some
328                  * space reserved for promiscuous filters.
329                  */
330                 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
331                     !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
332                                       vsi->state)) {
333                         promisc_forced_on = true;
334                         netdev_warn(netdev,
335                                     "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
336                                     vsi->vsi_num);
337                 } else {
338                         err = -EIO;
339                         goto out;
340                 }
341         }
342         /* check for changes in promiscuous modes */
343         if (changed_flags & IFF_ALLMULTI) {
344                 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
345                         if (vsi->vlan_ena)
346                                 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
347                         else
348                                 promisc_m = ICE_MCAST_PROMISC_BITS;
349
350                         err = ice_cfg_promisc(vsi, promisc_m, true);
351                         if (err) {
352                                 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
353                                            vsi->vsi_num);
354                                 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
355                                 goto out_promisc;
356                         }
357                 } else if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
358                         if (vsi->vlan_ena)
359                                 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
360                         else
361                                 promisc_m = ICE_MCAST_PROMISC_BITS;
362
363                         err = ice_cfg_promisc(vsi, promisc_m, false);
364                         if (err) {
365                                 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
366                                            vsi->vsi_num);
367                                 vsi->current_netdev_flags |= IFF_ALLMULTI;
368                                 goto out_promisc;
369                         }
370                 }
371         }
372
373         if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
374             test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
375                 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
376                 if (vsi->current_netdev_flags & IFF_PROMISC) {
377                         /* Apply Rx filter rule to get traffic from wire */
378                         status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
379                                                   ICE_FLTR_RX);
380                         if (status) {
381                                 netdev_err(netdev, "Error setting default VSI %i Rx rule\n",
382                                            vsi->vsi_num);
383                                 vsi->current_netdev_flags &= ~IFF_PROMISC;
384                                 err = -EIO;
385                                 goto out_promisc;
386                         }
387                 } else {
388                         /* Clear Rx filter to remove traffic from wire */
389                         status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
390                                                   ICE_FLTR_RX);
391                         if (status) {
392                                 netdev_err(netdev, "Error clearing default VSI %i Rx rule\n",
393                                            vsi->vsi_num);
394                                 vsi->current_netdev_flags |= IFF_PROMISC;
395                                 err = -EIO;
396                                 goto out_promisc;
397                         }
398                 }
399         }
400         goto exit;
401
402 out_promisc:
403         set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
404         goto exit;
405 out:
406         /* if something went wrong then set the changed flag so we try again */
407         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
408         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
409 exit:
410         clear_bit(__ICE_CFG_BUSY, vsi->state);
411         return err;
412 }
413
414 /**
415  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
416  * @pf: board private structure
417  */
418 static void ice_sync_fltr_subtask(struct ice_pf *pf)
419 {
420         int v;
421
422         if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
423                 return;
424
425         clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
426
427         ice_for_each_vsi(pf, v)
428                 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
429                     ice_vsi_sync_fltr(pf->vsi[v])) {
430                         /* come back and try again later */
431                         set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
432                         break;
433                 }
434 }
435
436 /**
437  * ice_dis_vsi - pause a VSI
438  * @vsi: the VSI being paused
439  * @locked: is the rtnl_lock already held
440  */
441 static void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
442 {
443         if (test_bit(__ICE_DOWN, vsi->state))
444                 return;
445
446         set_bit(__ICE_NEEDS_RESTART, vsi->state);
447
448         if (vsi->type == ICE_VSI_PF && vsi->netdev) {
449                 if (netif_running(vsi->netdev)) {
450                         if (!locked) {
451                                 rtnl_lock();
452                                 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
453                                 rtnl_unlock();
454                         } else {
455                                 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
456                         }
457                 } else {
458                         ice_vsi_close(vsi);
459                 }
460         }
461 }
462
463 /**
464  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
465  * @pf: the PF
466  * @locked: is the rtnl_lock already held
467  */
468 #ifdef CONFIG_DCB
469 void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
470 #else
471 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
472 #endif /* CONFIG_DCB */
473 {
474         int v;
475
476         ice_for_each_vsi(pf, v)
477                 if (pf->vsi[v])
478                         ice_dis_vsi(pf->vsi[v], locked);
479 }
480
481 /**
482  * ice_prepare_for_reset - prep for the core to reset
483  * @pf: board private structure
484  *
485  * Inform or close all dependent features in prep for reset.
486  */
487 static void
488 ice_prepare_for_reset(struct ice_pf *pf)
489 {
490         struct ice_hw *hw = &pf->hw;
491
492         /* already prepared for reset */
493         if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
494                 return;
495
496         /* Notify VFs of impending reset */
497         if (ice_check_sq_alive(hw, &hw->mailboxq))
498                 ice_vc_notify_reset(pf);
499
500         /* disable the VSIs and their queues that are not already DOWN */
501         ice_pf_dis_all_vsi(pf, false);
502
503         if (hw->port_info)
504                 ice_sched_clear_port(hw->port_info);
505
506         ice_shutdown_all_ctrlq(hw);
507
508         set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
509 }
510
511 /**
512  * ice_do_reset - Initiate one of many types of resets
513  * @pf: board private structure
514  * @reset_type: reset type requested
515  * before this function was called.
516  */
517 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
518 {
519         struct device *dev = &pf->pdev->dev;
520         struct ice_hw *hw = &pf->hw;
521
522         dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
523         WARN_ON(in_interrupt());
524
525         ice_prepare_for_reset(pf);
526
527         /* trigger the reset */
528         if (ice_reset(hw, reset_type)) {
529                 dev_err(dev, "reset %d failed\n", reset_type);
530                 set_bit(__ICE_RESET_FAILED, pf->state);
531                 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
532                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
533                 clear_bit(__ICE_PFR_REQ, pf->state);
534                 clear_bit(__ICE_CORER_REQ, pf->state);
535                 clear_bit(__ICE_GLOBR_REQ, pf->state);
536                 return;
537         }
538
539         /* PFR is a bit of a special case because it doesn't result in an OICR
540          * interrupt. So for PFR, rebuild after the reset and clear the reset-
541          * associated state bits.
542          */
543         if (reset_type == ICE_RESET_PFR) {
544                 pf->pfr_count++;
545                 ice_rebuild(pf);
546                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
547                 clear_bit(__ICE_PFR_REQ, pf->state);
548                 ice_reset_all_vfs(pf, true);
549         }
550 }
551
552 /**
553  * ice_reset_subtask - Set up for resetting the device and driver
554  * @pf: board private structure
555  */
556 static void ice_reset_subtask(struct ice_pf *pf)
557 {
558         enum ice_reset_req reset_type = ICE_RESET_INVAL;
559
560         /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
561          * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
562          * of reset is pending and sets bits in pf->state indicating the reset
563          * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
564          * prepare for pending reset if not already (for PF software-initiated
565          * global resets the software should already be prepared for it as
566          * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
567          * by firmware or software on other PFs, that bit is not set so prepare
568          * for the reset now), poll for reset done, rebuild and return.
569          */
570         if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
571                 /* Perform the largest reset requested */
572                 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
573                         reset_type = ICE_RESET_CORER;
574                 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
575                         reset_type = ICE_RESET_GLOBR;
576                 /* return if no valid reset type requested */
577                 if (reset_type == ICE_RESET_INVAL)
578                         return;
579                 ice_prepare_for_reset(pf);
580
581                 /* make sure we are ready to rebuild */
582                 if (ice_check_reset(&pf->hw)) {
583                         set_bit(__ICE_RESET_FAILED, pf->state);
584                 } else {
585                         /* done with reset. start rebuild */
586                         pf->hw.reset_ongoing = false;
587                         ice_rebuild(pf);
588                         /* clear bit to resume normal operations, but
589                          * ICE_NEEDS_RESTART bit is set in case rebuild failed
590                          */
591                         clear_bit(__ICE_RESET_OICR_RECV, pf->state);
592                         clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
593                         clear_bit(__ICE_PFR_REQ, pf->state);
594                         clear_bit(__ICE_CORER_REQ, pf->state);
595                         clear_bit(__ICE_GLOBR_REQ, pf->state);
596                         ice_reset_all_vfs(pf, true);
597                 }
598
599                 return;
600         }
601
602         /* No pending resets to finish processing. Check for new resets */
603         if (test_bit(__ICE_PFR_REQ, pf->state))
604                 reset_type = ICE_RESET_PFR;
605         if (test_bit(__ICE_CORER_REQ, pf->state))
606                 reset_type = ICE_RESET_CORER;
607         if (test_bit(__ICE_GLOBR_REQ, pf->state))
608                 reset_type = ICE_RESET_GLOBR;
609         /* If no valid reset type requested just return */
610         if (reset_type == ICE_RESET_INVAL)
611                 return;
612
613         /* reset if not already down or busy */
614         if (!test_bit(__ICE_DOWN, pf->state) &&
615             !test_bit(__ICE_CFG_BUSY, pf->state)) {
616                 ice_do_reset(pf, reset_type);
617         }
618 }
619
620 /**
621  * ice_print_link_msg - print link up or down message
622  * @vsi: the VSI whose link status is being queried
623  * @isup: boolean for if the link is now up or down
624  */
625 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
626 {
627         struct ice_aqc_get_phy_caps_data *caps;
628         enum ice_status status;
629         const char *fec_req;
630         const char *speed;
631         const char *fec;
632         const char *fc;
633
634         if (!vsi)
635                 return;
636
637         if (vsi->current_isup == isup)
638                 return;
639
640         vsi->current_isup = isup;
641
642         if (!isup) {
643                 netdev_info(vsi->netdev, "NIC Link is Down\n");
644                 return;
645         }
646
647         switch (vsi->port_info->phy.link_info.link_speed) {
648         case ICE_AQ_LINK_SPEED_40GB:
649                 speed = "40 G";
650                 break;
651         case ICE_AQ_LINK_SPEED_25GB:
652                 speed = "25 G";
653                 break;
654         case ICE_AQ_LINK_SPEED_20GB:
655                 speed = "20 G";
656                 break;
657         case ICE_AQ_LINK_SPEED_10GB:
658                 speed = "10 G";
659                 break;
660         case ICE_AQ_LINK_SPEED_5GB:
661                 speed = "5 G";
662                 break;
663         case ICE_AQ_LINK_SPEED_2500MB:
664                 speed = "2.5 G";
665                 break;
666         case ICE_AQ_LINK_SPEED_1000MB:
667                 speed = "1 G";
668                 break;
669         case ICE_AQ_LINK_SPEED_100MB:
670                 speed = "100 M";
671                 break;
672         default:
673                 speed = "Unknown";
674                 break;
675         }
676
677         switch (vsi->port_info->fc.current_mode) {
678         case ICE_FC_FULL:
679                 fc = "RX/TX";
680                 break;
681         case ICE_FC_TX_PAUSE:
682                 fc = "TX";
683                 break;
684         case ICE_FC_RX_PAUSE:
685                 fc = "RX";
686                 break;
687         case ICE_FC_NONE:
688                 fc = "None";
689                 break;
690         default:
691                 fc = "Unknown";
692                 break;
693         }
694
695         /* Get FEC mode based on negotiated link info */
696         switch (vsi->port_info->phy.link_info.fec_info) {
697         case ICE_AQ_LINK_25G_RS_528_FEC_EN:
698                 /* fall through */
699         case ICE_AQ_LINK_25G_RS_544_FEC_EN:
700                 fec = "RS-FEC";
701                 break;
702         case ICE_AQ_LINK_25G_KR_FEC_EN:
703                 fec = "FC-FEC/BASE-R";
704                 break;
705         default:
706                 fec = "NONE";
707                 break;
708         }
709
710         /* Get FEC mode requested based on PHY caps last SW configuration */
711         caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
712         if (!caps) {
713                 fec_req = "Unknown";
714                 goto done;
715         }
716
717         status = ice_aq_get_phy_caps(vsi->port_info, false,
718                                      ICE_AQC_REPORT_SW_CFG, caps, NULL);
719         if (status)
720                 netdev_info(vsi->netdev, "Get phy capability failed.\n");
721
722         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
723             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
724                 fec_req = "RS-FEC";
725         else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
726                  caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
727                 fec_req = "FC-FEC/BASE-R";
728         else
729                 fec_req = "NONE";
730
731         devm_kfree(&vsi->back->pdev->dev, caps);
732
733 done:
734         netdev_info(vsi->netdev, "NIC Link is up %sbps, Requested FEC: %s, FEC: %s, Flow Control: %s\n",
735                     speed, fec_req, fec, fc);
736 }
737
738 /**
739  * ice_vsi_link_event - update the VSI's netdev
740  * @vsi: the VSI on which the link event occurred
741  * @link_up: whether or not the VSI needs to be set up or down
742  */
743 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
744 {
745         if (!vsi)
746                 return;
747
748         if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
749                 return;
750
751         if (vsi->type == ICE_VSI_PF) {
752                 if (link_up == netif_carrier_ok(vsi->netdev))
753                         return;
754
755                 if (link_up) {
756                         netif_carrier_on(vsi->netdev);
757                         netif_tx_wake_all_queues(vsi->netdev);
758                 } else {
759                         netif_carrier_off(vsi->netdev);
760                         netif_tx_stop_all_queues(vsi->netdev);
761                 }
762         }
763 }
764
765 /**
766  * ice_link_event - process the link event
767  * @pf: pf that the link event is associated with
768  * @pi: port_info for the port that the link event is associated with
769  * @link_up: true if the physical link is up and false if it is down
770  * @link_speed: current link speed received from the link event
771  *
772  * Returns 0 on success and negative on failure
773  */
774 static int
775 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
776                u16 link_speed)
777 {
778         struct ice_phy_info *phy_info;
779         struct ice_vsi *vsi;
780         u16 old_link_speed;
781         bool old_link;
782         int result;
783
784         phy_info = &pi->phy;
785         phy_info->link_info_old = phy_info->link_info;
786
787         old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
788         old_link_speed = phy_info->link_info_old.link_speed;
789
790         /* update the link info structures and re-enable link events,
791          * don't bail on failure due to other book keeping needed
792          */
793         result = ice_update_link_info(pi);
794         if (result)
795                 dev_dbg(&pf->pdev->dev,
796                         "Failed to update link status and re-enable link events for port %d\n",
797                         pi->lport);
798
799         /* if the old link up/down and speed is the same as the new */
800         if (link_up == old_link && link_speed == old_link_speed)
801                 return result;
802
803         vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
804         if (!vsi || !vsi->port_info)
805                 return -EINVAL;
806
807         ice_vsi_link_event(vsi, link_up);
808         ice_print_link_msg(vsi, link_up);
809
810         if (pf->num_alloc_vfs)
811                 ice_vc_notify_link_state(pf);
812
813         return result;
814 }
815
816 /**
817  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
818  * @pf: board private structure
819  */
820 static void ice_watchdog_subtask(struct ice_pf *pf)
821 {
822         int i;
823
824         /* if interface is down do nothing */
825         if (test_bit(__ICE_DOWN, pf->state) ||
826             test_bit(__ICE_CFG_BUSY, pf->state))
827                 return;
828
829         /* make sure we don't do these things too often */
830         if (time_before(jiffies,
831                         pf->serv_tmr_prev + pf->serv_tmr_period))
832                 return;
833
834         pf->serv_tmr_prev = jiffies;
835
836         /* Update the stats for active netdevs so the network stack
837          * can look at updated numbers whenever it cares to
838          */
839         ice_update_pf_stats(pf);
840         ice_for_each_vsi(pf, i)
841                 if (pf->vsi[i] && pf->vsi[i]->netdev)
842                         ice_update_vsi_stats(pf->vsi[i]);
843 }
844
845 /**
846  * ice_init_link_events - enable/initialize link events
847  * @pi: pointer to the port_info instance
848  *
849  * Returns -EIO on failure, 0 on success
850  */
851 static int ice_init_link_events(struct ice_port_info *pi)
852 {
853         u16 mask;
854
855         mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
856                        ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
857
858         if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
859                 dev_dbg(ice_hw_to_dev(pi->hw),
860                         "Failed to set link event mask for port %d\n",
861                         pi->lport);
862                 return -EIO;
863         }
864
865         if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
866                 dev_dbg(ice_hw_to_dev(pi->hw),
867                         "Failed to enable link events for port %d\n",
868                         pi->lport);
869                 return -EIO;
870         }
871
872         return 0;
873 }
874
875 /**
876  * ice_handle_link_event - handle link event via ARQ
877  * @pf: pf that the link event is associated with
878  * @event: event structure containing link status info
879  */
880 static int
881 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
882 {
883         struct ice_aqc_get_link_status_data *link_data;
884         struct ice_port_info *port_info;
885         int status;
886
887         link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
888         port_info = pf->hw.port_info;
889         if (!port_info)
890                 return -EINVAL;
891
892         status = ice_link_event(pf, port_info,
893                                 !!(link_data->link_info & ICE_AQ_LINK_UP),
894                                 le16_to_cpu(link_data->link_speed));
895         if (status)
896                 dev_dbg(&pf->pdev->dev,
897                         "Could not process link event, error %d\n", status);
898
899         return status;
900 }
901
902 /**
903  * __ice_clean_ctrlq - helper function to clean controlq rings
904  * @pf: ptr to struct ice_pf
905  * @q_type: specific Control queue type
906  */
907 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
908 {
909         struct ice_rq_event_info event;
910         struct ice_hw *hw = &pf->hw;
911         struct ice_ctl_q_info *cq;
912         u16 pending, i = 0;
913         const char *qtype;
914         u32 oldval, val;
915
916         /* Do not clean control queue if/when PF reset fails */
917         if (test_bit(__ICE_RESET_FAILED, pf->state))
918                 return 0;
919
920         switch (q_type) {
921         case ICE_CTL_Q_ADMIN:
922                 cq = &hw->adminq;
923                 qtype = "Admin";
924                 break;
925         case ICE_CTL_Q_MAILBOX:
926                 cq = &hw->mailboxq;
927                 qtype = "Mailbox";
928                 break;
929         default:
930                 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
931                          q_type);
932                 return 0;
933         }
934
935         /* check for error indications - PF_xx_AxQLEN register layout for
936          * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
937          */
938         val = rd32(hw, cq->rq.len);
939         if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
940                    PF_FW_ARQLEN_ARQCRIT_M)) {
941                 oldval = val;
942                 if (val & PF_FW_ARQLEN_ARQVFE_M)
943                         dev_dbg(&pf->pdev->dev,
944                                 "%s Receive Queue VF Error detected\n", qtype);
945                 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
946                         dev_dbg(&pf->pdev->dev,
947                                 "%s Receive Queue Overflow Error detected\n",
948                                 qtype);
949                 }
950                 if (val & PF_FW_ARQLEN_ARQCRIT_M)
951                         dev_dbg(&pf->pdev->dev,
952                                 "%s Receive Queue Critical Error detected\n",
953                                 qtype);
954                 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
955                          PF_FW_ARQLEN_ARQCRIT_M);
956                 if (oldval != val)
957                         wr32(hw, cq->rq.len, val);
958         }
959
960         val = rd32(hw, cq->sq.len);
961         if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
962                    PF_FW_ATQLEN_ATQCRIT_M)) {
963                 oldval = val;
964                 if (val & PF_FW_ATQLEN_ATQVFE_M)
965                         dev_dbg(&pf->pdev->dev,
966                                 "%s Send Queue VF Error detected\n", qtype);
967                 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
968                         dev_dbg(&pf->pdev->dev,
969                                 "%s Send Queue Overflow Error detected\n",
970                                 qtype);
971                 }
972                 if (val & PF_FW_ATQLEN_ATQCRIT_M)
973                         dev_dbg(&pf->pdev->dev,
974                                 "%s Send Queue Critical Error detected\n",
975                                 qtype);
976                 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
977                          PF_FW_ATQLEN_ATQCRIT_M);
978                 if (oldval != val)
979                         wr32(hw, cq->sq.len, val);
980         }
981
982         event.buf_len = cq->rq_buf_size;
983         event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
984                                      GFP_KERNEL);
985         if (!event.msg_buf)
986                 return 0;
987
988         do {
989                 enum ice_status ret;
990                 u16 opcode;
991
992                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
993                 if (ret == ICE_ERR_AQ_NO_WORK)
994                         break;
995                 if (ret) {
996                         dev_err(&pf->pdev->dev,
997                                 "%s Receive Queue event error %d\n", qtype,
998                                 ret);
999                         break;
1000                 }
1001
1002                 opcode = le16_to_cpu(event.desc.opcode);
1003
1004                 switch (opcode) {
1005                 case ice_aqc_opc_get_link_status:
1006                         if (ice_handle_link_event(pf, &event))
1007                                 dev_err(&pf->pdev->dev,
1008                                         "Could not handle link event\n");
1009                         break;
1010                 case ice_mbx_opc_send_msg_to_pf:
1011                         ice_vc_process_vf_msg(pf, &event);
1012                         break;
1013                 case ice_aqc_opc_fw_logging:
1014                         ice_output_fw_log(hw, &event.desc, event.msg_buf);
1015                         break;
1016                 case ice_aqc_opc_lldp_set_mib_change:
1017                         ice_dcb_process_lldp_set_mib_change(pf, &event);
1018                         break;
1019                 default:
1020                         dev_dbg(&pf->pdev->dev,
1021                                 "%s Receive Queue unknown event 0x%04x ignored\n",
1022                                 qtype, opcode);
1023                         break;
1024                 }
1025         } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1026
1027         devm_kfree(&pf->pdev->dev, event.msg_buf);
1028
1029         return pending && (i == ICE_DFLT_IRQ_WORK);
1030 }
1031
1032 /**
1033  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1034  * @hw: pointer to hardware info
1035  * @cq: control queue information
1036  *
1037  * returns true if there are pending messages in a queue, false if there aren't
1038  */
1039 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1040 {
1041         u16 ntu;
1042
1043         ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1044         return cq->rq.next_to_clean != ntu;
1045 }
1046
1047 /**
1048  * ice_clean_adminq_subtask - clean the AdminQ rings
1049  * @pf: board private structure
1050  */
1051 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1052 {
1053         struct ice_hw *hw = &pf->hw;
1054
1055         if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1056                 return;
1057
1058         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1059                 return;
1060
1061         clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1062
1063         /* There might be a situation where new messages arrive to a control
1064          * queue between processing the last message and clearing the
1065          * EVENT_PENDING bit. So before exiting, check queue head again (using
1066          * ice_ctrlq_pending) and process new messages if any.
1067          */
1068         if (ice_ctrlq_pending(hw, &hw->adminq))
1069                 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1070
1071         ice_flush(hw);
1072 }
1073
1074 /**
1075  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1076  * @pf: board private structure
1077  */
1078 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1079 {
1080         struct ice_hw *hw = &pf->hw;
1081
1082         if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1083                 return;
1084
1085         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1086                 return;
1087
1088         clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1089
1090         if (ice_ctrlq_pending(hw, &hw->mailboxq))
1091                 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1092
1093         ice_flush(hw);
1094 }
1095
1096 /**
1097  * ice_service_task_schedule - schedule the service task to wake up
1098  * @pf: board private structure
1099  *
1100  * If not already scheduled, this puts the task into the work queue.
1101  */
1102 static void ice_service_task_schedule(struct ice_pf *pf)
1103 {
1104         if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1105             !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1106             !test_bit(__ICE_NEEDS_RESTART, pf->state))
1107                 queue_work(ice_wq, &pf->serv_task);
1108 }
1109
1110 /**
1111  * ice_service_task_complete - finish up the service task
1112  * @pf: board private structure
1113  */
1114 static void ice_service_task_complete(struct ice_pf *pf)
1115 {
1116         WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1117
1118         /* force memory (pf->state) to sync before next service task */
1119         smp_mb__before_atomic();
1120         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1121 }
1122
1123 /**
1124  * ice_service_task_stop - stop service task and cancel works
1125  * @pf: board private structure
1126  */
1127 static void ice_service_task_stop(struct ice_pf *pf)
1128 {
1129         set_bit(__ICE_SERVICE_DIS, pf->state);
1130
1131         if (pf->serv_tmr.function)
1132                 del_timer_sync(&pf->serv_tmr);
1133         if (pf->serv_task.func)
1134                 cancel_work_sync(&pf->serv_task);
1135
1136         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1137 }
1138
1139 /**
1140  * ice_service_task_restart - restart service task and schedule works
1141  * @pf: board private structure
1142  *
1143  * This function is needed for suspend and resume works (e.g WoL scenario)
1144  */
1145 static void ice_service_task_restart(struct ice_pf *pf)
1146 {
1147         clear_bit(__ICE_SERVICE_DIS, pf->state);
1148         ice_service_task_schedule(pf);
1149 }
1150
1151 /**
1152  * ice_service_timer - timer callback to schedule service task
1153  * @t: pointer to timer_list
1154  */
1155 static void ice_service_timer(struct timer_list *t)
1156 {
1157         struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1158
1159         mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1160         ice_service_task_schedule(pf);
1161 }
1162
1163 /**
1164  * ice_handle_mdd_event - handle malicious driver detect event
1165  * @pf: pointer to the PF structure
1166  *
1167  * Called from service task. OICR interrupt handler indicates MDD event
1168  */
1169 static void ice_handle_mdd_event(struct ice_pf *pf)
1170 {
1171         struct ice_hw *hw = &pf->hw;
1172         bool mdd_detected = false;
1173         u32 reg;
1174         int i;
1175
1176         if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1177                 return;
1178
1179         /* find what triggered the MDD event */
1180         reg = rd32(hw, GL_MDET_TX_PQM);
1181         if (reg & GL_MDET_TX_PQM_VALID_M) {
1182                 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1183                                 GL_MDET_TX_PQM_PF_NUM_S;
1184                 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1185                                 GL_MDET_TX_PQM_VF_NUM_S;
1186                 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1187                                 GL_MDET_TX_PQM_MAL_TYPE_S;
1188                 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1189                                 GL_MDET_TX_PQM_QNUM_S);
1190
1191                 if (netif_msg_tx_err(pf))
1192                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1193                                  event, queue, pf_num, vf_num);
1194                 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1195                 mdd_detected = true;
1196         }
1197
1198         reg = rd32(hw, GL_MDET_TX_TCLAN);
1199         if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1200                 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1201                                 GL_MDET_TX_TCLAN_PF_NUM_S;
1202                 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1203                                 GL_MDET_TX_TCLAN_VF_NUM_S;
1204                 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1205                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1206                 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1207                                 GL_MDET_TX_TCLAN_QNUM_S);
1208
1209                 if (netif_msg_rx_err(pf))
1210                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1211                                  event, queue, pf_num, vf_num);
1212                 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1213                 mdd_detected = true;
1214         }
1215
1216         reg = rd32(hw, GL_MDET_RX);
1217         if (reg & GL_MDET_RX_VALID_M) {
1218                 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1219                                 GL_MDET_RX_PF_NUM_S;
1220                 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1221                                 GL_MDET_RX_VF_NUM_S;
1222                 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1223                                 GL_MDET_RX_MAL_TYPE_S;
1224                 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1225                                 GL_MDET_RX_QNUM_S);
1226
1227                 if (netif_msg_rx_err(pf))
1228                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1229                                  event, queue, pf_num, vf_num);
1230                 wr32(hw, GL_MDET_RX, 0xffffffff);
1231                 mdd_detected = true;
1232         }
1233
1234         if (mdd_detected) {
1235                 bool pf_mdd_detected = false;
1236
1237                 reg = rd32(hw, PF_MDET_TX_PQM);
1238                 if (reg & PF_MDET_TX_PQM_VALID_M) {
1239                         wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1240                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1241                         pf_mdd_detected = true;
1242                 }
1243
1244                 reg = rd32(hw, PF_MDET_TX_TCLAN);
1245                 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1246                         wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1247                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1248                         pf_mdd_detected = true;
1249                 }
1250
1251                 reg = rd32(hw, PF_MDET_RX);
1252                 if (reg & PF_MDET_RX_VALID_M) {
1253                         wr32(hw, PF_MDET_RX, 0xFFFF);
1254                         dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1255                         pf_mdd_detected = true;
1256                 }
1257                 /* Queue belongs to the PF initiate a reset */
1258                 if (pf_mdd_detected) {
1259                         set_bit(__ICE_NEEDS_RESTART, pf->state);
1260                         ice_service_task_schedule(pf);
1261                 }
1262         }
1263
1264         /* check to see if one of the VFs caused the MDD */
1265         for (i = 0; i < pf->num_alloc_vfs; i++) {
1266                 struct ice_vf *vf = &pf->vf[i];
1267
1268                 bool vf_mdd_detected = false;
1269
1270                 reg = rd32(hw, VP_MDET_TX_PQM(i));
1271                 if (reg & VP_MDET_TX_PQM_VALID_M) {
1272                         wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1273                         vf_mdd_detected = true;
1274                         dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1275                                  i);
1276                 }
1277
1278                 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1279                 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1280                         wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1281                         vf_mdd_detected = true;
1282                         dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1283                                  i);
1284                 }
1285
1286                 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1287                 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1288                         wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1289                         vf_mdd_detected = true;
1290                         dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1291                                  i);
1292                 }
1293
1294                 reg = rd32(hw, VP_MDET_RX(i));
1295                 if (reg & VP_MDET_RX_VALID_M) {
1296                         wr32(hw, VP_MDET_RX(i), 0xFFFF);
1297                         vf_mdd_detected = true;
1298                         dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
1299                                  i);
1300                 }
1301
1302                 if (vf_mdd_detected) {
1303                         vf->num_mdd_events++;
1304                         if (vf->num_mdd_events > 1)
1305                                 dev_info(&pf->pdev->dev, "VF %d has had %llu MDD events since last boot\n",
1306                                          i, vf->num_mdd_events);
1307                 }
1308         }
1309 }
1310
1311 /**
1312  * ice_service_task - manage and run subtasks
1313  * @work: pointer to work_struct contained by the PF struct
1314  */
1315 static void ice_service_task(struct work_struct *work)
1316 {
1317         struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1318         unsigned long start_time = jiffies;
1319
1320         /* subtasks */
1321
1322         /* process reset requests first */
1323         ice_reset_subtask(pf);
1324
1325         /* bail if a reset/recovery cycle is pending or rebuild failed */
1326         if (ice_is_reset_in_progress(pf->state) ||
1327             test_bit(__ICE_SUSPENDED, pf->state) ||
1328             test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1329                 ice_service_task_complete(pf);
1330                 return;
1331         }
1332
1333         ice_check_for_hang_subtask(pf);
1334         ice_sync_fltr_subtask(pf);
1335         ice_handle_mdd_event(pf);
1336         ice_process_vflr_event(pf);
1337         ice_watchdog_subtask(pf);
1338         ice_clean_adminq_subtask(pf);
1339         ice_clean_mailboxq_subtask(pf);
1340
1341         /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1342         ice_service_task_complete(pf);
1343
1344         /* If the tasks have taken longer than one service timer period
1345          * or there is more work to be done, reset the service timer to
1346          * schedule the service task now.
1347          */
1348         if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1349             test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1350             test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1351             test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1352             test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1353                 mod_timer(&pf->serv_tmr, jiffies);
1354 }
1355
1356 /**
1357  * ice_set_ctrlq_len - helper function to set controlq length
1358  * @hw: pointer to the HW instance
1359  */
1360 static void ice_set_ctrlq_len(struct ice_hw *hw)
1361 {
1362         hw->adminq.num_rq_entries = ICE_AQ_LEN;
1363         hw->adminq.num_sq_entries = ICE_AQ_LEN;
1364         hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1365         hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1366         hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
1367         hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
1368         hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1369         hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1370 }
1371
1372 /**
1373  * ice_irq_affinity_notify - Callback for affinity changes
1374  * @notify: context as to what irq was changed
1375  * @mask: the new affinity mask
1376  *
1377  * This is a callback function used by the irq_set_affinity_notifier function
1378  * so that we may register to receive changes to the irq affinity masks.
1379  */
1380 static void
1381 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1382                         const cpumask_t *mask)
1383 {
1384         struct ice_q_vector *q_vector =
1385                 container_of(notify, struct ice_q_vector, affinity_notify);
1386
1387         cpumask_copy(&q_vector->affinity_mask, mask);
1388 }
1389
1390 /**
1391  * ice_irq_affinity_release - Callback for affinity notifier release
1392  * @ref: internal core kernel usage
1393  *
1394  * This is a callback function used by the irq_set_affinity_notifier function
1395  * to inform the current notification subscriber that they will no longer
1396  * receive notifications.
1397  */
1398 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1399
1400 /**
1401  * ice_vsi_ena_irq - Enable IRQ for the given VSI
1402  * @vsi: the VSI being configured
1403  */
1404 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1405 {
1406         struct ice_pf *pf = vsi->back;
1407         struct ice_hw *hw = &pf->hw;
1408
1409         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1410                 int i;
1411
1412                 ice_for_each_q_vector(vsi, i)
1413                         ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1414         }
1415
1416         ice_flush(hw);
1417         return 0;
1418 }
1419
1420 /**
1421  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1422  * @vsi: the VSI being configured
1423  * @basename: name for the vector
1424  */
1425 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1426 {
1427         int q_vectors = vsi->num_q_vectors;
1428         struct ice_pf *pf = vsi->back;
1429         int base = vsi->base_vector;
1430         int rx_int_idx = 0;
1431         int tx_int_idx = 0;
1432         int vector, err;
1433         int irq_num;
1434
1435         for (vector = 0; vector < q_vectors; vector++) {
1436                 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1437
1438                 irq_num = pf->msix_entries[base + vector].vector;
1439
1440                 if (q_vector->tx.ring && q_vector->rx.ring) {
1441                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1442                                  "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1443                         tx_int_idx++;
1444                 } else if (q_vector->rx.ring) {
1445                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1446                                  "%s-%s-%d", basename, "rx", rx_int_idx++);
1447                 } else if (q_vector->tx.ring) {
1448                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1449                                  "%s-%s-%d", basename, "tx", tx_int_idx++);
1450                 } else {
1451                         /* skip this unused q_vector */
1452                         continue;
1453                 }
1454                 err = devm_request_irq(&pf->pdev->dev, irq_num,
1455                                        vsi->irq_handler, 0,
1456                                        q_vector->name, q_vector);
1457                 if (err) {
1458                         netdev_err(vsi->netdev,
1459                                    "MSIX request_irq failed, error: %d\n", err);
1460                         goto free_q_irqs;
1461                 }
1462
1463                 /* register for affinity change notifications */
1464                 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1465                 q_vector->affinity_notify.release = ice_irq_affinity_release;
1466                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1467
1468                 /* assign the mask for this irq */
1469                 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1470         }
1471
1472         vsi->irqs_ready = true;
1473         return 0;
1474
1475 free_q_irqs:
1476         while (vector) {
1477                 vector--;
1478                 irq_num = pf->msix_entries[base + vector].vector,
1479                 irq_set_affinity_notifier(irq_num, NULL);
1480                 irq_set_affinity_hint(irq_num, NULL);
1481                 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1482         }
1483         return err;
1484 }
1485
1486 /**
1487  * ice_ena_misc_vector - enable the non-queue interrupts
1488  * @pf: board private structure
1489  */
1490 static void ice_ena_misc_vector(struct ice_pf *pf)
1491 {
1492         struct ice_hw *hw = &pf->hw;
1493         u32 val;
1494
1495         /* clear things first */
1496         wr32(hw, PFINT_OICR_ENA, 0);    /* disable all */
1497         rd32(hw, PFINT_OICR);           /* read to clear */
1498
1499         val = (PFINT_OICR_ECC_ERR_M |
1500                PFINT_OICR_MAL_DETECT_M |
1501                PFINT_OICR_GRST_M |
1502                PFINT_OICR_PCI_EXCEPTION_M |
1503                PFINT_OICR_VFLR_M |
1504                PFINT_OICR_HMC_ERR_M |
1505                PFINT_OICR_PE_CRITERR_M);
1506
1507         wr32(hw, PFINT_OICR_ENA, val);
1508
1509         /* SW_ITR_IDX = 0, but don't change INTENA */
1510         wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1511              GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1512 }
1513
1514 /**
1515  * ice_misc_intr - misc interrupt handler
1516  * @irq: interrupt number
1517  * @data: pointer to a q_vector
1518  */
1519 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1520 {
1521         struct ice_pf *pf = (struct ice_pf *)data;
1522         struct ice_hw *hw = &pf->hw;
1523         irqreturn_t ret = IRQ_NONE;
1524         u32 oicr, ena_mask;
1525
1526         set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1527         set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1528
1529         oicr = rd32(hw, PFINT_OICR);
1530         ena_mask = rd32(hw, PFINT_OICR_ENA);
1531
1532         if (oicr & PFINT_OICR_SWINT_M) {
1533                 ena_mask &= ~PFINT_OICR_SWINT_M;
1534                 pf->sw_int_count++;
1535         }
1536
1537         if (oicr & PFINT_OICR_MAL_DETECT_M) {
1538                 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
1539                 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1540         }
1541         if (oicr & PFINT_OICR_VFLR_M) {
1542                 ena_mask &= ~PFINT_OICR_VFLR_M;
1543                 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
1544         }
1545
1546         if (oicr & PFINT_OICR_GRST_M) {
1547                 u32 reset;
1548
1549                 /* we have a reset warning */
1550                 ena_mask &= ~PFINT_OICR_GRST_M;
1551                 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1552                         GLGEN_RSTAT_RESET_TYPE_S;
1553
1554                 if (reset == ICE_RESET_CORER)
1555                         pf->corer_count++;
1556                 else if (reset == ICE_RESET_GLOBR)
1557                         pf->globr_count++;
1558                 else if (reset == ICE_RESET_EMPR)
1559                         pf->empr_count++;
1560                 else
1561                         dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
1562                                 reset);
1563
1564                 /* If a reset cycle isn't already in progress, we set a bit in
1565                  * pf->state so that the service task can start a reset/rebuild.
1566                  * We also make note of which reset happened so that peer
1567                  * devices/drivers can be informed.
1568                  */
1569                 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
1570                         if (reset == ICE_RESET_CORER)
1571                                 set_bit(__ICE_CORER_RECV, pf->state);
1572                         else if (reset == ICE_RESET_GLOBR)
1573                                 set_bit(__ICE_GLOBR_RECV, pf->state);
1574                         else
1575                                 set_bit(__ICE_EMPR_RECV, pf->state);
1576
1577                         /* There are couple of different bits at play here.
1578                          * hw->reset_ongoing indicates whether the hardware is
1579                          * in reset. This is set to true when a reset interrupt
1580                          * is received and set back to false after the driver
1581                          * has determined that the hardware is out of reset.
1582                          *
1583                          * __ICE_RESET_OICR_RECV in pf->state indicates
1584                          * that a post reset rebuild is required before the
1585                          * driver is operational again. This is set above.
1586                          *
1587                          * As this is the start of the reset/rebuild cycle, set
1588                          * both to indicate that.
1589                          */
1590                         hw->reset_ongoing = true;
1591                 }
1592         }
1593
1594         if (oicr & PFINT_OICR_HMC_ERR_M) {
1595                 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1596                 dev_dbg(&pf->pdev->dev,
1597                         "HMC Error interrupt - info 0x%x, data 0x%x\n",
1598                         rd32(hw, PFHMC_ERRORINFO),
1599                         rd32(hw, PFHMC_ERRORDATA));
1600         }
1601
1602         /* Report any remaining unexpected interrupts */
1603         oicr &= ena_mask;
1604         if (oicr) {
1605                 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1606                         oicr);
1607                 /* If a critical error is pending there is no choice but to
1608                  * reset the device.
1609                  */
1610                 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1611                             PFINT_OICR_PCI_EXCEPTION_M |
1612                             PFINT_OICR_ECC_ERR_M)) {
1613                         set_bit(__ICE_PFR_REQ, pf->state);
1614                         ice_service_task_schedule(pf);
1615                 }
1616         }
1617         ret = IRQ_HANDLED;
1618
1619         if (!test_bit(__ICE_DOWN, pf->state)) {
1620                 ice_service_task_schedule(pf);
1621                 ice_irq_dynamic_ena(hw, NULL, NULL);
1622         }
1623
1624         return ret;
1625 }
1626
1627 /**
1628  * ice_dis_ctrlq_interrupts - disable control queue interrupts
1629  * @hw: pointer to HW structure
1630  */
1631 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
1632 {
1633         /* disable Admin queue Interrupt causes */
1634         wr32(hw, PFINT_FW_CTL,
1635              rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
1636
1637         /* disable Mailbox queue Interrupt causes */
1638         wr32(hw, PFINT_MBX_CTL,
1639              rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
1640
1641         /* disable Control queue Interrupt causes */
1642         wr32(hw, PFINT_OICR_CTL,
1643              rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
1644
1645         ice_flush(hw);
1646 }
1647
1648 /**
1649  * ice_free_irq_msix_misc - Unroll misc vector setup
1650  * @pf: board private structure
1651  */
1652 static void ice_free_irq_msix_misc(struct ice_pf *pf)
1653 {
1654         struct ice_hw *hw = &pf->hw;
1655
1656         ice_dis_ctrlq_interrupts(hw);
1657
1658         /* disable OICR interrupt */
1659         wr32(hw, PFINT_OICR_ENA, 0);
1660         ice_flush(hw);
1661
1662         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
1663                 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
1664                 devm_free_irq(&pf->pdev->dev,
1665                               pf->msix_entries[pf->oicr_idx].vector, pf);
1666         }
1667
1668         pf->num_avail_sw_msix += 1;
1669         ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
1670 }
1671
1672 /**
1673  * ice_ena_ctrlq_interrupts - enable control queue interrupts
1674  * @hw: pointer to HW structure
1675  * @reg_idx: HW vector index to associate the control queue interrupts with
1676  */
1677 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
1678 {
1679         u32 val;
1680
1681         val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
1682                PFINT_OICR_CTL_CAUSE_ENA_M);
1683         wr32(hw, PFINT_OICR_CTL, val);
1684
1685         /* enable Admin queue Interrupt causes */
1686         val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
1687                PFINT_FW_CTL_CAUSE_ENA_M);
1688         wr32(hw, PFINT_FW_CTL, val);
1689
1690         /* enable Mailbox queue Interrupt causes */
1691         val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
1692                PFINT_MBX_CTL_CAUSE_ENA_M);
1693         wr32(hw, PFINT_MBX_CTL, val);
1694
1695         ice_flush(hw);
1696 }
1697
1698 /**
1699  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
1700  * @pf: board private structure
1701  *
1702  * This sets up the handler for MSIX 0, which is used to manage the
1703  * non-queue interrupts, e.g. AdminQ and errors. This is not used
1704  * when in MSI or Legacy interrupt mode.
1705  */
1706 static int ice_req_irq_msix_misc(struct ice_pf *pf)
1707 {
1708         struct ice_hw *hw = &pf->hw;
1709         int oicr_idx, err = 0;
1710
1711         if (!pf->int_name[0])
1712                 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
1713                          dev_driver_string(&pf->pdev->dev),
1714                          dev_name(&pf->pdev->dev));
1715
1716         /* Do not request IRQ but do enable OICR interrupt since settings are
1717          * lost during reset. Note that this function is called only during
1718          * rebuild path and not while reset is in progress.
1719          */
1720         if (ice_is_reset_in_progress(pf->state))
1721                 goto skip_req_irq;
1722
1723         /* reserve one vector in irq_tracker for misc interrupts */
1724         oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1725         if (oicr_idx < 0)
1726                 return oicr_idx;
1727
1728         pf->num_avail_sw_msix -= 1;
1729         pf->oicr_idx = oicr_idx;
1730
1731         err = devm_request_irq(&pf->pdev->dev,
1732                                pf->msix_entries[pf->oicr_idx].vector,
1733                                ice_misc_intr, 0, pf->int_name, pf);
1734         if (err) {
1735                 dev_err(&pf->pdev->dev,
1736                         "devm_request_irq for %s failed: %d\n",
1737                         pf->int_name, err);
1738                 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1739                 pf->num_avail_sw_msix += 1;
1740                 return err;
1741         }
1742
1743 skip_req_irq:
1744         ice_ena_misc_vector(pf);
1745
1746         ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
1747         wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
1748              ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
1749
1750         ice_flush(hw);
1751         ice_irq_dynamic_ena(hw, NULL, NULL);
1752
1753         return 0;
1754 }
1755
1756 /**
1757  * ice_napi_add - register NAPI handler for the VSI
1758  * @vsi: VSI for which NAPI handler is to be registered
1759  *
1760  * This function is only called in the driver's load path. Registering the NAPI
1761  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
1762  * reset/rebuild, etc.)
1763  */
1764 static void ice_napi_add(struct ice_vsi *vsi)
1765 {
1766         int v_idx;
1767
1768         if (!vsi->netdev)
1769                 return;
1770
1771         ice_for_each_q_vector(vsi, v_idx)
1772                 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
1773                                ice_napi_poll, NAPI_POLL_WEIGHT);
1774 }
1775
1776 /**
1777  * ice_cfg_netdev - Allocate, configure and register a netdev
1778  * @vsi: the VSI associated with the new netdev
1779  *
1780  * Returns 0 on success, negative value on failure
1781  */
1782 static int ice_cfg_netdev(struct ice_vsi *vsi)
1783 {
1784         netdev_features_t csumo_features;
1785         netdev_features_t vlano_features;
1786         netdev_features_t dflt_features;
1787         netdev_features_t tso_features;
1788         struct ice_netdev_priv *np;
1789         struct net_device *netdev;
1790         u8 mac_addr[ETH_ALEN];
1791         int err;
1792
1793         netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
1794                                     vsi->alloc_rxq);
1795         if (!netdev)
1796                 return -ENOMEM;
1797
1798         vsi->netdev = netdev;
1799         np = netdev_priv(netdev);
1800         np->vsi = vsi;
1801
1802         dflt_features = NETIF_F_SG      |
1803                         NETIF_F_HIGHDMA |
1804                         NETIF_F_RXHASH;
1805
1806         csumo_features = NETIF_F_RXCSUM   |
1807                          NETIF_F_IP_CSUM  |
1808                          NETIF_F_SCTP_CRC |
1809                          NETIF_F_IPV6_CSUM;
1810
1811         vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
1812                          NETIF_F_HW_VLAN_CTAG_TX     |
1813                          NETIF_F_HW_VLAN_CTAG_RX;
1814
1815         tso_features = NETIF_F_TSO;
1816
1817         /* set features that user can change */
1818         netdev->hw_features = dflt_features | csumo_features |
1819                               vlano_features | tso_features;
1820
1821         /* enable features */
1822         netdev->features |= netdev->hw_features;
1823         /* encap and VLAN devices inherit default, csumo and tso features */
1824         netdev->hw_enc_features |= dflt_features | csumo_features |
1825                                    tso_features;
1826         netdev->vlan_features |= dflt_features | csumo_features |
1827                                  tso_features;
1828
1829         if (vsi->type == ICE_VSI_PF) {
1830                 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
1831                 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
1832
1833                 ether_addr_copy(netdev->dev_addr, mac_addr);
1834                 ether_addr_copy(netdev->perm_addr, mac_addr);
1835         }
1836
1837         netdev->priv_flags |= IFF_UNICAST_FLT;
1838
1839         /* assign netdev_ops */
1840         netdev->netdev_ops = &ice_netdev_ops;
1841
1842         /* setup watchdog timeout value to be 5 second */
1843         netdev->watchdog_timeo = 5 * HZ;
1844
1845         ice_set_ethtool_ops(netdev);
1846
1847         netdev->min_mtu = ETH_MIN_MTU;
1848         netdev->max_mtu = ICE_MAX_MTU;
1849
1850         err = register_netdev(vsi->netdev);
1851         if (err)
1852                 return err;
1853
1854         netif_carrier_off(vsi->netdev);
1855
1856         /* make sure transmit queues start off as stopped */
1857         netif_tx_stop_all_queues(vsi->netdev);
1858
1859         return 0;
1860 }
1861
1862 /**
1863  * ice_fill_rss_lut - Fill the RSS lookup table with default values
1864  * @lut: Lookup table
1865  * @rss_table_size: Lookup table size
1866  * @rss_size: Range of queue number for hashing
1867  */
1868 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
1869 {
1870         u16 i;
1871
1872         for (i = 0; i < rss_table_size; i++)
1873                 lut[i] = i % rss_size;
1874 }
1875
1876 /**
1877  * ice_pf_vsi_setup - Set up a PF VSI
1878  * @pf: board private structure
1879  * @pi: pointer to the port_info instance
1880  *
1881  * Returns pointer to the successfully allocated VSI software struct
1882  * on success, otherwise returns NULL on failure.
1883  */
1884 static struct ice_vsi *
1885 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
1886 {
1887         return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
1888 }
1889
1890 /**
1891  * ice_lb_vsi_setup - Set up a loopback VSI
1892  * @pf: board private structure
1893  * @pi: pointer to the port_info instance
1894  *
1895  * Returns pointer to the successfully allocated VSI software struct
1896  * on success, otherwise returns NULL on failure.
1897  */
1898 struct ice_vsi *
1899 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
1900 {
1901         return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
1902 }
1903
1904 /**
1905  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
1906  * @netdev: network interface to be adjusted
1907  * @proto: unused protocol
1908  * @vid: VLAN ID to be added
1909  *
1910  * net_device_ops implementation for adding VLAN IDs
1911  */
1912 static int
1913 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
1914                     u16 vid)
1915 {
1916         struct ice_netdev_priv *np = netdev_priv(netdev);
1917         struct ice_vsi *vsi = np->vsi;
1918         int ret;
1919
1920         if (vid >= VLAN_N_VID) {
1921                 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
1922                            vid, VLAN_N_VID);
1923                 return -EINVAL;
1924         }
1925
1926         if (vsi->info.pvid)
1927                 return -EINVAL;
1928
1929         /* Enable VLAN pruning when VLAN 0 is added */
1930         if (unlikely(!vid)) {
1931                 ret = ice_cfg_vlan_pruning(vsi, true, false);
1932                 if (ret)
1933                         return ret;
1934         }
1935
1936         /* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is
1937          * needed to continue allowing all untagged packets since VLAN prune
1938          * list is applied to all packets by the switch
1939          */
1940         ret = ice_vsi_add_vlan(vsi, vid);
1941         if (!ret) {
1942                 vsi->vlan_ena = true;
1943                 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
1944         }
1945
1946         return ret;
1947 }
1948
1949 /**
1950  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
1951  * @netdev: network interface to be adjusted
1952  * @proto: unused protocol
1953  * @vid: VLAN ID to be removed
1954  *
1955  * net_device_ops implementation for removing VLAN IDs
1956  */
1957 static int
1958 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
1959                      u16 vid)
1960 {
1961         struct ice_netdev_priv *np = netdev_priv(netdev);
1962         struct ice_vsi *vsi = np->vsi;
1963         int ret;
1964
1965         if (vsi->info.pvid)
1966                 return -EINVAL;
1967
1968         /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
1969          * information
1970          */
1971         ret = ice_vsi_kill_vlan(vsi, vid);
1972         if (ret)
1973                 return ret;
1974
1975         /* Disable VLAN pruning when VLAN 0 is removed */
1976         if (unlikely(!vid))
1977                 ret = ice_cfg_vlan_pruning(vsi, false, false);
1978
1979         vsi->vlan_ena = false;
1980         set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
1981         return ret;
1982 }
1983
1984 /**
1985  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
1986  * @pf: board private structure
1987  *
1988  * Returns 0 on success, negative value on failure
1989  */
1990 static int ice_setup_pf_sw(struct ice_pf *pf)
1991 {
1992         struct ice_vsi *vsi;
1993         int status = 0;
1994
1995         if (ice_is_reset_in_progress(pf->state))
1996                 return -EBUSY;
1997
1998         vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
1999         if (!vsi) {
2000                 status = -ENOMEM;
2001                 goto unroll_vsi_setup;
2002         }
2003
2004         status = ice_cfg_netdev(vsi);
2005         if (status) {
2006                 status = -ENODEV;
2007                 goto unroll_vsi_setup;
2008         }
2009
2010         /* registering the NAPI handler requires both the queues and
2011          * netdev to be created, which are done in ice_pf_vsi_setup()
2012          * and ice_cfg_netdev() respectively
2013          */
2014         ice_napi_add(vsi);
2015
2016         status = ice_init_mac_fltr(pf);
2017         if (status)
2018                 goto unroll_napi_add;
2019
2020         return status;
2021
2022 unroll_napi_add:
2023         if (vsi) {
2024                 ice_napi_del(vsi);
2025                 if (vsi->netdev) {
2026                         if (vsi->netdev->reg_state == NETREG_REGISTERED)
2027                                 unregister_netdev(vsi->netdev);
2028                         free_netdev(vsi->netdev);
2029                         vsi->netdev = NULL;
2030                 }
2031         }
2032
2033 unroll_vsi_setup:
2034         if (vsi) {
2035                 ice_vsi_free_q_vectors(vsi);
2036                 ice_vsi_delete(vsi);
2037                 ice_vsi_put_qs(vsi);
2038                 pf->q_left_tx += vsi->alloc_txq;
2039                 pf->q_left_rx += vsi->alloc_rxq;
2040                 ice_vsi_clear(vsi);
2041         }
2042         return status;
2043 }
2044
2045 /**
2046  * ice_determine_q_usage - Calculate queue distribution
2047  * @pf: board private structure
2048  *
2049  * Return -ENOMEM if we don't get enough queues for all ports
2050  */
2051 static void ice_determine_q_usage(struct ice_pf *pf)
2052 {
2053         u16 q_left_tx, q_left_rx;
2054
2055         q_left_tx = pf->hw.func_caps.common_cap.num_txq;
2056         q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
2057
2058         pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
2059
2060         /* only 1 Rx queue unless RSS is enabled */
2061         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2062                 pf->num_lan_rx = 1;
2063         else
2064                 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
2065
2066         pf->q_left_tx = q_left_tx - pf->num_lan_tx;
2067         pf->q_left_rx = q_left_rx - pf->num_lan_rx;
2068 }
2069
2070 /**
2071  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
2072  * @pf: board private structure to initialize
2073  */
2074 static void ice_deinit_pf(struct ice_pf *pf)
2075 {
2076         ice_service_task_stop(pf);
2077         mutex_destroy(&pf->sw_mutex);
2078         mutex_destroy(&pf->avail_q_mutex);
2079 }
2080
2081 /**
2082  * ice_init_pf - Initialize general software structures (struct ice_pf)
2083  * @pf: board private structure to initialize
2084  */
2085 static void ice_init_pf(struct ice_pf *pf)
2086 {
2087         bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
2088         set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2089 #ifdef CONFIG_PCI_IOV
2090         if (pf->hw.func_caps.common_cap.sr_iov_1_1) {
2091                 struct ice_hw *hw = &pf->hw;
2092
2093                 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
2094                 pf->num_vfs_supported = min_t(int, hw->func_caps.num_allocd_vfs,
2095                                               ICE_MAX_VF_COUNT);
2096         }
2097 #endif /* CONFIG_PCI_IOV */
2098
2099         mutex_init(&pf->sw_mutex);
2100         mutex_init(&pf->avail_q_mutex);
2101
2102         /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
2103         mutex_lock(&pf->avail_q_mutex);
2104         bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
2105         bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
2106         mutex_unlock(&pf->avail_q_mutex);
2107
2108         if (pf->hw.func_caps.common_cap.rss_table_size)
2109                 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
2110
2111         /* setup service timer and periodic service task */
2112         timer_setup(&pf->serv_tmr, ice_service_timer, 0);
2113         pf->serv_tmr_period = HZ;
2114         INIT_WORK(&pf->serv_task, ice_service_task);
2115         clear_bit(__ICE_SERVICE_SCHED, pf->state);
2116 }
2117
2118 /**
2119  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
2120  * @pf: board private structure
2121  *
2122  * compute the number of MSIX vectors required (v_budget) and request from
2123  * the OS. Return the number of vectors reserved or negative on failure
2124  */
2125 static int ice_ena_msix_range(struct ice_pf *pf)
2126 {
2127         int v_left, v_actual, v_budget = 0;
2128         int needed, err, i;
2129
2130         v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
2131
2132         /* reserve one vector for miscellaneous handler */
2133         needed = 1;
2134         v_budget += needed;
2135         v_left -= needed;
2136
2137         /* reserve vectors for LAN traffic */
2138         pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
2139         v_budget += pf->num_lan_msix;
2140         v_left -= pf->num_lan_msix;
2141
2142         pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
2143                                         sizeof(*pf->msix_entries), GFP_KERNEL);
2144
2145         if (!pf->msix_entries) {
2146                 err = -ENOMEM;
2147                 goto exit_err;
2148         }
2149
2150         for (i = 0; i < v_budget; i++)
2151                 pf->msix_entries[i].entry = i;
2152
2153         /* actually reserve the vectors */
2154         v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
2155                                          ICE_MIN_MSIX, v_budget);
2156
2157         if (v_actual < 0) {
2158                 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
2159                 err = v_actual;
2160                 goto msix_err;
2161         }
2162
2163         if (v_actual < v_budget) {
2164                 dev_warn(&pf->pdev->dev,
2165                          "not enough vectors. requested = %d, obtained = %d\n",
2166                          v_budget, v_actual);
2167                 if (v_actual >= (pf->num_lan_msix + 1)) {
2168                         pf->num_avail_sw_msix = v_actual -
2169                                                 (pf->num_lan_msix + 1);
2170                 } else if (v_actual >= 2) {
2171                         pf->num_lan_msix = 1;
2172                         pf->num_avail_sw_msix = v_actual - 2;
2173                 } else {
2174                         pci_disable_msix(pf->pdev);
2175                         err = -ERANGE;
2176                         goto msix_err;
2177                 }
2178         }
2179
2180         return v_actual;
2181
2182 msix_err:
2183         devm_kfree(&pf->pdev->dev, pf->msix_entries);
2184         goto exit_err;
2185
2186 exit_err:
2187         pf->num_lan_msix = 0;
2188         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2189         return err;
2190 }
2191
2192 /**
2193  * ice_dis_msix - Disable MSI-X interrupt setup in OS
2194  * @pf: board private structure
2195  */
2196 static void ice_dis_msix(struct ice_pf *pf)
2197 {
2198         pci_disable_msix(pf->pdev);
2199         devm_kfree(&pf->pdev->dev, pf->msix_entries);
2200         pf->msix_entries = NULL;
2201         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2202 }
2203
2204 /**
2205  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
2206  * @pf: board private structure
2207  */
2208 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
2209 {
2210         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2211                 ice_dis_msix(pf);
2212
2213         if (pf->irq_tracker) {
2214                 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
2215                 pf->irq_tracker = NULL;
2216         }
2217 }
2218
2219 /**
2220  * ice_init_interrupt_scheme - Determine proper interrupt scheme
2221  * @pf: board private structure to initialize
2222  */
2223 static int ice_init_interrupt_scheme(struct ice_pf *pf)
2224 {
2225         int vectors;
2226
2227         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2228                 vectors = ice_ena_msix_range(pf);
2229         else
2230                 return -ENODEV;
2231
2232         if (vectors < 0)
2233                 return vectors;
2234
2235         /* set up vector assignment tracking */
2236         pf->irq_tracker =
2237                 devm_kzalloc(&pf->pdev->dev, sizeof(*pf->irq_tracker) +
2238                              (sizeof(u16) * vectors), GFP_KERNEL);
2239         if (!pf->irq_tracker) {
2240                 ice_dis_msix(pf);
2241                 return -ENOMEM;
2242         }
2243
2244         /* populate SW interrupts pool with number of OS granted IRQs. */
2245         pf->num_avail_sw_msix = vectors;
2246         pf->irq_tracker->num_entries = vectors;
2247         pf->irq_tracker->end = pf->irq_tracker->num_entries;
2248
2249         return 0;
2250 }
2251
2252 /**
2253  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
2254  * @pf: pointer to the PF structure
2255  *
2256  * There is no error returned here because the driver should be able to handle
2257  * 128 Byte cache lines, so we only print a warning in case issues are seen,
2258  * specifically with Tx.
2259  */
2260 static void ice_verify_cacheline_size(struct ice_pf *pf)
2261 {
2262         if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
2263                 dev_warn(&pf->pdev->dev,
2264                          "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
2265                          ICE_CACHE_LINE_BYTES);
2266 }
2267
2268 /**
2269  * ice_probe - Device initialization routine
2270  * @pdev: PCI device information struct
2271  * @ent: entry in ice_pci_tbl
2272  *
2273  * Returns 0 on success, negative on failure
2274  */
2275 static int
2276 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
2277 {
2278         struct device *dev = &pdev->dev;
2279         struct ice_pf *pf;
2280         struct ice_hw *hw;
2281         int err;
2282
2283         /* this driver uses devres, see Documentation/driver-model/devres.txt */
2284         err = pcim_enable_device(pdev);
2285         if (err)
2286                 return err;
2287
2288         err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
2289         if (err) {
2290                 dev_err(dev, "BAR0 I/O map error %d\n", err);
2291                 return err;
2292         }
2293
2294         pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL);
2295         if (!pf)
2296                 return -ENOMEM;
2297
2298         /* set up for high or low dma */
2299         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2300         if (err)
2301                 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2302         if (err) {
2303                 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
2304                 return err;
2305         }
2306
2307         pci_enable_pcie_error_reporting(pdev);
2308         pci_set_master(pdev);
2309
2310         pf->pdev = pdev;
2311         pci_set_drvdata(pdev, pf);
2312         set_bit(__ICE_DOWN, pf->state);
2313         /* Disable service task until DOWN bit is cleared */
2314         set_bit(__ICE_SERVICE_DIS, pf->state);
2315
2316         hw = &pf->hw;
2317         hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
2318         hw->back = pf;
2319         hw->vendor_id = pdev->vendor;
2320         hw->device_id = pdev->device;
2321         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2322         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2323         hw->subsystem_device_id = pdev->subsystem_device;
2324         hw->bus.device = PCI_SLOT(pdev->devfn);
2325         hw->bus.func = PCI_FUNC(pdev->devfn);
2326         ice_set_ctrlq_len(hw);
2327
2328         pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
2329
2330 #ifndef CONFIG_DYNAMIC_DEBUG
2331         if (debug < -1)
2332                 hw->debug_mask = debug;
2333 #endif
2334
2335         err = ice_init_hw(hw);
2336         if (err) {
2337                 dev_err(dev, "ice_init_hw failed: %d\n", err);
2338                 err = -EIO;
2339                 goto err_exit_unroll;
2340         }
2341
2342         dev_info(dev, "firmware %d.%d.%05d api %d.%d\n",
2343                  hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
2344                  hw->api_maj_ver, hw->api_min_ver);
2345
2346         ice_init_pf(pf);
2347
2348         err = ice_init_pf_dcb(pf, false);
2349         if (err) {
2350                 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2351                 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
2352
2353                 /* do not fail overall init if DCB init fails */
2354                 err = 0;
2355         }
2356
2357         ice_determine_q_usage(pf);
2358
2359         pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
2360         if (!pf->num_alloc_vsi) {
2361                 err = -EIO;
2362                 goto err_init_pf_unroll;
2363         }
2364
2365         pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
2366                                GFP_KERNEL);
2367         if (!pf->vsi) {
2368                 err = -ENOMEM;
2369                 goto err_init_pf_unroll;
2370         }
2371
2372         err = ice_init_interrupt_scheme(pf);
2373         if (err) {
2374                 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
2375                 err = -EIO;
2376                 goto err_init_interrupt_unroll;
2377         }
2378
2379         /* Driver is mostly up */
2380         clear_bit(__ICE_DOWN, pf->state);
2381
2382         /* In case of MSIX we are going to setup the misc vector right here
2383          * to handle admin queue events etc. In case of legacy and MSI
2384          * the misc functionality and queue processing is combined in
2385          * the same vector and that gets setup at open.
2386          */
2387         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2388                 err = ice_req_irq_msix_misc(pf);
2389                 if (err) {
2390                         dev_err(dev, "setup of misc vector failed: %d\n", err);
2391                         goto err_init_interrupt_unroll;
2392                 }
2393         }
2394
2395         /* create switch struct for the switch element created by FW on boot */
2396         pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
2397         if (!pf->first_sw) {
2398                 err = -ENOMEM;
2399                 goto err_msix_misc_unroll;
2400         }
2401
2402         if (hw->evb_veb)
2403                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
2404         else
2405                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
2406
2407         pf->first_sw->pf = pf;
2408
2409         /* record the sw_id available for later use */
2410         pf->first_sw->sw_id = hw->port_info->sw_id;
2411
2412         err = ice_setup_pf_sw(pf);
2413         if (err) {
2414                 dev_err(dev, "probe failed due to setup pf switch:%d\n", err);
2415                 goto err_alloc_sw_unroll;
2416         }
2417
2418         clear_bit(__ICE_SERVICE_DIS, pf->state);
2419
2420         /* since everything is good, start the service timer */
2421         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2422
2423         err = ice_init_link_events(pf->hw.port_info);
2424         if (err) {
2425                 dev_err(dev, "ice_init_link_events failed: %d\n", err);
2426                 goto err_alloc_sw_unroll;
2427         }
2428
2429         ice_verify_cacheline_size(pf);
2430
2431         return 0;
2432
2433 err_alloc_sw_unroll:
2434         set_bit(__ICE_SERVICE_DIS, pf->state);
2435         set_bit(__ICE_DOWN, pf->state);
2436         devm_kfree(&pf->pdev->dev, pf->first_sw);
2437 err_msix_misc_unroll:
2438         ice_free_irq_msix_misc(pf);
2439 err_init_interrupt_unroll:
2440         ice_clear_interrupt_scheme(pf);
2441         devm_kfree(dev, pf->vsi);
2442 err_init_pf_unroll:
2443         ice_deinit_pf(pf);
2444         ice_deinit_hw(hw);
2445 err_exit_unroll:
2446         pci_disable_pcie_error_reporting(pdev);
2447         return err;
2448 }
2449
2450 /**
2451  * ice_remove - Device removal routine
2452  * @pdev: PCI device information struct
2453  */
2454 static void ice_remove(struct pci_dev *pdev)
2455 {
2456         struct ice_pf *pf = pci_get_drvdata(pdev);
2457         int i;
2458
2459         if (!pf)
2460                 return;
2461
2462         for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
2463                 if (!ice_is_reset_in_progress(pf->state))
2464                         break;
2465                 msleep(100);
2466         }
2467
2468         set_bit(__ICE_DOWN, pf->state);
2469         ice_service_task_stop(pf);
2470
2471         if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
2472                 ice_free_vfs(pf);
2473         ice_vsi_release_all(pf);
2474         ice_free_irq_msix_misc(pf);
2475         ice_for_each_vsi(pf, i) {
2476                 if (!pf->vsi[i])
2477                         continue;
2478                 ice_vsi_free_q_vectors(pf->vsi[i]);
2479         }
2480         ice_clear_interrupt_scheme(pf);
2481         ice_deinit_pf(pf);
2482         ice_deinit_hw(&pf->hw);
2483         pci_disable_pcie_error_reporting(pdev);
2484 }
2485
2486 /**
2487  * ice_pci_err_detected - warning that PCI error has been detected
2488  * @pdev: PCI device information struct
2489  * @err: the type of PCI error
2490  *
2491  * Called to warn that something happened on the PCI bus and the error handling
2492  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
2493  */
2494 static pci_ers_result_t
2495 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err)
2496 {
2497         struct ice_pf *pf = pci_get_drvdata(pdev);
2498
2499         if (!pf) {
2500                 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
2501                         __func__, err);
2502                 return PCI_ERS_RESULT_DISCONNECT;
2503         }
2504
2505         if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2506                 ice_service_task_stop(pf);
2507
2508                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2509                         set_bit(__ICE_PFR_REQ, pf->state);
2510                         ice_prepare_for_reset(pf);
2511                 }
2512         }
2513
2514         return PCI_ERS_RESULT_NEED_RESET;
2515 }
2516
2517 /**
2518  * ice_pci_err_slot_reset - a PCI slot reset has just happened
2519  * @pdev: PCI device information struct
2520  *
2521  * Called to determine if the driver can recover from the PCI slot reset by
2522  * using a register read to determine if the device is recoverable.
2523  */
2524 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
2525 {
2526         struct ice_pf *pf = pci_get_drvdata(pdev);
2527         pci_ers_result_t result;
2528         int err;
2529         u32 reg;
2530
2531         err = pci_enable_device_mem(pdev);
2532         if (err) {
2533                 dev_err(&pdev->dev,
2534                         "Cannot re-enable PCI device after reset, error %d\n",
2535                         err);
2536                 result = PCI_ERS_RESULT_DISCONNECT;
2537         } else {
2538                 pci_set_master(pdev);
2539                 pci_restore_state(pdev);
2540                 pci_save_state(pdev);
2541                 pci_wake_from_d3(pdev, false);
2542
2543                 /* Check for life */
2544                 reg = rd32(&pf->hw, GLGEN_RTRIG);
2545                 if (!reg)
2546                         result = PCI_ERS_RESULT_RECOVERED;
2547                 else
2548                         result = PCI_ERS_RESULT_DISCONNECT;
2549         }
2550
2551         err = pci_cleanup_aer_uncorrect_error_status(pdev);
2552         if (err)
2553                 dev_dbg(&pdev->dev,
2554                         "pci_cleanup_aer_uncorrect_error_status failed, error %d\n",
2555                         err);
2556                 /* non-fatal, continue */
2557
2558         return result;
2559 }
2560
2561 /**
2562  * ice_pci_err_resume - restart operations after PCI error recovery
2563  * @pdev: PCI device information struct
2564  *
2565  * Called to allow the driver to bring things back up after PCI error and/or
2566  * reset recovery have finished
2567  */
2568 static void ice_pci_err_resume(struct pci_dev *pdev)
2569 {
2570         struct ice_pf *pf = pci_get_drvdata(pdev);
2571
2572         if (!pf) {
2573                 dev_err(&pdev->dev,
2574                         "%s failed, device is unrecoverable\n", __func__);
2575                 return;
2576         }
2577
2578         if (test_bit(__ICE_SUSPENDED, pf->state)) {
2579                 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
2580                         __func__);
2581                 return;
2582         }
2583
2584         ice_do_reset(pf, ICE_RESET_PFR);
2585         ice_service_task_restart(pf);
2586         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2587 }
2588
2589 /**
2590  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
2591  * @pdev: PCI device information struct
2592  */
2593 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
2594 {
2595         struct ice_pf *pf = pci_get_drvdata(pdev);
2596
2597         if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2598                 ice_service_task_stop(pf);
2599
2600                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2601                         set_bit(__ICE_PFR_REQ, pf->state);
2602                         ice_prepare_for_reset(pf);
2603                 }
2604         }
2605 }
2606
2607 /**
2608  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
2609  * @pdev: PCI device information struct
2610  */
2611 static void ice_pci_err_reset_done(struct pci_dev *pdev)
2612 {
2613         ice_pci_err_resume(pdev);
2614 }
2615
2616 /* ice_pci_tbl - PCI Device ID Table
2617  *
2618  * Wildcard entries (PCI_ANY_ID) should come last
2619  * Last entry must be all 0s
2620  *
2621  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
2622  *   Class, Class Mask, private data (not used) }
2623  */
2624 static const struct pci_device_id ice_pci_tbl[] = {
2625         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
2626         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
2627         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
2628         /* required last entry */
2629         { 0, }
2630 };
2631 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
2632
2633 static const struct pci_error_handlers ice_pci_err_handler = {
2634         .error_detected = ice_pci_err_detected,
2635         .slot_reset = ice_pci_err_slot_reset,
2636         .reset_prepare = ice_pci_err_reset_prepare,
2637         .reset_done = ice_pci_err_reset_done,
2638         .resume = ice_pci_err_resume
2639 };
2640
2641 static struct pci_driver ice_driver = {
2642         .name = KBUILD_MODNAME,
2643         .id_table = ice_pci_tbl,
2644         .probe = ice_probe,
2645         .remove = ice_remove,
2646         .sriov_configure = ice_sriov_configure,
2647         .err_handler = &ice_pci_err_handler
2648 };
2649
2650 /**
2651  * ice_module_init - Driver registration routine
2652  *
2653  * ice_module_init is the first routine called when the driver is
2654  * loaded. All it does is register with the PCI subsystem.
2655  */
2656 static int __init ice_module_init(void)
2657 {
2658         int status;
2659
2660         pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
2661         pr_info("%s\n", ice_copyright);
2662
2663         ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
2664         if (!ice_wq) {
2665                 pr_err("Failed to create workqueue\n");
2666                 return -ENOMEM;
2667         }
2668
2669         status = pci_register_driver(&ice_driver);
2670         if (status) {
2671                 pr_err("failed to register pci driver, err %d\n", status);
2672                 destroy_workqueue(ice_wq);
2673         }
2674
2675         return status;
2676 }
2677 module_init(ice_module_init);
2678
2679 /**
2680  * ice_module_exit - Driver exit cleanup routine
2681  *
2682  * ice_module_exit is called just before the driver is removed
2683  * from memory.
2684  */
2685 static void __exit ice_module_exit(void)
2686 {
2687         pci_unregister_driver(&ice_driver);
2688         destroy_workqueue(ice_wq);
2689         pr_info("module unloaded\n");
2690 }
2691 module_exit(ice_module_exit);
2692
2693 /**
2694  * ice_set_mac_address - NDO callback to set MAC address
2695  * @netdev: network interface device structure
2696  * @pi: pointer to an address structure
2697  *
2698  * Returns 0 on success, negative on failure
2699  */
2700 static int ice_set_mac_address(struct net_device *netdev, void *pi)
2701 {
2702         struct ice_netdev_priv *np = netdev_priv(netdev);
2703         struct ice_vsi *vsi = np->vsi;
2704         struct ice_pf *pf = vsi->back;
2705         struct ice_hw *hw = &pf->hw;
2706         struct sockaddr *addr = pi;
2707         enum ice_status status;
2708         LIST_HEAD(a_mac_list);
2709         LIST_HEAD(r_mac_list);
2710         u8 flags = 0;
2711         int err;
2712         u8 *mac;
2713
2714         mac = (u8 *)addr->sa_data;
2715
2716         if (!is_valid_ether_addr(mac))
2717                 return -EADDRNOTAVAIL;
2718
2719         if (ether_addr_equal(netdev->dev_addr, mac)) {
2720                 netdev_warn(netdev, "already using mac %pM\n", mac);
2721                 return 0;
2722         }
2723
2724         if (test_bit(__ICE_DOWN, pf->state) ||
2725             ice_is_reset_in_progress(pf->state)) {
2726                 netdev_err(netdev, "can't set mac %pM. device not ready\n",
2727                            mac);
2728                 return -EBUSY;
2729         }
2730
2731         /* When we change the MAC address we also have to change the MAC address
2732          * based filter rules that were created previously for the old MAC
2733          * address. So first, we remove the old filter rule using ice_remove_mac
2734          * and then create a new filter rule using ice_add_mac. Note that for
2735          * both these operations, we first need to form a "list" of MAC
2736          * addresses (even though in this case, we have only 1 MAC address to be
2737          * added/removed) and this done using ice_add_mac_to_list. Depending on
2738          * the ensuing operation this "list" of MAC addresses is either to be
2739          * added or removed from the filter.
2740          */
2741         err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
2742         if (err) {
2743                 err = -EADDRNOTAVAIL;
2744                 goto free_lists;
2745         }
2746
2747         status = ice_remove_mac(hw, &r_mac_list);
2748         if (status) {
2749                 err = -EADDRNOTAVAIL;
2750                 goto free_lists;
2751         }
2752
2753         err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
2754         if (err) {
2755                 err = -EADDRNOTAVAIL;
2756                 goto free_lists;
2757         }
2758
2759         status = ice_add_mac(hw, &a_mac_list);
2760         if (status) {
2761                 err = -EADDRNOTAVAIL;
2762                 goto free_lists;
2763         }
2764
2765 free_lists:
2766         /* free list entries */
2767         ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
2768         ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
2769
2770         if (err) {
2771                 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
2772                            mac);
2773                 return err;
2774         }
2775
2776         /* change the netdev's MAC address */
2777         memcpy(netdev->dev_addr, mac, netdev->addr_len);
2778         netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
2779                    netdev->dev_addr);
2780
2781         /* write new MAC address to the firmware */
2782         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2783         status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
2784         if (status) {
2785                 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
2786                            mac);
2787         }
2788         return 0;
2789 }
2790
2791 /**
2792  * ice_set_rx_mode - NDO callback to set the netdev filters
2793  * @netdev: network interface device structure
2794  */
2795 static void ice_set_rx_mode(struct net_device *netdev)
2796 {
2797         struct ice_netdev_priv *np = netdev_priv(netdev);
2798         struct ice_vsi *vsi = np->vsi;
2799
2800         if (!vsi)
2801                 return;
2802
2803         /* Set the flags to synchronize filters
2804          * ndo_set_rx_mode may be triggered even without a change in netdev
2805          * flags
2806          */
2807         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
2808         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
2809         set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
2810
2811         /* schedule our worker thread which will take care of
2812          * applying the new filter changes
2813          */
2814         ice_service_task_schedule(vsi->back);
2815 }
2816
2817 /**
2818  * ice_fdb_add - add an entry to the hardware database
2819  * @ndm: the input from the stack
2820  * @tb: pointer to array of nladdr (unused)
2821  * @dev: the net device pointer
2822  * @addr: the MAC address entry being added
2823  * @vid: VLAN ID
2824  * @flags: instructions from stack about fdb operation
2825  * @extack: netlink extended ack
2826  */
2827 static int
2828 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
2829             struct net_device *dev, const unsigned char *addr, u16 vid,
2830             u16 flags, struct netlink_ext_ack __always_unused *extack)
2831 {
2832         int err;
2833
2834         if (vid) {
2835                 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
2836                 return -EINVAL;
2837         }
2838         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2839                 netdev_err(dev, "FDB only supports static addresses\n");
2840                 return -EINVAL;
2841         }
2842
2843         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2844                 err = dev_uc_add_excl(dev, addr);
2845         else if (is_multicast_ether_addr(addr))
2846                 err = dev_mc_add_excl(dev, addr);
2847         else
2848                 err = -EINVAL;
2849
2850         /* Only return duplicate errors if NLM_F_EXCL is set */
2851         if (err == -EEXIST && !(flags & NLM_F_EXCL))
2852                 err = 0;
2853
2854         return err;
2855 }
2856
2857 /**
2858  * ice_fdb_del - delete an entry from the hardware database
2859  * @ndm: the input from the stack
2860  * @tb: pointer to array of nladdr (unused)
2861  * @dev: the net device pointer
2862  * @addr: the MAC address entry being added
2863  * @vid: VLAN ID
2864  */
2865 static int
2866 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
2867             struct net_device *dev, const unsigned char *addr,
2868             __always_unused u16 vid)
2869 {
2870         int err;
2871
2872         if (ndm->ndm_state & NUD_PERMANENT) {
2873                 netdev_err(dev, "FDB only supports static addresses\n");
2874                 return -EINVAL;
2875         }
2876
2877         if (is_unicast_ether_addr(addr))
2878                 err = dev_uc_del(dev, addr);
2879         else if (is_multicast_ether_addr(addr))
2880                 err = dev_mc_del(dev, addr);
2881         else
2882                 err = -EINVAL;
2883
2884         return err;
2885 }
2886
2887 /**
2888  * ice_set_features - set the netdev feature flags
2889  * @netdev: ptr to the netdev being adjusted
2890  * @features: the feature set that the stack is suggesting
2891  */
2892 static int
2893 ice_set_features(struct net_device *netdev, netdev_features_t features)
2894 {
2895         struct ice_netdev_priv *np = netdev_priv(netdev);
2896         struct ice_vsi *vsi = np->vsi;
2897         int ret = 0;
2898
2899         /* Multiple features can be changed in one call so keep features in
2900          * separate if/else statements to guarantee each feature is checked
2901          */
2902         if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
2903                 ret = ice_vsi_manage_rss_lut(vsi, true);
2904         else if (!(features & NETIF_F_RXHASH) &&
2905                  netdev->features & NETIF_F_RXHASH)
2906                 ret = ice_vsi_manage_rss_lut(vsi, false);
2907
2908         if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
2909             !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2910                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2911         else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
2912                  (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2913                 ret = ice_vsi_manage_vlan_stripping(vsi, false);
2914
2915         if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
2916             !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2917                 ret = ice_vsi_manage_vlan_insertion(vsi);
2918         else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
2919                  (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2920                 ret = ice_vsi_manage_vlan_insertion(vsi);
2921
2922         if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2923             !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2924                 ret = ice_cfg_vlan_pruning(vsi, true, false);
2925         else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2926                  (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2927                 ret = ice_cfg_vlan_pruning(vsi, false, false);
2928
2929         return ret;
2930 }
2931
2932 /**
2933  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
2934  * @vsi: VSI to setup VLAN properties for
2935  */
2936 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
2937 {
2938         int ret = 0;
2939
2940         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2941                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2942         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
2943                 ret = ice_vsi_manage_vlan_insertion(vsi);
2944
2945         return ret;
2946 }
2947
2948 /**
2949  * ice_vsi_cfg - Setup the VSI
2950  * @vsi: the VSI being configured
2951  *
2952  * Return 0 on success and negative value on error
2953  */
2954 int ice_vsi_cfg(struct ice_vsi *vsi)
2955 {
2956         int err;
2957
2958         if (vsi->netdev) {
2959                 ice_set_rx_mode(vsi->netdev);
2960
2961                 err = ice_vsi_vlan_setup(vsi);
2962
2963                 if (err)
2964                         return err;
2965         }
2966         ice_vsi_cfg_dcb_rings(vsi);
2967
2968         err = ice_vsi_cfg_lan_txqs(vsi);
2969         if (!err)
2970                 err = ice_vsi_cfg_rxqs(vsi);
2971
2972         return err;
2973 }
2974
2975 /**
2976  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
2977  * @vsi: the VSI being configured
2978  */
2979 static void ice_napi_enable_all(struct ice_vsi *vsi)
2980 {
2981         int q_idx;
2982
2983         if (!vsi->netdev)
2984                 return;
2985
2986         ice_for_each_q_vector(vsi, q_idx) {
2987                 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
2988
2989                 if (q_vector->rx.ring || q_vector->tx.ring)
2990                         napi_enable(&q_vector->napi);
2991         }
2992 }
2993
2994 /**
2995  * ice_up_complete - Finish the last steps of bringing up a connection
2996  * @vsi: The VSI being configured
2997  *
2998  * Return 0 on success and negative value on error
2999  */
3000 static int ice_up_complete(struct ice_vsi *vsi)
3001 {
3002         struct ice_pf *pf = vsi->back;
3003         int err;
3004
3005         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3006                 ice_vsi_cfg_msix(vsi);
3007         else
3008                 return -ENOTSUPP;
3009
3010         /* Enable only Rx rings, Tx rings were enabled by the FW when the
3011          * Tx queue group list was configured and the context bits were
3012          * programmed using ice_vsi_cfg_txqs
3013          */
3014         err = ice_vsi_start_rx_rings(vsi);
3015         if (err)
3016                 return err;
3017
3018         clear_bit(__ICE_DOWN, vsi->state);
3019         ice_napi_enable_all(vsi);
3020         ice_vsi_ena_irq(vsi);
3021
3022         if (vsi->port_info &&
3023             (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
3024             vsi->netdev) {
3025                 ice_print_link_msg(vsi, true);
3026                 netif_tx_start_all_queues(vsi->netdev);
3027                 netif_carrier_on(vsi->netdev);
3028         }
3029
3030         ice_service_task_schedule(pf);
3031
3032         return 0;
3033 }
3034
3035 /**
3036  * ice_up - Bring the connection back up after being down
3037  * @vsi: VSI being configured
3038  */
3039 int ice_up(struct ice_vsi *vsi)
3040 {
3041         int err;
3042
3043         err = ice_vsi_cfg(vsi);
3044         if (!err)
3045                 err = ice_up_complete(vsi);
3046
3047         return err;
3048 }
3049
3050 /**
3051  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
3052  * @ring: Tx or Rx ring to read stats from
3053  * @pkts: packets stats counter
3054  * @bytes: bytes stats counter
3055  *
3056  * This function fetches stats from the ring considering the atomic operations
3057  * that needs to be performed to read u64 values in 32 bit machine.
3058  */
3059 static void
3060 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
3061 {
3062         unsigned int start;
3063         *pkts = 0;
3064         *bytes = 0;
3065
3066         if (!ring)
3067                 return;
3068         do {
3069                 start = u64_stats_fetch_begin_irq(&ring->syncp);
3070                 *pkts = ring->stats.pkts;
3071                 *bytes = ring->stats.bytes;
3072         } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3073 }
3074
3075 /**
3076  * ice_update_vsi_ring_stats - Update VSI stats counters
3077  * @vsi: the VSI to be updated
3078  */
3079 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
3080 {
3081         struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
3082         struct ice_ring *ring;
3083         u64 pkts, bytes;
3084         int i;
3085
3086         /* reset netdev stats */
3087         vsi_stats->tx_packets = 0;
3088         vsi_stats->tx_bytes = 0;
3089         vsi_stats->rx_packets = 0;
3090         vsi_stats->rx_bytes = 0;
3091
3092         /* reset non-netdev (extended) stats */
3093         vsi->tx_restart = 0;
3094         vsi->tx_busy = 0;
3095         vsi->tx_linearize = 0;
3096         vsi->rx_buf_failed = 0;
3097         vsi->rx_page_failed = 0;
3098
3099         rcu_read_lock();
3100
3101         /* update Tx rings counters */
3102         ice_for_each_txq(vsi, i) {
3103                 ring = READ_ONCE(vsi->tx_rings[i]);
3104                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3105                 vsi_stats->tx_packets += pkts;
3106                 vsi_stats->tx_bytes += bytes;
3107                 vsi->tx_restart += ring->tx_stats.restart_q;
3108                 vsi->tx_busy += ring->tx_stats.tx_busy;
3109                 vsi->tx_linearize += ring->tx_stats.tx_linearize;
3110         }
3111
3112         /* update Rx rings counters */
3113         ice_for_each_rxq(vsi, i) {
3114                 ring = READ_ONCE(vsi->rx_rings[i]);
3115                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3116                 vsi_stats->rx_packets += pkts;
3117                 vsi_stats->rx_bytes += bytes;
3118                 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
3119                 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
3120         }
3121
3122         rcu_read_unlock();
3123 }
3124
3125 /**
3126  * ice_update_vsi_stats - Update VSI stats counters
3127  * @vsi: the VSI to be updated
3128  */
3129 static void ice_update_vsi_stats(struct ice_vsi *vsi)
3130 {
3131         struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
3132         struct ice_eth_stats *cur_es = &vsi->eth_stats;
3133         struct ice_pf *pf = vsi->back;
3134
3135         if (test_bit(__ICE_DOWN, vsi->state) ||
3136             test_bit(__ICE_CFG_BUSY, pf->state))
3137                 return;
3138
3139         /* get stats as recorded by Tx/Rx rings */
3140         ice_update_vsi_ring_stats(vsi);
3141
3142         /* get VSI stats as recorded by the hardware */
3143         ice_update_eth_stats(vsi);
3144
3145         cur_ns->tx_errors = cur_es->tx_errors;
3146         cur_ns->rx_dropped = cur_es->rx_discards;
3147         cur_ns->tx_dropped = cur_es->tx_discards;
3148         cur_ns->multicast = cur_es->rx_multicast;
3149
3150         /* update some more netdev stats if this is main VSI */
3151         if (vsi->type == ICE_VSI_PF) {
3152                 cur_ns->rx_crc_errors = pf->stats.crc_errors;
3153                 cur_ns->rx_errors = pf->stats.crc_errors +
3154                                     pf->stats.illegal_bytes;
3155                 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
3156         }
3157 }
3158
3159 /**
3160  * ice_update_pf_stats - Update PF port stats counters
3161  * @pf: PF whose stats needs to be updated
3162  */
3163 static void ice_update_pf_stats(struct ice_pf *pf)
3164 {
3165         struct ice_hw_port_stats *prev_ps, *cur_ps;
3166         struct ice_hw *hw = &pf->hw;
3167         u8 pf_id;
3168
3169         prev_ps = &pf->stats_prev;
3170         cur_ps = &pf->stats;
3171         pf_id = hw->pf_id;
3172
3173         ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
3174                           pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
3175                           &cur_ps->eth.rx_bytes);
3176
3177         ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
3178                           pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
3179                           &cur_ps->eth.rx_unicast);
3180
3181         ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
3182                           pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
3183                           &cur_ps->eth.rx_multicast);
3184
3185         ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
3186                           pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
3187                           &cur_ps->eth.rx_broadcast);
3188
3189         ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
3190                           pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
3191                           &cur_ps->eth.tx_bytes);
3192
3193         ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
3194                           pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
3195                           &cur_ps->eth.tx_unicast);
3196
3197         ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
3198                           pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
3199                           &cur_ps->eth.tx_multicast);
3200
3201         ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
3202                           pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
3203                           &cur_ps->eth.tx_broadcast);
3204
3205         ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
3206                           &prev_ps->tx_dropped_link_down,
3207                           &cur_ps->tx_dropped_link_down);
3208
3209         ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
3210                           pf->stat_prev_loaded, &prev_ps->rx_size_64,
3211                           &cur_ps->rx_size_64);
3212
3213         ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
3214                           pf->stat_prev_loaded, &prev_ps->rx_size_127,
3215                           &cur_ps->rx_size_127);
3216
3217         ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
3218                           pf->stat_prev_loaded, &prev_ps->rx_size_255,
3219                           &cur_ps->rx_size_255);
3220
3221         ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
3222                           pf->stat_prev_loaded, &prev_ps->rx_size_511,
3223                           &cur_ps->rx_size_511);
3224
3225         ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
3226                           GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
3227                           &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
3228
3229         ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
3230                           GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
3231                           &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
3232
3233         ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
3234                           GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
3235                           &prev_ps->rx_size_big, &cur_ps->rx_size_big);
3236
3237         ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
3238                           pf->stat_prev_loaded, &prev_ps->tx_size_64,
3239                           &cur_ps->tx_size_64);
3240
3241         ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
3242                           pf->stat_prev_loaded, &prev_ps->tx_size_127,
3243                           &cur_ps->tx_size_127);
3244
3245         ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
3246                           pf->stat_prev_loaded, &prev_ps->tx_size_255,
3247                           &cur_ps->tx_size_255);
3248
3249         ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
3250                           pf->stat_prev_loaded, &prev_ps->tx_size_511,
3251                           &cur_ps->tx_size_511);
3252
3253         ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
3254                           GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
3255                           &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
3256
3257         ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
3258                           GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
3259                           &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
3260
3261         ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
3262                           GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
3263                           &prev_ps->tx_size_big, &cur_ps->tx_size_big);
3264
3265         ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
3266                           &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
3267
3268         ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
3269                           &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
3270
3271         ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
3272                           &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
3273
3274         ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
3275                           &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
3276
3277         ice_update_dcb_stats(pf);
3278
3279         ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
3280                           &prev_ps->crc_errors, &cur_ps->crc_errors);
3281
3282         ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
3283                           &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
3284
3285         ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
3286                           &prev_ps->mac_local_faults,
3287                           &cur_ps->mac_local_faults);
3288
3289         ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
3290                           &prev_ps->mac_remote_faults,
3291                           &cur_ps->mac_remote_faults);
3292
3293         ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
3294                           &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
3295
3296         ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
3297                           &prev_ps->rx_undersize, &cur_ps->rx_undersize);
3298
3299         ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
3300                           &prev_ps->rx_fragments, &cur_ps->rx_fragments);
3301
3302         ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
3303                           &prev_ps->rx_oversize, &cur_ps->rx_oversize);
3304
3305         ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
3306                           &prev_ps->rx_jabber, &cur_ps->rx_jabber);
3307
3308         pf->stat_prev_loaded = true;
3309 }
3310
3311 /**
3312  * ice_get_stats64 - get statistics for network device structure
3313  * @netdev: network interface device structure
3314  * @stats: main device statistics structure
3315  */
3316 static
3317 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
3318 {
3319         struct ice_netdev_priv *np = netdev_priv(netdev);
3320         struct rtnl_link_stats64 *vsi_stats;
3321         struct ice_vsi *vsi = np->vsi;
3322
3323         vsi_stats = &vsi->net_stats;
3324
3325         if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
3326                 return;
3327         /* netdev packet/byte stats come from ring counter. These are obtained
3328          * by summing up ring counters (done by ice_update_vsi_ring_stats).
3329          */
3330         ice_update_vsi_ring_stats(vsi);
3331         stats->tx_packets = vsi_stats->tx_packets;
3332         stats->tx_bytes = vsi_stats->tx_bytes;
3333         stats->rx_packets = vsi_stats->rx_packets;
3334         stats->rx_bytes = vsi_stats->rx_bytes;
3335
3336         /* The rest of the stats can be read from the hardware but instead we
3337          * just return values that the watchdog task has already obtained from
3338          * the hardware.
3339          */
3340         stats->multicast = vsi_stats->multicast;
3341         stats->tx_errors = vsi_stats->tx_errors;
3342         stats->tx_dropped = vsi_stats->tx_dropped;
3343         stats->rx_errors = vsi_stats->rx_errors;
3344         stats->rx_dropped = vsi_stats->rx_dropped;
3345         stats->rx_crc_errors = vsi_stats->rx_crc_errors;
3346         stats->rx_length_errors = vsi_stats->rx_length_errors;
3347 }
3348
3349 /**
3350  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3351  * @vsi: VSI having NAPI disabled
3352  */
3353 static void ice_napi_disable_all(struct ice_vsi *vsi)
3354 {
3355         int q_idx;
3356
3357         if (!vsi->netdev)
3358                 return;
3359
3360         ice_for_each_q_vector(vsi, q_idx) {
3361                 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3362
3363                 if (q_vector->rx.ring || q_vector->tx.ring)
3364                         napi_disable(&q_vector->napi);
3365         }
3366 }
3367
3368 /**
3369  * ice_force_phys_link_state - Force the physical link state
3370  * @vsi: VSI to force the physical link state to up/down
3371  * @link_up: true/false indicates to set the physical link to up/down
3372  *
3373  * Force the physical link state by getting the current PHY capabilities from
3374  * hardware and setting the PHY config based on the determined capabilities. If
3375  * link changes a link event will be triggered because both the Enable Automatic
3376  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
3377  *
3378  * Returns 0 on success, negative on failure
3379  */
3380 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
3381 {
3382         struct ice_aqc_get_phy_caps_data *pcaps;
3383         struct ice_aqc_set_phy_cfg_data *cfg;
3384         struct ice_port_info *pi;
3385         struct device *dev;
3386         int retcode;
3387
3388         if (!vsi || !vsi->port_info || !vsi->back)
3389                 return -EINVAL;
3390         if (vsi->type != ICE_VSI_PF)
3391                 return 0;
3392
3393         dev = &vsi->back->pdev->dev;
3394
3395         pi = vsi->port_info;
3396
3397         pcaps = devm_kzalloc(dev, sizeof(*pcaps), GFP_KERNEL);
3398         if (!pcaps)
3399                 return -ENOMEM;
3400
3401         retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
3402                                       NULL);
3403         if (retcode) {
3404                 dev_err(dev,
3405                         "Failed to get phy capabilities, VSI %d error %d\n",
3406                         vsi->vsi_num, retcode);
3407                 retcode = -EIO;
3408                 goto out;
3409         }
3410
3411         /* No change in link */
3412         if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
3413             link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
3414                 goto out;
3415
3416         cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
3417         if (!cfg) {
3418                 retcode = -ENOMEM;
3419                 goto out;
3420         }
3421
3422         cfg->phy_type_low = pcaps->phy_type_low;
3423         cfg->phy_type_high = pcaps->phy_type_high;
3424         cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
3425         cfg->low_power_ctrl = pcaps->low_power_ctrl;
3426         cfg->eee_cap = pcaps->eee_cap;
3427         cfg->eeer_value = pcaps->eeer_value;
3428         cfg->link_fec_opt = pcaps->link_fec_options;
3429         if (link_up)
3430                 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
3431         else
3432                 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
3433
3434         retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL);
3435         if (retcode) {
3436                 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
3437                         vsi->vsi_num, retcode);
3438                 retcode = -EIO;
3439         }
3440
3441         devm_kfree(dev, cfg);
3442 out:
3443         devm_kfree(dev, pcaps);
3444         return retcode;
3445 }
3446
3447 /**
3448  * ice_down - Shutdown the connection
3449  * @vsi: The VSI being stopped
3450  */
3451 int ice_down(struct ice_vsi *vsi)
3452 {
3453         int i, tx_err, rx_err, link_err = 0;
3454
3455         /* Caller of this function is expected to set the
3456          * vsi->state __ICE_DOWN bit
3457          */
3458         if (vsi->netdev) {
3459                 netif_carrier_off(vsi->netdev);
3460                 netif_tx_disable(vsi->netdev);
3461         }
3462
3463         ice_vsi_dis_irq(vsi);
3464
3465         tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
3466         if (tx_err)
3467                 netdev_err(vsi->netdev,
3468                            "Failed stop Tx rings, VSI %d error %d\n",
3469                            vsi->vsi_num, tx_err);
3470
3471         rx_err = ice_vsi_stop_rx_rings(vsi);
3472         if (rx_err)
3473                 netdev_err(vsi->netdev,
3474                            "Failed stop Rx rings, VSI %d error %d\n",
3475                            vsi->vsi_num, rx_err);
3476
3477         ice_napi_disable_all(vsi);
3478
3479         if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
3480                 link_err = ice_force_phys_link_state(vsi, false);
3481                 if (link_err)
3482                         netdev_err(vsi->netdev,
3483                                    "Failed to set physical link down, VSI %d error %d\n",
3484                                    vsi->vsi_num, link_err);
3485         }
3486
3487         ice_for_each_txq(vsi, i)
3488                 ice_clean_tx_ring(vsi->tx_rings[i]);
3489
3490         ice_for_each_rxq(vsi, i)
3491                 ice_clean_rx_ring(vsi->rx_rings[i]);
3492
3493         if (tx_err || rx_err || link_err) {
3494                 netdev_err(vsi->netdev,
3495                            "Failed to close VSI 0x%04X on switch 0x%04X\n",
3496                            vsi->vsi_num, vsi->vsw->sw_id);
3497                 return -EIO;
3498         }
3499
3500         return 0;
3501 }
3502
3503 /**
3504  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
3505  * @vsi: VSI having resources allocated
3506  *
3507  * Return 0 on success, negative on failure
3508  */
3509 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
3510 {
3511         int i, err = 0;
3512
3513         if (!vsi->num_txq) {
3514                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
3515                         vsi->vsi_num);
3516                 return -EINVAL;
3517         }
3518
3519         ice_for_each_txq(vsi, i) {
3520                 vsi->tx_rings[i]->netdev = vsi->netdev;
3521                 err = ice_setup_tx_ring(vsi->tx_rings[i]);
3522                 if (err)
3523                         break;
3524         }
3525
3526         return err;
3527 }
3528
3529 /**
3530  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
3531  * @vsi: VSI having resources allocated
3532  *
3533  * Return 0 on success, negative on failure
3534  */
3535 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
3536 {
3537         int i, err = 0;
3538
3539         if (!vsi->num_rxq) {
3540                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
3541                         vsi->vsi_num);
3542                 return -EINVAL;
3543         }
3544
3545         ice_for_each_rxq(vsi, i) {
3546                 vsi->rx_rings[i]->netdev = vsi->netdev;
3547                 err = ice_setup_rx_ring(vsi->rx_rings[i]);
3548                 if (err)
3549                         break;
3550         }
3551
3552         return err;
3553 }
3554
3555 /**
3556  * ice_vsi_req_irq - Request IRQ from the OS
3557  * @vsi: The VSI IRQ is being requested for
3558  * @basename: name for the vector
3559  *
3560  * Return 0 on success and a negative value on error
3561  */
3562 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
3563 {
3564         struct ice_pf *pf = vsi->back;
3565         int err = -EINVAL;
3566
3567         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3568                 err = ice_vsi_req_irq_msix(vsi, basename);
3569
3570         return err;
3571 }
3572
3573 /**
3574  * ice_vsi_open - Called when a network interface is made active
3575  * @vsi: the VSI to open
3576  *
3577  * Initialization of the VSI
3578  *
3579  * Returns 0 on success, negative value on error
3580  */
3581 static int ice_vsi_open(struct ice_vsi *vsi)
3582 {
3583         char int_name[ICE_INT_NAME_STR_LEN];
3584         struct ice_pf *pf = vsi->back;
3585         int err;
3586
3587         /* allocate descriptors */
3588         err = ice_vsi_setup_tx_rings(vsi);
3589         if (err)
3590                 goto err_setup_tx;
3591
3592         err = ice_vsi_setup_rx_rings(vsi);
3593         if (err)
3594                 goto err_setup_rx;
3595
3596         err = ice_vsi_cfg(vsi);
3597         if (err)
3598                 goto err_setup_rx;
3599
3600         snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3601                  dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
3602         err = ice_vsi_req_irq(vsi, int_name);
3603         if (err)
3604                 goto err_setup_rx;
3605
3606         /* Notify the stack of the actual queue counts. */
3607         err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
3608         if (err)
3609                 goto err_set_qs;
3610
3611         err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
3612         if (err)
3613                 goto err_set_qs;
3614
3615         err = ice_up_complete(vsi);
3616         if (err)
3617                 goto err_up_complete;
3618
3619         return 0;
3620
3621 err_up_complete:
3622         ice_down(vsi);
3623 err_set_qs:
3624         ice_vsi_free_irq(vsi);
3625 err_setup_rx:
3626         ice_vsi_free_rx_rings(vsi);
3627 err_setup_tx:
3628         ice_vsi_free_tx_rings(vsi);
3629
3630         return err;
3631 }
3632
3633 /**
3634  * ice_vsi_release_all - Delete all VSIs
3635  * @pf: PF from which all VSIs are being removed
3636  */
3637 static void ice_vsi_release_all(struct ice_pf *pf)
3638 {
3639         int err, i;
3640
3641         if (!pf->vsi)
3642                 return;
3643
3644         ice_for_each_vsi(pf, i) {
3645                 if (!pf->vsi[i])
3646                         continue;
3647
3648                 err = ice_vsi_release(pf->vsi[i]);
3649                 if (err)
3650                         dev_dbg(&pf->pdev->dev,
3651                                 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
3652                                 i, err, pf->vsi[i]->vsi_num);
3653         }
3654 }
3655
3656 /**
3657  * ice_ena_vsi - resume a VSI
3658  * @vsi: the VSI being resume
3659  * @locked: is the rtnl_lock already held
3660  */
3661 static int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
3662 {
3663         int err = 0;
3664
3665         if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
3666                 return err;
3667
3668         clear_bit(__ICE_NEEDS_RESTART, vsi->state);
3669
3670         if (vsi->netdev && vsi->type == ICE_VSI_PF) {
3671                 struct net_device *netd = vsi->netdev;
3672
3673                 if (netif_running(vsi->netdev)) {
3674                         if (locked) {
3675                                 err = netd->netdev_ops->ndo_open(netd);
3676                         } else {
3677                                 rtnl_lock();
3678                                 err = netd->netdev_ops->ndo_open(netd);
3679                                 rtnl_unlock();
3680                         }
3681                 } else {
3682                         err = ice_vsi_open(vsi);
3683                 }
3684         }
3685
3686         return err;
3687 }
3688
3689 /**
3690  * ice_pf_ena_all_vsi - Resume all VSIs on a PF
3691  * @pf: the PF
3692  * @locked: is the rtnl_lock already held
3693  */
3694 #ifdef CONFIG_DCB
3695 int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3696 #else
3697 static int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3698 #endif /* CONFIG_DCB */
3699 {
3700         int v;
3701
3702         ice_for_each_vsi(pf, v)
3703                 if (pf->vsi[v])
3704                         if (ice_ena_vsi(pf->vsi[v], locked))
3705                                 return -EIO;
3706
3707         return 0;
3708 }
3709
3710 /**
3711  * ice_vsi_rebuild_all - rebuild all VSIs in pf
3712  * @pf: the PF
3713  */
3714 static int ice_vsi_rebuild_all(struct ice_pf *pf)
3715 {
3716         int i;
3717
3718         /* loop through pf->vsi array and reinit the VSI if found */
3719         ice_for_each_vsi(pf, i) {
3720                 int err;
3721
3722                 if (!pf->vsi[i])
3723                         continue;
3724
3725                 err = ice_vsi_rebuild(pf->vsi[i]);
3726                 if (err) {
3727                         dev_err(&pf->pdev->dev,
3728                                 "VSI at index %d rebuild failed\n",
3729                                 pf->vsi[i]->idx);
3730                         return err;
3731                 }
3732
3733                 dev_info(&pf->pdev->dev,
3734                          "VSI at index %d rebuilt. vsi_num = 0x%x\n",
3735                          pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3736         }
3737
3738         return 0;
3739 }
3740
3741 /**
3742  * ice_vsi_replay_all - replay all VSIs configuration in the PF
3743  * @pf: the PF
3744  */
3745 static int ice_vsi_replay_all(struct ice_pf *pf)
3746 {
3747         struct ice_hw *hw = &pf->hw;
3748         enum ice_status ret;
3749         int i;
3750
3751         /* loop through pf->vsi array and replay the VSI if found */
3752         ice_for_each_vsi(pf, i) {
3753                 if (!pf->vsi[i])
3754                         continue;
3755
3756                 ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
3757                 if (ret) {
3758                         dev_err(&pf->pdev->dev,
3759                                 "VSI at index %d replay failed %d\n",
3760                                 pf->vsi[i]->idx, ret);
3761                         return -EIO;
3762                 }
3763
3764                 /* Re-map HW VSI number, using VSI handle that has been
3765                  * previously validated in ice_replay_vsi() call above
3766                  */
3767                 pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
3768
3769                 dev_info(&pf->pdev->dev,
3770                          "VSI at index %d filter replayed successfully - vsi_num %i\n",
3771                          pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3772         }
3773
3774         /* Clean up replay filter after successful re-configuration */
3775         ice_replay_post(hw);
3776         return 0;
3777 }
3778
3779 /**
3780  * ice_rebuild - rebuild after reset
3781  * @pf: pf to rebuild
3782  */
3783 static void ice_rebuild(struct ice_pf *pf)
3784 {
3785         struct device *dev = &pf->pdev->dev;
3786         struct ice_hw *hw = &pf->hw;
3787         enum ice_status ret;
3788         int err, i;
3789
3790         if (test_bit(__ICE_DOWN, pf->state))
3791                 goto clear_recovery;
3792
3793         dev_dbg(dev, "rebuilding pf\n");
3794
3795         ret = ice_init_all_ctrlq(hw);
3796         if (ret) {
3797                 dev_err(dev, "control queues init failed %d\n", ret);
3798                 goto err_init_ctrlq;
3799         }
3800
3801         ret = ice_clear_pf_cfg(hw);
3802         if (ret) {
3803                 dev_err(dev, "clear PF configuration failed %d\n", ret);
3804                 goto err_init_ctrlq;
3805         }
3806
3807         ice_clear_pxe_mode(hw);
3808
3809         ret = ice_get_caps(hw);
3810         if (ret) {
3811                 dev_err(dev, "ice_get_caps failed %d\n", ret);
3812                 goto err_init_ctrlq;
3813         }
3814
3815         err = ice_sched_init_port(hw->port_info);
3816         if (err)
3817                 goto err_sched_init_port;
3818
3819         ice_dcb_rebuild(pf);
3820
3821         err = ice_vsi_rebuild_all(pf);
3822         if (err) {
3823                 dev_err(dev, "ice_vsi_rebuild_all failed\n");
3824                 goto err_vsi_rebuild;
3825         }
3826
3827         err = ice_update_link_info(hw->port_info);
3828         if (err)
3829                 dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
3830
3831         /* Replay all VSIs Configuration, including filters after reset */
3832         if (ice_vsi_replay_all(pf)) {
3833                 dev_err(&pf->pdev->dev,
3834                         "error replaying VSI configurations with switch filter rules\n");
3835                 goto err_vsi_rebuild;
3836         }
3837
3838         /* start misc vector */
3839         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3840                 err = ice_req_irq_msix_misc(pf);
3841                 if (err) {
3842                         dev_err(dev, "misc vector setup failed: %d\n", err);
3843                         goto err_vsi_rebuild;
3844                 }
3845         }
3846
3847         /* restart the VSIs that were rebuilt and running before the reset */
3848         err = ice_pf_ena_all_vsi(pf, false);
3849         if (err) {
3850                 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
3851                 /* no need to disable VSIs in tear down path in ice_rebuild()
3852                  * since its already taken care in ice_vsi_open()
3853                  */
3854                 goto err_vsi_rebuild;
3855         }
3856
3857         ice_for_each_vsi(pf, i) {
3858                 bool link_up;
3859
3860                 if (!pf->vsi[i] || pf->vsi[i]->type != ICE_VSI_PF)
3861                         continue;
3862                 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
3863                 if (link_up) {
3864                         netif_carrier_on(pf->vsi[i]->netdev);
3865                         netif_tx_wake_all_queues(pf->vsi[i]->netdev);
3866                 } else {
3867                         netif_carrier_off(pf->vsi[i]->netdev);
3868                         netif_tx_stop_all_queues(pf->vsi[i]->netdev);
3869                 }
3870         }
3871
3872         /* if we get here, reset flow is successful */
3873         clear_bit(__ICE_RESET_FAILED, pf->state);
3874         return;
3875
3876 err_vsi_rebuild:
3877         ice_vsi_release_all(pf);
3878 err_sched_init_port:
3879         ice_sched_cleanup_all(hw);
3880 err_init_ctrlq:
3881         ice_shutdown_all_ctrlq(hw);
3882         set_bit(__ICE_RESET_FAILED, pf->state);
3883 clear_recovery:
3884         /* set this bit in PF state to control service task scheduling */
3885         set_bit(__ICE_NEEDS_RESTART, pf->state);
3886         dev_err(dev, "Rebuild failed, unload and reload driver\n");
3887 }
3888
3889 /**
3890  * ice_change_mtu - NDO callback to change the MTU
3891  * @netdev: network interface device structure
3892  * @new_mtu: new value for maximum frame size
3893  *
3894  * Returns 0 on success, negative on failure
3895  */
3896 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
3897 {
3898         struct ice_netdev_priv *np = netdev_priv(netdev);
3899         struct ice_vsi *vsi = np->vsi;
3900         struct ice_pf *pf = vsi->back;
3901         u8 count = 0;
3902
3903         if (new_mtu == netdev->mtu) {
3904                 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
3905                 return 0;
3906         }
3907
3908         if (new_mtu < netdev->min_mtu) {
3909                 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
3910                            netdev->min_mtu);
3911                 return -EINVAL;
3912         } else if (new_mtu > netdev->max_mtu) {
3913                 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
3914                            netdev->min_mtu);
3915                 return -EINVAL;
3916         }
3917         /* if a reset is in progress, wait for some time for it to complete */
3918         do {
3919                 if (ice_is_reset_in_progress(pf->state)) {
3920                         count++;
3921                         usleep_range(1000, 2000);
3922                 } else {
3923                         break;
3924                 }
3925
3926         } while (count < 100);
3927
3928         if (count == 100) {
3929                 netdev_err(netdev, "can't change mtu. Device is busy\n");
3930                 return -EBUSY;
3931         }
3932
3933         netdev->mtu = new_mtu;
3934
3935         /* if VSI is up, bring it down and then back up */
3936         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
3937                 int err;
3938
3939                 err = ice_down(vsi);
3940                 if (err) {
3941                         netdev_err(netdev, "change mtu if_up err %d\n", err);
3942                         return err;
3943                 }
3944
3945                 err = ice_up(vsi);
3946                 if (err) {
3947                         netdev_err(netdev, "change mtu if_up err %d\n", err);
3948                         return err;
3949                 }
3950         }
3951
3952         netdev_info(netdev, "changed MTU to %d\n", new_mtu);
3953         return 0;
3954 }
3955
3956 /**
3957  * ice_set_rss - Set RSS keys and lut
3958  * @vsi: Pointer to VSI structure
3959  * @seed: RSS hash seed
3960  * @lut: Lookup table
3961  * @lut_size: Lookup table size
3962  *
3963  * Returns 0 on success, negative on failure
3964  */
3965 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3966 {
3967         struct ice_pf *pf = vsi->back;
3968         struct ice_hw *hw = &pf->hw;
3969         enum ice_status status;
3970
3971         if (seed) {
3972                 struct ice_aqc_get_set_rss_keys *buf =
3973                                   (struct ice_aqc_get_set_rss_keys *)seed;
3974
3975                 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
3976
3977                 if (status) {
3978                         dev_err(&pf->pdev->dev,
3979                                 "Cannot set RSS key, err %d aq_err %d\n",
3980                                 status, hw->adminq.rq_last_status);
3981                         return -EIO;
3982                 }
3983         }
3984
3985         if (lut) {
3986                 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
3987                                             lut, lut_size);
3988                 if (status) {
3989                         dev_err(&pf->pdev->dev,
3990                                 "Cannot set RSS lut, err %d aq_err %d\n",
3991                                 status, hw->adminq.rq_last_status);
3992                         return -EIO;
3993                 }
3994         }
3995
3996         return 0;
3997 }
3998
3999 /**
4000  * ice_get_rss - Get RSS keys and lut
4001  * @vsi: Pointer to VSI structure
4002  * @seed: Buffer to store the keys
4003  * @lut: Buffer to store the lookup table entries
4004  * @lut_size: Size of buffer to store the lookup table entries
4005  *
4006  * Returns 0 on success, negative on failure
4007  */
4008 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4009 {
4010         struct ice_pf *pf = vsi->back;
4011         struct ice_hw *hw = &pf->hw;
4012         enum ice_status status;
4013
4014         if (seed) {
4015                 struct ice_aqc_get_set_rss_keys *buf =
4016                                   (struct ice_aqc_get_set_rss_keys *)seed;
4017
4018                 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
4019                 if (status) {
4020                         dev_err(&pf->pdev->dev,
4021                                 "Cannot get RSS key, err %d aq_err %d\n",
4022                                 status, hw->adminq.rq_last_status);
4023                         return -EIO;
4024                 }
4025         }
4026
4027         if (lut) {
4028                 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4029                                             lut, lut_size);
4030                 if (status) {
4031                         dev_err(&pf->pdev->dev,
4032                                 "Cannot get RSS lut, err %d aq_err %d\n",
4033                                 status, hw->adminq.rq_last_status);
4034                         return -EIO;
4035                 }
4036         }
4037
4038         return 0;
4039 }
4040
4041 /**
4042  * ice_bridge_getlink - Get the hardware bridge mode
4043  * @skb: skb buff
4044  * @pid: process ID
4045  * @seq: RTNL message seq
4046  * @dev: the netdev being configured
4047  * @filter_mask: filter mask passed in
4048  * @nlflags: netlink flags passed in
4049  *
4050  * Return the bridge mode (VEB/VEPA)
4051  */
4052 static int
4053 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4054                    struct net_device *dev, u32 filter_mask, int nlflags)
4055 {
4056         struct ice_netdev_priv *np = netdev_priv(dev);
4057         struct ice_vsi *vsi = np->vsi;
4058         struct ice_pf *pf = vsi->back;
4059         u16 bmode;
4060
4061         bmode = pf->first_sw->bridge_mode;
4062
4063         return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
4064                                        filter_mask, NULL);
4065 }
4066
4067 /**
4068  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
4069  * @vsi: Pointer to VSI structure
4070  * @bmode: Hardware bridge mode (VEB/VEPA)
4071  *
4072  * Returns 0 on success, negative on failure
4073  */
4074 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
4075 {
4076         struct device *dev = &vsi->back->pdev->dev;
4077         struct ice_aqc_vsi_props *vsi_props;
4078         struct ice_hw *hw = &vsi->back->hw;
4079         struct ice_vsi_ctx *ctxt;
4080         enum ice_status status;
4081         int ret = 0;
4082
4083         vsi_props = &vsi->info;
4084
4085         ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
4086         if (!ctxt)
4087                 return -ENOMEM;
4088
4089         ctxt->info = vsi->info;
4090
4091         if (bmode == BRIDGE_MODE_VEB)
4092                 /* change from VEPA to VEB mode */
4093                 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4094         else
4095                 /* change from VEB to VEPA mode */
4096                 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4097         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4098
4099         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4100         if (status) {
4101                 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
4102                         bmode, status, hw->adminq.sq_last_status);
4103                 ret = -EIO;
4104                 goto out;
4105         }
4106         /* Update sw flags for book keeping */
4107         vsi_props->sw_flags = ctxt->info.sw_flags;
4108
4109 out:
4110         devm_kfree(dev, ctxt);
4111         return ret;
4112 }
4113
4114 /**
4115  * ice_bridge_setlink - Set the hardware bridge mode
4116  * @dev: the netdev being configured
4117  * @nlh: RTNL message
4118  * @flags: bridge setlink flags
4119  * @extack: netlink extended ack
4120  *
4121  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
4122  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
4123  * not already set for all VSIs connected to this switch. And also update the
4124  * unicast switch filter rules for the corresponding switch of the netdev.
4125  */
4126 static int
4127 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4128                    u16 __always_unused flags,
4129                    struct netlink_ext_ack __always_unused *extack)
4130 {
4131         struct ice_netdev_priv *np = netdev_priv(dev);
4132         struct ice_pf *pf = np->vsi->back;
4133         struct nlattr *attr, *br_spec;
4134         struct ice_hw *hw = &pf->hw;
4135         enum ice_status status;
4136         struct ice_sw *pf_sw;
4137         int rem, v, err = 0;
4138
4139         pf_sw = pf->first_sw;
4140         /* find the attribute in the netlink message */
4141         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4142
4143         nla_for_each_nested(attr, br_spec, rem) {
4144                 __u16 mode;
4145
4146                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4147                         continue;
4148                 mode = nla_get_u16(attr);
4149                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4150                         return -EINVAL;
4151                 /* Continue  if bridge mode is not being flipped */
4152                 if (mode == pf_sw->bridge_mode)
4153                         continue;
4154                 /* Iterates through the PF VSI list and update the loopback
4155                  * mode of the VSI
4156                  */
4157                 ice_for_each_vsi(pf, v) {
4158                         if (!pf->vsi[v])
4159                                 continue;
4160                         err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
4161                         if (err)
4162                                 return err;
4163                 }
4164
4165                 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
4166                 /* Update the unicast switch filter rules for the corresponding
4167                  * switch of the netdev
4168                  */
4169                 status = ice_update_sw_rule_bridge_mode(hw);
4170                 if (status) {
4171                         netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n",
4172                                    mode, status, hw->adminq.sq_last_status);
4173                         /* revert hw->evb_veb */
4174                         hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
4175                         return -EIO;
4176                 }
4177
4178                 pf_sw->bridge_mode = mode;
4179         }
4180
4181         return 0;
4182 }
4183
4184 /**
4185  * ice_tx_timeout - Respond to a Tx Hang
4186  * @netdev: network interface device structure
4187  */
4188 static void ice_tx_timeout(struct net_device *netdev)
4189 {
4190         struct ice_netdev_priv *np = netdev_priv(netdev);
4191         struct ice_ring *tx_ring = NULL;
4192         struct ice_vsi *vsi = np->vsi;
4193         struct ice_pf *pf = vsi->back;
4194         int hung_queue = -1;
4195         u32 i;
4196
4197         pf->tx_timeout_count++;
4198
4199         /* find the stopped queue the same way dev_watchdog() does */
4200         for (i = 0; i < netdev->num_tx_queues; i++) {
4201                 unsigned long trans_start;
4202                 struct netdev_queue *q;
4203
4204                 q = netdev_get_tx_queue(netdev, i);
4205                 trans_start = q->trans_start;
4206                 if (netif_xmit_stopped(q) &&
4207                     time_after(jiffies,
4208                                trans_start + netdev->watchdog_timeo)) {
4209                         hung_queue = i;
4210                         break;
4211                 }
4212         }
4213
4214         if (i == netdev->num_tx_queues)
4215                 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
4216         else
4217                 /* now that we have an index, find the tx_ring struct */
4218                 for (i = 0; i < vsi->num_txq; i++)
4219                         if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4220                                 if (hung_queue == vsi->tx_rings[i]->q_index) {
4221                                         tx_ring = vsi->tx_rings[i];
4222                                         break;
4223                                 }
4224
4225         /* Reset recovery level if enough time has elapsed after last timeout.
4226          * Also ensure no new reset action happens before next timeout period.
4227          */
4228         if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
4229                 pf->tx_timeout_recovery_level = 1;
4230         else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
4231                                        netdev->watchdog_timeo)))
4232                 return;
4233
4234         if (tx_ring) {
4235                 struct ice_hw *hw = &pf->hw;
4236                 u32 head, val = 0;
4237
4238                 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[hung_queue])) &
4239                         QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
4240                 /* Read interrupt register */
4241                 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4242                         val = rd32(hw,
4243                                    GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
4244
4245                 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
4246                             vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
4247                             head, tx_ring->next_to_use, val);
4248         }
4249
4250         pf->tx_timeout_last_recovery = jiffies;
4251         netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
4252                     pf->tx_timeout_recovery_level, hung_queue);
4253
4254         switch (pf->tx_timeout_recovery_level) {
4255         case 1:
4256                 set_bit(__ICE_PFR_REQ, pf->state);
4257                 break;
4258         case 2:
4259                 set_bit(__ICE_CORER_REQ, pf->state);
4260                 break;
4261         case 3:
4262                 set_bit(__ICE_GLOBR_REQ, pf->state);
4263                 break;
4264         default:
4265                 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
4266                 set_bit(__ICE_DOWN, pf->state);
4267                 set_bit(__ICE_NEEDS_RESTART, vsi->state);
4268                 set_bit(__ICE_SERVICE_DIS, pf->state);
4269                 break;
4270         }
4271
4272         ice_service_task_schedule(pf);
4273         pf->tx_timeout_recovery_level++;
4274 }
4275
4276 /**
4277  * ice_open - Called when a network interface becomes active
4278  * @netdev: network interface device structure
4279  *
4280  * The open entry point is called when a network interface is made
4281  * active by the system (IFF_UP). At this point all resources needed
4282  * for transmit and receive operations are allocated, the interrupt
4283  * handler is registered with the OS, the netdev watchdog is enabled,
4284  * and the stack is notified that the interface is ready.
4285  *
4286  * Returns 0 on success, negative value on failure
4287  */
4288 int ice_open(struct net_device *netdev)
4289 {
4290         struct ice_netdev_priv *np = netdev_priv(netdev);
4291         struct ice_vsi *vsi = np->vsi;
4292         int err;
4293
4294         if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
4295                 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
4296                 return -EIO;
4297         }
4298
4299         netif_carrier_off(netdev);
4300
4301         err = ice_force_phys_link_state(vsi, true);
4302         if (err) {
4303                 netdev_err(netdev,
4304                            "Failed to set physical link up, error %d\n", err);
4305                 return err;
4306         }
4307
4308         err = ice_vsi_open(vsi);
4309         if (err)
4310                 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
4311                            vsi->vsi_num, vsi->vsw->sw_id);
4312         return err;
4313 }
4314
4315 /**
4316  * ice_stop - Disables a network interface
4317  * @netdev: network interface device structure
4318  *
4319  * The stop entry point is called when an interface is de-activated by the OS,
4320  * and the netdevice enters the DOWN state. The hardware is still under the
4321  * driver's control, but the netdev interface is disabled.
4322  *
4323  * Returns success only - not allowed to fail
4324  */
4325 int ice_stop(struct net_device *netdev)
4326 {
4327         struct ice_netdev_priv *np = netdev_priv(netdev);
4328         struct ice_vsi *vsi = np->vsi;
4329
4330         ice_vsi_close(vsi);
4331
4332         return 0;
4333 }
4334
4335 /**
4336  * ice_features_check - Validate encapsulated packet conforms to limits
4337  * @skb: skb buffer
4338  * @netdev: This port's netdev
4339  * @features: Offload features that the stack believes apply
4340  */
4341 static netdev_features_t
4342 ice_features_check(struct sk_buff *skb,
4343                    struct net_device __always_unused *netdev,
4344                    netdev_features_t features)
4345 {
4346         size_t len;
4347
4348         /* No point in doing any of this if neither checksum nor GSO are
4349          * being requested for this frame. We can rule out both by just
4350          * checking for CHECKSUM_PARTIAL
4351          */
4352         if (skb->ip_summed != CHECKSUM_PARTIAL)
4353                 return features;
4354
4355         /* We cannot support GSO if the MSS is going to be less than
4356          * 64 bytes. If it is then we need to drop support for GSO.
4357          */
4358         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4359                 features &= ~NETIF_F_GSO_MASK;
4360
4361         len = skb_network_header(skb) - skb->data;
4362         if (len & ~(ICE_TXD_MACLEN_MAX))
4363                 goto out_rm_features;
4364
4365         len = skb_transport_header(skb) - skb_network_header(skb);
4366         if (len & ~(ICE_TXD_IPLEN_MAX))
4367                 goto out_rm_features;
4368
4369         if (skb->encapsulation) {
4370                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4371                 if (len & ~(ICE_TXD_L4LEN_MAX))
4372                         goto out_rm_features;
4373
4374                 len = skb_inner_transport_header(skb) -
4375                       skb_inner_network_header(skb);
4376                 if (len & ~(ICE_TXD_IPLEN_MAX))
4377                         goto out_rm_features;
4378         }
4379
4380         return features;
4381 out_rm_features:
4382         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4383 }
4384
4385 static const struct net_device_ops ice_netdev_ops = {
4386         .ndo_open = ice_open,
4387         .ndo_stop = ice_stop,
4388         .ndo_start_xmit = ice_start_xmit,
4389         .ndo_features_check = ice_features_check,
4390         .ndo_set_rx_mode = ice_set_rx_mode,
4391         .ndo_set_mac_address = ice_set_mac_address,
4392         .ndo_validate_addr = eth_validate_addr,
4393         .ndo_change_mtu = ice_change_mtu,
4394         .ndo_get_stats64 = ice_get_stats64,
4395         .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
4396         .ndo_set_vf_mac = ice_set_vf_mac,
4397         .ndo_get_vf_config = ice_get_vf_cfg,
4398         .ndo_set_vf_trust = ice_set_vf_trust,
4399         .ndo_set_vf_vlan = ice_set_vf_port_vlan,
4400         .ndo_set_vf_link_state = ice_set_vf_link_state,
4401         .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
4402         .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
4403         .ndo_set_features = ice_set_features,
4404         .ndo_bridge_getlink = ice_bridge_getlink,
4405         .ndo_bridge_setlink = ice_bridge_setlink,
4406         .ndo_fdb_add = ice_fdb_add,
4407         .ndo_fdb_del = ice_fdb_del,
4408         .ndo_tx_timeout = ice_tx_timeout,
4409 };