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