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