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