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 
2613 	/* assign the prog only when it's not already present on VSI;
2614 	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2615 	 * VSI rebuild that happens under ethtool -L can expose us to
2616 	 * the bpf_prog refcount issues as we would be swapping same
2617 	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2618 	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2619 	 * this is not harmful as dev_xdp_install bumps the refcount
2620 	 * before calling the op exposed by the driver;
2621 	 */
2622 	if (!ice_is_xdp_ena_vsi(vsi))
2623 		ice_vsi_assign_bpf_prog(vsi, prog);
2624 
2625 	return 0;
2626 clear_xdp_rings:
2627 	ice_for_each_xdp_txq(vsi, i)
2628 		if (vsi->xdp_rings[i]) {
2629 			kfree_rcu(vsi->xdp_rings[i], rcu);
2630 			vsi->xdp_rings[i] = NULL;
2631 		}
2632 
2633 err_map_xdp:
2634 	mutex_lock(&pf->avail_q_mutex);
2635 	ice_for_each_xdp_txq(vsi, i) {
2636 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2637 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2638 	}
2639 	mutex_unlock(&pf->avail_q_mutex);
2640 
2641 	devm_kfree(dev, vsi->xdp_rings);
2642 	return -ENOMEM;
2643 }
2644 
2645 /**
2646  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2647  * @vsi: VSI to remove XDP rings
2648  *
2649  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2650  * resources
2651  */
2652 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2653 {
2654 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2655 	struct ice_pf *pf = vsi->back;
2656 	int i, v_idx;
2657 
2658 	/* q_vectors are freed in reset path so there's no point in detaching
2659 	 * rings; in case of rebuild being triggered not from reset bits
2660 	 * in pf->state won't be set, so additionally check first q_vector
2661 	 * against NULL
2662 	 */
2663 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2664 		goto free_qmap;
2665 
2666 	ice_for_each_q_vector(vsi, v_idx) {
2667 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2668 		struct ice_tx_ring *ring;
2669 
2670 		ice_for_each_tx_ring(ring, q_vector->tx)
2671 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2672 				break;
2673 
2674 		/* restore the value of last node prior to XDP setup */
2675 		q_vector->tx.tx_ring = ring;
2676 	}
2677 
2678 free_qmap:
2679 	mutex_lock(&pf->avail_q_mutex);
2680 	ice_for_each_xdp_txq(vsi, i) {
2681 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2682 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2683 	}
2684 	mutex_unlock(&pf->avail_q_mutex);
2685 
2686 	ice_for_each_xdp_txq(vsi, i)
2687 		if (vsi->xdp_rings[i]) {
2688 			if (vsi->xdp_rings[i]->desc)
2689 				ice_free_tx_ring(vsi->xdp_rings[i]);
2690 			kfree_rcu(vsi->xdp_rings[i], rcu);
2691 			vsi->xdp_rings[i] = NULL;
2692 		}
2693 
2694 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2695 	vsi->xdp_rings = NULL;
2696 
2697 	if (static_key_enabled(&ice_xdp_locking_key))
2698 		static_branch_dec(&ice_xdp_locking_key);
2699 
2700 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2701 		return 0;
2702 
2703 	ice_vsi_assign_bpf_prog(vsi, NULL);
2704 
2705 	/* notify Tx scheduler that we destroyed XDP queues and bring
2706 	 * back the old number of child nodes
2707 	 */
2708 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2709 		max_txqs[i] = vsi->num_txq;
2710 
2711 	/* change number of XDP Tx queues to 0 */
2712 	vsi->num_xdp_txq = 0;
2713 
2714 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2715 			       max_txqs);
2716 }
2717 
2718 /**
2719  * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2720  * @vsi: VSI to schedule napi on
2721  */
2722 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2723 {
2724 	int i;
2725 
2726 	ice_for_each_rxq(vsi, i) {
2727 		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2728 
2729 		if (rx_ring->xsk_pool)
2730 			napi_schedule(&rx_ring->q_vector->napi);
2731 	}
2732 }
2733 
2734 /**
2735  * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2736  * @vsi: VSI to determine the count of XDP Tx qs
2737  *
2738  * returns 0 if Tx qs count is higher than at least half of CPU count,
2739  * -ENOMEM otherwise
2740  */
2741 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2742 {
2743 	u16 avail = ice_get_avail_txq_count(vsi->back);
2744 	u16 cpus = num_possible_cpus();
2745 
2746 	if (avail < cpus / 2)
2747 		return -ENOMEM;
2748 
2749 	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2750 
2751 	if (vsi->num_xdp_txq < cpus)
2752 		static_branch_inc(&ice_xdp_locking_key);
2753 
2754 	return 0;
2755 }
2756 
2757 /**
2758  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2759  * @vsi: VSI to setup XDP for
2760  * @prog: XDP program
2761  * @extack: netlink extended ack
2762  */
2763 static int
2764 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2765 		   struct netlink_ext_ack *extack)
2766 {
2767 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2768 	bool if_running = netif_running(vsi->netdev);
2769 	int ret = 0, xdp_ring_err = 0;
2770 
2771 	if (frame_size > vsi->rx_buf_len) {
2772 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2773 		return -EOPNOTSUPP;
2774 	}
2775 
2776 	/* need to stop netdev while setting up the program for Rx rings */
2777 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2778 		ret = ice_down(vsi);
2779 		if (ret) {
2780 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2781 			return ret;
2782 		}
2783 	}
2784 
2785 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2786 		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2787 		if (xdp_ring_err) {
2788 			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2789 		} else {
2790 			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2791 			if (xdp_ring_err)
2792 				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2793 		}
2794 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2795 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2796 		if (xdp_ring_err)
2797 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2798 	} else {
2799 		/* safe to call even when prog == vsi->xdp_prog as
2800 		 * dev_xdp_install in net/core/dev.c incremented prog's
2801 		 * refcount so corresponding bpf_prog_put won't cause
2802 		 * underflow
2803 		 */
2804 		ice_vsi_assign_bpf_prog(vsi, prog);
2805 	}
2806 
2807 	if (if_running)
2808 		ret = ice_up(vsi);
2809 
2810 	if (!ret && prog)
2811 		ice_vsi_rx_napi_schedule(vsi);
2812 
2813 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2814 }
2815 
2816 /**
2817  * ice_xdp_safe_mode - XDP handler for safe mode
2818  * @dev: netdevice
2819  * @xdp: XDP command
2820  */
2821 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2822 			     struct netdev_bpf *xdp)
2823 {
2824 	NL_SET_ERR_MSG_MOD(xdp->extack,
2825 			   "Please provide working DDP firmware package in order to use XDP\n"
2826 			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
2827 	return -EOPNOTSUPP;
2828 }
2829 
2830 /**
2831  * ice_xdp - implements XDP handler
2832  * @dev: netdevice
2833  * @xdp: XDP command
2834  */
2835 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2836 {
2837 	struct ice_netdev_priv *np = netdev_priv(dev);
2838 	struct ice_vsi *vsi = np->vsi;
2839 
2840 	if (vsi->type != ICE_VSI_PF) {
2841 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2842 		return -EINVAL;
2843 	}
2844 
2845 	switch (xdp->command) {
2846 	case XDP_SETUP_PROG:
2847 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2848 	case XDP_SETUP_XSK_POOL:
2849 		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2850 					  xdp->xsk.queue_id);
2851 	default:
2852 		return -EINVAL;
2853 	}
2854 }
2855 
2856 /**
2857  * ice_ena_misc_vector - enable the non-queue interrupts
2858  * @pf: board private structure
2859  */
2860 static void ice_ena_misc_vector(struct ice_pf *pf)
2861 {
2862 	struct ice_hw *hw = &pf->hw;
2863 	u32 val;
2864 
2865 	/* Disable anti-spoof detection interrupt to prevent spurious event
2866 	 * interrupts during a function reset. Anti-spoof functionally is
2867 	 * still supported.
2868 	 */
2869 	val = rd32(hw, GL_MDCK_TX_TDPU);
2870 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2871 	wr32(hw, GL_MDCK_TX_TDPU, val);
2872 
2873 	/* clear things first */
2874 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
2875 	rd32(hw, PFINT_OICR);		/* read to clear */
2876 
2877 	val = (PFINT_OICR_ECC_ERR_M |
2878 	       PFINT_OICR_MAL_DETECT_M |
2879 	       PFINT_OICR_GRST_M |
2880 	       PFINT_OICR_PCI_EXCEPTION_M |
2881 	       PFINT_OICR_VFLR_M |
2882 	       PFINT_OICR_HMC_ERR_M |
2883 	       PFINT_OICR_PE_PUSH_M |
2884 	       PFINT_OICR_PE_CRITERR_M);
2885 
2886 	wr32(hw, PFINT_OICR_ENA, val);
2887 
2888 	/* SW_ITR_IDX = 0, but don't change INTENA */
2889 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2890 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2891 }
2892 
2893 /**
2894  * ice_misc_intr - misc interrupt handler
2895  * @irq: interrupt number
2896  * @data: pointer to a q_vector
2897  */
2898 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2899 {
2900 	struct ice_pf *pf = (struct ice_pf *)data;
2901 	struct ice_hw *hw = &pf->hw;
2902 	irqreturn_t ret = IRQ_NONE;
2903 	struct device *dev;
2904 	u32 oicr, ena_mask;
2905 
2906 	dev = ice_pf_to_dev(pf);
2907 	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
2908 	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2909 	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
2910 
2911 	oicr = rd32(hw, PFINT_OICR);
2912 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2913 
2914 	if (oicr & PFINT_OICR_SWINT_M) {
2915 		ena_mask &= ~PFINT_OICR_SWINT_M;
2916 		pf->sw_int_count++;
2917 	}
2918 
2919 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2920 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2921 		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
2922 	}
2923 	if (oicr & PFINT_OICR_VFLR_M) {
2924 		/* disable any further VFLR event notifications */
2925 		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
2926 			u32 reg = rd32(hw, PFINT_OICR_ENA);
2927 
2928 			reg &= ~PFINT_OICR_VFLR_M;
2929 			wr32(hw, PFINT_OICR_ENA, reg);
2930 		} else {
2931 			ena_mask &= ~PFINT_OICR_VFLR_M;
2932 			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
2933 		}
2934 	}
2935 
2936 	if (oicr & PFINT_OICR_GRST_M) {
2937 		u32 reset;
2938 
2939 		/* we have a reset warning */
2940 		ena_mask &= ~PFINT_OICR_GRST_M;
2941 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2942 			GLGEN_RSTAT_RESET_TYPE_S;
2943 
2944 		if (reset == ICE_RESET_CORER)
2945 			pf->corer_count++;
2946 		else if (reset == ICE_RESET_GLOBR)
2947 			pf->globr_count++;
2948 		else if (reset == ICE_RESET_EMPR)
2949 			pf->empr_count++;
2950 		else
2951 			dev_dbg(dev, "Invalid reset type %d\n", reset);
2952 
2953 		/* If a reset cycle isn't already in progress, we set a bit in
2954 		 * pf->state so that the service task can start a reset/rebuild.
2955 		 */
2956 		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
2957 			if (reset == ICE_RESET_CORER)
2958 				set_bit(ICE_CORER_RECV, pf->state);
2959 			else if (reset == ICE_RESET_GLOBR)
2960 				set_bit(ICE_GLOBR_RECV, pf->state);
2961 			else
2962 				set_bit(ICE_EMPR_RECV, pf->state);
2963 
2964 			/* There are couple of different bits at play here.
2965 			 * hw->reset_ongoing indicates whether the hardware is
2966 			 * in reset. This is set to true when a reset interrupt
2967 			 * is received and set back to false after the driver
2968 			 * has determined that the hardware is out of reset.
2969 			 *
2970 			 * ICE_RESET_OICR_RECV in pf->state indicates
2971 			 * that a post reset rebuild is required before the
2972 			 * driver is operational again. This is set above.
2973 			 *
2974 			 * As this is the start of the reset/rebuild cycle, set
2975 			 * both to indicate that.
2976 			 */
2977 			hw->reset_ongoing = true;
2978 		}
2979 	}
2980 
2981 	if (oicr & PFINT_OICR_TSYN_TX_M) {
2982 		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
2983 		ice_ptp_process_ts(pf);
2984 	}
2985 
2986 	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
2987 		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2988 		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
2989 
2990 		/* Save EVENTs from GTSYN register */
2991 		pf->ptp.ext_ts_irq |= gltsyn_stat & (GLTSYN_STAT_EVENT0_M |
2992 						     GLTSYN_STAT_EVENT1_M |
2993 						     GLTSYN_STAT_EVENT2_M);
2994 		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
2995 		kthread_queue_work(pf->ptp.kworker, &pf->ptp.extts_work);
2996 	}
2997 
2998 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
2999 	if (oicr & ICE_AUX_CRIT_ERR) {
3000 		struct iidc_event *event;
3001 
3002 		ena_mask &= ~ICE_AUX_CRIT_ERR;
3003 		event = kzalloc(sizeof(*event), GFP_KERNEL);
3004 		if (event) {
3005 			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
3006 			/* report the entire OICR value to AUX driver */
3007 			event->reg = oicr;
3008 			ice_send_event_to_aux(pf, event);
3009 			kfree(event);
3010 		}
3011 	}
3012 
3013 	/* Report any remaining unexpected interrupts */
3014 	oicr &= ena_mask;
3015 	if (oicr) {
3016 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3017 		/* If a critical error is pending there is no choice but to
3018 		 * reset the device.
3019 		 */
3020 		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3021 			    PFINT_OICR_ECC_ERR_M)) {
3022 			set_bit(ICE_PFR_REQ, pf->state);
3023 			ice_service_task_schedule(pf);
3024 		}
3025 	}
3026 	ret = IRQ_HANDLED;
3027 
3028 	ice_service_task_schedule(pf);
3029 	ice_irq_dynamic_ena(hw, NULL, NULL);
3030 
3031 	return ret;
3032 }
3033 
3034 /**
3035  * ice_dis_ctrlq_interrupts - disable control queue interrupts
3036  * @hw: pointer to HW structure
3037  */
3038 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3039 {
3040 	/* disable Admin queue Interrupt causes */
3041 	wr32(hw, PFINT_FW_CTL,
3042 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3043 
3044 	/* disable Mailbox queue Interrupt causes */
3045 	wr32(hw, PFINT_MBX_CTL,
3046 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3047 
3048 	wr32(hw, PFINT_SB_CTL,
3049 	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3050 
3051 	/* disable Control queue Interrupt causes */
3052 	wr32(hw, PFINT_OICR_CTL,
3053 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3054 
3055 	ice_flush(hw);
3056 }
3057 
3058 /**
3059  * ice_free_irq_msix_misc - Unroll misc vector setup
3060  * @pf: board private structure
3061  */
3062 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3063 {
3064 	struct ice_hw *hw = &pf->hw;
3065 
3066 	ice_dis_ctrlq_interrupts(hw);
3067 
3068 	/* disable OICR interrupt */
3069 	wr32(hw, PFINT_OICR_ENA, 0);
3070 	ice_flush(hw);
3071 
3072 	if (pf->msix_entries) {
3073 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
3074 		devm_free_irq(ice_pf_to_dev(pf),
3075 			      pf->msix_entries[pf->oicr_idx].vector, pf);
3076 	}
3077 
3078 	pf->num_avail_sw_msix += 1;
3079 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
3080 }
3081 
3082 /**
3083  * ice_ena_ctrlq_interrupts - enable control queue interrupts
3084  * @hw: pointer to HW structure
3085  * @reg_idx: HW vector index to associate the control queue interrupts with
3086  */
3087 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3088 {
3089 	u32 val;
3090 
3091 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3092 	       PFINT_OICR_CTL_CAUSE_ENA_M);
3093 	wr32(hw, PFINT_OICR_CTL, val);
3094 
3095 	/* enable Admin queue Interrupt causes */
3096 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3097 	       PFINT_FW_CTL_CAUSE_ENA_M);
3098 	wr32(hw, PFINT_FW_CTL, val);
3099 
3100 	/* enable Mailbox queue Interrupt causes */
3101 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3102 	       PFINT_MBX_CTL_CAUSE_ENA_M);
3103 	wr32(hw, PFINT_MBX_CTL, val);
3104 
3105 	/* This enables Sideband queue Interrupt causes */
3106 	val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3107 	       PFINT_SB_CTL_CAUSE_ENA_M);
3108 	wr32(hw, PFINT_SB_CTL, val);
3109 
3110 	ice_flush(hw);
3111 }
3112 
3113 /**
3114  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3115  * @pf: board private structure
3116  *
3117  * This sets up the handler for MSIX 0, which is used to manage the
3118  * non-queue interrupts, e.g. AdminQ and errors. This is not used
3119  * when in MSI or Legacy interrupt mode.
3120  */
3121 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3122 {
3123 	struct device *dev = ice_pf_to_dev(pf);
3124 	struct ice_hw *hw = &pf->hw;
3125 	int oicr_idx, err = 0;
3126 
3127 	if (!pf->int_name[0])
3128 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3129 			 dev_driver_string(dev), dev_name(dev));
3130 
3131 	/* Do not request IRQ but do enable OICR interrupt since settings are
3132 	 * lost during reset. Note that this function is called only during
3133 	 * rebuild path and not while reset is in progress.
3134 	 */
3135 	if (ice_is_reset_in_progress(pf->state))
3136 		goto skip_req_irq;
3137 
3138 	/* reserve one vector in irq_tracker for misc interrupts */
3139 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3140 	if (oicr_idx < 0)
3141 		return oicr_idx;
3142 
3143 	pf->num_avail_sw_msix -= 1;
3144 	pf->oicr_idx = (u16)oicr_idx;
3145 
3146 	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
3147 			       ice_misc_intr, 0, pf->int_name, pf);
3148 	if (err) {
3149 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
3150 			pf->int_name, err);
3151 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3152 		pf->num_avail_sw_msix += 1;
3153 		return err;
3154 	}
3155 
3156 skip_req_irq:
3157 	ice_ena_misc_vector(pf);
3158 
3159 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
3160 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
3161 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3162 
3163 	ice_flush(hw);
3164 	ice_irq_dynamic_ena(hw, NULL, NULL);
3165 
3166 	return 0;
3167 }
3168 
3169 /**
3170  * ice_napi_add - register NAPI handler for the VSI
3171  * @vsi: VSI for which NAPI handler is to be registered
3172  *
3173  * This function is only called in the driver's load path. Registering the NAPI
3174  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3175  * reset/rebuild, etc.)
3176  */
3177 static void ice_napi_add(struct ice_vsi *vsi)
3178 {
3179 	int v_idx;
3180 
3181 	if (!vsi->netdev)
3182 		return;
3183 
3184 	ice_for_each_q_vector(vsi, v_idx)
3185 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3186 			       ice_napi_poll, NAPI_POLL_WEIGHT);
3187 }
3188 
3189 /**
3190  * ice_set_ops - set netdev and ethtools ops for the given netdev
3191  * @netdev: netdev instance
3192  */
3193 static void ice_set_ops(struct net_device *netdev)
3194 {
3195 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3196 
3197 	if (ice_is_safe_mode(pf)) {
3198 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3199 		ice_set_ethtool_safe_mode_ops(netdev);
3200 		return;
3201 	}
3202 
3203 	netdev->netdev_ops = &ice_netdev_ops;
3204 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3205 	ice_set_ethtool_ops(netdev);
3206 }
3207 
3208 /**
3209  * ice_set_netdev_features - set features for the given netdev
3210  * @netdev: netdev instance
3211  */
3212 static void ice_set_netdev_features(struct net_device *netdev)
3213 {
3214 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3215 	netdev_features_t csumo_features;
3216 	netdev_features_t vlano_features;
3217 	netdev_features_t dflt_features;
3218 	netdev_features_t tso_features;
3219 
3220 	if (ice_is_safe_mode(pf)) {
3221 		/* safe mode */
3222 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3223 		netdev->hw_features = netdev->features;
3224 		return;
3225 	}
3226 
3227 	dflt_features = NETIF_F_SG	|
3228 			NETIF_F_HIGHDMA	|
3229 			NETIF_F_NTUPLE	|
3230 			NETIF_F_RXHASH;
3231 
3232 	csumo_features = NETIF_F_RXCSUM	  |
3233 			 NETIF_F_IP_CSUM  |
3234 			 NETIF_F_SCTP_CRC |
3235 			 NETIF_F_IPV6_CSUM;
3236 
3237 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3238 			 NETIF_F_HW_VLAN_CTAG_TX     |
3239 			 NETIF_F_HW_VLAN_CTAG_RX;
3240 
3241 	tso_features = NETIF_F_TSO			|
3242 		       NETIF_F_TSO_ECN			|
3243 		       NETIF_F_TSO6			|
3244 		       NETIF_F_GSO_GRE			|
3245 		       NETIF_F_GSO_UDP_TUNNEL		|
3246 		       NETIF_F_GSO_GRE_CSUM		|
3247 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3248 		       NETIF_F_GSO_PARTIAL		|
3249 		       NETIF_F_GSO_IPXIP4		|
3250 		       NETIF_F_GSO_IPXIP6		|
3251 		       NETIF_F_GSO_UDP_L4;
3252 
3253 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3254 					NETIF_F_GSO_GRE_CSUM;
3255 	/* set features that user can change */
3256 	netdev->hw_features = dflt_features | csumo_features |
3257 			      vlano_features | tso_features;
3258 
3259 	/* add support for HW_CSUM on packets with MPLS header */
3260 	netdev->mpls_features =  NETIF_F_HW_CSUM;
3261 
3262 	/* enable features */
3263 	netdev->features |= netdev->hw_features;
3264 
3265 	netdev->hw_features |= NETIF_F_HW_TC;
3266 
3267 	/* encap and VLAN devices inherit default, csumo and tso features */
3268 	netdev->hw_enc_features |= dflt_features | csumo_features |
3269 				   tso_features;
3270 	netdev->vlan_features |= dflt_features | csumo_features |
3271 				 tso_features;
3272 }
3273 
3274 /**
3275  * ice_cfg_netdev - Allocate, configure and register a netdev
3276  * @vsi: the VSI associated with the new netdev
3277  *
3278  * Returns 0 on success, negative value on failure
3279  */
3280 static int ice_cfg_netdev(struct ice_vsi *vsi)
3281 {
3282 	struct ice_netdev_priv *np;
3283 	struct net_device *netdev;
3284 	u8 mac_addr[ETH_ALEN];
3285 
3286 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
3287 				    vsi->alloc_rxq);
3288 	if (!netdev)
3289 		return -ENOMEM;
3290 
3291 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3292 	vsi->netdev = netdev;
3293 	np = netdev_priv(netdev);
3294 	np->vsi = vsi;
3295 
3296 	ice_set_netdev_features(netdev);
3297 
3298 	ice_set_ops(netdev);
3299 
3300 	if (vsi->type == ICE_VSI_PF) {
3301 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
3302 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
3303 		eth_hw_addr_set(netdev, mac_addr);
3304 		ether_addr_copy(netdev->perm_addr, mac_addr);
3305 	}
3306 
3307 	netdev->priv_flags |= IFF_UNICAST_FLT;
3308 
3309 	/* Setup netdev TC information */
3310 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
3311 
3312 	/* setup watchdog timeout value to be 5 second */
3313 	netdev->watchdog_timeo = 5 * HZ;
3314 
3315 	netdev->min_mtu = ETH_MIN_MTU;
3316 	netdev->max_mtu = ICE_MAX_MTU;
3317 
3318 	return 0;
3319 }
3320 
3321 /**
3322  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3323  * @lut: Lookup table
3324  * @rss_table_size: Lookup table size
3325  * @rss_size: Range of queue number for hashing
3326  */
3327 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3328 {
3329 	u16 i;
3330 
3331 	for (i = 0; i < rss_table_size; i++)
3332 		lut[i] = i % rss_size;
3333 }
3334 
3335 /**
3336  * ice_pf_vsi_setup - Set up a PF VSI
3337  * @pf: board private structure
3338  * @pi: pointer to the port_info instance
3339  *
3340  * Returns pointer to the successfully allocated VSI software struct
3341  * on success, otherwise returns NULL on failure.
3342  */
3343 static struct ice_vsi *
3344 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3345 {
3346 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID, NULL);
3347 }
3348 
3349 static struct ice_vsi *
3350 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3351 		   struct ice_channel *ch)
3352 {
3353 	return ice_vsi_setup(pf, pi, ICE_VSI_CHNL, ICE_INVAL_VFID, ch);
3354 }
3355 
3356 /**
3357  * ice_ctrl_vsi_setup - Set up a control VSI
3358  * @pf: board private structure
3359  * @pi: pointer to the port_info instance
3360  *
3361  * Returns pointer to the successfully allocated VSI software struct
3362  * on success, otherwise returns NULL on failure.
3363  */
3364 static struct ice_vsi *
3365 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3366 {
3367 	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, ICE_INVAL_VFID, NULL);
3368 }
3369 
3370 /**
3371  * ice_lb_vsi_setup - Set up a loopback VSI
3372  * @pf: board private structure
3373  * @pi: pointer to the port_info instance
3374  *
3375  * Returns pointer to the successfully allocated VSI software struct
3376  * on success, otherwise returns NULL on failure.
3377  */
3378 struct ice_vsi *
3379 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3380 {
3381 	return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID, NULL);
3382 }
3383 
3384 /**
3385  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3386  * @netdev: network interface to be adjusted
3387  * @proto: unused protocol
3388  * @vid: VLAN ID to be added
3389  *
3390  * net_device_ops implementation for adding VLAN IDs
3391  */
3392 static int
3393 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
3394 		    u16 vid)
3395 {
3396 	struct ice_netdev_priv *np = netdev_priv(netdev);
3397 	struct ice_vsi *vsi = np->vsi;
3398 	int ret;
3399 
3400 	/* VLAN 0 is added by default during load/reset */
3401 	if (!vid)
3402 		return 0;
3403 
3404 	/* Enable VLAN pruning when a VLAN other than 0 is added */
3405 	if (!ice_vsi_is_vlan_pruning_ena(vsi)) {
3406 		ret = ice_cfg_vlan_pruning(vsi, true);
3407 		if (ret)
3408 			return ret;
3409 	}
3410 
3411 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3412 	 * packets aren't pruned by the device's internal switch on Rx
3413 	 */
3414 	ret = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3415 	if (!ret)
3416 		set_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
3417 
3418 	return ret;
3419 }
3420 
3421 /**
3422  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3423  * @netdev: network interface to be adjusted
3424  * @proto: unused protocol
3425  * @vid: VLAN ID to be removed
3426  *
3427  * net_device_ops implementation for removing VLAN IDs
3428  */
3429 static int
3430 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
3431 		     u16 vid)
3432 {
3433 	struct ice_netdev_priv *np = netdev_priv(netdev);
3434 	struct ice_vsi *vsi = np->vsi;
3435 	int ret;
3436 
3437 	/* don't allow removal of VLAN 0 */
3438 	if (!vid)
3439 		return 0;
3440 
3441 	/* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3442 	 * information
3443 	 */
3444 	ret = ice_vsi_kill_vlan(vsi, vid);
3445 	if (ret)
3446 		return ret;
3447 
3448 	/* Disable pruning when VLAN 0 is the only VLAN rule */
3449 	if (vsi->num_vlan == 1 && ice_vsi_is_vlan_pruning_ena(vsi))
3450 		ret = ice_cfg_vlan_pruning(vsi, false);
3451 
3452 	set_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
3453 	return ret;
3454 }
3455 
3456 /**
3457  * ice_rep_indr_tc_block_unbind
3458  * @cb_priv: indirection block private data
3459  */
3460 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3461 {
3462 	struct ice_indr_block_priv *indr_priv = cb_priv;
3463 
3464 	list_del(&indr_priv->list);
3465 	kfree(indr_priv);
3466 }
3467 
3468 /**
3469  * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3470  * @vsi: VSI struct which has the netdev
3471  */
3472 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3473 {
3474 	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3475 
3476 	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3477 				 ice_rep_indr_tc_block_unbind);
3478 }
3479 
3480 /**
3481  * ice_tc_indir_block_remove - clean indirect TC block notifications
3482  * @pf: PF structure
3483  */
3484 static void ice_tc_indir_block_remove(struct ice_pf *pf)
3485 {
3486 	struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3487 
3488 	if (!pf_vsi)
3489 		return;
3490 
3491 	ice_tc_indir_block_unregister(pf_vsi);
3492 }
3493 
3494 /**
3495  * ice_tc_indir_block_register - Register TC indirect block notifications
3496  * @vsi: VSI struct which has the netdev
3497  *
3498  * Returns 0 on success, negative value on failure
3499  */
3500 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3501 {
3502 	struct ice_netdev_priv *np;
3503 
3504 	if (!vsi || !vsi->netdev)
3505 		return -EINVAL;
3506 
3507 	np = netdev_priv(vsi->netdev);
3508 
3509 	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3510 	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3511 }
3512 
3513 /**
3514  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3515  * @pf: board private structure
3516  *
3517  * Returns 0 on success, negative value on failure
3518  */
3519 static int ice_setup_pf_sw(struct ice_pf *pf)
3520 {
3521 	struct device *dev = ice_pf_to_dev(pf);
3522 	struct ice_vsi *vsi;
3523 	int status = 0;
3524 
3525 	if (ice_is_reset_in_progress(pf->state))
3526 		return -EBUSY;
3527 
3528 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3529 	if (!vsi)
3530 		return -ENOMEM;
3531 
3532 	/* init channel list */
3533 	INIT_LIST_HEAD(&vsi->ch_list);
3534 
3535 	status = ice_cfg_netdev(vsi);
3536 	if (status) {
3537 		status = -ENODEV;
3538 		goto unroll_vsi_setup;
3539 	}
3540 	/* netdev has to be configured before setting frame size */
3541 	ice_vsi_cfg_frame_size(vsi);
3542 
3543 	/* init indirect block notifications */
3544 	status = ice_tc_indir_block_register(vsi);
3545 	if (status) {
3546 		dev_err(dev, "Failed to register netdev notifier\n");
3547 		goto unroll_cfg_netdev;
3548 	}
3549 
3550 	/* Setup DCB netlink interface */
3551 	ice_dcbnl_setup(vsi);
3552 
3553 	/* registering the NAPI handler requires both the queues and
3554 	 * netdev to be created, which are done in ice_pf_vsi_setup()
3555 	 * and ice_cfg_netdev() respectively
3556 	 */
3557 	ice_napi_add(vsi);
3558 
3559 	status = ice_set_cpu_rx_rmap(vsi);
3560 	if (status) {
3561 		dev_err(dev, "Failed to set CPU Rx map VSI %d error %d\n",
3562 			vsi->vsi_num, status);
3563 		status = -EINVAL;
3564 		goto unroll_napi_add;
3565 	}
3566 	status = ice_init_mac_fltr(pf);
3567 	if (status)
3568 		goto free_cpu_rx_map;
3569 
3570 	return status;
3571 
3572 free_cpu_rx_map:
3573 	ice_free_cpu_rx_rmap(vsi);
3574 unroll_napi_add:
3575 	ice_tc_indir_block_unregister(vsi);
3576 unroll_cfg_netdev:
3577 	if (vsi) {
3578 		ice_napi_del(vsi);
3579 		if (vsi->netdev) {
3580 			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3581 			free_netdev(vsi->netdev);
3582 			vsi->netdev = NULL;
3583 		}
3584 	}
3585 
3586 unroll_vsi_setup:
3587 	ice_vsi_release(vsi);
3588 	return status;
3589 }
3590 
3591 /**
3592  * ice_get_avail_q_count - Get count of queues in use
3593  * @pf_qmap: bitmap to get queue use count from
3594  * @lock: pointer to a mutex that protects access to pf_qmap
3595  * @size: size of the bitmap
3596  */
3597 static u16
3598 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3599 {
3600 	unsigned long bit;
3601 	u16 count = 0;
3602 
3603 	mutex_lock(lock);
3604 	for_each_clear_bit(bit, pf_qmap, size)
3605 		count++;
3606 	mutex_unlock(lock);
3607 
3608 	return count;
3609 }
3610 
3611 /**
3612  * ice_get_avail_txq_count - Get count of Tx queues in use
3613  * @pf: pointer to an ice_pf instance
3614  */
3615 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3616 {
3617 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3618 				     pf->max_pf_txqs);
3619 }
3620 
3621 /**
3622  * ice_get_avail_rxq_count - Get count of Rx queues in use
3623  * @pf: pointer to an ice_pf instance
3624  */
3625 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3626 {
3627 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3628 				     pf->max_pf_rxqs);
3629 }
3630 
3631 /**
3632  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3633  * @pf: board private structure to initialize
3634  */
3635 static void ice_deinit_pf(struct ice_pf *pf)
3636 {
3637 	ice_service_task_stop(pf);
3638 	mutex_destroy(&pf->sw_mutex);
3639 	mutex_destroy(&pf->tc_mutex);
3640 	mutex_destroy(&pf->avail_q_mutex);
3641 
3642 	if (pf->avail_txqs) {
3643 		bitmap_free(pf->avail_txqs);
3644 		pf->avail_txqs = NULL;
3645 	}
3646 
3647 	if (pf->avail_rxqs) {
3648 		bitmap_free(pf->avail_rxqs);
3649 		pf->avail_rxqs = NULL;
3650 	}
3651 
3652 	if (pf->ptp.clock)
3653 		ptp_clock_unregister(pf->ptp.clock);
3654 }
3655 
3656 /**
3657  * ice_set_pf_caps - set PFs capability flags
3658  * @pf: pointer to the PF instance
3659  */
3660 static void ice_set_pf_caps(struct ice_pf *pf)
3661 {
3662 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3663 
3664 	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3665 	clear_bit(ICE_FLAG_AUX_ENA, pf->flags);
3666 	if (func_caps->common_cap.rdma) {
3667 		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3668 		set_bit(ICE_FLAG_AUX_ENA, pf->flags);
3669 	}
3670 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3671 	if (func_caps->common_cap.dcb)
3672 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3673 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3674 	if (func_caps->common_cap.sr_iov_1_1) {
3675 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3676 		pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
3677 					      ICE_MAX_VF_COUNT);
3678 	}
3679 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3680 	if (func_caps->common_cap.rss_table_size)
3681 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3682 
3683 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3684 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3685 		u16 unused;
3686 
3687 		/* ctrl_vsi_idx will be set to a valid value when flow director
3688 		 * is setup by ice_init_fdir
3689 		 */
3690 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3691 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3692 		/* force guaranteed filter pool for PF */
3693 		ice_alloc_fd_guar_item(&pf->hw, &unused,
3694 				       func_caps->fd_fltr_guar);
3695 		/* force shared filter pool for PF */
3696 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3697 				       func_caps->fd_fltr_best_effort);
3698 	}
3699 
3700 	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3701 	if (func_caps->common_cap.ieee_1588)
3702 		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3703 
3704 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3705 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3706 }
3707 
3708 /**
3709  * ice_init_pf - Initialize general software structures (struct ice_pf)
3710  * @pf: board private structure to initialize
3711  */
3712 static int ice_init_pf(struct ice_pf *pf)
3713 {
3714 	ice_set_pf_caps(pf);
3715 
3716 	mutex_init(&pf->sw_mutex);
3717 	mutex_init(&pf->tc_mutex);
3718 
3719 	INIT_HLIST_HEAD(&pf->aq_wait_list);
3720 	spin_lock_init(&pf->aq_wait_lock);
3721 	init_waitqueue_head(&pf->aq_wait_queue);
3722 
3723 	init_waitqueue_head(&pf->reset_wait_queue);
3724 
3725 	/* setup service timer and periodic service task */
3726 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3727 	pf->serv_tmr_period = HZ;
3728 	INIT_WORK(&pf->serv_task, ice_service_task);
3729 	clear_bit(ICE_SERVICE_SCHED, pf->state);
3730 
3731 	mutex_init(&pf->avail_q_mutex);
3732 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3733 	if (!pf->avail_txqs)
3734 		return -ENOMEM;
3735 
3736 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3737 	if (!pf->avail_rxqs) {
3738 		devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
3739 		pf->avail_txqs = NULL;
3740 		return -ENOMEM;
3741 	}
3742 
3743 	return 0;
3744 }
3745 
3746 /**
3747  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3748  * @pf: board private structure
3749  *
3750  * compute the number of MSIX vectors required (v_budget) and request from
3751  * the OS. Return the number of vectors reserved or negative on failure
3752  */
3753 static int ice_ena_msix_range(struct ice_pf *pf)
3754 {
3755 	int num_cpus, v_left, v_actual, v_other, v_budget = 0;
3756 	struct device *dev = ice_pf_to_dev(pf);
3757 	int needed, err, i;
3758 
3759 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3760 	num_cpus = num_online_cpus();
3761 
3762 	/* reserve for LAN miscellaneous handler */
3763 	needed = ICE_MIN_LAN_OICR_MSIX;
3764 	if (v_left < needed)
3765 		goto no_hw_vecs_left_err;
3766 	v_budget += needed;
3767 	v_left -= needed;
3768 
3769 	/* reserve for flow director */
3770 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3771 		needed = ICE_FDIR_MSIX;
3772 		if (v_left < needed)
3773 			goto no_hw_vecs_left_err;
3774 		v_budget += needed;
3775 		v_left -= needed;
3776 	}
3777 
3778 	/* reserve for switchdev */
3779 	needed = ICE_ESWITCH_MSIX;
3780 	if (v_left < needed)
3781 		goto no_hw_vecs_left_err;
3782 	v_budget += needed;
3783 	v_left -= needed;
3784 
3785 	/* total used for non-traffic vectors */
3786 	v_other = v_budget;
3787 
3788 	/* reserve vectors for LAN traffic */
3789 	needed = num_cpus;
3790 	if (v_left < needed)
3791 		goto no_hw_vecs_left_err;
3792 	pf->num_lan_msix = needed;
3793 	v_budget += needed;
3794 	v_left -= needed;
3795 
3796 	/* reserve vectors for RDMA auxiliary driver */
3797 	if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) {
3798 		needed = num_cpus + ICE_RDMA_NUM_AEQ_MSIX;
3799 		if (v_left < needed)
3800 			goto no_hw_vecs_left_err;
3801 		pf->num_rdma_msix = needed;
3802 		v_budget += needed;
3803 		v_left -= needed;
3804 	}
3805 
3806 	pf->msix_entries = devm_kcalloc(dev, v_budget,
3807 					sizeof(*pf->msix_entries), GFP_KERNEL);
3808 	if (!pf->msix_entries) {
3809 		err = -ENOMEM;
3810 		goto exit_err;
3811 	}
3812 
3813 	for (i = 0; i < v_budget; i++)
3814 		pf->msix_entries[i].entry = i;
3815 
3816 	/* actually reserve the vectors */
3817 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3818 					 ICE_MIN_MSIX, v_budget);
3819 	if (v_actual < 0) {
3820 		dev_err(dev, "unable to reserve MSI-X vectors\n");
3821 		err = v_actual;
3822 		goto msix_err;
3823 	}
3824 
3825 	if (v_actual < v_budget) {
3826 		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
3827 			 v_budget, v_actual);
3828 
3829 		if (v_actual < ICE_MIN_MSIX) {
3830 			/* error if we can't get minimum vectors */
3831 			pci_disable_msix(pf->pdev);
3832 			err = -ERANGE;
3833 			goto msix_err;
3834 		} else {
3835 			int v_remain = v_actual - v_other;
3836 			int v_rdma = 0, v_min_rdma = 0;
3837 
3838 			if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) {
3839 				/* Need at least 1 interrupt in addition to
3840 				 * AEQ MSIX
3841 				 */
3842 				v_rdma = ICE_RDMA_NUM_AEQ_MSIX + 1;
3843 				v_min_rdma = ICE_MIN_RDMA_MSIX;
3844 			}
3845 
3846 			if (v_actual == ICE_MIN_MSIX ||
3847 			    v_remain < ICE_MIN_LAN_TXRX_MSIX + v_min_rdma) {
3848 				dev_warn(dev, "Not enough MSI-X vectors to support RDMA.\n");
3849 				clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3850 
3851 				pf->num_rdma_msix = 0;
3852 				pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
3853 			} else if ((v_remain < ICE_MIN_LAN_TXRX_MSIX + v_rdma) ||
3854 				   (v_remain - v_rdma < v_rdma)) {
3855 				/* Support minimum RDMA and give remaining
3856 				 * vectors to LAN MSIX
3857 				 */
3858 				pf->num_rdma_msix = v_min_rdma;
3859 				pf->num_lan_msix = v_remain - v_min_rdma;
3860 			} else {
3861 				/* Split remaining MSIX with RDMA after
3862 				 * accounting for AEQ MSIX
3863 				 */
3864 				pf->num_rdma_msix = (v_remain - ICE_RDMA_NUM_AEQ_MSIX) / 2 +
3865 						    ICE_RDMA_NUM_AEQ_MSIX;
3866 				pf->num_lan_msix = v_remain - pf->num_rdma_msix;
3867 			}
3868 
3869 			dev_notice(dev, "Enabled %d MSI-X vectors for LAN traffic.\n",
3870 				   pf->num_lan_msix);
3871 
3872 			if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
3873 				dev_notice(dev, "Enabled %d MSI-X vectors for RDMA.\n",
3874 					   pf->num_rdma_msix);
3875 		}
3876 	}
3877 
3878 	return v_actual;
3879 
3880 msix_err:
3881 	devm_kfree(dev, pf->msix_entries);
3882 	goto exit_err;
3883 
3884 no_hw_vecs_left_err:
3885 	dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
3886 		needed, v_left);
3887 	err = -ERANGE;
3888 exit_err:
3889 	pf->num_rdma_msix = 0;
3890 	pf->num_lan_msix = 0;
3891 	return err;
3892 }
3893 
3894 /**
3895  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3896  * @pf: board private structure
3897  */
3898 static void ice_dis_msix(struct ice_pf *pf)
3899 {
3900 	pci_disable_msix(pf->pdev);
3901 	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
3902 	pf->msix_entries = NULL;
3903 }
3904 
3905 /**
3906  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3907  * @pf: board private structure
3908  */
3909 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3910 {
3911 	ice_dis_msix(pf);
3912 
3913 	if (pf->irq_tracker) {
3914 		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
3915 		pf->irq_tracker = NULL;
3916 	}
3917 }
3918 
3919 /**
3920  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3921  * @pf: board private structure to initialize
3922  */
3923 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3924 {
3925 	int vectors;
3926 
3927 	vectors = ice_ena_msix_range(pf);
3928 
3929 	if (vectors < 0)
3930 		return vectors;
3931 
3932 	/* set up vector assignment tracking */
3933 	pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
3934 				       struct_size(pf->irq_tracker, list, vectors),
3935 				       GFP_KERNEL);
3936 	if (!pf->irq_tracker) {
3937 		ice_dis_msix(pf);
3938 		return -ENOMEM;
3939 	}
3940 
3941 	/* populate SW interrupts pool with number of OS granted IRQs. */
3942 	pf->num_avail_sw_msix = (u16)vectors;
3943 	pf->irq_tracker->num_entries = (u16)vectors;
3944 	pf->irq_tracker->end = pf->irq_tracker->num_entries;
3945 
3946 	return 0;
3947 }
3948 
3949 /**
3950  * ice_is_wol_supported - check if WoL is supported
3951  * @hw: pointer to hardware info
3952  *
3953  * Check if WoL is supported based on the HW configuration.
3954  * Returns true if NVM supports and enables WoL for this port, false otherwise
3955  */
3956 bool ice_is_wol_supported(struct ice_hw *hw)
3957 {
3958 	u16 wol_ctrl;
3959 
3960 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3961 	 * word) indicates WoL is not supported on the corresponding PF ID.
3962 	 */
3963 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3964 		return false;
3965 
3966 	return !(BIT(hw->port_info->lport) & wol_ctrl);
3967 }
3968 
3969 /**
3970  * ice_vsi_recfg_qs - Change the number of queues on a VSI
3971  * @vsi: VSI being changed
3972  * @new_rx: new number of Rx queues
3973  * @new_tx: new number of Tx queues
3974  *
3975  * Only change the number of queues if new_tx, or new_rx is non-0.
3976  *
3977  * Returns 0 on success.
3978  */
3979 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
3980 {
3981 	struct ice_pf *pf = vsi->back;
3982 	int err = 0, timeout = 50;
3983 
3984 	if (!new_rx && !new_tx)
3985 		return -EINVAL;
3986 
3987 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
3988 		timeout--;
3989 		if (!timeout)
3990 			return -EBUSY;
3991 		usleep_range(1000, 2000);
3992 	}
3993 
3994 	if (new_tx)
3995 		vsi->req_txq = (u16)new_tx;
3996 	if (new_rx)
3997 		vsi->req_rxq = (u16)new_rx;
3998 
3999 	/* set for the next time the netdev is started */
4000 	if (!netif_running(vsi->netdev)) {
4001 		ice_vsi_rebuild(vsi, false);
4002 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4003 		goto done;
4004 	}
4005 
4006 	ice_vsi_close(vsi);
4007 	ice_vsi_rebuild(vsi, false);
4008 	ice_pf_dcb_recfg(pf);
4009 	ice_vsi_open(vsi);
4010 done:
4011 	clear_bit(ICE_CFG_BUSY, pf->state);
4012 	return err;
4013 }
4014 
4015 /**
4016  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4017  * @pf: PF to configure
4018  *
4019  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4020  * VSI can still Tx/Rx VLAN tagged packets.
4021  */
4022 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4023 {
4024 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4025 	struct ice_vsi_ctx *ctxt;
4026 	enum ice_status status;
4027 	struct ice_hw *hw;
4028 
4029 	if (!vsi)
4030 		return;
4031 
4032 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4033 	if (!ctxt)
4034 		return;
4035 
4036 	hw = &pf->hw;
4037 	ctxt->info = vsi->info;
4038 
4039 	ctxt->info.valid_sections =
4040 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4041 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4042 			    ICE_AQ_VSI_PROP_SW_VALID);
4043 
4044 	/* disable VLAN anti-spoof */
4045 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4046 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4047 
4048 	/* disable VLAN pruning and keep all other settings */
4049 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4050 
4051 	/* allow all VLANs on Tx and don't strip on Rx */
4052 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL |
4053 		ICE_AQ_VSI_VLAN_EMOD_NOTHING;
4054 
4055 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4056 	if (status) {
4057 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %s aq_err %s\n",
4058 			ice_stat_str(status),
4059 			ice_aq_str(hw->adminq.sq_last_status));
4060 	} else {
4061 		vsi->info.sec_flags = ctxt->info.sec_flags;
4062 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4063 		vsi->info.vlan_flags = ctxt->info.vlan_flags;
4064 	}
4065 
4066 	kfree(ctxt);
4067 }
4068 
4069 /**
4070  * ice_log_pkg_init - log result of DDP package load
4071  * @hw: pointer to hardware info
4072  * @status: status of package load
4073  */
4074 static void
4075 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status)
4076 {
4077 	struct ice_pf *pf = (struct ice_pf *)hw->back;
4078 	struct device *dev = ice_pf_to_dev(pf);
4079 
4080 	switch (*status) {
4081 	case ICE_SUCCESS:
4082 		/* The package download AdminQ command returned success because
4083 		 * this download succeeded or ICE_ERR_AQ_NO_WORK since there is
4084 		 * already a package loaded on the device.
4085 		 */
4086 		if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
4087 		    hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
4088 		    hw->pkg_ver.update == hw->active_pkg_ver.update &&
4089 		    hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
4090 		    !memcmp(hw->pkg_name, hw->active_pkg_name,
4091 			    sizeof(hw->pkg_name))) {
4092 			if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST)
4093 				dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4094 					 hw->active_pkg_name,
4095 					 hw->active_pkg_ver.major,
4096 					 hw->active_pkg_ver.minor,
4097 					 hw->active_pkg_ver.update,
4098 					 hw->active_pkg_ver.draft);
4099 			else
4100 				dev_info(dev, "The DDP package was successfully loaded: %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 		} else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
4107 			   hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
4108 			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",
4109 				hw->active_pkg_name,
4110 				hw->active_pkg_ver.major,
4111 				hw->active_pkg_ver.minor,
4112 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4113 			*status = ICE_ERR_NOT_SUPPORTED;
4114 		} else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
4115 			   hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
4116 			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",
4117 				 hw->active_pkg_name,
4118 				 hw->active_pkg_ver.major,
4119 				 hw->active_pkg_ver.minor,
4120 				 hw->active_pkg_ver.update,
4121 				 hw->active_pkg_ver.draft,
4122 				 hw->pkg_name,
4123 				 hw->pkg_ver.major,
4124 				 hw->pkg_ver.minor,
4125 				 hw->pkg_ver.update,
4126 				 hw->pkg_ver.draft);
4127 		} else {
4128 			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");
4129 			*status = ICE_ERR_NOT_SUPPORTED;
4130 		}
4131 		break;
4132 	case ICE_ERR_FW_DDP_MISMATCH:
4133 		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");
4134 		break;
4135 	case ICE_ERR_BUF_TOO_SHORT:
4136 	case ICE_ERR_CFG:
4137 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4138 		break;
4139 	case ICE_ERR_NOT_SUPPORTED:
4140 		/* Package File version not supported */
4141 		if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ ||
4142 		    (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
4143 		     hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR))
4144 			dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4145 		else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ ||
4146 			 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
4147 			  hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR))
4148 			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",
4149 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4150 		break;
4151 	case ICE_ERR_AQ_ERROR:
4152 		switch (hw->pkg_dwnld_status) {
4153 		case ICE_AQ_RC_ENOSEC:
4154 		case ICE_AQ_RC_EBADSIG:
4155 			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");
4156 			return;
4157 		case ICE_AQ_RC_ESVN:
4158 			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");
4159 			return;
4160 		case ICE_AQ_RC_EBADMAN:
4161 		case ICE_AQ_RC_EBADBUF:
4162 			dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4163 			/* poll for reset to complete */
4164 			if (ice_check_reset(hw))
4165 				dev_err(dev, "Error resetting device. Please reload the driver\n");
4166 			return;
4167 		default:
4168 			break;
4169 		}
4170 		fallthrough;
4171 	default:
4172 		dev_err(dev, "An unknown error (%d) occurred when loading the DDP package.  Entering Safe Mode.\n",
4173 			*status);
4174 		break;
4175 	}
4176 }
4177 
4178 /**
4179  * ice_load_pkg - load/reload the DDP Package file
4180  * @firmware: firmware structure when firmware requested or NULL for reload
4181  * @pf: pointer to the PF instance
4182  *
4183  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4184  * initialize HW tables.
4185  */
4186 static void
4187 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4188 {
4189 	enum ice_status status = ICE_ERR_PARAM;
4190 	struct device *dev = ice_pf_to_dev(pf);
4191 	struct ice_hw *hw = &pf->hw;
4192 
4193 	/* Load DDP Package */
4194 	if (firmware && !hw->pkg_copy) {
4195 		status = ice_copy_and_init_pkg(hw, firmware->data,
4196 					       firmware->size);
4197 		ice_log_pkg_init(hw, &status);
4198 	} else if (!firmware && hw->pkg_copy) {
4199 		/* Reload package during rebuild after CORER/GLOBR reset */
4200 		status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4201 		ice_log_pkg_init(hw, &status);
4202 	} else {
4203 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4204 	}
4205 
4206 	if (status) {
4207 		/* Safe Mode */
4208 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4209 		return;
4210 	}
4211 
4212 	/* Successful download package is the precondition for advanced
4213 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4214 	 */
4215 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4216 }
4217 
4218 /**
4219  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4220  * @pf: pointer to the PF structure
4221  *
4222  * There is no error returned here because the driver should be able to handle
4223  * 128 Byte cache lines, so we only print a warning in case issues are seen,
4224  * specifically with Tx.
4225  */
4226 static void ice_verify_cacheline_size(struct ice_pf *pf)
4227 {
4228 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4229 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4230 			 ICE_CACHE_LINE_BYTES);
4231 }
4232 
4233 /**
4234  * ice_send_version - update firmware with driver version
4235  * @pf: PF struct
4236  *
4237  * Returns ICE_SUCCESS on success, else error code
4238  */
4239 static enum ice_status ice_send_version(struct ice_pf *pf)
4240 {
4241 	struct ice_driver_ver dv;
4242 
4243 	dv.major_ver = 0xff;
4244 	dv.minor_ver = 0xff;
4245 	dv.build_ver = 0xff;
4246 	dv.subbuild_ver = 0;
4247 	strscpy((char *)dv.driver_string, UTS_RELEASE,
4248 		sizeof(dv.driver_string));
4249 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4250 }
4251 
4252 /**
4253  * ice_init_fdir - Initialize flow director VSI and configuration
4254  * @pf: pointer to the PF instance
4255  *
4256  * returns 0 on success, negative on error
4257  */
4258 static int ice_init_fdir(struct ice_pf *pf)
4259 {
4260 	struct device *dev = ice_pf_to_dev(pf);
4261 	struct ice_vsi *ctrl_vsi;
4262 	int err;
4263 
4264 	/* Side Band Flow Director needs to have a control VSI.
4265 	 * Allocate it and store it in the PF.
4266 	 */
4267 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4268 	if (!ctrl_vsi) {
4269 		dev_dbg(dev, "could not create control VSI\n");
4270 		return -ENOMEM;
4271 	}
4272 
4273 	err = ice_vsi_open_ctrl(ctrl_vsi);
4274 	if (err) {
4275 		dev_dbg(dev, "could not open control VSI\n");
4276 		goto err_vsi_open;
4277 	}
4278 
4279 	mutex_init(&pf->hw.fdir_fltr_lock);
4280 
4281 	err = ice_fdir_create_dflt_rules(pf);
4282 	if (err)
4283 		goto err_fdir_rule;
4284 
4285 	return 0;
4286 
4287 err_fdir_rule:
4288 	ice_fdir_release_flows(&pf->hw);
4289 	ice_vsi_close(ctrl_vsi);
4290 err_vsi_open:
4291 	ice_vsi_release(ctrl_vsi);
4292 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4293 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4294 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4295 	}
4296 	return err;
4297 }
4298 
4299 /**
4300  * ice_get_opt_fw_name - return optional firmware file name or NULL
4301  * @pf: pointer to the PF instance
4302  */
4303 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4304 {
4305 	/* Optional firmware name same as default with additional dash
4306 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4307 	 */
4308 	struct pci_dev *pdev = pf->pdev;
4309 	char *opt_fw_filename;
4310 	u64 dsn;
4311 
4312 	/* Determine the name of the optional file using the DSN (two
4313 	 * dwords following the start of the DSN Capability).
4314 	 */
4315 	dsn = pci_get_dsn(pdev);
4316 	if (!dsn)
4317 		return NULL;
4318 
4319 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4320 	if (!opt_fw_filename)
4321 		return NULL;
4322 
4323 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4324 		 ICE_DDP_PKG_PATH, dsn);
4325 
4326 	return opt_fw_filename;
4327 }
4328 
4329 /**
4330  * ice_request_fw - Device initialization routine
4331  * @pf: pointer to the PF instance
4332  */
4333 static void ice_request_fw(struct ice_pf *pf)
4334 {
4335 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4336 	const struct firmware *firmware = NULL;
4337 	struct device *dev = ice_pf_to_dev(pf);
4338 	int err = 0;
4339 
4340 	/* optional device-specific DDP (if present) overrides the default DDP
4341 	 * package file. kernel logs a debug message if the file doesn't exist,
4342 	 * and warning messages for other errors.
4343 	 */
4344 	if (opt_fw_filename) {
4345 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4346 		if (err) {
4347 			kfree(opt_fw_filename);
4348 			goto dflt_pkg_load;
4349 		}
4350 
4351 		/* request for firmware was successful. Download to device */
4352 		ice_load_pkg(firmware, pf);
4353 		kfree(opt_fw_filename);
4354 		release_firmware(firmware);
4355 		return;
4356 	}
4357 
4358 dflt_pkg_load:
4359 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4360 	if (err) {
4361 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4362 		return;
4363 	}
4364 
4365 	/* request for firmware was successful. Download to device */
4366 	ice_load_pkg(firmware, pf);
4367 	release_firmware(firmware);
4368 }
4369 
4370 /**
4371  * ice_print_wake_reason - show the wake up cause in the log
4372  * @pf: pointer to the PF struct
4373  */
4374 static void ice_print_wake_reason(struct ice_pf *pf)
4375 {
4376 	u32 wus = pf->wakeup_reason;
4377 	const char *wake_str;
4378 
4379 	/* if no wake event, nothing to print */
4380 	if (!wus)
4381 		return;
4382 
4383 	if (wus & PFPM_WUS_LNKC_M)
4384 		wake_str = "Link\n";
4385 	else if (wus & PFPM_WUS_MAG_M)
4386 		wake_str = "Magic Packet\n";
4387 	else if (wus & PFPM_WUS_MNG_M)
4388 		wake_str = "Management\n";
4389 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4390 		wake_str = "Firmware Reset\n";
4391 	else
4392 		wake_str = "Unknown\n";
4393 
4394 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4395 }
4396 
4397 /**
4398  * ice_register_netdev - register netdev and devlink port
4399  * @pf: pointer to the PF struct
4400  */
4401 static int ice_register_netdev(struct ice_pf *pf)
4402 {
4403 	struct ice_vsi *vsi;
4404 	int err = 0;
4405 
4406 	vsi = ice_get_main_vsi(pf);
4407 	if (!vsi || !vsi->netdev)
4408 		return -EIO;
4409 
4410 	err = register_netdev(vsi->netdev);
4411 	if (err)
4412 		goto err_register_netdev;
4413 
4414 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4415 	netif_carrier_off(vsi->netdev);
4416 	netif_tx_stop_all_queues(vsi->netdev);
4417 	err = ice_devlink_create_pf_port(pf);
4418 	if (err)
4419 		goto err_devlink_create;
4420 
4421 	devlink_port_type_eth_set(&pf->devlink_port, vsi->netdev);
4422 
4423 	return 0;
4424 err_devlink_create:
4425 	unregister_netdev(vsi->netdev);
4426 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4427 err_register_netdev:
4428 	free_netdev(vsi->netdev);
4429 	vsi->netdev = NULL;
4430 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4431 	return err;
4432 }
4433 
4434 /**
4435  * ice_probe - Device initialization routine
4436  * @pdev: PCI device information struct
4437  * @ent: entry in ice_pci_tbl
4438  *
4439  * Returns 0 on success, negative on failure
4440  */
4441 static int
4442 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
4443 {
4444 	struct device *dev = &pdev->dev;
4445 	struct ice_pf *pf;
4446 	struct ice_hw *hw;
4447 	int i, err;
4448 
4449 	if (pdev->is_virtfn) {
4450 		dev_err(dev, "can't probe a virtual function\n");
4451 		return -EINVAL;
4452 	}
4453 
4454 	/* this driver uses devres, see
4455 	 * Documentation/driver-api/driver-model/devres.rst
4456 	 */
4457 	err = pcim_enable_device(pdev);
4458 	if (err)
4459 		return err;
4460 
4461 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
4462 	if (err) {
4463 		dev_err(dev, "BAR0 I/O map error %d\n", err);
4464 		return err;
4465 	}
4466 
4467 	pf = ice_allocate_pf(dev);
4468 	if (!pf)
4469 		return -ENOMEM;
4470 
4471 	/* initialize Auxiliary index to invalid value */
4472 	pf->aux_idx = -1;
4473 
4474 	/* set up for high or low DMA */
4475 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4476 	if (err)
4477 		err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
4478 	if (err) {
4479 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4480 		return err;
4481 	}
4482 
4483 	pci_enable_pcie_error_reporting(pdev);
4484 	pci_set_master(pdev);
4485 
4486 	pf->pdev = pdev;
4487 	pci_set_drvdata(pdev, pf);
4488 	set_bit(ICE_DOWN, pf->state);
4489 	/* Disable service task until DOWN bit is cleared */
4490 	set_bit(ICE_SERVICE_DIS, pf->state);
4491 
4492 	hw = &pf->hw;
4493 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4494 	pci_save_state(pdev);
4495 
4496 	hw->back = pf;
4497 	hw->vendor_id = pdev->vendor;
4498 	hw->device_id = pdev->device;
4499 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4500 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
4501 	hw->subsystem_device_id = pdev->subsystem_device;
4502 	hw->bus.device = PCI_SLOT(pdev->devfn);
4503 	hw->bus.func = PCI_FUNC(pdev->devfn);
4504 	ice_set_ctrlq_len(hw);
4505 
4506 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4507 
4508 #ifndef CONFIG_DYNAMIC_DEBUG
4509 	if (debug < -1)
4510 		hw->debug_mask = debug;
4511 #endif
4512 
4513 	err = ice_init_hw(hw);
4514 	if (err) {
4515 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4516 		err = -EIO;
4517 		goto err_exit_unroll;
4518 	}
4519 
4520 	ice_init_feature_support(pf);
4521 
4522 	ice_request_fw(pf);
4523 
4524 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4525 	 * set in pf->state, which will cause ice_is_safe_mode to return
4526 	 * true
4527 	 */
4528 	if (ice_is_safe_mode(pf)) {
4529 		dev_err(dev, "Package download failed. Advanced features disabled - Device now in Safe Mode\n");
4530 		/* we already got function/device capabilities but these don't
4531 		 * reflect what the driver needs to do in safe mode. Instead of
4532 		 * adding conditional logic everywhere to ignore these
4533 		 * device/function capabilities, override them.
4534 		 */
4535 		ice_set_safe_mode_caps(hw);
4536 	}
4537 
4538 	err = ice_init_pf(pf);
4539 	if (err) {
4540 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4541 		goto err_init_pf_unroll;
4542 	}
4543 
4544 	ice_devlink_init_regions(pf);
4545 
4546 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4547 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4548 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4549 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4550 	i = 0;
4551 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4552 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4553 			pf->hw.tnl.valid_count[TNL_VXLAN];
4554 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4555 			UDP_TUNNEL_TYPE_VXLAN;
4556 		i++;
4557 	}
4558 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4559 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4560 			pf->hw.tnl.valid_count[TNL_GENEVE];
4561 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4562 			UDP_TUNNEL_TYPE_GENEVE;
4563 		i++;
4564 	}
4565 
4566 	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4567 	if (!pf->num_alloc_vsi) {
4568 		err = -EIO;
4569 		goto err_init_pf_unroll;
4570 	}
4571 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4572 		dev_warn(&pf->pdev->dev,
4573 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4574 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4575 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4576 	}
4577 
4578 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4579 			       GFP_KERNEL);
4580 	if (!pf->vsi) {
4581 		err = -ENOMEM;
4582 		goto err_init_pf_unroll;
4583 	}
4584 
4585 	err = ice_init_interrupt_scheme(pf);
4586 	if (err) {
4587 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4588 		err = -EIO;
4589 		goto err_init_vsi_unroll;
4590 	}
4591 
4592 	/* In case of MSIX we are going to setup the misc vector right here
4593 	 * to handle admin queue events etc. In case of legacy and MSI
4594 	 * the misc functionality and queue processing is combined in
4595 	 * the same vector and that gets setup at open.
4596 	 */
4597 	err = ice_req_irq_msix_misc(pf);
4598 	if (err) {
4599 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4600 		goto err_init_interrupt_unroll;
4601 	}
4602 
4603 	/* create switch struct for the switch element created by FW on boot */
4604 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4605 	if (!pf->first_sw) {
4606 		err = -ENOMEM;
4607 		goto err_msix_misc_unroll;
4608 	}
4609 
4610 	if (hw->evb_veb)
4611 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4612 	else
4613 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4614 
4615 	pf->first_sw->pf = pf;
4616 
4617 	/* record the sw_id available for later use */
4618 	pf->first_sw->sw_id = hw->port_info->sw_id;
4619 
4620 	err = ice_setup_pf_sw(pf);
4621 	if (err) {
4622 		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4623 		goto err_alloc_sw_unroll;
4624 	}
4625 
4626 	clear_bit(ICE_SERVICE_DIS, pf->state);
4627 
4628 	/* tell the firmware we are up */
4629 	err = ice_send_version(pf);
4630 	if (err) {
4631 		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4632 			UTS_RELEASE, err);
4633 		goto err_send_version_unroll;
4634 	}
4635 
4636 	/* since everything is good, start the service timer */
4637 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4638 
4639 	err = ice_init_link_events(pf->hw.port_info);
4640 	if (err) {
4641 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4642 		goto err_send_version_unroll;
4643 	}
4644 
4645 	/* not a fatal error if this fails */
4646 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4647 	if (err)
4648 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4649 
4650 	/* not a fatal error if this fails */
4651 	err = ice_update_link_info(pf->hw.port_info);
4652 	if (err)
4653 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4654 
4655 	ice_init_link_dflt_override(pf->hw.port_info);
4656 
4657 	ice_check_link_cfg_err(pf,
4658 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4659 
4660 	/* if media available, initialize PHY settings */
4661 	if (pf->hw.port_info->phy.link_info.link_info &
4662 	    ICE_AQ_MEDIA_AVAILABLE) {
4663 		/* not a fatal error if this fails */
4664 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4665 		if (err)
4666 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4667 
4668 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4669 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4670 
4671 			if (vsi)
4672 				ice_configure_phy(vsi);
4673 		}
4674 	} else {
4675 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4676 	}
4677 
4678 	ice_verify_cacheline_size(pf);
4679 
4680 	/* Save wakeup reason register for later use */
4681 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4682 
4683 	/* check for a power management event */
4684 	ice_print_wake_reason(pf);
4685 
4686 	/* clear wake status, all bits */
4687 	wr32(hw, PFPM_WUS, U32_MAX);
4688 
4689 	/* Disable WoL at init, wait for user to enable */
4690 	device_set_wakeup_enable(dev, false);
4691 
4692 	if (ice_is_safe_mode(pf)) {
4693 		ice_set_safe_mode_vlan_cfg(pf);
4694 		goto probe_done;
4695 	}
4696 
4697 	/* initialize DDP driven features */
4698 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4699 		ice_ptp_init(pf);
4700 
4701 	/* Note: Flow director init failure is non-fatal to load */
4702 	if (ice_init_fdir(pf))
4703 		dev_err(dev, "could not initialize flow director\n");
4704 
4705 	/* Note: DCB init failure is non-fatal to load */
4706 	if (ice_init_pf_dcb(pf, false)) {
4707 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4708 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4709 	} else {
4710 		ice_cfg_lldp_mib_change(&pf->hw, true);
4711 	}
4712 
4713 	if (ice_init_lag(pf))
4714 		dev_warn(dev, "Failed to init link aggregation support\n");
4715 
4716 	/* print PCI link speed and width */
4717 	pcie_print_link_status(pf->pdev);
4718 
4719 probe_done:
4720 	err = ice_register_netdev(pf);
4721 	if (err)
4722 		goto err_netdev_reg;
4723 
4724 	/* ready to go, so clear down state bit */
4725 	clear_bit(ICE_DOWN, pf->state);
4726 	if (ice_is_aux_ena(pf)) {
4727 		pf->aux_idx = ida_alloc(&ice_aux_ida, GFP_KERNEL);
4728 		if (pf->aux_idx < 0) {
4729 			dev_err(dev, "Failed to allocate device ID for AUX driver\n");
4730 			err = -ENOMEM;
4731 			goto err_netdev_reg;
4732 		}
4733 
4734 		err = ice_init_rdma(pf);
4735 		if (err) {
4736 			dev_err(dev, "Failed to initialize RDMA: %d\n", err);
4737 			err = -EIO;
4738 			goto err_init_aux_unroll;
4739 		}
4740 	} else {
4741 		dev_warn(dev, "RDMA is not supported on this device\n");
4742 	}
4743 
4744 	ice_devlink_register(pf);
4745 	return 0;
4746 
4747 err_init_aux_unroll:
4748 	pf->adev = NULL;
4749 	ida_free(&ice_aux_ida, pf->aux_idx);
4750 err_netdev_reg:
4751 err_send_version_unroll:
4752 	ice_vsi_release_all(pf);
4753 err_alloc_sw_unroll:
4754 	set_bit(ICE_SERVICE_DIS, pf->state);
4755 	set_bit(ICE_DOWN, pf->state);
4756 	devm_kfree(dev, pf->first_sw);
4757 err_msix_misc_unroll:
4758 	ice_free_irq_msix_misc(pf);
4759 err_init_interrupt_unroll:
4760 	ice_clear_interrupt_scheme(pf);
4761 err_init_vsi_unroll:
4762 	devm_kfree(dev, pf->vsi);
4763 err_init_pf_unroll:
4764 	ice_deinit_pf(pf);
4765 	ice_devlink_destroy_regions(pf);
4766 	ice_deinit_hw(hw);
4767 err_exit_unroll:
4768 	pci_disable_pcie_error_reporting(pdev);
4769 	pci_disable_device(pdev);
4770 	return err;
4771 }
4772 
4773 /**
4774  * ice_set_wake - enable or disable Wake on LAN
4775  * @pf: pointer to the PF struct
4776  *
4777  * Simple helper for WoL control
4778  */
4779 static void ice_set_wake(struct ice_pf *pf)
4780 {
4781 	struct ice_hw *hw = &pf->hw;
4782 	bool wol = pf->wol_ena;
4783 
4784 	/* clear wake state, otherwise new wake events won't fire */
4785 	wr32(hw, PFPM_WUS, U32_MAX);
4786 
4787 	/* enable / disable APM wake up, no RMW needed */
4788 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4789 
4790 	/* set magic packet filter enabled */
4791 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4792 }
4793 
4794 /**
4795  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
4796  * @pf: pointer to the PF struct
4797  *
4798  * Issue firmware command to enable multicast magic wake, making
4799  * sure that any locally administered address (LAA) is used for
4800  * wake, and that PF reset doesn't undo the LAA.
4801  */
4802 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4803 {
4804 	struct device *dev = ice_pf_to_dev(pf);
4805 	struct ice_hw *hw = &pf->hw;
4806 	enum ice_status status;
4807 	u8 mac_addr[ETH_ALEN];
4808 	struct ice_vsi *vsi;
4809 	u8 flags;
4810 
4811 	if (!pf->wol_ena)
4812 		return;
4813 
4814 	vsi = ice_get_main_vsi(pf);
4815 	if (!vsi)
4816 		return;
4817 
4818 	/* Get current MAC address in case it's an LAA */
4819 	if (vsi->netdev)
4820 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4821 	else
4822 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4823 
4824 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4825 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4826 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4827 
4828 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4829 	if (status)
4830 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %s aq_err %s\n",
4831 			ice_stat_str(status),
4832 			ice_aq_str(hw->adminq.sq_last_status));
4833 }
4834 
4835 /**
4836  * ice_remove - Device removal routine
4837  * @pdev: PCI device information struct
4838  */
4839 static void ice_remove(struct pci_dev *pdev)
4840 {
4841 	struct ice_pf *pf = pci_get_drvdata(pdev);
4842 	int i;
4843 
4844 	ice_devlink_unregister(pf);
4845 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4846 		if (!ice_is_reset_in_progress(pf->state))
4847 			break;
4848 		msleep(100);
4849 	}
4850 
4851 	ice_tc_indir_block_remove(pf);
4852 
4853 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4854 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
4855 		ice_free_vfs(pf);
4856 	}
4857 
4858 	ice_service_task_stop(pf);
4859 
4860 	ice_aq_cancel_waiting_tasks(pf);
4861 	ice_unplug_aux_dev(pf);
4862 	if (pf->aux_idx >= 0)
4863 		ida_free(&ice_aux_ida, pf->aux_idx);
4864 	set_bit(ICE_DOWN, pf->state);
4865 
4866 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4867 	ice_deinit_lag(pf);
4868 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4869 		ice_ptp_release(pf);
4870 	if (!ice_is_safe_mode(pf))
4871 		ice_remove_arfs(pf);
4872 	ice_setup_mc_magic_wake(pf);
4873 	ice_vsi_release_all(pf);
4874 	ice_set_wake(pf);
4875 	ice_free_irq_msix_misc(pf);
4876 	ice_for_each_vsi(pf, i) {
4877 		if (!pf->vsi[i])
4878 			continue;
4879 		ice_vsi_free_q_vectors(pf->vsi[i]);
4880 	}
4881 	ice_deinit_pf(pf);
4882 	ice_devlink_destroy_regions(pf);
4883 	ice_deinit_hw(&pf->hw);
4884 
4885 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
4886 	 * do it via ice_schedule_reset() since there is no need to rebuild
4887 	 * and the service task is already stopped.
4888 	 */
4889 	ice_reset(&pf->hw, ICE_RESET_PFR);
4890 	pci_wait_for_pending_transaction(pdev);
4891 	ice_clear_interrupt_scheme(pf);
4892 	pci_disable_pcie_error_reporting(pdev);
4893 	pci_disable_device(pdev);
4894 }
4895 
4896 /**
4897  * ice_shutdown - PCI callback for shutting down device
4898  * @pdev: PCI device information struct
4899  */
4900 static void ice_shutdown(struct pci_dev *pdev)
4901 {
4902 	struct ice_pf *pf = pci_get_drvdata(pdev);
4903 
4904 	ice_remove(pdev);
4905 
4906 	if (system_state == SYSTEM_POWER_OFF) {
4907 		pci_wake_from_d3(pdev, pf->wol_ena);
4908 		pci_set_power_state(pdev, PCI_D3hot);
4909 	}
4910 }
4911 
4912 #ifdef CONFIG_PM
4913 /**
4914  * ice_prepare_for_shutdown - prep for PCI shutdown
4915  * @pf: board private structure
4916  *
4917  * Inform or close all dependent features in prep for PCI device shutdown
4918  */
4919 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4920 {
4921 	struct ice_hw *hw = &pf->hw;
4922 	u32 v;
4923 
4924 	/* Notify VFs of impending reset */
4925 	if (ice_check_sq_alive(hw, &hw->mailboxq))
4926 		ice_vc_notify_reset(pf);
4927 
4928 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4929 
4930 	/* disable the VSIs and their queues that are not already DOWN */
4931 	ice_pf_dis_all_vsi(pf, false);
4932 
4933 	ice_for_each_vsi(pf, v)
4934 		if (pf->vsi[v])
4935 			pf->vsi[v]->vsi_num = 0;
4936 
4937 	ice_shutdown_all_ctrlq(hw);
4938 }
4939 
4940 /**
4941  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4942  * @pf: board private structure to reinitialize
4943  *
4944  * This routine reinitialize interrupt scheme that was cleared during
4945  * power management suspend callback.
4946  *
4947  * This should be called during resume routine to re-allocate the q_vectors
4948  * and reacquire interrupts.
4949  */
4950 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4951 {
4952 	struct device *dev = ice_pf_to_dev(pf);
4953 	int ret, v;
4954 
4955 	/* Since we clear MSIX flag during suspend, we need to
4956 	 * set it back during resume...
4957 	 */
4958 
4959 	ret = ice_init_interrupt_scheme(pf);
4960 	if (ret) {
4961 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4962 		return ret;
4963 	}
4964 
4965 	/* Remap vectors and rings, after successful re-init interrupts */
4966 	ice_for_each_vsi(pf, v) {
4967 		if (!pf->vsi[v])
4968 			continue;
4969 
4970 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4971 		if (ret)
4972 			goto err_reinit;
4973 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4974 	}
4975 
4976 	ret = ice_req_irq_msix_misc(pf);
4977 	if (ret) {
4978 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4979 			ret);
4980 		goto err_reinit;
4981 	}
4982 
4983 	return 0;
4984 
4985 err_reinit:
4986 	while (v--)
4987 		if (pf->vsi[v])
4988 			ice_vsi_free_q_vectors(pf->vsi[v]);
4989 
4990 	return ret;
4991 }
4992 
4993 /**
4994  * ice_suspend
4995  * @dev: generic device information structure
4996  *
4997  * Power Management callback to quiesce the device and prepare
4998  * for D3 transition.
4999  */
5000 static int __maybe_unused ice_suspend(struct device *dev)
5001 {
5002 	struct pci_dev *pdev = to_pci_dev(dev);
5003 	struct ice_pf *pf;
5004 	int disabled, v;
5005 
5006 	pf = pci_get_drvdata(pdev);
5007 
5008 	if (!ice_pf_state_is_nominal(pf)) {
5009 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5010 		return -EBUSY;
5011 	}
5012 
5013 	/* Stop watchdog tasks until resume completion.
5014 	 * Even though it is most likely that the service task is
5015 	 * disabled if the device is suspended or down, the service task's
5016 	 * state is controlled by a different state bit, and we should
5017 	 * store and honor whatever state that bit is in at this point.
5018 	 */
5019 	disabled = ice_service_task_stop(pf);
5020 
5021 	ice_unplug_aux_dev(pf);
5022 
5023 	/* Already suspended?, then there is nothing to do */
5024 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5025 		if (!disabled)
5026 			ice_service_task_restart(pf);
5027 		return 0;
5028 	}
5029 
5030 	if (test_bit(ICE_DOWN, pf->state) ||
5031 	    ice_is_reset_in_progress(pf->state)) {
5032 		dev_err(dev, "can't suspend device in reset or already down\n");
5033 		if (!disabled)
5034 			ice_service_task_restart(pf);
5035 		return 0;
5036 	}
5037 
5038 	ice_setup_mc_magic_wake(pf);
5039 
5040 	ice_prepare_for_shutdown(pf);
5041 
5042 	ice_set_wake(pf);
5043 
5044 	/* Free vectors, clear the interrupt scheme and release IRQs
5045 	 * for proper hibernation, especially with large number of CPUs.
5046 	 * Otherwise hibernation might fail when mapping all the vectors back
5047 	 * to CPU0.
5048 	 */
5049 	ice_free_irq_msix_misc(pf);
5050 	ice_for_each_vsi(pf, v) {
5051 		if (!pf->vsi[v])
5052 			continue;
5053 		ice_vsi_free_q_vectors(pf->vsi[v]);
5054 	}
5055 	ice_free_cpu_rx_rmap(ice_get_main_vsi(pf));
5056 	ice_clear_interrupt_scheme(pf);
5057 
5058 	pci_save_state(pdev);
5059 	pci_wake_from_d3(pdev, pf->wol_ena);
5060 	pci_set_power_state(pdev, PCI_D3hot);
5061 	return 0;
5062 }
5063 
5064 /**
5065  * ice_resume - PM callback for waking up from D3
5066  * @dev: generic device information structure
5067  */
5068 static int __maybe_unused ice_resume(struct device *dev)
5069 {
5070 	struct pci_dev *pdev = to_pci_dev(dev);
5071 	enum ice_reset_req reset_type;
5072 	struct ice_pf *pf;
5073 	struct ice_hw *hw;
5074 	int ret;
5075 
5076 	pci_set_power_state(pdev, PCI_D0);
5077 	pci_restore_state(pdev);
5078 	pci_save_state(pdev);
5079 
5080 	if (!pci_device_is_present(pdev))
5081 		return -ENODEV;
5082 
5083 	ret = pci_enable_device_mem(pdev);
5084 	if (ret) {
5085 		dev_err(dev, "Cannot enable device after suspend\n");
5086 		return ret;
5087 	}
5088 
5089 	pf = pci_get_drvdata(pdev);
5090 	hw = &pf->hw;
5091 
5092 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5093 	ice_print_wake_reason(pf);
5094 
5095 	/* We cleared the interrupt scheme when we suspended, so we need to
5096 	 * restore it now to resume device functionality.
5097 	 */
5098 	ret = ice_reinit_interrupt_scheme(pf);
5099 	if (ret)
5100 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5101 
5102 	clear_bit(ICE_DOWN, pf->state);
5103 	/* Now perform PF reset and rebuild */
5104 	reset_type = ICE_RESET_PFR;
5105 	/* re-enable service task for reset, but allow reset to schedule it */
5106 	clear_bit(ICE_SERVICE_DIS, pf->state);
5107 
5108 	if (ice_schedule_reset(pf, reset_type))
5109 		dev_err(dev, "Reset during resume failed.\n");
5110 
5111 	clear_bit(ICE_SUSPENDED, pf->state);
5112 	ice_service_task_restart(pf);
5113 
5114 	/* Restart the service task */
5115 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5116 
5117 	return 0;
5118 }
5119 #endif /* CONFIG_PM */
5120 
5121 /**
5122  * ice_pci_err_detected - warning that PCI error has been detected
5123  * @pdev: PCI device information struct
5124  * @err: the type of PCI error
5125  *
5126  * Called to warn that something happened on the PCI bus and the error handling
5127  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5128  */
5129 static pci_ers_result_t
5130 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5131 {
5132 	struct ice_pf *pf = pci_get_drvdata(pdev);
5133 
5134 	if (!pf) {
5135 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5136 			__func__, err);
5137 		return PCI_ERS_RESULT_DISCONNECT;
5138 	}
5139 
5140 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5141 		ice_service_task_stop(pf);
5142 
5143 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5144 			set_bit(ICE_PFR_REQ, pf->state);
5145 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5146 		}
5147 	}
5148 
5149 	return PCI_ERS_RESULT_NEED_RESET;
5150 }
5151 
5152 /**
5153  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5154  * @pdev: PCI device information struct
5155  *
5156  * Called to determine if the driver can recover from the PCI slot reset by
5157  * using a register read to determine if the device is recoverable.
5158  */
5159 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5160 {
5161 	struct ice_pf *pf = pci_get_drvdata(pdev);
5162 	pci_ers_result_t result;
5163 	int err;
5164 	u32 reg;
5165 
5166 	err = pci_enable_device_mem(pdev);
5167 	if (err) {
5168 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5169 			err);
5170 		result = PCI_ERS_RESULT_DISCONNECT;
5171 	} else {
5172 		pci_set_master(pdev);
5173 		pci_restore_state(pdev);
5174 		pci_save_state(pdev);
5175 		pci_wake_from_d3(pdev, false);
5176 
5177 		/* Check for life */
5178 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5179 		if (!reg)
5180 			result = PCI_ERS_RESULT_RECOVERED;
5181 		else
5182 			result = PCI_ERS_RESULT_DISCONNECT;
5183 	}
5184 
5185 	err = pci_aer_clear_nonfatal_status(pdev);
5186 	if (err)
5187 		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
5188 			err);
5189 		/* non-fatal, continue */
5190 
5191 	return result;
5192 }
5193 
5194 /**
5195  * ice_pci_err_resume - restart operations after PCI error recovery
5196  * @pdev: PCI device information struct
5197  *
5198  * Called to allow the driver to bring things back up after PCI error and/or
5199  * reset recovery have finished
5200  */
5201 static void ice_pci_err_resume(struct pci_dev *pdev)
5202 {
5203 	struct ice_pf *pf = pci_get_drvdata(pdev);
5204 
5205 	if (!pf) {
5206 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5207 			__func__);
5208 		return;
5209 	}
5210 
5211 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5212 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5213 			__func__);
5214 		return;
5215 	}
5216 
5217 	ice_restore_all_vfs_msi_state(pdev);
5218 
5219 	ice_do_reset(pf, ICE_RESET_PFR);
5220 	ice_service_task_restart(pf);
5221 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5222 }
5223 
5224 /**
5225  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5226  * @pdev: PCI device information struct
5227  */
5228 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5229 {
5230 	struct ice_pf *pf = pci_get_drvdata(pdev);
5231 
5232 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5233 		ice_service_task_stop(pf);
5234 
5235 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5236 			set_bit(ICE_PFR_REQ, pf->state);
5237 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5238 		}
5239 	}
5240 }
5241 
5242 /**
5243  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5244  * @pdev: PCI device information struct
5245  */
5246 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5247 {
5248 	ice_pci_err_resume(pdev);
5249 }
5250 
5251 /* ice_pci_tbl - PCI Device ID Table
5252  *
5253  * Wildcard entries (PCI_ANY_ID) should come last
5254  * Last entry must be all 0s
5255  *
5256  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5257  *   Class, Class Mask, private data (not used) }
5258  */
5259 static const struct pci_device_id ice_pci_tbl[] = {
5260 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5261 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5262 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5263 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5264 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5265 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5266 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5267 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5268 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5269 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5270 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5271 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5272 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5273 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5274 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5275 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5276 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5277 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5278 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5279 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5280 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5281 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5282 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5283 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5284 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5285 	/* required last entry */
5286 	{ 0, }
5287 };
5288 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5289 
5290 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5291 
5292 static const struct pci_error_handlers ice_pci_err_handler = {
5293 	.error_detected = ice_pci_err_detected,
5294 	.slot_reset = ice_pci_err_slot_reset,
5295 	.reset_prepare = ice_pci_err_reset_prepare,
5296 	.reset_done = ice_pci_err_reset_done,
5297 	.resume = ice_pci_err_resume
5298 };
5299 
5300 static struct pci_driver ice_driver = {
5301 	.name = KBUILD_MODNAME,
5302 	.id_table = ice_pci_tbl,
5303 	.probe = ice_probe,
5304 	.remove = ice_remove,
5305 #ifdef CONFIG_PM
5306 	.driver.pm = &ice_pm_ops,
5307 #endif /* CONFIG_PM */
5308 	.shutdown = ice_shutdown,
5309 	.sriov_configure = ice_sriov_configure,
5310 	.err_handler = &ice_pci_err_handler
5311 };
5312 
5313 /**
5314  * ice_module_init - Driver registration routine
5315  *
5316  * ice_module_init is the first routine called when the driver is
5317  * loaded. All it does is register with the PCI subsystem.
5318  */
5319 static int __init ice_module_init(void)
5320 {
5321 	int status;
5322 
5323 	pr_info("%s\n", ice_driver_string);
5324 	pr_info("%s\n", ice_copyright);
5325 
5326 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
5327 	if (!ice_wq) {
5328 		pr_err("Failed to create workqueue\n");
5329 		return -ENOMEM;
5330 	}
5331 
5332 	status = pci_register_driver(&ice_driver);
5333 	if (status) {
5334 		pr_err("failed to register PCI driver, err %d\n", status);
5335 		destroy_workqueue(ice_wq);
5336 	}
5337 
5338 	return status;
5339 }
5340 module_init(ice_module_init);
5341 
5342 /**
5343  * ice_module_exit - Driver exit cleanup routine
5344  *
5345  * ice_module_exit is called just before the driver is removed
5346  * from memory.
5347  */
5348 static void __exit ice_module_exit(void)
5349 {
5350 	pci_unregister_driver(&ice_driver);
5351 	destroy_workqueue(ice_wq);
5352 	pr_info("module unloaded\n");
5353 }
5354 module_exit(ice_module_exit);
5355 
5356 /**
5357  * ice_set_mac_address - NDO callback to set MAC address
5358  * @netdev: network interface device structure
5359  * @pi: pointer to an address structure
5360  *
5361  * Returns 0 on success, negative on failure
5362  */
5363 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5364 {
5365 	struct ice_netdev_priv *np = netdev_priv(netdev);
5366 	struct ice_vsi *vsi = np->vsi;
5367 	struct ice_pf *pf = vsi->back;
5368 	struct ice_hw *hw = &pf->hw;
5369 	struct sockaddr *addr = pi;
5370 	enum ice_status status;
5371 	u8 old_mac[ETH_ALEN];
5372 	u8 flags = 0;
5373 	int err = 0;
5374 	u8 *mac;
5375 
5376 	mac = (u8 *)addr->sa_data;
5377 
5378 	if (!is_valid_ether_addr(mac))
5379 		return -EADDRNOTAVAIL;
5380 
5381 	if (ether_addr_equal(netdev->dev_addr, mac)) {
5382 		netdev_dbg(netdev, "already using mac %pM\n", mac);
5383 		return 0;
5384 	}
5385 
5386 	if (test_bit(ICE_DOWN, pf->state) ||
5387 	    ice_is_reset_in_progress(pf->state)) {
5388 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5389 			   mac);
5390 		return -EBUSY;
5391 	}
5392 
5393 	if (ice_chnl_dmac_fltr_cnt(pf)) {
5394 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5395 			   mac);
5396 		return -EAGAIN;
5397 	}
5398 
5399 	netif_addr_lock_bh(netdev);
5400 	ether_addr_copy(old_mac, netdev->dev_addr);
5401 	/* change the netdev's MAC address */
5402 	eth_hw_addr_set(netdev, mac);
5403 	netif_addr_unlock_bh(netdev);
5404 
5405 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5406 	status = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5407 	if (status && status != ICE_ERR_DOES_NOT_EXIST) {
5408 		err = -EADDRNOTAVAIL;
5409 		goto err_update_filters;
5410 	}
5411 
5412 	/* Add filter for new MAC. If filter exists, return success */
5413 	status = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5414 	if (status == ICE_ERR_ALREADY_EXISTS)
5415 		/* Although this MAC filter is already present in hardware it's
5416 		 * possible in some cases (e.g. bonding) that dev_addr was
5417 		 * modified outside of the driver and needs to be restored back
5418 		 * to this value.
5419 		 */
5420 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5421 	else if (status)
5422 		/* error if the new filter addition failed */
5423 		err = -EADDRNOTAVAIL;
5424 
5425 err_update_filters:
5426 	if (err) {
5427 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5428 			   mac);
5429 		netif_addr_lock_bh(netdev);
5430 		eth_hw_addr_set(netdev, old_mac);
5431 		netif_addr_unlock_bh(netdev);
5432 		return err;
5433 	}
5434 
5435 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5436 		   netdev->dev_addr);
5437 
5438 	/* write new MAC address to the firmware */
5439 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5440 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5441 	if (status) {
5442 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %s\n",
5443 			   mac, ice_stat_str(status));
5444 	}
5445 	return 0;
5446 }
5447 
5448 /**
5449  * ice_set_rx_mode - NDO callback to set the netdev filters
5450  * @netdev: network interface device structure
5451  */
5452 static void ice_set_rx_mode(struct net_device *netdev)
5453 {
5454 	struct ice_netdev_priv *np = netdev_priv(netdev);
5455 	struct ice_vsi *vsi = np->vsi;
5456 
5457 	if (!vsi)
5458 		return;
5459 
5460 	/* Set the flags to synchronize filters
5461 	 * ndo_set_rx_mode may be triggered even without a change in netdev
5462 	 * flags
5463 	 */
5464 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5465 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5466 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5467 
5468 	/* schedule our worker thread which will take care of
5469 	 * applying the new filter changes
5470 	 */
5471 	ice_service_task_schedule(vsi->back);
5472 }
5473 
5474 /**
5475  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5476  * @netdev: network interface device structure
5477  * @queue_index: Queue ID
5478  * @maxrate: maximum bandwidth in Mbps
5479  */
5480 static int
5481 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5482 {
5483 	struct ice_netdev_priv *np = netdev_priv(netdev);
5484 	struct ice_vsi *vsi = np->vsi;
5485 	enum ice_status status;
5486 	u16 q_handle;
5487 	u8 tc;
5488 
5489 	/* Validate maxrate requested is within permitted range */
5490 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5491 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5492 			   maxrate, queue_index);
5493 		return -EINVAL;
5494 	}
5495 
5496 	q_handle = vsi->tx_rings[queue_index]->q_handle;
5497 	tc = ice_dcb_get_tc(vsi, queue_index);
5498 
5499 	/* Set BW back to default, when user set maxrate to 0 */
5500 	if (!maxrate)
5501 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5502 					       q_handle, ICE_MAX_BW);
5503 	else
5504 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5505 					  q_handle, ICE_MAX_BW, maxrate * 1000);
5506 	if (status) {
5507 		netdev_err(netdev, "Unable to set Tx max rate, error %s\n",
5508 			   ice_stat_str(status));
5509 		return -EIO;
5510 	}
5511 
5512 	return 0;
5513 }
5514 
5515 /**
5516  * ice_fdb_add - add an entry to the hardware database
5517  * @ndm: the input from the stack
5518  * @tb: pointer to array of nladdr (unused)
5519  * @dev: the net device pointer
5520  * @addr: the MAC address entry being added
5521  * @vid: VLAN ID
5522  * @flags: instructions from stack about fdb operation
5523  * @extack: netlink extended ack
5524  */
5525 static int
5526 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5527 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5528 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5529 {
5530 	int err;
5531 
5532 	if (vid) {
5533 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5534 		return -EINVAL;
5535 	}
5536 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5537 		netdev_err(dev, "FDB only supports static addresses\n");
5538 		return -EINVAL;
5539 	}
5540 
5541 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5542 		err = dev_uc_add_excl(dev, addr);
5543 	else if (is_multicast_ether_addr(addr))
5544 		err = dev_mc_add_excl(dev, addr);
5545 	else
5546 		err = -EINVAL;
5547 
5548 	/* Only return duplicate errors if NLM_F_EXCL is set */
5549 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5550 		err = 0;
5551 
5552 	return err;
5553 }
5554 
5555 /**
5556  * ice_fdb_del - delete an entry from the hardware database
5557  * @ndm: the input from the stack
5558  * @tb: pointer to array of nladdr (unused)
5559  * @dev: the net device pointer
5560  * @addr: the MAC address entry being added
5561  * @vid: VLAN ID
5562  */
5563 static int
5564 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5565 	    struct net_device *dev, const unsigned char *addr,
5566 	    __always_unused u16 vid)
5567 {
5568 	int err;
5569 
5570 	if (ndm->ndm_state & NUD_PERMANENT) {
5571 		netdev_err(dev, "FDB only supports static addresses\n");
5572 		return -EINVAL;
5573 	}
5574 
5575 	if (is_unicast_ether_addr(addr))
5576 		err = dev_uc_del(dev, addr);
5577 	else if (is_multicast_ether_addr(addr))
5578 		err = dev_mc_del(dev, addr);
5579 	else
5580 		err = -EINVAL;
5581 
5582 	return err;
5583 }
5584 
5585 /**
5586  * ice_set_features - set the netdev feature flags
5587  * @netdev: ptr to the netdev being adjusted
5588  * @features: the feature set that the stack is suggesting
5589  */
5590 static int
5591 ice_set_features(struct net_device *netdev, netdev_features_t features)
5592 {
5593 	struct ice_netdev_priv *np = netdev_priv(netdev);
5594 	struct ice_vsi *vsi = np->vsi;
5595 	struct ice_pf *pf = vsi->back;
5596 	int ret = 0;
5597 
5598 	/* Don't set any netdev advanced features with device in Safe Mode */
5599 	if (ice_is_safe_mode(vsi->back)) {
5600 		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5601 		return ret;
5602 	}
5603 
5604 	/* Do not change setting during reset */
5605 	if (ice_is_reset_in_progress(pf->state)) {
5606 		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5607 		return -EBUSY;
5608 	}
5609 
5610 	/* Multiple features can be changed in one call so keep features in
5611 	 * separate if/else statements to guarantee each feature is checked
5612 	 */
5613 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5614 		ice_vsi_manage_rss_lut(vsi, true);
5615 	else if (!(features & NETIF_F_RXHASH) &&
5616 		 netdev->features & NETIF_F_RXHASH)
5617 		ice_vsi_manage_rss_lut(vsi, false);
5618 
5619 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5620 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5621 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5622 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5623 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5624 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
5625 
5626 	if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5627 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5628 		ret = ice_vsi_manage_vlan_insertion(vsi);
5629 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5630 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5631 		ret = ice_vsi_manage_vlan_insertion(vsi);
5632 
5633 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5634 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5635 		ret = ice_cfg_vlan_pruning(vsi, true);
5636 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5637 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5638 		ret = ice_cfg_vlan_pruning(vsi, false);
5639 
5640 	if ((features & NETIF_F_NTUPLE) &&
5641 	    !(netdev->features & NETIF_F_NTUPLE)) {
5642 		ice_vsi_manage_fdir(vsi, true);
5643 		ice_init_arfs(vsi);
5644 	} else if (!(features & NETIF_F_NTUPLE) &&
5645 		 (netdev->features & NETIF_F_NTUPLE)) {
5646 		ice_vsi_manage_fdir(vsi, false);
5647 		ice_clear_arfs(vsi);
5648 	}
5649 
5650 	/* don't turn off hw_tc_offload when ADQ is already enabled */
5651 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
5652 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
5653 		return -EACCES;
5654 	}
5655 
5656 	if ((features & NETIF_F_HW_TC) &&
5657 	    !(netdev->features & NETIF_F_HW_TC))
5658 		set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
5659 	else
5660 		clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
5661 
5662 	return ret;
5663 }
5664 
5665 /**
5666  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5667  * @vsi: VSI to setup VLAN properties for
5668  */
5669 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5670 {
5671 	int ret = 0;
5672 
5673 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5674 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5675 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5676 		ret = ice_vsi_manage_vlan_insertion(vsi);
5677 
5678 	return ret;
5679 }
5680 
5681 /**
5682  * ice_vsi_cfg - Setup the VSI
5683  * @vsi: the VSI being configured
5684  *
5685  * Return 0 on success and negative value on error
5686  */
5687 int ice_vsi_cfg(struct ice_vsi *vsi)
5688 {
5689 	int err;
5690 
5691 	if (vsi->netdev) {
5692 		ice_set_rx_mode(vsi->netdev);
5693 
5694 		err = ice_vsi_vlan_setup(vsi);
5695 
5696 		if (err)
5697 			return err;
5698 	}
5699 	ice_vsi_cfg_dcb_rings(vsi);
5700 
5701 	err = ice_vsi_cfg_lan_txqs(vsi);
5702 	if (!err && ice_is_xdp_ena_vsi(vsi))
5703 		err = ice_vsi_cfg_xdp_txqs(vsi);
5704 	if (!err)
5705 		err = ice_vsi_cfg_rxqs(vsi);
5706 
5707 	return err;
5708 }
5709 
5710 /* THEORY OF MODERATION:
5711  * The ice driver hardware works differently than the hardware that DIMLIB was
5712  * originally made for. ice hardware doesn't have packet count limits that
5713  * can trigger an interrupt, but it *does* have interrupt rate limit support,
5714  * which is hard-coded to a limit of 250,000 ints/second.
5715  * If not using dynamic moderation, the INTRL value can be modified
5716  * by ethtool rx-usecs-high.
5717  */
5718 struct ice_dim {
5719 	/* the throttle rate for interrupts, basically worst case delay before
5720 	 * an initial interrupt fires, value is stored in microseconds.
5721 	 */
5722 	u16 itr;
5723 };
5724 
5725 /* Make a different profile for Rx that doesn't allow quite so aggressive
5726  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
5727  * second.
5728  */
5729 static const struct ice_dim rx_profile[] = {
5730 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
5731 	{8},    /* 125,000 ints/s */
5732 	{16},   /*  62,500 ints/s */
5733 	{62},   /*  16,129 ints/s */
5734 	{126}   /*   7,936 ints/s */
5735 };
5736 
5737 /* The transmit profile, which has the same sorts of values
5738  * as the previous struct
5739  */
5740 static const struct ice_dim tx_profile[] = {
5741 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
5742 	{8},    /* 125,000 ints/s */
5743 	{40},   /*  16,125 ints/s */
5744 	{128},  /*   7,812 ints/s */
5745 	{256}   /*   3,906 ints/s */
5746 };
5747 
5748 static void ice_tx_dim_work(struct work_struct *work)
5749 {
5750 	struct ice_ring_container *rc;
5751 	struct dim *dim;
5752 	u16 itr;
5753 
5754 	dim = container_of(work, struct dim, work);
5755 	rc = (struct ice_ring_container *)dim->priv;
5756 
5757 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
5758 
5759 	/* look up the values in our local table */
5760 	itr = tx_profile[dim->profile_ix].itr;
5761 
5762 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
5763 	ice_write_itr(rc, itr);
5764 
5765 	dim->state = DIM_START_MEASURE;
5766 }
5767 
5768 static void ice_rx_dim_work(struct work_struct *work)
5769 {
5770 	struct ice_ring_container *rc;
5771 	struct dim *dim;
5772 	u16 itr;
5773 
5774 	dim = container_of(work, struct dim, work);
5775 	rc = (struct ice_ring_container *)dim->priv;
5776 
5777 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
5778 
5779 	/* look up the values in our local table */
5780 	itr = rx_profile[dim->profile_ix].itr;
5781 
5782 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
5783 	ice_write_itr(rc, itr);
5784 
5785 	dim->state = DIM_START_MEASURE;
5786 }
5787 
5788 #define ICE_DIM_DEFAULT_PROFILE_IX 1
5789 
5790 /**
5791  * ice_init_moderation - set up interrupt moderation
5792  * @q_vector: the vector containing rings to be configured
5793  *
5794  * Set up interrupt moderation registers, with the intent to do the right thing
5795  * when called from reset or from probe, and whether or not dynamic moderation
5796  * is enabled or not. Take special care to write all the registers in both
5797  * dynamic moderation mode or not in order to make sure hardware is in a known
5798  * state.
5799  */
5800 static void ice_init_moderation(struct ice_q_vector *q_vector)
5801 {
5802 	struct ice_ring_container *rc;
5803 	bool tx_dynamic, rx_dynamic;
5804 
5805 	rc = &q_vector->tx;
5806 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
5807 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5808 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
5809 	rc->dim.priv = rc;
5810 	tx_dynamic = ITR_IS_DYNAMIC(rc);
5811 
5812 	/* set the initial TX ITR to match the above */
5813 	ice_write_itr(rc, tx_dynamic ?
5814 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
5815 
5816 	rc = &q_vector->rx;
5817 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
5818 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5819 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
5820 	rc->dim.priv = rc;
5821 	rx_dynamic = ITR_IS_DYNAMIC(rc);
5822 
5823 	/* set the initial RX ITR to match the above */
5824 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
5825 				       rc->itr_setting);
5826 
5827 	ice_set_q_vector_intrl(q_vector);
5828 }
5829 
5830 /**
5831  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5832  * @vsi: the VSI being configured
5833  */
5834 static void ice_napi_enable_all(struct ice_vsi *vsi)
5835 {
5836 	int q_idx;
5837 
5838 	if (!vsi->netdev)
5839 		return;
5840 
5841 	ice_for_each_q_vector(vsi, q_idx) {
5842 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5843 
5844 		ice_init_moderation(q_vector);
5845 
5846 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
5847 			napi_enable(&q_vector->napi);
5848 	}
5849 }
5850 
5851 /**
5852  * ice_up_complete - Finish the last steps of bringing up a connection
5853  * @vsi: The VSI being configured
5854  *
5855  * Return 0 on success and negative value on error
5856  */
5857 static int ice_up_complete(struct ice_vsi *vsi)
5858 {
5859 	struct ice_pf *pf = vsi->back;
5860 	int err;
5861 
5862 	ice_vsi_cfg_msix(vsi);
5863 
5864 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
5865 	 * Tx queue group list was configured and the context bits were
5866 	 * programmed using ice_vsi_cfg_txqs
5867 	 */
5868 	err = ice_vsi_start_all_rx_rings(vsi);
5869 	if (err)
5870 		return err;
5871 
5872 	clear_bit(ICE_VSI_DOWN, vsi->state);
5873 	ice_napi_enable_all(vsi);
5874 	ice_vsi_ena_irq(vsi);
5875 
5876 	if (vsi->port_info &&
5877 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5878 	    vsi->netdev) {
5879 		ice_print_link_msg(vsi, true);
5880 		netif_tx_start_all_queues(vsi->netdev);
5881 		netif_carrier_on(vsi->netdev);
5882 	}
5883 
5884 	/* clear this now, and the first stats read will be used as baseline */
5885 	vsi->stat_offsets_loaded = false;
5886 
5887 	ice_service_task_schedule(pf);
5888 
5889 	return 0;
5890 }
5891 
5892 /**
5893  * ice_up - Bring the connection back up after being down
5894  * @vsi: VSI being configured
5895  */
5896 int ice_up(struct ice_vsi *vsi)
5897 {
5898 	int err;
5899 
5900 	err = ice_vsi_cfg(vsi);
5901 	if (!err)
5902 		err = ice_up_complete(vsi);
5903 
5904 	return err;
5905 }
5906 
5907 /**
5908  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5909  * @syncp: pointer to u64_stats_sync
5910  * @stats: stats that pkts and bytes count will be taken from
5911  * @pkts: packets stats counter
5912  * @bytes: bytes stats counter
5913  *
5914  * This function fetches stats from the ring considering the atomic operations
5915  * that needs to be performed to read u64 values in 32 bit machine.
5916  */
5917 static void
5918 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp, struct ice_q_stats stats,
5919 			     u64 *pkts, u64 *bytes)
5920 {
5921 	unsigned int start;
5922 
5923 	do {
5924 		start = u64_stats_fetch_begin_irq(syncp);
5925 		*pkts = stats.pkts;
5926 		*bytes = stats.bytes;
5927 	} while (u64_stats_fetch_retry_irq(syncp, start));
5928 }
5929 
5930 /**
5931  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5932  * @vsi: the VSI to be updated
5933  * @vsi_stats: the stats struct to be updated
5934  * @rings: rings to work on
5935  * @count: number of rings
5936  */
5937 static void
5938 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
5939 			     struct rtnl_link_stats64 *vsi_stats,
5940 			     struct ice_tx_ring **rings, u16 count)
5941 {
5942 	u16 i;
5943 
5944 	for (i = 0; i < count; i++) {
5945 		struct ice_tx_ring *ring;
5946 		u64 pkts = 0, bytes = 0;
5947 
5948 		ring = READ_ONCE(rings[i]);
5949 		if (ring)
5950 			ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
5951 		vsi_stats->tx_packets += pkts;
5952 		vsi_stats->tx_bytes += bytes;
5953 		vsi->tx_restart += ring->tx_stats.restart_q;
5954 		vsi->tx_busy += ring->tx_stats.tx_busy;
5955 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
5956 	}
5957 }
5958 
5959 /**
5960  * ice_update_vsi_ring_stats - Update VSI stats counters
5961  * @vsi: the VSI to be updated
5962  */
5963 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5964 {
5965 	struct rtnl_link_stats64 *vsi_stats;
5966 	u64 pkts, bytes;
5967 	int i;
5968 
5969 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
5970 	if (!vsi_stats)
5971 		return;
5972 
5973 	/* reset non-netdev (extended) stats */
5974 	vsi->tx_restart = 0;
5975 	vsi->tx_busy = 0;
5976 	vsi->tx_linearize = 0;
5977 	vsi->rx_buf_failed = 0;
5978 	vsi->rx_page_failed = 0;
5979 
5980 	rcu_read_lock();
5981 
5982 	/* update Tx rings counters */
5983 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
5984 				     vsi->num_txq);
5985 
5986 	/* update Rx rings counters */
5987 	ice_for_each_rxq(vsi, i) {
5988 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
5989 
5990 		ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
5991 		vsi_stats->rx_packets += pkts;
5992 		vsi_stats->rx_bytes += bytes;
5993 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5994 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5995 	}
5996 
5997 	/* update XDP Tx rings counters */
5998 	if (ice_is_xdp_ena_vsi(vsi))
5999 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6000 					     vsi->num_xdp_txq);
6001 
6002 	rcu_read_unlock();
6003 
6004 	vsi->net_stats.tx_packets = vsi_stats->tx_packets;
6005 	vsi->net_stats.tx_bytes = vsi_stats->tx_bytes;
6006 	vsi->net_stats.rx_packets = vsi_stats->rx_packets;
6007 	vsi->net_stats.rx_bytes = vsi_stats->rx_bytes;
6008 
6009 	kfree(vsi_stats);
6010 }
6011 
6012 /**
6013  * ice_update_vsi_stats - Update VSI stats counters
6014  * @vsi: the VSI to be updated
6015  */
6016 void ice_update_vsi_stats(struct ice_vsi *vsi)
6017 {
6018 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6019 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6020 	struct ice_pf *pf = vsi->back;
6021 
6022 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6023 	    test_bit(ICE_CFG_BUSY, pf->state))
6024 		return;
6025 
6026 	/* get stats as recorded by Tx/Rx rings */
6027 	ice_update_vsi_ring_stats(vsi);
6028 
6029 	/* get VSI stats as recorded by the hardware */
6030 	ice_update_eth_stats(vsi);
6031 
6032 	cur_ns->tx_errors = cur_es->tx_errors;
6033 	cur_ns->rx_dropped = cur_es->rx_discards;
6034 	cur_ns->tx_dropped = cur_es->tx_discards;
6035 	cur_ns->multicast = cur_es->rx_multicast;
6036 
6037 	/* update some more netdev stats if this is main VSI */
6038 	if (vsi->type == ICE_VSI_PF) {
6039 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6040 		cur_ns->rx_errors = pf->stats.crc_errors +
6041 				    pf->stats.illegal_bytes +
6042 				    pf->stats.rx_len_errors +
6043 				    pf->stats.rx_undersize +
6044 				    pf->hw_csum_rx_error +
6045 				    pf->stats.rx_jabber +
6046 				    pf->stats.rx_fragments +
6047 				    pf->stats.rx_oversize;
6048 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6049 		/* record drops from the port level */
6050 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6051 	}
6052 }
6053 
6054 /**
6055  * ice_update_pf_stats - Update PF port stats counters
6056  * @pf: PF whose stats needs to be updated
6057  */
6058 void ice_update_pf_stats(struct ice_pf *pf)
6059 {
6060 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6061 	struct ice_hw *hw = &pf->hw;
6062 	u16 fd_ctr_base;
6063 	u8 port;
6064 
6065 	port = hw->port_info->lport;
6066 	prev_ps = &pf->stats_prev;
6067 	cur_ps = &pf->stats;
6068 
6069 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6070 			  &prev_ps->eth.rx_bytes,
6071 			  &cur_ps->eth.rx_bytes);
6072 
6073 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6074 			  &prev_ps->eth.rx_unicast,
6075 			  &cur_ps->eth.rx_unicast);
6076 
6077 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6078 			  &prev_ps->eth.rx_multicast,
6079 			  &cur_ps->eth.rx_multicast);
6080 
6081 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6082 			  &prev_ps->eth.rx_broadcast,
6083 			  &cur_ps->eth.rx_broadcast);
6084 
6085 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6086 			  &prev_ps->eth.rx_discards,
6087 			  &cur_ps->eth.rx_discards);
6088 
6089 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6090 			  &prev_ps->eth.tx_bytes,
6091 			  &cur_ps->eth.tx_bytes);
6092 
6093 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6094 			  &prev_ps->eth.tx_unicast,
6095 			  &cur_ps->eth.tx_unicast);
6096 
6097 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6098 			  &prev_ps->eth.tx_multicast,
6099 			  &cur_ps->eth.tx_multicast);
6100 
6101 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6102 			  &prev_ps->eth.tx_broadcast,
6103 			  &cur_ps->eth.tx_broadcast);
6104 
6105 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6106 			  &prev_ps->tx_dropped_link_down,
6107 			  &cur_ps->tx_dropped_link_down);
6108 
6109 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6110 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6111 
6112 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6113 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6114 
6115 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6116 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6117 
6118 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6119 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6120 
6121 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6122 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6123 
6124 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6125 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6126 
6127 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6128 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6129 
6130 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6131 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6132 
6133 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6134 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6135 
6136 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6137 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6138 
6139 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6140 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6141 
6142 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6143 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6144 
6145 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6146 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6147 
6148 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6149 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6150 
6151 	fd_ctr_base = hw->fd_ctr_base;
6152 
6153 	ice_stat_update40(hw,
6154 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6155 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6156 			  &cur_ps->fd_sb_match);
6157 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6158 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6159 
6160 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6161 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6162 
6163 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6164 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6165 
6166 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6167 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6168 
6169 	ice_update_dcb_stats(pf);
6170 
6171 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6172 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6173 
6174 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6175 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6176 
6177 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6178 			  &prev_ps->mac_local_faults,
6179 			  &cur_ps->mac_local_faults);
6180 
6181 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6182 			  &prev_ps->mac_remote_faults,
6183 			  &cur_ps->mac_remote_faults);
6184 
6185 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6186 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6187 
6188 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6189 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6190 
6191 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6192 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6193 
6194 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6195 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6196 
6197 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6198 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6199 
6200 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6201 
6202 	pf->stat_prev_loaded = true;
6203 }
6204 
6205 /**
6206  * ice_get_stats64 - get statistics for network device structure
6207  * @netdev: network interface device structure
6208  * @stats: main device statistics structure
6209  */
6210 static
6211 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6212 {
6213 	struct ice_netdev_priv *np = netdev_priv(netdev);
6214 	struct rtnl_link_stats64 *vsi_stats;
6215 	struct ice_vsi *vsi = np->vsi;
6216 
6217 	vsi_stats = &vsi->net_stats;
6218 
6219 	if (!vsi->num_txq || !vsi->num_rxq)
6220 		return;
6221 
6222 	/* netdev packet/byte stats come from ring counter. These are obtained
6223 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6224 	 * But, only call the update routine and read the registers if VSI is
6225 	 * not down.
6226 	 */
6227 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
6228 		ice_update_vsi_ring_stats(vsi);
6229 	stats->tx_packets = vsi_stats->tx_packets;
6230 	stats->tx_bytes = vsi_stats->tx_bytes;
6231 	stats->rx_packets = vsi_stats->rx_packets;
6232 	stats->rx_bytes = vsi_stats->rx_bytes;
6233 
6234 	/* The rest of the stats can be read from the hardware but instead we
6235 	 * just return values that the watchdog task has already obtained from
6236 	 * the hardware.
6237 	 */
6238 	stats->multicast = vsi_stats->multicast;
6239 	stats->tx_errors = vsi_stats->tx_errors;
6240 	stats->tx_dropped = vsi_stats->tx_dropped;
6241 	stats->rx_errors = vsi_stats->rx_errors;
6242 	stats->rx_dropped = vsi_stats->rx_dropped;
6243 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6244 	stats->rx_length_errors = vsi_stats->rx_length_errors;
6245 }
6246 
6247 /**
6248  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6249  * @vsi: VSI having NAPI disabled
6250  */
6251 static void ice_napi_disable_all(struct ice_vsi *vsi)
6252 {
6253 	int q_idx;
6254 
6255 	if (!vsi->netdev)
6256 		return;
6257 
6258 	ice_for_each_q_vector(vsi, q_idx) {
6259 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6260 
6261 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6262 			napi_disable(&q_vector->napi);
6263 
6264 		cancel_work_sync(&q_vector->tx.dim.work);
6265 		cancel_work_sync(&q_vector->rx.dim.work);
6266 	}
6267 }
6268 
6269 /**
6270  * ice_down - Shutdown the connection
6271  * @vsi: The VSI being stopped
6272  */
6273 int ice_down(struct ice_vsi *vsi)
6274 {
6275 	int i, tx_err, rx_err, link_err = 0;
6276 
6277 	/* Caller of this function is expected to set the
6278 	 * vsi->state ICE_DOWN bit
6279 	 */
6280 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6281 		netif_carrier_off(vsi->netdev);
6282 		netif_tx_disable(vsi->netdev);
6283 	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6284 		ice_eswitch_stop_all_tx_queues(vsi->back);
6285 	}
6286 
6287 	ice_vsi_dis_irq(vsi);
6288 
6289 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6290 	if (tx_err)
6291 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6292 			   vsi->vsi_num, tx_err);
6293 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6294 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6295 		if (tx_err)
6296 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6297 				   vsi->vsi_num, tx_err);
6298 	}
6299 
6300 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
6301 	if (rx_err)
6302 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6303 			   vsi->vsi_num, rx_err);
6304 
6305 	ice_napi_disable_all(vsi);
6306 
6307 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
6308 		link_err = ice_force_phys_link_state(vsi, false);
6309 		if (link_err)
6310 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
6311 				   vsi->vsi_num, link_err);
6312 	}
6313 
6314 	ice_for_each_txq(vsi, i)
6315 		ice_clean_tx_ring(vsi->tx_rings[i]);
6316 
6317 	ice_for_each_rxq(vsi, i)
6318 		ice_clean_rx_ring(vsi->rx_rings[i]);
6319 
6320 	if (tx_err || rx_err || link_err) {
6321 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6322 			   vsi->vsi_num, vsi->vsw->sw_id);
6323 		return -EIO;
6324 	}
6325 
6326 	return 0;
6327 }
6328 
6329 /**
6330  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
6331  * @vsi: VSI having resources allocated
6332  *
6333  * Return 0 on success, negative on failure
6334  */
6335 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
6336 {
6337 	int i, err = 0;
6338 
6339 	if (!vsi->num_txq) {
6340 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
6341 			vsi->vsi_num);
6342 		return -EINVAL;
6343 	}
6344 
6345 	ice_for_each_txq(vsi, i) {
6346 		struct ice_tx_ring *ring = vsi->tx_rings[i];
6347 
6348 		if (!ring)
6349 			return -EINVAL;
6350 
6351 		if (vsi->netdev)
6352 			ring->netdev = vsi->netdev;
6353 		err = ice_setup_tx_ring(ring);
6354 		if (err)
6355 			break;
6356 	}
6357 
6358 	return err;
6359 }
6360 
6361 /**
6362  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
6363  * @vsi: VSI having resources allocated
6364  *
6365  * Return 0 on success, negative on failure
6366  */
6367 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
6368 {
6369 	int i, err = 0;
6370 
6371 	if (!vsi->num_rxq) {
6372 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
6373 			vsi->vsi_num);
6374 		return -EINVAL;
6375 	}
6376 
6377 	ice_for_each_rxq(vsi, i) {
6378 		struct ice_rx_ring *ring = vsi->rx_rings[i];
6379 
6380 		if (!ring)
6381 			return -EINVAL;
6382 
6383 		if (vsi->netdev)
6384 			ring->netdev = vsi->netdev;
6385 		err = ice_setup_rx_ring(ring);
6386 		if (err)
6387 			break;
6388 	}
6389 
6390 	return err;
6391 }
6392 
6393 /**
6394  * ice_vsi_open_ctrl - open control VSI for use
6395  * @vsi: the VSI to open
6396  *
6397  * Initialization of the Control VSI
6398  *
6399  * Returns 0 on success, negative value on error
6400  */
6401 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
6402 {
6403 	char int_name[ICE_INT_NAME_STR_LEN];
6404 	struct ice_pf *pf = vsi->back;
6405 	struct device *dev;
6406 	int err;
6407 
6408 	dev = ice_pf_to_dev(pf);
6409 	/* allocate descriptors */
6410 	err = ice_vsi_setup_tx_rings(vsi);
6411 	if (err)
6412 		goto err_setup_tx;
6413 
6414 	err = ice_vsi_setup_rx_rings(vsi);
6415 	if (err)
6416 		goto err_setup_rx;
6417 
6418 	err = ice_vsi_cfg(vsi);
6419 	if (err)
6420 		goto err_setup_rx;
6421 
6422 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
6423 		 dev_driver_string(dev), dev_name(dev));
6424 	err = ice_vsi_req_irq_msix(vsi, int_name);
6425 	if (err)
6426 		goto err_setup_rx;
6427 
6428 	ice_vsi_cfg_msix(vsi);
6429 
6430 	err = ice_vsi_start_all_rx_rings(vsi);
6431 	if (err)
6432 		goto err_up_complete;
6433 
6434 	clear_bit(ICE_VSI_DOWN, vsi->state);
6435 	ice_vsi_ena_irq(vsi);
6436 
6437 	return 0;
6438 
6439 err_up_complete:
6440 	ice_down(vsi);
6441 err_setup_rx:
6442 	ice_vsi_free_rx_rings(vsi);
6443 err_setup_tx:
6444 	ice_vsi_free_tx_rings(vsi);
6445 
6446 	return err;
6447 }
6448 
6449 /**
6450  * ice_vsi_open - Called when a network interface is made active
6451  * @vsi: the VSI to open
6452  *
6453  * Initialization of the VSI
6454  *
6455  * Returns 0 on success, negative value on error
6456  */
6457 int ice_vsi_open(struct ice_vsi *vsi)
6458 {
6459 	char int_name[ICE_INT_NAME_STR_LEN];
6460 	struct ice_pf *pf = vsi->back;
6461 	int err;
6462 
6463 	/* allocate descriptors */
6464 	err = ice_vsi_setup_tx_rings(vsi);
6465 	if (err)
6466 		goto err_setup_tx;
6467 
6468 	err = ice_vsi_setup_rx_rings(vsi);
6469 	if (err)
6470 		goto err_setup_rx;
6471 
6472 	err = ice_vsi_cfg(vsi);
6473 	if (err)
6474 		goto err_setup_rx;
6475 
6476 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
6477 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
6478 	err = ice_vsi_req_irq_msix(vsi, int_name);
6479 	if (err)
6480 		goto err_setup_rx;
6481 
6482 	if (vsi->type == ICE_VSI_PF) {
6483 		/* Notify the stack of the actual queue counts. */
6484 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
6485 		if (err)
6486 			goto err_set_qs;
6487 
6488 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
6489 		if (err)
6490 			goto err_set_qs;
6491 	}
6492 
6493 	err = ice_up_complete(vsi);
6494 	if (err)
6495 		goto err_up_complete;
6496 
6497 	return 0;
6498 
6499 err_up_complete:
6500 	ice_down(vsi);
6501 err_set_qs:
6502 	ice_vsi_free_irq(vsi);
6503 err_setup_rx:
6504 	ice_vsi_free_rx_rings(vsi);
6505 err_setup_tx:
6506 	ice_vsi_free_tx_rings(vsi);
6507 
6508 	return err;
6509 }
6510 
6511 /**
6512  * ice_vsi_release_all - Delete all VSIs
6513  * @pf: PF from which all VSIs are being removed
6514  */
6515 static void ice_vsi_release_all(struct ice_pf *pf)
6516 {
6517 	int err, i;
6518 
6519 	if (!pf->vsi)
6520 		return;
6521 
6522 	ice_for_each_vsi(pf, i) {
6523 		if (!pf->vsi[i])
6524 			continue;
6525 
6526 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
6527 			continue;
6528 
6529 		err = ice_vsi_release(pf->vsi[i]);
6530 		if (err)
6531 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
6532 				i, err, pf->vsi[i]->vsi_num);
6533 	}
6534 }
6535 
6536 /**
6537  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
6538  * @pf: pointer to the PF instance
6539  * @type: VSI type to rebuild
6540  *
6541  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
6542  */
6543 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
6544 {
6545 	struct device *dev = ice_pf_to_dev(pf);
6546 	enum ice_status status;
6547 	int i, err;
6548 
6549 	ice_for_each_vsi(pf, i) {
6550 		struct ice_vsi *vsi = pf->vsi[i];
6551 
6552 		if (!vsi || vsi->type != type)
6553 			continue;
6554 
6555 		/* rebuild the VSI */
6556 		err = ice_vsi_rebuild(vsi, true);
6557 		if (err) {
6558 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
6559 				err, vsi->idx, ice_vsi_type_str(type));
6560 			return err;
6561 		}
6562 
6563 		/* replay filters for the VSI */
6564 		status = ice_replay_vsi(&pf->hw, vsi->idx);
6565 		if (status) {
6566 			dev_err(dev, "replay VSI failed, status %s, VSI index %d, type %s\n",
6567 				ice_stat_str(status), vsi->idx,
6568 				ice_vsi_type_str(type));
6569 			return -EIO;
6570 		}
6571 
6572 		/* Re-map HW VSI number, using VSI handle that has been
6573 		 * previously validated in ice_replay_vsi() call above
6574 		 */
6575 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
6576 
6577 		/* enable the VSI */
6578 		err = ice_ena_vsi(vsi, false);
6579 		if (err) {
6580 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
6581 				err, vsi->idx, ice_vsi_type_str(type));
6582 			return err;
6583 		}
6584 
6585 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
6586 			 ice_vsi_type_str(type));
6587 	}
6588 
6589 	return 0;
6590 }
6591 
6592 /**
6593  * ice_update_pf_netdev_link - Update PF netdev link status
6594  * @pf: pointer to the PF instance
6595  */
6596 static void ice_update_pf_netdev_link(struct ice_pf *pf)
6597 {
6598 	bool link_up;
6599 	int i;
6600 
6601 	ice_for_each_vsi(pf, i) {
6602 		struct ice_vsi *vsi = pf->vsi[i];
6603 
6604 		if (!vsi || vsi->type != ICE_VSI_PF)
6605 			return;
6606 
6607 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
6608 		if (link_up) {
6609 			netif_carrier_on(pf->vsi[i]->netdev);
6610 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
6611 		} else {
6612 			netif_carrier_off(pf->vsi[i]->netdev);
6613 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
6614 		}
6615 	}
6616 }
6617 
6618 /**
6619  * ice_rebuild - rebuild after reset
6620  * @pf: PF to rebuild
6621  * @reset_type: type of reset
6622  *
6623  * Do not rebuild VF VSI in this flow because that is already handled via
6624  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
6625  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
6626  * to reset/rebuild all the VF VSI twice.
6627  */
6628 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
6629 {
6630 	struct device *dev = ice_pf_to_dev(pf);
6631 	struct ice_hw *hw = &pf->hw;
6632 	enum ice_status ret;
6633 	int err;
6634 
6635 	if (test_bit(ICE_DOWN, pf->state))
6636 		goto clear_recovery;
6637 
6638 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
6639 
6640 	ret = ice_init_all_ctrlq(hw);
6641 	if (ret) {
6642 		dev_err(dev, "control queues init failed %s\n",
6643 			ice_stat_str(ret));
6644 		goto err_init_ctrlq;
6645 	}
6646 
6647 	/* if DDP was previously loaded successfully */
6648 	if (!ice_is_safe_mode(pf)) {
6649 		/* reload the SW DB of filter tables */
6650 		if (reset_type == ICE_RESET_PFR)
6651 			ice_fill_blk_tbls(hw);
6652 		else
6653 			/* Reload DDP Package after CORER/GLOBR reset */
6654 			ice_load_pkg(NULL, pf);
6655 	}
6656 
6657 	ret = ice_clear_pf_cfg(hw);
6658 	if (ret) {
6659 		dev_err(dev, "clear PF configuration failed %s\n",
6660 			ice_stat_str(ret));
6661 		goto err_init_ctrlq;
6662 	}
6663 
6664 	if (pf->first_sw->dflt_vsi_ena)
6665 		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
6666 	/* clear the default VSI configuration if it exists */
6667 	pf->first_sw->dflt_vsi = NULL;
6668 	pf->first_sw->dflt_vsi_ena = false;
6669 
6670 	ice_clear_pxe_mode(hw);
6671 
6672 	ret = ice_init_nvm(hw);
6673 	if (ret) {
6674 		dev_err(dev, "ice_init_nvm failed %s\n", ice_stat_str(ret));
6675 		goto err_init_ctrlq;
6676 	}
6677 
6678 	ret = ice_get_caps(hw);
6679 	if (ret) {
6680 		dev_err(dev, "ice_get_caps failed %s\n", ice_stat_str(ret));
6681 		goto err_init_ctrlq;
6682 	}
6683 
6684 	ret = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
6685 	if (ret) {
6686 		dev_err(dev, "set_mac_cfg failed %s\n", ice_stat_str(ret));
6687 		goto err_init_ctrlq;
6688 	}
6689 
6690 	err = ice_sched_init_port(hw->port_info);
6691 	if (err)
6692 		goto err_sched_init_port;
6693 
6694 	/* start misc vector */
6695 	err = ice_req_irq_msix_misc(pf);
6696 	if (err) {
6697 		dev_err(dev, "misc vector setup failed: %d\n", err);
6698 		goto err_sched_init_port;
6699 	}
6700 
6701 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6702 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
6703 		if (!rd32(hw, PFQF_FD_SIZE)) {
6704 			u16 unused, guar, b_effort;
6705 
6706 			guar = hw->func_caps.fd_fltr_guar;
6707 			b_effort = hw->func_caps.fd_fltr_best_effort;
6708 
6709 			/* force guaranteed filter pool for PF */
6710 			ice_alloc_fd_guar_item(hw, &unused, guar);
6711 			/* force shared filter pool for PF */
6712 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6713 		}
6714 	}
6715 
6716 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6717 		ice_dcb_rebuild(pf);
6718 
6719 	/* If the PF previously had enabled PTP, PTP init needs to happen before
6720 	 * the VSI rebuild. If not, this causes the PTP link status events to
6721 	 * fail.
6722 	 */
6723 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
6724 		ice_ptp_init(pf);
6725 
6726 	/* rebuild PF VSI */
6727 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6728 	if (err) {
6729 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6730 		goto err_vsi_rebuild;
6731 	}
6732 
6733 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
6734 	if (err) {
6735 		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
6736 		goto err_vsi_rebuild;
6737 	}
6738 
6739 	if (reset_type == ICE_RESET_PFR) {
6740 		err = ice_rebuild_channels(pf);
6741 		if (err) {
6742 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
6743 				err);
6744 			goto err_vsi_rebuild;
6745 		}
6746 	}
6747 
6748 	/* If Flow Director is active */
6749 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6750 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6751 		if (err) {
6752 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
6753 			goto err_vsi_rebuild;
6754 		}
6755 
6756 		/* replay HW Flow Director recipes */
6757 		if (hw->fdir_prof)
6758 			ice_fdir_replay_flows(hw);
6759 
6760 		/* replay Flow Director filters */
6761 		ice_fdir_replay_fltrs(pf);
6762 
6763 		ice_rebuild_arfs(pf);
6764 	}
6765 
6766 	ice_update_pf_netdev_link(pf);
6767 
6768 	/* tell the firmware we are up */
6769 	ret = ice_send_version(pf);
6770 	if (ret) {
6771 		dev_err(dev, "Rebuild failed due to error sending driver version: %s\n",
6772 			ice_stat_str(ret));
6773 		goto err_vsi_rebuild;
6774 	}
6775 
6776 	ice_replay_post(hw);
6777 
6778 	/* if we get here, reset flow is successful */
6779 	clear_bit(ICE_RESET_FAILED, pf->state);
6780 
6781 	ice_plug_aux_dev(pf);
6782 	return;
6783 
6784 err_vsi_rebuild:
6785 err_sched_init_port:
6786 	ice_sched_cleanup_all(hw);
6787 err_init_ctrlq:
6788 	ice_shutdown_all_ctrlq(hw);
6789 	set_bit(ICE_RESET_FAILED, pf->state);
6790 clear_recovery:
6791 	/* set this bit in PF state to control service task scheduling */
6792 	set_bit(ICE_NEEDS_RESTART, pf->state);
6793 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
6794 }
6795 
6796 /**
6797  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6798  * @vsi: Pointer to VSI structure
6799  */
6800 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6801 {
6802 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6803 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6804 	else
6805 		return ICE_RXBUF_3072;
6806 }
6807 
6808 /**
6809  * ice_change_mtu - NDO callback to change the MTU
6810  * @netdev: network interface device structure
6811  * @new_mtu: new value for maximum frame size
6812  *
6813  * Returns 0 on success, negative on failure
6814  */
6815 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6816 {
6817 	struct ice_netdev_priv *np = netdev_priv(netdev);
6818 	struct ice_vsi *vsi = np->vsi;
6819 	struct ice_pf *pf = vsi->back;
6820 	struct iidc_event *event;
6821 	u8 count = 0;
6822 	int err = 0;
6823 
6824 	if (new_mtu == (int)netdev->mtu) {
6825 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6826 		return 0;
6827 	}
6828 
6829 	if (ice_is_xdp_ena_vsi(vsi)) {
6830 		int frame_size = ice_max_xdp_frame_size(vsi);
6831 
6832 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6833 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
6834 				   frame_size - ICE_ETH_PKT_HDR_PAD);
6835 			return -EINVAL;
6836 		}
6837 	}
6838 
6839 	/* if a reset is in progress, wait for some time for it to complete */
6840 	do {
6841 		if (ice_is_reset_in_progress(pf->state)) {
6842 			count++;
6843 			usleep_range(1000, 2000);
6844 		} else {
6845 			break;
6846 		}
6847 
6848 	} while (count < 100);
6849 
6850 	if (count == 100) {
6851 		netdev_err(netdev, "can't change MTU. Device is busy\n");
6852 		return -EBUSY;
6853 	}
6854 
6855 	event = kzalloc(sizeof(*event), GFP_KERNEL);
6856 	if (!event)
6857 		return -ENOMEM;
6858 
6859 	set_bit(IIDC_EVENT_BEFORE_MTU_CHANGE, event->type);
6860 	ice_send_event_to_aux(pf, event);
6861 	clear_bit(IIDC_EVENT_BEFORE_MTU_CHANGE, event->type);
6862 
6863 	netdev->mtu = (unsigned int)new_mtu;
6864 
6865 	/* if VSI is up, bring it down and then back up */
6866 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6867 		err = ice_down(vsi);
6868 		if (err) {
6869 			netdev_err(netdev, "change MTU if_down err %d\n", err);
6870 			goto event_after;
6871 		}
6872 
6873 		err = ice_up(vsi);
6874 		if (err) {
6875 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6876 			goto event_after;
6877 		}
6878 	}
6879 
6880 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6881 event_after:
6882 	set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
6883 	ice_send_event_to_aux(pf, event);
6884 	kfree(event);
6885 
6886 	return err;
6887 }
6888 
6889 /**
6890  * ice_eth_ioctl - Access the hwtstamp interface
6891  * @netdev: network interface device structure
6892  * @ifr: interface request data
6893  * @cmd: ioctl command
6894  */
6895 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6896 {
6897 	struct ice_netdev_priv *np = netdev_priv(netdev);
6898 	struct ice_pf *pf = np->vsi->back;
6899 
6900 	switch (cmd) {
6901 	case SIOCGHWTSTAMP:
6902 		return ice_ptp_get_ts_config(pf, ifr);
6903 	case SIOCSHWTSTAMP:
6904 		return ice_ptp_set_ts_config(pf, ifr);
6905 	default:
6906 		return -EOPNOTSUPP;
6907 	}
6908 }
6909 
6910 /**
6911  * ice_aq_str - convert AQ err code to a string
6912  * @aq_err: the AQ error code to convert
6913  */
6914 const char *ice_aq_str(enum ice_aq_err aq_err)
6915 {
6916 	switch (aq_err) {
6917 	case ICE_AQ_RC_OK:
6918 		return "OK";
6919 	case ICE_AQ_RC_EPERM:
6920 		return "ICE_AQ_RC_EPERM";
6921 	case ICE_AQ_RC_ENOENT:
6922 		return "ICE_AQ_RC_ENOENT";
6923 	case ICE_AQ_RC_ENOMEM:
6924 		return "ICE_AQ_RC_ENOMEM";
6925 	case ICE_AQ_RC_EBUSY:
6926 		return "ICE_AQ_RC_EBUSY";
6927 	case ICE_AQ_RC_EEXIST:
6928 		return "ICE_AQ_RC_EEXIST";
6929 	case ICE_AQ_RC_EINVAL:
6930 		return "ICE_AQ_RC_EINVAL";
6931 	case ICE_AQ_RC_ENOSPC:
6932 		return "ICE_AQ_RC_ENOSPC";
6933 	case ICE_AQ_RC_ENOSYS:
6934 		return "ICE_AQ_RC_ENOSYS";
6935 	case ICE_AQ_RC_EMODE:
6936 		return "ICE_AQ_RC_EMODE";
6937 	case ICE_AQ_RC_ENOSEC:
6938 		return "ICE_AQ_RC_ENOSEC";
6939 	case ICE_AQ_RC_EBADSIG:
6940 		return "ICE_AQ_RC_EBADSIG";
6941 	case ICE_AQ_RC_ESVN:
6942 		return "ICE_AQ_RC_ESVN";
6943 	case ICE_AQ_RC_EBADMAN:
6944 		return "ICE_AQ_RC_EBADMAN";
6945 	case ICE_AQ_RC_EBADBUF:
6946 		return "ICE_AQ_RC_EBADBUF";
6947 	}
6948 
6949 	return "ICE_AQ_RC_UNKNOWN";
6950 }
6951 
6952 /**
6953  * ice_stat_str - convert status err code to a string
6954  * @stat_err: the status error code to convert
6955  */
6956 const char *ice_stat_str(enum ice_status stat_err)
6957 {
6958 	switch (stat_err) {
6959 	case ICE_SUCCESS:
6960 		return "OK";
6961 	case ICE_ERR_PARAM:
6962 		return "ICE_ERR_PARAM";
6963 	case ICE_ERR_NOT_IMPL:
6964 		return "ICE_ERR_NOT_IMPL";
6965 	case ICE_ERR_NOT_READY:
6966 		return "ICE_ERR_NOT_READY";
6967 	case ICE_ERR_NOT_SUPPORTED:
6968 		return "ICE_ERR_NOT_SUPPORTED";
6969 	case ICE_ERR_BAD_PTR:
6970 		return "ICE_ERR_BAD_PTR";
6971 	case ICE_ERR_INVAL_SIZE:
6972 		return "ICE_ERR_INVAL_SIZE";
6973 	case ICE_ERR_DEVICE_NOT_SUPPORTED:
6974 		return "ICE_ERR_DEVICE_NOT_SUPPORTED";
6975 	case ICE_ERR_RESET_FAILED:
6976 		return "ICE_ERR_RESET_FAILED";
6977 	case ICE_ERR_FW_API_VER:
6978 		return "ICE_ERR_FW_API_VER";
6979 	case ICE_ERR_NO_MEMORY:
6980 		return "ICE_ERR_NO_MEMORY";
6981 	case ICE_ERR_CFG:
6982 		return "ICE_ERR_CFG";
6983 	case ICE_ERR_OUT_OF_RANGE:
6984 		return "ICE_ERR_OUT_OF_RANGE";
6985 	case ICE_ERR_ALREADY_EXISTS:
6986 		return "ICE_ERR_ALREADY_EXISTS";
6987 	case ICE_ERR_NVM:
6988 		return "ICE_ERR_NVM";
6989 	case ICE_ERR_NVM_CHECKSUM:
6990 		return "ICE_ERR_NVM_CHECKSUM";
6991 	case ICE_ERR_BUF_TOO_SHORT:
6992 		return "ICE_ERR_BUF_TOO_SHORT";
6993 	case ICE_ERR_NVM_BLANK_MODE:
6994 		return "ICE_ERR_NVM_BLANK_MODE";
6995 	case ICE_ERR_IN_USE:
6996 		return "ICE_ERR_IN_USE";
6997 	case ICE_ERR_MAX_LIMIT:
6998 		return "ICE_ERR_MAX_LIMIT";
6999 	case ICE_ERR_RESET_ONGOING:
7000 		return "ICE_ERR_RESET_ONGOING";
7001 	case ICE_ERR_HW_TABLE:
7002 		return "ICE_ERR_HW_TABLE";
7003 	case ICE_ERR_DOES_NOT_EXIST:
7004 		return "ICE_ERR_DOES_NOT_EXIST";
7005 	case ICE_ERR_FW_DDP_MISMATCH:
7006 		return "ICE_ERR_FW_DDP_MISMATCH";
7007 	case ICE_ERR_AQ_ERROR:
7008 		return "ICE_ERR_AQ_ERROR";
7009 	case ICE_ERR_AQ_TIMEOUT:
7010 		return "ICE_ERR_AQ_TIMEOUT";
7011 	case ICE_ERR_AQ_FULL:
7012 		return "ICE_ERR_AQ_FULL";
7013 	case ICE_ERR_AQ_NO_WORK:
7014 		return "ICE_ERR_AQ_NO_WORK";
7015 	case ICE_ERR_AQ_EMPTY:
7016 		return "ICE_ERR_AQ_EMPTY";
7017 	case ICE_ERR_AQ_FW_CRITICAL:
7018 		return "ICE_ERR_AQ_FW_CRITICAL";
7019 	}
7020 
7021 	return "ICE_ERR_UNKNOWN";
7022 }
7023 
7024 /**
7025  * ice_set_rss_lut - Set RSS LUT
7026  * @vsi: Pointer to VSI structure
7027  * @lut: Lookup table
7028  * @lut_size: Lookup table size
7029  *
7030  * Returns 0 on success, negative on failure
7031  */
7032 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7033 {
7034 	struct ice_aq_get_set_rss_lut_params params = {};
7035 	struct ice_hw *hw = &vsi->back->hw;
7036 	enum ice_status status;
7037 
7038 	if (!lut)
7039 		return -EINVAL;
7040 
7041 	params.vsi_handle = vsi->idx;
7042 	params.lut_size = lut_size;
7043 	params.lut_type = vsi->rss_lut_type;
7044 	params.lut = lut;
7045 
7046 	status = ice_aq_set_rss_lut(hw, &params);
7047 	if (status) {
7048 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %s aq_err %s\n",
7049 			ice_stat_str(status),
7050 			ice_aq_str(hw->adminq.sq_last_status));
7051 		return -EIO;
7052 	}
7053 
7054 	return 0;
7055 }
7056 
7057 /**
7058  * ice_set_rss_key - Set RSS key
7059  * @vsi: Pointer to the VSI structure
7060  * @seed: RSS hash seed
7061  *
7062  * Returns 0 on success, negative on failure
7063  */
7064 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7065 {
7066 	struct ice_hw *hw = &vsi->back->hw;
7067 	enum ice_status status;
7068 
7069 	if (!seed)
7070 		return -EINVAL;
7071 
7072 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7073 	if (status) {
7074 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %s aq_err %s\n",
7075 			ice_stat_str(status),
7076 			ice_aq_str(hw->adminq.sq_last_status));
7077 		return -EIO;
7078 	}
7079 
7080 	return 0;
7081 }
7082 
7083 /**
7084  * ice_get_rss_lut - Get RSS LUT
7085  * @vsi: Pointer to VSI structure
7086  * @lut: Buffer to store the lookup table entries
7087  * @lut_size: Size of buffer to store the lookup table entries
7088  *
7089  * Returns 0 on success, negative on failure
7090  */
7091 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7092 {
7093 	struct ice_aq_get_set_rss_lut_params params = {};
7094 	struct ice_hw *hw = &vsi->back->hw;
7095 	enum ice_status status;
7096 
7097 	if (!lut)
7098 		return -EINVAL;
7099 
7100 	params.vsi_handle = vsi->idx;
7101 	params.lut_size = lut_size;
7102 	params.lut_type = vsi->rss_lut_type;
7103 	params.lut = lut;
7104 
7105 	status = ice_aq_get_rss_lut(hw, &params);
7106 	if (status) {
7107 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %s aq_err %s\n",
7108 			ice_stat_str(status),
7109 			ice_aq_str(hw->adminq.sq_last_status));
7110 		return -EIO;
7111 	}
7112 
7113 	return 0;
7114 }
7115 
7116 /**
7117  * ice_get_rss_key - Get RSS key
7118  * @vsi: Pointer to VSI structure
7119  * @seed: Buffer to store the key in
7120  *
7121  * Returns 0 on success, negative on failure
7122  */
7123 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7124 {
7125 	struct ice_hw *hw = &vsi->back->hw;
7126 	enum ice_status status;
7127 
7128 	if (!seed)
7129 		return -EINVAL;
7130 
7131 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7132 	if (status) {
7133 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %s aq_err %s\n",
7134 			ice_stat_str(status),
7135 			ice_aq_str(hw->adminq.sq_last_status));
7136 		return -EIO;
7137 	}
7138 
7139 	return 0;
7140 }
7141 
7142 /**
7143  * ice_bridge_getlink - Get the hardware bridge mode
7144  * @skb: skb buff
7145  * @pid: process ID
7146  * @seq: RTNL message seq
7147  * @dev: the netdev being configured
7148  * @filter_mask: filter mask passed in
7149  * @nlflags: netlink flags passed in
7150  *
7151  * Return the bridge mode (VEB/VEPA)
7152  */
7153 static int
7154 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7155 		   struct net_device *dev, u32 filter_mask, int nlflags)
7156 {
7157 	struct ice_netdev_priv *np = netdev_priv(dev);
7158 	struct ice_vsi *vsi = np->vsi;
7159 	struct ice_pf *pf = vsi->back;
7160 	u16 bmode;
7161 
7162 	bmode = pf->first_sw->bridge_mode;
7163 
7164 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7165 				       filter_mask, NULL);
7166 }
7167 
7168 /**
7169  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7170  * @vsi: Pointer to VSI structure
7171  * @bmode: Hardware bridge mode (VEB/VEPA)
7172  *
7173  * Returns 0 on success, negative on failure
7174  */
7175 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7176 {
7177 	struct ice_aqc_vsi_props *vsi_props;
7178 	struct ice_hw *hw = &vsi->back->hw;
7179 	struct ice_vsi_ctx *ctxt;
7180 	enum ice_status status;
7181 	int ret = 0;
7182 
7183 	vsi_props = &vsi->info;
7184 
7185 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7186 	if (!ctxt)
7187 		return -ENOMEM;
7188 
7189 	ctxt->info = vsi->info;
7190 
7191 	if (bmode == BRIDGE_MODE_VEB)
7192 		/* change from VEPA to VEB mode */
7193 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7194 	else
7195 		/* change from VEB to VEPA mode */
7196 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7197 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7198 
7199 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7200 	if (status) {
7201 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %s aq_err %s\n",
7202 			bmode, ice_stat_str(status),
7203 			ice_aq_str(hw->adminq.sq_last_status));
7204 		ret = -EIO;
7205 		goto out;
7206 	}
7207 	/* Update sw flags for book keeping */
7208 	vsi_props->sw_flags = ctxt->info.sw_flags;
7209 
7210 out:
7211 	kfree(ctxt);
7212 	return ret;
7213 }
7214 
7215 /**
7216  * ice_bridge_setlink - Set the hardware bridge mode
7217  * @dev: the netdev being configured
7218  * @nlh: RTNL message
7219  * @flags: bridge setlink flags
7220  * @extack: netlink extended ack
7221  *
7222  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7223  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7224  * not already set for all VSIs connected to this switch. And also update the
7225  * unicast switch filter rules for the corresponding switch of the netdev.
7226  */
7227 static int
7228 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7229 		   u16 __always_unused flags,
7230 		   struct netlink_ext_ack __always_unused *extack)
7231 {
7232 	struct ice_netdev_priv *np = netdev_priv(dev);
7233 	struct ice_pf *pf = np->vsi->back;
7234 	struct nlattr *attr, *br_spec;
7235 	struct ice_hw *hw = &pf->hw;
7236 	enum ice_status status;
7237 	struct ice_sw *pf_sw;
7238 	int rem, v, err = 0;
7239 
7240 	pf_sw = pf->first_sw;
7241 	/* find the attribute in the netlink message */
7242 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7243 
7244 	nla_for_each_nested(attr, br_spec, rem) {
7245 		__u16 mode;
7246 
7247 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
7248 			continue;
7249 		mode = nla_get_u16(attr);
7250 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7251 			return -EINVAL;
7252 		/* Continue  if bridge mode is not being flipped */
7253 		if (mode == pf_sw->bridge_mode)
7254 			continue;
7255 		/* Iterates through the PF VSI list and update the loopback
7256 		 * mode of the VSI
7257 		 */
7258 		ice_for_each_vsi(pf, v) {
7259 			if (!pf->vsi[v])
7260 				continue;
7261 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7262 			if (err)
7263 				return err;
7264 		}
7265 
7266 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7267 		/* Update the unicast switch filter rules for the corresponding
7268 		 * switch of the netdev
7269 		 */
7270 		status = ice_update_sw_rule_bridge_mode(hw);
7271 		if (status) {
7272 			netdev_err(dev, "switch rule update failed, mode = %d err %s aq_err %s\n",
7273 				   mode, ice_stat_str(status),
7274 				   ice_aq_str(hw->adminq.sq_last_status));
7275 			/* revert hw->evb_veb */
7276 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7277 			return -EIO;
7278 		}
7279 
7280 		pf_sw->bridge_mode = mode;
7281 	}
7282 
7283 	return 0;
7284 }
7285 
7286 /**
7287  * ice_tx_timeout - Respond to a Tx Hang
7288  * @netdev: network interface device structure
7289  * @txqueue: Tx queue
7290  */
7291 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7292 {
7293 	struct ice_netdev_priv *np = netdev_priv(netdev);
7294 	struct ice_tx_ring *tx_ring = NULL;
7295 	struct ice_vsi *vsi = np->vsi;
7296 	struct ice_pf *pf = vsi->back;
7297 	u32 i;
7298 
7299 	pf->tx_timeout_count++;
7300 
7301 	/* Check if PFC is enabled for the TC to which the queue belongs
7302 	 * to. If yes then Tx timeout is not caused by a hung queue, no
7303 	 * need to reset and rebuild
7304 	 */
7305 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7306 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7307 			 txqueue);
7308 		return;
7309 	}
7310 
7311 	/* now that we have an index, find the tx_ring struct */
7312 	ice_for_each_txq(vsi, i)
7313 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7314 			if (txqueue == vsi->tx_rings[i]->q_index) {
7315 				tx_ring = vsi->tx_rings[i];
7316 				break;
7317 			}
7318 
7319 	/* Reset recovery level if enough time has elapsed after last timeout.
7320 	 * Also ensure no new reset action happens before next timeout period.
7321 	 */
7322 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7323 		pf->tx_timeout_recovery_level = 1;
7324 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7325 				       netdev->watchdog_timeo)))
7326 		return;
7327 
7328 	if (tx_ring) {
7329 		struct ice_hw *hw = &pf->hw;
7330 		u32 head, val = 0;
7331 
7332 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7333 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7334 		/* Read interrupt register */
7335 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7336 
7337 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7338 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7339 			    head, tx_ring->next_to_use, val);
7340 	}
7341 
7342 	pf->tx_timeout_last_recovery = jiffies;
7343 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7344 		    pf->tx_timeout_recovery_level, txqueue);
7345 
7346 	switch (pf->tx_timeout_recovery_level) {
7347 	case 1:
7348 		set_bit(ICE_PFR_REQ, pf->state);
7349 		break;
7350 	case 2:
7351 		set_bit(ICE_CORER_REQ, pf->state);
7352 		break;
7353 	case 3:
7354 		set_bit(ICE_GLOBR_REQ, pf->state);
7355 		break;
7356 	default:
7357 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7358 		set_bit(ICE_DOWN, pf->state);
7359 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7360 		set_bit(ICE_SERVICE_DIS, pf->state);
7361 		break;
7362 	}
7363 
7364 	ice_service_task_schedule(pf);
7365 	pf->tx_timeout_recovery_level++;
7366 }
7367 
7368 /**
7369  * ice_setup_tc_cls_flower - flower classifier offloads
7370  * @np: net device to configure
7371  * @filter_dev: device on which filter is added
7372  * @cls_flower: offload data
7373  */
7374 static int
7375 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7376 			struct net_device *filter_dev,
7377 			struct flow_cls_offload *cls_flower)
7378 {
7379 	struct ice_vsi *vsi = np->vsi;
7380 
7381 	if (cls_flower->common.chain_index)
7382 		return -EOPNOTSUPP;
7383 
7384 	switch (cls_flower->command) {
7385 	case FLOW_CLS_REPLACE:
7386 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7387 	case FLOW_CLS_DESTROY:
7388 		return ice_del_cls_flower(vsi, cls_flower);
7389 	default:
7390 		return -EINVAL;
7391 	}
7392 }
7393 
7394 /**
7395  * ice_setup_tc_block_cb - callback handler registered for TC block
7396  * @type: TC SETUP type
7397  * @type_data: TC flower offload data that contains user input
7398  * @cb_priv: netdev private data
7399  */
7400 static int
7401 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7402 {
7403 	struct ice_netdev_priv *np = cb_priv;
7404 
7405 	switch (type) {
7406 	case TC_SETUP_CLSFLOWER:
7407 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7408 					       type_data);
7409 	default:
7410 		return -EOPNOTSUPP;
7411 	}
7412 }
7413 
7414 /**
7415  * ice_validate_mqprio_qopt - Validate TCF input parameters
7416  * @vsi: Pointer to VSI
7417  * @mqprio_qopt: input parameters for mqprio queue configuration
7418  *
7419  * This function validates MQPRIO params, such as qcount (power of 2 wherever
7420  * needed), and make sure user doesn't specify qcount and BW rate limit
7421  * for TCs, which are more than "num_tc"
7422  */
7423 static int
7424 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
7425 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
7426 {
7427 	u64 sum_max_rate = 0, sum_min_rate = 0;
7428 	int non_power_of_2_qcount = 0;
7429 	struct ice_pf *pf = vsi->back;
7430 	int max_rss_q_cnt = 0;
7431 	struct device *dev;
7432 	int i, speed;
7433 	u8 num_tc;
7434 
7435 	if (vsi->type != ICE_VSI_PF)
7436 		return -EINVAL;
7437 
7438 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7439 	    mqprio_qopt->qopt.num_tc < 1 ||
7440 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
7441 		return -EINVAL;
7442 
7443 	dev = ice_pf_to_dev(pf);
7444 	vsi->ch_rss_size = 0;
7445 	num_tc = mqprio_qopt->qopt.num_tc;
7446 
7447 	for (i = 0; num_tc; i++) {
7448 		int qcount = mqprio_qopt->qopt.count[i];
7449 		u64 max_rate, min_rate, rem;
7450 
7451 		if (!qcount)
7452 			return -EINVAL;
7453 
7454 		if (is_power_of_2(qcount)) {
7455 			if (non_power_of_2_qcount &&
7456 			    qcount > non_power_of_2_qcount) {
7457 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
7458 					qcount, non_power_of_2_qcount);
7459 				return -EINVAL;
7460 			}
7461 			if (qcount > max_rss_q_cnt)
7462 				max_rss_q_cnt = qcount;
7463 		} else {
7464 			if (non_power_of_2_qcount &&
7465 			    qcount != non_power_of_2_qcount) {
7466 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
7467 					qcount, non_power_of_2_qcount);
7468 				return -EINVAL;
7469 			}
7470 			if (qcount < max_rss_q_cnt) {
7471 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
7472 					qcount, max_rss_q_cnt);
7473 				return -EINVAL;
7474 			}
7475 			max_rss_q_cnt = qcount;
7476 			non_power_of_2_qcount = qcount;
7477 		}
7478 
7479 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
7480 		 * converts the bandwidth rate limit into Bytes/s when
7481 		 * passing it down to the driver. So convert input bandwidth
7482 		 * from Bytes/s to Kbps
7483 		 */
7484 		max_rate = mqprio_qopt->max_rate[i];
7485 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
7486 		sum_max_rate += max_rate;
7487 
7488 		/* min_rate is minimum guaranteed rate and it can't be zero */
7489 		min_rate = mqprio_qopt->min_rate[i];
7490 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
7491 		sum_min_rate += min_rate;
7492 
7493 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
7494 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
7495 				min_rate, ICE_MIN_BW_LIMIT);
7496 			return -EINVAL;
7497 		}
7498 
7499 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
7500 		if (rem) {
7501 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
7502 				i, ICE_MIN_BW_LIMIT);
7503 			return -EINVAL;
7504 		}
7505 
7506 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
7507 		if (rem) {
7508 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
7509 				i, ICE_MIN_BW_LIMIT);
7510 			return -EINVAL;
7511 		}
7512 
7513 		/* min_rate can't be more than max_rate, except when max_rate
7514 		 * is zero (implies max_rate sought is max line rate). In such
7515 		 * a case min_rate can be more than max.
7516 		 */
7517 		if (max_rate && min_rate > max_rate) {
7518 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
7519 				min_rate, max_rate);
7520 			return -EINVAL;
7521 		}
7522 
7523 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7524 			break;
7525 		if (mqprio_qopt->qopt.offset[i + 1] !=
7526 		    (mqprio_qopt->qopt.offset[i] + qcount))
7527 			return -EINVAL;
7528 	}
7529 	if (vsi->num_rxq <
7530 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7531 		return -EINVAL;
7532 	if (vsi->num_txq <
7533 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7534 		return -EINVAL;
7535 
7536 	speed = ice_get_link_speed_kbps(vsi);
7537 	if (sum_max_rate && sum_max_rate > (u64)speed) {
7538 		dev_err(dev, "Invalid max Tx rate(%llu) Kbps > speed(%u) Kbps specified\n",
7539 			sum_max_rate, speed);
7540 		return -EINVAL;
7541 	}
7542 	if (sum_min_rate && sum_min_rate > (u64)speed) {
7543 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
7544 			sum_min_rate, speed);
7545 		return -EINVAL;
7546 	}
7547 
7548 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
7549 	vsi->ch_rss_size = max_rss_q_cnt;
7550 
7551 	return 0;
7552 }
7553 
7554 /**
7555  * ice_add_channel - add a channel by adding VSI
7556  * @pf: ptr to PF device
7557  * @sw_id: underlying HW switching element ID
7558  * @ch: ptr to channel structure
7559  *
7560  * Add a channel (VSI) using add_vsi and queue_map
7561  */
7562 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
7563 {
7564 	struct device *dev = ice_pf_to_dev(pf);
7565 	struct ice_vsi *vsi;
7566 
7567 	if (ch->type != ICE_VSI_CHNL) {
7568 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
7569 		return -EINVAL;
7570 	}
7571 
7572 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
7573 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
7574 		dev_err(dev, "create chnl VSI failure\n");
7575 		return -EINVAL;
7576 	}
7577 
7578 	ch->sw_id = sw_id;
7579 	ch->vsi_num = vsi->vsi_num;
7580 	ch->info.mapping_flags = vsi->info.mapping_flags;
7581 	ch->ch_vsi = vsi;
7582 	/* set the back pointer of channel for newly created VSI */
7583 	vsi->ch = ch;
7584 
7585 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
7586 	       sizeof(vsi->info.q_mapping));
7587 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
7588 	       sizeof(vsi->info.tc_mapping));
7589 
7590 	return 0;
7591 }
7592 
7593 /**
7594  * ice_chnl_cfg_res
7595  * @vsi: the VSI being setup
7596  * @ch: ptr to channel structure
7597  *
7598  * Configure channel specific resources such as rings, vector.
7599  */
7600 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
7601 {
7602 	int i;
7603 
7604 	for (i = 0; i < ch->num_txq; i++) {
7605 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
7606 		struct ice_ring_container *rc;
7607 		struct ice_tx_ring *tx_ring;
7608 		struct ice_rx_ring *rx_ring;
7609 
7610 		tx_ring = vsi->tx_rings[ch->base_q + i];
7611 		rx_ring = vsi->rx_rings[ch->base_q + i];
7612 		if (!tx_ring || !rx_ring)
7613 			continue;
7614 
7615 		/* setup ring being channel enabled */
7616 		tx_ring->ch = ch;
7617 		rx_ring->ch = ch;
7618 
7619 		/* following code block sets up vector specific attributes */
7620 		tx_q_vector = tx_ring->q_vector;
7621 		rx_q_vector = rx_ring->q_vector;
7622 		if (!tx_q_vector && !rx_q_vector)
7623 			continue;
7624 
7625 		if (tx_q_vector) {
7626 			tx_q_vector->ch = ch;
7627 			/* setup Tx and Rx ITR setting if DIM is off */
7628 			rc = &tx_q_vector->tx;
7629 			if (!ITR_IS_DYNAMIC(rc))
7630 				ice_write_itr(rc, rc->itr_setting);
7631 		}
7632 		if (rx_q_vector) {
7633 			rx_q_vector->ch = ch;
7634 			/* setup Tx and Rx ITR setting if DIM is off */
7635 			rc = &rx_q_vector->rx;
7636 			if (!ITR_IS_DYNAMIC(rc))
7637 				ice_write_itr(rc, rc->itr_setting);
7638 		}
7639 	}
7640 
7641 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
7642 	 * GLINT_ITR register would have written to perform in-context
7643 	 * update, hence perform flush
7644 	 */
7645 	if (ch->num_txq || ch->num_rxq)
7646 		ice_flush(&vsi->back->hw);
7647 }
7648 
7649 /**
7650  * ice_cfg_chnl_all_res - configure channel resources
7651  * @vsi: pte to main_vsi
7652  * @ch: ptr to channel structure
7653  *
7654  * This function configures channel specific resources such as flow-director
7655  * counter index, and other resources such as queues, vectors, ITR settings
7656  */
7657 static void
7658 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
7659 {
7660 	/* configure channel (aka ADQ) resources such as queues, vectors,
7661 	 * ITR settings for channel specific vectors and anything else
7662 	 */
7663 	ice_chnl_cfg_res(vsi, ch);
7664 }
7665 
7666 /**
7667  * ice_setup_hw_channel - setup new channel
7668  * @pf: ptr to PF device
7669  * @vsi: the VSI being setup
7670  * @ch: ptr to channel structure
7671  * @sw_id: underlying HW switching element ID
7672  * @type: type of channel to be created (VMDq2/VF)
7673  *
7674  * Setup new channel (VSI) based on specified type (VMDq2/VF)
7675  * and configures Tx rings accordingly
7676  */
7677 static int
7678 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
7679 		     struct ice_channel *ch, u16 sw_id, u8 type)
7680 {
7681 	struct device *dev = ice_pf_to_dev(pf);
7682 	int ret;
7683 
7684 	ch->base_q = vsi->next_base_q;
7685 	ch->type = type;
7686 
7687 	ret = ice_add_channel(pf, sw_id, ch);
7688 	if (ret) {
7689 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
7690 		return ret;
7691 	}
7692 
7693 	/* configure/setup ADQ specific resources */
7694 	ice_cfg_chnl_all_res(vsi, ch);
7695 
7696 	/* make sure to update the next_base_q so that subsequent channel's
7697 	 * (aka ADQ) VSI queue map is correct
7698 	 */
7699 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
7700 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
7701 		ch->num_rxq);
7702 
7703 	return 0;
7704 }
7705 
7706 /**
7707  * ice_setup_channel - setup new channel using uplink element
7708  * @pf: ptr to PF device
7709  * @vsi: the VSI being setup
7710  * @ch: ptr to channel structure
7711  *
7712  * Setup new channel (VSI) based on specified type (VMDq2/VF)
7713  * and uplink switching element
7714  */
7715 static bool
7716 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
7717 		  struct ice_channel *ch)
7718 {
7719 	struct device *dev = ice_pf_to_dev(pf);
7720 	u16 sw_id;
7721 	int ret;
7722 
7723 	if (vsi->type != ICE_VSI_PF) {
7724 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
7725 		return false;
7726 	}
7727 
7728 	sw_id = pf->first_sw->sw_id;
7729 
7730 	/* create channel (VSI) */
7731 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
7732 	if (ret) {
7733 		dev_err(dev, "failed to setup hw_channel\n");
7734 		return false;
7735 	}
7736 	dev_dbg(dev, "successfully created channel()\n");
7737 
7738 	return ch->ch_vsi ? true : false;
7739 }
7740 
7741 /**
7742  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
7743  * @vsi: VSI to be configured
7744  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
7745  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
7746  */
7747 static int
7748 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
7749 {
7750 	int err;
7751 
7752 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
7753 	if (err)
7754 		return err;
7755 
7756 	return ice_set_max_bw_limit(vsi, max_tx_rate);
7757 }
7758 
7759 /**
7760  * ice_create_q_channel - function to create channel
7761  * @vsi: VSI to be configured
7762  * @ch: ptr to channel (it contains channel specific params)
7763  *
7764  * This function creates channel (VSI) using num_queues specified by user,
7765  * reconfigs RSS if needed.
7766  */
7767 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
7768 {
7769 	struct ice_pf *pf = vsi->back;
7770 	struct device *dev;
7771 
7772 	if (!ch)
7773 		return -EINVAL;
7774 
7775 	dev = ice_pf_to_dev(pf);
7776 	if (!ch->num_txq || !ch->num_rxq) {
7777 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
7778 		return -EINVAL;
7779 	}
7780 
7781 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
7782 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
7783 			vsi->cnt_q_avail, ch->num_txq);
7784 		return -EINVAL;
7785 	}
7786 
7787 	if (!ice_setup_channel(pf, vsi, ch)) {
7788 		dev_info(dev, "Failed to setup channel\n");
7789 		return -EINVAL;
7790 	}
7791 	/* configure BW rate limit */
7792 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
7793 		int ret;
7794 
7795 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
7796 				       ch->min_tx_rate);
7797 		if (ret)
7798 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
7799 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
7800 		else
7801 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
7802 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
7803 	}
7804 
7805 	vsi->cnt_q_avail -= ch->num_txq;
7806 
7807 	return 0;
7808 }
7809 
7810 /**
7811  * ice_rem_all_chnl_fltrs - removes all channel filters
7812  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
7813  *
7814  * Remove all advanced switch filters only if they are channel specific
7815  * tc-flower based filter
7816  */
7817 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
7818 {
7819 	struct ice_tc_flower_fltr *fltr;
7820 	struct hlist_node *node;
7821 
7822 	/* to remove all channel filters, iterate an ordered list of filters */
7823 	hlist_for_each_entry_safe(fltr, node,
7824 				  &pf->tc_flower_fltr_list,
7825 				  tc_flower_node) {
7826 		struct ice_rule_query_data rule;
7827 		int status;
7828 
7829 		/* for now process only channel specific filters */
7830 		if (!ice_is_chnl_fltr(fltr))
7831 			continue;
7832 
7833 		rule.rid = fltr->rid;
7834 		rule.rule_id = fltr->rule_id;
7835 		rule.vsi_handle = fltr->dest_id;
7836 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
7837 		if (status) {
7838 			if (status == -ENOENT)
7839 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
7840 					rule.rule_id);
7841 			else
7842 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
7843 					status);
7844 		} else if (fltr->dest_vsi) {
7845 			/* update advanced switch filter count */
7846 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
7847 				u32 flags = fltr->flags;
7848 
7849 				fltr->dest_vsi->num_chnl_fltr--;
7850 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
7851 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
7852 					pf->num_dmac_chnl_fltrs--;
7853 			}
7854 		}
7855 
7856 		hlist_del(&fltr->tc_flower_node);
7857 		kfree(fltr);
7858 	}
7859 }
7860 
7861 /**
7862  * ice_remove_q_channels - Remove queue channels for the TCs
7863  * @vsi: VSI to be configured
7864  * @rem_fltr: delete advanced switch filter or not
7865  *
7866  * Remove queue channels for the TCs
7867  */
7868 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
7869 {
7870 	struct ice_channel *ch, *ch_tmp;
7871 	struct ice_pf *pf = vsi->back;
7872 	int i;
7873 
7874 	/* remove all tc-flower based filter if they are channel filters only */
7875 	if (rem_fltr)
7876 		ice_rem_all_chnl_fltrs(pf);
7877 
7878 	/* perform cleanup for channels if they exist */
7879 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
7880 		struct ice_vsi *ch_vsi;
7881 
7882 		list_del(&ch->list);
7883 		ch_vsi = ch->ch_vsi;
7884 		if (!ch_vsi) {
7885 			kfree(ch);
7886 			continue;
7887 		}
7888 
7889 		/* Reset queue contexts */
7890 		for (i = 0; i < ch->num_rxq; i++) {
7891 			struct ice_tx_ring *tx_ring;
7892 			struct ice_rx_ring *rx_ring;
7893 
7894 			tx_ring = vsi->tx_rings[ch->base_q + i];
7895 			rx_ring = vsi->rx_rings[ch->base_q + i];
7896 			if (tx_ring) {
7897 				tx_ring->ch = NULL;
7898 				if (tx_ring->q_vector)
7899 					tx_ring->q_vector->ch = NULL;
7900 			}
7901 			if (rx_ring) {
7902 				rx_ring->ch = NULL;
7903 				if (rx_ring->q_vector)
7904 					rx_ring->q_vector->ch = NULL;
7905 			}
7906 		}
7907 
7908 		/* clear the VSI from scheduler tree */
7909 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
7910 
7911 		/* Delete VSI from FW */
7912 		ice_vsi_delete(ch->ch_vsi);
7913 
7914 		/* Delete VSI from PF and HW VSI arrays */
7915 		ice_vsi_clear(ch->ch_vsi);
7916 
7917 		/* free the channel */
7918 		kfree(ch);
7919 	}
7920 
7921 	/* clear the channel VSI map which is stored in main VSI */
7922 	ice_for_each_chnl_tc(i)
7923 		vsi->tc_map_vsi[i] = NULL;
7924 
7925 	/* reset main VSI's all TC information */
7926 	vsi->all_enatc = 0;
7927 	vsi->all_numtc = 0;
7928 }
7929 
7930 /**
7931  * ice_rebuild_channels - rebuild channel
7932  * @pf: ptr to PF
7933  *
7934  * Recreate channel VSIs and replay filters
7935  */
7936 static int ice_rebuild_channels(struct ice_pf *pf)
7937 {
7938 	struct device *dev = ice_pf_to_dev(pf);
7939 	struct ice_vsi *main_vsi;
7940 	bool rem_adv_fltr = true;
7941 	struct ice_channel *ch;
7942 	struct ice_vsi *vsi;
7943 	int tc_idx = 1;
7944 	int i, err;
7945 
7946 	main_vsi = ice_get_main_vsi(pf);
7947 	if (!main_vsi)
7948 		return 0;
7949 
7950 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
7951 	    main_vsi->old_numtc == 1)
7952 		return 0; /* nothing to be done */
7953 
7954 	/* reconfigure main VSI based on old value of TC and cached values
7955 	 * for MQPRIO opts
7956 	 */
7957 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
7958 	if (err) {
7959 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
7960 			main_vsi->old_ena_tc, main_vsi->vsi_num);
7961 		return err;
7962 	}
7963 
7964 	/* rebuild ADQ VSIs */
7965 	ice_for_each_vsi(pf, i) {
7966 		enum ice_vsi_type type;
7967 
7968 		vsi = pf->vsi[i];
7969 		if (!vsi || vsi->type != ICE_VSI_CHNL)
7970 			continue;
7971 
7972 		type = vsi->type;
7973 
7974 		/* rebuild ADQ VSI */
7975 		err = ice_vsi_rebuild(vsi, true);
7976 		if (err) {
7977 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
7978 				ice_vsi_type_str(type), vsi->idx, err);
7979 			goto cleanup;
7980 		}
7981 
7982 		/* Re-map HW VSI number, using VSI handle that has been
7983 		 * previously validated in ice_replay_vsi() call above
7984 		 */
7985 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7986 
7987 		/* replay filters for the VSI */
7988 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7989 		if (err) {
7990 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
7991 				ice_vsi_type_str(type), err, vsi->idx);
7992 			rem_adv_fltr = false;
7993 			goto cleanup;
7994 		}
7995 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
7996 			 ice_vsi_type_str(type), vsi->idx);
7997 
7998 		/* store ADQ VSI at correct TC index in main VSI's
7999 		 * map of TC to VSI
8000 		 */
8001 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8002 	}
8003 
8004 	/* ADQ VSI(s) has been rebuilt successfully, so setup
8005 	 * channel for main VSI's Tx and Rx rings
8006 	 */
8007 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8008 		struct ice_vsi *ch_vsi;
8009 
8010 		ch_vsi = ch->ch_vsi;
8011 		if (!ch_vsi)
8012 			continue;
8013 
8014 		/* reconfig channel resources */
8015 		ice_cfg_chnl_all_res(main_vsi, ch);
8016 
8017 		/* replay BW rate limit if it is non-zero */
8018 		if (!ch->max_tx_rate && !ch->min_tx_rate)
8019 			continue;
8020 
8021 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8022 				       ch->min_tx_rate);
8023 		if (err)
8024 			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",
8025 				err, ch->max_tx_rate, ch->min_tx_rate,
8026 				ch_vsi->vsi_num);
8027 		else
8028 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8029 				ch->max_tx_rate, ch->min_tx_rate,
8030 				ch_vsi->vsi_num);
8031 	}
8032 
8033 	/* reconfig RSS for main VSI */
8034 	if (main_vsi->ch_rss_size)
8035 		ice_vsi_cfg_rss_lut_key(main_vsi);
8036 
8037 	return 0;
8038 
8039 cleanup:
8040 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8041 	return err;
8042 }
8043 
8044 /**
8045  * ice_create_q_channels - Add queue channel for the given TCs
8046  * @vsi: VSI to be configured
8047  *
8048  * Configures queue channel mapping to the given TCs
8049  */
8050 static int ice_create_q_channels(struct ice_vsi *vsi)
8051 {
8052 	struct ice_pf *pf = vsi->back;
8053 	struct ice_channel *ch;
8054 	int ret = 0, i;
8055 
8056 	ice_for_each_chnl_tc(i) {
8057 		if (!(vsi->all_enatc & BIT(i)))
8058 			continue;
8059 
8060 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8061 		if (!ch) {
8062 			ret = -ENOMEM;
8063 			goto err_free;
8064 		}
8065 		INIT_LIST_HEAD(&ch->list);
8066 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8067 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8068 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8069 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8070 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8071 
8072 		/* convert to Kbits/s */
8073 		if (ch->max_tx_rate)
8074 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8075 						  ICE_BW_KBPS_DIVISOR);
8076 		if (ch->min_tx_rate)
8077 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8078 						  ICE_BW_KBPS_DIVISOR);
8079 
8080 		ret = ice_create_q_channel(vsi, ch);
8081 		if (ret) {
8082 			dev_err(ice_pf_to_dev(pf),
8083 				"failed creating channel TC:%d\n", i);
8084 			kfree(ch);
8085 			goto err_free;
8086 		}
8087 		list_add_tail(&ch->list, &vsi->ch_list);
8088 		vsi->tc_map_vsi[i] = ch->ch_vsi;
8089 		dev_dbg(ice_pf_to_dev(pf),
8090 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8091 	}
8092 	return 0;
8093 
8094 err_free:
8095 	ice_remove_q_channels(vsi, false);
8096 
8097 	return ret;
8098 }
8099 
8100 /**
8101  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8102  * @netdev: net device to configure
8103  * @type_data: TC offload data
8104  */
8105 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8106 {
8107 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8108 	struct ice_netdev_priv *np = netdev_priv(netdev);
8109 	struct ice_vsi *vsi = np->vsi;
8110 	struct ice_pf *pf = vsi->back;
8111 	u16 mode, ena_tc_qdisc = 0;
8112 	int cur_txq, cur_rxq;
8113 	u8 hw = 0, num_tcf;
8114 	struct device *dev;
8115 	int ret, i;
8116 
8117 	dev = ice_pf_to_dev(pf);
8118 	num_tcf = mqprio_qopt->qopt.num_tc;
8119 	hw = mqprio_qopt->qopt.hw;
8120 	mode = mqprio_qopt->mode;
8121 	if (!hw) {
8122 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8123 		vsi->ch_rss_size = 0;
8124 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8125 		goto config_tcf;
8126 	}
8127 
8128 	/* Generate queue region map for number of TCF requested */
8129 	for (i = 0; i < num_tcf; i++)
8130 		ena_tc_qdisc |= BIT(i);
8131 
8132 	switch (mode) {
8133 	case TC_MQPRIO_MODE_CHANNEL:
8134 
8135 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8136 		if (ret) {
8137 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8138 				   ret);
8139 			return ret;
8140 		}
8141 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8142 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8143 		/* don't assume state of hw_tc_offload during driver load
8144 		 * and set the flag for TC flower filter if hw_tc_offload
8145 		 * already ON
8146 		 */
8147 		if (vsi->netdev->features & NETIF_F_HW_TC)
8148 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8149 		break;
8150 	default:
8151 		return -EINVAL;
8152 	}
8153 
8154 config_tcf:
8155 
8156 	/* Requesting same TCF configuration as already enabled */
8157 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8158 	    mode != TC_MQPRIO_MODE_CHANNEL)
8159 		return 0;
8160 
8161 	/* Pause VSI queues */
8162 	ice_dis_vsi(vsi, true);
8163 
8164 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8165 		ice_remove_q_channels(vsi, true);
8166 
8167 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8168 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8169 				     num_online_cpus());
8170 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8171 				     num_online_cpus());
8172 	} else {
8173 		/* logic to rebuild VSI, same like ethtool -L */
8174 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8175 
8176 		for (i = 0; i < num_tcf; i++) {
8177 			if (!(ena_tc_qdisc & BIT(i)))
8178 				continue;
8179 
8180 			offset = vsi->mqprio_qopt.qopt.offset[i];
8181 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8182 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8183 		}
8184 		vsi->req_txq = offset + qcount_tx;
8185 		vsi->req_rxq = offset + qcount_rx;
8186 
8187 		/* store away original rss_size info, so that it gets reused
8188 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8189 		 * determine, what should be the rss_sizefor main VSI
8190 		 */
8191 		vsi->orig_rss_size = vsi->rss_size;
8192 	}
8193 
8194 	/* save current values of Tx and Rx queues before calling VSI rebuild
8195 	 * for fallback option
8196 	 */
8197 	cur_txq = vsi->num_txq;
8198 	cur_rxq = vsi->num_rxq;
8199 
8200 	/* proceed with rebuild main VSI using correct number of queues */
8201 	ret = ice_vsi_rebuild(vsi, false);
8202 	if (ret) {
8203 		/* fallback to current number of queues */
8204 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8205 		vsi->req_txq = cur_txq;
8206 		vsi->req_rxq = cur_rxq;
8207 		clear_bit(ICE_RESET_FAILED, pf->state);
8208 		if (ice_vsi_rebuild(vsi, false)) {
8209 			dev_err(dev, "Rebuild of main VSI failed again\n");
8210 			return ret;
8211 		}
8212 	}
8213 
8214 	vsi->all_numtc = num_tcf;
8215 	vsi->all_enatc = ena_tc_qdisc;
8216 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8217 	if (ret) {
8218 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8219 			   vsi->vsi_num);
8220 		goto exit;
8221 	}
8222 
8223 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8224 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8225 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8226 
8227 		/* set TC0 rate limit if specified */
8228 		if (max_tx_rate || min_tx_rate) {
8229 			/* convert to Kbits/s */
8230 			if (max_tx_rate)
8231 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8232 			if (min_tx_rate)
8233 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8234 
8235 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8236 			if (!ret) {
8237 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8238 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8239 			} else {
8240 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8241 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8242 				goto exit;
8243 			}
8244 		}
8245 		ret = ice_create_q_channels(vsi);
8246 		if (ret) {
8247 			netdev_err(netdev, "failed configuring queue channels\n");
8248 			goto exit;
8249 		} else {
8250 			netdev_dbg(netdev, "successfully configured channels\n");
8251 		}
8252 	}
8253 
8254 	if (vsi->ch_rss_size)
8255 		ice_vsi_cfg_rss_lut_key(vsi);
8256 
8257 exit:
8258 	/* if error, reset the all_numtc and all_enatc */
8259 	if (ret) {
8260 		vsi->all_numtc = 0;
8261 		vsi->all_enatc = 0;
8262 	}
8263 	/* resume VSI */
8264 	ice_ena_vsi(vsi, true);
8265 
8266 	return ret;
8267 }
8268 
8269 static LIST_HEAD(ice_block_cb_list);
8270 
8271 static int
8272 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8273 	     void *type_data)
8274 {
8275 	struct ice_netdev_priv *np = netdev_priv(netdev);
8276 	struct ice_pf *pf = np->vsi->back;
8277 	int err;
8278 
8279 	switch (type) {
8280 	case TC_SETUP_BLOCK:
8281 		return flow_block_cb_setup_simple(type_data,
8282 						  &ice_block_cb_list,
8283 						  ice_setup_tc_block_cb,
8284 						  np, np, true);
8285 	case TC_SETUP_QDISC_MQPRIO:
8286 		/* setup traffic classifier for receive side */
8287 		mutex_lock(&pf->tc_mutex);
8288 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8289 		mutex_unlock(&pf->tc_mutex);
8290 		return err;
8291 	default:
8292 		return -EOPNOTSUPP;
8293 	}
8294 	return -EOPNOTSUPP;
8295 }
8296 
8297 static struct ice_indr_block_priv *
8298 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8299 			   struct net_device *netdev)
8300 {
8301 	struct ice_indr_block_priv *cb_priv;
8302 
8303 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8304 		if (!cb_priv->netdev)
8305 			return NULL;
8306 		if (cb_priv->netdev == netdev)
8307 			return cb_priv;
8308 	}
8309 	return NULL;
8310 }
8311 
8312 static int
8313 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
8314 			void *indr_priv)
8315 {
8316 	struct ice_indr_block_priv *priv = indr_priv;
8317 	struct ice_netdev_priv *np = priv->np;
8318 
8319 	switch (type) {
8320 	case TC_SETUP_CLSFLOWER:
8321 		return ice_setup_tc_cls_flower(np, priv->netdev,
8322 					       (struct flow_cls_offload *)
8323 					       type_data);
8324 	default:
8325 		return -EOPNOTSUPP;
8326 	}
8327 }
8328 
8329 static int
8330 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
8331 			struct ice_netdev_priv *np,
8332 			struct flow_block_offload *f, void *data,
8333 			void (*cleanup)(struct flow_block_cb *block_cb))
8334 {
8335 	struct ice_indr_block_priv *indr_priv;
8336 	struct flow_block_cb *block_cb;
8337 
8338 	if (!ice_is_tunnel_supported(netdev) &&
8339 	    !(is_vlan_dev(netdev) &&
8340 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
8341 		return -EOPNOTSUPP;
8342 
8343 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8344 		return -EOPNOTSUPP;
8345 
8346 	switch (f->command) {
8347 	case FLOW_BLOCK_BIND:
8348 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8349 		if (indr_priv)
8350 			return -EEXIST;
8351 
8352 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
8353 		if (!indr_priv)
8354 			return -ENOMEM;
8355 
8356 		indr_priv->netdev = netdev;
8357 		indr_priv->np = np;
8358 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
8359 
8360 		block_cb =
8361 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
8362 						 indr_priv, indr_priv,
8363 						 ice_rep_indr_tc_block_unbind,
8364 						 f, netdev, sch, data, np,
8365 						 cleanup);
8366 
8367 		if (IS_ERR(block_cb)) {
8368 			list_del(&indr_priv->list);
8369 			kfree(indr_priv);
8370 			return PTR_ERR(block_cb);
8371 		}
8372 		flow_block_cb_add(block_cb, f);
8373 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
8374 		break;
8375 	case FLOW_BLOCK_UNBIND:
8376 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8377 		if (!indr_priv)
8378 			return -ENOENT;
8379 
8380 		block_cb = flow_block_cb_lookup(f->block,
8381 						ice_indr_setup_block_cb,
8382 						indr_priv);
8383 		if (!block_cb)
8384 			return -ENOENT;
8385 
8386 		flow_indr_block_cb_remove(block_cb, f);
8387 
8388 		list_del(&block_cb->driver_list);
8389 		break;
8390 	default:
8391 		return -EOPNOTSUPP;
8392 	}
8393 	return 0;
8394 }
8395 
8396 static int
8397 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
8398 		     void *cb_priv, enum tc_setup_type type, void *type_data,
8399 		     void *data,
8400 		     void (*cleanup)(struct flow_block_cb *block_cb))
8401 {
8402 	switch (type) {
8403 	case TC_SETUP_BLOCK:
8404 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
8405 					       data, cleanup);
8406 
8407 	default:
8408 		return -EOPNOTSUPP;
8409 	}
8410 }
8411 
8412 /**
8413  * ice_open - Called when a network interface becomes active
8414  * @netdev: network interface device structure
8415  *
8416  * The open entry point is called when a network interface is made
8417  * active by the system (IFF_UP). At this point all resources needed
8418  * for transmit and receive operations are allocated, the interrupt
8419  * handler is registered with the OS, the netdev watchdog is enabled,
8420  * and the stack is notified that the interface is ready.
8421  *
8422  * Returns 0 on success, negative value on failure
8423  */
8424 int ice_open(struct net_device *netdev)
8425 {
8426 	struct ice_netdev_priv *np = netdev_priv(netdev);
8427 	struct ice_pf *pf = np->vsi->back;
8428 
8429 	if (ice_is_reset_in_progress(pf->state)) {
8430 		netdev_err(netdev, "can't open net device while reset is in progress");
8431 		return -EBUSY;
8432 	}
8433 
8434 	return ice_open_internal(netdev);
8435 }
8436 
8437 /**
8438  * ice_open_internal - Called when a network interface becomes active
8439  * @netdev: network interface device structure
8440  *
8441  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
8442  * handling routine
8443  *
8444  * Returns 0 on success, negative value on failure
8445  */
8446 int ice_open_internal(struct net_device *netdev)
8447 {
8448 	struct ice_netdev_priv *np = netdev_priv(netdev);
8449 	struct ice_vsi *vsi = np->vsi;
8450 	struct ice_pf *pf = vsi->back;
8451 	struct ice_port_info *pi;
8452 	enum ice_status status;
8453 	int err;
8454 
8455 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
8456 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
8457 		return -EIO;
8458 	}
8459 
8460 	netif_carrier_off(netdev);
8461 
8462 	pi = vsi->port_info;
8463 	status = ice_update_link_info(pi);
8464 	if (status) {
8465 		netdev_err(netdev, "Failed to get link info, error %s\n",
8466 			   ice_stat_str(status));
8467 		return -EIO;
8468 	}
8469 
8470 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
8471 
8472 	/* Set PHY if there is media, otherwise, turn off PHY */
8473 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
8474 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8475 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
8476 			err = ice_init_phy_user_cfg(pi);
8477 			if (err) {
8478 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
8479 					   err);
8480 				return err;
8481 			}
8482 		}
8483 
8484 		err = ice_configure_phy(vsi);
8485 		if (err) {
8486 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
8487 				   err);
8488 			return err;
8489 		}
8490 	} else {
8491 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8492 		ice_set_link(vsi, false);
8493 	}
8494 
8495 	err = ice_vsi_open(vsi);
8496 	if (err)
8497 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
8498 			   vsi->vsi_num, vsi->vsw->sw_id);
8499 
8500 	/* Update existing tunnels information */
8501 	udp_tunnel_get_rx_info(netdev);
8502 
8503 	return err;
8504 }
8505 
8506 /**
8507  * ice_stop - Disables a network interface
8508  * @netdev: network interface device structure
8509  *
8510  * The stop entry point is called when an interface is de-activated by the OS,
8511  * and the netdevice enters the DOWN state. The hardware is still under the
8512  * driver's control, but the netdev interface is disabled.
8513  *
8514  * Returns success only - not allowed to fail
8515  */
8516 int ice_stop(struct net_device *netdev)
8517 {
8518 	struct ice_netdev_priv *np = netdev_priv(netdev);
8519 	struct ice_vsi *vsi = np->vsi;
8520 	struct ice_pf *pf = vsi->back;
8521 
8522 	if (ice_is_reset_in_progress(pf->state)) {
8523 		netdev_err(netdev, "can't stop net device while reset is in progress");
8524 		return -EBUSY;
8525 	}
8526 
8527 	ice_vsi_close(vsi);
8528 
8529 	return 0;
8530 }
8531 
8532 /**
8533  * ice_features_check - Validate encapsulated packet conforms to limits
8534  * @skb: skb buffer
8535  * @netdev: This port's netdev
8536  * @features: Offload features that the stack believes apply
8537  */
8538 static netdev_features_t
8539 ice_features_check(struct sk_buff *skb,
8540 		   struct net_device __always_unused *netdev,
8541 		   netdev_features_t features)
8542 {
8543 	size_t len;
8544 
8545 	/* No point in doing any of this if neither checksum nor GSO are
8546 	 * being requested for this frame. We can rule out both by just
8547 	 * checking for CHECKSUM_PARTIAL
8548 	 */
8549 	if (skb->ip_summed != CHECKSUM_PARTIAL)
8550 		return features;
8551 
8552 	/* We cannot support GSO if the MSS is going to be less than
8553 	 * 64 bytes. If it is then we need to drop support for GSO.
8554 	 */
8555 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
8556 		features &= ~NETIF_F_GSO_MASK;
8557 
8558 	len = skb_network_header(skb) - skb->data;
8559 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
8560 		goto out_rm_features;
8561 
8562 	len = skb_transport_header(skb) - skb_network_header(skb);
8563 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8564 		goto out_rm_features;
8565 
8566 	if (skb->encapsulation) {
8567 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
8568 		if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
8569 			goto out_rm_features;
8570 
8571 		len = skb_inner_transport_header(skb) -
8572 		      skb_inner_network_header(skb);
8573 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8574 			goto out_rm_features;
8575 	}
8576 
8577 	return features;
8578 out_rm_features:
8579 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
8580 }
8581 
8582 static const struct net_device_ops ice_netdev_safe_mode_ops = {
8583 	.ndo_open = ice_open,
8584 	.ndo_stop = ice_stop,
8585 	.ndo_start_xmit = ice_start_xmit,
8586 	.ndo_set_mac_address = ice_set_mac_address,
8587 	.ndo_validate_addr = eth_validate_addr,
8588 	.ndo_change_mtu = ice_change_mtu,
8589 	.ndo_get_stats64 = ice_get_stats64,
8590 	.ndo_tx_timeout = ice_tx_timeout,
8591 	.ndo_bpf = ice_xdp_safe_mode,
8592 };
8593 
8594 static const struct net_device_ops ice_netdev_ops = {
8595 	.ndo_open = ice_open,
8596 	.ndo_stop = ice_stop,
8597 	.ndo_start_xmit = ice_start_xmit,
8598 	.ndo_select_queue = ice_select_queue,
8599 	.ndo_features_check = ice_features_check,
8600 	.ndo_set_rx_mode = ice_set_rx_mode,
8601 	.ndo_set_mac_address = ice_set_mac_address,
8602 	.ndo_validate_addr = eth_validate_addr,
8603 	.ndo_change_mtu = ice_change_mtu,
8604 	.ndo_get_stats64 = ice_get_stats64,
8605 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
8606 	.ndo_eth_ioctl = ice_eth_ioctl,
8607 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
8608 	.ndo_set_vf_mac = ice_set_vf_mac,
8609 	.ndo_get_vf_config = ice_get_vf_cfg,
8610 	.ndo_set_vf_trust = ice_set_vf_trust,
8611 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
8612 	.ndo_set_vf_link_state = ice_set_vf_link_state,
8613 	.ndo_get_vf_stats = ice_get_vf_stats,
8614 	.ndo_set_vf_rate = ice_set_vf_bw,
8615 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
8616 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
8617 	.ndo_setup_tc = ice_setup_tc,
8618 	.ndo_set_features = ice_set_features,
8619 	.ndo_bridge_getlink = ice_bridge_getlink,
8620 	.ndo_bridge_setlink = ice_bridge_setlink,
8621 	.ndo_fdb_add = ice_fdb_add,
8622 	.ndo_fdb_del = ice_fdb_del,
8623 #ifdef CONFIG_RFS_ACCEL
8624 	.ndo_rx_flow_steer = ice_rx_flow_steer,
8625 #endif
8626 	.ndo_tx_timeout = ice_tx_timeout,
8627 	.ndo_bpf = ice_xdp,
8628 	.ndo_xdp_xmit = ice_xdp_xmit,
8629 	.ndo_xsk_wakeup = ice_xsk_wakeup,
8630 };
8631