]> Git Repo - linux.git/blob - drivers/net/ethernet/intel/ice/ice_main.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux.git] / drivers / net / ethernet / intel / ice / ice_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <generated/utsrelease.h>
9 #include "ice.h"
10 #include "ice_base.h"
11 #include "ice_lib.h"
12 #include "ice_fltr.h"
13 #include "ice_dcb_lib.h"
14 #include "ice_dcb_nl.h"
15 #include "ice_devlink.h"
16
17 #define DRV_SUMMARY     "Intel(R) Ethernet Connection E800 Series Linux Driver"
18 static const char ice_driver_string[] = DRV_SUMMARY;
19 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
20
21 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
22 #define ICE_DDP_PKG_PATH        "intel/ice/ddp/"
23 #define ICE_DDP_PKG_FILE        ICE_DDP_PKG_PATH "ice.pkg"
24
25 MODULE_AUTHOR("Intel Corporation, <[email protected]>");
26 MODULE_DESCRIPTION(DRV_SUMMARY);
27 MODULE_LICENSE("GPL v2");
28 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
29
30 static int debug = -1;
31 module_param(debug, int, 0644);
32 #ifndef CONFIG_DYNAMIC_DEBUG
33 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
34 #else
35 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
36 #endif /* !CONFIG_DYNAMIC_DEBUG */
37
38 static struct workqueue_struct *ice_wq;
39 static const struct net_device_ops ice_netdev_safe_mode_ops;
40 static const struct net_device_ops ice_netdev_ops;
41 static int ice_vsi_open(struct ice_vsi *vsi);
42
43 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
44
45 static void ice_vsi_release_all(struct ice_pf *pf);
46
47 /**
48  * ice_get_tx_pending - returns number of Tx descriptors not processed
49  * @ring: the ring of descriptors
50  */
51 static u16 ice_get_tx_pending(struct ice_ring *ring)
52 {
53         u16 head, tail;
54
55         head = ring->next_to_clean;
56         tail = ring->next_to_use;
57
58         if (head != tail)
59                 return (head < tail) ?
60                         tail - head : (tail + ring->count - head);
61         return 0;
62 }
63
64 /**
65  * ice_check_for_hang_subtask - check for and recover hung queues
66  * @pf: pointer to PF struct
67  */
68 static void ice_check_for_hang_subtask(struct ice_pf *pf)
69 {
70         struct ice_vsi *vsi = NULL;
71         struct ice_hw *hw;
72         unsigned int i;
73         int packets;
74         u32 v;
75
76         ice_for_each_vsi(pf, v)
77                 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
78                         vsi = pf->vsi[v];
79                         break;
80                 }
81
82         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
83                 return;
84
85         if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
86                 return;
87
88         hw = &vsi->back->hw;
89
90         for (i = 0; i < vsi->num_txq; i++) {
91                 struct ice_ring *tx_ring = vsi->tx_rings[i];
92
93                 if (tx_ring && tx_ring->desc) {
94                         /* If packet counter has not changed the queue is
95                          * likely stalled, so force an interrupt for this
96                          * queue.
97                          *
98                          * prev_pkt would be negative if there was no
99                          * pending work.
100                          */
101                         packets = tx_ring->stats.pkts & INT_MAX;
102                         if (tx_ring->tx_stats.prev_pkt == packets) {
103                                 /* Trigger sw interrupt to revive the queue */
104                                 ice_trigger_sw_intr(hw, tx_ring->q_vector);
105                                 continue;
106                         }
107
108                         /* Memory barrier between read of packet count and call
109                          * to ice_get_tx_pending()
110                          */
111                         smp_rmb();
112                         tx_ring->tx_stats.prev_pkt =
113                             ice_get_tx_pending(tx_ring) ? packets : -1;
114                 }
115         }
116 }
117
118 /**
119  * ice_init_mac_fltr - Set initial MAC filters
120  * @pf: board private structure
121  *
122  * Set initial set of MAC filters for PF VSI; configure filters for permanent
123  * address and broadcast address. If an error is encountered, netdevice will be
124  * unregistered.
125  */
126 static int ice_init_mac_fltr(struct ice_pf *pf)
127 {
128         enum ice_status status;
129         struct ice_vsi *vsi;
130         u8 *perm_addr;
131
132         vsi = ice_get_main_vsi(pf);
133         if (!vsi)
134                 return -EINVAL;
135
136         perm_addr = vsi->port_info->mac.perm_addr;
137         status = ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
138         if (!status)
139                 return 0;
140
141         /* We aren't useful with no MAC filters, so unregister if we
142          * had an error
143          */
144         if (vsi->netdev->reg_state == NETREG_REGISTERED) {
145                 dev_err(ice_pf_to_dev(pf), "Could not add MAC filters error %s. Unregistering device\n",
146                         ice_stat_str(status));
147                 unregister_netdev(vsi->netdev);
148                 free_netdev(vsi->netdev);
149                 vsi->netdev = NULL;
150         }
151
152         return -EIO;
153 }
154
155 /**
156  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
157  * @netdev: the net device on which the sync is happening
158  * @addr: MAC address to sync
159  *
160  * This is a callback function which is called by the in kernel device sync
161  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
162  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
163  * MAC filters from the hardware.
164  */
165 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
166 {
167         struct ice_netdev_priv *np = netdev_priv(netdev);
168         struct ice_vsi *vsi = np->vsi;
169
170         if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
171                                      ICE_FWD_TO_VSI))
172                 return -EINVAL;
173
174         return 0;
175 }
176
177 /**
178  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
179  * @netdev: the net device on which the unsync is happening
180  * @addr: MAC address to unsync
181  *
182  * This is a callback function which is called by the in kernel device unsync
183  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
184  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
185  * delete the MAC filters from the hardware.
186  */
187 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
188 {
189         struct ice_netdev_priv *np = netdev_priv(netdev);
190         struct ice_vsi *vsi = np->vsi;
191
192         if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
193                                      ICE_FWD_TO_VSI))
194                 return -EINVAL;
195
196         return 0;
197 }
198
199 /**
200  * ice_vsi_fltr_changed - check if filter state changed
201  * @vsi: VSI to be checked
202  *
203  * returns true if filter state has changed, false otherwise.
204  */
205 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
206 {
207         return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
208                test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
209                test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
210 }
211
212 /**
213  * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
214  * @vsi: the VSI being configured
215  * @promisc_m: mask of promiscuous config bits
216  * @set_promisc: enable or disable promisc flag request
217  *
218  */
219 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
220 {
221         struct ice_hw *hw = &vsi->back->hw;
222         enum ice_status status = 0;
223
224         if (vsi->type != ICE_VSI_PF)
225                 return 0;
226
227         if (vsi->num_vlan > 1) {
228                 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
229                                                   set_promisc);
230         } else {
231                 if (set_promisc)
232                         status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
233                                                      0);
234                 else
235                         status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
236                                                        0);
237         }
238
239         if (status)
240                 return -EIO;
241
242         return 0;
243 }
244
245 /**
246  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
247  * @vsi: ptr to the VSI
248  *
249  * Push any outstanding VSI filter changes through the AdminQ.
250  */
251 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
252 {
253         struct device *dev = ice_pf_to_dev(vsi->back);
254         struct net_device *netdev = vsi->netdev;
255         bool promisc_forced_on = false;
256         struct ice_pf *pf = vsi->back;
257         struct ice_hw *hw = &pf->hw;
258         enum ice_status status = 0;
259         u32 changed_flags = 0;
260         u8 promisc_m;
261         int err = 0;
262
263         if (!vsi->netdev)
264                 return -EINVAL;
265
266         while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
267                 usleep_range(1000, 2000);
268
269         changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
270         vsi->current_netdev_flags = vsi->netdev->flags;
271
272         INIT_LIST_HEAD(&vsi->tmp_sync_list);
273         INIT_LIST_HEAD(&vsi->tmp_unsync_list);
274
275         if (ice_vsi_fltr_changed(vsi)) {
276                 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
277                 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
278                 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
279
280                 /* grab the netdev's addr_list_lock */
281                 netif_addr_lock_bh(netdev);
282                 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
283                               ice_add_mac_to_unsync_list);
284                 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
285                               ice_add_mac_to_unsync_list);
286                 /* our temp lists are populated. release lock */
287                 netif_addr_unlock_bh(netdev);
288         }
289
290         /* Remove MAC addresses in the unsync list */
291         status = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
292         ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
293         if (status) {
294                 netdev_err(netdev, "Failed to delete MAC filters\n");
295                 /* if we failed because of alloc failures, just bail */
296                 if (status == ICE_ERR_NO_MEMORY) {
297                         err = -ENOMEM;
298                         goto out;
299                 }
300         }
301
302         /* Add MAC addresses in the sync list */
303         status = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
304         ice_fltr_free_list(dev, &vsi->tmp_sync_list);
305         /* If filter is added successfully or already exists, do not go into
306          * 'if' condition and report it as error. Instead continue processing
307          * rest of the function.
308          */
309         if (status && status != ICE_ERR_ALREADY_EXISTS) {
310                 netdev_err(netdev, "Failed to add MAC filters\n");
311                 /* If there is no more space for new umac filters, VSI
312                  * should go into promiscuous mode. There should be some
313                  * space reserved for promiscuous filters.
314                  */
315                 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
316                     !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
317                                       vsi->state)) {
318                         promisc_forced_on = true;
319                         netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
320                                     vsi->vsi_num);
321                 } else {
322                         err = -EIO;
323                         goto out;
324                 }
325         }
326         /* check for changes in promiscuous modes */
327         if (changed_flags & IFF_ALLMULTI) {
328                 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
329                         if (vsi->num_vlan > 1)
330                                 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
331                         else
332                                 promisc_m = ICE_MCAST_PROMISC_BITS;
333
334                         err = ice_cfg_promisc(vsi, promisc_m, true);
335                         if (err) {
336                                 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
337                                            vsi->vsi_num);
338                                 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
339                                 goto out_promisc;
340                         }
341                 } else {
342                         /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
343                         if (vsi->num_vlan > 1)
344                                 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
345                         else
346                                 promisc_m = ICE_MCAST_PROMISC_BITS;
347
348                         err = ice_cfg_promisc(vsi, promisc_m, false);
349                         if (err) {
350                                 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
351                                            vsi->vsi_num);
352                                 vsi->current_netdev_flags |= IFF_ALLMULTI;
353                                 goto out_promisc;
354                         }
355                 }
356         }
357
358         if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
359             test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
360                 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
361                 if (vsi->current_netdev_flags & IFF_PROMISC) {
362                         /* Apply Rx filter rule to get traffic from wire */
363                         if (!ice_is_dflt_vsi_in_use(pf->first_sw)) {
364                                 err = ice_set_dflt_vsi(pf->first_sw, vsi);
365                                 if (err && err != -EEXIST) {
366                                         netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
367                                                    err, vsi->vsi_num);
368                                         vsi->current_netdev_flags &=
369                                                 ~IFF_PROMISC;
370                                         goto out_promisc;
371                                 }
372                                 ice_cfg_vlan_pruning(vsi, false, false);
373                         }
374                 } else {
375                         /* Clear Rx filter to remove traffic from wire */
376                         if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) {
377                                 err = ice_clear_dflt_vsi(pf->first_sw);
378                                 if (err) {
379                                         netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
380                                                    err, vsi->vsi_num);
381                                         vsi->current_netdev_flags |=
382                                                 IFF_PROMISC;
383                                         goto out_promisc;
384                                 }
385                                 if (vsi->num_vlan > 1)
386                                         ice_cfg_vlan_pruning(vsi, true, false);
387                         }
388                 }
389         }
390         goto exit;
391
392 out_promisc:
393         set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
394         goto exit;
395 out:
396         /* if something went wrong then set the changed flag so we try again */
397         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
398         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
399 exit:
400         clear_bit(__ICE_CFG_BUSY, vsi->state);
401         return err;
402 }
403
404 /**
405  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
406  * @pf: board private structure
407  */
408 static void ice_sync_fltr_subtask(struct ice_pf *pf)
409 {
410         int v;
411
412         if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
413                 return;
414
415         clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
416
417         ice_for_each_vsi(pf, v)
418                 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
419                     ice_vsi_sync_fltr(pf->vsi[v])) {
420                         /* come back and try again later */
421                         set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
422                         break;
423                 }
424 }
425
426 /**
427  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
428  * @pf: the PF
429  * @locked: is the rtnl_lock already held
430  */
431 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
432 {
433         int v;
434
435         ice_for_each_vsi(pf, v)
436                 if (pf->vsi[v])
437                         ice_dis_vsi(pf->vsi[v], locked);
438 }
439
440 /**
441  * ice_prepare_for_reset - prep for the core to reset
442  * @pf: board private structure
443  *
444  * Inform or close all dependent features in prep for reset.
445  */
446 static void
447 ice_prepare_for_reset(struct ice_pf *pf)
448 {
449         struct ice_hw *hw = &pf->hw;
450         unsigned int i;
451
452         /* already prepared for reset */
453         if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
454                 return;
455
456         /* Notify VFs of impending reset */
457         if (ice_check_sq_alive(hw, &hw->mailboxq))
458                 ice_vc_notify_reset(pf);
459
460         /* Disable VFs until reset is completed */
461         ice_for_each_vf(pf, i)
462                 ice_set_vf_state_qs_dis(&pf->vf[i]);
463
464         /* clear SW filtering DB */
465         ice_clear_hw_tbls(hw);
466         /* disable the VSIs and their queues that are not already DOWN */
467         ice_pf_dis_all_vsi(pf, false);
468
469         if (hw->port_info)
470                 ice_sched_clear_port(hw->port_info);
471
472         ice_shutdown_all_ctrlq(hw);
473
474         set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
475 }
476
477 /**
478  * ice_do_reset - Initiate one of many types of resets
479  * @pf: board private structure
480  * @reset_type: reset type requested
481  * before this function was called.
482  */
483 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
484 {
485         struct device *dev = ice_pf_to_dev(pf);
486         struct ice_hw *hw = &pf->hw;
487
488         dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
489
490         ice_prepare_for_reset(pf);
491
492         /* trigger the reset */
493         if (ice_reset(hw, reset_type)) {
494                 dev_err(dev, "reset %d failed\n", reset_type);
495                 set_bit(__ICE_RESET_FAILED, pf->state);
496                 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
497                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
498                 clear_bit(__ICE_PFR_REQ, pf->state);
499                 clear_bit(__ICE_CORER_REQ, pf->state);
500                 clear_bit(__ICE_GLOBR_REQ, pf->state);
501                 return;
502         }
503
504         /* PFR is a bit of a special case because it doesn't result in an OICR
505          * interrupt. So for PFR, rebuild after the reset and clear the reset-
506          * associated state bits.
507          */
508         if (reset_type == ICE_RESET_PFR) {
509                 pf->pfr_count++;
510                 ice_rebuild(pf, reset_type);
511                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
512                 clear_bit(__ICE_PFR_REQ, pf->state);
513                 ice_reset_all_vfs(pf, true);
514         }
515 }
516
517 /**
518  * ice_reset_subtask - Set up for resetting the device and driver
519  * @pf: board private structure
520  */
521 static void ice_reset_subtask(struct ice_pf *pf)
522 {
523         enum ice_reset_req reset_type = ICE_RESET_INVAL;
524
525         /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
526          * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
527          * of reset is pending and sets bits in pf->state indicating the reset
528          * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
529          * prepare for pending reset if not already (for PF software-initiated
530          * global resets the software should already be prepared for it as
531          * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
532          * by firmware or software on other PFs, that bit is not set so prepare
533          * for the reset now), poll for reset done, rebuild and return.
534          */
535         if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
536                 /* Perform the largest reset requested */
537                 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
538                         reset_type = ICE_RESET_CORER;
539                 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
540                         reset_type = ICE_RESET_GLOBR;
541                 if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state))
542                         reset_type = ICE_RESET_EMPR;
543                 /* return if no valid reset type requested */
544                 if (reset_type == ICE_RESET_INVAL)
545                         return;
546                 ice_prepare_for_reset(pf);
547
548                 /* make sure we are ready to rebuild */
549                 if (ice_check_reset(&pf->hw)) {
550                         set_bit(__ICE_RESET_FAILED, pf->state);
551                 } else {
552                         /* done with reset. start rebuild */
553                         pf->hw.reset_ongoing = false;
554                         ice_rebuild(pf, reset_type);
555                         /* clear bit to resume normal operations, but
556                          * ICE_NEEDS_RESTART bit is set in case rebuild failed
557                          */
558                         clear_bit(__ICE_RESET_OICR_RECV, pf->state);
559                         clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
560                         clear_bit(__ICE_PFR_REQ, pf->state);
561                         clear_bit(__ICE_CORER_REQ, pf->state);
562                         clear_bit(__ICE_GLOBR_REQ, pf->state);
563                         ice_reset_all_vfs(pf, true);
564                 }
565
566                 return;
567         }
568
569         /* No pending resets to finish processing. Check for new resets */
570         if (test_bit(__ICE_PFR_REQ, pf->state))
571                 reset_type = ICE_RESET_PFR;
572         if (test_bit(__ICE_CORER_REQ, pf->state))
573                 reset_type = ICE_RESET_CORER;
574         if (test_bit(__ICE_GLOBR_REQ, pf->state))
575                 reset_type = ICE_RESET_GLOBR;
576         /* If no valid reset type requested just return */
577         if (reset_type == ICE_RESET_INVAL)
578                 return;
579
580         /* reset if not already down or busy */
581         if (!test_bit(__ICE_DOWN, pf->state) &&
582             !test_bit(__ICE_CFG_BUSY, pf->state)) {
583                 ice_do_reset(pf, reset_type);
584         }
585 }
586
587 /**
588  * ice_print_topo_conflict - print topology conflict message
589  * @vsi: the VSI whose topology status is being checked
590  */
591 static void ice_print_topo_conflict(struct ice_vsi *vsi)
592 {
593         switch (vsi->port_info->phy.link_info.topo_media_conflict) {
594         case ICE_AQ_LINK_TOPO_CONFLICT:
595         case ICE_AQ_LINK_MEDIA_CONFLICT:
596         case ICE_AQ_LINK_TOPO_UNREACH_PRT:
597         case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
598         case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
599                 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");
600                 break;
601         case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
602                 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");
603                 break;
604         default:
605                 break;
606         }
607 }
608
609 /**
610  * ice_print_link_msg - print link up or down message
611  * @vsi: the VSI whose link status is being queried
612  * @isup: boolean for if the link is now up or down
613  */
614 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
615 {
616         struct ice_aqc_get_phy_caps_data *caps;
617         const char *an_advertised;
618         enum ice_status status;
619         const char *fec_req;
620         const char *speed;
621         const char *fec;
622         const char *fc;
623         const char *an;
624
625         if (!vsi)
626                 return;
627
628         if (vsi->current_isup == isup)
629                 return;
630
631         vsi->current_isup = isup;
632
633         if (!isup) {
634                 netdev_info(vsi->netdev, "NIC Link is Down\n");
635                 return;
636         }
637
638         switch (vsi->port_info->phy.link_info.link_speed) {
639         case ICE_AQ_LINK_SPEED_100GB:
640                 speed = "100 G";
641                 break;
642         case ICE_AQ_LINK_SPEED_50GB:
643                 speed = "50 G";
644                 break;
645         case ICE_AQ_LINK_SPEED_40GB:
646                 speed = "40 G";
647                 break;
648         case ICE_AQ_LINK_SPEED_25GB:
649                 speed = "25 G";
650                 break;
651         case ICE_AQ_LINK_SPEED_20GB:
652                 speed = "20 G";
653                 break;
654         case ICE_AQ_LINK_SPEED_10GB:
655                 speed = "10 G";
656                 break;
657         case ICE_AQ_LINK_SPEED_5GB:
658                 speed = "5 G";
659                 break;
660         case ICE_AQ_LINK_SPEED_2500MB:
661                 speed = "2.5 G";
662                 break;
663         case ICE_AQ_LINK_SPEED_1000MB:
664                 speed = "1 G";
665                 break;
666         case ICE_AQ_LINK_SPEED_100MB:
667                 speed = "100 M";
668                 break;
669         default:
670                 speed = "Unknown ";
671                 break;
672         }
673
674         switch (vsi->port_info->fc.current_mode) {
675         case ICE_FC_FULL:
676                 fc = "Rx/Tx";
677                 break;
678         case ICE_FC_TX_PAUSE:
679                 fc = "Tx";
680                 break;
681         case ICE_FC_RX_PAUSE:
682                 fc = "Rx";
683                 break;
684         case ICE_FC_NONE:
685                 fc = "None";
686                 break;
687         default:
688                 fc = "Unknown";
689                 break;
690         }
691
692         /* Get FEC mode based on negotiated link info */
693         switch (vsi->port_info->phy.link_info.fec_info) {
694         case ICE_AQ_LINK_25G_RS_528_FEC_EN:
695         case ICE_AQ_LINK_25G_RS_544_FEC_EN:
696                 fec = "RS-FEC";
697                 break;
698         case ICE_AQ_LINK_25G_KR_FEC_EN:
699                 fec = "FC-FEC/BASE-R";
700                 break;
701         default:
702                 fec = "NONE";
703                 break;
704         }
705
706         /* check if autoneg completed, might be false due to not supported */
707         if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
708                 an = "True";
709         else
710                 an = "False";
711
712         /* Get FEC mode requested based on PHY caps last SW configuration */
713         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
714         if (!caps) {
715                 fec_req = "Unknown";
716                 an_advertised = "Unknown";
717                 goto done;
718         }
719
720         status = ice_aq_get_phy_caps(vsi->port_info, false,
721                                      ICE_AQC_REPORT_SW_CFG, caps, NULL);
722         if (status)
723                 netdev_info(vsi->netdev, "Get phy capability failed.\n");
724
725         an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
726
727         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
728             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
729                 fec_req = "RS-FEC";
730         else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
731                  caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
732                 fec_req = "FC-FEC/BASE-R";
733         else
734                 fec_req = "NONE";
735
736         kfree(caps);
737
738 done:
739         netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
740                     speed, fec_req, fec, an_advertised, an, fc);
741         ice_print_topo_conflict(vsi);
742 }
743
744 /**
745  * ice_vsi_link_event - update the VSI's netdev
746  * @vsi: the VSI on which the link event occurred
747  * @link_up: whether or not the VSI needs to be set up or down
748  */
749 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
750 {
751         if (!vsi)
752                 return;
753
754         if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
755                 return;
756
757         if (vsi->type == ICE_VSI_PF) {
758                 if (link_up == netif_carrier_ok(vsi->netdev))
759                         return;
760
761                 if (link_up) {
762                         netif_carrier_on(vsi->netdev);
763                         netif_tx_wake_all_queues(vsi->netdev);
764                 } else {
765                         netif_carrier_off(vsi->netdev);
766                         netif_tx_stop_all_queues(vsi->netdev);
767                 }
768         }
769 }
770
771 /**
772  * ice_set_dflt_mib - send a default config MIB to the FW
773  * @pf: private PF struct
774  *
775  * This function sends a default configuration MIB to the FW.
776  *
777  * If this function errors out at any point, the driver is still able to
778  * function.  The main impact is that LFC may not operate as expected.
779  * Therefore an error state in this function should be treated with a DBG
780  * message and continue on with driver rebuild/reenable.
781  */
782 static void ice_set_dflt_mib(struct ice_pf *pf)
783 {
784         struct device *dev = ice_pf_to_dev(pf);
785         u8 mib_type, *buf, *lldpmib = NULL;
786         u16 len, typelen, offset = 0;
787         struct ice_lldp_org_tlv *tlv;
788         struct ice_hw *hw;
789         u32 ouisubtype;
790
791         if (!pf) {
792                 dev_dbg(dev, "%s NULL pf pointer\n", __func__);
793                 return;
794         }
795
796         hw = &pf->hw;
797         mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
798         lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
799         if (!lldpmib) {
800                 dev_dbg(dev, "%s Failed to allocate MIB memory\n",
801                         __func__);
802                 return;
803         }
804
805         /* Add ETS CFG TLV */
806         tlv = (struct ice_lldp_org_tlv *)lldpmib;
807         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
808                    ICE_IEEE_ETS_TLV_LEN);
809         tlv->typelen = htons(typelen);
810         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
811                       ICE_IEEE_SUBTYPE_ETS_CFG);
812         tlv->ouisubtype = htonl(ouisubtype);
813
814         buf = tlv->tlvinfo;
815         buf[0] = 0;
816
817         /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
818          * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
819          * Octets 13 - 20 are TSA values - leave as zeros
820          */
821         buf[5] = 0x64;
822         len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
823         offset += len + 2;
824         tlv = (struct ice_lldp_org_tlv *)
825                 ((char *)tlv + sizeof(tlv->typelen) + len);
826
827         /* Add ETS REC TLV */
828         buf = tlv->tlvinfo;
829         tlv->typelen = htons(typelen);
830
831         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
832                       ICE_IEEE_SUBTYPE_ETS_REC);
833         tlv->ouisubtype = htonl(ouisubtype);
834
835         /* First octet of buf is reserved
836          * Octets 1 - 4 map UP to TC - all UPs map to zero
837          * Octets 5 - 12 are BW values - set TC 0 to 100%.
838          * Octets 13 - 20 are TSA value - leave as zeros
839          */
840         buf[5] = 0x64;
841         offset += len + 2;
842         tlv = (struct ice_lldp_org_tlv *)
843                 ((char *)tlv + sizeof(tlv->typelen) + len);
844
845         /* Add PFC CFG TLV */
846         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
847                    ICE_IEEE_PFC_TLV_LEN);
848         tlv->typelen = htons(typelen);
849
850         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
851                       ICE_IEEE_SUBTYPE_PFC_CFG);
852         tlv->ouisubtype = htonl(ouisubtype);
853
854         /* Octet 1 left as all zeros - PFC disabled */
855         buf[0] = 0x08;
856         len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
857         offset += len + 2;
858
859         if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
860                 dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
861
862         kfree(lldpmib);
863 }
864
865 /**
866  * ice_link_event - process the link event
867  * @pf: PF that the link event is associated with
868  * @pi: port_info for the port that the link event is associated with
869  * @link_up: true if the physical link is up and false if it is down
870  * @link_speed: current link speed received from the link event
871  *
872  * Returns 0 on success and negative on failure
873  */
874 static int
875 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
876                u16 link_speed)
877 {
878         struct device *dev = ice_pf_to_dev(pf);
879         struct ice_phy_info *phy_info;
880         struct ice_vsi *vsi;
881         u16 old_link_speed;
882         bool old_link;
883         int result;
884
885         phy_info = &pi->phy;
886         phy_info->link_info_old = phy_info->link_info;
887
888         old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
889         old_link_speed = phy_info->link_info_old.link_speed;
890
891         /* update the link info structures and re-enable link events,
892          * don't bail on failure due to other book keeping needed
893          */
894         result = ice_update_link_info(pi);
895         if (result)
896                 dev_dbg(dev, "Failed to update link status and re-enable link events for port %d\n",
897                         pi->lport);
898
899         /* Check if the link state is up after updating link info, and treat
900          * this event as an UP event since the link is actually UP now.
901          */
902         if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
903                 link_up = true;
904
905         vsi = ice_get_main_vsi(pf);
906         if (!vsi || !vsi->port_info)
907                 return -EINVAL;
908
909         /* turn off PHY if media was removed */
910         if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
911             !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
912                 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
913
914                 result = ice_aq_set_link_restart_an(pi, false, NULL);
915                 if (result) {
916                         dev_dbg(dev, "Failed to set link down, VSI %d error %d\n",
917                                 vsi->vsi_num, result);
918                         return result;
919                 }
920         }
921
922         /* if the old link up/down and speed is the same as the new */
923         if (link_up == old_link && link_speed == old_link_speed)
924                 return result;
925
926         if (ice_is_dcb_active(pf)) {
927                 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
928                         ice_dcb_rebuild(pf);
929         } else {
930                 if (link_up)
931                         ice_set_dflt_mib(pf);
932         }
933         ice_vsi_link_event(vsi, link_up);
934         ice_print_link_msg(vsi, link_up);
935
936         ice_vc_notify_link_state(pf);
937
938         return result;
939 }
940
941 /**
942  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
943  * @pf: board private structure
944  */
945 static void ice_watchdog_subtask(struct ice_pf *pf)
946 {
947         int i;
948
949         /* if interface is down do nothing */
950         if (test_bit(__ICE_DOWN, pf->state) ||
951             test_bit(__ICE_CFG_BUSY, pf->state))
952                 return;
953
954         /* make sure we don't do these things too often */
955         if (time_before(jiffies,
956                         pf->serv_tmr_prev + pf->serv_tmr_period))
957                 return;
958
959         pf->serv_tmr_prev = jiffies;
960
961         /* Update the stats for active netdevs so the network stack
962          * can look at updated numbers whenever it cares to
963          */
964         ice_update_pf_stats(pf);
965         ice_for_each_vsi(pf, i)
966                 if (pf->vsi[i] && pf->vsi[i]->netdev)
967                         ice_update_vsi_stats(pf->vsi[i]);
968 }
969
970 /**
971  * ice_init_link_events - enable/initialize link events
972  * @pi: pointer to the port_info instance
973  *
974  * Returns -EIO on failure, 0 on success
975  */
976 static int ice_init_link_events(struct ice_port_info *pi)
977 {
978         u16 mask;
979
980         mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
981                        ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
982
983         if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
984                 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
985                         pi->lport);
986                 return -EIO;
987         }
988
989         if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
990                 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
991                         pi->lport);
992                 return -EIO;
993         }
994
995         return 0;
996 }
997
998 /**
999  * ice_handle_link_event - handle link event via ARQ
1000  * @pf: PF that the link event is associated with
1001  * @event: event structure containing link status info
1002  */
1003 static int
1004 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1005 {
1006         struct ice_aqc_get_link_status_data *link_data;
1007         struct ice_port_info *port_info;
1008         int status;
1009
1010         link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1011         port_info = pf->hw.port_info;
1012         if (!port_info)
1013                 return -EINVAL;
1014
1015         status = ice_link_event(pf, port_info,
1016                                 !!(link_data->link_info & ICE_AQ_LINK_UP),
1017                                 le16_to_cpu(link_data->link_speed));
1018         if (status)
1019                 dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1020                         status);
1021
1022         return status;
1023 }
1024
1025 enum ice_aq_task_state {
1026         ICE_AQ_TASK_WAITING = 0,
1027         ICE_AQ_TASK_COMPLETE,
1028         ICE_AQ_TASK_CANCELED,
1029 };
1030
1031 struct ice_aq_task {
1032         struct hlist_node entry;
1033
1034         u16 opcode;
1035         struct ice_rq_event_info *event;
1036         enum ice_aq_task_state state;
1037 };
1038
1039 /**
1040  * ice_wait_for_aq_event - Wait for an AdminQ event from firmware
1041  * @pf: pointer to the PF private structure
1042  * @opcode: the opcode to wait for
1043  * @timeout: how long to wait, in jiffies
1044  * @event: storage for the event info
1045  *
1046  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1047  * current thread will be put to sleep until the specified event occurs or
1048  * until the given timeout is reached.
1049  *
1050  * To obtain only the descriptor contents, pass an event without an allocated
1051  * msg_buf. If the complete data buffer is desired, allocate the
1052  * event->msg_buf with enough space ahead of time.
1053  *
1054  * Returns: zero on success, or a negative error code on failure.
1055  */
1056 int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1057                           struct ice_rq_event_info *event)
1058 {
1059         struct device *dev = ice_pf_to_dev(pf);
1060         struct ice_aq_task *task;
1061         unsigned long start;
1062         long ret;
1063         int err;
1064
1065         task = kzalloc(sizeof(*task), GFP_KERNEL);
1066         if (!task)
1067                 return -ENOMEM;
1068
1069         INIT_HLIST_NODE(&task->entry);
1070         task->opcode = opcode;
1071         task->event = event;
1072         task->state = ICE_AQ_TASK_WAITING;
1073
1074         spin_lock_bh(&pf->aq_wait_lock);
1075         hlist_add_head(&task->entry, &pf->aq_wait_list);
1076         spin_unlock_bh(&pf->aq_wait_lock);
1077
1078         start = jiffies;
1079
1080         ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1081                                                timeout);
1082         switch (task->state) {
1083         case ICE_AQ_TASK_WAITING:
1084                 err = ret < 0 ? ret : -ETIMEDOUT;
1085                 break;
1086         case ICE_AQ_TASK_CANCELED:
1087                 err = ret < 0 ? ret : -ECANCELED;
1088                 break;
1089         case ICE_AQ_TASK_COMPLETE:
1090                 err = ret < 0 ? ret : 0;
1091                 break;
1092         default:
1093                 WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1094                 err = -EINVAL;
1095                 break;
1096         }
1097
1098         dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1099                 jiffies_to_msecs(jiffies - start),
1100                 jiffies_to_msecs(timeout),
1101                 opcode);
1102
1103         spin_lock_bh(&pf->aq_wait_lock);
1104         hlist_del(&task->entry);
1105         spin_unlock_bh(&pf->aq_wait_lock);
1106         kfree(task);
1107
1108         return err;
1109 }
1110
1111 /**
1112  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1113  * @pf: pointer to the PF private structure
1114  * @opcode: the opcode of the event
1115  * @event: the event to check
1116  *
1117  * Loops over the current list of pending threads waiting for an AdminQ event.
1118  * For each matching task, copy the contents of the event into the task
1119  * structure and wake up the thread.
1120  *
1121  * If multiple threads wait for the same opcode, they will all be woken up.
1122  *
1123  * Note that event->msg_buf will only be duplicated if the event has a buffer
1124  * with enough space already allocated. Otherwise, only the descriptor and
1125  * message length will be copied.
1126  *
1127  * Returns: true if an event was found, false otherwise
1128  */
1129 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1130                                 struct ice_rq_event_info *event)
1131 {
1132         struct ice_aq_task *task;
1133         bool found = false;
1134
1135         spin_lock_bh(&pf->aq_wait_lock);
1136         hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1137                 if (task->state || task->opcode != opcode)
1138                         continue;
1139
1140                 memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1141                 task->event->msg_len = event->msg_len;
1142
1143                 /* Only copy the data buffer if a destination was set */
1144                 if (task->event->msg_buf &&
1145                     task->event->buf_len > event->buf_len) {
1146                         memcpy(task->event->msg_buf, event->msg_buf,
1147                                event->buf_len);
1148                         task->event->buf_len = event->buf_len;
1149                 }
1150
1151                 task->state = ICE_AQ_TASK_COMPLETE;
1152                 found = true;
1153         }
1154         spin_unlock_bh(&pf->aq_wait_lock);
1155
1156         if (found)
1157                 wake_up(&pf->aq_wait_queue);
1158 }
1159
1160 /**
1161  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1162  * @pf: the PF private structure
1163  *
1164  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1165  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1166  */
1167 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1168 {
1169         struct ice_aq_task *task;
1170
1171         spin_lock_bh(&pf->aq_wait_lock);
1172         hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1173                 task->state = ICE_AQ_TASK_CANCELED;
1174         spin_unlock_bh(&pf->aq_wait_lock);
1175
1176         wake_up(&pf->aq_wait_queue);
1177 }
1178
1179 /**
1180  * __ice_clean_ctrlq - helper function to clean controlq rings
1181  * @pf: ptr to struct ice_pf
1182  * @q_type: specific Control queue type
1183  */
1184 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1185 {
1186         struct device *dev = ice_pf_to_dev(pf);
1187         struct ice_rq_event_info event;
1188         struct ice_hw *hw = &pf->hw;
1189         struct ice_ctl_q_info *cq;
1190         u16 pending, i = 0;
1191         const char *qtype;
1192         u32 oldval, val;
1193
1194         /* Do not clean control queue if/when PF reset fails */
1195         if (test_bit(__ICE_RESET_FAILED, pf->state))
1196                 return 0;
1197
1198         switch (q_type) {
1199         case ICE_CTL_Q_ADMIN:
1200                 cq = &hw->adminq;
1201                 qtype = "Admin";
1202                 break;
1203         case ICE_CTL_Q_MAILBOX:
1204                 cq = &hw->mailboxq;
1205                 qtype = "Mailbox";
1206                 break;
1207         default:
1208                 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1209                 return 0;
1210         }
1211
1212         /* check for error indications - PF_xx_AxQLEN register layout for
1213          * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1214          */
1215         val = rd32(hw, cq->rq.len);
1216         if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1217                    PF_FW_ARQLEN_ARQCRIT_M)) {
1218                 oldval = val;
1219                 if (val & PF_FW_ARQLEN_ARQVFE_M)
1220                         dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1221                                 qtype);
1222                 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1223                         dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1224                                 qtype);
1225                 }
1226                 if (val & PF_FW_ARQLEN_ARQCRIT_M)
1227                         dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1228                                 qtype);
1229                 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1230                          PF_FW_ARQLEN_ARQCRIT_M);
1231                 if (oldval != val)
1232                         wr32(hw, cq->rq.len, val);
1233         }
1234
1235         val = rd32(hw, cq->sq.len);
1236         if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1237                    PF_FW_ATQLEN_ATQCRIT_M)) {
1238                 oldval = val;
1239                 if (val & PF_FW_ATQLEN_ATQVFE_M)
1240                         dev_dbg(dev, "%s Send Queue VF Error detected\n",
1241                                 qtype);
1242                 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1243                         dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1244                                 qtype);
1245                 }
1246                 if (val & PF_FW_ATQLEN_ATQCRIT_M)
1247                         dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1248                                 qtype);
1249                 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1250                          PF_FW_ATQLEN_ATQCRIT_M);
1251                 if (oldval != val)
1252                         wr32(hw, cq->sq.len, val);
1253         }
1254
1255         event.buf_len = cq->rq_buf_size;
1256         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1257         if (!event.msg_buf)
1258                 return 0;
1259
1260         do {
1261                 enum ice_status ret;
1262                 u16 opcode;
1263
1264                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1265                 if (ret == ICE_ERR_AQ_NO_WORK)
1266                         break;
1267                 if (ret) {
1268                         dev_err(dev, "%s Receive Queue event error %s\n", qtype,
1269                                 ice_stat_str(ret));
1270                         break;
1271                 }
1272
1273                 opcode = le16_to_cpu(event.desc.opcode);
1274
1275                 /* Notify any thread that might be waiting for this event */
1276                 ice_aq_check_events(pf, opcode, &event);
1277
1278                 switch (opcode) {
1279                 case ice_aqc_opc_get_link_status:
1280                         if (ice_handle_link_event(pf, &event))
1281                                 dev_err(dev, "Could not handle link event\n");
1282                         break;
1283                 case ice_aqc_opc_event_lan_overflow:
1284                         ice_vf_lan_overflow_event(pf, &event);
1285                         break;
1286                 case ice_mbx_opc_send_msg_to_pf:
1287                         ice_vc_process_vf_msg(pf, &event);
1288                         break;
1289                 case ice_aqc_opc_fw_logging:
1290                         ice_output_fw_log(hw, &event.desc, event.msg_buf);
1291                         break;
1292                 case ice_aqc_opc_lldp_set_mib_change:
1293                         ice_dcb_process_lldp_set_mib_change(pf, &event);
1294                         break;
1295                 default:
1296                         dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1297                                 qtype, opcode);
1298                         break;
1299                 }
1300         } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1301
1302         kfree(event.msg_buf);
1303
1304         return pending && (i == ICE_DFLT_IRQ_WORK);
1305 }
1306
1307 /**
1308  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1309  * @hw: pointer to hardware info
1310  * @cq: control queue information
1311  *
1312  * returns true if there are pending messages in a queue, false if there aren't
1313  */
1314 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1315 {
1316         u16 ntu;
1317
1318         ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1319         return cq->rq.next_to_clean != ntu;
1320 }
1321
1322 /**
1323  * ice_clean_adminq_subtask - clean the AdminQ rings
1324  * @pf: board private structure
1325  */
1326 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1327 {
1328         struct ice_hw *hw = &pf->hw;
1329
1330         if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1331                 return;
1332
1333         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1334                 return;
1335
1336         clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1337
1338         /* There might be a situation where new messages arrive to a control
1339          * queue between processing the last message and clearing the
1340          * EVENT_PENDING bit. So before exiting, check queue head again (using
1341          * ice_ctrlq_pending) and process new messages if any.
1342          */
1343         if (ice_ctrlq_pending(hw, &hw->adminq))
1344                 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1345
1346         ice_flush(hw);
1347 }
1348
1349 /**
1350  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1351  * @pf: board private structure
1352  */
1353 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1354 {
1355         struct ice_hw *hw = &pf->hw;
1356
1357         if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1358                 return;
1359
1360         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1361                 return;
1362
1363         clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1364
1365         if (ice_ctrlq_pending(hw, &hw->mailboxq))
1366                 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1367
1368         ice_flush(hw);
1369 }
1370
1371 /**
1372  * ice_service_task_schedule - schedule the service task to wake up
1373  * @pf: board private structure
1374  *
1375  * If not already scheduled, this puts the task into the work queue.
1376  */
1377 void ice_service_task_schedule(struct ice_pf *pf)
1378 {
1379         if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1380             !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1381             !test_bit(__ICE_NEEDS_RESTART, pf->state))
1382                 queue_work(ice_wq, &pf->serv_task);
1383 }
1384
1385 /**
1386  * ice_service_task_complete - finish up the service task
1387  * @pf: board private structure
1388  */
1389 static void ice_service_task_complete(struct ice_pf *pf)
1390 {
1391         WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1392
1393         /* force memory (pf->state) to sync before next service task */
1394         smp_mb__before_atomic();
1395         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1396 }
1397
1398 /**
1399  * ice_service_task_stop - stop service task and cancel works
1400  * @pf: board private structure
1401  *
1402  * Return 0 if the __ICE_SERVICE_DIS bit was not already set,
1403  * 1 otherwise.
1404  */
1405 static int ice_service_task_stop(struct ice_pf *pf)
1406 {
1407         int ret;
1408
1409         ret = test_and_set_bit(__ICE_SERVICE_DIS, pf->state);
1410
1411         if (pf->serv_tmr.function)
1412                 del_timer_sync(&pf->serv_tmr);
1413         if (pf->serv_task.func)
1414                 cancel_work_sync(&pf->serv_task);
1415
1416         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1417         return ret;
1418 }
1419
1420 /**
1421  * ice_service_task_restart - restart service task and schedule works
1422  * @pf: board private structure
1423  *
1424  * This function is needed for suspend and resume works (e.g WoL scenario)
1425  */
1426 static void ice_service_task_restart(struct ice_pf *pf)
1427 {
1428         clear_bit(__ICE_SERVICE_DIS, pf->state);
1429         ice_service_task_schedule(pf);
1430 }
1431
1432 /**
1433  * ice_service_timer - timer callback to schedule service task
1434  * @t: pointer to timer_list
1435  */
1436 static void ice_service_timer(struct timer_list *t)
1437 {
1438         struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1439
1440         mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1441         ice_service_task_schedule(pf);
1442 }
1443
1444 /**
1445  * ice_handle_mdd_event - handle malicious driver detect event
1446  * @pf: pointer to the PF structure
1447  *
1448  * Called from service task. OICR interrupt handler indicates MDD event.
1449  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1450  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1451  * disable the queue, the PF can be configured to reset the VF using ethtool
1452  * private flag mdd-auto-reset-vf.
1453  */
1454 static void ice_handle_mdd_event(struct ice_pf *pf)
1455 {
1456         struct device *dev = ice_pf_to_dev(pf);
1457         struct ice_hw *hw = &pf->hw;
1458         unsigned int i;
1459         u32 reg;
1460
1461         if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state)) {
1462                 /* Since the VF MDD event logging is rate limited, check if
1463                  * there are pending MDD events.
1464                  */
1465                 ice_print_vfs_mdd_events(pf);
1466                 return;
1467         }
1468
1469         /* find what triggered an MDD event */
1470         reg = rd32(hw, GL_MDET_TX_PQM);
1471         if (reg & GL_MDET_TX_PQM_VALID_M) {
1472                 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1473                                 GL_MDET_TX_PQM_PF_NUM_S;
1474                 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1475                                 GL_MDET_TX_PQM_VF_NUM_S;
1476                 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1477                                 GL_MDET_TX_PQM_MAL_TYPE_S;
1478                 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1479                                 GL_MDET_TX_PQM_QNUM_S);
1480
1481                 if (netif_msg_tx_err(pf))
1482                         dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1483                                  event, queue, pf_num, vf_num);
1484                 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1485         }
1486
1487         reg = rd32(hw, GL_MDET_TX_TCLAN);
1488         if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1489                 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1490                                 GL_MDET_TX_TCLAN_PF_NUM_S;
1491                 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1492                                 GL_MDET_TX_TCLAN_VF_NUM_S;
1493                 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1494                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1495                 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1496                                 GL_MDET_TX_TCLAN_QNUM_S);
1497
1498                 if (netif_msg_tx_err(pf))
1499                         dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1500                                  event, queue, pf_num, vf_num);
1501                 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1502         }
1503
1504         reg = rd32(hw, GL_MDET_RX);
1505         if (reg & GL_MDET_RX_VALID_M) {
1506                 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1507                                 GL_MDET_RX_PF_NUM_S;
1508                 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1509                                 GL_MDET_RX_VF_NUM_S;
1510                 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1511                                 GL_MDET_RX_MAL_TYPE_S;
1512                 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1513                                 GL_MDET_RX_QNUM_S);
1514
1515                 if (netif_msg_rx_err(pf))
1516                         dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1517                                  event, queue, pf_num, vf_num);
1518                 wr32(hw, GL_MDET_RX, 0xffffffff);
1519         }
1520
1521         /* check to see if this PF caused an MDD event */
1522         reg = rd32(hw, PF_MDET_TX_PQM);
1523         if (reg & PF_MDET_TX_PQM_VALID_M) {
1524                 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1525                 if (netif_msg_tx_err(pf))
1526                         dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1527         }
1528
1529         reg = rd32(hw, PF_MDET_TX_TCLAN);
1530         if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1531                 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1532                 if (netif_msg_tx_err(pf))
1533                         dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1534         }
1535
1536         reg = rd32(hw, PF_MDET_RX);
1537         if (reg & PF_MDET_RX_VALID_M) {
1538                 wr32(hw, PF_MDET_RX, 0xFFFF);
1539                 if (netif_msg_rx_err(pf))
1540                         dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1541         }
1542
1543         /* Check to see if one of the VFs caused an MDD event, and then
1544          * increment counters and set print pending
1545          */
1546         ice_for_each_vf(pf, i) {
1547                 struct ice_vf *vf = &pf->vf[i];
1548
1549                 reg = rd32(hw, VP_MDET_TX_PQM(i));
1550                 if (reg & VP_MDET_TX_PQM_VALID_M) {
1551                         wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1552                         vf->mdd_tx_events.count++;
1553                         set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1554                         if (netif_msg_tx_err(pf))
1555                                 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1556                                          i);
1557                 }
1558
1559                 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1560                 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1561                         wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1562                         vf->mdd_tx_events.count++;
1563                         set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1564                         if (netif_msg_tx_err(pf))
1565                                 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1566                                          i);
1567                 }
1568
1569                 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1570                 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1571                         wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1572                         vf->mdd_tx_events.count++;
1573                         set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1574                         if (netif_msg_tx_err(pf))
1575                                 dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1576                                          i);
1577                 }
1578
1579                 reg = rd32(hw, VP_MDET_RX(i));
1580                 if (reg & VP_MDET_RX_VALID_M) {
1581                         wr32(hw, VP_MDET_RX(i), 0xFFFF);
1582                         vf->mdd_rx_events.count++;
1583                         set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1584                         if (netif_msg_rx_err(pf))
1585                                 dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1586                                          i);
1587
1588                         /* Since the queue is disabled on VF Rx MDD events, the
1589                          * PF can be configured to reset the VF through ethtool
1590                          * private flag mdd-auto-reset-vf.
1591                          */
1592                         if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1593                                 /* VF MDD event counters will be cleared by
1594                                  * reset, so print the event prior to reset.
1595                                  */
1596                                 ice_print_vf_rx_mdd_event(vf);
1597                                 ice_reset_vf(&pf->vf[i], false);
1598                         }
1599                 }
1600         }
1601
1602         ice_print_vfs_mdd_events(pf);
1603 }
1604
1605 /**
1606  * ice_force_phys_link_state - Force the physical link state
1607  * @vsi: VSI to force the physical link state to up/down
1608  * @link_up: true/false indicates to set the physical link to up/down
1609  *
1610  * Force the physical link state by getting the current PHY capabilities from
1611  * hardware and setting the PHY config based on the determined capabilities. If
1612  * link changes a link event will be triggered because both the Enable Automatic
1613  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1614  *
1615  * Returns 0 on success, negative on failure
1616  */
1617 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1618 {
1619         struct ice_aqc_get_phy_caps_data *pcaps;
1620         struct ice_aqc_set_phy_cfg_data *cfg;
1621         struct ice_port_info *pi;
1622         struct device *dev;
1623         int retcode;
1624
1625         if (!vsi || !vsi->port_info || !vsi->back)
1626                 return -EINVAL;
1627         if (vsi->type != ICE_VSI_PF)
1628                 return 0;
1629
1630         dev = ice_pf_to_dev(vsi->back);
1631
1632         pi = vsi->port_info;
1633
1634         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1635         if (!pcaps)
1636                 return -ENOMEM;
1637
1638         retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1639                                       NULL);
1640         if (retcode) {
1641                 dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1642                         vsi->vsi_num, retcode);
1643                 retcode = -EIO;
1644                 goto out;
1645         }
1646
1647         /* No change in link */
1648         if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1649             link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1650                 goto out;
1651
1652         /* Use the current user PHY configuration. The current user PHY
1653          * configuration is initialized during probe from PHY capabilities
1654          * software mode, and updated on set PHY configuration.
1655          */
1656         cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1657         if (!cfg) {
1658                 retcode = -ENOMEM;
1659                 goto out;
1660         }
1661
1662         cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1663         if (link_up)
1664                 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1665         else
1666                 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1667
1668         retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1669         if (retcode) {
1670                 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1671                         vsi->vsi_num, retcode);
1672                 retcode = -EIO;
1673         }
1674
1675         kfree(cfg);
1676 out:
1677         kfree(pcaps);
1678         return retcode;
1679 }
1680
1681 /**
1682  * ice_init_nvm_phy_type - Initialize the NVM PHY type
1683  * @pi: port info structure
1684  *
1685  * Initialize nvm_phy_type_[low|high] for link lenient mode support
1686  */
1687 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1688 {
1689         struct ice_aqc_get_phy_caps_data *pcaps;
1690         struct ice_pf *pf = pi->hw->back;
1691         enum ice_status status;
1692         int err = 0;
1693
1694         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1695         if (!pcaps)
1696                 return -ENOMEM;
1697
1698         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_NVM_CAP, pcaps,
1699                                      NULL);
1700
1701         if (status) {
1702                 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1703                 err = -EIO;
1704                 goto out;
1705         }
1706
1707         pf->nvm_phy_type_hi = pcaps->phy_type_high;
1708         pf->nvm_phy_type_lo = pcaps->phy_type_low;
1709
1710 out:
1711         kfree(pcaps);
1712         return err;
1713 }
1714
1715 /**
1716  * ice_init_link_dflt_override - Initialize link default override
1717  * @pi: port info structure
1718  *
1719  * Initialize link default override and PHY total port shutdown during probe
1720  */
1721 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1722 {
1723         struct ice_link_default_override_tlv *ldo;
1724         struct ice_pf *pf = pi->hw->back;
1725
1726         ldo = &pf->link_dflt_override;
1727         if (ice_get_link_default_override(ldo, pi))
1728                 return;
1729
1730         if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1731                 return;
1732
1733         /* Enable Total Port Shutdown (override/replace link-down-on-close
1734          * ethtool private flag) for ports with Port Disable bit set.
1735          */
1736         set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1737         set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1738 }
1739
1740 /**
1741  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1742  * @pi: port info structure
1743  *
1744  * If default override is enabled, initialized the user PHY cfg speed and FEC
1745  * settings using the default override mask from the NVM.
1746  *
1747  * The PHY should only be configured with the default override settings the
1748  * first time media is available. The __ICE_LINK_DEFAULT_OVERRIDE_PENDING state
1749  * is used to indicate that the user PHY cfg default override is initialized
1750  * and the PHY has not been configured with the default override settings. The
1751  * state is set here, and cleared in ice_configure_phy the first time the PHY is
1752  * configured.
1753  */
1754 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
1755 {
1756         struct ice_link_default_override_tlv *ldo;
1757         struct ice_aqc_set_phy_cfg_data *cfg;
1758         struct ice_phy_info *phy = &pi->phy;
1759         struct ice_pf *pf = pi->hw->back;
1760
1761         ldo = &pf->link_dflt_override;
1762
1763         /* If link default override is enabled, use to mask NVM PHY capabilities
1764          * for speed and FEC default configuration.
1765          */
1766         cfg = &phy->curr_user_phy_cfg;
1767
1768         if (ldo->phy_type_low || ldo->phy_type_high) {
1769                 cfg->phy_type_low = pf->nvm_phy_type_lo &
1770                                     cpu_to_le64(ldo->phy_type_low);
1771                 cfg->phy_type_high = pf->nvm_phy_type_hi &
1772                                      cpu_to_le64(ldo->phy_type_high);
1773         }
1774         cfg->link_fec_opt = ldo->fec_options;
1775         phy->curr_user_fec_req = ICE_FEC_AUTO;
1776
1777         set_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
1778 }
1779
1780 /**
1781  * ice_init_phy_user_cfg - Initialize the PHY user configuration
1782  * @pi: port info structure
1783  *
1784  * Initialize the current user PHY configuration, speed, FEC, and FC requested
1785  * mode to default. The PHY defaults are from get PHY capabilities topology
1786  * with media so call when media is first available. An error is returned if
1787  * called when media is not available. The PHY initialization completed state is
1788  * set here.
1789  *
1790  * These configurations are used when setting PHY
1791  * configuration. The user PHY configuration is updated on set PHY
1792  * configuration. Returns 0 on success, negative on failure
1793  */
1794 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
1795 {
1796         struct ice_aqc_get_phy_caps_data *pcaps;
1797         struct ice_phy_info *phy = &pi->phy;
1798         struct ice_pf *pf = pi->hw->back;
1799         enum ice_status status;
1800         struct ice_vsi *vsi;
1801         int err = 0;
1802
1803         if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1804                 return -EIO;
1805
1806         vsi = ice_get_main_vsi(pf);
1807         if (!vsi)
1808                 return -EINVAL;
1809
1810         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1811         if (!pcaps)
1812                 return -ENOMEM;
1813
1814         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1815                                      NULL);
1816         if (status) {
1817                 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1818                 err = -EIO;
1819                 goto err_out;
1820         }
1821
1822         ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
1823
1824         /* check if lenient mode is supported and enabled */
1825         if (ice_fw_supports_link_override(&vsi->back->hw) &&
1826             !(pcaps->module_compliance_enforcement &
1827               ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
1828                 set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
1829
1830                 /* if link default override is enabled, initialize user PHY
1831                  * configuration with link default override values
1832                  */
1833                 if (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN) {
1834                         ice_init_phy_cfg_dflt_override(pi);
1835                         goto out;
1836                 }
1837         }
1838
1839         /* if link default override is not enabled, initialize PHY using
1840          * topology with media
1841          */
1842         phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
1843                                                       pcaps->link_fec_options);
1844         phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
1845
1846 out:
1847         phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
1848         set_bit(__ICE_PHY_INIT_COMPLETE, pf->state);
1849 err_out:
1850         kfree(pcaps);
1851         return err;
1852 }
1853
1854 /**
1855  * ice_configure_phy - configure PHY
1856  * @vsi: VSI of PHY
1857  *
1858  * Set the PHY configuration. If the current PHY configuration is the same as
1859  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
1860  * configure the based get PHY capabilities for topology with media.
1861  */
1862 static int ice_configure_phy(struct ice_vsi *vsi)
1863 {
1864         struct device *dev = ice_pf_to_dev(vsi->back);
1865         struct ice_aqc_get_phy_caps_data *pcaps;
1866         struct ice_aqc_set_phy_cfg_data *cfg;
1867         struct ice_port_info *pi;
1868         enum ice_status status;
1869         int err = 0;
1870
1871         pi = vsi->port_info;
1872         if (!pi)
1873                 return -EINVAL;
1874
1875         /* Ensure we have media as we cannot configure a medialess port */
1876         if (!(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1877                 return -EPERM;
1878
1879         ice_print_topo_conflict(vsi);
1880
1881         if (vsi->port_info->phy.link_info.topo_media_conflict ==
1882             ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
1883                 return -EPERM;
1884
1885         if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
1886                 return ice_force_phys_link_state(vsi, true);
1887
1888         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1889         if (!pcaps)
1890                 return -ENOMEM;
1891
1892         /* Get current PHY config */
1893         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1894                                      NULL);
1895         if (status) {
1896                 dev_err(dev, "Failed to get PHY configuration, VSI %d error %s\n",
1897                         vsi->vsi_num, ice_stat_str(status));
1898                 err = -EIO;
1899                 goto done;
1900         }
1901
1902         /* If PHY enable link is configured and configuration has not changed,
1903          * there's nothing to do
1904          */
1905         if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
1906             ice_phy_caps_equals_cfg(pcaps, &pi->phy.curr_user_phy_cfg))
1907                 goto done;
1908
1909         /* Use PHY topology as baseline for configuration */
1910         memset(pcaps, 0, sizeof(*pcaps));
1911         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1912                                      NULL);
1913         if (status) {
1914                 dev_err(dev, "Failed to get PHY topology, VSI %d error %s\n",
1915                         vsi->vsi_num, ice_stat_str(status));
1916                 err = -EIO;
1917                 goto done;
1918         }
1919
1920         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1921         if (!cfg) {
1922                 err = -ENOMEM;
1923                 goto done;
1924         }
1925
1926         ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
1927
1928         /* Speed - If default override pending, use curr_user_phy_cfg set in
1929          * ice_init_phy_user_cfg_ldo.
1930          */
1931         if (test_and_clear_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING,
1932                                vsi->back->state)) {
1933                 cfg->phy_type_low = pi->phy.curr_user_phy_cfg.phy_type_low;
1934                 cfg->phy_type_high = pi->phy.curr_user_phy_cfg.phy_type_high;
1935         } else {
1936                 u64 phy_low = 0, phy_high = 0;
1937
1938                 ice_update_phy_type(&phy_low, &phy_high,
1939                                     pi->phy.curr_user_speed_req);
1940                 cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
1941                 cfg->phy_type_high = pcaps->phy_type_high &
1942                                      cpu_to_le64(phy_high);
1943         }
1944
1945         /* Can't provide what was requested; use PHY capabilities */
1946         if (!cfg->phy_type_low && !cfg->phy_type_high) {
1947                 cfg->phy_type_low = pcaps->phy_type_low;
1948                 cfg->phy_type_high = pcaps->phy_type_high;
1949         }
1950
1951         /* FEC */
1952         ice_cfg_phy_fec(pi, cfg, pi->phy.curr_user_fec_req);
1953
1954         /* Can't provide what was requested; use PHY capabilities */
1955         if (cfg->link_fec_opt !=
1956             (cfg->link_fec_opt & pcaps->link_fec_options)) {
1957                 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
1958                 cfg->link_fec_opt = pcaps->link_fec_options;
1959         }
1960
1961         /* Flow Control - always supported; no need to check against
1962          * capabilities
1963          */
1964         ice_cfg_phy_fc(pi, cfg, pi->phy.curr_user_fc_req);
1965
1966         /* Enable link and link update */
1967         cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
1968
1969         status = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1970         if (status) {
1971                 dev_err(dev, "Failed to set phy config, VSI %d error %s\n",
1972                         vsi->vsi_num, ice_stat_str(status));
1973                 err = -EIO;
1974         }
1975
1976         kfree(cfg);
1977 done:
1978         kfree(pcaps);
1979         return err;
1980 }
1981
1982 /**
1983  * ice_check_media_subtask - Check for media
1984  * @pf: pointer to PF struct
1985  *
1986  * If media is available, then initialize PHY user configuration if it is not
1987  * been, and configure the PHY if the interface is up.
1988  */
1989 static void ice_check_media_subtask(struct ice_pf *pf)
1990 {
1991         struct ice_port_info *pi;
1992         struct ice_vsi *vsi;
1993         int err;
1994
1995         /* No need to check for media if it's already present */
1996         if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
1997                 return;
1998
1999         vsi = ice_get_main_vsi(pf);
2000         if (!vsi)
2001                 return;
2002
2003         /* Refresh link info and check if media is present */
2004         pi = vsi->port_info;
2005         err = ice_update_link_info(pi);
2006         if (err)
2007                 return;
2008
2009         if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2010                 if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state))
2011                         ice_init_phy_user_cfg(pi);
2012
2013                 /* PHY settings are reset on media insertion, reconfigure
2014                  * PHY to preserve settings.
2015                  */
2016                 if (test_bit(__ICE_DOWN, vsi->state) &&
2017                     test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2018                         return;
2019
2020                 err = ice_configure_phy(vsi);
2021                 if (!err)
2022                         clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2023
2024                 /* A Link Status Event will be generated; the event handler
2025                  * will complete bringing the interface up
2026                  */
2027         }
2028 }
2029
2030 /**
2031  * ice_service_task - manage and run subtasks
2032  * @work: pointer to work_struct contained by the PF struct
2033  */
2034 static void ice_service_task(struct work_struct *work)
2035 {
2036         struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2037         unsigned long start_time = jiffies;
2038
2039         /* subtasks */
2040
2041         /* process reset requests first */
2042         ice_reset_subtask(pf);
2043
2044         /* bail if a reset/recovery cycle is pending or rebuild failed */
2045         if (ice_is_reset_in_progress(pf->state) ||
2046             test_bit(__ICE_SUSPENDED, pf->state) ||
2047             test_bit(__ICE_NEEDS_RESTART, pf->state)) {
2048                 ice_service_task_complete(pf);
2049                 return;
2050         }
2051
2052         ice_clean_adminq_subtask(pf);
2053         ice_check_media_subtask(pf);
2054         ice_check_for_hang_subtask(pf);
2055         ice_sync_fltr_subtask(pf);
2056         ice_handle_mdd_event(pf);
2057         ice_watchdog_subtask(pf);
2058
2059         if (ice_is_safe_mode(pf)) {
2060                 ice_service_task_complete(pf);
2061                 return;
2062         }
2063
2064         ice_process_vflr_event(pf);
2065         ice_clean_mailboxq_subtask(pf);
2066         ice_sync_arfs_fltrs(pf);
2067         /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
2068         ice_service_task_complete(pf);
2069
2070         /* If the tasks have taken longer than one service timer period
2071          * or there is more work to be done, reset the service timer to
2072          * schedule the service task now.
2073          */
2074         if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2075             test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
2076             test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
2077             test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2078             test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
2079                 mod_timer(&pf->serv_tmr, jiffies);
2080 }
2081
2082 /**
2083  * ice_set_ctrlq_len - helper function to set controlq length
2084  * @hw: pointer to the HW instance
2085  */
2086 static void ice_set_ctrlq_len(struct ice_hw *hw)
2087 {
2088         hw->adminq.num_rq_entries = ICE_AQ_LEN;
2089         hw->adminq.num_sq_entries = ICE_AQ_LEN;
2090         hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2091         hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2092         hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2093         hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2094         hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2095         hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2096 }
2097
2098 /**
2099  * ice_schedule_reset - schedule a reset
2100  * @pf: board private structure
2101  * @reset: reset being requested
2102  */
2103 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2104 {
2105         struct device *dev = ice_pf_to_dev(pf);
2106
2107         /* bail out if earlier reset has failed */
2108         if (test_bit(__ICE_RESET_FAILED, pf->state)) {
2109                 dev_dbg(dev, "earlier reset has failed\n");
2110                 return -EIO;
2111         }
2112         /* bail if reset/recovery already in progress */
2113         if (ice_is_reset_in_progress(pf->state)) {
2114                 dev_dbg(dev, "Reset already in progress\n");
2115                 return -EBUSY;
2116         }
2117
2118         switch (reset) {
2119         case ICE_RESET_PFR:
2120                 set_bit(__ICE_PFR_REQ, pf->state);
2121                 break;
2122         case ICE_RESET_CORER:
2123                 set_bit(__ICE_CORER_REQ, pf->state);
2124                 break;
2125         case ICE_RESET_GLOBR:
2126                 set_bit(__ICE_GLOBR_REQ, pf->state);
2127                 break;
2128         default:
2129                 return -EINVAL;
2130         }
2131
2132         ice_service_task_schedule(pf);
2133         return 0;
2134 }
2135
2136 /**
2137  * ice_irq_affinity_notify - Callback for affinity changes
2138  * @notify: context as to what irq was changed
2139  * @mask: the new affinity mask
2140  *
2141  * This is a callback function used by the irq_set_affinity_notifier function
2142  * so that we may register to receive changes to the irq affinity masks.
2143  */
2144 static void
2145 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2146                         const cpumask_t *mask)
2147 {
2148         struct ice_q_vector *q_vector =
2149                 container_of(notify, struct ice_q_vector, affinity_notify);
2150
2151         cpumask_copy(&q_vector->affinity_mask, mask);
2152 }
2153
2154 /**
2155  * ice_irq_affinity_release - Callback for affinity notifier release
2156  * @ref: internal core kernel usage
2157  *
2158  * This is a callback function used by the irq_set_affinity_notifier function
2159  * to inform the current notification subscriber that they will no longer
2160  * receive notifications.
2161  */
2162 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2163
2164 /**
2165  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2166  * @vsi: the VSI being configured
2167  */
2168 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2169 {
2170         struct ice_hw *hw = &vsi->back->hw;
2171         int i;
2172
2173         ice_for_each_q_vector(vsi, i)
2174                 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2175
2176         ice_flush(hw);
2177         return 0;
2178 }
2179
2180 /**
2181  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2182  * @vsi: the VSI being configured
2183  * @basename: name for the vector
2184  */
2185 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2186 {
2187         int q_vectors = vsi->num_q_vectors;
2188         struct ice_pf *pf = vsi->back;
2189         int base = vsi->base_vector;
2190         struct device *dev;
2191         int rx_int_idx = 0;
2192         int tx_int_idx = 0;
2193         int vector, err;
2194         int irq_num;
2195
2196         dev = ice_pf_to_dev(pf);
2197         for (vector = 0; vector < q_vectors; vector++) {
2198                 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2199
2200                 irq_num = pf->msix_entries[base + vector].vector;
2201
2202                 if (q_vector->tx.ring && q_vector->rx.ring) {
2203                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2204                                  "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2205                         tx_int_idx++;
2206                 } else if (q_vector->rx.ring) {
2207                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2208                                  "%s-%s-%d", basename, "rx", rx_int_idx++);
2209                 } else if (q_vector->tx.ring) {
2210                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2211                                  "%s-%s-%d", basename, "tx", tx_int_idx++);
2212                 } else {
2213                         /* skip this unused q_vector */
2214                         continue;
2215                 }
2216                 err = devm_request_irq(dev, irq_num, vsi->irq_handler, 0,
2217                                        q_vector->name, q_vector);
2218                 if (err) {
2219                         netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2220                                    err);
2221                         goto free_q_irqs;
2222                 }
2223
2224                 /* register for affinity change notifications */
2225                 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2226                         struct irq_affinity_notify *affinity_notify;
2227
2228                         affinity_notify = &q_vector->affinity_notify;
2229                         affinity_notify->notify = ice_irq_affinity_notify;
2230                         affinity_notify->release = ice_irq_affinity_release;
2231                         irq_set_affinity_notifier(irq_num, affinity_notify);
2232                 }
2233
2234                 /* assign the mask for this irq */
2235                 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2236         }
2237
2238         vsi->irqs_ready = true;
2239         return 0;
2240
2241 free_q_irqs:
2242         while (vector) {
2243                 vector--;
2244                 irq_num = pf->msix_entries[base + vector].vector;
2245                 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2246                         irq_set_affinity_notifier(irq_num, NULL);
2247                 irq_set_affinity_hint(irq_num, NULL);
2248                 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2249         }
2250         return err;
2251 }
2252
2253 /**
2254  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2255  * @vsi: VSI to setup Tx rings used by XDP
2256  *
2257  * Return 0 on success and negative value on error
2258  */
2259 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2260 {
2261         struct device *dev = ice_pf_to_dev(vsi->back);
2262         int i;
2263
2264         for (i = 0; i < vsi->num_xdp_txq; i++) {
2265                 u16 xdp_q_idx = vsi->alloc_txq + i;
2266                 struct ice_ring *xdp_ring;
2267
2268                 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2269
2270                 if (!xdp_ring)
2271                         goto free_xdp_rings;
2272
2273                 xdp_ring->q_index = xdp_q_idx;
2274                 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2275                 xdp_ring->ring_active = false;
2276                 xdp_ring->vsi = vsi;
2277                 xdp_ring->netdev = NULL;
2278                 xdp_ring->dev = dev;
2279                 xdp_ring->count = vsi->num_tx_desc;
2280                 WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2281                 if (ice_setup_tx_ring(xdp_ring))
2282                         goto free_xdp_rings;
2283                 ice_set_ring_xdp(xdp_ring);
2284                 xdp_ring->xsk_pool = ice_xsk_pool(xdp_ring);
2285         }
2286
2287         return 0;
2288
2289 free_xdp_rings:
2290         for (; i >= 0; i--)
2291                 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2292                         ice_free_tx_ring(vsi->xdp_rings[i]);
2293         return -ENOMEM;
2294 }
2295
2296 /**
2297  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2298  * @vsi: VSI to set the bpf prog on
2299  * @prog: the bpf prog pointer
2300  */
2301 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2302 {
2303         struct bpf_prog *old_prog;
2304         int i;
2305
2306         old_prog = xchg(&vsi->xdp_prog, prog);
2307         if (old_prog)
2308                 bpf_prog_put(old_prog);
2309
2310         ice_for_each_rxq(vsi, i)
2311                 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2312 }
2313
2314 /**
2315  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2316  * @vsi: VSI to bring up Tx rings used by XDP
2317  * @prog: bpf program that will be assigned to VSI
2318  *
2319  * Return 0 on success and negative value on error
2320  */
2321 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2322 {
2323         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2324         int xdp_rings_rem = vsi->num_xdp_txq;
2325         struct ice_pf *pf = vsi->back;
2326         struct ice_qs_cfg xdp_qs_cfg = {
2327                 .qs_mutex = &pf->avail_q_mutex,
2328                 .pf_map = pf->avail_txqs,
2329                 .pf_map_size = pf->max_pf_txqs,
2330                 .q_count = vsi->num_xdp_txq,
2331                 .scatter_count = ICE_MAX_SCATTER_TXQS,
2332                 .vsi_map = vsi->txq_map,
2333                 .vsi_map_offset = vsi->alloc_txq,
2334                 .mapping_mode = ICE_VSI_MAP_CONTIG
2335         };
2336         enum ice_status status;
2337         struct device *dev;
2338         int i, v_idx;
2339
2340         dev = ice_pf_to_dev(pf);
2341         vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2342                                       sizeof(*vsi->xdp_rings), GFP_KERNEL);
2343         if (!vsi->xdp_rings)
2344                 return -ENOMEM;
2345
2346         vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2347         if (__ice_vsi_get_qs(&xdp_qs_cfg))
2348                 goto err_map_xdp;
2349
2350         if (ice_xdp_alloc_setup_rings(vsi))
2351                 goto clear_xdp_rings;
2352
2353         /* follow the logic from ice_vsi_map_rings_to_vectors */
2354         ice_for_each_q_vector(vsi, v_idx) {
2355                 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2356                 int xdp_rings_per_v, q_id, q_base;
2357
2358                 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2359                                                vsi->num_q_vectors - v_idx);
2360                 q_base = vsi->num_xdp_txq - xdp_rings_rem;
2361
2362                 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2363                         struct ice_ring *xdp_ring = vsi->xdp_rings[q_id];
2364
2365                         xdp_ring->q_vector = q_vector;
2366                         xdp_ring->next = q_vector->tx.ring;
2367                         q_vector->tx.ring = xdp_ring;
2368                 }
2369                 xdp_rings_rem -= xdp_rings_per_v;
2370         }
2371
2372         /* omit the scheduler update if in reset path; XDP queues will be
2373          * taken into account at the end of ice_vsi_rebuild, where
2374          * ice_cfg_vsi_lan is being called
2375          */
2376         if (ice_is_reset_in_progress(pf->state))
2377                 return 0;
2378
2379         /* tell the Tx scheduler that right now we have
2380          * additional queues
2381          */
2382         for (i = 0; i < vsi->tc_cfg.numtc; i++)
2383                 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2384
2385         status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2386                                  max_txqs);
2387         if (status) {
2388                 dev_err(dev, "Failed VSI LAN queue config for XDP, error: %s\n",
2389                         ice_stat_str(status));
2390                 goto clear_xdp_rings;
2391         }
2392         ice_vsi_assign_bpf_prog(vsi, prog);
2393
2394         return 0;
2395 clear_xdp_rings:
2396         for (i = 0; i < vsi->num_xdp_txq; i++)
2397                 if (vsi->xdp_rings[i]) {
2398                         kfree_rcu(vsi->xdp_rings[i], rcu);
2399                         vsi->xdp_rings[i] = NULL;
2400                 }
2401
2402 err_map_xdp:
2403         mutex_lock(&pf->avail_q_mutex);
2404         for (i = 0; i < vsi->num_xdp_txq; i++) {
2405                 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2406                 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2407         }
2408         mutex_unlock(&pf->avail_q_mutex);
2409
2410         devm_kfree(dev, vsi->xdp_rings);
2411         return -ENOMEM;
2412 }
2413
2414 /**
2415  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2416  * @vsi: VSI to remove XDP rings
2417  *
2418  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2419  * resources
2420  */
2421 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2422 {
2423         u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2424         struct ice_pf *pf = vsi->back;
2425         int i, v_idx;
2426
2427         /* q_vectors are freed in reset path so there's no point in detaching
2428          * rings; in case of rebuild being triggered not from reset bits
2429          * in pf->state won't be set, so additionally check first q_vector
2430          * against NULL
2431          */
2432         if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2433                 goto free_qmap;
2434
2435         ice_for_each_q_vector(vsi, v_idx) {
2436                 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2437                 struct ice_ring *ring;
2438
2439                 ice_for_each_ring(ring, q_vector->tx)
2440                         if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2441                                 break;
2442
2443                 /* restore the value of last node prior to XDP setup */
2444                 q_vector->tx.ring = ring;
2445         }
2446
2447 free_qmap:
2448         mutex_lock(&pf->avail_q_mutex);
2449         for (i = 0; i < vsi->num_xdp_txq; i++) {
2450                 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2451                 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2452         }
2453         mutex_unlock(&pf->avail_q_mutex);
2454
2455         for (i = 0; i < vsi->num_xdp_txq; i++)
2456                 if (vsi->xdp_rings[i]) {
2457                         if (vsi->xdp_rings[i]->desc)
2458                                 ice_free_tx_ring(vsi->xdp_rings[i]);
2459                         kfree_rcu(vsi->xdp_rings[i], rcu);
2460                         vsi->xdp_rings[i] = NULL;
2461                 }
2462
2463         devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2464         vsi->xdp_rings = NULL;
2465
2466         if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2467                 return 0;
2468
2469         ice_vsi_assign_bpf_prog(vsi, NULL);
2470
2471         /* notify Tx scheduler that we destroyed XDP queues and bring
2472          * back the old number of child nodes
2473          */
2474         for (i = 0; i < vsi->tc_cfg.numtc; i++)
2475                 max_txqs[i] = vsi->num_txq;
2476
2477         /* change number of XDP Tx queues to 0 */
2478         vsi->num_xdp_txq = 0;
2479
2480         return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2481                                max_txqs);
2482 }
2483
2484 /**
2485  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2486  * @vsi: VSI to setup XDP for
2487  * @prog: XDP program
2488  * @extack: netlink extended ack
2489  */
2490 static int
2491 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2492                    struct netlink_ext_ack *extack)
2493 {
2494         int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2495         bool if_running = netif_running(vsi->netdev);
2496         int ret = 0, xdp_ring_err = 0;
2497
2498         if (frame_size > vsi->rx_buf_len) {
2499                 NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2500                 return -EOPNOTSUPP;
2501         }
2502
2503         /* need to stop netdev while setting up the program for Rx rings */
2504         if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) {
2505                 ret = ice_down(vsi);
2506                 if (ret) {
2507                         NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2508                         return ret;
2509                 }
2510         }
2511
2512         if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2513                 vsi->num_xdp_txq = vsi->alloc_rxq;
2514                 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2515                 if (xdp_ring_err)
2516                         NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2517         } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2518                 xdp_ring_err = ice_destroy_xdp_rings(vsi);
2519                 if (xdp_ring_err)
2520                         NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2521         } else {
2522                 ice_vsi_assign_bpf_prog(vsi, prog);
2523         }
2524
2525         if (if_running)
2526                 ret = ice_up(vsi);
2527
2528         if (!ret && prog && vsi->xsk_pools) {
2529                 int i;
2530
2531                 ice_for_each_rxq(vsi, i) {
2532                         struct ice_ring *rx_ring = vsi->rx_rings[i];
2533
2534                         if (rx_ring->xsk_pool)
2535                                 napi_schedule(&rx_ring->q_vector->napi);
2536                 }
2537         }
2538
2539         return (ret || xdp_ring_err) ? -ENOMEM : 0;
2540 }
2541
2542 /**
2543  * ice_xdp - implements XDP handler
2544  * @dev: netdevice
2545  * @xdp: XDP command
2546  */
2547 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2548 {
2549         struct ice_netdev_priv *np = netdev_priv(dev);
2550         struct ice_vsi *vsi = np->vsi;
2551
2552         if (vsi->type != ICE_VSI_PF) {
2553                 NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2554                 return -EINVAL;
2555         }
2556
2557         switch (xdp->command) {
2558         case XDP_SETUP_PROG:
2559                 return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2560         case XDP_SETUP_XSK_POOL:
2561                 return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2562                                           xdp->xsk.queue_id);
2563         default:
2564                 return -EINVAL;
2565         }
2566 }
2567
2568 /**
2569  * ice_ena_misc_vector - enable the non-queue interrupts
2570  * @pf: board private structure
2571  */
2572 static void ice_ena_misc_vector(struct ice_pf *pf)
2573 {
2574         struct ice_hw *hw = &pf->hw;
2575         u32 val;
2576
2577         /* Disable anti-spoof detection interrupt to prevent spurious event
2578          * interrupts during a function reset. Anti-spoof functionally is
2579          * still supported.
2580          */
2581         val = rd32(hw, GL_MDCK_TX_TDPU);
2582         val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2583         wr32(hw, GL_MDCK_TX_TDPU, val);
2584
2585         /* clear things first */
2586         wr32(hw, PFINT_OICR_ENA, 0);    /* disable all */
2587         rd32(hw, PFINT_OICR);           /* read to clear */
2588
2589         val = (PFINT_OICR_ECC_ERR_M |
2590                PFINT_OICR_MAL_DETECT_M |
2591                PFINT_OICR_GRST_M |
2592                PFINT_OICR_PCI_EXCEPTION_M |
2593                PFINT_OICR_VFLR_M |
2594                PFINT_OICR_HMC_ERR_M |
2595                PFINT_OICR_PE_CRITERR_M);
2596
2597         wr32(hw, PFINT_OICR_ENA, val);
2598
2599         /* SW_ITR_IDX = 0, but don't change INTENA */
2600         wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2601              GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2602 }
2603
2604 /**
2605  * ice_misc_intr - misc interrupt handler
2606  * @irq: interrupt number
2607  * @data: pointer to a q_vector
2608  */
2609 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2610 {
2611         struct ice_pf *pf = (struct ice_pf *)data;
2612         struct ice_hw *hw = &pf->hw;
2613         irqreturn_t ret = IRQ_NONE;
2614         struct device *dev;
2615         u32 oicr, ena_mask;
2616
2617         dev = ice_pf_to_dev(pf);
2618         set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2619         set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2620
2621         oicr = rd32(hw, PFINT_OICR);
2622         ena_mask = rd32(hw, PFINT_OICR_ENA);
2623
2624         if (oicr & PFINT_OICR_SWINT_M) {
2625                 ena_mask &= ~PFINT_OICR_SWINT_M;
2626                 pf->sw_int_count++;
2627         }
2628
2629         if (oicr & PFINT_OICR_MAL_DETECT_M) {
2630                 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2631                 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2632         }
2633         if (oicr & PFINT_OICR_VFLR_M) {
2634                 /* disable any further VFLR event notifications */
2635                 if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
2636                         u32 reg = rd32(hw, PFINT_OICR_ENA);
2637
2638                         reg &= ~PFINT_OICR_VFLR_M;
2639                         wr32(hw, PFINT_OICR_ENA, reg);
2640                 } else {
2641                         ena_mask &= ~PFINT_OICR_VFLR_M;
2642                         set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
2643                 }
2644         }
2645
2646         if (oicr & PFINT_OICR_GRST_M) {
2647                 u32 reset;
2648
2649                 /* we have a reset warning */
2650                 ena_mask &= ~PFINT_OICR_GRST_M;
2651                 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2652                         GLGEN_RSTAT_RESET_TYPE_S;
2653
2654                 if (reset == ICE_RESET_CORER)
2655                         pf->corer_count++;
2656                 else if (reset == ICE_RESET_GLOBR)
2657                         pf->globr_count++;
2658                 else if (reset == ICE_RESET_EMPR)
2659                         pf->empr_count++;
2660                 else
2661                         dev_dbg(dev, "Invalid reset type %d\n", reset);
2662
2663                 /* If a reset cycle isn't already in progress, we set a bit in
2664                  * pf->state so that the service task can start a reset/rebuild.
2665                  * We also make note of which reset happened so that peer
2666                  * devices/drivers can be informed.
2667                  */
2668                 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
2669                         if (reset == ICE_RESET_CORER)
2670                                 set_bit(__ICE_CORER_RECV, pf->state);
2671                         else if (reset == ICE_RESET_GLOBR)
2672                                 set_bit(__ICE_GLOBR_RECV, pf->state);
2673                         else
2674                                 set_bit(__ICE_EMPR_RECV, pf->state);
2675
2676                         /* There are couple of different bits at play here.
2677                          * hw->reset_ongoing indicates whether the hardware is
2678                          * in reset. This is set to true when a reset interrupt
2679                          * is received and set back to false after the driver
2680                          * has determined that the hardware is out of reset.
2681                          *
2682                          * __ICE_RESET_OICR_RECV in pf->state indicates
2683                          * that a post reset rebuild is required before the
2684                          * driver is operational again. This is set above.
2685                          *
2686                          * As this is the start of the reset/rebuild cycle, set
2687                          * both to indicate that.
2688                          */
2689                         hw->reset_ongoing = true;
2690                 }
2691         }
2692
2693         if (oicr & PFINT_OICR_HMC_ERR_M) {
2694                 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2695                 dev_dbg(dev, "HMC Error interrupt - info 0x%x, data 0x%x\n",
2696                         rd32(hw, PFHMC_ERRORINFO),
2697                         rd32(hw, PFHMC_ERRORDATA));
2698         }
2699
2700         /* Report any remaining unexpected interrupts */
2701         oicr &= ena_mask;
2702         if (oicr) {
2703                 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
2704                 /* If a critical error is pending there is no choice but to
2705                  * reset the device.
2706                  */
2707                 if (oicr & (PFINT_OICR_PE_CRITERR_M |
2708                             PFINT_OICR_PCI_EXCEPTION_M |
2709                             PFINT_OICR_ECC_ERR_M)) {
2710                         set_bit(__ICE_PFR_REQ, pf->state);
2711                         ice_service_task_schedule(pf);
2712                 }
2713         }
2714         ret = IRQ_HANDLED;
2715
2716         ice_service_task_schedule(pf);
2717         ice_irq_dynamic_ena(hw, NULL, NULL);
2718
2719         return ret;
2720 }
2721
2722 /**
2723  * ice_dis_ctrlq_interrupts - disable control queue interrupts
2724  * @hw: pointer to HW structure
2725  */
2726 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
2727 {
2728         /* disable Admin queue Interrupt causes */
2729         wr32(hw, PFINT_FW_CTL,
2730              rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
2731
2732         /* disable Mailbox queue Interrupt causes */
2733         wr32(hw, PFINT_MBX_CTL,
2734              rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
2735
2736         /* disable Control queue Interrupt causes */
2737         wr32(hw, PFINT_OICR_CTL,
2738              rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
2739
2740         ice_flush(hw);
2741 }
2742
2743 /**
2744  * ice_free_irq_msix_misc - Unroll misc vector setup
2745  * @pf: board private structure
2746  */
2747 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2748 {
2749         struct ice_hw *hw = &pf->hw;
2750
2751         ice_dis_ctrlq_interrupts(hw);
2752
2753         /* disable OICR interrupt */
2754         wr32(hw, PFINT_OICR_ENA, 0);
2755         ice_flush(hw);
2756
2757         if (pf->msix_entries) {
2758                 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2759                 devm_free_irq(ice_pf_to_dev(pf),
2760                               pf->msix_entries[pf->oicr_idx].vector, pf);
2761         }
2762
2763         pf->num_avail_sw_msix += 1;
2764         ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2765 }
2766
2767 /**
2768  * ice_ena_ctrlq_interrupts - enable control queue interrupts
2769  * @hw: pointer to HW structure
2770  * @reg_idx: HW vector index to associate the control queue interrupts with
2771  */
2772 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
2773 {
2774         u32 val;
2775
2776         val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2777                PFINT_OICR_CTL_CAUSE_ENA_M);
2778         wr32(hw, PFINT_OICR_CTL, val);
2779
2780         /* enable Admin queue Interrupt causes */
2781         val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2782                PFINT_FW_CTL_CAUSE_ENA_M);
2783         wr32(hw, PFINT_FW_CTL, val);
2784
2785         /* enable Mailbox queue Interrupt causes */
2786         val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
2787                PFINT_MBX_CTL_CAUSE_ENA_M);
2788         wr32(hw, PFINT_MBX_CTL, val);
2789
2790         ice_flush(hw);
2791 }
2792
2793 /**
2794  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2795  * @pf: board private structure
2796  *
2797  * This sets up the handler for MSIX 0, which is used to manage the
2798  * non-queue interrupts, e.g. AdminQ and errors. This is not used
2799  * when in MSI or Legacy interrupt mode.
2800  */
2801 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2802 {
2803         struct device *dev = ice_pf_to_dev(pf);
2804         struct ice_hw *hw = &pf->hw;
2805         int oicr_idx, err = 0;
2806
2807         if (!pf->int_name[0])
2808                 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2809                          dev_driver_string(dev), dev_name(dev));
2810
2811         /* Do not request IRQ but do enable OICR interrupt since settings are
2812          * lost during reset. Note that this function is called only during
2813          * rebuild path and not while reset is in progress.
2814          */
2815         if (ice_is_reset_in_progress(pf->state))
2816                 goto skip_req_irq;
2817
2818         /* reserve one vector in irq_tracker for misc interrupts */
2819         oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2820         if (oicr_idx < 0)
2821                 return oicr_idx;
2822
2823         pf->num_avail_sw_msix -= 1;
2824         pf->oicr_idx = (u16)oicr_idx;
2825
2826         err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
2827                                ice_misc_intr, 0, pf->int_name, pf);
2828         if (err) {
2829                 dev_err(dev, "devm_request_irq for %s failed: %d\n",
2830                         pf->int_name, err);
2831                 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2832                 pf->num_avail_sw_msix += 1;
2833                 return err;
2834         }
2835
2836 skip_req_irq:
2837         ice_ena_misc_vector(pf);
2838
2839         ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
2840         wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2841              ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
2842
2843         ice_flush(hw);
2844         ice_irq_dynamic_ena(hw, NULL, NULL);
2845
2846         return 0;
2847 }
2848
2849 /**
2850  * ice_napi_add - register NAPI handler for the VSI
2851  * @vsi: VSI for which NAPI handler is to be registered
2852  *
2853  * This function is only called in the driver's load path. Registering the NAPI
2854  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2855  * reset/rebuild, etc.)
2856  */
2857 static void ice_napi_add(struct ice_vsi *vsi)
2858 {
2859         int v_idx;
2860
2861         if (!vsi->netdev)
2862                 return;
2863
2864         ice_for_each_q_vector(vsi, v_idx)
2865                 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
2866                                ice_napi_poll, NAPI_POLL_WEIGHT);
2867 }
2868
2869 /**
2870  * ice_set_ops - set netdev and ethtools ops for the given netdev
2871  * @netdev: netdev instance
2872  */
2873 static void ice_set_ops(struct net_device *netdev)
2874 {
2875         struct ice_pf *pf = ice_netdev_to_pf(netdev);
2876
2877         if (ice_is_safe_mode(pf)) {
2878                 netdev->netdev_ops = &ice_netdev_safe_mode_ops;
2879                 ice_set_ethtool_safe_mode_ops(netdev);
2880                 return;
2881         }
2882
2883         netdev->netdev_ops = &ice_netdev_ops;
2884         netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
2885         ice_set_ethtool_ops(netdev);
2886 }
2887
2888 /**
2889  * ice_set_netdev_features - set features for the given netdev
2890  * @netdev: netdev instance
2891  */
2892 static void ice_set_netdev_features(struct net_device *netdev)
2893 {
2894         struct ice_pf *pf = ice_netdev_to_pf(netdev);
2895         netdev_features_t csumo_features;
2896         netdev_features_t vlano_features;
2897         netdev_features_t dflt_features;
2898         netdev_features_t tso_features;
2899
2900         if (ice_is_safe_mode(pf)) {
2901                 /* safe mode */
2902                 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
2903                 netdev->hw_features = netdev->features;
2904                 return;
2905         }
2906
2907         dflt_features = NETIF_F_SG      |
2908                         NETIF_F_HIGHDMA |
2909                         NETIF_F_NTUPLE  |
2910                         NETIF_F_RXHASH;
2911
2912         csumo_features = NETIF_F_RXCSUM   |
2913                          NETIF_F_IP_CSUM  |
2914                          NETIF_F_SCTP_CRC |
2915                          NETIF_F_IPV6_CSUM;
2916
2917         vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2918                          NETIF_F_HW_VLAN_CTAG_TX     |
2919                          NETIF_F_HW_VLAN_CTAG_RX;
2920
2921         tso_features = NETIF_F_TSO                      |
2922                        NETIF_F_TSO_ECN                  |
2923                        NETIF_F_TSO6                     |
2924                        NETIF_F_GSO_GRE                  |
2925                        NETIF_F_GSO_UDP_TUNNEL           |
2926                        NETIF_F_GSO_GRE_CSUM             |
2927                        NETIF_F_GSO_UDP_TUNNEL_CSUM      |
2928                        NETIF_F_GSO_PARTIAL              |
2929                        NETIF_F_GSO_IPXIP4               |
2930                        NETIF_F_GSO_IPXIP6               |
2931                        NETIF_F_GSO_UDP_L4;
2932
2933         netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
2934                                         NETIF_F_GSO_GRE_CSUM;
2935         /* set features that user can change */
2936         netdev->hw_features = dflt_features | csumo_features |
2937                               vlano_features | tso_features;
2938
2939         /* add support for HW_CSUM on packets with MPLS header */
2940         netdev->mpls_features =  NETIF_F_HW_CSUM;
2941
2942         /* enable features */
2943         netdev->features |= netdev->hw_features;
2944         /* encap and VLAN devices inherit default, csumo and tso features */
2945         netdev->hw_enc_features |= dflt_features | csumo_features |
2946                                    tso_features;
2947         netdev->vlan_features |= dflt_features | csumo_features |
2948                                  tso_features;
2949 }
2950
2951 /**
2952  * ice_cfg_netdev - Allocate, configure and register a netdev
2953  * @vsi: the VSI associated with the new netdev
2954  *
2955  * Returns 0 on success, negative value on failure
2956  */
2957 static int ice_cfg_netdev(struct ice_vsi *vsi)
2958 {
2959         struct ice_pf *pf = vsi->back;
2960         struct ice_netdev_priv *np;
2961         struct net_device *netdev;
2962         u8 mac_addr[ETH_ALEN];
2963         int err;
2964
2965         err = ice_devlink_create_port(vsi);
2966         if (err)
2967                 return err;
2968
2969         netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
2970                                     vsi->alloc_rxq);
2971         if (!netdev) {
2972                 err = -ENOMEM;
2973                 goto err_destroy_devlink_port;
2974         }
2975
2976         vsi->netdev = netdev;
2977         np = netdev_priv(netdev);
2978         np->vsi = vsi;
2979
2980         ice_set_netdev_features(netdev);
2981
2982         ice_set_ops(netdev);
2983
2984         if (vsi->type == ICE_VSI_PF) {
2985                 SET_NETDEV_DEV(netdev, ice_pf_to_dev(pf));
2986                 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2987                 ether_addr_copy(netdev->dev_addr, mac_addr);
2988                 ether_addr_copy(netdev->perm_addr, mac_addr);
2989         }
2990
2991         netdev->priv_flags |= IFF_UNICAST_FLT;
2992
2993         /* Setup netdev TC information */
2994         ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
2995
2996         /* setup watchdog timeout value to be 5 second */
2997         netdev->watchdog_timeo = 5 * HZ;
2998
2999         netdev->min_mtu = ETH_MIN_MTU;
3000         netdev->max_mtu = ICE_MAX_MTU;
3001
3002         err = register_netdev(vsi->netdev);
3003         if (err)
3004                 goto err_free_netdev;
3005
3006         devlink_port_type_eth_set(&vsi->devlink_port, vsi->netdev);
3007
3008         netif_carrier_off(vsi->netdev);
3009
3010         /* make sure transmit queues start off as stopped */
3011         netif_tx_stop_all_queues(vsi->netdev);
3012
3013         return 0;
3014
3015 err_free_netdev:
3016         free_netdev(vsi->netdev);
3017         vsi->netdev = NULL;
3018 err_destroy_devlink_port:
3019         ice_devlink_destroy_port(vsi);
3020         return err;
3021 }
3022
3023 /**
3024  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3025  * @lut: Lookup table
3026  * @rss_table_size: Lookup table size
3027  * @rss_size: Range of queue number for hashing
3028  */
3029 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3030 {
3031         u16 i;
3032
3033         for (i = 0; i < rss_table_size; i++)
3034                 lut[i] = i % rss_size;
3035 }
3036
3037 /**
3038  * ice_pf_vsi_setup - Set up a PF VSI
3039  * @pf: board private structure
3040  * @pi: pointer to the port_info instance
3041  *
3042  * Returns pointer to the successfully allocated VSI software struct
3043  * on success, otherwise returns NULL on failure.
3044  */
3045 static struct ice_vsi *
3046 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3047 {
3048         return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
3049 }
3050
3051 /**
3052  * ice_ctrl_vsi_setup - Set up a control VSI
3053  * @pf: board private structure
3054  * @pi: pointer to the port_info instance
3055  *
3056  * Returns pointer to the successfully allocated VSI software struct
3057  * on success, otherwise returns NULL on failure.
3058  */
3059 static struct ice_vsi *
3060 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3061 {
3062         return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, ICE_INVAL_VFID);
3063 }
3064
3065 /**
3066  * ice_lb_vsi_setup - Set up a loopback VSI
3067  * @pf: board private structure
3068  * @pi: pointer to the port_info instance
3069  *
3070  * Returns pointer to the successfully allocated VSI software struct
3071  * on success, otherwise returns NULL on failure.
3072  */
3073 struct ice_vsi *
3074 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3075 {
3076         return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
3077 }
3078
3079 /**
3080  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3081  * @netdev: network interface to be adjusted
3082  * @proto: unused protocol
3083  * @vid: VLAN ID to be added
3084  *
3085  * net_device_ops implementation for adding VLAN IDs
3086  */
3087 static int
3088 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
3089                     u16 vid)
3090 {
3091         struct ice_netdev_priv *np = netdev_priv(netdev);
3092         struct ice_vsi *vsi = np->vsi;
3093         int ret;
3094
3095         if (vid >= VLAN_N_VID) {
3096                 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
3097                            vid, VLAN_N_VID);
3098                 return -EINVAL;
3099         }
3100
3101         if (vsi->info.pvid)
3102                 return -EINVAL;
3103
3104         /* VLAN 0 is added by default during load/reset */
3105         if (!vid)
3106                 return 0;
3107
3108         /* Enable VLAN pruning when a VLAN other than 0 is added */
3109         if (!ice_vsi_is_vlan_pruning_ena(vsi)) {
3110                 ret = ice_cfg_vlan_pruning(vsi, true, false);
3111                 if (ret)
3112                         return ret;
3113         }
3114
3115         /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3116          * packets aren't pruned by the device's internal switch on Rx
3117          */
3118         ret = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3119         if (!ret)
3120                 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3121
3122         return ret;
3123 }
3124
3125 /**
3126  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3127  * @netdev: network interface to be adjusted
3128  * @proto: unused protocol
3129  * @vid: VLAN ID to be removed
3130  *
3131  * net_device_ops implementation for removing VLAN IDs
3132  */
3133 static int
3134 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
3135                      u16 vid)
3136 {
3137         struct ice_netdev_priv *np = netdev_priv(netdev);
3138         struct ice_vsi *vsi = np->vsi;
3139         int ret;
3140
3141         if (vsi->info.pvid)
3142                 return -EINVAL;
3143
3144         /* don't allow removal of VLAN 0 */
3145         if (!vid)
3146                 return 0;
3147
3148         /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3149          * information
3150          */
3151         ret = ice_vsi_kill_vlan(vsi, vid);
3152         if (ret)
3153                 return ret;
3154
3155         /* Disable pruning when VLAN 0 is the only VLAN rule */
3156         if (vsi->num_vlan == 1 && ice_vsi_is_vlan_pruning_ena(vsi))
3157                 ret = ice_cfg_vlan_pruning(vsi, false, false);
3158
3159         set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3160         return ret;
3161 }
3162
3163 /**
3164  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3165  * @pf: board private structure
3166  *
3167  * Returns 0 on success, negative value on failure
3168  */
3169 static int ice_setup_pf_sw(struct ice_pf *pf)
3170 {
3171         struct ice_vsi *vsi;
3172         int status = 0;
3173
3174         if (ice_is_reset_in_progress(pf->state))
3175                 return -EBUSY;
3176
3177         vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3178         if (!vsi)
3179                 return -ENOMEM;
3180
3181         status = ice_cfg_netdev(vsi);
3182         if (status) {
3183                 status = -ENODEV;
3184                 goto unroll_vsi_setup;
3185         }
3186         /* netdev has to be configured before setting frame size */
3187         ice_vsi_cfg_frame_size(vsi);
3188
3189         /* Setup DCB netlink interface */
3190         ice_dcbnl_setup(vsi);
3191
3192         /* registering the NAPI handler requires both the queues and
3193          * netdev to be created, which are done in ice_pf_vsi_setup()
3194          * and ice_cfg_netdev() respectively
3195          */
3196         ice_napi_add(vsi);
3197
3198         status = ice_set_cpu_rx_rmap(vsi);
3199         if (status) {
3200                 dev_err(ice_pf_to_dev(pf), "Failed to set CPU Rx map VSI %d error %d\n",
3201                         vsi->vsi_num, status);
3202                 status = -EINVAL;
3203                 goto unroll_napi_add;
3204         }
3205         status = ice_init_mac_fltr(pf);
3206         if (status)
3207                 goto free_cpu_rx_map;
3208
3209         return status;
3210
3211 free_cpu_rx_map:
3212         ice_free_cpu_rx_rmap(vsi);
3213
3214 unroll_napi_add:
3215         if (vsi) {
3216                 ice_napi_del(vsi);
3217                 if (vsi->netdev) {
3218                         if (vsi->netdev->reg_state == NETREG_REGISTERED)
3219                                 unregister_netdev(vsi->netdev);
3220                         free_netdev(vsi->netdev);
3221                         vsi->netdev = NULL;
3222                 }
3223         }
3224
3225 unroll_vsi_setup:
3226         ice_vsi_release(vsi);
3227         return status;
3228 }
3229
3230 /**
3231  * ice_get_avail_q_count - Get count of queues in use
3232  * @pf_qmap: bitmap to get queue use count from
3233  * @lock: pointer to a mutex that protects access to pf_qmap
3234  * @size: size of the bitmap
3235  */
3236 static u16
3237 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3238 {
3239         unsigned long bit;
3240         u16 count = 0;
3241
3242         mutex_lock(lock);
3243         for_each_clear_bit(bit, pf_qmap, size)
3244                 count++;
3245         mutex_unlock(lock);
3246
3247         return count;
3248 }
3249
3250 /**
3251  * ice_get_avail_txq_count - Get count of Tx queues in use
3252  * @pf: pointer to an ice_pf instance
3253  */
3254 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3255 {
3256         return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3257                                      pf->max_pf_txqs);
3258 }
3259
3260 /**
3261  * ice_get_avail_rxq_count - Get count of Rx queues in use
3262  * @pf: pointer to an ice_pf instance
3263  */
3264 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3265 {
3266         return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3267                                      pf->max_pf_rxqs);
3268 }
3269
3270 /**
3271  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3272  * @pf: board private structure to initialize
3273  */
3274 static void ice_deinit_pf(struct ice_pf *pf)
3275 {
3276         ice_service_task_stop(pf);
3277         mutex_destroy(&pf->sw_mutex);
3278         mutex_destroy(&pf->tc_mutex);
3279         mutex_destroy(&pf->avail_q_mutex);
3280
3281         if (pf->avail_txqs) {
3282                 bitmap_free(pf->avail_txqs);
3283                 pf->avail_txqs = NULL;
3284         }
3285
3286         if (pf->avail_rxqs) {
3287                 bitmap_free(pf->avail_rxqs);
3288                 pf->avail_rxqs = NULL;
3289         }
3290 }
3291
3292 /**
3293  * ice_set_pf_caps - set PFs capability flags
3294  * @pf: pointer to the PF instance
3295  */
3296 static void ice_set_pf_caps(struct ice_pf *pf)
3297 {
3298         struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3299
3300         clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3301         if (func_caps->common_cap.dcb)
3302                 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3303         clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3304         if (func_caps->common_cap.sr_iov_1_1) {
3305                 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3306                 pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
3307                                               ICE_MAX_VF_COUNT);
3308         }
3309         clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3310         if (func_caps->common_cap.rss_table_size)
3311                 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3312
3313         clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3314         if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3315                 u16 unused;
3316
3317                 /* ctrl_vsi_idx will be set to a valid value when flow director
3318                  * is setup by ice_init_fdir
3319                  */
3320                 pf->ctrl_vsi_idx = ICE_NO_VSI;
3321                 set_bit(ICE_FLAG_FD_ENA, pf->flags);
3322                 /* force guaranteed filter pool for PF */
3323                 ice_alloc_fd_guar_item(&pf->hw, &unused,
3324                                        func_caps->fd_fltr_guar);
3325                 /* force shared filter pool for PF */
3326                 ice_alloc_fd_shrd_item(&pf->hw, &unused,
3327                                        func_caps->fd_fltr_best_effort);
3328         }
3329
3330         pf->max_pf_txqs = func_caps->common_cap.num_txq;
3331         pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3332 }
3333
3334 /**
3335  * ice_init_pf - Initialize general software structures (struct ice_pf)
3336  * @pf: board private structure to initialize
3337  */
3338 static int ice_init_pf(struct ice_pf *pf)
3339 {
3340         ice_set_pf_caps(pf);
3341
3342         mutex_init(&pf->sw_mutex);
3343         mutex_init(&pf->tc_mutex);
3344
3345         INIT_HLIST_HEAD(&pf->aq_wait_list);
3346         spin_lock_init(&pf->aq_wait_lock);
3347         init_waitqueue_head(&pf->aq_wait_queue);
3348
3349         /* setup service timer and periodic service task */
3350         timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3351         pf->serv_tmr_period = HZ;
3352         INIT_WORK(&pf->serv_task, ice_service_task);
3353         clear_bit(__ICE_SERVICE_SCHED, pf->state);
3354
3355         mutex_init(&pf->avail_q_mutex);
3356         pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3357         if (!pf->avail_txqs)
3358                 return -ENOMEM;
3359
3360         pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3361         if (!pf->avail_rxqs) {
3362                 devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
3363                 pf->avail_txqs = NULL;
3364                 return -ENOMEM;
3365         }
3366
3367         return 0;
3368 }
3369
3370 /**
3371  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3372  * @pf: board private structure
3373  *
3374  * compute the number of MSIX vectors required (v_budget) and request from
3375  * the OS. Return the number of vectors reserved or negative on failure
3376  */
3377 static int ice_ena_msix_range(struct ice_pf *pf)
3378 {
3379         struct device *dev = ice_pf_to_dev(pf);
3380         int v_left, v_actual, v_budget = 0;
3381         int needed, err, i;
3382
3383         v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3384
3385         /* reserve one vector for miscellaneous handler */
3386         needed = 1;
3387         if (v_left < needed)
3388                 goto no_hw_vecs_left_err;
3389         v_budget += needed;
3390         v_left -= needed;
3391
3392         /* reserve vectors for LAN traffic */
3393         needed = min_t(int, num_online_cpus(), v_left);
3394         if (v_left < needed)
3395                 goto no_hw_vecs_left_err;
3396         pf->num_lan_msix = needed;
3397         v_budget += needed;
3398         v_left -= needed;
3399
3400         /* reserve one vector for flow director */
3401         if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3402                 needed = ICE_FDIR_MSIX;
3403                 if (v_left < needed)
3404                         goto no_hw_vecs_left_err;
3405                 v_budget += needed;
3406                 v_left -= needed;
3407         }
3408
3409         pf->msix_entries = devm_kcalloc(dev, v_budget,
3410                                         sizeof(*pf->msix_entries), GFP_KERNEL);
3411
3412         if (!pf->msix_entries) {
3413                 err = -ENOMEM;
3414                 goto exit_err;
3415         }
3416
3417         for (i = 0; i < v_budget; i++)
3418                 pf->msix_entries[i].entry = i;
3419
3420         /* actually reserve the vectors */
3421         v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3422                                          ICE_MIN_MSIX, v_budget);
3423
3424         if (v_actual < 0) {
3425                 dev_err(dev, "unable to reserve MSI-X vectors\n");
3426                 err = v_actual;
3427                 goto msix_err;
3428         }
3429
3430         if (v_actual < v_budget) {
3431                 dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
3432                          v_budget, v_actual);
3433 /* 2 vectors each for LAN and RDMA (traffic + OICR), one for flow director */
3434 #define ICE_MIN_LAN_VECS 2
3435 #define ICE_MIN_RDMA_VECS 2
3436 #define ICE_MIN_VECS (ICE_MIN_LAN_VECS + ICE_MIN_RDMA_VECS + 1)
3437
3438                 if (v_actual < ICE_MIN_LAN_VECS) {
3439                         /* error if we can't get minimum vectors */
3440                         pci_disable_msix(pf->pdev);
3441                         err = -ERANGE;
3442                         goto msix_err;
3443                 } else {
3444                         pf->num_lan_msix = ICE_MIN_LAN_VECS;
3445                 }
3446         }
3447
3448         return v_actual;
3449
3450 msix_err:
3451         devm_kfree(dev, pf->msix_entries);
3452         goto exit_err;
3453
3454 no_hw_vecs_left_err:
3455         dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
3456                 needed, v_left);
3457         err = -ERANGE;
3458 exit_err:
3459         pf->num_lan_msix = 0;
3460         return err;
3461 }
3462
3463 /**
3464  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3465  * @pf: board private structure
3466  */
3467 static void ice_dis_msix(struct ice_pf *pf)
3468 {
3469         pci_disable_msix(pf->pdev);
3470         devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
3471         pf->msix_entries = NULL;
3472 }
3473
3474 /**
3475  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3476  * @pf: board private structure
3477  */
3478 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3479 {
3480         ice_dis_msix(pf);
3481
3482         if (pf->irq_tracker) {
3483                 devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
3484                 pf->irq_tracker = NULL;
3485         }
3486 }
3487
3488 /**
3489  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3490  * @pf: board private structure to initialize
3491  */
3492 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3493 {
3494         int vectors;
3495
3496         vectors = ice_ena_msix_range(pf);
3497
3498         if (vectors < 0)
3499                 return vectors;
3500
3501         /* set up vector assignment tracking */
3502         pf->irq_tracker =
3503                 devm_kzalloc(ice_pf_to_dev(pf), sizeof(*pf->irq_tracker) +
3504                              (sizeof(u16) * vectors), GFP_KERNEL);
3505         if (!pf->irq_tracker) {
3506                 ice_dis_msix(pf);
3507                 return -ENOMEM;
3508         }
3509
3510         /* populate SW interrupts pool with number of OS granted IRQs. */
3511         pf->num_avail_sw_msix = (u16)vectors;
3512         pf->irq_tracker->num_entries = (u16)vectors;
3513         pf->irq_tracker->end = pf->irq_tracker->num_entries;
3514
3515         return 0;
3516 }
3517
3518 /**
3519  * ice_is_wol_supported - get NVM state of WoL
3520  * @pf: board private structure
3521  *
3522  * Check if WoL is supported based on the HW configuration.
3523  * Returns true if NVM supports and enables WoL for this port, false otherwise
3524  */
3525 bool ice_is_wol_supported(struct ice_pf *pf)
3526 {
3527         struct ice_hw *hw = &pf->hw;
3528         u16 wol_ctrl;
3529
3530         /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3531          * word) indicates WoL is not supported on the corresponding PF ID.
3532          */
3533         if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3534                 return false;
3535
3536         return !(BIT(hw->pf_id) & wol_ctrl);
3537 }
3538
3539 /**
3540  * ice_vsi_recfg_qs - Change the number of queues on a VSI
3541  * @vsi: VSI being changed
3542  * @new_rx: new number of Rx queues
3543  * @new_tx: new number of Tx queues
3544  *
3545  * Only change the number of queues if new_tx, or new_rx is non-0.
3546  *
3547  * Returns 0 on success.
3548  */
3549 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
3550 {
3551         struct ice_pf *pf = vsi->back;
3552         int err = 0, timeout = 50;
3553
3554         if (!new_rx && !new_tx)
3555                 return -EINVAL;
3556
3557         while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
3558                 timeout--;
3559                 if (!timeout)
3560                         return -EBUSY;
3561                 usleep_range(1000, 2000);
3562         }
3563
3564         if (new_tx)
3565                 vsi->req_txq = (u16)new_tx;
3566         if (new_rx)
3567                 vsi->req_rxq = (u16)new_rx;
3568
3569         /* set for the next time the netdev is started */
3570         if (!netif_running(vsi->netdev)) {
3571                 ice_vsi_rebuild(vsi, false);
3572                 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
3573                 goto done;
3574         }
3575
3576         ice_vsi_close(vsi);
3577         ice_vsi_rebuild(vsi, false);
3578         ice_pf_dcb_recfg(pf);
3579         ice_vsi_open(vsi);
3580 done:
3581         clear_bit(__ICE_CFG_BUSY, pf->state);
3582         return err;
3583 }
3584
3585 /**
3586  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
3587  * @pf: PF to configure
3588  *
3589  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
3590  * VSI can still Tx/Rx VLAN tagged packets.
3591  */
3592 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
3593 {
3594         struct ice_vsi *vsi = ice_get_main_vsi(pf);
3595         struct ice_vsi_ctx *ctxt;
3596         enum ice_status status;
3597         struct ice_hw *hw;
3598
3599         if (!vsi)
3600                 return;
3601
3602         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
3603         if (!ctxt)
3604                 return;
3605
3606         hw = &pf->hw;
3607         ctxt->info = vsi->info;
3608
3609         ctxt->info.valid_sections =
3610                 cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
3611                             ICE_AQ_VSI_PROP_SECURITY_VALID |
3612                             ICE_AQ_VSI_PROP_SW_VALID);
3613
3614         /* disable VLAN anti-spoof */
3615         ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3616                                   ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3617
3618         /* disable VLAN pruning and keep all other settings */
3619         ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
3620
3621         /* allow all VLANs on Tx and don't strip on Rx */
3622         ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL |
3623                 ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3624
3625         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
3626         if (status) {
3627                 dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %s aq_err %s\n",
3628                         ice_stat_str(status),
3629                         ice_aq_str(hw->adminq.sq_last_status));
3630         } else {
3631                 vsi->info.sec_flags = ctxt->info.sec_flags;
3632                 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
3633                 vsi->info.vlan_flags = ctxt->info.vlan_flags;
3634         }
3635
3636         kfree(ctxt);
3637 }
3638
3639 /**
3640  * ice_log_pkg_init - log result of DDP package load
3641  * @hw: pointer to hardware info
3642  * @status: status of package load
3643  */
3644 static void
3645 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status)
3646 {
3647         struct ice_pf *pf = (struct ice_pf *)hw->back;
3648         struct device *dev = ice_pf_to_dev(pf);
3649
3650         switch (*status) {
3651         case ICE_SUCCESS:
3652                 /* The package download AdminQ command returned success because
3653                  * this download succeeded or ICE_ERR_AQ_NO_WORK since there is
3654                  * already a package loaded on the device.
3655                  */
3656                 if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
3657                     hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
3658                     hw->pkg_ver.update == hw->active_pkg_ver.update &&
3659                     hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
3660                     !memcmp(hw->pkg_name, hw->active_pkg_name,
3661                             sizeof(hw->pkg_name))) {
3662                         if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST)
3663                                 dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
3664                                          hw->active_pkg_name,
3665                                          hw->active_pkg_ver.major,
3666                                          hw->active_pkg_ver.minor,
3667                                          hw->active_pkg_ver.update,
3668                                          hw->active_pkg_ver.draft);
3669                         else
3670                                 dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
3671                                          hw->active_pkg_name,
3672                                          hw->active_pkg_ver.major,
3673                                          hw->active_pkg_ver.minor,
3674                                          hw->active_pkg_ver.update,
3675                                          hw->active_pkg_ver.draft);
3676                 } else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
3677                            hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
3678                         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",
3679                                 hw->active_pkg_name,
3680                                 hw->active_pkg_ver.major,
3681                                 hw->active_pkg_ver.minor,
3682                                 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3683                         *status = ICE_ERR_NOT_SUPPORTED;
3684                 } else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3685                            hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
3686                         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",
3687                                  hw->active_pkg_name,
3688                                  hw->active_pkg_ver.major,
3689                                  hw->active_pkg_ver.minor,
3690                                  hw->active_pkg_ver.update,
3691                                  hw->active_pkg_ver.draft,
3692                                  hw->pkg_name,
3693                                  hw->pkg_ver.major,
3694                                  hw->pkg_ver.minor,
3695                                  hw->pkg_ver.update,
3696                                  hw->pkg_ver.draft);
3697                 } else {
3698                         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");
3699                         *status = ICE_ERR_NOT_SUPPORTED;
3700                 }
3701                 break;
3702         case ICE_ERR_FW_DDP_MISMATCH:
3703                 dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package.  Please update the device's NVM.  Entering safe mode.\n");
3704                 break;
3705         case ICE_ERR_BUF_TOO_SHORT:
3706         case ICE_ERR_CFG:
3707                 dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
3708                 break;
3709         case ICE_ERR_NOT_SUPPORTED:
3710                 /* Package File version not supported */
3711                 if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ ||
3712                     (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3713                      hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR))
3714                         dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
3715                 else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ ||
3716                          (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3717                           hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR))
3718                         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",
3719                                 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3720                 break;
3721         case ICE_ERR_AQ_ERROR:
3722                 switch (hw->pkg_dwnld_status) {
3723                 case ICE_AQ_RC_ENOSEC:
3724                 case ICE_AQ_RC_EBADSIG:
3725                         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");
3726                         return;
3727                 case ICE_AQ_RC_ESVN:
3728                         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");
3729                         return;
3730                 case ICE_AQ_RC_EBADMAN:
3731                 case ICE_AQ_RC_EBADBUF:
3732                         dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
3733                         /* poll for reset to complete */
3734                         if (ice_check_reset(hw))
3735                                 dev_err(dev, "Error resetting device. Please reload the driver\n");
3736                         return;
3737                 default:
3738                         break;
3739                 }
3740                 fallthrough;
3741         default:
3742                 dev_err(dev, "An unknown error (%d) occurred when loading the DDP package.  Entering Safe Mode.\n",
3743                         *status);
3744                 break;
3745         }
3746 }
3747
3748 /**
3749  * ice_load_pkg - load/reload the DDP Package file
3750  * @firmware: firmware structure when firmware requested or NULL for reload
3751  * @pf: pointer to the PF instance
3752  *
3753  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
3754  * initialize HW tables.
3755  */
3756 static void
3757 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
3758 {
3759         enum ice_status status = ICE_ERR_PARAM;
3760         struct device *dev = ice_pf_to_dev(pf);
3761         struct ice_hw *hw = &pf->hw;
3762
3763         /* Load DDP Package */
3764         if (firmware && !hw->pkg_copy) {
3765                 status = ice_copy_and_init_pkg(hw, firmware->data,
3766                                                firmware->size);
3767                 ice_log_pkg_init(hw, &status);
3768         } else if (!firmware && hw->pkg_copy) {
3769                 /* Reload package during rebuild after CORER/GLOBR reset */
3770                 status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
3771                 ice_log_pkg_init(hw, &status);
3772         } else {
3773                 dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
3774         }
3775
3776         if (status) {
3777                 /* Safe Mode */
3778                 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3779                 return;
3780         }
3781
3782         /* Successful download package is the precondition for advanced
3783          * features, hence setting the ICE_FLAG_ADV_FEATURES flag
3784          */
3785         set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3786 }
3787
3788 /**
3789  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
3790  * @pf: pointer to the PF structure
3791  *
3792  * There is no error returned here because the driver should be able to handle
3793  * 128 Byte cache lines, so we only print a warning in case issues are seen,
3794  * specifically with Tx.
3795  */
3796 static void ice_verify_cacheline_size(struct ice_pf *pf)
3797 {
3798         if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
3799                 dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
3800                          ICE_CACHE_LINE_BYTES);
3801 }
3802
3803 /**
3804  * ice_send_version - update firmware with driver version
3805  * @pf: PF struct
3806  *
3807  * Returns ICE_SUCCESS on success, else error code
3808  */
3809 static enum ice_status ice_send_version(struct ice_pf *pf)
3810 {
3811         struct ice_driver_ver dv;
3812
3813         dv.major_ver = 0xff;
3814         dv.minor_ver = 0xff;
3815         dv.build_ver = 0xff;
3816         dv.subbuild_ver = 0;
3817         strscpy((char *)dv.driver_string, UTS_RELEASE,
3818                 sizeof(dv.driver_string));
3819         return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
3820 }
3821
3822 /**
3823  * ice_init_fdir - Initialize flow director VSI and configuration
3824  * @pf: pointer to the PF instance
3825  *
3826  * returns 0 on success, negative on error
3827  */
3828 static int ice_init_fdir(struct ice_pf *pf)
3829 {
3830         struct device *dev = ice_pf_to_dev(pf);
3831         struct ice_vsi *ctrl_vsi;
3832         int err;
3833
3834         /* Side Band Flow Director needs to have a control VSI.
3835          * Allocate it and store it in the PF.
3836          */
3837         ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
3838         if (!ctrl_vsi) {
3839                 dev_dbg(dev, "could not create control VSI\n");
3840                 return -ENOMEM;
3841         }
3842
3843         err = ice_vsi_open_ctrl(ctrl_vsi);
3844         if (err) {
3845                 dev_dbg(dev, "could not open control VSI\n");
3846                 goto err_vsi_open;
3847         }
3848
3849         mutex_init(&pf->hw.fdir_fltr_lock);
3850
3851         err = ice_fdir_create_dflt_rules(pf);
3852         if (err)
3853                 goto err_fdir_rule;
3854
3855         return 0;
3856
3857 err_fdir_rule:
3858         ice_fdir_release_flows(&pf->hw);
3859         ice_vsi_close(ctrl_vsi);
3860 err_vsi_open:
3861         ice_vsi_release(ctrl_vsi);
3862         if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
3863                 pf->vsi[pf->ctrl_vsi_idx] = NULL;
3864                 pf->ctrl_vsi_idx = ICE_NO_VSI;
3865         }
3866         return err;
3867 }
3868
3869 /**
3870  * ice_get_opt_fw_name - return optional firmware file name or NULL
3871  * @pf: pointer to the PF instance
3872  */
3873 static char *ice_get_opt_fw_name(struct ice_pf *pf)
3874 {
3875         /* Optional firmware name same as default with additional dash
3876          * followed by a EUI-64 identifier (PCIe Device Serial Number)
3877          */
3878         struct pci_dev *pdev = pf->pdev;
3879         char *opt_fw_filename;
3880         u64 dsn;
3881
3882         /* Determine the name of the optional file using the DSN (two
3883          * dwords following the start of the DSN Capability).
3884          */
3885         dsn = pci_get_dsn(pdev);
3886         if (!dsn)
3887                 return NULL;
3888
3889         opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
3890         if (!opt_fw_filename)
3891                 return NULL;
3892
3893         snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
3894                  ICE_DDP_PKG_PATH, dsn);
3895
3896         return opt_fw_filename;
3897 }
3898
3899 /**
3900  * ice_request_fw - Device initialization routine
3901  * @pf: pointer to the PF instance
3902  */
3903 static void ice_request_fw(struct ice_pf *pf)
3904 {
3905         char *opt_fw_filename = ice_get_opt_fw_name(pf);
3906         const struct firmware *firmware = NULL;
3907         struct device *dev = ice_pf_to_dev(pf);
3908         int err = 0;
3909
3910         /* optional device-specific DDP (if present) overrides the default DDP
3911          * package file. kernel logs a debug message if the file doesn't exist,
3912          * and warning messages for other errors.
3913          */
3914         if (opt_fw_filename) {
3915                 err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
3916                 if (err) {
3917                         kfree(opt_fw_filename);
3918                         goto dflt_pkg_load;
3919                 }
3920
3921                 /* request for firmware was successful. Download to device */
3922                 ice_load_pkg(firmware, pf);
3923                 kfree(opt_fw_filename);
3924                 release_firmware(firmware);
3925                 return;
3926         }
3927
3928 dflt_pkg_load:
3929         err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
3930         if (err) {
3931                 dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
3932                 return;
3933         }
3934
3935         /* request for firmware was successful. Download to device */
3936         ice_load_pkg(firmware, pf);
3937         release_firmware(firmware);
3938 }
3939
3940 /**
3941  * ice_print_wake_reason - show the wake up cause in the log
3942  * @pf: pointer to the PF struct
3943  */
3944 static void ice_print_wake_reason(struct ice_pf *pf)
3945 {
3946         u32 wus = pf->wakeup_reason;
3947         const char *wake_str;
3948
3949         /* if no wake event, nothing to print */
3950         if (!wus)
3951                 return;
3952
3953         if (wus & PFPM_WUS_LNKC_M)
3954                 wake_str = "Link\n";
3955         else if (wus & PFPM_WUS_MAG_M)
3956                 wake_str = "Magic Packet\n";
3957         else if (wus & PFPM_WUS_MNG_M)
3958                 wake_str = "Management\n";
3959         else if (wus & PFPM_WUS_FW_RST_WK_M)
3960                 wake_str = "Firmware Reset\n";
3961         else
3962                 wake_str = "Unknown\n";
3963
3964         dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
3965 }
3966
3967 /**
3968  * ice_probe - Device initialization routine
3969  * @pdev: PCI device information struct
3970  * @ent: entry in ice_pci_tbl
3971  *
3972  * Returns 0 on success, negative on failure
3973  */
3974 static int
3975 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
3976 {
3977         struct device *dev = &pdev->dev;
3978         struct ice_pf *pf;
3979         struct ice_hw *hw;
3980         int i, err;
3981
3982         /* this driver uses devres, see
3983          * Documentation/driver-api/driver-model/devres.rst
3984          */
3985         err = pcim_enable_device(pdev);
3986         if (err)
3987                 return err;
3988
3989         err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3990         if (err) {
3991                 dev_err(dev, "BAR0 I/O map error %d\n", err);
3992                 return err;
3993         }
3994
3995         pf = ice_allocate_pf(dev);
3996         if (!pf)
3997                 return -ENOMEM;
3998
3999         /* set up for high or low DMA */
4000         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4001         if (err)
4002                 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
4003         if (err) {
4004                 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4005                 return err;
4006         }
4007
4008         pci_enable_pcie_error_reporting(pdev);
4009         pci_set_master(pdev);
4010
4011         pf->pdev = pdev;
4012         pci_set_drvdata(pdev, pf);
4013         set_bit(__ICE_DOWN, pf->state);
4014         /* Disable service task until DOWN bit is cleared */
4015         set_bit(__ICE_SERVICE_DIS, pf->state);
4016
4017         hw = &pf->hw;
4018         hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4019         pci_save_state(pdev);
4020
4021         hw->back = pf;
4022         hw->vendor_id = pdev->vendor;
4023         hw->device_id = pdev->device;
4024         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4025         hw->subsystem_vendor_id = pdev->subsystem_vendor;
4026         hw->subsystem_device_id = pdev->subsystem_device;
4027         hw->bus.device = PCI_SLOT(pdev->devfn);
4028         hw->bus.func = PCI_FUNC(pdev->devfn);
4029         ice_set_ctrlq_len(hw);
4030
4031         pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4032
4033         err = ice_devlink_register(pf);
4034         if (err) {
4035                 dev_err(dev, "ice_devlink_register failed: %d\n", err);
4036                 goto err_exit_unroll;
4037         }
4038
4039 #ifndef CONFIG_DYNAMIC_DEBUG
4040         if (debug < -1)
4041                 hw->debug_mask = debug;
4042 #endif
4043
4044         err = ice_init_hw(hw);
4045         if (err) {
4046                 dev_err(dev, "ice_init_hw failed: %d\n", err);
4047                 err = -EIO;
4048                 goto err_exit_unroll;
4049         }
4050
4051         ice_request_fw(pf);
4052
4053         /* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4054          * set in pf->state, which will cause ice_is_safe_mode to return
4055          * true
4056          */
4057         if (ice_is_safe_mode(pf)) {
4058                 dev_err(dev, "Package download failed. Advanced features disabled - Device now in Safe Mode\n");
4059                 /* we already got function/device capabilities but these don't
4060                  * reflect what the driver needs to do in safe mode. Instead of
4061                  * adding conditional logic everywhere to ignore these
4062                  * device/function capabilities, override them.
4063                  */
4064                 ice_set_safe_mode_caps(hw);
4065         }
4066
4067         err = ice_init_pf(pf);
4068         if (err) {
4069                 dev_err(dev, "ice_init_pf failed: %d\n", err);
4070                 goto err_init_pf_unroll;
4071         }
4072
4073         ice_devlink_init_regions(pf);
4074
4075         pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4076         pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4077         pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4078         pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4079         i = 0;
4080         if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4081                 pf->hw.udp_tunnel_nic.tables[i].n_entries =
4082                         pf->hw.tnl.valid_count[TNL_VXLAN];
4083                 pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4084                         UDP_TUNNEL_TYPE_VXLAN;
4085                 i++;
4086         }
4087         if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4088                 pf->hw.udp_tunnel_nic.tables[i].n_entries =
4089                         pf->hw.tnl.valid_count[TNL_GENEVE];
4090                 pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4091                         UDP_TUNNEL_TYPE_GENEVE;
4092                 i++;
4093         }
4094
4095         pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4096         if (!pf->num_alloc_vsi) {
4097                 err = -EIO;
4098                 goto err_init_pf_unroll;
4099         }
4100         if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4101                 dev_warn(&pf->pdev->dev,
4102                          "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4103                          pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4104                 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4105         }
4106
4107         pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4108                                GFP_KERNEL);
4109         if (!pf->vsi) {
4110                 err = -ENOMEM;
4111                 goto err_init_pf_unroll;
4112         }
4113
4114         err = ice_init_interrupt_scheme(pf);
4115         if (err) {
4116                 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4117                 err = -EIO;
4118                 goto err_init_vsi_unroll;
4119         }
4120
4121         /* In case of MSIX we are going to setup the misc vector right here
4122          * to handle admin queue events etc. In case of legacy and MSI
4123          * the misc functionality and queue processing is combined in
4124          * the same vector and that gets setup at open.
4125          */
4126         err = ice_req_irq_msix_misc(pf);
4127         if (err) {
4128                 dev_err(dev, "setup of misc vector failed: %d\n", err);
4129                 goto err_init_interrupt_unroll;
4130         }
4131
4132         /* create switch struct for the switch element created by FW on boot */
4133         pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4134         if (!pf->first_sw) {
4135                 err = -ENOMEM;
4136                 goto err_msix_misc_unroll;
4137         }
4138
4139         if (hw->evb_veb)
4140                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4141         else
4142                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4143
4144         pf->first_sw->pf = pf;
4145
4146         /* record the sw_id available for later use */
4147         pf->first_sw->sw_id = hw->port_info->sw_id;
4148
4149         err = ice_setup_pf_sw(pf);
4150         if (err) {
4151                 dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4152                 goto err_alloc_sw_unroll;
4153         }
4154
4155         clear_bit(__ICE_SERVICE_DIS, pf->state);
4156
4157         /* tell the firmware we are up */
4158         err = ice_send_version(pf);
4159         if (err) {
4160                 dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4161                         UTS_RELEASE, err);
4162                 goto err_send_version_unroll;
4163         }
4164
4165         /* since everything is good, start the service timer */
4166         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4167
4168         err = ice_init_link_events(pf->hw.port_info);
4169         if (err) {
4170                 dev_err(dev, "ice_init_link_events failed: %d\n", err);
4171                 goto err_send_version_unroll;
4172         }
4173
4174         err = ice_init_nvm_phy_type(pf->hw.port_info);
4175         if (err) {
4176                 dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4177                 goto err_send_version_unroll;
4178         }
4179
4180         err = ice_update_link_info(pf->hw.port_info);
4181         if (err) {
4182                 dev_err(dev, "ice_update_link_info failed: %d\n", err);
4183                 goto err_send_version_unroll;
4184         }
4185
4186         ice_init_link_dflt_override(pf->hw.port_info);
4187
4188         /* if media available, initialize PHY settings */
4189         if (pf->hw.port_info->phy.link_info.link_info &
4190             ICE_AQ_MEDIA_AVAILABLE) {
4191                 err = ice_init_phy_user_cfg(pf->hw.port_info);
4192                 if (err) {
4193                         dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4194                         goto err_send_version_unroll;
4195                 }
4196
4197                 if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4198                         struct ice_vsi *vsi = ice_get_main_vsi(pf);
4199
4200                         if (vsi)
4201                                 ice_configure_phy(vsi);
4202                 }
4203         } else {
4204                 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4205         }
4206
4207         ice_verify_cacheline_size(pf);
4208
4209         /* Save wakeup reason register for later use */
4210         pf->wakeup_reason = rd32(hw, PFPM_WUS);
4211
4212         /* check for a power management event */
4213         ice_print_wake_reason(pf);
4214
4215         /* clear wake status, all bits */
4216         wr32(hw, PFPM_WUS, U32_MAX);
4217
4218         /* Disable WoL at init, wait for user to enable */
4219         device_set_wakeup_enable(dev, false);
4220
4221         if (ice_is_safe_mode(pf)) {
4222                 ice_set_safe_mode_vlan_cfg(pf);
4223                 goto probe_done;
4224         }
4225
4226         /* initialize DDP driven features */
4227
4228         /* Note: Flow director init failure is non-fatal to load */
4229         if (ice_init_fdir(pf))
4230                 dev_err(dev, "could not initialize flow director\n");
4231
4232         /* Note: DCB init failure is non-fatal to load */
4233         if (ice_init_pf_dcb(pf, false)) {
4234                 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4235                 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4236         } else {
4237                 ice_cfg_lldp_mib_change(&pf->hw, true);
4238         }
4239
4240         /* print PCI link speed and width */
4241         pcie_print_link_status(pf->pdev);
4242
4243 probe_done:
4244         /* ready to go, so clear down state bit */
4245         clear_bit(__ICE_DOWN, pf->state);
4246         return 0;
4247
4248 err_send_version_unroll:
4249         ice_vsi_release_all(pf);
4250 err_alloc_sw_unroll:
4251         set_bit(__ICE_SERVICE_DIS, pf->state);
4252         set_bit(__ICE_DOWN, pf->state);
4253         devm_kfree(dev, pf->first_sw);
4254 err_msix_misc_unroll:
4255         ice_free_irq_msix_misc(pf);
4256 err_init_interrupt_unroll:
4257         ice_clear_interrupt_scheme(pf);
4258 err_init_vsi_unroll:
4259         devm_kfree(dev, pf->vsi);
4260 err_init_pf_unroll:
4261         ice_deinit_pf(pf);
4262         ice_devlink_destroy_regions(pf);
4263         ice_deinit_hw(hw);
4264 err_exit_unroll:
4265         ice_devlink_unregister(pf);
4266         pci_disable_pcie_error_reporting(pdev);
4267         pci_disable_device(pdev);
4268         return err;
4269 }
4270
4271 /**
4272  * ice_set_wake - enable or disable Wake on LAN
4273  * @pf: pointer to the PF struct
4274  *
4275  * Simple helper for WoL control
4276  */
4277 static void ice_set_wake(struct ice_pf *pf)
4278 {
4279         struct ice_hw *hw = &pf->hw;
4280         bool wol = pf->wol_ena;
4281
4282         /* clear wake state, otherwise new wake events won't fire */
4283         wr32(hw, PFPM_WUS, U32_MAX);
4284
4285         /* enable / disable APM wake up, no RMW needed */
4286         wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4287
4288         /* set magic packet filter enabled */
4289         wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4290 }
4291
4292 /**
4293  * ice_setup_magic_mc_wake - setup device to wake on multicast magic packet
4294  * @pf: pointer to the PF struct
4295  *
4296  * Issue firmware command to enable multicast magic wake, making
4297  * sure that any locally administered address (LAA) is used for
4298  * wake, and that PF reset doesn't undo the LAA.
4299  */
4300 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4301 {
4302         struct device *dev = ice_pf_to_dev(pf);
4303         struct ice_hw *hw = &pf->hw;
4304         enum ice_status status;
4305         u8 mac_addr[ETH_ALEN];
4306         struct ice_vsi *vsi;
4307         u8 flags;
4308
4309         if (!pf->wol_ena)
4310                 return;
4311
4312         vsi = ice_get_main_vsi(pf);
4313         if (!vsi)
4314                 return;
4315
4316         /* Get current MAC address in case it's an LAA */
4317         if (vsi->netdev)
4318                 ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4319         else
4320                 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4321
4322         flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4323                 ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4324                 ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4325
4326         status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4327         if (status)
4328                 dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %s aq_err %s\n",
4329                         ice_stat_str(status),
4330                         ice_aq_str(hw->adminq.sq_last_status));
4331 }
4332
4333 /**
4334  * ice_remove - Device removal routine
4335  * @pdev: PCI device information struct
4336  */
4337 static void ice_remove(struct pci_dev *pdev)
4338 {
4339         struct ice_pf *pf = pci_get_drvdata(pdev);
4340         int i;
4341
4342         if (!pf)
4343                 return;
4344
4345         for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4346                 if (!ice_is_reset_in_progress(pf->state))
4347                         break;
4348                 msleep(100);
4349         }
4350
4351         if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4352                 set_bit(__ICE_VF_RESETS_DISABLED, pf->state);
4353                 ice_free_vfs(pf);
4354         }
4355
4356         set_bit(__ICE_DOWN, pf->state);
4357         ice_service_task_stop(pf);
4358
4359         ice_aq_cancel_waiting_tasks(pf);
4360
4361         mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4362         if (!ice_is_safe_mode(pf))
4363                 ice_remove_arfs(pf);
4364         ice_setup_mc_magic_wake(pf);
4365         ice_vsi_release_all(pf);
4366         ice_set_wake(pf);
4367         ice_free_irq_msix_misc(pf);
4368         ice_for_each_vsi(pf, i) {
4369                 if (!pf->vsi[i])
4370                         continue;
4371                 ice_vsi_free_q_vectors(pf->vsi[i]);
4372         }
4373         ice_deinit_pf(pf);
4374         ice_devlink_destroy_regions(pf);
4375         ice_deinit_hw(&pf->hw);
4376         ice_devlink_unregister(pf);
4377
4378         /* Issue a PFR as part of the prescribed driver unload flow.  Do not
4379          * do it via ice_schedule_reset() since there is no need to rebuild
4380          * and the service task is already stopped.
4381          */
4382         ice_reset(&pf->hw, ICE_RESET_PFR);
4383         pci_wait_for_pending_transaction(pdev);
4384         ice_clear_interrupt_scheme(pf);
4385         pci_disable_pcie_error_reporting(pdev);
4386         pci_disable_device(pdev);
4387 }
4388
4389 /**
4390  * ice_shutdown - PCI callback for shutting down device
4391  * @pdev: PCI device information struct
4392  */
4393 static void ice_shutdown(struct pci_dev *pdev)
4394 {
4395         struct ice_pf *pf = pci_get_drvdata(pdev);
4396
4397         ice_remove(pdev);
4398
4399         if (system_state == SYSTEM_POWER_OFF) {
4400                 pci_wake_from_d3(pdev, pf->wol_ena);
4401                 pci_set_power_state(pdev, PCI_D3hot);
4402         }
4403 }
4404
4405 #ifdef CONFIG_PM
4406 /**
4407  * ice_prepare_for_shutdown - prep for PCI shutdown
4408  * @pf: board private structure
4409  *
4410  * Inform or close all dependent features in prep for PCI device shutdown
4411  */
4412 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4413 {
4414         struct ice_hw *hw = &pf->hw;
4415         u32 v;
4416
4417         /* Notify VFs of impending reset */
4418         if (ice_check_sq_alive(hw, &hw->mailboxq))
4419                 ice_vc_notify_reset(pf);
4420
4421         dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4422
4423         /* disable the VSIs and their queues that are not already DOWN */
4424         ice_pf_dis_all_vsi(pf, false);
4425
4426         ice_for_each_vsi(pf, v)
4427                 if (pf->vsi[v])
4428                         pf->vsi[v]->vsi_num = 0;
4429
4430         ice_shutdown_all_ctrlq(hw);
4431 }
4432
4433 /**
4434  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4435  * @pf: board private structure to reinitialize
4436  *
4437  * This routine reinitialize interrupt scheme that was cleared during
4438  * power management suspend callback.
4439  *
4440  * This should be called during resume routine to re-allocate the q_vectors
4441  * and reacquire interrupts.
4442  */
4443 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4444 {
4445         struct device *dev = ice_pf_to_dev(pf);
4446         int ret, v;
4447
4448         /* Since we clear MSIX flag during suspend, we need to
4449          * set it back during resume...
4450          */
4451
4452         ret = ice_init_interrupt_scheme(pf);
4453         if (ret) {
4454                 dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4455                 return ret;
4456         }
4457
4458         /* Remap vectors and rings, after successful re-init interrupts */
4459         ice_for_each_vsi(pf, v) {
4460                 if (!pf->vsi[v])
4461                         continue;
4462
4463                 ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4464                 if (ret)
4465                         goto err_reinit;
4466                 ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4467         }
4468
4469         ret = ice_req_irq_msix_misc(pf);
4470         if (ret) {
4471                 dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4472                         ret);
4473                 goto err_reinit;
4474         }
4475
4476         return 0;
4477
4478 err_reinit:
4479         while (v--)
4480                 if (pf->vsi[v])
4481                         ice_vsi_free_q_vectors(pf->vsi[v]);
4482
4483         return ret;
4484 }
4485
4486 /**
4487  * ice_suspend
4488  * @dev: generic device information structure
4489  *
4490  * Power Management callback to quiesce the device and prepare
4491  * for D3 transition.
4492  */
4493 static int __maybe_unused ice_suspend(struct device *dev)
4494 {
4495         struct pci_dev *pdev = to_pci_dev(dev);
4496         struct ice_pf *pf;
4497         int disabled, v;
4498
4499         pf = pci_get_drvdata(pdev);
4500
4501         if (!ice_pf_state_is_nominal(pf)) {
4502                 dev_err(dev, "Device is not ready, no need to suspend it\n");
4503                 return -EBUSY;
4504         }
4505
4506         /* Stop watchdog tasks until resume completion.
4507          * Even though it is most likely that the service task is
4508          * disabled if the device is suspended or down, the service task's
4509          * state is controlled by a different state bit, and we should
4510          * store and honor whatever state that bit is in at this point.
4511          */
4512         disabled = ice_service_task_stop(pf);
4513
4514         /* Already suspended?, then there is nothing to do */
4515         if (test_and_set_bit(__ICE_SUSPENDED, pf->state)) {
4516                 if (!disabled)
4517                         ice_service_task_restart(pf);
4518                 return 0;
4519         }
4520
4521         if (test_bit(__ICE_DOWN, pf->state) ||
4522             ice_is_reset_in_progress(pf->state)) {
4523                 dev_err(dev, "can't suspend device in reset or already down\n");
4524                 if (!disabled)
4525                         ice_service_task_restart(pf);
4526                 return 0;
4527         }
4528
4529         ice_setup_mc_magic_wake(pf);
4530
4531         ice_prepare_for_shutdown(pf);
4532
4533         ice_set_wake(pf);
4534
4535         /* Free vectors, clear the interrupt scheme and release IRQs
4536          * for proper hibernation, especially with large number of CPUs.
4537          * Otherwise hibernation might fail when mapping all the vectors back
4538          * to CPU0.
4539          */
4540         ice_free_irq_msix_misc(pf);
4541         ice_for_each_vsi(pf, v) {
4542                 if (!pf->vsi[v])
4543                         continue;
4544                 ice_vsi_free_q_vectors(pf->vsi[v]);
4545         }
4546         ice_clear_interrupt_scheme(pf);
4547
4548         pci_save_state(pdev);
4549         pci_wake_from_d3(pdev, pf->wol_ena);
4550         pci_set_power_state(pdev, PCI_D3hot);
4551         return 0;
4552 }
4553
4554 /**
4555  * ice_resume - PM callback for waking up from D3
4556  * @dev: generic device information structure
4557  */
4558 static int __maybe_unused ice_resume(struct device *dev)
4559 {
4560         struct pci_dev *pdev = to_pci_dev(dev);
4561         enum ice_reset_req reset_type;
4562         struct ice_pf *pf;
4563         struct ice_hw *hw;
4564         int ret;
4565
4566         pci_set_power_state(pdev, PCI_D0);
4567         pci_restore_state(pdev);
4568         pci_save_state(pdev);
4569
4570         if (!pci_device_is_present(pdev))
4571                 return -ENODEV;
4572
4573         ret = pci_enable_device_mem(pdev);
4574         if (ret) {
4575                 dev_err(dev, "Cannot enable device after suspend\n");
4576                 return ret;
4577         }
4578
4579         pf = pci_get_drvdata(pdev);
4580         hw = &pf->hw;
4581
4582         pf->wakeup_reason = rd32(hw, PFPM_WUS);
4583         ice_print_wake_reason(pf);
4584
4585         /* We cleared the interrupt scheme when we suspended, so we need to
4586          * restore it now to resume device functionality.
4587          */
4588         ret = ice_reinit_interrupt_scheme(pf);
4589         if (ret)
4590                 dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
4591
4592         clear_bit(__ICE_DOWN, pf->state);
4593         /* Now perform PF reset and rebuild */
4594         reset_type = ICE_RESET_PFR;
4595         /* re-enable service task for reset, but allow reset to schedule it */
4596         clear_bit(__ICE_SERVICE_DIS, pf->state);
4597
4598         if (ice_schedule_reset(pf, reset_type))
4599                 dev_err(dev, "Reset during resume failed.\n");
4600
4601         clear_bit(__ICE_SUSPENDED, pf->state);
4602         ice_service_task_restart(pf);
4603
4604         /* Restart the service task */
4605         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4606
4607         return 0;
4608 }
4609 #endif /* CONFIG_PM */
4610
4611 /**
4612  * ice_pci_err_detected - warning that PCI error has been detected
4613  * @pdev: PCI device information struct
4614  * @err: the type of PCI error
4615  *
4616  * Called to warn that something happened on the PCI bus and the error handling
4617  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
4618  */
4619 static pci_ers_result_t
4620 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
4621 {
4622         struct ice_pf *pf = pci_get_drvdata(pdev);
4623
4624         if (!pf) {
4625                 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
4626                         __func__, err);
4627                 return PCI_ERS_RESULT_DISCONNECT;
4628         }
4629
4630         if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4631                 ice_service_task_stop(pf);
4632
4633                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4634                         set_bit(__ICE_PFR_REQ, pf->state);
4635                         ice_prepare_for_reset(pf);
4636                 }
4637         }
4638
4639         return PCI_ERS_RESULT_NEED_RESET;
4640 }
4641
4642 /**
4643  * ice_pci_err_slot_reset - a PCI slot reset has just happened
4644  * @pdev: PCI device information struct
4645  *
4646  * Called to determine if the driver can recover from the PCI slot reset by
4647  * using a register read to determine if the device is recoverable.
4648  */
4649 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
4650 {
4651         struct ice_pf *pf = pci_get_drvdata(pdev);
4652         pci_ers_result_t result;
4653         int err;
4654         u32 reg;
4655
4656         err = pci_enable_device_mem(pdev);
4657         if (err) {
4658                 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
4659                         err);
4660                 result = PCI_ERS_RESULT_DISCONNECT;
4661         } else {
4662                 pci_set_master(pdev);
4663                 pci_restore_state(pdev);
4664                 pci_save_state(pdev);
4665                 pci_wake_from_d3(pdev, false);
4666
4667                 /* Check for life */
4668                 reg = rd32(&pf->hw, GLGEN_RTRIG);
4669                 if (!reg)
4670                         result = PCI_ERS_RESULT_RECOVERED;
4671                 else
4672                         result = PCI_ERS_RESULT_DISCONNECT;
4673         }
4674
4675         err = pci_aer_clear_nonfatal_status(pdev);
4676         if (err)
4677                 dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
4678                         err);
4679                 /* non-fatal, continue */
4680
4681         return result;
4682 }
4683
4684 /**
4685  * ice_pci_err_resume - restart operations after PCI error recovery
4686  * @pdev: PCI device information struct
4687  *
4688  * Called to allow the driver to bring things back up after PCI error and/or
4689  * reset recovery have finished
4690  */
4691 static void ice_pci_err_resume(struct pci_dev *pdev)
4692 {
4693         struct ice_pf *pf = pci_get_drvdata(pdev);
4694
4695         if (!pf) {
4696                 dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
4697                         __func__);
4698                 return;
4699         }
4700
4701         if (test_bit(__ICE_SUSPENDED, pf->state)) {
4702                 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
4703                         __func__);
4704                 return;
4705         }
4706
4707         ice_restore_all_vfs_msi_state(pdev);
4708
4709         ice_do_reset(pf, ICE_RESET_PFR);
4710         ice_service_task_restart(pf);
4711         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4712 }
4713
4714 /**
4715  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
4716  * @pdev: PCI device information struct
4717  */
4718 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
4719 {
4720         struct ice_pf *pf = pci_get_drvdata(pdev);
4721
4722         if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4723                 ice_service_task_stop(pf);
4724
4725                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4726                         set_bit(__ICE_PFR_REQ, pf->state);
4727                         ice_prepare_for_reset(pf);
4728                 }
4729         }
4730 }
4731
4732 /**
4733  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
4734  * @pdev: PCI device information struct
4735  */
4736 static void ice_pci_err_reset_done(struct pci_dev *pdev)
4737 {
4738         ice_pci_err_resume(pdev);
4739 }
4740
4741 /* ice_pci_tbl - PCI Device ID Table
4742  *
4743  * Wildcard entries (PCI_ANY_ID) should come last
4744  * Last entry must be all 0s
4745  *
4746  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
4747  *   Class, Class Mask, private data (not used) }
4748  */
4749 static const struct pci_device_id ice_pci_tbl[] = {
4750         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
4751         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
4752         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
4753         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
4754         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
4755         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
4756         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
4757         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
4758         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
4759         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
4760         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
4761         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
4762         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
4763         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
4764         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
4765         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
4766         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
4767         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
4768         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
4769         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
4770         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
4771         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
4772         { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
4773         /* required last entry */
4774         { 0, }
4775 };
4776 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
4777
4778 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
4779
4780 static const struct pci_error_handlers ice_pci_err_handler = {
4781         .error_detected = ice_pci_err_detected,
4782         .slot_reset = ice_pci_err_slot_reset,
4783         .reset_prepare = ice_pci_err_reset_prepare,
4784         .reset_done = ice_pci_err_reset_done,
4785         .resume = ice_pci_err_resume
4786 };
4787
4788 static struct pci_driver ice_driver = {
4789         .name = KBUILD_MODNAME,
4790         .id_table = ice_pci_tbl,
4791         .probe = ice_probe,
4792         .remove = ice_remove,
4793 #ifdef CONFIG_PM
4794         .driver.pm = &ice_pm_ops,
4795 #endif /* CONFIG_PM */
4796         .shutdown = ice_shutdown,
4797         .sriov_configure = ice_sriov_configure,
4798         .err_handler = &ice_pci_err_handler
4799 };
4800
4801 /**
4802  * ice_module_init - Driver registration routine
4803  *
4804  * ice_module_init is the first routine called when the driver is
4805  * loaded. All it does is register with the PCI subsystem.
4806  */
4807 static int __init ice_module_init(void)
4808 {
4809         int status;
4810
4811         pr_info("%s\n", ice_driver_string);
4812         pr_info("%s\n", ice_copyright);
4813
4814         ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
4815         if (!ice_wq) {
4816                 pr_err("Failed to create workqueue\n");
4817                 return -ENOMEM;
4818         }
4819
4820         status = pci_register_driver(&ice_driver);
4821         if (status) {
4822                 pr_err("failed to register PCI driver, err %d\n", status);
4823                 destroy_workqueue(ice_wq);
4824         }
4825
4826         return status;
4827 }
4828 module_init(ice_module_init);
4829
4830 /**
4831  * ice_module_exit - Driver exit cleanup routine
4832  *
4833  * ice_module_exit is called just before the driver is removed
4834  * from memory.
4835  */
4836 static void __exit ice_module_exit(void)
4837 {
4838         pci_unregister_driver(&ice_driver);
4839         destroy_workqueue(ice_wq);
4840         pr_info("module unloaded\n");
4841 }
4842 module_exit(ice_module_exit);
4843
4844 /**
4845  * ice_set_mac_address - NDO callback to set MAC address
4846  * @netdev: network interface device structure
4847  * @pi: pointer to an address structure
4848  *
4849  * Returns 0 on success, negative on failure
4850  */
4851 static int ice_set_mac_address(struct net_device *netdev, void *pi)
4852 {
4853         struct ice_netdev_priv *np = netdev_priv(netdev);
4854         struct ice_vsi *vsi = np->vsi;
4855         struct ice_pf *pf = vsi->back;
4856         struct ice_hw *hw = &pf->hw;
4857         struct sockaddr *addr = pi;
4858         enum ice_status status;
4859         u8 flags = 0;
4860         int err = 0;
4861         u8 *mac;
4862
4863         mac = (u8 *)addr->sa_data;
4864
4865         if (!is_valid_ether_addr(mac))
4866                 return -EADDRNOTAVAIL;
4867
4868         if (ether_addr_equal(netdev->dev_addr, mac)) {
4869                 netdev_warn(netdev, "already using mac %pM\n", mac);
4870                 return 0;
4871         }
4872
4873         if (test_bit(__ICE_DOWN, pf->state) ||
4874             ice_is_reset_in_progress(pf->state)) {
4875                 netdev_err(netdev, "can't set mac %pM. device not ready\n",
4876                            mac);
4877                 return -EBUSY;
4878         }
4879
4880         /* Clean up old MAC filter. Not an error if old filter doesn't exist */
4881         status = ice_fltr_remove_mac(vsi, netdev->dev_addr, ICE_FWD_TO_VSI);
4882         if (status && status != ICE_ERR_DOES_NOT_EXIST) {
4883                 err = -EADDRNOTAVAIL;
4884                 goto err_update_filters;
4885         }
4886
4887         /* Add filter for new MAC. If filter exists, just return success */
4888         status = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
4889         if (status == ICE_ERR_ALREADY_EXISTS) {
4890                 netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
4891                 return 0;
4892         }
4893
4894         /* error if the new filter addition failed */
4895         if (status)
4896                 err = -EADDRNOTAVAIL;
4897
4898 err_update_filters:
4899         if (err) {
4900                 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
4901                            mac);
4902                 return err;
4903         }
4904
4905         /* change the netdev's MAC address */
4906         memcpy(netdev->dev_addr, mac, netdev->addr_len);
4907         netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
4908                    netdev->dev_addr);
4909
4910         /* write new MAC address to the firmware */
4911         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
4912         status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
4913         if (status) {
4914                 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %s\n",
4915                            mac, ice_stat_str(status));
4916         }
4917         return 0;
4918 }
4919
4920 /**
4921  * ice_set_rx_mode - NDO callback to set the netdev filters
4922  * @netdev: network interface device structure
4923  */
4924 static void ice_set_rx_mode(struct net_device *netdev)
4925 {
4926         struct ice_netdev_priv *np = netdev_priv(netdev);
4927         struct ice_vsi *vsi = np->vsi;
4928
4929         if (!vsi)
4930                 return;
4931
4932         /* Set the flags to synchronize filters
4933          * ndo_set_rx_mode may be triggered even without a change in netdev
4934          * flags
4935          */
4936         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
4937         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
4938         set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
4939
4940         /* schedule our worker thread which will take care of
4941          * applying the new filter changes
4942          */
4943         ice_service_task_schedule(vsi->back);
4944 }
4945
4946 /**
4947  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
4948  * @netdev: network interface device structure
4949  * @queue_index: Queue ID
4950  * @maxrate: maximum bandwidth in Mbps
4951  */
4952 static int
4953 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
4954 {
4955         struct ice_netdev_priv *np = netdev_priv(netdev);
4956         struct ice_vsi *vsi = np->vsi;
4957         enum ice_status status;
4958         u16 q_handle;
4959         u8 tc;
4960
4961         /* Validate maxrate requested is within permitted range */
4962         if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
4963                 netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
4964                            maxrate, queue_index);
4965                 return -EINVAL;
4966         }
4967
4968         q_handle = vsi->tx_rings[queue_index]->q_handle;
4969         tc = ice_dcb_get_tc(vsi, queue_index);
4970
4971         /* Set BW back to default, when user set maxrate to 0 */
4972         if (!maxrate)
4973                 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
4974                                                q_handle, ICE_MAX_BW);
4975         else
4976                 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
4977                                           q_handle, ICE_MAX_BW, maxrate * 1000);
4978         if (status) {
4979                 netdev_err(netdev, "Unable to set Tx max rate, error %s\n",
4980                            ice_stat_str(status));
4981                 return -EIO;
4982         }
4983
4984         return 0;
4985 }
4986
4987 /**
4988  * ice_fdb_add - add an entry to the hardware database
4989  * @ndm: the input from the stack
4990  * @tb: pointer to array of nladdr (unused)
4991  * @dev: the net device pointer
4992  * @addr: the MAC address entry being added
4993  * @vid: VLAN ID
4994  * @flags: instructions from stack about fdb operation
4995  * @extack: netlink extended ack
4996  */
4997 static int
4998 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
4999             struct net_device *dev, const unsigned char *addr, u16 vid,
5000             u16 flags, struct netlink_ext_ack __always_unused *extack)
5001 {
5002         int err;
5003
5004         if (vid) {
5005                 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5006                 return -EINVAL;
5007         }
5008         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5009                 netdev_err(dev, "FDB only supports static addresses\n");
5010                 return -EINVAL;
5011         }
5012
5013         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5014                 err = dev_uc_add_excl(dev, addr);
5015         else if (is_multicast_ether_addr(addr))
5016                 err = dev_mc_add_excl(dev, addr);
5017         else
5018                 err = -EINVAL;
5019
5020         /* Only return duplicate errors if NLM_F_EXCL is set */
5021         if (err == -EEXIST && !(flags & NLM_F_EXCL))
5022                 err = 0;
5023
5024         return err;
5025 }
5026
5027 /**
5028  * ice_fdb_del - delete an entry from the hardware database
5029  * @ndm: the input from the stack
5030  * @tb: pointer to array of nladdr (unused)
5031  * @dev: the net device pointer
5032  * @addr: the MAC address entry being added
5033  * @vid: VLAN ID
5034  */
5035 static int
5036 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5037             struct net_device *dev, const unsigned char *addr,
5038             __always_unused u16 vid)
5039 {
5040         int err;
5041
5042         if (ndm->ndm_state & NUD_PERMANENT) {
5043                 netdev_err(dev, "FDB only supports static addresses\n");
5044                 return -EINVAL;
5045         }
5046
5047         if (is_unicast_ether_addr(addr))
5048                 err = dev_uc_del(dev, addr);
5049         else if (is_multicast_ether_addr(addr))
5050                 err = dev_mc_del(dev, addr);
5051         else
5052                 err = -EINVAL;
5053
5054         return err;
5055 }
5056
5057 /**
5058  * ice_set_features - set the netdev feature flags
5059  * @netdev: ptr to the netdev being adjusted
5060  * @features: the feature set that the stack is suggesting
5061  */
5062 static int
5063 ice_set_features(struct net_device *netdev, netdev_features_t features)
5064 {
5065         struct ice_netdev_priv *np = netdev_priv(netdev);
5066         struct ice_vsi *vsi = np->vsi;
5067         struct ice_pf *pf = vsi->back;
5068         int ret = 0;
5069
5070         /* Don't set any netdev advanced features with device in Safe Mode */
5071         if (ice_is_safe_mode(vsi->back)) {
5072                 dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5073                 return ret;
5074         }
5075
5076         /* Do not change setting during reset */
5077         if (ice_is_reset_in_progress(pf->state)) {
5078                 dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5079                 return -EBUSY;
5080         }
5081
5082         /* Multiple features can be changed in one call so keep features in
5083          * separate if/else statements to guarantee each feature is checked
5084          */
5085         if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5086                 ret = ice_vsi_manage_rss_lut(vsi, true);
5087         else if (!(features & NETIF_F_RXHASH) &&
5088                  netdev->features & NETIF_F_RXHASH)
5089                 ret = ice_vsi_manage_rss_lut(vsi, false);
5090
5091         if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5092             !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5093                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
5094         else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5095                  (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5096                 ret = ice_vsi_manage_vlan_stripping(vsi, false);
5097
5098         if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5099             !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5100                 ret = ice_vsi_manage_vlan_insertion(vsi);
5101         else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5102                  (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5103                 ret = ice_vsi_manage_vlan_insertion(vsi);
5104
5105         if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5106             !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5107                 ret = ice_cfg_vlan_pruning(vsi, true, false);
5108         else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5109                  (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5110                 ret = ice_cfg_vlan_pruning(vsi, false, false);
5111
5112         if ((features & NETIF_F_NTUPLE) &&
5113             !(netdev->features & NETIF_F_NTUPLE)) {
5114                 ice_vsi_manage_fdir(vsi, true);
5115                 ice_init_arfs(vsi);
5116         } else if (!(features & NETIF_F_NTUPLE) &&
5117                  (netdev->features & NETIF_F_NTUPLE)) {
5118                 ice_vsi_manage_fdir(vsi, false);
5119                 ice_clear_arfs(vsi);
5120         }
5121
5122         return ret;
5123 }
5124
5125 /**
5126  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5127  * @vsi: VSI to setup VLAN properties for
5128  */
5129 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5130 {
5131         int ret = 0;
5132
5133         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5134                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
5135         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5136                 ret = ice_vsi_manage_vlan_insertion(vsi);
5137
5138         return ret;
5139 }
5140
5141 /**
5142  * ice_vsi_cfg - Setup the VSI
5143  * @vsi: the VSI being configured
5144  *
5145  * Return 0 on success and negative value on error
5146  */
5147 int ice_vsi_cfg(struct ice_vsi *vsi)
5148 {
5149         int err;
5150
5151         if (vsi->netdev) {
5152                 ice_set_rx_mode(vsi->netdev);
5153
5154                 err = ice_vsi_vlan_setup(vsi);
5155
5156                 if (err)
5157                         return err;
5158         }
5159         ice_vsi_cfg_dcb_rings(vsi);
5160
5161         err = ice_vsi_cfg_lan_txqs(vsi);
5162         if (!err && ice_is_xdp_ena_vsi(vsi))
5163                 err = ice_vsi_cfg_xdp_txqs(vsi);
5164         if (!err)
5165                 err = ice_vsi_cfg_rxqs(vsi);
5166
5167         return err;
5168 }
5169
5170 /**
5171  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5172  * @vsi: the VSI being configured
5173  */
5174 static void ice_napi_enable_all(struct ice_vsi *vsi)
5175 {
5176         int q_idx;
5177
5178         if (!vsi->netdev)
5179                 return;
5180
5181         ice_for_each_q_vector(vsi, q_idx) {
5182                 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5183
5184                 if (q_vector->rx.ring || q_vector->tx.ring)
5185                         napi_enable(&q_vector->napi);
5186         }
5187 }
5188
5189 /**
5190  * ice_up_complete - Finish the last steps of bringing up a connection
5191  * @vsi: The VSI being configured
5192  *
5193  * Return 0 on success and negative value on error
5194  */
5195 static int ice_up_complete(struct ice_vsi *vsi)
5196 {
5197         struct ice_pf *pf = vsi->back;
5198         int err;
5199
5200         ice_vsi_cfg_msix(vsi);
5201
5202         /* Enable only Rx rings, Tx rings were enabled by the FW when the
5203          * Tx queue group list was configured and the context bits were
5204          * programmed using ice_vsi_cfg_txqs
5205          */
5206         err = ice_vsi_start_all_rx_rings(vsi);
5207         if (err)
5208                 return err;
5209
5210         clear_bit(__ICE_DOWN, vsi->state);
5211         ice_napi_enable_all(vsi);
5212         ice_vsi_ena_irq(vsi);
5213
5214         if (vsi->port_info &&
5215             (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5216             vsi->netdev) {
5217                 ice_print_link_msg(vsi, true);
5218                 netif_tx_start_all_queues(vsi->netdev);
5219                 netif_carrier_on(vsi->netdev);
5220         }
5221
5222         ice_service_task_schedule(pf);
5223
5224         return 0;
5225 }
5226
5227 /**
5228  * ice_up - Bring the connection back up after being down
5229  * @vsi: VSI being configured
5230  */
5231 int ice_up(struct ice_vsi *vsi)
5232 {
5233         int err;
5234
5235         err = ice_vsi_cfg(vsi);
5236         if (!err)
5237                 err = ice_up_complete(vsi);
5238
5239         return err;
5240 }
5241
5242 /**
5243  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5244  * @ring: Tx or Rx ring to read stats from
5245  * @pkts: packets stats counter
5246  * @bytes: bytes stats counter
5247  *
5248  * This function fetches stats from the ring considering the atomic operations
5249  * that needs to be performed to read u64 values in 32 bit machine.
5250  */
5251 static void
5252 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
5253 {
5254         unsigned int start;
5255         *pkts = 0;
5256         *bytes = 0;
5257
5258         if (!ring)
5259                 return;
5260         do {
5261                 start = u64_stats_fetch_begin_irq(&ring->syncp);
5262                 *pkts = ring->stats.pkts;
5263                 *bytes = ring->stats.bytes;
5264         } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
5265 }
5266
5267 /**
5268  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5269  * @vsi: the VSI to be updated
5270  * @rings: rings to work on
5271  * @count: number of rings
5272  */
5273 static void
5274 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi, struct ice_ring **rings,
5275                              u16 count)
5276 {
5277         struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5278         u16 i;
5279
5280         for (i = 0; i < count; i++) {
5281                 struct ice_ring *ring;
5282                 u64 pkts, bytes;
5283
5284                 ring = READ_ONCE(rings[i]);
5285                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5286                 vsi_stats->tx_packets += pkts;
5287                 vsi_stats->tx_bytes += bytes;
5288                 vsi->tx_restart += ring->tx_stats.restart_q;
5289                 vsi->tx_busy += ring->tx_stats.tx_busy;
5290                 vsi->tx_linearize += ring->tx_stats.tx_linearize;
5291         }
5292 }
5293
5294 /**
5295  * ice_update_vsi_ring_stats - Update VSI stats counters
5296  * @vsi: the VSI to be updated
5297  */
5298 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5299 {
5300         struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5301         struct ice_ring *ring;
5302         u64 pkts, bytes;
5303         int i;
5304
5305         /* reset netdev stats */
5306         vsi_stats->tx_packets = 0;
5307         vsi_stats->tx_bytes = 0;
5308         vsi_stats->rx_packets = 0;
5309         vsi_stats->rx_bytes = 0;
5310
5311         /* reset non-netdev (extended) stats */
5312         vsi->tx_restart = 0;
5313         vsi->tx_busy = 0;
5314         vsi->tx_linearize = 0;
5315         vsi->rx_buf_failed = 0;
5316         vsi->rx_page_failed = 0;
5317         vsi->rx_gro_dropped = 0;
5318
5319         rcu_read_lock();
5320
5321         /* update Tx rings counters */
5322         ice_update_vsi_tx_ring_stats(vsi, vsi->tx_rings, vsi->num_txq);
5323
5324         /* update Rx rings counters */
5325         ice_for_each_rxq(vsi, i) {
5326                 ring = READ_ONCE(vsi->rx_rings[i]);
5327                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5328                 vsi_stats->rx_packets += pkts;
5329                 vsi_stats->rx_bytes += bytes;
5330                 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5331                 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5332                 vsi->rx_gro_dropped += ring->rx_stats.gro_dropped;
5333         }
5334
5335         /* update XDP Tx rings counters */
5336         if (ice_is_xdp_ena_vsi(vsi))
5337                 ice_update_vsi_tx_ring_stats(vsi, vsi->xdp_rings,
5338                                              vsi->num_xdp_txq);
5339
5340         rcu_read_unlock();
5341 }
5342
5343 /**
5344  * ice_update_vsi_stats - Update VSI stats counters
5345  * @vsi: the VSI to be updated
5346  */
5347 void ice_update_vsi_stats(struct ice_vsi *vsi)
5348 {
5349         struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
5350         struct ice_eth_stats *cur_es = &vsi->eth_stats;
5351         struct ice_pf *pf = vsi->back;
5352
5353         if (test_bit(__ICE_DOWN, vsi->state) ||
5354             test_bit(__ICE_CFG_BUSY, pf->state))
5355                 return;
5356
5357         /* get stats as recorded by Tx/Rx rings */
5358         ice_update_vsi_ring_stats(vsi);
5359
5360         /* get VSI stats as recorded by the hardware */
5361         ice_update_eth_stats(vsi);
5362
5363         cur_ns->tx_errors = cur_es->tx_errors;
5364         cur_ns->rx_dropped = cur_es->rx_discards + vsi->rx_gro_dropped;
5365         cur_ns->tx_dropped = cur_es->tx_discards;
5366         cur_ns->multicast = cur_es->rx_multicast;
5367
5368         /* update some more netdev stats if this is main VSI */
5369         if (vsi->type == ICE_VSI_PF) {
5370                 cur_ns->rx_crc_errors = pf->stats.crc_errors;
5371                 cur_ns->rx_errors = pf->stats.crc_errors +
5372                                     pf->stats.illegal_bytes +
5373                                     pf->stats.rx_len_errors +
5374                                     pf->stats.rx_undersize +
5375                                     pf->hw_csum_rx_error +
5376                                     pf->stats.rx_jabber +
5377                                     pf->stats.rx_fragments +
5378                                     pf->stats.rx_oversize;
5379                 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
5380                 /* record drops from the port level */
5381                 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
5382         }
5383 }
5384
5385 /**
5386  * ice_update_pf_stats - Update PF port stats counters
5387  * @pf: PF whose stats needs to be updated
5388  */
5389 void ice_update_pf_stats(struct ice_pf *pf)
5390 {
5391         struct ice_hw_port_stats *prev_ps, *cur_ps;
5392         struct ice_hw *hw = &pf->hw;
5393         u16 fd_ctr_base;
5394         u8 port;
5395
5396         port = hw->port_info->lport;
5397         prev_ps = &pf->stats_prev;
5398         cur_ps = &pf->stats;
5399
5400         ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
5401                           &prev_ps->eth.rx_bytes,
5402                           &cur_ps->eth.rx_bytes);
5403
5404         ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
5405                           &prev_ps->eth.rx_unicast,
5406                           &cur_ps->eth.rx_unicast);
5407
5408         ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
5409                           &prev_ps->eth.rx_multicast,
5410                           &cur_ps->eth.rx_multicast);
5411
5412         ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
5413                           &prev_ps->eth.rx_broadcast,
5414                           &cur_ps->eth.rx_broadcast);
5415
5416         ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
5417                           &prev_ps->eth.rx_discards,
5418                           &cur_ps->eth.rx_discards);
5419
5420         ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
5421                           &prev_ps->eth.tx_bytes,
5422                           &cur_ps->eth.tx_bytes);
5423
5424         ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
5425                           &prev_ps->eth.tx_unicast,
5426                           &cur_ps->eth.tx_unicast);
5427
5428         ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
5429                           &prev_ps->eth.tx_multicast,
5430                           &cur_ps->eth.tx_multicast);
5431
5432         ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
5433                           &prev_ps->eth.tx_broadcast,
5434                           &cur_ps->eth.tx_broadcast);
5435
5436         ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
5437                           &prev_ps->tx_dropped_link_down,
5438                           &cur_ps->tx_dropped_link_down);
5439
5440         ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
5441                           &prev_ps->rx_size_64, &cur_ps->rx_size_64);
5442
5443         ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
5444                           &prev_ps->rx_size_127, &cur_ps->rx_size_127);
5445
5446         ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
5447                           &prev_ps->rx_size_255, &cur_ps->rx_size_255);
5448
5449         ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
5450                           &prev_ps->rx_size_511, &cur_ps->rx_size_511);
5451
5452         ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
5453                           &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
5454
5455         ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
5456                           &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5457
5458         ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
5459                           &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5460
5461         ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
5462                           &prev_ps->tx_size_64, &cur_ps->tx_size_64);
5463
5464         ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
5465                           &prev_ps->tx_size_127, &cur_ps->tx_size_127);
5466
5467         ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
5468                           &prev_ps->tx_size_255, &cur_ps->tx_size_255);
5469
5470         ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
5471                           &prev_ps->tx_size_511, &cur_ps->tx_size_511);
5472
5473         ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
5474                           &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5475
5476         ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
5477                           &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5478
5479         ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
5480                           &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5481
5482         fd_ctr_base = hw->fd_ctr_base;
5483
5484         ice_stat_update40(hw,
5485                           GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
5486                           pf->stat_prev_loaded, &prev_ps->fd_sb_match,
5487                           &cur_ps->fd_sb_match);
5488         ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
5489                           &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5490
5491         ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
5492                           &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5493
5494         ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
5495                           &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5496
5497         ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
5498                           &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5499
5500         ice_update_dcb_stats(pf);
5501
5502         ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
5503                           &prev_ps->crc_errors, &cur_ps->crc_errors);
5504
5505         ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
5506                           &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5507
5508         ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
5509                           &prev_ps->mac_local_faults,
5510                           &cur_ps->mac_local_faults);
5511
5512         ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
5513                           &prev_ps->mac_remote_faults,
5514                           &cur_ps->mac_remote_faults);
5515
5516         ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
5517                           &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5518
5519         ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
5520                           &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5521
5522         ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
5523                           &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5524
5525         ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
5526                           &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5527
5528         ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
5529                           &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5530
5531         cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
5532
5533         pf->stat_prev_loaded = true;
5534 }
5535
5536 /**
5537  * ice_get_stats64 - get statistics for network device structure
5538  * @netdev: network interface device structure
5539  * @stats: main device statistics structure
5540  */
5541 static
5542 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5543 {
5544         struct ice_netdev_priv *np = netdev_priv(netdev);
5545         struct rtnl_link_stats64 *vsi_stats;
5546         struct ice_vsi *vsi = np->vsi;
5547
5548         vsi_stats = &vsi->net_stats;
5549
5550         if (!vsi->num_txq || !vsi->num_rxq)
5551                 return;
5552
5553         /* netdev packet/byte stats come from ring counter. These are obtained
5554          * by summing up ring counters (done by ice_update_vsi_ring_stats).
5555          * But, only call the update routine and read the registers if VSI is
5556          * not down.
5557          */
5558         if (!test_bit(__ICE_DOWN, vsi->state))
5559                 ice_update_vsi_ring_stats(vsi);
5560         stats->tx_packets = vsi_stats->tx_packets;
5561         stats->tx_bytes = vsi_stats->tx_bytes;
5562         stats->rx_packets = vsi_stats->rx_packets;
5563         stats->rx_bytes = vsi_stats->rx_bytes;
5564
5565         /* The rest of the stats can be read from the hardware but instead we
5566          * just return values that the watchdog task has already obtained from
5567          * the hardware.
5568          */
5569         stats->multicast = vsi_stats->multicast;
5570         stats->tx_errors = vsi_stats->tx_errors;
5571         stats->tx_dropped = vsi_stats->tx_dropped;
5572         stats->rx_errors = vsi_stats->rx_errors;
5573         stats->rx_dropped = vsi_stats->rx_dropped;
5574         stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5575         stats->rx_length_errors = vsi_stats->rx_length_errors;
5576 }
5577
5578 /**
5579  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5580  * @vsi: VSI having NAPI disabled
5581  */
5582 static void ice_napi_disable_all(struct ice_vsi *vsi)
5583 {
5584         int q_idx;
5585
5586         if (!vsi->netdev)
5587                 return;
5588
5589         ice_for_each_q_vector(vsi, q_idx) {
5590                 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5591
5592                 if (q_vector->rx.ring || q_vector->tx.ring)
5593                         napi_disable(&q_vector->napi);
5594         }
5595 }
5596
5597 /**
5598  * ice_down - Shutdown the connection
5599  * @vsi: The VSI being stopped
5600  */
5601 int ice_down(struct ice_vsi *vsi)
5602 {
5603         int i, tx_err, rx_err, link_err = 0;
5604
5605         /* Caller of this function is expected to set the
5606          * vsi->state __ICE_DOWN bit
5607          */
5608         if (vsi->netdev) {
5609                 netif_carrier_off(vsi->netdev);
5610                 netif_tx_disable(vsi->netdev);
5611         }
5612
5613         ice_vsi_dis_irq(vsi);
5614
5615         tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
5616         if (tx_err)
5617                 netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
5618                            vsi->vsi_num, tx_err);
5619         if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
5620                 tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
5621                 if (tx_err)
5622                         netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
5623                                    vsi->vsi_num, tx_err);
5624         }
5625
5626         rx_err = ice_vsi_stop_all_rx_rings(vsi);
5627         if (rx_err)
5628                 netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
5629                            vsi->vsi_num, rx_err);
5630
5631         ice_napi_disable_all(vsi);
5632
5633         if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
5634                 link_err = ice_force_phys_link_state(vsi, false);
5635                 if (link_err)
5636                         netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
5637                                    vsi->vsi_num, link_err);
5638         }
5639
5640         ice_for_each_txq(vsi, i)
5641                 ice_clean_tx_ring(vsi->tx_rings[i]);
5642
5643         ice_for_each_rxq(vsi, i)
5644                 ice_clean_rx_ring(vsi->rx_rings[i]);
5645
5646         if (tx_err || rx_err || link_err) {
5647                 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5648                            vsi->vsi_num, vsi->vsw->sw_id);
5649                 return -EIO;
5650         }
5651
5652         return 0;
5653 }
5654
5655 /**
5656  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5657  * @vsi: VSI having resources allocated
5658  *
5659  * Return 0 on success, negative on failure
5660  */
5661 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5662 {
5663         int i, err = 0;
5664
5665         if (!vsi->num_txq) {
5666                 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
5667                         vsi->vsi_num);
5668                 return -EINVAL;
5669         }
5670
5671         ice_for_each_txq(vsi, i) {
5672                 struct ice_ring *ring = vsi->tx_rings[i];
5673
5674                 if (!ring)
5675                         return -EINVAL;
5676
5677                 ring->netdev = vsi->netdev;
5678                 err = ice_setup_tx_ring(ring);
5679                 if (err)
5680                         break;
5681         }
5682
5683         return err;
5684 }
5685
5686 /**
5687  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5688  * @vsi: VSI having resources allocated
5689  *
5690  * Return 0 on success, negative on failure
5691  */
5692 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5693 {
5694         int i, err = 0;
5695
5696         if (!vsi->num_rxq) {
5697                 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
5698                         vsi->vsi_num);
5699                 return -EINVAL;
5700         }
5701
5702         ice_for_each_rxq(vsi, i) {
5703                 struct ice_ring *ring = vsi->rx_rings[i];
5704
5705                 if (!ring)
5706                         return -EINVAL;
5707
5708                 ring->netdev = vsi->netdev;
5709                 err = ice_setup_rx_ring(ring);
5710                 if (err)
5711                         break;
5712         }
5713
5714         return err;
5715 }
5716
5717 /**
5718  * ice_vsi_open_ctrl - open control VSI for use
5719  * @vsi: the VSI to open
5720  *
5721  * Initialization of the Control VSI
5722  *
5723  * Returns 0 on success, negative value on error
5724  */
5725 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
5726 {
5727         char int_name[ICE_INT_NAME_STR_LEN];
5728         struct ice_pf *pf = vsi->back;
5729         struct device *dev;
5730         int err;
5731
5732         dev = ice_pf_to_dev(pf);
5733         /* allocate descriptors */
5734         err = ice_vsi_setup_tx_rings(vsi);
5735         if (err)
5736                 goto err_setup_tx;
5737
5738         err = ice_vsi_setup_rx_rings(vsi);
5739         if (err)
5740                 goto err_setup_rx;
5741
5742         err = ice_vsi_cfg(vsi);
5743         if (err)
5744                 goto err_setup_rx;
5745
5746         snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
5747                  dev_driver_string(dev), dev_name(dev));
5748         err = ice_vsi_req_irq_msix(vsi, int_name);
5749         if (err)
5750                 goto err_setup_rx;
5751
5752         ice_vsi_cfg_msix(vsi);
5753
5754         err = ice_vsi_start_all_rx_rings(vsi);
5755         if (err)
5756                 goto err_up_complete;
5757
5758         clear_bit(__ICE_DOWN, vsi->state);
5759         ice_vsi_ena_irq(vsi);
5760
5761         return 0;
5762
5763 err_up_complete:
5764         ice_down(vsi);
5765 err_setup_rx:
5766         ice_vsi_free_rx_rings(vsi);
5767 err_setup_tx:
5768         ice_vsi_free_tx_rings(vsi);
5769
5770         return err;
5771 }
5772
5773 /**
5774  * ice_vsi_open - Called when a network interface is made active
5775  * @vsi: the VSI to open
5776  *
5777  * Initialization of the VSI
5778  *
5779  * Returns 0 on success, negative value on error
5780  */
5781 static int ice_vsi_open(struct ice_vsi *vsi)
5782 {
5783         char int_name[ICE_INT_NAME_STR_LEN];
5784         struct ice_pf *pf = vsi->back;
5785         int err;
5786
5787         /* allocate descriptors */
5788         err = ice_vsi_setup_tx_rings(vsi);
5789         if (err)
5790                 goto err_setup_tx;
5791
5792         err = ice_vsi_setup_rx_rings(vsi);
5793         if (err)
5794                 goto err_setup_rx;
5795
5796         err = ice_vsi_cfg(vsi);
5797         if (err)
5798                 goto err_setup_rx;
5799
5800         snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5801                  dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
5802         err = ice_vsi_req_irq_msix(vsi, int_name);
5803         if (err)
5804                 goto err_setup_rx;
5805
5806         /* Notify the stack of the actual queue counts. */
5807         err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5808         if (err)
5809                 goto err_set_qs;
5810
5811         err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5812         if (err)
5813                 goto err_set_qs;
5814
5815         err = ice_up_complete(vsi);
5816         if (err)
5817                 goto err_up_complete;
5818
5819         return 0;
5820
5821 err_up_complete:
5822         ice_down(vsi);
5823 err_set_qs:
5824         ice_vsi_free_irq(vsi);
5825 err_setup_rx:
5826         ice_vsi_free_rx_rings(vsi);
5827 err_setup_tx:
5828         ice_vsi_free_tx_rings(vsi);
5829
5830         return err;
5831 }
5832
5833 /**
5834  * ice_vsi_release_all - Delete all VSIs
5835  * @pf: PF from which all VSIs are being removed
5836  */
5837 static void ice_vsi_release_all(struct ice_pf *pf)
5838 {
5839         int err, i;
5840
5841         if (!pf->vsi)
5842                 return;
5843
5844         ice_for_each_vsi(pf, i) {
5845                 if (!pf->vsi[i])
5846                         continue;
5847
5848                 err = ice_vsi_release(pf->vsi[i]);
5849                 if (err)
5850                         dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5851                                 i, err, pf->vsi[i]->vsi_num);
5852         }
5853 }
5854
5855 /**
5856  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
5857  * @pf: pointer to the PF instance
5858  * @type: VSI type to rebuild
5859  *
5860  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
5861  */
5862 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
5863 {
5864         struct device *dev = ice_pf_to_dev(pf);
5865         enum ice_status status;
5866         int i, err;
5867
5868         ice_for_each_vsi(pf, i) {
5869                 struct ice_vsi *vsi = pf->vsi[i];
5870
5871                 if (!vsi || vsi->type != type)
5872                         continue;
5873
5874                 /* rebuild the VSI */
5875                 err = ice_vsi_rebuild(vsi, true);
5876                 if (err) {
5877                         dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
5878                                 err, vsi->idx, ice_vsi_type_str(type));
5879                         return err;
5880                 }
5881
5882                 /* replay filters for the VSI */
5883                 status = ice_replay_vsi(&pf->hw, vsi->idx);
5884                 if (status) {
5885                         dev_err(dev, "replay VSI failed, status %s, VSI index %d, type %s\n",
5886                                 ice_stat_str(status), vsi->idx,
5887                                 ice_vsi_type_str(type));
5888                         return -EIO;
5889                 }
5890
5891                 /* Re-map HW VSI number, using VSI handle that has been
5892                  * previously validated in ice_replay_vsi() call above
5893                  */
5894                 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
5895
5896                 /* enable the VSI */
5897                 err = ice_ena_vsi(vsi, false);
5898                 if (err) {
5899                         dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
5900                                 err, vsi->idx, ice_vsi_type_str(type));
5901                         return err;
5902                 }
5903
5904                 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
5905                          ice_vsi_type_str(type));
5906         }
5907
5908         return 0;
5909 }
5910
5911 /**
5912  * ice_update_pf_netdev_link - Update PF netdev link status
5913  * @pf: pointer to the PF instance
5914  */
5915 static void ice_update_pf_netdev_link(struct ice_pf *pf)
5916 {
5917         bool link_up;
5918         int i;
5919
5920         ice_for_each_vsi(pf, i) {
5921                 struct ice_vsi *vsi = pf->vsi[i];
5922
5923                 if (!vsi || vsi->type != ICE_VSI_PF)
5924                         return;
5925
5926                 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
5927                 if (link_up) {
5928                         netif_carrier_on(pf->vsi[i]->netdev);
5929                         netif_tx_wake_all_queues(pf->vsi[i]->netdev);
5930                 } else {
5931                         netif_carrier_off(pf->vsi[i]->netdev);
5932                         netif_tx_stop_all_queues(pf->vsi[i]->netdev);
5933                 }
5934         }
5935 }
5936
5937 /**
5938  * ice_rebuild - rebuild after reset
5939  * @pf: PF to rebuild
5940  * @reset_type: type of reset
5941  *
5942  * Do not rebuild VF VSI in this flow because that is already handled via
5943  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
5944  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
5945  * to reset/rebuild all the VF VSI twice.
5946  */
5947 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
5948 {
5949         struct device *dev = ice_pf_to_dev(pf);
5950         struct ice_hw *hw = &pf->hw;
5951         enum ice_status ret;
5952         int err;
5953
5954         if (test_bit(__ICE_DOWN, pf->state))
5955                 goto clear_recovery;
5956
5957         dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
5958
5959         ret = ice_init_all_ctrlq(hw);
5960         if (ret) {
5961                 dev_err(dev, "control queues init failed %s\n",
5962                         ice_stat_str(ret));
5963                 goto err_init_ctrlq;
5964         }
5965
5966         /* if DDP was previously loaded successfully */
5967         if (!ice_is_safe_mode(pf)) {
5968                 /* reload the SW DB of filter tables */
5969                 if (reset_type == ICE_RESET_PFR)
5970                         ice_fill_blk_tbls(hw);
5971                 else
5972                         /* Reload DDP Package after CORER/GLOBR reset */
5973                         ice_load_pkg(NULL, pf);
5974         }
5975
5976         ret = ice_clear_pf_cfg(hw);
5977         if (ret) {
5978                 dev_err(dev, "clear PF configuration failed %s\n",
5979                         ice_stat_str(ret));
5980                 goto err_init_ctrlq;
5981         }
5982
5983         if (pf->first_sw->dflt_vsi_ena)
5984                 dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
5985         /* clear the default VSI configuration if it exists */
5986         pf->first_sw->dflt_vsi = NULL;
5987         pf->first_sw->dflt_vsi_ena = false;
5988
5989         ice_clear_pxe_mode(hw);
5990
5991         ret = ice_get_caps(hw);
5992         if (ret) {
5993                 dev_err(dev, "ice_get_caps failed %s\n", ice_stat_str(ret));
5994                 goto err_init_ctrlq;
5995         }
5996
5997         ret = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
5998         if (ret) {
5999                 dev_err(dev, "set_mac_cfg failed %s\n", ice_stat_str(ret));
6000                 goto err_init_ctrlq;
6001         }
6002
6003         err = ice_sched_init_port(hw->port_info);
6004         if (err)
6005                 goto err_sched_init_port;
6006
6007         /* start misc vector */
6008         err = ice_req_irq_msix_misc(pf);
6009         if (err) {
6010                 dev_err(dev, "misc vector setup failed: %d\n", err);
6011                 goto err_sched_init_port;
6012         }
6013
6014         if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6015                 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
6016                 if (!rd32(hw, PFQF_FD_SIZE)) {
6017                         u16 unused, guar, b_effort;
6018
6019                         guar = hw->func_caps.fd_fltr_guar;
6020                         b_effort = hw->func_caps.fd_fltr_best_effort;
6021
6022                         /* force guaranteed filter pool for PF */
6023                         ice_alloc_fd_guar_item(hw, &unused, guar);
6024                         /* force shared filter pool for PF */
6025                         ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6026                 }
6027         }
6028
6029         if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6030                 ice_dcb_rebuild(pf);
6031
6032         /* rebuild PF VSI */
6033         err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6034         if (err) {
6035                 dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6036                 goto err_vsi_rebuild;
6037         }
6038
6039         /* If Flow Director is active */
6040         if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6041                 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6042                 if (err) {
6043                         dev_err(dev, "control VSI rebuild failed: %d\n", err);
6044                         goto err_vsi_rebuild;
6045                 }
6046
6047                 /* replay HW Flow Director recipes */
6048                 if (hw->fdir_prof)
6049                         ice_fdir_replay_flows(hw);
6050
6051                 /* replay Flow Director filters */
6052                 ice_fdir_replay_fltrs(pf);
6053
6054                 ice_rebuild_arfs(pf);
6055         }
6056
6057         ice_update_pf_netdev_link(pf);
6058
6059         /* tell the firmware we are up */
6060         ret = ice_send_version(pf);
6061         if (ret) {
6062                 dev_err(dev, "Rebuild failed due to error sending driver version: %s\n",
6063                         ice_stat_str(ret));
6064                 goto err_vsi_rebuild;
6065         }
6066
6067         ice_replay_post(hw);
6068
6069         /* if we get here, reset flow is successful */
6070         clear_bit(__ICE_RESET_FAILED, pf->state);
6071         return;
6072
6073 err_vsi_rebuild:
6074 err_sched_init_port:
6075         ice_sched_cleanup_all(hw);
6076 err_init_ctrlq:
6077         ice_shutdown_all_ctrlq(hw);
6078         set_bit(__ICE_RESET_FAILED, pf->state);
6079 clear_recovery:
6080         /* set this bit in PF state to control service task scheduling */
6081         set_bit(__ICE_NEEDS_RESTART, pf->state);
6082         dev_err(dev, "Rebuild failed, unload and reload driver\n");
6083 }
6084
6085 /**
6086  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6087  * @vsi: Pointer to VSI structure
6088  */
6089 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6090 {
6091         if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6092                 return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6093         else
6094                 return ICE_RXBUF_3072;
6095 }
6096
6097 /**
6098  * ice_change_mtu - NDO callback to change the MTU
6099  * @netdev: network interface device structure
6100  * @new_mtu: new value for maximum frame size
6101  *
6102  * Returns 0 on success, negative on failure
6103  */
6104 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6105 {
6106         struct ice_netdev_priv *np = netdev_priv(netdev);
6107         struct ice_vsi *vsi = np->vsi;
6108         struct ice_pf *pf = vsi->back;
6109         u8 count = 0;
6110
6111         if (new_mtu == (int)netdev->mtu) {
6112                 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6113                 return 0;
6114         }
6115
6116         if (ice_is_xdp_ena_vsi(vsi)) {
6117                 int frame_size = ice_max_xdp_frame_size(vsi);
6118
6119                 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6120                         netdev_err(netdev, "max MTU for XDP usage is %d\n",
6121                                    frame_size - ICE_ETH_PKT_HDR_PAD);
6122                         return -EINVAL;
6123                 }
6124         }
6125
6126         if (new_mtu < (int)netdev->min_mtu) {
6127                 netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
6128                            netdev->min_mtu);
6129                 return -EINVAL;
6130         } else if (new_mtu > (int)netdev->max_mtu) {
6131                 netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
6132                            netdev->min_mtu);
6133                 return -EINVAL;
6134         }
6135         /* if a reset is in progress, wait for some time for it to complete */
6136         do {
6137                 if (ice_is_reset_in_progress(pf->state)) {
6138                         count++;
6139                         usleep_range(1000, 2000);
6140                 } else {
6141                         break;
6142                 }
6143
6144         } while (count < 100);
6145
6146         if (count == 100) {
6147                 netdev_err(netdev, "can't change MTU. Device is busy\n");
6148                 return -EBUSY;
6149         }
6150
6151         netdev->mtu = (unsigned int)new_mtu;
6152
6153         /* if VSI is up, bring it down and then back up */
6154         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
6155                 int err;
6156
6157                 err = ice_down(vsi);
6158                 if (err) {
6159                         netdev_err(netdev, "change MTU if_up err %d\n", err);
6160                         return err;
6161                 }
6162
6163                 err = ice_up(vsi);
6164                 if (err) {
6165                         netdev_err(netdev, "change MTU if_up err %d\n", err);
6166                         return err;
6167                 }
6168         }
6169
6170         netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6171         return 0;
6172 }
6173
6174 /**
6175  * ice_aq_str - convert AQ err code to a string
6176  * @aq_err: the AQ error code to convert
6177  */
6178 const char *ice_aq_str(enum ice_aq_err aq_err)
6179 {
6180         switch (aq_err) {
6181         case ICE_AQ_RC_OK:
6182                 return "OK";
6183         case ICE_AQ_RC_EPERM:
6184                 return "ICE_AQ_RC_EPERM";
6185         case ICE_AQ_RC_ENOENT:
6186                 return "ICE_AQ_RC_ENOENT";
6187         case ICE_AQ_RC_ENOMEM:
6188                 return "ICE_AQ_RC_ENOMEM";
6189         case ICE_AQ_RC_EBUSY:
6190                 return "ICE_AQ_RC_EBUSY";
6191         case ICE_AQ_RC_EEXIST:
6192                 return "ICE_AQ_RC_EEXIST";
6193         case ICE_AQ_RC_EINVAL:
6194                 return "ICE_AQ_RC_EINVAL";
6195         case ICE_AQ_RC_ENOSPC:
6196                 return "ICE_AQ_RC_ENOSPC";
6197         case ICE_AQ_RC_ENOSYS:
6198                 return "ICE_AQ_RC_ENOSYS";
6199         case ICE_AQ_RC_EMODE:
6200                 return "ICE_AQ_RC_EMODE";
6201         case ICE_AQ_RC_ENOSEC:
6202                 return "ICE_AQ_RC_ENOSEC";
6203         case ICE_AQ_RC_EBADSIG:
6204                 return "ICE_AQ_RC_EBADSIG";
6205         case ICE_AQ_RC_ESVN:
6206                 return "ICE_AQ_RC_ESVN";
6207         case ICE_AQ_RC_EBADMAN:
6208                 return "ICE_AQ_RC_EBADMAN";
6209         case ICE_AQ_RC_EBADBUF:
6210                 return "ICE_AQ_RC_EBADBUF";
6211         }
6212
6213         return "ICE_AQ_RC_UNKNOWN";
6214 }
6215
6216 /**
6217  * ice_stat_str - convert status err code to a string
6218  * @stat_err: the status error code to convert
6219  */
6220 const char *ice_stat_str(enum ice_status stat_err)
6221 {
6222         switch (stat_err) {
6223         case ICE_SUCCESS:
6224                 return "OK";
6225         case ICE_ERR_PARAM:
6226                 return "ICE_ERR_PARAM";
6227         case ICE_ERR_NOT_IMPL:
6228                 return "ICE_ERR_NOT_IMPL";
6229         case ICE_ERR_NOT_READY:
6230                 return "ICE_ERR_NOT_READY";
6231         case ICE_ERR_NOT_SUPPORTED:
6232                 return "ICE_ERR_NOT_SUPPORTED";
6233         case ICE_ERR_BAD_PTR:
6234                 return "ICE_ERR_BAD_PTR";
6235         case ICE_ERR_INVAL_SIZE:
6236                 return "ICE_ERR_INVAL_SIZE";
6237         case ICE_ERR_DEVICE_NOT_SUPPORTED:
6238                 return "ICE_ERR_DEVICE_NOT_SUPPORTED";
6239         case ICE_ERR_RESET_FAILED:
6240                 return "ICE_ERR_RESET_FAILED";
6241         case ICE_ERR_FW_API_VER:
6242                 return "ICE_ERR_FW_API_VER";
6243         case ICE_ERR_NO_MEMORY:
6244                 return "ICE_ERR_NO_MEMORY";
6245         case ICE_ERR_CFG:
6246                 return "ICE_ERR_CFG";
6247         case ICE_ERR_OUT_OF_RANGE:
6248                 return "ICE_ERR_OUT_OF_RANGE";
6249         case ICE_ERR_ALREADY_EXISTS:
6250                 return "ICE_ERR_ALREADY_EXISTS";
6251         case ICE_ERR_NVM_CHECKSUM:
6252                 return "ICE_ERR_NVM_CHECKSUM";
6253         case ICE_ERR_BUF_TOO_SHORT:
6254                 return "ICE_ERR_BUF_TOO_SHORT";
6255         case ICE_ERR_NVM_BLANK_MODE:
6256                 return "ICE_ERR_NVM_BLANK_MODE";
6257         case ICE_ERR_IN_USE:
6258                 return "ICE_ERR_IN_USE";
6259         case ICE_ERR_MAX_LIMIT:
6260                 return "ICE_ERR_MAX_LIMIT";
6261         case ICE_ERR_RESET_ONGOING:
6262                 return "ICE_ERR_RESET_ONGOING";
6263         case ICE_ERR_HW_TABLE:
6264                 return "ICE_ERR_HW_TABLE";
6265         case ICE_ERR_DOES_NOT_EXIST:
6266                 return "ICE_ERR_DOES_NOT_EXIST";
6267         case ICE_ERR_FW_DDP_MISMATCH:
6268                 return "ICE_ERR_FW_DDP_MISMATCH";
6269         case ICE_ERR_AQ_ERROR:
6270                 return "ICE_ERR_AQ_ERROR";
6271         case ICE_ERR_AQ_TIMEOUT:
6272                 return "ICE_ERR_AQ_TIMEOUT";
6273         case ICE_ERR_AQ_FULL:
6274                 return "ICE_ERR_AQ_FULL";
6275         case ICE_ERR_AQ_NO_WORK:
6276                 return "ICE_ERR_AQ_NO_WORK";
6277         case ICE_ERR_AQ_EMPTY:
6278                 return "ICE_ERR_AQ_EMPTY";
6279         case ICE_ERR_AQ_FW_CRITICAL:
6280                 return "ICE_ERR_AQ_FW_CRITICAL";
6281         }
6282
6283         return "ICE_ERR_UNKNOWN";
6284 }
6285
6286 /**
6287  * ice_set_rss - Set RSS keys and lut
6288  * @vsi: Pointer to VSI structure
6289  * @seed: RSS hash seed
6290  * @lut: Lookup table
6291  * @lut_size: Lookup table size
6292  *
6293  * Returns 0 on success, negative on failure
6294  */
6295 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6296 {
6297         struct ice_pf *pf = vsi->back;
6298         struct ice_hw *hw = &pf->hw;
6299         enum ice_status status;
6300         struct device *dev;
6301
6302         dev = ice_pf_to_dev(pf);
6303         if (seed) {
6304                 struct ice_aqc_get_set_rss_keys *buf =
6305                                   (struct ice_aqc_get_set_rss_keys *)seed;
6306
6307                 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
6308
6309                 if (status) {
6310                         dev_err(dev, "Cannot set RSS key, err %s aq_err %s\n",
6311                                 ice_stat_str(status),
6312                                 ice_aq_str(hw->adminq.sq_last_status));
6313                         return -EIO;
6314                 }
6315         }
6316
6317         if (lut) {
6318                 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6319                                             lut, lut_size);
6320                 if (status) {
6321                         dev_err(dev, "Cannot set RSS lut, err %s aq_err %s\n",
6322                                 ice_stat_str(status),
6323                                 ice_aq_str(hw->adminq.sq_last_status));
6324                         return -EIO;
6325                 }
6326         }
6327
6328         return 0;
6329 }
6330
6331 /**
6332  * ice_get_rss - Get RSS keys and lut
6333  * @vsi: Pointer to VSI structure
6334  * @seed: Buffer to store the keys
6335  * @lut: Buffer to store the lookup table entries
6336  * @lut_size: Size of buffer to store the lookup table entries
6337  *
6338  * Returns 0 on success, negative on failure
6339  */
6340 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6341 {
6342         struct ice_pf *pf = vsi->back;
6343         struct ice_hw *hw = &pf->hw;
6344         enum ice_status status;
6345         struct device *dev;
6346
6347         dev = ice_pf_to_dev(pf);
6348         if (seed) {
6349                 struct ice_aqc_get_set_rss_keys *buf =
6350                                   (struct ice_aqc_get_set_rss_keys *)seed;
6351
6352                 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
6353                 if (status) {
6354                         dev_err(dev, "Cannot get RSS key, err %s aq_err %s\n",
6355                                 ice_stat_str(status),
6356                                 ice_aq_str(hw->adminq.sq_last_status));
6357                         return -EIO;
6358                 }
6359         }
6360
6361         if (lut) {
6362                 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6363                                             lut, lut_size);
6364                 if (status) {
6365                         dev_err(dev, "Cannot get RSS lut, err %s aq_err %s\n",
6366                                 ice_stat_str(status),
6367                                 ice_aq_str(hw->adminq.sq_last_status));
6368                         return -EIO;
6369                 }
6370         }
6371
6372         return 0;
6373 }
6374
6375 /**
6376  * ice_bridge_getlink - Get the hardware bridge mode
6377  * @skb: skb buff
6378  * @pid: process ID
6379  * @seq: RTNL message seq
6380  * @dev: the netdev being configured
6381  * @filter_mask: filter mask passed in
6382  * @nlflags: netlink flags passed in
6383  *
6384  * Return the bridge mode (VEB/VEPA)
6385  */
6386 static int
6387 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
6388                    struct net_device *dev, u32 filter_mask, int nlflags)
6389 {
6390         struct ice_netdev_priv *np = netdev_priv(dev);
6391         struct ice_vsi *vsi = np->vsi;
6392         struct ice_pf *pf = vsi->back;
6393         u16 bmode;
6394
6395         bmode = pf->first_sw->bridge_mode;
6396
6397         return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
6398                                        filter_mask, NULL);
6399 }
6400
6401 /**
6402  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
6403  * @vsi: Pointer to VSI structure
6404  * @bmode: Hardware bridge mode (VEB/VEPA)
6405  *
6406  * Returns 0 on success, negative on failure
6407  */
6408 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
6409 {
6410         struct ice_aqc_vsi_props *vsi_props;
6411         struct ice_hw *hw = &vsi->back->hw;
6412         struct ice_vsi_ctx *ctxt;
6413         enum ice_status status;
6414         int ret = 0;
6415
6416         vsi_props = &vsi->info;
6417
6418         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
6419         if (!ctxt)
6420                 return -ENOMEM;
6421
6422         ctxt->info = vsi->info;
6423
6424         if (bmode == BRIDGE_MODE_VEB)
6425                 /* change from VEPA to VEB mode */
6426                 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6427         else
6428                 /* change from VEB to VEPA mode */
6429                 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6430         ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
6431
6432         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
6433         if (status) {
6434                 dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %s aq_err %s\n",
6435                         bmode, ice_stat_str(status),
6436                         ice_aq_str(hw->adminq.sq_last_status));
6437                 ret = -EIO;
6438                 goto out;
6439         }
6440         /* Update sw flags for book keeping */
6441         vsi_props->sw_flags = ctxt->info.sw_flags;
6442
6443 out:
6444         kfree(ctxt);
6445         return ret;
6446 }
6447
6448 /**
6449  * ice_bridge_setlink - Set the hardware bridge mode
6450  * @dev: the netdev being configured
6451  * @nlh: RTNL message
6452  * @flags: bridge setlink flags
6453  * @extack: netlink extended ack
6454  *
6455  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
6456  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
6457  * not already set for all VSIs connected to this switch. And also update the
6458  * unicast switch filter rules for the corresponding switch of the netdev.
6459  */
6460 static int
6461 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
6462                    u16 __always_unused flags,
6463                    struct netlink_ext_ack __always_unused *extack)
6464 {
6465         struct ice_netdev_priv *np = netdev_priv(dev);
6466         struct ice_pf *pf = np->vsi->back;
6467         struct nlattr *attr, *br_spec;
6468         struct ice_hw *hw = &pf->hw;
6469         enum ice_status status;
6470         struct ice_sw *pf_sw;
6471         int rem, v, err = 0;
6472
6473         pf_sw = pf->first_sw;
6474         /* find the attribute in the netlink message */
6475         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
6476
6477         nla_for_each_nested(attr, br_spec, rem) {
6478                 __u16 mode;
6479
6480                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
6481                         continue;
6482                 mode = nla_get_u16(attr);
6483                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
6484                         return -EINVAL;
6485                 /* Continue  if bridge mode is not being flipped */
6486                 if (mode == pf_sw->bridge_mode)
6487                         continue;
6488                 /* Iterates through the PF VSI list and update the loopback
6489                  * mode of the VSI
6490                  */
6491                 ice_for_each_vsi(pf, v) {
6492                         if (!pf->vsi[v])
6493                                 continue;
6494                         err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
6495                         if (err)
6496                                 return err;
6497                 }
6498
6499                 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
6500                 /* Update the unicast switch filter rules for the corresponding
6501                  * switch of the netdev
6502                  */
6503                 status = ice_update_sw_rule_bridge_mode(hw);
6504                 if (status) {
6505                         netdev_err(dev, "switch rule update failed, mode = %d err %s aq_err %s\n",
6506                                    mode, ice_stat_str(status),
6507                                    ice_aq_str(hw->adminq.sq_last_status));
6508                         /* revert hw->evb_veb */
6509                         hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
6510                         return -EIO;
6511                 }
6512
6513                 pf_sw->bridge_mode = mode;
6514         }
6515
6516         return 0;
6517 }
6518
6519 /**
6520  * ice_tx_timeout - Respond to a Tx Hang
6521  * @netdev: network interface device structure
6522  * @txqueue: Tx queue
6523  */
6524 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
6525 {
6526         struct ice_netdev_priv *np = netdev_priv(netdev);
6527         struct ice_ring *tx_ring = NULL;
6528         struct ice_vsi *vsi = np->vsi;
6529         struct ice_pf *pf = vsi->back;
6530         u32 i;
6531
6532         pf->tx_timeout_count++;
6533
6534         /* Check if PFC is enabled for the TC to which the queue belongs
6535          * to. If yes then Tx timeout is not caused by a hung queue, no
6536          * need to reset and rebuild
6537          */
6538         if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
6539                 dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
6540                          txqueue);
6541                 return;
6542         }
6543
6544         /* now that we have an index, find the tx_ring struct */
6545         for (i = 0; i < vsi->num_txq; i++)
6546                 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
6547                         if (txqueue == vsi->tx_rings[i]->q_index) {
6548                                 tx_ring = vsi->tx_rings[i];
6549                                 break;
6550                         }
6551
6552         /* Reset recovery level if enough time has elapsed after last timeout.
6553          * Also ensure no new reset action happens before next timeout period.
6554          */
6555         if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
6556                 pf->tx_timeout_recovery_level = 1;
6557         else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
6558                                        netdev->watchdog_timeo)))
6559                 return;
6560
6561         if (tx_ring) {
6562                 struct ice_hw *hw = &pf->hw;
6563                 u32 head, val = 0;
6564
6565                 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
6566                         QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
6567                 /* Read interrupt register */
6568                 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
6569
6570                 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
6571                             vsi->vsi_num, txqueue, tx_ring->next_to_clean,
6572                             head, tx_ring->next_to_use, val);
6573         }
6574
6575         pf->tx_timeout_last_recovery = jiffies;
6576         netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
6577                     pf->tx_timeout_recovery_level, txqueue);
6578
6579         switch (pf->tx_timeout_recovery_level) {
6580         case 1:
6581                 set_bit(__ICE_PFR_REQ, pf->state);
6582                 break;
6583         case 2:
6584                 set_bit(__ICE_CORER_REQ, pf->state);
6585                 break;
6586         case 3:
6587                 set_bit(__ICE_GLOBR_REQ, pf->state);
6588                 break;
6589         default:
6590                 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
6591                 set_bit(__ICE_DOWN, pf->state);
6592                 set_bit(__ICE_NEEDS_RESTART, vsi->state);
6593                 set_bit(__ICE_SERVICE_DIS, pf->state);
6594                 break;
6595         }
6596
6597         ice_service_task_schedule(pf);
6598         pf->tx_timeout_recovery_level++;
6599 }
6600
6601 /**
6602  * ice_open - Called when a network interface becomes active
6603  * @netdev: network interface device structure
6604  *
6605  * The open entry point is called when a network interface is made
6606  * active by the system (IFF_UP). At this point all resources needed
6607  * for transmit and receive operations are allocated, the interrupt
6608  * handler is registered with the OS, the netdev watchdog is enabled,
6609  * and the stack is notified that the interface is ready.
6610  *
6611  * Returns 0 on success, negative value on failure
6612  */
6613 int ice_open(struct net_device *netdev)
6614 {
6615         struct ice_netdev_priv *np = netdev_priv(netdev);
6616         struct ice_vsi *vsi = np->vsi;
6617         struct ice_pf *pf = vsi->back;
6618         struct ice_port_info *pi;
6619         int err;
6620
6621         if (test_bit(__ICE_NEEDS_RESTART, pf->state)) {
6622                 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6623                 return -EIO;
6624         }
6625
6626         if (test_bit(__ICE_DOWN, pf->state)) {
6627                 netdev_err(netdev, "device is not ready yet\n");
6628                 return -EBUSY;
6629         }
6630
6631         netif_carrier_off(netdev);
6632
6633         pi = vsi->port_info;
6634         err = ice_update_link_info(pi);
6635         if (err) {
6636                 netdev_err(netdev, "Failed to get link info, error %d\n",
6637                            err);
6638                 return err;
6639         }
6640
6641         /* Set PHY if there is media, otherwise, turn off PHY */
6642         if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
6643                 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6644                 if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state)) {
6645                         err = ice_init_phy_user_cfg(pi);
6646                         if (err) {
6647                                 netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
6648                                            err);
6649                                 return err;
6650                         }
6651                 }
6652
6653                 err = ice_configure_phy(vsi);
6654                 if (err) {
6655                         netdev_err(netdev, "Failed to set physical link up, error %d\n",
6656                                    err);
6657                         return err;
6658                 }
6659         } else {
6660                 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6661                 err = ice_aq_set_link_restart_an(pi, false, NULL);
6662                 if (err) {
6663                         netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
6664                                    vsi->vsi_num, err);
6665                         return err;
6666                 }
6667         }
6668
6669         err = ice_vsi_open(vsi);
6670         if (err)
6671                 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6672                            vsi->vsi_num, vsi->vsw->sw_id);
6673
6674         /* Update existing tunnels information */
6675         udp_tunnel_get_rx_info(netdev);
6676
6677         return err;
6678 }
6679
6680 /**
6681  * ice_stop - Disables a network interface
6682  * @netdev: network interface device structure
6683  *
6684  * The stop entry point is called when an interface is de-activated by the OS,
6685  * and the netdevice enters the DOWN state. The hardware is still under the
6686  * driver's control, but the netdev interface is disabled.
6687  *
6688  * Returns success only - not allowed to fail
6689  */
6690 int ice_stop(struct net_device *netdev)
6691 {
6692         struct ice_netdev_priv *np = netdev_priv(netdev);
6693         struct ice_vsi *vsi = np->vsi;
6694
6695         ice_vsi_close(vsi);
6696
6697         return 0;
6698 }
6699
6700 /**
6701  * ice_features_check - Validate encapsulated packet conforms to limits
6702  * @skb: skb buffer
6703  * @netdev: This port's netdev
6704  * @features: Offload features that the stack believes apply
6705  */
6706 static netdev_features_t
6707 ice_features_check(struct sk_buff *skb,
6708                    struct net_device __always_unused *netdev,
6709                    netdev_features_t features)
6710 {
6711         size_t len;
6712
6713         /* No point in doing any of this if neither checksum nor GSO are
6714          * being requested for this frame. We can rule out both by just
6715          * checking for CHECKSUM_PARTIAL
6716          */
6717         if (skb->ip_summed != CHECKSUM_PARTIAL)
6718                 return features;
6719
6720         /* We cannot support GSO if the MSS is going to be less than
6721          * 64 bytes. If it is then we need to drop support for GSO.
6722          */
6723         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6724                 features &= ~NETIF_F_GSO_MASK;
6725
6726         len = skb_network_header(skb) - skb->data;
6727         if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
6728                 goto out_rm_features;
6729
6730         len = skb_transport_header(skb) - skb_network_header(skb);
6731         if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6732                 goto out_rm_features;
6733
6734         if (skb->encapsulation) {
6735                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
6736                 if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
6737                         goto out_rm_features;
6738
6739                 len = skb_inner_transport_header(skb) -
6740                       skb_inner_network_header(skb);
6741                 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6742                         goto out_rm_features;
6743         }
6744
6745         return features;
6746 out_rm_features:
6747         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6748 }
6749
6750 static const struct net_device_ops ice_netdev_safe_mode_ops = {
6751         .ndo_open = ice_open,
6752         .ndo_stop = ice_stop,
6753         .ndo_start_xmit = ice_start_xmit,
6754         .ndo_set_mac_address = ice_set_mac_address,
6755         .ndo_validate_addr = eth_validate_addr,
6756         .ndo_change_mtu = ice_change_mtu,
6757         .ndo_get_stats64 = ice_get_stats64,
6758         .ndo_tx_timeout = ice_tx_timeout,
6759 };
6760
6761 static const struct net_device_ops ice_netdev_ops = {
6762         .ndo_open = ice_open,
6763         .ndo_stop = ice_stop,
6764         .ndo_start_xmit = ice_start_xmit,
6765         .ndo_features_check = ice_features_check,
6766         .ndo_set_rx_mode = ice_set_rx_mode,
6767         .ndo_set_mac_address = ice_set_mac_address,
6768         .ndo_validate_addr = eth_validate_addr,
6769         .ndo_change_mtu = ice_change_mtu,
6770         .ndo_get_stats64 = ice_get_stats64,
6771         .ndo_set_tx_maxrate = ice_set_tx_maxrate,
6772         .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
6773         .ndo_set_vf_mac = ice_set_vf_mac,
6774         .ndo_get_vf_config = ice_get_vf_cfg,
6775         .ndo_set_vf_trust = ice_set_vf_trust,
6776         .ndo_set_vf_vlan = ice_set_vf_port_vlan,
6777         .ndo_set_vf_link_state = ice_set_vf_link_state,
6778         .ndo_get_vf_stats = ice_get_vf_stats,
6779         .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6780         .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6781         .ndo_set_features = ice_set_features,
6782         .ndo_bridge_getlink = ice_bridge_getlink,
6783         .ndo_bridge_setlink = ice_bridge_setlink,
6784         .ndo_fdb_add = ice_fdb_add,
6785         .ndo_fdb_del = ice_fdb_del,
6786 #ifdef CONFIG_RFS_ACCEL
6787         .ndo_rx_flow_steer = ice_rx_flow_steer,
6788 #endif
6789         .ndo_tx_timeout = ice_tx_timeout,
6790         .ndo_bpf = ice_xdp,
6791         .ndo_xdp_xmit = ice_xdp_xmit,
6792         .ndo_xsk_wakeup = ice_xsk_wakeup,
6793         .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
6794         .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
6795 };
This page took 0.437116 seconds and 4 git commands to generate.