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