xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_main.c (revision 6486c0f44ed8e91073c1b08e83075e3832618ae5)
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 /**
4524  * ice_wait_for_fw - wait for full FW readiness
4525  * @hw: pointer to the hardware structure
4526  * @timeout: milliseconds that can elapse before timing out
4527  */
4528 static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4529 {
4530 	int fw_loading;
4531 	u32 elapsed = 0;
4532 
4533 	while (elapsed <= timeout) {
4534 		fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4535 
4536 		/* firmware was not yet loaded, we have to wait more */
4537 		if (fw_loading) {
4538 			elapsed += 100;
4539 			msleep(100);
4540 			continue;
4541 		}
4542 		return 0;
4543 	}
4544 
4545 	return -ETIMEDOUT;
4546 }
4547 
4548 static int ice_init_dev(struct ice_pf *pf)
4549 {
4550 	struct device *dev = ice_pf_to_dev(pf);
4551 	struct ice_hw *hw = &pf->hw;
4552 	int err;
4553 
4554 	err = ice_init_hw(hw);
4555 	if (err) {
4556 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4557 		return err;
4558 	}
4559 
4560 	/* Some cards require longer initialization times
4561 	 * due to necessity of loading FW from an external source.
4562 	 * This can take even half a minute.
4563 	 */
4564 	if (ice_is_pf_c827(hw)) {
4565 		err = ice_wait_for_fw(hw, 30000);
4566 		if (err) {
4567 			dev_err(dev, "ice_wait_for_fw timed out");
4568 			return err;
4569 		}
4570 	}
4571 
4572 	ice_init_feature_support(pf);
4573 
4574 	ice_request_fw(pf);
4575 
4576 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4577 	 * set in pf->state, which will cause ice_is_safe_mode to return
4578 	 * true
4579 	 */
4580 	if (ice_is_safe_mode(pf)) {
4581 		/* we already got function/device capabilities but these don't
4582 		 * reflect what the driver needs to do in safe mode. Instead of
4583 		 * adding conditional logic everywhere to ignore these
4584 		 * device/function capabilities, override them.
4585 		 */
4586 		ice_set_safe_mode_caps(hw);
4587 	}
4588 
4589 	err = ice_init_pf(pf);
4590 	if (err) {
4591 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4592 		goto err_init_pf;
4593 	}
4594 
4595 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4596 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4597 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4598 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4599 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4600 		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4601 			pf->hw.tnl.valid_count[TNL_VXLAN];
4602 		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4603 			UDP_TUNNEL_TYPE_VXLAN;
4604 	}
4605 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4606 		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4607 			pf->hw.tnl.valid_count[TNL_GENEVE];
4608 		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4609 			UDP_TUNNEL_TYPE_GENEVE;
4610 	}
4611 
4612 	err = ice_init_interrupt_scheme(pf);
4613 	if (err) {
4614 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4615 		err = -EIO;
4616 		goto err_init_interrupt_scheme;
4617 	}
4618 
4619 	/* In case of MSIX we are going to setup the misc vector right here
4620 	 * to handle admin queue events etc. In case of legacy and MSI
4621 	 * the misc functionality and queue processing is combined in
4622 	 * the same vector and that gets setup at open.
4623 	 */
4624 	err = ice_req_irq_msix_misc(pf);
4625 	if (err) {
4626 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4627 		goto err_req_irq_msix_misc;
4628 	}
4629 
4630 	return 0;
4631 
4632 err_req_irq_msix_misc:
4633 	ice_clear_interrupt_scheme(pf);
4634 err_init_interrupt_scheme:
4635 	ice_deinit_pf(pf);
4636 err_init_pf:
4637 	ice_deinit_hw(hw);
4638 	return err;
4639 }
4640 
4641 static void ice_deinit_dev(struct ice_pf *pf)
4642 {
4643 	ice_free_irq_msix_misc(pf);
4644 	ice_deinit_pf(pf);
4645 	ice_deinit_hw(&pf->hw);
4646 
4647 	/* Service task is already stopped, so call reset directly. */
4648 	ice_reset(&pf->hw, ICE_RESET_PFR);
4649 	pci_wait_for_pending_transaction(pf->pdev);
4650 	ice_clear_interrupt_scheme(pf);
4651 }
4652 
4653 static void ice_init_features(struct ice_pf *pf)
4654 {
4655 	struct device *dev = ice_pf_to_dev(pf);
4656 
4657 	if (ice_is_safe_mode(pf))
4658 		return;
4659 
4660 	/* initialize DDP driven features */
4661 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4662 		ice_ptp_init(pf);
4663 
4664 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4665 		ice_gnss_init(pf);
4666 
4667 	/* Note: Flow director init failure is non-fatal to load */
4668 	if (ice_init_fdir(pf))
4669 		dev_err(dev, "could not initialize flow director\n");
4670 
4671 	/* Note: DCB init failure is non-fatal to load */
4672 	if (ice_init_pf_dcb(pf, false)) {
4673 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4674 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4675 	} else {
4676 		ice_cfg_lldp_mib_change(&pf->hw, true);
4677 	}
4678 
4679 	if (ice_init_lag(pf))
4680 		dev_warn(dev, "Failed to init link aggregation support\n");
4681 }
4682 
4683 static void ice_deinit_features(struct ice_pf *pf)
4684 {
4685 	ice_deinit_lag(pf);
4686 	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4687 		ice_cfg_lldp_mib_change(&pf->hw, false);
4688 	ice_deinit_fdir(pf);
4689 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4690 		ice_gnss_exit(pf);
4691 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4692 		ice_ptp_release(pf);
4693 }
4694 
4695 static void ice_init_wakeup(struct ice_pf *pf)
4696 {
4697 	/* Save wakeup reason register for later use */
4698 	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4699 
4700 	/* check for a power management event */
4701 	ice_print_wake_reason(pf);
4702 
4703 	/* clear wake status, all bits */
4704 	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4705 
4706 	/* Disable WoL at init, wait for user to enable */
4707 	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4708 }
4709 
4710 static int ice_init_link(struct ice_pf *pf)
4711 {
4712 	struct device *dev = ice_pf_to_dev(pf);
4713 	int err;
4714 
4715 	err = ice_init_link_events(pf->hw.port_info);
4716 	if (err) {
4717 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4718 		return err;
4719 	}
4720 
4721 	/* not a fatal error if this fails */
4722 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4723 	if (err)
4724 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4725 
4726 	/* not a fatal error if this fails */
4727 	err = ice_update_link_info(pf->hw.port_info);
4728 	if (err)
4729 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4730 
4731 	ice_init_link_dflt_override(pf->hw.port_info);
4732 
4733 	ice_check_link_cfg_err(pf,
4734 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4735 
4736 	/* if media available, initialize PHY settings */
4737 	if (pf->hw.port_info->phy.link_info.link_info &
4738 	    ICE_AQ_MEDIA_AVAILABLE) {
4739 		/* not a fatal error if this fails */
4740 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4741 		if (err)
4742 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4743 
4744 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4745 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4746 
4747 			if (vsi)
4748 				ice_configure_phy(vsi);
4749 		}
4750 	} else {
4751 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4752 	}
4753 
4754 	return err;
4755 }
4756 
4757 static int ice_init_pf_sw(struct ice_pf *pf)
4758 {
4759 	bool dvm = ice_is_dvm_ena(&pf->hw);
4760 	struct ice_vsi *vsi;
4761 	int err;
4762 
4763 	/* create switch struct for the switch element created by FW on boot */
4764 	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4765 	if (!pf->first_sw)
4766 		return -ENOMEM;
4767 
4768 	if (pf->hw.evb_veb)
4769 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4770 	else
4771 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4772 
4773 	pf->first_sw->pf = pf;
4774 
4775 	/* record the sw_id available for later use */
4776 	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4777 
4778 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4779 	if (err)
4780 		goto err_aq_set_port_params;
4781 
4782 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4783 	if (!vsi) {
4784 		err = -ENOMEM;
4785 		goto err_pf_vsi_setup;
4786 	}
4787 
4788 	return 0;
4789 
4790 err_pf_vsi_setup:
4791 err_aq_set_port_params:
4792 	kfree(pf->first_sw);
4793 	return err;
4794 }
4795 
4796 static void ice_deinit_pf_sw(struct ice_pf *pf)
4797 {
4798 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4799 
4800 	if (!vsi)
4801 		return;
4802 
4803 	ice_vsi_release(vsi);
4804 	kfree(pf->first_sw);
4805 }
4806 
4807 static int ice_alloc_vsis(struct ice_pf *pf)
4808 {
4809 	struct device *dev = ice_pf_to_dev(pf);
4810 
4811 	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
4812 	if (!pf->num_alloc_vsi)
4813 		return -EIO;
4814 
4815 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4816 		dev_warn(dev,
4817 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4818 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4819 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4820 	}
4821 
4822 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4823 			       GFP_KERNEL);
4824 	if (!pf->vsi)
4825 		return -ENOMEM;
4826 
4827 	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
4828 				     sizeof(*pf->vsi_stats), GFP_KERNEL);
4829 	if (!pf->vsi_stats) {
4830 		devm_kfree(dev, pf->vsi);
4831 		return -ENOMEM;
4832 	}
4833 
4834 	return 0;
4835 }
4836 
4837 static void ice_dealloc_vsis(struct ice_pf *pf)
4838 {
4839 	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
4840 	pf->vsi_stats = NULL;
4841 
4842 	pf->num_alloc_vsi = 0;
4843 	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
4844 	pf->vsi = NULL;
4845 }
4846 
4847 static int ice_init_devlink(struct ice_pf *pf)
4848 {
4849 	int err;
4850 
4851 	err = ice_devlink_register_params(pf);
4852 	if (err)
4853 		return err;
4854 
4855 	ice_devlink_init_regions(pf);
4856 	ice_devlink_register(pf);
4857 
4858 	return 0;
4859 }
4860 
4861 static void ice_deinit_devlink(struct ice_pf *pf)
4862 {
4863 	ice_devlink_unregister(pf);
4864 	ice_devlink_destroy_regions(pf);
4865 	ice_devlink_unregister_params(pf);
4866 }
4867 
4868 static int ice_init(struct ice_pf *pf)
4869 {
4870 	int err;
4871 
4872 	err = ice_init_dev(pf);
4873 	if (err)
4874 		return err;
4875 
4876 	err = ice_alloc_vsis(pf);
4877 	if (err)
4878 		goto err_alloc_vsis;
4879 
4880 	err = ice_init_pf_sw(pf);
4881 	if (err)
4882 		goto err_init_pf_sw;
4883 
4884 	ice_init_wakeup(pf);
4885 
4886 	err = ice_init_link(pf);
4887 	if (err)
4888 		goto err_init_link;
4889 
4890 	err = ice_send_version(pf);
4891 	if (err)
4892 		goto err_init_link;
4893 
4894 	ice_verify_cacheline_size(pf);
4895 
4896 	if (ice_is_safe_mode(pf))
4897 		ice_set_safe_mode_vlan_cfg(pf);
4898 	else
4899 		/* print PCI link speed and width */
4900 		pcie_print_link_status(pf->pdev);
4901 
4902 	/* ready to go, so clear down state bit */
4903 	clear_bit(ICE_DOWN, pf->state);
4904 	clear_bit(ICE_SERVICE_DIS, pf->state);
4905 
4906 	/* since everything is good, start the service timer */
4907 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4908 
4909 	return 0;
4910 
4911 err_init_link:
4912 	ice_deinit_pf_sw(pf);
4913 err_init_pf_sw:
4914 	ice_dealloc_vsis(pf);
4915 err_alloc_vsis:
4916 	ice_deinit_dev(pf);
4917 	return err;
4918 }
4919 
4920 static void ice_deinit(struct ice_pf *pf)
4921 {
4922 	set_bit(ICE_SERVICE_DIS, pf->state);
4923 	set_bit(ICE_DOWN, pf->state);
4924 
4925 	ice_deinit_pf_sw(pf);
4926 	ice_dealloc_vsis(pf);
4927 	ice_deinit_dev(pf);
4928 }
4929 
4930 /**
4931  * ice_load - load pf by init hw and starting VSI
4932  * @pf: pointer to the pf instance
4933  */
4934 int ice_load(struct ice_pf *pf)
4935 {
4936 	struct ice_vsi_cfg_params params = {};
4937 	struct ice_vsi *vsi;
4938 	int err;
4939 
4940 	err = ice_init_dev(pf);
4941 	if (err)
4942 		return err;
4943 
4944 	vsi = ice_get_main_vsi(pf);
4945 
4946 	params = ice_vsi_to_params(vsi);
4947 	params.flags = ICE_VSI_FLAG_INIT;
4948 
4949 	rtnl_lock();
4950 	err = ice_vsi_cfg(vsi, &params);
4951 	if (err)
4952 		goto err_vsi_cfg;
4953 
4954 	err = ice_start_eth(ice_get_main_vsi(pf));
4955 	if (err)
4956 		goto err_start_eth;
4957 	rtnl_unlock();
4958 
4959 	err = ice_init_rdma(pf);
4960 	if (err)
4961 		goto err_init_rdma;
4962 
4963 	ice_init_features(pf);
4964 	ice_service_task_restart(pf);
4965 
4966 	clear_bit(ICE_DOWN, pf->state);
4967 
4968 	return 0;
4969 
4970 err_init_rdma:
4971 	ice_vsi_close(ice_get_main_vsi(pf));
4972 	rtnl_lock();
4973 err_start_eth:
4974 	ice_vsi_decfg(ice_get_main_vsi(pf));
4975 err_vsi_cfg:
4976 	rtnl_unlock();
4977 	ice_deinit_dev(pf);
4978 	return err;
4979 }
4980 
4981 /**
4982  * ice_unload - unload pf by stopping VSI and deinit hw
4983  * @pf: pointer to the pf instance
4984  */
4985 void ice_unload(struct ice_pf *pf)
4986 {
4987 	ice_deinit_features(pf);
4988 	ice_deinit_rdma(pf);
4989 	rtnl_lock();
4990 	ice_stop_eth(ice_get_main_vsi(pf));
4991 	ice_vsi_decfg(ice_get_main_vsi(pf));
4992 	rtnl_unlock();
4993 	ice_deinit_dev(pf);
4994 }
4995 
4996 /**
4997  * ice_probe - Device initialization routine
4998  * @pdev: PCI device information struct
4999  * @ent: entry in ice_pci_tbl
5000  *
5001  * Returns 0 on success, negative on failure
5002  */
5003 static int
5004 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5005 {
5006 	struct device *dev = &pdev->dev;
5007 	struct ice_pf *pf;
5008 	struct ice_hw *hw;
5009 	int err;
5010 
5011 	if (pdev->is_virtfn) {
5012 		dev_err(dev, "can't probe a virtual function\n");
5013 		return -EINVAL;
5014 	}
5015 
5016 	/* this driver uses devres, see
5017 	 * Documentation/driver-api/driver-model/devres.rst
5018 	 */
5019 	err = pcim_enable_device(pdev);
5020 	if (err)
5021 		return err;
5022 
5023 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5024 	if (err) {
5025 		dev_err(dev, "BAR0 I/O map error %d\n", err);
5026 		return err;
5027 	}
5028 
5029 	pf = ice_allocate_pf(dev);
5030 	if (!pf)
5031 		return -ENOMEM;
5032 
5033 	/* initialize Auxiliary index to invalid value */
5034 	pf->aux_idx = -1;
5035 
5036 	/* set up for high or low DMA */
5037 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5038 	if (err) {
5039 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5040 		return err;
5041 	}
5042 
5043 	pci_set_master(pdev);
5044 
5045 	pf->pdev = pdev;
5046 	pci_set_drvdata(pdev, pf);
5047 	set_bit(ICE_DOWN, pf->state);
5048 	/* Disable service task until DOWN bit is cleared */
5049 	set_bit(ICE_SERVICE_DIS, pf->state);
5050 
5051 	hw = &pf->hw;
5052 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5053 	pci_save_state(pdev);
5054 
5055 	hw->back = pf;
5056 	hw->port_info = NULL;
5057 	hw->vendor_id = pdev->vendor;
5058 	hw->device_id = pdev->device;
5059 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5060 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5061 	hw->subsystem_device_id = pdev->subsystem_device;
5062 	hw->bus.device = PCI_SLOT(pdev->devfn);
5063 	hw->bus.func = PCI_FUNC(pdev->devfn);
5064 	ice_set_ctrlq_len(hw);
5065 
5066 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5067 
5068 #ifndef CONFIG_DYNAMIC_DEBUG
5069 	if (debug < -1)
5070 		hw->debug_mask = debug;
5071 #endif
5072 
5073 	err = ice_init(pf);
5074 	if (err)
5075 		goto err_init;
5076 
5077 	err = ice_init_eth(pf);
5078 	if (err)
5079 		goto err_init_eth;
5080 
5081 	err = ice_init_rdma(pf);
5082 	if (err)
5083 		goto err_init_rdma;
5084 
5085 	err = ice_init_devlink(pf);
5086 	if (err)
5087 		goto err_init_devlink;
5088 
5089 	ice_init_features(pf);
5090 
5091 	return 0;
5092 
5093 err_init_devlink:
5094 	ice_deinit_rdma(pf);
5095 err_init_rdma:
5096 	ice_deinit_eth(pf);
5097 err_init_eth:
5098 	ice_deinit(pf);
5099 err_init:
5100 	pci_disable_device(pdev);
5101 	return err;
5102 }
5103 
5104 /**
5105  * ice_set_wake - enable or disable Wake on LAN
5106  * @pf: pointer to the PF struct
5107  *
5108  * Simple helper for WoL control
5109  */
5110 static void ice_set_wake(struct ice_pf *pf)
5111 {
5112 	struct ice_hw *hw = &pf->hw;
5113 	bool wol = pf->wol_ena;
5114 
5115 	/* clear wake state, otherwise new wake events won't fire */
5116 	wr32(hw, PFPM_WUS, U32_MAX);
5117 
5118 	/* enable / disable APM wake up, no RMW needed */
5119 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5120 
5121 	/* set magic packet filter enabled */
5122 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5123 }
5124 
5125 /**
5126  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5127  * @pf: pointer to the PF struct
5128  *
5129  * Issue firmware command to enable multicast magic wake, making
5130  * sure that any locally administered address (LAA) is used for
5131  * wake, and that PF reset doesn't undo the LAA.
5132  */
5133 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5134 {
5135 	struct device *dev = ice_pf_to_dev(pf);
5136 	struct ice_hw *hw = &pf->hw;
5137 	u8 mac_addr[ETH_ALEN];
5138 	struct ice_vsi *vsi;
5139 	int status;
5140 	u8 flags;
5141 
5142 	if (!pf->wol_ena)
5143 		return;
5144 
5145 	vsi = ice_get_main_vsi(pf);
5146 	if (!vsi)
5147 		return;
5148 
5149 	/* Get current MAC address in case it's an LAA */
5150 	if (vsi->netdev)
5151 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5152 	else
5153 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5154 
5155 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5156 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5157 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5158 
5159 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5160 	if (status)
5161 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5162 			status, ice_aq_str(hw->adminq.sq_last_status));
5163 }
5164 
5165 /**
5166  * ice_remove - Device removal routine
5167  * @pdev: PCI device information struct
5168  */
5169 static void ice_remove(struct pci_dev *pdev)
5170 {
5171 	struct ice_pf *pf = pci_get_drvdata(pdev);
5172 	int i;
5173 
5174 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5175 		if (!ice_is_reset_in_progress(pf->state))
5176 			break;
5177 		msleep(100);
5178 	}
5179 
5180 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5181 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5182 		ice_free_vfs(pf);
5183 	}
5184 
5185 	ice_service_task_stop(pf);
5186 	ice_aq_cancel_waiting_tasks(pf);
5187 	set_bit(ICE_DOWN, pf->state);
5188 
5189 	if (!ice_is_safe_mode(pf))
5190 		ice_remove_arfs(pf);
5191 	ice_deinit_features(pf);
5192 	ice_deinit_devlink(pf);
5193 	ice_deinit_rdma(pf);
5194 	ice_deinit_eth(pf);
5195 	ice_deinit(pf);
5196 
5197 	ice_vsi_release_all(pf);
5198 
5199 	ice_setup_mc_magic_wake(pf);
5200 	ice_set_wake(pf);
5201 
5202 	pci_disable_device(pdev);
5203 }
5204 
5205 /**
5206  * ice_shutdown - PCI callback for shutting down device
5207  * @pdev: PCI device information struct
5208  */
5209 static void ice_shutdown(struct pci_dev *pdev)
5210 {
5211 	struct ice_pf *pf = pci_get_drvdata(pdev);
5212 
5213 	ice_remove(pdev);
5214 
5215 	if (system_state == SYSTEM_POWER_OFF) {
5216 		pci_wake_from_d3(pdev, pf->wol_ena);
5217 		pci_set_power_state(pdev, PCI_D3hot);
5218 	}
5219 }
5220 
5221 #ifdef CONFIG_PM
5222 /**
5223  * ice_prepare_for_shutdown - prep for PCI shutdown
5224  * @pf: board private structure
5225  *
5226  * Inform or close all dependent features in prep for PCI device shutdown
5227  */
5228 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5229 {
5230 	struct ice_hw *hw = &pf->hw;
5231 	u32 v;
5232 
5233 	/* Notify VFs of impending reset */
5234 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5235 		ice_vc_notify_reset(pf);
5236 
5237 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5238 
5239 	/* disable the VSIs and their queues that are not already DOWN */
5240 	ice_pf_dis_all_vsi(pf, false);
5241 
5242 	ice_for_each_vsi(pf, v)
5243 		if (pf->vsi[v])
5244 			pf->vsi[v]->vsi_num = 0;
5245 
5246 	ice_shutdown_all_ctrlq(hw);
5247 }
5248 
5249 /**
5250  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5251  * @pf: board private structure to reinitialize
5252  *
5253  * This routine reinitialize interrupt scheme that was cleared during
5254  * power management suspend callback.
5255  *
5256  * This should be called during resume routine to re-allocate the q_vectors
5257  * and reacquire interrupts.
5258  */
5259 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5260 {
5261 	struct device *dev = ice_pf_to_dev(pf);
5262 	int ret, v;
5263 
5264 	/* Since we clear MSIX flag during suspend, we need to
5265 	 * set it back during resume...
5266 	 */
5267 
5268 	ret = ice_init_interrupt_scheme(pf);
5269 	if (ret) {
5270 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5271 		return ret;
5272 	}
5273 
5274 	/* Remap vectors and rings, after successful re-init interrupts */
5275 	ice_for_each_vsi(pf, v) {
5276 		if (!pf->vsi[v])
5277 			continue;
5278 
5279 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5280 		if (ret)
5281 			goto err_reinit;
5282 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5283 	}
5284 
5285 	ret = ice_req_irq_msix_misc(pf);
5286 	if (ret) {
5287 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5288 			ret);
5289 		goto err_reinit;
5290 	}
5291 
5292 	return 0;
5293 
5294 err_reinit:
5295 	while (v--)
5296 		if (pf->vsi[v])
5297 			ice_vsi_free_q_vectors(pf->vsi[v]);
5298 
5299 	return ret;
5300 }
5301 
5302 /**
5303  * ice_suspend
5304  * @dev: generic device information structure
5305  *
5306  * Power Management callback to quiesce the device and prepare
5307  * for D3 transition.
5308  */
5309 static int __maybe_unused ice_suspend(struct device *dev)
5310 {
5311 	struct pci_dev *pdev = to_pci_dev(dev);
5312 	struct ice_pf *pf;
5313 	int disabled, v;
5314 
5315 	pf = pci_get_drvdata(pdev);
5316 
5317 	if (!ice_pf_state_is_nominal(pf)) {
5318 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5319 		return -EBUSY;
5320 	}
5321 
5322 	/* Stop watchdog tasks until resume completion.
5323 	 * Even though it is most likely that the service task is
5324 	 * disabled if the device is suspended or down, the service task's
5325 	 * state is controlled by a different state bit, and we should
5326 	 * store and honor whatever state that bit is in at this point.
5327 	 */
5328 	disabled = ice_service_task_stop(pf);
5329 
5330 	ice_unplug_aux_dev(pf);
5331 
5332 	/* Already suspended?, then there is nothing to do */
5333 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5334 		if (!disabled)
5335 			ice_service_task_restart(pf);
5336 		return 0;
5337 	}
5338 
5339 	if (test_bit(ICE_DOWN, pf->state) ||
5340 	    ice_is_reset_in_progress(pf->state)) {
5341 		dev_err(dev, "can't suspend device in reset or already down\n");
5342 		if (!disabled)
5343 			ice_service_task_restart(pf);
5344 		return 0;
5345 	}
5346 
5347 	ice_setup_mc_magic_wake(pf);
5348 
5349 	ice_prepare_for_shutdown(pf);
5350 
5351 	ice_set_wake(pf);
5352 
5353 	/* Free vectors, clear the interrupt scheme and release IRQs
5354 	 * for proper hibernation, especially with large number of CPUs.
5355 	 * Otherwise hibernation might fail when mapping all the vectors back
5356 	 * to CPU0.
5357 	 */
5358 	ice_free_irq_msix_misc(pf);
5359 	ice_for_each_vsi(pf, v) {
5360 		if (!pf->vsi[v])
5361 			continue;
5362 		ice_vsi_free_q_vectors(pf->vsi[v]);
5363 	}
5364 	ice_clear_interrupt_scheme(pf);
5365 
5366 	pci_save_state(pdev);
5367 	pci_wake_from_d3(pdev, pf->wol_ena);
5368 	pci_set_power_state(pdev, PCI_D3hot);
5369 	return 0;
5370 }
5371 
5372 /**
5373  * ice_resume - PM callback for waking up from D3
5374  * @dev: generic device information structure
5375  */
5376 static int __maybe_unused ice_resume(struct device *dev)
5377 {
5378 	struct pci_dev *pdev = to_pci_dev(dev);
5379 	enum ice_reset_req reset_type;
5380 	struct ice_pf *pf;
5381 	struct ice_hw *hw;
5382 	int ret;
5383 
5384 	pci_set_power_state(pdev, PCI_D0);
5385 	pci_restore_state(pdev);
5386 	pci_save_state(pdev);
5387 
5388 	if (!pci_device_is_present(pdev))
5389 		return -ENODEV;
5390 
5391 	ret = pci_enable_device_mem(pdev);
5392 	if (ret) {
5393 		dev_err(dev, "Cannot enable device after suspend\n");
5394 		return ret;
5395 	}
5396 
5397 	pf = pci_get_drvdata(pdev);
5398 	hw = &pf->hw;
5399 
5400 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5401 	ice_print_wake_reason(pf);
5402 
5403 	/* We cleared the interrupt scheme when we suspended, so we need to
5404 	 * restore it now to resume device functionality.
5405 	 */
5406 	ret = ice_reinit_interrupt_scheme(pf);
5407 	if (ret)
5408 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5409 
5410 	clear_bit(ICE_DOWN, pf->state);
5411 	/* Now perform PF reset and rebuild */
5412 	reset_type = ICE_RESET_PFR;
5413 	/* re-enable service task for reset, but allow reset to schedule it */
5414 	clear_bit(ICE_SERVICE_DIS, pf->state);
5415 
5416 	if (ice_schedule_reset(pf, reset_type))
5417 		dev_err(dev, "Reset during resume failed.\n");
5418 
5419 	clear_bit(ICE_SUSPENDED, pf->state);
5420 	ice_service_task_restart(pf);
5421 
5422 	/* Restart the service task */
5423 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5424 
5425 	return 0;
5426 }
5427 #endif /* CONFIG_PM */
5428 
5429 /**
5430  * ice_pci_err_detected - warning that PCI error has been detected
5431  * @pdev: PCI device information struct
5432  * @err: the type of PCI error
5433  *
5434  * Called to warn that something happened on the PCI bus and the error handling
5435  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5436  */
5437 static pci_ers_result_t
5438 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5439 {
5440 	struct ice_pf *pf = pci_get_drvdata(pdev);
5441 
5442 	if (!pf) {
5443 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5444 			__func__, err);
5445 		return PCI_ERS_RESULT_DISCONNECT;
5446 	}
5447 
5448 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5449 		ice_service_task_stop(pf);
5450 
5451 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5452 			set_bit(ICE_PFR_REQ, pf->state);
5453 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5454 		}
5455 	}
5456 
5457 	return PCI_ERS_RESULT_NEED_RESET;
5458 }
5459 
5460 /**
5461  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5462  * @pdev: PCI device information struct
5463  *
5464  * Called to determine if the driver can recover from the PCI slot reset by
5465  * using a register read to determine if the device is recoverable.
5466  */
5467 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5468 {
5469 	struct ice_pf *pf = pci_get_drvdata(pdev);
5470 	pci_ers_result_t result;
5471 	int err;
5472 	u32 reg;
5473 
5474 	err = pci_enable_device_mem(pdev);
5475 	if (err) {
5476 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5477 			err);
5478 		result = PCI_ERS_RESULT_DISCONNECT;
5479 	} else {
5480 		pci_set_master(pdev);
5481 		pci_restore_state(pdev);
5482 		pci_save_state(pdev);
5483 		pci_wake_from_d3(pdev, false);
5484 
5485 		/* Check for life */
5486 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5487 		if (!reg)
5488 			result = PCI_ERS_RESULT_RECOVERED;
5489 		else
5490 			result = PCI_ERS_RESULT_DISCONNECT;
5491 	}
5492 
5493 	return result;
5494 }
5495 
5496 /**
5497  * ice_pci_err_resume - restart operations after PCI error recovery
5498  * @pdev: PCI device information struct
5499  *
5500  * Called to allow the driver to bring things back up after PCI error and/or
5501  * reset recovery have finished
5502  */
5503 static void ice_pci_err_resume(struct pci_dev *pdev)
5504 {
5505 	struct ice_pf *pf = pci_get_drvdata(pdev);
5506 
5507 	if (!pf) {
5508 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5509 			__func__);
5510 		return;
5511 	}
5512 
5513 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5514 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5515 			__func__);
5516 		return;
5517 	}
5518 
5519 	ice_restore_all_vfs_msi_state(pdev);
5520 
5521 	ice_do_reset(pf, ICE_RESET_PFR);
5522 	ice_service_task_restart(pf);
5523 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5524 }
5525 
5526 /**
5527  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5528  * @pdev: PCI device information struct
5529  */
5530 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5531 {
5532 	struct ice_pf *pf = pci_get_drvdata(pdev);
5533 
5534 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5535 		ice_service_task_stop(pf);
5536 
5537 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5538 			set_bit(ICE_PFR_REQ, pf->state);
5539 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5540 		}
5541 	}
5542 }
5543 
5544 /**
5545  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5546  * @pdev: PCI device information struct
5547  */
5548 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5549 {
5550 	ice_pci_err_resume(pdev);
5551 }
5552 
5553 /* ice_pci_tbl - PCI Device ID Table
5554  *
5555  * Wildcard entries (PCI_ANY_ID) should come last
5556  * Last entry must be all 0s
5557  *
5558  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5559  *   Class, Class Mask, private data (not used) }
5560  */
5561 static const struct pci_device_id ice_pci_tbl[] = {
5562 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5563 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5564 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5565 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5566 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5567 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5568 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5569 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5570 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5571 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5572 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5573 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5574 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5575 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5576 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5577 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5578 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5579 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5580 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5581 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5582 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5583 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5584 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5585 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5586 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5587 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT), 0 },
5588 	/* required last entry */
5589 	{ 0, }
5590 };
5591 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5592 
5593 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5594 
5595 static const struct pci_error_handlers ice_pci_err_handler = {
5596 	.error_detected = ice_pci_err_detected,
5597 	.slot_reset = ice_pci_err_slot_reset,
5598 	.reset_prepare = ice_pci_err_reset_prepare,
5599 	.reset_done = ice_pci_err_reset_done,
5600 	.resume = ice_pci_err_resume
5601 };
5602 
5603 static struct pci_driver ice_driver = {
5604 	.name = KBUILD_MODNAME,
5605 	.id_table = ice_pci_tbl,
5606 	.probe = ice_probe,
5607 	.remove = ice_remove,
5608 #ifdef CONFIG_PM
5609 	.driver.pm = &ice_pm_ops,
5610 #endif /* CONFIG_PM */
5611 	.shutdown = ice_shutdown,
5612 	.sriov_configure = ice_sriov_configure,
5613 	.err_handler = &ice_pci_err_handler
5614 };
5615 
5616 /**
5617  * ice_module_init - Driver registration routine
5618  *
5619  * ice_module_init is the first routine called when the driver is
5620  * loaded. All it does is register with the PCI subsystem.
5621  */
5622 static int __init ice_module_init(void)
5623 {
5624 	int status = -ENOMEM;
5625 
5626 	pr_info("%s\n", ice_driver_string);
5627 	pr_info("%s\n", ice_copyright);
5628 
5629 	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5630 	if (!ice_wq) {
5631 		pr_err("Failed to create workqueue\n");
5632 		return status;
5633 	}
5634 
5635 	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5636 	if (!ice_lag_wq) {
5637 		pr_err("Failed to create LAG workqueue\n");
5638 		goto err_dest_wq;
5639 	}
5640 
5641 	status = pci_register_driver(&ice_driver);
5642 	if (status) {
5643 		pr_err("failed to register PCI driver, err %d\n", status);
5644 		goto err_dest_lag_wq;
5645 	}
5646 
5647 	return 0;
5648 
5649 err_dest_lag_wq:
5650 	destroy_workqueue(ice_lag_wq);
5651 err_dest_wq:
5652 	destroy_workqueue(ice_wq);
5653 	return status;
5654 }
5655 module_init(ice_module_init);
5656 
5657 /**
5658  * ice_module_exit - Driver exit cleanup routine
5659  *
5660  * ice_module_exit is called just before the driver is removed
5661  * from memory.
5662  */
5663 static void __exit ice_module_exit(void)
5664 {
5665 	pci_unregister_driver(&ice_driver);
5666 	destroy_workqueue(ice_wq);
5667 	destroy_workqueue(ice_lag_wq);
5668 	pr_info("module unloaded\n");
5669 }
5670 module_exit(ice_module_exit);
5671 
5672 /**
5673  * ice_set_mac_address - NDO callback to set MAC address
5674  * @netdev: network interface device structure
5675  * @pi: pointer to an address structure
5676  *
5677  * Returns 0 on success, negative on failure
5678  */
5679 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5680 {
5681 	struct ice_netdev_priv *np = netdev_priv(netdev);
5682 	struct ice_vsi *vsi = np->vsi;
5683 	struct ice_pf *pf = vsi->back;
5684 	struct ice_hw *hw = &pf->hw;
5685 	struct sockaddr *addr = pi;
5686 	u8 old_mac[ETH_ALEN];
5687 	u8 flags = 0;
5688 	u8 *mac;
5689 	int err;
5690 
5691 	mac = (u8 *)addr->sa_data;
5692 
5693 	if (!is_valid_ether_addr(mac))
5694 		return -EADDRNOTAVAIL;
5695 
5696 	if (test_bit(ICE_DOWN, pf->state) ||
5697 	    ice_is_reset_in_progress(pf->state)) {
5698 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5699 			   mac);
5700 		return -EBUSY;
5701 	}
5702 
5703 	if (ice_chnl_dmac_fltr_cnt(pf)) {
5704 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5705 			   mac);
5706 		return -EAGAIN;
5707 	}
5708 
5709 	netif_addr_lock_bh(netdev);
5710 	ether_addr_copy(old_mac, netdev->dev_addr);
5711 	/* change the netdev's MAC address */
5712 	eth_hw_addr_set(netdev, mac);
5713 	netif_addr_unlock_bh(netdev);
5714 
5715 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5716 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5717 	if (err && err != -ENOENT) {
5718 		err = -EADDRNOTAVAIL;
5719 		goto err_update_filters;
5720 	}
5721 
5722 	/* Add filter for new MAC. If filter exists, return success */
5723 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5724 	if (err == -EEXIST) {
5725 		/* Although this MAC filter is already present in hardware it's
5726 		 * possible in some cases (e.g. bonding) that dev_addr was
5727 		 * modified outside of the driver and needs to be restored back
5728 		 * to this value.
5729 		 */
5730 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5731 
5732 		return 0;
5733 	} else if (err) {
5734 		/* error if the new filter addition failed */
5735 		err = -EADDRNOTAVAIL;
5736 	}
5737 
5738 err_update_filters:
5739 	if (err) {
5740 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5741 			   mac);
5742 		netif_addr_lock_bh(netdev);
5743 		eth_hw_addr_set(netdev, old_mac);
5744 		netif_addr_unlock_bh(netdev);
5745 		return err;
5746 	}
5747 
5748 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5749 		   netdev->dev_addr);
5750 
5751 	/* write new MAC address to the firmware */
5752 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5753 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5754 	if (err) {
5755 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5756 			   mac, err);
5757 	}
5758 	return 0;
5759 }
5760 
5761 /**
5762  * ice_set_rx_mode - NDO callback to set the netdev filters
5763  * @netdev: network interface device structure
5764  */
5765 static void ice_set_rx_mode(struct net_device *netdev)
5766 {
5767 	struct ice_netdev_priv *np = netdev_priv(netdev);
5768 	struct ice_vsi *vsi = np->vsi;
5769 
5770 	if (!vsi || ice_is_switchdev_running(vsi->back))
5771 		return;
5772 
5773 	/* Set the flags to synchronize filters
5774 	 * ndo_set_rx_mode may be triggered even without a change in netdev
5775 	 * flags
5776 	 */
5777 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5778 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5779 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5780 
5781 	/* schedule our worker thread which will take care of
5782 	 * applying the new filter changes
5783 	 */
5784 	ice_service_task_schedule(vsi->back);
5785 }
5786 
5787 /**
5788  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5789  * @netdev: network interface device structure
5790  * @queue_index: Queue ID
5791  * @maxrate: maximum bandwidth in Mbps
5792  */
5793 static int
5794 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5795 {
5796 	struct ice_netdev_priv *np = netdev_priv(netdev);
5797 	struct ice_vsi *vsi = np->vsi;
5798 	u16 q_handle;
5799 	int status;
5800 	u8 tc;
5801 
5802 	/* Validate maxrate requested is within permitted range */
5803 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5804 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5805 			   maxrate, queue_index);
5806 		return -EINVAL;
5807 	}
5808 
5809 	q_handle = vsi->tx_rings[queue_index]->q_handle;
5810 	tc = ice_dcb_get_tc(vsi, queue_index);
5811 
5812 	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
5813 	if (!vsi) {
5814 		netdev_err(netdev, "Invalid VSI for given queue %d\n",
5815 			   queue_index);
5816 		return -EINVAL;
5817 	}
5818 
5819 	/* Set BW back to default, when user set maxrate to 0 */
5820 	if (!maxrate)
5821 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5822 					       q_handle, ICE_MAX_BW);
5823 	else
5824 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5825 					  q_handle, ICE_MAX_BW, maxrate * 1000);
5826 	if (status)
5827 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5828 			   status);
5829 
5830 	return status;
5831 }
5832 
5833 /**
5834  * ice_fdb_add - add an entry to the hardware database
5835  * @ndm: the input from the stack
5836  * @tb: pointer to array of nladdr (unused)
5837  * @dev: the net device pointer
5838  * @addr: the MAC address entry being added
5839  * @vid: VLAN ID
5840  * @flags: instructions from stack about fdb operation
5841  * @extack: netlink extended ack
5842  */
5843 static int
5844 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5845 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5846 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5847 {
5848 	int err;
5849 
5850 	if (vid) {
5851 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5852 		return -EINVAL;
5853 	}
5854 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5855 		netdev_err(dev, "FDB only supports static addresses\n");
5856 		return -EINVAL;
5857 	}
5858 
5859 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5860 		err = dev_uc_add_excl(dev, addr);
5861 	else if (is_multicast_ether_addr(addr))
5862 		err = dev_mc_add_excl(dev, addr);
5863 	else
5864 		err = -EINVAL;
5865 
5866 	/* Only return duplicate errors if NLM_F_EXCL is set */
5867 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5868 		err = 0;
5869 
5870 	return err;
5871 }
5872 
5873 /**
5874  * ice_fdb_del - delete an entry from the hardware database
5875  * @ndm: the input from the stack
5876  * @tb: pointer to array of nladdr (unused)
5877  * @dev: the net device pointer
5878  * @addr: the MAC address entry being added
5879  * @vid: VLAN ID
5880  * @extack: netlink extended ack
5881  */
5882 static int
5883 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5884 	    struct net_device *dev, const unsigned char *addr,
5885 	    __always_unused u16 vid, struct netlink_ext_ack *extack)
5886 {
5887 	int err;
5888 
5889 	if (ndm->ndm_state & NUD_PERMANENT) {
5890 		netdev_err(dev, "FDB only supports static addresses\n");
5891 		return -EINVAL;
5892 	}
5893 
5894 	if (is_unicast_ether_addr(addr))
5895 		err = dev_uc_del(dev, addr);
5896 	else if (is_multicast_ether_addr(addr))
5897 		err = dev_mc_del(dev, addr);
5898 	else
5899 		err = -EINVAL;
5900 
5901 	return err;
5902 }
5903 
5904 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5905 					 NETIF_F_HW_VLAN_CTAG_TX | \
5906 					 NETIF_F_HW_VLAN_STAG_RX | \
5907 					 NETIF_F_HW_VLAN_STAG_TX)
5908 
5909 #define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5910 					 NETIF_F_HW_VLAN_STAG_RX)
5911 
5912 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
5913 					 NETIF_F_HW_VLAN_STAG_FILTER)
5914 
5915 /**
5916  * ice_fix_features - fix the netdev features flags based on device limitations
5917  * @netdev: ptr to the netdev that flags are being fixed on
5918  * @features: features that need to be checked and possibly fixed
5919  *
5920  * Make sure any fixups are made to features in this callback. This enables the
5921  * driver to not have to check unsupported configurations throughout the driver
5922  * because that's the responsiblity of this callback.
5923  *
5924  * Single VLAN Mode (SVM) Supported Features:
5925  *	NETIF_F_HW_VLAN_CTAG_FILTER
5926  *	NETIF_F_HW_VLAN_CTAG_RX
5927  *	NETIF_F_HW_VLAN_CTAG_TX
5928  *
5929  * Double VLAN Mode (DVM) Supported Features:
5930  *	NETIF_F_HW_VLAN_CTAG_FILTER
5931  *	NETIF_F_HW_VLAN_CTAG_RX
5932  *	NETIF_F_HW_VLAN_CTAG_TX
5933  *
5934  *	NETIF_F_HW_VLAN_STAG_FILTER
5935  *	NETIF_HW_VLAN_STAG_RX
5936  *	NETIF_HW_VLAN_STAG_TX
5937  *
5938  * Features that need fixing:
5939  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
5940  *	These are mutually exlusive as the VSI context cannot support multiple
5941  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
5942  *	is not done, then default to clearing the requested STAG offload
5943  *	settings.
5944  *
5945  *	All supported filtering has to be enabled or disabled together. For
5946  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
5947  *	together. If this is not done, then default to VLAN filtering disabled.
5948  *	These are mutually exclusive as there is currently no way to
5949  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
5950  *	prune rules.
5951  */
5952 static netdev_features_t
5953 ice_fix_features(struct net_device *netdev, netdev_features_t features)
5954 {
5955 	struct ice_netdev_priv *np = netdev_priv(netdev);
5956 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
5957 	bool cur_ctag, cur_stag, req_ctag, req_stag;
5958 
5959 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
5960 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5961 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5962 
5963 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
5964 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5965 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5966 
5967 	if (req_vlan_fltr != cur_vlan_fltr) {
5968 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
5969 			if (req_ctag && req_stag) {
5970 				features |= NETIF_VLAN_FILTERING_FEATURES;
5971 			} else if (!req_ctag && !req_stag) {
5972 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5973 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
5974 				   (!cur_stag && req_stag && !cur_ctag)) {
5975 				features |= NETIF_VLAN_FILTERING_FEATURES;
5976 				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");
5977 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
5978 				   (cur_stag && !req_stag && cur_ctag)) {
5979 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5980 				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");
5981 			}
5982 		} else {
5983 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
5984 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
5985 
5986 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
5987 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5988 		}
5989 	}
5990 
5991 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
5992 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
5993 		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");
5994 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
5995 			      NETIF_F_HW_VLAN_STAG_TX);
5996 	}
5997 
5998 	if (!(netdev->features & NETIF_F_RXFCS) &&
5999 	    (features & NETIF_F_RXFCS) &&
6000 	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6001 	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6002 		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6003 		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6004 	}
6005 
6006 	return features;
6007 }
6008 
6009 /**
6010  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6011  * @vsi: PF's VSI
6012  * @features: features used to determine VLAN offload settings
6013  *
6014  * First, determine the vlan_ethertype based on the VLAN offload bits in
6015  * features. Then determine if stripping and insertion should be enabled or
6016  * disabled. Finally enable or disable VLAN stripping and insertion.
6017  */
6018 static int
6019 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6020 {
6021 	bool enable_stripping = true, enable_insertion = true;
6022 	struct ice_vsi_vlan_ops *vlan_ops;
6023 	int strip_err = 0, insert_err = 0;
6024 	u16 vlan_ethertype = 0;
6025 
6026 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6027 
6028 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6029 		vlan_ethertype = ETH_P_8021AD;
6030 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6031 		vlan_ethertype = ETH_P_8021Q;
6032 
6033 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6034 		enable_stripping = false;
6035 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6036 		enable_insertion = false;
6037 
6038 	if (enable_stripping)
6039 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6040 	else
6041 		strip_err = vlan_ops->dis_stripping(vsi);
6042 
6043 	if (enable_insertion)
6044 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6045 	else
6046 		insert_err = vlan_ops->dis_insertion(vsi);
6047 
6048 	if (strip_err || insert_err)
6049 		return -EIO;
6050 
6051 	return 0;
6052 }
6053 
6054 /**
6055  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6056  * @vsi: PF's VSI
6057  * @features: features used to determine VLAN filtering settings
6058  *
6059  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6060  * features.
6061  */
6062 static int
6063 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6064 {
6065 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6066 	int err = 0;
6067 
6068 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6069 	 * if either bit is set
6070 	 */
6071 	if (features &
6072 	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6073 		err = vlan_ops->ena_rx_filtering(vsi);
6074 	else
6075 		err = vlan_ops->dis_rx_filtering(vsi);
6076 
6077 	return err;
6078 }
6079 
6080 /**
6081  * ice_set_vlan_features - set VLAN settings based on suggested feature set
6082  * @netdev: ptr to the netdev being adjusted
6083  * @features: the feature set that the stack is suggesting
6084  *
6085  * Only update VLAN settings if the requested_vlan_features are different than
6086  * the current_vlan_features.
6087  */
6088 static int
6089 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6090 {
6091 	netdev_features_t current_vlan_features, requested_vlan_features;
6092 	struct ice_netdev_priv *np = netdev_priv(netdev);
6093 	struct ice_vsi *vsi = np->vsi;
6094 	int err;
6095 
6096 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6097 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6098 	if (current_vlan_features ^ requested_vlan_features) {
6099 		if ((features & NETIF_F_RXFCS) &&
6100 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6101 			dev_err(ice_pf_to_dev(vsi->back),
6102 				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6103 			return -EIO;
6104 		}
6105 
6106 		err = ice_set_vlan_offload_features(vsi, features);
6107 		if (err)
6108 			return err;
6109 	}
6110 
6111 	current_vlan_features = netdev->features &
6112 		NETIF_VLAN_FILTERING_FEATURES;
6113 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6114 	if (current_vlan_features ^ requested_vlan_features) {
6115 		err = ice_set_vlan_filtering_features(vsi, features);
6116 		if (err)
6117 			return err;
6118 	}
6119 
6120 	return 0;
6121 }
6122 
6123 /**
6124  * ice_set_loopback - turn on/off loopback mode on underlying PF
6125  * @vsi: ptr to VSI
6126  * @ena: flag to indicate the on/off setting
6127  */
6128 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6129 {
6130 	bool if_running = netif_running(vsi->netdev);
6131 	int ret;
6132 
6133 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6134 		ret = ice_down(vsi);
6135 		if (ret) {
6136 			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6137 			return ret;
6138 		}
6139 	}
6140 	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6141 	if (ret)
6142 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6143 	if (if_running)
6144 		ret = ice_up(vsi);
6145 
6146 	return ret;
6147 }
6148 
6149 /**
6150  * ice_set_features - set the netdev feature flags
6151  * @netdev: ptr to the netdev being adjusted
6152  * @features: the feature set that the stack is suggesting
6153  */
6154 static int
6155 ice_set_features(struct net_device *netdev, netdev_features_t features)
6156 {
6157 	netdev_features_t changed = netdev->features ^ features;
6158 	struct ice_netdev_priv *np = netdev_priv(netdev);
6159 	struct ice_vsi *vsi = np->vsi;
6160 	struct ice_pf *pf = vsi->back;
6161 	int ret = 0;
6162 
6163 	/* Don't set any netdev advanced features with device in Safe Mode */
6164 	if (ice_is_safe_mode(pf)) {
6165 		dev_err(ice_pf_to_dev(pf),
6166 			"Device is in Safe Mode - not enabling advanced netdev features\n");
6167 		return ret;
6168 	}
6169 
6170 	/* Do not change setting during reset */
6171 	if (ice_is_reset_in_progress(pf->state)) {
6172 		dev_err(ice_pf_to_dev(pf),
6173 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6174 		return -EBUSY;
6175 	}
6176 
6177 	/* Multiple features can be changed in one call so keep features in
6178 	 * separate if/else statements to guarantee each feature is checked
6179 	 */
6180 	if (changed & NETIF_F_RXHASH)
6181 		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6182 
6183 	ret = ice_set_vlan_features(netdev, features);
6184 	if (ret)
6185 		return ret;
6186 
6187 	/* Turn on receive of FCS aka CRC, and after setting this
6188 	 * flag the packet data will have the 4 byte CRC appended
6189 	 */
6190 	if (changed & NETIF_F_RXFCS) {
6191 		if ((features & NETIF_F_RXFCS) &&
6192 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6193 			dev_err(ice_pf_to_dev(vsi->back),
6194 				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6195 			return -EIO;
6196 		}
6197 
6198 		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6199 		ret = ice_down_up(vsi);
6200 		if (ret)
6201 			return ret;
6202 	}
6203 
6204 	if (changed & NETIF_F_NTUPLE) {
6205 		bool ena = !!(features & NETIF_F_NTUPLE);
6206 
6207 		ice_vsi_manage_fdir(vsi, ena);
6208 		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6209 	}
6210 
6211 	/* don't turn off hw_tc_offload when ADQ is already enabled */
6212 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6213 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6214 		return -EACCES;
6215 	}
6216 
6217 	if (changed & NETIF_F_HW_TC) {
6218 		bool ena = !!(features & NETIF_F_HW_TC);
6219 
6220 		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6221 		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6222 	}
6223 
6224 	if (changed & NETIF_F_LOOPBACK)
6225 		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6226 
6227 	return ret;
6228 }
6229 
6230 /**
6231  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6232  * @vsi: VSI to setup VLAN properties for
6233  */
6234 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6235 {
6236 	int err;
6237 
6238 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6239 	if (err)
6240 		return err;
6241 
6242 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6243 	if (err)
6244 		return err;
6245 
6246 	return ice_vsi_add_vlan_zero(vsi);
6247 }
6248 
6249 /**
6250  * ice_vsi_cfg_lan - Setup the VSI lan related config
6251  * @vsi: the VSI being configured
6252  *
6253  * Return 0 on success and negative value on error
6254  */
6255 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6256 {
6257 	int err;
6258 
6259 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6260 		ice_set_rx_mode(vsi->netdev);
6261 
6262 		err = ice_vsi_vlan_setup(vsi);
6263 		if (err)
6264 			return err;
6265 	}
6266 	ice_vsi_cfg_dcb_rings(vsi);
6267 
6268 	err = ice_vsi_cfg_lan_txqs(vsi);
6269 	if (!err && ice_is_xdp_ena_vsi(vsi))
6270 		err = ice_vsi_cfg_xdp_txqs(vsi);
6271 	if (!err)
6272 		err = ice_vsi_cfg_rxqs(vsi);
6273 
6274 	return err;
6275 }
6276 
6277 /* THEORY OF MODERATION:
6278  * The ice driver hardware works differently than the hardware that DIMLIB was
6279  * originally made for. ice hardware doesn't have packet count limits that
6280  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6281  * which is hard-coded to a limit of 250,000 ints/second.
6282  * If not using dynamic moderation, the INTRL value can be modified
6283  * by ethtool rx-usecs-high.
6284  */
6285 struct ice_dim {
6286 	/* the throttle rate for interrupts, basically worst case delay before
6287 	 * an initial interrupt fires, value is stored in microseconds.
6288 	 */
6289 	u16 itr;
6290 };
6291 
6292 /* Make a different profile for Rx that doesn't allow quite so aggressive
6293  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6294  * second.
6295  */
6296 static const struct ice_dim rx_profile[] = {
6297 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6298 	{8},    /* 125,000 ints/s */
6299 	{16},   /*  62,500 ints/s */
6300 	{62},   /*  16,129 ints/s */
6301 	{126}   /*   7,936 ints/s */
6302 };
6303 
6304 /* The transmit profile, which has the same sorts of values
6305  * as the previous struct
6306  */
6307 static const struct ice_dim tx_profile[] = {
6308 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6309 	{8},    /* 125,000 ints/s */
6310 	{40},   /*  16,125 ints/s */
6311 	{128},  /*   7,812 ints/s */
6312 	{256}   /*   3,906 ints/s */
6313 };
6314 
6315 static void ice_tx_dim_work(struct work_struct *work)
6316 {
6317 	struct ice_ring_container *rc;
6318 	struct dim *dim;
6319 	u16 itr;
6320 
6321 	dim = container_of(work, struct dim, work);
6322 	rc = dim->priv;
6323 
6324 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6325 
6326 	/* look up the values in our local table */
6327 	itr = tx_profile[dim->profile_ix].itr;
6328 
6329 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6330 	ice_write_itr(rc, itr);
6331 
6332 	dim->state = DIM_START_MEASURE;
6333 }
6334 
6335 static void ice_rx_dim_work(struct work_struct *work)
6336 {
6337 	struct ice_ring_container *rc;
6338 	struct dim *dim;
6339 	u16 itr;
6340 
6341 	dim = container_of(work, struct dim, work);
6342 	rc = dim->priv;
6343 
6344 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6345 
6346 	/* look up the values in our local table */
6347 	itr = rx_profile[dim->profile_ix].itr;
6348 
6349 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6350 	ice_write_itr(rc, itr);
6351 
6352 	dim->state = DIM_START_MEASURE;
6353 }
6354 
6355 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6356 
6357 /**
6358  * ice_init_moderation - set up interrupt moderation
6359  * @q_vector: the vector containing rings to be configured
6360  *
6361  * Set up interrupt moderation registers, with the intent to do the right thing
6362  * when called from reset or from probe, and whether or not dynamic moderation
6363  * is enabled or not. Take special care to write all the registers in both
6364  * dynamic moderation mode or not in order to make sure hardware is in a known
6365  * state.
6366  */
6367 static void ice_init_moderation(struct ice_q_vector *q_vector)
6368 {
6369 	struct ice_ring_container *rc;
6370 	bool tx_dynamic, rx_dynamic;
6371 
6372 	rc = &q_vector->tx;
6373 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6374 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6375 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6376 	rc->dim.priv = rc;
6377 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6378 
6379 	/* set the initial TX ITR to match the above */
6380 	ice_write_itr(rc, tx_dynamic ?
6381 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6382 
6383 	rc = &q_vector->rx;
6384 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6385 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6386 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6387 	rc->dim.priv = rc;
6388 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6389 
6390 	/* set the initial RX ITR to match the above */
6391 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6392 				       rc->itr_setting);
6393 
6394 	ice_set_q_vector_intrl(q_vector);
6395 }
6396 
6397 /**
6398  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6399  * @vsi: the VSI being configured
6400  */
6401 static void ice_napi_enable_all(struct ice_vsi *vsi)
6402 {
6403 	int q_idx;
6404 
6405 	if (!vsi->netdev)
6406 		return;
6407 
6408 	ice_for_each_q_vector(vsi, q_idx) {
6409 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6410 
6411 		ice_init_moderation(q_vector);
6412 
6413 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6414 			napi_enable(&q_vector->napi);
6415 	}
6416 }
6417 
6418 /**
6419  * ice_up_complete - Finish the last steps of bringing up a connection
6420  * @vsi: The VSI being configured
6421  *
6422  * Return 0 on success and negative value on error
6423  */
6424 static int ice_up_complete(struct ice_vsi *vsi)
6425 {
6426 	struct ice_pf *pf = vsi->back;
6427 	int err;
6428 
6429 	ice_vsi_cfg_msix(vsi);
6430 
6431 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6432 	 * Tx queue group list was configured and the context bits were
6433 	 * programmed using ice_vsi_cfg_txqs
6434 	 */
6435 	err = ice_vsi_start_all_rx_rings(vsi);
6436 	if (err)
6437 		return err;
6438 
6439 	clear_bit(ICE_VSI_DOWN, vsi->state);
6440 	ice_napi_enable_all(vsi);
6441 	ice_vsi_ena_irq(vsi);
6442 
6443 	if (vsi->port_info &&
6444 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6445 	    vsi->netdev && vsi->type == ICE_VSI_PF) {
6446 		ice_print_link_msg(vsi, true);
6447 		netif_tx_start_all_queues(vsi->netdev);
6448 		netif_carrier_on(vsi->netdev);
6449 		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6450 	}
6451 
6452 	/* Perform an initial read of the statistics registers now to
6453 	 * set the baseline so counters are ready when interface is up
6454 	 */
6455 	ice_update_eth_stats(vsi);
6456 
6457 	if (vsi->type == ICE_VSI_PF)
6458 		ice_service_task_schedule(pf);
6459 
6460 	return 0;
6461 }
6462 
6463 /**
6464  * ice_up - Bring the connection back up after being down
6465  * @vsi: VSI being configured
6466  */
6467 int ice_up(struct ice_vsi *vsi)
6468 {
6469 	int err;
6470 
6471 	err = ice_vsi_cfg_lan(vsi);
6472 	if (!err)
6473 		err = ice_up_complete(vsi);
6474 
6475 	return err;
6476 }
6477 
6478 /**
6479  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6480  * @syncp: pointer to u64_stats_sync
6481  * @stats: stats that pkts and bytes count will be taken from
6482  * @pkts: packets stats counter
6483  * @bytes: bytes stats counter
6484  *
6485  * This function fetches stats from the ring considering the atomic operations
6486  * that needs to be performed to read u64 values in 32 bit machine.
6487  */
6488 void
6489 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6490 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6491 {
6492 	unsigned int start;
6493 
6494 	do {
6495 		start = u64_stats_fetch_begin(syncp);
6496 		*pkts = stats.pkts;
6497 		*bytes = stats.bytes;
6498 	} while (u64_stats_fetch_retry(syncp, start));
6499 }
6500 
6501 /**
6502  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6503  * @vsi: the VSI to be updated
6504  * @vsi_stats: the stats struct to be updated
6505  * @rings: rings to work on
6506  * @count: number of rings
6507  */
6508 static void
6509 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6510 			     struct rtnl_link_stats64 *vsi_stats,
6511 			     struct ice_tx_ring **rings, u16 count)
6512 {
6513 	u16 i;
6514 
6515 	for (i = 0; i < count; i++) {
6516 		struct ice_tx_ring *ring;
6517 		u64 pkts = 0, bytes = 0;
6518 
6519 		ring = READ_ONCE(rings[i]);
6520 		if (!ring || !ring->ring_stats)
6521 			continue;
6522 		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6523 					     ring->ring_stats->stats, &pkts,
6524 					     &bytes);
6525 		vsi_stats->tx_packets += pkts;
6526 		vsi_stats->tx_bytes += bytes;
6527 		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6528 		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6529 		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6530 	}
6531 }
6532 
6533 /**
6534  * ice_update_vsi_ring_stats - Update VSI stats counters
6535  * @vsi: the VSI to be updated
6536  */
6537 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6538 {
6539 	struct rtnl_link_stats64 *net_stats, *stats_prev;
6540 	struct rtnl_link_stats64 *vsi_stats;
6541 	u64 pkts, bytes;
6542 	int i;
6543 
6544 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6545 	if (!vsi_stats)
6546 		return;
6547 
6548 	/* reset non-netdev (extended) stats */
6549 	vsi->tx_restart = 0;
6550 	vsi->tx_busy = 0;
6551 	vsi->tx_linearize = 0;
6552 	vsi->rx_buf_failed = 0;
6553 	vsi->rx_page_failed = 0;
6554 
6555 	rcu_read_lock();
6556 
6557 	/* update Tx rings counters */
6558 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6559 				     vsi->num_txq);
6560 
6561 	/* update Rx rings counters */
6562 	ice_for_each_rxq(vsi, i) {
6563 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6564 		struct ice_ring_stats *ring_stats;
6565 
6566 		ring_stats = ring->ring_stats;
6567 		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6568 					     ring_stats->stats, &pkts,
6569 					     &bytes);
6570 		vsi_stats->rx_packets += pkts;
6571 		vsi_stats->rx_bytes += bytes;
6572 		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6573 		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6574 	}
6575 
6576 	/* update XDP Tx rings counters */
6577 	if (ice_is_xdp_ena_vsi(vsi))
6578 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6579 					     vsi->num_xdp_txq);
6580 
6581 	rcu_read_unlock();
6582 
6583 	net_stats = &vsi->net_stats;
6584 	stats_prev = &vsi->net_stats_prev;
6585 
6586 	/* clear prev counters after reset */
6587 	if (vsi_stats->tx_packets < stats_prev->tx_packets ||
6588 	    vsi_stats->rx_packets < stats_prev->rx_packets) {
6589 		stats_prev->tx_packets = 0;
6590 		stats_prev->tx_bytes = 0;
6591 		stats_prev->rx_packets = 0;
6592 		stats_prev->rx_bytes = 0;
6593 	}
6594 
6595 	/* update netdev counters */
6596 	net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6597 	net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6598 	net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6599 	net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6600 
6601 	stats_prev->tx_packets = vsi_stats->tx_packets;
6602 	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6603 	stats_prev->rx_packets = vsi_stats->rx_packets;
6604 	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6605 
6606 	kfree(vsi_stats);
6607 }
6608 
6609 /**
6610  * ice_update_vsi_stats - Update VSI stats counters
6611  * @vsi: the VSI to be updated
6612  */
6613 void ice_update_vsi_stats(struct ice_vsi *vsi)
6614 {
6615 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6616 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6617 	struct ice_pf *pf = vsi->back;
6618 
6619 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6620 	    test_bit(ICE_CFG_BUSY, pf->state))
6621 		return;
6622 
6623 	/* get stats as recorded by Tx/Rx rings */
6624 	ice_update_vsi_ring_stats(vsi);
6625 
6626 	/* get VSI stats as recorded by the hardware */
6627 	ice_update_eth_stats(vsi);
6628 
6629 	cur_ns->tx_errors = cur_es->tx_errors;
6630 	cur_ns->rx_dropped = cur_es->rx_discards;
6631 	cur_ns->tx_dropped = cur_es->tx_discards;
6632 	cur_ns->multicast = cur_es->rx_multicast;
6633 
6634 	/* update some more netdev stats if this is main VSI */
6635 	if (vsi->type == ICE_VSI_PF) {
6636 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6637 		cur_ns->rx_errors = pf->stats.crc_errors +
6638 				    pf->stats.illegal_bytes +
6639 				    pf->stats.rx_len_errors +
6640 				    pf->stats.rx_undersize +
6641 				    pf->hw_csum_rx_error +
6642 				    pf->stats.rx_jabber +
6643 				    pf->stats.rx_fragments +
6644 				    pf->stats.rx_oversize;
6645 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6646 		/* record drops from the port level */
6647 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6648 	}
6649 }
6650 
6651 /**
6652  * ice_update_pf_stats - Update PF port stats counters
6653  * @pf: PF whose stats needs to be updated
6654  */
6655 void ice_update_pf_stats(struct ice_pf *pf)
6656 {
6657 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6658 	struct ice_hw *hw = &pf->hw;
6659 	u16 fd_ctr_base;
6660 	u8 port;
6661 
6662 	port = hw->port_info->lport;
6663 	prev_ps = &pf->stats_prev;
6664 	cur_ps = &pf->stats;
6665 
6666 	if (ice_is_reset_in_progress(pf->state))
6667 		pf->stat_prev_loaded = false;
6668 
6669 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6670 			  &prev_ps->eth.rx_bytes,
6671 			  &cur_ps->eth.rx_bytes);
6672 
6673 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6674 			  &prev_ps->eth.rx_unicast,
6675 			  &cur_ps->eth.rx_unicast);
6676 
6677 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6678 			  &prev_ps->eth.rx_multicast,
6679 			  &cur_ps->eth.rx_multicast);
6680 
6681 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6682 			  &prev_ps->eth.rx_broadcast,
6683 			  &cur_ps->eth.rx_broadcast);
6684 
6685 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6686 			  &prev_ps->eth.rx_discards,
6687 			  &cur_ps->eth.rx_discards);
6688 
6689 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6690 			  &prev_ps->eth.tx_bytes,
6691 			  &cur_ps->eth.tx_bytes);
6692 
6693 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6694 			  &prev_ps->eth.tx_unicast,
6695 			  &cur_ps->eth.tx_unicast);
6696 
6697 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6698 			  &prev_ps->eth.tx_multicast,
6699 			  &cur_ps->eth.tx_multicast);
6700 
6701 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6702 			  &prev_ps->eth.tx_broadcast,
6703 			  &cur_ps->eth.tx_broadcast);
6704 
6705 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6706 			  &prev_ps->tx_dropped_link_down,
6707 			  &cur_ps->tx_dropped_link_down);
6708 
6709 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6710 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6711 
6712 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6713 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6714 
6715 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6716 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6717 
6718 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6719 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6720 
6721 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6722 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6723 
6724 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6725 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6726 
6727 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6728 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6729 
6730 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6731 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6732 
6733 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6734 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6735 
6736 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6737 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6738 
6739 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6740 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6741 
6742 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6743 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6744 
6745 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6746 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6747 
6748 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6749 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6750 
6751 	fd_ctr_base = hw->fd_ctr_base;
6752 
6753 	ice_stat_update40(hw,
6754 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6755 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6756 			  &cur_ps->fd_sb_match);
6757 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6758 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6759 
6760 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6761 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6762 
6763 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6764 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6765 
6766 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6767 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6768 
6769 	ice_update_dcb_stats(pf);
6770 
6771 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6772 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6773 
6774 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6775 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6776 
6777 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6778 			  &prev_ps->mac_local_faults,
6779 			  &cur_ps->mac_local_faults);
6780 
6781 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6782 			  &prev_ps->mac_remote_faults,
6783 			  &cur_ps->mac_remote_faults);
6784 
6785 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6786 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6787 
6788 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6789 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6790 
6791 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6792 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6793 
6794 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6795 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6796 
6797 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6798 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6799 
6800 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6801 
6802 	pf->stat_prev_loaded = true;
6803 }
6804 
6805 /**
6806  * ice_get_stats64 - get statistics for network device structure
6807  * @netdev: network interface device structure
6808  * @stats: main device statistics structure
6809  */
6810 static
6811 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6812 {
6813 	struct ice_netdev_priv *np = netdev_priv(netdev);
6814 	struct rtnl_link_stats64 *vsi_stats;
6815 	struct ice_vsi *vsi = np->vsi;
6816 
6817 	vsi_stats = &vsi->net_stats;
6818 
6819 	if (!vsi->num_txq || !vsi->num_rxq)
6820 		return;
6821 
6822 	/* netdev packet/byte stats come from ring counter. These are obtained
6823 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6824 	 * But, only call the update routine and read the registers if VSI is
6825 	 * not down.
6826 	 */
6827 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
6828 		ice_update_vsi_ring_stats(vsi);
6829 	stats->tx_packets = vsi_stats->tx_packets;
6830 	stats->tx_bytes = vsi_stats->tx_bytes;
6831 	stats->rx_packets = vsi_stats->rx_packets;
6832 	stats->rx_bytes = vsi_stats->rx_bytes;
6833 
6834 	/* The rest of the stats can be read from the hardware but instead we
6835 	 * just return values that the watchdog task has already obtained from
6836 	 * the hardware.
6837 	 */
6838 	stats->multicast = vsi_stats->multicast;
6839 	stats->tx_errors = vsi_stats->tx_errors;
6840 	stats->tx_dropped = vsi_stats->tx_dropped;
6841 	stats->rx_errors = vsi_stats->rx_errors;
6842 	stats->rx_dropped = vsi_stats->rx_dropped;
6843 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6844 	stats->rx_length_errors = vsi_stats->rx_length_errors;
6845 }
6846 
6847 /**
6848  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6849  * @vsi: VSI having NAPI disabled
6850  */
6851 static void ice_napi_disable_all(struct ice_vsi *vsi)
6852 {
6853 	int q_idx;
6854 
6855 	if (!vsi->netdev)
6856 		return;
6857 
6858 	ice_for_each_q_vector(vsi, q_idx) {
6859 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6860 
6861 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6862 			napi_disable(&q_vector->napi);
6863 
6864 		cancel_work_sync(&q_vector->tx.dim.work);
6865 		cancel_work_sync(&q_vector->rx.dim.work);
6866 	}
6867 }
6868 
6869 /**
6870  * ice_down - Shutdown the connection
6871  * @vsi: The VSI being stopped
6872  *
6873  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6874  */
6875 int ice_down(struct ice_vsi *vsi)
6876 {
6877 	int i, tx_err, rx_err, vlan_err = 0;
6878 
6879 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6880 
6881 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6882 		vlan_err = ice_vsi_del_vlan_zero(vsi);
6883 		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6884 		netif_carrier_off(vsi->netdev);
6885 		netif_tx_disable(vsi->netdev);
6886 	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6887 		ice_eswitch_stop_all_tx_queues(vsi->back);
6888 	}
6889 
6890 	ice_vsi_dis_irq(vsi);
6891 
6892 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6893 	if (tx_err)
6894 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6895 			   vsi->vsi_num, tx_err);
6896 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6897 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6898 		if (tx_err)
6899 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6900 				   vsi->vsi_num, tx_err);
6901 	}
6902 
6903 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
6904 	if (rx_err)
6905 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6906 			   vsi->vsi_num, rx_err);
6907 
6908 	ice_napi_disable_all(vsi);
6909 
6910 	ice_for_each_txq(vsi, i)
6911 		ice_clean_tx_ring(vsi->tx_rings[i]);
6912 
6913 	if (ice_is_xdp_ena_vsi(vsi))
6914 		ice_for_each_xdp_txq(vsi, i)
6915 			ice_clean_tx_ring(vsi->xdp_rings[i]);
6916 
6917 	ice_for_each_rxq(vsi, i)
6918 		ice_clean_rx_ring(vsi->rx_rings[i]);
6919 
6920 	if (tx_err || rx_err || vlan_err) {
6921 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6922 			   vsi->vsi_num, vsi->vsw->sw_id);
6923 		return -EIO;
6924 	}
6925 
6926 	return 0;
6927 }
6928 
6929 /**
6930  * ice_down_up - shutdown the VSI connection and bring it up
6931  * @vsi: the VSI to be reconnected
6932  */
6933 int ice_down_up(struct ice_vsi *vsi)
6934 {
6935 	int ret;
6936 
6937 	/* if DOWN already set, nothing to do */
6938 	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
6939 		return 0;
6940 
6941 	ret = ice_down(vsi);
6942 	if (ret)
6943 		return ret;
6944 
6945 	ret = ice_up(vsi);
6946 	if (ret) {
6947 		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
6948 		return ret;
6949 	}
6950 
6951 	return 0;
6952 }
6953 
6954 /**
6955  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
6956  * @vsi: VSI having resources allocated
6957  *
6958  * Return 0 on success, negative on failure
6959  */
6960 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
6961 {
6962 	int i, err = 0;
6963 
6964 	if (!vsi->num_txq) {
6965 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
6966 			vsi->vsi_num);
6967 		return -EINVAL;
6968 	}
6969 
6970 	ice_for_each_txq(vsi, i) {
6971 		struct ice_tx_ring *ring = vsi->tx_rings[i];
6972 
6973 		if (!ring)
6974 			return -EINVAL;
6975 
6976 		if (vsi->netdev)
6977 			ring->netdev = vsi->netdev;
6978 		err = ice_setup_tx_ring(ring);
6979 		if (err)
6980 			break;
6981 	}
6982 
6983 	return err;
6984 }
6985 
6986 /**
6987  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
6988  * @vsi: VSI having resources allocated
6989  *
6990  * Return 0 on success, negative on failure
6991  */
6992 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
6993 {
6994 	int i, err = 0;
6995 
6996 	if (!vsi->num_rxq) {
6997 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
6998 			vsi->vsi_num);
6999 		return -EINVAL;
7000 	}
7001 
7002 	ice_for_each_rxq(vsi, i) {
7003 		struct ice_rx_ring *ring = vsi->rx_rings[i];
7004 
7005 		if (!ring)
7006 			return -EINVAL;
7007 
7008 		if (vsi->netdev)
7009 			ring->netdev = vsi->netdev;
7010 		err = ice_setup_rx_ring(ring);
7011 		if (err)
7012 			break;
7013 	}
7014 
7015 	return err;
7016 }
7017 
7018 /**
7019  * ice_vsi_open_ctrl - open control VSI for use
7020  * @vsi: the VSI to open
7021  *
7022  * Initialization of the Control VSI
7023  *
7024  * Returns 0 on success, negative value on error
7025  */
7026 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7027 {
7028 	char int_name[ICE_INT_NAME_STR_LEN];
7029 	struct ice_pf *pf = vsi->back;
7030 	struct device *dev;
7031 	int err;
7032 
7033 	dev = ice_pf_to_dev(pf);
7034 	/* allocate descriptors */
7035 	err = ice_vsi_setup_tx_rings(vsi);
7036 	if (err)
7037 		goto err_setup_tx;
7038 
7039 	err = ice_vsi_setup_rx_rings(vsi);
7040 	if (err)
7041 		goto err_setup_rx;
7042 
7043 	err = ice_vsi_cfg_lan(vsi);
7044 	if (err)
7045 		goto err_setup_rx;
7046 
7047 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7048 		 dev_driver_string(dev), dev_name(dev));
7049 	err = ice_vsi_req_irq_msix(vsi, int_name);
7050 	if (err)
7051 		goto err_setup_rx;
7052 
7053 	ice_vsi_cfg_msix(vsi);
7054 
7055 	err = ice_vsi_start_all_rx_rings(vsi);
7056 	if (err)
7057 		goto err_up_complete;
7058 
7059 	clear_bit(ICE_VSI_DOWN, vsi->state);
7060 	ice_vsi_ena_irq(vsi);
7061 
7062 	return 0;
7063 
7064 err_up_complete:
7065 	ice_down(vsi);
7066 err_setup_rx:
7067 	ice_vsi_free_rx_rings(vsi);
7068 err_setup_tx:
7069 	ice_vsi_free_tx_rings(vsi);
7070 
7071 	return err;
7072 }
7073 
7074 /**
7075  * ice_vsi_open - Called when a network interface is made active
7076  * @vsi: the VSI to open
7077  *
7078  * Initialization of the VSI
7079  *
7080  * Returns 0 on success, negative value on error
7081  */
7082 int ice_vsi_open(struct ice_vsi *vsi)
7083 {
7084 	char int_name[ICE_INT_NAME_STR_LEN];
7085 	struct ice_pf *pf = vsi->back;
7086 	int err;
7087 
7088 	/* allocate descriptors */
7089 	err = ice_vsi_setup_tx_rings(vsi);
7090 	if (err)
7091 		goto err_setup_tx;
7092 
7093 	err = ice_vsi_setup_rx_rings(vsi);
7094 	if (err)
7095 		goto err_setup_rx;
7096 
7097 	err = ice_vsi_cfg_lan(vsi);
7098 	if (err)
7099 		goto err_setup_rx;
7100 
7101 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7102 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7103 	err = ice_vsi_req_irq_msix(vsi, int_name);
7104 	if (err)
7105 		goto err_setup_rx;
7106 
7107 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7108 
7109 	if (vsi->type == ICE_VSI_PF) {
7110 		/* Notify the stack of the actual queue counts. */
7111 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7112 		if (err)
7113 			goto err_set_qs;
7114 
7115 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7116 		if (err)
7117 			goto err_set_qs;
7118 	}
7119 
7120 	err = ice_up_complete(vsi);
7121 	if (err)
7122 		goto err_up_complete;
7123 
7124 	return 0;
7125 
7126 err_up_complete:
7127 	ice_down(vsi);
7128 err_set_qs:
7129 	ice_vsi_free_irq(vsi);
7130 err_setup_rx:
7131 	ice_vsi_free_rx_rings(vsi);
7132 err_setup_tx:
7133 	ice_vsi_free_tx_rings(vsi);
7134 
7135 	return err;
7136 }
7137 
7138 /**
7139  * ice_vsi_release_all - Delete all VSIs
7140  * @pf: PF from which all VSIs are being removed
7141  */
7142 static void ice_vsi_release_all(struct ice_pf *pf)
7143 {
7144 	int err, i;
7145 
7146 	if (!pf->vsi)
7147 		return;
7148 
7149 	ice_for_each_vsi(pf, i) {
7150 		if (!pf->vsi[i])
7151 			continue;
7152 
7153 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7154 			continue;
7155 
7156 		err = ice_vsi_release(pf->vsi[i]);
7157 		if (err)
7158 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7159 				i, err, pf->vsi[i]->vsi_num);
7160 	}
7161 }
7162 
7163 /**
7164  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7165  * @pf: pointer to the PF instance
7166  * @type: VSI type to rebuild
7167  *
7168  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7169  */
7170 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7171 {
7172 	struct device *dev = ice_pf_to_dev(pf);
7173 	int i, err;
7174 
7175 	ice_for_each_vsi(pf, i) {
7176 		struct ice_vsi *vsi = pf->vsi[i];
7177 
7178 		if (!vsi || vsi->type != type)
7179 			continue;
7180 
7181 		/* rebuild the VSI */
7182 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7183 		if (err) {
7184 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7185 				err, vsi->idx, ice_vsi_type_str(type));
7186 			return err;
7187 		}
7188 
7189 		/* replay filters for the VSI */
7190 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7191 		if (err) {
7192 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7193 				err, vsi->idx, ice_vsi_type_str(type));
7194 			return err;
7195 		}
7196 
7197 		/* Re-map HW VSI number, using VSI handle that has been
7198 		 * previously validated in ice_replay_vsi() call above
7199 		 */
7200 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7201 
7202 		/* enable the VSI */
7203 		err = ice_ena_vsi(vsi, false);
7204 		if (err) {
7205 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7206 				err, vsi->idx, ice_vsi_type_str(type));
7207 			return err;
7208 		}
7209 
7210 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7211 			 ice_vsi_type_str(type));
7212 	}
7213 
7214 	return 0;
7215 }
7216 
7217 /**
7218  * ice_update_pf_netdev_link - Update PF netdev link status
7219  * @pf: pointer to the PF instance
7220  */
7221 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7222 {
7223 	bool link_up;
7224 	int i;
7225 
7226 	ice_for_each_vsi(pf, i) {
7227 		struct ice_vsi *vsi = pf->vsi[i];
7228 
7229 		if (!vsi || vsi->type != ICE_VSI_PF)
7230 			return;
7231 
7232 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7233 		if (link_up) {
7234 			netif_carrier_on(pf->vsi[i]->netdev);
7235 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7236 		} else {
7237 			netif_carrier_off(pf->vsi[i]->netdev);
7238 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7239 		}
7240 	}
7241 }
7242 
7243 /**
7244  * ice_rebuild - rebuild after reset
7245  * @pf: PF to rebuild
7246  * @reset_type: type of reset
7247  *
7248  * Do not rebuild VF VSI in this flow because that is already handled via
7249  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7250  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7251  * to reset/rebuild all the VF VSI twice.
7252  */
7253 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7254 {
7255 	struct device *dev = ice_pf_to_dev(pf);
7256 	struct ice_hw *hw = &pf->hw;
7257 	bool dvm;
7258 	int err;
7259 
7260 	if (test_bit(ICE_DOWN, pf->state))
7261 		goto clear_recovery;
7262 
7263 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7264 
7265 #define ICE_EMP_RESET_SLEEP_MS 5000
7266 	if (reset_type == ICE_RESET_EMPR) {
7267 		/* If an EMP reset has occurred, any previously pending flash
7268 		 * update will have completed. We no longer know whether or
7269 		 * not the NVM update EMP reset is restricted.
7270 		 */
7271 		pf->fw_emp_reset_disabled = false;
7272 
7273 		msleep(ICE_EMP_RESET_SLEEP_MS);
7274 	}
7275 
7276 	err = ice_init_all_ctrlq(hw);
7277 	if (err) {
7278 		dev_err(dev, "control queues init failed %d\n", err);
7279 		goto err_init_ctrlq;
7280 	}
7281 
7282 	/* if DDP was previously loaded successfully */
7283 	if (!ice_is_safe_mode(pf)) {
7284 		/* reload the SW DB of filter tables */
7285 		if (reset_type == ICE_RESET_PFR)
7286 			ice_fill_blk_tbls(hw);
7287 		else
7288 			/* Reload DDP Package after CORER/GLOBR reset */
7289 			ice_load_pkg(NULL, pf);
7290 	}
7291 
7292 	err = ice_clear_pf_cfg(hw);
7293 	if (err) {
7294 		dev_err(dev, "clear PF configuration failed %d\n", err);
7295 		goto err_init_ctrlq;
7296 	}
7297 
7298 	ice_clear_pxe_mode(hw);
7299 
7300 	err = ice_init_nvm(hw);
7301 	if (err) {
7302 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7303 		goto err_init_ctrlq;
7304 	}
7305 
7306 	err = ice_get_caps(hw);
7307 	if (err) {
7308 		dev_err(dev, "ice_get_caps failed %d\n", err);
7309 		goto err_init_ctrlq;
7310 	}
7311 
7312 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7313 	if (err) {
7314 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7315 		goto err_init_ctrlq;
7316 	}
7317 
7318 	dvm = ice_is_dvm_ena(hw);
7319 
7320 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7321 	if (err)
7322 		goto err_init_ctrlq;
7323 
7324 	err = ice_sched_init_port(hw->port_info);
7325 	if (err)
7326 		goto err_sched_init_port;
7327 
7328 	/* start misc vector */
7329 	err = ice_req_irq_msix_misc(pf);
7330 	if (err) {
7331 		dev_err(dev, "misc vector setup failed: %d\n", err);
7332 		goto err_sched_init_port;
7333 	}
7334 
7335 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7336 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7337 		if (!rd32(hw, PFQF_FD_SIZE)) {
7338 			u16 unused, guar, b_effort;
7339 
7340 			guar = hw->func_caps.fd_fltr_guar;
7341 			b_effort = hw->func_caps.fd_fltr_best_effort;
7342 
7343 			/* force guaranteed filter pool for PF */
7344 			ice_alloc_fd_guar_item(hw, &unused, guar);
7345 			/* force shared filter pool for PF */
7346 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7347 		}
7348 	}
7349 
7350 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7351 		ice_dcb_rebuild(pf);
7352 
7353 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7354 	 * the VSI rebuild. If not, this causes the PTP link status events to
7355 	 * fail.
7356 	 */
7357 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7358 		ice_ptp_reset(pf);
7359 
7360 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7361 		ice_gnss_init(pf);
7362 
7363 	/* rebuild PF VSI */
7364 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7365 	if (err) {
7366 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7367 		goto err_vsi_rebuild;
7368 	}
7369 
7370 	/* configure PTP timestamping after VSI rebuild */
7371 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7372 		ice_ptp_cfg_timestamp(pf, false);
7373 
7374 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
7375 	if (err) {
7376 		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
7377 		goto err_vsi_rebuild;
7378 	}
7379 
7380 	if (reset_type == ICE_RESET_PFR) {
7381 		err = ice_rebuild_channels(pf);
7382 		if (err) {
7383 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7384 				err);
7385 			goto err_vsi_rebuild;
7386 		}
7387 	}
7388 
7389 	/* If Flow Director is active */
7390 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7391 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7392 		if (err) {
7393 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7394 			goto err_vsi_rebuild;
7395 		}
7396 
7397 		/* replay HW Flow Director recipes */
7398 		if (hw->fdir_prof)
7399 			ice_fdir_replay_flows(hw);
7400 
7401 		/* replay Flow Director filters */
7402 		ice_fdir_replay_fltrs(pf);
7403 
7404 		ice_rebuild_arfs(pf);
7405 	}
7406 
7407 	ice_update_pf_netdev_link(pf);
7408 
7409 	/* tell the firmware we are up */
7410 	err = ice_send_version(pf);
7411 	if (err) {
7412 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7413 			err);
7414 		goto err_vsi_rebuild;
7415 	}
7416 
7417 	ice_replay_post(hw);
7418 
7419 	/* if we get here, reset flow is successful */
7420 	clear_bit(ICE_RESET_FAILED, pf->state);
7421 
7422 	ice_plug_aux_dev(pf);
7423 	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7424 		ice_lag_rebuild(pf);
7425 	return;
7426 
7427 err_vsi_rebuild:
7428 err_sched_init_port:
7429 	ice_sched_cleanup_all(hw);
7430 err_init_ctrlq:
7431 	ice_shutdown_all_ctrlq(hw);
7432 	set_bit(ICE_RESET_FAILED, pf->state);
7433 clear_recovery:
7434 	/* set this bit in PF state to control service task scheduling */
7435 	set_bit(ICE_NEEDS_RESTART, pf->state);
7436 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7437 }
7438 
7439 /**
7440  * ice_change_mtu - NDO callback to change the MTU
7441  * @netdev: network interface device structure
7442  * @new_mtu: new value for maximum frame size
7443  *
7444  * Returns 0 on success, negative on failure
7445  */
7446 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7447 {
7448 	struct ice_netdev_priv *np = netdev_priv(netdev);
7449 	struct ice_vsi *vsi = np->vsi;
7450 	struct ice_pf *pf = vsi->back;
7451 	struct bpf_prog *prog;
7452 	u8 count = 0;
7453 	int err = 0;
7454 
7455 	if (new_mtu == (int)netdev->mtu) {
7456 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7457 		return 0;
7458 	}
7459 
7460 	prog = vsi->xdp_prog;
7461 	if (prog && !prog->aux->xdp_has_frags) {
7462 		int frame_size = ice_max_xdp_frame_size(vsi);
7463 
7464 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7465 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7466 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7467 			return -EINVAL;
7468 		}
7469 	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7470 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7471 			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7472 				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7473 			return -EINVAL;
7474 		}
7475 	}
7476 
7477 	/* if a reset is in progress, wait for some time for it to complete */
7478 	do {
7479 		if (ice_is_reset_in_progress(pf->state)) {
7480 			count++;
7481 			usleep_range(1000, 2000);
7482 		} else {
7483 			break;
7484 		}
7485 
7486 	} while (count < 100);
7487 
7488 	if (count == 100) {
7489 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7490 		return -EBUSY;
7491 	}
7492 
7493 	netdev->mtu = (unsigned int)new_mtu;
7494 	err = ice_down_up(vsi);
7495 	if (err)
7496 		return err;
7497 
7498 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7499 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7500 
7501 	return err;
7502 }
7503 
7504 /**
7505  * ice_eth_ioctl - Access the hwtstamp interface
7506  * @netdev: network interface device structure
7507  * @ifr: interface request data
7508  * @cmd: ioctl command
7509  */
7510 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7511 {
7512 	struct ice_netdev_priv *np = netdev_priv(netdev);
7513 	struct ice_pf *pf = np->vsi->back;
7514 
7515 	switch (cmd) {
7516 	case SIOCGHWTSTAMP:
7517 		return ice_ptp_get_ts_config(pf, ifr);
7518 	case SIOCSHWTSTAMP:
7519 		return ice_ptp_set_ts_config(pf, ifr);
7520 	default:
7521 		return -EOPNOTSUPP;
7522 	}
7523 }
7524 
7525 /**
7526  * ice_aq_str - convert AQ err code to a string
7527  * @aq_err: the AQ error code to convert
7528  */
7529 const char *ice_aq_str(enum ice_aq_err aq_err)
7530 {
7531 	switch (aq_err) {
7532 	case ICE_AQ_RC_OK:
7533 		return "OK";
7534 	case ICE_AQ_RC_EPERM:
7535 		return "ICE_AQ_RC_EPERM";
7536 	case ICE_AQ_RC_ENOENT:
7537 		return "ICE_AQ_RC_ENOENT";
7538 	case ICE_AQ_RC_ENOMEM:
7539 		return "ICE_AQ_RC_ENOMEM";
7540 	case ICE_AQ_RC_EBUSY:
7541 		return "ICE_AQ_RC_EBUSY";
7542 	case ICE_AQ_RC_EEXIST:
7543 		return "ICE_AQ_RC_EEXIST";
7544 	case ICE_AQ_RC_EINVAL:
7545 		return "ICE_AQ_RC_EINVAL";
7546 	case ICE_AQ_RC_ENOSPC:
7547 		return "ICE_AQ_RC_ENOSPC";
7548 	case ICE_AQ_RC_ENOSYS:
7549 		return "ICE_AQ_RC_ENOSYS";
7550 	case ICE_AQ_RC_EMODE:
7551 		return "ICE_AQ_RC_EMODE";
7552 	case ICE_AQ_RC_ENOSEC:
7553 		return "ICE_AQ_RC_ENOSEC";
7554 	case ICE_AQ_RC_EBADSIG:
7555 		return "ICE_AQ_RC_EBADSIG";
7556 	case ICE_AQ_RC_ESVN:
7557 		return "ICE_AQ_RC_ESVN";
7558 	case ICE_AQ_RC_EBADMAN:
7559 		return "ICE_AQ_RC_EBADMAN";
7560 	case ICE_AQ_RC_EBADBUF:
7561 		return "ICE_AQ_RC_EBADBUF";
7562 	}
7563 
7564 	return "ICE_AQ_RC_UNKNOWN";
7565 }
7566 
7567 /**
7568  * ice_set_rss_lut - Set RSS LUT
7569  * @vsi: Pointer to VSI structure
7570  * @lut: Lookup table
7571  * @lut_size: Lookup table size
7572  *
7573  * Returns 0 on success, negative on failure
7574  */
7575 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7576 {
7577 	struct ice_aq_get_set_rss_lut_params params = {};
7578 	struct ice_hw *hw = &vsi->back->hw;
7579 	int status;
7580 
7581 	if (!lut)
7582 		return -EINVAL;
7583 
7584 	params.vsi_handle = vsi->idx;
7585 	params.lut_size = lut_size;
7586 	params.lut_type = vsi->rss_lut_type;
7587 	params.lut = lut;
7588 
7589 	status = ice_aq_set_rss_lut(hw, &params);
7590 	if (status)
7591 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7592 			status, ice_aq_str(hw->adminq.sq_last_status));
7593 
7594 	return status;
7595 }
7596 
7597 /**
7598  * ice_set_rss_key - Set RSS key
7599  * @vsi: Pointer to the VSI structure
7600  * @seed: RSS hash seed
7601  *
7602  * Returns 0 on success, negative on failure
7603  */
7604 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7605 {
7606 	struct ice_hw *hw = &vsi->back->hw;
7607 	int status;
7608 
7609 	if (!seed)
7610 		return -EINVAL;
7611 
7612 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7613 	if (status)
7614 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7615 			status, ice_aq_str(hw->adminq.sq_last_status));
7616 
7617 	return status;
7618 }
7619 
7620 /**
7621  * ice_get_rss_lut - Get RSS LUT
7622  * @vsi: Pointer to VSI structure
7623  * @lut: Buffer to store the lookup table entries
7624  * @lut_size: Size of buffer to store the lookup table entries
7625  *
7626  * Returns 0 on success, negative on failure
7627  */
7628 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7629 {
7630 	struct ice_aq_get_set_rss_lut_params params = {};
7631 	struct ice_hw *hw = &vsi->back->hw;
7632 	int status;
7633 
7634 	if (!lut)
7635 		return -EINVAL;
7636 
7637 	params.vsi_handle = vsi->idx;
7638 	params.lut_size = lut_size;
7639 	params.lut_type = vsi->rss_lut_type;
7640 	params.lut = lut;
7641 
7642 	status = ice_aq_get_rss_lut(hw, &params);
7643 	if (status)
7644 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7645 			status, ice_aq_str(hw->adminq.sq_last_status));
7646 
7647 	return status;
7648 }
7649 
7650 /**
7651  * ice_get_rss_key - Get RSS key
7652  * @vsi: Pointer to VSI structure
7653  * @seed: Buffer to store the key in
7654  *
7655  * Returns 0 on success, negative on failure
7656  */
7657 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7658 {
7659 	struct ice_hw *hw = &vsi->back->hw;
7660 	int status;
7661 
7662 	if (!seed)
7663 		return -EINVAL;
7664 
7665 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7666 	if (status)
7667 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7668 			status, ice_aq_str(hw->adminq.sq_last_status));
7669 
7670 	return status;
7671 }
7672 
7673 /**
7674  * ice_bridge_getlink - Get the hardware bridge mode
7675  * @skb: skb buff
7676  * @pid: process ID
7677  * @seq: RTNL message seq
7678  * @dev: the netdev being configured
7679  * @filter_mask: filter mask passed in
7680  * @nlflags: netlink flags passed in
7681  *
7682  * Return the bridge mode (VEB/VEPA)
7683  */
7684 static int
7685 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7686 		   struct net_device *dev, u32 filter_mask, int nlflags)
7687 {
7688 	struct ice_netdev_priv *np = netdev_priv(dev);
7689 	struct ice_vsi *vsi = np->vsi;
7690 	struct ice_pf *pf = vsi->back;
7691 	u16 bmode;
7692 
7693 	bmode = pf->first_sw->bridge_mode;
7694 
7695 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7696 				       filter_mask, NULL);
7697 }
7698 
7699 /**
7700  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7701  * @vsi: Pointer to VSI structure
7702  * @bmode: Hardware bridge mode (VEB/VEPA)
7703  *
7704  * Returns 0 on success, negative on failure
7705  */
7706 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7707 {
7708 	struct ice_aqc_vsi_props *vsi_props;
7709 	struct ice_hw *hw = &vsi->back->hw;
7710 	struct ice_vsi_ctx *ctxt;
7711 	int ret;
7712 
7713 	vsi_props = &vsi->info;
7714 
7715 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7716 	if (!ctxt)
7717 		return -ENOMEM;
7718 
7719 	ctxt->info = vsi->info;
7720 
7721 	if (bmode == BRIDGE_MODE_VEB)
7722 		/* change from VEPA to VEB mode */
7723 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7724 	else
7725 		/* change from VEB to VEPA mode */
7726 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7727 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7728 
7729 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7730 	if (ret) {
7731 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7732 			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7733 		goto out;
7734 	}
7735 	/* Update sw flags for book keeping */
7736 	vsi_props->sw_flags = ctxt->info.sw_flags;
7737 
7738 out:
7739 	kfree(ctxt);
7740 	return ret;
7741 }
7742 
7743 /**
7744  * ice_bridge_setlink - Set the hardware bridge mode
7745  * @dev: the netdev being configured
7746  * @nlh: RTNL message
7747  * @flags: bridge setlink flags
7748  * @extack: netlink extended ack
7749  *
7750  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7751  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7752  * not already set for all VSIs connected to this switch. And also update the
7753  * unicast switch filter rules for the corresponding switch of the netdev.
7754  */
7755 static int
7756 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7757 		   u16 __always_unused flags,
7758 		   struct netlink_ext_ack __always_unused *extack)
7759 {
7760 	struct ice_netdev_priv *np = netdev_priv(dev);
7761 	struct ice_pf *pf = np->vsi->back;
7762 	struct nlattr *attr, *br_spec;
7763 	struct ice_hw *hw = &pf->hw;
7764 	struct ice_sw *pf_sw;
7765 	int rem, v, err = 0;
7766 
7767 	pf_sw = pf->first_sw;
7768 	/* find the attribute in the netlink message */
7769 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7770 
7771 	nla_for_each_nested(attr, br_spec, rem) {
7772 		__u16 mode;
7773 
7774 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
7775 			continue;
7776 		mode = nla_get_u16(attr);
7777 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7778 			return -EINVAL;
7779 		/* Continue  if bridge mode is not being flipped */
7780 		if (mode == pf_sw->bridge_mode)
7781 			continue;
7782 		/* Iterates through the PF VSI list and update the loopback
7783 		 * mode of the VSI
7784 		 */
7785 		ice_for_each_vsi(pf, v) {
7786 			if (!pf->vsi[v])
7787 				continue;
7788 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7789 			if (err)
7790 				return err;
7791 		}
7792 
7793 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7794 		/* Update the unicast switch filter rules for the corresponding
7795 		 * switch of the netdev
7796 		 */
7797 		err = ice_update_sw_rule_bridge_mode(hw);
7798 		if (err) {
7799 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7800 				   mode, err,
7801 				   ice_aq_str(hw->adminq.sq_last_status));
7802 			/* revert hw->evb_veb */
7803 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7804 			return err;
7805 		}
7806 
7807 		pf_sw->bridge_mode = mode;
7808 	}
7809 
7810 	return 0;
7811 }
7812 
7813 /**
7814  * ice_tx_timeout - Respond to a Tx Hang
7815  * @netdev: network interface device structure
7816  * @txqueue: Tx queue
7817  */
7818 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7819 {
7820 	struct ice_netdev_priv *np = netdev_priv(netdev);
7821 	struct ice_tx_ring *tx_ring = NULL;
7822 	struct ice_vsi *vsi = np->vsi;
7823 	struct ice_pf *pf = vsi->back;
7824 	u32 i;
7825 
7826 	pf->tx_timeout_count++;
7827 
7828 	/* Check if PFC is enabled for the TC to which the queue belongs
7829 	 * to. If yes then Tx timeout is not caused by a hung queue, no
7830 	 * need to reset and rebuild
7831 	 */
7832 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7833 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7834 			 txqueue);
7835 		return;
7836 	}
7837 
7838 	/* now that we have an index, find the tx_ring struct */
7839 	ice_for_each_txq(vsi, i)
7840 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7841 			if (txqueue == vsi->tx_rings[i]->q_index) {
7842 				tx_ring = vsi->tx_rings[i];
7843 				break;
7844 			}
7845 
7846 	/* Reset recovery level if enough time has elapsed after last timeout.
7847 	 * Also ensure no new reset action happens before next timeout period.
7848 	 */
7849 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7850 		pf->tx_timeout_recovery_level = 1;
7851 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7852 				       netdev->watchdog_timeo)))
7853 		return;
7854 
7855 	if (tx_ring) {
7856 		struct ice_hw *hw = &pf->hw;
7857 		u32 head, val = 0;
7858 
7859 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7860 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7861 		/* Read interrupt register */
7862 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7863 
7864 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7865 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7866 			    head, tx_ring->next_to_use, val);
7867 	}
7868 
7869 	pf->tx_timeout_last_recovery = jiffies;
7870 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7871 		    pf->tx_timeout_recovery_level, txqueue);
7872 
7873 	switch (pf->tx_timeout_recovery_level) {
7874 	case 1:
7875 		set_bit(ICE_PFR_REQ, pf->state);
7876 		break;
7877 	case 2:
7878 		set_bit(ICE_CORER_REQ, pf->state);
7879 		break;
7880 	case 3:
7881 		set_bit(ICE_GLOBR_REQ, pf->state);
7882 		break;
7883 	default:
7884 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7885 		set_bit(ICE_DOWN, pf->state);
7886 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7887 		set_bit(ICE_SERVICE_DIS, pf->state);
7888 		break;
7889 	}
7890 
7891 	ice_service_task_schedule(pf);
7892 	pf->tx_timeout_recovery_level++;
7893 }
7894 
7895 /**
7896  * ice_setup_tc_cls_flower - flower classifier offloads
7897  * @np: net device to configure
7898  * @filter_dev: device on which filter is added
7899  * @cls_flower: offload data
7900  */
7901 static int
7902 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7903 			struct net_device *filter_dev,
7904 			struct flow_cls_offload *cls_flower)
7905 {
7906 	struct ice_vsi *vsi = np->vsi;
7907 
7908 	if (cls_flower->common.chain_index)
7909 		return -EOPNOTSUPP;
7910 
7911 	switch (cls_flower->command) {
7912 	case FLOW_CLS_REPLACE:
7913 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7914 	case FLOW_CLS_DESTROY:
7915 		return ice_del_cls_flower(vsi, cls_flower);
7916 	default:
7917 		return -EINVAL;
7918 	}
7919 }
7920 
7921 /**
7922  * ice_setup_tc_block_cb - callback handler registered for TC block
7923  * @type: TC SETUP type
7924  * @type_data: TC flower offload data that contains user input
7925  * @cb_priv: netdev private data
7926  */
7927 static int
7928 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7929 {
7930 	struct ice_netdev_priv *np = cb_priv;
7931 
7932 	switch (type) {
7933 	case TC_SETUP_CLSFLOWER:
7934 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7935 					       type_data);
7936 	default:
7937 		return -EOPNOTSUPP;
7938 	}
7939 }
7940 
7941 /**
7942  * ice_validate_mqprio_qopt - Validate TCF input parameters
7943  * @vsi: Pointer to VSI
7944  * @mqprio_qopt: input parameters for mqprio queue configuration
7945  *
7946  * This function validates MQPRIO params, such as qcount (power of 2 wherever
7947  * needed), and make sure user doesn't specify qcount and BW rate limit
7948  * for TCs, which are more than "num_tc"
7949  */
7950 static int
7951 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
7952 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
7953 {
7954 	int non_power_of_2_qcount = 0;
7955 	struct ice_pf *pf = vsi->back;
7956 	int max_rss_q_cnt = 0;
7957 	u64 sum_min_rate = 0;
7958 	struct device *dev;
7959 	int i, speed;
7960 	u8 num_tc;
7961 
7962 	if (vsi->type != ICE_VSI_PF)
7963 		return -EINVAL;
7964 
7965 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7966 	    mqprio_qopt->qopt.num_tc < 1 ||
7967 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
7968 		return -EINVAL;
7969 
7970 	dev = ice_pf_to_dev(pf);
7971 	vsi->ch_rss_size = 0;
7972 	num_tc = mqprio_qopt->qopt.num_tc;
7973 	speed = ice_get_link_speed_kbps(vsi);
7974 
7975 	for (i = 0; num_tc; i++) {
7976 		int qcount = mqprio_qopt->qopt.count[i];
7977 		u64 max_rate, min_rate, rem;
7978 
7979 		if (!qcount)
7980 			return -EINVAL;
7981 
7982 		if (is_power_of_2(qcount)) {
7983 			if (non_power_of_2_qcount &&
7984 			    qcount > non_power_of_2_qcount) {
7985 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
7986 					qcount, non_power_of_2_qcount);
7987 				return -EINVAL;
7988 			}
7989 			if (qcount > max_rss_q_cnt)
7990 				max_rss_q_cnt = qcount;
7991 		} else {
7992 			if (non_power_of_2_qcount &&
7993 			    qcount != non_power_of_2_qcount) {
7994 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
7995 					qcount, non_power_of_2_qcount);
7996 				return -EINVAL;
7997 			}
7998 			if (qcount < max_rss_q_cnt) {
7999 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8000 					qcount, max_rss_q_cnt);
8001 				return -EINVAL;
8002 			}
8003 			max_rss_q_cnt = qcount;
8004 			non_power_of_2_qcount = qcount;
8005 		}
8006 
8007 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8008 		 * converts the bandwidth rate limit into Bytes/s when
8009 		 * passing it down to the driver. So convert input bandwidth
8010 		 * from Bytes/s to Kbps
8011 		 */
8012 		max_rate = mqprio_qopt->max_rate[i];
8013 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8014 
8015 		/* min_rate is minimum guaranteed rate and it can't be zero */
8016 		min_rate = mqprio_qopt->min_rate[i];
8017 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8018 		sum_min_rate += min_rate;
8019 
8020 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8021 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8022 				min_rate, ICE_MIN_BW_LIMIT);
8023 			return -EINVAL;
8024 		}
8025 
8026 		if (max_rate && max_rate > speed) {
8027 			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8028 				i, max_rate, speed);
8029 			return -EINVAL;
8030 		}
8031 
8032 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8033 		if (rem) {
8034 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8035 				i, ICE_MIN_BW_LIMIT);
8036 			return -EINVAL;
8037 		}
8038 
8039 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8040 		if (rem) {
8041 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8042 				i, ICE_MIN_BW_LIMIT);
8043 			return -EINVAL;
8044 		}
8045 
8046 		/* min_rate can't be more than max_rate, except when max_rate
8047 		 * is zero (implies max_rate sought is max line rate). In such
8048 		 * a case min_rate can be more than max.
8049 		 */
8050 		if (max_rate && min_rate > max_rate) {
8051 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8052 				min_rate, max_rate);
8053 			return -EINVAL;
8054 		}
8055 
8056 		if (i >= mqprio_qopt->qopt.num_tc - 1)
8057 			break;
8058 		if (mqprio_qopt->qopt.offset[i + 1] !=
8059 		    (mqprio_qopt->qopt.offset[i] + qcount))
8060 			return -EINVAL;
8061 	}
8062 	if (vsi->num_rxq <
8063 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8064 		return -EINVAL;
8065 	if (vsi->num_txq <
8066 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8067 		return -EINVAL;
8068 
8069 	if (sum_min_rate && sum_min_rate > (u64)speed) {
8070 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8071 			sum_min_rate, speed);
8072 		return -EINVAL;
8073 	}
8074 
8075 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8076 	vsi->ch_rss_size = max_rss_q_cnt;
8077 
8078 	return 0;
8079 }
8080 
8081 /**
8082  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8083  * @pf: ptr to PF device
8084  * @vsi: ptr to VSI
8085  */
8086 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8087 {
8088 	struct device *dev = ice_pf_to_dev(pf);
8089 	bool added = false;
8090 	struct ice_hw *hw;
8091 	int flow;
8092 
8093 	if (!(vsi->num_gfltr || vsi->num_bfltr))
8094 		return -EINVAL;
8095 
8096 	hw = &pf->hw;
8097 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8098 		struct ice_fd_hw_prof *prof;
8099 		int tun, status;
8100 		u64 entry_h;
8101 
8102 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8103 		      hw->fdir_prof[flow]->cnt))
8104 			continue;
8105 
8106 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8107 			enum ice_flow_priority prio;
8108 			u64 prof_id;
8109 
8110 			/* add this VSI to FDir profile for this flow */
8111 			prio = ICE_FLOW_PRIO_NORMAL;
8112 			prof = hw->fdir_prof[flow];
8113 			prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
8114 			status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
8115 						    prof->vsi_h[0], vsi->idx,
8116 						    prio, prof->fdir_seg[tun],
8117 						    &entry_h);
8118 			if (status) {
8119 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8120 					vsi->idx, flow);
8121 				continue;
8122 			}
8123 
8124 			prof->entry_h[prof->cnt][tun] = entry_h;
8125 		}
8126 
8127 		/* store VSI for filter replay and delete */
8128 		prof->vsi_h[prof->cnt] = vsi->idx;
8129 		prof->cnt++;
8130 
8131 		added = true;
8132 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8133 			flow);
8134 	}
8135 
8136 	if (!added)
8137 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8138 
8139 	return 0;
8140 }
8141 
8142 /**
8143  * ice_add_channel - add a channel by adding VSI
8144  * @pf: ptr to PF device
8145  * @sw_id: underlying HW switching element ID
8146  * @ch: ptr to channel structure
8147  *
8148  * Add a channel (VSI) using add_vsi and queue_map
8149  */
8150 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8151 {
8152 	struct device *dev = ice_pf_to_dev(pf);
8153 	struct ice_vsi *vsi;
8154 
8155 	if (ch->type != ICE_VSI_CHNL) {
8156 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8157 		return -EINVAL;
8158 	}
8159 
8160 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8161 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8162 		dev_err(dev, "create chnl VSI failure\n");
8163 		return -EINVAL;
8164 	}
8165 
8166 	ice_add_vsi_to_fdir(pf, vsi);
8167 
8168 	ch->sw_id = sw_id;
8169 	ch->vsi_num = vsi->vsi_num;
8170 	ch->info.mapping_flags = vsi->info.mapping_flags;
8171 	ch->ch_vsi = vsi;
8172 	/* set the back pointer of channel for newly created VSI */
8173 	vsi->ch = ch;
8174 
8175 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8176 	       sizeof(vsi->info.q_mapping));
8177 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8178 	       sizeof(vsi->info.tc_mapping));
8179 
8180 	return 0;
8181 }
8182 
8183 /**
8184  * ice_chnl_cfg_res
8185  * @vsi: the VSI being setup
8186  * @ch: ptr to channel structure
8187  *
8188  * Configure channel specific resources such as rings, vector.
8189  */
8190 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8191 {
8192 	int i;
8193 
8194 	for (i = 0; i < ch->num_txq; i++) {
8195 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8196 		struct ice_ring_container *rc;
8197 		struct ice_tx_ring *tx_ring;
8198 		struct ice_rx_ring *rx_ring;
8199 
8200 		tx_ring = vsi->tx_rings[ch->base_q + i];
8201 		rx_ring = vsi->rx_rings[ch->base_q + i];
8202 		if (!tx_ring || !rx_ring)
8203 			continue;
8204 
8205 		/* setup ring being channel enabled */
8206 		tx_ring->ch = ch;
8207 		rx_ring->ch = ch;
8208 
8209 		/* following code block sets up vector specific attributes */
8210 		tx_q_vector = tx_ring->q_vector;
8211 		rx_q_vector = rx_ring->q_vector;
8212 		if (!tx_q_vector && !rx_q_vector)
8213 			continue;
8214 
8215 		if (tx_q_vector) {
8216 			tx_q_vector->ch = ch;
8217 			/* setup Tx and Rx ITR setting if DIM is off */
8218 			rc = &tx_q_vector->tx;
8219 			if (!ITR_IS_DYNAMIC(rc))
8220 				ice_write_itr(rc, rc->itr_setting);
8221 		}
8222 		if (rx_q_vector) {
8223 			rx_q_vector->ch = ch;
8224 			/* setup Tx and Rx ITR setting if DIM is off */
8225 			rc = &rx_q_vector->rx;
8226 			if (!ITR_IS_DYNAMIC(rc))
8227 				ice_write_itr(rc, rc->itr_setting);
8228 		}
8229 	}
8230 
8231 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8232 	 * GLINT_ITR register would have written to perform in-context
8233 	 * update, hence perform flush
8234 	 */
8235 	if (ch->num_txq || ch->num_rxq)
8236 		ice_flush(&vsi->back->hw);
8237 }
8238 
8239 /**
8240  * ice_cfg_chnl_all_res - configure channel resources
8241  * @vsi: pte to main_vsi
8242  * @ch: ptr to channel structure
8243  *
8244  * This function configures channel specific resources such as flow-director
8245  * counter index, and other resources such as queues, vectors, ITR settings
8246  */
8247 static void
8248 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8249 {
8250 	/* configure channel (aka ADQ) resources such as queues, vectors,
8251 	 * ITR settings for channel specific vectors and anything else
8252 	 */
8253 	ice_chnl_cfg_res(vsi, ch);
8254 }
8255 
8256 /**
8257  * ice_setup_hw_channel - setup new channel
8258  * @pf: ptr to PF device
8259  * @vsi: the VSI being setup
8260  * @ch: ptr to channel structure
8261  * @sw_id: underlying HW switching element ID
8262  * @type: type of channel to be created (VMDq2/VF)
8263  *
8264  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8265  * and configures Tx rings accordingly
8266  */
8267 static int
8268 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8269 		     struct ice_channel *ch, u16 sw_id, u8 type)
8270 {
8271 	struct device *dev = ice_pf_to_dev(pf);
8272 	int ret;
8273 
8274 	ch->base_q = vsi->next_base_q;
8275 	ch->type = type;
8276 
8277 	ret = ice_add_channel(pf, sw_id, ch);
8278 	if (ret) {
8279 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8280 		return ret;
8281 	}
8282 
8283 	/* configure/setup ADQ specific resources */
8284 	ice_cfg_chnl_all_res(vsi, ch);
8285 
8286 	/* make sure to update the next_base_q so that subsequent channel's
8287 	 * (aka ADQ) VSI queue map is correct
8288 	 */
8289 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8290 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8291 		ch->num_rxq);
8292 
8293 	return 0;
8294 }
8295 
8296 /**
8297  * ice_setup_channel - setup new channel using uplink element
8298  * @pf: ptr to PF device
8299  * @vsi: the VSI being setup
8300  * @ch: ptr to channel structure
8301  *
8302  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8303  * and uplink switching element
8304  */
8305 static bool
8306 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8307 		  struct ice_channel *ch)
8308 {
8309 	struct device *dev = ice_pf_to_dev(pf);
8310 	u16 sw_id;
8311 	int ret;
8312 
8313 	if (vsi->type != ICE_VSI_PF) {
8314 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8315 		return false;
8316 	}
8317 
8318 	sw_id = pf->first_sw->sw_id;
8319 
8320 	/* create channel (VSI) */
8321 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8322 	if (ret) {
8323 		dev_err(dev, "failed to setup hw_channel\n");
8324 		return false;
8325 	}
8326 	dev_dbg(dev, "successfully created channel()\n");
8327 
8328 	return ch->ch_vsi ? true : false;
8329 }
8330 
8331 /**
8332  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8333  * @vsi: VSI to be configured
8334  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8335  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8336  */
8337 static int
8338 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8339 {
8340 	int err;
8341 
8342 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8343 	if (err)
8344 		return err;
8345 
8346 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8347 }
8348 
8349 /**
8350  * ice_create_q_channel - function to create channel
8351  * @vsi: VSI to be configured
8352  * @ch: ptr to channel (it contains channel specific params)
8353  *
8354  * This function creates channel (VSI) using num_queues specified by user,
8355  * reconfigs RSS if needed.
8356  */
8357 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8358 {
8359 	struct ice_pf *pf = vsi->back;
8360 	struct device *dev;
8361 
8362 	if (!ch)
8363 		return -EINVAL;
8364 
8365 	dev = ice_pf_to_dev(pf);
8366 	if (!ch->num_txq || !ch->num_rxq) {
8367 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8368 		return -EINVAL;
8369 	}
8370 
8371 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8372 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8373 			vsi->cnt_q_avail, ch->num_txq);
8374 		return -EINVAL;
8375 	}
8376 
8377 	if (!ice_setup_channel(pf, vsi, ch)) {
8378 		dev_info(dev, "Failed to setup channel\n");
8379 		return -EINVAL;
8380 	}
8381 	/* configure BW rate limit */
8382 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8383 		int ret;
8384 
8385 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8386 				       ch->min_tx_rate);
8387 		if (ret)
8388 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8389 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8390 		else
8391 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8392 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8393 	}
8394 
8395 	vsi->cnt_q_avail -= ch->num_txq;
8396 
8397 	return 0;
8398 }
8399 
8400 /**
8401  * ice_rem_all_chnl_fltrs - removes all channel filters
8402  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8403  *
8404  * Remove all advanced switch filters only if they are channel specific
8405  * tc-flower based filter
8406  */
8407 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8408 {
8409 	struct ice_tc_flower_fltr *fltr;
8410 	struct hlist_node *node;
8411 
8412 	/* to remove all channel filters, iterate an ordered list of filters */
8413 	hlist_for_each_entry_safe(fltr, node,
8414 				  &pf->tc_flower_fltr_list,
8415 				  tc_flower_node) {
8416 		struct ice_rule_query_data rule;
8417 		int status;
8418 
8419 		/* for now process only channel specific filters */
8420 		if (!ice_is_chnl_fltr(fltr))
8421 			continue;
8422 
8423 		rule.rid = fltr->rid;
8424 		rule.rule_id = fltr->rule_id;
8425 		rule.vsi_handle = fltr->dest_vsi_handle;
8426 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8427 		if (status) {
8428 			if (status == -ENOENT)
8429 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8430 					rule.rule_id);
8431 			else
8432 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8433 					status);
8434 		} else if (fltr->dest_vsi) {
8435 			/* update advanced switch filter count */
8436 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8437 				u32 flags = fltr->flags;
8438 
8439 				fltr->dest_vsi->num_chnl_fltr--;
8440 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8441 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8442 					pf->num_dmac_chnl_fltrs--;
8443 			}
8444 		}
8445 
8446 		hlist_del(&fltr->tc_flower_node);
8447 		kfree(fltr);
8448 	}
8449 }
8450 
8451 /**
8452  * ice_remove_q_channels - Remove queue channels for the TCs
8453  * @vsi: VSI to be configured
8454  * @rem_fltr: delete advanced switch filter or not
8455  *
8456  * Remove queue channels for the TCs
8457  */
8458 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8459 {
8460 	struct ice_channel *ch, *ch_tmp;
8461 	struct ice_pf *pf = vsi->back;
8462 	int i;
8463 
8464 	/* remove all tc-flower based filter if they are channel filters only */
8465 	if (rem_fltr)
8466 		ice_rem_all_chnl_fltrs(pf);
8467 
8468 	/* remove ntuple filters since queue configuration is being changed */
8469 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8470 		struct ice_hw *hw = &pf->hw;
8471 
8472 		mutex_lock(&hw->fdir_fltr_lock);
8473 		ice_fdir_del_all_fltrs(vsi);
8474 		mutex_unlock(&hw->fdir_fltr_lock);
8475 	}
8476 
8477 	/* perform cleanup for channels if they exist */
8478 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8479 		struct ice_vsi *ch_vsi;
8480 
8481 		list_del(&ch->list);
8482 		ch_vsi = ch->ch_vsi;
8483 		if (!ch_vsi) {
8484 			kfree(ch);
8485 			continue;
8486 		}
8487 
8488 		/* Reset queue contexts */
8489 		for (i = 0; i < ch->num_rxq; i++) {
8490 			struct ice_tx_ring *tx_ring;
8491 			struct ice_rx_ring *rx_ring;
8492 
8493 			tx_ring = vsi->tx_rings[ch->base_q + i];
8494 			rx_ring = vsi->rx_rings[ch->base_q + i];
8495 			if (tx_ring) {
8496 				tx_ring->ch = NULL;
8497 				if (tx_ring->q_vector)
8498 					tx_ring->q_vector->ch = NULL;
8499 			}
8500 			if (rx_ring) {
8501 				rx_ring->ch = NULL;
8502 				if (rx_ring->q_vector)
8503 					rx_ring->q_vector->ch = NULL;
8504 			}
8505 		}
8506 
8507 		/* Release FD resources for the channel VSI */
8508 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8509 
8510 		/* clear the VSI from scheduler tree */
8511 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8512 
8513 		/* Delete VSI from FW, PF and HW VSI arrays */
8514 		ice_vsi_delete(ch->ch_vsi);
8515 
8516 		/* free the channel */
8517 		kfree(ch);
8518 	}
8519 
8520 	/* clear the channel VSI map which is stored in main VSI */
8521 	ice_for_each_chnl_tc(i)
8522 		vsi->tc_map_vsi[i] = NULL;
8523 
8524 	/* reset main VSI's all TC information */
8525 	vsi->all_enatc = 0;
8526 	vsi->all_numtc = 0;
8527 }
8528 
8529 /**
8530  * ice_rebuild_channels - rebuild channel
8531  * @pf: ptr to PF
8532  *
8533  * Recreate channel VSIs and replay filters
8534  */
8535 static int ice_rebuild_channels(struct ice_pf *pf)
8536 {
8537 	struct device *dev = ice_pf_to_dev(pf);
8538 	struct ice_vsi *main_vsi;
8539 	bool rem_adv_fltr = true;
8540 	struct ice_channel *ch;
8541 	struct ice_vsi *vsi;
8542 	int tc_idx = 1;
8543 	int i, err;
8544 
8545 	main_vsi = ice_get_main_vsi(pf);
8546 	if (!main_vsi)
8547 		return 0;
8548 
8549 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8550 	    main_vsi->old_numtc == 1)
8551 		return 0; /* nothing to be done */
8552 
8553 	/* reconfigure main VSI based on old value of TC and cached values
8554 	 * for MQPRIO opts
8555 	 */
8556 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8557 	if (err) {
8558 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8559 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8560 		return err;
8561 	}
8562 
8563 	/* rebuild ADQ VSIs */
8564 	ice_for_each_vsi(pf, i) {
8565 		enum ice_vsi_type type;
8566 
8567 		vsi = pf->vsi[i];
8568 		if (!vsi || vsi->type != ICE_VSI_CHNL)
8569 			continue;
8570 
8571 		type = vsi->type;
8572 
8573 		/* rebuild ADQ VSI */
8574 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8575 		if (err) {
8576 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8577 				ice_vsi_type_str(type), vsi->idx, err);
8578 			goto cleanup;
8579 		}
8580 
8581 		/* Re-map HW VSI number, using VSI handle that has been
8582 		 * previously validated in ice_replay_vsi() call above
8583 		 */
8584 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8585 
8586 		/* replay filters for the VSI */
8587 		err = ice_replay_vsi(&pf->hw, vsi->idx);
8588 		if (err) {
8589 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8590 				ice_vsi_type_str(type), err, vsi->idx);
8591 			rem_adv_fltr = false;
8592 			goto cleanup;
8593 		}
8594 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8595 			 ice_vsi_type_str(type), vsi->idx);
8596 
8597 		/* store ADQ VSI at correct TC index in main VSI's
8598 		 * map of TC to VSI
8599 		 */
8600 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8601 	}
8602 
8603 	/* ADQ VSI(s) has been rebuilt successfully, so setup
8604 	 * channel for main VSI's Tx and Rx rings
8605 	 */
8606 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8607 		struct ice_vsi *ch_vsi;
8608 
8609 		ch_vsi = ch->ch_vsi;
8610 		if (!ch_vsi)
8611 			continue;
8612 
8613 		/* reconfig channel resources */
8614 		ice_cfg_chnl_all_res(main_vsi, ch);
8615 
8616 		/* replay BW rate limit if it is non-zero */
8617 		if (!ch->max_tx_rate && !ch->min_tx_rate)
8618 			continue;
8619 
8620 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8621 				       ch->min_tx_rate);
8622 		if (err)
8623 			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",
8624 				err, ch->max_tx_rate, ch->min_tx_rate,
8625 				ch_vsi->vsi_num);
8626 		else
8627 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8628 				ch->max_tx_rate, ch->min_tx_rate,
8629 				ch_vsi->vsi_num);
8630 	}
8631 
8632 	/* reconfig RSS for main VSI */
8633 	if (main_vsi->ch_rss_size)
8634 		ice_vsi_cfg_rss_lut_key(main_vsi);
8635 
8636 	return 0;
8637 
8638 cleanup:
8639 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8640 	return err;
8641 }
8642 
8643 /**
8644  * ice_create_q_channels - Add queue channel for the given TCs
8645  * @vsi: VSI to be configured
8646  *
8647  * Configures queue channel mapping to the given TCs
8648  */
8649 static int ice_create_q_channels(struct ice_vsi *vsi)
8650 {
8651 	struct ice_pf *pf = vsi->back;
8652 	struct ice_channel *ch;
8653 	int ret = 0, i;
8654 
8655 	ice_for_each_chnl_tc(i) {
8656 		if (!(vsi->all_enatc & BIT(i)))
8657 			continue;
8658 
8659 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8660 		if (!ch) {
8661 			ret = -ENOMEM;
8662 			goto err_free;
8663 		}
8664 		INIT_LIST_HEAD(&ch->list);
8665 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8666 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8667 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8668 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8669 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8670 
8671 		/* convert to Kbits/s */
8672 		if (ch->max_tx_rate)
8673 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8674 						  ICE_BW_KBPS_DIVISOR);
8675 		if (ch->min_tx_rate)
8676 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8677 						  ICE_BW_KBPS_DIVISOR);
8678 
8679 		ret = ice_create_q_channel(vsi, ch);
8680 		if (ret) {
8681 			dev_err(ice_pf_to_dev(pf),
8682 				"failed creating channel TC:%d\n", i);
8683 			kfree(ch);
8684 			goto err_free;
8685 		}
8686 		list_add_tail(&ch->list, &vsi->ch_list);
8687 		vsi->tc_map_vsi[i] = ch->ch_vsi;
8688 		dev_dbg(ice_pf_to_dev(pf),
8689 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8690 	}
8691 	return 0;
8692 
8693 err_free:
8694 	ice_remove_q_channels(vsi, false);
8695 
8696 	return ret;
8697 }
8698 
8699 /**
8700  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8701  * @netdev: net device to configure
8702  * @type_data: TC offload data
8703  */
8704 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8705 {
8706 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8707 	struct ice_netdev_priv *np = netdev_priv(netdev);
8708 	struct ice_vsi *vsi = np->vsi;
8709 	struct ice_pf *pf = vsi->back;
8710 	u16 mode, ena_tc_qdisc = 0;
8711 	int cur_txq, cur_rxq;
8712 	u8 hw = 0, num_tcf;
8713 	struct device *dev;
8714 	int ret, i;
8715 
8716 	dev = ice_pf_to_dev(pf);
8717 	num_tcf = mqprio_qopt->qopt.num_tc;
8718 	hw = mqprio_qopt->qopt.hw;
8719 	mode = mqprio_qopt->mode;
8720 	if (!hw) {
8721 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8722 		vsi->ch_rss_size = 0;
8723 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8724 		goto config_tcf;
8725 	}
8726 
8727 	/* Generate queue region map for number of TCF requested */
8728 	for (i = 0; i < num_tcf; i++)
8729 		ena_tc_qdisc |= BIT(i);
8730 
8731 	switch (mode) {
8732 	case TC_MQPRIO_MODE_CHANNEL:
8733 
8734 		if (pf->hw.port_info->is_custom_tx_enabled) {
8735 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
8736 			return -EBUSY;
8737 		}
8738 		ice_tear_down_devlink_rate_tree(pf);
8739 
8740 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8741 		if (ret) {
8742 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8743 				   ret);
8744 			return ret;
8745 		}
8746 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8747 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8748 		/* don't assume state of hw_tc_offload during driver load
8749 		 * and set the flag for TC flower filter if hw_tc_offload
8750 		 * already ON
8751 		 */
8752 		if (vsi->netdev->features & NETIF_F_HW_TC)
8753 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8754 		break;
8755 	default:
8756 		return -EINVAL;
8757 	}
8758 
8759 config_tcf:
8760 
8761 	/* Requesting same TCF configuration as already enabled */
8762 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8763 	    mode != TC_MQPRIO_MODE_CHANNEL)
8764 		return 0;
8765 
8766 	/* Pause VSI queues */
8767 	ice_dis_vsi(vsi, true);
8768 
8769 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8770 		ice_remove_q_channels(vsi, true);
8771 
8772 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8773 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8774 				     num_online_cpus());
8775 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8776 				     num_online_cpus());
8777 	} else {
8778 		/* logic to rebuild VSI, same like ethtool -L */
8779 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8780 
8781 		for (i = 0; i < num_tcf; i++) {
8782 			if (!(ena_tc_qdisc & BIT(i)))
8783 				continue;
8784 
8785 			offset = vsi->mqprio_qopt.qopt.offset[i];
8786 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8787 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8788 		}
8789 		vsi->req_txq = offset + qcount_tx;
8790 		vsi->req_rxq = offset + qcount_rx;
8791 
8792 		/* store away original rss_size info, so that it gets reused
8793 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8794 		 * determine, what should be the rss_sizefor main VSI
8795 		 */
8796 		vsi->orig_rss_size = vsi->rss_size;
8797 	}
8798 
8799 	/* save current values of Tx and Rx queues before calling VSI rebuild
8800 	 * for fallback option
8801 	 */
8802 	cur_txq = vsi->num_txq;
8803 	cur_rxq = vsi->num_rxq;
8804 
8805 	/* proceed with rebuild main VSI using correct number of queues */
8806 	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
8807 	if (ret) {
8808 		/* fallback to current number of queues */
8809 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8810 		vsi->req_txq = cur_txq;
8811 		vsi->req_rxq = cur_rxq;
8812 		clear_bit(ICE_RESET_FAILED, pf->state);
8813 		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
8814 			dev_err(dev, "Rebuild of main VSI failed again\n");
8815 			return ret;
8816 		}
8817 	}
8818 
8819 	vsi->all_numtc = num_tcf;
8820 	vsi->all_enatc = ena_tc_qdisc;
8821 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8822 	if (ret) {
8823 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8824 			   vsi->vsi_num);
8825 		goto exit;
8826 	}
8827 
8828 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8829 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8830 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8831 
8832 		/* set TC0 rate limit if specified */
8833 		if (max_tx_rate || min_tx_rate) {
8834 			/* convert to Kbits/s */
8835 			if (max_tx_rate)
8836 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8837 			if (min_tx_rate)
8838 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8839 
8840 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8841 			if (!ret) {
8842 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8843 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8844 			} else {
8845 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8846 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8847 				goto exit;
8848 			}
8849 		}
8850 		ret = ice_create_q_channels(vsi);
8851 		if (ret) {
8852 			netdev_err(netdev, "failed configuring queue channels\n");
8853 			goto exit;
8854 		} else {
8855 			netdev_dbg(netdev, "successfully configured channels\n");
8856 		}
8857 	}
8858 
8859 	if (vsi->ch_rss_size)
8860 		ice_vsi_cfg_rss_lut_key(vsi);
8861 
8862 exit:
8863 	/* if error, reset the all_numtc and all_enatc */
8864 	if (ret) {
8865 		vsi->all_numtc = 0;
8866 		vsi->all_enatc = 0;
8867 	}
8868 	/* resume VSI */
8869 	ice_ena_vsi(vsi, true);
8870 
8871 	return ret;
8872 }
8873 
8874 static LIST_HEAD(ice_block_cb_list);
8875 
8876 static int
8877 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8878 	     void *type_data)
8879 {
8880 	struct ice_netdev_priv *np = netdev_priv(netdev);
8881 	struct ice_pf *pf = np->vsi->back;
8882 	bool locked = false;
8883 	int err;
8884 
8885 	switch (type) {
8886 	case TC_SETUP_BLOCK:
8887 		return flow_block_cb_setup_simple(type_data,
8888 						  &ice_block_cb_list,
8889 						  ice_setup_tc_block_cb,
8890 						  np, np, true);
8891 	case TC_SETUP_QDISC_MQPRIO:
8892 		if (pf->adev) {
8893 			mutex_lock(&pf->adev_mutex);
8894 			device_lock(&pf->adev->dev);
8895 			locked = true;
8896 			if (pf->adev->dev.driver) {
8897 				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
8898 				err = -EBUSY;
8899 				goto adev_unlock;
8900 			}
8901 		}
8902 
8903 		/* setup traffic classifier for receive side */
8904 		mutex_lock(&pf->tc_mutex);
8905 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8906 		mutex_unlock(&pf->tc_mutex);
8907 
8908 adev_unlock:
8909 		if (locked) {
8910 			device_unlock(&pf->adev->dev);
8911 			mutex_unlock(&pf->adev_mutex);
8912 		}
8913 		return err;
8914 	default:
8915 		return -EOPNOTSUPP;
8916 	}
8917 	return -EOPNOTSUPP;
8918 }
8919 
8920 static struct ice_indr_block_priv *
8921 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8922 			   struct net_device *netdev)
8923 {
8924 	struct ice_indr_block_priv *cb_priv;
8925 
8926 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8927 		if (!cb_priv->netdev)
8928 			return NULL;
8929 		if (cb_priv->netdev == netdev)
8930 			return cb_priv;
8931 	}
8932 	return NULL;
8933 }
8934 
8935 static int
8936 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
8937 			void *indr_priv)
8938 {
8939 	struct ice_indr_block_priv *priv = indr_priv;
8940 	struct ice_netdev_priv *np = priv->np;
8941 
8942 	switch (type) {
8943 	case TC_SETUP_CLSFLOWER:
8944 		return ice_setup_tc_cls_flower(np, priv->netdev,
8945 					       (struct flow_cls_offload *)
8946 					       type_data);
8947 	default:
8948 		return -EOPNOTSUPP;
8949 	}
8950 }
8951 
8952 static int
8953 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
8954 			struct ice_netdev_priv *np,
8955 			struct flow_block_offload *f, void *data,
8956 			void (*cleanup)(struct flow_block_cb *block_cb))
8957 {
8958 	struct ice_indr_block_priv *indr_priv;
8959 	struct flow_block_cb *block_cb;
8960 
8961 	if (!ice_is_tunnel_supported(netdev) &&
8962 	    !(is_vlan_dev(netdev) &&
8963 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
8964 		return -EOPNOTSUPP;
8965 
8966 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8967 		return -EOPNOTSUPP;
8968 
8969 	switch (f->command) {
8970 	case FLOW_BLOCK_BIND:
8971 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8972 		if (indr_priv)
8973 			return -EEXIST;
8974 
8975 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
8976 		if (!indr_priv)
8977 			return -ENOMEM;
8978 
8979 		indr_priv->netdev = netdev;
8980 		indr_priv->np = np;
8981 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
8982 
8983 		block_cb =
8984 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
8985 						 indr_priv, indr_priv,
8986 						 ice_rep_indr_tc_block_unbind,
8987 						 f, netdev, sch, data, np,
8988 						 cleanup);
8989 
8990 		if (IS_ERR(block_cb)) {
8991 			list_del(&indr_priv->list);
8992 			kfree(indr_priv);
8993 			return PTR_ERR(block_cb);
8994 		}
8995 		flow_block_cb_add(block_cb, f);
8996 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
8997 		break;
8998 	case FLOW_BLOCK_UNBIND:
8999 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9000 		if (!indr_priv)
9001 			return -ENOENT;
9002 
9003 		block_cb = flow_block_cb_lookup(f->block,
9004 						ice_indr_setup_block_cb,
9005 						indr_priv);
9006 		if (!block_cb)
9007 			return -ENOENT;
9008 
9009 		flow_indr_block_cb_remove(block_cb, f);
9010 
9011 		list_del(&block_cb->driver_list);
9012 		break;
9013 	default:
9014 		return -EOPNOTSUPP;
9015 	}
9016 	return 0;
9017 }
9018 
9019 static int
9020 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9021 		     void *cb_priv, enum tc_setup_type type, void *type_data,
9022 		     void *data,
9023 		     void (*cleanup)(struct flow_block_cb *block_cb))
9024 {
9025 	switch (type) {
9026 	case TC_SETUP_BLOCK:
9027 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9028 					       data, cleanup);
9029 
9030 	default:
9031 		return -EOPNOTSUPP;
9032 	}
9033 }
9034 
9035 /**
9036  * ice_open - Called when a network interface becomes active
9037  * @netdev: network interface device structure
9038  *
9039  * The open entry point is called when a network interface is made
9040  * active by the system (IFF_UP). At this point all resources needed
9041  * for transmit and receive operations are allocated, the interrupt
9042  * handler is registered with the OS, the netdev watchdog is enabled,
9043  * and the stack is notified that the interface is ready.
9044  *
9045  * Returns 0 on success, negative value on failure
9046  */
9047 int ice_open(struct net_device *netdev)
9048 {
9049 	struct ice_netdev_priv *np = netdev_priv(netdev);
9050 	struct ice_pf *pf = np->vsi->back;
9051 
9052 	if (ice_is_reset_in_progress(pf->state)) {
9053 		netdev_err(netdev, "can't open net device while reset is in progress");
9054 		return -EBUSY;
9055 	}
9056 
9057 	return ice_open_internal(netdev);
9058 }
9059 
9060 /**
9061  * ice_open_internal - Called when a network interface becomes active
9062  * @netdev: network interface device structure
9063  *
9064  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9065  * handling routine
9066  *
9067  * Returns 0 on success, negative value on failure
9068  */
9069 int ice_open_internal(struct net_device *netdev)
9070 {
9071 	struct ice_netdev_priv *np = netdev_priv(netdev);
9072 	struct ice_vsi *vsi = np->vsi;
9073 	struct ice_pf *pf = vsi->back;
9074 	struct ice_port_info *pi;
9075 	int err;
9076 
9077 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9078 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9079 		return -EIO;
9080 	}
9081 
9082 	netif_carrier_off(netdev);
9083 
9084 	pi = vsi->port_info;
9085 	err = ice_update_link_info(pi);
9086 	if (err) {
9087 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9088 		return err;
9089 	}
9090 
9091 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9092 
9093 	/* Set PHY if there is media, otherwise, turn off PHY */
9094 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9095 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9096 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9097 			err = ice_init_phy_user_cfg(pi);
9098 			if (err) {
9099 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9100 					   err);
9101 				return err;
9102 			}
9103 		}
9104 
9105 		err = ice_configure_phy(vsi);
9106 		if (err) {
9107 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9108 				   err);
9109 			return err;
9110 		}
9111 	} else {
9112 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9113 		ice_set_link(vsi, false);
9114 	}
9115 
9116 	err = ice_vsi_open(vsi);
9117 	if (err)
9118 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9119 			   vsi->vsi_num, vsi->vsw->sw_id);
9120 
9121 	/* Update existing tunnels information */
9122 	udp_tunnel_get_rx_info(netdev);
9123 
9124 	return err;
9125 }
9126 
9127 /**
9128  * ice_stop - Disables a network interface
9129  * @netdev: network interface device structure
9130  *
9131  * The stop entry point is called when an interface is de-activated by the OS,
9132  * and the netdevice enters the DOWN state. The hardware is still under the
9133  * driver's control, but the netdev interface is disabled.
9134  *
9135  * Returns success only - not allowed to fail
9136  */
9137 int ice_stop(struct net_device *netdev)
9138 {
9139 	struct ice_netdev_priv *np = netdev_priv(netdev);
9140 	struct ice_vsi *vsi = np->vsi;
9141 	struct ice_pf *pf = vsi->back;
9142 
9143 	if (ice_is_reset_in_progress(pf->state)) {
9144 		netdev_err(netdev, "can't stop net device while reset is in progress");
9145 		return -EBUSY;
9146 	}
9147 
9148 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9149 		int link_err = ice_force_phys_link_state(vsi, false);
9150 
9151 		if (link_err) {
9152 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9153 				   vsi->vsi_num, link_err);
9154 			return -EIO;
9155 		}
9156 	}
9157 
9158 	ice_vsi_close(vsi);
9159 
9160 	return 0;
9161 }
9162 
9163 /**
9164  * ice_features_check - Validate encapsulated packet conforms to limits
9165  * @skb: skb buffer
9166  * @netdev: This port's netdev
9167  * @features: Offload features that the stack believes apply
9168  */
9169 static netdev_features_t
9170 ice_features_check(struct sk_buff *skb,
9171 		   struct net_device __always_unused *netdev,
9172 		   netdev_features_t features)
9173 {
9174 	bool gso = skb_is_gso(skb);
9175 	size_t len;
9176 
9177 	/* No point in doing any of this if neither checksum nor GSO are
9178 	 * being requested for this frame. We can rule out both by just
9179 	 * checking for CHECKSUM_PARTIAL
9180 	 */
9181 	if (skb->ip_summed != CHECKSUM_PARTIAL)
9182 		return features;
9183 
9184 	/* We cannot support GSO if the MSS is going to be less than
9185 	 * 64 bytes. If it is then we need to drop support for GSO.
9186 	 */
9187 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9188 		features &= ~NETIF_F_GSO_MASK;
9189 
9190 	len = skb_network_offset(skb);
9191 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9192 		goto out_rm_features;
9193 
9194 	len = skb_network_header_len(skb);
9195 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9196 		goto out_rm_features;
9197 
9198 	if (skb->encapsulation) {
9199 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9200 		 * the case of IPIP frames, the transport header pointer is
9201 		 * after the inner header! So check to make sure that this
9202 		 * is a GRE or UDP_TUNNEL frame before doing that math.
9203 		 */
9204 		if (gso && (skb_shinfo(skb)->gso_type &
9205 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9206 			len = skb_inner_network_header(skb) -
9207 			      skb_transport_header(skb);
9208 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9209 				goto out_rm_features;
9210 		}
9211 
9212 		len = skb_inner_network_header_len(skb);
9213 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9214 			goto out_rm_features;
9215 	}
9216 
9217 	return features;
9218 out_rm_features:
9219 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9220 }
9221 
9222 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9223 	.ndo_open = ice_open,
9224 	.ndo_stop = ice_stop,
9225 	.ndo_start_xmit = ice_start_xmit,
9226 	.ndo_set_mac_address = ice_set_mac_address,
9227 	.ndo_validate_addr = eth_validate_addr,
9228 	.ndo_change_mtu = ice_change_mtu,
9229 	.ndo_get_stats64 = ice_get_stats64,
9230 	.ndo_tx_timeout = ice_tx_timeout,
9231 	.ndo_bpf = ice_xdp_safe_mode,
9232 };
9233 
9234 static const struct net_device_ops ice_netdev_ops = {
9235 	.ndo_open = ice_open,
9236 	.ndo_stop = ice_stop,
9237 	.ndo_start_xmit = ice_start_xmit,
9238 	.ndo_select_queue = ice_select_queue,
9239 	.ndo_features_check = ice_features_check,
9240 	.ndo_fix_features = ice_fix_features,
9241 	.ndo_set_rx_mode = ice_set_rx_mode,
9242 	.ndo_set_mac_address = ice_set_mac_address,
9243 	.ndo_validate_addr = eth_validate_addr,
9244 	.ndo_change_mtu = ice_change_mtu,
9245 	.ndo_get_stats64 = ice_get_stats64,
9246 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9247 	.ndo_eth_ioctl = ice_eth_ioctl,
9248 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9249 	.ndo_set_vf_mac = ice_set_vf_mac,
9250 	.ndo_get_vf_config = ice_get_vf_cfg,
9251 	.ndo_set_vf_trust = ice_set_vf_trust,
9252 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9253 	.ndo_set_vf_link_state = ice_set_vf_link_state,
9254 	.ndo_get_vf_stats = ice_get_vf_stats,
9255 	.ndo_set_vf_rate = ice_set_vf_bw,
9256 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9257 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9258 	.ndo_setup_tc = ice_setup_tc,
9259 	.ndo_set_features = ice_set_features,
9260 	.ndo_bridge_getlink = ice_bridge_getlink,
9261 	.ndo_bridge_setlink = ice_bridge_setlink,
9262 	.ndo_fdb_add = ice_fdb_add,
9263 	.ndo_fdb_del = ice_fdb_del,
9264 #ifdef CONFIG_RFS_ACCEL
9265 	.ndo_rx_flow_steer = ice_rx_flow_steer,
9266 #endif
9267 	.ndo_tx_timeout = ice_tx_timeout,
9268 	.ndo_bpf = ice_xdp,
9269 	.ndo_xdp_xmit = ice_xdp_xmit,
9270 	.ndo_xsk_wakeup = ice_xsk_wakeup,
9271 };
9272