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