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