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