1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define DRV_VERSION "0.7.1-k"
11 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
12 const char ice_drv_ver[] = DRV_VERSION;
13 static const char ice_driver_string[] = DRV_SUMMARY;
14 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
16 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
17 MODULE_DESCRIPTION(DRV_SUMMARY);
18 MODULE_LICENSE("GPL");
19 MODULE_VERSION(DRV_VERSION);
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)");
26 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
27 #endif /* !CONFIG_DYNAMIC_DEBUG */
29 static struct workqueue_struct *ice_wq;
30 static const struct net_device_ops ice_netdev_ops;
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);
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
44 * void * is being used to keep the functionality generic. This lets us use this
45 * function on any array of pointers.
47 static int ice_get_free_slot(void *array, int size, int curr)
49 int **tmp_array = (int **)array;
52 if (curr < (size - 1) && !tmp_array[curr + 1]) {
57 while ((i < size) && (tmp_array[i]))
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
74 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
76 int start = res->search_hint;
79 id |= ICE_RES_VALID_BIT;
82 /* skip already allocated entries */
83 if (res->list[end++] & ICE_RES_VALID_BIT) {
85 if ((start + needed) > res->num_entries)
89 if (end == (start + needed)) {
92 /* there was enough, so assign it to the requestor */
96 if (end == res->num_entries)
99 res->search_hint = end;
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
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.
120 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
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);
134 /* search based on search_hint */
135 ret = ice_search_res(res, needed, id);
138 /* previous search failed. Reset search hint and try again */
139 res->search_hint = 0;
140 ret = ice_search_res(res, needed, id);
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
153 static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
158 if (!res || index >= res->num_entries)
161 id |= ICE_RES_VALID_BIT;
162 for (i = index; i < res->num_entries && res->list[i] == id; i++) {
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.
176 * Adds mac address filter entry to the temp list
178 * Returns 0 on success or ENOMEM on failure.
180 static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
183 struct ice_fltr_list_entry *tmp;
184 struct ice_pf *pf = vsi->back;
186 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
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);
197 INIT_LIST_HEAD(&tmp->list_entry);
198 list_add(&tmp->list_entry, add_list);
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
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.
213 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
215 struct ice_netdev_priv *np = netdev_priv(netdev);
216 struct ice_vsi *vsi = np->vsi;
218 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
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
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.
234 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
236 struct ice_netdev_priv *np = netdev_priv(netdev);
237 struct ice_vsi *vsi = np->vsi;
239 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
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
250 * Helper function to free filter lists previously created using
251 * ice_add_mac_to_list
253 static void ice_free_fltr_list(struct device *dev, struct list_head *h)
255 struct ice_fltr_list_entry *e, *tmp;
257 list_for_each_entry_safe(e, tmp, h, list_entry) {
258 list_del(&e->list_entry);
264 * ice_vsi_fltr_changed - check if filter state changed
265 * @vsi: VSI to be checked
267 * returns true if filter state has changed, false otherwise.
269 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
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);
277 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
278 * @vsi: ptr to the VSI
280 * Push any outstanding VSI filter changes through the AdminQ.
282 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
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;
296 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
297 usleep_range(1000, 2000);
299 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
300 vsi->current_netdev_flags = vsi->netdev->flags;
302 INIT_LIST_HEAD(&vsi->tmp_sync_list);
303 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
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);
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);
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);
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) {
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);
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.
341 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
342 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
344 promisc_forced_on = true;
346 "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
353 /* check for changes in promiscuous modes */
354 if (changed_flags & IFF_ALLMULTI)
355 netdev_warn(netdev, "Unsupported configuration\n");
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,
365 netdev_err(netdev, "Error setting default VSI %i tx rule\n",
367 vsi->current_netdev_flags &= ~IFF_PROMISC;
371 /* Apply RX filter rule to get traffic from wire */
372 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
375 netdev_err(netdev, "Error setting default VSI %i rx rule\n",
377 vsi->current_netdev_flags &= ~IFF_PROMISC;
382 /* Clear TX filter rule to stop traffic from VMs */
383 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
386 netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
388 vsi->current_netdev_flags |= IFF_PROMISC;
392 /* Clear filter RX to remove traffic from wire */
393 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
396 netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
398 vsi->current_netdev_flags |= IFF_PROMISC;
407 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
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);
414 clear_bit(__ICE_CFG_BUSY, vsi->state);
419 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
420 * @pf: board private structure
422 static void ice_sync_fltr_subtask(struct ice_pf *pf)
426 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
429 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
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);
441 * ice_is_reset_recovery_pending - schedule a reset
442 * @state: pf state field
444 static bool ice_is_reset_recovery_pending(unsigned long int *state)
446 return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
450 * ice_prepare_for_reset - prep for the core to reset
451 * @pf: board private structure
453 * Inform or close all dependent features in prep for reset.
456 ice_prepare_for_reset(struct ice_pf *pf)
458 struct ice_hw *hw = &pf->hw;
461 ice_for_each_vsi(pf, v)
463 ice_remove_vsi_fltr(hw, pf->vsi[v]->vsi_num);
465 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
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);
471 ice_for_each_vsi(pf, v)
473 pf->vsi[v]->vsi_num = 0;
475 ice_shutdown_all_ctrlq(hw);
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.
484 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
486 struct device *dev = &pf->pdev->dev;
487 struct ice_hw *hw = &pf->hw;
489 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
490 WARN_ON(in_interrupt());
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.
496 if (reset_type == ICE_RESET_PFR) {
497 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
498 ice_prepare_for_reset(pf);
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);
509 if (reset_type == ICE_RESET_PFR) {
512 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
517 * ice_reset_subtask - Set up for resetting the device and driver
518 * @pf: board private structure
520 static void ice_reset_subtask(struct ice_pf *pf)
522 enum ice_reset_req reset_type;
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.
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);
537 /* make sure we are ready to rebuild */
538 if (ice_check_reset(&pf->hw)) {
539 set_bit(__ICE_RESET_FAILED, pf->state);
541 /* done with reset. start rebuild */
542 pf->hw.reset_ongoing = false;
545 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
549 /* No pending resets to finish processing. Check for new resets */
550 if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
551 reset_type = ICE_RESET_GLOBR;
552 else if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
553 reset_type = ICE_RESET_CORER;
554 else if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
555 reset_type = ICE_RESET_PFR;
559 /* reset if not already down or resetting */
560 if (!test_bit(__ICE_DOWN, pf->state) &&
561 !test_bit(__ICE_CFG_BUSY, pf->state)) {
562 ice_do_reset(pf, reset_type);
570 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
571 * @pf: board private structure
573 static void ice_watchdog_subtask(struct ice_pf *pf)
577 /* if interface is down do nothing */
578 if (test_bit(__ICE_DOWN, pf->state) ||
579 test_bit(__ICE_CFG_BUSY, pf->state))
582 /* make sure we don't do these things too often */
583 if (time_before(jiffies,
584 pf->serv_tmr_prev + pf->serv_tmr_period))
587 pf->serv_tmr_prev = jiffies;
589 /* Update the stats for active netdevs so the network stack
590 * can look at updated numbers whenever it cares to
592 ice_update_pf_stats(pf);
593 for (i = 0; i < pf->num_alloc_vsi; i++)
594 if (pf->vsi[i] && pf->vsi[i]->netdev)
595 ice_update_vsi_stats(pf->vsi[i]);
599 * ice_print_link_msg - print link up or down message
600 * @vsi: the VSI whose link status is being queried
601 * @isup: boolean for if the link is now up or down
603 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
608 if (vsi->current_isup == isup)
611 vsi->current_isup = isup;
614 netdev_info(vsi->netdev, "NIC Link is Down\n");
618 switch (vsi->port_info->phy.link_info.link_speed) {
619 case ICE_AQ_LINK_SPEED_40GB:
622 case ICE_AQ_LINK_SPEED_25GB:
625 case ICE_AQ_LINK_SPEED_20GB:
628 case ICE_AQ_LINK_SPEED_10GB:
631 case ICE_AQ_LINK_SPEED_5GB:
634 case ICE_AQ_LINK_SPEED_2500MB:
637 case ICE_AQ_LINK_SPEED_1000MB:
640 case ICE_AQ_LINK_SPEED_100MB:
648 switch (vsi->port_info->fc.current_mode) {
652 case ICE_FC_TX_PAUSE:
655 case ICE_FC_RX_PAUSE:
666 netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
671 * ice_init_link_events - enable/initialize link events
672 * @pi: pointer to the port_info instance
674 * Returns -EIO on failure, 0 on success
676 static int ice_init_link_events(struct ice_port_info *pi)
680 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
681 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
683 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
684 dev_dbg(ice_hw_to_dev(pi->hw),
685 "Failed to set link event mask for port %d\n",
690 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
691 dev_dbg(ice_hw_to_dev(pi->hw),
692 "Failed to enable link events for port %d\n",
701 * ice_vsi_link_event - update the vsi's netdev
702 * @vsi: the vsi on which the link event occurred
703 * @link_up: whether or not the vsi needs to be set up or down
705 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
707 if (!vsi || test_bit(__ICE_DOWN, vsi->state))
710 if (vsi->type == ICE_VSI_PF) {
712 dev_dbg(&vsi->back->pdev->dev,
713 "vsi->netdev is not initialized!\n");
717 netif_carrier_on(vsi->netdev);
718 netif_tx_wake_all_queues(vsi->netdev);
720 netif_carrier_off(vsi->netdev);
721 netif_tx_stop_all_queues(vsi->netdev);
727 * ice_link_event - process the link event
728 * @pf: pf that the link event is associated with
729 * @pi: port_info for the port that the link event is associated with
731 * Returns -EIO if ice_get_link_status() fails
732 * Returns 0 on success
735 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
737 u8 new_link_speed, old_link_speed;
738 struct ice_phy_info *phy_info;
739 bool new_link_same_as_old;
740 bool new_link, old_link;
745 phy_info->link_info_old = phy_info->link_info;
746 /* Force ice_get_link_status() to update link info */
747 phy_info->get_link_info = true;
749 old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
750 old_link_speed = phy_info->link_info_old.link_speed;
753 if (ice_get_link_status(pi, &new_link)) {
754 dev_dbg(&pf->pdev->dev,
755 "Could not get link status for port %d\n", lport);
759 new_link_speed = phy_info->link_info.link_speed;
761 new_link_same_as_old = (new_link == old_link &&
762 new_link_speed == old_link_speed);
764 ice_for_each_vsi(pf, v) {
765 struct ice_vsi *vsi = pf->vsi[v];
767 if (!vsi || !vsi->port_info)
770 if (new_link_same_as_old &&
771 (test_bit(__ICE_DOWN, vsi->state) ||
772 new_link == netif_carrier_ok(vsi->netdev)))
775 if (vsi->port_info->lport == lport) {
776 ice_print_link_msg(vsi, new_link);
777 ice_vsi_link_event(vsi, new_link);
785 * ice_handle_link_event - handle link event via ARQ
786 * @pf: pf that the link event is associated with
788 * Return -EINVAL if port_info is null
789 * Return status on succes
791 static int ice_handle_link_event(struct ice_pf *pf)
793 struct ice_port_info *port_info;
796 port_info = pf->hw.port_info;
800 status = ice_link_event(pf, port_info);
802 dev_dbg(&pf->pdev->dev,
803 "Could not process link event, error %d\n", status);
809 * __ice_clean_ctrlq - helper function to clean controlq rings
810 * @pf: ptr to struct ice_pf
811 * @q_type: specific Control queue type
813 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
815 struct ice_rq_event_info event;
816 struct ice_hw *hw = &pf->hw;
817 struct ice_ctl_q_info *cq;
822 /* Do not clean control queue if/when PF reset fails */
823 if (test_bit(__ICE_RESET_FAILED, pf->state))
827 case ICE_CTL_Q_ADMIN:
832 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
837 /* check for error indications - PF_xx_AxQLEN register layout for
838 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
840 val = rd32(hw, cq->rq.len);
841 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
842 PF_FW_ARQLEN_ARQCRIT_M)) {
844 if (val & PF_FW_ARQLEN_ARQVFE_M)
845 dev_dbg(&pf->pdev->dev,
846 "%s Receive Queue VF Error detected\n", qtype);
847 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
848 dev_dbg(&pf->pdev->dev,
849 "%s Receive Queue Overflow Error detected\n",
852 if (val & PF_FW_ARQLEN_ARQCRIT_M)
853 dev_dbg(&pf->pdev->dev,
854 "%s Receive Queue Critical Error detected\n",
856 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
857 PF_FW_ARQLEN_ARQCRIT_M);
859 wr32(hw, cq->rq.len, val);
862 val = rd32(hw, cq->sq.len);
863 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
864 PF_FW_ATQLEN_ATQCRIT_M)) {
866 if (val & PF_FW_ATQLEN_ATQVFE_M)
867 dev_dbg(&pf->pdev->dev,
868 "%s Send Queue VF Error detected\n", qtype);
869 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
870 dev_dbg(&pf->pdev->dev,
871 "%s Send Queue Overflow Error detected\n",
874 if (val & PF_FW_ATQLEN_ATQCRIT_M)
875 dev_dbg(&pf->pdev->dev,
876 "%s Send Queue Critical Error detected\n",
878 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
879 PF_FW_ATQLEN_ATQCRIT_M);
881 wr32(hw, cq->sq.len, val);
884 event.buf_len = cq->rq_buf_size;
885 event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
894 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
895 if (ret == ICE_ERR_AQ_NO_WORK)
898 dev_err(&pf->pdev->dev,
899 "%s Receive Queue event error %d\n", qtype,
904 opcode = le16_to_cpu(event.desc.opcode);
907 case ice_aqc_opc_get_link_status:
908 if (ice_handle_link_event(pf))
909 dev_err(&pf->pdev->dev,
910 "Could not handle link event\n");
913 dev_dbg(&pf->pdev->dev,
914 "%s Receive Queue unknown event 0x%04x ignored\n",
918 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
920 devm_kfree(&pf->pdev->dev, event.msg_buf);
922 return pending && (i == ICE_DFLT_IRQ_WORK);
926 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
927 * @hw: pointer to hardware info
928 * @cq: control queue information
930 * returns true if there are pending messages in a queue, false if there aren't
932 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
936 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
937 return cq->rq.next_to_clean != ntu;
941 * ice_clean_adminq_subtask - clean the AdminQ rings
942 * @pf: board private structure
944 static void ice_clean_adminq_subtask(struct ice_pf *pf)
946 struct ice_hw *hw = &pf->hw;
948 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
951 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
954 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
956 /* There might be a situation where new messages arrive to a control
957 * queue between processing the last message and clearing the
958 * EVENT_PENDING bit. So before exiting, check queue head again (using
959 * ice_ctrlq_pending) and process new messages if any.
961 if (ice_ctrlq_pending(hw, &hw->adminq))
962 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
968 * ice_service_task_schedule - schedule the service task to wake up
969 * @pf: board private structure
971 * If not already scheduled, this puts the task into the work queue.
973 static void ice_service_task_schedule(struct ice_pf *pf)
975 if (!test_bit(__ICE_DOWN, pf->state) &&
976 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state))
977 queue_work(ice_wq, &pf->serv_task);
981 * ice_service_task_complete - finish up the service task
982 * @pf: board private structure
984 static void ice_service_task_complete(struct ice_pf *pf)
986 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
988 /* force memory (pf->state) to sync before next service task */
989 smp_mb__before_atomic();
990 clear_bit(__ICE_SERVICE_SCHED, pf->state);
994 * ice_service_timer - timer callback to schedule service task
995 * @t: pointer to timer_list
997 static void ice_service_timer(struct timer_list *t)
999 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1001 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1002 ice_service_task_schedule(pf);
1006 * ice_service_task - manage and run subtasks
1007 * @work: pointer to work_struct contained by the PF struct
1009 static void ice_service_task(struct work_struct *work)
1011 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1012 unsigned long start_time = jiffies;
1016 /* process reset requests first */
1017 ice_reset_subtask(pf);
1019 /* bail if a reset/recovery cycle is pending */
1020 if (ice_is_reset_recovery_pending(pf->state) ||
1021 test_bit(__ICE_SUSPENDED, pf->state)) {
1022 ice_service_task_complete(pf);
1026 ice_sync_fltr_subtask(pf);
1027 ice_watchdog_subtask(pf);
1028 ice_clean_adminq_subtask(pf);
1030 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1031 ice_service_task_complete(pf);
1033 /* If the tasks have taken longer than one service timer period
1034 * or there is more work to be done, reset the service timer to
1035 * schedule the service task now.
1037 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1038 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1039 mod_timer(&pf->serv_tmr, jiffies);
1043 * ice_set_ctrlq_len - helper function to set controlq length
1044 * @hw: pointer to the hw instance
1046 static void ice_set_ctrlq_len(struct ice_hw *hw)
1048 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1049 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1050 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1051 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1055 * ice_irq_affinity_notify - Callback for affinity changes
1056 * @notify: context as to what irq was changed
1057 * @mask: the new affinity mask
1059 * This is a callback function used by the irq_set_affinity_notifier function
1060 * so that we may register to receive changes to the irq affinity masks.
1062 static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1063 const cpumask_t *mask)
1065 struct ice_q_vector *q_vector =
1066 container_of(notify, struct ice_q_vector, affinity_notify);
1068 cpumask_copy(&q_vector->affinity_mask, mask);
1072 * ice_irq_affinity_release - Callback for affinity notifier release
1073 * @ref: internal core kernel usage
1075 * This is a callback function used by the irq_set_affinity_notifier function
1076 * to inform the current notification subscriber that they will no longer
1077 * receive notifications.
1079 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1082 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
1083 * @vsi: the VSI being un-configured
1085 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
1087 struct ice_pf *pf = vsi->back;
1088 struct ice_hw *hw = &pf->hw;
1089 int base = vsi->base_vector;
1093 /* disable interrupt causation from each queue */
1094 if (vsi->tx_rings) {
1095 ice_for_each_txq(vsi, i) {
1096 if (vsi->tx_rings[i]) {
1099 reg = vsi->tx_rings[i]->reg_idx;
1100 val = rd32(hw, QINT_TQCTL(reg));
1101 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1102 wr32(hw, QINT_TQCTL(reg), val);
1107 if (vsi->rx_rings) {
1108 ice_for_each_rxq(vsi, i) {
1109 if (vsi->rx_rings[i]) {
1112 reg = vsi->rx_rings[i]->reg_idx;
1113 val = rd32(hw, QINT_RQCTL(reg));
1114 val &= ~QINT_RQCTL_CAUSE_ENA_M;
1115 wr32(hw, QINT_RQCTL(reg), val);
1120 /* disable each interrupt */
1121 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1122 for (i = vsi->base_vector;
1123 i < (vsi->num_q_vectors + vsi->base_vector); i++)
1124 wr32(hw, GLINT_DYN_CTL(i), 0);
1127 for (i = 0; i < vsi->num_q_vectors; i++)
1128 synchronize_irq(pf->msix_entries[i + base].vector);
1133 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1134 * @vsi: the VSI being configured
1136 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1138 struct ice_pf *pf = vsi->back;
1139 struct ice_hw *hw = &pf->hw;
1141 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1144 for (i = 0; i < vsi->num_q_vectors; i++)
1145 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1153 * ice_vsi_delete - delete a VSI from the switch
1154 * @vsi: pointer to VSI being removed
1156 static void ice_vsi_delete(struct ice_vsi *vsi)
1158 struct ice_pf *pf = vsi->back;
1159 struct ice_vsi_ctx ctxt;
1160 enum ice_status status;
1162 ctxt.vsi_num = vsi->vsi_num;
1164 memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
1166 status = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
1168 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
1173 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1174 * @vsi: the VSI being configured
1175 * @basename: name for the vector
1177 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1179 int q_vectors = vsi->num_q_vectors;
1180 struct ice_pf *pf = vsi->back;
1181 int base = vsi->base_vector;
1187 for (vector = 0; vector < q_vectors; vector++) {
1188 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1190 irq_num = pf->msix_entries[base + vector].vector;
1192 if (q_vector->tx.ring && q_vector->rx.ring) {
1193 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1194 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1196 } else if (q_vector->rx.ring) {
1197 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1198 "%s-%s-%d", basename, "rx", rx_int_idx++);
1199 } else if (q_vector->tx.ring) {
1200 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1201 "%s-%s-%d", basename, "tx", tx_int_idx++);
1203 /* skip this unused q_vector */
1206 err = devm_request_irq(&pf->pdev->dev,
1207 pf->msix_entries[base + vector].vector,
1208 vsi->irq_handler, 0, q_vector->name,
1211 netdev_err(vsi->netdev,
1212 "MSIX request_irq failed, error: %d\n", err);
1216 /* register for affinity change notifications */
1217 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1218 q_vector->affinity_notify.release = ice_irq_affinity_release;
1219 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1221 /* assign the mask for this irq */
1222 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1225 vsi->irqs_ready = true;
1231 irq_num = pf->msix_entries[base + vector].vector,
1232 irq_set_affinity_notifier(irq_num, NULL);
1233 irq_set_affinity_hint(irq_num, NULL);
1234 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1240 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
1241 * @vsi: the VSI being configured
1243 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
1245 struct ice_hw_common_caps *cap;
1246 struct ice_pf *pf = vsi->back;
1248 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1253 cap = &pf->hw.func_caps.common_cap;
1254 switch (vsi->type) {
1256 /* PF VSI will inherit RSS instance of PF */
1257 vsi->rss_table_size = cap->rss_table_size;
1258 vsi->rss_size = min_t(int, num_online_cpus(),
1259 BIT(cap->rss_table_entry_width));
1260 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
1263 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1269 * ice_vsi_setup_q_map - Setup a VSI queue map
1270 * @vsi: the VSI being configured
1271 * @ctxt: VSI context structure
1273 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1275 u16 offset = 0, qmap = 0, numq_tc;
1276 u16 pow = 0, max_rss = 0, qcount;
1277 u16 qcount_tx = vsi->alloc_txq;
1278 u16 qcount_rx = vsi->alloc_rxq;
1279 bool ena_tc0 = false;
1282 /* at least TC0 should be enabled by default */
1283 if (vsi->tc_cfg.numtc) {
1284 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
1291 vsi->tc_cfg.numtc++;
1292 vsi->tc_cfg.ena_tc |= 1;
1295 numq_tc = qcount_rx / vsi->tc_cfg.numtc;
1297 /* TC mapping is a function of the number of Rx queues assigned to the
1298 * VSI for each traffic class and the offset of these queues.
1299 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1300 * queues allocated to TC0. No:of queues is a power-of-2.
1302 * If TC is not enabled, the queue offset is set to 0, and allocate one
1303 * queue, this way, traffic for the given TC will be sent to the default
1306 * Setup number and offset of Rx queues for all TCs for the VSI
1309 /* qcount will change if RSS is enabled */
1310 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
1311 if (vsi->type == ICE_VSI_PF)
1312 max_rss = ICE_MAX_LG_RSS_QS;
1314 max_rss = ICE_MAX_SMALL_RSS_QS;
1316 qcount = min_t(int, numq_tc, max_rss);
1317 qcount = min_t(int, qcount, vsi->rss_size);
1322 /* find the (rounded up) power-of-2 of qcount */
1323 pow = order_base_2(qcount);
1325 for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1326 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1327 /* TC is not enabled */
1328 vsi->tc_cfg.tc_info[i].qoffset = 0;
1329 vsi->tc_cfg.tc_info[i].qcount = 1;
1330 ctxt->info.tc_mapping[i] = 0;
1335 vsi->tc_cfg.tc_info[i].qoffset = offset;
1336 vsi->tc_cfg.tc_info[i].qcount = qcount;
1338 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1339 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1340 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1341 ICE_AQ_VSI_TC_Q_NUM_M);
1343 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1346 vsi->num_txq = qcount_tx;
1347 vsi->num_rxq = offset;
1349 /* Rx queue mapping */
1350 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1351 /* q_mapping buffer holds the info for the first queue allocated for
1352 * this VSI in the PF space and also the number of queues associated
1355 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1356 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1360 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
1361 * @ctxt: the VSI context being set
1363 * This initializes a default VSI context for all sections except the Queues.
1365 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
1369 memset(&ctxt->info, 0, sizeof(ctxt->info));
1370 /* VSI's should be allocated from shared pool */
1371 ctxt->alloc_from_pool = true;
1372 /* Src pruning enabled by default */
1373 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
1374 /* Traffic from VSI can be sent to LAN */
1375 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1377 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
1378 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
1379 * packets untagged/tagged.
1381 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
1382 ICE_AQ_VSI_VLAN_MODE_M) >>
1383 ICE_AQ_VSI_VLAN_MODE_S);
1385 /* Have 1:1 UP mapping for both ingress/egress tables */
1386 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1387 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1388 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1389 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1390 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1391 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1392 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1393 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1394 ctxt->info.ingress_table = cpu_to_le32(table);
1395 ctxt->info.egress_table = cpu_to_le32(table);
1396 /* Have 1:1 UP mapping for outer to inner UP table */
1397 ctxt->info.outer_up_table = cpu_to_le32(table);
1398 /* No Outer tag support outer_tag_flags remains to zero */
1402 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1403 * @ctxt: the VSI context being set
1404 * @vsi: the VSI being configured
1406 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1408 u8 lut_type, hash_type;
1410 switch (vsi->type) {
1412 /* PF VSI will inherit RSS instance of PF */
1413 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1414 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1417 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1422 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1423 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1424 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1425 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1429 * ice_vsi_add - Create a new VSI or fetch preallocated VSI
1430 * @vsi: the VSI being configured
1432 * This initializes a VSI context depending on the VSI type to be added and
1433 * passes it down to the add_vsi aq command to create a new VSI.
1435 static int ice_vsi_add(struct ice_vsi *vsi)
1437 struct ice_vsi_ctx ctxt = { 0 };
1438 struct ice_pf *pf = vsi->back;
1439 struct ice_hw *hw = &pf->hw;
1442 switch (vsi->type) {
1444 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
1450 ice_set_dflt_vsi_ctx(&ctxt);
1451 /* if the switch is in VEB mode, allow VSI loopback */
1452 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1453 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1455 /* Set LUT type and HASH type if RSS is enabled */
1456 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1457 ice_set_rss_vsi_ctx(&ctxt, vsi);
1459 ctxt.info.sw_id = vsi->port_info->sw_id;
1460 ice_vsi_setup_q_map(vsi, &ctxt);
1462 ret = ice_aq_add_vsi(hw, &ctxt, NULL);
1464 dev_err(&vsi->back->pdev->dev,
1465 "Add VSI AQ call failed, err %d\n", ret);
1468 vsi->info = ctxt.info;
1469 vsi->vsi_num = ctxt.vsi_num;
1475 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1476 * @vsi: the VSI being cleaned up
1478 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1480 struct ice_pf *pf = vsi->back;
1481 u16 vector = vsi->base_vector;
1482 struct ice_hw *hw = &pf->hw;
1487 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1488 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1490 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1491 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1492 for (q = 0; q < q_vector->num_ring_tx; q++) {
1493 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1497 for (q = 0; q < q_vector->num_ring_rx; q++) {
1498 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1507 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1508 * @vsi: the VSI having rings deallocated
1510 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1514 if (vsi->tx_rings) {
1515 for (i = 0; i < vsi->alloc_txq; i++) {
1516 if (vsi->tx_rings[i]) {
1517 kfree_rcu(vsi->tx_rings[i], rcu);
1518 vsi->tx_rings[i] = NULL;
1522 if (vsi->rx_rings) {
1523 for (i = 0; i < vsi->alloc_rxq; i++) {
1524 if (vsi->rx_rings[i]) {
1525 kfree_rcu(vsi->rx_rings[i], rcu);
1526 vsi->rx_rings[i] = NULL;
1533 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1534 * @vsi: VSI which is having rings allocated
1536 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1538 struct ice_pf *pf = vsi->back;
1541 /* Allocate tx_rings */
1542 for (i = 0; i < vsi->alloc_txq; i++) {
1543 struct ice_ring *ring;
1545 /* allocate with kzalloc(), free with kfree_rcu() */
1546 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1552 ring->reg_idx = vsi->txq_map[i];
1553 ring->ring_active = false;
1555 ring->netdev = vsi->netdev;
1556 ring->dev = &pf->pdev->dev;
1557 ring->count = vsi->num_desc;
1559 vsi->tx_rings[i] = ring;
1562 /* Allocate rx_rings */
1563 for (i = 0; i < vsi->alloc_rxq; i++) {
1564 struct ice_ring *ring;
1566 /* allocate with kzalloc(), free with kfree_rcu() */
1567 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1572 ring->reg_idx = vsi->rxq_map[i];
1573 ring->ring_active = false;
1575 ring->netdev = vsi->netdev;
1576 ring->dev = &pf->pdev->dev;
1577 ring->count = vsi->num_desc;
1578 vsi->rx_rings[i] = ring;
1584 ice_vsi_clear_rings(vsi);
1589 * ice_vsi_free_irq - Free the irq association with the OS
1590 * @vsi: the VSI being configured
1592 static void ice_vsi_free_irq(struct ice_vsi *vsi)
1594 struct ice_pf *pf = vsi->back;
1595 int base = vsi->base_vector;
1597 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1600 if (!vsi->q_vectors || !vsi->irqs_ready)
1603 vsi->irqs_ready = false;
1604 for (i = 0; i < vsi->num_q_vectors; i++) {
1605 u16 vector = i + base;
1608 irq_num = pf->msix_entries[vector].vector;
1610 /* free only the irqs that were actually requested */
1611 if (!vsi->q_vectors[i] ||
1612 !(vsi->q_vectors[i]->num_ring_tx ||
1613 vsi->q_vectors[i]->num_ring_rx))
1616 /* clear the affinity notifier in the IRQ descriptor */
1617 irq_set_affinity_notifier(irq_num, NULL);
1619 /* clear the affinity_mask in the IRQ descriptor */
1620 irq_set_affinity_hint(irq_num, NULL);
1621 synchronize_irq(irq_num);
1622 devm_free_irq(&pf->pdev->dev, irq_num,
1625 ice_vsi_release_msix(vsi);
1630 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1631 * @vsi: the VSI being configured
1633 static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1635 struct ice_pf *pf = vsi->back;
1636 u16 vector = vsi->base_vector;
1637 struct ice_hw *hw = &pf->hw;
1638 u32 txq = 0, rxq = 0;
1642 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1643 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1645 itr_gran = hw->itr_gran_200;
1647 if (q_vector->num_ring_rx) {
1649 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1651 q_vector->rx.latency_range = ICE_LOW_LATENCY;
1654 if (q_vector->num_ring_tx) {
1656 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1658 q_vector->tx.latency_range = ICE_LOW_LATENCY;
1660 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1661 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1663 /* Both Transmit Queue Interrupt Cause Control register
1664 * and Receive Queue Interrupt Cause control register
1665 * expects MSIX_INDX field to be the vector index
1666 * within the function space and not the absolute
1667 * vector index across PF or across device.
1668 * For SR-IOV VF VSIs queue vector index always starts
1669 * with 1 since first vector index(0) is used for OICR
1670 * in VF space. Since VMDq and other PF VSIs are withtin
1671 * the PF function space, use the vector index thats
1672 * tracked for this PF.
1674 for (q = 0; q < q_vector->num_ring_tx; q++) {
1678 val = QINT_TQCTL_CAUSE_ENA_M |
1679 (itr << QINT_TQCTL_ITR_INDX_S) |
1680 (vector << QINT_TQCTL_MSIX_INDX_S);
1681 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1685 for (q = 0; q < q_vector->num_ring_rx; q++) {
1689 val = QINT_RQCTL_CAUSE_ENA_M |
1690 (itr << QINT_RQCTL_ITR_INDX_S) |
1691 (vector << QINT_RQCTL_MSIX_INDX_S);
1692 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1701 * ice_ena_misc_vector - enable the non-queue interrupts
1702 * @pf: board private structure
1704 static void ice_ena_misc_vector(struct ice_pf *pf)
1706 struct ice_hw *hw = &pf->hw;
1709 /* clear things first */
1710 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1711 rd32(hw, PFINT_OICR); /* read to clear */
1713 val = (PFINT_OICR_ECC_ERR_M |
1714 PFINT_OICR_MAL_DETECT_M |
1716 PFINT_OICR_PCI_EXCEPTION_M |
1717 PFINT_OICR_HMC_ERR_M |
1718 PFINT_OICR_PE_CRITERR_M);
1720 wr32(hw, PFINT_OICR_ENA, val);
1722 /* SW_ITR_IDX = 0, but don't change INTENA */
1723 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1724 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1728 * ice_misc_intr - misc interrupt handler
1729 * @irq: interrupt number
1730 * @data: pointer to a q_vector
1732 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1734 struct ice_pf *pf = (struct ice_pf *)data;
1735 struct ice_hw *hw = &pf->hw;
1736 irqreturn_t ret = IRQ_NONE;
1739 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1741 oicr = rd32(hw, PFINT_OICR);
1742 ena_mask = rd32(hw, PFINT_OICR_ENA);
1744 if (oicr & PFINT_OICR_GRST_M) {
1746 /* we have a reset warning */
1747 ena_mask &= ~PFINT_OICR_GRST_M;
1748 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1749 GLGEN_RSTAT_RESET_TYPE_S;
1751 if (reset == ICE_RESET_CORER)
1753 else if (reset == ICE_RESET_GLOBR)
1758 /* If a reset cycle isn't already in progress, we set a bit in
1759 * pf->state so that the service task can start a reset/rebuild.
1760 * We also make note of which reset happened so that peer
1761 * devices/drivers can be informed.
1763 if (!test_and_set_bit(__ICE_RESET_RECOVERY_PENDING,
1765 if (reset == ICE_RESET_CORER)
1766 set_bit(__ICE_CORER_RECV, pf->state);
1767 else if (reset == ICE_RESET_GLOBR)
1768 set_bit(__ICE_GLOBR_RECV, pf->state);
1770 set_bit(__ICE_EMPR_RECV, pf->state);
1772 /* There are couple of different bits at play here.
1773 * hw->reset_ongoing indicates whether the hardware is
1774 * in reset. This is set to true when a reset interrupt
1775 * is received and set back to false after the driver
1776 * has determined that the hardware is out of reset.
1778 * __ICE_RESET_RECOVERY_PENDING in pf->state indicates
1779 * that a post reset rebuild is required before the
1780 * driver is operational again. This is set above.
1782 * As this is the start of the reset/rebuild cycle, set
1783 * both to indicate that.
1785 hw->reset_ongoing = true;
1789 if (oicr & PFINT_OICR_HMC_ERR_M) {
1790 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1791 dev_dbg(&pf->pdev->dev,
1792 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1793 rd32(hw, PFHMC_ERRORINFO),
1794 rd32(hw, PFHMC_ERRORDATA));
1797 /* Report and mask off any remaining unexpected interrupts */
1800 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1802 /* If a critical error is pending there is no choice but to
1805 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1806 PFINT_OICR_PCI_EXCEPTION_M |
1807 PFINT_OICR_ECC_ERR_M)) {
1808 set_bit(__ICE_PFR_REQ, pf->state);
1809 ice_service_task_schedule(pf);
1815 /* re-enable interrupt causes that are not handled during this pass */
1816 wr32(hw, PFINT_OICR_ENA, ena_mask);
1817 if (!test_bit(__ICE_DOWN, pf->state)) {
1818 ice_service_task_schedule(pf);
1819 ice_irq_dynamic_ena(hw, NULL, NULL);
1826 * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1827 * @vsi: the VSI being configured
1829 * This function maps descriptor rings to the queue-specific vectors allotted
1830 * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1831 * and Rx rings to the vector as "efficiently" as possible.
1833 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1835 int q_vectors = vsi->num_q_vectors;
1836 int tx_rings_rem, rx_rings_rem;
1839 /* initially assigning remaining rings count to VSIs num queue value */
1840 tx_rings_rem = vsi->num_txq;
1841 rx_rings_rem = vsi->num_rxq;
1843 for (v_id = 0; v_id < q_vectors; v_id++) {
1844 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1845 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1847 /* Tx rings mapping to vector */
1848 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1849 q_vector->num_ring_tx = tx_rings_per_v;
1850 q_vector->tx.ring = NULL;
1851 q_base = vsi->num_txq - tx_rings_rem;
1853 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1854 struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1856 tx_ring->q_vector = q_vector;
1857 tx_ring->next = q_vector->tx.ring;
1858 q_vector->tx.ring = tx_ring;
1860 tx_rings_rem -= tx_rings_per_v;
1862 /* Rx rings mapping to vector */
1863 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1864 q_vector->num_ring_rx = rx_rings_per_v;
1865 q_vector->rx.ring = NULL;
1866 q_base = vsi->num_rxq - rx_rings_rem;
1868 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1869 struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1871 rx_ring->q_vector = q_vector;
1872 rx_ring->next = q_vector->rx.ring;
1873 q_vector->rx.ring = rx_ring;
1875 rx_rings_rem -= rx_rings_per_v;
1880 * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
1881 * @vsi: the VSI being configured
1883 * Return 0 on success and a negative value on error
1885 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
1887 struct ice_pf *pf = vsi->back;
1889 switch (vsi->type) {
1891 vsi->alloc_txq = pf->num_lan_tx;
1892 vsi->alloc_rxq = pf->num_lan_rx;
1893 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
1894 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
1897 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1904 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
1906 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
1908 * On error: returns error code (negative)
1909 * On success: returns 0
1911 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
1913 struct ice_pf *pf = vsi->back;
1915 /* allocate memory for both Tx and Rx ring pointers */
1916 vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
1917 sizeof(struct ice_ring *), GFP_KERNEL);
1921 vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
1922 sizeof(struct ice_ring *), GFP_KERNEL);
1926 if (alloc_qvectors) {
1927 /* allocate memory for q_vector pointers */
1928 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
1930 sizeof(struct ice_q_vector *),
1932 if (!vsi->q_vectors)
1939 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
1941 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
1947 * ice_msix_clean_rings - MSIX mode Interrupt Handler
1948 * @irq: interrupt number
1949 * @data: pointer to a q_vector
1951 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
1953 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
1955 if (!q_vector->tx.ring && !q_vector->rx.ring)
1958 napi_schedule(&q_vector->napi);
1964 * ice_vsi_alloc - Allocates the next available struct vsi in the PF
1965 * @pf: board private structure
1966 * @type: type of VSI
1968 * returns a pointer to a VSI on success, NULL on failure.
1970 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
1972 struct ice_vsi *vsi = NULL;
1974 /* Need to protect the allocation of the VSIs at the PF level */
1975 mutex_lock(&pf->sw_mutex);
1977 /* If we have already allocated our maximum number of VSIs,
1978 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
1979 * is available to be populated
1981 if (pf->next_vsi == ICE_NO_VSI) {
1982 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
1986 vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
1992 set_bit(__ICE_DOWN, vsi->state);
1993 vsi->idx = pf->next_vsi;
1994 vsi->work_lmt = ICE_DFLT_IRQ_WORK;
1996 ice_vsi_set_num_qs(vsi);
1998 switch (vsi->type) {
2000 if (ice_vsi_alloc_arrays(vsi, true))
2003 /* Setup default MSIX irq handler for VSI */
2004 vsi->irq_handler = ice_msix_clean_rings;
2007 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
2011 /* fill VSI slot in the PF struct */
2012 pf->vsi[pf->next_vsi] = vsi;
2014 /* prepare pf->next_vsi for next use */
2015 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
2020 devm_kfree(&pf->pdev->dev, vsi);
2023 mutex_unlock(&pf->sw_mutex);
2028 * ice_free_irq_msix_misc - Unroll misc vector setup
2029 * @pf: board private structure
2031 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2033 /* disable OICR interrupt */
2034 wr32(&pf->hw, PFINT_OICR_ENA, 0);
2037 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
2038 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2039 devm_free_irq(&pf->pdev->dev,
2040 pf->msix_entries[pf->oicr_idx].vector, pf);
2043 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2047 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2048 * @pf: board private structure
2050 * This sets up the handler for MSIX 0, which is used to manage the
2051 * non-queue interrupts, e.g. AdminQ and errors. This is not used
2052 * when in MSI or Legacy interrupt mode.
2054 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2056 struct ice_hw *hw = &pf->hw;
2057 int oicr_idx, err = 0;
2061 if (!pf->int_name[0])
2062 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2063 dev_driver_string(&pf->pdev->dev),
2064 dev_name(&pf->pdev->dev));
2066 /* Do not request IRQ but do enable OICR interrupt since settings are
2067 * lost during reset. Note that this function is called only during
2068 * rebuild path and not while reset is in progress.
2070 if (ice_is_reset_recovery_pending(pf->state))
2073 /* reserve one vector in irq_tracker for misc interrupts */
2074 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2078 pf->oicr_idx = oicr_idx;
2080 err = devm_request_irq(&pf->pdev->dev,
2081 pf->msix_entries[pf->oicr_idx].vector,
2082 ice_misc_intr, 0, pf->int_name, pf);
2084 dev_err(&pf->pdev->dev,
2085 "devm_request_irq for %s failed: %d\n",
2087 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2092 ice_ena_misc_vector(pf);
2094 val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2095 PFINT_OICR_CTL_CAUSE_ENA_M);
2096 wr32(hw, PFINT_OICR_CTL, val);
2098 /* This enables Admin queue Interrupt causes */
2099 val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2100 PFINT_FW_CTL_CAUSE_ENA_M);
2101 wr32(hw, PFINT_FW_CTL, val);
2103 itr_gran = hw->itr_gran_200;
2105 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2106 ITR_TO_REG(ICE_ITR_8K, itr_gran));
2109 ice_irq_dynamic_ena(hw, NULL, NULL);
2115 * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
2116 * @vsi: the VSI getting queues
2118 * Return 0 on success and a negative value on error
2120 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
2122 struct ice_pf *pf = vsi->back;
2123 int offset, ret = 0;
2125 mutex_lock(&pf->avail_q_mutex);
2126 /* look for contiguous block of queues for tx */
2127 offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
2128 0, vsi->alloc_txq, 0);
2129 if (offset < ICE_MAX_TXQS) {
2132 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
2133 for (i = 0; i < vsi->alloc_txq; i++)
2134 vsi->txq_map[i] = i + offset;
2137 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
2140 /* look for contiguous block of queues for rx */
2141 offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
2142 0, vsi->alloc_rxq, 0);
2143 if (offset < ICE_MAX_RXQS) {
2146 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
2147 for (i = 0; i < vsi->alloc_rxq; i++)
2148 vsi->rxq_map[i] = i + offset;
2151 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
2153 mutex_unlock(&pf->avail_q_mutex);
2159 * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
2160 * @vsi: the VSI getting queues
2162 * Return 0 on success and a negative value on error
2164 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
2166 struct ice_pf *pf = vsi->back;
2169 mutex_lock(&pf->avail_q_mutex);
2171 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2172 for (i = 0; i < vsi->alloc_txq; i++) {
2173 index = find_next_zero_bit(pf->avail_txqs,
2174 ICE_MAX_TXQS, index);
2175 if (index < ICE_MAX_TXQS) {
2176 set_bit(index, pf->avail_txqs);
2177 vsi->txq_map[i] = index;
2179 goto err_scatter_tx;
2184 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2185 for (i = 0; i < vsi->alloc_rxq; i++) {
2186 index = find_next_zero_bit(pf->avail_rxqs,
2187 ICE_MAX_RXQS, index);
2188 if (index < ICE_MAX_RXQS) {
2189 set_bit(index, pf->avail_rxqs);
2190 vsi->rxq_map[i] = index;
2192 goto err_scatter_rx;
2197 mutex_unlock(&pf->avail_q_mutex);
2201 /* unflag any queues we have grabbed (i is failed position) */
2202 for (index = 0; index < i; index++) {
2203 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
2204 vsi->rxq_map[index] = 0;
2208 /* i is either position of failed attempt or vsi->alloc_txq */
2209 for (index = 0; index < i; index++) {
2210 clear_bit(vsi->txq_map[index], pf->avail_txqs);
2211 vsi->txq_map[index] = 0;
2214 mutex_unlock(&pf->avail_q_mutex);
2219 * ice_vsi_get_qs - Assign queues from PF to VSI
2220 * @vsi: the VSI to assign queues to
2222 * Returns 0 on success and a negative value on error
2224 static int ice_vsi_get_qs(struct ice_vsi *vsi)
2228 vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
2229 vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
2231 /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
2232 * modes individually to scatter if assigning contiguous queues
2235 ret = ice_vsi_get_qs_contig(vsi);
2237 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
2238 vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
2239 ICE_MAX_SCATTER_TXQS);
2240 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
2241 vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
2242 ICE_MAX_SCATTER_RXQS);
2243 ret = ice_vsi_get_qs_scatter(vsi);
2250 * ice_vsi_put_qs - Release queues from VSI to PF
2251 * @vsi: the VSI thats going to release queues
2253 static void ice_vsi_put_qs(struct ice_vsi *vsi)
2255 struct ice_pf *pf = vsi->back;
2258 mutex_lock(&pf->avail_q_mutex);
2260 for (i = 0; i < vsi->alloc_txq; i++) {
2261 clear_bit(vsi->txq_map[i], pf->avail_txqs);
2262 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
2265 for (i = 0; i < vsi->alloc_rxq; i++) {
2266 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
2267 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
2270 mutex_unlock(&pf->avail_q_mutex);
2274 * ice_free_q_vector - Free memory allocated for a specific interrupt vector
2275 * @vsi: VSI having the memory freed
2276 * @v_idx: index of the vector to be freed
2278 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
2280 struct ice_q_vector *q_vector;
2281 struct ice_ring *ring;
2283 if (!vsi->q_vectors[v_idx]) {
2284 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
2288 q_vector = vsi->q_vectors[v_idx];
2290 ice_for_each_ring(ring, q_vector->tx)
2291 ring->q_vector = NULL;
2292 ice_for_each_ring(ring, q_vector->rx)
2293 ring->q_vector = NULL;
2295 /* only VSI with an associated netdev is set up with NAPI */
2297 netif_napi_del(&q_vector->napi);
2299 devm_kfree(&vsi->back->pdev->dev, q_vector);
2300 vsi->q_vectors[v_idx] = NULL;
2304 * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
2305 * @vsi: the VSI having memory freed
2307 static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
2311 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
2312 ice_free_q_vector(vsi, v_idx);
2316 * ice_cfg_netdev - Setup the netdev flags
2317 * @vsi: the VSI being configured
2319 * Returns 0 on success, negative value on failure
2321 static int ice_cfg_netdev(struct ice_vsi *vsi)
2323 netdev_features_t csumo_features;
2324 netdev_features_t vlano_features;
2325 netdev_features_t dflt_features;
2326 netdev_features_t tso_features;
2327 struct ice_netdev_priv *np;
2328 struct net_device *netdev;
2329 u8 mac_addr[ETH_ALEN];
2331 netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
2332 vsi->alloc_txq, vsi->alloc_rxq);
2336 vsi->netdev = netdev;
2337 np = netdev_priv(netdev);
2340 dflt_features = NETIF_F_SG |
2344 csumo_features = NETIF_F_RXCSUM |
2348 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2349 NETIF_F_HW_VLAN_CTAG_TX |
2350 NETIF_F_HW_VLAN_CTAG_RX;
2352 tso_features = NETIF_F_TSO;
2354 /* set features that user can change */
2355 netdev->hw_features = dflt_features | csumo_features |
2356 vlano_features | tso_features;
2358 /* enable features */
2359 netdev->features |= netdev->hw_features;
2360 /* encap and VLAN devices inherit default, csumo and tso features */
2361 netdev->hw_enc_features |= dflt_features | csumo_features |
2363 netdev->vlan_features |= dflt_features | csumo_features |
2366 if (vsi->type == ICE_VSI_PF) {
2367 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
2368 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2370 ether_addr_copy(netdev->dev_addr, mac_addr);
2371 ether_addr_copy(netdev->perm_addr, mac_addr);
2374 netdev->priv_flags |= IFF_UNICAST_FLT;
2376 /* assign netdev_ops */
2377 netdev->netdev_ops = &ice_netdev_ops;
2379 /* setup watchdog timeout value to be 5 second */
2380 netdev->watchdog_timeo = 5 * HZ;
2382 ice_set_ethtool_ops(netdev);
2384 netdev->min_mtu = ETH_MIN_MTU;
2385 netdev->max_mtu = ICE_MAX_MTU;
2391 * ice_vsi_free_arrays - clean up vsi resources
2392 * @vsi: pointer to VSI being cleared
2393 * @free_qvectors: bool to specify if q_vectors should be deallocated
2395 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
2397 struct ice_pf *pf = vsi->back;
2399 /* free the ring and vector containers */
2400 if (free_qvectors && vsi->q_vectors) {
2401 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
2402 vsi->q_vectors = NULL;
2404 if (vsi->tx_rings) {
2405 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2406 vsi->tx_rings = NULL;
2408 if (vsi->rx_rings) {
2409 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2410 vsi->rx_rings = NULL;
2415 * ice_vsi_clear - clean up and deallocate the provided vsi
2416 * @vsi: pointer to VSI being cleared
2418 * This deallocates the vsi's queue resources, removes it from the PF's
2419 * VSI array if necessary, and deallocates the VSI
2421 * Returns 0 on success, negative on failure
2423 static int ice_vsi_clear(struct ice_vsi *vsi)
2425 struct ice_pf *pf = NULL;
2435 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
2436 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
2441 mutex_lock(&pf->sw_mutex);
2442 /* updates the PF for this cleared vsi */
2444 pf->vsi[vsi->idx] = NULL;
2445 if (vsi->idx < pf->next_vsi)
2446 pf->next_vsi = vsi->idx;
2448 ice_vsi_free_arrays(vsi, true);
2449 mutex_unlock(&pf->sw_mutex);
2450 devm_kfree(&pf->pdev->dev, vsi);
2456 * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
2457 * @vsi: the VSI being configured
2458 * @v_idx: index of the vector in the vsi struct
2460 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2462 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
2464 struct ice_pf *pf = vsi->back;
2465 struct ice_q_vector *q_vector;
2467 /* allocate q_vector */
2468 q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
2472 q_vector->vsi = vsi;
2473 q_vector->v_idx = v_idx;
2474 /* only set affinity_mask if the CPU is online */
2475 if (cpu_online(v_idx))
2476 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
2479 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
2481 /* tie q_vector and vsi together */
2482 vsi->q_vectors[v_idx] = q_vector;
2488 * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
2489 * @vsi: the VSI being configured
2491 * We allocate one q_vector per queue interrupt. If allocation fails we
2494 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
2496 struct ice_pf *pf = vsi->back;
2497 int v_idx = 0, num_q_vectors;
2500 if (vsi->q_vectors[0]) {
2501 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
2506 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2507 num_q_vectors = vsi->num_q_vectors;
2513 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
2514 err = ice_vsi_alloc_q_vector(vsi, v_idx);
2523 ice_free_q_vector(vsi, v_idx);
2525 dev_err(&pf->pdev->dev,
2526 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
2527 vsi->num_q_vectors, vsi->vsi_num, err);
2528 vsi->num_q_vectors = 0;
2533 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
2534 * @vsi: ptr to the VSI
2536 * This should only be called after ice_vsi_alloc() which allocates the
2537 * corresponding SW VSI structure and initializes num_queue_pairs for the
2538 * newly allocated VSI.
2540 * Returns 0 on success or negative on failure
2542 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
2544 struct ice_pf *pf = vsi->back;
2545 int num_q_vectors = 0;
2547 if (vsi->base_vector) {
2548 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
2549 vsi->vsi_num, vsi->base_vector);
2553 if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2556 switch (vsi->type) {
2558 num_q_vectors = vsi->num_q_vectors;
2561 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2567 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
2568 num_q_vectors, vsi->idx);
2570 if (vsi->base_vector < 0) {
2571 dev_err(&pf->pdev->dev,
2572 "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
2573 num_q_vectors, vsi->vsi_num, vsi->base_vector);
2581 * ice_fill_rss_lut - Fill the RSS lookup table with default values
2582 * @lut: Lookup table
2583 * @rss_table_size: Lookup table size
2584 * @rss_size: Range of queue number for hashing
2586 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2590 for (i = 0; i < rss_table_size; i++)
2591 lut[i] = i % rss_size;
2595 * ice_vsi_cfg_rss - Configure RSS params for a VSI
2596 * @vsi: VSI to be configured
2598 static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
2600 u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
2601 struct ice_aqc_get_set_rss_keys *key;
2602 struct ice_pf *pf = vsi->back;
2603 enum ice_status status;
2607 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
2609 lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2613 if (vsi->rss_lut_user)
2614 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
2616 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
2618 status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
2619 lut, vsi->rss_table_size);
2622 dev_err(&vsi->back->pdev->dev,
2623 "set_rss_lut failed, error %d\n", status);
2625 goto ice_vsi_cfg_rss_exit;
2628 key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
2631 goto ice_vsi_cfg_rss_exit;
2634 if (vsi->rss_hkey_user)
2635 memcpy(seed, vsi->rss_hkey_user,
2636 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2638 netdev_rss_key_fill((void *)seed,
2639 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2640 memcpy(&key->standard_rss_key, seed,
2641 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2643 status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
2646 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
2651 devm_kfree(&pf->pdev->dev, key);
2652 ice_vsi_cfg_rss_exit:
2653 devm_kfree(&pf->pdev->dev, lut);
2658 * ice_vsi_reinit_setup - return resource and reallocate resource for a VSI
2659 * @vsi: pointer to the ice_vsi
2661 * This reallocates the VSIs queue resources
2663 * Returns 0 on success and negative value on failure
2665 static int ice_vsi_reinit_setup(struct ice_vsi *vsi)
2667 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2673 ice_vsi_free_q_vectors(vsi);
2674 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2675 vsi->base_vector = 0;
2676 ice_vsi_clear_rings(vsi);
2677 ice_vsi_free_arrays(vsi, false);
2678 ice_vsi_set_num_qs(vsi);
2680 /* Initialize VSI struct elements and create VSI in FW */
2681 ret = ice_vsi_add(vsi);
2685 ret = ice_vsi_alloc_arrays(vsi, false);
2689 switch (vsi->type) {
2692 ret = ice_cfg_netdev(vsi);
2696 ret = register_netdev(vsi->netdev);
2700 netif_carrier_off(vsi->netdev);
2701 netif_tx_stop_all_queues(vsi->netdev);
2704 ret = ice_vsi_alloc_q_vectors(vsi);
2708 ret = ice_vsi_setup_vector_base(vsi);
2712 ret = ice_vsi_alloc_rings(vsi);
2716 ice_vsi_map_rings_to_vectors(vsi);
2722 ice_vsi_set_tc_cfg(vsi);
2724 /* configure VSI nodes based on number of queues and TC's */
2725 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2726 max_txqs[i] = vsi->num_txq;
2728 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2729 vsi->tc_cfg.ena_tc, max_txqs);
2731 dev_info(&vsi->back->pdev->dev,
2732 "Failed VSI lan queue config\n");
2738 ice_vsi_free_q_vectors(vsi);
2741 vsi->current_netdev_flags = 0;
2742 unregister_netdev(vsi->netdev);
2743 free_netdev(vsi->netdev);
2748 set_bit(__ICE_RESET_FAILED, vsi->back->state);
2753 * ice_vsi_setup - Set up a VSI by a given type
2754 * @pf: board private structure
2756 * @pi: pointer to the port_info instance
2758 * This allocates the sw VSI structure and its queue resources.
2760 * Returns pointer to the successfully allocated and configure VSI sw struct on
2761 * success, otherwise returns NULL on failure.
2763 static struct ice_vsi *
2764 ice_vsi_setup(struct ice_pf *pf, enum ice_vsi_type type,
2765 struct ice_port_info *pi)
2767 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2768 struct device *dev = &pf->pdev->dev;
2769 struct ice_vsi_ctx ctxt = { 0 };
2770 struct ice_vsi *vsi;
2773 vsi = ice_vsi_alloc(pf, type);
2775 dev_err(dev, "could not allocate VSI\n");
2779 vsi->port_info = pi;
2780 vsi->vsw = pf->first_sw;
2782 if (ice_vsi_get_qs(vsi)) {
2783 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2788 /* set RSS capabilities */
2789 ice_vsi_set_rss_params(vsi);
2791 /* create the VSI */
2792 ret = ice_vsi_add(vsi);
2796 ctxt.vsi_num = vsi->vsi_num;
2798 switch (vsi->type) {
2800 ret = ice_cfg_netdev(vsi);
2802 goto err_cfg_netdev;
2804 ret = register_netdev(vsi->netdev);
2806 goto err_register_netdev;
2808 netif_carrier_off(vsi->netdev);
2810 /* make sure transmit queues start off as stopped */
2811 netif_tx_stop_all_queues(vsi->netdev);
2812 ret = ice_vsi_alloc_q_vectors(vsi);
2816 ret = ice_vsi_setup_vector_base(vsi);
2820 ret = ice_vsi_alloc_rings(vsi);
2824 ice_vsi_map_rings_to_vectors(vsi);
2826 /* Do not exit if configuring RSS had an issue, at least
2827 * receive traffic on first queue. Hence no need to capture
2830 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2831 ice_vsi_cfg_rss(vsi);
2834 /* if vsi type is not recognized, clean up the resources and
2840 ice_vsi_set_tc_cfg(vsi);
2842 /* configure VSI nodes based on number of queues and TC's */
2843 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2844 max_txqs[i] = vsi->num_txq;
2846 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2847 vsi->tc_cfg.ena_tc, max_txqs);
2849 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
2856 ice_vsi_free_q_vectors(vsi);
2858 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
2859 unregister_netdev(vsi->netdev);
2860 err_register_netdev:
2862 free_netdev(vsi->netdev);
2866 ret = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
2868 dev_err(&vsi->back->pdev->dev,
2869 "Free VSI AQ call failed, err %d\n", ret);
2871 ice_vsi_put_qs(vsi);
2873 pf->q_left_tx += vsi->alloc_txq;
2874 pf->q_left_rx += vsi->alloc_rxq;
2881 * ice_vsi_add_vlan - Add vsi membership for given vlan
2882 * @vsi: the vsi being configured
2883 * @vid: vlan id to be added
2885 static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
2887 struct ice_fltr_list_entry *tmp;
2888 struct ice_pf *pf = vsi->back;
2889 LIST_HEAD(tmp_add_list);
2890 enum ice_status status;
2893 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
2897 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2898 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2899 tmp->fltr_info.flag = ICE_FLTR_TX;
2900 tmp->fltr_info.src = vsi->vsi_num;
2901 tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2902 tmp->fltr_info.l_data.vlan.vlan_id = vid;
2904 INIT_LIST_HEAD(&tmp->list_entry);
2905 list_add(&tmp->list_entry, &tmp_add_list);
2907 status = ice_add_vlan(&pf->hw, &tmp_add_list);
2910 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
2914 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2919 * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
2920 * @netdev: network interface to be adjusted
2921 * @proto: unused protocol
2922 * @vid: vlan id to be added
2924 * net_device_ops implementation for adding vlan ids
2926 static int ice_vlan_rx_add_vid(struct net_device *netdev,
2927 __always_unused __be16 proto, u16 vid)
2929 struct ice_netdev_priv *np = netdev_priv(netdev);
2930 struct ice_vsi *vsi = np->vsi;
2933 if (vid >= VLAN_N_VID) {
2934 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2942 /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
2943 * needed to continue allowing all untagged packets since VLAN prune
2944 * list is applied to all packets by the switch
2946 ret = ice_vsi_add_vlan(vsi, vid);
2949 set_bit(vid, vsi->active_vlans);
2955 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
2956 * @vsi: the VSI being configured
2957 * @vid: VLAN id to be removed
2959 static void ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
2961 struct ice_fltr_list_entry *list;
2962 struct ice_pf *pf = vsi->back;
2963 LIST_HEAD(tmp_add_list);
2965 list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
2969 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2970 list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2971 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2972 list->fltr_info.l_data.vlan.vlan_id = vid;
2973 list->fltr_info.flag = ICE_FLTR_TX;
2974 list->fltr_info.src = vsi->vsi_num;
2976 INIT_LIST_HEAD(&list->list_entry);
2977 list_add(&list->list_entry, &tmp_add_list);
2979 if (ice_remove_vlan(&pf->hw, &tmp_add_list))
2980 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
2983 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2987 * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2988 * @netdev: network interface to be adjusted
2989 * @proto: unused protocol
2990 * @vid: vlan id to be removed
2992 * net_device_ops implementation for removing vlan ids
2994 static int ice_vlan_rx_kill_vid(struct net_device *netdev,
2995 __always_unused __be16 proto, u16 vid)
2997 struct ice_netdev_priv *np = netdev_priv(netdev);
2998 struct ice_vsi *vsi = np->vsi;
3003 /* return code is ignored as there is nothing a user
3004 * can do about failure to remove and a log message was
3005 * already printed from the other function
3007 ice_vsi_kill_vlan(vsi, vid);
3009 clear_bit(vid, vsi->active_vlans);
3015 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3016 * @pf: board private structure
3018 * Returns 0 on success, negative value on failure
3020 static int ice_setup_pf_sw(struct ice_pf *pf)
3022 LIST_HEAD(tmp_add_list);
3023 u8 broadcast[ETH_ALEN];
3024 struct ice_vsi *vsi;
3027 if (!ice_is_reset_recovery_pending(pf->state)) {
3028 vsi = ice_vsi_setup(pf, ICE_VSI_PF, pf->hw.port_info);
3035 status = ice_vsi_reinit_setup(vsi);
3040 /* tmp_add_list contains a list of MAC addresses for which MAC
3041 * filters need to be programmed. Add the VSI's unicast MAC to
3044 status = ice_add_mac_to_list(vsi, &tmp_add_list,
3045 vsi->port_info->mac.perm_addr);
3049 /* VSI needs to receive broadcast traffic, so add the broadcast
3050 * MAC address to the list.
3052 eth_broadcast_addr(broadcast);
3053 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
3057 /* program MAC filters for entries in tmp_add_list */
3058 status = ice_add_mac(&pf->hw, &tmp_add_list);
3060 dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
3065 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3069 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3072 ice_vsi_free_q_vectors(vsi);
3073 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3074 unregister_netdev(vsi->netdev);
3076 free_netdev(vsi->netdev);
3080 ice_vsi_delete(vsi);
3081 ice_vsi_put_qs(vsi);
3082 pf->q_left_tx += vsi->alloc_txq;
3083 pf->q_left_rx += vsi->alloc_rxq;
3090 * ice_determine_q_usage - Calculate queue distribution
3091 * @pf: board private structure
3093 * Return -ENOMEM if we don't get enough queues for all ports
3095 static void ice_determine_q_usage(struct ice_pf *pf)
3097 u16 q_left_tx, q_left_rx;
3099 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
3100 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
3102 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
3104 /* only 1 rx queue unless RSS is enabled */
3105 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3108 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
3110 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
3111 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
3115 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3116 * @pf: board private structure to initialize
3118 static void ice_deinit_pf(struct ice_pf *pf)
3120 if (pf->serv_tmr.function)
3121 del_timer_sync(&pf->serv_tmr);
3122 if (pf->serv_task.func)
3123 cancel_work_sync(&pf->serv_task);
3124 mutex_destroy(&pf->sw_mutex);
3125 mutex_destroy(&pf->avail_q_mutex);
3129 * ice_init_pf - Initialize general software structures (struct ice_pf)
3130 * @pf: board private structure to initialize
3132 static void ice_init_pf(struct ice_pf *pf)
3134 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
3135 set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3137 mutex_init(&pf->sw_mutex);
3138 mutex_init(&pf->avail_q_mutex);
3140 /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
3141 mutex_lock(&pf->avail_q_mutex);
3142 bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
3143 bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
3144 mutex_unlock(&pf->avail_q_mutex);
3146 if (pf->hw.func_caps.common_cap.rss_table_size)
3147 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3149 /* setup service timer and periodic service task */
3150 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3151 pf->serv_tmr_period = HZ;
3152 INIT_WORK(&pf->serv_task, ice_service_task);
3153 clear_bit(__ICE_SERVICE_SCHED, pf->state);
3157 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3158 * @pf: board private structure
3160 * compute the number of MSIX vectors required (v_budget) and request from
3161 * the OS. Return the number of vectors reserved or negative on failure
3163 static int ice_ena_msix_range(struct ice_pf *pf)
3165 int v_left, v_actual, v_budget = 0;
3168 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3170 /* reserve one vector for miscellaneous handler */
3175 /* reserve vectors for LAN traffic */
3176 pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
3177 v_budget += pf->num_lan_msix;
3179 pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
3180 sizeof(struct msix_entry), GFP_KERNEL);
3182 if (!pf->msix_entries) {
3187 for (i = 0; i < v_budget; i++)
3188 pf->msix_entries[i].entry = i;
3190 /* actually reserve the vectors */
3191 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3192 ICE_MIN_MSIX, v_budget);
3195 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
3200 if (v_actual < v_budget) {
3201 dev_warn(&pf->pdev->dev,
3202 "not enough vectors. requested = %d, obtained = %d\n",
3203 v_budget, v_actual);
3204 if (v_actual >= (pf->num_lan_msix + 1)) {
3205 pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
3206 } else if (v_actual >= 2) {
3207 pf->num_lan_msix = 1;
3208 pf->num_avail_msix = v_actual - 2;
3210 pci_disable_msix(pf->pdev);
3219 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3223 pf->num_lan_msix = 0;
3224 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3229 * ice_dis_msix - Disable MSI-X interrupt setup in OS
3230 * @pf: board private structure
3232 static void ice_dis_msix(struct ice_pf *pf)
3234 pci_disable_msix(pf->pdev);
3235 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3236 pf->msix_entries = NULL;
3237 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3241 * ice_init_interrupt_scheme - Determine proper interrupt scheme
3242 * @pf: board private structure to initialize
3244 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3249 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3250 vectors = ice_ena_msix_range(pf);
3257 /* set up vector assignment tracking */
3258 size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
3260 pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
3261 if (!pf->irq_tracker) {
3266 pf->irq_tracker->num_entries = vectors;
3272 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3273 * @pf: board private structure
3275 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3277 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3280 if (pf->irq_tracker) {
3281 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
3282 pf->irq_tracker = NULL;
3287 * ice_probe - Device initialization routine
3288 * @pdev: PCI device information struct
3289 * @ent: entry in ice_pci_tbl
3291 * Returns 0 on success, negative on failure
3293 static int ice_probe(struct pci_dev *pdev,
3294 const struct pci_device_id __always_unused *ent)
3300 /* this driver uses devres, see Documentation/driver-model/devres.txt */
3301 err = pcim_enable_device(pdev);
3305 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3307 dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
3311 pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
3315 /* set up for high or low dma */
3316 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3318 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3320 dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3324 pci_enable_pcie_error_reporting(pdev);
3325 pci_set_master(pdev);
3328 pci_set_drvdata(pdev, pf);
3329 set_bit(__ICE_DOWN, pf->state);
3332 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3334 hw->vendor_id = pdev->vendor;
3335 hw->device_id = pdev->device;
3336 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3337 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3338 hw->subsystem_device_id = pdev->subsystem_device;
3339 hw->bus.device = PCI_SLOT(pdev->devfn);
3340 hw->bus.func = PCI_FUNC(pdev->devfn);
3341 ice_set_ctrlq_len(hw);
3343 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3345 #ifndef CONFIG_DYNAMIC_DEBUG
3347 hw->debug_mask = debug;
3350 err = ice_init_hw(hw);
3352 dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
3354 goto err_exit_unroll;
3357 dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
3358 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
3359 hw->api_maj_ver, hw->api_min_ver);
3363 ice_determine_q_usage(pf);
3365 pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
3366 hw->func_caps.guaranteed_num_vsi);
3367 if (!pf->num_alloc_vsi) {
3369 goto err_init_pf_unroll;
3372 pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
3373 sizeof(struct ice_vsi *), GFP_KERNEL);
3376 goto err_init_pf_unroll;
3379 err = ice_init_interrupt_scheme(pf);
3382 "ice_init_interrupt_scheme failed: %d\n", err);
3384 goto err_init_interrupt_unroll;
3387 /* In case of MSIX we are going to setup the misc vector right here
3388 * to handle admin queue events etc. In case of legacy and MSI
3389 * the misc functionality and queue processing is combined in
3390 * the same vector and that gets setup at open.
3392 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3393 err = ice_req_irq_msix_misc(pf);
3396 "setup of misc vector failed: %d\n", err);
3397 goto err_init_interrupt_unroll;
3401 /* create switch struct for the switch element created by FW on boot */
3402 pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
3404 if (!pf->first_sw) {
3406 goto err_msix_misc_unroll;
3409 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3410 pf->first_sw->pf = pf;
3412 /* record the sw_id available for later use */
3413 pf->first_sw->sw_id = hw->port_info->sw_id;
3415 err = ice_setup_pf_sw(pf);
3418 "probe failed due to setup pf switch:%d\n", err);
3419 goto err_alloc_sw_unroll;
3422 /* Driver is mostly up */
3423 clear_bit(__ICE_DOWN, pf->state);
3425 /* since everything is good, start the service timer */
3426 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3428 err = ice_init_link_events(pf->hw.port_info);
3430 dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
3431 goto err_alloc_sw_unroll;
3436 err_alloc_sw_unroll:
3437 set_bit(__ICE_DOWN, pf->state);
3438 devm_kfree(&pf->pdev->dev, pf->first_sw);
3439 err_msix_misc_unroll:
3440 ice_free_irq_msix_misc(pf);
3441 err_init_interrupt_unroll:
3442 ice_clear_interrupt_scheme(pf);
3443 devm_kfree(&pdev->dev, pf->vsi);
3448 pci_disable_pcie_error_reporting(pdev);
3453 * ice_remove - Device removal routine
3454 * @pdev: PCI device information struct
3456 static void ice_remove(struct pci_dev *pdev)
3458 struct ice_pf *pf = pci_get_drvdata(pdev);
3465 set_bit(__ICE_DOWN, pf->state);
3467 for (i = 0; i < pf->num_alloc_vsi; i++) {
3471 err = ice_vsi_release(pf->vsi[i]);
3473 dev_dbg(&pf->pdev->dev, "Failed to release VSI index %d (err %d)\n",
3477 ice_free_irq_msix_misc(pf);
3478 ice_clear_interrupt_scheme(pf);
3480 ice_deinit_hw(&pf->hw);
3481 pci_disable_pcie_error_reporting(pdev);
3484 /* ice_pci_tbl - PCI Device ID Table
3486 * Wildcard entries (PCI_ANY_ID) should come last
3487 * Last entry must be all 0s
3489 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3490 * Class, Class Mask, private data (not used) }
3492 static const struct pci_device_id ice_pci_tbl[] = {
3493 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
3494 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
3495 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
3496 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
3497 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
3498 /* required last entry */
3501 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3503 static struct pci_driver ice_driver = {
3504 .name = KBUILD_MODNAME,
3505 .id_table = ice_pci_tbl,
3507 .remove = ice_remove,
3511 * ice_module_init - Driver registration routine
3513 * ice_module_init is the first routine called when the driver is
3514 * loaded. All it does is register with the PCI subsystem.
3516 static int __init ice_module_init(void)
3520 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3521 pr_info("%s\n", ice_copyright);
3523 ice_wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, KBUILD_MODNAME);
3525 pr_err("Failed to create workqueue\n");
3529 status = pci_register_driver(&ice_driver);
3531 pr_err("failed to register pci driver, err %d\n", status);
3532 destroy_workqueue(ice_wq);
3537 module_init(ice_module_init);
3540 * ice_module_exit - Driver exit cleanup routine
3542 * ice_module_exit is called just before the driver is removed
3545 static void __exit ice_module_exit(void)
3547 pci_unregister_driver(&ice_driver);
3548 destroy_workqueue(ice_wq);
3549 pr_info("module unloaded\n");
3551 module_exit(ice_module_exit);
3554 * ice_set_mac_address - NDO callback to set mac address
3555 * @netdev: network interface device structure
3556 * @pi: pointer to an address structure
3558 * Returns 0 on success, negative on failure
3560 static int ice_set_mac_address(struct net_device *netdev, void *pi)
3562 struct ice_netdev_priv *np = netdev_priv(netdev);
3563 struct ice_vsi *vsi = np->vsi;
3564 struct ice_pf *pf = vsi->back;
3565 struct ice_hw *hw = &pf->hw;
3566 struct sockaddr *addr = pi;
3567 enum ice_status status;
3568 LIST_HEAD(a_mac_list);
3569 LIST_HEAD(r_mac_list);
3574 mac = (u8 *)addr->sa_data;
3576 if (!is_valid_ether_addr(mac))
3577 return -EADDRNOTAVAIL;
3579 if (ether_addr_equal(netdev->dev_addr, mac)) {
3580 netdev_warn(netdev, "already using mac %pM\n", mac);
3584 if (test_bit(__ICE_DOWN, pf->state) ||
3585 ice_is_reset_recovery_pending(pf->state)) {
3586 netdev_err(netdev, "can't set mac %pM. device not ready\n",
3591 /* When we change the mac address we also have to change the mac address
3592 * based filter rules that were created previously for the old mac
3593 * address. So first, we remove the old filter rule using ice_remove_mac
3594 * and then create a new filter rule using ice_add_mac. Note that for
3595 * both these operations, we first need to form a "list" of mac
3596 * addresses (even though in this case, we have only 1 mac address to be
3597 * added/removed) and this done using ice_add_mac_to_list. Depending on
3598 * the ensuing operation this "list" of mac addresses is either to be
3599 * added or removed from the filter.
3601 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
3603 err = -EADDRNOTAVAIL;
3607 status = ice_remove_mac(hw, &r_mac_list);
3609 err = -EADDRNOTAVAIL;
3613 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
3615 err = -EADDRNOTAVAIL;
3619 status = ice_add_mac(hw, &a_mac_list);
3621 err = -EADDRNOTAVAIL;
3626 /* free list entries */
3627 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
3628 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
3631 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
3636 /* change the netdev's mac address */
3637 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3638 netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
3641 /* write new mac address to the firmware */
3642 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3643 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3645 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
3652 * ice_set_rx_mode - NDO callback to set the netdev filters
3653 * @netdev: network interface device structure
3655 static void ice_set_rx_mode(struct net_device *netdev)
3657 struct ice_netdev_priv *np = netdev_priv(netdev);
3658 struct ice_vsi *vsi = np->vsi;
3663 /* Set the flags to synchronize filters
3664 * ndo_set_rx_mode may be triggered even without a change in netdev
3667 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3668 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3669 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3671 /* schedule our worker thread which will take care of
3672 * applying the new filter changes
3674 ice_service_task_schedule(vsi->back);
3678 * ice_fdb_add - add an entry to the hardware database
3679 * @ndm: the input from the stack
3680 * @tb: pointer to array of nladdr (unused)
3681 * @dev: the net device pointer
3682 * @addr: the MAC address entry being added
3684 * @flags: instructions from stack about fdb operation
3686 static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3687 struct net_device *dev, const unsigned char *addr,
3693 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3696 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3697 netdev_err(dev, "FDB only supports static addresses\n");
3701 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3702 err = dev_uc_add_excl(dev, addr);
3703 else if (is_multicast_ether_addr(addr))
3704 err = dev_mc_add_excl(dev, addr);
3708 /* Only return duplicate errors if NLM_F_EXCL is set */
3709 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3716 * ice_fdb_del - delete an entry from the hardware database
3717 * @ndm: the input from the stack
3718 * @tb: pointer to array of nladdr (unused)
3719 * @dev: the net device pointer
3720 * @addr: the MAC address entry being added
3723 static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
3724 struct net_device *dev, const unsigned char *addr,
3725 __always_unused u16 vid)
3729 if (ndm->ndm_state & NUD_PERMANENT) {
3730 netdev_err(dev, "FDB only supports static addresses\n");
3734 if (is_unicast_ether_addr(addr))
3735 err = dev_uc_del(dev, addr);
3736 else if (is_multicast_ether_addr(addr))
3737 err = dev_mc_del(dev, addr);
3745 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
3746 * @vsi: the vsi being changed
3748 static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
3750 struct device *dev = &vsi->back->pdev->dev;
3751 struct ice_hw *hw = &vsi->back->hw;
3752 struct ice_vsi_ctx ctxt = { 0 };
3753 enum ice_status status;
3755 /* Here we are configuring the VSI to let the driver add VLAN tags by
3756 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
3757 * insertion happens in the Tx hot path, in ice_tx_map.
3759 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
3761 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3762 ctxt.vsi_num = vsi->vsi_num;
3764 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3766 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
3767 status, hw->adminq.sq_last_status);
3771 vsi->info.vlan_flags = ctxt.info.vlan_flags;
3776 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
3777 * @vsi: the vsi being changed
3778 * @ena: boolean value indicating if this is a enable or disable request
3780 static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
3782 struct device *dev = &vsi->back->pdev->dev;
3783 struct ice_hw *hw = &vsi->back->hw;
3784 struct ice_vsi_ctx ctxt = { 0 };
3785 enum ice_status status;
3787 /* Here we are configuring what the VSI should do with the VLAN tag in
3788 * the Rx packet. We can either leave the tag in the packet or put it in
3789 * the Rx descriptor.
3792 /* Strip VLAN tag from Rx packet and put it in the desc */
3793 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
3795 /* Disable stripping. Leave tag in packet */
3796 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3799 /* Allow all packets untagged/tagged */
3800 ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
3802 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3803 ctxt.vsi_num = vsi->vsi_num;
3805 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3807 dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
3808 ena, status, hw->adminq.sq_last_status);
3812 vsi->info.vlan_flags = ctxt.info.vlan_flags;
3817 * ice_set_features - set the netdev feature flags
3818 * @netdev: ptr to the netdev being adjusted
3819 * @features: the feature set that the stack is suggesting
3821 static int ice_set_features(struct net_device *netdev,
3822 netdev_features_t features)
3824 struct ice_netdev_priv *np = netdev_priv(netdev);
3825 struct ice_vsi *vsi = np->vsi;
3828 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3829 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3830 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3831 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3832 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3833 ret = ice_vsi_manage_vlan_stripping(vsi, false);
3834 else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3835 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3836 ret = ice_vsi_manage_vlan_insertion(vsi);
3837 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3838 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3839 ret = ice_vsi_manage_vlan_insertion(vsi);
3845 * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
3846 * @vsi: VSI to setup vlan properties for
3848 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3852 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3853 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3854 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3855 ret = ice_vsi_manage_vlan_insertion(vsi);
3861 * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
3862 * @vsi: the VSI being brought back up
3864 static int ice_restore_vlan(struct ice_vsi *vsi)
3872 err = ice_vsi_vlan_setup(vsi);
3876 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
3877 err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
3886 * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
3887 * @ring: The Tx ring to configure
3888 * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
3889 * @pf_q: queue index in the PF space
3891 * Configure the Tx descriptor ring in TLAN context.
3894 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
3896 struct ice_vsi *vsi = ring->vsi;
3897 struct ice_hw *hw = &vsi->back->hw;
3899 tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
3901 tlan_ctx->port_num = vsi->port_info->lport;
3903 /* Transmit Queue Length */
3904 tlan_ctx->qlen = ring->count;
3907 tlan_ctx->pf_num = hw->pf_id;
3909 /* queue belongs to a specific VSI type
3910 * VF / VM index should be programmed per vmvf_type setting:
3911 * for vmvf_type = VF, it is VF number between 0-256
3912 * for vmvf_type = VM, it is VM number between 0-767
3913 * for PF or EMP this field should be set to zero
3915 switch (vsi->type) {
3917 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
3923 /* make sure the context is associated with the right VSI */
3924 tlan_ctx->src_vsi = vsi->vsi_num;
3926 tlan_ctx->tso_ena = ICE_TX_LEGACY;
3927 tlan_ctx->tso_qnum = pf_q;
3929 /* Legacy or Advanced Host Interface:
3930 * 0: Advanced Host Interface
3931 * 1: Legacy Host Interface
3933 tlan_ctx->legacy_int = ICE_TX_LEGACY;
3937 * ice_vsi_cfg_txqs - Configure the VSI for Tx
3938 * @vsi: the VSI being configured
3940 * Return 0 on success and a negative value on error
3941 * Configure the Tx VSI for operation.
3943 static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
3945 struct ice_aqc_add_tx_qgrp *qg_buf;
3946 struct ice_aqc_add_txqs_perq *txq;
3947 struct ice_pf *pf = vsi->back;
3948 enum ice_status status;
3949 u16 buf_len, i, pf_q;
3950 int err = 0, tc = 0;
3953 buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
3954 qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
3958 if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
3962 qg_buf->num_txqs = 1;
3965 /* set up and configure the tx queues */
3966 ice_for_each_txq(vsi, i) {
3967 struct ice_tlan_ctx tlan_ctx = { 0 };
3969 pf_q = vsi->txq_map[i];
3970 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
3971 /* copy context contents into the qg_buf */
3972 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
3973 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
3976 /* init queue specific tail reg. It is referred as transmit
3977 * comm scheduler queue doorbell.
3979 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
3980 status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
3981 num_q_grps, qg_buf, buf_len, NULL);
3983 dev_err(&vsi->back->pdev->dev,
3984 "Failed to set LAN Tx queue context, error: %d\n",
3990 /* Add Tx Queue TEID into the VSI tx ring from the response
3991 * This will complete configuring and enabling the queue.
3993 txq = &qg_buf->txqs[0];
3994 if (pf_q == le16_to_cpu(txq->txq_id))
3995 vsi->tx_rings[i]->txq_teid =
3996 le32_to_cpu(txq->q_teid);
3999 devm_kfree(&pf->pdev->dev, qg_buf);
4004 * ice_setup_rx_ctx - Configure a receive ring context
4005 * @ring: The Rx ring to configure
4007 * Configure the Rx descriptor ring in RLAN context.
4009 static int ice_setup_rx_ctx(struct ice_ring *ring)
4011 struct ice_vsi *vsi = ring->vsi;
4012 struct ice_hw *hw = &vsi->back->hw;
4013 u32 rxdid = ICE_RXDID_FLEX_NIC;
4014 struct ice_rlan_ctx rlan_ctx;
4019 /* what is RX queue number in global space of 2K rx queues */
4020 pf_q = vsi->rxq_map[ring->q_index];
4022 /* clear the context structure first */
4023 memset(&rlan_ctx, 0, sizeof(rlan_ctx));
4025 rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
4027 rlan_ctx.qlen = ring->count;
4029 /* Receive Packet Data Buffer Size.
4030 * The Packet Data Buffer Size is defined in 128 byte units.
4032 rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
4034 /* use 32 byte descriptors */
4037 /* Strip the Ethernet CRC bytes before the packet is posted to host
4040 rlan_ctx.crcstrip = 1;
4042 /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
4043 rlan_ctx.l2tsel = 1;
4045 rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
4046 rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
4047 rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
4049 /* This controls whether VLAN is stripped from inner headers
4050 * The VLAN in the inner L2 header is stripped to the receive
4051 * descriptor if enabled by this flag.
4053 rlan_ctx.showiv = 0;
4055 /* Max packet size for this queue - must not be set to a larger value
4058 rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
4059 ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
4061 /* Rx queue threshold in units of 64 */
4062 rlan_ctx.lrxqthresh = 1;
4064 /* Enable Flexible Descriptors in the queue context which
4065 * allows this driver to select a specific receive descriptor format
4067 regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
4068 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
4069 QRXFLXP_CNTXT_RXDID_IDX_M;
4071 /* increasing context priority to pick up profile id;
4072 * default is 0x01; setting to 0x03 to ensure profile
4073 * is programming if prev context is of same priority
4075 regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
4076 QRXFLXP_CNTXT_RXDID_PRIO_M;
4078 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
4080 /* Absolute queue number out of 2K needs to be passed */
4081 err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
4083 dev_err(&vsi->back->pdev->dev,
4084 "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
4089 /* init queue specific tail register */
4090 ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
4091 writel(0, ring->tail);
4092 ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
4098 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
4099 * @vsi: the VSI being configured
4101 * Return 0 on success and a negative value on error
4102 * Configure the Rx VSI for operation.
4104 static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
4109 if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
4110 vsi->max_frame = vsi->netdev->mtu +
4111 ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4113 vsi->max_frame = ICE_RXBUF_2048;
4115 vsi->rx_buf_len = ICE_RXBUF_2048;
4116 /* set up individual rings */
4117 for (i = 0; i < vsi->num_rxq && !err; i++)
4118 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
4121 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
4128 * ice_vsi_cfg - Setup the VSI
4129 * @vsi: the VSI being configured
4131 * Return 0 on success and negative value on error
4133 static int ice_vsi_cfg(struct ice_vsi *vsi)
4138 ice_set_rx_mode(vsi->netdev);
4139 err = ice_restore_vlan(vsi);
4144 err = ice_vsi_cfg_txqs(vsi);
4146 err = ice_vsi_cfg_rxqs(vsi);
4152 * ice_vsi_stop_tx_rings - Disable Tx rings
4153 * @vsi: the VSI being configured
4155 static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
4157 struct ice_pf *pf = vsi->back;
4158 struct ice_hw *hw = &pf->hw;
4159 enum ice_status status;
4164 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
4167 q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
4172 q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
4176 goto err_alloc_q_ids;
4179 /* set up the tx queue list to be disabled */
4180 ice_for_each_txq(vsi, i) {
4183 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
4188 q_ids[i] = vsi->txq_map[i];
4189 q_teids[i] = vsi->tx_rings[i]->txq_teid;
4191 /* clear cause_ena bit for disabled queues */
4192 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4193 val &= ~QINT_TQCTL_CAUSE_ENA_M;
4194 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4196 /* software is expected to wait for 100 ns */
4199 /* trigger a software interrupt for the vector associated to
4200 * the queue to schedule napi handler
4202 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
4203 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
4204 GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
4206 status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
4208 /* if the disable queue command was exercised during an active reset
4209 * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
4210 * the reset operation disables queues at the hardware level anyway.
4212 if (status == ICE_ERR_RESET_ONGOING) {
4213 dev_dbg(&pf->pdev->dev,
4214 "Reset in progress. LAN Tx queues already disabled\n");
4215 } else if (status) {
4216 dev_err(&pf->pdev->dev,
4217 "Failed to disable LAN Tx queues, error: %d\n",
4223 devm_kfree(&pf->pdev->dev, q_ids);
4226 devm_kfree(&pf->pdev->dev, q_teids);
4232 * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4233 * @pf: the PF being configured
4234 * @pf_q: the PF queue
4235 * @ena: enable or disable state of the queue
4237 * This routine will wait for the given Rx queue of the PF to reach the
4238 * enabled or disabled state.
4239 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4240 * multiple retries; else will return 0 in case of success.
4242 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
4246 for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
4247 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
4249 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4252 usleep_range(10, 20);
4254 if (i >= ICE_Q_WAIT_RETRY_LIMIT)
4261 * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
4262 * @vsi: the VSI being configured
4263 * @ena: start or stop the rx rings
4265 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
4267 struct ice_pf *pf = vsi->back;
4268 struct ice_hw *hw = &pf->hw;
4271 for (i = 0; i < vsi->num_rxq; i++) {
4272 int pf_q = vsi->rxq_map[i];
4275 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
4276 rx_reg = rd32(hw, QRX_CTRL(pf_q));
4277 if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
4278 ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
4280 usleep_range(1000, 2000);
4283 /* Skip if the queue is already in the requested state */
4284 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4287 /* turn on/off the queue */
4289 rx_reg |= QRX_CTRL_QENA_REQ_M;
4291 rx_reg &= ~QRX_CTRL_QENA_REQ_M;
4292 wr32(hw, QRX_CTRL(pf_q), rx_reg);
4294 /* wait for the change to finish */
4295 ret = ice_pf_rxq_wait(pf, pf_q, ena);
4297 dev_err(&pf->pdev->dev,
4298 "VSI idx %d Rx ring %d %sable timeout\n",
4299 vsi->idx, pf_q, (ena ? "en" : "dis"));
4308 * ice_vsi_start_rx_rings - start VSI's rx rings
4309 * @vsi: the VSI whose rings are to be started
4311 * Returns 0 on success and a negative value on error
4313 static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
4315 return ice_vsi_ctrl_rx_rings(vsi, true);
4319 * ice_vsi_stop_rx_rings - stop VSI's rx rings
4322 * Returns 0 on success and a negative value on error
4324 static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
4326 return ice_vsi_ctrl_rx_rings(vsi, false);
4330 * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
4332 * Returns 0 on success and a negative value on error
4334 static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
4338 err_tx = ice_vsi_stop_tx_rings(vsi);
4340 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
4342 err_rx = ice_vsi_stop_rx_rings(vsi);
4344 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
4346 if (err_tx || err_rx)
4353 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4354 * @vsi: the VSI being configured
4356 static void ice_napi_enable_all(struct ice_vsi *vsi)
4363 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4364 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
4366 if (q_vector->rx.ring || q_vector->tx.ring)
4367 napi_enable(&q_vector->napi);
4372 * ice_up_complete - Finish the last steps of bringing up a connection
4373 * @vsi: The VSI being configured
4375 * Return 0 on success and negative value on error
4377 static int ice_up_complete(struct ice_vsi *vsi)
4379 struct ice_pf *pf = vsi->back;
4382 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4383 ice_vsi_cfg_msix(vsi);
4387 /* Enable only Rx rings, Tx rings were enabled by the FW when the
4388 * Tx queue group list was configured and the context bits were
4389 * programmed using ice_vsi_cfg_txqs
4391 err = ice_vsi_start_rx_rings(vsi);
4395 clear_bit(__ICE_DOWN, vsi->state);
4396 ice_napi_enable_all(vsi);
4397 ice_vsi_ena_irq(vsi);
4399 if (vsi->port_info &&
4400 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
4402 ice_print_link_msg(vsi, true);
4403 netif_tx_start_all_queues(vsi->netdev);
4404 netif_carrier_on(vsi->netdev);
4407 /* clear this now, and the first stats read will be used as baseline */
4408 vsi->stat_offsets_loaded = false;
4410 ice_service_task_schedule(pf);
4416 * ice_up - Bring the connection back up after being down
4417 * @vsi: VSI being configured
4419 int ice_up(struct ice_vsi *vsi)
4423 err = ice_vsi_cfg(vsi);
4425 err = ice_up_complete(vsi);
4431 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
4432 * @ring: Tx or Rx ring to read stats from
4433 * @pkts: packets stats counter
4434 * @bytes: bytes stats counter
4436 * This function fetches stats from the ring considering the atomic operations
4437 * that needs to be performed to read u64 values in 32 bit machine.
4439 static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
4449 start = u64_stats_fetch_begin_irq(&ring->syncp);
4450 *pkts = ring->stats.pkts;
4451 *bytes = ring->stats.bytes;
4452 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4456 * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4457 * @hw: ptr to the hardware info
4458 * @hireg: high 32 bit HW register to read from
4459 * @loreg: low 32 bit HW register to read from
4460 * @prev_stat_loaded: bool to specify if previous stats are loaded
4461 * @prev_stat: ptr to previous loaded stat value
4462 * @cur_stat: ptr to current stat value
4464 static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
4465 bool prev_stat_loaded, u64 *prev_stat,
4470 new_data = rd32(hw, loreg);
4471 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
4473 /* device stats are not reset at PFR, they likely will not be zeroed
4474 * when the driver starts. So save the first values read and use them as
4475 * offsets to be subtracted from the raw values in order to report stats
4476 * that count from zero.
4478 if (!prev_stat_loaded)
4479 *prev_stat = new_data;
4480 if (likely(new_data >= *prev_stat))
4481 *cur_stat = new_data - *prev_stat;
4483 /* to manage the potential roll-over */
4484 *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
4485 *cur_stat &= 0xFFFFFFFFFFULL;
4489 * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4490 * @hw: ptr to the hardware info
4491 * @reg: HW register to read from
4492 * @prev_stat_loaded: bool to specify if previous stats are loaded
4493 * @prev_stat: ptr to previous loaded stat value
4494 * @cur_stat: ptr to current stat value
4496 static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4497 u64 *prev_stat, u64 *cur_stat)
4501 new_data = rd32(hw, reg);
4503 /* device stats are not reset at PFR, they likely will not be zeroed
4504 * when the driver starts. So save the first values read and use them as
4505 * offsets to be subtracted from the raw values in order to report stats
4506 * that count from zero.
4508 if (!prev_stat_loaded)
4509 *prev_stat = new_data;
4510 if (likely(new_data >= *prev_stat))
4511 *cur_stat = new_data - *prev_stat;
4513 /* to manage the potential roll-over */
4514 *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
4518 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
4519 * @vsi: the VSI to be updated
4521 static void ice_update_eth_stats(struct ice_vsi *vsi)
4523 struct ice_eth_stats *prev_es, *cur_es;
4524 struct ice_hw *hw = &vsi->back->hw;
4525 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
4527 prev_es = &vsi->eth_stats_prev;
4528 cur_es = &vsi->eth_stats;
4530 ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
4531 vsi->stat_offsets_loaded, &prev_es->rx_bytes,
4534 ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
4535 vsi->stat_offsets_loaded, &prev_es->rx_unicast,
4536 &cur_es->rx_unicast);
4538 ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
4539 vsi->stat_offsets_loaded, &prev_es->rx_multicast,
4540 &cur_es->rx_multicast);
4542 ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
4543 vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
4544 &cur_es->rx_broadcast);
4546 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
4547 &prev_es->rx_discards, &cur_es->rx_discards);
4549 ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
4550 vsi->stat_offsets_loaded, &prev_es->tx_bytes,
4553 ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
4554 vsi->stat_offsets_loaded, &prev_es->tx_unicast,
4555 &cur_es->tx_unicast);
4557 ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
4558 vsi->stat_offsets_loaded, &prev_es->tx_multicast,
4559 &cur_es->tx_multicast);
4561 ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
4562 vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
4563 &cur_es->tx_broadcast);
4565 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
4566 &prev_es->tx_errors, &cur_es->tx_errors);
4568 vsi->stat_offsets_loaded = true;
4572 * ice_update_vsi_ring_stats - Update VSI stats counters
4573 * @vsi: the VSI to be updated
4575 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
4577 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
4578 struct ice_ring *ring;
4582 /* reset netdev stats */
4583 vsi_stats->tx_packets = 0;
4584 vsi_stats->tx_bytes = 0;
4585 vsi_stats->rx_packets = 0;
4586 vsi_stats->rx_bytes = 0;
4588 /* reset non-netdev (extended) stats */
4589 vsi->tx_restart = 0;
4591 vsi->tx_linearize = 0;
4592 vsi->rx_buf_failed = 0;
4593 vsi->rx_page_failed = 0;
4597 /* update Tx rings counters */
4598 ice_for_each_txq(vsi, i) {
4599 ring = READ_ONCE(vsi->tx_rings[i]);
4600 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4601 vsi_stats->tx_packets += pkts;
4602 vsi_stats->tx_bytes += bytes;
4603 vsi->tx_restart += ring->tx_stats.restart_q;
4604 vsi->tx_busy += ring->tx_stats.tx_busy;
4605 vsi->tx_linearize += ring->tx_stats.tx_linearize;
4608 /* update Rx rings counters */
4609 ice_for_each_rxq(vsi, i) {
4610 ring = READ_ONCE(vsi->rx_rings[i]);
4611 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4612 vsi_stats->rx_packets += pkts;
4613 vsi_stats->rx_bytes += bytes;
4614 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4615 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4622 * ice_update_vsi_stats - Update VSI stats counters
4623 * @vsi: the VSI to be updated
4625 static void ice_update_vsi_stats(struct ice_vsi *vsi)
4627 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4628 struct ice_eth_stats *cur_es = &vsi->eth_stats;
4629 struct ice_pf *pf = vsi->back;
4631 if (test_bit(__ICE_DOWN, vsi->state) ||
4632 test_bit(__ICE_CFG_BUSY, pf->state))
4635 /* get stats as recorded by Tx/Rx rings */
4636 ice_update_vsi_ring_stats(vsi);
4638 /* get VSI stats as recorded by the hardware */
4639 ice_update_eth_stats(vsi);
4641 cur_ns->tx_errors = cur_es->tx_errors;
4642 cur_ns->rx_dropped = cur_es->rx_discards;
4643 cur_ns->tx_dropped = cur_es->tx_discards;
4644 cur_ns->multicast = cur_es->rx_multicast;
4646 /* update some more netdev stats if this is main VSI */
4647 if (vsi->type == ICE_VSI_PF) {
4648 cur_ns->rx_crc_errors = pf->stats.crc_errors;
4649 cur_ns->rx_errors = pf->stats.crc_errors +
4650 pf->stats.illegal_bytes;
4651 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4656 * ice_update_pf_stats - Update PF port stats counters
4657 * @pf: PF whose stats needs to be updated
4659 static void ice_update_pf_stats(struct ice_pf *pf)
4661 struct ice_hw_port_stats *prev_ps, *cur_ps;
4662 struct ice_hw *hw = &pf->hw;
4665 prev_ps = &pf->stats_prev;
4666 cur_ps = &pf->stats;
4669 ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
4670 pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
4671 &cur_ps->eth.rx_bytes);
4673 ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
4674 pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
4675 &cur_ps->eth.rx_unicast);
4677 ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
4678 pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
4679 &cur_ps->eth.rx_multicast);
4681 ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
4682 pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
4683 &cur_ps->eth.rx_broadcast);
4685 ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
4686 pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
4687 &cur_ps->eth.tx_bytes);
4689 ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
4690 pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
4691 &cur_ps->eth.tx_unicast);
4693 ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
4694 pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
4695 &cur_ps->eth.tx_multicast);
4697 ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
4698 pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
4699 &cur_ps->eth.tx_broadcast);
4701 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
4702 &prev_ps->tx_dropped_link_down,
4703 &cur_ps->tx_dropped_link_down);
4705 ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
4706 pf->stat_prev_loaded, &prev_ps->rx_size_64,
4707 &cur_ps->rx_size_64);
4709 ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
4710 pf->stat_prev_loaded, &prev_ps->rx_size_127,
4711 &cur_ps->rx_size_127);
4713 ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
4714 pf->stat_prev_loaded, &prev_ps->rx_size_255,
4715 &cur_ps->rx_size_255);
4717 ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
4718 pf->stat_prev_loaded, &prev_ps->rx_size_511,
4719 &cur_ps->rx_size_511);
4721 ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
4722 GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
4723 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
4725 ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
4726 GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
4727 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
4729 ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
4730 GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
4731 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
4733 ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
4734 pf->stat_prev_loaded, &prev_ps->tx_size_64,
4735 &cur_ps->tx_size_64);
4737 ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
4738 pf->stat_prev_loaded, &prev_ps->tx_size_127,
4739 &cur_ps->tx_size_127);
4741 ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
4742 pf->stat_prev_loaded, &prev_ps->tx_size_255,
4743 &cur_ps->tx_size_255);
4745 ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
4746 pf->stat_prev_loaded, &prev_ps->tx_size_511,
4747 &cur_ps->tx_size_511);
4749 ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
4750 GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
4751 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
4753 ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
4754 GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
4755 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
4757 ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
4758 GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
4759 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
4761 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
4762 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
4764 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
4765 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
4767 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
4768 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
4770 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
4771 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
4773 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
4774 &prev_ps->crc_errors, &cur_ps->crc_errors);
4776 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
4777 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
4779 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
4780 &prev_ps->mac_local_faults,
4781 &cur_ps->mac_local_faults);
4783 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
4784 &prev_ps->mac_remote_faults,
4785 &cur_ps->mac_remote_faults);
4787 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
4788 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
4790 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
4791 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
4793 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
4794 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
4796 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
4797 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
4799 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
4800 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
4802 pf->stat_prev_loaded = true;
4806 * ice_get_stats64 - get statistics for network device structure
4807 * @netdev: network interface device structure
4808 * @stats: main device statistics structure
4811 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
4813 struct ice_netdev_priv *np = netdev_priv(netdev);
4814 struct rtnl_link_stats64 *vsi_stats;
4815 struct ice_vsi *vsi = np->vsi;
4817 vsi_stats = &vsi->net_stats;
4819 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
4821 /* netdev packet/byte stats come from ring counter. These are obtained
4822 * by summing up ring counters (done by ice_update_vsi_ring_stats).
4824 ice_update_vsi_ring_stats(vsi);
4825 stats->tx_packets = vsi_stats->tx_packets;
4826 stats->tx_bytes = vsi_stats->tx_bytes;
4827 stats->rx_packets = vsi_stats->rx_packets;
4828 stats->rx_bytes = vsi_stats->rx_bytes;
4830 /* The rest of the stats can be read from the hardware but instead we
4831 * just return values that the watchdog task has already obtained from
4834 stats->multicast = vsi_stats->multicast;
4835 stats->tx_errors = vsi_stats->tx_errors;
4836 stats->tx_dropped = vsi_stats->tx_dropped;
4837 stats->rx_errors = vsi_stats->rx_errors;
4838 stats->rx_dropped = vsi_stats->rx_dropped;
4839 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
4840 stats->rx_length_errors = vsi_stats->rx_length_errors;
4844 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4845 * @vsi: VSI having NAPI disabled
4847 static void ice_napi_disable_all(struct ice_vsi *vsi)
4854 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4855 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
4857 if (q_vector->rx.ring || q_vector->tx.ring)
4858 napi_disable(&q_vector->napi);
4863 * ice_down - Shutdown the connection
4864 * @vsi: The VSI being stopped
4866 int ice_down(struct ice_vsi *vsi)
4870 /* Caller of this function is expected to set the
4871 * vsi->state __ICE_DOWN bit
4874 netif_carrier_off(vsi->netdev);
4875 netif_tx_disable(vsi->netdev);
4878 ice_vsi_dis_irq(vsi);
4879 err = ice_vsi_stop_tx_rx_rings(vsi);
4880 ice_napi_disable_all(vsi);
4882 ice_for_each_txq(vsi, i)
4883 ice_clean_tx_ring(vsi->tx_rings[i]);
4885 ice_for_each_rxq(vsi, i)
4886 ice_clean_rx_ring(vsi->rx_rings[i]);
4889 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
4890 vsi->vsi_num, vsi->vsw->sw_id);
4895 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
4896 * @vsi: VSI having resources allocated
4898 * Return 0 on success, negative on failure
4900 static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
4904 if (!vsi->num_txq) {
4905 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
4910 ice_for_each_txq(vsi, i) {
4911 err = ice_setup_tx_ring(vsi->tx_rings[i]);
4920 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
4921 * @vsi: VSI having resources allocated
4923 * Return 0 on success, negative on failure
4925 static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
4929 if (!vsi->num_rxq) {
4930 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
4935 ice_for_each_rxq(vsi, i) {
4936 err = ice_setup_rx_ring(vsi->rx_rings[i]);
4945 * ice_vsi_req_irq - Request IRQ from the OS
4946 * @vsi: The VSI IRQ is being requested for
4947 * @basename: name for the vector
4949 * Return 0 on success and a negative value on error
4951 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
4953 struct ice_pf *pf = vsi->back;
4956 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4957 err = ice_vsi_req_irq_msix(vsi, basename);
4963 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
4964 * @vsi: the VSI having resources freed
4966 static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
4973 ice_for_each_txq(vsi, i)
4974 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4975 ice_free_tx_ring(vsi->tx_rings[i]);
4979 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
4980 * @vsi: the VSI having resources freed
4982 static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
4989 ice_for_each_rxq(vsi, i)
4990 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
4991 ice_free_rx_ring(vsi->rx_rings[i]);
4995 * ice_vsi_open - Called when a network interface is made active
4996 * @vsi: the VSI to open
4998 * Initialization of the VSI
5000 * Returns 0 on success, negative value on error
5002 static int ice_vsi_open(struct ice_vsi *vsi)
5004 char int_name[ICE_INT_NAME_STR_LEN];
5005 struct ice_pf *pf = vsi->back;
5008 /* allocate descriptors */
5009 err = ice_vsi_setup_tx_rings(vsi);
5013 err = ice_vsi_setup_rx_rings(vsi);
5017 err = ice_vsi_cfg(vsi);
5021 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5022 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5023 err = ice_vsi_req_irq(vsi, int_name);
5027 /* Notify the stack of the actual queue counts. */
5028 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5032 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5036 err = ice_up_complete(vsi);
5038 goto err_up_complete;
5045 ice_vsi_free_irq(vsi);
5047 ice_vsi_free_rx_rings(vsi);
5049 ice_vsi_free_tx_rings(vsi);
5055 * ice_vsi_close - Shut down a VSI
5056 * @vsi: the VSI being shut down
5058 static void ice_vsi_close(struct ice_vsi *vsi)
5060 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
5063 ice_vsi_free_irq(vsi);
5064 ice_vsi_free_tx_rings(vsi);
5065 ice_vsi_free_rx_rings(vsi);
5069 * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
5070 * @vsi: the VSI being removed
5072 static void ice_rss_clean(struct ice_vsi *vsi)
5078 if (vsi->rss_hkey_user)
5079 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
5080 if (vsi->rss_lut_user)
5081 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
5085 * ice_vsi_release - Delete a VSI and free its resources
5086 * @vsi: the VSI being removed
5088 * Returns 0 on success or < 0 on error
5090 static int ice_vsi_release(struct ice_vsi *vsi)
5099 unregister_netdev(vsi->netdev);
5100 free_netdev(vsi->netdev);
5104 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
5107 /* Disable VSI and free resources */
5108 ice_vsi_dis_irq(vsi);
5111 /* reclaim interrupt vectors back to PF */
5112 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
5113 pf->num_avail_msix += vsi->num_q_vectors;
5115 ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
5116 ice_vsi_delete(vsi);
5117 ice_vsi_free_q_vectors(vsi);
5118 ice_vsi_clear_rings(vsi);
5120 ice_vsi_put_qs(vsi);
5121 pf->q_left_tx += vsi->alloc_txq;
5122 pf->q_left_rx += vsi->alloc_rxq;
5130 * ice_dis_vsi - pause a VSI
5131 * @vsi: the VSI being paused
5133 static void ice_dis_vsi(struct ice_vsi *vsi)
5135 if (test_bit(__ICE_DOWN, vsi->state))
5138 set_bit(__ICE_NEEDS_RESTART, vsi->state);
5140 if (vsi->netdev && netif_running(vsi->netdev) &&
5141 vsi->type == ICE_VSI_PF)
5142 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5148 * ice_ena_vsi - resume a VSI
5149 * @vsi: the VSI being resume
5151 static void ice_ena_vsi(struct ice_vsi *vsi)
5153 if (!test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
5156 if (vsi->netdev && netif_running(vsi->netdev))
5157 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5158 else if (ice_vsi_open(vsi))
5159 /* this clears the DOWN bit */
5160 dev_dbg(&vsi->back->pdev->dev, "Failed open VSI 0x%04X on switch 0x%04X\n",
5161 vsi->vsi_num, vsi->vsw->sw_id);
5165 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
5168 static void ice_pf_dis_all_vsi(struct ice_pf *pf)
5172 ice_for_each_vsi(pf, v)
5174 ice_dis_vsi(pf->vsi[v]);
5178 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
5181 static void ice_pf_ena_all_vsi(struct ice_pf *pf)
5185 ice_for_each_vsi(pf, v)
5187 ice_ena_vsi(pf->vsi[v]);
5191 * ice_rebuild - rebuild after reset
5192 * @pf: pf to rebuild
5194 static void ice_rebuild(struct ice_pf *pf)
5196 struct device *dev = &pf->pdev->dev;
5197 struct ice_hw *hw = &pf->hw;
5198 enum ice_status ret;
5201 if (test_bit(__ICE_DOWN, pf->state))
5202 goto clear_recovery;
5204 dev_dbg(dev, "rebuilding pf\n");
5206 ret = ice_init_all_ctrlq(hw);
5208 dev_err(dev, "control queues init failed %d\n", ret);
5212 ret = ice_clear_pf_cfg(hw);
5214 dev_err(dev, "clear PF configuration failed %d\n", ret);
5218 ice_clear_pxe_mode(hw);
5220 ret = ice_get_caps(hw);
5222 dev_err(dev, "ice_get_caps failed %d\n", ret);
5226 /* basic nic switch setup */
5227 err = ice_setup_pf_sw(pf);
5229 dev_err(dev, "ice_setup_pf_sw failed\n");
5233 /* start misc vector */
5234 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
5235 err = ice_req_irq_msix_misc(pf);
5237 dev_err(dev, "misc vector setup failed: %d\n", err);
5242 /* restart the VSIs that were rebuilt and running before the reset */
5243 ice_pf_ena_all_vsi(pf);
5248 ice_shutdown_all_ctrlq(hw);
5249 set_bit(__ICE_RESET_FAILED, pf->state);
5251 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
5255 * ice_change_mtu - NDO callback to change the MTU
5256 * @netdev: network interface device structure
5257 * @new_mtu: new value for maximum frame size
5259 * Returns 0 on success, negative on failure
5261 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
5263 struct ice_netdev_priv *np = netdev_priv(netdev);
5264 struct ice_vsi *vsi = np->vsi;
5265 struct ice_pf *pf = vsi->back;
5268 if (new_mtu == netdev->mtu) {
5269 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
5273 if (new_mtu < netdev->min_mtu) {
5274 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
5277 } else if (new_mtu > netdev->max_mtu) {
5278 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
5282 /* if a reset is in progress, wait for some time for it to complete */
5284 if (ice_is_reset_recovery_pending(pf->state)) {
5286 usleep_range(1000, 2000);
5291 } while (count < 100);
5294 netdev_err(netdev, "can't change mtu. Device is busy\n");
5298 netdev->mtu = new_mtu;
5300 /* if VSI is up, bring it down and then back up */
5301 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
5304 err = ice_down(vsi);
5306 netdev_err(netdev, "change mtu if_up err %d\n", err);
5312 netdev_err(netdev, "change mtu if_up err %d\n", err);
5317 netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
5322 * ice_set_rss - Set RSS keys and lut
5323 * @vsi: Pointer to VSI structure
5324 * @seed: RSS hash seed
5325 * @lut: Lookup table
5326 * @lut_size: Lookup table size
5328 * Returns 0 on success, negative on failure
5330 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5332 struct ice_pf *pf = vsi->back;
5333 struct ice_hw *hw = &pf->hw;
5334 enum ice_status status;
5337 struct ice_aqc_get_set_rss_keys *buf =
5338 (struct ice_aqc_get_set_rss_keys *)seed;
5340 status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
5343 dev_err(&pf->pdev->dev,
5344 "Cannot set RSS key, err %d aq_err %d\n",
5345 status, hw->adminq.rq_last_status);
5351 status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
5352 vsi->rss_lut_type, lut, lut_size);
5354 dev_err(&pf->pdev->dev,
5355 "Cannot set RSS lut, err %d aq_err %d\n",
5356 status, hw->adminq.rq_last_status);
5365 * ice_get_rss - Get RSS keys and lut
5366 * @vsi: Pointer to VSI structure
5367 * @seed: Buffer to store the keys
5368 * @lut: Buffer to store the lookup table entries
5369 * @lut_size: Size of buffer to store the lookup table entries
5371 * Returns 0 on success, negative on failure
5373 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5375 struct ice_pf *pf = vsi->back;
5376 struct ice_hw *hw = &pf->hw;
5377 enum ice_status status;
5380 struct ice_aqc_get_set_rss_keys *buf =
5381 (struct ice_aqc_get_set_rss_keys *)seed;
5383 status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
5385 dev_err(&pf->pdev->dev,
5386 "Cannot get RSS key, err %d aq_err %d\n",
5387 status, hw->adminq.rq_last_status);
5393 status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
5394 vsi->rss_lut_type, lut, lut_size);
5396 dev_err(&pf->pdev->dev,
5397 "Cannot get RSS lut, err %d aq_err %d\n",
5398 status, hw->adminq.rq_last_status);
5407 * ice_open - Called when a network interface becomes active
5408 * @netdev: network interface device structure
5410 * The open entry point is called when a network interface is made
5411 * active by the system (IFF_UP). At this point all resources needed
5412 * for transmit and receive operations are allocated, the interrupt
5413 * handler is registered with the OS, the netdev watchdog is enabled,
5414 * and the stack is notified that the interface is ready.
5416 * Returns 0 on success, negative value on failure
5418 static int ice_open(struct net_device *netdev)
5420 struct ice_netdev_priv *np = netdev_priv(netdev);
5421 struct ice_vsi *vsi = np->vsi;
5424 netif_carrier_off(netdev);
5426 err = ice_vsi_open(vsi);
5429 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
5430 vsi->vsi_num, vsi->vsw->sw_id);
5435 * ice_stop - Disables a network interface
5436 * @netdev: network interface device structure
5438 * The stop entry point is called when an interface is de-activated by the OS,
5439 * and the netdevice enters the DOWN state. The hardware is still under the
5440 * driver's control, but the netdev interface is disabled.
5442 * Returns success only - not allowed to fail
5444 static int ice_stop(struct net_device *netdev)
5446 struct ice_netdev_priv *np = netdev_priv(netdev);
5447 struct ice_vsi *vsi = np->vsi;
5455 * ice_features_check - Validate encapsulated packet conforms to limits
5457 * @netdev: This port's netdev
5458 * @features: Offload features that the stack believes apply
5460 static netdev_features_t
5461 ice_features_check(struct sk_buff *skb,
5462 struct net_device __always_unused *netdev,
5463 netdev_features_t features)
5467 /* No point in doing any of this if neither checksum nor GSO are
5468 * being requested for this frame. We can rule out both by just
5469 * checking for CHECKSUM_PARTIAL
5471 if (skb->ip_summed != CHECKSUM_PARTIAL)
5474 /* We cannot support GSO if the MSS is going to be less than
5475 * 64 bytes. If it is then we need to drop support for GSO.
5477 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
5478 features &= ~NETIF_F_GSO_MASK;
5480 len = skb_network_header(skb) - skb->data;
5481 if (len & ~(ICE_TXD_MACLEN_MAX))
5482 goto out_rm_features;
5484 len = skb_transport_header(skb) - skb_network_header(skb);
5485 if (len & ~(ICE_TXD_IPLEN_MAX))
5486 goto out_rm_features;
5488 if (skb->encapsulation) {
5489 len = skb_inner_network_header(skb) - skb_transport_header(skb);
5490 if (len & ~(ICE_TXD_L4LEN_MAX))
5491 goto out_rm_features;
5493 len = skb_inner_transport_header(skb) -
5494 skb_inner_network_header(skb);
5495 if (len & ~(ICE_TXD_IPLEN_MAX))
5496 goto out_rm_features;
5501 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
5504 static const struct net_device_ops ice_netdev_ops = {
5505 .ndo_open = ice_open,
5506 .ndo_stop = ice_stop,
5507 .ndo_start_xmit = ice_start_xmit,
5508 .ndo_features_check = ice_features_check,
5509 .ndo_set_rx_mode = ice_set_rx_mode,
5510 .ndo_set_mac_address = ice_set_mac_address,
5511 .ndo_validate_addr = eth_validate_addr,
5512 .ndo_change_mtu = ice_change_mtu,
5513 .ndo_get_stats64 = ice_get_stats64,
5514 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
5515 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
5516 .ndo_set_features = ice_set_features,
5517 .ndo_fdb_add = ice_fdb_add,
5518 .ndo_fdb_del = ice_fdb_del,