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