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