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