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