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