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