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