xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_main.c (revision dd2934a95701576203b2f61e8ded4e4a2f9183ea)
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 
10 #define DRV_VERSION	"0.7.1-k"
11 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
12 const char ice_drv_ver[] = DRV_VERSION;
13 static const char ice_driver_string[] = DRV_SUMMARY;
14 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
15 
16 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
17 MODULE_DESCRIPTION(DRV_SUMMARY);
18 MODULE_LICENSE("GPL");
19 MODULE_VERSION(DRV_VERSION);
20 
21 static int debug = -1;
22 module_param(debug, int, 0644);
23 #ifndef CONFIG_DYNAMIC_DEBUG
24 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
25 #else
26 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
27 #endif /* !CONFIG_DYNAMIC_DEBUG */
28 
29 static struct workqueue_struct *ice_wq;
30 static const struct net_device_ops ice_netdev_ops;
31 
32 static void ice_pf_dis_all_vsi(struct ice_pf *pf);
33 static void ice_rebuild(struct ice_pf *pf);
34 static int ice_vsi_release(struct ice_vsi *vsi);
35 static void ice_vsi_release_all(struct ice_pf *pf);
36 static void ice_update_vsi_stats(struct ice_vsi *vsi);
37 static void ice_update_pf_stats(struct ice_pf *pf);
38 
39 /**
40  * ice_get_tx_pending - returns number of Tx descriptors not processed
41  * @ring: the ring of descriptors
42  */
43 static u32 ice_get_tx_pending(struct ice_ring *ring)
44 {
45 	u32 head, tail;
46 
47 	head = ring->next_to_clean;
48 	tail = readl(ring->tail);
49 
50 	if (head != tail)
51 		return (head < tail) ?
52 			tail - head : (tail + ring->count - head);
53 	return 0;
54 }
55 
56 /**
57  * ice_check_for_hang_subtask - check for and recover hung queues
58  * @pf: pointer to PF struct
59  */
60 static void ice_check_for_hang_subtask(struct ice_pf *pf)
61 {
62 	struct ice_vsi *vsi = NULL;
63 	unsigned int i;
64 	u32 v, v_idx;
65 	int packets;
66 
67 	ice_for_each_vsi(pf, v)
68 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
69 			vsi = pf->vsi[v];
70 			break;
71 		}
72 
73 	if (!vsi || test_bit(__ICE_DOWN, vsi->state))
74 		return;
75 
76 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
77 		return;
78 
79 	for (i = 0; i < vsi->num_txq; i++) {
80 		struct ice_ring *tx_ring = vsi->tx_rings[i];
81 
82 		if (tx_ring && tx_ring->desc) {
83 			int itr = ICE_ITR_NONE;
84 
85 			/* If packet counter has not changed the queue is
86 			 * likely stalled, so force an interrupt for this
87 			 * queue.
88 			 *
89 			 * prev_pkt would be negative if there was no
90 			 * pending work.
91 			 */
92 			packets = tx_ring->stats.pkts & INT_MAX;
93 			if (tx_ring->tx_stats.prev_pkt == packets) {
94 				/* Trigger sw interrupt to revive the queue */
95 				v_idx = tx_ring->q_vector->v_idx;
96 				wr32(&vsi->back->hw,
97 				     GLINT_DYN_CTL(vsi->base_vector + v_idx),
98 				     (itr << GLINT_DYN_CTL_ITR_INDX_S) |
99 				     GLINT_DYN_CTL_SWINT_TRIG_M |
100 				     GLINT_DYN_CTL_INTENA_MSK_M);
101 				continue;
102 			}
103 
104 			/* Memory barrier between read of packet count and call
105 			 * to ice_get_tx_pending()
106 			 */
107 			smp_rmb();
108 			tx_ring->tx_stats.prev_pkt =
109 			    ice_get_tx_pending(tx_ring) ? packets : -1;
110 		}
111 	}
112 }
113 
114 /**
115  * ice_get_free_slot - get the next non-NULL location index in array
116  * @array: array to search
117  * @size: size of the array
118  * @curr: last known occupied index to be used as a search hint
119  *
120  * void * is being used to keep the functionality generic. This lets us use this
121  * function on any array of pointers.
122  */
123 static int ice_get_free_slot(void *array, int size, int curr)
124 {
125 	int **tmp_array = (int **)array;
126 	int next;
127 
128 	if (curr < (size - 1) && !tmp_array[curr + 1]) {
129 		next = curr + 1;
130 	} else {
131 		int i = 0;
132 
133 		while ((i < size) && (tmp_array[i]))
134 			i++;
135 		if (i == size)
136 			next = ICE_NO_VSI;
137 		else
138 			next = i;
139 	}
140 	return next;
141 }
142 
143 /**
144  * ice_search_res - Search the tracker for a block of resources
145  * @res: pointer to the resource
146  * @needed: size of the block needed
147  * @id: identifier to track owner
148  * Returns the base item index of the block, or -ENOMEM for error
149  */
150 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
151 {
152 	int start = res->search_hint;
153 	int end = start;
154 
155 	id |= ICE_RES_VALID_BIT;
156 
157 	do {
158 		/* skip already allocated entries */
159 		if (res->list[end++] & ICE_RES_VALID_BIT) {
160 			start = end;
161 			if ((start + needed) > res->num_entries)
162 				break;
163 		}
164 
165 		if (end == (start + needed)) {
166 			int i = start;
167 
168 			/* there was enough, so assign it to the requestor */
169 			while (i != end)
170 				res->list[i++] = id;
171 
172 			if (end == res->num_entries)
173 				end = 0;
174 
175 			res->search_hint = end;
176 			return start;
177 		}
178 	} while (1);
179 
180 	return -ENOMEM;
181 }
182 
183 /**
184  * ice_get_res - get a block of resources
185  * @pf: board private structure
186  * @res: pointer to the resource
187  * @needed: size of the block needed
188  * @id: identifier to track owner
189  *
190  * Returns the base item index of the block, or -ENOMEM for error
191  * The search_hint trick and lack of advanced fit-finding only works
192  * because we're highly likely to have all the same sized requests.
193  * Linear search time and any fragmentation should be minimal.
194  */
195 static int
196 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
197 {
198 	int ret;
199 
200 	if (!res || !pf)
201 		return -EINVAL;
202 
203 	if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
204 		dev_err(&pf->pdev->dev,
205 			"param err: needed=%d, num_entries = %d id=0x%04x\n",
206 			needed, res->num_entries, id);
207 		return -EINVAL;
208 	}
209 
210 	/* search based on search_hint */
211 	ret = ice_search_res(res, needed, id);
212 
213 	if (ret < 0) {
214 		/* previous search failed. Reset search hint and try again */
215 		res->search_hint = 0;
216 		ret = ice_search_res(res, needed, id);
217 	}
218 
219 	return ret;
220 }
221 
222 /**
223  * ice_free_res - free a block of resources
224  * @res: pointer to the resource
225  * @index: starting index previously returned by ice_get_res
226  * @id: identifier to track owner
227  * Returns number of resources freed
228  */
229 static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
230 {
231 	int count = 0;
232 	int i;
233 
234 	if (!res || index >= res->num_entries)
235 		return -EINVAL;
236 
237 	id |= ICE_RES_VALID_BIT;
238 	for (i = index; i < res->num_entries && res->list[i] == id; i++) {
239 		res->list[i] = 0;
240 		count++;
241 	}
242 
243 	return count;
244 }
245 
246 /**
247  * ice_add_mac_to_list - Add a mac address filter entry to the list
248  * @vsi: the VSI to be forwarded to
249  * @add_list: pointer to the list which contains MAC filter entries
250  * @macaddr: the MAC address to be added.
251  *
252  * Adds mac address filter entry to the temp list
253  *
254  * Returns 0 on success or ENOMEM on failure.
255  */
256 static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
257 			       const u8 *macaddr)
258 {
259 	struct ice_fltr_list_entry *tmp;
260 	struct ice_pf *pf = vsi->back;
261 
262 	tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
263 	if (!tmp)
264 		return -ENOMEM;
265 
266 	tmp->fltr_info.flag = ICE_FLTR_TX;
267 	tmp->fltr_info.src = vsi->vsi_num;
268 	tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
269 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
270 	tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
271 	ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
272 
273 	INIT_LIST_HEAD(&tmp->list_entry);
274 	list_add(&tmp->list_entry, add_list);
275 
276 	return 0;
277 }
278 
279 /**
280  * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
281  * @netdev: the net device on which the sync is happening
282  * @addr: mac address to sync
283  *
284  * This is a callback function which is called by the in kernel device sync
285  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
286  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
287  * mac filters from the hardware.
288  */
289 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
290 {
291 	struct ice_netdev_priv *np = netdev_priv(netdev);
292 	struct ice_vsi *vsi = np->vsi;
293 
294 	if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
295 		return -EINVAL;
296 
297 	return 0;
298 }
299 
300 /**
301  * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
302  * @netdev: the net device on which the unsync is happening
303  * @addr: mac address to unsync
304  *
305  * This is a callback function which is called by the in kernel device unsync
306  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
307  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
308  * delete the mac filters from the hardware.
309  */
310 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
311 {
312 	struct ice_netdev_priv *np = netdev_priv(netdev);
313 	struct ice_vsi *vsi = np->vsi;
314 
315 	if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
316 		return -EINVAL;
317 
318 	return 0;
319 }
320 
321 /**
322  * ice_free_fltr_list - free filter lists helper
323  * @dev: pointer to the device struct
324  * @h: pointer to the list head to be freed
325  *
326  * Helper function to free filter lists previously created using
327  * ice_add_mac_to_list
328  */
329 static void ice_free_fltr_list(struct device *dev, struct list_head *h)
330 {
331 	struct ice_fltr_list_entry *e, *tmp;
332 
333 	list_for_each_entry_safe(e, tmp, h, list_entry) {
334 		list_del(&e->list_entry);
335 		devm_kfree(dev, e);
336 	}
337 }
338 
339 /**
340  * ice_vsi_fltr_changed - check if filter state changed
341  * @vsi: VSI to be checked
342  *
343  * returns true if filter state has changed, false otherwise.
344  */
345 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
346 {
347 	return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
348 	       test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
349 	       test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
350 }
351 
352 /**
353  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
354  * @vsi: VSI to enable or disable VLAN pruning on
355  * @ena: set to true to enable VLAN pruning and false to disable it
356  *
357  * returns 0 if VSI is updated, negative otherwise
358  */
359 static int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
360 {
361 	struct ice_vsi_ctx *ctxt;
362 	struct device *dev;
363 	int status;
364 
365 	if (!vsi)
366 		return -EINVAL;
367 
368 	dev = &vsi->back->pdev->dev;
369 	ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
370 	if (!ctxt)
371 		return -ENOMEM;
372 
373 	ctxt->info = vsi->info;
374 
375 	if (ena) {
376 		ctxt->info.sec_flags |=
377 			ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
378 			ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
379 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
380 	} else {
381 		ctxt->info.sec_flags &=
382 			~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
383 			  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
384 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
385 	}
386 
387 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
388 						ICE_AQ_VSI_PROP_SW_VALID);
389 	ctxt->vsi_num = vsi->vsi_num;
390 	status = ice_aq_update_vsi(&vsi->back->hw, ctxt, NULL);
391 	if (status) {
392 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI %d failed, err = %d, aq_err = %d\n",
393 			   ena ? "Ena" : "Dis", vsi->vsi_num, status,
394 			   vsi->back->hw.adminq.sq_last_status);
395 		goto err_out;
396 	}
397 
398 	vsi->info.sec_flags = ctxt->info.sec_flags;
399 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
400 
401 	devm_kfree(dev, ctxt);
402 	return 0;
403 
404 err_out:
405 	devm_kfree(dev, ctxt);
406 	return -EIO;
407 }
408 
409 /**
410  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
411  * @vsi: ptr to the VSI
412  *
413  * Push any outstanding VSI filter changes through the AdminQ.
414  */
415 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
416 {
417 	struct device *dev = &vsi->back->pdev->dev;
418 	struct net_device *netdev = vsi->netdev;
419 	bool promisc_forced_on = false;
420 	struct ice_pf *pf = vsi->back;
421 	struct ice_hw *hw = &pf->hw;
422 	enum ice_status status = 0;
423 	u32 changed_flags = 0;
424 	int err = 0;
425 
426 	if (!vsi->netdev)
427 		return -EINVAL;
428 
429 	while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
430 		usleep_range(1000, 2000);
431 
432 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
433 	vsi->current_netdev_flags = vsi->netdev->flags;
434 
435 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
436 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
437 
438 	if (ice_vsi_fltr_changed(vsi)) {
439 		clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
440 		clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
441 		clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
442 
443 		/* grab the netdev's addr_list_lock */
444 		netif_addr_lock_bh(netdev);
445 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
446 			      ice_add_mac_to_unsync_list);
447 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
448 			      ice_add_mac_to_unsync_list);
449 		/* our temp lists are populated. release lock */
450 		netif_addr_unlock_bh(netdev);
451 	}
452 
453 	/* Remove mac addresses in the unsync list */
454 	status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
455 	ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
456 	if (status) {
457 		netdev_err(netdev, "Failed to delete MAC filters\n");
458 		/* if we failed because of alloc failures, just bail */
459 		if (status == ICE_ERR_NO_MEMORY) {
460 			err = -ENOMEM;
461 			goto out;
462 		}
463 	}
464 
465 	/* Add mac addresses in the sync list */
466 	status = ice_add_mac(hw, &vsi->tmp_sync_list);
467 	ice_free_fltr_list(dev, &vsi->tmp_sync_list);
468 	if (status) {
469 		netdev_err(netdev, "Failed to add MAC filters\n");
470 		/* If there is no more space for new umac filters, vsi
471 		 * should go into promiscuous mode. There should be some
472 		 * space reserved for promiscuous filters.
473 		 */
474 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
475 		    !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
476 				      vsi->state)) {
477 			promisc_forced_on = true;
478 			netdev_warn(netdev,
479 				    "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
480 				    vsi->vsi_num);
481 		} else {
482 			err = -EIO;
483 			goto out;
484 		}
485 	}
486 	/* check for changes in promiscuous modes */
487 	if (changed_flags & IFF_ALLMULTI)
488 		netdev_warn(netdev, "Unsupported configuration\n");
489 
490 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
491 	    test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
492 		clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
493 		if (vsi->current_netdev_flags & IFF_PROMISC) {
494 			/* Apply TX filter rule to get traffic from VMs */
495 			status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
496 						  ICE_FLTR_TX);
497 			if (status) {
498 				netdev_err(netdev, "Error setting default VSI %i tx rule\n",
499 					   vsi->vsi_num);
500 				vsi->current_netdev_flags &= ~IFF_PROMISC;
501 				err = -EIO;
502 				goto out_promisc;
503 			}
504 			/* Apply RX filter rule to get traffic from wire */
505 			status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
506 						  ICE_FLTR_RX);
507 			if (status) {
508 				netdev_err(netdev, "Error setting default VSI %i rx rule\n",
509 					   vsi->vsi_num);
510 				vsi->current_netdev_flags &= ~IFF_PROMISC;
511 				err = -EIO;
512 				goto out_promisc;
513 			}
514 		} else {
515 			/* Clear TX filter rule to stop traffic from VMs */
516 			status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
517 						  ICE_FLTR_TX);
518 			if (status) {
519 				netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
520 					   vsi->vsi_num);
521 				vsi->current_netdev_flags |= IFF_PROMISC;
522 				err = -EIO;
523 				goto out_promisc;
524 			}
525 			/* Clear filter RX to remove traffic from wire */
526 			status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
527 						  ICE_FLTR_RX);
528 			if (status) {
529 				netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
530 					   vsi->vsi_num);
531 				vsi->current_netdev_flags |= IFF_PROMISC;
532 				err = -EIO;
533 				goto out_promisc;
534 			}
535 		}
536 	}
537 	goto exit;
538 
539 out_promisc:
540 	set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
541 	goto exit;
542 out:
543 	/* if something went wrong then set the changed flag so we try again */
544 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
545 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
546 exit:
547 	clear_bit(__ICE_CFG_BUSY, vsi->state);
548 	return err;
549 }
550 
551 /**
552  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
553  * @pf: board private structure
554  */
555 static void ice_sync_fltr_subtask(struct ice_pf *pf)
556 {
557 	int v;
558 
559 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
560 		return;
561 
562 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
563 
564 	for (v = 0; v < pf->num_alloc_vsi; v++)
565 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
566 		    ice_vsi_sync_fltr(pf->vsi[v])) {
567 			/* come back and try again later */
568 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
569 			break;
570 		}
571 }
572 
573 /**
574  * ice_is_reset_recovery_pending - schedule a reset
575  * @state: pf state field
576  */
577 static bool ice_is_reset_recovery_pending(unsigned long int *state)
578 {
579 	return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
580 }
581 
582 /**
583  * ice_prepare_for_reset - prep for the core to reset
584  * @pf: board private structure
585  *
586  * Inform or close all dependent features in prep for reset.
587  */
588 static void
589 ice_prepare_for_reset(struct ice_pf *pf)
590 {
591 	struct ice_hw *hw = &pf->hw;
592 
593 	/* disable the VSIs and their queues that are not already DOWN */
594 	ice_pf_dis_all_vsi(pf);
595 
596 	ice_shutdown_all_ctrlq(hw);
597 
598 	set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
599 }
600 
601 /**
602  * ice_do_reset - Initiate one of many types of resets
603  * @pf: board private structure
604  * @reset_type: reset type requested
605  * before this function was called.
606  */
607 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
608 {
609 	struct device *dev = &pf->pdev->dev;
610 	struct ice_hw *hw = &pf->hw;
611 
612 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
613 	WARN_ON(in_interrupt());
614 
615 	/* PFR is a bit of a special case because it doesn't result in an OICR
616 	 * interrupt. Set pending bit here which otherwise gets set in the
617 	 * OICR handler.
618 	 */
619 	if (reset_type == ICE_RESET_PFR)
620 		set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
621 
622 	ice_prepare_for_reset(pf);
623 
624 	/* trigger the reset */
625 	if (ice_reset(hw, reset_type)) {
626 		dev_err(dev, "reset %d failed\n", reset_type);
627 		set_bit(__ICE_RESET_FAILED, pf->state);
628 		clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
629 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
630 		return;
631 	}
632 
633 	/* PFR is a bit of a special case because it doesn't result in an OICR
634 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
635 	 * associated state bits.
636 	 */
637 	if (reset_type == ICE_RESET_PFR) {
638 		pf->pfr_count++;
639 		ice_rebuild(pf);
640 		clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
641 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
642 	}
643 }
644 
645 /**
646  * ice_reset_subtask - Set up for resetting the device and driver
647  * @pf: board private structure
648  */
649 static void ice_reset_subtask(struct ice_pf *pf)
650 {
651 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
652 
653 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
654 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
655 	 * of reset is pending and sets bits in pf->state indicating the reset
656 	 * type and __ICE_RESET_RECOVERY_PENDING.  So, if the latter bit is set
657 	 * prepare for pending reset if not already (for PF software-initiated
658 	 * global resets the software should already be prepared for it as
659 	 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
660 	 * by firmware or software on other PFs, that bit is not set so prepare
661 	 * for the reset now), poll for reset done, rebuild and return.
662 	 */
663 	if (ice_is_reset_recovery_pending(pf->state)) {
664 		clear_bit(__ICE_GLOBR_RECV, pf->state);
665 		clear_bit(__ICE_CORER_RECV, pf->state);
666 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
667 			ice_prepare_for_reset(pf);
668 
669 		/* make sure we are ready to rebuild */
670 		if (ice_check_reset(&pf->hw)) {
671 			set_bit(__ICE_RESET_FAILED, pf->state);
672 		} else {
673 			/* done with reset. start rebuild */
674 			pf->hw.reset_ongoing = false;
675 			ice_rebuild(pf);
676 			/* clear bit to resume normal operations, but
677 			 * ICE_NEEDS_RESTART bit is set incase rebuild failed
678 			 */
679 			clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
680 			clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
681 		}
682 
683 		return;
684 	}
685 
686 	/* No pending resets to finish processing. Check for new resets */
687 	if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
688 		reset_type = ICE_RESET_PFR;
689 	if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
690 		reset_type = ICE_RESET_CORER;
691 	if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
692 		reset_type = ICE_RESET_GLOBR;
693 	/* If no valid reset type requested just return */
694 	if (reset_type == ICE_RESET_INVAL)
695 		return;
696 
697 	/* reset if not already down or busy */
698 	if (!test_bit(__ICE_DOWN, pf->state) &&
699 	    !test_bit(__ICE_CFG_BUSY, pf->state)) {
700 		ice_do_reset(pf, reset_type);
701 	}
702 }
703 
704 /**
705  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
706  * @pf: board private structure
707  */
708 static void ice_watchdog_subtask(struct ice_pf *pf)
709 {
710 	int i;
711 
712 	/* if interface is down do nothing */
713 	if (test_bit(__ICE_DOWN, pf->state) ||
714 	    test_bit(__ICE_CFG_BUSY, pf->state))
715 		return;
716 
717 	/* make sure we don't do these things too often */
718 	if (time_before(jiffies,
719 			pf->serv_tmr_prev + pf->serv_tmr_period))
720 		return;
721 
722 	pf->serv_tmr_prev = jiffies;
723 
724 	/* Update the stats for active netdevs so the network stack
725 	 * can look at updated numbers whenever it cares to
726 	 */
727 	ice_update_pf_stats(pf);
728 	for (i = 0; i < pf->num_alloc_vsi; i++)
729 		if (pf->vsi[i] && pf->vsi[i]->netdev)
730 			ice_update_vsi_stats(pf->vsi[i]);
731 }
732 
733 /**
734  * ice_print_link_msg - print link up or down message
735  * @vsi: the VSI whose link status is being queried
736  * @isup: boolean for if the link is now up or down
737  */
738 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
739 {
740 	const char *speed;
741 	const char *fc;
742 
743 	if (vsi->current_isup == isup)
744 		return;
745 
746 	vsi->current_isup = isup;
747 
748 	if (!isup) {
749 		netdev_info(vsi->netdev, "NIC Link is Down\n");
750 		return;
751 	}
752 
753 	switch (vsi->port_info->phy.link_info.link_speed) {
754 	case ICE_AQ_LINK_SPEED_40GB:
755 		speed = "40 G";
756 		break;
757 	case ICE_AQ_LINK_SPEED_25GB:
758 		speed = "25 G";
759 		break;
760 	case ICE_AQ_LINK_SPEED_20GB:
761 		speed = "20 G";
762 		break;
763 	case ICE_AQ_LINK_SPEED_10GB:
764 		speed = "10 G";
765 		break;
766 	case ICE_AQ_LINK_SPEED_5GB:
767 		speed = "5 G";
768 		break;
769 	case ICE_AQ_LINK_SPEED_2500MB:
770 		speed = "2.5 G";
771 		break;
772 	case ICE_AQ_LINK_SPEED_1000MB:
773 		speed = "1 G";
774 		break;
775 	case ICE_AQ_LINK_SPEED_100MB:
776 		speed = "100 M";
777 		break;
778 	default:
779 		speed = "Unknown";
780 		break;
781 	}
782 
783 	switch (vsi->port_info->fc.current_mode) {
784 	case ICE_FC_FULL:
785 		fc = "RX/TX";
786 		break;
787 	case ICE_FC_TX_PAUSE:
788 		fc = "TX";
789 		break;
790 	case ICE_FC_RX_PAUSE:
791 		fc = "RX";
792 		break;
793 	default:
794 		fc = "Unknown";
795 		break;
796 	}
797 
798 	netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
799 		    speed, fc);
800 }
801 
802 /**
803  * ice_init_link_events - enable/initialize link events
804  * @pi: pointer to the port_info instance
805  *
806  * Returns -EIO on failure, 0 on success
807  */
808 static int ice_init_link_events(struct ice_port_info *pi)
809 {
810 	u16 mask;
811 
812 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
813 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
814 
815 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
816 		dev_dbg(ice_hw_to_dev(pi->hw),
817 			"Failed to set link event mask for port %d\n",
818 			pi->lport);
819 		return -EIO;
820 	}
821 
822 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
823 		dev_dbg(ice_hw_to_dev(pi->hw),
824 			"Failed to enable link events for port %d\n",
825 			pi->lport);
826 		return -EIO;
827 	}
828 
829 	return 0;
830 }
831 
832 /**
833  * ice_vsi_link_event - update the vsi's netdev
834  * @vsi: the vsi on which the link event occurred
835  * @link_up: whether or not the vsi needs to be set up or down
836  */
837 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
838 {
839 	if (!vsi || test_bit(__ICE_DOWN, vsi->state))
840 		return;
841 
842 	if (vsi->type == ICE_VSI_PF) {
843 		if (!vsi->netdev) {
844 			dev_dbg(&vsi->back->pdev->dev,
845 				"vsi->netdev is not initialized!\n");
846 			return;
847 		}
848 		if (link_up) {
849 			netif_carrier_on(vsi->netdev);
850 			netif_tx_wake_all_queues(vsi->netdev);
851 		} else {
852 			netif_carrier_off(vsi->netdev);
853 			netif_tx_stop_all_queues(vsi->netdev);
854 		}
855 	}
856 }
857 
858 /**
859  * ice_link_event - process the link event
860  * @pf: pf that the link event is associated with
861  * @pi: port_info for the port that the link event is associated with
862  *
863  * Returns -EIO if ice_get_link_status() fails
864  * Returns 0 on success
865  */
866 static int
867 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
868 {
869 	u8 new_link_speed, old_link_speed;
870 	struct ice_phy_info *phy_info;
871 	bool new_link_same_as_old;
872 	bool new_link, old_link;
873 	u8 lport;
874 	u16 v;
875 
876 	phy_info = &pi->phy;
877 	phy_info->link_info_old = phy_info->link_info;
878 	/* Force ice_get_link_status() to update link info */
879 	phy_info->get_link_info = true;
880 
881 	old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
882 	old_link_speed = phy_info->link_info_old.link_speed;
883 
884 	lport = pi->lport;
885 	if (ice_get_link_status(pi, &new_link)) {
886 		dev_dbg(&pf->pdev->dev,
887 			"Could not get link status for port %d\n", lport);
888 		return -EIO;
889 	}
890 
891 	new_link_speed = phy_info->link_info.link_speed;
892 
893 	new_link_same_as_old = (new_link == old_link &&
894 				new_link_speed == old_link_speed);
895 
896 	ice_for_each_vsi(pf, v) {
897 		struct ice_vsi *vsi = pf->vsi[v];
898 
899 		if (!vsi || !vsi->port_info)
900 			continue;
901 
902 		if (new_link_same_as_old &&
903 		    (test_bit(__ICE_DOWN, vsi->state) ||
904 		    new_link == netif_carrier_ok(vsi->netdev)))
905 			continue;
906 
907 		if (vsi->port_info->lport == lport) {
908 			ice_print_link_msg(vsi, new_link);
909 			ice_vsi_link_event(vsi, new_link);
910 		}
911 	}
912 
913 	return 0;
914 }
915 
916 /**
917  * ice_handle_link_event - handle link event via ARQ
918  * @pf: pf that the link event is associated with
919  *
920  * Return -EINVAL if port_info is null
921  * Return status on succes
922  */
923 static int ice_handle_link_event(struct ice_pf *pf)
924 {
925 	struct ice_port_info *port_info;
926 	int status;
927 
928 	port_info = pf->hw.port_info;
929 	if (!port_info)
930 		return -EINVAL;
931 
932 	status = ice_link_event(pf, port_info);
933 	if (status)
934 		dev_dbg(&pf->pdev->dev,
935 			"Could not process link event, error %d\n", status);
936 
937 	return status;
938 }
939 
940 /**
941  * __ice_clean_ctrlq - helper function to clean controlq rings
942  * @pf: ptr to struct ice_pf
943  * @q_type: specific Control queue type
944  */
945 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
946 {
947 	struct ice_rq_event_info event;
948 	struct ice_hw *hw = &pf->hw;
949 	struct ice_ctl_q_info *cq;
950 	u16 pending, i = 0;
951 	const char *qtype;
952 	u32 oldval, val;
953 
954 	/* Do not clean control queue if/when PF reset fails */
955 	if (test_bit(__ICE_RESET_FAILED, pf->state))
956 		return 0;
957 
958 	switch (q_type) {
959 	case ICE_CTL_Q_ADMIN:
960 		cq = &hw->adminq;
961 		qtype = "Admin";
962 		break;
963 	default:
964 		dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
965 			 q_type);
966 		return 0;
967 	}
968 
969 	/* check for error indications - PF_xx_AxQLEN register layout for
970 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
971 	 */
972 	val = rd32(hw, cq->rq.len);
973 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
974 		   PF_FW_ARQLEN_ARQCRIT_M)) {
975 		oldval = val;
976 		if (val & PF_FW_ARQLEN_ARQVFE_M)
977 			dev_dbg(&pf->pdev->dev,
978 				"%s Receive Queue VF Error detected\n", qtype);
979 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
980 			dev_dbg(&pf->pdev->dev,
981 				"%s Receive Queue Overflow Error detected\n",
982 				qtype);
983 		}
984 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
985 			dev_dbg(&pf->pdev->dev,
986 				"%s Receive Queue Critical Error detected\n",
987 				qtype);
988 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
989 			 PF_FW_ARQLEN_ARQCRIT_M);
990 		if (oldval != val)
991 			wr32(hw, cq->rq.len, val);
992 	}
993 
994 	val = rd32(hw, cq->sq.len);
995 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
996 		   PF_FW_ATQLEN_ATQCRIT_M)) {
997 		oldval = val;
998 		if (val & PF_FW_ATQLEN_ATQVFE_M)
999 			dev_dbg(&pf->pdev->dev,
1000 				"%s Send Queue VF Error detected\n", qtype);
1001 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1002 			dev_dbg(&pf->pdev->dev,
1003 				"%s Send Queue Overflow Error detected\n",
1004 				qtype);
1005 		}
1006 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1007 			dev_dbg(&pf->pdev->dev,
1008 				"%s Send Queue Critical Error detected\n",
1009 				qtype);
1010 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1011 			 PF_FW_ATQLEN_ATQCRIT_M);
1012 		if (oldval != val)
1013 			wr32(hw, cq->sq.len, val);
1014 	}
1015 
1016 	event.buf_len = cq->rq_buf_size;
1017 	event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
1018 				     GFP_KERNEL);
1019 	if (!event.msg_buf)
1020 		return 0;
1021 
1022 	do {
1023 		enum ice_status ret;
1024 		u16 opcode;
1025 
1026 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1027 		if (ret == ICE_ERR_AQ_NO_WORK)
1028 			break;
1029 		if (ret) {
1030 			dev_err(&pf->pdev->dev,
1031 				"%s Receive Queue event error %d\n", qtype,
1032 				ret);
1033 			break;
1034 		}
1035 
1036 		opcode = le16_to_cpu(event.desc.opcode);
1037 
1038 		switch (opcode) {
1039 		case ice_aqc_opc_get_link_status:
1040 			if (ice_handle_link_event(pf))
1041 				dev_err(&pf->pdev->dev,
1042 					"Could not handle link event\n");
1043 			break;
1044 		case ice_aqc_opc_fw_logging:
1045 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1046 			break;
1047 		default:
1048 			dev_dbg(&pf->pdev->dev,
1049 				"%s Receive Queue unknown event 0x%04x ignored\n",
1050 				qtype, opcode);
1051 			break;
1052 		}
1053 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1054 
1055 	devm_kfree(&pf->pdev->dev, event.msg_buf);
1056 
1057 	return pending && (i == ICE_DFLT_IRQ_WORK);
1058 }
1059 
1060 /**
1061  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1062  * @hw: pointer to hardware info
1063  * @cq: control queue information
1064  *
1065  * returns true if there are pending messages in a queue, false if there aren't
1066  */
1067 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1068 {
1069 	u16 ntu;
1070 
1071 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1072 	return cq->rq.next_to_clean != ntu;
1073 }
1074 
1075 /**
1076  * ice_clean_adminq_subtask - clean the AdminQ rings
1077  * @pf: board private structure
1078  */
1079 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1080 {
1081 	struct ice_hw *hw = &pf->hw;
1082 
1083 	if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1084 		return;
1085 
1086 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1087 		return;
1088 
1089 	clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1090 
1091 	/* There might be a situation where new messages arrive to a control
1092 	 * queue between processing the last message and clearing the
1093 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1094 	 * ice_ctrlq_pending) and process new messages if any.
1095 	 */
1096 	if (ice_ctrlq_pending(hw, &hw->adminq))
1097 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1098 
1099 	ice_flush(hw);
1100 }
1101 
1102 /**
1103  * ice_service_task_schedule - schedule the service task to wake up
1104  * @pf: board private structure
1105  *
1106  * If not already scheduled, this puts the task into the work queue.
1107  */
1108 static void ice_service_task_schedule(struct ice_pf *pf)
1109 {
1110 	if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1111 	    !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1112 	    !test_bit(__ICE_NEEDS_RESTART, pf->state))
1113 		queue_work(ice_wq, &pf->serv_task);
1114 }
1115 
1116 /**
1117  * ice_service_task_complete - finish up the service task
1118  * @pf: board private structure
1119  */
1120 static void ice_service_task_complete(struct ice_pf *pf)
1121 {
1122 	WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1123 
1124 	/* force memory (pf->state) to sync before next service task */
1125 	smp_mb__before_atomic();
1126 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1127 }
1128 
1129 /**
1130  * ice_service_task_stop - stop service task and cancel works
1131  * @pf: board private structure
1132  */
1133 static void ice_service_task_stop(struct ice_pf *pf)
1134 {
1135 	set_bit(__ICE_SERVICE_DIS, pf->state);
1136 
1137 	if (pf->serv_tmr.function)
1138 		del_timer_sync(&pf->serv_tmr);
1139 	if (pf->serv_task.func)
1140 		cancel_work_sync(&pf->serv_task);
1141 
1142 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1143 }
1144 
1145 /**
1146  * ice_service_timer - timer callback to schedule service task
1147  * @t: pointer to timer_list
1148  */
1149 static void ice_service_timer(struct timer_list *t)
1150 {
1151 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1152 
1153 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1154 	ice_service_task_schedule(pf);
1155 }
1156 
1157 /**
1158  * ice_handle_mdd_event - handle malicious driver detect event
1159  * @pf: pointer to the PF structure
1160  *
1161  * Called from service task. OICR interrupt handler indicates MDD event
1162  */
1163 static void ice_handle_mdd_event(struct ice_pf *pf)
1164 {
1165 	struct ice_hw *hw = &pf->hw;
1166 	bool mdd_detected = false;
1167 	u32 reg;
1168 
1169 	if (!test_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1170 		return;
1171 
1172 	/* find what triggered the MDD event */
1173 	reg = rd32(hw, GL_MDET_TX_PQM);
1174 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1175 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1176 				GL_MDET_TX_PQM_PF_NUM_S;
1177 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1178 				GL_MDET_TX_PQM_VF_NUM_S;
1179 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1180 				GL_MDET_TX_PQM_MAL_TYPE_S;
1181 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1182 				GL_MDET_TX_PQM_QNUM_S);
1183 
1184 		if (netif_msg_tx_err(pf))
1185 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1186 				 event, queue, pf_num, vf_num);
1187 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1188 		mdd_detected = true;
1189 	}
1190 
1191 	reg = rd32(hw, GL_MDET_TX_TCLAN);
1192 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1193 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1194 				GL_MDET_TX_TCLAN_PF_NUM_S;
1195 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1196 				GL_MDET_TX_TCLAN_VF_NUM_S;
1197 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1198 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1199 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1200 				GL_MDET_TX_TCLAN_QNUM_S);
1201 
1202 		if (netif_msg_rx_err(pf))
1203 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1204 				 event, queue, pf_num, vf_num);
1205 		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1206 		mdd_detected = true;
1207 	}
1208 
1209 	reg = rd32(hw, GL_MDET_RX);
1210 	if (reg & GL_MDET_RX_VALID_M) {
1211 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1212 				GL_MDET_RX_PF_NUM_S;
1213 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1214 				GL_MDET_RX_VF_NUM_S;
1215 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1216 				GL_MDET_RX_MAL_TYPE_S;
1217 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1218 				GL_MDET_RX_QNUM_S);
1219 
1220 		if (netif_msg_rx_err(pf))
1221 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1222 				 event, queue, pf_num, vf_num);
1223 		wr32(hw, GL_MDET_RX, 0xffffffff);
1224 		mdd_detected = true;
1225 	}
1226 
1227 	if (mdd_detected) {
1228 		bool pf_mdd_detected = false;
1229 
1230 		reg = rd32(hw, PF_MDET_TX_PQM);
1231 		if (reg & PF_MDET_TX_PQM_VALID_M) {
1232 			wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1233 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1234 			pf_mdd_detected = true;
1235 		}
1236 
1237 		reg = rd32(hw, PF_MDET_TX_TCLAN);
1238 		if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1239 			wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1240 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1241 			pf_mdd_detected = true;
1242 		}
1243 
1244 		reg = rd32(hw, PF_MDET_RX);
1245 		if (reg & PF_MDET_RX_VALID_M) {
1246 			wr32(hw, PF_MDET_RX, 0xFFFF);
1247 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1248 			pf_mdd_detected = true;
1249 		}
1250 		/* Queue belongs to the PF initiate a reset */
1251 		if (pf_mdd_detected) {
1252 			set_bit(__ICE_NEEDS_RESTART, pf->state);
1253 			ice_service_task_schedule(pf);
1254 		}
1255 	}
1256 
1257 	/* re-enable MDD interrupt cause */
1258 	clear_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1259 	reg = rd32(hw, PFINT_OICR_ENA);
1260 	reg |= PFINT_OICR_MAL_DETECT_M;
1261 	wr32(hw, PFINT_OICR_ENA, reg);
1262 	ice_flush(hw);
1263 }
1264 
1265 /**
1266  * ice_service_task - manage and run subtasks
1267  * @work: pointer to work_struct contained by the PF struct
1268  */
1269 static void ice_service_task(struct work_struct *work)
1270 {
1271 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1272 	unsigned long start_time = jiffies;
1273 
1274 	/* subtasks */
1275 
1276 	/* process reset requests first */
1277 	ice_reset_subtask(pf);
1278 
1279 	/* bail if a reset/recovery cycle is pending or rebuild failed */
1280 	if (ice_is_reset_recovery_pending(pf->state) ||
1281 	    test_bit(__ICE_SUSPENDED, pf->state) ||
1282 	    test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1283 		ice_service_task_complete(pf);
1284 		return;
1285 	}
1286 
1287 	ice_check_for_hang_subtask(pf);
1288 	ice_sync_fltr_subtask(pf);
1289 	ice_handle_mdd_event(pf);
1290 	ice_watchdog_subtask(pf);
1291 	ice_clean_adminq_subtask(pf);
1292 
1293 	/* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1294 	ice_service_task_complete(pf);
1295 
1296 	/* If the tasks have taken longer than one service timer period
1297 	 * or there is more work to be done, reset the service timer to
1298 	 * schedule the service task now.
1299 	 */
1300 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1301 	    test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1302 	    test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1303 		mod_timer(&pf->serv_tmr, jiffies);
1304 }
1305 
1306 /**
1307  * ice_set_ctrlq_len - helper function to set controlq length
1308  * @hw: pointer to the hw instance
1309  */
1310 static void ice_set_ctrlq_len(struct ice_hw *hw)
1311 {
1312 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
1313 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
1314 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1315 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1316 }
1317 
1318 /**
1319  * ice_irq_affinity_notify - Callback for affinity changes
1320  * @notify: context as to what irq was changed
1321  * @mask: the new affinity mask
1322  *
1323  * This is a callback function used by the irq_set_affinity_notifier function
1324  * so that we may register to receive changes to the irq affinity masks.
1325  */
1326 static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1327 				    const cpumask_t *mask)
1328 {
1329 	struct ice_q_vector *q_vector =
1330 		container_of(notify, struct ice_q_vector, affinity_notify);
1331 
1332 	cpumask_copy(&q_vector->affinity_mask, mask);
1333 }
1334 
1335 /**
1336  * ice_irq_affinity_release - Callback for affinity notifier release
1337  * @ref: internal core kernel usage
1338  *
1339  * This is a callback function used by the irq_set_affinity_notifier function
1340  * to inform the current notification subscriber that they will no longer
1341  * receive notifications.
1342  */
1343 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1344 
1345 /**
1346  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
1347  * @vsi: the VSI being un-configured
1348  */
1349 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
1350 {
1351 	struct ice_pf *pf = vsi->back;
1352 	struct ice_hw *hw = &pf->hw;
1353 	int base = vsi->base_vector;
1354 	u32 val;
1355 	int i;
1356 
1357 	/* disable interrupt causation from each queue */
1358 	if (vsi->tx_rings) {
1359 		ice_for_each_txq(vsi, i) {
1360 			if (vsi->tx_rings[i]) {
1361 				u16 reg;
1362 
1363 				reg = vsi->tx_rings[i]->reg_idx;
1364 				val = rd32(hw, QINT_TQCTL(reg));
1365 				val &= ~QINT_TQCTL_CAUSE_ENA_M;
1366 				wr32(hw, QINT_TQCTL(reg), val);
1367 			}
1368 		}
1369 	}
1370 
1371 	if (vsi->rx_rings) {
1372 		ice_for_each_rxq(vsi, i) {
1373 			if (vsi->rx_rings[i]) {
1374 				u16 reg;
1375 
1376 				reg = vsi->rx_rings[i]->reg_idx;
1377 				val = rd32(hw, QINT_RQCTL(reg));
1378 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
1379 				wr32(hw, QINT_RQCTL(reg), val);
1380 			}
1381 		}
1382 	}
1383 
1384 	/* disable each interrupt */
1385 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1386 		for (i = vsi->base_vector;
1387 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
1388 			wr32(hw, GLINT_DYN_CTL(i), 0);
1389 
1390 		ice_flush(hw);
1391 		for (i = 0; i < vsi->num_q_vectors; i++)
1392 			synchronize_irq(pf->msix_entries[i + base].vector);
1393 	}
1394 }
1395 
1396 /**
1397  * ice_vsi_ena_irq - Enable IRQ for the given VSI
1398  * @vsi: the VSI being configured
1399  */
1400 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1401 {
1402 	struct ice_pf *pf = vsi->back;
1403 	struct ice_hw *hw = &pf->hw;
1404 
1405 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1406 		int i;
1407 
1408 		for (i = 0; i < vsi->num_q_vectors; i++)
1409 			ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1410 	}
1411 
1412 	ice_flush(hw);
1413 	return 0;
1414 }
1415 
1416 /**
1417  * ice_vsi_delete - delete a VSI from the switch
1418  * @vsi: pointer to VSI being removed
1419  */
1420 static void ice_vsi_delete(struct ice_vsi *vsi)
1421 {
1422 	struct ice_pf *pf = vsi->back;
1423 	struct ice_vsi_ctx ctxt;
1424 	enum ice_status status;
1425 
1426 	ctxt.vsi_num = vsi->vsi_num;
1427 
1428 	memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
1429 
1430 	status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
1431 	if (status)
1432 		dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
1433 			vsi->vsi_num);
1434 }
1435 
1436 /**
1437  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1438  * @vsi: the VSI being configured
1439  * @basename: name for the vector
1440  */
1441 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1442 {
1443 	int q_vectors = vsi->num_q_vectors;
1444 	struct ice_pf *pf = vsi->back;
1445 	int base = vsi->base_vector;
1446 	int rx_int_idx = 0;
1447 	int tx_int_idx = 0;
1448 	int vector, err;
1449 	int irq_num;
1450 
1451 	for (vector = 0; vector < q_vectors; vector++) {
1452 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1453 
1454 		irq_num = pf->msix_entries[base + vector].vector;
1455 
1456 		if (q_vector->tx.ring && q_vector->rx.ring) {
1457 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1458 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1459 			tx_int_idx++;
1460 		} else if (q_vector->rx.ring) {
1461 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1462 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
1463 		} else if (q_vector->tx.ring) {
1464 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1465 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
1466 		} else {
1467 			/* skip this unused q_vector */
1468 			continue;
1469 		}
1470 		err = devm_request_irq(&pf->pdev->dev,
1471 				       pf->msix_entries[base + vector].vector,
1472 				       vsi->irq_handler, 0, q_vector->name,
1473 				       q_vector);
1474 		if (err) {
1475 			netdev_err(vsi->netdev,
1476 				   "MSIX request_irq failed, error: %d\n", err);
1477 			goto free_q_irqs;
1478 		}
1479 
1480 		/* register for affinity change notifications */
1481 		q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1482 		q_vector->affinity_notify.release = ice_irq_affinity_release;
1483 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1484 
1485 		/* assign the mask for this irq */
1486 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1487 	}
1488 
1489 	vsi->irqs_ready = true;
1490 	return 0;
1491 
1492 free_q_irqs:
1493 	while (vector) {
1494 		vector--;
1495 		irq_num = pf->msix_entries[base + vector].vector,
1496 		irq_set_affinity_notifier(irq_num, NULL);
1497 		irq_set_affinity_hint(irq_num, NULL);
1498 		devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1499 	}
1500 	return err;
1501 }
1502 
1503 /**
1504  * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
1505  * @vsi: the VSI being configured
1506  */
1507 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
1508 {
1509 	struct ice_hw_common_caps *cap;
1510 	struct ice_pf *pf = vsi->back;
1511 
1512 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1513 		vsi->rss_size = 1;
1514 		return;
1515 	}
1516 
1517 	cap = &pf->hw.func_caps.common_cap;
1518 	switch (vsi->type) {
1519 	case ICE_VSI_PF:
1520 		/* PF VSI will inherit RSS instance of PF */
1521 		vsi->rss_table_size = cap->rss_table_size;
1522 		vsi->rss_size = min_t(int, num_online_cpus(),
1523 				      BIT(cap->rss_table_entry_width));
1524 		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
1525 		break;
1526 	default:
1527 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1528 		break;
1529 	}
1530 }
1531 
1532 /**
1533  * ice_vsi_setup_q_map - Setup a VSI queue map
1534  * @vsi: the VSI being configured
1535  * @ctxt: VSI context structure
1536  */
1537 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1538 {
1539 	u16 offset = 0, qmap = 0, numq_tc;
1540 	u16 pow = 0, max_rss = 0, qcount;
1541 	u16 qcount_tx = vsi->alloc_txq;
1542 	u16 qcount_rx = vsi->alloc_rxq;
1543 	bool ena_tc0 = false;
1544 	int i;
1545 
1546 	/* at least TC0 should be enabled by default */
1547 	if (vsi->tc_cfg.numtc) {
1548 		if (!(vsi->tc_cfg.ena_tc & BIT(0)))
1549 			ena_tc0 =  true;
1550 	} else {
1551 		ena_tc0 =  true;
1552 	}
1553 
1554 	if (ena_tc0) {
1555 		vsi->tc_cfg.numtc++;
1556 		vsi->tc_cfg.ena_tc |= 1;
1557 	}
1558 
1559 	numq_tc = qcount_rx / vsi->tc_cfg.numtc;
1560 
1561 	/* TC mapping is a function of the number of Rx queues assigned to the
1562 	 * VSI for each traffic class and the offset of these queues.
1563 	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1564 	 * queues allocated to TC0. No:of queues is a power-of-2.
1565 	 *
1566 	 * If TC is not enabled, the queue offset is set to 0, and allocate one
1567 	 * queue, this way, traffic for the given TC will be sent to the default
1568 	 * queue.
1569 	 *
1570 	 * Setup number and offset of Rx queues for all TCs for the VSI
1571 	 */
1572 
1573 	/* qcount will change if RSS is enabled */
1574 	if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
1575 		if (vsi->type == ICE_VSI_PF)
1576 			max_rss = ICE_MAX_LG_RSS_QS;
1577 		else
1578 			max_rss = ICE_MAX_SMALL_RSS_QS;
1579 
1580 		qcount = min_t(int, numq_tc, max_rss);
1581 		qcount = min_t(int, qcount, vsi->rss_size);
1582 	} else {
1583 		qcount = numq_tc;
1584 	}
1585 
1586 	/* find the (rounded up) power-of-2 of qcount */
1587 	pow = order_base_2(qcount);
1588 
1589 	for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1590 		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1591 			/* TC is not enabled */
1592 			vsi->tc_cfg.tc_info[i].qoffset = 0;
1593 			vsi->tc_cfg.tc_info[i].qcount = 1;
1594 			ctxt->info.tc_mapping[i] = 0;
1595 			continue;
1596 		}
1597 
1598 		/* TC is enabled */
1599 		vsi->tc_cfg.tc_info[i].qoffset = offset;
1600 		vsi->tc_cfg.tc_info[i].qcount = qcount;
1601 
1602 		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1603 			ICE_AQ_VSI_TC_Q_OFFSET_M) |
1604 			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1605 			 ICE_AQ_VSI_TC_Q_NUM_M);
1606 		offset += qcount;
1607 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1608 	}
1609 
1610 	vsi->num_txq = qcount_tx;
1611 	vsi->num_rxq = offset;
1612 
1613 	/* Rx queue mapping */
1614 	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1615 	/* q_mapping buffer holds the info for the first queue allocated for
1616 	 * this VSI in the PF space and also the number of queues associated
1617 	 * with this VSI.
1618 	 */
1619 	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1620 	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1621 }
1622 
1623 /**
1624  * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
1625  * @ctxt: the VSI context being set
1626  *
1627  * This initializes a default VSI context for all sections except the Queues.
1628  */
1629 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
1630 {
1631 	u32 table = 0;
1632 
1633 	memset(&ctxt->info, 0, sizeof(ctxt->info));
1634 	/* VSI's should be allocated from shared pool */
1635 	ctxt->alloc_from_pool = true;
1636 	/* Src pruning enabled by default */
1637 	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
1638 	/* Traffic from VSI can be sent to LAN */
1639 	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1640 
1641 	/* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
1642 	 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
1643 	 * packets untagged/tagged.
1644 	 */
1645 	ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
1646 				  ICE_AQ_VSI_VLAN_MODE_M) >>
1647 				 ICE_AQ_VSI_VLAN_MODE_S);
1648 
1649 	/* Have 1:1 UP mapping for both ingress/egress tables */
1650 	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1651 	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1652 	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1653 	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1654 	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1655 	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1656 	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1657 	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1658 	ctxt->info.ingress_table = cpu_to_le32(table);
1659 	ctxt->info.egress_table = cpu_to_le32(table);
1660 	/* Have 1:1 UP mapping for outer to inner UP table */
1661 	ctxt->info.outer_up_table = cpu_to_le32(table);
1662 	/* No Outer tag support outer_tag_flags remains to zero */
1663 }
1664 
1665 /**
1666  * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1667  * @ctxt: the VSI context being set
1668  * @vsi: the VSI being configured
1669  */
1670 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1671 {
1672 	u8 lut_type, hash_type;
1673 
1674 	switch (vsi->type) {
1675 	case ICE_VSI_PF:
1676 		/* PF VSI will inherit RSS instance of PF */
1677 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1678 		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1679 		break;
1680 	default:
1681 		dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1682 			 vsi->type);
1683 		return;
1684 	}
1685 
1686 	ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1687 				ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1688 				((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1689 				 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1690 }
1691 
1692 /**
1693  * ice_vsi_init - Create and initialize a VSI
1694  * @vsi: the VSI being configured
1695  *
1696  * This initializes a VSI context depending on the VSI type to be added and
1697  * passes it down to the add_vsi aq command to create a new VSI.
1698  */
1699 static int ice_vsi_init(struct ice_vsi *vsi)
1700 {
1701 	struct ice_vsi_ctx ctxt = { 0 };
1702 	struct ice_pf *pf = vsi->back;
1703 	struct ice_hw *hw = &pf->hw;
1704 	int ret = 0;
1705 
1706 	switch (vsi->type) {
1707 	case ICE_VSI_PF:
1708 		ctxt.flags = ICE_AQ_VSI_TYPE_PF;
1709 		break;
1710 	default:
1711 		return -ENODEV;
1712 	}
1713 
1714 	ice_set_dflt_vsi_ctx(&ctxt);
1715 	/* if the switch is in VEB mode, allow VSI loopback */
1716 	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1717 		ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1718 
1719 	/* Set LUT type and HASH type if RSS is enabled */
1720 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1721 		ice_set_rss_vsi_ctx(&ctxt, vsi);
1722 
1723 	ctxt.info.sw_id = vsi->port_info->sw_id;
1724 	ice_vsi_setup_q_map(vsi, &ctxt);
1725 
1726 	ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
1727 	if (ret) {
1728 		dev_err(&pf->pdev->dev,
1729 			"Add VSI failed, err %d\n", ret);
1730 		return -EIO;
1731 	}
1732 
1733 	/* keep context for update VSI operations */
1734 	vsi->info = ctxt.info;
1735 
1736 	/* record VSI number returned */
1737 	vsi->vsi_num = ctxt.vsi_num;
1738 
1739 	return ret;
1740 }
1741 
1742 /**
1743  * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1744  * @vsi: the VSI being cleaned up
1745  */
1746 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1747 {
1748 	struct ice_pf *pf = vsi->back;
1749 	u16 vector = vsi->base_vector;
1750 	struct ice_hw *hw = &pf->hw;
1751 	u32 txq = 0;
1752 	u32 rxq = 0;
1753 	int i, q;
1754 
1755 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1756 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1757 
1758 		wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1759 		wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1760 		for (q = 0; q < q_vector->num_ring_tx; q++) {
1761 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1762 			txq++;
1763 		}
1764 
1765 		for (q = 0; q < q_vector->num_ring_rx; q++) {
1766 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1767 			rxq++;
1768 		}
1769 	}
1770 
1771 	ice_flush(hw);
1772 }
1773 
1774 /**
1775  * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1776  * @vsi: the VSI having rings deallocated
1777  */
1778 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1779 {
1780 	int i;
1781 
1782 	if (vsi->tx_rings) {
1783 		for (i = 0; i < vsi->alloc_txq; i++) {
1784 			if (vsi->tx_rings[i]) {
1785 				kfree_rcu(vsi->tx_rings[i], rcu);
1786 				vsi->tx_rings[i] = NULL;
1787 			}
1788 		}
1789 	}
1790 	if (vsi->rx_rings) {
1791 		for (i = 0; i < vsi->alloc_rxq; i++) {
1792 			if (vsi->rx_rings[i]) {
1793 				kfree_rcu(vsi->rx_rings[i], rcu);
1794 				vsi->rx_rings[i] = NULL;
1795 			}
1796 		}
1797 	}
1798 }
1799 
1800 /**
1801  * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1802  * @vsi: VSI which is having rings allocated
1803  */
1804 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1805 {
1806 	struct ice_pf *pf = vsi->back;
1807 	int i;
1808 
1809 	/* Allocate tx_rings */
1810 	for (i = 0; i < vsi->alloc_txq; i++) {
1811 		struct ice_ring *ring;
1812 
1813 		/* allocate with kzalloc(), free with kfree_rcu() */
1814 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1815 
1816 		if (!ring)
1817 			goto err_out;
1818 
1819 		ring->q_index = i;
1820 		ring->reg_idx = vsi->txq_map[i];
1821 		ring->ring_active = false;
1822 		ring->vsi = vsi;
1823 		ring->netdev = vsi->netdev;
1824 		ring->dev = &pf->pdev->dev;
1825 		ring->count = vsi->num_desc;
1826 
1827 		vsi->tx_rings[i] = ring;
1828 	}
1829 
1830 	/* Allocate rx_rings */
1831 	for (i = 0; i < vsi->alloc_rxq; i++) {
1832 		struct ice_ring *ring;
1833 
1834 		/* allocate with kzalloc(), free with kfree_rcu() */
1835 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1836 		if (!ring)
1837 			goto err_out;
1838 
1839 		ring->q_index = i;
1840 		ring->reg_idx = vsi->rxq_map[i];
1841 		ring->ring_active = false;
1842 		ring->vsi = vsi;
1843 		ring->netdev = vsi->netdev;
1844 		ring->dev = &pf->pdev->dev;
1845 		ring->count = vsi->num_desc;
1846 		vsi->rx_rings[i] = ring;
1847 	}
1848 
1849 	return 0;
1850 
1851 err_out:
1852 	ice_vsi_clear_rings(vsi);
1853 	return -ENOMEM;
1854 }
1855 
1856 /**
1857  * ice_vsi_free_irq - Free the irq association with the OS
1858  * @vsi: the VSI being configured
1859  */
1860 static void ice_vsi_free_irq(struct ice_vsi *vsi)
1861 {
1862 	struct ice_pf *pf = vsi->back;
1863 	int base = vsi->base_vector;
1864 
1865 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1866 		int i;
1867 
1868 		if (!vsi->q_vectors || !vsi->irqs_ready)
1869 			return;
1870 
1871 		vsi->irqs_ready = false;
1872 		for (i = 0; i < vsi->num_q_vectors; i++) {
1873 			u16 vector = i + base;
1874 			int irq_num;
1875 
1876 			irq_num = pf->msix_entries[vector].vector;
1877 
1878 			/* free only the irqs that were actually requested */
1879 			if (!vsi->q_vectors[i] ||
1880 			    !(vsi->q_vectors[i]->num_ring_tx ||
1881 			      vsi->q_vectors[i]->num_ring_rx))
1882 				continue;
1883 
1884 			/* clear the affinity notifier in the IRQ descriptor */
1885 			irq_set_affinity_notifier(irq_num, NULL);
1886 
1887 			/* clear the affinity_mask in the IRQ descriptor */
1888 			irq_set_affinity_hint(irq_num, NULL);
1889 			synchronize_irq(irq_num);
1890 			devm_free_irq(&pf->pdev->dev, irq_num,
1891 				      vsi->q_vectors[i]);
1892 		}
1893 		ice_vsi_release_msix(vsi);
1894 	}
1895 }
1896 
1897 /**
1898  * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1899  * @vsi: the VSI being configured
1900  */
1901 static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1902 {
1903 	struct ice_pf *pf = vsi->back;
1904 	u16 vector = vsi->base_vector;
1905 	struct ice_hw *hw = &pf->hw;
1906 	u32 txq = 0, rxq = 0;
1907 	int i, q, itr;
1908 	u8 itr_gran;
1909 
1910 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1911 		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1912 
1913 		itr_gran = hw->itr_gran_200;
1914 
1915 		if (q_vector->num_ring_rx) {
1916 			q_vector->rx.itr =
1917 				ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1918 					   itr_gran);
1919 			q_vector->rx.latency_range = ICE_LOW_LATENCY;
1920 		}
1921 
1922 		if (q_vector->num_ring_tx) {
1923 			q_vector->tx.itr =
1924 				ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1925 					   itr_gran);
1926 			q_vector->tx.latency_range = ICE_LOW_LATENCY;
1927 		}
1928 		wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1929 		wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1930 
1931 		/* Both Transmit Queue Interrupt Cause Control register
1932 		 * and Receive Queue Interrupt Cause control register
1933 		 * expects MSIX_INDX field to be the vector index
1934 		 * within the function space and not the absolute
1935 		 * vector index across PF or across device.
1936 		 * For SR-IOV VF VSIs queue vector index always starts
1937 		 * with 1 since first vector index(0) is used for OICR
1938 		 * in VF space. Since VMDq and other PF VSIs are withtin
1939 		 * the PF function space, use the vector index thats
1940 		 * tracked for this PF.
1941 		 */
1942 		for (q = 0; q < q_vector->num_ring_tx; q++) {
1943 			u32 val;
1944 
1945 			itr = ICE_TX_ITR;
1946 			val = QINT_TQCTL_CAUSE_ENA_M |
1947 			      (itr << QINT_TQCTL_ITR_INDX_S)  |
1948 			      (vector << QINT_TQCTL_MSIX_INDX_S);
1949 			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1950 			txq++;
1951 		}
1952 
1953 		for (q = 0; q < q_vector->num_ring_rx; q++) {
1954 			u32 val;
1955 
1956 			itr = ICE_RX_ITR;
1957 			val = QINT_RQCTL_CAUSE_ENA_M |
1958 			      (itr << QINT_RQCTL_ITR_INDX_S)  |
1959 			      (vector << QINT_RQCTL_MSIX_INDX_S);
1960 			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1961 			rxq++;
1962 		}
1963 	}
1964 
1965 	ice_flush(hw);
1966 }
1967 
1968 /**
1969  * ice_ena_misc_vector - enable the non-queue interrupts
1970  * @pf: board private structure
1971  */
1972 static void ice_ena_misc_vector(struct ice_pf *pf)
1973 {
1974 	struct ice_hw *hw = &pf->hw;
1975 	u32 val;
1976 
1977 	/* clear things first */
1978 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
1979 	rd32(hw, PFINT_OICR);		/* read to clear */
1980 
1981 	val = (PFINT_OICR_ECC_ERR_M |
1982 	       PFINT_OICR_MAL_DETECT_M |
1983 	       PFINT_OICR_GRST_M |
1984 	       PFINT_OICR_PCI_EXCEPTION_M |
1985 	       PFINT_OICR_HMC_ERR_M |
1986 	       PFINT_OICR_PE_CRITERR_M);
1987 
1988 	wr32(hw, PFINT_OICR_ENA, val);
1989 
1990 	/* SW_ITR_IDX = 0, but don't change INTENA */
1991 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1992 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1993 }
1994 
1995 /**
1996  * ice_misc_intr - misc interrupt handler
1997  * @irq: interrupt number
1998  * @data: pointer to a q_vector
1999  */
2000 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2001 {
2002 	struct ice_pf *pf = (struct ice_pf *)data;
2003 	struct ice_hw *hw = &pf->hw;
2004 	irqreturn_t ret = IRQ_NONE;
2005 	u32 oicr, ena_mask;
2006 
2007 	set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2008 
2009 	oicr = rd32(hw, PFINT_OICR);
2010 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2011 
2012 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2013 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2014 		set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2015 	}
2016 
2017 	if (oicr & PFINT_OICR_GRST_M) {
2018 		u32 reset;
2019 
2020 		/* we have a reset warning */
2021 		ena_mask &= ~PFINT_OICR_GRST_M;
2022 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2023 			GLGEN_RSTAT_RESET_TYPE_S;
2024 
2025 		if (reset == ICE_RESET_CORER)
2026 			pf->corer_count++;
2027 		else if (reset == ICE_RESET_GLOBR)
2028 			pf->globr_count++;
2029 		else
2030 			pf->empr_count++;
2031 
2032 		/* If a reset cycle isn't already in progress, we set a bit in
2033 		 * pf->state so that the service task can start a reset/rebuild.
2034 		 * We also make note of which reset happened so that peer
2035 		 * devices/drivers can be informed.
2036 		 */
2037 		if (!test_and_set_bit(__ICE_RESET_RECOVERY_PENDING,
2038 				      pf->state)) {
2039 			if (reset == ICE_RESET_CORER)
2040 				set_bit(__ICE_CORER_RECV, pf->state);
2041 			else if (reset == ICE_RESET_GLOBR)
2042 				set_bit(__ICE_GLOBR_RECV, pf->state);
2043 			else
2044 				set_bit(__ICE_EMPR_RECV, pf->state);
2045 
2046 			/* There are couple of different bits at play here.
2047 			 * hw->reset_ongoing indicates whether the hardware is
2048 			 * in reset. This is set to true when a reset interrupt
2049 			 * is received and set back to false after the driver
2050 			 * has determined that the hardware is out of reset.
2051 			 *
2052 			 * __ICE_RESET_RECOVERY_PENDING in pf->state indicates
2053 			 * that a post reset rebuild is required before the
2054 			 * driver is operational again. This is set above.
2055 			 *
2056 			 * As this is the start of the reset/rebuild cycle, set
2057 			 * both to indicate that.
2058 			 */
2059 			hw->reset_ongoing = true;
2060 		}
2061 	}
2062 
2063 	if (oicr & PFINT_OICR_HMC_ERR_M) {
2064 		ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2065 		dev_dbg(&pf->pdev->dev,
2066 			"HMC Error interrupt - info 0x%x, data 0x%x\n",
2067 			rd32(hw, PFHMC_ERRORINFO),
2068 			rd32(hw, PFHMC_ERRORDATA));
2069 	}
2070 
2071 	/* Report and mask off any remaining unexpected interrupts */
2072 	oicr &= ena_mask;
2073 	if (oicr) {
2074 		dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
2075 			oicr);
2076 		/* If a critical error is pending there is no choice but to
2077 		 * reset the device.
2078 		 */
2079 		if (oicr & (PFINT_OICR_PE_CRITERR_M |
2080 			    PFINT_OICR_PCI_EXCEPTION_M |
2081 			    PFINT_OICR_ECC_ERR_M)) {
2082 			set_bit(__ICE_PFR_REQ, pf->state);
2083 			ice_service_task_schedule(pf);
2084 		}
2085 		ena_mask &= ~oicr;
2086 	}
2087 	ret = IRQ_HANDLED;
2088 
2089 	/* re-enable interrupt causes that are not handled during this pass */
2090 	wr32(hw, PFINT_OICR_ENA, ena_mask);
2091 	if (!test_bit(__ICE_DOWN, pf->state)) {
2092 		ice_service_task_schedule(pf);
2093 		ice_irq_dynamic_ena(hw, NULL, NULL);
2094 	}
2095 
2096 	return ret;
2097 }
2098 
2099 /**
2100  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
2101  * @vsi: the VSI being configured
2102  *
2103  * This function maps descriptor rings to the queue-specific vectors allotted
2104  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
2105  * and Rx rings to the vector as "efficiently" as possible.
2106  */
2107 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
2108 {
2109 	int q_vectors = vsi->num_q_vectors;
2110 	int tx_rings_rem, rx_rings_rem;
2111 	int v_id;
2112 
2113 	/* initially assigning remaining rings count to VSIs num queue value */
2114 	tx_rings_rem = vsi->num_txq;
2115 	rx_rings_rem = vsi->num_rxq;
2116 
2117 	for (v_id = 0; v_id < q_vectors; v_id++) {
2118 		struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
2119 		int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
2120 
2121 		/* Tx rings mapping to vector */
2122 		tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
2123 		q_vector->num_ring_tx = tx_rings_per_v;
2124 		q_vector->tx.ring = NULL;
2125 		q_base = vsi->num_txq - tx_rings_rem;
2126 
2127 		for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
2128 			struct ice_ring *tx_ring = vsi->tx_rings[q_id];
2129 
2130 			tx_ring->q_vector = q_vector;
2131 			tx_ring->next = q_vector->tx.ring;
2132 			q_vector->tx.ring = tx_ring;
2133 		}
2134 		tx_rings_rem -= tx_rings_per_v;
2135 
2136 		/* Rx rings mapping to vector */
2137 		rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
2138 		q_vector->num_ring_rx = rx_rings_per_v;
2139 		q_vector->rx.ring = NULL;
2140 		q_base = vsi->num_rxq - rx_rings_rem;
2141 
2142 		for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
2143 			struct ice_ring *rx_ring = vsi->rx_rings[q_id];
2144 
2145 			rx_ring->q_vector = q_vector;
2146 			rx_ring->next = q_vector->rx.ring;
2147 			q_vector->rx.ring = rx_ring;
2148 		}
2149 		rx_rings_rem -= rx_rings_per_v;
2150 	}
2151 }
2152 
2153 /**
2154  * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
2155  * @vsi: the VSI being configured
2156  *
2157  * Return 0 on success and a negative value on error
2158  */
2159 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
2160 {
2161 	struct ice_pf *pf = vsi->back;
2162 
2163 	switch (vsi->type) {
2164 	case ICE_VSI_PF:
2165 		vsi->alloc_txq = pf->num_lan_tx;
2166 		vsi->alloc_rxq = pf->num_lan_rx;
2167 		vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
2168 		vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
2169 		break;
2170 	default:
2171 		dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2172 			 vsi->type);
2173 		break;
2174 	}
2175 }
2176 
2177 /**
2178  * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
2179  * @vsi: VSI pointer
2180  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
2181  *
2182  * On error: returns error code (negative)
2183  * On success: returns 0
2184  */
2185 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
2186 {
2187 	struct ice_pf *pf = vsi->back;
2188 
2189 	/* allocate memory for both Tx and Rx ring pointers */
2190 	vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
2191 				     sizeof(struct ice_ring *), GFP_KERNEL);
2192 	if (!vsi->tx_rings)
2193 		goto err_txrings;
2194 
2195 	vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
2196 				     sizeof(struct ice_ring *), GFP_KERNEL);
2197 	if (!vsi->rx_rings)
2198 		goto err_rxrings;
2199 
2200 	if (alloc_qvectors) {
2201 		/* allocate memory for q_vector pointers */
2202 		vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
2203 					      vsi->num_q_vectors,
2204 					      sizeof(struct ice_q_vector *),
2205 					      GFP_KERNEL);
2206 		if (!vsi->q_vectors)
2207 			goto err_vectors;
2208 	}
2209 
2210 	return 0;
2211 
2212 err_vectors:
2213 	devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2214 err_rxrings:
2215 	devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2216 err_txrings:
2217 	return -ENOMEM;
2218 }
2219 
2220 /**
2221  * ice_msix_clean_rings - MSIX mode Interrupt Handler
2222  * @irq: interrupt number
2223  * @data: pointer to a q_vector
2224  */
2225 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
2226 {
2227 	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
2228 
2229 	if (!q_vector->tx.ring && !q_vector->rx.ring)
2230 		return IRQ_HANDLED;
2231 
2232 	napi_schedule(&q_vector->napi);
2233 
2234 	return IRQ_HANDLED;
2235 }
2236 
2237 /**
2238  * ice_vsi_alloc - Allocates the next available struct vsi in the PF
2239  * @pf: board private structure
2240  * @type: type of VSI
2241  *
2242  * returns a pointer to a VSI on success, NULL on failure.
2243  */
2244 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
2245 {
2246 	struct ice_vsi *vsi = NULL;
2247 
2248 	/* Need to protect the allocation of the VSIs at the PF level */
2249 	mutex_lock(&pf->sw_mutex);
2250 
2251 	/* If we have already allocated our maximum number of VSIs,
2252 	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
2253 	 * is available to be populated
2254 	 */
2255 	if (pf->next_vsi == ICE_NO_VSI) {
2256 		dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
2257 		goto unlock_pf;
2258 	}
2259 
2260 	vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
2261 	if (!vsi)
2262 		goto unlock_pf;
2263 
2264 	vsi->type = type;
2265 	vsi->back = pf;
2266 	set_bit(__ICE_DOWN, vsi->state);
2267 	vsi->idx = pf->next_vsi;
2268 	vsi->work_lmt = ICE_DFLT_IRQ_WORK;
2269 
2270 	ice_vsi_set_num_qs(vsi);
2271 
2272 	switch (vsi->type) {
2273 	case ICE_VSI_PF:
2274 		if (ice_vsi_alloc_arrays(vsi, true))
2275 			goto err_rings;
2276 
2277 		/* Setup default MSIX irq handler for VSI */
2278 		vsi->irq_handler = ice_msix_clean_rings;
2279 		break;
2280 	default:
2281 		dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
2282 		goto unlock_pf;
2283 	}
2284 
2285 	/* fill VSI slot in the PF struct */
2286 	pf->vsi[pf->next_vsi] = vsi;
2287 
2288 	/* prepare pf->next_vsi for next use */
2289 	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
2290 					 pf->next_vsi);
2291 	goto unlock_pf;
2292 
2293 err_rings:
2294 	devm_kfree(&pf->pdev->dev, vsi);
2295 	vsi = NULL;
2296 unlock_pf:
2297 	mutex_unlock(&pf->sw_mutex);
2298 	return vsi;
2299 }
2300 
2301 /**
2302  * ice_free_irq_msix_misc - Unroll misc vector setup
2303  * @pf: board private structure
2304  */
2305 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2306 {
2307 	/* disable OICR interrupt */
2308 	wr32(&pf->hw, PFINT_OICR_ENA, 0);
2309 	ice_flush(&pf->hw);
2310 
2311 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
2312 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2313 		devm_free_irq(&pf->pdev->dev,
2314 			      pf->msix_entries[pf->oicr_idx].vector, pf);
2315 	}
2316 
2317 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2318 }
2319 
2320 /**
2321  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2322  * @pf: board private structure
2323  *
2324  * This sets up the handler for MSIX 0, which is used to manage the
2325  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
2326  * when in MSI or Legacy interrupt mode.
2327  */
2328 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2329 {
2330 	struct ice_hw *hw = &pf->hw;
2331 	int oicr_idx, err = 0;
2332 	u8 itr_gran;
2333 	u32 val;
2334 
2335 	if (!pf->int_name[0])
2336 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2337 			 dev_driver_string(&pf->pdev->dev),
2338 			 dev_name(&pf->pdev->dev));
2339 
2340 	/* Do not request IRQ but do enable OICR interrupt since settings are
2341 	 * lost during reset. Note that this function is called only during
2342 	 * rebuild path and not while reset is in progress.
2343 	 */
2344 	if (ice_is_reset_recovery_pending(pf->state))
2345 		goto skip_req_irq;
2346 
2347 	/* reserve one vector in irq_tracker for misc interrupts */
2348 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2349 	if (oicr_idx < 0)
2350 		return oicr_idx;
2351 
2352 	pf->oicr_idx = oicr_idx;
2353 
2354 	err = devm_request_irq(&pf->pdev->dev,
2355 			       pf->msix_entries[pf->oicr_idx].vector,
2356 			       ice_misc_intr, 0, pf->int_name, pf);
2357 	if (err) {
2358 		dev_err(&pf->pdev->dev,
2359 			"devm_request_irq for %s failed: %d\n",
2360 			pf->int_name, err);
2361 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2362 		return err;
2363 	}
2364 
2365 skip_req_irq:
2366 	ice_ena_misc_vector(pf);
2367 
2368 	val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2369 	       PFINT_OICR_CTL_CAUSE_ENA_M);
2370 	wr32(hw, PFINT_OICR_CTL, val);
2371 
2372 	/* This enables Admin queue Interrupt causes */
2373 	val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2374 	       PFINT_FW_CTL_CAUSE_ENA_M);
2375 	wr32(hw, PFINT_FW_CTL, val);
2376 
2377 	itr_gran = hw->itr_gran_200;
2378 
2379 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2380 	     ITR_TO_REG(ICE_ITR_8K, itr_gran));
2381 
2382 	ice_flush(hw);
2383 	ice_irq_dynamic_ena(hw, NULL, NULL);
2384 
2385 	return 0;
2386 }
2387 
2388 /**
2389  * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
2390  * @vsi: the VSI getting queues
2391  *
2392  * Return 0 on success and a negative value on error
2393  */
2394 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
2395 {
2396 	struct ice_pf *pf = vsi->back;
2397 	int offset, ret = 0;
2398 
2399 	mutex_lock(&pf->avail_q_mutex);
2400 	/* look for contiguous block of queues for tx */
2401 	offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
2402 					    0, vsi->alloc_txq, 0);
2403 	if (offset < ICE_MAX_TXQS) {
2404 		int i;
2405 
2406 		bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
2407 		for (i = 0; i < vsi->alloc_txq; i++)
2408 			vsi->txq_map[i] = i + offset;
2409 	} else {
2410 		ret = -ENOMEM;
2411 		vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
2412 	}
2413 
2414 	/* look for contiguous block of queues for rx */
2415 	offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
2416 					    0, vsi->alloc_rxq, 0);
2417 	if (offset < ICE_MAX_RXQS) {
2418 		int i;
2419 
2420 		bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
2421 		for (i = 0; i < vsi->alloc_rxq; i++)
2422 			vsi->rxq_map[i] = i + offset;
2423 	} else {
2424 		ret = -ENOMEM;
2425 		vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
2426 	}
2427 	mutex_unlock(&pf->avail_q_mutex);
2428 
2429 	return ret;
2430 }
2431 
2432 /**
2433  * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
2434  * @vsi: the VSI getting queues
2435  *
2436  * Return 0 on success and a negative value on error
2437  */
2438 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
2439 {
2440 	struct ice_pf *pf = vsi->back;
2441 	int i, index = 0;
2442 
2443 	mutex_lock(&pf->avail_q_mutex);
2444 
2445 	if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2446 		for (i = 0; i < vsi->alloc_txq; i++) {
2447 			index = find_next_zero_bit(pf->avail_txqs,
2448 						   ICE_MAX_TXQS, index);
2449 			if (index < ICE_MAX_TXQS) {
2450 				set_bit(index, pf->avail_txqs);
2451 				vsi->txq_map[i] = index;
2452 			} else {
2453 				goto err_scatter_tx;
2454 			}
2455 		}
2456 	}
2457 
2458 	if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2459 		for (i = 0; i < vsi->alloc_rxq; i++) {
2460 			index = find_next_zero_bit(pf->avail_rxqs,
2461 						   ICE_MAX_RXQS, index);
2462 			if (index < ICE_MAX_RXQS) {
2463 				set_bit(index, pf->avail_rxqs);
2464 				vsi->rxq_map[i] = index;
2465 			} else {
2466 				goto err_scatter_rx;
2467 			}
2468 		}
2469 	}
2470 
2471 	mutex_unlock(&pf->avail_q_mutex);
2472 	return 0;
2473 
2474 err_scatter_rx:
2475 	/* unflag any queues we have grabbed (i is failed position) */
2476 	for (index = 0; index < i; index++) {
2477 		clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
2478 		vsi->rxq_map[index] = 0;
2479 	}
2480 	i = vsi->alloc_txq;
2481 err_scatter_tx:
2482 	/* i is either position of failed attempt or vsi->alloc_txq */
2483 	for (index = 0; index < i; index++) {
2484 		clear_bit(vsi->txq_map[index], pf->avail_txqs);
2485 		vsi->txq_map[index] = 0;
2486 	}
2487 
2488 	mutex_unlock(&pf->avail_q_mutex);
2489 	return -ENOMEM;
2490 }
2491 
2492 /**
2493  * ice_vsi_get_qs - Assign queues from PF to VSI
2494  * @vsi: the VSI to assign queues to
2495  *
2496  * Returns 0 on success and a negative value on error
2497  */
2498 static int ice_vsi_get_qs(struct ice_vsi *vsi)
2499 {
2500 	int ret = 0;
2501 
2502 	vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
2503 	vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
2504 
2505 	/* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
2506 	 * modes individually to scatter if assigning contiguous queues
2507 	 * to rx or tx fails
2508 	 */
2509 	ret = ice_vsi_get_qs_contig(vsi);
2510 	if (ret < 0) {
2511 		if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
2512 			vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
2513 					       ICE_MAX_SCATTER_TXQS);
2514 		if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
2515 			vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
2516 					       ICE_MAX_SCATTER_RXQS);
2517 		ret = ice_vsi_get_qs_scatter(vsi);
2518 	}
2519 
2520 	return ret;
2521 }
2522 
2523 /**
2524  * ice_vsi_put_qs - Release queues from VSI to PF
2525  * @vsi: the VSI thats going to release queues
2526  */
2527 static void ice_vsi_put_qs(struct ice_vsi *vsi)
2528 {
2529 	struct ice_pf *pf = vsi->back;
2530 	int i;
2531 
2532 	mutex_lock(&pf->avail_q_mutex);
2533 
2534 	for (i = 0; i < vsi->alloc_txq; i++) {
2535 		clear_bit(vsi->txq_map[i], pf->avail_txqs);
2536 		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
2537 	}
2538 
2539 	for (i = 0; i < vsi->alloc_rxq; i++) {
2540 		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
2541 		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
2542 	}
2543 
2544 	mutex_unlock(&pf->avail_q_mutex);
2545 }
2546 
2547 /**
2548  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
2549  * @vsi: VSI having the memory freed
2550  * @v_idx: index of the vector to be freed
2551  */
2552 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
2553 {
2554 	struct ice_q_vector *q_vector;
2555 	struct ice_ring *ring;
2556 
2557 	if (!vsi->q_vectors[v_idx]) {
2558 		dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
2559 			v_idx);
2560 		return;
2561 	}
2562 	q_vector = vsi->q_vectors[v_idx];
2563 
2564 	ice_for_each_ring(ring, q_vector->tx)
2565 		ring->q_vector = NULL;
2566 	ice_for_each_ring(ring, q_vector->rx)
2567 		ring->q_vector = NULL;
2568 
2569 	/* only VSI with an associated netdev is set up with NAPI */
2570 	if (vsi->netdev)
2571 		netif_napi_del(&q_vector->napi);
2572 
2573 	devm_kfree(&vsi->back->pdev->dev, q_vector);
2574 	vsi->q_vectors[v_idx] = NULL;
2575 }
2576 
2577 /**
2578  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
2579  * @vsi: the VSI having memory freed
2580  */
2581 static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
2582 {
2583 	int v_idx;
2584 
2585 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
2586 		ice_free_q_vector(vsi, v_idx);
2587 }
2588 
2589 /**
2590  * ice_cfg_netdev - Setup the netdev flags
2591  * @vsi: the VSI being configured
2592  *
2593  * Returns 0 on success, negative value on failure
2594  */
2595 static int ice_cfg_netdev(struct ice_vsi *vsi)
2596 {
2597 	netdev_features_t csumo_features;
2598 	netdev_features_t vlano_features;
2599 	netdev_features_t dflt_features;
2600 	netdev_features_t tso_features;
2601 	struct ice_netdev_priv *np;
2602 	struct net_device *netdev;
2603 	u8 mac_addr[ETH_ALEN];
2604 
2605 	netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
2606 				    vsi->alloc_txq, vsi->alloc_rxq);
2607 	if (!netdev)
2608 		return -ENOMEM;
2609 
2610 	vsi->netdev = netdev;
2611 	np = netdev_priv(netdev);
2612 	np->vsi = vsi;
2613 
2614 	dflt_features = NETIF_F_SG	|
2615 			NETIF_F_HIGHDMA	|
2616 			NETIF_F_RXHASH;
2617 
2618 	csumo_features = NETIF_F_RXCSUM	  |
2619 			 NETIF_F_IP_CSUM  |
2620 			 NETIF_F_IPV6_CSUM;
2621 
2622 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2623 			 NETIF_F_HW_VLAN_CTAG_TX     |
2624 			 NETIF_F_HW_VLAN_CTAG_RX;
2625 
2626 	tso_features = NETIF_F_TSO;
2627 
2628 	/* set features that user can change */
2629 	netdev->hw_features = dflt_features | csumo_features |
2630 			      vlano_features | tso_features;
2631 
2632 	/* enable features */
2633 	netdev->features |= netdev->hw_features;
2634 	/* encap and VLAN devices inherit default, csumo and tso features */
2635 	netdev->hw_enc_features |= dflt_features | csumo_features |
2636 				   tso_features;
2637 	netdev->vlan_features |= dflt_features | csumo_features |
2638 				 tso_features;
2639 
2640 	if (vsi->type == ICE_VSI_PF) {
2641 		SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
2642 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2643 
2644 		ether_addr_copy(netdev->dev_addr, mac_addr);
2645 		ether_addr_copy(netdev->perm_addr, mac_addr);
2646 	}
2647 
2648 	netdev->priv_flags |= IFF_UNICAST_FLT;
2649 
2650 	/* assign netdev_ops */
2651 	netdev->netdev_ops = &ice_netdev_ops;
2652 
2653 	/* setup watchdog timeout value to be 5 second */
2654 	netdev->watchdog_timeo = 5 * HZ;
2655 
2656 	ice_set_ethtool_ops(netdev);
2657 
2658 	netdev->min_mtu = ETH_MIN_MTU;
2659 	netdev->max_mtu = ICE_MAX_MTU;
2660 
2661 	return 0;
2662 }
2663 
2664 /**
2665  * ice_vsi_free_arrays - clean up vsi resources
2666  * @vsi: pointer to VSI being cleared
2667  * @free_qvectors: bool to specify if q_vectors should be deallocated
2668  */
2669 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
2670 {
2671 	struct ice_pf *pf = vsi->back;
2672 
2673 	/* free the ring and vector containers */
2674 	if (free_qvectors && vsi->q_vectors) {
2675 		devm_kfree(&pf->pdev->dev, vsi->q_vectors);
2676 		vsi->q_vectors = NULL;
2677 	}
2678 	if (vsi->tx_rings) {
2679 		devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2680 		vsi->tx_rings = NULL;
2681 	}
2682 	if (vsi->rx_rings) {
2683 		devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2684 		vsi->rx_rings = NULL;
2685 	}
2686 }
2687 
2688 /**
2689  * ice_vsi_clear - clean up and deallocate the provided vsi
2690  * @vsi: pointer to VSI being cleared
2691  *
2692  * This deallocates the vsi's queue resources, removes it from the PF's
2693  * VSI array if necessary, and deallocates the VSI
2694  *
2695  * Returns 0 on success, negative on failure
2696  */
2697 static int ice_vsi_clear(struct ice_vsi *vsi)
2698 {
2699 	struct ice_pf *pf = NULL;
2700 
2701 	if (!vsi)
2702 		return 0;
2703 
2704 	if (!vsi->back)
2705 		return -EINVAL;
2706 
2707 	pf = vsi->back;
2708 
2709 	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
2710 		dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
2711 			vsi->idx);
2712 		return -EINVAL;
2713 	}
2714 
2715 	mutex_lock(&pf->sw_mutex);
2716 	/* updates the PF for this cleared vsi */
2717 
2718 	pf->vsi[vsi->idx] = NULL;
2719 	if (vsi->idx < pf->next_vsi)
2720 		pf->next_vsi = vsi->idx;
2721 
2722 	ice_vsi_free_arrays(vsi, true);
2723 	mutex_unlock(&pf->sw_mutex);
2724 	devm_kfree(&pf->pdev->dev, vsi);
2725 
2726 	return 0;
2727 }
2728 
2729 /**
2730  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
2731  * @vsi: the VSI being configured
2732  * @v_idx: index of the vector in the vsi struct
2733  *
2734  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
2735  */
2736 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
2737 {
2738 	struct ice_pf *pf = vsi->back;
2739 	struct ice_q_vector *q_vector;
2740 
2741 	/* allocate q_vector */
2742 	q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
2743 	if (!q_vector)
2744 		return -ENOMEM;
2745 
2746 	q_vector->vsi = vsi;
2747 	q_vector->v_idx = v_idx;
2748 	/* only set affinity_mask if the CPU is online */
2749 	if (cpu_online(v_idx))
2750 		cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
2751 
2752 	if (vsi->netdev)
2753 		netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
2754 			       NAPI_POLL_WEIGHT);
2755 	/* tie q_vector and vsi together */
2756 	vsi->q_vectors[v_idx] = q_vector;
2757 
2758 	return 0;
2759 }
2760 
2761 /**
2762  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
2763  * @vsi: the VSI being configured
2764  *
2765  * We allocate one q_vector per queue interrupt.  If allocation fails we
2766  * return -ENOMEM.
2767  */
2768 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
2769 {
2770 	struct ice_pf *pf = vsi->back;
2771 	int v_idx = 0, num_q_vectors;
2772 	int err;
2773 
2774 	if (vsi->q_vectors[0]) {
2775 		dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
2776 			vsi->vsi_num);
2777 		return -EEXIST;
2778 	}
2779 
2780 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2781 		num_q_vectors = vsi->num_q_vectors;
2782 	} else {
2783 		err = -EINVAL;
2784 		goto err_out;
2785 	}
2786 
2787 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
2788 		err = ice_vsi_alloc_q_vector(vsi, v_idx);
2789 		if (err)
2790 			goto err_out;
2791 	}
2792 
2793 	return 0;
2794 
2795 err_out:
2796 	while (v_idx--)
2797 		ice_free_q_vector(vsi, v_idx);
2798 
2799 	dev_err(&pf->pdev->dev,
2800 		"Failed to allocate %d q_vector for VSI %d, ret=%d\n",
2801 		vsi->num_q_vectors, vsi->vsi_num, err);
2802 	vsi->num_q_vectors = 0;
2803 	return err;
2804 }
2805 
2806 /**
2807  * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
2808  * @vsi: ptr to the VSI
2809  *
2810  * This should only be called after ice_vsi_alloc() which allocates the
2811  * corresponding SW VSI structure and initializes num_queue_pairs for the
2812  * newly allocated VSI.
2813  *
2814  * Returns 0 on success or negative on failure
2815  */
2816 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
2817 {
2818 	struct ice_pf *pf = vsi->back;
2819 	int num_q_vectors = 0;
2820 
2821 	if (vsi->base_vector) {
2822 		dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
2823 			vsi->vsi_num, vsi->base_vector);
2824 		return -EEXIST;
2825 	}
2826 
2827 	if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2828 		return -ENOENT;
2829 
2830 	switch (vsi->type) {
2831 	case ICE_VSI_PF:
2832 		num_q_vectors = vsi->num_q_vectors;
2833 		break;
2834 	default:
2835 		dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2836 			 vsi->type);
2837 		break;
2838 	}
2839 
2840 	if (num_q_vectors)
2841 		vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
2842 					       num_q_vectors, vsi->idx);
2843 
2844 	if (vsi->base_vector < 0) {
2845 		dev_err(&pf->pdev->dev,
2846 			"Failed to get tracking for %d vectors for VSI %d, err=%d\n",
2847 			num_q_vectors, vsi->vsi_num, vsi->base_vector);
2848 		return -ENOENT;
2849 	}
2850 
2851 	return 0;
2852 }
2853 
2854 /**
2855  * ice_fill_rss_lut - Fill the RSS lookup table with default values
2856  * @lut: Lookup table
2857  * @rss_table_size: Lookup table size
2858  * @rss_size: Range of queue number for hashing
2859  */
2860 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2861 {
2862 	u16 i;
2863 
2864 	for (i = 0; i < rss_table_size; i++)
2865 		lut[i] = i % rss_size;
2866 }
2867 
2868 /**
2869  * ice_vsi_cfg_rss - Configure RSS params for a VSI
2870  * @vsi: VSI to be configured
2871  */
2872 static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
2873 {
2874 	u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
2875 	struct ice_aqc_get_set_rss_keys *key;
2876 	struct ice_pf *pf = vsi->back;
2877 	enum ice_status status;
2878 	int err = 0;
2879 	u8 *lut;
2880 
2881 	vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
2882 
2883 	lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2884 	if (!lut)
2885 		return -ENOMEM;
2886 
2887 	if (vsi->rss_lut_user)
2888 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
2889 	else
2890 		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
2891 
2892 	status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
2893 				    lut, vsi->rss_table_size);
2894 
2895 	if (status) {
2896 		dev_err(&vsi->back->pdev->dev,
2897 			"set_rss_lut failed, error %d\n", status);
2898 		err = -EIO;
2899 		goto ice_vsi_cfg_rss_exit;
2900 	}
2901 
2902 	key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
2903 	if (!key) {
2904 		err = -ENOMEM;
2905 		goto ice_vsi_cfg_rss_exit;
2906 	}
2907 
2908 	if (vsi->rss_hkey_user)
2909 		memcpy(seed, vsi->rss_hkey_user,
2910 		       ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2911 	else
2912 		netdev_rss_key_fill((void *)seed,
2913 				    ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2914 	memcpy(&key->standard_rss_key, seed,
2915 	       ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2916 
2917 	status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
2918 
2919 	if (status) {
2920 		dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
2921 			status);
2922 		err = -EIO;
2923 	}
2924 
2925 	devm_kfree(&pf->pdev->dev, key);
2926 ice_vsi_cfg_rss_exit:
2927 	devm_kfree(&pf->pdev->dev, lut);
2928 	return err;
2929 }
2930 
2931 /**
2932  * ice_vsi_rebuild - Rebuild VSI after reset
2933  * @vsi: vsi to be rebuild
2934  *
2935  * Returns 0 on success and negative value on failure
2936  */
2937 static int ice_vsi_rebuild(struct ice_vsi *vsi)
2938 {
2939 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2940 	int ret, i;
2941 
2942 	if (!vsi)
2943 		return -EINVAL;
2944 
2945 	ice_vsi_free_q_vectors(vsi);
2946 	ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2947 	vsi->base_vector = 0;
2948 	ice_vsi_clear_rings(vsi);
2949 	ice_vsi_free_arrays(vsi, false);
2950 	ice_vsi_set_num_qs(vsi);
2951 
2952 	/* Initialize VSI struct elements and create VSI in FW */
2953 	ret = ice_vsi_init(vsi);
2954 	if (ret < 0)
2955 		goto err_vsi;
2956 
2957 	ret = ice_vsi_alloc_arrays(vsi, false);
2958 	if (ret < 0)
2959 		goto err_vsi;
2960 
2961 	switch (vsi->type) {
2962 	case ICE_VSI_PF:
2963 		/* fall through */
2964 		ret = ice_vsi_alloc_q_vectors(vsi);
2965 		if (ret)
2966 			goto err_rings;
2967 
2968 		ret = ice_vsi_setup_vector_base(vsi);
2969 		if (ret)
2970 			goto err_vectors;
2971 
2972 		ret = ice_vsi_alloc_rings(vsi);
2973 		if (ret)
2974 			goto err_vectors;
2975 
2976 		ice_vsi_map_rings_to_vectors(vsi);
2977 		break;
2978 	default:
2979 		break;
2980 	}
2981 
2982 	ice_vsi_set_tc_cfg(vsi);
2983 
2984 	/* configure VSI nodes based on number of queues and TC's */
2985 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2986 		max_txqs[i] = vsi->num_txq;
2987 
2988 	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2989 			      vsi->tc_cfg.ena_tc, max_txqs);
2990 	if (ret) {
2991 		dev_info(&vsi->back->pdev->dev,
2992 			 "Failed VSI lan queue config\n");
2993 		goto err_vectors;
2994 	}
2995 	return 0;
2996 
2997 err_vectors:
2998 	ice_vsi_free_q_vectors(vsi);
2999 err_rings:
3000 	if (vsi->netdev) {
3001 		vsi->current_netdev_flags = 0;
3002 		unregister_netdev(vsi->netdev);
3003 		free_netdev(vsi->netdev);
3004 		vsi->netdev = NULL;
3005 	}
3006 err_vsi:
3007 	ice_vsi_clear(vsi);
3008 	set_bit(__ICE_RESET_FAILED, vsi->back->state);
3009 	return ret;
3010 }
3011 
3012 /**
3013  * ice_vsi_setup - Set up a VSI by a given type
3014  * @pf: board private structure
3015  * @pi: pointer to the port_info instance
3016  * @type: VSI type
3017  * @vf_id: defines VF id to which this VSI connects. This field is meant to be
3018  *         used only for ICE_VSI_VF VSI type. For other VSI types, should
3019  *         fill-in ICE_INVAL_VFID as input.
3020  *
3021  * This allocates the sw VSI structure and its queue resources.
3022  *
3023  * Returns pointer to the successfully allocated and configured VSI sw struct on
3024  * success, NULL on failure.
3025  */
3026 static struct ice_vsi *
3027 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3028 	      enum ice_vsi_type type, u16 __always_unused vf_id)
3029 {
3030 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3031 	struct device *dev = &pf->pdev->dev;
3032 	struct ice_vsi *vsi;
3033 	int ret, i;
3034 
3035 	vsi = ice_vsi_alloc(pf, type);
3036 	if (!vsi) {
3037 		dev_err(dev, "could not allocate VSI\n");
3038 		return NULL;
3039 	}
3040 
3041 	vsi->port_info = pi;
3042 	vsi->vsw = pf->first_sw;
3043 
3044 	if (ice_vsi_get_qs(vsi)) {
3045 		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
3046 			vsi->idx);
3047 		goto err_get_qs;
3048 	}
3049 
3050 	/* set RSS capabilities */
3051 	ice_vsi_set_rss_params(vsi);
3052 
3053 	/* create the VSI */
3054 	ret = ice_vsi_init(vsi);
3055 	if (ret)
3056 		goto err_vsi;
3057 
3058 	switch (vsi->type) {
3059 	case ICE_VSI_PF:
3060 		ret = ice_cfg_netdev(vsi);
3061 		if (ret)
3062 			goto err_cfg_netdev;
3063 
3064 		ret = register_netdev(vsi->netdev);
3065 		if (ret)
3066 			goto err_register_netdev;
3067 
3068 		netif_carrier_off(vsi->netdev);
3069 
3070 		/* make sure transmit queues start off as stopped */
3071 		netif_tx_stop_all_queues(vsi->netdev);
3072 		ret = ice_vsi_alloc_q_vectors(vsi);
3073 		if (ret)
3074 			goto err_msix;
3075 
3076 		ret = ice_vsi_setup_vector_base(vsi);
3077 		if (ret)
3078 			goto err_rings;
3079 
3080 		ret = ice_vsi_alloc_rings(vsi);
3081 		if (ret)
3082 			goto err_rings;
3083 
3084 		ice_vsi_map_rings_to_vectors(vsi);
3085 
3086 		/* Do not exit if configuring RSS had an issue, at least
3087 		 * receive traffic on first queue. Hence no need to capture
3088 		 * return value
3089 		 */
3090 		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3091 			ice_vsi_cfg_rss(vsi);
3092 		break;
3093 	default:
3094 		/* if vsi type is not recognized, clean up the resources and
3095 		 * exit
3096 		 */
3097 		goto err_rings;
3098 	}
3099 
3100 	ice_vsi_set_tc_cfg(vsi);
3101 
3102 	/* configure VSI nodes based on number of queues and TC's */
3103 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
3104 		max_txqs[i] = vsi->num_txq;
3105 
3106 	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
3107 			      vsi->tc_cfg.ena_tc, max_txqs);
3108 	if (ret) {
3109 		dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
3110 		goto err_rings;
3111 	}
3112 
3113 	return vsi;
3114 
3115 err_rings:
3116 	ice_vsi_free_q_vectors(vsi);
3117 err_msix:
3118 	if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3119 		unregister_netdev(vsi->netdev);
3120 err_register_netdev:
3121 	if (vsi->netdev) {
3122 		free_netdev(vsi->netdev);
3123 		vsi->netdev = NULL;
3124 	}
3125 err_cfg_netdev:
3126 	ice_vsi_delete(vsi);
3127 err_vsi:
3128 	ice_vsi_put_qs(vsi);
3129 err_get_qs:
3130 	pf->q_left_tx += vsi->alloc_txq;
3131 	pf->q_left_rx += vsi->alloc_rxq;
3132 	ice_vsi_clear(vsi);
3133 
3134 	return NULL;
3135 }
3136 
3137 /**
3138  * ice_pf_vsi_setup - Set up a PF VSI
3139  * @pf: board private structure
3140  * @pi: pointer to the port_info instance
3141  *
3142  * Returns pointer to the successfully allocated VSI sw struct on success,
3143  * otherwise returns NULL on failure.
3144  */
3145 static struct ice_vsi *
3146 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3147 {
3148 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
3149 }
3150 
3151 /**
3152  * ice_vsi_add_vlan - Add vsi membership for given vlan
3153  * @vsi: the vsi being configured
3154  * @vid: vlan id to be added
3155  */
3156 static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
3157 {
3158 	struct ice_fltr_list_entry *tmp;
3159 	struct ice_pf *pf = vsi->back;
3160 	LIST_HEAD(tmp_add_list);
3161 	enum ice_status status;
3162 	int err = 0;
3163 
3164 	tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
3165 	if (!tmp)
3166 		return -ENOMEM;
3167 
3168 	tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
3169 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3170 	tmp->fltr_info.flag = ICE_FLTR_TX;
3171 	tmp->fltr_info.src = vsi->vsi_num;
3172 	tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
3173 	tmp->fltr_info.l_data.vlan.vlan_id = vid;
3174 
3175 	INIT_LIST_HEAD(&tmp->list_entry);
3176 	list_add(&tmp->list_entry, &tmp_add_list);
3177 
3178 	status = ice_add_vlan(&pf->hw, &tmp_add_list);
3179 	if (status) {
3180 		err = -ENODEV;
3181 		dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
3182 			vid, vsi->vsi_num);
3183 	}
3184 
3185 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3186 	return err;
3187 }
3188 
3189 /**
3190  * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
3191  * @netdev: network interface to be adjusted
3192  * @proto: unused protocol
3193  * @vid: vlan id to be added
3194  *
3195  * net_device_ops implementation for adding vlan ids
3196  */
3197 static int ice_vlan_rx_add_vid(struct net_device *netdev,
3198 			       __always_unused __be16 proto, u16 vid)
3199 {
3200 	struct ice_netdev_priv *np = netdev_priv(netdev);
3201 	struct ice_vsi *vsi = np->vsi;
3202 	int ret;
3203 
3204 	if (vid >= VLAN_N_VID) {
3205 		netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
3206 			   vid, VLAN_N_VID);
3207 		return -EINVAL;
3208 	}
3209 
3210 	if (vsi->info.pvid)
3211 		return -EINVAL;
3212 
3213 	/* Enable VLAN pruning when VLAN 0 is added */
3214 	if (unlikely(!vid)) {
3215 		ret = ice_cfg_vlan_pruning(vsi, true);
3216 		if (ret)
3217 			return ret;
3218 	}
3219 
3220 	/* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
3221 	 * needed to continue allowing all untagged packets since VLAN prune
3222 	 * list is applied to all packets by the switch
3223 	 */
3224 	ret = ice_vsi_add_vlan(vsi, vid);
3225 
3226 	if (!ret)
3227 		set_bit(vid, vsi->active_vlans);
3228 
3229 	return ret;
3230 }
3231 
3232 /**
3233  * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
3234  * @vsi: the VSI being configured
3235  * @vid: VLAN id to be removed
3236  *
3237  * Returns 0 on success and negative on failure
3238  */
3239 static int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
3240 {
3241 	struct ice_fltr_list_entry *list;
3242 	struct ice_pf *pf = vsi->back;
3243 	LIST_HEAD(tmp_add_list);
3244 	int status = 0;
3245 
3246 	list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
3247 	if (!list)
3248 		return -ENOMEM;
3249 
3250 	list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
3251 	list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
3252 	list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3253 	list->fltr_info.l_data.vlan.vlan_id = vid;
3254 	list->fltr_info.flag = ICE_FLTR_TX;
3255 	list->fltr_info.src = vsi->vsi_num;
3256 
3257 	INIT_LIST_HEAD(&list->list_entry);
3258 	list_add(&list->list_entry, &tmp_add_list);
3259 
3260 	if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
3261 		dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
3262 			vid, vsi->vsi_num);
3263 		status = -EIO;
3264 	}
3265 
3266 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3267 	return status;
3268 }
3269 
3270 /**
3271  * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3272  * @netdev: network interface to be adjusted
3273  * @proto: unused protocol
3274  * @vid: vlan id to be removed
3275  *
3276  * net_device_ops implementation for removing vlan ids
3277  */
3278 static int ice_vlan_rx_kill_vid(struct net_device *netdev,
3279 				__always_unused __be16 proto, u16 vid)
3280 {
3281 	struct ice_netdev_priv *np = netdev_priv(netdev);
3282 	struct ice_vsi *vsi = np->vsi;
3283 	int status;
3284 
3285 	if (vsi->info.pvid)
3286 		return -EINVAL;
3287 
3288 	/* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3289 	 * information
3290 	 */
3291 	status = ice_vsi_kill_vlan(vsi, vid);
3292 	if (status)
3293 		return status;
3294 
3295 	clear_bit(vid, vsi->active_vlans);
3296 
3297 	/* Disable VLAN pruning when VLAN 0 is removed */
3298 	if (unlikely(!vid))
3299 		status = ice_cfg_vlan_pruning(vsi, false);
3300 
3301 	return status;
3302 }
3303 
3304 /**
3305  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3306  * @pf: board private structure
3307  *
3308  * Returns 0 on success, negative value on failure
3309  */
3310 static int ice_setup_pf_sw(struct ice_pf *pf)
3311 {
3312 	LIST_HEAD(tmp_add_list);
3313 	u8 broadcast[ETH_ALEN];
3314 	struct ice_vsi *vsi;
3315 	int status = 0;
3316 
3317 	if (ice_is_reset_recovery_pending(pf->state))
3318 		return -EBUSY;
3319 
3320 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3321 	if (!vsi) {
3322 		status = -ENOMEM;
3323 		goto unroll_vsi_setup;
3324 	}
3325 
3326 	/* To add a MAC filter, first add the MAC to a list and then
3327 	 * pass the list to ice_add_mac.
3328 	 */
3329 
3330 	 /* Add a unicast MAC filter so the VSI can get its packets */
3331 	status = ice_add_mac_to_list(vsi, &tmp_add_list,
3332 				     vsi->port_info->mac.perm_addr);
3333 	if (status)
3334 		goto unroll_vsi_setup;
3335 
3336 	/* VSI needs to receive broadcast traffic, so add the broadcast
3337 	 * MAC address to the list as well.
3338 	 */
3339 	eth_broadcast_addr(broadcast);
3340 	status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
3341 	if (status)
3342 		goto free_mac_list;
3343 
3344 	/* program MAC filters for entries in tmp_add_list */
3345 	status = ice_add_mac(&pf->hw, &tmp_add_list);
3346 	if (status) {
3347 		dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
3348 		status = -ENOMEM;
3349 		goto free_mac_list;
3350 	}
3351 
3352 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3353 	return status;
3354 
3355 free_mac_list:
3356 	ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3357 
3358 unroll_vsi_setup:
3359 	if (vsi) {
3360 		ice_vsi_free_q_vectors(vsi);
3361 		if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3362 			unregister_netdev(vsi->netdev);
3363 		if (vsi->netdev) {
3364 			free_netdev(vsi->netdev);
3365 			vsi->netdev = NULL;
3366 		}
3367 
3368 		ice_vsi_delete(vsi);
3369 		ice_vsi_put_qs(vsi);
3370 		pf->q_left_tx += vsi->alloc_txq;
3371 		pf->q_left_rx += vsi->alloc_rxq;
3372 		ice_vsi_clear(vsi);
3373 	}
3374 	return status;
3375 }
3376 
3377 /**
3378  * ice_determine_q_usage - Calculate queue distribution
3379  * @pf: board private structure
3380  *
3381  * Return -ENOMEM if we don't get enough queues for all ports
3382  */
3383 static void ice_determine_q_usage(struct ice_pf *pf)
3384 {
3385 	u16 q_left_tx, q_left_rx;
3386 
3387 	q_left_tx = pf->hw.func_caps.common_cap.num_txq;
3388 	q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
3389 
3390 	pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
3391 
3392 	/* only 1 rx queue unless RSS is enabled */
3393 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3394 		pf->num_lan_rx = 1;
3395 	else
3396 		pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
3397 
3398 	pf->q_left_tx = q_left_tx - pf->num_lan_tx;
3399 	pf->q_left_rx = q_left_rx - pf->num_lan_rx;
3400 }
3401 
3402 /**
3403  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3404  * @pf: board private structure to initialize
3405  */
3406 static void ice_deinit_pf(struct ice_pf *pf)
3407 {
3408 	ice_service_task_stop(pf);
3409 	mutex_destroy(&pf->sw_mutex);
3410 	mutex_destroy(&pf->avail_q_mutex);
3411 }
3412 
3413 /**
3414  * ice_init_pf - Initialize general software structures (struct ice_pf)
3415  * @pf: board private structure to initialize
3416  */
3417 static void ice_init_pf(struct ice_pf *pf)
3418 {
3419 	bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
3420 	set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3421 
3422 	mutex_init(&pf->sw_mutex);
3423 	mutex_init(&pf->avail_q_mutex);
3424 
3425 	/* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
3426 	mutex_lock(&pf->avail_q_mutex);
3427 	bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
3428 	bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
3429 	mutex_unlock(&pf->avail_q_mutex);
3430 
3431 	if (pf->hw.func_caps.common_cap.rss_table_size)
3432 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3433 
3434 	/* setup service timer and periodic service task */
3435 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3436 	pf->serv_tmr_period = HZ;
3437 	INIT_WORK(&pf->serv_task, ice_service_task);
3438 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
3439 }
3440 
3441 /**
3442  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3443  * @pf: board private structure
3444  *
3445  * compute the number of MSIX vectors required (v_budget) and request from
3446  * the OS. Return the number of vectors reserved or negative on failure
3447  */
3448 static int ice_ena_msix_range(struct ice_pf *pf)
3449 {
3450 	int v_left, v_actual, v_budget = 0;
3451 	int needed, err, i;
3452 
3453 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3454 
3455 	/* reserve one vector for miscellaneous handler */
3456 	needed = 1;
3457 	v_budget += needed;
3458 	v_left -= needed;
3459 
3460 	/* reserve vectors for LAN traffic */
3461 	pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
3462 	v_budget += pf->num_lan_msix;
3463 
3464 	pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
3465 					sizeof(struct msix_entry), GFP_KERNEL);
3466 
3467 	if (!pf->msix_entries) {
3468 		err = -ENOMEM;
3469 		goto exit_err;
3470 	}
3471 
3472 	for (i = 0; i < v_budget; i++)
3473 		pf->msix_entries[i].entry = i;
3474 
3475 	/* actually reserve the vectors */
3476 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3477 					 ICE_MIN_MSIX, v_budget);
3478 
3479 	if (v_actual < 0) {
3480 		dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
3481 		err = v_actual;
3482 		goto msix_err;
3483 	}
3484 
3485 	if (v_actual < v_budget) {
3486 		dev_warn(&pf->pdev->dev,
3487 			 "not enough vectors. requested = %d, obtained = %d\n",
3488 			 v_budget, v_actual);
3489 		if (v_actual >= (pf->num_lan_msix + 1)) {
3490 			pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
3491 		} else if (v_actual >= 2) {
3492 			pf->num_lan_msix = 1;
3493 			pf->num_avail_msix = v_actual - 2;
3494 		} else {
3495 			pci_disable_msix(pf->pdev);
3496 			err = -ERANGE;
3497 			goto msix_err;
3498 		}
3499 	}
3500 
3501 	return v_actual;
3502 
3503 msix_err:
3504 	devm_kfree(&pf->pdev->dev, pf->msix_entries);
3505 	goto exit_err;
3506 
3507 exit_err:
3508 	pf->num_lan_msix = 0;
3509 	clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3510 	return err;
3511 }
3512 
3513 /**
3514  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3515  * @pf: board private structure
3516  */
3517 static void ice_dis_msix(struct ice_pf *pf)
3518 {
3519 	pci_disable_msix(pf->pdev);
3520 	devm_kfree(&pf->pdev->dev, pf->msix_entries);
3521 	pf->msix_entries = NULL;
3522 	clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3523 }
3524 
3525 /**
3526  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3527  * @pf: board private structure to initialize
3528  */
3529 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3530 {
3531 	int vectors = 0;
3532 	ssize_t size;
3533 
3534 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3535 		vectors = ice_ena_msix_range(pf);
3536 	else
3537 		return -ENODEV;
3538 
3539 	if (vectors < 0)
3540 		return vectors;
3541 
3542 	/* set up vector assignment tracking */
3543 	size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
3544 
3545 	pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
3546 	if (!pf->irq_tracker) {
3547 		ice_dis_msix(pf);
3548 		return -ENOMEM;
3549 	}
3550 
3551 	pf->irq_tracker->num_entries = vectors;
3552 
3553 	return 0;
3554 }
3555 
3556 /**
3557  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3558  * @pf: board private structure
3559  */
3560 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3561 {
3562 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3563 		ice_dis_msix(pf);
3564 
3565 	if (pf->irq_tracker) {
3566 		devm_kfree(&pf->pdev->dev, pf->irq_tracker);
3567 		pf->irq_tracker = NULL;
3568 	}
3569 }
3570 
3571 /**
3572  * ice_probe - Device initialization routine
3573  * @pdev: PCI device information struct
3574  * @ent: entry in ice_pci_tbl
3575  *
3576  * Returns 0 on success, negative on failure
3577  */
3578 static int ice_probe(struct pci_dev *pdev,
3579 		     const struct pci_device_id __always_unused *ent)
3580 {
3581 	struct ice_pf *pf;
3582 	struct ice_hw *hw;
3583 	int err;
3584 
3585 	/* this driver uses devres, see Documentation/driver-model/devres.txt */
3586 	err = pcim_enable_device(pdev);
3587 	if (err)
3588 		return err;
3589 
3590 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3591 	if (err) {
3592 		dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
3593 		return err;
3594 	}
3595 
3596 	pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
3597 	if (!pf)
3598 		return -ENOMEM;
3599 
3600 	/* set up for high or low dma */
3601 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3602 	if (err)
3603 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3604 	if (err) {
3605 		dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3606 		return err;
3607 	}
3608 
3609 	pci_enable_pcie_error_reporting(pdev);
3610 	pci_set_master(pdev);
3611 
3612 	pf->pdev = pdev;
3613 	pci_set_drvdata(pdev, pf);
3614 	set_bit(__ICE_DOWN, pf->state);
3615 	/* Disable service task until DOWN bit is cleared */
3616 	set_bit(__ICE_SERVICE_DIS, pf->state);
3617 
3618 	hw = &pf->hw;
3619 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3620 	hw->back = pf;
3621 	hw->vendor_id = pdev->vendor;
3622 	hw->device_id = pdev->device;
3623 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3624 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3625 	hw->subsystem_device_id = pdev->subsystem_device;
3626 	hw->bus.device = PCI_SLOT(pdev->devfn);
3627 	hw->bus.func = PCI_FUNC(pdev->devfn);
3628 	ice_set_ctrlq_len(hw);
3629 
3630 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3631 
3632 #ifndef CONFIG_DYNAMIC_DEBUG
3633 	if (debug < -1)
3634 		hw->debug_mask = debug;
3635 #endif
3636 
3637 	err = ice_init_hw(hw);
3638 	if (err) {
3639 		dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
3640 		err = -EIO;
3641 		goto err_exit_unroll;
3642 	}
3643 
3644 	dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
3645 		 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
3646 		 hw->api_maj_ver, hw->api_min_ver);
3647 
3648 	ice_init_pf(pf);
3649 
3650 	ice_determine_q_usage(pf);
3651 
3652 	pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
3653 				  hw->func_caps.guaranteed_num_vsi);
3654 	if (!pf->num_alloc_vsi) {
3655 		err = -EIO;
3656 		goto err_init_pf_unroll;
3657 	}
3658 
3659 	pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
3660 			       sizeof(struct ice_vsi *), GFP_KERNEL);
3661 	if (!pf->vsi) {
3662 		err = -ENOMEM;
3663 		goto err_init_pf_unroll;
3664 	}
3665 
3666 	err = ice_init_interrupt_scheme(pf);
3667 	if (err) {
3668 		dev_err(&pdev->dev,
3669 			"ice_init_interrupt_scheme failed: %d\n", err);
3670 		err = -EIO;
3671 		goto err_init_interrupt_unroll;
3672 	}
3673 
3674 	/* Driver is mostly up */
3675 	clear_bit(__ICE_DOWN, pf->state);
3676 
3677 	/* In case of MSIX we are going to setup the misc vector right here
3678 	 * to handle admin queue events etc. In case of legacy and MSI
3679 	 * the misc functionality and queue processing is combined in
3680 	 * the same vector and that gets setup at open.
3681 	 */
3682 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3683 		err = ice_req_irq_msix_misc(pf);
3684 		if (err) {
3685 			dev_err(&pdev->dev,
3686 				"setup of misc vector failed: %d\n", err);
3687 			goto err_init_interrupt_unroll;
3688 		}
3689 	}
3690 
3691 	/* create switch struct for the switch element created by FW on boot */
3692 	pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
3693 				    GFP_KERNEL);
3694 	if (!pf->first_sw) {
3695 		err = -ENOMEM;
3696 		goto err_msix_misc_unroll;
3697 	}
3698 
3699 	if (hw->evb_veb)
3700 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3701 	else
3702 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
3703 
3704 	pf->first_sw->pf = pf;
3705 
3706 	/* record the sw_id available for later use */
3707 	pf->first_sw->sw_id = hw->port_info->sw_id;
3708 
3709 	err = ice_setup_pf_sw(pf);
3710 	if (err) {
3711 		dev_err(&pdev->dev,
3712 			"probe failed due to setup pf switch:%d\n", err);
3713 		goto err_alloc_sw_unroll;
3714 	}
3715 
3716 	clear_bit(__ICE_SERVICE_DIS, pf->state);
3717 
3718 	/* since everything is good, start the service timer */
3719 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3720 
3721 	err = ice_init_link_events(pf->hw.port_info);
3722 	if (err) {
3723 		dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
3724 		goto err_alloc_sw_unroll;
3725 	}
3726 
3727 	return 0;
3728 
3729 err_alloc_sw_unroll:
3730 	set_bit(__ICE_SERVICE_DIS, pf->state);
3731 	set_bit(__ICE_DOWN, pf->state);
3732 	devm_kfree(&pf->pdev->dev, pf->first_sw);
3733 err_msix_misc_unroll:
3734 	ice_free_irq_msix_misc(pf);
3735 err_init_interrupt_unroll:
3736 	ice_clear_interrupt_scheme(pf);
3737 	devm_kfree(&pdev->dev, pf->vsi);
3738 err_init_pf_unroll:
3739 	ice_deinit_pf(pf);
3740 	ice_deinit_hw(hw);
3741 err_exit_unroll:
3742 	pci_disable_pcie_error_reporting(pdev);
3743 	return err;
3744 }
3745 
3746 /**
3747  * ice_remove - Device removal routine
3748  * @pdev: PCI device information struct
3749  */
3750 static void ice_remove(struct pci_dev *pdev)
3751 {
3752 	struct ice_pf *pf = pci_get_drvdata(pdev);
3753 
3754 	if (!pf)
3755 		return;
3756 
3757 	set_bit(__ICE_DOWN, pf->state);
3758 	ice_service_task_stop(pf);
3759 
3760 	ice_vsi_release_all(pf);
3761 	ice_free_irq_msix_misc(pf);
3762 	ice_clear_interrupt_scheme(pf);
3763 	ice_deinit_pf(pf);
3764 	ice_deinit_hw(&pf->hw);
3765 	pci_disable_pcie_error_reporting(pdev);
3766 }
3767 
3768 /* ice_pci_tbl - PCI Device ID Table
3769  *
3770  * Wildcard entries (PCI_ANY_ID) should come last
3771  * Last entry must be all 0s
3772  *
3773  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3774  *   Class, Class Mask, private data (not used) }
3775  */
3776 static const struct pci_device_id ice_pci_tbl[] = {
3777 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
3778 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
3779 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
3780 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
3781 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
3782 	/* required last entry */
3783 	{ 0, }
3784 };
3785 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3786 
3787 static struct pci_driver ice_driver = {
3788 	.name = KBUILD_MODNAME,
3789 	.id_table = ice_pci_tbl,
3790 	.probe = ice_probe,
3791 	.remove = ice_remove,
3792 };
3793 
3794 /**
3795  * ice_module_init - Driver registration routine
3796  *
3797  * ice_module_init is the first routine called when the driver is
3798  * loaded. All it does is register with the PCI subsystem.
3799  */
3800 static int __init ice_module_init(void)
3801 {
3802 	int status;
3803 
3804 	pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3805 	pr_info("%s\n", ice_copyright);
3806 
3807 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
3808 	if (!ice_wq) {
3809 		pr_err("Failed to create workqueue\n");
3810 		return -ENOMEM;
3811 	}
3812 
3813 	status = pci_register_driver(&ice_driver);
3814 	if (status) {
3815 		pr_err("failed to register pci driver, err %d\n", status);
3816 		destroy_workqueue(ice_wq);
3817 	}
3818 
3819 	return status;
3820 }
3821 module_init(ice_module_init);
3822 
3823 /**
3824  * ice_module_exit - Driver exit cleanup routine
3825  *
3826  * ice_module_exit is called just before the driver is removed
3827  * from memory.
3828  */
3829 static void __exit ice_module_exit(void)
3830 {
3831 	pci_unregister_driver(&ice_driver);
3832 	destroy_workqueue(ice_wq);
3833 	pr_info("module unloaded\n");
3834 }
3835 module_exit(ice_module_exit);
3836 
3837 /**
3838  * ice_set_mac_address - NDO callback to set mac address
3839  * @netdev: network interface device structure
3840  * @pi: pointer to an address structure
3841  *
3842  * Returns 0 on success, negative on failure
3843  */
3844 static int ice_set_mac_address(struct net_device *netdev, void *pi)
3845 {
3846 	struct ice_netdev_priv *np = netdev_priv(netdev);
3847 	struct ice_vsi *vsi = np->vsi;
3848 	struct ice_pf *pf = vsi->back;
3849 	struct ice_hw *hw = &pf->hw;
3850 	struct sockaddr *addr = pi;
3851 	enum ice_status status;
3852 	LIST_HEAD(a_mac_list);
3853 	LIST_HEAD(r_mac_list);
3854 	u8 flags = 0;
3855 	int err;
3856 	u8 *mac;
3857 
3858 	mac = (u8 *)addr->sa_data;
3859 
3860 	if (!is_valid_ether_addr(mac))
3861 		return -EADDRNOTAVAIL;
3862 
3863 	if (ether_addr_equal(netdev->dev_addr, mac)) {
3864 		netdev_warn(netdev, "already using mac %pM\n", mac);
3865 		return 0;
3866 	}
3867 
3868 	if (test_bit(__ICE_DOWN, pf->state) ||
3869 	    ice_is_reset_recovery_pending(pf->state)) {
3870 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
3871 			   mac);
3872 		return -EBUSY;
3873 	}
3874 
3875 	/* When we change the mac address we also have to change the mac address
3876 	 * based filter rules that were created previously for the old mac
3877 	 * address. So first, we remove the old filter rule using ice_remove_mac
3878 	 * and then create a new filter rule using ice_add_mac. Note that for
3879 	 * both these operations, we first need to form a "list" of mac
3880 	 * addresses (even though in this case, we have only 1 mac address to be
3881 	 * added/removed) and this done using ice_add_mac_to_list. Depending on
3882 	 * the ensuing operation this "list" of mac addresses is either to be
3883 	 * added or removed from the filter.
3884 	 */
3885 	err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
3886 	if (err) {
3887 		err = -EADDRNOTAVAIL;
3888 		goto free_lists;
3889 	}
3890 
3891 	status = ice_remove_mac(hw, &r_mac_list);
3892 	if (status) {
3893 		err = -EADDRNOTAVAIL;
3894 		goto free_lists;
3895 	}
3896 
3897 	err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
3898 	if (err) {
3899 		err = -EADDRNOTAVAIL;
3900 		goto free_lists;
3901 	}
3902 
3903 	status = ice_add_mac(hw, &a_mac_list);
3904 	if (status) {
3905 		err = -EADDRNOTAVAIL;
3906 		goto free_lists;
3907 	}
3908 
3909 free_lists:
3910 	/* free list entries */
3911 	ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
3912 	ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
3913 
3914 	if (err) {
3915 		netdev_err(netdev, "can't set mac %pM. filter update failed\n",
3916 			   mac);
3917 		return err;
3918 	}
3919 
3920 	/* change the netdev's mac address */
3921 	memcpy(netdev->dev_addr, mac, netdev->addr_len);
3922 	netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
3923 		   netdev->dev_addr);
3924 
3925 	/* write new mac address to the firmware */
3926 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3927 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3928 	if (status) {
3929 		netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
3930 			   mac);
3931 	}
3932 	return 0;
3933 }
3934 
3935 /**
3936  * ice_set_rx_mode - NDO callback to set the netdev filters
3937  * @netdev: network interface device structure
3938  */
3939 static void ice_set_rx_mode(struct net_device *netdev)
3940 {
3941 	struct ice_netdev_priv *np = netdev_priv(netdev);
3942 	struct ice_vsi *vsi = np->vsi;
3943 
3944 	if (!vsi)
3945 		return;
3946 
3947 	/* Set the flags to synchronize filters
3948 	 * ndo_set_rx_mode may be triggered even without a change in netdev
3949 	 * flags
3950 	 */
3951 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3952 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3953 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3954 
3955 	/* schedule our worker thread which will take care of
3956 	 * applying the new filter changes
3957 	 */
3958 	ice_service_task_schedule(vsi->back);
3959 }
3960 
3961 /**
3962  * ice_fdb_add - add an entry to the hardware database
3963  * @ndm: the input from the stack
3964  * @tb: pointer to array of nladdr (unused)
3965  * @dev: the net device pointer
3966  * @addr: the MAC address entry being added
3967  * @vid: VLAN id
3968  * @flags: instructions from stack about fdb operation
3969  */
3970 static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3971 		       struct net_device *dev, const unsigned char *addr,
3972 		       u16 vid, u16 flags)
3973 {
3974 	int err;
3975 
3976 	if (vid) {
3977 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3978 		return -EINVAL;
3979 	}
3980 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3981 		netdev_err(dev, "FDB only supports static addresses\n");
3982 		return -EINVAL;
3983 	}
3984 
3985 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3986 		err = dev_uc_add_excl(dev, addr);
3987 	else if (is_multicast_ether_addr(addr))
3988 		err = dev_mc_add_excl(dev, addr);
3989 	else
3990 		err = -EINVAL;
3991 
3992 	/* Only return duplicate errors if NLM_F_EXCL is set */
3993 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3994 		err = 0;
3995 
3996 	return err;
3997 }
3998 
3999 /**
4000  * ice_fdb_del - delete an entry from the hardware database
4001  * @ndm: the input from the stack
4002  * @tb: pointer to array of nladdr (unused)
4003  * @dev: the net device pointer
4004  * @addr: the MAC address entry being added
4005  * @vid: VLAN id
4006  */
4007 static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
4008 		       struct net_device *dev, const unsigned char *addr,
4009 		       __always_unused u16 vid)
4010 {
4011 	int err;
4012 
4013 	if (ndm->ndm_state & NUD_PERMANENT) {
4014 		netdev_err(dev, "FDB only supports static addresses\n");
4015 		return -EINVAL;
4016 	}
4017 
4018 	if (is_unicast_ether_addr(addr))
4019 		err = dev_uc_del(dev, addr);
4020 	else if (is_multicast_ether_addr(addr))
4021 		err = dev_mc_del(dev, addr);
4022 	else
4023 		err = -EINVAL;
4024 
4025 	return err;
4026 }
4027 
4028 /**
4029  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
4030  * @vsi: the vsi being changed
4031  */
4032 static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
4033 {
4034 	struct device *dev = &vsi->back->pdev->dev;
4035 	struct ice_hw *hw = &vsi->back->hw;
4036 	struct ice_vsi_ctx ctxt = { 0 };
4037 	enum ice_status status;
4038 
4039 	/* Here we are configuring the VSI to let the driver add VLAN tags by
4040 	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
4041 	 * insertion happens in the Tx hot path, in ice_tx_map.
4042 	 */
4043 	ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
4044 
4045 	ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
4046 	ctxt.vsi_num = vsi->vsi_num;
4047 
4048 	status = ice_aq_update_vsi(hw, &ctxt, NULL);
4049 	if (status) {
4050 		dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
4051 			status, hw->adminq.sq_last_status);
4052 		return -EIO;
4053 	}
4054 
4055 	vsi->info.vlan_flags = ctxt.info.vlan_flags;
4056 	return 0;
4057 }
4058 
4059 /**
4060  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
4061  * @vsi: the vsi being changed
4062  * @ena: boolean value indicating if this is a enable or disable request
4063  */
4064 static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
4065 {
4066 	struct device *dev = &vsi->back->pdev->dev;
4067 	struct ice_hw *hw = &vsi->back->hw;
4068 	struct ice_vsi_ctx ctxt = { 0 };
4069 	enum ice_status status;
4070 
4071 	/* Here we are configuring what the VSI should do with the VLAN tag in
4072 	 * the Rx packet. We can either leave the tag in the packet or put it in
4073 	 * the Rx descriptor.
4074 	 */
4075 	if (ena) {
4076 		/* Strip VLAN tag from Rx packet and put it in the desc */
4077 		ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
4078 	} else {
4079 		/* Disable stripping. Leave tag in packet */
4080 		ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
4081 	}
4082 
4083 	/* Allow all packets untagged/tagged */
4084 	ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
4085 
4086 	ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
4087 	ctxt.vsi_num = vsi->vsi_num;
4088 
4089 	status = ice_aq_update_vsi(hw, &ctxt, NULL);
4090 	if (status) {
4091 		dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
4092 			ena, status, hw->adminq.sq_last_status);
4093 		return -EIO;
4094 	}
4095 
4096 	vsi->info.vlan_flags = ctxt.info.vlan_flags;
4097 	return 0;
4098 }
4099 
4100 /**
4101  * ice_set_features - set the netdev feature flags
4102  * @netdev: ptr to the netdev being adjusted
4103  * @features: the feature set that the stack is suggesting
4104  */
4105 static int ice_set_features(struct net_device *netdev,
4106 			    netdev_features_t features)
4107 {
4108 	struct ice_netdev_priv *np = netdev_priv(netdev);
4109 	struct ice_vsi *vsi = np->vsi;
4110 	int ret = 0;
4111 
4112 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
4113 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
4114 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
4115 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
4116 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
4117 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
4118 	else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
4119 		 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
4120 		ret = ice_vsi_manage_vlan_insertion(vsi);
4121 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
4122 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
4123 		ret = ice_vsi_manage_vlan_insertion(vsi);
4124 
4125 	return ret;
4126 }
4127 
4128 /**
4129  * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
4130  * @vsi: VSI to setup vlan properties for
4131  */
4132 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
4133 {
4134 	int ret = 0;
4135 
4136 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
4137 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
4138 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
4139 		ret = ice_vsi_manage_vlan_insertion(vsi);
4140 
4141 	return ret;
4142 }
4143 
4144 /**
4145  * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
4146  * @vsi: the VSI being brought back up
4147  */
4148 static int ice_restore_vlan(struct ice_vsi *vsi)
4149 {
4150 	int err;
4151 	u16 vid;
4152 
4153 	if (!vsi->netdev)
4154 		return -EINVAL;
4155 
4156 	err = ice_vsi_vlan_setup(vsi);
4157 	if (err)
4158 		return err;
4159 
4160 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
4161 		err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
4162 		if (err)
4163 			break;
4164 	}
4165 
4166 	return err;
4167 }
4168 
4169 /**
4170  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
4171  * @ring: The Tx ring to configure
4172  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
4173  * @pf_q: queue index in the PF space
4174  *
4175  * Configure the Tx descriptor ring in TLAN context.
4176  */
4177 static void
4178 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
4179 {
4180 	struct ice_vsi *vsi = ring->vsi;
4181 	struct ice_hw *hw = &vsi->back->hw;
4182 
4183 	tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
4184 
4185 	tlan_ctx->port_num = vsi->port_info->lport;
4186 
4187 	/* Transmit Queue Length */
4188 	tlan_ctx->qlen = ring->count;
4189 
4190 	/* PF number */
4191 	tlan_ctx->pf_num = hw->pf_id;
4192 
4193 	/* queue belongs to a specific VSI type
4194 	 * VF / VM index should be programmed per vmvf_type setting:
4195 	 * for vmvf_type = VF, it is VF number between 0-256
4196 	 * for vmvf_type = VM, it is VM number between 0-767
4197 	 * for PF or EMP this field should be set to zero
4198 	 */
4199 	switch (vsi->type) {
4200 	case ICE_VSI_PF:
4201 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
4202 		break;
4203 	default:
4204 		return;
4205 	}
4206 
4207 	/* make sure the context is associated with the right VSI */
4208 	tlan_ctx->src_vsi = vsi->vsi_num;
4209 
4210 	tlan_ctx->tso_ena = ICE_TX_LEGACY;
4211 	tlan_ctx->tso_qnum = pf_q;
4212 
4213 	/* Legacy or Advanced Host Interface:
4214 	 * 0: Advanced Host Interface
4215 	 * 1: Legacy Host Interface
4216 	 */
4217 	tlan_ctx->legacy_int = ICE_TX_LEGACY;
4218 }
4219 
4220 /**
4221  * ice_vsi_cfg_txqs - Configure the VSI for Tx
4222  * @vsi: the VSI being configured
4223  *
4224  * Return 0 on success and a negative value on error
4225  * Configure the Tx VSI for operation.
4226  */
4227 static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
4228 {
4229 	struct ice_aqc_add_tx_qgrp *qg_buf;
4230 	struct ice_aqc_add_txqs_perq *txq;
4231 	struct ice_pf *pf = vsi->back;
4232 	enum ice_status status;
4233 	u16 buf_len, i, pf_q;
4234 	int err = 0, tc = 0;
4235 	u8 num_q_grps;
4236 
4237 	buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
4238 	qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
4239 	if (!qg_buf)
4240 		return -ENOMEM;
4241 
4242 	if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
4243 		err = -EINVAL;
4244 		goto err_cfg_txqs;
4245 	}
4246 	qg_buf->num_txqs = 1;
4247 	num_q_grps = 1;
4248 
4249 	/* set up and configure the tx queues */
4250 	ice_for_each_txq(vsi, i) {
4251 		struct ice_tlan_ctx tlan_ctx = { 0 };
4252 
4253 		pf_q = vsi->txq_map[i];
4254 		ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
4255 		/* copy context contents into the qg_buf */
4256 		qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
4257 		ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
4258 			    ice_tlan_ctx_info);
4259 
4260 		/* init queue specific tail reg. It is referred as transmit
4261 		 * comm scheduler queue doorbell.
4262 		 */
4263 		vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
4264 		status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
4265 					 num_q_grps, qg_buf, buf_len, NULL);
4266 		if (status) {
4267 			dev_err(&vsi->back->pdev->dev,
4268 				"Failed to set LAN Tx queue context, error: %d\n",
4269 				status);
4270 			err = -ENODEV;
4271 			goto err_cfg_txqs;
4272 		}
4273 
4274 		/* Add Tx Queue TEID into the VSI tx ring from the response
4275 		 * This will complete configuring and enabling the queue.
4276 		 */
4277 		txq = &qg_buf->txqs[0];
4278 		if (pf_q == le16_to_cpu(txq->txq_id))
4279 			vsi->tx_rings[i]->txq_teid =
4280 				le32_to_cpu(txq->q_teid);
4281 	}
4282 err_cfg_txqs:
4283 	devm_kfree(&pf->pdev->dev, qg_buf);
4284 	return err;
4285 }
4286 
4287 /**
4288  * ice_setup_rx_ctx - Configure a receive ring context
4289  * @ring: The Rx ring to configure
4290  *
4291  * Configure the Rx descriptor ring in RLAN context.
4292  */
4293 static int ice_setup_rx_ctx(struct ice_ring *ring)
4294 {
4295 	struct ice_vsi *vsi = ring->vsi;
4296 	struct ice_hw *hw = &vsi->back->hw;
4297 	u32 rxdid = ICE_RXDID_FLEX_NIC;
4298 	struct ice_rlan_ctx rlan_ctx;
4299 	u32 regval;
4300 	u16 pf_q;
4301 	int err;
4302 
4303 	/* what is RX queue number in global space of 2K rx queues */
4304 	pf_q = vsi->rxq_map[ring->q_index];
4305 
4306 	/* clear the context structure first */
4307 	memset(&rlan_ctx, 0, sizeof(rlan_ctx));
4308 
4309 	rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
4310 
4311 	rlan_ctx.qlen = ring->count;
4312 
4313 	/* Receive Packet Data Buffer Size.
4314 	 * The Packet Data Buffer Size is defined in 128 byte units.
4315 	 */
4316 	rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
4317 
4318 	/* use 32 byte descriptors */
4319 	rlan_ctx.dsize = 1;
4320 
4321 	/* Strip the Ethernet CRC bytes before the packet is posted to host
4322 	 * memory.
4323 	 */
4324 	rlan_ctx.crcstrip = 1;
4325 
4326 	/* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
4327 	rlan_ctx.l2tsel = 1;
4328 
4329 	rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
4330 	rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
4331 	rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
4332 
4333 	/* This controls whether VLAN is stripped from inner headers
4334 	 * The VLAN in the inner L2 header is stripped to the receive
4335 	 * descriptor if enabled by this flag.
4336 	 */
4337 	rlan_ctx.showiv = 0;
4338 
4339 	/* Max packet size for this queue - must not be set to a larger value
4340 	 * than 5 x DBUF
4341 	 */
4342 	rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
4343 			       ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
4344 
4345 	/* Rx queue threshold in units of 64 */
4346 	rlan_ctx.lrxqthresh = 1;
4347 
4348 	 /* Enable Flexible Descriptors in the queue context which
4349 	  * allows this driver to select a specific receive descriptor format
4350 	  */
4351 	regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
4352 	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
4353 		QRXFLXP_CNTXT_RXDID_IDX_M;
4354 
4355 	/* increasing context priority to pick up profile id;
4356 	 * default is 0x01; setting to 0x03 to ensure profile
4357 	 * is programming if prev context is of same priority
4358 	 */
4359 	regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
4360 		QRXFLXP_CNTXT_RXDID_PRIO_M;
4361 
4362 	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
4363 
4364 	/* Absolute queue number out of 2K needs to be passed */
4365 	err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
4366 	if (err) {
4367 		dev_err(&vsi->back->pdev->dev,
4368 			"Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
4369 			pf_q, err);
4370 		return -EIO;
4371 	}
4372 
4373 	/* init queue specific tail register */
4374 	ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
4375 	writel(0, ring->tail);
4376 	ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
4377 
4378 	return 0;
4379 }
4380 
4381 /**
4382  * ice_vsi_cfg_rxqs - Configure the VSI for Rx
4383  * @vsi: the VSI being configured
4384  *
4385  * Return 0 on success and a negative value on error
4386  * Configure the Rx VSI for operation.
4387  */
4388 static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
4389 {
4390 	int err = 0;
4391 	u16 i;
4392 
4393 	if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
4394 		vsi->max_frame = vsi->netdev->mtu +
4395 			ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4396 	else
4397 		vsi->max_frame = ICE_RXBUF_2048;
4398 
4399 	vsi->rx_buf_len = ICE_RXBUF_2048;
4400 	/* set up individual rings */
4401 	for (i = 0; i < vsi->num_rxq && !err; i++)
4402 		err = ice_setup_rx_ctx(vsi->rx_rings[i]);
4403 
4404 	if (err) {
4405 		dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
4406 		return -EIO;
4407 	}
4408 	return err;
4409 }
4410 
4411 /**
4412  * ice_vsi_cfg - Setup the VSI
4413  * @vsi: the VSI being configured
4414  *
4415  * Return 0 on success and negative value on error
4416  */
4417 static int ice_vsi_cfg(struct ice_vsi *vsi)
4418 {
4419 	int err;
4420 
4421 	if (vsi->netdev) {
4422 		ice_set_rx_mode(vsi->netdev);
4423 		err = ice_restore_vlan(vsi);
4424 		if (err)
4425 			return err;
4426 	}
4427 
4428 	err = ice_vsi_cfg_txqs(vsi);
4429 	if (!err)
4430 		err = ice_vsi_cfg_rxqs(vsi);
4431 
4432 	return err;
4433 }
4434 
4435 /**
4436  * ice_vsi_stop_tx_rings - Disable Tx rings
4437  * @vsi: the VSI being configured
4438  */
4439 static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
4440 {
4441 	struct ice_pf *pf = vsi->back;
4442 	struct ice_hw *hw = &pf->hw;
4443 	enum ice_status status;
4444 	u32 *q_teids, val;
4445 	u16 *q_ids, i;
4446 	int err = 0;
4447 
4448 	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
4449 		return -EINVAL;
4450 
4451 	q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
4452 			       GFP_KERNEL);
4453 	if (!q_teids)
4454 		return -ENOMEM;
4455 
4456 	q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
4457 			     GFP_KERNEL);
4458 	if (!q_ids) {
4459 		err = -ENOMEM;
4460 		goto err_alloc_q_ids;
4461 	}
4462 
4463 	/* set up the tx queue list to be disabled */
4464 	ice_for_each_txq(vsi, i) {
4465 		u16 v_idx;
4466 
4467 		if (!vsi->tx_rings || !vsi->tx_rings[i]) {
4468 			err = -EINVAL;
4469 			goto err_out;
4470 		}
4471 
4472 		q_ids[i] = vsi->txq_map[i];
4473 		q_teids[i] = vsi->tx_rings[i]->txq_teid;
4474 
4475 		/* clear cause_ena bit for disabled queues */
4476 		val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4477 		val &= ~QINT_TQCTL_CAUSE_ENA_M;
4478 		wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4479 
4480 		/* software is expected to wait for 100 ns */
4481 		ndelay(100);
4482 
4483 		/* trigger a software interrupt for the vector associated to
4484 		 * the queue to schedule napi handler
4485 		 */
4486 		v_idx = vsi->tx_rings[i]->q_vector->v_idx;
4487 		wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
4488 		     GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
4489 	}
4490 	status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
4491 				 NULL);
4492 	/* if the disable queue command was exercised during an active reset
4493 	 * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
4494 	 * the reset operation disables queues at the hardware level anyway.
4495 	 */
4496 	if (status == ICE_ERR_RESET_ONGOING) {
4497 		dev_dbg(&pf->pdev->dev,
4498 			"Reset in progress. LAN Tx queues already disabled\n");
4499 	} else if (status) {
4500 		dev_err(&pf->pdev->dev,
4501 			"Failed to disable LAN Tx queues, error: %d\n",
4502 			status);
4503 		err = -ENODEV;
4504 	}
4505 
4506 err_out:
4507 	devm_kfree(&pf->pdev->dev, q_ids);
4508 
4509 err_alloc_q_ids:
4510 	devm_kfree(&pf->pdev->dev, q_teids);
4511 
4512 	return err;
4513 }
4514 
4515 /**
4516  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4517  * @pf: the PF being configured
4518  * @pf_q: the PF queue
4519  * @ena: enable or disable state of the queue
4520  *
4521  * This routine will wait for the given Rx queue of the PF to reach the
4522  * enabled or disabled state.
4523  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4524  * multiple retries; else will return 0 in case of success.
4525  */
4526 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
4527 {
4528 	int i;
4529 
4530 	for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
4531 		u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
4532 
4533 		if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4534 			break;
4535 
4536 		usleep_range(10, 20);
4537 	}
4538 	if (i >= ICE_Q_WAIT_RETRY_LIMIT)
4539 		return -ETIMEDOUT;
4540 
4541 	return 0;
4542 }
4543 
4544 /**
4545  * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
4546  * @vsi: the VSI being configured
4547  * @ena: start or stop the rx rings
4548  */
4549 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
4550 {
4551 	struct ice_pf *pf = vsi->back;
4552 	struct ice_hw *hw = &pf->hw;
4553 	int i, j, ret = 0;
4554 
4555 	for (i = 0; i < vsi->num_rxq; i++) {
4556 		int pf_q = vsi->rxq_map[i];
4557 		u32 rx_reg;
4558 
4559 		for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
4560 			rx_reg = rd32(hw, QRX_CTRL(pf_q));
4561 			if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
4562 			    ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
4563 				break;
4564 			usleep_range(1000, 2000);
4565 		}
4566 
4567 		/* Skip if the queue is already in the requested state */
4568 		if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4569 			continue;
4570 
4571 		/* turn on/off the queue */
4572 		if (ena)
4573 			rx_reg |= QRX_CTRL_QENA_REQ_M;
4574 		else
4575 			rx_reg &= ~QRX_CTRL_QENA_REQ_M;
4576 		wr32(hw, QRX_CTRL(pf_q), rx_reg);
4577 
4578 		/* wait for the change to finish */
4579 		ret = ice_pf_rxq_wait(pf, pf_q, ena);
4580 		if (ret) {
4581 			dev_err(&pf->pdev->dev,
4582 				"VSI idx %d Rx ring %d %sable timeout\n",
4583 				vsi->idx, pf_q, (ena ? "en" : "dis"));
4584 			break;
4585 		}
4586 	}
4587 
4588 	return ret;
4589 }
4590 
4591 /**
4592  * ice_vsi_start_rx_rings - start VSI's rx rings
4593  * @vsi: the VSI whose rings are to be started
4594  *
4595  * Returns 0 on success and a negative value on error
4596  */
4597 static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
4598 {
4599 	return ice_vsi_ctrl_rx_rings(vsi, true);
4600 }
4601 
4602 /**
4603  * ice_vsi_stop_rx_rings - stop VSI's rx rings
4604  * @vsi: the VSI
4605  *
4606  * Returns 0 on success and a negative value on error
4607  */
4608 static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
4609 {
4610 	return ice_vsi_ctrl_rx_rings(vsi, false);
4611 }
4612 
4613 /**
4614  * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
4615  * @vsi: the VSI
4616  * Returns 0 on success and a negative value on error
4617  */
4618 static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
4619 {
4620 	int err_tx, err_rx;
4621 
4622 	err_tx = ice_vsi_stop_tx_rings(vsi);
4623 	if (err_tx)
4624 		dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
4625 
4626 	err_rx = ice_vsi_stop_rx_rings(vsi);
4627 	if (err_rx)
4628 		dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
4629 
4630 	if (err_tx || err_rx)
4631 		return -EIO;
4632 
4633 	return 0;
4634 }
4635 
4636 /**
4637  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4638  * @vsi: the VSI being configured
4639  */
4640 static void ice_napi_enable_all(struct ice_vsi *vsi)
4641 {
4642 	int q_idx;
4643 
4644 	if (!vsi->netdev)
4645 		return;
4646 
4647 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4648 		napi_enable(&vsi->q_vectors[q_idx]->napi);
4649 }
4650 
4651 /**
4652  * ice_up_complete - Finish the last steps of bringing up a connection
4653  * @vsi: The VSI being configured
4654  *
4655  * Return 0 on success and negative value on error
4656  */
4657 static int ice_up_complete(struct ice_vsi *vsi)
4658 {
4659 	struct ice_pf *pf = vsi->back;
4660 	int err;
4661 
4662 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4663 		ice_vsi_cfg_msix(vsi);
4664 	else
4665 		return -ENOTSUPP;
4666 
4667 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
4668 	 * Tx queue group list was configured and the context bits were
4669 	 * programmed using ice_vsi_cfg_txqs
4670 	 */
4671 	err = ice_vsi_start_rx_rings(vsi);
4672 	if (err)
4673 		return err;
4674 
4675 	clear_bit(__ICE_DOWN, vsi->state);
4676 	ice_napi_enable_all(vsi);
4677 	ice_vsi_ena_irq(vsi);
4678 
4679 	if (vsi->port_info &&
4680 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
4681 	    vsi->netdev) {
4682 		ice_print_link_msg(vsi, true);
4683 		netif_tx_start_all_queues(vsi->netdev);
4684 		netif_carrier_on(vsi->netdev);
4685 	}
4686 
4687 	ice_service_task_schedule(pf);
4688 
4689 	return err;
4690 }
4691 
4692 /**
4693  * ice_up - Bring the connection back up after being down
4694  * @vsi: VSI being configured
4695  */
4696 int ice_up(struct ice_vsi *vsi)
4697 {
4698 	int err;
4699 
4700 	err = ice_vsi_cfg(vsi);
4701 	if (!err)
4702 		err = ice_up_complete(vsi);
4703 
4704 	return err;
4705 }
4706 
4707 /**
4708  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
4709  * @ring: Tx or Rx ring to read stats from
4710  * @pkts: packets stats counter
4711  * @bytes: bytes stats counter
4712  *
4713  * This function fetches stats from the ring considering the atomic operations
4714  * that needs to be performed to read u64 values in 32 bit machine.
4715  */
4716 static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
4717 					 u64 *bytes)
4718 {
4719 	unsigned int start;
4720 	*pkts = 0;
4721 	*bytes = 0;
4722 
4723 	if (!ring)
4724 		return;
4725 	do {
4726 		start = u64_stats_fetch_begin_irq(&ring->syncp);
4727 		*pkts = ring->stats.pkts;
4728 		*bytes = ring->stats.bytes;
4729 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4730 }
4731 
4732 /**
4733  * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4734  * @hw: ptr to the hardware info
4735  * @hireg: high 32 bit HW register to read from
4736  * @loreg: low 32 bit HW register to read from
4737  * @prev_stat_loaded: bool to specify if previous stats are loaded
4738  * @prev_stat: ptr to previous loaded stat value
4739  * @cur_stat: ptr to current stat value
4740  */
4741 static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
4742 			      bool prev_stat_loaded, u64 *prev_stat,
4743 			      u64 *cur_stat)
4744 {
4745 	u64 new_data;
4746 
4747 	new_data = rd32(hw, loreg);
4748 	new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
4749 
4750 	/* device stats are not reset at PFR, they likely will not be zeroed
4751 	 * when the driver starts. So save the first values read and use them as
4752 	 * offsets to be subtracted from the raw values in order to report stats
4753 	 * that count from zero.
4754 	 */
4755 	if (!prev_stat_loaded)
4756 		*prev_stat = new_data;
4757 	if (likely(new_data >= *prev_stat))
4758 		*cur_stat = new_data - *prev_stat;
4759 	else
4760 		/* to manage the potential roll-over */
4761 		*cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
4762 	*cur_stat &= 0xFFFFFFFFFFULL;
4763 }
4764 
4765 /**
4766  * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4767  * @hw: ptr to the hardware info
4768  * @reg: HW register to read from
4769  * @prev_stat_loaded: bool to specify if previous stats are loaded
4770  * @prev_stat: ptr to previous loaded stat value
4771  * @cur_stat: ptr to current stat value
4772  */
4773 static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4774 			      u64 *prev_stat, u64 *cur_stat)
4775 {
4776 	u32 new_data;
4777 
4778 	new_data = rd32(hw, reg);
4779 
4780 	/* device stats are not reset at PFR, they likely will not be zeroed
4781 	 * when the driver starts. So save the first values read and use them as
4782 	 * offsets to be subtracted from the raw values in order to report stats
4783 	 * that count from zero.
4784 	 */
4785 	if (!prev_stat_loaded)
4786 		*prev_stat = new_data;
4787 	if (likely(new_data >= *prev_stat))
4788 		*cur_stat = new_data - *prev_stat;
4789 	else
4790 		/* to manage the potential roll-over */
4791 		*cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
4792 }
4793 
4794 /**
4795  * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
4796  * @vsi: the VSI to be updated
4797  */
4798 static void ice_update_eth_stats(struct ice_vsi *vsi)
4799 {
4800 	struct ice_eth_stats *prev_es, *cur_es;
4801 	struct ice_hw *hw = &vsi->back->hw;
4802 	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
4803 
4804 	prev_es = &vsi->eth_stats_prev;
4805 	cur_es = &vsi->eth_stats;
4806 
4807 	ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
4808 			  vsi->stat_offsets_loaded, &prev_es->rx_bytes,
4809 			  &cur_es->rx_bytes);
4810 
4811 	ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
4812 			  vsi->stat_offsets_loaded, &prev_es->rx_unicast,
4813 			  &cur_es->rx_unicast);
4814 
4815 	ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
4816 			  vsi->stat_offsets_loaded, &prev_es->rx_multicast,
4817 			  &cur_es->rx_multicast);
4818 
4819 	ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
4820 			  vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
4821 			  &cur_es->rx_broadcast);
4822 
4823 	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
4824 			  &prev_es->rx_discards, &cur_es->rx_discards);
4825 
4826 	ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
4827 			  vsi->stat_offsets_loaded, &prev_es->tx_bytes,
4828 			  &cur_es->tx_bytes);
4829 
4830 	ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
4831 			  vsi->stat_offsets_loaded, &prev_es->tx_unicast,
4832 			  &cur_es->tx_unicast);
4833 
4834 	ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
4835 			  vsi->stat_offsets_loaded, &prev_es->tx_multicast,
4836 			  &cur_es->tx_multicast);
4837 
4838 	ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
4839 			  vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
4840 			  &cur_es->tx_broadcast);
4841 
4842 	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
4843 			  &prev_es->tx_errors, &cur_es->tx_errors);
4844 
4845 	vsi->stat_offsets_loaded = true;
4846 }
4847 
4848 /**
4849  * ice_update_vsi_ring_stats - Update VSI stats counters
4850  * @vsi: the VSI to be updated
4851  */
4852 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
4853 {
4854 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
4855 	struct ice_ring *ring;
4856 	u64 pkts, bytes;
4857 	int i;
4858 
4859 	/* reset netdev stats */
4860 	vsi_stats->tx_packets = 0;
4861 	vsi_stats->tx_bytes = 0;
4862 	vsi_stats->rx_packets = 0;
4863 	vsi_stats->rx_bytes = 0;
4864 
4865 	/* reset non-netdev (extended) stats */
4866 	vsi->tx_restart = 0;
4867 	vsi->tx_busy = 0;
4868 	vsi->tx_linearize = 0;
4869 	vsi->rx_buf_failed = 0;
4870 	vsi->rx_page_failed = 0;
4871 
4872 	rcu_read_lock();
4873 
4874 	/* update Tx rings counters */
4875 	ice_for_each_txq(vsi, i) {
4876 		ring = READ_ONCE(vsi->tx_rings[i]);
4877 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4878 		vsi_stats->tx_packets += pkts;
4879 		vsi_stats->tx_bytes += bytes;
4880 		vsi->tx_restart += ring->tx_stats.restart_q;
4881 		vsi->tx_busy += ring->tx_stats.tx_busy;
4882 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
4883 	}
4884 
4885 	/* update Rx rings counters */
4886 	ice_for_each_rxq(vsi, i) {
4887 		ring = READ_ONCE(vsi->rx_rings[i]);
4888 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4889 		vsi_stats->rx_packets += pkts;
4890 		vsi_stats->rx_bytes += bytes;
4891 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4892 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4893 	}
4894 
4895 	rcu_read_unlock();
4896 }
4897 
4898 /**
4899  * ice_update_vsi_stats - Update VSI stats counters
4900  * @vsi: the VSI to be updated
4901  */
4902 static void ice_update_vsi_stats(struct ice_vsi *vsi)
4903 {
4904 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4905 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
4906 	struct ice_pf *pf = vsi->back;
4907 
4908 	if (test_bit(__ICE_DOWN, vsi->state) ||
4909 	    test_bit(__ICE_CFG_BUSY, pf->state))
4910 		return;
4911 
4912 	/* get stats as recorded by Tx/Rx rings */
4913 	ice_update_vsi_ring_stats(vsi);
4914 
4915 	/* get VSI stats as recorded by the hardware */
4916 	ice_update_eth_stats(vsi);
4917 
4918 	cur_ns->tx_errors = cur_es->tx_errors;
4919 	cur_ns->rx_dropped = cur_es->rx_discards;
4920 	cur_ns->tx_dropped = cur_es->tx_discards;
4921 	cur_ns->multicast = cur_es->rx_multicast;
4922 
4923 	/* update some more netdev stats if this is main VSI */
4924 	if (vsi->type == ICE_VSI_PF) {
4925 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
4926 		cur_ns->rx_errors = pf->stats.crc_errors +
4927 				    pf->stats.illegal_bytes;
4928 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4929 	}
4930 }
4931 
4932 /**
4933  * ice_update_pf_stats - Update PF port stats counters
4934  * @pf: PF whose stats needs to be updated
4935  */
4936 static void ice_update_pf_stats(struct ice_pf *pf)
4937 {
4938 	struct ice_hw_port_stats *prev_ps, *cur_ps;
4939 	struct ice_hw *hw = &pf->hw;
4940 	u8 pf_id;
4941 
4942 	prev_ps = &pf->stats_prev;
4943 	cur_ps = &pf->stats;
4944 	pf_id = hw->pf_id;
4945 
4946 	ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
4947 			  pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
4948 			  &cur_ps->eth.rx_bytes);
4949 
4950 	ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
4951 			  pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
4952 			  &cur_ps->eth.rx_unicast);
4953 
4954 	ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
4955 			  pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
4956 			  &cur_ps->eth.rx_multicast);
4957 
4958 	ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
4959 			  pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
4960 			  &cur_ps->eth.rx_broadcast);
4961 
4962 	ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
4963 			  pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
4964 			  &cur_ps->eth.tx_bytes);
4965 
4966 	ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
4967 			  pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
4968 			  &cur_ps->eth.tx_unicast);
4969 
4970 	ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
4971 			  pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
4972 			  &cur_ps->eth.tx_multicast);
4973 
4974 	ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
4975 			  pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
4976 			  &cur_ps->eth.tx_broadcast);
4977 
4978 	ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
4979 			  &prev_ps->tx_dropped_link_down,
4980 			  &cur_ps->tx_dropped_link_down);
4981 
4982 	ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
4983 			  pf->stat_prev_loaded, &prev_ps->rx_size_64,
4984 			  &cur_ps->rx_size_64);
4985 
4986 	ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
4987 			  pf->stat_prev_loaded, &prev_ps->rx_size_127,
4988 			  &cur_ps->rx_size_127);
4989 
4990 	ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
4991 			  pf->stat_prev_loaded, &prev_ps->rx_size_255,
4992 			  &cur_ps->rx_size_255);
4993 
4994 	ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
4995 			  pf->stat_prev_loaded, &prev_ps->rx_size_511,
4996 			  &cur_ps->rx_size_511);
4997 
4998 	ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
4999 			  GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
5000 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
5001 
5002 	ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
5003 			  GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
5004 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5005 
5006 	ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
5007 			  GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
5008 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5009 
5010 	ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
5011 			  pf->stat_prev_loaded, &prev_ps->tx_size_64,
5012 			  &cur_ps->tx_size_64);
5013 
5014 	ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
5015 			  pf->stat_prev_loaded, &prev_ps->tx_size_127,
5016 			  &cur_ps->tx_size_127);
5017 
5018 	ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
5019 			  pf->stat_prev_loaded, &prev_ps->tx_size_255,
5020 			  &cur_ps->tx_size_255);
5021 
5022 	ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
5023 			  pf->stat_prev_loaded, &prev_ps->tx_size_511,
5024 			  &cur_ps->tx_size_511);
5025 
5026 	ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
5027 			  GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
5028 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5029 
5030 	ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
5031 			  GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
5032 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5033 
5034 	ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
5035 			  GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
5036 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5037 
5038 	ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
5039 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5040 
5041 	ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
5042 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5043 
5044 	ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
5045 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5046 
5047 	ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
5048 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5049 
5050 	ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
5051 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
5052 
5053 	ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
5054 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5055 
5056 	ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
5057 			  &prev_ps->mac_local_faults,
5058 			  &cur_ps->mac_local_faults);
5059 
5060 	ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
5061 			  &prev_ps->mac_remote_faults,
5062 			  &cur_ps->mac_remote_faults);
5063 
5064 	ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
5065 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5066 
5067 	ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
5068 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5069 
5070 	ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
5071 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5072 
5073 	ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
5074 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5075 
5076 	ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
5077 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5078 
5079 	pf->stat_prev_loaded = true;
5080 }
5081 
5082 /**
5083  * ice_get_stats64 - get statistics for network device structure
5084  * @netdev: network interface device structure
5085  * @stats: main device statistics structure
5086  */
5087 static
5088 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5089 {
5090 	struct ice_netdev_priv *np = netdev_priv(netdev);
5091 	struct rtnl_link_stats64 *vsi_stats;
5092 	struct ice_vsi *vsi = np->vsi;
5093 
5094 	vsi_stats = &vsi->net_stats;
5095 
5096 	if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
5097 		return;
5098 	/* netdev packet/byte stats come from ring counter. These are obtained
5099 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
5100 	 */
5101 	ice_update_vsi_ring_stats(vsi);
5102 	stats->tx_packets = vsi_stats->tx_packets;
5103 	stats->tx_bytes = vsi_stats->tx_bytes;
5104 	stats->rx_packets = vsi_stats->rx_packets;
5105 	stats->rx_bytes = vsi_stats->rx_bytes;
5106 
5107 	/* The rest of the stats can be read from the hardware but instead we
5108 	 * just return values that the watchdog task has already obtained from
5109 	 * the hardware.
5110 	 */
5111 	stats->multicast = vsi_stats->multicast;
5112 	stats->tx_errors = vsi_stats->tx_errors;
5113 	stats->tx_dropped = vsi_stats->tx_dropped;
5114 	stats->rx_errors = vsi_stats->rx_errors;
5115 	stats->rx_dropped = vsi_stats->rx_dropped;
5116 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5117 	stats->rx_length_errors = vsi_stats->rx_length_errors;
5118 }
5119 
5120 #ifdef CONFIG_NET_POLL_CONTROLLER
5121 /**
5122  * ice_netpoll - polling "interrupt" handler
5123  * @netdev: network interface device structure
5124  *
5125  * Used by netconsole to send skbs without having to re-enable interrupts.
5126  * This is not called in the normal interrupt path.
5127  */
5128 static void ice_netpoll(struct net_device *netdev)
5129 {
5130 	struct ice_netdev_priv *np = netdev_priv(netdev);
5131 	struct ice_vsi *vsi = np->vsi;
5132 	struct ice_pf *pf = vsi->back;
5133 	int i;
5134 
5135 	if (test_bit(__ICE_DOWN, vsi->state) ||
5136 	    !test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
5137 		return;
5138 
5139 	for (i = 0; i < vsi->num_q_vectors; i++)
5140 		ice_msix_clean_rings(0, vsi->q_vectors[i]);
5141 }
5142 #endif /* CONFIG_NET_POLL_CONTROLLER */
5143 
5144 /**
5145  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5146  * @vsi: VSI having NAPI disabled
5147  */
5148 static void ice_napi_disable_all(struct ice_vsi *vsi)
5149 {
5150 	int q_idx;
5151 
5152 	if (!vsi->netdev)
5153 		return;
5154 
5155 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
5156 		napi_disable(&vsi->q_vectors[q_idx]->napi);
5157 }
5158 
5159 /**
5160  * ice_down - Shutdown the connection
5161  * @vsi: The VSI being stopped
5162  */
5163 int ice_down(struct ice_vsi *vsi)
5164 {
5165 	int i, err;
5166 
5167 	/* Caller of this function is expected to set the
5168 	 * vsi->state __ICE_DOWN bit
5169 	 */
5170 	if (vsi->netdev) {
5171 		netif_carrier_off(vsi->netdev);
5172 		netif_tx_disable(vsi->netdev);
5173 	}
5174 
5175 	ice_vsi_dis_irq(vsi);
5176 	err = ice_vsi_stop_tx_rx_rings(vsi);
5177 	ice_napi_disable_all(vsi);
5178 
5179 	ice_for_each_txq(vsi, i)
5180 		ice_clean_tx_ring(vsi->tx_rings[i]);
5181 
5182 	ice_for_each_rxq(vsi, i)
5183 		ice_clean_rx_ring(vsi->rx_rings[i]);
5184 
5185 	if (err)
5186 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5187 			   vsi->vsi_num, vsi->vsw->sw_id);
5188 	return err;
5189 }
5190 
5191 /**
5192  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5193  * @vsi: VSI having resources allocated
5194  *
5195  * Return 0 on success, negative on failure
5196  */
5197 static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5198 {
5199 	int i, err = 0;
5200 
5201 	if (!vsi->num_txq) {
5202 		dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
5203 			vsi->vsi_num);
5204 		return -EINVAL;
5205 	}
5206 
5207 	ice_for_each_txq(vsi, i) {
5208 		err = ice_setup_tx_ring(vsi->tx_rings[i]);
5209 		if (err)
5210 			break;
5211 	}
5212 
5213 	return err;
5214 }
5215 
5216 /**
5217  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5218  * @vsi: VSI having resources allocated
5219  *
5220  * Return 0 on success, negative on failure
5221  */
5222 static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5223 {
5224 	int i, err = 0;
5225 
5226 	if (!vsi->num_rxq) {
5227 		dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
5228 			vsi->vsi_num);
5229 		return -EINVAL;
5230 	}
5231 
5232 	ice_for_each_rxq(vsi, i) {
5233 		err = ice_setup_rx_ring(vsi->rx_rings[i]);
5234 		if (err)
5235 			break;
5236 	}
5237 
5238 	return err;
5239 }
5240 
5241 /**
5242  * ice_vsi_req_irq - Request IRQ from the OS
5243  * @vsi: The VSI IRQ is being requested for
5244  * @basename: name for the vector
5245  *
5246  * Return 0 on success and a negative value on error
5247  */
5248 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
5249 {
5250 	struct ice_pf *pf = vsi->back;
5251 	int err = -EINVAL;
5252 
5253 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
5254 		err = ice_vsi_req_irq_msix(vsi, basename);
5255 
5256 	return err;
5257 }
5258 
5259 /**
5260  * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
5261  * @vsi: the VSI having resources freed
5262  */
5263 static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
5264 {
5265 	int i;
5266 
5267 	if (!vsi->tx_rings)
5268 		return;
5269 
5270 	ice_for_each_txq(vsi, i)
5271 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
5272 			ice_free_tx_ring(vsi->tx_rings[i]);
5273 }
5274 
5275 /**
5276  * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
5277  * @vsi: the VSI having resources freed
5278  */
5279 static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
5280 {
5281 	int i;
5282 
5283 	if (!vsi->rx_rings)
5284 		return;
5285 
5286 	ice_for_each_rxq(vsi, i)
5287 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
5288 			ice_free_rx_ring(vsi->rx_rings[i]);
5289 }
5290 
5291 /**
5292  * ice_vsi_open - Called when a network interface is made active
5293  * @vsi: the VSI to open
5294  *
5295  * Initialization of the VSI
5296  *
5297  * Returns 0 on success, negative value on error
5298  */
5299 static int ice_vsi_open(struct ice_vsi *vsi)
5300 {
5301 	char int_name[ICE_INT_NAME_STR_LEN];
5302 	struct ice_pf *pf = vsi->back;
5303 	int err;
5304 
5305 	/* allocate descriptors */
5306 	err = ice_vsi_setup_tx_rings(vsi);
5307 	if (err)
5308 		goto err_setup_tx;
5309 
5310 	err = ice_vsi_setup_rx_rings(vsi);
5311 	if (err)
5312 		goto err_setup_rx;
5313 
5314 	err = ice_vsi_cfg(vsi);
5315 	if (err)
5316 		goto err_setup_rx;
5317 
5318 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5319 		 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5320 	err = ice_vsi_req_irq(vsi, int_name);
5321 	if (err)
5322 		goto err_setup_rx;
5323 
5324 	/* Notify the stack of the actual queue counts. */
5325 	err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5326 	if (err)
5327 		goto err_set_qs;
5328 
5329 	err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5330 	if (err)
5331 		goto err_set_qs;
5332 
5333 	err = ice_up_complete(vsi);
5334 	if (err)
5335 		goto err_up_complete;
5336 
5337 	return 0;
5338 
5339 err_up_complete:
5340 	ice_down(vsi);
5341 err_set_qs:
5342 	ice_vsi_free_irq(vsi);
5343 err_setup_rx:
5344 	ice_vsi_free_rx_rings(vsi);
5345 err_setup_tx:
5346 	ice_vsi_free_tx_rings(vsi);
5347 
5348 	return err;
5349 }
5350 
5351 /**
5352  * ice_vsi_close - Shut down a VSI
5353  * @vsi: the VSI being shut down
5354  */
5355 static void ice_vsi_close(struct ice_vsi *vsi)
5356 {
5357 	if (!test_and_set_bit(__ICE_DOWN, vsi->state))
5358 		ice_down(vsi);
5359 
5360 	ice_vsi_free_irq(vsi);
5361 	ice_vsi_free_tx_rings(vsi);
5362 	ice_vsi_free_rx_rings(vsi);
5363 }
5364 
5365 /**
5366  * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
5367  * @vsi: the VSI being removed
5368  */
5369 static void ice_rss_clean(struct ice_vsi *vsi)
5370 {
5371 	struct ice_pf *pf;
5372 
5373 	pf = vsi->back;
5374 
5375 	if (vsi->rss_hkey_user)
5376 		devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
5377 	if (vsi->rss_lut_user)
5378 		devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
5379 }
5380 
5381 /**
5382  * ice_vsi_release - Delete a VSI and free its resources
5383  * @vsi: the VSI being removed
5384  *
5385  * Returns 0 on success or < 0 on error
5386  */
5387 static int ice_vsi_release(struct ice_vsi *vsi)
5388 {
5389 	struct ice_pf *pf;
5390 
5391 	if (!vsi->back)
5392 		return -ENODEV;
5393 	pf = vsi->back;
5394 	/* do not unregister and free netdevs while driver is in the reset
5395 	 * recovery pending state. Since reset/rebuild happens through PF
5396 	 * service task workqueue, its not a good idea to unregister netdev
5397 	 * that is associated to the PF that is running the work queue items
5398 	 * currently. This is done to avoid check_flush_dependency() warning
5399 	 * on this wq
5400 	 */
5401 	if (vsi->netdev && !ice_is_reset_recovery_pending(pf->state)) {
5402 		unregister_netdev(vsi->netdev);
5403 		free_netdev(vsi->netdev);
5404 		vsi->netdev = NULL;
5405 	}
5406 
5407 	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
5408 		ice_rss_clean(vsi);
5409 
5410 	/* Disable VSI and free resources */
5411 	ice_vsi_dis_irq(vsi);
5412 	ice_vsi_close(vsi);
5413 
5414 	/* reclaim interrupt vectors back to PF */
5415 	ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
5416 	pf->num_avail_msix += vsi->num_q_vectors;
5417 
5418 	ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
5419 	ice_vsi_delete(vsi);
5420 	ice_vsi_free_q_vectors(vsi);
5421 	ice_vsi_clear_rings(vsi);
5422 
5423 	ice_vsi_put_qs(vsi);
5424 	pf->q_left_tx += vsi->alloc_txq;
5425 	pf->q_left_rx += vsi->alloc_rxq;
5426 
5427 	/* retain SW VSI data structure since it is needed to unregister and
5428 	 * free VSI netdev when PF is not in reset recovery pending state,\
5429 	 * for ex: during rmmod.
5430 	 */
5431 	if (!ice_is_reset_recovery_pending(pf->state))
5432 		ice_vsi_clear(vsi);
5433 
5434 	return 0;
5435 }
5436 
5437 /**
5438  * ice_vsi_release_all - Delete all VSIs
5439  * @pf: PF from which all VSIs are being removed
5440  */
5441 static void ice_vsi_release_all(struct ice_pf *pf)
5442 {
5443 	int err, i;
5444 
5445 	if (!pf->vsi)
5446 		return;
5447 
5448 	for (i = 0; i < pf->num_alloc_vsi; i++) {
5449 		if (!pf->vsi[i])
5450 			continue;
5451 
5452 		err = ice_vsi_release(pf->vsi[i]);
5453 		if (err)
5454 			dev_dbg(&pf->pdev->dev,
5455 				"Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5456 				i, err, pf->vsi[i]->vsi_num);
5457 	}
5458 }
5459 
5460 /**
5461  * ice_dis_vsi - pause a VSI
5462  * @vsi: the VSI being paused
5463  */
5464 static void ice_dis_vsi(struct ice_vsi *vsi)
5465 {
5466 	if (test_bit(__ICE_DOWN, vsi->state))
5467 		return;
5468 
5469 	set_bit(__ICE_NEEDS_RESTART, vsi->state);
5470 
5471 	if (vsi->netdev && netif_running(vsi->netdev) &&
5472 	    vsi->type == ICE_VSI_PF) {
5473 		rtnl_lock();
5474 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5475 		rtnl_unlock();
5476 	} else {
5477 		ice_vsi_close(vsi);
5478 	}
5479 }
5480 
5481 /**
5482  * ice_ena_vsi - resume a VSI
5483  * @vsi: the VSI being resume
5484  */
5485 static int ice_ena_vsi(struct ice_vsi *vsi)
5486 {
5487 	int err = 0;
5488 
5489 	if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
5490 		if (vsi->netdev && netif_running(vsi->netdev)) {
5491 			rtnl_lock();
5492 			err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5493 			rtnl_unlock();
5494 		}
5495 
5496 	return err;
5497 }
5498 
5499 /**
5500  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
5501  * @pf: the PF
5502  */
5503 static void ice_pf_dis_all_vsi(struct ice_pf *pf)
5504 {
5505 	int v;
5506 
5507 	ice_for_each_vsi(pf, v)
5508 		if (pf->vsi[v])
5509 			ice_dis_vsi(pf->vsi[v]);
5510 }
5511 
5512 /**
5513  * ice_pf_ena_all_vsi - Resume all VSIs on a PF
5514  * @pf: the PF
5515  */
5516 static int ice_pf_ena_all_vsi(struct ice_pf *pf)
5517 {
5518 	int v;
5519 
5520 	ice_for_each_vsi(pf, v)
5521 		if (pf->vsi[v])
5522 			if (ice_ena_vsi(pf->vsi[v]))
5523 				return -EIO;
5524 
5525 	return 0;
5526 }
5527 
5528 /**
5529  * ice_vsi_rebuild_all - rebuild all VSIs in pf
5530  * @pf: the PF
5531  */
5532 static int ice_vsi_rebuild_all(struct ice_pf *pf)
5533 {
5534 	int i;
5535 
5536 	/* loop through pf->vsi array and reinit the VSI if found */
5537 	for (i = 0; i < pf->num_alloc_vsi; i++) {
5538 		int err;
5539 
5540 		if (!pf->vsi[i])
5541 			continue;
5542 
5543 		err = ice_vsi_rebuild(pf->vsi[i]);
5544 		if (err) {
5545 			dev_err(&pf->pdev->dev,
5546 				"VSI at index %d rebuild failed\n",
5547 				pf->vsi[i]->idx);
5548 			return err;
5549 		}
5550 
5551 		dev_info(&pf->pdev->dev,
5552 			 "VSI at index %d rebuilt. vsi_num = 0x%x\n",
5553 			 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
5554 	}
5555 
5556 	return 0;
5557 }
5558 
5559 /**
5560  * ice_rebuild - rebuild after reset
5561  * @pf: pf to rebuild
5562  */
5563 static void ice_rebuild(struct ice_pf *pf)
5564 {
5565 	struct device *dev = &pf->pdev->dev;
5566 	struct ice_hw *hw = &pf->hw;
5567 	enum ice_status ret;
5568 	int err;
5569 
5570 	if (test_bit(__ICE_DOWN, pf->state))
5571 		goto clear_recovery;
5572 
5573 	dev_dbg(dev, "rebuilding pf\n");
5574 
5575 	ret = ice_init_all_ctrlq(hw);
5576 	if (ret) {
5577 		dev_err(dev, "control queues init failed %d\n", ret);
5578 		goto err_init_ctrlq;
5579 	}
5580 
5581 	ret = ice_clear_pf_cfg(hw);
5582 	if (ret) {
5583 		dev_err(dev, "clear PF configuration failed %d\n", ret);
5584 		goto err_init_ctrlq;
5585 	}
5586 
5587 	ice_clear_pxe_mode(hw);
5588 
5589 	ret = ice_get_caps(hw);
5590 	if (ret) {
5591 		dev_err(dev, "ice_get_caps failed %d\n", ret);
5592 		goto err_init_ctrlq;
5593 	}
5594 
5595 	err = ice_sched_init_port(hw->port_info);
5596 	if (err)
5597 		goto err_sched_init_port;
5598 
5599 	err = ice_vsi_rebuild_all(pf);
5600 	if (err) {
5601 		dev_err(dev, "ice_vsi_rebuild_all failed\n");
5602 		goto err_vsi_rebuild;
5603 	}
5604 
5605 	ret = ice_replay_all_fltr(&pf->hw);
5606 	if (ret) {
5607 		dev_err(&pf->pdev->dev,
5608 			"error replaying switch filter rules\n");
5609 		goto err_vsi_rebuild;
5610 	}
5611 
5612 	/* start misc vector */
5613 	if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
5614 		err = ice_req_irq_msix_misc(pf);
5615 		if (err) {
5616 			dev_err(dev, "misc vector setup failed: %d\n", err);
5617 			goto err_vsi_rebuild;
5618 		}
5619 	}
5620 
5621 	/* restart the VSIs that were rebuilt and running before the reset */
5622 	err = ice_pf_ena_all_vsi(pf);
5623 	if (err) {
5624 		dev_err(&pf->pdev->dev, "error enabling VSIs\n");
5625 		/* no need to disable VSIs in tear down path in ice_rebuild()
5626 		 * since its already taken care in ice_vsi_open()
5627 		 */
5628 		goto err_vsi_rebuild;
5629 	}
5630 
5631 	/* if we get here, reset flow is successful */
5632 	clear_bit(__ICE_RESET_FAILED, pf->state);
5633 	return;
5634 
5635 err_vsi_rebuild:
5636 	ice_vsi_release_all(pf);
5637 err_sched_init_port:
5638 	ice_sched_cleanup_all(hw);
5639 err_init_ctrlq:
5640 	ice_shutdown_all_ctrlq(hw);
5641 	set_bit(__ICE_RESET_FAILED, pf->state);
5642 clear_recovery:
5643 	/* set this bit in PF state to control service task scheduling */
5644 	set_bit(__ICE_NEEDS_RESTART, pf->state);
5645 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
5646 }
5647 
5648 /**
5649  * ice_change_mtu - NDO callback to change the MTU
5650  * @netdev: network interface device structure
5651  * @new_mtu: new value for maximum frame size
5652  *
5653  * Returns 0 on success, negative on failure
5654  */
5655 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
5656 {
5657 	struct ice_netdev_priv *np = netdev_priv(netdev);
5658 	struct ice_vsi *vsi = np->vsi;
5659 	struct ice_pf *pf = vsi->back;
5660 	u8 count = 0;
5661 
5662 	if (new_mtu == netdev->mtu) {
5663 		netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
5664 		return 0;
5665 	}
5666 
5667 	if (new_mtu < netdev->min_mtu) {
5668 		netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
5669 			   netdev->min_mtu);
5670 		return -EINVAL;
5671 	} else if (new_mtu > netdev->max_mtu) {
5672 		netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
5673 			   netdev->min_mtu);
5674 		return -EINVAL;
5675 	}
5676 	/* if a reset is in progress, wait for some time for it to complete */
5677 	do {
5678 		if (ice_is_reset_recovery_pending(pf->state)) {
5679 			count++;
5680 			usleep_range(1000, 2000);
5681 		} else {
5682 			break;
5683 		}
5684 
5685 	} while (count < 100);
5686 
5687 	if (count == 100) {
5688 		netdev_err(netdev, "can't change mtu. Device is busy\n");
5689 		return -EBUSY;
5690 	}
5691 
5692 	netdev->mtu = new_mtu;
5693 
5694 	/* if VSI is up, bring it down and then back up */
5695 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
5696 		int err;
5697 
5698 		err = ice_down(vsi);
5699 		if (err) {
5700 			netdev_err(netdev, "change mtu if_up err %d\n", err);
5701 			return err;
5702 		}
5703 
5704 		err = ice_up(vsi);
5705 		if (err) {
5706 			netdev_err(netdev, "change mtu if_up err %d\n", err);
5707 			return err;
5708 		}
5709 	}
5710 
5711 	netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
5712 	return 0;
5713 }
5714 
5715 /**
5716  * ice_set_rss - Set RSS keys and lut
5717  * @vsi: Pointer to VSI structure
5718  * @seed: RSS hash seed
5719  * @lut: Lookup table
5720  * @lut_size: Lookup table size
5721  *
5722  * Returns 0 on success, negative on failure
5723  */
5724 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5725 {
5726 	struct ice_pf *pf = vsi->back;
5727 	struct ice_hw *hw = &pf->hw;
5728 	enum ice_status status;
5729 
5730 	if (seed) {
5731 		struct ice_aqc_get_set_rss_keys *buf =
5732 				  (struct ice_aqc_get_set_rss_keys *)seed;
5733 
5734 		status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
5735 
5736 		if (status) {
5737 			dev_err(&pf->pdev->dev,
5738 				"Cannot set RSS key, err %d aq_err %d\n",
5739 				status, hw->adminq.rq_last_status);
5740 			return -EIO;
5741 		}
5742 	}
5743 
5744 	if (lut) {
5745 		status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
5746 					    vsi->rss_lut_type, lut, lut_size);
5747 		if (status) {
5748 			dev_err(&pf->pdev->dev,
5749 				"Cannot set RSS lut, err %d aq_err %d\n",
5750 				status, hw->adminq.rq_last_status);
5751 			return -EIO;
5752 		}
5753 	}
5754 
5755 	return 0;
5756 }
5757 
5758 /**
5759  * ice_get_rss - Get RSS keys and lut
5760  * @vsi: Pointer to VSI structure
5761  * @seed: Buffer to store the keys
5762  * @lut: Buffer to store the lookup table entries
5763  * @lut_size: Size of buffer to store the lookup table entries
5764  *
5765  * Returns 0 on success, negative on failure
5766  */
5767 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5768 {
5769 	struct ice_pf *pf = vsi->back;
5770 	struct ice_hw *hw = &pf->hw;
5771 	enum ice_status status;
5772 
5773 	if (seed) {
5774 		struct ice_aqc_get_set_rss_keys *buf =
5775 				  (struct ice_aqc_get_set_rss_keys *)seed;
5776 
5777 		status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
5778 		if (status) {
5779 			dev_err(&pf->pdev->dev,
5780 				"Cannot get RSS key, err %d aq_err %d\n",
5781 				status, hw->adminq.rq_last_status);
5782 			return -EIO;
5783 		}
5784 	}
5785 
5786 	if (lut) {
5787 		status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
5788 					    vsi->rss_lut_type, lut, lut_size);
5789 		if (status) {
5790 			dev_err(&pf->pdev->dev,
5791 				"Cannot get RSS lut, err %d aq_err %d\n",
5792 				status, hw->adminq.rq_last_status);
5793 			return -EIO;
5794 		}
5795 	}
5796 
5797 	return 0;
5798 }
5799 
5800 /**
5801  * ice_bridge_getlink - Get the hardware bridge mode
5802  * @skb: skb buff
5803  * @pid: process id
5804  * @seq: RTNL message seq
5805  * @dev: the netdev being configured
5806  * @filter_mask: filter mask passed in
5807  * @nlflags: netlink flags passed in
5808  *
5809  * Return the bridge mode (VEB/VEPA)
5810  */
5811 static int
5812 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
5813 		   struct net_device *dev, u32 filter_mask, int nlflags)
5814 {
5815 	struct ice_netdev_priv *np = netdev_priv(dev);
5816 	struct ice_vsi *vsi = np->vsi;
5817 	struct ice_pf *pf = vsi->back;
5818 	u16 bmode;
5819 
5820 	bmode = pf->first_sw->bridge_mode;
5821 
5822 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
5823 				       filter_mask, NULL);
5824 }
5825 
5826 /**
5827  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
5828  * @vsi: Pointer to VSI structure
5829  * @bmode: Hardware bridge mode (VEB/VEPA)
5830  *
5831  * Returns 0 on success, negative on failure
5832  */
5833 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
5834 {
5835 	struct device *dev = &vsi->back->pdev->dev;
5836 	struct ice_aqc_vsi_props *vsi_props;
5837 	struct ice_hw *hw = &vsi->back->hw;
5838 	struct ice_vsi_ctx ctxt = { 0 };
5839 	enum ice_status status;
5840 
5841 	vsi_props = &vsi->info;
5842 	ctxt.info = vsi->info;
5843 
5844 	if (bmode == BRIDGE_MODE_VEB)
5845 		/* change from VEPA to VEB mode */
5846 		ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
5847 	else
5848 		/* change from VEB to VEPA mode */
5849 		ctxt.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
5850 	ctxt.vsi_num = vsi->vsi_num;
5851 	ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
5852 	status = ice_aq_update_vsi(hw, &ctxt, NULL);
5853 	if (status) {
5854 		dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
5855 			bmode, status, hw->adminq.sq_last_status);
5856 		return -EIO;
5857 	}
5858 	/* Update sw flags for book keeping */
5859 	vsi_props->sw_flags = ctxt.info.sw_flags;
5860 
5861 	return 0;
5862 }
5863 
5864 /**
5865  * ice_bridge_setlink - Set the hardware bridge mode
5866  * @dev: the netdev being configured
5867  * @nlh: RTNL message
5868  * @flags: bridge setlink flags
5869  *
5870  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
5871  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
5872  * not already set for all VSIs connected to this switch. And also update the
5873  * unicast switch filter rules for the corresponding switch of the netdev.
5874  */
5875 static int
5876 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
5877 		   u16 __always_unused flags)
5878 {
5879 	struct ice_netdev_priv *np = netdev_priv(dev);
5880 	struct ice_pf *pf = np->vsi->back;
5881 	struct nlattr *attr, *br_spec;
5882 	struct ice_hw *hw = &pf->hw;
5883 	enum ice_status status;
5884 	struct ice_sw *pf_sw;
5885 	int rem, v, err = 0;
5886 
5887 	pf_sw = pf->first_sw;
5888 	/* find the attribute in the netlink message */
5889 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5890 
5891 	nla_for_each_nested(attr, br_spec, rem) {
5892 		__u16 mode;
5893 
5894 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
5895 			continue;
5896 		mode = nla_get_u16(attr);
5897 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
5898 			return -EINVAL;
5899 		/* Continue  if bridge mode is not being flipped */
5900 		if (mode == pf_sw->bridge_mode)
5901 			continue;
5902 		/* Iterates through the PF VSI list and update the loopback
5903 		 * mode of the VSI
5904 		 */
5905 		ice_for_each_vsi(pf, v) {
5906 			if (!pf->vsi[v])
5907 				continue;
5908 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
5909 			if (err)
5910 				return err;
5911 		}
5912 
5913 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
5914 		/* Update the unicast switch filter rules for the corresponding
5915 		 * switch of the netdev
5916 		 */
5917 		status = ice_update_sw_rule_bridge_mode(hw);
5918 		if (status) {
5919 			netdev_err(dev, "update SW_RULE for bridge mode failed,  = %d err %d aq_err %d\n",
5920 				   mode, status, hw->adminq.sq_last_status);
5921 			/* revert hw->evb_veb */
5922 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
5923 			return -EIO;
5924 		}
5925 
5926 		pf_sw->bridge_mode = mode;
5927 	}
5928 
5929 	return 0;
5930 }
5931 
5932 /**
5933  * ice_tx_timeout - Respond to a Tx Hang
5934  * @netdev: network interface device structure
5935  */
5936 static void ice_tx_timeout(struct net_device *netdev)
5937 {
5938 	struct ice_netdev_priv *np = netdev_priv(netdev);
5939 	struct ice_ring *tx_ring = NULL;
5940 	struct ice_vsi *vsi = np->vsi;
5941 	struct ice_pf *pf = vsi->back;
5942 	u32 head, val = 0, i;
5943 	int hung_queue = -1;
5944 
5945 	pf->tx_timeout_count++;
5946 
5947 	/* find the stopped queue the same way the stack does */
5948 	for (i = 0; i < netdev->num_tx_queues; i++) {
5949 		struct netdev_queue *q;
5950 		unsigned long trans_start;
5951 
5952 		q = netdev_get_tx_queue(netdev, i);
5953 		trans_start = q->trans_start;
5954 		if (netif_xmit_stopped(q) &&
5955 		    time_after(jiffies,
5956 			       (trans_start + netdev->watchdog_timeo))) {
5957 			hung_queue = i;
5958 			break;
5959 		}
5960 	}
5961 
5962 	if (i == netdev->num_tx_queues) {
5963 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
5964 	} else {
5965 		/* now that we have an index, find the tx_ring struct */
5966 		for (i = 0; i < vsi->num_txq; i++) {
5967 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
5968 				if (hung_queue ==
5969 				    vsi->tx_rings[i]->q_index) {
5970 					tx_ring = vsi->tx_rings[i];
5971 					break;
5972 				}
5973 			}
5974 		}
5975 	}
5976 
5977 	/* Reset recovery level if enough time has elapsed after last timeout.
5978 	 * Also ensure no new reset action happens before next timeout period.
5979 	 */
5980 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
5981 		pf->tx_timeout_recovery_level = 1;
5982 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
5983 				       netdev->watchdog_timeo)))
5984 		return;
5985 
5986 	if (tx_ring) {
5987 		head = tx_ring->next_to_clean;
5988 		/* Read interrupt register */
5989 		if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
5990 			val = rd32(&pf->hw,
5991 				   GLINT_DYN_CTL(tx_ring->q_vector->v_idx +
5992 						tx_ring->vsi->base_vector - 1));
5993 
5994 		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",
5995 			    vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
5996 			    head, tx_ring->next_to_use,
5997 			    readl(tx_ring->tail), val);
5998 	}
5999 
6000 	pf->tx_timeout_last_recovery = jiffies;
6001 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
6002 		    pf->tx_timeout_recovery_level, hung_queue);
6003 
6004 	switch (pf->tx_timeout_recovery_level) {
6005 	case 1:
6006 		set_bit(__ICE_PFR_REQ, pf->state);
6007 		break;
6008 	case 2:
6009 		set_bit(__ICE_CORER_REQ, pf->state);
6010 		break;
6011 	case 3:
6012 		set_bit(__ICE_GLOBR_REQ, pf->state);
6013 		break;
6014 	default:
6015 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
6016 		set_bit(__ICE_DOWN, pf->state);
6017 		set_bit(__ICE_NEEDS_RESTART, vsi->state);
6018 		set_bit(__ICE_SERVICE_DIS, pf->state);
6019 		break;
6020 	}
6021 
6022 	ice_service_task_schedule(pf);
6023 	pf->tx_timeout_recovery_level++;
6024 }
6025 
6026 /**
6027  * ice_open - Called when a network interface becomes active
6028  * @netdev: network interface device structure
6029  *
6030  * The open entry point is called when a network interface is made
6031  * active by the system (IFF_UP).  At this point all resources needed
6032  * for transmit and receive operations are allocated, the interrupt
6033  * handler is registered with the OS, the netdev watchdog is enabled,
6034  * and the stack is notified that the interface is ready.
6035  *
6036  * Returns 0 on success, negative value on failure
6037  */
6038 static int ice_open(struct net_device *netdev)
6039 {
6040 	struct ice_netdev_priv *np = netdev_priv(netdev);
6041 	struct ice_vsi *vsi = np->vsi;
6042 	int err;
6043 
6044 	if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
6045 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6046 		return -EIO;
6047 	}
6048 
6049 	netif_carrier_off(netdev);
6050 
6051 	err = ice_vsi_open(vsi);
6052 
6053 	if (err)
6054 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6055 			   vsi->vsi_num, vsi->vsw->sw_id);
6056 	return err;
6057 }
6058 
6059 /**
6060  * ice_stop - Disables a network interface
6061  * @netdev: network interface device structure
6062  *
6063  * The stop entry point is called when an interface is de-activated by the OS,
6064  * and the netdevice enters the DOWN state.  The hardware is still under the
6065  * driver's control, but the netdev interface is disabled.
6066  *
6067  * Returns success only - not allowed to fail
6068  */
6069 static int ice_stop(struct net_device *netdev)
6070 {
6071 	struct ice_netdev_priv *np = netdev_priv(netdev);
6072 	struct ice_vsi *vsi = np->vsi;
6073 
6074 	ice_vsi_close(vsi);
6075 
6076 	return 0;
6077 }
6078 
6079 /**
6080  * ice_features_check - Validate encapsulated packet conforms to limits
6081  * @skb: skb buffer
6082  * @netdev: This port's netdev
6083  * @features: Offload features that the stack believes apply
6084  */
6085 static netdev_features_t
6086 ice_features_check(struct sk_buff *skb,
6087 		   struct net_device __always_unused *netdev,
6088 		   netdev_features_t features)
6089 {
6090 	size_t len;
6091 
6092 	/* No point in doing any of this if neither checksum nor GSO are
6093 	 * being requested for this frame.  We can rule out both by just
6094 	 * checking for CHECKSUM_PARTIAL
6095 	 */
6096 	if (skb->ip_summed != CHECKSUM_PARTIAL)
6097 		return features;
6098 
6099 	/* We cannot support GSO if the MSS is going to be less than
6100 	 * 64 bytes.  If it is then we need to drop support for GSO.
6101 	 */
6102 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6103 		features &= ~NETIF_F_GSO_MASK;
6104 
6105 	len = skb_network_header(skb) - skb->data;
6106 	if (len & ~(ICE_TXD_MACLEN_MAX))
6107 		goto out_rm_features;
6108 
6109 	len = skb_transport_header(skb) - skb_network_header(skb);
6110 	if (len & ~(ICE_TXD_IPLEN_MAX))
6111 		goto out_rm_features;
6112 
6113 	if (skb->encapsulation) {
6114 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
6115 		if (len & ~(ICE_TXD_L4LEN_MAX))
6116 			goto out_rm_features;
6117 
6118 		len = skb_inner_transport_header(skb) -
6119 		      skb_inner_network_header(skb);
6120 		if (len & ~(ICE_TXD_IPLEN_MAX))
6121 			goto out_rm_features;
6122 	}
6123 
6124 	return features;
6125 out_rm_features:
6126 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6127 }
6128 
6129 static const struct net_device_ops ice_netdev_ops = {
6130 	.ndo_open = ice_open,
6131 	.ndo_stop = ice_stop,
6132 	.ndo_start_xmit = ice_start_xmit,
6133 	.ndo_features_check = ice_features_check,
6134 	.ndo_set_rx_mode = ice_set_rx_mode,
6135 	.ndo_set_mac_address = ice_set_mac_address,
6136 	.ndo_validate_addr = eth_validate_addr,
6137 	.ndo_change_mtu = ice_change_mtu,
6138 	.ndo_get_stats64 = ice_get_stats64,
6139 #ifdef CONFIG_NET_POLL_CONTROLLER
6140 	.ndo_poll_controller = ice_netpoll,
6141 #endif /* CONFIG_NET_POLL_CONTROLLER */
6142 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6143 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6144 	.ndo_set_features = ice_set_features,
6145 	.ndo_bridge_getlink = ice_bridge_getlink,
6146 	.ndo_bridge_setlink = ice_bridge_setlink,
6147 	.ndo_fdb_add = ice_fdb_add,
6148 	.ndo_fdb_del = ice_fdb_del,
6149 	.ndo_tx_timeout = ice_tx_timeout,
6150 };
6151