xref: /linux/drivers/net/ethernet/intel/ice/ice_main.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
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 	u8 *buf_copy;
4540 	int err;
4541 
4542 	dev = ice_pf_to_dev(pf);
4543 	/* ice_cfg_tx_topo buf argument is not a constant,
4544 	 * so we have to make a copy
4545 	 */
4546 	buf_copy = kmemdup(firmware->data, firmware->size, GFP_KERNEL);
4547 
4548 	err = ice_cfg_tx_topo(hw, buf_copy, firmware->size);
4549 	if (!err) {
4550 		if (hw->num_tx_sched_layers > num_tx_sched_layers)
4551 			dev_info(dev, "Tx scheduling layers switching feature disabled\n");
4552 		else
4553 			dev_info(dev, "Tx scheduling layers switching feature enabled\n");
4554 		/* if there was a change in topology ice_cfg_tx_topo triggered
4555 		 * a CORER and we need to re-init hw
4556 		 */
4557 		ice_deinit_hw(hw);
4558 		err = ice_init_hw(hw);
4559 
4560 		return err;
4561 	} else if (err == -EIO) {
4562 		dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
4563 	}
4564 
4565 	return 0;
4566 }
4567 
4568 /**
4569  * ice_init_ddp_config - DDP related configuration
4570  * @hw: pointer to the hardware structure
4571  * @pf: pointer to pf structure
4572  *
4573  * This function loads DDP file from the disk, then initializes Tx
4574  * topology. At the end DDP package is loaded on the card.
4575  *
4576  * Return: zero when init was successful, negative values otherwise.
4577  */
4578 static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
4579 {
4580 	struct device *dev = ice_pf_to_dev(pf);
4581 	const struct firmware *firmware = NULL;
4582 	int err;
4583 
4584 	err = ice_request_fw(pf, &firmware);
4585 	if (err) {
4586 		dev_err(dev, "Fail during requesting FW: %d\n", err);
4587 		return err;
4588 	}
4589 
4590 	err = ice_init_tx_topology(hw, firmware);
4591 	if (err) {
4592 		dev_err(dev, "Fail during initialization of Tx topology: %d\n",
4593 			err);
4594 		release_firmware(firmware);
4595 		return err;
4596 	}
4597 
4598 	/* Download firmware to device */
4599 	ice_load_pkg(firmware, pf);
4600 	release_firmware(firmware);
4601 
4602 	return 0;
4603 }
4604 
4605 /**
4606  * ice_print_wake_reason - show the wake up cause in the log
4607  * @pf: pointer to the PF struct
4608  */
4609 static void ice_print_wake_reason(struct ice_pf *pf)
4610 {
4611 	u32 wus = pf->wakeup_reason;
4612 	const char *wake_str;
4613 
4614 	/* if no wake event, nothing to print */
4615 	if (!wus)
4616 		return;
4617 
4618 	if (wus & PFPM_WUS_LNKC_M)
4619 		wake_str = "Link\n";
4620 	else if (wus & PFPM_WUS_MAG_M)
4621 		wake_str = "Magic Packet\n";
4622 	else if (wus & PFPM_WUS_MNG_M)
4623 		wake_str = "Management\n";
4624 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4625 		wake_str = "Firmware Reset\n";
4626 	else
4627 		wake_str = "Unknown\n";
4628 
4629 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4630 }
4631 
4632 /**
4633  * ice_pf_fwlog_update_module - update 1 module
4634  * @pf: pointer to the PF struct
4635  * @log_level: log_level to use for the @module
4636  * @module: module to update
4637  */
4638 void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4639 {
4640 	struct ice_hw *hw = &pf->hw;
4641 
4642 	hw->fwlog_cfg.module_entries[module].log_level = log_level;
4643 }
4644 
4645 /**
4646  * ice_register_netdev - register netdev
4647  * @vsi: pointer to the VSI struct
4648  */
4649 static int ice_register_netdev(struct ice_vsi *vsi)
4650 {
4651 	int err;
4652 
4653 	if (!vsi || !vsi->netdev)
4654 		return -EIO;
4655 
4656 	err = register_netdev(vsi->netdev);
4657 	if (err)
4658 		return err;
4659 
4660 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4661 	netif_carrier_off(vsi->netdev);
4662 	netif_tx_stop_all_queues(vsi->netdev);
4663 
4664 	return 0;
4665 }
4666 
4667 static void ice_unregister_netdev(struct ice_vsi *vsi)
4668 {
4669 	if (!vsi || !vsi->netdev)
4670 		return;
4671 
4672 	unregister_netdev(vsi->netdev);
4673 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4674 }
4675 
4676 /**
4677  * ice_cfg_netdev - Allocate, configure and register a netdev
4678  * @vsi: the VSI associated with the new netdev
4679  *
4680  * Returns 0 on success, negative value on failure
4681  */
4682 static int ice_cfg_netdev(struct ice_vsi *vsi)
4683 {
4684 	struct ice_netdev_priv *np;
4685 	struct net_device *netdev;
4686 	u8 mac_addr[ETH_ALEN];
4687 
4688 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4689 				    vsi->alloc_rxq);
4690 	if (!netdev)
4691 		return -ENOMEM;
4692 
4693 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4694 	vsi->netdev = netdev;
4695 	np = netdev_priv(netdev);
4696 	np->vsi = vsi;
4697 
4698 	ice_set_netdev_features(netdev);
4699 	ice_set_ops(vsi);
4700 
4701 	if (vsi->type == ICE_VSI_PF) {
4702 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4703 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4704 		eth_hw_addr_set(netdev, mac_addr);
4705 	}
4706 
4707 	netdev->priv_flags |= IFF_UNICAST_FLT;
4708 
4709 	/* Setup netdev TC information */
4710 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4711 
4712 	netdev->max_mtu = ICE_MAX_MTU;
4713 
4714 	return 0;
4715 }
4716 
4717 static void ice_decfg_netdev(struct ice_vsi *vsi)
4718 {
4719 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4720 	free_netdev(vsi->netdev);
4721 	vsi->netdev = NULL;
4722 }
4723 
4724 /**
4725  * ice_wait_for_fw - wait for full FW readiness
4726  * @hw: pointer to the hardware structure
4727  * @timeout: milliseconds that can elapse before timing out
4728  */
4729 static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4730 {
4731 	int fw_loading;
4732 	u32 elapsed = 0;
4733 
4734 	while (elapsed <= timeout) {
4735 		fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4736 
4737 		/* firmware was not yet loaded, we have to wait more */
4738 		if (fw_loading) {
4739 			elapsed += 100;
4740 			msleep(100);
4741 			continue;
4742 		}
4743 		return 0;
4744 	}
4745 
4746 	return -ETIMEDOUT;
4747 }
4748 
4749 int ice_init_dev(struct ice_pf *pf)
4750 {
4751 	struct device *dev = ice_pf_to_dev(pf);
4752 	struct ice_hw *hw = &pf->hw;
4753 	int err;
4754 
4755 	err = ice_init_hw(hw);
4756 	if (err) {
4757 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4758 		return err;
4759 	}
4760 
4761 	/* Some cards require longer initialization times
4762 	 * due to necessity of loading FW from an external source.
4763 	 * This can take even half a minute.
4764 	 */
4765 	if (ice_is_pf_c827(hw)) {
4766 		err = ice_wait_for_fw(hw, 30000);
4767 		if (err) {
4768 			dev_err(dev, "ice_wait_for_fw timed out");
4769 			return err;
4770 		}
4771 	}
4772 
4773 	ice_init_feature_support(pf);
4774 
4775 	err = ice_init_ddp_config(hw, pf);
4776 	if (err)
4777 		return err;
4778 
4779 	/* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
4780 	 * set in pf->state, which will cause ice_is_safe_mode to return
4781 	 * true
4782 	 */
4783 	if (ice_is_safe_mode(pf)) {
4784 		/* we already got function/device capabilities but these don't
4785 		 * reflect what the driver needs to do in safe mode. Instead of
4786 		 * adding conditional logic everywhere to ignore these
4787 		 * device/function capabilities, override them.
4788 		 */
4789 		ice_set_safe_mode_caps(hw);
4790 	}
4791 
4792 	err = ice_init_pf(pf);
4793 	if (err) {
4794 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4795 		goto err_init_pf;
4796 	}
4797 
4798 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4799 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4800 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4801 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4802 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4803 		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4804 			pf->hw.tnl.valid_count[TNL_VXLAN];
4805 		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4806 			UDP_TUNNEL_TYPE_VXLAN;
4807 	}
4808 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4809 		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4810 			pf->hw.tnl.valid_count[TNL_GENEVE];
4811 		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4812 			UDP_TUNNEL_TYPE_GENEVE;
4813 	}
4814 
4815 	err = ice_init_interrupt_scheme(pf);
4816 	if (err) {
4817 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4818 		err = -EIO;
4819 		goto err_init_interrupt_scheme;
4820 	}
4821 
4822 	/* In case of MSIX we are going to setup the misc vector right here
4823 	 * to handle admin queue events etc. In case of legacy and MSI
4824 	 * the misc functionality and queue processing is combined in
4825 	 * the same vector and that gets setup at open.
4826 	 */
4827 	err = ice_req_irq_msix_misc(pf);
4828 	if (err) {
4829 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4830 		goto err_req_irq_msix_misc;
4831 	}
4832 
4833 	return 0;
4834 
4835 err_req_irq_msix_misc:
4836 	ice_clear_interrupt_scheme(pf);
4837 err_init_interrupt_scheme:
4838 	ice_deinit_pf(pf);
4839 err_init_pf:
4840 	ice_deinit_hw(hw);
4841 	return err;
4842 }
4843 
4844 void ice_deinit_dev(struct ice_pf *pf)
4845 {
4846 	ice_free_irq_msix_misc(pf);
4847 	ice_deinit_pf(pf);
4848 	ice_deinit_hw(&pf->hw);
4849 
4850 	/* Service task is already stopped, so call reset directly. */
4851 	ice_reset(&pf->hw, ICE_RESET_PFR);
4852 	pci_wait_for_pending_transaction(pf->pdev);
4853 	ice_clear_interrupt_scheme(pf);
4854 }
4855 
4856 static void ice_init_features(struct ice_pf *pf)
4857 {
4858 	struct device *dev = ice_pf_to_dev(pf);
4859 
4860 	if (ice_is_safe_mode(pf))
4861 		return;
4862 
4863 	/* initialize DDP driven features */
4864 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4865 		ice_ptp_init(pf);
4866 
4867 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4868 		ice_gnss_init(pf);
4869 
4870 	if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4871 	    ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4872 		ice_dpll_init(pf);
4873 
4874 	/* Note: Flow director init failure is non-fatal to load */
4875 	if (ice_init_fdir(pf))
4876 		dev_err(dev, "could not initialize flow director\n");
4877 
4878 	/* Note: DCB init failure is non-fatal to load */
4879 	if (ice_init_pf_dcb(pf, false)) {
4880 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4881 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4882 	} else {
4883 		ice_cfg_lldp_mib_change(&pf->hw, true);
4884 	}
4885 
4886 	if (ice_init_lag(pf))
4887 		dev_warn(dev, "Failed to init link aggregation support\n");
4888 
4889 	ice_hwmon_init(pf);
4890 }
4891 
4892 static void ice_deinit_features(struct ice_pf *pf)
4893 {
4894 	if (ice_is_safe_mode(pf))
4895 		return;
4896 
4897 	ice_deinit_lag(pf);
4898 	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4899 		ice_cfg_lldp_mib_change(&pf->hw, false);
4900 	ice_deinit_fdir(pf);
4901 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4902 		ice_gnss_exit(pf);
4903 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4904 		ice_ptp_release(pf);
4905 	if (test_bit(ICE_FLAG_DPLL, pf->flags))
4906 		ice_dpll_deinit(pf);
4907 	if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4908 		xa_destroy(&pf->eswitch.reprs);
4909 }
4910 
4911 static void ice_init_wakeup(struct ice_pf *pf)
4912 {
4913 	/* Save wakeup reason register for later use */
4914 	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4915 
4916 	/* check for a power management event */
4917 	ice_print_wake_reason(pf);
4918 
4919 	/* clear wake status, all bits */
4920 	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4921 
4922 	/* Disable WoL at init, wait for user to enable */
4923 	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4924 }
4925 
4926 static int ice_init_link(struct ice_pf *pf)
4927 {
4928 	struct device *dev = ice_pf_to_dev(pf);
4929 	int err;
4930 
4931 	err = ice_init_link_events(pf->hw.port_info);
4932 	if (err) {
4933 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4934 		return err;
4935 	}
4936 
4937 	/* not a fatal error if this fails */
4938 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4939 	if (err)
4940 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4941 
4942 	/* not a fatal error if this fails */
4943 	err = ice_update_link_info(pf->hw.port_info);
4944 	if (err)
4945 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4946 
4947 	ice_init_link_dflt_override(pf->hw.port_info);
4948 
4949 	ice_check_link_cfg_err(pf,
4950 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4951 
4952 	/* if media available, initialize PHY settings */
4953 	if (pf->hw.port_info->phy.link_info.link_info &
4954 	    ICE_AQ_MEDIA_AVAILABLE) {
4955 		/* not a fatal error if this fails */
4956 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4957 		if (err)
4958 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4959 
4960 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4961 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4962 
4963 			if (vsi)
4964 				ice_configure_phy(vsi);
4965 		}
4966 	} else {
4967 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4968 	}
4969 
4970 	return err;
4971 }
4972 
4973 static int ice_init_pf_sw(struct ice_pf *pf)
4974 {
4975 	bool dvm = ice_is_dvm_ena(&pf->hw);
4976 	struct ice_vsi *vsi;
4977 	int err;
4978 
4979 	/* create switch struct for the switch element created by FW on boot */
4980 	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4981 	if (!pf->first_sw)
4982 		return -ENOMEM;
4983 
4984 	if (pf->hw.evb_veb)
4985 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4986 	else
4987 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4988 
4989 	pf->first_sw->pf = pf;
4990 
4991 	/* record the sw_id available for later use */
4992 	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4993 
4994 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4995 	if (err)
4996 		goto err_aq_set_port_params;
4997 
4998 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4999 	if (!vsi) {
5000 		err = -ENOMEM;
5001 		goto err_pf_vsi_setup;
5002 	}
5003 
5004 	return 0;
5005 
5006 err_pf_vsi_setup:
5007 err_aq_set_port_params:
5008 	kfree(pf->first_sw);
5009 	return err;
5010 }
5011 
5012 static void ice_deinit_pf_sw(struct ice_pf *pf)
5013 {
5014 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
5015 
5016 	if (!vsi)
5017 		return;
5018 
5019 	ice_vsi_release(vsi);
5020 	kfree(pf->first_sw);
5021 }
5022 
5023 static int ice_alloc_vsis(struct ice_pf *pf)
5024 {
5025 	struct device *dev = ice_pf_to_dev(pf);
5026 
5027 	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
5028 	if (!pf->num_alloc_vsi)
5029 		return -EIO;
5030 
5031 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
5032 		dev_warn(dev,
5033 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
5034 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
5035 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
5036 	}
5037 
5038 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
5039 			       GFP_KERNEL);
5040 	if (!pf->vsi)
5041 		return -ENOMEM;
5042 
5043 	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
5044 				     sizeof(*pf->vsi_stats), GFP_KERNEL);
5045 	if (!pf->vsi_stats) {
5046 		devm_kfree(dev, pf->vsi);
5047 		return -ENOMEM;
5048 	}
5049 
5050 	return 0;
5051 }
5052 
5053 static void ice_dealloc_vsis(struct ice_pf *pf)
5054 {
5055 	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
5056 	pf->vsi_stats = NULL;
5057 
5058 	pf->num_alloc_vsi = 0;
5059 	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
5060 	pf->vsi = NULL;
5061 }
5062 
5063 static int ice_init_devlink(struct ice_pf *pf)
5064 {
5065 	int err;
5066 
5067 	err = ice_devlink_register_params(pf);
5068 	if (err)
5069 		return err;
5070 
5071 	ice_devlink_init_regions(pf);
5072 	ice_devlink_register(pf);
5073 
5074 	return 0;
5075 }
5076 
5077 static void ice_deinit_devlink(struct ice_pf *pf)
5078 {
5079 	ice_devlink_unregister(pf);
5080 	ice_devlink_destroy_regions(pf);
5081 	ice_devlink_unregister_params(pf);
5082 }
5083 
5084 static int ice_init(struct ice_pf *pf)
5085 {
5086 	int err;
5087 
5088 	err = ice_init_dev(pf);
5089 	if (err)
5090 		return err;
5091 
5092 	err = ice_alloc_vsis(pf);
5093 	if (err)
5094 		goto err_alloc_vsis;
5095 
5096 	err = ice_init_pf_sw(pf);
5097 	if (err)
5098 		goto err_init_pf_sw;
5099 
5100 	ice_init_wakeup(pf);
5101 
5102 	err = ice_init_link(pf);
5103 	if (err)
5104 		goto err_init_link;
5105 
5106 	err = ice_send_version(pf);
5107 	if (err)
5108 		goto err_init_link;
5109 
5110 	ice_verify_cacheline_size(pf);
5111 
5112 	if (ice_is_safe_mode(pf))
5113 		ice_set_safe_mode_vlan_cfg(pf);
5114 	else
5115 		/* print PCI link speed and width */
5116 		pcie_print_link_status(pf->pdev);
5117 
5118 	/* ready to go, so clear down state bit */
5119 	clear_bit(ICE_DOWN, pf->state);
5120 	clear_bit(ICE_SERVICE_DIS, pf->state);
5121 
5122 	/* since everything is good, start the service timer */
5123 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5124 
5125 	return 0;
5126 
5127 err_init_link:
5128 	ice_deinit_pf_sw(pf);
5129 err_init_pf_sw:
5130 	ice_dealloc_vsis(pf);
5131 err_alloc_vsis:
5132 	ice_deinit_dev(pf);
5133 	return err;
5134 }
5135 
5136 static void ice_deinit(struct ice_pf *pf)
5137 {
5138 	set_bit(ICE_SERVICE_DIS, pf->state);
5139 	set_bit(ICE_DOWN, pf->state);
5140 
5141 	ice_deinit_pf_sw(pf);
5142 	ice_dealloc_vsis(pf);
5143 	ice_deinit_dev(pf);
5144 }
5145 
5146 /**
5147  * ice_load - load pf by init hw and starting VSI
5148  * @pf: pointer to the pf instance
5149  *
5150  * This function has to be called under devl_lock.
5151  */
5152 int ice_load(struct ice_pf *pf)
5153 {
5154 	struct ice_vsi *vsi;
5155 	int err;
5156 
5157 	devl_assert_locked(priv_to_devlink(pf));
5158 
5159 	vsi = ice_get_main_vsi(pf);
5160 
5161 	/* init channel list */
5162 	INIT_LIST_HEAD(&vsi->ch_list);
5163 
5164 	err = ice_cfg_netdev(vsi);
5165 	if (err)
5166 		return err;
5167 
5168 	/* Setup DCB netlink interface */
5169 	ice_dcbnl_setup(vsi);
5170 
5171 	err = ice_init_mac_fltr(pf);
5172 	if (err)
5173 		goto err_init_mac_fltr;
5174 
5175 	err = ice_devlink_create_pf_port(pf);
5176 	if (err)
5177 		goto err_devlink_create_pf_port;
5178 
5179 	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
5180 
5181 	err = ice_register_netdev(vsi);
5182 	if (err)
5183 		goto err_register_netdev;
5184 
5185 	err = ice_tc_indir_block_register(vsi);
5186 	if (err)
5187 		goto err_tc_indir_block_register;
5188 
5189 	ice_napi_add(vsi);
5190 
5191 	err = ice_init_rdma(pf);
5192 	if (err)
5193 		goto err_init_rdma;
5194 
5195 	ice_init_features(pf);
5196 	ice_service_task_restart(pf);
5197 
5198 	clear_bit(ICE_DOWN, pf->state);
5199 
5200 	return 0;
5201 
5202 err_init_rdma:
5203 	ice_tc_indir_block_unregister(vsi);
5204 err_tc_indir_block_register:
5205 	ice_unregister_netdev(vsi);
5206 err_register_netdev:
5207 	ice_devlink_destroy_pf_port(pf);
5208 err_devlink_create_pf_port:
5209 err_init_mac_fltr:
5210 	ice_decfg_netdev(vsi);
5211 	return err;
5212 }
5213 
5214 /**
5215  * ice_unload - unload pf by stopping VSI and deinit hw
5216  * @pf: pointer to the pf instance
5217  *
5218  * This function has to be called under devl_lock.
5219  */
5220 void ice_unload(struct ice_pf *pf)
5221 {
5222 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
5223 
5224 	devl_assert_locked(priv_to_devlink(pf));
5225 
5226 	ice_deinit_features(pf);
5227 	ice_deinit_rdma(pf);
5228 	ice_tc_indir_block_unregister(vsi);
5229 	ice_unregister_netdev(vsi);
5230 	ice_devlink_destroy_pf_port(pf);
5231 	ice_decfg_netdev(vsi);
5232 }
5233 
5234 /**
5235  * ice_probe - Device initialization routine
5236  * @pdev: PCI device information struct
5237  * @ent: entry in ice_pci_tbl
5238  *
5239  * Returns 0 on success, negative on failure
5240  */
5241 static int
5242 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5243 {
5244 	struct device *dev = &pdev->dev;
5245 	struct ice_adapter *adapter;
5246 	struct ice_pf *pf;
5247 	struct ice_hw *hw;
5248 	int err;
5249 
5250 	if (pdev->is_virtfn) {
5251 		dev_err(dev, "can't probe a virtual function\n");
5252 		return -EINVAL;
5253 	}
5254 
5255 	/* when under a kdump kernel initiate a reset before enabling the
5256 	 * device in order to clear out any pending DMA transactions. These
5257 	 * transactions can cause some systems to machine check when doing
5258 	 * the pcim_enable_device() below.
5259 	 */
5260 	if (is_kdump_kernel()) {
5261 		pci_save_state(pdev);
5262 		pci_clear_master(pdev);
5263 		err = pcie_flr(pdev);
5264 		if (err)
5265 			return err;
5266 		pci_restore_state(pdev);
5267 	}
5268 
5269 	/* this driver uses devres, see
5270 	 * Documentation/driver-api/driver-model/devres.rst
5271 	 */
5272 	err = pcim_enable_device(pdev);
5273 	if (err)
5274 		return err;
5275 
5276 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5277 	if (err) {
5278 		dev_err(dev, "BAR0 I/O map error %d\n", err);
5279 		return err;
5280 	}
5281 
5282 	pf = ice_allocate_pf(dev);
5283 	if (!pf)
5284 		return -ENOMEM;
5285 
5286 	/* initialize Auxiliary index to invalid value */
5287 	pf->aux_idx = -1;
5288 
5289 	/* set up for high or low DMA */
5290 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5291 	if (err) {
5292 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5293 		return err;
5294 	}
5295 
5296 	pci_set_master(pdev);
5297 
5298 	adapter = ice_adapter_get(pdev);
5299 	if (IS_ERR(adapter))
5300 		return PTR_ERR(adapter);
5301 
5302 	pf->pdev = pdev;
5303 	pf->adapter = adapter;
5304 	pci_set_drvdata(pdev, pf);
5305 	set_bit(ICE_DOWN, pf->state);
5306 	/* Disable service task until DOWN bit is cleared */
5307 	set_bit(ICE_SERVICE_DIS, pf->state);
5308 
5309 	hw = &pf->hw;
5310 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5311 	pci_save_state(pdev);
5312 
5313 	hw->back = pf;
5314 	hw->port_info = NULL;
5315 	hw->vendor_id = pdev->vendor;
5316 	hw->device_id = pdev->device;
5317 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5318 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5319 	hw->subsystem_device_id = pdev->subsystem_device;
5320 	hw->bus.device = PCI_SLOT(pdev->devfn);
5321 	hw->bus.func = PCI_FUNC(pdev->devfn);
5322 	ice_set_ctrlq_len(hw);
5323 
5324 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5325 
5326 #ifndef CONFIG_DYNAMIC_DEBUG
5327 	if (debug < -1)
5328 		hw->debug_mask = debug;
5329 #endif
5330 
5331 	err = ice_init(pf);
5332 	if (err)
5333 		goto err_init;
5334 
5335 	devl_lock(priv_to_devlink(pf));
5336 	err = ice_load(pf);
5337 	if (err)
5338 		goto err_load;
5339 
5340 	err = ice_init_devlink(pf);
5341 	if (err)
5342 		goto err_init_devlink;
5343 	devl_unlock(priv_to_devlink(pf));
5344 
5345 	return 0;
5346 
5347 err_init_devlink:
5348 	ice_unload(pf);
5349 err_load:
5350 	devl_unlock(priv_to_devlink(pf));
5351 	ice_deinit(pf);
5352 err_init:
5353 	ice_adapter_put(pdev);
5354 	pci_disable_device(pdev);
5355 	return err;
5356 }
5357 
5358 /**
5359  * ice_set_wake - enable or disable Wake on LAN
5360  * @pf: pointer to the PF struct
5361  *
5362  * Simple helper for WoL control
5363  */
5364 static void ice_set_wake(struct ice_pf *pf)
5365 {
5366 	struct ice_hw *hw = &pf->hw;
5367 	bool wol = pf->wol_ena;
5368 
5369 	/* clear wake state, otherwise new wake events won't fire */
5370 	wr32(hw, PFPM_WUS, U32_MAX);
5371 
5372 	/* enable / disable APM wake up, no RMW needed */
5373 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5374 
5375 	/* set magic packet filter enabled */
5376 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5377 }
5378 
5379 /**
5380  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5381  * @pf: pointer to the PF struct
5382  *
5383  * Issue firmware command to enable multicast magic wake, making
5384  * sure that any locally administered address (LAA) is used for
5385  * wake, and that PF reset doesn't undo the LAA.
5386  */
5387 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5388 {
5389 	struct device *dev = ice_pf_to_dev(pf);
5390 	struct ice_hw *hw = &pf->hw;
5391 	u8 mac_addr[ETH_ALEN];
5392 	struct ice_vsi *vsi;
5393 	int status;
5394 	u8 flags;
5395 
5396 	if (!pf->wol_ena)
5397 		return;
5398 
5399 	vsi = ice_get_main_vsi(pf);
5400 	if (!vsi)
5401 		return;
5402 
5403 	/* Get current MAC address in case it's an LAA */
5404 	if (vsi->netdev)
5405 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5406 	else
5407 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5408 
5409 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5410 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5411 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5412 
5413 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5414 	if (status)
5415 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5416 			status, ice_aq_str(hw->adminq.sq_last_status));
5417 }
5418 
5419 /**
5420  * ice_remove - Device removal routine
5421  * @pdev: PCI device information struct
5422  */
5423 static void ice_remove(struct pci_dev *pdev)
5424 {
5425 	struct ice_pf *pf = pci_get_drvdata(pdev);
5426 	int i;
5427 
5428 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5429 		if (!ice_is_reset_in_progress(pf->state))
5430 			break;
5431 		msleep(100);
5432 	}
5433 
5434 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5435 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5436 		ice_free_vfs(pf);
5437 	}
5438 
5439 	ice_hwmon_exit(pf);
5440 
5441 	ice_service_task_stop(pf);
5442 	ice_aq_cancel_waiting_tasks(pf);
5443 	set_bit(ICE_DOWN, pf->state);
5444 
5445 	if (!ice_is_safe_mode(pf))
5446 		ice_remove_arfs(pf);
5447 
5448 	devl_lock(priv_to_devlink(pf));
5449 	ice_dealloc_all_dynamic_ports(pf);
5450 	ice_deinit_devlink(pf);
5451 
5452 	ice_unload(pf);
5453 	devl_unlock(priv_to_devlink(pf));
5454 
5455 	ice_deinit(pf);
5456 	ice_vsi_release_all(pf);
5457 
5458 	ice_setup_mc_magic_wake(pf);
5459 	ice_set_wake(pf);
5460 
5461 	ice_adapter_put(pdev);
5462 	pci_disable_device(pdev);
5463 }
5464 
5465 /**
5466  * ice_shutdown - PCI callback for shutting down device
5467  * @pdev: PCI device information struct
5468  */
5469 static void ice_shutdown(struct pci_dev *pdev)
5470 {
5471 	struct ice_pf *pf = pci_get_drvdata(pdev);
5472 
5473 	ice_remove(pdev);
5474 
5475 	if (system_state == SYSTEM_POWER_OFF) {
5476 		pci_wake_from_d3(pdev, pf->wol_ena);
5477 		pci_set_power_state(pdev, PCI_D3hot);
5478 	}
5479 }
5480 
5481 /**
5482  * ice_prepare_for_shutdown - prep for PCI shutdown
5483  * @pf: board private structure
5484  *
5485  * Inform or close all dependent features in prep for PCI device shutdown
5486  */
5487 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5488 {
5489 	struct ice_hw *hw = &pf->hw;
5490 	u32 v;
5491 
5492 	/* Notify VFs of impending reset */
5493 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5494 		ice_vc_notify_reset(pf);
5495 
5496 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5497 
5498 	/* disable the VSIs and their queues that are not already DOWN */
5499 	ice_pf_dis_all_vsi(pf, false);
5500 
5501 	ice_for_each_vsi(pf, v)
5502 		if (pf->vsi[v])
5503 			pf->vsi[v]->vsi_num = 0;
5504 
5505 	ice_shutdown_all_ctrlq(hw, true);
5506 }
5507 
5508 /**
5509  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5510  * @pf: board private structure to reinitialize
5511  *
5512  * This routine reinitialize interrupt scheme that was cleared during
5513  * power management suspend callback.
5514  *
5515  * This should be called during resume routine to re-allocate the q_vectors
5516  * and reacquire interrupts.
5517  */
5518 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5519 {
5520 	struct device *dev = ice_pf_to_dev(pf);
5521 	int ret, v;
5522 
5523 	/* Since we clear MSIX flag during suspend, we need to
5524 	 * set it back during resume...
5525 	 */
5526 
5527 	ret = ice_init_interrupt_scheme(pf);
5528 	if (ret) {
5529 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5530 		return ret;
5531 	}
5532 
5533 	/* Remap vectors and rings, after successful re-init interrupts */
5534 	ice_for_each_vsi(pf, v) {
5535 		if (!pf->vsi[v])
5536 			continue;
5537 
5538 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5539 		if (ret)
5540 			goto err_reinit;
5541 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5542 		rtnl_lock();
5543 		ice_vsi_set_napi_queues(pf->vsi[v]);
5544 		rtnl_unlock();
5545 	}
5546 
5547 	ret = ice_req_irq_msix_misc(pf);
5548 	if (ret) {
5549 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5550 			ret);
5551 		goto err_reinit;
5552 	}
5553 
5554 	return 0;
5555 
5556 err_reinit:
5557 	while (v--)
5558 		if (pf->vsi[v]) {
5559 			rtnl_lock();
5560 			ice_vsi_clear_napi_queues(pf->vsi[v]);
5561 			rtnl_unlock();
5562 			ice_vsi_free_q_vectors(pf->vsi[v]);
5563 		}
5564 
5565 	return ret;
5566 }
5567 
5568 /**
5569  * ice_suspend
5570  * @dev: generic device information structure
5571  *
5572  * Power Management callback to quiesce the device and prepare
5573  * for D3 transition.
5574  */
5575 static int ice_suspend(struct device *dev)
5576 {
5577 	struct pci_dev *pdev = to_pci_dev(dev);
5578 	struct ice_pf *pf;
5579 	int disabled, v;
5580 
5581 	pf = pci_get_drvdata(pdev);
5582 
5583 	if (!ice_pf_state_is_nominal(pf)) {
5584 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5585 		return -EBUSY;
5586 	}
5587 
5588 	/* Stop watchdog tasks until resume completion.
5589 	 * Even though it is most likely that the service task is
5590 	 * disabled if the device is suspended or down, the service task's
5591 	 * state is controlled by a different state bit, and we should
5592 	 * store and honor whatever state that bit is in at this point.
5593 	 */
5594 	disabled = ice_service_task_stop(pf);
5595 
5596 	ice_deinit_rdma(pf);
5597 
5598 	/* Already suspended?, then there is nothing to do */
5599 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5600 		if (!disabled)
5601 			ice_service_task_restart(pf);
5602 		return 0;
5603 	}
5604 
5605 	if (test_bit(ICE_DOWN, pf->state) ||
5606 	    ice_is_reset_in_progress(pf->state)) {
5607 		dev_err(dev, "can't suspend device in reset or already down\n");
5608 		if (!disabled)
5609 			ice_service_task_restart(pf);
5610 		return 0;
5611 	}
5612 
5613 	ice_setup_mc_magic_wake(pf);
5614 
5615 	ice_prepare_for_shutdown(pf);
5616 
5617 	ice_set_wake(pf);
5618 
5619 	/* Free vectors, clear the interrupt scheme and release IRQs
5620 	 * for proper hibernation, especially with large number of CPUs.
5621 	 * Otherwise hibernation might fail when mapping all the vectors back
5622 	 * to CPU0.
5623 	 */
5624 	ice_free_irq_msix_misc(pf);
5625 	ice_for_each_vsi(pf, v) {
5626 		if (!pf->vsi[v])
5627 			continue;
5628 		rtnl_lock();
5629 		ice_vsi_clear_napi_queues(pf->vsi[v]);
5630 		rtnl_unlock();
5631 		ice_vsi_free_q_vectors(pf->vsi[v]);
5632 	}
5633 	ice_clear_interrupt_scheme(pf);
5634 
5635 	pci_save_state(pdev);
5636 	pci_wake_from_d3(pdev, pf->wol_ena);
5637 	pci_set_power_state(pdev, PCI_D3hot);
5638 	return 0;
5639 }
5640 
5641 /**
5642  * ice_resume - PM callback for waking up from D3
5643  * @dev: generic device information structure
5644  */
5645 static int ice_resume(struct device *dev)
5646 {
5647 	struct pci_dev *pdev = to_pci_dev(dev);
5648 	enum ice_reset_req reset_type;
5649 	struct ice_pf *pf;
5650 	struct ice_hw *hw;
5651 	int ret;
5652 
5653 	pci_set_power_state(pdev, PCI_D0);
5654 	pci_restore_state(pdev);
5655 	pci_save_state(pdev);
5656 
5657 	if (!pci_device_is_present(pdev))
5658 		return -ENODEV;
5659 
5660 	ret = pci_enable_device_mem(pdev);
5661 	if (ret) {
5662 		dev_err(dev, "Cannot enable device after suspend\n");
5663 		return ret;
5664 	}
5665 
5666 	pf = pci_get_drvdata(pdev);
5667 	hw = &pf->hw;
5668 
5669 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5670 	ice_print_wake_reason(pf);
5671 
5672 	/* We cleared the interrupt scheme when we suspended, so we need to
5673 	 * restore it now to resume device functionality.
5674 	 */
5675 	ret = ice_reinit_interrupt_scheme(pf);
5676 	if (ret)
5677 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5678 
5679 	ret = ice_init_rdma(pf);
5680 	if (ret)
5681 		dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5682 			ret);
5683 
5684 	clear_bit(ICE_DOWN, pf->state);
5685 	/* Now perform PF reset and rebuild */
5686 	reset_type = ICE_RESET_PFR;
5687 	/* re-enable service task for reset, but allow reset to schedule it */
5688 	clear_bit(ICE_SERVICE_DIS, pf->state);
5689 
5690 	if (ice_schedule_reset(pf, reset_type))
5691 		dev_err(dev, "Reset during resume failed.\n");
5692 
5693 	clear_bit(ICE_SUSPENDED, pf->state);
5694 	ice_service_task_restart(pf);
5695 
5696 	/* Restart the service task */
5697 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5698 
5699 	return 0;
5700 }
5701 
5702 /**
5703  * ice_pci_err_detected - warning that PCI error has been detected
5704  * @pdev: PCI device information struct
5705  * @err: the type of PCI error
5706  *
5707  * Called to warn that something happened on the PCI bus and the error handling
5708  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5709  */
5710 static pci_ers_result_t
5711 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5712 {
5713 	struct ice_pf *pf = pci_get_drvdata(pdev);
5714 
5715 	if (!pf) {
5716 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5717 			__func__, err);
5718 		return PCI_ERS_RESULT_DISCONNECT;
5719 	}
5720 
5721 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5722 		ice_service_task_stop(pf);
5723 
5724 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5725 			set_bit(ICE_PFR_REQ, pf->state);
5726 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5727 		}
5728 	}
5729 
5730 	return PCI_ERS_RESULT_NEED_RESET;
5731 }
5732 
5733 /**
5734  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5735  * @pdev: PCI device information struct
5736  *
5737  * Called to determine if the driver can recover from the PCI slot reset by
5738  * using a register read to determine if the device is recoverable.
5739  */
5740 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5741 {
5742 	struct ice_pf *pf = pci_get_drvdata(pdev);
5743 	pci_ers_result_t result;
5744 	int err;
5745 	u32 reg;
5746 
5747 	err = pci_enable_device_mem(pdev);
5748 	if (err) {
5749 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5750 			err);
5751 		result = PCI_ERS_RESULT_DISCONNECT;
5752 	} else {
5753 		pci_set_master(pdev);
5754 		pci_restore_state(pdev);
5755 		pci_save_state(pdev);
5756 		pci_wake_from_d3(pdev, false);
5757 
5758 		/* Check for life */
5759 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5760 		if (!reg)
5761 			result = PCI_ERS_RESULT_RECOVERED;
5762 		else
5763 			result = PCI_ERS_RESULT_DISCONNECT;
5764 	}
5765 
5766 	return result;
5767 }
5768 
5769 /**
5770  * ice_pci_err_resume - restart operations after PCI error recovery
5771  * @pdev: PCI device information struct
5772  *
5773  * Called to allow the driver to bring things back up after PCI error and/or
5774  * reset recovery have finished
5775  */
5776 static void ice_pci_err_resume(struct pci_dev *pdev)
5777 {
5778 	struct ice_pf *pf = pci_get_drvdata(pdev);
5779 
5780 	if (!pf) {
5781 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5782 			__func__);
5783 		return;
5784 	}
5785 
5786 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5787 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5788 			__func__);
5789 		return;
5790 	}
5791 
5792 	ice_restore_all_vfs_msi_state(pf);
5793 
5794 	ice_do_reset(pf, ICE_RESET_PFR);
5795 	ice_service_task_restart(pf);
5796 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5797 }
5798 
5799 /**
5800  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5801  * @pdev: PCI device information struct
5802  */
5803 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5804 {
5805 	struct ice_pf *pf = pci_get_drvdata(pdev);
5806 
5807 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5808 		ice_service_task_stop(pf);
5809 
5810 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5811 			set_bit(ICE_PFR_REQ, pf->state);
5812 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5813 		}
5814 	}
5815 }
5816 
5817 /**
5818  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5819  * @pdev: PCI device information struct
5820  */
5821 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5822 {
5823 	ice_pci_err_resume(pdev);
5824 }
5825 
5826 /* ice_pci_tbl - PCI Device ID Table
5827  *
5828  * Wildcard entries (PCI_ANY_ID) should come last
5829  * Last entry must be all 0s
5830  *
5831  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5832  *   Class, Class Mask, private data (not used) }
5833  */
5834 static const struct pci_device_id ice_pci_tbl[] = {
5835 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5836 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5837 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5838 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5839 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5840 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5841 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5842 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5843 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5844 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5845 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5846 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5847 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5848 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5849 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5850 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5851 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5852 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5853 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5854 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5855 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5856 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5857 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5858 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5859 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5860 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
5861 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
5862 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
5863 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
5864 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
5865 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
5866 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
5867 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
5868 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
5869 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
5870 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
5871 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
5872 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
5873 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
5874 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
5875 	/* required last entry */
5876 	{}
5877 };
5878 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5879 
5880 static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5881 
5882 static const struct pci_error_handlers ice_pci_err_handler = {
5883 	.error_detected = ice_pci_err_detected,
5884 	.slot_reset = ice_pci_err_slot_reset,
5885 	.reset_prepare = ice_pci_err_reset_prepare,
5886 	.reset_done = ice_pci_err_reset_done,
5887 	.resume = ice_pci_err_resume
5888 };
5889 
5890 static struct pci_driver ice_driver = {
5891 	.name = KBUILD_MODNAME,
5892 	.id_table = ice_pci_tbl,
5893 	.probe = ice_probe,
5894 	.remove = ice_remove,
5895 	.driver.pm = pm_sleep_ptr(&ice_pm_ops),
5896 	.shutdown = ice_shutdown,
5897 	.sriov_configure = ice_sriov_configure,
5898 	.sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5899 	.sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
5900 	.err_handler = &ice_pci_err_handler
5901 };
5902 
5903 /**
5904  * ice_module_init - Driver registration routine
5905  *
5906  * ice_module_init is the first routine called when the driver is
5907  * loaded. All it does is register with the PCI subsystem.
5908  */
5909 static int __init ice_module_init(void)
5910 {
5911 	int status = -ENOMEM;
5912 
5913 	pr_info("%s\n", ice_driver_string);
5914 	pr_info("%s\n", ice_copyright);
5915 
5916 	ice_adv_lnk_speed_maps_init();
5917 
5918 	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5919 	if (!ice_wq) {
5920 		pr_err("Failed to create workqueue\n");
5921 		return status;
5922 	}
5923 
5924 	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5925 	if (!ice_lag_wq) {
5926 		pr_err("Failed to create LAG workqueue\n");
5927 		goto err_dest_wq;
5928 	}
5929 
5930 	ice_debugfs_init();
5931 
5932 	status = pci_register_driver(&ice_driver);
5933 	if (status) {
5934 		pr_err("failed to register PCI driver, err %d\n", status);
5935 		goto err_dest_lag_wq;
5936 	}
5937 
5938 	status = ice_sf_driver_register();
5939 	if (status) {
5940 		pr_err("Failed to register SF driver, err %d\n", status);
5941 		goto err_sf_driver;
5942 	}
5943 
5944 	return 0;
5945 
5946 err_sf_driver:
5947 	pci_unregister_driver(&ice_driver);
5948 err_dest_lag_wq:
5949 	destroy_workqueue(ice_lag_wq);
5950 	ice_debugfs_exit();
5951 err_dest_wq:
5952 	destroy_workqueue(ice_wq);
5953 	return status;
5954 }
5955 module_init(ice_module_init);
5956 
5957 /**
5958  * ice_module_exit - Driver exit cleanup routine
5959  *
5960  * ice_module_exit is called just before the driver is removed
5961  * from memory.
5962  */
5963 static void __exit ice_module_exit(void)
5964 {
5965 	ice_sf_driver_unregister();
5966 	pci_unregister_driver(&ice_driver);
5967 	ice_debugfs_exit();
5968 	destroy_workqueue(ice_wq);
5969 	destroy_workqueue(ice_lag_wq);
5970 	pr_info("module unloaded\n");
5971 }
5972 module_exit(ice_module_exit);
5973 
5974 /**
5975  * ice_set_mac_address - NDO callback to set MAC address
5976  * @netdev: network interface device structure
5977  * @pi: pointer to an address structure
5978  *
5979  * Returns 0 on success, negative on failure
5980  */
5981 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5982 {
5983 	struct ice_netdev_priv *np = netdev_priv(netdev);
5984 	struct ice_vsi *vsi = np->vsi;
5985 	struct ice_pf *pf = vsi->back;
5986 	struct ice_hw *hw = &pf->hw;
5987 	struct sockaddr *addr = pi;
5988 	u8 old_mac[ETH_ALEN];
5989 	u8 flags = 0;
5990 	u8 *mac;
5991 	int err;
5992 
5993 	mac = (u8 *)addr->sa_data;
5994 
5995 	if (!is_valid_ether_addr(mac))
5996 		return -EADDRNOTAVAIL;
5997 
5998 	if (test_bit(ICE_DOWN, pf->state) ||
5999 	    ice_is_reset_in_progress(pf->state)) {
6000 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
6001 			   mac);
6002 		return -EBUSY;
6003 	}
6004 
6005 	if (ice_chnl_dmac_fltr_cnt(pf)) {
6006 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
6007 			   mac);
6008 		return -EAGAIN;
6009 	}
6010 
6011 	netif_addr_lock_bh(netdev);
6012 	ether_addr_copy(old_mac, netdev->dev_addr);
6013 	/* change the netdev's MAC address */
6014 	eth_hw_addr_set(netdev, mac);
6015 	netif_addr_unlock_bh(netdev);
6016 
6017 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
6018 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
6019 	if (err && err != -ENOENT) {
6020 		err = -EADDRNOTAVAIL;
6021 		goto err_update_filters;
6022 	}
6023 
6024 	/* Add filter for new MAC. If filter exists, return success */
6025 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
6026 	if (err == -EEXIST) {
6027 		/* Although this MAC filter is already present in hardware it's
6028 		 * possible in some cases (e.g. bonding) that dev_addr was
6029 		 * modified outside of the driver and needs to be restored back
6030 		 * to this value.
6031 		 */
6032 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
6033 
6034 		return 0;
6035 	} else if (err) {
6036 		/* error if the new filter addition failed */
6037 		err = -EADDRNOTAVAIL;
6038 	}
6039 
6040 err_update_filters:
6041 	if (err) {
6042 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
6043 			   mac);
6044 		netif_addr_lock_bh(netdev);
6045 		eth_hw_addr_set(netdev, old_mac);
6046 		netif_addr_unlock_bh(netdev);
6047 		return err;
6048 	}
6049 
6050 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
6051 		   netdev->dev_addr);
6052 
6053 	/* write new MAC address to the firmware */
6054 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
6055 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
6056 	if (err) {
6057 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
6058 			   mac, err);
6059 	}
6060 	return 0;
6061 }
6062 
6063 /**
6064  * ice_set_rx_mode - NDO callback to set the netdev filters
6065  * @netdev: network interface device structure
6066  */
6067 static void ice_set_rx_mode(struct net_device *netdev)
6068 {
6069 	struct ice_netdev_priv *np = netdev_priv(netdev);
6070 	struct ice_vsi *vsi = np->vsi;
6071 
6072 	if (!vsi || ice_is_switchdev_running(vsi->back))
6073 		return;
6074 
6075 	/* Set the flags to synchronize filters
6076 	 * ndo_set_rx_mode may be triggered even without a change in netdev
6077 	 * flags
6078 	 */
6079 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
6080 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
6081 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
6082 
6083 	/* schedule our worker thread which will take care of
6084 	 * applying the new filter changes
6085 	 */
6086 	ice_service_task_schedule(vsi->back);
6087 }
6088 
6089 /**
6090  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
6091  * @netdev: network interface device structure
6092  * @queue_index: Queue ID
6093  * @maxrate: maximum bandwidth in Mbps
6094  */
6095 static int
6096 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
6097 {
6098 	struct ice_netdev_priv *np = netdev_priv(netdev);
6099 	struct ice_vsi *vsi = np->vsi;
6100 	u16 q_handle;
6101 	int status;
6102 	u8 tc;
6103 
6104 	/* Validate maxrate requested is within permitted range */
6105 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
6106 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
6107 			   maxrate, queue_index);
6108 		return -EINVAL;
6109 	}
6110 
6111 	q_handle = vsi->tx_rings[queue_index]->q_handle;
6112 	tc = ice_dcb_get_tc(vsi, queue_index);
6113 
6114 	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
6115 	if (!vsi) {
6116 		netdev_err(netdev, "Invalid VSI for given queue %d\n",
6117 			   queue_index);
6118 		return -EINVAL;
6119 	}
6120 
6121 	/* Set BW back to default, when user set maxrate to 0 */
6122 	if (!maxrate)
6123 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6124 					       q_handle, ICE_MAX_BW);
6125 	else
6126 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6127 					  q_handle, ICE_MAX_BW, maxrate * 1000);
6128 	if (status)
6129 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6130 			   status);
6131 
6132 	return status;
6133 }
6134 
6135 /**
6136  * ice_fdb_add - add an entry to the hardware database
6137  * @ndm: the input from the stack
6138  * @tb: pointer to array of nladdr (unused)
6139  * @dev: the net device pointer
6140  * @addr: the MAC address entry being added
6141  * @vid: VLAN ID
6142  * @flags: instructions from stack about fdb operation
6143  * @extack: netlink extended ack
6144  */
6145 static int
6146 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6147 	    struct net_device *dev, const unsigned char *addr, u16 vid,
6148 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
6149 {
6150 	int err;
6151 
6152 	if (vid) {
6153 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6154 		return -EINVAL;
6155 	}
6156 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6157 		netdev_err(dev, "FDB only supports static addresses\n");
6158 		return -EINVAL;
6159 	}
6160 
6161 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6162 		err = dev_uc_add_excl(dev, addr);
6163 	else if (is_multicast_ether_addr(addr))
6164 		err = dev_mc_add_excl(dev, addr);
6165 	else
6166 		err = -EINVAL;
6167 
6168 	/* Only return duplicate errors if NLM_F_EXCL is set */
6169 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
6170 		err = 0;
6171 
6172 	return err;
6173 }
6174 
6175 /**
6176  * ice_fdb_del - delete an entry from the hardware database
6177  * @ndm: the input from the stack
6178  * @tb: pointer to array of nladdr (unused)
6179  * @dev: the net device pointer
6180  * @addr: the MAC address entry being added
6181  * @vid: VLAN ID
6182  * @extack: netlink extended ack
6183  */
6184 static int
6185 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6186 	    struct net_device *dev, const unsigned char *addr,
6187 	    __always_unused u16 vid, struct netlink_ext_ack *extack)
6188 {
6189 	int err;
6190 
6191 	if (ndm->ndm_state & NUD_PERMANENT) {
6192 		netdev_err(dev, "FDB only supports static addresses\n");
6193 		return -EINVAL;
6194 	}
6195 
6196 	if (is_unicast_ether_addr(addr))
6197 		err = dev_uc_del(dev, addr);
6198 	else if (is_multicast_ether_addr(addr))
6199 		err = dev_mc_del(dev, addr);
6200 	else
6201 		err = -EINVAL;
6202 
6203 	return err;
6204 }
6205 
6206 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6207 					 NETIF_F_HW_VLAN_CTAG_TX | \
6208 					 NETIF_F_HW_VLAN_STAG_RX | \
6209 					 NETIF_F_HW_VLAN_STAG_TX)
6210 
6211 #define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6212 					 NETIF_F_HW_VLAN_STAG_RX)
6213 
6214 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
6215 					 NETIF_F_HW_VLAN_STAG_FILTER)
6216 
6217 /**
6218  * ice_fix_features - fix the netdev features flags based on device limitations
6219  * @netdev: ptr to the netdev that flags are being fixed on
6220  * @features: features that need to be checked and possibly fixed
6221  *
6222  * Make sure any fixups are made to features in this callback. This enables the
6223  * driver to not have to check unsupported configurations throughout the driver
6224  * because that's the responsiblity of this callback.
6225  *
6226  * Single VLAN Mode (SVM) Supported Features:
6227  *	NETIF_F_HW_VLAN_CTAG_FILTER
6228  *	NETIF_F_HW_VLAN_CTAG_RX
6229  *	NETIF_F_HW_VLAN_CTAG_TX
6230  *
6231  * Double VLAN Mode (DVM) Supported Features:
6232  *	NETIF_F_HW_VLAN_CTAG_FILTER
6233  *	NETIF_F_HW_VLAN_CTAG_RX
6234  *	NETIF_F_HW_VLAN_CTAG_TX
6235  *
6236  *	NETIF_F_HW_VLAN_STAG_FILTER
6237  *	NETIF_HW_VLAN_STAG_RX
6238  *	NETIF_HW_VLAN_STAG_TX
6239  *
6240  * Features that need fixing:
6241  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6242  *	These are mutually exlusive as the VSI context cannot support multiple
6243  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
6244  *	is not done, then default to clearing the requested STAG offload
6245  *	settings.
6246  *
6247  *	All supported filtering has to be enabled or disabled together. For
6248  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6249  *	together. If this is not done, then default to VLAN filtering disabled.
6250  *	These are mutually exclusive as there is currently no way to
6251  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6252  *	prune rules.
6253  */
6254 static netdev_features_t
6255 ice_fix_features(struct net_device *netdev, netdev_features_t features)
6256 {
6257 	struct ice_netdev_priv *np = netdev_priv(netdev);
6258 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6259 	bool cur_ctag, cur_stag, req_ctag, req_stag;
6260 
6261 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6262 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6263 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6264 
6265 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6266 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6267 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6268 
6269 	if (req_vlan_fltr != cur_vlan_fltr) {
6270 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6271 			if (req_ctag && req_stag) {
6272 				features |= NETIF_VLAN_FILTERING_FEATURES;
6273 			} else if (!req_ctag && !req_stag) {
6274 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6275 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
6276 				   (!cur_stag && req_stag && !cur_ctag)) {
6277 				features |= NETIF_VLAN_FILTERING_FEATURES;
6278 				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");
6279 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
6280 				   (cur_stag && !req_stag && cur_ctag)) {
6281 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6282 				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");
6283 			}
6284 		} else {
6285 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6286 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6287 
6288 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6289 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6290 		}
6291 	}
6292 
6293 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6294 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6295 		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");
6296 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6297 			      NETIF_F_HW_VLAN_STAG_TX);
6298 	}
6299 
6300 	if (!(netdev->features & NETIF_F_RXFCS) &&
6301 	    (features & NETIF_F_RXFCS) &&
6302 	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6303 	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6304 		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6305 		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6306 	}
6307 
6308 	return features;
6309 }
6310 
6311 /**
6312  * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6313  * @vsi: PF's VSI
6314  * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6315  *
6316  * Store current stripped VLAN proto in ring packet context,
6317  * so it can be accessed more efficiently by packet processing code.
6318  */
6319 static void
6320 ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6321 {
6322 	u16 i;
6323 
6324 	ice_for_each_alloc_rxq(vsi, i)
6325 		vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6326 }
6327 
6328 /**
6329  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6330  * @vsi: PF's VSI
6331  * @features: features used to determine VLAN offload settings
6332  *
6333  * First, determine the vlan_ethertype based on the VLAN offload bits in
6334  * features. Then determine if stripping and insertion should be enabled or
6335  * disabled. Finally enable or disable VLAN stripping and insertion.
6336  */
6337 static int
6338 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6339 {
6340 	bool enable_stripping = true, enable_insertion = true;
6341 	struct ice_vsi_vlan_ops *vlan_ops;
6342 	int strip_err = 0, insert_err = 0;
6343 	u16 vlan_ethertype = 0;
6344 
6345 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6346 
6347 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6348 		vlan_ethertype = ETH_P_8021AD;
6349 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6350 		vlan_ethertype = ETH_P_8021Q;
6351 
6352 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6353 		enable_stripping = false;
6354 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6355 		enable_insertion = false;
6356 
6357 	if (enable_stripping)
6358 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6359 	else
6360 		strip_err = vlan_ops->dis_stripping(vsi);
6361 
6362 	if (enable_insertion)
6363 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6364 	else
6365 		insert_err = vlan_ops->dis_insertion(vsi);
6366 
6367 	if (strip_err || insert_err)
6368 		return -EIO;
6369 
6370 	ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6371 				    htons(vlan_ethertype) : 0);
6372 
6373 	return 0;
6374 }
6375 
6376 /**
6377  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6378  * @vsi: PF's VSI
6379  * @features: features used to determine VLAN filtering settings
6380  *
6381  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6382  * features.
6383  */
6384 static int
6385 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6386 {
6387 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6388 	int err = 0;
6389 
6390 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6391 	 * if either bit is set
6392 	 */
6393 	if (features &
6394 	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6395 		err = vlan_ops->ena_rx_filtering(vsi);
6396 	else
6397 		err = vlan_ops->dis_rx_filtering(vsi);
6398 
6399 	return err;
6400 }
6401 
6402 /**
6403  * ice_set_vlan_features - set VLAN settings based on suggested feature set
6404  * @netdev: ptr to the netdev being adjusted
6405  * @features: the feature set that the stack is suggesting
6406  *
6407  * Only update VLAN settings if the requested_vlan_features are different than
6408  * the current_vlan_features.
6409  */
6410 static int
6411 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6412 {
6413 	netdev_features_t current_vlan_features, requested_vlan_features;
6414 	struct ice_netdev_priv *np = netdev_priv(netdev);
6415 	struct ice_vsi *vsi = np->vsi;
6416 	int err;
6417 
6418 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6419 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6420 	if (current_vlan_features ^ requested_vlan_features) {
6421 		if ((features & NETIF_F_RXFCS) &&
6422 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6423 			dev_err(ice_pf_to_dev(vsi->back),
6424 				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6425 			return -EIO;
6426 		}
6427 
6428 		err = ice_set_vlan_offload_features(vsi, features);
6429 		if (err)
6430 			return err;
6431 	}
6432 
6433 	current_vlan_features = netdev->features &
6434 		NETIF_VLAN_FILTERING_FEATURES;
6435 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6436 	if (current_vlan_features ^ requested_vlan_features) {
6437 		err = ice_set_vlan_filtering_features(vsi, features);
6438 		if (err)
6439 			return err;
6440 	}
6441 
6442 	return 0;
6443 }
6444 
6445 /**
6446  * ice_set_loopback - turn on/off loopback mode on underlying PF
6447  * @vsi: ptr to VSI
6448  * @ena: flag to indicate the on/off setting
6449  */
6450 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6451 {
6452 	bool if_running = netif_running(vsi->netdev);
6453 	int ret;
6454 
6455 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6456 		ret = ice_down(vsi);
6457 		if (ret) {
6458 			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6459 			return ret;
6460 		}
6461 	}
6462 	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6463 	if (ret)
6464 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6465 	if (if_running)
6466 		ret = ice_up(vsi);
6467 
6468 	return ret;
6469 }
6470 
6471 /**
6472  * ice_set_features - set the netdev feature flags
6473  * @netdev: ptr to the netdev being adjusted
6474  * @features: the feature set that the stack is suggesting
6475  */
6476 static int
6477 ice_set_features(struct net_device *netdev, netdev_features_t features)
6478 {
6479 	netdev_features_t changed = netdev->features ^ features;
6480 	struct ice_netdev_priv *np = netdev_priv(netdev);
6481 	struct ice_vsi *vsi = np->vsi;
6482 	struct ice_pf *pf = vsi->back;
6483 	int ret = 0;
6484 
6485 	/* Don't set any netdev advanced features with device in Safe Mode */
6486 	if (ice_is_safe_mode(pf)) {
6487 		dev_err(ice_pf_to_dev(pf),
6488 			"Device is in Safe Mode - not enabling advanced netdev features\n");
6489 		return ret;
6490 	}
6491 
6492 	/* Do not change setting during reset */
6493 	if (ice_is_reset_in_progress(pf->state)) {
6494 		dev_err(ice_pf_to_dev(pf),
6495 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6496 		return -EBUSY;
6497 	}
6498 
6499 	/* Multiple features can be changed in one call so keep features in
6500 	 * separate if/else statements to guarantee each feature is checked
6501 	 */
6502 	if (changed & NETIF_F_RXHASH)
6503 		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6504 
6505 	ret = ice_set_vlan_features(netdev, features);
6506 	if (ret)
6507 		return ret;
6508 
6509 	/* Turn on receive of FCS aka CRC, and after setting this
6510 	 * flag the packet data will have the 4 byte CRC appended
6511 	 */
6512 	if (changed & NETIF_F_RXFCS) {
6513 		if ((features & NETIF_F_RXFCS) &&
6514 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6515 			dev_err(ice_pf_to_dev(vsi->back),
6516 				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6517 			return -EIO;
6518 		}
6519 
6520 		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6521 		ret = ice_down_up(vsi);
6522 		if (ret)
6523 			return ret;
6524 	}
6525 
6526 	if (changed & NETIF_F_NTUPLE) {
6527 		bool ena = !!(features & NETIF_F_NTUPLE);
6528 
6529 		ice_vsi_manage_fdir(vsi, ena);
6530 		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6531 	}
6532 
6533 	/* don't turn off hw_tc_offload when ADQ is already enabled */
6534 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6535 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6536 		return -EACCES;
6537 	}
6538 
6539 	if (changed & NETIF_F_HW_TC) {
6540 		bool ena = !!(features & NETIF_F_HW_TC);
6541 
6542 		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6543 		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6544 	}
6545 
6546 	if (changed & NETIF_F_LOOPBACK)
6547 		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6548 
6549 	return ret;
6550 }
6551 
6552 /**
6553  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6554  * @vsi: VSI to setup VLAN properties for
6555  */
6556 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6557 {
6558 	int err;
6559 
6560 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6561 	if (err)
6562 		return err;
6563 
6564 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6565 	if (err)
6566 		return err;
6567 
6568 	return ice_vsi_add_vlan_zero(vsi);
6569 }
6570 
6571 /**
6572  * ice_vsi_cfg_lan - Setup the VSI lan related config
6573  * @vsi: the VSI being configured
6574  *
6575  * Return 0 on success and negative value on error
6576  */
6577 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6578 {
6579 	int err;
6580 
6581 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6582 		ice_set_rx_mode(vsi->netdev);
6583 
6584 		err = ice_vsi_vlan_setup(vsi);
6585 		if (err)
6586 			return err;
6587 	}
6588 	ice_vsi_cfg_dcb_rings(vsi);
6589 
6590 	err = ice_vsi_cfg_lan_txqs(vsi);
6591 	if (!err && ice_is_xdp_ena_vsi(vsi))
6592 		err = ice_vsi_cfg_xdp_txqs(vsi);
6593 	if (!err)
6594 		err = ice_vsi_cfg_rxqs(vsi);
6595 
6596 	return err;
6597 }
6598 
6599 /* THEORY OF MODERATION:
6600  * The ice driver hardware works differently than the hardware that DIMLIB was
6601  * originally made for. ice hardware doesn't have packet count limits that
6602  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6603  * which is hard-coded to a limit of 250,000 ints/second.
6604  * If not using dynamic moderation, the INTRL value can be modified
6605  * by ethtool rx-usecs-high.
6606  */
6607 struct ice_dim {
6608 	/* the throttle rate for interrupts, basically worst case delay before
6609 	 * an initial interrupt fires, value is stored in microseconds.
6610 	 */
6611 	u16 itr;
6612 };
6613 
6614 /* Make a different profile for Rx that doesn't allow quite so aggressive
6615  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6616  * second.
6617  */
6618 static const struct ice_dim rx_profile[] = {
6619 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6620 	{8},    /* 125,000 ints/s */
6621 	{16},   /*  62,500 ints/s */
6622 	{62},   /*  16,129 ints/s */
6623 	{126}   /*   7,936 ints/s */
6624 };
6625 
6626 /* The transmit profile, which has the same sorts of values
6627  * as the previous struct
6628  */
6629 static const struct ice_dim tx_profile[] = {
6630 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6631 	{8},    /* 125,000 ints/s */
6632 	{40},   /*  16,125 ints/s */
6633 	{128},  /*   7,812 ints/s */
6634 	{256}   /*   3,906 ints/s */
6635 };
6636 
6637 static void ice_tx_dim_work(struct work_struct *work)
6638 {
6639 	struct ice_ring_container *rc;
6640 	struct dim *dim;
6641 	u16 itr;
6642 
6643 	dim = container_of(work, struct dim, work);
6644 	rc = dim->priv;
6645 
6646 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6647 
6648 	/* look up the values in our local table */
6649 	itr = tx_profile[dim->profile_ix].itr;
6650 
6651 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6652 	ice_write_itr(rc, itr);
6653 
6654 	dim->state = DIM_START_MEASURE;
6655 }
6656 
6657 static void ice_rx_dim_work(struct work_struct *work)
6658 {
6659 	struct ice_ring_container *rc;
6660 	struct dim *dim;
6661 	u16 itr;
6662 
6663 	dim = container_of(work, struct dim, work);
6664 	rc = dim->priv;
6665 
6666 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6667 
6668 	/* look up the values in our local table */
6669 	itr = rx_profile[dim->profile_ix].itr;
6670 
6671 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6672 	ice_write_itr(rc, itr);
6673 
6674 	dim->state = DIM_START_MEASURE;
6675 }
6676 
6677 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6678 
6679 /**
6680  * ice_init_moderation - set up interrupt moderation
6681  * @q_vector: the vector containing rings to be configured
6682  *
6683  * Set up interrupt moderation registers, with the intent to do the right thing
6684  * when called from reset or from probe, and whether or not dynamic moderation
6685  * is enabled or not. Take special care to write all the registers in both
6686  * dynamic moderation mode or not in order to make sure hardware is in a known
6687  * state.
6688  */
6689 static void ice_init_moderation(struct ice_q_vector *q_vector)
6690 {
6691 	struct ice_ring_container *rc;
6692 	bool tx_dynamic, rx_dynamic;
6693 
6694 	rc = &q_vector->tx;
6695 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6696 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6697 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6698 	rc->dim.priv = rc;
6699 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6700 
6701 	/* set the initial TX ITR to match the above */
6702 	ice_write_itr(rc, tx_dynamic ?
6703 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6704 
6705 	rc = &q_vector->rx;
6706 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6707 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6708 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6709 	rc->dim.priv = rc;
6710 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6711 
6712 	/* set the initial RX ITR to match the above */
6713 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6714 				       rc->itr_setting);
6715 
6716 	ice_set_q_vector_intrl(q_vector);
6717 }
6718 
6719 /**
6720  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6721  * @vsi: the VSI being configured
6722  */
6723 static void ice_napi_enable_all(struct ice_vsi *vsi)
6724 {
6725 	int q_idx;
6726 
6727 	if (!vsi->netdev)
6728 		return;
6729 
6730 	ice_for_each_q_vector(vsi, q_idx) {
6731 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6732 
6733 		ice_init_moderation(q_vector);
6734 
6735 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6736 			napi_enable(&q_vector->napi);
6737 	}
6738 }
6739 
6740 /**
6741  * ice_up_complete - Finish the last steps of bringing up a connection
6742  * @vsi: The VSI being configured
6743  *
6744  * Return 0 on success and negative value on error
6745  */
6746 static int ice_up_complete(struct ice_vsi *vsi)
6747 {
6748 	struct ice_pf *pf = vsi->back;
6749 	int err;
6750 
6751 	ice_vsi_cfg_msix(vsi);
6752 
6753 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6754 	 * Tx queue group list was configured and the context bits were
6755 	 * programmed using ice_vsi_cfg_txqs
6756 	 */
6757 	err = ice_vsi_start_all_rx_rings(vsi);
6758 	if (err)
6759 		return err;
6760 
6761 	clear_bit(ICE_VSI_DOWN, vsi->state);
6762 	ice_napi_enable_all(vsi);
6763 	ice_vsi_ena_irq(vsi);
6764 
6765 	if (vsi->port_info &&
6766 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6767 	    ((vsi->netdev && (vsi->type == ICE_VSI_PF ||
6768 			      vsi->type == ICE_VSI_SF)))) {
6769 		ice_print_link_msg(vsi, true);
6770 		netif_tx_start_all_queues(vsi->netdev);
6771 		netif_carrier_on(vsi->netdev);
6772 		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6773 	}
6774 
6775 	/* Perform an initial read of the statistics registers now to
6776 	 * set the baseline so counters are ready when interface is up
6777 	 */
6778 	ice_update_eth_stats(vsi);
6779 
6780 	if (vsi->type == ICE_VSI_PF)
6781 		ice_service_task_schedule(pf);
6782 
6783 	return 0;
6784 }
6785 
6786 /**
6787  * ice_up - Bring the connection back up after being down
6788  * @vsi: VSI being configured
6789  */
6790 int ice_up(struct ice_vsi *vsi)
6791 {
6792 	int err;
6793 
6794 	err = ice_vsi_cfg_lan(vsi);
6795 	if (!err)
6796 		err = ice_up_complete(vsi);
6797 
6798 	return err;
6799 }
6800 
6801 /**
6802  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6803  * @syncp: pointer to u64_stats_sync
6804  * @stats: stats that pkts and bytes count will be taken from
6805  * @pkts: packets stats counter
6806  * @bytes: bytes stats counter
6807  *
6808  * This function fetches stats from the ring considering the atomic operations
6809  * that needs to be performed to read u64 values in 32 bit machine.
6810  */
6811 void
6812 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6813 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6814 {
6815 	unsigned int start;
6816 
6817 	do {
6818 		start = u64_stats_fetch_begin(syncp);
6819 		*pkts = stats.pkts;
6820 		*bytes = stats.bytes;
6821 	} while (u64_stats_fetch_retry(syncp, start));
6822 }
6823 
6824 /**
6825  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6826  * @vsi: the VSI to be updated
6827  * @vsi_stats: the stats struct to be updated
6828  * @rings: rings to work on
6829  * @count: number of rings
6830  */
6831 static void
6832 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6833 			     struct rtnl_link_stats64 *vsi_stats,
6834 			     struct ice_tx_ring **rings, u16 count)
6835 {
6836 	u16 i;
6837 
6838 	for (i = 0; i < count; i++) {
6839 		struct ice_tx_ring *ring;
6840 		u64 pkts = 0, bytes = 0;
6841 
6842 		ring = READ_ONCE(rings[i]);
6843 		if (!ring || !ring->ring_stats)
6844 			continue;
6845 		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6846 					     ring->ring_stats->stats, &pkts,
6847 					     &bytes);
6848 		vsi_stats->tx_packets += pkts;
6849 		vsi_stats->tx_bytes += bytes;
6850 		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6851 		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6852 		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6853 	}
6854 }
6855 
6856 /**
6857  * ice_update_vsi_ring_stats - Update VSI stats counters
6858  * @vsi: the VSI to be updated
6859  */
6860 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6861 {
6862 	struct rtnl_link_stats64 *net_stats, *stats_prev;
6863 	struct rtnl_link_stats64 *vsi_stats;
6864 	struct ice_pf *pf = vsi->back;
6865 	u64 pkts, bytes;
6866 	int i;
6867 
6868 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6869 	if (!vsi_stats)
6870 		return;
6871 
6872 	/* reset non-netdev (extended) stats */
6873 	vsi->tx_restart = 0;
6874 	vsi->tx_busy = 0;
6875 	vsi->tx_linearize = 0;
6876 	vsi->rx_buf_failed = 0;
6877 	vsi->rx_page_failed = 0;
6878 
6879 	rcu_read_lock();
6880 
6881 	/* update Tx rings counters */
6882 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6883 				     vsi->num_txq);
6884 
6885 	/* update Rx rings counters */
6886 	ice_for_each_rxq(vsi, i) {
6887 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6888 		struct ice_ring_stats *ring_stats;
6889 
6890 		ring_stats = ring->ring_stats;
6891 		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6892 					     ring_stats->stats, &pkts,
6893 					     &bytes);
6894 		vsi_stats->rx_packets += pkts;
6895 		vsi_stats->rx_bytes += bytes;
6896 		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6897 		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6898 	}
6899 
6900 	/* update XDP Tx rings counters */
6901 	if (ice_is_xdp_ena_vsi(vsi))
6902 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6903 					     vsi->num_xdp_txq);
6904 
6905 	rcu_read_unlock();
6906 
6907 	net_stats = &vsi->net_stats;
6908 	stats_prev = &vsi->net_stats_prev;
6909 
6910 	/* Update netdev counters, but keep in mind that values could start at
6911 	 * random value after PF reset. And as we increase the reported stat by
6912 	 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6913 	 * let's skip this round.
6914 	 */
6915 	if (likely(pf->stat_prev_loaded)) {
6916 		net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6917 		net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6918 		net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6919 		net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6920 	}
6921 
6922 	stats_prev->tx_packets = vsi_stats->tx_packets;
6923 	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6924 	stats_prev->rx_packets = vsi_stats->rx_packets;
6925 	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6926 
6927 	kfree(vsi_stats);
6928 }
6929 
6930 /**
6931  * ice_update_vsi_stats - Update VSI stats counters
6932  * @vsi: the VSI to be updated
6933  */
6934 void ice_update_vsi_stats(struct ice_vsi *vsi)
6935 {
6936 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6937 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6938 	struct ice_pf *pf = vsi->back;
6939 
6940 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6941 	    test_bit(ICE_CFG_BUSY, pf->state))
6942 		return;
6943 
6944 	/* get stats as recorded by Tx/Rx rings */
6945 	ice_update_vsi_ring_stats(vsi);
6946 
6947 	/* get VSI stats as recorded by the hardware */
6948 	ice_update_eth_stats(vsi);
6949 
6950 	cur_ns->tx_errors = cur_es->tx_errors;
6951 	cur_ns->rx_dropped = cur_es->rx_discards;
6952 	cur_ns->tx_dropped = cur_es->tx_discards;
6953 	cur_ns->multicast = cur_es->rx_multicast;
6954 
6955 	/* update some more netdev stats if this is main VSI */
6956 	if (vsi->type == ICE_VSI_PF) {
6957 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6958 		cur_ns->rx_errors = pf->stats.crc_errors +
6959 				    pf->stats.illegal_bytes +
6960 				    pf->stats.rx_undersize +
6961 				    pf->hw_csum_rx_error +
6962 				    pf->stats.rx_jabber +
6963 				    pf->stats.rx_fragments +
6964 				    pf->stats.rx_oversize;
6965 		/* record drops from the port level */
6966 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6967 	}
6968 }
6969 
6970 /**
6971  * ice_update_pf_stats - Update PF port stats counters
6972  * @pf: PF whose stats needs to be updated
6973  */
6974 void ice_update_pf_stats(struct ice_pf *pf)
6975 {
6976 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6977 	struct ice_hw *hw = &pf->hw;
6978 	u16 fd_ctr_base;
6979 	u8 port;
6980 
6981 	port = hw->port_info->lport;
6982 	prev_ps = &pf->stats_prev;
6983 	cur_ps = &pf->stats;
6984 
6985 	if (ice_is_reset_in_progress(pf->state))
6986 		pf->stat_prev_loaded = false;
6987 
6988 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6989 			  &prev_ps->eth.rx_bytes,
6990 			  &cur_ps->eth.rx_bytes);
6991 
6992 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6993 			  &prev_ps->eth.rx_unicast,
6994 			  &cur_ps->eth.rx_unicast);
6995 
6996 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6997 			  &prev_ps->eth.rx_multicast,
6998 			  &cur_ps->eth.rx_multicast);
6999 
7000 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
7001 			  &prev_ps->eth.rx_broadcast,
7002 			  &cur_ps->eth.rx_broadcast);
7003 
7004 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
7005 			  &prev_ps->eth.rx_discards,
7006 			  &cur_ps->eth.rx_discards);
7007 
7008 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
7009 			  &prev_ps->eth.tx_bytes,
7010 			  &cur_ps->eth.tx_bytes);
7011 
7012 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
7013 			  &prev_ps->eth.tx_unicast,
7014 			  &cur_ps->eth.tx_unicast);
7015 
7016 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
7017 			  &prev_ps->eth.tx_multicast,
7018 			  &cur_ps->eth.tx_multicast);
7019 
7020 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
7021 			  &prev_ps->eth.tx_broadcast,
7022 			  &cur_ps->eth.tx_broadcast);
7023 
7024 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
7025 			  &prev_ps->tx_dropped_link_down,
7026 			  &cur_ps->tx_dropped_link_down);
7027 
7028 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
7029 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
7030 
7031 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
7032 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
7033 
7034 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
7035 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
7036 
7037 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
7038 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
7039 
7040 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
7041 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
7042 
7043 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
7044 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
7045 
7046 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
7047 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
7048 
7049 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
7050 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
7051 
7052 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
7053 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
7054 
7055 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
7056 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
7057 
7058 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
7059 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
7060 
7061 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
7062 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
7063 
7064 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
7065 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
7066 
7067 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
7068 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
7069 
7070 	fd_ctr_base = hw->fd_ctr_base;
7071 
7072 	ice_stat_update40(hw,
7073 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
7074 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
7075 			  &cur_ps->fd_sb_match);
7076 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
7077 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
7078 
7079 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
7080 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
7081 
7082 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
7083 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
7084 
7085 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
7086 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
7087 
7088 	ice_update_dcb_stats(pf);
7089 
7090 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
7091 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
7092 
7093 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
7094 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
7095 
7096 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
7097 			  &prev_ps->mac_local_faults,
7098 			  &cur_ps->mac_local_faults);
7099 
7100 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
7101 			  &prev_ps->mac_remote_faults,
7102 			  &cur_ps->mac_remote_faults);
7103 
7104 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
7105 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
7106 
7107 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
7108 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
7109 
7110 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
7111 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
7112 
7113 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
7114 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
7115 
7116 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
7117 
7118 	pf->stat_prev_loaded = true;
7119 }
7120 
7121 /**
7122  * ice_get_stats64 - get statistics for network device structure
7123  * @netdev: network interface device structure
7124  * @stats: main device statistics structure
7125  */
7126 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7127 {
7128 	struct ice_netdev_priv *np = netdev_priv(netdev);
7129 	struct rtnl_link_stats64 *vsi_stats;
7130 	struct ice_vsi *vsi = np->vsi;
7131 
7132 	vsi_stats = &vsi->net_stats;
7133 
7134 	if (!vsi->num_txq || !vsi->num_rxq)
7135 		return;
7136 
7137 	/* netdev packet/byte stats come from ring counter. These are obtained
7138 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
7139 	 * But, only call the update routine and read the registers if VSI is
7140 	 * not down.
7141 	 */
7142 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
7143 		ice_update_vsi_ring_stats(vsi);
7144 	stats->tx_packets = vsi_stats->tx_packets;
7145 	stats->tx_bytes = vsi_stats->tx_bytes;
7146 	stats->rx_packets = vsi_stats->rx_packets;
7147 	stats->rx_bytes = vsi_stats->rx_bytes;
7148 
7149 	/* The rest of the stats can be read from the hardware but instead we
7150 	 * just return values that the watchdog task has already obtained from
7151 	 * the hardware.
7152 	 */
7153 	stats->multicast = vsi_stats->multicast;
7154 	stats->tx_errors = vsi_stats->tx_errors;
7155 	stats->tx_dropped = vsi_stats->tx_dropped;
7156 	stats->rx_errors = vsi_stats->rx_errors;
7157 	stats->rx_dropped = vsi_stats->rx_dropped;
7158 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7159 	stats->rx_length_errors = vsi_stats->rx_length_errors;
7160 }
7161 
7162 /**
7163  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7164  * @vsi: VSI having NAPI disabled
7165  */
7166 static void ice_napi_disable_all(struct ice_vsi *vsi)
7167 {
7168 	int q_idx;
7169 
7170 	if (!vsi->netdev)
7171 		return;
7172 
7173 	ice_for_each_q_vector(vsi, q_idx) {
7174 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7175 
7176 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
7177 			napi_disable(&q_vector->napi);
7178 
7179 		cancel_work_sync(&q_vector->tx.dim.work);
7180 		cancel_work_sync(&q_vector->rx.dim.work);
7181 	}
7182 }
7183 
7184 /**
7185  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
7186  * @vsi: the VSI being un-configured
7187  */
7188 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
7189 {
7190 	struct ice_pf *pf = vsi->back;
7191 	struct ice_hw *hw = &pf->hw;
7192 	u32 val;
7193 	int i;
7194 
7195 	/* disable interrupt causation from each Rx queue; Tx queues are
7196 	 * handled in ice_vsi_stop_tx_ring()
7197 	 */
7198 	if (vsi->rx_rings) {
7199 		ice_for_each_rxq(vsi, i) {
7200 			if (vsi->rx_rings[i]) {
7201 				u16 reg;
7202 
7203 				reg = vsi->rx_rings[i]->reg_idx;
7204 				val = rd32(hw, QINT_RQCTL(reg));
7205 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
7206 				wr32(hw, QINT_RQCTL(reg), val);
7207 			}
7208 		}
7209 	}
7210 
7211 	/* disable each interrupt */
7212 	ice_for_each_q_vector(vsi, i) {
7213 		if (!vsi->q_vectors[i])
7214 			continue;
7215 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
7216 	}
7217 
7218 	ice_flush(hw);
7219 
7220 	/* don't call synchronize_irq() for VF's from the host */
7221 	if (vsi->type == ICE_VSI_VF)
7222 		return;
7223 
7224 	ice_for_each_q_vector(vsi, i)
7225 		synchronize_irq(vsi->q_vectors[i]->irq.virq);
7226 }
7227 
7228 /**
7229  * ice_down - Shutdown the connection
7230  * @vsi: The VSI being stopped
7231  *
7232  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
7233  */
7234 int ice_down(struct ice_vsi *vsi)
7235 {
7236 	int i, tx_err, rx_err, vlan_err = 0;
7237 
7238 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7239 
7240 	if (vsi->netdev) {
7241 		vlan_err = ice_vsi_del_vlan_zero(vsi);
7242 		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
7243 		netif_carrier_off(vsi->netdev);
7244 		netif_tx_disable(vsi->netdev);
7245 	}
7246 
7247 	ice_vsi_dis_irq(vsi);
7248 
7249 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
7250 	if (tx_err)
7251 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
7252 			   vsi->vsi_num, tx_err);
7253 	if (!tx_err && vsi->xdp_rings) {
7254 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7255 		if (tx_err)
7256 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
7257 				   vsi->vsi_num, tx_err);
7258 	}
7259 
7260 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
7261 	if (rx_err)
7262 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
7263 			   vsi->vsi_num, rx_err);
7264 
7265 	ice_napi_disable_all(vsi);
7266 
7267 	ice_for_each_txq(vsi, i)
7268 		ice_clean_tx_ring(vsi->tx_rings[i]);
7269 
7270 	if (vsi->xdp_rings)
7271 		ice_for_each_xdp_txq(vsi, i)
7272 			ice_clean_tx_ring(vsi->xdp_rings[i]);
7273 
7274 	ice_for_each_rxq(vsi, i)
7275 		ice_clean_rx_ring(vsi->rx_rings[i]);
7276 
7277 	if (tx_err || rx_err || vlan_err) {
7278 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
7279 			   vsi->vsi_num, vsi->vsw->sw_id);
7280 		return -EIO;
7281 	}
7282 
7283 	return 0;
7284 }
7285 
7286 /**
7287  * ice_down_up - shutdown the VSI connection and bring it up
7288  * @vsi: the VSI to be reconnected
7289  */
7290 int ice_down_up(struct ice_vsi *vsi)
7291 {
7292 	int ret;
7293 
7294 	/* if DOWN already set, nothing to do */
7295 	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7296 		return 0;
7297 
7298 	ret = ice_down(vsi);
7299 	if (ret)
7300 		return ret;
7301 
7302 	ret = ice_up(vsi);
7303 	if (ret) {
7304 		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7305 		return ret;
7306 	}
7307 
7308 	return 0;
7309 }
7310 
7311 /**
7312  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7313  * @vsi: VSI having resources allocated
7314  *
7315  * Return 0 on success, negative on failure
7316  */
7317 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7318 {
7319 	int i, err = 0;
7320 
7321 	if (!vsi->num_txq) {
7322 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7323 			vsi->vsi_num);
7324 		return -EINVAL;
7325 	}
7326 
7327 	ice_for_each_txq(vsi, i) {
7328 		struct ice_tx_ring *ring = vsi->tx_rings[i];
7329 
7330 		if (!ring)
7331 			return -EINVAL;
7332 
7333 		if (vsi->netdev)
7334 			ring->netdev = vsi->netdev;
7335 		err = ice_setup_tx_ring(ring);
7336 		if (err)
7337 			break;
7338 	}
7339 
7340 	return err;
7341 }
7342 
7343 /**
7344  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7345  * @vsi: VSI having resources allocated
7346  *
7347  * Return 0 on success, negative on failure
7348  */
7349 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7350 {
7351 	int i, err = 0;
7352 
7353 	if (!vsi->num_rxq) {
7354 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7355 			vsi->vsi_num);
7356 		return -EINVAL;
7357 	}
7358 
7359 	ice_for_each_rxq(vsi, i) {
7360 		struct ice_rx_ring *ring = vsi->rx_rings[i];
7361 
7362 		if (!ring)
7363 			return -EINVAL;
7364 
7365 		if (vsi->netdev)
7366 			ring->netdev = vsi->netdev;
7367 		err = ice_setup_rx_ring(ring);
7368 		if (err)
7369 			break;
7370 	}
7371 
7372 	return err;
7373 }
7374 
7375 /**
7376  * ice_vsi_open_ctrl - open control VSI for use
7377  * @vsi: the VSI to open
7378  *
7379  * Initialization of the Control VSI
7380  *
7381  * Returns 0 on success, negative value on error
7382  */
7383 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7384 {
7385 	char int_name[ICE_INT_NAME_STR_LEN];
7386 	struct ice_pf *pf = vsi->back;
7387 	struct device *dev;
7388 	int err;
7389 
7390 	dev = ice_pf_to_dev(pf);
7391 	/* allocate descriptors */
7392 	err = ice_vsi_setup_tx_rings(vsi);
7393 	if (err)
7394 		goto err_setup_tx;
7395 
7396 	err = ice_vsi_setup_rx_rings(vsi);
7397 	if (err)
7398 		goto err_setup_rx;
7399 
7400 	err = ice_vsi_cfg_lan(vsi);
7401 	if (err)
7402 		goto err_setup_rx;
7403 
7404 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7405 		 dev_driver_string(dev), dev_name(dev));
7406 	err = ice_vsi_req_irq_msix(vsi, int_name);
7407 	if (err)
7408 		goto err_setup_rx;
7409 
7410 	ice_vsi_cfg_msix(vsi);
7411 
7412 	err = ice_vsi_start_all_rx_rings(vsi);
7413 	if (err)
7414 		goto err_up_complete;
7415 
7416 	clear_bit(ICE_VSI_DOWN, vsi->state);
7417 	ice_vsi_ena_irq(vsi);
7418 
7419 	return 0;
7420 
7421 err_up_complete:
7422 	ice_down(vsi);
7423 err_setup_rx:
7424 	ice_vsi_free_rx_rings(vsi);
7425 err_setup_tx:
7426 	ice_vsi_free_tx_rings(vsi);
7427 
7428 	return err;
7429 }
7430 
7431 /**
7432  * ice_vsi_open - Called when a network interface is made active
7433  * @vsi: the VSI to open
7434  *
7435  * Initialization of the VSI
7436  *
7437  * Returns 0 on success, negative value on error
7438  */
7439 int ice_vsi_open(struct ice_vsi *vsi)
7440 {
7441 	char int_name[ICE_INT_NAME_STR_LEN];
7442 	struct ice_pf *pf = vsi->back;
7443 	int err;
7444 
7445 	/* allocate descriptors */
7446 	err = ice_vsi_setup_tx_rings(vsi);
7447 	if (err)
7448 		goto err_setup_tx;
7449 
7450 	err = ice_vsi_setup_rx_rings(vsi);
7451 	if (err)
7452 		goto err_setup_rx;
7453 
7454 	err = ice_vsi_cfg_lan(vsi);
7455 	if (err)
7456 		goto err_setup_rx;
7457 
7458 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7459 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7460 	err = ice_vsi_req_irq_msix(vsi, int_name);
7461 	if (err)
7462 		goto err_setup_rx;
7463 
7464 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7465 
7466 	if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_SF) {
7467 		/* Notify the stack of the actual queue counts. */
7468 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7469 		if (err)
7470 			goto err_set_qs;
7471 
7472 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7473 		if (err)
7474 			goto err_set_qs;
7475 
7476 		ice_vsi_set_napi_queues(vsi);
7477 	}
7478 
7479 	err = ice_up_complete(vsi);
7480 	if (err)
7481 		goto err_up_complete;
7482 
7483 	return 0;
7484 
7485 err_up_complete:
7486 	ice_down(vsi);
7487 err_set_qs:
7488 	ice_vsi_free_irq(vsi);
7489 err_setup_rx:
7490 	ice_vsi_free_rx_rings(vsi);
7491 err_setup_tx:
7492 	ice_vsi_free_tx_rings(vsi);
7493 
7494 	return err;
7495 }
7496 
7497 /**
7498  * ice_vsi_release_all - Delete all VSIs
7499  * @pf: PF from which all VSIs are being removed
7500  */
7501 static void ice_vsi_release_all(struct ice_pf *pf)
7502 {
7503 	int err, i;
7504 
7505 	if (!pf->vsi)
7506 		return;
7507 
7508 	ice_for_each_vsi(pf, i) {
7509 		if (!pf->vsi[i])
7510 			continue;
7511 
7512 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7513 			continue;
7514 
7515 		err = ice_vsi_release(pf->vsi[i]);
7516 		if (err)
7517 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7518 				i, err, pf->vsi[i]->vsi_num);
7519 	}
7520 }
7521 
7522 /**
7523  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7524  * @pf: pointer to the PF instance
7525  * @type: VSI type to rebuild
7526  *
7527  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7528  */
7529 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7530 {
7531 	struct device *dev = ice_pf_to_dev(pf);
7532 	int i, err;
7533 
7534 	ice_for_each_vsi(pf, i) {
7535 		struct ice_vsi *vsi = pf->vsi[i];
7536 
7537 		if (!vsi || vsi->type != type)
7538 			continue;
7539 
7540 		/* rebuild the VSI */
7541 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7542 		if (err) {
7543 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7544 				err, vsi->idx, ice_vsi_type_str(type));
7545 			return err;
7546 		}
7547 
7548 		/* replay filters for the VSI */
7549 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7550 		if (err) {
7551 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7552 				err, vsi->idx, ice_vsi_type_str(type));
7553 			return err;
7554 		}
7555 
7556 		/* Re-map HW VSI number, using VSI handle that has been
7557 		 * previously validated in ice_replay_vsi() call above
7558 		 */
7559 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7560 
7561 		/* enable the VSI */
7562 		err = ice_ena_vsi(vsi, false);
7563 		if (err) {
7564 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7565 				err, vsi->idx, ice_vsi_type_str(type));
7566 			return err;
7567 		}
7568 
7569 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7570 			 ice_vsi_type_str(type));
7571 	}
7572 
7573 	return 0;
7574 }
7575 
7576 /**
7577  * ice_update_pf_netdev_link - Update PF netdev link status
7578  * @pf: pointer to the PF instance
7579  */
7580 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7581 {
7582 	bool link_up;
7583 	int i;
7584 
7585 	ice_for_each_vsi(pf, i) {
7586 		struct ice_vsi *vsi = pf->vsi[i];
7587 
7588 		if (!vsi || vsi->type != ICE_VSI_PF)
7589 			return;
7590 
7591 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7592 		if (link_up) {
7593 			netif_carrier_on(pf->vsi[i]->netdev);
7594 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7595 		} else {
7596 			netif_carrier_off(pf->vsi[i]->netdev);
7597 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7598 		}
7599 	}
7600 }
7601 
7602 /**
7603  * ice_rebuild - rebuild after reset
7604  * @pf: PF to rebuild
7605  * @reset_type: type of reset
7606  *
7607  * Do not rebuild VF VSI in this flow because that is already handled via
7608  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7609  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7610  * to reset/rebuild all the VF VSI twice.
7611  */
7612 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7613 {
7614 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
7615 	struct device *dev = ice_pf_to_dev(pf);
7616 	struct ice_hw *hw = &pf->hw;
7617 	bool dvm;
7618 	int err;
7619 
7620 	if (test_bit(ICE_DOWN, pf->state))
7621 		goto clear_recovery;
7622 
7623 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7624 
7625 #define ICE_EMP_RESET_SLEEP_MS 5000
7626 	if (reset_type == ICE_RESET_EMPR) {
7627 		/* If an EMP reset has occurred, any previously pending flash
7628 		 * update will have completed. We no longer know whether or
7629 		 * not the NVM update EMP reset is restricted.
7630 		 */
7631 		pf->fw_emp_reset_disabled = false;
7632 
7633 		msleep(ICE_EMP_RESET_SLEEP_MS);
7634 	}
7635 
7636 	err = ice_init_all_ctrlq(hw);
7637 	if (err) {
7638 		dev_err(dev, "control queues init failed %d\n", err);
7639 		goto err_init_ctrlq;
7640 	}
7641 
7642 	/* if DDP was previously loaded successfully */
7643 	if (!ice_is_safe_mode(pf)) {
7644 		/* reload the SW DB of filter tables */
7645 		if (reset_type == ICE_RESET_PFR)
7646 			ice_fill_blk_tbls(hw);
7647 		else
7648 			/* Reload DDP Package after CORER/GLOBR reset */
7649 			ice_load_pkg(NULL, pf);
7650 	}
7651 
7652 	err = ice_clear_pf_cfg(hw);
7653 	if (err) {
7654 		dev_err(dev, "clear PF configuration failed %d\n", err);
7655 		goto err_init_ctrlq;
7656 	}
7657 
7658 	ice_clear_pxe_mode(hw);
7659 
7660 	err = ice_init_nvm(hw);
7661 	if (err) {
7662 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7663 		goto err_init_ctrlq;
7664 	}
7665 
7666 	err = ice_get_caps(hw);
7667 	if (err) {
7668 		dev_err(dev, "ice_get_caps failed %d\n", err);
7669 		goto err_init_ctrlq;
7670 	}
7671 
7672 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7673 	if (err) {
7674 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7675 		goto err_init_ctrlq;
7676 	}
7677 
7678 	dvm = ice_is_dvm_ena(hw);
7679 
7680 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7681 	if (err)
7682 		goto err_init_ctrlq;
7683 
7684 	err = ice_sched_init_port(hw->port_info);
7685 	if (err)
7686 		goto err_sched_init_port;
7687 
7688 	/* start misc vector */
7689 	err = ice_req_irq_msix_misc(pf);
7690 	if (err) {
7691 		dev_err(dev, "misc vector setup failed: %d\n", err);
7692 		goto err_sched_init_port;
7693 	}
7694 
7695 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7696 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7697 		if (!rd32(hw, PFQF_FD_SIZE)) {
7698 			u16 unused, guar, b_effort;
7699 
7700 			guar = hw->func_caps.fd_fltr_guar;
7701 			b_effort = hw->func_caps.fd_fltr_best_effort;
7702 
7703 			/* force guaranteed filter pool for PF */
7704 			ice_alloc_fd_guar_item(hw, &unused, guar);
7705 			/* force shared filter pool for PF */
7706 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7707 		}
7708 	}
7709 
7710 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7711 		ice_dcb_rebuild(pf);
7712 
7713 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7714 	 * the VSI rebuild. If not, this causes the PTP link status events to
7715 	 * fail.
7716 	 */
7717 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7718 		ice_ptp_rebuild(pf, reset_type);
7719 
7720 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7721 		ice_gnss_init(pf);
7722 
7723 	/* rebuild PF VSI */
7724 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7725 	if (err) {
7726 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7727 		goto err_vsi_rebuild;
7728 	}
7729 
7730 	if (reset_type == ICE_RESET_PFR) {
7731 		err = ice_rebuild_channels(pf);
7732 		if (err) {
7733 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7734 				err);
7735 			goto err_vsi_rebuild;
7736 		}
7737 	}
7738 
7739 	/* If Flow Director is active */
7740 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7741 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7742 		if (err) {
7743 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7744 			goto err_vsi_rebuild;
7745 		}
7746 
7747 		/* replay HW Flow Director recipes */
7748 		if (hw->fdir_prof)
7749 			ice_fdir_replay_flows(hw);
7750 
7751 		/* replay Flow Director filters */
7752 		ice_fdir_replay_fltrs(pf);
7753 
7754 		ice_rebuild_arfs(pf);
7755 	}
7756 
7757 	if (vsi && vsi->netdev)
7758 		netif_device_attach(vsi->netdev);
7759 
7760 	ice_update_pf_netdev_link(pf);
7761 
7762 	/* tell the firmware we are up */
7763 	err = ice_send_version(pf);
7764 	if (err) {
7765 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7766 			err);
7767 		goto err_vsi_rebuild;
7768 	}
7769 
7770 	ice_replay_post(hw);
7771 
7772 	/* if we get here, reset flow is successful */
7773 	clear_bit(ICE_RESET_FAILED, pf->state);
7774 
7775 	ice_plug_aux_dev(pf);
7776 	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7777 		ice_lag_rebuild(pf);
7778 
7779 	/* Restore timestamp mode settings after VSI rebuild */
7780 	ice_ptp_restore_timestamp_mode(pf);
7781 	return;
7782 
7783 err_vsi_rebuild:
7784 err_sched_init_port:
7785 	ice_sched_cleanup_all(hw);
7786 err_init_ctrlq:
7787 	ice_shutdown_all_ctrlq(hw, false);
7788 	set_bit(ICE_RESET_FAILED, pf->state);
7789 clear_recovery:
7790 	/* set this bit in PF state to control service task scheduling */
7791 	set_bit(ICE_NEEDS_RESTART, pf->state);
7792 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7793 }
7794 
7795 /**
7796  * ice_change_mtu - NDO callback to change the MTU
7797  * @netdev: network interface device structure
7798  * @new_mtu: new value for maximum frame size
7799  *
7800  * Returns 0 on success, negative on failure
7801  */
7802 int ice_change_mtu(struct net_device *netdev, int new_mtu)
7803 {
7804 	struct ice_netdev_priv *np = netdev_priv(netdev);
7805 	struct ice_vsi *vsi = np->vsi;
7806 	struct ice_pf *pf = vsi->back;
7807 	struct bpf_prog *prog;
7808 	u8 count = 0;
7809 	int err = 0;
7810 
7811 	if (new_mtu == (int)netdev->mtu) {
7812 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7813 		return 0;
7814 	}
7815 
7816 	prog = vsi->xdp_prog;
7817 	if (prog && !prog->aux->xdp_has_frags) {
7818 		int frame_size = ice_max_xdp_frame_size(vsi);
7819 
7820 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7821 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7822 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7823 			return -EINVAL;
7824 		}
7825 	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7826 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7827 			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7828 				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7829 			return -EINVAL;
7830 		}
7831 	}
7832 
7833 	/* if a reset is in progress, wait for some time for it to complete */
7834 	do {
7835 		if (ice_is_reset_in_progress(pf->state)) {
7836 			count++;
7837 			usleep_range(1000, 2000);
7838 		} else {
7839 			break;
7840 		}
7841 
7842 	} while (count < 100);
7843 
7844 	if (count == 100) {
7845 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7846 		return -EBUSY;
7847 	}
7848 
7849 	WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
7850 	err = ice_down_up(vsi);
7851 	if (err)
7852 		return err;
7853 
7854 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7855 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7856 
7857 	return err;
7858 }
7859 
7860 /**
7861  * ice_eth_ioctl - Access the hwtstamp interface
7862  * @netdev: network interface device structure
7863  * @ifr: interface request data
7864  * @cmd: ioctl command
7865  */
7866 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7867 {
7868 	struct ice_netdev_priv *np = netdev_priv(netdev);
7869 	struct ice_pf *pf = np->vsi->back;
7870 
7871 	switch (cmd) {
7872 	case SIOCGHWTSTAMP:
7873 		return ice_ptp_get_ts_config(pf, ifr);
7874 	case SIOCSHWTSTAMP:
7875 		return ice_ptp_set_ts_config(pf, ifr);
7876 	default:
7877 		return -EOPNOTSUPP;
7878 	}
7879 }
7880 
7881 /**
7882  * ice_aq_str - convert AQ err code to a string
7883  * @aq_err: the AQ error code to convert
7884  */
7885 const char *ice_aq_str(enum ice_aq_err aq_err)
7886 {
7887 	switch (aq_err) {
7888 	case ICE_AQ_RC_OK:
7889 		return "OK";
7890 	case ICE_AQ_RC_EPERM:
7891 		return "ICE_AQ_RC_EPERM";
7892 	case ICE_AQ_RC_ENOENT:
7893 		return "ICE_AQ_RC_ENOENT";
7894 	case ICE_AQ_RC_ENOMEM:
7895 		return "ICE_AQ_RC_ENOMEM";
7896 	case ICE_AQ_RC_EBUSY:
7897 		return "ICE_AQ_RC_EBUSY";
7898 	case ICE_AQ_RC_EEXIST:
7899 		return "ICE_AQ_RC_EEXIST";
7900 	case ICE_AQ_RC_EINVAL:
7901 		return "ICE_AQ_RC_EINVAL";
7902 	case ICE_AQ_RC_ENOSPC:
7903 		return "ICE_AQ_RC_ENOSPC";
7904 	case ICE_AQ_RC_ENOSYS:
7905 		return "ICE_AQ_RC_ENOSYS";
7906 	case ICE_AQ_RC_EMODE:
7907 		return "ICE_AQ_RC_EMODE";
7908 	case ICE_AQ_RC_ENOSEC:
7909 		return "ICE_AQ_RC_ENOSEC";
7910 	case ICE_AQ_RC_EBADSIG:
7911 		return "ICE_AQ_RC_EBADSIG";
7912 	case ICE_AQ_RC_ESVN:
7913 		return "ICE_AQ_RC_ESVN";
7914 	case ICE_AQ_RC_EBADMAN:
7915 		return "ICE_AQ_RC_EBADMAN";
7916 	case ICE_AQ_RC_EBADBUF:
7917 		return "ICE_AQ_RC_EBADBUF";
7918 	}
7919 
7920 	return "ICE_AQ_RC_UNKNOWN";
7921 }
7922 
7923 /**
7924  * ice_set_rss_lut - Set RSS LUT
7925  * @vsi: Pointer to VSI structure
7926  * @lut: Lookup table
7927  * @lut_size: Lookup table size
7928  *
7929  * Returns 0 on success, negative on failure
7930  */
7931 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7932 {
7933 	struct ice_aq_get_set_rss_lut_params params = {};
7934 	struct ice_hw *hw = &vsi->back->hw;
7935 	int status;
7936 
7937 	if (!lut)
7938 		return -EINVAL;
7939 
7940 	params.vsi_handle = vsi->idx;
7941 	params.lut_size = lut_size;
7942 	params.lut_type = vsi->rss_lut_type;
7943 	params.lut = lut;
7944 
7945 	status = ice_aq_set_rss_lut(hw, &params);
7946 	if (status)
7947 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7948 			status, ice_aq_str(hw->adminq.sq_last_status));
7949 
7950 	return status;
7951 }
7952 
7953 /**
7954  * ice_set_rss_key - Set RSS key
7955  * @vsi: Pointer to the VSI structure
7956  * @seed: RSS hash seed
7957  *
7958  * Returns 0 on success, negative on failure
7959  */
7960 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7961 {
7962 	struct ice_hw *hw = &vsi->back->hw;
7963 	int status;
7964 
7965 	if (!seed)
7966 		return -EINVAL;
7967 
7968 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7969 	if (status)
7970 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7971 			status, ice_aq_str(hw->adminq.sq_last_status));
7972 
7973 	return status;
7974 }
7975 
7976 /**
7977  * ice_get_rss_lut - Get RSS LUT
7978  * @vsi: Pointer to VSI structure
7979  * @lut: Buffer to store the lookup table entries
7980  * @lut_size: Size of buffer to store the lookup table entries
7981  *
7982  * Returns 0 on success, negative on failure
7983  */
7984 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7985 {
7986 	struct ice_aq_get_set_rss_lut_params params = {};
7987 	struct ice_hw *hw = &vsi->back->hw;
7988 	int status;
7989 
7990 	if (!lut)
7991 		return -EINVAL;
7992 
7993 	params.vsi_handle = vsi->idx;
7994 	params.lut_size = lut_size;
7995 	params.lut_type = vsi->rss_lut_type;
7996 	params.lut = lut;
7997 
7998 	status = ice_aq_get_rss_lut(hw, &params);
7999 	if (status)
8000 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
8001 			status, ice_aq_str(hw->adminq.sq_last_status));
8002 
8003 	return status;
8004 }
8005 
8006 /**
8007  * ice_get_rss_key - Get RSS key
8008  * @vsi: Pointer to VSI structure
8009  * @seed: Buffer to store the key in
8010  *
8011  * Returns 0 on success, negative on failure
8012  */
8013 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
8014 {
8015 	struct ice_hw *hw = &vsi->back->hw;
8016 	int status;
8017 
8018 	if (!seed)
8019 		return -EINVAL;
8020 
8021 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
8022 	if (status)
8023 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
8024 			status, ice_aq_str(hw->adminq.sq_last_status));
8025 
8026 	return status;
8027 }
8028 
8029 /**
8030  * ice_set_rss_hfunc - Set RSS HASH function
8031  * @vsi: Pointer to VSI structure
8032  * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
8033  *
8034  * Returns 0 on success, negative on failure
8035  */
8036 int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
8037 {
8038 	struct ice_hw *hw = &vsi->back->hw;
8039 	struct ice_vsi_ctx *ctx;
8040 	bool symm;
8041 	int err;
8042 
8043 	if (hfunc == vsi->rss_hfunc)
8044 		return 0;
8045 
8046 	if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
8047 	    hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
8048 		return -EOPNOTSUPP;
8049 
8050 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8051 	if (!ctx)
8052 		return -ENOMEM;
8053 
8054 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
8055 	ctx->info.q_opt_rss = vsi->info.q_opt_rss;
8056 	ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
8057 	ctx->info.q_opt_rss |=
8058 		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
8059 	ctx->info.q_opt_tc = vsi->info.q_opt_tc;
8060 	ctx->info.q_opt_flags = vsi->info.q_opt_rss;
8061 
8062 	err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
8063 	if (err) {
8064 		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
8065 			vsi->vsi_num, err);
8066 	} else {
8067 		vsi->info.q_opt_rss = ctx->info.q_opt_rss;
8068 		vsi->rss_hfunc = hfunc;
8069 		netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
8070 			    hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
8071 			    "Symmetric " : "");
8072 	}
8073 	kfree(ctx);
8074 	if (err)
8075 		return err;
8076 
8077 	/* Fix the symmetry setting for all existing RSS configurations */
8078 	symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
8079 	return ice_set_rss_cfg_symm(hw, vsi, symm);
8080 }
8081 
8082 /**
8083  * ice_bridge_getlink - Get the hardware bridge mode
8084  * @skb: skb buff
8085  * @pid: process ID
8086  * @seq: RTNL message seq
8087  * @dev: the netdev being configured
8088  * @filter_mask: filter mask passed in
8089  * @nlflags: netlink flags passed in
8090  *
8091  * Return the bridge mode (VEB/VEPA)
8092  */
8093 static int
8094 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8095 		   struct net_device *dev, u32 filter_mask, int nlflags)
8096 {
8097 	struct ice_netdev_priv *np = netdev_priv(dev);
8098 	struct ice_vsi *vsi = np->vsi;
8099 	struct ice_pf *pf = vsi->back;
8100 	u16 bmode;
8101 
8102 	bmode = pf->first_sw->bridge_mode;
8103 
8104 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
8105 				       filter_mask, NULL);
8106 }
8107 
8108 /**
8109  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
8110  * @vsi: Pointer to VSI structure
8111  * @bmode: Hardware bridge mode (VEB/VEPA)
8112  *
8113  * Returns 0 on success, negative on failure
8114  */
8115 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
8116 {
8117 	struct ice_aqc_vsi_props *vsi_props;
8118 	struct ice_hw *hw = &vsi->back->hw;
8119 	struct ice_vsi_ctx *ctxt;
8120 	int ret;
8121 
8122 	vsi_props = &vsi->info;
8123 
8124 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
8125 	if (!ctxt)
8126 		return -ENOMEM;
8127 
8128 	ctxt->info = vsi->info;
8129 
8130 	if (bmode == BRIDGE_MODE_VEB)
8131 		/* change from VEPA to VEB mode */
8132 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8133 	else
8134 		/* change from VEB to VEPA mode */
8135 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8136 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
8137 
8138 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
8139 	if (ret) {
8140 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
8141 			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
8142 		goto out;
8143 	}
8144 	/* Update sw flags for book keeping */
8145 	vsi_props->sw_flags = ctxt->info.sw_flags;
8146 
8147 out:
8148 	kfree(ctxt);
8149 	return ret;
8150 }
8151 
8152 /**
8153  * ice_bridge_setlink - Set the hardware bridge mode
8154  * @dev: the netdev being configured
8155  * @nlh: RTNL message
8156  * @flags: bridge setlink flags
8157  * @extack: netlink extended ack
8158  *
8159  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
8160  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
8161  * not already set for all VSIs connected to this switch. And also update the
8162  * unicast switch filter rules for the corresponding switch of the netdev.
8163  */
8164 static int
8165 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8166 		   u16 __always_unused flags,
8167 		   struct netlink_ext_ack __always_unused *extack)
8168 {
8169 	struct ice_netdev_priv *np = netdev_priv(dev);
8170 	struct ice_pf *pf = np->vsi->back;
8171 	struct nlattr *attr, *br_spec;
8172 	struct ice_hw *hw = &pf->hw;
8173 	struct ice_sw *pf_sw;
8174 	int rem, v, err = 0;
8175 
8176 	pf_sw = pf->first_sw;
8177 	/* find the attribute in the netlink message */
8178 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8179 	if (!br_spec)
8180 		return -EINVAL;
8181 
8182 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
8183 		__u16 mode = nla_get_u16(attr);
8184 
8185 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8186 			return -EINVAL;
8187 		/* Continue  if bridge mode is not being flipped */
8188 		if (mode == pf_sw->bridge_mode)
8189 			continue;
8190 		/* Iterates through the PF VSI list and update the loopback
8191 		 * mode of the VSI
8192 		 */
8193 		ice_for_each_vsi(pf, v) {
8194 			if (!pf->vsi[v])
8195 				continue;
8196 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8197 			if (err)
8198 				return err;
8199 		}
8200 
8201 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8202 		/* Update the unicast switch filter rules for the corresponding
8203 		 * switch of the netdev
8204 		 */
8205 		err = ice_update_sw_rule_bridge_mode(hw);
8206 		if (err) {
8207 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
8208 				   mode, err,
8209 				   ice_aq_str(hw->adminq.sq_last_status));
8210 			/* revert hw->evb_veb */
8211 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
8212 			return err;
8213 		}
8214 
8215 		pf_sw->bridge_mode = mode;
8216 	}
8217 
8218 	return 0;
8219 }
8220 
8221 /**
8222  * ice_tx_timeout - Respond to a Tx Hang
8223  * @netdev: network interface device structure
8224  * @txqueue: Tx queue
8225  */
8226 void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
8227 {
8228 	struct ice_netdev_priv *np = netdev_priv(netdev);
8229 	struct ice_tx_ring *tx_ring = NULL;
8230 	struct ice_vsi *vsi = np->vsi;
8231 	struct ice_pf *pf = vsi->back;
8232 	u32 i;
8233 
8234 	pf->tx_timeout_count++;
8235 
8236 	/* Check if PFC is enabled for the TC to which the queue belongs
8237 	 * to. If yes then Tx timeout is not caused by a hung queue, no
8238 	 * need to reset and rebuild
8239 	 */
8240 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8241 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8242 			 txqueue);
8243 		return;
8244 	}
8245 
8246 	/* now that we have an index, find the tx_ring struct */
8247 	ice_for_each_txq(vsi, i)
8248 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8249 			if (txqueue == vsi->tx_rings[i]->q_index) {
8250 				tx_ring = vsi->tx_rings[i];
8251 				break;
8252 			}
8253 
8254 	/* Reset recovery level if enough time has elapsed after last timeout.
8255 	 * Also ensure no new reset action happens before next timeout period.
8256 	 */
8257 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8258 		pf->tx_timeout_recovery_level = 1;
8259 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8260 				       netdev->watchdog_timeo)))
8261 		return;
8262 
8263 	if (tx_ring) {
8264 		struct ice_hw *hw = &pf->hw;
8265 		u32 head, val = 0;
8266 
8267 		head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8268 				 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
8269 		/* Read interrupt register */
8270 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
8271 
8272 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
8273 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
8274 			    head, tx_ring->next_to_use, val);
8275 	}
8276 
8277 	pf->tx_timeout_last_recovery = jiffies;
8278 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
8279 		    pf->tx_timeout_recovery_level, txqueue);
8280 
8281 	switch (pf->tx_timeout_recovery_level) {
8282 	case 1:
8283 		set_bit(ICE_PFR_REQ, pf->state);
8284 		break;
8285 	case 2:
8286 		set_bit(ICE_CORER_REQ, pf->state);
8287 		break;
8288 	case 3:
8289 		set_bit(ICE_GLOBR_REQ, pf->state);
8290 		break;
8291 	default:
8292 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
8293 		set_bit(ICE_DOWN, pf->state);
8294 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
8295 		set_bit(ICE_SERVICE_DIS, pf->state);
8296 		break;
8297 	}
8298 
8299 	ice_service_task_schedule(pf);
8300 	pf->tx_timeout_recovery_level++;
8301 }
8302 
8303 /**
8304  * ice_setup_tc_cls_flower - flower classifier offloads
8305  * @np: net device to configure
8306  * @filter_dev: device on which filter is added
8307  * @cls_flower: offload data
8308  */
8309 static int
8310 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8311 			struct net_device *filter_dev,
8312 			struct flow_cls_offload *cls_flower)
8313 {
8314 	struct ice_vsi *vsi = np->vsi;
8315 
8316 	if (cls_flower->common.chain_index)
8317 		return -EOPNOTSUPP;
8318 
8319 	switch (cls_flower->command) {
8320 	case FLOW_CLS_REPLACE:
8321 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
8322 	case FLOW_CLS_DESTROY:
8323 		return ice_del_cls_flower(vsi, cls_flower);
8324 	default:
8325 		return -EINVAL;
8326 	}
8327 }
8328 
8329 /**
8330  * ice_setup_tc_block_cb - callback handler registered for TC block
8331  * @type: TC SETUP type
8332  * @type_data: TC flower offload data that contains user input
8333  * @cb_priv: netdev private data
8334  */
8335 static int
8336 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8337 {
8338 	struct ice_netdev_priv *np = cb_priv;
8339 
8340 	switch (type) {
8341 	case TC_SETUP_CLSFLOWER:
8342 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8343 					       type_data);
8344 	default:
8345 		return -EOPNOTSUPP;
8346 	}
8347 }
8348 
8349 /**
8350  * ice_validate_mqprio_qopt - Validate TCF input parameters
8351  * @vsi: Pointer to VSI
8352  * @mqprio_qopt: input parameters for mqprio queue configuration
8353  *
8354  * This function validates MQPRIO params, such as qcount (power of 2 wherever
8355  * needed), and make sure user doesn't specify qcount and BW rate limit
8356  * for TCs, which are more than "num_tc"
8357  */
8358 static int
8359 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8360 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
8361 {
8362 	int non_power_of_2_qcount = 0;
8363 	struct ice_pf *pf = vsi->back;
8364 	int max_rss_q_cnt = 0;
8365 	u64 sum_min_rate = 0;
8366 	struct device *dev;
8367 	int i, speed;
8368 	u8 num_tc;
8369 
8370 	if (vsi->type != ICE_VSI_PF)
8371 		return -EINVAL;
8372 
8373 	if (mqprio_qopt->qopt.offset[0] != 0 ||
8374 	    mqprio_qopt->qopt.num_tc < 1 ||
8375 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8376 		return -EINVAL;
8377 
8378 	dev = ice_pf_to_dev(pf);
8379 	vsi->ch_rss_size = 0;
8380 	num_tc = mqprio_qopt->qopt.num_tc;
8381 	speed = ice_get_link_speed_kbps(vsi);
8382 
8383 	for (i = 0; num_tc; i++) {
8384 		int qcount = mqprio_qopt->qopt.count[i];
8385 		u64 max_rate, min_rate, rem;
8386 
8387 		if (!qcount)
8388 			return -EINVAL;
8389 
8390 		if (is_power_of_2(qcount)) {
8391 			if (non_power_of_2_qcount &&
8392 			    qcount > non_power_of_2_qcount) {
8393 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8394 					qcount, non_power_of_2_qcount);
8395 				return -EINVAL;
8396 			}
8397 			if (qcount > max_rss_q_cnt)
8398 				max_rss_q_cnt = qcount;
8399 		} else {
8400 			if (non_power_of_2_qcount &&
8401 			    qcount != non_power_of_2_qcount) {
8402 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8403 					qcount, non_power_of_2_qcount);
8404 				return -EINVAL;
8405 			}
8406 			if (qcount < max_rss_q_cnt) {
8407 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8408 					qcount, max_rss_q_cnt);
8409 				return -EINVAL;
8410 			}
8411 			max_rss_q_cnt = qcount;
8412 			non_power_of_2_qcount = qcount;
8413 		}
8414 
8415 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8416 		 * converts the bandwidth rate limit into Bytes/s when
8417 		 * passing it down to the driver. So convert input bandwidth
8418 		 * from Bytes/s to Kbps
8419 		 */
8420 		max_rate = mqprio_qopt->max_rate[i];
8421 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8422 
8423 		/* min_rate is minimum guaranteed rate and it can't be zero */
8424 		min_rate = mqprio_qopt->min_rate[i];
8425 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8426 		sum_min_rate += min_rate;
8427 
8428 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8429 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8430 				min_rate, ICE_MIN_BW_LIMIT);
8431 			return -EINVAL;
8432 		}
8433 
8434 		if (max_rate && max_rate > speed) {
8435 			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8436 				i, max_rate, speed);
8437 			return -EINVAL;
8438 		}
8439 
8440 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8441 		if (rem) {
8442 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8443 				i, ICE_MIN_BW_LIMIT);
8444 			return -EINVAL;
8445 		}
8446 
8447 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8448 		if (rem) {
8449 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8450 				i, ICE_MIN_BW_LIMIT);
8451 			return -EINVAL;
8452 		}
8453 
8454 		/* min_rate can't be more than max_rate, except when max_rate
8455 		 * is zero (implies max_rate sought is max line rate). In such
8456 		 * a case min_rate can be more than max.
8457 		 */
8458 		if (max_rate && min_rate > max_rate) {
8459 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8460 				min_rate, max_rate);
8461 			return -EINVAL;
8462 		}
8463 
8464 		if (i >= mqprio_qopt->qopt.num_tc - 1)
8465 			break;
8466 		if (mqprio_qopt->qopt.offset[i + 1] !=
8467 		    (mqprio_qopt->qopt.offset[i] + qcount))
8468 			return -EINVAL;
8469 	}
8470 	if (vsi->num_rxq <
8471 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8472 		return -EINVAL;
8473 	if (vsi->num_txq <
8474 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8475 		return -EINVAL;
8476 
8477 	if (sum_min_rate && sum_min_rate > (u64)speed) {
8478 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8479 			sum_min_rate, speed);
8480 		return -EINVAL;
8481 	}
8482 
8483 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8484 	vsi->ch_rss_size = max_rss_q_cnt;
8485 
8486 	return 0;
8487 }
8488 
8489 /**
8490  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8491  * @pf: ptr to PF device
8492  * @vsi: ptr to VSI
8493  */
8494 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8495 {
8496 	struct device *dev = ice_pf_to_dev(pf);
8497 	bool added = false;
8498 	struct ice_hw *hw;
8499 	int flow;
8500 
8501 	if (!(vsi->num_gfltr || vsi->num_bfltr))
8502 		return -EINVAL;
8503 
8504 	hw = &pf->hw;
8505 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8506 		struct ice_fd_hw_prof *prof;
8507 		int tun, status;
8508 		u64 entry_h;
8509 
8510 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8511 		      hw->fdir_prof[flow]->cnt))
8512 			continue;
8513 
8514 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8515 			enum ice_flow_priority prio;
8516 
8517 			/* add this VSI to FDir profile for this flow */
8518 			prio = ICE_FLOW_PRIO_NORMAL;
8519 			prof = hw->fdir_prof[flow];
8520 			status = ice_flow_add_entry(hw, ICE_BLK_FD,
8521 						    prof->prof_id[tun],
8522 						    prof->vsi_h[0], vsi->idx,
8523 						    prio, prof->fdir_seg[tun],
8524 						    &entry_h);
8525 			if (status) {
8526 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8527 					vsi->idx, flow);
8528 				continue;
8529 			}
8530 
8531 			prof->entry_h[prof->cnt][tun] = entry_h;
8532 		}
8533 
8534 		/* store VSI for filter replay and delete */
8535 		prof->vsi_h[prof->cnt] = vsi->idx;
8536 		prof->cnt++;
8537 
8538 		added = true;
8539 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8540 			flow);
8541 	}
8542 
8543 	if (!added)
8544 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8545 
8546 	return 0;
8547 }
8548 
8549 /**
8550  * ice_add_channel - add a channel by adding VSI
8551  * @pf: ptr to PF device
8552  * @sw_id: underlying HW switching element ID
8553  * @ch: ptr to channel structure
8554  *
8555  * Add a channel (VSI) using add_vsi and queue_map
8556  */
8557 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8558 {
8559 	struct device *dev = ice_pf_to_dev(pf);
8560 	struct ice_vsi *vsi;
8561 
8562 	if (ch->type != ICE_VSI_CHNL) {
8563 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8564 		return -EINVAL;
8565 	}
8566 
8567 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8568 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8569 		dev_err(dev, "create chnl VSI failure\n");
8570 		return -EINVAL;
8571 	}
8572 
8573 	ice_add_vsi_to_fdir(pf, vsi);
8574 
8575 	ch->sw_id = sw_id;
8576 	ch->vsi_num = vsi->vsi_num;
8577 	ch->info.mapping_flags = vsi->info.mapping_flags;
8578 	ch->ch_vsi = vsi;
8579 	/* set the back pointer of channel for newly created VSI */
8580 	vsi->ch = ch;
8581 
8582 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8583 	       sizeof(vsi->info.q_mapping));
8584 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8585 	       sizeof(vsi->info.tc_mapping));
8586 
8587 	return 0;
8588 }
8589 
8590 /**
8591  * ice_chnl_cfg_res
8592  * @vsi: the VSI being setup
8593  * @ch: ptr to channel structure
8594  *
8595  * Configure channel specific resources such as rings, vector.
8596  */
8597 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8598 {
8599 	int i;
8600 
8601 	for (i = 0; i < ch->num_txq; i++) {
8602 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8603 		struct ice_ring_container *rc;
8604 		struct ice_tx_ring *tx_ring;
8605 		struct ice_rx_ring *rx_ring;
8606 
8607 		tx_ring = vsi->tx_rings[ch->base_q + i];
8608 		rx_ring = vsi->rx_rings[ch->base_q + i];
8609 		if (!tx_ring || !rx_ring)
8610 			continue;
8611 
8612 		/* setup ring being channel enabled */
8613 		tx_ring->ch = ch;
8614 		rx_ring->ch = ch;
8615 
8616 		/* following code block sets up vector specific attributes */
8617 		tx_q_vector = tx_ring->q_vector;
8618 		rx_q_vector = rx_ring->q_vector;
8619 		if (!tx_q_vector && !rx_q_vector)
8620 			continue;
8621 
8622 		if (tx_q_vector) {
8623 			tx_q_vector->ch = ch;
8624 			/* setup Tx and Rx ITR setting if DIM is off */
8625 			rc = &tx_q_vector->tx;
8626 			if (!ITR_IS_DYNAMIC(rc))
8627 				ice_write_itr(rc, rc->itr_setting);
8628 		}
8629 		if (rx_q_vector) {
8630 			rx_q_vector->ch = ch;
8631 			/* setup Tx and Rx ITR setting if DIM is off */
8632 			rc = &rx_q_vector->rx;
8633 			if (!ITR_IS_DYNAMIC(rc))
8634 				ice_write_itr(rc, rc->itr_setting);
8635 		}
8636 	}
8637 
8638 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8639 	 * GLINT_ITR register would have written to perform in-context
8640 	 * update, hence perform flush
8641 	 */
8642 	if (ch->num_txq || ch->num_rxq)
8643 		ice_flush(&vsi->back->hw);
8644 }
8645 
8646 /**
8647  * ice_cfg_chnl_all_res - configure channel resources
8648  * @vsi: pte to main_vsi
8649  * @ch: ptr to channel structure
8650  *
8651  * This function configures channel specific resources such as flow-director
8652  * counter index, and other resources such as queues, vectors, ITR settings
8653  */
8654 static void
8655 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8656 {
8657 	/* configure channel (aka ADQ) resources such as queues, vectors,
8658 	 * ITR settings for channel specific vectors and anything else
8659 	 */
8660 	ice_chnl_cfg_res(vsi, ch);
8661 }
8662 
8663 /**
8664  * ice_setup_hw_channel - setup new channel
8665  * @pf: ptr to PF device
8666  * @vsi: the VSI being setup
8667  * @ch: ptr to channel structure
8668  * @sw_id: underlying HW switching element ID
8669  * @type: type of channel to be created (VMDq2/VF)
8670  *
8671  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8672  * and configures Tx rings accordingly
8673  */
8674 static int
8675 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8676 		     struct ice_channel *ch, u16 sw_id, u8 type)
8677 {
8678 	struct device *dev = ice_pf_to_dev(pf);
8679 	int ret;
8680 
8681 	ch->base_q = vsi->next_base_q;
8682 	ch->type = type;
8683 
8684 	ret = ice_add_channel(pf, sw_id, ch);
8685 	if (ret) {
8686 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8687 		return ret;
8688 	}
8689 
8690 	/* configure/setup ADQ specific resources */
8691 	ice_cfg_chnl_all_res(vsi, ch);
8692 
8693 	/* make sure to update the next_base_q so that subsequent channel's
8694 	 * (aka ADQ) VSI queue map is correct
8695 	 */
8696 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8697 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8698 		ch->num_rxq);
8699 
8700 	return 0;
8701 }
8702 
8703 /**
8704  * ice_setup_channel - setup new channel using uplink element
8705  * @pf: ptr to PF device
8706  * @vsi: the VSI being setup
8707  * @ch: ptr to channel structure
8708  *
8709  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8710  * and uplink switching element
8711  */
8712 static bool
8713 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8714 		  struct ice_channel *ch)
8715 {
8716 	struct device *dev = ice_pf_to_dev(pf);
8717 	u16 sw_id;
8718 	int ret;
8719 
8720 	if (vsi->type != ICE_VSI_PF) {
8721 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8722 		return false;
8723 	}
8724 
8725 	sw_id = pf->first_sw->sw_id;
8726 
8727 	/* create channel (VSI) */
8728 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8729 	if (ret) {
8730 		dev_err(dev, "failed to setup hw_channel\n");
8731 		return false;
8732 	}
8733 	dev_dbg(dev, "successfully created channel()\n");
8734 
8735 	return ch->ch_vsi ? true : false;
8736 }
8737 
8738 /**
8739  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8740  * @vsi: VSI to be configured
8741  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8742  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8743  */
8744 static int
8745 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8746 {
8747 	int err;
8748 
8749 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8750 	if (err)
8751 		return err;
8752 
8753 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8754 }
8755 
8756 /**
8757  * ice_create_q_channel - function to create channel
8758  * @vsi: VSI to be configured
8759  * @ch: ptr to channel (it contains channel specific params)
8760  *
8761  * This function creates channel (VSI) using num_queues specified by user,
8762  * reconfigs RSS if needed.
8763  */
8764 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8765 {
8766 	struct ice_pf *pf = vsi->back;
8767 	struct device *dev;
8768 
8769 	if (!ch)
8770 		return -EINVAL;
8771 
8772 	dev = ice_pf_to_dev(pf);
8773 	if (!ch->num_txq || !ch->num_rxq) {
8774 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8775 		return -EINVAL;
8776 	}
8777 
8778 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8779 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8780 			vsi->cnt_q_avail, ch->num_txq);
8781 		return -EINVAL;
8782 	}
8783 
8784 	if (!ice_setup_channel(pf, vsi, ch)) {
8785 		dev_info(dev, "Failed to setup channel\n");
8786 		return -EINVAL;
8787 	}
8788 	/* configure BW rate limit */
8789 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8790 		int ret;
8791 
8792 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8793 				       ch->min_tx_rate);
8794 		if (ret)
8795 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8796 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8797 		else
8798 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8799 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8800 	}
8801 
8802 	vsi->cnt_q_avail -= ch->num_txq;
8803 
8804 	return 0;
8805 }
8806 
8807 /**
8808  * ice_rem_all_chnl_fltrs - removes all channel filters
8809  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8810  *
8811  * Remove all advanced switch filters only if they are channel specific
8812  * tc-flower based filter
8813  */
8814 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8815 {
8816 	struct ice_tc_flower_fltr *fltr;
8817 	struct hlist_node *node;
8818 
8819 	/* to remove all channel filters, iterate an ordered list of filters */
8820 	hlist_for_each_entry_safe(fltr, node,
8821 				  &pf->tc_flower_fltr_list,
8822 				  tc_flower_node) {
8823 		struct ice_rule_query_data rule;
8824 		int status;
8825 
8826 		/* for now process only channel specific filters */
8827 		if (!ice_is_chnl_fltr(fltr))
8828 			continue;
8829 
8830 		rule.rid = fltr->rid;
8831 		rule.rule_id = fltr->rule_id;
8832 		rule.vsi_handle = fltr->dest_vsi_handle;
8833 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8834 		if (status) {
8835 			if (status == -ENOENT)
8836 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8837 					rule.rule_id);
8838 			else
8839 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8840 					status);
8841 		} else if (fltr->dest_vsi) {
8842 			/* update advanced switch filter count */
8843 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8844 				u32 flags = fltr->flags;
8845 
8846 				fltr->dest_vsi->num_chnl_fltr--;
8847 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8848 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8849 					pf->num_dmac_chnl_fltrs--;
8850 			}
8851 		}
8852 
8853 		hlist_del(&fltr->tc_flower_node);
8854 		kfree(fltr);
8855 	}
8856 }
8857 
8858 /**
8859  * ice_remove_q_channels - Remove queue channels for the TCs
8860  * @vsi: VSI to be configured
8861  * @rem_fltr: delete advanced switch filter or not
8862  *
8863  * Remove queue channels for the TCs
8864  */
8865 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8866 {
8867 	struct ice_channel *ch, *ch_tmp;
8868 	struct ice_pf *pf = vsi->back;
8869 	int i;
8870 
8871 	/* remove all tc-flower based filter if they are channel filters only */
8872 	if (rem_fltr)
8873 		ice_rem_all_chnl_fltrs(pf);
8874 
8875 	/* remove ntuple filters since queue configuration is being changed */
8876 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8877 		struct ice_hw *hw = &pf->hw;
8878 
8879 		mutex_lock(&hw->fdir_fltr_lock);
8880 		ice_fdir_del_all_fltrs(vsi);
8881 		mutex_unlock(&hw->fdir_fltr_lock);
8882 	}
8883 
8884 	/* perform cleanup for channels if they exist */
8885 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8886 		struct ice_vsi *ch_vsi;
8887 
8888 		list_del(&ch->list);
8889 		ch_vsi = ch->ch_vsi;
8890 		if (!ch_vsi) {
8891 			kfree(ch);
8892 			continue;
8893 		}
8894 
8895 		/* Reset queue contexts */
8896 		for (i = 0; i < ch->num_rxq; i++) {
8897 			struct ice_tx_ring *tx_ring;
8898 			struct ice_rx_ring *rx_ring;
8899 
8900 			tx_ring = vsi->tx_rings[ch->base_q + i];
8901 			rx_ring = vsi->rx_rings[ch->base_q + i];
8902 			if (tx_ring) {
8903 				tx_ring->ch = NULL;
8904 				if (tx_ring->q_vector)
8905 					tx_ring->q_vector->ch = NULL;
8906 			}
8907 			if (rx_ring) {
8908 				rx_ring->ch = NULL;
8909 				if (rx_ring->q_vector)
8910 					rx_ring->q_vector->ch = NULL;
8911 			}
8912 		}
8913 
8914 		/* Release FD resources for the channel VSI */
8915 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8916 
8917 		/* clear the VSI from scheduler tree */
8918 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8919 
8920 		/* Delete VSI from FW, PF and HW VSI arrays */
8921 		ice_vsi_delete(ch->ch_vsi);
8922 
8923 		/* free the channel */
8924 		kfree(ch);
8925 	}
8926 
8927 	/* clear the channel VSI map which is stored in main VSI */
8928 	ice_for_each_chnl_tc(i)
8929 		vsi->tc_map_vsi[i] = NULL;
8930 
8931 	/* reset main VSI's all TC information */
8932 	vsi->all_enatc = 0;
8933 	vsi->all_numtc = 0;
8934 }
8935 
8936 /**
8937  * ice_rebuild_channels - rebuild channel
8938  * @pf: ptr to PF
8939  *
8940  * Recreate channel VSIs and replay filters
8941  */
8942 static int ice_rebuild_channels(struct ice_pf *pf)
8943 {
8944 	struct device *dev = ice_pf_to_dev(pf);
8945 	struct ice_vsi *main_vsi;
8946 	bool rem_adv_fltr = true;
8947 	struct ice_channel *ch;
8948 	struct ice_vsi *vsi;
8949 	int tc_idx = 1;
8950 	int i, err;
8951 
8952 	main_vsi = ice_get_main_vsi(pf);
8953 	if (!main_vsi)
8954 		return 0;
8955 
8956 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8957 	    main_vsi->old_numtc == 1)
8958 		return 0; /* nothing to be done */
8959 
8960 	/* reconfigure main VSI based on old value of TC and cached values
8961 	 * for MQPRIO opts
8962 	 */
8963 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8964 	if (err) {
8965 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8966 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8967 		return err;
8968 	}
8969 
8970 	/* rebuild ADQ VSIs */
8971 	ice_for_each_vsi(pf, i) {
8972 		enum ice_vsi_type type;
8973 
8974 		vsi = pf->vsi[i];
8975 		if (!vsi || vsi->type != ICE_VSI_CHNL)
8976 			continue;
8977 
8978 		type = vsi->type;
8979 
8980 		/* rebuild ADQ VSI */
8981 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8982 		if (err) {
8983 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8984 				ice_vsi_type_str(type), vsi->idx, err);
8985 			goto cleanup;
8986 		}
8987 
8988 		/* Re-map HW VSI number, using VSI handle that has been
8989 		 * previously validated in ice_replay_vsi() call above
8990 		 */
8991 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8992 
8993 		/* replay filters for the VSI */
8994 		err = ice_replay_vsi(&pf->hw, vsi->idx);
8995 		if (err) {
8996 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8997 				ice_vsi_type_str(type), err, vsi->idx);
8998 			rem_adv_fltr = false;
8999 			goto cleanup;
9000 		}
9001 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
9002 			 ice_vsi_type_str(type), vsi->idx);
9003 
9004 		/* store ADQ VSI at correct TC index in main VSI's
9005 		 * map of TC to VSI
9006 		 */
9007 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
9008 	}
9009 
9010 	/* ADQ VSI(s) has been rebuilt successfully, so setup
9011 	 * channel for main VSI's Tx and Rx rings
9012 	 */
9013 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
9014 		struct ice_vsi *ch_vsi;
9015 
9016 		ch_vsi = ch->ch_vsi;
9017 		if (!ch_vsi)
9018 			continue;
9019 
9020 		/* reconfig channel resources */
9021 		ice_cfg_chnl_all_res(main_vsi, ch);
9022 
9023 		/* replay BW rate limit if it is non-zero */
9024 		if (!ch->max_tx_rate && !ch->min_tx_rate)
9025 			continue;
9026 
9027 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
9028 				       ch->min_tx_rate);
9029 		if (err)
9030 			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",
9031 				err, ch->max_tx_rate, ch->min_tx_rate,
9032 				ch_vsi->vsi_num);
9033 		else
9034 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9035 				ch->max_tx_rate, ch->min_tx_rate,
9036 				ch_vsi->vsi_num);
9037 	}
9038 
9039 	/* reconfig RSS for main VSI */
9040 	if (main_vsi->ch_rss_size)
9041 		ice_vsi_cfg_rss_lut_key(main_vsi);
9042 
9043 	return 0;
9044 
9045 cleanup:
9046 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
9047 	return err;
9048 }
9049 
9050 /**
9051  * ice_create_q_channels - Add queue channel for the given TCs
9052  * @vsi: VSI to be configured
9053  *
9054  * Configures queue channel mapping to the given TCs
9055  */
9056 static int ice_create_q_channels(struct ice_vsi *vsi)
9057 {
9058 	struct ice_pf *pf = vsi->back;
9059 	struct ice_channel *ch;
9060 	int ret = 0, i;
9061 
9062 	ice_for_each_chnl_tc(i) {
9063 		if (!(vsi->all_enatc & BIT(i)))
9064 			continue;
9065 
9066 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
9067 		if (!ch) {
9068 			ret = -ENOMEM;
9069 			goto err_free;
9070 		}
9071 		INIT_LIST_HEAD(&ch->list);
9072 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
9073 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
9074 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
9075 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
9076 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
9077 
9078 		/* convert to Kbits/s */
9079 		if (ch->max_tx_rate)
9080 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
9081 						  ICE_BW_KBPS_DIVISOR);
9082 		if (ch->min_tx_rate)
9083 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
9084 						  ICE_BW_KBPS_DIVISOR);
9085 
9086 		ret = ice_create_q_channel(vsi, ch);
9087 		if (ret) {
9088 			dev_err(ice_pf_to_dev(pf),
9089 				"failed creating channel TC:%d\n", i);
9090 			kfree(ch);
9091 			goto err_free;
9092 		}
9093 		list_add_tail(&ch->list, &vsi->ch_list);
9094 		vsi->tc_map_vsi[i] = ch->ch_vsi;
9095 		dev_dbg(ice_pf_to_dev(pf),
9096 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
9097 	}
9098 	return 0;
9099 
9100 err_free:
9101 	ice_remove_q_channels(vsi, false);
9102 
9103 	return ret;
9104 }
9105 
9106 /**
9107  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
9108  * @netdev: net device to configure
9109  * @type_data: TC offload data
9110  */
9111 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
9112 {
9113 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
9114 	struct ice_netdev_priv *np = netdev_priv(netdev);
9115 	struct ice_vsi *vsi = np->vsi;
9116 	struct ice_pf *pf = vsi->back;
9117 	u16 mode, ena_tc_qdisc = 0;
9118 	int cur_txq, cur_rxq;
9119 	u8 hw = 0, num_tcf;
9120 	struct device *dev;
9121 	int ret, i;
9122 
9123 	dev = ice_pf_to_dev(pf);
9124 	num_tcf = mqprio_qopt->qopt.num_tc;
9125 	hw = mqprio_qopt->qopt.hw;
9126 	mode = mqprio_qopt->mode;
9127 	if (!hw) {
9128 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9129 		vsi->ch_rss_size = 0;
9130 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9131 		goto config_tcf;
9132 	}
9133 
9134 	/* Generate queue region map for number of TCF requested */
9135 	for (i = 0; i < num_tcf; i++)
9136 		ena_tc_qdisc |= BIT(i);
9137 
9138 	switch (mode) {
9139 	case TC_MQPRIO_MODE_CHANNEL:
9140 
9141 		if (pf->hw.port_info->is_custom_tx_enabled) {
9142 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
9143 			return -EBUSY;
9144 		}
9145 		ice_tear_down_devlink_rate_tree(pf);
9146 
9147 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
9148 		if (ret) {
9149 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
9150 				   ret);
9151 			return ret;
9152 		}
9153 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9154 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9155 		/* don't assume state of hw_tc_offload during driver load
9156 		 * and set the flag for TC flower filter if hw_tc_offload
9157 		 * already ON
9158 		 */
9159 		if (vsi->netdev->features & NETIF_F_HW_TC)
9160 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
9161 		break;
9162 	default:
9163 		return -EINVAL;
9164 	}
9165 
9166 config_tcf:
9167 
9168 	/* Requesting same TCF configuration as already enabled */
9169 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9170 	    mode != TC_MQPRIO_MODE_CHANNEL)
9171 		return 0;
9172 
9173 	/* Pause VSI queues */
9174 	ice_dis_vsi(vsi, true);
9175 
9176 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9177 		ice_remove_q_channels(vsi, true);
9178 
9179 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9180 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9181 				     num_online_cpus());
9182 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9183 				     num_online_cpus());
9184 	} else {
9185 		/* logic to rebuild VSI, same like ethtool -L */
9186 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9187 
9188 		for (i = 0; i < num_tcf; i++) {
9189 			if (!(ena_tc_qdisc & BIT(i)))
9190 				continue;
9191 
9192 			offset = vsi->mqprio_qopt.qopt.offset[i];
9193 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9194 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9195 		}
9196 		vsi->req_txq = offset + qcount_tx;
9197 		vsi->req_rxq = offset + qcount_rx;
9198 
9199 		/* store away original rss_size info, so that it gets reused
9200 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9201 		 * determine, what should be the rss_sizefor main VSI
9202 		 */
9203 		vsi->orig_rss_size = vsi->rss_size;
9204 	}
9205 
9206 	/* save current values of Tx and Rx queues before calling VSI rebuild
9207 	 * for fallback option
9208 	 */
9209 	cur_txq = vsi->num_txq;
9210 	cur_rxq = vsi->num_rxq;
9211 
9212 	/* proceed with rebuild main VSI using correct number of queues */
9213 	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
9214 	if (ret) {
9215 		/* fallback to current number of queues */
9216 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9217 		vsi->req_txq = cur_txq;
9218 		vsi->req_rxq = cur_rxq;
9219 		clear_bit(ICE_RESET_FAILED, pf->state);
9220 		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
9221 			dev_err(dev, "Rebuild of main VSI failed again\n");
9222 			return ret;
9223 		}
9224 	}
9225 
9226 	vsi->all_numtc = num_tcf;
9227 	vsi->all_enatc = ena_tc_qdisc;
9228 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9229 	if (ret) {
9230 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9231 			   vsi->vsi_num);
9232 		goto exit;
9233 	}
9234 
9235 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9236 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9237 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9238 
9239 		/* set TC0 rate limit if specified */
9240 		if (max_tx_rate || min_tx_rate) {
9241 			/* convert to Kbits/s */
9242 			if (max_tx_rate)
9243 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9244 			if (min_tx_rate)
9245 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9246 
9247 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9248 			if (!ret) {
9249 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9250 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9251 			} else {
9252 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9253 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9254 				goto exit;
9255 			}
9256 		}
9257 		ret = ice_create_q_channels(vsi);
9258 		if (ret) {
9259 			netdev_err(netdev, "failed configuring queue channels\n");
9260 			goto exit;
9261 		} else {
9262 			netdev_dbg(netdev, "successfully configured channels\n");
9263 		}
9264 	}
9265 
9266 	if (vsi->ch_rss_size)
9267 		ice_vsi_cfg_rss_lut_key(vsi);
9268 
9269 exit:
9270 	/* if error, reset the all_numtc and all_enatc */
9271 	if (ret) {
9272 		vsi->all_numtc = 0;
9273 		vsi->all_enatc = 0;
9274 	}
9275 	/* resume VSI */
9276 	ice_ena_vsi(vsi, true);
9277 
9278 	return ret;
9279 }
9280 
9281 static LIST_HEAD(ice_block_cb_list);
9282 
9283 static int
9284 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9285 	     void *type_data)
9286 {
9287 	struct ice_netdev_priv *np = netdev_priv(netdev);
9288 	struct ice_pf *pf = np->vsi->back;
9289 	bool locked = false;
9290 	int err;
9291 
9292 	switch (type) {
9293 	case TC_SETUP_BLOCK:
9294 		return flow_block_cb_setup_simple(type_data,
9295 						  &ice_block_cb_list,
9296 						  ice_setup_tc_block_cb,
9297 						  np, np, true);
9298 	case TC_SETUP_QDISC_MQPRIO:
9299 		if (ice_is_eswitch_mode_switchdev(pf)) {
9300 			netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9301 			return -EOPNOTSUPP;
9302 		}
9303 
9304 		if (pf->adev) {
9305 			mutex_lock(&pf->adev_mutex);
9306 			device_lock(&pf->adev->dev);
9307 			locked = true;
9308 			if (pf->adev->dev.driver) {
9309 				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9310 				err = -EBUSY;
9311 				goto adev_unlock;
9312 			}
9313 		}
9314 
9315 		/* setup traffic classifier for receive side */
9316 		mutex_lock(&pf->tc_mutex);
9317 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9318 		mutex_unlock(&pf->tc_mutex);
9319 
9320 adev_unlock:
9321 		if (locked) {
9322 			device_unlock(&pf->adev->dev);
9323 			mutex_unlock(&pf->adev_mutex);
9324 		}
9325 		return err;
9326 	default:
9327 		return -EOPNOTSUPP;
9328 	}
9329 	return -EOPNOTSUPP;
9330 }
9331 
9332 static struct ice_indr_block_priv *
9333 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9334 			   struct net_device *netdev)
9335 {
9336 	struct ice_indr_block_priv *cb_priv;
9337 
9338 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9339 		if (!cb_priv->netdev)
9340 			return NULL;
9341 		if (cb_priv->netdev == netdev)
9342 			return cb_priv;
9343 	}
9344 	return NULL;
9345 }
9346 
9347 static int
9348 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9349 			void *indr_priv)
9350 {
9351 	struct ice_indr_block_priv *priv = indr_priv;
9352 	struct ice_netdev_priv *np = priv->np;
9353 
9354 	switch (type) {
9355 	case TC_SETUP_CLSFLOWER:
9356 		return ice_setup_tc_cls_flower(np, priv->netdev,
9357 					       (struct flow_cls_offload *)
9358 					       type_data);
9359 	default:
9360 		return -EOPNOTSUPP;
9361 	}
9362 }
9363 
9364 static int
9365 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9366 			struct ice_netdev_priv *np,
9367 			struct flow_block_offload *f, void *data,
9368 			void (*cleanup)(struct flow_block_cb *block_cb))
9369 {
9370 	struct ice_indr_block_priv *indr_priv;
9371 	struct flow_block_cb *block_cb;
9372 
9373 	if (!ice_is_tunnel_supported(netdev) &&
9374 	    !(is_vlan_dev(netdev) &&
9375 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
9376 		return -EOPNOTSUPP;
9377 
9378 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9379 		return -EOPNOTSUPP;
9380 
9381 	switch (f->command) {
9382 	case FLOW_BLOCK_BIND:
9383 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9384 		if (indr_priv)
9385 			return -EEXIST;
9386 
9387 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9388 		if (!indr_priv)
9389 			return -ENOMEM;
9390 
9391 		indr_priv->netdev = netdev;
9392 		indr_priv->np = np;
9393 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9394 
9395 		block_cb =
9396 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9397 						 indr_priv, indr_priv,
9398 						 ice_rep_indr_tc_block_unbind,
9399 						 f, netdev, sch, data, np,
9400 						 cleanup);
9401 
9402 		if (IS_ERR(block_cb)) {
9403 			list_del(&indr_priv->list);
9404 			kfree(indr_priv);
9405 			return PTR_ERR(block_cb);
9406 		}
9407 		flow_block_cb_add(block_cb, f);
9408 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9409 		break;
9410 	case FLOW_BLOCK_UNBIND:
9411 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9412 		if (!indr_priv)
9413 			return -ENOENT;
9414 
9415 		block_cb = flow_block_cb_lookup(f->block,
9416 						ice_indr_setup_block_cb,
9417 						indr_priv);
9418 		if (!block_cb)
9419 			return -ENOENT;
9420 
9421 		flow_indr_block_cb_remove(block_cb, f);
9422 
9423 		list_del(&block_cb->driver_list);
9424 		break;
9425 	default:
9426 		return -EOPNOTSUPP;
9427 	}
9428 	return 0;
9429 }
9430 
9431 static int
9432 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9433 		     void *cb_priv, enum tc_setup_type type, void *type_data,
9434 		     void *data,
9435 		     void (*cleanup)(struct flow_block_cb *block_cb))
9436 {
9437 	switch (type) {
9438 	case TC_SETUP_BLOCK:
9439 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9440 					       data, cleanup);
9441 
9442 	default:
9443 		return -EOPNOTSUPP;
9444 	}
9445 }
9446 
9447 /**
9448  * ice_open - Called when a network interface becomes active
9449  * @netdev: network interface device structure
9450  *
9451  * The open entry point is called when a network interface is made
9452  * active by the system (IFF_UP). At this point all resources needed
9453  * for transmit and receive operations are allocated, the interrupt
9454  * handler is registered with the OS, the netdev watchdog is enabled,
9455  * and the stack is notified that the interface is ready.
9456  *
9457  * Returns 0 on success, negative value on failure
9458  */
9459 int ice_open(struct net_device *netdev)
9460 {
9461 	struct ice_netdev_priv *np = netdev_priv(netdev);
9462 	struct ice_pf *pf = np->vsi->back;
9463 
9464 	if (ice_is_reset_in_progress(pf->state)) {
9465 		netdev_err(netdev, "can't open net device while reset is in progress");
9466 		return -EBUSY;
9467 	}
9468 
9469 	return ice_open_internal(netdev);
9470 }
9471 
9472 /**
9473  * ice_open_internal - Called when a network interface becomes active
9474  * @netdev: network interface device structure
9475  *
9476  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9477  * handling routine
9478  *
9479  * Returns 0 on success, negative value on failure
9480  */
9481 int ice_open_internal(struct net_device *netdev)
9482 {
9483 	struct ice_netdev_priv *np = netdev_priv(netdev);
9484 	struct ice_vsi *vsi = np->vsi;
9485 	struct ice_pf *pf = vsi->back;
9486 	struct ice_port_info *pi;
9487 	int err;
9488 
9489 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9490 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9491 		return -EIO;
9492 	}
9493 
9494 	netif_carrier_off(netdev);
9495 
9496 	pi = vsi->port_info;
9497 	err = ice_update_link_info(pi);
9498 	if (err) {
9499 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9500 		return err;
9501 	}
9502 
9503 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9504 
9505 	/* Set PHY if there is media, otherwise, turn off PHY */
9506 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9507 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9508 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9509 			err = ice_init_phy_user_cfg(pi);
9510 			if (err) {
9511 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9512 					   err);
9513 				return err;
9514 			}
9515 		}
9516 
9517 		err = ice_configure_phy(vsi);
9518 		if (err) {
9519 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9520 				   err);
9521 			return err;
9522 		}
9523 	} else {
9524 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9525 		ice_set_link(vsi, false);
9526 	}
9527 
9528 	err = ice_vsi_open(vsi);
9529 	if (err)
9530 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9531 			   vsi->vsi_num, vsi->vsw->sw_id);
9532 
9533 	/* Update existing tunnels information */
9534 	udp_tunnel_get_rx_info(netdev);
9535 
9536 	return err;
9537 }
9538 
9539 /**
9540  * ice_stop - Disables a network interface
9541  * @netdev: network interface device structure
9542  *
9543  * The stop entry point is called when an interface is de-activated by the OS,
9544  * and the netdevice enters the DOWN state. The hardware is still under the
9545  * driver's control, but the netdev interface is disabled.
9546  *
9547  * Returns success only - not allowed to fail
9548  */
9549 int ice_stop(struct net_device *netdev)
9550 {
9551 	struct ice_netdev_priv *np = netdev_priv(netdev);
9552 	struct ice_vsi *vsi = np->vsi;
9553 	struct ice_pf *pf = vsi->back;
9554 
9555 	if (ice_is_reset_in_progress(pf->state)) {
9556 		netdev_err(netdev, "can't stop net device while reset is in progress");
9557 		return -EBUSY;
9558 	}
9559 
9560 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9561 		int link_err = ice_force_phys_link_state(vsi, false);
9562 
9563 		if (link_err) {
9564 			if (link_err == -ENOMEDIUM)
9565 				netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9566 					    vsi->vsi_num);
9567 			else
9568 				netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9569 					   vsi->vsi_num, link_err);
9570 
9571 			ice_vsi_close(vsi);
9572 			return -EIO;
9573 		}
9574 	}
9575 
9576 	ice_vsi_close(vsi);
9577 
9578 	return 0;
9579 }
9580 
9581 /**
9582  * ice_features_check - Validate encapsulated packet conforms to limits
9583  * @skb: skb buffer
9584  * @netdev: This port's netdev
9585  * @features: Offload features that the stack believes apply
9586  */
9587 static netdev_features_t
9588 ice_features_check(struct sk_buff *skb,
9589 		   struct net_device __always_unused *netdev,
9590 		   netdev_features_t features)
9591 {
9592 	bool gso = skb_is_gso(skb);
9593 	size_t len;
9594 
9595 	/* No point in doing any of this if neither checksum nor GSO are
9596 	 * being requested for this frame. We can rule out both by just
9597 	 * checking for CHECKSUM_PARTIAL
9598 	 */
9599 	if (skb->ip_summed != CHECKSUM_PARTIAL)
9600 		return features;
9601 
9602 	/* We cannot support GSO if the MSS is going to be less than
9603 	 * 64 bytes. If it is then we need to drop support for GSO.
9604 	 */
9605 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9606 		features &= ~NETIF_F_GSO_MASK;
9607 
9608 	len = skb_network_offset(skb);
9609 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9610 		goto out_rm_features;
9611 
9612 	len = skb_network_header_len(skb);
9613 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9614 		goto out_rm_features;
9615 
9616 	if (skb->encapsulation) {
9617 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9618 		 * the case of IPIP frames, the transport header pointer is
9619 		 * after the inner header! So check to make sure that this
9620 		 * is a GRE or UDP_TUNNEL frame before doing that math.
9621 		 */
9622 		if (gso && (skb_shinfo(skb)->gso_type &
9623 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9624 			len = skb_inner_network_header(skb) -
9625 			      skb_transport_header(skb);
9626 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9627 				goto out_rm_features;
9628 		}
9629 
9630 		len = skb_inner_network_header_len(skb);
9631 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9632 			goto out_rm_features;
9633 	}
9634 
9635 	return features;
9636 out_rm_features:
9637 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9638 }
9639 
9640 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9641 	.ndo_open = ice_open,
9642 	.ndo_stop = ice_stop,
9643 	.ndo_start_xmit = ice_start_xmit,
9644 	.ndo_set_mac_address = ice_set_mac_address,
9645 	.ndo_validate_addr = eth_validate_addr,
9646 	.ndo_change_mtu = ice_change_mtu,
9647 	.ndo_get_stats64 = ice_get_stats64,
9648 	.ndo_tx_timeout = ice_tx_timeout,
9649 	.ndo_bpf = ice_xdp_safe_mode,
9650 };
9651 
9652 static const struct net_device_ops ice_netdev_ops = {
9653 	.ndo_open = ice_open,
9654 	.ndo_stop = ice_stop,
9655 	.ndo_start_xmit = ice_start_xmit,
9656 	.ndo_select_queue = ice_select_queue,
9657 	.ndo_features_check = ice_features_check,
9658 	.ndo_fix_features = ice_fix_features,
9659 	.ndo_set_rx_mode = ice_set_rx_mode,
9660 	.ndo_set_mac_address = ice_set_mac_address,
9661 	.ndo_validate_addr = eth_validate_addr,
9662 	.ndo_change_mtu = ice_change_mtu,
9663 	.ndo_get_stats64 = ice_get_stats64,
9664 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9665 	.ndo_eth_ioctl = ice_eth_ioctl,
9666 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9667 	.ndo_set_vf_mac = ice_set_vf_mac,
9668 	.ndo_get_vf_config = ice_get_vf_cfg,
9669 	.ndo_set_vf_trust = ice_set_vf_trust,
9670 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9671 	.ndo_set_vf_link_state = ice_set_vf_link_state,
9672 	.ndo_get_vf_stats = ice_get_vf_stats,
9673 	.ndo_set_vf_rate = ice_set_vf_bw,
9674 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9675 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9676 	.ndo_setup_tc = ice_setup_tc,
9677 	.ndo_set_features = ice_set_features,
9678 	.ndo_bridge_getlink = ice_bridge_getlink,
9679 	.ndo_bridge_setlink = ice_bridge_setlink,
9680 	.ndo_fdb_add = ice_fdb_add,
9681 	.ndo_fdb_del = ice_fdb_del,
9682 #ifdef CONFIG_RFS_ACCEL
9683 	.ndo_rx_flow_steer = ice_rx_flow_steer,
9684 #endif
9685 	.ndo_tx_timeout = ice_tx_timeout,
9686 	.ndo_bpf = ice_xdp,
9687 	.ndo_xdp_xmit = ice_xdp_xmit,
9688 	.ndo_xsk_wakeup = ice_xsk_wakeup,
9689 };
9690