]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_main.c
ice: Add support for VF reset events
[linux.git] / drivers / net / ethernet / intel / ice / ice_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include "ice.h"
9 #include "ice_lib.h"
10
11 #define DRV_VERSION     "0.7.1-k"
12 #define DRV_SUMMARY     "Intel(R) Ethernet Connection E800 Series Linux Driver"
13 const char ice_drv_ver[] = DRV_VERSION;
14 static const char ice_driver_string[] = DRV_SUMMARY;
15 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
16
17 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
18 MODULE_DESCRIPTION(DRV_SUMMARY);
19 MODULE_LICENSE("GPL v2");
20 MODULE_VERSION(DRV_VERSION);
21
22 static int debug = -1;
23 module_param(debug, int, 0644);
24 #ifndef CONFIG_DYNAMIC_DEBUG
25 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
26 #else
27 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
28 #endif /* !CONFIG_DYNAMIC_DEBUG */
29
30 static struct workqueue_struct *ice_wq;
31 static const struct net_device_ops ice_netdev_ops;
32
33 static void ice_pf_dis_all_vsi(struct ice_pf *pf);
34 static void ice_rebuild(struct ice_pf *pf);
35
36 static void ice_vsi_release_all(struct ice_pf *pf);
37 static void ice_update_vsi_stats(struct ice_vsi *vsi);
38 static void ice_update_pf_stats(struct ice_pf *pf);
39
40 /**
41  * ice_get_tx_pending - returns number of Tx descriptors not processed
42  * @ring: the ring of descriptors
43  */
44 static u32 ice_get_tx_pending(struct ice_ring *ring)
45 {
46         u32 head, tail;
47
48         head = ring->next_to_clean;
49         tail = readl(ring->tail);
50
51         if (head != tail)
52                 return (head < tail) ?
53                         tail - head : (tail + ring->count - head);
54         return 0;
55 }
56
57 /**
58  * ice_check_for_hang_subtask - check for and recover hung queues
59  * @pf: pointer to PF struct
60  */
61 static void ice_check_for_hang_subtask(struct ice_pf *pf)
62 {
63         struct ice_vsi *vsi = NULL;
64         unsigned int i;
65         u32 v, v_idx;
66         int packets;
67
68         ice_for_each_vsi(pf, v)
69                 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
70                         vsi = pf->vsi[v];
71                         break;
72                 }
73
74         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
75                 return;
76
77         if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
78                 return;
79
80         for (i = 0; i < vsi->num_txq; i++) {
81                 struct ice_ring *tx_ring = vsi->tx_rings[i];
82
83                 if (tx_ring && tx_ring->desc) {
84                         int itr = ICE_ITR_NONE;
85
86                         /* If packet counter has not changed the queue is
87                          * likely stalled, so force an interrupt for this
88                          * queue.
89                          *
90                          * prev_pkt would be negative if there was no
91                          * pending work.
92                          */
93                         packets = tx_ring->stats.pkts & INT_MAX;
94                         if (tx_ring->tx_stats.prev_pkt == packets) {
95                                 /* Trigger sw interrupt to revive the queue */
96                                 v_idx = tx_ring->q_vector->v_idx;
97                                 wr32(&vsi->back->hw,
98                                      GLINT_DYN_CTL(vsi->hw_base_vector + v_idx),
99                                      (itr << GLINT_DYN_CTL_ITR_INDX_S) |
100                                      GLINT_DYN_CTL_SWINT_TRIG_M |
101                                      GLINT_DYN_CTL_INTENA_MSK_M);
102                                 continue;
103                         }
104
105                         /* Memory barrier between read of packet count and call
106                          * to ice_get_tx_pending()
107                          */
108                         smp_rmb();
109                         tx_ring->tx_stats.prev_pkt =
110                             ice_get_tx_pending(tx_ring) ? packets : -1;
111                 }
112         }
113 }
114
115 /**
116  * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
117  * @netdev: the net device on which the sync is happening
118  * @addr: mac address to sync
119  *
120  * This is a callback function which is called by the in kernel device sync
121  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
122  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
123  * mac filters from the hardware.
124  */
125 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
126 {
127         struct ice_netdev_priv *np = netdev_priv(netdev);
128         struct ice_vsi *vsi = np->vsi;
129
130         if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
131                 return -EINVAL;
132
133         return 0;
134 }
135
136 /**
137  * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
138  * @netdev: the net device on which the unsync is happening
139  * @addr: mac address to unsync
140  *
141  * This is a callback function which is called by the in kernel device unsync
142  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
143  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
144  * delete the mac filters from the hardware.
145  */
146 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
147 {
148         struct ice_netdev_priv *np = netdev_priv(netdev);
149         struct ice_vsi *vsi = np->vsi;
150
151         if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
152                 return -EINVAL;
153
154         return 0;
155 }
156
157 /**
158  * ice_vsi_fltr_changed - check if filter state changed
159  * @vsi: VSI to be checked
160  *
161  * returns true if filter state has changed, false otherwise.
162  */
163 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
164 {
165         return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
166                test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
167                test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
168 }
169
170 /**
171  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
172  * @vsi: ptr to the VSI
173  *
174  * Push any outstanding VSI filter changes through the AdminQ.
175  */
176 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
177 {
178         struct device *dev = &vsi->back->pdev->dev;
179         struct net_device *netdev = vsi->netdev;
180         bool promisc_forced_on = false;
181         struct ice_pf *pf = vsi->back;
182         struct ice_hw *hw = &pf->hw;
183         enum ice_status status = 0;
184         u32 changed_flags = 0;
185         int err = 0;
186
187         if (!vsi->netdev)
188                 return -EINVAL;
189
190         while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
191                 usleep_range(1000, 2000);
192
193         changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
194         vsi->current_netdev_flags = vsi->netdev->flags;
195
196         INIT_LIST_HEAD(&vsi->tmp_sync_list);
197         INIT_LIST_HEAD(&vsi->tmp_unsync_list);
198
199         if (ice_vsi_fltr_changed(vsi)) {
200                 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
201                 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
202                 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
203
204                 /* grab the netdev's addr_list_lock */
205                 netif_addr_lock_bh(netdev);
206                 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
207                               ice_add_mac_to_unsync_list);
208                 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
209                               ice_add_mac_to_unsync_list);
210                 /* our temp lists are populated. release lock */
211                 netif_addr_unlock_bh(netdev);
212         }
213
214         /* Remove mac addresses in the unsync list */
215         status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
216         ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
217         if (status) {
218                 netdev_err(netdev, "Failed to delete MAC filters\n");
219                 /* if we failed because of alloc failures, just bail */
220                 if (status == ICE_ERR_NO_MEMORY) {
221                         err = -ENOMEM;
222                         goto out;
223                 }
224         }
225
226         /* Add mac addresses in the sync list */
227         status = ice_add_mac(hw, &vsi->tmp_sync_list);
228         ice_free_fltr_list(dev, &vsi->tmp_sync_list);
229         if (status) {
230                 netdev_err(netdev, "Failed to add MAC filters\n");
231                 /* If there is no more space for new umac filters, vsi
232                  * should go into promiscuous mode. There should be some
233                  * space reserved for promiscuous filters.
234                  */
235                 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
236                     !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
237                                       vsi->state)) {
238                         promisc_forced_on = true;
239                         netdev_warn(netdev,
240                                     "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
241                                     vsi->vsi_num);
242                 } else {
243                         err = -EIO;
244                         goto out;
245                 }
246         }
247         /* check for changes in promiscuous modes */
248         if (changed_flags & IFF_ALLMULTI)
249                 netdev_warn(netdev, "Unsupported configuration\n");
250
251         if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
252             test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
253                 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
254                 if (vsi->current_netdev_flags & IFF_PROMISC) {
255                         /* Apply TX filter rule to get traffic from VMs */
256                         status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
257                                                   ICE_FLTR_TX);
258                         if (status) {
259                                 netdev_err(netdev, "Error setting default VSI %i tx rule\n",
260                                            vsi->vsi_num);
261                                 vsi->current_netdev_flags &= ~IFF_PROMISC;
262                                 err = -EIO;
263                                 goto out_promisc;
264                         }
265                         /* Apply RX filter rule to get traffic from wire */
266                         status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
267                                                   ICE_FLTR_RX);
268                         if (status) {
269                                 netdev_err(netdev, "Error setting default VSI %i rx rule\n",
270                                            vsi->vsi_num);
271                                 vsi->current_netdev_flags &= ~IFF_PROMISC;
272                                 err = -EIO;
273                                 goto out_promisc;
274                         }
275                 } else {
276                         /* Clear TX filter rule to stop traffic from VMs */
277                         status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
278                                                   ICE_FLTR_TX);
279                         if (status) {
280                                 netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
281                                            vsi->vsi_num);
282                                 vsi->current_netdev_flags |= IFF_PROMISC;
283                                 err = -EIO;
284                                 goto out_promisc;
285                         }
286                         /* Clear RX filter to remove traffic from wire */
287                         status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
288                                                   ICE_FLTR_RX);
289                         if (status) {
290                                 netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
291                                            vsi->vsi_num);
292                                 vsi->current_netdev_flags |= IFF_PROMISC;
293                                 err = -EIO;
294                                 goto out_promisc;
295                         }
296                 }
297         }
298         goto exit;
299
300 out_promisc:
301         set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
302         goto exit;
303 out:
304         /* if something went wrong then set the changed flag so we try again */
305         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
306         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
307 exit:
308         clear_bit(__ICE_CFG_BUSY, vsi->state);
309         return err;
310 }
311
312 /**
313  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
314  * @pf: board private structure
315  */
316 static void ice_sync_fltr_subtask(struct ice_pf *pf)
317 {
318         int v;
319
320         if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
321                 return;
322
323         clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
324
325         for (v = 0; v < pf->num_alloc_vsi; v++)
326                 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
327                     ice_vsi_sync_fltr(pf->vsi[v])) {
328                         /* come back and try again later */
329                         set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
330                         break;
331                 }
332 }
333
334 /**
335  * ice_prepare_for_reset - prep for the core to reset
336  * @pf: board private structure
337  *
338  * Inform or close all dependent features in prep for reset.
339  */
340 static void
341 ice_prepare_for_reset(struct ice_pf *pf)
342 {
343         struct ice_hw *hw = &pf->hw;
344
345         /* Notify VFs of impending reset */
346         if (ice_check_sq_alive(hw, &hw->mailboxq))
347                 ice_vc_notify_reset(pf);
348
349         /* disable the VSIs and their queues that are not already DOWN */
350         ice_pf_dis_all_vsi(pf);
351
352         ice_shutdown_all_ctrlq(hw);
353
354         set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
355 }
356
357 /**
358  * ice_do_reset - Initiate one of many types of resets
359  * @pf: board private structure
360  * @reset_type: reset type requested
361  * before this function was called.
362  */
363 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
364 {
365         struct device *dev = &pf->pdev->dev;
366         struct ice_hw *hw = &pf->hw;
367
368         dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
369         WARN_ON(in_interrupt());
370
371         ice_prepare_for_reset(pf);
372
373         /* trigger the reset */
374         if (ice_reset(hw, reset_type)) {
375                 dev_err(dev, "reset %d failed\n", reset_type);
376                 set_bit(__ICE_RESET_FAILED, pf->state);
377                 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
378                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
379                 clear_bit(__ICE_PFR_REQ, pf->state);
380                 clear_bit(__ICE_CORER_REQ, pf->state);
381                 clear_bit(__ICE_GLOBR_REQ, pf->state);
382                 return;
383         }
384
385         /* PFR is a bit of a special case because it doesn't result in an OICR
386          * interrupt. So for PFR, rebuild after the reset and clear the reset-
387          * associated state bits.
388          */
389         if (reset_type == ICE_RESET_PFR) {
390                 pf->pfr_count++;
391                 ice_rebuild(pf);
392                 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
393                 clear_bit(__ICE_PFR_REQ, pf->state);
394         }
395 }
396
397 /**
398  * ice_reset_subtask - Set up for resetting the device and driver
399  * @pf: board private structure
400  */
401 static void ice_reset_subtask(struct ice_pf *pf)
402 {
403         enum ice_reset_req reset_type = ICE_RESET_INVAL;
404
405         /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
406          * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
407          * of reset is pending and sets bits in pf->state indicating the reset
408          * type and __ICE_RESET_OICR_RECV.  So, if the latter bit is set
409          * prepare for pending reset if not already (for PF software-initiated
410          * global resets the software should already be prepared for it as
411          * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
412          * by firmware or software on other PFs, that bit is not set so prepare
413          * for the reset now), poll for reset done, rebuild and return.
414          */
415         if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
416                 clear_bit(__ICE_GLOBR_RECV, pf->state);
417                 clear_bit(__ICE_CORER_RECV, pf->state);
418                 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
419                         ice_prepare_for_reset(pf);
420
421                 /* make sure we are ready to rebuild */
422                 if (ice_check_reset(&pf->hw)) {
423                         set_bit(__ICE_RESET_FAILED, pf->state);
424                 } else {
425                         /* done with reset. start rebuild */
426                         pf->hw.reset_ongoing = false;
427                         ice_rebuild(pf);
428                         /* clear bit to resume normal operations, but
429                          * ICE_NEEDS_RESTART bit is set incase rebuild failed
430                          */
431                         clear_bit(__ICE_RESET_OICR_RECV, pf->state);
432                         clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
433                         clear_bit(__ICE_PFR_REQ, pf->state);
434                         clear_bit(__ICE_CORER_REQ, pf->state);
435                         clear_bit(__ICE_GLOBR_REQ, pf->state);
436                 }
437
438                 return;
439         }
440
441         /* No pending resets to finish processing. Check for new resets */
442         if (test_bit(__ICE_PFR_REQ, pf->state))
443                 reset_type = ICE_RESET_PFR;
444         if (test_bit(__ICE_CORER_REQ, pf->state))
445                 reset_type = ICE_RESET_CORER;
446         if (test_bit(__ICE_GLOBR_REQ, pf->state))
447                 reset_type = ICE_RESET_GLOBR;
448         /* If no valid reset type requested just return */
449         if (reset_type == ICE_RESET_INVAL)
450                 return;
451
452         /* reset if not already down or busy */
453         if (!test_bit(__ICE_DOWN, pf->state) &&
454             !test_bit(__ICE_CFG_BUSY, pf->state)) {
455                 ice_do_reset(pf, reset_type);
456         }
457 }
458
459 /**
460  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
461  * @pf: board private structure
462  */
463 static void ice_watchdog_subtask(struct ice_pf *pf)
464 {
465         int i;
466
467         /* if interface is down do nothing */
468         if (test_bit(__ICE_DOWN, pf->state) ||
469             test_bit(__ICE_CFG_BUSY, pf->state))
470                 return;
471
472         /* make sure we don't do these things too often */
473         if (time_before(jiffies,
474                         pf->serv_tmr_prev + pf->serv_tmr_period))
475                 return;
476
477         pf->serv_tmr_prev = jiffies;
478
479         /* Update the stats for active netdevs so the network stack
480          * can look at updated numbers whenever it cares to
481          */
482         ice_update_pf_stats(pf);
483         for (i = 0; i < pf->num_alloc_vsi; i++)
484                 if (pf->vsi[i] && pf->vsi[i]->netdev)
485                         ice_update_vsi_stats(pf->vsi[i]);
486 }
487
488 /**
489  * ice_print_link_msg - print link up or down message
490  * @vsi: the VSI whose link status is being queried
491  * @isup: boolean for if the link is now up or down
492  */
493 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
494 {
495         const char *speed;
496         const char *fc;
497
498         if (vsi->current_isup == isup)
499                 return;
500
501         vsi->current_isup = isup;
502
503         if (!isup) {
504                 netdev_info(vsi->netdev, "NIC Link is Down\n");
505                 return;
506         }
507
508         switch (vsi->port_info->phy.link_info.link_speed) {
509         case ICE_AQ_LINK_SPEED_40GB:
510                 speed = "40 G";
511                 break;
512         case ICE_AQ_LINK_SPEED_25GB:
513                 speed = "25 G";
514                 break;
515         case ICE_AQ_LINK_SPEED_20GB:
516                 speed = "20 G";
517                 break;
518         case ICE_AQ_LINK_SPEED_10GB:
519                 speed = "10 G";
520                 break;
521         case ICE_AQ_LINK_SPEED_5GB:
522                 speed = "5 G";
523                 break;
524         case ICE_AQ_LINK_SPEED_2500MB:
525                 speed = "2.5 G";
526                 break;
527         case ICE_AQ_LINK_SPEED_1000MB:
528                 speed = "1 G";
529                 break;
530         case ICE_AQ_LINK_SPEED_100MB:
531                 speed = "100 M";
532                 break;
533         default:
534                 speed = "Unknown";
535                 break;
536         }
537
538         switch (vsi->port_info->fc.current_mode) {
539         case ICE_FC_FULL:
540                 fc = "RX/TX";
541                 break;
542         case ICE_FC_TX_PAUSE:
543                 fc = "TX";
544                 break;
545         case ICE_FC_RX_PAUSE:
546                 fc = "RX";
547                 break;
548         default:
549                 fc = "Unknown";
550                 break;
551         }
552
553         netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
554                     speed, fc);
555 }
556
557 /**
558  * ice_init_link_events - enable/initialize link events
559  * @pi: pointer to the port_info instance
560  *
561  * Returns -EIO on failure, 0 on success
562  */
563 static int ice_init_link_events(struct ice_port_info *pi)
564 {
565         u16 mask;
566
567         mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
568                        ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
569
570         if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
571                 dev_dbg(ice_hw_to_dev(pi->hw),
572                         "Failed to set link event mask for port %d\n",
573                         pi->lport);
574                 return -EIO;
575         }
576
577         if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
578                 dev_dbg(ice_hw_to_dev(pi->hw),
579                         "Failed to enable link events for port %d\n",
580                         pi->lport);
581                 return -EIO;
582         }
583
584         return 0;
585 }
586
587 /**
588  * ice_vsi_link_event - update the vsi's netdev
589  * @vsi: the vsi on which the link event occurred
590  * @link_up: whether or not the vsi needs to be set up or down
591  */
592 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
593 {
594         if (!vsi || test_bit(__ICE_DOWN, vsi->state))
595                 return;
596
597         if (vsi->type == ICE_VSI_PF) {
598                 if (!vsi->netdev) {
599                         dev_dbg(&vsi->back->pdev->dev,
600                                 "vsi->netdev is not initialized!\n");
601                         return;
602                 }
603                 if (link_up) {
604                         netif_carrier_on(vsi->netdev);
605                         netif_tx_wake_all_queues(vsi->netdev);
606                 } else {
607                         netif_carrier_off(vsi->netdev);
608                         netif_tx_stop_all_queues(vsi->netdev);
609                 }
610         }
611 }
612
613 /**
614  * ice_link_event - process the link event
615  * @pf: pf that the link event is associated with
616  * @pi: port_info for the port that the link event is associated with
617  *
618  * Returns -EIO if ice_get_link_status() fails
619  * Returns 0 on success
620  */
621 static int
622 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
623 {
624         u8 new_link_speed, old_link_speed;
625         struct ice_phy_info *phy_info;
626         bool new_link_same_as_old;
627         bool new_link, old_link;
628         u8 lport;
629         u16 v;
630
631         phy_info = &pi->phy;
632         phy_info->link_info_old = phy_info->link_info;
633         /* Force ice_get_link_status() to update link info */
634         phy_info->get_link_info = true;
635
636         old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
637         old_link_speed = phy_info->link_info_old.link_speed;
638
639         lport = pi->lport;
640         if (ice_get_link_status(pi, &new_link)) {
641                 dev_dbg(&pf->pdev->dev,
642                         "Could not get link status for port %d\n", lport);
643                 return -EIO;
644         }
645
646         new_link_speed = phy_info->link_info.link_speed;
647
648         new_link_same_as_old = (new_link == old_link &&
649                                 new_link_speed == old_link_speed);
650
651         ice_for_each_vsi(pf, v) {
652                 struct ice_vsi *vsi = pf->vsi[v];
653
654                 if (!vsi || !vsi->port_info)
655                         continue;
656
657                 if (new_link_same_as_old &&
658                     (test_bit(__ICE_DOWN, vsi->state) ||
659                     new_link == netif_carrier_ok(vsi->netdev)))
660                         continue;
661
662                 if (vsi->port_info->lport == lport) {
663                         ice_print_link_msg(vsi, new_link);
664                         ice_vsi_link_event(vsi, new_link);
665                 }
666         }
667
668         return 0;
669 }
670
671 /**
672  * ice_handle_link_event - handle link event via ARQ
673  * @pf: pf that the link event is associated with
674  *
675  * Return -EINVAL if port_info is null
676  * Return status on succes
677  */
678 static int ice_handle_link_event(struct ice_pf *pf)
679 {
680         struct ice_port_info *port_info;
681         int status;
682
683         port_info = pf->hw.port_info;
684         if (!port_info)
685                 return -EINVAL;
686
687         status = ice_link_event(pf, port_info);
688         if (status)
689                 dev_dbg(&pf->pdev->dev,
690                         "Could not process link event, error %d\n", status);
691
692         return status;
693 }
694
695 /**
696  * __ice_clean_ctrlq - helper function to clean controlq rings
697  * @pf: ptr to struct ice_pf
698  * @q_type: specific Control queue type
699  */
700 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
701 {
702         struct ice_rq_event_info event;
703         struct ice_hw *hw = &pf->hw;
704         struct ice_ctl_q_info *cq;
705         u16 pending, i = 0;
706         const char *qtype;
707         u32 oldval, val;
708
709         /* Do not clean control queue if/when PF reset fails */
710         if (test_bit(__ICE_RESET_FAILED, pf->state))
711                 return 0;
712
713         switch (q_type) {
714         case ICE_CTL_Q_ADMIN:
715                 cq = &hw->adminq;
716                 qtype = "Admin";
717                 break;
718         case ICE_CTL_Q_MAILBOX:
719                 cq = &hw->mailboxq;
720                 qtype = "Mailbox";
721                 break;
722         default:
723                 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
724                          q_type);
725                 return 0;
726         }
727
728         /* check for error indications - PF_xx_AxQLEN register layout for
729          * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
730          */
731         val = rd32(hw, cq->rq.len);
732         if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
733                    PF_FW_ARQLEN_ARQCRIT_M)) {
734                 oldval = val;
735                 if (val & PF_FW_ARQLEN_ARQVFE_M)
736                         dev_dbg(&pf->pdev->dev,
737                                 "%s Receive Queue VF Error detected\n", qtype);
738                 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
739                         dev_dbg(&pf->pdev->dev,
740                                 "%s Receive Queue Overflow Error detected\n",
741                                 qtype);
742                 }
743                 if (val & PF_FW_ARQLEN_ARQCRIT_M)
744                         dev_dbg(&pf->pdev->dev,
745                                 "%s Receive Queue Critical Error detected\n",
746                                 qtype);
747                 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
748                          PF_FW_ARQLEN_ARQCRIT_M);
749                 if (oldval != val)
750                         wr32(hw, cq->rq.len, val);
751         }
752
753         val = rd32(hw, cq->sq.len);
754         if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
755                    PF_FW_ATQLEN_ATQCRIT_M)) {
756                 oldval = val;
757                 if (val & PF_FW_ATQLEN_ATQVFE_M)
758                         dev_dbg(&pf->pdev->dev,
759                                 "%s Send Queue VF Error detected\n", qtype);
760                 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
761                         dev_dbg(&pf->pdev->dev,
762                                 "%s Send Queue Overflow Error detected\n",
763                                 qtype);
764                 }
765                 if (val & PF_FW_ATQLEN_ATQCRIT_M)
766                         dev_dbg(&pf->pdev->dev,
767                                 "%s Send Queue Critical Error detected\n",
768                                 qtype);
769                 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
770                          PF_FW_ATQLEN_ATQCRIT_M);
771                 if (oldval != val)
772                         wr32(hw, cq->sq.len, val);
773         }
774
775         event.buf_len = cq->rq_buf_size;
776         event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
777                                      GFP_KERNEL);
778         if (!event.msg_buf)
779                 return 0;
780
781         do {
782                 enum ice_status ret;
783                 u16 opcode;
784
785                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
786                 if (ret == ICE_ERR_AQ_NO_WORK)
787                         break;
788                 if (ret) {
789                         dev_err(&pf->pdev->dev,
790                                 "%s Receive Queue event error %d\n", qtype,
791                                 ret);
792                         break;
793                 }
794
795                 opcode = le16_to_cpu(event.desc.opcode);
796
797                 switch (opcode) {
798                 case ice_aqc_opc_get_link_status:
799                         if (ice_handle_link_event(pf))
800                                 dev_err(&pf->pdev->dev,
801                                         "Could not handle link event\n");
802                         break;
803                 case ice_aqc_opc_fw_logging:
804                         ice_output_fw_log(hw, &event.desc, event.msg_buf);
805                         break;
806                 default:
807                         dev_dbg(&pf->pdev->dev,
808                                 "%s Receive Queue unknown event 0x%04x ignored\n",
809                                 qtype, opcode);
810                         break;
811                 }
812         } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
813
814         devm_kfree(&pf->pdev->dev, event.msg_buf);
815
816         return pending && (i == ICE_DFLT_IRQ_WORK);
817 }
818
819 /**
820  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
821  * @hw: pointer to hardware info
822  * @cq: control queue information
823  *
824  * returns true if there are pending messages in a queue, false if there aren't
825  */
826 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
827 {
828         u16 ntu;
829
830         ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
831         return cq->rq.next_to_clean != ntu;
832 }
833
834 /**
835  * ice_clean_adminq_subtask - clean the AdminQ rings
836  * @pf: board private structure
837  */
838 static void ice_clean_adminq_subtask(struct ice_pf *pf)
839 {
840         struct ice_hw *hw = &pf->hw;
841
842         if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
843                 return;
844
845         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
846                 return;
847
848         clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
849
850         /* There might be a situation where new messages arrive to a control
851          * queue between processing the last message and clearing the
852          * EVENT_PENDING bit. So before exiting, check queue head again (using
853          * ice_ctrlq_pending) and process new messages if any.
854          */
855         if (ice_ctrlq_pending(hw, &hw->adminq))
856                 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
857
858         ice_flush(hw);
859 }
860
861 /**
862  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
863  * @pf: board private structure
864  */
865 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
866 {
867         struct ice_hw *hw = &pf->hw;
868
869         if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
870                 return;
871
872         if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
873                 return;
874
875         clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
876
877         if (ice_ctrlq_pending(hw, &hw->mailboxq))
878                 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
879
880         ice_flush(hw);
881 }
882
883 /**
884  * ice_service_task_schedule - schedule the service task to wake up
885  * @pf: board private structure
886  *
887  * If not already scheduled, this puts the task into the work queue.
888  */
889 static void ice_service_task_schedule(struct ice_pf *pf)
890 {
891         if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
892             !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
893             !test_bit(__ICE_NEEDS_RESTART, pf->state))
894                 queue_work(ice_wq, &pf->serv_task);
895 }
896
897 /**
898  * ice_service_task_complete - finish up the service task
899  * @pf: board private structure
900  */
901 static void ice_service_task_complete(struct ice_pf *pf)
902 {
903         WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
904
905         /* force memory (pf->state) to sync before next service task */
906         smp_mb__before_atomic();
907         clear_bit(__ICE_SERVICE_SCHED, pf->state);
908 }
909
910 /**
911  * ice_service_task_stop - stop service task and cancel works
912  * @pf: board private structure
913  */
914 static void ice_service_task_stop(struct ice_pf *pf)
915 {
916         set_bit(__ICE_SERVICE_DIS, pf->state);
917
918         if (pf->serv_tmr.function)
919                 del_timer_sync(&pf->serv_tmr);
920         if (pf->serv_task.func)
921                 cancel_work_sync(&pf->serv_task);
922
923         clear_bit(__ICE_SERVICE_SCHED, pf->state);
924 }
925
926 /**
927  * ice_service_timer - timer callback to schedule service task
928  * @t: pointer to timer_list
929  */
930 static void ice_service_timer(struct timer_list *t)
931 {
932         struct ice_pf *pf = from_timer(pf, t, serv_tmr);
933
934         mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
935         ice_service_task_schedule(pf);
936 }
937
938 /**
939  * ice_handle_mdd_event - handle malicious driver detect event
940  * @pf: pointer to the PF structure
941  *
942  * Called from service task. OICR interrupt handler indicates MDD event
943  */
944 static void ice_handle_mdd_event(struct ice_pf *pf)
945 {
946         struct ice_hw *hw = &pf->hw;
947         bool mdd_detected = false;
948         u32 reg;
949
950         if (!test_bit(__ICE_MDD_EVENT_PENDING, pf->state))
951                 return;
952
953         /* find what triggered the MDD event */
954         reg = rd32(hw, GL_MDET_TX_PQM);
955         if (reg & GL_MDET_TX_PQM_VALID_M) {
956                 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
957                                 GL_MDET_TX_PQM_PF_NUM_S;
958                 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
959                                 GL_MDET_TX_PQM_VF_NUM_S;
960                 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
961                                 GL_MDET_TX_PQM_MAL_TYPE_S;
962                 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
963                                 GL_MDET_TX_PQM_QNUM_S);
964
965                 if (netif_msg_tx_err(pf))
966                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
967                                  event, queue, pf_num, vf_num);
968                 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
969                 mdd_detected = true;
970         }
971
972         reg = rd32(hw, GL_MDET_TX_TCLAN);
973         if (reg & GL_MDET_TX_TCLAN_VALID_M) {
974                 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
975                                 GL_MDET_TX_TCLAN_PF_NUM_S;
976                 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
977                                 GL_MDET_TX_TCLAN_VF_NUM_S;
978                 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
979                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
980                 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
981                                 GL_MDET_TX_TCLAN_QNUM_S);
982
983                 if (netif_msg_rx_err(pf))
984                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
985                                  event, queue, pf_num, vf_num);
986                 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
987                 mdd_detected = true;
988         }
989
990         reg = rd32(hw, GL_MDET_RX);
991         if (reg & GL_MDET_RX_VALID_M) {
992                 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
993                                 GL_MDET_RX_PF_NUM_S;
994                 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
995                                 GL_MDET_RX_VF_NUM_S;
996                 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
997                                 GL_MDET_RX_MAL_TYPE_S;
998                 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
999                                 GL_MDET_RX_QNUM_S);
1000
1001                 if (netif_msg_rx_err(pf))
1002                         dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1003                                  event, queue, pf_num, vf_num);
1004                 wr32(hw, GL_MDET_RX, 0xffffffff);
1005                 mdd_detected = true;
1006         }
1007
1008         if (mdd_detected) {
1009                 bool pf_mdd_detected = false;
1010
1011                 reg = rd32(hw, PF_MDET_TX_PQM);
1012                 if (reg & PF_MDET_TX_PQM_VALID_M) {
1013                         wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1014                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1015                         pf_mdd_detected = true;
1016                 }
1017
1018                 reg = rd32(hw, PF_MDET_TX_TCLAN);
1019                 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1020                         wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1021                         dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1022                         pf_mdd_detected = true;
1023                 }
1024
1025                 reg = rd32(hw, PF_MDET_RX);
1026                 if (reg & PF_MDET_RX_VALID_M) {
1027                         wr32(hw, PF_MDET_RX, 0xFFFF);
1028                         dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1029                         pf_mdd_detected = true;
1030                 }
1031                 /* Queue belongs to the PF initiate a reset */
1032                 if (pf_mdd_detected) {
1033                         set_bit(__ICE_NEEDS_RESTART, pf->state);
1034                         ice_service_task_schedule(pf);
1035                 }
1036         }
1037
1038         /* re-enable MDD interrupt cause */
1039         clear_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1040         reg = rd32(hw, PFINT_OICR_ENA);
1041         reg |= PFINT_OICR_MAL_DETECT_M;
1042         wr32(hw, PFINT_OICR_ENA, reg);
1043         ice_flush(hw);
1044 }
1045
1046 /**
1047  * ice_service_task - manage and run subtasks
1048  * @work: pointer to work_struct contained by the PF struct
1049  */
1050 static void ice_service_task(struct work_struct *work)
1051 {
1052         struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1053         unsigned long start_time = jiffies;
1054
1055         /* subtasks */
1056
1057         /* process reset requests first */
1058         ice_reset_subtask(pf);
1059
1060         /* bail if a reset/recovery cycle is pending or rebuild failed */
1061         if (ice_is_reset_in_progress(pf->state) ||
1062             test_bit(__ICE_SUSPENDED, pf->state) ||
1063             test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1064                 ice_service_task_complete(pf);
1065                 return;
1066         }
1067
1068         ice_check_for_hang_subtask(pf);
1069         ice_sync_fltr_subtask(pf);
1070         ice_handle_mdd_event(pf);
1071         ice_process_vflr_event(pf);
1072         ice_watchdog_subtask(pf);
1073         ice_clean_adminq_subtask(pf);
1074         ice_clean_mailboxq_subtask(pf);
1075
1076         /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1077         ice_service_task_complete(pf);
1078
1079         /* If the tasks have taken longer than one service timer period
1080          * or there is more work to be done, reset the service timer to
1081          * schedule the service task now.
1082          */
1083         if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1084             test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1085             test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1086             test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1087             test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1088                 mod_timer(&pf->serv_tmr, jiffies);
1089 }
1090
1091 /**
1092  * ice_set_ctrlq_len - helper function to set controlq length
1093  * @hw: pointer to the hw instance
1094  */
1095 static void ice_set_ctrlq_len(struct ice_hw *hw)
1096 {
1097         hw->adminq.num_rq_entries = ICE_AQ_LEN;
1098         hw->adminq.num_sq_entries = ICE_AQ_LEN;
1099         hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1100         hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1101         hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
1102         hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
1103         hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1104         hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1105 }
1106
1107 /**
1108  * ice_irq_affinity_notify - Callback for affinity changes
1109  * @notify: context as to what irq was changed
1110  * @mask: the new affinity mask
1111  *
1112  * This is a callback function used by the irq_set_affinity_notifier function
1113  * so that we may register to receive changes to the irq affinity masks.
1114  */
1115 static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1116                                     const cpumask_t *mask)
1117 {
1118         struct ice_q_vector *q_vector =
1119                 container_of(notify, struct ice_q_vector, affinity_notify);
1120
1121         cpumask_copy(&q_vector->affinity_mask, mask);
1122 }
1123
1124 /**
1125  * ice_irq_affinity_release - Callback for affinity notifier release
1126  * @ref: internal core kernel usage
1127  *
1128  * This is a callback function used by the irq_set_affinity_notifier function
1129  * to inform the current notification subscriber that they will no longer
1130  * receive notifications.
1131  */
1132 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1133
1134 /**
1135  * ice_vsi_ena_irq - Enable IRQ for the given VSI
1136  * @vsi: the VSI being configured
1137  */
1138 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1139 {
1140         struct ice_pf *pf = vsi->back;
1141         struct ice_hw *hw = &pf->hw;
1142
1143         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1144                 int i;
1145
1146                 for (i = 0; i < vsi->num_q_vectors; i++)
1147                         ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1148         }
1149
1150         ice_flush(hw);
1151         return 0;
1152 }
1153
1154 /**
1155  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1156  * @vsi: the VSI being configured
1157  * @basename: name for the vector
1158  */
1159 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1160 {
1161         int q_vectors = vsi->num_q_vectors;
1162         struct ice_pf *pf = vsi->back;
1163         int base = vsi->sw_base_vector;
1164         int rx_int_idx = 0;
1165         int tx_int_idx = 0;
1166         int vector, err;
1167         int irq_num;
1168
1169         for (vector = 0; vector < q_vectors; vector++) {
1170                 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1171
1172                 irq_num = pf->msix_entries[base + vector].vector;
1173
1174                 if (q_vector->tx.ring && q_vector->rx.ring) {
1175                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1176                                  "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1177                         tx_int_idx++;
1178                 } else if (q_vector->rx.ring) {
1179                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1180                                  "%s-%s-%d", basename, "rx", rx_int_idx++);
1181                 } else if (q_vector->tx.ring) {
1182                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1183                                  "%s-%s-%d", basename, "tx", tx_int_idx++);
1184                 } else {
1185                         /* skip this unused q_vector */
1186                         continue;
1187                 }
1188                 err = devm_request_irq(&pf->pdev->dev,
1189                                        pf->msix_entries[base + vector].vector,
1190                                        vsi->irq_handler, 0, q_vector->name,
1191                                        q_vector);
1192                 if (err) {
1193                         netdev_err(vsi->netdev,
1194                                    "MSIX request_irq failed, error: %d\n", err);
1195                         goto free_q_irqs;
1196                 }
1197
1198                 /* register for affinity change notifications */
1199                 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1200                 q_vector->affinity_notify.release = ice_irq_affinity_release;
1201                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1202
1203                 /* assign the mask for this irq */
1204                 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1205         }
1206
1207         vsi->irqs_ready = true;
1208         return 0;
1209
1210 free_q_irqs:
1211         while (vector) {
1212                 vector--;
1213                 irq_num = pf->msix_entries[base + vector].vector,
1214                 irq_set_affinity_notifier(irq_num, NULL);
1215                 irq_set_affinity_hint(irq_num, NULL);
1216                 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1217         }
1218         return err;
1219 }
1220
1221 /**
1222  * ice_ena_misc_vector - enable the non-queue interrupts
1223  * @pf: board private structure
1224  */
1225 static void ice_ena_misc_vector(struct ice_pf *pf)
1226 {
1227         struct ice_hw *hw = &pf->hw;
1228         u32 val;
1229
1230         /* clear things first */
1231         wr32(hw, PFINT_OICR_ENA, 0);    /* disable all */
1232         rd32(hw, PFINT_OICR);           /* read to clear */
1233
1234         val = (PFINT_OICR_ECC_ERR_M |
1235                PFINT_OICR_MAL_DETECT_M |
1236                PFINT_OICR_GRST_M |
1237                PFINT_OICR_PCI_EXCEPTION_M |
1238                PFINT_OICR_VFLR_M |
1239                PFINT_OICR_HMC_ERR_M |
1240                PFINT_OICR_PE_CRITERR_M);
1241
1242         wr32(hw, PFINT_OICR_ENA, val);
1243
1244         /* SW_ITR_IDX = 0, but don't change INTENA */
1245         wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
1246              GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1247 }
1248
1249 /**
1250  * ice_misc_intr - misc interrupt handler
1251  * @irq: interrupt number
1252  * @data: pointer to a q_vector
1253  */
1254 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1255 {
1256         struct ice_pf *pf = (struct ice_pf *)data;
1257         struct ice_hw *hw = &pf->hw;
1258         irqreturn_t ret = IRQ_NONE;
1259         u32 oicr, ena_mask;
1260
1261         set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1262         set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1263
1264         oicr = rd32(hw, PFINT_OICR);
1265         ena_mask = rd32(hw, PFINT_OICR_ENA);
1266
1267         if (oicr & PFINT_OICR_MAL_DETECT_M) {
1268                 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
1269                 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1270         }
1271         if (oicr & PFINT_OICR_VFLR_M) {
1272                 ena_mask &= ~PFINT_OICR_VFLR_M;
1273                 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
1274         }
1275
1276         if (oicr & PFINT_OICR_GRST_M) {
1277                 u32 reset;
1278
1279                 /* we have a reset warning */
1280                 ena_mask &= ~PFINT_OICR_GRST_M;
1281                 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1282                         GLGEN_RSTAT_RESET_TYPE_S;
1283
1284                 if (reset == ICE_RESET_CORER)
1285                         pf->corer_count++;
1286                 else if (reset == ICE_RESET_GLOBR)
1287                         pf->globr_count++;
1288                 else if (reset == ICE_RESET_EMPR)
1289                         pf->empr_count++;
1290                 else
1291                         dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
1292                                 reset);
1293
1294                 /* If a reset cycle isn't already in progress, we set a bit in
1295                  * pf->state so that the service task can start a reset/rebuild.
1296                  * We also make note of which reset happened so that peer
1297                  * devices/drivers can be informed.
1298                  */
1299                 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
1300                         if (reset == ICE_RESET_CORER)
1301                                 set_bit(__ICE_CORER_RECV, pf->state);
1302                         else if (reset == ICE_RESET_GLOBR)
1303                                 set_bit(__ICE_GLOBR_RECV, pf->state);
1304                         else
1305                                 set_bit(__ICE_EMPR_RECV, pf->state);
1306
1307                         /* There are couple of different bits at play here.
1308                          * hw->reset_ongoing indicates whether the hardware is
1309                          * in reset. This is set to true when a reset interrupt
1310                          * is received and set back to false after the driver
1311                          * has determined that the hardware is out of reset.
1312                          *
1313                          * __ICE_RESET_OICR_RECV in pf->state indicates
1314                          * that a post reset rebuild is required before the
1315                          * driver is operational again. This is set above.
1316                          *
1317                          * As this is the start of the reset/rebuild cycle, set
1318                          * both to indicate that.
1319                          */
1320                         hw->reset_ongoing = true;
1321                 }
1322         }
1323
1324         if (oicr & PFINT_OICR_HMC_ERR_M) {
1325                 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1326                 dev_dbg(&pf->pdev->dev,
1327                         "HMC Error interrupt - info 0x%x, data 0x%x\n",
1328                         rd32(hw, PFHMC_ERRORINFO),
1329                         rd32(hw, PFHMC_ERRORDATA));
1330         }
1331
1332         /* Report and mask off any remaining unexpected interrupts */
1333         oicr &= ena_mask;
1334         if (oicr) {
1335                 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1336                         oicr);
1337                 /* If a critical error is pending there is no choice but to
1338                  * reset the device.
1339                  */
1340                 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1341                             PFINT_OICR_PCI_EXCEPTION_M |
1342                             PFINT_OICR_ECC_ERR_M)) {
1343                         set_bit(__ICE_PFR_REQ, pf->state);
1344                         ice_service_task_schedule(pf);
1345                 }
1346                 ena_mask &= ~oicr;
1347         }
1348         ret = IRQ_HANDLED;
1349
1350         /* re-enable interrupt causes that are not handled during this pass */
1351         wr32(hw, PFINT_OICR_ENA, ena_mask);
1352         if (!test_bit(__ICE_DOWN, pf->state)) {
1353                 ice_service_task_schedule(pf);
1354                 ice_irq_dynamic_ena(hw, NULL, NULL);
1355         }
1356
1357         return ret;
1358 }
1359
1360 /**
1361  * ice_free_irq_msix_misc - Unroll misc vector setup
1362  * @pf: board private structure
1363  */
1364 static void ice_free_irq_msix_misc(struct ice_pf *pf)
1365 {
1366         /* disable OICR interrupt */
1367         wr32(&pf->hw, PFINT_OICR_ENA, 0);
1368         ice_flush(&pf->hw);
1369
1370         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
1371                 synchronize_irq(pf->msix_entries[pf->sw_oicr_idx].vector);
1372                 devm_free_irq(&pf->pdev->dev,
1373                               pf->msix_entries[pf->sw_oicr_idx].vector, pf);
1374         }
1375
1376         pf->num_avail_sw_msix += 1;
1377         ice_free_res(pf->sw_irq_tracker, pf->sw_oicr_idx, ICE_RES_MISC_VEC_ID);
1378         pf->num_avail_hw_msix += 1;
1379         ice_free_res(pf->hw_irq_tracker, pf->hw_oicr_idx, ICE_RES_MISC_VEC_ID);
1380 }
1381
1382 /**
1383  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
1384  * @pf: board private structure
1385  *
1386  * This sets up the handler for MSIX 0, which is used to manage the
1387  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
1388  * when in MSI or Legacy interrupt mode.
1389  */
1390 static int ice_req_irq_msix_misc(struct ice_pf *pf)
1391 {
1392         struct ice_hw *hw = &pf->hw;
1393         int oicr_idx, err = 0;
1394         u8 itr_gran;
1395         u32 val;
1396
1397         if (!pf->int_name[0])
1398                 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
1399                          dev_driver_string(&pf->pdev->dev),
1400                          dev_name(&pf->pdev->dev));
1401
1402         /* Do not request IRQ but do enable OICR interrupt since settings are
1403          * lost during reset. Note that this function is called only during
1404          * rebuild path and not while reset is in progress.
1405          */
1406         if (ice_is_reset_in_progress(pf->state))
1407                 goto skip_req_irq;
1408
1409         /* reserve one vector in sw_irq_tracker for misc interrupts */
1410         oicr_idx = ice_get_res(pf, pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1411         if (oicr_idx < 0)
1412                 return oicr_idx;
1413
1414         pf->num_avail_sw_msix -= 1;
1415         pf->sw_oicr_idx = oicr_idx;
1416
1417         /* reserve one vector in hw_irq_tracker for misc interrupts */
1418         oicr_idx = ice_get_res(pf, pf->hw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1419         if (oicr_idx < 0) {
1420                 ice_free_res(pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1421                 pf->num_avail_sw_msix += 1;
1422                 return oicr_idx;
1423         }
1424         pf->num_avail_hw_msix -= 1;
1425         pf->hw_oicr_idx = oicr_idx;
1426
1427         err = devm_request_irq(&pf->pdev->dev,
1428                                pf->msix_entries[pf->sw_oicr_idx].vector,
1429                                ice_misc_intr, 0, pf->int_name, pf);
1430         if (err) {
1431                 dev_err(&pf->pdev->dev,
1432                         "devm_request_irq for %s failed: %d\n",
1433                         pf->int_name, err);
1434                 ice_free_res(pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1435                 pf->num_avail_sw_msix += 1;
1436                 ice_free_res(pf->hw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1437                 pf->num_avail_hw_msix += 1;
1438                 return err;
1439         }
1440
1441 skip_req_irq:
1442         ice_ena_misc_vector(pf);
1443
1444         val = ((pf->hw_oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
1445                PFINT_OICR_CTL_CAUSE_ENA_M);
1446         wr32(hw, PFINT_OICR_CTL, val);
1447
1448         /* This enables Admin queue Interrupt causes */
1449         val = ((pf->hw_oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
1450                PFINT_FW_CTL_CAUSE_ENA_M);
1451         wr32(hw, PFINT_FW_CTL, val);
1452
1453         /* This enables Mailbox queue Interrupt causes */
1454         val = ((pf->hw_oicr_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
1455                PFINT_MBX_CTL_CAUSE_ENA_M);
1456         wr32(hw, PFINT_MBX_CTL, val);
1457
1458         itr_gran = hw->itr_gran;
1459
1460         wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->hw_oicr_idx),
1461              ITR_TO_REG(ICE_ITR_8K, itr_gran));
1462
1463         ice_flush(hw);
1464         ice_irq_dynamic_ena(hw, NULL, NULL);
1465
1466         return 0;
1467 }
1468
1469 /**
1470  * ice_napi_del - Remove NAPI handler for the VSI
1471  * @vsi: VSI for which NAPI handler is to be removed
1472  */
1473 static void ice_napi_del(struct ice_vsi *vsi)
1474 {
1475         int v_idx;
1476
1477         if (!vsi->netdev)
1478                 return;
1479
1480         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
1481                 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
1482 }
1483
1484 /**
1485  * ice_napi_add - register NAPI handler for the VSI
1486  * @vsi: VSI for which NAPI handler is to be registered
1487  *
1488  * This function is only called in the driver's load path. Registering the NAPI
1489  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
1490  * reset/rebuild, etc.)
1491  */
1492 static void ice_napi_add(struct ice_vsi *vsi)
1493 {
1494         int v_idx;
1495
1496         if (!vsi->netdev)
1497                 return;
1498
1499         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
1500                 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
1501                                ice_napi_poll, NAPI_POLL_WEIGHT);
1502 }
1503
1504 /**
1505  * ice_cfg_netdev - Allocate, configure and register a netdev
1506  * @vsi: the VSI associated with the new netdev
1507  *
1508  * Returns 0 on success, negative value on failure
1509  */
1510 static int ice_cfg_netdev(struct ice_vsi *vsi)
1511 {
1512         netdev_features_t csumo_features;
1513         netdev_features_t vlano_features;
1514         netdev_features_t dflt_features;
1515         netdev_features_t tso_features;
1516         struct ice_netdev_priv *np;
1517         struct net_device *netdev;
1518         u8 mac_addr[ETH_ALEN];
1519         int err;
1520
1521         netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
1522                                     vsi->alloc_txq, vsi->alloc_rxq);
1523         if (!netdev)
1524                 return -ENOMEM;
1525
1526         vsi->netdev = netdev;
1527         np = netdev_priv(netdev);
1528         np->vsi = vsi;
1529
1530         dflt_features = NETIF_F_SG      |
1531                         NETIF_F_HIGHDMA |
1532                         NETIF_F_RXHASH;
1533
1534         csumo_features = NETIF_F_RXCSUM   |
1535                          NETIF_F_IP_CSUM  |
1536                          NETIF_F_IPV6_CSUM;
1537
1538         vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
1539                          NETIF_F_HW_VLAN_CTAG_TX     |
1540                          NETIF_F_HW_VLAN_CTAG_RX;
1541
1542         tso_features = NETIF_F_TSO;
1543
1544         /* set features that user can change */
1545         netdev->hw_features = dflt_features | csumo_features |
1546                               vlano_features | tso_features;
1547
1548         /* enable features */
1549         netdev->features |= netdev->hw_features;
1550         /* encap and VLAN devices inherit default, csumo and tso features */
1551         netdev->hw_enc_features |= dflt_features | csumo_features |
1552                                    tso_features;
1553         netdev->vlan_features |= dflt_features | csumo_features |
1554                                  tso_features;
1555
1556         if (vsi->type == ICE_VSI_PF) {
1557                 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
1558                 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
1559
1560                 ether_addr_copy(netdev->dev_addr, mac_addr);
1561                 ether_addr_copy(netdev->perm_addr, mac_addr);
1562         }
1563
1564         netdev->priv_flags |= IFF_UNICAST_FLT;
1565
1566         /* assign netdev_ops */
1567         netdev->netdev_ops = &ice_netdev_ops;
1568
1569         /* setup watchdog timeout value to be 5 second */
1570         netdev->watchdog_timeo = 5 * HZ;
1571
1572         ice_set_ethtool_ops(netdev);
1573
1574         netdev->min_mtu = ETH_MIN_MTU;
1575         netdev->max_mtu = ICE_MAX_MTU;
1576
1577         err = register_netdev(vsi->netdev);
1578         if (err)
1579                 return err;
1580
1581         netif_carrier_off(vsi->netdev);
1582
1583         /* make sure transmit queues start off as stopped */
1584         netif_tx_stop_all_queues(vsi->netdev);
1585
1586         return 0;
1587 }
1588
1589 /**
1590  * ice_fill_rss_lut - Fill the RSS lookup table with default values
1591  * @lut: Lookup table
1592  * @rss_table_size: Lookup table size
1593  * @rss_size: Range of queue number for hashing
1594  */
1595 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
1596 {
1597         u16 i;
1598
1599         for (i = 0; i < rss_table_size; i++)
1600                 lut[i] = i % rss_size;
1601 }
1602
1603 /**
1604  * ice_pf_vsi_setup - Set up a PF VSI
1605  * @pf: board private structure
1606  * @pi: pointer to the port_info instance
1607  *
1608  * Returns pointer to the successfully allocated VSI sw struct on success,
1609  * otherwise returns NULL on failure.
1610  */
1611 static struct ice_vsi *
1612 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
1613 {
1614         return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
1615 }
1616
1617 /**
1618  * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
1619  * @netdev: network interface to be adjusted
1620  * @proto: unused protocol
1621  * @vid: vlan id to be added
1622  *
1623  * net_device_ops implementation for adding vlan ids
1624  */
1625 static int ice_vlan_rx_add_vid(struct net_device *netdev,
1626                                __always_unused __be16 proto, u16 vid)
1627 {
1628         struct ice_netdev_priv *np = netdev_priv(netdev);
1629         struct ice_vsi *vsi = np->vsi;
1630         int ret;
1631
1632         if (vid >= VLAN_N_VID) {
1633                 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
1634                            vid, VLAN_N_VID);
1635                 return -EINVAL;
1636         }
1637
1638         if (vsi->info.pvid)
1639                 return -EINVAL;
1640
1641         /* Enable VLAN pruning when VLAN 0 is added */
1642         if (unlikely(!vid)) {
1643                 ret = ice_cfg_vlan_pruning(vsi, true);
1644                 if (ret)
1645                         return ret;
1646         }
1647
1648         /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
1649          * needed to continue allowing all untagged packets since VLAN prune
1650          * list is applied to all packets by the switch
1651          */
1652         ret = ice_vsi_add_vlan(vsi, vid);
1653
1654         if (!ret)
1655                 set_bit(vid, vsi->active_vlans);
1656
1657         return ret;
1658 }
1659
1660 /**
1661  * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1662  * @netdev: network interface to be adjusted
1663  * @proto: unused protocol
1664  * @vid: vlan id to be removed
1665  *
1666  * net_device_ops implementation for removing vlan ids
1667  */
1668 static int ice_vlan_rx_kill_vid(struct net_device *netdev,
1669                                 __always_unused __be16 proto, u16 vid)
1670 {
1671         struct ice_netdev_priv *np = netdev_priv(netdev);
1672         struct ice_vsi *vsi = np->vsi;
1673         int status;
1674
1675         if (vsi->info.pvid)
1676                 return -EINVAL;
1677
1678         /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
1679          * information
1680          */
1681         status = ice_vsi_kill_vlan(vsi, vid);
1682         if (status)
1683                 return status;
1684
1685         clear_bit(vid, vsi->active_vlans);
1686
1687         /* Disable VLAN pruning when VLAN 0 is removed */
1688         if (unlikely(!vid))
1689                 status = ice_cfg_vlan_pruning(vsi, false);
1690
1691         return status;
1692 }
1693
1694 /**
1695  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
1696  * @pf: board private structure
1697  *
1698  * Returns 0 on success, negative value on failure
1699  */
1700 static int ice_setup_pf_sw(struct ice_pf *pf)
1701 {
1702         LIST_HEAD(tmp_add_list);
1703         u8 broadcast[ETH_ALEN];
1704         struct ice_vsi *vsi;
1705         int status = 0;
1706
1707         if (ice_is_reset_in_progress(pf->state))
1708                 return -EBUSY;
1709
1710         vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
1711         if (!vsi) {
1712                 status = -ENOMEM;
1713                 goto unroll_vsi_setup;
1714         }
1715
1716         status = ice_cfg_netdev(vsi);
1717         if (status) {
1718                 status = -ENODEV;
1719                 goto unroll_vsi_setup;
1720         }
1721
1722         /* registering the NAPI handler requires both the queues and
1723          * netdev to be created, which are done in ice_pf_vsi_setup()
1724          * and ice_cfg_netdev() respectively
1725          */
1726         ice_napi_add(vsi);
1727
1728         /* To add a MAC filter, first add the MAC to a list and then
1729          * pass the list to ice_add_mac.
1730          */
1731
1732          /* Add a unicast MAC filter so the VSI can get its packets */
1733         status = ice_add_mac_to_list(vsi, &tmp_add_list,
1734                                      vsi->port_info->mac.perm_addr);
1735         if (status)
1736                 goto unroll_napi_add;
1737
1738         /* VSI needs to receive broadcast traffic, so add the broadcast
1739          * MAC address to the list as well.
1740          */
1741         eth_broadcast_addr(broadcast);
1742         status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
1743         if (status)
1744                 goto free_mac_list;
1745
1746         /* program MAC filters for entries in tmp_add_list */
1747         status = ice_add_mac(&pf->hw, &tmp_add_list);
1748         if (status) {
1749                 dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
1750                 status = -ENOMEM;
1751                 goto free_mac_list;
1752         }
1753
1754         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1755         return status;
1756
1757 free_mac_list:
1758         ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1759
1760 unroll_napi_add:
1761         if (vsi) {
1762                 ice_napi_del(vsi);
1763                 if (vsi->netdev) {
1764                         if (vsi->netdev->reg_state == NETREG_REGISTERED)
1765                                 unregister_netdev(vsi->netdev);
1766                         free_netdev(vsi->netdev);
1767                         vsi->netdev = NULL;
1768                 }
1769         }
1770
1771 unroll_vsi_setup:
1772         if (vsi) {
1773                 ice_vsi_free_q_vectors(vsi);
1774                 ice_vsi_delete(vsi);
1775                 ice_vsi_put_qs(vsi);
1776                 pf->q_left_tx += vsi->alloc_txq;
1777                 pf->q_left_rx += vsi->alloc_rxq;
1778                 ice_vsi_clear(vsi);
1779         }
1780         return status;
1781 }
1782
1783 /**
1784  * ice_determine_q_usage - Calculate queue distribution
1785  * @pf: board private structure
1786  *
1787  * Return -ENOMEM if we don't get enough queues for all ports
1788  */
1789 static void ice_determine_q_usage(struct ice_pf *pf)
1790 {
1791         u16 q_left_tx, q_left_rx;
1792
1793         q_left_tx = pf->hw.func_caps.common_cap.num_txq;
1794         q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
1795
1796         pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
1797
1798         /* only 1 rx queue unless RSS is enabled */
1799         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1800                 pf->num_lan_rx = 1;
1801         else
1802                 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
1803
1804         pf->q_left_tx = q_left_tx - pf->num_lan_tx;
1805         pf->q_left_rx = q_left_rx - pf->num_lan_rx;
1806 }
1807
1808 /**
1809  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
1810  * @pf: board private structure to initialize
1811  */
1812 static void ice_deinit_pf(struct ice_pf *pf)
1813 {
1814         ice_service_task_stop(pf);
1815         mutex_destroy(&pf->sw_mutex);
1816         mutex_destroy(&pf->avail_q_mutex);
1817 }
1818
1819 /**
1820  * ice_init_pf - Initialize general software structures (struct ice_pf)
1821  * @pf: board private structure to initialize
1822  */
1823 static void ice_init_pf(struct ice_pf *pf)
1824 {
1825         bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
1826         set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
1827 #ifdef CONFIG_PCI_IOV
1828         if (pf->hw.func_caps.common_cap.sr_iov_1_1) {
1829                 struct ice_hw *hw = &pf->hw;
1830
1831                 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
1832                 pf->num_vfs_supported = min_t(int, hw->func_caps.num_allocd_vfs,
1833                                               ICE_MAX_VF_COUNT);
1834         }
1835 #endif /* CONFIG_PCI_IOV */
1836
1837         mutex_init(&pf->sw_mutex);
1838         mutex_init(&pf->avail_q_mutex);
1839
1840         /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
1841         mutex_lock(&pf->avail_q_mutex);
1842         bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
1843         bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
1844         mutex_unlock(&pf->avail_q_mutex);
1845
1846         if (pf->hw.func_caps.common_cap.rss_table_size)
1847                 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
1848
1849         /* setup service timer and periodic service task */
1850         timer_setup(&pf->serv_tmr, ice_service_timer, 0);
1851         pf->serv_tmr_period = HZ;
1852         INIT_WORK(&pf->serv_task, ice_service_task);
1853         clear_bit(__ICE_SERVICE_SCHED, pf->state);
1854 }
1855
1856 /**
1857  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
1858  * @pf: board private structure
1859  *
1860  * compute the number of MSIX vectors required (v_budget) and request from
1861  * the OS. Return the number of vectors reserved or negative on failure
1862  */
1863 static int ice_ena_msix_range(struct ice_pf *pf)
1864 {
1865         int v_left, v_actual, v_budget = 0;
1866         int needed, err, i;
1867
1868         v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
1869
1870         /* reserve one vector for miscellaneous handler */
1871         needed = 1;
1872         v_budget += needed;
1873         v_left -= needed;
1874
1875         /* reserve vectors for LAN traffic */
1876         pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
1877         v_budget += pf->num_lan_msix;
1878         v_left -= pf->num_lan_msix;
1879
1880         pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
1881                                         sizeof(struct msix_entry), GFP_KERNEL);
1882
1883         if (!pf->msix_entries) {
1884                 err = -ENOMEM;
1885                 goto exit_err;
1886         }
1887
1888         for (i = 0; i < v_budget; i++)
1889                 pf->msix_entries[i].entry = i;
1890
1891         /* actually reserve the vectors */
1892         v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
1893                                          ICE_MIN_MSIX, v_budget);
1894
1895         if (v_actual < 0) {
1896                 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
1897                 err = v_actual;
1898                 goto msix_err;
1899         }
1900
1901         if (v_actual < v_budget) {
1902                 dev_warn(&pf->pdev->dev,
1903                          "not enough vectors. requested = %d, obtained = %d\n",
1904                          v_budget, v_actual);
1905                 if (v_actual >= (pf->num_lan_msix + 1)) {
1906                         pf->num_avail_sw_msix = v_actual -
1907                                                 (pf->num_lan_msix + 1);
1908                 } else if (v_actual >= 2) {
1909                         pf->num_lan_msix = 1;
1910                         pf->num_avail_sw_msix = v_actual - 2;
1911                 } else {
1912                         pci_disable_msix(pf->pdev);
1913                         err = -ERANGE;
1914                         goto msix_err;
1915                 }
1916         }
1917
1918         return v_actual;
1919
1920 msix_err:
1921         devm_kfree(&pf->pdev->dev, pf->msix_entries);
1922         goto exit_err;
1923
1924 exit_err:
1925         pf->num_lan_msix = 0;
1926         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
1927         return err;
1928 }
1929
1930 /**
1931  * ice_dis_msix - Disable MSI-X interrupt setup in OS
1932  * @pf: board private structure
1933  */
1934 static void ice_dis_msix(struct ice_pf *pf)
1935 {
1936         pci_disable_msix(pf->pdev);
1937         devm_kfree(&pf->pdev->dev, pf->msix_entries);
1938         pf->msix_entries = NULL;
1939         clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
1940 }
1941
1942 /**
1943  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
1944  * @pf: board private structure
1945  */
1946 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
1947 {
1948         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
1949                 ice_dis_msix(pf);
1950
1951         if (pf->sw_irq_tracker) {
1952                 devm_kfree(&pf->pdev->dev, pf->sw_irq_tracker);
1953                 pf->sw_irq_tracker = NULL;
1954         }
1955
1956         if (pf->hw_irq_tracker) {
1957                 devm_kfree(&pf->pdev->dev, pf->hw_irq_tracker);
1958                 pf->hw_irq_tracker = NULL;
1959         }
1960 }
1961
1962 /**
1963  * ice_init_interrupt_scheme - Determine proper interrupt scheme
1964  * @pf: board private structure to initialize
1965  */
1966 static int ice_init_interrupt_scheme(struct ice_pf *pf)
1967 {
1968         int vectors = 0, hw_vectors = 0;
1969         ssize_t size;
1970
1971         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
1972                 vectors = ice_ena_msix_range(pf);
1973         else
1974                 return -ENODEV;
1975
1976         if (vectors < 0)
1977                 return vectors;
1978
1979         /* set up vector assignment tracking */
1980         size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
1981
1982         pf->sw_irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
1983         if (!pf->sw_irq_tracker) {
1984                 ice_dis_msix(pf);
1985                 return -ENOMEM;
1986         }
1987
1988         /* populate SW interrupts pool with number of OS granted IRQs. */
1989         pf->num_avail_sw_msix = vectors;
1990         pf->sw_irq_tracker->num_entries = vectors;
1991
1992         /* set up HW vector assignment tracking */
1993         hw_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
1994         size = sizeof(struct ice_res_tracker) + (sizeof(u16) * hw_vectors);
1995
1996         pf->hw_irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
1997         if (!pf->hw_irq_tracker) {
1998                 ice_clear_interrupt_scheme(pf);
1999                 return -ENOMEM;
2000         }
2001
2002         /* populate HW interrupts pool with number of HW supported irqs. */
2003         pf->num_avail_hw_msix = hw_vectors;
2004         pf->hw_irq_tracker->num_entries = hw_vectors;
2005
2006         return 0;
2007 }
2008
2009 /**
2010  * ice_probe - Device initialization routine
2011  * @pdev: PCI device information struct
2012  * @ent: entry in ice_pci_tbl
2013  *
2014  * Returns 0 on success, negative on failure
2015  */
2016 static int ice_probe(struct pci_dev *pdev,
2017                      const struct pci_device_id __always_unused *ent)
2018 {
2019         struct ice_pf *pf;
2020         struct ice_hw *hw;
2021         int err;
2022
2023         /* this driver uses devres, see Documentation/driver-model/devres.txt */
2024         err = pcim_enable_device(pdev);
2025         if (err)
2026                 return err;
2027
2028         err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
2029         if (err) {
2030                 dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
2031                 return err;
2032         }
2033
2034         pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
2035         if (!pf)
2036                 return -ENOMEM;
2037
2038         /* set up for high or low dma */
2039         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2040         if (err)
2041                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2042         if (err) {
2043                 dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
2044                 return err;
2045         }
2046
2047         pci_enable_pcie_error_reporting(pdev);
2048         pci_set_master(pdev);
2049
2050         pf->pdev = pdev;
2051         pci_set_drvdata(pdev, pf);
2052         set_bit(__ICE_DOWN, pf->state);
2053         /* Disable service task until DOWN bit is cleared */
2054         set_bit(__ICE_SERVICE_DIS, pf->state);
2055
2056         hw = &pf->hw;
2057         hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
2058         hw->back = pf;
2059         hw->vendor_id = pdev->vendor;
2060         hw->device_id = pdev->device;
2061         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2062         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2063         hw->subsystem_device_id = pdev->subsystem_device;
2064         hw->bus.device = PCI_SLOT(pdev->devfn);
2065         hw->bus.func = PCI_FUNC(pdev->devfn);
2066         ice_set_ctrlq_len(hw);
2067
2068         pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
2069
2070 #ifndef CONFIG_DYNAMIC_DEBUG
2071         if (debug < -1)
2072                 hw->debug_mask = debug;
2073 #endif
2074
2075         err = ice_init_hw(hw);
2076         if (err) {
2077                 dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
2078                 err = -EIO;
2079                 goto err_exit_unroll;
2080         }
2081
2082         dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
2083                  hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
2084                  hw->api_maj_ver, hw->api_min_ver);
2085
2086         ice_init_pf(pf);
2087
2088         ice_determine_q_usage(pf);
2089
2090         pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
2091                                   hw->func_caps.guaranteed_num_vsi);
2092         if (!pf->num_alloc_vsi) {
2093                 err = -EIO;
2094                 goto err_init_pf_unroll;
2095         }
2096
2097         pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
2098                                sizeof(struct ice_vsi *), GFP_KERNEL);
2099         if (!pf->vsi) {
2100                 err = -ENOMEM;
2101                 goto err_init_pf_unroll;
2102         }
2103
2104         err = ice_init_interrupt_scheme(pf);
2105         if (err) {
2106                 dev_err(&pdev->dev,
2107                         "ice_init_interrupt_scheme failed: %d\n", err);
2108                 err = -EIO;
2109                 goto err_init_interrupt_unroll;
2110         }
2111
2112         /* Driver is mostly up */
2113         clear_bit(__ICE_DOWN, pf->state);
2114
2115         /* In case of MSIX we are going to setup the misc vector right here
2116          * to handle admin queue events etc. In case of legacy and MSI
2117          * the misc functionality and queue processing is combined in
2118          * the same vector and that gets setup at open.
2119          */
2120         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2121                 err = ice_req_irq_msix_misc(pf);
2122                 if (err) {
2123                         dev_err(&pdev->dev,
2124                                 "setup of misc vector failed: %d\n", err);
2125                         goto err_init_interrupt_unroll;
2126                 }
2127         }
2128
2129         /* create switch struct for the switch element created by FW on boot */
2130         pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
2131                                     GFP_KERNEL);
2132         if (!pf->first_sw) {
2133                 err = -ENOMEM;
2134                 goto err_msix_misc_unroll;
2135         }
2136
2137         if (hw->evb_veb)
2138                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
2139         else
2140                 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
2141
2142         pf->first_sw->pf = pf;
2143
2144         /* record the sw_id available for later use */
2145         pf->first_sw->sw_id = hw->port_info->sw_id;
2146
2147         err = ice_setup_pf_sw(pf);
2148         if (err) {
2149                 dev_err(&pdev->dev,
2150                         "probe failed due to setup pf switch:%d\n", err);
2151                 goto err_alloc_sw_unroll;
2152         }
2153
2154         clear_bit(__ICE_SERVICE_DIS, pf->state);
2155
2156         /* since everything is good, start the service timer */
2157         mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2158
2159         err = ice_init_link_events(pf->hw.port_info);
2160         if (err) {
2161                 dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
2162                 goto err_alloc_sw_unroll;
2163         }
2164
2165         return 0;
2166
2167 err_alloc_sw_unroll:
2168         set_bit(__ICE_SERVICE_DIS, pf->state);
2169         set_bit(__ICE_DOWN, pf->state);
2170         devm_kfree(&pf->pdev->dev, pf->first_sw);
2171 err_msix_misc_unroll:
2172         ice_free_irq_msix_misc(pf);
2173 err_init_interrupt_unroll:
2174         ice_clear_interrupt_scheme(pf);
2175         devm_kfree(&pdev->dev, pf->vsi);
2176 err_init_pf_unroll:
2177         ice_deinit_pf(pf);
2178         ice_deinit_hw(hw);
2179 err_exit_unroll:
2180         pci_disable_pcie_error_reporting(pdev);
2181         return err;
2182 }
2183
2184 /**
2185  * ice_remove - Device removal routine
2186  * @pdev: PCI device information struct
2187  */
2188 static void ice_remove(struct pci_dev *pdev)
2189 {
2190         struct ice_pf *pf = pci_get_drvdata(pdev);
2191         int i;
2192
2193         if (!pf)
2194                 return;
2195
2196         set_bit(__ICE_DOWN, pf->state);
2197         ice_service_task_stop(pf);
2198
2199         if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
2200                 ice_free_vfs(pf);
2201         ice_vsi_release_all(pf);
2202         ice_free_irq_msix_misc(pf);
2203         ice_for_each_vsi(pf, i) {
2204                 if (!pf->vsi[i])
2205                         continue;
2206                 ice_vsi_free_q_vectors(pf->vsi[i]);
2207         }
2208         ice_clear_interrupt_scheme(pf);
2209         ice_deinit_pf(pf);
2210         ice_deinit_hw(&pf->hw);
2211         pci_disable_pcie_error_reporting(pdev);
2212 }
2213
2214 /* ice_pci_tbl - PCI Device ID Table
2215  *
2216  * Wildcard entries (PCI_ANY_ID) should come last
2217  * Last entry must be all 0s
2218  *
2219  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
2220  *   Class, Class Mask, private data (not used) }
2221  */
2222 static const struct pci_device_id ice_pci_tbl[] = {
2223         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
2224         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
2225         { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
2226         /* required last entry */
2227         { 0, }
2228 };
2229 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
2230
2231 static struct pci_driver ice_driver = {
2232         .name = KBUILD_MODNAME,
2233         .id_table = ice_pci_tbl,
2234         .probe = ice_probe,
2235         .remove = ice_remove,
2236         .sriov_configure = ice_sriov_configure,
2237 };
2238
2239 /**
2240  * ice_module_init - Driver registration routine
2241  *
2242  * ice_module_init is the first routine called when the driver is
2243  * loaded. All it does is register with the PCI subsystem.
2244  */
2245 static int __init ice_module_init(void)
2246 {
2247         int status;
2248
2249         pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
2250         pr_info("%s\n", ice_copyright);
2251
2252         ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
2253         if (!ice_wq) {
2254                 pr_err("Failed to create workqueue\n");
2255                 return -ENOMEM;
2256         }
2257
2258         status = pci_register_driver(&ice_driver);
2259         if (status) {
2260                 pr_err("failed to register pci driver, err %d\n", status);
2261                 destroy_workqueue(ice_wq);
2262         }
2263
2264         return status;
2265 }
2266 module_init(ice_module_init);
2267
2268 /**
2269  * ice_module_exit - Driver exit cleanup routine
2270  *
2271  * ice_module_exit is called just before the driver is removed
2272  * from memory.
2273  */
2274 static void __exit ice_module_exit(void)
2275 {
2276         pci_unregister_driver(&ice_driver);
2277         destroy_workqueue(ice_wq);
2278         pr_info("module unloaded\n");
2279 }
2280 module_exit(ice_module_exit);
2281
2282 /**
2283  * ice_set_mac_address - NDO callback to set mac address
2284  * @netdev: network interface device structure
2285  * @pi: pointer to an address structure
2286  *
2287  * Returns 0 on success, negative on failure
2288  */
2289 static int ice_set_mac_address(struct net_device *netdev, void *pi)
2290 {
2291         struct ice_netdev_priv *np = netdev_priv(netdev);
2292         struct ice_vsi *vsi = np->vsi;
2293         struct ice_pf *pf = vsi->back;
2294         struct ice_hw *hw = &pf->hw;
2295         struct sockaddr *addr = pi;
2296         enum ice_status status;
2297         LIST_HEAD(a_mac_list);
2298         LIST_HEAD(r_mac_list);
2299         u8 flags = 0;
2300         int err;
2301         u8 *mac;
2302
2303         mac = (u8 *)addr->sa_data;
2304
2305         if (!is_valid_ether_addr(mac))
2306                 return -EADDRNOTAVAIL;
2307
2308         if (ether_addr_equal(netdev->dev_addr, mac)) {
2309                 netdev_warn(netdev, "already using mac %pM\n", mac);
2310                 return 0;
2311         }
2312
2313         if (test_bit(__ICE_DOWN, pf->state) ||
2314             ice_is_reset_in_progress(pf->state)) {
2315                 netdev_err(netdev, "can't set mac %pM. device not ready\n",
2316                            mac);
2317                 return -EBUSY;
2318         }
2319
2320         /* When we change the mac address we also have to change the mac address
2321          * based filter rules that were created previously for the old mac
2322          * address. So first, we remove the old filter rule using ice_remove_mac
2323          * and then create a new filter rule using ice_add_mac. Note that for
2324          * both these operations, we first need to form a "list" of mac
2325          * addresses (even though in this case, we have only 1 mac address to be
2326          * added/removed) and this done using ice_add_mac_to_list. Depending on
2327          * the ensuing operation this "list" of mac addresses is either to be
2328          * added or removed from the filter.
2329          */
2330         err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
2331         if (err) {
2332                 err = -EADDRNOTAVAIL;
2333                 goto free_lists;
2334         }
2335
2336         status = ice_remove_mac(hw, &r_mac_list);
2337         if (status) {
2338                 err = -EADDRNOTAVAIL;
2339                 goto free_lists;
2340         }
2341
2342         err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
2343         if (err) {
2344                 err = -EADDRNOTAVAIL;
2345                 goto free_lists;
2346         }
2347
2348         status = ice_add_mac(hw, &a_mac_list);
2349         if (status) {
2350                 err = -EADDRNOTAVAIL;
2351                 goto free_lists;
2352         }
2353
2354 free_lists:
2355         /* free list entries */
2356         ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
2357         ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
2358
2359         if (err) {
2360                 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
2361                            mac);
2362                 return err;
2363         }
2364
2365         /* change the netdev's mac address */
2366         memcpy(netdev->dev_addr, mac, netdev->addr_len);
2367         netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
2368                    netdev->dev_addr);
2369
2370         /* write new mac address to the firmware */
2371         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2372         status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
2373         if (status) {
2374                 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
2375                            mac);
2376         }
2377         return 0;
2378 }
2379
2380 /**
2381  * ice_set_rx_mode - NDO callback to set the netdev filters
2382  * @netdev: network interface device structure
2383  */
2384 static void ice_set_rx_mode(struct net_device *netdev)
2385 {
2386         struct ice_netdev_priv *np = netdev_priv(netdev);
2387         struct ice_vsi *vsi = np->vsi;
2388
2389         if (!vsi)
2390                 return;
2391
2392         /* Set the flags to synchronize filters
2393          * ndo_set_rx_mode may be triggered even without a change in netdev
2394          * flags
2395          */
2396         set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
2397         set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
2398         set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
2399
2400         /* schedule our worker thread which will take care of
2401          * applying the new filter changes
2402          */
2403         ice_service_task_schedule(vsi->back);
2404 }
2405
2406 /**
2407  * ice_fdb_add - add an entry to the hardware database
2408  * @ndm: the input from the stack
2409  * @tb: pointer to array of nladdr (unused)
2410  * @dev: the net device pointer
2411  * @addr: the MAC address entry being added
2412  * @vid: VLAN id
2413  * @flags: instructions from stack about fdb operation
2414  */
2415 static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
2416                        struct net_device *dev, const unsigned char *addr,
2417                        u16 vid, u16 flags)
2418 {
2419         int err;
2420
2421         if (vid) {
2422                 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
2423                 return -EINVAL;
2424         }
2425         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2426                 netdev_err(dev, "FDB only supports static addresses\n");
2427                 return -EINVAL;
2428         }
2429
2430         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2431                 err = dev_uc_add_excl(dev, addr);
2432         else if (is_multicast_ether_addr(addr))
2433                 err = dev_mc_add_excl(dev, addr);
2434         else
2435                 err = -EINVAL;
2436
2437         /* Only return duplicate errors if NLM_F_EXCL is set */
2438         if (err == -EEXIST && !(flags & NLM_F_EXCL))
2439                 err = 0;
2440
2441         return err;
2442 }
2443
2444 /**
2445  * ice_fdb_del - delete an entry from the hardware database
2446  * @ndm: the input from the stack
2447  * @tb: pointer to array of nladdr (unused)
2448  * @dev: the net device pointer
2449  * @addr: the MAC address entry being added
2450  * @vid: VLAN id
2451  */
2452 static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
2453                        struct net_device *dev, const unsigned char *addr,
2454                        __always_unused u16 vid)
2455 {
2456         int err;
2457
2458         if (ndm->ndm_state & NUD_PERMANENT) {
2459                 netdev_err(dev, "FDB only supports static addresses\n");
2460                 return -EINVAL;
2461         }
2462
2463         if (is_unicast_ether_addr(addr))
2464                 err = dev_uc_del(dev, addr);
2465         else if (is_multicast_ether_addr(addr))
2466                 err = dev_mc_del(dev, addr);
2467         else
2468                 err = -EINVAL;
2469
2470         return err;
2471 }
2472
2473 /**
2474  * ice_set_features - set the netdev feature flags
2475  * @netdev: ptr to the netdev being adjusted
2476  * @features: the feature set that the stack is suggesting
2477  */
2478 static int ice_set_features(struct net_device *netdev,
2479                             netdev_features_t features)
2480 {
2481         struct ice_netdev_priv *np = netdev_priv(netdev);
2482         struct ice_vsi *vsi = np->vsi;
2483         int ret = 0;
2484
2485         if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
2486                 ret = ice_vsi_manage_rss_lut(vsi, true);
2487         else if (!(features & NETIF_F_RXHASH) &&
2488                  netdev->features & NETIF_F_RXHASH)
2489                 ret = ice_vsi_manage_rss_lut(vsi, false);
2490
2491         if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
2492             !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2493                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2494         else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
2495                  (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2496                 ret = ice_vsi_manage_vlan_stripping(vsi, false);
2497         else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
2498                  !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2499                 ret = ice_vsi_manage_vlan_insertion(vsi);
2500         else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
2501                  (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2502                 ret = ice_vsi_manage_vlan_insertion(vsi);
2503
2504         return ret;
2505 }
2506
2507 /**
2508  * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
2509  * @vsi: VSI to setup vlan properties for
2510  */
2511 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
2512 {
2513         int ret = 0;
2514
2515         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2516                 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2517         if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
2518                 ret = ice_vsi_manage_vlan_insertion(vsi);
2519
2520         return ret;
2521 }
2522
2523 /**
2524  * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
2525  * @vsi: the VSI being brought back up
2526  */
2527 static int ice_restore_vlan(struct ice_vsi *vsi)
2528 {
2529         int err;
2530         u16 vid;
2531
2532         if (!vsi->netdev)
2533                 return -EINVAL;
2534
2535         err = ice_vsi_vlan_setup(vsi);
2536         if (err)
2537                 return err;
2538
2539         for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
2540                 err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
2541                 if (err)
2542                         break;
2543         }
2544
2545         return err;
2546 }
2547
2548 /**
2549  * ice_vsi_cfg - Setup the VSI
2550  * @vsi: the VSI being configured
2551  *
2552  * Return 0 on success and negative value on error
2553  */
2554 static int ice_vsi_cfg(struct ice_vsi *vsi)
2555 {
2556         int err;
2557
2558         if (vsi->netdev) {
2559                 ice_set_rx_mode(vsi->netdev);
2560                 err = ice_restore_vlan(vsi);
2561                 if (err)
2562                         return err;
2563         }
2564
2565         err = ice_vsi_cfg_txqs(vsi);
2566         if (!err)
2567                 err = ice_vsi_cfg_rxqs(vsi);
2568
2569         return err;
2570 }
2571
2572 /**
2573  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
2574  * @vsi: the VSI being configured
2575  */
2576 static void ice_napi_enable_all(struct ice_vsi *vsi)
2577 {
2578         int q_idx;
2579
2580         if (!vsi->netdev)
2581                 return;
2582
2583         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
2584                 napi_enable(&vsi->q_vectors[q_idx]->napi);
2585 }
2586
2587 /**
2588  * ice_up_complete - Finish the last steps of bringing up a connection
2589  * @vsi: The VSI being configured
2590  *
2591  * Return 0 on success and negative value on error
2592  */
2593 static int ice_up_complete(struct ice_vsi *vsi)
2594 {
2595         struct ice_pf *pf = vsi->back;
2596         int err;
2597
2598         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2599                 ice_vsi_cfg_msix(vsi);
2600         else
2601                 return -ENOTSUPP;
2602
2603         /* Enable only Rx rings, Tx rings were enabled by the FW when the
2604          * Tx queue group list was configured and the context bits were
2605          * programmed using ice_vsi_cfg_txqs
2606          */
2607         err = ice_vsi_start_rx_rings(vsi);
2608         if (err)
2609                 return err;
2610
2611         clear_bit(__ICE_DOWN, vsi->state);
2612         ice_napi_enable_all(vsi);
2613         ice_vsi_ena_irq(vsi);
2614
2615         if (vsi->port_info &&
2616             (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
2617             vsi->netdev) {
2618                 ice_print_link_msg(vsi, true);
2619                 netif_tx_start_all_queues(vsi->netdev);
2620                 netif_carrier_on(vsi->netdev);
2621         }
2622
2623         ice_service_task_schedule(pf);
2624
2625         return err;
2626 }
2627
2628 /**
2629  * ice_up - Bring the connection back up after being down
2630  * @vsi: VSI being configured
2631  */
2632 int ice_up(struct ice_vsi *vsi)
2633 {
2634         int err;
2635
2636         err = ice_vsi_cfg(vsi);
2637         if (!err)
2638                 err = ice_up_complete(vsi);
2639
2640         return err;
2641 }
2642
2643 /**
2644  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
2645  * @ring: Tx or Rx ring to read stats from
2646  * @pkts: packets stats counter
2647  * @bytes: bytes stats counter
2648  *
2649  * This function fetches stats from the ring considering the atomic operations
2650  * that needs to be performed to read u64 values in 32 bit machine.
2651  */
2652 static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
2653                                          u64 *bytes)
2654 {
2655         unsigned int start;
2656         *pkts = 0;
2657         *bytes = 0;
2658
2659         if (!ring)
2660                 return;
2661         do {
2662                 start = u64_stats_fetch_begin_irq(&ring->syncp);
2663                 *pkts = ring->stats.pkts;
2664                 *bytes = ring->stats.bytes;
2665         } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
2666 }
2667
2668 /**
2669  * ice_update_vsi_ring_stats - Update VSI stats counters
2670  * @vsi: the VSI to be updated
2671  */
2672 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
2673 {
2674         struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
2675         struct ice_ring *ring;
2676         u64 pkts, bytes;
2677         int i;
2678
2679         /* reset netdev stats */
2680         vsi_stats->tx_packets = 0;
2681         vsi_stats->tx_bytes = 0;
2682         vsi_stats->rx_packets = 0;
2683         vsi_stats->rx_bytes = 0;
2684
2685         /* reset non-netdev (extended) stats */
2686         vsi->tx_restart = 0;
2687         vsi->tx_busy = 0;
2688         vsi->tx_linearize = 0;
2689         vsi->rx_buf_failed = 0;
2690         vsi->rx_page_failed = 0;
2691
2692         rcu_read_lock();
2693
2694         /* update Tx rings counters */
2695         ice_for_each_txq(vsi, i) {
2696                 ring = READ_ONCE(vsi->tx_rings[i]);
2697                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
2698                 vsi_stats->tx_packets += pkts;
2699                 vsi_stats->tx_bytes += bytes;
2700                 vsi->tx_restart += ring->tx_stats.restart_q;
2701                 vsi->tx_busy += ring->tx_stats.tx_busy;
2702                 vsi->tx_linearize += ring->tx_stats.tx_linearize;
2703         }
2704
2705         /* update Rx rings counters */
2706         ice_for_each_rxq(vsi, i) {
2707                 ring = READ_ONCE(vsi->rx_rings[i]);
2708                 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
2709                 vsi_stats->rx_packets += pkts;
2710                 vsi_stats->rx_bytes += bytes;
2711                 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
2712                 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
2713         }
2714
2715         rcu_read_unlock();
2716 }
2717
2718 /**
2719  * ice_update_vsi_stats - Update VSI stats counters
2720  * @vsi: the VSI to be updated
2721  */
2722 static void ice_update_vsi_stats(struct ice_vsi *vsi)
2723 {
2724         struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
2725         struct ice_eth_stats *cur_es = &vsi->eth_stats;
2726         struct ice_pf *pf = vsi->back;
2727
2728         if (test_bit(__ICE_DOWN, vsi->state) ||
2729             test_bit(__ICE_CFG_BUSY, pf->state))
2730                 return;
2731
2732         /* get stats as recorded by Tx/Rx rings */
2733         ice_update_vsi_ring_stats(vsi);
2734
2735         /* get VSI stats as recorded by the hardware */
2736         ice_update_eth_stats(vsi);
2737
2738         cur_ns->tx_errors = cur_es->tx_errors;
2739         cur_ns->rx_dropped = cur_es->rx_discards;
2740         cur_ns->tx_dropped = cur_es->tx_discards;
2741         cur_ns->multicast = cur_es->rx_multicast;
2742
2743         /* update some more netdev stats if this is main VSI */
2744         if (vsi->type == ICE_VSI_PF) {
2745                 cur_ns->rx_crc_errors = pf->stats.crc_errors;
2746                 cur_ns->rx_errors = pf->stats.crc_errors +
2747                                     pf->stats.illegal_bytes;
2748                 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
2749         }
2750 }
2751
2752 /**
2753  * ice_update_pf_stats - Update PF port stats counters
2754  * @pf: PF whose stats needs to be updated
2755  */
2756 static void ice_update_pf_stats(struct ice_pf *pf)
2757 {
2758         struct ice_hw_port_stats *prev_ps, *cur_ps;
2759         struct ice_hw *hw = &pf->hw;
2760         u8 pf_id;
2761
2762         prev_ps = &pf->stats_prev;
2763         cur_ps = &pf->stats;
2764         pf_id = hw->pf_id;
2765
2766         ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
2767                           pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
2768                           &cur_ps->eth.rx_bytes);
2769
2770         ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
2771                           pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
2772                           &cur_ps->eth.rx_unicast);
2773
2774         ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
2775                           pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
2776                           &cur_ps->eth.rx_multicast);
2777
2778         ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
2779                           pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
2780                           &cur_ps->eth.rx_broadcast);
2781
2782         ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
2783                           pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
2784                           &cur_ps->eth.tx_bytes);
2785
2786         ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
2787                           pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
2788                           &cur_ps->eth.tx_unicast);
2789
2790         ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
2791                           pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
2792                           &cur_ps->eth.tx_multicast);
2793
2794         ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
2795                           pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
2796                           &cur_ps->eth.tx_broadcast);
2797
2798         ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
2799                           &prev_ps->tx_dropped_link_down,
2800                           &cur_ps->tx_dropped_link_down);
2801
2802         ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
2803                           pf->stat_prev_loaded, &prev_ps->rx_size_64,
2804                           &cur_ps->rx_size_64);
2805
2806         ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
2807                           pf->stat_prev_loaded, &prev_ps->rx_size_127,
2808                           &cur_ps->rx_size_127);
2809
2810         ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
2811                           pf->stat_prev_loaded, &prev_ps->rx_size_255,
2812                           &cur_ps->rx_size_255);
2813
2814         ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
2815                           pf->stat_prev_loaded, &prev_ps->rx_size_511,
2816                           &cur_ps->rx_size_511);
2817
2818         ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
2819                           GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
2820                           &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
2821
2822         ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
2823                           GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
2824                           &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
2825
2826         ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
2827                           GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
2828                           &prev_ps->rx_size_big, &cur_ps->rx_size_big);
2829
2830         ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
2831                           pf->stat_prev_loaded, &prev_ps->tx_size_64,
2832                           &cur_ps->tx_size_64);
2833
2834         ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
2835                           pf->stat_prev_loaded, &prev_ps->tx_size_127,
2836                           &cur_ps->tx_size_127);
2837
2838         ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
2839                           pf->stat_prev_loaded, &prev_ps->tx_size_255,
2840                           &cur_ps->tx_size_255);
2841
2842         ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
2843                           pf->stat_prev_loaded, &prev_ps->tx_size_511,
2844                           &cur_ps->tx_size_511);
2845
2846         ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
2847                           GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
2848                           &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
2849
2850         ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
2851                           GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
2852                           &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
2853
2854         ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
2855                           GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
2856                           &prev_ps->tx_size_big, &cur_ps->tx_size_big);
2857
2858         ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
2859                           &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
2860
2861         ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
2862                           &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
2863
2864         ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
2865                           &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
2866
2867         ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
2868                           &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
2869
2870         ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
2871                           &prev_ps->crc_errors, &cur_ps->crc_errors);
2872
2873         ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
2874                           &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
2875
2876         ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
2877                           &prev_ps->mac_local_faults,
2878                           &cur_ps->mac_local_faults);
2879
2880         ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
2881                           &prev_ps->mac_remote_faults,
2882                           &cur_ps->mac_remote_faults);
2883
2884         ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
2885                           &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
2886
2887         ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
2888                           &prev_ps->rx_undersize, &cur_ps->rx_undersize);
2889
2890         ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
2891                           &prev_ps->rx_fragments, &cur_ps->rx_fragments);
2892
2893         ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
2894                           &prev_ps->rx_oversize, &cur_ps->rx_oversize);
2895
2896         ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
2897                           &prev_ps->rx_jabber, &cur_ps->rx_jabber);
2898
2899         pf->stat_prev_loaded = true;
2900 }
2901
2902 /**
2903  * ice_get_stats64 - get statistics for network device structure
2904  * @netdev: network interface device structure
2905  * @stats: main device statistics structure
2906  */
2907 static
2908 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
2909 {
2910         struct ice_netdev_priv *np = netdev_priv(netdev);
2911         struct rtnl_link_stats64 *vsi_stats;
2912         struct ice_vsi *vsi = np->vsi;
2913
2914         vsi_stats = &vsi->net_stats;
2915
2916         if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
2917                 return;
2918         /* netdev packet/byte stats come from ring counter. These are obtained
2919          * by summing up ring counters (done by ice_update_vsi_ring_stats).
2920          */
2921         ice_update_vsi_ring_stats(vsi);
2922         stats->tx_packets = vsi_stats->tx_packets;
2923         stats->tx_bytes = vsi_stats->tx_bytes;
2924         stats->rx_packets = vsi_stats->rx_packets;
2925         stats->rx_bytes = vsi_stats->rx_bytes;
2926
2927         /* The rest of the stats can be read from the hardware but instead we
2928          * just return values that the watchdog task has already obtained from
2929          * the hardware.
2930          */
2931         stats->multicast = vsi_stats->multicast;
2932         stats->tx_errors = vsi_stats->tx_errors;
2933         stats->tx_dropped = vsi_stats->tx_dropped;
2934         stats->rx_errors = vsi_stats->rx_errors;
2935         stats->rx_dropped = vsi_stats->rx_dropped;
2936         stats->rx_crc_errors = vsi_stats->rx_crc_errors;
2937         stats->rx_length_errors = vsi_stats->rx_length_errors;
2938 }
2939
2940 /**
2941  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
2942  * @vsi: VSI having NAPI disabled
2943  */
2944 static void ice_napi_disable_all(struct ice_vsi *vsi)
2945 {
2946         int q_idx;
2947
2948         if (!vsi->netdev)
2949                 return;
2950
2951         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
2952                 napi_disable(&vsi->q_vectors[q_idx]->napi);
2953 }
2954
2955 /**
2956  * ice_down - Shutdown the connection
2957  * @vsi: The VSI being stopped
2958  */
2959 int ice_down(struct ice_vsi *vsi)
2960 {
2961         int i, tx_err, rx_err;
2962
2963         /* Caller of this function is expected to set the
2964          * vsi->state __ICE_DOWN bit
2965          */
2966         if (vsi->netdev) {
2967                 netif_carrier_off(vsi->netdev);
2968                 netif_tx_disable(vsi->netdev);
2969         }
2970
2971         ice_vsi_dis_irq(vsi);
2972         tx_err = ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0);
2973         if (tx_err)
2974                 netdev_err(vsi->netdev,
2975                            "Failed stop Tx rings, VSI %d error %d\n",
2976                            vsi->vsi_num, tx_err);
2977
2978         rx_err = ice_vsi_stop_rx_rings(vsi);
2979         if (rx_err)
2980                 netdev_err(vsi->netdev,
2981                            "Failed stop Rx rings, VSI %d error %d\n",
2982                            vsi->vsi_num, rx_err);
2983
2984         ice_napi_disable_all(vsi);
2985
2986         ice_for_each_txq(vsi, i)
2987                 ice_clean_tx_ring(vsi->tx_rings[i]);
2988
2989         ice_for_each_rxq(vsi, i)
2990                 ice_clean_rx_ring(vsi->rx_rings[i]);
2991
2992         if (tx_err || rx_err) {
2993                 netdev_err(vsi->netdev,
2994                            "Failed to close VSI 0x%04X on switch 0x%04X\n",
2995                            vsi->vsi_num, vsi->vsw->sw_id);
2996                 return -EIO;
2997         }
2998
2999         return 0;
3000 }
3001
3002 /**
3003  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
3004  * @vsi: VSI having resources allocated
3005  *
3006  * Return 0 on success, negative on failure
3007  */
3008 static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
3009 {
3010         int i, err = 0;
3011
3012         if (!vsi->num_txq) {
3013                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
3014                         vsi->vsi_num);
3015                 return -EINVAL;
3016         }
3017
3018         ice_for_each_txq(vsi, i) {
3019                 vsi->tx_rings[i]->netdev = vsi->netdev;
3020                 err = ice_setup_tx_ring(vsi->tx_rings[i]);
3021                 if (err)
3022                         break;
3023         }
3024
3025         return err;
3026 }
3027
3028 /**
3029  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
3030  * @vsi: VSI having resources allocated
3031  *
3032  * Return 0 on success, negative on failure
3033  */
3034 static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
3035 {
3036         int i, err = 0;
3037
3038         if (!vsi->num_rxq) {
3039                 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
3040                         vsi->vsi_num);
3041                 return -EINVAL;
3042         }
3043
3044         ice_for_each_rxq(vsi, i) {
3045                 vsi->rx_rings[i]->netdev = vsi->netdev;
3046                 err = ice_setup_rx_ring(vsi->rx_rings[i]);
3047                 if (err)
3048                         break;
3049         }
3050
3051         return err;
3052 }
3053
3054 /**
3055  * ice_vsi_req_irq - Request IRQ from the OS
3056  * @vsi: The VSI IRQ is being requested for
3057  * @basename: name for the vector
3058  *
3059  * Return 0 on success and a negative value on error
3060  */
3061 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
3062 {
3063         struct ice_pf *pf = vsi->back;
3064         int err = -EINVAL;
3065
3066         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3067                 err = ice_vsi_req_irq_msix(vsi, basename);
3068
3069         return err;
3070 }
3071
3072 /**
3073  * ice_vsi_open - Called when a network interface is made active
3074  * @vsi: the VSI to open
3075  *
3076  * Initialization of the VSI
3077  *
3078  * Returns 0 on success, negative value on error
3079  */
3080 static int ice_vsi_open(struct ice_vsi *vsi)
3081 {
3082         char int_name[ICE_INT_NAME_STR_LEN];
3083         struct ice_pf *pf = vsi->back;
3084         int err;
3085
3086         /* allocate descriptors */
3087         err = ice_vsi_setup_tx_rings(vsi);
3088         if (err)
3089                 goto err_setup_tx;
3090
3091         err = ice_vsi_setup_rx_rings(vsi);
3092         if (err)
3093                 goto err_setup_rx;
3094
3095         err = ice_vsi_cfg(vsi);
3096         if (err)
3097                 goto err_setup_rx;
3098
3099         snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3100                  dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
3101         err = ice_vsi_req_irq(vsi, int_name);
3102         if (err)
3103                 goto err_setup_rx;
3104
3105         /* Notify the stack of the actual queue counts. */
3106         err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
3107         if (err)
3108                 goto err_set_qs;
3109
3110         err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
3111         if (err)
3112                 goto err_set_qs;
3113
3114         err = ice_up_complete(vsi);
3115         if (err)
3116                 goto err_up_complete;
3117
3118         return 0;
3119
3120 err_up_complete:
3121         ice_down(vsi);
3122 err_set_qs:
3123         ice_vsi_free_irq(vsi);
3124 err_setup_rx:
3125         ice_vsi_free_rx_rings(vsi);
3126 err_setup_tx:
3127         ice_vsi_free_tx_rings(vsi);
3128
3129         return err;
3130 }
3131
3132 /**
3133  * ice_vsi_release_all - Delete all VSIs
3134  * @pf: PF from which all VSIs are being removed
3135  */
3136 static void ice_vsi_release_all(struct ice_pf *pf)
3137 {
3138         int err, i;
3139
3140         if (!pf->vsi)
3141                 return;
3142
3143         for (i = 0; i < pf->num_alloc_vsi; i++) {
3144                 if (!pf->vsi[i])
3145                         continue;
3146
3147                 err = ice_vsi_release(pf->vsi[i]);
3148                 if (err)
3149                         dev_dbg(&pf->pdev->dev,
3150                                 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
3151                                 i, err, pf->vsi[i]->vsi_num);
3152         }
3153 }
3154
3155 /**
3156  * ice_dis_vsi - pause a VSI
3157  * @vsi: the VSI being paused
3158  */
3159 static void ice_dis_vsi(struct ice_vsi *vsi)
3160 {
3161         if (test_bit(__ICE_DOWN, vsi->state))
3162                 return;
3163
3164         set_bit(__ICE_NEEDS_RESTART, vsi->state);
3165
3166         if (vsi->netdev && netif_running(vsi->netdev) &&
3167             vsi->type == ICE_VSI_PF) {
3168                 rtnl_lock();
3169                 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3170                 rtnl_unlock();
3171         } else {
3172                 ice_vsi_close(vsi);
3173         }
3174 }
3175
3176 /**
3177  * ice_ena_vsi - resume a VSI
3178  * @vsi: the VSI being resume
3179  */
3180 static int ice_ena_vsi(struct ice_vsi *vsi)
3181 {
3182         int err = 0;
3183
3184         if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
3185                 if (vsi->netdev && netif_running(vsi->netdev)) {
3186                         rtnl_lock();
3187                         err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3188                         rtnl_unlock();
3189                 }
3190
3191         return err;
3192 }
3193
3194 /**
3195  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
3196  * @pf: the PF
3197  */
3198 static void ice_pf_dis_all_vsi(struct ice_pf *pf)
3199 {
3200         int v;
3201
3202         ice_for_each_vsi(pf, v)
3203                 if (pf->vsi[v])
3204                         ice_dis_vsi(pf->vsi[v]);
3205 }
3206
3207 /**
3208  * ice_pf_ena_all_vsi - Resume all VSIs on a PF
3209  * @pf: the PF
3210  */
3211 static int ice_pf_ena_all_vsi(struct ice_pf *pf)
3212 {
3213         int v;
3214
3215         ice_for_each_vsi(pf, v)
3216                 if (pf->vsi[v])
3217                         if (ice_ena_vsi(pf->vsi[v]))
3218                                 return -EIO;
3219
3220         return 0;
3221 }
3222
3223 /**
3224  * ice_vsi_rebuild_all - rebuild all VSIs in pf
3225  * @pf: the PF
3226  */
3227 static int ice_vsi_rebuild_all(struct ice_pf *pf)
3228 {
3229         int i;
3230
3231         /* loop through pf->vsi array and reinit the VSI if found */
3232         for (i = 0; i < pf->num_alloc_vsi; i++) {
3233                 int err;
3234
3235                 if (!pf->vsi[i])
3236                         continue;
3237
3238                 /* VF VSI rebuild isn't supported yet */
3239                 if (pf->vsi[i]->type == ICE_VSI_VF)
3240                         continue;
3241
3242                 err = ice_vsi_rebuild(pf->vsi[i]);
3243                 if (err) {
3244                         dev_err(&pf->pdev->dev,
3245                                 "VSI at index %d rebuild failed\n",
3246                                 pf->vsi[i]->idx);
3247                         return err;
3248                 }
3249
3250                 dev_info(&pf->pdev->dev,
3251                          "VSI at index %d rebuilt. vsi_num = 0x%x\n",
3252                          pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3253         }
3254
3255         return 0;
3256 }
3257
3258 /**
3259  * ice_vsi_replay_all - replay all VSIs configuration in the PF
3260  * @pf: the PF
3261  */
3262 static int ice_vsi_replay_all(struct ice_pf *pf)
3263 {
3264         struct ice_hw *hw = &pf->hw;
3265         enum ice_status ret;
3266         int i;
3267
3268         /* loop through pf->vsi array and replay the VSI if found */
3269         for (i = 0; i < pf->num_alloc_vsi; i++) {
3270                 if (!pf->vsi[i])
3271                         continue;
3272
3273                 ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
3274                 if (ret) {
3275                         dev_err(&pf->pdev->dev,
3276                                 "VSI at index %d replay failed %d\n",
3277                                 pf->vsi[i]->idx, ret);
3278                         return -EIO;
3279                 }
3280
3281                 /* Re-map HW VSI number, using VSI handle that has been
3282                  * previously validated in ice_replay_vsi() call above
3283                  */
3284                 pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
3285
3286                 dev_info(&pf->pdev->dev,
3287                          "VSI at index %d filter replayed successfully - vsi_num %i\n",
3288                          pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3289         }
3290
3291         /* Clean up replay filter after successful re-configuration */
3292         ice_replay_post(hw);
3293         return 0;
3294 }
3295
3296 /**
3297  * ice_rebuild - rebuild after reset
3298  * @pf: pf to rebuild
3299  */
3300 static void ice_rebuild(struct ice_pf *pf)
3301 {
3302         struct device *dev = &pf->pdev->dev;
3303         struct ice_hw *hw = &pf->hw;
3304         enum ice_status ret;
3305         int err;
3306
3307         if (test_bit(__ICE_DOWN, pf->state))
3308                 goto clear_recovery;
3309
3310         dev_dbg(dev, "rebuilding pf\n");
3311
3312         ret = ice_init_all_ctrlq(hw);
3313         if (ret) {
3314                 dev_err(dev, "control queues init failed %d\n", ret);
3315                 goto err_init_ctrlq;
3316         }
3317
3318         ret = ice_clear_pf_cfg(hw);
3319         if (ret) {
3320                 dev_err(dev, "clear PF configuration failed %d\n", ret);
3321                 goto err_init_ctrlq;
3322         }
3323
3324         ice_clear_pxe_mode(hw);
3325
3326         ret = ice_get_caps(hw);
3327         if (ret) {
3328                 dev_err(dev, "ice_get_caps failed %d\n", ret);
3329                 goto err_init_ctrlq;
3330         }
3331
3332         err = ice_sched_init_port(hw->port_info);
3333         if (err)
3334                 goto err_sched_init_port;
3335
3336         /* reset search_hint of irq_trackers to 0 since interrupts are
3337          * reclaimed and could be allocated from beginning during VSI rebuild
3338          */
3339         pf->sw_irq_tracker->search_hint = 0;
3340         pf->hw_irq_tracker->search_hint = 0;
3341
3342         err = ice_vsi_rebuild_all(pf);
3343         if (err) {
3344                 dev_err(dev, "ice_vsi_rebuild_all failed\n");
3345                 goto err_vsi_rebuild;
3346         }
3347
3348         err = ice_update_link_info(hw->port_info);
3349         if (err)
3350                 dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
3351
3352         /* Replay all VSIs Configuration, including filters after reset */
3353         if (ice_vsi_replay_all(pf)) {
3354                 dev_err(&pf->pdev->dev,
3355                         "error replaying VSI configurations with switch filter rules\n");
3356                 goto err_vsi_rebuild;
3357         }
3358
3359         /* start misc vector */
3360         if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3361                 err = ice_req_irq_msix_misc(pf);
3362                 if (err) {
3363                         dev_err(dev, "misc vector setup failed: %d\n", err);
3364                         goto err_vsi_rebuild;
3365                 }
3366         }
3367
3368         /* restart the VSIs that were rebuilt and running before the reset */
3369         err = ice_pf_ena_all_vsi(pf);
3370         if (err) {
3371                 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
3372                 /* no need to disable VSIs in tear down path in ice_rebuild()
3373                  * since its already taken care in ice_vsi_open()
3374                  */
3375                 goto err_vsi_rebuild;
3376         }
3377
3378         ice_reset_all_vfs(pf, true);
3379         /* if we get here, reset flow is successful */
3380         clear_bit(__ICE_RESET_FAILED, pf->state);
3381         return;
3382
3383 err_vsi_rebuild:
3384         ice_vsi_release_all(pf);
3385 err_sched_init_port:
3386         ice_sched_cleanup_all(hw);
3387 err_init_ctrlq:
3388         ice_shutdown_all_ctrlq(hw);
3389         set_bit(__ICE_RESET_FAILED, pf->state);
3390 clear_recovery:
3391         /* set this bit in PF state to control service task scheduling */
3392         set_bit(__ICE_NEEDS_RESTART, pf->state);
3393         dev_err(dev, "Rebuild failed, unload and reload driver\n");
3394 }
3395
3396 /**
3397  * ice_change_mtu - NDO callback to change the MTU
3398  * @netdev: network interface device structure
3399  * @new_mtu: new value for maximum frame size
3400  *
3401  * Returns 0 on success, negative on failure
3402  */
3403 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
3404 {
3405         struct ice_netdev_priv *np = netdev_priv(netdev);
3406         struct ice_vsi *vsi = np->vsi;
3407         struct ice_pf *pf = vsi->back;
3408         u8 count = 0;
3409
3410         if (new_mtu == netdev->mtu) {
3411                 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
3412                 return 0;
3413         }
3414
3415         if (new_mtu < netdev->min_mtu) {
3416                 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
3417                            netdev->min_mtu);
3418                 return -EINVAL;
3419         } else if (new_mtu > netdev->max_mtu) {
3420                 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
3421                            netdev->min_mtu);
3422                 return -EINVAL;
3423         }
3424         /* if a reset is in progress, wait for some time for it to complete */
3425         do {
3426                 if (ice_is_reset_in_progress(pf->state)) {
3427                         count++;
3428                         usleep_range(1000, 2000);
3429                 } else {
3430                         break;
3431                 }
3432
3433         } while (count < 100);
3434
3435         if (count == 100) {
3436                 netdev_err(netdev, "can't change mtu. Device is busy\n");
3437                 return -EBUSY;
3438         }
3439
3440         netdev->mtu = new_mtu;
3441
3442         /* if VSI is up, bring it down and then back up */
3443         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
3444                 int err;
3445
3446                 err = ice_down(vsi);
3447                 if (err) {
3448                         netdev_err(netdev, "change mtu if_up err %d\n", err);
3449                         return err;
3450                 }
3451
3452                 err = ice_up(vsi);
3453                 if (err) {
3454                         netdev_err(netdev, "change mtu if_up err %d\n", err);
3455                         return err;
3456                 }
3457         }
3458
3459         netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
3460         return 0;
3461 }
3462
3463 /**
3464  * ice_set_rss - Set RSS keys and lut
3465  * @vsi: Pointer to VSI structure
3466  * @seed: RSS hash seed
3467  * @lut: Lookup table
3468  * @lut_size: Lookup table size
3469  *
3470  * Returns 0 on success, negative on failure
3471  */
3472 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3473 {
3474         struct ice_pf *pf = vsi->back;
3475         struct ice_hw *hw = &pf->hw;
3476         enum ice_status status;
3477
3478         if (seed) {
3479                 struct ice_aqc_get_set_rss_keys *buf =
3480                                   (struct ice_aqc_get_set_rss_keys *)seed;
3481
3482                 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
3483
3484                 if (status) {
3485                         dev_err(&pf->pdev->dev,
3486                                 "Cannot set RSS key, err %d aq_err %d\n",
3487                                 status, hw->adminq.rq_last_status);
3488                         return -EIO;
3489                 }
3490         }
3491
3492         if (lut) {
3493                 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
3494                                             lut, lut_size);
3495                 if (status) {
3496                         dev_err(&pf->pdev->dev,
3497                                 "Cannot set RSS lut, err %d aq_err %d\n",
3498                                 status, hw->adminq.rq_last_status);
3499                         return -EIO;
3500                 }
3501         }
3502
3503         return 0;
3504 }
3505
3506 /**
3507  * ice_get_rss - Get RSS keys and lut
3508  * @vsi: Pointer to VSI structure
3509  * @seed: Buffer to store the keys
3510  * @lut: Buffer to store the lookup table entries
3511  * @lut_size: Size of buffer to store the lookup table entries
3512  *
3513  * Returns 0 on success, negative on failure
3514  */
3515 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3516 {
3517         struct ice_pf *pf = vsi->back;
3518         struct ice_hw *hw = &pf->hw;
3519         enum ice_status status;
3520
3521         if (seed) {
3522                 struct ice_aqc_get_set_rss_keys *buf =
3523                                   (struct ice_aqc_get_set_rss_keys *)seed;
3524
3525                 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
3526                 if (status) {
3527                         dev_err(&pf->pdev->dev,
3528                                 "Cannot get RSS key, err %d aq_err %d\n",
3529                                 status, hw->adminq.rq_last_status);
3530                         return -EIO;
3531                 }
3532         }
3533
3534         if (lut) {
3535                 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
3536                                             lut, lut_size);
3537                 if (status) {
3538                         dev_err(&pf->pdev->dev,
3539                                 "Cannot get RSS lut, err %d aq_err %d\n",
3540                                 status, hw->adminq.rq_last_status);
3541                         return -EIO;
3542                 }
3543         }
3544
3545         return 0;
3546 }
3547
3548 /**
3549  * ice_bridge_getlink - Get the hardware bridge mode
3550  * @skb: skb buff
3551  * @pid: process id
3552  * @seq: RTNL message seq
3553  * @dev: the netdev being configured
3554  * @filter_mask: filter mask passed in
3555  * @nlflags: netlink flags passed in
3556  *
3557  * Return the bridge mode (VEB/VEPA)
3558  */
3559 static int
3560 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3561                    struct net_device *dev, u32 filter_mask, int nlflags)
3562 {
3563         struct ice_netdev_priv *np = netdev_priv(dev);
3564         struct ice_vsi *vsi = np->vsi;
3565         struct ice_pf *pf = vsi->back;
3566         u16 bmode;
3567
3568         bmode = pf->first_sw->bridge_mode;
3569
3570         return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
3571                                        filter_mask, NULL);
3572 }
3573
3574 /**
3575  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
3576  * @vsi: Pointer to VSI structure
3577  * @bmode: Hardware bridge mode (VEB/VEPA)
3578  *
3579  * Returns 0 on success, negative on failure
3580  */
3581 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
3582 {
3583         struct device *dev = &vsi->back->pdev->dev;
3584         struct ice_aqc_vsi_props *vsi_props;
3585         struct ice_hw *hw = &vsi->back->hw;
3586         struct ice_vsi_ctx ctxt = { 0 };
3587         enum ice_status status;
3588
3589         vsi_props = &vsi->info;
3590         ctxt.info = vsi->info;
3591
3592         if (bmode == BRIDGE_MODE_VEB)
3593                 /* change from VEPA to VEB mode */
3594                 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
3595         else
3596                 /* change from VEB to VEPA mode */
3597                 ctxt.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
3598         ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
3599
3600         status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
3601         if (status) {
3602                 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
3603                         bmode, status, hw->adminq.sq_last_status);
3604                 return -EIO;
3605         }
3606         /* Update sw flags for book keeping */
3607         vsi_props->sw_flags = ctxt.info.sw_flags;
3608
3609         return 0;
3610 }
3611
3612 /**
3613  * ice_bridge_setlink - Set the hardware bridge mode
3614  * @dev: the netdev being configured
3615  * @nlh: RTNL message
3616  * @flags: bridge setlink flags
3617  *
3618  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
3619  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
3620  * not already set for all VSIs connected to this switch. And also update the
3621  * unicast switch filter rules for the corresponding switch of the netdev.
3622  */
3623 static int
3624 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
3625                    u16 __always_unused flags)
3626 {
3627         struct ice_netdev_priv *np = netdev_priv(dev);
3628         struct ice_pf *pf = np->vsi->back;
3629         struct nlattr *attr, *br_spec;
3630         struct ice_hw *hw = &pf->hw;
3631         enum ice_status status;
3632         struct ice_sw *pf_sw;
3633         int rem, v, err = 0;
3634
3635         pf_sw = pf->first_sw;
3636         /* find the attribute in the netlink message */
3637         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3638
3639         nla_for_each_nested(attr, br_spec, rem) {
3640                 __u16 mode;
3641
3642                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
3643                         continue;
3644                 mode = nla_get_u16(attr);
3645                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
3646                         return -EINVAL;
3647                 /* Continue  if bridge mode is not being flipped */
3648                 if (mode == pf_sw->bridge_mode)
3649                         continue;
3650                 /* Iterates through the PF VSI list and update the loopback
3651                  * mode of the VSI
3652                  */
3653                 ice_for_each_vsi(pf, v) {
3654                         if (!pf->vsi[v])
3655                                 continue;
3656                         err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
3657                         if (err)
3658                                 return err;
3659                 }
3660
3661                 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
3662                 /* Update the unicast switch filter rules for the corresponding
3663                  * switch of the netdev
3664                  */
3665                 status = ice_update_sw_rule_bridge_mode(hw);
3666                 if (status) {
3667                         netdev_err(dev, "update SW_RULE for bridge mode failed,  = %d err %d aq_err %d\n",
3668                                    mode, status, hw->adminq.sq_last_status);
3669                         /* revert hw->evb_veb */
3670                         hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
3671                         return -EIO;
3672                 }
3673
3674                 pf_sw->bridge_mode = mode;
3675         }
3676
3677         return 0;
3678 }
3679
3680 /**
3681  * ice_tx_timeout - Respond to a Tx Hang
3682  * @netdev: network interface device structure
3683  */
3684 static void ice_tx_timeout(struct net_device *netdev)
3685 {
3686         struct ice_netdev_priv *np = netdev_priv(netdev);
3687         struct ice_ring *tx_ring = NULL;
3688         struct ice_vsi *vsi = np->vsi;
3689         struct ice_pf *pf = vsi->back;
3690         u32 head, val = 0, i;
3691         int hung_queue = -1;
3692
3693         pf->tx_timeout_count++;
3694
3695         /* find the stopped queue the same way the stack does */
3696         for (i = 0; i < netdev->num_tx_queues; i++) {
3697                 struct netdev_queue *q;
3698                 unsigned long trans_start;
3699
3700                 q = netdev_get_tx_queue(netdev, i);
3701                 trans_start = q->trans_start;
3702                 if (netif_xmit_stopped(q) &&
3703                     time_after(jiffies,
3704                                (trans_start + netdev->watchdog_timeo))) {
3705                         hung_queue = i;
3706                         break;
3707                 }
3708         }
3709
3710         if (i == netdev->num_tx_queues) {
3711                 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
3712         } else {
3713                 /* now that we have an index, find the tx_ring struct */
3714                 for (i = 0; i < vsi->num_txq; i++) {
3715                         if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
3716                                 if (hung_queue ==
3717                                     vsi->tx_rings[i]->q_index) {
3718                                         tx_ring = vsi->tx_rings[i];
3719                                         break;
3720                                 }
3721                         }
3722                 }
3723         }
3724
3725         /* Reset recovery level if enough time has elapsed after last timeout.
3726          * Also ensure no new reset action happens before next timeout period.
3727          */
3728         if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
3729                 pf->tx_timeout_recovery_level = 1;
3730         else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
3731                                        netdev->watchdog_timeo)))
3732                 return;
3733
3734         if (tx_ring) {
3735                 head = tx_ring->next_to_clean;
3736                 /* Read interrupt register */
3737                 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3738                         val = rd32(&pf->hw,
3739                                    GLINT_DYN_CTL(tx_ring->q_vector->v_idx +
3740                                         tx_ring->vsi->hw_base_vector));
3741
3742                 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
3743                             vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
3744                             head, tx_ring->next_to_use,
3745                             readl(tx_ring->tail), val);
3746         }
3747
3748         pf->tx_timeout_last_recovery = jiffies;
3749         netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
3750                     pf->tx_timeout_recovery_level, hung_queue);
3751
3752         switch (pf->tx_timeout_recovery_level) {
3753         case 1:
3754                 set_bit(__ICE_PFR_REQ, pf->state);
3755                 break;
3756         case 2:
3757                 set_bit(__ICE_CORER_REQ, pf->state);
3758                 break;
3759         case 3:
3760                 set_bit(__ICE_GLOBR_REQ, pf->state);
3761                 break;
3762         default:
3763                 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
3764                 set_bit(__ICE_DOWN, pf->state);
3765                 set_bit(__ICE_NEEDS_RESTART, vsi->state);
3766                 set_bit(__ICE_SERVICE_DIS, pf->state);
3767                 break;
3768         }
3769
3770         ice_service_task_schedule(pf);
3771         pf->tx_timeout_recovery_level++;
3772 }
3773
3774 /**
3775  * ice_open - Called when a network interface becomes active
3776  * @netdev: network interface device structure
3777  *
3778  * The open entry point is called when a network interface is made
3779  * active by the system (IFF_UP).  At this point all resources needed
3780  * for transmit and receive operations are allocated, the interrupt
3781  * handler is registered with the OS, the netdev watchdog is enabled,
3782  * and the stack is notified that the interface is ready.
3783  *
3784  * Returns 0 on success, negative value on failure
3785  */
3786 static int ice_open(struct net_device *netdev)
3787 {
3788         struct ice_netdev_priv *np = netdev_priv(netdev);
3789         struct ice_vsi *vsi = np->vsi;
3790         int err;
3791
3792         if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
3793                 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
3794                 return -EIO;
3795         }
3796
3797         netif_carrier_off(netdev);
3798
3799         err = ice_vsi_open(vsi);
3800
3801         if (err)
3802                 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
3803                            vsi->vsi_num, vsi->vsw->sw_id);
3804         return err;
3805 }
3806
3807 /**
3808  * ice_stop - Disables a network interface
3809  * @netdev: network interface device structure
3810  *
3811  * The stop entry point is called when an interface is de-activated by the OS,
3812  * and the netdevice enters the DOWN state.  The hardware is still under the
3813  * driver's control, but the netdev interface is disabled.
3814  *
3815  * Returns success only - not allowed to fail
3816  */
3817 static int ice_stop(struct net_device *netdev)
3818 {
3819         struct ice_netdev_priv *np = netdev_priv(netdev);
3820         struct ice_vsi *vsi = np->vsi;
3821
3822         ice_vsi_close(vsi);
3823
3824         return 0;
3825 }
3826
3827 /**
3828  * ice_features_check - Validate encapsulated packet conforms to limits
3829  * @skb: skb buffer
3830  * @netdev: This port's netdev
3831  * @features: Offload features that the stack believes apply
3832  */
3833 static netdev_features_t
3834 ice_features_check(struct sk_buff *skb,
3835                    struct net_device __always_unused *netdev,
3836                    netdev_features_t features)
3837 {
3838         size_t len;
3839
3840         /* No point in doing any of this if neither checksum nor GSO are
3841          * being requested for this frame.  We can rule out both by just
3842          * checking for CHECKSUM_PARTIAL
3843          */
3844         if (skb->ip_summed != CHECKSUM_PARTIAL)
3845                 return features;
3846
3847         /* We cannot support GSO if the MSS is going to be less than
3848          * 64 bytes.  If it is then we need to drop support for GSO.
3849          */
3850         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3851                 features &= ~NETIF_F_GSO_MASK;
3852
3853         len = skb_network_header(skb) - skb->data;
3854         if (len & ~(ICE_TXD_MACLEN_MAX))
3855                 goto out_rm_features;
3856
3857         len = skb_transport_header(skb) - skb_network_header(skb);
3858         if (len & ~(ICE_TXD_IPLEN_MAX))
3859                 goto out_rm_features;
3860
3861         if (skb->encapsulation) {
3862                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3863                 if (len & ~(ICE_TXD_L4LEN_MAX))
3864                         goto out_rm_features;
3865
3866                 len = skb_inner_transport_header(skb) -
3867                       skb_inner_network_header(skb);
3868                 if (len & ~(ICE_TXD_IPLEN_MAX))
3869                         goto out_rm_features;
3870         }
3871
3872         return features;
3873 out_rm_features:
3874         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3875 }
3876
3877 static const struct net_device_ops ice_netdev_ops = {
3878         .ndo_open = ice_open,
3879         .ndo_stop = ice_stop,
3880         .ndo_start_xmit = ice_start_xmit,
3881         .ndo_features_check = ice_features_check,
3882         .ndo_set_rx_mode = ice_set_rx_mode,
3883         .ndo_set_mac_address = ice_set_mac_address,
3884         .ndo_validate_addr = eth_validate_addr,
3885         .ndo_change_mtu = ice_change_mtu,
3886         .ndo_get_stats64 = ice_get_stats64,
3887         .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
3888         .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
3889         .ndo_set_features = ice_set_features,
3890         .ndo_bridge_getlink = ice_bridge_getlink,
3891         .ndo_bridge_setlink = ice_bridge_setlink,
3892         .ndo_fdb_add = ice_fdb_add,
3893         .ndo_fdb_del = ice_fdb_del,
3894         .ndo_tx_timeout = ice_tx_timeout,
3895 };