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