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