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