xref: /linux/drivers/net/ethernet/intel/ice/ice_main.c (revision ff8ee11e778520c5716b7f165d2c7ce14d6a068b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2023, Intel Corporation. */
3 
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 #include "ice.h"
11 #include "ice_base.h"
12 #include "ice_lib.h"
13 #include "ice_fltr.h"
14 #include "ice_dcb_lib.h"
15 #include "ice_dcb_nl.h"
16 #include "devlink/devlink.h"
17 #include "devlink/devlink_port.h"
18 #include "ice_sf_eth.h"
19 #include "ice_hwmon.h"
20 /* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
21  * ice tracepoint functions. This must be done exactly once across the
22  * ice driver.
23  */
24 #define CREATE_TRACE_POINTS
25 #include "ice_trace.h"
26 #include "ice_eswitch.h"
27 #include "ice_tc_lib.h"
28 #include "ice_vsi_vlan_ops.h"
29 #include <net/xdp_sock_drv.h>
30 
31 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
32 static const char ice_driver_string[] = DRV_SUMMARY;
33 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
34 
35 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
36 #define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
37 #define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
38 
39 MODULE_DESCRIPTION(DRV_SUMMARY);
40 MODULE_IMPORT_NS(LIBIE);
41 MODULE_LICENSE("GPL v2");
42 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
43 
44 static int debug = -1;
45 module_param(debug, int, 0644);
46 #ifndef CONFIG_DYNAMIC_DEBUG
47 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
48 #else
49 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
50 #endif /* !CONFIG_DYNAMIC_DEBUG */
51 
52 DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
53 EXPORT_SYMBOL(ice_xdp_locking_key);
54 
55 /**
56  * ice_hw_to_dev - Get device pointer from the hardware structure
57  * @hw: pointer to the device HW structure
58  *
59  * Used to access the device pointer from compilation units which can't easily
60  * include the definition of struct ice_pf without leading to circular header
61  * dependencies.
62  */
63 struct device *ice_hw_to_dev(struct ice_hw *hw)
64 {
65 	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
66 
67 	return &pf->pdev->dev;
68 }
69 
70 static struct workqueue_struct *ice_wq;
71 struct workqueue_struct *ice_lag_wq;
72 static const struct net_device_ops ice_netdev_safe_mode_ops;
73 static const struct net_device_ops ice_netdev_ops;
74 
75 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
76 
77 static void ice_vsi_release_all(struct ice_pf *pf);
78 
79 static int ice_rebuild_channels(struct ice_pf *pf);
80 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
81 
82 static int
83 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
84 		     void *cb_priv, enum tc_setup_type type, void *type_data,
85 		     void *data,
86 		     void (*cleanup)(struct flow_block_cb *block_cb));
87 
88 bool netif_is_ice(const struct net_device *dev)
89 {
90 	return dev && (dev->netdev_ops == &ice_netdev_ops);
91 }
92 
93 /**
94  * ice_get_tx_pending - returns number of Tx descriptors not processed
95  * @ring: the ring of descriptors
96  */
97 static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
98 {
99 	u16 head, tail;
100 
101 	head = ring->next_to_clean;
102 	tail = ring->next_to_use;
103 
104 	if (head != tail)
105 		return (head < tail) ?
106 			tail - head : (tail + ring->count - head);
107 	return 0;
108 }
109 
110 /**
111  * ice_check_for_hang_subtask - check for and recover hung queues
112  * @pf: pointer to PF struct
113  */
114 static void ice_check_for_hang_subtask(struct ice_pf *pf)
115 {
116 	struct ice_vsi *vsi = NULL;
117 	struct ice_hw *hw;
118 	unsigned int i;
119 	int packets;
120 	u32 v;
121 
122 	ice_for_each_vsi(pf, v)
123 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
124 			vsi = pf->vsi[v];
125 			break;
126 		}
127 
128 	if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
129 		return;
130 
131 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
132 		return;
133 
134 	hw = &vsi->back->hw;
135 
136 	ice_for_each_txq(vsi, i) {
137 		struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
138 		struct ice_ring_stats *ring_stats;
139 
140 		if (!tx_ring)
141 			continue;
142 		if (ice_ring_ch_enabled(tx_ring))
143 			continue;
144 
145 		ring_stats = tx_ring->ring_stats;
146 		if (!ring_stats)
147 			continue;
148 
149 		if (tx_ring->desc) {
150 			/* If packet counter has not changed the queue is
151 			 * likely stalled, so force an interrupt for this
152 			 * queue.
153 			 *
154 			 * prev_pkt would be negative if there was no
155 			 * pending work.
156 			 */
157 			packets = ring_stats->stats.pkts & INT_MAX;
158 			if (ring_stats->tx_stats.prev_pkt == packets) {
159 				/* Trigger sw interrupt to revive the queue */
160 				ice_trigger_sw_intr(hw, tx_ring->q_vector);
161 				continue;
162 			}
163 
164 			/* Memory barrier between read of packet count and call
165 			 * to ice_get_tx_pending()
166 			 */
167 			smp_rmb();
168 			ring_stats->tx_stats.prev_pkt =
169 			    ice_get_tx_pending(tx_ring) ? packets : -1;
170 		}
171 	}
172 }
173 
174 /**
175  * ice_init_mac_fltr - Set initial MAC filters
176  * @pf: board private structure
177  *
178  * Set initial set of MAC filters for PF VSI; configure filters for permanent
179  * address and broadcast address. If an error is encountered, netdevice will be
180  * unregistered.
181  */
182 static int ice_init_mac_fltr(struct ice_pf *pf)
183 {
184 	struct ice_vsi *vsi;
185 	u8 *perm_addr;
186 
187 	vsi = ice_get_main_vsi(pf);
188 	if (!vsi)
189 		return -EINVAL;
190 
191 	perm_addr = vsi->port_info->mac.perm_addr;
192 	return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
193 }
194 
195 /**
196  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
197  * @netdev: the net device on which the sync is happening
198  * @addr: MAC address to sync
199  *
200  * This is a callback function which is called by the in kernel device sync
201  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
202  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
203  * MAC filters from the hardware.
204  */
205 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
206 {
207 	struct ice_netdev_priv *np = netdev_priv(netdev);
208 	struct ice_vsi *vsi = np->vsi;
209 
210 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
211 				     ICE_FWD_TO_VSI))
212 		return -EINVAL;
213 
214 	return 0;
215 }
216 
217 /**
218  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
219  * @netdev: the net device on which the unsync is happening
220  * @addr: MAC address to unsync
221  *
222  * This is a callback function which is called by the in kernel device unsync
223  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
224  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
225  * delete the MAC filters from the hardware.
226  */
227 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
228 {
229 	struct ice_netdev_priv *np = netdev_priv(netdev);
230 	struct ice_vsi *vsi = np->vsi;
231 
232 	/* Under some circumstances, we might receive a request to delete our
233 	 * own device address from our uc list. Because we store the device
234 	 * address in the VSI's MAC filter list, we need to ignore such
235 	 * requests and not delete our device address from this list.
236 	 */
237 	if (ether_addr_equal(addr, netdev->dev_addr))
238 		return 0;
239 
240 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
241 				     ICE_FWD_TO_VSI))
242 		return -EINVAL;
243 
244 	return 0;
245 }
246 
247 /**
248  * ice_vsi_fltr_changed - check if filter state changed
249  * @vsi: VSI to be checked
250  *
251  * returns true if filter state has changed, false otherwise.
252  */
253 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
254 {
255 	return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
256 	       test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
257 }
258 
259 /**
260  * ice_set_promisc - Enable promiscuous mode for a given PF
261  * @vsi: the VSI being configured
262  * @promisc_m: mask of promiscuous config bits
263  *
264  */
265 static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
266 {
267 	int status;
268 
269 	if (vsi->type != ICE_VSI_PF)
270 		return 0;
271 
272 	if (ice_vsi_has_non_zero_vlans(vsi)) {
273 		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
274 		status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
275 						       promisc_m);
276 	} else {
277 		status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
278 						  promisc_m, 0);
279 	}
280 	if (status && status != -EEXIST)
281 		return status;
282 
283 	netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
284 		   vsi->vsi_num, promisc_m);
285 	return 0;
286 }
287 
288 /**
289  * ice_clear_promisc - Disable promiscuous mode for a given PF
290  * @vsi: the VSI being configured
291  * @promisc_m: mask of promiscuous config bits
292  *
293  */
294 static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
295 {
296 	int status;
297 
298 	if (vsi->type != ICE_VSI_PF)
299 		return 0;
300 
301 	if (ice_vsi_has_non_zero_vlans(vsi)) {
302 		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
303 		status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
304 							 promisc_m);
305 	} else {
306 		status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
307 						    promisc_m, 0);
308 	}
309 
310 	netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
311 		   vsi->vsi_num, promisc_m);
312 	return status;
313 }
314 
315 /**
316  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
317  * @vsi: ptr to the VSI
318  *
319  * Push any outstanding VSI filter changes through the AdminQ.
320  */
321 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
322 {
323 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
324 	struct device *dev = ice_pf_to_dev(vsi->back);
325 	struct net_device *netdev = vsi->netdev;
326 	bool promisc_forced_on = false;
327 	struct ice_pf *pf = vsi->back;
328 	struct ice_hw *hw = &pf->hw;
329 	u32 changed_flags = 0;
330 	int err;
331 
332 	if (!vsi->netdev)
333 		return -EINVAL;
334 
335 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
336 		usleep_range(1000, 2000);
337 
338 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
339 	vsi->current_netdev_flags = vsi->netdev->flags;
340 
341 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
342 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
343 
344 	if (ice_vsi_fltr_changed(vsi)) {
345 		clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
346 		clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
347 
348 		/* grab the netdev's addr_list_lock */
349 		netif_addr_lock_bh(netdev);
350 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
351 			      ice_add_mac_to_unsync_list);
352 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
353 			      ice_add_mac_to_unsync_list);
354 		/* our temp lists are populated. release lock */
355 		netif_addr_unlock_bh(netdev);
356 	}
357 
358 	/* Remove MAC addresses in the unsync list */
359 	err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
360 	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
361 	if (err) {
362 		netdev_err(netdev, "Failed to delete MAC filters\n");
363 		/* if we failed because of alloc failures, just bail */
364 		if (err == -ENOMEM)
365 			goto out;
366 	}
367 
368 	/* Add MAC addresses in the sync list */
369 	err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
370 	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
371 	/* If filter is added successfully or already exists, do not go into
372 	 * 'if' condition and report it as error. Instead continue processing
373 	 * rest of the function.
374 	 */
375 	if (err && err != -EEXIST) {
376 		netdev_err(netdev, "Failed to add MAC filters\n");
377 		/* If there is no more space for new umac filters, VSI
378 		 * should go into promiscuous mode. There should be some
379 		 * space reserved for promiscuous filters.
380 		 */
381 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
382 		    !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
383 				      vsi->state)) {
384 			promisc_forced_on = true;
385 			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
386 				    vsi->vsi_num);
387 		} else {
388 			goto out;
389 		}
390 	}
391 	err = 0;
392 	/* check for changes in promiscuous modes */
393 	if (changed_flags & IFF_ALLMULTI) {
394 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
395 			err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
396 			if (err) {
397 				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
398 				goto out_promisc;
399 			}
400 		} else {
401 			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
402 			err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
403 			if (err) {
404 				vsi->current_netdev_flags |= IFF_ALLMULTI;
405 				goto out_promisc;
406 			}
407 		}
408 	}
409 
410 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
411 	    test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
412 		clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
413 		if (vsi->current_netdev_flags & IFF_PROMISC) {
414 			/* Apply Rx filter rule to get traffic from wire */
415 			if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
416 				err = ice_set_dflt_vsi(vsi);
417 				if (err && err != -EEXIST) {
418 					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
419 						   err, vsi->vsi_num);
420 					vsi->current_netdev_flags &=
421 						~IFF_PROMISC;
422 					goto out_promisc;
423 				}
424 				err = 0;
425 				vlan_ops->dis_rx_filtering(vsi);
426 
427 				/* promiscuous mode implies allmulticast so
428 				 * that VSIs that are in promiscuous mode are
429 				 * subscribed to multicast packets coming to
430 				 * the port
431 				 */
432 				err = ice_set_promisc(vsi,
433 						      ICE_MCAST_PROMISC_BITS);
434 				if (err)
435 					goto out_promisc;
436 			}
437 		} else {
438 			/* Clear Rx filter to remove traffic from wire */
439 			if (ice_is_vsi_dflt_vsi(vsi)) {
440 				err = ice_clear_dflt_vsi(vsi);
441 				if (err) {
442 					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
443 						   err, vsi->vsi_num);
444 					vsi->current_netdev_flags |=
445 						IFF_PROMISC;
446 					goto out_promisc;
447 				}
448 				if (vsi->netdev->features &
449 				    NETIF_F_HW_VLAN_CTAG_FILTER)
450 					vlan_ops->ena_rx_filtering(vsi);
451 			}
452 
453 			/* disable allmulti here, but only if allmulti is not
454 			 * still enabled for the netdev
455 			 */
456 			if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
457 				err = ice_clear_promisc(vsi,
458 							ICE_MCAST_PROMISC_BITS);
459 				if (err) {
460 					netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
461 						   err, vsi->vsi_num);
462 				}
463 			}
464 		}
465 	}
466 	goto exit;
467 
468 out_promisc:
469 	set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
470 	goto exit;
471 out:
472 	/* if something went wrong then set the changed flag so we try again */
473 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
474 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
475 exit:
476 	clear_bit(ICE_CFG_BUSY, vsi->state);
477 	return err;
478 }
479 
480 /**
481  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
482  * @pf: board private structure
483  */
484 static void ice_sync_fltr_subtask(struct ice_pf *pf)
485 {
486 	int v;
487 
488 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
489 		return;
490 
491 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
492 
493 	ice_for_each_vsi(pf, v)
494 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
495 		    ice_vsi_sync_fltr(pf->vsi[v])) {
496 			/* come back and try again later */
497 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
498 			break;
499 		}
500 }
501 
502 /**
503  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
504  * @pf: the PF
505  * @locked: is the rtnl_lock already held
506  */
507 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
508 {
509 	int node;
510 	int v;
511 
512 	ice_for_each_vsi(pf, v)
513 		if (pf->vsi[v])
514 			ice_dis_vsi(pf->vsi[v], locked);
515 
516 	for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
517 		pf->pf_agg_node[node].num_vsis = 0;
518 
519 	for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
520 		pf->vf_agg_node[node].num_vsis = 0;
521 }
522 
523 /**
524  * ice_clear_sw_switch_recipes - clear switch recipes
525  * @pf: board private structure
526  *
527  * Mark switch recipes as not created in sw structures. There are cases where
528  * rules (especially advanced rules) need to be restored, either re-read from
529  * hardware or added again. For example after the reset. 'recp_created' flag
530  * prevents from doing that and need to be cleared upfront.
531  */
532 static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
533 {
534 	struct ice_sw_recipe *recp;
535 	u8 i;
536 
537 	recp = pf->hw.switch_info->recp_list;
538 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
539 		recp[i].recp_created = false;
540 }
541 
542 /**
543  * ice_prepare_for_reset - prep for reset
544  * @pf: board private structure
545  * @reset_type: reset type requested
546  *
547  * Inform or close all dependent features in prep for reset.
548  */
549 static void
550 ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
551 {
552 	struct ice_hw *hw = &pf->hw;
553 	struct ice_vsi *vsi;
554 	struct ice_vf *vf;
555 	unsigned int bkt;
556 
557 	dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
558 
559 	/* already prepared for reset */
560 	if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
561 		return;
562 
563 	synchronize_irq(pf->oicr_irq.virq);
564 
565 	ice_unplug_aux_dev(pf);
566 
567 	/* Notify VFs of impending reset */
568 	if (ice_check_sq_alive(hw, &hw->mailboxq))
569 		ice_vc_notify_reset(pf);
570 
571 	/* Disable VFs until reset is completed */
572 	mutex_lock(&pf->vfs.table_lock);
573 	ice_for_each_vf(pf, bkt, vf)
574 		ice_set_vf_state_dis(vf);
575 	mutex_unlock(&pf->vfs.table_lock);
576 
577 	if (ice_is_eswitch_mode_switchdev(pf)) {
578 		if (reset_type != ICE_RESET_PFR)
579 			ice_clear_sw_switch_recipes(pf);
580 	}
581 
582 	/* release ADQ specific HW and SW resources */
583 	vsi = ice_get_main_vsi(pf);
584 	if (!vsi)
585 		goto skip;
586 
587 	/* to be on safe side, reset orig_rss_size so that normal flow
588 	 * of deciding rss_size can take precedence
589 	 */
590 	vsi->orig_rss_size = 0;
591 
592 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
593 		if (reset_type == ICE_RESET_PFR) {
594 			vsi->old_ena_tc = vsi->all_enatc;
595 			vsi->old_numtc = vsi->all_numtc;
596 		} else {
597 			ice_remove_q_channels(vsi, true);
598 
599 			/* for other reset type, do not support channel rebuild
600 			 * hence reset needed info
601 			 */
602 			vsi->old_ena_tc = 0;
603 			vsi->all_enatc = 0;
604 			vsi->old_numtc = 0;
605 			vsi->all_numtc = 0;
606 			vsi->req_txq = 0;
607 			vsi->req_rxq = 0;
608 			clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
609 			memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
610 		}
611 	}
612 
613 	if (vsi->netdev)
614 		netif_device_detach(vsi->netdev);
615 skip:
616 
617 	/* clear SW filtering DB */
618 	ice_clear_hw_tbls(hw);
619 	/* disable the VSIs and their queues that are not already DOWN */
620 	set_bit(ICE_VSI_REBUILD_PENDING, ice_get_main_vsi(pf)->state);
621 	ice_pf_dis_all_vsi(pf, false);
622 
623 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
624 		ice_ptp_prepare_for_reset(pf, reset_type);
625 
626 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
627 		ice_gnss_exit(pf);
628 
629 	if (hw->port_info)
630 		ice_sched_clear_port(hw->port_info);
631 
632 	ice_shutdown_all_ctrlq(hw, false);
633 
634 	set_bit(ICE_PREPARED_FOR_RESET, pf->state);
635 }
636 
637 /**
638  * ice_do_reset - Initiate one of many types of resets
639  * @pf: board private structure
640  * @reset_type: reset type requested before this function was called.
641  */
642 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
643 {
644 	struct device *dev = ice_pf_to_dev(pf);
645 	struct ice_hw *hw = &pf->hw;
646 
647 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
648 
649 	if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
650 		dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
651 		reset_type = ICE_RESET_CORER;
652 	}
653 
654 	ice_prepare_for_reset(pf, reset_type);
655 
656 	/* trigger the reset */
657 	if (ice_reset(hw, reset_type)) {
658 		dev_err(dev, "reset %d failed\n", reset_type);
659 		set_bit(ICE_RESET_FAILED, pf->state);
660 		clear_bit(ICE_RESET_OICR_RECV, pf->state);
661 		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
662 		clear_bit(ICE_PFR_REQ, pf->state);
663 		clear_bit(ICE_CORER_REQ, pf->state);
664 		clear_bit(ICE_GLOBR_REQ, pf->state);
665 		wake_up(&pf->reset_wait_queue);
666 		return;
667 	}
668 
669 	/* PFR is a bit of a special case because it doesn't result in an OICR
670 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
671 	 * associated state bits.
672 	 */
673 	if (reset_type == ICE_RESET_PFR) {
674 		pf->pfr_count++;
675 		ice_rebuild(pf, reset_type);
676 		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
677 		clear_bit(ICE_PFR_REQ, pf->state);
678 		wake_up(&pf->reset_wait_queue);
679 		ice_reset_all_vfs(pf);
680 	}
681 }
682 
683 /**
684  * ice_reset_subtask - Set up for resetting the device and driver
685  * @pf: board private structure
686  */
687 static void ice_reset_subtask(struct ice_pf *pf)
688 {
689 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
690 
691 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
692 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
693 	 * of reset is pending and sets bits in pf->state indicating the reset
694 	 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
695 	 * prepare for pending reset if not already (for PF software-initiated
696 	 * global resets the software should already be prepared for it as
697 	 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
698 	 * by firmware or software on other PFs, that bit is not set so prepare
699 	 * for the reset now), poll for reset done, rebuild and return.
700 	 */
701 	if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
702 		/* Perform the largest reset requested */
703 		if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
704 			reset_type = ICE_RESET_CORER;
705 		if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
706 			reset_type = ICE_RESET_GLOBR;
707 		if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
708 			reset_type = ICE_RESET_EMPR;
709 		/* return if no valid reset type requested */
710 		if (reset_type == ICE_RESET_INVAL)
711 			return;
712 		ice_prepare_for_reset(pf, reset_type);
713 
714 		/* make sure we are ready to rebuild */
715 		if (ice_check_reset(&pf->hw)) {
716 			set_bit(ICE_RESET_FAILED, pf->state);
717 		} else {
718 			/* done with reset. start rebuild */
719 			pf->hw.reset_ongoing = false;
720 			ice_rebuild(pf, reset_type);
721 			/* clear bit to resume normal operations, but
722 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
723 			 */
724 			clear_bit(ICE_RESET_OICR_RECV, pf->state);
725 			clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
726 			clear_bit(ICE_PFR_REQ, pf->state);
727 			clear_bit(ICE_CORER_REQ, pf->state);
728 			clear_bit(ICE_GLOBR_REQ, pf->state);
729 			wake_up(&pf->reset_wait_queue);
730 			ice_reset_all_vfs(pf);
731 		}
732 
733 		return;
734 	}
735 
736 	/* No pending resets to finish processing. Check for new resets */
737 	if (test_bit(ICE_PFR_REQ, pf->state)) {
738 		reset_type = ICE_RESET_PFR;
739 		if (pf->lag && pf->lag->bonded) {
740 			dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
741 			reset_type = ICE_RESET_CORER;
742 		}
743 	}
744 	if (test_bit(ICE_CORER_REQ, pf->state))
745 		reset_type = ICE_RESET_CORER;
746 	if (test_bit(ICE_GLOBR_REQ, pf->state))
747 		reset_type = ICE_RESET_GLOBR;
748 	/* If no valid reset type requested just return */
749 	if (reset_type == ICE_RESET_INVAL)
750 		return;
751 
752 	/* reset if not already down or busy */
753 	if (!test_bit(ICE_DOWN, pf->state) &&
754 	    !test_bit(ICE_CFG_BUSY, pf->state)) {
755 		ice_do_reset(pf, reset_type);
756 	}
757 }
758 
759 /**
760  * ice_print_topo_conflict - print topology conflict message
761  * @vsi: the VSI whose topology status is being checked
762  */
763 static void ice_print_topo_conflict(struct ice_vsi *vsi)
764 {
765 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
766 	case ICE_AQ_LINK_TOPO_CONFLICT:
767 	case ICE_AQ_LINK_MEDIA_CONFLICT:
768 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
769 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
770 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
771 		netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
772 		break;
773 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
774 		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
775 			netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
776 		else
777 			netdev_err(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");
778 		break;
779 	default:
780 		break;
781 	}
782 }
783 
784 /**
785  * ice_print_link_msg - print link up or down message
786  * @vsi: the VSI whose link status is being queried
787  * @isup: boolean for if the link is now up or down
788  */
789 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
790 {
791 	struct ice_aqc_get_phy_caps_data *caps;
792 	const char *an_advertised;
793 	const char *fec_req;
794 	const char *speed;
795 	const char *fec;
796 	const char *fc;
797 	const char *an;
798 	int status;
799 
800 	if (!vsi)
801 		return;
802 
803 	if (vsi->current_isup == isup)
804 		return;
805 
806 	vsi->current_isup = isup;
807 
808 	if (!isup) {
809 		netdev_info(vsi->netdev, "NIC Link is Down\n");
810 		return;
811 	}
812 
813 	switch (vsi->port_info->phy.link_info.link_speed) {
814 	case ICE_AQ_LINK_SPEED_200GB:
815 		speed = "200 G";
816 		break;
817 	case ICE_AQ_LINK_SPEED_100GB:
818 		speed = "100 G";
819 		break;
820 	case ICE_AQ_LINK_SPEED_50GB:
821 		speed = "50 G";
822 		break;
823 	case ICE_AQ_LINK_SPEED_40GB:
824 		speed = "40 G";
825 		break;
826 	case ICE_AQ_LINK_SPEED_25GB:
827 		speed = "25 G";
828 		break;
829 	case ICE_AQ_LINK_SPEED_20GB:
830 		speed = "20 G";
831 		break;
832 	case ICE_AQ_LINK_SPEED_10GB:
833 		speed = "10 G";
834 		break;
835 	case ICE_AQ_LINK_SPEED_5GB:
836 		speed = "5 G";
837 		break;
838 	case ICE_AQ_LINK_SPEED_2500MB:
839 		speed = "2.5 G";
840 		break;
841 	case ICE_AQ_LINK_SPEED_1000MB:
842 		speed = "1 G";
843 		break;
844 	case ICE_AQ_LINK_SPEED_100MB:
845 		speed = "100 M";
846 		break;
847 	default:
848 		speed = "Unknown ";
849 		break;
850 	}
851 
852 	switch (vsi->port_info->fc.current_mode) {
853 	case ICE_FC_FULL:
854 		fc = "Rx/Tx";
855 		break;
856 	case ICE_FC_TX_PAUSE:
857 		fc = "Tx";
858 		break;
859 	case ICE_FC_RX_PAUSE:
860 		fc = "Rx";
861 		break;
862 	case ICE_FC_NONE:
863 		fc = "None";
864 		break;
865 	default:
866 		fc = "Unknown";
867 		break;
868 	}
869 
870 	/* Get FEC mode based on negotiated link info */
871 	switch (vsi->port_info->phy.link_info.fec_info) {
872 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
873 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
874 		fec = "RS-FEC";
875 		break;
876 	case ICE_AQ_LINK_25G_KR_FEC_EN:
877 		fec = "FC-FEC/BASE-R";
878 		break;
879 	default:
880 		fec = "NONE";
881 		break;
882 	}
883 
884 	/* check if autoneg completed, might be false due to not supported */
885 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
886 		an = "True";
887 	else
888 		an = "False";
889 
890 	/* Get FEC mode requested based on PHY caps last SW configuration */
891 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
892 	if (!caps) {
893 		fec_req = "Unknown";
894 		an_advertised = "Unknown";
895 		goto done;
896 	}
897 
898 	status = ice_aq_get_phy_caps(vsi->port_info, false,
899 				     ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
900 	if (status)
901 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
902 
903 	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
904 
905 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
906 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
907 		fec_req = "RS-FEC";
908 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
909 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
910 		fec_req = "FC-FEC/BASE-R";
911 	else
912 		fec_req = "NONE";
913 
914 	kfree(caps);
915 
916 done:
917 	netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
918 		    speed, fec_req, fec, an_advertised, an, fc);
919 	ice_print_topo_conflict(vsi);
920 }
921 
922 /**
923  * ice_vsi_link_event - update the VSI's netdev
924  * @vsi: the VSI on which the link event occurred
925  * @link_up: whether or not the VSI needs to be set up or down
926  */
927 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
928 {
929 	if (!vsi)
930 		return;
931 
932 	if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
933 		return;
934 
935 	if (vsi->type == ICE_VSI_PF) {
936 		if (link_up == netif_carrier_ok(vsi->netdev))
937 			return;
938 
939 		if (link_up) {
940 			netif_carrier_on(vsi->netdev);
941 			netif_tx_wake_all_queues(vsi->netdev);
942 		} else {
943 			netif_carrier_off(vsi->netdev);
944 			netif_tx_stop_all_queues(vsi->netdev);
945 		}
946 	}
947 }
948 
949 /**
950  * ice_set_dflt_mib - send a default config MIB to the FW
951  * @pf: private PF struct
952  *
953  * This function sends a default configuration MIB to the FW.
954  *
955  * If this function errors out at any point, the driver is still able to
956  * function.  The main impact is that LFC may not operate as expected.
957  * Therefore an error state in this function should be treated with a DBG
958  * message and continue on with driver rebuild/reenable.
959  */
960 static void ice_set_dflt_mib(struct ice_pf *pf)
961 {
962 	struct device *dev = ice_pf_to_dev(pf);
963 	u8 mib_type, *buf, *lldpmib = NULL;
964 	u16 len, typelen, offset = 0;
965 	struct ice_lldp_org_tlv *tlv;
966 	struct ice_hw *hw = &pf->hw;
967 	u32 ouisubtype;
968 
969 	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
970 	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
971 	if (!lldpmib) {
972 		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
973 			__func__);
974 		return;
975 	}
976 
977 	/* Add ETS CFG TLV */
978 	tlv = (struct ice_lldp_org_tlv *)lldpmib;
979 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
980 		   ICE_IEEE_ETS_TLV_LEN);
981 	tlv->typelen = htons(typelen);
982 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
983 		      ICE_IEEE_SUBTYPE_ETS_CFG);
984 	tlv->ouisubtype = htonl(ouisubtype);
985 
986 	buf = tlv->tlvinfo;
987 	buf[0] = 0;
988 
989 	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
990 	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
991 	 * Octets 13 - 20 are TSA values - leave as zeros
992 	 */
993 	buf[5] = 0x64;
994 	len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
995 	offset += len + 2;
996 	tlv = (struct ice_lldp_org_tlv *)
997 		((char *)tlv + sizeof(tlv->typelen) + len);
998 
999 	/* Add ETS REC TLV */
1000 	buf = tlv->tlvinfo;
1001 	tlv->typelen = htons(typelen);
1002 
1003 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1004 		      ICE_IEEE_SUBTYPE_ETS_REC);
1005 	tlv->ouisubtype = htonl(ouisubtype);
1006 
1007 	/* First octet of buf is reserved
1008 	 * Octets 1 - 4 map UP to TC - all UPs map to zero
1009 	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
1010 	 * Octets 13 - 20 are TSA value - leave as zeros
1011 	 */
1012 	buf[5] = 0x64;
1013 	offset += len + 2;
1014 	tlv = (struct ice_lldp_org_tlv *)
1015 		((char *)tlv + sizeof(tlv->typelen) + len);
1016 
1017 	/* Add PFC CFG TLV */
1018 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1019 		   ICE_IEEE_PFC_TLV_LEN);
1020 	tlv->typelen = htons(typelen);
1021 
1022 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1023 		      ICE_IEEE_SUBTYPE_PFC_CFG);
1024 	tlv->ouisubtype = htonl(ouisubtype);
1025 
1026 	/* Octet 1 left as all zeros - PFC disabled */
1027 	buf[0] = 0x08;
1028 	len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
1029 	offset += len + 2;
1030 
1031 	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1032 		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1033 
1034 	kfree(lldpmib);
1035 }
1036 
1037 /**
1038  * ice_check_phy_fw_load - check if PHY FW load failed
1039  * @pf: pointer to PF struct
1040  * @link_cfg_err: bitmap from the link info structure
1041  *
1042  * check if external PHY FW load failed and print an error message if it did
1043  */
1044 static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1045 {
1046 	if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1047 		clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1048 		return;
1049 	}
1050 
1051 	if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1052 		return;
1053 
1054 	if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1055 		dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
1056 		set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1057 	}
1058 }
1059 
1060 /**
1061  * ice_check_module_power
1062  * @pf: pointer to PF struct
1063  * @link_cfg_err: bitmap from the link info structure
1064  *
1065  * check module power level returned by a previous call to aq_get_link_info
1066  * and print error messages if module power level is not supported
1067  */
1068 static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1069 {
1070 	/* if module power level is supported, clear the flag */
1071 	if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1072 			      ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1073 		clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1074 		return;
1075 	}
1076 
1077 	/* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1078 	 * above block didn't clear this bit, there's nothing to do
1079 	 */
1080 	if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1081 		return;
1082 
1083 	if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1084 		dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1085 		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1086 	} else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1087 		dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1088 		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1089 	}
1090 }
1091 
1092 /**
1093  * ice_check_link_cfg_err - check if link configuration failed
1094  * @pf: pointer to the PF struct
1095  * @link_cfg_err: bitmap from the link info structure
1096  *
1097  * print if any link configuration failure happens due to the value in the
1098  * link_cfg_err parameter in the link info structure
1099  */
1100 static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1101 {
1102 	ice_check_module_power(pf, link_cfg_err);
1103 	ice_check_phy_fw_load(pf, link_cfg_err);
1104 }
1105 
1106 /**
1107  * ice_link_event - process the link event
1108  * @pf: PF that the link event is associated with
1109  * @pi: port_info for the port that the link event is associated with
1110  * @link_up: true if the physical link is up and false if it is down
1111  * @link_speed: current link speed received from the link event
1112  *
1113  * Returns 0 on success and negative on failure
1114  */
1115 static int
1116 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1117 	       u16 link_speed)
1118 {
1119 	struct device *dev = ice_pf_to_dev(pf);
1120 	struct ice_phy_info *phy_info;
1121 	struct ice_vsi *vsi;
1122 	u16 old_link_speed;
1123 	bool old_link;
1124 	int status;
1125 
1126 	phy_info = &pi->phy;
1127 	phy_info->link_info_old = phy_info->link_info;
1128 
1129 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1130 	old_link_speed = phy_info->link_info_old.link_speed;
1131 
1132 	/* update the link info structures and re-enable link events,
1133 	 * don't bail on failure due to other book keeping needed
1134 	 */
1135 	status = ice_update_link_info(pi);
1136 	if (status)
1137 		dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1138 			pi->lport, status,
1139 			ice_aq_str(pi->hw->adminq.sq_last_status));
1140 
1141 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1142 
1143 	/* Check if the link state is up after updating link info, and treat
1144 	 * this event as an UP event since the link is actually UP now.
1145 	 */
1146 	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1147 		link_up = true;
1148 
1149 	vsi = ice_get_main_vsi(pf);
1150 	if (!vsi || !vsi->port_info)
1151 		return -EINVAL;
1152 
1153 	/* turn off PHY if media was removed */
1154 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1155 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1156 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1157 		ice_set_link(vsi, false);
1158 	}
1159 
1160 	/* if the old link up/down and speed is the same as the new */
1161 	if (link_up == old_link && link_speed == old_link_speed)
1162 		return 0;
1163 
1164 	ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1165 
1166 	if (ice_is_dcb_active(pf)) {
1167 		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1168 			ice_dcb_rebuild(pf);
1169 	} else {
1170 		if (link_up)
1171 			ice_set_dflt_mib(pf);
1172 	}
1173 	ice_vsi_link_event(vsi, link_up);
1174 	ice_print_link_msg(vsi, link_up);
1175 
1176 	ice_vc_notify_link_state(pf);
1177 
1178 	return 0;
1179 }
1180 
1181 /**
1182  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1183  * @pf: board private structure
1184  */
1185 static void ice_watchdog_subtask(struct ice_pf *pf)
1186 {
1187 	int i;
1188 
1189 	/* if interface is down do nothing */
1190 	if (test_bit(ICE_DOWN, pf->state) ||
1191 	    test_bit(ICE_CFG_BUSY, pf->state))
1192 		return;
1193 
1194 	/* make sure we don't do these things too often */
1195 	if (time_before(jiffies,
1196 			pf->serv_tmr_prev + pf->serv_tmr_period))
1197 		return;
1198 
1199 	pf->serv_tmr_prev = jiffies;
1200 
1201 	/* Update the stats for active netdevs so the network stack
1202 	 * can look at updated numbers whenever it cares to
1203 	 */
1204 	ice_update_pf_stats(pf);
1205 	ice_for_each_vsi(pf, i)
1206 		if (pf->vsi[i] && pf->vsi[i]->netdev)
1207 			ice_update_vsi_stats(pf->vsi[i]);
1208 }
1209 
1210 /**
1211  * ice_init_link_events - enable/initialize link events
1212  * @pi: pointer to the port_info instance
1213  *
1214  * Returns -EIO on failure, 0 on success
1215  */
1216 static int ice_init_link_events(struct ice_port_info *pi)
1217 {
1218 	u16 mask;
1219 
1220 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1221 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1222 		       ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1223 
1224 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1225 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1226 			pi->lport);
1227 		return -EIO;
1228 	}
1229 
1230 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1231 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1232 			pi->lport);
1233 		return -EIO;
1234 	}
1235 
1236 	return 0;
1237 }
1238 
1239 /**
1240  * ice_handle_link_event - handle link event via ARQ
1241  * @pf: PF that the link event is associated with
1242  * @event: event structure containing link status info
1243  */
1244 static int
1245 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1246 {
1247 	struct ice_aqc_get_link_status_data *link_data;
1248 	struct ice_port_info *port_info;
1249 	int status;
1250 
1251 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1252 	port_info = pf->hw.port_info;
1253 	if (!port_info)
1254 		return -EINVAL;
1255 
1256 	status = ice_link_event(pf, port_info,
1257 				!!(link_data->link_info & ICE_AQ_LINK_UP),
1258 				le16_to_cpu(link_data->link_speed));
1259 	if (status)
1260 		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1261 			status);
1262 
1263 	return status;
1264 }
1265 
1266 /**
1267  * ice_get_fwlog_data - copy the FW log data from ARQ event
1268  * @pf: PF that the FW log event is associated with
1269  * @event: event structure containing FW log data
1270  */
1271 static void
1272 ice_get_fwlog_data(struct ice_pf *pf, struct ice_rq_event_info *event)
1273 {
1274 	struct ice_fwlog_data *fwlog;
1275 	struct ice_hw *hw = &pf->hw;
1276 
1277 	fwlog = &hw->fwlog_ring.rings[hw->fwlog_ring.tail];
1278 
1279 	memset(fwlog->data, 0, PAGE_SIZE);
1280 	fwlog->data_size = le16_to_cpu(event->desc.datalen);
1281 
1282 	memcpy(fwlog->data, event->msg_buf, fwlog->data_size);
1283 	ice_fwlog_ring_increment(&hw->fwlog_ring.tail, hw->fwlog_ring.size);
1284 
1285 	if (ice_fwlog_ring_full(&hw->fwlog_ring)) {
1286 		/* the rings are full so bump the head to create room */
1287 		ice_fwlog_ring_increment(&hw->fwlog_ring.head,
1288 					 hw->fwlog_ring.size);
1289 	}
1290 }
1291 
1292 /**
1293  * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
1294  * @pf: pointer to the PF private structure
1295  * @task: intermediate helper storage and identifier for waiting
1296  * @opcode: the opcode to wait for
1297  *
1298  * Prepares to wait for a specific AdminQ completion event on the ARQ for
1299  * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
1300  *
1301  * Calls are separated to allow caller registering for event before sending
1302  * the command, which mitigates a race between registering and FW responding.
1303  *
1304  * To obtain only the descriptor contents, pass an task->event with null
1305  * msg_buf. If the complete data buffer is desired, allocate the
1306  * task->event.msg_buf with enough space ahead of time.
1307  */
1308 void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1309 			   u16 opcode)
1310 {
1311 	INIT_HLIST_NODE(&task->entry);
1312 	task->opcode = opcode;
1313 	task->state = ICE_AQ_TASK_WAITING;
1314 
1315 	spin_lock_bh(&pf->aq_wait_lock);
1316 	hlist_add_head(&task->entry, &pf->aq_wait_list);
1317 	spin_unlock_bh(&pf->aq_wait_lock);
1318 }
1319 
1320 /**
1321  * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1322  * @pf: pointer to the PF private structure
1323  * @task: ptr prepared by ice_aq_prep_for_event()
1324  * @timeout: how long to wait, in jiffies
1325  *
1326  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1327  * current thread will be put to sleep until the specified event occurs or
1328  * until the given timeout is reached.
1329  *
1330  * Returns: zero on success, or a negative error code on failure.
1331  */
1332 int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1333 			  unsigned long timeout)
1334 {
1335 	enum ice_aq_task_state *state = &task->state;
1336 	struct device *dev = ice_pf_to_dev(pf);
1337 	unsigned long start = jiffies;
1338 	long ret;
1339 	int err;
1340 
1341 	ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1342 					       *state != ICE_AQ_TASK_WAITING,
1343 					       timeout);
1344 	switch (*state) {
1345 	case ICE_AQ_TASK_NOT_PREPARED:
1346 		WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1347 		err = -EINVAL;
1348 		break;
1349 	case ICE_AQ_TASK_WAITING:
1350 		err = ret < 0 ? ret : -ETIMEDOUT;
1351 		break;
1352 	case ICE_AQ_TASK_CANCELED:
1353 		err = ret < 0 ? ret : -ECANCELED;
1354 		break;
1355 	case ICE_AQ_TASK_COMPLETE:
1356 		err = ret < 0 ? ret : 0;
1357 		break;
1358 	default:
1359 		WARN(1, "Unexpected AdminQ wait task state %u", *state);
1360 		err = -EINVAL;
1361 		break;
1362 	}
1363 
1364 	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1365 		jiffies_to_msecs(jiffies - start),
1366 		jiffies_to_msecs(timeout),
1367 		task->opcode);
1368 
1369 	spin_lock_bh(&pf->aq_wait_lock);
1370 	hlist_del(&task->entry);
1371 	spin_unlock_bh(&pf->aq_wait_lock);
1372 
1373 	return err;
1374 }
1375 
1376 /**
1377  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1378  * @pf: pointer to the PF private structure
1379  * @opcode: the opcode of the event
1380  * @event: the event to check
1381  *
1382  * Loops over the current list of pending threads waiting for an AdminQ event.
1383  * For each matching task, copy the contents of the event into the task
1384  * structure and wake up the thread.
1385  *
1386  * If multiple threads wait for the same opcode, they will all be woken up.
1387  *
1388  * Note that event->msg_buf will only be duplicated if the event has a buffer
1389  * with enough space already allocated. Otherwise, only the descriptor and
1390  * message length will be copied.
1391  *
1392  * Returns: true if an event was found, false otherwise
1393  */
1394 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1395 				struct ice_rq_event_info *event)
1396 {
1397 	struct ice_rq_event_info *task_ev;
1398 	struct ice_aq_task *task;
1399 	bool found = false;
1400 
1401 	spin_lock_bh(&pf->aq_wait_lock);
1402 	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1403 		if (task->state != ICE_AQ_TASK_WAITING)
1404 			continue;
1405 		if (task->opcode != opcode)
1406 			continue;
1407 
1408 		task_ev = &task->event;
1409 		memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1410 		task_ev->msg_len = event->msg_len;
1411 
1412 		/* Only copy the data buffer if a destination was set */
1413 		if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1414 			memcpy(task_ev->msg_buf, event->msg_buf,
1415 			       event->buf_len);
1416 			task_ev->buf_len = event->buf_len;
1417 		}
1418 
1419 		task->state = ICE_AQ_TASK_COMPLETE;
1420 		found = true;
1421 	}
1422 	spin_unlock_bh(&pf->aq_wait_lock);
1423 
1424 	if (found)
1425 		wake_up(&pf->aq_wait_queue);
1426 }
1427 
1428 /**
1429  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1430  * @pf: the PF private structure
1431  *
1432  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1433  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1434  */
1435 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1436 {
1437 	struct ice_aq_task *task;
1438 
1439 	spin_lock_bh(&pf->aq_wait_lock);
1440 	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1441 		task->state = ICE_AQ_TASK_CANCELED;
1442 	spin_unlock_bh(&pf->aq_wait_lock);
1443 
1444 	wake_up(&pf->aq_wait_queue);
1445 }
1446 
1447 #define ICE_MBX_OVERFLOW_WATERMARK 64
1448 
1449 /**
1450  * __ice_clean_ctrlq - helper function to clean controlq rings
1451  * @pf: ptr to struct ice_pf
1452  * @q_type: specific Control queue type
1453  */
1454 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1455 {
1456 	struct device *dev = ice_pf_to_dev(pf);
1457 	struct ice_rq_event_info event;
1458 	struct ice_hw *hw = &pf->hw;
1459 	struct ice_ctl_q_info *cq;
1460 	u16 pending, i = 0;
1461 	const char *qtype;
1462 	u32 oldval, val;
1463 
1464 	/* Do not clean control queue if/when PF reset fails */
1465 	if (test_bit(ICE_RESET_FAILED, pf->state))
1466 		return 0;
1467 
1468 	switch (q_type) {
1469 	case ICE_CTL_Q_ADMIN:
1470 		cq = &hw->adminq;
1471 		qtype = "Admin";
1472 		break;
1473 	case ICE_CTL_Q_SB:
1474 		cq = &hw->sbq;
1475 		qtype = "Sideband";
1476 		break;
1477 	case ICE_CTL_Q_MAILBOX:
1478 		cq = &hw->mailboxq;
1479 		qtype = "Mailbox";
1480 		/* we are going to try to detect a malicious VF, so set the
1481 		 * state to begin detection
1482 		 */
1483 		hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1484 		break;
1485 	default:
1486 		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1487 		return 0;
1488 	}
1489 
1490 	/* check for error indications - PF_xx_AxQLEN register layout for
1491 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1492 	 */
1493 	val = rd32(hw, cq->rq.len);
1494 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1495 		   PF_FW_ARQLEN_ARQCRIT_M)) {
1496 		oldval = val;
1497 		if (val & PF_FW_ARQLEN_ARQVFE_M)
1498 			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1499 				qtype);
1500 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1501 			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1502 				qtype);
1503 		}
1504 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1505 			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1506 				qtype);
1507 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1508 			 PF_FW_ARQLEN_ARQCRIT_M);
1509 		if (oldval != val)
1510 			wr32(hw, cq->rq.len, val);
1511 	}
1512 
1513 	val = rd32(hw, cq->sq.len);
1514 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1515 		   PF_FW_ATQLEN_ATQCRIT_M)) {
1516 		oldval = val;
1517 		if (val & PF_FW_ATQLEN_ATQVFE_M)
1518 			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1519 				qtype);
1520 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1521 			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1522 				qtype);
1523 		}
1524 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1525 			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1526 				qtype);
1527 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1528 			 PF_FW_ATQLEN_ATQCRIT_M);
1529 		if (oldval != val)
1530 			wr32(hw, cq->sq.len, val);
1531 	}
1532 
1533 	event.buf_len = cq->rq_buf_size;
1534 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1535 	if (!event.msg_buf)
1536 		return 0;
1537 
1538 	do {
1539 		struct ice_mbx_data data = {};
1540 		u16 opcode;
1541 		int ret;
1542 
1543 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1544 		if (ret == -EALREADY)
1545 			break;
1546 		if (ret) {
1547 			dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1548 				ret);
1549 			break;
1550 		}
1551 
1552 		opcode = le16_to_cpu(event.desc.opcode);
1553 
1554 		/* Notify any thread that might be waiting for this event */
1555 		ice_aq_check_events(pf, opcode, &event);
1556 
1557 		switch (opcode) {
1558 		case ice_aqc_opc_get_link_status:
1559 			if (ice_handle_link_event(pf, &event))
1560 				dev_err(dev, "Could not handle link event\n");
1561 			break;
1562 		case ice_aqc_opc_event_lan_overflow:
1563 			ice_vf_lan_overflow_event(pf, &event);
1564 			break;
1565 		case ice_mbx_opc_send_msg_to_pf:
1566 			data.num_msg_proc = i;
1567 			data.num_pending_arq = pending;
1568 			data.max_num_msgs_mbx = hw->mailboxq.num_rq_entries;
1569 			data.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
1570 
1571 			ice_vc_process_vf_msg(pf, &event, &data);
1572 			break;
1573 		case ice_aqc_opc_fw_logs_event:
1574 			ice_get_fwlog_data(pf, &event);
1575 			break;
1576 		case ice_aqc_opc_lldp_set_mib_change:
1577 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1578 			break;
1579 		default:
1580 			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1581 				qtype, opcode);
1582 			break;
1583 		}
1584 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1585 
1586 	kfree(event.msg_buf);
1587 
1588 	return pending && (i == ICE_DFLT_IRQ_WORK);
1589 }
1590 
1591 /**
1592  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1593  * @hw: pointer to hardware info
1594  * @cq: control queue information
1595  *
1596  * returns true if there are pending messages in a queue, false if there aren't
1597  */
1598 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1599 {
1600 	u16 ntu;
1601 
1602 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1603 	return cq->rq.next_to_clean != ntu;
1604 }
1605 
1606 /**
1607  * ice_clean_adminq_subtask - clean the AdminQ rings
1608  * @pf: board private structure
1609  */
1610 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1611 {
1612 	struct ice_hw *hw = &pf->hw;
1613 
1614 	if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1615 		return;
1616 
1617 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1618 		return;
1619 
1620 	clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1621 
1622 	/* There might be a situation where new messages arrive to a control
1623 	 * queue between processing the last message and clearing the
1624 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1625 	 * ice_ctrlq_pending) and process new messages if any.
1626 	 */
1627 	if (ice_ctrlq_pending(hw, &hw->adminq))
1628 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1629 
1630 	ice_flush(hw);
1631 }
1632 
1633 /**
1634  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1635  * @pf: board private structure
1636  */
1637 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1638 {
1639 	struct ice_hw *hw = &pf->hw;
1640 
1641 	if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1642 		return;
1643 
1644 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1645 		return;
1646 
1647 	clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1648 
1649 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1650 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1651 
1652 	ice_flush(hw);
1653 }
1654 
1655 /**
1656  * ice_clean_sbq_subtask - clean the Sideband Queue rings
1657  * @pf: board private structure
1658  */
1659 static void ice_clean_sbq_subtask(struct ice_pf *pf)
1660 {
1661 	struct ice_hw *hw = &pf->hw;
1662 
1663 	/* if mac_type is not generic, sideband is not supported
1664 	 * and there's nothing to do here
1665 	 */
1666 	if (!ice_is_generic_mac(hw)) {
1667 		clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1668 		return;
1669 	}
1670 
1671 	if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1672 		return;
1673 
1674 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1675 		return;
1676 
1677 	clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1678 
1679 	if (ice_ctrlq_pending(hw, &hw->sbq))
1680 		__ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1681 
1682 	ice_flush(hw);
1683 }
1684 
1685 /**
1686  * ice_service_task_schedule - schedule the service task to wake up
1687  * @pf: board private structure
1688  *
1689  * If not already scheduled, this puts the task into the work queue.
1690  */
1691 void ice_service_task_schedule(struct ice_pf *pf)
1692 {
1693 	if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1694 	    !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1695 	    !test_bit(ICE_NEEDS_RESTART, pf->state))
1696 		queue_work(ice_wq, &pf->serv_task);
1697 }
1698 
1699 /**
1700  * ice_service_task_complete - finish up the service task
1701  * @pf: board private structure
1702  */
1703 static void ice_service_task_complete(struct ice_pf *pf)
1704 {
1705 	WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1706 
1707 	/* force memory (pf->state) to sync before next service task */
1708 	smp_mb__before_atomic();
1709 	clear_bit(ICE_SERVICE_SCHED, pf->state);
1710 }
1711 
1712 /**
1713  * ice_service_task_stop - stop service task and cancel works
1714  * @pf: board private structure
1715  *
1716  * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1717  * 1 otherwise.
1718  */
1719 static int ice_service_task_stop(struct ice_pf *pf)
1720 {
1721 	int ret;
1722 
1723 	ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1724 
1725 	if (pf->serv_tmr.function)
1726 		del_timer_sync(&pf->serv_tmr);
1727 	if (pf->serv_task.func)
1728 		cancel_work_sync(&pf->serv_task);
1729 
1730 	clear_bit(ICE_SERVICE_SCHED, pf->state);
1731 	return ret;
1732 }
1733 
1734 /**
1735  * ice_service_task_restart - restart service task and schedule works
1736  * @pf: board private structure
1737  *
1738  * This function is needed for suspend and resume works (e.g WoL scenario)
1739  */
1740 static void ice_service_task_restart(struct ice_pf *pf)
1741 {
1742 	clear_bit(ICE_SERVICE_DIS, pf->state);
1743 	ice_service_task_schedule(pf);
1744 }
1745 
1746 /**
1747  * ice_service_timer - timer callback to schedule service task
1748  * @t: pointer to timer_list
1749  */
1750 static void ice_service_timer(struct timer_list *t)
1751 {
1752 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1753 
1754 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1755 	ice_service_task_schedule(pf);
1756 }
1757 
1758 /**
1759  * ice_mdd_maybe_reset_vf - reset VF after MDD event
1760  * @pf: pointer to the PF structure
1761  * @vf: pointer to the VF structure
1762  * @reset_vf_tx: whether Tx MDD has occurred
1763  * @reset_vf_rx: whether Rx MDD has occurred
1764  *
1765  * Since the queue can get stuck on VF MDD events, the PF can be configured to
1766  * automatically reset the VF by enabling the private ethtool flag
1767  * mdd-auto-reset-vf.
1768  */
1769 static void ice_mdd_maybe_reset_vf(struct ice_pf *pf, struct ice_vf *vf,
1770 				   bool reset_vf_tx, bool reset_vf_rx)
1771 {
1772 	struct device *dev = ice_pf_to_dev(pf);
1773 
1774 	if (!test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags))
1775 		return;
1776 
1777 	/* VF MDD event counters will be cleared by reset, so print the event
1778 	 * prior to reset.
1779 	 */
1780 	if (reset_vf_tx)
1781 		ice_print_vf_tx_mdd_event(vf);
1782 
1783 	if (reset_vf_rx)
1784 		ice_print_vf_rx_mdd_event(vf);
1785 
1786 	dev_info(dev, "PF-to-VF reset on PF %d VF %d due to MDD event\n",
1787 		 pf->hw.pf_id, vf->vf_id);
1788 	ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1789 }
1790 
1791 /**
1792  * ice_handle_mdd_event - handle malicious driver detect event
1793  * @pf: pointer to the PF structure
1794  *
1795  * Called from service task. OICR interrupt handler indicates MDD event.
1796  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1797  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1798  * disable the queue, the PF can be configured to reset the VF using ethtool
1799  * private flag mdd-auto-reset-vf.
1800  */
1801 static void ice_handle_mdd_event(struct ice_pf *pf)
1802 {
1803 	struct device *dev = ice_pf_to_dev(pf);
1804 	struct ice_hw *hw = &pf->hw;
1805 	struct ice_vf *vf;
1806 	unsigned int bkt;
1807 	u32 reg;
1808 
1809 	if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1810 		/* Since the VF MDD event logging is rate limited, check if
1811 		 * there are pending MDD events.
1812 		 */
1813 		ice_print_vfs_mdd_events(pf);
1814 		return;
1815 	}
1816 
1817 	/* find what triggered an MDD event */
1818 	reg = rd32(hw, GL_MDET_TX_PQM);
1819 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1820 		u8 pf_num = FIELD_GET(GL_MDET_TX_PQM_PF_NUM_M, reg);
1821 		u16 vf_num = FIELD_GET(GL_MDET_TX_PQM_VF_NUM_M, reg);
1822 		u8 event = FIELD_GET(GL_MDET_TX_PQM_MAL_TYPE_M, reg);
1823 		u16 queue = FIELD_GET(GL_MDET_TX_PQM_QNUM_M, reg);
1824 
1825 		if (netif_msg_tx_err(pf))
1826 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1827 				 event, queue, pf_num, vf_num);
1828 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1829 	}
1830 
1831 	reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
1832 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1833 		u8 pf_num = FIELD_GET(GL_MDET_TX_TCLAN_PF_NUM_M, reg);
1834 		u16 vf_num = FIELD_GET(GL_MDET_TX_TCLAN_VF_NUM_M, reg);
1835 		u8 event = FIELD_GET(GL_MDET_TX_TCLAN_MAL_TYPE_M, reg);
1836 		u16 queue = FIELD_GET(GL_MDET_TX_TCLAN_QNUM_M, reg);
1837 
1838 		if (netif_msg_tx_err(pf))
1839 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1840 				 event, queue, pf_num, vf_num);
1841 		wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
1842 	}
1843 
1844 	reg = rd32(hw, GL_MDET_RX);
1845 	if (reg & GL_MDET_RX_VALID_M) {
1846 		u8 pf_num = FIELD_GET(GL_MDET_RX_PF_NUM_M, reg);
1847 		u16 vf_num = FIELD_GET(GL_MDET_RX_VF_NUM_M, reg);
1848 		u8 event = FIELD_GET(GL_MDET_RX_MAL_TYPE_M, reg);
1849 		u16 queue = FIELD_GET(GL_MDET_RX_QNUM_M, reg);
1850 
1851 		if (netif_msg_rx_err(pf))
1852 			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1853 				 event, queue, pf_num, vf_num);
1854 		wr32(hw, GL_MDET_RX, 0xffffffff);
1855 	}
1856 
1857 	/* check to see if this PF caused an MDD event */
1858 	reg = rd32(hw, PF_MDET_TX_PQM);
1859 	if (reg & PF_MDET_TX_PQM_VALID_M) {
1860 		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1861 		if (netif_msg_tx_err(pf))
1862 			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1863 	}
1864 
1865 	reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
1866 	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1867 		wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
1868 		if (netif_msg_tx_err(pf))
1869 			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1870 	}
1871 
1872 	reg = rd32(hw, PF_MDET_RX);
1873 	if (reg & PF_MDET_RX_VALID_M) {
1874 		wr32(hw, PF_MDET_RX, 0xFFFF);
1875 		if (netif_msg_rx_err(pf))
1876 			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1877 	}
1878 
1879 	/* Check to see if one of the VFs caused an MDD event, and then
1880 	 * increment counters and set print pending
1881 	 */
1882 	mutex_lock(&pf->vfs.table_lock);
1883 	ice_for_each_vf(pf, bkt, vf) {
1884 		bool reset_vf_tx = false, reset_vf_rx = false;
1885 
1886 		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1887 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1888 			wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1889 			vf->mdd_tx_events.count++;
1890 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1891 			if (netif_msg_tx_err(pf))
1892 				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1893 					 vf->vf_id);
1894 
1895 			reset_vf_tx = true;
1896 		}
1897 
1898 		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1899 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1900 			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1901 			vf->mdd_tx_events.count++;
1902 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1903 			if (netif_msg_tx_err(pf))
1904 				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1905 					 vf->vf_id);
1906 
1907 			reset_vf_tx = true;
1908 		}
1909 
1910 		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1911 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1912 			wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1913 			vf->mdd_tx_events.count++;
1914 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1915 			if (netif_msg_tx_err(pf))
1916 				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1917 					 vf->vf_id);
1918 
1919 			reset_vf_tx = true;
1920 		}
1921 
1922 		reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1923 		if (reg & VP_MDET_RX_VALID_M) {
1924 			wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1925 			vf->mdd_rx_events.count++;
1926 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1927 			if (netif_msg_rx_err(pf))
1928 				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1929 					 vf->vf_id);
1930 
1931 			reset_vf_rx = true;
1932 		}
1933 
1934 		if (reset_vf_tx || reset_vf_rx)
1935 			ice_mdd_maybe_reset_vf(pf, vf, reset_vf_tx,
1936 					       reset_vf_rx);
1937 	}
1938 	mutex_unlock(&pf->vfs.table_lock);
1939 
1940 	ice_print_vfs_mdd_events(pf);
1941 }
1942 
1943 /**
1944  * ice_force_phys_link_state - Force the physical link state
1945  * @vsi: VSI to force the physical link state to up/down
1946  * @link_up: true/false indicates to set the physical link to up/down
1947  *
1948  * Force the physical link state by getting the current PHY capabilities from
1949  * hardware and setting the PHY config based on the determined capabilities. If
1950  * link changes a link event will be triggered because both the Enable Automatic
1951  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1952  *
1953  * Returns 0 on success, negative on failure
1954  */
1955 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1956 {
1957 	struct ice_aqc_get_phy_caps_data *pcaps;
1958 	struct ice_aqc_set_phy_cfg_data *cfg;
1959 	struct ice_port_info *pi;
1960 	struct device *dev;
1961 	int retcode;
1962 
1963 	if (!vsi || !vsi->port_info || !vsi->back)
1964 		return -EINVAL;
1965 	if (vsi->type != ICE_VSI_PF)
1966 		return 0;
1967 
1968 	dev = ice_pf_to_dev(vsi->back);
1969 
1970 	pi = vsi->port_info;
1971 
1972 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1973 	if (!pcaps)
1974 		return -ENOMEM;
1975 
1976 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1977 				      NULL);
1978 	if (retcode) {
1979 		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1980 			vsi->vsi_num, retcode);
1981 		retcode = -EIO;
1982 		goto out;
1983 	}
1984 
1985 	/* No change in link */
1986 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1987 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1988 		goto out;
1989 
1990 	/* Use the current user PHY configuration. The current user PHY
1991 	 * configuration is initialized during probe from PHY capabilities
1992 	 * software mode, and updated on set PHY configuration.
1993 	 */
1994 	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1995 	if (!cfg) {
1996 		retcode = -ENOMEM;
1997 		goto out;
1998 	}
1999 
2000 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2001 	if (link_up)
2002 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
2003 	else
2004 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
2005 
2006 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
2007 	if (retcode) {
2008 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2009 			vsi->vsi_num, retcode);
2010 		retcode = -EIO;
2011 	}
2012 
2013 	kfree(cfg);
2014 out:
2015 	kfree(pcaps);
2016 	return retcode;
2017 }
2018 
2019 /**
2020  * ice_init_nvm_phy_type - Initialize the NVM PHY type
2021  * @pi: port info structure
2022  *
2023  * Initialize nvm_phy_type_[low|high] for link lenient mode support
2024  */
2025 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
2026 {
2027 	struct ice_aqc_get_phy_caps_data *pcaps;
2028 	struct ice_pf *pf = pi->hw->back;
2029 	int err;
2030 
2031 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2032 	if (!pcaps)
2033 		return -ENOMEM;
2034 
2035 	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
2036 				  pcaps, NULL);
2037 
2038 	if (err) {
2039 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2040 		goto out;
2041 	}
2042 
2043 	pf->nvm_phy_type_hi = pcaps->phy_type_high;
2044 	pf->nvm_phy_type_lo = pcaps->phy_type_low;
2045 
2046 out:
2047 	kfree(pcaps);
2048 	return err;
2049 }
2050 
2051 /**
2052  * ice_init_link_dflt_override - Initialize link default override
2053  * @pi: port info structure
2054  *
2055  * Initialize link default override and PHY total port shutdown during probe
2056  */
2057 static void ice_init_link_dflt_override(struct ice_port_info *pi)
2058 {
2059 	struct ice_link_default_override_tlv *ldo;
2060 	struct ice_pf *pf = pi->hw->back;
2061 
2062 	ldo = &pf->link_dflt_override;
2063 	if (ice_get_link_default_override(ldo, pi))
2064 		return;
2065 
2066 	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2067 		return;
2068 
2069 	/* Enable Total Port Shutdown (override/replace link-down-on-close
2070 	 * ethtool private flag) for ports with Port Disable bit set.
2071 	 */
2072 	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2073 	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
2074 }
2075 
2076 /**
2077  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2078  * @pi: port info structure
2079  *
2080  * If default override is enabled, initialize the user PHY cfg speed and FEC
2081  * settings using the default override mask from the NVM.
2082  *
2083  * The PHY should only be configured with the default override settings the
2084  * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2085  * is used to indicate that the user PHY cfg default override is initialized
2086  * and the PHY has not been configured with the default override settings. The
2087  * state is set here, and cleared in ice_configure_phy the first time the PHY is
2088  * configured.
2089  *
2090  * This function should be called only if the FW doesn't support default
2091  * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2092  */
2093 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2094 {
2095 	struct ice_link_default_override_tlv *ldo;
2096 	struct ice_aqc_set_phy_cfg_data *cfg;
2097 	struct ice_phy_info *phy = &pi->phy;
2098 	struct ice_pf *pf = pi->hw->back;
2099 
2100 	ldo = &pf->link_dflt_override;
2101 
2102 	/* If link default override is enabled, use to mask NVM PHY capabilities
2103 	 * for speed and FEC default configuration.
2104 	 */
2105 	cfg = &phy->curr_user_phy_cfg;
2106 
2107 	if (ldo->phy_type_low || ldo->phy_type_high) {
2108 		cfg->phy_type_low = pf->nvm_phy_type_lo &
2109 				    cpu_to_le64(ldo->phy_type_low);
2110 		cfg->phy_type_high = pf->nvm_phy_type_hi &
2111 				     cpu_to_le64(ldo->phy_type_high);
2112 	}
2113 	cfg->link_fec_opt = ldo->fec_options;
2114 	phy->curr_user_fec_req = ICE_FEC_AUTO;
2115 
2116 	set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2117 }
2118 
2119 /**
2120  * ice_init_phy_user_cfg - Initialize the PHY user configuration
2121  * @pi: port info structure
2122  *
2123  * Initialize the current user PHY configuration, speed, FEC, and FC requested
2124  * mode to default. The PHY defaults are from get PHY capabilities topology
2125  * with media so call when media is first available. An error is returned if
2126  * called when media is not available. The PHY initialization completed state is
2127  * set here.
2128  *
2129  * These configurations are used when setting PHY
2130  * configuration. The user PHY configuration is updated on set PHY
2131  * configuration. Returns 0 on success, negative on failure
2132  */
2133 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2134 {
2135 	struct ice_aqc_get_phy_caps_data *pcaps;
2136 	struct ice_phy_info *phy = &pi->phy;
2137 	struct ice_pf *pf = pi->hw->back;
2138 	int err;
2139 
2140 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2141 		return -EIO;
2142 
2143 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2144 	if (!pcaps)
2145 		return -ENOMEM;
2146 
2147 	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2148 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2149 					  pcaps, NULL);
2150 	else
2151 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2152 					  pcaps, NULL);
2153 	if (err) {
2154 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2155 		goto err_out;
2156 	}
2157 
2158 	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2159 
2160 	/* check if lenient mode is supported and enabled */
2161 	if (ice_fw_supports_link_override(pi->hw) &&
2162 	    !(pcaps->module_compliance_enforcement &
2163 	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2164 		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2165 
2166 		/* if the FW supports default PHY configuration mode, then the driver
2167 		 * does not have to apply link override settings. If not,
2168 		 * initialize user PHY configuration with link override values
2169 		 */
2170 		if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2171 		    (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2172 			ice_init_phy_cfg_dflt_override(pi);
2173 			goto out;
2174 		}
2175 	}
2176 
2177 	/* if link default override is not enabled, set user flow control and
2178 	 * FEC settings based on what get_phy_caps returned
2179 	 */
2180 	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2181 						      pcaps->link_fec_options);
2182 	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2183 
2184 out:
2185 	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2186 	set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2187 err_out:
2188 	kfree(pcaps);
2189 	return err;
2190 }
2191 
2192 /**
2193  * ice_configure_phy - configure PHY
2194  * @vsi: VSI of PHY
2195  *
2196  * Set the PHY configuration. If the current PHY configuration is the same as
2197  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2198  * configure the based get PHY capabilities for topology with media.
2199  */
2200 static int ice_configure_phy(struct ice_vsi *vsi)
2201 {
2202 	struct device *dev = ice_pf_to_dev(vsi->back);
2203 	struct ice_port_info *pi = vsi->port_info;
2204 	struct ice_aqc_get_phy_caps_data *pcaps;
2205 	struct ice_aqc_set_phy_cfg_data *cfg;
2206 	struct ice_phy_info *phy = &pi->phy;
2207 	struct ice_pf *pf = vsi->back;
2208 	int err;
2209 
2210 	/* Ensure we have media as we cannot configure a medialess port */
2211 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2212 		return -ENOMEDIUM;
2213 
2214 	ice_print_topo_conflict(vsi);
2215 
2216 	if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2217 	    phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2218 		return -EPERM;
2219 
2220 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2221 		return ice_force_phys_link_state(vsi, true);
2222 
2223 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2224 	if (!pcaps)
2225 		return -ENOMEM;
2226 
2227 	/* Get current PHY config */
2228 	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2229 				  NULL);
2230 	if (err) {
2231 		dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2232 			vsi->vsi_num, err);
2233 		goto done;
2234 	}
2235 
2236 	/* If PHY enable link is configured and configuration has not changed,
2237 	 * there's nothing to do
2238 	 */
2239 	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2240 	    ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2241 		goto done;
2242 
2243 	/* Use PHY topology as baseline for configuration */
2244 	memset(pcaps, 0, sizeof(*pcaps));
2245 	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2246 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2247 					  pcaps, NULL);
2248 	else
2249 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2250 					  pcaps, NULL);
2251 	if (err) {
2252 		dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2253 			vsi->vsi_num, err);
2254 		goto done;
2255 	}
2256 
2257 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2258 	if (!cfg) {
2259 		err = -ENOMEM;
2260 		goto done;
2261 	}
2262 
2263 	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2264 
2265 	/* Speed - If default override pending, use curr_user_phy_cfg set in
2266 	 * ice_init_phy_user_cfg_ldo.
2267 	 */
2268 	if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2269 			       vsi->back->state)) {
2270 		cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2271 		cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2272 	} else {
2273 		u64 phy_low = 0, phy_high = 0;
2274 
2275 		ice_update_phy_type(&phy_low, &phy_high,
2276 				    pi->phy.curr_user_speed_req);
2277 		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2278 		cfg->phy_type_high = pcaps->phy_type_high &
2279 				     cpu_to_le64(phy_high);
2280 	}
2281 
2282 	/* Can't provide what was requested; use PHY capabilities */
2283 	if (!cfg->phy_type_low && !cfg->phy_type_high) {
2284 		cfg->phy_type_low = pcaps->phy_type_low;
2285 		cfg->phy_type_high = pcaps->phy_type_high;
2286 	}
2287 
2288 	/* FEC */
2289 	ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2290 
2291 	/* Can't provide what was requested; use PHY capabilities */
2292 	if (cfg->link_fec_opt !=
2293 	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
2294 		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2295 		cfg->link_fec_opt = pcaps->link_fec_options;
2296 	}
2297 
2298 	/* Flow Control - always supported; no need to check against
2299 	 * capabilities
2300 	 */
2301 	ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2302 
2303 	/* Enable link and link update */
2304 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2305 
2306 	err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2307 	if (err)
2308 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2309 			vsi->vsi_num, err);
2310 
2311 	kfree(cfg);
2312 done:
2313 	kfree(pcaps);
2314 	return err;
2315 }
2316 
2317 /**
2318  * ice_check_media_subtask - Check for media
2319  * @pf: pointer to PF struct
2320  *
2321  * If media is available, then initialize PHY user configuration if it is not
2322  * been, and configure the PHY if the interface is up.
2323  */
2324 static void ice_check_media_subtask(struct ice_pf *pf)
2325 {
2326 	struct ice_port_info *pi;
2327 	struct ice_vsi *vsi;
2328 	int err;
2329 
2330 	/* No need to check for media if it's already present */
2331 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2332 		return;
2333 
2334 	vsi = ice_get_main_vsi(pf);
2335 	if (!vsi)
2336 		return;
2337 
2338 	/* Refresh link info and check if media is present */
2339 	pi = vsi->port_info;
2340 	err = ice_update_link_info(pi);
2341 	if (err)
2342 		return;
2343 
2344 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2345 
2346 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2347 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2348 			ice_init_phy_user_cfg(pi);
2349 
2350 		/* PHY settings are reset on media insertion, reconfigure
2351 		 * PHY to preserve settings.
2352 		 */
2353 		if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2354 		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2355 			return;
2356 
2357 		err = ice_configure_phy(vsi);
2358 		if (!err)
2359 			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2360 
2361 		/* A Link Status Event will be generated; the event handler
2362 		 * will complete bringing the interface up
2363 		 */
2364 	}
2365 }
2366 
2367 /**
2368  * ice_service_task - manage and run subtasks
2369  * @work: pointer to work_struct contained by the PF struct
2370  */
2371 static void ice_service_task(struct work_struct *work)
2372 {
2373 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2374 	unsigned long start_time = jiffies;
2375 
2376 	/* subtasks */
2377 
2378 	/* process reset requests first */
2379 	ice_reset_subtask(pf);
2380 
2381 	/* bail if a reset/recovery cycle is pending or rebuild failed */
2382 	if (ice_is_reset_in_progress(pf->state) ||
2383 	    test_bit(ICE_SUSPENDED, pf->state) ||
2384 	    test_bit(ICE_NEEDS_RESTART, pf->state)) {
2385 		ice_service_task_complete(pf);
2386 		return;
2387 	}
2388 
2389 	if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2390 		struct iidc_event *event;
2391 
2392 		event = kzalloc(sizeof(*event), GFP_KERNEL);
2393 		if (event) {
2394 			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2395 			/* report the entire OICR value to AUX driver */
2396 			swap(event->reg, pf->oicr_err_reg);
2397 			ice_send_event_to_aux(pf, event);
2398 			kfree(event);
2399 		}
2400 	}
2401 
2402 	/* unplug aux dev per request, if an unplug request came in
2403 	 * while processing a plug request, this will handle it
2404 	 */
2405 	if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2406 		ice_unplug_aux_dev(pf);
2407 
2408 	/* Plug aux device per request */
2409 	if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2410 		ice_plug_aux_dev(pf);
2411 
2412 	if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2413 		struct iidc_event *event;
2414 
2415 		event = kzalloc(sizeof(*event), GFP_KERNEL);
2416 		if (event) {
2417 			set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2418 			ice_send_event_to_aux(pf, event);
2419 			kfree(event);
2420 		}
2421 	}
2422 
2423 	ice_clean_adminq_subtask(pf);
2424 	ice_check_media_subtask(pf);
2425 	ice_check_for_hang_subtask(pf);
2426 	ice_sync_fltr_subtask(pf);
2427 	ice_handle_mdd_event(pf);
2428 	ice_watchdog_subtask(pf);
2429 
2430 	if (ice_is_safe_mode(pf)) {
2431 		ice_service_task_complete(pf);
2432 		return;
2433 	}
2434 
2435 	ice_process_vflr_event(pf);
2436 	ice_clean_mailboxq_subtask(pf);
2437 	ice_clean_sbq_subtask(pf);
2438 	ice_sync_arfs_fltrs(pf);
2439 	ice_flush_fdir_ctx(pf);
2440 
2441 	/* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2442 	ice_service_task_complete(pf);
2443 
2444 	/* If the tasks have taken longer than one service timer period
2445 	 * or there is more work to be done, reset the service timer to
2446 	 * schedule the service task now.
2447 	 */
2448 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2449 	    test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2450 	    test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2451 	    test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2452 	    test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2453 	    test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2454 	    test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2455 		mod_timer(&pf->serv_tmr, jiffies);
2456 }
2457 
2458 /**
2459  * ice_set_ctrlq_len - helper function to set controlq length
2460  * @hw: pointer to the HW instance
2461  */
2462 static void ice_set_ctrlq_len(struct ice_hw *hw)
2463 {
2464 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2465 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2466 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2467 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2468 	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2469 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2470 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2471 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2472 	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2473 	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2474 	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2475 	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2476 }
2477 
2478 /**
2479  * ice_schedule_reset - schedule a reset
2480  * @pf: board private structure
2481  * @reset: reset being requested
2482  */
2483 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2484 {
2485 	struct device *dev = ice_pf_to_dev(pf);
2486 
2487 	/* bail out if earlier reset has failed */
2488 	if (test_bit(ICE_RESET_FAILED, pf->state)) {
2489 		dev_dbg(dev, "earlier reset has failed\n");
2490 		return -EIO;
2491 	}
2492 	/* bail if reset/recovery already in progress */
2493 	if (ice_is_reset_in_progress(pf->state)) {
2494 		dev_dbg(dev, "Reset already in progress\n");
2495 		return -EBUSY;
2496 	}
2497 
2498 	switch (reset) {
2499 	case ICE_RESET_PFR:
2500 		set_bit(ICE_PFR_REQ, pf->state);
2501 		break;
2502 	case ICE_RESET_CORER:
2503 		set_bit(ICE_CORER_REQ, pf->state);
2504 		break;
2505 	case ICE_RESET_GLOBR:
2506 		set_bit(ICE_GLOBR_REQ, pf->state);
2507 		break;
2508 	default:
2509 		return -EINVAL;
2510 	}
2511 
2512 	ice_service_task_schedule(pf);
2513 	return 0;
2514 }
2515 
2516 /**
2517  * ice_irq_affinity_notify - Callback for affinity changes
2518  * @notify: context as to what irq was changed
2519  * @mask: the new affinity mask
2520  *
2521  * This is a callback function used by the irq_set_affinity_notifier function
2522  * so that we may register to receive changes to the irq affinity masks.
2523  */
2524 static void
2525 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2526 			const cpumask_t *mask)
2527 {
2528 	struct ice_q_vector *q_vector =
2529 		container_of(notify, struct ice_q_vector, affinity_notify);
2530 
2531 	cpumask_copy(&q_vector->affinity_mask, mask);
2532 }
2533 
2534 /**
2535  * ice_irq_affinity_release - Callback for affinity notifier release
2536  * @ref: internal core kernel usage
2537  *
2538  * This is a callback function used by the irq_set_affinity_notifier function
2539  * to inform the current notification subscriber that they will no longer
2540  * receive notifications.
2541  */
2542 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2543 
2544 /**
2545  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2546  * @vsi: the VSI being configured
2547  */
2548 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2549 {
2550 	struct ice_hw *hw = &vsi->back->hw;
2551 	int i;
2552 
2553 	ice_for_each_q_vector(vsi, i)
2554 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2555 
2556 	ice_flush(hw);
2557 	return 0;
2558 }
2559 
2560 /**
2561  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2562  * @vsi: the VSI being configured
2563  * @basename: name for the vector
2564  */
2565 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2566 {
2567 	int q_vectors = vsi->num_q_vectors;
2568 	struct ice_pf *pf = vsi->back;
2569 	struct device *dev;
2570 	int rx_int_idx = 0;
2571 	int tx_int_idx = 0;
2572 	int vector, err;
2573 	int irq_num;
2574 
2575 	dev = ice_pf_to_dev(pf);
2576 	for (vector = 0; vector < q_vectors; vector++) {
2577 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2578 
2579 		irq_num = q_vector->irq.virq;
2580 
2581 		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2582 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2583 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2584 			tx_int_idx++;
2585 		} else if (q_vector->rx.rx_ring) {
2586 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2587 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2588 		} else if (q_vector->tx.tx_ring) {
2589 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2590 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2591 		} else {
2592 			/* skip this unused q_vector */
2593 			continue;
2594 		}
2595 		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2596 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2597 					       IRQF_SHARED, q_vector->name,
2598 					       q_vector);
2599 		else
2600 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2601 					       0, q_vector->name, q_vector);
2602 		if (err) {
2603 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2604 				   err);
2605 			goto free_q_irqs;
2606 		}
2607 
2608 		/* register for affinity change notifications */
2609 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2610 			struct irq_affinity_notify *affinity_notify;
2611 
2612 			affinity_notify = &q_vector->affinity_notify;
2613 			affinity_notify->notify = ice_irq_affinity_notify;
2614 			affinity_notify->release = ice_irq_affinity_release;
2615 			irq_set_affinity_notifier(irq_num, affinity_notify);
2616 		}
2617 
2618 		/* assign the mask for this irq */
2619 		irq_update_affinity_hint(irq_num, &q_vector->affinity_mask);
2620 	}
2621 
2622 	err = ice_set_cpu_rx_rmap(vsi);
2623 	if (err) {
2624 		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2625 			   vsi->vsi_num, ERR_PTR(err));
2626 		goto free_q_irqs;
2627 	}
2628 
2629 	vsi->irqs_ready = true;
2630 	return 0;
2631 
2632 free_q_irqs:
2633 	while (vector--) {
2634 		irq_num = vsi->q_vectors[vector]->irq.virq;
2635 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2636 			irq_set_affinity_notifier(irq_num, NULL);
2637 		irq_update_affinity_hint(irq_num, NULL);
2638 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2639 	}
2640 	return err;
2641 }
2642 
2643 /**
2644  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2645  * @vsi: VSI to setup Tx rings used by XDP
2646  *
2647  * Return 0 on success and negative value on error
2648  */
2649 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2650 {
2651 	struct device *dev = ice_pf_to_dev(vsi->back);
2652 	struct ice_tx_desc *tx_desc;
2653 	int i, j;
2654 
2655 	ice_for_each_xdp_txq(vsi, i) {
2656 		u16 xdp_q_idx = vsi->alloc_txq + i;
2657 		struct ice_ring_stats *ring_stats;
2658 		struct ice_tx_ring *xdp_ring;
2659 
2660 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2661 		if (!xdp_ring)
2662 			goto free_xdp_rings;
2663 
2664 		ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2665 		if (!ring_stats) {
2666 			ice_free_tx_ring(xdp_ring);
2667 			goto free_xdp_rings;
2668 		}
2669 
2670 		xdp_ring->ring_stats = ring_stats;
2671 		xdp_ring->q_index = xdp_q_idx;
2672 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2673 		xdp_ring->vsi = vsi;
2674 		xdp_ring->netdev = NULL;
2675 		xdp_ring->dev = dev;
2676 		xdp_ring->count = vsi->num_tx_desc;
2677 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2678 		if (ice_setup_tx_ring(xdp_ring))
2679 			goto free_xdp_rings;
2680 		ice_set_ring_xdp(xdp_ring);
2681 		spin_lock_init(&xdp_ring->tx_lock);
2682 		for (j = 0; j < xdp_ring->count; j++) {
2683 			tx_desc = ICE_TX_DESC(xdp_ring, j);
2684 			tx_desc->cmd_type_offset_bsz = 0;
2685 		}
2686 	}
2687 
2688 	return 0;
2689 
2690 free_xdp_rings:
2691 	for (; i >= 0; i--) {
2692 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2693 			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2694 			vsi->xdp_rings[i]->ring_stats = NULL;
2695 			ice_free_tx_ring(vsi->xdp_rings[i]);
2696 		}
2697 	}
2698 	return -ENOMEM;
2699 }
2700 
2701 /**
2702  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2703  * @vsi: VSI to set the bpf prog on
2704  * @prog: the bpf prog pointer
2705  */
2706 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2707 {
2708 	struct bpf_prog *old_prog;
2709 	int i;
2710 
2711 	old_prog = xchg(&vsi->xdp_prog, prog);
2712 	ice_for_each_rxq(vsi, i)
2713 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2714 
2715 	if (old_prog)
2716 		bpf_prog_put(old_prog);
2717 }
2718 
2719 static struct ice_tx_ring *ice_xdp_ring_from_qid(struct ice_vsi *vsi, int qid)
2720 {
2721 	struct ice_q_vector *q_vector;
2722 	struct ice_tx_ring *ring;
2723 
2724 	if (static_key_enabled(&ice_xdp_locking_key))
2725 		return vsi->xdp_rings[qid % vsi->num_xdp_txq];
2726 
2727 	q_vector = vsi->rx_rings[qid]->q_vector;
2728 	ice_for_each_tx_ring(ring, q_vector->tx)
2729 		if (ice_ring_is_xdp(ring))
2730 			return ring;
2731 
2732 	return NULL;
2733 }
2734 
2735 /**
2736  * ice_map_xdp_rings - Map XDP rings to interrupt vectors
2737  * @vsi: the VSI with XDP rings being configured
2738  *
2739  * Map XDP rings to interrupt vectors and perform the configuration steps
2740  * dependent on the mapping.
2741  */
2742 void ice_map_xdp_rings(struct ice_vsi *vsi)
2743 {
2744 	int xdp_rings_rem = vsi->num_xdp_txq;
2745 	int v_idx, q_idx;
2746 
2747 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2748 	ice_for_each_q_vector(vsi, v_idx) {
2749 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2750 		int xdp_rings_per_v, q_id, q_base;
2751 
2752 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2753 					       vsi->num_q_vectors - v_idx);
2754 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2755 
2756 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2757 			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2758 
2759 			xdp_ring->q_vector = q_vector;
2760 			xdp_ring->next = q_vector->tx.tx_ring;
2761 			q_vector->tx.tx_ring = xdp_ring;
2762 		}
2763 		xdp_rings_rem -= xdp_rings_per_v;
2764 	}
2765 
2766 	ice_for_each_rxq(vsi, q_idx) {
2767 		vsi->rx_rings[q_idx]->xdp_ring = ice_xdp_ring_from_qid(vsi,
2768 								       q_idx);
2769 		ice_tx_xsk_pool(vsi, q_idx);
2770 	}
2771 }
2772 
2773 /**
2774  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2775  * @vsi: VSI to bring up Tx rings used by XDP
2776  * @prog: bpf program that will be assigned to VSI
2777  * @cfg_type: create from scratch or restore the existing configuration
2778  *
2779  * Return 0 on success and negative value on error
2780  */
2781 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
2782 			  enum ice_xdp_cfg cfg_type)
2783 {
2784 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2785 	struct ice_pf *pf = vsi->back;
2786 	struct ice_qs_cfg xdp_qs_cfg = {
2787 		.qs_mutex = &pf->avail_q_mutex,
2788 		.pf_map = pf->avail_txqs,
2789 		.pf_map_size = pf->max_pf_txqs,
2790 		.q_count = vsi->num_xdp_txq,
2791 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2792 		.vsi_map = vsi->txq_map,
2793 		.vsi_map_offset = vsi->alloc_txq,
2794 		.mapping_mode = ICE_VSI_MAP_CONTIG
2795 	};
2796 	struct device *dev;
2797 	int status, i;
2798 
2799 	dev = ice_pf_to_dev(pf);
2800 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2801 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2802 	if (!vsi->xdp_rings)
2803 		return -ENOMEM;
2804 
2805 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2806 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2807 		goto err_map_xdp;
2808 
2809 	if (static_key_enabled(&ice_xdp_locking_key))
2810 		netdev_warn(vsi->netdev,
2811 			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2812 
2813 	if (ice_xdp_alloc_setup_rings(vsi))
2814 		goto clear_xdp_rings;
2815 
2816 	/* omit the scheduler update if in reset path; XDP queues will be
2817 	 * taken into account at the end of ice_vsi_rebuild, where
2818 	 * ice_cfg_vsi_lan is being called
2819 	 */
2820 	if (cfg_type == ICE_XDP_CFG_PART)
2821 		return 0;
2822 
2823 	ice_map_xdp_rings(vsi);
2824 
2825 	/* tell the Tx scheduler that right now we have
2826 	 * additional queues
2827 	 */
2828 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2829 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2830 
2831 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2832 				 max_txqs);
2833 	if (status) {
2834 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2835 			status);
2836 		goto clear_xdp_rings;
2837 	}
2838 
2839 	/* assign the prog only when it's not already present on VSI;
2840 	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2841 	 * VSI rebuild that happens under ethtool -L can expose us to
2842 	 * the bpf_prog refcount issues as we would be swapping same
2843 	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2844 	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2845 	 * this is not harmful as dev_xdp_install bumps the refcount
2846 	 * before calling the op exposed by the driver;
2847 	 */
2848 	if (!ice_is_xdp_ena_vsi(vsi))
2849 		ice_vsi_assign_bpf_prog(vsi, prog);
2850 
2851 	return 0;
2852 clear_xdp_rings:
2853 	ice_for_each_xdp_txq(vsi, i)
2854 		if (vsi->xdp_rings[i]) {
2855 			kfree_rcu(vsi->xdp_rings[i], rcu);
2856 			vsi->xdp_rings[i] = NULL;
2857 		}
2858 
2859 err_map_xdp:
2860 	mutex_lock(&pf->avail_q_mutex);
2861 	ice_for_each_xdp_txq(vsi, i) {
2862 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2863 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2864 	}
2865 	mutex_unlock(&pf->avail_q_mutex);
2866 
2867 	devm_kfree(dev, vsi->xdp_rings);
2868 	return -ENOMEM;
2869 }
2870 
2871 /**
2872  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2873  * @vsi: VSI to remove XDP rings
2874  * @cfg_type: disable XDP permanently or allow it to be restored later
2875  *
2876  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2877  * resources
2878  */
2879 int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
2880 {
2881 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2882 	struct ice_pf *pf = vsi->back;
2883 	int i, v_idx;
2884 
2885 	/* q_vectors are freed in reset path so there's no point in detaching
2886 	 * rings
2887 	 */
2888 	if (cfg_type == ICE_XDP_CFG_PART)
2889 		goto free_qmap;
2890 
2891 	ice_for_each_q_vector(vsi, v_idx) {
2892 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2893 		struct ice_tx_ring *ring;
2894 
2895 		ice_for_each_tx_ring(ring, q_vector->tx)
2896 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2897 				break;
2898 
2899 		/* restore the value of last node prior to XDP setup */
2900 		q_vector->tx.tx_ring = ring;
2901 	}
2902 
2903 free_qmap:
2904 	mutex_lock(&pf->avail_q_mutex);
2905 	ice_for_each_xdp_txq(vsi, i) {
2906 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2907 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2908 	}
2909 	mutex_unlock(&pf->avail_q_mutex);
2910 
2911 	ice_for_each_xdp_txq(vsi, i)
2912 		if (vsi->xdp_rings[i]) {
2913 			if (vsi->xdp_rings[i]->desc) {
2914 				synchronize_rcu();
2915 				ice_free_tx_ring(vsi->xdp_rings[i]);
2916 			}
2917 			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2918 			vsi->xdp_rings[i]->ring_stats = NULL;
2919 			kfree_rcu(vsi->xdp_rings[i], rcu);
2920 			vsi->xdp_rings[i] = NULL;
2921 		}
2922 
2923 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2924 	vsi->xdp_rings = NULL;
2925 
2926 	if (static_key_enabled(&ice_xdp_locking_key))
2927 		static_branch_dec(&ice_xdp_locking_key);
2928 
2929 	if (cfg_type == ICE_XDP_CFG_PART)
2930 		return 0;
2931 
2932 	ice_vsi_assign_bpf_prog(vsi, NULL);
2933 
2934 	/* notify Tx scheduler that we destroyed XDP queues and bring
2935 	 * back the old number of child nodes
2936 	 */
2937 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2938 		max_txqs[i] = vsi->num_txq;
2939 
2940 	/* change number of XDP Tx queues to 0 */
2941 	vsi->num_xdp_txq = 0;
2942 
2943 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2944 			       max_txqs);
2945 }
2946 
2947 /**
2948  * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2949  * @vsi: VSI to schedule napi on
2950  */
2951 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2952 {
2953 	int i;
2954 
2955 	ice_for_each_rxq(vsi, i) {
2956 		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2957 
2958 		if (READ_ONCE(rx_ring->xsk_pool))
2959 			napi_schedule(&rx_ring->q_vector->napi);
2960 	}
2961 }
2962 
2963 /**
2964  * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2965  * @vsi: VSI to determine the count of XDP Tx qs
2966  *
2967  * returns 0 if Tx qs count is higher than at least half of CPU count,
2968  * -ENOMEM otherwise
2969  */
2970 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2971 {
2972 	u16 avail = ice_get_avail_txq_count(vsi->back);
2973 	u16 cpus = num_possible_cpus();
2974 
2975 	if (avail < cpus / 2)
2976 		return -ENOMEM;
2977 
2978 	if (vsi->type == ICE_VSI_SF)
2979 		avail = vsi->alloc_txq;
2980 
2981 	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2982 
2983 	if (vsi->num_xdp_txq < cpus)
2984 		static_branch_inc(&ice_xdp_locking_key);
2985 
2986 	return 0;
2987 }
2988 
2989 /**
2990  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2991  * @vsi: Pointer to VSI structure
2992  */
2993 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2994 {
2995 	if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2996 		return ICE_RXBUF_1664;
2997 	else
2998 		return ICE_RXBUF_3072;
2999 }
3000 
3001 /**
3002  * ice_xdp_setup_prog - Add or remove XDP eBPF program
3003  * @vsi: VSI to setup XDP for
3004  * @prog: XDP program
3005  * @extack: netlink extended ack
3006  */
3007 static int
3008 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
3009 		   struct netlink_ext_ack *extack)
3010 {
3011 	unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
3012 	int ret = 0, xdp_ring_err = 0;
3013 	bool if_running;
3014 
3015 	if (prog && !prog->aux->xdp_has_frags) {
3016 		if (frame_size > ice_max_xdp_frame_size(vsi)) {
3017 			NL_SET_ERR_MSG_MOD(extack,
3018 					   "MTU is too large for linear frames and XDP prog does not support frags");
3019 			return -EOPNOTSUPP;
3020 		}
3021 	}
3022 
3023 	/* hot swap progs and avoid toggling link */
3024 	if (ice_is_xdp_ena_vsi(vsi) == !!prog ||
3025 	    test_bit(ICE_VSI_REBUILD_PENDING, vsi->state)) {
3026 		ice_vsi_assign_bpf_prog(vsi, prog);
3027 		return 0;
3028 	}
3029 
3030 	if_running = netif_running(vsi->netdev) &&
3031 		     !test_and_set_bit(ICE_VSI_DOWN, vsi->state);
3032 
3033 	/* need to stop netdev while setting up the program for Rx rings */
3034 	if (if_running) {
3035 		ret = ice_down(vsi);
3036 		if (ret) {
3037 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
3038 			return ret;
3039 		}
3040 	}
3041 
3042 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
3043 		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
3044 		if (xdp_ring_err) {
3045 			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
3046 		} else {
3047 			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
3048 							     ICE_XDP_CFG_FULL);
3049 			if (xdp_ring_err)
3050 				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
3051 		}
3052 		xdp_features_set_redirect_target(vsi->netdev, true);
3053 		/* reallocate Rx queues that are used for zero-copy */
3054 		xdp_ring_err = ice_realloc_zc_buf(vsi, true);
3055 		if (xdp_ring_err)
3056 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
3057 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
3058 		xdp_features_clear_redirect_target(vsi->netdev);
3059 		xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
3060 		if (xdp_ring_err)
3061 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
3062 		/* reallocate Rx queues that were used for zero-copy */
3063 		xdp_ring_err = ice_realloc_zc_buf(vsi, false);
3064 		if (xdp_ring_err)
3065 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
3066 	}
3067 
3068 	if (if_running)
3069 		ret = ice_up(vsi);
3070 
3071 	if (!ret && prog)
3072 		ice_vsi_rx_napi_schedule(vsi);
3073 
3074 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
3075 }
3076 
3077 /**
3078  * ice_xdp_safe_mode - XDP handler for safe mode
3079  * @dev: netdevice
3080  * @xdp: XDP command
3081  */
3082 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
3083 			     struct netdev_bpf *xdp)
3084 {
3085 	NL_SET_ERR_MSG_MOD(xdp->extack,
3086 			   "Please provide working DDP firmware package in order to use XDP\n"
3087 			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3088 	return -EOPNOTSUPP;
3089 }
3090 
3091 /**
3092  * ice_xdp - implements XDP handler
3093  * @dev: netdevice
3094  * @xdp: XDP command
3095  */
3096 int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3097 {
3098 	struct ice_netdev_priv *np = netdev_priv(dev);
3099 	struct ice_vsi *vsi = np->vsi;
3100 	int ret;
3101 
3102 	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_SF) {
3103 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF or SF VSI");
3104 		return -EINVAL;
3105 	}
3106 
3107 	mutex_lock(&vsi->xdp_state_lock);
3108 
3109 	switch (xdp->command) {
3110 	case XDP_SETUP_PROG:
3111 		ret = ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
3112 		break;
3113 	case XDP_SETUP_XSK_POOL:
3114 		ret = ice_xsk_pool_setup(vsi, xdp->xsk.pool, xdp->xsk.queue_id);
3115 		break;
3116 	default:
3117 		ret = -EINVAL;
3118 	}
3119 
3120 	mutex_unlock(&vsi->xdp_state_lock);
3121 	return ret;
3122 }
3123 
3124 /**
3125  * ice_ena_misc_vector - enable the non-queue interrupts
3126  * @pf: board private structure
3127  */
3128 static void ice_ena_misc_vector(struct ice_pf *pf)
3129 {
3130 	struct ice_hw *hw = &pf->hw;
3131 	u32 pf_intr_start_offset;
3132 	u32 val;
3133 
3134 	/* Disable anti-spoof detection interrupt to prevent spurious event
3135 	 * interrupts during a function reset. Anti-spoof functionally is
3136 	 * still supported.
3137 	 */
3138 	val = rd32(hw, GL_MDCK_TX_TDPU);
3139 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3140 	wr32(hw, GL_MDCK_TX_TDPU, val);
3141 
3142 	/* clear things first */
3143 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
3144 	rd32(hw, PFINT_OICR);		/* read to clear */
3145 
3146 	val = (PFINT_OICR_ECC_ERR_M |
3147 	       PFINT_OICR_MAL_DETECT_M |
3148 	       PFINT_OICR_GRST_M |
3149 	       PFINT_OICR_PCI_EXCEPTION_M |
3150 	       PFINT_OICR_VFLR_M |
3151 	       PFINT_OICR_HMC_ERR_M |
3152 	       PFINT_OICR_PE_PUSH_M |
3153 	       PFINT_OICR_PE_CRITERR_M);
3154 
3155 	wr32(hw, PFINT_OICR_ENA, val);
3156 
3157 	/* SW_ITR_IDX = 0, but don't change INTENA */
3158 	wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
3159 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3160 
3161 	if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3162 		return;
3163 	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3164 	wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3165 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3166 }
3167 
3168 /**
3169  * ice_ll_ts_intr - ll_ts interrupt handler
3170  * @irq: interrupt number
3171  * @data: pointer to a q_vector
3172  */
3173 static irqreturn_t ice_ll_ts_intr(int __always_unused irq, void *data)
3174 {
3175 	struct ice_pf *pf = data;
3176 	u32 pf_intr_start_offset;
3177 	struct ice_ptp_tx *tx;
3178 	unsigned long flags;
3179 	struct ice_hw *hw;
3180 	u32 val;
3181 	u8 idx;
3182 
3183 	hw = &pf->hw;
3184 	tx = &pf->ptp.port.tx;
3185 	spin_lock_irqsave(&tx->lock, flags);
3186 	ice_ptp_complete_tx_single_tstamp(tx);
3187 
3188 	idx = find_next_bit_wrap(tx->in_use, tx->len,
3189 				 tx->last_ll_ts_idx_read + 1);
3190 	if (idx != tx->len)
3191 		ice_ptp_req_tx_single_tstamp(tx, idx);
3192 	spin_unlock_irqrestore(&tx->lock, flags);
3193 
3194 	val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3195 	      (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
3196 	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3197 	wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3198 	     val);
3199 
3200 	return IRQ_HANDLED;
3201 }
3202 
3203 /**
3204  * ice_misc_intr - misc interrupt handler
3205  * @irq: interrupt number
3206  * @data: pointer to a q_vector
3207  */
3208 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3209 {
3210 	struct ice_pf *pf = (struct ice_pf *)data;
3211 	irqreturn_t ret = IRQ_HANDLED;
3212 	struct ice_hw *hw = &pf->hw;
3213 	struct device *dev;
3214 	u32 oicr, ena_mask;
3215 
3216 	dev = ice_pf_to_dev(pf);
3217 	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3218 	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3219 	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3220 
3221 	oicr = rd32(hw, PFINT_OICR);
3222 	ena_mask = rd32(hw, PFINT_OICR_ENA);
3223 
3224 	if (oicr & PFINT_OICR_SWINT_M) {
3225 		ena_mask &= ~PFINT_OICR_SWINT_M;
3226 		pf->sw_int_count++;
3227 	}
3228 
3229 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3230 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3231 		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3232 	}
3233 	if (oicr & PFINT_OICR_VFLR_M) {
3234 		/* disable any further VFLR event notifications */
3235 		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3236 			u32 reg = rd32(hw, PFINT_OICR_ENA);
3237 
3238 			reg &= ~PFINT_OICR_VFLR_M;
3239 			wr32(hw, PFINT_OICR_ENA, reg);
3240 		} else {
3241 			ena_mask &= ~PFINT_OICR_VFLR_M;
3242 			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3243 		}
3244 	}
3245 
3246 	if (oicr & PFINT_OICR_GRST_M) {
3247 		u32 reset;
3248 
3249 		/* we have a reset warning */
3250 		ena_mask &= ~PFINT_OICR_GRST_M;
3251 		reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
3252 				  rd32(hw, GLGEN_RSTAT));
3253 
3254 		if (reset == ICE_RESET_CORER)
3255 			pf->corer_count++;
3256 		else if (reset == ICE_RESET_GLOBR)
3257 			pf->globr_count++;
3258 		else if (reset == ICE_RESET_EMPR)
3259 			pf->empr_count++;
3260 		else
3261 			dev_dbg(dev, "Invalid reset type %d\n", reset);
3262 
3263 		/* If a reset cycle isn't already in progress, we set a bit in
3264 		 * pf->state so that the service task can start a reset/rebuild.
3265 		 */
3266 		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3267 			if (reset == ICE_RESET_CORER)
3268 				set_bit(ICE_CORER_RECV, pf->state);
3269 			else if (reset == ICE_RESET_GLOBR)
3270 				set_bit(ICE_GLOBR_RECV, pf->state);
3271 			else
3272 				set_bit(ICE_EMPR_RECV, pf->state);
3273 
3274 			/* There are couple of different bits at play here.
3275 			 * hw->reset_ongoing indicates whether the hardware is
3276 			 * in reset. This is set to true when a reset interrupt
3277 			 * is received and set back to false after the driver
3278 			 * has determined that the hardware is out of reset.
3279 			 *
3280 			 * ICE_RESET_OICR_RECV in pf->state indicates
3281 			 * that a post reset rebuild is required before the
3282 			 * driver is operational again. This is set above.
3283 			 *
3284 			 * As this is the start of the reset/rebuild cycle, set
3285 			 * both to indicate that.
3286 			 */
3287 			hw->reset_ongoing = true;
3288 		}
3289 	}
3290 
3291 	if (oicr & PFINT_OICR_TSYN_TX_M) {
3292 		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3293 		if (ice_pf_state_is_nominal(pf) &&
3294 		    pf->hw.dev_caps.ts_dev_info.ts_ll_int_read) {
3295 			struct ice_ptp_tx *tx = &pf->ptp.port.tx;
3296 			unsigned long flags;
3297 			u8 idx;
3298 
3299 			spin_lock_irqsave(&tx->lock, flags);
3300 			idx = find_next_bit_wrap(tx->in_use, tx->len,
3301 						 tx->last_ll_ts_idx_read + 1);
3302 			if (idx != tx->len)
3303 				ice_ptp_req_tx_single_tstamp(tx, idx);
3304 			spin_unlock_irqrestore(&tx->lock, flags);
3305 		} else if (ice_ptp_pf_handles_tx_interrupt(pf)) {
3306 			set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
3307 			ret = IRQ_WAKE_THREAD;
3308 		}
3309 	}
3310 
3311 	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3312 		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3313 		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3314 
3315 		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3316 
3317 		if (ice_pf_src_tmr_owned(pf)) {
3318 			/* Save EVENTs from GLTSYN register */
3319 			pf->ptp.ext_ts_irq |= gltsyn_stat &
3320 					      (GLTSYN_STAT_EVENT0_M |
3321 					       GLTSYN_STAT_EVENT1_M |
3322 					       GLTSYN_STAT_EVENT2_M);
3323 
3324 			ice_ptp_extts_event(pf);
3325 		}
3326 	}
3327 
3328 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3329 	if (oicr & ICE_AUX_CRIT_ERR) {
3330 		pf->oicr_err_reg |= oicr;
3331 		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3332 		ena_mask &= ~ICE_AUX_CRIT_ERR;
3333 	}
3334 
3335 	/* Report any remaining unexpected interrupts */
3336 	oicr &= ena_mask;
3337 	if (oicr) {
3338 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3339 		/* If a critical error is pending there is no choice but to
3340 		 * reset the device.
3341 		 */
3342 		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3343 			    PFINT_OICR_ECC_ERR_M)) {
3344 			set_bit(ICE_PFR_REQ, pf->state);
3345 		}
3346 	}
3347 	ice_service_task_schedule(pf);
3348 	if (ret == IRQ_HANDLED)
3349 		ice_irq_dynamic_ena(hw, NULL, NULL);
3350 
3351 	return ret;
3352 }
3353 
3354 /**
3355  * ice_misc_intr_thread_fn - misc interrupt thread function
3356  * @irq: interrupt number
3357  * @data: pointer to a q_vector
3358  */
3359 static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3360 {
3361 	struct ice_pf *pf = data;
3362 	struct ice_hw *hw;
3363 
3364 	hw = &pf->hw;
3365 
3366 	if (ice_is_reset_in_progress(pf->state))
3367 		goto skip_irq;
3368 
3369 	if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3370 		/* Process outstanding Tx timestamps. If there is more work,
3371 		 * re-arm the interrupt to trigger again.
3372 		 */
3373 		if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3374 			wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3375 			ice_flush(hw);
3376 		}
3377 	}
3378 
3379 skip_irq:
3380 	ice_irq_dynamic_ena(hw, NULL, NULL);
3381 
3382 	return IRQ_HANDLED;
3383 }
3384 
3385 /**
3386  * ice_dis_ctrlq_interrupts - disable control queue interrupts
3387  * @hw: pointer to HW structure
3388  */
3389 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3390 {
3391 	/* disable Admin queue Interrupt causes */
3392 	wr32(hw, PFINT_FW_CTL,
3393 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3394 
3395 	/* disable Mailbox queue Interrupt causes */
3396 	wr32(hw, PFINT_MBX_CTL,
3397 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3398 
3399 	wr32(hw, PFINT_SB_CTL,
3400 	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3401 
3402 	/* disable Control queue Interrupt causes */
3403 	wr32(hw, PFINT_OICR_CTL,
3404 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3405 
3406 	ice_flush(hw);
3407 }
3408 
3409 /**
3410  * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
3411  * @pf: board private structure
3412  */
3413 static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
3414 {
3415 	int irq_num = pf->ll_ts_irq.virq;
3416 
3417 	synchronize_irq(irq_num);
3418 	devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
3419 
3420 	ice_free_irq(pf, pf->ll_ts_irq);
3421 }
3422 
3423 /**
3424  * ice_free_irq_msix_misc - Unroll misc vector setup
3425  * @pf: board private structure
3426  */
3427 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3428 {
3429 	int misc_irq_num = pf->oicr_irq.virq;
3430 	struct ice_hw *hw = &pf->hw;
3431 
3432 	ice_dis_ctrlq_interrupts(hw);
3433 
3434 	/* disable OICR interrupt */
3435 	wr32(hw, PFINT_OICR_ENA, 0);
3436 	ice_flush(hw);
3437 
3438 	synchronize_irq(misc_irq_num);
3439 	devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
3440 
3441 	ice_free_irq(pf, pf->oicr_irq);
3442 	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3443 		ice_free_irq_msix_ll_ts(pf);
3444 }
3445 
3446 /**
3447  * ice_ena_ctrlq_interrupts - enable control queue interrupts
3448  * @hw: pointer to HW structure
3449  * @reg_idx: HW vector index to associate the control queue interrupts with
3450  */
3451 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3452 {
3453 	u32 val;
3454 
3455 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3456 	       PFINT_OICR_CTL_CAUSE_ENA_M);
3457 	wr32(hw, PFINT_OICR_CTL, val);
3458 
3459 	/* enable Admin queue Interrupt causes */
3460 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3461 	       PFINT_FW_CTL_CAUSE_ENA_M);
3462 	wr32(hw, PFINT_FW_CTL, val);
3463 
3464 	/* enable Mailbox queue Interrupt causes */
3465 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3466 	       PFINT_MBX_CTL_CAUSE_ENA_M);
3467 	wr32(hw, PFINT_MBX_CTL, val);
3468 
3469 	if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
3470 		/* enable Sideband queue Interrupt causes */
3471 		val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3472 		       PFINT_SB_CTL_CAUSE_ENA_M);
3473 		wr32(hw, PFINT_SB_CTL, val);
3474 	}
3475 
3476 	ice_flush(hw);
3477 }
3478 
3479 /**
3480  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3481  * @pf: board private structure
3482  *
3483  * This sets up the handler for MSIX 0, which is used to manage the
3484  * non-queue interrupts, e.g. AdminQ and errors. This is not used
3485  * when in MSI or Legacy interrupt mode.
3486  */
3487 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3488 {
3489 	struct device *dev = ice_pf_to_dev(pf);
3490 	struct ice_hw *hw = &pf->hw;
3491 	u32 pf_intr_start_offset;
3492 	struct msi_map irq;
3493 	int err = 0;
3494 
3495 	if (!pf->int_name[0])
3496 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3497 			 dev_driver_string(dev), dev_name(dev));
3498 
3499 	if (!pf->int_name_ll_ts[0])
3500 		snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
3501 			 "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
3502 	/* Do not request IRQ but do enable OICR interrupt since settings are
3503 	 * lost during reset. Note that this function is called only during
3504 	 * rebuild path and not while reset is in progress.
3505 	 */
3506 	if (ice_is_reset_in_progress(pf->state))
3507 		goto skip_req_irq;
3508 
3509 	/* reserve one vector in irq_tracker for misc interrupts */
3510 	irq = ice_alloc_irq(pf, false);
3511 	if (irq.index < 0)
3512 		return irq.index;
3513 
3514 	pf->oicr_irq = irq;
3515 	err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3516 					ice_misc_intr_thread_fn, 0,
3517 					pf->int_name, pf);
3518 	if (err) {
3519 		dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3520 			pf->int_name, err);
3521 		ice_free_irq(pf, pf->oicr_irq);
3522 		return err;
3523 	}
3524 
3525 	/* reserve one vector in irq_tracker for ll_ts interrupt */
3526 	if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3527 		goto skip_req_irq;
3528 
3529 	irq = ice_alloc_irq(pf, false);
3530 	if (irq.index < 0)
3531 		return irq.index;
3532 
3533 	pf->ll_ts_irq = irq;
3534 	err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
3535 			       pf->int_name_ll_ts, pf);
3536 	if (err) {
3537 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
3538 			pf->int_name_ll_ts, err);
3539 		ice_free_irq(pf, pf->ll_ts_irq);
3540 		return err;
3541 	}
3542 
3543 skip_req_irq:
3544 	ice_ena_misc_vector(pf);
3545 
3546 	ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3547 	/* This enables LL TS interrupt */
3548 	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3549 	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3550 		wr32(hw, PFINT_SB_CTL,
3551 		     ((pf->ll_ts_irq.index + pf_intr_start_offset) &
3552 		      PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
3553 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3554 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3555 
3556 	ice_flush(hw);
3557 	ice_irq_dynamic_ena(hw, NULL, NULL);
3558 
3559 	return 0;
3560 }
3561 
3562 /**
3563  * ice_set_ops - set netdev and ethtools ops for the given netdev
3564  * @vsi: the VSI associated with the new netdev
3565  */
3566 static void ice_set_ops(struct ice_vsi *vsi)
3567 {
3568 	struct net_device *netdev = vsi->netdev;
3569 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3570 
3571 	if (ice_is_safe_mode(pf)) {
3572 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3573 		ice_set_ethtool_safe_mode_ops(netdev);
3574 		return;
3575 	}
3576 
3577 	netdev->netdev_ops = &ice_netdev_ops;
3578 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3579 	netdev->xdp_metadata_ops = &ice_xdp_md_ops;
3580 	ice_set_ethtool_ops(netdev);
3581 
3582 	if (vsi->type != ICE_VSI_PF)
3583 		return;
3584 
3585 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3586 			       NETDEV_XDP_ACT_XSK_ZEROCOPY |
3587 			       NETDEV_XDP_ACT_RX_SG;
3588 	netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3589 }
3590 
3591 /**
3592  * ice_set_netdev_features - set features for the given netdev
3593  * @netdev: netdev instance
3594  */
3595 void ice_set_netdev_features(struct net_device *netdev)
3596 {
3597 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3598 	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3599 	netdev_features_t csumo_features;
3600 	netdev_features_t vlano_features;
3601 	netdev_features_t dflt_features;
3602 	netdev_features_t tso_features;
3603 
3604 	if (ice_is_safe_mode(pf)) {
3605 		/* safe mode */
3606 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3607 		netdev->hw_features = netdev->features;
3608 		return;
3609 	}
3610 
3611 	dflt_features = NETIF_F_SG	|
3612 			NETIF_F_HIGHDMA	|
3613 			NETIF_F_NTUPLE	|
3614 			NETIF_F_RXHASH;
3615 
3616 	csumo_features = NETIF_F_RXCSUM	  |
3617 			 NETIF_F_IP_CSUM  |
3618 			 NETIF_F_SCTP_CRC |
3619 			 NETIF_F_IPV6_CSUM;
3620 
3621 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3622 			 NETIF_F_HW_VLAN_CTAG_TX     |
3623 			 NETIF_F_HW_VLAN_CTAG_RX;
3624 
3625 	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3626 	if (is_dvm_ena)
3627 		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3628 
3629 	tso_features = NETIF_F_TSO			|
3630 		       NETIF_F_TSO_ECN			|
3631 		       NETIF_F_TSO6			|
3632 		       NETIF_F_GSO_GRE			|
3633 		       NETIF_F_GSO_UDP_TUNNEL		|
3634 		       NETIF_F_GSO_GRE_CSUM		|
3635 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3636 		       NETIF_F_GSO_PARTIAL		|
3637 		       NETIF_F_GSO_IPXIP4		|
3638 		       NETIF_F_GSO_IPXIP6		|
3639 		       NETIF_F_GSO_UDP_L4;
3640 
3641 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3642 					NETIF_F_GSO_GRE_CSUM;
3643 	/* set features that user can change */
3644 	netdev->hw_features = dflt_features | csumo_features |
3645 			      vlano_features | tso_features;
3646 
3647 	/* add support for HW_CSUM on packets with MPLS header */
3648 	netdev->mpls_features =  NETIF_F_HW_CSUM |
3649 				 NETIF_F_TSO     |
3650 				 NETIF_F_TSO6;
3651 
3652 	/* enable features */
3653 	netdev->features |= netdev->hw_features;
3654 
3655 	netdev->hw_features |= NETIF_F_HW_TC;
3656 	netdev->hw_features |= NETIF_F_LOOPBACK;
3657 
3658 	/* encap and VLAN devices inherit default, csumo and tso features */
3659 	netdev->hw_enc_features |= dflt_features | csumo_features |
3660 				   tso_features;
3661 	netdev->vlan_features |= dflt_features | csumo_features |
3662 				 tso_features;
3663 
3664 	/* advertise support but don't enable by default since only one type of
3665 	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3666 	 * type turns on the other has to be turned off. This is enforced by the
3667 	 * ice_fix_features() ndo callback.
3668 	 */
3669 	if (is_dvm_ena)
3670 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3671 			NETIF_F_HW_VLAN_STAG_TX;
3672 
3673 	/* Leave CRC / FCS stripping enabled by default, but allow the value to
3674 	 * be changed at runtime
3675 	 */
3676 	netdev->hw_features |= NETIF_F_RXFCS;
3677 
3678 	netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
3679 }
3680 
3681 /**
3682  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3683  * @lut: Lookup table
3684  * @rss_table_size: Lookup table size
3685  * @rss_size: Range of queue number for hashing
3686  */
3687 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3688 {
3689 	u16 i;
3690 
3691 	for (i = 0; i < rss_table_size; i++)
3692 		lut[i] = i % rss_size;
3693 }
3694 
3695 /**
3696  * ice_pf_vsi_setup - Set up a PF VSI
3697  * @pf: board private structure
3698  * @pi: pointer to the port_info instance
3699  *
3700  * Returns pointer to the successfully allocated VSI software struct
3701  * on success, otherwise returns NULL on failure.
3702  */
3703 static struct ice_vsi *
3704 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3705 {
3706 	struct ice_vsi_cfg_params params = {};
3707 
3708 	params.type = ICE_VSI_PF;
3709 	params.port_info = pi;
3710 	params.flags = ICE_VSI_FLAG_INIT;
3711 
3712 	return ice_vsi_setup(pf, &params);
3713 }
3714 
3715 static struct ice_vsi *
3716 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3717 		   struct ice_channel *ch)
3718 {
3719 	struct ice_vsi_cfg_params params = {};
3720 
3721 	params.type = ICE_VSI_CHNL;
3722 	params.port_info = pi;
3723 	params.ch = ch;
3724 	params.flags = ICE_VSI_FLAG_INIT;
3725 
3726 	return ice_vsi_setup(pf, &params);
3727 }
3728 
3729 /**
3730  * ice_ctrl_vsi_setup - Set up a control VSI
3731  * @pf: board private structure
3732  * @pi: pointer to the port_info instance
3733  *
3734  * Returns pointer to the successfully allocated VSI software struct
3735  * on success, otherwise returns NULL on failure.
3736  */
3737 static struct ice_vsi *
3738 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3739 {
3740 	struct ice_vsi_cfg_params params = {};
3741 
3742 	params.type = ICE_VSI_CTRL;
3743 	params.port_info = pi;
3744 	params.flags = ICE_VSI_FLAG_INIT;
3745 
3746 	return ice_vsi_setup(pf, &params);
3747 }
3748 
3749 /**
3750  * ice_lb_vsi_setup - Set up a loopback VSI
3751  * @pf: board private structure
3752  * @pi: pointer to the port_info instance
3753  *
3754  * Returns pointer to the successfully allocated VSI software struct
3755  * on success, otherwise returns NULL on failure.
3756  */
3757 struct ice_vsi *
3758 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3759 {
3760 	struct ice_vsi_cfg_params params = {};
3761 
3762 	params.type = ICE_VSI_LB;
3763 	params.port_info = pi;
3764 	params.flags = ICE_VSI_FLAG_INIT;
3765 
3766 	return ice_vsi_setup(pf, &params);
3767 }
3768 
3769 /**
3770  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3771  * @netdev: network interface to be adjusted
3772  * @proto: VLAN TPID
3773  * @vid: VLAN ID to be added
3774  *
3775  * net_device_ops implementation for adding VLAN IDs
3776  */
3777 int ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3778 {
3779 	struct ice_netdev_priv *np = netdev_priv(netdev);
3780 	struct ice_vsi_vlan_ops *vlan_ops;
3781 	struct ice_vsi *vsi = np->vsi;
3782 	struct ice_vlan vlan;
3783 	int ret;
3784 
3785 	/* VLAN 0 is added by default during load/reset */
3786 	if (!vid)
3787 		return 0;
3788 
3789 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3790 		usleep_range(1000, 2000);
3791 
3792 	/* Add multicast promisc rule for the VLAN ID to be added if
3793 	 * all-multicast is currently enabled.
3794 	 */
3795 	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3796 		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3797 					       ICE_MCAST_VLAN_PROMISC_BITS,
3798 					       vid);
3799 		if (ret)
3800 			goto finish;
3801 	}
3802 
3803 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3804 
3805 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3806 	 * packets aren't pruned by the device's internal switch on Rx
3807 	 */
3808 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3809 	ret = vlan_ops->add_vlan(vsi, &vlan);
3810 	if (ret)
3811 		goto finish;
3812 
3813 	/* If all-multicast is currently enabled and this VLAN ID is only one
3814 	 * besides VLAN-0 we have to update look-up type of multicast promisc
3815 	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3816 	 */
3817 	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3818 	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3819 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3820 					   ICE_MCAST_PROMISC_BITS, 0);
3821 		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3822 					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3823 	}
3824 
3825 finish:
3826 	clear_bit(ICE_CFG_BUSY, vsi->state);
3827 
3828 	return ret;
3829 }
3830 
3831 /**
3832  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3833  * @netdev: network interface to be adjusted
3834  * @proto: VLAN TPID
3835  * @vid: VLAN ID to be removed
3836  *
3837  * net_device_ops implementation for removing VLAN IDs
3838  */
3839 int ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3840 {
3841 	struct ice_netdev_priv *np = netdev_priv(netdev);
3842 	struct ice_vsi_vlan_ops *vlan_ops;
3843 	struct ice_vsi *vsi = np->vsi;
3844 	struct ice_vlan vlan;
3845 	int ret;
3846 
3847 	/* don't allow removal of VLAN 0 */
3848 	if (!vid)
3849 		return 0;
3850 
3851 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3852 		usleep_range(1000, 2000);
3853 
3854 	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3855 				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3856 	if (ret) {
3857 		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3858 			   vsi->vsi_num);
3859 		vsi->current_netdev_flags |= IFF_ALLMULTI;
3860 	}
3861 
3862 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3863 
3864 	/* Make sure VLAN delete is successful before updating VLAN
3865 	 * information
3866 	 */
3867 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3868 	ret = vlan_ops->del_vlan(vsi, &vlan);
3869 	if (ret)
3870 		goto finish;
3871 
3872 	/* Remove multicast promisc rule for the removed VLAN ID if
3873 	 * all-multicast is enabled.
3874 	 */
3875 	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3876 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3877 					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3878 
3879 	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3880 		/* Update look-up type of multicast promisc rule for VLAN 0
3881 		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3882 		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3883 		 */
3884 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3885 			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3886 						   ICE_MCAST_VLAN_PROMISC_BITS,
3887 						   0);
3888 			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3889 						 ICE_MCAST_PROMISC_BITS, 0);
3890 		}
3891 	}
3892 
3893 finish:
3894 	clear_bit(ICE_CFG_BUSY, vsi->state);
3895 
3896 	return ret;
3897 }
3898 
3899 /**
3900  * ice_rep_indr_tc_block_unbind
3901  * @cb_priv: indirection block private data
3902  */
3903 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3904 {
3905 	struct ice_indr_block_priv *indr_priv = cb_priv;
3906 
3907 	list_del(&indr_priv->list);
3908 	kfree(indr_priv);
3909 }
3910 
3911 /**
3912  * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3913  * @vsi: VSI struct which has the netdev
3914  */
3915 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3916 {
3917 	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3918 
3919 	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3920 				 ice_rep_indr_tc_block_unbind);
3921 }
3922 
3923 /**
3924  * ice_tc_indir_block_register - Register TC indirect block notifications
3925  * @vsi: VSI struct which has the netdev
3926  *
3927  * Returns 0 on success, negative value on failure
3928  */
3929 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3930 {
3931 	struct ice_netdev_priv *np;
3932 
3933 	if (!vsi || !vsi->netdev)
3934 		return -EINVAL;
3935 
3936 	np = netdev_priv(vsi->netdev);
3937 
3938 	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3939 	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3940 }
3941 
3942 /**
3943  * ice_get_avail_q_count - Get count of queues in use
3944  * @pf_qmap: bitmap to get queue use count from
3945  * @lock: pointer to a mutex that protects access to pf_qmap
3946  * @size: size of the bitmap
3947  */
3948 static u16
3949 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3950 {
3951 	unsigned long bit;
3952 	u16 count = 0;
3953 
3954 	mutex_lock(lock);
3955 	for_each_clear_bit(bit, pf_qmap, size)
3956 		count++;
3957 	mutex_unlock(lock);
3958 
3959 	return count;
3960 }
3961 
3962 /**
3963  * ice_get_avail_txq_count - Get count of Tx queues in use
3964  * @pf: pointer to an ice_pf instance
3965  */
3966 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3967 {
3968 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3969 				     pf->max_pf_txqs);
3970 }
3971 
3972 /**
3973  * ice_get_avail_rxq_count - Get count of Rx queues in use
3974  * @pf: pointer to an ice_pf instance
3975  */
3976 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3977 {
3978 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3979 				     pf->max_pf_rxqs);
3980 }
3981 
3982 /**
3983  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3984  * @pf: board private structure to initialize
3985  */
3986 static void ice_deinit_pf(struct ice_pf *pf)
3987 {
3988 	ice_service_task_stop(pf);
3989 	mutex_destroy(&pf->lag_mutex);
3990 	mutex_destroy(&pf->adev_mutex);
3991 	mutex_destroy(&pf->sw_mutex);
3992 	mutex_destroy(&pf->tc_mutex);
3993 	mutex_destroy(&pf->avail_q_mutex);
3994 	mutex_destroy(&pf->vfs.table_lock);
3995 
3996 	if (pf->avail_txqs) {
3997 		bitmap_free(pf->avail_txqs);
3998 		pf->avail_txqs = NULL;
3999 	}
4000 
4001 	if (pf->avail_rxqs) {
4002 		bitmap_free(pf->avail_rxqs);
4003 		pf->avail_rxqs = NULL;
4004 	}
4005 
4006 	if (pf->ptp.clock)
4007 		ptp_clock_unregister(pf->ptp.clock);
4008 
4009 	xa_destroy(&pf->dyn_ports);
4010 	xa_destroy(&pf->sf_nums);
4011 }
4012 
4013 /**
4014  * ice_set_pf_caps - set PFs capability flags
4015  * @pf: pointer to the PF instance
4016  */
4017 static void ice_set_pf_caps(struct ice_pf *pf)
4018 {
4019 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
4020 
4021 	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4022 	if (func_caps->common_cap.rdma)
4023 		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4024 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4025 	if (func_caps->common_cap.dcb)
4026 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4027 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4028 	if (func_caps->common_cap.sr_iov_1_1) {
4029 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4030 		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
4031 					      ICE_MAX_SRIOV_VFS);
4032 	}
4033 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
4034 	if (func_caps->common_cap.rss_table_size)
4035 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
4036 
4037 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
4038 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
4039 		u16 unused;
4040 
4041 		/* ctrl_vsi_idx will be set to a valid value when flow director
4042 		 * is setup by ice_init_fdir
4043 		 */
4044 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4045 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
4046 		/* force guaranteed filter pool for PF */
4047 		ice_alloc_fd_guar_item(&pf->hw, &unused,
4048 				       func_caps->fd_fltr_guar);
4049 		/* force shared filter pool for PF */
4050 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
4051 				       func_caps->fd_fltr_best_effort);
4052 	}
4053 
4054 	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4055 	if (func_caps->common_cap.ieee_1588 &&
4056 	    !(pf->hw.mac_type == ICE_MAC_E830))
4057 		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4058 
4059 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
4060 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
4061 }
4062 
4063 /**
4064  * ice_init_pf - Initialize general software structures (struct ice_pf)
4065  * @pf: board private structure to initialize
4066  */
4067 static int ice_init_pf(struct ice_pf *pf)
4068 {
4069 	ice_set_pf_caps(pf);
4070 
4071 	mutex_init(&pf->sw_mutex);
4072 	mutex_init(&pf->tc_mutex);
4073 	mutex_init(&pf->adev_mutex);
4074 	mutex_init(&pf->lag_mutex);
4075 
4076 	INIT_HLIST_HEAD(&pf->aq_wait_list);
4077 	spin_lock_init(&pf->aq_wait_lock);
4078 	init_waitqueue_head(&pf->aq_wait_queue);
4079 
4080 	init_waitqueue_head(&pf->reset_wait_queue);
4081 
4082 	/* setup service timer and periodic service task */
4083 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
4084 	pf->serv_tmr_period = HZ;
4085 	INIT_WORK(&pf->serv_task, ice_service_task);
4086 	clear_bit(ICE_SERVICE_SCHED, pf->state);
4087 
4088 	mutex_init(&pf->avail_q_mutex);
4089 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
4090 	if (!pf->avail_txqs)
4091 		return -ENOMEM;
4092 
4093 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
4094 	if (!pf->avail_rxqs) {
4095 		bitmap_free(pf->avail_txqs);
4096 		pf->avail_txqs = NULL;
4097 		return -ENOMEM;
4098 	}
4099 
4100 	mutex_init(&pf->vfs.table_lock);
4101 	hash_init(pf->vfs.table);
4102 	ice_mbx_init_snapshot(&pf->hw);
4103 
4104 	xa_init(&pf->dyn_ports);
4105 	xa_init(&pf->sf_nums);
4106 
4107 	return 0;
4108 }
4109 
4110 /**
4111  * ice_is_wol_supported - check if WoL is supported
4112  * @hw: pointer to hardware info
4113  *
4114  * Check if WoL is supported based on the HW configuration.
4115  * Returns true if NVM supports and enables WoL for this port, false otherwise
4116  */
4117 bool ice_is_wol_supported(struct ice_hw *hw)
4118 {
4119 	u16 wol_ctrl;
4120 
4121 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4122 	 * word) indicates WoL is not supported on the corresponding PF ID.
4123 	 */
4124 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4125 		return false;
4126 
4127 	return !(BIT(hw->port_info->lport) & wol_ctrl);
4128 }
4129 
4130 /**
4131  * ice_vsi_recfg_qs - Change the number of queues on a VSI
4132  * @vsi: VSI being changed
4133  * @new_rx: new number of Rx queues
4134  * @new_tx: new number of Tx queues
4135  * @locked: is adev device_lock held
4136  *
4137  * Only change the number of queues if new_tx, or new_rx is non-0.
4138  *
4139  * Returns 0 on success.
4140  */
4141 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
4142 {
4143 	struct ice_pf *pf = vsi->back;
4144 	int i, err = 0, timeout = 50;
4145 
4146 	if (!new_rx && !new_tx)
4147 		return -EINVAL;
4148 
4149 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4150 		timeout--;
4151 		if (!timeout)
4152 			return -EBUSY;
4153 		usleep_range(1000, 2000);
4154 	}
4155 
4156 	if (new_tx)
4157 		vsi->req_txq = (u16)new_tx;
4158 	if (new_rx)
4159 		vsi->req_rxq = (u16)new_rx;
4160 
4161 	/* set for the next time the netdev is started */
4162 	if (!netif_running(vsi->netdev)) {
4163 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4164 		if (err)
4165 			goto rebuild_err;
4166 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4167 		goto done;
4168 	}
4169 
4170 	ice_vsi_close(vsi);
4171 	err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4172 	if (err)
4173 		goto rebuild_err;
4174 
4175 	ice_for_each_traffic_class(i) {
4176 		if (vsi->tc_cfg.ena_tc & BIT(i))
4177 			netdev_set_tc_queue(vsi->netdev,
4178 					    vsi->tc_cfg.tc_info[i].netdev_tc,
4179 					    vsi->tc_cfg.tc_info[i].qcount_tx,
4180 					    vsi->tc_cfg.tc_info[i].qoffset);
4181 	}
4182 	ice_pf_dcb_recfg(pf, locked);
4183 	ice_vsi_open(vsi);
4184 	goto done;
4185 
4186 rebuild_err:
4187 	dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4188 		err);
4189 done:
4190 	clear_bit(ICE_CFG_BUSY, pf->state);
4191 	return err;
4192 }
4193 
4194 /**
4195  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4196  * @pf: PF to configure
4197  *
4198  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4199  * VSI can still Tx/Rx VLAN tagged packets.
4200  */
4201 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4202 {
4203 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4204 	struct ice_vsi_ctx *ctxt;
4205 	struct ice_hw *hw;
4206 	int status;
4207 
4208 	if (!vsi)
4209 		return;
4210 
4211 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4212 	if (!ctxt)
4213 		return;
4214 
4215 	hw = &pf->hw;
4216 	ctxt->info = vsi->info;
4217 
4218 	ctxt->info.valid_sections =
4219 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4220 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4221 			    ICE_AQ_VSI_PROP_SW_VALID);
4222 
4223 	/* disable VLAN anti-spoof */
4224 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4225 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4226 
4227 	/* disable VLAN pruning and keep all other settings */
4228 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4229 
4230 	/* allow all VLANs on Tx and don't strip on Rx */
4231 	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4232 		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4233 
4234 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4235 	if (status) {
4236 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4237 			status, ice_aq_str(hw->adminq.sq_last_status));
4238 	} else {
4239 		vsi->info.sec_flags = ctxt->info.sec_flags;
4240 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4241 		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4242 	}
4243 
4244 	kfree(ctxt);
4245 }
4246 
4247 /**
4248  * ice_log_pkg_init - log result of DDP package load
4249  * @hw: pointer to hardware info
4250  * @state: state of package load
4251  */
4252 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4253 {
4254 	struct ice_pf *pf = hw->back;
4255 	struct device *dev;
4256 
4257 	dev = ice_pf_to_dev(pf);
4258 
4259 	switch (state) {
4260 	case ICE_DDP_PKG_SUCCESS:
4261 		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4262 			 hw->active_pkg_name,
4263 			 hw->active_pkg_ver.major,
4264 			 hw->active_pkg_ver.minor,
4265 			 hw->active_pkg_ver.update,
4266 			 hw->active_pkg_ver.draft);
4267 		break;
4268 	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4269 		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4270 			 hw->active_pkg_name,
4271 			 hw->active_pkg_ver.major,
4272 			 hw->active_pkg_ver.minor,
4273 			 hw->active_pkg_ver.update,
4274 			 hw->active_pkg_ver.draft);
4275 		break;
4276 	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4277 		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",
4278 			hw->active_pkg_name,
4279 			hw->active_pkg_ver.major,
4280 			hw->active_pkg_ver.minor,
4281 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4282 		break;
4283 	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4284 		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",
4285 			 hw->active_pkg_name,
4286 			 hw->active_pkg_ver.major,
4287 			 hw->active_pkg_ver.minor,
4288 			 hw->active_pkg_ver.update,
4289 			 hw->active_pkg_ver.draft,
4290 			 hw->pkg_name,
4291 			 hw->pkg_ver.major,
4292 			 hw->pkg_ver.minor,
4293 			 hw->pkg_ver.update,
4294 			 hw->pkg_ver.draft);
4295 		break;
4296 	case ICE_DDP_PKG_FW_MISMATCH:
4297 		dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package.  Please update the device's NVM.  Entering safe mode.\n");
4298 		break;
4299 	case ICE_DDP_PKG_INVALID_FILE:
4300 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4301 		break;
4302 	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4303 		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4304 		break;
4305 	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4306 		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",
4307 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4308 		break;
4309 	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4310 		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");
4311 		break;
4312 	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4313 		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");
4314 		break;
4315 	case ICE_DDP_PKG_LOAD_ERROR:
4316 		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4317 		/* poll for reset to complete */
4318 		if (ice_check_reset(hw))
4319 			dev_err(dev, "Error resetting device. Please reload the driver\n");
4320 		break;
4321 	case ICE_DDP_PKG_ERR:
4322 	default:
4323 		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4324 		break;
4325 	}
4326 }
4327 
4328 /**
4329  * ice_load_pkg - load/reload the DDP Package file
4330  * @firmware: firmware structure when firmware requested or NULL for reload
4331  * @pf: pointer to the PF instance
4332  *
4333  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4334  * initialize HW tables.
4335  */
4336 static void
4337 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4338 {
4339 	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4340 	struct device *dev = ice_pf_to_dev(pf);
4341 	struct ice_hw *hw = &pf->hw;
4342 
4343 	/* Load DDP Package */
4344 	if (firmware && !hw->pkg_copy) {
4345 		state = ice_copy_and_init_pkg(hw, firmware->data,
4346 					      firmware->size);
4347 		ice_log_pkg_init(hw, state);
4348 	} else if (!firmware && hw->pkg_copy) {
4349 		/* Reload package during rebuild after CORER/GLOBR reset */
4350 		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4351 		ice_log_pkg_init(hw, state);
4352 	} else {
4353 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4354 	}
4355 
4356 	if (!ice_is_init_pkg_successful(state)) {
4357 		/* Safe Mode */
4358 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4359 		return;
4360 	}
4361 
4362 	/* Successful download package is the precondition for advanced
4363 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4364 	 */
4365 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4366 }
4367 
4368 /**
4369  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4370  * @pf: pointer to the PF structure
4371  *
4372  * There is no error returned here because the driver should be able to handle
4373  * 128 Byte cache lines, so we only print a warning in case issues are seen,
4374  * specifically with Tx.
4375  */
4376 static void ice_verify_cacheline_size(struct ice_pf *pf)
4377 {
4378 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4379 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4380 			 ICE_CACHE_LINE_BYTES);
4381 }
4382 
4383 /**
4384  * ice_send_version - update firmware with driver version
4385  * @pf: PF struct
4386  *
4387  * Returns 0 on success, else error code
4388  */
4389 static int ice_send_version(struct ice_pf *pf)
4390 {
4391 	struct ice_driver_ver dv;
4392 
4393 	dv.major_ver = 0xff;
4394 	dv.minor_ver = 0xff;
4395 	dv.build_ver = 0xff;
4396 	dv.subbuild_ver = 0;
4397 	strscpy((char *)dv.driver_string, UTS_RELEASE,
4398 		sizeof(dv.driver_string));
4399 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4400 }
4401 
4402 /**
4403  * ice_init_fdir - Initialize flow director VSI and configuration
4404  * @pf: pointer to the PF instance
4405  *
4406  * returns 0 on success, negative on error
4407  */
4408 static int ice_init_fdir(struct ice_pf *pf)
4409 {
4410 	struct device *dev = ice_pf_to_dev(pf);
4411 	struct ice_vsi *ctrl_vsi;
4412 	int err;
4413 
4414 	/* Side Band Flow Director needs to have a control VSI.
4415 	 * Allocate it and store it in the PF.
4416 	 */
4417 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4418 	if (!ctrl_vsi) {
4419 		dev_dbg(dev, "could not create control VSI\n");
4420 		return -ENOMEM;
4421 	}
4422 
4423 	err = ice_vsi_open_ctrl(ctrl_vsi);
4424 	if (err) {
4425 		dev_dbg(dev, "could not open control VSI\n");
4426 		goto err_vsi_open;
4427 	}
4428 
4429 	mutex_init(&pf->hw.fdir_fltr_lock);
4430 
4431 	err = ice_fdir_create_dflt_rules(pf);
4432 	if (err)
4433 		goto err_fdir_rule;
4434 
4435 	return 0;
4436 
4437 err_fdir_rule:
4438 	ice_fdir_release_flows(&pf->hw);
4439 	ice_vsi_close(ctrl_vsi);
4440 err_vsi_open:
4441 	ice_vsi_release(ctrl_vsi);
4442 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4443 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4444 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4445 	}
4446 	return err;
4447 }
4448 
4449 static void ice_deinit_fdir(struct ice_pf *pf)
4450 {
4451 	struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4452 
4453 	if (!vsi)
4454 		return;
4455 
4456 	ice_vsi_manage_fdir(vsi, false);
4457 	ice_vsi_release(vsi);
4458 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4459 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4460 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4461 	}
4462 
4463 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4464 }
4465 
4466 /**
4467  * ice_get_opt_fw_name - return optional firmware file name or NULL
4468  * @pf: pointer to the PF instance
4469  */
4470 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4471 {
4472 	/* Optional firmware name same as default with additional dash
4473 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4474 	 */
4475 	struct pci_dev *pdev = pf->pdev;
4476 	char *opt_fw_filename;
4477 	u64 dsn;
4478 
4479 	/* Determine the name of the optional file using the DSN (two
4480 	 * dwords following the start of the DSN Capability).
4481 	 */
4482 	dsn = pci_get_dsn(pdev);
4483 	if (!dsn)
4484 		return NULL;
4485 
4486 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4487 	if (!opt_fw_filename)
4488 		return NULL;
4489 
4490 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4491 		 ICE_DDP_PKG_PATH, dsn);
4492 
4493 	return opt_fw_filename;
4494 }
4495 
4496 /**
4497  * ice_request_fw - Device initialization routine
4498  * @pf: pointer to the PF instance
4499  * @firmware: double pointer to firmware struct
4500  *
4501  * Return: zero when successful, negative values otherwise.
4502  */
4503 static int ice_request_fw(struct ice_pf *pf, const struct firmware **firmware)
4504 {
4505 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4506 	struct device *dev = ice_pf_to_dev(pf);
4507 	int err = 0;
4508 
4509 	/* optional device-specific DDP (if present) overrides the default DDP
4510 	 * package file. kernel logs a debug message if the file doesn't exist,
4511 	 * and warning messages for other errors.
4512 	 */
4513 	if (opt_fw_filename) {
4514 		err = firmware_request_nowarn(firmware, opt_fw_filename, dev);
4515 		kfree(opt_fw_filename);
4516 		if (!err)
4517 			return err;
4518 	}
4519 	err = request_firmware(firmware, ICE_DDP_PKG_FILE, dev);
4520 	if (err)
4521 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4522 
4523 	return err;
4524 }
4525 
4526 /**
4527  * ice_init_tx_topology - performs Tx topology initialization
4528  * @hw: pointer to the hardware structure
4529  * @firmware: pointer to firmware structure
4530  *
4531  * Return: zero when init was successful, negative values otherwise.
4532  */
4533 static int
4534 ice_init_tx_topology(struct ice_hw *hw, const struct firmware *firmware)
4535 {
4536 	u8 num_tx_sched_layers = hw->num_tx_sched_layers;
4537 	struct ice_pf *pf = hw->back;
4538 	struct device *dev;
4539 	int err;
4540 
4541 	dev = ice_pf_to_dev(pf);
4542 	err = ice_cfg_tx_topo(hw, firmware->data, firmware->size);
4543 	if (!err) {
4544 		if (hw->num_tx_sched_layers > num_tx_sched_layers)
4545 			dev_info(dev, "Tx scheduling layers switching feature disabled\n");
4546 		else
4547 			dev_info(dev, "Tx scheduling layers switching feature enabled\n");
4548 		/* if there was a change in topology ice_cfg_tx_topo triggered
4549 		 * a CORER and we need to re-init hw
4550 		 */
4551 		ice_deinit_hw(hw);
4552 		err = ice_init_hw(hw);
4553 
4554 		return err;
4555 	} else if (err == -EIO) {
4556 		dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
4557 	}
4558 
4559 	return 0;
4560 }
4561 
4562 /**
4563  * ice_init_ddp_config - DDP related configuration
4564  * @hw: pointer to the hardware structure
4565  * @pf: pointer to pf structure
4566  *
4567  * This function loads DDP file from the disk, then initializes Tx
4568  * topology. At the end DDP package is loaded on the card.
4569  *
4570  * Return: zero when init was successful, negative values otherwise.
4571  */
4572 static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
4573 {
4574 	struct device *dev = ice_pf_to_dev(pf);
4575 	const struct firmware *firmware = NULL;
4576 	int err;
4577 
4578 	err = ice_request_fw(pf, &firmware);
4579 	if (err) {
4580 		dev_err(dev, "Fail during requesting FW: %d\n", err);
4581 		return err;
4582 	}
4583 
4584 	err = ice_init_tx_topology(hw, firmware);
4585 	if (err) {
4586 		dev_err(dev, "Fail during initialization of Tx topology: %d\n",
4587 			err);
4588 		release_firmware(firmware);
4589 		return err;
4590 	}
4591 
4592 	/* Download firmware to device */
4593 	ice_load_pkg(firmware, pf);
4594 	release_firmware(firmware);
4595 
4596 	return 0;
4597 }
4598 
4599 /**
4600  * ice_print_wake_reason - show the wake up cause in the log
4601  * @pf: pointer to the PF struct
4602  */
4603 static void ice_print_wake_reason(struct ice_pf *pf)
4604 {
4605 	u32 wus = pf->wakeup_reason;
4606 	const char *wake_str;
4607 
4608 	/* if no wake event, nothing to print */
4609 	if (!wus)
4610 		return;
4611 
4612 	if (wus & PFPM_WUS_LNKC_M)
4613 		wake_str = "Link\n";
4614 	else if (wus & PFPM_WUS_MAG_M)
4615 		wake_str = "Magic Packet\n";
4616 	else if (wus & PFPM_WUS_MNG_M)
4617 		wake_str = "Management\n";
4618 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4619 		wake_str = "Firmware Reset\n";
4620 	else
4621 		wake_str = "Unknown\n";
4622 
4623 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4624 }
4625 
4626 /**
4627  * ice_pf_fwlog_update_module - update 1 module
4628  * @pf: pointer to the PF struct
4629  * @log_level: log_level to use for the @module
4630  * @module: module to update
4631  */
4632 void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4633 {
4634 	struct ice_hw *hw = &pf->hw;
4635 
4636 	hw->fwlog_cfg.module_entries[module].log_level = log_level;
4637 }
4638 
4639 /**
4640  * ice_register_netdev - register netdev
4641  * @vsi: pointer to the VSI struct
4642  */
4643 static int ice_register_netdev(struct ice_vsi *vsi)
4644 {
4645 	int err;
4646 
4647 	if (!vsi || !vsi->netdev)
4648 		return -EIO;
4649 
4650 	err = register_netdev(vsi->netdev);
4651 	if (err)
4652 		return err;
4653 
4654 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4655 	netif_carrier_off(vsi->netdev);
4656 	netif_tx_stop_all_queues(vsi->netdev);
4657 
4658 	return 0;
4659 }
4660 
4661 static void ice_unregister_netdev(struct ice_vsi *vsi)
4662 {
4663 	if (!vsi || !vsi->netdev)
4664 		return;
4665 
4666 	unregister_netdev(vsi->netdev);
4667 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4668 }
4669 
4670 /**
4671  * ice_cfg_netdev - Allocate, configure and register a netdev
4672  * @vsi: the VSI associated with the new netdev
4673  *
4674  * Returns 0 on success, negative value on failure
4675  */
4676 static int ice_cfg_netdev(struct ice_vsi *vsi)
4677 {
4678 	struct ice_netdev_priv *np;
4679 	struct net_device *netdev;
4680 	u8 mac_addr[ETH_ALEN];
4681 
4682 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4683 				    vsi->alloc_rxq);
4684 	if (!netdev)
4685 		return -ENOMEM;
4686 
4687 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4688 	vsi->netdev = netdev;
4689 	np = netdev_priv(netdev);
4690 	np->vsi = vsi;
4691 
4692 	ice_set_netdev_features(netdev);
4693 	ice_set_ops(vsi);
4694 
4695 	if (vsi->type == ICE_VSI_PF) {
4696 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4697 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4698 		eth_hw_addr_set(netdev, mac_addr);
4699 	}
4700 
4701 	netdev->priv_flags |= IFF_UNICAST_FLT;
4702 
4703 	/* Setup netdev TC information */
4704 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4705 
4706 	netdev->max_mtu = ICE_MAX_MTU;
4707 
4708 	return 0;
4709 }
4710 
4711 static void ice_decfg_netdev(struct ice_vsi *vsi)
4712 {
4713 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4714 	free_netdev(vsi->netdev);
4715 	vsi->netdev = NULL;
4716 }
4717 
4718 /**
4719  * ice_wait_for_fw - wait for full FW readiness
4720  * @hw: pointer to the hardware structure
4721  * @timeout: milliseconds that can elapse before timing out
4722  */
4723 static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4724 {
4725 	int fw_loading;
4726 	u32 elapsed = 0;
4727 
4728 	while (elapsed <= timeout) {
4729 		fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4730 
4731 		/* firmware was not yet loaded, we have to wait more */
4732 		if (fw_loading) {
4733 			elapsed += 100;
4734 			msleep(100);
4735 			continue;
4736 		}
4737 		return 0;
4738 	}
4739 
4740 	return -ETIMEDOUT;
4741 }
4742 
4743 int ice_init_dev(struct ice_pf *pf)
4744 {
4745 	struct device *dev = ice_pf_to_dev(pf);
4746 	struct ice_hw *hw = &pf->hw;
4747 	int err;
4748 
4749 	err = ice_init_hw(hw);
4750 	if (err) {
4751 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4752 		return err;
4753 	}
4754 
4755 	/* Some cards require longer initialization times
4756 	 * due to necessity of loading FW from an external source.
4757 	 * This can take even half a minute.
4758 	 */
4759 	if (ice_is_pf_c827(hw)) {
4760 		err = ice_wait_for_fw(hw, 30000);
4761 		if (err) {
4762 			dev_err(dev, "ice_wait_for_fw timed out");
4763 			return err;
4764 		}
4765 	}
4766 
4767 	ice_init_feature_support(pf);
4768 
4769 	err = ice_init_ddp_config(hw, pf);
4770 	if (err)
4771 		return err;
4772 
4773 	/* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
4774 	 * set in pf->state, which will cause ice_is_safe_mode to return
4775 	 * true
4776 	 */
4777 	if (ice_is_safe_mode(pf)) {
4778 		/* we already got function/device capabilities but these don't
4779 		 * reflect what the driver needs to do in safe mode. Instead of
4780 		 * adding conditional logic everywhere to ignore these
4781 		 * device/function capabilities, override them.
4782 		 */
4783 		ice_set_safe_mode_caps(hw);
4784 	}
4785 
4786 	err = ice_init_pf(pf);
4787 	if (err) {
4788 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4789 		goto err_init_pf;
4790 	}
4791 
4792 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4793 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4794 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4795 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4796 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4797 		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4798 			pf->hw.tnl.valid_count[TNL_VXLAN];
4799 		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4800 			UDP_TUNNEL_TYPE_VXLAN;
4801 	}
4802 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4803 		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4804 			pf->hw.tnl.valid_count[TNL_GENEVE];
4805 		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4806 			UDP_TUNNEL_TYPE_GENEVE;
4807 	}
4808 
4809 	err = ice_init_interrupt_scheme(pf);
4810 	if (err) {
4811 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4812 		err = -EIO;
4813 		goto err_init_interrupt_scheme;
4814 	}
4815 
4816 	/* In case of MSIX we are going to setup the misc vector right here
4817 	 * to handle admin queue events etc. In case of legacy and MSI
4818 	 * the misc functionality and queue processing is combined in
4819 	 * the same vector and that gets setup at open.
4820 	 */
4821 	err = ice_req_irq_msix_misc(pf);
4822 	if (err) {
4823 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4824 		goto err_req_irq_msix_misc;
4825 	}
4826 
4827 	return 0;
4828 
4829 err_req_irq_msix_misc:
4830 	ice_clear_interrupt_scheme(pf);
4831 err_init_interrupt_scheme:
4832 	ice_deinit_pf(pf);
4833 err_init_pf:
4834 	ice_deinit_hw(hw);
4835 	return err;
4836 }
4837 
4838 void ice_deinit_dev(struct ice_pf *pf)
4839 {
4840 	ice_free_irq_msix_misc(pf);
4841 	ice_deinit_pf(pf);
4842 	ice_deinit_hw(&pf->hw);
4843 
4844 	/* Service task is already stopped, so call reset directly. */
4845 	ice_reset(&pf->hw, ICE_RESET_PFR);
4846 	pci_wait_for_pending_transaction(pf->pdev);
4847 	ice_clear_interrupt_scheme(pf);
4848 }
4849 
4850 static void ice_init_features(struct ice_pf *pf)
4851 {
4852 	struct device *dev = ice_pf_to_dev(pf);
4853 
4854 	if (ice_is_safe_mode(pf))
4855 		return;
4856 
4857 	/* initialize DDP driven features */
4858 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4859 		ice_ptp_init(pf);
4860 
4861 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4862 		ice_gnss_init(pf);
4863 
4864 	if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4865 	    ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4866 		ice_dpll_init(pf);
4867 
4868 	/* Note: Flow director init failure is non-fatal to load */
4869 	if (ice_init_fdir(pf))
4870 		dev_err(dev, "could not initialize flow director\n");
4871 
4872 	/* Note: DCB init failure is non-fatal to load */
4873 	if (ice_init_pf_dcb(pf, false)) {
4874 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4875 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4876 	} else {
4877 		ice_cfg_lldp_mib_change(&pf->hw, true);
4878 	}
4879 
4880 	if (ice_init_lag(pf))
4881 		dev_warn(dev, "Failed to init link aggregation support\n");
4882 
4883 	ice_hwmon_init(pf);
4884 }
4885 
4886 static void ice_deinit_features(struct ice_pf *pf)
4887 {
4888 	if (ice_is_safe_mode(pf))
4889 		return;
4890 
4891 	ice_deinit_lag(pf);
4892 	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4893 		ice_cfg_lldp_mib_change(&pf->hw, false);
4894 	ice_deinit_fdir(pf);
4895 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4896 		ice_gnss_exit(pf);
4897 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4898 		ice_ptp_release(pf);
4899 	if (test_bit(ICE_FLAG_DPLL, pf->flags))
4900 		ice_dpll_deinit(pf);
4901 	if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4902 		xa_destroy(&pf->eswitch.reprs);
4903 }
4904 
4905 static void ice_init_wakeup(struct ice_pf *pf)
4906 {
4907 	/* Save wakeup reason register for later use */
4908 	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4909 
4910 	/* check for a power management event */
4911 	ice_print_wake_reason(pf);
4912 
4913 	/* clear wake status, all bits */
4914 	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4915 
4916 	/* Disable WoL at init, wait for user to enable */
4917 	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4918 }
4919 
4920 static int ice_init_link(struct ice_pf *pf)
4921 {
4922 	struct device *dev = ice_pf_to_dev(pf);
4923 	int err;
4924 
4925 	err = ice_init_link_events(pf->hw.port_info);
4926 	if (err) {
4927 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4928 		return err;
4929 	}
4930 
4931 	/* not a fatal error if this fails */
4932 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4933 	if (err)
4934 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4935 
4936 	/* not a fatal error if this fails */
4937 	err = ice_update_link_info(pf->hw.port_info);
4938 	if (err)
4939 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4940 
4941 	ice_init_link_dflt_override(pf->hw.port_info);
4942 
4943 	ice_check_link_cfg_err(pf,
4944 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4945 
4946 	/* if media available, initialize PHY settings */
4947 	if (pf->hw.port_info->phy.link_info.link_info &
4948 	    ICE_AQ_MEDIA_AVAILABLE) {
4949 		/* not a fatal error if this fails */
4950 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4951 		if (err)
4952 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4953 
4954 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4955 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4956 
4957 			if (vsi)
4958 				ice_configure_phy(vsi);
4959 		}
4960 	} else {
4961 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4962 	}
4963 
4964 	return err;
4965 }
4966 
4967 static int ice_init_pf_sw(struct ice_pf *pf)
4968 {
4969 	bool dvm = ice_is_dvm_ena(&pf->hw);
4970 	struct ice_vsi *vsi;
4971 	int err;
4972 
4973 	/* create switch struct for the switch element created by FW on boot */
4974 	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4975 	if (!pf->first_sw)
4976 		return -ENOMEM;
4977 
4978 	if (pf->hw.evb_veb)
4979 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4980 	else
4981 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4982 
4983 	pf->first_sw->pf = pf;
4984 
4985 	/* record the sw_id available for later use */
4986 	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4987 
4988 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4989 	if (err)
4990 		goto err_aq_set_port_params;
4991 
4992 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4993 	if (!vsi) {
4994 		err = -ENOMEM;
4995 		goto err_pf_vsi_setup;
4996 	}
4997 
4998 	return 0;
4999 
5000 err_pf_vsi_setup:
5001 err_aq_set_port_params:
5002 	kfree(pf->first_sw);
5003 	return err;
5004 }
5005 
5006 static void ice_deinit_pf_sw(struct ice_pf *pf)
5007 {
5008 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
5009 
5010 	if (!vsi)
5011 		return;
5012 
5013 	ice_vsi_release(vsi);
5014 	kfree(pf->first_sw);
5015 }
5016 
5017 static int ice_alloc_vsis(struct ice_pf *pf)
5018 {
5019 	struct device *dev = ice_pf_to_dev(pf);
5020 
5021 	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
5022 	if (!pf->num_alloc_vsi)
5023 		return -EIO;
5024 
5025 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
5026 		dev_warn(dev,
5027 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
5028 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
5029 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
5030 	}
5031 
5032 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
5033 			       GFP_KERNEL);
5034 	if (!pf->vsi)
5035 		return -ENOMEM;
5036 
5037 	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
5038 				     sizeof(*pf->vsi_stats), GFP_KERNEL);
5039 	if (!pf->vsi_stats) {
5040 		devm_kfree(dev, pf->vsi);
5041 		return -ENOMEM;
5042 	}
5043 
5044 	return 0;
5045 }
5046 
5047 static void ice_dealloc_vsis(struct ice_pf *pf)
5048 {
5049 	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
5050 	pf->vsi_stats = NULL;
5051 
5052 	pf->num_alloc_vsi = 0;
5053 	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
5054 	pf->vsi = NULL;
5055 }
5056 
5057 static int ice_init_devlink(struct ice_pf *pf)
5058 {
5059 	int err;
5060 
5061 	err = ice_devlink_register_params(pf);
5062 	if (err)
5063 		return err;
5064 
5065 	ice_devlink_init_regions(pf);
5066 	ice_devlink_register(pf);
5067 
5068 	return 0;
5069 }
5070 
5071 static void ice_deinit_devlink(struct ice_pf *pf)
5072 {
5073 	ice_devlink_unregister(pf);
5074 	ice_devlink_destroy_regions(pf);
5075 	ice_devlink_unregister_params(pf);
5076 }
5077 
5078 static int ice_init(struct ice_pf *pf)
5079 {
5080 	int err;
5081 
5082 	err = ice_init_dev(pf);
5083 	if (err)
5084 		return err;
5085 
5086 	err = ice_alloc_vsis(pf);
5087 	if (err)
5088 		goto err_alloc_vsis;
5089 
5090 	err = ice_init_pf_sw(pf);
5091 	if (err)
5092 		goto err_init_pf_sw;
5093 
5094 	ice_init_wakeup(pf);
5095 
5096 	err = ice_init_link(pf);
5097 	if (err)
5098 		goto err_init_link;
5099 
5100 	err = ice_send_version(pf);
5101 	if (err)
5102 		goto err_init_link;
5103 
5104 	ice_verify_cacheline_size(pf);
5105 
5106 	if (ice_is_safe_mode(pf))
5107 		ice_set_safe_mode_vlan_cfg(pf);
5108 	else
5109 		/* print PCI link speed and width */
5110 		pcie_print_link_status(pf->pdev);
5111 
5112 	/* ready to go, so clear down state bit */
5113 	clear_bit(ICE_DOWN, pf->state);
5114 	clear_bit(ICE_SERVICE_DIS, pf->state);
5115 
5116 	/* since everything is good, start the service timer */
5117 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5118 
5119 	return 0;
5120 
5121 err_init_link:
5122 	ice_deinit_pf_sw(pf);
5123 err_init_pf_sw:
5124 	ice_dealloc_vsis(pf);
5125 err_alloc_vsis:
5126 	ice_deinit_dev(pf);
5127 	return err;
5128 }
5129 
5130 static void ice_deinit(struct ice_pf *pf)
5131 {
5132 	set_bit(ICE_SERVICE_DIS, pf->state);
5133 	set_bit(ICE_DOWN, pf->state);
5134 
5135 	ice_deinit_pf_sw(pf);
5136 	ice_dealloc_vsis(pf);
5137 	ice_deinit_dev(pf);
5138 }
5139 
5140 /**
5141  * ice_load - load pf by init hw and starting VSI
5142  * @pf: pointer to the pf instance
5143  *
5144  * This function has to be called under devl_lock.
5145  */
5146 int ice_load(struct ice_pf *pf)
5147 {
5148 	struct ice_vsi *vsi;
5149 	int err;
5150 
5151 	devl_assert_locked(priv_to_devlink(pf));
5152 
5153 	vsi = ice_get_main_vsi(pf);
5154 
5155 	/* init channel list */
5156 	INIT_LIST_HEAD(&vsi->ch_list);
5157 
5158 	err = ice_cfg_netdev(vsi);
5159 	if (err)
5160 		return err;
5161 
5162 	/* Setup DCB netlink interface */
5163 	ice_dcbnl_setup(vsi);
5164 
5165 	err = ice_init_mac_fltr(pf);
5166 	if (err)
5167 		goto err_init_mac_fltr;
5168 
5169 	err = ice_devlink_create_pf_port(pf);
5170 	if (err)
5171 		goto err_devlink_create_pf_port;
5172 
5173 	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
5174 
5175 	err = ice_register_netdev(vsi);
5176 	if (err)
5177 		goto err_register_netdev;
5178 
5179 	err = ice_tc_indir_block_register(vsi);
5180 	if (err)
5181 		goto err_tc_indir_block_register;
5182 
5183 	ice_napi_add(vsi);
5184 
5185 	err = ice_init_rdma(pf);
5186 	if (err)
5187 		goto err_init_rdma;
5188 
5189 	ice_init_features(pf);
5190 	ice_service_task_restart(pf);
5191 
5192 	clear_bit(ICE_DOWN, pf->state);
5193 
5194 	return 0;
5195 
5196 err_init_rdma:
5197 	ice_tc_indir_block_unregister(vsi);
5198 err_tc_indir_block_register:
5199 	ice_unregister_netdev(vsi);
5200 err_register_netdev:
5201 	ice_devlink_destroy_pf_port(pf);
5202 err_devlink_create_pf_port:
5203 err_init_mac_fltr:
5204 	ice_decfg_netdev(vsi);
5205 	return err;
5206 }
5207 
5208 /**
5209  * ice_unload - unload pf by stopping VSI and deinit hw
5210  * @pf: pointer to the pf instance
5211  *
5212  * This function has to be called under devl_lock.
5213  */
5214 void ice_unload(struct ice_pf *pf)
5215 {
5216 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
5217 
5218 	devl_assert_locked(priv_to_devlink(pf));
5219 
5220 	ice_deinit_features(pf);
5221 	ice_deinit_rdma(pf);
5222 	ice_tc_indir_block_unregister(vsi);
5223 	ice_unregister_netdev(vsi);
5224 	ice_devlink_destroy_pf_port(pf);
5225 	ice_decfg_netdev(vsi);
5226 }
5227 
5228 /**
5229  * ice_probe - Device initialization routine
5230  * @pdev: PCI device information struct
5231  * @ent: entry in ice_pci_tbl
5232  *
5233  * Returns 0 on success, negative on failure
5234  */
5235 static int
5236 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5237 {
5238 	struct device *dev = &pdev->dev;
5239 	struct ice_adapter *adapter;
5240 	struct ice_pf *pf;
5241 	struct ice_hw *hw;
5242 	int err;
5243 
5244 	if (pdev->is_virtfn) {
5245 		dev_err(dev, "can't probe a virtual function\n");
5246 		return -EINVAL;
5247 	}
5248 
5249 	/* when under a kdump kernel initiate a reset before enabling the
5250 	 * device in order to clear out any pending DMA transactions. These
5251 	 * transactions can cause some systems to machine check when doing
5252 	 * the pcim_enable_device() below.
5253 	 */
5254 	if (is_kdump_kernel()) {
5255 		pci_save_state(pdev);
5256 		pci_clear_master(pdev);
5257 		err = pcie_flr(pdev);
5258 		if (err)
5259 			return err;
5260 		pci_restore_state(pdev);
5261 	}
5262 
5263 	/* this driver uses devres, see
5264 	 * Documentation/driver-api/driver-model/devres.rst
5265 	 */
5266 	err = pcim_enable_device(pdev);
5267 	if (err)
5268 		return err;
5269 
5270 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5271 	if (err) {
5272 		dev_err(dev, "BAR0 I/O map error %d\n", err);
5273 		return err;
5274 	}
5275 
5276 	pf = ice_allocate_pf(dev);
5277 	if (!pf)
5278 		return -ENOMEM;
5279 
5280 	/* initialize Auxiliary index to invalid value */
5281 	pf->aux_idx = -1;
5282 
5283 	/* set up for high or low DMA */
5284 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5285 	if (err) {
5286 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5287 		return err;
5288 	}
5289 
5290 	pci_set_master(pdev);
5291 
5292 	adapter = ice_adapter_get(pdev);
5293 	if (IS_ERR(adapter))
5294 		return PTR_ERR(adapter);
5295 
5296 	pf->pdev = pdev;
5297 	pf->adapter = adapter;
5298 	pci_set_drvdata(pdev, pf);
5299 	set_bit(ICE_DOWN, pf->state);
5300 	/* Disable service task until DOWN bit is cleared */
5301 	set_bit(ICE_SERVICE_DIS, pf->state);
5302 
5303 	hw = &pf->hw;
5304 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5305 	pci_save_state(pdev);
5306 
5307 	hw->back = pf;
5308 	hw->port_info = NULL;
5309 	hw->vendor_id = pdev->vendor;
5310 	hw->device_id = pdev->device;
5311 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5312 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5313 	hw->subsystem_device_id = pdev->subsystem_device;
5314 	hw->bus.device = PCI_SLOT(pdev->devfn);
5315 	hw->bus.func = PCI_FUNC(pdev->devfn);
5316 	ice_set_ctrlq_len(hw);
5317 
5318 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5319 
5320 #ifndef CONFIG_DYNAMIC_DEBUG
5321 	if (debug < -1)
5322 		hw->debug_mask = debug;
5323 #endif
5324 
5325 	err = ice_init(pf);
5326 	if (err)
5327 		goto err_init;
5328 
5329 	devl_lock(priv_to_devlink(pf));
5330 	err = ice_load(pf);
5331 	if (err)
5332 		goto err_load;
5333 
5334 	err = ice_init_devlink(pf);
5335 	if (err)
5336 		goto err_init_devlink;
5337 	devl_unlock(priv_to_devlink(pf));
5338 
5339 	return 0;
5340 
5341 err_init_devlink:
5342 	ice_unload(pf);
5343 err_load:
5344 	devl_unlock(priv_to_devlink(pf));
5345 	ice_deinit(pf);
5346 err_init:
5347 	ice_adapter_put(pdev);
5348 	return err;
5349 }
5350 
5351 /**
5352  * ice_set_wake - enable or disable Wake on LAN
5353  * @pf: pointer to the PF struct
5354  *
5355  * Simple helper for WoL control
5356  */
5357 static void ice_set_wake(struct ice_pf *pf)
5358 {
5359 	struct ice_hw *hw = &pf->hw;
5360 	bool wol = pf->wol_ena;
5361 
5362 	/* clear wake state, otherwise new wake events won't fire */
5363 	wr32(hw, PFPM_WUS, U32_MAX);
5364 
5365 	/* enable / disable APM wake up, no RMW needed */
5366 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5367 
5368 	/* set magic packet filter enabled */
5369 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5370 }
5371 
5372 /**
5373  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5374  * @pf: pointer to the PF struct
5375  *
5376  * Issue firmware command to enable multicast magic wake, making
5377  * sure that any locally administered address (LAA) is used for
5378  * wake, and that PF reset doesn't undo the LAA.
5379  */
5380 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5381 {
5382 	struct device *dev = ice_pf_to_dev(pf);
5383 	struct ice_hw *hw = &pf->hw;
5384 	u8 mac_addr[ETH_ALEN];
5385 	struct ice_vsi *vsi;
5386 	int status;
5387 	u8 flags;
5388 
5389 	if (!pf->wol_ena)
5390 		return;
5391 
5392 	vsi = ice_get_main_vsi(pf);
5393 	if (!vsi)
5394 		return;
5395 
5396 	/* Get current MAC address in case it's an LAA */
5397 	if (vsi->netdev)
5398 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5399 	else
5400 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5401 
5402 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5403 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5404 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5405 
5406 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5407 	if (status)
5408 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5409 			status, ice_aq_str(hw->adminq.sq_last_status));
5410 }
5411 
5412 /**
5413  * ice_remove - Device removal routine
5414  * @pdev: PCI device information struct
5415  */
5416 static void ice_remove(struct pci_dev *pdev)
5417 {
5418 	struct ice_pf *pf = pci_get_drvdata(pdev);
5419 	int i;
5420 
5421 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5422 		if (!ice_is_reset_in_progress(pf->state))
5423 			break;
5424 		msleep(100);
5425 	}
5426 
5427 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5428 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5429 		ice_free_vfs(pf);
5430 	}
5431 
5432 	ice_hwmon_exit(pf);
5433 
5434 	ice_service_task_stop(pf);
5435 	ice_aq_cancel_waiting_tasks(pf);
5436 	set_bit(ICE_DOWN, pf->state);
5437 
5438 	if (!ice_is_safe_mode(pf))
5439 		ice_remove_arfs(pf);
5440 
5441 	devl_lock(priv_to_devlink(pf));
5442 	ice_dealloc_all_dynamic_ports(pf);
5443 	ice_deinit_devlink(pf);
5444 
5445 	ice_unload(pf);
5446 	devl_unlock(priv_to_devlink(pf));
5447 
5448 	ice_deinit(pf);
5449 	ice_vsi_release_all(pf);
5450 
5451 	ice_setup_mc_magic_wake(pf);
5452 	ice_set_wake(pf);
5453 
5454 	ice_adapter_put(pdev);
5455 }
5456 
5457 /**
5458  * ice_shutdown - PCI callback for shutting down device
5459  * @pdev: PCI device information struct
5460  */
5461 static void ice_shutdown(struct pci_dev *pdev)
5462 {
5463 	struct ice_pf *pf = pci_get_drvdata(pdev);
5464 
5465 	ice_remove(pdev);
5466 
5467 	if (system_state == SYSTEM_POWER_OFF) {
5468 		pci_wake_from_d3(pdev, pf->wol_ena);
5469 		pci_set_power_state(pdev, PCI_D3hot);
5470 	}
5471 }
5472 
5473 /**
5474  * ice_prepare_for_shutdown - prep for PCI shutdown
5475  * @pf: board private structure
5476  *
5477  * Inform or close all dependent features in prep for PCI device shutdown
5478  */
5479 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5480 {
5481 	struct ice_hw *hw = &pf->hw;
5482 	u32 v;
5483 
5484 	/* Notify VFs of impending reset */
5485 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5486 		ice_vc_notify_reset(pf);
5487 
5488 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5489 
5490 	/* disable the VSIs and their queues that are not already DOWN */
5491 	ice_pf_dis_all_vsi(pf, false);
5492 
5493 	ice_for_each_vsi(pf, v)
5494 		if (pf->vsi[v])
5495 			pf->vsi[v]->vsi_num = 0;
5496 
5497 	ice_shutdown_all_ctrlq(hw, true);
5498 }
5499 
5500 /**
5501  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5502  * @pf: board private structure to reinitialize
5503  *
5504  * This routine reinitialize interrupt scheme that was cleared during
5505  * power management suspend callback.
5506  *
5507  * This should be called during resume routine to re-allocate the q_vectors
5508  * and reacquire interrupts.
5509  */
5510 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5511 {
5512 	struct device *dev = ice_pf_to_dev(pf);
5513 	int ret, v;
5514 
5515 	/* Since we clear MSIX flag during suspend, we need to
5516 	 * set it back during resume...
5517 	 */
5518 
5519 	ret = ice_init_interrupt_scheme(pf);
5520 	if (ret) {
5521 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5522 		return ret;
5523 	}
5524 
5525 	/* Remap vectors and rings, after successful re-init interrupts */
5526 	ice_for_each_vsi(pf, v) {
5527 		if (!pf->vsi[v])
5528 			continue;
5529 
5530 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5531 		if (ret)
5532 			goto err_reinit;
5533 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5534 		rtnl_lock();
5535 		ice_vsi_set_napi_queues(pf->vsi[v]);
5536 		rtnl_unlock();
5537 	}
5538 
5539 	ret = ice_req_irq_msix_misc(pf);
5540 	if (ret) {
5541 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5542 			ret);
5543 		goto err_reinit;
5544 	}
5545 
5546 	return 0;
5547 
5548 err_reinit:
5549 	while (v--)
5550 		if (pf->vsi[v]) {
5551 			rtnl_lock();
5552 			ice_vsi_clear_napi_queues(pf->vsi[v]);
5553 			rtnl_unlock();
5554 			ice_vsi_free_q_vectors(pf->vsi[v]);
5555 		}
5556 
5557 	return ret;
5558 }
5559 
5560 /**
5561  * ice_suspend
5562  * @dev: generic device information structure
5563  *
5564  * Power Management callback to quiesce the device and prepare
5565  * for D3 transition.
5566  */
5567 static int ice_suspend(struct device *dev)
5568 {
5569 	struct pci_dev *pdev = to_pci_dev(dev);
5570 	struct ice_pf *pf;
5571 	int disabled, v;
5572 
5573 	pf = pci_get_drvdata(pdev);
5574 
5575 	if (!ice_pf_state_is_nominal(pf)) {
5576 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5577 		return -EBUSY;
5578 	}
5579 
5580 	/* Stop watchdog tasks until resume completion.
5581 	 * Even though it is most likely that the service task is
5582 	 * disabled if the device is suspended or down, the service task's
5583 	 * state is controlled by a different state bit, and we should
5584 	 * store and honor whatever state that bit is in at this point.
5585 	 */
5586 	disabled = ice_service_task_stop(pf);
5587 
5588 	ice_deinit_rdma(pf);
5589 
5590 	/* Already suspended?, then there is nothing to do */
5591 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5592 		if (!disabled)
5593 			ice_service_task_restart(pf);
5594 		return 0;
5595 	}
5596 
5597 	if (test_bit(ICE_DOWN, pf->state) ||
5598 	    ice_is_reset_in_progress(pf->state)) {
5599 		dev_err(dev, "can't suspend device in reset or already down\n");
5600 		if (!disabled)
5601 			ice_service_task_restart(pf);
5602 		return 0;
5603 	}
5604 
5605 	ice_setup_mc_magic_wake(pf);
5606 
5607 	ice_prepare_for_shutdown(pf);
5608 
5609 	ice_set_wake(pf);
5610 
5611 	/* Free vectors, clear the interrupt scheme and release IRQs
5612 	 * for proper hibernation, especially with large number of CPUs.
5613 	 * Otherwise hibernation might fail when mapping all the vectors back
5614 	 * to CPU0.
5615 	 */
5616 	ice_free_irq_msix_misc(pf);
5617 	ice_for_each_vsi(pf, v) {
5618 		if (!pf->vsi[v])
5619 			continue;
5620 		rtnl_lock();
5621 		ice_vsi_clear_napi_queues(pf->vsi[v]);
5622 		rtnl_unlock();
5623 		ice_vsi_free_q_vectors(pf->vsi[v]);
5624 	}
5625 	ice_clear_interrupt_scheme(pf);
5626 
5627 	pci_save_state(pdev);
5628 	pci_wake_from_d3(pdev, pf->wol_ena);
5629 	pci_set_power_state(pdev, PCI_D3hot);
5630 	return 0;
5631 }
5632 
5633 /**
5634  * ice_resume - PM callback for waking up from D3
5635  * @dev: generic device information structure
5636  */
5637 static int ice_resume(struct device *dev)
5638 {
5639 	struct pci_dev *pdev = to_pci_dev(dev);
5640 	enum ice_reset_req reset_type;
5641 	struct ice_pf *pf;
5642 	struct ice_hw *hw;
5643 	int ret;
5644 
5645 	pci_set_power_state(pdev, PCI_D0);
5646 	pci_restore_state(pdev);
5647 	pci_save_state(pdev);
5648 
5649 	if (!pci_device_is_present(pdev))
5650 		return -ENODEV;
5651 
5652 	ret = pci_enable_device_mem(pdev);
5653 	if (ret) {
5654 		dev_err(dev, "Cannot enable device after suspend\n");
5655 		return ret;
5656 	}
5657 
5658 	pf = pci_get_drvdata(pdev);
5659 	hw = &pf->hw;
5660 
5661 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5662 	ice_print_wake_reason(pf);
5663 
5664 	/* We cleared the interrupt scheme when we suspended, so we need to
5665 	 * restore it now to resume device functionality.
5666 	 */
5667 	ret = ice_reinit_interrupt_scheme(pf);
5668 	if (ret)
5669 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5670 
5671 	ret = ice_init_rdma(pf);
5672 	if (ret)
5673 		dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5674 			ret);
5675 
5676 	clear_bit(ICE_DOWN, pf->state);
5677 	/* Now perform PF reset and rebuild */
5678 	reset_type = ICE_RESET_PFR;
5679 	/* re-enable service task for reset, but allow reset to schedule it */
5680 	clear_bit(ICE_SERVICE_DIS, pf->state);
5681 
5682 	if (ice_schedule_reset(pf, reset_type))
5683 		dev_err(dev, "Reset during resume failed.\n");
5684 
5685 	clear_bit(ICE_SUSPENDED, pf->state);
5686 	ice_service_task_restart(pf);
5687 
5688 	/* Restart the service task */
5689 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5690 
5691 	return 0;
5692 }
5693 
5694 /**
5695  * ice_pci_err_detected - warning that PCI error has been detected
5696  * @pdev: PCI device information struct
5697  * @err: the type of PCI error
5698  *
5699  * Called to warn that something happened on the PCI bus and the error handling
5700  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5701  */
5702 static pci_ers_result_t
5703 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5704 {
5705 	struct ice_pf *pf = pci_get_drvdata(pdev);
5706 
5707 	if (!pf) {
5708 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5709 			__func__, err);
5710 		return PCI_ERS_RESULT_DISCONNECT;
5711 	}
5712 
5713 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5714 		ice_service_task_stop(pf);
5715 
5716 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5717 			set_bit(ICE_PFR_REQ, pf->state);
5718 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5719 		}
5720 	}
5721 
5722 	return PCI_ERS_RESULT_NEED_RESET;
5723 }
5724 
5725 /**
5726  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5727  * @pdev: PCI device information struct
5728  *
5729  * Called to determine if the driver can recover from the PCI slot reset by
5730  * using a register read to determine if the device is recoverable.
5731  */
5732 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5733 {
5734 	struct ice_pf *pf = pci_get_drvdata(pdev);
5735 	pci_ers_result_t result;
5736 	int err;
5737 	u32 reg;
5738 
5739 	err = pci_enable_device_mem(pdev);
5740 	if (err) {
5741 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5742 			err);
5743 		result = PCI_ERS_RESULT_DISCONNECT;
5744 	} else {
5745 		pci_set_master(pdev);
5746 		pci_restore_state(pdev);
5747 		pci_save_state(pdev);
5748 		pci_wake_from_d3(pdev, false);
5749 
5750 		/* Check for life */
5751 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5752 		if (!reg)
5753 			result = PCI_ERS_RESULT_RECOVERED;
5754 		else
5755 			result = PCI_ERS_RESULT_DISCONNECT;
5756 	}
5757 
5758 	return result;
5759 }
5760 
5761 /**
5762  * ice_pci_err_resume - restart operations after PCI error recovery
5763  * @pdev: PCI device information struct
5764  *
5765  * Called to allow the driver to bring things back up after PCI error and/or
5766  * reset recovery have finished
5767  */
5768 static void ice_pci_err_resume(struct pci_dev *pdev)
5769 {
5770 	struct ice_pf *pf = pci_get_drvdata(pdev);
5771 
5772 	if (!pf) {
5773 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5774 			__func__);
5775 		return;
5776 	}
5777 
5778 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5779 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5780 			__func__);
5781 		return;
5782 	}
5783 
5784 	ice_restore_all_vfs_msi_state(pf);
5785 
5786 	ice_do_reset(pf, ICE_RESET_PFR);
5787 	ice_service_task_restart(pf);
5788 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5789 }
5790 
5791 /**
5792  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5793  * @pdev: PCI device information struct
5794  */
5795 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5796 {
5797 	struct ice_pf *pf = pci_get_drvdata(pdev);
5798 
5799 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5800 		ice_service_task_stop(pf);
5801 
5802 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5803 			set_bit(ICE_PFR_REQ, pf->state);
5804 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5805 		}
5806 	}
5807 }
5808 
5809 /**
5810  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5811  * @pdev: PCI device information struct
5812  */
5813 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5814 {
5815 	ice_pci_err_resume(pdev);
5816 }
5817 
5818 /* ice_pci_tbl - PCI Device ID Table
5819  *
5820  * Wildcard entries (PCI_ANY_ID) should come last
5821  * Last entry must be all 0s
5822  *
5823  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5824  *   Class, Class Mask, private data (not used) }
5825  */
5826 static const struct pci_device_id ice_pci_tbl[] = {
5827 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5828 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5829 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5830 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5831 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5832 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5833 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5834 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5835 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5836 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5837 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5838 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5839 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5840 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5841 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5842 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5843 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5844 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5845 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5846 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5847 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5848 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5849 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5850 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5851 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5852 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
5853 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
5854 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
5855 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
5856 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
5857 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
5858 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
5859 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
5860 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
5861 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
5862 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
5863 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
5864 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
5865 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
5866 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
5867 	/* required last entry */
5868 	{}
5869 };
5870 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5871 
5872 static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5873 
5874 static const struct pci_error_handlers ice_pci_err_handler = {
5875 	.error_detected = ice_pci_err_detected,
5876 	.slot_reset = ice_pci_err_slot_reset,
5877 	.reset_prepare = ice_pci_err_reset_prepare,
5878 	.reset_done = ice_pci_err_reset_done,
5879 	.resume = ice_pci_err_resume
5880 };
5881 
5882 static struct pci_driver ice_driver = {
5883 	.name = KBUILD_MODNAME,
5884 	.id_table = ice_pci_tbl,
5885 	.probe = ice_probe,
5886 	.remove = ice_remove,
5887 	.driver.pm = pm_sleep_ptr(&ice_pm_ops),
5888 	.shutdown = ice_shutdown,
5889 	.sriov_configure = ice_sriov_configure,
5890 	.sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5891 	.sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
5892 	.err_handler = &ice_pci_err_handler
5893 };
5894 
5895 /**
5896  * ice_module_init - Driver registration routine
5897  *
5898  * ice_module_init is the first routine called when the driver is
5899  * loaded. All it does is register with the PCI subsystem.
5900  */
5901 static int __init ice_module_init(void)
5902 {
5903 	int status = -ENOMEM;
5904 
5905 	pr_info("%s\n", ice_driver_string);
5906 	pr_info("%s\n", ice_copyright);
5907 
5908 	ice_adv_lnk_speed_maps_init();
5909 
5910 	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5911 	if (!ice_wq) {
5912 		pr_err("Failed to create workqueue\n");
5913 		return status;
5914 	}
5915 
5916 	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5917 	if (!ice_lag_wq) {
5918 		pr_err("Failed to create LAG workqueue\n");
5919 		goto err_dest_wq;
5920 	}
5921 
5922 	ice_debugfs_init();
5923 
5924 	status = pci_register_driver(&ice_driver);
5925 	if (status) {
5926 		pr_err("failed to register PCI driver, err %d\n", status);
5927 		goto err_dest_lag_wq;
5928 	}
5929 
5930 	status = ice_sf_driver_register();
5931 	if (status) {
5932 		pr_err("Failed to register SF driver, err %d\n", status);
5933 		goto err_sf_driver;
5934 	}
5935 
5936 	return 0;
5937 
5938 err_sf_driver:
5939 	pci_unregister_driver(&ice_driver);
5940 err_dest_lag_wq:
5941 	destroy_workqueue(ice_lag_wq);
5942 	ice_debugfs_exit();
5943 err_dest_wq:
5944 	destroy_workqueue(ice_wq);
5945 	return status;
5946 }
5947 module_init(ice_module_init);
5948 
5949 /**
5950  * ice_module_exit - Driver exit cleanup routine
5951  *
5952  * ice_module_exit is called just before the driver is removed
5953  * from memory.
5954  */
5955 static void __exit ice_module_exit(void)
5956 {
5957 	ice_sf_driver_unregister();
5958 	pci_unregister_driver(&ice_driver);
5959 	ice_debugfs_exit();
5960 	destroy_workqueue(ice_wq);
5961 	destroy_workqueue(ice_lag_wq);
5962 	pr_info("module unloaded\n");
5963 }
5964 module_exit(ice_module_exit);
5965 
5966 /**
5967  * ice_set_mac_address - NDO callback to set MAC address
5968  * @netdev: network interface device structure
5969  * @pi: pointer to an address structure
5970  *
5971  * Returns 0 on success, negative on failure
5972  */
5973 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5974 {
5975 	struct ice_netdev_priv *np = netdev_priv(netdev);
5976 	struct ice_vsi *vsi = np->vsi;
5977 	struct ice_pf *pf = vsi->back;
5978 	struct ice_hw *hw = &pf->hw;
5979 	struct sockaddr *addr = pi;
5980 	u8 old_mac[ETH_ALEN];
5981 	u8 flags = 0;
5982 	u8 *mac;
5983 	int err;
5984 
5985 	mac = (u8 *)addr->sa_data;
5986 
5987 	if (!is_valid_ether_addr(mac))
5988 		return -EADDRNOTAVAIL;
5989 
5990 	if (test_bit(ICE_DOWN, pf->state) ||
5991 	    ice_is_reset_in_progress(pf->state)) {
5992 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5993 			   mac);
5994 		return -EBUSY;
5995 	}
5996 
5997 	if (ice_chnl_dmac_fltr_cnt(pf)) {
5998 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5999 			   mac);
6000 		return -EAGAIN;
6001 	}
6002 
6003 	netif_addr_lock_bh(netdev);
6004 	ether_addr_copy(old_mac, netdev->dev_addr);
6005 	/* change the netdev's MAC address */
6006 	eth_hw_addr_set(netdev, mac);
6007 	netif_addr_unlock_bh(netdev);
6008 
6009 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
6010 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
6011 	if (err && err != -ENOENT) {
6012 		err = -EADDRNOTAVAIL;
6013 		goto err_update_filters;
6014 	}
6015 
6016 	/* Add filter for new MAC. If filter exists, return success */
6017 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
6018 	if (err == -EEXIST) {
6019 		/* Although this MAC filter is already present in hardware it's
6020 		 * possible in some cases (e.g. bonding) that dev_addr was
6021 		 * modified outside of the driver and needs to be restored back
6022 		 * to this value.
6023 		 */
6024 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
6025 
6026 		return 0;
6027 	} else if (err) {
6028 		/* error if the new filter addition failed */
6029 		err = -EADDRNOTAVAIL;
6030 	}
6031 
6032 err_update_filters:
6033 	if (err) {
6034 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
6035 			   mac);
6036 		netif_addr_lock_bh(netdev);
6037 		eth_hw_addr_set(netdev, old_mac);
6038 		netif_addr_unlock_bh(netdev);
6039 		return err;
6040 	}
6041 
6042 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
6043 		   netdev->dev_addr);
6044 
6045 	/* write new MAC address to the firmware */
6046 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
6047 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
6048 	if (err) {
6049 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
6050 			   mac, err);
6051 	}
6052 	return 0;
6053 }
6054 
6055 /**
6056  * ice_set_rx_mode - NDO callback to set the netdev filters
6057  * @netdev: network interface device structure
6058  */
6059 static void ice_set_rx_mode(struct net_device *netdev)
6060 {
6061 	struct ice_netdev_priv *np = netdev_priv(netdev);
6062 	struct ice_vsi *vsi = np->vsi;
6063 
6064 	if (!vsi || ice_is_switchdev_running(vsi->back))
6065 		return;
6066 
6067 	/* Set the flags to synchronize filters
6068 	 * ndo_set_rx_mode may be triggered even without a change in netdev
6069 	 * flags
6070 	 */
6071 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
6072 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
6073 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
6074 
6075 	/* schedule our worker thread which will take care of
6076 	 * applying the new filter changes
6077 	 */
6078 	ice_service_task_schedule(vsi->back);
6079 }
6080 
6081 /**
6082  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
6083  * @netdev: network interface device structure
6084  * @queue_index: Queue ID
6085  * @maxrate: maximum bandwidth in Mbps
6086  */
6087 static int
6088 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
6089 {
6090 	struct ice_netdev_priv *np = netdev_priv(netdev);
6091 	struct ice_vsi *vsi = np->vsi;
6092 	u16 q_handle;
6093 	int status;
6094 	u8 tc;
6095 
6096 	/* Validate maxrate requested is within permitted range */
6097 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
6098 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
6099 			   maxrate, queue_index);
6100 		return -EINVAL;
6101 	}
6102 
6103 	q_handle = vsi->tx_rings[queue_index]->q_handle;
6104 	tc = ice_dcb_get_tc(vsi, queue_index);
6105 
6106 	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
6107 	if (!vsi) {
6108 		netdev_err(netdev, "Invalid VSI for given queue %d\n",
6109 			   queue_index);
6110 		return -EINVAL;
6111 	}
6112 
6113 	/* Set BW back to default, when user set maxrate to 0 */
6114 	if (!maxrate)
6115 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6116 					       q_handle, ICE_MAX_BW);
6117 	else
6118 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6119 					  q_handle, ICE_MAX_BW, maxrate * 1000);
6120 	if (status)
6121 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6122 			   status);
6123 
6124 	return status;
6125 }
6126 
6127 /**
6128  * ice_fdb_add - add an entry to the hardware database
6129  * @ndm: the input from the stack
6130  * @tb: pointer to array of nladdr (unused)
6131  * @dev: the net device pointer
6132  * @addr: the MAC address entry being added
6133  * @vid: VLAN ID
6134  * @flags: instructions from stack about fdb operation
6135  * @extack: netlink extended ack
6136  */
6137 static int
6138 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6139 	    struct net_device *dev, const unsigned char *addr, u16 vid,
6140 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
6141 {
6142 	int err;
6143 
6144 	if (vid) {
6145 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6146 		return -EINVAL;
6147 	}
6148 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6149 		netdev_err(dev, "FDB only supports static addresses\n");
6150 		return -EINVAL;
6151 	}
6152 
6153 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6154 		err = dev_uc_add_excl(dev, addr);
6155 	else if (is_multicast_ether_addr(addr))
6156 		err = dev_mc_add_excl(dev, addr);
6157 	else
6158 		err = -EINVAL;
6159 
6160 	/* Only return duplicate errors if NLM_F_EXCL is set */
6161 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
6162 		err = 0;
6163 
6164 	return err;
6165 }
6166 
6167 /**
6168  * ice_fdb_del - delete an entry from the hardware database
6169  * @ndm: the input from the stack
6170  * @tb: pointer to array of nladdr (unused)
6171  * @dev: the net device pointer
6172  * @addr: the MAC address entry being added
6173  * @vid: VLAN ID
6174  * @extack: netlink extended ack
6175  */
6176 static int
6177 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6178 	    struct net_device *dev, const unsigned char *addr,
6179 	    __always_unused u16 vid, struct netlink_ext_ack *extack)
6180 {
6181 	int err;
6182 
6183 	if (ndm->ndm_state & NUD_PERMANENT) {
6184 		netdev_err(dev, "FDB only supports static addresses\n");
6185 		return -EINVAL;
6186 	}
6187 
6188 	if (is_unicast_ether_addr(addr))
6189 		err = dev_uc_del(dev, addr);
6190 	else if (is_multicast_ether_addr(addr))
6191 		err = dev_mc_del(dev, addr);
6192 	else
6193 		err = -EINVAL;
6194 
6195 	return err;
6196 }
6197 
6198 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6199 					 NETIF_F_HW_VLAN_CTAG_TX | \
6200 					 NETIF_F_HW_VLAN_STAG_RX | \
6201 					 NETIF_F_HW_VLAN_STAG_TX)
6202 
6203 #define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6204 					 NETIF_F_HW_VLAN_STAG_RX)
6205 
6206 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
6207 					 NETIF_F_HW_VLAN_STAG_FILTER)
6208 
6209 /**
6210  * ice_fix_features - fix the netdev features flags based on device limitations
6211  * @netdev: ptr to the netdev that flags are being fixed on
6212  * @features: features that need to be checked and possibly fixed
6213  *
6214  * Make sure any fixups are made to features in this callback. This enables the
6215  * driver to not have to check unsupported configurations throughout the driver
6216  * because that's the responsiblity of this callback.
6217  *
6218  * Single VLAN Mode (SVM) Supported Features:
6219  *	NETIF_F_HW_VLAN_CTAG_FILTER
6220  *	NETIF_F_HW_VLAN_CTAG_RX
6221  *	NETIF_F_HW_VLAN_CTAG_TX
6222  *
6223  * Double VLAN Mode (DVM) Supported Features:
6224  *	NETIF_F_HW_VLAN_CTAG_FILTER
6225  *	NETIF_F_HW_VLAN_CTAG_RX
6226  *	NETIF_F_HW_VLAN_CTAG_TX
6227  *
6228  *	NETIF_F_HW_VLAN_STAG_FILTER
6229  *	NETIF_HW_VLAN_STAG_RX
6230  *	NETIF_HW_VLAN_STAG_TX
6231  *
6232  * Features that need fixing:
6233  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6234  *	These are mutually exlusive as the VSI context cannot support multiple
6235  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
6236  *	is not done, then default to clearing the requested STAG offload
6237  *	settings.
6238  *
6239  *	All supported filtering has to be enabled or disabled together. For
6240  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6241  *	together. If this is not done, then default to VLAN filtering disabled.
6242  *	These are mutually exclusive as there is currently no way to
6243  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6244  *	prune rules.
6245  */
6246 static netdev_features_t
6247 ice_fix_features(struct net_device *netdev, netdev_features_t features)
6248 {
6249 	struct ice_netdev_priv *np = netdev_priv(netdev);
6250 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6251 	bool cur_ctag, cur_stag, req_ctag, req_stag;
6252 
6253 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6254 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6255 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6256 
6257 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6258 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6259 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6260 
6261 	if (req_vlan_fltr != cur_vlan_fltr) {
6262 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6263 			if (req_ctag && req_stag) {
6264 				features |= NETIF_VLAN_FILTERING_FEATURES;
6265 			} else if (!req_ctag && !req_stag) {
6266 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6267 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
6268 				   (!cur_stag && req_stag && !cur_ctag)) {
6269 				features |= NETIF_VLAN_FILTERING_FEATURES;
6270 				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
6271 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
6272 				   (cur_stag && !req_stag && cur_ctag)) {
6273 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6274 				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
6275 			}
6276 		} else {
6277 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6278 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6279 
6280 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6281 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6282 		}
6283 	}
6284 
6285 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6286 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6287 		netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
6288 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6289 			      NETIF_F_HW_VLAN_STAG_TX);
6290 	}
6291 
6292 	if (!(netdev->features & NETIF_F_RXFCS) &&
6293 	    (features & NETIF_F_RXFCS) &&
6294 	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6295 	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6296 		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6297 		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6298 	}
6299 
6300 	return features;
6301 }
6302 
6303 /**
6304  * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6305  * @vsi: PF's VSI
6306  * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6307  *
6308  * Store current stripped VLAN proto in ring packet context,
6309  * so it can be accessed more efficiently by packet processing code.
6310  */
6311 static void
6312 ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6313 {
6314 	u16 i;
6315 
6316 	ice_for_each_alloc_rxq(vsi, i)
6317 		vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6318 }
6319 
6320 /**
6321  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6322  * @vsi: PF's VSI
6323  * @features: features used to determine VLAN offload settings
6324  *
6325  * First, determine the vlan_ethertype based on the VLAN offload bits in
6326  * features. Then determine if stripping and insertion should be enabled or
6327  * disabled. Finally enable or disable VLAN stripping and insertion.
6328  */
6329 static int
6330 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6331 {
6332 	bool enable_stripping = true, enable_insertion = true;
6333 	struct ice_vsi_vlan_ops *vlan_ops;
6334 	int strip_err = 0, insert_err = 0;
6335 	u16 vlan_ethertype = 0;
6336 
6337 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6338 
6339 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6340 		vlan_ethertype = ETH_P_8021AD;
6341 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6342 		vlan_ethertype = ETH_P_8021Q;
6343 
6344 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6345 		enable_stripping = false;
6346 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6347 		enable_insertion = false;
6348 
6349 	if (enable_stripping)
6350 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6351 	else
6352 		strip_err = vlan_ops->dis_stripping(vsi);
6353 
6354 	if (enable_insertion)
6355 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6356 	else
6357 		insert_err = vlan_ops->dis_insertion(vsi);
6358 
6359 	if (strip_err || insert_err)
6360 		return -EIO;
6361 
6362 	ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6363 				    htons(vlan_ethertype) : 0);
6364 
6365 	return 0;
6366 }
6367 
6368 /**
6369  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6370  * @vsi: PF's VSI
6371  * @features: features used to determine VLAN filtering settings
6372  *
6373  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6374  * features.
6375  */
6376 static int
6377 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6378 {
6379 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6380 	int err = 0;
6381 
6382 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6383 	 * if either bit is set
6384 	 */
6385 	if (features &
6386 	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6387 		err = vlan_ops->ena_rx_filtering(vsi);
6388 	else
6389 		err = vlan_ops->dis_rx_filtering(vsi);
6390 
6391 	return err;
6392 }
6393 
6394 /**
6395  * ice_set_vlan_features - set VLAN settings based on suggested feature set
6396  * @netdev: ptr to the netdev being adjusted
6397  * @features: the feature set that the stack is suggesting
6398  *
6399  * Only update VLAN settings if the requested_vlan_features are different than
6400  * the current_vlan_features.
6401  */
6402 static int
6403 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6404 {
6405 	netdev_features_t current_vlan_features, requested_vlan_features;
6406 	struct ice_netdev_priv *np = netdev_priv(netdev);
6407 	struct ice_vsi *vsi = np->vsi;
6408 	int err;
6409 
6410 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6411 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6412 	if (current_vlan_features ^ requested_vlan_features) {
6413 		if ((features & NETIF_F_RXFCS) &&
6414 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6415 			dev_err(ice_pf_to_dev(vsi->back),
6416 				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6417 			return -EIO;
6418 		}
6419 
6420 		err = ice_set_vlan_offload_features(vsi, features);
6421 		if (err)
6422 			return err;
6423 	}
6424 
6425 	current_vlan_features = netdev->features &
6426 		NETIF_VLAN_FILTERING_FEATURES;
6427 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6428 	if (current_vlan_features ^ requested_vlan_features) {
6429 		err = ice_set_vlan_filtering_features(vsi, features);
6430 		if (err)
6431 			return err;
6432 	}
6433 
6434 	return 0;
6435 }
6436 
6437 /**
6438  * ice_set_loopback - turn on/off loopback mode on underlying PF
6439  * @vsi: ptr to VSI
6440  * @ena: flag to indicate the on/off setting
6441  */
6442 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6443 {
6444 	bool if_running = netif_running(vsi->netdev);
6445 	int ret;
6446 
6447 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6448 		ret = ice_down(vsi);
6449 		if (ret) {
6450 			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6451 			return ret;
6452 		}
6453 	}
6454 	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6455 	if (ret)
6456 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6457 	if (if_running)
6458 		ret = ice_up(vsi);
6459 
6460 	return ret;
6461 }
6462 
6463 /**
6464  * ice_set_features - set the netdev feature flags
6465  * @netdev: ptr to the netdev being adjusted
6466  * @features: the feature set that the stack is suggesting
6467  */
6468 static int
6469 ice_set_features(struct net_device *netdev, netdev_features_t features)
6470 {
6471 	netdev_features_t changed = netdev->features ^ features;
6472 	struct ice_netdev_priv *np = netdev_priv(netdev);
6473 	struct ice_vsi *vsi = np->vsi;
6474 	struct ice_pf *pf = vsi->back;
6475 	int ret = 0;
6476 
6477 	/* Don't set any netdev advanced features with device in Safe Mode */
6478 	if (ice_is_safe_mode(pf)) {
6479 		dev_err(ice_pf_to_dev(pf),
6480 			"Device is in Safe Mode - not enabling advanced netdev features\n");
6481 		return ret;
6482 	}
6483 
6484 	/* Do not change setting during reset */
6485 	if (ice_is_reset_in_progress(pf->state)) {
6486 		dev_err(ice_pf_to_dev(pf),
6487 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6488 		return -EBUSY;
6489 	}
6490 
6491 	/* Multiple features can be changed in one call so keep features in
6492 	 * separate if/else statements to guarantee each feature is checked
6493 	 */
6494 	if (changed & NETIF_F_RXHASH)
6495 		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6496 
6497 	ret = ice_set_vlan_features(netdev, features);
6498 	if (ret)
6499 		return ret;
6500 
6501 	/* Turn on receive of FCS aka CRC, and after setting this
6502 	 * flag the packet data will have the 4 byte CRC appended
6503 	 */
6504 	if (changed & NETIF_F_RXFCS) {
6505 		if ((features & NETIF_F_RXFCS) &&
6506 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6507 			dev_err(ice_pf_to_dev(vsi->back),
6508 				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6509 			return -EIO;
6510 		}
6511 
6512 		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6513 		ret = ice_down_up(vsi);
6514 		if (ret)
6515 			return ret;
6516 	}
6517 
6518 	if (changed & NETIF_F_NTUPLE) {
6519 		bool ena = !!(features & NETIF_F_NTUPLE);
6520 
6521 		ice_vsi_manage_fdir(vsi, ena);
6522 		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6523 	}
6524 
6525 	/* don't turn off hw_tc_offload when ADQ is already enabled */
6526 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6527 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6528 		return -EACCES;
6529 	}
6530 
6531 	if (changed & NETIF_F_HW_TC) {
6532 		bool ena = !!(features & NETIF_F_HW_TC);
6533 
6534 		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6535 		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6536 	}
6537 
6538 	if (changed & NETIF_F_LOOPBACK)
6539 		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6540 
6541 	return ret;
6542 }
6543 
6544 /**
6545  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6546  * @vsi: VSI to setup VLAN properties for
6547  */
6548 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6549 {
6550 	int err;
6551 
6552 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6553 	if (err)
6554 		return err;
6555 
6556 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6557 	if (err)
6558 		return err;
6559 
6560 	return ice_vsi_add_vlan_zero(vsi);
6561 }
6562 
6563 /**
6564  * ice_vsi_cfg_lan - Setup the VSI lan related config
6565  * @vsi: the VSI being configured
6566  *
6567  * Return 0 on success and negative value on error
6568  */
6569 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6570 {
6571 	int err;
6572 
6573 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6574 		ice_set_rx_mode(vsi->netdev);
6575 
6576 		err = ice_vsi_vlan_setup(vsi);
6577 		if (err)
6578 			return err;
6579 	}
6580 	ice_vsi_cfg_dcb_rings(vsi);
6581 
6582 	err = ice_vsi_cfg_lan_txqs(vsi);
6583 	if (!err && ice_is_xdp_ena_vsi(vsi))
6584 		err = ice_vsi_cfg_xdp_txqs(vsi);
6585 	if (!err)
6586 		err = ice_vsi_cfg_rxqs(vsi);
6587 
6588 	return err;
6589 }
6590 
6591 /* THEORY OF MODERATION:
6592  * The ice driver hardware works differently than the hardware that DIMLIB was
6593  * originally made for. ice hardware doesn't have packet count limits that
6594  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6595  * which is hard-coded to a limit of 250,000 ints/second.
6596  * If not using dynamic moderation, the INTRL value can be modified
6597  * by ethtool rx-usecs-high.
6598  */
6599 struct ice_dim {
6600 	/* the throttle rate for interrupts, basically worst case delay before
6601 	 * an initial interrupt fires, value is stored in microseconds.
6602 	 */
6603 	u16 itr;
6604 };
6605 
6606 /* Make a different profile for Rx that doesn't allow quite so aggressive
6607  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6608  * second.
6609  */
6610 static const struct ice_dim rx_profile[] = {
6611 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6612 	{8},    /* 125,000 ints/s */
6613 	{16},   /*  62,500 ints/s */
6614 	{62},   /*  16,129 ints/s */
6615 	{126}   /*   7,936 ints/s */
6616 };
6617 
6618 /* The transmit profile, which has the same sorts of values
6619  * as the previous struct
6620  */
6621 static const struct ice_dim tx_profile[] = {
6622 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6623 	{8},    /* 125,000 ints/s */
6624 	{40},   /*  16,125 ints/s */
6625 	{128},  /*   7,812 ints/s */
6626 	{256}   /*   3,906 ints/s */
6627 };
6628 
6629 static void ice_tx_dim_work(struct work_struct *work)
6630 {
6631 	struct ice_ring_container *rc;
6632 	struct dim *dim;
6633 	u16 itr;
6634 
6635 	dim = container_of(work, struct dim, work);
6636 	rc = dim->priv;
6637 
6638 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6639 
6640 	/* look up the values in our local table */
6641 	itr = tx_profile[dim->profile_ix].itr;
6642 
6643 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6644 	ice_write_itr(rc, itr);
6645 
6646 	dim->state = DIM_START_MEASURE;
6647 }
6648 
6649 static void ice_rx_dim_work(struct work_struct *work)
6650 {
6651 	struct ice_ring_container *rc;
6652 	struct dim *dim;
6653 	u16 itr;
6654 
6655 	dim = container_of(work, struct dim, work);
6656 	rc = dim->priv;
6657 
6658 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6659 
6660 	/* look up the values in our local table */
6661 	itr = rx_profile[dim->profile_ix].itr;
6662 
6663 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6664 	ice_write_itr(rc, itr);
6665 
6666 	dim->state = DIM_START_MEASURE;
6667 }
6668 
6669 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6670 
6671 /**
6672  * ice_init_moderation - set up interrupt moderation
6673  * @q_vector: the vector containing rings to be configured
6674  *
6675  * Set up interrupt moderation registers, with the intent to do the right thing
6676  * when called from reset or from probe, and whether or not dynamic moderation
6677  * is enabled or not. Take special care to write all the registers in both
6678  * dynamic moderation mode or not in order to make sure hardware is in a known
6679  * state.
6680  */
6681 static void ice_init_moderation(struct ice_q_vector *q_vector)
6682 {
6683 	struct ice_ring_container *rc;
6684 	bool tx_dynamic, rx_dynamic;
6685 
6686 	rc = &q_vector->tx;
6687 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6688 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6689 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6690 	rc->dim.priv = rc;
6691 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6692 
6693 	/* set the initial TX ITR to match the above */
6694 	ice_write_itr(rc, tx_dynamic ?
6695 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6696 
6697 	rc = &q_vector->rx;
6698 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6699 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6700 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6701 	rc->dim.priv = rc;
6702 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6703 
6704 	/* set the initial RX ITR to match the above */
6705 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6706 				       rc->itr_setting);
6707 
6708 	ice_set_q_vector_intrl(q_vector);
6709 }
6710 
6711 /**
6712  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6713  * @vsi: the VSI being configured
6714  */
6715 static void ice_napi_enable_all(struct ice_vsi *vsi)
6716 {
6717 	int q_idx;
6718 
6719 	if (!vsi->netdev)
6720 		return;
6721 
6722 	ice_for_each_q_vector(vsi, q_idx) {
6723 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6724 
6725 		ice_init_moderation(q_vector);
6726 
6727 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6728 			napi_enable(&q_vector->napi);
6729 	}
6730 }
6731 
6732 /**
6733  * ice_up_complete - Finish the last steps of bringing up a connection
6734  * @vsi: The VSI being configured
6735  *
6736  * Return 0 on success and negative value on error
6737  */
6738 static int ice_up_complete(struct ice_vsi *vsi)
6739 {
6740 	struct ice_pf *pf = vsi->back;
6741 	int err;
6742 
6743 	ice_vsi_cfg_msix(vsi);
6744 
6745 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6746 	 * Tx queue group list was configured and the context bits were
6747 	 * programmed using ice_vsi_cfg_txqs
6748 	 */
6749 	err = ice_vsi_start_all_rx_rings(vsi);
6750 	if (err)
6751 		return err;
6752 
6753 	clear_bit(ICE_VSI_DOWN, vsi->state);
6754 	ice_napi_enable_all(vsi);
6755 	ice_vsi_ena_irq(vsi);
6756 
6757 	if (vsi->port_info &&
6758 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6759 	    ((vsi->netdev && (vsi->type == ICE_VSI_PF ||
6760 			      vsi->type == ICE_VSI_SF)))) {
6761 		ice_print_link_msg(vsi, true);
6762 		netif_tx_start_all_queues(vsi->netdev);
6763 		netif_carrier_on(vsi->netdev);
6764 		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6765 	}
6766 
6767 	/* Perform an initial read of the statistics registers now to
6768 	 * set the baseline so counters are ready when interface is up
6769 	 */
6770 	ice_update_eth_stats(vsi);
6771 
6772 	if (vsi->type == ICE_VSI_PF)
6773 		ice_service_task_schedule(pf);
6774 
6775 	return 0;
6776 }
6777 
6778 /**
6779  * ice_up - Bring the connection back up after being down
6780  * @vsi: VSI being configured
6781  */
6782 int ice_up(struct ice_vsi *vsi)
6783 {
6784 	int err;
6785 
6786 	err = ice_vsi_cfg_lan(vsi);
6787 	if (!err)
6788 		err = ice_up_complete(vsi);
6789 
6790 	return err;
6791 }
6792 
6793 /**
6794  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6795  * @syncp: pointer to u64_stats_sync
6796  * @stats: stats that pkts and bytes count will be taken from
6797  * @pkts: packets stats counter
6798  * @bytes: bytes stats counter
6799  *
6800  * This function fetches stats from the ring considering the atomic operations
6801  * that needs to be performed to read u64 values in 32 bit machine.
6802  */
6803 void
6804 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6805 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6806 {
6807 	unsigned int start;
6808 
6809 	do {
6810 		start = u64_stats_fetch_begin(syncp);
6811 		*pkts = stats.pkts;
6812 		*bytes = stats.bytes;
6813 	} while (u64_stats_fetch_retry(syncp, start));
6814 }
6815 
6816 /**
6817  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6818  * @vsi: the VSI to be updated
6819  * @vsi_stats: the stats struct to be updated
6820  * @rings: rings to work on
6821  * @count: number of rings
6822  */
6823 static void
6824 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6825 			     struct rtnl_link_stats64 *vsi_stats,
6826 			     struct ice_tx_ring **rings, u16 count)
6827 {
6828 	u16 i;
6829 
6830 	for (i = 0; i < count; i++) {
6831 		struct ice_tx_ring *ring;
6832 		u64 pkts = 0, bytes = 0;
6833 
6834 		ring = READ_ONCE(rings[i]);
6835 		if (!ring || !ring->ring_stats)
6836 			continue;
6837 		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6838 					     ring->ring_stats->stats, &pkts,
6839 					     &bytes);
6840 		vsi_stats->tx_packets += pkts;
6841 		vsi_stats->tx_bytes += bytes;
6842 		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6843 		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6844 		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6845 	}
6846 }
6847 
6848 /**
6849  * ice_update_vsi_ring_stats - Update VSI stats counters
6850  * @vsi: the VSI to be updated
6851  */
6852 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6853 {
6854 	struct rtnl_link_stats64 *net_stats, *stats_prev;
6855 	struct rtnl_link_stats64 *vsi_stats;
6856 	struct ice_pf *pf = vsi->back;
6857 	u64 pkts, bytes;
6858 	int i;
6859 
6860 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6861 	if (!vsi_stats)
6862 		return;
6863 
6864 	/* reset non-netdev (extended) stats */
6865 	vsi->tx_restart = 0;
6866 	vsi->tx_busy = 0;
6867 	vsi->tx_linearize = 0;
6868 	vsi->rx_buf_failed = 0;
6869 	vsi->rx_page_failed = 0;
6870 
6871 	rcu_read_lock();
6872 
6873 	/* update Tx rings counters */
6874 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6875 				     vsi->num_txq);
6876 
6877 	/* update Rx rings counters */
6878 	ice_for_each_rxq(vsi, i) {
6879 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6880 		struct ice_ring_stats *ring_stats;
6881 
6882 		ring_stats = ring->ring_stats;
6883 		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6884 					     ring_stats->stats, &pkts,
6885 					     &bytes);
6886 		vsi_stats->rx_packets += pkts;
6887 		vsi_stats->rx_bytes += bytes;
6888 		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6889 		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6890 	}
6891 
6892 	/* update XDP Tx rings counters */
6893 	if (ice_is_xdp_ena_vsi(vsi))
6894 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6895 					     vsi->num_xdp_txq);
6896 
6897 	rcu_read_unlock();
6898 
6899 	net_stats = &vsi->net_stats;
6900 	stats_prev = &vsi->net_stats_prev;
6901 
6902 	/* Update netdev counters, but keep in mind that values could start at
6903 	 * random value after PF reset. And as we increase the reported stat by
6904 	 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6905 	 * let's skip this round.
6906 	 */
6907 	if (likely(pf->stat_prev_loaded)) {
6908 		net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6909 		net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6910 		net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6911 		net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6912 	}
6913 
6914 	stats_prev->tx_packets = vsi_stats->tx_packets;
6915 	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6916 	stats_prev->rx_packets = vsi_stats->rx_packets;
6917 	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6918 
6919 	kfree(vsi_stats);
6920 }
6921 
6922 /**
6923  * ice_update_vsi_stats - Update VSI stats counters
6924  * @vsi: the VSI to be updated
6925  */
6926 void ice_update_vsi_stats(struct ice_vsi *vsi)
6927 {
6928 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6929 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6930 	struct ice_pf *pf = vsi->back;
6931 
6932 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6933 	    test_bit(ICE_CFG_BUSY, pf->state))
6934 		return;
6935 
6936 	/* get stats as recorded by Tx/Rx rings */
6937 	ice_update_vsi_ring_stats(vsi);
6938 
6939 	/* get VSI stats as recorded by the hardware */
6940 	ice_update_eth_stats(vsi);
6941 
6942 	cur_ns->tx_errors = cur_es->tx_errors;
6943 	cur_ns->rx_dropped = cur_es->rx_discards;
6944 	cur_ns->tx_dropped = cur_es->tx_discards;
6945 	cur_ns->multicast = cur_es->rx_multicast;
6946 
6947 	/* update some more netdev stats if this is main VSI */
6948 	if (vsi->type == ICE_VSI_PF) {
6949 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6950 		cur_ns->rx_errors = pf->stats.crc_errors +
6951 				    pf->stats.illegal_bytes +
6952 				    pf->stats.rx_undersize +
6953 				    pf->hw_csum_rx_error +
6954 				    pf->stats.rx_jabber +
6955 				    pf->stats.rx_fragments +
6956 				    pf->stats.rx_oversize;
6957 		/* record drops from the port level */
6958 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6959 	}
6960 }
6961 
6962 /**
6963  * ice_update_pf_stats - Update PF port stats counters
6964  * @pf: PF whose stats needs to be updated
6965  */
6966 void ice_update_pf_stats(struct ice_pf *pf)
6967 {
6968 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6969 	struct ice_hw *hw = &pf->hw;
6970 	u16 fd_ctr_base;
6971 	u8 port;
6972 
6973 	port = hw->port_info->lport;
6974 	prev_ps = &pf->stats_prev;
6975 	cur_ps = &pf->stats;
6976 
6977 	if (ice_is_reset_in_progress(pf->state))
6978 		pf->stat_prev_loaded = false;
6979 
6980 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6981 			  &prev_ps->eth.rx_bytes,
6982 			  &cur_ps->eth.rx_bytes);
6983 
6984 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6985 			  &prev_ps->eth.rx_unicast,
6986 			  &cur_ps->eth.rx_unicast);
6987 
6988 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6989 			  &prev_ps->eth.rx_multicast,
6990 			  &cur_ps->eth.rx_multicast);
6991 
6992 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6993 			  &prev_ps->eth.rx_broadcast,
6994 			  &cur_ps->eth.rx_broadcast);
6995 
6996 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6997 			  &prev_ps->eth.rx_discards,
6998 			  &cur_ps->eth.rx_discards);
6999 
7000 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
7001 			  &prev_ps->eth.tx_bytes,
7002 			  &cur_ps->eth.tx_bytes);
7003 
7004 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
7005 			  &prev_ps->eth.tx_unicast,
7006 			  &cur_ps->eth.tx_unicast);
7007 
7008 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
7009 			  &prev_ps->eth.tx_multicast,
7010 			  &cur_ps->eth.tx_multicast);
7011 
7012 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
7013 			  &prev_ps->eth.tx_broadcast,
7014 			  &cur_ps->eth.tx_broadcast);
7015 
7016 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
7017 			  &prev_ps->tx_dropped_link_down,
7018 			  &cur_ps->tx_dropped_link_down);
7019 
7020 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
7021 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
7022 
7023 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
7024 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
7025 
7026 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
7027 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
7028 
7029 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
7030 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
7031 
7032 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
7033 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
7034 
7035 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
7036 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
7037 
7038 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
7039 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
7040 
7041 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
7042 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
7043 
7044 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
7045 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
7046 
7047 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
7048 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
7049 
7050 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
7051 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
7052 
7053 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
7054 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
7055 
7056 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
7057 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
7058 
7059 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
7060 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
7061 
7062 	fd_ctr_base = hw->fd_ctr_base;
7063 
7064 	ice_stat_update40(hw,
7065 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
7066 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
7067 			  &cur_ps->fd_sb_match);
7068 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
7069 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
7070 
7071 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
7072 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
7073 
7074 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
7075 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
7076 
7077 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
7078 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
7079 
7080 	ice_update_dcb_stats(pf);
7081 
7082 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
7083 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
7084 
7085 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
7086 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
7087 
7088 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
7089 			  &prev_ps->mac_local_faults,
7090 			  &cur_ps->mac_local_faults);
7091 
7092 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
7093 			  &prev_ps->mac_remote_faults,
7094 			  &cur_ps->mac_remote_faults);
7095 
7096 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
7097 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
7098 
7099 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
7100 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
7101 
7102 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
7103 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
7104 
7105 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
7106 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
7107 
7108 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
7109 
7110 	pf->stat_prev_loaded = true;
7111 }
7112 
7113 /**
7114  * ice_get_stats64 - get statistics for network device structure
7115  * @netdev: network interface device structure
7116  * @stats: main device statistics structure
7117  */
7118 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7119 {
7120 	struct ice_netdev_priv *np = netdev_priv(netdev);
7121 	struct rtnl_link_stats64 *vsi_stats;
7122 	struct ice_vsi *vsi = np->vsi;
7123 
7124 	vsi_stats = &vsi->net_stats;
7125 
7126 	if (!vsi->num_txq || !vsi->num_rxq)
7127 		return;
7128 
7129 	/* netdev packet/byte stats come from ring counter. These are obtained
7130 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
7131 	 * But, only call the update routine and read the registers if VSI is
7132 	 * not down.
7133 	 */
7134 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
7135 		ice_update_vsi_ring_stats(vsi);
7136 	stats->tx_packets = vsi_stats->tx_packets;
7137 	stats->tx_bytes = vsi_stats->tx_bytes;
7138 	stats->rx_packets = vsi_stats->rx_packets;
7139 	stats->rx_bytes = vsi_stats->rx_bytes;
7140 
7141 	/* The rest of the stats can be read from the hardware but instead we
7142 	 * just return values that the watchdog task has already obtained from
7143 	 * the hardware.
7144 	 */
7145 	stats->multicast = vsi_stats->multicast;
7146 	stats->tx_errors = vsi_stats->tx_errors;
7147 	stats->tx_dropped = vsi_stats->tx_dropped;
7148 	stats->rx_errors = vsi_stats->rx_errors;
7149 	stats->rx_dropped = vsi_stats->rx_dropped;
7150 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7151 	stats->rx_length_errors = vsi_stats->rx_length_errors;
7152 }
7153 
7154 /**
7155  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7156  * @vsi: VSI having NAPI disabled
7157  */
7158 static void ice_napi_disable_all(struct ice_vsi *vsi)
7159 {
7160 	int q_idx;
7161 
7162 	if (!vsi->netdev)
7163 		return;
7164 
7165 	ice_for_each_q_vector(vsi, q_idx) {
7166 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7167 
7168 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
7169 			napi_disable(&q_vector->napi);
7170 
7171 		cancel_work_sync(&q_vector->tx.dim.work);
7172 		cancel_work_sync(&q_vector->rx.dim.work);
7173 	}
7174 }
7175 
7176 /**
7177  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
7178  * @vsi: the VSI being un-configured
7179  */
7180 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
7181 {
7182 	struct ice_pf *pf = vsi->back;
7183 	struct ice_hw *hw = &pf->hw;
7184 	u32 val;
7185 	int i;
7186 
7187 	/* disable interrupt causation from each Rx queue; Tx queues are
7188 	 * handled in ice_vsi_stop_tx_ring()
7189 	 */
7190 	if (vsi->rx_rings) {
7191 		ice_for_each_rxq(vsi, i) {
7192 			if (vsi->rx_rings[i]) {
7193 				u16 reg;
7194 
7195 				reg = vsi->rx_rings[i]->reg_idx;
7196 				val = rd32(hw, QINT_RQCTL(reg));
7197 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
7198 				wr32(hw, QINT_RQCTL(reg), val);
7199 			}
7200 		}
7201 	}
7202 
7203 	/* disable each interrupt */
7204 	ice_for_each_q_vector(vsi, i) {
7205 		if (!vsi->q_vectors[i])
7206 			continue;
7207 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
7208 	}
7209 
7210 	ice_flush(hw);
7211 
7212 	/* don't call synchronize_irq() for VF's from the host */
7213 	if (vsi->type == ICE_VSI_VF)
7214 		return;
7215 
7216 	ice_for_each_q_vector(vsi, i)
7217 		synchronize_irq(vsi->q_vectors[i]->irq.virq);
7218 }
7219 
7220 /**
7221  * ice_down - Shutdown the connection
7222  * @vsi: The VSI being stopped
7223  *
7224  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
7225  */
7226 int ice_down(struct ice_vsi *vsi)
7227 {
7228 	int i, tx_err, rx_err, vlan_err = 0;
7229 
7230 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7231 
7232 	if (vsi->netdev) {
7233 		vlan_err = ice_vsi_del_vlan_zero(vsi);
7234 		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
7235 		netif_carrier_off(vsi->netdev);
7236 		netif_tx_disable(vsi->netdev);
7237 	}
7238 
7239 	ice_vsi_dis_irq(vsi);
7240 
7241 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
7242 	if (tx_err)
7243 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
7244 			   vsi->vsi_num, tx_err);
7245 	if (!tx_err && vsi->xdp_rings) {
7246 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7247 		if (tx_err)
7248 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
7249 				   vsi->vsi_num, tx_err);
7250 	}
7251 
7252 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
7253 	if (rx_err)
7254 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
7255 			   vsi->vsi_num, rx_err);
7256 
7257 	ice_napi_disable_all(vsi);
7258 
7259 	ice_for_each_txq(vsi, i)
7260 		ice_clean_tx_ring(vsi->tx_rings[i]);
7261 
7262 	if (vsi->xdp_rings)
7263 		ice_for_each_xdp_txq(vsi, i)
7264 			ice_clean_tx_ring(vsi->xdp_rings[i]);
7265 
7266 	ice_for_each_rxq(vsi, i)
7267 		ice_clean_rx_ring(vsi->rx_rings[i]);
7268 
7269 	if (tx_err || rx_err || vlan_err) {
7270 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
7271 			   vsi->vsi_num, vsi->vsw->sw_id);
7272 		return -EIO;
7273 	}
7274 
7275 	return 0;
7276 }
7277 
7278 /**
7279  * ice_down_up - shutdown the VSI connection and bring it up
7280  * @vsi: the VSI to be reconnected
7281  */
7282 int ice_down_up(struct ice_vsi *vsi)
7283 {
7284 	int ret;
7285 
7286 	/* if DOWN already set, nothing to do */
7287 	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7288 		return 0;
7289 
7290 	ret = ice_down(vsi);
7291 	if (ret)
7292 		return ret;
7293 
7294 	ret = ice_up(vsi);
7295 	if (ret) {
7296 		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7297 		return ret;
7298 	}
7299 
7300 	return 0;
7301 }
7302 
7303 /**
7304  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7305  * @vsi: VSI having resources allocated
7306  *
7307  * Return 0 on success, negative on failure
7308  */
7309 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7310 {
7311 	int i, err = 0;
7312 
7313 	if (!vsi->num_txq) {
7314 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7315 			vsi->vsi_num);
7316 		return -EINVAL;
7317 	}
7318 
7319 	ice_for_each_txq(vsi, i) {
7320 		struct ice_tx_ring *ring = vsi->tx_rings[i];
7321 
7322 		if (!ring)
7323 			return -EINVAL;
7324 
7325 		if (vsi->netdev)
7326 			ring->netdev = vsi->netdev;
7327 		err = ice_setup_tx_ring(ring);
7328 		if (err)
7329 			break;
7330 	}
7331 
7332 	return err;
7333 }
7334 
7335 /**
7336  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7337  * @vsi: VSI having resources allocated
7338  *
7339  * Return 0 on success, negative on failure
7340  */
7341 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7342 {
7343 	int i, err = 0;
7344 
7345 	if (!vsi->num_rxq) {
7346 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7347 			vsi->vsi_num);
7348 		return -EINVAL;
7349 	}
7350 
7351 	ice_for_each_rxq(vsi, i) {
7352 		struct ice_rx_ring *ring = vsi->rx_rings[i];
7353 
7354 		if (!ring)
7355 			return -EINVAL;
7356 
7357 		if (vsi->netdev)
7358 			ring->netdev = vsi->netdev;
7359 		err = ice_setup_rx_ring(ring);
7360 		if (err)
7361 			break;
7362 	}
7363 
7364 	return err;
7365 }
7366 
7367 /**
7368  * ice_vsi_open_ctrl - open control VSI for use
7369  * @vsi: the VSI to open
7370  *
7371  * Initialization of the Control VSI
7372  *
7373  * Returns 0 on success, negative value on error
7374  */
7375 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7376 {
7377 	char int_name[ICE_INT_NAME_STR_LEN];
7378 	struct ice_pf *pf = vsi->back;
7379 	struct device *dev;
7380 	int err;
7381 
7382 	dev = ice_pf_to_dev(pf);
7383 	/* allocate descriptors */
7384 	err = ice_vsi_setup_tx_rings(vsi);
7385 	if (err)
7386 		goto err_setup_tx;
7387 
7388 	err = ice_vsi_setup_rx_rings(vsi);
7389 	if (err)
7390 		goto err_setup_rx;
7391 
7392 	err = ice_vsi_cfg_lan(vsi);
7393 	if (err)
7394 		goto err_setup_rx;
7395 
7396 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7397 		 dev_driver_string(dev), dev_name(dev));
7398 	err = ice_vsi_req_irq_msix(vsi, int_name);
7399 	if (err)
7400 		goto err_setup_rx;
7401 
7402 	ice_vsi_cfg_msix(vsi);
7403 
7404 	err = ice_vsi_start_all_rx_rings(vsi);
7405 	if (err)
7406 		goto err_up_complete;
7407 
7408 	clear_bit(ICE_VSI_DOWN, vsi->state);
7409 	ice_vsi_ena_irq(vsi);
7410 
7411 	return 0;
7412 
7413 err_up_complete:
7414 	ice_down(vsi);
7415 err_setup_rx:
7416 	ice_vsi_free_rx_rings(vsi);
7417 err_setup_tx:
7418 	ice_vsi_free_tx_rings(vsi);
7419 
7420 	return err;
7421 }
7422 
7423 /**
7424  * ice_vsi_open - Called when a network interface is made active
7425  * @vsi: the VSI to open
7426  *
7427  * Initialization of the VSI
7428  *
7429  * Returns 0 on success, negative value on error
7430  */
7431 int ice_vsi_open(struct ice_vsi *vsi)
7432 {
7433 	char int_name[ICE_INT_NAME_STR_LEN];
7434 	struct ice_pf *pf = vsi->back;
7435 	int err;
7436 
7437 	/* allocate descriptors */
7438 	err = ice_vsi_setup_tx_rings(vsi);
7439 	if (err)
7440 		goto err_setup_tx;
7441 
7442 	err = ice_vsi_setup_rx_rings(vsi);
7443 	if (err)
7444 		goto err_setup_rx;
7445 
7446 	err = ice_vsi_cfg_lan(vsi);
7447 	if (err)
7448 		goto err_setup_rx;
7449 
7450 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7451 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7452 	err = ice_vsi_req_irq_msix(vsi, int_name);
7453 	if (err)
7454 		goto err_setup_rx;
7455 
7456 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7457 
7458 	if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_SF) {
7459 		/* Notify the stack of the actual queue counts. */
7460 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7461 		if (err)
7462 			goto err_set_qs;
7463 
7464 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7465 		if (err)
7466 			goto err_set_qs;
7467 
7468 		ice_vsi_set_napi_queues(vsi);
7469 	}
7470 
7471 	err = ice_up_complete(vsi);
7472 	if (err)
7473 		goto err_up_complete;
7474 
7475 	return 0;
7476 
7477 err_up_complete:
7478 	ice_down(vsi);
7479 err_set_qs:
7480 	ice_vsi_free_irq(vsi);
7481 err_setup_rx:
7482 	ice_vsi_free_rx_rings(vsi);
7483 err_setup_tx:
7484 	ice_vsi_free_tx_rings(vsi);
7485 
7486 	return err;
7487 }
7488 
7489 /**
7490  * ice_vsi_release_all - Delete all VSIs
7491  * @pf: PF from which all VSIs are being removed
7492  */
7493 static void ice_vsi_release_all(struct ice_pf *pf)
7494 {
7495 	int err, i;
7496 
7497 	if (!pf->vsi)
7498 		return;
7499 
7500 	ice_for_each_vsi(pf, i) {
7501 		if (!pf->vsi[i])
7502 			continue;
7503 
7504 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7505 			continue;
7506 
7507 		err = ice_vsi_release(pf->vsi[i]);
7508 		if (err)
7509 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7510 				i, err, pf->vsi[i]->vsi_num);
7511 	}
7512 }
7513 
7514 /**
7515  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7516  * @pf: pointer to the PF instance
7517  * @type: VSI type to rebuild
7518  *
7519  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7520  */
7521 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7522 {
7523 	struct device *dev = ice_pf_to_dev(pf);
7524 	int i, err;
7525 
7526 	ice_for_each_vsi(pf, i) {
7527 		struct ice_vsi *vsi = pf->vsi[i];
7528 
7529 		if (!vsi || vsi->type != type)
7530 			continue;
7531 
7532 		/* rebuild the VSI */
7533 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7534 		if (err) {
7535 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7536 				err, vsi->idx, ice_vsi_type_str(type));
7537 			return err;
7538 		}
7539 
7540 		/* replay filters for the VSI */
7541 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7542 		if (err) {
7543 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7544 				err, vsi->idx, ice_vsi_type_str(type));
7545 			return err;
7546 		}
7547 
7548 		/* Re-map HW VSI number, using VSI handle that has been
7549 		 * previously validated in ice_replay_vsi() call above
7550 		 */
7551 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7552 
7553 		/* enable the VSI */
7554 		err = ice_ena_vsi(vsi, false);
7555 		if (err) {
7556 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7557 				err, vsi->idx, ice_vsi_type_str(type));
7558 			return err;
7559 		}
7560 
7561 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7562 			 ice_vsi_type_str(type));
7563 	}
7564 
7565 	return 0;
7566 }
7567 
7568 /**
7569  * ice_update_pf_netdev_link - Update PF netdev link status
7570  * @pf: pointer to the PF instance
7571  */
7572 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7573 {
7574 	bool link_up;
7575 	int i;
7576 
7577 	ice_for_each_vsi(pf, i) {
7578 		struct ice_vsi *vsi = pf->vsi[i];
7579 
7580 		if (!vsi || vsi->type != ICE_VSI_PF)
7581 			return;
7582 
7583 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7584 		if (link_up) {
7585 			netif_carrier_on(pf->vsi[i]->netdev);
7586 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7587 		} else {
7588 			netif_carrier_off(pf->vsi[i]->netdev);
7589 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7590 		}
7591 	}
7592 }
7593 
7594 /**
7595  * ice_rebuild - rebuild after reset
7596  * @pf: PF to rebuild
7597  * @reset_type: type of reset
7598  *
7599  * Do not rebuild VF VSI in this flow because that is already handled via
7600  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7601  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7602  * to reset/rebuild all the VF VSI twice.
7603  */
7604 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7605 {
7606 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
7607 	struct device *dev = ice_pf_to_dev(pf);
7608 	struct ice_hw *hw = &pf->hw;
7609 	bool dvm;
7610 	int err;
7611 
7612 	if (test_bit(ICE_DOWN, pf->state))
7613 		goto clear_recovery;
7614 
7615 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7616 
7617 #define ICE_EMP_RESET_SLEEP_MS 5000
7618 	if (reset_type == ICE_RESET_EMPR) {
7619 		/* If an EMP reset has occurred, any previously pending flash
7620 		 * update will have completed. We no longer know whether or
7621 		 * not the NVM update EMP reset is restricted.
7622 		 */
7623 		pf->fw_emp_reset_disabled = false;
7624 
7625 		msleep(ICE_EMP_RESET_SLEEP_MS);
7626 	}
7627 
7628 	err = ice_init_all_ctrlq(hw);
7629 	if (err) {
7630 		dev_err(dev, "control queues init failed %d\n", err);
7631 		goto err_init_ctrlq;
7632 	}
7633 
7634 	/* if DDP was previously loaded successfully */
7635 	if (!ice_is_safe_mode(pf)) {
7636 		/* reload the SW DB of filter tables */
7637 		if (reset_type == ICE_RESET_PFR)
7638 			ice_fill_blk_tbls(hw);
7639 		else
7640 			/* Reload DDP Package after CORER/GLOBR reset */
7641 			ice_load_pkg(NULL, pf);
7642 	}
7643 
7644 	err = ice_clear_pf_cfg(hw);
7645 	if (err) {
7646 		dev_err(dev, "clear PF configuration failed %d\n", err);
7647 		goto err_init_ctrlq;
7648 	}
7649 
7650 	ice_clear_pxe_mode(hw);
7651 
7652 	err = ice_init_nvm(hw);
7653 	if (err) {
7654 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7655 		goto err_init_ctrlq;
7656 	}
7657 
7658 	err = ice_get_caps(hw);
7659 	if (err) {
7660 		dev_err(dev, "ice_get_caps failed %d\n", err);
7661 		goto err_init_ctrlq;
7662 	}
7663 
7664 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7665 	if (err) {
7666 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7667 		goto err_init_ctrlq;
7668 	}
7669 
7670 	dvm = ice_is_dvm_ena(hw);
7671 
7672 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7673 	if (err)
7674 		goto err_init_ctrlq;
7675 
7676 	err = ice_sched_init_port(hw->port_info);
7677 	if (err)
7678 		goto err_sched_init_port;
7679 
7680 	/* start misc vector */
7681 	err = ice_req_irq_msix_misc(pf);
7682 	if (err) {
7683 		dev_err(dev, "misc vector setup failed: %d\n", err);
7684 		goto err_sched_init_port;
7685 	}
7686 
7687 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7688 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7689 		if (!rd32(hw, PFQF_FD_SIZE)) {
7690 			u16 unused, guar, b_effort;
7691 
7692 			guar = hw->func_caps.fd_fltr_guar;
7693 			b_effort = hw->func_caps.fd_fltr_best_effort;
7694 
7695 			/* force guaranteed filter pool for PF */
7696 			ice_alloc_fd_guar_item(hw, &unused, guar);
7697 			/* force shared filter pool for PF */
7698 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7699 		}
7700 	}
7701 
7702 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7703 		ice_dcb_rebuild(pf);
7704 
7705 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7706 	 * the VSI rebuild. If not, this causes the PTP link status events to
7707 	 * fail.
7708 	 */
7709 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7710 		ice_ptp_rebuild(pf, reset_type);
7711 
7712 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7713 		ice_gnss_init(pf);
7714 
7715 	/* rebuild PF VSI */
7716 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7717 	if (err) {
7718 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7719 		goto err_vsi_rebuild;
7720 	}
7721 
7722 	if (reset_type == ICE_RESET_PFR) {
7723 		err = ice_rebuild_channels(pf);
7724 		if (err) {
7725 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7726 				err);
7727 			goto err_vsi_rebuild;
7728 		}
7729 	}
7730 
7731 	/* If Flow Director is active */
7732 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7733 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7734 		if (err) {
7735 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7736 			goto err_vsi_rebuild;
7737 		}
7738 
7739 		/* replay HW Flow Director recipes */
7740 		if (hw->fdir_prof)
7741 			ice_fdir_replay_flows(hw);
7742 
7743 		/* replay Flow Director filters */
7744 		ice_fdir_replay_fltrs(pf);
7745 
7746 		ice_rebuild_arfs(pf);
7747 	}
7748 
7749 	if (vsi && vsi->netdev)
7750 		netif_device_attach(vsi->netdev);
7751 
7752 	ice_update_pf_netdev_link(pf);
7753 
7754 	/* tell the firmware we are up */
7755 	err = ice_send_version(pf);
7756 	if (err) {
7757 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7758 			err);
7759 		goto err_vsi_rebuild;
7760 	}
7761 
7762 	ice_replay_post(hw);
7763 
7764 	/* if we get here, reset flow is successful */
7765 	clear_bit(ICE_RESET_FAILED, pf->state);
7766 
7767 	ice_plug_aux_dev(pf);
7768 	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7769 		ice_lag_rebuild(pf);
7770 
7771 	/* Restore timestamp mode settings after VSI rebuild */
7772 	ice_ptp_restore_timestamp_mode(pf);
7773 	return;
7774 
7775 err_vsi_rebuild:
7776 err_sched_init_port:
7777 	ice_sched_cleanup_all(hw);
7778 err_init_ctrlq:
7779 	ice_shutdown_all_ctrlq(hw, false);
7780 	set_bit(ICE_RESET_FAILED, pf->state);
7781 clear_recovery:
7782 	/* set this bit in PF state to control service task scheduling */
7783 	set_bit(ICE_NEEDS_RESTART, pf->state);
7784 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7785 }
7786 
7787 /**
7788  * ice_change_mtu - NDO callback to change the MTU
7789  * @netdev: network interface device structure
7790  * @new_mtu: new value for maximum frame size
7791  *
7792  * Returns 0 on success, negative on failure
7793  */
7794 int ice_change_mtu(struct net_device *netdev, int new_mtu)
7795 {
7796 	struct ice_netdev_priv *np = netdev_priv(netdev);
7797 	struct ice_vsi *vsi = np->vsi;
7798 	struct ice_pf *pf = vsi->back;
7799 	struct bpf_prog *prog;
7800 	u8 count = 0;
7801 	int err = 0;
7802 
7803 	if (new_mtu == (int)netdev->mtu) {
7804 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7805 		return 0;
7806 	}
7807 
7808 	prog = vsi->xdp_prog;
7809 	if (prog && !prog->aux->xdp_has_frags) {
7810 		int frame_size = ice_max_xdp_frame_size(vsi);
7811 
7812 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7813 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7814 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7815 			return -EINVAL;
7816 		}
7817 	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7818 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7819 			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7820 				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7821 			return -EINVAL;
7822 		}
7823 	}
7824 
7825 	/* if a reset is in progress, wait for some time for it to complete */
7826 	do {
7827 		if (ice_is_reset_in_progress(pf->state)) {
7828 			count++;
7829 			usleep_range(1000, 2000);
7830 		} else {
7831 			break;
7832 		}
7833 
7834 	} while (count < 100);
7835 
7836 	if (count == 100) {
7837 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7838 		return -EBUSY;
7839 	}
7840 
7841 	WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
7842 	err = ice_down_up(vsi);
7843 	if (err)
7844 		return err;
7845 
7846 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7847 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7848 
7849 	return err;
7850 }
7851 
7852 /**
7853  * ice_eth_ioctl - Access the hwtstamp interface
7854  * @netdev: network interface device structure
7855  * @ifr: interface request data
7856  * @cmd: ioctl command
7857  */
7858 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7859 {
7860 	struct ice_netdev_priv *np = netdev_priv(netdev);
7861 	struct ice_pf *pf = np->vsi->back;
7862 
7863 	switch (cmd) {
7864 	case SIOCGHWTSTAMP:
7865 		return ice_ptp_get_ts_config(pf, ifr);
7866 	case SIOCSHWTSTAMP:
7867 		return ice_ptp_set_ts_config(pf, ifr);
7868 	default:
7869 		return -EOPNOTSUPP;
7870 	}
7871 }
7872 
7873 /**
7874  * ice_aq_str - convert AQ err code to a string
7875  * @aq_err: the AQ error code to convert
7876  */
7877 const char *ice_aq_str(enum ice_aq_err aq_err)
7878 {
7879 	switch (aq_err) {
7880 	case ICE_AQ_RC_OK:
7881 		return "OK";
7882 	case ICE_AQ_RC_EPERM:
7883 		return "ICE_AQ_RC_EPERM";
7884 	case ICE_AQ_RC_ENOENT:
7885 		return "ICE_AQ_RC_ENOENT";
7886 	case ICE_AQ_RC_ENOMEM:
7887 		return "ICE_AQ_RC_ENOMEM";
7888 	case ICE_AQ_RC_EBUSY:
7889 		return "ICE_AQ_RC_EBUSY";
7890 	case ICE_AQ_RC_EEXIST:
7891 		return "ICE_AQ_RC_EEXIST";
7892 	case ICE_AQ_RC_EINVAL:
7893 		return "ICE_AQ_RC_EINVAL";
7894 	case ICE_AQ_RC_ENOSPC:
7895 		return "ICE_AQ_RC_ENOSPC";
7896 	case ICE_AQ_RC_ENOSYS:
7897 		return "ICE_AQ_RC_ENOSYS";
7898 	case ICE_AQ_RC_EMODE:
7899 		return "ICE_AQ_RC_EMODE";
7900 	case ICE_AQ_RC_ENOSEC:
7901 		return "ICE_AQ_RC_ENOSEC";
7902 	case ICE_AQ_RC_EBADSIG:
7903 		return "ICE_AQ_RC_EBADSIG";
7904 	case ICE_AQ_RC_ESVN:
7905 		return "ICE_AQ_RC_ESVN";
7906 	case ICE_AQ_RC_EBADMAN:
7907 		return "ICE_AQ_RC_EBADMAN";
7908 	case ICE_AQ_RC_EBADBUF:
7909 		return "ICE_AQ_RC_EBADBUF";
7910 	}
7911 
7912 	return "ICE_AQ_RC_UNKNOWN";
7913 }
7914 
7915 /**
7916  * ice_set_rss_lut - Set RSS LUT
7917  * @vsi: Pointer to VSI structure
7918  * @lut: Lookup table
7919  * @lut_size: Lookup table size
7920  *
7921  * Returns 0 on success, negative on failure
7922  */
7923 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7924 {
7925 	struct ice_aq_get_set_rss_lut_params params = {};
7926 	struct ice_hw *hw = &vsi->back->hw;
7927 	int status;
7928 
7929 	if (!lut)
7930 		return -EINVAL;
7931 
7932 	params.vsi_handle = vsi->idx;
7933 	params.lut_size = lut_size;
7934 	params.lut_type = vsi->rss_lut_type;
7935 	params.lut = lut;
7936 
7937 	status = ice_aq_set_rss_lut(hw, &params);
7938 	if (status)
7939 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7940 			status, ice_aq_str(hw->adminq.sq_last_status));
7941 
7942 	return status;
7943 }
7944 
7945 /**
7946  * ice_set_rss_key - Set RSS key
7947  * @vsi: Pointer to the VSI structure
7948  * @seed: RSS hash seed
7949  *
7950  * Returns 0 on success, negative on failure
7951  */
7952 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7953 {
7954 	struct ice_hw *hw = &vsi->back->hw;
7955 	int status;
7956 
7957 	if (!seed)
7958 		return -EINVAL;
7959 
7960 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7961 	if (status)
7962 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7963 			status, ice_aq_str(hw->adminq.sq_last_status));
7964 
7965 	return status;
7966 }
7967 
7968 /**
7969  * ice_get_rss_lut - Get RSS LUT
7970  * @vsi: Pointer to VSI structure
7971  * @lut: Buffer to store the lookup table entries
7972  * @lut_size: Size of buffer to store the lookup table entries
7973  *
7974  * Returns 0 on success, negative on failure
7975  */
7976 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7977 {
7978 	struct ice_aq_get_set_rss_lut_params params = {};
7979 	struct ice_hw *hw = &vsi->back->hw;
7980 	int status;
7981 
7982 	if (!lut)
7983 		return -EINVAL;
7984 
7985 	params.vsi_handle = vsi->idx;
7986 	params.lut_size = lut_size;
7987 	params.lut_type = vsi->rss_lut_type;
7988 	params.lut = lut;
7989 
7990 	status = ice_aq_get_rss_lut(hw, &params);
7991 	if (status)
7992 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7993 			status, ice_aq_str(hw->adminq.sq_last_status));
7994 
7995 	return status;
7996 }
7997 
7998 /**
7999  * ice_get_rss_key - Get RSS key
8000  * @vsi: Pointer to VSI structure
8001  * @seed: Buffer to store the key in
8002  *
8003  * Returns 0 on success, negative on failure
8004  */
8005 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
8006 {
8007 	struct ice_hw *hw = &vsi->back->hw;
8008 	int status;
8009 
8010 	if (!seed)
8011 		return -EINVAL;
8012 
8013 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
8014 	if (status)
8015 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
8016 			status, ice_aq_str(hw->adminq.sq_last_status));
8017 
8018 	return status;
8019 }
8020 
8021 /**
8022  * ice_set_rss_hfunc - Set RSS HASH function
8023  * @vsi: Pointer to VSI structure
8024  * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
8025  *
8026  * Returns 0 on success, negative on failure
8027  */
8028 int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
8029 {
8030 	struct ice_hw *hw = &vsi->back->hw;
8031 	struct ice_vsi_ctx *ctx;
8032 	bool symm;
8033 	int err;
8034 
8035 	if (hfunc == vsi->rss_hfunc)
8036 		return 0;
8037 
8038 	if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
8039 	    hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
8040 		return -EOPNOTSUPP;
8041 
8042 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8043 	if (!ctx)
8044 		return -ENOMEM;
8045 
8046 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
8047 	ctx->info.q_opt_rss = vsi->info.q_opt_rss;
8048 	ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
8049 	ctx->info.q_opt_rss |=
8050 		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
8051 	ctx->info.q_opt_tc = vsi->info.q_opt_tc;
8052 	ctx->info.q_opt_flags = vsi->info.q_opt_rss;
8053 
8054 	err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
8055 	if (err) {
8056 		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
8057 			vsi->vsi_num, err);
8058 	} else {
8059 		vsi->info.q_opt_rss = ctx->info.q_opt_rss;
8060 		vsi->rss_hfunc = hfunc;
8061 		netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
8062 			    hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
8063 			    "Symmetric " : "");
8064 	}
8065 	kfree(ctx);
8066 	if (err)
8067 		return err;
8068 
8069 	/* Fix the symmetry setting for all existing RSS configurations */
8070 	symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
8071 	return ice_set_rss_cfg_symm(hw, vsi, symm);
8072 }
8073 
8074 /**
8075  * ice_bridge_getlink - Get the hardware bridge mode
8076  * @skb: skb buff
8077  * @pid: process ID
8078  * @seq: RTNL message seq
8079  * @dev: the netdev being configured
8080  * @filter_mask: filter mask passed in
8081  * @nlflags: netlink flags passed in
8082  *
8083  * Return the bridge mode (VEB/VEPA)
8084  */
8085 static int
8086 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8087 		   struct net_device *dev, u32 filter_mask, int nlflags)
8088 {
8089 	struct ice_netdev_priv *np = netdev_priv(dev);
8090 	struct ice_vsi *vsi = np->vsi;
8091 	struct ice_pf *pf = vsi->back;
8092 	u16 bmode;
8093 
8094 	bmode = pf->first_sw->bridge_mode;
8095 
8096 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
8097 				       filter_mask, NULL);
8098 }
8099 
8100 /**
8101  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
8102  * @vsi: Pointer to VSI structure
8103  * @bmode: Hardware bridge mode (VEB/VEPA)
8104  *
8105  * Returns 0 on success, negative on failure
8106  */
8107 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
8108 {
8109 	struct ice_aqc_vsi_props *vsi_props;
8110 	struct ice_hw *hw = &vsi->back->hw;
8111 	struct ice_vsi_ctx *ctxt;
8112 	int ret;
8113 
8114 	vsi_props = &vsi->info;
8115 
8116 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
8117 	if (!ctxt)
8118 		return -ENOMEM;
8119 
8120 	ctxt->info = vsi->info;
8121 
8122 	if (bmode == BRIDGE_MODE_VEB)
8123 		/* change from VEPA to VEB mode */
8124 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8125 	else
8126 		/* change from VEB to VEPA mode */
8127 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8128 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
8129 
8130 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
8131 	if (ret) {
8132 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
8133 			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
8134 		goto out;
8135 	}
8136 	/* Update sw flags for book keeping */
8137 	vsi_props->sw_flags = ctxt->info.sw_flags;
8138 
8139 out:
8140 	kfree(ctxt);
8141 	return ret;
8142 }
8143 
8144 /**
8145  * ice_bridge_setlink - Set the hardware bridge mode
8146  * @dev: the netdev being configured
8147  * @nlh: RTNL message
8148  * @flags: bridge setlink flags
8149  * @extack: netlink extended ack
8150  *
8151  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
8152  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
8153  * not already set for all VSIs connected to this switch. And also update the
8154  * unicast switch filter rules for the corresponding switch of the netdev.
8155  */
8156 static int
8157 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8158 		   u16 __always_unused flags,
8159 		   struct netlink_ext_ack __always_unused *extack)
8160 {
8161 	struct ice_netdev_priv *np = netdev_priv(dev);
8162 	struct ice_pf *pf = np->vsi->back;
8163 	struct nlattr *attr, *br_spec;
8164 	struct ice_hw *hw = &pf->hw;
8165 	struct ice_sw *pf_sw;
8166 	int rem, v, err = 0;
8167 
8168 	pf_sw = pf->first_sw;
8169 	/* find the attribute in the netlink message */
8170 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8171 	if (!br_spec)
8172 		return -EINVAL;
8173 
8174 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
8175 		__u16 mode = nla_get_u16(attr);
8176 
8177 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8178 			return -EINVAL;
8179 		/* Continue  if bridge mode is not being flipped */
8180 		if (mode == pf_sw->bridge_mode)
8181 			continue;
8182 		/* Iterates through the PF VSI list and update the loopback
8183 		 * mode of the VSI
8184 		 */
8185 		ice_for_each_vsi(pf, v) {
8186 			if (!pf->vsi[v])
8187 				continue;
8188 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8189 			if (err)
8190 				return err;
8191 		}
8192 
8193 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8194 		/* Update the unicast switch filter rules for the corresponding
8195 		 * switch of the netdev
8196 		 */
8197 		err = ice_update_sw_rule_bridge_mode(hw);
8198 		if (err) {
8199 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
8200 				   mode, err,
8201 				   ice_aq_str(hw->adminq.sq_last_status));
8202 			/* revert hw->evb_veb */
8203 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
8204 			return err;
8205 		}
8206 
8207 		pf_sw->bridge_mode = mode;
8208 	}
8209 
8210 	return 0;
8211 }
8212 
8213 /**
8214  * ice_tx_timeout - Respond to a Tx Hang
8215  * @netdev: network interface device structure
8216  * @txqueue: Tx queue
8217  */
8218 void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
8219 {
8220 	struct ice_netdev_priv *np = netdev_priv(netdev);
8221 	struct ice_tx_ring *tx_ring = NULL;
8222 	struct ice_vsi *vsi = np->vsi;
8223 	struct ice_pf *pf = vsi->back;
8224 	u32 i;
8225 
8226 	pf->tx_timeout_count++;
8227 
8228 	/* Check if PFC is enabled for the TC to which the queue belongs
8229 	 * to. If yes then Tx timeout is not caused by a hung queue, no
8230 	 * need to reset and rebuild
8231 	 */
8232 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8233 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8234 			 txqueue);
8235 		return;
8236 	}
8237 
8238 	/* now that we have an index, find the tx_ring struct */
8239 	ice_for_each_txq(vsi, i)
8240 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8241 			if (txqueue == vsi->tx_rings[i]->q_index) {
8242 				tx_ring = vsi->tx_rings[i];
8243 				break;
8244 			}
8245 
8246 	/* Reset recovery level if enough time has elapsed after last timeout.
8247 	 * Also ensure no new reset action happens before next timeout period.
8248 	 */
8249 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8250 		pf->tx_timeout_recovery_level = 1;
8251 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8252 				       netdev->watchdog_timeo)))
8253 		return;
8254 
8255 	if (tx_ring) {
8256 		struct ice_hw *hw = &pf->hw;
8257 		u32 head, val = 0;
8258 
8259 		head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8260 				 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
8261 		/* Read interrupt register */
8262 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
8263 
8264 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
8265 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
8266 			    head, tx_ring->next_to_use, val);
8267 	}
8268 
8269 	pf->tx_timeout_last_recovery = jiffies;
8270 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
8271 		    pf->tx_timeout_recovery_level, txqueue);
8272 
8273 	switch (pf->tx_timeout_recovery_level) {
8274 	case 1:
8275 		set_bit(ICE_PFR_REQ, pf->state);
8276 		break;
8277 	case 2:
8278 		set_bit(ICE_CORER_REQ, pf->state);
8279 		break;
8280 	case 3:
8281 		set_bit(ICE_GLOBR_REQ, pf->state);
8282 		break;
8283 	default:
8284 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
8285 		set_bit(ICE_DOWN, pf->state);
8286 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
8287 		set_bit(ICE_SERVICE_DIS, pf->state);
8288 		break;
8289 	}
8290 
8291 	ice_service_task_schedule(pf);
8292 	pf->tx_timeout_recovery_level++;
8293 }
8294 
8295 /**
8296  * ice_setup_tc_cls_flower - flower classifier offloads
8297  * @np: net device to configure
8298  * @filter_dev: device on which filter is added
8299  * @cls_flower: offload data
8300  */
8301 static int
8302 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8303 			struct net_device *filter_dev,
8304 			struct flow_cls_offload *cls_flower)
8305 {
8306 	struct ice_vsi *vsi = np->vsi;
8307 
8308 	if (cls_flower->common.chain_index)
8309 		return -EOPNOTSUPP;
8310 
8311 	switch (cls_flower->command) {
8312 	case FLOW_CLS_REPLACE:
8313 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
8314 	case FLOW_CLS_DESTROY:
8315 		return ice_del_cls_flower(vsi, cls_flower);
8316 	default:
8317 		return -EINVAL;
8318 	}
8319 }
8320 
8321 /**
8322  * ice_setup_tc_block_cb - callback handler registered for TC block
8323  * @type: TC SETUP type
8324  * @type_data: TC flower offload data that contains user input
8325  * @cb_priv: netdev private data
8326  */
8327 static int
8328 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8329 {
8330 	struct ice_netdev_priv *np = cb_priv;
8331 
8332 	switch (type) {
8333 	case TC_SETUP_CLSFLOWER:
8334 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8335 					       type_data);
8336 	default:
8337 		return -EOPNOTSUPP;
8338 	}
8339 }
8340 
8341 /**
8342  * ice_validate_mqprio_qopt - Validate TCF input parameters
8343  * @vsi: Pointer to VSI
8344  * @mqprio_qopt: input parameters for mqprio queue configuration
8345  *
8346  * This function validates MQPRIO params, such as qcount (power of 2 wherever
8347  * needed), and make sure user doesn't specify qcount and BW rate limit
8348  * for TCs, which are more than "num_tc"
8349  */
8350 static int
8351 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8352 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
8353 {
8354 	int non_power_of_2_qcount = 0;
8355 	struct ice_pf *pf = vsi->back;
8356 	int max_rss_q_cnt = 0;
8357 	u64 sum_min_rate = 0;
8358 	struct device *dev;
8359 	int i, speed;
8360 	u8 num_tc;
8361 
8362 	if (vsi->type != ICE_VSI_PF)
8363 		return -EINVAL;
8364 
8365 	if (mqprio_qopt->qopt.offset[0] != 0 ||
8366 	    mqprio_qopt->qopt.num_tc < 1 ||
8367 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8368 		return -EINVAL;
8369 
8370 	dev = ice_pf_to_dev(pf);
8371 	vsi->ch_rss_size = 0;
8372 	num_tc = mqprio_qopt->qopt.num_tc;
8373 	speed = ice_get_link_speed_kbps(vsi);
8374 
8375 	for (i = 0; num_tc; i++) {
8376 		int qcount = mqprio_qopt->qopt.count[i];
8377 		u64 max_rate, min_rate, rem;
8378 
8379 		if (!qcount)
8380 			return -EINVAL;
8381 
8382 		if (is_power_of_2(qcount)) {
8383 			if (non_power_of_2_qcount &&
8384 			    qcount > non_power_of_2_qcount) {
8385 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8386 					qcount, non_power_of_2_qcount);
8387 				return -EINVAL;
8388 			}
8389 			if (qcount > max_rss_q_cnt)
8390 				max_rss_q_cnt = qcount;
8391 		} else {
8392 			if (non_power_of_2_qcount &&
8393 			    qcount != non_power_of_2_qcount) {
8394 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8395 					qcount, non_power_of_2_qcount);
8396 				return -EINVAL;
8397 			}
8398 			if (qcount < max_rss_q_cnt) {
8399 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8400 					qcount, max_rss_q_cnt);
8401 				return -EINVAL;
8402 			}
8403 			max_rss_q_cnt = qcount;
8404 			non_power_of_2_qcount = qcount;
8405 		}
8406 
8407 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8408 		 * converts the bandwidth rate limit into Bytes/s when
8409 		 * passing it down to the driver. So convert input bandwidth
8410 		 * from Bytes/s to Kbps
8411 		 */
8412 		max_rate = mqprio_qopt->max_rate[i];
8413 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8414 
8415 		/* min_rate is minimum guaranteed rate and it can't be zero */
8416 		min_rate = mqprio_qopt->min_rate[i];
8417 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8418 		sum_min_rate += min_rate;
8419 
8420 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8421 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8422 				min_rate, ICE_MIN_BW_LIMIT);
8423 			return -EINVAL;
8424 		}
8425 
8426 		if (max_rate && max_rate > speed) {
8427 			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8428 				i, max_rate, speed);
8429 			return -EINVAL;
8430 		}
8431 
8432 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8433 		if (rem) {
8434 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8435 				i, ICE_MIN_BW_LIMIT);
8436 			return -EINVAL;
8437 		}
8438 
8439 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8440 		if (rem) {
8441 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8442 				i, ICE_MIN_BW_LIMIT);
8443 			return -EINVAL;
8444 		}
8445 
8446 		/* min_rate can't be more than max_rate, except when max_rate
8447 		 * is zero (implies max_rate sought is max line rate). In such
8448 		 * a case min_rate can be more than max.
8449 		 */
8450 		if (max_rate && min_rate > max_rate) {
8451 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8452 				min_rate, max_rate);
8453 			return -EINVAL;
8454 		}
8455 
8456 		if (i >= mqprio_qopt->qopt.num_tc - 1)
8457 			break;
8458 		if (mqprio_qopt->qopt.offset[i + 1] !=
8459 		    (mqprio_qopt->qopt.offset[i] + qcount))
8460 			return -EINVAL;
8461 	}
8462 	if (vsi->num_rxq <
8463 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8464 		return -EINVAL;
8465 	if (vsi->num_txq <
8466 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8467 		return -EINVAL;
8468 
8469 	if (sum_min_rate && sum_min_rate > (u64)speed) {
8470 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8471 			sum_min_rate, speed);
8472 		return -EINVAL;
8473 	}
8474 
8475 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8476 	vsi->ch_rss_size = max_rss_q_cnt;
8477 
8478 	return 0;
8479 }
8480 
8481 /**
8482  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8483  * @pf: ptr to PF device
8484  * @vsi: ptr to VSI
8485  */
8486 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8487 {
8488 	struct device *dev = ice_pf_to_dev(pf);
8489 	bool added = false;
8490 	struct ice_hw *hw;
8491 	int flow;
8492 
8493 	if (!(vsi->num_gfltr || vsi->num_bfltr))
8494 		return -EINVAL;
8495 
8496 	hw = &pf->hw;
8497 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8498 		struct ice_fd_hw_prof *prof;
8499 		int tun, status;
8500 		u64 entry_h;
8501 
8502 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8503 		      hw->fdir_prof[flow]->cnt))
8504 			continue;
8505 
8506 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8507 			enum ice_flow_priority prio;
8508 
8509 			/* add this VSI to FDir profile for this flow */
8510 			prio = ICE_FLOW_PRIO_NORMAL;
8511 			prof = hw->fdir_prof[flow];
8512 			status = ice_flow_add_entry(hw, ICE_BLK_FD,
8513 						    prof->prof_id[tun],
8514 						    prof->vsi_h[0], vsi->idx,
8515 						    prio, prof->fdir_seg[tun],
8516 						    &entry_h);
8517 			if (status) {
8518 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8519 					vsi->idx, flow);
8520 				continue;
8521 			}
8522 
8523 			prof->entry_h[prof->cnt][tun] = entry_h;
8524 		}
8525 
8526 		/* store VSI for filter replay and delete */
8527 		prof->vsi_h[prof->cnt] = vsi->idx;
8528 		prof->cnt++;
8529 
8530 		added = true;
8531 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8532 			flow);
8533 	}
8534 
8535 	if (!added)
8536 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8537 
8538 	return 0;
8539 }
8540 
8541 /**
8542  * ice_add_channel - add a channel by adding VSI
8543  * @pf: ptr to PF device
8544  * @sw_id: underlying HW switching element ID
8545  * @ch: ptr to channel structure
8546  *
8547  * Add a channel (VSI) using add_vsi and queue_map
8548  */
8549 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8550 {
8551 	struct device *dev = ice_pf_to_dev(pf);
8552 	struct ice_vsi *vsi;
8553 
8554 	if (ch->type != ICE_VSI_CHNL) {
8555 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8556 		return -EINVAL;
8557 	}
8558 
8559 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8560 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8561 		dev_err(dev, "create chnl VSI failure\n");
8562 		return -EINVAL;
8563 	}
8564 
8565 	ice_add_vsi_to_fdir(pf, vsi);
8566 
8567 	ch->sw_id = sw_id;
8568 	ch->vsi_num = vsi->vsi_num;
8569 	ch->info.mapping_flags = vsi->info.mapping_flags;
8570 	ch->ch_vsi = vsi;
8571 	/* set the back pointer of channel for newly created VSI */
8572 	vsi->ch = ch;
8573 
8574 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8575 	       sizeof(vsi->info.q_mapping));
8576 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8577 	       sizeof(vsi->info.tc_mapping));
8578 
8579 	return 0;
8580 }
8581 
8582 /**
8583  * ice_chnl_cfg_res
8584  * @vsi: the VSI being setup
8585  * @ch: ptr to channel structure
8586  *
8587  * Configure channel specific resources such as rings, vector.
8588  */
8589 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8590 {
8591 	int i;
8592 
8593 	for (i = 0; i < ch->num_txq; i++) {
8594 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8595 		struct ice_ring_container *rc;
8596 		struct ice_tx_ring *tx_ring;
8597 		struct ice_rx_ring *rx_ring;
8598 
8599 		tx_ring = vsi->tx_rings[ch->base_q + i];
8600 		rx_ring = vsi->rx_rings[ch->base_q + i];
8601 		if (!tx_ring || !rx_ring)
8602 			continue;
8603 
8604 		/* setup ring being channel enabled */
8605 		tx_ring->ch = ch;
8606 		rx_ring->ch = ch;
8607 
8608 		/* following code block sets up vector specific attributes */
8609 		tx_q_vector = tx_ring->q_vector;
8610 		rx_q_vector = rx_ring->q_vector;
8611 		if (!tx_q_vector && !rx_q_vector)
8612 			continue;
8613 
8614 		if (tx_q_vector) {
8615 			tx_q_vector->ch = ch;
8616 			/* setup Tx and Rx ITR setting if DIM is off */
8617 			rc = &tx_q_vector->tx;
8618 			if (!ITR_IS_DYNAMIC(rc))
8619 				ice_write_itr(rc, rc->itr_setting);
8620 		}
8621 		if (rx_q_vector) {
8622 			rx_q_vector->ch = ch;
8623 			/* setup Tx and Rx ITR setting if DIM is off */
8624 			rc = &rx_q_vector->rx;
8625 			if (!ITR_IS_DYNAMIC(rc))
8626 				ice_write_itr(rc, rc->itr_setting);
8627 		}
8628 	}
8629 
8630 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8631 	 * GLINT_ITR register would have written to perform in-context
8632 	 * update, hence perform flush
8633 	 */
8634 	if (ch->num_txq || ch->num_rxq)
8635 		ice_flush(&vsi->back->hw);
8636 }
8637 
8638 /**
8639  * ice_cfg_chnl_all_res - configure channel resources
8640  * @vsi: pte to main_vsi
8641  * @ch: ptr to channel structure
8642  *
8643  * This function configures channel specific resources such as flow-director
8644  * counter index, and other resources such as queues, vectors, ITR settings
8645  */
8646 static void
8647 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8648 {
8649 	/* configure channel (aka ADQ) resources such as queues, vectors,
8650 	 * ITR settings for channel specific vectors and anything else
8651 	 */
8652 	ice_chnl_cfg_res(vsi, ch);
8653 }
8654 
8655 /**
8656  * ice_setup_hw_channel - setup new channel
8657  * @pf: ptr to PF device
8658  * @vsi: the VSI being setup
8659  * @ch: ptr to channel structure
8660  * @sw_id: underlying HW switching element ID
8661  * @type: type of channel to be created (VMDq2/VF)
8662  *
8663  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8664  * and configures Tx rings accordingly
8665  */
8666 static int
8667 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8668 		     struct ice_channel *ch, u16 sw_id, u8 type)
8669 {
8670 	struct device *dev = ice_pf_to_dev(pf);
8671 	int ret;
8672 
8673 	ch->base_q = vsi->next_base_q;
8674 	ch->type = type;
8675 
8676 	ret = ice_add_channel(pf, sw_id, ch);
8677 	if (ret) {
8678 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8679 		return ret;
8680 	}
8681 
8682 	/* configure/setup ADQ specific resources */
8683 	ice_cfg_chnl_all_res(vsi, ch);
8684 
8685 	/* make sure to update the next_base_q so that subsequent channel's
8686 	 * (aka ADQ) VSI queue map is correct
8687 	 */
8688 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8689 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8690 		ch->num_rxq);
8691 
8692 	return 0;
8693 }
8694 
8695 /**
8696  * ice_setup_channel - setup new channel using uplink element
8697  * @pf: ptr to PF device
8698  * @vsi: the VSI being setup
8699  * @ch: ptr to channel structure
8700  *
8701  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8702  * and uplink switching element
8703  */
8704 static bool
8705 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8706 		  struct ice_channel *ch)
8707 {
8708 	struct device *dev = ice_pf_to_dev(pf);
8709 	u16 sw_id;
8710 	int ret;
8711 
8712 	if (vsi->type != ICE_VSI_PF) {
8713 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8714 		return false;
8715 	}
8716 
8717 	sw_id = pf->first_sw->sw_id;
8718 
8719 	/* create channel (VSI) */
8720 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8721 	if (ret) {
8722 		dev_err(dev, "failed to setup hw_channel\n");
8723 		return false;
8724 	}
8725 	dev_dbg(dev, "successfully created channel()\n");
8726 
8727 	return ch->ch_vsi ? true : false;
8728 }
8729 
8730 /**
8731  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8732  * @vsi: VSI to be configured
8733  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8734  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8735  */
8736 static int
8737 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8738 {
8739 	int err;
8740 
8741 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8742 	if (err)
8743 		return err;
8744 
8745 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8746 }
8747 
8748 /**
8749  * ice_create_q_channel - function to create channel
8750  * @vsi: VSI to be configured
8751  * @ch: ptr to channel (it contains channel specific params)
8752  *
8753  * This function creates channel (VSI) using num_queues specified by user,
8754  * reconfigs RSS if needed.
8755  */
8756 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8757 {
8758 	struct ice_pf *pf = vsi->back;
8759 	struct device *dev;
8760 
8761 	if (!ch)
8762 		return -EINVAL;
8763 
8764 	dev = ice_pf_to_dev(pf);
8765 	if (!ch->num_txq || !ch->num_rxq) {
8766 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8767 		return -EINVAL;
8768 	}
8769 
8770 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8771 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8772 			vsi->cnt_q_avail, ch->num_txq);
8773 		return -EINVAL;
8774 	}
8775 
8776 	if (!ice_setup_channel(pf, vsi, ch)) {
8777 		dev_info(dev, "Failed to setup channel\n");
8778 		return -EINVAL;
8779 	}
8780 	/* configure BW rate limit */
8781 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8782 		int ret;
8783 
8784 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8785 				       ch->min_tx_rate);
8786 		if (ret)
8787 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8788 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8789 		else
8790 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8791 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8792 	}
8793 
8794 	vsi->cnt_q_avail -= ch->num_txq;
8795 
8796 	return 0;
8797 }
8798 
8799 /**
8800  * ice_rem_all_chnl_fltrs - removes all channel filters
8801  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8802  *
8803  * Remove all advanced switch filters only if they are channel specific
8804  * tc-flower based filter
8805  */
8806 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8807 {
8808 	struct ice_tc_flower_fltr *fltr;
8809 	struct hlist_node *node;
8810 
8811 	/* to remove all channel filters, iterate an ordered list of filters */
8812 	hlist_for_each_entry_safe(fltr, node,
8813 				  &pf->tc_flower_fltr_list,
8814 				  tc_flower_node) {
8815 		struct ice_rule_query_data rule;
8816 		int status;
8817 
8818 		/* for now process only channel specific filters */
8819 		if (!ice_is_chnl_fltr(fltr))
8820 			continue;
8821 
8822 		rule.rid = fltr->rid;
8823 		rule.rule_id = fltr->rule_id;
8824 		rule.vsi_handle = fltr->dest_vsi_handle;
8825 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8826 		if (status) {
8827 			if (status == -ENOENT)
8828 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8829 					rule.rule_id);
8830 			else
8831 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8832 					status);
8833 		} else if (fltr->dest_vsi) {
8834 			/* update advanced switch filter count */
8835 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8836 				u32 flags = fltr->flags;
8837 
8838 				fltr->dest_vsi->num_chnl_fltr--;
8839 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8840 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8841 					pf->num_dmac_chnl_fltrs--;
8842 			}
8843 		}
8844 
8845 		hlist_del(&fltr->tc_flower_node);
8846 		kfree(fltr);
8847 	}
8848 }
8849 
8850 /**
8851  * ice_remove_q_channels - Remove queue channels for the TCs
8852  * @vsi: VSI to be configured
8853  * @rem_fltr: delete advanced switch filter or not
8854  *
8855  * Remove queue channels for the TCs
8856  */
8857 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8858 {
8859 	struct ice_channel *ch, *ch_tmp;
8860 	struct ice_pf *pf = vsi->back;
8861 	int i;
8862 
8863 	/* remove all tc-flower based filter if they are channel filters only */
8864 	if (rem_fltr)
8865 		ice_rem_all_chnl_fltrs(pf);
8866 
8867 	/* remove ntuple filters since queue configuration is being changed */
8868 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8869 		struct ice_hw *hw = &pf->hw;
8870 
8871 		mutex_lock(&hw->fdir_fltr_lock);
8872 		ice_fdir_del_all_fltrs(vsi);
8873 		mutex_unlock(&hw->fdir_fltr_lock);
8874 	}
8875 
8876 	/* perform cleanup for channels if they exist */
8877 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8878 		struct ice_vsi *ch_vsi;
8879 
8880 		list_del(&ch->list);
8881 		ch_vsi = ch->ch_vsi;
8882 		if (!ch_vsi) {
8883 			kfree(ch);
8884 			continue;
8885 		}
8886 
8887 		/* Reset queue contexts */
8888 		for (i = 0; i < ch->num_rxq; i++) {
8889 			struct ice_tx_ring *tx_ring;
8890 			struct ice_rx_ring *rx_ring;
8891 
8892 			tx_ring = vsi->tx_rings[ch->base_q + i];
8893 			rx_ring = vsi->rx_rings[ch->base_q + i];
8894 			if (tx_ring) {
8895 				tx_ring->ch = NULL;
8896 				if (tx_ring->q_vector)
8897 					tx_ring->q_vector->ch = NULL;
8898 			}
8899 			if (rx_ring) {
8900 				rx_ring->ch = NULL;
8901 				if (rx_ring->q_vector)
8902 					rx_ring->q_vector->ch = NULL;
8903 			}
8904 		}
8905 
8906 		/* Release FD resources for the channel VSI */
8907 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8908 
8909 		/* clear the VSI from scheduler tree */
8910 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8911 
8912 		/* Delete VSI from FW, PF and HW VSI arrays */
8913 		ice_vsi_delete(ch->ch_vsi);
8914 
8915 		/* free the channel */
8916 		kfree(ch);
8917 	}
8918 
8919 	/* clear the channel VSI map which is stored in main VSI */
8920 	ice_for_each_chnl_tc(i)
8921 		vsi->tc_map_vsi[i] = NULL;
8922 
8923 	/* reset main VSI's all TC information */
8924 	vsi->all_enatc = 0;
8925 	vsi->all_numtc = 0;
8926 }
8927 
8928 /**
8929  * ice_rebuild_channels - rebuild channel
8930  * @pf: ptr to PF
8931  *
8932  * Recreate channel VSIs and replay filters
8933  */
8934 static int ice_rebuild_channels(struct ice_pf *pf)
8935 {
8936 	struct device *dev = ice_pf_to_dev(pf);
8937 	struct ice_vsi *main_vsi;
8938 	bool rem_adv_fltr = true;
8939 	struct ice_channel *ch;
8940 	struct ice_vsi *vsi;
8941 	int tc_idx = 1;
8942 	int i, err;
8943 
8944 	main_vsi = ice_get_main_vsi(pf);
8945 	if (!main_vsi)
8946 		return 0;
8947 
8948 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8949 	    main_vsi->old_numtc == 1)
8950 		return 0; /* nothing to be done */
8951 
8952 	/* reconfigure main VSI based on old value of TC and cached values
8953 	 * for MQPRIO opts
8954 	 */
8955 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8956 	if (err) {
8957 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8958 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8959 		return err;
8960 	}
8961 
8962 	/* rebuild ADQ VSIs */
8963 	ice_for_each_vsi(pf, i) {
8964 		enum ice_vsi_type type;
8965 
8966 		vsi = pf->vsi[i];
8967 		if (!vsi || vsi->type != ICE_VSI_CHNL)
8968 			continue;
8969 
8970 		type = vsi->type;
8971 
8972 		/* rebuild ADQ VSI */
8973 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8974 		if (err) {
8975 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8976 				ice_vsi_type_str(type), vsi->idx, err);
8977 			goto cleanup;
8978 		}
8979 
8980 		/* Re-map HW VSI number, using VSI handle that has been
8981 		 * previously validated in ice_replay_vsi() call above
8982 		 */
8983 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8984 
8985 		/* replay filters for the VSI */
8986 		err = ice_replay_vsi(&pf->hw, vsi->idx);
8987 		if (err) {
8988 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8989 				ice_vsi_type_str(type), err, vsi->idx);
8990 			rem_adv_fltr = false;
8991 			goto cleanup;
8992 		}
8993 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8994 			 ice_vsi_type_str(type), vsi->idx);
8995 
8996 		/* store ADQ VSI at correct TC index in main VSI's
8997 		 * map of TC to VSI
8998 		 */
8999 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
9000 	}
9001 
9002 	/* ADQ VSI(s) has been rebuilt successfully, so setup
9003 	 * channel for main VSI's Tx and Rx rings
9004 	 */
9005 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
9006 		struct ice_vsi *ch_vsi;
9007 
9008 		ch_vsi = ch->ch_vsi;
9009 		if (!ch_vsi)
9010 			continue;
9011 
9012 		/* reconfig channel resources */
9013 		ice_cfg_chnl_all_res(main_vsi, ch);
9014 
9015 		/* replay BW rate limit if it is non-zero */
9016 		if (!ch->max_tx_rate && !ch->min_tx_rate)
9017 			continue;
9018 
9019 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
9020 				       ch->min_tx_rate);
9021 		if (err)
9022 			dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9023 				err, ch->max_tx_rate, ch->min_tx_rate,
9024 				ch_vsi->vsi_num);
9025 		else
9026 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9027 				ch->max_tx_rate, ch->min_tx_rate,
9028 				ch_vsi->vsi_num);
9029 	}
9030 
9031 	/* reconfig RSS for main VSI */
9032 	if (main_vsi->ch_rss_size)
9033 		ice_vsi_cfg_rss_lut_key(main_vsi);
9034 
9035 	return 0;
9036 
9037 cleanup:
9038 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
9039 	return err;
9040 }
9041 
9042 /**
9043  * ice_create_q_channels - Add queue channel for the given TCs
9044  * @vsi: VSI to be configured
9045  *
9046  * Configures queue channel mapping to the given TCs
9047  */
9048 static int ice_create_q_channels(struct ice_vsi *vsi)
9049 {
9050 	struct ice_pf *pf = vsi->back;
9051 	struct ice_channel *ch;
9052 	int ret = 0, i;
9053 
9054 	ice_for_each_chnl_tc(i) {
9055 		if (!(vsi->all_enatc & BIT(i)))
9056 			continue;
9057 
9058 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
9059 		if (!ch) {
9060 			ret = -ENOMEM;
9061 			goto err_free;
9062 		}
9063 		INIT_LIST_HEAD(&ch->list);
9064 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
9065 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
9066 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
9067 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
9068 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
9069 
9070 		/* convert to Kbits/s */
9071 		if (ch->max_tx_rate)
9072 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
9073 						  ICE_BW_KBPS_DIVISOR);
9074 		if (ch->min_tx_rate)
9075 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
9076 						  ICE_BW_KBPS_DIVISOR);
9077 
9078 		ret = ice_create_q_channel(vsi, ch);
9079 		if (ret) {
9080 			dev_err(ice_pf_to_dev(pf),
9081 				"failed creating channel TC:%d\n", i);
9082 			kfree(ch);
9083 			goto err_free;
9084 		}
9085 		list_add_tail(&ch->list, &vsi->ch_list);
9086 		vsi->tc_map_vsi[i] = ch->ch_vsi;
9087 		dev_dbg(ice_pf_to_dev(pf),
9088 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
9089 	}
9090 	return 0;
9091 
9092 err_free:
9093 	ice_remove_q_channels(vsi, false);
9094 
9095 	return ret;
9096 }
9097 
9098 /**
9099  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
9100  * @netdev: net device to configure
9101  * @type_data: TC offload data
9102  */
9103 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
9104 {
9105 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
9106 	struct ice_netdev_priv *np = netdev_priv(netdev);
9107 	struct ice_vsi *vsi = np->vsi;
9108 	struct ice_pf *pf = vsi->back;
9109 	u16 mode, ena_tc_qdisc = 0;
9110 	int cur_txq, cur_rxq;
9111 	u8 hw = 0, num_tcf;
9112 	struct device *dev;
9113 	int ret, i;
9114 
9115 	dev = ice_pf_to_dev(pf);
9116 	num_tcf = mqprio_qopt->qopt.num_tc;
9117 	hw = mqprio_qopt->qopt.hw;
9118 	mode = mqprio_qopt->mode;
9119 	if (!hw) {
9120 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9121 		vsi->ch_rss_size = 0;
9122 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9123 		goto config_tcf;
9124 	}
9125 
9126 	/* Generate queue region map for number of TCF requested */
9127 	for (i = 0; i < num_tcf; i++)
9128 		ena_tc_qdisc |= BIT(i);
9129 
9130 	switch (mode) {
9131 	case TC_MQPRIO_MODE_CHANNEL:
9132 
9133 		if (pf->hw.port_info->is_custom_tx_enabled) {
9134 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
9135 			return -EBUSY;
9136 		}
9137 		ice_tear_down_devlink_rate_tree(pf);
9138 
9139 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
9140 		if (ret) {
9141 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
9142 				   ret);
9143 			return ret;
9144 		}
9145 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9146 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9147 		/* don't assume state of hw_tc_offload during driver load
9148 		 * and set the flag for TC flower filter if hw_tc_offload
9149 		 * already ON
9150 		 */
9151 		if (vsi->netdev->features & NETIF_F_HW_TC)
9152 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
9153 		break;
9154 	default:
9155 		return -EINVAL;
9156 	}
9157 
9158 config_tcf:
9159 
9160 	/* Requesting same TCF configuration as already enabled */
9161 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9162 	    mode != TC_MQPRIO_MODE_CHANNEL)
9163 		return 0;
9164 
9165 	/* Pause VSI queues */
9166 	ice_dis_vsi(vsi, true);
9167 
9168 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9169 		ice_remove_q_channels(vsi, true);
9170 
9171 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9172 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9173 				     num_online_cpus());
9174 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9175 				     num_online_cpus());
9176 	} else {
9177 		/* logic to rebuild VSI, same like ethtool -L */
9178 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9179 
9180 		for (i = 0; i < num_tcf; i++) {
9181 			if (!(ena_tc_qdisc & BIT(i)))
9182 				continue;
9183 
9184 			offset = vsi->mqprio_qopt.qopt.offset[i];
9185 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9186 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9187 		}
9188 		vsi->req_txq = offset + qcount_tx;
9189 		vsi->req_rxq = offset + qcount_rx;
9190 
9191 		/* store away original rss_size info, so that it gets reused
9192 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9193 		 * determine, what should be the rss_sizefor main VSI
9194 		 */
9195 		vsi->orig_rss_size = vsi->rss_size;
9196 	}
9197 
9198 	/* save current values of Tx and Rx queues before calling VSI rebuild
9199 	 * for fallback option
9200 	 */
9201 	cur_txq = vsi->num_txq;
9202 	cur_rxq = vsi->num_rxq;
9203 
9204 	/* proceed with rebuild main VSI using correct number of queues */
9205 	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
9206 	if (ret) {
9207 		/* fallback to current number of queues */
9208 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9209 		vsi->req_txq = cur_txq;
9210 		vsi->req_rxq = cur_rxq;
9211 		clear_bit(ICE_RESET_FAILED, pf->state);
9212 		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
9213 			dev_err(dev, "Rebuild of main VSI failed again\n");
9214 			return ret;
9215 		}
9216 	}
9217 
9218 	vsi->all_numtc = num_tcf;
9219 	vsi->all_enatc = ena_tc_qdisc;
9220 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9221 	if (ret) {
9222 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9223 			   vsi->vsi_num);
9224 		goto exit;
9225 	}
9226 
9227 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9228 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9229 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9230 
9231 		/* set TC0 rate limit if specified */
9232 		if (max_tx_rate || min_tx_rate) {
9233 			/* convert to Kbits/s */
9234 			if (max_tx_rate)
9235 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9236 			if (min_tx_rate)
9237 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9238 
9239 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9240 			if (!ret) {
9241 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9242 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9243 			} else {
9244 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9245 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9246 				goto exit;
9247 			}
9248 		}
9249 		ret = ice_create_q_channels(vsi);
9250 		if (ret) {
9251 			netdev_err(netdev, "failed configuring queue channels\n");
9252 			goto exit;
9253 		} else {
9254 			netdev_dbg(netdev, "successfully configured channels\n");
9255 		}
9256 	}
9257 
9258 	if (vsi->ch_rss_size)
9259 		ice_vsi_cfg_rss_lut_key(vsi);
9260 
9261 exit:
9262 	/* if error, reset the all_numtc and all_enatc */
9263 	if (ret) {
9264 		vsi->all_numtc = 0;
9265 		vsi->all_enatc = 0;
9266 	}
9267 	/* resume VSI */
9268 	ice_ena_vsi(vsi, true);
9269 
9270 	return ret;
9271 }
9272 
9273 static LIST_HEAD(ice_block_cb_list);
9274 
9275 static int
9276 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9277 	     void *type_data)
9278 {
9279 	struct ice_netdev_priv *np = netdev_priv(netdev);
9280 	struct ice_pf *pf = np->vsi->back;
9281 	bool locked = false;
9282 	int err;
9283 
9284 	switch (type) {
9285 	case TC_SETUP_BLOCK:
9286 		return flow_block_cb_setup_simple(type_data,
9287 						  &ice_block_cb_list,
9288 						  ice_setup_tc_block_cb,
9289 						  np, np, true);
9290 	case TC_SETUP_QDISC_MQPRIO:
9291 		if (ice_is_eswitch_mode_switchdev(pf)) {
9292 			netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9293 			return -EOPNOTSUPP;
9294 		}
9295 
9296 		if (pf->adev) {
9297 			mutex_lock(&pf->adev_mutex);
9298 			device_lock(&pf->adev->dev);
9299 			locked = true;
9300 			if (pf->adev->dev.driver) {
9301 				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9302 				err = -EBUSY;
9303 				goto adev_unlock;
9304 			}
9305 		}
9306 
9307 		/* setup traffic classifier for receive side */
9308 		mutex_lock(&pf->tc_mutex);
9309 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9310 		mutex_unlock(&pf->tc_mutex);
9311 
9312 adev_unlock:
9313 		if (locked) {
9314 			device_unlock(&pf->adev->dev);
9315 			mutex_unlock(&pf->adev_mutex);
9316 		}
9317 		return err;
9318 	default:
9319 		return -EOPNOTSUPP;
9320 	}
9321 	return -EOPNOTSUPP;
9322 }
9323 
9324 static struct ice_indr_block_priv *
9325 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9326 			   struct net_device *netdev)
9327 {
9328 	struct ice_indr_block_priv *cb_priv;
9329 
9330 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9331 		if (!cb_priv->netdev)
9332 			return NULL;
9333 		if (cb_priv->netdev == netdev)
9334 			return cb_priv;
9335 	}
9336 	return NULL;
9337 }
9338 
9339 static int
9340 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9341 			void *indr_priv)
9342 {
9343 	struct ice_indr_block_priv *priv = indr_priv;
9344 	struct ice_netdev_priv *np = priv->np;
9345 
9346 	switch (type) {
9347 	case TC_SETUP_CLSFLOWER:
9348 		return ice_setup_tc_cls_flower(np, priv->netdev,
9349 					       (struct flow_cls_offload *)
9350 					       type_data);
9351 	default:
9352 		return -EOPNOTSUPP;
9353 	}
9354 }
9355 
9356 static int
9357 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9358 			struct ice_netdev_priv *np,
9359 			struct flow_block_offload *f, void *data,
9360 			void (*cleanup)(struct flow_block_cb *block_cb))
9361 {
9362 	struct ice_indr_block_priv *indr_priv;
9363 	struct flow_block_cb *block_cb;
9364 
9365 	if (!ice_is_tunnel_supported(netdev) &&
9366 	    !(is_vlan_dev(netdev) &&
9367 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
9368 		return -EOPNOTSUPP;
9369 
9370 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9371 		return -EOPNOTSUPP;
9372 
9373 	switch (f->command) {
9374 	case FLOW_BLOCK_BIND:
9375 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9376 		if (indr_priv)
9377 			return -EEXIST;
9378 
9379 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9380 		if (!indr_priv)
9381 			return -ENOMEM;
9382 
9383 		indr_priv->netdev = netdev;
9384 		indr_priv->np = np;
9385 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9386 
9387 		block_cb =
9388 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9389 						 indr_priv, indr_priv,
9390 						 ice_rep_indr_tc_block_unbind,
9391 						 f, netdev, sch, data, np,
9392 						 cleanup);
9393 
9394 		if (IS_ERR(block_cb)) {
9395 			list_del(&indr_priv->list);
9396 			kfree(indr_priv);
9397 			return PTR_ERR(block_cb);
9398 		}
9399 		flow_block_cb_add(block_cb, f);
9400 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9401 		break;
9402 	case FLOW_BLOCK_UNBIND:
9403 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9404 		if (!indr_priv)
9405 			return -ENOENT;
9406 
9407 		block_cb = flow_block_cb_lookup(f->block,
9408 						ice_indr_setup_block_cb,
9409 						indr_priv);
9410 		if (!block_cb)
9411 			return -ENOENT;
9412 
9413 		flow_indr_block_cb_remove(block_cb, f);
9414 
9415 		list_del(&block_cb->driver_list);
9416 		break;
9417 	default:
9418 		return -EOPNOTSUPP;
9419 	}
9420 	return 0;
9421 }
9422 
9423 static int
9424 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9425 		     void *cb_priv, enum tc_setup_type type, void *type_data,
9426 		     void *data,
9427 		     void (*cleanup)(struct flow_block_cb *block_cb))
9428 {
9429 	switch (type) {
9430 	case TC_SETUP_BLOCK:
9431 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9432 					       data, cleanup);
9433 
9434 	default:
9435 		return -EOPNOTSUPP;
9436 	}
9437 }
9438 
9439 /**
9440  * ice_open - Called when a network interface becomes active
9441  * @netdev: network interface device structure
9442  *
9443  * The open entry point is called when a network interface is made
9444  * active by the system (IFF_UP). At this point all resources needed
9445  * for transmit and receive operations are allocated, the interrupt
9446  * handler is registered with the OS, the netdev watchdog is enabled,
9447  * and the stack is notified that the interface is ready.
9448  *
9449  * Returns 0 on success, negative value on failure
9450  */
9451 int ice_open(struct net_device *netdev)
9452 {
9453 	struct ice_netdev_priv *np = netdev_priv(netdev);
9454 	struct ice_pf *pf = np->vsi->back;
9455 
9456 	if (ice_is_reset_in_progress(pf->state)) {
9457 		netdev_err(netdev, "can't open net device while reset is in progress");
9458 		return -EBUSY;
9459 	}
9460 
9461 	return ice_open_internal(netdev);
9462 }
9463 
9464 /**
9465  * ice_open_internal - Called when a network interface becomes active
9466  * @netdev: network interface device structure
9467  *
9468  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9469  * handling routine
9470  *
9471  * Returns 0 on success, negative value on failure
9472  */
9473 int ice_open_internal(struct net_device *netdev)
9474 {
9475 	struct ice_netdev_priv *np = netdev_priv(netdev);
9476 	struct ice_vsi *vsi = np->vsi;
9477 	struct ice_pf *pf = vsi->back;
9478 	struct ice_port_info *pi;
9479 	int err;
9480 
9481 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9482 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9483 		return -EIO;
9484 	}
9485 
9486 	netif_carrier_off(netdev);
9487 
9488 	pi = vsi->port_info;
9489 	err = ice_update_link_info(pi);
9490 	if (err) {
9491 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9492 		return err;
9493 	}
9494 
9495 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9496 
9497 	/* Set PHY if there is media, otherwise, turn off PHY */
9498 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9499 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9500 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9501 			err = ice_init_phy_user_cfg(pi);
9502 			if (err) {
9503 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9504 					   err);
9505 				return err;
9506 			}
9507 		}
9508 
9509 		err = ice_configure_phy(vsi);
9510 		if (err) {
9511 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9512 				   err);
9513 			return err;
9514 		}
9515 	} else {
9516 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9517 		ice_set_link(vsi, false);
9518 	}
9519 
9520 	err = ice_vsi_open(vsi);
9521 	if (err)
9522 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9523 			   vsi->vsi_num, vsi->vsw->sw_id);
9524 
9525 	/* Update existing tunnels information */
9526 	udp_tunnel_get_rx_info(netdev);
9527 
9528 	return err;
9529 }
9530 
9531 /**
9532  * ice_stop - Disables a network interface
9533  * @netdev: network interface device structure
9534  *
9535  * The stop entry point is called when an interface is de-activated by the OS,
9536  * and the netdevice enters the DOWN state. The hardware is still under the
9537  * driver's control, but the netdev interface is disabled.
9538  *
9539  * Returns success only - not allowed to fail
9540  */
9541 int ice_stop(struct net_device *netdev)
9542 {
9543 	struct ice_netdev_priv *np = netdev_priv(netdev);
9544 	struct ice_vsi *vsi = np->vsi;
9545 	struct ice_pf *pf = vsi->back;
9546 
9547 	if (ice_is_reset_in_progress(pf->state)) {
9548 		netdev_err(netdev, "can't stop net device while reset is in progress");
9549 		return -EBUSY;
9550 	}
9551 
9552 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9553 		int link_err = ice_force_phys_link_state(vsi, false);
9554 
9555 		if (link_err) {
9556 			if (link_err == -ENOMEDIUM)
9557 				netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9558 					    vsi->vsi_num);
9559 			else
9560 				netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9561 					   vsi->vsi_num, link_err);
9562 
9563 			ice_vsi_close(vsi);
9564 			return -EIO;
9565 		}
9566 	}
9567 
9568 	ice_vsi_close(vsi);
9569 
9570 	return 0;
9571 }
9572 
9573 /**
9574  * ice_features_check - Validate encapsulated packet conforms to limits
9575  * @skb: skb buffer
9576  * @netdev: This port's netdev
9577  * @features: Offload features that the stack believes apply
9578  */
9579 static netdev_features_t
9580 ice_features_check(struct sk_buff *skb,
9581 		   struct net_device __always_unused *netdev,
9582 		   netdev_features_t features)
9583 {
9584 	bool gso = skb_is_gso(skb);
9585 	size_t len;
9586 
9587 	/* No point in doing any of this if neither checksum nor GSO are
9588 	 * being requested for this frame. We can rule out both by just
9589 	 * checking for CHECKSUM_PARTIAL
9590 	 */
9591 	if (skb->ip_summed != CHECKSUM_PARTIAL)
9592 		return features;
9593 
9594 	/* We cannot support GSO if the MSS is going to be less than
9595 	 * 64 bytes. If it is then we need to drop support for GSO.
9596 	 */
9597 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9598 		features &= ~NETIF_F_GSO_MASK;
9599 
9600 	len = skb_network_offset(skb);
9601 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9602 		goto out_rm_features;
9603 
9604 	len = skb_network_header_len(skb);
9605 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9606 		goto out_rm_features;
9607 
9608 	if (skb->encapsulation) {
9609 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9610 		 * the case of IPIP frames, the transport header pointer is
9611 		 * after the inner header! So check to make sure that this
9612 		 * is a GRE or UDP_TUNNEL frame before doing that math.
9613 		 */
9614 		if (gso && (skb_shinfo(skb)->gso_type &
9615 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9616 			len = skb_inner_network_header(skb) -
9617 			      skb_transport_header(skb);
9618 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9619 				goto out_rm_features;
9620 		}
9621 
9622 		len = skb_inner_network_header_len(skb);
9623 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9624 			goto out_rm_features;
9625 	}
9626 
9627 	return features;
9628 out_rm_features:
9629 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9630 }
9631 
9632 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9633 	.ndo_open = ice_open,
9634 	.ndo_stop = ice_stop,
9635 	.ndo_start_xmit = ice_start_xmit,
9636 	.ndo_set_mac_address = ice_set_mac_address,
9637 	.ndo_validate_addr = eth_validate_addr,
9638 	.ndo_change_mtu = ice_change_mtu,
9639 	.ndo_get_stats64 = ice_get_stats64,
9640 	.ndo_tx_timeout = ice_tx_timeout,
9641 	.ndo_bpf = ice_xdp_safe_mode,
9642 };
9643 
9644 static const struct net_device_ops ice_netdev_ops = {
9645 	.ndo_open = ice_open,
9646 	.ndo_stop = ice_stop,
9647 	.ndo_start_xmit = ice_start_xmit,
9648 	.ndo_select_queue = ice_select_queue,
9649 	.ndo_features_check = ice_features_check,
9650 	.ndo_fix_features = ice_fix_features,
9651 	.ndo_set_rx_mode = ice_set_rx_mode,
9652 	.ndo_set_mac_address = ice_set_mac_address,
9653 	.ndo_validate_addr = eth_validate_addr,
9654 	.ndo_change_mtu = ice_change_mtu,
9655 	.ndo_get_stats64 = ice_get_stats64,
9656 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9657 	.ndo_eth_ioctl = ice_eth_ioctl,
9658 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9659 	.ndo_set_vf_mac = ice_set_vf_mac,
9660 	.ndo_get_vf_config = ice_get_vf_cfg,
9661 	.ndo_set_vf_trust = ice_set_vf_trust,
9662 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9663 	.ndo_set_vf_link_state = ice_set_vf_link_state,
9664 	.ndo_get_vf_stats = ice_get_vf_stats,
9665 	.ndo_set_vf_rate = ice_set_vf_bw,
9666 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9667 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9668 	.ndo_setup_tc = ice_setup_tc,
9669 	.ndo_set_features = ice_set_features,
9670 	.ndo_bridge_getlink = ice_bridge_getlink,
9671 	.ndo_bridge_setlink = ice_bridge_setlink,
9672 	.ndo_fdb_add = ice_fdb_add,
9673 	.ndo_fdb_del = ice_fdb_del,
9674 #ifdef CONFIG_RFS_ACCEL
9675 	.ndo_rx_flow_steer = ice_rx_flow_steer,
9676 #endif
9677 	.ndo_tx_timeout = ice_tx_timeout,
9678 	.ndo_bpf = ice_xdp,
9679 	.ndo_xdp_xmit = ice_xdp_xmit,
9680 	.ndo_xsk_wakeup = ice_xsk_wakeup,
9681 };
9682