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 
17 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
18 static const char ice_driver_string[] = DRV_SUMMARY;
19 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
20 
21 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
22 #define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
23 #define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
24 
25 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
26 MODULE_DESCRIPTION(DRV_SUMMARY);
27 MODULE_LICENSE("GPL v2");
28 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
29 
30 static int debug = -1;
31 module_param(debug, int, 0644);
32 #ifndef CONFIG_DYNAMIC_DEBUG
33 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
34 #else
35 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
36 #endif /* !CONFIG_DYNAMIC_DEBUG */
37 
38 static struct workqueue_struct *ice_wq;
39 static const struct net_device_ops ice_netdev_safe_mode_ops;
40 static const struct net_device_ops ice_netdev_ops;
41 static int ice_vsi_open(struct ice_vsi *vsi);
42 
43 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
44 
45 static void ice_vsi_release_all(struct ice_pf *pf);
46 
47 /**
48  * ice_get_tx_pending - returns number of Tx descriptors not processed
49  * @ring: the ring of descriptors
50  */
51 static u16 ice_get_tx_pending(struct ice_ring *ring)
52 {
53 	u16 head, tail;
54 
55 	head = ring->next_to_clean;
56 	tail = ring->next_to_use;
57 
58 	if (head != tail)
59 		return (head < tail) ?
60 			tail - head : (tail + ring->count - head);
61 	return 0;
62 }
63 
64 /**
65  * ice_check_for_hang_subtask - check for and recover hung queues
66  * @pf: pointer to PF struct
67  */
68 static void ice_check_for_hang_subtask(struct ice_pf *pf)
69 {
70 	struct ice_vsi *vsi = NULL;
71 	struct ice_hw *hw;
72 	unsigned int i;
73 	int packets;
74 	u32 v;
75 
76 	ice_for_each_vsi(pf, v)
77 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
78 			vsi = pf->vsi[v];
79 			break;
80 		}
81 
82 	if (!vsi || test_bit(__ICE_DOWN, vsi->state))
83 		return;
84 
85 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
86 		return;
87 
88 	hw = &vsi->back->hw;
89 
90 	for (i = 0; i < vsi->num_txq; i++) {
91 		struct ice_ring *tx_ring = vsi->tx_rings[i];
92 
93 		if (tx_ring && tx_ring->desc) {
94 			/* If packet counter has not changed the queue is
95 			 * likely stalled, so force an interrupt for this
96 			 * queue.
97 			 *
98 			 * prev_pkt would be negative if there was no
99 			 * pending work.
100 			 */
101 			packets = tx_ring->stats.pkts & INT_MAX;
102 			if (tx_ring->tx_stats.prev_pkt == packets) {
103 				/* Trigger sw interrupt to revive the queue */
104 				ice_trigger_sw_intr(hw, tx_ring->q_vector);
105 				continue;
106 			}
107 
108 			/* Memory barrier between read of packet count and call
109 			 * to ice_get_tx_pending()
110 			 */
111 			smp_rmb();
112 			tx_ring->tx_stats.prev_pkt =
113 			    ice_get_tx_pending(tx_ring) ? packets : -1;
114 		}
115 	}
116 }
117 
118 /**
119  * ice_init_mac_fltr - Set initial MAC filters
120  * @pf: board private structure
121  *
122  * Set initial set of MAC filters for PF VSI; configure filters for permanent
123  * address and broadcast address. If an error is encountered, netdevice will be
124  * unregistered.
125  */
126 static int ice_init_mac_fltr(struct ice_pf *pf)
127 {
128 	enum ice_status status;
129 	struct ice_vsi *vsi;
130 	u8 *perm_addr;
131 
132 	vsi = ice_get_main_vsi(pf);
133 	if (!vsi)
134 		return -EINVAL;
135 
136 	perm_addr = vsi->port_info->mac.perm_addr;
137 	status = ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
138 	if (!status)
139 		return 0;
140 
141 	/* We aren't useful with no MAC filters, so unregister if we
142 	 * had an error
143 	 */
144 	if (vsi->netdev->reg_state == NETREG_REGISTERED) {
145 		dev_err(ice_pf_to_dev(pf), "Could not add MAC filters error %s. Unregistering device\n",
146 			ice_stat_str(status));
147 		unregister_netdev(vsi->netdev);
148 		free_netdev(vsi->netdev);
149 		vsi->netdev = NULL;
150 	}
151 
152 	return -EIO;
153 }
154 
155 /**
156  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
157  * @netdev: the net device on which the sync is happening
158  * @addr: MAC address to sync
159  *
160  * This is a callback function which is called by the in kernel device sync
161  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
162  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
163  * MAC filters from the hardware.
164  */
165 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
166 {
167 	struct ice_netdev_priv *np = netdev_priv(netdev);
168 	struct ice_vsi *vsi = np->vsi;
169 
170 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
171 				     ICE_FWD_TO_VSI))
172 		return -EINVAL;
173 
174 	return 0;
175 }
176 
177 /**
178  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
179  * @netdev: the net device on which the unsync is happening
180  * @addr: MAC address to unsync
181  *
182  * This is a callback function which is called by the in kernel device unsync
183  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
184  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
185  * delete the MAC filters from the hardware.
186  */
187 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
188 {
189 	struct ice_netdev_priv *np = netdev_priv(netdev);
190 	struct ice_vsi *vsi = np->vsi;
191 
192 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
193 				     ICE_FWD_TO_VSI))
194 		return -EINVAL;
195 
196 	return 0;
197 }
198 
199 /**
200  * ice_vsi_fltr_changed - check if filter state changed
201  * @vsi: VSI to be checked
202  *
203  * returns true if filter state has changed, false otherwise.
204  */
205 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
206 {
207 	return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
208 	       test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
209 	       test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
210 }
211 
212 /**
213  * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
214  * @vsi: the VSI being configured
215  * @promisc_m: mask of promiscuous config bits
216  * @set_promisc: enable or disable promisc flag request
217  *
218  */
219 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
220 {
221 	struct ice_hw *hw = &vsi->back->hw;
222 	enum ice_status status = 0;
223 
224 	if (vsi->type != ICE_VSI_PF)
225 		return 0;
226 
227 	if (vsi->vlan_ena) {
228 		status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
229 						  set_promisc);
230 	} else {
231 		if (set_promisc)
232 			status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
233 						     0);
234 		else
235 			status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
236 						       0);
237 	}
238 
239 	if (status)
240 		return -EIO;
241 
242 	return 0;
243 }
244 
245 /**
246  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
247  * @vsi: ptr to the VSI
248  *
249  * Push any outstanding VSI filter changes through the AdminQ.
250  */
251 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
252 {
253 	struct device *dev = ice_pf_to_dev(vsi->back);
254 	struct net_device *netdev = vsi->netdev;
255 	bool promisc_forced_on = false;
256 	struct ice_pf *pf = vsi->back;
257 	struct ice_hw *hw = &pf->hw;
258 	enum ice_status status = 0;
259 	u32 changed_flags = 0;
260 	u8 promisc_m;
261 	int err = 0;
262 
263 	if (!vsi->netdev)
264 		return -EINVAL;
265 
266 	while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
267 		usleep_range(1000, 2000);
268 
269 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
270 	vsi->current_netdev_flags = vsi->netdev->flags;
271 
272 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
273 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
274 
275 	if (ice_vsi_fltr_changed(vsi)) {
276 		clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
277 		clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
278 		clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
279 
280 		/* grab the netdev's addr_list_lock */
281 		netif_addr_lock_bh(netdev);
282 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
283 			      ice_add_mac_to_unsync_list);
284 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
285 			      ice_add_mac_to_unsync_list);
286 		/* our temp lists are populated. release lock */
287 		netif_addr_unlock_bh(netdev);
288 	}
289 
290 	/* Remove MAC addresses in the unsync list */
291 	status = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
292 	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
293 	if (status) {
294 		netdev_err(netdev, "Failed to delete MAC filters\n");
295 		/* if we failed because of alloc failures, just bail */
296 		if (status == ICE_ERR_NO_MEMORY) {
297 			err = -ENOMEM;
298 			goto out;
299 		}
300 	}
301 
302 	/* Add MAC addresses in the sync list */
303 	status = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
304 	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
305 	/* If filter is added successfully or already exists, do not go into
306 	 * 'if' condition and report it as error. Instead continue processing
307 	 * rest of the function.
308 	 */
309 	if (status && status != ICE_ERR_ALREADY_EXISTS) {
310 		netdev_err(netdev, "Failed to add MAC filters\n");
311 		/* If there is no more space for new umac filters, VSI
312 		 * should go into promiscuous mode. There should be some
313 		 * space reserved for promiscuous filters.
314 		 */
315 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
316 		    !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
317 				      vsi->state)) {
318 			promisc_forced_on = true;
319 			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
320 				    vsi->vsi_num);
321 		} else {
322 			err = -EIO;
323 			goto out;
324 		}
325 	}
326 	/* check for changes in promiscuous modes */
327 	if (changed_flags & IFF_ALLMULTI) {
328 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
329 			if (vsi->vlan_ena)
330 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
331 			else
332 				promisc_m = ICE_MCAST_PROMISC_BITS;
333 
334 			err = ice_cfg_promisc(vsi, promisc_m, true);
335 			if (err) {
336 				netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
337 					   vsi->vsi_num);
338 				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
339 				goto out_promisc;
340 			}
341 		} else {
342 			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
343 			if (vsi->vlan_ena)
344 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
345 			else
346 				promisc_m = ICE_MCAST_PROMISC_BITS;
347 
348 			err = ice_cfg_promisc(vsi, promisc_m, false);
349 			if (err) {
350 				netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
351 					   vsi->vsi_num);
352 				vsi->current_netdev_flags |= IFF_ALLMULTI;
353 				goto out_promisc;
354 			}
355 		}
356 	}
357 
358 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
359 	    test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
360 		clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
361 		if (vsi->current_netdev_flags & IFF_PROMISC) {
362 			/* Apply Rx filter rule to get traffic from wire */
363 			if (!ice_is_dflt_vsi_in_use(pf->first_sw)) {
364 				err = ice_set_dflt_vsi(pf->first_sw, vsi);
365 				if (err && err != -EEXIST) {
366 					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
367 						   err, vsi->vsi_num);
368 					vsi->current_netdev_flags &=
369 						~IFF_PROMISC;
370 					goto out_promisc;
371 				}
372 				ice_cfg_vlan_pruning(vsi, false, false);
373 			}
374 		} else {
375 			/* Clear Rx filter to remove traffic from wire */
376 			if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) {
377 				err = ice_clear_dflt_vsi(pf->first_sw);
378 				if (err) {
379 					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
380 						   err, vsi->vsi_num);
381 					vsi->current_netdev_flags |=
382 						IFF_PROMISC;
383 					goto out_promisc;
384 				}
385 				if (vsi->num_vlan > 1)
386 					ice_cfg_vlan_pruning(vsi, true, false);
387 			}
388 		}
389 	}
390 	goto exit;
391 
392 out_promisc:
393 	set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
394 	goto exit;
395 out:
396 	/* if something went wrong then set the changed flag so we try again */
397 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
398 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
399 exit:
400 	clear_bit(__ICE_CFG_BUSY, vsi->state);
401 	return err;
402 }
403 
404 /**
405  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
406  * @pf: board private structure
407  */
408 static void ice_sync_fltr_subtask(struct ice_pf *pf)
409 {
410 	int v;
411 
412 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
413 		return;
414 
415 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
416 
417 	ice_for_each_vsi(pf, v)
418 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
419 		    ice_vsi_sync_fltr(pf->vsi[v])) {
420 			/* come back and try again later */
421 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
422 			break;
423 		}
424 }
425 
426 /**
427  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
428  * @pf: the PF
429  * @locked: is the rtnl_lock already held
430  */
431 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
432 {
433 	int v;
434 
435 	ice_for_each_vsi(pf, v)
436 		if (pf->vsi[v])
437 			ice_dis_vsi(pf->vsi[v], locked);
438 }
439 
440 /**
441  * ice_prepare_for_reset - prep for the core to reset
442  * @pf: board private structure
443  *
444  * Inform or close all dependent features in prep for reset.
445  */
446 static void
447 ice_prepare_for_reset(struct ice_pf *pf)
448 {
449 	struct ice_hw *hw = &pf->hw;
450 	unsigned int i;
451 
452 	/* already prepared for reset */
453 	if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
454 		return;
455 
456 	/* Notify VFs of impending reset */
457 	if (ice_check_sq_alive(hw, &hw->mailboxq))
458 		ice_vc_notify_reset(pf);
459 
460 	/* Disable VFs until reset is completed */
461 	ice_for_each_vf(pf, i)
462 		ice_set_vf_state_qs_dis(&pf->vf[i]);
463 
464 	/* clear SW filtering DB */
465 	ice_clear_hw_tbls(hw);
466 	/* disable the VSIs and their queues that are not already DOWN */
467 	ice_pf_dis_all_vsi(pf, false);
468 
469 	if (hw->port_info)
470 		ice_sched_clear_port(hw->port_info);
471 
472 	ice_shutdown_all_ctrlq(hw);
473 
474 	set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
475 }
476 
477 /**
478  * ice_do_reset - Initiate one of many types of resets
479  * @pf: board private structure
480  * @reset_type: reset type requested
481  * before this function was called.
482  */
483 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
484 {
485 	struct device *dev = ice_pf_to_dev(pf);
486 	struct ice_hw *hw = &pf->hw;
487 
488 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
489 
490 	ice_prepare_for_reset(pf);
491 
492 	/* trigger the reset */
493 	if (ice_reset(hw, reset_type)) {
494 		dev_err(dev, "reset %d failed\n", reset_type);
495 		set_bit(__ICE_RESET_FAILED, pf->state);
496 		clear_bit(__ICE_RESET_OICR_RECV, pf->state);
497 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
498 		clear_bit(__ICE_PFR_REQ, pf->state);
499 		clear_bit(__ICE_CORER_REQ, pf->state);
500 		clear_bit(__ICE_GLOBR_REQ, pf->state);
501 		return;
502 	}
503 
504 	/* PFR is a bit of a special case because it doesn't result in an OICR
505 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
506 	 * associated state bits.
507 	 */
508 	if (reset_type == ICE_RESET_PFR) {
509 		pf->pfr_count++;
510 		ice_rebuild(pf, reset_type);
511 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
512 		clear_bit(__ICE_PFR_REQ, pf->state);
513 		ice_reset_all_vfs(pf, true);
514 	}
515 }
516 
517 /**
518  * ice_reset_subtask - Set up for resetting the device and driver
519  * @pf: board private structure
520  */
521 static void ice_reset_subtask(struct ice_pf *pf)
522 {
523 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
524 
525 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
526 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
527 	 * of reset is pending and sets bits in pf->state indicating the reset
528 	 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
529 	 * prepare for pending reset if not already (for PF software-initiated
530 	 * global resets the software should already be prepared for it as
531 	 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
532 	 * by firmware or software on other PFs, that bit is not set so prepare
533 	 * for the reset now), poll for reset done, rebuild and return.
534 	 */
535 	if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
536 		/* Perform the largest reset requested */
537 		if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
538 			reset_type = ICE_RESET_CORER;
539 		if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
540 			reset_type = ICE_RESET_GLOBR;
541 		if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state))
542 			reset_type = ICE_RESET_EMPR;
543 		/* return if no valid reset type requested */
544 		if (reset_type == ICE_RESET_INVAL)
545 			return;
546 		ice_prepare_for_reset(pf);
547 
548 		/* make sure we are ready to rebuild */
549 		if (ice_check_reset(&pf->hw)) {
550 			set_bit(__ICE_RESET_FAILED, pf->state);
551 		} else {
552 			/* done with reset. start rebuild */
553 			pf->hw.reset_ongoing = false;
554 			ice_rebuild(pf, reset_type);
555 			/* clear bit to resume normal operations, but
556 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
557 			 */
558 			clear_bit(__ICE_RESET_OICR_RECV, pf->state);
559 			clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
560 			clear_bit(__ICE_PFR_REQ, pf->state);
561 			clear_bit(__ICE_CORER_REQ, pf->state);
562 			clear_bit(__ICE_GLOBR_REQ, pf->state);
563 			ice_reset_all_vfs(pf, true);
564 		}
565 
566 		return;
567 	}
568 
569 	/* No pending resets to finish processing. Check for new resets */
570 	if (test_bit(__ICE_PFR_REQ, pf->state))
571 		reset_type = ICE_RESET_PFR;
572 	if (test_bit(__ICE_CORER_REQ, pf->state))
573 		reset_type = ICE_RESET_CORER;
574 	if (test_bit(__ICE_GLOBR_REQ, pf->state))
575 		reset_type = ICE_RESET_GLOBR;
576 	/* If no valid reset type requested just return */
577 	if (reset_type == ICE_RESET_INVAL)
578 		return;
579 
580 	/* reset if not already down or busy */
581 	if (!test_bit(__ICE_DOWN, pf->state) &&
582 	    !test_bit(__ICE_CFG_BUSY, pf->state)) {
583 		ice_do_reset(pf, reset_type);
584 	}
585 }
586 
587 /**
588  * ice_print_topo_conflict - print topology conflict message
589  * @vsi: the VSI whose topology status is being checked
590  */
591 static void ice_print_topo_conflict(struct ice_vsi *vsi)
592 {
593 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
594 	case ICE_AQ_LINK_TOPO_CONFLICT:
595 	case ICE_AQ_LINK_MEDIA_CONFLICT:
596 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
597 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
598 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
599 		netdev_info(vsi->netdev, "Possible mis-configuration of the Ethernet port detected, please use the Intel(R) Ethernet Port Configuration Tool application to address the issue.\n");
600 		break;
601 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
602 		netdev_info(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");
603 		break;
604 	default:
605 		break;
606 	}
607 }
608 
609 /**
610  * ice_print_link_msg - print link up or down message
611  * @vsi: the VSI whose link status is being queried
612  * @isup: boolean for if the link is now up or down
613  */
614 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
615 {
616 	struct ice_aqc_get_phy_caps_data *caps;
617 	const char *an_advertised;
618 	enum ice_status status;
619 	const char *fec_req;
620 	const char *speed;
621 	const char *fec;
622 	const char *fc;
623 	const char *an;
624 
625 	if (!vsi)
626 		return;
627 
628 	if (vsi->current_isup == isup)
629 		return;
630 
631 	vsi->current_isup = isup;
632 
633 	if (!isup) {
634 		netdev_info(vsi->netdev, "NIC Link is Down\n");
635 		return;
636 	}
637 
638 	switch (vsi->port_info->phy.link_info.link_speed) {
639 	case ICE_AQ_LINK_SPEED_100GB:
640 		speed = "100 G";
641 		break;
642 	case ICE_AQ_LINK_SPEED_50GB:
643 		speed = "50 G";
644 		break;
645 	case ICE_AQ_LINK_SPEED_40GB:
646 		speed = "40 G";
647 		break;
648 	case ICE_AQ_LINK_SPEED_25GB:
649 		speed = "25 G";
650 		break;
651 	case ICE_AQ_LINK_SPEED_20GB:
652 		speed = "20 G";
653 		break;
654 	case ICE_AQ_LINK_SPEED_10GB:
655 		speed = "10 G";
656 		break;
657 	case ICE_AQ_LINK_SPEED_5GB:
658 		speed = "5 G";
659 		break;
660 	case ICE_AQ_LINK_SPEED_2500MB:
661 		speed = "2.5 G";
662 		break;
663 	case ICE_AQ_LINK_SPEED_1000MB:
664 		speed = "1 G";
665 		break;
666 	case ICE_AQ_LINK_SPEED_100MB:
667 		speed = "100 M";
668 		break;
669 	default:
670 		speed = "Unknown";
671 		break;
672 	}
673 
674 	switch (vsi->port_info->fc.current_mode) {
675 	case ICE_FC_FULL:
676 		fc = "Rx/Tx";
677 		break;
678 	case ICE_FC_TX_PAUSE:
679 		fc = "Tx";
680 		break;
681 	case ICE_FC_RX_PAUSE:
682 		fc = "Rx";
683 		break;
684 	case ICE_FC_NONE:
685 		fc = "None";
686 		break;
687 	default:
688 		fc = "Unknown";
689 		break;
690 	}
691 
692 	/* Get FEC mode based on negotiated link info */
693 	switch (vsi->port_info->phy.link_info.fec_info) {
694 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
695 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
696 		fec = "RS-FEC";
697 		break;
698 	case ICE_AQ_LINK_25G_KR_FEC_EN:
699 		fec = "FC-FEC/BASE-R";
700 		break;
701 	default:
702 		fec = "NONE";
703 		break;
704 	}
705 
706 	/* check if autoneg completed, might be false due to not supported */
707 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
708 		an = "True";
709 	else
710 		an = "False";
711 
712 	/* Get FEC mode requested based on PHY caps last SW configuration */
713 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
714 	if (!caps) {
715 		fec_req = "Unknown";
716 		an_advertised = "Unknown";
717 		goto done;
718 	}
719 
720 	status = ice_aq_get_phy_caps(vsi->port_info, false,
721 				     ICE_AQC_REPORT_SW_CFG, caps, NULL);
722 	if (status)
723 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
724 
725 	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
726 
727 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
728 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
729 		fec_req = "RS-FEC";
730 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
731 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
732 		fec_req = "FC-FEC/BASE-R";
733 	else
734 		fec_req = "NONE";
735 
736 	kfree(caps);
737 
738 done:
739 	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",
740 		    speed, fec_req, fec, an_advertised, an, fc);
741 	ice_print_topo_conflict(vsi);
742 }
743 
744 /**
745  * ice_vsi_link_event - update the VSI's netdev
746  * @vsi: the VSI on which the link event occurred
747  * @link_up: whether or not the VSI needs to be set up or down
748  */
749 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
750 {
751 	if (!vsi)
752 		return;
753 
754 	if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
755 		return;
756 
757 	if (vsi->type == ICE_VSI_PF) {
758 		if (link_up == netif_carrier_ok(vsi->netdev))
759 			return;
760 
761 		if (link_up) {
762 			netif_carrier_on(vsi->netdev);
763 			netif_tx_wake_all_queues(vsi->netdev);
764 		} else {
765 			netif_carrier_off(vsi->netdev);
766 			netif_tx_stop_all_queues(vsi->netdev);
767 		}
768 	}
769 }
770 
771 /**
772  * ice_set_dflt_mib - send a default config MIB to the FW
773  * @pf: private PF struct
774  *
775  * This function sends a default configuration MIB to the FW.
776  *
777  * If this function errors out at any point, the driver is still able to
778  * function.  The main impact is that LFC may not operate as expected.
779  * Therefore an error state in this function should be treated with a DBG
780  * message and continue on with driver rebuild/reenable.
781  */
782 static void ice_set_dflt_mib(struct ice_pf *pf)
783 {
784 	struct device *dev = ice_pf_to_dev(pf);
785 	u8 mib_type, *buf, *lldpmib = NULL;
786 	u16 len, typelen, offset = 0;
787 	struct ice_lldp_org_tlv *tlv;
788 	struct ice_hw *hw;
789 	u32 ouisubtype;
790 
791 	if (!pf) {
792 		dev_dbg(dev, "%s NULL pf pointer\n", __func__);
793 		return;
794 	}
795 
796 	hw = &pf->hw;
797 	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
798 	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
799 	if (!lldpmib) {
800 		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
801 			__func__);
802 		return;
803 	}
804 
805 	/* Add ETS CFG TLV */
806 	tlv = (struct ice_lldp_org_tlv *)lldpmib;
807 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
808 		   ICE_IEEE_ETS_TLV_LEN);
809 	tlv->typelen = htons(typelen);
810 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
811 		      ICE_IEEE_SUBTYPE_ETS_CFG);
812 	tlv->ouisubtype = htonl(ouisubtype);
813 
814 	buf = tlv->tlvinfo;
815 	buf[0] = 0;
816 
817 	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
818 	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
819 	 * Octets 13 - 20 are TSA values - leave as zeros
820 	 */
821 	buf[5] = 0x64;
822 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
823 	offset += len + 2;
824 	tlv = (struct ice_lldp_org_tlv *)
825 		((char *)tlv + sizeof(tlv->typelen) + len);
826 
827 	/* Add ETS REC TLV */
828 	buf = tlv->tlvinfo;
829 	tlv->typelen = htons(typelen);
830 
831 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
832 		      ICE_IEEE_SUBTYPE_ETS_REC);
833 	tlv->ouisubtype = htonl(ouisubtype);
834 
835 	/* First octet of buf is reserved
836 	 * Octets 1 - 4 map UP to TC - all UPs map to zero
837 	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
838 	 * Octets 13 - 20 are TSA value - leave as zeros
839 	 */
840 	buf[5] = 0x64;
841 	offset += len + 2;
842 	tlv = (struct ice_lldp_org_tlv *)
843 		((char *)tlv + sizeof(tlv->typelen) + len);
844 
845 	/* Add PFC CFG TLV */
846 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
847 		   ICE_IEEE_PFC_TLV_LEN);
848 	tlv->typelen = htons(typelen);
849 
850 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
851 		      ICE_IEEE_SUBTYPE_PFC_CFG);
852 	tlv->ouisubtype = htonl(ouisubtype);
853 
854 	/* Octet 1 left as all zeros - PFC disabled */
855 	buf[0] = 0x08;
856 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
857 	offset += len + 2;
858 
859 	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
860 		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
861 
862 	kfree(lldpmib);
863 }
864 
865 /**
866  * ice_link_event - process the link event
867  * @pf: PF that the link event is associated with
868  * @pi: port_info for the port that the link event is associated with
869  * @link_up: true if the physical link is up and false if it is down
870  * @link_speed: current link speed received from the link event
871  *
872  * Returns 0 on success and negative on failure
873  */
874 static int
875 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
876 	       u16 link_speed)
877 {
878 	struct device *dev = ice_pf_to_dev(pf);
879 	struct ice_phy_info *phy_info;
880 	struct ice_vsi *vsi;
881 	u16 old_link_speed;
882 	bool old_link;
883 	int result;
884 
885 	phy_info = &pi->phy;
886 	phy_info->link_info_old = phy_info->link_info;
887 
888 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
889 	old_link_speed = phy_info->link_info_old.link_speed;
890 
891 	/* update the link info structures and re-enable link events,
892 	 * don't bail on failure due to other book keeping needed
893 	 */
894 	result = ice_update_link_info(pi);
895 	if (result)
896 		dev_dbg(dev, "Failed to update link status and re-enable link events for port %d\n",
897 			pi->lport);
898 
899 	/* Check if the link state is up after updating link info, and treat
900 	 * this event as an UP event since the link is actually UP now.
901 	 */
902 	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
903 		link_up = true;
904 
905 	vsi = ice_get_main_vsi(pf);
906 	if (!vsi || !vsi->port_info)
907 		return -EINVAL;
908 
909 	/* turn off PHY if media was removed */
910 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
911 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
912 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
913 
914 		result = ice_aq_set_link_restart_an(pi, false, NULL);
915 		if (result) {
916 			dev_dbg(dev, "Failed to set link down, VSI %d error %d\n",
917 				vsi->vsi_num, result);
918 			return result;
919 		}
920 	}
921 
922 	/* if the old link up/down and speed is the same as the new */
923 	if (link_up == old_link && link_speed == old_link_speed)
924 		return result;
925 
926 	if (ice_is_dcb_active(pf)) {
927 		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
928 			ice_dcb_rebuild(pf);
929 	} else {
930 		if (link_up)
931 			ice_set_dflt_mib(pf);
932 	}
933 	ice_vsi_link_event(vsi, link_up);
934 	ice_print_link_msg(vsi, link_up);
935 
936 	ice_vc_notify_link_state(pf);
937 
938 	return result;
939 }
940 
941 /**
942  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
943  * @pf: board private structure
944  */
945 static void ice_watchdog_subtask(struct ice_pf *pf)
946 {
947 	int i;
948 
949 	/* if interface is down do nothing */
950 	if (test_bit(__ICE_DOWN, pf->state) ||
951 	    test_bit(__ICE_CFG_BUSY, pf->state))
952 		return;
953 
954 	/* make sure we don't do these things too often */
955 	if (time_before(jiffies,
956 			pf->serv_tmr_prev + pf->serv_tmr_period))
957 		return;
958 
959 	pf->serv_tmr_prev = jiffies;
960 
961 	/* Update the stats for active netdevs so the network stack
962 	 * can look at updated numbers whenever it cares to
963 	 */
964 	ice_update_pf_stats(pf);
965 	ice_for_each_vsi(pf, i)
966 		if (pf->vsi[i] && pf->vsi[i]->netdev)
967 			ice_update_vsi_stats(pf->vsi[i]);
968 }
969 
970 /**
971  * ice_init_link_events - enable/initialize link events
972  * @pi: pointer to the port_info instance
973  *
974  * Returns -EIO on failure, 0 on success
975  */
976 static int ice_init_link_events(struct ice_port_info *pi)
977 {
978 	u16 mask;
979 
980 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
981 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
982 
983 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
984 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
985 			pi->lport);
986 		return -EIO;
987 	}
988 
989 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
990 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
991 			pi->lport);
992 		return -EIO;
993 	}
994 
995 	return 0;
996 }
997 
998 /**
999  * ice_handle_link_event - handle link event via ARQ
1000  * @pf: PF that the link event is associated with
1001  * @event: event structure containing link status info
1002  */
1003 static int
1004 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1005 {
1006 	struct ice_aqc_get_link_status_data *link_data;
1007 	struct ice_port_info *port_info;
1008 	int status;
1009 
1010 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1011 	port_info = pf->hw.port_info;
1012 	if (!port_info)
1013 		return -EINVAL;
1014 
1015 	status = ice_link_event(pf, port_info,
1016 				!!(link_data->link_info & ICE_AQ_LINK_UP),
1017 				le16_to_cpu(link_data->link_speed));
1018 	if (status)
1019 		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1020 			status);
1021 
1022 	return status;
1023 }
1024 
1025 enum ice_aq_task_state {
1026 	ICE_AQ_TASK_WAITING = 0,
1027 	ICE_AQ_TASK_COMPLETE,
1028 	ICE_AQ_TASK_CANCELED,
1029 };
1030 
1031 struct ice_aq_task {
1032 	struct hlist_node entry;
1033 
1034 	u16 opcode;
1035 	struct ice_rq_event_info *event;
1036 	enum ice_aq_task_state state;
1037 };
1038 
1039 /**
1040  * ice_wait_for_aq_event - Wait for an AdminQ event from firmware
1041  * @pf: pointer to the PF private structure
1042  * @opcode: the opcode to wait for
1043  * @timeout: how long to wait, in jiffies
1044  * @event: storage for the event info
1045  *
1046  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1047  * current thread will be put to sleep until the specified event occurs or
1048  * until the given timeout is reached.
1049  *
1050  * To obtain only the descriptor contents, pass an event without an allocated
1051  * msg_buf. If the complete data buffer is desired, allocate the
1052  * event->msg_buf with enough space ahead of time.
1053  *
1054  * Returns: zero on success, or a negative error code on failure.
1055  */
1056 int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1057 			  struct ice_rq_event_info *event)
1058 {
1059 	struct ice_aq_task *task;
1060 	long ret;
1061 	int err;
1062 
1063 	task = kzalloc(sizeof(*task), GFP_KERNEL);
1064 	if (!task)
1065 		return -ENOMEM;
1066 
1067 	INIT_HLIST_NODE(&task->entry);
1068 	task->opcode = opcode;
1069 	task->event = event;
1070 	task->state = ICE_AQ_TASK_WAITING;
1071 
1072 	spin_lock_bh(&pf->aq_wait_lock);
1073 	hlist_add_head(&task->entry, &pf->aq_wait_list);
1074 	spin_unlock_bh(&pf->aq_wait_lock);
1075 
1076 	ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1077 					       timeout);
1078 	switch (task->state) {
1079 	case ICE_AQ_TASK_WAITING:
1080 		err = ret < 0 ? ret : -ETIMEDOUT;
1081 		break;
1082 	case ICE_AQ_TASK_CANCELED:
1083 		err = ret < 0 ? ret : -ECANCELED;
1084 		break;
1085 	case ICE_AQ_TASK_COMPLETE:
1086 		err = ret < 0 ? ret : 0;
1087 		break;
1088 	default:
1089 		WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1090 		err = -EINVAL;
1091 		break;
1092 	}
1093 
1094 	spin_lock_bh(&pf->aq_wait_lock);
1095 	hlist_del(&task->entry);
1096 	spin_unlock_bh(&pf->aq_wait_lock);
1097 	kfree(task);
1098 
1099 	return err;
1100 }
1101 
1102 /**
1103  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1104  * @pf: pointer to the PF private structure
1105  * @opcode: the opcode of the event
1106  * @event: the event to check
1107  *
1108  * Loops over the current list of pending threads waiting for an AdminQ event.
1109  * For each matching task, copy the contents of the event into the task
1110  * structure and wake up the thread.
1111  *
1112  * If multiple threads wait for the same opcode, they will all be woken up.
1113  *
1114  * Note that event->msg_buf will only be duplicated if the event has a buffer
1115  * with enough space already allocated. Otherwise, only the descriptor and
1116  * message length will be copied.
1117  *
1118  * Returns: true if an event was found, false otherwise
1119  */
1120 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1121 				struct ice_rq_event_info *event)
1122 {
1123 	struct ice_aq_task *task;
1124 	bool found = false;
1125 
1126 	spin_lock_bh(&pf->aq_wait_lock);
1127 	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1128 		if (task->state || task->opcode != opcode)
1129 			continue;
1130 
1131 		memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1132 		task->event->msg_len = event->msg_len;
1133 
1134 		/* Only copy the data buffer if a destination was set */
1135 		if (task->event->msg_buf &&
1136 		    task->event->buf_len > event->buf_len) {
1137 			memcpy(task->event->msg_buf, event->msg_buf,
1138 			       event->buf_len);
1139 			task->event->buf_len = event->buf_len;
1140 		}
1141 
1142 		task->state = ICE_AQ_TASK_COMPLETE;
1143 		found = true;
1144 	}
1145 	spin_unlock_bh(&pf->aq_wait_lock);
1146 
1147 	if (found)
1148 		wake_up(&pf->aq_wait_queue);
1149 }
1150 
1151 /**
1152  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1153  * @pf: the PF private structure
1154  *
1155  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1156  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1157  */
1158 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1159 {
1160 	struct ice_aq_task *task;
1161 
1162 	spin_lock_bh(&pf->aq_wait_lock);
1163 	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1164 		task->state = ICE_AQ_TASK_CANCELED;
1165 	spin_unlock_bh(&pf->aq_wait_lock);
1166 
1167 	wake_up(&pf->aq_wait_queue);
1168 }
1169 
1170 /**
1171  * __ice_clean_ctrlq - helper function to clean controlq rings
1172  * @pf: ptr to struct ice_pf
1173  * @q_type: specific Control queue type
1174  */
1175 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1176 {
1177 	struct device *dev = ice_pf_to_dev(pf);
1178 	struct ice_rq_event_info event;
1179 	struct ice_hw *hw = &pf->hw;
1180 	struct ice_ctl_q_info *cq;
1181 	u16 pending, i = 0;
1182 	const char *qtype;
1183 	u32 oldval, val;
1184 
1185 	/* Do not clean control queue if/when PF reset fails */
1186 	if (test_bit(__ICE_RESET_FAILED, pf->state))
1187 		return 0;
1188 
1189 	switch (q_type) {
1190 	case ICE_CTL_Q_ADMIN:
1191 		cq = &hw->adminq;
1192 		qtype = "Admin";
1193 		break;
1194 	case ICE_CTL_Q_MAILBOX:
1195 		cq = &hw->mailboxq;
1196 		qtype = "Mailbox";
1197 		break;
1198 	default:
1199 		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1200 		return 0;
1201 	}
1202 
1203 	/* check for error indications - PF_xx_AxQLEN register layout for
1204 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1205 	 */
1206 	val = rd32(hw, cq->rq.len);
1207 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1208 		   PF_FW_ARQLEN_ARQCRIT_M)) {
1209 		oldval = val;
1210 		if (val & PF_FW_ARQLEN_ARQVFE_M)
1211 			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1212 				qtype);
1213 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1214 			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1215 				qtype);
1216 		}
1217 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1218 			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1219 				qtype);
1220 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1221 			 PF_FW_ARQLEN_ARQCRIT_M);
1222 		if (oldval != val)
1223 			wr32(hw, cq->rq.len, val);
1224 	}
1225 
1226 	val = rd32(hw, cq->sq.len);
1227 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1228 		   PF_FW_ATQLEN_ATQCRIT_M)) {
1229 		oldval = val;
1230 		if (val & PF_FW_ATQLEN_ATQVFE_M)
1231 			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1232 				qtype);
1233 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1234 			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1235 				qtype);
1236 		}
1237 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1238 			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1239 				qtype);
1240 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1241 			 PF_FW_ATQLEN_ATQCRIT_M);
1242 		if (oldval != val)
1243 			wr32(hw, cq->sq.len, val);
1244 	}
1245 
1246 	event.buf_len = cq->rq_buf_size;
1247 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1248 	if (!event.msg_buf)
1249 		return 0;
1250 
1251 	do {
1252 		enum ice_status ret;
1253 		u16 opcode;
1254 
1255 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1256 		if (ret == ICE_ERR_AQ_NO_WORK)
1257 			break;
1258 		if (ret) {
1259 			dev_err(dev, "%s Receive Queue event error %s\n", qtype,
1260 				ice_stat_str(ret));
1261 			break;
1262 		}
1263 
1264 		opcode = le16_to_cpu(event.desc.opcode);
1265 
1266 		/* Notify any thread that might be waiting for this event */
1267 		ice_aq_check_events(pf, opcode, &event);
1268 
1269 		switch (opcode) {
1270 		case ice_aqc_opc_get_link_status:
1271 			if (ice_handle_link_event(pf, &event))
1272 				dev_err(dev, "Could not handle link event\n");
1273 			break;
1274 		case ice_aqc_opc_event_lan_overflow:
1275 			ice_vf_lan_overflow_event(pf, &event);
1276 			break;
1277 		case ice_mbx_opc_send_msg_to_pf:
1278 			ice_vc_process_vf_msg(pf, &event);
1279 			break;
1280 		case ice_aqc_opc_fw_logging:
1281 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1282 			break;
1283 		case ice_aqc_opc_lldp_set_mib_change:
1284 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1285 			break;
1286 		default:
1287 			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1288 				qtype, opcode);
1289 			break;
1290 		}
1291 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1292 
1293 	kfree(event.msg_buf);
1294 
1295 	return pending && (i == ICE_DFLT_IRQ_WORK);
1296 }
1297 
1298 /**
1299  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1300  * @hw: pointer to hardware info
1301  * @cq: control queue information
1302  *
1303  * returns true if there are pending messages in a queue, false if there aren't
1304  */
1305 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1306 {
1307 	u16 ntu;
1308 
1309 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1310 	return cq->rq.next_to_clean != ntu;
1311 }
1312 
1313 /**
1314  * ice_clean_adminq_subtask - clean the AdminQ rings
1315  * @pf: board private structure
1316  */
1317 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1318 {
1319 	struct ice_hw *hw = &pf->hw;
1320 
1321 	if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1322 		return;
1323 
1324 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1325 		return;
1326 
1327 	clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1328 
1329 	/* There might be a situation where new messages arrive to a control
1330 	 * queue between processing the last message and clearing the
1331 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1332 	 * ice_ctrlq_pending) and process new messages if any.
1333 	 */
1334 	if (ice_ctrlq_pending(hw, &hw->adminq))
1335 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1336 
1337 	ice_flush(hw);
1338 }
1339 
1340 /**
1341  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1342  * @pf: board private structure
1343  */
1344 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1345 {
1346 	struct ice_hw *hw = &pf->hw;
1347 
1348 	if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1349 		return;
1350 
1351 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1352 		return;
1353 
1354 	clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1355 
1356 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1357 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1358 
1359 	ice_flush(hw);
1360 }
1361 
1362 /**
1363  * ice_service_task_schedule - schedule the service task to wake up
1364  * @pf: board private structure
1365  *
1366  * If not already scheduled, this puts the task into the work queue.
1367  */
1368 void ice_service_task_schedule(struct ice_pf *pf)
1369 {
1370 	if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1371 	    !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1372 	    !test_bit(__ICE_NEEDS_RESTART, pf->state))
1373 		queue_work(ice_wq, &pf->serv_task);
1374 }
1375 
1376 /**
1377  * ice_service_task_complete - finish up the service task
1378  * @pf: board private structure
1379  */
1380 static void ice_service_task_complete(struct ice_pf *pf)
1381 {
1382 	WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1383 
1384 	/* force memory (pf->state) to sync before next service task */
1385 	smp_mb__before_atomic();
1386 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1387 }
1388 
1389 /**
1390  * ice_service_task_stop - stop service task and cancel works
1391  * @pf: board private structure
1392  *
1393  * Return 0 if the __ICE_SERVICE_DIS bit was not already set,
1394  * 1 otherwise.
1395  */
1396 static int ice_service_task_stop(struct ice_pf *pf)
1397 {
1398 	int ret;
1399 
1400 	ret = test_and_set_bit(__ICE_SERVICE_DIS, pf->state);
1401 
1402 	if (pf->serv_tmr.function)
1403 		del_timer_sync(&pf->serv_tmr);
1404 	if (pf->serv_task.func)
1405 		cancel_work_sync(&pf->serv_task);
1406 
1407 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1408 	return ret;
1409 }
1410 
1411 /**
1412  * ice_service_task_restart - restart service task and schedule works
1413  * @pf: board private structure
1414  *
1415  * This function is needed for suspend and resume works (e.g WoL scenario)
1416  */
1417 static void ice_service_task_restart(struct ice_pf *pf)
1418 {
1419 	clear_bit(__ICE_SERVICE_DIS, pf->state);
1420 	ice_service_task_schedule(pf);
1421 }
1422 
1423 /**
1424  * ice_service_timer - timer callback to schedule service task
1425  * @t: pointer to timer_list
1426  */
1427 static void ice_service_timer(struct timer_list *t)
1428 {
1429 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1430 
1431 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1432 	ice_service_task_schedule(pf);
1433 }
1434 
1435 /**
1436  * ice_handle_mdd_event - handle malicious driver detect event
1437  * @pf: pointer to the PF structure
1438  *
1439  * Called from service task. OICR interrupt handler indicates MDD event.
1440  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1441  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1442  * disable the queue, the PF can be configured to reset the VF using ethtool
1443  * private flag mdd-auto-reset-vf.
1444  */
1445 static void ice_handle_mdd_event(struct ice_pf *pf)
1446 {
1447 	struct device *dev = ice_pf_to_dev(pf);
1448 	struct ice_hw *hw = &pf->hw;
1449 	unsigned int i;
1450 	u32 reg;
1451 
1452 	if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state)) {
1453 		/* Since the VF MDD event logging is rate limited, check if
1454 		 * there are pending MDD events.
1455 		 */
1456 		ice_print_vfs_mdd_events(pf);
1457 		return;
1458 	}
1459 
1460 	/* find what triggered an MDD event */
1461 	reg = rd32(hw, GL_MDET_TX_PQM);
1462 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1463 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1464 				GL_MDET_TX_PQM_PF_NUM_S;
1465 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1466 				GL_MDET_TX_PQM_VF_NUM_S;
1467 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1468 				GL_MDET_TX_PQM_MAL_TYPE_S;
1469 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1470 				GL_MDET_TX_PQM_QNUM_S);
1471 
1472 		if (netif_msg_tx_err(pf))
1473 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1474 				 event, queue, pf_num, vf_num);
1475 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1476 	}
1477 
1478 	reg = rd32(hw, GL_MDET_TX_TCLAN);
1479 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1480 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1481 				GL_MDET_TX_TCLAN_PF_NUM_S;
1482 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1483 				GL_MDET_TX_TCLAN_VF_NUM_S;
1484 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1485 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1486 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1487 				GL_MDET_TX_TCLAN_QNUM_S);
1488 
1489 		if (netif_msg_tx_err(pf))
1490 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1491 				 event, queue, pf_num, vf_num);
1492 		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1493 	}
1494 
1495 	reg = rd32(hw, GL_MDET_RX);
1496 	if (reg & GL_MDET_RX_VALID_M) {
1497 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1498 				GL_MDET_RX_PF_NUM_S;
1499 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1500 				GL_MDET_RX_VF_NUM_S;
1501 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1502 				GL_MDET_RX_MAL_TYPE_S;
1503 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1504 				GL_MDET_RX_QNUM_S);
1505 
1506 		if (netif_msg_rx_err(pf))
1507 			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1508 				 event, queue, pf_num, vf_num);
1509 		wr32(hw, GL_MDET_RX, 0xffffffff);
1510 	}
1511 
1512 	/* check to see if this PF caused an MDD event */
1513 	reg = rd32(hw, PF_MDET_TX_PQM);
1514 	if (reg & PF_MDET_TX_PQM_VALID_M) {
1515 		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1516 		if (netif_msg_tx_err(pf))
1517 			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1518 	}
1519 
1520 	reg = rd32(hw, PF_MDET_TX_TCLAN);
1521 	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1522 		wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1523 		if (netif_msg_tx_err(pf))
1524 			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1525 	}
1526 
1527 	reg = rd32(hw, PF_MDET_RX);
1528 	if (reg & PF_MDET_RX_VALID_M) {
1529 		wr32(hw, PF_MDET_RX, 0xFFFF);
1530 		if (netif_msg_rx_err(pf))
1531 			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1532 	}
1533 
1534 	/* Check to see if one of the VFs caused an MDD event, and then
1535 	 * increment counters and set print pending
1536 	 */
1537 	ice_for_each_vf(pf, i) {
1538 		struct ice_vf *vf = &pf->vf[i];
1539 
1540 		reg = rd32(hw, VP_MDET_TX_PQM(i));
1541 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1542 			wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1543 			vf->mdd_tx_events.count++;
1544 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1545 			if (netif_msg_tx_err(pf))
1546 				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1547 					 i);
1548 		}
1549 
1550 		reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1551 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1552 			wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1553 			vf->mdd_tx_events.count++;
1554 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1555 			if (netif_msg_tx_err(pf))
1556 				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1557 					 i);
1558 		}
1559 
1560 		reg = rd32(hw, VP_MDET_TX_TDPU(i));
1561 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1562 			wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1563 			vf->mdd_tx_events.count++;
1564 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1565 			if (netif_msg_tx_err(pf))
1566 				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1567 					 i);
1568 		}
1569 
1570 		reg = rd32(hw, VP_MDET_RX(i));
1571 		if (reg & VP_MDET_RX_VALID_M) {
1572 			wr32(hw, VP_MDET_RX(i), 0xFFFF);
1573 			vf->mdd_rx_events.count++;
1574 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1575 			if (netif_msg_rx_err(pf))
1576 				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1577 					 i);
1578 
1579 			/* Since the queue is disabled on VF Rx MDD events, the
1580 			 * PF can be configured to reset the VF through ethtool
1581 			 * private flag mdd-auto-reset-vf.
1582 			 */
1583 			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1584 				/* VF MDD event counters will be cleared by
1585 				 * reset, so print the event prior to reset.
1586 				 */
1587 				ice_print_vf_rx_mdd_event(vf);
1588 				ice_reset_vf(&pf->vf[i], false);
1589 			}
1590 		}
1591 	}
1592 
1593 	ice_print_vfs_mdd_events(pf);
1594 }
1595 
1596 /**
1597  * ice_force_phys_link_state - Force the physical link state
1598  * @vsi: VSI to force the physical link state to up/down
1599  * @link_up: true/false indicates to set the physical link to up/down
1600  *
1601  * Force the physical link state by getting the current PHY capabilities from
1602  * hardware and setting the PHY config based on the determined capabilities. If
1603  * link changes a link event will be triggered because both the Enable Automatic
1604  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1605  *
1606  * Returns 0 on success, negative on failure
1607  */
1608 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1609 {
1610 	struct ice_aqc_get_phy_caps_data *pcaps;
1611 	struct ice_aqc_set_phy_cfg_data *cfg;
1612 	struct ice_port_info *pi;
1613 	struct device *dev;
1614 	int retcode;
1615 
1616 	if (!vsi || !vsi->port_info || !vsi->back)
1617 		return -EINVAL;
1618 	if (vsi->type != ICE_VSI_PF)
1619 		return 0;
1620 
1621 	dev = ice_pf_to_dev(vsi->back);
1622 
1623 	pi = vsi->port_info;
1624 
1625 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1626 	if (!pcaps)
1627 		return -ENOMEM;
1628 
1629 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1630 				      NULL);
1631 	if (retcode) {
1632 		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1633 			vsi->vsi_num, retcode);
1634 		retcode = -EIO;
1635 		goto out;
1636 	}
1637 
1638 	/* No change in link */
1639 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1640 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1641 		goto out;
1642 
1643 	/* Use the current user PHY configuration. The current user PHY
1644 	 * configuration is initialized during probe from PHY capabilities
1645 	 * software mode, and updated on set PHY configuration.
1646 	 */
1647 	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1648 	if (!cfg) {
1649 		retcode = -ENOMEM;
1650 		goto out;
1651 	}
1652 
1653 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1654 	if (link_up)
1655 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1656 	else
1657 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1658 
1659 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1660 	if (retcode) {
1661 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1662 			vsi->vsi_num, retcode);
1663 		retcode = -EIO;
1664 	}
1665 
1666 	kfree(cfg);
1667 out:
1668 	kfree(pcaps);
1669 	return retcode;
1670 }
1671 
1672 /**
1673  * ice_init_nvm_phy_type - Initialize the NVM PHY type
1674  * @pi: port info structure
1675  *
1676  * Initialize nvm_phy_type_[low|high] for link lenient mode support
1677  */
1678 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1679 {
1680 	struct ice_aqc_get_phy_caps_data *pcaps;
1681 	struct ice_pf *pf = pi->hw->back;
1682 	enum ice_status status;
1683 	int err = 0;
1684 
1685 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1686 	if (!pcaps)
1687 		return -ENOMEM;
1688 
1689 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_NVM_CAP, pcaps,
1690 				     NULL);
1691 
1692 	if (status) {
1693 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1694 		err = -EIO;
1695 		goto out;
1696 	}
1697 
1698 	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1699 	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1700 
1701 out:
1702 	kfree(pcaps);
1703 	return err;
1704 }
1705 
1706 /**
1707  * ice_init_link_dflt_override - Initialize link default override
1708  * @pi: port info structure
1709  *
1710  * Initialize link default override and PHY total port shutdown during probe
1711  */
1712 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1713 {
1714 	struct ice_link_default_override_tlv *ldo;
1715 	struct ice_pf *pf = pi->hw->back;
1716 
1717 	ldo = &pf->link_dflt_override;
1718 	if (ice_get_link_default_override(ldo, pi))
1719 		return;
1720 
1721 	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1722 		return;
1723 
1724 	/* Enable Total Port Shutdown (override/replace link-down-on-close
1725 	 * ethtool private flag) for ports with Port Disable bit set.
1726 	 */
1727 	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1728 	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1729 }
1730 
1731 /**
1732  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1733  * @pi: port info structure
1734  *
1735  * If default override is enabled, initialized the user PHY cfg speed and FEC
1736  * settings using the default override mask from the NVM.
1737  *
1738  * The PHY should only be configured with the default override settings the
1739  * first time media is available. The __ICE_LINK_DEFAULT_OVERRIDE_PENDING state
1740  * is used to indicate that the user PHY cfg default override is initialized
1741  * and the PHY has not been configured with the default override settings. The
1742  * state is set here, and cleared in ice_configure_phy the first time the PHY is
1743  * configured.
1744  */
1745 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
1746 {
1747 	struct ice_link_default_override_tlv *ldo;
1748 	struct ice_aqc_set_phy_cfg_data *cfg;
1749 	struct ice_phy_info *phy = &pi->phy;
1750 	struct ice_pf *pf = pi->hw->back;
1751 
1752 	ldo = &pf->link_dflt_override;
1753 
1754 	/* If link default override is enabled, use to mask NVM PHY capabilities
1755 	 * for speed and FEC default configuration.
1756 	 */
1757 	cfg = &phy->curr_user_phy_cfg;
1758 
1759 	if (ldo->phy_type_low || ldo->phy_type_high) {
1760 		cfg->phy_type_low = pf->nvm_phy_type_lo &
1761 				    cpu_to_le64(ldo->phy_type_low);
1762 		cfg->phy_type_high = pf->nvm_phy_type_hi &
1763 				     cpu_to_le64(ldo->phy_type_high);
1764 	}
1765 	cfg->link_fec_opt = ldo->fec_options;
1766 	phy->curr_user_fec_req = ICE_FEC_AUTO;
1767 
1768 	set_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
1769 }
1770 
1771 /**
1772  * ice_init_phy_user_cfg - Initialize the PHY user configuration
1773  * @pi: port info structure
1774  *
1775  * Initialize the current user PHY configuration, speed, FEC, and FC requested
1776  * mode to default. The PHY defaults are from get PHY capabilities topology
1777  * with media so call when media is first available. An error is returned if
1778  * called when media is not available. The PHY initialization completed state is
1779  * set here.
1780  *
1781  * These configurations are used when setting PHY
1782  * configuration. The user PHY configuration is updated on set PHY
1783  * configuration. Returns 0 on success, negative on failure
1784  */
1785 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
1786 {
1787 	struct ice_aqc_get_phy_caps_data *pcaps;
1788 	struct ice_phy_info *phy = &pi->phy;
1789 	struct ice_pf *pf = pi->hw->back;
1790 	enum ice_status status;
1791 	struct ice_vsi *vsi;
1792 	int err = 0;
1793 
1794 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1795 		return -EIO;
1796 
1797 	vsi = ice_get_main_vsi(pf);
1798 	if (!vsi)
1799 		return -EINVAL;
1800 
1801 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1802 	if (!pcaps)
1803 		return -ENOMEM;
1804 
1805 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1806 				     NULL);
1807 	if (status) {
1808 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1809 		err = -EIO;
1810 		goto err_out;
1811 	}
1812 
1813 	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
1814 
1815 	/* check if lenient mode is supported and enabled */
1816 	if (ice_fw_supports_link_override(&vsi->back->hw) &&
1817 	    !(pcaps->module_compliance_enforcement &
1818 	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
1819 		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
1820 
1821 		/* if link default override is enabled, initialize user PHY
1822 		 * configuration with link default override values
1823 		 */
1824 		if (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN) {
1825 			ice_init_phy_cfg_dflt_override(pi);
1826 			goto out;
1827 		}
1828 	}
1829 
1830 	/* if link default override is not enabled, initialize PHY using
1831 	 * topology with media
1832 	 */
1833 	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
1834 						      pcaps->link_fec_options);
1835 	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
1836 
1837 out:
1838 	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
1839 	set_bit(__ICE_PHY_INIT_COMPLETE, pf->state);
1840 err_out:
1841 	kfree(pcaps);
1842 	return err;
1843 }
1844 
1845 /**
1846  * ice_configure_phy - configure PHY
1847  * @vsi: VSI of PHY
1848  *
1849  * Set the PHY configuration. If the current PHY configuration is the same as
1850  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
1851  * configure the based get PHY capabilities for topology with media.
1852  */
1853 static int ice_configure_phy(struct ice_vsi *vsi)
1854 {
1855 	struct device *dev = ice_pf_to_dev(vsi->back);
1856 	struct ice_aqc_get_phy_caps_data *pcaps;
1857 	struct ice_aqc_set_phy_cfg_data *cfg;
1858 	struct ice_port_info *pi;
1859 	enum ice_status status;
1860 	int err = 0;
1861 
1862 	pi = vsi->port_info;
1863 	if (!pi)
1864 		return -EINVAL;
1865 
1866 	/* Ensure we have media as we cannot configure a medialess port */
1867 	if (!(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1868 		return -EPERM;
1869 
1870 	ice_print_topo_conflict(vsi);
1871 
1872 	if (vsi->port_info->phy.link_info.topo_media_conflict ==
1873 	    ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
1874 		return -EPERM;
1875 
1876 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
1877 		return ice_force_phys_link_state(vsi, true);
1878 
1879 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1880 	if (!pcaps)
1881 		return -ENOMEM;
1882 
1883 	/* Get current PHY config */
1884 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1885 				     NULL);
1886 	if (status) {
1887 		dev_err(dev, "Failed to get PHY configuration, VSI %d error %s\n",
1888 			vsi->vsi_num, ice_stat_str(status));
1889 		err = -EIO;
1890 		goto done;
1891 	}
1892 
1893 	/* If PHY enable link is configured and configuration has not changed,
1894 	 * there's nothing to do
1895 	 */
1896 	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
1897 	    ice_phy_caps_equals_cfg(pcaps, &pi->phy.curr_user_phy_cfg))
1898 		goto done;
1899 
1900 	/* Use PHY topology as baseline for configuration */
1901 	memset(pcaps, 0, sizeof(*pcaps));
1902 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1903 				     NULL);
1904 	if (status) {
1905 		dev_err(dev, "Failed to get PHY topology, VSI %d error %s\n",
1906 			vsi->vsi_num, ice_stat_str(status));
1907 		err = -EIO;
1908 		goto done;
1909 	}
1910 
1911 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1912 	if (!cfg) {
1913 		err = -ENOMEM;
1914 		goto done;
1915 	}
1916 
1917 	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
1918 
1919 	/* Speed - If default override pending, use curr_user_phy_cfg set in
1920 	 * ice_init_phy_user_cfg_ldo.
1921 	 */
1922 	if (test_and_clear_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING,
1923 			       vsi->back->state)) {
1924 		cfg->phy_type_low = pi->phy.curr_user_phy_cfg.phy_type_low;
1925 		cfg->phy_type_high = pi->phy.curr_user_phy_cfg.phy_type_high;
1926 	} else {
1927 		u64 phy_low = 0, phy_high = 0;
1928 
1929 		ice_update_phy_type(&phy_low, &phy_high,
1930 				    pi->phy.curr_user_speed_req);
1931 		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
1932 		cfg->phy_type_high = pcaps->phy_type_high &
1933 				     cpu_to_le64(phy_high);
1934 	}
1935 
1936 	/* Can't provide what was requested; use PHY capabilities */
1937 	if (!cfg->phy_type_low && !cfg->phy_type_high) {
1938 		cfg->phy_type_low = pcaps->phy_type_low;
1939 		cfg->phy_type_high = pcaps->phy_type_high;
1940 	}
1941 
1942 	/* FEC */
1943 	ice_cfg_phy_fec(pi, cfg, pi->phy.curr_user_fec_req);
1944 
1945 	/* Can't provide what was requested; use PHY capabilities */
1946 	if (cfg->link_fec_opt !=
1947 	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
1948 		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
1949 		cfg->link_fec_opt = pcaps->link_fec_options;
1950 	}
1951 
1952 	/* Flow Control - always supported; no need to check against
1953 	 * capabilities
1954 	 */
1955 	ice_cfg_phy_fc(pi, cfg, pi->phy.curr_user_fc_req);
1956 
1957 	/* Enable link and link update */
1958 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
1959 
1960 	status = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1961 	if (status) {
1962 		dev_err(dev, "Failed to set phy config, VSI %d error %s\n",
1963 			vsi->vsi_num, ice_stat_str(status));
1964 		err = -EIO;
1965 	}
1966 
1967 	kfree(cfg);
1968 done:
1969 	kfree(pcaps);
1970 	return err;
1971 }
1972 
1973 /**
1974  * ice_check_media_subtask - Check for media
1975  * @pf: pointer to PF struct
1976  *
1977  * If media is available, then initialize PHY user configuration if it is not
1978  * been, and configure the PHY if the interface is up.
1979  */
1980 static void ice_check_media_subtask(struct ice_pf *pf)
1981 {
1982 	struct ice_port_info *pi;
1983 	struct ice_vsi *vsi;
1984 	int err;
1985 
1986 	/* No need to check for media if it's already present */
1987 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
1988 		return;
1989 
1990 	vsi = ice_get_main_vsi(pf);
1991 	if (!vsi)
1992 		return;
1993 
1994 	/* Refresh link info and check if media is present */
1995 	pi = vsi->port_info;
1996 	err = ice_update_link_info(pi);
1997 	if (err)
1998 		return;
1999 
2000 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2001 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state))
2002 			ice_init_phy_user_cfg(pi);
2003 
2004 		/* PHY settings are reset on media insertion, reconfigure
2005 		 * PHY to preserve settings.
2006 		 */
2007 		if (test_bit(__ICE_DOWN, vsi->state) &&
2008 		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2009 			return;
2010 
2011 		err = ice_configure_phy(vsi);
2012 		if (!err)
2013 			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2014 
2015 		/* A Link Status Event will be generated; the event handler
2016 		 * will complete bringing the interface up
2017 		 */
2018 	}
2019 }
2020 
2021 /**
2022  * ice_service_task - manage and run subtasks
2023  * @work: pointer to work_struct contained by the PF struct
2024  */
2025 static void ice_service_task(struct work_struct *work)
2026 {
2027 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2028 	unsigned long start_time = jiffies;
2029 
2030 	/* subtasks */
2031 
2032 	/* process reset requests first */
2033 	ice_reset_subtask(pf);
2034 
2035 	/* bail if a reset/recovery cycle is pending or rebuild failed */
2036 	if (ice_is_reset_in_progress(pf->state) ||
2037 	    test_bit(__ICE_SUSPENDED, pf->state) ||
2038 	    test_bit(__ICE_NEEDS_RESTART, pf->state)) {
2039 		ice_service_task_complete(pf);
2040 		return;
2041 	}
2042 
2043 	ice_clean_adminq_subtask(pf);
2044 	ice_check_media_subtask(pf);
2045 	ice_check_for_hang_subtask(pf);
2046 	ice_sync_fltr_subtask(pf);
2047 	ice_handle_mdd_event(pf);
2048 	ice_watchdog_subtask(pf);
2049 
2050 	if (ice_is_safe_mode(pf)) {
2051 		ice_service_task_complete(pf);
2052 		return;
2053 	}
2054 
2055 	ice_process_vflr_event(pf);
2056 	ice_clean_mailboxq_subtask(pf);
2057 	ice_sync_arfs_fltrs(pf);
2058 	/* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
2059 	ice_service_task_complete(pf);
2060 
2061 	/* If the tasks have taken longer than one service timer period
2062 	 * or there is more work to be done, reset the service timer to
2063 	 * schedule the service task now.
2064 	 */
2065 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2066 	    test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
2067 	    test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
2068 	    test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2069 	    test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
2070 		mod_timer(&pf->serv_tmr, jiffies);
2071 }
2072 
2073 /**
2074  * ice_set_ctrlq_len - helper function to set controlq length
2075  * @hw: pointer to the HW instance
2076  */
2077 static void ice_set_ctrlq_len(struct ice_hw *hw)
2078 {
2079 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2080 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2081 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2082 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2083 	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2084 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2085 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2086 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2087 }
2088 
2089 /**
2090  * ice_schedule_reset - schedule a reset
2091  * @pf: board private structure
2092  * @reset: reset being requested
2093  */
2094 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2095 {
2096 	struct device *dev = ice_pf_to_dev(pf);
2097 
2098 	/* bail out if earlier reset has failed */
2099 	if (test_bit(__ICE_RESET_FAILED, pf->state)) {
2100 		dev_dbg(dev, "earlier reset has failed\n");
2101 		return -EIO;
2102 	}
2103 	/* bail if reset/recovery already in progress */
2104 	if (ice_is_reset_in_progress(pf->state)) {
2105 		dev_dbg(dev, "Reset already in progress\n");
2106 		return -EBUSY;
2107 	}
2108 
2109 	switch (reset) {
2110 	case ICE_RESET_PFR:
2111 		set_bit(__ICE_PFR_REQ, pf->state);
2112 		break;
2113 	case ICE_RESET_CORER:
2114 		set_bit(__ICE_CORER_REQ, pf->state);
2115 		break;
2116 	case ICE_RESET_GLOBR:
2117 		set_bit(__ICE_GLOBR_REQ, pf->state);
2118 		break;
2119 	default:
2120 		return -EINVAL;
2121 	}
2122 
2123 	ice_service_task_schedule(pf);
2124 	return 0;
2125 }
2126 
2127 /**
2128  * ice_irq_affinity_notify - Callback for affinity changes
2129  * @notify: context as to what irq was changed
2130  * @mask: the new affinity mask
2131  *
2132  * This is a callback function used by the irq_set_affinity_notifier function
2133  * so that we may register to receive changes to the irq affinity masks.
2134  */
2135 static void
2136 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2137 			const cpumask_t *mask)
2138 {
2139 	struct ice_q_vector *q_vector =
2140 		container_of(notify, struct ice_q_vector, affinity_notify);
2141 
2142 	cpumask_copy(&q_vector->affinity_mask, mask);
2143 }
2144 
2145 /**
2146  * ice_irq_affinity_release - Callback for affinity notifier release
2147  * @ref: internal core kernel usage
2148  *
2149  * This is a callback function used by the irq_set_affinity_notifier function
2150  * to inform the current notification subscriber that they will no longer
2151  * receive notifications.
2152  */
2153 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2154 
2155 /**
2156  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2157  * @vsi: the VSI being configured
2158  */
2159 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2160 {
2161 	struct ice_hw *hw = &vsi->back->hw;
2162 	int i;
2163 
2164 	ice_for_each_q_vector(vsi, i)
2165 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2166 
2167 	ice_flush(hw);
2168 	return 0;
2169 }
2170 
2171 /**
2172  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2173  * @vsi: the VSI being configured
2174  * @basename: name for the vector
2175  */
2176 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2177 {
2178 	int q_vectors = vsi->num_q_vectors;
2179 	struct ice_pf *pf = vsi->back;
2180 	int base = vsi->base_vector;
2181 	struct device *dev;
2182 	int rx_int_idx = 0;
2183 	int tx_int_idx = 0;
2184 	int vector, err;
2185 	int irq_num;
2186 
2187 	dev = ice_pf_to_dev(pf);
2188 	for (vector = 0; vector < q_vectors; vector++) {
2189 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2190 
2191 		irq_num = pf->msix_entries[base + vector].vector;
2192 
2193 		if (q_vector->tx.ring && q_vector->rx.ring) {
2194 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2195 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2196 			tx_int_idx++;
2197 		} else if (q_vector->rx.ring) {
2198 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2199 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2200 		} else if (q_vector->tx.ring) {
2201 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2202 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2203 		} else {
2204 			/* skip this unused q_vector */
2205 			continue;
2206 		}
2207 		err = devm_request_irq(dev, irq_num, vsi->irq_handler, 0,
2208 				       q_vector->name, q_vector);
2209 		if (err) {
2210 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2211 				   err);
2212 			goto free_q_irqs;
2213 		}
2214 
2215 		/* register for affinity change notifications */
2216 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2217 			struct irq_affinity_notify *affinity_notify;
2218 
2219 			affinity_notify = &q_vector->affinity_notify;
2220 			affinity_notify->notify = ice_irq_affinity_notify;
2221 			affinity_notify->release = ice_irq_affinity_release;
2222 			irq_set_affinity_notifier(irq_num, affinity_notify);
2223 		}
2224 
2225 		/* assign the mask for this irq */
2226 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2227 	}
2228 
2229 	vsi->irqs_ready = true;
2230 	return 0;
2231 
2232 free_q_irqs:
2233 	while (vector) {
2234 		vector--;
2235 		irq_num = pf->msix_entries[base + vector].vector;
2236 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2237 			irq_set_affinity_notifier(irq_num, NULL);
2238 		irq_set_affinity_hint(irq_num, NULL);
2239 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2240 	}
2241 	return err;
2242 }
2243 
2244 /**
2245  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2246  * @vsi: VSI to setup Tx rings used by XDP
2247  *
2248  * Return 0 on success and negative value on error
2249  */
2250 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2251 {
2252 	struct device *dev = ice_pf_to_dev(vsi->back);
2253 	int i;
2254 
2255 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2256 		u16 xdp_q_idx = vsi->alloc_txq + i;
2257 		struct ice_ring *xdp_ring;
2258 
2259 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2260 
2261 		if (!xdp_ring)
2262 			goto free_xdp_rings;
2263 
2264 		xdp_ring->q_index = xdp_q_idx;
2265 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2266 		xdp_ring->ring_active = false;
2267 		xdp_ring->vsi = vsi;
2268 		xdp_ring->netdev = NULL;
2269 		xdp_ring->dev = dev;
2270 		xdp_ring->count = vsi->num_tx_desc;
2271 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2272 		if (ice_setup_tx_ring(xdp_ring))
2273 			goto free_xdp_rings;
2274 		ice_set_ring_xdp(xdp_ring);
2275 		xdp_ring->xsk_pool = ice_xsk_pool(xdp_ring);
2276 	}
2277 
2278 	return 0;
2279 
2280 free_xdp_rings:
2281 	for (; i >= 0; i--)
2282 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2283 			ice_free_tx_ring(vsi->xdp_rings[i]);
2284 	return -ENOMEM;
2285 }
2286 
2287 /**
2288  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2289  * @vsi: VSI to set the bpf prog on
2290  * @prog: the bpf prog pointer
2291  */
2292 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2293 {
2294 	struct bpf_prog *old_prog;
2295 	int i;
2296 
2297 	old_prog = xchg(&vsi->xdp_prog, prog);
2298 	if (old_prog)
2299 		bpf_prog_put(old_prog);
2300 
2301 	ice_for_each_rxq(vsi, i)
2302 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2303 }
2304 
2305 /**
2306  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2307  * @vsi: VSI to bring up Tx rings used by XDP
2308  * @prog: bpf program that will be assigned to VSI
2309  *
2310  * Return 0 on success and negative value on error
2311  */
2312 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2313 {
2314 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2315 	int xdp_rings_rem = vsi->num_xdp_txq;
2316 	struct ice_pf *pf = vsi->back;
2317 	struct ice_qs_cfg xdp_qs_cfg = {
2318 		.qs_mutex = &pf->avail_q_mutex,
2319 		.pf_map = pf->avail_txqs,
2320 		.pf_map_size = pf->max_pf_txqs,
2321 		.q_count = vsi->num_xdp_txq,
2322 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2323 		.vsi_map = vsi->txq_map,
2324 		.vsi_map_offset = vsi->alloc_txq,
2325 		.mapping_mode = ICE_VSI_MAP_CONTIG
2326 	};
2327 	enum ice_status status;
2328 	struct device *dev;
2329 	int i, v_idx;
2330 
2331 	dev = ice_pf_to_dev(pf);
2332 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2333 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2334 	if (!vsi->xdp_rings)
2335 		return -ENOMEM;
2336 
2337 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2338 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2339 		goto err_map_xdp;
2340 
2341 	if (ice_xdp_alloc_setup_rings(vsi))
2342 		goto clear_xdp_rings;
2343 
2344 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2345 	ice_for_each_q_vector(vsi, v_idx) {
2346 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2347 		int xdp_rings_per_v, q_id, q_base;
2348 
2349 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2350 					       vsi->num_q_vectors - v_idx);
2351 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2352 
2353 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2354 			struct ice_ring *xdp_ring = vsi->xdp_rings[q_id];
2355 
2356 			xdp_ring->q_vector = q_vector;
2357 			xdp_ring->next = q_vector->tx.ring;
2358 			q_vector->tx.ring = xdp_ring;
2359 		}
2360 		xdp_rings_rem -= xdp_rings_per_v;
2361 	}
2362 
2363 	/* omit the scheduler update if in reset path; XDP queues will be
2364 	 * taken into account at the end of ice_vsi_rebuild, where
2365 	 * ice_cfg_vsi_lan is being called
2366 	 */
2367 	if (ice_is_reset_in_progress(pf->state))
2368 		return 0;
2369 
2370 	/* tell the Tx scheduler that right now we have
2371 	 * additional queues
2372 	 */
2373 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2374 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2375 
2376 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2377 				 max_txqs);
2378 	if (status) {
2379 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %s\n",
2380 			ice_stat_str(status));
2381 		goto clear_xdp_rings;
2382 	}
2383 	ice_vsi_assign_bpf_prog(vsi, prog);
2384 
2385 	return 0;
2386 clear_xdp_rings:
2387 	for (i = 0; i < vsi->num_xdp_txq; i++)
2388 		if (vsi->xdp_rings[i]) {
2389 			kfree_rcu(vsi->xdp_rings[i], rcu);
2390 			vsi->xdp_rings[i] = NULL;
2391 		}
2392 
2393 err_map_xdp:
2394 	mutex_lock(&pf->avail_q_mutex);
2395 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2396 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2397 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2398 	}
2399 	mutex_unlock(&pf->avail_q_mutex);
2400 
2401 	devm_kfree(dev, vsi->xdp_rings);
2402 	return -ENOMEM;
2403 }
2404 
2405 /**
2406  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2407  * @vsi: VSI to remove XDP rings
2408  *
2409  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2410  * resources
2411  */
2412 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2413 {
2414 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2415 	struct ice_pf *pf = vsi->back;
2416 	int i, v_idx;
2417 
2418 	/* q_vectors are freed in reset path so there's no point in detaching
2419 	 * rings; in case of rebuild being triggered not from reset reset bits
2420 	 * in pf->state won't be set, so additionally check first q_vector
2421 	 * against NULL
2422 	 */
2423 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2424 		goto free_qmap;
2425 
2426 	ice_for_each_q_vector(vsi, v_idx) {
2427 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2428 		struct ice_ring *ring;
2429 
2430 		ice_for_each_ring(ring, q_vector->tx)
2431 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2432 				break;
2433 
2434 		/* restore the value of last node prior to XDP setup */
2435 		q_vector->tx.ring = ring;
2436 	}
2437 
2438 free_qmap:
2439 	mutex_lock(&pf->avail_q_mutex);
2440 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2441 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2442 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2443 	}
2444 	mutex_unlock(&pf->avail_q_mutex);
2445 
2446 	for (i = 0; i < vsi->num_xdp_txq; i++)
2447 		if (vsi->xdp_rings[i]) {
2448 			if (vsi->xdp_rings[i]->desc)
2449 				ice_free_tx_ring(vsi->xdp_rings[i]);
2450 			kfree_rcu(vsi->xdp_rings[i], rcu);
2451 			vsi->xdp_rings[i] = NULL;
2452 		}
2453 
2454 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2455 	vsi->xdp_rings = NULL;
2456 
2457 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2458 		return 0;
2459 
2460 	ice_vsi_assign_bpf_prog(vsi, NULL);
2461 
2462 	/* notify Tx scheduler that we destroyed XDP queues and bring
2463 	 * back the old number of child nodes
2464 	 */
2465 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2466 		max_txqs[i] = vsi->num_txq;
2467 
2468 	/* change number of XDP Tx queues to 0 */
2469 	vsi->num_xdp_txq = 0;
2470 
2471 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2472 			       max_txqs);
2473 }
2474 
2475 /**
2476  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2477  * @vsi: VSI to setup XDP for
2478  * @prog: XDP program
2479  * @extack: netlink extended ack
2480  */
2481 static int
2482 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2483 		   struct netlink_ext_ack *extack)
2484 {
2485 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2486 	bool if_running = netif_running(vsi->netdev);
2487 	int ret = 0, xdp_ring_err = 0;
2488 
2489 	if (frame_size > vsi->rx_buf_len) {
2490 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2491 		return -EOPNOTSUPP;
2492 	}
2493 
2494 	/* need to stop netdev while setting up the program for Rx rings */
2495 	if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) {
2496 		ret = ice_down(vsi);
2497 		if (ret) {
2498 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2499 			return ret;
2500 		}
2501 	}
2502 
2503 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2504 		vsi->num_xdp_txq = vsi->alloc_rxq;
2505 		xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2506 		if (xdp_ring_err)
2507 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2508 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2509 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2510 		if (xdp_ring_err)
2511 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2512 	} else {
2513 		ice_vsi_assign_bpf_prog(vsi, prog);
2514 	}
2515 
2516 	if (if_running)
2517 		ret = ice_up(vsi);
2518 
2519 	if (!ret && prog && vsi->xsk_pools) {
2520 		int i;
2521 
2522 		ice_for_each_rxq(vsi, i) {
2523 			struct ice_ring *rx_ring = vsi->rx_rings[i];
2524 
2525 			if (rx_ring->xsk_pool)
2526 				napi_schedule(&rx_ring->q_vector->napi);
2527 		}
2528 	}
2529 
2530 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2531 }
2532 
2533 /**
2534  * ice_xdp - implements XDP handler
2535  * @dev: netdevice
2536  * @xdp: XDP command
2537  */
2538 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2539 {
2540 	struct ice_netdev_priv *np = netdev_priv(dev);
2541 	struct ice_vsi *vsi = np->vsi;
2542 
2543 	if (vsi->type != ICE_VSI_PF) {
2544 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2545 		return -EINVAL;
2546 	}
2547 
2548 	switch (xdp->command) {
2549 	case XDP_SETUP_PROG:
2550 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2551 	case XDP_SETUP_XSK_POOL:
2552 		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2553 					  xdp->xsk.queue_id);
2554 	default:
2555 		return -EINVAL;
2556 	}
2557 }
2558 
2559 /**
2560  * ice_ena_misc_vector - enable the non-queue interrupts
2561  * @pf: board private structure
2562  */
2563 static void ice_ena_misc_vector(struct ice_pf *pf)
2564 {
2565 	struct ice_hw *hw = &pf->hw;
2566 	u32 val;
2567 
2568 	/* Disable anti-spoof detection interrupt to prevent spurious event
2569 	 * interrupts during a function reset. Anti-spoof functionally is
2570 	 * still supported.
2571 	 */
2572 	val = rd32(hw, GL_MDCK_TX_TDPU);
2573 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2574 	wr32(hw, GL_MDCK_TX_TDPU, val);
2575 
2576 	/* clear things first */
2577 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
2578 	rd32(hw, PFINT_OICR);		/* read to clear */
2579 
2580 	val = (PFINT_OICR_ECC_ERR_M |
2581 	       PFINT_OICR_MAL_DETECT_M |
2582 	       PFINT_OICR_GRST_M |
2583 	       PFINT_OICR_PCI_EXCEPTION_M |
2584 	       PFINT_OICR_VFLR_M |
2585 	       PFINT_OICR_HMC_ERR_M |
2586 	       PFINT_OICR_PE_CRITERR_M);
2587 
2588 	wr32(hw, PFINT_OICR_ENA, val);
2589 
2590 	/* SW_ITR_IDX = 0, but don't change INTENA */
2591 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2592 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2593 }
2594 
2595 /**
2596  * ice_misc_intr - misc interrupt handler
2597  * @irq: interrupt number
2598  * @data: pointer to a q_vector
2599  */
2600 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2601 {
2602 	struct ice_pf *pf = (struct ice_pf *)data;
2603 	struct ice_hw *hw = &pf->hw;
2604 	irqreturn_t ret = IRQ_NONE;
2605 	struct device *dev;
2606 	u32 oicr, ena_mask;
2607 
2608 	dev = ice_pf_to_dev(pf);
2609 	set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2610 	set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2611 
2612 	oicr = rd32(hw, PFINT_OICR);
2613 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2614 
2615 	if (oicr & PFINT_OICR_SWINT_M) {
2616 		ena_mask &= ~PFINT_OICR_SWINT_M;
2617 		pf->sw_int_count++;
2618 	}
2619 
2620 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2621 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2622 		set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2623 	}
2624 	if (oicr & PFINT_OICR_VFLR_M) {
2625 		/* disable any further VFLR event notifications */
2626 		if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
2627 			u32 reg = rd32(hw, PFINT_OICR_ENA);
2628 
2629 			reg &= ~PFINT_OICR_VFLR_M;
2630 			wr32(hw, PFINT_OICR_ENA, reg);
2631 		} else {
2632 			ena_mask &= ~PFINT_OICR_VFLR_M;
2633 			set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
2634 		}
2635 	}
2636 
2637 	if (oicr & PFINT_OICR_GRST_M) {
2638 		u32 reset;
2639 
2640 		/* we have a reset warning */
2641 		ena_mask &= ~PFINT_OICR_GRST_M;
2642 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2643 			GLGEN_RSTAT_RESET_TYPE_S;
2644 
2645 		if (reset == ICE_RESET_CORER)
2646 			pf->corer_count++;
2647 		else if (reset == ICE_RESET_GLOBR)
2648 			pf->globr_count++;
2649 		else if (reset == ICE_RESET_EMPR)
2650 			pf->empr_count++;
2651 		else
2652 			dev_dbg(dev, "Invalid reset type %d\n", reset);
2653 
2654 		/* If a reset cycle isn't already in progress, we set a bit in
2655 		 * pf->state so that the service task can start a reset/rebuild.
2656 		 * We also make note of which reset happened so that peer
2657 		 * devices/drivers can be informed.
2658 		 */
2659 		if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
2660 			if (reset == ICE_RESET_CORER)
2661 				set_bit(__ICE_CORER_RECV, pf->state);
2662 			else if (reset == ICE_RESET_GLOBR)
2663 				set_bit(__ICE_GLOBR_RECV, pf->state);
2664 			else
2665 				set_bit(__ICE_EMPR_RECV, pf->state);
2666 
2667 			/* There are couple of different bits at play here.
2668 			 * hw->reset_ongoing indicates whether the hardware is
2669 			 * in reset. This is set to true when a reset interrupt
2670 			 * is received and set back to false after the driver
2671 			 * has determined that the hardware is out of reset.
2672 			 *
2673 			 * __ICE_RESET_OICR_RECV in pf->state indicates
2674 			 * that a post reset rebuild is required before the
2675 			 * driver is operational again. This is set above.
2676 			 *
2677 			 * As this is the start of the reset/rebuild cycle, set
2678 			 * both to indicate that.
2679 			 */
2680 			hw->reset_ongoing = true;
2681 		}
2682 	}
2683 
2684 	if (oicr & PFINT_OICR_HMC_ERR_M) {
2685 		ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2686 		dev_dbg(dev, "HMC Error interrupt - info 0x%x, data 0x%x\n",
2687 			rd32(hw, PFHMC_ERRORINFO),
2688 			rd32(hw, PFHMC_ERRORDATA));
2689 	}
2690 
2691 	/* Report any remaining unexpected interrupts */
2692 	oicr &= ena_mask;
2693 	if (oicr) {
2694 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
2695 		/* If a critical error is pending there is no choice but to
2696 		 * reset the device.
2697 		 */
2698 		if (oicr & (PFINT_OICR_PE_CRITERR_M |
2699 			    PFINT_OICR_PCI_EXCEPTION_M |
2700 			    PFINT_OICR_ECC_ERR_M)) {
2701 			set_bit(__ICE_PFR_REQ, pf->state);
2702 			ice_service_task_schedule(pf);
2703 		}
2704 	}
2705 	ret = IRQ_HANDLED;
2706 
2707 	ice_service_task_schedule(pf);
2708 	ice_irq_dynamic_ena(hw, NULL, NULL);
2709 
2710 	return ret;
2711 }
2712 
2713 /**
2714  * ice_dis_ctrlq_interrupts - disable control queue interrupts
2715  * @hw: pointer to HW structure
2716  */
2717 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
2718 {
2719 	/* disable Admin queue Interrupt causes */
2720 	wr32(hw, PFINT_FW_CTL,
2721 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
2722 
2723 	/* disable Mailbox queue Interrupt causes */
2724 	wr32(hw, PFINT_MBX_CTL,
2725 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
2726 
2727 	/* disable Control queue Interrupt causes */
2728 	wr32(hw, PFINT_OICR_CTL,
2729 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
2730 
2731 	ice_flush(hw);
2732 }
2733 
2734 /**
2735  * ice_free_irq_msix_misc - Unroll misc vector setup
2736  * @pf: board private structure
2737  */
2738 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2739 {
2740 	struct ice_hw *hw = &pf->hw;
2741 
2742 	ice_dis_ctrlq_interrupts(hw);
2743 
2744 	/* disable OICR interrupt */
2745 	wr32(hw, PFINT_OICR_ENA, 0);
2746 	ice_flush(hw);
2747 
2748 	if (pf->msix_entries) {
2749 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2750 		devm_free_irq(ice_pf_to_dev(pf),
2751 			      pf->msix_entries[pf->oicr_idx].vector, pf);
2752 	}
2753 
2754 	pf->num_avail_sw_msix += 1;
2755 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2756 }
2757 
2758 /**
2759  * ice_ena_ctrlq_interrupts - enable control queue interrupts
2760  * @hw: pointer to HW structure
2761  * @reg_idx: HW vector index to associate the control queue interrupts with
2762  */
2763 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
2764 {
2765 	u32 val;
2766 
2767 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2768 	       PFINT_OICR_CTL_CAUSE_ENA_M);
2769 	wr32(hw, PFINT_OICR_CTL, val);
2770 
2771 	/* enable Admin queue Interrupt causes */
2772 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2773 	       PFINT_FW_CTL_CAUSE_ENA_M);
2774 	wr32(hw, PFINT_FW_CTL, val);
2775 
2776 	/* enable Mailbox queue Interrupt causes */
2777 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
2778 	       PFINT_MBX_CTL_CAUSE_ENA_M);
2779 	wr32(hw, PFINT_MBX_CTL, val);
2780 
2781 	ice_flush(hw);
2782 }
2783 
2784 /**
2785  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2786  * @pf: board private structure
2787  *
2788  * This sets up the handler for MSIX 0, which is used to manage the
2789  * non-queue interrupts, e.g. AdminQ and errors. This is not used
2790  * when in MSI or Legacy interrupt mode.
2791  */
2792 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2793 {
2794 	struct device *dev = ice_pf_to_dev(pf);
2795 	struct ice_hw *hw = &pf->hw;
2796 	int oicr_idx, err = 0;
2797 
2798 	if (!pf->int_name[0])
2799 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2800 			 dev_driver_string(dev), dev_name(dev));
2801 
2802 	/* Do not request IRQ but do enable OICR interrupt since settings are
2803 	 * lost during reset. Note that this function is called only during
2804 	 * rebuild path and not while reset is in progress.
2805 	 */
2806 	if (ice_is_reset_in_progress(pf->state))
2807 		goto skip_req_irq;
2808 
2809 	/* reserve one vector in irq_tracker for misc interrupts */
2810 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2811 	if (oicr_idx < 0)
2812 		return oicr_idx;
2813 
2814 	pf->num_avail_sw_msix -= 1;
2815 	pf->oicr_idx = (u16)oicr_idx;
2816 
2817 	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
2818 			       ice_misc_intr, 0, pf->int_name, pf);
2819 	if (err) {
2820 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
2821 			pf->int_name, err);
2822 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2823 		pf->num_avail_sw_msix += 1;
2824 		return err;
2825 	}
2826 
2827 skip_req_irq:
2828 	ice_ena_misc_vector(pf);
2829 
2830 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
2831 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2832 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
2833 
2834 	ice_flush(hw);
2835 	ice_irq_dynamic_ena(hw, NULL, NULL);
2836 
2837 	return 0;
2838 }
2839 
2840 /**
2841  * ice_napi_add - register NAPI handler for the VSI
2842  * @vsi: VSI for which NAPI handler is to be registered
2843  *
2844  * This function is only called in the driver's load path. Registering the NAPI
2845  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2846  * reset/rebuild, etc.)
2847  */
2848 static void ice_napi_add(struct ice_vsi *vsi)
2849 {
2850 	int v_idx;
2851 
2852 	if (!vsi->netdev)
2853 		return;
2854 
2855 	ice_for_each_q_vector(vsi, v_idx)
2856 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
2857 			       ice_napi_poll, NAPI_POLL_WEIGHT);
2858 }
2859 
2860 /**
2861  * ice_set_ops - set netdev and ethtools ops for the given netdev
2862  * @netdev: netdev instance
2863  */
2864 static void ice_set_ops(struct net_device *netdev)
2865 {
2866 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2867 
2868 	if (ice_is_safe_mode(pf)) {
2869 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
2870 		ice_set_ethtool_safe_mode_ops(netdev);
2871 		return;
2872 	}
2873 
2874 	netdev->netdev_ops = &ice_netdev_ops;
2875 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
2876 	ice_set_ethtool_ops(netdev);
2877 }
2878 
2879 /**
2880  * ice_set_netdev_features - set features for the given netdev
2881  * @netdev: netdev instance
2882  */
2883 static void ice_set_netdev_features(struct net_device *netdev)
2884 {
2885 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2886 	netdev_features_t csumo_features;
2887 	netdev_features_t vlano_features;
2888 	netdev_features_t dflt_features;
2889 	netdev_features_t tso_features;
2890 
2891 	if (ice_is_safe_mode(pf)) {
2892 		/* safe mode */
2893 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
2894 		netdev->hw_features = netdev->features;
2895 		return;
2896 	}
2897 
2898 	dflt_features = NETIF_F_SG	|
2899 			NETIF_F_HIGHDMA	|
2900 			NETIF_F_NTUPLE	|
2901 			NETIF_F_RXHASH;
2902 
2903 	csumo_features = NETIF_F_RXCSUM	  |
2904 			 NETIF_F_IP_CSUM  |
2905 			 NETIF_F_SCTP_CRC |
2906 			 NETIF_F_IPV6_CSUM;
2907 
2908 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2909 			 NETIF_F_HW_VLAN_CTAG_TX     |
2910 			 NETIF_F_HW_VLAN_CTAG_RX;
2911 
2912 	tso_features = NETIF_F_TSO			|
2913 		       NETIF_F_TSO_ECN			|
2914 		       NETIF_F_TSO6			|
2915 		       NETIF_F_GSO_GRE			|
2916 		       NETIF_F_GSO_UDP_TUNNEL		|
2917 		       NETIF_F_GSO_GRE_CSUM		|
2918 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
2919 		       NETIF_F_GSO_PARTIAL		|
2920 		       NETIF_F_GSO_IPXIP4		|
2921 		       NETIF_F_GSO_IPXIP6		|
2922 		       NETIF_F_GSO_UDP_L4;
2923 
2924 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
2925 					NETIF_F_GSO_GRE_CSUM;
2926 	/* set features that user can change */
2927 	netdev->hw_features = dflt_features | csumo_features |
2928 			      vlano_features | tso_features;
2929 
2930 	/* add support for HW_CSUM on packets with MPLS header */
2931 	netdev->mpls_features =  NETIF_F_HW_CSUM;
2932 
2933 	/* enable features */
2934 	netdev->features |= netdev->hw_features;
2935 	/* encap and VLAN devices inherit default, csumo and tso features */
2936 	netdev->hw_enc_features |= dflt_features | csumo_features |
2937 				   tso_features;
2938 	netdev->vlan_features |= dflt_features | csumo_features |
2939 				 tso_features;
2940 }
2941 
2942 /**
2943  * ice_cfg_netdev - Allocate, configure and register a netdev
2944  * @vsi: the VSI associated with the new netdev
2945  *
2946  * Returns 0 on success, negative value on failure
2947  */
2948 static int ice_cfg_netdev(struct ice_vsi *vsi)
2949 {
2950 	struct ice_pf *pf = vsi->back;
2951 	struct ice_netdev_priv *np;
2952 	struct net_device *netdev;
2953 	u8 mac_addr[ETH_ALEN];
2954 	int err;
2955 
2956 	err = ice_devlink_create_port(pf);
2957 	if (err)
2958 		return err;
2959 
2960 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
2961 				    vsi->alloc_rxq);
2962 	if (!netdev) {
2963 		err = -ENOMEM;
2964 		goto err_destroy_devlink_port;
2965 	}
2966 
2967 	vsi->netdev = netdev;
2968 	np = netdev_priv(netdev);
2969 	np->vsi = vsi;
2970 
2971 	ice_set_netdev_features(netdev);
2972 
2973 	ice_set_ops(netdev);
2974 
2975 	if (vsi->type == ICE_VSI_PF) {
2976 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(pf));
2977 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2978 		ether_addr_copy(netdev->dev_addr, mac_addr);
2979 		ether_addr_copy(netdev->perm_addr, mac_addr);
2980 	}
2981 
2982 	netdev->priv_flags |= IFF_UNICAST_FLT;
2983 
2984 	/* Setup netdev TC information */
2985 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
2986 
2987 	/* setup watchdog timeout value to be 5 second */
2988 	netdev->watchdog_timeo = 5 * HZ;
2989 
2990 	netdev->min_mtu = ETH_MIN_MTU;
2991 	netdev->max_mtu = ICE_MAX_MTU;
2992 
2993 	err = register_netdev(vsi->netdev);
2994 	if (err)
2995 		goto err_free_netdev;
2996 
2997 	devlink_port_type_eth_set(&pf->devlink_port, vsi->netdev);
2998 
2999 	netif_carrier_off(vsi->netdev);
3000 
3001 	/* make sure transmit queues start off as stopped */
3002 	netif_tx_stop_all_queues(vsi->netdev);
3003 
3004 	return 0;
3005 
3006 err_free_netdev:
3007 	free_netdev(vsi->netdev);
3008 	vsi->netdev = NULL;
3009 err_destroy_devlink_port:
3010 	ice_devlink_destroy_port(pf);
3011 	return err;
3012 }
3013 
3014 /**
3015  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3016  * @lut: Lookup table
3017  * @rss_table_size: Lookup table size
3018  * @rss_size: Range of queue number for hashing
3019  */
3020 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3021 {
3022 	u16 i;
3023 
3024 	for (i = 0; i < rss_table_size; i++)
3025 		lut[i] = i % rss_size;
3026 }
3027 
3028 /**
3029  * ice_pf_vsi_setup - Set up a PF VSI
3030  * @pf: board private structure
3031  * @pi: pointer to the port_info instance
3032  *
3033  * Returns pointer to the successfully allocated VSI software struct
3034  * on success, otherwise returns NULL on failure.
3035  */
3036 static struct ice_vsi *
3037 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3038 {
3039 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
3040 }
3041 
3042 /**
3043  * ice_ctrl_vsi_setup - Set up a control VSI
3044  * @pf: board private structure
3045  * @pi: pointer to the port_info instance
3046  *
3047  * Returns pointer to the successfully allocated VSI software struct
3048  * on success, otherwise returns NULL on failure.
3049  */
3050 static struct ice_vsi *
3051 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3052 {
3053 	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, ICE_INVAL_VFID);
3054 }
3055 
3056 /**
3057  * ice_lb_vsi_setup - Set up a loopback VSI
3058  * @pf: board private structure
3059  * @pi: pointer to the port_info instance
3060  *
3061  * Returns pointer to the successfully allocated VSI software struct
3062  * on success, otherwise returns NULL on failure.
3063  */
3064 struct ice_vsi *
3065 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3066 {
3067 	return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
3068 }
3069 
3070 /**
3071  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3072  * @netdev: network interface to be adjusted
3073  * @proto: unused protocol
3074  * @vid: VLAN ID to be added
3075  *
3076  * net_device_ops implementation for adding VLAN IDs
3077  */
3078 static int
3079 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
3080 		    u16 vid)
3081 {
3082 	struct ice_netdev_priv *np = netdev_priv(netdev);
3083 	struct ice_vsi *vsi = np->vsi;
3084 	int ret;
3085 
3086 	if (vid >= VLAN_N_VID) {
3087 		netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
3088 			   vid, VLAN_N_VID);
3089 		return -EINVAL;
3090 	}
3091 
3092 	if (vsi->info.pvid)
3093 		return -EINVAL;
3094 
3095 	/* VLAN 0 is added by default during load/reset */
3096 	if (!vid)
3097 		return 0;
3098 
3099 	/* Enable VLAN pruning when a VLAN other than 0 is added */
3100 	if (!ice_vsi_is_vlan_pruning_ena(vsi)) {
3101 		ret = ice_cfg_vlan_pruning(vsi, true, false);
3102 		if (ret)
3103 			return ret;
3104 	}
3105 
3106 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3107 	 * packets aren't pruned by the device's internal switch on Rx
3108 	 */
3109 	ret = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3110 	if (!ret) {
3111 		vsi->vlan_ena = true;
3112 		set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3113 	}
3114 
3115 	return ret;
3116 }
3117 
3118 /**
3119  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3120  * @netdev: network interface to be adjusted
3121  * @proto: unused protocol
3122  * @vid: VLAN ID to be removed
3123  *
3124  * net_device_ops implementation for removing VLAN IDs
3125  */
3126 static int
3127 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
3128 		     u16 vid)
3129 {
3130 	struct ice_netdev_priv *np = netdev_priv(netdev);
3131 	struct ice_vsi *vsi = np->vsi;
3132 	int ret;
3133 
3134 	if (vsi->info.pvid)
3135 		return -EINVAL;
3136 
3137 	/* don't allow removal of VLAN 0 */
3138 	if (!vid)
3139 		return 0;
3140 
3141 	/* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3142 	 * information
3143 	 */
3144 	ret = ice_vsi_kill_vlan(vsi, vid);
3145 	if (ret)
3146 		return ret;
3147 
3148 	/* Disable pruning when VLAN 0 is the only VLAN rule */
3149 	if (vsi->num_vlan == 1 && ice_vsi_is_vlan_pruning_ena(vsi))
3150 		ret = ice_cfg_vlan_pruning(vsi, false, false);
3151 
3152 	vsi->vlan_ena = false;
3153 	set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3154 	return ret;
3155 }
3156 
3157 /**
3158  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3159  * @pf: board private structure
3160  *
3161  * Returns 0 on success, negative value on failure
3162  */
3163 static int ice_setup_pf_sw(struct ice_pf *pf)
3164 {
3165 	struct ice_vsi *vsi;
3166 	int status = 0;
3167 
3168 	if (ice_is_reset_in_progress(pf->state))
3169 		return -EBUSY;
3170 
3171 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3172 	if (!vsi) {
3173 		status = -ENOMEM;
3174 		goto unroll_vsi_setup;
3175 	}
3176 
3177 	status = ice_cfg_netdev(vsi);
3178 	if (status) {
3179 		status = -ENODEV;
3180 		goto unroll_vsi_setup;
3181 	}
3182 	/* netdev has to be configured before setting frame size */
3183 	ice_vsi_cfg_frame_size(vsi);
3184 
3185 	/* Setup DCB netlink interface */
3186 	ice_dcbnl_setup(vsi);
3187 
3188 	/* registering the NAPI handler requires both the queues and
3189 	 * netdev to be created, which are done in ice_pf_vsi_setup()
3190 	 * and ice_cfg_netdev() respectively
3191 	 */
3192 	ice_napi_add(vsi);
3193 
3194 	status = ice_set_cpu_rx_rmap(vsi);
3195 	if (status) {
3196 		dev_err(ice_pf_to_dev(pf), "Failed to set CPU Rx map VSI %d error %d\n",
3197 			vsi->vsi_num, status);
3198 		status = -EINVAL;
3199 		goto unroll_napi_add;
3200 	}
3201 	status = ice_init_mac_fltr(pf);
3202 	if (status)
3203 		goto free_cpu_rx_map;
3204 
3205 	return status;
3206 
3207 free_cpu_rx_map:
3208 	ice_free_cpu_rx_rmap(vsi);
3209 
3210 unroll_napi_add:
3211 	if (vsi) {
3212 		ice_napi_del(vsi);
3213 		if (vsi->netdev) {
3214 			if (vsi->netdev->reg_state == NETREG_REGISTERED)
3215 				unregister_netdev(vsi->netdev);
3216 			free_netdev(vsi->netdev);
3217 			vsi->netdev = NULL;
3218 		}
3219 	}
3220 
3221 unroll_vsi_setup:
3222 	if (vsi) {
3223 		ice_vsi_free_q_vectors(vsi);
3224 		ice_vsi_delete(vsi);
3225 		ice_vsi_put_qs(vsi);
3226 		ice_vsi_clear(vsi);
3227 	}
3228 	return status;
3229 }
3230 
3231 /**
3232  * ice_get_avail_q_count - Get count of queues in use
3233  * @pf_qmap: bitmap to get queue use count from
3234  * @lock: pointer to a mutex that protects access to pf_qmap
3235  * @size: size of the bitmap
3236  */
3237 static u16
3238 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3239 {
3240 	unsigned long bit;
3241 	u16 count = 0;
3242 
3243 	mutex_lock(lock);
3244 	for_each_clear_bit(bit, pf_qmap, size)
3245 		count++;
3246 	mutex_unlock(lock);
3247 
3248 	return count;
3249 }
3250 
3251 /**
3252  * ice_get_avail_txq_count - Get count of Tx queues in use
3253  * @pf: pointer to an ice_pf instance
3254  */
3255 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3256 {
3257 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3258 				     pf->max_pf_txqs);
3259 }
3260 
3261 /**
3262  * ice_get_avail_rxq_count - Get count of Rx queues in use
3263  * @pf: pointer to an ice_pf instance
3264  */
3265 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3266 {
3267 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3268 				     pf->max_pf_rxqs);
3269 }
3270 
3271 /**
3272  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3273  * @pf: board private structure to initialize
3274  */
3275 static void ice_deinit_pf(struct ice_pf *pf)
3276 {
3277 	ice_service_task_stop(pf);
3278 	mutex_destroy(&pf->sw_mutex);
3279 	mutex_destroy(&pf->tc_mutex);
3280 	mutex_destroy(&pf->avail_q_mutex);
3281 
3282 	if (pf->avail_txqs) {
3283 		bitmap_free(pf->avail_txqs);
3284 		pf->avail_txqs = NULL;
3285 	}
3286 
3287 	if (pf->avail_rxqs) {
3288 		bitmap_free(pf->avail_rxqs);
3289 		pf->avail_rxqs = NULL;
3290 	}
3291 }
3292 
3293 /**
3294  * ice_set_pf_caps - set PFs capability flags
3295  * @pf: pointer to the PF instance
3296  */
3297 static void ice_set_pf_caps(struct ice_pf *pf)
3298 {
3299 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3300 
3301 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3302 	if (func_caps->common_cap.dcb)
3303 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3304 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3305 	if (func_caps->common_cap.sr_iov_1_1) {
3306 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3307 		pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
3308 					      ICE_MAX_VF_COUNT);
3309 	}
3310 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3311 	if (func_caps->common_cap.rss_table_size)
3312 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3313 
3314 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3315 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3316 		u16 unused;
3317 
3318 		/* ctrl_vsi_idx will be set to a valid value when flow director
3319 		 * is setup by ice_init_fdir
3320 		 */
3321 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3322 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3323 		/* force guaranteed filter pool for PF */
3324 		ice_alloc_fd_guar_item(&pf->hw, &unused,
3325 				       func_caps->fd_fltr_guar);
3326 		/* force shared filter pool for PF */
3327 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3328 				       func_caps->fd_fltr_best_effort);
3329 	}
3330 
3331 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3332 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3333 }
3334 
3335 /**
3336  * ice_init_pf - Initialize general software structures (struct ice_pf)
3337  * @pf: board private structure to initialize
3338  */
3339 static int ice_init_pf(struct ice_pf *pf)
3340 {
3341 	ice_set_pf_caps(pf);
3342 
3343 	mutex_init(&pf->sw_mutex);
3344 	mutex_init(&pf->tc_mutex);
3345 
3346 	INIT_HLIST_HEAD(&pf->aq_wait_list);
3347 	spin_lock_init(&pf->aq_wait_lock);
3348 	init_waitqueue_head(&pf->aq_wait_queue);
3349 
3350 	/* setup service timer and periodic service task */
3351 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3352 	pf->serv_tmr_period = HZ;
3353 	INIT_WORK(&pf->serv_task, ice_service_task);
3354 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
3355 
3356 	mutex_init(&pf->avail_q_mutex);
3357 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3358 	if (!pf->avail_txqs)
3359 		return -ENOMEM;
3360 
3361 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3362 	if (!pf->avail_rxqs) {
3363 		devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
3364 		pf->avail_txqs = NULL;
3365 		return -ENOMEM;
3366 	}
3367 
3368 	return 0;
3369 }
3370 
3371 /**
3372  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3373  * @pf: board private structure
3374  *
3375  * compute the number of MSIX vectors required (v_budget) and request from
3376  * the OS. Return the number of vectors reserved or negative on failure
3377  */
3378 static int ice_ena_msix_range(struct ice_pf *pf)
3379 {
3380 	struct device *dev = ice_pf_to_dev(pf);
3381 	int v_left, v_actual, v_budget = 0;
3382 	int needed, err, i;
3383 
3384 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3385 
3386 	/* reserve one vector for miscellaneous handler */
3387 	needed = 1;
3388 	if (v_left < needed)
3389 		goto no_hw_vecs_left_err;
3390 	v_budget += needed;
3391 	v_left -= needed;
3392 
3393 	/* reserve vectors for LAN traffic */
3394 	needed = min_t(int, num_online_cpus(), v_left);
3395 	if (v_left < needed)
3396 		goto no_hw_vecs_left_err;
3397 	pf->num_lan_msix = needed;
3398 	v_budget += needed;
3399 	v_left -= needed;
3400 
3401 	/* reserve one vector for flow director */
3402 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3403 		needed = ICE_FDIR_MSIX;
3404 		if (v_left < needed)
3405 			goto no_hw_vecs_left_err;
3406 		v_budget += needed;
3407 		v_left -= needed;
3408 	}
3409 
3410 	pf->msix_entries = devm_kcalloc(dev, v_budget,
3411 					sizeof(*pf->msix_entries), GFP_KERNEL);
3412 
3413 	if (!pf->msix_entries) {
3414 		err = -ENOMEM;
3415 		goto exit_err;
3416 	}
3417 
3418 	for (i = 0; i < v_budget; i++)
3419 		pf->msix_entries[i].entry = i;
3420 
3421 	/* actually reserve the vectors */
3422 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3423 					 ICE_MIN_MSIX, v_budget);
3424 
3425 	if (v_actual < 0) {
3426 		dev_err(dev, "unable to reserve MSI-X vectors\n");
3427 		err = v_actual;
3428 		goto msix_err;
3429 	}
3430 
3431 	if (v_actual < v_budget) {
3432 		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
3433 			 v_budget, v_actual);
3434 /* 2 vectors each for LAN and RDMA (traffic + OICR), one for flow director */
3435 #define ICE_MIN_LAN_VECS 2
3436 #define ICE_MIN_RDMA_VECS 2
3437 #define ICE_MIN_VECS (ICE_MIN_LAN_VECS + ICE_MIN_RDMA_VECS + 1)
3438 
3439 		if (v_actual < ICE_MIN_LAN_VECS) {
3440 			/* error if we can't get minimum vectors */
3441 			pci_disable_msix(pf->pdev);
3442 			err = -ERANGE;
3443 			goto msix_err;
3444 		} else {
3445 			pf->num_lan_msix = ICE_MIN_LAN_VECS;
3446 		}
3447 	}
3448 
3449 	return v_actual;
3450 
3451 msix_err:
3452 	devm_kfree(dev, pf->msix_entries);
3453 	goto exit_err;
3454 
3455 no_hw_vecs_left_err:
3456 	dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
3457 		needed, v_left);
3458 	err = -ERANGE;
3459 exit_err:
3460 	pf->num_lan_msix = 0;
3461 	return err;
3462 }
3463 
3464 /**
3465  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3466  * @pf: board private structure
3467  */
3468 static void ice_dis_msix(struct ice_pf *pf)
3469 {
3470 	pci_disable_msix(pf->pdev);
3471 	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
3472 	pf->msix_entries = NULL;
3473 }
3474 
3475 /**
3476  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3477  * @pf: board private structure
3478  */
3479 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3480 {
3481 	ice_dis_msix(pf);
3482 
3483 	if (pf->irq_tracker) {
3484 		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
3485 		pf->irq_tracker = NULL;
3486 	}
3487 }
3488 
3489 /**
3490  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3491  * @pf: board private structure to initialize
3492  */
3493 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3494 {
3495 	int vectors;
3496 
3497 	vectors = ice_ena_msix_range(pf);
3498 
3499 	if (vectors < 0)
3500 		return vectors;
3501 
3502 	/* set up vector assignment tracking */
3503 	pf->irq_tracker =
3504 		devm_kzalloc(ice_pf_to_dev(pf), sizeof(*pf->irq_tracker) +
3505 			     (sizeof(u16) * vectors), GFP_KERNEL);
3506 	if (!pf->irq_tracker) {
3507 		ice_dis_msix(pf);
3508 		return -ENOMEM;
3509 	}
3510 
3511 	/* populate SW interrupts pool with number of OS granted IRQs. */
3512 	pf->num_avail_sw_msix = (u16)vectors;
3513 	pf->irq_tracker->num_entries = (u16)vectors;
3514 	pf->irq_tracker->end = pf->irq_tracker->num_entries;
3515 
3516 	return 0;
3517 }
3518 
3519 /**
3520  * ice_is_wol_supported - get NVM state of WoL
3521  * @pf: board private structure
3522  *
3523  * Check if WoL is supported based on the HW configuration.
3524  * Returns true if NVM supports and enables WoL for this port, false otherwise
3525  */
3526 bool ice_is_wol_supported(struct ice_pf *pf)
3527 {
3528 	struct ice_hw *hw = &pf->hw;
3529 	u16 wol_ctrl;
3530 
3531 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3532 	 * word) indicates WoL is not supported on the corresponding PF ID.
3533 	 */
3534 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3535 		return false;
3536 
3537 	return !(BIT(hw->pf_id) & wol_ctrl);
3538 }
3539 
3540 /**
3541  * ice_vsi_recfg_qs - Change the number of queues on a VSI
3542  * @vsi: VSI being changed
3543  * @new_rx: new number of Rx queues
3544  * @new_tx: new number of Tx queues
3545  *
3546  * Only change the number of queues if new_tx, or new_rx is non-0.
3547  *
3548  * Returns 0 on success.
3549  */
3550 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
3551 {
3552 	struct ice_pf *pf = vsi->back;
3553 	int err = 0, timeout = 50;
3554 
3555 	if (!new_rx && !new_tx)
3556 		return -EINVAL;
3557 
3558 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
3559 		timeout--;
3560 		if (!timeout)
3561 			return -EBUSY;
3562 		usleep_range(1000, 2000);
3563 	}
3564 
3565 	if (new_tx)
3566 		vsi->req_txq = (u16)new_tx;
3567 	if (new_rx)
3568 		vsi->req_rxq = (u16)new_rx;
3569 
3570 	/* set for the next time the netdev is started */
3571 	if (!netif_running(vsi->netdev)) {
3572 		ice_vsi_rebuild(vsi, false);
3573 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
3574 		goto done;
3575 	}
3576 
3577 	ice_vsi_close(vsi);
3578 	ice_vsi_rebuild(vsi, false);
3579 	ice_pf_dcb_recfg(pf);
3580 	ice_vsi_open(vsi);
3581 done:
3582 	clear_bit(__ICE_CFG_BUSY, pf->state);
3583 	return err;
3584 }
3585 
3586 /**
3587  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
3588  * @pf: PF to configure
3589  *
3590  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
3591  * VSI can still Tx/Rx VLAN tagged packets.
3592  */
3593 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
3594 {
3595 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
3596 	struct ice_vsi_ctx *ctxt;
3597 	enum ice_status status;
3598 	struct ice_hw *hw;
3599 
3600 	if (!vsi)
3601 		return;
3602 
3603 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
3604 	if (!ctxt)
3605 		return;
3606 
3607 	hw = &pf->hw;
3608 	ctxt->info = vsi->info;
3609 
3610 	ctxt->info.valid_sections =
3611 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
3612 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
3613 			    ICE_AQ_VSI_PROP_SW_VALID);
3614 
3615 	/* disable VLAN anti-spoof */
3616 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3617 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3618 
3619 	/* disable VLAN pruning and keep all other settings */
3620 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
3621 
3622 	/* allow all VLANs on Tx and don't strip on Rx */
3623 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL |
3624 		ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3625 
3626 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
3627 	if (status) {
3628 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %s aq_err %s\n",
3629 			ice_stat_str(status),
3630 			ice_aq_str(hw->adminq.sq_last_status));
3631 	} else {
3632 		vsi->info.sec_flags = ctxt->info.sec_flags;
3633 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
3634 		vsi->info.vlan_flags = ctxt->info.vlan_flags;
3635 	}
3636 
3637 	kfree(ctxt);
3638 }
3639 
3640 /**
3641  * ice_log_pkg_init - log result of DDP package load
3642  * @hw: pointer to hardware info
3643  * @status: status of package load
3644  */
3645 static void
3646 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status)
3647 {
3648 	struct ice_pf *pf = (struct ice_pf *)hw->back;
3649 	struct device *dev = ice_pf_to_dev(pf);
3650 
3651 	switch (*status) {
3652 	case ICE_SUCCESS:
3653 		/* The package download AdminQ command returned success because
3654 		 * this download succeeded or ICE_ERR_AQ_NO_WORK since there is
3655 		 * already a package loaded on the device.
3656 		 */
3657 		if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
3658 		    hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
3659 		    hw->pkg_ver.update == hw->active_pkg_ver.update &&
3660 		    hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
3661 		    !memcmp(hw->pkg_name, hw->active_pkg_name,
3662 			    sizeof(hw->pkg_name))) {
3663 			if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST)
3664 				dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
3665 					 hw->active_pkg_name,
3666 					 hw->active_pkg_ver.major,
3667 					 hw->active_pkg_ver.minor,
3668 					 hw->active_pkg_ver.update,
3669 					 hw->active_pkg_ver.draft);
3670 			else
3671 				dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
3672 					 hw->active_pkg_name,
3673 					 hw->active_pkg_ver.major,
3674 					 hw->active_pkg_ver.minor,
3675 					 hw->active_pkg_ver.update,
3676 					 hw->active_pkg_ver.draft);
3677 		} else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
3678 			   hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
3679 			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",
3680 				hw->active_pkg_name,
3681 				hw->active_pkg_ver.major,
3682 				hw->active_pkg_ver.minor,
3683 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3684 			*status = ICE_ERR_NOT_SUPPORTED;
3685 		} else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3686 			   hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
3687 			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",
3688 				 hw->active_pkg_name,
3689 				 hw->active_pkg_ver.major,
3690 				 hw->active_pkg_ver.minor,
3691 				 hw->active_pkg_ver.update,
3692 				 hw->active_pkg_ver.draft,
3693 				 hw->pkg_name,
3694 				 hw->pkg_ver.major,
3695 				 hw->pkg_ver.minor,
3696 				 hw->pkg_ver.update,
3697 				 hw->pkg_ver.draft);
3698 		} else {
3699 			dev_err(dev, "An unknown error occurred when loading the DDP package, please reboot the system.  If the problem persists, update the NVM.  Entering Safe Mode.\n");
3700 			*status = ICE_ERR_NOT_SUPPORTED;
3701 		}
3702 		break;
3703 	case ICE_ERR_FW_DDP_MISMATCH:
3704 		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");
3705 		break;
3706 	case ICE_ERR_BUF_TOO_SHORT:
3707 	case ICE_ERR_CFG:
3708 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
3709 		break;
3710 	case ICE_ERR_NOT_SUPPORTED:
3711 		/* Package File version not supported */
3712 		if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ ||
3713 		    (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3714 		     hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR))
3715 			dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
3716 		else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ ||
3717 			 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3718 			  hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR))
3719 			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",
3720 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3721 		break;
3722 	case ICE_ERR_AQ_ERROR:
3723 		switch (hw->pkg_dwnld_status) {
3724 		case ICE_AQ_RC_ENOSEC:
3725 		case ICE_AQ_RC_EBADSIG:
3726 			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");
3727 			return;
3728 		case ICE_AQ_RC_ESVN:
3729 			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");
3730 			return;
3731 		case ICE_AQ_RC_EBADMAN:
3732 		case ICE_AQ_RC_EBADBUF:
3733 			dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
3734 			/* poll for reset to complete */
3735 			if (ice_check_reset(hw))
3736 				dev_err(dev, "Error resetting device. Please reload the driver\n");
3737 			return;
3738 		default:
3739 			break;
3740 		}
3741 		fallthrough;
3742 	default:
3743 		dev_err(dev, "An unknown error (%d) occurred when loading the DDP package.  Entering Safe Mode.\n",
3744 			*status);
3745 		break;
3746 	}
3747 }
3748 
3749 /**
3750  * ice_load_pkg - load/reload the DDP Package file
3751  * @firmware: firmware structure when firmware requested or NULL for reload
3752  * @pf: pointer to the PF instance
3753  *
3754  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
3755  * initialize HW tables.
3756  */
3757 static void
3758 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
3759 {
3760 	enum ice_status status = ICE_ERR_PARAM;
3761 	struct device *dev = ice_pf_to_dev(pf);
3762 	struct ice_hw *hw = &pf->hw;
3763 
3764 	/* Load DDP Package */
3765 	if (firmware && !hw->pkg_copy) {
3766 		status = ice_copy_and_init_pkg(hw, firmware->data,
3767 					       firmware->size);
3768 		ice_log_pkg_init(hw, &status);
3769 	} else if (!firmware && hw->pkg_copy) {
3770 		/* Reload package during rebuild after CORER/GLOBR reset */
3771 		status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
3772 		ice_log_pkg_init(hw, &status);
3773 	} else {
3774 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
3775 	}
3776 
3777 	if (status) {
3778 		/* Safe Mode */
3779 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3780 		return;
3781 	}
3782 
3783 	/* Successful download package is the precondition for advanced
3784 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
3785 	 */
3786 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3787 }
3788 
3789 /**
3790  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
3791  * @pf: pointer to the PF structure
3792  *
3793  * There is no error returned here because the driver should be able to handle
3794  * 128 Byte cache lines, so we only print a warning in case issues are seen,
3795  * specifically with Tx.
3796  */
3797 static void ice_verify_cacheline_size(struct ice_pf *pf)
3798 {
3799 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
3800 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
3801 			 ICE_CACHE_LINE_BYTES);
3802 }
3803 
3804 /**
3805  * ice_send_version - update firmware with driver version
3806  * @pf: PF struct
3807  *
3808  * Returns ICE_SUCCESS on success, else error code
3809  */
3810 static enum ice_status ice_send_version(struct ice_pf *pf)
3811 {
3812 	struct ice_driver_ver dv;
3813 
3814 	dv.major_ver = 0xff;
3815 	dv.minor_ver = 0xff;
3816 	dv.build_ver = 0xff;
3817 	dv.subbuild_ver = 0;
3818 	strscpy((char *)dv.driver_string, UTS_RELEASE,
3819 		sizeof(dv.driver_string));
3820 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
3821 }
3822 
3823 /**
3824  * ice_init_fdir - Initialize flow director VSI and configuration
3825  * @pf: pointer to the PF instance
3826  *
3827  * returns 0 on success, negative on error
3828  */
3829 static int ice_init_fdir(struct ice_pf *pf)
3830 {
3831 	struct device *dev = ice_pf_to_dev(pf);
3832 	struct ice_vsi *ctrl_vsi;
3833 	int err;
3834 
3835 	/* Side Band Flow Director needs to have a control VSI.
3836 	 * Allocate it and store it in the PF.
3837 	 */
3838 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
3839 	if (!ctrl_vsi) {
3840 		dev_dbg(dev, "could not create control VSI\n");
3841 		return -ENOMEM;
3842 	}
3843 
3844 	err = ice_vsi_open_ctrl(ctrl_vsi);
3845 	if (err) {
3846 		dev_dbg(dev, "could not open control VSI\n");
3847 		goto err_vsi_open;
3848 	}
3849 
3850 	mutex_init(&pf->hw.fdir_fltr_lock);
3851 
3852 	err = ice_fdir_create_dflt_rules(pf);
3853 	if (err)
3854 		goto err_fdir_rule;
3855 
3856 	return 0;
3857 
3858 err_fdir_rule:
3859 	ice_fdir_release_flows(&pf->hw);
3860 	ice_vsi_close(ctrl_vsi);
3861 err_vsi_open:
3862 	ice_vsi_release(ctrl_vsi);
3863 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
3864 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
3865 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3866 	}
3867 	return err;
3868 }
3869 
3870 /**
3871  * ice_get_opt_fw_name - return optional firmware file name or NULL
3872  * @pf: pointer to the PF instance
3873  */
3874 static char *ice_get_opt_fw_name(struct ice_pf *pf)
3875 {
3876 	/* Optional firmware name same as default with additional dash
3877 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
3878 	 */
3879 	struct pci_dev *pdev = pf->pdev;
3880 	char *opt_fw_filename;
3881 	u64 dsn;
3882 
3883 	/* Determine the name of the optional file using the DSN (two
3884 	 * dwords following the start of the DSN Capability).
3885 	 */
3886 	dsn = pci_get_dsn(pdev);
3887 	if (!dsn)
3888 		return NULL;
3889 
3890 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
3891 	if (!opt_fw_filename)
3892 		return NULL;
3893 
3894 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
3895 		 ICE_DDP_PKG_PATH, dsn);
3896 
3897 	return opt_fw_filename;
3898 }
3899 
3900 /**
3901  * ice_request_fw - Device initialization routine
3902  * @pf: pointer to the PF instance
3903  */
3904 static void ice_request_fw(struct ice_pf *pf)
3905 {
3906 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
3907 	const struct firmware *firmware = NULL;
3908 	struct device *dev = ice_pf_to_dev(pf);
3909 	int err = 0;
3910 
3911 	/* optional device-specific DDP (if present) overrides the default DDP
3912 	 * package file. kernel logs a debug message if the file doesn't exist,
3913 	 * and warning messages for other errors.
3914 	 */
3915 	if (opt_fw_filename) {
3916 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
3917 		if (err) {
3918 			kfree(opt_fw_filename);
3919 			goto dflt_pkg_load;
3920 		}
3921 
3922 		/* request for firmware was successful. Download to device */
3923 		ice_load_pkg(firmware, pf);
3924 		kfree(opt_fw_filename);
3925 		release_firmware(firmware);
3926 		return;
3927 	}
3928 
3929 dflt_pkg_load:
3930 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
3931 	if (err) {
3932 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
3933 		return;
3934 	}
3935 
3936 	/* request for firmware was successful. Download to device */
3937 	ice_load_pkg(firmware, pf);
3938 	release_firmware(firmware);
3939 }
3940 
3941 /**
3942  * ice_print_wake_reason - show the wake up cause in the log
3943  * @pf: pointer to the PF struct
3944  */
3945 static void ice_print_wake_reason(struct ice_pf *pf)
3946 {
3947 	u32 wus = pf->wakeup_reason;
3948 	const char *wake_str;
3949 
3950 	/* if no wake event, nothing to print */
3951 	if (!wus)
3952 		return;
3953 
3954 	if (wus & PFPM_WUS_LNKC_M)
3955 		wake_str = "Link\n";
3956 	else if (wus & PFPM_WUS_MAG_M)
3957 		wake_str = "Magic Packet\n";
3958 	else if (wus & PFPM_WUS_MNG_M)
3959 		wake_str = "Management\n";
3960 	else if (wus & PFPM_WUS_FW_RST_WK_M)
3961 		wake_str = "Firmware Reset\n";
3962 	else
3963 		wake_str = "Unknown\n";
3964 
3965 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
3966 }
3967 
3968 /**
3969  * ice_probe - Device initialization routine
3970  * @pdev: PCI device information struct
3971  * @ent: entry in ice_pci_tbl
3972  *
3973  * Returns 0 on success, negative on failure
3974  */
3975 static int
3976 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
3977 {
3978 	struct device *dev = &pdev->dev;
3979 	struct ice_pf *pf;
3980 	struct ice_hw *hw;
3981 	int i, err;
3982 
3983 	/* this driver uses devres, see
3984 	 * Documentation/driver-api/driver-model/devres.rst
3985 	 */
3986 	err = pcim_enable_device(pdev);
3987 	if (err)
3988 		return err;
3989 
3990 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3991 	if (err) {
3992 		dev_err(dev, "BAR0 I/O map error %d\n", err);
3993 		return err;
3994 	}
3995 
3996 	pf = ice_allocate_pf(dev);
3997 	if (!pf)
3998 		return -ENOMEM;
3999 
4000 	/* set up for high or low DMA */
4001 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4002 	if (err)
4003 		err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
4004 	if (err) {
4005 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4006 		return err;
4007 	}
4008 
4009 	pci_enable_pcie_error_reporting(pdev);
4010 	pci_set_master(pdev);
4011 
4012 	pf->pdev = pdev;
4013 	pci_set_drvdata(pdev, pf);
4014 	set_bit(__ICE_DOWN, pf->state);
4015 	/* Disable service task until DOWN bit is cleared */
4016 	set_bit(__ICE_SERVICE_DIS, pf->state);
4017 
4018 	hw = &pf->hw;
4019 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4020 	pci_save_state(pdev);
4021 
4022 	hw->back = pf;
4023 	hw->vendor_id = pdev->vendor;
4024 	hw->device_id = pdev->device;
4025 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4026 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
4027 	hw->subsystem_device_id = pdev->subsystem_device;
4028 	hw->bus.device = PCI_SLOT(pdev->devfn);
4029 	hw->bus.func = PCI_FUNC(pdev->devfn);
4030 	ice_set_ctrlq_len(hw);
4031 
4032 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4033 
4034 	err = ice_devlink_register(pf);
4035 	if (err) {
4036 		dev_err(dev, "ice_devlink_register failed: %d\n", err);
4037 		goto err_exit_unroll;
4038 	}
4039 
4040 #ifndef CONFIG_DYNAMIC_DEBUG
4041 	if (debug < -1)
4042 		hw->debug_mask = debug;
4043 #endif
4044 
4045 	err = ice_init_hw(hw);
4046 	if (err) {
4047 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4048 		err = -EIO;
4049 		goto err_exit_unroll;
4050 	}
4051 
4052 	ice_request_fw(pf);
4053 
4054 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4055 	 * set in pf->state, which will cause ice_is_safe_mode to return
4056 	 * true
4057 	 */
4058 	if (ice_is_safe_mode(pf)) {
4059 		dev_err(dev, "Package download failed. Advanced features disabled - Device now in Safe Mode\n");
4060 		/* we already got function/device capabilities but these don't
4061 		 * reflect what the driver needs to do in safe mode. Instead of
4062 		 * adding conditional logic everywhere to ignore these
4063 		 * device/function capabilities, override them.
4064 		 */
4065 		ice_set_safe_mode_caps(hw);
4066 	}
4067 
4068 	err = ice_init_pf(pf);
4069 	if (err) {
4070 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4071 		goto err_init_pf_unroll;
4072 	}
4073 
4074 	ice_devlink_init_regions(pf);
4075 
4076 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4077 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4078 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4079 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4080 	i = 0;
4081 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4082 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4083 			pf->hw.tnl.valid_count[TNL_VXLAN];
4084 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4085 			UDP_TUNNEL_TYPE_VXLAN;
4086 		i++;
4087 	}
4088 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4089 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4090 			pf->hw.tnl.valid_count[TNL_GENEVE];
4091 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4092 			UDP_TUNNEL_TYPE_GENEVE;
4093 		i++;
4094 	}
4095 
4096 	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4097 	if (!pf->num_alloc_vsi) {
4098 		err = -EIO;
4099 		goto err_init_pf_unroll;
4100 	}
4101 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4102 		dev_warn(&pf->pdev->dev,
4103 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4104 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4105 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4106 	}
4107 
4108 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4109 			       GFP_KERNEL);
4110 	if (!pf->vsi) {
4111 		err = -ENOMEM;
4112 		goto err_init_pf_unroll;
4113 	}
4114 
4115 	err = ice_init_interrupt_scheme(pf);
4116 	if (err) {
4117 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4118 		err = -EIO;
4119 		goto err_init_vsi_unroll;
4120 	}
4121 
4122 	/* In case of MSIX we are going to setup the misc vector right here
4123 	 * to handle admin queue events etc. In case of legacy and MSI
4124 	 * the misc functionality and queue processing is combined in
4125 	 * the same vector and that gets setup at open.
4126 	 */
4127 	err = ice_req_irq_msix_misc(pf);
4128 	if (err) {
4129 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4130 		goto err_init_interrupt_unroll;
4131 	}
4132 
4133 	/* create switch struct for the switch element created by FW on boot */
4134 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4135 	if (!pf->first_sw) {
4136 		err = -ENOMEM;
4137 		goto err_msix_misc_unroll;
4138 	}
4139 
4140 	if (hw->evb_veb)
4141 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4142 	else
4143 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4144 
4145 	pf->first_sw->pf = pf;
4146 
4147 	/* record the sw_id available for later use */
4148 	pf->first_sw->sw_id = hw->port_info->sw_id;
4149 
4150 	err = ice_setup_pf_sw(pf);
4151 	if (err) {
4152 		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4153 		goto err_alloc_sw_unroll;
4154 	}
4155 
4156 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4157 
4158 	/* tell the firmware we are up */
4159 	err = ice_send_version(pf);
4160 	if (err) {
4161 		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4162 			UTS_RELEASE, err);
4163 		goto err_send_version_unroll;
4164 	}
4165 
4166 	/* since everything is good, start the service timer */
4167 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4168 
4169 	err = ice_init_link_events(pf->hw.port_info);
4170 	if (err) {
4171 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4172 		goto err_send_version_unroll;
4173 	}
4174 
4175 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4176 	if (err) {
4177 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4178 		goto err_send_version_unroll;
4179 	}
4180 
4181 	err = ice_update_link_info(pf->hw.port_info);
4182 	if (err) {
4183 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4184 		goto err_send_version_unroll;
4185 	}
4186 
4187 	ice_init_link_dflt_override(pf->hw.port_info);
4188 
4189 	/* if media available, initialize PHY settings */
4190 	if (pf->hw.port_info->phy.link_info.link_info &
4191 	    ICE_AQ_MEDIA_AVAILABLE) {
4192 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4193 		if (err) {
4194 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4195 			goto err_send_version_unroll;
4196 		}
4197 
4198 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4199 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4200 
4201 			if (vsi)
4202 				ice_configure_phy(vsi);
4203 		}
4204 	} else {
4205 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4206 	}
4207 
4208 	ice_verify_cacheline_size(pf);
4209 
4210 	/* Save wakeup reason register for later use */
4211 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4212 
4213 	/* check for a power management event */
4214 	ice_print_wake_reason(pf);
4215 
4216 	/* clear wake status, all bits */
4217 	wr32(hw, PFPM_WUS, U32_MAX);
4218 
4219 	/* Disable WoL at init, wait for user to enable */
4220 	device_set_wakeup_enable(dev, false);
4221 
4222 	if (ice_is_safe_mode(pf)) {
4223 		ice_set_safe_mode_vlan_cfg(pf);
4224 		goto probe_done;
4225 	}
4226 
4227 	/* initialize DDP driven features */
4228 
4229 	/* Note: Flow director init failure is non-fatal to load */
4230 	if (ice_init_fdir(pf))
4231 		dev_err(dev, "could not initialize flow director\n");
4232 
4233 	/* Note: DCB init failure is non-fatal to load */
4234 	if (ice_init_pf_dcb(pf, false)) {
4235 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4236 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4237 	} else {
4238 		ice_cfg_lldp_mib_change(&pf->hw, true);
4239 	}
4240 
4241 	/* print PCI link speed and width */
4242 	pcie_print_link_status(pf->pdev);
4243 
4244 probe_done:
4245 	/* ready to go, so clear down state bit */
4246 	clear_bit(__ICE_DOWN, pf->state);
4247 	return 0;
4248 
4249 err_send_version_unroll:
4250 	ice_vsi_release_all(pf);
4251 err_alloc_sw_unroll:
4252 	ice_devlink_destroy_port(pf);
4253 	set_bit(__ICE_SERVICE_DIS, pf->state);
4254 	set_bit(__ICE_DOWN, pf->state);
4255 	devm_kfree(dev, pf->first_sw);
4256 err_msix_misc_unroll:
4257 	ice_free_irq_msix_misc(pf);
4258 err_init_interrupt_unroll:
4259 	ice_clear_interrupt_scheme(pf);
4260 err_init_vsi_unroll:
4261 	devm_kfree(dev, pf->vsi);
4262 err_init_pf_unroll:
4263 	ice_deinit_pf(pf);
4264 	ice_devlink_destroy_regions(pf);
4265 	ice_deinit_hw(hw);
4266 err_exit_unroll:
4267 	ice_devlink_unregister(pf);
4268 	pci_disable_pcie_error_reporting(pdev);
4269 	pci_disable_device(pdev);
4270 	return err;
4271 }
4272 
4273 /**
4274  * ice_set_wake - enable or disable Wake on LAN
4275  * @pf: pointer to the PF struct
4276  *
4277  * Simple helper for WoL control
4278  */
4279 static void ice_set_wake(struct ice_pf *pf)
4280 {
4281 	struct ice_hw *hw = &pf->hw;
4282 	bool wol = pf->wol_ena;
4283 
4284 	/* clear wake state, otherwise new wake events won't fire */
4285 	wr32(hw, PFPM_WUS, U32_MAX);
4286 
4287 	/* enable / disable APM wake up, no RMW needed */
4288 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4289 
4290 	/* set magic packet filter enabled */
4291 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4292 }
4293 
4294 /**
4295  * ice_setup_magic_mc_wake - setup device to wake on multicast magic packet
4296  * @pf: pointer to the PF struct
4297  *
4298  * Issue firmware command to enable multicast magic wake, making
4299  * sure that any locally administered address (LAA) is used for
4300  * wake, and that PF reset doesn't undo the LAA.
4301  */
4302 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4303 {
4304 	struct device *dev = ice_pf_to_dev(pf);
4305 	struct ice_hw *hw = &pf->hw;
4306 	enum ice_status status;
4307 	u8 mac_addr[ETH_ALEN];
4308 	struct ice_vsi *vsi;
4309 	u8 flags;
4310 
4311 	if (!pf->wol_ena)
4312 		return;
4313 
4314 	vsi = ice_get_main_vsi(pf);
4315 	if (!vsi)
4316 		return;
4317 
4318 	/* Get current MAC address in case it's an LAA */
4319 	if (vsi->netdev)
4320 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4321 	else
4322 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4323 
4324 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4325 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4326 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4327 
4328 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4329 	if (status)
4330 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %s aq_err %s\n",
4331 			ice_stat_str(status),
4332 			ice_aq_str(hw->adminq.sq_last_status));
4333 }
4334 
4335 /**
4336  * ice_remove - Device removal routine
4337  * @pdev: PCI device information struct
4338  */
4339 static void ice_remove(struct pci_dev *pdev)
4340 {
4341 	struct ice_pf *pf = pci_get_drvdata(pdev);
4342 	int i;
4343 
4344 	if (!pf)
4345 		return;
4346 
4347 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4348 		if (!ice_is_reset_in_progress(pf->state))
4349 			break;
4350 		msleep(100);
4351 	}
4352 
4353 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4354 		set_bit(__ICE_VF_RESETS_DISABLED, pf->state);
4355 		ice_free_vfs(pf);
4356 	}
4357 
4358 	set_bit(__ICE_DOWN, pf->state);
4359 	ice_service_task_stop(pf);
4360 
4361 	ice_aq_cancel_waiting_tasks(pf);
4362 
4363 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4364 	if (!ice_is_safe_mode(pf))
4365 		ice_remove_arfs(pf);
4366 	ice_setup_mc_magic_wake(pf);
4367 	ice_devlink_destroy_port(pf);
4368 	ice_vsi_release_all(pf);
4369 	ice_set_wake(pf);
4370 	ice_free_irq_msix_misc(pf);
4371 	ice_for_each_vsi(pf, i) {
4372 		if (!pf->vsi[i])
4373 			continue;
4374 		ice_vsi_free_q_vectors(pf->vsi[i]);
4375 	}
4376 	ice_deinit_pf(pf);
4377 	ice_devlink_destroy_regions(pf);
4378 	ice_deinit_hw(&pf->hw);
4379 	ice_devlink_unregister(pf);
4380 
4381 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
4382 	 * do it via ice_schedule_reset() since there is no need to rebuild
4383 	 * and the service task is already stopped.
4384 	 */
4385 	ice_reset(&pf->hw, ICE_RESET_PFR);
4386 	pci_wait_for_pending_transaction(pdev);
4387 	ice_clear_interrupt_scheme(pf);
4388 	pci_disable_pcie_error_reporting(pdev);
4389 	pci_disable_device(pdev);
4390 }
4391 
4392 /**
4393  * ice_shutdown - PCI callback for shutting down device
4394  * @pdev: PCI device information struct
4395  */
4396 static void ice_shutdown(struct pci_dev *pdev)
4397 {
4398 	struct ice_pf *pf = pci_get_drvdata(pdev);
4399 
4400 	ice_remove(pdev);
4401 
4402 	if (system_state == SYSTEM_POWER_OFF) {
4403 		pci_wake_from_d3(pdev, pf->wol_ena);
4404 		pci_set_power_state(pdev, PCI_D3hot);
4405 	}
4406 }
4407 
4408 #ifdef CONFIG_PM
4409 /**
4410  * ice_prepare_for_shutdown - prep for PCI shutdown
4411  * @pf: board private structure
4412  *
4413  * Inform or close all dependent features in prep for PCI device shutdown
4414  */
4415 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4416 {
4417 	struct ice_hw *hw = &pf->hw;
4418 	u32 v;
4419 
4420 	/* Notify VFs of impending reset */
4421 	if (ice_check_sq_alive(hw, &hw->mailboxq))
4422 		ice_vc_notify_reset(pf);
4423 
4424 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4425 
4426 	/* disable the VSIs and their queues that are not already DOWN */
4427 	ice_pf_dis_all_vsi(pf, false);
4428 
4429 	ice_for_each_vsi(pf, v)
4430 		if (pf->vsi[v])
4431 			pf->vsi[v]->vsi_num = 0;
4432 
4433 	ice_shutdown_all_ctrlq(hw);
4434 }
4435 
4436 /**
4437  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4438  * @pf: board private structure to reinitialize
4439  *
4440  * This routine reinitialize interrupt scheme that was cleared during
4441  * power management suspend callback.
4442  *
4443  * This should be called during resume routine to re-allocate the q_vectors
4444  * and reacquire interrupts.
4445  */
4446 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4447 {
4448 	struct device *dev = ice_pf_to_dev(pf);
4449 	int ret, v;
4450 
4451 	/* Since we clear MSIX flag during suspend, we need to
4452 	 * set it back during resume...
4453 	 */
4454 
4455 	ret = ice_init_interrupt_scheme(pf);
4456 	if (ret) {
4457 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4458 		return ret;
4459 	}
4460 
4461 	/* Remap vectors and rings, after successful re-init interrupts */
4462 	ice_for_each_vsi(pf, v) {
4463 		if (!pf->vsi[v])
4464 			continue;
4465 
4466 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4467 		if (ret)
4468 			goto err_reinit;
4469 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4470 	}
4471 
4472 	ret = ice_req_irq_msix_misc(pf);
4473 	if (ret) {
4474 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4475 			ret);
4476 		goto err_reinit;
4477 	}
4478 
4479 	return 0;
4480 
4481 err_reinit:
4482 	while (v--)
4483 		if (pf->vsi[v])
4484 			ice_vsi_free_q_vectors(pf->vsi[v]);
4485 
4486 	return ret;
4487 }
4488 
4489 /**
4490  * ice_suspend
4491  * @dev: generic device information structure
4492  *
4493  * Power Management callback to quiesce the device and prepare
4494  * for D3 transition.
4495  */
4496 static int __maybe_unused ice_suspend(struct device *dev)
4497 {
4498 	struct pci_dev *pdev = to_pci_dev(dev);
4499 	struct ice_pf *pf;
4500 	int disabled, v;
4501 
4502 	pf = pci_get_drvdata(pdev);
4503 
4504 	if (!ice_pf_state_is_nominal(pf)) {
4505 		dev_err(dev, "Device is not ready, no need to suspend it\n");
4506 		return -EBUSY;
4507 	}
4508 
4509 	/* Stop watchdog tasks until resume completion.
4510 	 * Even though it is most likely that the service task is
4511 	 * disabled if the device is suspended or down, the service task's
4512 	 * state is controlled by a different state bit, and we should
4513 	 * store and honor whatever state that bit is in at this point.
4514 	 */
4515 	disabled = ice_service_task_stop(pf);
4516 
4517 	/* Already suspended?, then there is nothing to do */
4518 	if (test_and_set_bit(__ICE_SUSPENDED, pf->state)) {
4519 		if (!disabled)
4520 			ice_service_task_restart(pf);
4521 		return 0;
4522 	}
4523 
4524 	if (test_bit(__ICE_DOWN, pf->state) ||
4525 	    ice_is_reset_in_progress(pf->state)) {
4526 		dev_err(dev, "can't suspend device in reset or already down\n");
4527 		if (!disabled)
4528 			ice_service_task_restart(pf);
4529 		return 0;
4530 	}
4531 
4532 	ice_setup_mc_magic_wake(pf);
4533 
4534 	ice_prepare_for_shutdown(pf);
4535 
4536 	ice_set_wake(pf);
4537 
4538 	/* Free vectors, clear the interrupt scheme and release IRQs
4539 	 * for proper hibernation, especially with large number of CPUs.
4540 	 * Otherwise hibernation might fail when mapping all the vectors back
4541 	 * to CPU0.
4542 	 */
4543 	ice_free_irq_msix_misc(pf);
4544 	ice_for_each_vsi(pf, v) {
4545 		if (!pf->vsi[v])
4546 			continue;
4547 		ice_vsi_free_q_vectors(pf->vsi[v]);
4548 	}
4549 	ice_clear_interrupt_scheme(pf);
4550 
4551 	pci_wake_from_d3(pdev, pf->wol_ena);
4552 	pci_set_power_state(pdev, PCI_D3hot);
4553 	return 0;
4554 }
4555 
4556 /**
4557  * ice_resume - PM callback for waking up from D3
4558  * @dev: generic device information structure
4559  */
4560 static int __maybe_unused ice_resume(struct device *dev)
4561 {
4562 	struct pci_dev *pdev = to_pci_dev(dev);
4563 	enum ice_reset_req reset_type;
4564 	struct ice_pf *pf;
4565 	struct ice_hw *hw;
4566 	int ret;
4567 
4568 	pci_set_power_state(pdev, PCI_D0);
4569 	pci_restore_state(pdev);
4570 	pci_save_state(pdev);
4571 
4572 	if (!pci_device_is_present(pdev))
4573 		return -ENODEV;
4574 
4575 	ret = pci_enable_device_mem(pdev);
4576 	if (ret) {
4577 		dev_err(dev, "Cannot enable device after suspend\n");
4578 		return ret;
4579 	}
4580 
4581 	pf = pci_get_drvdata(pdev);
4582 	hw = &pf->hw;
4583 
4584 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4585 	ice_print_wake_reason(pf);
4586 
4587 	/* We cleared the interrupt scheme when we suspended, so we need to
4588 	 * restore it now to resume device functionality.
4589 	 */
4590 	ret = ice_reinit_interrupt_scheme(pf);
4591 	if (ret)
4592 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
4593 
4594 	clear_bit(__ICE_DOWN, pf->state);
4595 	/* Now perform PF reset and rebuild */
4596 	reset_type = ICE_RESET_PFR;
4597 	/* re-enable service task for reset, but allow reset to schedule it */
4598 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4599 
4600 	if (ice_schedule_reset(pf, reset_type))
4601 		dev_err(dev, "Reset during resume failed.\n");
4602 
4603 	clear_bit(__ICE_SUSPENDED, pf->state);
4604 	ice_service_task_restart(pf);
4605 
4606 	/* Restart the service task */
4607 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4608 
4609 	return 0;
4610 }
4611 #endif /* CONFIG_PM */
4612 
4613 /**
4614  * ice_pci_err_detected - warning that PCI error has been detected
4615  * @pdev: PCI device information struct
4616  * @err: the type of PCI error
4617  *
4618  * Called to warn that something happened on the PCI bus and the error handling
4619  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
4620  */
4621 static pci_ers_result_t
4622 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
4623 {
4624 	struct ice_pf *pf = pci_get_drvdata(pdev);
4625 
4626 	if (!pf) {
4627 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
4628 			__func__, err);
4629 		return PCI_ERS_RESULT_DISCONNECT;
4630 	}
4631 
4632 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4633 		ice_service_task_stop(pf);
4634 
4635 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4636 			set_bit(__ICE_PFR_REQ, pf->state);
4637 			ice_prepare_for_reset(pf);
4638 		}
4639 	}
4640 
4641 	return PCI_ERS_RESULT_NEED_RESET;
4642 }
4643 
4644 /**
4645  * ice_pci_err_slot_reset - a PCI slot reset has just happened
4646  * @pdev: PCI device information struct
4647  *
4648  * Called to determine if the driver can recover from the PCI slot reset by
4649  * using a register read to determine if the device is recoverable.
4650  */
4651 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
4652 {
4653 	struct ice_pf *pf = pci_get_drvdata(pdev);
4654 	pci_ers_result_t result;
4655 	int err;
4656 	u32 reg;
4657 
4658 	err = pci_enable_device_mem(pdev);
4659 	if (err) {
4660 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
4661 			err);
4662 		result = PCI_ERS_RESULT_DISCONNECT;
4663 	} else {
4664 		pci_set_master(pdev);
4665 		pci_restore_state(pdev);
4666 		pci_save_state(pdev);
4667 		pci_wake_from_d3(pdev, false);
4668 
4669 		/* Check for life */
4670 		reg = rd32(&pf->hw, GLGEN_RTRIG);
4671 		if (!reg)
4672 			result = PCI_ERS_RESULT_RECOVERED;
4673 		else
4674 			result = PCI_ERS_RESULT_DISCONNECT;
4675 	}
4676 
4677 	err = pci_aer_clear_nonfatal_status(pdev);
4678 	if (err)
4679 		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
4680 			err);
4681 		/* non-fatal, continue */
4682 
4683 	return result;
4684 }
4685 
4686 /**
4687  * ice_pci_err_resume - restart operations after PCI error recovery
4688  * @pdev: PCI device information struct
4689  *
4690  * Called to allow the driver to bring things back up after PCI error and/or
4691  * reset recovery have finished
4692  */
4693 static void ice_pci_err_resume(struct pci_dev *pdev)
4694 {
4695 	struct ice_pf *pf = pci_get_drvdata(pdev);
4696 
4697 	if (!pf) {
4698 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
4699 			__func__);
4700 		return;
4701 	}
4702 
4703 	if (test_bit(__ICE_SUSPENDED, pf->state)) {
4704 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
4705 			__func__);
4706 		return;
4707 	}
4708 
4709 	ice_restore_all_vfs_msi_state(pdev);
4710 
4711 	ice_do_reset(pf, ICE_RESET_PFR);
4712 	ice_service_task_restart(pf);
4713 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4714 }
4715 
4716 /**
4717  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
4718  * @pdev: PCI device information struct
4719  */
4720 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
4721 {
4722 	struct ice_pf *pf = pci_get_drvdata(pdev);
4723 
4724 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4725 		ice_service_task_stop(pf);
4726 
4727 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4728 			set_bit(__ICE_PFR_REQ, pf->state);
4729 			ice_prepare_for_reset(pf);
4730 		}
4731 	}
4732 }
4733 
4734 /**
4735  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
4736  * @pdev: PCI device information struct
4737  */
4738 static void ice_pci_err_reset_done(struct pci_dev *pdev)
4739 {
4740 	ice_pci_err_resume(pdev);
4741 }
4742 
4743 /* ice_pci_tbl - PCI Device ID Table
4744  *
4745  * Wildcard entries (PCI_ANY_ID) should come last
4746  * Last entry must be all 0s
4747  *
4748  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
4749  *   Class, Class Mask, private data (not used) }
4750  */
4751 static const struct pci_device_id ice_pci_tbl[] = {
4752 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
4753 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
4754 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
4755 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
4756 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
4757 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
4758 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
4759 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
4760 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
4761 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
4762 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
4763 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
4764 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
4765 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
4766 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
4767 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
4768 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
4769 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
4770 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
4771 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
4772 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
4773 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
4774 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
4775 	/* required last entry */
4776 	{ 0, }
4777 };
4778 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
4779 
4780 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
4781 
4782 static const struct pci_error_handlers ice_pci_err_handler = {
4783 	.error_detected = ice_pci_err_detected,
4784 	.slot_reset = ice_pci_err_slot_reset,
4785 	.reset_prepare = ice_pci_err_reset_prepare,
4786 	.reset_done = ice_pci_err_reset_done,
4787 	.resume = ice_pci_err_resume
4788 };
4789 
4790 static struct pci_driver ice_driver = {
4791 	.name = KBUILD_MODNAME,
4792 	.id_table = ice_pci_tbl,
4793 	.probe = ice_probe,
4794 	.remove = ice_remove,
4795 #ifdef CONFIG_PM
4796 	.driver.pm = &ice_pm_ops,
4797 #endif /* CONFIG_PM */
4798 	.shutdown = ice_shutdown,
4799 	.sriov_configure = ice_sriov_configure,
4800 	.err_handler = &ice_pci_err_handler
4801 };
4802 
4803 /**
4804  * ice_module_init - Driver registration routine
4805  *
4806  * ice_module_init is the first routine called when the driver is
4807  * loaded. All it does is register with the PCI subsystem.
4808  */
4809 static int __init ice_module_init(void)
4810 {
4811 	int status;
4812 
4813 	pr_info("%s\n", ice_driver_string);
4814 	pr_info("%s\n", ice_copyright);
4815 
4816 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
4817 	if (!ice_wq) {
4818 		pr_err("Failed to create workqueue\n");
4819 		return -ENOMEM;
4820 	}
4821 
4822 	status = pci_register_driver(&ice_driver);
4823 	if (status) {
4824 		pr_err("failed to register PCI driver, err %d\n", status);
4825 		destroy_workqueue(ice_wq);
4826 	}
4827 
4828 	return status;
4829 }
4830 module_init(ice_module_init);
4831 
4832 /**
4833  * ice_module_exit - Driver exit cleanup routine
4834  *
4835  * ice_module_exit is called just before the driver is removed
4836  * from memory.
4837  */
4838 static void __exit ice_module_exit(void)
4839 {
4840 	pci_unregister_driver(&ice_driver);
4841 	destroy_workqueue(ice_wq);
4842 	pr_info("module unloaded\n");
4843 }
4844 module_exit(ice_module_exit);
4845 
4846 /**
4847  * ice_set_mac_address - NDO callback to set MAC address
4848  * @netdev: network interface device structure
4849  * @pi: pointer to an address structure
4850  *
4851  * Returns 0 on success, negative on failure
4852  */
4853 static int ice_set_mac_address(struct net_device *netdev, void *pi)
4854 {
4855 	struct ice_netdev_priv *np = netdev_priv(netdev);
4856 	struct ice_vsi *vsi = np->vsi;
4857 	struct ice_pf *pf = vsi->back;
4858 	struct ice_hw *hw = &pf->hw;
4859 	struct sockaddr *addr = pi;
4860 	enum ice_status status;
4861 	u8 flags = 0;
4862 	int err = 0;
4863 	u8 *mac;
4864 
4865 	mac = (u8 *)addr->sa_data;
4866 
4867 	if (!is_valid_ether_addr(mac))
4868 		return -EADDRNOTAVAIL;
4869 
4870 	if (ether_addr_equal(netdev->dev_addr, mac)) {
4871 		netdev_warn(netdev, "already using mac %pM\n", mac);
4872 		return 0;
4873 	}
4874 
4875 	if (test_bit(__ICE_DOWN, pf->state) ||
4876 	    ice_is_reset_in_progress(pf->state)) {
4877 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
4878 			   mac);
4879 		return -EBUSY;
4880 	}
4881 
4882 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
4883 	status = ice_fltr_remove_mac(vsi, netdev->dev_addr, ICE_FWD_TO_VSI);
4884 	if (status && status != ICE_ERR_DOES_NOT_EXIST) {
4885 		err = -EADDRNOTAVAIL;
4886 		goto err_update_filters;
4887 	}
4888 
4889 	/* Add filter for new MAC. If filter exists, just return success */
4890 	status = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
4891 	if (status == ICE_ERR_ALREADY_EXISTS) {
4892 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
4893 		return 0;
4894 	}
4895 
4896 	/* error if the new filter addition failed */
4897 	if (status)
4898 		err = -EADDRNOTAVAIL;
4899 
4900 err_update_filters:
4901 	if (err) {
4902 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
4903 			   mac);
4904 		return err;
4905 	}
4906 
4907 	/* change the netdev's MAC address */
4908 	memcpy(netdev->dev_addr, mac, netdev->addr_len);
4909 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
4910 		   netdev->dev_addr);
4911 
4912 	/* write new MAC address to the firmware */
4913 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
4914 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
4915 	if (status) {
4916 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %s\n",
4917 			   mac, ice_stat_str(status));
4918 	}
4919 	return 0;
4920 }
4921 
4922 /**
4923  * ice_set_rx_mode - NDO callback to set the netdev filters
4924  * @netdev: network interface device structure
4925  */
4926 static void ice_set_rx_mode(struct net_device *netdev)
4927 {
4928 	struct ice_netdev_priv *np = netdev_priv(netdev);
4929 	struct ice_vsi *vsi = np->vsi;
4930 
4931 	if (!vsi)
4932 		return;
4933 
4934 	/* Set the flags to synchronize filters
4935 	 * ndo_set_rx_mode may be triggered even without a change in netdev
4936 	 * flags
4937 	 */
4938 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
4939 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
4940 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
4941 
4942 	/* schedule our worker thread which will take care of
4943 	 * applying the new filter changes
4944 	 */
4945 	ice_service_task_schedule(vsi->back);
4946 }
4947 
4948 /**
4949  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
4950  * @netdev: network interface device structure
4951  * @queue_index: Queue ID
4952  * @maxrate: maximum bandwidth in Mbps
4953  */
4954 static int
4955 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
4956 {
4957 	struct ice_netdev_priv *np = netdev_priv(netdev);
4958 	struct ice_vsi *vsi = np->vsi;
4959 	enum ice_status status;
4960 	u16 q_handle;
4961 	u8 tc;
4962 
4963 	/* Validate maxrate requested is within permitted range */
4964 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
4965 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
4966 			   maxrate, queue_index);
4967 		return -EINVAL;
4968 	}
4969 
4970 	q_handle = vsi->tx_rings[queue_index]->q_handle;
4971 	tc = ice_dcb_get_tc(vsi, queue_index);
4972 
4973 	/* Set BW back to default, when user set maxrate to 0 */
4974 	if (!maxrate)
4975 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
4976 					       q_handle, ICE_MAX_BW);
4977 	else
4978 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
4979 					  q_handle, ICE_MAX_BW, maxrate * 1000);
4980 	if (status) {
4981 		netdev_err(netdev, "Unable to set Tx max rate, error %s\n",
4982 			   ice_stat_str(status));
4983 		return -EIO;
4984 	}
4985 
4986 	return 0;
4987 }
4988 
4989 /**
4990  * ice_fdb_add - add an entry to the hardware database
4991  * @ndm: the input from the stack
4992  * @tb: pointer to array of nladdr (unused)
4993  * @dev: the net device pointer
4994  * @addr: the MAC address entry being added
4995  * @vid: VLAN ID
4996  * @flags: instructions from stack about fdb operation
4997  * @extack: netlink extended ack
4998  */
4999 static int
5000 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5001 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5002 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5003 {
5004 	int err;
5005 
5006 	if (vid) {
5007 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5008 		return -EINVAL;
5009 	}
5010 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5011 		netdev_err(dev, "FDB only supports static addresses\n");
5012 		return -EINVAL;
5013 	}
5014 
5015 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5016 		err = dev_uc_add_excl(dev, addr);
5017 	else if (is_multicast_ether_addr(addr))
5018 		err = dev_mc_add_excl(dev, addr);
5019 	else
5020 		err = -EINVAL;
5021 
5022 	/* Only return duplicate errors if NLM_F_EXCL is set */
5023 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5024 		err = 0;
5025 
5026 	return err;
5027 }
5028 
5029 /**
5030  * ice_fdb_del - delete an entry from the hardware database
5031  * @ndm: the input from the stack
5032  * @tb: pointer to array of nladdr (unused)
5033  * @dev: the net device pointer
5034  * @addr: the MAC address entry being added
5035  * @vid: VLAN ID
5036  */
5037 static int
5038 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5039 	    struct net_device *dev, const unsigned char *addr,
5040 	    __always_unused u16 vid)
5041 {
5042 	int err;
5043 
5044 	if (ndm->ndm_state & NUD_PERMANENT) {
5045 		netdev_err(dev, "FDB only supports static addresses\n");
5046 		return -EINVAL;
5047 	}
5048 
5049 	if (is_unicast_ether_addr(addr))
5050 		err = dev_uc_del(dev, addr);
5051 	else if (is_multicast_ether_addr(addr))
5052 		err = dev_mc_del(dev, addr);
5053 	else
5054 		err = -EINVAL;
5055 
5056 	return err;
5057 }
5058 
5059 /**
5060  * ice_set_features - set the netdev feature flags
5061  * @netdev: ptr to the netdev being adjusted
5062  * @features: the feature set that the stack is suggesting
5063  */
5064 static int
5065 ice_set_features(struct net_device *netdev, netdev_features_t features)
5066 {
5067 	struct ice_netdev_priv *np = netdev_priv(netdev);
5068 	struct ice_vsi *vsi = np->vsi;
5069 	struct ice_pf *pf = vsi->back;
5070 	int ret = 0;
5071 
5072 	/* Don't set any netdev advanced features with device in Safe Mode */
5073 	if (ice_is_safe_mode(vsi->back)) {
5074 		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5075 		return ret;
5076 	}
5077 
5078 	/* Do not change setting during reset */
5079 	if (ice_is_reset_in_progress(pf->state)) {
5080 		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5081 		return -EBUSY;
5082 	}
5083 
5084 	/* Multiple features can be changed in one call so keep features in
5085 	 * separate if/else statements to guarantee each feature is checked
5086 	 */
5087 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5088 		ret = ice_vsi_manage_rss_lut(vsi, true);
5089 	else if (!(features & NETIF_F_RXHASH) &&
5090 		 netdev->features & NETIF_F_RXHASH)
5091 		ret = ice_vsi_manage_rss_lut(vsi, false);
5092 
5093 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5094 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5095 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5096 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5097 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5098 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
5099 
5100 	if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5101 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5102 		ret = ice_vsi_manage_vlan_insertion(vsi);
5103 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5104 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5105 		ret = ice_vsi_manage_vlan_insertion(vsi);
5106 
5107 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5108 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5109 		ret = ice_cfg_vlan_pruning(vsi, true, false);
5110 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5111 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5112 		ret = ice_cfg_vlan_pruning(vsi, false, false);
5113 
5114 	if ((features & NETIF_F_NTUPLE) &&
5115 	    !(netdev->features & NETIF_F_NTUPLE)) {
5116 		ice_vsi_manage_fdir(vsi, true);
5117 		ice_init_arfs(vsi);
5118 	} else if (!(features & NETIF_F_NTUPLE) &&
5119 		 (netdev->features & NETIF_F_NTUPLE)) {
5120 		ice_vsi_manage_fdir(vsi, false);
5121 		ice_clear_arfs(vsi);
5122 	}
5123 
5124 	return ret;
5125 }
5126 
5127 /**
5128  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5129  * @vsi: VSI to setup VLAN properties for
5130  */
5131 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5132 {
5133 	int ret = 0;
5134 
5135 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5136 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5137 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5138 		ret = ice_vsi_manage_vlan_insertion(vsi);
5139 
5140 	return ret;
5141 }
5142 
5143 /**
5144  * ice_vsi_cfg - Setup the VSI
5145  * @vsi: the VSI being configured
5146  *
5147  * Return 0 on success and negative value on error
5148  */
5149 int ice_vsi_cfg(struct ice_vsi *vsi)
5150 {
5151 	int err;
5152 
5153 	if (vsi->netdev) {
5154 		ice_set_rx_mode(vsi->netdev);
5155 
5156 		err = ice_vsi_vlan_setup(vsi);
5157 
5158 		if (err)
5159 			return err;
5160 	}
5161 	ice_vsi_cfg_dcb_rings(vsi);
5162 
5163 	err = ice_vsi_cfg_lan_txqs(vsi);
5164 	if (!err && ice_is_xdp_ena_vsi(vsi))
5165 		err = ice_vsi_cfg_xdp_txqs(vsi);
5166 	if (!err)
5167 		err = ice_vsi_cfg_rxqs(vsi);
5168 
5169 	return err;
5170 }
5171 
5172 /**
5173  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5174  * @vsi: the VSI being configured
5175  */
5176 static void ice_napi_enable_all(struct ice_vsi *vsi)
5177 {
5178 	int q_idx;
5179 
5180 	if (!vsi->netdev)
5181 		return;
5182 
5183 	ice_for_each_q_vector(vsi, q_idx) {
5184 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5185 
5186 		if (q_vector->rx.ring || q_vector->tx.ring)
5187 			napi_enable(&q_vector->napi);
5188 	}
5189 }
5190 
5191 /**
5192  * ice_up_complete - Finish the last steps of bringing up a connection
5193  * @vsi: The VSI being configured
5194  *
5195  * Return 0 on success and negative value on error
5196  */
5197 static int ice_up_complete(struct ice_vsi *vsi)
5198 {
5199 	struct ice_pf *pf = vsi->back;
5200 	int err;
5201 
5202 	ice_vsi_cfg_msix(vsi);
5203 
5204 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
5205 	 * Tx queue group list was configured and the context bits were
5206 	 * programmed using ice_vsi_cfg_txqs
5207 	 */
5208 	err = ice_vsi_start_all_rx_rings(vsi);
5209 	if (err)
5210 		return err;
5211 
5212 	clear_bit(__ICE_DOWN, vsi->state);
5213 	ice_napi_enable_all(vsi);
5214 	ice_vsi_ena_irq(vsi);
5215 
5216 	if (vsi->port_info &&
5217 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5218 	    vsi->netdev) {
5219 		ice_print_link_msg(vsi, true);
5220 		netif_tx_start_all_queues(vsi->netdev);
5221 		netif_carrier_on(vsi->netdev);
5222 	}
5223 
5224 	ice_service_task_schedule(pf);
5225 
5226 	return 0;
5227 }
5228 
5229 /**
5230  * ice_up - Bring the connection back up after being down
5231  * @vsi: VSI being configured
5232  */
5233 int ice_up(struct ice_vsi *vsi)
5234 {
5235 	int err;
5236 
5237 	err = ice_vsi_cfg(vsi);
5238 	if (!err)
5239 		err = ice_up_complete(vsi);
5240 
5241 	return err;
5242 }
5243 
5244 /**
5245  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5246  * @ring: Tx or Rx ring to read stats from
5247  * @pkts: packets stats counter
5248  * @bytes: bytes stats counter
5249  *
5250  * This function fetches stats from the ring considering the atomic operations
5251  * that needs to be performed to read u64 values in 32 bit machine.
5252  */
5253 static void
5254 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
5255 {
5256 	unsigned int start;
5257 	*pkts = 0;
5258 	*bytes = 0;
5259 
5260 	if (!ring)
5261 		return;
5262 	do {
5263 		start = u64_stats_fetch_begin_irq(&ring->syncp);
5264 		*pkts = ring->stats.pkts;
5265 		*bytes = ring->stats.bytes;
5266 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
5267 }
5268 
5269 /**
5270  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5271  * @vsi: the VSI to be updated
5272  * @rings: rings to work on
5273  * @count: number of rings
5274  */
5275 static void
5276 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi, struct ice_ring **rings,
5277 			     u16 count)
5278 {
5279 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5280 	u16 i;
5281 
5282 	for (i = 0; i < count; i++) {
5283 		struct ice_ring *ring;
5284 		u64 pkts, bytes;
5285 
5286 		ring = READ_ONCE(rings[i]);
5287 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5288 		vsi_stats->tx_packets += pkts;
5289 		vsi_stats->tx_bytes += bytes;
5290 		vsi->tx_restart += ring->tx_stats.restart_q;
5291 		vsi->tx_busy += ring->tx_stats.tx_busy;
5292 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
5293 	}
5294 }
5295 
5296 /**
5297  * ice_update_vsi_ring_stats - Update VSI stats counters
5298  * @vsi: the VSI to be updated
5299  */
5300 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5301 {
5302 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5303 	struct ice_ring *ring;
5304 	u64 pkts, bytes;
5305 	int i;
5306 
5307 	/* reset netdev stats */
5308 	vsi_stats->tx_packets = 0;
5309 	vsi_stats->tx_bytes = 0;
5310 	vsi_stats->rx_packets = 0;
5311 	vsi_stats->rx_bytes = 0;
5312 
5313 	/* reset non-netdev (extended) stats */
5314 	vsi->tx_restart = 0;
5315 	vsi->tx_busy = 0;
5316 	vsi->tx_linearize = 0;
5317 	vsi->rx_buf_failed = 0;
5318 	vsi->rx_page_failed = 0;
5319 	vsi->rx_gro_dropped = 0;
5320 
5321 	rcu_read_lock();
5322 
5323 	/* update Tx rings counters */
5324 	ice_update_vsi_tx_ring_stats(vsi, vsi->tx_rings, vsi->num_txq);
5325 
5326 	/* update Rx rings counters */
5327 	ice_for_each_rxq(vsi, i) {
5328 		ring = READ_ONCE(vsi->rx_rings[i]);
5329 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5330 		vsi_stats->rx_packets += pkts;
5331 		vsi_stats->rx_bytes += bytes;
5332 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5333 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5334 		vsi->rx_gro_dropped += ring->rx_stats.gro_dropped;
5335 	}
5336 
5337 	/* update XDP Tx rings counters */
5338 	if (ice_is_xdp_ena_vsi(vsi))
5339 		ice_update_vsi_tx_ring_stats(vsi, vsi->xdp_rings,
5340 					     vsi->num_xdp_txq);
5341 
5342 	rcu_read_unlock();
5343 }
5344 
5345 /**
5346  * ice_update_vsi_stats - Update VSI stats counters
5347  * @vsi: the VSI to be updated
5348  */
5349 void ice_update_vsi_stats(struct ice_vsi *vsi)
5350 {
5351 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
5352 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
5353 	struct ice_pf *pf = vsi->back;
5354 
5355 	if (test_bit(__ICE_DOWN, vsi->state) ||
5356 	    test_bit(__ICE_CFG_BUSY, pf->state))
5357 		return;
5358 
5359 	/* get stats as recorded by Tx/Rx rings */
5360 	ice_update_vsi_ring_stats(vsi);
5361 
5362 	/* get VSI stats as recorded by the hardware */
5363 	ice_update_eth_stats(vsi);
5364 
5365 	cur_ns->tx_errors = cur_es->tx_errors;
5366 	cur_ns->rx_dropped = cur_es->rx_discards + vsi->rx_gro_dropped;
5367 	cur_ns->tx_dropped = cur_es->tx_discards;
5368 	cur_ns->multicast = cur_es->rx_multicast;
5369 
5370 	/* update some more netdev stats if this is main VSI */
5371 	if (vsi->type == ICE_VSI_PF) {
5372 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
5373 		cur_ns->rx_errors = pf->stats.crc_errors +
5374 				    pf->stats.illegal_bytes +
5375 				    pf->stats.rx_len_errors +
5376 				    pf->stats.rx_undersize +
5377 				    pf->hw_csum_rx_error +
5378 				    pf->stats.rx_jabber +
5379 				    pf->stats.rx_fragments +
5380 				    pf->stats.rx_oversize;
5381 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
5382 		/* record drops from the port level */
5383 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
5384 	}
5385 }
5386 
5387 /**
5388  * ice_update_pf_stats - Update PF port stats counters
5389  * @pf: PF whose stats needs to be updated
5390  */
5391 void ice_update_pf_stats(struct ice_pf *pf)
5392 {
5393 	struct ice_hw_port_stats *prev_ps, *cur_ps;
5394 	struct ice_hw *hw = &pf->hw;
5395 	u16 fd_ctr_base;
5396 	u8 port;
5397 
5398 	port = hw->port_info->lport;
5399 	prev_ps = &pf->stats_prev;
5400 	cur_ps = &pf->stats;
5401 
5402 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
5403 			  &prev_ps->eth.rx_bytes,
5404 			  &cur_ps->eth.rx_bytes);
5405 
5406 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
5407 			  &prev_ps->eth.rx_unicast,
5408 			  &cur_ps->eth.rx_unicast);
5409 
5410 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
5411 			  &prev_ps->eth.rx_multicast,
5412 			  &cur_ps->eth.rx_multicast);
5413 
5414 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
5415 			  &prev_ps->eth.rx_broadcast,
5416 			  &cur_ps->eth.rx_broadcast);
5417 
5418 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
5419 			  &prev_ps->eth.rx_discards,
5420 			  &cur_ps->eth.rx_discards);
5421 
5422 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
5423 			  &prev_ps->eth.tx_bytes,
5424 			  &cur_ps->eth.tx_bytes);
5425 
5426 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
5427 			  &prev_ps->eth.tx_unicast,
5428 			  &cur_ps->eth.tx_unicast);
5429 
5430 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
5431 			  &prev_ps->eth.tx_multicast,
5432 			  &cur_ps->eth.tx_multicast);
5433 
5434 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
5435 			  &prev_ps->eth.tx_broadcast,
5436 			  &cur_ps->eth.tx_broadcast);
5437 
5438 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
5439 			  &prev_ps->tx_dropped_link_down,
5440 			  &cur_ps->tx_dropped_link_down);
5441 
5442 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
5443 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
5444 
5445 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
5446 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
5447 
5448 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
5449 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
5450 
5451 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
5452 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
5453 
5454 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
5455 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
5456 
5457 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
5458 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5459 
5460 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
5461 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5462 
5463 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
5464 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
5465 
5466 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
5467 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
5468 
5469 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
5470 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
5471 
5472 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
5473 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
5474 
5475 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
5476 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5477 
5478 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
5479 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5480 
5481 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
5482 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5483 
5484 	fd_ctr_base = hw->fd_ctr_base;
5485 
5486 	ice_stat_update40(hw,
5487 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
5488 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
5489 			  &cur_ps->fd_sb_match);
5490 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
5491 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5492 
5493 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
5494 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5495 
5496 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
5497 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5498 
5499 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
5500 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5501 
5502 	ice_update_dcb_stats(pf);
5503 
5504 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
5505 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
5506 
5507 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
5508 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5509 
5510 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
5511 			  &prev_ps->mac_local_faults,
5512 			  &cur_ps->mac_local_faults);
5513 
5514 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
5515 			  &prev_ps->mac_remote_faults,
5516 			  &cur_ps->mac_remote_faults);
5517 
5518 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
5519 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5520 
5521 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
5522 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5523 
5524 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
5525 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5526 
5527 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
5528 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5529 
5530 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
5531 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5532 
5533 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
5534 
5535 	pf->stat_prev_loaded = true;
5536 }
5537 
5538 /**
5539  * ice_get_stats64 - get statistics for network device structure
5540  * @netdev: network interface device structure
5541  * @stats: main device statistics structure
5542  */
5543 static
5544 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5545 {
5546 	struct ice_netdev_priv *np = netdev_priv(netdev);
5547 	struct rtnl_link_stats64 *vsi_stats;
5548 	struct ice_vsi *vsi = np->vsi;
5549 
5550 	vsi_stats = &vsi->net_stats;
5551 
5552 	if (!vsi->num_txq || !vsi->num_rxq)
5553 		return;
5554 
5555 	/* netdev packet/byte stats come from ring counter. These are obtained
5556 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
5557 	 * But, only call the update routine and read the registers if VSI is
5558 	 * not down.
5559 	 */
5560 	if (!test_bit(__ICE_DOWN, vsi->state))
5561 		ice_update_vsi_ring_stats(vsi);
5562 	stats->tx_packets = vsi_stats->tx_packets;
5563 	stats->tx_bytes = vsi_stats->tx_bytes;
5564 	stats->rx_packets = vsi_stats->rx_packets;
5565 	stats->rx_bytes = vsi_stats->rx_bytes;
5566 
5567 	/* The rest of the stats can be read from the hardware but instead we
5568 	 * just return values that the watchdog task has already obtained from
5569 	 * the hardware.
5570 	 */
5571 	stats->multicast = vsi_stats->multicast;
5572 	stats->tx_errors = vsi_stats->tx_errors;
5573 	stats->tx_dropped = vsi_stats->tx_dropped;
5574 	stats->rx_errors = vsi_stats->rx_errors;
5575 	stats->rx_dropped = vsi_stats->rx_dropped;
5576 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5577 	stats->rx_length_errors = vsi_stats->rx_length_errors;
5578 }
5579 
5580 /**
5581  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5582  * @vsi: VSI having NAPI disabled
5583  */
5584 static void ice_napi_disable_all(struct ice_vsi *vsi)
5585 {
5586 	int q_idx;
5587 
5588 	if (!vsi->netdev)
5589 		return;
5590 
5591 	ice_for_each_q_vector(vsi, q_idx) {
5592 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5593 
5594 		if (q_vector->rx.ring || q_vector->tx.ring)
5595 			napi_disable(&q_vector->napi);
5596 	}
5597 }
5598 
5599 /**
5600  * ice_down - Shutdown the connection
5601  * @vsi: The VSI being stopped
5602  */
5603 int ice_down(struct ice_vsi *vsi)
5604 {
5605 	int i, tx_err, rx_err, link_err = 0;
5606 
5607 	/* Caller of this function is expected to set the
5608 	 * vsi->state __ICE_DOWN bit
5609 	 */
5610 	if (vsi->netdev) {
5611 		netif_carrier_off(vsi->netdev);
5612 		netif_tx_disable(vsi->netdev);
5613 	}
5614 
5615 	ice_vsi_dis_irq(vsi);
5616 
5617 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
5618 	if (tx_err)
5619 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
5620 			   vsi->vsi_num, tx_err);
5621 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
5622 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
5623 		if (tx_err)
5624 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
5625 				   vsi->vsi_num, tx_err);
5626 	}
5627 
5628 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
5629 	if (rx_err)
5630 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
5631 			   vsi->vsi_num, rx_err);
5632 
5633 	ice_napi_disable_all(vsi);
5634 
5635 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
5636 		link_err = ice_force_phys_link_state(vsi, false);
5637 		if (link_err)
5638 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
5639 				   vsi->vsi_num, link_err);
5640 	}
5641 
5642 	ice_for_each_txq(vsi, i)
5643 		ice_clean_tx_ring(vsi->tx_rings[i]);
5644 
5645 	ice_for_each_rxq(vsi, i)
5646 		ice_clean_rx_ring(vsi->rx_rings[i]);
5647 
5648 	if (tx_err || rx_err || link_err) {
5649 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5650 			   vsi->vsi_num, vsi->vsw->sw_id);
5651 		return -EIO;
5652 	}
5653 
5654 	return 0;
5655 }
5656 
5657 /**
5658  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5659  * @vsi: VSI having resources allocated
5660  *
5661  * Return 0 on success, negative on failure
5662  */
5663 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5664 {
5665 	int i, err = 0;
5666 
5667 	if (!vsi->num_txq) {
5668 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
5669 			vsi->vsi_num);
5670 		return -EINVAL;
5671 	}
5672 
5673 	ice_for_each_txq(vsi, i) {
5674 		struct ice_ring *ring = vsi->tx_rings[i];
5675 
5676 		if (!ring)
5677 			return -EINVAL;
5678 
5679 		ring->netdev = vsi->netdev;
5680 		err = ice_setup_tx_ring(ring);
5681 		if (err)
5682 			break;
5683 	}
5684 
5685 	return err;
5686 }
5687 
5688 /**
5689  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5690  * @vsi: VSI having resources allocated
5691  *
5692  * Return 0 on success, negative on failure
5693  */
5694 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5695 {
5696 	int i, err = 0;
5697 
5698 	if (!vsi->num_rxq) {
5699 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
5700 			vsi->vsi_num);
5701 		return -EINVAL;
5702 	}
5703 
5704 	ice_for_each_rxq(vsi, i) {
5705 		struct ice_ring *ring = vsi->rx_rings[i];
5706 
5707 		if (!ring)
5708 			return -EINVAL;
5709 
5710 		ring->netdev = vsi->netdev;
5711 		err = ice_setup_rx_ring(ring);
5712 		if (err)
5713 			break;
5714 	}
5715 
5716 	return err;
5717 }
5718 
5719 /**
5720  * ice_vsi_open_ctrl - open control VSI for use
5721  * @vsi: the VSI to open
5722  *
5723  * Initialization of the Control VSI
5724  *
5725  * Returns 0 on success, negative value on error
5726  */
5727 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
5728 {
5729 	char int_name[ICE_INT_NAME_STR_LEN];
5730 	struct ice_pf *pf = vsi->back;
5731 	struct device *dev;
5732 	int err;
5733 
5734 	dev = ice_pf_to_dev(pf);
5735 	/* allocate descriptors */
5736 	err = ice_vsi_setup_tx_rings(vsi);
5737 	if (err)
5738 		goto err_setup_tx;
5739 
5740 	err = ice_vsi_setup_rx_rings(vsi);
5741 	if (err)
5742 		goto err_setup_rx;
5743 
5744 	err = ice_vsi_cfg(vsi);
5745 	if (err)
5746 		goto err_setup_rx;
5747 
5748 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
5749 		 dev_driver_string(dev), dev_name(dev));
5750 	err = ice_vsi_req_irq_msix(vsi, int_name);
5751 	if (err)
5752 		goto err_setup_rx;
5753 
5754 	ice_vsi_cfg_msix(vsi);
5755 
5756 	err = ice_vsi_start_all_rx_rings(vsi);
5757 	if (err)
5758 		goto err_up_complete;
5759 
5760 	clear_bit(__ICE_DOWN, vsi->state);
5761 	ice_vsi_ena_irq(vsi);
5762 
5763 	return 0;
5764 
5765 err_up_complete:
5766 	ice_down(vsi);
5767 err_setup_rx:
5768 	ice_vsi_free_rx_rings(vsi);
5769 err_setup_tx:
5770 	ice_vsi_free_tx_rings(vsi);
5771 
5772 	return err;
5773 }
5774 
5775 /**
5776  * ice_vsi_open - Called when a network interface is made active
5777  * @vsi: the VSI to open
5778  *
5779  * Initialization of the VSI
5780  *
5781  * Returns 0 on success, negative value on error
5782  */
5783 static int ice_vsi_open(struct ice_vsi *vsi)
5784 {
5785 	char int_name[ICE_INT_NAME_STR_LEN];
5786 	struct ice_pf *pf = vsi->back;
5787 	int err;
5788 
5789 	/* allocate descriptors */
5790 	err = ice_vsi_setup_tx_rings(vsi);
5791 	if (err)
5792 		goto err_setup_tx;
5793 
5794 	err = ice_vsi_setup_rx_rings(vsi);
5795 	if (err)
5796 		goto err_setup_rx;
5797 
5798 	err = ice_vsi_cfg(vsi);
5799 	if (err)
5800 		goto err_setup_rx;
5801 
5802 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5803 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
5804 	err = ice_vsi_req_irq_msix(vsi, int_name);
5805 	if (err)
5806 		goto err_setup_rx;
5807 
5808 	/* Notify the stack of the actual queue counts. */
5809 	err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5810 	if (err)
5811 		goto err_set_qs;
5812 
5813 	err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5814 	if (err)
5815 		goto err_set_qs;
5816 
5817 	err = ice_up_complete(vsi);
5818 	if (err)
5819 		goto err_up_complete;
5820 
5821 	return 0;
5822 
5823 err_up_complete:
5824 	ice_down(vsi);
5825 err_set_qs:
5826 	ice_vsi_free_irq(vsi);
5827 err_setup_rx:
5828 	ice_vsi_free_rx_rings(vsi);
5829 err_setup_tx:
5830 	ice_vsi_free_tx_rings(vsi);
5831 
5832 	return err;
5833 }
5834 
5835 /**
5836  * ice_vsi_release_all - Delete all VSIs
5837  * @pf: PF from which all VSIs are being removed
5838  */
5839 static void ice_vsi_release_all(struct ice_pf *pf)
5840 {
5841 	int err, i;
5842 
5843 	if (!pf->vsi)
5844 		return;
5845 
5846 	ice_for_each_vsi(pf, i) {
5847 		if (!pf->vsi[i])
5848 			continue;
5849 
5850 		err = ice_vsi_release(pf->vsi[i]);
5851 		if (err)
5852 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5853 				i, err, pf->vsi[i]->vsi_num);
5854 	}
5855 }
5856 
5857 /**
5858  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
5859  * @pf: pointer to the PF instance
5860  * @type: VSI type to rebuild
5861  *
5862  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
5863  */
5864 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
5865 {
5866 	struct device *dev = ice_pf_to_dev(pf);
5867 	enum ice_status status;
5868 	int i, err;
5869 
5870 	ice_for_each_vsi(pf, i) {
5871 		struct ice_vsi *vsi = pf->vsi[i];
5872 
5873 		if (!vsi || vsi->type != type)
5874 			continue;
5875 
5876 		/* rebuild the VSI */
5877 		err = ice_vsi_rebuild(vsi, true);
5878 		if (err) {
5879 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
5880 				err, vsi->idx, ice_vsi_type_str(type));
5881 			return err;
5882 		}
5883 
5884 		/* replay filters for the VSI */
5885 		status = ice_replay_vsi(&pf->hw, vsi->idx);
5886 		if (status) {
5887 			dev_err(dev, "replay VSI failed, status %s, VSI index %d, type %s\n",
5888 				ice_stat_str(status), vsi->idx,
5889 				ice_vsi_type_str(type));
5890 			return -EIO;
5891 		}
5892 
5893 		/* Re-map HW VSI number, using VSI handle that has been
5894 		 * previously validated in ice_replay_vsi() call above
5895 		 */
5896 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
5897 
5898 		/* enable the VSI */
5899 		err = ice_ena_vsi(vsi, false);
5900 		if (err) {
5901 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
5902 				err, vsi->idx, ice_vsi_type_str(type));
5903 			return err;
5904 		}
5905 
5906 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
5907 			 ice_vsi_type_str(type));
5908 	}
5909 
5910 	return 0;
5911 }
5912 
5913 /**
5914  * ice_update_pf_netdev_link - Update PF netdev link status
5915  * @pf: pointer to the PF instance
5916  */
5917 static void ice_update_pf_netdev_link(struct ice_pf *pf)
5918 {
5919 	bool link_up;
5920 	int i;
5921 
5922 	ice_for_each_vsi(pf, i) {
5923 		struct ice_vsi *vsi = pf->vsi[i];
5924 
5925 		if (!vsi || vsi->type != ICE_VSI_PF)
5926 			return;
5927 
5928 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
5929 		if (link_up) {
5930 			netif_carrier_on(pf->vsi[i]->netdev);
5931 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
5932 		} else {
5933 			netif_carrier_off(pf->vsi[i]->netdev);
5934 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
5935 		}
5936 	}
5937 }
5938 
5939 /**
5940  * ice_rebuild - rebuild after reset
5941  * @pf: PF to rebuild
5942  * @reset_type: type of reset
5943  *
5944  * Do not rebuild VF VSI in this flow because that is already handled via
5945  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
5946  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
5947  * to reset/rebuild all the VF VSI twice.
5948  */
5949 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
5950 {
5951 	struct device *dev = ice_pf_to_dev(pf);
5952 	struct ice_hw *hw = &pf->hw;
5953 	enum ice_status ret;
5954 	int err;
5955 
5956 	if (test_bit(__ICE_DOWN, pf->state))
5957 		goto clear_recovery;
5958 
5959 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
5960 
5961 	ret = ice_init_all_ctrlq(hw);
5962 	if (ret) {
5963 		dev_err(dev, "control queues init failed %s\n",
5964 			ice_stat_str(ret));
5965 		goto err_init_ctrlq;
5966 	}
5967 
5968 	/* if DDP was previously loaded successfully */
5969 	if (!ice_is_safe_mode(pf)) {
5970 		/* reload the SW DB of filter tables */
5971 		if (reset_type == ICE_RESET_PFR)
5972 			ice_fill_blk_tbls(hw);
5973 		else
5974 			/* Reload DDP Package after CORER/GLOBR reset */
5975 			ice_load_pkg(NULL, pf);
5976 	}
5977 
5978 	ret = ice_clear_pf_cfg(hw);
5979 	if (ret) {
5980 		dev_err(dev, "clear PF configuration failed %s\n",
5981 			ice_stat_str(ret));
5982 		goto err_init_ctrlq;
5983 	}
5984 
5985 	if (pf->first_sw->dflt_vsi_ena)
5986 		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
5987 	/* clear the default VSI configuration if it exists */
5988 	pf->first_sw->dflt_vsi = NULL;
5989 	pf->first_sw->dflt_vsi_ena = false;
5990 
5991 	ice_clear_pxe_mode(hw);
5992 
5993 	ret = ice_get_caps(hw);
5994 	if (ret) {
5995 		dev_err(dev, "ice_get_caps failed %s\n", ice_stat_str(ret));
5996 		goto err_init_ctrlq;
5997 	}
5998 
5999 	ret = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
6000 	if (ret) {
6001 		dev_err(dev, "set_mac_cfg failed %s\n", ice_stat_str(ret));
6002 		goto err_init_ctrlq;
6003 	}
6004 
6005 	err = ice_sched_init_port(hw->port_info);
6006 	if (err)
6007 		goto err_sched_init_port;
6008 
6009 	/* start misc vector */
6010 	err = ice_req_irq_msix_misc(pf);
6011 	if (err) {
6012 		dev_err(dev, "misc vector setup failed: %d\n", err);
6013 		goto err_sched_init_port;
6014 	}
6015 
6016 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6017 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
6018 		if (!rd32(hw, PFQF_FD_SIZE)) {
6019 			u16 unused, guar, b_effort;
6020 
6021 			guar = hw->func_caps.fd_fltr_guar;
6022 			b_effort = hw->func_caps.fd_fltr_best_effort;
6023 
6024 			/* force guaranteed filter pool for PF */
6025 			ice_alloc_fd_guar_item(hw, &unused, guar);
6026 			/* force shared filter pool for PF */
6027 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6028 		}
6029 	}
6030 
6031 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6032 		ice_dcb_rebuild(pf);
6033 
6034 	/* rebuild PF VSI */
6035 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6036 	if (err) {
6037 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6038 		goto err_vsi_rebuild;
6039 	}
6040 
6041 	/* If Flow Director is active */
6042 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6043 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6044 		if (err) {
6045 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
6046 			goto err_vsi_rebuild;
6047 		}
6048 
6049 		/* replay HW Flow Director recipes */
6050 		if (hw->fdir_prof)
6051 			ice_fdir_replay_flows(hw);
6052 
6053 		/* replay Flow Director filters */
6054 		ice_fdir_replay_fltrs(pf);
6055 
6056 		ice_rebuild_arfs(pf);
6057 	}
6058 
6059 	ice_update_pf_netdev_link(pf);
6060 
6061 	/* tell the firmware we are up */
6062 	ret = ice_send_version(pf);
6063 	if (ret) {
6064 		dev_err(dev, "Rebuild failed due to error sending driver version: %s\n",
6065 			ice_stat_str(ret));
6066 		goto err_vsi_rebuild;
6067 	}
6068 
6069 	ice_replay_post(hw);
6070 
6071 	/* if we get here, reset flow is successful */
6072 	clear_bit(__ICE_RESET_FAILED, pf->state);
6073 	return;
6074 
6075 err_vsi_rebuild:
6076 err_sched_init_port:
6077 	ice_sched_cleanup_all(hw);
6078 err_init_ctrlq:
6079 	ice_shutdown_all_ctrlq(hw);
6080 	set_bit(__ICE_RESET_FAILED, pf->state);
6081 clear_recovery:
6082 	/* set this bit in PF state to control service task scheduling */
6083 	set_bit(__ICE_NEEDS_RESTART, pf->state);
6084 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
6085 }
6086 
6087 /**
6088  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6089  * @vsi: Pointer to VSI structure
6090  */
6091 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6092 {
6093 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6094 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6095 	else
6096 		return ICE_RXBUF_3072;
6097 }
6098 
6099 /**
6100  * ice_change_mtu - NDO callback to change the MTU
6101  * @netdev: network interface device structure
6102  * @new_mtu: new value for maximum frame size
6103  *
6104  * Returns 0 on success, negative on failure
6105  */
6106 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6107 {
6108 	struct ice_netdev_priv *np = netdev_priv(netdev);
6109 	struct ice_vsi *vsi = np->vsi;
6110 	struct ice_pf *pf = vsi->back;
6111 	u8 count = 0;
6112 
6113 	if (new_mtu == (int)netdev->mtu) {
6114 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6115 		return 0;
6116 	}
6117 
6118 	if (ice_is_xdp_ena_vsi(vsi)) {
6119 		int frame_size = ice_max_xdp_frame_size(vsi);
6120 
6121 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6122 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
6123 				   frame_size - ICE_ETH_PKT_HDR_PAD);
6124 			return -EINVAL;
6125 		}
6126 	}
6127 
6128 	if (new_mtu < (int)netdev->min_mtu) {
6129 		netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
6130 			   netdev->min_mtu);
6131 		return -EINVAL;
6132 	} else if (new_mtu > (int)netdev->max_mtu) {
6133 		netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
6134 			   netdev->min_mtu);
6135 		return -EINVAL;
6136 	}
6137 	/* if a reset is in progress, wait for some time for it to complete */
6138 	do {
6139 		if (ice_is_reset_in_progress(pf->state)) {
6140 			count++;
6141 			usleep_range(1000, 2000);
6142 		} else {
6143 			break;
6144 		}
6145 
6146 	} while (count < 100);
6147 
6148 	if (count == 100) {
6149 		netdev_err(netdev, "can't change MTU. Device is busy\n");
6150 		return -EBUSY;
6151 	}
6152 
6153 	netdev->mtu = (unsigned int)new_mtu;
6154 
6155 	/* if VSI is up, bring it down and then back up */
6156 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
6157 		int err;
6158 
6159 		err = ice_down(vsi);
6160 		if (err) {
6161 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6162 			return err;
6163 		}
6164 
6165 		err = ice_up(vsi);
6166 		if (err) {
6167 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6168 			return err;
6169 		}
6170 	}
6171 
6172 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6173 	return 0;
6174 }
6175 
6176 /**
6177  * ice_aq_str - convert AQ err code to a string
6178  * @aq_err: the AQ error code to convert
6179  */
6180 const char *ice_aq_str(enum ice_aq_err aq_err)
6181 {
6182 	switch (aq_err) {
6183 	case ICE_AQ_RC_OK:
6184 		return "OK";
6185 	case ICE_AQ_RC_EPERM:
6186 		return "ICE_AQ_RC_EPERM";
6187 	case ICE_AQ_RC_ENOENT:
6188 		return "ICE_AQ_RC_ENOENT";
6189 	case ICE_AQ_RC_ENOMEM:
6190 		return "ICE_AQ_RC_ENOMEM";
6191 	case ICE_AQ_RC_EBUSY:
6192 		return "ICE_AQ_RC_EBUSY";
6193 	case ICE_AQ_RC_EEXIST:
6194 		return "ICE_AQ_RC_EEXIST";
6195 	case ICE_AQ_RC_EINVAL:
6196 		return "ICE_AQ_RC_EINVAL";
6197 	case ICE_AQ_RC_ENOSPC:
6198 		return "ICE_AQ_RC_ENOSPC";
6199 	case ICE_AQ_RC_ENOSYS:
6200 		return "ICE_AQ_RC_ENOSYS";
6201 	case ICE_AQ_RC_EMODE:
6202 		return "ICE_AQ_RC_EMODE";
6203 	case ICE_AQ_RC_ENOSEC:
6204 		return "ICE_AQ_RC_ENOSEC";
6205 	case ICE_AQ_RC_EBADSIG:
6206 		return "ICE_AQ_RC_EBADSIG";
6207 	case ICE_AQ_RC_ESVN:
6208 		return "ICE_AQ_RC_ESVN";
6209 	case ICE_AQ_RC_EBADMAN:
6210 		return "ICE_AQ_RC_EBADMAN";
6211 	case ICE_AQ_RC_EBADBUF:
6212 		return "ICE_AQ_RC_EBADBUF";
6213 	}
6214 
6215 	return "ICE_AQ_RC_UNKNOWN";
6216 }
6217 
6218 /**
6219  * ice_stat_str - convert status err code to a string
6220  * @stat_err: the status error code to convert
6221  */
6222 const char *ice_stat_str(enum ice_status stat_err)
6223 {
6224 	switch (stat_err) {
6225 	case ICE_SUCCESS:
6226 		return "OK";
6227 	case ICE_ERR_PARAM:
6228 		return "ICE_ERR_PARAM";
6229 	case ICE_ERR_NOT_IMPL:
6230 		return "ICE_ERR_NOT_IMPL";
6231 	case ICE_ERR_NOT_READY:
6232 		return "ICE_ERR_NOT_READY";
6233 	case ICE_ERR_NOT_SUPPORTED:
6234 		return "ICE_ERR_NOT_SUPPORTED";
6235 	case ICE_ERR_BAD_PTR:
6236 		return "ICE_ERR_BAD_PTR";
6237 	case ICE_ERR_INVAL_SIZE:
6238 		return "ICE_ERR_INVAL_SIZE";
6239 	case ICE_ERR_DEVICE_NOT_SUPPORTED:
6240 		return "ICE_ERR_DEVICE_NOT_SUPPORTED";
6241 	case ICE_ERR_RESET_FAILED:
6242 		return "ICE_ERR_RESET_FAILED";
6243 	case ICE_ERR_FW_API_VER:
6244 		return "ICE_ERR_FW_API_VER";
6245 	case ICE_ERR_NO_MEMORY:
6246 		return "ICE_ERR_NO_MEMORY";
6247 	case ICE_ERR_CFG:
6248 		return "ICE_ERR_CFG";
6249 	case ICE_ERR_OUT_OF_RANGE:
6250 		return "ICE_ERR_OUT_OF_RANGE";
6251 	case ICE_ERR_ALREADY_EXISTS:
6252 		return "ICE_ERR_ALREADY_EXISTS";
6253 	case ICE_ERR_NVM_CHECKSUM:
6254 		return "ICE_ERR_NVM_CHECKSUM";
6255 	case ICE_ERR_BUF_TOO_SHORT:
6256 		return "ICE_ERR_BUF_TOO_SHORT";
6257 	case ICE_ERR_NVM_BLANK_MODE:
6258 		return "ICE_ERR_NVM_BLANK_MODE";
6259 	case ICE_ERR_IN_USE:
6260 		return "ICE_ERR_IN_USE";
6261 	case ICE_ERR_MAX_LIMIT:
6262 		return "ICE_ERR_MAX_LIMIT";
6263 	case ICE_ERR_RESET_ONGOING:
6264 		return "ICE_ERR_RESET_ONGOING";
6265 	case ICE_ERR_HW_TABLE:
6266 		return "ICE_ERR_HW_TABLE";
6267 	case ICE_ERR_DOES_NOT_EXIST:
6268 		return "ICE_ERR_DOES_NOT_EXIST";
6269 	case ICE_ERR_FW_DDP_MISMATCH:
6270 		return "ICE_ERR_FW_DDP_MISMATCH";
6271 	case ICE_ERR_AQ_ERROR:
6272 		return "ICE_ERR_AQ_ERROR";
6273 	case ICE_ERR_AQ_TIMEOUT:
6274 		return "ICE_ERR_AQ_TIMEOUT";
6275 	case ICE_ERR_AQ_FULL:
6276 		return "ICE_ERR_AQ_FULL";
6277 	case ICE_ERR_AQ_NO_WORK:
6278 		return "ICE_ERR_AQ_NO_WORK";
6279 	case ICE_ERR_AQ_EMPTY:
6280 		return "ICE_ERR_AQ_EMPTY";
6281 	case ICE_ERR_AQ_FW_CRITICAL:
6282 		return "ICE_ERR_AQ_FW_CRITICAL";
6283 	}
6284 
6285 	return "ICE_ERR_UNKNOWN";
6286 }
6287 
6288 /**
6289  * ice_set_rss - Set RSS keys and lut
6290  * @vsi: Pointer to VSI structure
6291  * @seed: RSS hash seed
6292  * @lut: Lookup table
6293  * @lut_size: Lookup table size
6294  *
6295  * Returns 0 on success, negative on failure
6296  */
6297 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6298 {
6299 	struct ice_pf *pf = vsi->back;
6300 	struct ice_hw *hw = &pf->hw;
6301 	enum ice_status status;
6302 	struct device *dev;
6303 
6304 	dev = ice_pf_to_dev(pf);
6305 	if (seed) {
6306 		struct ice_aqc_get_set_rss_keys *buf =
6307 				  (struct ice_aqc_get_set_rss_keys *)seed;
6308 
6309 		status = ice_aq_set_rss_key(hw, vsi->idx, buf);
6310 
6311 		if (status) {
6312 			dev_err(dev, "Cannot set RSS key, err %s aq_err %s\n",
6313 				ice_stat_str(status),
6314 				ice_aq_str(hw->adminq.sq_last_status));
6315 			return -EIO;
6316 		}
6317 	}
6318 
6319 	if (lut) {
6320 		status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6321 					    lut, lut_size);
6322 		if (status) {
6323 			dev_err(dev, "Cannot set RSS lut, err %s aq_err %s\n",
6324 				ice_stat_str(status),
6325 				ice_aq_str(hw->adminq.sq_last_status));
6326 			return -EIO;
6327 		}
6328 	}
6329 
6330 	return 0;
6331 }
6332 
6333 /**
6334  * ice_get_rss - Get RSS keys and lut
6335  * @vsi: Pointer to VSI structure
6336  * @seed: Buffer to store the keys
6337  * @lut: Buffer to store the lookup table entries
6338  * @lut_size: Size of buffer to store the lookup table entries
6339  *
6340  * Returns 0 on success, negative on failure
6341  */
6342 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6343 {
6344 	struct ice_pf *pf = vsi->back;
6345 	struct ice_hw *hw = &pf->hw;
6346 	enum ice_status status;
6347 	struct device *dev;
6348 
6349 	dev = ice_pf_to_dev(pf);
6350 	if (seed) {
6351 		struct ice_aqc_get_set_rss_keys *buf =
6352 				  (struct ice_aqc_get_set_rss_keys *)seed;
6353 
6354 		status = ice_aq_get_rss_key(hw, vsi->idx, buf);
6355 		if (status) {
6356 			dev_err(dev, "Cannot get RSS key, err %s aq_err %s\n",
6357 				ice_stat_str(status),
6358 				ice_aq_str(hw->adminq.sq_last_status));
6359 			return -EIO;
6360 		}
6361 	}
6362 
6363 	if (lut) {
6364 		status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6365 					    lut, lut_size);
6366 		if (status) {
6367 			dev_err(dev, "Cannot get RSS lut, err %s aq_err %s\n",
6368 				ice_stat_str(status),
6369 				ice_aq_str(hw->adminq.sq_last_status));
6370 			return -EIO;
6371 		}
6372 	}
6373 
6374 	return 0;
6375 }
6376 
6377 /**
6378  * ice_bridge_getlink - Get the hardware bridge mode
6379  * @skb: skb buff
6380  * @pid: process ID
6381  * @seq: RTNL message seq
6382  * @dev: the netdev being configured
6383  * @filter_mask: filter mask passed in
6384  * @nlflags: netlink flags passed in
6385  *
6386  * Return the bridge mode (VEB/VEPA)
6387  */
6388 static int
6389 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
6390 		   struct net_device *dev, u32 filter_mask, int nlflags)
6391 {
6392 	struct ice_netdev_priv *np = netdev_priv(dev);
6393 	struct ice_vsi *vsi = np->vsi;
6394 	struct ice_pf *pf = vsi->back;
6395 	u16 bmode;
6396 
6397 	bmode = pf->first_sw->bridge_mode;
6398 
6399 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
6400 				       filter_mask, NULL);
6401 }
6402 
6403 /**
6404  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
6405  * @vsi: Pointer to VSI structure
6406  * @bmode: Hardware bridge mode (VEB/VEPA)
6407  *
6408  * Returns 0 on success, negative on failure
6409  */
6410 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
6411 {
6412 	struct ice_aqc_vsi_props *vsi_props;
6413 	struct ice_hw *hw = &vsi->back->hw;
6414 	struct ice_vsi_ctx *ctxt;
6415 	enum ice_status status;
6416 	int ret = 0;
6417 
6418 	vsi_props = &vsi->info;
6419 
6420 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
6421 	if (!ctxt)
6422 		return -ENOMEM;
6423 
6424 	ctxt->info = vsi->info;
6425 
6426 	if (bmode == BRIDGE_MODE_VEB)
6427 		/* change from VEPA to VEB mode */
6428 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6429 	else
6430 		/* change from VEB to VEPA mode */
6431 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6432 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
6433 
6434 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
6435 	if (status) {
6436 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %s aq_err %s\n",
6437 			bmode, ice_stat_str(status),
6438 			ice_aq_str(hw->adminq.sq_last_status));
6439 		ret = -EIO;
6440 		goto out;
6441 	}
6442 	/* Update sw flags for book keeping */
6443 	vsi_props->sw_flags = ctxt->info.sw_flags;
6444 
6445 out:
6446 	kfree(ctxt);
6447 	return ret;
6448 }
6449 
6450 /**
6451  * ice_bridge_setlink - Set the hardware bridge mode
6452  * @dev: the netdev being configured
6453  * @nlh: RTNL message
6454  * @flags: bridge setlink flags
6455  * @extack: netlink extended ack
6456  *
6457  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
6458  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
6459  * not already set for all VSIs connected to this switch. And also update the
6460  * unicast switch filter rules for the corresponding switch of the netdev.
6461  */
6462 static int
6463 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
6464 		   u16 __always_unused flags,
6465 		   struct netlink_ext_ack __always_unused *extack)
6466 {
6467 	struct ice_netdev_priv *np = netdev_priv(dev);
6468 	struct ice_pf *pf = np->vsi->back;
6469 	struct nlattr *attr, *br_spec;
6470 	struct ice_hw *hw = &pf->hw;
6471 	enum ice_status status;
6472 	struct ice_sw *pf_sw;
6473 	int rem, v, err = 0;
6474 
6475 	pf_sw = pf->first_sw;
6476 	/* find the attribute in the netlink message */
6477 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
6478 
6479 	nla_for_each_nested(attr, br_spec, rem) {
6480 		__u16 mode;
6481 
6482 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
6483 			continue;
6484 		mode = nla_get_u16(attr);
6485 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
6486 			return -EINVAL;
6487 		/* Continue  if bridge mode is not being flipped */
6488 		if (mode == pf_sw->bridge_mode)
6489 			continue;
6490 		/* Iterates through the PF VSI list and update the loopback
6491 		 * mode of the VSI
6492 		 */
6493 		ice_for_each_vsi(pf, v) {
6494 			if (!pf->vsi[v])
6495 				continue;
6496 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
6497 			if (err)
6498 				return err;
6499 		}
6500 
6501 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
6502 		/* Update the unicast switch filter rules for the corresponding
6503 		 * switch of the netdev
6504 		 */
6505 		status = ice_update_sw_rule_bridge_mode(hw);
6506 		if (status) {
6507 			netdev_err(dev, "switch rule update failed, mode = %d err %s aq_err %s\n",
6508 				   mode, ice_stat_str(status),
6509 				   ice_aq_str(hw->adminq.sq_last_status));
6510 			/* revert hw->evb_veb */
6511 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
6512 			return -EIO;
6513 		}
6514 
6515 		pf_sw->bridge_mode = mode;
6516 	}
6517 
6518 	return 0;
6519 }
6520 
6521 /**
6522  * ice_tx_timeout - Respond to a Tx Hang
6523  * @netdev: network interface device structure
6524  * @txqueue: Tx queue
6525  */
6526 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
6527 {
6528 	struct ice_netdev_priv *np = netdev_priv(netdev);
6529 	struct ice_ring *tx_ring = NULL;
6530 	struct ice_vsi *vsi = np->vsi;
6531 	struct ice_pf *pf = vsi->back;
6532 	u32 i;
6533 
6534 	pf->tx_timeout_count++;
6535 
6536 	/* Check if PFC is enabled for the TC to which the queue belongs
6537 	 * to. If yes then Tx timeout is not caused by a hung queue, no
6538 	 * need to reset and rebuild
6539 	 */
6540 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
6541 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
6542 			 txqueue);
6543 		return;
6544 	}
6545 
6546 	/* now that we have an index, find the tx_ring struct */
6547 	for (i = 0; i < vsi->num_txq; i++)
6548 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
6549 			if (txqueue == vsi->tx_rings[i]->q_index) {
6550 				tx_ring = vsi->tx_rings[i];
6551 				break;
6552 			}
6553 
6554 	/* Reset recovery level if enough time has elapsed after last timeout.
6555 	 * Also ensure no new reset action happens before next timeout period.
6556 	 */
6557 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
6558 		pf->tx_timeout_recovery_level = 1;
6559 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
6560 				       netdev->watchdog_timeo)))
6561 		return;
6562 
6563 	if (tx_ring) {
6564 		struct ice_hw *hw = &pf->hw;
6565 		u32 head, val = 0;
6566 
6567 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
6568 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
6569 		/* Read interrupt register */
6570 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
6571 
6572 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
6573 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
6574 			    head, tx_ring->next_to_use, val);
6575 	}
6576 
6577 	pf->tx_timeout_last_recovery = jiffies;
6578 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
6579 		    pf->tx_timeout_recovery_level, txqueue);
6580 
6581 	switch (pf->tx_timeout_recovery_level) {
6582 	case 1:
6583 		set_bit(__ICE_PFR_REQ, pf->state);
6584 		break;
6585 	case 2:
6586 		set_bit(__ICE_CORER_REQ, pf->state);
6587 		break;
6588 	case 3:
6589 		set_bit(__ICE_GLOBR_REQ, pf->state);
6590 		break;
6591 	default:
6592 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
6593 		set_bit(__ICE_DOWN, pf->state);
6594 		set_bit(__ICE_NEEDS_RESTART, vsi->state);
6595 		set_bit(__ICE_SERVICE_DIS, pf->state);
6596 		break;
6597 	}
6598 
6599 	ice_service_task_schedule(pf);
6600 	pf->tx_timeout_recovery_level++;
6601 }
6602 
6603 /**
6604  * ice_open - Called when a network interface becomes active
6605  * @netdev: network interface device structure
6606  *
6607  * The open entry point is called when a network interface is made
6608  * active by the system (IFF_UP). At this point all resources needed
6609  * for transmit and receive operations are allocated, the interrupt
6610  * handler is registered with the OS, the netdev watchdog is enabled,
6611  * and the stack is notified that the interface is ready.
6612  *
6613  * Returns 0 on success, negative value on failure
6614  */
6615 int ice_open(struct net_device *netdev)
6616 {
6617 	struct ice_netdev_priv *np = netdev_priv(netdev);
6618 	struct ice_vsi *vsi = np->vsi;
6619 	struct ice_pf *pf = vsi->back;
6620 	struct ice_port_info *pi;
6621 	int err;
6622 
6623 	if (test_bit(__ICE_NEEDS_RESTART, pf->state)) {
6624 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6625 		return -EIO;
6626 	}
6627 
6628 	if (test_bit(__ICE_DOWN, pf->state)) {
6629 		netdev_err(netdev, "device is not ready yet\n");
6630 		return -EBUSY;
6631 	}
6632 
6633 	netif_carrier_off(netdev);
6634 
6635 	pi = vsi->port_info;
6636 	err = ice_update_link_info(pi);
6637 	if (err) {
6638 		netdev_err(netdev, "Failed to get link info, error %d\n",
6639 			   err);
6640 		return err;
6641 	}
6642 
6643 	/* Set PHY if there is media, otherwise, turn off PHY */
6644 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
6645 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6646 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state)) {
6647 			err = ice_init_phy_user_cfg(pi);
6648 			if (err) {
6649 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
6650 					   err);
6651 				return err;
6652 			}
6653 		}
6654 
6655 		err = ice_configure_phy(vsi);
6656 		if (err) {
6657 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
6658 				   err);
6659 			return err;
6660 		}
6661 	} else {
6662 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6663 		err = ice_aq_set_link_restart_an(pi, false, NULL);
6664 		if (err) {
6665 			netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
6666 				   vsi->vsi_num, err);
6667 			return err;
6668 		}
6669 	}
6670 
6671 	err = ice_vsi_open(vsi);
6672 	if (err)
6673 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6674 			   vsi->vsi_num, vsi->vsw->sw_id);
6675 
6676 	/* Update existing tunnels information */
6677 	udp_tunnel_get_rx_info(netdev);
6678 
6679 	return err;
6680 }
6681 
6682 /**
6683  * ice_stop - Disables a network interface
6684  * @netdev: network interface device structure
6685  *
6686  * The stop entry point is called when an interface is de-activated by the OS,
6687  * and the netdevice enters the DOWN state. The hardware is still under the
6688  * driver's control, but the netdev interface is disabled.
6689  *
6690  * Returns success only - not allowed to fail
6691  */
6692 int ice_stop(struct net_device *netdev)
6693 {
6694 	struct ice_netdev_priv *np = netdev_priv(netdev);
6695 	struct ice_vsi *vsi = np->vsi;
6696 
6697 	ice_vsi_close(vsi);
6698 
6699 	return 0;
6700 }
6701 
6702 /**
6703  * ice_features_check - Validate encapsulated packet conforms to limits
6704  * @skb: skb buffer
6705  * @netdev: This port's netdev
6706  * @features: Offload features that the stack believes apply
6707  */
6708 static netdev_features_t
6709 ice_features_check(struct sk_buff *skb,
6710 		   struct net_device __always_unused *netdev,
6711 		   netdev_features_t features)
6712 {
6713 	size_t len;
6714 
6715 	/* No point in doing any of this if neither checksum nor GSO are
6716 	 * being requested for this frame. We can rule out both by just
6717 	 * checking for CHECKSUM_PARTIAL
6718 	 */
6719 	if (skb->ip_summed != CHECKSUM_PARTIAL)
6720 		return features;
6721 
6722 	/* We cannot support GSO if the MSS is going to be less than
6723 	 * 64 bytes. If it is then we need to drop support for GSO.
6724 	 */
6725 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6726 		features &= ~NETIF_F_GSO_MASK;
6727 
6728 	len = skb_network_header(skb) - skb->data;
6729 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
6730 		goto out_rm_features;
6731 
6732 	len = skb_transport_header(skb) - skb_network_header(skb);
6733 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6734 		goto out_rm_features;
6735 
6736 	if (skb->encapsulation) {
6737 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
6738 		if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
6739 			goto out_rm_features;
6740 
6741 		len = skb_inner_transport_header(skb) -
6742 		      skb_inner_network_header(skb);
6743 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6744 			goto out_rm_features;
6745 	}
6746 
6747 	return features;
6748 out_rm_features:
6749 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6750 }
6751 
6752 static const struct net_device_ops ice_netdev_safe_mode_ops = {
6753 	.ndo_open = ice_open,
6754 	.ndo_stop = ice_stop,
6755 	.ndo_start_xmit = ice_start_xmit,
6756 	.ndo_set_mac_address = ice_set_mac_address,
6757 	.ndo_validate_addr = eth_validate_addr,
6758 	.ndo_change_mtu = ice_change_mtu,
6759 	.ndo_get_stats64 = ice_get_stats64,
6760 	.ndo_tx_timeout = ice_tx_timeout,
6761 };
6762 
6763 static const struct net_device_ops ice_netdev_ops = {
6764 	.ndo_open = ice_open,
6765 	.ndo_stop = ice_stop,
6766 	.ndo_start_xmit = ice_start_xmit,
6767 	.ndo_features_check = ice_features_check,
6768 	.ndo_set_rx_mode = ice_set_rx_mode,
6769 	.ndo_set_mac_address = ice_set_mac_address,
6770 	.ndo_validate_addr = eth_validate_addr,
6771 	.ndo_change_mtu = ice_change_mtu,
6772 	.ndo_get_stats64 = ice_get_stats64,
6773 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
6774 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
6775 	.ndo_set_vf_mac = ice_set_vf_mac,
6776 	.ndo_get_vf_config = ice_get_vf_cfg,
6777 	.ndo_set_vf_trust = ice_set_vf_trust,
6778 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
6779 	.ndo_set_vf_link_state = ice_set_vf_link_state,
6780 	.ndo_get_vf_stats = ice_get_vf_stats,
6781 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6782 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6783 	.ndo_set_features = ice_set_features,
6784 	.ndo_bridge_getlink = ice_bridge_getlink,
6785 	.ndo_bridge_setlink = ice_bridge_setlink,
6786 	.ndo_fdb_add = ice_fdb_add,
6787 	.ndo_fdb_del = ice_fdb_del,
6788 #ifdef CONFIG_RFS_ACCEL
6789 	.ndo_rx_flow_steer = ice_rx_flow_steer,
6790 #endif
6791 	.ndo_tx_timeout = ice_tx_timeout,
6792 	.ndo_bpf = ice_xdp,
6793 	.ndo_xdp_xmit = ice_xdp_xmit,
6794 	.ndo_xsk_wakeup = ice_xsk_wakeup,
6795 	.ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
6796 	.ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
6797 };
6798