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