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