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