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