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