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