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