xref: /linux/drivers/net/ethernet/intel/ice/ice_main.c (revision 4d66c56f7efe122d09d06cd3ebfa52a43d51a9cb)
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 "ice.h"
9 #include "ice_base.h"
10 #include "ice_lib.h"
11 #include "ice_dcb_lib.h"
12 #include "ice_dcb_nl.h"
13 
14 #define DRV_VERSION_MAJOR 0
15 #define DRV_VERSION_MINOR 8
16 #define DRV_VERSION_BUILD 1
17 
18 #define DRV_VERSION	__stringify(DRV_VERSION_MAJOR) "." \
19 			__stringify(DRV_VERSION_MINOR) "." \
20 			__stringify(DRV_VERSION_BUILD) "-k"
21 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
22 const char ice_drv_ver[] = DRV_VERSION;
23 static const char ice_driver_string[] = DRV_SUMMARY;
24 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
25 
26 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
27 #define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
28 #define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
29 
30 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
31 MODULE_DESCRIPTION(DRV_SUMMARY);
32 MODULE_LICENSE("GPL v2");
33 MODULE_VERSION(DRV_VERSION);
34 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
35 
36 static int debug = -1;
37 module_param(debug, int, 0644);
38 #ifndef CONFIG_DYNAMIC_DEBUG
39 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
40 #else
41 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
42 #endif /* !CONFIG_DYNAMIC_DEBUG */
43 
44 static struct workqueue_struct *ice_wq;
45 static const struct net_device_ops ice_netdev_safe_mode_ops;
46 static const struct net_device_ops ice_netdev_ops;
47 
48 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
49 
50 static void ice_vsi_release_all(struct ice_pf *pf);
51 
52 /**
53  * ice_get_tx_pending - returns number of Tx descriptors not processed
54  * @ring: the ring of descriptors
55  */
56 static u16 ice_get_tx_pending(struct ice_ring *ring)
57 {
58 	u16 head, tail;
59 
60 	head = ring->next_to_clean;
61 	tail = ring->next_to_use;
62 
63 	if (head != tail)
64 		return (head < tail) ?
65 			tail - head : (tail + ring->count - head);
66 	return 0;
67 }
68 
69 /**
70  * ice_check_for_hang_subtask - check for and recover hung queues
71  * @pf: pointer to PF struct
72  */
73 static void ice_check_for_hang_subtask(struct ice_pf *pf)
74 {
75 	struct ice_vsi *vsi = NULL;
76 	struct ice_hw *hw;
77 	unsigned int i;
78 	int packets;
79 	u32 v;
80 
81 	ice_for_each_vsi(pf, v)
82 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
83 			vsi = pf->vsi[v];
84 			break;
85 		}
86 
87 	if (!vsi || test_bit(__ICE_DOWN, vsi->state))
88 		return;
89 
90 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
91 		return;
92 
93 	hw = &vsi->back->hw;
94 
95 	for (i = 0; i < vsi->num_txq; i++) {
96 		struct ice_ring *tx_ring = vsi->tx_rings[i];
97 
98 		if (tx_ring && tx_ring->desc) {
99 			/* If packet counter has not changed the queue is
100 			 * likely stalled, so force an interrupt for this
101 			 * queue.
102 			 *
103 			 * prev_pkt would be negative if there was no
104 			 * pending work.
105 			 */
106 			packets = tx_ring->stats.pkts & INT_MAX;
107 			if (tx_ring->tx_stats.prev_pkt == packets) {
108 				/* Trigger sw interrupt to revive the queue */
109 				ice_trigger_sw_intr(hw, tx_ring->q_vector);
110 				continue;
111 			}
112 
113 			/* Memory barrier between read of packet count and call
114 			 * to ice_get_tx_pending()
115 			 */
116 			smp_rmb();
117 			tx_ring->tx_stats.prev_pkt =
118 			    ice_get_tx_pending(tx_ring) ? packets : -1;
119 		}
120 	}
121 }
122 
123 /**
124  * ice_init_mac_fltr - Set initial MAC filters
125  * @pf: board private structure
126  *
127  * Set initial set of MAC filters for PF VSI; configure filters for permanent
128  * address and broadcast address. If an error is encountered, netdevice will be
129  * unregistered.
130  */
131 static int ice_init_mac_fltr(struct ice_pf *pf)
132 {
133 	enum ice_status status;
134 	u8 broadcast[ETH_ALEN];
135 	struct ice_vsi *vsi;
136 
137 	vsi = ice_get_main_vsi(pf);
138 	if (!vsi)
139 		return -EINVAL;
140 
141 	/* To add a MAC filter, first add the MAC to a list and then
142 	 * pass the list to ice_add_mac.
143 	 */
144 
145 	 /* Add a unicast MAC filter so the VSI can get its packets */
146 	status = ice_vsi_cfg_mac_fltr(vsi, vsi->port_info->mac.perm_addr, true);
147 	if (status)
148 		goto unregister;
149 
150 	/* VSI needs to receive broadcast traffic, so add the broadcast
151 	 * MAC address to the list as well.
152 	 */
153 	eth_broadcast_addr(broadcast);
154 	status = ice_vsi_cfg_mac_fltr(vsi, broadcast, true);
155 	if (status)
156 		goto unregister;
157 
158 	return 0;
159 unregister:
160 	/* We aren't useful with no MAC filters, so unregister if we
161 	 * had an error
162 	 */
163 	if (status && vsi->netdev->reg_state == NETREG_REGISTERED) {
164 		dev_err(&pf->pdev->dev,
165 			"Could not add MAC filters error %d. Unregistering device\n",
166 			status);
167 		unregister_netdev(vsi->netdev);
168 		free_netdev(vsi->netdev);
169 		vsi->netdev = NULL;
170 	}
171 
172 	return -EIO;
173 }
174 
175 /**
176  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
177  * @netdev: the net device on which the sync is happening
178  * @addr: MAC address to sync
179  *
180  * This is a callback function which is called by the in kernel device sync
181  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
182  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
183  * MAC filters from the hardware.
184  */
185 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
186 {
187 	struct ice_netdev_priv *np = netdev_priv(netdev);
188 	struct ice_vsi *vsi = np->vsi;
189 
190 	if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
191 		return -EINVAL;
192 
193 	return 0;
194 }
195 
196 /**
197  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
198  * @netdev: the net device on which the unsync is happening
199  * @addr: MAC address to unsync
200  *
201  * This is a callback function which is called by the in kernel device unsync
202  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
203  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
204  * delete the MAC filters from the hardware.
205  */
206 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
207 {
208 	struct ice_netdev_priv *np = netdev_priv(netdev);
209 	struct ice_vsi *vsi = np->vsi;
210 
211 	if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
212 		return -EINVAL;
213 
214 	return 0;
215 }
216 
217 /**
218  * ice_vsi_fltr_changed - check if filter state changed
219  * @vsi: VSI to be checked
220  *
221  * returns true if filter state has changed, false otherwise.
222  */
223 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
224 {
225 	return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
226 	       test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
227 	       test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
228 }
229 
230 /**
231  * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
232  * @vsi: the VSI being configured
233  * @promisc_m: mask of promiscuous config bits
234  * @set_promisc: enable or disable promisc flag request
235  *
236  */
237 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
238 {
239 	struct ice_hw *hw = &vsi->back->hw;
240 	enum ice_status status = 0;
241 
242 	if (vsi->type != ICE_VSI_PF)
243 		return 0;
244 
245 	if (vsi->vlan_ena) {
246 		status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
247 						  set_promisc);
248 	} else {
249 		if (set_promisc)
250 			status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
251 						     0);
252 		else
253 			status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
254 						       0);
255 	}
256 
257 	if (status)
258 		return -EIO;
259 
260 	return 0;
261 }
262 
263 /**
264  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
265  * @vsi: ptr to the VSI
266  *
267  * Push any outstanding VSI filter changes through the AdminQ.
268  */
269 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
270 {
271 	struct device *dev = &vsi->back->pdev->dev;
272 	struct net_device *netdev = vsi->netdev;
273 	bool promisc_forced_on = false;
274 	struct ice_pf *pf = vsi->back;
275 	struct ice_hw *hw = &pf->hw;
276 	enum ice_status status = 0;
277 	u32 changed_flags = 0;
278 	u8 promisc_m;
279 	int err = 0;
280 
281 	if (!vsi->netdev)
282 		return -EINVAL;
283 
284 	while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
285 		usleep_range(1000, 2000);
286 
287 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
288 	vsi->current_netdev_flags = vsi->netdev->flags;
289 
290 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
291 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
292 
293 	if (ice_vsi_fltr_changed(vsi)) {
294 		clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
295 		clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
296 		clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
297 
298 		/* grab the netdev's addr_list_lock */
299 		netif_addr_lock_bh(netdev);
300 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
301 			      ice_add_mac_to_unsync_list);
302 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
303 			      ice_add_mac_to_unsync_list);
304 		/* our temp lists are populated. release lock */
305 		netif_addr_unlock_bh(netdev);
306 	}
307 
308 	/* Remove MAC addresses in the unsync list */
309 	status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
310 	ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
311 	if (status) {
312 		netdev_err(netdev, "Failed to delete MAC filters\n");
313 		/* if we failed because of alloc failures, just bail */
314 		if (status == ICE_ERR_NO_MEMORY) {
315 			err = -ENOMEM;
316 			goto out;
317 		}
318 	}
319 
320 	/* Add MAC addresses in the sync list */
321 	status = ice_add_mac(hw, &vsi->tmp_sync_list);
322 	ice_free_fltr_list(dev, &vsi->tmp_sync_list);
323 	/* If filter is added successfully or already exists, do not go into
324 	 * 'if' condition and report it as error. Instead continue processing
325 	 * rest of the function.
326 	 */
327 	if (status && status != ICE_ERR_ALREADY_EXISTS) {
328 		netdev_err(netdev, "Failed to add MAC filters\n");
329 		/* If there is no more space for new umac filters, VSI
330 		 * should go into promiscuous mode. There should be some
331 		 * space reserved for promiscuous filters.
332 		 */
333 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
334 		    !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
335 				      vsi->state)) {
336 			promisc_forced_on = true;
337 			netdev_warn(netdev,
338 				    "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
339 				    vsi->vsi_num);
340 		} else {
341 			err = -EIO;
342 			goto out;
343 		}
344 	}
345 	/* check for changes in promiscuous modes */
346 	if (changed_flags & IFF_ALLMULTI) {
347 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
348 			if (vsi->vlan_ena)
349 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
350 			else
351 				promisc_m = ICE_MCAST_PROMISC_BITS;
352 
353 			err = ice_cfg_promisc(vsi, promisc_m, true);
354 			if (err) {
355 				netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
356 					   vsi->vsi_num);
357 				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
358 				goto out_promisc;
359 			}
360 		} else if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
361 			if (vsi->vlan_ena)
362 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
363 			else
364 				promisc_m = ICE_MCAST_PROMISC_BITS;
365 
366 			err = ice_cfg_promisc(vsi, promisc_m, false);
367 			if (err) {
368 				netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
369 					   vsi->vsi_num);
370 				vsi->current_netdev_flags |= IFF_ALLMULTI;
371 				goto out_promisc;
372 			}
373 		}
374 	}
375 
376 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
377 	    test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
378 		clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
379 		if (vsi->current_netdev_flags & IFF_PROMISC) {
380 			/* Apply Rx filter rule to get traffic from wire */
381 			status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
382 						  ICE_FLTR_RX);
383 			if (status) {
384 				netdev_err(netdev, "Error setting default VSI %i Rx rule\n",
385 					   vsi->vsi_num);
386 				vsi->current_netdev_flags &= ~IFF_PROMISC;
387 				err = -EIO;
388 				goto out_promisc;
389 			}
390 		} else {
391 			/* Clear Rx filter to remove traffic from wire */
392 			status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
393 						  ICE_FLTR_RX);
394 			if (status) {
395 				netdev_err(netdev, "Error clearing default VSI %i Rx rule\n",
396 					   vsi->vsi_num);
397 				vsi->current_netdev_flags |= IFF_PROMISC;
398 				err = -EIO;
399 				goto out_promisc;
400 			}
401 		}
402 	}
403 	goto exit;
404 
405 out_promisc:
406 	set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
407 	goto exit;
408 out:
409 	/* if something went wrong then set the changed flag so we try again */
410 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
411 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
412 exit:
413 	clear_bit(__ICE_CFG_BUSY, vsi->state);
414 	return err;
415 }
416 
417 /**
418  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
419  * @pf: board private structure
420  */
421 static void ice_sync_fltr_subtask(struct ice_pf *pf)
422 {
423 	int v;
424 
425 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
426 		return;
427 
428 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
429 
430 	ice_for_each_vsi(pf, v)
431 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
432 		    ice_vsi_sync_fltr(pf->vsi[v])) {
433 			/* come back and try again later */
434 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
435 			break;
436 		}
437 }
438 
439 /**
440  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
441  * @pf: the PF
442  * @locked: is the rtnl_lock already held
443  */
444 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
445 {
446 	int v;
447 
448 	ice_for_each_vsi(pf, v)
449 		if (pf->vsi[v])
450 			ice_dis_vsi(pf->vsi[v], locked);
451 }
452 
453 /**
454  * ice_prepare_for_reset - prep for the core to reset
455  * @pf: board private structure
456  *
457  * Inform or close all dependent features in prep for reset.
458  */
459 static void
460 ice_prepare_for_reset(struct ice_pf *pf)
461 {
462 	struct ice_hw *hw = &pf->hw;
463 	int i;
464 
465 	/* already prepared for reset */
466 	if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
467 		return;
468 
469 	/* Notify VFs of impending reset */
470 	if (ice_check_sq_alive(hw, &hw->mailboxq))
471 		ice_vc_notify_reset(pf);
472 
473 	/* Disable VFs until reset is completed */
474 	for (i = 0; i < pf->num_alloc_vfs; i++)
475 		ice_set_vf_state_qs_dis(&pf->vf[i]);
476 
477 	/* clear SW filtering DB */
478 	ice_clear_hw_tbls(hw);
479 	/* disable the VSIs and their queues that are not already DOWN */
480 	ice_pf_dis_all_vsi(pf, false);
481 
482 	if (hw->port_info)
483 		ice_sched_clear_port(hw->port_info);
484 
485 	ice_shutdown_all_ctrlq(hw);
486 
487 	set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
488 }
489 
490 /**
491  * ice_do_reset - Initiate one of many types of resets
492  * @pf: board private structure
493  * @reset_type: reset type requested
494  * before this function was called.
495  */
496 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
497 {
498 	struct device *dev = &pf->pdev->dev;
499 	struct ice_hw *hw = &pf->hw;
500 
501 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
502 	WARN_ON(in_interrupt());
503 
504 	ice_prepare_for_reset(pf);
505 
506 	/* trigger the reset */
507 	if (ice_reset(hw, reset_type)) {
508 		dev_err(dev, "reset %d failed\n", reset_type);
509 		set_bit(__ICE_RESET_FAILED, pf->state);
510 		clear_bit(__ICE_RESET_OICR_RECV, pf->state);
511 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
512 		clear_bit(__ICE_PFR_REQ, pf->state);
513 		clear_bit(__ICE_CORER_REQ, pf->state);
514 		clear_bit(__ICE_GLOBR_REQ, pf->state);
515 		return;
516 	}
517 
518 	/* PFR is a bit of a special case because it doesn't result in an OICR
519 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
520 	 * associated state bits.
521 	 */
522 	if (reset_type == ICE_RESET_PFR) {
523 		pf->pfr_count++;
524 		ice_rebuild(pf, reset_type);
525 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
526 		clear_bit(__ICE_PFR_REQ, pf->state);
527 		ice_reset_all_vfs(pf, true);
528 	}
529 }
530 
531 /**
532  * ice_reset_subtask - Set up for resetting the device and driver
533  * @pf: board private structure
534  */
535 static void ice_reset_subtask(struct ice_pf *pf)
536 {
537 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
538 
539 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
540 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
541 	 * of reset is pending and sets bits in pf->state indicating the reset
542 	 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
543 	 * prepare for pending reset if not already (for PF software-initiated
544 	 * global resets the software should already be prepared for it as
545 	 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
546 	 * by firmware or software on other PFs, that bit is not set so prepare
547 	 * for the reset now), poll for reset done, rebuild and return.
548 	 */
549 	if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
550 		/* Perform the largest reset requested */
551 		if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
552 			reset_type = ICE_RESET_CORER;
553 		if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
554 			reset_type = ICE_RESET_GLOBR;
555 		if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state))
556 			reset_type = ICE_RESET_EMPR;
557 		/* return if no valid reset type requested */
558 		if (reset_type == ICE_RESET_INVAL)
559 			return;
560 		ice_prepare_for_reset(pf);
561 
562 		/* make sure we are ready to rebuild */
563 		if (ice_check_reset(&pf->hw)) {
564 			set_bit(__ICE_RESET_FAILED, pf->state);
565 		} else {
566 			/* done with reset. start rebuild */
567 			pf->hw.reset_ongoing = false;
568 			ice_rebuild(pf, reset_type);
569 			/* clear bit to resume normal operations, but
570 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
571 			 */
572 			clear_bit(__ICE_RESET_OICR_RECV, pf->state);
573 			clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
574 			clear_bit(__ICE_PFR_REQ, pf->state);
575 			clear_bit(__ICE_CORER_REQ, pf->state);
576 			clear_bit(__ICE_GLOBR_REQ, pf->state);
577 			ice_reset_all_vfs(pf, true);
578 		}
579 
580 		return;
581 	}
582 
583 	/* No pending resets to finish processing. Check for new resets */
584 	if (test_bit(__ICE_PFR_REQ, pf->state))
585 		reset_type = ICE_RESET_PFR;
586 	if (test_bit(__ICE_CORER_REQ, pf->state))
587 		reset_type = ICE_RESET_CORER;
588 	if (test_bit(__ICE_GLOBR_REQ, pf->state))
589 		reset_type = ICE_RESET_GLOBR;
590 	/* If no valid reset type requested just return */
591 	if (reset_type == ICE_RESET_INVAL)
592 		return;
593 
594 	/* reset if not already down or busy */
595 	if (!test_bit(__ICE_DOWN, pf->state) &&
596 	    !test_bit(__ICE_CFG_BUSY, pf->state)) {
597 		ice_do_reset(pf, reset_type);
598 	}
599 }
600 
601 /**
602  * ice_print_topo_conflict - print topology conflict message
603  * @vsi: the VSI whose topology status is being checked
604  */
605 static void ice_print_topo_conflict(struct ice_vsi *vsi)
606 {
607 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
608 	case ICE_AQ_LINK_TOPO_CONFLICT:
609 	case ICE_AQ_LINK_MEDIA_CONFLICT:
610 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
611 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
612 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
613 		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");
614 		break;
615 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
616 		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");
617 		break;
618 	default:
619 		break;
620 	}
621 }
622 
623 /**
624  * ice_print_link_msg - print link up or down message
625  * @vsi: the VSI whose link status is being queried
626  * @isup: boolean for if the link is now up or down
627  */
628 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
629 {
630 	struct ice_aqc_get_phy_caps_data *caps;
631 	enum ice_status status;
632 	const char *fec_req;
633 	const char *speed;
634 	const char *fec;
635 	const char *fc;
636 	const char *an;
637 
638 	if (!vsi)
639 		return;
640 
641 	if (vsi->current_isup == isup)
642 		return;
643 
644 	vsi->current_isup = isup;
645 
646 	if (!isup) {
647 		netdev_info(vsi->netdev, "NIC Link is Down\n");
648 		return;
649 	}
650 
651 	switch (vsi->port_info->phy.link_info.link_speed) {
652 	case ICE_AQ_LINK_SPEED_100GB:
653 		speed = "100 G";
654 		break;
655 	case ICE_AQ_LINK_SPEED_50GB:
656 		speed = "50 G";
657 		break;
658 	case ICE_AQ_LINK_SPEED_40GB:
659 		speed = "40 G";
660 		break;
661 	case ICE_AQ_LINK_SPEED_25GB:
662 		speed = "25 G";
663 		break;
664 	case ICE_AQ_LINK_SPEED_20GB:
665 		speed = "20 G";
666 		break;
667 	case ICE_AQ_LINK_SPEED_10GB:
668 		speed = "10 G";
669 		break;
670 	case ICE_AQ_LINK_SPEED_5GB:
671 		speed = "5 G";
672 		break;
673 	case ICE_AQ_LINK_SPEED_2500MB:
674 		speed = "2.5 G";
675 		break;
676 	case ICE_AQ_LINK_SPEED_1000MB:
677 		speed = "1 G";
678 		break;
679 	case ICE_AQ_LINK_SPEED_100MB:
680 		speed = "100 M";
681 		break;
682 	default:
683 		speed = "Unknown";
684 		break;
685 	}
686 
687 	switch (vsi->port_info->fc.current_mode) {
688 	case ICE_FC_FULL:
689 		fc = "Rx/Tx";
690 		break;
691 	case ICE_FC_TX_PAUSE:
692 		fc = "Tx";
693 		break;
694 	case ICE_FC_RX_PAUSE:
695 		fc = "Rx";
696 		break;
697 	case ICE_FC_NONE:
698 		fc = "None";
699 		break;
700 	default:
701 		fc = "Unknown";
702 		break;
703 	}
704 
705 	/* Get FEC mode based on negotiated link info */
706 	switch (vsi->port_info->phy.link_info.fec_info) {
707 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
708 		/* fall through */
709 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
710 		fec = "RS-FEC";
711 		break;
712 	case ICE_AQ_LINK_25G_KR_FEC_EN:
713 		fec = "FC-FEC/BASE-R";
714 		break;
715 	default:
716 		fec = "NONE";
717 		break;
718 	}
719 
720 	/* check if autoneg completed, might be false due to not supported */
721 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
722 		an = "True";
723 	else
724 		an = "False";
725 
726 	/* Get FEC mode requested based on PHY caps last SW configuration */
727 	caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
728 	if (!caps) {
729 		fec_req = "Unknown";
730 		goto done;
731 	}
732 
733 	status = ice_aq_get_phy_caps(vsi->port_info, false,
734 				     ICE_AQC_REPORT_SW_CFG, caps, NULL);
735 	if (status)
736 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
737 
738 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
739 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
740 		fec_req = "RS-FEC";
741 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
742 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
743 		fec_req = "FC-FEC/BASE-R";
744 	else
745 		fec_req = "NONE";
746 
747 	devm_kfree(&vsi->back->pdev->dev, caps);
748 
749 done:
750 	netdev_info(vsi->netdev, "NIC Link is up %sbps, Requested FEC: %s, FEC: %s, Autoneg: %s, Flow Control: %s\n",
751 		    speed, fec_req, fec, an, fc);
752 	ice_print_topo_conflict(vsi);
753 }
754 
755 /**
756  * ice_vsi_link_event - update the VSI's netdev
757  * @vsi: the VSI on which the link event occurred
758  * @link_up: whether or not the VSI needs to be set up or down
759  */
760 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
761 {
762 	if (!vsi)
763 		return;
764 
765 	if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
766 		return;
767 
768 	if (vsi->type == ICE_VSI_PF) {
769 		if (link_up == netif_carrier_ok(vsi->netdev))
770 			return;
771 
772 		if (link_up) {
773 			netif_carrier_on(vsi->netdev);
774 			netif_tx_wake_all_queues(vsi->netdev);
775 		} else {
776 			netif_carrier_off(vsi->netdev);
777 			netif_tx_stop_all_queues(vsi->netdev);
778 		}
779 	}
780 }
781 
782 /**
783  * ice_link_event - process the link event
784  * @pf: PF that the link event is associated with
785  * @pi: port_info for the port that the link event is associated with
786  * @link_up: true if the physical link is up and false if it is down
787  * @link_speed: current link speed received from the link event
788  *
789  * Returns 0 on success and negative on failure
790  */
791 static int
792 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
793 	       u16 link_speed)
794 {
795 	struct ice_phy_info *phy_info;
796 	struct ice_vsi *vsi;
797 	u16 old_link_speed;
798 	bool old_link;
799 	int result;
800 
801 	phy_info = &pi->phy;
802 	phy_info->link_info_old = phy_info->link_info;
803 
804 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
805 	old_link_speed = phy_info->link_info_old.link_speed;
806 
807 	/* update the link info structures and re-enable link events,
808 	 * don't bail on failure due to other book keeping needed
809 	 */
810 	result = ice_update_link_info(pi);
811 	if (result)
812 		dev_dbg(&pf->pdev->dev,
813 			"Failed to update link status and re-enable link events for port %d\n",
814 			pi->lport);
815 
816 	/* if the old link up/down and speed is the same as the new */
817 	if (link_up == old_link && link_speed == old_link_speed)
818 		return result;
819 
820 	vsi = ice_get_main_vsi(pf);
821 	if (!vsi || !vsi->port_info)
822 		return -EINVAL;
823 
824 	/* turn off PHY if media was removed */
825 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
826 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
827 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
828 
829 		result = ice_aq_set_link_restart_an(pi, false, NULL);
830 		if (result) {
831 			dev_dbg(&pf->pdev->dev,
832 				"Failed to set link down, VSI %d error %d\n",
833 				vsi->vsi_num, result);
834 			return result;
835 		}
836 	}
837 
838 	ice_vsi_link_event(vsi, link_up);
839 	ice_print_link_msg(vsi, link_up);
840 
841 	if (pf->num_alloc_vfs)
842 		ice_vc_notify_link_state(pf);
843 
844 	return result;
845 }
846 
847 /**
848  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
849  * @pf: board private structure
850  */
851 static void ice_watchdog_subtask(struct ice_pf *pf)
852 {
853 	int i;
854 
855 	/* if interface is down do nothing */
856 	if (test_bit(__ICE_DOWN, pf->state) ||
857 	    test_bit(__ICE_CFG_BUSY, pf->state))
858 		return;
859 
860 	/* make sure we don't do these things too often */
861 	if (time_before(jiffies,
862 			pf->serv_tmr_prev + pf->serv_tmr_period))
863 		return;
864 
865 	pf->serv_tmr_prev = jiffies;
866 
867 	/* Update the stats for active netdevs so the network stack
868 	 * can look at updated numbers whenever it cares to
869 	 */
870 	ice_update_pf_stats(pf);
871 	ice_for_each_vsi(pf, i)
872 		if (pf->vsi[i] && pf->vsi[i]->netdev)
873 			ice_update_vsi_stats(pf->vsi[i]);
874 }
875 
876 /**
877  * ice_init_link_events - enable/initialize link events
878  * @pi: pointer to the port_info instance
879  *
880  * Returns -EIO on failure, 0 on success
881  */
882 static int ice_init_link_events(struct ice_port_info *pi)
883 {
884 	u16 mask;
885 
886 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
887 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
888 
889 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
890 		dev_dbg(ice_hw_to_dev(pi->hw),
891 			"Failed to set link event mask for port %d\n",
892 			pi->lport);
893 		return -EIO;
894 	}
895 
896 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
897 		dev_dbg(ice_hw_to_dev(pi->hw),
898 			"Failed to enable link events for port %d\n",
899 			pi->lport);
900 		return -EIO;
901 	}
902 
903 	return 0;
904 }
905 
906 /**
907  * ice_handle_link_event - handle link event via ARQ
908  * @pf: PF that the link event is associated with
909  * @event: event structure containing link status info
910  */
911 static int
912 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
913 {
914 	struct ice_aqc_get_link_status_data *link_data;
915 	struct ice_port_info *port_info;
916 	int status;
917 
918 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
919 	port_info = pf->hw.port_info;
920 	if (!port_info)
921 		return -EINVAL;
922 
923 	status = ice_link_event(pf, port_info,
924 				!!(link_data->link_info & ICE_AQ_LINK_UP),
925 				le16_to_cpu(link_data->link_speed));
926 	if (status)
927 		dev_dbg(&pf->pdev->dev,
928 			"Could not process link event, error %d\n", status);
929 
930 	return status;
931 }
932 
933 /**
934  * __ice_clean_ctrlq - helper function to clean controlq rings
935  * @pf: ptr to struct ice_pf
936  * @q_type: specific Control queue type
937  */
938 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
939 {
940 	struct ice_rq_event_info event;
941 	struct ice_hw *hw = &pf->hw;
942 	struct ice_ctl_q_info *cq;
943 	u16 pending, i = 0;
944 	const char *qtype;
945 	u32 oldval, val;
946 
947 	/* Do not clean control queue if/when PF reset fails */
948 	if (test_bit(__ICE_RESET_FAILED, pf->state))
949 		return 0;
950 
951 	switch (q_type) {
952 	case ICE_CTL_Q_ADMIN:
953 		cq = &hw->adminq;
954 		qtype = "Admin";
955 		break;
956 	case ICE_CTL_Q_MAILBOX:
957 		cq = &hw->mailboxq;
958 		qtype = "Mailbox";
959 		break;
960 	default:
961 		dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
962 			 q_type);
963 		return 0;
964 	}
965 
966 	/* check for error indications - PF_xx_AxQLEN register layout for
967 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
968 	 */
969 	val = rd32(hw, cq->rq.len);
970 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
971 		   PF_FW_ARQLEN_ARQCRIT_M)) {
972 		oldval = val;
973 		if (val & PF_FW_ARQLEN_ARQVFE_M)
974 			dev_dbg(&pf->pdev->dev,
975 				"%s Receive Queue VF Error detected\n", qtype);
976 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
977 			dev_dbg(&pf->pdev->dev,
978 				"%s Receive Queue Overflow Error detected\n",
979 				qtype);
980 		}
981 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
982 			dev_dbg(&pf->pdev->dev,
983 				"%s Receive Queue Critical Error detected\n",
984 				qtype);
985 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
986 			 PF_FW_ARQLEN_ARQCRIT_M);
987 		if (oldval != val)
988 			wr32(hw, cq->rq.len, val);
989 	}
990 
991 	val = rd32(hw, cq->sq.len);
992 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
993 		   PF_FW_ATQLEN_ATQCRIT_M)) {
994 		oldval = val;
995 		if (val & PF_FW_ATQLEN_ATQVFE_M)
996 			dev_dbg(&pf->pdev->dev,
997 				"%s Send Queue VF Error detected\n", qtype);
998 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
999 			dev_dbg(&pf->pdev->dev,
1000 				"%s Send Queue Overflow Error detected\n",
1001 				qtype);
1002 		}
1003 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1004 			dev_dbg(&pf->pdev->dev,
1005 				"%s Send Queue Critical Error detected\n",
1006 				qtype);
1007 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1008 			 PF_FW_ATQLEN_ATQCRIT_M);
1009 		if (oldval != val)
1010 			wr32(hw, cq->sq.len, val);
1011 	}
1012 
1013 	event.buf_len = cq->rq_buf_size;
1014 	event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
1015 				     GFP_KERNEL);
1016 	if (!event.msg_buf)
1017 		return 0;
1018 
1019 	do {
1020 		enum ice_status ret;
1021 		u16 opcode;
1022 
1023 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1024 		if (ret == ICE_ERR_AQ_NO_WORK)
1025 			break;
1026 		if (ret) {
1027 			dev_err(&pf->pdev->dev,
1028 				"%s Receive Queue event error %d\n", qtype,
1029 				ret);
1030 			break;
1031 		}
1032 
1033 		opcode = le16_to_cpu(event.desc.opcode);
1034 
1035 		switch (opcode) {
1036 		case ice_aqc_opc_get_link_status:
1037 			if (ice_handle_link_event(pf, &event))
1038 				dev_err(&pf->pdev->dev,
1039 					"Could not handle link event\n");
1040 			break;
1041 		case ice_mbx_opc_send_msg_to_pf:
1042 			ice_vc_process_vf_msg(pf, &event);
1043 			break;
1044 		case ice_aqc_opc_fw_logging:
1045 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1046 			break;
1047 		case ice_aqc_opc_lldp_set_mib_change:
1048 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1049 			break;
1050 		default:
1051 			dev_dbg(&pf->pdev->dev,
1052 				"%s Receive Queue unknown event 0x%04x ignored\n",
1053 				qtype, opcode);
1054 			break;
1055 		}
1056 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1057 
1058 	devm_kfree(&pf->pdev->dev, event.msg_buf);
1059 
1060 	return pending && (i == ICE_DFLT_IRQ_WORK);
1061 }
1062 
1063 /**
1064  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1065  * @hw: pointer to hardware info
1066  * @cq: control queue information
1067  *
1068  * returns true if there are pending messages in a queue, false if there aren't
1069  */
1070 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1071 {
1072 	u16 ntu;
1073 
1074 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1075 	return cq->rq.next_to_clean != ntu;
1076 }
1077 
1078 /**
1079  * ice_clean_adminq_subtask - clean the AdminQ rings
1080  * @pf: board private structure
1081  */
1082 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1083 {
1084 	struct ice_hw *hw = &pf->hw;
1085 
1086 	if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1087 		return;
1088 
1089 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1090 		return;
1091 
1092 	clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1093 
1094 	/* There might be a situation where new messages arrive to a control
1095 	 * queue between processing the last message and clearing the
1096 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1097 	 * ice_ctrlq_pending) and process new messages if any.
1098 	 */
1099 	if (ice_ctrlq_pending(hw, &hw->adminq))
1100 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1101 
1102 	ice_flush(hw);
1103 }
1104 
1105 /**
1106  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1107  * @pf: board private structure
1108  */
1109 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1110 {
1111 	struct ice_hw *hw = &pf->hw;
1112 
1113 	if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1114 		return;
1115 
1116 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1117 		return;
1118 
1119 	clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1120 
1121 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1122 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1123 
1124 	ice_flush(hw);
1125 }
1126 
1127 /**
1128  * ice_service_task_schedule - schedule the service task to wake up
1129  * @pf: board private structure
1130  *
1131  * If not already scheduled, this puts the task into the work queue.
1132  */
1133 static void ice_service_task_schedule(struct ice_pf *pf)
1134 {
1135 	if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1136 	    !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1137 	    !test_bit(__ICE_NEEDS_RESTART, pf->state))
1138 		queue_work(ice_wq, &pf->serv_task);
1139 }
1140 
1141 /**
1142  * ice_service_task_complete - finish up the service task
1143  * @pf: board private structure
1144  */
1145 static void ice_service_task_complete(struct ice_pf *pf)
1146 {
1147 	WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1148 
1149 	/* force memory (pf->state) to sync before next service task */
1150 	smp_mb__before_atomic();
1151 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1152 }
1153 
1154 /**
1155  * ice_service_task_stop - stop service task and cancel works
1156  * @pf: board private structure
1157  */
1158 static void ice_service_task_stop(struct ice_pf *pf)
1159 {
1160 	set_bit(__ICE_SERVICE_DIS, pf->state);
1161 
1162 	if (pf->serv_tmr.function)
1163 		del_timer_sync(&pf->serv_tmr);
1164 	if (pf->serv_task.func)
1165 		cancel_work_sync(&pf->serv_task);
1166 
1167 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1168 }
1169 
1170 /**
1171  * ice_service_task_restart - restart service task and schedule works
1172  * @pf: board private structure
1173  *
1174  * This function is needed for suspend and resume works (e.g WoL scenario)
1175  */
1176 static void ice_service_task_restart(struct ice_pf *pf)
1177 {
1178 	clear_bit(__ICE_SERVICE_DIS, pf->state);
1179 	ice_service_task_schedule(pf);
1180 }
1181 
1182 /**
1183  * ice_service_timer - timer callback to schedule service task
1184  * @t: pointer to timer_list
1185  */
1186 static void ice_service_timer(struct timer_list *t)
1187 {
1188 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1189 
1190 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1191 	ice_service_task_schedule(pf);
1192 }
1193 
1194 /**
1195  * ice_handle_mdd_event - handle malicious driver detect event
1196  * @pf: pointer to the PF structure
1197  *
1198  * Called from service task. OICR interrupt handler indicates MDD event
1199  */
1200 static void ice_handle_mdd_event(struct ice_pf *pf)
1201 {
1202 	struct ice_hw *hw = &pf->hw;
1203 	bool mdd_detected = false;
1204 	u32 reg;
1205 	int i;
1206 
1207 	if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1208 		return;
1209 
1210 	/* find what triggered the MDD event */
1211 	reg = rd32(hw, GL_MDET_TX_PQM);
1212 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1213 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1214 				GL_MDET_TX_PQM_PF_NUM_S;
1215 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1216 				GL_MDET_TX_PQM_VF_NUM_S;
1217 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1218 				GL_MDET_TX_PQM_MAL_TYPE_S;
1219 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1220 				GL_MDET_TX_PQM_QNUM_S);
1221 
1222 		if (netif_msg_tx_err(pf))
1223 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1224 				 event, queue, pf_num, vf_num);
1225 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1226 		mdd_detected = true;
1227 	}
1228 
1229 	reg = rd32(hw, GL_MDET_TX_TCLAN);
1230 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1231 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1232 				GL_MDET_TX_TCLAN_PF_NUM_S;
1233 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1234 				GL_MDET_TX_TCLAN_VF_NUM_S;
1235 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1236 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1237 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1238 				GL_MDET_TX_TCLAN_QNUM_S);
1239 
1240 		if (netif_msg_rx_err(pf))
1241 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1242 				 event, queue, pf_num, vf_num);
1243 		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1244 		mdd_detected = true;
1245 	}
1246 
1247 	reg = rd32(hw, GL_MDET_RX);
1248 	if (reg & GL_MDET_RX_VALID_M) {
1249 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1250 				GL_MDET_RX_PF_NUM_S;
1251 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1252 				GL_MDET_RX_VF_NUM_S;
1253 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1254 				GL_MDET_RX_MAL_TYPE_S;
1255 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1256 				GL_MDET_RX_QNUM_S);
1257 
1258 		if (netif_msg_rx_err(pf))
1259 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1260 				 event, queue, pf_num, vf_num);
1261 		wr32(hw, GL_MDET_RX, 0xffffffff);
1262 		mdd_detected = true;
1263 	}
1264 
1265 	if (mdd_detected) {
1266 		bool pf_mdd_detected = false;
1267 
1268 		reg = rd32(hw, PF_MDET_TX_PQM);
1269 		if (reg & PF_MDET_TX_PQM_VALID_M) {
1270 			wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1271 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1272 			pf_mdd_detected = true;
1273 		}
1274 
1275 		reg = rd32(hw, PF_MDET_TX_TCLAN);
1276 		if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1277 			wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1278 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1279 			pf_mdd_detected = true;
1280 		}
1281 
1282 		reg = rd32(hw, PF_MDET_RX);
1283 		if (reg & PF_MDET_RX_VALID_M) {
1284 			wr32(hw, PF_MDET_RX, 0xFFFF);
1285 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1286 			pf_mdd_detected = true;
1287 		}
1288 		/* Queue belongs to the PF initiate a reset */
1289 		if (pf_mdd_detected) {
1290 			set_bit(__ICE_NEEDS_RESTART, pf->state);
1291 			ice_service_task_schedule(pf);
1292 		}
1293 	}
1294 
1295 	/* check to see if one of the VFs caused the MDD */
1296 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1297 		struct ice_vf *vf = &pf->vf[i];
1298 
1299 		bool vf_mdd_detected = false;
1300 
1301 		reg = rd32(hw, VP_MDET_TX_PQM(i));
1302 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1303 			wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1304 			vf_mdd_detected = true;
1305 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1306 				 i);
1307 		}
1308 
1309 		reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1310 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1311 			wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1312 			vf_mdd_detected = true;
1313 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1314 				 i);
1315 		}
1316 
1317 		reg = rd32(hw, VP_MDET_TX_TDPU(i));
1318 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1319 			wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1320 			vf_mdd_detected = true;
1321 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1322 				 i);
1323 		}
1324 
1325 		reg = rd32(hw, VP_MDET_RX(i));
1326 		if (reg & VP_MDET_RX_VALID_M) {
1327 			wr32(hw, VP_MDET_RX(i), 0xFFFF);
1328 			vf_mdd_detected = true;
1329 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
1330 				 i);
1331 		}
1332 
1333 		if (vf_mdd_detected) {
1334 			vf->num_mdd_events++;
1335 			if (vf->num_mdd_events &&
1336 			    vf->num_mdd_events <= ICE_MDD_EVENTS_THRESHOLD)
1337 				dev_info(&pf->pdev->dev,
1338 					 "VF %d has had %llu MDD events since last boot, Admin might need to reload AVF driver with this number of events\n",
1339 					 i, vf->num_mdd_events);
1340 		}
1341 	}
1342 }
1343 
1344 /**
1345  * ice_force_phys_link_state - Force the physical link state
1346  * @vsi: VSI to force the physical link state to up/down
1347  * @link_up: true/false indicates to set the physical link to up/down
1348  *
1349  * Force the physical link state by getting the current PHY capabilities from
1350  * hardware and setting the PHY config based on the determined capabilities. If
1351  * link changes a link event will be triggered because both the Enable Automatic
1352  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1353  *
1354  * Returns 0 on success, negative on failure
1355  */
1356 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1357 {
1358 	struct ice_aqc_get_phy_caps_data *pcaps;
1359 	struct ice_aqc_set_phy_cfg_data *cfg;
1360 	struct ice_port_info *pi;
1361 	struct device *dev;
1362 	int retcode;
1363 
1364 	if (!vsi || !vsi->port_info || !vsi->back)
1365 		return -EINVAL;
1366 	if (vsi->type != ICE_VSI_PF)
1367 		return 0;
1368 
1369 	dev = &vsi->back->pdev->dev;
1370 
1371 	pi = vsi->port_info;
1372 
1373 	pcaps = devm_kzalloc(dev, sizeof(*pcaps), GFP_KERNEL);
1374 	if (!pcaps)
1375 		return -ENOMEM;
1376 
1377 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1378 				      NULL);
1379 	if (retcode) {
1380 		dev_err(dev,
1381 			"Failed to get phy capabilities, VSI %d error %d\n",
1382 			vsi->vsi_num, retcode);
1383 		retcode = -EIO;
1384 		goto out;
1385 	}
1386 
1387 	/* No change in link */
1388 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1389 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1390 		goto out;
1391 
1392 	cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
1393 	if (!cfg) {
1394 		retcode = -ENOMEM;
1395 		goto out;
1396 	}
1397 
1398 	cfg->phy_type_low = pcaps->phy_type_low;
1399 	cfg->phy_type_high = pcaps->phy_type_high;
1400 	cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1401 	cfg->low_power_ctrl = pcaps->low_power_ctrl;
1402 	cfg->eee_cap = pcaps->eee_cap;
1403 	cfg->eeer_value = pcaps->eeer_value;
1404 	cfg->link_fec_opt = pcaps->link_fec_options;
1405 	if (link_up)
1406 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1407 	else
1408 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1409 
1410 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL);
1411 	if (retcode) {
1412 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1413 			vsi->vsi_num, retcode);
1414 		retcode = -EIO;
1415 	}
1416 
1417 	devm_kfree(dev, cfg);
1418 out:
1419 	devm_kfree(dev, pcaps);
1420 	return retcode;
1421 }
1422 
1423 /**
1424  * ice_check_media_subtask - Check for media; bring link up if detected.
1425  * @pf: pointer to PF struct
1426  */
1427 static void ice_check_media_subtask(struct ice_pf *pf)
1428 {
1429 	struct ice_port_info *pi;
1430 	struct ice_vsi *vsi;
1431 	int err;
1432 
1433 	vsi = ice_get_main_vsi(pf);
1434 	if (!vsi)
1435 		return;
1436 
1437 	/* No need to check for media if it's already present or the interface
1438 	 * is down
1439 	 */
1440 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) ||
1441 	    test_bit(__ICE_DOWN, vsi->state))
1442 		return;
1443 
1444 	/* Refresh link info and check if media is present */
1445 	pi = vsi->port_info;
1446 	err = ice_update_link_info(pi);
1447 	if (err)
1448 		return;
1449 
1450 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
1451 		err = ice_force_phys_link_state(vsi, true);
1452 		if (err)
1453 			return;
1454 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1455 
1456 		/* A Link Status Event will be generated; the event handler
1457 		 * will complete bringing the interface up
1458 		 */
1459 	}
1460 }
1461 
1462 /**
1463  * ice_service_task - manage and run subtasks
1464  * @work: pointer to work_struct contained by the PF struct
1465  */
1466 static void ice_service_task(struct work_struct *work)
1467 {
1468 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1469 	unsigned long start_time = jiffies;
1470 
1471 	/* subtasks */
1472 
1473 	/* process reset requests first */
1474 	ice_reset_subtask(pf);
1475 
1476 	/* bail if a reset/recovery cycle is pending or rebuild failed */
1477 	if (ice_is_reset_in_progress(pf->state) ||
1478 	    test_bit(__ICE_SUSPENDED, pf->state) ||
1479 	    test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1480 		ice_service_task_complete(pf);
1481 		return;
1482 	}
1483 
1484 	ice_clean_adminq_subtask(pf);
1485 	ice_check_media_subtask(pf);
1486 	ice_check_for_hang_subtask(pf);
1487 	ice_sync_fltr_subtask(pf);
1488 	ice_handle_mdd_event(pf);
1489 	ice_watchdog_subtask(pf);
1490 
1491 	if (ice_is_safe_mode(pf)) {
1492 		ice_service_task_complete(pf);
1493 		return;
1494 	}
1495 
1496 	ice_process_vflr_event(pf);
1497 	ice_clean_mailboxq_subtask(pf);
1498 
1499 	/* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1500 	ice_service_task_complete(pf);
1501 
1502 	/* If the tasks have taken longer than one service timer period
1503 	 * or there is more work to be done, reset the service timer to
1504 	 * schedule the service task now.
1505 	 */
1506 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1507 	    test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1508 	    test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1509 	    test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1510 	    test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1511 		mod_timer(&pf->serv_tmr, jiffies);
1512 }
1513 
1514 /**
1515  * ice_set_ctrlq_len - helper function to set controlq length
1516  * @hw: pointer to the HW instance
1517  */
1518 static void ice_set_ctrlq_len(struct ice_hw *hw)
1519 {
1520 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
1521 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
1522 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1523 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1524 	hw->mailboxq.num_rq_entries = ICE_MBXRQ_LEN;
1525 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
1526 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1527 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1528 }
1529 
1530 /**
1531  * ice_irq_affinity_notify - Callback for affinity changes
1532  * @notify: context as to what irq was changed
1533  * @mask: the new affinity mask
1534  *
1535  * This is a callback function used by the irq_set_affinity_notifier function
1536  * so that we may register to receive changes to the irq affinity masks.
1537  */
1538 static void
1539 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1540 			const cpumask_t *mask)
1541 {
1542 	struct ice_q_vector *q_vector =
1543 		container_of(notify, struct ice_q_vector, affinity_notify);
1544 
1545 	cpumask_copy(&q_vector->affinity_mask, mask);
1546 }
1547 
1548 /**
1549  * ice_irq_affinity_release - Callback for affinity notifier release
1550  * @ref: internal core kernel usage
1551  *
1552  * This is a callback function used by the irq_set_affinity_notifier function
1553  * to inform the current notification subscriber that they will no longer
1554  * receive notifications.
1555  */
1556 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1557 
1558 /**
1559  * ice_vsi_ena_irq - Enable IRQ for the given VSI
1560  * @vsi: the VSI being configured
1561  */
1562 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1563 {
1564 	struct ice_hw *hw = &vsi->back->hw;
1565 	int i;
1566 
1567 	ice_for_each_q_vector(vsi, i)
1568 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1569 
1570 	ice_flush(hw);
1571 	return 0;
1572 }
1573 
1574 /**
1575  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1576  * @vsi: the VSI being configured
1577  * @basename: name for the vector
1578  */
1579 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1580 {
1581 	int q_vectors = vsi->num_q_vectors;
1582 	struct ice_pf *pf = vsi->back;
1583 	int base = vsi->base_vector;
1584 	int rx_int_idx = 0;
1585 	int tx_int_idx = 0;
1586 	int vector, err;
1587 	int irq_num;
1588 
1589 	for (vector = 0; vector < q_vectors; vector++) {
1590 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1591 
1592 		irq_num = pf->msix_entries[base + vector].vector;
1593 
1594 		if (q_vector->tx.ring && q_vector->rx.ring) {
1595 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1596 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1597 			tx_int_idx++;
1598 		} else if (q_vector->rx.ring) {
1599 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1600 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
1601 		} else if (q_vector->tx.ring) {
1602 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1603 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
1604 		} else {
1605 			/* skip this unused q_vector */
1606 			continue;
1607 		}
1608 		err = devm_request_irq(&pf->pdev->dev, irq_num,
1609 				       vsi->irq_handler, 0,
1610 				       q_vector->name, q_vector);
1611 		if (err) {
1612 			netdev_err(vsi->netdev,
1613 				   "MSIX request_irq failed, error: %d\n", err);
1614 			goto free_q_irqs;
1615 		}
1616 
1617 		/* register for affinity change notifications */
1618 		q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1619 		q_vector->affinity_notify.release = ice_irq_affinity_release;
1620 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1621 
1622 		/* assign the mask for this irq */
1623 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1624 	}
1625 
1626 	vsi->irqs_ready = true;
1627 	return 0;
1628 
1629 free_q_irqs:
1630 	while (vector) {
1631 		vector--;
1632 		irq_num = pf->msix_entries[base + vector].vector,
1633 		irq_set_affinity_notifier(irq_num, NULL);
1634 		irq_set_affinity_hint(irq_num, NULL);
1635 		devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1636 	}
1637 	return err;
1638 }
1639 
1640 /**
1641  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
1642  * @vsi: VSI to setup Tx rings used by XDP
1643  *
1644  * Return 0 on success and negative value on error
1645  */
1646 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
1647 {
1648 	struct device *dev = &vsi->back->pdev->dev;
1649 	int i;
1650 
1651 	for (i = 0; i < vsi->num_xdp_txq; i++) {
1652 		u16 xdp_q_idx = vsi->alloc_txq + i;
1653 		struct ice_ring *xdp_ring;
1654 
1655 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
1656 
1657 		if (!xdp_ring)
1658 			goto free_xdp_rings;
1659 
1660 		xdp_ring->q_index = xdp_q_idx;
1661 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
1662 		xdp_ring->ring_active = false;
1663 		xdp_ring->vsi = vsi;
1664 		xdp_ring->netdev = NULL;
1665 		xdp_ring->dev = dev;
1666 		xdp_ring->count = vsi->num_tx_desc;
1667 		vsi->xdp_rings[i] = xdp_ring;
1668 		if (ice_setup_tx_ring(xdp_ring))
1669 			goto free_xdp_rings;
1670 		ice_set_ring_xdp(xdp_ring);
1671 		xdp_ring->xsk_umem = ice_xsk_umem(xdp_ring);
1672 	}
1673 
1674 	return 0;
1675 
1676 free_xdp_rings:
1677 	for (; i >= 0; i--)
1678 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
1679 			ice_free_tx_ring(vsi->xdp_rings[i]);
1680 	return -ENOMEM;
1681 }
1682 
1683 /**
1684  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
1685  * @vsi: VSI to set the bpf prog on
1686  * @prog: the bpf prog pointer
1687  */
1688 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
1689 {
1690 	struct bpf_prog *old_prog;
1691 	int i;
1692 
1693 	old_prog = xchg(&vsi->xdp_prog, prog);
1694 	if (old_prog)
1695 		bpf_prog_put(old_prog);
1696 
1697 	ice_for_each_rxq(vsi, i)
1698 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
1699 }
1700 
1701 /**
1702  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
1703  * @vsi: VSI to bring up Tx rings used by XDP
1704  * @prog: bpf program that will be assigned to VSI
1705  *
1706  * Return 0 on success and negative value on error
1707  */
1708 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
1709 {
1710 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1711 	int xdp_rings_rem = vsi->num_xdp_txq;
1712 	struct ice_pf *pf = vsi->back;
1713 	struct ice_qs_cfg xdp_qs_cfg = {
1714 		.qs_mutex = &pf->avail_q_mutex,
1715 		.pf_map = pf->avail_txqs,
1716 		.pf_map_size = pf->max_pf_txqs,
1717 		.q_count = vsi->num_xdp_txq,
1718 		.scatter_count = ICE_MAX_SCATTER_TXQS,
1719 		.vsi_map = vsi->txq_map,
1720 		.vsi_map_offset = vsi->alloc_txq,
1721 		.mapping_mode = ICE_VSI_MAP_CONTIG
1722 	};
1723 	enum ice_status status;
1724 	int i, v_idx;
1725 
1726 	vsi->xdp_rings = devm_kcalloc(&pf->pdev->dev, vsi->num_xdp_txq,
1727 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
1728 	if (!vsi->xdp_rings)
1729 		return -ENOMEM;
1730 
1731 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
1732 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
1733 		goto err_map_xdp;
1734 
1735 	if (ice_xdp_alloc_setup_rings(vsi))
1736 		goto clear_xdp_rings;
1737 
1738 	/* follow the logic from ice_vsi_map_rings_to_vectors */
1739 	ice_for_each_q_vector(vsi, v_idx) {
1740 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
1741 		int xdp_rings_per_v, q_id, q_base;
1742 
1743 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
1744 					       vsi->num_q_vectors - v_idx);
1745 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
1746 
1747 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
1748 			struct ice_ring *xdp_ring = vsi->xdp_rings[q_id];
1749 
1750 			xdp_ring->q_vector = q_vector;
1751 			xdp_ring->next = q_vector->tx.ring;
1752 			q_vector->tx.ring = xdp_ring;
1753 		}
1754 		xdp_rings_rem -= xdp_rings_per_v;
1755 	}
1756 
1757 	/* omit the scheduler update if in reset path; XDP queues will be
1758 	 * taken into account at the end of ice_vsi_rebuild, where
1759 	 * ice_cfg_vsi_lan is being called
1760 	 */
1761 	if (ice_is_reset_in_progress(pf->state))
1762 		return 0;
1763 
1764 	/* tell the Tx scheduler that right now we have
1765 	 * additional queues
1766 	 */
1767 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
1768 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
1769 
1770 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1771 				 max_txqs);
1772 	if (status) {
1773 		dev_err(&pf->pdev->dev,
1774 			"Failed VSI LAN queue config for XDP, error:%d\n",
1775 			status);
1776 		goto clear_xdp_rings;
1777 	}
1778 	ice_vsi_assign_bpf_prog(vsi, prog);
1779 
1780 	return 0;
1781 clear_xdp_rings:
1782 	for (i = 0; i < vsi->num_xdp_txq; i++)
1783 		if (vsi->xdp_rings[i]) {
1784 			kfree_rcu(vsi->xdp_rings[i], rcu);
1785 			vsi->xdp_rings[i] = NULL;
1786 		}
1787 
1788 err_map_xdp:
1789 	mutex_lock(&pf->avail_q_mutex);
1790 	for (i = 0; i < vsi->num_xdp_txq; i++) {
1791 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
1792 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
1793 	}
1794 	mutex_unlock(&pf->avail_q_mutex);
1795 
1796 	devm_kfree(&pf->pdev->dev, vsi->xdp_rings);
1797 	return -ENOMEM;
1798 }
1799 
1800 /**
1801  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
1802  * @vsi: VSI to remove XDP rings
1803  *
1804  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
1805  * resources
1806  */
1807 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
1808 {
1809 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1810 	struct ice_pf *pf = vsi->back;
1811 	int i, v_idx;
1812 
1813 	/* q_vectors are freed in reset path so there's no point in detaching
1814 	 * rings; in case of rebuild being triggered not from reset reset bits
1815 	 * in pf->state won't be set, so additionally check first q_vector
1816 	 * against NULL
1817 	 */
1818 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
1819 		goto free_qmap;
1820 
1821 	ice_for_each_q_vector(vsi, v_idx) {
1822 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
1823 		struct ice_ring *ring;
1824 
1825 		ice_for_each_ring(ring, q_vector->tx)
1826 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
1827 				break;
1828 
1829 		/* restore the value of last node prior to XDP setup */
1830 		q_vector->tx.ring = ring;
1831 	}
1832 
1833 free_qmap:
1834 	mutex_lock(&pf->avail_q_mutex);
1835 	for (i = 0; i < vsi->num_xdp_txq; i++) {
1836 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
1837 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
1838 	}
1839 	mutex_unlock(&pf->avail_q_mutex);
1840 
1841 	for (i = 0; i < vsi->num_xdp_txq; i++)
1842 		if (vsi->xdp_rings[i]) {
1843 			if (vsi->xdp_rings[i]->desc)
1844 				ice_free_tx_ring(vsi->xdp_rings[i]);
1845 			kfree_rcu(vsi->xdp_rings[i], rcu);
1846 			vsi->xdp_rings[i] = NULL;
1847 		}
1848 
1849 	devm_kfree(&pf->pdev->dev, vsi->xdp_rings);
1850 	vsi->xdp_rings = NULL;
1851 
1852 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
1853 		return 0;
1854 
1855 	ice_vsi_assign_bpf_prog(vsi, NULL);
1856 
1857 	/* notify Tx scheduler that we destroyed XDP queues and bring
1858 	 * back the old number of child nodes
1859 	 */
1860 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
1861 		max_txqs[i] = vsi->num_txq;
1862 
1863 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1864 			       max_txqs);
1865 }
1866 
1867 /**
1868  * ice_xdp_setup_prog - Add or remove XDP eBPF program
1869  * @vsi: VSI to setup XDP for
1870  * @prog: XDP program
1871  * @extack: netlink extended ack
1872  */
1873 static int
1874 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
1875 		   struct netlink_ext_ack *extack)
1876 {
1877 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
1878 	bool if_running = netif_running(vsi->netdev);
1879 	int ret = 0, xdp_ring_err = 0;
1880 
1881 	if (frame_size > vsi->rx_buf_len) {
1882 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
1883 		return -EOPNOTSUPP;
1884 	}
1885 
1886 	/* need to stop netdev while setting up the program for Rx rings */
1887 	if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) {
1888 		ret = ice_down(vsi);
1889 		if (ret) {
1890 			NL_SET_ERR_MSG_MOD(extack,
1891 					   "Preparing device for XDP attach failed");
1892 			return ret;
1893 		}
1894 	}
1895 
1896 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
1897 		vsi->num_xdp_txq = vsi->alloc_txq;
1898 		xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
1899 		if (xdp_ring_err)
1900 			NL_SET_ERR_MSG_MOD(extack,
1901 					   "Setting up XDP Tx resources failed");
1902 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
1903 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
1904 		if (xdp_ring_err)
1905 			NL_SET_ERR_MSG_MOD(extack,
1906 					   "Freeing XDP Tx resources failed");
1907 	} else {
1908 		ice_vsi_assign_bpf_prog(vsi, prog);
1909 	}
1910 
1911 	if (if_running)
1912 		ret = ice_up(vsi);
1913 
1914 	if (!ret && prog && vsi->xsk_umems) {
1915 		int i;
1916 
1917 		ice_for_each_rxq(vsi, i) {
1918 			struct ice_ring *rx_ring = vsi->rx_rings[i];
1919 
1920 			if (rx_ring->xsk_umem)
1921 				napi_schedule(&rx_ring->q_vector->napi);
1922 		}
1923 	}
1924 
1925 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
1926 }
1927 
1928 /**
1929  * ice_xdp - implements XDP handler
1930  * @dev: netdevice
1931  * @xdp: XDP command
1932  */
1933 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1934 {
1935 	struct ice_netdev_priv *np = netdev_priv(dev);
1936 	struct ice_vsi *vsi = np->vsi;
1937 
1938 	if (vsi->type != ICE_VSI_PF) {
1939 		NL_SET_ERR_MSG_MOD(xdp->extack,
1940 				   "XDP can be loaded only on PF VSI");
1941 		return -EINVAL;
1942 	}
1943 
1944 	switch (xdp->command) {
1945 	case XDP_SETUP_PROG:
1946 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
1947 	case XDP_QUERY_PROG:
1948 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
1949 		return 0;
1950 	case XDP_SETUP_XSK_UMEM:
1951 		return ice_xsk_umem_setup(vsi, xdp->xsk.umem,
1952 					  xdp->xsk.queue_id);
1953 	default:
1954 		return -EINVAL;
1955 	}
1956 }
1957 
1958 /**
1959  * ice_ena_misc_vector - enable the non-queue interrupts
1960  * @pf: board private structure
1961  */
1962 static void ice_ena_misc_vector(struct ice_pf *pf)
1963 {
1964 	struct ice_hw *hw = &pf->hw;
1965 	u32 val;
1966 
1967 	/* clear things first */
1968 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
1969 	rd32(hw, PFINT_OICR);		/* read to clear */
1970 
1971 	val = (PFINT_OICR_ECC_ERR_M |
1972 	       PFINT_OICR_MAL_DETECT_M |
1973 	       PFINT_OICR_GRST_M |
1974 	       PFINT_OICR_PCI_EXCEPTION_M |
1975 	       PFINT_OICR_VFLR_M |
1976 	       PFINT_OICR_HMC_ERR_M |
1977 	       PFINT_OICR_PE_CRITERR_M);
1978 
1979 	wr32(hw, PFINT_OICR_ENA, val);
1980 
1981 	/* SW_ITR_IDX = 0, but don't change INTENA */
1982 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1983 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1984 }
1985 
1986 /**
1987  * ice_misc_intr - misc interrupt handler
1988  * @irq: interrupt number
1989  * @data: pointer to a q_vector
1990  */
1991 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1992 {
1993 	struct ice_pf *pf = (struct ice_pf *)data;
1994 	struct ice_hw *hw = &pf->hw;
1995 	irqreturn_t ret = IRQ_NONE;
1996 	u32 oicr, ena_mask;
1997 
1998 	set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1999 	set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2000 
2001 	oicr = rd32(hw, PFINT_OICR);
2002 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2003 
2004 	if (oicr & PFINT_OICR_SWINT_M) {
2005 		ena_mask &= ~PFINT_OICR_SWINT_M;
2006 		pf->sw_int_count++;
2007 	}
2008 
2009 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2010 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2011 		set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2012 	}
2013 	if (oicr & PFINT_OICR_VFLR_M) {
2014 		ena_mask &= ~PFINT_OICR_VFLR_M;
2015 		set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
2016 	}
2017 
2018 	if (oicr & PFINT_OICR_GRST_M) {
2019 		u32 reset;
2020 
2021 		/* we have a reset warning */
2022 		ena_mask &= ~PFINT_OICR_GRST_M;
2023 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2024 			GLGEN_RSTAT_RESET_TYPE_S;
2025 
2026 		if (reset == ICE_RESET_CORER)
2027 			pf->corer_count++;
2028 		else if (reset == ICE_RESET_GLOBR)
2029 			pf->globr_count++;
2030 		else if (reset == ICE_RESET_EMPR)
2031 			pf->empr_count++;
2032 		else
2033 			dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
2034 				reset);
2035 
2036 		/* If a reset cycle isn't already in progress, we set a bit in
2037 		 * pf->state so that the service task can start a reset/rebuild.
2038 		 * We also make note of which reset happened so that peer
2039 		 * devices/drivers can be informed.
2040 		 */
2041 		if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
2042 			if (reset == ICE_RESET_CORER)
2043 				set_bit(__ICE_CORER_RECV, pf->state);
2044 			else if (reset == ICE_RESET_GLOBR)
2045 				set_bit(__ICE_GLOBR_RECV, pf->state);
2046 			else
2047 				set_bit(__ICE_EMPR_RECV, pf->state);
2048 
2049 			/* There are couple of different bits at play here.
2050 			 * hw->reset_ongoing indicates whether the hardware is
2051 			 * in reset. This is set to true when a reset interrupt
2052 			 * is received and set back to false after the driver
2053 			 * has determined that the hardware is out of reset.
2054 			 *
2055 			 * __ICE_RESET_OICR_RECV in pf->state indicates
2056 			 * that a post reset rebuild is required before the
2057 			 * driver is operational again. This is set above.
2058 			 *
2059 			 * As this is the start of the reset/rebuild cycle, set
2060 			 * both to indicate that.
2061 			 */
2062 			hw->reset_ongoing = true;
2063 		}
2064 	}
2065 
2066 	if (oicr & PFINT_OICR_HMC_ERR_M) {
2067 		ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2068 		dev_dbg(&pf->pdev->dev,
2069 			"HMC Error interrupt - info 0x%x, data 0x%x\n",
2070 			rd32(hw, PFHMC_ERRORINFO),
2071 			rd32(hw, PFHMC_ERRORDATA));
2072 	}
2073 
2074 	/* Report any remaining unexpected interrupts */
2075 	oicr &= ena_mask;
2076 	if (oicr) {
2077 		dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
2078 			oicr);
2079 		/* If a critical error is pending there is no choice but to
2080 		 * reset the device.
2081 		 */
2082 		if (oicr & (PFINT_OICR_PE_CRITERR_M |
2083 			    PFINT_OICR_PCI_EXCEPTION_M |
2084 			    PFINT_OICR_ECC_ERR_M)) {
2085 			set_bit(__ICE_PFR_REQ, pf->state);
2086 			ice_service_task_schedule(pf);
2087 		}
2088 	}
2089 	ret = IRQ_HANDLED;
2090 
2091 	if (!test_bit(__ICE_DOWN, pf->state)) {
2092 		ice_service_task_schedule(pf);
2093 		ice_irq_dynamic_ena(hw, NULL, NULL);
2094 	}
2095 
2096 	return ret;
2097 }
2098 
2099 /**
2100  * ice_dis_ctrlq_interrupts - disable control queue interrupts
2101  * @hw: pointer to HW structure
2102  */
2103 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
2104 {
2105 	/* disable Admin queue Interrupt causes */
2106 	wr32(hw, PFINT_FW_CTL,
2107 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
2108 
2109 	/* disable Mailbox queue Interrupt causes */
2110 	wr32(hw, PFINT_MBX_CTL,
2111 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
2112 
2113 	/* disable Control queue Interrupt causes */
2114 	wr32(hw, PFINT_OICR_CTL,
2115 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
2116 
2117 	ice_flush(hw);
2118 }
2119 
2120 /**
2121  * ice_free_irq_msix_misc - Unroll misc vector setup
2122  * @pf: board private structure
2123  */
2124 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2125 {
2126 	struct ice_hw *hw = &pf->hw;
2127 
2128 	ice_dis_ctrlq_interrupts(hw);
2129 
2130 	/* disable OICR interrupt */
2131 	wr32(hw, PFINT_OICR_ENA, 0);
2132 	ice_flush(hw);
2133 
2134 	if (pf->msix_entries) {
2135 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2136 		devm_free_irq(&pf->pdev->dev,
2137 			      pf->msix_entries[pf->oicr_idx].vector, pf);
2138 	}
2139 
2140 	pf->num_avail_sw_msix += 1;
2141 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2142 }
2143 
2144 /**
2145  * ice_ena_ctrlq_interrupts - enable control queue interrupts
2146  * @hw: pointer to HW structure
2147  * @reg_idx: HW vector index to associate the control queue interrupts with
2148  */
2149 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
2150 {
2151 	u32 val;
2152 
2153 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2154 	       PFINT_OICR_CTL_CAUSE_ENA_M);
2155 	wr32(hw, PFINT_OICR_CTL, val);
2156 
2157 	/* enable Admin queue Interrupt causes */
2158 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2159 	       PFINT_FW_CTL_CAUSE_ENA_M);
2160 	wr32(hw, PFINT_FW_CTL, val);
2161 
2162 	/* enable Mailbox queue Interrupt causes */
2163 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
2164 	       PFINT_MBX_CTL_CAUSE_ENA_M);
2165 	wr32(hw, PFINT_MBX_CTL, val);
2166 
2167 	ice_flush(hw);
2168 }
2169 
2170 /**
2171  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2172  * @pf: board private structure
2173  *
2174  * This sets up the handler for MSIX 0, which is used to manage the
2175  * non-queue interrupts, e.g. AdminQ and errors. This is not used
2176  * when in MSI or Legacy interrupt mode.
2177  */
2178 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2179 {
2180 	struct ice_hw *hw = &pf->hw;
2181 	int oicr_idx, err = 0;
2182 
2183 	if (!pf->int_name[0])
2184 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2185 			 dev_driver_string(&pf->pdev->dev),
2186 			 dev_name(&pf->pdev->dev));
2187 
2188 	/* Do not request IRQ but do enable OICR interrupt since settings are
2189 	 * lost during reset. Note that this function is called only during
2190 	 * rebuild path and not while reset is in progress.
2191 	 */
2192 	if (ice_is_reset_in_progress(pf->state))
2193 		goto skip_req_irq;
2194 
2195 	/* reserve one vector in irq_tracker for misc interrupts */
2196 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2197 	if (oicr_idx < 0)
2198 		return oicr_idx;
2199 
2200 	pf->num_avail_sw_msix -= 1;
2201 	pf->oicr_idx = oicr_idx;
2202 
2203 	err = devm_request_irq(&pf->pdev->dev,
2204 			       pf->msix_entries[pf->oicr_idx].vector,
2205 			       ice_misc_intr, 0, pf->int_name, pf);
2206 	if (err) {
2207 		dev_err(&pf->pdev->dev,
2208 			"devm_request_irq for %s failed: %d\n",
2209 			pf->int_name, err);
2210 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2211 		pf->num_avail_sw_msix += 1;
2212 		return err;
2213 	}
2214 
2215 skip_req_irq:
2216 	ice_ena_misc_vector(pf);
2217 
2218 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
2219 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2220 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
2221 
2222 	ice_flush(hw);
2223 	ice_irq_dynamic_ena(hw, NULL, NULL);
2224 
2225 	return 0;
2226 }
2227 
2228 /**
2229  * ice_napi_add - register NAPI handler for the VSI
2230  * @vsi: VSI for which NAPI handler is to be registered
2231  *
2232  * This function is only called in the driver's load path. Registering the NAPI
2233  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2234  * reset/rebuild, etc.)
2235  */
2236 static void ice_napi_add(struct ice_vsi *vsi)
2237 {
2238 	int v_idx;
2239 
2240 	if (!vsi->netdev)
2241 		return;
2242 
2243 	ice_for_each_q_vector(vsi, v_idx)
2244 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
2245 			       ice_napi_poll, NAPI_POLL_WEIGHT);
2246 }
2247 
2248 /**
2249  * ice_set_ops - set netdev and ethtools ops for the given netdev
2250  * @netdev: netdev instance
2251  */
2252 static void ice_set_ops(struct net_device *netdev)
2253 {
2254 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2255 
2256 	if (ice_is_safe_mode(pf)) {
2257 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
2258 		ice_set_ethtool_safe_mode_ops(netdev);
2259 		return;
2260 	}
2261 
2262 	netdev->netdev_ops = &ice_netdev_ops;
2263 	ice_set_ethtool_ops(netdev);
2264 }
2265 
2266 /**
2267  * ice_set_netdev_features - set features for the given netdev
2268  * @netdev: netdev instance
2269  */
2270 static void ice_set_netdev_features(struct net_device *netdev)
2271 {
2272 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2273 	netdev_features_t csumo_features;
2274 	netdev_features_t vlano_features;
2275 	netdev_features_t dflt_features;
2276 	netdev_features_t tso_features;
2277 
2278 	if (ice_is_safe_mode(pf)) {
2279 		/* safe mode */
2280 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
2281 		netdev->hw_features = netdev->features;
2282 		return;
2283 	}
2284 
2285 	dflt_features = NETIF_F_SG	|
2286 			NETIF_F_HIGHDMA	|
2287 			NETIF_F_RXHASH;
2288 
2289 	csumo_features = NETIF_F_RXCSUM	  |
2290 			 NETIF_F_IP_CSUM  |
2291 			 NETIF_F_SCTP_CRC |
2292 			 NETIF_F_IPV6_CSUM;
2293 
2294 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2295 			 NETIF_F_HW_VLAN_CTAG_TX     |
2296 			 NETIF_F_HW_VLAN_CTAG_RX;
2297 
2298 	tso_features = NETIF_F_TSO;
2299 
2300 	/* set features that user can change */
2301 	netdev->hw_features = dflt_features | csumo_features |
2302 			      vlano_features | tso_features;
2303 
2304 	/* enable features */
2305 	netdev->features |= netdev->hw_features;
2306 	/* encap and VLAN devices inherit default, csumo and tso features */
2307 	netdev->hw_enc_features |= dflt_features | csumo_features |
2308 				   tso_features;
2309 	netdev->vlan_features |= dflt_features | csumo_features |
2310 				 tso_features;
2311 }
2312 
2313 /**
2314  * ice_cfg_netdev - Allocate, configure and register a netdev
2315  * @vsi: the VSI associated with the new netdev
2316  *
2317  * Returns 0 on success, negative value on failure
2318  */
2319 static int ice_cfg_netdev(struct ice_vsi *vsi)
2320 {
2321 	struct ice_pf *pf = vsi->back;
2322 	struct ice_netdev_priv *np;
2323 	struct net_device *netdev;
2324 	u8 mac_addr[ETH_ALEN];
2325 	int err;
2326 
2327 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
2328 				    vsi->alloc_rxq);
2329 	if (!netdev)
2330 		return -ENOMEM;
2331 
2332 	vsi->netdev = netdev;
2333 	np = netdev_priv(netdev);
2334 	np->vsi = vsi;
2335 
2336 	ice_set_netdev_features(netdev);
2337 
2338 	ice_set_ops(netdev);
2339 
2340 	if (vsi->type == ICE_VSI_PF) {
2341 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
2342 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2343 		ether_addr_copy(netdev->dev_addr, mac_addr);
2344 		ether_addr_copy(netdev->perm_addr, mac_addr);
2345 	}
2346 
2347 	netdev->priv_flags |= IFF_UNICAST_FLT;
2348 
2349 	/* Setup netdev TC information */
2350 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
2351 
2352 	/* setup watchdog timeout value to be 5 second */
2353 	netdev->watchdog_timeo = 5 * HZ;
2354 
2355 	netdev->min_mtu = ETH_MIN_MTU;
2356 	netdev->max_mtu = ICE_MAX_MTU;
2357 
2358 	err = register_netdev(vsi->netdev);
2359 	if (err)
2360 		return err;
2361 
2362 	netif_carrier_off(vsi->netdev);
2363 
2364 	/* make sure transmit queues start off as stopped */
2365 	netif_tx_stop_all_queues(vsi->netdev);
2366 
2367 	return 0;
2368 }
2369 
2370 /**
2371  * ice_fill_rss_lut - Fill the RSS lookup table with default values
2372  * @lut: Lookup table
2373  * @rss_table_size: Lookup table size
2374  * @rss_size: Range of queue number for hashing
2375  */
2376 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2377 {
2378 	u16 i;
2379 
2380 	for (i = 0; i < rss_table_size; i++)
2381 		lut[i] = i % rss_size;
2382 }
2383 
2384 /**
2385  * ice_pf_vsi_setup - Set up a PF VSI
2386  * @pf: board private structure
2387  * @pi: pointer to the port_info instance
2388  *
2389  * Returns pointer to the successfully allocated VSI software struct
2390  * on success, otherwise returns NULL on failure.
2391  */
2392 static struct ice_vsi *
2393 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2394 {
2395 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
2396 }
2397 
2398 /**
2399  * ice_lb_vsi_setup - Set up a loopback VSI
2400  * @pf: board private structure
2401  * @pi: pointer to the port_info instance
2402  *
2403  * Returns pointer to the successfully allocated VSI software struct
2404  * on success, otherwise returns NULL on failure.
2405  */
2406 struct ice_vsi *
2407 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2408 {
2409 	return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
2410 }
2411 
2412 /**
2413  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
2414  * @netdev: network interface to be adjusted
2415  * @proto: unused protocol
2416  * @vid: VLAN ID to be added
2417  *
2418  * net_device_ops implementation for adding VLAN IDs
2419  */
2420 static int
2421 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
2422 		    u16 vid)
2423 {
2424 	struct ice_netdev_priv *np = netdev_priv(netdev);
2425 	struct ice_vsi *vsi = np->vsi;
2426 	int ret;
2427 
2428 	if (vid >= VLAN_N_VID) {
2429 		netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2430 			   vid, VLAN_N_VID);
2431 		return -EINVAL;
2432 	}
2433 
2434 	if (vsi->info.pvid)
2435 		return -EINVAL;
2436 
2437 	/* Enable VLAN pruning when VLAN 0 is added */
2438 	if (unlikely(!vid)) {
2439 		ret = ice_cfg_vlan_pruning(vsi, true, false);
2440 		if (ret)
2441 			return ret;
2442 	}
2443 
2444 	/* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is
2445 	 * needed to continue allowing all untagged packets since VLAN prune
2446 	 * list is applied to all packets by the switch
2447 	 */
2448 	ret = ice_vsi_add_vlan(vsi, vid);
2449 	if (!ret) {
2450 		vsi->vlan_ena = true;
2451 		set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2452 	}
2453 
2454 	return ret;
2455 }
2456 
2457 /**
2458  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
2459  * @netdev: network interface to be adjusted
2460  * @proto: unused protocol
2461  * @vid: VLAN ID to be removed
2462  *
2463  * net_device_ops implementation for removing VLAN IDs
2464  */
2465 static int
2466 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
2467 		     u16 vid)
2468 {
2469 	struct ice_netdev_priv *np = netdev_priv(netdev);
2470 	struct ice_vsi *vsi = np->vsi;
2471 	int ret;
2472 
2473 	if (vsi->info.pvid)
2474 		return -EINVAL;
2475 
2476 	/* Make sure ice_vsi_kill_vlan is successful before updating VLAN
2477 	 * information
2478 	 */
2479 	ret = ice_vsi_kill_vlan(vsi, vid);
2480 	if (ret)
2481 		return ret;
2482 
2483 	/* Disable VLAN pruning when VLAN 0 is removed */
2484 	if (unlikely(!vid))
2485 		ret = ice_cfg_vlan_pruning(vsi, false, false);
2486 
2487 	vsi->vlan_ena = false;
2488 	set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2489 	return ret;
2490 }
2491 
2492 /**
2493  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
2494  * @pf: board private structure
2495  *
2496  * Returns 0 on success, negative value on failure
2497  */
2498 static int ice_setup_pf_sw(struct ice_pf *pf)
2499 {
2500 	struct ice_vsi *vsi;
2501 	int status = 0;
2502 
2503 	if (ice_is_reset_in_progress(pf->state))
2504 		return -EBUSY;
2505 
2506 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
2507 	if (!vsi) {
2508 		status = -ENOMEM;
2509 		goto unroll_vsi_setup;
2510 	}
2511 
2512 	status = ice_cfg_netdev(vsi);
2513 	if (status) {
2514 		status = -ENODEV;
2515 		goto unroll_vsi_setup;
2516 	}
2517 	/* netdev has to be configured before setting frame size */
2518 	ice_vsi_cfg_frame_size(vsi);
2519 
2520 	/* Setup DCB netlink interface */
2521 	ice_dcbnl_setup(vsi);
2522 
2523 	/* registering the NAPI handler requires both the queues and
2524 	 * netdev to be created, which are done in ice_pf_vsi_setup()
2525 	 * and ice_cfg_netdev() respectively
2526 	 */
2527 	ice_napi_add(vsi);
2528 
2529 	status = ice_init_mac_fltr(pf);
2530 	if (status)
2531 		goto unroll_napi_add;
2532 
2533 	return status;
2534 
2535 unroll_napi_add:
2536 	if (vsi) {
2537 		ice_napi_del(vsi);
2538 		if (vsi->netdev) {
2539 			if (vsi->netdev->reg_state == NETREG_REGISTERED)
2540 				unregister_netdev(vsi->netdev);
2541 			free_netdev(vsi->netdev);
2542 			vsi->netdev = NULL;
2543 		}
2544 	}
2545 
2546 unroll_vsi_setup:
2547 	if (vsi) {
2548 		ice_vsi_free_q_vectors(vsi);
2549 		ice_vsi_delete(vsi);
2550 		ice_vsi_put_qs(vsi);
2551 		ice_vsi_clear(vsi);
2552 	}
2553 	return status;
2554 }
2555 
2556 /**
2557  * ice_get_avail_q_count - Get count of queues in use
2558  * @pf_qmap: bitmap to get queue use count from
2559  * @lock: pointer to a mutex that protects access to pf_qmap
2560  * @size: size of the bitmap
2561  */
2562 static u16
2563 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
2564 {
2565 	u16 count = 0, bit;
2566 
2567 	mutex_lock(lock);
2568 	for_each_clear_bit(bit, pf_qmap, size)
2569 		count++;
2570 	mutex_unlock(lock);
2571 
2572 	return count;
2573 }
2574 
2575 /**
2576  * ice_get_avail_txq_count - Get count of Tx queues in use
2577  * @pf: pointer to an ice_pf instance
2578  */
2579 u16 ice_get_avail_txq_count(struct ice_pf *pf)
2580 {
2581 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
2582 				     pf->max_pf_txqs);
2583 }
2584 
2585 /**
2586  * ice_get_avail_rxq_count - Get count of Rx queues in use
2587  * @pf: pointer to an ice_pf instance
2588  */
2589 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
2590 {
2591 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
2592 				     pf->max_pf_rxqs);
2593 }
2594 
2595 /**
2596  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
2597  * @pf: board private structure to initialize
2598  */
2599 static void ice_deinit_pf(struct ice_pf *pf)
2600 {
2601 	ice_service_task_stop(pf);
2602 	mutex_destroy(&pf->sw_mutex);
2603 	mutex_destroy(&pf->tc_mutex);
2604 	mutex_destroy(&pf->avail_q_mutex);
2605 
2606 	if (pf->avail_txqs) {
2607 		bitmap_free(pf->avail_txqs);
2608 		pf->avail_txqs = NULL;
2609 	}
2610 
2611 	if (pf->avail_rxqs) {
2612 		bitmap_free(pf->avail_rxqs);
2613 		pf->avail_rxqs = NULL;
2614 	}
2615 }
2616 
2617 /**
2618  * ice_set_pf_caps - set PFs capability flags
2619  * @pf: pointer to the PF instance
2620  */
2621 static void ice_set_pf_caps(struct ice_pf *pf)
2622 {
2623 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
2624 
2625 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2626 	if (func_caps->common_cap.dcb)
2627 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2628 #ifdef CONFIG_PCI_IOV
2629 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
2630 	if (func_caps->common_cap.sr_iov_1_1) {
2631 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
2632 		pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
2633 					      ICE_MAX_VF_COUNT);
2634 	}
2635 #endif /* CONFIG_PCI_IOV */
2636 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
2637 	if (func_caps->common_cap.rss_table_size)
2638 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
2639 
2640 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
2641 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
2642 }
2643 
2644 /**
2645  * ice_init_pf - Initialize general software structures (struct ice_pf)
2646  * @pf: board private structure to initialize
2647  */
2648 static int ice_init_pf(struct ice_pf *pf)
2649 {
2650 	ice_set_pf_caps(pf);
2651 
2652 	mutex_init(&pf->sw_mutex);
2653 	mutex_init(&pf->tc_mutex);
2654 
2655 	/* setup service timer and periodic service task */
2656 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
2657 	pf->serv_tmr_period = HZ;
2658 	INIT_WORK(&pf->serv_task, ice_service_task);
2659 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
2660 
2661 	mutex_init(&pf->avail_q_mutex);
2662 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
2663 	if (!pf->avail_txqs)
2664 		return -ENOMEM;
2665 
2666 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
2667 	if (!pf->avail_rxqs) {
2668 		devm_kfree(&pf->pdev->dev, pf->avail_txqs);
2669 		pf->avail_txqs = NULL;
2670 		return -ENOMEM;
2671 	}
2672 
2673 	return 0;
2674 }
2675 
2676 /**
2677  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
2678  * @pf: board private structure
2679  *
2680  * compute the number of MSIX vectors required (v_budget) and request from
2681  * the OS. Return the number of vectors reserved or negative on failure
2682  */
2683 static int ice_ena_msix_range(struct ice_pf *pf)
2684 {
2685 	int v_left, v_actual, v_budget = 0;
2686 	int needed, err, i;
2687 
2688 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
2689 
2690 	/* reserve one vector for miscellaneous handler */
2691 	needed = 1;
2692 	if (v_left < needed)
2693 		goto no_hw_vecs_left_err;
2694 	v_budget += needed;
2695 	v_left -= needed;
2696 
2697 	/* reserve vectors for LAN traffic */
2698 	needed = min_t(int, num_online_cpus(), v_left);
2699 	if (v_left < needed)
2700 		goto no_hw_vecs_left_err;
2701 	pf->num_lan_msix = needed;
2702 	v_budget += needed;
2703 	v_left -= needed;
2704 
2705 	pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
2706 					sizeof(*pf->msix_entries), GFP_KERNEL);
2707 
2708 	if (!pf->msix_entries) {
2709 		err = -ENOMEM;
2710 		goto exit_err;
2711 	}
2712 
2713 	for (i = 0; i < v_budget; i++)
2714 		pf->msix_entries[i].entry = i;
2715 
2716 	/* actually reserve the vectors */
2717 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
2718 					 ICE_MIN_MSIX, v_budget);
2719 
2720 	if (v_actual < 0) {
2721 		dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
2722 		err = v_actual;
2723 		goto msix_err;
2724 	}
2725 
2726 	if (v_actual < v_budget) {
2727 		dev_warn(&pf->pdev->dev,
2728 			 "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
2729 			 v_budget, v_actual);
2730 /* 2 vectors for LAN (traffic + OICR) */
2731 #define ICE_MIN_LAN_VECS 2
2732 
2733 		if (v_actual < ICE_MIN_LAN_VECS) {
2734 			/* error if we can't get minimum vectors */
2735 			pci_disable_msix(pf->pdev);
2736 			err = -ERANGE;
2737 			goto msix_err;
2738 		} else {
2739 			pf->num_lan_msix = ICE_MIN_LAN_VECS;
2740 		}
2741 	}
2742 
2743 	return v_actual;
2744 
2745 msix_err:
2746 	devm_kfree(&pf->pdev->dev, pf->msix_entries);
2747 	goto exit_err;
2748 
2749 no_hw_vecs_left_err:
2750 	dev_err(&pf->pdev->dev,
2751 		"not enough device MSI-X vectors. requested = %d, available = %d\n",
2752 		needed, v_left);
2753 	err = -ERANGE;
2754 exit_err:
2755 	pf->num_lan_msix = 0;
2756 	return err;
2757 }
2758 
2759 /**
2760  * ice_dis_msix - Disable MSI-X interrupt setup in OS
2761  * @pf: board private structure
2762  */
2763 static void ice_dis_msix(struct ice_pf *pf)
2764 {
2765 	pci_disable_msix(pf->pdev);
2766 	devm_kfree(&pf->pdev->dev, pf->msix_entries);
2767 	pf->msix_entries = NULL;
2768 }
2769 
2770 /**
2771  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
2772  * @pf: board private structure
2773  */
2774 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
2775 {
2776 	ice_dis_msix(pf);
2777 
2778 	if (pf->irq_tracker) {
2779 		devm_kfree(&pf->pdev->dev, pf->irq_tracker);
2780 		pf->irq_tracker = NULL;
2781 	}
2782 }
2783 
2784 /**
2785  * ice_init_interrupt_scheme - Determine proper interrupt scheme
2786  * @pf: board private structure to initialize
2787  */
2788 static int ice_init_interrupt_scheme(struct ice_pf *pf)
2789 {
2790 	int vectors;
2791 
2792 	vectors = ice_ena_msix_range(pf);
2793 
2794 	if (vectors < 0)
2795 		return vectors;
2796 
2797 	/* set up vector assignment tracking */
2798 	pf->irq_tracker =
2799 		devm_kzalloc(&pf->pdev->dev, sizeof(*pf->irq_tracker) +
2800 			     (sizeof(u16) * vectors), GFP_KERNEL);
2801 	if (!pf->irq_tracker) {
2802 		ice_dis_msix(pf);
2803 		return -ENOMEM;
2804 	}
2805 
2806 	/* populate SW interrupts pool with number of OS granted IRQs. */
2807 	pf->num_avail_sw_msix = vectors;
2808 	pf->irq_tracker->num_entries = vectors;
2809 	pf->irq_tracker->end = pf->irq_tracker->num_entries;
2810 
2811 	return 0;
2812 }
2813 
2814 /**
2815  * ice_log_pkg_init - log result of DDP package load
2816  * @hw: pointer to hardware info
2817  * @status: status of package load
2818  */
2819 static void
2820 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status)
2821 {
2822 	struct ice_pf *pf = (struct ice_pf *)hw->back;
2823 	struct device *dev = &pf->pdev->dev;
2824 
2825 	switch (*status) {
2826 	case ICE_SUCCESS:
2827 		/* The package download AdminQ command returned success because
2828 		 * this download succeeded or ICE_ERR_AQ_NO_WORK since there is
2829 		 * already a package loaded on the device.
2830 		 */
2831 		if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
2832 		    hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
2833 		    hw->pkg_ver.update == hw->active_pkg_ver.update &&
2834 		    hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
2835 		    !memcmp(hw->pkg_name, hw->active_pkg_name,
2836 			    sizeof(hw->pkg_name))) {
2837 			if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST)
2838 				dev_info(dev,
2839 					 "DDP package already present on device: %s version %d.%d.%d.%d\n",
2840 					 hw->active_pkg_name,
2841 					 hw->active_pkg_ver.major,
2842 					 hw->active_pkg_ver.minor,
2843 					 hw->active_pkg_ver.update,
2844 					 hw->active_pkg_ver.draft);
2845 			else
2846 				dev_info(dev,
2847 					 "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
2848 					 hw->active_pkg_name,
2849 					 hw->active_pkg_ver.major,
2850 					 hw->active_pkg_ver.minor,
2851 					 hw->active_pkg_ver.update,
2852 					 hw->active_pkg_ver.draft);
2853 		} else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
2854 			   hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
2855 			dev_err(dev,
2856 				"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",
2857 				hw->active_pkg_name,
2858 				hw->active_pkg_ver.major,
2859 				hw->active_pkg_ver.minor,
2860 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
2861 			*status = ICE_ERR_NOT_SUPPORTED;
2862 		} else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
2863 			   hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
2864 			dev_info(dev,
2865 				 "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",
2866 				 hw->active_pkg_name,
2867 				 hw->active_pkg_ver.major,
2868 				 hw->active_pkg_ver.minor,
2869 				 hw->active_pkg_ver.update,
2870 				 hw->active_pkg_ver.draft,
2871 				 hw->pkg_name,
2872 				 hw->pkg_ver.major,
2873 				 hw->pkg_ver.minor,
2874 				 hw->pkg_ver.update,
2875 				 hw->pkg_ver.draft);
2876 		} else {
2877 			dev_err(dev,
2878 				"An unknown error occurred when loading the DDP package, please reboot the system.  If the problem persists, update the NVM.  Entering Safe Mode.\n");
2879 			*status = ICE_ERR_NOT_SUPPORTED;
2880 		}
2881 		break;
2882 	case ICE_ERR_BUF_TOO_SHORT:
2883 		/* fall-through */
2884 	case ICE_ERR_CFG:
2885 		dev_err(dev,
2886 			"The DDP package file is invalid. Entering Safe Mode.\n");
2887 		break;
2888 	case ICE_ERR_NOT_SUPPORTED:
2889 		/* Package File version not supported */
2890 		if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ ||
2891 		    (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
2892 		     hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR))
2893 			dev_err(dev,
2894 				"The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
2895 		else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ ||
2896 			 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
2897 			  hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR))
2898 			dev_err(dev,
2899 				"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",
2900 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
2901 		break;
2902 	case ICE_ERR_AQ_ERROR:
2903 		switch (hw->pkg_dwnld_status) {
2904 		case ICE_AQ_RC_ENOSEC:
2905 		case ICE_AQ_RC_EBADSIG:
2906 			dev_err(dev,
2907 				"The DDP package could not be loaded because its signature is not valid.  Please use a valid DDP Package.  Entering Safe Mode.\n");
2908 			return;
2909 		case ICE_AQ_RC_ESVN:
2910 			dev_err(dev,
2911 				"The DDP Package could not be loaded because its security revision is too low.  Please use an updated DDP Package.  Entering Safe Mode.\n");
2912 			return;
2913 		case ICE_AQ_RC_EBADMAN:
2914 		case ICE_AQ_RC_EBADBUF:
2915 			dev_err(dev,
2916 				"An error occurred on the device while loading the DDP package.  The device will be reset.\n");
2917 			return;
2918 		default:
2919 			break;
2920 		}
2921 		/* fall-through */
2922 	default:
2923 		dev_err(dev,
2924 			"An unknown error (%d) occurred when loading the DDP package.  Entering Safe Mode.\n",
2925 			*status);
2926 		break;
2927 	}
2928 }
2929 
2930 /**
2931  * ice_load_pkg - load/reload the DDP Package file
2932  * @firmware: firmware structure when firmware requested or NULL for reload
2933  * @pf: pointer to the PF instance
2934  *
2935  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
2936  * initialize HW tables.
2937  */
2938 static void
2939 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
2940 {
2941 	enum ice_status status = ICE_ERR_PARAM;
2942 	struct device *dev = &pf->pdev->dev;
2943 	struct ice_hw *hw = &pf->hw;
2944 
2945 	/* Load DDP Package */
2946 	if (firmware && !hw->pkg_copy) {
2947 		status = ice_copy_and_init_pkg(hw, firmware->data,
2948 					       firmware->size);
2949 		ice_log_pkg_init(hw, &status);
2950 	} else if (!firmware && hw->pkg_copy) {
2951 		/* Reload package during rebuild after CORER/GLOBR reset */
2952 		status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
2953 		ice_log_pkg_init(hw, &status);
2954 	} else {
2955 		dev_err(dev,
2956 			"The DDP package file failed to load. Entering Safe Mode.\n");
2957 	}
2958 
2959 	if (status) {
2960 		/* Safe Mode */
2961 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
2962 		return;
2963 	}
2964 
2965 	/* Successful download package is the precondition for advanced
2966 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
2967 	 */
2968 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
2969 }
2970 
2971 /**
2972  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
2973  * @pf: pointer to the PF structure
2974  *
2975  * There is no error returned here because the driver should be able to handle
2976  * 128 Byte cache lines, so we only print a warning in case issues are seen,
2977  * specifically with Tx.
2978  */
2979 static void ice_verify_cacheline_size(struct ice_pf *pf)
2980 {
2981 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
2982 		dev_warn(&pf->pdev->dev,
2983 			 "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
2984 			 ICE_CACHE_LINE_BYTES);
2985 }
2986 
2987 /**
2988  * ice_send_version - update firmware with driver version
2989  * @pf: PF struct
2990  *
2991  * Returns ICE_SUCCESS on success, else error code
2992  */
2993 static enum ice_status ice_send_version(struct ice_pf *pf)
2994 {
2995 	struct ice_driver_ver dv;
2996 
2997 	dv.major_ver = DRV_VERSION_MAJOR;
2998 	dv.minor_ver = DRV_VERSION_MINOR;
2999 	dv.build_ver = DRV_VERSION_BUILD;
3000 	dv.subbuild_ver = 0;
3001 	strscpy((char *)dv.driver_string, DRV_VERSION,
3002 		sizeof(dv.driver_string));
3003 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
3004 }
3005 
3006 /**
3007  * ice_get_opt_fw_name - return optional firmware file name or NULL
3008  * @pf: pointer to the PF instance
3009  */
3010 static char *ice_get_opt_fw_name(struct ice_pf *pf)
3011 {
3012 	/* Optional firmware name same as default with additional dash
3013 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
3014 	 */
3015 	struct pci_dev *pdev = pf->pdev;
3016 	char *opt_fw_filename = NULL;
3017 	u32 dword;
3018 	u8 dsn[8];
3019 	int pos;
3020 
3021 	/* Determine the name of the optional file using the DSN (two
3022 	 * dwords following the start of the DSN Capability).
3023 	 */
3024 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
3025 	if (pos) {
3026 		opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
3027 		if (!opt_fw_filename)
3028 			return NULL;
3029 
3030 		pci_read_config_dword(pdev, pos + 4, &dword);
3031 		put_unaligned_le32(dword, &dsn[0]);
3032 		pci_read_config_dword(pdev, pos + 8, &dword);
3033 		put_unaligned_le32(dword, &dsn[4]);
3034 		snprintf(opt_fw_filename, NAME_MAX,
3035 			 "%sice-%02x%02x%02x%02x%02x%02x%02x%02x.pkg",
3036 			 ICE_DDP_PKG_PATH,
3037 			 dsn[7], dsn[6], dsn[5], dsn[4],
3038 			 dsn[3], dsn[2], dsn[1], dsn[0]);
3039 	}
3040 
3041 	return opt_fw_filename;
3042 }
3043 
3044 /**
3045  * ice_request_fw - Device initialization routine
3046  * @pf: pointer to the PF instance
3047  */
3048 static void ice_request_fw(struct ice_pf *pf)
3049 {
3050 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
3051 	const struct firmware *firmware = NULL;
3052 	struct device *dev = &pf->pdev->dev;
3053 	int err = 0;
3054 
3055 	/* optional device-specific DDP (if present) overrides the default DDP
3056 	 * package file. kernel logs a debug message if the file doesn't exist,
3057 	 * and warning messages for other errors.
3058 	 */
3059 	if (opt_fw_filename) {
3060 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
3061 		if (err) {
3062 			kfree(opt_fw_filename);
3063 			goto dflt_pkg_load;
3064 		}
3065 
3066 		/* request for firmware was successful. Download to device */
3067 		ice_load_pkg(firmware, pf);
3068 		kfree(opt_fw_filename);
3069 		release_firmware(firmware);
3070 		return;
3071 	}
3072 
3073 dflt_pkg_load:
3074 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
3075 	if (err) {
3076 		dev_err(dev,
3077 			"The DDP package file was not found or could not be read. Entering Safe Mode\n");
3078 		return;
3079 	}
3080 
3081 	/* request for firmware was successful. Download to device */
3082 	ice_load_pkg(firmware, pf);
3083 	release_firmware(firmware);
3084 }
3085 
3086 /**
3087  * ice_probe - Device initialization routine
3088  * @pdev: PCI device information struct
3089  * @ent: entry in ice_pci_tbl
3090  *
3091  * Returns 0 on success, negative on failure
3092  */
3093 static int
3094 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
3095 {
3096 	struct device *dev = &pdev->dev;
3097 	struct ice_pf *pf;
3098 	struct ice_hw *hw;
3099 	int err;
3100 
3101 	/* this driver uses devres, see Documentation/driver-api/driver-model/devres.rst */
3102 	err = pcim_enable_device(pdev);
3103 	if (err)
3104 		return err;
3105 
3106 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3107 	if (err) {
3108 		dev_err(dev, "BAR0 I/O map error %d\n", err);
3109 		return err;
3110 	}
3111 
3112 	pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL);
3113 	if (!pf)
3114 		return -ENOMEM;
3115 
3116 	/* set up for high or low DMA */
3117 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3118 	if (err)
3119 		err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3120 	if (err) {
3121 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
3122 		return err;
3123 	}
3124 
3125 	pci_enable_pcie_error_reporting(pdev);
3126 	pci_set_master(pdev);
3127 
3128 	pf->pdev = pdev;
3129 	pci_set_drvdata(pdev, pf);
3130 	set_bit(__ICE_DOWN, pf->state);
3131 	/* Disable service task until DOWN bit is cleared */
3132 	set_bit(__ICE_SERVICE_DIS, pf->state);
3133 
3134 	hw = &pf->hw;
3135 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3136 	pci_save_state(pdev);
3137 
3138 	hw->back = pf;
3139 	hw->vendor_id = pdev->vendor;
3140 	hw->device_id = pdev->device;
3141 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3142 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3143 	hw->subsystem_device_id = pdev->subsystem_device;
3144 	hw->bus.device = PCI_SLOT(pdev->devfn);
3145 	hw->bus.func = PCI_FUNC(pdev->devfn);
3146 	ice_set_ctrlq_len(hw);
3147 
3148 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3149 
3150 #ifndef CONFIG_DYNAMIC_DEBUG
3151 	if (debug < -1)
3152 		hw->debug_mask = debug;
3153 #endif
3154 
3155 	err = ice_init_hw(hw);
3156 	if (err) {
3157 		dev_err(dev, "ice_init_hw failed: %d\n", err);
3158 		err = -EIO;
3159 		goto err_exit_unroll;
3160 	}
3161 
3162 	dev_info(dev, "firmware %d.%d.%d api %d.%d.%d nvm %s build 0x%08x\n",
3163 		 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
3164 		 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
3165 		 ice_nvm_version_str(hw), hw->fw_build);
3166 
3167 	ice_request_fw(pf);
3168 
3169 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
3170 	 * set in pf->state, which will cause ice_is_safe_mode to return
3171 	 * true
3172 	 */
3173 	if (ice_is_safe_mode(pf)) {
3174 		dev_err(dev,
3175 			"Package download failed. Advanced features disabled - Device now in Safe Mode\n");
3176 		/* we already got function/device capabilities but these don't
3177 		 * reflect what the driver needs to do in safe mode. Instead of
3178 		 * adding conditional logic everywhere to ignore these
3179 		 * device/function capabilities, override them.
3180 		 */
3181 		ice_set_safe_mode_caps(hw);
3182 	}
3183 
3184 	err = ice_init_pf(pf);
3185 	if (err) {
3186 		dev_err(dev, "ice_init_pf failed: %d\n", err);
3187 		goto err_init_pf_unroll;
3188 	}
3189 
3190 	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
3191 	if (!pf->num_alloc_vsi) {
3192 		err = -EIO;
3193 		goto err_init_pf_unroll;
3194 	}
3195 
3196 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
3197 			       GFP_KERNEL);
3198 	if (!pf->vsi) {
3199 		err = -ENOMEM;
3200 		goto err_init_pf_unroll;
3201 	}
3202 
3203 	err = ice_init_interrupt_scheme(pf);
3204 	if (err) {
3205 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
3206 		err = -EIO;
3207 		goto err_init_interrupt_unroll;
3208 	}
3209 
3210 	/* Driver is mostly up */
3211 	clear_bit(__ICE_DOWN, pf->state);
3212 
3213 	/* In case of MSIX we are going to setup the misc vector right here
3214 	 * to handle admin queue events etc. In case of legacy and MSI
3215 	 * the misc functionality and queue processing is combined in
3216 	 * the same vector and that gets setup at open.
3217 	 */
3218 	err = ice_req_irq_msix_misc(pf);
3219 	if (err) {
3220 		dev_err(dev, "setup of misc vector failed: %d\n", err);
3221 		goto err_init_interrupt_unroll;
3222 	}
3223 
3224 	/* create switch struct for the switch element created by FW on boot */
3225 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
3226 	if (!pf->first_sw) {
3227 		err = -ENOMEM;
3228 		goto err_msix_misc_unroll;
3229 	}
3230 
3231 	if (hw->evb_veb)
3232 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3233 	else
3234 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
3235 
3236 	pf->first_sw->pf = pf;
3237 
3238 	/* record the sw_id available for later use */
3239 	pf->first_sw->sw_id = hw->port_info->sw_id;
3240 
3241 	err = ice_setup_pf_sw(pf);
3242 	if (err) {
3243 		dev_err(dev, "probe failed due to setup PF switch:%d\n", err);
3244 		goto err_alloc_sw_unroll;
3245 	}
3246 
3247 	clear_bit(__ICE_SERVICE_DIS, pf->state);
3248 
3249 	/* tell the firmware we are up */
3250 	err = ice_send_version(pf);
3251 	if (err) {
3252 		dev_err(dev,
3253 			"probe failed sending driver version %s. error: %d\n",
3254 			ice_drv_ver, err);
3255 		goto err_alloc_sw_unroll;
3256 	}
3257 
3258 	/* since everything is good, start the service timer */
3259 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3260 
3261 	err = ice_init_link_events(pf->hw.port_info);
3262 	if (err) {
3263 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
3264 		goto err_alloc_sw_unroll;
3265 	}
3266 
3267 	ice_verify_cacheline_size(pf);
3268 
3269 	/* If no DDP driven features have to be setup, return here */
3270 	if (ice_is_safe_mode(pf))
3271 		return 0;
3272 
3273 	/* initialize DDP driven features */
3274 
3275 	/* Note: DCB init failure is non-fatal to load */
3276 	if (ice_init_pf_dcb(pf, false)) {
3277 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3278 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
3279 	} else {
3280 		ice_cfg_lldp_mib_change(&pf->hw, true);
3281 	}
3282 
3283 	/* print PCI link speed and width */
3284 	pcie_print_link_status(pf->pdev);
3285 
3286 	return 0;
3287 
3288 err_alloc_sw_unroll:
3289 	set_bit(__ICE_SERVICE_DIS, pf->state);
3290 	set_bit(__ICE_DOWN, pf->state);
3291 	devm_kfree(&pf->pdev->dev, pf->first_sw);
3292 err_msix_misc_unroll:
3293 	ice_free_irq_msix_misc(pf);
3294 err_init_interrupt_unroll:
3295 	ice_clear_interrupt_scheme(pf);
3296 	devm_kfree(dev, pf->vsi);
3297 err_init_pf_unroll:
3298 	ice_deinit_pf(pf);
3299 	ice_deinit_hw(hw);
3300 err_exit_unroll:
3301 	pci_disable_pcie_error_reporting(pdev);
3302 	return err;
3303 }
3304 
3305 /**
3306  * ice_remove - Device removal routine
3307  * @pdev: PCI device information struct
3308  */
3309 static void ice_remove(struct pci_dev *pdev)
3310 {
3311 	struct ice_pf *pf = pci_get_drvdata(pdev);
3312 	int i;
3313 
3314 	if (!pf)
3315 		return;
3316 
3317 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
3318 		if (!ice_is_reset_in_progress(pf->state))
3319 			break;
3320 		msleep(100);
3321 	}
3322 
3323 	set_bit(__ICE_DOWN, pf->state);
3324 	ice_service_task_stop(pf);
3325 
3326 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
3327 		ice_free_vfs(pf);
3328 	ice_vsi_release_all(pf);
3329 	ice_free_irq_msix_misc(pf);
3330 	ice_for_each_vsi(pf, i) {
3331 		if (!pf->vsi[i])
3332 			continue;
3333 		ice_vsi_free_q_vectors(pf->vsi[i]);
3334 	}
3335 	ice_deinit_pf(pf);
3336 	ice_deinit_hw(&pf->hw);
3337 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
3338 	 * do it via ice_schedule_reset() since there is no need to rebuild
3339 	 * and the service task is already stopped.
3340 	 */
3341 	ice_reset(&pf->hw, ICE_RESET_PFR);
3342 	pci_wait_for_pending_transaction(pdev);
3343 	ice_clear_interrupt_scheme(pf);
3344 	pci_disable_pcie_error_reporting(pdev);
3345 }
3346 
3347 /**
3348  * ice_pci_err_detected - warning that PCI error has been detected
3349  * @pdev: PCI device information struct
3350  * @err: the type of PCI error
3351  *
3352  * Called to warn that something happened on the PCI bus and the error handling
3353  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
3354  */
3355 static pci_ers_result_t
3356 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err)
3357 {
3358 	struct ice_pf *pf = pci_get_drvdata(pdev);
3359 
3360 	if (!pf) {
3361 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
3362 			__func__, err);
3363 		return PCI_ERS_RESULT_DISCONNECT;
3364 	}
3365 
3366 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
3367 		ice_service_task_stop(pf);
3368 
3369 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
3370 			set_bit(__ICE_PFR_REQ, pf->state);
3371 			ice_prepare_for_reset(pf);
3372 		}
3373 	}
3374 
3375 	return PCI_ERS_RESULT_NEED_RESET;
3376 }
3377 
3378 /**
3379  * ice_pci_err_slot_reset - a PCI slot reset has just happened
3380  * @pdev: PCI device information struct
3381  *
3382  * Called to determine if the driver can recover from the PCI slot reset by
3383  * using a register read to determine if the device is recoverable.
3384  */
3385 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
3386 {
3387 	struct ice_pf *pf = pci_get_drvdata(pdev);
3388 	pci_ers_result_t result;
3389 	int err;
3390 	u32 reg;
3391 
3392 	err = pci_enable_device_mem(pdev);
3393 	if (err) {
3394 		dev_err(&pdev->dev,
3395 			"Cannot re-enable PCI device after reset, error %d\n",
3396 			err);
3397 		result = PCI_ERS_RESULT_DISCONNECT;
3398 	} else {
3399 		pci_set_master(pdev);
3400 		pci_restore_state(pdev);
3401 		pci_save_state(pdev);
3402 		pci_wake_from_d3(pdev, false);
3403 
3404 		/* Check for life */
3405 		reg = rd32(&pf->hw, GLGEN_RTRIG);
3406 		if (!reg)
3407 			result = PCI_ERS_RESULT_RECOVERED;
3408 		else
3409 			result = PCI_ERS_RESULT_DISCONNECT;
3410 	}
3411 
3412 	err = pci_cleanup_aer_uncorrect_error_status(pdev);
3413 	if (err)
3414 		dev_dbg(&pdev->dev,
3415 			"pci_cleanup_aer_uncorrect_error_status failed, error %d\n",
3416 			err);
3417 		/* non-fatal, continue */
3418 
3419 	return result;
3420 }
3421 
3422 /**
3423  * ice_pci_err_resume - restart operations after PCI error recovery
3424  * @pdev: PCI device information struct
3425  *
3426  * Called to allow the driver to bring things back up after PCI error and/or
3427  * reset recovery have finished
3428  */
3429 static void ice_pci_err_resume(struct pci_dev *pdev)
3430 {
3431 	struct ice_pf *pf = pci_get_drvdata(pdev);
3432 
3433 	if (!pf) {
3434 		dev_err(&pdev->dev,
3435 			"%s failed, device is unrecoverable\n", __func__);
3436 		return;
3437 	}
3438 
3439 	if (test_bit(__ICE_SUSPENDED, pf->state)) {
3440 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
3441 			__func__);
3442 		return;
3443 	}
3444 
3445 	ice_do_reset(pf, ICE_RESET_PFR);
3446 	ice_service_task_restart(pf);
3447 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3448 }
3449 
3450 /**
3451  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
3452  * @pdev: PCI device information struct
3453  */
3454 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
3455 {
3456 	struct ice_pf *pf = pci_get_drvdata(pdev);
3457 
3458 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
3459 		ice_service_task_stop(pf);
3460 
3461 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
3462 			set_bit(__ICE_PFR_REQ, pf->state);
3463 			ice_prepare_for_reset(pf);
3464 		}
3465 	}
3466 }
3467 
3468 /**
3469  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
3470  * @pdev: PCI device information struct
3471  */
3472 static void ice_pci_err_reset_done(struct pci_dev *pdev)
3473 {
3474 	ice_pci_err_resume(pdev);
3475 }
3476 
3477 /* ice_pci_tbl - PCI Device ID Table
3478  *
3479  * Wildcard entries (PCI_ANY_ID) should come last
3480  * Last entry must be all 0s
3481  *
3482  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3483  *   Class, Class Mask, private data (not used) }
3484  */
3485 static const struct pci_device_id ice_pci_tbl[] = {
3486 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
3487 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
3488 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
3489 	/* required last entry */
3490 	{ 0, }
3491 };
3492 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3493 
3494 static const struct pci_error_handlers ice_pci_err_handler = {
3495 	.error_detected = ice_pci_err_detected,
3496 	.slot_reset = ice_pci_err_slot_reset,
3497 	.reset_prepare = ice_pci_err_reset_prepare,
3498 	.reset_done = ice_pci_err_reset_done,
3499 	.resume = ice_pci_err_resume
3500 };
3501 
3502 static struct pci_driver ice_driver = {
3503 	.name = KBUILD_MODNAME,
3504 	.id_table = ice_pci_tbl,
3505 	.probe = ice_probe,
3506 	.remove = ice_remove,
3507 	.sriov_configure = ice_sriov_configure,
3508 	.err_handler = &ice_pci_err_handler
3509 };
3510 
3511 /**
3512  * ice_module_init - Driver registration routine
3513  *
3514  * ice_module_init is the first routine called when the driver is
3515  * loaded. All it does is register with the PCI subsystem.
3516  */
3517 static int __init ice_module_init(void)
3518 {
3519 	int status;
3520 
3521 	pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3522 	pr_info("%s\n", ice_copyright);
3523 
3524 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
3525 	if (!ice_wq) {
3526 		pr_err("Failed to create workqueue\n");
3527 		return -ENOMEM;
3528 	}
3529 
3530 	status = pci_register_driver(&ice_driver);
3531 	if (status) {
3532 		pr_err("failed to register PCI driver, err %d\n", status);
3533 		destroy_workqueue(ice_wq);
3534 	}
3535 
3536 	return status;
3537 }
3538 module_init(ice_module_init);
3539 
3540 /**
3541  * ice_module_exit - Driver exit cleanup routine
3542  *
3543  * ice_module_exit is called just before the driver is removed
3544  * from memory.
3545  */
3546 static void __exit ice_module_exit(void)
3547 {
3548 	pci_unregister_driver(&ice_driver);
3549 	destroy_workqueue(ice_wq);
3550 	pr_info("module unloaded\n");
3551 }
3552 module_exit(ice_module_exit);
3553 
3554 /**
3555  * ice_set_mac_address - NDO callback to set MAC address
3556  * @netdev: network interface device structure
3557  * @pi: pointer to an address structure
3558  *
3559  * Returns 0 on success, negative on failure
3560  */
3561 static int ice_set_mac_address(struct net_device *netdev, void *pi)
3562 {
3563 	struct ice_netdev_priv *np = netdev_priv(netdev);
3564 	struct ice_vsi *vsi = np->vsi;
3565 	struct ice_pf *pf = vsi->back;
3566 	struct ice_hw *hw = &pf->hw;
3567 	struct sockaddr *addr = pi;
3568 	enum ice_status status;
3569 	u8 flags = 0;
3570 	int err = 0;
3571 	u8 *mac;
3572 
3573 	mac = (u8 *)addr->sa_data;
3574 
3575 	if (!is_valid_ether_addr(mac))
3576 		return -EADDRNOTAVAIL;
3577 
3578 	if (ether_addr_equal(netdev->dev_addr, mac)) {
3579 		netdev_warn(netdev, "already using mac %pM\n", mac);
3580 		return 0;
3581 	}
3582 
3583 	if (test_bit(__ICE_DOWN, pf->state) ||
3584 	    ice_is_reset_in_progress(pf->state)) {
3585 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
3586 			   mac);
3587 		return -EBUSY;
3588 	}
3589 
3590 	/* When we change the MAC address we also have to change the MAC address
3591 	 * based filter rules that were created previously for the old MAC
3592 	 * address. So first, we remove the old filter rule using ice_remove_mac
3593 	 * and then create a new filter rule using ice_add_mac via
3594 	 * ice_vsi_cfg_mac_fltr function call for both add and/or remove
3595 	 * filters.
3596 	 */
3597 	status = ice_vsi_cfg_mac_fltr(vsi, netdev->dev_addr, false);
3598 	if (status) {
3599 		err = -EADDRNOTAVAIL;
3600 		goto err_update_filters;
3601 	}
3602 
3603 	status = ice_vsi_cfg_mac_fltr(vsi, mac, true);
3604 	if (status) {
3605 		err = -EADDRNOTAVAIL;
3606 		goto err_update_filters;
3607 	}
3608 
3609 err_update_filters:
3610 	if (err) {
3611 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
3612 			   mac);
3613 		return err;
3614 	}
3615 
3616 	/* change the netdev's MAC address */
3617 	memcpy(netdev->dev_addr, mac, netdev->addr_len);
3618 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
3619 		   netdev->dev_addr);
3620 
3621 	/* write new MAC address to the firmware */
3622 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3623 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3624 	if (status) {
3625 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
3626 			   mac, status);
3627 	}
3628 	return 0;
3629 }
3630 
3631 /**
3632  * ice_set_rx_mode - NDO callback to set the netdev filters
3633  * @netdev: network interface device structure
3634  */
3635 static void ice_set_rx_mode(struct net_device *netdev)
3636 {
3637 	struct ice_netdev_priv *np = netdev_priv(netdev);
3638 	struct ice_vsi *vsi = np->vsi;
3639 
3640 	if (!vsi)
3641 		return;
3642 
3643 	/* Set the flags to synchronize filters
3644 	 * ndo_set_rx_mode may be triggered even without a change in netdev
3645 	 * flags
3646 	 */
3647 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3648 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3649 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3650 
3651 	/* schedule our worker thread which will take care of
3652 	 * applying the new filter changes
3653 	 */
3654 	ice_service_task_schedule(vsi->back);
3655 }
3656 
3657 /**
3658  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
3659  * @netdev: network interface device structure
3660  * @queue_index: Queue ID
3661  * @maxrate: maximum bandwidth in Mbps
3662  */
3663 static int
3664 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
3665 {
3666 	struct ice_netdev_priv *np = netdev_priv(netdev);
3667 	struct ice_vsi *vsi = np->vsi;
3668 	enum ice_status status;
3669 	u16 q_handle;
3670 	u8 tc;
3671 
3672 	/* Validate maxrate requested is within permitted range */
3673 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
3674 		netdev_err(netdev,
3675 			   "Invalid max rate %d specified for the queue %d\n",
3676 			   maxrate, queue_index);
3677 		return -EINVAL;
3678 	}
3679 
3680 	q_handle = vsi->tx_rings[queue_index]->q_handle;
3681 	tc = ice_dcb_get_tc(vsi, queue_index);
3682 
3683 	/* Set BW back to default, when user set maxrate to 0 */
3684 	if (!maxrate)
3685 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
3686 					       q_handle, ICE_MAX_BW);
3687 	else
3688 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
3689 					  q_handle, ICE_MAX_BW, maxrate * 1000);
3690 	if (status) {
3691 		netdev_err(netdev,
3692 			   "Unable to set Tx max rate, error %d\n", status);
3693 		return -EIO;
3694 	}
3695 
3696 	return 0;
3697 }
3698 
3699 /**
3700  * ice_fdb_add - add an entry to the hardware database
3701  * @ndm: the input from the stack
3702  * @tb: pointer to array of nladdr (unused)
3703  * @dev: the net device pointer
3704  * @addr: the MAC address entry being added
3705  * @vid: VLAN ID
3706  * @flags: instructions from stack about fdb operation
3707  * @extack: netlink extended ack
3708  */
3709 static int
3710 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3711 	    struct net_device *dev, const unsigned char *addr, u16 vid,
3712 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
3713 {
3714 	int err;
3715 
3716 	if (vid) {
3717 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3718 		return -EINVAL;
3719 	}
3720 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3721 		netdev_err(dev, "FDB only supports static addresses\n");
3722 		return -EINVAL;
3723 	}
3724 
3725 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3726 		err = dev_uc_add_excl(dev, addr);
3727 	else if (is_multicast_ether_addr(addr))
3728 		err = dev_mc_add_excl(dev, addr);
3729 	else
3730 		err = -EINVAL;
3731 
3732 	/* Only return duplicate errors if NLM_F_EXCL is set */
3733 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3734 		err = 0;
3735 
3736 	return err;
3737 }
3738 
3739 /**
3740  * ice_fdb_del - delete an entry from the hardware database
3741  * @ndm: the input from the stack
3742  * @tb: pointer to array of nladdr (unused)
3743  * @dev: the net device pointer
3744  * @addr: the MAC address entry being added
3745  * @vid: VLAN ID
3746  */
3747 static int
3748 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
3749 	    struct net_device *dev, const unsigned char *addr,
3750 	    __always_unused u16 vid)
3751 {
3752 	int err;
3753 
3754 	if (ndm->ndm_state & NUD_PERMANENT) {
3755 		netdev_err(dev, "FDB only supports static addresses\n");
3756 		return -EINVAL;
3757 	}
3758 
3759 	if (is_unicast_ether_addr(addr))
3760 		err = dev_uc_del(dev, addr);
3761 	else if (is_multicast_ether_addr(addr))
3762 		err = dev_mc_del(dev, addr);
3763 	else
3764 		err = -EINVAL;
3765 
3766 	return err;
3767 }
3768 
3769 /**
3770  * ice_set_features - set the netdev feature flags
3771  * @netdev: ptr to the netdev being adjusted
3772  * @features: the feature set that the stack is suggesting
3773  */
3774 static int
3775 ice_set_features(struct net_device *netdev, netdev_features_t features)
3776 {
3777 	struct ice_netdev_priv *np = netdev_priv(netdev);
3778 	struct ice_vsi *vsi = np->vsi;
3779 	struct ice_pf *pf = vsi->back;
3780 	int ret = 0;
3781 
3782 	/* Don't set any netdev advanced features with device in Safe Mode */
3783 	if (ice_is_safe_mode(vsi->back)) {
3784 		dev_err(&vsi->back->pdev->dev,
3785 			"Device is in Safe Mode - not enabling advanced netdev features\n");
3786 		return ret;
3787 	}
3788 
3789 	/* Do not change setting during reset */
3790 	if (ice_is_reset_in_progress(pf->state)) {
3791 		dev_err(&vsi->back->pdev->dev,
3792 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
3793 		return -EBUSY;
3794 	}
3795 
3796 	/* Multiple features can be changed in one call so keep features in
3797 	 * separate if/else statements to guarantee each feature is checked
3798 	 */
3799 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
3800 		ret = ice_vsi_manage_rss_lut(vsi, true);
3801 	else if (!(features & NETIF_F_RXHASH) &&
3802 		 netdev->features & NETIF_F_RXHASH)
3803 		ret = ice_vsi_manage_rss_lut(vsi, false);
3804 
3805 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3806 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3807 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
3808 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3809 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3810 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
3811 
3812 	if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3813 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3814 		ret = ice_vsi_manage_vlan_insertion(vsi);
3815 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3816 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3817 		ret = ice_vsi_manage_vlan_insertion(vsi);
3818 
3819 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3820 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3821 		ret = ice_cfg_vlan_pruning(vsi, true, false);
3822 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3823 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3824 		ret = ice_cfg_vlan_pruning(vsi, false, false);
3825 
3826 	return ret;
3827 }
3828 
3829 /**
3830  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
3831  * @vsi: VSI to setup VLAN properties for
3832  */
3833 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3834 {
3835 	int ret = 0;
3836 
3837 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3838 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
3839 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3840 		ret = ice_vsi_manage_vlan_insertion(vsi);
3841 
3842 	return ret;
3843 }
3844 
3845 /**
3846  * ice_vsi_cfg - Setup the VSI
3847  * @vsi: the VSI being configured
3848  *
3849  * Return 0 on success and negative value on error
3850  */
3851 int ice_vsi_cfg(struct ice_vsi *vsi)
3852 {
3853 	int err;
3854 
3855 	if (vsi->netdev) {
3856 		ice_set_rx_mode(vsi->netdev);
3857 
3858 		err = ice_vsi_vlan_setup(vsi);
3859 
3860 		if (err)
3861 			return err;
3862 	}
3863 	ice_vsi_cfg_dcb_rings(vsi);
3864 
3865 	err = ice_vsi_cfg_lan_txqs(vsi);
3866 	if (!err && ice_is_xdp_ena_vsi(vsi))
3867 		err = ice_vsi_cfg_xdp_txqs(vsi);
3868 	if (!err)
3869 		err = ice_vsi_cfg_rxqs(vsi);
3870 
3871 	return err;
3872 }
3873 
3874 /**
3875  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3876  * @vsi: the VSI being configured
3877  */
3878 static void ice_napi_enable_all(struct ice_vsi *vsi)
3879 {
3880 	int q_idx;
3881 
3882 	if (!vsi->netdev)
3883 		return;
3884 
3885 	ice_for_each_q_vector(vsi, q_idx) {
3886 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3887 
3888 		if (q_vector->rx.ring || q_vector->tx.ring)
3889 			napi_enable(&q_vector->napi);
3890 	}
3891 }
3892 
3893 /**
3894  * ice_up_complete - Finish the last steps of bringing up a connection
3895  * @vsi: The VSI being configured
3896  *
3897  * Return 0 on success and negative value on error
3898  */
3899 static int ice_up_complete(struct ice_vsi *vsi)
3900 {
3901 	struct ice_pf *pf = vsi->back;
3902 	int err;
3903 
3904 	ice_vsi_cfg_msix(vsi);
3905 
3906 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
3907 	 * Tx queue group list was configured and the context bits were
3908 	 * programmed using ice_vsi_cfg_txqs
3909 	 */
3910 	err = ice_vsi_start_rx_rings(vsi);
3911 	if (err)
3912 		return err;
3913 
3914 	clear_bit(__ICE_DOWN, vsi->state);
3915 	ice_napi_enable_all(vsi);
3916 	ice_vsi_ena_irq(vsi);
3917 
3918 	if (vsi->port_info &&
3919 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
3920 	    vsi->netdev) {
3921 		ice_print_link_msg(vsi, true);
3922 		netif_tx_start_all_queues(vsi->netdev);
3923 		netif_carrier_on(vsi->netdev);
3924 	}
3925 
3926 	ice_service_task_schedule(pf);
3927 
3928 	return 0;
3929 }
3930 
3931 /**
3932  * ice_up - Bring the connection back up after being down
3933  * @vsi: VSI being configured
3934  */
3935 int ice_up(struct ice_vsi *vsi)
3936 {
3937 	int err;
3938 
3939 	err = ice_vsi_cfg(vsi);
3940 	if (!err)
3941 		err = ice_up_complete(vsi);
3942 
3943 	return err;
3944 }
3945 
3946 /**
3947  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
3948  * @ring: Tx or Rx ring to read stats from
3949  * @pkts: packets stats counter
3950  * @bytes: bytes stats counter
3951  *
3952  * This function fetches stats from the ring considering the atomic operations
3953  * that needs to be performed to read u64 values in 32 bit machine.
3954  */
3955 static void
3956 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
3957 {
3958 	unsigned int start;
3959 	*pkts = 0;
3960 	*bytes = 0;
3961 
3962 	if (!ring)
3963 		return;
3964 	do {
3965 		start = u64_stats_fetch_begin_irq(&ring->syncp);
3966 		*pkts = ring->stats.pkts;
3967 		*bytes = ring->stats.bytes;
3968 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3969 }
3970 
3971 /**
3972  * ice_update_vsi_ring_stats - Update VSI stats counters
3973  * @vsi: the VSI to be updated
3974  */
3975 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
3976 {
3977 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
3978 	struct ice_ring *ring;
3979 	u64 pkts, bytes;
3980 	int i;
3981 
3982 	/* reset netdev stats */
3983 	vsi_stats->tx_packets = 0;
3984 	vsi_stats->tx_bytes = 0;
3985 	vsi_stats->rx_packets = 0;
3986 	vsi_stats->rx_bytes = 0;
3987 
3988 	/* reset non-netdev (extended) stats */
3989 	vsi->tx_restart = 0;
3990 	vsi->tx_busy = 0;
3991 	vsi->tx_linearize = 0;
3992 	vsi->rx_buf_failed = 0;
3993 	vsi->rx_page_failed = 0;
3994 
3995 	rcu_read_lock();
3996 
3997 	/* update Tx rings counters */
3998 	ice_for_each_txq(vsi, i) {
3999 		ring = READ_ONCE(vsi->tx_rings[i]);
4000 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4001 		vsi_stats->tx_packets += pkts;
4002 		vsi_stats->tx_bytes += bytes;
4003 		vsi->tx_restart += ring->tx_stats.restart_q;
4004 		vsi->tx_busy += ring->tx_stats.tx_busy;
4005 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
4006 	}
4007 
4008 	/* update Rx rings counters */
4009 	ice_for_each_rxq(vsi, i) {
4010 		ring = READ_ONCE(vsi->rx_rings[i]);
4011 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4012 		vsi_stats->rx_packets += pkts;
4013 		vsi_stats->rx_bytes += bytes;
4014 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4015 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4016 	}
4017 
4018 	rcu_read_unlock();
4019 }
4020 
4021 /**
4022  * ice_update_vsi_stats - Update VSI stats counters
4023  * @vsi: the VSI to be updated
4024  */
4025 void ice_update_vsi_stats(struct ice_vsi *vsi)
4026 {
4027 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4028 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
4029 	struct ice_pf *pf = vsi->back;
4030 
4031 	if (test_bit(__ICE_DOWN, vsi->state) ||
4032 	    test_bit(__ICE_CFG_BUSY, pf->state))
4033 		return;
4034 
4035 	/* get stats as recorded by Tx/Rx rings */
4036 	ice_update_vsi_ring_stats(vsi);
4037 
4038 	/* get VSI stats as recorded by the hardware */
4039 	ice_update_eth_stats(vsi);
4040 
4041 	cur_ns->tx_errors = cur_es->tx_errors;
4042 	cur_ns->rx_dropped = cur_es->rx_discards;
4043 	cur_ns->tx_dropped = cur_es->tx_discards;
4044 	cur_ns->multicast = cur_es->rx_multicast;
4045 
4046 	/* update some more netdev stats if this is main VSI */
4047 	if (vsi->type == ICE_VSI_PF) {
4048 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
4049 		cur_ns->rx_errors = pf->stats.crc_errors +
4050 				    pf->stats.illegal_bytes;
4051 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4052 		/* record drops from the port level */
4053 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
4054 	}
4055 }
4056 
4057 /**
4058  * ice_update_pf_stats - Update PF port stats counters
4059  * @pf: PF whose stats needs to be updated
4060  */
4061 void ice_update_pf_stats(struct ice_pf *pf)
4062 {
4063 	struct ice_hw_port_stats *prev_ps, *cur_ps;
4064 	struct ice_hw *hw = &pf->hw;
4065 	u8 port;
4066 
4067 	port = hw->port_info->lport;
4068 	prev_ps = &pf->stats_prev;
4069 	cur_ps = &pf->stats;
4070 
4071 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
4072 			  &prev_ps->eth.rx_bytes,
4073 			  &cur_ps->eth.rx_bytes);
4074 
4075 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
4076 			  &prev_ps->eth.rx_unicast,
4077 			  &cur_ps->eth.rx_unicast);
4078 
4079 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
4080 			  &prev_ps->eth.rx_multicast,
4081 			  &cur_ps->eth.rx_multicast);
4082 
4083 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
4084 			  &prev_ps->eth.rx_broadcast,
4085 			  &cur_ps->eth.rx_broadcast);
4086 
4087 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
4088 			  &prev_ps->eth.rx_discards,
4089 			  &cur_ps->eth.rx_discards);
4090 
4091 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
4092 			  &prev_ps->eth.tx_bytes,
4093 			  &cur_ps->eth.tx_bytes);
4094 
4095 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
4096 			  &prev_ps->eth.tx_unicast,
4097 			  &cur_ps->eth.tx_unicast);
4098 
4099 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
4100 			  &prev_ps->eth.tx_multicast,
4101 			  &cur_ps->eth.tx_multicast);
4102 
4103 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
4104 			  &prev_ps->eth.tx_broadcast,
4105 			  &cur_ps->eth.tx_broadcast);
4106 
4107 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
4108 			  &prev_ps->tx_dropped_link_down,
4109 			  &cur_ps->tx_dropped_link_down);
4110 
4111 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
4112 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
4113 
4114 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
4115 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
4116 
4117 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
4118 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
4119 
4120 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
4121 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
4122 
4123 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
4124 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
4125 
4126 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
4127 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
4128 
4129 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
4130 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
4131 
4132 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
4133 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
4134 
4135 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
4136 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
4137 
4138 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
4139 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
4140 
4141 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
4142 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
4143 
4144 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
4145 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
4146 
4147 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
4148 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
4149 
4150 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
4151 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
4152 
4153 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
4154 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
4155 
4156 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
4157 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
4158 
4159 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
4160 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
4161 
4162 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
4163 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
4164 
4165 	ice_update_dcb_stats(pf);
4166 
4167 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
4168 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
4169 
4170 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
4171 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
4172 
4173 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
4174 			  &prev_ps->mac_local_faults,
4175 			  &cur_ps->mac_local_faults);
4176 
4177 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
4178 			  &prev_ps->mac_remote_faults,
4179 			  &cur_ps->mac_remote_faults);
4180 
4181 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
4182 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
4183 
4184 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
4185 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
4186 
4187 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
4188 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
4189 
4190 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
4191 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
4192 
4193 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
4194 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
4195 
4196 	pf->stat_prev_loaded = true;
4197 }
4198 
4199 /**
4200  * ice_get_stats64 - get statistics for network device structure
4201  * @netdev: network interface device structure
4202  * @stats: main device statistics structure
4203  */
4204 static
4205 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
4206 {
4207 	struct ice_netdev_priv *np = netdev_priv(netdev);
4208 	struct rtnl_link_stats64 *vsi_stats;
4209 	struct ice_vsi *vsi = np->vsi;
4210 
4211 	vsi_stats = &vsi->net_stats;
4212 
4213 	if (!vsi->num_txq || !vsi->num_rxq)
4214 		return;
4215 
4216 	/* netdev packet/byte stats come from ring counter. These are obtained
4217 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
4218 	 * But, only call the update routine and read the registers if VSI is
4219 	 * not down.
4220 	 */
4221 	if (!test_bit(__ICE_DOWN, vsi->state))
4222 		ice_update_vsi_ring_stats(vsi);
4223 	stats->tx_packets = vsi_stats->tx_packets;
4224 	stats->tx_bytes = vsi_stats->tx_bytes;
4225 	stats->rx_packets = vsi_stats->rx_packets;
4226 	stats->rx_bytes = vsi_stats->rx_bytes;
4227 
4228 	/* The rest of the stats can be read from the hardware but instead we
4229 	 * just return values that the watchdog task has already obtained from
4230 	 * the hardware.
4231 	 */
4232 	stats->multicast = vsi_stats->multicast;
4233 	stats->tx_errors = vsi_stats->tx_errors;
4234 	stats->tx_dropped = vsi_stats->tx_dropped;
4235 	stats->rx_errors = vsi_stats->rx_errors;
4236 	stats->rx_dropped = vsi_stats->rx_dropped;
4237 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
4238 	stats->rx_length_errors = vsi_stats->rx_length_errors;
4239 }
4240 
4241 /**
4242  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4243  * @vsi: VSI having NAPI disabled
4244  */
4245 static void ice_napi_disable_all(struct ice_vsi *vsi)
4246 {
4247 	int q_idx;
4248 
4249 	if (!vsi->netdev)
4250 		return;
4251 
4252 	ice_for_each_q_vector(vsi, q_idx) {
4253 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
4254 
4255 		if (q_vector->rx.ring || q_vector->tx.ring)
4256 			napi_disable(&q_vector->napi);
4257 	}
4258 }
4259 
4260 /**
4261  * ice_down - Shutdown the connection
4262  * @vsi: The VSI being stopped
4263  */
4264 int ice_down(struct ice_vsi *vsi)
4265 {
4266 	int i, tx_err, rx_err, link_err = 0;
4267 
4268 	/* Caller of this function is expected to set the
4269 	 * vsi->state __ICE_DOWN bit
4270 	 */
4271 	if (vsi->netdev) {
4272 		netif_carrier_off(vsi->netdev);
4273 		netif_tx_disable(vsi->netdev);
4274 	}
4275 
4276 	ice_vsi_dis_irq(vsi);
4277 
4278 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
4279 	if (tx_err)
4280 		netdev_err(vsi->netdev,
4281 			   "Failed stop Tx rings, VSI %d error %d\n",
4282 			   vsi->vsi_num, tx_err);
4283 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
4284 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
4285 		if (tx_err)
4286 			netdev_err(vsi->netdev,
4287 				   "Failed stop XDP rings, VSI %d error %d\n",
4288 				   vsi->vsi_num, tx_err);
4289 	}
4290 
4291 	rx_err = ice_vsi_stop_rx_rings(vsi);
4292 	if (rx_err)
4293 		netdev_err(vsi->netdev,
4294 			   "Failed stop Rx rings, VSI %d error %d\n",
4295 			   vsi->vsi_num, rx_err);
4296 
4297 	ice_napi_disable_all(vsi);
4298 
4299 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
4300 		link_err = ice_force_phys_link_state(vsi, false);
4301 		if (link_err)
4302 			netdev_err(vsi->netdev,
4303 				   "Failed to set physical link down, VSI %d error %d\n",
4304 				   vsi->vsi_num, link_err);
4305 	}
4306 
4307 	ice_for_each_txq(vsi, i)
4308 		ice_clean_tx_ring(vsi->tx_rings[i]);
4309 
4310 	ice_for_each_rxq(vsi, i)
4311 		ice_clean_rx_ring(vsi->rx_rings[i]);
4312 
4313 	if (tx_err || rx_err || link_err) {
4314 		netdev_err(vsi->netdev,
4315 			   "Failed to close VSI 0x%04X on switch 0x%04X\n",
4316 			   vsi->vsi_num, vsi->vsw->sw_id);
4317 		return -EIO;
4318 	}
4319 
4320 	return 0;
4321 }
4322 
4323 /**
4324  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
4325  * @vsi: VSI having resources allocated
4326  *
4327  * Return 0 on success, negative on failure
4328  */
4329 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
4330 {
4331 	int i, err = 0;
4332 
4333 	if (!vsi->num_txq) {
4334 		dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
4335 			vsi->vsi_num);
4336 		return -EINVAL;
4337 	}
4338 
4339 	ice_for_each_txq(vsi, i) {
4340 		struct ice_ring *ring = vsi->tx_rings[i];
4341 
4342 		if (!ring)
4343 			return -EINVAL;
4344 
4345 		ring->netdev = vsi->netdev;
4346 		err = ice_setup_tx_ring(ring);
4347 		if (err)
4348 			break;
4349 	}
4350 
4351 	return err;
4352 }
4353 
4354 /**
4355  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
4356  * @vsi: VSI having resources allocated
4357  *
4358  * Return 0 on success, negative on failure
4359  */
4360 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
4361 {
4362 	int i, err = 0;
4363 
4364 	if (!vsi->num_rxq) {
4365 		dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
4366 			vsi->vsi_num);
4367 		return -EINVAL;
4368 	}
4369 
4370 	ice_for_each_rxq(vsi, i) {
4371 		struct ice_ring *ring = vsi->rx_rings[i];
4372 
4373 		if (!ring)
4374 			return -EINVAL;
4375 
4376 		ring->netdev = vsi->netdev;
4377 		err = ice_setup_rx_ring(ring);
4378 		if (err)
4379 			break;
4380 	}
4381 
4382 	return err;
4383 }
4384 
4385 /**
4386  * ice_vsi_open - Called when a network interface is made active
4387  * @vsi: the VSI to open
4388  *
4389  * Initialization of the VSI
4390  *
4391  * Returns 0 on success, negative value on error
4392  */
4393 static int ice_vsi_open(struct ice_vsi *vsi)
4394 {
4395 	char int_name[ICE_INT_NAME_STR_LEN];
4396 	struct ice_pf *pf = vsi->back;
4397 	int err;
4398 
4399 	/* allocate descriptors */
4400 	err = ice_vsi_setup_tx_rings(vsi);
4401 	if (err)
4402 		goto err_setup_tx;
4403 
4404 	err = ice_vsi_setup_rx_rings(vsi);
4405 	if (err)
4406 		goto err_setup_rx;
4407 
4408 	err = ice_vsi_cfg(vsi);
4409 	if (err)
4410 		goto err_setup_rx;
4411 
4412 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4413 		 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4414 	err = ice_vsi_req_irq_msix(vsi, int_name);
4415 	if (err)
4416 		goto err_setup_rx;
4417 
4418 	/* Notify the stack of the actual queue counts. */
4419 	err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
4420 	if (err)
4421 		goto err_set_qs;
4422 
4423 	err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
4424 	if (err)
4425 		goto err_set_qs;
4426 
4427 	err = ice_up_complete(vsi);
4428 	if (err)
4429 		goto err_up_complete;
4430 
4431 	return 0;
4432 
4433 err_up_complete:
4434 	ice_down(vsi);
4435 err_set_qs:
4436 	ice_vsi_free_irq(vsi);
4437 err_setup_rx:
4438 	ice_vsi_free_rx_rings(vsi);
4439 err_setup_tx:
4440 	ice_vsi_free_tx_rings(vsi);
4441 
4442 	return err;
4443 }
4444 
4445 /**
4446  * ice_vsi_release_all - Delete all VSIs
4447  * @pf: PF from which all VSIs are being removed
4448  */
4449 static void ice_vsi_release_all(struct ice_pf *pf)
4450 {
4451 	int err, i;
4452 
4453 	if (!pf->vsi)
4454 		return;
4455 
4456 	ice_for_each_vsi(pf, i) {
4457 		if (!pf->vsi[i])
4458 			continue;
4459 
4460 		err = ice_vsi_release(pf->vsi[i]);
4461 		if (err)
4462 			dev_dbg(&pf->pdev->dev,
4463 				"Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
4464 				i, err, pf->vsi[i]->vsi_num);
4465 	}
4466 }
4467 
4468 /**
4469  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
4470  * @pf: pointer to the PF instance
4471  * @type: VSI type to rebuild
4472  *
4473  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
4474  */
4475 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
4476 {
4477 	enum ice_status status;
4478 	int i, err;
4479 
4480 	ice_for_each_vsi(pf, i) {
4481 		struct ice_vsi *vsi = pf->vsi[i];
4482 
4483 		if (!vsi || vsi->type != type)
4484 			continue;
4485 
4486 		/* rebuild the VSI */
4487 		err = ice_vsi_rebuild(vsi);
4488 		if (err) {
4489 			dev_err(&pf->pdev->dev,
4490 				"rebuild VSI failed, err %d, VSI index %d, type %s\n",
4491 				err, vsi->idx, ice_vsi_type_str(type));
4492 			return err;
4493 		}
4494 
4495 		/* replay filters for the VSI */
4496 		status = ice_replay_vsi(&pf->hw, vsi->idx);
4497 		if (status) {
4498 			dev_err(&pf->pdev->dev,
4499 				"replay VSI failed, status %d, VSI index %d, type %s\n",
4500 				status, vsi->idx, ice_vsi_type_str(type));
4501 			return -EIO;
4502 		}
4503 
4504 		/* Re-map HW VSI number, using VSI handle that has been
4505 		 * previously validated in ice_replay_vsi() call above
4506 		 */
4507 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
4508 
4509 		/* enable the VSI */
4510 		err = ice_ena_vsi(vsi, false);
4511 		if (err) {
4512 			dev_err(&pf->pdev->dev,
4513 				"enable VSI failed, err %d, VSI index %d, type %s\n",
4514 				err, vsi->idx, ice_vsi_type_str(type));
4515 			return err;
4516 		}
4517 
4518 		dev_info(&pf->pdev->dev, "VSI rebuilt. VSI index %d, type %s\n",
4519 			 vsi->idx, ice_vsi_type_str(type));
4520 	}
4521 
4522 	return 0;
4523 }
4524 
4525 /**
4526  * ice_update_pf_netdev_link - Update PF netdev link status
4527  * @pf: pointer to the PF instance
4528  */
4529 static void ice_update_pf_netdev_link(struct ice_pf *pf)
4530 {
4531 	bool link_up;
4532 	int i;
4533 
4534 	ice_for_each_vsi(pf, i) {
4535 		struct ice_vsi *vsi = pf->vsi[i];
4536 
4537 		if (!vsi || vsi->type != ICE_VSI_PF)
4538 			return;
4539 
4540 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
4541 		if (link_up) {
4542 			netif_carrier_on(pf->vsi[i]->netdev);
4543 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
4544 		} else {
4545 			netif_carrier_off(pf->vsi[i]->netdev);
4546 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
4547 		}
4548 	}
4549 }
4550 
4551 /**
4552  * ice_rebuild - rebuild after reset
4553  * @pf: PF to rebuild
4554  * @reset_type: type of reset
4555  */
4556 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
4557 {
4558 	struct device *dev = &pf->pdev->dev;
4559 	struct ice_hw *hw = &pf->hw;
4560 	enum ice_status ret;
4561 	int err;
4562 
4563 	if (test_bit(__ICE_DOWN, pf->state))
4564 		goto clear_recovery;
4565 
4566 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
4567 
4568 	ret = ice_init_all_ctrlq(hw);
4569 	if (ret) {
4570 		dev_err(dev, "control queues init failed %d\n", ret);
4571 		goto err_init_ctrlq;
4572 	}
4573 
4574 	/* if DDP was previously loaded successfully */
4575 	if (!ice_is_safe_mode(pf)) {
4576 		/* reload the SW DB of filter tables */
4577 		if (reset_type == ICE_RESET_PFR)
4578 			ice_fill_blk_tbls(hw);
4579 		else
4580 			/* Reload DDP Package after CORER/GLOBR reset */
4581 			ice_load_pkg(NULL, pf);
4582 	}
4583 
4584 	ret = ice_clear_pf_cfg(hw);
4585 	if (ret) {
4586 		dev_err(dev, "clear PF configuration failed %d\n", ret);
4587 		goto err_init_ctrlq;
4588 	}
4589 
4590 	ice_clear_pxe_mode(hw);
4591 
4592 	ret = ice_get_caps(hw);
4593 	if (ret) {
4594 		dev_err(dev, "ice_get_caps failed %d\n", ret);
4595 		goto err_init_ctrlq;
4596 	}
4597 
4598 	err = ice_sched_init_port(hw->port_info);
4599 	if (err)
4600 		goto err_sched_init_port;
4601 
4602 	err = ice_update_link_info(hw->port_info);
4603 	if (err)
4604 		dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
4605 
4606 	/* start misc vector */
4607 	err = ice_req_irq_msix_misc(pf);
4608 	if (err) {
4609 		dev_err(dev, "misc vector setup failed: %d\n", err);
4610 		goto err_sched_init_port;
4611 	}
4612 
4613 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
4614 		ice_dcb_rebuild(pf);
4615 
4616 	/* rebuild PF VSI */
4617 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
4618 	if (err) {
4619 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
4620 		goto err_vsi_rebuild;
4621 	}
4622 
4623 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4624 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_VF);
4625 		if (err) {
4626 			dev_err(dev, "VF VSI rebuild failed: %d\n", err);
4627 			goto err_vsi_rebuild;
4628 		}
4629 	}
4630 
4631 	ice_update_pf_netdev_link(pf);
4632 
4633 	/* tell the firmware we are up */
4634 	ret = ice_send_version(pf);
4635 	if (ret) {
4636 		dev_err(dev,
4637 			"Rebuild failed due to error sending driver version: %d\n",
4638 			ret);
4639 		goto err_vsi_rebuild;
4640 	}
4641 
4642 	ice_replay_post(hw);
4643 
4644 	/* if we get here, reset flow is successful */
4645 	clear_bit(__ICE_RESET_FAILED, pf->state);
4646 	return;
4647 
4648 err_vsi_rebuild:
4649 err_sched_init_port:
4650 	ice_sched_cleanup_all(hw);
4651 err_init_ctrlq:
4652 	ice_shutdown_all_ctrlq(hw);
4653 	set_bit(__ICE_RESET_FAILED, pf->state);
4654 clear_recovery:
4655 	/* set this bit in PF state to control service task scheduling */
4656 	set_bit(__ICE_NEEDS_RESTART, pf->state);
4657 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
4658 }
4659 
4660 /**
4661  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
4662  * @vsi: Pointer to VSI structure
4663  */
4664 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
4665 {
4666 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
4667 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
4668 	else
4669 		return ICE_RXBUF_3072;
4670 }
4671 
4672 /**
4673  * ice_change_mtu - NDO callback to change the MTU
4674  * @netdev: network interface device structure
4675  * @new_mtu: new value for maximum frame size
4676  *
4677  * Returns 0 on success, negative on failure
4678  */
4679 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
4680 {
4681 	struct ice_netdev_priv *np = netdev_priv(netdev);
4682 	struct ice_vsi *vsi = np->vsi;
4683 	struct ice_pf *pf = vsi->back;
4684 	u8 count = 0;
4685 
4686 	if (new_mtu == netdev->mtu) {
4687 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
4688 		return 0;
4689 	}
4690 
4691 	if (ice_is_xdp_ena_vsi(vsi)) {
4692 		int frame_size = ice_max_xdp_frame_size(vsi);
4693 
4694 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
4695 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
4696 				   frame_size - ICE_ETH_PKT_HDR_PAD);
4697 			return -EINVAL;
4698 		}
4699 	}
4700 
4701 	if (new_mtu < netdev->min_mtu) {
4702 		netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
4703 			   netdev->min_mtu);
4704 		return -EINVAL;
4705 	} else if (new_mtu > netdev->max_mtu) {
4706 		netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
4707 			   netdev->min_mtu);
4708 		return -EINVAL;
4709 	}
4710 	/* if a reset is in progress, wait for some time for it to complete */
4711 	do {
4712 		if (ice_is_reset_in_progress(pf->state)) {
4713 			count++;
4714 			usleep_range(1000, 2000);
4715 		} else {
4716 			break;
4717 		}
4718 
4719 	} while (count < 100);
4720 
4721 	if (count == 100) {
4722 		netdev_err(netdev, "can't change MTU. Device is busy\n");
4723 		return -EBUSY;
4724 	}
4725 
4726 	netdev->mtu = new_mtu;
4727 
4728 	/* if VSI is up, bring it down and then back up */
4729 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
4730 		int err;
4731 
4732 		err = ice_down(vsi);
4733 		if (err) {
4734 			netdev_err(netdev, "change MTU if_up err %d\n", err);
4735 			return err;
4736 		}
4737 
4738 		err = ice_up(vsi);
4739 		if (err) {
4740 			netdev_err(netdev, "change MTU if_up err %d\n", err);
4741 			return err;
4742 		}
4743 	}
4744 
4745 	netdev_info(netdev, "changed MTU to %d\n", new_mtu);
4746 	return 0;
4747 }
4748 
4749 /**
4750  * ice_set_rss - Set RSS keys and lut
4751  * @vsi: Pointer to VSI structure
4752  * @seed: RSS hash seed
4753  * @lut: Lookup table
4754  * @lut_size: Lookup table size
4755  *
4756  * Returns 0 on success, negative on failure
4757  */
4758 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4759 {
4760 	struct ice_pf *pf = vsi->back;
4761 	struct ice_hw *hw = &pf->hw;
4762 	enum ice_status status;
4763 
4764 	if (seed) {
4765 		struct ice_aqc_get_set_rss_keys *buf =
4766 				  (struct ice_aqc_get_set_rss_keys *)seed;
4767 
4768 		status = ice_aq_set_rss_key(hw, vsi->idx, buf);
4769 
4770 		if (status) {
4771 			dev_err(&pf->pdev->dev,
4772 				"Cannot set RSS key, err %d aq_err %d\n",
4773 				status, hw->adminq.rq_last_status);
4774 			return -EIO;
4775 		}
4776 	}
4777 
4778 	if (lut) {
4779 		status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4780 					    lut, lut_size);
4781 		if (status) {
4782 			dev_err(&pf->pdev->dev,
4783 				"Cannot set RSS lut, err %d aq_err %d\n",
4784 				status, hw->adminq.rq_last_status);
4785 			return -EIO;
4786 		}
4787 	}
4788 
4789 	return 0;
4790 }
4791 
4792 /**
4793  * ice_get_rss - Get RSS keys and lut
4794  * @vsi: Pointer to VSI structure
4795  * @seed: Buffer to store the keys
4796  * @lut: Buffer to store the lookup table entries
4797  * @lut_size: Size of buffer to store the lookup table entries
4798  *
4799  * Returns 0 on success, negative on failure
4800  */
4801 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4802 {
4803 	struct ice_pf *pf = vsi->back;
4804 	struct ice_hw *hw = &pf->hw;
4805 	enum ice_status status;
4806 
4807 	if (seed) {
4808 		struct ice_aqc_get_set_rss_keys *buf =
4809 				  (struct ice_aqc_get_set_rss_keys *)seed;
4810 
4811 		status = ice_aq_get_rss_key(hw, vsi->idx, buf);
4812 		if (status) {
4813 			dev_err(&pf->pdev->dev,
4814 				"Cannot get RSS key, err %d aq_err %d\n",
4815 				status, hw->adminq.rq_last_status);
4816 			return -EIO;
4817 		}
4818 	}
4819 
4820 	if (lut) {
4821 		status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4822 					    lut, lut_size);
4823 		if (status) {
4824 			dev_err(&pf->pdev->dev,
4825 				"Cannot get RSS lut, err %d aq_err %d\n",
4826 				status, hw->adminq.rq_last_status);
4827 			return -EIO;
4828 		}
4829 	}
4830 
4831 	return 0;
4832 }
4833 
4834 /**
4835  * ice_bridge_getlink - Get the hardware bridge mode
4836  * @skb: skb buff
4837  * @pid: process ID
4838  * @seq: RTNL message seq
4839  * @dev: the netdev being configured
4840  * @filter_mask: filter mask passed in
4841  * @nlflags: netlink flags passed in
4842  *
4843  * Return the bridge mode (VEB/VEPA)
4844  */
4845 static int
4846 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4847 		   struct net_device *dev, u32 filter_mask, int nlflags)
4848 {
4849 	struct ice_netdev_priv *np = netdev_priv(dev);
4850 	struct ice_vsi *vsi = np->vsi;
4851 	struct ice_pf *pf = vsi->back;
4852 	u16 bmode;
4853 
4854 	bmode = pf->first_sw->bridge_mode;
4855 
4856 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
4857 				       filter_mask, NULL);
4858 }
4859 
4860 /**
4861  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
4862  * @vsi: Pointer to VSI structure
4863  * @bmode: Hardware bridge mode (VEB/VEPA)
4864  *
4865  * Returns 0 on success, negative on failure
4866  */
4867 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
4868 {
4869 	struct device *dev = &vsi->back->pdev->dev;
4870 	struct ice_aqc_vsi_props *vsi_props;
4871 	struct ice_hw *hw = &vsi->back->hw;
4872 	struct ice_vsi_ctx *ctxt;
4873 	enum ice_status status;
4874 	int ret = 0;
4875 
4876 	vsi_props = &vsi->info;
4877 
4878 	ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
4879 	if (!ctxt)
4880 		return -ENOMEM;
4881 
4882 	ctxt->info = vsi->info;
4883 
4884 	if (bmode == BRIDGE_MODE_VEB)
4885 		/* change from VEPA to VEB mode */
4886 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4887 	else
4888 		/* change from VEB to VEPA mode */
4889 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4890 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4891 
4892 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4893 	if (status) {
4894 		dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
4895 			bmode, status, hw->adminq.sq_last_status);
4896 		ret = -EIO;
4897 		goto out;
4898 	}
4899 	/* Update sw flags for book keeping */
4900 	vsi_props->sw_flags = ctxt->info.sw_flags;
4901 
4902 out:
4903 	devm_kfree(dev, ctxt);
4904 	return ret;
4905 }
4906 
4907 /**
4908  * ice_bridge_setlink - Set the hardware bridge mode
4909  * @dev: the netdev being configured
4910  * @nlh: RTNL message
4911  * @flags: bridge setlink flags
4912  * @extack: netlink extended ack
4913  *
4914  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
4915  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
4916  * not already set for all VSIs connected to this switch. And also update the
4917  * unicast switch filter rules for the corresponding switch of the netdev.
4918  */
4919 static int
4920 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4921 		   u16 __always_unused flags,
4922 		   struct netlink_ext_ack __always_unused *extack)
4923 {
4924 	struct ice_netdev_priv *np = netdev_priv(dev);
4925 	struct ice_pf *pf = np->vsi->back;
4926 	struct nlattr *attr, *br_spec;
4927 	struct ice_hw *hw = &pf->hw;
4928 	enum ice_status status;
4929 	struct ice_sw *pf_sw;
4930 	int rem, v, err = 0;
4931 
4932 	pf_sw = pf->first_sw;
4933 	/* find the attribute in the netlink message */
4934 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4935 
4936 	nla_for_each_nested(attr, br_spec, rem) {
4937 		__u16 mode;
4938 
4939 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
4940 			continue;
4941 		mode = nla_get_u16(attr);
4942 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4943 			return -EINVAL;
4944 		/* Continue  if bridge mode is not being flipped */
4945 		if (mode == pf_sw->bridge_mode)
4946 			continue;
4947 		/* Iterates through the PF VSI list and update the loopback
4948 		 * mode of the VSI
4949 		 */
4950 		ice_for_each_vsi(pf, v) {
4951 			if (!pf->vsi[v])
4952 				continue;
4953 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
4954 			if (err)
4955 				return err;
4956 		}
4957 
4958 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
4959 		/* Update the unicast switch filter rules for the corresponding
4960 		 * switch of the netdev
4961 		 */
4962 		status = ice_update_sw_rule_bridge_mode(hw);
4963 		if (status) {
4964 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n",
4965 				   mode, status, hw->adminq.sq_last_status);
4966 			/* revert hw->evb_veb */
4967 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
4968 			return -EIO;
4969 		}
4970 
4971 		pf_sw->bridge_mode = mode;
4972 	}
4973 
4974 	return 0;
4975 }
4976 
4977 /**
4978  * ice_tx_timeout - Respond to a Tx Hang
4979  * @netdev: network interface device structure
4980  */
4981 static void ice_tx_timeout(struct net_device *netdev)
4982 {
4983 	struct ice_netdev_priv *np = netdev_priv(netdev);
4984 	struct ice_ring *tx_ring = NULL;
4985 	struct ice_vsi *vsi = np->vsi;
4986 	struct ice_pf *pf = vsi->back;
4987 	int hung_queue = -1;
4988 	u32 i;
4989 
4990 	pf->tx_timeout_count++;
4991 
4992 	/* find the stopped queue the same way dev_watchdog() does */
4993 	for (i = 0; i < netdev->num_tx_queues; i++) {
4994 		unsigned long trans_start;
4995 		struct netdev_queue *q;
4996 
4997 		q = netdev_get_tx_queue(netdev, i);
4998 		trans_start = q->trans_start;
4999 		if (netif_xmit_stopped(q) &&
5000 		    time_after(jiffies,
5001 			       trans_start + netdev->watchdog_timeo)) {
5002 			hung_queue = i;
5003 			break;
5004 		}
5005 	}
5006 
5007 	if (i == netdev->num_tx_queues)
5008 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
5009 	else
5010 		/* now that we have an index, find the tx_ring struct */
5011 		for (i = 0; i < vsi->num_txq; i++)
5012 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
5013 				if (hung_queue == vsi->tx_rings[i]->q_index) {
5014 					tx_ring = vsi->tx_rings[i];
5015 					break;
5016 				}
5017 
5018 	/* Reset recovery level if enough time has elapsed after last timeout.
5019 	 * Also ensure no new reset action happens before next timeout period.
5020 	 */
5021 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
5022 		pf->tx_timeout_recovery_level = 1;
5023 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
5024 				       netdev->watchdog_timeo)))
5025 		return;
5026 
5027 	if (tx_ring) {
5028 		struct ice_hw *hw = &pf->hw;
5029 		u32 head, val = 0;
5030 
5031 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[hung_queue])) &
5032 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
5033 		/* Read interrupt register */
5034 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
5035 
5036 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
5037 			    vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
5038 			    head, tx_ring->next_to_use, val);
5039 	}
5040 
5041 	pf->tx_timeout_last_recovery = jiffies;
5042 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
5043 		    pf->tx_timeout_recovery_level, hung_queue);
5044 
5045 	switch (pf->tx_timeout_recovery_level) {
5046 	case 1:
5047 		set_bit(__ICE_PFR_REQ, pf->state);
5048 		break;
5049 	case 2:
5050 		set_bit(__ICE_CORER_REQ, pf->state);
5051 		break;
5052 	case 3:
5053 		set_bit(__ICE_GLOBR_REQ, pf->state);
5054 		break;
5055 	default:
5056 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
5057 		set_bit(__ICE_DOWN, pf->state);
5058 		set_bit(__ICE_NEEDS_RESTART, vsi->state);
5059 		set_bit(__ICE_SERVICE_DIS, pf->state);
5060 		break;
5061 	}
5062 
5063 	ice_service_task_schedule(pf);
5064 	pf->tx_timeout_recovery_level++;
5065 }
5066 
5067 /**
5068  * ice_open - Called when a network interface becomes active
5069  * @netdev: network interface device structure
5070  *
5071  * The open entry point is called when a network interface is made
5072  * active by the system (IFF_UP). At this point all resources needed
5073  * for transmit and receive operations are allocated, the interrupt
5074  * handler is registered with the OS, the netdev watchdog is enabled,
5075  * and the stack is notified that the interface is ready.
5076  *
5077  * Returns 0 on success, negative value on failure
5078  */
5079 int ice_open(struct net_device *netdev)
5080 {
5081 	struct ice_netdev_priv *np = netdev_priv(netdev);
5082 	struct ice_vsi *vsi = np->vsi;
5083 	struct ice_port_info *pi;
5084 	int err;
5085 
5086 	if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
5087 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
5088 		return -EIO;
5089 	}
5090 
5091 	netif_carrier_off(netdev);
5092 
5093 	pi = vsi->port_info;
5094 	err = ice_update_link_info(pi);
5095 	if (err) {
5096 		netdev_err(netdev, "Failed to get link info, error %d\n",
5097 			   err);
5098 		return err;
5099 	}
5100 
5101 	/* Set PHY if there is media, otherwise, turn off PHY */
5102 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
5103 		err = ice_force_phys_link_state(vsi, true);
5104 		if (err) {
5105 			netdev_err(netdev,
5106 				   "Failed to set physical link up, error %d\n",
5107 				   err);
5108 			return err;
5109 		}
5110 	} else {
5111 		err = ice_aq_set_link_restart_an(pi, false, NULL);
5112 		if (err) {
5113 			netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
5114 				   vsi->vsi_num, err);
5115 			return err;
5116 		}
5117 		set_bit(ICE_FLAG_NO_MEDIA, vsi->back->flags);
5118 	}
5119 
5120 	err = ice_vsi_open(vsi);
5121 	if (err)
5122 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
5123 			   vsi->vsi_num, vsi->vsw->sw_id);
5124 	return err;
5125 }
5126 
5127 /**
5128  * ice_stop - Disables a network interface
5129  * @netdev: network interface device structure
5130  *
5131  * The stop entry point is called when an interface is de-activated by the OS,
5132  * and the netdevice enters the DOWN state. The hardware is still under the
5133  * driver's control, but the netdev interface is disabled.
5134  *
5135  * Returns success only - not allowed to fail
5136  */
5137 int ice_stop(struct net_device *netdev)
5138 {
5139 	struct ice_netdev_priv *np = netdev_priv(netdev);
5140 	struct ice_vsi *vsi = np->vsi;
5141 
5142 	ice_vsi_close(vsi);
5143 
5144 	return 0;
5145 }
5146 
5147 /**
5148  * ice_features_check - Validate encapsulated packet conforms to limits
5149  * @skb: skb buffer
5150  * @netdev: This port's netdev
5151  * @features: Offload features that the stack believes apply
5152  */
5153 static netdev_features_t
5154 ice_features_check(struct sk_buff *skb,
5155 		   struct net_device __always_unused *netdev,
5156 		   netdev_features_t features)
5157 {
5158 	size_t len;
5159 
5160 	/* No point in doing any of this if neither checksum nor GSO are
5161 	 * being requested for this frame. We can rule out both by just
5162 	 * checking for CHECKSUM_PARTIAL
5163 	 */
5164 	if (skb->ip_summed != CHECKSUM_PARTIAL)
5165 		return features;
5166 
5167 	/* We cannot support GSO if the MSS is going to be less than
5168 	 * 64 bytes. If it is then we need to drop support for GSO.
5169 	 */
5170 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
5171 		features &= ~NETIF_F_GSO_MASK;
5172 
5173 	len = skb_network_header(skb) - skb->data;
5174 	if (len & ~(ICE_TXD_MACLEN_MAX))
5175 		goto out_rm_features;
5176 
5177 	len = skb_transport_header(skb) - skb_network_header(skb);
5178 	if (len & ~(ICE_TXD_IPLEN_MAX))
5179 		goto out_rm_features;
5180 
5181 	if (skb->encapsulation) {
5182 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
5183 		if (len & ~(ICE_TXD_L4LEN_MAX))
5184 			goto out_rm_features;
5185 
5186 		len = skb_inner_transport_header(skb) -
5187 		      skb_inner_network_header(skb);
5188 		if (len & ~(ICE_TXD_IPLEN_MAX))
5189 			goto out_rm_features;
5190 	}
5191 
5192 	return features;
5193 out_rm_features:
5194 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
5195 }
5196 
5197 static const struct net_device_ops ice_netdev_safe_mode_ops = {
5198 	.ndo_open = ice_open,
5199 	.ndo_stop = ice_stop,
5200 	.ndo_start_xmit = ice_start_xmit,
5201 	.ndo_set_mac_address = ice_set_mac_address,
5202 	.ndo_validate_addr = eth_validate_addr,
5203 	.ndo_change_mtu = ice_change_mtu,
5204 	.ndo_get_stats64 = ice_get_stats64,
5205 	.ndo_tx_timeout = ice_tx_timeout,
5206 };
5207 
5208 static const struct net_device_ops ice_netdev_ops = {
5209 	.ndo_open = ice_open,
5210 	.ndo_stop = ice_stop,
5211 	.ndo_start_xmit = ice_start_xmit,
5212 	.ndo_features_check = ice_features_check,
5213 	.ndo_set_rx_mode = ice_set_rx_mode,
5214 	.ndo_set_mac_address = ice_set_mac_address,
5215 	.ndo_validate_addr = eth_validate_addr,
5216 	.ndo_change_mtu = ice_change_mtu,
5217 	.ndo_get_stats64 = ice_get_stats64,
5218 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
5219 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
5220 	.ndo_set_vf_mac = ice_set_vf_mac,
5221 	.ndo_get_vf_config = ice_get_vf_cfg,
5222 	.ndo_set_vf_trust = ice_set_vf_trust,
5223 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
5224 	.ndo_set_vf_link_state = ice_set_vf_link_state,
5225 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
5226 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
5227 	.ndo_set_features = ice_set_features,
5228 	.ndo_bridge_getlink = ice_bridge_getlink,
5229 	.ndo_bridge_setlink = ice_bridge_setlink,
5230 	.ndo_fdb_add = ice_fdb_add,
5231 	.ndo_fdb_del = ice_fdb_del,
5232 	.ndo_tx_timeout = ice_tx_timeout,
5233 	.ndo_bpf = ice_xdp,
5234 	.ndo_xdp_xmit = ice_xdp_xmit,
5235 	.ndo_xsk_wakeup = ice_xsk_wakeup,
5236 };
5237