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