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 	WARN_ON(in_interrupt());
490 
491 	ice_prepare_for_reset(pf);
492 
493 	/* trigger the reset */
494 	if (ice_reset(hw, reset_type)) {
495 		dev_err(dev, "reset %d failed\n", reset_type);
496 		set_bit(__ICE_RESET_FAILED, pf->state);
497 		clear_bit(__ICE_RESET_OICR_RECV, pf->state);
498 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
499 		clear_bit(__ICE_PFR_REQ, pf->state);
500 		clear_bit(__ICE_CORER_REQ, pf->state);
501 		clear_bit(__ICE_GLOBR_REQ, pf->state);
502 		return;
503 	}
504 
505 	/* PFR is a bit of a special case because it doesn't result in an OICR
506 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
507 	 * associated state bits.
508 	 */
509 	if (reset_type == ICE_RESET_PFR) {
510 		pf->pfr_count++;
511 		ice_rebuild(pf, reset_type);
512 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
513 		clear_bit(__ICE_PFR_REQ, pf->state);
514 		ice_reset_all_vfs(pf, true);
515 	}
516 }
517 
518 /**
519  * ice_reset_subtask - Set up for resetting the device and driver
520  * @pf: board private structure
521  */
522 static void ice_reset_subtask(struct ice_pf *pf)
523 {
524 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
525 
526 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
527 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
528 	 * of reset is pending and sets bits in pf->state indicating the reset
529 	 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
530 	 * prepare for pending reset if not already (for PF software-initiated
531 	 * global resets the software should already be prepared for it as
532 	 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
533 	 * by firmware or software on other PFs, that bit is not set so prepare
534 	 * for the reset now), poll for reset done, rebuild and return.
535 	 */
536 	if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
537 		/* Perform the largest reset requested */
538 		if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
539 			reset_type = ICE_RESET_CORER;
540 		if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
541 			reset_type = ICE_RESET_GLOBR;
542 		if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state))
543 			reset_type = ICE_RESET_EMPR;
544 		/* return if no valid reset type requested */
545 		if (reset_type == ICE_RESET_INVAL)
546 			return;
547 		ice_prepare_for_reset(pf);
548 
549 		/* make sure we are ready to rebuild */
550 		if (ice_check_reset(&pf->hw)) {
551 			set_bit(__ICE_RESET_FAILED, pf->state);
552 		} else {
553 			/* done with reset. start rebuild */
554 			pf->hw.reset_ongoing = false;
555 			ice_rebuild(pf, reset_type);
556 			/* clear bit to resume normal operations, but
557 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
558 			 */
559 			clear_bit(__ICE_RESET_OICR_RECV, pf->state);
560 			clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
561 			clear_bit(__ICE_PFR_REQ, pf->state);
562 			clear_bit(__ICE_CORER_REQ, pf->state);
563 			clear_bit(__ICE_GLOBR_REQ, pf->state);
564 			ice_reset_all_vfs(pf, true);
565 		}
566 
567 		return;
568 	}
569 
570 	/* No pending resets to finish processing. Check for new resets */
571 	if (test_bit(__ICE_PFR_REQ, pf->state))
572 		reset_type = ICE_RESET_PFR;
573 	if (test_bit(__ICE_CORER_REQ, pf->state))
574 		reset_type = ICE_RESET_CORER;
575 	if (test_bit(__ICE_GLOBR_REQ, pf->state))
576 		reset_type = ICE_RESET_GLOBR;
577 	/* If no valid reset type requested just return */
578 	if (reset_type == ICE_RESET_INVAL)
579 		return;
580 
581 	/* reset if not already down or busy */
582 	if (!test_bit(__ICE_DOWN, pf->state) &&
583 	    !test_bit(__ICE_CFG_BUSY, pf->state)) {
584 		ice_do_reset(pf, reset_type);
585 	}
586 }
587 
588 /**
589  * ice_print_topo_conflict - print topology conflict message
590  * @vsi: the VSI whose topology status is being checked
591  */
592 static void ice_print_topo_conflict(struct ice_vsi *vsi)
593 {
594 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
595 	case ICE_AQ_LINK_TOPO_CONFLICT:
596 	case ICE_AQ_LINK_MEDIA_CONFLICT:
597 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
598 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
599 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
600 		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");
601 		break;
602 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
603 		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");
604 		break;
605 	default:
606 		break;
607 	}
608 }
609 
610 /**
611  * ice_print_link_msg - print link up or down message
612  * @vsi: the VSI whose link status is being queried
613  * @isup: boolean for if the link is now up or down
614  */
615 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
616 {
617 	struct ice_aqc_get_phy_caps_data *caps;
618 	const char *an_advertised;
619 	enum ice_status status;
620 	const char *fec_req;
621 	const char *speed;
622 	const char *fec;
623 	const char *fc;
624 	const char *an;
625 
626 	if (!vsi)
627 		return;
628 
629 	if (vsi->current_isup == isup)
630 		return;
631 
632 	vsi->current_isup = isup;
633 
634 	if (!isup) {
635 		netdev_info(vsi->netdev, "NIC Link is Down\n");
636 		return;
637 	}
638 
639 	switch (vsi->port_info->phy.link_info.link_speed) {
640 	case ICE_AQ_LINK_SPEED_100GB:
641 		speed = "100 G";
642 		break;
643 	case ICE_AQ_LINK_SPEED_50GB:
644 		speed = "50 G";
645 		break;
646 	case ICE_AQ_LINK_SPEED_40GB:
647 		speed = "40 G";
648 		break;
649 	case ICE_AQ_LINK_SPEED_25GB:
650 		speed = "25 G";
651 		break;
652 	case ICE_AQ_LINK_SPEED_20GB:
653 		speed = "20 G";
654 		break;
655 	case ICE_AQ_LINK_SPEED_10GB:
656 		speed = "10 G";
657 		break;
658 	case ICE_AQ_LINK_SPEED_5GB:
659 		speed = "5 G";
660 		break;
661 	case ICE_AQ_LINK_SPEED_2500MB:
662 		speed = "2.5 G";
663 		break;
664 	case ICE_AQ_LINK_SPEED_1000MB:
665 		speed = "1 G";
666 		break;
667 	case ICE_AQ_LINK_SPEED_100MB:
668 		speed = "100 M";
669 		break;
670 	default:
671 		speed = "Unknown";
672 		break;
673 	}
674 
675 	switch (vsi->port_info->fc.current_mode) {
676 	case ICE_FC_FULL:
677 		fc = "Rx/Tx";
678 		break;
679 	case ICE_FC_TX_PAUSE:
680 		fc = "Tx";
681 		break;
682 	case ICE_FC_RX_PAUSE:
683 		fc = "Rx";
684 		break;
685 	case ICE_FC_NONE:
686 		fc = "None";
687 		break;
688 	default:
689 		fc = "Unknown";
690 		break;
691 	}
692 
693 	/* Get FEC mode based on negotiated link info */
694 	switch (vsi->port_info->phy.link_info.fec_info) {
695 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
696 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
697 		fec = "RS-FEC";
698 		break;
699 	case ICE_AQ_LINK_25G_KR_FEC_EN:
700 		fec = "FC-FEC/BASE-R";
701 		break;
702 	default:
703 		fec = "NONE";
704 		break;
705 	}
706 
707 	/* check if autoneg completed, might be false due to not supported */
708 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
709 		an = "True";
710 	else
711 		an = "False";
712 
713 	/* Get FEC mode requested based on PHY caps last SW configuration */
714 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
715 	if (!caps) {
716 		fec_req = "Unknown";
717 		an_advertised = "Unknown";
718 		goto done;
719 	}
720 
721 	status = ice_aq_get_phy_caps(vsi->port_info, false,
722 				     ICE_AQC_REPORT_SW_CFG, caps, NULL);
723 	if (status)
724 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
725 
726 	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
727 
728 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
729 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
730 		fec_req = "RS-FEC";
731 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
732 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
733 		fec_req = "FC-FEC/BASE-R";
734 	else
735 		fec_req = "NONE";
736 
737 	kfree(caps);
738 
739 done:
740 	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",
741 		    speed, fec_req, fec, an_advertised, an, fc);
742 	ice_print_topo_conflict(vsi);
743 }
744 
745 /**
746  * ice_vsi_link_event - update the VSI's netdev
747  * @vsi: the VSI on which the link event occurred
748  * @link_up: whether or not the VSI needs to be set up or down
749  */
750 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
751 {
752 	if (!vsi)
753 		return;
754 
755 	if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
756 		return;
757 
758 	if (vsi->type == ICE_VSI_PF) {
759 		if (link_up == netif_carrier_ok(vsi->netdev))
760 			return;
761 
762 		if (link_up) {
763 			netif_carrier_on(vsi->netdev);
764 			netif_tx_wake_all_queues(vsi->netdev);
765 		} else {
766 			netif_carrier_off(vsi->netdev);
767 			netif_tx_stop_all_queues(vsi->netdev);
768 		}
769 	}
770 }
771 
772 /**
773  * ice_set_dflt_mib - send a default config MIB to the FW
774  * @pf: private PF struct
775  *
776  * This function sends a default configuration MIB to the FW.
777  *
778  * If this function errors out at any point, the driver is still able to
779  * function.  The main impact is that LFC may not operate as expected.
780  * Therefore an error state in this function should be treated with a DBG
781  * message and continue on with driver rebuild/reenable.
782  */
783 static void ice_set_dflt_mib(struct ice_pf *pf)
784 {
785 	struct device *dev = ice_pf_to_dev(pf);
786 	u8 mib_type, *buf, *lldpmib = NULL;
787 	u16 len, typelen, offset = 0;
788 	struct ice_lldp_org_tlv *tlv;
789 	struct ice_hw *hw;
790 	u32 ouisubtype;
791 
792 	if (!pf) {
793 		dev_dbg(dev, "%s NULL pf pointer\n", __func__);
794 		return;
795 	}
796 
797 	hw = &pf->hw;
798 	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
799 	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
800 	if (!lldpmib) {
801 		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
802 			__func__);
803 		return;
804 	}
805 
806 	/* Add ETS CFG TLV */
807 	tlv = (struct ice_lldp_org_tlv *)lldpmib;
808 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
809 		   ICE_IEEE_ETS_TLV_LEN);
810 	tlv->typelen = htons(typelen);
811 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
812 		      ICE_IEEE_SUBTYPE_ETS_CFG);
813 	tlv->ouisubtype = htonl(ouisubtype);
814 
815 	buf = tlv->tlvinfo;
816 	buf[0] = 0;
817 
818 	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
819 	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
820 	 * Octets 13 - 20 are TSA values - leave as zeros
821 	 */
822 	buf[5] = 0x64;
823 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
824 	offset += len + 2;
825 	tlv = (struct ice_lldp_org_tlv *)
826 		((char *)tlv + sizeof(tlv->typelen) + len);
827 
828 	/* Add ETS REC TLV */
829 	buf = tlv->tlvinfo;
830 	tlv->typelen = htons(typelen);
831 
832 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
833 		      ICE_IEEE_SUBTYPE_ETS_REC);
834 	tlv->ouisubtype = htonl(ouisubtype);
835 
836 	/* First octet of buf is reserved
837 	 * Octets 1 - 4 map UP to TC - all UPs map to zero
838 	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
839 	 * Octets 13 - 20 are TSA value - leave as zeros
840 	 */
841 	buf[5] = 0x64;
842 	offset += len + 2;
843 	tlv = (struct ice_lldp_org_tlv *)
844 		((char *)tlv + sizeof(tlv->typelen) + len);
845 
846 	/* Add PFC CFG TLV */
847 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
848 		   ICE_IEEE_PFC_TLV_LEN);
849 	tlv->typelen = htons(typelen);
850 
851 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
852 		      ICE_IEEE_SUBTYPE_PFC_CFG);
853 	tlv->ouisubtype = htonl(ouisubtype);
854 
855 	/* Octet 1 left as all zeros - PFC disabled */
856 	buf[0] = 0x08;
857 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
858 	offset += len + 2;
859 
860 	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
861 		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
862 
863 	kfree(lldpmib);
864 }
865 
866 /**
867  * ice_link_event - process the link event
868  * @pf: PF that the link event is associated with
869  * @pi: port_info for the port that the link event is associated with
870  * @link_up: true if the physical link is up and false if it is down
871  * @link_speed: current link speed received from the link event
872  *
873  * Returns 0 on success and negative on failure
874  */
875 static int
876 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
877 	       u16 link_speed)
878 {
879 	struct device *dev = ice_pf_to_dev(pf);
880 	struct ice_phy_info *phy_info;
881 	struct ice_vsi *vsi;
882 	u16 old_link_speed;
883 	bool old_link;
884 	int result;
885 
886 	phy_info = &pi->phy;
887 	phy_info->link_info_old = phy_info->link_info;
888 
889 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
890 	old_link_speed = phy_info->link_info_old.link_speed;
891 
892 	/* update the link info structures and re-enable link events,
893 	 * don't bail on failure due to other book keeping needed
894 	 */
895 	result = ice_update_link_info(pi);
896 	if (result)
897 		dev_dbg(dev, "Failed to update link status and re-enable link events for port %d\n",
898 			pi->lport);
899 
900 	/* Check if the link state is up after updating link info, and treat
901 	 * this event as an UP event since the link is actually UP now.
902 	 */
903 	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
904 		link_up = true;
905 
906 	vsi = ice_get_main_vsi(pf);
907 	if (!vsi || !vsi->port_info)
908 		return -EINVAL;
909 
910 	/* turn off PHY if media was removed */
911 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
912 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
913 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
914 
915 		result = ice_aq_set_link_restart_an(pi, false, NULL);
916 		if (result) {
917 			dev_dbg(dev, "Failed to set link down, VSI %d error %d\n",
918 				vsi->vsi_num, result);
919 			return result;
920 		}
921 	}
922 
923 	/* if the old link up/down and speed is the same as the new */
924 	if (link_up == old_link && link_speed == old_link_speed)
925 		return result;
926 
927 	if (ice_is_dcb_active(pf)) {
928 		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
929 			ice_dcb_rebuild(pf);
930 	} else {
931 		if (link_up)
932 			ice_set_dflt_mib(pf);
933 	}
934 	ice_vsi_link_event(vsi, link_up);
935 	ice_print_link_msg(vsi, link_up);
936 
937 	ice_vc_notify_link_state(pf);
938 
939 	return result;
940 }
941 
942 /**
943  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
944  * @pf: board private structure
945  */
946 static void ice_watchdog_subtask(struct ice_pf *pf)
947 {
948 	int i;
949 
950 	/* if interface is down do nothing */
951 	if (test_bit(__ICE_DOWN, pf->state) ||
952 	    test_bit(__ICE_CFG_BUSY, pf->state))
953 		return;
954 
955 	/* make sure we don't do these things too often */
956 	if (time_before(jiffies,
957 			pf->serv_tmr_prev + pf->serv_tmr_period))
958 		return;
959 
960 	pf->serv_tmr_prev = jiffies;
961 
962 	/* Update the stats for active netdevs so the network stack
963 	 * can look at updated numbers whenever it cares to
964 	 */
965 	ice_update_pf_stats(pf);
966 	ice_for_each_vsi(pf, i)
967 		if (pf->vsi[i] && pf->vsi[i]->netdev)
968 			ice_update_vsi_stats(pf->vsi[i]);
969 }
970 
971 /**
972  * ice_init_link_events - enable/initialize link events
973  * @pi: pointer to the port_info instance
974  *
975  * Returns -EIO on failure, 0 on success
976  */
977 static int ice_init_link_events(struct ice_port_info *pi)
978 {
979 	u16 mask;
980 
981 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
982 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
983 
984 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
985 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
986 			pi->lport);
987 		return -EIO;
988 	}
989 
990 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
991 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
992 			pi->lport);
993 		return -EIO;
994 	}
995 
996 	return 0;
997 }
998 
999 /**
1000  * ice_handle_link_event - handle link event via ARQ
1001  * @pf: PF that the link event is associated with
1002  * @event: event structure containing link status info
1003  */
1004 static int
1005 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1006 {
1007 	struct ice_aqc_get_link_status_data *link_data;
1008 	struct ice_port_info *port_info;
1009 	int status;
1010 
1011 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1012 	port_info = pf->hw.port_info;
1013 	if (!port_info)
1014 		return -EINVAL;
1015 
1016 	status = ice_link_event(pf, port_info,
1017 				!!(link_data->link_info & ICE_AQ_LINK_UP),
1018 				le16_to_cpu(link_data->link_speed));
1019 	if (status)
1020 		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1021 			status);
1022 
1023 	return status;
1024 }
1025 
1026 enum ice_aq_task_state {
1027 	ICE_AQ_TASK_WAITING = 0,
1028 	ICE_AQ_TASK_COMPLETE,
1029 	ICE_AQ_TASK_CANCELED,
1030 };
1031 
1032 struct ice_aq_task {
1033 	struct hlist_node entry;
1034 
1035 	u16 opcode;
1036 	struct ice_rq_event_info *event;
1037 	enum ice_aq_task_state state;
1038 };
1039 
1040 /**
1041  * ice_wait_for_aq_event - Wait for an AdminQ event from firmware
1042  * @pf: pointer to the PF private structure
1043  * @opcode: the opcode to wait for
1044  * @timeout: how long to wait, in jiffies
1045  * @event: storage for the event info
1046  *
1047  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1048  * current thread will be put to sleep until the specified event occurs or
1049  * until the given timeout is reached.
1050  *
1051  * To obtain only the descriptor contents, pass an event without an allocated
1052  * msg_buf. If the complete data buffer is desired, allocate the
1053  * event->msg_buf with enough space ahead of time.
1054  *
1055  * Returns: zero on success, or a negative error code on failure.
1056  */
1057 int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1058 			  struct ice_rq_event_info *event)
1059 {
1060 	struct ice_aq_task *task;
1061 	long ret;
1062 	int err;
1063 
1064 	task = kzalloc(sizeof(*task), GFP_KERNEL);
1065 	if (!task)
1066 		return -ENOMEM;
1067 
1068 	INIT_HLIST_NODE(&task->entry);
1069 	task->opcode = opcode;
1070 	task->event = event;
1071 	task->state = ICE_AQ_TASK_WAITING;
1072 
1073 	spin_lock_bh(&pf->aq_wait_lock);
1074 	hlist_add_head(&task->entry, &pf->aq_wait_list);
1075 	spin_unlock_bh(&pf->aq_wait_lock);
1076 
1077 	ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1078 					       timeout);
1079 	switch (task->state) {
1080 	case ICE_AQ_TASK_WAITING:
1081 		err = ret < 0 ? ret : -ETIMEDOUT;
1082 		break;
1083 	case ICE_AQ_TASK_CANCELED:
1084 		err = ret < 0 ? ret : -ECANCELED;
1085 		break;
1086 	case ICE_AQ_TASK_COMPLETE:
1087 		err = ret < 0 ? ret : 0;
1088 		break;
1089 	default:
1090 		WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1091 		err = -EINVAL;
1092 		break;
1093 	}
1094 
1095 	spin_lock_bh(&pf->aq_wait_lock);
1096 	hlist_del(&task->entry);
1097 	spin_unlock_bh(&pf->aq_wait_lock);
1098 	kfree(task);
1099 
1100 	return err;
1101 }
1102 
1103 /**
1104  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1105  * @pf: pointer to the PF private structure
1106  * @opcode: the opcode of the event
1107  * @event: the event to check
1108  *
1109  * Loops over the current list of pending threads waiting for an AdminQ event.
1110  * For each matching task, copy the contents of the event into the task
1111  * structure and wake up the thread.
1112  *
1113  * If multiple threads wait for the same opcode, they will all be woken up.
1114  *
1115  * Note that event->msg_buf will only be duplicated if the event has a buffer
1116  * with enough space already allocated. Otherwise, only the descriptor and
1117  * message length will be copied.
1118  *
1119  * Returns: true if an event was found, false otherwise
1120  */
1121 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1122 				struct ice_rq_event_info *event)
1123 {
1124 	struct ice_aq_task *task;
1125 	bool found = false;
1126 
1127 	spin_lock_bh(&pf->aq_wait_lock);
1128 	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1129 		if (task->state || task->opcode != opcode)
1130 			continue;
1131 
1132 		memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1133 		task->event->msg_len = event->msg_len;
1134 
1135 		/* Only copy the data buffer if a destination was set */
1136 		if (task->event->msg_buf &&
1137 		    task->event->buf_len > event->buf_len) {
1138 			memcpy(task->event->msg_buf, event->msg_buf,
1139 			       event->buf_len);
1140 			task->event->buf_len = event->buf_len;
1141 		}
1142 
1143 		task->state = ICE_AQ_TASK_COMPLETE;
1144 		found = true;
1145 	}
1146 	spin_unlock_bh(&pf->aq_wait_lock);
1147 
1148 	if (found)
1149 		wake_up(&pf->aq_wait_queue);
1150 }
1151 
1152 /**
1153  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1154  * @pf: the PF private structure
1155  *
1156  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1157  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1158  */
1159 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1160 {
1161 	struct ice_aq_task *task;
1162 
1163 	spin_lock_bh(&pf->aq_wait_lock);
1164 	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1165 		task->state = ICE_AQ_TASK_CANCELED;
1166 	spin_unlock_bh(&pf->aq_wait_lock);
1167 
1168 	wake_up(&pf->aq_wait_queue);
1169 }
1170 
1171 /**
1172  * __ice_clean_ctrlq - helper function to clean controlq rings
1173  * @pf: ptr to struct ice_pf
1174  * @q_type: specific Control queue type
1175  */
1176 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1177 {
1178 	struct device *dev = ice_pf_to_dev(pf);
1179 	struct ice_rq_event_info event;
1180 	struct ice_hw *hw = &pf->hw;
1181 	struct ice_ctl_q_info *cq;
1182 	u16 pending, i = 0;
1183 	const char *qtype;
1184 	u32 oldval, val;
1185 
1186 	/* Do not clean control queue if/when PF reset fails */
1187 	if (test_bit(__ICE_RESET_FAILED, pf->state))
1188 		return 0;
1189 
1190 	switch (q_type) {
1191 	case ICE_CTL_Q_ADMIN:
1192 		cq = &hw->adminq;
1193 		qtype = "Admin";
1194 		break;
1195 	case ICE_CTL_Q_MAILBOX:
1196 		cq = &hw->mailboxq;
1197 		qtype = "Mailbox";
1198 		break;
1199 	default:
1200 		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1201 		return 0;
1202 	}
1203 
1204 	/* check for error indications - PF_xx_AxQLEN register layout for
1205 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1206 	 */
1207 	val = rd32(hw, cq->rq.len);
1208 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1209 		   PF_FW_ARQLEN_ARQCRIT_M)) {
1210 		oldval = val;
1211 		if (val & PF_FW_ARQLEN_ARQVFE_M)
1212 			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1213 				qtype);
1214 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1215 			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1216 				qtype);
1217 		}
1218 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1219 			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1220 				qtype);
1221 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1222 			 PF_FW_ARQLEN_ARQCRIT_M);
1223 		if (oldval != val)
1224 			wr32(hw, cq->rq.len, val);
1225 	}
1226 
1227 	val = rd32(hw, cq->sq.len);
1228 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1229 		   PF_FW_ATQLEN_ATQCRIT_M)) {
1230 		oldval = val;
1231 		if (val & PF_FW_ATQLEN_ATQVFE_M)
1232 			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1233 				qtype);
1234 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1235 			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1236 				qtype);
1237 		}
1238 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1239 			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1240 				qtype);
1241 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1242 			 PF_FW_ATQLEN_ATQCRIT_M);
1243 		if (oldval != val)
1244 			wr32(hw, cq->sq.len, val);
1245 	}
1246 
1247 	event.buf_len = cq->rq_buf_size;
1248 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1249 	if (!event.msg_buf)
1250 		return 0;
1251 
1252 	do {
1253 		enum ice_status ret;
1254 		u16 opcode;
1255 
1256 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1257 		if (ret == ICE_ERR_AQ_NO_WORK)
1258 			break;
1259 		if (ret) {
1260 			dev_err(dev, "%s Receive Queue event error %s\n", qtype,
1261 				ice_stat_str(ret));
1262 			break;
1263 		}
1264 
1265 		opcode = le16_to_cpu(event.desc.opcode);
1266 
1267 		/* Notify any thread that might be waiting for this event */
1268 		ice_aq_check_events(pf, opcode, &event);
1269 
1270 		switch (opcode) {
1271 		case ice_aqc_opc_get_link_status:
1272 			if (ice_handle_link_event(pf, &event))
1273 				dev_err(dev, "Could not handle link event\n");
1274 			break;
1275 		case ice_aqc_opc_event_lan_overflow:
1276 			ice_vf_lan_overflow_event(pf, &event);
1277 			break;
1278 		case ice_mbx_opc_send_msg_to_pf:
1279 			ice_vc_process_vf_msg(pf, &event);
1280 			break;
1281 		case ice_aqc_opc_fw_logging:
1282 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1283 			break;
1284 		case ice_aqc_opc_lldp_set_mib_change:
1285 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1286 			break;
1287 		default:
1288 			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1289 				qtype, opcode);
1290 			break;
1291 		}
1292 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1293 
1294 	kfree(event.msg_buf);
1295 
1296 	return pending && (i == ICE_DFLT_IRQ_WORK);
1297 }
1298 
1299 /**
1300  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1301  * @hw: pointer to hardware info
1302  * @cq: control queue information
1303  *
1304  * returns true if there are pending messages in a queue, false if there aren't
1305  */
1306 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1307 {
1308 	u16 ntu;
1309 
1310 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1311 	return cq->rq.next_to_clean != ntu;
1312 }
1313 
1314 /**
1315  * ice_clean_adminq_subtask - clean the AdminQ rings
1316  * @pf: board private structure
1317  */
1318 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1319 {
1320 	struct ice_hw *hw = &pf->hw;
1321 
1322 	if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1323 		return;
1324 
1325 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1326 		return;
1327 
1328 	clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1329 
1330 	/* There might be a situation where new messages arrive to a control
1331 	 * queue between processing the last message and clearing the
1332 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1333 	 * ice_ctrlq_pending) and process new messages if any.
1334 	 */
1335 	if (ice_ctrlq_pending(hw, &hw->adminq))
1336 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1337 
1338 	ice_flush(hw);
1339 }
1340 
1341 /**
1342  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1343  * @pf: board private structure
1344  */
1345 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1346 {
1347 	struct ice_hw *hw = &pf->hw;
1348 
1349 	if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1350 		return;
1351 
1352 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1353 		return;
1354 
1355 	clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1356 
1357 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1358 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1359 
1360 	ice_flush(hw);
1361 }
1362 
1363 /**
1364  * ice_service_task_schedule - schedule the service task to wake up
1365  * @pf: board private structure
1366  *
1367  * If not already scheduled, this puts the task into the work queue.
1368  */
1369 void ice_service_task_schedule(struct ice_pf *pf)
1370 {
1371 	if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1372 	    !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1373 	    !test_bit(__ICE_NEEDS_RESTART, pf->state))
1374 		queue_work(ice_wq, &pf->serv_task);
1375 }
1376 
1377 /**
1378  * ice_service_task_complete - finish up the service task
1379  * @pf: board private structure
1380  */
1381 static void ice_service_task_complete(struct ice_pf *pf)
1382 {
1383 	WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1384 
1385 	/* force memory (pf->state) to sync before next service task */
1386 	smp_mb__before_atomic();
1387 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1388 }
1389 
1390 /**
1391  * ice_service_task_stop - stop service task and cancel works
1392  * @pf: board private structure
1393  *
1394  * Return 0 if the __ICE_SERVICE_DIS bit was not already set,
1395  * 1 otherwise.
1396  */
1397 static int ice_service_task_stop(struct ice_pf *pf)
1398 {
1399 	int ret;
1400 
1401 	ret = test_and_set_bit(__ICE_SERVICE_DIS, pf->state);
1402 
1403 	if (pf->serv_tmr.function)
1404 		del_timer_sync(&pf->serv_tmr);
1405 	if (pf->serv_task.func)
1406 		cancel_work_sync(&pf->serv_task);
1407 
1408 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1409 	return ret;
1410 }
1411 
1412 /**
1413  * ice_service_task_restart - restart service task and schedule works
1414  * @pf: board private structure
1415  *
1416  * This function is needed for suspend and resume works (e.g WoL scenario)
1417  */
1418 static void ice_service_task_restart(struct ice_pf *pf)
1419 {
1420 	clear_bit(__ICE_SERVICE_DIS, pf->state);
1421 	ice_service_task_schedule(pf);
1422 }
1423 
1424 /**
1425  * ice_service_timer - timer callback to schedule service task
1426  * @t: pointer to timer_list
1427  */
1428 static void ice_service_timer(struct timer_list *t)
1429 {
1430 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1431 
1432 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1433 	ice_service_task_schedule(pf);
1434 }
1435 
1436 /**
1437  * ice_handle_mdd_event - handle malicious driver detect event
1438  * @pf: pointer to the PF structure
1439  *
1440  * Called from service task. OICR interrupt handler indicates MDD event.
1441  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1442  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1443  * disable the queue, the PF can be configured to reset the VF using ethtool
1444  * private flag mdd-auto-reset-vf.
1445  */
1446 static void ice_handle_mdd_event(struct ice_pf *pf)
1447 {
1448 	struct device *dev = ice_pf_to_dev(pf);
1449 	struct ice_hw *hw = &pf->hw;
1450 	unsigned int i;
1451 	u32 reg;
1452 
1453 	if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state)) {
1454 		/* Since the VF MDD event logging is rate limited, check if
1455 		 * there are pending MDD events.
1456 		 */
1457 		ice_print_vfs_mdd_events(pf);
1458 		return;
1459 	}
1460 
1461 	/* find what triggered an MDD event */
1462 	reg = rd32(hw, GL_MDET_TX_PQM);
1463 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1464 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1465 				GL_MDET_TX_PQM_PF_NUM_S;
1466 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1467 				GL_MDET_TX_PQM_VF_NUM_S;
1468 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1469 				GL_MDET_TX_PQM_MAL_TYPE_S;
1470 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1471 				GL_MDET_TX_PQM_QNUM_S);
1472 
1473 		if (netif_msg_tx_err(pf))
1474 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1475 				 event, queue, pf_num, vf_num);
1476 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1477 	}
1478 
1479 	reg = rd32(hw, GL_MDET_TX_TCLAN);
1480 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1481 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1482 				GL_MDET_TX_TCLAN_PF_NUM_S;
1483 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1484 				GL_MDET_TX_TCLAN_VF_NUM_S;
1485 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1486 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1487 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1488 				GL_MDET_TX_TCLAN_QNUM_S);
1489 
1490 		if (netif_msg_tx_err(pf))
1491 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1492 				 event, queue, pf_num, vf_num);
1493 		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1494 	}
1495 
1496 	reg = rd32(hw, GL_MDET_RX);
1497 	if (reg & GL_MDET_RX_VALID_M) {
1498 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1499 				GL_MDET_RX_PF_NUM_S;
1500 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1501 				GL_MDET_RX_VF_NUM_S;
1502 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1503 				GL_MDET_RX_MAL_TYPE_S;
1504 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1505 				GL_MDET_RX_QNUM_S);
1506 
1507 		if (netif_msg_rx_err(pf))
1508 			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1509 				 event, queue, pf_num, vf_num);
1510 		wr32(hw, GL_MDET_RX, 0xffffffff);
1511 	}
1512 
1513 	/* check to see if this PF caused an MDD event */
1514 	reg = rd32(hw, PF_MDET_TX_PQM);
1515 	if (reg & PF_MDET_TX_PQM_VALID_M) {
1516 		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1517 		if (netif_msg_tx_err(pf))
1518 			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1519 	}
1520 
1521 	reg = rd32(hw, PF_MDET_TX_TCLAN);
1522 	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1523 		wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1524 		if (netif_msg_tx_err(pf))
1525 			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1526 	}
1527 
1528 	reg = rd32(hw, PF_MDET_RX);
1529 	if (reg & PF_MDET_RX_VALID_M) {
1530 		wr32(hw, PF_MDET_RX, 0xFFFF);
1531 		if (netif_msg_rx_err(pf))
1532 			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1533 	}
1534 
1535 	/* Check to see if one of the VFs caused an MDD event, and then
1536 	 * increment counters and set print pending
1537 	 */
1538 	ice_for_each_vf(pf, i) {
1539 		struct ice_vf *vf = &pf->vf[i];
1540 
1541 		reg = rd32(hw, VP_MDET_TX_PQM(i));
1542 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1543 			wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1544 			vf->mdd_tx_events.count++;
1545 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1546 			if (netif_msg_tx_err(pf))
1547 				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1548 					 i);
1549 		}
1550 
1551 		reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1552 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1553 			wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1554 			vf->mdd_tx_events.count++;
1555 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1556 			if (netif_msg_tx_err(pf))
1557 				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1558 					 i);
1559 		}
1560 
1561 		reg = rd32(hw, VP_MDET_TX_TDPU(i));
1562 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1563 			wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1564 			vf->mdd_tx_events.count++;
1565 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1566 			if (netif_msg_tx_err(pf))
1567 				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1568 					 i);
1569 		}
1570 
1571 		reg = rd32(hw, VP_MDET_RX(i));
1572 		if (reg & VP_MDET_RX_VALID_M) {
1573 			wr32(hw, VP_MDET_RX(i), 0xFFFF);
1574 			vf->mdd_rx_events.count++;
1575 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1576 			if (netif_msg_rx_err(pf))
1577 				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1578 					 i);
1579 
1580 			/* Since the queue is disabled on VF Rx MDD events, the
1581 			 * PF can be configured to reset the VF through ethtool
1582 			 * private flag mdd-auto-reset-vf.
1583 			 */
1584 			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1585 				/* VF MDD event counters will be cleared by
1586 				 * reset, so print the event prior to reset.
1587 				 */
1588 				ice_print_vf_rx_mdd_event(vf);
1589 				ice_reset_vf(&pf->vf[i], false);
1590 			}
1591 		}
1592 	}
1593 
1594 	ice_print_vfs_mdd_events(pf);
1595 }
1596 
1597 /**
1598  * ice_force_phys_link_state - Force the physical link state
1599  * @vsi: VSI to force the physical link state to up/down
1600  * @link_up: true/false indicates to set the physical link to up/down
1601  *
1602  * Force the physical link state by getting the current PHY capabilities from
1603  * hardware and setting the PHY config based on the determined capabilities. If
1604  * link changes a link event will be triggered because both the Enable Automatic
1605  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1606  *
1607  * Returns 0 on success, negative on failure
1608  */
1609 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1610 {
1611 	struct ice_aqc_get_phy_caps_data *pcaps;
1612 	struct ice_aqc_set_phy_cfg_data *cfg;
1613 	struct ice_port_info *pi;
1614 	struct device *dev;
1615 	int retcode;
1616 
1617 	if (!vsi || !vsi->port_info || !vsi->back)
1618 		return -EINVAL;
1619 	if (vsi->type != ICE_VSI_PF)
1620 		return 0;
1621 
1622 	dev = ice_pf_to_dev(vsi->back);
1623 
1624 	pi = vsi->port_info;
1625 
1626 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1627 	if (!pcaps)
1628 		return -ENOMEM;
1629 
1630 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1631 				      NULL);
1632 	if (retcode) {
1633 		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1634 			vsi->vsi_num, retcode);
1635 		retcode = -EIO;
1636 		goto out;
1637 	}
1638 
1639 	/* No change in link */
1640 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1641 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1642 		goto out;
1643 
1644 	/* Use the current user PHY configuration. The current user PHY
1645 	 * configuration is initialized during probe from PHY capabilities
1646 	 * software mode, and updated on set PHY configuration.
1647 	 */
1648 	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1649 	if (!cfg) {
1650 		retcode = -ENOMEM;
1651 		goto out;
1652 	}
1653 
1654 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1655 	if (link_up)
1656 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1657 	else
1658 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1659 
1660 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1661 	if (retcode) {
1662 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1663 			vsi->vsi_num, retcode);
1664 		retcode = -EIO;
1665 	}
1666 
1667 	kfree(cfg);
1668 out:
1669 	kfree(pcaps);
1670 	return retcode;
1671 }
1672 
1673 /**
1674  * ice_init_nvm_phy_type - Initialize the NVM PHY type
1675  * @pi: port info structure
1676  *
1677  * Initialize nvm_phy_type_[low|high] for link lenient mode support
1678  */
1679 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1680 {
1681 	struct ice_aqc_get_phy_caps_data *pcaps;
1682 	struct ice_pf *pf = pi->hw->back;
1683 	enum ice_status status;
1684 	int err = 0;
1685 
1686 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1687 	if (!pcaps)
1688 		return -ENOMEM;
1689 
1690 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_NVM_CAP, pcaps,
1691 				     NULL);
1692 
1693 	if (status) {
1694 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1695 		err = -EIO;
1696 		goto out;
1697 	}
1698 
1699 	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1700 	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1701 
1702 out:
1703 	kfree(pcaps);
1704 	return err;
1705 }
1706 
1707 /**
1708  * ice_init_link_dflt_override - Initialize link default override
1709  * @pi: port info structure
1710  *
1711  * Initialize link default override and PHY total port shutdown during probe
1712  */
1713 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1714 {
1715 	struct ice_link_default_override_tlv *ldo;
1716 	struct ice_pf *pf = pi->hw->back;
1717 
1718 	ldo = &pf->link_dflt_override;
1719 	if (ice_get_link_default_override(ldo, pi))
1720 		return;
1721 
1722 	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1723 		return;
1724 
1725 	/* Enable Total Port Shutdown (override/replace link-down-on-close
1726 	 * ethtool private flag) for ports with Port Disable bit set.
1727 	 */
1728 	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1729 	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1730 }
1731 
1732 /**
1733  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1734  * @pi: port info structure
1735  *
1736  * If default override is enabled, initialized the user PHY cfg speed and FEC
1737  * settings using the default override mask from the NVM.
1738  *
1739  * The PHY should only be configured with the default override settings the
1740  * first time media is available. The __ICE_LINK_DEFAULT_OVERRIDE_PENDING state
1741  * is used to indicate that the user PHY cfg default override is initialized
1742  * and the PHY has not been configured with the default override settings. The
1743  * state is set here, and cleared in ice_configure_phy the first time the PHY is
1744  * configured.
1745  */
1746 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
1747 {
1748 	struct ice_link_default_override_tlv *ldo;
1749 	struct ice_aqc_set_phy_cfg_data *cfg;
1750 	struct ice_phy_info *phy = &pi->phy;
1751 	struct ice_pf *pf = pi->hw->back;
1752 
1753 	ldo = &pf->link_dflt_override;
1754 
1755 	/* If link default override is enabled, use to mask NVM PHY capabilities
1756 	 * for speed and FEC default configuration.
1757 	 */
1758 	cfg = &phy->curr_user_phy_cfg;
1759 
1760 	if (ldo->phy_type_low || ldo->phy_type_high) {
1761 		cfg->phy_type_low = pf->nvm_phy_type_lo &
1762 				    cpu_to_le64(ldo->phy_type_low);
1763 		cfg->phy_type_high = pf->nvm_phy_type_hi &
1764 				     cpu_to_le64(ldo->phy_type_high);
1765 	}
1766 	cfg->link_fec_opt = ldo->fec_options;
1767 	phy->curr_user_fec_req = ICE_FEC_AUTO;
1768 
1769 	set_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
1770 }
1771 
1772 /**
1773  * ice_init_phy_user_cfg - Initialize the PHY user configuration
1774  * @pi: port info structure
1775  *
1776  * Initialize the current user PHY configuration, speed, FEC, and FC requested
1777  * mode to default. The PHY defaults are from get PHY capabilities topology
1778  * with media so call when media is first available. An error is returned if
1779  * called when media is not available. The PHY initialization completed state is
1780  * set here.
1781  *
1782  * These configurations are used when setting PHY
1783  * configuration. The user PHY configuration is updated on set PHY
1784  * configuration. Returns 0 on success, negative on failure
1785  */
1786 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
1787 {
1788 	struct ice_aqc_get_phy_caps_data *pcaps;
1789 	struct ice_phy_info *phy = &pi->phy;
1790 	struct ice_pf *pf = pi->hw->back;
1791 	enum ice_status status;
1792 	struct ice_vsi *vsi;
1793 	int err = 0;
1794 
1795 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1796 		return -EIO;
1797 
1798 	vsi = ice_get_main_vsi(pf);
1799 	if (!vsi)
1800 		return -EINVAL;
1801 
1802 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1803 	if (!pcaps)
1804 		return -ENOMEM;
1805 
1806 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1807 				     NULL);
1808 	if (status) {
1809 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1810 		err = -EIO;
1811 		goto err_out;
1812 	}
1813 
1814 	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
1815 
1816 	/* check if lenient mode is supported and enabled */
1817 	if (ice_fw_supports_link_override(&vsi->back->hw) &&
1818 	    !(pcaps->module_compliance_enforcement &
1819 	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
1820 		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
1821 
1822 		/* if link default override is enabled, initialize user PHY
1823 		 * configuration with link default override values
1824 		 */
1825 		if (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN) {
1826 			ice_init_phy_cfg_dflt_override(pi);
1827 			goto out;
1828 		}
1829 	}
1830 
1831 	/* if link default override is not enabled, initialize PHY using
1832 	 * topology with media
1833 	 */
1834 	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
1835 						      pcaps->link_fec_options);
1836 	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
1837 
1838 out:
1839 	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
1840 	set_bit(__ICE_PHY_INIT_COMPLETE, pf->state);
1841 err_out:
1842 	kfree(pcaps);
1843 	return err;
1844 }
1845 
1846 /**
1847  * ice_configure_phy - configure PHY
1848  * @vsi: VSI of PHY
1849  *
1850  * Set the PHY configuration. If the current PHY configuration is the same as
1851  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
1852  * configure the based get PHY capabilities for topology with media.
1853  */
1854 static int ice_configure_phy(struct ice_vsi *vsi)
1855 {
1856 	struct device *dev = ice_pf_to_dev(vsi->back);
1857 	struct ice_aqc_get_phy_caps_data *pcaps;
1858 	struct ice_aqc_set_phy_cfg_data *cfg;
1859 	struct ice_port_info *pi;
1860 	enum ice_status status;
1861 	int err = 0;
1862 
1863 	pi = vsi->port_info;
1864 	if (!pi)
1865 		return -EINVAL;
1866 
1867 	/* Ensure we have media as we cannot configure a medialess port */
1868 	if (!(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1869 		return -EPERM;
1870 
1871 	ice_print_topo_conflict(vsi);
1872 
1873 	if (vsi->port_info->phy.link_info.topo_media_conflict ==
1874 	    ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
1875 		return -EPERM;
1876 
1877 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
1878 		return ice_force_phys_link_state(vsi, true);
1879 
1880 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1881 	if (!pcaps)
1882 		return -ENOMEM;
1883 
1884 	/* Get current PHY config */
1885 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1886 				     NULL);
1887 	if (status) {
1888 		dev_err(dev, "Failed to get PHY configuration, VSI %d error %s\n",
1889 			vsi->vsi_num, ice_stat_str(status));
1890 		err = -EIO;
1891 		goto done;
1892 	}
1893 
1894 	/* If PHY enable link is configured and configuration has not changed,
1895 	 * there's nothing to do
1896 	 */
1897 	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
1898 	    ice_phy_caps_equals_cfg(pcaps, &pi->phy.curr_user_phy_cfg))
1899 		goto done;
1900 
1901 	/* Use PHY topology as baseline for configuration */
1902 	memset(pcaps, 0, sizeof(*pcaps));
1903 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1904 				     NULL);
1905 	if (status) {
1906 		dev_err(dev, "Failed to get PHY topology, VSI %d error %s\n",
1907 			vsi->vsi_num, ice_stat_str(status));
1908 		err = -EIO;
1909 		goto done;
1910 	}
1911 
1912 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1913 	if (!cfg) {
1914 		err = -ENOMEM;
1915 		goto done;
1916 	}
1917 
1918 	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
1919 
1920 	/* Speed - If default override pending, use curr_user_phy_cfg set in
1921 	 * ice_init_phy_user_cfg_ldo.
1922 	 */
1923 	if (test_and_clear_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING,
1924 			       vsi->back->state)) {
1925 		cfg->phy_type_low = pi->phy.curr_user_phy_cfg.phy_type_low;
1926 		cfg->phy_type_high = pi->phy.curr_user_phy_cfg.phy_type_high;
1927 	} else {
1928 		u64 phy_low = 0, phy_high = 0;
1929 
1930 		ice_update_phy_type(&phy_low, &phy_high,
1931 				    pi->phy.curr_user_speed_req);
1932 		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
1933 		cfg->phy_type_high = pcaps->phy_type_high &
1934 				     cpu_to_le64(phy_high);
1935 	}
1936 
1937 	/* Can't provide what was requested; use PHY capabilities */
1938 	if (!cfg->phy_type_low && !cfg->phy_type_high) {
1939 		cfg->phy_type_low = pcaps->phy_type_low;
1940 		cfg->phy_type_high = pcaps->phy_type_high;
1941 	}
1942 
1943 	/* FEC */
1944 	ice_cfg_phy_fec(pi, cfg, pi->phy.curr_user_fec_req);
1945 
1946 	/* Can't provide what was requested; use PHY capabilities */
1947 	if (cfg->link_fec_opt !=
1948 	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
1949 		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
1950 		cfg->link_fec_opt = pcaps->link_fec_options;
1951 	}
1952 
1953 	/* Flow Control - always supported; no need to check against
1954 	 * capabilities
1955 	 */
1956 	ice_cfg_phy_fc(pi, cfg, pi->phy.curr_user_fc_req);
1957 
1958 	/* Enable link and link update */
1959 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
1960 
1961 	status = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1962 	if (status) {
1963 		dev_err(dev, "Failed to set phy config, VSI %d error %s\n",
1964 			vsi->vsi_num, ice_stat_str(status));
1965 		err = -EIO;
1966 	}
1967 
1968 	kfree(cfg);
1969 done:
1970 	kfree(pcaps);
1971 	return err;
1972 }
1973 
1974 /**
1975  * ice_check_media_subtask - Check for media
1976  * @pf: pointer to PF struct
1977  *
1978  * If media is available, then initialize PHY user configuration if it is not
1979  * been, and configure the PHY if the interface is up.
1980  */
1981 static void ice_check_media_subtask(struct ice_pf *pf)
1982 {
1983 	struct ice_port_info *pi;
1984 	struct ice_vsi *vsi;
1985 	int err;
1986 
1987 	/* No need to check for media if it's already present */
1988 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
1989 		return;
1990 
1991 	vsi = ice_get_main_vsi(pf);
1992 	if (!vsi)
1993 		return;
1994 
1995 	/* Refresh link info and check if media is present */
1996 	pi = vsi->port_info;
1997 	err = ice_update_link_info(pi);
1998 	if (err)
1999 		return;
2000 
2001 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2002 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state))
2003 			ice_init_phy_user_cfg(pi);
2004 
2005 		/* PHY settings are reset on media insertion, reconfigure
2006 		 * PHY to preserve settings.
2007 		 */
2008 		if (test_bit(__ICE_DOWN, vsi->state) &&
2009 		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2010 			return;
2011 
2012 		err = ice_configure_phy(vsi);
2013 		if (!err)
2014 			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2015 
2016 		/* A Link Status Event will be generated; the event handler
2017 		 * will complete bringing the interface up
2018 		 */
2019 	}
2020 }
2021 
2022 /**
2023  * ice_service_task - manage and run subtasks
2024  * @work: pointer to work_struct contained by the PF struct
2025  */
2026 static void ice_service_task(struct work_struct *work)
2027 {
2028 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2029 	unsigned long start_time = jiffies;
2030 
2031 	/* subtasks */
2032 
2033 	/* process reset requests first */
2034 	ice_reset_subtask(pf);
2035 
2036 	/* bail if a reset/recovery cycle is pending or rebuild failed */
2037 	if (ice_is_reset_in_progress(pf->state) ||
2038 	    test_bit(__ICE_SUSPENDED, pf->state) ||
2039 	    test_bit(__ICE_NEEDS_RESTART, pf->state)) {
2040 		ice_service_task_complete(pf);
2041 		return;
2042 	}
2043 
2044 	ice_clean_adminq_subtask(pf);
2045 	ice_check_media_subtask(pf);
2046 	ice_check_for_hang_subtask(pf);
2047 	ice_sync_fltr_subtask(pf);
2048 	ice_handle_mdd_event(pf);
2049 	ice_watchdog_subtask(pf);
2050 
2051 	if (ice_is_safe_mode(pf)) {
2052 		ice_service_task_complete(pf);
2053 		return;
2054 	}
2055 
2056 	ice_process_vflr_event(pf);
2057 	ice_clean_mailboxq_subtask(pf);
2058 	ice_sync_arfs_fltrs(pf);
2059 	/* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
2060 	ice_service_task_complete(pf);
2061 
2062 	/* If the tasks have taken longer than one service timer period
2063 	 * or there is more work to be done, reset the service timer to
2064 	 * schedule the service task now.
2065 	 */
2066 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2067 	    test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
2068 	    test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
2069 	    test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2070 	    test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
2071 		mod_timer(&pf->serv_tmr, jiffies);
2072 }
2073 
2074 /**
2075  * ice_set_ctrlq_len - helper function to set controlq length
2076  * @hw: pointer to the HW instance
2077  */
2078 static void ice_set_ctrlq_len(struct ice_hw *hw)
2079 {
2080 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2081 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2082 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2083 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2084 	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2085 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2086 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2087 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2088 }
2089 
2090 /**
2091  * ice_schedule_reset - schedule a reset
2092  * @pf: board private structure
2093  * @reset: reset being requested
2094  */
2095 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2096 {
2097 	struct device *dev = ice_pf_to_dev(pf);
2098 
2099 	/* bail out if earlier reset has failed */
2100 	if (test_bit(__ICE_RESET_FAILED, pf->state)) {
2101 		dev_dbg(dev, "earlier reset has failed\n");
2102 		return -EIO;
2103 	}
2104 	/* bail if reset/recovery already in progress */
2105 	if (ice_is_reset_in_progress(pf->state)) {
2106 		dev_dbg(dev, "Reset already in progress\n");
2107 		return -EBUSY;
2108 	}
2109 
2110 	switch (reset) {
2111 	case ICE_RESET_PFR:
2112 		set_bit(__ICE_PFR_REQ, pf->state);
2113 		break;
2114 	case ICE_RESET_CORER:
2115 		set_bit(__ICE_CORER_REQ, pf->state);
2116 		break;
2117 	case ICE_RESET_GLOBR:
2118 		set_bit(__ICE_GLOBR_REQ, pf->state);
2119 		break;
2120 	default:
2121 		return -EINVAL;
2122 	}
2123 
2124 	ice_service_task_schedule(pf);
2125 	return 0;
2126 }
2127 
2128 /**
2129  * ice_irq_affinity_notify - Callback for affinity changes
2130  * @notify: context as to what irq was changed
2131  * @mask: the new affinity mask
2132  *
2133  * This is a callback function used by the irq_set_affinity_notifier function
2134  * so that we may register to receive changes to the irq affinity masks.
2135  */
2136 static void
2137 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2138 			const cpumask_t *mask)
2139 {
2140 	struct ice_q_vector *q_vector =
2141 		container_of(notify, struct ice_q_vector, affinity_notify);
2142 
2143 	cpumask_copy(&q_vector->affinity_mask, mask);
2144 }
2145 
2146 /**
2147  * ice_irq_affinity_release - Callback for affinity notifier release
2148  * @ref: internal core kernel usage
2149  *
2150  * This is a callback function used by the irq_set_affinity_notifier function
2151  * to inform the current notification subscriber that they will no longer
2152  * receive notifications.
2153  */
2154 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2155 
2156 /**
2157  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2158  * @vsi: the VSI being configured
2159  */
2160 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2161 {
2162 	struct ice_hw *hw = &vsi->back->hw;
2163 	int i;
2164 
2165 	ice_for_each_q_vector(vsi, i)
2166 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2167 
2168 	ice_flush(hw);
2169 	return 0;
2170 }
2171 
2172 /**
2173  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2174  * @vsi: the VSI being configured
2175  * @basename: name for the vector
2176  */
2177 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2178 {
2179 	int q_vectors = vsi->num_q_vectors;
2180 	struct ice_pf *pf = vsi->back;
2181 	int base = vsi->base_vector;
2182 	struct device *dev;
2183 	int rx_int_idx = 0;
2184 	int tx_int_idx = 0;
2185 	int vector, err;
2186 	int irq_num;
2187 
2188 	dev = ice_pf_to_dev(pf);
2189 	for (vector = 0; vector < q_vectors; vector++) {
2190 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2191 
2192 		irq_num = pf->msix_entries[base + vector].vector;
2193 
2194 		if (q_vector->tx.ring && q_vector->rx.ring) {
2195 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2196 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2197 			tx_int_idx++;
2198 		} else if (q_vector->rx.ring) {
2199 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2200 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2201 		} else if (q_vector->tx.ring) {
2202 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2203 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2204 		} else {
2205 			/* skip this unused q_vector */
2206 			continue;
2207 		}
2208 		err = devm_request_irq(dev, irq_num, vsi->irq_handler, 0,
2209 				       q_vector->name, q_vector);
2210 		if (err) {
2211 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2212 				   err);
2213 			goto free_q_irqs;
2214 		}
2215 
2216 		/* register for affinity change notifications */
2217 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2218 			struct irq_affinity_notify *affinity_notify;
2219 
2220 			affinity_notify = &q_vector->affinity_notify;
2221 			affinity_notify->notify = ice_irq_affinity_notify;
2222 			affinity_notify->release = ice_irq_affinity_release;
2223 			irq_set_affinity_notifier(irq_num, affinity_notify);
2224 		}
2225 
2226 		/* assign the mask for this irq */
2227 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2228 	}
2229 
2230 	vsi->irqs_ready = true;
2231 	return 0;
2232 
2233 free_q_irqs:
2234 	while (vector) {
2235 		vector--;
2236 		irq_num = pf->msix_entries[base + vector].vector;
2237 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2238 			irq_set_affinity_notifier(irq_num, NULL);
2239 		irq_set_affinity_hint(irq_num, NULL);
2240 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2241 	}
2242 	return err;
2243 }
2244 
2245 /**
2246  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2247  * @vsi: VSI to setup Tx rings used by XDP
2248  *
2249  * Return 0 on success and negative value on error
2250  */
2251 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2252 {
2253 	struct device *dev = ice_pf_to_dev(vsi->back);
2254 	int i;
2255 
2256 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2257 		u16 xdp_q_idx = vsi->alloc_txq + i;
2258 		struct ice_ring *xdp_ring;
2259 
2260 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2261 
2262 		if (!xdp_ring)
2263 			goto free_xdp_rings;
2264 
2265 		xdp_ring->q_index = xdp_q_idx;
2266 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2267 		xdp_ring->ring_active = false;
2268 		xdp_ring->vsi = vsi;
2269 		xdp_ring->netdev = NULL;
2270 		xdp_ring->dev = dev;
2271 		xdp_ring->count = vsi->num_tx_desc;
2272 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2273 		if (ice_setup_tx_ring(xdp_ring))
2274 			goto free_xdp_rings;
2275 		ice_set_ring_xdp(xdp_ring);
2276 		xdp_ring->xsk_umem = ice_xsk_umem(xdp_ring);
2277 	}
2278 
2279 	return 0;
2280 
2281 free_xdp_rings:
2282 	for (; i >= 0; i--)
2283 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2284 			ice_free_tx_ring(vsi->xdp_rings[i]);
2285 	return -ENOMEM;
2286 }
2287 
2288 /**
2289  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2290  * @vsi: VSI to set the bpf prog on
2291  * @prog: the bpf prog pointer
2292  */
2293 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2294 {
2295 	struct bpf_prog *old_prog;
2296 	int i;
2297 
2298 	old_prog = xchg(&vsi->xdp_prog, prog);
2299 	if (old_prog)
2300 		bpf_prog_put(old_prog);
2301 
2302 	ice_for_each_rxq(vsi, i)
2303 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2304 }
2305 
2306 /**
2307  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2308  * @vsi: VSI to bring up Tx rings used by XDP
2309  * @prog: bpf program that will be assigned to VSI
2310  *
2311  * Return 0 on success and negative value on error
2312  */
2313 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2314 {
2315 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2316 	int xdp_rings_rem = vsi->num_xdp_txq;
2317 	struct ice_pf *pf = vsi->back;
2318 	struct ice_qs_cfg xdp_qs_cfg = {
2319 		.qs_mutex = &pf->avail_q_mutex,
2320 		.pf_map = pf->avail_txqs,
2321 		.pf_map_size = pf->max_pf_txqs,
2322 		.q_count = vsi->num_xdp_txq,
2323 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2324 		.vsi_map = vsi->txq_map,
2325 		.vsi_map_offset = vsi->alloc_txq,
2326 		.mapping_mode = ICE_VSI_MAP_CONTIG
2327 	};
2328 	enum ice_status status;
2329 	struct device *dev;
2330 	int i, v_idx;
2331 
2332 	dev = ice_pf_to_dev(pf);
2333 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2334 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2335 	if (!vsi->xdp_rings)
2336 		return -ENOMEM;
2337 
2338 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2339 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2340 		goto err_map_xdp;
2341 
2342 	if (ice_xdp_alloc_setup_rings(vsi))
2343 		goto clear_xdp_rings;
2344 
2345 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2346 	ice_for_each_q_vector(vsi, v_idx) {
2347 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2348 		int xdp_rings_per_v, q_id, q_base;
2349 
2350 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2351 					       vsi->num_q_vectors - v_idx);
2352 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2353 
2354 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2355 			struct ice_ring *xdp_ring = vsi->xdp_rings[q_id];
2356 
2357 			xdp_ring->q_vector = q_vector;
2358 			xdp_ring->next = q_vector->tx.ring;
2359 			q_vector->tx.ring = xdp_ring;
2360 		}
2361 		xdp_rings_rem -= xdp_rings_per_v;
2362 	}
2363 
2364 	/* omit the scheduler update if in reset path; XDP queues will be
2365 	 * taken into account at the end of ice_vsi_rebuild, where
2366 	 * ice_cfg_vsi_lan is being called
2367 	 */
2368 	if (ice_is_reset_in_progress(pf->state))
2369 		return 0;
2370 
2371 	/* tell the Tx scheduler that right now we have
2372 	 * additional queues
2373 	 */
2374 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2375 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2376 
2377 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2378 				 max_txqs);
2379 	if (status) {
2380 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %s\n",
2381 			ice_stat_str(status));
2382 		goto clear_xdp_rings;
2383 	}
2384 	ice_vsi_assign_bpf_prog(vsi, prog);
2385 
2386 	return 0;
2387 clear_xdp_rings:
2388 	for (i = 0; i < vsi->num_xdp_txq; i++)
2389 		if (vsi->xdp_rings[i]) {
2390 			kfree_rcu(vsi->xdp_rings[i], rcu);
2391 			vsi->xdp_rings[i] = NULL;
2392 		}
2393 
2394 err_map_xdp:
2395 	mutex_lock(&pf->avail_q_mutex);
2396 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2397 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2398 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2399 	}
2400 	mutex_unlock(&pf->avail_q_mutex);
2401 
2402 	devm_kfree(dev, vsi->xdp_rings);
2403 	return -ENOMEM;
2404 }
2405 
2406 /**
2407  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2408  * @vsi: VSI to remove XDP rings
2409  *
2410  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2411  * resources
2412  */
2413 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2414 {
2415 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2416 	struct ice_pf *pf = vsi->back;
2417 	int i, v_idx;
2418 
2419 	/* q_vectors are freed in reset path so there's no point in detaching
2420 	 * rings; in case of rebuild being triggered not from reset reset bits
2421 	 * in pf->state won't be set, so additionally check first q_vector
2422 	 * against NULL
2423 	 */
2424 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2425 		goto free_qmap;
2426 
2427 	ice_for_each_q_vector(vsi, v_idx) {
2428 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2429 		struct ice_ring *ring;
2430 
2431 		ice_for_each_ring(ring, q_vector->tx)
2432 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2433 				break;
2434 
2435 		/* restore the value of last node prior to XDP setup */
2436 		q_vector->tx.ring = ring;
2437 	}
2438 
2439 free_qmap:
2440 	mutex_lock(&pf->avail_q_mutex);
2441 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2442 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2443 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2444 	}
2445 	mutex_unlock(&pf->avail_q_mutex);
2446 
2447 	for (i = 0; i < vsi->num_xdp_txq; i++)
2448 		if (vsi->xdp_rings[i]) {
2449 			if (vsi->xdp_rings[i]->desc)
2450 				ice_free_tx_ring(vsi->xdp_rings[i]);
2451 			kfree_rcu(vsi->xdp_rings[i], rcu);
2452 			vsi->xdp_rings[i] = NULL;
2453 		}
2454 
2455 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2456 	vsi->xdp_rings = NULL;
2457 
2458 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2459 		return 0;
2460 
2461 	ice_vsi_assign_bpf_prog(vsi, NULL);
2462 
2463 	/* notify Tx scheduler that we destroyed XDP queues and bring
2464 	 * back the old number of child nodes
2465 	 */
2466 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2467 		max_txqs[i] = vsi->num_txq;
2468 
2469 	/* change number of XDP Tx queues to 0 */
2470 	vsi->num_xdp_txq = 0;
2471 
2472 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2473 			       max_txqs);
2474 }
2475 
2476 /**
2477  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2478  * @vsi: VSI to setup XDP for
2479  * @prog: XDP program
2480  * @extack: netlink extended ack
2481  */
2482 static int
2483 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2484 		   struct netlink_ext_ack *extack)
2485 {
2486 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2487 	bool if_running = netif_running(vsi->netdev);
2488 	int ret = 0, xdp_ring_err = 0;
2489 
2490 	if (frame_size > vsi->rx_buf_len) {
2491 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2492 		return -EOPNOTSUPP;
2493 	}
2494 
2495 	/* need to stop netdev while setting up the program for Rx rings */
2496 	if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) {
2497 		ret = ice_down(vsi);
2498 		if (ret) {
2499 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2500 			return ret;
2501 		}
2502 	}
2503 
2504 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2505 		vsi->num_xdp_txq = vsi->alloc_rxq;
2506 		xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2507 		if (xdp_ring_err)
2508 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2509 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2510 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2511 		if (xdp_ring_err)
2512 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2513 	} else {
2514 		ice_vsi_assign_bpf_prog(vsi, prog);
2515 	}
2516 
2517 	if (if_running)
2518 		ret = ice_up(vsi);
2519 
2520 	if (!ret && prog && vsi->xsk_umems) {
2521 		int i;
2522 
2523 		ice_for_each_rxq(vsi, i) {
2524 			struct ice_ring *rx_ring = vsi->rx_rings[i];
2525 
2526 			if (rx_ring->xsk_umem)
2527 				napi_schedule(&rx_ring->q_vector->napi);
2528 		}
2529 	}
2530 
2531 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2532 }
2533 
2534 /**
2535  * ice_xdp - implements XDP handler
2536  * @dev: netdevice
2537  * @xdp: XDP command
2538  */
2539 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2540 {
2541 	struct ice_netdev_priv *np = netdev_priv(dev);
2542 	struct ice_vsi *vsi = np->vsi;
2543 
2544 	if (vsi->type != ICE_VSI_PF) {
2545 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2546 		return -EINVAL;
2547 	}
2548 
2549 	switch (xdp->command) {
2550 	case XDP_SETUP_PROG:
2551 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2552 	case XDP_SETUP_XSK_UMEM:
2553 		return ice_xsk_umem_setup(vsi, xdp->xsk.umem,
2554 					  xdp->xsk.queue_id);
2555 	default:
2556 		return -EINVAL;
2557 	}
2558 }
2559 
2560 /**
2561  * ice_ena_misc_vector - enable the non-queue interrupts
2562  * @pf: board private structure
2563  */
2564 static void ice_ena_misc_vector(struct ice_pf *pf)
2565 {
2566 	struct ice_hw *hw = &pf->hw;
2567 	u32 val;
2568 
2569 	/* Disable anti-spoof detection interrupt to prevent spurious event
2570 	 * interrupts during a function reset. Anti-spoof functionally is
2571 	 * still supported.
2572 	 */
2573 	val = rd32(hw, GL_MDCK_TX_TDPU);
2574 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2575 	wr32(hw, GL_MDCK_TX_TDPU, val);
2576 
2577 	/* clear things first */
2578 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
2579 	rd32(hw, PFINT_OICR);		/* read to clear */
2580 
2581 	val = (PFINT_OICR_ECC_ERR_M |
2582 	       PFINT_OICR_MAL_DETECT_M |
2583 	       PFINT_OICR_GRST_M |
2584 	       PFINT_OICR_PCI_EXCEPTION_M |
2585 	       PFINT_OICR_VFLR_M |
2586 	       PFINT_OICR_HMC_ERR_M |
2587 	       PFINT_OICR_PE_CRITERR_M);
2588 
2589 	wr32(hw, PFINT_OICR_ENA, val);
2590 
2591 	/* SW_ITR_IDX = 0, but don't change INTENA */
2592 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2593 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2594 }
2595 
2596 /**
2597  * ice_misc_intr - misc interrupt handler
2598  * @irq: interrupt number
2599  * @data: pointer to a q_vector
2600  */
2601 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2602 {
2603 	struct ice_pf *pf = (struct ice_pf *)data;
2604 	struct ice_hw *hw = &pf->hw;
2605 	irqreturn_t ret = IRQ_NONE;
2606 	struct device *dev;
2607 	u32 oicr, ena_mask;
2608 
2609 	dev = ice_pf_to_dev(pf);
2610 	set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2611 	set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2612 
2613 	oicr = rd32(hw, PFINT_OICR);
2614 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2615 
2616 	if (oicr & PFINT_OICR_SWINT_M) {
2617 		ena_mask &= ~PFINT_OICR_SWINT_M;
2618 		pf->sw_int_count++;
2619 	}
2620 
2621 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2622 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2623 		set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2624 	}
2625 	if (oicr & PFINT_OICR_VFLR_M) {
2626 		/* disable any further VFLR event notifications */
2627 		if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
2628 			u32 reg = rd32(hw, PFINT_OICR_ENA);
2629 
2630 			reg &= ~PFINT_OICR_VFLR_M;
2631 			wr32(hw, PFINT_OICR_ENA, reg);
2632 		} else {
2633 			ena_mask &= ~PFINT_OICR_VFLR_M;
2634 			set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
2635 		}
2636 	}
2637 
2638 	if (oicr & PFINT_OICR_GRST_M) {
2639 		u32 reset;
2640 
2641 		/* we have a reset warning */
2642 		ena_mask &= ~PFINT_OICR_GRST_M;
2643 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2644 			GLGEN_RSTAT_RESET_TYPE_S;
2645 
2646 		if (reset == ICE_RESET_CORER)
2647 			pf->corer_count++;
2648 		else if (reset == ICE_RESET_GLOBR)
2649 			pf->globr_count++;
2650 		else if (reset == ICE_RESET_EMPR)
2651 			pf->empr_count++;
2652 		else
2653 			dev_dbg(dev, "Invalid reset type %d\n", reset);
2654 
2655 		/* If a reset cycle isn't already in progress, we set a bit in
2656 		 * pf->state so that the service task can start a reset/rebuild.
2657 		 * We also make note of which reset happened so that peer
2658 		 * devices/drivers can be informed.
2659 		 */
2660 		if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
2661 			if (reset == ICE_RESET_CORER)
2662 				set_bit(__ICE_CORER_RECV, pf->state);
2663 			else if (reset == ICE_RESET_GLOBR)
2664 				set_bit(__ICE_GLOBR_RECV, pf->state);
2665 			else
2666 				set_bit(__ICE_EMPR_RECV, pf->state);
2667 
2668 			/* There are couple of different bits at play here.
2669 			 * hw->reset_ongoing indicates whether the hardware is
2670 			 * in reset. This is set to true when a reset interrupt
2671 			 * is received and set back to false after the driver
2672 			 * has determined that the hardware is out of reset.
2673 			 *
2674 			 * __ICE_RESET_OICR_RECV in pf->state indicates
2675 			 * that a post reset rebuild is required before the
2676 			 * driver is operational again. This is set above.
2677 			 *
2678 			 * As this is the start of the reset/rebuild cycle, set
2679 			 * both to indicate that.
2680 			 */
2681 			hw->reset_ongoing = true;
2682 		}
2683 	}
2684 
2685 	if (oicr & PFINT_OICR_HMC_ERR_M) {
2686 		ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2687 		dev_dbg(dev, "HMC Error interrupt - info 0x%x, data 0x%x\n",
2688 			rd32(hw, PFHMC_ERRORINFO),
2689 			rd32(hw, PFHMC_ERRORDATA));
2690 	}
2691 
2692 	/* Report any remaining unexpected interrupts */
2693 	oicr &= ena_mask;
2694 	if (oicr) {
2695 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
2696 		/* If a critical error is pending there is no choice but to
2697 		 * reset the device.
2698 		 */
2699 		if (oicr & (PFINT_OICR_PE_CRITERR_M |
2700 			    PFINT_OICR_PCI_EXCEPTION_M |
2701 			    PFINT_OICR_ECC_ERR_M)) {
2702 			set_bit(__ICE_PFR_REQ, pf->state);
2703 			ice_service_task_schedule(pf);
2704 		}
2705 	}
2706 	ret = IRQ_HANDLED;
2707 
2708 	ice_service_task_schedule(pf);
2709 	ice_irq_dynamic_ena(hw, NULL, NULL);
2710 
2711 	return ret;
2712 }
2713 
2714 /**
2715  * ice_dis_ctrlq_interrupts - disable control queue interrupts
2716  * @hw: pointer to HW structure
2717  */
2718 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
2719 {
2720 	/* disable Admin queue Interrupt causes */
2721 	wr32(hw, PFINT_FW_CTL,
2722 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
2723 
2724 	/* disable Mailbox queue Interrupt causes */
2725 	wr32(hw, PFINT_MBX_CTL,
2726 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
2727 
2728 	/* disable Control queue Interrupt causes */
2729 	wr32(hw, PFINT_OICR_CTL,
2730 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
2731 
2732 	ice_flush(hw);
2733 }
2734 
2735 /**
2736  * ice_free_irq_msix_misc - Unroll misc vector setup
2737  * @pf: board private structure
2738  */
2739 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2740 {
2741 	struct ice_hw *hw = &pf->hw;
2742 
2743 	ice_dis_ctrlq_interrupts(hw);
2744 
2745 	/* disable OICR interrupt */
2746 	wr32(hw, PFINT_OICR_ENA, 0);
2747 	ice_flush(hw);
2748 
2749 	if (pf->msix_entries) {
2750 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2751 		devm_free_irq(ice_pf_to_dev(pf),
2752 			      pf->msix_entries[pf->oicr_idx].vector, pf);
2753 	}
2754 
2755 	pf->num_avail_sw_msix += 1;
2756 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2757 }
2758 
2759 /**
2760  * ice_ena_ctrlq_interrupts - enable control queue interrupts
2761  * @hw: pointer to HW structure
2762  * @reg_idx: HW vector index to associate the control queue interrupts with
2763  */
2764 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
2765 {
2766 	u32 val;
2767 
2768 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2769 	       PFINT_OICR_CTL_CAUSE_ENA_M);
2770 	wr32(hw, PFINT_OICR_CTL, val);
2771 
2772 	/* enable Admin queue Interrupt causes */
2773 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2774 	       PFINT_FW_CTL_CAUSE_ENA_M);
2775 	wr32(hw, PFINT_FW_CTL, val);
2776 
2777 	/* enable Mailbox queue Interrupt causes */
2778 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
2779 	       PFINT_MBX_CTL_CAUSE_ENA_M);
2780 	wr32(hw, PFINT_MBX_CTL, val);
2781 
2782 	ice_flush(hw);
2783 }
2784 
2785 /**
2786  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2787  * @pf: board private structure
2788  *
2789  * This sets up the handler for MSIX 0, which is used to manage the
2790  * non-queue interrupts, e.g. AdminQ and errors. This is not used
2791  * when in MSI or Legacy interrupt mode.
2792  */
2793 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2794 {
2795 	struct device *dev = ice_pf_to_dev(pf);
2796 	struct ice_hw *hw = &pf->hw;
2797 	int oicr_idx, err = 0;
2798 
2799 	if (!pf->int_name[0])
2800 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2801 			 dev_driver_string(dev), dev_name(dev));
2802 
2803 	/* Do not request IRQ but do enable OICR interrupt since settings are
2804 	 * lost during reset. Note that this function is called only during
2805 	 * rebuild path and not while reset is in progress.
2806 	 */
2807 	if (ice_is_reset_in_progress(pf->state))
2808 		goto skip_req_irq;
2809 
2810 	/* reserve one vector in irq_tracker for misc interrupts */
2811 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2812 	if (oicr_idx < 0)
2813 		return oicr_idx;
2814 
2815 	pf->num_avail_sw_msix -= 1;
2816 	pf->oicr_idx = (u16)oicr_idx;
2817 
2818 	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
2819 			       ice_misc_intr, 0, pf->int_name, pf);
2820 	if (err) {
2821 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
2822 			pf->int_name, err);
2823 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2824 		pf->num_avail_sw_msix += 1;
2825 		return err;
2826 	}
2827 
2828 skip_req_irq:
2829 	ice_ena_misc_vector(pf);
2830 
2831 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
2832 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2833 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
2834 
2835 	ice_flush(hw);
2836 	ice_irq_dynamic_ena(hw, NULL, NULL);
2837 
2838 	return 0;
2839 }
2840 
2841 /**
2842  * ice_napi_add - register NAPI handler for the VSI
2843  * @vsi: VSI for which NAPI handler is to be registered
2844  *
2845  * This function is only called in the driver's load path. Registering the NAPI
2846  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2847  * reset/rebuild, etc.)
2848  */
2849 static void ice_napi_add(struct ice_vsi *vsi)
2850 {
2851 	int v_idx;
2852 
2853 	if (!vsi->netdev)
2854 		return;
2855 
2856 	ice_for_each_q_vector(vsi, v_idx)
2857 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
2858 			       ice_napi_poll, NAPI_POLL_WEIGHT);
2859 }
2860 
2861 /**
2862  * ice_set_ops - set netdev and ethtools ops for the given netdev
2863  * @netdev: netdev instance
2864  */
2865 static void ice_set_ops(struct net_device *netdev)
2866 {
2867 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2868 
2869 	if (ice_is_safe_mode(pf)) {
2870 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
2871 		ice_set_ethtool_safe_mode_ops(netdev);
2872 		return;
2873 	}
2874 
2875 	netdev->netdev_ops = &ice_netdev_ops;
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 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->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4077 	if (!pf->num_alloc_vsi) {
4078 		err = -EIO;
4079 		goto err_init_pf_unroll;
4080 	}
4081 
4082 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4083 			       GFP_KERNEL);
4084 	if (!pf->vsi) {
4085 		err = -ENOMEM;
4086 		goto err_init_pf_unroll;
4087 	}
4088 
4089 	err = ice_init_interrupt_scheme(pf);
4090 	if (err) {
4091 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4092 		err = -EIO;
4093 		goto err_init_vsi_unroll;
4094 	}
4095 
4096 	/* In case of MSIX we are going to setup the misc vector right here
4097 	 * to handle admin queue events etc. In case of legacy and MSI
4098 	 * the misc functionality and queue processing is combined in
4099 	 * the same vector and that gets setup at open.
4100 	 */
4101 	err = ice_req_irq_msix_misc(pf);
4102 	if (err) {
4103 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4104 		goto err_init_interrupt_unroll;
4105 	}
4106 
4107 	/* create switch struct for the switch element created by FW on boot */
4108 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4109 	if (!pf->first_sw) {
4110 		err = -ENOMEM;
4111 		goto err_msix_misc_unroll;
4112 	}
4113 
4114 	if (hw->evb_veb)
4115 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4116 	else
4117 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4118 
4119 	pf->first_sw->pf = pf;
4120 
4121 	/* record the sw_id available for later use */
4122 	pf->first_sw->sw_id = hw->port_info->sw_id;
4123 
4124 	err = ice_setup_pf_sw(pf);
4125 	if (err) {
4126 		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4127 		goto err_alloc_sw_unroll;
4128 	}
4129 
4130 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4131 
4132 	/* tell the firmware we are up */
4133 	err = ice_send_version(pf);
4134 	if (err) {
4135 		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4136 			UTS_RELEASE, err);
4137 		goto err_send_version_unroll;
4138 	}
4139 
4140 	/* since everything is good, start the service timer */
4141 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4142 
4143 	err = ice_init_link_events(pf->hw.port_info);
4144 	if (err) {
4145 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4146 		goto err_send_version_unroll;
4147 	}
4148 
4149 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4150 	if (err) {
4151 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4152 		goto err_send_version_unroll;
4153 	}
4154 
4155 	err = ice_update_link_info(pf->hw.port_info);
4156 	if (err) {
4157 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4158 		goto err_send_version_unroll;
4159 	}
4160 
4161 	ice_init_link_dflt_override(pf->hw.port_info);
4162 
4163 	/* if media available, initialize PHY settings */
4164 	if (pf->hw.port_info->phy.link_info.link_info &
4165 	    ICE_AQ_MEDIA_AVAILABLE) {
4166 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4167 		if (err) {
4168 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4169 			goto err_send_version_unroll;
4170 		}
4171 
4172 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4173 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4174 
4175 			if (vsi)
4176 				ice_configure_phy(vsi);
4177 		}
4178 	} else {
4179 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4180 	}
4181 
4182 	ice_verify_cacheline_size(pf);
4183 
4184 	/* Save wakeup reason register for later use */
4185 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4186 
4187 	/* check for a power management event */
4188 	ice_print_wake_reason(pf);
4189 
4190 	/* clear wake status, all bits */
4191 	wr32(hw, PFPM_WUS, U32_MAX);
4192 
4193 	/* Disable WoL at init, wait for user to enable */
4194 	device_set_wakeup_enable(dev, false);
4195 
4196 	if (ice_is_safe_mode(pf)) {
4197 		ice_set_safe_mode_vlan_cfg(pf);
4198 		goto probe_done;
4199 	}
4200 
4201 	/* initialize DDP driven features */
4202 
4203 	/* Note: Flow director init failure is non-fatal to load */
4204 	if (ice_init_fdir(pf))
4205 		dev_err(dev, "could not initialize flow director\n");
4206 
4207 	/* Note: DCB init failure is non-fatal to load */
4208 	if (ice_init_pf_dcb(pf, false)) {
4209 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4210 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4211 	} else {
4212 		ice_cfg_lldp_mib_change(&pf->hw, true);
4213 	}
4214 
4215 	/* print PCI link speed and width */
4216 	pcie_print_link_status(pf->pdev);
4217 
4218 probe_done:
4219 	/* ready to go, so clear down state bit */
4220 	clear_bit(__ICE_DOWN, pf->state);
4221 	return 0;
4222 
4223 err_send_version_unroll:
4224 	ice_vsi_release_all(pf);
4225 err_alloc_sw_unroll:
4226 	ice_devlink_destroy_port(pf);
4227 	set_bit(__ICE_SERVICE_DIS, pf->state);
4228 	set_bit(__ICE_DOWN, pf->state);
4229 	devm_kfree(dev, pf->first_sw);
4230 err_msix_misc_unroll:
4231 	ice_free_irq_msix_misc(pf);
4232 err_init_interrupt_unroll:
4233 	ice_clear_interrupt_scheme(pf);
4234 err_init_vsi_unroll:
4235 	devm_kfree(dev, pf->vsi);
4236 err_init_pf_unroll:
4237 	ice_deinit_pf(pf);
4238 	ice_devlink_destroy_regions(pf);
4239 	ice_deinit_hw(hw);
4240 err_exit_unroll:
4241 	ice_devlink_unregister(pf);
4242 	pci_disable_pcie_error_reporting(pdev);
4243 	pci_disable_device(pdev);
4244 	return err;
4245 }
4246 
4247 /**
4248  * ice_set_wake - enable or disable Wake on LAN
4249  * @pf: pointer to the PF struct
4250  *
4251  * Simple helper for WoL control
4252  */
4253 static void ice_set_wake(struct ice_pf *pf)
4254 {
4255 	struct ice_hw *hw = &pf->hw;
4256 	bool wol = pf->wol_ena;
4257 
4258 	/* clear wake state, otherwise new wake events won't fire */
4259 	wr32(hw, PFPM_WUS, U32_MAX);
4260 
4261 	/* enable / disable APM wake up, no RMW needed */
4262 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4263 
4264 	/* set magic packet filter enabled */
4265 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4266 }
4267 
4268 /**
4269  * ice_setup_magic_mc_wake - setup device to wake on multicast magic packet
4270  * @pf: pointer to the PF struct
4271  *
4272  * Issue firmware command to enable multicast magic wake, making
4273  * sure that any locally administered address (LAA) is used for
4274  * wake, and that PF reset doesn't undo the LAA.
4275  */
4276 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4277 {
4278 	struct device *dev = ice_pf_to_dev(pf);
4279 	struct ice_hw *hw = &pf->hw;
4280 	enum ice_status status;
4281 	u8 mac_addr[ETH_ALEN];
4282 	struct ice_vsi *vsi;
4283 	u8 flags;
4284 
4285 	if (!pf->wol_ena)
4286 		return;
4287 
4288 	vsi = ice_get_main_vsi(pf);
4289 	if (!vsi)
4290 		return;
4291 
4292 	/* Get current MAC address in case it's an LAA */
4293 	if (vsi->netdev)
4294 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4295 	else
4296 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4297 
4298 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4299 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4300 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4301 
4302 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4303 	if (status)
4304 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %s aq_err %s\n",
4305 			ice_stat_str(status),
4306 			ice_aq_str(hw->adminq.sq_last_status));
4307 }
4308 
4309 /**
4310  * ice_remove - Device removal routine
4311  * @pdev: PCI device information struct
4312  */
4313 static void ice_remove(struct pci_dev *pdev)
4314 {
4315 	struct ice_pf *pf = pci_get_drvdata(pdev);
4316 	int i;
4317 
4318 	if (!pf)
4319 		return;
4320 
4321 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4322 		if (!ice_is_reset_in_progress(pf->state))
4323 			break;
4324 		msleep(100);
4325 	}
4326 
4327 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4328 		set_bit(__ICE_VF_RESETS_DISABLED, pf->state);
4329 		ice_free_vfs(pf);
4330 	}
4331 
4332 	set_bit(__ICE_DOWN, pf->state);
4333 	ice_service_task_stop(pf);
4334 
4335 	ice_aq_cancel_waiting_tasks(pf);
4336 
4337 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4338 	if (!ice_is_safe_mode(pf))
4339 		ice_remove_arfs(pf);
4340 	ice_setup_mc_magic_wake(pf);
4341 	ice_devlink_destroy_port(pf);
4342 	ice_vsi_release_all(pf);
4343 	ice_set_wake(pf);
4344 	ice_free_irq_msix_misc(pf);
4345 	ice_for_each_vsi(pf, i) {
4346 		if (!pf->vsi[i])
4347 			continue;
4348 		ice_vsi_free_q_vectors(pf->vsi[i]);
4349 	}
4350 	ice_deinit_pf(pf);
4351 	ice_devlink_destroy_regions(pf);
4352 	ice_deinit_hw(&pf->hw);
4353 	ice_devlink_unregister(pf);
4354 
4355 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
4356 	 * do it via ice_schedule_reset() since there is no need to rebuild
4357 	 * and the service task is already stopped.
4358 	 */
4359 	ice_reset(&pf->hw, ICE_RESET_PFR);
4360 	pci_wait_for_pending_transaction(pdev);
4361 	ice_clear_interrupt_scheme(pf);
4362 	pci_disable_pcie_error_reporting(pdev);
4363 	pci_disable_device(pdev);
4364 }
4365 
4366 /**
4367  * ice_shutdown - PCI callback for shutting down device
4368  * @pdev: PCI device information struct
4369  */
4370 static void ice_shutdown(struct pci_dev *pdev)
4371 {
4372 	struct ice_pf *pf = pci_get_drvdata(pdev);
4373 
4374 	ice_remove(pdev);
4375 
4376 	if (system_state == SYSTEM_POWER_OFF) {
4377 		pci_wake_from_d3(pdev, pf->wol_ena);
4378 		pci_set_power_state(pdev, PCI_D3hot);
4379 	}
4380 }
4381 
4382 #ifdef CONFIG_PM
4383 /**
4384  * ice_prepare_for_shutdown - prep for PCI shutdown
4385  * @pf: board private structure
4386  *
4387  * Inform or close all dependent features in prep for PCI device shutdown
4388  */
4389 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4390 {
4391 	struct ice_hw *hw = &pf->hw;
4392 	u32 v;
4393 
4394 	/* Notify VFs of impending reset */
4395 	if (ice_check_sq_alive(hw, &hw->mailboxq))
4396 		ice_vc_notify_reset(pf);
4397 
4398 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4399 
4400 	/* disable the VSIs and their queues that are not already DOWN */
4401 	ice_pf_dis_all_vsi(pf, false);
4402 
4403 	ice_for_each_vsi(pf, v)
4404 		if (pf->vsi[v])
4405 			pf->vsi[v]->vsi_num = 0;
4406 
4407 	ice_shutdown_all_ctrlq(hw);
4408 }
4409 
4410 /**
4411  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4412  * @pf: board private structure to reinitialize
4413  *
4414  * This routine reinitialize interrupt scheme that was cleared during
4415  * power management suspend callback.
4416  *
4417  * This should be called during resume routine to re-allocate the q_vectors
4418  * and reacquire interrupts.
4419  */
4420 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4421 {
4422 	struct device *dev = ice_pf_to_dev(pf);
4423 	int ret, v;
4424 
4425 	/* Since we clear MSIX flag during suspend, we need to
4426 	 * set it back during resume...
4427 	 */
4428 
4429 	ret = ice_init_interrupt_scheme(pf);
4430 	if (ret) {
4431 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4432 		return ret;
4433 	}
4434 
4435 	/* Remap vectors and rings, after successful re-init interrupts */
4436 	ice_for_each_vsi(pf, v) {
4437 		if (!pf->vsi[v])
4438 			continue;
4439 
4440 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4441 		if (ret)
4442 			goto err_reinit;
4443 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4444 	}
4445 
4446 	ret = ice_req_irq_msix_misc(pf);
4447 	if (ret) {
4448 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4449 			ret);
4450 		goto err_reinit;
4451 	}
4452 
4453 	return 0;
4454 
4455 err_reinit:
4456 	while (v--)
4457 		if (pf->vsi[v])
4458 			ice_vsi_free_q_vectors(pf->vsi[v]);
4459 
4460 	return ret;
4461 }
4462 
4463 /**
4464  * ice_suspend
4465  * @dev: generic device information structure
4466  *
4467  * Power Management callback to quiesce the device and prepare
4468  * for D3 transition.
4469  */
4470 static int __maybe_unused ice_suspend(struct device *dev)
4471 {
4472 	struct pci_dev *pdev = to_pci_dev(dev);
4473 	struct ice_pf *pf;
4474 	int disabled, v;
4475 
4476 	pf = pci_get_drvdata(pdev);
4477 
4478 	if (!ice_pf_state_is_nominal(pf)) {
4479 		dev_err(dev, "Device is not ready, no need to suspend it\n");
4480 		return -EBUSY;
4481 	}
4482 
4483 	/* Stop watchdog tasks until resume completion.
4484 	 * Even though it is most likely that the service task is
4485 	 * disabled if the device is suspended or down, the service task's
4486 	 * state is controlled by a different state bit, and we should
4487 	 * store and honor whatever state that bit is in at this point.
4488 	 */
4489 	disabled = ice_service_task_stop(pf);
4490 
4491 	/* Already suspended?, then there is nothing to do */
4492 	if (test_and_set_bit(__ICE_SUSPENDED, pf->state)) {
4493 		if (!disabled)
4494 			ice_service_task_restart(pf);
4495 		return 0;
4496 	}
4497 
4498 	if (test_bit(__ICE_DOWN, pf->state) ||
4499 	    ice_is_reset_in_progress(pf->state)) {
4500 		dev_err(dev, "can't suspend device in reset or already down\n");
4501 		if (!disabled)
4502 			ice_service_task_restart(pf);
4503 		return 0;
4504 	}
4505 
4506 	ice_setup_mc_magic_wake(pf);
4507 
4508 	ice_prepare_for_shutdown(pf);
4509 
4510 	ice_set_wake(pf);
4511 
4512 	/* Free vectors, clear the interrupt scheme and release IRQs
4513 	 * for proper hibernation, especially with large number of CPUs.
4514 	 * Otherwise hibernation might fail when mapping all the vectors back
4515 	 * to CPU0.
4516 	 */
4517 	ice_free_irq_msix_misc(pf);
4518 	ice_for_each_vsi(pf, v) {
4519 		if (!pf->vsi[v])
4520 			continue;
4521 		ice_vsi_free_q_vectors(pf->vsi[v]);
4522 	}
4523 	ice_clear_interrupt_scheme(pf);
4524 
4525 	pci_wake_from_d3(pdev, pf->wol_ena);
4526 	pci_set_power_state(pdev, PCI_D3hot);
4527 	return 0;
4528 }
4529 
4530 /**
4531  * ice_resume - PM callback for waking up from D3
4532  * @dev: generic device information structure
4533  */
4534 static int __maybe_unused ice_resume(struct device *dev)
4535 {
4536 	struct pci_dev *pdev = to_pci_dev(dev);
4537 	enum ice_reset_req reset_type;
4538 	struct ice_pf *pf;
4539 	struct ice_hw *hw;
4540 	int ret;
4541 
4542 	pci_set_power_state(pdev, PCI_D0);
4543 	pci_restore_state(pdev);
4544 	pci_save_state(pdev);
4545 
4546 	if (!pci_device_is_present(pdev))
4547 		return -ENODEV;
4548 
4549 	ret = pci_enable_device_mem(pdev);
4550 	if (ret) {
4551 		dev_err(dev, "Cannot enable device after suspend\n");
4552 		return ret;
4553 	}
4554 
4555 	pf = pci_get_drvdata(pdev);
4556 	hw = &pf->hw;
4557 
4558 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4559 	ice_print_wake_reason(pf);
4560 
4561 	/* We cleared the interrupt scheme when we suspended, so we need to
4562 	 * restore it now to resume device functionality.
4563 	 */
4564 	ret = ice_reinit_interrupt_scheme(pf);
4565 	if (ret)
4566 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
4567 
4568 	clear_bit(__ICE_DOWN, pf->state);
4569 	/* Now perform PF reset and rebuild */
4570 	reset_type = ICE_RESET_PFR;
4571 	/* re-enable service task for reset, but allow reset to schedule it */
4572 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4573 
4574 	if (ice_schedule_reset(pf, reset_type))
4575 		dev_err(dev, "Reset during resume failed.\n");
4576 
4577 	clear_bit(__ICE_SUSPENDED, pf->state);
4578 	ice_service_task_restart(pf);
4579 
4580 	/* Restart the service task */
4581 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4582 
4583 	return 0;
4584 }
4585 #endif /* CONFIG_PM */
4586 
4587 /**
4588  * ice_pci_err_detected - warning that PCI error has been detected
4589  * @pdev: PCI device information struct
4590  * @err: the type of PCI error
4591  *
4592  * Called to warn that something happened on the PCI bus and the error handling
4593  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
4594  */
4595 static pci_ers_result_t
4596 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
4597 {
4598 	struct ice_pf *pf = pci_get_drvdata(pdev);
4599 
4600 	if (!pf) {
4601 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
4602 			__func__, err);
4603 		return PCI_ERS_RESULT_DISCONNECT;
4604 	}
4605 
4606 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4607 		ice_service_task_stop(pf);
4608 
4609 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4610 			set_bit(__ICE_PFR_REQ, pf->state);
4611 			ice_prepare_for_reset(pf);
4612 		}
4613 	}
4614 
4615 	return PCI_ERS_RESULT_NEED_RESET;
4616 }
4617 
4618 /**
4619  * ice_pci_err_slot_reset - a PCI slot reset has just happened
4620  * @pdev: PCI device information struct
4621  *
4622  * Called to determine if the driver can recover from the PCI slot reset by
4623  * using a register read to determine if the device is recoverable.
4624  */
4625 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
4626 {
4627 	struct ice_pf *pf = pci_get_drvdata(pdev);
4628 	pci_ers_result_t result;
4629 	int err;
4630 	u32 reg;
4631 
4632 	err = pci_enable_device_mem(pdev);
4633 	if (err) {
4634 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
4635 			err);
4636 		result = PCI_ERS_RESULT_DISCONNECT;
4637 	} else {
4638 		pci_set_master(pdev);
4639 		pci_restore_state(pdev);
4640 		pci_save_state(pdev);
4641 		pci_wake_from_d3(pdev, false);
4642 
4643 		/* Check for life */
4644 		reg = rd32(&pf->hw, GLGEN_RTRIG);
4645 		if (!reg)
4646 			result = PCI_ERS_RESULT_RECOVERED;
4647 		else
4648 			result = PCI_ERS_RESULT_DISCONNECT;
4649 	}
4650 
4651 	err = pci_aer_clear_nonfatal_status(pdev);
4652 	if (err)
4653 		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
4654 			err);
4655 		/* non-fatal, continue */
4656 
4657 	return result;
4658 }
4659 
4660 /**
4661  * ice_pci_err_resume - restart operations after PCI error recovery
4662  * @pdev: PCI device information struct
4663  *
4664  * Called to allow the driver to bring things back up after PCI error and/or
4665  * reset recovery have finished
4666  */
4667 static void ice_pci_err_resume(struct pci_dev *pdev)
4668 {
4669 	struct ice_pf *pf = pci_get_drvdata(pdev);
4670 
4671 	if (!pf) {
4672 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
4673 			__func__);
4674 		return;
4675 	}
4676 
4677 	if (test_bit(__ICE_SUSPENDED, pf->state)) {
4678 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
4679 			__func__);
4680 		return;
4681 	}
4682 
4683 	ice_restore_all_vfs_msi_state(pdev);
4684 
4685 	ice_do_reset(pf, ICE_RESET_PFR);
4686 	ice_service_task_restart(pf);
4687 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4688 }
4689 
4690 /**
4691  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
4692  * @pdev: PCI device information struct
4693  */
4694 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
4695 {
4696 	struct ice_pf *pf = pci_get_drvdata(pdev);
4697 
4698 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4699 		ice_service_task_stop(pf);
4700 
4701 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4702 			set_bit(__ICE_PFR_REQ, pf->state);
4703 			ice_prepare_for_reset(pf);
4704 		}
4705 	}
4706 }
4707 
4708 /**
4709  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
4710  * @pdev: PCI device information struct
4711  */
4712 static void ice_pci_err_reset_done(struct pci_dev *pdev)
4713 {
4714 	ice_pci_err_resume(pdev);
4715 }
4716 
4717 /* ice_pci_tbl - PCI Device ID Table
4718  *
4719  * Wildcard entries (PCI_ANY_ID) should come last
4720  * Last entry must be all 0s
4721  *
4722  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
4723  *   Class, Class Mask, private data (not used) }
4724  */
4725 static const struct pci_device_id ice_pci_tbl[] = {
4726 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
4727 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
4728 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
4729 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
4730 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
4731 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
4732 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
4733 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
4734 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
4735 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
4736 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
4737 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
4738 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
4739 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
4740 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
4741 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
4742 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
4743 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
4744 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
4745 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
4746 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
4747 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
4748 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
4749 	/* required last entry */
4750 	{ 0, }
4751 };
4752 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
4753 
4754 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
4755 
4756 static const struct pci_error_handlers ice_pci_err_handler = {
4757 	.error_detected = ice_pci_err_detected,
4758 	.slot_reset = ice_pci_err_slot_reset,
4759 	.reset_prepare = ice_pci_err_reset_prepare,
4760 	.reset_done = ice_pci_err_reset_done,
4761 	.resume = ice_pci_err_resume
4762 };
4763 
4764 static struct pci_driver ice_driver = {
4765 	.name = KBUILD_MODNAME,
4766 	.id_table = ice_pci_tbl,
4767 	.probe = ice_probe,
4768 	.remove = ice_remove,
4769 #ifdef CONFIG_PM
4770 	.driver.pm = &ice_pm_ops,
4771 #endif /* CONFIG_PM */
4772 	.shutdown = ice_shutdown,
4773 	.sriov_configure = ice_sriov_configure,
4774 	.err_handler = &ice_pci_err_handler
4775 };
4776 
4777 /**
4778  * ice_module_init - Driver registration routine
4779  *
4780  * ice_module_init is the first routine called when the driver is
4781  * loaded. All it does is register with the PCI subsystem.
4782  */
4783 static int __init ice_module_init(void)
4784 {
4785 	int status;
4786 
4787 	pr_info("%s\n", ice_driver_string);
4788 	pr_info("%s\n", ice_copyright);
4789 
4790 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
4791 	if (!ice_wq) {
4792 		pr_err("Failed to create workqueue\n");
4793 		return -ENOMEM;
4794 	}
4795 
4796 	status = pci_register_driver(&ice_driver);
4797 	if (status) {
4798 		pr_err("failed to register PCI driver, err %d\n", status);
4799 		destroy_workqueue(ice_wq);
4800 	}
4801 
4802 	return status;
4803 }
4804 module_init(ice_module_init);
4805 
4806 /**
4807  * ice_module_exit - Driver exit cleanup routine
4808  *
4809  * ice_module_exit is called just before the driver is removed
4810  * from memory.
4811  */
4812 static void __exit ice_module_exit(void)
4813 {
4814 	pci_unregister_driver(&ice_driver);
4815 	destroy_workqueue(ice_wq);
4816 	pr_info("module unloaded\n");
4817 }
4818 module_exit(ice_module_exit);
4819 
4820 /**
4821  * ice_set_mac_address - NDO callback to set MAC address
4822  * @netdev: network interface device structure
4823  * @pi: pointer to an address structure
4824  *
4825  * Returns 0 on success, negative on failure
4826  */
4827 static int ice_set_mac_address(struct net_device *netdev, void *pi)
4828 {
4829 	struct ice_netdev_priv *np = netdev_priv(netdev);
4830 	struct ice_vsi *vsi = np->vsi;
4831 	struct ice_pf *pf = vsi->back;
4832 	struct ice_hw *hw = &pf->hw;
4833 	struct sockaddr *addr = pi;
4834 	enum ice_status status;
4835 	u8 flags = 0;
4836 	int err = 0;
4837 	u8 *mac;
4838 
4839 	mac = (u8 *)addr->sa_data;
4840 
4841 	if (!is_valid_ether_addr(mac))
4842 		return -EADDRNOTAVAIL;
4843 
4844 	if (ether_addr_equal(netdev->dev_addr, mac)) {
4845 		netdev_warn(netdev, "already using mac %pM\n", mac);
4846 		return 0;
4847 	}
4848 
4849 	if (test_bit(__ICE_DOWN, pf->state) ||
4850 	    ice_is_reset_in_progress(pf->state)) {
4851 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
4852 			   mac);
4853 		return -EBUSY;
4854 	}
4855 
4856 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
4857 	status = ice_fltr_remove_mac(vsi, netdev->dev_addr, ICE_FWD_TO_VSI);
4858 	if (status && status != ICE_ERR_DOES_NOT_EXIST) {
4859 		err = -EADDRNOTAVAIL;
4860 		goto err_update_filters;
4861 	}
4862 
4863 	/* Add filter for new MAC. If filter exists, just return success */
4864 	status = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
4865 	if (status == ICE_ERR_ALREADY_EXISTS) {
4866 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
4867 		return 0;
4868 	}
4869 
4870 	/* error if the new filter addition failed */
4871 	if (status)
4872 		err = -EADDRNOTAVAIL;
4873 
4874 err_update_filters:
4875 	if (err) {
4876 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
4877 			   mac);
4878 		return err;
4879 	}
4880 
4881 	/* change the netdev's MAC address */
4882 	memcpy(netdev->dev_addr, mac, netdev->addr_len);
4883 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
4884 		   netdev->dev_addr);
4885 
4886 	/* write new MAC address to the firmware */
4887 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
4888 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
4889 	if (status) {
4890 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %s\n",
4891 			   mac, ice_stat_str(status));
4892 	}
4893 	return 0;
4894 }
4895 
4896 /**
4897  * ice_set_rx_mode - NDO callback to set the netdev filters
4898  * @netdev: network interface device structure
4899  */
4900 static void ice_set_rx_mode(struct net_device *netdev)
4901 {
4902 	struct ice_netdev_priv *np = netdev_priv(netdev);
4903 	struct ice_vsi *vsi = np->vsi;
4904 
4905 	if (!vsi)
4906 		return;
4907 
4908 	/* Set the flags to synchronize filters
4909 	 * ndo_set_rx_mode may be triggered even without a change in netdev
4910 	 * flags
4911 	 */
4912 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
4913 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
4914 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
4915 
4916 	/* schedule our worker thread which will take care of
4917 	 * applying the new filter changes
4918 	 */
4919 	ice_service_task_schedule(vsi->back);
4920 }
4921 
4922 /**
4923  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
4924  * @netdev: network interface device structure
4925  * @queue_index: Queue ID
4926  * @maxrate: maximum bandwidth in Mbps
4927  */
4928 static int
4929 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
4930 {
4931 	struct ice_netdev_priv *np = netdev_priv(netdev);
4932 	struct ice_vsi *vsi = np->vsi;
4933 	enum ice_status status;
4934 	u16 q_handle;
4935 	u8 tc;
4936 
4937 	/* Validate maxrate requested is within permitted range */
4938 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
4939 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
4940 			   maxrate, queue_index);
4941 		return -EINVAL;
4942 	}
4943 
4944 	q_handle = vsi->tx_rings[queue_index]->q_handle;
4945 	tc = ice_dcb_get_tc(vsi, queue_index);
4946 
4947 	/* Set BW back to default, when user set maxrate to 0 */
4948 	if (!maxrate)
4949 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
4950 					       q_handle, ICE_MAX_BW);
4951 	else
4952 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
4953 					  q_handle, ICE_MAX_BW, maxrate * 1000);
4954 	if (status) {
4955 		netdev_err(netdev, "Unable to set Tx max rate, error %s\n",
4956 			   ice_stat_str(status));
4957 		return -EIO;
4958 	}
4959 
4960 	return 0;
4961 }
4962 
4963 /**
4964  * ice_fdb_add - add an entry to the hardware database
4965  * @ndm: the input from the stack
4966  * @tb: pointer to array of nladdr (unused)
4967  * @dev: the net device pointer
4968  * @addr: the MAC address entry being added
4969  * @vid: VLAN ID
4970  * @flags: instructions from stack about fdb operation
4971  * @extack: netlink extended ack
4972  */
4973 static int
4974 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
4975 	    struct net_device *dev, const unsigned char *addr, u16 vid,
4976 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
4977 {
4978 	int err;
4979 
4980 	if (vid) {
4981 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
4982 		return -EINVAL;
4983 	}
4984 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4985 		netdev_err(dev, "FDB only supports static addresses\n");
4986 		return -EINVAL;
4987 	}
4988 
4989 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4990 		err = dev_uc_add_excl(dev, addr);
4991 	else if (is_multicast_ether_addr(addr))
4992 		err = dev_mc_add_excl(dev, addr);
4993 	else
4994 		err = -EINVAL;
4995 
4996 	/* Only return duplicate errors if NLM_F_EXCL is set */
4997 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
4998 		err = 0;
4999 
5000 	return err;
5001 }
5002 
5003 /**
5004  * ice_fdb_del - delete an entry from the hardware database
5005  * @ndm: the input from the stack
5006  * @tb: pointer to array of nladdr (unused)
5007  * @dev: the net device pointer
5008  * @addr: the MAC address entry being added
5009  * @vid: VLAN ID
5010  */
5011 static int
5012 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5013 	    struct net_device *dev, const unsigned char *addr,
5014 	    __always_unused u16 vid)
5015 {
5016 	int err;
5017 
5018 	if (ndm->ndm_state & NUD_PERMANENT) {
5019 		netdev_err(dev, "FDB only supports static addresses\n");
5020 		return -EINVAL;
5021 	}
5022 
5023 	if (is_unicast_ether_addr(addr))
5024 		err = dev_uc_del(dev, addr);
5025 	else if (is_multicast_ether_addr(addr))
5026 		err = dev_mc_del(dev, addr);
5027 	else
5028 		err = -EINVAL;
5029 
5030 	return err;
5031 }
5032 
5033 /**
5034  * ice_set_features - set the netdev feature flags
5035  * @netdev: ptr to the netdev being adjusted
5036  * @features: the feature set that the stack is suggesting
5037  */
5038 static int
5039 ice_set_features(struct net_device *netdev, netdev_features_t features)
5040 {
5041 	struct ice_netdev_priv *np = netdev_priv(netdev);
5042 	struct ice_vsi *vsi = np->vsi;
5043 	struct ice_pf *pf = vsi->back;
5044 	int ret = 0;
5045 
5046 	/* Don't set any netdev advanced features with device in Safe Mode */
5047 	if (ice_is_safe_mode(vsi->back)) {
5048 		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5049 		return ret;
5050 	}
5051 
5052 	/* Do not change setting during reset */
5053 	if (ice_is_reset_in_progress(pf->state)) {
5054 		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5055 		return -EBUSY;
5056 	}
5057 
5058 	/* Multiple features can be changed in one call so keep features in
5059 	 * separate if/else statements to guarantee each feature is checked
5060 	 */
5061 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5062 		ret = ice_vsi_manage_rss_lut(vsi, true);
5063 	else if (!(features & NETIF_F_RXHASH) &&
5064 		 netdev->features & NETIF_F_RXHASH)
5065 		ret = ice_vsi_manage_rss_lut(vsi, false);
5066 
5067 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5068 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5069 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5070 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5071 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5072 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
5073 
5074 	if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5075 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5076 		ret = ice_vsi_manage_vlan_insertion(vsi);
5077 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5078 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5079 		ret = ice_vsi_manage_vlan_insertion(vsi);
5080 
5081 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5082 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5083 		ret = ice_cfg_vlan_pruning(vsi, true, false);
5084 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5085 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5086 		ret = ice_cfg_vlan_pruning(vsi, false, false);
5087 
5088 	if ((features & NETIF_F_NTUPLE) &&
5089 	    !(netdev->features & NETIF_F_NTUPLE)) {
5090 		ice_vsi_manage_fdir(vsi, true);
5091 		ice_init_arfs(vsi);
5092 	} else if (!(features & NETIF_F_NTUPLE) &&
5093 		 (netdev->features & NETIF_F_NTUPLE)) {
5094 		ice_vsi_manage_fdir(vsi, false);
5095 		ice_clear_arfs(vsi);
5096 	}
5097 
5098 	return ret;
5099 }
5100 
5101 /**
5102  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5103  * @vsi: VSI to setup VLAN properties for
5104  */
5105 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5106 {
5107 	int ret = 0;
5108 
5109 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5110 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5111 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5112 		ret = ice_vsi_manage_vlan_insertion(vsi);
5113 
5114 	return ret;
5115 }
5116 
5117 /**
5118  * ice_vsi_cfg - Setup the VSI
5119  * @vsi: the VSI being configured
5120  *
5121  * Return 0 on success and negative value on error
5122  */
5123 int ice_vsi_cfg(struct ice_vsi *vsi)
5124 {
5125 	int err;
5126 
5127 	if (vsi->netdev) {
5128 		ice_set_rx_mode(vsi->netdev);
5129 
5130 		err = ice_vsi_vlan_setup(vsi);
5131 
5132 		if (err)
5133 			return err;
5134 	}
5135 	ice_vsi_cfg_dcb_rings(vsi);
5136 
5137 	err = ice_vsi_cfg_lan_txqs(vsi);
5138 	if (!err && ice_is_xdp_ena_vsi(vsi))
5139 		err = ice_vsi_cfg_xdp_txqs(vsi);
5140 	if (!err)
5141 		err = ice_vsi_cfg_rxqs(vsi);
5142 
5143 	return err;
5144 }
5145 
5146 /**
5147  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5148  * @vsi: the VSI being configured
5149  */
5150 static void ice_napi_enable_all(struct ice_vsi *vsi)
5151 {
5152 	int q_idx;
5153 
5154 	if (!vsi->netdev)
5155 		return;
5156 
5157 	ice_for_each_q_vector(vsi, q_idx) {
5158 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5159 
5160 		if (q_vector->rx.ring || q_vector->tx.ring)
5161 			napi_enable(&q_vector->napi);
5162 	}
5163 }
5164 
5165 /**
5166  * ice_up_complete - Finish the last steps of bringing up a connection
5167  * @vsi: The VSI being configured
5168  *
5169  * Return 0 on success and negative value on error
5170  */
5171 static int ice_up_complete(struct ice_vsi *vsi)
5172 {
5173 	struct ice_pf *pf = vsi->back;
5174 	int err;
5175 
5176 	ice_vsi_cfg_msix(vsi);
5177 
5178 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
5179 	 * Tx queue group list was configured and the context bits were
5180 	 * programmed using ice_vsi_cfg_txqs
5181 	 */
5182 	err = ice_vsi_start_all_rx_rings(vsi);
5183 	if (err)
5184 		return err;
5185 
5186 	clear_bit(__ICE_DOWN, vsi->state);
5187 	ice_napi_enable_all(vsi);
5188 	ice_vsi_ena_irq(vsi);
5189 
5190 	if (vsi->port_info &&
5191 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5192 	    vsi->netdev) {
5193 		ice_print_link_msg(vsi, true);
5194 		netif_tx_start_all_queues(vsi->netdev);
5195 		netif_carrier_on(vsi->netdev);
5196 	}
5197 
5198 	ice_service_task_schedule(pf);
5199 
5200 	return 0;
5201 }
5202 
5203 /**
5204  * ice_up - Bring the connection back up after being down
5205  * @vsi: VSI being configured
5206  */
5207 int ice_up(struct ice_vsi *vsi)
5208 {
5209 	int err;
5210 
5211 	err = ice_vsi_cfg(vsi);
5212 	if (!err)
5213 		err = ice_up_complete(vsi);
5214 
5215 	return err;
5216 }
5217 
5218 /**
5219  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5220  * @ring: Tx or Rx ring to read stats from
5221  * @pkts: packets stats counter
5222  * @bytes: bytes stats counter
5223  *
5224  * This function fetches stats from the ring considering the atomic operations
5225  * that needs to be performed to read u64 values in 32 bit machine.
5226  */
5227 static void
5228 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
5229 {
5230 	unsigned int start;
5231 	*pkts = 0;
5232 	*bytes = 0;
5233 
5234 	if (!ring)
5235 		return;
5236 	do {
5237 		start = u64_stats_fetch_begin_irq(&ring->syncp);
5238 		*pkts = ring->stats.pkts;
5239 		*bytes = ring->stats.bytes;
5240 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
5241 }
5242 
5243 /**
5244  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5245  * @vsi: the VSI to be updated
5246  * @rings: rings to work on
5247  * @count: number of rings
5248  */
5249 static void
5250 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi, struct ice_ring **rings,
5251 			     u16 count)
5252 {
5253 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5254 	u16 i;
5255 
5256 	for (i = 0; i < count; i++) {
5257 		struct ice_ring *ring;
5258 		u64 pkts, bytes;
5259 
5260 		ring = READ_ONCE(rings[i]);
5261 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5262 		vsi_stats->tx_packets += pkts;
5263 		vsi_stats->tx_bytes += bytes;
5264 		vsi->tx_restart += ring->tx_stats.restart_q;
5265 		vsi->tx_busy += ring->tx_stats.tx_busy;
5266 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
5267 	}
5268 }
5269 
5270 /**
5271  * ice_update_vsi_ring_stats - Update VSI stats counters
5272  * @vsi: the VSI to be updated
5273  */
5274 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5275 {
5276 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5277 	struct ice_ring *ring;
5278 	u64 pkts, bytes;
5279 	int i;
5280 
5281 	/* reset netdev stats */
5282 	vsi_stats->tx_packets = 0;
5283 	vsi_stats->tx_bytes = 0;
5284 	vsi_stats->rx_packets = 0;
5285 	vsi_stats->rx_bytes = 0;
5286 
5287 	/* reset non-netdev (extended) stats */
5288 	vsi->tx_restart = 0;
5289 	vsi->tx_busy = 0;
5290 	vsi->tx_linearize = 0;
5291 	vsi->rx_buf_failed = 0;
5292 	vsi->rx_page_failed = 0;
5293 	vsi->rx_gro_dropped = 0;
5294 
5295 	rcu_read_lock();
5296 
5297 	/* update Tx rings counters */
5298 	ice_update_vsi_tx_ring_stats(vsi, vsi->tx_rings, vsi->num_txq);
5299 
5300 	/* update Rx rings counters */
5301 	ice_for_each_rxq(vsi, i) {
5302 		ring = READ_ONCE(vsi->rx_rings[i]);
5303 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5304 		vsi_stats->rx_packets += pkts;
5305 		vsi_stats->rx_bytes += bytes;
5306 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5307 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5308 		vsi->rx_gro_dropped += ring->rx_stats.gro_dropped;
5309 	}
5310 
5311 	/* update XDP Tx rings counters */
5312 	if (ice_is_xdp_ena_vsi(vsi))
5313 		ice_update_vsi_tx_ring_stats(vsi, vsi->xdp_rings,
5314 					     vsi->num_xdp_txq);
5315 
5316 	rcu_read_unlock();
5317 }
5318 
5319 /**
5320  * ice_update_vsi_stats - Update VSI stats counters
5321  * @vsi: the VSI to be updated
5322  */
5323 void ice_update_vsi_stats(struct ice_vsi *vsi)
5324 {
5325 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
5326 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
5327 	struct ice_pf *pf = vsi->back;
5328 
5329 	if (test_bit(__ICE_DOWN, vsi->state) ||
5330 	    test_bit(__ICE_CFG_BUSY, pf->state))
5331 		return;
5332 
5333 	/* get stats as recorded by Tx/Rx rings */
5334 	ice_update_vsi_ring_stats(vsi);
5335 
5336 	/* get VSI stats as recorded by the hardware */
5337 	ice_update_eth_stats(vsi);
5338 
5339 	cur_ns->tx_errors = cur_es->tx_errors;
5340 	cur_ns->rx_dropped = cur_es->rx_discards + vsi->rx_gro_dropped;
5341 	cur_ns->tx_dropped = cur_es->tx_discards;
5342 	cur_ns->multicast = cur_es->rx_multicast;
5343 
5344 	/* update some more netdev stats if this is main VSI */
5345 	if (vsi->type == ICE_VSI_PF) {
5346 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
5347 		cur_ns->rx_errors = pf->stats.crc_errors +
5348 				    pf->stats.illegal_bytes +
5349 				    pf->stats.rx_len_errors +
5350 				    pf->stats.rx_undersize +
5351 				    pf->hw_csum_rx_error +
5352 				    pf->stats.rx_jabber +
5353 				    pf->stats.rx_fragments +
5354 				    pf->stats.rx_oversize;
5355 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
5356 		/* record drops from the port level */
5357 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
5358 	}
5359 }
5360 
5361 /**
5362  * ice_update_pf_stats - Update PF port stats counters
5363  * @pf: PF whose stats needs to be updated
5364  */
5365 void ice_update_pf_stats(struct ice_pf *pf)
5366 {
5367 	struct ice_hw_port_stats *prev_ps, *cur_ps;
5368 	struct ice_hw *hw = &pf->hw;
5369 	u16 fd_ctr_base;
5370 	u8 port;
5371 
5372 	port = hw->port_info->lport;
5373 	prev_ps = &pf->stats_prev;
5374 	cur_ps = &pf->stats;
5375 
5376 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
5377 			  &prev_ps->eth.rx_bytes,
5378 			  &cur_ps->eth.rx_bytes);
5379 
5380 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
5381 			  &prev_ps->eth.rx_unicast,
5382 			  &cur_ps->eth.rx_unicast);
5383 
5384 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
5385 			  &prev_ps->eth.rx_multicast,
5386 			  &cur_ps->eth.rx_multicast);
5387 
5388 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
5389 			  &prev_ps->eth.rx_broadcast,
5390 			  &cur_ps->eth.rx_broadcast);
5391 
5392 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
5393 			  &prev_ps->eth.rx_discards,
5394 			  &cur_ps->eth.rx_discards);
5395 
5396 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
5397 			  &prev_ps->eth.tx_bytes,
5398 			  &cur_ps->eth.tx_bytes);
5399 
5400 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
5401 			  &prev_ps->eth.tx_unicast,
5402 			  &cur_ps->eth.tx_unicast);
5403 
5404 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
5405 			  &prev_ps->eth.tx_multicast,
5406 			  &cur_ps->eth.tx_multicast);
5407 
5408 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
5409 			  &prev_ps->eth.tx_broadcast,
5410 			  &cur_ps->eth.tx_broadcast);
5411 
5412 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
5413 			  &prev_ps->tx_dropped_link_down,
5414 			  &cur_ps->tx_dropped_link_down);
5415 
5416 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
5417 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
5418 
5419 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
5420 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
5421 
5422 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
5423 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
5424 
5425 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
5426 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
5427 
5428 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
5429 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
5430 
5431 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
5432 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5433 
5434 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
5435 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5436 
5437 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
5438 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
5439 
5440 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
5441 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
5442 
5443 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
5444 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
5445 
5446 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
5447 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
5448 
5449 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
5450 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5451 
5452 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
5453 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5454 
5455 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
5456 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5457 
5458 	fd_ctr_base = hw->fd_ctr_base;
5459 
5460 	ice_stat_update40(hw,
5461 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
5462 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
5463 			  &cur_ps->fd_sb_match);
5464 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
5465 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5466 
5467 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
5468 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5469 
5470 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
5471 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5472 
5473 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
5474 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5475 
5476 	ice_update_dcb_stats(pf);
5477 
5478 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
5479 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
5480 
5481 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
5482 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5483 
5484 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
5485 			  &prev_ps->mac_local_faults,
5486 			  &cur_ps->mac_local_faults);
5487 
5488 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
5489 			  &prev_ps->mac_remote_faults,
5490 			  &cur_ps->mac_remote_faults);
5491 
5492 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
5493 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5494 
5495 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
5496 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5497 
5498 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
5499 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5500 
5501 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
5502 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5503 
5504 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
5505 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5506 
5507 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
5508 
5509 	pf->stat_prev_loaded = true;
5510 }
5511 
5512 /**
5513  * ice_get_stats64 - get statistics for network device structure
5514  * @netdev: network interface device structure
5515  * @stats: main device statistics structure
5516  */
5517 static
5518 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5519 {
5520 	struct ice_netdev_priv *np = netdev_priv(netdev);
5521 	struct rtnl_link_stats64 *vsi_stats;
5522 	struct ice_vsi *vsi = np->vsi;
5523 
5524 	vsi_stats = &vsi->net_stats;
5525 
5526 	if (!vsi->num_txq || !vsi->num_rxq)
5527 		return;
5528 
5529 	/* netdev packet/byte stats come from ring counter. These are obtained
5530 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
5531 	 * But, only call the update routine and read the registers if VSI is
5532 	 * not down.
5533 	 */
5534 	if (!test_bit(__ICE_DOWN, vsi->state))
5535 		ice_update_vsi_ring_stats(vsi);
5536 	stats->tx_packets = vsi_stats->tx_packets;
5537 	stats->tx_bytes = vsi_stats->tx_bytes;
5538 	stats->rx_packets = vsi_stats->rx_packets;
5539 	stats->rx_bytes = vsi_stats->rx_bytes;
5540 
5541 	/* The rest of the stats can be read from the hardware but instead we
5542 	 * just return values that the watchdog task has already obtained from
5543 	 * the hardware.
5544 	 */
5545 	stats->multicast = vsi_stats->multicast;
5546 	stats->tx_errors = vsi_stats->tx_errors;
5547 	stats->tx_dropped = vsi_stats->tx_dropped;
5548 	stats->rx_errors = vsi_stats->rx_errors;
5549 	stats->rx_dropped = vsi_stats->rx_dropped;
5550 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5551 	stats->rx_length_errors = vsi_stats->rx_length_errors;
5552 }
5553 
5554 /**
5555  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5556  * @vsi: VSI having NAPI disabled
5557  */
5558 static void ice_napi_disable_all(struct ice_vsi *vsi)
5559 {
5560 	int q_idx;
5561 
5562 	if (!vsi->netdev)
5563 		return;
5564 
5565 	ice_for_each_q_vector(vsi, q_idx) {
5566 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5567 
5568 		if (q_vector->rx.ring || q_vector->tx.ring)
5569 			napi_disable(&q_vector->napi);
5570 	}
5571 }
5572 
5573 /**
5574  * ice_down - Shutdown the connection
5575  * @vsi: The VSI being stopped
5576  */
5577 int ice_down(struct ice_vsi *vsi)
5578 {
5579 	int i, tx_err, rx_err, link_err = 0;
5580 
5581 	/* Caller of this function is expected to set the
5582 	 * vsi->state __ICE_DOWN bit
5583 	 */
5584 	if (vsi->netdev) {
5585 		netif_carrier_off(vsi->netdev);
5586 		netif_tx_disable(vsi->netdev);
5587 	}
5588 
5589 	ice_vsi_dis_irq(vsi);
5590 
5591 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
5592 	if (tx_err)
5593 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
5594 			   vsi->vsi_num, tx_err);
5595 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
5596 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
5597 		if (tx_err)
5598 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
5599 				   vsi->vsi_num, tx_err);
5600 	}
5601 
5602 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
5603 	if (rx_err)
5604 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
5605 			   vsi->vsi_num, rx_err);
5606 
5607 	ice_napi_disable_all(vsi);
5608 
5609 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
5610 		link_err = ice_force_phys_link_state(vsi, false);
5611 		if (link_err)
5612 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
5613 				   vsi->vsi_num, link_err);
5614 	}
5615 
5616 	ice_for_each_txq(vsi, i)
5617 		ice_clean_tx_ring(vsi->tx_rings[i]);
5618 
5619 	ice_for_each_rxq(vsi, i)
5620 		ice_clean_rx_ring(vsi->rx_rings[i]);
5621 
5622 	if (tx_err || rx_err || link_err) {
5623 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5624 			   vsi->vsi_num, vsi->vsw->sw_id);
5625 		return -EIO;
5626 	}
5627 
5628 	return 0;
5629 }
5630 
5631 /**
5632  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5633  * @vsi: VSI having resources allocated
5634  *
5635  * Return 0 on success, negative on failure
5636  */
5637 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5638 {
5639 	int i, err = 0;
5640 
5641 	if (!vsi->num_txq) {
5642 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
5643 			vsi->vsi_num);
5644 		return -EINVAL;
5645 	}
5646 
5647 	ice_for_each_txq(vsi, i) {
5648 		struct ice_ring *ring = vsi->tx_rings[i];
5649 
5650 		if (!ring)
5651 			return -EINVAL;
5652 
5653 		ring->netdev = vsi->netdev;
5654 		err = ice_setup_tx_ring(ring);
5655 		if (err)
5656 			break;
5657 	}
5658 
5659 	return err;
5660 }
5661 
5662 /**
5663  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5664  * @vsi: VSI having resources allocated
5665  *
5666  * Return 0 on success, negative on failure
5667  */
5668 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5669 {
5670 	int i, err = 0;
5671 
5672 	if (!vsi->num_rxq) {
5673 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
5674 			vsi->vsi_num);
5675 		return -EINVAL;
5676 	}
5677 
5678 	ice_for_each_rxq(vsi, i) {
5679 		struct ice_ring *ring = vsi->rx_rings[i];
5680 
5681 		if (!ring)
5682 			return -EINVAL;
5683 
5684 		ring->netdev = vsi->netdev;
5685 		err = ice_setup_rx_ring(ring);
5686 		if (err)
5687 			break;
5688 	}
5689 
5690 	return err;
5691 }
5692 
5693 /**
5694  * ice_vsi_open_ctrl - open control VSI for use
5695  * @vsi: the VSI to open
5696  *
5697  * Initialization of the Control VSI
5698  *
5699  * Returns 0 on success, negative value on error
5700  */
5701 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
5702 {
5703 	char int_name[ICE_INT_NAME_STR_LEN];
5704 	struct ice_pf *pf = vsi->back;
5705 	struct device *dev;
5706 	int err;
5707 
5708 	dev = ice_pf_to_dev(pf);
5709 	/* allocate descriptors */
5710 	err = ice_vsi_setup_tx_rings(vsi);
5711 	if (err)
5712 		goto err_setup_tx;
5713 
5714 	err = ice_vsi_setup_rx_rings(vsi);
5715 	if (err)
5716 		goto err_setup_rx;
5717 
5718 	err = ice_vsi_cfg(vsi);
5719 	if (err)
5720 		goto err_setup_rx;
5721 
5722 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
5723 		 dev_driver_string(dev), dev_name(dev));
5724 	err = ice_vsi_req_irq_msix(vsi, int_name);
5725 	if (err)
5726 		goto err_setup_rx;
5727 
5728 	ice_vsi_cfg_msix(vsi);
5729 
5730 	err = ice_vsi_start_all_rx_rings(vsi);
5731 	if (err)
5732 		goto err_up_complete;
5733 
5734 	clear_bit(__ICE_DOWN, vsi->state);
5735 	ice_vsi_ena_irq(vsi);
5736 
5737 	return 0;
5738 
5739 err_up_complete:
5740 	ice_down(vsi);
5741 err_setup_rx:
5742 	ice_vsi_free_rx_rings(vsi);
5743 err_setup_tx:
5744 	ice_vsi_free_tx_rings(vsi);
5745 
5746 	return err;
5747 }
5748 
5749 /**
5750  * ice_vsi_open - Called when a network interface is made active
5751  * @vsi: the VSI to open
5752  *
5753  * Initialization of the VSI
5754  *
5755  * Returns 0 on success, negative value on error
5756  */
5757 static int ice_vsi_open(struct ice_vsi *vsi)
5758 {
5759 	char int_name[ICE_INT_NAME_STR_LEN];
5760 	struct ice_pf *pf = vsi->back;
5761 	int err;
5762 
5763 	/* allocate descriptors */
5764 	err = ice_vsi_setup_tx_rings(vsi);
5765 	if (err)
5766 		goto err_setup_tx;
5767 
5768 	err = ice_vsi_setup_rx_rings(vsi);
5769 	if (err)
5770 		goto err_setup_rx;
5771 
5772 	err = ice_vsi_cfg(vsi);
5773 	if (err)
5774 		goto err_setup_rx;
5775 
5776 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5777 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
5778 	err = ice_vsi_req_irq_msix(vsi, int_name);
5779 	if (err)
5780 		goto err_setup_rx;
5781 
5782 	/* Notify the stack of the actual queue counts. */
5783 	err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5784 	if (err)
5785 		goto err_set_qs;
5786 
5787 	err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5788 	if (err)
5789 		goto err_set_qs;
5790 
5791 	err = ice_up_complete(vsi);
5792 	if (err)
5793 		goto err_up_complete;
5794 
5795 	return 0;
5796 
5797 err_up_complete:
5798 	ice_down(vsi);
5799 err_set_qs:
5800 	ice_vsi_free_irq(vsi);
5801 err_setup_rx:
5802 	ice_vsi_free_rx_rings(vsi);
5803 err_setup_tx:
5804 	ice_vsi_free_tx_rings(vsi);
5805 
5806 	return err;
5807 }
5808 
5809 /**
5810  * ice_vsi_release_all - Delete all VSIs
5811  * @pf: PF from which all VSIs are being removed
5812  */
5813 static void ice_vsi_release_all(struct ice_pf *pf)
5814 {
5815 	int err, i;
5816 
5817 	if (!pf->vsi)
5818 		return;
5819 
5820 	ice_for_each_vsi(pf, i) {
5821 		if (!pf->vsi[i])
5822 			continue;
5823 
5824 		err = ice_vsi_release(pf->vsi[i]);
5825 		if (err)
5826 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5827 				i, err, pf->vsi[i]->vsi_num);
5828 	}
5829 }
5830 
5831 /**
5832  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
5833  * @pf: pointer to the PF instance
5834  * @type: VSI type to rebuild
5835  *
5836  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
5837  */
5838 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
5839 {
5840 	struct device *dev = ice_pf_to_dev(pf);
5841 	enum ice_status status;
5842 	int i, err;
5843 
5844 	ice_for_each_vsi(pf, i) {
5845 		struct ice_vsi *vsi = pf->vsi[i];
5846 
5847 		if (!vsi || vsi->type != type)
5848 			continue;
5849 
5850 		/* rebuild the VSI */
5851 		err = ice_vsi_rebuild(vsi, true);
5852 		if (err) {
5853 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
5854 				err, vsi->idx, ice_vsi_type_str(type));
5855 			return err;
5856 		}
5857 
5858 		/* replay filters for the VSI */
5859 		status = ice_replay_vsi(&pf->hw, vsi->idx);
5860 		if (status) {
5861 			dev_err(dev, "replay VSI failed, status %s, VSI index %d, type %s\n",
5862 				ice_stat_str(status), vsi->idx,
5863 				ice_vsi_type_str(type));
5864 			return -EIO;
5865 		}
5866 
5867 		/* Re-map HW VSI number, using VSI handle that has been
5868 		 * previously validated in ice_replay_vsi() call above
5869 		 */
5870 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
5871 
5872 		/* enable the VSI */
5873 		err = ice_ena_vsi(vsi, false);
5874 		if (err) {
5875 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
5876 				err, vsi->idx, ice_vsi_type_str(type));
5877 			return err;
5878 		}
5879 
5880 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
5881 			 ice_vsi_type_str(type));
5882 	}
5883 
5884 	return 0;
5885 }
5886 
5887 /**
5888  * ice_update_pf_netdev_link - Update PF netdev link status
5889  * @pf: pointer to the PF instance
5890  */
5891 static void ice_update_pf_netdev_link(struct ice_pf *pf)
5892 {
5893 	bool link_up;
5894 	int i;
5895 
5896 	ice_for_each_vsi(pf, i) {
5897 		struct ice_vsi *vsi = pf->vsi[i];
5898 
5899 		if (!vsi || vsi->type != ICE_VSI_PF)
5900 			return;
5901 
5902 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
5903 		if (link_up) {
5904 			netif_carrier_on(pf->vsi[i]->netdev);
5905 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
5906 		} else {
5907 			netif_carrier_off(pf->vsi[i]->netdev);
5908 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
5909 		}
5910 	}
5911 }
5912 
5913 /**
5914  * ice_rebuild - rebuild after reset
5915  * @pf: PF to rebuild
5916  * @reset_type: type of reset
5917  *
5918  * Do not rebuild VF VSI in this flow because that is already handled via
5919  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
5920  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
5921  * to reset/rebuild all the VF VSI twice.
5922  */
5923 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
5924 {
5925 	struct device *dev = ice_pf_to_dev(pf);
5926 	struct ice_hw *hw = &pf->hw;
5927 	enum ice_status ret;
5928 	int err;
5929 
5930 	if (test_bit(__ICE_DOWN, pf->state))
5931 		goto clear_recovery;
5932 
5933 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
5934 
5935 	ret = ice_init_all_ctrlq(hw);
5936 	if (ret) {
5937 		dev_err(dev, "control queues init failed %s\n",
5938 			ice_stat_str(ret));
5939 		goto err_init_ctrlq;
5940 	}
5941 
5942 	/* if DDP was previously loaded successfully */
5943 	if (!ice_is_safe_mode(pf)) {
5944 		/* reload the SW DB of filter tables */
5945 		if (reset_type == ICE_RESET_PFR)
5946 			ice_fill_blk_tbls(hw);
5947 		else
5948 			/* Reload DDP Package after CORER/GLOBR reset */
5949 			ice_load_pkg(NULL, pf);
5950 	}
5951 
5952 	ret = ice_clear_pf_cfg(hw);
5953 	if (ret) {
5954 		dev_err(dev, "clear PF configuration failed %s\n",
5955 			ice_stat_str(ret));
5956 		goto err_init_ctrlq;
5957 	}
5958 
5959 	if (pf->first_sw->dflt_vsi_ena)
5960 		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
5961 	/* clear the default VSI configuration if it exists */
5962 	pf->first_sw->dflt_vsi = NULL;
5963 	pf->first_sw->dflt_vsi_ena = false;
5964 
5965 	ice_clear_pxe_mode(hw);
5966 
5967 	ret = ice_get_caps(hw);
5968 	if (ret) {
5969 		dev_err(dev, "ice_get_caps failed %s\n", ice_stat_str(ret));
5970 		goto err_init_ctrlq;
5971 	}
5972 
5973 	ret = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
5974 	if (ret) {
5975 		dev_err(dev, "set_mac_cfg failed %s\n", ice_stat_str(ret));
5976 		goto err_init_ctrlq;
5977 	}
5978 
5979 	err = ice_sched_init_port(hw->port_info);
5980 	if (err)
5981 		goto err_sched_init_port;
5982 
5983 	/* start misc vector */
5984 	err = ice_req_irq_msix_misc(pf);
5985 	if (err) {
5986 		dev_err(dev, "misc vector setup failed: %d\n", err);
5987 		goto err_sched_init_port;
5988 	}
5989 
5990 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
5991 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
5992 		if (!rd32(hw, PFQF_FD_SIZE)) {
5993 			u16 unused, guar, b_effort;
5994 
5995 			guar = hw->func_caps.fd_fltr_guar;
5996 			b_effort = hw->func_caps.fd_fltr_best_effort;
5997 
5998 			/* force guaranteed filter pool for PF */
5999 			ice_alloc_fd_guar_item(hw, &unused, guar);
6000 			/* force shared filter pool for PF */
6001 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6002 		}
6003 	}
6004 
6005 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6006 		ice_dcb_rebuild(pf);
6007 
6008 	/* rebuild PF VSI */
6009 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6010 	if (err) {
6011 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6012 		goto err_vsi_rebuild;
6013 	}
6014 
6015 	/* If Flow Director is active */
6016 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6017 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6018 		if (err) {
6019 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
6020 			goto err_vsi_rebuild;
6021 		}
6022 
6023 		/* replay HW Flow Director recipes */
6024 		if (hw->fdir_prof)
6025 			ice_fdir_replay_flows(hw);
6026 
6027 		/* replay Flow Director filters */
6028 		ice_fdir_replay_fltrs(pf);
6029 
6030 		ice_rebuild_arfs(pf);
6031 	}
6032 
6033 	ice_update_pf_netdev_link(pf);
6034 
6035 	/* tell the firmware we are up */
6036 	ret = ice_send_version(pf);
6037 	if (ret) {
6038 		dev_err(dev, "Rebuild failed due to error sending driver version: %s\n",
6039 			ice_stat_str(ret));
6040 		goto err_vsi_rebuild;
6041 	}
6042 
6043 	ice_replay_post(hw);
6044 
6045 	/* if we get here, reset flow is successful */
6046 	clear_bit(__ICE_RESET_FAILED, pf->state);
6047 	return;
6048 
6049 err_vsi_rebuild:
6050 err_sched_init_port:
6051 	ice_sched_cleanup_all(hw);
6052 err_init_ctrlq:
6053 	ice_shutdown_all_ctrlq(hw);
6054 	set_bit(__ICE_RESET_FAILED, pf->state);
6055 clear_recovery:
6056 	/* set this bit in PF state to control service task scheduling */
6057 	set_bit(__ICE_NEEDS_RESTART, pf->state);
6058 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
6059 }
6060 
6061 /**
6062  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6063  * @vsi: Pointer to VSI structure
6064  */
6065 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6066 {
6067 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6068 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6069 	else
6070 		return ICE_RXBUF_3072;
6071 }
6072 
6073 /**
6074  * ice_change_mtu - NDO callback to change the MTU
6075  * @netdev: network interface device structure
6076  * @new_mtu: new value for maximum frame size
6077  *
6078  * Returns 0 on success, negative on failure
6079  */
6080 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6081 {
6082 	struct ice_netdev_priv *np = netdev_priv(netdev);
6083 	struct ice_vsi *vsi = np->vsi;
6084 	struct ice_pf *pf = vsi->back;
6085 	u8 count = 0;
6086 
6087 	if (new_mtu == (int)netdev->mtu) {
6088 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6089 		return 0;
6090 	}
6091 
6092 	if (ice_is_xdp_ena_vsi(vsi)) {
6093 		int frame_size = ice_max_xdp_frame_size(vsi);
6094 
6095 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6096 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
6097 				   frame_size - ICE_ETH_PKT_HDR_PAD);
6098 			return -EINVAL;
6099 		}
6100 	}
6101 
6102 	if (new_mtu < (int)netdev->min_mtu) {
6103 		netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
6104 			   netdev->min_mtu);
6105 		return -EINVAL;
6106 	} else if (new_mtu > (int)netdev->max_mtu) {
6107 		netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
6108 			   netdev->min_mtu);
6109 		return -EINVAL;
6110 	}
6111 	/* if a reset is in progress, wait for some time for it to complete */
6112 	do {
6113 		if (ice_is_reset_in_progress(pf->state)) {
6114 			count++;
6115 			usleep_range(1000, 2000);
6116 		} else {
6117 			break;
6118 		}
6119 
6120 	} while (count < 100);
6121 
6122 	if (count == 100) {
6123 		netdev_err(netdev, "can't change MTU. Device is busy\n");
6124 		return -EBUSY;
6125 	}
6126 
6127 	netdev->mtu = (unsigned int)new_mtu;
6128 
6129 	/* if VSI is up, bring it down and then back up */
6130 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
6131 		int err;
6132 
6133 		err = ice_down(vsi);
6134 		if (err) {
6135 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6136 			return err;
6137 		}
6138 
6139 		err = ice_up(vsi);
6140 		if (err) {
6141 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6142 			return err;
6143 		}
6144 	}
6145 
6146 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6147 	return 0;
6148 }
6149 
6150 /**
6151  * ice_aq_str - convert AQ err code to a string
6152  * @aq_err: the AQ error code to convert
6153  */
6154 const char *ice_aq_str(enum ice_aq_err aq_err)
6155 {
6156 	switch (aq_err) {
6157 	case ICE_AQ_RC_OK:
6158 		return "OK";
6159 	case ICE_AQ_RC_EPERM:
6160 		return "ICE_AQ_RC_EPERM";
6161 	case ICE_AQ_RC_ENOENT:
6162 		return "ICE_AQ_RC_ENOENT";
6163 	case ICE_AQ_RC_ENOMEM:
6164 		return "ICE_AQ_RC_ENOMEM";
6165 	case ICE_AQ_RC_EBUSY:
6166 		return "ICE_AQ_RC_EBUSY";
6167 	case ICE_AQ_RC_EEXIST:
6168 		return "ICE_AQ_RC_EEXIST";
6169 	case ICE_AQ_RC_EINVAL:
6170 		return "ICE_AQ_RC_EINVAL";
6171 	case ICE_AQ_RC_ENOSPC:
6172 		return "ICE_AQ_RC_ENOSPC";
6173 	case ICE_AQ_RC_ENOSYS:
6174 		return "ICE_AQ_RC_ENOSYS";
6175 	case ICE_AQ_RC_EMODE:
6176 		return "ICE_AQ_RC_EMODE";
6177 	case ICE_AQ_RC_ENOSEC:
6178 		return "ICE_AQ_RC_ENOSEC";
6179 	case ICE_AQ_RC_EBADSIG:
6180 		return "ICE_AQ_RC_EBADSIG";
6181 	case ICE_AQ_RC_ESVN:
6182 		return "ICE_AQ_RC_ESVN";
6183 	case ICE_AQ_RC_EBADMAN:
6184 		return "ICE_AQ_RC_EBADMAN";
6185 	case ICE_AQ_RC_EBADBUF:
6186 		return "ICE_AQ_RC_EBADBUF";
6187 	}
6188 
6189 	return "ICE_AQ_RC_UNKNOWN";
6190 }
6191 
6192 /**
6193  * ice_stat_str - convert status err code to a string
6194  * @stat_err: the status error code to convert
6195  */
6196 const char *ice_stat_str(enum ice_status stat_err)
6197 {
6198 	switch (stat_err) {
6199 	case ICE_SUCCESS:
6200 		return "OK";
6201 	case ICE_ERR_PARAM:
6202 		return "ICE_ERR_PARAM";
6203 	case ICE_ERR_NOT_IMPL:
6204 		return "ICE_ERR_NOT_IMPL";
6205 	case ICE_ERR_NOT_READY:
6206 		return "ICE_ERR_NOT_READY";
6207 	case ICE_ERR_NOT_SUPPORTED:
6208 		return "ICE_ERR_NOT_SUPPORTED";
6209 	case ICE_ERR_BAD_PTR:
6210 		return "ICE_ERR_BAD_PTR";
6211 	case ICE_ERR_INVAL_SIZE:
6212 		return "ICE_ERR_INVAL_SIZE";
6213 	case ICE_ERR_DEVICE_NOT_SUPPORTED:
6214 		return "ICE_ERR_DEVICE_NOT_SUPPORTED";
6215 	case ICE_ERR_RESET_FAILED:
6216 		return "ICE_ERR_RESET_FAILED";
6217 	case ICE_ERR_FW_API_VER:
6218 		return "ICE_ERR_FW_API_VER";
6219 	case ICE_ERR_NO_MEMORY:
6220 		return "ICE_ERR_NO_MEMORY";
6221 	case ICE_ERR_CFG:
6222 		return "ICE_ERR_CFG";
6223 	case ICE_ERR_OUT_OF_RANGE:
6224 		return "ICE_ERR_OUT_OF_RANGE";
6225 	case ICE_ERR_ALREADY_EXISTS:
6226 		return "ICE_ERR_ALREADY_EXISTS";
6227 	case ICE_ERR_NVM_CHECKSUM:
6228 		return "ICE_ERR_NVM_CHECKSUM";
6229 	case ICE_ERR_BUF_TOO_SHORT:
6230 		return "ICE_ERR_BUF_TOO_SHORT";
6231 	case ICE_ERR_NVM_BLANK_MODE:
6232 		return "ICE_ERR_NVM_BLANK_MODE";
6233 	case ICE_ERR_IN_USE:
6234 		return "ICE_ERR_IN_USE";
6235 	case ICE_ERR_MAX_LIMIT:
6236 		return "ICE_ERR_MAX_LIMIT";
6237 	case ICE_ERR_RESET_ONGOING:
6238 		return "ICE_ERR_RESET_ONGOING";
6239 	case ICE_ERR_HW_TABLE:
6240 		return "ICE_ERR_HW_TABLE";
6241 	case ICE_ERR_DOES_NOT_EXIST:
6242 		return "ICE_ERR_DOES_NOT_EXIST";
6243 	case ICE_ERR_FW_DDP_MISMATCH:
6244 		return "ICE_ERR_FW_DDP_MISMATCH";
6245 	case ICE_ERR_AQ_ERROR:
6246 		return "ICE_ERR_AQ_ERROR";
6247 	case ICE_ERR_AQ_TIMEOUT:
6248 		return "ICE_ERR_AQ_TIMEOUT";
6249 	case ICE_ERR_AQ_FULL:
6250 		return "ICE_ERR_AQ_FULL";
6251 	case ICE_ERR_AQ_NO_WORK:
6252 		return "ICE_ERR_AQ_NO_WORK";
6253 	case ICE_ERR_AQ_EMPTY:
6254 		return "ICE_ERR_AQ_EMPTY";
6255 	case ICE_ERR_AQ_FW_CRITICAL:
6256 		return "ICE_ERR_AQ_FW_CRITICAL";
6257 	}
6258 
6259 	return "ICE_ERR_UNKNOWN";
6260 }
6261 
6262 /**
6263  * ice_set_rss - Set RSS keys and lut
6264  * @vsi: Pointer to VSI structure
6265  * @seed: RSS hash seed
6266  * @lut: Lookup table
6267  * @lut_size: Lookup table size
6268  *
6269  * Returns 0 on success, negative on failure
6270  */
6271 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6272 {
6273 	struct ice_pf *pf = vsi->back;
6274 	struct ice_hw *hw = &pf->hw;
6275 	enum ice_status status;
6276 	struct device *dev;
6277 
6278 	dev = ice_pf_to_dev(pf);
6279 	if (seed) {
6280 		struct ice_aqc_get_set_rss_keys *buf =
6281 				  (struct ice_aqc_get_set_rss_keys *)seed;
6282 
6283 		status = ice_aq_set_rss_key(hw, vsi->idx, buf);
6284 
6285 		if (status) {
6286 			dev_err(dev, "Cannot set RSS key, err %s aq_err %s\n",
6287 				ice_stat_str(status),
6288 				ice_aq_str(hw->adminq.sq_last_status));
6289 			return -EIO;
6290 		}
6291 	}
6292 
6293 	if (lut) {
6294 		status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6295 					    lut, lut_size);
6296 		if (status) {
6297 			dev_err(dev, "Cannot set RSS lut, err %s aq_err %s\n",
6298 				ice_stat_str(status),
6299 				ice_aq_str(hw->adminq.sq_last_status));
6300 			return -EIO;
6301 		}
6302 	}
6303 
6304 	return 0;
6305 }
6306 
6307 /**
6308  * ice_get_rss - Get RSS keys and lut
6309  * @vsi: Pointer to VSI structure
6310  * @seed: Buffer to store the keys
6311  * @lut: Buffer to store the lookup table entries
6312  * @lut_size: Size of buffer to store the lookup table entries
6313  *
6314  * Returns 0 on success, negative on failure
6315  */
6316 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6317 {
6318 	struct ice_pf *pf = vsi->back;
6319 	struct ice_hw *hw = &pf->hw;
6320 	enum ice_status status;
6321 	struct device *dev;
6322 
6323 	dev = ice_pf_to_dev(pf);
6324 	if (seed) {
6325 		struct ice_aqc_get_set_rss_keys *buf =
6326 				  (struct ice_aqc_get_set_rss_keys *)seed;
6327 
6328 		status = ice_aq_get_rss_key(hw, vsi->idx, buf);
6329 		if (status) {
6330 			dev_err(dev, "Cannot get RSS key, err %s aq_err %s\n",
6331 				ice_stat_str(status),
6332 				ice_aq_str(hw->adminq.sq_last_status));
6333 			return -EIO;
6334 		}
6335 	}
6336 
6337 	if (lut) {
6338 		status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6339 					    lut, lut_size);
6340 		if (status) {
6341 			dev_err(dev, "Cannot get RSS lut, err %s aq_err %s\n",
6342 				ice_stat_str(status),
6343 				ice_aq_str(hw->adminq.sq_last_status));
6344 			return -EIO;
6345 		}
6346 	}
6347 
6348 	return 0;
6349 }
6350 
6351 /**
6352  * ice_bridge_getlink - Get the hardware bridge mode
6353  * @skb: skb buff
6354  * @pid: process ID
6355  * @seq: RTNL message seq
6356  * @dev: the netdev being configured
6357  * @filter_mask: filter mask passed in
6358  * @nlflags: netlink flags passed in
6359  *
6360  * Return the bridge mode (VEB/VEPA)
6361  */
6362 static int
6363 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
6364 		   struct net_device *dev, u32 filter_mask, int nlflags)
6365 {
6366 	struct ice_netdev_priv *np = netdev_priv(dev);
6367 	struct ice_vsi *vsi = np->vsi;
6368 	struct ice_pf *pf = vsi->back;
6369 	u16 bmode;
6370 
6371 	bmode = pf->first_sw->bridge_mode;
6372 
6373 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
6374 				       filter_mask, NULL);
6375 }
6376 
6377 /**
6378  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
6379  * @vsi: Pointer to VSI structure
6380  * @bmode: Hardware bridge mode (VEB/VEPA)
6381  *
6382  * Returns 0 on success, negative on failure
6383  */
6384 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
6385 {
6386 	struct ice_aqc_vsi_props *vsi_props;
6387 	struct ice_hw *hw = &vsi->back->hw;
6388 	struct ice_vsi_ctx *ctxt;
6389 	enum ice_status status;
6390 	int ret = 0;
6391 
6392 	vsi_props = &vsi->info;
6393 
6394 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
6395 	if (!ctxt)
6396 		return -ENOMEM;
6397 
6398 	ctxt->info = vsi->info;
6399 
6400 	if (bmode == BRIDGE_MODE_VEB)
6401 		/* change from VEPA to VEB mode */
6402 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6403 	else
6404 		/* change from VEB to VEPA mode */
6405 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6406 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
6407 
6408 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
6409 	if (status) {
6410 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %s aq_err %s\n",
6411 			bmode, ice_stat_str(status),
6412 			ice_aq_str(hw->adminq.sq_last_status));
6413 		ret = -EIO;
6414 		goto out;
6415 	}
6416 	/* Update sw flags for book keeping */
6417 	vsi_props->sw_flags = ctxt->info.sw_flags;
6418 
6419 out:
6420 	kfree(ctxt);
6421 	return ret;
6422 }
6423 
6424 /**
6425  * ice_bridge_setlink - Set the hardware bridge mode
6426  * @dev: the netdev being configured
6427  * @nlh: RTNL message
6428  * @flags: bridge setlink flags
6429  * @extack: netlink extended ack
6430  *
6431  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
6432  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
6433  * not already set for all VSIs connected to this switch. And also update the
6434  * unicast switch filter rules for the corresponding switch of the netdev.
6435  */
6436 static int
6437 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
6438 		   u16 __always_unused flags,
6439 		   struct netlink_ext_ack __always_unused *extack)
6440 {
6441 	struct ice_netdev_priv *np = netdev_priv(dev);
6442 	struct ice_pf *pf = np->vsi->back;
6443 	struct nlattr *attr, *br_spec;
6444 	struct ice_hw *hw = &pf->hw;
6445 	enum ice_status status;
6446 	struct ice_sw *pf_sw;
6447 	int rem, v, err = 0;
6448 
6449 	pf_sw = pf->first_sw;
6450 	/* find the attribute in the netlink message */
6451 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
6452 
6453 	nla_for_each_nested(attr, br_spec, rem) {
6454 		__u16 mode;
6455 
6456 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
6457 			continue;
6458 		mode = nla_get_u16(attr);
6459 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
6460 			return -EINVAL;
6461 		/* Continue  if bridge mode is not being flipped */
6462 		if (mode == pf_sw->bridge_mode)
6463 			continue;
6464 		/* Iterates through the PF VSI list and update the loopback
6465 		 * mode of the VSI
6466 		 */
6467 		ice_for_each_vsi(pf, v) {
6468 			if (!pf->vsi[v])
6469 				continue;
6470 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
6471 			if (err)
6472 				return err;
6473 		}
6474 
6475 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
6476 		/* Update the unicast switch filter rules for the corresponding
6477 		 * switch of the netdev
6478 		 */
6479 		status = ice_update_sw_rule_bridge_mode(hw);
6480 		if (status) {
6481 			netdev_err(dev, "switch rule update failed, mode = %d err %s aq_err %s\n",
6482 				   mode, ice_stat_str(status),
6483 				   ice_aq_str(hw->adminq.sq_last_status));
6484 			/* revert hw->evb_veb */
6485 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
6486 			return -EIO;
6487 		}
6488 
6489 		pf_sw->bridge_mode = mode;
6490 	}
6491 
6492 	return 0;
6493 }
6494 
6495 /**
6496  * ice_tx_timeout - Respond to a Tx Hang
6497  * @netdev: network interface device structure
6498  * @txqueue: Tx queue
6499  */
6500 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
6501 {
6502 	struct ice_netdev_priv *np = netdev_priv(netdev);
6503 	struct ice_ring *tx_ring = NULL;
6504 	struct ice_vsi *vsi = np->vsi;
6505 	struct ice_pf *pf = vsi->back;
6506 	u32 i;
6507 
6508 	pf->tx_timeout_count++;
6509 
6510 	/* Check if PFC is enabled for the TC to which the queue belongs
6511 	 * to. If yes then Tx timeout is not caused by a hung queue, no
6512 	 * need to reset and rebuild
6513 	 */
6514 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
6515 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
6516 			 txqueue);
6517 		return;
6518 	}
6519 
6520 	/* now that we have an index, find the tx_ring struct */
6521 	for (i = 0; i < vsi->num_txq; i++)
6522 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
6523 			if (txqueue == vsi->tx_rings[i]->q_index) {
6524 				tx_ring = vsi->tx_rings[i];
6525 				break;
6526 			}
6527 
6528 	/* Reset recovery level if enough time has elapsed after last timeout.
6529 	 * Also ensure no new reset action happens before next timeout period.
6530 	 */
6531 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
6532 		pf->tx_timeout_recovery_level = 1;
6533 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
6534 				       netdev->watchdog_timeo)))
6535 		return;
6536 
6537 	if (tx_ring) {
6538 		struct ice_hw *hw = &pf->hw;
6539 		u32 head, val = 0;
6540 
6541 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
6542 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
6543 		/* Read interrupt register */
6544 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
6545 
6546 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
6547 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
6548 			    head, tx_ring->next_to_use, val);
6549 	}
6550 
6551 	pf->tx_timeout_last_recovery = jiffies;
6552 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
6553 		    pf->tx_timeout_recovery_level, txqueue);
6554 
6555 	switch (pf->tx_timeout_recovery_level) {
6556 	case 1:
6557 		set_bit(__ICE_PFR_REQ, pf->state);
6558 		break;
6559 	case 2:
6560 		set_bit(__ICE_CORER_REQ, pf->state);
6561 		break;
6562 	case 3:
6563 		set_bit(__ICE_GLOBR_REQ, pf->state);
6564 		break;
6565 	default:
6566 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
6567 		set_bit(__ICE_DOWN, pf->state);
6568 		set_bit(__ICE_NEEDS_RESTART, vsi->state);
6569 		set_bit(__ICE_SERVICE_DIS, pf->state);
6570 		break;
6571 	}
6572 
6573 	ice_service_task_schedule(pf);
6574 	pf->tx_timeout_recovery_level++;
6575 }
6576 
6577 /**
6578  * ice_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
6579  * @netdev: This physical port's netdev
6580  * @ti: Tunnel endpoint information
6581  */
6582 static void
6583 ice_udp_tunnel_add(struct net_device *netdev, struct udp_tunnel_info *ti)
6584 {
6585 	struct ice_netdev_priv *np = netdev_priv(netdev);
6586 	struct ice_vsi *vsi = np->vsi;
6587 	struct ice_pf *pf = vsi->back;
6588 	enum ice_tunnel_type tnl_type;
6589 	u16 port = ntohs(ti->port);
6590 	enum ice_status status;
6591 
6592 	switch (ti->type) {
6593 	case UDP_TUNNEL_TYPE_VXLAN:
6594 		tnl_type = TNL_VXLAN;
6595 		break;
6596 	case UDP_TUNNEL_TYPE_GENEVE:
6597 		tnl_type = TNL_GENEVE;
6598 		break;
6599 	default:
6600 		netdev_err(netdev, "Unknown tunnel type\n");
6601 		return;
6602 	}
6603 
6604 	status = ice_create_tunnel(&pf->hw, tnl_type, port);
6605 	if (status == ICE_ERR_OUT_OF_RANGE)
6606 		netdev_info(netdev, "Max tunneled UDP ports reached, port %d not added\n",
6607 			    port);
6608 	else if (status)
6609 		netdev_err(netdev, "Error adding UDP tunnel - %s\n",
6610 			   ice_stat_str(status));
6611 }
6612 
6613 /**
6614  * ice_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
6615  * @netdev: This physical port's netdev
6616  * @ti: Tunnel endpoint information
6617  */
6618 static void
6619 ice_udp_tunnel_del(struct net_device *netdev, struct udp_tunnel_info *ti)
6620 {
6621 	struct ice_netdev_priv *np = netdev_priv(netdev);
6622 	struct ice_vsi *vsi = np->vsi;
6623 	struct ice_pf *pf = vsi->back;
6624 	u16 port = ntohs(ti->port);
6625 	enum ice_status status;
6626 	bool retval;
6627 
6628 	retval = ice_tunnel_port_in_use(&pf->hw, port, NULL);
6629 	if (!retval) {
6630 		netdev_info(netdev, "port %d not found in UDP tunnels list\n",
6631 			    port);
6632 		return;
6633 	}
6634 
6635 	status = ice_destroy_tunnel(&pf->hw, port, false);
6636 	if (status)
6637 		netdev_err(netdev, "error deleting port %d from UDP tunnels list\n",
6638 			   port);
6639 }
6640 
6641 /**
6642  * ice_open - Called when a network interface becomes active
6643  * @netdev: network interface device structure
6644  *
6645  * The open entry point is called when a network interface is made
6646  * active by the system (IFF_UP). At this point all resources needed
6647  * for transmit and receive operations are allocated, the interrupt
6648  * handler is registered with the OS, the netdev watchdog is enabled,
6649  * and the stack is notified that the interface is ready.
6650  *
6651  * Returns 0 on success, negative value on failure
6652  */
6653 int ice_open(struct net_device *netdev)
6654 {
6655 	struct ice_netdev_priv *np = netdev_priv(netdev);
6656 	struct ice_vsi *vsi = np->vsi;
6657 	struct ice_pf *pf = vsi->back;
6658 	struct ice_port_info *pi;
6659 	int err;
6660 
6661 	if (test_bit(__ICE_NEEDS_RESTART, pf->state)) {
6662 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6663 		return -EIO;
6664 	}
6665 
6666 	if (test_bit(__ICE_DOWN, pf->state)) {
6667 		netdev_err(netdev, "device is not ready yet\n");
6668 		return -EBUSY;
6669 	}
6670 
6671 	netif_carrier_off(netdev);
6672 
6673 	pi = vsi->port_info;
6674 	err = ice_update_link_info(pi);
6675 	if (err) {
6676 		netdev_err(netdev, "Failed to get link info, error %d\n",
6677 			   err);
6678 		return err;
6679 	}
6680 
6681 	/* Set PHY if there is media, otherwise, turn off PHY */
6682 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
6683 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6684 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state)) {
6685 			err = ice_init_phy_user_cfg(pi);
6686 			if (err) {
6687 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
6688 					   err);
6689 				return err;
6690 			}
6691 		}
6692 
6693 		err = ice_configure_phy(vsi);
6694 		if (err) {
6695 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
6696 				   err);
6697 			return err;
6698 		}
6699 	} else {
6700 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6701 		err = ice_aq_set_link_restart_an(pi, false, NULL);
6702 		if (err) {
6703 			netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
6704 				   vsi->vsi_num, err);
6705 			return err;
6706 		}
6707 	}
6708 
6709 	err = ice_vsi_open(vsi);
6710 	if (err)
6711 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6712 			   vsi->vsi_num, vsi->vsw->sw_id);
6713 
6714 	/* Update existing tunnels information */
6715 	udp_tunnel_get_rx_info(netdev);
6716 
6717 	return err;
6718 }
6719 
6720 /**
6721  * ice_stop - Disables a network interface
6722  * @netdev: network interface device structure
6723  *
6724  * The stop entry point is called when an interface is de-activated by the OS,
6725  * and the netdevice enters the DOWN state. The hardware is still under the
6726  * driver's control, but the netdev interface is disabled.
6727  *
6728  * Returns success only - not allowed to fail
6729  */
6730 int ice_stop(struct net_device *netdev)
6731 {
6732 	struct ice_netdev_priv *np = netdev_priv(netdev);
6733 	struct ice_vsi *vsi = np->vsi;
6734 
6735 	ice_vsi_close(vsi);
6736 
6737 	return 0;
6738 }
6739 
6740 /**
6741  * ice_features_check - Validate encapsulated packet conforms to limits
6742  * @skb: skb buffer
6743  * @netdev: This port's netdev
6744  * @features: Offload features that the stack believes apply
6745  */
6746 static netdev_features_t
6747 ice_features_check(struct sk_buff *skb,
6748 		   struct net_device __always_unused *netdev,
6749 		   netdev_features_t features)
6750 {
6751 	size_t len;
6752 
6753 	/* No point in doing any of this if neither checksum nor GSO are
6754 	 * being requested for this frame. We can rule out both by just
6755 	 * checking for CHECKSUM_PARTIAL
6756 	 */
6757 	if (skb->ip_summed != CHECKSUM_PARTIAL)
6758 		return features;
6759 
6760 	/* We cannot support GSO if the MSS is going to be less than
6761 	 * 64 bytes. If it is then we need to drop support for GSO.
6762 	 */
6763 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6764 		features &= ~NETIF_F_GSO_MASK;
6765 
6766 	len = skb_network_header(skb) - skb->data;
6767 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
6768 		goto out_rm_features;
6769 
6770 	len = skb_transport_header(skb) - skb_network_header(skb);
6771 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6772 		goto out_rm_features;
6773 
6774 	if (skb->encapsulation) {
6775 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
6776 		if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
6777 			goto out_rm_features;
6778 
6779 		len = skb_inner_transport_header(skb) -
6780 		      skb_inner_network_header(skb);
6781 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6782 			goto out_rm_features;
6783 	}
6784 
6785 	return features;
6786 out_rm_features:
6787 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6788 }
6789 
6790 static const struct net_device_ops ice_netdev_safe_mode_ops = {
6791 	.ndo_open = ice_open,
6792 	.ndo_stop = ice_stop,
6793 	.ndo_start_xmit = ice_start_xmit,
6794 	.ndo_set_mac_address = ice_set_mac_address,
6795 	.ndo_validate_addr = eth_validate_addr,
6796 	.ndo_change_mtu = ice_change_mtu,
6797 	.ndo_get_stats64 = ice_get_stats64,
6798 	.ndo_tx_timeout = ice_tx_timeout,
6799 };
6800 
6801 static const struct net_device_ops ice_netdev_ops = {
6802 	.ndo_open = ice_open,
6803 	.ndo_stop = ice_stop,
6804 	.ndo_start_xmit = ice_start_xmit,
6805 	.ndo_features_check = ice_features_check,
6806 	.ndo_set_rx_mode = ice_set_rx_mode,
6807 	.ndo_set_mac_address = ice_set_mac_address,
6808 	.ndo_validate_addr = eth_validate_addr,
6809 	.ndo_change_mtu = ice_change_mtu,
6810 	.ndo_get_stats64 = ice_get_stats64,
6811 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
6812 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
6813 	.ndo_set_vf_mac = ice_set_vf_mac,
6814 	.ndo_get_vf_config = ice_get_vf_cfg,
6815 	.ndo_set_vf_trust = ice_set_vf_trust,
6816 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
6817 	.ndo_set_vf_link_state = ice_set_vf_link_state,
6818 	.ndo_get_vf_stats = ice_get_vf_stats,
6819 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6820 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6821 	.ndo_set_features = ice_set_features,
6822 	.ndo_bridge_getlink = ice_bridge_getlink,
6823 	.ndo_bridge_setlink = ice_bridge_setlink,
6824 	.ndo_fdb_add = ice_fdb_add,
6825 	.ndo_fdb_del = ice_fdb_del,
6826 #ifdef CONFIG_RFS_ACCEL
6827 	.ndo_rx_flow_steer = ice_rx_flow_steer,
6828 #endif
6829 	.ndo_tx_timeout = ice_tx_timeout,
6830 	.ndo_bpf = ice_xdp,
6831 	.ndo_xdp_xmit = ice_xdp_xmit,
6832 	.ndo_xsk_wakeup = ice_xsk_wakeup,
6833 	.ndo_udp_tunnel_add = ice_udp_tunnel_add,
6834 	.ndo_udp_tunnel_del = ice_udp_tunnel_del,
6835 };
6836