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.4-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;
492 /* already prepared for reset */
493 if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
496 /* Notify VFs of impending reset */
497 if (ice_check_sq_alive(hw, &hw->mailboxq))
498 ice_vc_notify_reset(pf);
500 /* disable the VSIs and their queues that are not already DOWN */
501 ice_pf_dis_all_vsi(pf, false);
504 ice_sched_clear_port(hw->port_info);
506 ice_shutdown_all_ctrlq(hw);
508 set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
512 * ice_do_reset - Initiate one of many types of resets
513 * @pf: board private structure
514 * @reset_type: reset type requested
515 * before this function was called.
517 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
519 struct device *dev = &pf->pdev->dev;
520 struct ice_hw *hw = &pf->hw;
522 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
523 WARN_ON(in_interrupt());
525 ice_prepare_for_reset(pf);
527 /* trigger the reset */
528 if (ice_reset(hw, reset_type)) {
529 dev_err(dev, "reset %d failed\n", reset_type);
530 set_bit(__ICE_RESET_FAILED, pf->state);
531 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
532 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
533 clear_bit(__ICE_PFR_REQ, pf->state);
534 clear_bit(__ICE_CORER_REQ, pf->state);
535 clear_bit(__ICE_GLOBR_REQ, pf->state);
539 /* PFR is a bit of a special case because it doesn't result in an OICR
540 * interrupt. So for PFR, rebuild after the reset and clear the reset-
541 * associated state bits.
543 if (reset_type == ICE_RESET_PFR) {
546 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
547 clear_bit(__ICE_PFR_REQ, pf->state);
548 ice_reset_all_vfs(pf, true);
553 * ice_reset_subtask - Set up for resetting the device and driver
554 * @pf: board private structure
556 static void ice_reset_subtask(struct ice_pf *pf)
558 enum ice_reset_req reset_type = ICE_RESET_INVAL;
560 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
561 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
562 * of reset is pending and sets bits in pf->state indicating the reset
563 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
564 * prepare for pending reset if not already (for PF software-initiated
565 * global resets the software should already be prepared for it as
566 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
567 * by firmware or software on other PFs, that bit is not set so prepare
568 * for the reset now), poll for reset done, rebuild and return.
570 if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
571 /* Perform the largest reset requested */
572 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
573 reset_type = ICE_RESET_CORER;
574 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
575 reset_type = ICE_RESET_GLOBR;
576 /* return if no valid reset type requested */
577 if (reset_type == ICE_RESET_INVAL)
579 ice_prepare_for_reset(pf);
581 /* make sure we are ready to rebuild */
582 if (ice_check_reset(&pf->hw)) {
583 set_bit(__ICE_RESET_FAILED, pf->state);
585 /* done with reset. start rebuild */
586 pf->hw.reset_ongoing = false;
588 /* clear bit to resume normal operations, but
589 * ICE_NEEDS_RESTART bit is set in case rebuild failed
591 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
592 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
593 clear_bit(__ICE_PFR_REQ, pf->state);
594 clear_bit(__ICE_CORER_REQ, pf->state);
595 clear_bit(__ICE_GLOBR_REQ, pf->state);
596 ice_reset_all_vfs(pf, true);
602 /* No pending resets to finish processing. Check for new resets */
603 if (test_bit(__ICE_PFR_REQ, pf->state))
604 reset_type = ICE_RESET_PFR;
605 if (test_bit(__ICE_CORER_REQ, pf->state))
606 reset_type = ICE_RESET_CORER;
607 if (test_bit(__ICE_GLOBR_REQ, pf->state))
608 reset_type = ICE_RESET_GLOBR;
609 /* If no valid reset type requested just return */
610 if (reset_type == ICE_RESET_INVAL)
613 /* reset if not already down or busy */
614 if (!test_bit(__ICE_DOWN, pf->state) &&
615 !test_bit(__ICE_CFG_BUSY, pf->state)) {
616 ice_do_reset(pf, reset_type);
621 * ice_print_link_msg - print link up or down message
622 * @vsi: the VSI whose link status is being queried
623 * @isup: boolean for if the link is now up or down
625 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
627 struct ice_aqc_get_phy_caps_data *caps;
628 enum ice_status status;
637 if (vsi->current_isup == isup)
640 vsi->current_isup = isup;
643 netdev_info(vsi->netdev, "NIC Link is Down\n");
647 switch (vsi->port_info->phy.link_info.link_speed) {
648 case ICE_AQ_LINK_SPEED_100GB:
651 case ICE_AQ_LINK_SPEED_50GB:
654 case ICE_AQ_LINK_SPEED_40GB:
657 case ICE_AQ_LINK_SPEED_25GB:
660 case ICE_AQ_LINK_SPEED_20GB:
663 case ICE_AQ_LINK_SPEED_10GB:
666 case ICE_AQ_LINK_SPEED_5GB:
669 case ICE_AQ_LINK_SPEED_2500MB:
672 case ICE_AQ_LINK_SPEED_1000MB:
675 case ICE_AQ_LINK_SPEED_100MB:
683 switch (vsi->port_info->fc.current_mode) {
687 case ICE_FC_TX_PAUSE:
690 case ICE_FC_RX_PAUSE:
701 /* Get FEC mode based on negotiated link info */
702 switch (vsi->port_info->phy.link_info.fec_info) {
703 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
705 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
708 case ICE_AQ_LINK_25G_KR_FEC_EN:
709 fec = "FC-FEC/BASE-R";
716 /* Get FEC mode requested based on PHY caps last SW configuration */
717 caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
723 status = ice_aq_get_phy_caps(vsi->port_info, false,
724 ICE_AQC_REPORT_SW_CFG, caps, NULL);
726 netdev_info(vsi->netdev, "Get phy capability failed.\n");
728 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
729 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
731 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
732 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
733 fec_req = "FC-FEC/BASE-R";
737 devm_kfree(&vsi->back->pdev->dev, caps);
740 netdev_info(vsi->netdev, "NIC Link is up %sbps, Requested FEC: %s, FEC: %s, Flow Control: %s\n",
741 speed, fec_req, fec, fc);
745 * ice_vsi_link_event - update the VSI's netdev
746 * @vsi: the VSI on which the link event occurred
747 * @link_up: whether or not the VSI needs to be set up or down
749 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
754 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
757 if (vsi->type == ICE_VSI_PF) {
758 if (link_up == netif_carrier_ok(vsi->netdev))
762 netif_carrier_on(vsi->netdev);
763 netif_tx_wake_all_queues(vsi->netdev);
765 netif_carrier_off(vsi->netdev);
766 netif_tx_stop_all_queues(vsi->netdev);
772 * ice_link_event - process the link event
773 * @pf: PF that the link event is associated with
774 * @pi: port_info for the port that the link event is associated with
775 * @link_up: true if the physical link is up and false if it is down
776 * @link_speed: current link speed received from the link event
778 * Returns 0 on success and negative on failure
781 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
784 struct ice_phy_info *phy_info;
791 phy_info->link_info_old = phy_info->link_info;
793 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
794 old_link_speed = phy_info->link_info_old.link_speed;
796 /* update the link info structures and re-enable link events,
797 * don't bail on failure due to other book keeping needed
799 result = ice_update_link_info(pi);
801 dev_dbg(&pf->pdev->dev,
802 "Failed to update link status and re-enable link events for port %d\n",
805 /* if the old link up/down and speed is the same as the new */
806 if (link_up == old_link && link_speed == old_link_speed)
809 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
810 if (!vsi || !vsi->port_info)
813 /* turn off PHY if media was removed */
814 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
815 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
816 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
818 result = ice_aq_set_link_restart_an(pi, false, NULL);
820 dev_dbg(&pf->pdev->dev,
821 "Failed to set link down, VSI %d error %d\n",
822 vsi->vsi_num, result);
827 ice_vsi_link_event(vsi, link_up);
828 ice_print_link_msg(vsi, link_up);
830 if (pf->num_alloc_vfs)
831 ice_vc_notify_link_state(pf);
837 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
838 * @pf: board private structure
840 static void ice_watchdog_subtask(struct ice_pf *pf)
844 /* if interface is down do nothing */
845 if (test_bit(__ICE_DOWN, pf->state) ||
846 test_bit(__ICE_CFG_BUSY, pf->state))
849 /* make sure we don't do these things too often */
850 if (time_before(jiffies,
851 pf->serv_tmr_prev + pf->serv_tmr_period))
854 pf->serv_tmr_prev = jiffies;
856 /* Update the stats for active netdevs so the network stack
857 * can look at updated numbers whenever it cares to
859 ice_update_pf_stats(pf);
860 ice_for_each_vsi(pf, i)
861 if (pf->vsi[i] && pf->vsi[i]->netdev)
862 ice_update_vsi_stats(pf->vsi[i]);
866 * ice_init_link_events - enable/initialize link events
867 * @pi: pointer to the port_info instance
869 * Returns -EIO on failure, 0 on success
871 static int ice_init_link_events(struct ice_port_info *pi)
875 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
876 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
878 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
879 dev_dbg(ice_hw_to_dev(pi->hw),
880 "Failed to set link event mask for port %d\n",
885 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
886 dev_dbg(ice_hw_to_dev(pi->hw),
887 "Failed to enable link events for port %d\n",
896 * ice_handle_link_event - handle link event via ARQ
897 * @pf: PF that the link event is associated with
898 * @event: event structure containing link status info
901 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
903 struct ice_aqc_get_link_status_data *link_data;
904 struct ice_port_info *port_info;
907 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
908 port_info = pf->hw.port_info;
912 status = ice_link_event(pf, port_info,
913 !!(link_data->link_info & ICE_AQ_LINK_UP),
914 le16_to_cpu(link_data->link_speed));
916 dev_dbg(&pf->pdev->dev,
917 "Could not process link event, error %d\n", status);
923 * __ice_clean_ctrlq - helper function to clean controlq rings
924 * @pf: ptr to struct ice_pf
925 * @q_type: specific Control queue type
927 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
929 struct ice_rq_event_info event;
930 struct ice_hw *hw = &pf->hw;
931 struct ice_ctl_q_info *cq;
936 /* Do not clean control queue if/when PF reset fails */
937 if (test_bit(__ICE_RESET_FAILED, pf->state))
941 case ICE_CTL_Q_ADMIN:
945 case ICE_CTL_Q_MAILBOX:
950 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
955 /* check for error indications - PF_xx_AxQLEN register layout for
956 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
958 val = rd32(hw, cq->rq.len);
959 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
960 PF_FW_ARQLEN_ARQCRIT_M)) {
962 if (val & PF_FW_ARQLEN_ARQVFE_M)
963 dev_dbg(&pf->pdev->dev,
964 "%s Receive Queue VF Error detected\n", qtype);
965 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
966 dev_dbg(&pf->pdev->dev,
967 "%s Receive Queue Overflow Error detected\n",
970 if (val & PF_FW_ARQLEN_ARQCRIT_M)
971 dev_dbg(&pf->pdev->dev,
972 "%s Receive Queue Critical Error detected\n",
974 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
975 PF_FW_ARQLEN_ARQCRIT_M);
977 wr32(hw, cq->rq.len, val);
980 val = rd32(hw, cq->sq.len);
981 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
982 PF_FW_ATQLEN_ATQCRIT_M)) {
984 if (val & PF_FW_ATQLEN_ATQVFE_M)
985 dev_dbg(&pf->pdev->dev,
986 "%s Send Queue VF Error detected\n", qtype);
987 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
988 dev_dbg(&pf->pdev->dev,
989 "%s Send Queue Overflow Error detected\n",
992 if (val & PF_FW_ATQLEN_ATQCRIT_M)
993 dev_dbg(&pf->pdev->dev,
994 "%s Send Queue Critical Error detected\n",
996 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
997 PF_FW_ATQLEN_ATQCRIT_M);
999 wr32(hw, cq->sq.len, val);
1002 event.buf_len = cq->rq_buf_size;
1003 event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
1009 enum ice_status ret;
1012 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1013 if (ret == ICE_ERR_AQ_NO_WORK)
1016 dev_err(&pf->pdev->dev,
1017 "%s Receive Queue event error %d\n", qtype,
1022 opcode = le16_to_cpu(event.desc.opcode);
1025 case ice_aqc_opc_get_link_status:
1026 if (ice_handle_link_event(pf, &event))
1027 dev_err(&pf->pdev->dev,
1028 "Could not handle link event\n");
1030 case ice_mbx_opc_send_msg_to_pf:
1031 ice_vc_process_vf_msg(pf, &event);
1033 case ice_aqc_opc_fw_logging:
1034 ice_output_fw_log(hw, &event.desc, event.msg_buf);
1036 case ice_aqc_opc_lldp_set_mib_change:
1037 ice_dcb_process_lldp_set_mib_change(pf, &event);
1040 dev_dbg(&pf->pdev->dev,
1041 "%s Receive Queue unknown event 0x%04x ignored\n",
1045 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1047 devm_kfree(&pf->pdev->dev, event.msg_buf);
1049 return pending && (i == ICE_DFLT_IRQ_WORK);
1053 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1054 * @hw: pointer to hardware info
1055 * @cq: control queue information
1057 * returns true if there are pending messages in a queue, false if there aren't
1059 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1063 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1064 return cq->rq.next_to_clean != ntu;
1068 * ice_clean_adminq_subtask - clean the AdminQ rings
1069 * @pf: board private structure
1071 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1073 struct ice_hw *hw = &pf->hw;
1075 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1078 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1081 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1083 /* There might be a situation where new messages arrive to a control
1084 * queue between processing the last message and clearing the
1085 * EVENT_PENDING bit. So before exiting, check queue head again (using
1086 * ice_ctrlq_pending) and process new messages if any.
1088 if (ice_ctrlq_pending(hw, &hw->adminq))
1089 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1095 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1096 * @pf: board private structure
1098 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1100 struct ice_hw *hw = &pf->hw;
1102 if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1105 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1108 clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1110 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1111 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1117 * ice_service_task_schedule - schedule the service task to wake up
1118 * @pf: board private structure
1120 * If not already scheduled, this puts the task into the work queue.
1122 static void ice_service_task_schedule(struct ice_pf *pf)
1124 if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1125 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1126 !test_bit(__ICE_NEEDS_RESTART, pf->state))
1127 queue_work(ice_wq, &pf->serv_task);
1131 * ice_service_task_complete - finish up the service task
1132 * @pf: board private structure
1134 static void ice_service_task_complete(struct ice_pf *pf)
1136 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1138 /* force memory (pf->state) to sync before next service task */
1139 smp_mb__before_atomic();
1140 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1144 * ice_service_task_stop - stop service task and cancel works
1145 * @pf: board private structure
1147 static void ice_service_task_stop(struct ice_pf *pf)
1149 set_bit(__ICE_SERVICE_DIS, pf->state);
1151 if (pf->serv_tmr.function)
1152 del_timer_sync(&pf->serv_tmr);
1153 if (pf->serv_task.func)
1154 cancel_work_sync(&pf->serv_task);
1156 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1160 * ice_service_task_restart - restart service task and schedule works
1161 * @pf: board private structure
1163 * This function is needed for suspend and resume works (e.g WoL scenario)
1165 static void ice_service_task_restart(struct ice_pf *pf)
1167 clear_bit(__ICE_SERVICE_DIS, pf->state);
1168 ice_service_task_schedule(pf);
1172 * ice_service_timer - timer callback to schedule service task
1173 * @t: pointer to timer_list
1175 static void ice_service_timer(struct timer_list *t)
1177 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1179 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1180 ice_service_task_schedule(pf);
1184 * ice_handle_mdd_event - handle malicious driver detect event
1185 * @pf: pointer to the PF structure
1187 * Called from service task. OICR interrupt handler indicates MDD event
1189 static void ice_handle_mdd_event(struct ice_pf *pf)
1191 struct ice_hw *hw = &pf->hw;
1192 bool mdd_detected = false;
1196 if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1199 /* find what triggered the MDD event */
1200 reg = rd32(hw, GL_MDET_TX_PQM);
1201 if (reg & GL_MDET_TX_PQM_VALID_M) {
1202 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1203 GL_MDET_TX_PQM_PF_NUM_S;
1204 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1205 GL_MDET_TX_PQM_VF_NUM_S;
1206 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1207 GL_MDET_TX_PQM_MAL_TYPE_S;
1208 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1209 GL_MDET_TX_PQM_QNUM_S);
1211 if (netif_msg_tx_err(pf))
1212 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1213 event, queue, pf_num, vf_num);
1214 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1215 mdd_detected = true;
1218 reg = rd32(hw, GL_MDET_TX_TCLAN);
1219 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1220 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1221 GL_MDET_TX_TCLAN_PF_NUM_S;
1222 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1223 GL_MDET_TX_TCLAN_VF_NUM_S;
1224 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1225 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1226 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1227 GL_MDET_TX_TCLAN_QNUM_S);
1229 if (netif_msg_rx_err(pf))
1230 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1231 event, queue, pf_num, vf_num);
1232 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1233 mdd_detected = true;
1236 reg = rd32(hw, GL_MDET_RX);
1237 if (reg & GL_MDET_RX_VALID_M) {
1238 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1239 GL_MDET_RX_PF_NUM_S;
1240 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1241 GL_MDET_RX_VF_NUM_S;
1242 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1243 GL_MDET_RX_MAL_TYPE_S;
1244 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1247 if (netif_msg_rx_err(pf))
1248 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1249 event, queue, pf_num, vf_num);
1250 wr32(hw, GL_MDET_RX, 0xffffffff);
1251 mdd_detected = true;
1255 bool pf_mdd_detected = false;
1257 reg = rd32(hw, PF_MDET_TX_PQM);
1258 if (reg & PF_MDET_TX_PQM_VALID_M) {
1259 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1260 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1261 pf_mdd_detected = true;
1264 reg = rd32(hw, PF_MDET_TX_TCLAN);
1265 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1266 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1267 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1268 pf_mdd_detected = true;
1271 reg = rd32(hw, PF_MDET_RX);
1272 if (reg & PF_MDET_RX_VALID_M) {
1273 wr32(hw, PF_MDET_RX, 0xFFFF);
1274 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1275 pf_mdd_detected = true;
1277 /* Queue belongs to the PF initiate a reset */
1278 if (pf_mdd_detected) {
1279 set_bit(__ICE_NEEDS_RESTART, pf->state);
1280 ice_service_task_schedule(pf);
1284 /* check to see if one of the VFs caused the MDD */
1285 for (i = 0; i < pf->num_alloc_vfs; i++) {
1286 struct ice_vf *vf = &pf->vf[i];
1288 bool vf_mdd_detected = false;
1290 reg = rd32(hw, VP_MDET_TX_PQM(i));
1291 if (reg & VP_MDET_TX_PQM_VALID_M) {
1292 wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1293 vf_mdd_detected = true;
1294 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1298 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1299 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1300 wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1301 vf_mdd_detected = true;
1302 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1306 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1307 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1308 wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1309 vf_mdd_detected = true;
1310 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1314 reg = rd32(hw, VP_MDET_RX(i));
1315 if (reg & VP_MDET_RX_VALID_M) {
1316 wr32(hw, VP_MDET_RX(i), 0xFFFF);
1317 vf_mdd_detected = true;
1318 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
1322 if (vf_mdd_detected) {
1323 vf->num_mdd_events++;
1324 if (vf->num_mdd_events > 1)
1325 dev_info(&pf->pdev->dev, "VF %d has had %llu MDD events since last boot\n",
1326 i, vf->num_mdd_events);
1332 * ice_force_phys_link_state - Force the physical link state
1333 * @vsi: VSI to force the physical link state to up/down
1334 * @link_up: true/false indicates to set the physical link to up/down
1336 * Force the physical link state by getting the current PHY capabilities from
1337 * hardware and setting the PHY config based on the determined capabilities. If
1338 * link changes a link event will be triggered because both the Enable Automatic
1339 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1341 * Returns 0 on success, negative on failure
1343 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1345 struct ice_aqc_get_phy_caps_data *pcaps;
1346 struct ice_aqc_set_phy_cfg_data *cfg;
1347 struct ice_port_info *pi;
1351 if (!vsi || !vsi->port_info || !vsi->back)
1353 if (vsi->type != ICE_VSI_PF)
1356 dev = &vsi->back->pdev->dev;
1358 pi = vsi->port_info;
1360 pcaps = devm_kzalloc(dev, sizeof(*pcaps), GFP_KERNEL);
1364 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1368 "Failed to get phy capabilities, VSI %d error %d\n",
1369 vsi->vsi_num, retcode);
1374 /* No change in link */
1375 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1376 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1379 cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
1385 cfg->phy_type_low = pcaps->phy_type_low;
1386 cfg->phy_type_high = pcaps->phy_type_high;
1387 cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1388 cfg->low_power_ctrl = pcaps->low_power_ctrl;
1389 cfg->eee_cap = pcaps->eee_cap;
1390 cfg->eeer_value = pcaps->eeer_value;
1391 cfg->link_fec_opt = pcaps->link_fec_options;
1393 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1395 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1397 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL);
1399 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1400 vsi->vsi_num, retcode);
1404 devm_kfree(dev, cfg);
1406 devm_kfree(dev, pcaps);
1411 * ice_check_media_subtask - Check for media; bring link up if detected.
1412 * @pf: pointer to PF struct
1414 static void ice_check_media_subtask(struct ice_pf *pf)
1416 struct ice_port_info *pi;
1417 struct ice_vsi *vsi;
1420 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
1424 /* No need to check for media if it's already present or the interface
1427 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) ||
1428 test_bit(__ICE_DOWN, vsi->state))
1431 /* Refresh link info and check if media is present */
1432 pi = vsi->port_info;
1433 err = ice_update_link_info(pi);
1437 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
1438 err = ice_force_phys_link_state(vsi, true);
1441 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1443 /* A Link Status Event will be generated; the event handler
1444 * will complete bringing the interface up
1450 * ice_service_task - manage and run subtasks
1451 * @work: pointer to work_struct contained by the PF struct
1453 static void ice_service_task(struct work_struct *work)
1455 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1456 unsigned long start_time = jiffies;
1460 /* process reset requests first */
1461 ice_reset_subtask(pf);
1463 /* bail if a reset/recovery cycle is pending or rebuild failed */
1464 if (ice_is_reset_in_progress(pf->state) ||
1465 test_bit(__ICE_SUSPENDED, pf->state) ||
1466 test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1467 ice_service_task_complete(pf);
1471 ice_check_media_subtask(pf);
1472 ice_check_for_hang_subtask(pf);
1473 ice_sync_fltr_subtask(pf);
1474 ice_handle_mdd_event(pf);
1475 ice_process_vflr_event(pf);
1476 ice_watchdog_subtask(pf);
1477 ice_clean_adminq_subtask(pf);
1478 ice_clean_mailboxq_subtask(pf);
1480 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1481 ice_service_task_complete(pf);
1483 /* If the tasks have taken longer than one service timer period
1484 * or there is more work to be done, reset the service timer to
1485 * schedule the service task now.
1487 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1488 test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1489 test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1490 test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1491 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1492 mod_timer(&pf->serv_tmr, jiffies);
1496 * ice_set_ctrlq_len - helper function to set controlq length
1497 * @hw: pointer to the HW instance
1499 static void ice_set_ctrlq_len(struct ice_hw *hw)
1501 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1502 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1503 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1504 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1505 hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
1506 hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
1507 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1508 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1512 * ice_irq_affinity_notify - Callback for affinity changes
1513 * @notify: context as to what irq was changed
1514 * @mask: the new affinity mask
1516 * This is a callback function used by the irq_set_affinity_notifier function
1517 * so that we may register to receive changes to the irq affinity masks.
1520 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1521 const cpumask_t *mask)
1523 struct ice_q_vector *q_vector =
1524 container_of(notify, struct ice_q_vector, affinity_notify);
1526 cpumask_copy(&q_vector->affinity_mask, mask);
1530 * ice_irq_affinity_release - Callback for affinity notifier release
1531 * @ref: internal core kernel usage
1533 * This is a callback function used by the irq_set_affinity_notifier function
1534 * to inform the current notification subscriber that they will no longer
1535 * receive notifications.
1537 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1540 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1541 * @vsi: the VSI being configured
1543 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1545 struct ice_pf *pf = vsi->back;
1546 struct ice_hw *hw = &pf->hw;
1548 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1551 ice_for_each_q_vector(vsi, i)
1552 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1560 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1561 * @vsi: the VSI being configured
1562 * @basename: name for the vector
1564 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1566 int q_vectors = vsi->num_q_vectors;
1567 struct ice_pf *pf = vsi->back;
1568 int base = vsi->base_vector;
1574 for (vector = 0; vector < q_vectors; vector++) {
1575 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1577 irq_num = pf->msix_entries[base + vector].vector;
1579 if (q_vector->tx.ring && q_vector->rx.ring) {
1580 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1581 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1583 } else if (q_vector->rx.ring) {
1584 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1585 "%s-%s-%d", basename, "rx", rx_int_idx++);
1586 } else if (q_vector->tx.ring) {
1587 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1588 "%s-%s-%d", basename, "tx", tx_int_idx++);
1590 /* skip this unused q_vector */
1593 err = devm_request_irq(&pf->pdev->dev, irq_num,
1594 vsi->irq_handler, 0,
1595 q_vector->name, q_vector);
1597 netdev_err(vsi->netdev,
1598 "MSIX request_irq failed, error: %d\n", err);
1602 /* register for affinity change notifications */
1603 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1604 q_vector->affinity_notify.release = ice_irq_affinity_release;
1605 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1607 /* assign the mask for this irq */
1608 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1611 vsi->irqs_ready = true;
1617 irq_num = pf->msix_entries[base + vector].vector,
1618 irq_set_affinity_notifier(irq_num, NULL);
1619 irq_set_affinity_hint(irq_num, NULL);
1620 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1626 * ice_ena_misc_vector - enable the non-queue interrupts
1627 * @pf: board private structure
1629 static void ice_ena_misc_vector(struct ice_pf *pf)
1631 struct ice_hw *hw = &pf->hw;
1634 /* clear things first */
1635 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1636 rd32(hw, PFINT_OICR); /* read to clear */
1638 val = (PFINT_OICR_ECC_ERR_M |
1639 PFINT_OICR_MAL_DETECT_M |
1641 PFINT_OICR_PCI_EXCEPTION_M |
1643 PFINT_OICR_HMC_ERR_M |
1644 PFINT_OICR_PE_CRITERR_M);
1646 wr32(hw, PFINT_OICR_ENA, val);
1648 /* SW_ITR_IDX = 0, but don't change INTENA */
1649 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1650 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1654 * ice_misc_intr - misc interrupt handler
1655 * @irq: interrupt number
1656 * @data: pointer to a q_vector
1658 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1660 struct ice_pf *pf = (struct ice_pf *)data;
1661 struct ice_hw *hw = &pf->hw;
1662 irqreturn_t ret = IRQ_NONE;
1665 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1666 set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1668 oicr = rd32(hw, PFINT_OICR);
1669 ena_mask = rd32(hw, PFINT_OICR_ENA);
1671 if (oicr & PFINT_OICR_SWINT_M) {
1672 ena_mask &= ~PFINT_OICR_SWINT_M;
1676 if (oicr & PFINT_OICR_MAL_DETECT_M) {
1677 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
1678 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1680 if (oicr & PFINT_OICR_VFLR_M) {
1681 ena_mask &= ~PFINT_OICR_VFLR_M;
1682 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
1685 if (oicr & PFINT_OICR_GRST_M) {
1688 /* we have a reset warning */
1689 ena_mask &= ~PFINT_OICR_GRST_M;
1690 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1691 GLGEN_RSTAT_RESET_TYPE_S;
1693 if (reset == ICE_RESET_CORER)
1695 else if (reset == ICE_RESET_GLOBR)
1697 else if (reset == ICE_RESET_EMPR)
1700 dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
1703 /* If a reset cycle isn't already in progress, we set a bit in
1704 * pf->state so that the service task can start a reset/rebuild.
1705 * We also make note of which reset happened so that peer
1706 * devices/drivers can be informed.
1708 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
1709 if (reset == ICE_RESET_CORER)
1710 set_bit(__ICE_CORER_RECV, pf->state);
1711 else if (reset == ICE_RESET_GLOBR)
1712 set_bit(__ICE_GLOBR_RECV, pf->state);
1714 set_bit(__ICE_EMPR_RECV, pf->state);
1716 /* There are couple of different bits at play here.
1717 * hw->reset_ongoing indicates whether the hardware is
1718 * in reset. This is set to true when a reset interrupt
1719 * is received and set back to false after the driver
1720 * has determined that the hardware is out of reset.
1722 * __ICE_RESET_OICR_RECV in pf->state indicates
1723 * that a post reset rebuild is required before the
1724 * driver is operational again. This is set above.
1726 * As this is the start of the reset/rebuild cycle, set
1727 * both to indicate that.
1729 hw->reset_ongoing = true;
1733 if (oicr & PFINT_OICR_HMC_ERR_M) {
1734 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1735 dev_dbg(&pf->pdev->dev,
1736 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1737 rd32(hw, PFHMC_ERRORINFO),
1738 rd32(hw, PFHMC_ERRORDATA));
1741 /* Report any remaining unexpected interrupts */
1744 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1746 /* If a critical error is pending there is no choice but to
1749 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1750 PFINT_OICR_PCI_EXCEPTION_M |
1751 PFINT_OICR_ECC_ERR_M)) {
1752 set_bit(__ICE_PFR_REQ, pf->state);
1753 ice_service_task_schedule(pf);
1758 if (!test_bit(__ICE_DOWN, pf->state)) {
1759 ice_service_task_schedule(pf);
1760 ice_irq_dynamic_ena(hw, NULL, NULL);
1767 * ice_dis_ctrlq_interrupts - disable control queue interrupts
1768 * @hw: pointer to HW structure
1770 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
1772 /* disable Admin queue Interrupt causes */
1773 wr32(hw, PFINT_FW_CTL,
1774 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
1776 /* disable Mailbox queue Interrupt causes */
1777 wr32(hw, PFINT_MBX_CTL,
1778 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
1780 /* disable Control queue Interrupt causes */
1781 wr32(hw, PFINT_OICR_CTL,
1782 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
1788 * ice_free_irq_msix_misc - Unroll misc vector setup
1789 * @pf: board private structure
1791 static void ice_free_irq_msix_misc(struct ice_pf *pf)
1793 struct ice_hw *hw = &pf->hw;
1795 ice_dis_ctrlq_interrupts(hw);
1797 /* disable OICR interrupt */
1798 wr32(hw, PFINT_OICR_ENA, 0);
1801 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
1802 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
1803 devm_free_irq(&pf->pdev->dev,
1804 pf->msix_entries[pf->oicr_idx].vector, pf);
1807 pf->num_avail_sw_msix += 1;
1808 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
1812 * ice_ena_ctrlq_interrupts - enable control queue interrupts
1813 * @hw: pointer to HW structure
1814 * @reg_idx: HW vector index to associate the control queue interrupts with
1816 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
1820 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
1821 PFINT_OICR_CTL_CAUSE_ENA_M);
1822 wr32(hw, PFINT_OICR_CTL, val);
1824 /* enable Admin queue Interrupt causes */
1825 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
1826 PFINT_FW_CTL_CAUSE_ENA_M);
1827 wr32(hw, PFINT_FW_CTL, val);
1829 /* enable Mailbox queue Interrupt causes */
1830 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
1831 PFINT_MBX_CTL_CAUSE_ENA_M);
1832 wr32(hw, PFINT_MBX_CTL, val);
1838 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
1839 * @pf: board private structure
1841 * This sets up the handler for MSIX 0, which is used to manage the
1842 * non-queue interrupts, e.g. AdminQ and errors. This is not used
1843 * when in MSI or Legacy interrupt mode.
1845 static int ice_req_irq_msix_misc(struct ice_pf *pf)
1847 struct ice_hw *hw = &pf->hw;
1848 int oicr_idx, err = 0;
1850 if (!pf->int_name[0])
1851 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
1852 dev_driver_string(&pf->pdev->dev),
1853 dev_name(&pf->pdev->dev));
1855 /* Do not request IRQ but do enable OICR interrupt since settings are
1856 * lost during reset. Note that this function is called only during
1857 * rebuild path and not while reset is in progress.
1859 if (ice_is_reset_in_progress(pf->state))
1862 /* reserve one vector in irq_tracker for misc interrupts */
1863 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1867 pf->num_avail_sw_msix -= 1;
1868 pf->oicr_idx = oicr_idx;
1870 err = devm_request_irq(&pf->pdev->dev,
1871 pf->msix_entries[pf->oicr_idx].vector,
1872 ice_misc_intr, 0, pf->int_name, pf);
1874 dev_err(&pf->pdev->dev,
1875 "devm_request_irq for %s failed: %d\n",
1877 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1878 pf->num_avail_sw_msix += 1;
1883 ice_ena_misc_vector(pf);
1885 ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
1886 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
1887 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
1890 ice_irq_dynamic_ena(hw, NULL, NULL);
1896 * ice_napi_add - register NAPI handler for the VSI
1897 * @vsi: VSI for which NAPI handler is to be registered
1899 * This function is only called in the driver's load path. Registering the NAPI
1900 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
1901 * reset/rebuild, etc.)
1903 static void ice_napi_add(struct ice_vsi *vsi)
1910 ice_for_each_q_vector(vsi, v_idx)
1911 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
1912 ice_napi_poll, NAPI_POLL_WEIGHT);
1916 * ice_cfg_netdev - Allocate, configure and register a netdev
1917 * @vsi: the VSI associated with the new netdev
1919 * Returns 0 on success, negative value on failure
1921 static int ice_cfg_netdev(struct ice_vsi *vsi)
1923 netdev_features_t csumo_features;
1924 netdev_features_t vlano_features;
1925 netdev_features_t dflt_features;
1926 netdev_features_t tso_features;
1927 struct ice_netdev_priv *np;
1928 struct net_device *netdev;
1929 u8 mac_addr[ETH_ALEN];
1932 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
1937 vsi->netdev = netdev;
1938 np = netdev_priv(netdev);
1941 dflt_features = NETIF_F_SG |
1945 csumo_features = NETIF_F_RXCSUM |
1950 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
1951 NETIF_F_HW_VLAN_CTAG_TX |
1952 NETIF_F_HW_VLAN_CTAG_RX;
1954 tso_features = NETIF_F_TSO;
1956 /* set features that user can change */
1957 netdev->hw_features = dflt_features | csumo_features |
1958 vlano_features | tso_features;
1960 /* enable features */
1961 netdev->features |= netdev->hw_features;
1962 /* encap and VLAN devices inherit default, csumo and tso features */
1963 netdev->hw_enc_features |= dflt_features | csumo_features |
1965 netdev->vlan_features |= dflt_features | csumo_features |
1968 if (vsi->type == ICE_VSI_PF) {
1969 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
1970 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
1972 ether_addr_copy(netdev->dev_addr, mac_addr);
1973 ether_addr_copy(netdev->perm_addr, mac_addr);
1976 netdev->priv_flags |= IFF_UNICAST_FLT;
1978 /* assign netdev_ops */
1979 netdev->netdev_ops = &ice_netdev_ops;
1981 /* setup watchdog timeout value to be 5 second */
1982 netdev->watchdog_timeo = 5 * HZ;
1984 ice_set_ethtool_ops(netdev);
1986 netdev->min_mtu = ETH_MIN_MTU;
1987 netdev->max_mtu = ICE_MAX_MTU;
1989 err = register_netdev(vsi->netdev);
1993 netif_carrier_off(vsi->netdev);
1995 /* make sure transmit queues start off as stopped */
1996 netif_tx_stop_all_queues(vsi->netdev);
2002 * ice_fill_rss_lut - Fill the RSS lookup table with default values
2003 * @lut: Lookup table
2004 * @rss_table_size: Lookup table size
2005 * @rss_size: Range of queue number for hashing
2007 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2011 for (i = 0; i < rss_table_size; i++)
2012 lut[i] = i % rss_size;
2016 * ice_pf_vsi_setup - Set up a PF VSI
2017 * @pf: board private structure
2018 * @pi: pointer to the port_info instance
2020 * Returns pointer to the successfully allocated VSI software struct
2021 * on success, otherwise returns NULL on failure.
2023 static struct ice_vsi *
2024 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2026 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
2030 * ice_lb_vsi_setup - Set up a loopback VSI
2031 * @pf: board private structure
2032 * @pi: pointer to the port_info instance
2034 * Returns pointer to the successfully allocated VSI software struct
2035 * on success, otherwise returns NULL on failure.
2038 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
2040 return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
2044 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
2045 * @netdev: network interface to be adjusted
2046 * @proto: unused protocol
2047 * @vid: VLAN ID to be added
2049 * net_device_ops implementation for adding VLAN IDs
2052 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
2055 struct ice_netdev_priv *np = netdev_priv(netdev);
2056 struct ice_vsi *vsi = np->vsi;
2059 if (vid >= VLAN_N_VID) {
2060 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2068 /* Enable VLAN pruning when VLAN 0 is added */
2069 if (unlikely(!vid)) {
2070 ret = ice_cfg_vlan_pruning(vsi, true, false);
2075 /* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is
2076 * needed to continue allowing all untagged packets since VLAN prune
2077 * list is applied to all packets by the switch
2079 ret = ice_vsi_add_vlan(vsi, vid);
2081 vsi->vlan_ena = true;
2082 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2089 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
2090 * @netdev: network interface to be adjusted
2091 * @proto: unused protocol
2092 * @vid: VLAN ID to be removed
2094 * net_device_ops implementation for removing VLAN IDs
2097 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
2100 struct ice_netdev_priv *np = netdev_priv(netdev);
2101 struct ice_vsi *vsi = np->vsi;
2107 /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
2110 ret = ice_vsi_kill_vlan(vsi, vid);
2114 /* Disable VLAN pruning when VLAN 0 is removed */
2116 ret = ice_cfg_vlan_pruning(vsi, false, false);
2118 vsi->vlan_ena = false;
2119 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
2124 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
2125 * @pf: board private structure
2127 * Returns 0 on success, negative value on failure
2129 static int ice_setup_pf_sw(struct ice_pf *pf)
2131 struct ice_vsi *vsi;
2134 if (ice_is_reset_in_progress(pf->state))
2137 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
2140 goto unroll_vsi_setup;
2143 status = ice_cfg_netdev(vsi);
2146 goto unroll_vsi_setup;
2149 /* registering the NAPI handler requires both the queues and
2150 * netdev to be created, which are done in ice_pf_vsi_setup()
2151 * and ice_cfg_netdev() respectively
2155 status = ice_init_mac_fltr(pf);
2157 goto unroll_napi_add;
2165 if (vsi->netdev->reg_state == NETREG_REGISTERED)
2166 unregister_netdev(vsi->netdev);
2167 free_netdev(vsi->netdev);
2174 ice_vsi_free_q_vectors(vsi);
2175 ice_vsi_delete(vsi);
2176 ice_vsi_put_qs(vsi);
2177 pf->q_left_tx += vsi->alloc_txq;
2178 pf->q_left_rx += vsi->alloc_rxq;
2185 * ice_determine_q_usage - Calculate queue distribution
2186 * @pf: board private structure
2188 * Return -ENOMEM if we don't get enough queues for all ports
2190 static void ice_determine_q_usage(struct ice_pf *pf)
2192 u16 q_left_tx, q_left_rx;
2194 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
2195 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
2197 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
2199 /* only 1 Rx queue unless RSS is enabled */
2200 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2203 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
2205 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
2206 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
2210 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
2211 * @pf: board private structure to initialize
2213 static void ice_deinit_pf(struct ice_pf *pf)
2215 ice_service_task_stop(pf);
2216 mutex_destroy(&pf->sw_mutex);
2217 mutex_destroy(&pf->avail_q_mutex);
2221 * ice_init_pf - Initialize general software structures (struct ice_pf)
2222 * @pf: board private structure to initialize
2224 static void ice_init_pf(struct ice_pf *pf)
2226 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
2227 set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
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;
2327 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2332 * ice_dis_msix - Disable MSI-X interrupt setup in OS
2333 * @pf: board private structure
2335 static void ice_dis_msix(struct ice_pf *pf)
2337 pci_disable_msix(pf->pdev);
2338 devm_kfree(&pf->pdev->dev, pf->msix_entries);
2339 pf->msix_entries = NULL;
2340 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2344 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
2345 * @pf: board private structure
2347 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
2349 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2352 if (pf->irq_tracker) {
2353 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
2354 pf->irq_tracker = NULL;
2359 * ice_init_interrupt_scheme - Determine proper interrupt scheme
2360 * @pf: board private structure to initialize
2362 static int ice_init_interrupt_scheme(struct ice_pf *pf)
2366 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2367 vectors = ice_ena_msix_range(pf);
2374 /* set up vector assignment tracking */
2376 devm_kzalloc(&pf->pdev->dev, sizeof(*pf->irq_tracker) +
2377 (sizeof(u16) * vectors), GFP_KERNEL);
2378 if (!pf->irq_tracker) {
2383 /* populate SW interrupts pool with number of OS granted IRQs. */
2384 pf->num_avail_sw_msix = vectors;
2385 pf->irq_tracker->num_entries = vectors;
2386 pf->irq_tracker->end = pf->irq_tracker->num_entries;
2392 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
2393 * @pf: pointer to the PF structure
2395 * There is no error returned here because the driver should be able to handle
2396 * 128 Byte cache lines, so we only print a warning in case issues are seen,
2397 * specifically with Tx.
2399 static void ice_verify_cacheline_size(struct ice_pf *pf)
2401 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
2402 dev_warn(&pf->pdev->dev,
2403 "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
2404 ICE_CACHE_LINE_BYTES);
2408 * ice_probe - Device initialization routine
2409 * @pdev: PCI device information struct
2410 * @ent: entry in ice_pci_tbl
2412 * Returns 0 on success, negative on failure
2415 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
2417 struct device *dev = &pdev->dev;
2422 /* this driver uses devres, see Documentation/driver-api/driver-model/devres.rst */
2423 err = pcim_enable_device(pdev);
2427 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
2429 dev_err(dev, "BAR0 I/O map error %d\n", err);
2433 pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL);
2437 /* set up for high or low DMA */
2438 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2440 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2442 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
2446 pci_enable_pcie_error_reporting(pdev);
2447 pci_set_master(pdev);
2450 pci_set_drvdata(pdev, pf);
2451 set_bit(__ICE_DOWN, pf->state);
2452 /* Disable service task until DOWN bit is cleared */
2453 set_bit(__ICE_SERVICE_DIS, pf->state);
2456 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
2458 hw->vendor_id = pdev->vendor;
2459 hw->device_id = pdev->device;
2460 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2461 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2462 hw->subsystem_device_id = pdev->subsystem_device;
2463 hw->bus.device = PCI_SLOT(pdev->devfn);
2464 hw->bus.func = PCI_FUNC(pdev->devfn);
2465 ice_set_ctrlq_len(hw);
2467 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
2469 #ifndef CONFIG_DYNAMIC_DEBUG
2471 hw->debug_mask = debug;
2474 err = ice_init_hw(hw);
2476 dev_err(dev, "ice_init_hw failed: %d\n", err);
2478 goto err_exit_unroll;
2481 dev_info(dev, "firmware %d.%d.%05d api %d.%d\n",
2482 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
2483 hw->api_maj_ver, hw->api_min_ver);
2487 err = ice_init_pf_dcb(pf, false);
2489 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2490 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
2492 /* do not fail overall init if DCB init fails */
2496 ice_determine_q_usage(pf);
2498 pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
2499 if (!pf->num_alloc_vsi) {
2501 goto err_init_pf_unroll;
2504 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
2508 goto err_init_pf_unroll;
2511 err = ice_init_interrupt_scheme(pf);
2513 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
2515 goto err_init_interrupt_unroll;
2518 /* Driver is mostly up */
2519 clear_bit(__ICE_DOWN, pf->state);
2521 /* In case of MSIX we are going to setup the misc vector right here
2522 * to handle admin queue events etc. In case of legacy and MSI
2523 * the misc functionality and queue processing is combined in
2524 * the same vector and that gets setup at open.
2526 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2527 err = ice_req_irq_msix_misc(pf);
2529 dev_err(dev, "setup of misc vector failed: %d\n", err);
2530 goto err_init_interrupt_unroll;
2534 /* create switch struct for the switch element created by FW on boot */
2535 pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
2536 if (!pf->first_sw) {
2538 goto err_msix_misc_unroll;
2542 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
2544 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
2546 pf->first_sw->pf = pf;
2548 /* record the sw_id available for later use */
2549 pf->first_sw->sw_id = hw->port_info->sw_id;
2551 err = ice_setup_pf_sw(pf);
2553 dev_err(dev, "probe failed due to setup PF switch:%d\n", err);
2554 goto err_alloc_sw_unroll;
2557 clear_bit(__ICE_SERVICE_DIS, pf->state);
2559 /* since everything is good, start the service timer */
2560 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2562 err = ice_init_link_events(pf->hw.port_info);
2564 dev_err(dev, "ice_init_link_events failed: %d\n", err);
2565 goto err_alloc_sw_unroll;
2568 ice_verify_cacheline_size(pf);
2572 err_alloc_sw_unroll:
2573 set_bit(__ICE_SERVICE_DIS, pf->state);
2574 set_bit(__ICE_DOWN, pf->state);
2575 devm_kfree(&pf->pdev->dev, pf->first_sw);
2576 err_msix_misc_unroll:
2577 ice_free_irq_msix_misc(pf);
2578 err_init_interrupt_unroll:
2579 ice_clear_interrupt_scheme(pf);
2580 devm_kfree(dev, pf->vsi);
2585 pci_disable_pcie_error_reporting(pdev);
2590 * ice_remove - Device removal routine
2591 * @pdev: PCI device information struct
2593 static void ice_remove(struct pci_dev *pdev)
2595 struct ice_pf *pf = pci_get_drvdata(pdev);
2601 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
2602 if (!ice_is_reset_in_progress(pf->state))
2607 set_bit(__ICE_DOWN, pf->state);
2608 ice_service_task_stop(pf);
2610 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
2612 ice_vsi_release_all(pf);
2613 ice_free_irq_msix_misc(pf);
2614 ice_for_each_vsi(pf, i) {
2617 ice_vsi_free_q_vectors(pf->vsi[i]);
2619 ice_clear_interrupt_scheme(pf);
2621 ice_deinit_hw(&pf->hw);
2622 pci_disable_pcie_error_reporting(pdev);
2626 * ice_pci_err_detected - warning that PCI error has been detected
2627 * @pdev: PCI device information struct
2628 * @err: the type of PCI error
2630 * Called to warn that something happened on the PCI bus and the error handling
2631 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
2633 static pci_ers_result_t
2634 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err)
2636 struct ice_pf *pf = pci_get_drvdata(pdev);
2639 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
2641 return PCI_ERS_RESULT_DISCONNECT;
2644 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2645 ice_service_task_stop(pf);
2647 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2648 set_bit(__ICE_PFR_REQ, pf->state);
2649 ice_prepare_for_reset(pf);
2653 return PCI_ERS_RESULT_NEED_RESET;
2657 * ice_pci_err_slot_reset - a PCI slot reset has just happened
2658 * @pdev: PCI device information struct
2660 * Called to determine if the driver can recover from the PCI slot reset by
2661 * using a register read to determine if the device is recoverable.
2663 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
2665 struct ice_pf *pf = pci_get_drvdata(pdev);
2666 pci_ers_result_t result;
2670 err = pci_enable_device_mem(pdev);
2673 "Cannot re-enable PCI device after reset, error %d\n",
2675 result = PCI_ERS_RESULT_DISCONNECT;
2677 pci_set_master(pdev);
2678 pci_restore_state(pdev);
2679 pci_save_state(pdev);
2680 pci_wake_from_d3(pdev, false);
2682 /* Check for life */
2683 reg = rd32(&pf->hw, GLGEN_RTRIG);
2685 result = PCI_ERS_RESULT_RECOVERED;
2687 result = PCI_ERS_RESULT_DISCONNECT;
2690 err = pci_cleanup_aer_uncorrect_error_status(pdev);
2693 "pci_cleanup_aer_uncorrect_error_status failed, error %d\n",
2695 /* non-fatal, continue */
2701 * ice_pci_err_resume - restart operations after PCI error recovery
2702 * @pdev: PCI device information struct
2704 * Called to allow the driver to bring things back up after PCI error and/or
2705 * reset recovery have finished
2707 static void ice_pci_err_resume(struct pci_dev *pdev)
2709 struct ice_pf *pf = pci_get_drvdata(pdev);
2713 "%s failed, device is unrecoverable\n", __func__);
2717 if (test_bit(__ICE_SUSPENDED, pf->state)) {
2718 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
2723 ice_do_reset(pf, ICE_RESET_PFR);
2724 ice_service_task_restart(pf);
2725 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2729 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
2730 * @pdev: PCI device information struct
2732 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
2734 struct ice_pf *pf = pci_get_drvdata(pdev);
2736 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2737 ice_service_task_stop(pf);
2739 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2740 set_bit(__ICE_PFR_REQ, pf->state);
2741 ice_prepare_for_reset(pf);
2747 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
2748 * @pdev: PCI device information struct
2750 static void ice_pci_err_reset_done(struct pci_dev *pdev)
2752 ice_pci_err_resume(pdev);
2755 /* ice_pci_tbl - PCI Device ID Table
2757 * Wildcard entries (PCI_ANY_ID) should come last
2758 * Last entry must be all 0s
2760 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
2761 * Class, Class Mask, private data (not used) }
2763 static const struct pci_device_id ice_pci_tbl[] = {
2764 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
2765 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
2766 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
2767 /* required last entry */
2770 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
2772 static const struct pci_error_handlers ice_pci_err_handler = {
2773 .error_detected = ice_pci_err_detected,
2774 .slot_reset = ice_pci_err_slot_reset,
2775 .reset_prepare = ice_pci_err_reset_prepare,
2776 .reset_done = ice_pci_err_reset_done,
2777 .resume = ice_pci_err_resume
2780 static struct pci_driver ice_driver = {
2781 .name = KBUILD_MODNAME,
2782 .id_table = ice_pci_tbl,
2784 .remove = ice_remove,
2785 .sriov_configure = ice_sriov_configure,
2786 .err_handler = &ice_pci_err_handler
2790 * ice_module_init - Driver registration routine
2792 * ice_module_init is the first routine called when the driver is
2793 * loaded. All it does is register with the PCI subsystem.
2795 static int __init ice_module_init(void)
2799 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
2800 pr_info("%s\n", ice_copyright);
2802 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
2804 pr_err("Failed to create workqueue\n");
2808 status = pci_register_driver(&ice_driver);
2810 pr_err("failed to register PCI driver, err %d\n", status);
2811 destroy_workqueue(ice_wq);
2816 module_init(ice_module_init);
2819 * ice_module_exit - Driver exit cleanup routine
2821 * ice_module_exit is called just before the driver is removed
2824 static void __exit ice_module_exit(void)
2826 pci_unregister_driver(&ice_driver);
2827 destroy_workqueue(ice_wq);
2828 pr_info("module unloaded\n");
2830 module_exit(ice_module_exit);
2833 * ice_set_mac_address - NDO callback to set MAC address
2834 * @netdev: network interface device structure
2835 * @pi: pointer to an address structure
2837 * Returns 0 on success, negative on failure
2839 static int ice_set_mac_address(struct net_device *netdev, void *pi)
2841 struct ice_netdev_priv *np = netdev_priv(netdev);
2842 struct ice_vsi *vsi = np->vsi;
2843 struct ice_pf *pf = vsi->back;
2844 struct ice_hw *hw = &pf->hw;
2845 struct sockaddr *addr = pi;
2846 enum ice_status status;
2847 LIST_HEAD(a_mac_list);
2848 LIST_HEAD(r_mac_list);
2853 mac = (u8 *)addr->sa_data;
2855 if (!is_valid_ether_addr(mac))
2856 return -EADDRNOTAVAIL;
2858 if (ether_addr_equal(netdev->dev_addr, mac)) {
2859 netdev_warn(netdev, "already using mac %pM\n", mac);
2863 if (test_bit(__ICE_DOWN, pf->state) ||
2864 ice_is_reset_in_progress(pf->state)) {
2865 netdev_err(netdev, "can't set mac %pM. device not ready\n",
2870 /* When we change the MAC address we also have to change the MAC address
2871 * based filter rules that were created previously for the old MAC
2872 * address. So first, we remove the old filter rule using ice_remove_mac
2873 * and then create a new filter rule using ice_add_mac. Note that for
2874 * both these operations, we first need to form a "list" of MAC
2875 * addresses (even though in this case, we have only 1 MAC address to be
2876 * added/removed) and this done using ice_add_mac_to_list. Depending on
2877 * the ensuing operation this "list" of MAC addresses is either to be
2878 * added or removed from the filter.
2880 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
2882 err = -EADDRNOTAVAIL;
2886 status = ice_remove_mac(hw, &r_mac_list);
2888 err = -EADDRNOTAVAIL;
2892 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
2894 err = -EADDRNOTAVAIL;
2898 status = ice_add_mac(hw, &a_mac_list);
2900 err = -EADDRNOTAVAIL;
2905 /* free list entries */
2906 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
2907 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
2910 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
2915 /* change the netdev's MAC address */
2916 memcpy(netdev->dev_addr, mac, netdev->addr_len);
2917 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
2920 /* write new MAC address to the firmware */
2921 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2922 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
2924 netdev_err(netdev, "can't set MAC %pM. write to firmware failed.\n",
2931 * ice_set_rx_mode - NDO callback to set the netdev filters
2932 * @netdev: network interface device structure
2934 static void ice_set_rx_mode(struct net_device *netdev)
2936 struct ice_netdev_priv *np = netdev_priv(netdev);
2937 struct ice_vsi *vsi = np->vsi;
2942 /* Set the flags to synchronize filters
2943 * ndo_set_rx_mode may be triggered even without a change in netdev
2946 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
2947 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
2948 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
2950 /* schedule our worker thread which will take care of
2951 * applying the new filter changes
2953 ice_service_task_schedule(vsi->back);
2957 * ice_fdb_add - add an entry to the hardware database
2958 * @ndm: the input from the stack
2959 * @tb: pointer to array of nladdr (unused)
2960 * @dev: the net device pointer
2961 * @addr: the MAC address entry being added
2963 * @flags: instructions from stack about fdb operation
2964 * @extack: netlink extended ack
2967 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
2968 struct net_device *dev, const unsigned char *addr, u16 vid,
2969 u16 flags, struct netlink_ext_ack __always_unused *extack)
2974 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
2977 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2978 netdev_err(dev, "FDB only supports static addresses\n");
2982 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2983 err = dev_uc_add_excl(dev, addr);
2984 else if (is_multicast_ether_addr(addr))
2985 err = dev_mc_add_excl(dev, addr);
2989 /* Only return duplicate errors if NLM_F_EXCL is set */
2990 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2997 * ice_fdb_del - delete an entry from the hardware database
2998 * @ndm: the input from the stack
2999 * @tb: pointer to array of nladdr (unused)
3000 * @dev: the net device pointer
3001 * @addr: the MAC address entry being added
3005 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
3006 struct net_device *dev, const unsigned char *addr,
3007 __always_unused u16 vid)
3011 if (ndm->ndm_state & NUD_PERMANENT) {
3012 netdev_err(dev, "FDB only supports static addresses\n");
3016 if (is_unicast_ether_addr(addr))
3017 err = dev_uc_del(dev, addr);
3018 else if (is_multicast_ether_addr(addr))
3019 err = dev_mc_del(dev, addr);
3027 * ice_set_features - set the netdev feature flags
3028 * @netdev: ptr to the netdev being adjusted
3029 * @features: the feature set that the stack is suggesting
3032 ice_set_features(struct net_device *netdev, netdev_features_t features)
3034 struct ice_netdev_priv *np = netdev_priv(netdev);
3035 struct ice_vsi *vsi = np->vsi;
3038 /* Multiple features can be changed in one call so keep features in
3039 * separate if/else statements to guarantee each feature is checked
3041 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
3042 ret = ice_vsi_manage_rss_lut(vsi, true);
3043 else if (!(features & NETIF_F_RXHASH) &&
3044 netdev->features & NETIF_F_RXHASH)
3045 ret = ice_vsi_manage_rss_lut(vsi, false);
3047 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3048 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3049 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3050 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3051 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3052 ret = ice_vsi_manage_vlan_stripping(vsi, false);
3054 if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3055 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3056 ret = ice_vsi_manage_vlan_insertion(vsi);
3057 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3058 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3059 ret = ice_vsi_manage_vlan_insertion(vsi);
3061 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3062 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3063 ret = ice_cfg_vlan_pruning(vsi, true, false);
3064 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3065 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3066 ret = ice_cfg_vlan_pruning(vsi, false, false);
3072 * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
3073 * @vsi: VSI to setup VLAN properties for
3075 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3079 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3080 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3081 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3082 ret = ice_vsi_manage_vlan_insertion(vsi);
3088 * ice_vsi_cfg - Setup the VSI
3089 * @vsi: the VSI being configured
3091 * Return 0 on success and negative value on error
3093 int ice_vsi_cfg(struct ice_vsi *vsi)
3098 ice_set_rx_mode(vsi->netdev);
3100 err = ice_vsi_vlan_setup(vsi);
3105 ice_vsi_cfg_dcb_rings(vsi);
3107 err = ice_vsi_cfg_lan_txqs(vsi);
3109 err = ice_vsi_cfg_rxqs(vsi);
3115 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3116 * @vsi: the VSI being configured
3118 static void ice_napi_enable_all(struct ice_vsi *vsi)
3125 ice_for_each_q_vector(vsi, q_idx) {
3126 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3128 if (q_vector->rx.ring || q_vector->tx.ring)
3129 napi_enable(&q_vector->napi);
3134 * ice_up_complete - Finish the last steps of bringing up a connection
3135 * @vsi: The VSI being configured
3137 * Return 0 on success and negative value on error
3139 static int ice_up_complete(struct ice_vsi *vsi)
3141 struct ice_pf *pf = vsi->back;
3144 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3145 ice_vsi_cfg_msix(vsi);
3149 /* Enable only Rx rings, Tx rings were enabled by the FW when the
3150 * Tx queue group list was configured and the context bits were
3151 * programmed using ice_vsi_cfg_txqs
3153 err = ice_vsi_start_rx_rings(vsi);
3157 clear_bit(__ICE_DOWN, vsi->state);
3158 ice_napi_enable_all(vsi);
3159 ice_vsi_ena_irq(vsi);
3161 if (vsi->port_info &&
3162 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
3164 ice_print_link_msg(vsi, true);
3165 netif_tx_start_all_queues(vsi->netdev);
3166 netif_carrier_on(vsi->netdev);
3169 ice_service_task_schedule(pf);
3175 * ice_up - Bring the connection back up after being down
3176 * @vsi: VSI being configured
3178 int ice_up(struct ice_vsi *vsi)
3182 err = ice_vsi_cfg(vsi);
3184 err = ice_up_complete(vsi);
3190 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
3191 * @ring: Tx or Rx ring to read stats from
3192 * @pkts: packets stats counter
3193 * @bytes: bytes stats counter
3195 * This function fetches stats from the ring considering the atomic operations
3196 * that needs to be performed to read u64 values in 32 bit machine.
3199 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
3208 start = u64_stats_fetch_begin_irq(&ring->syncp);
3209 *pkts = ring->stats.pkts;
3210 *bytes = ring->stats.bytes;
3211 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3215 * ice_update_vsi_ring_stats - Update VSI stats counters
3216 * @vsi: the VSI to be updated
3218 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
3220 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
3221 struct ice_ring *ring;
3225 /* reset netdev stats */
3226 vsi_stats->tx_packets = 0;
3227 vsi_stats->tx_bytes = 0;
3228 vsi_stats->rx_packets = 0;
3229 vsi_stats->rx_bytes = 0;
3231 /* reset non-netdev (extended) stats */
3232 vsi->tx_restart = 0;
3234 vsi->tx_linearize = 0;
3235 vsi->rx_buf_failed = 0;
3236 vsi->rx_page_failed = 0;
3240 /* update Tx rings counters */
3241 ice_for_each_txq(vsi, i) {
3242 ring = READ_ONCE(vsi->tx_rings[i]);
3243 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3244 vsi_stats->tx_packets += pkts;
3245 vsi_stats->tx_bytes += bytes;
3246 vsi->tx_restart += ring->tx_stats.restart_q;
3247 vsi->tx_busy += ring->tx_stats.tx_busy;
3248 vsi->tx_linearize += ring->tx_stats.tx_linearize;
3251 /* update Rx rings counters */
3252 ice_for_each_rxq(vsi, i) {
3253 ring = READ_ONCE(vsi->rx_rings[i]);
3254 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3255 vsi_stats->rx_packets += pkts;
3256 vsi_stats->rx_bytes += bytes;
3257 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
3258 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
3265 * ice_update_vsi_stats - Update VSI stats counters
3266 * @vsi: the VSI to be updated
3268 static void ice_update_vsi_stats(struct ice_vsi *vsi)
3270 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
3271 struct ice_eth_stats *cur_es = &vsi->eth_stats;
3272 struct ice_pf *pf = vsi->back;
3274 if (test_bit(__ICE_DOWN, vsi->state) ||
3275 test_bit(__ICE_CFG_BUSY, pf->state))
3278 /* get stats as recorded by Tx/Rx rings */
3279 ice_update_vsi_ring_stats(vsi);
3281 /* get VSI stats as recorded by the hardware */
3282 ice_update_eth_stats(vsi);
3284 cur_ns->tx_errors = cur_es->tx_errors;
3285 cur_ns->rx_dropped = cur_es->rx_discards;
3286 cur_ns->tx_dropped = cur_es->tx_discards;
3287 cur_ns->multicast = cur_es->rx_multicast;
3289 /* update some more netdev stats if this is main VSI */
3290 if (vsi->type == ICE_VSI_PF) {
3291 cur_ns->rx_crc_errors = pf->stats.crc_errors;
3292 cur_ns->rx_errors = pf->stats.crc_errors +
3293 pf->stats.illegal_bytes;
3294 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
3299 * ice_update_pf_stats - Update PF port stats counters
3300 * @pf: PF whose stats needs to be updated
3302 static void ice_update_pf_stats(struct ice_pf *pf)
3304 struct ice_hw_port_stats *prev_ps, *cur_ps;
3305 struct ice_hw *hw = &pf->hw;
3308 prev_ps = &pf->stats_prev;
3309 cur_ps = &pf->stats;
3312 ice_stat_update40(hw, GLPRT_GORCL(pf_id), pf->stat_prev_loaded,
3313 &prev_ps->eth.rx_bytes,
3314 &cur_ps->eth.rx_bytes);
3316 ice_stat_update40(hw, GLPRT_UPRCL(pf_id), pf->stat_prev_loaded,
3317 &prev_ps->eth.rx_unicast,
3318 &cur_ps->eth.rx_unicast);
3320 ice_stat_update40(hw, GLPRT_MPRCL(pf_id), pf->stat_prev_loaded,
3321 &prev_ps->eth.rx_multicast,
3322 &cur_ps->eth.rx_multicast);
3324 ice_stat_update40(hw, GLPRT_BPRCL(pf_id), pf->stat_prev_loaded,
3325 &prev_ps->eth.rx_broadcast,
3326 &cur_ps->eth.rx_broadcast);
3328 ice_stat_update40(hw, GLPRT_GOTCL(pf_id), pf->stat_prev_loaded,
3329 &prev_ps->eth.tx_bytes,
3330 &cur_ps->eth.tx_bytes);
3332 ice_stat_update40(hw, GLPRT_UPTCL(pf_id), pf->stat_prev_loaded,
3333 &prev_ps->eth.tx_unicast,
3334 &cur_ps->eth.tx_unicast);
3336 ice_stat_update40(hw, GLPRT_MPTCL(pf_id), pf->stat_prev_loaded,
3337 &prev_ps->eth.tx_multicast,
3338 &cur_ps->eth.tx_multicast);
3340 ice_stat_update40(hw, GLPRT_BPTCL(pf_id), pf->stat_prev_loaded,
3341 &prev_ps->eth.tx_broadcast,
3342 &cur_ps->eth.tx_broadcast);
3344 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
3345 &prev_ps->tx_dropped_link_down,
3346 &cur_ps->tx_dropped_link_down);
3348 ice_stat_update40(hw, GLPRT_PRC64L(pf_id), pf->stat_prev_loaded,
3349 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
3351 ice_stat_update40(hw, GLPRT_PRC127L(pf_id), pf->stat_prev_loaded,
3352 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
3354 ice_stat_update40(hw, GLPRT_PRC255L(pf_id), pf->stat_prev_loaded,
3355 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
3357 ice_stat_update40(hw, GLPRT_PRC511L(pf_id), pf->stat_prev_loaded,
3358 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
3360 ice_stat_update40(hw, GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
3361 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
3363 ice_stat_update40(hw, GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
3364 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
3366 ice_stat_update40(hw, GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
3367 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
3369 ice_stat_update40(hw, GLPRT_PTC64L(pf_id), pf->stat_prev_loaded,
3370 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
3372 ice_stat_update40(hw, GLPRT_PTC127L(pf_id), pf->stat_prev_loaded,
3373 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
3375 ice_stat_update40(hw, GLPRT_PTC255L(pf_id), pf->stat_prev_loaded,
3376 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
3378 ice_stat_update40(hw, GLPRT_PTC511L(pf_id), pf->stat_prev_loaded,
3379 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
3381 ice_stat_update40(hw, GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
3382 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
3384 ice_stat_update40(hw, GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
3385 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
3387 ice_stat_update40(hw, GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
3388 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
3390 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
3391 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
3393 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
3394 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
3396 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
3397 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
3399 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
3400 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
3402 ice_update_dcb_stats(pf);
3404 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
3405 &prev_ps->crc_errors, &cur_ps->crc_errors);
3407 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
3408 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
3410 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
3411 &prev_ps->mac_local_faults,
3412 &cur_ps->mac_local_faults);
3414 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
3415 &prev_ps->mac_remote_faults,
3416 &cur_ps->mac_remote_faults);
3418 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
3419 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
3421 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
3422 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
3424 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
3425 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
3427 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
3428 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
3430 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
3431 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
3433 pf->stat_prev_loaded = true;
3437 * ice_get_stats64 - get statistics for network device structure
3438 * @netdev: network interface device structure
3439 * @stats: main device statistics structure
3442 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
3444 struct ice_netdev_priv *np = netdev_priv(netdev);
3445 struct rtnl_link_stats64 *vsi_stats;
3446 struct ice_vsi *vsi = np->vsi;
3448 vsi_stats = &vsi->net_stats;
3450 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
3452 /* netdev packet/byte stats come from ring counter. These are obtained
3453 * by summing up ring counters (done by ice_update_vsi_ring_stats).
3455 ice_update_vsi_ring_stats(vsi);
3456 stats->tx_packets = vsi_stats->tx_packets;
3457 stats->tx_bytes = vsi_stats->tx_bytes;
3458 stats->rx_packets = vsi_stats->rx_packets;
3459 stats->rx_bytes = vsi_stats->rx_bytes;
3461 /* The rest of the stats can be read from the hardware but instead we
3462 * just return values that the watchdog task has already obtained from
3465 stats->multicast = vsi_stats->multicast;
3466 stats->tx_errors = vsi_stats->tx_errors;
3467 stats->tx_dropped = vsi_stats->tx_dropped;
3468 stats->rx_errors = vsi_stats->rx_errors;
3469 stats->rx_dropped = vsi_stats->rx_dropped;
3470 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
3471 stats->rx_length_errors = vsi_stats->rx_length_errors;
3475 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3476 * @vsi: VSI having NAPI disabled
3478 static void ice_napi_disable_all(struct ice_vsi *vsi)
3485 ice_for_each_q_vector(vsi, q_idx) {
3486 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3488 if (q_vector->rx.ring || q_vector->tx.ring)
3489 napi_disable(&q_vector->napi);
3494 * ice_down - Shutdown the connection
3495 * @vsi: The VSI being stopped
3497 int ice_down(struct ice_vsi *vsi)
3499 int i, tx_err, rx_err, link_err = 0;
3501 /* Caller of this function is expected to set the
3502 * vsi->state __ICE_DOWN bit
3505 netif_carrier_off(vsi->netdev);
3506 netif_tx_disable(vsi->netdev);
3509 ice_vsi_dis_irq(vsi);
3511 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
3513 netdev_err(vsi->netdev,
3514 "Failed stop Tx rings, VSI %d error %d\n",
3515 vsi->vsi_num, tx_err);
3517 rx_err = ice_vsi_stop_rx_rings(vsi);
3519 netdev_err(vsi->netdev,
3520 "Failed stop Rx rings, VSI %d error %d\n",
3521 vsi->vsi_num, rx_err);
3523 ice_napi_disable_all(vsi);
3525 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
3526 link_err = ice_force_phys_link_state(vsi, false);
3528 netdev_err(vsi->netdev,
3529 "Failed to set physical link down, VSI %d error %d\n",
3530 vsi->vsi_num, link_err);
3533 ice_for_each_txq(vsi, i)
3534 ice_clean_tx_ring(vsi->tx_rings[i]);
3536 ice_for_each_rxq(vsi, i)
3537 ice_clean_rx_ring(vsi->rx_rings[i]);
3539 if (tx_err || rx_err || link_err) {
3540 netdev_err(vsi->netdev,
3541 "Failed to close VSI 0x%04X on switch 0x%04X\n",
3542 vsi->vsi_num, vsi->vsw->sw_id);
3550 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
3551 * @vsi: VSI having resources allocated
3553 * Return 0 on success, negative on failure
3555 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
3559 if (!vsi->num_txq) {
3560 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
3565 ice_for_each_txq(vsi, i) {
3566 vsi->tx_rings[i]->netdev = vsi->netdev;
3567 err = ice_setup_tx_ring(vsi->tx_rings[i]);
3576 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
3577 * @vsi: VSI having resources allocated
3579 * Return 0 on success, negative on failure
3581 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
3585 if (!vsi->num_rxq) {
3586 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
3591 ice_for_each_rxq(vsi, i) {
3592 vsi->rx_rings[i]->netdev = vsi->netdev;
3593 err = ice_setup_rx_ring(vsi->rx_rings[i]);
3602 * ice_vsi_req_irq - Request IRQ from the OS
3603 * @vsi: The VSI IRQ is being requested for
3604 * @basename: name for the vector
3606 * Return 0 on success and a negative value on error
3608 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
3610 struct ice_pf *pf = vsi->back;
3613 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3614 err = ice_vsi_req_irq_msix(vsi, basename);
3620 * ice_vsi_open - Called when a network interface is made active
3621 * @vsi: the VSI to open
3623 * Initialization of the VSI
3625 * Returns 0 on success, negative value on error
3627 static int ice_vsi_open(struct ice_vsi *vsi)
3629 char int_name[ICE_INT_NAME_STR_LEN];
3630 struct ice_pf *pf = vsi->back;
3633 /* allocate descriptors */
3634 err = ice_vsi_setup_tx_rings(vsi);
3638 err = ice_vsi_setup_rx_rings(vsi);
3642 err = ice_vsi_cfg(vsi);
3646 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3647 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
3648 err = ice_vsi_req_irq(vsi, int_name);
3652 /* Notify the stack of the actual queue counts. */
3653 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
3657 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
3661 err = ice_up_complete(vsi);
3663 goto err_up_complete;
3670 ice_vsi_free_irq(vsi);
3672 ice_vsi_free_rx_rings(vsi);
3674 ice_vsi_free_tx_rings(vsi);
3680 * ice_vsi_release_all - Delete all VSIs
3681 * @pf: PF from which all VSIs are being removed
3683 static void ice_vsi_release_all(struct ice_pf *pf)
3690 ice_for_each_vsi(pf, i) {
3694 err = ice_vsi_release(pf->vsi[i]);
3696 dev_dbg(&pf->pdev->dev,
3697 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
3698 i, err, pf->vsi[i]->vsi_num);
3703 * ice_ena_vsi - resume a VSI
3704 * @vsi: the VSI being resume
3705 * @locked: is the rtnl_lock already held
3707 static int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
3711 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
3714 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
3716 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
3717 struct net_device *netd = vsi->netdev;
3719 if (netif_running(vsi->netdev)) {
3721 err = netd->netdev_ops->ndo_open(netd);
3724 err = netd->netdev_ops->ndo_open(netd);
3728 err = ice_vsi_open(vsi);
3736 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
3738 * @locked: is the rtnl_lock already held
3741 int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3743 static int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3744 #endif /* CONFIG_DCB */
3748 ice_for_each_vsi(pf, v)
3750 if (ice_ena_vsi(pf->vsi[v], locked))
3757 * ice_vsi_rebuild_all - rebuild all VSIs in PF
3760 static int ice_vsi_rebuild_all(struct ice_pf *pf)
3764 /* loop through pf->vsi array and reinit the VSI if found */
3765 ice_for_each_vsi(pf, i) {
3771 err = ice_vsi_rebuild(pf->vsi[i]);
3773 dev_err(&pf->pdev->dev,
3774 "VSI at index %d rebuild failed\n",
3779 dev_info(&pf->pdev->dev,
3780 "VSI at index %d rebuilt. vsi_num = 0x%x\n",
3781 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3788 * ice_vsi_replay_all - replay all VSIs configuration in the PF
3791 static int ice_vsi_replay_all(struct ice_pf *pf)
3793 struct ice_hw *hw = &pf->hw;
3794 enum ice_status ret;
3797 /* loop through pf->vsi array and replay the VSI if found */
3798 ice_for_each_vsi(pf, i) {
3802 ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
3804 dev_err(&pf->pdev->dev,
3805 "VSI at index %d replay failed %d\n",
3806 pf->vsi[i]->idx, ret);
3810 /* Re-map HW VSI number, using VSI handle that has been
3811 * previously validated in ice_replay_vsi() call above
3813 pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
3815 dev_info(&pf->pdev->dev,
3816 "VSI at index %d filter replayed successfully - vsi_num %i\n",
3817 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3820 /* Clean up replay filter after successful re-configuration */
3821 ice_replay_post(hw);
3826 * ice_rebuild - rebuild after reset
3827 * @pf: PF to rebuild
3829 static void ice_rebuild(struct ice_pf *pf)
3831 struct device *dev = &pf->pdev->dev;
3832 struct ice_hw *hw = &pf->hw;
3833 enum ice_status ret;
3836 if (test_bit(__ICE_DOWN, pf->state))
3837 goto clear_recovery;
3839 dev_dbg(dev, "rebuilding PF\n");
3841 ret = ice_init_all_ctrlq(hw);
3843 dev_err(dev, "control queues init failed %d\n", ret);
3844 goto err_init_ctrlq;
3847 ret = ice_clear_pf_cfg(hw);
3849 dev_err(dev, "clear PF configuration failed %d\n", ret);
3850 goto err_init_ctrlq;
3853 ice_clear_pxe_mode(hw);
3855 ret = ice_get_caps(hw);
3857 dev_err(dev, "ice_get_caps failed %d\n", ret);
3858 goto err_init_ctrlq;
3861 err = ice_sched_init_port(hw->port_info);
3863 goto err_sched_init_port;
3865 ice_dcb_rebuild(pf);
3867 err = ice_vsi_rebuild_all(pf);
3869 dev_err(dev, "ice_vsi_rebuild_all failed\n");
3870 goto err_vsi_rebuild;
3873 err = ice_update_link_info(hw->port_info);
3875 dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
3877 /* Replay all VSIs Configuration, including filters after reset */
3878 if (ice_vsi_replay_all(pf)) {
3879 dev_err(&pf->pdev->dev,
3880 "error replaying VSI configurations with switch filter rules\n");
3881 goto err_vsi_rebuild;
3884 /* start misc vector */
3885 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3886 err = ice_req_irq_msix_misc(pf);
3888 dev_err(dev, "misc vector setup failed: %d\n", err);
3889 goto err_vsi_rebuild;
3893 /* restart the VSIs that were rebuilt and running before the reset */
3894 err = ice_pf_ena_all_vsi(pf, false);
3896 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
3897 /* no need to disable VSIs in tear down path in ice_rebuild()
3898 * since its already taken care in ice_vsi_open()
3900 goto err_vsi_rebuild;
3903 ice_for_each_vsi(pf, i) {
3906 if (!pf->vsi[i] || pf->vsi[i]->type != ICE_VSI_PF)
3908 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
3910 netif_carrier_on(pf->vsi[i]->netdev);
3911 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
3913 netif_carrier_off(pf->vsi[i]->netdev);
3914 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
3918 /* if we get here, reset flow is successful */
3919 clear_bit(__ICE_RESET_FAILED, pf->state);
3923 ice_vsi_release_all(pf);
3924 err_sched_init_port:
3925 ice_sched_cleanup_all(hw);
3927 ice_shutdown_all_ctrlq(hw);
3928 set_bit(__ICE_RESET_FAILED, pf->state);
3930 /* set this bit in PF state to control service task scheduling */
3931 set_bit(__ICE_NEEDS_RESTART, pf->state);
3932 dev_err(dev, "Rebuild failed, unload and reload driver\n");
3936 * ice_change_mtu - NDO callback to change the MTU
3937 * @netdev: network interface device structure
3938 * @new_mtu: new value for maximum frame size
3940 * Returns 0 on success, negative on failure
3942 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
3944 struct ice_netdev_priv *np = netdev_priv(netdev);
3945 struct ice_vsi *vsi = np->vsi;
3946 struct ice_pf *pf = vsi->back;
3949 if (new_mtu == netdev->mtu) {
3950 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
3954 if (new_mtu < netdev->min_mtu) {
3955 netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
3958 } else if (new_mtu > netdev->max_mtu) {
3959 netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
3963 /* if a reset is in progress, wait for some time for it to complete */
3965 if (ice_is_reset_in_progress(pf->state)) {
3967 usleep_range(1000, 2000);
3972 } while (count < 100);
3975 netdev_err(netdev, "can't change MTU. Device is busy\n");
3979 netdev->mtu = new_mtu;
3981 /* if VSI is up, bring it down and then back up */
3982 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
3985 err = ice_down(vsi);
3987 netdev_err(netdev, "change MTU if_up err %d\n", err);
3993 netdev_err(netdev, "change MTU if_up err %d\n", err);
3998 netdev_info(netdev, "changed MTU to %d\n", new_mtu);
4003 * ice_set_rss - Set RSS keys and lut
4004 * @vsi: Pointer to VSI structure
4005 * @seed: RSS hash seed
4006 * @lut: Lookup table
4007 * @lut_size: Lookup table size
4009 * Returns 0 on success, negative on failure
4011 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4013 struct ice_pf *pf = vsi->back;
4014 struct ice_hw *hw = &pf->hw;
4015 enum ice_status status;
4018 struct ice_aqc_get_set_rss_keys *buf =
4019 (struct ice_aqc_get_set_rss_keys *)seed;
4021 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
4024 dev_err(&pf->pdev->dev,
4025 "Cannot set RSS key, err %d aq_err %d\n",
4026 status, hw->adminq.rq_last_status);
4032 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4035 dev_err(&pf->pdev->dev,
4036 "Cannot set RSS lut, err %d aq_err %d\n",
4037 status, hw->adminq.rq_last_status);
4046 * ice_get_rss - Get RSS keys and lut
4047 * @vsi: Pointer to VSI structure
4048 * @seed: Buffer to store the keys
4049 * @lut: Buffer to store the lookup table entries
4050 * @lut_size: Size of buffer to store the lookup table entries
4052 * Returns 0 on success, negative on failure
4054 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
4056 struct ice_pf *pf = vsi->back;
4057 struct ice_hw *hw = &pf->hw;
4058 enum ice_status status;
4061 struct ice_aqc_get_set_rss_keys *buf =
4062 (struct ice_aqc_get_set_rss_keys *)seed;
4064 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
4066 dev_err(&pf->pdev->dev,
4067 "Cannot get RSS key, err %d aq_err %d\n",
4068 status, hw->adminq.rq_last_status);
4074 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
4077 dev_err(&pf->pdev->dev,
4078 "Cannot get RSS lut, err %d aq_err %d\n",
4079 status, hw->adminq.rq_last_status);
4088 * ice_bridge_getlink - Get the hardware bridge mode
4091 * @seq: RTNL message seq
4092 * @dev: the netdev being configured
4093 * @filter_mask: filter mask passed in
4094 * @nlflags: netlink flags passed in
4096 * Return the bridge mode (VEB/VEPA)
4099 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4100 struct net_device *dev, u32 filter_mask, int nlflags)
4102 struct ice_netdev_priv *np = netdev_priv(dev);
4103 struct ice_vsi *vsi = np->vsi;
4104 struct ice_pf *pf = vsi->back;
4107 bmode = pf->first_sw->bridge_mode;
4109 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
4114 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
4115 * @vsi: Pointer to VSI structure
4116 * @bmode: Hardware bridge mode (VEB/VEPA)
4118 * Returns 0 on success, negative on failure
4120 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
4122 struct device *dev = &vsi->back->pdev->dev;
4123 struct ice_aqc_vsi_props *vsi_props;
4124 struct ice_hw *hw = &vsi->back->hw;
4125 struct ice_vsi_ctx *ctxt;
4126 enum ice_status status;
4129 vsi_props = &vsi->info;
4131 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
4135 ctxt->info = vsi->info;
4137 if (bmode == BRIDGE_MODE_VEB)
4138 /* change from VEPA to VEB mode */
4139 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4141 /* change from VEB to VEPA mode */
4142 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4143 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4145 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4147 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
4148 bmode, status, hw->adminq.sq_last_status);
4152 /* Update sw flags for book keeping */
4153 vsi_props->sw_flags = ctxt->info.sw_flags;
4156 devm_kfree(dev, ctxt);
4161 * ice_bridge_setlink - Set the hardware bridge mode
4162 * @dev: the netdev being configured
4163 * @nlh: RTNL message
4164 * @flags: bridge setlink flags
4165 * @extack: netlink extended ack
4167 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
4168 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
4169 * not already set for all VSIs connected to this switch. And also update the
4170 * unicast switch filter rules for the corresponding switch of the netdev.
4173 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4174 u16 __always_unused flags,
4175 struct netlink_ext_ack __always_unused *extack)
4177 struct ice_netdev_priv *np = netdev_priv(dev);
4178 struct ice_pf *pf = np->vsi->back;
4179 struct nlattr *attr, *br_spec;
4180 struct ice_hw *hw = &pf->hw;
4181 enum ice_status status;
4182 struct ice_sw *pf_sw;
4183 int rem, v, err = 0;
4185 pf_sw = pf->first_sw;
4186 /* find the attribute in the netlink message */
4187 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4189 nla_for_each_nested(attr, br_spec, rem) {
4192 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4194 mode = nla_get_u16(attr);
4195 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4197 /* Continue if bridge mode is not being flipped */
4198 if (mode == pf_sw->bridge_mode)
4200 /* Iterates through the PF VSI list and update the loopback
4203 ice_for_each_vsi(pf, v) {
4206 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
4211 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
4212 /* Update the unicast switch filter rules for the corresponding
4213 * switch of the netdev
4215 status = ice_update_sw_rule_bridge_mode(hw);
4217 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n",
4218 mode, status, hw->adminq.sq_last_status);
4219 /* revert hw->evb_veb */
4220 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
4224 pf_sw->bridge_mode = mode;
4231 * ice_tx_timeout - Respond to a Tx Hang
4232 * @netdev: network interface device structure
4234 static void ice_tx_timeout(struct net_device *netdev)
4236 struct ice_netdev_priv *np = netdev_priv(netdev);
4237 struct ice_ring *tx_ring = NULL;
4238 struct ice_vsi *vsi = np->vsi;
4239 struct ice_pf *pf = vsi->back;
4240 int hung_queue = -1;
4243 pf->tx_timeout_count++;
4245 /* find the stopped queue the same way dev_watchdog() does */
4246 for (i = 0; i < netdev->num_tx_queues; i++) {
4247 unsigned long trans_start;
4248 struct netdev_queue *q;
4250 q = netdev_get_tx_queue(netdev, i);
4251 trans_start = q->trans_start;
4252 if (netif_xmit_stopped(q) &&
4254 trans_start + netdev->watchdog_timeo)) {
4260 if (i == netdev->num_tx_queues)
4261 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
4263 /* now that we have an index, find the tx_ring struct */
4264 for (i = 0; i < vsi->num_txq; i++)
4265 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4266 if (hung_queue == vsi->tx_rings[i]->q_index) {
4267 tx_ring = vsi->tx_rings[i];
4271 /* Reset recovery level if enough time has elapsed after last timeout.
4272 * Also ensure no new reset action happens before next timeout period.
4274 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
4275 pf->tx_timeout_recovery_level = 1;
4276 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
4277 netdev->watchdog_timeo)))
4281 struct ice_hw *hw = &pf->hw;
4284 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[hung_queue])) &
4285 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
4286 /* Read interrupt register */
4287 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4289 GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
4291 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
4292 vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
4293 head, tx_ring->next_to_use, val);
4296 pf->tx_timeout_last_recovery = jiffies;
4297 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
4298 pf->tx_timeout_recovery_level, hung_queue);
4300 switch (pf->tx_timeout_recovery_level) {
4302 set_bit(__ICE_PFR_REQ, pf->state);
4305 set_bit(__ICE_CORER_REQ, pf->state);
4308 set_bit(__ICE_GLOBR_REQ, pf->state);
4311 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
4312 set_bit(__ICE_DOWN, pf->state);
4313 set_bit(__ICE_NEEDS_RESTART, vsi->state);
4314 set_bit(__ICE_SERVICE_DIS, pf->state);
4318 ice_service_task_schedule(pf);
4319 pf->tx_timeout_recovery_level++;
4323 * ice_open - Called when a network interface becomes active
4324 * @netdev: network interface device structure
4326 * The open entry point is called when a network interface is made
4327 * active by the system (IFF_UP). At this point all resources needed
4328 * for transmit and receive operations are allocated, the interrupt
4329 * handler is registered with the OS, the netdev watchdog is enabled,
4330 * and the stack is notified that the interface is ready.
4332 * Returns 0 on success, negative value on failure
4334 int ice_open(struct net_device *netdev)
4336 struct ice_netdev_priv *np = netdev_priv(netdev);
4337 struct ice_vsi *vsi = np->vsi;
4338 struct ice_port_info *pi;
4341 if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
4342 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
4346 netif_carrier_off(netdev);
4348 pi = vsi->port_info;
4349 err = ice_update_link_info(pi);
4351 netdev_err(netdev, "Failed to get link info, error %d\n",
4356 /* Set PHY if there is media, otherwise, turn off PHY */
4357 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
4358 err = ice_force_phys_link_state(vsi, true);
4361 "Failed to set physical link up, error %d\n",
4366 err = ice_aq_set_link_restart_an(pi, false, NULL);
4368 netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
4372 set_bit(ICE_FLAG_NO_MEDIA, vsi->back->flags);
4375 err = ice_vsi_open(vsi);
4377 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
4378 vsi->vsi_num, vsi->vsw->sw_id);
4383 * ice_stop - Disables a network interface
4384 * @netdev: network interface device structure
4386 * The stop entry point is called when an interface is de-activated by the OS,
4387 * and the netdevice enters the DOWN state. The hardware is still under the
4388 * driver's control, but the netdev interface is disabled.
4390 * Returns success only - not allowed to fail
4392 int ice_stop(struct net_device *netdev)
4394 struct ice_netdev_priv *np = netdev_priv(netdev);
4395 struct ice_vsi *vsi = np->vsi;
4403 * ice_features_check - Validate encapsulated packet conforms to limits
4405 * @netdev: This port's netdev
4406 * @features: Offload features that the stack believes apply
4408 static netdev_features_t
4409 ice_features_check(struct sk_buff *skb,
4410 struct net_device __always_unused *netdev,
4411 netdev_features_t features)
4415 /* No point in doing any of this if neither checksum nor GSO are
4416 * being requested for this frame. We can rule out both by just
4417 * checking for CHECKSUM_PARTIAL
4419 if (skb->ip_summed != CHECKSUM_PARTIAL)
4422 /* We cannot support GSO if the MSS is going to be less than
4423 * 64 bytes. If it is then we need to drop support for GSO.
4425 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4426 features &= ~NETIF_F_GSO_MASK;
4428 len = skb_network_header(skb) - skb->data;
4429 if (len & ~(ICE_TXD_MACLEN_MAX))
4430 goto out_rm_features;
4432 len = skb_transport_header(skb) - skb_network_header(skb);
4433 if (len & ~(ICE_TXD_IPLEN_MAX))
4434 goto out_rm_features;
4436 if (skb->encapsulation) {
4437 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4438 if (len & ~(ICE_TXD_L4LEN_MAX))
4439 goto out_rm_features;
4441 len = skb_inner_transport_header(skb) -
4442 skb_inner_network_header(skb);
4443 if (len & ~(ICE_TXD_IPLEN_MAX))
4444 goto out_rm_features;
4449 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4452 static const struct net_device_ops ice_netdev_ops = {
4453 .ndo_open = ice_open,
4454 .ndo_stop = ice_stop,
4455 .ndo_start_xmit = ice_start_xmit,
4456 .ndo_features_check = ice_features_check,
4457 .ndo_set_rx_mode = ice_set_rx_mode,
4458 .ndo_set_mac_address = ice_set_mac_address,
4459 .ndo_validate_addr = eth_validate_addr,
4460 .ndo_change_mtu = ice_change_mtu,
4461 .ndo_get_stats64 = ice_get_stats64,
4462 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
4463 .ndo_set_vf_mac = ice_set_vf_mac,
4464 .ndo_get_vf_config = ice_get_vf_cfg,
4465 .ndo_set_vf_trust = ice_set_vf_trust,
4466 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
4467 .ndo_set_vf_link_state = ice_set_vf_link_state,
4468 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
4469 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
4470 .ndo_set_features = ice_set_features,
4471 .ndo_bridge_getlink = ice_bridge_getlink,
4472 .ndo_bridge_setlink = ice_bridge_setlink,
4473 .ndo_fdb_add = ice_fdb_add,
4474 .ndo_fdb_del = ice_fdb_del,
4475 .ndo_tx_timeout = ice_tx_timeout,