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