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
8 #include <generated/utsrelease.h>
13 #include "ice_dcb_lib.h"
14 #include "ice_dcb_nl.h"
15 #include "ice_devlink.h"
16 /* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
17 * ice tracepoint functions. This must be done exactly once across the
20 #define CREATE_TRACE_POINTS
21 #include "ice_trace.h"
22 #include "ice_eswitch.h"
23 #include "ice_tc_lib.h"
25 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
26 static const char ice_driver_string[] = DRV_SUMMARY;
27 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
29 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
30 #define ICE_DDP_PKG_PATH "intel/ice/ddp/"
31 #define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg"
33 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
34 MODULE_DESCRIPTION(DRV_SUMMARY);
35 MODULE_LICENSE("GPL v2");
36 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
38 static int debug = -1;
39 module_param(debug, int, 0644);
40 #ifndef CONFIG_DYNAMIC_DEBUG
41 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
43 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
44 #endif /* !CONFIG_DYNAMIC_DEBUG */
46 static DEFINE_IDA(ice_aux_ida);
47 DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
48 EXPORT_SYMBOL(ice_xdp_locking_key);
50 static struct workqueue_struct *ice_wq;
51 static const struct net_device_ops ice_netdev_safe_mode_ops;
52 static const struct net_device_ops ice_netdev_ops;
54 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
56 static void ice_vsi_release_all(struct ice_pf *pf);
58 static int ice_rebuild_channels(struct ice_pf *pf);
59 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
62 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
63 void *cb_priv, enum tc_setup_type type, void *type_data,
65 void (*cleanup)(struct flow_block_cb *block_cb));
67 bool netif_is_ice(struct net_device *dev)
69 return dev && (dev->netdev_ops == &ice_netdev_ops);
73 * ice_get_tx_pending - returns number of Tx descriptors not processed
74 * @ring: the ring of descriptors
76 static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
80 head = ring->next_to_clean;
81 tail = ring->next_to_use;
84 return (head < tail) ?
85 tail - head : (tail + ring->count - head);
90 * ice_check_for_hang_subtask - check for and recover hung queues
91 * @pf: pointer to PF struct
93 static void ice_check_for_hang_subtask(struct ice_pf *pf)
95 struct ice_vsi *vsi = NULL;
101 ice_for_each_vsi(pf, v)
102 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
107 if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
110 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
115 ice_for_each_txq(vsi, i) {
116 struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
120 if (ice_ring_ch_enabled(tx_ring))
124 /* If packet counter has not changed the queue is
125 * likely stalled, so force an interrupt for this
128 * prev_pkt would be negative if there was no
131 packets = tx_ring->stats.pkts & INT_MAX;
132 if (tx_ring->tx_stats.prev_pkt == packets) {
133 /* Trigger sw interrupt to revive the queue */
134 ice_trigger_sw_intr(hw, tx_ring->q_vector);
138 /* Memory barrier between read of packet count and call
139 * to ice_get_tx_pending()
142 tx_ring->tx_stats.prev_pkt =
143 ice_get_tx_pending(tx_ring) ? packets : -1;
149 * ice_init_mac_fltr - Set initial MAC filters
150 * @pf: board private structure
152 * Set initial set of MAC filters for PF VSI; configure filters for permanent
153 * address and broadcast address. If an error is encountered, netdevice will be
156 static int ice_init_mac_fltr(struct ice_pf *pf)
161 vsi = ice_get_main_vsi(pf);
165 perm_addr = vsi->port_info->mac.perm_addr;
166 return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
170 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
171 * @netdev: the net device on which the sync is happening
172 * @addr: MAC address to sync
174 * This is a callback function which is called by the in kernel device sync
175 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
176 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
177 * MAC filters from the hardware.
179 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
181 struct ice_netdev_priv *np = netdev_priv(netdev);
182 struct ice_vsi *vsi = np->vsi;
184 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
192 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
193 * @netdev: the net device on which the unsync is happening
194 * @addr: MAC address to unsync
196 * This is a callback function which is called by the in kernel device unsync
197 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
198 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
199 * delete the MAC filters from the hardware.
201 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
203 struct ice_netdev_priv *np = netdev_priv(netdev);
204 struct ice_vsi *vsi = np->vsi;
206 /* Under some circumstances, we might receive a request to delete our
207 * own device address from our uc list. Because we store the device
208 * address in the VSI's MAC filter list, we need to ignore such
209 * requests and not delete our device address from this list.
211 if (ether_addr_equal(addr, netdev->dev_addr))
214 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
222 * ice_vsi_fltr_changed - check if filter state changed
223 * @vsi: VSI to be checked
225 * returns true if filter state has changed, false otherwise.
227 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
229 return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
230 test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state) ||
231 test_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
235 * ice_set_promisc - Enable promiscuous mode for a given PF
236 * @vsi: the VSI being configured
237 * @promisc_m: mask of promiscuous config bits
240 static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
244 if (vsi->type != ICE_VSI_PF)
247 if (vsi->num_vlan > 1)
248 status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi, promisc_m);
250 status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m, 0);
255 * ice_clear_promisc - Disable promiscuous mode for a given PF
256 * @vsi: the VSI being configured
257 * @promisc_m: mask of promiscuous config bits
260 static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
264 if (vsi->type != ICE_VSI_PF)
267 if (vsi->num_vlan > 1)
268 status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi, promisc_m);
270 status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m, 0);
275 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
276 * @vsi: ptr to the VSI
278 * Push any outstanding VSI filter changes through the AdminQ.
280 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
282 struct device *dev = ice_pf_to_dev(vsi->back);
283 struct net_device *netdev = vsi->netdev;
284 bool promisc_forced_on = false;
285 struct ice_pf *pf = vsi->back;
286 struct ice_hw *hw = &pf->hw;
287 u32 changed_flags = 0;
294 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
295 usleep_range(1000, 2000);
297 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
298 vsi->current_netdev_flags = vsi->netdev->flags;
300 INIT_LIST_HEAD(&vsi->tmp_sync_list);
301 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
303 if (ice_vsi_fltr_changed(vsi)) {
304 clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
305 clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
306 clear_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
308 /* grab the netdev's addr_list_lock */
309 netif_addr_lock_bh(netdev);
310 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
311 ice_add_mac_to_unsync_list);
312 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
313 ice_add_mac_to_unsync_list);
314 /* our temp lists are populated. release lock */
315 netif_addr_unlock_bh(netdev);
318 /* Remove MAC addresses in the unsync list */
319 err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
320 ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
322 netdev_err(netdev, "Failed to delete MAC filters\n");
323 /* if we failed because of alloc failures, just bail */
328 /* Add MAC addresses in the sync list */
329 err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
330 ice_fltr_free_list(dev, &vsi->tmp_sync_list);
331 /* If filter is added successfully or already exists, do not go into
332 * 'if' condition and report it as error. Instead continue processing
333 * rest of the function.
335 if (err && err != -EEXIST) {
336 netdev_err(netdev, "Failed to add MAC filters\n");
337 /* If there is no more space for new umac filters, VSI
338 * should go into promiscuous mode. There should be some
339 * space reserved for promiscuous filters.
341 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
342 !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
344 promisc_forced_on = true;
345 netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
352 /* check for changes in promiscuous modes */
353 if (changed_flags & IFF_ALLMULTI) {
354 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
355 if (vsi->num_vlan > 1)
356 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
358 promisc_m = ICE_MCAST_PROMISC_BITS;
360 err = ice_set_promisc(vsi, promisc_m);
362 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
364 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
368 /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
369 if (vsi->num_vlan > 1)
370 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
372 promisc_m = ICE_MCAST_PROMISC_BITS;
374 err = ice_clear_promisc(vsi, promisc_m);
376 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
378 vsi->current_netdev_flags |= IFF_ALLMULTI;
384 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
385 test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
386 clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
387 if (vsi->current_netdev_flags & IFF_PROMISC) {
388 /* Apply Rx filter rule to get traffic from wire */
389 if (!ice_is_dflt_vsi_in_use(pf->first_sw)) {
390 err = ice_set_dflt_vsi(pf->first_sw, vsi);
391 if (err && err != -EEXIST) {
392 netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
394 vsi->current_netdev_flags &=
399 ice_cfg_vlan_pruning(vsi, false);
402 /* Clear Rx filter to remove traffic from wire */
403 if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) {
404 err = ice_clear_dflt_vsi(pf->first_sw);
406 netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
408 vsi->current_netdev_flags |=
412 if (vsi->num_vlan > 1)
413 ice_cfg_vlan_pruning(vsi, true);
420 set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
423 /* if something went wrong then set the changed flag so we try again */
424 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
425 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
427 clear_bit(ICE_CFG_BUSY, vsi->state);
432 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
433 * @pf: board private structure
435 static void ice_sync_fltr_subtask(struct ice_pf *pf)
439 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
442 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
444 ice_for_each_vsi(pf, v)
445 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
446 ice_vsi_sync_fltr(pf->vsi[v])) {
447 /* come back and try again later */
448 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
454 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
456 * @locked: is the rtnl_lock already held
458 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
463 ice_for_each_vsi(pf, v)
465 ice_dis_vsi(pf->vsi[v], locked);
467 for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
468 pf->pf_agg_node[node].num_vsis = 0;
470 for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
471 pf->vf_agg_node[node].num_vsis = 0;
475 * ice_clear_sw_switch_recipes - clear switch recipes
476 * @pf: board private structure
478 * Mark switch recipes as not created in sw structures. There are cases where
479 * rules (especially advanced rules) need to be restored, either re-read from
480 * hardware or added again. For example after the reset. 'recp_created' flag
481 * prevents from doing that and need to be cleared upfront.
483 static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
485 struct ice_sw_recipe *recp;
488 recp = pf->hw.switch_info->recp_list;
489 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
490 recp[i].recp_created = false;
494 * ice_prepare_for_reset - prep for reset
495 * @pf: board private structure
496 * @reset_type: reset type requested
498 * Inform or close all dependent features in prep for reset.
501 ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
503 struct ice_hw *hw = &pf->hw;
507 dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
509 /* already prepared for reset */
510 if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
513 ice_unplug_aux_dev(pf);
515 /* Notify VFs of impending reset */
516 if (ice_check_sq_alive(hw, &hw->mailboxq))
517 ice_vc_notify_reset(pf);
519 /* Disable VFs until reset is completed */
520 ice_for_each_vf(pf, i)
521 ice_set_vf_state_qs_dis(&pf->vf[i]);
523 if (ice_is_eswitch_mode_switchdev(pf)) {
524 if (reset_type != ICE_RESET_PFR)
525 ice_clear_sw_switch_recipes(pf);
528 /* release ADQ specific HW and SW resources */
529 vsi = ice_get_main_vsi(pf);
533 /* to be on safe side, reset orig_rss_size so that normal flow
534 * of deciding rss_size can take precedence
536 vsi->orig_rss_size = 0;
538 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
539 if (reset_type == ICE_RESET_PFR) {
540 vsi->old_ena_tc = vsi->all_enatc;
541 vsi->old_numtc = vsi->all_numtc;
543 ice_remove_q_channels(vsi, true);
545 /* for other reset type, do not support channel rebuild
546 * hence reset needed info
554 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
555 memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
560 /* clear SW filtering DB */
561 ice_clear_hw_tbls(hw);
562 /* disable the VSIs and their queues that are not already DOWN */
563 ice_pf_dis_all_vsi(pf, false);
565 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
566 ice_ptp_prepare_for_reset(pf);
569 ice_sched_clear_port(hw->port_info);
571 ice_shutdown_all_ctrlq(hw);
573 set_bit(ICE_PREPARED_FOR_RESET, pf->state);
577 * ice_do_reset - Initiate one of many types of resets
578 * @pf: board private structure
579 * @reset_type: reset type requested before this function was called.
581 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
583 struct device *dev = ice_pf_to_dev(pf);
584 struct ice_hw *hw = &pf->hw;
586 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
588 ice_prepare_for_reset(pf, reset_type);
590 /* trigger the reset */
591 if (ice_reset(hw, reset_type)) {
592 dev_err(dev, "reset %d failed\n", reset_type);
593 set_bit(ICE_RESET_FAILED, pf->state);
594 clear_bit(ICE_RESET_OICR_RECV, pf->state);
595 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
596 clear_bit(ICE_PFR_REQ, pf->state);
597 clear_bit(ICE_CORER_REQ, pf->state);
598 clear_bit(ICE_GLOBR_REQ, pf->state);
599 wake_up(&pf->reset_wait_queue);
603 /* PFR is a bit of a special case because it doesn't result in an OICR
604 * interrupt. So for PFR, rebuild after the reset and clear the reset-
605 * associated state bits.
607 if (reset_type == ICE_RESET_PFR) {
609 ice_rebuild(pf, reset_type);
610 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
611 clear_bit(ICE_PFR_REQ, pf->state);
612 wake_up(&pf->reset_wait_queue);
613 ice_reset_all_vfs(pf, true);
618 * ice_reset_subtask - Set up for resetting the device and driver
619 * @pf: board private structure
621 static void ice_reset_subtask(struct ice_pf *pf)
623 enum ice_reset_req reset_type = ICE_RESET_INVAL;
625 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
626 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
627 * of reset is pending and sets bits in pf->state indicating the reset
628 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
629 * prepare for pending reset if not already (for PF software-initiated
630 * global resets the software should already be prepared for it as
631 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
632 * by firmware or software on other PFs, that bit is not set so prepare
633 * for the reset now), poll for reset done, rebuild and return.
635 if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
636 /* Perform the largest reset requested */
637 if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
638 reset_type = ICE_RESET_CORER;
639 if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
640 reset_type = ICE_RESET_GLOBR;
641 if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
642 reset_type = ICE_RESET_EMPR;
643 /* return if no valid reset type requested */
644 if (reset_type == ICE_RESET_INVAL)
646 ice_prepare_for_reset(pf, reset_type);
648 /* make sure we are ready to rebuild */
649 if (ice_check_reset(&pf->hw)) {
650 set_bit(ICE_RESET_FAILED, pf->state);
652 /* done with reset. start rebuild */
653 pf->hw.reset_ongoing = false;
654 ice_rebuild(pf, reset_type);
655 /* clear bit to resume normal operations, but
656 * ICE_NEEDS_RESTART bit is set in case rebuild failed
658 clear_bit(ICE_RESET_OICR_RECV, pf->state);
659 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
660 clear_bit(ICE_PFR_REQ, pf->state);
661 clear_bit(ICE_CORER_REQ, pf->state);
662 clear_bit(ICE_GLOBR_REQ, pf->state);
663 wake_up(&pf->reset_wait_queue);
664 ice_reset_all_vfs(pf, true);
670 /* No pending resets to finish processing. Check for new resets */
671 if (test_bit(ICE_PFR_REQ, pf->state))
672 reset_type = ICE_RESET_PFR;
673 if (test_bit(ICE_CORER_REQ, pf->state))
674 reset_type = ICE_RESET_CORER;
675 if (test_bit(ICE_GLOBR_REQ, pf->state))
676 reset_type = ICE_RESET_GLOBR;
677 /* If no valid reset type requested just return */
678 if (reset_type == ICE_RESET_INVAL)
681 /* reset if not already down or busy */
682 if (!test_bit(ICE_DOWN, pf->state) &&
683 !test_bit(ICE_CFG_BUSY, pf->state)) {
684 ice_do_reset(pf, reset_type);
689 * ice_print_topo_conflict - print topology conflict message
690 * @vsi: the VSI whose topology status is being checked
692 static void ice_print_topo_conflict(struct ice_vsi *vsi)
694 switch (vsi->port_info->phy.link_info.topo_media_conflict) {
695 case ICE_AQ_LINK_TOPO_CONFLICT:
696 case ICE_AQ_LINK_MEDIA_CONFLICT:
697 case ICE_AQ_LINK_TOPO_UNREACH_PRT:
698 case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
699 case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
700 netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
702 case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
703 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
704 netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
706 netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
714 * ice_print_link_msg - print link up or down message
715 * @vsi: the VSI whose link status is being queried
716 * @isup: boolean for if the link is now up or down
718 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
720 struct ice_aqc_get_phy_caps_data *caps;
721 const char *an_advertised;
732 if (vsi->current_isup == isup)
735 vsi->current_isup = isup;
738 netdev_info(vsi->netdev, "NIC Link is Down\n");
742 switch (vsi->port_info->phy.link_info.link_speed) {
743 case ICE_AQ_LINK_SPEED_100GB:
746 case ICE_AQ_LINK_SPEED_50GB:
749 case ICE_AQ_LINK_SPEED_40GB:
752 case ICE_AQ_LINK_SPEED_25GB:
755 case ICE_AQ_LINK_SPEED_20GB:
758 case ICE_AQ_LINK_SPEED_10GB:
761 case ICE_AQ_LINK_SPEED_5GB:
764 case ICE_AQ_LINK_SPEED_2500MB:
767 case ICE_AQ_LINK_SPEED_1000MB:
770 case ICE_AQ_LINK_SPEED_100MB:
778 switch (vsi->port_info->fc.current_mode) {
782 case ICE_FC_TX_PAUSE:
785 case ICE_FC_RX_PAUSE:
796 /* Get FEC mode based on negotiated link info */
797 switch (vsi->port_info->phy.link_info.fec_info) {
798 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
799 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
802 case ICE_AQ_LINK_25G_KR_FEC_EN:
803 fec = "FC-FEC/BASE-R";
810 /* check if autoneg completed, might be false due to not supported */
811 if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
816 /* Get FEC mode requested based on PHY caps last SW configuration */
817 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
820 an_advertised = "Unknown";
824 status = ice_aq_get_phy_caps(vsi->port_info, false,
825 ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
827 netdev_info(vsi->netdev, "Get phy capability failed.\n");
829 an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
831 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
832 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
834 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
835 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
836 fec_req = "FC-FEC/BASE-R";
843 netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
844 speed, fec_req, fec, an_advertised, an, fc);
845 ice_print_topo_conflict(vsi);
849 * ice_vsi_link_event - update the VSI's netdev
850 * @vsi: the VSI on which the link event occurred
851 * @link_up: whether or not the VSI needs to be set up or down
853 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
858 if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
861 if (vsi->type == ICE_VSI_PF) {
862 if (link_up == netif_carrier_ok(vsi->netdev))
866 netif_carrier_on(vsi->netdev);
867 netif_tx_wake_all_queues(vsi->netdev);
869 netif_carrier_off(vsi->netdev);
870 netif_tx_stop_all_queues(vsi->netdev);
876 * ice_set_dflt_mib - send a default config MIB to the FW
877 * @pf: private PF struct
879 * This function sends a default configuration MIB to the FW.
881 * If this function errors out at any point, the driver is still able to
882 * function. The main impact is that LFC may not operate as expected.
883 * Therefore an error state in this function should be treated with a DBG
884 * message and continue on with driver rebuild/reenable.
886 static void ice_set_dflt_mib(struct ice_pf *pf)
888 struct device *dev = ice_pf_to_dev(pf);
889 u8 mib_type, *buf, *lldpmib = NULL;
890 u16 len, typelen, offset = 0;
891 struct ice_lldp_org_tlv *tlv;
892 struct ice_hw *hw = &pf->hw;
895 mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
896 lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
898 dev_dbg(dev, "%s Failed to allocate MIB memory\n",
903 /* Add ETS CFG TLV */
904 tlv = (struct ice_lldp_org_tlv *)lldpmib;
905 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
906 ICE_IEEE_ETS_TLV_LEN);
907 tlv->typelen = htons(typelen);
908 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
909 ICE_IEEE_SUBTYPE_ETS_CFG);
910 tlv->ouisubtype = htonl(ouisubtype);
915 /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
916 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
917 * Octets 13 - 20 are TSA values - leave as zeros
920 len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
922 tlv = (struct ice_lldp_org_tlv *)
923 ((char *)tlv + sizeof(tlv->typelen) + len);
925 /* Add ETS REC TLV */
927 tlv->typelen = htons(typelen);
929 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
930 ICE_IEEE_SUBTYPE_ETS_REC);
931 tlv->ouisubtype = htonl(ouisubtype);
933 /* First octet of buf is reserved
934 * Octets 1 - 4 map UP to TC - all UPs map to zero
935 * Octets 5 - 12 are BW values - set TC 0 to 100%.
936 * Octets 13 - 20 are TSA value - leave as zeros
940 tlv = (struct ice_lldp_org_tlv *)
941 ((char *)tlv + sizeof(tlv->typelen) + len);
943 /* Add PFC CFG TLV */
944 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
945 ICE_IEEE_PFC_TLV_LEN);
946 tlv->typelen = htons(typelen);
948 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
949 ICE_IEEE_SUBTYPE_PFC_CFG);
950 tlv->ouisubtype = htonl(ouisubtype);
952 /* Octet 1 left as all zeros - PFC disabled */
954 len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
957 if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
958 dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
964 * ice_check_phy_fw_load - check if PHY FW load failed
965 * @pf: pointer to PF struct
966 * @link_cfg_err: bitmap from the link info structure
968 * check if external PHY FW load failed and print an error message if it did
970 static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
972 if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
973 clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
977 if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
980 if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
981 dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
982 set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
987 * ice_check_module_power
988 * @pf: pointer to PF struct
989 * @link_cfg_err: bitmap from the link info structure
991 * check module power level returned by a previous call to aq_get_link_info
992 * and print error messages if module power level is not supported
994 static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
996 /* if module power level is supported, clear the flag */
997 if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
998 ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
999 clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1003 /* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1004 * above block didn't clear this bit, there's nothing to do
1006 if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1009 if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1010 dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1011 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1012 } else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1013 dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1014 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1019 * ice_check_link_cfg_err - check if link configuration failed
1020 * @pf: pointer to the PF struct
1021 * @link_cfg_err: bitmap from the link info structure
1023 * print if any link configuration failure happens due to the value in the
1024 * link_cfg_err parameter in the link info structure
1026 static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1028 ice_check_module_power(pf, link_cfg_err);
1029 ice_check_phy_fw_load(pf, link_cfg_err);
1033 * ice_link_event - process the link event
1034 * @pf: PF that the link event is associated with
1035 * @pi: port_info for the port that the link event is associated with
1036 * @link_up: true if the physical link is up and false if it is down
1037 * @link_speed: current link speed received from the link event
1039 * Returns 0 on success and negative on failure
1042 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1045 struct device *dev = ice_pf_to_dev(pf);
1046 struct ice_phy_info *phy_info;
1047 struct ice_vsi *vsi;
1052 phy_info = &pi->phy;
1053 phy_info->link_info_old = phy_info->link_info;
1055 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1056 old_link_speed = phy_info->link_info_old.link_speed;
1058 /* update the link info structures and re-enable link events,
1059 * don't bail on failure due to other book keeping needed
1061 status = ice_update_link_info(pi);
1063 dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1065 ice_aq_str(pi->hw->adminq.sq_last_status));
1067 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1069 /* Check if the link state is up after updating link info, and treat
1070 * this event as an UP event since the link is actually UP now.
1072 if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1075 vsi = ice_get_main_vsi(pf);
1076 if (!vsi || !vsi->port_info)
1079 /* turn off PHY if media was removed */
1080 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1081 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1082 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1083 ice_set_link(vsi, false);
1086 /* if the old link up/down and speed is the same as the new */
1087 if (link_up == old_link && link_speed == old_link_speed)
1090 if (!ice_is_e810(&pf->hw))
1091 ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1093 if (ice_is_dcb_active(pf)) {
1094 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1095 ice_dcb_rebuild(pf);
1098 ice_set_dflt_mib(pf);
1100 ice_vsi_link_event(vsi, link_up);
1101 ice_print_link_msg(vsi, link_up);
1103 ice_vc_notify_link_state(pf);
1109 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1110 * @pf: board private structure
1112 static void ice_watchdog_subtask(struct ice_pf *pf)
1116 /* if interface is down do nothing */
1117 if (test_bit(ICE_DOWN, pf->state) ||
1118 test_bit(ICE_CFG_BUSY, pf->state))
1121 /* make sure we don't do these things too often */
1122 if (time_before(jiffies,
1123 pf->serv_tmr_prev + pf->serv_tmr_period))
1126 pf->serv_tmr_prev = jiffies;
1128 /* Update the stats for active netdevs so the network stack
1129 * can look at updated numbers whenever it cares to
1131 ice_update_pf_stats(pf);
1132 ice_for_each_vsi(pf, i)
1133 if (pf->vsi[i] && pf->vsi[i]->netdev)
1134 ice_update_vsi_stats(pf->vsi[i]);
1138 * ice_init_link_events - enable/initialize link events
1139 * @pi: pointer to the port_info instance
1141 * Returns -EIO on failure, 0 on success
1143 static int ice_init_link_events(struct ice_port_info *pi)
1147 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1148 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1149 ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1151 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1152 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1157 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1158 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1167 * ice_handle_link_event - handle link event via ARQ
1168 * @pf: PF that the link event is associated with
1169 * @event: event structure containing link status info
1172 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1174 struct ice_aqc_get_link_status_data *link_data;
1175 struct ice_port_info *port_info;
1178 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1179 port_info = pf->hw.port_info;
1183 status = ice_link_event(pf, port_info,
1184 !!(link_data->link_info & ICE_AQ_LINK_UP),
1185 le16_to_cpu(link_data->link_speed));
1187 dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1193 enum ice_aq_task_state {
1194 ICE_AQ_TASK_WAITING = 0,
1195 ICE_AQ_TASK_COMPLETE,
1196 ICE_AQ_TASK_CANCELED,
1199 struct ice_aq_task {
1200 struct hlist_node entry;
1203 struct ice_rq_event_info *event;
1204 enum ice_aq_task_state state;
1208 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1209 * @pf: pointer to the PF private structure
1210 * @opcode: the opcode to wait for
1211 * @timeout: how long to wait, in jiffies
1212 * @event: storage for the event info
1214 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1215 * current thread will be put to sleep until the specified event occurs or
1216 * until the given timeout is reached.
1218 * To obtain only the descriptor contents, pass an event without an allocated
1219 * msg_buf. If the complete data buffer is desired, allocate the
1220 * event->msg_buf with enough space ahead of time.
1222 * Returns: zero on success, or a negative error code on failure.
1224 int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1225 struct ice_rq_event_info *event)
1227 struct device *dev = ice_pf_to_dev(pf);
1228 struct ice_aq_task *task;
1229 unsigned long start;
1233 task = kzalloc(sizeof(*task), GFP_KERNEL);
1237 INIT_HLIST_NODE(&task->entry);
1238 task->opcode = opcode;
1239 task->event = event;
1240 task->state = ICE_AQ_TASK_WAITING;
1242 spin_lock_bh(&pf->aq_wait_lock);
1243 hlist_add_head(&task->entry, &pf->aq_wait_list);
1244 spin_unlock_bh(&pf->aq_wait_lock);
1248 ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1250 switch (task->state) {
1251 case ICE_AQ_TASK_WAITING:
1252 err = ret < 0 ? ret : -ETIMEDOUT;
1254 case ICE_AQ_TASK_CANCELED:
1255 err = ret < 0 ? ret : -ECANCELED;
1257 case ICE_AQ_TASK_COMPLETE:
1258 err = ret < 0 ? ret : 0;
1261 WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1266 dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1267 jiffies_to_msecs(jiffies - start),
1268 jiffies_to_msecs(timeout),
1271 spin_lock_bh(&pf->aq_wait_lock);
1272 hlist_del(&task->entry);
1273 spin_unlock_bh(&pf->aq_wait_lock);
1280 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1281 * @pf: pointer to the PF private structure
1282 * @opcode: the opcode of the event
1283 * @event: the event to check
1285 * Loops over the current list of pending threads waiting for an AdminQ event.
1286 * For each matching task, copy the contents of the event into the task
1287 * structure and wake up the thread.
1289 * If multiple threads wait for the same opcode, they will all be woken up.
1291 * Note that event->msg_buf will only be duplicated if the event has a buffer
1292 * with enough space already allocated. Otherwise, only the descriptor and
1293 * message length will be copied.
1295 * Returns: true if an event was found, false otherwise
1297 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1298 struct ice_rq_event_info *event)
1300 struct ice_aq_task *task;
1303 spin_lock_bh(&pf->aq_wait_lock);
1304 hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1305 if (task->state || task->opcode != opcode)
1308 memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1309 task->event->msg_len = event->msg_len;
1311 /* Only copy the data buffer if a destination was set */
1312 if (task->event->msg_buf &&
1313 task->event->buf_len > event->buf_len) {
1314 memcpy(task->event->msg_buf, event->msg_buf,
1316 task->event->buf_len = event->buf_len;
1319 task->state = ICE_AQ_TASK_COMPLETE;
1322 spin_unlock_bh(&pf->aq_wait_lock);
1325 wake_up(&pf->aq_wait_queue);
1329 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1330 * @pf: the PF private structure
1332 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1333 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1335 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1337 struct ice_aq_task *task;
1339 spin_lock_bh(&pf->aq_wait_lock);
1340 hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1341 task->state = ICE_AQ_TASK_CANCELED;
1342 spin_unlock_bh(&pf->aq_wait_lock);
1344 wake_up(&pf->aq_wait_queue);
1348 * __ice_clean_ctrlq - helper function to clean controlq rings
1349 * @pf: ptr to struct ice_pf
1350 * @q_type: specific Control queue type
1352 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1354 struct device *dev = ice_pf_to_dev(pf);
1355 struct ice_rq_event_info event;
1356 struct ice_hw *hw = &pf->hw;
1357 struct ice_ctl_q_info *cq;
1362 /* Do not clean control queue if/when PF reset fails */
1363 if (test_bit(ICE_RESET_FAILED, pf->state))
1367 case ICE_CTL_Q_ADMIN:
1375 case ICE_CTL_Q_MAILBOX:
1378 /* we are going to try to detect a malicious VF, so set the
1379 * state to begin detection
1381 hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1384 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1388 /* check for error indications - PF_xx_AxQLEN register layout for
1389 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1391 val = rd32(hw, cq->rq.len);
1392 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1393 PF_FW_ARQLEN_ARQCRIT_M)) {
1395 if (val & PF_FW_ARQLEN_ARQVFE_M)
1396 dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1398 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1399 dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1402 if (val & PF_FW_ARQLEN_ARQCRIT_M)
1403 dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1405 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1406 PF_FW_ARQLEN_ARQCRIT_M);
1408 wr32(hw, cq->rq.len, val);
1411 val = rd32(hw, cq->sq.len);
1412 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1413 PF_FW_ATQLEN_ATQCRIT_M)) {
1415 if (val & PF_FW_ATQLEN_ATQVFE_M)
1416 dev_dbg(dev, "%s Send Queue VF Error detected\n",
1418 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1419 dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1422 if (val & PF_FW_ATQLEN_ATQCRIT_M)
1423 dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1425 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1426 PF_FW_ATQLEN_ATQCRIT_M);
1428 wr32(hw, cq->sq.len, val);
1431 event.buf_len = cq->rq_buf_size;
1432 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1440 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1441 if (ret == -EALREADY)
1444 dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1449 opcode = le16_to_cpu(event.desc.opcode);
1451 /* Notify any thread that might be waiting for this event */
1452 ice_aq_check_events(pf, opcode, &event);
1455 case ice_aqc_opc_get_link_status:
1456 if (ice_handle_link_event(pf, &event))
1457 dev_err(dev, "Could not handle link event\n");
1459 case ice_aqc_opc_event_lan_overflow:
1460 ice_vf_lan_overflow_event(pf, &event);
1462 case ice_mbx_opc_send_msg_to_pf:
1463 if (!ice_is_malicious_vf(pf, &event, i, pending))
1464 ice_vc_process_vf_msg(pf, &event);
1466 case ice_aqc_opc_fw_logging:
1467 ice_output_fw_log(hw, &event.desc, event.msg_buf);
1469 case ice_aqc_opc_lldp_set_mib_change:
1470 ice_dcb_process_lldp_set_mib_change(pf, &event);
1473 dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1477 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1479 kfree(event.msg_buf);
1481 return pending && (i == ICE_DFLT_IRQ_WORK);
1485 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1486 * @hw: pointer to hardware info
1487 * @cq: control queue information
1489 * returns true if there are pending messages in a queue, false if there aren't
1491 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1495 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1496 return cq->rq.next_to_clean != ntu;
1500 * ice_clean_adminq_subtask - clean the AdminQ rings
1501 * @pf: board private structure
1503 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1505 struct ice_hw *hw = &pf->hw;
1507 if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1510 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1513 clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1515 /* There might be a situation where new messages arrive to a control
1516 * queue between processing the last message and clearing the
1517 * EVENT_PENDING bit. So before exiting, check queue head again (using
1518 * ice_ctrlq_pending) and process new messages if any.
1520 if (ice_ctrlq_pending(hw, &hw->adminq))
1521 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1527 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1528 * @pf: board private structure
1530 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1532 struct ice_hw *hw = &pf->hw;
1534 if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1537 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1540 clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1542 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1543 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1549 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1550 * @pf: board private structure
1552 static void ice_clean_sbq_subtask(struct ice_pf *pf)
1554 struct ice_hw *hw = &pf->hw;
1556 /* Nothing to do here if sideband queue is not supported */
1557 if (!ice_is_sbq_supported(hw)) {
1558 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1562 if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1565 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1568 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1570 if (ice_ctrlq_pending(hw, &hw->sbq))
1571 __ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1577 * ice_service_task_schedule - schedule the service task to wake up
1578 * @pf: board private structure
1580 * If not already scheduled, this puts the task into the work queue.
1582 void ice_service_task_schedule(struct ice_pf *pf)
1584 if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1585 !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1586 !test_bit(ICE_NEEDS_RESTART, pf->state))
1587 queue_work(ice_wq, &pf->serv_task);
1591 * ice_service_task_complete - finish up the service task
1592 * @pf: board private structure
1594 static void ice_service_task_complete(struct ice_pf *pf)
1596 WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1598 /* force memory (pf->state) to sync before next service task */
1599 smp_mb__before_atomic();
1600 clear_bit(ICE_SERVICE_SCHED, pf->state);
1604 * ice_service_task_stop - stop service task and cancel works
1605 * @pf: board private structure
1607 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1610 static int ice_service_task_stop(struct ice_pf *pf)
1614 ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1616 if (pf->serv_tmr.function)
1617 del_timer_sync(&pf->serv_tmr);
1618 if (pf->serv_task.func)
1619 cancel_work_sync(&pf->serv_task);
1621 clear_bit(ICE_SERVICE_SCHED, pf->state);
1626 * ice_service_task_restart - restart service task and schedule works
1627 * @pf: board private structure
1629 * This function is needed for suspend and resume works (e.g WoL scenario)
1631 static void ice_service_task_restart(struct ice_pf *pf)
1633 clear_bit(ICE_SERVICE_DIS, pf->state);
1634 ice_service_task_schedule(pf);
1638 * ice_service_timer - timer callback to schedule service task
1639 * @t: pointer to timer_list
1641 static void ice_service_timer(struct timer_list *t)
1643 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1645 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1646 ice_service_task_schedule(pf);
1650 * ice_handle_mdd_event - handle malicious driver detect event
1651 * @pf: pointer to the PF structure
1653 * Called from service task. OICR interrupt handler indicates MDD event.
1654 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1655 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1656 * disable the queue, the PF can be configured to reset the VF using ethtool
1657 * private flag mdd-auto-reset-vf.
1659 static void ice_handle_mdd_event(struct ice_pf *pf)
1661 struct device *dev = ice_pf_to_dev(pf);
1662 struct ice_hw *hw = &pf->hw;
1666 if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1667 /* Since the VF MDD event logging is rate limited, check if
1668 * there are pending MDD events.
1670 ice_print_vfs_mdd_events(pf);
1674 /* find what triggered an MDD event */
1675 reg = rd32(hw, GL_MDET_TX_PQM);
1676 if (reg & GL_MDET_TX_PQM_VALID_M) {
1677 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1678 GL_MDET_TX_PQM_PF_NUM_S;
1679 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1680 GL_MDET_TX_PQM_VF_NUM_S;
1681 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1682 GL_MDET_TX_PQM_MAL_TYPE_S;
1683 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1684 GL_MDET_TX_PQM_QNUM_S);
1686 if (netif_msg_tx_err(pf))
1687 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1688 event, queue, pf_num, vf_num);
1689 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1692 reg = rd32(hw, GL_MDET_TX_TCLAN);
1693 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1694 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1695 GL_MDET_TX_TCLAN_PF_NUM_S;
1696 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1697 GL_MDET_TX_TCLAN_VF_NUM_S;
1698 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1699 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1700 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1701 GL_MDET_TX_TCLAN_QNUM_S);
1703 if (netif_msg_tx_err(pf))
1704 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1705 event, queue, pf_num, vf_num);
1706 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1709 reg = rd32(hw, GL_MDET_RX);
1710 if (reg & GL_MDET_RX_VALID_M) {
1711 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1712 GL_MDET_RX_PF_NUM_S;
1713 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1714 GL_MDET_RX_VF_NUM_S;
1715 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1716 GL_MDET_RX_MAL_TYPE_S;
1717 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1720 if (netif_msg_rx_err(pf))
1721 dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1722 event, queue, pf_num, vf_num);
1723 wr32(hw, GL_MDET_RX, 0xffffffff);
1726 /* check to see if this PF caused an MDD event */
1727 reg = rd32(hw, PF_MDET_TX_PQM);
1728 if (reg & PF_MDET_TX_PQM_VALID_M) {
1729 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1730 if (netif_msg_tx_err(pf))
1731 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1734 reg = rd32(hw, PF_MDET_TX_TCLAN);
1735 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1736 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1737 if (netif_msg_tx_err(pf))
1738 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1741 reg = rd32(hw, PF_MDET_RX);
1742 if (reg & PF_MDET_RX_VALID_M) {
1743 wr32(hw, PF_MDET_RX, 0xFFFF);
1744 if (netif_msg_rx_err(pf))
1745 dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1748 /* Check to see if one of the VFs caused an MDD event, and then
1749 * increment counters and set print pending
1751 ice_for_each_vf(pf, i) {
1752 struct ice_vf *vf = &pf->vf[i];
1754 reg = rd32(hw, VP_MDET_TX_PQM(i));
1755 if (reg & VP_MDET_TX_PQM_VALID_M) {
1756 wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1757 vf->mdd_tx_events.count++;
1758 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1759 if (netif_msg_tx_err(pf))
1760 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1764 reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1765 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1766 wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1767 vf->mdd_tx_events.count++;
1768 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1769 if (netif_msg_tx_err(pf))
1770 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1774 reg = rd32(hw, VP_MDET_TX_TDPU(i));
1775 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1776 wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1777 vf->mdd_tx_events.count++;
1778 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1779 if (netif_msg_tx_err(pf))
1780 dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1784 reg = rd32(hw, VP_MDET_RX(i));
1785 if (reg & VP_MDET_RX_VALID_M) {
1786 wr32(hw, VP_MDET_RX(i), 0xFFFF);
1787 vf->mdd_rx_events.count++;
1788 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1789 if (netif_msg_rx_err(pf))
1790 dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1793 /* Since the queue is disabled on VF Rx MDD events, the
1794 * PF can be configured to reset the VF through ethtool
1795 * private flag mdd-auto-reset-vf.
1797 if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1798 /* VF MDD event counters will be cleared by
1799 * reset, so print the event prior to reset.
1801 ice_print_vf_rx_mdd_event(vf);
1802 mutex_lock(&pf->vf[i].cfg_lock);
1803 ice_reset_vf(&pf->vf[i], false);
1804 mutex_unlock(&pf->vf[i].cfg_lock);
1809 ice_print_vfs_mdd_events(pf);
1813 * ice_force_phys_link_state - Force the physical link state
1814 * @vsi: VSI to force the physical link state to up/down
1815 * @link_up: true/false indicates to set the physical link to up/down
1817 * Force the physical link state by getting the current PHY capabilities from
1818 * hardware and setting the PHY config based on the determined capabilities. If
1819 * link changes a link event will be triggered because both the Enable Automatic
1820 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1822 * Returns 0 on success, negative on failure
1824 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1826 struct ice_aqc_get_phy_caps_data *pcaps;
1827 struct ice_aqc_set_phy_cfg_data *cfg;
1828 struct ice_port_info *pi;
1832 if (!vsi || !vsi->port_info || !vsi->back)
1834 if (vsi->type != ICE_VSI_PF)
1837 dev = ice_pf_to_dev(vsi->back);
1839 pi = vsi->port_info;
1841 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1845 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1848 dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1849 vsi->vsi_num, retcode);
1854 /* No change in link */
1855 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1856 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1859 /* Use the current user PHY configuration. The current user PHY
1860 * configuration is initialized during probe from PHY capabilities
1861 * software mode, and updated on set PHY configuration.
1863 cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1869 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1871 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1873 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1875 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1877 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1878 vsi->vsi_num, retcode);
1889 * ice_init_nvm_phy_type - Initialize the NVM PHY type
1890 * @pi: port info structure
1892 * Initialize nvm_phy_type_[low|high] for link lenient mode support
1894 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1896 struct ice_aqc_get_phy_caps_data *pcaps;
1897 struct ice_pf *pf = pi->hw->back;
1900 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1904 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
1908 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1912 pf->nvm_phy_type_hi = pcaps->phy_type_high;
1913 pf->nvm_phy_type_lo = pcaps->phy_type_low;
1921 * ice_init_link_dflt_override - Initialize link default override
1922 * @pi: port info structure
1924 * Initialize link default override and PHY total port shutdown during probe
1926 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1928 struct ice_link_default_override_tlv *ldo;
1929 struct ice_pf *pf = pi->hw->back;
1931 ldo = &pf->link_dflt_override;
1932 if (ice_get_link_default_override(ldo, pi))
1935 if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1938 /* Enable Total Port Shutdown (override/replace link-down-on-close
1939 * ethtool private flag) for ports with Port Disable bit set.
1941 set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1942 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1946 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1947 * @pi: port info structure
1949 * If default override is enabled, initialize the user PHY cfg speed and FEC
1950 * settings using the default override mask from the NVM.
1952 * The PHY should only be configured with the default override settings the
1953 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
1954 * is used to indicate that the user PHY cfg default override is initialized
1955 * and the PHY has not been configured with the default override settings. The
1956 * state is set here, and cleared in ice_configure_phy the first time the PHY is
1959 * This function should be called only if the FW doesn't support default
1960 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
1962 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
1964 struct ice_link_default_override_tlv *ldo;
1965 struct ice_aqc_set_phy_cfg_data *cfg;
1966 struct ice_phy_info *phy = &pi->phy;
1967 struct ice_pf *pf = pi->hw->back;
1969 ldo = &pf->link_dflt_override;
1971 /* If link default override is enabled, use to mask NVM PHY capabilities
1972 * for speed and FEC default configuration.
1974 cfg = &phy->curr_user_phy_cfg;
1976 if (ldo->phy_type_low || ldo->phy_type_high) {
1977 cfg->phy_type_low = pf->nvm_phy_type_lo &
1978 cpu_to_le64(ldo->phy_type_low);
1979 cfg->phy_type_high = pf->nvm_phy_type_hi &
1980 cpu_to_le64(ldo->phy_type_high);
1982 cfg->link_fec_opt = ldo->fec_options;
1983 phy->curr_user_fec_req = ICE_FEC_AUTO;
1985 set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
1989 * ice_init_phy_user_cfg - Initialize the PHY user configuration
1990 * @pi: port info structure
1992 * Initialize the current user PHY configuration, speed, FEC, and FC requested
1993 * mode to default. The PHY defaults are from get PHY capabilities topology
1994 * with media so call when media is first available. An error is returned if
1995 * called when media is not available. The PHY initialization completed state is
1998 * These configurations are used when setting PHY
1999 * configuration. The user PHY configuration is updated on set PHY
2000 * configuration. Returns 0 on success, negative on failure
2002 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2004 struct ice_aqc_get_phy_caps_data *pcaps;
2005 struct ice_phy_info *phy = &pi->phy;
2006 struct ice_pf *pf = pi->hw->back;
2009 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2012 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2016 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2017 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2020 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2023 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2027 ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2029 /* check if lenient mode is supported and enabled */
2030 if (ice_fw_supports_link_override(pi->hw) &&
2031 !(pcaps->module_compliance_enforcement &
2032 ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2033 set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2035 /* if the FW supports default PHY configuration mode, then the driver
2036 * does not have to apply link override settings. If not,
2037 * initialize user PHY configuration with link override values
2039 if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2040 (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2041 ice_init_phy_cfg_dflt_override(pi);
2046 /* if link default override is not enabled, set user flow control and
2047 * FEC settings based on what get_phy_caps returned
2049 phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2050 pcaps->link_fec_options);
2051 phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2054 phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2055 set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2062 * ice_configure_phy - configure PHY
2065 * Set the PHY configuration. If the current PHY configuration is the same as
2066 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2067 * configure the based get PHY capabilities for topology with media.
2069 static int ice_configure_phy(struct ice_vsi *vsi)
2071 struct device *dev = ice_pf_to_dev(vsi->back);
2072 struct ice_port_info *pi = vsi->port_info;
2073 struct ice_aqc_get_phy_caps_data *pcaps;
2074 struct ice_aqc_set_phy_cfg_data *cfg;
2075 struct ice_phy_info *phy = &pi->phy;
2076 struct ice_pf *pf = vsi->back;
2079 /* Ensure we have media as we cannot configure a medialess port */
2080 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2083 ice_print_topo_conflict(vsi);
2085 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2086 phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2089 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2090 return ice_force_phys_link_state(vsi, true);
2092 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2096 /* Get current PHY config */
2097 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2100 dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2105 /* If PHY enable link is configured and configuration has not changed,
2106 * there's nothing to do
2108 if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2109 ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2112 /* Use PHY topology as baseline for configuration */
2113 memset(pcaps, 0, sizeof(*pcaps));
2114 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2115 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2118 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2121 dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2126 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2132 ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2134 /* Speed - If default override pending, use curr_user_phy_cfg set in
2135 * ice_init_phy_user_cfg_ldo.
2137 if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2138 vsi->back->state)) {
2139 cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2140 cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2142 u64 phy_low = 0, phy_high = 0;
2144 ice_update_phy_type(&phy_low, &phy_high,
2145 pi->phy.curr_user_speed_req);
2146 cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2147 cfg->phy_type_high = pcaps->phy_type_high &
2148 cpu_to_le64(phy_high);
2151 /* Can't provide what was requested; use PHY capabilities */
2152 if (!cfg->phy_type_low && !cfg->phy_type_high) {
2153 cfg->phy_type_low = pcaps->phy_type_low;
2154 cfg->phy_type_high = pcaps->phy_type_high;
2158 ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2160 /* Can't provide what was requested; use PHY capabilities */
2161 if (cfg->link_fec_opt !=
2162 (cfg->link_fec_opt & pcaps->link_fec_options)) {
2163 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2164 cfg->link_fec_opt = pcaps->link_fec_options;
2167 /* Flow Control - always supported; no need to check against
2170 ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2172 /* Enable link and link update */
2173 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2175 err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2177 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2187 * ice_check_media_subtask - Check for media
2188 * @pf: pointer to PF struct
2190 * If media is available, then initialize PHY user configuration if it is not
2191 * been, and configure the PHY if the interface is up.
2193 static void ice_check_media_subtask(struct ice_pf *pf)
2195 struct ice_port_info *pi;
2196 struct ice_vsi *vsi;
2199 /* No need to check for media if it's already present */
2200 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2203 vsi = ice_get_main_vsi(pf);
2207 /* Refresh link info and check if media is present */
2208 pi = vsi->port_info;
2209 err = ice_update_link_info(pi);
2213 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2215 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2216 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2217 ice_init_phy_user_cfg(pi);
2219 /* PHY settings are reset on media insertion, reconfigure
2220 * PHY to preserve settings.
2222 if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2223 test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2226 err = ice_configure_phy(vsi);
2228 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2230 /* A Link Status Event will be generated; the event handler
2231 * will complete bringing the interface up
2237 * ice_service_task - manage and run subtasks
2238 * @work: pointer to work_struct contained by the PF struct
2240 static void ice_service_task(struct work_struct *work)
2242 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2243 unsigned long start_time = jiffies;
2247 /* process reset requests first */
2248 ice_reset_subtask(pf);
2250 /* bail if a reset/recovery cycle is pending or rebuild failed */
2251 if (ice_is_reset_in_progress(pf->state) ||
2252 test_bit(ICE_SUSPENDED, pf->state) ||
2253 test_bit(ICE_NEEDS_RESTART, pf->state)) {
2254 ice_service_task_complete(pf);
2258 if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2259 ice_plug_aux_dev(pf);
2261 ice_clean_adminq_subtask(pf);
2262 ice_check_media_subtask(pf);
2263 ice_check_for_hang_subtask(pf);
2264 ice_sync_fltr_subtask(pf);
2265 ice_handle_mdd_event(pf);
2266 ice_watchdog_subtask(pf);
2268 if (ice_is_safe_mode(pf)) {
2269 ice_service_task_complete(pf);
2273 ice_process_vflr_event(pf);
2274 ice_clean_mailboxq_subtask(pf);
2275 ice_clean_sbq_subtask(pf);
2276 ice_sync_arfs_fltrs(pf);
2277 ice_flush_fdir_ctx(pf);
2279 /* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2280 ice_service_task_complete(pf);
2282 /* If the tasks have taken longer than one service timer period
2283 * or there is more work to be done, reset the service timer to
2284 * schedule the service task now.
2286 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2287 test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2288 test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2289 test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2290 test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2291 test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2292 test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2293 mod_timer(&pf->serv_tmr, jiffies);
2297 * ice_set_ctrlq_len - helper function to set controlq length
2298 * @hw: pointer to the HW instance
2300 static void ice_set_ctrlq_len(struct ice_hw *hw)
2302 hw->adminq.num_rq_entries = ICE_AQ_LEN;
2303 hw->adminq.num_sq_entries = ICE_AQ_LEN;
2304 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2305 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2306 hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2307 hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2308 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2309 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2310 hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2311 hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2312 hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2313 hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2317 * ice_schedule_reset - schedule a reset
2318 * @pf: board private structure
2319 * @reset: reset being requested
2321 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2323 struct device *dev = ice_pf_to_dev(pf);
2325 /* bail out if earlier reset has failed */
2326 if (test_bit(ICE_RESET_FAILED, pf->state)) {
2327 dev_dbg(dev, "earlier reset has failed\n");
2330 /* bail if reset/recovery already in progress */
2331 if (ice_is_reset_in_progress(pf->state)) {
2332 dev_dbg(dev, "Reset already in progress\n");
2336 ice_unplug_aux_dev(pf);
2340 set_bit(ICE_PFR_REQ, pf->state);
2342 case ICE_RESET_CORER:
2343 set_bit(ICE_CORER_REQ, pf->state);
2345 case ICE_RESET_GLOBR:
2346 set_bit(ICE_GLOBR_REQ, pf->state);
2352 ice_service_task_schedule(pf);
2357 * ice_irq_affinity_notify - Callback for affinity changes
2358 * @notify: context as to what irq was changed
2359 * @mask: the new affinity mask
2361 * This is a callback function used by the irq_set_affinity_notifier function
2362 * so that we may register to receive changes to the irq affinity masks.
2365 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2366 const cpumask_t *mask)
2368 struct ice_q_vector *q_vector =
2369 container_of(notify, struct ice_q_vector, affinity_notify);
2371 cpumask_copy(&q_vector->affinity_mask, mask);
2375 * ice_irq_affinity_release - Callback for affinity notifier release
2376 * @ref: internal core kernel usage
2378 * This is a callback function used by the irq_set_affinity_notifier function
2379 * to inform the current notification subscriber that they will no longer
2380 * receive notifications.
2382 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2385 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2386 * @vsi: the VSI being configured
2388 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2390 struct ice_hw *hw = &vsi->back->hw;
2393 ice_for_each_q_vector(vsi, i)
2394 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2401 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2402 * @vsi: the VSI being configured
2403 * @basename: name for the vector
2405 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2407 int q_vectors = vsi->num_q_vectors;
2408 struct ice_pf *pf = vsi->back;
2409 int base = vsi->base_vector;
2416 dev = ice_pf_to_dev(pf);
2417 for (vector = 0; vector < q_vectors; vector++) {
2418 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2420 irq_num = pf->msix_entries[base + vector].vector;
2422 if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2423 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2424 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2426 } else if (q_vector->rx.rx_ring) {
2427 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2428 "%s-%s-%d", basename, "rx", rx_int_idx++);
2429 } else if (q_vector->tx.tx_ring) {
2430 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2431 "%s-%s-%d", basename, "tx", tx_int_idx++);
2433 /* skip this unused q_vector */
2436 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID)
2437 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2438 IRQF_SHARED, q_vector->name,
2441 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2442 0, q_vector->name, q_vector);
2444 netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2449 /* register for affinity change notifications */
2450 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2451 struct irq_affinity_notify *affinity_notify;
2453 affinity_notify = &q_vector->affinity_notify;
2454 affinity_notify->notify = ice_irq_affinity_notify;
2455 affinity_notify->release = ice_irq_affinity_release;
2456 irq_set_affinity_notifier(irq_num, affinity_notify);
2459 /* assign the mask for this irq */
2460 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2463 vsi->irqs_ready = true;
2469 irq_num = pf->msix_entries[base + vector].vector;
2470 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2471 irq_set_affinity_notifier(irq_num, NULL);
2472 irq_set_affinity_hint(irq_num, NULL);
2473 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2479 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2480 * @vsi: VSI to setup Tx rings used by XDP
2482 * Return 0 on success and negative value on error
2484 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2486 struct device *dev = ice_pf_to_dev(vsi->back);
2487 struct ice_tx_desc *tx_desc;
2490 ice_for_each_xdp_txq(vsi, i) {
2491 u16 xdp_q_idx = vsi->alloc_txq + i;
2492 struct ice_tx_ring *xdp_ring;
2494 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2497 goto free_xdp_rings;
2499 xdp_ring->q_index = xdp_q_idx;
2500 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2501 xdp_ring->vsi = vsi;
2502 xdp_ring->netdev = NULL;
2503 xdp_ring->next_dd = ICE_TX_THRESH - 1;
2504 xdp_ring->next_rs = ICE_TX_THRESH - 1;
2505 xdp_ring->dev = dev;
2506 xdp_ring->count = vsi->num_tx_desc;
2507 WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2508 if (ice_setup_tx_ring(xdp_ring))
2509 goto free_xdp_rings;
2510 ice_set_ring_xdp(xdp_ring);
2511 xdp_ring->xsk_pool = ice_tx_xsk_pool(xdp_ring);
2512 spin_lock_init(&xdp_ring->tx_lock);
2513 for (j = 0; j < xdp_ring->count; j++) {
2514 tx_desc = ICE_TX_DESC(xdp_ring, j);
2515 tx_desc->cmd_type_offset_bsz = cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE);
2519 ice_for_each_rxq(vsi, i) {
2520 if (static_key_enabled(&ice_xdp_locking_key))
2521 vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2523 vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i];
2530 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2531 ice_free_tx_ring(vsi->xdp_rings[i]);
2536 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2537 * @vsi: VSI to set the bpf prog on
2538 * @prog: the bpf prog pointer
2540 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2542 struct bpf_prog *old_prog;
2545 old_prog = xchg(&vsi->xdp_prog, prog);
2547 bpf_prog_put(old_prog);
2549 ice_for_each_rxq(vsi, i)
2550 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2554 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2555 * @vsi: VSI to bring up Tx rings used by XDP
2556 * @prog: bpf program that will be assigned to VSI
2558 * Return 0 on success and negative value on error
2560 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2562 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2563 int xdp_rings_rem = vsi->num_xdp_txq;
2564 struct ice_pf *pf = vsi->back;
2565 struct ice_qs_cfg xdp_qs_cfg = {
2566 .qs_mutex = &pf->avail_q_mutex,
2567 .pf_map = pf->avail_txqs,
2568 .pf_map_size = pf->max_pf_txqs,
2569 .q_count = vsi->num_xdp_txq,
2570 .scatter_count = ICE_MAX_SCATTER_TXQS,
2571 .vsi_map = vsi->txq_map,
2572 .vsi_map_offset = vsi->alloc_txq,
2573 .mapping_mode = ICE_VSI_MAP_CONTIG
2579 dev = ice_pf_to_dev(pf);
2580 vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2581 sizeof(*vsi->xdp_rings), GFP_KERNEL);
2582 if (!vsi->xdp_rings)
2585 vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2586 if (__ice_vsi_get_qs(&xdp_qs_cfg))
2589 if (static_key_enabled(&ice_xdp_locking_key))
2590 netdev_warn(vsi->netdev,
2591 "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2593 if (ice_xdp_alloc_setup_rings(vsi))
2594 goto clear_xdp_rings;
2596 /* follow the logic from ice_vsi_map_rings_to_vectors */
2597 ice_for_each_q_vector(vsi, v_idx) {
2598 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2599 int xdp_rings_per_v, q_id, q_base;
2601 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2602 vsi->num_q_vectors - v_idx);
2603 q_base = vsi->num_xdp_txq - xdp_rings_rem;
2605 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2606 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2608 xdp_ring->q_vector = q_vector;
2609 xdp_ring->next = q_vector->tx.tx_ring;
2610 q_vector->tx.tx_ring = xdp_ring;
2612 xdp_rings_rem -= xdp_rings_per_v;
2615 /* omit the scheduler update if in reset path; XDP queues will be
2616 * taken into account at the end of ice_vsi_rebuild, where
2617 * ice_cfg_vsi_lan is being called
2619 if (ice_is_reset_in_progress(pf->state))
2622 /* tell the Tx scheduler that right now we have
2625 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2626 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2628 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2631 dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2633 goto clear_xdp_rings;
2636 /* assign the prog only when it's not already present on VSI;
2637 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2638 * VSI rebuild that happens under ethtool -L can expose us to
2639 * the bpf_prog refcount issues as we would be swapping same
2640 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2641 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2642 * this is not harmful as dev_xdp_install bumps the refcount
2643 * before calling the op exposed by the driver;
2645 if (!ice_is_xdp_ena_vsi(vsi))
2646 ice_vsi_assign_bpf_prog(vsi, prog);
2650 ice_for_each_xdp_txq(vsi, i)
2651 if (vsi->xdp_rings[i]) {
2652 kfree_rcu(vsi->xdp_rings[i], rcu);
2653 vsi->xdp_rings[i] = NULL;
2657 mutex_lock(&pf->avail_q_mutex);
2658 ice_for_each_xdp_txq(vsi, i) {
2659 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2660 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2662 mutex_unlock(&pf->avail_q_mutex);
2664 devm_kfree(dev, vsi->xdp_rings);
2669 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2670 * @vsi: VSI to remove XDP rings
2672 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2675 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2677 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2678 struct ice_pf *pf = vsi->back;
2681 /* q_vectors are freed in reset path so there's no point in detaching
2682 * rings; in case of rebuild being triggered not from reset bits
2683 * in pf->state won't be set, so additionally check first q_vector
2686 if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2689 ice_for_each_q_vector(vsi, v_idx) {
2690 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2691 struct ice_tx_ring *ring;
2693 ice_for_each_tx_ring(ring, q_vector->tx)
2694 if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2697 /* restore the value of last node prior to XDP setup */
2698 q_vector->tx.tx_ring = ring;
2702 mutex_lock(&pf->avail_q_mutex);
2703 ice_for_each_xdp_txq(vsi, i) {
2704 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2705 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2707 mutex_unlock(&pf->avail_q_mutex);
2709 ice_for_each_xdp_txq(vsi, i)
2710 if (vsi->xdp_rings[i]) {
2711 if (vsi->xdp_rings[i]->desc)
2712 ice_free_tx_ring(vsi->xdp_rings[i]);
2713 kfree_rcu(vsi->xdp_rings[i], rcu);
2714 vsi->xdp_rings[i] = NULL;
2717 devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2718 vsi->xdp_rings = NULL;
2720 if (static_key_enabled(&ice_xdp_locking_key))
2721 static_branch_dec(&ice_xdp_locking_key);
2723 if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2726 ice_vsi_assign_bpf_prog(vsi, NULL);
2728 /* notify Tx scheduler that we destroyed XDP queues and bring
2729 * back the old number of child nodes
2731 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2732 max_txqs[i] = vsi->num_txq;
2734 /* change number of XDP Tx queues to 0 */
2735 vsi->num_xdp_txq = 0;
2737 return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2742 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2743 * @vsi: VSI to schedule napi on
2745 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2749 ice_for_each_rxq(vsi, i) {
2750 struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2752 if (rx_ring->xsk_pool)
2753 napi_schedule(&rx_ring->q_vector->napi);
2758 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2759 * @vsi: VSI to determine the count of XDP Tx qs
2761 * returns 0 if Tx qs count is higher than at least half of CPU count,
2764 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2766 u16 avail = ice_get_avail_txq_count(vsi->back);
2767 u16 cpus = num_possible_cpus();
2769 if (avail < cpus / 2)
2772 vsi->num_xdp_txq = min_t(u16, avail, cpus);
2774 if (vsi->num_xdp_txq < cpus)
2775 static_branch_inc(&ice_xdp_locking_key);
2781 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2782 * @vsi: VSI to setup XDP for
2783 * @prog: XDP program
2784 * @extack: netlink extended ack
2787 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2788 struct netlink_ext_ack *extack)
2790 int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2791 bool if_running = netif_running(vsi->netdev);
2792 int ret = 0, xdp_ring_err = 0;
2794 if (frame_size > vsi->rx_buf_len) {
2795 NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2799 /* need to stop netdev while setting up the program for Rx rings */
2800 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2801 ret = ice_down(vsi);
2803 NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2808 if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2809 xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2811 NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2813 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2815 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2817 } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2818 xdp_ring_err = ice_destroy_xdp_rings(vsi);
2820 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2822 /* safe to call even when prog == vsi->xdp_prog as
2823 * dev_xdp_install in net/core/dev.c incremented prog's
2824 * refcount so corresponding bpf_prog_put won't cause
2827 ice_vsi_assign_bpf_prog(vsi, prog);
2834 ice_vsi_rx_napi_schedule(vsi);
2836 return (ret || xdp_ring_err) ? -ENOMEM : 0;
2840 * ice_xdp_safe_mode - XDP handler for safe mode
2844 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2845 struct netdev_bpf *xdp)
2847 NL_SET_ERR_MSG_MOD(xdp->extack,
2848 "Please provide working DDP firmware package in order to use XDP\n"
2849 "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
2854 * ice_xdp - implements XDP handler
2858 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2860 struct ice_netdev_priv *np = netdev_priv(dev);
2861 struct ice_vsi *vsi = np->vsi;
2863 if (vsi->type != ICE_VSI_PF) {
2864 NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2868 switch (xdp->command) {
2869 case XDP_SETUP_PROG:
2870 return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2871 case XDP_SETUP_XSK_POOL:
2872 return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2880 * ice_ena_misc_vector - enable the non-queue interrupts
2881 * @pf: board private structure
2883 static void ice_ena_misc_vector(struct ice_pf *pf)
2885 struct ice_hw *hw = &pf->hw;
2888 /* Disable anti-spoof detection interrupt to prevent spurious event
2889 * interrupts during a function reset. Anti-spoof functionally is
2892 val = rd32(hw, GL_MDCK_TX_TDPU);
2893 val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2894 wr32(hw, GL_MDCK_TX_TDPU, val);
2896 /* clear things first */
2897 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
2898 rd32(hw, PFINT_OICR); /* read to clear */
2900 val = (PFINT_OICR_ECC_ERR_M |
2901 PFINT_OICR_MAL_DETECT_M |
2903 PFINT_OICR_PCI_EXCEPTION_M |
2905 PFINT_OICR_HMC_ERR_M |
2906 PFINT_OICR_PE_PUSH_M |
2907 PFINT_OICR_PE_CRITERR_M);
2909 wr32(hw, PFINT_OICR_ENA, val);
2911 /* SW_ITR_IDX = 0, but don't change INTENA */
2912 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2913 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2917 * ice_misc_intr - misc interrupt handler
2918 * @irq: interrupt number
2919 * @data: pointer to a q_vector
2921 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2923 struct ice_pf *pf = (struct ice_pf *)data;
2924 struct ice_hw *hw = &pf->hw;
2925 irqreturn_t ret = IRQ_NONE;
2929 dev = ice_pf_to_dev(pf);
2930 set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
2931 set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2932 set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
2934 oicr = rd32(hw, PFINT_OICR);
2935 ena_mask = rd32(hw, PFINT_OICR_ENA);
2937 if (oicr & PFINT_OICR_SWINT_M) {
2938 ena_mask &= ~PFINT_OICR_SWINT_M;
2942 if (oicr & PFINT_OICR_MAL_DETECT_M) {
2943 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2944 set_bit(ICE_MDD_EVENT_PENDING, pf->state);
2946 if (oicr & PFINT_OICR_VFLR_M) {
2947 /* disable any further VFLR event notifications */
2948 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
2949 u32 reg = rd32(hw, PFINT_OICR_ENA);
2951 reg &= ~PFINT_OICR_VFLR_M;
2952 wr32(hw, PFINT_OICR_ENA, reg);
2954 ena_mask &= ~PFINT_OICR_VFLR_M;
2955 set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
2959 if (oicr & PFINT_OICR_GRST_M) {
2962 /* we have a reset warning */
2963 ena_mask &= ~PFINT_OICR_GRST_M;
2964 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2965 GLGEN_RSTAT_RESET_TYPE_S;
2967 if (reset == ICE_RESET_CORER)
2969 else if (reset == ICE_RESET_GLOBR)
2971 else if (reset == ICE_RESET_EMPR)
2974 dev_dbg(dev, "Invalid reset type %d\n", reset);
2976 /* If a reset cycle isn't already in progress, we set a bit in
2977 * pf->state so that the service task can start a reset/rebuild.
2979 if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
2980 if (reset == ICE_RESET_CORER)
2981 set_bit(ICE_CORER_RECV, pf->state);
2982 else if (reset == ICE_RESET_GLOBR)
2983 set_bit(ICE_GLOBR_RECV, pf->state);
2985 set_bit(ICE_EMPR_RECV, pf->state);
2987 /* There are couple of different bits at play here.
2988 * hw->reset_ongoing indicates whether the hardware is
2989 * in reset. This is set to true when a reset interrupt
2990 * is received and set back to false after the driver
2991 * has determined that the hardware is out of reset.
2993 * ICE_RESET_OICR_RECV in pf->state indicates
2994 * that a post reset rebuild is required before the
2995 * driver is operational again. This is set above.
2997 * As this is the start of the reset/rebuild cycle, set
2998 * both to indicate that.
3000 hw->reset_ongoing = true;
3004 if (oicr & PFINT_OICR_TSYN_TX_M) {
3005 ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3006 ice_ptp_process_ts(pf);
3009 if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3010 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3011 u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3013 /* Save EVENTs from GTSYN register */
3014 pf->ptp.ext_ts_irq |= gltsyn_stat & (GLTSYN_STAT_EVENT0_M |
3015 GLTSYN_STAT_EVENT1_M |
3016 GLTSYN_STAT_EVENT2_M);
3017 ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3018 kthread_queue_work(pf->ptp.kworker, &pf->ptp.extts_work);
3021 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3022 if (oicr & ICE_AUX_CRIT_ERR) {
3023 struct iidc_event *event;
3025 ena_mask &= ~ICE_AUX_CRIT_ERR;
3026 event = kzalloc(sizeof(*event), GFP_KERNEL);
3028 set_bit(IIDC_EVENT_CRIT_ERR, event->type);
3029 /* report the entire OICR value to AUX driver */
3031 ice_send_event_to_aux(pf, event);
3036 /* Report any remaining unexpected interrupts */
3039 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3040 /* If a critical error is pending there is no choice but to
3043 if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3044 PFINT_OICR_ECC_ERR_M)) {
3045 set_bit(ICE_PFR_REQ, pf->state);
3046 ice_service_task_schedule(pf);
3051 ice_service_task_schedule(pf);
3052 ice_irq_dynamic_ena(hw, NULL, NULL);
3058 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3059 * @hw: pointer to HW structure
3061 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3063 /* disable Admin queue Interrupt causes */
3064 wr32(hw, PFINT_FW_CTL,
3065 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3067 /* disable Mailbox queue Interrupt causes */
3068 wr32(hw, PFINT_MBX_CTL,
3069 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3071 wr32(hw, PFINT_SB_CTL,
3072 rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3074 /* disable Control queue Interrupt causes */
3075 wr32(hw, PFINT_OICR_CTL,
3076 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3082 * ice_free_irq_msix_misc - Unroll misc vector setup
3083 * @pf: board private structure
3085 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3087 struct ice_hw *hw = &pf->hw;
3089 ice_dis_ctrlq_interrupts(hw);
3091 /* disable OICR interrupt */
3092 wr32(hw, PFINT_OICR_ENA, 0);
3095 if (pf->msix_entries) {
3096 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
3097 devm_free_irq(ice_pf_to_dev(pf),
3098 pf->msix_entries[pf->oicr_idx].vector, pf);
3101 pf->num_avail_sw_msix += 1;
3102 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
3106 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3107 * @hw: pointer to HW structure
3108 * @reg_idx: HW vector index to associate the control queue interrupts with
3110 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3114 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3115 PFINT_OICR_CTL_CAUSE_ENA_M);
3116 wr32(hw, PFINT_OICR_CTL, val);
3118 /* enable Admin queue Interrupt causes */
3119 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3120 PFINT_FW_CTL_CAUSE_ENA_M);
3121 wr32(hw, PFINT_FW_CTL, val);
3123 /* enable Mailbox queue Interrupt causes */
3124 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3125 PFINT_MBX_CTL_CAUSE_ENA_M);
3126 wr32(hw, PFINT_MBX_CTL, val);
3128 /* This enables Sideband queue Interrupt causes */
3129 val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3130 PFINT_SB_CTL_CAUSE_ENA_M);
3131 wr32(hw, PFINT_SB_CTL, val);
3137 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3138 * @pf: board private structure
3140 * This sets up the handler for MSIX 0, which is used to manage the
3141 * non-queue interrupts, e.g. AdminQ and errors. This is not used
3142 * when in MSI or Legacy interrupt mode.
3144 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3146 struct device *dev = ice_pf_to_dev(pf);
3147 struct ice_hw *hw = &pf->hw;
3148 int oicr_idx, err = 0;
3150 if (!pf->int_name[0])
3151 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3152 dev_driver_string(dev), dev_name(dev));
3154 /* Do not request IRQ but do enable OICR interrupt since settings are
3155 * lost during reset. Note that this function is called only during
3156 * rebuild path and not while reset is in progress.
3158 if (ice_is_reset_in_progress(pf->state))
3161 /* reserve one vector in irq_tracker for misc interrupts */
3162 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3166 pf->num_avail_sw_msix -= 1;
3167 pf->oicr_idx = (u16)oicr_idx;
3169 err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
3170 ice_misc_intr, 0, pf->int_name, pf);
3172 dev_err(dev, "devm_request_irq for %s failed: %d\n",
3174 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3175 pf->num_avail_sw_msix += 1;
3180 ice_ena_misc_vector(pf);
3182 ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
3183 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
3184 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3187 ice_irq_dynamic_ena(hw, NULL, NULL);
3193 * ice_napi_add - register NAPI handler for the VSI
3194 * @vsi: VSI for which NAPI handler is to be registered
3196 * This function is only called in the driver's load path. Registering the NAPI
3197 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3198 * reset/rebuild, etc.)
3200 static void ice_napi_add(struct ice_vsi *vsi)
3207 ice_for_each_q_vector(vsi, v_idx)
3208 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3209 ice_napi_poll, NAPI_POLL_WEIGHT);
3213 * ice_set_ops - set netdev and ethtools ops for the given netdev
3214 * @netdev: netdev instance
3216 static void ice_set_ops(struct net_device *netdev)
3218 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3220 if (ice_is_safe_mode(pf)) {
3221 netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3222 ice_set_ethtool_safe_mode_ops(netdev);
3226 netdev->netdev_ops = &ice_netdev_ops;
3227 netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3228 ice_set_ethtool_ops(netdev);
3232 * ice_set_netdev_features - set features for the given netdev
3233 * @netdev: netdev instance
3235 static void ice_set_netdev_features(struct net_device *netdev)
3237 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3238 netdev_features_t csumo_features;
3239 netdev_features_t vlano_features;
3240 netdev_features_t dflt_features;
3241 netdev_features_t tso_features;
3243 if (ice_is_safe_mode(pf)) {
3245 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3246 netdev->hw_features = netdev->features;
3250 dflt_features = NETIF_F_SG |
3255 csumo_features = NETIF_F_RXCSUM |
3260 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3261 NETIF_F_HW_VLAN_CTAG_TX |
3262 NETIF_F_HW_VLAN_CTAG_RX;
3264 tso_features = NETIF_F_TSO |
3268 NETIF_F_GSO_UDP_TUNNEL |
3269 NETIF_F_GSO_GRE_CSUM |
3270 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3271 NETIF_F_GSO_PARTIAL |
3272 NETIF_F_GSO_IPXIP4 |
3273 NETIF_F_GSO_IPXIP6 |
3276 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3277 NETIF_F_GSO_GRE_CSUM;
3278 /* set features that user can change */
3279 netdev->hw_features = dflt_features | csumo_features |
3280 vlano_features | tso_features;
3282 /* add support for HW_CSUM on packets with MPLS header */
3283 netdev->mpls_features = NETIF_F_HW_CSUM;
3285 /* enable features */
3286 netdev->features |= netdev->hw_features;
3288 netdev->hw_features |= NETIF_F_HW_TC;
3290 /* encap and VLAN devices inherit default, csumo and tso features */
3291 netdev->hw_enc_features |= dflt_features | csumo_features |
3293 netdev->vlan_features |= dflt_features | csumo_features |
3298 * ice_cfg_netdev - Allocate, configure and register a netdev
3299 * @vsi: the VSI associated with the new netdev
3301 * Returns 0 on success, negative value on failure
3303 static int ice_cfg_netdev(struct ice_vsi *vsi)
3305 struct ice_netdev_priv *np;
3306 struct net_device *netdev;
3307 u8 mac_addr[ETH_ALEN];
3309 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
3314 set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3315 vsi->netdev = netdev;
3316 np = netdev_priv(netdev);
3319 ice_set_netdev_features(netdev);
3321 ice_set_ops(netdev);
3323 if (vsi->type == ICE_VSI_PF) {
3324 SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
3325 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
3326 eth_hw_addr_set(netdev, mac_addr);
3327 ether_addr_copy(netdev->perm_addr, mac_addr);
3330 netdev->priv_flags |= IFF_UNICAST_FLT;
3332 /* Setup netdev TC information */
3333 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
3335 /* setup watchdog timeout value to be 5 second */
3336 netdev->watchdog_timeo = 5 * HZ;
3338 netdev->min_mtu = ETH_MIN_MTU;
3339 netdev->max_mtu = ICE_MAX_MTU;
3345 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3346 * @lut: Lookup table
3347 * @rss_table_size: Lookup table size
3348 * @rss_size: Range of queue number for hashing
3350 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3354 for (i = 0; i < rss_table_size; i++)
3355 lut[i] = i % rss_size;
3359 * ice_pf_vsi_setup - Set up a PF VSI
3360 * @pf: board private structure
3361 * @pi: pointer to the port_info instance
3363 * Returns pointer to the successfully allocated VSI software struct
3364 * on success, otherwise returns NULL on failure.
3366 static struct ice_vsi *
3367 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3369 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID, NULL);
3372 static struct ice_vsi *
3373 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3374 struct ice_channel *ch)
3376 return ice_vsi_setup(pf, pi, ICE_VSI_CHNL, ICE_INVAL_VFID, ch);
3380 * ice_ctrl_vsi_setup - Set up a control VSI
3381 * @pf: board private structure
3382 * @pi: pointer to the port_info instance
3384 * Returns pointer to the successfully allocated VSI software struct
3385 * on success, otherwise returns NULL on failure.
3387 static struct ice_vsi *
3388 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3390 return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, ICE_INVAL_VFID, NULL);
3394 * ice_lb_vsi_setup - Set up a loopback VSI
3395 * @pf: board private structure
3396 * @pi: pointer to the port_info instance
3398 * Returns pointer to the successfully allocated VSI software struct
3399 * on success, otherwise returns NULL on failure.
3402 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3404 return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID, NULL);
3408 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3409 * @netdev: network interface to be adjusted
3410 * @proto: unused protocol
3411 * @vid: VLAN ID to be added
3413 * net_device_ops implementation for adding VLAN IDs
3416 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
3419 struct ice_netdev_priv *np = netdev_priv(netdev);
3420 struct ice_vsi *vsi = np->vsi;
3423 /* VLAN 0 is added by default during load/reset */
3427 /* Enable VLAN pruning when a VLAN other than 0 is added */
3428 if (!ice_vsi_is_vlan_pruning_ena(vsi)) {
3429 ret = ice_cfg_vlan_pruning(vsi, true);
3434 /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3435 * packets aren't pruned by the device's internal switch on Rx
3437 ret = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3439 set_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
3445 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3446 * @netdev: network interface to be adjusted
3447 * @proto: unused protocol
3448 * @vid: VLAN ID to be removed
3450 * net_device_ops implementation for removing VLAN IDs
3453 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
3456 struct ice_netdev_priv *np = netdev_priv(netdev);
3457 struct ice_vsi *vsi = np->vsi;
3460 /* don't allow removal of VLAN 0 */
3464 /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3467 ret = ice_vsi_kill_vlan(vsi, vid);
3471 /* Disable pruning when VLAN 0 is the only VLAN rule */
3472 if (vsi->num_vlan == 1 && ice_vsi_is_vlan_pruning_ena(vsi))
3473 ret = ice_cfg_vlan_pruning(vsi, false);
3475 set_bit(ICE_VSI_VLAN_FLTR_CHANGED, vsi->state);
3480 * ice_rep_indr_tc_block_unbind
3481 * @cb_priv: indirection block private data
3483 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3485 struct ice_indr_block_priv *indr_priv = cb_priv;
3487 list_del(&indr_priv->list);
3492 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3493 * @vsi: VSI struct which has the netdev
3495 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3497 struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3499 flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3500 ice_rep_indr_tc_block_unbind);
3504 * ice_tc_indir_block_remove - clean indirect TC block notifications
3507 static void ice_tc_indir_block_remove(struct ice_pf *pf)
3509 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3514 ice_tc_indir_block_unregister(pf_vsi);
3518 * ice_tc_indir_block_register - Register TC indirect block notifications
3519 * @vsi: VSI struct which has the netdev
3521 * Returns 0 on success, negative value on failure
3523 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3525 struct ice_netdev_priv *np;
3527 if (!vsi || !vsi->netdev)
3530 np = netdev_priv(vsi->netdev);
3532 INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3533 return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3537 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3538 * @pf: board private structure
3540 * Returns 0 on success, negative value on failure
3542 static int ice_setup_pf_sw(struct ice_pf *pf)
3544 struct device *dev = ice_pf_to_dev(pf);
3545 struct ice_vsi *vsi;
3548 if (ice_is_reset_in_progress(pf->state))
3551 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3555 /* init channel list */
3556 INIT_LIST_HEAD(&vsi->ch_list);
3558 status = ice_cfg_netdev(vsi);
3560 goto unroll_vsi_setup;
3561 /* netdev has to be configured before setting frame size */
3562 ice_vsi_cfg_frame_size(vsi);
3564 /* init indirect block notifications */
3565 status = ice_tc_indir_block_register(vsi);
3567 dev_err(dev, "Failed to register netdev notifier\n");
3568 goto unroll_cfg_netdev;
3571 /* Setup DCB netlink interface */
3572 ice_dcbnl_setup(vsi);
3574 /* registering the NAPI handler requires both the queues and
3575 * netdev to be created, which are done in ice_pf_vsi_setup()
3576 * and ice_cfg_netdev() respectively
3580 status = ice_set_cpu_rx_rmap(vsi);
3582 dev_err(dev, "Failed to set CPU Rx map VSI %d error %d\n",
3583 vsi->vsi_num, status);
3584 goto unroll_napi_add;
3586 status = ice_init_mac_fltr(pf);
3588 goto free_cpu_rx_map;
3593 ice_free_cpu_rx_rmap(vsi);
3595 ice_tc_indir_block_unregister(vsi);
3600 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3601 free_netdev(vsi->netdev);
3607 ice_vsi_release(vsi);
3612 * ice_get_avail_q_count - Get count of queues in use
3613 * @pf_qmap: bitmap to get queue use count from
3614 * @lock: pointer to a mutex that protects access to pf_qmap
3615 * @size: size of the bitmap
3618 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3624 for_each_clear_bit(bit, pf_qmap, size)
3632 * ice_get_avail_txq_count - Get count of Tx queues in use
3633 * @pf: pointer to an ice_pf instance
3635 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3637 return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3642 * ice_get_avail_rxq_count - Get count of Rx queues in use
3643 * @pf: pointer to an ice_pf instance
3645 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3647 return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3652 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3653 * @pf: board private structure to initialize
3655 static void ice_deinit_pf(struct ice_pf *pf)
3657 ice_service_task_stop(pf);
3658 mutex_destroy(&pf->sw_mutex);
3659 mutex_destroy(&pf->tc_mutex);
3660 mutex_destroy(&pf->avail_q_mutex);
3662 if (pf->avail_txqs) {
3663 bitmap_free(pf->avail_txqs);
3664 pf->avail_txqs = NULL;
3667 if (pf->avail_rxqs) {
3668 bitmap_free(pf->avail_rxqs);
3669 pf->avail_rxqs = NULL;
3673 ptp_clock_unregister(pf->ptp.clock);
3677 * ice_set_pf_caps - set PFs capability flags
3678 * @pf: pointer to the PF instance
3680 static void ice_set_pf_caps(struct ice_pf *pf)
3682 struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3684 clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3685 clear_bit(ICE_FLAG_AUX_ENA, pf->flags);
3686 if (func_caps->common_cap.rdma) {
3687 set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3688 set_bit(ICE_FLAG_AUX_ENA, pf->flags);
3690 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3691 if (func_caps->common_cap.dcb)
3692 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3693 clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3694 if (func_caps->common_cap.sr_iov_1_1) {
3695 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3696 pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
3699 clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3700 if (func_caps->common_cap.rss_table_size)
3701 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3703 clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3704 if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3707 /* ctrl_vsi_idx will be set to a valid value when flow director
3708 * is setup by ice_init_fdir
3710 pf->ctrl_vsi_idx = ICE_NO_VSI;
3711 set_bit(ICE_FLAG_FD_ENA, pf->flags);
3712 /* force guaranteed filter pool for PF */
3713 ice_alloc_fd_guar_item(&pf->hw, &unused,
3714 func_caps->fd_fltr_guar);
3715 /* force shared filter pool for PF */
3716 ice_alloc_fd_shrd_item(&pf->hw, &unused,
3717 func_caps->fd_fltr_best_effort);
3720 clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3721 if (func_caps->common_cap.ieee_1588)
3722 set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3724 pf->max_pf_txqs = func_caps->common_cap.num_txq;
3725 pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3729 * ice_init_pf - Initialize general software structures (struct ice_pf)
3730 * @pf: board private structure to initialize
3732 static int ice_init_pf(struct ice_pf *pf)
3734 ice_set_pf_caps(pf);
3736 mutex_init(&pf->sw_mutex);
3737 mutex_init(&pf->tc_mutex);
3739 INIT_HLIST_HEAD(&pf->aq_wait_list);
3740 spin_lock_init(&pf->aq_wait_lock);
3741 init_waitqueue_head(&pf->aq_wait_queue);
3743 init_waitqueue_head(&pf->reset_wait_queue);
3745 /* setup service timer and periodic service task */
3746 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3747 pf->serv_tmr_period = HZ;
3748 INIT_WORK(&pf->serv_task, ice_service_task);
3749 clear_bit(ICE_SERVICE_SCHED, pf->state);
3751 mutex_init(&pf->avail_q_mutex);
3752 pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3753 if (!pf->avail_txqs)
3756 pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3757 if (!pf->avail_rxqs) {
3758 devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
3759 pf->avail_txqs = NULL;
3767 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3768 * @pf: board private structure
3770 * compute the number of MSIX vectors required (v_budget) and request from
3771 * the OS. Return the number of vectors reserved or negative on failure
3773 static int ice_ena_msix_range(struct ice_pf *pf)
3775 int num_cpus, v_left, v_actual, v_other, v_budget = 0;
3776 struct device *dev = ice_pf_to_dev(pf);
3779 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3780 num_cpus = num_online_cpus();
3782 /* reserve for LAN miscellaneous handler */
3783 needed = ICE_MIN_LAN_OICR_MSIX;
3784 if (v_left < needed)
3785 goto no_hw_vecs_left_err;
3789 /* reserve for flow director */
3790 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3791 needed = ICE_FDIR_MSIX;
3792 if (v_left < needed)
3793 goto no_hw_vecs_left_err;
3798 /* reserve for switchdev */
3799 needed = ICE_ESWITCH_MSIX;
3800 if (v_left < needed)
3801 goto no_hw_vecs_left_err;
3805 /* total used for non-traffic vectors */
3808 /* reserve vectors for LAN traffic */
3810 if (v_left < needed)
3811 goto no_hw_vecs_left_err;
3812 pf->num_lan_msix = needed;
3816 /* reserve vectors for RDMA auxiliary driver */
3817 if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) {
3818 needed = num_cpus + ICE_RDMA_NUM_AEQ_MSIX;
3819 if (v_left < needed)
3820 goto no_hw_vecs_left_err;
3821 pf->num_rdma_msix = needed;
3826 pf->msix_entries = devm_kcalloc(dev, v_budget,
3827 sizeof(*pf->msix_entries), GFP_KERNEL);
3828 if (!pf->msix_entries) {
3833 for (i = 0; i < v_budget; i++)
3834 pf->msix_entries[i].entry = i;
3836 /* actually reserve the vectors */
3837 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3838 ICE_MIN_MSIX, v_budget);
3840 dev_err(dev, "unable to reserve MSI-X vectors\n");
3845 if (v_actual < v_budget) {
3846 dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
3847 v_budget, v_actual);
3849 if (v_actual < ICE_MIN_MSIX) {
3850 /* error if we can't get minimum vectors */
3851 pci_disable_msix(pf->pdev);
3855 int v_remain = v_actual - v_other;
3856 int v_rdma = 0, v_min_rdma = 0;
3858 if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) {
3859 /* Need at least 1 interrupt in addition to
3862 v_rdma = ICE_RDMA_NUM_AEQ_MSIX + 1;
3863 v_min_rdma = ICE_MIN_RDMA_MSIX;
3866 if (v_actual == ICE_MIN_MSIX ||
3867 v_remain < ICE_MIN_LAN_TXRX_MSIX + v_min_rdma) {
3868 dev_warn(dev, "Not enough MSI-X vectors to support RDMA.\n");
3869 clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3871 pf->num_rdma_msix = 0;
3872 pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
3873 } else if ((v_remain < ICE_MIN_LAN_TXRX_MSIX + v_rdma) ||
3874 (v_remain - v_rdma < v_rdma)) {
3875 /* Support minimum RDMA and give remaining
3876 * vectors to LAN MSIX
3878 pf->num_rdma_msix = v_min_rdma;
3879 pf->num_lan_msix = v_remain - v_min_rdma;
3881 /* Split remaining MSIX with RDMA after
3882 * accounting for AEQ MSIX
3884 pf->num_rdma_msix = (v_remain - ICE_RDMA_NUM_AEQ_MSIX) / 2 +
3885 ICE_RDMA_NUM_AEQ_MSIX;
3886 pf->num_lan_msix = v_remain - pf->num_rdma_msix;
3889 dev_notice(dev, "Enabled %d MSI-X vectors for LAN traffic.\n",
3892 if (test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
3893 dev_notice(dev, "Enabled %d MSI-X vectors for RDMA.\n",
3901 devm_kfree(dev, pf->msix_entries);
3904 no_hw_vecs_left_err:
3905 dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
3909 pf->num_rdma_msix = 0;
3910 pf->num_lan_msix = 0;
3915 * ice_dis_msix - Disable MSI-X interrupt setup in OS
3916 * @pf: board private structure
3918 static void ice_dis_msix(struct ice_pf *pf)
3920 pci_disable_msix(pf->pdev);
3921 devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
3922 pf->msix_entries = NULL;
3926 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3927 * @pf: board private structure
3929 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3933 if (pf->irq_tracker) {
3934 devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
3935 pf->irq_tracker = NULL;
3940 * ice_init_interrupt_scheme - Determine proper interrupt scheme
3941 * @pf: board private structure to initialize
3943 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3947 vectors = ice_ena_msix_range(pf);
3952 /* set up vector assignment tracking */
3953 pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
3954 struct_size(pf->irq_tracker, list, vectors),
3956 if (!pf->irq_tracker) {
3961 /* populate SW interrupts pool with number of OS granted IRQs. */
3962 pf->num_avail_sw_msix = (u16)vectors;
3963 pf->irq_tracker->num_entries = (u16)vectors;
3964 pf->irq_tracker->end = pf->irq_tracker->num_entries;
3970 * ice_is_wol_supported - check if WoL is supported
3971 * @hw: pointer to hardware info
3973 * Check if WoL is supported based on the HW configuration.
3974 * Returns true if NVM supports and enables WoL for this port, false otherwise
3976 bool ice_is_wol_supported(struct ice_hw *hw)
3980 /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3981 * word) indicates WoL is not supported on the corresponding PF ID.
3983 if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3986 return !(BIT(hw->port_info->lport) & wol_ctrl);
3990 * ice_vsi_recfg_qs - Change the number of queues on a VSI
3991 * @vsi: VSI being changed
3992 * @new_rx: new number of Rx queues
3993 * @new_tx: new number of Tx queues
3995 * Only change the number of queues if new_tx, or new_rx is non-0.
3997 * Returns 0 on success.
3999 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
4001 struct ice_pf *pf = vsi->back;
4002 int err = 0, timeout = 50;
4004 if (!new_rx && !new_tx)
4007 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4011 usleep_range(1000, 2000);
4015 vsi->req_txq = (u16)new_tx;
4017 vsi->req_rxq = (u16)new_rx;
4019 /* set for the next time the netdev is started */
4020 if (!netif_running(vsi->netdev)) {
4021 ice_vsi_rebuild(vsi, false);
4022 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4027 ice_vsi_rebuild(vsi, false);
4028 ice_pf_dcb_recfg(pf);
4031 clear_bit(ICE_CFG_BUSY, pf->state);
4036 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4037 * @pf: PF to configure
4039 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4040 * VSI can still Tx/Rx VLAN tagged packets.
4042 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4044 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4045 struct ice_vsi_ctx *ctxt;
4052 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4057 ctxt->info = vsi->info;
4059 ctxt->info.valid_sections =
4060 cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4061 ICE_AQ_VSI_PROP_SECURITY_VALID |
4062 ICE_AQ_VSI_PROP_SW_VALID);
4064 /* disable VLAN anti-spoof */
4065 ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4066 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4068 /* disable VLAN pruning and keep all other settings */
4069 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4071 /* allow all VLANs on Tx and don't strip on Rx */
4072 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL |
4073 ICE_AQ_VSI_VLAN_EMOD_NOTHING;
4075 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4077 dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4078 status, ice_aq_str(hw->adminq.sq_last_status));
4080 vsi->info.sec_flags = ctxt->info.sec_flags;
4081 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4082 vsi->info.vlan_flags = ctxt->info.vlan_flags;
4089 * ice_log_pkg_init - log result of DDP package load
4090 * @hw: pointer to hardware info
4091 * @state: state of package load
4093 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4095 struct ice_pf *pf = hw->back;
4098 dev = ice_pf_to_dev(pf);
4101 case ICE_DDP_PKG_SUCCESS:
4102 dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4103 hw->active_pkg_name,
4104 hw->active_pkg_ver.major,
4105 hw->active_pkg_ver.minor,
4106 hw->active_pkg_ver.update,
4107 hw->active_pkg_ver.draft);
4109 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4110 dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4111 hw->active_pkg_name,
4112 hw->active_pkg_ver.major,
4113 hw->active_pkg_ver.minor,
4114 hw->active_pkg_ver.update,
4115 hw->active_pkg_ver.draft);
4117 case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4118 dev_err(dev, "The device has a DDP package that is not supported by the driver. The device has package '%s' version %d.%d.x.x. The driver requires version %d.%d.x.x. Entering Safe Mode.\n",
4119 hw->active_pkg_name,
4120 hw->active_pkg_ver.major,
4121 hw->active_pkg_ver.minor,
4122 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4124 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4125 dev_info(dev, "The driver could not load the DDP package file because a compatible DDP package is already present on the device. The device has package '%s' version %d.%d.%d.%d. The package file found by the driver: '%s' version %d.%d.%d.%d.\n",
4126 hw->active_pkg_name,
4127 hw->active_pkg_ver.major,
4128 hw->active_pkg_ver.minor,
4129 hw->active_pkg_ver.update,
4130 hw->active_pkg_ver.draft,
4137 case ICE_DDP_PKG_FW_MISMATCH:
4138 dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package. Please update the device's NVM. Entering safe mode.\n");
4140 case ICE_DDP_PKG_INVALID_FILE:
4141 dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4143 case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4144 dev_err(dev, "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n");
4146 case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4147 dev_err(dev, "The DDP package file version is lower than the driver supports. The driver requires version %d.%d.x.x. Please use an updated DDP Package file. Entering Safe Mode.\n",
4148 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4150 case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4151 dev_err(dev, "The DDP package could not be loaded because its signature is not valid. Please use a valid DDP Package. Entering Safe Mode.\n");
4153 case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4154 dev_err(dev, "The DDP Package could not be loaded because its security revision is too low. Please use an updated DDP Package. Entering Safe Mode.\n");
4156 case ICE_DDP_PKG_LOAD_ERROR:
4157 dev_err(dev, "An error occurred on the device while loading the DDP package. The device will be reset.\n");
4158 /* poll for reset to complete */
4159 if (ice_check_reset(hw))
4160 dev_err(dev, "Error resetting device. Please reload the driver\n");
4162 case ICE_DDP_PKG_ERR:
4164 dev_err(dev, "An unknown error occurred when loading the DDP package. Entering Safe Mode.\n");
4170 * ice_load_pkg - load/reload the DDP Package file
4171 * @firmware: firmware structure when firmware requested or NULL for reload
4172 * @pf: pointer to the PF instance
4174 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4175 * initialize HW tables.
4178 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4180 enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4181 struct device *dev = ice_pf_to_dev(pf);
4182 struct ice_hw *hw = &pf->hw;
4184 /* Load DDP Package */
4185 if (firmware && !hw->pkg_copy) {
4186 state = ice_copy_and_init_pkg(hw, firmware->data,
4188 ice_log_pkg_init(hw, state);
4189 } else if (!firmware && hw->pkg_copy) {
4190 /* Reload package during rebuild after CORER/GLOBR reset */
4191 state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4192 ice_log_pkg_init(hw, state);
4194 dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4197 if (!ice_is_init_pkg_successful(state)) {
4199 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4203 /* Successful download package is the precondition for advanced
4204 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4206 set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4210 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4211 * @pf: pointer to the PF structure
4213 * There is no error returned here because the driver should be able to handle
4214 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4215 * specifically with Tx.
4217 static void ice_verify_cacheline_size(struct ice_pf *pf)
4219 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4220 dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4221 ICE_CACHE_LINE_BYTES);
4225 * ice_send_version - update firmware with driver version
4228 * Returns 0 on success, else error code
4230 static int ice_send_version(struct ice_pf *pf)
4232 struct ice_driver_ver dv;
4234 dv.major_ver = 0xff;
4235 dv.minor_ver = 0xff;
4236 dv.build_ver = 0xff;
4237 dv.subbuild_ver = 0;
4238 strscpy((char *)dv.driver_string, UTS_RELEASE,
4239 sizeof(dv.driver_string));
4240 return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4244 * ice_init_fdir - Initialize flow director VSI and configuration
4245 * @pf: pointer to the PF instance
4247 * returns 0 on success, negative on error
4249 static int ice_init_fdir(struct ice_pf *pf)
4251 struct device *dev = ice_pf_to_dev(pf);
4252 struct ice_vsi *ctrl_vsi;
4255 /* Side Band Flow Director needs to have a control VSI.
4256 * Allocate it and store it in the PF.
4258 ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4260 dev_dbg(dev, "could not create control VSI\n");
4264 err = ice_vsi_open_ctrl(ctrl_vsi);
4266 dev_dbg(dev, "could not open control VSI\n");
4270 mutex_init(&pf->hw.fdir_fltr_lock);
4272 err = ice_fdir_create_dflt_rules(pf);
4279 ice_fdir_release_flows(&pf->hw);
4280 ice_vsi_close(ctrl_vsi);
4282 ice_vsi_release(ctrl_vsi);
4283 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4284 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4285 pf->ctrl_vsi_idx = ICE_NO_VSI;
4291 * ice_get_opt_fw_name - return optional firmware file name or NULL
4292 * @pf: pointer to the PF instance
4294 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4296 /* Optional firmware name same as default with additional dash
4297 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4299 struct pci_dev *pdev = pf->pdev;
4300 char *opt_fw_filename;
4303 /* Determine the name of the optional file using the DSN (two
4304 * dwords following the start of the DSN Capability).
4306 dsn = pci_get_dsn(pdev);
4310 opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4311 if (!opt_fw_filename)
4314 snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4315 ICE_DDP_PKG_PATH, dsn);
4317 return opt_fw_filename;
4321 * ice_request_fw - Device initialization routine
4322 * @pf: pointer to the PF instance
4324 static void ice_request_fw(struct ice_pf *pf)
4326 char *opt_fw_filename = ice_get_opt_fw_name(pf);
4327 const struct firmware *firmware = NULL;
4328 struct device *dev = ice_pf_to_dev(pf);
4331 /* optional device-specific DDP (if present) overrides the default DDP
4332 * package file. kernel logs a debug message if the file doesn't exist,
4333 * and warning messages for other errors.
4335 if (opt_fw_filename) {
4336 err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4338 kfree(opt_fw_filename);
4342 /* request for firmware was successful. Download to device */
4343 ice_load_pkg(firmware, pf);
4344 kfree(opt_fw_filename);
4345 release_firmware(firmware);
4350 err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4352 dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4356 /* request for firmware was successful. Download to device */
4357 ice_load_pkg(firmware, pf);
4358 release_firmware(firmware);
4362 * ice_print_wake_reason - show the wake up cause in the log
4363 * @pf: pointer to the PF struct
4365 static void ice_print_wake_reason(struct ice_pf *pf)
4367 u32 wus = pf->wakeup_reason;
4368 const char *wake_str;
4370 /* if no wake event, nothing to print */
4374 if (wus & PFPM_WUS_LNKC_M)
4375 wake_str = "Link\n";
4376 else if (wus & PFPM_WUS_MAG_M)
4377 wake_str = "Magic Packet\n";
4378 else if (wus & PFPM_WUS_MNG_M)
4379 wake_str = "Management\n";
4380 else if (wus & PFPM_WUS_FW_RST_WK_M)
4381 wake_str = "Firmware Reset\n";
4383 wake_str = "Unknown\n";
4385 dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4389 * ice_register_netdev - register netdev and devlink port
4390 * @pf: pointer to the PF struct
4392 static int ice_register_netdev(struct ice_pf *pf)
4394 struct ice_vsi *vsi;
4397 vsi = ice_get_main_vsi(pf);
4398 if (!vsi || !vsi->netdev)
4401 err = register_netdev(vsi->netdev);
4403 goto err_register_netdev;
4405 set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4406 netif_carrier_off(vsi->netdev);
4407 netif_tx_stop_all_queues(vsi->netdev);
4408 err = ice_devlink_create_pf_port(pf);
4410 goto err_devlink_create;
4412 devlink_port_type_eth_set(&pf->devlink_port, vsi->netdev);
4416 unregister_netdev(vsi->netdev);
4417 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4418 err_register_netdev:
4419 free_netdev(vsi->netdev);
4421 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4426 * ice_probe - Device initialization routine
4427 * @pdev: PCI device information struct
4428 * @ent: entry in ice_pci_tbl
4430 * Returns 0 on success, negative on failure
4433 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
4435 struct device *dev = &pdev->dev;
4440 if (pdev->is_virtfn) {
4441 dev_err(dev, "can't probe a virtual function\n");
4445 /* this driver uses devres, see
4446 * Documentation/driver-api/driver-model/devres.rst
4448 err = pcim_enable_device(pdev);
4452 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
4454 dev_err(dev, "BAR0 I/O map error %d\n", err);
4458 pf = ice_allocate_pf(dev);
4462 /* initialize Auxiliary index to invalid value */
4465 /* set up for high or low DMA */
4466 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4468 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
4470 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4474 pci_enable_pcie_error_reporting(pdev);
4475 pci_set_master(pdev);
4478 pci_set_drvdata(pdev, pf);
4479 set_bit(ICE_DOWN, pf->state);
4480 /* Disable service task until DOWN bit is cleared */
4481 set_bit(ICE_SERVICE_DIS, pf->state);
4484 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4485 pci_save_state(pdev);
4488 hw->vendor_id = pdev->vendor;
4489 hw->device_id = pdev->device;
4490 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4491 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4492 hw->subsystem_device_id = pdev->subsystem_device;
4493 hw->bus.device = PCI_SLOT(pdev->devfn);
4494 hw->bus.func = PCI_FUNC(pdev->devfn);
4495 ice_set_ctrlq_len(hw);
4497 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4499 #ifndef CONFIG_DYNAMIC_DEBUG
4501 hw->debug_mask = debug;
4504 err = ice_init_hw(hw);
4506 dev_err(dev, "ice_init_hw failed: %d\n", err);
4508 goto err_exit_unroll;
4511 ice_init_feature_support(pf);
4515 /* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4516 * set in pf->state, which will cause ice_is_safe_mode to return
4519 if (ice_is_safe_mode(pf)) {
4520 /* we already got function/device capabilities but these don't
4521 * reflect what the driver needs to do in safe mode. Instead of
4522 * adding conditional logic everywhere to ignore these
4523 * device/function capabilities, override them.
4525 ice_set_safe_mode_caps(hw);
4528 err = ice_init_pf(pf);
4530 dev_err(dev, "ice_init_pf failed: %d\n", err);
4531 goto err_init_pf_unroll;
4534 ice_devlink_init_regions(pf);
4536 pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4537 pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4538 pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4539 pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4541 if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4542 pf->hw.udp_tunnel_nic.tables[i].n_entries =
4543 pf->hw.tnl.valid_count[TNL_VXLAN];
4544 pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4545 UDP_TUNNEL_TYPE_VXLAN;
4548 if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4549 pf->hw.udp_tunnel_nic.tables[i].n_entries =
4550 pf->hw.tnl.valid_count[TNL_GENEVE];
4551 pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4552 UDP_TUNNEL_TYPE_GENEVE;
4556 pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4557 if (!pf->num_alloc_vsi) {
4559 goto err_init_pf_unroll;
4561 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4562 dev_warn(&pf->pdev->dev,
4563 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4564 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4565 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4568 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4572 goto err_init_pf_unroll;
4575 err = ice_init_interrupt_scheme(pf);
4577 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4579 goto err_init_vsi_unroll;
4582 /* In case of MSIX we are going to setup the misc vector right here
4583 * to handle admin queue events etc. In case of legacy and MSI
4584 * the misc functionality and queue processing is combined in
4585 * the same vector and that gets setup at open.
4587 err = ice_req_irq_msix_misc(pf);
4589 dev_err(dev, "setup of misc vector failed: %d\n", err);
4590 goto err_init_interrupt_unroll;
4593 /* create switch struct for the switch element created by FW on boot */
4594 pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4595 if (!pf->first_sw) {
4597 goto err_msix_misc_unroll;
4601 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4603 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4605 pf->first_sw->pf = pf;
4607 /* record the sw_id available for later use */
4608 pf->first_sw->sw_id = hw->port_info->sw_id;
4610 err = ice_setup_pf_sw(pf);
4612 dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4613 goto err_alloc_sw_unroll;
4616 clear_bit(ICE_SERVICE_DIS, pf->state);
4618 /* tell the firmware we are up */
4619 err = ice_send_version(pf);
4621 dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4623 goto err_send_version_unroll;
4626 /* since everything is good, start the service timer */
4627 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4629 err = ice_init_link_events(pf->hw.port_info);
4631 dev_err(dev, "ice_init_link_events failed: %d\n", err);
4632 goto err_send_version_unroll;
4635 /* not a fatal error if this fails */
4636 err = ice_init_nvm_phy_type(pf->hw.port_info);
4638 dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4640 /* not a fatal error if this fails */
4641 err = ice_update_link_info(pf->hw.port_info);
4643 dev_err(dev, "ice_update_link_info failed: %d\n", err);
4645 ice_init_link_dflt_override(pf->hw.port_info);
4647 ice_check_link_cfg_err(pf,
4648 pf->hw.port_info->phy.link_info.link_cfg_err);
4650 /* if media available, initialize PHY settings */
4651 if (pf->hw.port_info->phy.link_info.link_info &
4652 ICE_AQ_MEDIA_AVAILABLE) {
4653 /* not a fatal error if this fails */
4654 err = ice_init_phy_user_cfg(pf->hw.port_info);
4656 dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4658 if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4659 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4662 ice_configure_phy(vsi);
4665 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4668 ice_verify_cacheline_size(pf);
4670 /* Save wakeup reason register for later use */
4671 pf->wakeup_reason = rd32(hw, PFPM_WUS);
4673 /* check for a power management event */
4674 ice_print_wake_reason(pf);
4676 /* clear wake status, all bits */
4677 wr32(hw, PFPM_WUS, U32_MAX);
4679 /* Disable WoL at init, wait for user to enable */
4680 device_set_wakeup_enable(dev, false);
4682 if (ice_is_safe_mode(pf)) {
4683 ice_set_safe_mode_vlan_cfg(pf);
4687 /* initialize DDP driven features */
4688 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4691 /* Note: Flow director init failure is non-fatal to load */
4692 if (ice_init_fdir(pf))
4693 dev_err(dev, "could not initialize flow director\n");
4695 /* Note: DCB init failure is non-fatal to load */
4696 if (ice_init_pf_dcb(pf, false)) {
4697 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4698 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4700 ice_cfg_lldp_mib_change(&pf->hw, true);
4703 if (ice_init_lag(pf))
4704 dev_warn(dev, "Failed to init link aggregation support\n");
4706 /* print PCI link speed and width */
4707 pcie_print_link_status(pf->pdev);
4710 err = ice_register_netdev(pf);
4712 goto err_netdev_reg;
4714 err = ice_devlink_register_params(pf);
4716 goto err_netdev_reg;
4718 /* ready to go, so clear down state bit */
4719 clear_bit(ICE_DOWN, pf->state);
4720 if (ice_is_aux_ena(pf)) {
4721 pf->aux_idx = ida_alloc(&ice_aux_ida, GFP_KERNEL);
4722 if (pf->aux_idx < 0) {
4723 dev_err(dev, "Failed to allocate device ID for AUX driver\n");
4725 goto err_devlink_reg_param;
4728 err = ice_init_rdma(pf);
4730 dev_err(dev, "Failed to initialize RDMA: %d\n", err);
4732 goto err_init_aux_unroll;
4735 dev_warn(dev, "RDMA is not supported on this device\n");
4738 ice_devlink_register(pf);
4741 err_init_aux_unroll:
4743 ida_free(&ice_aux_ida, pf->aux_idx);
4744 err_devlink_reg_param:
4745 ice_devlink_unregister_params(pf);
4747 err_send_version_unroll:
4748 ice_vsi_release_all(pf);
4749 err_alloc_sw_unroll:
4750 set_bit(ICE_SERVICE_DIS, pf->state);
4751 set_bit(ICE_DOWN, pf->state);
4752 devm_kfree(dev, pf->first_sw);
4753 err_msix_misc_unroll:
4754 ice_free_irq_msix_misc(pf);
4755 err_init_interrupt_unroll:
4756 ice_clear_interrupt_scheme(pf);
4757 err_init_vsi_unroll:
4758 devm_kfree(dev, pf->vsi);
4761 ice_devlink_destroy_regions(pf);
4764 pci_disable_pcie_error_reporting(pdev);
4765 pci_disable_device(pdev);
4770 * ice_set_wake - enable or disable Wake on LAN
4771 * @pf: pointer to the PF struct
4773 * Simple helper for WoL control
4775 static void ice_set_wake(struct ice_pf *pf)
4777 struct ice_hw *hw = &pf->hw;
4778 bool wol = pf->wol_ena;
4780 /* clear wake state, otherwise new wake events won't fire */
4781 wr32(hw, PFPM_WUS, U32_MAX);
4783 /* enable / disable APM wake up, no RMW needed */
4784 wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4786 /* set magic packet filter enabled */
4787 wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4791 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
4792 * @pf: pointer to the PF struct
4794 * Issue firmware command to enable multicast magic wake, making
4795 * sure that any locally administered address (LAA) is used for
4796 * wake, and that PF reset doesn't undo the LAA.
4798 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4800 struct device *dev = ice_pf_to_dev(pf);
4801 struct ice_hw *hw = &pf->hw;
4802 u8 mac_addr[ETH_ALEN];
4803 struct ice_vsi *vsi;
4810 vsi = ice_get_main_vsi(pf);
4814 /* Get current MAC address in case it's an LAA */
4816 ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4818 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4820 flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4821 ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4822 ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4824 status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4826 dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
4827 status, ice_aq_str(hw->adminq.sq_last_status));
4831 * ice_remove - Device removal routine
4832 * @pdev: PCI device information struct
4834 static void ice_remove(struct pci_dev *pdev)
4836 struct ice_pf *pf = pci_get_drvdata(pdev);
4839 ice_devlink_unregister(pf);
4840 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4841 if (!ice_is_reset_in_progress(pf->state))
4846 ice_tc_indir_block_remove(pf);
4848 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4849 set_bit(ICE_VF_RESETS_DISABLED, pf->state);
4853 ice_service_task_stop(pf);
4855 ice_aq_cancel_waiting_tasks(pf);
4856 ice_unplug_aux_dev(pf);
4857 if (pf->aux_idx >= 0)
4858 ida_free(&ice_aux_ida, pf->aux_idx);
4859 ice_devlink_unregister_params(pf);
4860 set_bit(ICE_DOWN, pf->state);
4862 mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4864 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4865 ice_ptp_release(pf);
4866 if (!ice_is_safe_mode(pf))
4867 ice_remove_arfs(pf);
4868 ice_setup_mc_magic_wake(pf);
4869 ice_vsi_release_all(pf);
4871 ice_free_irq_msix_misc(pf);
4872 ice_for_each_vsi(pf, i) {
4875 ice_vsi_free_q_vectors(pf->vsi[i]);
4878 ice_devlink_destroy_regions(pf);
4879 ice_deinit_hw(&pf->hw);
4881 /* Issue a PFR as part of the prescribed driver unload flow. Do not
4882 * do it via ice_schedule_reset() since there is no need to rebuild
4883 * and the service task is already stopped.
4885 ice_reset(&pf->hw, ICE_RESET_PFR);
4886 pci_wait_for_pending_transaction(pdev);
4887 ice_clear_interrupt_scheme(pf);
4888 pci_disable_pcie_error_reporting(pdev);
4889 pci_disable_device(pdev);
4893 * ice_shutdown - PCI callback for shutting down device
4894 * @pdev: PCI device information struct
4896 static void ice_shutdown(struct pci_dev *pdev)
4898 struct ice_pf *pf = pci_get_drvdata(pdev);
4902 if (system_state == SYSTEM_POWER_OFF) {
4903 pci_wake_from_d3(pdev, pf->wol_ena);
4904 pci_set_power_state(pdev, PCI_D3hot);
4910 * ice_prepare_for_shutdown - prep for PCI shutdown
4911 * @pf: board private structure
4913 * Inform or close all dependent features in prep for PCI device shutdown
4915 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4917 struct ice_hw *hw = &pf->hw;
4920 /* Notify VFs of impending reset */
4921 if (ice_check_sq_alive(hw, &hw->mailboxq))
4922 ice_vc_notify_reset(pf);
4924 dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4926 /* disable the VSIs and their queues that are not already DOWN */
4927 ice_pf_dis_all_vsi(pf, false);
4929 ice_for_each_vsi(pf, v)
4931 pf->vsi[v]->vsi_num = 0;
4933 ice_shutdown_all_ctrlq(hw);
4937 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4938 * @pf: board private structure to reinitialize
4940 * This routine reinitialize interrupt scheme that was cleared during
4941 * power management suspend callback.
4943 * This should be called during resume routine to re-allocate the q_vectors
4944 * and reacquire interrupts.
4946 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4948 struct device *dev = ice_pf_to_dev(pf);
4951 /* Since we clear MSIX flag during suspend, we need to
4952 * set it back during resume...
4955 ret = ice_init_interrupt_scheme(pf);
4957 dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4961 /* Remap vectors and rings, after successful re-init interrupts */
4962 ice_for_each_vsi(pf, v) {
4966 ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4969 ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4972 ret = ice_req_irq_msix_misc(pf);
4974 dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4984 ice_vsi_free_q_vectors(pf->vsi[v]);
4991 * @dev: generic device information structure
4993 * Power Management callback to quiesce the device and prepare
4994 * for D3 transition.
4996 static int __maybe_unused ice_suspend(struct device *dev)
4998 struct pci_dev *pdev = to_pci_dev(dev);
5002 pf = pci_get_drvdata(pdev);
5004 if (!ice_pf_state_is_nominal(pf)) {
5005 dev_err(dev, "Device is not ready, no need to suspend it\n");
5009 /* Stop watchdog tasks until resume completion.
5010 * Even though it is most likely that the service task is
5011 * disabled if the device is suspended or down, the service task's
5012 * state is controlled by a different state bit, and we should
5013 * store and honor whatever state that bit is in at this point.
5015 disabled = ice_service_task_stop(pf);
5017 ice_unplug_aux_dev(pf);
5019 /* Already suspended?, then there is nothing to do */
5020 if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5022 ice_service_task_restart(pf);
5026 if (test_bit(ICE_DOWN, pf->state) ||
5027 ice_is_reset_in_progress(pf->state)) {
5028 dev_err(dev, "can't suspend device in reset or already down\n");
5030 ice_service_task_restart(pf);
5034 ice_setup_mc_magic_wake(pf);
5036 ice_prepare_for_shutdown(pf);
5040 /* Free vectors, clear the interrupt scheme and release IRQs
5041 * for proper hibernation, especially with large number of CPUs.
5042 * Otherwise hibernation might fail when mapping all the vectors back
5045 ice_free_irq_msix_misc(pf);
5046 ice_for_each_vsi(pf, v) {
5049 ice_vsi_free_q_vectors(pf->vsi[v]);
5051 ice_free_cpu_rx_rmap(ice_get_main_vsi(pf));
5052 ice_clear_interrupt_scheme(pf);
5054 pci_save_state(pdev);
5055 pci_wake_from_d3(pdev, pf->wol_ena);
5056 pci_set_power_state(pdev, PCI_D3hot);
5061 * ice_resume - PM callback for waking up from D3
5062 * @dev: generic device information structure
5064 static int __maybe_unused ice_resume(struct device *dev)
5066 struct pci_dev *pdev = to_pci_dev(dev);
5067 enum ice_reset_req reset_type;
5072 pci_set_power_state(pdev, PCI_D0);
5073 pci_restore_state(pdev);
5074 pci_save_state(pdev);
5076 if (!pci_device_is_present(pdev))
5079 ret = pci_enable_device_mem(pdev);
5081 dev_err(dev, "Cannot enable device after suspend\n");
5085 pf = pci_get_drvdata(pdev);
5088 pf->wakeup_reason = rd32(hw, PFPM_WUS);
5089 ice_print_wake_reason(pf);
5091 /* We cleared the interrupt scheme when we suspended, so we need to
5092 * restore it now to resume device functionality.
5094 ret = ice_reinit_interrupt_scheme(pf);
5096 dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5098 clear_bit(ICE_DOWN, pf->state);
5099 /* Now perform PF reset and rebuild */
5100 reset_type = ICE_RESET_PFR;
5101 /* re-enable service task for reset, but allow reset to schedule it */
5102 clear_bit(ICE_SERVICE_DIS, pf->state);
5104 if (ice_schedule_reset(pf, reset_type))
5105 dev_err(dev, "Reset during resume failed.\n");
5107 clear_bit(ICE_SUSPENDED, pf->state);
5108 ice_service_task_restart(pf);
5110 /* Restart the service task */
5111 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5115 #endif /* CONFIG_PM */
5118 * ice_pci_err_detected - warning that PCI error has been detected
5119 * @pdev: PCI device information struct
5120 * @err: the type of PCI error
5122 * Called to warn that something happened on the PCI bus and the error handling
5123 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
5125 static pci_ers_result_t
5126 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5128 struct ice_pf *pf = pci_get_drvdata(pdev);
5131 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5133 return PCI_ERS_RESULT_DISCONNECT;
5136 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5137 ice_service_task_stop(pf);
5139 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5140 set_bit(ICE_PFR_REQ, pf->state);
5141 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5145 return PCI_ERS_RESULT_NEED_RESET;
5149 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5150 * @pdev: PCI device information struct
5152 * Called to determine if the driver can recover from the PCI slot reset by
5153 * using a register read to determine if the device is recoverable.
5155 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5157 struct ice_pf *pf = pci_get_drvdata(pdev);
5158 pci_ers_result_t result;
5162 err = pci_enable_device_mem(pdev);
5164 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5166 result = PCI_ERS_RESULT_DISCONNECT;
5168 pci_set_master(pdev);
5169 pci_restore_state(pdev);
5170 pci_save_state(pdev);
5171 pci_wake_from_d3(pdev, false);
5173 /* Check for life */
5174 reg = rd32(&pf->hw, GLGEN_RTRIG);
5176 result = PCI_ERS_RESULT_RECOVERED;
5178 result = PCI_ERS_RESULT_DISCONNECT;
5181 err = pci_aer_clear_nonfatal_status(pdev);
5183 dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
5185 /* non-fatal, continue */
5191 * ice_pci_err_resume - restart operations after PCI error recovery
5192 * @pdev: PCI device information struct
5194 * Called to allow the driver to bring things back up after PCI error and/or
5195 * reset recovery have finished
5197 static void ice_pci_err_resume(struct pci_dev *pdev)
5199 struct ice_pf *pf = pci_get_drvdata(pdev);
5202 dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5207 if (test_bit(ICE_SUSPENDED, pf->state)) {
5208 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5213 ice_restore_all_vfs_msi_state(pdev);
5215 ice_do_reset(pf, ICE_RESET_PFR);
5216 ice_service_task_restart(pf);
5217 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5221 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5222 * @pdev: PCI device information struct
5224 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5226 struct ice_pf *pf = pci_get_drvdata(pdev);
5228 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5229 ice_service_task_stop(pf);
5231 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5232 set_bit(ICE_PFR_REQ, pf->state);
5233 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5239 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5240 * @pdev: PCI device information struct
5242 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5244 ice_pci_err_resume(pdev);
5247 /* ice_pci_tbl - PCI Device ID Table
5249 * Wildcard entries (PCI_ANY_ID) should come last
5250 * Last entry must be all 0s
5252 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5253 * Class, Class Mask, private data (not used) }
5255 static const struct pci_device_id ice_pci_tbl[] = {
5256 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5257 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5258 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5259 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5260 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5261 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5262 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5263 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5264 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5265 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5266 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5267 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5268 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5269 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5270 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5271 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5272 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5273 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5274 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5275 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5276 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5277 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5278 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5279 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5280 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5281 /* required last entry */
5284 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5286 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5288 static const struct pci_error_handlers ice_pci_err_handler = {
5289 .error_detected = ice_pci_err_detected,
5290 .slot_reset = ice_pci_err_slot_reset,
5291 .reset_prepare = ice_pci_err_reset_prepare,
5292 .reset_done = ice_pci_err_reset_done,
5293 .resume = ice_pci_err_resume
5296 static struct pci_driver ice_driver = {
5297 .name = KBUILD_MODNAME,
5298 .id_table = ice_pci_tbl,
5300 .remove = ice_remove,
5302 .driver.pm = &ice_pm_ops,
5303 #endif /* CONFIG_PM */
5304 .shutdown = ice_shutdown,
5305 .sriov_configure = ice_sriov_configure,
5306 .err_handler = &ice_pci_err_handler
5310 * ice_module_init - Driver registration routine
5312 * ice_module_init is the first routine called when the driver is
5313 * loaded. All it does is register with the PCI subsystem.
5315 static int __init ice_module_init(void)
5319 pr_info("%s\n", ice_driver_string);
5320 pr_info("%s\n", ice_copyright);
5322 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
5324 pr_err("Failed to create workqueue\n");
5328 status = pci_register_driver(&ice_driver);
5330 pr_err("failed to register PCI driver, err %d\n", status);
5331 destroy_workqueue(ice_wq);
5336 module_init(ice_module_init);
5339 * ice_module_exit - Driver exit cleanup routine
5341 * ice_module_exit is called just before the driver is removed
5344 static void __exit ice_module_exit(void)
5346 pci_unregister_driver(&ice_driver);
5347 destroy_workqueue(ice_wq);
5348 pr_info("module unloaded\n");
5350 module_exit(ice_module_exit);
5353 * ice_set_mac_address - NDO callback to set MAC address
5354 * @netdev: network interface device structure
5355 * @pi: pointer to an address structure
5357 * Returns 0 on success, negative on failure
5359 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5361 struct ice_netdev_priv *np = netdev_priv(netdev);
5362 struct ice_vsi *vsi = np->vsi;
5363 struct ice_pf *pf = vsi->back;
5364 struct ice_hw *hw = &pf->hw;
5365 struct sockaddr *addr = pi;
5366 u8 old_mac[ETH_ALEN];
5371 mac = (u8 *)addr->sa_data;
5373 if (!is_valid_ether_addr(mac))
5374 return -EADDRNOTAVAIL;
5376 if (ether_addr_equal(netdev->dev_addr, mac)) {
5377 netdev_dbg(netdev, "already using mac %pM\n", mac);
5381 if (test_bit(ICE_DOWN, pf->state) ||
5382 ice_is_reset_in_progress(pf->state)) {
5383 netdev_err(netdev, "can't set mac %pM. device not ready\n",
5388 if (ice_chnl_dmac_fltr_cnt(pf)) {
5389 netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5394 netif_addr_lock_bh(netdev);
5395 ether_addr_copy(old_mac, netdev->dev_addr);
5396 /* change the netdev's MAC address */
5397 eth_hw_addr_set(netdev, mac);
5398 netif_addr_unlock_bh(netdev);
5400 /* Clean up old MAC filter. Not an error if old filter doesn't exist */
5401 err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5402 if (err && err != -ENOENT) {
5403 err = -EADDRNOTAVAIL;
5404 goto err_update_filters;
5407 /* Add filter for new MAC. If filter exists, return success */
5408 err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5410 /* Although this MAC filter is already present in hardware it's
5411 * possible in some cases (e.g. bonding) that dev_addr was
5412 * modified outside of the driver and needs to be restored back
5415 netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5417 /* error if the new filter addition failed */
5418 err = -EADDRNOTAVAIL;
5422 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5424 netif_addr_lock_bh(netdev);
5425 eth_hw_addr_set(netdev, old_mac);
5426 netif_addr_unlock_bh(netdev);
5430 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5433 /* write new MAC address to the firmware */
5434 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5435 err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5437 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5444 * ice_set_rx_mode - NDO callback to set the netdev filters
5445 * @netdev: network interface device structure
5447 static void ice_set_rx_mode(struct net_device *netdev)
5449 struct ice_netdev_priv *np = netdev_priv(netdev);
5450 struct ice_vsi *vsi = np->vsi;
5455 /* Set the flags to synchronize filters
5456 * ndo_set_rx_mode may be triggered even without a change in netdev
5459 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5460 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5461 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5463 /* schedule our worker thread which will take care of
5464 * applying the new filter changes
5466 ice_service_task_schedule(vsi->back);
5470 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5471 * @netdev: network interface device structure
5472 * @queue_index: Queue ID
5473 * @maxrate: maximum bandwidth in Mbps
5476 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5478 struct ice_netdev_priv *np = netdev_priv(netdev);
5479 struct ice_vsi *vsi = np->vsi;
5484 /* Validate maxrate requested is within permitted range */
5485 if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5486 netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5487 maxrate, queue_index);
5491 q_handle = vsi->tx_rings[queue_index]->q_handle;
5492 tc = ice_dcb_get_tc(vsi, queue_index);
5494 /* Set BW back to default, when user set maxrate to 0 */
5496 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5497 q_handle, ICE_MAX_BW);
5499 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5500 q_handle, ICE_MAX_BW, maxrate * 1000);
5502 netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5509 * ice_fdb_add - add an entry to the hardware database
5510 * @ndm: the input from the stack
5511 * @tb: pointer to array of nladdr (unused)
5512 * @dev: the net device pointer
5513 * @addr: the MAC address entry being added
5515 * @flags: instructions from stack about fdb operation
5516 * @extack: netlink extended ack
5519 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5520 struct net_device *dev, const unsigned char *addr, u16 vid,
5521 u16 flags, struct netlink_ext_ack __always_unused *extack)
5526 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5529 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5530 netdev_err(dev, "FDB only supports static addresses\n");
5534 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5535 err = dev_uc_add_excl(dev, addr);
5536 else if (is_multicast_ether_addr(addr))
5537 err = dev_mc_add_excl(dev, addr);
5541 /* Only return duplicate errors if NLM_F_EXCL is set */
5542 if (err == -EEXIST && !(flags & NLM_F_EXCL))
5549 * ice_fdb_del - delete an entry from the hardware database
5550 * @ndm: the input from the stack
5551 * @tb: pointer to array of nladdr (unused)
5552 * @dev: the net device pointer
5553 * @addr: the MAC address entry being added
5557 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5558 struct net_device *dev, const unsigned char *addr,
5559 __always_unused u16 vid)
5563 if (ndm->ndm_state & NUD_PERMANENT) {
5564 netdev_err(dev, "FDB only supports static addresses\n");
5568 if (is_unicast_ether_addr(addr))
5569 err = dev_uc_del(dev, addr);
5570 else if (is_multicast_ether_addr(addr))
5571 err = dev_mc_del(dev, addr);
5579 * ice_set_features - set the netdev feature flags
5580 * @netdev: ptr to the netdev being adjusted
5581 * @features: the feature set that the stack is suggesting
5584 ice_set_features(struct net_device *netdev, netdev_features_t features)
5586 struct ice_netdev_priv *np = netdev_priv(netdev);
5587 struct ice_vsi *vsi = np->vsi;
5588 struct ice_pf *pf = vsi->back;
5591 /* Don't set any netdev advanced features with device in Safe Mode */
5592 if (ice_is_safe_mode(vsi->back)) {
5593 dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5597 /* Do not change setting during reset */
5598 if (ice_is_reset_in_progress(pf->state)) {
5599 dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5603 /* Multiple features can be changed in one call so keep features in
5604 * separate if/else statements to guarantee each feature is checked
5606 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5607 ice_vsi_manage_rss_lut(vsi, true);
5608 else if (!(features & NETIF_F_RXHASH) &&
5609 netdev->features & NETIF_F_RXHASH)
5610 ice_vsi_manage_rss_lut(vsi, false);
5612 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5613 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5614 ret = ice_vsi_manage_vlan_stripping(vsi, true);
5615 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5616 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5617 ret = ice_vsi_manage_vlan_stripping(vsi, false);
5619 if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5620 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5621 ret = ice_vsi_manage_vlan_insertion(vsi);
5622 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5623 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5624 ret = ice_vsi_manage_vlan_insertion(vsi);
5626 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5627 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5628 ret = ice_cfg_vlan_pruning(vsi, true);
5629 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5630 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5631 ret = ice_cfg_vlan_pruning(vsi, false);
5633 if ((features & NETIF_F_NTUPLE) &&
5634 !(netdev->features & NETIF_F_NTUPLE)) {
5635 ice_vsi_manage_fdir(vsi, true);
5637 } else if (!(features & NETIF_F_NTUPLE) &&
5638 (netdev->features & NETIF_F_NTUPLE)) {
5639 ice_vsi_manage_fdir(vsi, false);
5640 ice_clear_arfs(vsi);
5643 /* don't turn off hw_tc_offload when ADQ is already enabled */
5644 if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
5645 dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
5649 if ((features & NETIF_F_HW_TC) &&
5650 !(netdev->features & NETIF_F_HW_TC))
5651 set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
5653 clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
5659 * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5660 * @vsi: VSI to setup VLAN properties for
5662 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5666 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5667 ret = ice_vsi_manage_vlan_stripping(vsi, true);
5668 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5669 ret = ice_vsi_manage_vlan_insertion(vsi);
5675 * ice_vsi_cfg - Setup the VSI
5676 * @vsi: the VSI being configured
5678 * Return 0 on success and negative value on error
5680 int ice_vsi_cfg(struct ice_vsi *vsi)
5685 ice_set_rx_mode(vsi->netdev);
5687 err = ice_vsi_vlan_setup(vsi);
5692 ice_vsi_cfg_dcb_rings(vsi);
5694 err = ice_vsi_cfg_lan_txqs(vsi);
5695 if (!err && ice_is_xdp_ena_vsi(vsi))
5696 err = ice_vsi_cfg_xdp_txqs(vsi);
5698 err = ice_vsi_cfg_rxqs(vsi);
5703 /* THEORY OF MODERATION:
5704 * The ice driver hardware works differently than the hardware that DIMLIB was
5705 * originally made for. ice hardware doesn't have packet count limits that
5706 * can trigger an interrupt, but it *does* have interrupt rate limit support,
5707 * which is hard-coded to a limit of 250,000 ints/second.
5708 * If not using dynamic moderation, the INTRL value can be modified
5709 * by ethtool rx-usecs-high.
5712 /* the throttle rate for interrupts, basically worst case delay before
5713 * an initial interrupt fires, value is stored in microseconds.
5718 /* Make a different profile for Rx that doesn't allow quite so aggressive
5719 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
5722 static const struct ice_dim rx_profile[] = {
5723 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
5724 {8}, /* 125,000 ints/s */
5725 {16}, /* 62,500 ints/s */
5726 {62}, /* 16,129 ints/s */
5727 {126} /* 7,936 ints/s */
5730 /* The transmit profile, which has the same sorts of values
5731 * as the previous struct
5733 static const struct ice_dim tx_profile[] = {
5734 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
5735 {8}, /* 125,000 ints/s */
5736 {40}, /* 16,125 ints/s */
5737 {128}, /* 7,812 ints/s */
5738 {256} /* 3,906 ints/s */
5741 static void ice_tx_dim_work(struct work_struct *work)
5743 struct ice_ring_container *rc;
5747 dim = container_of(work, struct dim, work);
5748 rc = (struct ice_ring_container *)dim->priv;
5750 WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
5752 /* look up the values in our local table */
5753 itr = tx_profile[dim->profile_ix].itr;
5755 ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
5756 ice_write_itr(rc, itr);
5758 dim->state = DIM_START_MEASURE;
5761 static void ice_rx_dim_work(struct work_struct *work)
5763 struct ice_ring_container *rc;
5767 dim = container_of(work, struct dim, work);
5768 rc = (struct ice_ring_container *)dim->priv;
5770 WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
5772 /* look up the values in our local table */
5773 itr = rx_profile[dim->profile_ix].itr;
5775 ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
5776 ice_write_itr(rc, itr);
5778 dim->state = DIM_START_MEASURE;
5781 #define ICE_DIM_DEFAULT_PROFILE_IX 1
5784 * ice_init_moderation - set up interrupt moderation
5785 * @q_vector: the vector containing rings to be configured
5787 * Set up interrupt moderation registers, with the intent to do the right thing
5788 * when called from reset or from probe, and whether or not dynamic moderation
5789 * is enabled or not. Take special care to write all the registers in both
5790 * dynamic moderation mode or not in order to make sure hardware is in a known
5793 static void ice_init_moderation(struct ice_q_vector *q_vector)
5795 struct ice_ring_container *rc;
5796 bool tx_dynamic, rx_dynamic;
5799 INIT_WORK(&rc->dim.work, ice_tx_dim_work);
5800 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5801 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
5803 tx_dynamic = ITR_IS_DYNAMIC(rc);
5805 /* set the initial TX ITR to match the above */
5806 ice_write_itr(rc, tx_dynamic ?
5807 tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
5810 INIT_WORK(&rc->dim.work, ice_rx_dim_work);
5811 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5812 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
5814 rx_dynamic = ITR_IS_DYNAMIC(rc);
5816 /* set the initial RX ITR to match the above */
5817 ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
5820 ice_set_q_vector_intrl(q_vector);
5824 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5825 * @vsi: the VSI being configured
5827 static void ice_napi_enable_all(struct ice_vsi *vsi)
5834 ice_for_each_q_vector(vsi, q_idx) {
5835 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5837 ice_init_moderation(q_vector);
5839 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
5840 napi_enable(&q_vector->napi);
5845 * ice_up_complete - Finish the last steps of bringing up a connection
5846 * @vsi: The VSI being configured
5848 * Return 0 on success and negative value on error
5850 static int ice_up_complete(struct ice_vsi *vsi)
5852 struct ice_pf *pf = vsi->back;
5855 ice_vsi_cfg_msix(vsi);
5857 /* Enable only Rx rings, Tx rings were enabled by the FW when the
5858 * Tx queue group list was configured and the context bits were
5859 * programmed using ice_vsi_cfg_txqs
5861 err = ice_vsi_start_all_rx_rings(vsi);
5865 clear_bit(ICE_VSI_DOWN, vsi->state);
5866 ice_napi_enable_all(vsi);
5867 ice_vsi_ena_irq(vsi);
5869 if (vsi->port_info &&
5870 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5872 ice_print_link_msg(vsi, true);
5873 netif_tx_start_all_queues(vsi->netdev);
5874 netif_carrier_on(vsi->netdev);
5875 if (!ice_is_e810(&pf->hw))
5876 ice_ptp_link_change(pf, pf->hw.pf_id, true);
5879 /* clear this now, and the first stats read will be used as baseline */
5880 vsi->stat_offsets_loaded = false;
5882 ice_service_task_schedule(pf);
5888 * ice_up - Bring the connection back up after being down
5889 * @vsi: VSI being configured
5891 int ice_up(struct ice_vsi *vsi)
5895 err = ice_vsi_cfg(vsi);
5897 err = ice_up_complete(vsi);
5903 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5904 * @syncp: pointer to u64_stats_sync
5905 * @stats: stats that pkts and bytes count will be taken from
5906 * @pkts: packets stats counter
5907 * @bytes: bytes stats counter
5909 * This function fetches stats from the ring considering the atomic operations
5910 * that needs to be performed to read u64 values in 32 bit machine.
5913 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp, struct ice_q_stats stats,
5914 u64 *pkts, u64 *bytes)
5919 start = u64_stats_fetch_begin_irq(syncp);
5921 *bytes = stats.bytes;
5922 } while (u64_stats_fetch_retry_irq(syncp, start));
5926 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5927 * @vsi: the VSI to be updated
5928 * @vsi_stats: the stats struct to be updated
5929 * @rings: rings to work on
5930 * @count: number of rings
5933 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
5934 struct rtnl_link_stats64 *vsi_stats,
5935 struct ice_tx_ring **rings, u16 count)
5939 for (i = 0; i < count; i++) {
5940 struct ice_tx_ring *ring;
5941 u64 pkts = 0, bytes = 0;
5943 ring = READ_ONCE(rings[i]);
5945 ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
5946 vsi_stats->tx_packets += pkts;
5947 vsi_stats->tx_bytes += bytes;
5948 vsi->tx_restart += ring->tx_stats.restart_q;
5949 vsi->tx_busy += ring->tx_stats.tx_busy;
5950 vsi->tx_linearize += ring->tx_stats.tx_linearize;
5955 * ice_update_vsi_ring_stats - Update VSI stats counters
5956 * @vsi: the VSI to be updated
5958 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5960 struct rtnl_link_stats64 *vsi_stats;
5964 vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
5968 /* reset non-netdev (extended) stats */
5969 vsi->tx_restart = 0;
5971 vsi->tx_linearize = 0;
5972 vsi->rx_buf_failed = 0;
5973 vsi->rx_page_failed = 0;
5977 /* update Tx rings counters */
5978 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
5981 /* update Rx rings counters */
5982 ice_for_each_rxq(vsi, i) {
5983 struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
5985 ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
5986 vsi_stats->rx_packets += pkts;
5987 vsi_stats->rx_bytes += bytes;
5988 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5989 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5992 /* update XDP Tx rings counters */
5993 if (ice_is_xdp_ena_vsi(vsi))
5994 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
5999 vsi->net_stats.tx_packets = vsi_stats->tx_packets;
6000 vsi->net_stats.tx_bytes = vsi_stats->tx_bytes;
6001 vsi->net_stats.rx_packets = vsi_stats->rx_packets;
6002 vsi->net_stats.rx_bytes = vsi_stats->rx_bytes;
6008 * ice_update_vsi_stats - Update VSI stats counters
6009 * @vsi: the VSI to be updated
6011 void ice_update_vsi_stats(struct ice_vsi *vsi)
6013 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6014 struct ice_eth_stats *cur_es = &vsi->eth_stats;
6015 struct ice_pf *pf = vsi->back;
6017 if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6018 test_bit(ICE_CFG_BUSY, pf->state))
6021 /* get stats as recorded by Tx/Rx rings */
6022 ice_update_vsi_ring_stats(vsi);
6024 /* get VSI stats as recorded by the hardware */
6025 ice_update_eth_stats(vsi);
6027 cur_ns->tx_errors = cur_es->tx_errors;
6028 cur_ns->rx_dropped = cur_es->rx_discards;
6029 cur_ns->tx_dropped = cur_es->tx_discards;
6030 cur_ns->multicast = cur_es->rx_multicast;
6032 /* update some more netdev stats if this is main VSI */
6033 if (vsi->type == ICE_VSI_PF) {
6034 cur_ns->rx_crc_errors = pf->stats.crc_errors;
6035 cur_ns->rx_errors = pf->stats.crc_errors +
6036 pf->stats.illegal_bytes +
6037 pf->stats.rx_len_errors +
6038 pf->stats.rx_undersize +
6039 pf->hw_csum_rx_error +
6040 pf->stats.rx_jabber +
6041 pf->stats.rx_fragments +
6042 pf->stats.rx_oversize;
6043 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6044 /* record drops from the port level */
6045 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6050 * ice_update_pf_stats - Update PF port stats counters
6051 * @pf: PF whose stats needs to be updated
6053 void ice_update_pf_stats(struct ice_pf *pf)
6055 struct ice_hw_port_stats *prev_ps, *cur_ps;
6056 struct ice_hw *hw = &pf->hw;
6060 port = hw->port_info->lport;
6061 prev_ps = &pf->stats_prev;
6062 cur_ps = &pf->stats;
6064 ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6065 &prev_ps->eth.rx_bytes,
6066 &cur_ps->eth.rx_bytes);
6068 ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6069 &prev_ps->eth.rx_unicast,
6070 &cur_ps->eth.rx_unicast);
6072 ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6073 &prev_ps->eth.rx_multicast,
6074 &cur_ps->eth.rx_multicast);
6076 ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6077 &prev_ps->eth.rx_broadcast,
6078 &cur_ps->eth.rx_broadcast);
6080 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6081 &prev_ps->eth.rx_discards,
6082 &cur_ps->eth.rx_discards);
6084 ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6085 &prev_ps->eth.tx_bytes,
6086 &cur_ps->eth.tx_bytes);
6088 ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6089 &prev_ps->eth.tx_unicast,
6090 &cur_ps->eth.tx_unicast);
6092 ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6093 &prev_ps->eth.tx_multicast,
6094 &cur_ps->eth.tx_multicast);
6096 ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6097 &prev_ps->eth.tx_broadcast,
6098 &cur_ps->eth.tx_broadcast);
6100 ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6101 &prev_ps->tx_dropped_link_down,
6102 &cur_ps->tx_dropped_link_down);
6104 ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6105 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6107 ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6108 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6110 ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6111 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6113 ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6114 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6116 ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6117 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6119 ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6120 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6122 ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6123 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6125 ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6126 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6128 ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6129 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6131 ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6132 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6134 ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6135 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6137 ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6138 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6140 ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6141 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6143 ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6144 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6146 fd_ctr_base = hw->fd_ctr_base;
6148 ice_stat_update40(hw,
6149 GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6150 pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6151 &cur_ps->fd_sb_match);
6152 ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6153 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6155 ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6156 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6158 ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6159 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6161 ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6162 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6164 ice_update_dcb_stats(pf);
6166 ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6167 &prev_ps->crc_errors, &cur_ps->crc_errors);
6169 ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6170 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6172 ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6173 &prev_ps->mac_local_faults,
6174 &cur_ps->mac_local_faults);
6176 ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6177 &prev_ps->mac_remote_faults,
6178 &cur_ps->mac_remote_faults);
6180 ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6181 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6183 ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6184 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6186 ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6187 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6189 ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6190 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6192 ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6193 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6195 cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6197 pf->stat_prev_loaded = true;
6201 * ice_get_stats64 - get statistics for network device structure
6202 * @netdev: network interface device structure
6203 * @stats: main device statistics structure
6206 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6208 struct ice_netdev_priv *np = netdev_priv(netdev);
6209 struct rtnl_link_stats64 *vsi_stats;
6210 struct ice_vsi *vsi = np->vsi;
6212 vsi_stats = &vsi->net_stats;
6214 if (!vsi->num_txq || !vsi->num_rxq)
6217 /* netdev packet/byte stats come from ring counter. These are obtained
6218 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6219 * But, only call the update routine and read the registers if VSI is
6222 if (!test_bit(ICE_VSI_DOWN, vsi->state))
6223 ice_update_vsi_ring_stats(vsi);
6224 stats->tx_packets = vsi_stats->tx_packets;
6225 stats->tx_bytes = vsi_stats->tx_bytes;
6226 stats->rx_packets = vsi_stats->rx_packets;
6227 stats->rx_bytes = vsi_stats->rx_bytes;
6229 /* The rest of the stats can be read from the hardware but instead we
6230 * just return values that the watchdog task has already obtained from
6233 stats->multicast = vsi_stats->multicast;
6234 stats->tx_errors = vsi_stats->tx_errors;
6235 stats->tx_dropped = vsi_stats->tx_dropped;
6236 stats->rx_errors = vsi_stats->rx_errors;
6237 stats->rx_dropped = vsi_stats->rx_dropped;
6238 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6239 stats->rx_length_errors = vsi_stats->rx_length_errors;
6243 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6244 * @vsi: VSI having NAPI disabled
6246 static void ice_napi_disable_all(struct ice_vsi *vsi)
6253 ice_for_each_q_vector(vsi, q_idx) {
6254 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6256 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6257 napi_disable(&q_vector->napi);
6259 cancel_work_sync(&q_vector->tx.dim.work);
6260 cancel_work_sync(&q_vector->rx.dim.work);
6265 * ice_down - Shutdown the connection
6266 * @vsi: The VSI being stopped
6268 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6270 int ice_down(struct ice_vsi *vsi)
6272 int i, tx_err, rx_err, link_err = 0;
6274 WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6276 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6277 if (!ice_is_e810(&vsi->back->hw))
6278 ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6279 netif_carrier_off(vsi->netdev);
6280 netif_tx_disable(vsi->netdev);
6281 } else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6282 ice_eswitch_stop_all_tx_queues(vsi->back);
6285 ice_vsi_dis_irq(vsi);
6287 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6289 netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6290 vsi->vsi_num, tx_err);
6291 if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6292 tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6294 netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6295 vsi->vsi_num, tx_err);
6298 rx_err = ice_vsi_stop_all_rx_rings(vsi);
6300 netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6301 vsi->vsi_num, rx_err);
6303 ice_napi_disable_all(vsi);
6305 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
6306 link_err = ice_force_phys_link_state(vsi, false);
6308 netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
6309 vsi->vsi_num, link_err);
6312 ice_for_each_txq(vsi, i)
6313 ice_clean_tx_ring(vsi->tx_rings[i]);
6315 ice_for_each_rxq(vsi, i)
6316 ice_clean_rx_ring(vsi->rx_rings[i]);
6318 if (tx_err || rx_err || link_err) {
6319 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6320 vsi->vsi_num, vsi->vsw->sw_id);
6328 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
6329 * @vsi: VSI having resources allocated
6331 * Return 0 on success, negative on failure
6333 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
6337 if (!vsi->num_txq) {
6338 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
6343 ice_for_each_txq(vsi, i) {
6344 struct ice_tx_ring *ring = vsi->tx_rings[i];
6350 ring->netdev = vsi->netdev;
6351 err = ice_setup_tx_ring(ring);
6360 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
6361 * @vsi: VSI having resources allocated
6363 * Return 0 on success, negative on failure
6365 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
6369 if (!vsi->num_rxq) {
6370 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
6375 ice_for_each_rxq(vsi, i) {
6376 struct ice_rx_ring *ring = vsi->rx_rings[i];
6382 ring->netdev = vsi->netdev;
6383 err = ice_setup_rx_ring(ring);
6392 * ice_vsi_open_ctrl - open control VSI for use
6393 * @vsi: the VSI to open
6395 * Initialization of the Control VSI
6397 * Returns 0 on success, negative value on error
6399 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
6401 char int_name[ICE_INT_NAME_STR_LEN];
6402 struct ice_pf *pf = vsi->back;
6406 dev = ice_pf_to_dev(pf);
6407 /* allocate descriptors */
6408 err = ice_vsi_setup_tx_rings(vsi);
6412 err = ice_vsi_setup_rx_rings(vsi);
6416 err = ice_vsi_cfg(vsi);
6420 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
6421 dev_driver_string(dev), dev_name(dev));
6422 err = ice_vsi_req_irq_msix(vsi, int_name);
6426 ice_vsi_cfg_msix(vsi);
6428 err = ice_vsi_start_all_rx_rings(vsi);
6430 goto err_up_complete;
6432 clear_bit(ICE_VSI_DOWN, vsi->state);
6433 ice_vsi_ena_irq(vsi);
6440 ice_vsi_free_rx_rings(vsi);
6442 ice_vsi_free_tx_rings(vsi);
6448 * ice_vsi_open - Called when a network interface is made active
6449 * @vsi: the VSI to open
6451 * Initialization of the VSI
6453 * Returns 0 on success, negative value on error
6455 int ice_vsi_open(struct ice_vsi *vsi)
6457 char int_name[ICE_INT_NAME_STR_LEN];
6458 struct ice_pf *pf = vsi->back;
6461 /* allocate descriptors */
6462 err = ice_vsi_setup_tx_rings(vsi);
6466 err = ice_vsi_setup_rx_rings(vsi);
6470 err = ice_vsi_cfg(vsi);
6474 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
6475 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
6476 err = ice_vsi_req_irq_msix(vsi, int_name);
6480 if (vsi->type == ICE_VSI_PF) {
6481 /* Notify the stack of the actual queue counts. */
6482 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
6486 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
6491 err = ice_up_complete(vsi);
6493 goto err_up_complete;
6500 ice_vsi_free_irq(vsi);
6502 ice_vsi_free_rx_rings(vsi);
6504 ice_vsi_free_tx_rings(vsi);
6510 * ice_vsi_release_all - Delete all VSIs
6511 * @pf: PF from which all VSIs are being removed
6513 static void ice_vsi_release_all(struct ice_pf *pf)
6520 ice_for_each_vsi(pf, i) {
6524 if (pf->vsi[i]->type == ICE_VSI_CHNL)
6527 err = ice_vsi_release(pf->vsi[i]);
6529 dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
6530 i, err, pf->vsi[i]->vsi_num);
6535 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
6536 * @pf: pointer to the PF instance
6537 * @type: VSI type to rebuild
6539 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
6541 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
6543 struct device *dev = ice_pf_to_dev(pf);
6546 ice_for_each_vsi(pf, i) {
6547 struct ice_vsi *vsi = pf->vsi[i];
6549 if (!vsi || vsi->type != type)
6552 /* rebuild the VSI */
6553 err = ice_vsi_rebuild(vsi, true);
6555 dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
6556 err, vsi->idx, ice_vsi_type_str(type));
6560 /* replay filters for the VSI */
6561 err = ice_replay_vsi(&pf->hw, vsi->idx);
6563 dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
6564 err, vsi->idx, ice_vsi_type_str(type));
6568 /* Re-map HW VSI number, using VSI handle that has been
6569 * previously validated in ice_replay_vsi() call above
6571 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
6573 /* enable the VSI */
6574 err = ice_ena_vsi(vsi, false);
6576 dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
6577 err, vsi->idx, ice_vsi_type_str(type));
6581 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
6582 ice_vsi_type_str(type));
6589 * ice_update_pf_netdev_link - Update PF netdev link status
6590 * @pf: pointer to the PF instance
6592 static void ice_update_pf_netdev_link(struct ice_pf *pf)
6597 ice_for_each_vsi(pf, i) {
6598 struct ice_vsi *vsi = pf->vsi[i];
6600 if (!vsi || vsi->type != ICE_VSI_PF)
6603 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
6605 netif_carrier_on(pf->vsi[i]->netdev);
6606 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
6608 netif_carrier_off(pf->vsi[i]->netdev);
6609 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
6615 * ice_rebuild - rebuild after reset
6616 * @pf: PF to rebuild
6617 * @reset_type: type of reset
6619 * Do not rebuild VF VSI in this flow because that is already handled via
6620 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
6621 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
6622 * to reset/rebuild all the VF VSI twice.
6624 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
6626 struct device *dev = ice_pf_to_dev(pf);
6627 struct ice_hw *hw = &pf->hw;
6630 if (test_bit(ICE_DOWN, pf->state))
6631 goto clear_recovery;
6633 dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
6635 if (reset_type == ICE_RESET_EMPR) {
6636 /* If an EMP reset has occurred, any previously pending flash
6637 * update will have completed. We no longer know whether or
6638 * not the NVM update EMP reset is restricted.
6640 pf->fw_emp_reset_disabled = false;
6643 err = ice_init_all_ctrlq(hw);
6645 dev_err(dev, "control queues init failed %d\n", err);
6646 goto err_init_ctrlq;
6649 /* if DDP was previously loaded successfully */
6650 if (!ice_is_safe_mode(pf)) {
6651 /* reload the SW DB of filter tables */
6652 if (reset_type == ICE_RESET_PFR)
6653 ice_fill_blk_tbls(hw);
6655 /* Reload DDP Package after CORER/GLOBR reset */
6656 ice_load_pkg(NULL, pf);
6659 err = ice_clear_pf_cfg(hw);
6661 dev_err(dev, "clear PF configuration failed %d\n", err);
6662 goto err_init_ctrlq;
6665 if (pf->first_sw->dflt_vsi_ena)
6666 dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
6667 /* clear the default VSI configuration if it exists */
6668 pf->first_sw->dflt_vsi = NULL;
6669 pf->first_sw->dflt_vsi_ena = false;
6671 ice_clear_pxe_mode(hw);
6673 err = ice_init_nvm(hw);
6675 dev_err(dev, "ice_init_nvm failed %d\n", err);
6676 goto err_init_ctrlq;
6679 err = ice_get_caps(hw);
6681 dev_err(dev, "ice_get_caps failed %d\n", err);
6682 goto err_init_ctrlq;
6685 err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
6687 dev_err(dev, "set_mac_cfg failed %d\n", err);
6688 goto err_init_ctrlq;
6691 err = ice_sched_init_port(hw->port_info);
6693 goto err_sched_init_port;
6695 /* start misc vector */
6696 err = ice_req_irq_msix_misc(pf);
6698 dev_err(dev, "misc vector setup failed: %d\n", err);
6699 goto err_sched_init_port;
6702 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6703 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
6704 if (!rd32(hw, PFQF_FD_SIZE)) {
6705 u16 unused, guar, b_effort;
6707 guar = hw->func_caps.fd_fltr_guar;
6708 b_effort = hw->func_caps.fd_fltr_best_effort;
6710 /* force guaranteed filter pool for PF */
6711 ice_alloc_fd_guar_item(hw, &unused, guar);
6712 /* force shared filter pool for PF */
6713 ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6717 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6718 ice_dcb_rebuild(pf);
6720 /* If the PF previously had enabled PTP, PTP init needs to happen before
6721 * the VSI rebuild. If not, this causes the PTP link status events to
6724 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
6727 /* rebuild PF VSI */
6728 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6730 dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6731 goto err_vsi_rebuild;
6734 /* configure PTP timestamping after VSI rebuild */
6735 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
6736 ice_ptp_cfg_timestamp(pf, false);
6738 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
6740 dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
6741 goto err_vsi_rebuild;
6744 if (reset_type == ICE_RESET_PFR) {
6745 err = ice_rebuild_channels(pf);
6747 dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
6749 goto err_vsi_rebuild;
6753 /* If Flow Director is active */
6754 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6755 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6757 dev_err(dev, "control VSI rebuild failed: %d\n", err);
6758 goto err_vsi_rebuild;
6761 /* replay HW Flow Director recipes */
6763 ice_fdir_replay_flows(hw);
6765 /* replay Flow Director filters */
6766 ice_fdir_replay_fltrs(pf);
6768 ice_rebuild_arfs(pf);
6771 ice_update_pf_netdev_link(pf);
6773 /* tell the firmware we are up */
6774 err = ice_send_version(pf);
6776 dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
6778 goto err_vsi_rebuild;
6781 ice_replay_post(hw);
6783 /* if we get here, reset flow is successful */
6784 clear_bit(ICE_RESET_FAILED, pf->state);
6786 ice_plug_aux_dev(pf);
6790 err_sched_init_port:
6791 ice_sched_cleanup_all(hw);
6793 ice_shutdown_all_ctrlq(hw);
6794 set_bit(ICE_RESET_FAILED, pf->state);
6796 /* set this bit in PF state to control service task scheduling */
6797 set_bit(ICE_NEEDS_RESTART, pf->state);
6798 dev_err(dev, "Rebuild failed, unload and reload driver\n");
6802 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6803 * @vsi: Pointer to VSI structure
6805 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6807 if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6808 return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6810 return ICE_RXBUF_3072;
6814 * ice_change_mtu - NDO callback to change the MTU
6815 * @netdev: network interface device structure
6816 * @new_mtu: new value for maximum frame size
6818 * Returns 0 on success, negative on failure
6820 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6822 struct ice_netdev_priv *np = netdev_priv(netdev);
6823 struct ice_vsi *vsi = np->vsi;
6824 struct ice_pf *pf = vsi->back;
6825 struct iidc_event *event;
6829 if (new_mtu == (int)netdev->mtu) {
6830 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6834 if (ice_is_xdp_ena_vsi(vsi)) {
6835 int frame_size = ice_max_xdp_frame_size(vsi);
6837 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6838 netdev_err(netdev, "max MTU for XDP usage is %d\n",
6839 frame_size - ICE_ETH_PKT_HDR_PAD);
6844 /* if a reset is in progress, wait for some time for it to complete */
6846 if (ice_is_reset_in_progress(pf->state)) {
6848 usleep_range(1000, 2000);
6853 } while (count < 100);
6856 netdev_err(netdev, "can't change MTU. Device is busy\n");
6860 event = kzalloc(sizeof(*event), GFP_KERNEL);
6864 set_bit(IIDC_EVENT_BEFORE_MTU_CHANGE, event->type);
6865 ice_send_event_to_aux(pf, event);
6866 clear_bit(IIDC_EVENT_BEFORE_MTU_CHANGE, event->type);
6868 netdev->mtu = (unsigned int)new_mtu;
6870 /* if VSI is up, bring it down and then back up */
6871 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6872 err = ice_down(vsi);
6874 netdev_err(netdev, "change MTU if_down err %d\n", err);
6880 netdev_err(netdev, "change MTU if_up err %d\n", err);
6885 netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6887 set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
6888 ice_send_event_to_aux(pf, event);
6895 * ice_eth_ioctl - Access the hwtstamp interface
6896 * @netdev: network interface device structure
6897 * @ifr: interface request data
6898 * @cmd: ioctl command
6900 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6902 struct ice_netdev_priv *np = netdev_priv(netdev);
6903 struct ice_pf *pf = np->vsi->back;
6907 return ice_ptp_get_ts_config(pf, ifr);
6909 return ice_ptp_set_ts_config(pf, ifr);
6916 * ice_aq_str - convert AQ err code to a string
6917 * @aq_err: the AQ error code to convert
6919 const char *ice_aq_str(enum ice_aq_err aq_err)
6924 case ICE_AQ_RC_EPERM:
6925 return "ICE_AQ_RC_EPERM";
6926 case ICE_AQ_RC_ENOENT:
6927 return "ICE_AQ_RC_ENOENT";
6928 case ICE_AQ_RC_ENOMEM:
6929 return "ICE_AQ_RC_ENOMEM";
6930 case ICE_AQ_RC_EBUSY:
6931 return "ICE_AQ_RC_EBUSY";
6932 case ICE_AQ_RC_EEXIST:
6933 return "ICE_AQ_RC_EEXIST";
6934 case ICE_AQ_RC_EINVAL:
6935 return "ICE_AQ_RC_EINVAL";
6936 case ICE_AQ_RC_ENOSPC:
6937 return "ICE_AQ_RC_ENOSPC";
6938 case ICE_AQ_RC_ENOSYS:
6939 return "ICE_AQ_RC_ENOSYS";
6940 case ICE_AQ_RC_EMODE:
6941 return "ICE_AQ_RC_EMODE";
6942 case ICE_AQ_RC_ENOSEC:
6943 return "ICE_AQ_RC_ENOSEC";
6944 case ICE_AQ_RC_EBADSIG:
6945 return "ICE_AQ_RC_EBADSIG";
6946 case ICE_AQ_RC_ESVN:
6947 return "ICE_AQ_RC_ESVN";
6948 case ICE_AQ_RC_EBADMAN:
6949 return "ICE_AQ_RC_EBADMAN";
6950 case ICE_AQ_RC_EBADBUF:
6951 return "ICE_AQ_RC_EBADBUF";
6954 return "ICE_AQ_RC_UNKNOWN";
6958 * ice_set_rss_lut - Set RSS LUT
6959 * @vsi: Pointer to VSI structure
6960 * @lut: Lookup table
6961 * @lut_size: Lookup table size
6963 * Returns 0 on success, negative on failure
6965 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
6967 struct ice_aq_get_set_rss_lut_params params = {};
6968 struct ice_hw *hw = &vsi->back->hw;
6974 params.vsi_handle = vsi->idx;
6975 params.lut_size = lut_size;
6976 params.lut_type = vsi->rss_lut_type;
6979 status = ice_aq_set_rss_lut(hw, ¶ms);
6981 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
6982 status, ice_aq_str(hw->adminq.sq_last_status));
6988 * ice_set_rss_key - Set RSS key
6989 * @vsi: Pointer to the VSI structure
6990 * @seed: RSS hash seed
6992 * Returns 0 on success, negative on failure
6994 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
6996 struct ice_hw *hw = &vsi->back->hw;
7002 status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7004 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7005 status, ice_aq_str(hw->adminq.sq_last_status));
7011 * ice_get_rss_lut - Get RSS LUT
7012 * @vsi: Pointer to VSI structure
7013 * @lut: Buffer to store the lookup table entries
7014 * @lut_size: Size of buffer to store the lookup table entries
7016 * Returns 0 on success, negative on failure
7018 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7020 struct ice_aq_get_set_rss_lut_params params = {};
7021 struct ice_hw *hw = &vsi->back->hw;
7027 params.vsi_handle = vsi->idx;
7028 params.lut_size = lut_size;
7029 params.lut_type = vsi->rss_lut_type;
7032 status = ice_aq_get_rss_lut(hw, ¶ms);
7034 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7035 status, ice_aq_str(hw->adminq.sq_last_status));
7041 * ice_get_rss_key - Get RSS key
7042 * @vsi: Pointer to VSI structure
7043 * @seed: Buffer to store the key in
7045 * Returns 0 on success, negative on failure
7047 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7049 struct ice_hw *hw = &vsi->back->hw;
7055 status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7057 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7058 status, ice_aq_str(hw->adminq.sq_last_status));
7064 * ice_bridge_getlink - Get the hardware bridge mode
7067 * @seq: RTNL message seq
7068 * @dev: the netdev being configured
7069 * @filter_mask: filter mask passed in
7070 * @nlflags: netlink flags passed in
7072 * Return the bridge mode (VEB/VEPA)
7075 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7076 struct net_device *dev, u32 filter_mask, int nlflags)
7078 struct ice_netdev_priv *np = netdev_priv(dev);
7079 struct ice_vsi *vsi = np->vsi;
7080 struct ice_pf *pf = vsi->back;
7083 bmode = pf->first_sw->bridge_mode;
7085 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7090 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7091 * @vsi: Pointer to VSI structure
7092 * @bmode: Hardware bridge mode (VEB/VEPA)
7094 * Returns 0 on success, negative on failure
7096 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7098 struct ice_aqc_vsi_props *vsi_props;
7099 struct ice_hw *hw = &vsi->back->hw;
7100 struct ice_vsi_ctx *ctxt;
7103 vsi_props = &vsi->info;
7105 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7109 ctxt->info = vsi->info;
7111 if (bmode == BRIDGE_MODE_VEB)
7112 /* change from VEPA to VEB mode */
7113 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7115 /* change from VEB to VEPA mode */
7116 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7117 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7119 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7121 dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7122 bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7125 /* Update sw flags for book keeping */
7126 vsi_props->sw_flags = ctxt->info.sw_flags;
7134 * ice_bridge_setlink - Set the hardware bridge mode
7135 * @dev: the netdev being configured
7136 * @nlh: RTNL message
7137 * @flags: bridge setlink flags
7138 * @extack: netlink extended ack
7140 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7141 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7142 * not already set for all VSIs connected to this switch. And also update the
7143 * unicast switch filter rules for the corresponding switch of the netdev.
7146 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7147 u16 __always_unused flags,
7148 struct netlink_ext_ack __always_unused *extack)
7150 struct ice_netdev_priv *np = netdev_priv(dev);
7151 struct ice_pf *pf = np->vsi->back;
7152 struct nlattr *attr, *br_spec;
7153 struct ice_hw *hw = &pf->hw;
7154 struct ice_sw *pf_sw;
7155 int rem, v, err = 0;
7157 pf_sw = pf->first_sw;
7158 /* find the attribute in the netlink message */
7159 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7161 nla_for_each_nested(attr, br_spec, rem) {
7164 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7166 mode = nla_get_u16(attr);
7167 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7169 /* Continue if bridge mode is not being flipped */
7170 if (mode == pf_sw->bridge_mode)
7172 /* Iterates through the PF VSI list and update the loopback
7175 ice_for_each_vsi(pf, v) {
7178 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7183 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7184 /* Update the unicast switch filter rules for the corresponding
7185 * switch of the netdev
7187 err = ice_update_sw_rule_bridge_mode(hw);
7189 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7191 ice_aq_str(hw->adminq.sq_last_status));
7192 /* revert hw->evb_veb */
7193 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7197 pf_sw->bridge_mode = mode;
7204 * ice_tx_timeout - Respond to a Tx Hang
7205 * @netdev: network interface device structure
7206 * @txqueue: Tx queue
7208 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7210 struct ice_netdev_priv *np = netdev_priv(netdev);
7211 struct ice_tx_ring *tx_ring = NULL;
7212 struct ice_vsi *vsi = np->vsi;
7213 struct ice_pf *pf = vsi->back;
7216 pf->tx_timeout_count++;
7218 /* Check if PFC is enabled for the TC to which the queue belongs
7219 * to. If yes then Tx timeout is not caused by a hung queue, no
7220 * need to reset and rebuild
7222 if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7223 dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7228 /* now that we have an index, find the tx_ring struct */
7229 ice_for_each_txq(vsi, i)
7230 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7231 if (txqueue == vsi->tx_rings[i]->q_index) {
7232 tx_ring = vsi->tx_rings[i];
7236 /* Reset recovery level if enough time has elapsed after last timeout.
7237 * Also ensure no new reset action happens before next timeout period.
7239 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7240 pf->tx_timeout_recovery_level = 1;
7241 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7242 netdev->watchdog_timeo)))
7246 struct ice_hw *hw = &pf->hw;
7249 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7250 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7251 /* Read interrupt register */
7252 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7254 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7255 vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7256 head, tx_ring->next_to_use, val);
7259 pf->tx_timeout_last_recovery = jiffies;
7260 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7261 pf->tx_timeout_recovery_level, txqueue);
7263 switch (pf->tx_timeout_recovery_level) {
7265 set_bit(ICE_PFR_REQ, pf->state);
7268 set_bit(ICE_CORER_REQ, pf->state);
7271 set_bit(ICE_GLOBR_REQ, pf->state);
7274 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7275 set_bit(ICE_DOWN, pf->state);
7276 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7277 set_bit(ICE_SERVICE_DIS, pf->state);
7281 ice_service_task_schedule(pf);
7282 pf->tx_timeout_recovery_level++;
7286 * ice_setup_tc_cls_flower - flower classifier offloads
7287 * @np: net device to configure
7288 * @filter_dev: device on which filter is added
7289 * @cls_flower: offload data
7292 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7293 struct net_device *filter_dev,
7294 struct flow_cls_offload *cls_flower)
7296 struct ice_vsi *vsi = np->vsi;
7298 if (cls_flower->common.chain_index)
7301 switch (cls_flower->command) {
7302 case FLOW_CLS_REPLACE:
7303 return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7304 case FLOW_CLS_DESTROY:
7305 return ice_del_cls_flower(vsi, cls_flower);
7312 * ice_setup_tc_block_cb - callback handler registered for TC block
7313 * @type: TC SETUP type
7314 * @type_data: TC flower offload data that contains user input
7315 * @cb_priv: netdev private data
7318 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7320 struct ice_netdev_priv *np = cb_priv;
7323 case TC_SETUP_CLSFLOWER:
7324 return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7332 * ice_validate_mqprio_qopt - Validate TCF input parameters
7333 * @vsi: Pointer to VSI
7334 * @mqprio_qopt: input parameters for mqprio queue configuration
7336 * This function validates MQPRIO params, such as qcount (power of 2 wherever
7337 * needed), and make sure user doesn't specify qcount and BW rate limit
7338 * for TCs, which are more than "num_tc"
7341 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
7342 struct tc_mqprio_qopt_offload *mqprio_qopt)
7344 u64 sum_max_rate = 0, sum_min_rate = 0;
7345 int non_power_of_2_qcount = 0;
7346 struct ice_pf *pf = vsi->back;
7347 int max_rss_q_cnt = 0;
7352 if (vsi->type != ICE_VSI_PF)
7355 if (mqprio_qopt->qopt.offset[0] != 0 ||
7356 mqprio_qopt->qopt.num_tc < 1 ||
7357 mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
7360 dev = ice_pf_to_dev(pf);
7361 vsi->ch_rss_size = 0;
7362 num_tc = mqprio_qopt->qopt.num_tc;
7364 for (i = 0; num_tc; i++) {
7365 int qcount = mqprio_qopt->qopt.count[i];
7366 u64 max_rate, min_rate, rem;
7371 if (is_power_of_2(qcount)) {
7372 if (non_power_of_2_qcount &&
7373 qcount > non_power_of_2_qcount) {
7374 dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
7375 qcount, non_power_of_2_qcount);
7378 if (qcount > max_rss_q_cnt)
7379 max_rss_q_cnt = qcount;
7381 if (non_power_of_2_qcount &&
7382 qcount != non_power_of_2_qcount) {
7383 dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
7384 qcount, non_power_of_2_qcount);
7387 if (qcount < max_rss_q_cnt) {
7388 dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
7389 qcount, max_rss_q_cnt);
7392 max_rss_q_cnt = qcount;
7393 non_power_of_2_qcount = qcount;
7396 /* TC command takes input in K/N/Gbps or K/M/Gbit etc but
7397 * converts the bandwidth rate limit into Bytes/s when
7398 * passing it down to the driver. So convert input bandwidth
7399 * from Bytes/s to Kbps
7401 max_rate = mqprio_qopt->max_rate[i];
7402 max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
7403 sum_max_rate += max_rate;
7405 /* min_rate is minimum guaranteed rate and it can't be zero */
7406 min_rate = mqprio_qopt->min_rate[i];
7407 min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
7408 sum_min_rate += min_rate;
7410 if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
7411 dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
7412 min_rate, ICE_MIN_BW_LIMIT);
7416 iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
7418 dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
7419 i, ICE_MIN_BW_LIMIT);
7423 iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
7425 dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
7426 i, ICE_MIN_BW_LIMIT);
7430 /* min_rate can't be more than max_rate, except when max_rate
7431 * is zero (implies max_rate sought is max line rate). In such
7432 * a case min_rate can be more than max.
7434 if (max_rate && min_rate > max_rate) {
7435 dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
7436 min_rate, max_rate);
7440 if (i >= mqprio_qopt->qopt.num_tc - 1)
7442 if (mqprio_qopt->qopt.offset[i + 1] !=
7443 (mqprio_qopt->qopt.offset[i] + qcount))
7447 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7450 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7453 speed = ice_get_link_speed_kbps(vsi);
7454 if (sum_max_rate && sum_max_rate > (u64)speed) {
7455 dev_err(dev, "Invalid max Tx rate(%llu) Kbps > speed(%u) Kbps specified\n",
7456 sum_max_rate, speed);
7459 if (sum_min_rate && sum_min_rate > (u64)speed) {
7460 dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
7461 sum_min_rate, speed);
7465 /* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
7466 vsi->ch_rss_size = max_rss_q_cnt;
7472 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
7473 * @pf: ptr to PF device
7476 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
7478 struct device *dev = ice_pf_to_dev(pf);
7483 if (!(vsi->num_gfltr || vsi->num_bfltr))
7487 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
7488 struct ice_fd_hw_prof *prof;
7492 if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
7493 hw->fdir_prof[flow]->cnt))
7496 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
7497 enum ice_flow_priority prio;
7500 /* add this VSI to FDir profile for this flow */
7501 prio = ICE_FLOW_PRIO_NORMAL;
7502 prof = hw->fdir_prof[flow];
7503 prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
7504 status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
7505 prof->vsi_h[0], vsi->idx,
7506 prio, prof->fdir_seg[tun],
7509 dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
7514 prof->entry_h[prof->cnt][tun] = entry_h;
7517 /* store VSI for filter replay and delete */
7518 prof->vsi_h[prof->cnt] = vsi->idx;
7522 dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
7527 dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
7533 * ice_add_channel - add a channel by adding VSI
7534 * @pf: ptr to PF device
7535 * @sw_id: underlying HW switching element ID
7536 * @ch: ptr to channel structure
7538 * Add a channel (VSI) using add_vsi and queue_map
7540 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
7542 struct device *dev = ice_pf_to_dev(pf);
7543 struct ice_vsi *vsi;
7545 if (ch->type != ICE_VSI_CHNL) {
7546 dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
7550 vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
7551 if (!vsi || vsi->type != ICE_VSI_CHNL) {
7552 dev_err(dev, "create chnl VSI failure\n");
7556 ice_add_vsi_to_fdir(pf, vsi);
7559 ch->vsi_num = vsi->vsi_num;
7560 ch->info.mapping_flags = vsi->info.mapping_flags;
7562 /* set the back pointer of channel for newly created VSI */
7565 memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
7566 sizeof(vsi->info.q_mapping));
7567 memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
7568 sizeof(vsi->info.tc_mapping));
7575 * @vsi: the VSI being setup
7576 * @ch: ptr to channel structure
7578 * Configure channel specific resources such as rings, vector.
7580 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
7584 for (i = 0; i < ch->num_txq; i++) {
7585 struct ice_q_vector *tx_q_vector, *rx_q_vector;
7586 struct ice_ring_container *rc;
7587 struct ice_tx_ring *tx_ring;
7588 struct ice_rx_ring *rx_ring;
7590 tx_ring = vsi->tx_rings[ch->base_q + i];
7591 rx_ring = vsi->rx_rings[ch->base_q + i];
7592 if (!tx_ring || !rx_ring)
7595 /* setup ring being channel enabled */
7599 /* following code block sets up vector specific attributes */
7600 tx_q_vector = tx_ring->q_vector;
7601 rx_q_vector = rx_ring->q_vector;
7602 if (!tx_q_vector && !rx_q_vector)
7606 tx_q_vector->ch = ch;
7607 /* setup Tx and Rx ITR setting if DIM is off */
7608 rc = &tx_q_vector->tx;
7609 if (!ITR_IS_DYNAMIC(rc))
7610 ice_write_itr(rc, rc->itr_setting);
7613 rx_q_vector->ch = ch;
7614 /* setup Tx and Rx ITR setting if DIM is off */
7615 rc = &rx_q_vector->rx;
7616 if (!ITR_IS_DYNAMIC(rc))
7617 ice_write_itr(rc, rc->itr_setting);
7621 /* it is safe to assume that, if channel has non-zero num_t[r]xq, then
7622 * GLINT_ITR register would have written to perform in-context
7623 * update, hence perform flush
7625 if (ch->num_txq || ch->num_rxq)
7626 ice_flush(&vsi->back->hw);
7630 * ice_cfg_chnl_all_res - configure channel resources
7631 * @vsi: pte to main_vsi
7632 * @ch: ptr to channel structure
7634 * This function configures channel specific resources such as flow-director
7635 * counter index, and other resources such as queues, vectors, ITR settings
7638 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
7640 /* configure channel (aka ADQ) resources such as queues, vectors,
7641 * ITR settings for channel specific vectors and anything else
7643 ice_chnl_cfg_res(vsi, ch);
7647 * ice_setup_hw_channel - setup new channel
7648 * @pf: ptr to PF device
7649 * @vsi: the VSI being setup
7650 * @ch: ptr to channel structure
7651 * @sw_id: underlying HW switching element ID
7652 * @type: type of channel to be created (VMDq2/VF)
7654 * Setup new channel (VSI) based on specified type (VMDq2/VF)
7655 * and configures Tx rings accordingly
7658 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
7659 struct ice_channel *ch, u16 sw_id, u8 type)
7661 struct device *dev = ice_pf_to_dev(pf);
7664 ch->base_q = vsi->next_base_q;
7667 ret = ice_add_channel(pf, sw_id, ch);
7669 dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
7673 /* configure/setup ADQ specific resources */
7674 ice_cfg_chnl_all_res(vsi, ch);
7676 /* make sure to update the next_base_q so that subsequent channel's
7677 * (aka ADQ) VSI queue map is correct
7679 vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
7680 dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
7687 * ice_setup_channel - setup new channel using uplink element
7688 * @pf: ptr to PF device
7689 * @vsi: the VSI being setup
7690 * @ch: ptr to channel structure
7692 * Setup new channel (VSI) based on specified type (VMDq2/VF)
7693 * and uplink switching element
7696 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
7697 struct ice_channel *ch)
7699 struct device *dev = ice_pf_to_dev(pf);
7703 if (vsi->type != ICE_VSI_PF) {
7704 dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
7708 sw_id = pf->first_sw->sw_id;
7710 /* create channel (VSI) */
7711 ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
7713 dev_err(dev, "failed to setup hw_channel\n");
7716 dev_dbg(dev, "successfully created channel()\n");
7718 return ch->ch_vsi ? true : false;
7722 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
7723 * @vsi: VSI to be configured
7724 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
7725 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
7728 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
7732 err = ice_set_min_bw_limit(vsi, min_tx_rate);
7736 return ice_set_max_bw_limit(vsi, max_tx_rate);
7740 * ice_create_q_channel - function to create channel
7741 * @vsi: VSI to be configured
7742 * @ch: ptr to channel (it contains channel specific params)
7744 * This function creates channel (VSI) using num_queues specified by user,
7745 * reconfigs RSS if needed.
7747 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
7749 struct ice_pf *pf = vsi->back;
7755 dev = ice_pf_to_dev(pf);
7756 if (!ch->num_txq || !ch->num_rxq) {
7757 dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
7761 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
7762 dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
7763 vsi->cnt_q_avail, ch->num_txq);
7767 if (!ice_setup_channel(pf, vsi, ch)) {
7768 dev_info(dev, "Failed to setup channel\n");
7771 /* configure BW rate limit */
7772 if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
7775 ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
7778 dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
7779 ch->max_tx_rate, ch->ch_vsi->vsi_num);
7781 dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
7782 ch->max_tx_rate, ch->ch_vsi->vsi_num);
7785 vsi->cnt_q_avail -= ch->num_txq;
7791 * ice_rem_all_chnl_fltrs - removes all channel filters
7792 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
7794 * Remove all advanced switch filters only if they are channel specific
7795 * tc-flower based filter
7797 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
7799 struct ice_tc_flower_fltr *fltr;
7800 struct hlist_node *node;
7802 /* to remove all channel filters, iterate an ordered list of filters */
7803 hlist_for_each_entry_safe(fltr, node,
7804 &pf->tc_flower_fltr_list,
7806 struct ice_rule_query_data rule;
7809 /* for now process only channel specific filters */
7810 if (!ice_is_chnl_fltr(fltr))
7813 rule.rid = fltr->rid;
7814 rule.rule_id = fltr->rule_id;
7815 rule.vsi_handle = fltr->dest_id;
7816 status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
7818 if (status == -ENOENT)
7819 dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
7822 dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
7824 } else if (fltr->dest_vsi) {
7825 /* update advanced switch filter count */
7826 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
7827 u32 flags = fltr->flags;
7829 fltr->dest_vsi->num_chnl_fltr--;
7830 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
7831 ICE_TC_FLWR_FIELD_ENC_DST_MAC))
7832 pf->num_dmac_chnl_fltrs--;
7836 hlist_del(&fltr->tc_flower_node);
7842 * ice_remove_q_channels - Remove queue channels for the TCs
7843 * @vsi: VSI to be configured
7844 * @rem_fltr: delete advanced switch filter or not
7846 * Remove queue channels for the TCs
7848 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
7850 struct ice_channel *ch, *ch_tmp;
7851 struct ice_pf *pf = vsi->back;
7854 /* remove all tc-flower based filter if they are channel filters only */
7856 ice_rem_all_chnl_fltrs(pf);
7858 /* remove ntuple filters since queue configuration is being changed */
7859 if (vsi->netdev->features & NETIF_F_NTUPLE) {
7860 struct ice_hw *hw = &pf->hw;
7862 mutex_lock(&hw->fdir_fltr_lock);
7863 ice_fdir_del_all_fltrs(vsi);
7864 mutex_unlock(&hw->fdir_fltr_lock);
7867 /* perform cleanup for channels if they exist */
7868 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
7869 struct ice_vsi *ch_vsi;
7871 list_del(&ch->list);
7872 ch_vsi = ch->ch_vsi;
7878 /* Reset queue contexts */
7879 for (i = 0; i < ch->num_rxq; i++) {
7880 struct ice_tx_ring *tx_ring;
7881 struct ice_rx_ring *rx_ring;
7883 tx_ring = vsi->tx_rings[ch->base_q + i];
7884 rx_ring = vsi->rx_rings[ch->base_q + i];
7887 if (tx_ring->q_vector)
7888 tx_ring->q_vector->ch = NULL;
7892 if (rx_ring->q_vector)
7893 rx_ring->q_vector->ch = NULL;
7897 /* Release FD resources for the channel VSI */
7898 ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
7900 /* clear the VSI from scheduler tree */
7901 ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
7903 /* Delete VSI from FW */
7904 ice_vsi_delete(ch->ch_vsi);
7906 /* Delete VSI from PF and HW VSI arrays */
7907 ice_vsi_clear(ch->ch_vsi);
7909 /* free the channel */
7913 /* clear the channel VSI map which is stored in main VSI */
7914 ice_for_each_chnl_tc(i)
7915 vsi->tc_map_vsi[i] = NULL;
7917 /* reset main VSI's all TC information */
7923 * ice_rebuild_channels - rebuild channel
7926 * Recreate channel VSIs and replay filters
7928 static int ice_rebuild_channels(struct ice_pf *pf)
7930 struct device *dev = ice_pf_to_dev(pf);
7931 struct ice_vsi *main_vsi;
7932 bool rem_adv_fltr = true;
7933 struct ice_channel *ch;
7934 struct ice_vsi *vsi;
7938 main_vsi = ice_get_main_vsi(pf);
7942 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
7943 main_vsi->old_numtc == 1)
7944 return 0; /* nothing to be done */
7946 /* reconfigure main VSI based on old value of TC and cached values
7949 err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
7951 dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
7952 main_vsi->old_ena_tc, main_vsi->vsi_num);
7956 /* rebuild ADQ VSIs */
7957 ice_for_each_vsi(pf, i) {
7958 enum ice_vsi_type type;
7961 if (!vsi || vsi->type != ICE_VSI_CHNL)
7966 /* rebuild ADQ VSI */
7967 err = ice_vsi_rebuild(vsi, true);
7969 dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
7970 ice_vsi_type_str(type), vsi->idx, err);
7974 /* Re-map HW VSI number, using VSI handle that has been
7975 * previously validated in ice_replay_vsi() call above
7977 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7979 /* replay filters for the VSI */
7980 err = ice_replay_vsi(&pf->hw, vsi->idx);
7982 dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
7983 ice_vsi_type_str(type), err, vsi->idx);
7984 rem_adv_fltr = false;
7987 dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
7988 ice_vsi_type_str(type), vsi->idx);
7990 /* store ADQ VSI at correct TC index in main VSI's
7993 main_vsi->tc_map_vsi[tc_idx++] = vsi;
7996 /* ADQ VSI(s) has been rebuilt successfully, so setup
7997 * channel for main VSI's Tx and Rx rings
7999 list_for_each_entry(ch, &main_vsi->ch_list, list) {
8000 struct ice_vsi *ch_vsi;
8002 ch_vsi = ch->ch_vsi;
8006 /* reconfig channel resources */
8007 ice_cfg_chnl_all_res(main_vsi, ch);
8009 /* replay BW rate limit if it is non-zero */
8010 if (!ch->max_tx_rate && !ch->min_tx_rate)
8013 err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8016 dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8017 err, ch->max_tx_rate, ch->min_tx_rate,
8020 dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8021 ch->max_tx_rate, ch->min_tx_rate,
8025 /* reconfig RSS for main VSI */
8026 if (main_vsi->ch_rss_size)
8027 ice_vsi_cfg_rss_lut_key(main_vsi);
8032 ice_remove_q_channels(main_vsi, rem_adv_fltr);
8037 * ice_create_q_channels - Add queue channel for the given TCs
8038 * @vsi: VSI to be configured
8040 * Configures queue channel mapping to the given TCs
8042 static int ice_create_q_channels(struct ice_vsi *vsi)
8044 struct ice_pf *pf = vsi->back;
8045 struct ice_channel *ch;
8048 ice_for_each_chnl_tc(i) {
8049 if (!(vsi->all_enatc & BIT(i)))
8052 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8057 INIT_LIST_HEAD(&ch->list);
8058 ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8059 ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8060 ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8061 ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8062 ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8064 /* convert to Kbits/s */
8065 if (ch->max_tx_rate)
8066 ch->max_tx_rate = div_u64(ch->max_tx_rate,
8067 ICE_BW_KBPS_DIVISOR);
8068 if (ch->min_tx_rate)
8069 ch->min_tx_rate = div_u64(ch->min_tx_rate,
8070 ICE_BW_KBPS_DIVISOR);
8072 ret = ice_create_q_channel(vsi, ch);
8074 dev_err(ice_pf_to_dev(pf),
8075 "failed creating channel TC:%d\n", i);
8079 list_add_tail(&ch->list, &vsi->ch_list);
8080 vsi->tc_map_vsi[i] = ch->ch_vsi;
8081 dev_dbg(ice_pf_to_dev(pf),
8082 "successfully created channel: VSI %pK\n", ch->ch_vsi);
8087 ice_remove_q_channels(vsi, false);
8093 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8094 * @netdev: net device to configure
8095 * @type_data: TC offload data
8097 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8099 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8100 struct ice_netdev_priv *np = netdev_priv(netdev);
8101 struct ice_vsi *vsi = np->vsi;
8102 struct ice_pf *pf = vsi->back;
8103 u16 mode, ena_tc_qdisc = 0;
8104 int cur_txq, cur_rxq;
8109 dev = ice_pf_to_dev(pf);
8110 num_tcf = mqprio_qopt->qopt.num_tc;
8111 hw = mqprio_qopt->qopt.hw;
8112 mode = mqprio_qopt->mode;
8114 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8115 vsi->ch_rss_size = 0;
8116 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8120 /* Generate queue region map for number of TCF requested */
8121 for (i = 0; i < num_tcf; i++)
8122 ena_tc_qdisc |= BIT(i);
8125 case TC_MQPRIO_MODE_CHANNEL:
8127 ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8129 netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8133 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8134 set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8135 /* don't assume state of hw_tc_offload during driver load
8136 * and set the flag for TC flower filter if hw_tc_offload
8139 if (vsi->netdev->features & NETIF_F_HW_TC)
8140 set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8148 /* Requesting same TCF configuration as already enabled */
8149 if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8150 mode != TC_MQPRIO_MODE_CHANNEL)
8153 /* Pause VSI queues */
8154 ice_dis_vsi(vsi, true);
8156 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8157 ice_remove_q_channels(vsi, true);
8159 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8160 vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8162 vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8165 /* logic to rebuild VSI, same like ethtool -L */
8166 u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8168 for (i = 0; i < num_tcf; i++) {
8169 if (!(ena_tc_qdisc & BIT(i)))
8172 offset = vsi->mqprio_qopt.qopt.offset[i];
8173 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8174 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8176 vsi->req_txq = offset + qcount_tx;
8177 vsi->req_rxq = offset + qcount_rx;
8179 /* store away original rss_size info, so that it gets reused
8180 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8181 * determine, what should be the rss_sizefor main VSI
8183 vsi->orig_rss_size = vsi->rss_size;
8186 /* save current values of Tx and Rx queues before calling VSI rebuild
8187 * for fallback option
8189 cur_txq = vsi->num_txq;
8190 cur_rxq = vsi->num_rxq;
8192 /* proceed with rebuild main VSI using correct number of queues */
8193 ret = ice_vsi_rebuild(vsi, false);
8195 /* fallback to current number of queues */
8196 dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8197 vsi->req_txq = cur_txq;
8198 vsi->req_rxq = cur_rxq;
8199 clear_bit(ICE_RESET_FAILED, pf->state);
8200 if (ice_vsi_rebuild(vsi, false)) {
8201 dev_err(dev, "Rebuild of main VSI failed again\n");
8206 vsi->all_numtc = num_tcf;
8207 vsi->all_enatc = ena_tc_qdisc;
8208 ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8210 netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8215 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8216 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8217 u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8219 /* set TC0 rate limit if specified */
8220 if (max_tx_rate || min_tx_rate) {
8221 /* convert to Kbits/s */
8223 max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8225 min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8227 ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8229 dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8230 max_tx_rate, min_tx_rate, vsi->vsi_num);
8232 dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8233 max_tx_rate, min_tx_rate, vsi->vsi_num);
8237 ret = ice_create_q_channels(vsi);
8239 netdev_err(netdev, "failed configuring queue channels\n");
8242 netdev_dbg(netdev, "successfully configured channels\n");
8246 if (vsi->ch_rss_size)
8247 ice_vsi_cfg_rss_lut_key(vsi);
8250 /* if error, reset the all_numtc and all_enatc */
8256 ice_ena_vsi(vsi, true);
8261 static LIST_HEAD(ice_block_cb_list);
8264 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8267 struct ice_netdev_priv *np = netdev_priv(netdev);
8268 struct ice_pf *pf = np->vsi->back;
8272 case TC_SETUP_BLOCK:
8273 return flow_block_cb_setup_simple(type_data,
8275 ice_setup_tc_block_cb,
8277 case TC_SETUP_QDISC_MQPRIO:
8278 /* setup traffic classifier for receive side */
8279 mutex_lock(&pf->tc_mutex);
8280 err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8281 mutex_unlock(&pf->tc_mutex);
8289 static struct ice_indr_block_priv *
8290 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8291 struct net_device *netdev)
8293 struct ice_indr_block_priv *cb_priv;
8295 list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8296 if (!cb_priv->netdev)
8298 if (cb_priv->netdev == netdev)
8305 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
8308 struct ice_indr_block_priv *priv = indr_priv;
8309 struct ice_netdev_priv *np = priv->np;
8312 case TC_SETUP_CLSFLOWER:
8313 return ice_setup_tc_cls_flower(np, priv->netdev,
8314 (struct flow_cls_offload *)
8322 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
8323 struct ice_netdev_priv *np,
8324 struct flow_block_offload *f, void *data,
8325 void (*cleanup)(struct flow_block_cb *block_cb))
8327 struct ice_indr_block_priv *indr_priv;
8328 struct flow_block_cb *block_cb;
8330 if (!ice_is_tunnel_supported(netdev) &&
8331 !(is_vlan_dev(netdev) &&
8332 vlan_dev_real_dev(netdev) == np->vsi->netdev))
8335 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8338 switch (f->command) {
8339 case FLOW_BLOCK_BIND:
8340 indr_priv = ice_indr_block_priv_lookup(np, netdev);
8344 indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
8348 indr_priv->netdev = netdev;
8350 list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
8353 flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
8354 indr_priv, indr_priv,
8355 ice_rep_indr_tc_block_unbind,
8356 f, netdev, sch, data, np,
8359 if (IS_ERR(block_cb)) {
8360 list_del(&indr_priv->list);
8362 return PTR_ERR(block_cb);
8364 flow_block_cb_add(block_cb, f);
8365 list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
8367 case FLOW_BLOCK_UNBIND:
8368 indr_priv = ice_indr_block_priv_lookup(np, netdev);
8372 block_cb = flow_block_cb_lookup(f->block,
8373 ice_indr_setup_block_cb,
8378 flow_indr_block_cb_remove(block_cb, f);
8380 list_del(&block_cb->driver_list);
8389 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
8390 void *cb_priv, enum tc_setup_type type, void *type_data,
8392 void (*cleanup)(struct flow_block_cb *block_cb))
8395 case TC_SETUP_BLOCK:
8396 return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
8405 * ice_open - Called when a network interface becomes active
8406 * @netdev: network interface device structure
8408 * The open entry point is called when a network interface is made
8409 * active by the system (IFF_UP). At this point all resources needed
8410 * for transmit and receive operations are allocated, the interrupt
8411 * handler is registered with the OS, the netdev watchdog is enabled,
8412 * and the stack is notified that the interface is ready.
8414 * Returns 0 on success, negative value on failure
8416 int ice_open(struct net_device *netdev)
8418 struct ice_netdev_priv *np = netdev_priv(netdev);
8419 struct ice_pf *pf = np->vsi->back;
8421 if (ice_is_reset_in_progress(pf->state)) {
8422 netdev_err(netdev, "can't open net device while reset is in progress");
8426 return ice_open_internal(netdev);
8430 * ice_open_internal - Called when a network interface becomes active
8431 * @netdev: network interface device structure
8433 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
8436 * Returns 0 on success, negative value on failure
8438 int ice_open_internal(struct net_device *netdev)
8440 struct ice_netdev_priv *np = netdev_priv(netdev);
8441 struct ice_vsi *vsi = np->vsi;
8442 struct ice_pf *pf = vsi->back;
8443 struct ice_port_info *pi;
8446 if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
8447 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
8451 netif_carrier_off(netdev);
8453 pi = vsi->port_info;
8454 err = ice_update_link_info(pi);
8456 netdev_err(netdev, "Failed to get link info, error %d\n", err);
8460 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
8462 /* Set PHY if there is media, otherwise, turn off PHY */
8463 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
8464 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8465 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
8466 err = ice_init_phy_user_cfg(pi);
8468 netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
8474 err = ice_configure_phy(vsi);
8476 netdev_err(netdev, "Failed to set physical link up, error %d\n",
8481 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8482 ice_set_link(vsi, false);
8485 err = ice_vsi_open(vsi);
8487 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
8488 vsi->vsi_num, vsi->vsw->sw_id);
8490 /* Update existing tunnels information */
8491 udp_tunnel_get_rx_info(netdev);
8497 * ice_stop - Disables a network interface
8498 * @netdev: network interface device structure
8500 * The stop entry point is called when an interface is de-activated by the OS,
8501 * and the netdevice enters the DOWN state. The hardware is still under the
8502 * driver's control, but the netdev interface is disabled.
8504 * Returns success only - not allowed to fail
8506 int ice_stop(struct net_device *netdev)
8508 struct ice_netdev_priv *np = netdev_priv(netdev);
8509 struct ice_vsi *vsi = np->vsi;
8510 struct ice_pf *pf = vsi->back;
8512 if (ice_is_reset_in_progress(pf->state)) {
8513 netdev_err(netdev, "can't stop net device while reset is in progress");
8523 * ice_features_check - Validate encapsulated packet conforms to limits
8525 * @netdev: This port's netdev
8526 * @features: Offload features that the stack believes apply
8528 static netdev_features_t
8529 ice_features_check(struct sk_buff *skb,
8530 struct net_device __always_unused *netdev,
8531 netdev_features_t features)
8533 bool gso = skb_is_gso(skb);
8536 /* No point in doing any of this if neither checksum nor GSO are
8537 * being requested for this frame. We can rule out both by just
8538 * checking for CHECKSUM_PARTIAL
8540 if (skb->ip_summed != CHECKSUM_PARTIAL)
8543 /* We cannot support GSO if the MSS is going to be less than
8544 * 64 bytes. If it is then we need to drop support for GSO.
8546 if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
8547 features &= ~NETIF_F_GSO_MASK;
8549 len = skb_network_offset(skb);
8550 if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
8551 goto out_rm_features;
8553 len = skb_network_header_len(skb);
8554 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8555 goto out_rm_features;
8557 if (skb->encapsulation) {
8558 /* this must work for VXLAN frames AND IPIP/SIT frames, and in
8559 * the case of IPIP frames, the transport header pointer is
8560 * after the inner header! So check to make sure that this
8561 * is a GRE or UDP_TUNNEL frame before doing that math.
8563 if (gso && (skb_shinfo(skb)->gso_type &
8564 (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
8565 len = skb_inner_network_header(skb) -
8566 skb_transport_header(skb);
8567 if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
8568 goto out_rm_features;
8571 len = skb_inner_network_header_len(skb);
8572 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8573 goto out_rm_features;
8578 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
8581 static const struct net_device_ops ice_netdev_safe_mode_ops = {
8582 .ndo_open = ice_open,
8583 .ndo_stop = ice_stop,
8584 .ndo_start_xmit = ice_start_xmit,
8585 .ndo_set_mac_address = ice_set_mac_address,
8586 .ndo_validate_addr = eth_validate_addr,
8587 .ndo_change_mtu = ice_change_mtu,
8588 .ndo_get_stats64 = ice_get_stats64,
8589 .ndo_tx_timeout = ice_tx_timeout,
8590 .ndo_bpf = ice_xdp_safe_mode,
8593 static const struct net_device_ops ice_netdev_ops = {
8594 .ndo_open = ice_open,
8595 .ndo_stop = ice_stop,
8596 .ndo_start_xmit = ice_start_xmit,
8597 .ndo_select_queue = ice_select_queue,
8598 .ndo_features_check = ice_features_check,
8599 .ndo_set_rx_mode = ice_set_rx_mode,
8600 .ndo_set_mac_address = ice_set_mac_address,
8601 .ndo_validate_addr = eth_validate_addr,
8602 .ndo_change_mtu = ice_change_mtu,
8603 .ndo_get_stats64 = ice_get_stats64,
8604 .ndo_set_tx_maxrate = ice_set_tx_maxrate,
8605 .ndo_eth_ioctl = ice_eth_ioctl,
8606 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
8607 .ndo_set_vf_mac = ice_set_vf_mac,
8608 .ndo_get_vf_config = ice_get_vf_cfg,
8609 .ndo_set_vf_trust = ice_set_vf_trust,
8610 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
8611 .ndo_set_vf_link_state = ice_set_vf_link_state,
8612 .ndo_get_vf_stats = ice_get_vf_stats,
8613 .ndo_set_vf_rate = ice_set_vf_bw,
8614 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
8615 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
8616 .ndo_setup_tc = ice_setup_tc,
8617 .ndo_set_features = ice_set_features,
8618 .ndo_bridge_getlink = ice_bridge_getlink,
8619 .ndo_bridge_setlink = ice_bridge_setlink,
8620 .ndo_fdb_add = ice_fdb_add,
8621 .ndo_fdb_del = ice_fdb_del,
8622 #ifdef CONFIG_RFS_ACCEL
8623 .ndo_rx_flow_steer = ice_rx_flow_steer,
8625 .ndo_tx_timeout = ice_tx_timeout,
8627 .ndo_xdp_xmit = ice_xdp_xmit,
8628 .ndo_xsk_wakeup = ice_xsk_wakeup,