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