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