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