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