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