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