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;
68 ice_for_each_vsi(pf, v)
69 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
74 if (!vsi || test_bit(__ICE_DOWN, vsi->state))
77 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
80 for (i = 0; i < vsi->num_txq; i++) {
81 struct ice_ring *tx_ring = vsi->tx_rings[i];
83 if (tx_ring && tx_ring->desc) {
84 int itr = ICE_ITR_NONE;
86 /* If packet counter has not changed the queue is
87 * likely stalled, so force an interrupt for this
90 * prev_pkt would be negative if there was no
93 packets = tx_ring->stats.pkts & INT_MAX;
94 if (tx_ring->tx_stats.prev_pkt == packets) {
95 /* Trigger sw interrupt to revive the queue */
96 v_idx = tx_ring->q_vector->v_idx;
98 GLINT_DYN_CTL(vsi->base_vector + v_idx),
99 (itr << GLINT_DYN_CTL_ITR_INDX_S) |
100 GLINT_DYN_CTL_SWINT_TRIG_M |
101 GLINT_DYN_CTL_INTENA_MSK_M);
105 /* Memory barrier between read of packet count and call
106 * to ice_get_tx_pending()
109 tx_ring->tx_stats.prev_pkt =
110 ice_get_tx_pending(tx_ring) ? packets : -1;
116 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
117 * @netdev: the net device on which the sync is happening
118 * @addr: MAC address to sync
120 * This is a callback function which is called by the in kernel device sync
121 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
122 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
123 * MAC filters from the hardware.
125 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
127 struct ice_netdev_priv *np = netdev_priv(netdev);
128 struct ice_vsi *vsi = np->vsi;
130 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
137 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
138 * @netdev: the net device on which the unsync is happening
139 * @addr: MAC address to unsync
141 * This is a callback function which is called by the in kernel device unsync
142 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
143 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
144 * delete the MAC filters from the hardware.
146 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
148 struct ice_netdev_priv *np = netdev_priv(netdev);
149 struct ice_vsi *vsi = np->vsi;
151 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
158 * ice_vsi_fltr_changed - check if filter state changed
159 * @vsi: VSI to be checked
161 * returns true if filter state has changed, false otherwise.
163 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
165 return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
166 test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
167 test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
171 * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
172 * @vsi: the VSI being configured
173 * @promisc_m: mask of promiscuous config bits
174 * @set_promisc: enable or disable promisc flag request
177 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
179 struct ice_hw *hw = &vsi->back->hw;
180 enum ice_status status = 0;
182 if (vsi->type != ICE_VSI_PF)
186 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
190 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
193 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
204 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
205 * @vsi: ptr to the VSI
207 * Push any outstanding VSI filter changes through the AdminQ.
209 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
211 struct device *dev = &vsi->back->pdev->dev;
212 struct net_device *netdev = vsi->netdev;
213 bool promisc_forced_on = false;
214 struct ice_pf *pf = vsi->back;
215 struct ice_hw *hw = &pf->hw;
216 enum ice_status status = 0;
217 u32 changed_flags = 0;
224 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
225 usleep_range(1000, 2000);
227 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
228 vsi->current_netdev_flags = vsi->netdev->flags;
230 INIT_LIST_HEAD(&vsi->tmp_sync_list);
231 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
233 if (ice_vsi_fltr_changed(vsi)) {
234 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
235 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
236 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
238 /* grab the netdev's addr_list_lock */
239 netif_addr_lock_bh(netdev);
240 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
241 ice_add_mac_to_unsync_list);
242 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
243 ice_add_mac_to_unsync_list);
244 /* our temp lists are populated. release lock */
245 netif_addr_unlock_bh(netdev);
248 /* Remove MAC addresses in the unsync list */
249 status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
250 ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
252 netdev_err(netdev, "Failed to delete MAC filters\n");
253 /* if we failed because of alloc failures, just bail */
254 if (status == ICE_ERR_NO_MEMORY) {
260 /* Add MAC addresses in the sync list */
261 status = ice_add_mac(hw, &vsi->tmp_sync_list);
262 ice_free_fltr_list(dev, &vsi->tmp_sync_list);
263 /* If filter is added successfully or already exists, do not go into
264 * 'if' condition and report it as error. Instead continue processing
265 * rest of the function.
267 if (status && status != ICE_ERR_ALREADY_EXISTS) {
268 netdev_err(netdev, "Failed to add MAC filters\n");
269 /* If there is no more space for new umac filters, VSI
270 * should go into promiscuous mode. There should be some
271 * space reserved for promiscuous filters.
273 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
274 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
276 promisc_forced_on = true;
278 "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
285 /* check for changes in promiscuous modes */
286 if (changed_flags & IFF_ALLMULTI) {
287 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
289 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
291 promisc_m = ICE_MCAST_PROMISC_BITS;
293 err = ice_cfg_promisc(vsi, promisc_m, true);
295 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
297 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
300 } else if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
302 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
304 promisc_m = ICE_MCAST_PROMISC_BITS;
306 err = ice_cfg_promisc(vsi, promisc_m, false);
308 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
310 vsi->current_netdev_flags |= IFF_ALLMULTI;
316 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
317 test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
318 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
319 if (vsi->current_netdev_flags & IFF_PROMISC) {
320 /* Apply Rx filter rule to get traffic from wire */
321 status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
324 netdev_err(netdev, "Error setting default VSI %i Rx rule\n",
326 vsi->current_netdev_flags &= ~IFF_PROMISC;
331 /* Clear Rx filter to remove traffic from wire */
332 status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
335 netdev_err(netdev, "Error clearing default VSI %i Rx rule\n",
337 vsi->current_netdev_flags |= IFF_PROMISC;
346 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
349 /* if something went wrong then set the changed flag so we try again */
350 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
351 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
353 clear_bit(__ICE_CFG_BUSY, vsi->state);
358 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
359 * @pf: board private structure
361 static void ice_sync_fltr_subtask(struct ice_pf *pf)
365 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
368 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
370 ice_for_each_vsi(pf, v)
371 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
372 ice_vsi_sync_fltr(pf->vsi[v])) {
373 /* come back and try again later */
374 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
380 * ice_dis_vsi - pause a VSI
381 * @vsi: the VSI being paused
382 * @locked: is the rtnl_lock already held
384 static void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
386 if (test_bit(__ICE_DOWN, vsi->state))
389 set_bit(__ICE_NEEDS_RESTART, vsi->state);
391 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
392 if (netif_running(vsi->netdev)) {
395 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
398 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
407 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
409 * @locked: is the rtnl_lock already held
412 void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
414 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
415 #endif /* CONFIG_DCB */
419 ice_for_each_vsi(pf, v)
421 ice_dis_vsi(pf->vsi[v], locked);
425 * ice_prepare_for_reset - prep for the core to reset
426 * @pf: board private structure
428 * Inform or close all dependent features in prep for reset.
431 ice_prepare_for_reset(struct ice_pf *pf)
433 struct ice_hw *hw = &pf->hw;
435 /* already prepared for reset */
436 if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
439 /* Notify VFs of impending reset */
440 if (ice_check_sq_alive(hw, &hw->mailboxq))
441 ice_vc_notify_reset(pf);
443 /* disable the VSIs and their queues that are not already DOWN */
444 ice_pf_dis_all_vsi(pf, false);
447 ice_sched_clear_port(hw->port_info);
449 ice_shutdown_all_ctrlq(hw);
451 set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
455 * ice_do_reset - Initiate one of many types of resets
456 * @pf: board private structure
457 * @reset_type: reset type requested
458 * before this function was called.
460 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
462 struct device *dev = &pf->pdev->dev;
463 struct ice_hw *hw = &pf->hw;
465 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
466 WARN_ON(in_interrupt());
468 ice_prepare_for_reset(pf);
470 /* trigger the reset */
471 if (ice_reset(hw, reset_type)) {
472 dev_err(dev, "reset %d failed\n", reset_type);
473 set_bit(__ICE_RESET_FAILED, pf->state);
474 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
475 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
476 clear_bit(__ICE_PFR_REQ, pf->state);
477 clear_bit(__ICE_CORER_REQ, pf->state);
478 clear_bit(__ICE_GLOBR_REQ, pf->state);
482 /* PFR is a bit of a special case because it doesn't result in an OICR
483 * interrupt. So for PFR, rebuild after the reset and clear the reset-
484 * associated state bits.
486 if (reset_type == ICE_RESET_PFR) {
489 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
490 clear_bit(__ICE_PFR_REQ, pf->state);
491 ice_reset_all_vfs(pf, true);
496 * ice_reset_subtask - Set up for resetting the device and driver
497 * @pf: board private structure
499 static void ice_reset_subtask(struct ice_pf *pf)
501 enum ice_reset_req reset_type = ICE_RESET_INVAL;
503 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
504 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
505 * of reset is pending and sets bits in pf->state indicating the reset
506 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
507 * prepare for pending reset if not already (for PF software-initiated
508 * global resets the software should already be prepared for it as
509 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
510 * by firmware or software on other PFs, that bit is not set so prepare
511 * for the reset now), poll for reset done, rebuild and return.
513 if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
514 /* Perform the largest reset requested */
515 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
516 reset_type = ICE_RESET_CORER;
517 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
518 reset_type = ICE_RESET_GLOBR;
519 /* return if no valid reset type requested */
520 if (reset_type == ICE_RESET_INVAL)
522 ice_prepare_for_reset(pf);
524 /* make sure we are ready to rebuild */
525 if (ice_check_reset(&pf->hw)) {
526 set_bit(__ICE_RESET_FAILED, pf->state);
528 /* done with reset. start rebuild */
529 pf->hw.reset_ongoing = false;
531 /* clear bit to resume normal operations, but
532 * ICE_NEEDS_RESTART bit is set in case rebuild failed
534 clear_bit(__ICE_RESET_OICR_RECV, pf->state);
535 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
536 clear_bit(__ICE_PFR_REQ, pf->state);
537 clear_bit(__ICE_CORER_REQ, pf->state);
538 clear_bit(__ICE_GLOBR_REQ, pf->state);
539 ice_reset_all_vfs(pf, true);
545 /* No pending resets to finish processing. Check for new resets */
546 if (test_bit(__ICE_PFR_REQ, pf->state))
547 reset_type = ICE_RESET_PFR;
548 if (test_bit(__ICE_CORER_REQ, pf->state))
549 reset_type = ICE_RESET_CORER;
550 if (test_bit(__ICE_GLOBR_REQ, pf->state))
551 reset_type = ICE_RESET_GLOBR;
552 /* If no valid reset type requested just return */
553 if (reset_type == ICE_RESET_INVAL)
556 /* reset if not already down or busy */
557 if (!test_bit(__ICE_DOWN, pf->state) &&
558 !test_bit(__ICE_CFG_BUSY, pf->state)) {
559 ice_do_reset(pf, reset_type);
564 * ice_print_link_msg - print link up or down message
565 * @vsi: the VSI whose link status is being queried
566 * @isup: boolean for if the link is now up or down
568 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
576 if (vsi->current_isup == isup)
579 vsi->current_isup = isup;
582 netdev_info(vsi->netdev, "NIC Link is Down\n");
586 switch (vsi->port_info->phy.link_info.link_speed) {
587 case ICE_AQ_LINK_SPEED_40GB:
590 case ICE_AQ_LINK_SPEED_25GB:
593 case ICE_AQ_LINK_SPEED_20GB:
596 case ICE_AQ_LINK_SPEED_10GB:
599 case ICE_AQ_LINK_SPEED_5GB:
602 case ICE_AQ_LINK_SPEED_2500MB:
605 case ICE_AQ_LINK_SPEED_1000MB:
608 case ICE_AQ_LINK_SPEED_100MB:
616 switch (vsi->port_info->fc.current_mode) {
620 case ICE_FC_TX_PAUSE:
623 case ICE_FC_RX_PAUSE:
634 netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
639 * ice_vsi_link_event - update the VSI's netdev
640 * @vsi: the VSI on which the link event occurred
641 * @link_up: whether or not the VSI needs to be set up or down
643 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
648 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
651 if (vsi->type == ICE_VSI_PF) {
652 if (link_up == netif_carrier_ok(vsi->netdev))
656 netif_carrier_on(vsi->netdev);
657 netif_tx_wake_all_queues(vsi->netdev);
659 netif_carrier_off(vsi->netdev);
660 netif_tx_stop_all_queues(vsi->netdev);
666 * ice_link_event - process the link event
667 * @pf: pf that the link event is associated with
668 * @pi: port_info for the port that the link event is associated with
669 * @link_up: true if the physical link is up and false if it is down
670 * @link_speed: current link speed received from the link event
672 * Returns 0 on success and negative on failure
675 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
678 struct ice_phy_info *phy_info;
685 phy_info->link_info_old = phy_info->link_info;
687 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
688 old_link_speed = phy_info->link_info_old.link_speed;
690 /* update the link info structures and re-enable link events,
691 * don't bail on failure due to other book keeping needed
693 result = ice_update_link_info(pi);
695 dev_dbg(&pf->pdev->dev,
696 "Failed to update link status and re-enable link events for port %d\n",
699 /* if the old link up/down and speed is the same as the new */
700 if (link_up == old_link && link_speed == old_link_speed)
703 vsi = ice_find_vsi_by_type(pf, ICE_VSI_PF);
704 if (!vsi || !vsi->port_info)
707 ice_vsi_link_event(vsi, link_up);
708 ice_print_link_msg(vsi, link_up);
710 if (pf->num_alloc_vfs)
711 ice_vc_notify_link_state(pf);
717 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
718 * @pf: board private structure
720 static void ice_watchdog_subtask(struct ice_pf *pf)
724 /* if interface is down do nothing */
725 if (test_bit(__ICE_DOWN, pf->state) ||
726 test_bit(__ICE_CFG_BUSY, pf->state))
729 /* make sure we don't do these things too often */
730 if (time_before(jiffies,
731 pf->serv_tmr_prev + pf->serv_tmr_period))
734 pf->serv_tmr_prev = jiffies;
736 /* Update the stats for active netdevs so the network stack
737 * can look at updated numbers whenever it cares to
739 ice_update_pf_stats(pf);
740 ice_for_each_vsi(pf, i)
741 if (pf->vsi[i] && pf->vsi[i]->netdev)
742 ice_update_vsi_stats(pf->vsi[i]);
746 * ice_init_link_events - enable/initialize link events
747 * @pi: pointer to the port_info instance
749 * Returns -EIO on failure, 0 on success
751 static int ice_init_link_events(struct ice_port_info *pi)
755 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
756 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
758 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
759 dev_dbg(ice_hw_to_dev(pi->hw),
760 "Failed to set link event mask for port %d\n",
765 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
766 dev_dbg(ice_hw_to_dev(pi->hw),
767 "Failed to enable link events for port %d\n",
776 * ice_handle_link_event - handle link event via ARQ
777 * @pf: pf that the link event is associated with
778 * @event: event structure containing link status info
781 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
783 struct ice_aqc_get_link_status_data *link_data;
784 struct ice_port_info *port_info;
787 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
788 port_info = pf->hw.port_info;
792 status = ice_link_event(pf, port_info,
793 !!(link_data->link_info & ICE_AQ_LINK_UP),
794 le16_to_cpu(link_data->link_speed));
796 dev_dbg(&pf->pdev->dev,
797 "Could not process link event, error %d\n", status);
803 * __ice_clean_ctrlq - helper function to clean controlq rings
804 * @pf: ptr to struct ice_pf
805 * @q_type: specific Control queue type
807 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
809 struct ice_rq_event_info event;
810 struct ice_hw *hw = &pf->hw;
811 struct ice_ctl_q_info *cq;
816 /* Do not clean control queue if/when PF reset fails */
817 if (test_bit(__ICE_RESET_FAILED, pf->state))
821 case ICE_CTL_Q_ADMIN:
825 case ICE_CTL_Q_MAILBOX:
830 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
835 /* check for error indications - PF_xx_AxQLEN register layout for
836 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
838 val = rd32(hw, cq->rq.len);
839 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
840 PF_FW_ARQLEN_ARQCRIT_M)) {
842 if (val & PF_FW_ARQLEN_ARQVFE_M)
843 dev_dbg(&pf->pdev->dev,
844 "%s Receive Queue VF Error detected\n", qtype);
845 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
846 dev_dbg(&pf->pdev->dev,
847 "%s Receive Queue Overflow Error detected\n",
850 if (val & PF_FW_ARQLEN_ARQCRIT_M)
851 dev_dbg(&pf->pdev->dev,
852 "%s Receive Queue Critical Error detected\n",
854 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
855 PF_FW_ARQLEN_ARQCRIT_M);
857 wr32(hw, cq->rq.len, val);
860 val = rd32(hw, cq->sq.len);
861 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
862 PF_FW_ATQLEN_ATQCRIT_M)) {
864 if (val & PF_FW_ATQLEN_ATQVFE_M)
865 dev_dbg(&pf->pdev->dev,
866 "%s Send Queue VF Error detected\n", qtype);
867 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
868 dev_dbg(&pf->pdev->dev,
869 "%s Send Queue Overflow Error detected\n",
872 if (val & PF_FW_ATQLEN_ATQCRIT_M)
873 dev_dbg(&pf->pdev->dev,
874 "%s Send Queue Critical Error detected\n",
876 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
877 PF_FW_ATQLEN_ATQCRIT_M);
879 wr32(hw, cq->sq.len, val);
882 event.buf_len = cq->rq_buf_size;
883 event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
892 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
893 if (ret == ICE_ERR_AQ_NO_WORK)
896 dev_err(&pf->pdev->dev,
897 "%s Receive Queue event error %d\n", qtype,
902 opcode = le16_to_cpu(event.desc.opcode);
905 case ice_aqc_opc_get_link_status:
906 if (ice_handle_link_event(pf, &event))
907 dev_err(&pf->pdev->dev,
908 "Could not handle link event\n");
910 case ice_mbx_opc_send_msg_to_pf:
911 ice_vc_process_vf_msg(pf, &event);
913 case ice_aqc_opc_fw_logging:
914 ice_output_fw_log(hw, &event.desc, event.msg_buf);
916 case ice_aqc_opc_lldp_set_mib_change:
917 ice_dcb_process_lldp_set_mib_change(pf, &event);
920 dev_dbg(&pf->pdev->dev,
921 "%s Receive Queue unknown event 0x%04x ignored\n",
925 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
927 devm_kfree(&pf->pdev->dev, event.msg_buf);
929 return pending && (i == ICE_DFLT_IRQ_WORK);
933 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
934 * @hw: pointer to hardware info
935 * @cq: control queue information
937 * returns true if there are pending messages in a queue, false if there aren't
939 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
943 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
944 return cq->rq.next_to_clean != ntu;
948 * ice_clean_adminq_subtask - clean the AdminQ rings
949 * @pf: board private structure
951 static void ice_clean_adminq_subtask(struct ice_pf *pf)
953 struct ice_hw *hw = &pf->hw;
955 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
958 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
961 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
963 /* There might be a situation where new messages arrive to a control
964 * queue between processing the last message and clearing the
965 * EVENT_PENDING bit. So before exiting, check queue head again (using
966 * ice_ctrlq_pending) and process new messages if any.
968 if (ice_ctrlq_pending(hw, &hw->adminq))
969 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
975 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
976 * @pf: board private structure
978 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
980 struct ice_hw *hw = &pf->hw;
982 if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
985 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
988 clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
990 if (ice_ctrlq_pending(hw, &hw->mailboxq))
991 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
997 * ice_service_task_schedule - schedule the service task to wake up
998 * @pf: board private structure
1000 * If not already scheduled, this puts the task into the work queue.
1002 static void ice_service_task_schedule(struct ice_pf *pf)
1004 if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1005 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1006 !test_bit(__ICE_NEEDS_RESTART, pf->state))
1007 queue_work(ice_wq, &pf->serv_task);
1011 * ice_service_task_complete - finish up the service task
1012 * @pf: board private structure
1014 static void ice_service_task_complete(struct ice_pf *pf)
1016 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1018 /* force memory (pf->state) to sync before next service task */
1019 smp_mb__before_atomic();
1020 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1024 * ice_service_task_stop - stop service task and cancel works
1025 * @pf: board private structure
1027 static void ice_service_task_stop(struct ice_pf *pf)
1029 set_bit(__ICE_SERVICE_DIS, pf->state);
1031 if (pf->serv_tmr.function)
1032 del_timer_sync(&pf->serv_tmr);
1033 if (pf->serv_task.func)
1034 cancel_work_sync(&pf->serv_task);
1036 clear_bit(__ICE_SERVICE_SCHED, pf->state);
1040 * ice_service_task_restart - restart service task and schedule works
1041 * @pf: board private structure
1043 * This function is needed for suspend and resume works (e.g WoL scenario)
1045 static void ice_service_task_restart(struct ice_pf *pf)
1047 clear_bit(__ICE_SERVICE_DIS, pf->state);
1048 ice_service_task_schedule(pf);
1052 * ice_service_timer - timer callback to schedule service task
1053 * @t: pointer to timer_list
1055 static void ice_service_timer(struct timer_list *t)
1057 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1059 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1060 ice_service_task_schedule(pf);
1064 * ice_handle_mdd_event - handle malicious driver detect event
1065 * @pf: pointer to the PF structure
1067 * Called from service task. OICR interrupt handler indicates MDD event
1069 static void ice_handle_mdd_event(struct ice_pf *pf)
1071 struct ice_hw *hw = &pf->hw;
1072 bool mdd_detected = false;
1076 if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state))
1079 /* find what triggered the MDD event */
1080 reg = rd32(hw, GL_MDET_TX_PQM);
1081 if (reg & GL_MDET_TX_PQM_VALID_M) {
1082 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1083 GL_MDET_TX_PQM_PF_NUM_S;
1084 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1085 GL_MDET_TX_PQM_VF_NUM_S;
1086 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1087 GL_MDET_TX_PQM_MAL_TYPE_S;
1088 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1089 GL_MDET_TX_PQM_QNUM_S);
1091 if (netif_msg_tx_err(pf))
1092 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1093 event, queue, pf_num, vf_num);
1094 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1095 mdd_detected = true;
1098 reg = rd32(hw, GL_MDET_TX_TCLAN);
1099 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1100 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1101 GL_MDET_TX_TCLAN_PF_NUM_S;
1102 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1103 GL_MDET_TX_TCLAN_VF_NUM_S;
1104 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1105 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1106 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1107 GL_MDET_TX_TCLAN_QNUM_S);
1109 if (netif_msg_rx_err(pf))
1110 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1111 event, queue, pf_num, vf_num);
1112 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1113 mdd_detected = true;
1116 reg = rd32(hw, GL_MDET_RX);
1117 if (reg & GL_MDET_RX_VALID_M) {
1118 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1119 GL_MDET_RX_PF_NUM_S;
1120 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1121 GL_MDET_RX_VF_NUM_S;
1122 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1123 GL_MDET_RX_MAL_TYPE_S;
1124 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1127 if (netif_msg_rx_err(pf))
1128 dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1129 event, queue, pf_num, vf_num);
1130 wr32(hw, GL_MDET_RX, 0xffffffff);
1131 mdd_detected = true;
1135 bool pf_mdd_detected = false;
1137 reg = rd32(hw, PF_MDET_TX_PQM);
1138 if (reg & PF_MDET_TX_PQM_VALID_M) {
1139 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1140 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1141 pf_mdd_detected = true;
1144 reg = rd32(hw, PF_MDET_TX_TCLAN);
1145 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1146 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1147 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
1148 pf_mdd_detected = true;
1151 reg = rd32(hw, PF_MDET_RX);
1152 if (reg & PF_MDET_RX_VALID_M) {
1153 wr32(hw, PF_MDET_RX, 0xFFFF);
1154 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
1155 pf_mdd_detected = true;
1157 /* Queue belongs to the PF initiate a reset */
1158 if (pf_mdd_detected) {
1159 set_bit(__ICE_NEEDS_RESTART, pf->state);
1160 ice_service_task_schedule(pf);
1164 /* see if one of the VFs needs to be reset */
1165 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
1166 struct ice_vf *vf = &pf->vf[i];
1168 mdd_detected = false;
1170 reg = rd32(hw, VP_MDET_TX_PQM(i));
1171 if (reg & VP_MDET_TX_PQM_VALID_M) {
1172 wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1173 mdd_detected = true;
1174 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1178 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1179 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1180 wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1181 mdd_detected = true;
1182 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1186 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1187 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1188 wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1189 mdd_detected = true;
1190 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
1194 reg = rd32(hw, VP_MDET_RX(i));
1195 if (reg & VP_MDET_RX_VALID_M) {
1196 wr32(hw, VP_MDET_RX(i), 0xFFFF);
1197 mdd_detected = true;
1198 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
1203 vf->num_mdd_events++;
1204 dev_info(&pf->pdev->dev,
1205 "Use PF Control I/F to re-enable the VF\n");
1206 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1213 * ice_service_task - manage and run subtasks
1214 * @work: pointer to work_struct contained by the PF struct
1216 static void ice_service_task(struct work_struct *work)
1218 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
1219 unsigned long start_time = jiffies;
1223 /* process reset requests first */
1224 ice_reset_subtask(pf);
1226 /* bail if a reset/recovery cycle is pending or rebuild failed */
1227 if (ice_is_reset_in_progress(pf->state) ||
1228 test_bit(__ICE_SUSPENDED, pf->state) ||
1229 test_bit(__ICE_NEEDS_RESTART, pf->state)) {
1230 ice_service_task_complete(pf);
1234 ice_check_for_hang_subtask(pf);
1235 ice_sync_fltr_subtask(pf);
1236 ice_handle_mdd_event(pf);
1237 ice_process_vflr_event(pf);
1238 ice_watchdog_subtask(pf);
1239 ice_clean_adminq_subtask(pf);
1240 ice_clean_mailboxq_subtask(pf);
1242 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1243 ice_service_task_complete(pf);
1245 /* If the tasks have taken longer than one service timer period
1246 * or there is more work to be done, reset the service timer to
1247 * schedule the service task now.
1249 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1250 test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
1251 test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1252 test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
1253 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1254 mod_timer(&pf->serv_tmr, jiffies);
1258 * ice_set_ctrlq_len - helper function to set controlq length
1259 * @hw: pointer to the HW instance
1261 static void ice_set_ctrlq_len(struct ice_hw *hw)
1263 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1264 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1265 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1266 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1267 hw->mailboxq.num_rq_entries = ICE_MBXQ_LEN;
1268 hw->mailboxq.num_sq_entries = ICE_MBXQ_LEN;
1269 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1270 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
1274 * ice_irq_affinity_notify - Callback for affinity changes
1275 * @notify: context as to what irq was changed
1276 * @mask: the new affinity mask
1278 * This is a callback function used by the irq_set_affinity_notifier function
1279 * so that we may register to receive changes to the irq affinity masks.
1282 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1283 const cpumask_t *mask)
1285 struct ice_q_vector *q_vector =
1286 container_of(notify, struct ice_q_vector, affinity_notify);
1288 cpumask_copy(&q_vector->affinity_mask, mask);
1292 * ice_irq_affinity_release - Callback for affinity notifier release
1293 * @ref: internal core kernel usage
1295 * This is a callback function used by the irq_set_affinity_notifier function
1296 * to inform the current notification subscriber that they will no longer
1297 * receive notifications.
1299 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1302 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1303 * @vsi: the VSI being configured
1305 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1307 struct ice_pf *pf = vsi->back;
1308 struct ice_hw *hw = &pf->hw;
1310 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1313 ice_for_each_q_vector(vsi, i)
1314 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1322 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1323 * @vsi: the VSI being configured
1324 * @basename: name for the vector
1326 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1328 int q_vectors = vsi->num_q_vectors;
1329 struct ice_pf *pf = vsi->back;
1330 int base = vsi->base_vector;
1336 for (vector = 0; vector < q_vectors; vector++) {
1337 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1339 irq_num = pf->msix_entries[base + vector].vector;
1341 if (q_vector->tx.ring && q_vector->rx.ring) {
1342 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1343 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1345 } else if (q_vector->rx.ring) {
1346 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1347 "%s-%s-%d", basename, "rx", rx_int_idx++);
1348 } else if (q_vector->tx.ring) {
1349 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1350 "%s-%s-%d", basename, "tx", tx_int_idx++);
1352 /* skip this unused q_vector */
1355 err = devm_request_irq(&pf->pdev->dev, irq_num,
1356 vsi->irq_handler, 0,
1357 q_vector->name, q_vector);
1359 netdev_err(vsi->netdev,
1360 "MSIX request_irq failed, error: %d\n", err);
1364 /* register for affinity change notifications */
1365 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1366 q_vector->affinity_notify.release = ice_irq_affinity_release;
1367 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1369 /* assign the mask for this irq */
1370 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1373 vsi->irqs_ready = true;
1379 irq_num = pf->msix_entries[base + vector].vector,
1380 irq_set_affinity_notifier(irq_num, NULL);
1381 irq_set_affinity_hint(irq_num, NULL);
1382 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1388 * ice_ena_misc_vector - enable the non-queue interrupts
1389 * @pf: board private structure
1391 static void ice_ena_misc_vector(struct ice_pf *pf)
1393 struct ice_hw *hw = &pf->hw;
1396 /* clear things first */
1397 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1398 rd32(hw, PFINT_OICR); /* read to clear */
1400 val = (PFINT_OICR_ECC_ERR_M |
1401 PFINT_OICR_MAL_DETECT_M |
1403 PFINT_OICR_PCI_EXCEPTION_M |
1405 PFINT_OICR_HMC_ERR_M |
1406 PFINT_OICR_PE_CRITERR_M);
1408 wr32(hw, PFINT_OICR_ENA, val);
1410 /* SW_ITR_IDX = 0, but don't change INTENA */
1411 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1412 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1416 * ice_misc_intr - misc interrupt handler
1417 * @irq: interrupt number
1418 * @data: pointer to a q_vector
1420 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1422 struct ice_pf *pf = (struct ice_pf *)data;
1423 struct ice_hw *hw = &pf->hw;
1424 irqreturn_t ret = IRQ_NONE;
1427 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1428 set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1430 oicr = rd32(hw, PFINT_OICR);
1431 ena_mask = rd32(hw, PFINT_OICR_ENA);
1433 if (oicr & PFINT_OICR_SWINT_M) {
1434 ena_mask &= ~PFINT_OICR_SWINT_M;
1438 if (oicr & PFINT_OICR_MAL_DETECT_M) {
1439 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
1440 set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
1442 if (oicr & PFINT_OICR_VFLR_M) {
1443 ena_mask &= ~PFINT_OICR_VFLR_M;
1444 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
1447 if (oicr & PFINT_OICR_GRST_M) {
1450 /* we have a reset warning */
1451 ena_mask &= ~PFINT_OICR_GRST_M;
1452 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1453 GLGEN_RSTAT_RESET_TYPE_S;
1455 if (reset == ICE_RESET_CORER)
1457 else if (reset == ICE_RESET_GLOBR)
1459 else if (reset == ICE_RESET_EMPR)
1462 dev_dbg(&pf->pdev->dev, "Invalid reset type %d\n",
1465 /* If a reset cycle isn't already in progress, we set a bit in
1466 * pf->state so that the service task can start a reset/rebuild.
1467 * We also make note of which reset happened so that peer
1468 * devices/drivers can be informed.
1470 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
1471 if (reset == ICE_RESET_CORER)
1472 set_bit(__ICE_CORER_RECV, pf->state);
1473 else if (reset == ICE_RESET_GLOBR)
1474 set_bit(__ICE_GLOBR_RECV, pf->state);
1476 set_bit(__ICE_EMPR_RECV, pf->state);
1478 /* There are couple of different bits at play here.
1479 * hw->reset_ongoing indicates whether the hardware is
1480 * in reset. This is set to true when a reset interrupt
1481 * is received and set back to false after the driver
1482 * has determined that the hardware is out of reset.
1484 * __ICE_RESET_OICR_RECV in pf->state indicates
1485 * that a post reset rebuild is required before the
1486 * driver is operational again. This is set above.
1488 * As this is the start of the reset/rebuild cycle, set
1489 * both to indicate that.
1491 hw->reset_ongoing = true;
1495 if (oicr & PFINT_OICR_HMC_ERR_M) {
1496 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1497 dev_dbg(&pf->pdev->dev,
1498 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1499 rd32(hw, PFHMC_ERRORINFO),
1500 rd32(hw, PFHMC_ERRORDATA));
1503 /* Report any remaining unexpected interrupts */
1506 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1508 /* If a critical error is pending there is no choice but to
1511 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1512 PFINT_OICR_PCI_EXCEPTION_M |
1513 PFINT_OICR_ECC_ERR_M)) {
1514 set_bit(__ICE_PFR_REQ, pf->state);
1515 ice_service_task_schedule(pf);
1520 if (!test_bit(__ICE_DOWN, pf->state)) {
1521 ice_service_task_schedule(pf);
1522 ice_irq_dynamic_ena(hw, NULL, NULL);
1529 * ice_dis_ctrlq_interrupts - disable control queue interrupts
1530 * @hw: pointer to HW structure
1532 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
1534 /* disable Admin queue Interrupt causes */
1535 wr32(hw, PFINT_FW_CTL,
1536 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
1538 /* disable Mailbox queue Interrupt causes */
1539 wr32(hw, PFINT_MBX_CTL,
1540 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
1542 /* disable Control queue Interrupt causes */
1543 wr32(hw, PFINT_OICR_CTL,
1544 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
1550 * ice_free_irq_msix_misc - Unroll misc vector setup
1551 * @pf: board private structure
1553 static void ice_free_irq_msix_misc(struct ice_pf *pf)
1555 struct ice_hw *hw = &pf->hw;
1557 ice_dis_ctrlq_interrupts(hw);
1559 /* disable OICR interrupt */
1560 wr32(hw, PFINT_OICR_ENA, 0);
1563 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
1564 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
1565 devm_free_irq(&pf->pdev->dev,
1566 pf->msix_entries[pf->oicr_idx].vector, pf);
1569 pf->num_avail_sw_msix += 1;
1570 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
1574 * ice_ena_ctrlq_interrupts - enable control queue interrupts
1575 * @hw: pointer to HW structure
1576 * @reg_idx: HW vector index to associate the control queue interrupts with
1578 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
1582 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
1583 PFINT_OICR_CTL_CAUSE_ENA_M);
1584 wr32(hw, PFINT_OICR_CTL, val);
1586 /* enable Admin queue Interrupt causes */
1587 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
1588 PFINT_FW_CTL_CAUSE_ENA_M);
1589 wr32(hw, PFINT_FW_CTL, val);
1591 /* enable Mailbox queue Interrupt causes */
1592 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
1593 PFINT_MBX_CTL_CAUSE_ENA_M);
1594 wr32(hw, PFINT_MBX_CTL, val);
1600 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
1601 * @pf: board private structure
1603 * This sets up the handler for MSIX 0, which is used to manage the
1604 * non-queue interrupts, e.g. AdminQ and errors. This is not used
1605 * when in MSI or Legacy interrupt mode.
1607 static int ice_req_irq_msix_misc(struct ice_pf *pf)
1609 struct ice_hw *hw = &pf->hw;
1610 int oicr_idx, err = 0;
1612 if (!pf->int_name[0])
1613 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
1614 dev_driver_string(&pf->pdev->dev),
1615 dev_name(&pf->pdev->dev));
1617 /* Do not request IRQ but do enable OICR interrupt since settings are
1618 * lost during reset. Note that this function is called only during
1619 * rebuild path and not while reset is in progress.
1621 if (ice_is_reset_in_progress(pf->state))
1624 /* reserve one vector in irq_tracker for misc interrupts */
1625 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1629 pf->num_avail_sw_msix -= 1;
1630 pf->oicr_idx = oicr_idx;
1632 err = devm_request_irq(&pf->pdev->dev,
1633 pf->msix_entries[pf->oicr_idx].vector,
1634 ice_misc_intr, 0, pf->int_name, pf);
1636 dev_err(&pf->pdev->dev,
1637 "devm_request_irq for %s failed: %d\n",
1639 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
1640 pf->num_avail_sw_msix += 1;
1645 ice_ena_misc_vector(pf);
1647 ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
1648 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
1649 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
1652 ice_irq_dynamic_ena(hw, NULL, NULL);
1658 * ice_napi_del - Remove NAPI handler for the VSI
1659 * @vsi: VSI for which NAPI handler is to be removed
1661 static void ice_napi_del(struct ice_vsi *vsi)
1668 ice_for_each_q_vector(vsi, v_idx)
1669 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
1673 * ice_napi_add - register NAPI handler for the VSI
1674 * @vsi: VSI for which NAPI handler is to be registered
1676 * This function is only called in the driver's load path. Registering the NAPI
1677 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
1678 * reset/rebuild, etc.)
1680 static void ice_napi_add(struct ice_vsi *vsi)
1687 ice_for_each_q_vector(vsi, v_idx)
1688 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
1689 ice_napi_poll, NAPI_POLL_WEIGHT);
1693 * ice_cfg_netdev - Allocate, configure and register a netdev
1694 * @vsi: the VSI associated with the new netdev
1696 * Returns 0 on success, negative value on failure
1698 static int ice_cfg_netdev(struct ice_vsi *vsi)
1700 netdev_features_t csumo_features;
1701 netdev_features_t vlano_features;
1702 netdev_features_t dflt_features;
1703 netdev_features_t tso_features;
1704 struct ice_netdev_priv *np;
1705 struct net_device *netdev;
1706 u8 mac_addr[ETH_ALEN];
1709 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
1714 vsi->netdev = netdev;
1715 np = netdev_priv(netdev);
1718 dflt_features = NETIF_F_SG |
1722 csumo_features = NETIF_F_RXCSUM |
1727 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
1728 NETIF_F_HW_VLAN_CTAG_TX |
1729 NETIF_F_HW_VLAN_CTAG_RX;
1731 tso_features = NETIF_F_TSO;
1733 /* set features that user can change */
1734 netdev->hw_features = dflt_features | csumo_features |
1735 vlano_features | tso_features;
1737 /* enable features */
1738 netdev->features |= netdev->hw_features;
1739 /* encap and VLAN devices inherit default, csumo and tso features */
1740 netdev->hw_enc_features |= dflt_features | csumo_features |
1742 netdev->vlan_features |= dflt_features | csumo_features |
1745 if (vsi->type == ICE_VSI_PF) {
1746 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
1747 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
1749 ether_addr_copy(netdev->dev_addr, mac_addr);
1750 ether_addr_copy(netdev->perm_addr, mac_addr);
1753 netdev->priv_flags |= IFF_UNICAST_FLT;
1755 /* assign netdev_ops */
1756 netdev->netdev_ops = &ice_netdev_ops;
1758 /* setup watchdog timeout value to be 5 second */
1759 netdev->watchdog_timeo = 5 * HZ;
1761 ice_set_ethtool_ops(netdev);
1763 netdev->min_mtu = ETH_MIN_MTU;
1764 netdev->max_mtu = ICE_MAX_MTU;
1766 err = register_netdev(vsi->netdev);
1770 netif_carrier_off(vsi->netdev);
1772 /* make sure transmit queues start off as stopped */
1773 netif_tx_stop_all_queues(vsi->netdev);
1779 * ice_fill_rss_lut - Fill the RSS lookup table with default values
1780 * @lut: Lookup table
1781 * @rss_table_size: Lookup table size
1782 * @rss_size: Range of queue number for hashing
1784 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
1788 for (i = 0; i < rss_table_size; i++)
1789 lut[i] = i % rss_size;
1793 * ice_pf_vsi_setup - Set up a PF VSI
1794 * @pf: board private structure
1795 * @pi: pointer to the port_info instance
1797 * Returns pointer to the successfully allocated VSI software struct
1798 * on success, otherwise returns NULL on failure.
1800 static struct ice_vsi *
1801 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
1803 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
1807 * ice_lb_vsi_setup - Set up a loopback VSI
1808 * @pf: board private structure
1809 * @pi: pointer to the port_info instance
1811 * Returns pointer to the successfully allocated VSI software struct
1812 * on success, otherwise returns NULL on failure.
1815 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
1817 return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
1821 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
1822 * @netdev: network interface to be adjusted
1823 * @proto: unused protocol
1824 * @vid: VLAN ID to be added
1826 * net_device_ops implementation for adding VLAN IDs
1829 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
1832 struct ice_netdev_priv *np = netdev_priv(netdev);
1833 struct ice_vsi *vsi = np->vsi;
1836 if (vid >= VLAN_N_VID) {
1837 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
1845 /* Enable VLAN pruning when VLAN 0 is added */
1846 if (unlikely(!vid)) {
1847 ret = ice_cfg_vlan_pruning(vsi, true, false);
1852 /* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is
1853 * needed to continue allowing all untagged packets since VLAN prune
1854 * list is applied to all packets by the switch
1856 ret = ice_vsi_add_vlan(vsi, vid);
1858 vsi->vlan_ena = true;
1859 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
1866 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
1867 * @netdev: network interface to be adjusted
1868 * @proto: unused protocol
1869 * @vid: VLAN ID to be removed
1871 * net_device_ops implementation for removing VLAN IDs
1874 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
1877 struct ice_netdev_priv *np = netdev_priv(netdev);
1878 struct ice_vsi *vsi = np->vsi;
1884 /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
1887 ret = ice_vsi_kill_vlan(vsi, vid);
1891 /* Disable VLAN pruning when VLAN 0 is removed */
1893 ret = ice_cfg_vlan_pruning(vsi, false, false);
1895 vsi->vlan_ena = false;
1896 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
1901 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
1902 * @pf: board private structure
1904 * Returns 0 on success, negative value on failure
1906 static int ice_setup_pf_sw(struct ice_pf *pf)
1908 LIST_HEAD(tmp_add_list);
1909 u8 broadcast[ETH_ALEN];
1910 struct ice_vsi *vsi;
1913 if (ice_is_reset_in_progress(pf->state))
1916 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
1919 goto unroll_vsi_setup;
1922 status = ice_cfg_netdev(vsi);
1925 goto unroll_vsi_setup;
1928 /* registering the NAPI handler requires both the queues and
1929 * netdev to be created, which are done in ice_pf_vsi_setup()
1930 * and ice_cfg_netdev() respectively
1934 /* To add a MAC filter, first add the MAC to a list and then
1935 * pass the list to ice_add_mac.
1938 /* Add a unicast MAC filter so the VSI can get its packets */
1939 status = ice_add_mac_to_list(vsi, &tmp_add_list,
1940 vsi->port_info->mac.perm_addr);
1942 goto unroll_napi_add;
1944 /* VSI needs to receive broadcast traffic, so add the broadcast
1945 * MAC address to the list as well.
1947 eth_broadcast_addr(broadcast);
1948 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
1952 /* program MAC filters for entries in tmp_add_list */
1953 status = ice_add_mac(&pf->hw, &tmp_add_list);
1955 dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
1960 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1964 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
1970 if (vsi->netdev->reg_state == NETREG_REGISTERED)
1971 unregister_netdev(vsi->netdev);
1972 free_netdev(vsi->netdev);
1979 ice_vsi_free_q_vectors(vsi);
1980 ice_vsi_delete(vsi);
1981 ice_vsi_put_qs(vsi);
1982 pf->q_left_tx += vsi->alloc_txq;
1983 pf->q_left_rx += vsi->alloc_rxq;
1990 * ice_determine_q_usage - Calculate queue distribution
1991 * @pf: board private structure
1993 * Return -ENOMEM if we don't get enough queues for all ports
1995 static void ice_determine_q_usage(struct ice_pf *pf)
1997 u16 q_left_tx, q_left_rx;
1999 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
2000 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
2002 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
2004 /* only 1 Rx queue unless RSS is enabled */
2005 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2008 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
2010 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
2011 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
2015 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
2016 * @pf: board private structure to initialize
2018 static void ice_deinit_pf(struct ice_pf *pf)
2020 ice_service_task_stop(pf);
2021 mutex_destroy(&pf->sw_mutex);
2022 mutex_destroy(&pf->avail_q_mutex);
2026 * ice_init_pf - Initialize general software structures (struct ice_pf)
2027 * @pf: board private structure to initialize
2029 static void ice_init_pf(struct ice_pf *pf)
2031 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
2032 set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2033 #ifdef CONFIG_PCI_IOV
2034 if (pf->hw.func_caps.common_cap.sr_iov_1_1) {
2035 struct ice_hw *hw = &pf->hw;
2037 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
2038 pf->num_vfs_supported = min_t(int, hw->func_caps.num_allocd_vfs,
2041 #endif /* CONFIG_PCI_IOV */
2043 mutex_init(&pf->sw_mutex);
2044 mutex_init(&pf->avail_q_mutex);
2046 /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
2047 mutex_lock(&pf->avail_q_mutex);
2048 bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
2049 bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
2050 mutex_unlock(&pf->avail_q_mutex);
2052 if (pf->hw.func_caps.common_cap.rss_table_size)
2053 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
2055 /* setup service timer and periodic service task */
2056 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
2057 pf->serv_tmr_period = HZ;
2058 INIT_WORK(&pf->serv_task, ice_service_task);
2059 clear_bit(__ICE_SERVICE_SCHED, pf->state);
2063 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
2064 * @pf: board private structure
2066 * compute the number of MSIX vectors required (v_budget) and request from
2067 * the OS. Return the number of vectors reserved or negative on failure
2069 static int ice_ena_msix_range(struct ice_pf *pf)
2071 int v_left, v_actual, v_budget = 0;
2074 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
2076 /* reserve one vector for miscellaneous handler */
2081 /* reserve vectors for LAN traffic */
2082 pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
2083 v_budget += pf->num_lan_msix;
2084 v_left -= pf->num_lan_msix;
2086 pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
2087 sizeof(*pf->msix_entries), GFP_KERNEL);
2089 if (!pf->msix_entries) {
2094 for (i = 0; i < v_budget; i++)
2095 pf->msix_entries[i].entry = i;
2097 /* actually reserve the vectors */
2098 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
2099 ICE_MIN_MSIX, v_budget);
2102 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
2107 if (v_actual < v_budget) {
2108 dev_warn(&pf->pdev->dev,
2109 "not enough vectors. requested = %d, obtained = %d\n",
2110 v_budget, v_actual);
2111 if (v_actual >= (pf->num_lan_msix + 1)) {
2112 pf->num_avail_sw_msix = v_actual -
2113 (pf->num_lan_msix + 1);
2114 } else if (v_actual >= 2) {
2115 pf->num_lan_msix = 1;
2116 pf->num_avail_sw_msix = v_actual - 2;
2118 pci_disable_msix(pf->pdev);
2127 devm_kfree(&pf->pdev->dev, pf->msix_entries);
2131 pf->num_lan_msix = 0;
2132 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2137 * ice_dis_msix - Disable MSI-X interrupt setup in OS
2138 * @pf: board private structure
2140 static void ice_dis_msix(struct ice_pf *pf)
2142 pci_disable_msix(pf->pdev);
2143 devm_kfree(&pf->pdev->dev, pf->msix_entries);
2144 pf->msix_entries = NULL;
2145 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
2149 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
2150 * @pf: board private structure
2152 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
2154 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2157 if (pf->irq_tracker) {
2158 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
2159 pf->irq_tracker = NULL;
2164 * ice_init_interrupt_scheme - Determine proper interrupt scheme
2165 * @pf: board private structure to initialize
2167 static int ice_init_interrupt_scheme(struct ice_pf *pf)
2171 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2172 vectors = ice_ena_msix_range(pf);
2179 /* set up vector assignment tracking */
2181 devm_kzalloc(&pf->pdev->dev, sizeof(*pf->irq_tracker) +
2182 (sizeof(u16) * vectors), GFP_KERNEL);
2183 if (!pf->irq_tracker) {
2188 /* populate SW interrupts pool with number of OS granted IRQs. */
2189 pf->num_avail_sw_msix = vectors;
2190 pf->irq_tracker->num_entries = vectors;
2191 pf->irq_tracker->end = pf->irq_tracker->num_entries;
2197 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
2198 * @pf: pointer to the PF structure
2200 * There is no error returned here because the driver should be able to handle
2201 * 128 Byte cache lines, so we only print a warning in case issues are seen,
2202 * specifically with Tx.
2204 static void ice_verify_cacheline_size(struct ice_pf *pf)
2206 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
2207 dev_warn(&pf->pdev->dev,
2208 "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
2209 ICE_CACHE_LINE_BYTES);
2213 * ice_probe - Device initialization routine
2214 * @pdev: PCI device information struct
2215 * @ent: entry in ice_pci_tbl
2217 * Returns 0 on success, negative on failure
2220 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
2222 struct device *dev = &pdev->dev;
2227 /* this driver uses devres, see Documentation/driver-model/devres.txt */
2228 err = pcim_enable_device(pdev);
2232 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
2234 dev_err(dev, "BAR0 I/O map error %d\n", err);
2238 pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL);
2242 /* set up for high or low dma */
2243 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2245 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2247 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
2251 pci_enable_pcie_error_reporting(pdev);
2252 pci_set_master(pdev);
2255 pci_set_drvdata(pdev, pf);
2256 set_bit(__ICE_DOWN, pf->state);
2257 /* Disable service task until DOWN bit is cleared */
2258 set_bit(__ICE_SERVICE_DIS, pf->state);
2261 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
2263 hw->vendor_id = pdev->vendor;
2264 hw->device_id = pdev->device;
2265 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2266 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2267 hw->subsystem_device_id = pdev->subsystem_device;
2268 hw->bus.device = PCI_SLOT(pdev->devfn);
2269 hw->bus.func = PCI_FUNC(pdev->devfn);
2270 ice_set_ctrlq_len(hw);
2272 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
2274 #ifndef CONFIG_DYNAMIC_DEBUG
2276 hw->debug_mask = debug;
2279 err = ice_init_hw(hw);
2281 dev_err(dev, "ice_init_hw failed: %d\n", err);
2283 goto err_exit_unroll;
2286 dev_info(dev, "firmware %d.%d.%05d api %d.%d\n",
2287 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
2288 hw->api_maj_ver, hw->api_min_ver);
2292 err = ice_init_pf_dcb(pf, false);
2294 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
2295 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
2297 /* do not fail overall init if DCB init fails */
2301 ice_determine_q_usage(pf);
2303 pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
2304 if (!pf->num_alloc_vsi) {
2306 goto err_init_pf_unroll;
2309 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
2313 goto err_init_pf_unroll;
2316 err = ice_init_interrupt_scheme(pf);
2318 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
2320 goto err_init_interrupt_unroll;
2323 /* Driver is mostly up */
2324 clear_bit(__ICE_DOWN, pf->state);
2326 /* In case of MSIX we are going to setup the misc vector right here
2327 * to handle admin queue events etc. In case of legacy and MSI
2328 * the misc functionality and queue processing is combined in
2329 * the same vector and that gets setup at open.
2331 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2332 err = ice_req_irq_msix_misc(pf);
2334 dev_err(dev, "setup of misc vector failed: %d\n", err);
2335 goto err_init_interrupt_unroll;
2339 /* create switch struct for the switch element created by FW on boot */
2340 pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
2341 if (!pf->first_sw) {
2343 goto err_msix_misc_unroll;
2347 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
2349 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
2351 pf->first_sw->pf = pf;
2353 /* record the sw_id available for later use */
2354 pf->first_sw->sw_id = hw->port_info->sw_id;
2356 err = ice_setup_pf_sw(pf);
2358 dev_err(dev, "probe failed due to setup pf switch:%d\n", err);
2359 goto err_alloc_sw_unroll;
2362 clear_bit(__ICE_SERVICE_DIS, pf->state);
2364 /* since everything is good, start the service timer */
2365 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2367 err = ice_init_link_events(pf->hw.port_info);
2369 dev_err(dev, "ice_init_link_events failed: %d\n", err);
2370 goto err_alloc_sw_unroll;
2373 ice_verify_cacheline_size(pf);
2377 err_alloc_sw_unroll:
2378 set_bit(__ICE_SERVICE_DIS, pf->state);
2379 set_bit(__ICE_DOWN, pf->state);
2380 devm_kfree(&pf->pdev->dev, pf->first_sw);
2381 err_msix_misc_unroll:
2382 ice_free_irq_msix_misc(pf);
2383 err_init_interrupt_unroll:
2384 ice_clear_interrupt_scheme(pf);
2385 devm_kfree(dev, pf->vsi);
2390 pci_disable_pcie_error_reporting(pdev);
2395 * ice_remove - Device removal routine
2396 * @pdev: PCI device information struct
2398 static void ice_remove(struct pci_dev *pdev)
2400 struct ice_pf *pf = pci_get_drvdata(pdev);
2406 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
2407 if (!ice_is_reset_in_progress(pf->state))
2412 set_bit(__ICE_DOWN, pf->state);
2413 ice_service_task_stop(pf);
2415 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags))
2417 ice_vsi_release_all(pf);
2418 ice_free_irq_msix_misc(pf);
2419 ice_for_each_vsi(pf, i) {
2422 ice_vsi_free_q_vectors(pf->vsi[i]);
2424 ice_clear_interrupt_scheme(pf);
2426 ice_deinit_hw(&pf->hw);
2427 pci_disable_pcie_error_reporting(pdev);
2431 * ice_pci_err_detected - warning that PCI error has been detected
2432 * @pdev: PCI device information struct
2433 * @err: the type of PCI error
2435 * Called to warn that something happened on the PCI bus and the error handling
2436 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
2438 static pci_ers_result_t
2439 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err)
2441 struct ice_pf *pf = pci_get_drvdata(pdev);
2444 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
2446 return PCI_ERS_RESULT_DISCONNECT;
2449 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2450 ice_service_task_stop(pf);
2452 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2453 set_bit(__ICE_PFR_REQ, pf->state);
2454 ice_prepare_for_reset(pf);
2458 return PCI_ERS_RESULT_NEED_RESET;
2462 * ice_pci_err_slot_reset - a PCI slot reset has just happened
2463 * @pdev: PCI device information struct
2465 * Called to determine if the driver can recover from the PCI slot reset by
2466 * using a register read to determine if the device is recoverable.
2468 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
2470 struct ice_pf *pf = pci_get_drvdata(pdev);
2471 pci_ers_result_t result;
2475 err = pci_enable_device_mem(pdev);
2478 "Cannot re-enable PCI device after reset, error %d\n",
2480 result = PCI_ERS_RESULT_DISCONNECT;
2482 pci_set_master(pdev);
2483 pci_restore_state(pdev);
2484 pci_save_state(pdev);
2485 pci_wake_from_d3(pdev, false);
2487 /* Check for life */
2488 reg = rd32(&pf->hw, GLGEN_RTRIG);
2490 result = PCI_ERS_RESULT_RECOVERED;
2492 result = PCI_ERS_RESULT_DISCONNECT;
2495 err = pci_cleanup_aer_uncorrect_error_status(pdev);
2498 "pci_cleanup_aer_uncorrect_error_status failed, error %d\n",
2500 /* non-fatal, continue */
2506 * ice_pci_err_resume - restart operations after PCI error recovery
2507 * @pdev: PCI device information struct
2509 * Called to allow the driver to bring things back up after PCI error and/or
2510 * reset recovery have finished
2512 static void ice_pci_err_resume(struct pci_dev *pdev)
2514 struct ice_pf *pf = pci_get_drvdata(pdev);
2518 "%s failed, device is unrecoverable\n", __func__);
2522 if (test_bit(__ICE_SUSPENDED, pf->state)) {
2523 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
2528 ice_do_reset(pf, ICE_RESET_PFR);
2529 ice_service_task_restart(pf);
2530 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
2534 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
2535 * @pdev: PCI device information struct
2537 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
2539 struct ice_pf *pf = pci_get_drvdata(pdev);
2541 if (!test_bit(__ICE_SUSPENDED, pf->state)) {
2542 ice_service_task_stop(pf);
2544 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
2545 set_bit(__ICE_PFR_REQ, pf->state);
2546 ice_prepare_for_reset(pf);
2552 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
2553 * @pdev: PCI device information struct
2555 static void ice_pci_err_reset_done(struct pci_dev *pdev)
2557 ice_pci_err_resume(pdev);
2560 /* ice_pci_tbl - PCI Device ID Table
2562 * Wildcard entries (PCI_ANY_ID) should come last
2563 * Last entry must be all 0s
2565 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
2566 * Class, Class Mask, private data (not used) }
2568 static const struct pci_device_id ice_pci_tbl[] = {
2569 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
2570 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
2571 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
2572 /* required last entry */
2575 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
2577 static const struct pci_error_handlers ice_pci_err_handler = {
2578 .error_detected = ice_pci_err_detected,
2579 .slot_reset = ice_pci_err_slot_reset,
2580 .reset_prepare = ice_pci_err_reset_prepare,
2581 .reset_done = ice_pci_err_reset_done,
2582 .resume = ice_pci_err_resume
2585 static struct pci_driver ice_driver = {
2586 .name = KBUILD_MODNAME,
2587 .id_table = ice_pci_tbl,
2589 .remove = ice_remove,
2590 .sriov_configure = ice_sriov_configure,
2591 .err_handler = &ice_pci_err_handler
2595 * ice_module_init - Driver registration routine
2597 * ice_module_init is the first routine called when the driver is
2598 * loaded. All it does is register with the PCI subsystem.
2600 static int __init ice_module_init(void)
2604 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
2605 pr_info("%s\n", ice_copyright);
2607 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
2609 pr_err("Failed to create workqueue\n");
2613 status = pci_register_driver(&ice_driver);
2615 pr_err("failed to register pci driver, err %d\n", status);
2616 destroy_workqueue(ice_wq);
2621 module_init(ice_module_init);
2624 * ice_module_exit - Driver exit cleanup routine
2626 * ice_module_exit is called just before the driver is removed
2629 static void __exit ice_module_exit(void)
2631 pci_unregister_driver(&ice_driver);
2632 destroy_workqueue(ice_wq);
2633 pr_info("module unloaded\n");
2635 module_exit(ice_module_exit);
2638 * ice_set_mac_address - NDO callback to set MAC address
2639 * @netdev: network interface device structure
2640 * @pi: pointer to an address structure
2642 * Returns 0 on success, negative on failure
2644 static int ice_set_mac_address(struct net_device *netdev, void *pi)
2646 struct ice_netdev_priv *np = netdev_priv(netdev);
2647 struct ice_vsi *vsi = np->vsi;
2648 struct ice_pf *pf = vsi->back;
2649 struct ice_hw *hw = &pf->hw;
2650 struct sockaddr *addr = pi;
2651 enum ice_status status;
2652 LIST_HEAD(a_mac_list);
2653 LIST_HEAD(r_mac_list);
2658 mac = (u8 *)addr->sa_data;
2660 if (!is_valid_ether_addr(mac))
2661 return -EADDRNOTAVAIL;
2663 if (ether_addr_equal(netdev->dev_addr, mac)) {
2664 netdev_warn(netdev, "already using mac %pM\n", mac);
2668 if (test_bit(__ICE_DOWN, pf->state) ||
2669 ice_is_reset_in_progress(pf->state)) {
2670 netdev_err(netdev, "can't set mac %pM. device not ready\n",
2675 /* When we change the MAC address we also have to change the MAC address
2676 * based filter rules that were created previously for the old MAC
2677 * address. So first, we remove the old filter rule using ice_remove_mac
2678 * and then create a new filter rule using ice_add_mac. Note that for
2679 * both these operations, we first need to form a "list" of MAC
2680 * addresses (even though in this case, we have only 1 MAC address to be
2681 * added/removed) and this done using ice_add_mac_to_list. Depending on
2682 * the ensuing operation this "list" of MAC addresses is either to be
2683 * added or removed from the filter.
2685 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
2687 err = -EADDRNOTAVAIL;
2691 status = ice_remove_mac(hw, &r_mac_list);
2693 err = -EADDRNOTAVAIL;
2697 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
2699 err = -EADDRNOTAVAIL;
2703 status = ice_add_mac(hw, &a_mac_list);
2705 err = -EADDRNOTAVAIL;
2710 /* free list entries */
2711 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
2712 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
2715 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
2720 /* change the netdev's MAC address */
2721 memcpy(netdev->dev_addr, mac, netdev->addr_len);
2722 netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
2725 /* write new MAC address to the firmware */
2726 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2727 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
2729 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
2736 * ice_set_rx_mode - NDO callback to set the netdev filters
2737 * @netdev: network interface device structure
2739 static void ice_set_rx_mode(struct net_device *netdev)
2741 struct ice_netdev_priv *np = netdev_priv(netdev);
2742 struct ice_vsi *vsi = np->vsi;
2747 /* Set the flags to synchronize filters
2748 * ndo_set_rx_mode may be triggered even without a change in netdev
2751 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
2752 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
2753 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
2755 /* schedule our worker thread which will take care of
2756 * applying the new filter changes
2758 ice_service_task_schedule(vsi->back);
2762 * ice_fdb_add - add an entry to the hardware database
2763 * @ndm: the input from the stack
2764 * @tb: pointer to array of nladdr (unused)
2765 * @dev: the net device pointer
2766 * @addr: the MAC address entry being added
2768 * @flags: instructions from stack about fdb operation
2769 * @extack: netlink extended ack
2772 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
2773 struct net_device *dev, const unsigned char *addr, u16 vid,
2774 u16 flags, struct netlink_ext_ack __always_unused *extack)
2779 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
2782 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2783 netdev_err(dev, "FDB only supports static addresses\n");
2787 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2788 err = dev_uc_add_excl(dev, addr);
2789 else if (is_multicast_ether_addr(addr))
2790 err = dev_mc_add_excl(dev, addr);
2794 /* Only return duplicate errors if NLM_F_EXCL is set */
2795 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2802 * ice_fdb_del - delete an entry from the hardware database
2803 * @ndm: the input from the stack
2804 * @tb: pointer to array of nladdr (unused)
2805 * @dev: the net device pointer
2806 * @addr: the MAC address entry being added
2810 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
2811 struct net_device *dev, const unsigned char *addr,
2812 __always_unused u16 vid)
2816 if (ndm->ndm_state & NUD_PERMANENT) {
2817 netdev_err(dev, "FDB only supports static addresses\n");
2821 if (is_unicast_ether_addr(addr))
2822 err = dev_uc_del(dev, addr);
2823 else if (is_multicast_ether_addr(addr))
2824 err = dev_mc_del(dev, addr);
2832 * ice_set_features - set the netdev feature flags
2833 * @netdev: ptr to the netdev being adjusted
2834 * @features: the feature set that the stack is suggesting
2837 ice_set_features(struct net_device *netdev, netdev_features_t features)
2839 struct ice_netdev_priv *np = netdev_priv(netdev);
2840 struct ice_vsi *vsi = np->vsi;
2843 /* Multiple features can be changed in one call so keep features in
2844 * separate if/else statements to guarantee each feature is checked
2846 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
2847 ret = ice_vsi_manage_rss_lut(vsi, true);
2848 else if (!(features & NETIF_F_RXHASH) &&
2849 netdev->features & NETIF_F_RXHASH)
2850 ret = ice_vsi_manage_rss_lut(vsi, false);
2852 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
2853 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2854 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2855 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
2856 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
2857 ret = ice_vsi_manage_vlan_stripping(vsi, false);
2859 if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
2860 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2861 ret = ice_vsi_manage_vlan_insertion(vsi);
2862 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
2863 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
2864 ret = ice_vsi_manage_vlan_insertion(vsi);
2866 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2867 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2868 ret = ice_cfg_vlan_pruning(vsi, true, false);
2869 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2870 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2871 ret = ice_cfg_vlan_pruning(vsi, false, false);
2877 * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
2878 * @vsi: VSI to setup VLAN properties for
2880 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
2884 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2885 ret = ice_vsi_manage_vlan_stripping(vsi, true);
2886 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
2887 ret = ice_vsi_manage_vlan_insertion(vsi);
2893 * ice_vsi_cfg - Setup the VSI
2894 * @vsi: the VSI being configured
2896 * Return 0 on success and negative value on error
2898 int ice_vsi_cfg(struct ice_vsi *vsi)
2903 ice_set_rx_mode(vsi->netdev);
2905 err = ice_vsi_vlan_setup(vsi);
2910 ice_vsi_cfg_dcb_rings(vsi);
2912 err = ice_vsi_cfg_lan_txqs(vsi);
2914 err = ice_vsi_cfg_rxqs(vsi);
2920 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
2921 * @vsi: the VSI being configured
2923 static void ice_napi_enable_all(struct ice_vsi *vsi)
2930 ice_for_each_q_vector(vsi, q_idx) {
2931 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
2933 if (q_vector->rx.ring || q_vector->tx.ring)
2934 napi_enable(&q_vector->napi);
2939 * ice_up_complete - Finish the last steps of bringing up a connection
2940 * @vsi: The VSI being configured
2942 * Return 0 on success and negative value on error
2944 static int ice_up_complete(struct ice_vsi *vsi)
2946 struct ice_pf *pf = vsi->back;
2949 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2950 ice_vsi_cfg_msix(vsi);
2954 /* Enable only Rx rings, Tx rings were enabled by the FW when the
2955 * Tx queue group list was configured and the context bits were
2956 * programmed using ice_vsi_cfg_txqs
2958 err = ice_vsi_start_rx_rings(vsi);
2962 clear_bit(__ICE_DOWN, vsi->state);
2963 ice_napi_enable_all(vsi);
2964 ice_vsi_ena_irq(vsi);
2966 if (vsi->port_info &&
2967 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
2969 ice_print_link_msg(vsi, true);
2970 netif_tx_start_all_queues(vsi->netdev);
2971 netif_carrier_on(vsi->netdev);
2974 ice_service_task_schedule(pf);
2980 * ice_up - Bring the connection back up after being down
2981 * @vsi: VSI being configured
2983 int ice_up(struct ice_vsi *vsi)
2987 err = ice_vsi_cfg(vsi);
2989 err = ice_up_complete(vsi);
2995 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
2996 * @ring: Tx or Rx ring to read stats from
2997 * @pkts: packets stats counter
2998 * @bytes: bytes stats counter
3000 * This function fetches stats from the ring considering the atomic operations
3001 * that needs to be performed to read u64 values in 32 bit machine.
3004 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
3013 start = u64_stats_fetch_begin_irq(&ring->syncp);
3014 *pkts = ring->stats.pkts;
3015 *bytes = ring->stats.bytes;
3016 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3020 * ice_update_vsi_ring_stats - Update VSI stats counters
3021 * @vsi: the VSI to be updated
3023 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
3025 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
3026 struct ice_ring *ring;
3030 /* reset netdev stats */
3031 vsi_stats->tx_packets = 0;
3032 vsi_stats->tx_bytes = 0;
3033 vsi_stats->rx_packets = 0;
3034 vsi_stats->rx_bytes = 0;
3036 /* reset non-netdev (extended) stats */
3037 vsi->tx_restart = 0;
3039 vsi->tx_linearize = 0;
3040 vsi->rx_buf_failed = 0;
3041 vsi->rx_page_failed = 0;
3045 /* update Tx rings counters */
3046 ice_for_each_txq(vsi, i) {
3047 ring = READ_ONCE(vsi->tx_rings[i]);
3048 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3049 vsi_stats->tx_packets += pkts;
3050 vsi_stats->tx_bytes += bytes;
3051 vsi->tx_restart += ring->tx_stats.restart_q;
3052 vsi->tx_busy += ring->tx_stats.tx_busy;
3053 vsi->tx_linearize += ring->tx_stats.tx_linearize;
3056 /* update Rx rings counters */
3057 ice_for_each_rxq(vsi, i) {
3058 ring = READ_ONCE(vsi->rx_rings[i]);
3059 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
3060 vsi_stats->rx_packets += pkts;
3061 vsi_stats->rx_bytes += bytes;
3062 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
3063 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
3070 * ice_update_vsi_stats - Update VSI stats counters
3071 * @vsi: the VSI to be updated
3073 static void ice_update_vsi_stats(struct ice_vsi *vsi)
3075 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
3076 struct ice_eth_stats *cur_es = &vsi->eth_stats;
3077 struct ice_pf *pf = vsi->back;
3079 if (test_bit(__ICE_DOWN, vsi->state) ||
3080 test_bit(__ICE_CFG_BUSY, pf->state))
3083 /* get stats as recorded by Tx/Rx rings */
3084 ice_update_vsi_ring_stats(vsi);
3086 /* get VSI stats as recorded by the hardware */
3087 ice_update_eth_stats(vsi);
3089 cur_ns->tx_errors = cur_es->tx_errors;
3090 cur_ns->rx_dropped = cur_es->rx_discards;
3091 cur_ns->tx_dropped = cur_es->tx_discards;
3092 cur_ns->multicast = cur_es->rx_multicast;
3094 /* update some more netdev stats if this is main VSI */
3095 if (vsi->type == ICE_VSI_PF) {
3096 cur_ns->rx_crc_errors = pf->stats.crc_errors;
3097 cur_ns->rx_errors = pf->stats.crc_errors +
3098 pf->stats.illegal_bytes;
3099 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
3104 * ice_update_pf_stats - Update PF port stats counters
3105 * @pf: PF whose stats needs to be updated
3107 static void ice_update_pf_stats(struct ice_pf *pf)
3109 struct ice_hw_port_stats *prev_ps, *cur_ps;
3110 struct ice_hw *hw = &pf->hw;
3113 prev_ps = &pf->stats_prev;
3114 cur_ps = &pf->stats;
3117 ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
3118 pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
3119 &cur_ps->eth.rx_bytes);
3121 ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
3122 pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
3123 &cur_ps->eth.rx_unicast);
3125 ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
3126 pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
3127 &cur_ps->eth.rx_multicast);
3129 ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
3130 pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
3131 &cur_ps->eth.rx_broadcast);
3133 ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
3134 pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
3135 &cur_ps->eth.tx_bytes);
3137 ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
3138 pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
3139 &cur_ps->eth.tx_unicast);
3141 ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
3142 pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
3143 &cur_ps->eth.tx_multicast);
3145 ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
3146 pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
3147 &cur_ps->eth.tx_broadcast);
3149 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
3150 &prev_ps->tx_dropped_link_down,
3151 &cur_ps->tx_dropped_link_down);
3153 ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
3154 pf->stat_prev_loaded, &prev_ps->rx_size_64,
3155 &cur_ps->rx_size_64);
3157 ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
3158 pf->stat_prev_loaded, &prev_ps->rx_size_127,
3159 &cur_ps->rx_size_127);
3161 ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
3162 pf->stat_prev_loaded, &prev_ps->rx_size_255,
3163 &cur_ps->rx_size_255);
3165 ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
3166 pf->stat_prev_loaded, &prev_ps->rx_size_511,
3167 &cur_ps->rx_size_511);
3169 ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
3170 GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
3171 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
3173 ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
3174 GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
3175 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
3177 ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
3178 GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
3179 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
3181 ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
3182 pf->stat_prev_loaded, &prev_ps->tx_size_64,
3183 &cur_ps->tx_size_64);
3185 ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
3186 pf->stat_prev_loaded, &prev_ps->tx_size_127,
3187 &cur_ps->tx_size_127);
3189 ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
3190 pf->stat_prev_loaded, &prev_ps->tx_size_255,
3191 &cur_ps->tx_size_255);
3193 ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
3194 pf->stat_prev_loaded, &prev_ps->tx_size_511,
3195 &cur_ps->tx_size_511);
3197 ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
3198 GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
3199 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
3201 ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
3202 GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
3203 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
3205 ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
3206 GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
3207 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
3209 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
3210 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
3212 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
3213 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
3215 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
3216 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
3218 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
3219 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
3221 ice_update_dcb_stats(pf);
3223 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
3224 &prev_ps->crc_errors, &cur_ps->crc_errors);
3226 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
3227 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
3229 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
3230 &prev_ps->mac_local_faults,
3231 &cur_ps->mac_local_faults);
3233 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
3234 &prev_ps->mac_remote_faults,
3235 &cur_ps->mac_remote_faults);
3237 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
3238 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
3240 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
3241 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
3243 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
3244 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
3246 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
3247 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
3249 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
3250 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
3252 pf->stat_prev_loaded = true;
3256 * ice_get_stats64 - get statistics for network device structure
3257 * @netdev: network interface device structure
3258 * @stats: main device statistics structure
3261 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
3263 struct ice_netdev_priv *np = netdev_priv(netdev);
3264 struct rtnl_link_stats64 *vsi_stats;
3265 struct ice_vsi *vsi = np->vsi;
3267 vsi_stats = &vsi->net_stats;
3269 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
3271 /* netdev packet/byte stats come from ring counter. These are obtained
3272 * by summing up ring counters (done by ice_update_vsi_ring_stats).
3274 ice_update_vsi_ring_stats(vsi);
3275 stats->tx_packets = vsi_stats->tx_packets;
3276 stats->tx_bytes = vsi_stats->tx_bytes;
3277 stats->rx_packets = vsi_stats->rx_packets;
3278 stats->rx_bytes = vsi_stats->rx_bytes;
3280 /* The rest of the stats can be read from the hardware but instead we
3281 * just return values that the watchdog task has already obtained from
3284 stats->multicast = vsi_stats->multicast;
3285 stats->tx_errors = vsi_stats->tx_errors;
3286 stats->tx_dropped = vsi_stats->tx_dropped;
3287 stats->rx_errors = vsi_stats->rx_errors;
3288 stats->rx_dropped = vsi_stats->rx_dropped;
3289 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
3290 stats->rx_length_errors = vsi_stats->rx_length_errors;
3294 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3295 * @vsi: VSI having NAPI disabled
3297 static void ice_napi_disable_all(struct ice_vsi *vsi)
3304 ice_for_each_q_vector(vsi, q_idx) {
3305 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3307 if (q_vector->rx.ring || q_vector->tx.ring)
3308 napi_disable(&q_vector->napi);
3313 * ice_force_phys_link_state - Force the physical link state
3314 * @vsi: VSI to force the physical link state to up/down
3315 * @link_up: true/false indicates to set the physical link to up/down
3317 * Force the physical link state by getting the current PHY capabilities from
3318 * hardware and setting the PHY config based on the determined capabilities. If
3319 * link changes a link event will be triggered because both the Enable Automatic
3320 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
3322 * Returns 0 on success, negative on failure
3324 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
3326 struct ice_aqc_get_phy_caps_data *pcaps;
3327 struct ice_aqc_set_phy_cfg_data *cfg;
3328 struct ice_port_info *pi;
3332 if (!vsi || !vsi->port_info || !vsi->back)
3334 if (vsi->type != ICE_VSI_PF)
3337 dev = &vsi->back->pdev->dev;
3339 pi = vsi->port_info;
3341 pcaps = devm_kzalloc(dev, sizeof(*pcaps), GFP_KERNEL);
3345 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
3349 "Failed to get phy capabilities, VSI %d error %d\n",
3350 vsi->vsi_num, retcode);
3355 /* No change in link */
3356 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
3357 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
3360 cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
3366 cfg->phy_type_low = pcaps->phy_type_low;
3367 cfg->phy_type_high = pcaps->phy_type_high;
3368 cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
3369 cfg->low_power_ctrl = pcaps->low_power_ctrl;
3370 cfg->eee_cap = pcaps->eee_cap;
3371 cfg->eeer_value = pcaps->eeer_value;
3372 cfg->link_fec_opt = pcaps->link_fec_options;
3374 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
3376 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
3378 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL);
3380 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
3381 vsi->vsi_num, retcode);
3385 devm_kfree(dev, cfg);
3387 devm_kfree(dev, pcaps);
3392 * ice_down - Shutdown the connection
3393 * @vsi: The VSI being stopped
3395 int ice_down(struct ice_vsi *vsi)
3397 int i, tx_err, rx_err, link_err = 0;
3399 /* Caller of this function is expected to set the
3400 * vsi->state __ICE_DOWN bit
3403 netif_carrier_off(vsi->netdev);
3404 netif_tx_disable(vsi->netdev);
3407 ice_vsi_dis_irq(vsi);
3409 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
3411 netdev_err(vsi->netdev,
3412 "Failed stop Tx rings, VSI %d error %d\n",
3413 vsi->vsi_num, tx_err);
3415 rx_err = ice_vsi_stop_rx_rings(vsi);
3417 netdev_err(vsi->netdev,
3418 "Failed stop Rx rings, VSI %d error %d\n",
3419 vsi->vsi_num, rx_err);
3421 ice_napi_disable_all(vsi);
3423 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
3424 link_err = ice_force_phys_link_state(vsi, false);
3426 netdev_err(vsi->netdev,
3427 "Failed to set physical link down, VSI %d error %d\n",
3428 vsi->vsi_num, link_err);
3431 ice_for_each_txq(vsi, i)
3432 ice_clean_tx_ring(vsi->tx_rings[i]);
3434 ice_for_each_rxq(vsi, i)
3435 ice_clean_rx_ring(vsi->rx_rings[i]);
3437 if (tx_err || rx_err || link_err) {
3438 netdev_err(vsi->netdev,
3439 "Failed to close VSI 0x%04X on switch 0x%04X\n",
3440 vsi->vsi_num, vsi->vsw->sw_id);
3448 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
3449 * @vsi: VSI having resources allocated
3451 * Return 0 on success, negative on failure
3453 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
3457 if (!vsi->num_txq) {
3458 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
3463 ice_for_each_txq(vsi, i) {
3464 vsi->tx_rings[i]->netdev = vsi->netdev;
3465 err = ice_setup_tx_ring(vsi->tx_rings[i]);
3474 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
3475 * @vsi: VSI having resources allocated
3477 * Return 0 on success, negative on failure
3479 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
3483 if (!vsi->num_rxq) {
3484 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
3489 ice_for_each_rxq(vsi, i) {
3490 vsi->rx_rings[i]->netdev = vsi->netdev;
3491 err = ice_setup_rx_ring(vsi->rx_rings[i]);
3500 * ice_vsi_req_irq - Request IRQ from the OS
3501 * @vsi: The VSI IRQ is being requested for
3502 * @basename: name for the vector
3504 * Return 0 on success and a negative value on error
3506 static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
3508 struct ice_pf *pf = vsi->back;
3511 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3512 err = ice_vsi_req_irq_msix(vsi, basename);
3518 * ice_vsi_open - Called when a network interface is made active
3519 * @vsi: the VSI to open
3521 * Initialization of the VSI
3523 * Returns 0 on success, negative value on error
3525 static int ice_vsi_open(struct ice_vsi *vsi)
3527 char int_name[ICE_INT_NAME_STR_LEN];
3528 struct ice_pf *pf = vsi->back;
3531 /* allocate descriptors */
3532 err = ice_vsi_setup_tx_rings(vsi);
3536 err = ice_vsi_setup_rx_rings(vsi);
3540 err = ice_vsi_cfg(vsi);
3544 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3545 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
3546 err = ice_vsi_req_irq(vsi, int_name);
3550 /* Notify the stack of the actual queue counts. */
3551 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
3555 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
3559 err = ice_up_complete(vsi);
3561 goto err_up_complete;
3568 ice_vsi_free_irq(vsi);
3570 ice_vsi_free_rx_rings(vsi);
3572 ice_vsi_free_tx_rings(vsi);
3578 * ice_vsi_release_all - Delete all VSIs
3579 * @pf: PF from which all VSIs are being removed
3581 static void ice_vsi_release_all(struct ice_pf *pf)
3588 ice_for_each_vsi(pf, i) {
3592 err = ice_vsi_release(pf->vsi[i]);
3594 dev_dbg(&pf->pdev->dev,
3595 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
3596 i, err, pf->vsi[i]->vsi_num);
3601 * ice_ena_vsi - resume a VSI
3602 * @vsi: the VSI being resume
3603 * @locked: is the rtnl_lock already held
3605 static int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
3609 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
3612 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
3614 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
3615 struct net_device *netd = vsi->netdev;
3617 if (netif_running(vsi->netdev)) {
3619 err = netd->netdev_ops->ndo_open(netd);
3622 err = netd->netdev_ops->ndo_open(netd);
3626 err = ice_vsi_open(vsi);
3634 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
3636 * @locked: is the rtnl_lock already held
3639 int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3641 static int ice_pf_ena_all_vsi(struct ice_pf *pf, bool locked)
3642 #endif /* CONFIG_DCB */
3646 ice_for_each_vsi(pf, v)
3648 if (ice_ena_vsi(pf->vsi[v], locked))
3655 * ice_vsi_rebuild_all - rebuild all VSIs in pf
3658 static int ice_vsi_rebuild_all(struct ice_pf *pf)
3662 /* loop through pf->vsi array and reinit the VSI if found */
3663 ice_for_each_vsi(pf, i) {
3669 err = ice_vsi_rebuild(pf->vsi[i]);
3671 dev_err(&pf->pdev->dev,
3672 "VSI at index %d rebuild failed\n",
3677 dev_info(&pf->pdev->dev,
3678 "VSI at index %d rebuilt. vsi_num = 0x%x\n",
3679 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3686 * ice_vsi_replay_all - replay all VSIs configuration in the PF
3689 static int ice_vsi_replay_all(struct ice_pf *pf)
3691 struct ice_hw *hw = &pf->hw;
3692 enum ice_status ret;
3695 /* loop through pf->vsi array and replay the VSI if found */
3696 ice_for_each_vsi(pf, i) {
3700 ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
3702 dev_err(&pf->pdev->dev,
3703 "VSI at index %d replay failed %d\n",
3704 pf->vsi[i]->idx, ret);
3708 /* Re-map HW VSI number, using VSI handle that has been
3709 * previously validated in ice_replay_vsi() call above
3711 pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
3713 dev_info(&pf->pdev->dev,
3714 "VSI at index %d filter replayed successfully - vsi_num %i\n",
3715 pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
3718 /* Clean up replay filter after successful re-configuration */
3719 ice_replay_post(hw);
3724 * ice_rebuild - rebuild after reset
3725 * @pf: pf to rebuild
3727 static void ice_rebuild(struct ice_pf *pf)
3729 struct device *dev = &pf->pdev->dev;
3730 struct ice_hw *hw = &pf->hw;
3731 enum ice_status ret;
3734 if (test_bit(__ICE_DOWN, pf->state))
3735 goto clear_recovery;
3737 dev_dbg(dev, "rebuilding pf\n");
3739 ret = ice_init_all_ctrlq(hw);
3741 dev_err(dev, "control queues init failed %d\n", ret);
3742 goto err_init_ctrlq;
3745 ret = ice_clear_pf_cfg(hw);
3747 dev_err(dev, "clear PF configuration failed %d\n", ret);
3748 goto err_init_ctrlq;
3751 ice_clear_pxe_mode(hw);
3753 ret = ice_get_caps(hw);
3755 dev_err(dev, "ice_get_caps failed %d\n", ret);
3756 goto err_init_ctrlq;
3759 err = ice_sched_init_port(hw->port_info);
3761 goto err_sched_init_port;
3763 ice_dcb_rebuild(pf);
3765 err = ice_vsi_rebuild_all(pf);
3767 dev_err(dev, "ice_vsi_rebuild_all failed\n");
3768 goto err_vsi_rebuild;
3771 err = ice_update_link_info(hw->port_info);
3773 dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
3775 /* Replay all VSIs Configuration, including filters after reset */
3776 if (ice_vsi_replay_all(pf)) {
3777 dev_err(&pf->pdev->dev,
3778 "error replaying VSI configurations with switch filter rules\n");
3779 goto err_vsi_rebuild;
3782 /* start misc vector */
3783 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3784 err = ice_req_irq_msix_misc(pf);
3786 dev_err(dev, "misc vector setup failed: %d\n", err);
3787 goto err_vsi_rebuild;
3791 /* restart the VSIs that were rebuilt and running before the reset */
3792 err = ice_pf_ena_all_vsi(pf, false);
3794 dev_err(&pf->pdev->dev, "error enabling VSIs\n");
3795 /* no need to disable VSIs in tear down path in ice_rebuild()
3796 * since its already taken care in ice_vsi_open()
3798 goto err_vsi_rebuild;
3801 ice_for_each_vsi(pf, i) {
3804 if (!pf->vsi[i] || pf->vsi[i]->type != ICE_VSI_PF)
3806 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
3808 netif_carrier_on(pf->vsi[i]->netdev);
3809 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
3811 netif_carrier_off(pf->vsi[i]->netdev);
3812 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
3816 /* if we get here, reset flow is successful */
3817 clear_bit(__ICE_RESET_FAILED, pf->state);
3821 ice_vsi_release_all(pf);
3822 err_sched_init_port:
3823 ice_sched_cleanup_all(hw);
3825 ice_shutdown_all_ctrlq(hw);
3826 set_bit(__ICE_RESET_FAILED, pf->state);
3828 /* set this bit in PF state to control service task scheduling */
3829 set_bit(__ICE_NEEDS_RESTART, pf->state);
3830 dev_err(dev, "Rebuild failed, unload and reload driver\n");
3834 * ice_change_mtu - NDO callback to change the MTU
3835 * @netdev: network interface device structure
3836 * @new_mtu: new value for maximum frame size
3838 * Returns 0 on success, negative on failure
3840 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
3842 struct ice_netdev_priv *np = netdev_priv(netdev);
3843 struct ice_vsi *vsi = np->vsi;
3844 struct ice_pf *pf = vsi->back;
3847 if (new_mtu == netdev->mtu) {
3848 netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
3852 if (new_mtu < netdev->min_mtu) {
3853 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
3856 } else if (new_mtu > netdev->max_mtu) {
3857 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
3861 /* if a reset is in progress, wait for some time for it to complete */
3863 if (ice_is_reset_in_progress(pf->state)) {
3865 usleep_range(1000, 2000);
3870 } while (count < 100);
3873 netdev_err(netdev, "can't change mtu. Device is busy\n");
3877 netdev->mtu = new_mtu;
3879 /* if VSI is up, bring it down and then back up */
3880 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
3883 err = ice_down(vsi);
3885 netdev_err(netdev, "change mtu if_up err %d\n", err);
3891 netdev_err(netdev, "change mtu if_up err %d\n", err);
3896 netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
3901 * ice_set_rss - Set RSS keys and lut
3902 * @vsi: Pointer to VSI structure
3903 * @seed: RSS hash seed
3904 * @lut: Lookup table
3905 * @lut_size: Lookup table size
3907 * Returns 0 on success, negative on failure
3909 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3911 struct ice_pf *pf = vsi->back;
3912 struct ice_hw *hw = &pf->hw;
3913 enum ice_status status;
3916 struct ice_aqc_get_set_rss_keys *buf =
3917 (struct ice_aqc_get_set_rss_keys *)seed;
3919 status = ice_aq_set_rss_key(hw, vsi->idx, buf);
3922 dev_err(&pf->pdev->dev,
3923 "Cannot set RSS key, err %d aq_err %d\n",
3924 status, hw->adminq.rq_last_status);
3930 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
3933 dev_err(&pf->pdev->dev,
3934 "Cannot set RSS lut, err %d aq_err %d\n",
3935 status, hw->adminq.rq_last_status);
3944 * ice_get_rss - Get RSS keys and lut
3945 * @vsi: Pointer to VSI structure
3946 * @seed: Buffer to store the keys
3947 * @lut: Buffer to store the lookup table entries
3948 * @lut_size: Size of buffer to store the lookup table entries
3950 * Returns 0 on success, negative on failure
3952 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
3954 struct ice_pf *pf = vsi->back;
3955 struct ice_hw *hw = &pf->hw;
3956 enum ice_status status;
3959 struct ice_aqc_get_set_rss_keys *buf =
3960 (struct ice_aqc_get_set_rss_keys *)seed;
3962 status = ice_aq_get_rss_key(hw, vsi->idx, buf);
3964 dev_err(&pf->pdev->dev,
3965 "Cannot get RSS key, err %d aq_err %d\n",
3966 status, hw->adminq.rq_last_status);
3972 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
3975 dev_err(&pf->pdev->dev,
3976 "Cannot get RSS lut, err %d aq_err %d\n",
3977 status, hw->adminq.rq_last_status);
3986 * ice_bridge_getlink - Get the hardware bridge mode
3989 * @seq: RTNL message seq
3990 * @dev: the netdev being configured
3991 * @filter_mask: filter mask passed in
3992 * @nlflags: netlink flags passed in
3994 * Return the bridge mode (VEB/VEPA)
3997 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3998 struct net_device *dev, u32 filter_mask, int nlflags)
4000 struct ice_netdev_priv *np = netdev_priv(dev);
4001 struct ice_vsi *vsi = np->vsi;
4002 struct ice_pf *pf = vsi->back;
4005 bmode = pf->first_sw->bridge_mode;
4007 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
4012 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
4013 * @vsi: Pointer to VSI structure
4014 * @bmode: Hardware bridge mode (VEB/VEPA)
4016 * Returns 0 on success, negative on failure
4018 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
4020 struct device *dev = &vsi->back->pdev->dev;
4021 struct ice_aqc_vsi_props *vsi_props;
4022 struct ice_hw *hw = &vsi->back->hw;
4023 struct ice_vsi_ctx *ctxt;
4024 enum ice_status status;
4027 vsi_props = &vsi->info;
4029 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
4033 ctxt->info = vsi->info;
4035 if (bmode == BRIDGE_MODE_VEB)
4036 /* change from VEPA to VEB mode */
4037 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4039 /* change from VEB to VEPA mode */
4040 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
4041 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4043 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4045 dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
4046 bmode, status, hw->adminq.sq_last_status);
4050 /* Update sw flags for book keeping */
4051 vsi_props->sw_flags = ctxt->info.sw_flags;
4054 devm_kfree(dev, ctxt);
4059 * ice_bridge_setlink - Set the hardware bridge mode
4060 * @dev: the netdev being configured
4061 * @nlh: RTNL message
4062 * @flags: bridge setlink flags
4063 * @extack: netlink extended ack
4065 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
4066 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
4067 * not already set for all VSIs connected to this switch. And also update the
4068 * unicast switch filter rules for the corresponding switch of the netdev.
4071 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4072 u16 __always_unused flags,
4073 struct netlink_ext_ack __always_unused *extack)
4075 struct ice_netdev_priv *np = netdev_priv(dev);
4076 struct ice_pf *pf = np->vsi->back;
4077 struct nlattr *attr, *br_spec;
4078 struct ice_hw *hw = &pf->hw;
4079 enum ice_status status;
4080 struct ice_sw *pf_sw;
4081 int rem, v, err = 0;
4083 pf_sw = pf->first_sw;
4084 /* find the attribute in the netlink message */
4085 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4087 nla_for_each_nested(attr, br_spec, rem) {
4090 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4092 mode = nla_get_u16(attr);
4093 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4095 /* Continue if bridge mode is not being flipped */
4096 if (mode == pf_sw->bridge_mode)
4098 /* Iterates through the PF VSI list and update the loopback
4101 ice_for_each_vsi(pf, v) {
4104 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
4109 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
4110 /* Update the unicast switch filter rules for the corresponding
4111 * switch of the netdev
4113 status = ice_update_sw_rule_bridge_mode(hw);
4115 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n",
4116 mode, status, hw->adminq.sq_last_status);
4117 /* revert hw->evb_veb */
4118 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
4122 pf_sw->bridge_mode = mode;
4129 * ice_tx_timeout - Respond to a Tx Hang
4130 * @netdev: network interface device structure
4132 static void ice_tx_timeout(struct net_device *netdev)
4134 struct ice_netdev_priv *np = netdev_priv(netdev);
4135 struct ice_ring *tx_ring = NULL;
4136 struct ice_vsi *vsi = np->vsi;
4137 struct ice_pf *pf = vsi->back;
4138 int hung_queue = -1;
4141 pf->tx_timeout_count++;
4143 /* find the stopped queue the same way dev_watchdog() does */
4144 for (i = 0; i < netdev->num_tx_queues; i++) {
4145 unsigned long trans_start;
4146 struct netdev_queue *q;
4148 q = netdev_get_tx_queue(netdev, i);
4149 trans_start = q->trans_start;
4150 if (netif_xmit_stopped(q) &&
4152 trans_start + netdev->watchdog_timeo)) {
4158 if (i == netdev->num_tx_queues)
4159 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
4161 /* now that we have an index, find the tx_ring struct */
4162 for (i = 0; i < vsi->num_txq; i++)
4163 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4164 if (hung_queue == vsi->tx_rings[i]->q_index) {
4165 tx_ring = vsi->tx_rings[i];
4169 /* Reset recovery level if enough time has elapsed after last timeout.
4170 * Also ensure no new reset action happens before next timeout period.
4172 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
4173 pf->tx_timeout_recovery_level = 1;
4174 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
4175 netdev->watchdog_timeo)))
4179 struct ice_hw *hw = &pf->hw;
4182 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[hung_queue])) &
4183 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
4184 /* Read interrupt register */
4185 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4187 GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
4189 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
4190 vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
4191 head, tx_ring->next_to_use, val);
4194 pf->tx_timeout_last_recovery = jiffies;
4195 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
4196 pf->tx_timeout_recovery_level, hung_queue);
4198 switch (pf->tx_timeout_recovery_level) {
4200 set_bit(__ICE_PFR_REQ, pf->state);
4203 set_bit(__ICE_CORER_REQ, pf->state);
4206 set_bit(__ICE_GLOBR_REQ, pf->state);
4209 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
4210 set_bit(__ICE_DOWN, pf->state);
4211 set_bit(__ICE_NEEDS_RESTART, vsi->state);
4212 set_bit(__ICE_SERVICE_DIS, pf->state);
4216 ice_service_task_schedule(pf);
4217 pf->tx_timeout_recovery_level++;
4221 * ice_open - Called when a network interface becomes active
4222 * @netdev: network interface device structure
4224 * The open entry point is called when a network interface is made
4225 * active by the system (IFF_UP). At this point all resources needed
4226 * for transmit and receive operations are allocated, the interrupt
4227 * handler is registered with the OS, the netdev watchdog is enabled,
4228 * and the stack is notified that the interface is ready.
4230 * Returns 0 on success, negative value on failure
4232 int ice_open(struct net_device *netdev)
4234 struct ice_netdev_priv *np = netdev_priv(netdev);
4235 struct ice_vsi *vsi = np->vsi;
4238 if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
4239 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
4243 netif_carrier_off(netdev);
4245 err = ice_force_phys_link_state(vsi, true);
4248 "Failed to set physical link up, error %d\n", err);
4252 err = ice_vsi_open(vsi);
4254 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
4255 vsi->vsi_num, vsi->vsw->sw_id);
4260 * ice_stop - Disables a network interface
4261 * @netdev: network interface device structure
4263 * The stop entry point is called when an interface is de-activated by the OS,
4264 * and the netdevice enters the DOWN state. The hardware is still under the
4265 * driver's control, but the netdev interface is disabled.
4267 * Returns success only - not allowed to fail
4269 int ice_stop(struct net_device *netdev)
4271 struct ice_netdev_priv *np = netdev_priv(netdev);
4272 struct ice_vsi *vsi = np->vsi;
4280 * ice_features_check - Validate encapsulated packet conforms to limits
4282 * @netdev: This port's netdev
4283 * @features: Offload features that the stack believes apply
4285 static netdev_features_t
4286 ice_features_check(struct sk_buff *skb,
4287 struct net_device __always_unused *netdev,
4288 netdev_features_t features)
4292 /* No point in doing any of this if neither checksum nor GSO are
4293 * being requested for this frame. We can rule out both by just
4294 * checking for CHECKSUM_PARTIAL
4296 if (skb->ip_summed != CHECKSUM_PARTIAL)
4299 /* We cannot support GSO if the MSS is going to be less than
4300 * 64 bytes. If it is then we need to drop support for GSO.
4302 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4303 features &= ~NETIF_F_GSO_MASK;
4305 len = skb_network_header(skb) - skb->data;
4306 if (len & ~(ICE_TXD_MACLEN_MAX))
4307 goto out_rm_features;
4309 len = skb_transport_header(skb) - skb_network_header(skb);
4310 if (len & ~(ICE_TXD_IPLEN_MAX))
4311 goto out_rm_features;
4313 if (skb->encapsulation) {
4314 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4315 if (len & ~(ICE_TXD_L4LEN_MAX))
4316 goto out_rm_features;
4318 len = skb_inner_transport_header(skb) -
4319 skb_inner_network_header(skb);
4320 if (len & ~(ICE_TXD_IPLEN_MAX))
4321 goto out_rm_features;
4326 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4329 static const struct net_device_ops ice_netdev_ops = {
4330 .ndo_open = ice_open,
4331 .ndo_stop = ice_stop,
4332 .ndo_start_xmit = ice_start_xmit,
4333 .ndo_features_check = ice_features_check,
4334 .ndo_set_rx_mode = ice_set_rx_mode,
4335 .ndo_set_mac_address = ice_set_mac_address,
4336 .ndo_validate_addr = eth_validate_addr,
4337 .ndo_change_mtu = ice_change_mtu,
4338 .ndo_get_stats64 = ice_get_stats64,
4339 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
4340 .ndo_set_vf_mac = ice_set_vf_mac,
4341 .ndo_get_vf_config = ice_get_vf_cfg,
4342 .ndo_set_vf_trust = ice_set_vf_trust,
4343 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
4344 .ndo_set_vf_link_state = ice_set_vf_link_state,
4345 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
4346 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
4347 .ndo_set_features = ice_set_features,
4348 .ndo_bridge_getlink = ice_bridge_getlink,
4349 .ndo_bridge_setlink = ice_bridge_setlink,
4350 .ndo_fdb_add = ice_fdb_add,
4351 .ndo_fdb_del = ice_fdb_del,
4352 .ndo_tx_timeout = ice_tx_timeout,