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