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 "ice-0.7.0-k"
11 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
12 const char ice_drv_ver[] = DRV_VERSION;
13 static const char ice_driver_string[] = DRV_SUMMARY;
14 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
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_vsi_release_all(struct ice_pf *pf);
36 static void ice_update_vsi_stats(struct ice_vsi *vsi);
37 static void ice_update_pf_stats(struct ice_pf *pf);
40 * ice_get_free_slot - get the next non-NULL location index in array
41 * @array: array to search
42 * @size: size of the array
43 * @curr: last known occupied index to be used as a search hint
45 * void * is being used to keep the functionality generic. This lets us use this
46 * function on any array of pointers.
48 static int ice_get_free_slot(void *array, int size, int curr)
50 int **tmp_array = (int **)array;
53 if (curr < (size - 1) && !tmp_array[curr + 1]) {
58 while ((i < size) && (tmp_array[i]))
69 * ice_search_res - Search the tracker for a block of resources
70 * @res: pointer to the resource
71 * @needed: size of the block needed
72 * @id: identifier to track owner
73 * Returns the base item index of the block, or -ENOMEM for error
75 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
77 int start = res->search_hint;
80 id |= ICE_RES_VALID_BIT;
83 /* skip already allocated entries */
84 if (res->list[end++] & ICE_RES_VALID_BIT) {
86 if ((start + needed) > res->num_entries)
90 if (end == (start + needed)) {
93 /* there was enough, so assign it to the requestor */
97 if (end == res->num_entries)
100 res->search_hint = end;
109 * ice_get_res - get a block of resources
110 * @pf: board private structure
111 * @res: pointer to the resource
112 * @needed: size of the block needed
113 * @id: identifier to track owner
115 * Returns the base item index of the block, or -ENOMEM for error
116 * The search_hint trick and lack of advanced fit-finding only works
117 * because we're highly likely to have all the same sized requests.
118 * Linear search time and any fragmentation should be minimal.
121 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
128 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
129 dev_err(&pf->pdev->dev,
130 "param err: needed=%d, num_entries = %d id=0x%04x\n",
131 needed, res->num_entries, id);
135 /* search based on search_hint */
136 ret = ice_search_res(res, needed, id);
139 /* previous search failed. Reset search hint and try again */
140 res->search_hint = 0;
141 ret = ice_search_res(res, needed, id);
148 * ice_free_res - free a block of resources
149 * @res: pointer to the resource
150 * @index: starting index previously returned by ice_get_res
151 * @id: identifier to track owner
152 * Returns number of resources freed
154 static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
159 if (!res || index >= res->num_entries)
162 id |= ICE_RES_VALID_BIT;
163 for (i = index; i < res->num_entries && res->list[i] == id; i++) {
172 * ice_add_mac_to_list - Add a mac address filter entry to the list
173 * @vsi: the VSI to be forwarded to
174 * @add_list: pointer to the list which contains MAC filter entries
175 * @macaddr: the MAC address to be added.
177 * Adds mac address filter entry to the temp list
179 * Returns 0 on success or ENOMEM on failure.
181 static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
184 struct ice_fltr_list_entry *tmp;
185 struct ice_pf *pf = vsi->back;
187 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
191 tmp->fltr_info.flag = ICE_FLTR_TX;
192 tmp->fltr_info.src = vsi->vsi_num;
193 tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
194 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
195 tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
196 ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
198 INIT_LIST_HEAD(&tmp->list_entry);
199 list_add(&tmp->list_entry, add_list);
205 * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
206 * @netdev: the net device on which the sync is happening
207 * @addr: mac address to sync
209 * This is a callback function which is called by the in kernel device sync
210 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
211 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
212 * mac filters from the hardware.
214 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
216 struct ice_netdev_priv *np = netdev_priv(netdev);
217 struct ice_vsi *vsi = np->vsi;
219 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
226 * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
227 * @netdev: the net device on which the unsync is happening
228 * @addr: mac address to unsync
230 * This is a callback function which is called by the in kernel device unsync
231 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
232 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
233 * delete the mac filters from the hardware.
235 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
237 struct ice_netdev_priv *np = netdev_priv(netdev);
238 struct ice_vsi *vsi = np->vsi;
240 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
247 * ice_free_fltr_list - free filter lists helper
248 * @dev: pointer to the device struct
249 * @h: pointer to the list head to be freed
251 * Helper function to free filter lists previously created using
252 * ice_add_mac_to_list
254 static void ice_free_fltr_list(struct device *dev, struct list_head *h)
256 struct ice_fltr_list_entry *e, *tmp;
258 list_for_each_entry_safe(e, tmp, h, list_entry) {
259 list_del(&e->list_entry);
265 * ice_vsi_fltr_changed - check if filter state changed
266 * @vsi: VSI to be checked
268 * returns true if filter state has changed, false otherwise.
270 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
272 return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
273 test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
274 test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
278 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
279 * @vsi: ptr to the VSI
281 * Push any outstanding VSI filter changes through the AdminQ.
283 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
285 struct device *dev = &vsi->back->pdev->dev;
286 struct net_device *netdev = vsi->netdev;
287 bool promisc_forced_on = false;
288 struct ice_pf *pf = vsi->back;
289 struct ice_hw *hw = &pf->hw;
290 enum ice_status status = 0;
291 u32 changed_flags = 0;
297 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
298 usleep_range(1000, 2000);
300 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
301 vsi->current_netdev_flags = vsi->netdev->flags;
303 INIT_LIST_HEAD(&vsi->tmp_sync_list);
304 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
306 if (ice_vsi_fltr_changed(vsi)) {
307 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
308 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
309 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
311 /* grab the netdev's addr_list_lock */
312 netif_addr_lock_bh(netdev);
313 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
314 ice_add_mac_to_unsync_list);
315 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
316 ice_add_mac_to_unsync_list);
317 /* our temp lists are populated. release lock */
318 netif_addr_unlock_bh(netdev);
321 /* Remove mac addresses in the unsync list */
322 status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
323 ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
325 netdev_err(netdev, "Failed to delete MAC filters\n");
326 /* if we failed because of alloc failures, just bail */
327 if (status == ICE_ERR_NO_MEMORY) {
333 /* Add mac addresses in the sync list */
334 status = ice_add_mac(hw, &vsi->tmp_sync_list);
335 ice_free_fltr_list(dev, &vsi->tmp_sync_list);
337 netdev_err(netdev, "Failed to add MAC filters\n");
338 /* If there is no more space for new umac filters, vsi
339 * should go into promiscuous mode. There should be some
340 * space reserved for promiscuous filters.
342 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
343 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
345 promisc_forced_on = true;
347 "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
354 /* check for changes in promiscuous modes */
355 if (changed_flags & IFF_ALLMULTI)
356 netdev_warn(netdev, "Unsupported configuration\n");
358 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
359 test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
360 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
361 if (vsi->current_netdev_flags & IFF_PROMISC) {
362 /* Apply TX filter rule to get traffic from VMs */
363 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
366 netdev_err(netdev, "Error setting default VSI %i tx rule\n",
368 vsi->current_netdev_flags &= ~IFF_PROMISC;
372 /* Apply RX filter rule to get traffic from wire */
373 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
376 netdev_err(netdev, "Error setting default VSI %i rx rule\n",
378 vsi->current_netdev_flags &= ~IFF_PROMISC;
383 /* Clear TX filter rule to stop traffic from VMs */
384 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
387 netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
389 vsi->current_netdev_flags |= IFF_PROMISC;
393 /* Clear filter RX to remove traffic from wire */
394 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
397 netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
399 vsi->current_netdev_flags |= IFF_PROMISC;
408 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
411 /* if something went wrong then set the changed flag so we try again */
412 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
413 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
415 clear_bit(__ICE_CFG_BUSY, vsi->state);
420 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
421 * @pf: board private structure
423 static void ice_sync_fltr_subtask(struct ice_pf *pf)
427 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
430 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
432 for (v = 0; v < pf->num_alloc_vsi; v++)
433 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
434 ice_vsi_sync_fltr(pf->vsi[v])) {
435 /* come back and try again later */
436 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
442 * ice_is_reset_recovery_pending - schedule a reset
443 * @state: pf state field
445 static bool ice_is_reset_recovery_pending(unsigned long int *state)
447 return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
451 * ice_prepare_for_reset - prep for the core to reset
452 * @pf: board private structure
454 * Inform or close all dependent features in prep for reset.
457 ice_prepare_for_reset(struct ice_pf *pf)
459 struct ice_hw *hw = &pf->hw;
461 /* disable the VSIs and their queues that are not already DOWN */
462 ice_pf_dis_all_vsi(pf);
464 ice_shutdown_all_ctrlq(hw);
466 set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
470 * ice_do_reset - Initiate one of many types of resets
471 * @pf: board private structure
472 * @reset_type: reset type requested
473 * before this function was called.
475 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
477 struct device *dev = &pf->pdev->dev;
478 struct ice_hw *hw = &pf->hw;
480 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
481 WARN_ON(in_interrupt());
483 /* PFR is a bit of a special case because it doesn't result in an OICR
484 * interrupt. Set pending bit here which otherwise gets set in the
487 if (reset_type == ICE_RESET_PFR)
488 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
490 ice_prepare_for_reset(pf);
492 /* trigger the reset */
493 if (ice_reset(hw, reset_type)) {
494 dev_err(dev, "reset %d failed\n", reset_type);
495 set_bit(__ICE_RESET_FAILED, pf->state);
496 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
497 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
501 /* PFR is a bit of a special case because it doesn't result in an OICR
502 * interrupt. So for PFR, rebuild after the reset and clear the reset-
503 * associated state bits.
505 if (reset_type == ICE_RESET_PFR) {
508 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
509 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
514 * ice_reset_subtask - Set up for resetting the device and driver
515 * @pf: board private structure
517 static void ice_reset_subtask(struct ice_pf *pf)
519 enum ice_reset_req reset_type = ICE_RESET_INVAL;
521 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
522 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
523 * of reset is pending and sets bits in pf->state indicating the reset
524 * type and __ICE_RESET_RECOVERY_PENDING. So, if the latter bit is set
525 * prepare for pending reset if not already (for PF software-initiated
526 * global resets the software should already be prepared for it as
527 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
528 * by firmware or software on other PFs, that bit is not set so prepare
529 * for the reset now), poll for reset done, rebuild and return.
531 if (ice_is_reset_recovery_pending(pf->state)) {
532 clear_bit(__ICE_GLOBR_RECV, pf->state);
533 clear_bit(__ICE_CORER_RECV, pf->state);
534 if (!test_bit(__ICE_PREPARED_FOR_RESET, 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;
544 /* clear bit to resume normal operations, but
545 * ICE_NEEDS_RESTART bit is set incase rebuild failed
547 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
548 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
554 /* No pending resets to finish processing. Check for new resets */
555 if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
556 reset_type = ICE_RESET_PFR;
557 if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
558 reset_type = ICE_RESET_CORER;
559 if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
560 reset_type = ICE_RESET_GLOBR;
561 /* If no valid reset type requested just return */
562 if (reset_type == ICE_RESET_INVAL)
565 /* reset if not already down or busy */
566 if (!test_bit(__ICE_DOWN, pf->state) &&
567 !test_bit(__ICE_CFG_BUSY, pf->state)) {
568 ice_do_reset(pf, reset_type);
573 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
574 * @pf: board private structure
576 static void ice_watchdog_subtask(struct ice_pf *pf)
580 /* if interface is down do nothing */
581 if (test_bit(__ICE_DOWN, pf->state) ||
582 test_bit(__ICE_CFG_BUSY, pf->state))
585 /* make sure we don't do these things too often */
586 if (time_before(jiffies,
587 pf->serv_tmr_prev + pf->serv_tmr_period))
590 pf->serv_tmr_prev = jiffies;
592 /* Update the stats for active netdevs so the network stack
593 * can look at updated numbers whenever it cares to
595 ice_update_pf_stats(pf);
596 for (i = 0; i < pf->num_alloc_vsi; i++)
597 if (pf->vsi[i] && pf->vsi[i]->netdev)
598 ice_update_vsi_stats(pf->vsi[i]);
602 * ice_print_link_msg - print link up or down message
603 * @vsi: the VSI whose link status is being queried
604 * @isup: boolean for if the link is now up or down
606 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
611 if (vsi->current_isup == isup)
614 vsi->current_isup = isup;
617 netdev_info(vsi->netdev, "NIC Link is Down\n");
621 switch (vsi->port_info->phy.link_info.link_speed) {
622 case ICE_AQ_LINK_SPEED_40GB:
625 case ICE_AQ_LINK_SPEED_25GB:
628 case ICE_AQ_LINK_SPEED_20GB:
631 case ICE_AQ_LINK_SPEED_10GB:
634 case ICE_AQ_LINK_SPEED_5GB:
637 case ICE_AQ_LINK_SPEED_2500MB:
640 case ICE_AQ_LINK_SPEED_1000MB:
643 case ICE_AQ_LINK_SPEED_100MB:
651 switch (vsi->port_info->fc.current_mode) {
655 case ICE_FC_TX_PAUSE:
658 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 !test_bit(__ICE_NEEDS_RESTART, pf->state))
978 queue_work(ice_wq, &pf->serv_task);
982 * ice_service_task_complete - finish up the service task
983 * @pf: board private structure
985 static void ice_service_task_complete(struct ice_pf *pf)
987 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
989 /* force memory (pf->state) to sync before next service task */
990 smp_mb__before_atomic();
991 clear_bit(__ICE_SERVICE_SCHED, pf->state);
995 * ice_service_timer - timer callback to schedule service task
996 * @t: pointer to timer_list
998 static void ice_service_timer(struct timer_list *t)
1000 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1002 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1003 ice_service_task_schedule(pf);
1007 * ice_service_task - manage and run subtasks
1008 * @work: pointer to work_struct contained by the PF struct
1010 static void ice_service_task(struct work_struct *work)
1012 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1013 unsigned long start_time = jiffies;
1017 /* process reset requests first */
1018 ice_reset_subtask(pf);
1020 /* bail if a reset/recovery cycle is pending or rebuild failed */
1021 if (ice_is_reset_recovery_pending(pf->state) ||
1022 test_bit(__ICE_SUSPENDED, pf->state) ||
1023 test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1024 ice_service_task_complete(pf);
1028 ice_sync_fltr_subtask(pf);
1029 ice_watchdog_subtask(pf);
1030 ice_clean_adminq_subtask(pf);
1032 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1033 ice_service_task_complete(pf);
1035 /* If the tasks have taken longer than one service timer period
1036 * or there is more work to be done, reset the service timer to
1037 * schedule the service task now.
1039 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1040 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1041 mod_timer(&pf->serv_tmr, jiffies);
1045 * ice_set_ctrlq_len - helper function to set controlq length
1046 * @hw: pointer to the hw instance
1048 static void ice_set_ctrlq_len(struct ice_hw *hw)
1050 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1051 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1052 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1053 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1057 * ice_irq_affinity_notify - Callback for affinity changes
1058 * @notify: context as to what irq was changed
1059 * @mask: the new affinity mask
1061 * This is a callback function used by the irq_set_affinity_notifier function
1062 * so that we may register to receive changes to the irq affinity masks.
1064 static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1065 const cpumask_t *mask)
1067 struct ice_q_vector *q_vector =
1068 container_of(notify, struct ice_q_vector, affinity_notify);
1070 cpumask_copy(&q_vector->affinity_mask, mask);
1074 * ice_irq_affinity_release - Callback for affinity notifier release
1075 * @ref: internal core kernel usage
1077 * This is a callback function used by the irq_set_affinity_notifier function
1078 * to inform the current notification subscriber that they will no longer
1079 * receive notifications.
1081 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1084 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
1085 * @vsi: the VSI being un-configured
1087 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
1089 struct ice_pf *pf = vsi->back;
1090 struct ice_hw *hw = &pf->hw;
1091 int base = vsi->base_vector;
1095 /* disable interrupt causation from each queue */
1096 if (vsi->tx_rings) {
1097 ice_for_each_txq(vsi, i) {
1098 if (vsi->tx_rings[i]) {
1101 reg = vsi->tx_rings[i]->reg_idx;
1102 val = rd32(hw, QINT_TQCTL(reg));
1103 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1104 wr32(hw, QINT_TQCTL(reg), val);
1109 if (vsi->rx_rings) {
1110 ice_for_each_rxq(vsi, i) {
1111 if (vsi->rx_rings[i]) {
1114 reg = vsi->rx_rings[i]->reg_idx;
1115 val = rd32(hw, QINT_RQCTL(reg));
1116 val &= ~QINT_RQCTL_CAUSE_ENA_M;
1117 wr32(hw, QINT_RQCTL(reg), val);
1122 /* disable each interrupt */
1123 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1124 for (i = vsi->base_vector;
1125 i < (vsi->num_q_vectors + vsi->base_vector); i++)
1126 wr32(hw, GLINT_DYN_CTL(i), 0);
1129 for (i = 0; i < vsi->num_q_vectors; i++)
1130 synchronize_irq(pf->msix_entries[i + base].vector);
1135 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1136 * @vsi: the VSI being configured
1138 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1140 struct ice_pf *pf = vsi->back;
1141 struct ice_hw *hw = &pf->hw;
1143 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1146 for (i = 0; i < vsi->num_q_vectors; i++)
1147 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1155 * ice_vsi_delete - delete a VSI from the switch
1156 * @vsi: pointer to VSI being removed
1158 static void ice_vsi_delete(struct ice_vsi *vsi)
1160 struct ice_pf *pf = vsi->back;
1161 struct ice_vsi_ctx ctxt;
1162 enum ice_status status;
1164 ctxt.vsi_num = vsi->vsi_num;
1166 memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
1168 status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
1170 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
1175 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1176 * @vsi: the VSI being configured
1177 * @basename: name for the vector
1179 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1181 int q_vectors = vsi->num_q_vectors;
1182 struct ice_pf *pf = vsi->back;
1183 int base = vsi->base_vector;
1189 for (vector = 0; vector < q_vectors; vector++) {
1190 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1192 irq_num = pf->msix_entries[base + vector].vector;
1194 if (q_vector->tx.ring && q_vector->rx.ring) {
1195 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1196 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1198 } else if (q_vector->rx.ring) {
1199 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1200 "%s-%s-%d", basename, "rx", rx_int_idx++);
1201 } else if (q_vector->tx.ring) {
1202 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1203 "%s-%s-%d", basename, "tx", tx_int_idx++);
1205 /* skip this unused q_vector */
1208 err = devm_request_irq(&pf->pdev->dev,
1209 pf->msix_entries[base + vector].vector,
1210 vsi->irq_handler, 0, q_vector->name,
1213 netdev_err(vsi->netdev,
1214 "MSIX request_irq failed, error: %d\n", err);
1218 /* register for affinity change notifications */
1219 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1220 q_vector->affinity_notify.release = ice_irq_affinity_release;
1221 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1223 /* assign the mask for this irq */
1224 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1227 vsi->irqs_ready = true;
1233 irq_num = pf->msix_entries[base + vector].vector,
1234 irq_set_affinity_notifier(irq_num, NULL);
1235 irq_set_affinity_hint(irq_num, NULL);
1236 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1242 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
1243 * @vsi: the VSI being configured
1245 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
1247 struct ice_hw_common_caps *cap;
1248 struct ice_pf *pf = vsi->back;
1250 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1255 cap = &pf->hw.func_caps.common_cap;
1256 switch (vsi->type) {
1258 /* PF VSI will inherit RSS instance of PF */
1259 vsi->rss_table_size = cap->rss_table_size;
1260 vsi->rss_size = min_t(int, num_online_cpus(),
1261 BIT(cap->rss_table_entry_width));
1262 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
1265 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1271 * ice_vsi_setup_q_map - Setup a VSI queue map
1272 * @vsi: the VSI being configured
1273 * @ctxt: VSI context structure
1275 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1277 u16 offset = 0, qmap = 0, numq_tc;
1278 u16 pow = 0, max_rss = 0, qcount;
1279 u16 qcount_tx = vsi->alloc_txq;
1280 u16 qcount_rx = vsi->alloc_rxq;
1281 bool ena_tc0 = false;
1284 /* at least TC0 should be enabled by default */
1285 if (vsi->tc_cfg.numtc) {
1286 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
1293 vsi->tc_cfg.numtc++;
1294 vsi->tc_cfg.ena_tc |= 1;
1297 numq_tc = qcount_rx / vsi->tc_cfg.numtc;
1299 /* TC mapping is a function of the number of Rx queues assigned to the
1300 * VSI for each traffic class and the offset of these queues.
1301 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1302 * queues allocated to TC0. No:of queues is a power-of-2.
1304 * If TC is not enabled, the queue offset is set to 0, and allocate one
1305 * queue, this way, traffic for the given TC will be sent to the default
1308 * Setup number and offset of Rx queues for all TCs for the VSI
1311 /* qcount will change if RSS is enabled */
1312 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
1313 if (vsi->type == ICE_VSI_PF)
1314 max_rss = ICE_MAX_LG_RSS_QS;
1316 max_rss = ICE_MAX_SMALL_RSS_QS;
1318 qcount = min_t(int, numq_tc, max_rss);
1319 qcount = min_t(int, qcount, vsi->rss_size);
1324 /* find the (rounded up) power-of-2 of qcount */
1325 pow = order_base_2(qcount);
1327 for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1328 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1329 /* TC is not enabled */
1330 vsi->tc_cfg.tc_info[i].qoffset = 0;
1331 vsi->tc_cfg.tc_info[i].qcount = 1;
1332 ctxt->info.tc_mapping[i] = 0;
1337 vsi->tc_cfg.tc_info[i].qoffset = offset;
1338 vsi->tc_cfg.tc_info[i].qcount = qcount;
1340 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1341 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1342 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1343 ICE_AQ_VSI_TC_Q_NUM_M);
1345 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1348 vsi->num_txq = qcount_tx;
1349 vsi->num_rxq = offset;
1351 /* Rx queue mapping */
1352 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1353 /* q_mapping buffer holds the info for the first queue allocated for
1354 * this VSI in the PF space and also the number of queues associated
1357 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1358 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1362 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
1363 * @ctxt: the VSI context being set
1365 * This initializes a default VSI context for all sections except the Queues.
1367 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
1371 memset(&ctxt->info, 0, sizeof(ctxt->info));
1372 /* VSI's should be allocated from shared pool */
1373 ctxt->alloc_from_pool = true;
1374 /* Src pruning enabled by default */
1375 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
1376 /* Traffic from VSI can be sent to LAN */
1377 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1379 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
1380 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
1381 * packets untagged/tagged.
1383 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
1384 ICE_AQ_VSI_VLAN_MODE_M) >>
1385 ICE_AQ_VSI_VLAN_MODE_S);
1387 /* Have 1:1 UP mapping for both ingress/egress tables */
1388 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1389 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1390 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1391 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1392 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1393 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1394 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1395 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1396 ctxt->info.ingress_table = cpu_to_le32(table);
1397 ctxt->info.egress_table = cpu_to_le32(table);
1398 /* Have 1:1 UP mapping for outer to inner UP table */
1399 ctxt->info.outer_up_table = cpu_to_le32(table);
1400 /* No Outer tag support outer_tag_flags remains to zero */
1404 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1405 * @ctxt: the VSI context being set
1406 * @vsi: the VSI being configured
1408 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1410 u8 lut_type, hash_type;
1412 switch (vsi->type) {
1414 /* PF VSI will inherit RSS instance of PF */
1415 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1416 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1419 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1424 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1425 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1426 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1427 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1431 * ice_vsi_init - Create and initialize a VSI
1432 * @vsi: the VSI being configured
1434 * This initializes a VSI context depending on the VSI type to be added and
1435 * passes it down to the add_vsi aq command to create a new VSI.
1437 static int ice_vsi_init(struct ice_vsi *vsi)
1439 struct ice_vsi_ctx ctxt = { 0 };
1440 struct ice_pf *pf = vsi->back;
1441 struct ice_hw *hw = &pf->hw;
1444 switch (vsi->type) {
1446 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
1452 ice_set_dflt_vsi_ctx(&ctxt);
1453 /* if the switch is in VEB mode, allow VSI loopback */
1454 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1455 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1457 /* Set LUT type and HASH type if RSS is enabled */
1458 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1459 ice_set_rss_vsi_ctx(&ctxt, vsi);
1461 ctxt.info.sw_id = vsi->port_info->sw_id;
1462 ice_vsi_setup_q_map(vsi, &ctxt);
1464 ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
1466 dev_err(&pf->pdev->dev,
1467 "Add VSI failed, err %d\n", ret);
1471 /* keep context for update VSI operations */
1472 vsi->info = ctxt.info;
1474 /* record VSI number returned */
1475 vsi->vsi_num = ctxt.vsi_num;
1481 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1482 * @vsi: the VSI being cleaned up
1484 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1486 struct ice_pf *pf = vsi->back;
1487 u16 vector = vsi->base_vector;
1488 struct ice_hw *hw = &pf->hw;
1493 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1494 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1496 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1497 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1498 for (q = 0; q < q_vector->num_ring_tx; q++) {
1499 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1503 for (q = 0; q < q_vector->num_ring_rx; q++) {
1504 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1513 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1514 * @vsi: the VSI having rings deallocated
1516 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1520 if (vsi->tx_rings) {
1521 for (i = 0; i < vsi->alloc_txq; i++) {
1522 if (vsi->tx_rings[i]) {
1523 kfree_rcu(vsi->tx_rings[i], rcu);
1524 vsi->tx_rings[i] = NULL;
1528 if (vsi->rx_rings) {
1529 for (i = 0; i < vsi->alloc_rxq; i++) {
1530 if (vsi->rx_rings[i]) {
1531 kfree_rcu(vsi->rx_rings[i], rcu);
1532 vsi->rx_rings[i] = NULL;
1539 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1540 * @vsi: VSI which is having rings allocated
1542 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1544 struct ice_pf *pf = vsi->back;
1547 /* Allocate tx_rings */
1548 for (i = 0; i < vsi->alloc_txq; i++) {
1549 struct ice_ring *ring;
1551 /* allocate with kzalloc(), free with kfree_rcu() */
1552 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1558 ring->reg_idx = vsi->txq_map[i];
1559 ring->ring_active = false;
1561 ring->netdev = vsi->netdev;
1562 ring->dev = &pf->pdev->dev;
1563 ring->count = vsi->num_desc;
1565 vsi->tx_rings[i] = ring;
1568 /* Allocate rx_rings */
1569 for (i = 0; i < vsi->alloc_rxq; i++) {
1570 struct ice_ring *ring;
1572 /* allocate with kzalloc(), free with kfree_rcu() */
1573 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1578 ring->reg_idx = vsi->rxq_map[i];
1579 ring->ring_active = false;
1581 ring->netdev = vsi->netdev;
1582 ring->dev = &pf->pdev->dev;
1583 ring->count = vsi->num_desc;
1584 vsi->rx_rings[i] = ring;
1590 ice_vsi_clear_rings(vsi);
1595 * ice_vsi_free_irq - Free the irq association with the OS
1596 * @vsi: the VSI being configured
1598 static void ice_vsi_free_irq(struct ice_vsi *vsi)
1600 struct ice_pf *pf = vsi->back;
1601 int base = vsi->base_vector;
1603 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1606 if (!vsi->q_vectors || !vsi->irqs_ready)
1609 vsi->irqs_ready = false;
1610 for (i = 0; i < vsi->num_q_vectors; i++) {
1611 u16 vector = i + base;
1614 irq_num = pf->msix_entries[vector].vector;
1616 /* free only the irqs that were actually requested */
1617 if (!vsi->q_vectors[i] ||
1618 !(vsi->q_vectors[i]->num_ring_tx ||
1619 vsi->q_vectors[i]->num_ring_rx))
1622 /* clear the affinity notifier in the IRQ descriptor */
1623 irq_set_affinity_notifier(irq_num, NULL);
1625 /* clear the affinity_mask in the IRQ descriptor */
1626 irq_set_affinity_hint(irq_num, NULL);
1627 synchronize_irq(irq_num);
1628 devm_free_irq(&pf->pdev->dev, irq_num,
1631 ice_vsi_release_msix(vsi);
1636 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1637 * @vsi: the VSI being configured
1639 static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1641 struct ice_pf *pf = vsi->back;
1642 u16 vector = vsi->base_vector;
1643 struct ice_hw *hw = &pf->hw;
1644 u32 txq = 0, rxq = 0;
1648 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1649 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1651 itr_gran = hw->itr_gran_200;
1653 if (q_vector->num_ring_rx) {
1655 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1657 q_vector->rx.latency_range = ICE_LOW_LATENCY;
1660 if (q_vector->num_ring_tx) {
1662 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1664 q_vector->tx.latency_range = ICE_LOW_LATENCY;
1666 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1667 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1669 /* Both Transmit Queue Interrupt Cause Control register
1670 * and Receive Queue Interrupt Cause control register
1671 * expects MSIX_INDX field to be the vector index
1672 * within the function space and not the absolute
1673 * vector index across PF or across device.
1674 * For SR-IOV VF VSIs queue vector index always starts
1675 * with 1 since first vector index(0) is used for OICR
1676 * in VF space. Since VMDq and other PF VSIs are withtin
1677 * the PF function space, use the vector index thats
1678 * tracked for this PF.
1680 for (q = 0; q < q_vector->num_ring_tx; q++) {
1684 val = QINT_TQCTL_CAUSE_ENA_M |
1685 (itr << QINT_TQCTL_ITR_INDX_S) |
1686 (vector << QINT_TQCTL_MSIX_INDX_S);
1687 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1691 for (q = 0; q < q_vector->num_ring_rx; q++) {
1695 val = QINT_RQCTL_CAUSE_ENA_M |
1696 (itr << QINT_RQCTL_ITR_INDX_S) |
1697 (vector << QINT_RQCTL_MSIX_INDX_S);
1698 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1707 * ice_ena_misc_vector - enable the non-queue interrupts
1708 * @pf: board private structure
1710 static void ice_ena_misc_vector(struct ice_pf *pf)
1712 struct ice_hw *hw = &pf->hw;
1715 /* clear things first */
1716 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1717 rd32(hw, PFINT_OICR); /* read to clear */
1719 val = (PFINT_OICR_ECC_ERR_M |
1720 PFINT_OICR_MAL_DETECT_M |
1722 PFINT_OICR_PCI_EXCEPTION_M |
1723 PFINT_OICR_HMC_ERR_M |
1724 PFINT_OICR_PE_CRITERR_M);
1726 wr32(hw, PFINT_OICR_ENA, val);
1728 /* SW_ITR_IDX = 0, but don't change INTENA */
1729 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1730 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1734 * ice_misc_intr - misc interrupt handler
1735 * @irq: interrupt number
1736 * @data: pointer to a q_vector
1738 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1740 struct ice_pf *pf = (struct ice_pf *)data;
1741 struct ice_hw *hw = &pf->hw;
1742 irqreturn_t ret = IRQ_NONE;
1745 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1747 oicr = rd32(hw, PFINT_OICR);
1748 ena_mask = rd32(hw, PFINT_OICR_ENA);
1750 if (oicr & PFINT_OICR_GRST_M) {
1752 /* we have a reset warning */
1753 ena_mask &= ~PFINT_OICR_GRST_M;
1754 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1755 GLGEN_RSTAT_RESET_TYPE_S;
1757 if (reset == ICE_RESET_CORER)
1759 else if (reset == ICE_RESET_GLOBR)
1764 /* If a reset cycle isn't already in progress, we set a bit in
1765 * pf->state so that the service task can start a reset/rebuild.
1766 * We also make note of which reset happened so that peer
1767 * devices/drivers can be informed.
1769 if (!test_and_set_bit(__ICE_RESET_RECOVERY_PENDING,
1771 if (reset == ICE_RESET_CORER)
1772 set_bit(__ICE_CORER_RECV, pf->state);
1773 else if (reset == ICE_RESET_GLOBR)
1774 set_bit(__ICE_GLOBR_RECV, pf->state);
1776 set_bit(__ICE_EMPR_RECV, pf->state);
1778 /* There are couple of different bits at play here.
1779 * hw->reset_ongoing indicates whether the hardware is
1780 * in reset. This is set to true when a reset interrupt
1781 * is received and set back to false after the driver
1782 * has determined that the hardware is out of reset.
1784 * __ICE_RESET_RECOVERY_PENDING in pf->state indicates
1785 * that a post reset rebuild is required before the
1786 * driver is operational again. This is set above.
1788 * As this is the start of the reset/rebuild cycle, set
1789 * both to indicate that.
1791 hw->reset_ongoing = true;
1795 if (oicr & PFINT_OICR_HMC_ERR_M) {
1796 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1797 dev_dbg(&pf->pdev->dev,
1798 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1799 rd32(hw, PFHMC_ERRORINFO),
1800 rd32(hw, PFHMC_ERRORDATA));
1803 /* Report and mask off any remaining unexpected interrupts */
1806 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1808 /* If a critical error is pending there is no choice but to
1811 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1812 PFINT_OICR_PCI_EXCEPTION_M |
1813 PFINT_OICR_ECC_ERR_M)) {
1814 set_bit(__ICE_PFR_REQ, pf->state);
1815 ice_service_task_schedule(pf);
1821 /* re-enable interrupt causes that are not handled during this pass */
1822 wr32(hw, PFINT_OICR_ENA, ena_mask);
1823 if (!test_bit(__ICE_DOWN, pf->state)) {
1824 ice_service_task_schedule(pf);
1825 ice_irq_dynamic_ena(hw, NULL, NULL);
1832 * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1833 * @vsi: the VSI being configured
1835 * This function maps descriptor rings to the queue-specific vectors allotted
1836 * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1837 * and Rx rings to the vector as "efficiently" as possible.
1839 static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1841 int q_vectors = vsi->num_q_vectors;
1842 int tx_rings_rem, rx_rings_rem;
1845 /* initially assigning remaining rings count to VSIs num queue value */
1846 tx_rings_rem = vsi->num_txq;
1847 rx_rings_rem = vsi->num_rxq;
1849 for (v_id = 0; v_id < q_vectors; v_id++) {
1850 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1851 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1853 /* Tx rings mapping to vector */
1854 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1855 q_vector->num_ring_tx = tx_rings_per_v;
1856 q_vector->tx.ring = NULL;
1857 q_base = vsi->num_txq - tx_rings_rem;
1859 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1860 struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1862 tx_ring->q_vector = q_vector;
1863 tx_ring->next = q_vector->tx.ring;
1864 q_vector->tx.ring = tx_ring;
1866 tx_rings_rem -= tx_rings_per_v;
1868 /* Rx rings mapping to vector */
1869 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1870 q_vector->num_ring_rx = rx_rings_per_v;
1871 q_vector->rx.ring = NULL;
1872 q_base = vsi->num_rxq - rx_rings_rem;
1874 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1875 struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1877 rx_ring->q_vector = q_vector;
1878 rx_ring->next = q_vector->rx.ring;
1879 q_vector->rx.ring = rx_ring;
1881 rx_rings_rem -= rx_rings_per_v;
1886 * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
1887 * @vsi: the VSI being configured
1889 * Return 0 on success and a negative value on error
1891 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
1893 struct ice_pf *pf = vsi->back;
1895 switch (vsi->type) {
1897 vsi->alloc_txq = pf->num_lan_tx;
1898 vsi->alloc_rxq = pf->num_lan_rx;
1899 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
1900 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
1903 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1910 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
1912 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
1914 * On error: returns error code (negative)
1915 * On success: returns 0
1917 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
1919 struct ice_pf *pf = vsi->back;
1921 /* allocate memory for both Tx and Rx ring pointers */
1922 vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
1923 sizeof(struct ice_ring *), GFP_KERNEL);
1927 vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
1928 sizeof(struct ice_ring *), GFP_KERNEL);
1932 if (alloc_qvectors) {
1933 /* allocate memory for q_vector pointers */
1934 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
1936 sizeof(struct ice_q_vector *),
1938 if (!vsi->q_vectors)
1945 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
1947 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
1953 * ice_msix_clean_rings - MSIX mode Interrupt Handler
1954 * @irq: interrupt number
1955 * @data: pointer to a q_vector
1957 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
1959 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
1961 if (!q_vector->tx.ring && !q_vector->rx.ring)
1964 napi_schedule(&q_vector->napi);
1970 * ice_vsi_alloc - Allocates the next available struct vsi in the PF
1971 * @pf: board private structure
1972 * @type: type of VSI
1974 * returns a pointer to a VSI on success, NULL on failure.
1976 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
1978 struct ice_vsi *vsi = NULL;
1980 /* Need to protect the allocation of the VSIs at the PF level */
1981 mutex_lock(&pf->sw_mutex);
1983 /* If we have already allocated our maximum number of VSIs,
1984 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
1985 * is available to be populated
1987 if (pf->next_vsi == ICE_NO_VSI) {
1988 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
1992 vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
1998 set_bit(__ICE_DOWN, vsi->state);
1999 vsi->idx = pf->next_vsi;
2000 vsi->work_lmt = ICE_DFLT_IRQ_WORK;
2002 ice_vsi_set_num_qs(vsi);
2004 switch (vsi->type) {
2006 if (ice_vsi_alloc_arrays(vsi, true))
2009 /* Setup default MSIX irq handler for VSI */
2010 vsi->irq_handler = ice_msix_clean_rings;
2013 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
2017 /* fill VSI slot in the PF struct */
2018 pf->vsi[pf->next_vsi] = vsi;
2020 /* prepare pf->next_vsi for next use */
2021 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
2026 devm_kfree(&pf->pdev->dev, vsi);
2029 mutex_unlock(&pf->sw_mutex);
2034 * ice_free_irq_msix_misc - Unroll misc vector setup
2035 * @pf: board private structure
2037 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2039 /* disable OICR interrupt */
2040 wr32(&pf->hw, PFINT_OICR_ENA, 0);
2043 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
2044 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2045 devm_free_irq(&pf->pdev->dev,
2046 pf->msix_entries[pf->oicr_idx].vector, pf);
2049 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2053 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2054 * @pf: board private structure
2056 * This sets up the handler for MSIX 0, which is used to manage the
2057 * non-queue interrupts, e.g. AdminQ and errors. This is not used
2058 * when in MSI or Legacy interrupt mode.
2060 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2062 struct ice_hw *hw = &pf->hw;
2063 int oicr_idx, err = 0;
2067 if (!pf->int_name[0])
2068 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2069 dev_driver_string(&pf->pdev->dev),
2070 dev_name(&pf->pdev->dev));
2072 /* Do not request IRQ but do enable OICR interrupt since settings are
2073 * lost during reset. Note that this function is called only during
2074 * rebuild path and not while reset is in progress.
2076 if (ice_is_reset_recovery_pending(pf->state))
2079 /* reserve one vector in irq_tracker for misc interrupts */
2080 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2084 pf->oicr_idx = oicr_idx;
2086 err = devm_request_irq(&pf->pdev->dev,
2087 pf->msix_entries[pf->oicr_idx].vector,
2088 ice_misc_intr, 0, pf->int_name, pf);
2090 dev_err(&pf->pdev->dev,
2091 "devm_request_irq for %s failed: %d\n",
2093 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2098 ice_ena_misc_vector(pf);
2100 val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2101 PFINT_OICR_CTL_CAUSE_ENA_M);
2102 wr32(hw, PFINT_OICR_CTL, val);
2104 /* This enables Admin queue Interrupt causes */
2105 val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2106 PFINT_FW_CTL_CAUSE_ENA_M);
2107 wr32(hw, PFINT_FW_CTL, val);
2109 itr_gran = hw->itr_gran_200;
2111 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2112 ITR_TO_REG(ICE_ITR_8K, itr_gran));
2115 ice_irq_dynamic_ena(hw, NULL, NULL);
2121 * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
2122 * @vsi: the VSI getting queues
2124 * Return 0 on success and a negative value on error
2126 static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
2128 struct ice_pf *pf = vsi->back;
2129 int offset, ret = 0;
2131 mutex_lock(&pf->avail_q_mutex);
2132 /* look for contiguous block of queues for tx */
2133 offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
2134 0, vsi->alloc_txq, 0);
2135 if (offset < ICE_MAX_TXQS) {
2138 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
2139 for (i = 0; i < vsi->alloc_txq; i++)
2140 vsi->txq_map[i] = i + offset;
2143 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
2146 /* look for contiguous block of queues for rx */
2147 offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
2148 0, vsi->alloc_rxq, 0);
2149 if (offset < ICE_MAX_RXQS) {
2152 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
2153 for (i = 0; i < vsi->alloc_rxq; i++)
2154 vsi->rxq_map[i] = i + offset;
2157 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
2159 mutex_unlock(&pf->avail_q_mutex);
2165 * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
2166 * @vsi: the VSI getting queues
2168 * Return 0 on success and a negative value on error
2170 static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
2172 struct ice_pf *pf = vsi->back;
2175 mutex_lock(&pf->avail_q_mutex);
2177 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2178 for (i = 0; i < vsi->alloc_txq; i++) {
2179 index = find_next_zero_bit(pf->avail_txqs,
2180 ICE_MAX_TXQS, index);
2181 if (index < ICE_MAX_TXQS) {
2182 set_bit(index, pf->avail_txqs);
2183 vsi->txq_map[i] = index;
2185 goto err_scatter_tx;
2190 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2191 for (i = 0; i < vsi->alloc_rxq; i++) {
2192 index = find_next_zero_bit(pf->avail_rxqs,
2193 ICE_MAX_RXQS, index);
2194 if (index < ICE_MAX_RXQS) {
2195 set_bit(index, pf->avail_rxqs);
2196 vsi->rxq_map[i] = index;
2198 goto err_scatter_rx;
2203 mutex_unlock(&pf->avail_q_mutex);
2207 /* unflag any queues we have grabbed (i is failed position) */
2208 for (index = 0; index < i; index++) {
2209 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
2210 vsi->rxq_map[index] = 0;
2214 /* i is either position of failed attempt or vsi->alloc_txq */
2215 for (index = 0; index < i; index++) {
2216 clear_bit(vsi->txq_map[index], pf->avail_txqs);
2217 vsi->txq_map[index] = 0;
2220 mutex_unlock(&pf->avail_q_mutex);
2225 * ice_vsi_get_qs - Assign queues from PF to VSI
2226 * @vsi: the VSI to assign queues to
2228 * Returns 0 on success and a negative value on error
2230 static int ice_vsi_get_qs(struct ice_vsi *vsi)
2234 vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
2235 vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
2237 /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
2238 * modes individually to scatter if assigning contiguous queues
2241 ret = ice_vsi_get_qs_contig(vsi);
2243 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
2244 vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
2245 ICE_MAX_SCATTER_TXQS);
2246 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
2247 vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
2248 ICE_MAX_SCATTER_RXQS);
2249 ret = ice_vsi_get_qs_scatter(vsi);
2256 * ice_vsi_put_qs - Release queues from VSI to PF
2257 * @vsi: the VSI thats going to release queues
2259 static void ice_vsi_put_qs(struct ice_vsi *vsi)
2261 struct ice_pf *pf = vsi->back;
2264 mutex_lock(&pf->avail_q_mutex);
2266 for (i = 0; i < vsi->alloc_txq; i++) {
2267 clear_bit(vsi->txq_map[i], pf->avail_txqs);
2268 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
2271 for (i = 0; i < vsi->alloc_rxq; i++) {
2272 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
2273 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
2276 mutex_unlock(&pf->avail_q_mutex);
2280 * ice_free_q_vector - Free memory allocated for a specific interrupt vector
2281 * @vsi: VSI having the memory freed
2282 * @v_idx: index of the vector to be freed
2284 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
2286 struct ice_q_vector *q_vector;
2287 struct ice_ring *ring;
2289 if (!vsi->q_vectors[v_idx]) {
2290 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
2294 q_vector = vsi->q_vectors[v_idx];
2296 ice_for_each_ring(ring, q_vector->tx)
2297 ring->q_vector = NULL;
2298 ice_for_each_ring(ring, q_vector->rx)
2299 ring->q_vector = NULL;
2301 /* only VSI with an associated netdev is set up with NAPI */
2303 netif_napi_del(&q_vector->napi);
2305 devm_kfree(&vsi->back->pdev->dev, q_vector);
2306 vsi->q_vectors[v_idx] = NULL;
2310 * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
2311 * @vsi: the VSI having memory freed
2313 static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
2317 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
2318 ice_free_q_vector(vsi, v_idx);
2322 * ice_cfg_netdev - Setup the netdev flags
2323 * @vsi: the VSI being configured
2325 * Returns 0 on success, negative value on failure
2327 static int ice_cfg_netdev(struct ice_vsi *vsi)
2329 netdev_features_t csumo_features;
2330 netdev_features_t vlano_features;
2331 netdev_features_t dflt_features;
2332 netdev_features_t tso_features;
2333 struct ice_netdev_priv *np;
2334 struct net_device *netdev;
2335 u8 mac_addr[ETH_ALEN];
2337 netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
2338 vsi->alloc_txq, vsi->alloc_rxq);
2342 vsi->netdev = netdev;
2343 np = netdev_priv(netdev);
2346 dflt_features = NETIF_F_SG |
2350 csumo_features = NETIF_F_RXCSUM |
2354 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2355 NETIF_F_HW_VLAN_CTAG_TX |
2356 NETIF_F_HW_VLAN_CTAG_RX;
2358 tso_features = NETIF_F_TSO;
2360 /* set features that user can change */
2361 netdev->hw_features = dflt_features | csumo_features |
2362 vlano_features | tso_features;
2364 /* enable features */
2365 netdev->features |= netdev->hw_features;
2366 /* encap and VLAN devices inherit default, csumo and tso features */
2367 netdev->hw_enc_features |= dflt_features | csumo_features |
2369 netdev->vlan_features |= dflt_features | csumo_features |
2372 if (vsi->type == ICE_VSI_PF) {
2373 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
2374 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2376 ether_addr_copy(netdev->dev_addr, mac_addr);
2377 ether_addr_copy(netdev->perm_addr, mac_addr);
2380 netdev->priv_flags |= IFF_UNICAST_FLT;
2382 /* assign netdev_ops */
2383 netdev->netdev_ops = &ice_netdev_ops;
2385 /* setup watchdog timeout value to be 5 second */
2386 netdev->watchdog_timeo = 5 * HZ;
2388 ice_set_ethtool_ops(netdev);
2390 netdev->min_mtu = ETH_MIN_MTU;
2391 netdev->max_mtu = ICE_MAX_MTU;
2397 * ice_vsi_free_arrays - clean up vsi resources
2398 * @vsi: pointer to VSI being cleared
2399 * @free_qvectors: bool to specify if q_vectors should be deallocated
2401 static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
2403 struct ice_pf *pf = vsi->back;
2405 /* free the ring and vector containers */
2406 if (free_qvectors && vsi->q_vectors) {
2407 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
2408 vsi->q_vectors = NULL;
2410 if (vsi->tx_rings) {
2411 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2412 vsi->tx_rings = NULL;
2414 if (vsi->rx_rings) {
2415 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2416 vsi->rx_rings = NULL;
2421 * ice_vsi_clear - clean up and deallocate the provided vsi
2422 * @vsi: pointer to VSI being cleared
2424 * This deallocates the vsi's queue resources, removes it from the PF's
2425 * VSI array if necessary, and deallocates the VSI
2427 * Returns 0 on success, negative on failure
2429 static int ice_vsi_clear(struct ice_vsi *vsi)
2431 struct ice_pf *pf = NULL;
2441 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
2442 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
2447 mutex_lock(&pf->sw_mutex);
2448 /* updates the PF for this cleared vsi */
2450 pf->vsi[vsi->idx] = NULL;
2451 if (vsi->idx < pf->next_vsi)
2452 pf->next_vsi = vsi->idx;
2454 ice_vsi_free_arrays(vsi, true);
2455 mutex_unlock(&pf->sw_mutex);
2456 devm_kfree(&pf->pdev->dev, vsi);
2462 * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
2463 * @vsi: the VSI being configured
2464 * @v_idx: index of the vector in the vsi struct
2466 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2468 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
2470 struct ice_pf *pf = vsi->back;
2471 struct ice_q_vector *q_vector;
2473 /* allocate q_vector */
2474 q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
2478 q_vector->vsi = vsi;
2479 q_vector->v_idx = v_idx;
2480 /* only set affinity_mask if the CPU is online */
2481 if (cpu_online(v_idx))
2482 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
2485 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
2487 /* tie q_vector and vsi together */
2488 vsi->q_vectors[v_idx] = q_vector;
2494 * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
2495 * @vsi: the VSI being configured
2497 * We allocate one q_vector per queue interrupt. If allocation fails we
2500 static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
2502 struct ice_pf *pf = vsi->back;
2503 int v_idx = 0, num_q_vectors;
2506 if (vsi->q_vectors[0]) {
2507 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
2512 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2513 num_q_vectors = vsi->num_q_vectors;
2519 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
2520 err = ice_vsi_alloc_q_vector(vsi, v_idx);
2529 ice_free_q_vector(vsi, v_idx);
2531 dev_err(&pf->pdev->dev,
2532 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
2533 vsi->num_q_vectors, vsi->vsi_num, err);
2534 vsi->num_q_vectors = 0;
2539 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
2540 * @vsi: ptr to the VSI
2542 * This should only be called after ice_vsi_alloc() which allocates the
2543 * corresponding SW VSI structure and initializes num_queue_pairs for the
2544 * newly allocated VSI.
2546 * Returns 0 on success or negative on failure
2548 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
2550 struct ice_pf *pf = vsi->back;
2551 int num_q_vectors = 0;
2553 if (vsi->base_vector) {
2554 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
2555 vsi->vsi_num, vsi->base_vector);
2559 if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2562 switch (vsi->type) {
2564 num_q_vectors = vsi->num_q_vectors;
2567 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2573 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
2574 num_q_vectors, vsi->idx);
2576 if (vsi->base_vector < 0) {
2577 dev_err(&pf->pdev->dev,
2578 "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
2579 num_q_vectors, vsi->vsi_num, vsi->base_vector);
2587 * ice_fill_rss_lut - Fill the RSS lookup table with default values
2588 * @lut: Lookup table
2589 * @rss_table_size: Lookup table size
2590 * @rss_size: Range of queue number for hashing
2592 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2596 for (i = 0; i < rss_table_size; i++)
2597 lut[i] = i % rss_size;
2601 * ice_vsi_cfg_rss - Configure RSS params for a VSI
2602 * @vsi: VSI to be configured
2604 static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
2606 u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
2607 struct ice_aqc_get_set_rss_keys *key;
2608 struct ice_pf *pf = vsi->back;
2609 enum ice_status status;
2613 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
2615 lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2619 if (vsi->rss_lut_user)
2620 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
2622 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
2624 status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
2625 lut, vsi->rss_table_size);
2628 dev_err(&vsi->back->pdev->dev,
2629 "set_rss_lut failed, error %d\n", status);
2631 goto ice_vsi_cfg_rss_exit;
2634 key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
2637 goto ice_vsi_cfg_rss_exit;
2640 if (vsi->rss_hkey_user)
2641 memcpy(seed, vsi->rss_hkey_user,
2642 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2644 netdev_rss_key_fill((void *)seed,
2645 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2646 memcpy(&key->standard_rss_key, seed,
2647 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2649 status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
2652 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
2657 devm_kfree(&pf->pdev->dev, key);
2658 ice_vsi_cfg_rss_exit:
2659 devm_kfree(&pf->pdev->dev, lut);
2664 * ice_vsi_rebuild - Rebuild VSI after reset
2665 * @vsi: vsi to be rebuild
2667 * Returns 0 on success and negative value on failure
2669 static int ice_vsi_rebuild(struct ice_vsi *vsi)
2671 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2677 ice_vsi_free_q_vectors(vsi);
2678 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2679 vsi->base_vector = 0;
2680 ice_vsi_clear_rings(vsi);
2681 ice_vsi_free_arrays(vsi, false);
2682 ice_vsi_set_num_qs(vsi);
2684 /* Initialize VSI struct elements and create VSI in FW */
2685 ret = ice_vsi_init(vsi);
2689 ret = ice_vsi_alloc_arrays(vsi, false);
2693 switch (vsi->type) {
2696 ret = ice_vsi_alloc_q_vectors(vsi);
2700 ret = ice_vsi_setup_vector_base(vsi);
2704 ret = ice_vsi_alloc_rings(vsi);
2708 ice_vsi_map_rings_to_vectors(vsi);
2714 ice_vsi_set_tc_cfg(vsi);
2716 /* configure VSI nodes based on number of queues and TC's */
2717 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2718 max_txqs[i] = vsi->num_txq;
2720 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2721 vsi->tc_cfg.ena_tc, max_txqs);
2723 dev_info(&vsi->back->pdev->dev,
2724 "Failed VSI lan queue config\n");
2730 ice_vsi_free_q_vectors(vsi);
2733 vsi->current_netdev_flags = 0;
2734 unregister_netdev(vsi->netdev);
2735 free_netdev(vsi->netdev);
2740 set_bit(__ICE_RESET_FAILED, vsi->back->state);
2745 * ice_vsi_setup - Set up a VSI by a given type
2746 * @pf: board private structure
2747 * @pi: pointer to the port_info instance
2749 * @vf_id: defines VF id to which this VSI connects. This field is meant to be
2750 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2751 * fill-in ICE_INVAL_VFID as input.
2753 * This allocates the sw VSI structure and its queue resources.
2755 * Returns pointer to the successfully allocated and configured VSI sw struct on
2756 * success, NULL on failure.
2758 static struct ice_vsi *
2759 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2760 enum ice_vsi_type type, u16 __always_unused vf_id)
2762 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2763 struct device *dev = &pf->pdev->dev;
2764 struct ice_vsi *vsi;
2767 vsi = ice_vsi_alloc(pf, type);
2769 dev_err(dev, "could not allocate VSI\n");
2773 vsi->port_info = pi;
2774 vsi->vsw = pf->first_sw;
2776 if (ice_vsi_get_qs(vsi)) {
2777 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2782 /* set RSS capabilities */
2783 ice_vsi_set_rss_params(vsi);
2785 /* create the VSI */
2786 ret = ice_vsi_init(vsi);
2790 switch (vsi->type) {
2792 ret = ice_cfg_netdev(vsi);
2794 goto err_cfg_netdev;
2796 ret = register_netdev(vsi->netdev);
2798 goto err_register_netdev;
2800 netif_carrier_off(vsi->netdev);
2802 /* make sure transmit queues start off as stopped */
2803 netif_tx_stop_all_queues(vsi->netdev);
2804 ret = ice_vsi_alloc_q_vectors(vsi);
2808 ret = ice_vsi_setup_vector_base(vsi);
2812 ret = ice_vsi_alloc_rings(vsi);
2816 ice_vsi_map_rings_to_vectors(vsi);
2818 /* Do not exit if configuring RSS had an issue, at least
2819 * receive traffic on first queue. Hence no need to capture
2822 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2823 ice_vsi_cfg_rss(vsi);
2826 /* if vsi type is not recognized, clean up the resources and
2832 ice_vsi_set_tc_cfg(vsi);
2834 /* configure VSI nodes based on number of queues and TC's */
2835 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2836 max_txqs[i] = vsi->num_txq;
2838 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2839 vsi->tc_cfg.ena_tc, max_txqs);
2841 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
2848 ice_vsi_free_q_vectors(vsi);
2850 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
2851 unregister_netdev(vsi->netdev);
2852 err_register_netdev:
2854 free_netdev(vsi->netdev);
2858 ice_vsi_delete(vsi);
2860 ice_vsi_put_qs(vsi);
2862 pf->q_left_tx += vsi->alloc_txq;
2863 pf->q_left_rx += vsi->alloc_rxq;
2870 * ice_pf_vsi_setup - Set up a PF VSI
2871 * @pf: board private structure
2872 * @pi: pointer to the port_info instance
2874 * Returns pointer to the successfully allocated VSI sw struct on success,
2875 * otherwise returns NULL on failure.
2877 static struct ice_vsi *
2878 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2880 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
2884 * ice_vsi_add_vlan - Add vsi membership for given vlan
2885 * @vsi: the vsi being configured
2886 * @vid: vlan id to be added
2888 static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
2890 struct ice_fltr_list_entry *tmp;
2891 struct ice_pf *pf = vsi->back;
2892 LIST_HEAD(tmp_add_list);
2893 enum ice_status status;
2896 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
2900 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2901 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2902 tmp->fltr_info.flag = ICE_FLTR_TX;
2903 tmp->fltr_info.src = vsi->vsi_num;
2904 tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2905 tmp->fltr_info.l_data.vlan.vlan_id = vid;
2907 INIT_LIST_HEAD(&tmp->list_entry);
2908 list_add(&tmp->list_entry, &tmp_add_list);
2910 status = ice_add_vlan(&pf->hw, &tmp_add_list);
2913 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
2917 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2922 * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
2923 * @netdev: network interface to be adjusted
2924 * @proto: unused protocol
2925 * @vid: vlan id to be added
2927 * net_device_ops implementation for adding vlan ids
2929 static int ice_vlan_rx_add_vid(struct net_device *netdev,
2930 __always_unused __be16 proto, u16 vid)
2932 struct ice_netdev_priv *np = netdev_priv(netdev);
2933 struct ice_vsi *vsi = np->vsi;
2936 if (vid >= VLAN_N_VID) {
2937 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2945 /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
2946 * needed to continue allowing all untagged packets since VLAN prune
2947 * list is applied to all packets by the switch
2949 ret = ice_vsi_add_vlan(vsi, vid);
2952 set_bit(vid, vsi->active_vlans);
2958 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
2959 * @vsi: the VSI being configured
2960 * @vid: VLAN id to be removed
2962 static void ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
2964 struct ice_fltr_list_entry *list;
2965 struct ice_pf *pf = vsi->back;
2966 LIST_HEAD(tmp_add_list);
2968 list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
2972 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2973 list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2974 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2975 list->fltr_info.l_data.vlan.vlan_id = vid;
2976 list->fltr_info.flag = ICE_FLTR_TX;
2977 list->fltr_info.src = vsi->vsi_num;
2979 INIT_LIST_HEAD(&list->list_entry);
2980 list_add(&list->list_entry, &tmp_add_list);
2982 if (ice_remove_vlan(&pf->hw, &tmp_add_list))
2983 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
2986 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2990 * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2991 * @netdev: network interface to be adjusted
2992 * @proto: unused protocol
2993 * @vid: vlan id to be removed
2995 * net_device_ops implementation for removing vlan ids
2997 static int ice_vlan_rx_kill_vid(struct net_device *netdev,
2998 __always_unused __be16 proto, u16 vid)
3000 struct ice_netdev_priv *np = netdev_priv(netdev);
3001 struct ice_vsi *vsi = np->vsi;
3006 /* return code is ignored as there is nothing a user
3007 * can do about failure to remove and a log message was
3008 * already printed from the other function
3010 ice_vsi_kill_vlan(vsi, vid);
3012 clear_bit(vid, vsi->active_vlans);
3018 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3019 * @pf: board private structure
3021 * Returns 0 on success, negative value on failure
3023 static int ice_setup_pf_sw(struct ice_pf *pf)
3025 LIST_HEAD(tmp_add_list);
3026 u8 broadcast[ETH_ALEN];
3027 struct ice_vsi *vsi;
3030 if (ice_is_reset_recovery_pending(pf->state))
3033 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3036 goto unroll_vsi_setup;
3039 /* To add a MAC filter, first add the MAC to a list and then
3040 * pass the list to ice_add_mac.
3043 /* Add a unicast MAC filter so the VSI can get its packets */
3044 status = ice_add_mac_to_list(vsi, &tmp_add_list,
3045 vsi->port_info->mac.perm_addr);
3047 goto unroll_vsi_setup;
3049 /* VSI needs to receive broadcast traffic, so add the broadcast
3050 * MAC address to the list as well.
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);
3073 ice_vsi_free_q_vectors(vsi);
3074 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3075 unregister_netdev(vsi->netdev);
3077 free_netdev(vsi->netdev);
3081 ice_vsi_delete(vsi);
3082 ice_vsi_put_qs(vsi);
3083 pf->q_left_tx += vsi->alloc_txq;
3084 pf->q_left_rx += vsi->alloc_rxq;
3091 * ice_determine_q_usage - Calculate queue distribution
3092 * @pf: board private structure
3094 * Return -ENOMEM if we don't get enough queues for all ports
3096 static void ice_determine_q_usage(struct ice_pf *pf)
3098 u16 q_left_tx, q_left_rx;
3100 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
3101 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
3103 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
3105 /* only 1 rx queue unless RSS is enabled */
3106 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3109 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
3111 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
3112 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
3116 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3117 * @pf: board private structure to initialize
3119 static void ice_deinit_pf(struct ice_pf *pf)
3121 if (pf->serv_tmr.function)
3122 del_timer_sync(&pf->serv_tmr);
3123 if (pf->serv_task.func)
3124 cancel_work_sync(&pf->serv_task);
3125 mutex_destroy(&pf->sw_mutex);
3126 mutex_destroy(&pf->avail_q_mutex);
3130 * ice_init_pf - Initialize general software structures (struct ice_pf)
3131 * @pf: board private structure to initialize
3133 static void ice_init_pf(struct ice_pf *pf)
3135 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
3136 set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3138 mutex_init(&pf->sw_mutex);
3139 mutex_init(&pf->avail_q_mutex);
3141 /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
3142 mutex_lock(&pf->avail_q_mutex);
3143 bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
3144 bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
3145 mutex_unlock(&pf->avail_q_mutex);
3147 if (pf->hw.func_caps.common_cap.rss_table_size)
3148 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3150 /* setup service timer and periodic service task */
3151 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3152 pf->serv_tmr_period = HZ;
3153 INIT_WORK(&pf->serv_task, ice_service_task);
3154 clear_bit(__ICE_SERVICE_SCHED, pf->state);
3158 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3159 * @pf: board private structure
3161 * compute the number of MSIX vectors required (v_budget) and request from
3162 * the OS. Return the number of vectors reserved or negative on failure
3164 static int ice_ena_msix_range(struct ice_pf *pf)
3166 int v_left, v_actual, v_budget = 0;
3169 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3171 /* reserve one vector for miscellaneous handler */
3176 /* reserve vectors for LAN traffic */
3177 pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
3178 v_budget += pf->num_lan_msix;
3180 pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
3181 sizeof(struct msix_entry), GFP_KERNEL);
3183 if (!pf->msix_entries) {
3188 for (i = 0; i < v_budget; i++)
3189 pf->msix_entries[i].entry = i;
3191 /* actually reserve the vectors */
3192 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3193 ICE_MIN_MSIX, v_budget);
3196 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
3201 if (v_actual < v_budget) {
3202 dev_warn(&pf->pdev->dev,
3203 "not enough vectors. requested = %d, obtained = %d\n",
3204 v_budget, v_actual);
3205 if (v_actual >= (pf->num_lan_msix + 1)) {
3206 pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
3207 } else if (v_actual >= 2) {
3208 pf->num_lan_msix = 1;
3209 pf->num_avail_msix = v_actual - 2;
3211 pci_disable_msix(pf->pdev);
3220 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3224 pf->num_lan_msix = 0;
3225 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3230 * ice_dis_msix - Disable MSI-X interrupt setup in OS
3231 * @pf: board private structure
3233 static void ice_dis_msix(struct ice_pf *pf)
3235 pci_disable_msix(pf->pdev);
3236 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3237 pf->msix_entries = NULL;
3238 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3242 * ice_init_interrupt_scheme - Determine proper interrupt scheme
3243 * @pf: board private structure to initialize
3245 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3250 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3251 vectors = ice_ena_msix_range(pf);
3258 /* set up vector assignment tracking */
3259 size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
3261 pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
3262 if (!pf->irq_tracker) {
3267 pf->irq_tracker->num_entries = vectors;
3273 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3274 * @pf: board private structure
3276 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3278 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3281 if (pf->irq_tracker) {
3282 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
3283 pf->irq_tracker = NULL;
3288 * ice_probe - Device initialization routine
3289 * @pdev: PCI device information struct
3290 * @ent: entry in ice_pci_tbl
3292 * Returns 0 on success, negative on failure
3294 static int ice_probe(struct pci_dev *pdev,
3295 const struct pci_device_id __always_unused *ent)
3301 /* this driver uses devres, see Documentation/driver-model/devres.txt */
3302 err = pcim_enable_device(pdev);
3306 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3308 dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
3312 pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
3316 /* set up for high or low dma */
3317 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3319 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3321 dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3325 pci_enable_pcie_error_reporting(pdev);
3326 pci_set_master(pdev);
3329 pci_set_drvdata(pdev, pf);
3330 set_bit(__ICE_DOWN, pf->state);
3333 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3335 hw->vendor_id = pdev->vendor;
3336 hw->device_id = pdev->device;
3337 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3338 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3339 hw->subsystem_device_id = pdev->subsystem_device;
3340 hw->bus.device = PCI_SLOT(pdev->devfn);
3341 hw->bus.func = PCI_FUNC(pdev->devfn);
3342 ice_set_ctrlq_len(hw);
3344 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3346 #ifndef CONFIG_DYNAMIC_DEBUG
3348 hw->debug_mask = debug;
3351 err = ice_init_hw(hw);
3353 dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
3355 goto err_exit_unroll;
3358 dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
3359 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
3360 hw->api_maj_ver, hw->api_min_ver);
3364 ice_determine_q_usage(pf);
3366 pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
3367 hw->func_caps.guaranteed_num_vsi);
3368 if (!pf->num_alloc_vsi) {
3370 goto err_init_pf_unroll;
3373 pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
3374 sizeof(struct ice_vsi *), GFP_KERNEL);
3377 goto err_init_pf_unroll;
3380 err = ice_init_interrupt_scheme(pf);
3383 "ice_init_interrupt_scheme failed: %d\n", err);
3385 goto err_init_interrupt_unroll;
3388 /* In case of MSIX we are going to setup the misc vector right here
3389 * to handle admin queue events etc. In case of legacy and MSI
3390 * the misc functionality and queue processing is combined in
3391 * the same vector and that gets setup at open.
3393 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3394 err = ice_req_irq_msix_misc(pf);
3397 "setup of misc vector failed: %d\n", err);
3398 goto err_init_interrupt_unroll;
3402 /* create switch struct for the switch element created by FW on boot */
3403 pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
3405 if (!pf->first_sw) {
3407 goto err_msix_misc_unroll;
3410 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3411 pf->first_sw->pf = pf;
3413 /* record the sw_id available for later use */
3414 pf->first_sw->sw_id = hw->port_info->sw_id;
3416 err = ice_setup_pf_sw(pf);
3419 "probe failed due to setup pf switch:%d\n", err);
3420 goto err_alloc_sw_unroll;
3423 /* Driver is mostly up */
3424 clear_bit(__ICE_DOWN, pf->state);
3426 /* since everything is good, start the service timer */
3427 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3429 err = ice_init_link_events(pf->hw.port_info);
3431 dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
3432 goto err_alloc_sw_unroll;
3437 err_alloc_sw_unroll:
3438 set_bit(__ICE_DOWN, pf->state);
3439 devm_kfree(&pf->pdev->dev, pf->first_sw);
3440 err_msix_misc_unroll:
3441 ice_free_irq_msix_misc(pf);
3442 err_init_interrupt_unroll:
3443 ice_clear_interrupt_scheme(pf);
3444 devm_kfree(&pdev->dev, pf->vsi);
3449 pci_disable_pcie_error_reporting(pdev);
3454 * ice_remove - Device removal routine
3455 * @pdev: PCI device information struct
3457 static void ice_remove(struct pci_dev *pdev)
3459 struct ice_pf *pf = pci_get_drvdata(pdev);
3464 set_bit(__ICE_DOWN, pf->state);
3466 ice_vsi_release_all(pf);
3467 ice_free_irq_msix_misc(pf);
3468 ice_clear_interrupt_scheme(pf);
3470 ice_deinit_hw(&pf->hw);
3471 pci_disable_pcie_error_reporting(pdev);
3474 /* ice_pci_tbl - PCI Device ID Table
3476 * Wildcard entries (PCI_ANY_ID) should come last
3477 * Last entry must be all 0s
3479 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3480 * Class, Class Mask, private data (not used) }
3482 static const struct pci_device_id ice_pci_tbl[] = {
3483 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
3484 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
3485 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
3486 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
3487 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
3488 /* required last entry */
3491 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3493 static struct pci_driver ice_driver = {
3494 .name = KBUILD_MODNAME,
3495 .id_table = ice_pci_tbl,
3497 .remove = ice_remove,
3501 * ice_module_init - Driver registration routine
3503 * ice_module_init is the first routine called when the driver is
3504 * loaded. All it does is register with the PCI subsystem.
3506 static int __init ice_module_init(void)
3510 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3511 pr_info("%s\n", ice_copyright);
3513 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
3515 pr_err("Failed to create workqueue\n");
3519 status = pci_register_driver(&ice_driver);
3521 pr_err("failed to register pci driver, err %d\n", status);
3522 destroy_workqueue(ice_wq);
3527 module_init(ice_module_init);
3530 * ice_module_exit - Driver exit cleanup routine
3532 * ice_module_exit is called just before the driver is removed
3535 static void __exit ice_module_exit(void)
3537 pci_unregister_driver(&ice_driver);
3538 destroy_workqueue(ice_wq);
3539 pr_info("module unloaded\n");
3541 module_exit(ice_module_exit);
3544 * ice_set_mac_address - NDO callback to set mac address
3545 * @netdev: network interface device structure
3546 * @pi: pointer to an address structure
3548 * Returns 0 on success, negative on failure
3550 static int ice_set_mac_address(struct net_device *netdev, void *pi)
3552 struct ice_netdev_priv *np = netdev_priv(netdev);
3553 struct ice_vsi *vsi = np->vsi;
3554 struct ice_pf *pf = vsi->back;
3555 struct ice_hw *hw = &pf->hw;
3556 struct sockaddr *addr = pi;
3557 enum ice_status status;
3558 LIST_HEAD(a_mac_list);
3559 LIST_HEAD(r_mac_list);
3564 mac = (u8 *)addr->sa_data;
3566 if (!is_valid_ether_addr(mac))
3567 return -EADDRNOTAVAIL;
3569 if (ether_addr_equal(netdev->dev_addr, mac)) {
3570 netdev_warn(netdev, "already using mac %pM\n", mac);
3574 if (test_bit(__ICE_DOWN, pf->state) ||
3575 ice_is_reset_recovery_pending(pf->state)) {
3576 netdev_err(netdev, "can't set mac %pM. device not ready\n",
3581 /* When we change the mac address we also have to change the mac address
3582 * based filter rules that were created previously for the old mac
3583 * address. So first, we remove the old filter rule using ice_remove_mac
3584 * and then create a new filter rule using ice_add_mac. Note that for
3585 * both these operations, we first need to form a "list" of mac
3586 * addresses (even though in this case, we have only 1 mac address to be
3587 * added/removed) and this done using ice_add_mac_to_list. Depending on
3588 * the ensuing operation this "list" of mac addresses is either to be
3589 * added or removed from the filter.
3591 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
3593 err = -EADDRNOTAVAIL;
3597 status = ice_remove_mac(hw, &r_mac_list);
3599 err = -EADDRNOTAVAIL;
3603 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
3605 err = -EADDRNOTAVAIL;
3609 status = ice_add_mac(hw, &a_mac_list);
3611 err = -EADDRNOTAVAIL;
3616 /* free list entries */
3617 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
3618 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
3621 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
3626 /* change the netdev's mac address */
3627 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3628 netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
3631 /* write new mac address to the firmware */
3632 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3633 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3635 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
3642 * ice_set_rx_mode - NDO callback to set the netdev filters
3643 * @netdev: network interface device structure
3645 static void ice_set_rx_mode(struct net_device *netdev)
3647 struct ice_netdev_priv *np = netdev_priv(netdev);
3648 struct ice_vsi *vsi = np->vsi;
3653 /* Set the flags to synchronize filters
3654 * ndo_set_rx_mode may be triggered even without a change in netdev
3657 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3658 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3659 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3661 /* schedule our worker thread which will take care of
3662 * applying the new filter changes
3664 ice_service_task_schedule(vsi->back);
3668 * ice_fdb_add - add an entry to the hardware database
3669 * @ndm: the input from the stack
3670 * @tb: pointer to array of nladdr (unused)
3671 * @dev: the net device pointer
3672 * @addr: the MAC address entry being added
3674 * @flags: instructions from stack about fdb operation
3676 static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3677 struct net_device *dev, const unsigned char *addr,
3683 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3686 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3687 netdev_err(dev, "FDB only supports static addresses\n");
3691 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3692 err = dev_uc_add_excl(dev, addr);
3693 else if (is_multicast_ether_addr(addr))
3694 err = dev_mc_add_excl(dev, addr);
3698 /* Only return duplicate errors if NLM_F_EXCL is set */
3699 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3706 * ice_fdb_del - delete an entry from the hardware database
3707 * @ndm: the input from the stack
3708 * @tb: pointer to array of nladdr (unused)
3709 * @dev: the net device pointer
3710 * @addr: the MAC address entry being added
3713 static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
3714 struct net_device *dev, const unsigned char *addr,
3715 __always_unused u16 vid)
3719 if (ndm->ndm_state & NUD_PERMANENT) {
3720 netdev_err(dev, "FDB only supports static addresses\n");
3724 if (is_unicast_ether_addr(addr))
3725 err = dev_uc_del(dev, addr);
3726 else if (is_multicast_ether_addr(addr))
3727 err = dev_mc_del(dev, addr);
3735 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
3736 * @vsi: the vsi being changed
3738 static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
3740 struct device *dev = &vsi->back->pdev->dev;
3741 struct ice_hw *hw = &vsi->back->hw;
3742 struct ice_vsi_ctx ctxt = { 0 };
3743 enum ice_status status;
3745 /* Here we are configuring the VSI to let the driver add VLAN tags by
3746 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
3747 * insertion happens in the Tx hot path, in ice_tx_map.
3749 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
3751 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3752 ctxt.vsi_num = vsi->vsi_num;
3754 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3756 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
3757 status, hw->adminq.sq_last_status);
3761 vsi->info.vlan_flags = ctxt.info.vlan_flags;
3766 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
3767 * @vsi: the vsi being changed
3768 * @ena: boolean value indicating if this is a enable or disable request
3770 static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
3772 struct device *dev = &vsi->back->pdev->dev;
3773 struct ice_hw *hw = &vsi->back->hw;
3774 struct ice_vsi_ctx ctxt = { 0 };
3775 enum ice_status status;
3777 /* Here we are configuring what the VSI should do with the VLAN tag in
3778 * the Rx packet. We can either leave the tag in the packet or put it in
3779 * the Rx descriptor.
3782 /* Strip VLAN tag from Rx packet and put it in the desc */
3783 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
3785 /* Disable stripping. Leave tag in packet */
3786 ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3789 /* Allow all packets untagged/tagged */
3790 ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
3792 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3793 ctxt.vsi_num = vsi->vsi_num;
3795 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3797 dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
3798 ena, status, hw->adminq.sq_last_status);
3802 vsi->info.vlan_flags = ctxt.info.vlan_flags;
3807 * ice_set_features - set the netdev feature flags
3808 * @netdev: ptr to the netdev being adjusted
3809 * @features: the feature set that the stack is suggesting
3811 static int ice_set_features(struct net_device *netdev,
3812 netdev_features_t features)
3814 struct ice_netdev_priv *np = netdev_priv(netdev);
3815 struct ice_vsi *vsi = np->vsi;
3818 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3819 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3820 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3821 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3822 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3823 ret = ice_vsi_manage_vlan_stripping(vsi, false);
3824 else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3825 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3826 ret = ice_vsi_manage_vlan_insertion(vsi);
3827 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3828 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3829 ret = ice_vsi_manage_vlan_insertion(vsi);
3835 * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
3836 * @vsi: VSI to setup vlan properties for
3838 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3842 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3843 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3844 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3845 ret = ice_vsi_manage_vlan_insertion(vsi);
3851 * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
3852 * @vsi: the VSI being brought back up
3854 static int ice_restore_vlan(struct ice_vsi *vsi)
3862 err = ice_vsi_vlan_setup(vsi);
3866 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
3867 err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
3876 * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
3877 * @ring: The Tx ring to configure
3878 * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
3879 * @pf_q: queue index in the PF space
3881 * Configure the Tx descriptor ring in TLAN context.
3884 ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
3886 struct ice_vsi *vsi = ring->vsi;
3887 struct ice_hw *hw = &vsi->back->hw;
3889 tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
3891 tlan_ctx->port_num = vsi->port_info->lport;
3893 /* Transmit Queue Length */
3894 tlan_ctx->qlen = ring->count;
3897 tlan_ctx->pf_num = hw->pf_id;
3899 /* queue belongs to a specific VSI type
3900 * VF / VM index should be programmed per vmvf_type setting:
3901 * for vmvf_type = VF, it is VF number between 0-256
3902 * for vmvf_type = VM, it is VM number between 0-767
3903 * for PF or EMP this field should be set to zero
3905 switch (vsi->type) {
3907 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
3913 /* make sure the context is associated with the right VSI */
3914 tlan_ctx->src_vsi = vsi->vsi_num;
3916 tlan_ctx->tso_ena = ICE_TX_LEGACY;
3917 tlan_ctx->tso_qnum = pf_q;
3919 /* Legacy or Advanced Host Interface:
3920 * 0: Advanced Host Interface
3921 * 1: Legacy Host Interface
3923 tlan_ctx->legacy_int = ICE_TX_LEGACY;
3927 * ice_vsi_cfg_txqs - Configure the VSI for Tx
3928 * @vsi: the VSI being configured
3930 * Return 0 on success and a negative value on error
3931 * Configure the Tx VSI for operation.
3933 static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
3935 struct ice_aqc_add_tx_qgrp *qg_buf;
3936 struct ice_aqc_add_txqs_perq *txq;
3937 struct ice_pf *pf = vsi->back;
3938 enum ice_status status;
3939 u16 buf_len, i, pf_q;
3940 int err = 0, tc = 0;
3943 buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
3944 qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
3948 if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
3952 qg_buf->num_txqs = 1;
3955 /* set up and configure the tx queues */
3956 ice_for_each_txq(vsi, i) {
3957 struct ice_tlan_ctx tlan_ctx = { 0 };
3959 pf_q = vsi->txq_map[i];
3960 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
3961 /* copy context contents into the qg_buf */
3962 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
3963 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
3966 /* init queue specific tail reg. It is referred as transmit
3967 * comm scheduler queue doorbell.
3969 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
3970 status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
3971 num_q_grps, qg_buf, buf_len, NULL);
3973 dev_err(&vsi->back->pdev->dev,
3974 "Failed to set LAN Tx queue context, error: %d\n",
3980 /* Add Tx Queue TEID into the VSI tx ring from the response
3981 * This will complete configuring and enabling the queue.
3983 txq = &qg_buf->txqs[0];
3984 if (pf_q == le16_to_cpu(txq->txq_id))
3985 vsi->tx_rings[i]->txq_teid =
3986 le32_to_cpu(txq->q_teid);
3989 devm_kfree(&pf->pdev->dev, qg_buf);
3994 * ice_setup_rx_ctx - Configure a receive ring context
3995 * @ring: The Rx ring to configure
3997 * Configure the Rx descriptor ring in RLAN context.
3999 static int ice_setup_rx_ctx(struct ice_ring *ring)
4001 struct ice_vsi *vsi = ring->vsi;
4002 struct ice_hw *hw = &vsi->back->hw;
4003 u32 rxdid = ICE_RXDID_FLEX_NIC;
4004 struct ice_rlan_ctx rlan_ctx;
4009 /* what is RX queue number in global space of 2K rx queues */
4010 pf_q = vsi->rxq_map[ring->q_index];
4012 /* clear the context structure first */
4013 memset(&rlan_ctx, 0, sizeof(rlan_ctx));
4015 rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
4017 rlan_ctx.qlen = ring->count;
4019 /* Receive Packet Data Buffer Size.
4020 * The Packet Data Buffer Size is defined in 128 byte units.
4022 rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
4024 /* use 32 byte descriptors */
4027 /* Strip the Ethernet CRC bytes before the packet is posted to host
4030 rlan_ctx.crcstrip = 1;
4032 /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
4033 rlan_ctx.l2tsel = 1;
4035 rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
4036 rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
4037 rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
4039 /* This controls whether VLAN is stripped from inner headers
4040 * The VLAN in the inner L2 header is stripped to the receive
4041 * descriptor if enabled by this flag.
4043 rlan_ctx.showiv = 0;
4045 /* Max packet size for this queue - must not be set to a larger value
4048 rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
4049 ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
4051 /* Rx queue threshold in units of 64 */
4052 rlan_ctx.lrxqthresh = 1;
4054 /* Enable Flexible Descriptors in the queue context which
4055 * allows this driver to select a specific receive descriptor format
4057 regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
4058 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
4059 QRXFLXP_CNTXT_RXDID_IDX_M;
4061 /* increasing context priority to pick up profile id;
4062 * default is 0x01; setting to 0x03 to ensure profile
4063 * is programming if prev context is of same priority
4065 regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
4066 QRXFLXP_CNTXT_RXDID_PRIO_M;
4068 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
4070 /* Absolute queue number out of 2K needs to be passed */
4071 err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
4073 dev_err(&vsi->back->pdev->dev,
4074 "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
4079 /* init queue specific tail register */
4080 ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
4081 writel(0, ring->tail);
4082 ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
4088 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
4089 * @vsi: the VSI being configured
4091 * Return 0 on success and a negative value on error
4092 * Configure the Rx VSI for operation.
4094 static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
4099 if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
4100 vsi->max_frame = vsi->netdev->mtu +
4101 ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4103 vsi->max_frame = ICE_RXBUF_2048;
4105 vsi->rx_buf_len = ICE_RXBUF_2048;
4106 /* set up individual rings */
4107 for (i = 0; i < vsi->num_rxq && !err; i++)
4108 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
4111 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
4118 * ice_vsi_cfg - Setup the VSI
4119 * @vsi: the VSI being configured
4121 * Return 0 on success and negative value on error
4123 static int ice_vsi_cfg(struct ice_vsi *vsi)
4128 ice_set_rx_mode(vsi->netdev);
4129 err = ice_restore_vlan(vsi);
4134 err = ice_vsi_cfg_txqs(vsi);
4136 err = ice_vsi_cfg_rxqs(vsi);
4142 * ice_vsi_stop_tx_rings - Disable Tx rings
4143 * @vsi: the VSI being configured
4145 static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
4147 struct ice_pf *pf = vsi->back;
4148 struct ice_hw *hw = &pf->hw;
4149 enum ice_status status;
4154 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
4157 q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
4162 q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
4166 goto err_alloc_q_ids;
4169 /* set up the tx queue list to be disabled */
4170 ice_for_each_txq(vsi, i) {
4173 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
4178 q_ids[i] = vsi->txq_map[i];
4179 q_teids[i] = vsi->tx_rings[i]->txq_teid;
4181 /* clear cause_ena bit for disabled queues */
4182 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4183 val &= ~QINT_TQCTL_CAUSE_ENA_M;
4184 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4186 /* software is expected to wait for 100 ns */
4189 /* trigger a software interrupt for the vector associated to
4190 * the queue to schedule napi handler
4192 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
4193 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
4194 GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
4196 status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
4198 /* if the disable queue command was exercised during an active reset
4199 * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
4200 * the reset operation disables queues at the hardware level anyway.
4202 if (status == ICE_ERR_RESET_ONGOING) {
4203 dev_dbg(&pf->pdev->dev,
4204 "Reset in progress. LAN Tx queues already disabled\n");
4205 } else if (status) {
4206 dev_err(&pf->pdev->dev,
4207 "Failed to disable LAN Tx queues, error: %d\n",
4213 devm_kfree(&pf->pdev->dev, q_ids);
4216 devm_kfree(&pf->pdev->dev, q_teids);
4222 * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4223 * @pf: the PF being configured
4224 * @pf_q: the PF queue
4225 * @ena: enable or disable state of the queue
4227 * This routine will wait for the given Rx queue of the PF to reach the
4228 * enabled or disabled state.
4229 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4230 * multiple retries; else will return 0 in case of success.
4232 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
4236 for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
4237 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
4239 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4242 usleep_range(10, 20);
4244 if (i >= ICE_Q_WAIT_RETRY_LIMIT)
4251 * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
4252 * @vsi: the VSI being configured
4253 * @ena: start or stop the rx rings
4255 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
4257 struct ice_pf *pf = vsi->back;
4258 struct ice_hw *hw = &pf->hw;
4261 for (i = 0; i < vsi->num_rxq; i++) {
4262 int pf_q = vsi->rxq_map[i];
4265 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
4266 rx_reg = rd32(hw, QRX_CTRL(pf_q));
4267 if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
4268 ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
4270 usleep_range(1000, 2000);
4273 /* Skip if the queue is already in the requested state */
4274 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4277 /* turn on/off the queue */
4279 rx_reg |= QRX_CTRL_QENA_REQ_M;
4281 rx_reg &= ~QRX_CTRL_QENA_REQ_M;
4282 wr32(hw, QRX_CTRL(pf_q), rx_reg);
4284 /* wait for the change to finish */
4285 ret = ice_pf_rxq_wait(pf, pf_q, ena);
4287 dev_err(&pf->pdev->dev,
4288 "VSI idx %d Rx ring %d %sable timeout\n",
4289 vsi->idx, pf_q, (ena ? "en" : "dis"));
4298 * ice_vsi_start_rx_rings - start VSI's rx rings
4299 * @vsi: the VSI whose rings are to be started
4301 * Returns 0 on success and a negative value on error
4303 static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
4305 return ice_vsi_ctrl_rx_rings(vsi, true);
4309 * ice_vsi_stop_rx_rings - stop VSI's rx rings
4312 * Returns 0 on success and a negative value on error
4314 static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
4316 return ice_vsi_ctrl_rx_rings(vsi, false);
4320 * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
4322 * Returns 0 on success and a negative value on error
4324 static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
4328 err_tx = ice_vsi_stop_tx_rings(vsi);
4330 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
4332 err_rx = ice_vsi_stop_rx_rings(vsi);
4334 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
4336 if (err_tx || err_rx)
4343 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4344 * @vsi: the VSI being configured
4346 static void ice_napi_enable_all(struct ice_vsi *vsi)
4353 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4354 napi_enable(&vsi->q_vectors[q_idx]->napi);
4358 * ice_up_complete - Finish the last steps of bringing up a connection
4359 * @vsi: The VSI being configured
4361 * Return 0 on success and negative value on error
4363 static int ice_up_complete(struct ice_vsi *vsi)
4365 struct ice_pf *pf = vsi->back;
4368 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4369 ice_vsi_cfg_msix(vsi);
4373 /* Enable only Rx rings, Tx rings were enabled by the FW when the
4374 * Tx queue group list was configured and the context bits were
4375 * programmed using ice_vsi_cfg_txqs
4377 err = ice_vsi_start_rx_rings(vsi);
4381 clear_bit(__ICE_DOWN, vsi->state);
4382 ice_napi_enable_all(vsi);
4383 ice_vsi_ena_irq(vsi);
4385 if (vsi->port_info &&
4386 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
4388 ice_print_link_msg(vsi, true);
4389 netif_tx_start_all_queues(vsi->netdev);
4390 netif_carrier_on(vsi->netdev);
4393 ice_service_task_schedule(pf);
4399 * ice_up - Bring the connection back up after being down
4400 * @vsi: VSI being configured
4402 int ice_up(struct ice_vsi *vsi)
4406 err = ice_vsi_cfg(vsi);
4408 err = ice_up_complete(vsi);
4414 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
4415 * @ring: Tx or Rx ring to read stats from
4416 * @pkts: packets stats counter
4417 * @bytes: bytes stats counter
4419 * This function fetches stats from the ring considering the atomic operations
4420 * that needs to be performed to read u64 values in 32 bit machine.
4422 static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
4432 start = u64_stats_fetch_begin_irq(&ring->syncp);
4433 *pkts = ring->stats.pkts;
4434 *bytes = ring->stats.bytes;
4435 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4439 * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4440 * @hw: ptr to the hardware info
4441 * @hireg: high 32 bit HW register to read from
4442 * @loreg: low 32 bit HW register to read from
4443 * @prev_stat_loaded: bool to specify if previous stats are loaded
4444 * @prev_stat: ptr to previous loaded stat value
4445 * @cur_stat: ptr to current stat value
4447 static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
4448 bool prev_stat_loaded, u64 *prev_stat,
4453 new_data = rd32(hw, loreg);
4454 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
4456 /* device stats are not reset at PFR, they likely will not be zeroed
4457 * when the driver starts. So save the first values read and use them as
4458 * offsets to be subtracted from the raw values in order to report stats
4459 * that count from zero.
4461 if (!prev_stat_loaded)
4462 *prev_stat = new_data;
4463 if (likely(new_data >= *prev_stat))
4464 *cur_stat = new_data - *prev_stat;
4466 /* to manage the potential roll-over */
4467 *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
4468 *cur_stat &= 0xFFFFFFFFFFULL;
4472 * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4473 * @hw: ptr to the hardware info
4474 * @reg: HW register to read from
4475 * @prev_stat_loaded: bool to specify if previous stats are loaded
4476 * @prev_stat: ptr to previous loaded stat value
4477 * @cur_stat: ptr to current stat value
4479 static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4480 u64 *prev_stat, u64 *cur_stat)
4484 new_data = rd32(hw, reg);
4486 /* device stats are not reset at PFR, they likely will not be zeroed
4487 * when the driver starts. So save the first values read and use them as
4488 * offsets to be subtracted from the raw values in order to report stats
4489 * that count from zero.
4491 if (!prev_stat_loaded)
4492 *prev_stat = new_data;
4493 if (likely(new_data >= *prev_stat))
4494 *cur_stat = new_data - *prev_stat;
4496 /* to manage the potential roll-over */
4497 *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
4501 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
4502 * @vsi: the VSI to be updated
4504 static void ice_update_eth_stats(struct ice_vsi *vsi)
4506 struct ice_eth_stats *prev_es, *cur_es;
4507 struct ice_hw *hw = &vsi->back->hw;
4508 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
4510 prev_es = &vsi->eth_stats_prev;
4511 cur_es = &vsi->eth_stats;
4513 ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
4514 vsi->stat_offsets_loaded, &prev_es->rx_bytes,
4517 ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
4518 vsi->stat_offsets_loaded, &prev_es->rx_unicast,
4519 &cur_es->rx_unicast);
4521 ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
4522 vsi->stat_offsets_loaded, &prev_es->rx_multicast,
4523 &cur_es->rx_multicast);
4525 ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
4526 vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
4527 &cur_es->rx_broadcast);
4529 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
4530 &prev_es->rx_discards, &cur_es->rx_discards);
4532 ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
4533 vsi->stat_offsets_loaded, &prev_es->tx_bytes,
4536 ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
4537 vsi->stat_offsets_loaded, &prev_es->tx_unicast,
4538 &cur_es->tx_unicast);
4540 ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
4541 vsi->stat_offsets_loaded, &prev_es->tx_multicast,
4542 &cur_es->tx_multicast);
4544 ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
4545 vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
4546 &cur_es->tx_broadcast);
4548 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
4549 &prev_es->tx_errors, &cur_es->tx_errors);
4551 vsi->stat_offsets_loaded = true;
4555 * ice_update_vsi_ring_stats - Update VSI stats counters
4556 * @vsi: the VSI to be updated
4558 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
4560 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
4561 struct ice_ring *ring;
4565 /* reset netdev stats */
4566 vsi_stats->tx_packets = 0;
4567 vsi_stats->tx_bytes = 0;
4568 vsi_stats->rx_packets = 0;
4569 vsi_stats->rx_bytes = 0;
4571 /* reset non-netdev (extended) stats */
4572 vsi->tx_restart = 0;
4574 vsi->tx_linearize = 0;
4575 vsi->rx_buf_failed = 0;
4576 vsi->rx_page_failed = 0;
4580 /* update Tx rings counters */
4581 ice_for_each_txq(vsi, i) {
4582 ring = READ_ONCE(vsi->tx_rings[i]);
4583 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4584 vsi_stats->tx_packets += pkts;
4585 vsi_stats->tx_bytes += bytes;
4586 vsi->tx_restart += ring->tx_stats.restart_q;
4587 vsi->tx_busy += ring->tx_stats.tx_busy;
4588 vsi->tx_linearize += ring->tx_stats.tx_linearize;
4591 /* update Rx rings counters */
4592 ice_for_each_rxq(vsi, i) {
4593 ring = READ_ONCE(vsi->rx_rings[i]);
4594 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4595 vsi_stats->rx_packets += pkts;
4596 vsi_stats->rx_bytes += bytes;
4597 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4598 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4605 * ice_update_vsi_stats - Update VSI stats counters
4606 * @vsi: the VSI to be updated
4608 static void ice_update_vsi_stats(struct ice_vsi *vsi)
4610 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4611 struct ice_eth_stats *cur_es = &vsi->eth_stats;
4612 struct ice_pf *pf = vsi->back;
4614 if (test_bit(__ICE_DOWN, vsi->state) ||
4615 test_bit(__ICE_CFG_BUSY, pf->state))
4618 /* get stats as recorded by Tx/Rx rings */
4619 ice_update_vsi_ring_stats(vsi);
4621 /* get VSI stats as recorded by the hardware */
4622 ice_update_eth_stats(vsi);
4624 cur_ns->tx_errors = cur_es->tx_errors;
4625 cur_ns->rx_dropped = cur_es->rx_discards;
4626 cur_ns->tx_dropped = cur_es->tx_discards;
4627 cur_ns->multicast = cur_es->rx_multicast;
4629 /* update some more netdev stats if this is main VSI */
4630 if (vsi->type == ICE_VSI_PF) {
4631 cur_ns->rx_crc_errors = pf->stats.crc_errors;
4632 cur_ns->rx_errors = pf->stats.crc_errors +
4633 pf->stats.illegal_bytes;
4634 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4639 * ice_update_pf_stats - Update PF port stats counters
4640 * @pf: PF whose stats needs to be updated
4642 static void ice_update_pf_stats(struct ice_pf *pf)
4644 struct ice_hw_port_stats *prev_ps, *cur_ps;
4645 struct ice_hw *hw = &pf->hw;
4648 prev_ps = &pf->stats_prev;
4649 cur_ps = &pf->stats;
4652 ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
4653 pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
4654 &cur_ps->eth.rx_bytes);
4656 ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
4657 pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
4658 &cur_ps->eth.rx_unicast);
4660 ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
4661 pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
4662 &cur_ps->eth.rx_multicast);
4664 ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
4665 pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
4666 &cur_ps->eth.rx_broadcast);
4668 ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
4669 pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
4670 &cur_ps->eth.tx_bytes);
4672 ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
4673 pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
4674 &cur_ps->eth.tx_unicast);
4676 ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
4677 pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
4678 &cur_ps->eth.tx_multicast);
4680 ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
4681 pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
4682 &cur_ps->eth.tx_broadcast);
4684 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
4685 &prev_ps->tx_dropped_link_down,
4686 &cur_ps->tx_dropped_link_down);
4688 ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
4689 pf->stat_prev_loaded, &prev_ps->rx_size_64,
4690 &cur_ps->rx_size_64);
4692 ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
4693 pf->stat_prev_loaded, &prev_ps->rx_size_127,
4694 &cur_ps->rx_size_127);
4696 ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
4697 pf->stat_prev_loaded, &prev_ps->rx_size_255,
4698 &cur_ps->rx_size_255);
4700 ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
4701 pf->stat_prev_loaded, &prev_ps->rx_size_511,
4702 &cur_ps->rx_size_511);
4704 ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
4705 GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
4706 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
4708 ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
4709 GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
4710 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
4712 ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
4713 GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
4714 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
4716 ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
4717 pf->stat_prev_loaded, &prev_ps->tx_size_64,
4718 &cur_ps->tx_size_64);
4720 ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
4721 pf->stat_prev_loaded, &prev_ps->tx_size_127,
4722 &cur_ps->tx_size_127);
4724 ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
4725 pf->stat_prev_loaded, &prev_ps->tx_size_255,
4726 &cur_ps->tx_size_255);
4728 ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
4729 pf->stat_prev_loaded, &prev_ps->tx_size_511,
4730 &cur_ps->tx_size_511);
4732 ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
4733 GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
4734 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
4736 ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
4737 GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
4738 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
4740 ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
4741 GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
4742 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
4744 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
4745 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
4747 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
4748 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
4750 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
4751 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
4753 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
4754 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
4756 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
4757 &prev_ps->crc_errors, &cur_ps->crc_errors);
4759 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
4760 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
4762 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
4763 &prev_ps->mac_local_faults,
4764 &cur_ps->mac_local_faults);
4766 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
4767 &prev_ps->mac_remote_faults,
4768 &cur_ps->mac_remote_faults);
4770 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
4771 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
4773 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
4774 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
4776 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
4777 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
4779 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
4780 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
4782 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
4783 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
4785 pf->stat_prev_loaded = true;
4789 * ice_get_stats64 - get statistics for network device structure
4790 * @netdev: network interface device structure
4791 * @stats: main device statistics structure
4794 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
4796 struct ice_netdev_priv *np = netdev_priv(netdev);
4797 struct rtnl_link_stats64 *vsi_stats;
4798 struct ice_vsi *vsi = np->vsi;
4800 vsi_stats = &vsi->net_stats;
4802 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
4804 /* netdev packet/byte stats come from ring counter. These are obtained
4805 * by summing up ring counters (done by ice_update_vsi_ring_stats).
4807 ice_update_vsi_ring_stats(vsi);
4808 stats->tx_packets = vsi_stats->tx_packets;
4809 stats->tx_bytes = vsi_stats->tx_bytes;
4810 stats->rx_packets = vsi_stats->rx_packets;
4811 stats->rx_bytes = vsi_stats->rx_bytes;
4813 /* The rest of the stats can be read from the hardware but instead we
4814 * just return values that the watchdog task has already obtained from
4817 stats->multicast = vsi_stats->multicast;
4818 stats->tx_errors = vsi_stats->tx_errors;
4819 stats->tx_dropped = vsi_stats->tx_dropped;
4820 stats->rx_errors = vsi_stats->rx_errors;
4821 stats->rx_dropped = vsi_stats->rx_dropped;
4822 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
4823 stats->rx_length_errors = vsi_stats->rx_length_errors;
4826 #ifdef CONFIG_NET_POLL_CONTROLLER
4828 * ice_netpoll - polling "interrupt" handler
4829 * @netdev: network interface device structure
4831 * Used by netconsole to send skbs without having to re-enable interrupts.
4832 * This is not called in the normal interrupt path.
4834 static void ice_netpoll(struct net_device *netdev)
4836 struct ice_netdev_priv *np = netdev_priv(netdev);
4837 struct ice_vsi *vsi = np->vsi;
4838 struct ice_pf *pf = vsi->back;
4841 if (test_bit(__ICE_DOWN, vsi->state) ||
4842 !test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4845 for (i = 0; i < vsi->num_q_vectors; i++)
4846 ice_msix_clean_rings(0, vsi->q_vectors[i]);
4848 #endif /* CONFIG_NET_POLL_CONTROLLER */
4851 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4852 * @vsi: VSI having NAPI disabled
4854 static void ice_napi_disable_all(struct ice_vsi *vsi)
4861 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4862 napi_disable(&vsi->q_vectors[q_idx]->napi);
4866 * ice_down - Shutdown the connection
4867 * @vsi: The VSI being stopped
4869 int ice_down(struct ice_vsi *vsi)
4873 /* Caller of this function is expected to set the
4874 * vsi->state __ICE_DOWN bit
4877 netif_carrier_off(vsi->netdev);
4878 netif_tx_disable(vsi->netdev);
4881 ice_vsi_dis_irq(vsi);
4882 err = ice_vsi_stop_tx_rx_rings(vsi);
4883 ice_napi_disable_all(vsi);
4885 ice_for_each_txq(vsi, i)
4886 ice_clean_tx_ring(vsi->tx_rings[i]);
4888 ice_for_each_rxq(vsi, i)
4889 ice_clean_rx_ring(vsi->rx_rings[i]);
4892 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
4893 vsi->vsi_num, vsi->vsw->sw_id);
4898 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
4899 * @vsi: VSI having resources allocated
4901 * Return 0 on success, negative on failure
4903 static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
4907 if (!vsi->num_txq) {
4908 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
4913 ice_for_each_txq(vsi, i) {
4914 err = ice_setup_tx_ring(vsi->tx_rings[i]);
4923 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
4924 * @vsi: VSI having resources allocated
4926 * Return 0 on success, negative on failure
4928 static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
4932 if (!vsi->num_rxq) {
4933 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
4938 ice_for_each_rxq(vsi, i) {
4939 err = ice_setup_rx_ring(vsi->rx_rings[i]);
4948 * ice_vsi_req_irq - Request IRQ from the OS
4949 * @vsi: The VSI IRQ is being requested for
4950 * @basename: name for the vector
4952 * Return 0 on success and a negative value on error
4954 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
4956 struct ice_pf *pf = vsi->back;
4959 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4960 err = ice_vsi_req_irq_msix(vsi, basename);
4966 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
4967 * @vsi: the VSI having resources freed
4969 static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
4976 ice_for_each_txq(vsi, i)
4977 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4978 ice_free_tx_ring(vsi->tx_rings[i]);
4982 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
4983 * @vsi: the VSI having resources freed
4985 static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
4992 ice_for_each_rxq(vsi, i)
4993 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
4994 ice_free_rx_ring(vsi->rx_rings[i]);
4998 * ice_vsi_open - Called when a network interface is made active
4999 * @vsi: the VSI to open
5001 * Initialization of the VSI
5003 * Returns 0 on success, negative value on error
5005 static int ice_vsi_open(struct ice_vsi *vsi)
5007 char int_name[ICE_INT_NAME_STR_LEN];
5008 struct ice_pf *pf = vsi->back;
5011 /* allocate descriptors */
5012 err = ice_vsi_setup_tx_rings(vsi);
5016 err = ice_vsi_setup_rx_rings(vsi);
5020 err = ice_vsi_cfg(vsi);
5024 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5025 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5026 err = ice_vsi_req_irq(vsi, int_name);
5030 /* Notify the stack of the actual queue counts. */
5031 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5035 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5039 err = ice_up_complete(vsi);
5041 goto err_up_complete;
5048 ice_vsi_free_irq(vsi);
5050 ice_vsi_free_rx_rings(vsi);
5052 ice_vsi_free_tx_rings(vsi);
5058 * ice_vsi_close - Shut down a VSI
5059 * @vsi: the VSI being shut down
5061 static void ice_vsi_close(struct ice_vsi *vsi)
5063 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
5066 ice_vsi_free_irq(vsi);
5067 ice_vsi_free_tx_rings(vsi);
5068 ice_vsi_free_rx_rings(vsi);
5072 * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
5073 * @vsi: the VSI being removed
5075 static void ice_rss_clean(struct ice_vsi *vsi)
5081 if (vsi->rss_hkey_user)
5082 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
5083 if (vsi->rss_lut_user)
5084 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
5088 * ice_vsi_release - Delete a VSI and free its resources
5089 * @vsi: the VSI being removed
5091 * Returns 0 on success or < 0 on error
5093 static int ice_vsi_release(struct ice_vsi *vsi)
5100 /* do not unregister and free netdevs while driver is in the reset
5101 * recovery pending state. Since reset/rebuild happens through PF
5102 * service task workqueue, its not a good idea to unregister netdev
5103 * that is associated to the PF that is running the work queue items
5104 * currently. This is done to avoid check_flush_dependency() warning
5107 if (vsi->netdev && !ice_is_reset_recovery_pending(pf->state)) {
5108 unregister_netdev(vsi->netdev);
5109 free_netdev(vsi->netdev);
5113 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
5116 /* Disable VSI and free resources */
5117 ice_vsi_dis_irq(vsi);
5120 /* reclaim interrupt vectors back to PF */
5121 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
5122 pf->num_avail_msix += vsi->num_q_vectors;
5124 ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
5125 ice_vsi_delete(vsi);
5126 ice_vsi_free_q_vectors(vsi);
5127 ice_vsi_clear_rings(vsi);
5129 ice_vsi_put_qs(vsi);
5130 pf->q_left_tx += vsi->alloc_txq;
5131 pf->q_left_rx += vsi->alloc_rxq;
5133 /* retain SW VSI data structure since it is needed to unregister and
5134 * free VSI netdev when PF is not in reset recovery pending state,\
5135 * for ex: during rmmod.
5137 if (!ice_is_reset_recovery_pending(pf->state))
5144 * ice_vsi_release_all - Delete all VSIs
5145 * @pf: PF from which all VSIs are being removed
5147 static void ice_vsi_release_all(struct ice_pf *pf)
5154 for (i = 0; i < pf->num_alloc_vsi; i++) {
5158 err = ice_vsi_release(pf->vsi[i]);
5160 dev_dbg(&pf->pdev->dev,
5161 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5162 i, err, pf->vsi[i]->vsi_num);
5167 * ice_dis_vsi - pause a VSI
5168 * @vsi: the VSI being paused
5170 static void ice_dis_vsi(struct ice_vsi *vsi)
5172 if (test_bit(__ICE_DOWN, vsi->state))
5175 set_bit(__ICE_NEEDS_RESTART, vsi->state);
5177 if (vsi->netdev && netif_running(vsi->netdev) &&
5178 vsi->type == ICE_VSI_PF) {
5180 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5188 * ice_ena_vsi - resume a VSI
5189 * @vsi: the VSI being resume
5191 static int ice_ena_vsi(struct ice_vsi *vsi)
5195 if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
5196 if (vsi->netdev && netif_running(vsi->netdev)) {
5198 err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5206 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
5209 static void ice_pf_dis_all_vsi(struct ice_pf *pf)
5213 ice_for_each_vsi(pf, v)
5215 ice_dis_vsi(pf->vsi[v]);
5219 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
5222 static int ice_pf_ena_all_vsi(struct ice_pf *pf)
5226 ice_for_each_vsi(pf, v)
5228 if (ice_ena_vsi(pf->vsi[v]))
5235 * ice_vsi_rebuild_all - rebuild all VSIs in pf
5238 static int ice_vsi_rebuild_all(struct ice_pf *pf)
5242 /* loop through pf->vsi array and reinit the VSI if found */
5243 for (i = 0; i < pf->num_alloc_vsi; i++) {
5249 err = ice_vsi_rebuild(pf->vsi[i]);
5251 dev_err(&pf->pdev->dev,
5252 "VSI at index %d rebuild failed\n",
5257 dev_info(&pf->pdev->dev,
5258 "VSI at index %d rebuilt. vsi_num = 0x%x\n",
5259 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
5266 * ice_rebuild - rebuild after reset
5267 * @pf: pf to rebuild
5269 static void ice_rebuild(struct ice_pf *pf)
5271 struct device *dev = &pf->pdev->dev;
5272 struct ice_hw *hw = &pf->hw;
5273 enum ice_status ret;
5276 if (test_bit(__ICE_DOWN, pf->state))
5277 goto clear_recovery;
5279 dev_dbg(dev, "rebuilding pf\n");
5281 ret = ice_init_all_ctrlq(hw);
5283 dev_err(dev, "control queues init failed %d\n", ret);
5284 goto err_init_ctrlq;
5287 ret = ice_clear_pf_cfg(hw);
5289 dev_err(dev, "clear PF configuration failed %d\n", ret);
5290 goto err_init_ctrlq;
5293 ice_clear_pxe_mode(hw);
5295 ret = ice_get_caps(hw);
5297 dev_err(dev, "ice_get_caps failed %d\n", ret);
5298 goto err_init_ctrlq;
5301 err = ice_sched_init_port(hw->port_info);
5303 goto err_sched_init_port;
5305 err = ice_vsi_rebuild_all(pf);
5307 dev_err(dev, "ice_vsi_rebuild_all failed\n");
5308 goto err_vsi_rebuild;
5311 ret = ice_replay_all_fltr(&pf->hw);
5313 dev_err(&pf->pdev->dev,
5314 "error replaying switch filter rules\n");
5315 goto err_vsi_rebuild;
5318 /* start misc vector */
5319 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
5320 err = ice_req_irq_msix_misc(pf);
5322 dev_err(dev, "misc vector setup failed: %d\n", err);
5323 goto err_vsi_rebuild;
5327 /* restart the VSIs that were rebuilt and running before the reset */
5328 err = ice_pf_ena_all_vsi(pf);
5330 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
5331 /* no need to disable VSIs in tear down path in ice_rebuild()
5332 * since its already taken care in ice_vsi_open()
5334 goto err_vsi_rebuild;
5337 /* if we get here, reset flow is successful */
5338 clear_bit(__ICE_RESET_FAILED, pf->state);
5342 ice_vsi_release_all(pf);
5343 err_sched_init_port:
5344 ice_sched_cleanup_all(hw);
5346 ice_shutdown_all_ctrlq(hw);
5347 set_bit(__ICE_RESET_FAILED, pf->state);
5349 /* set this bit in PF state to control service task scheduling */
5350 set_bit(__ICE_NEEDS_RESTART, pf->state);
5351 dev_err(dev, "Rebuild failed, unload and reload driver\n");
5355 * ice_change_mtu - NDO callback to change the MTU
5356 * @netdev: network interface device structure
5357 * @new_mtu: new value for maximum frame size
5359 * Returns 0 on success, negative on failure
5361 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
5363 struct ice_netdev_priv *np = netdev_priv(netdev);
5364 struct ice_vsi *vsi = np->vsi;
5365 struct ice_pf *pf = vsi->back;
5368 if (new_mtu == netdev->mtu) {
5369 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
5373 if (new_mtu < netdev->min_mtu) {
5374 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
5377 } else if (new_mtu > netdev->max_mtu) {
5378 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
5382 /* if a reset is in progress, wait for some time for it to complete */
5384 if (ice_is_reset_recovery_pending(pf->state)) {
5386 usleep_range(1000, 2000);
5391 } while (count < 100);
5394 netdev_err(netdev, "can't change mtu. Device is busy\n");
5398 netdev->mtu = new_mtu;
5400 /* if VSI is up, bring it down and then back up */
5401 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
5404 err = ice_down(vsi);
5406 netdev_err(netdev, "change mtu if_up err %d\n", err);
5412 netdev_err(netdev, "change mtu if_up err %d\n", err);
5417 netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
5422 * ice_set_rss - Set RSS keys and lut
5423 * @vsi: Pointer to VSI structure
5424 * @seed: RSS hash seed
5425 * @lut: Lookup table
5426 * @lut_size: Lookup table size
5428 * Returns 0 on success, negative on failure
5430 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5432 struct ice_pf *pf = vsi->back;
5433 struct ice_hw *hw = &pf->hw;
5434 enum ice_status status;
5437 struct ice_aqc_get_set_rss_keys *buf =
5438 (struct ice_aqc_get_set_rss_keys *)seed;
5440 status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
5443 dev_err(&pf->pdev->dev,
5444 "Cannot set RSS key, err %d aq_err %d\n",
5445 status, hw->adminq.rq_last_status);
5451 status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
5452 vsi->rss_lut_type, lut, lut_size);
5454 dev_err(&pf->pdev->dev,
5455 "Cannot set RSS lut, err %d aq_err %d\n",
5456 status, hw->adminq.rq_last_status);
5465 * ice_get_rss - Get RSS keys and lut
5466 * @vsi: Pointer to VSI structure
5467 * @seed: Buffer to store the keys
5468 * @lut: Buffer to store the lookup table entries
5469 * @lut_size: Size of buffer to store the lookup table entries
5471 * Returns 0 on success, negative on failure
5473 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5475 struct ice_pf *pf = vsi->back;
5476 struct ice_hw *hw = &pf->hw;
5477 enum ice_status status;
5480 struct ice_aqc_get_set_rss_keys *buf =
5481 (struct ice_aqc_get_set_rss_keys *)seed;
5483 status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
5485 dev_err(&pf->pdev->dev,
5486 "Cannot get RSS key, err %d aq_err %d\n",
5487 status, hw->adminq.rq_last_status);
5493 status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
5494 vsi->rss_lut_type, lut, lut_size);
5496 dev_err(&pf->pdev->dev,
5497 "Cannot get RSS lut, err %d aq_err %d\n",
5498 status, hw->adminq.rq_last_status);
5507 * ice_open - Called when a network interface becomes active
5508 * @netdev: network interface device structure
5510 * The open entry point is called when a network interface is made
5511 * active by the system (IFF_UP). At this point all resources needed
5512 * for transmit and receive operations are allocated, the interrupt
5513 * handler is registered with the OS, the netdev watchdog is enabled,
5514 * and the stack is notified that the interface is ready.
5516 * Returns 0 on success, negative value on failure
5518 static int ice_open(struct net_device *netdev)
5520 struct ice_netdev_priv *np = netdev_priv(netdev);
5521 struct ice_vsi *vsi = np->vsi;
5524 if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
5525 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
5529 netif_carrier_off(netdev);
5531 err = ice_vsi_open(vsi);
5534 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
5535 vsi->vsi_num, vsi->vsw->sw_id);
5540 * ice_stop - Disables a network interface
5541 * @netdev: network interface device structure
5543 * The stop entry point is called when an interface is de-activated by the OS,
5544 * and the netdevice enters the DOWN state. The hardware is still under the
5545 * driver's control, but the netdev interface is disabled.
5547 * Returns success only - not allowed to fail
5549 static int ice_stop(struct net_device *netdev)
5551 struct ice_netdev_priv *np = netdev_priv(netdev);
5552 struct ice_vsi *vsi = np->vsi;
5560 * ice_features_check - Validate encapsulated packet conforms to limits
5562 * @netdev: This port's netdev
5563 * @features: Offload features that the stack believes apply
5565 static netdev_features_t
5566 ice_features_check(struct sk_buff *skb,
5567 struct net_device __always_unused *netdev,
5568 netdev_features_t features)
5572 /* No point in doing any of this if neither checksum nor GSO are
5573 * being requested for this frame. We can rule out both by just
5574 * checking for CHECKSUM_PARTIAL
5576 if (skb->ip_summed != CHECKSUM_PARTIAL)
5579 /* We cannot support GSO if the MSS is going to be less than
5580 * 64 bytes. If it is then we need to drop support for GSO.
5582 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
5583 features &= ~NETIF_F_GSO_MASK;
5585 len = skb_network_header(skb) - skb->data;
5586 if (len & ~(ICE_TXD_MACLEN_MAX))
5587 goto out_rm_features;
5589 len = skb_transport_header(skb) - skb_network_header(skb);
5590 if (len & ~(ICE_TXD_IPLEN_MAX))
5591 goto out_rm_features;
5593 if (skb->encapsulation) {
5594 len = skb_inner_network_header(skb) - skb_transport_header(skb);
5595 if (len & ~(ICE_TXD_L4LEN_MAX))
5596 goto out_rm_features;
5598 len = skb_inner_transport_header(skb) -
5599 skb_inner_network_header(skb);
5600 if (len & ~(ICE_TXD_IPLEN_MAX))
5601 goto out_rm_features;
5606 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
5609 static const struct net_device_ops ice_netdev_ops = {
5610 .ndo_open = ice_open,
5611 .ndo_stop = ice_stop,
5612 .ndo_start_xmit = ice_start_xmit,
5613 .ndo_features_check = ice_features_check,
5614 .ndo_set_rx_mode = ice_set_rx_mode,
5615 .ndo_set_mac_address = ice_set_mac_address,
5616 .ndo_validate_addr = eth_validate_addr,
5617 .ndo_change_mtu = ice_change_mtu,
5618 .ndo_get_stats64 = ice_get_stats64,
5619 #ifdef CONFIG_NET_POLL_CONTROLLER
5620 .ndo_poll_controller = ice_netpoll,
5621 #endif /* CONFIG_NET_POLL_CONTROLLER */
5622 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
5623 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
5624 .ndo_set_features = ice_set_features,
5625 .ndo_fdb_add = ice_fdb_add,
5626 .ndo_fdb_del = ice_fdb_del,