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