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