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