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