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