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