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