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