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