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 #include "ice_dcb_lib.h"
12 #define DRV_VERSION "0.7.5-k"
13 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
14 const char ice_drv_ver[] = DRV_VERSION;
15 static const char ice_driver_string[] = DRV_SUMMARY;
16 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
18 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
19 MODULE_DESCRIPTION(DRV_SUMMARY);
20 MODULE_LICENSE("GPL v2");
21 MODULE_VERSION(DRV_VERSION);
23 static int debug = -1;
24 module_param(debug, int, 0644);
25 #ifndef CONFIG_DYNAMIC_DEBUG
26 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
28 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
29 #endif /* !CONFIG_DYNAMIC_DEBUG */
31 static struct workqueue_struct *ice_wq;
32 static const struct net_device_ops ice_netdev_ops;
34 static void ice_rebuild(struct ice_pf *pf);
36 static void ice_vsi_release_all(struct ice_pf *pf);
37 static void ice_update_vsi_stats(struct ice_vsi *vsi);
38 static void ice_update_pf_stats(struct ice_pf *pf);
41 * ice_get_tx_pending - returns number of Tx descriptors not processed
42 * @ring: the ring of descriptors
44 static u32 ice_get_tx_pending(struct ice_ring *ring)
48 head = ring->next_to_clean;
49 tail = readl(ring->tail);
52 return (head < tail) ?
53 tail - head : (tail + ring->count - head);
58 * ice_check_for_hang_subtask - check for and recover hung queues
59 * @pf: pointer to PF struct
61 static void ice_check_for_hang_subtask(struct ice_pf *pf)
63 struct ice_vsi *vsi = NULL;
69 ice_for_each_vsi(pf, v)
70 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
75 if (!vsi || test_bit(__ICE_DOWN, vsi->state))
78 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
83 for (i = 0; i < vsi->num_txq; i++) {
84 struct ice_ring *tx_ring = vsi->tx_rings[i];
86 if (tx_ring && tx_ring->desc) {
87 /* If packet counter has not changed the queue is
88 * likely stalled, so force an interrupt for this
91 * prev_pkt would be negative if there was no
94 packets = tx_ring->stats.pkts & INT_MAX;
95 if (tx_ring->tx_stats.prev_pkt == packets) {
96 /* Trigger sw interrupt to revive the queue */
97 ice_trigger_sw_intr(hw, tx_ring->q_vector);
101 /* Memory barrier between read of packet count and call
102 * to ice_get_tx_pending()
105 tx_ring->tx_stats.prev_pkt =
106 ice_get_tx_pending(tx_ring) ? packets : -1;
112 * ice_init_mac_fltr - Set initial MAC filters
113 * @pf: board private structure
115 * Set initial set of MAC filters for PF VSI; configure filters for permanent
116 * address and broadcast address. If an error is encountered, netdevice will be
119 static int ice_init_mac_fltr(struct ice_pf *pf)
121 LIST_HEAD(tmp_add_list);
122 u8 broadcast[ETH_ALEN];
126 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
130 /* To add a MAC filter, first add the MAC to a list and then
131 * pass the list to ice_add_mac.
134 /* Add a unicast MAC filter so the VSI can get its packets */
135 status = ice_add_mac_to_list(vsi, &tmp_add_list,
136 vsi->port_info->mac.perm_addr);
140 /* VSI needs to receive broadcast traffic, so add the broadcast
141 * MAC address to the list as well.
143 eth_broadcast_addr(broadcast);
144 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
148 /* Program MAC filters for entries in tmp_add_list */
149 status = ice_add_mac(&pf->hw, &tmp_add_list);
154 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
157 /* We aren't useful with no MAC filters, so unregister if we
160 if (status && vsi->netdev->reg_state == NETREG_REGISTERED) {
161 dev_err(&pf->pdev->dev,
162 "Could not add MAC filters error %d. Unregistering device\n",
164 unregister_netdev(vsi->netdev);
165 free_netdev(vsi->netdev);
173 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
174 * @netdev: the net device on which the sync is happening
175 * @addr: MAC address to sync
177 * This is a callback function which is called by the in kernel device sync
178 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
179 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
180 * MAC filters from the hardware.
182 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
184 struct ice_netdev_priv *np = netdev_priv(netdev);
185 struct ice_vsi *vsi = np->vsi;
187 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
194 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
195 * @netdev: the net device on which the unsync is happening
196 * @addr: MAC address to unsync
198 * This is a callback function which is called by the in kernel device unsync
199 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
200 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
201 * delete the MAC filters from the hardware.
203 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
205 struct ice_netdev_priv *np = netdev_priv(netdev);
206 struct ice_vsi *vsi = np->vsi;
208 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
215 * ice_vsi_fltr_changed - check if filter state changed
216 * @vsi: VSI to be checked
218 * returns true if filter state has changed, false otherwise.
220 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
222 return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
223 test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
224 test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
228 * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
229 * @vsi: the VSI being configured
230 * @promisc_m: mask of promiscuous config bits
231 * @set_promisc: enable or disable promisc flag request
234 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
236 struct ice_hw *hw = &vsi->back->hw;
237 enum ice_status status = 0;
239 if (vsi->type != ICE_VSI_PF)
243 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
247 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
250 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
261 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
262 * @vsi: ptr to the VSI
264 * Push any outstanding VSI filter changes through the AdminQ.
266 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
268 struct device *dev = &vsi->back->pdev->dev;
269 struct net_device *netdev = vsi->netdev;
270 bool promisc_forced_on = false;
271 struct ice_pf *pf = vsi->back;
272 struct ice_hw *hw = &pf->hw;
273 enum ice_status status = 0;
274 u32 changed_flags = 0;
281 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
282 usleep_range(1000, 2000);
284 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
285 vsi->current_netdev_flags = vsi->netdev->flags;
287 INIT_LIST_HEAD(&vsi->tmp_sync_list);
288 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
290 if (ice_vsi_fltr_changed(vsi)) {
291 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
292 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
293 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
295 /* grab the netdev's addr_list_lock */
296 netif_addr_lock_bh(netdev);
297 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
298 ice_add_mac_to_unsync_list);
299 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
300 ice_add_mac_to_unsync_list);
301 /* our temp lists are populated. release lock */
302 netif_addr_unlock_bh(netdev);
305 /* Remove MAC addresses in the unsync list */
306 status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
307 ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
309 netdev_err(netdev, "Failed to delete MAC filters\n");
310 /* if we failed because of alloc failures, just bail */
311 if (status == ICE_ERR_NO_MEMORY) {
317 /* Add MAC addresses in the sync list */
318 status = ice_add_mac(hw, &vsi->tmp_sync_list);
319 ice_free_fltr_list(dev, &vsi->tmp_sync_list);
320 /* If filter is added successfully or already exists, do not go into
321 * 'if' condition and report it as error. Instead continue processing
322 * rest of the function.
324 if (status && status != ICE_ERR_ALREADY_EXISTS) {
325 netdev_err(netdev, "Failed to add MAC filters\n");
326 /* If there is no more space for new umac filters, VSI
327 * should go into promiscuous mode. There should be some
328 * space reserved for promiscuous filters.
330 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
331 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
333 promisc_forced_on = true;
335 "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
342 /* check for changes in promiscuous modes */
343 if (changed_flags & IFF_ALLMULTI) {
344 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
346 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
348 promisc_m = ICE_MCAST_PROMISC_BITS;
350 err = ice_cfg_promisc(vsi, promisc_m, true);
352 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
354 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
357 } else if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
359 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
361 promisc_m = ICE_MCAST_PROMISC_BITS;
363 err = ice_cfg_promisc(vsi, promisc_m, false);
365 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
367 vsi->current_netdev_flags |= IFF_ALLMULTI;
373 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
374 test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
375 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
376 if (vsi->current_netdev_flags & IFF_PROMISC) {
377 /* Apply Rx filter rule to get traffic from wire */
378 status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
381 netdev_err(netdev, "Error setting default VSI %i Rx rule\n",
383 vsi->current_netdev_flags &= ~IFF_PROMISC;
388 /* Clear Rx filter to remove traffic from wire */
389 status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
392 netdev_err(netdev, "Error clearing default VSI %i Rx rule\n",
394 vsi->current_netdev_flags |= IFF_PROMISC;
403 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
406 /* if something went wrong then set the changed flag so we try again */
407 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
408 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
410 clear_bit(__ICE_CFG_BUSY, vsi->state);
415 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
416 * @pf: board private structure
418 static void ice_sync_fltr_subtask(struct ice_pf *pf)
422 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
425 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
427 ice_for_each_vsi(pf, v)
428 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
429 ice_vsi_sync_fltr(pf->vsi[v])) {
430 /* come back and try again later */
431 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
437 * ice_dis_vsi - pause a VSI
438 * @vsi: the VSI being paused
439 * @locked: is the rtnl_lock already held
441 static void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
443 if (test_bit(__ICE_DOWN, vsi->state))
446 set_bit(__ICE_NEEDS_RESTART, vsi->state);
448 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
449 if (netif_running(vsi->netdev)) {
452 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
455 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
464 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
466 * @locked: is the rtnl_lock already held
469 void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
471 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
472 #endif /* CONFIG_DCB */
476 ice_for_each_vsi(pf, v)
478 ice_dis_vsi(pf->vsi[v], locked);
482 * ice_prepare_for_reset - prep for the core to reset
483 * @pf: board private structure
485 * Inform or close all dependent features in prep for reset.
488 ice_prepare_for_reset(struct ice_pf *pf)
490 struct ice_hw *hw = &pf->hw;
493 /* already prepared for reset */
494 if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
497 /* Notify VFs of impending reset */
498 if (ice_check_sq_alive(hw, &hw->mailboxq))
499 ice_vc_notify_reset(pf);
501 /* Disable VFs until reset is completed */
502 for (i = 0; i < pf->num_alloc_vfs; i++)
503 clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
505 /* disable the VSIs and their queues that are not already DOWN */
506 ice_pf_dis_all_vsi(pf, false);
509 ice_sched_clear_port(hw->port_info);
511 ice_shutdown_all_ctrlq(hw);
513 set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
517 * ice_do_reset - Initiate one of many types of resets
518 * @pf: board private structure
519 * @reset_type: reset type requested
520 * before this function was called.
522 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
524 struct device *dev = &pf->pdev->dev;
525 struct ice_hw *hw = &pf->hw;
527 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
528 WARN_ON(in_interrupt());
530 ice_prepare_for_reset(pf);
532 /* trigger the reset */
533 if (ice_reset(hw, reset_type)) {
534 dev_err(dev, "reset %d failed\n", reset_type);
535 set_bit(__ICE_RESET_FAILED, pf->state);
536 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
537 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
538 clear_bit(__ICE_PFR_REQ, pf->state);
539 clear_bit(__ICE_CORER_REQ, pf->state);
540 clear_bit(__ICE_GLOBR_REQ, pf->state);
544 /* PFR is a bit of a special case because it doesn't result in an OICR
545 * interrupt. So for PFR, rebuild after the reset and clear the reset-
546 * associated state bits.
548 if (reset_type == ICE_RESET_PFR) {
551 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
552 clear_bit(__ICE_PFR_REQ, pf->state);
553 ice_reset_all_vfs(pf, true);
558 * ice_reset_subtask - Set up for resetting the device and driver
559 * @pf: board private structure
561 static void ice_reset_subtask(struct ice_pf *pf)
563 enum ice_reset_req reset_type = ICE_RESET_INVAL;
565 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
566 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
567 * of reset is pending and sets bits in pf->state indicating the reset
568 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
569 * prepare for pending reset if not already (for PF software-initiated
570 * global resets the software should already be prepared for it as
571 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
572 * by firmware or software on other PFs, that bit is not set so prepare
573 * for the reset now), poll for reset done, rebuild and return.
575 if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
576 /* Perform the largest reset requested */
577 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
578 reset_type = ICE_RESET_CORER;
579 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
580 reset_type = ICE_RESET_GLOBR;
581 /* return if no valid reset type requested */
582 if (reset_type == ICE_RESET_INVAL)
584 ice_prepare_for_reset(pf);
586 /* make sure we are ready to rebuild */
587 if (ice_check_reset(&pf->hw)) {
588 set_bit(__ICE_RESET_FAILED, pf->state);
590 /* done with reset. start rebuild */
591 pf->hw.reset_ongoing = false;
593 /* clear bit to resume normal operations, but
594 * ICE_NEEDS_RESTART bit is set in case rebuild failed
596 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
597 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
598 clear_bit(__ICE_PFR_REQ, pf->state);
599 clear_bit(__ICE_CORER_REQ, pf->state);
600 clear_bit(__ICE_GLOBR_REQ, pf->state);
601 ice_reset_all_vfs(pf, true);
607 /* No pending resets to finish processing. Check for new resets */
608 if (test_bit(__ICE_PFR_REQ, pf->state))
609 reset_type = ICE_RESET_PFR;
610 if (test_bit(__ICE_CORER_REQ, pf->state))
611 reset_type = ICE_RESET_CORER;
612 if (test_bit(__ICE_GLOBR_REQ, pf->state))
613 reset_type = ICE_RESET_GLOBR;
614 /* If no valid reset type requested just return */
615 if (reset_type == ICE_RESET_INVAL)
618 /* reset if not already down or busy */
619 if (!test_bit(__ICE_DOWN, pf->state) &&
620 !test_bit(__ICE_CFG_BUSY, pf->state)) {
621 ice_do_reset(pf, reset_type);
626 * ice_print_link_msg - print link up or down message
627 * @vsi: the VSI whose link status is being queried
628 * @isup: boolean for if the link is now up or down
630 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
632 struct ice_aqc_get_phy_caps_data *caps;
633 enum ice_status status;
642 if (vsi->current_isup == isup)
645 vsi->current_isup = isup;
648 netdev_info(vsi->netdev, "NIC Link is Down\n");
652 switch (vsi->port_info->phy.link_info.link_speed) {
653 case ICE_AQ_LINK_SPEED_100GB:
656 case ICE_AQ_LINK_SPEED_50GB:
659 case ICE_AQ_LINK_SPEED_40GB:
662 case ICE_AQ_LINK_SPEED_25GB:
665 case ICE_AQ_LINK_SPEED_20GB:
668 case ICE_AQ_LINK_SPEED_10GB:
671 case ICE_AQ_LINK_SPEED_5GB:
674 case ICE_AQ_LINK_SPEED_2500MB:
677 case ICE_AQ_LINK_SPEED_1000MB:
680 case ICE_AQ_LINK_SPEED_100MB:
688 switch (vsi->port_info->fc.current_mode) {
692 case ICE_FC_TX_PAUSE:
695 case ICE_FC_RX_PAUSE:
706 /* Get FEC mode based on negotiated link info */
707 switch (vsi->port_info->phy.link_info.fec_info) {
708 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
710 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
713 case ICE_AQ_LINK_25G_KR_FEC_EN:
714 fec = "FC-FEC/BASE-R";
721 /* Get FEC mode requested based on PHY caps last SW configuration */
722 caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
728 status = ice_aq_get_phy_caps(vsi->port_info, false,
729 ICE_AQC_REPORT_SW_CFG, caps, NULL);
731 netdev_info(vsi->netdev, "Get phy capability failed.\n");
733 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
734 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
736 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
737 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
738 fec_req = "FC-FEC/BASE-R";
742 devm_kfree(&vsi->back->pdev->dev, caps);
745 netdev_info(vsi->netdev, "NIC Link is up %sbps, Requested FEC: %s, FEC: %s, Flow Control: %s\n",
746 speed, fec_req, fec, fc);
750 * ice_vsi_link_event - update the VSI's netdev
751 * @vsi: the VSI on which the link event occurred
752 * @link_up: whether or not the VSI needs to be set up or down
754 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
759 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
762 if (vsi->type == ICE_VSI_PF) {
763 if (link_up == netif_carrier_ok(vsi->netdev))
767 netif_carrier_on(vsi->netdev);
768 netif_tx_wake_all_queues(vsi->netdev);
770 netif_carrier_off(vsi->netdev);
771 netif_tx_stop_all_queues(vsi->netdev);
777 * ice_link_event - process the link event
778 * @pf: PF that the link event is associated with
779 * @pi: port_info for the port that the link event is associated with
780 * @link_up: true if the physical link is up and false if it is down
781 * @link_speed: current link speed received from the link event
783 * Returns 0 on success and negative on failure
786 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
789 struct ice_phy_info *phy_info;
796 phy_info->link_info_old = phy_info->link_info;
798 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
799 old_link_speed = phy_info->link_info_old.link_speed;
801 /* update the link info structures and re-enable link events,
802 * don't bail on failure due to other book keeping needed
804 result = ice_update_link_info(pi);
806 dev_dbg(&pf->pdev->dev,
807 "Failed to update link status and re-enable link events for port %d\n",
810 /* if the old link up/down and speed is the same as the new */
811 if (link_up == old_link && link_speed == old_link_speed)
814 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
815 if (!vsi || !vsi->port_info)
818 /* turn off PHY if media was removed */
819 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
820 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
821 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
823 result = ice_aq_set_link_restart_an(pi, false, NULL);
825 dev_dbg(&pf->pdev->dev,
826 "Failed to set link down, VSI %d error %d\n",
827 vsi->vsi_num, result);
832 ice_vsi_link_event(vsi, link_up);
833 ice_print_link_msg(vsi, link_up);
835 if (pf->num_alloc_vfs)
836 ice_vc_notify_link_state(pf);
842 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
843 * @pf: board private structure
845 static void ice_watchdog_subtask(struct ice_pf *pf)
849 /* if interface is down do nothing */
850 if (test_bit(__ICE_DOWN, pf->state) ||
851 test_bit(__ICE_CFG_BUSY, pf->state))
854 /* make sure we don't do these things too often */
855 if (time_before(jiffies,
856 pf->serv_tmr_prev + pf->serv_tmr_period))
859 pf->serv_tmr_prev = jiffies;
861 /* Update the stats for active netdevs so the network stack
862 * can look at updated numbers whenever it cares to
864 ice_update_pf_stats(pf);
865 ice_for_each_vsi(pf, i)
866 if (pf->vsi[i] && pf->vsi[i]->netdev)
867 ice_update_vsi_stats(pf->vsi[i]);
871 * ice_init_link_events - enable/initialize link events
872 * @pi: pointer to the port_info instance
874 * Returns -EIO on failure, 0 on success
876 static int ice_init_link_events(struct ice_port_info *pi)
880 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
881 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
883 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
884 dev_dbg(ice_hw_to_dev(pi->hw),
885 "Failed to set link event mask for port %d\n",
890 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
891 dev_dbg(ice_hw_to_dev(pi->hw),
892 "Failed to enable link events for port %d\n",
901 * ice_handle_link_event - handle link event via ARQ
902 * @pf: PF that the link event is associated with
903 * @event: event structure containing link status info
906 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
908 struct ice_aqc_get_link_status_data *link_data;
909 struct ice_port_info *port_info;
912 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
913 port_info = pf->hw.port_info;
917 status = ice_link_event(pf, port_info,
918 !!(link_data->link_info & ICE_AQ_LINK_UP),
919 le16_to_cpu(link_data->link_speed));
921 dev_dbg(&pf->pdev->dev,
922 "Could not process link event, error %d\n", status);
928 * __ice_clean_ctrlq - helper function to clean controlq rings
929 * @pf: ptr to struct ice_pf
930 * @q_type: specific Control queue type
932 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
934 struct ice_rq_event_info event;
935 struct ice_hw *hw = &pf->hw;
936 struct ice_ctl_q_info *cq;
941 /* Do not clean control queue if/when PF reset fails */
942 if (test_bit(__ICE_RESET_FAILED, pf->state))
946 case ICE_CTL_Q_ADMIN:
950 case ICE_CTL_Q_MAILBOX:
955 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
960 /* check for error indications - PF_xx_AxQLEN register layout for
961 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
963 val = rd32(hw, cq->rq.len);
964 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
965 PF_FW_ARQLEN_ARQCRIT_M)) {
967 if (val & PF_FW_ARQLEN_ARQVFE_M)
968 dev_dbg(&pf->pdev->dev,
969 "%s Receive Queue VF Error detected\n", qtype);
970 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
971 dev_dbg(&pf->pdev->dev,
972 "%s Receive Queue Overflow Error detected\n",
975 if (val & PF_FW_ARQLEN_ARQCRIT_M)
976 dev_dbg(&pf->pdev->dev,
977 "%s Receive Queue Critical Error detected\n",
979 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
980 PF_FW_ARQLEN_ARQCRIT_M);
982 wr32(hw, cq->rq.len, val);
985 val = rd32(hw, cq->sq.len);
986 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
987 PF_FW_ATQLEN_ATQCRIT_M)) {
989 if (val & PF_FW_ATQLEN_ATQVFE_M)
990 dev_dbg(&pf->pdev->dev,
991 "%s Send Queue VF Error detected\n", qtype);
992 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
993 dev_dbg(&pf->pdev->dev,
994 "%s Send Queue Overflow Error detected\n",
997 if (val & PF_FW_ATQLEN_ATQCRIT_M)
998 dev_dbg(&pf->pdev->dev,
999 "%s Send Queue Critical Error detected\n",
1001 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1002 PF_FW_ATQLEN_ATQCRIT_M);
1004 wr32(hw, cq->sq.len, val);
1007 event.buf_len = cq->rq_buf_size;
1008 event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
1014 enum ice_status ret;
1017 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1018 if (ret == ICE_ERR_AQ_NO_WORK)
1021 dev_err(&pf->pdev->dev,
1022 "%s Receive Queue event error %d\n", qtype,
1027 opcode = le16_to_cpu(event.desc.opcode);
1030 case ice_aqc_opc_get_link_status:
1031 if (ice_handle_link_event(pf, &event))
1032 dev_err(&pf->pdev->dev,
1033 "Could not handle link event\n");
1035 case ice_mbx_opc_send_msg_to_pf:
1036 ice_vc_process_vf_msg(pf, &event);
1038 case ice_aqc_opc_fw_logging:
1039 ice_output_fw_log(hw, &event.desc, event.msg_buf);
1041 case ice_aqc_opc_lldp_set_mib_change:
1042 ice_dcb_process_lldp_set_mib_change(pf, &event);
1045 dev_dbg(&pf->pdev->dev,
1046 "%s Receive Queue unknown event 0x%04x ignored\n",
1050 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1052 devm_kfree(&pf->pdev->dev, event.msg_buf);
1054 return pending && (i == ICE_DFLT_IRQ_WORK);
1058 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1059 * @hw: pointer to hardware info
1060 * @cq: control queue information
1062 * returns true if there are pending messages in a queue, false if there aren't
1064 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1068 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1069 return cq->rq.next_to_clean != ntu;
1073 * ice_clean_adminq_subtask - clean the AdminQ rings
1074 * @pf: board private structure
1076 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1078 struct ice_hw *hw = &pf->hw;
1080 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1083 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1086 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1088 /* There might be a situation where new messages arrive to a control
1089 * queue between processing the last message and clearing the
1090 * EVENT_PENDING bit. So before exiting, check queue head again (using
1091 * ice_ctrlq_pending) and process new messages if any.
1093 if (ice_ctrlq_pending(hw, &hw->adminq))
1094 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1100 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1101 * @pf: board private structure
1103 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1105 struct ice_hw *hw = &pf->hw;
1107 if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1110 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1113 clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1115 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1116 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1122 * ice_service_task_schedule - schedule the service task to wake up
1123 * @pf: board private structure
1125 * If not already scheduled, this puts the task into the work queue.
1127 static void ice_service_task_schedule(struct ice_pf *pf)
1129 if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1130 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1131 !test_bit(__ICE_NEEDS_RESTART, pf->state))
1132 queue_work(ice_wq, &pf->serv_task);
1136 * ice_service_task_complete - finish up the service task
1137 * @pf: board private structure
1139 static void ice_service_task_complete(struct ice_pf *pf)
1141 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1143 /* force memory (pf->state) to sync before next service task */
1144 smp_mb__before_atomic();
1145 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1149 * ice_service_task_stop - stop service task and cancel works
1150 * @pf: board private structure
1152 static void ice_service_task_stop(struct ice_pf *pf)
1154 set_bit(__ICE_SERVICE_DIS, pf->state);
1156 if (pf->serv_tmr.function)
1157 del_timer_sync(&pf->serv_tmr);
1158 if (pf->serv_task.func)
1159 cancel_work_sync(&pf->serv_task);
1161 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1165 * ice_service_task_restart - restart service task and schedule works
1166 * @pf: board private structure
1168 * This function is needed for suspend and resume works (e.g WoL scenario)
1170 static void ice_service_task_restart(struct ice_pf *pf)
1172 clear_bit(__ICE_SERVICE_DIS, pf->state);
1173 ice_service_task_schedule(pf);
1177 * ice_service_timer - timer callback to schedule service task
1178 * @t: pointer to timer_list
1180 static void ice_service_timer(struct timer_list *t)
1182 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1184 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1185 ice_service_task_schedule(pf);
1189 * ice_handle_mdd_event - handle malicious driver detect event
1190 * @pf: pointer to the PF structure
1192 * Called from service task. OICR interrupt handler indicates MDD event
1194 static void ice_handle_mdd_event(struct ice_pf *pf)
1196 struct ice_hw *hw = &pf->hw;
1197 bool mdd_detected = false;
1201 if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1204 /* find what triggered the MDD event */
1205 reg = rd32(hw, GL_MDET_TX_PQM);
1206 if (reg & GL_MDET_TX_PQM_VALID_M) {
1207 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1208 GL_MDET_TX_PQM_PF_NUM_S;
1209 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1210 GL_MDET_TX_PQM_VF_NUM_S;
1211 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1212 GL_MDET_TX_PQM_MAL_TYPE_S;
1213 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1214 GL_MDET_TX_PQM_QNUM_S);
1216 if (netif_msg_tx_err(pf))
1217 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1218 event, queue, pf_num, vf_num);
1219 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1220 mdd_detected = true;
1223 reg = rd32(hw, GL_MDET_TX_TCLAN);
1224 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1225 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1226 GL_MDET_TX_TCLAN_PF_NUM_S;
1227 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1228 GL_MDET_TX_TCLAN_VF_NUM_S;
1229 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1230 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1231 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1232 GL_MDET_TX_TCLAN_QNUM_S);
1234 if (netif_msg_rx_err(pf))
1235 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1236 event, queue, pf_num, vf_num);
1237 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1238 mdd_detected = true;
1241 reg = rd32(hw, GL_MDET_RX);
1242 if (reg & GL_MDET_RX_VALID_M) {
1243 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1244 GL_MDET_RX_PF_NUM_S;
1245 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1246 GL_MDET_RX_VF_NUM_S;
1247 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1248 GL_MDET_RX_MAL_TYPE_S;
1249 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1252 if (netif_msg_rx_err(pf))
1253 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1254 event, queue, pf_num, vf_num);
1255 wr32(hw, GL_MDET_RX, 0xffffffff);
1256 mdd_detected = true;
1260 bool pf_mdd_detected = false;
1262 reg = rd32(hw, PF_MDET_TX_PQM);
1263 if (reg & PF_MDET_TX_PQM_VALID_M) {
1264 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1265 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1266 pf_mdd_detected = true;
1269 reg = rd32(hw, PF_MDET_TX_TCLAN);
1270 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1271 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1272 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1273 pf_mdd_detected = true;
1276 reg = rd32(hw, PF_MDET_RX);
1277 if (reg & PF_MDET_RX_VALID_M) {
1278 wr32(hw, PF_MDET_RX, 0xFFFF);
1279 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1280 pf_mdd_detected = true;
1282 /* Queue belongs to the PF initiate a reset */
1283 if (pf_mdd_detected) {
1284 set_bit(__ICE_NEEDS_RESTART, pf->state);
1285 ice_service_task_schedule(pf);
1289 /* check to see if one of the VFs caused the MDD */
1290 for (i = 0; i < pf->num_alloc_vfs; i++) {
1291 struct ice_vf *vf = &pf->vf[i];
1293 bool vf_mdd_detected = false;
1295 reg = rd32(hw, VP_MDET_TX_PQM(i));
1296 if (reg & VP_MDET_TX_PQM_VALID_M) {
1297 wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1298 vf_mdd_detected = true;
1299 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1303 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1304 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1305 wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1306 vf_mdd_detected = true;
1307 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1311 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1312 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1313 wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1314 vf_mdd_detected = true;
1315 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1319 reg = rd32(hw, VP_MDET_RX(i));
1320 if (reg & VP_MDET_RX_VALID_M) {
1321 wr32(hw, VP_MDET_RX(i), 0xFFFF);
1322 vf_mdd_detected = true;
1323 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
1327 if (vf_mdd_detected) {
1328 vf->num_mdd_events++;
1329 if (vf->num_mdd_events > 1)
1330 dev_info(&pf->pdev->dev, "VF %d has had %llu MDD events since last boot\n",
1331 i, vf->num_mdd_events);
1337 * ice_force_phys_link_state - Force the physical link state
1338 * @vsi: VSI to force the physical link state to up/down
1339 * @link_up: true/false indicates to set the physical link to up/down
1341 * Force the physical link state by getting the current PHY capabilities from
1342 * hardware and setting the PHY config based on the determined capabilities. If
1343 * link changes a link event will be triggered because both the Enable Automatic
1344 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1346 * Returns 0 on success, negative on failure
1348 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1350 struct ice_aqc_get_phy_caps_data *pcaps;
1351 struct ice_aqc_set_phy_cfg_data *cfg;
1352 struct ice_port_info *pi;
1356 if (!vsi || !vsi->port_info || !vsi->back)
1358 if (vsi->type != ICE_VSI_PF)
1361 dev = &vsi->back->pdev->dev;
1363 pi = vsi->port_info;
1365 pcaps = devm_kzalloc(dev, sizeof(*pcaps), GFP_KERNEL);
1369 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1373 "Failed to get phy capabilities, VSI %d error %d\n",
1374 vsi->vsi_num, retcode);
1379 /* No change in link */
1380 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1381 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1384 cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
1390 cfg->phy_type_low = pcaps->phy_type_low;
1391 cfg->phy_type_high = pcaps->phy_type_high;
1392 cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1393 cfg->low_power_ctrl = pcaps->low_power_ctrl;
1394 cfg->eee_cap = pcaps->eee_cap;
1395 cfg->eeer_value = pcaps->eeer_value;
1396 cfg->link_fec_opt = pcaps->link_fec_options;
1398 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1400 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1402 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL);
1404 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1405 vsi->vsi_num, retcode);
1409 devm_kfree(dev, cfg);
1411 devm_kfree(dev, pcaps);
1416 * ice_check_media_subtask - Check for media; bring link up if detected.
1417 * @pf: pointer to PF struct
1419 static void ice_check_media_subtask(struct ice_pf *pf)
1421 struct ice_port_info *pi;
1422 struct ice_vsi *vsi;
1425 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
1429 /* No need to check for media if it's already present or the interface
1432 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) ||
1433 test_bit(__ICE_DOWN, vsi->state))
1436 /* Refresh link info and check if media is present */
1437 pi = vsi->port_info;
1438 err = ice_update_link_info(pi);
1442 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
1443 err = ice_force_phys_link_state(vsi, true);
1446 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1448 /* A Link Status Event will be generated; the event handler
1449 * will complete bringing the interface up
1455 * ice_service_task - manage and run subtasks
1456 * @work: pointer to work_struct contained by the PF struct
1458 static void ice_service_task(struct work_struct *work)
1460 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1461 unsigned long start_time = jiffies;
1465 /* process reset requests first */
1466 ice_reset_subtask(pf);
1468 /* bail if a reset/recovery cycle is pending or rebuild failed */
1469 if (ice_is_reset_in_progress(pf->state) ||
1470 test_bit(__ICE_SUSPENDED, pf->state) ||
1471 test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1472 ice_service_task_complete(pf);
1476 ice_check_media_subtask(pf);
1477 ice_check_for_hang_subtask(pf);
1478 ice_sync_fltr_subtask(pf);
1479 ice_handle_mdd_event(pf);
1480 ice_process_vflr_event(pf);
1481 ice_watchdog_subtask(pf);
1482 ice_clean_adminq_subtask(pf);
1483 ice_clean_mailboxq_subtask(pf);
1485 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1486 ice_service_task_complete(pf);
1488 /* If the tasks have taken longer than one service timer period
1489 * or there is more work to be done, reset the service timer to
1490 * schedule the service task now.
1492 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1493 test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1494 test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1495 test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1496 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1497 mod_timer(&pf->serv_tmr, jiffies);
1501 * ice_set_ctrlq_len - helper function to set controlq length
1502 * @hw: pointer to the HW instance
1504 static void ice_set_ctrlq_len(struct ice_hw *hw)
1506 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1507 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1508 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1509 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1510 hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
1511 hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
1512 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1513 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1517 * ice_irq_affinity_notify - Callback for affinity changes
1518 * @notify: context as to what irq was changed
1519 * @mask: the new affinity mask
1521 * This is a callback function used by the irq_set_affinity_notifier function
1522 * so that we may register to receive changes to the irq affinity masks.
1525 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1526 const cpumask_t *mask)
1528 struct ice_q_vector *q_vector =
1529 container_of(notify, struct ice_q_vector, affinity_notify);
1531 cpumask_copy(&q_vector->affinity_mask, mask);
1535 * ice_irq_affinity_release - Callback for affinity notifier release
1536 * @ref: internal core kernel usage
1538 * This is a callback function used by the irq_set_affinity_notifier function
1539 * to inform the current notification subscriber that they will no longer
1540 * receive notifications.
1542 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1545 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1546 * @vsi: the VSI being configured
1548 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1550 struct ice_hw *hw = &vsi->back->hw;
1553 ice_for_each_q_vector(vsi, i)
1554 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1561 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1562 * @vsi: the VSI being configured
1563 * @basename: name for the vector
1565 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1567 int q_vectors = vsi->num_q_vectors;
1568 struct ice_pf *pf = vsi->back;
1569 int base = vsi->base_vector;
1575 for (vector = 0; vector < q_vectors; vector++) {
1576 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1578 irq_num = pf->msix_entries[base + vector].vector;
1580 if (q_vector->tx.ring && q_vector->rx.ring) {
1581 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1582 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1584 } else if (q_vector->rx.ring) {
1585 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1586 "%s-%s-%d", basename, "rx", rx_int_idx++);
1587 } else if (q_vector->tx.ring) {
1588 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1589 "%s-%s-%d", basename, "tx", tx_int_idx++);
1591 /* skip this unused q_vector */
1594 err = devm_request_irq(&pf->pdev->dev, irq_num,
1595 vsi->irq_handler, 0,
1596 q_vector->name, q_vector);
1598 netdev_err(vsi->netdev,
1599 "MSIX request_irq failed, error: %d\n", err);
1603 /* register for affinity change notifications */
1604 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1605 q_vector->affinity_notify.release = ice_irq_affinity_release;
1606 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1608 /* assign the mask for this irq */
1609 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1612 vsi->irqs_ready = true;
1618 irq_num = pf->msix_entries[base + vector].vector,
1619 irq_set_affinity_notifier(irq_num, NULL);
1620 irq_set_affinity_hint(irq_num, NULL);
1621 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1627 * ice_ena_misc_vector - enable the non-queue interrupts
1628 * @pf: board private structure
1630 static void ice_ena_misc_vector(struct ice_pf *pf)
1632 struct ice_hw *hw = &pf->hw;
1635 /* clear things first */
1636 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1637 rd32(hw, PFINT_OICR); /* read to clear */
1639 val = (PFINT_OICR_ECC_ERR_M |
1640 PFINT_OICR_MAL_DETECT_M |
1642 PFINT_OICR_PCI_EXCEPTION_M |
1644 PFINT_OICR_HMC_ERR_M |
1645 PFINT_OICR_PE_CRITERR_M);
1647 wr32(hw, PFINT_OICR_ENA, val);
1649 /* SW_ITR_IDX = 0, but don't change INTENA */
1650 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1651 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1655 * ice_misc_intr - misc interrupt handler
1656 * @irq: interrupt number
1657 * @data: pointer to a q_vector
1659 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1661 struct ice_pf *pf = (struct ice_pf *)data;
1662 struct ice_hw *hw = &pf->hw;
1663 irqreturn_t ret = IRQ_NONE;
1666 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1667 set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1669 oicr = rd32(hw, PFINT_OICR);
1670 ena_mask = rd32(hw, PFINT_OICR_ENA);
1672 if (oicr & PFINT_OICR_SWINT_M) {
1673 ena_mask &= ~PFINT_OICR_SWINT_M;
1677 if (oicr & PFINT_OICR_MAL_DETECT_M) {
1678 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
1679 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1681 if (oicr & PFINT_OICR_VFLR_M) {
1682 ena_mask &= ~PFINT_OICR_VFLR_M;
1683 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
1686 if (oicr & PFINT_OICR_GRST_M) {
1689 /* we have a reset warning */
1690 ena_mask &= ~PFINT_OICR_GRST_M;
1691 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1692 GLGEN_RSTAT_RESET_TYPE_S;
1694 if (reset == ICE_RESET_CORER)
1696 else if (reset == ICE_RESET_GLOBR)
1698 else if (reset == ICE_RESET_EMPR)
1701 dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
1704 /* If a reset cycle isn't already in progress, we set a bit in
1705 * pf->state so that the service task can start a reset/rebuild.
1706 * We also make note of which reset happened so that peer
1707 * devices/drivers can be informed.
1709 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
1710 if (reset == ICE_RESET_CORER)
1711 set_bit(__ICE_CORER_RECV, pf->state);
1712 else if (reset == ICE_RESET_GLOBR)
1713 set_bit(__ICE_GLOBR_RECV, pf->state);
1715 set_bit(__ICE_EMPR_RECV, pf->state);
1717 /* There are couple of different bits at play here.
1718 * hw->reset_ongoing indicates whether the hardware is
1719 * in reset. This is set to true when a reset interrupt
1720 * is received and set back to false after the driver
1721 * has determined that the hardware is out of reset.
1723 * __ICE_RESET_OICR_RECV in pf->state indicates
1724 * that a post reset rebuild is required before the
1725 * driver is operational again. This is set above.
1727 * As this is the start of the reset/rebuild cycle, set
1728 * both to indicate that.
1730 hw->reset_ongoing = true;
1734 if (oicr & PFINT_OICR_HMC_ERR_M) {
1735 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1736 dev_dbg(&pf->pdev->dev,
1737 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1738 rd32(hw, PFHMC_ERRORINFO),
1739 rd32(hw, PFHMC_ERRORDATA));
1742 /* Report any remaining unexpected interrupts */
1745 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1747 /* If a critical error is pending there is no choice but to
1750 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1751 PFINT_OICR_PCI_EXCEPTION_M |
1752 PFINT_OICR_ECC_ERR_M)) {
1753 set_bit(__ICE_PFR_REQ, pf->state);
1754 ice_service_task_schedule(pf);
1759 if (!test_bit(__ICE_DOWN, pf->state)) {
1760 ice_service_task_schedule(pf);
1761 ice_irq_dynamic_ena(hw, NULL, NULL);
1768 * ice_dis_ctrlq_interrupts - disable control queue interrupts
1769 * @hw: pointer to HW structure
1771 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
1773 /* disable Admin queue Interrupt causes */
1774 wr32(hw, PFINT_FW_CTL,
1775 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
1777 /* disable Mailbox queue Interrupt causes */
1778 wr32(hw, PFINT_MBX_CTL,
1779 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
1781 /* disable Control queue Interrupt causes */
1782 wr32(hw, PFINT_OICR_CTL,
1783 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
1789 * ice_free_irq_msix_misc - Unroll misc vector setup
1790 * @pf: board private structure
1792 static void ice_free_irq_msix_misc(struct ice_pf *pf)
1794 struct ice_hw *hw = &pf->hw;
1796 ice_dis_ctrlq_interrupts(hw);
1798 /* disable OICR interrupt */
1799 wr32(hw, PFINT_OICR_ENA, 0);
1802 if (pf->msix_entries) {
1803 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
1804 devm_free_irq(&pf->pdev->dev,
1805 pf->msix_entries[pf->oicr_idx].vector, pf);
1808 pf->num_avail_sw_msix += 1;
1809 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
1813 * ice_ena_ctrlq_interrupts - enable control queue interrupts
1814 * @hw: pointer to HW structure
1815 * @reg_idx: HW vector index to associate the control queue interrupts with
1817 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
1821 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
1822 PFINT_OICR_CTL_CAUSE_ENA_M);
1823 wr32(hw, PFINT_OICR_CTL, val);
1825 /* enable Admin queue Interrupt causes */
1826 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
1827 PFINT_FW_CTL_CAUSE_ENA_M);
1828 wr32(hw, PFINT_FW_CTL, val);
1830 /* enable Mailbox queue Interrupt causes */
1831 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
1832 PFINT_MBX_CTL_CAUSE_ENA_M);
1833 wr32(hw, PFINT_MBX_CTL, val);
1839 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
1840 * @pf: board private structure
1842 * This sets up the handler for MSIX 0, which is used to manage the
1843 * non-queue interrupts, e.g. AdminQ and errors. This is not used
1844 * when in MSI or Legacy interrupt mode.
1846 static int ice_req_irq_msix_misc(struct ice_pf *pf)
1848 struct ice_hw *hw = &pf->hw;
1849 int oicr_idx, err = 0;
1851 if (!pf->int_name[0])
1852 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
1853 dev_driver_string(&pf->pdev->dev),
1854 dev_name(&pf->pdev->dev));
1856 /* Do not request IRQ but do enable OICR interrupt since settings are
1857 * lost during reset. Note that this function is called only during
1858 * rebuild path and not while reset is in progress.
1860 if (ice_is_reset_in_progress(pf->state))
1863 /* reserve one vector in irq_tracker for misc interrupts */
1864 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1868 pf->num_avail_sw_msix -= 1;
1869 pf->oicr_idx = oicr_idx;
1871 err = devm_request_irq(&pf->pdev->dev,
1872 pf->msix_entries[pf->oicr_idx].vector,
1873 ice_misc_intr, 0, pf->int_name, pf);
1875 dev_err(&pf->pdev->dev,
1876 "devm_request_irq for %s failed: %d\n",
1878 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1879 pf->num_avail_sw_msix += 1;
1884 ice_ena_misc_vector(pf);
1886 ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
1887 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
1888 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
1891 ice_irq_dynamic_ena(hw, NULL, NULL);
1897 * ice_napi_add - register NAPI handler for the VSI
1898 * @vsi: VSI for which NAPI handler is to be registered
1900 * This function is only called in the driver's load path. Registering the NAPI
1901 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
1902 * reset/rebuild, etc.)
1904 static void ice_napi_add(struct ice_vsi *vsi)
1911 ice_for_each_q_vector(vsi, v_idx)
1912 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
1913 ice_napi_poll, NAPI_POLL_WEIGHT);
1917 * ice_cfg_netdev - Allocate, configure and register a netdev
1918 * @vsi: the VSI associated with the new netdev
1920 * Returns 0 on success, negative value on failure
1922 static int ice_cfg_netdev(struct ice_vsi *vsi)
1924 netdev_features_t csumo_features;
1925 netdev_features_t vlano_features;
1926 netdev_features_t dflt_features;
1927 netdev_features_t tso_features;
1928 struct ice_netdev_priv *np;
1929 struct net_device *netdev;
1930 u8 mac_addr[ETH_ALEN];
1933 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
1938 vsi->netdev = netdev;
1939 np = netdev_priv(netdev);
1942 dflt_features = NETIF_F_SG |
1946 csumo_features = NETIF_F_RXCSUM |
1951 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
1952 NETIF_F_HW_VLAN_CTAG_TX |
1953 NETIF_F_HW_VLAN_CTAG_RX;
1955 tso_features = NETIF_F_TSO;
1957 /* set features that user can change */
1958 netdev->hw_features = dflt_features | csumo_features |
1959 vlano_features | tso_features;
1961 /* enable features */
1962 netdev->features |= netdev->hw_features;
1963 /* encap and VLAN devices inherit default, csumo and tso features */
1964 netdev->hw_enc_features |= dflt_features | csumo_features |
1966 netdev->vlan_features |= dflt_features | csumo_features |
1969 if (vsi->type == ICE_VSI_PF) {
1970 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
1971 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
1973 ether_addr_copy(netdev->dev_addr, mac_addr);
1974 ether_addr_copy(netdev->perm_addr, mac_addr);
1977 netdev->priv_flags |= IFF_UNICAST_FLT;
1979 /* assign netdev_ops */
1980 netdev->netdev_ops = &ice_netdev_ops;
1982 /* setup watchdog timeout value to be 5 second */
1983 netdev->watchdog_timeo = 5 * HZ;
1985 ice_set_ethtool_ops(netdev);
1987 netdev->min_mtu = ETH_MIN_MTU;
1988 netdev->max_mtu = ICE_MAX_MTU;
1990 err = register_netdev(vsi->netdev);
1994 netif_carrier_off(vsi->netdev);
1996 /* make sure transmit queues start off as stopped */
1997 netif_tx_stop_all_queues(vsi->netdev);
2003 * ice_fill_rss_lut - Fill the RSS lookup table with default values
2004 * @lut: Lookup table
2005 * @rss_table_size: Lookup table size
2006 * @rss_size: Range of queue number for hashing
2008 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2012 for (i = 0; i < rss_table_size; i++)
2013 lut[i] = i % rss_size;
2017 * ice_pf_vsi_setup - Set up a PF VSI
2018 * @pf: board private structure
2019 * @pi: pointer to the port_info instance
2021 * Returns pointer to the successfully allocated VSI software struct
2022 * on success, otherwise returns NULL on failure.
2024 static struct ice_vsi *
2025 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2027 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
2031 * ice_lb_vsi_setup - Set up a loopback VSI
2032 * @pf: board private structure
2033 * @pi: pointer to the port_info instance
2035 * Returns pointer to the successfully allocated VSI software struct
2036 * on success, otherwise returns NULL on failure.
2039 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2041 return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
2045 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
2046 * @netdev: network interface to be adjusted
2047 * @proto: unused protocol
2048 * @vid: VLAN ID to be added
2050 * net_device_ops implementation for adding VLAN IDs
2053 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
2056 struct ice_netdev_priv *np = netdev_priv(netdev);
2057 struct ice_vsi *vsi = np->vsi;
2060 if (vid >= VLAN_N_VID) {
2061 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2069 /* Enable VLAN pruning when VLAN 0 is added */
2070 if (unlikely(!vid)) {
2071 ret = ice_cfg_vlan_pruning(vsi, true, false);
2076 /* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is
2077 * needed to continue allowing all untagged packets since VLAN prune
2078 * list is applied to all packets by the switch
2080 ret = ice_vsi_add_vlan(vsi, vid);
2082 vsi->vlan_ena = true;
2083 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2090 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
2091 * @netdev: network interface to be adjusted
2092 * @proto: unused protocol
2093 * @vid: VLAN ID to be removed
2095 * net_device_ops implementation for removing VLAN IDs
2098 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
2101 struct ice_netdev_priv *np = netdev_priv(netdev);
2102 struct ice_vsi *vsi = np->vsi;
2108 /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
2111 ret = ice_vsi_kill_vlan(vsi, vid);
2115 /* Disable VLAN pruning when VLAN 0 is removed */
2117 ret = ice_cfg_vlan_pruning(vsi, false, false);
2119 vsi->vlan_ena = false;
2120 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2125 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
2126 * @pf: board private structure
2128 * Returns 0 on success, negative value on failure
2130 static int ice_setup_pf_sw(struct ice_pf *pf)
2132 struct ice_vsi *vsi;
2135 if (ice_is_reset_in_progress(pf->state))
2138 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
2141 goto unroll_vsi_setup;
2144 status = ice_cfg_netdev(vsi);
2147 goto unroll_vsi_setup;
2150 /* registering the NAPI handler requires both the queues and
2151 * netdev to be created, which are done in ice_pf_vsi_setup()
2152 * and ice_cfg_netdev() respectively
2156 status = ice_init_mac_fltr(pf);
2158 goto unroll_napi_add;
2166 if (vsi->netdev->reg_state == NETREG_REGISTERED)
2167 unregister_netdev(vsi->netdev);
2168 free_netdev(vsi->netdev);
2175 ice_vsi_free_q_vectors(vsi);
2176 ice_vsi_delete(vsi);
2177 ice_vsi_put_qs(vsi);
2178 pf->q_left_tx += vsi->alloc_txq;
2179 pf->q_left_rx += vsi->alloc_rxq;
2186 * ice_determine_q_usage - Calculate queue distribution
2187 * @pf: board private structure
2189 * Return -ENOMEM if we don't get enough queues for all ports
2191 static void ice_determine_q_usage(struct ice_pf *pf)
2193 u16 q_left_tx, q_left_rx;
2195 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
2196 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
2198 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
2200 /* only 1 Rx queue unless RSS is enabled */
2201 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2204 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
2206 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
2207 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
2211 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
2212 * @pf: board private structure to initialize
2214 static void ice_deinit_pf(struct ice_pf *pf)
2216 ice_service_task_stop(pf);
2217 mutex_destroy(&pf->sw_mutex);
2218 mutex_destroy(&pf->avail_q_mutex);
2222 * ice_init_pf - Initialize general software structures (struct ice_pf)
2223 * @pf: board private structure to initialize
2225 static void ice_init_pf(struct ice_pf *pf)
2227 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
2228 #ifdef CONFIG_PCI_IOV
2229 if (pf->hw.func_caps.common_cap.sr_iov_1_1) {
2230 struct ice_hw *hw = &pf->hw;
2232 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
2233 pf->num_vfs_supported = min_t(int, hw->func_caps.num_allocd_vfs,
2236 #endif /* CONFIG_PCI_IOV */
2238 mutex_init(&pf->sw_mutex);
2239 mutex_init(&pf->avail_q_mutex);
2241 /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
2242 mutex_lock(&pf->avail_q_mutex);
2243 bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
2244 bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
2245 mutex_unlock(&pf->avail_q_mutex);
2247 if (pf->hw.func_caps.common_cap.rss_table_size)
2248 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
2250 /* setup service timer and periodic service task */
2251 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
2252 pf->serv_tmr_period = HZ;
2253 INIT_WORK(&pf->serv_task, ice_service_task);
2254 clear_bit(__ICE_SERVICE_SCHED, pf->state);
2258 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
2259 * @pf: board private structure
2261 * compute the number of MSIX vectors required (v_budget) and request from
2262 * the OS. Return the number of vectors reserved or negative on failure
2264 static int ice_ena_msix_range(struct ice_pf *pf)
2266 int v_left, v_actual, v_budget = 0;
2269 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
2271 /* reserve one vector for miscellaneous handler */
2276 /* reserve vectors for LAN traffic */
2277 pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
2278 v_budget += pf->num_lan_msix;
2279 v_left -= pf->num_lan_msix;
2281 pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
2282 sizeof(*pf->msix_entries), GFP_KERNEL);
2284 if (!pf->msix_entries) {
2289 for (i = 0; i < v_budget; i++)
2290 pf->msix_entries[i].entry = i;
2292 /* actually reserve the vectors */
2293 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
2294 ICE_MIN_MSIX, v_budget);
2297 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
2302 if (v_actual < v_budget) {
2303 dev_warn(&pf->pdev->dev,
2304 "not enough vectors. requested = %d, obtained = %d\n",
2305 v_budget, v_actual);
2306 if (v_actual >= (pf->num_lan_msix + 1)) {
2307 pf->num_avail_sw_msix = v_actual -
2308 (pf->num_lan_msix + 1);
2309 } else if (v_actual >= 2) {
2310 pf->num_lan_msix = 1;
2311 pf->num_avail_sw_msix = v_actual - 2;
2313 pci_disable_msix(pf->pdev);
2322 devm_kfree(&pf->pdev->dev, pf->msix_entries);
2326 pf->num_lan_msix = 0;
2331 * ice_dis_msix - Disable MSI-X interrupt setup in OS
2332 * @pf: board private structure
2334 static void ice_dis_msix(struct ice_pf *pf)
2336 pci_disable_msix(pf->pdev);
2337 devm_kfree(&pf->pdev->dev, pf->msix_entries);
2338 pf->msix_entries = NULL;
2342 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
2343 * @pf: board private structure
2345 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
2349 if (pf->irq_tracker) {
2350 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
2351 pf->irq_tracker = NULL;
2356 * ice_init_interrupt_scheme - Determine proper interrupt scheme
2357 * @pf: board private structure to initialize
2359 static int ice_init_interrupt_scheme(struct ice_pf *pf)
2363 vectors = ice_ena_msix_range(pf);
2368 /* set up vector assignment tracking */
2370 devm_kzalloc(&pf->pdev->dev, sizeof(*pf->irq_tracker) +
2371 (sizeof(u16) * vectors), GFP_KERNEL);
2372 if (!pf->irq_tracker) {
2377 /* populate SW interrupts pool with number of OS granted IRQs. */
2378 pf->num_avail_sw_msix = vectors;
2379 pf->irq_tracker->num_entries = vectors;
2380 pf->irq_tracker->end = pf->irq_tracker->num_entries;
2386 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
2387 * @pf: pointer to the PF structure
2389 * There is no error returned here because the driver should be able to handle
2390 * 128 Byte cache lines, so we only print a warning in case issues are seen,
2391 * specifically with Tx.
2393 static void ice_verify_cacheline_size(struct ice_pf *pf)
2395 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
2396 dev_warn(&pf->pdev->dev,
2397 "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
2398 ICE_CACHE_LINE_BYTES);
2402 * ice_probe - Device initialization routine
2403 * @pdev: PCI device information struct
2404 * @ent: entry in ice_pci_tbl
2406 * Returns 0 on success, negative on failure
2409 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
2411 struct device *dev = &pdev->dev;
2416 /* this driver uses devres, see Documentation/driver-api/driver-model/devres.rst */
2417 err = pcim_enable_device(pdev);
2421 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
2423 dev_err(dev, "BAR0 I/O map error %d\n", err);
2427 pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL);
2431 /* set up for high or low DMA */
2432 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2434 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2436 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
2440 pci_enable_pcie_error_reporting(pdev);
2441 pci_set_master(pdev);
2444 pci_set_drvdata(pdev, pf);
2445 set_bit(__ICE_DOWN, pf->state);
2446 /* Disable service task until DOWN bit is cleared */
2447 set_bit(__ICE_SERVICE_DIS, pf->state);
2450 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
2452 hw->vendor_id = pdev->vendor;
2453 hw->device_id = pdev->device;
2454 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2455 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2456 hw->subsystem_device_id = pdev->subsystem_device;
2457 hw->bus.device = PCI_SLOT(pdev->devfn);
2458 hw->bus.func = PCI_FUNC(pdev->devfn);
2459 ice_set_ctrlq_len(hw);
2461 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
2463 #ifndef CONFIG_DYNAMIC_DEBUG
2465 hw->debug_mask = debug;
2468 err = ice_init_hw(hw);
2470 dev_err(dev, "ice_init_hw failed: %d\n", err);
2472 goto err_exit_unroll;
2475 dev_info(dev, "firmware %d.%d.%05d api %d.%d\n",
2476 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
2477 hw->api_maj_ver, hw->api_min_ver);
2481 err = ice_init_pf_dcb(pf, false);
2483 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2484 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
2486 /* do not fail overall init if DCB init fails */
2490 ice_determine_q_usage(pf);
2492 pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
2493 if (!pf->num_alloc_vsi) {
2495 goto err_init_pf_unroll;
2498 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
2502 goto err_init_pf_unroll;
2505 err = ice_init_interrupt_scheme(pf);
2507 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
2509 goto err_init_interrupt_unroll;
2512 /* Driver is mostly up */
2513 clear_bit(__ICE_DOWN, pf->state);
2515 /* In case of MSIX we are going to setup the misc vector right here
2516 * to handle admin queue events etc. In case of legacy and MSI
2517 * the misc functionality and queue processing is combined in
2518 * the same vector and that gets setup at open.
2520 err = ice_req_irq_msix_misc(pf);
2522 dev_err(dev, "setup of misc vector failed: %d\n", err);
2523 goto err_init_interrupt_unroll;
2526 /* create switch struct for the switch element created by FW on boot */
2527 pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
2528 if (!pf->first_sw) {
2530 goto err_msix_misc_unroll;
2534 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
2536 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
2538 pf->first_sw->pf = pf;
2540 /* record the sw_id available for later use */
2541 pf->first_sw->sw_id = hw->port_info->sw_id;
2543 err = ice_setup_pf_sw(pf);
2545 dev_err(dev, "probe failed due to setup PF switch:%d\n", err);
2546 goto err_alloc_sw_unroll;
2549 clear_bit(__ICE_SERVICE_DIS, pf->state);
2551 /* since everything is good, start the service timer */
2552 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2554 err = ice_init_link_events(pf->hw.port_info);
2556 dev_err(dev, "ice_init_link_events failed: %d\n", err);
2557 goto err_alloc_sw_unroll;
2560 ice_verify_cacheline_size(pf);
2564 err_alloc_sw_unroll:
2565 set_bit(__ICE_SERVICE_DIS, pf->state);
2566 set_bit(__ICE_DOWN, pf->state);
2567 devm_kfree(&pf->pdev->dev, pf->first_sw);
2568 err_msix_misc_unroll:
2569 ice_free_irq_msix_misc(pf);
2570 err_init_interrupt_unroll:
2571 ice_clear_interrupt_scheme(pf);
2572 devm_kfree(dev, pf->vsi);
2577 pci_disable_pcie_error_reporting(pdev);
2582 * ice_remove - Device removal routine
2583 * @pdev: PCI device information struct
2585 static void ice_remove(struct pci_dev *pdev)
2587 struct ice_pf *pf = pci_get_drvdata(pdev);
2593 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
2594 if (!ice_is_reset_in_progress(pf->state))
2599 set_bit(__ICE_DOWN, pf->state);
2600 ice_service_task_stop(pf);
2602 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
2604 ice_vsi_release_all(pf);
2605 ice_free_irq_msix_misc(pf);
2606 ice_for_each_vsi(pf, i) {
2609 ice_vsi_free_q_vectors(pf->vsi[i]);
2611 ice_clear_interrupt_scheme(pf);
2613 ice_deinit_hw(&pf->hw);
2614 pci_disable_pcie_error_reporting(pdev);
2618 * ice_pci_err_detected - warning that PCI error has been detected
2619 * @pdev: PCI device information struct
2620 * @err: the type of PCI error
2622 * Called to warn that something happened on the PCI bus and the error handling
2623 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
2625 static pci_ers_result_t
2626 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err)
2628 struct ice_pf *pf = pci_get_drvdata(pdev);
2631 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
2633 return PCI_ERS_RESULT_DISCONNECT;
2636 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2637 ice_service_task_stop(pf);
2639 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2640 set_bit(__ICE_PFR_REQ, pf->state);
2641 ice_prepare_for_reset(pf);
2645 return PCI_ERS_RESULT_NEED_RESET;
2649 * ice_pci_err_slot_reset - a PCI slot reset has just happened
2650 * @pdev: PCI device information struct
2652 * Called to determine if the driver can recover from the PCI slot reset by
2653 * using a register read to determine if the device is recoverable.
2655 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
2657 struct ice_pf *pf = pci_get_drvdata(pdev);
2658 pci_ers_result_t result;
2662 err = pci_enable_device_mem(pdev);
2665 "Cannot re-enable PCI device after reset, error %d\n",
2667 result = PCI_ERS_RESULT_DISCONNECT;
2669 pci_set_master(pdev);
2670 pci_restore_state(pdev);
2671 pci_save_state(pdev);
2672 pci_wake_from_d3(pdev, false);
2674 /* Check for life */
2675 reg = rd32(&pf->hw, GLGEN_RTRIG);
2677 result = PCI_ERS_RESULT_RECOVERED;
2679 result = PCI_ERS_RESULT_DISCONNECT;
2682 err = pci_cleanup_aer_uncorrect_error_status(pdev);
2685 "pci_cleanup_aer_uncorrect_error_status failed, error %d\n",
2687 /* non-fatal, continue */
2693 * ice_pci_err_resume - restart operations after PCI error recovery
2694 * @pdev: PCI device information struct
2696 * Called to allow the driver to bring things back up after PCI error and/or
2697 * reset recovery have finished
2699 static void ice_pci_err_resume(struct pci_dev *pdev)
2701 struct ice_pf *pf = pci_get_drvdata(pdev);
2705 "%s failed, device is unrecoverable\n", __func__);
2709 if (test_bit(__ICE_SUSPENDED, pf->state)) {
2710 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
2715 ice_do_reset(pf, ICE_RESET_PFR);
2716 ice_service_task_restart(pf);
2717 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2721 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
2722 * @pdev: PCI device information struct
2724 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
2726 struct ice_pf *pf = pci_get_drvdata(pdev);
2728 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2729 ice_service_task_stop(pf);
2731 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2732 set_bit(__ICE_PFR_REQ, pf->state);
2733 ice_prepare_for_reset(pf);
2739 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
2740 * @pdev: PCI device information struct
2742 static void ice_pci_err_reset_done(struct pci_dev *pdev)
2744 ice_pci_err_resume(pdev);
2747 /* ice_pci_tbl - PCI Device ID Table
2749 * Wildcard entries (PCI_ANY_ID) should come last
2750 * Last entry must be all 0s
2752 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
2753 * Class, Class Mask, private data (not used) }
2755 static const struct pci_device_id ice_pci_tbl[] = {
2756 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
2757 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
2758 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
2759 /* required last entry */
2762 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
2764 static const struct pci_error_handlers ice_pci_err_handler = {
2765 .error_detected = ice_pci_err_detected,
2766 .slot_reset = ice_pci_err_slot_reset,
2767 .reset_prepare = ice_pci_err_reset_prepare,
2768 .reset_done = ice_pci_err_reset_done,
2769 .resume = ice_pci_err_resume
2772 static struct pci_driver ice_driver = {
2773 .name = KBUILD_MODNAME,
2774 .id_table = ice_pci_tbl,
2776 .remove = ice_remove,
2777 .sriov_configure = ice_sriov_configure,
2778 .err_handler = &ice_pci_err_handler
2782 * ice_module_init - Driver registration routine
2784 * ice_module_init is the first routine called when the driver is
2785 * loaded. All it does is register with the PCI subsystem.
2787 static int __init ice_module_init(void)
2791 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
2792 pr_info("%s\n", ice_copyright);
2794 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
2796 pr_err("Failed to create workqueue\n");
2800 status = pci_register_driver(&ice_driver);
2802 pr_err("failed to register PCI driver, err %d\n", status);
2803 destroy_workqueue(ice_wq);
2808 module_init(ice_module_init);
2811 * ice_module_exit - Driver exit cleanup routine
2813 * ice_module_exit is called just before the driver is removed
2816 static void __exit ice_module_exit(void)
2818 pci_unregister_driver(&ice_driver);
2819 destroy_workqueue(ice_wq);
2820 pr_info("module unloaded\n");
2822 module_exit(ice_module_exit);
2825 * ice_set_mac_address - NDO callback to set MAC address
2826 * @netdev: network interface device structure
2827 * @pi: pointer to an address structure
2829 * Returns 0 on success, negative on failure
2831 static int ice_set_mac_address(struct net_device *netdev, void *pi)
2833 struct ice_netdev_priv *np = netdev_priv(netdev);
2834 struct ice_vsi *vsi = np->vsi;
2835 struct ice_pf *pf = vsi->back;
2836 struct ice_hw *hw = &pf->hw;
2837 struct sockaddr *addr = pi;
2838 enum ice_status status;
2839 LIST_HEAD(a_mac_list);
2840 LIST_HEAD(r_mac_list);
2845 mac = (u8 *)addr->sa_data;
2847 if (!is_valid_ether_addr(mac))
2848 return -EADDRNOTAVAIL;
2850 if (ether_addr_equal(netdev->dev_addr, mac)) {
2851 netdev_warn(netdev, "already using mac %pM\n", mac);
2855 if (test_bit(__ICE_DOWN, pf->state) ||
2856 ice_is_reset_in_progress(pf->state)) {
2857 netdev_err(netdev, "can't set mac %pM. device not ready\n",
2862 /* When we change the MAC address we also have to change the MAC address
2863 * based filter rules that were created previously for the old MAC
2864 * address. So first, we remove the old filter rule using ice_remove_mac
2865 * and then create a new filter rule using ice_add_mac. Note that for
2866 * both these operations, we first need to form a "list" of MAC
2867 * addresses (even though in this case, we have only 1 MAC address to be
2868 * added/removed) and this done using ice_add_mac_to_list. Depending on
2869 * the ensuing operation this "list" of MAC addresses is either to be
2870 * added or removed from the filter.
2872 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
2874 err = -EADDRNOTAVAIL;
2878 status = ice_remove_mac(hw, &r_mac_list);
2880 err = -EADDRNOTAVAIL;
2884 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
2886 err = -EADDRNOTAVAIL;
2890 status = ice_add_mac(hw, &a_mac_list);
2892 err = -EADDRNOTAVAIL;
2897 /* free list entries */
2898 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
2899 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
2902 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
2907 /* change the netdev's MAC address */
2908 memcpy(netdev->dev_addr, mac, netdev->addr_len);
2909 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
2912 /* write new MAC address to the firmware */
2913 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2914 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
2916 netdev_err(netdev, "can't set MAC %pM. write to firmware failed.\n",
2923 * ice_set_rx_mode - NDO callback to set the netdev filters
2924 * @netdev: network interface device structure
2926 static void ice_set_rx_mode(struct net_device *netdev)
2928 struct ice_netdev_priv *np = netdev_priv(netdev);
2929 struct ice_vsi *vsi = np->vsi;
2934 /* Set the flags to synchronize filters
2935 * ndo_set_rx_mode may be triggered even without a change in netdev
2938 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
2939 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
2940 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
2942 /* schedule our worker thread which will take care of
2943 * applying the new filter changes
2945 ice_service_task_schedule(vsi->back);
2949 * ice_fdb_add - add an entry to the hardware database
2950 * @ndm: the input from the stack
2951 * @tb: pointer to array of nladdr (unused)
2952 * @dev: the net device pointer
2953 * @addr: the MAC address entry being added
2955 * @flags: instructions from stack about fdb operation
2956 * @extack: netlink extended ack
2959 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
2960 struct net_device *dev, const unsigned char *addr, u16 vid,
2961 u16 flags, struct netlink_ext_ack __always_unused *extack)
2966 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
2969 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2970 netdev_err(dev, "FDB only supports static addresses\n");
2974 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2975 err = dev_uc_add_excl(dev, addr);
2976 else if (is_multicast_ether_addr(addr))
2977 err = dev_mc_add_excl(dev, addr);
2981 /* Only return duplicate errors if NLM_F_EXCL is set */
2982 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2989 * ice_fdb_del - delete an entry from the hardware database
2990 * @ndm: the input from the stack
2991 * @tb: pointer to array of nladdr (unused)
2992 * @dev: the net device pointer
2993 * @addr: the MAC address entry being added
2997 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
2998 struct net_device *dev, const unsigned char *addr,
2999 __always_unused u16 vid)
3003 if (ndm->ndm_state & NUD_PERMANENT) {
3004 netdev_err(dev, "FDB only supports static addresses\n");
3008 if (is_unicast_ether_addr(addr))
3009 err = dev_uc_del(dev, addr);
3010 else if (is_multicast_ether_addr(addr))
3011 err = dev_mc_del(dev, addr);
3019 * ice_set_features - set the netdev feature flags
3020 * @netdev: ptr to the netdev being adjusted
3021 * @features: the feature set that the stack is suggesting
3024 ice_set_features(struct net_device *netdev, netdev_features_t features)
3026 struct ice_netdev_priv *np = netdev_priv(netdev);
3027 struct ice_vsi *vsi = np->vsi;
3030 /* Multiple features can be changed in one call so keep features in
3031 * separate if/else statements to guarantee each feature is checked
3033 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
3034 ret = ice_vsi_manage_rss_lut(vsi, true);
3035 else if (!(features & NETIF_F_RXHASH) &&
3036 netdev->features & NETIF_F_RXHASH)
3037 ret = ice_vsi_manage_rss_lut(vsi, false);
3039 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3040 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3041 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3042 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3043 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3044 ret = ice_vsi_manage_vlan_stripping(vsi, false);
3046 if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3047 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3048 ret = ice_vsi_manage_vlan_insertion(vsi);
3049 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3050 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3051 ret = ice_vsi_manage_vlan_insertion(vsi);
3053 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3054 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3055 ret = ice_cfg_vlan_pruning(vsi, true, false);
3056 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3057 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3058 ret = ice_cfg_vlan_pruning(vsi, false, false);
3064 * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
3065 * @vsi: VSI to setup VLAN properties for
3067 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3071 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3072 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3073 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3074 ret = ice_vsi_manage_vlan_insertion(vsi);
3080 * ice_vsi_cfg - Setup the VSI
3081 * @vsi: the VSI being configured
3083 * Return 0 on success and negative value on error
3085 int ice_vsi_cfg(struct ice_vsi *vsi)
3090 ice_set_rx_mode(vsi->netdev);
3092 err = ice_vsi_vlan_setup(vsi);
3097 ice_vsi_cfg_dcb_rings(vsi);
3099 err = ice_vsi_cfg_lan_txqs(vsi);
3101 err = ice_vsi_cfg_rxqs(vsi);
3107 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3108 * @vsi: the VSI being configured
3110 static void ice_napi_enable_all(struct ice_vsi *vsi)
3117 ice_for_each_q_vector(vsi, q_idx) {
3118 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3120 if (q_vector->rx.ring || q_vector->tx.ring)
3121 napi_enable(&q_vector->napi);
3126 * ice_up_complete - Finish the last steps of bringing up a connection
3127 * @vsi: The VSI being configured
3129 * Return 0 on success and negative value on error
3131 static int ice_up_complete(struct ice_vsi *vsi)
3133 struct ice_pf *pf = vsi->back;
3136 ice_vsi_cfg_msix(vsi);
3138 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3139 * Tx queue group list was configured and the context bits were
3140 * programmed using ice_vsi_cfg_txqs
3142 err = ice_vsi_start_rx_rings(vsi);
3146 clear_bit(__ICE_DOWN, vsi->state);
3147 ice_napi_enable_all(vsi);
3148 ice_vsi_ena_irq(vsi);
3150 if (vsi->port_info &&
3151 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
3153 ice_print_link_msg(vsi, true);
3154 netif_tx_start_all_queues(vsi->netdev);
3155 netif_carrier_on(vsi->netdev);
3158 ice_service_task_schedule(pf);
3164 * ice_up - Bring the connection back up after being down
3165 * @vsi: VSI being configured
3167 int ice_up(struct ice_vsi *vsi)
3171 err = ice_vsi_cfg(vsi);
3173 err = ice_up_complete(vsi);
3179 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
3180 * @ring: Tx or Rx ring to read stats from
3181 * @pkts: packets stats counter
3182 * @bytes: bytes stats counter
3184 * This function fetches stats from the ring considering the atomic operations
3185 * that needs to be performed to read u64 values in 32 bit machine.
3188 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
3197 start = u64_stats_fetch_begin_irq(&ring->syncp);
3198 *pkts = ring->stats.pkts;
3199 *bytes = ring->stats.bytes;
3200 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3204 * ice_update_vsi_ring_stats - Update VSI stats counters
3205 * @vsi: the VSI to be updated
3207 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
3209 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
3210 struct ice_ring *ring;
3214 /* reset netdev stats */
3215 vsi_stats->tx_packets = 0;
3216 vsi_stats->tx_bytes = 0;
3217 vsi_stats->rx_packets = 0;
3218 vsi_stats->rx_bytes = 0;
3220 /* reset non-netdev (extended) stats */
3221 vsi->tx_restart = 0;
3223 vsi->tx_linearize = 0;
3224 vsi->rx_buf_failed = 0;
3225 vsi->rx_page_failed = 0;
3229 /* update Tx rings counters */
3230 ice_for_each_txq(vsi, i) {
3231 ring = READ_ONCE(vsi->tx_rings[i]);
3232 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3233 vsi_stats->tx_packets += pkts;
3234 vsi_stats->tx_bytes += bytes;
3235 vsi->tx_restart += ring->tx_stats.restart_q;
3236 vsi->tx_busy += ring->tx_stats.tx_busy;
3237 vsi->tx_linearize += ring->tx_stats.tx_linearize;
3240 /* update Rx rings counters */
3241 ice_for_each_rxq(vsi, i) {
3242 ring = READ_ONCE(vsi->rx_rings[i]);
3243 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3244 vsi_stats->rx_packets += pkts;
3245 vsi_stats->rx_bytes += bytes;
3246 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
3247 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
3254 * ice_update_vsi_stats - Update VSI stats counters
3255 * @vsi: the VSI to be updated
3257 static void ice_update_vsi_stats(struct ice_vsi *vsi)
3259 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
3260 struct ice_eth_stats *cur_es = &vsi->eth_stats;
3261 struct ice_pf *pf = vsi->back;
3263 if (test_bit(__ICE_DOWN, vsi->state) ||
3264 test_bit(__ICE_CFG_BUSY, pf->state))
3267 /* get stats as recorded by Tx/Rx rings */
3268 ice_update_vsi_ring_stats(vsi);
3270 /* get VSI stats as recorded by the hardware */
3271 ice_update_eth_stats(vsi);
3273 cur_ns->tx_errors = cur_es->tx_errors;
3274 cur_ns->rx_dropped = cur_es->rx_discards;
3275 cur_ns->tx_dropped = cur_es->tx_discards;
3276 cur_ns->multicast = cur_es->rx_multicast;
3278 /* update some more netdev stats if this is main VSI */
3279 if (vsi->type == ICE_VSI_PF) {
3280 cur_ns->rx_crc_errors = pf->stats.crc_errors;
3281 cur_ns->rx_errors = pf->stats.crc_errors +
3282 pf->stats.illegal_bytes;
3283 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
3284 /* record drops from the port level */
3285 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
3290 * ice_update_pf_stats - Update PF port stats counters
3291 * @pf: PF whose stats needs to be updated
3293 static void ice_update_pf_stats(struct ice_pf *pf)
3295 struct ice_hw_port_stats *prev_ps, *cur_ps;
3296 struct ice_hw *hw = &pf->hw;
3299 prev_ps = &pf->stats_prev;
3300 cur_ps = &pf->stats;
3303 ice_stat_update40(hw, GLPRT_GORCL(pf_id), pf->stat_prev_loaded,
3304 &prev_ps->eth.rx_bytes,
3305 &cur_ps->eth.rx_bytes);
3307 ice_stat_update40(hw, GLPRT_UPRCL(pf_id), pf->stat_prev_loaded,
3308 &prev_ps->eth.rx_unicast,
3309 &cur_ps->eth.rx_unicast);
3311 ice_stat_update40(hw, GLPRT_MPRCL(pf_id), pf->stat_prev_loaded,
3312 &prev_ps->eth.rx_multicast,
3313 &cur_ps->eth.rx_multicast);
3315 ice_stat_update40(hw, GLPRT_BPRCL(pf_id), pf->stat_prev_loaded,
3316 &prev_ps->eth.rx_broadcast,
3317 &cur_ps->eth.rx_broadcast);
3319 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
3320 &prev_ps->eth.rx_discards,
3321 &cur_ps->eth.rx_discards);
3323 ice_stat_update40(hw, GLPRT_GOTCL(pf_id), pf->stat_prev_loaded,
3324 &prev_ps->eth.tx_bytes,
3325 &cur_ps->eth.tx_bytes);
3327 ice_stat_update40(hw, GLPRT_UPTCL(pf_id), pf->stat_prev_loaded,
3328 &prev_ps->eth.tx_unicast,
3329 &cur_ps->eth.tx_unicast);
3331 ice_stat_update40(hw, GLPRT_MPTCL(pf_id), pf->stat_prev_loaded,
3332 &prev_ps->eth.tx_multicast,
3333 &cur_ps->eth.tx_multicast);
3335 ice_stat_update40(hw, GLPRT_BPTCL(pf_id), pf->stat_prev_loaded,
3336 &prev_ps->eth.tx_broadcast,
3337 &cur_ps->eth.tx_broadcast);
3339 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
3340 &prev_ps->tx_dropped_link_down,
3341 &cur_ps->tx_dropped_link_down);
3343 ice_stat_update40(hw, GLPRT_PRC64L(pf_id), pf->stat_prev_loaded,
3344 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
3346 ice_stat_update40(hw, GLPRT_PRC127L(pf_id), pf->stat_prev_loaded,
3347 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
3349 ice_stat_update40(hw, GLPRT_PRC255L(pf_id), pf->stat_prev_loaded,
3350 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
3352 ice_stat_update40(hw, GLPRT_PRC511L(pf_id), pf->stat_prev_loaded,
3353 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
3355 ice_stat_update40(hw, GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
3356 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
3358 ice_stat_update40(hw, GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
3359 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
3361 ice_stat_update40(hw, GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
3362 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
3364 ice_stat_update40(hw, GLPRT_PTC64L(pf_id), pf->stat_prev_loaded,
3365 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
3367 ice_stat_update40(hw, GLPRT_PTC127L(pf_id), pf->stat_prev_loaded,
3368 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
3370 ice_stat_update40(hw, GLPRT_PTC255L(pf_id), pf->stat_prev_loaded,
3371 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
3373 ice_stat_update40(hw, GLPRT_PTC511L(pf_id), pf->stat_prev_loaded,
3374 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
3376 ice_stat_update40(hw, GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
3377 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
3379 ice_stat_update40(hw, GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
3380 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
3382 ice_stat_update40(hw, GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
3383 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
3385 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
3386 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
3388 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
3389 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
3391 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
3392 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
3394 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
3395 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
3397 ice_update_dcb_stats(pf);
3399 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
3400 &prev_ps->crc_errors, &cur_ps->crc_errors);
3402 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
3403 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
3405 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
3406 &prev_ps->mac_local_faults,
3407 &cur_ps->mac_local_faults);
3409 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
3410 &prev_ps->mac_remote_faults,
3411 &cur_ps->mac_remote_faults);
3413 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
3414 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
3416 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
3417 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
3419 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
3420 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
3422 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
3423 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
3425 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
3426 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
3428 pf->stat_prev_loaded = true;
3432 * ice_get_stats64 - get statistics for network device structure
3433 * @netdev: network interface device structure
3434 * @stats: main device statistics structure
3437 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
3439 struct ice_netdev_priv *np = netdev_priv(netdev);
3440 struct rtnl_link_stats64 *vsi_stats;
3441 struct ice_vsi *vsi = np->vsi;
3443 vsi_stats = &vsi->net_stats;
3445 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
3447 /* netdev packet/byte stats come from ring counter. These are obtained
3448 * by summing up ring counters (done by ice_update_vsi_ring_stats).
3450 ice_update_vsi_ring_stats(vsi);
3451 stats->tx_packets = vsi_stats->tx_packets;
3452 stats->tx_bytes = vsi_stats->tx_bytes;
3453 stats->rx_packets = vsi_stats->rx_packets;
3454 stats->rx_bytes = vsi_stats->rx_bytes;
3456 /* The rest of the stats can be read from the hardware but instead we
3457 * just return values that the watchdog task has already obtained from
3460 stats->multicast = vsi_stats->multicast;
3461 stats->tx_errors = vsi_stats->tx_errors;
3462 stats->tx_dropped = vsi_stats->tx_dropped;
3463 stats->rx_errors = vsi_stats->rx_errors;
3464 stats->rx_dropped = vsi_stats->rx_dropped;
3465 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
3466 stats->rx_length_errors = vsi_stats->rx_length_errors;
3470 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3471 * @vsi: VSI having NAPI disabled
3473 static void ice_napi_disable_all(struct ice_vsi *vsi)
3480 ice_for_each_q_vector(vsi, q_idx) {
3481 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3483 if (q_vector->rx.ring || q_vector->tx.ring)
3484 napi_disable(&q_vector->napi);
3489 * ice_down - Shutdown the connection
3490 * @vsi: The VSI being stopped
3492 int ice_down(struct ice_vsi *vsi)
3494 int i, tx_err, rx_err, link_err = 0;
3496 /* Caller of this function is expected to set the
3497 * vsi->state __ICE_DOWN bit
3500 netif_carrier_off(vsi->netdev);
3501 netif_tx_disable(vsi->netdev);
3504 ice_vsi_dis_irq(vsi);
3506 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
3508 netdev_err(vsi->netdev,
3509 "Failed stop Tx rings, VSI %d error %d\n",
3510 vsi->vsi_num, tx_err);
3512 rx_err = ice_vsi_stop_rx_rings(vsi);
3514 netdev_err(vsi->netdev,
3515 "Failed stop Rx rings, VSI %d error %d\n",
3516 vsi->vsi_num, rx_err);
3518 ice_napi_disable_all(vsi);
3520 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
3521 link_err = ice_force_phys_link_state(vsi, false);
3523 netdev_err(vsi->netdev,
3524 "Failed to set physical link down, VSI %d error %d\n",
3525 vsi->vsi_num, link_err);
3528 ice_for_each_txq(vsi, i)
3529 ice_clean_tx_ring(vsi->tx_rings[i]);
3531 ice_for_each_rxq(vsi, i)
3532 ice_clean_rx_ring(vsi->rx_rings[i]);
3534 if (tx_err || rx_err || link_err) {
3535 netdev_err(vsi->netdev,
3536 "Failed to close VSI 0x%04X on switch 0x%04X\n",
3537 vsi->vsi_num, vsi->vsw->sw_id);
3545 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
3546 * @vsi: VSI having resources allocated
3548 * Return 0 on success, negative on failure
3550 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
3554 if (!vsi->num_txq) {
3555 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
3560 ice_for_each_txq(vsi, i) {
3561 vsi->tx_rings[i]->netdev = vsi->netdev;
3562 err = ice_setup_tx_ring(vsi->tx_rings[i]);
3571 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
3572 * @vsi: VSI having resources allocated
3574 * Return 0 on success, negative on failure
3576 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
3580 if (!vsi->num_rxq) {
3581 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
3586 ice_for_each_rxq(vsi, i) {
3587 vsi->rx_rings[i]->netdev = vsi->netdev;
3588 err = ice_setup_rx_ring(vsi->rx_rings[i]);
3597 * ice_vsi_open - Called when a network interface is made active
3598 * @vsi: the VSI to open
3600 * Initialization of the VSI
3602 * Returns 0 on success, negative value on error
3604 static int ice_vsi_open(struct ice_vsi *vsi)
3606 char int_name[ICE_INT_NAME_STR_LEN];
3607 struct ice_pf *pf = vsi->back;
3610 /* allocate descriptors */
3611 err = ice_vsi_setup_tx_rings(vsi);
3615 err = ice_vsi_setup_rx_rings(vsi);
3619 err = ice_vsi_cfg(vsi);
3623 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3624 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
3625 err = ice_vsi_req_irq_msix(vsi, int_name);
3629 /* Notify the stack of the actual queue counts. */
3630 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
3634 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
3638 err = ice_up_complete(vsi);
3640 goto err_up_complete;
3647 ice_vsi_free_irq(vsi);
3649 ice_vsi_free_rx_rings(vsi);
3651 ice_vsi_free_tx_rings(vsi);
3657 * ice_vsi_release_all - Delete all VSIs
3658 * @pf: PF from which all VSIs are being removed
3660 static void ice_vsi_release_all(struct ice_pf *pf)
3667 ice_for_each_vsi(pf, i) {
3671 err = ice_vsi_release(pf->vsi[i]);
3673 dev_dbg(&pf->pdev->dev,
3674 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
3675 i, err, pf->vsi[i]->vsi_num);
3680 * ice_ena_vsi - resume a VSI
3681 * @vsi: the VSI being resume
3682 * @locked: is the rtnl_lock already held
3684 static int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
3688 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
3691 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
3693 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
3694 struct net_device *netd = vsi->netdev;
3696 if (netif_running(vsi->netdev)) {
3698 err = netd->netdev_ops->ndo_open(netd);
3701 err = netd->netdev_ops->ndo_open(netd);
3705 err = ice_vsi_open(vsi);
3713 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
3715 * @locked: is the rtnl_lock already held
3718 int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3720 static int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3721 #endif /* CONFIG_DCB */
3725 ice_for_each_vsi(pf, v)
3727 if (ice_ena_vsi(pf->vsi[v], locked))
3734 * ice_vsi_rebuild_all - rebuild all VSIs in PF
3737 static int ice_vsi_rebuild_all(struct ice_pf *pf)
3741 /* loop through pf->vsi array and reinit the VSI if found */
3742 ice_for_each_vsi(pf, i) {
3748 err = ice_vsi_rebuild(pf->vsi[i]);
3750 dev_err(&pf->pdev->dev,
3751 "VSI at index %d rebuild failed\n",
3756 dev_info(&pf->pdev->dev,
3757 "VSI at index %d rebuilt. vsi_num = 0x%x\n",
3758 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3765 * ice_vsi_replay_all - replay all VSIs configuration in the PF
3768 static int ice_vsi_replay_all(struct ice_pf *pf)
3770 struct ice_hw *hw = &pf->hw;
3771 enum ice_status ret;
3774 /* loop through pf->vsi array and replay the VSI if found */
3775 ice_for_each_vsi(pf, i) {
3779 ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
3781 dev_err(&pf->pdev->dev,
3782 "VSI at index %d replay failed %d\n",
3783 pf->vsi[i]->idx, ret);
3787 /* Re-map HW VSI number, using VSI handle that has been
3788 * previously validated in ice_replay_vsi() call above
3790 pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
3792 dev_info(&pf->pdev->dev,
3793 "VSI at index %d filter replayed successfully - vsi_num %i\n",
3794 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3797 /* Clean up replay filter after successful re-configuration */
3798 ice_replay_post(hw);
3803 * ice_rebuild - rebuild after reset
3804 * @pf: PF to rebuild
3806 static void ice_rebuild(struct ice_pf *pf)
3808 struct device *dev = &pf->pdev->dev;
3809 struct ice_hw *hw = &pf->hw;
3810 enum ice_status ret;
3813 if (test_bit(__ICE_DOWN, pf->state))
3814 goto clear_recovery;
3816 dev_dbg(dev, "rebuilding PF\n");
3818 ret = ice_init_all_ctrlq(hw);
3820 dev_err(dev, "control queues init failed %d\n", ret);
3821 goto err_init_ctrlq;
3824 ret = ice_clear_pf_cfg(hw);
3826 dev_err(dev, "clear PF configuration failed %d\n", ret);
3827 goto err_init_ctrlq;
3830 ice_clear_pxe_mode(hw);
3832 ret = ice_get_caps(hw);
3834 dev_err(dev, "ice_get_caps failed %d\n", ret);
3835 goto err_init_ctrlq;
3838 err = ice_sched_init_port(hw->port_info);
3840 goto err_sched_init_port;
3842 ice_dcb_rebuild(pf);
3844 err = ice_vsi_rebuild_all(pf);
3846 dev_err(dev, "ice_vsi_rebuild_all failed\n");
3847 goto err_vsi_rebuild;
3850 err = ice_update_link_info(hw->port_info);
3852 dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
3854 /* Replay all VSIs Configuration, including filters after reset */
3855 if (ice_vsi_replay_all(pf)) {
3856 dev_err(&pf->pdev->dev,
3857 "error replaying VSI configurations with switch filter rules\n");
3858 goto err_vsi_rebuild;
3861 /* start misc vector */
3862 err = ice_req_irq_msix_misc(pf);
3864 dev_err(dev, "misc vector setup failed: %d\n", err);
3865 goto err_vsi_rebuild;
3868 /* restart the VSIs that were rebuilt and running before the reset */
3869 err = ice_pf_ena_all_vsi(pf, false);
3871 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
3872 /* no need to disable VSIs in tear down path in ice_rebuild()
3873 * since its already taken care in ice_vsi_open()
3875 goto err_vsi_rebuild;
3878 ice_for_each_vsi(pf, i) {
3881 if (!pf->vsi[i] || pf->vsi[i]->type != ICE_VSI_PF)
3883 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
3885 netif_carrier_on(pf->vsi[i]->netdev);
3886 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
3888 netif_carrier_off(pf->vsi[i]->netdev);
3889 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
3893 /* if we get here, reset flow is successful */
3894 clear_bit(__ICE_RESET_FAILED, pf->state);
3898 ice_vsi_release_all(pf);
3899 err_sched_init_port:
3900 ice_sched_cleanup_all(hw);
3902 ice_shutdown_all_ctrlq(hw);
3903 set_bit(__ICE_RESET_FAILED, pf->state);
3905 /* set this bit in PF state to control service task scheduling */
3906 set_bit(__ICE_NEEDS_RESTART, pf->state);
3907 dev_err(dev, "Rebuild failed, unload and reload driver\n");
3911 * ice_change_mtu - NDO callback to change the MTU
3912 * @netdev: network interface device structure
3913 * @new_mtu: new value for maximum frame size
3915 * Returns 0 on success, negative on failure
3917 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
3919 struct ice_netdev_priv *np = netdev_priv(netdev);
3920 struct ice_vsi *vsi = np->vsi;
3921 struct ice_pf *pf = vsi->back;
3924 if (new_mtu == netdev->mtu) {
3925 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
3929 if (new_mtu < netdev->min_mtu) {
3930 netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
3933 } else if (new_mtu > netdev->max_mtu) {
3934 netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
3938 /* if a reset is in progress, wait for some time for it to complete */
3940 if (ice_is_reset_in_progress(pf->state)) {
3942 usleep_range(1000, 2000);
3947 } while (count < 100);
3950 netdev_err(netdev, "can't change MTU. Device is busy\n");
3954 netdev->mtu = new_mtu;
3956 /* if VSI is up, bring it down and then back up */
3957 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
3960 err = ice_down(vsi);
3962 netdev_err(netdev, "change MTU if_up err %d\n", err);
3968 netdev_err(netdev, "change MTU if_up err %d\n", err);
3973 netdev_info(netdev, "changed MTU to %d\n", new_mtu);
3978 * ice_set_rss - Set RSS keys and lut
3979 * @vsi: Pointer to VSI structure
3980 * @seed: RSS hash seed
3981 * @lut: Lookup table
3982 * @lut_size: Lookup table size
3984 * Returns 0 on success, negative on failure
3986 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3988 struct ice_pf *pf = vsi->back;
3989 struct ice_hw *hw = &pf->hw;
3990 enum ice_status status;
3993 struct ice_aqc_get_set_rss_keys *buf =
3994 (struct ice_aqc_get_set_rss_keys *)seed;
3996 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
3999 dev_err(&pf->pdev->dev,
4000 "Cannot set RSS key, err %d aq_err %d\n",
4001 status, hw->adminq.rq_last_status);
4007 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4010 dev_err(&pf->pdev->dev,
4011 "Cannot set RSS lut, err %d aq_err %d\n",
4012 status, hw->adminq.rq_last_status);
4021 * ice_get_rss - Get RSS keys and lut
4022 * @vsi: Pointer to VSI structure
4023 * @seed: Buffer to store the keys
4024 * @lut: Buffer to store the lookup table entries
4025 * @lut_size: Size of buffer to store the lookup table entries
4027 * Returns 0 on success, negative on failure
4029 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4031 struct ice_pf *pf = vsi->back;
4032 struct ice_hw *hw = &pf->hw;
4033 enum ice_status status;
4036 struct ice_aqc_get_set_rss_keys *buf =
4037 (struct ice_aqc_get_set_rss_keys *)seed;
4039 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
4041 dev_err(&pf->pdev->dev,
4042 "Cannot get RSS key, err %d aq_err %d\n",
4043 status, hw->adminq.rq_last_status);
4049 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4052 dev_err(&pf->pdev->dev,
4053 "Cannot get RSS lut, err %d aq_err %d\n",
4054 status, hw->adminq.rq_last_status);
4063 * ice_bridge_getlink - Get the hardware bridge mode
4066 * @seq: RTNL message seq
4067 * @dev: the netdev being configured
4068 * @filter_mask: filter mask passed in
4069 * @nlflags: netlink flags passed in
4071 * Return the bridge mode (VEB/VEPA)
4074 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4075 struct net_device *dev, u32 filter_mask, int nlflags)
4077 struct ice_netdev_priv *np = netdev_priv(dev);
4078 struct ice_vsi *vsi = np->vsi;
4079 struct ice_pf *pf = vsi->back;
4082 bmode = pf->first_sw->bridge_mode;
4084 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
4089 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
4090 * @vsi: Pointer to VSI structure
4091 * @bmode: Hardware bridge mode (VEB/VEPA)
4093 * Returns 0 on success, negative on failure
4095 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
4097 struct device *dev = &vsi->back->pdev->dev;
4098 struct ice_aqc_vsi_props *vsi_props;
4099 struct ice_hw *hw = &vsi->back->hw;
4100 struct ice_vsi_ctx *ctxt;
4101 enum ice_status status;
4104 vsi_props = &vsi->info;
4106 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
4110 ctxt->info = vsi->info;
4112 if (bmode == BRIDGE_MODE_VEB)
4113 /* change from VEPA to VEB mode */
4114 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4116 /* change from VEB to VEPA mode */
4117 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4118 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4120 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4122 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
4123 bmode, status, hw->adminq.sq_last_status);
4127 /* Update sw flags for book keeping */
4128 vsi_props->sw_flags = ctxt->info.sw_flags;
4131 devm_kfree(dev, ctxt);
4136 * ice_bridge_setlink - Set the hardware bridge mode
4137 * @dev: the netdev being configured
4138 * @nlh: RTNL message
4139 * @flags: bridge setlink flags
4140 * @extack: netlink extended ack
4142 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
4143 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
4144 * not already set for all VSIs connected to this switch. And also update the
4145 * unicast switch filter rules for the corresponding switch of the netdev.
4148 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4149 u16 __always_unused flags,
4150 struct netlink_ext_ack __always_unused *extack)
4152 struct ice_netdev_priv *np = netdev_priv(dev);
4153 struct ice_pf *pf = np->vsi->back;
4154 struct nlattr *attr, *br_spec;
4155 struct ice_hw *hw = &pf->hw;
4156 enum ice_status status;
4157 struct ice_sw *pf_sw;
4158 int rem, v, err = 0;
4160 pf_sw = pf->first_sw;
4161 /* find the attribute in the netlink message */
4162 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4164 nla_for_each_nested(attr, br_spec, rem) {
4167 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4169 mode = nla_get_u16(attr);
4170 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4172 /* Continue if bridge mode is not being flipped */
4173 if (mode == pf_sw->bridge_mode)
4175 /* Iterates through the PF VSI list and update the loopback
4178 ice_for_each_vsi(pf, v) {
4181 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
4186 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
4187 /* Update the unicast switch filter rules for the corresponding
4188 * switch of the netdev
4190 status = ice_update_sw_rule_bridge_mode(hw);
4192 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n",
4193 mode, status, hw->adminq.sq_last_status);
4194 /* revert hw->evb_veb */
4195 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
4199 pf_sw->bridge_mode = mode;
4206 * ice_tx_timeout - Respond to a Tx Hang
4207 * @netdev: network interface device structure
4209 static void ice_tx_timeout(struct net_device *netdev)
4211 struct ice_netdev_priv *np = netdev_priv(netdev);
4212 struct ice_ring *tx_ring = NULL;
4213 struct ice_vsi *vsi = np->vsi;
4214 struct ice_pf *pf = vsi->back;
4215 int hung_queue = -1;
4218 pf->tx_timeout_count++;
4220 /* find the stopped queue the same way dev_watchdog() does */
4221 for (i = 0; i < netdev->num_tx_queues; i++) {
4222 unsigned long trans_start;
4223 struct netdev_queue *q;
4225 q = netdev_get_tx_queue(netdev, i);
4226 trans_start = q->trans_start;
4227 if (netif_xmit_stopped(q) &&
4229 trans_start + netdev->watchdog_timeo)) {
4235 if (i == netdev->num_tx_queues)
4236 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
4238 /* now that we have an index, find the tx_ring struct */
4239 for (i = 0; i < vsi->num_txq; i++)
4240 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4241 if (hung_queue == vsi->tx_rings[i]->q_index) {
4242 tx_ring = vsi->tx_rings[i];
4246 /* Reset recovery level if enough time has elapsed after last timeout.
4247 * Also ensure no new reset action happens before next timeout period.
4249 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
4250 pf->tx_timeout_recovery_level = 1;
4251 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
4252 netdev->watchdog_timeo)))
4256 struct ice_hw *hw = &pf->hw;
4259 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[hung_queue])) &
4260 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
4261 /* Read interrupt register */
4262 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
4264 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
4265 vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
4266 head, tx_ring->next_to_use, val);
4269 pf->tx_timeout_last_recovery = jiffies;
4270 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
4271 pf->tx_timeout_recovery_level, hung_queue);
4273 switch (pf->tx_timeout_recovery_level) {
4275 set_bit(__ICE_PFR_REQ, pf->state);
4278 set_bit(__ICE_CORER_REQ, pf->state);
4281 set_bit(__ICE_GLOBR_REQ, pf->state);
4284 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
4285 set_bit(__ICE_DOWN, pf->state);
4286 set_bit(__ICE_NEEDS_RESTART, vsi->state);
4287 set_bit(__ICE_SERVICE_DIS, pf->state);
4291 ice_service_task_schedule(pf);
4292 pf->tx_timeout_recovery_level++;
4296 * ice_open - Called when a network interface becomes active
4297 * @netdev: network interface device structure
4299 * The open entry point is called when a network interface is made
4300 * active by the system (IFF_UP). At this point all resources needed
4301 * for transmit and receive operations are allocated, the interrupt
4302 * handler is registered with the OS, the netdev watchdog is enabled,
4303 * and the stack is notified that the interface is ready.
4305 * Returns 0 on success, negative value on failure
4307 int ice_open(struct net_device *netdev)
4309 struct ice_netdev_priv *np = netdev_priv(netdev);
4310 struct ice_vsi *vsi = np->vsi;
4311 struct ice_port_info *pi;
4314 if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
4315 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
4319 netif_carrier_off(netdev);
4321 pi = vsi->port_info;
4322 err = ice_update_link_info(pi);
4324 netdev_err(netdev, "Failed to get link info, error %d\n",
4329 /* Set PHY if there is media, otherwise, turn off PHY */
4330 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
4331 err = ice_force_phys_link_state(vsi, true);
4334 "Failed to set physical link up, error %d\n",
4339 err = ice_aq_set_link_restart_an(pi, false, NULL);
4341 netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
4345 set_bit(ICE_FLAG_NO_MEDIA, vsi->back->flags);
4348 err = ice_vsi_open(vsi);
4350 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
4351 vsi->vsi_num, vsi->vsw->sw_id);
4356 * ice_stop - Disables a network interface
4357 * @netdev: network interface device structure
4359 * The stop entry point is called when an interface is de-activated by the OS,
4360 * and the netdevice enters the DOWN state. The hardware is still under the
4361 * driver's control, but the netdev interface is disabled.
4363 * Returns success only - not allowed to fail
4365 int ice_stop(struct net_device *netdev)
4367 struct ice_netdev_priv *np = netdev_priv(netdev);
4368 struct ice_vsi *vsi = np->vsi;
4376 * ice_features_check - Validate encapsulated packet conforms to limits
4378 * @netdev: This port's netdev
4379 * @features: Offload features that the stack believes apply
4381 static netdev_features_t
4382 ice_features_check(struct sk_buff *skb,
4383 struct net_device __always_unused *netdev,
4384 netdev_features_t features)
4388 /* No point in doing any of this if neither checksum nor GSO are
4389 * being requested for this frame. We can rule out both by just
4390 * checking for CHECKSUM_PARTIAL
4392 if (skb->ip_summed != CHECKSUM_PARTIAL)
4395 /* We cannot support GSO if the MSS is going to be less than
4396 * 64 bytes. If it is then we need to drop support for GSO.
4398 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4399 features &= ~NETIF_F_GSO_MASK;
4401 len = skb_network_header(skb) - skb->data;
4402 if (len & ~(ICE_TXD_MACLEN_MAX))
4403 goto out_rm_features;
4405 len = skb_transport_header(skb) - skb_network_header(skb);
4406 if (len & ~(ICE_TXD_IPLEN_MAX))
4407 goto out_rm_features;
4409 if (skb->encapsulation) {
4410 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4411 if (len & ~(ICE_TXD_L4LEN_MAX))
4412 goto out_rm_features;
4414 len = skb_inner_transport_header(skb) -
4415 skb_inner_network_header(skb);
4416 if (len & ~(ICE_TXD_IPLEN_MAX))
4417 goto out_rm_features;
4422 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4425 static const struct net_device_ops ice_netdev_ops = {
4426 .ndo_open = ice_open,
4427 .ndo_stop = ice_stop,
4428 .ndo_start_xmit = ice_start_xmit,
4429 .ndo_features_check = ice_features_check,
4430 .ndo_set_rx_mode = ice_set_rx_mode,
4431 .ndo_set_mac_address = ice_set_mac_address,
4432 .ndo_validate_addr = eth_validate_addr,
4433 .ndo_change_mtu = ice_change_mtu,
4434 .ndo_get_stats64 = ice_get_stats64,
4435 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
4436 .ndo_set_vf_mac = ice_set_vf_mac,
4437 .ndo_get_vf_config = ice_get_vf_cfg,
4438 .ndo_set_vf_trust = ice_set_vf_trust,
4439 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
4440 .ndo_set_vf_link_state = ice_set_vf_link_state,
4441 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
4442 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
4443 .ndo_set_features = ice_set_features,
4444 .ndo_bridge_getlink = ice_bridge_getlink,
4445 .ndo_bridge_setlink = ice_bridge_setlink,
4446 .ndo_fdb_add = ice_fdb_add,
4447 .ndo_fdb_del = ice_fdb_del,
4448 .ndo_tx_timeout = ice_tx_timeout,