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