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