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