cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ice_main.c (245387B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (c) 2018, Intel Corporation. */
      3
      4/* Intel(R) Ethernet Connection E800 Series Linux Driver */
      5
      6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
      7
      8#include <generated/utsrelease.h>
      9#include "ice.h"
     10#include "ice_base.h"
     11#include "ice_lib.h"
     12#include "ice_fltr.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
     18 * ice driver.
     19 */
     20#define CREATE_TRACE_POINTS
     21#include "ice_trace.h"
     22#include "ice_eswitch.h"
     23#include "ice_tc_lib.h"
     24#include "ice_vsi_vlan_ops.h"
     25
     26#define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
     27static const char ice_driver_string[] = DRV_SUMMARY;
     28static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
     29
     30/* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
     31#define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
     32#define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
     33
     34MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
     35MODULE_DESCRIPTION(DRV_SUMMARY);
     36MODULE_LICENSE("GPL v2");
     37MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
     38
     39static int debug = -1;
     40module_param(debug, int, 0644);
     41#ifndef CONFIG_DYNAMIC_DEBUG
     42MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
     43#else
     44MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
     45#endif /* !CONFIG_DYNAMIC_DEBUG */
     46
     47static DEFINE_IDA(ice_aux_ida);
     48DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
     49EXPORT_SYMBOL(ice_xdp_locking_key);
     50
     51/**
     52 * ice_hw_to_dev - Get device pointer from the hardware structure
     53 * @hw: pointer to the device HW structure
     54 *
     55 * Used to access the device pointer from compilation units which can't easily
     56 * include the definition of struct ice_pf without leading to circular header
     57 * dependencies.
     58 */
     59struct device *ice_hw_to_dev(struct ice_hw *hw)
     60{
     61	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
     62
     63	return &pf->pdev->dev;
     64}
     65
     66static struct workqueue_struct *ice_wq;
     67static const struct net_device_ops ice_netdev_safe_mode_ops;
     68static const struct net_device_ops ice_netdev_ops;
     69
     70static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
     71
     72static void ice_vsi_release_all(struct ice_pf *pf);
     73
     74static int ice_rebuild_channels(struct ice_pf *pf);
     75static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
     76
     77static int
     78ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
     79		     void *cb_priv, enum tc_setup_type type, void *type_data,
     80		     void *data,
     81		     void (*cleanup)(struct flow_block_cb *block_cb));
     82
     83bool netif_is_ice(struct net_device *dev)
     84{
     85	return dev && (dev->netdev_ops == &ice_netdev_ops);
     86}
     87
     88/**
     89 * ice_get_tx_pending - returns number of Tx descriptors not processed
     90 * @ring: the ring of descriptors
     91 */
     92static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
     93{
     94	u16 head, tail;
     95
     96	head = ring->next_to_clean;
     97	tail = ring->next_to_use;
     98
     99	if (head != tail)
    100		return (head < tail) ?
    101			tail - head : (tail + ring->count - head);
    102	return 0;
    103}
    104
    105/**
    106 * ice_check_for_hang_subtask - check for and recover hung queues
    107 * @pf: pointer to PF struct
    108 */
    109static void ice_check_for_hang_subtask(struct ice_pf *pf)
    110{
    111	struct ice_vsi *vsi = NULL;
    112	struct ice_hw *hw;
    113	unsigned int i;
    114	int packets;
    115	u32 v;
    116
    117	ice_for_each_vsi(pf, v)
    118		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
    119			vsi = pf->vsi[v];
    120			break;
    121		}
    122
    123	if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
    124		return;
    125
    126	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
    127		return;
    128
    129	hw = &vsi->back->hw;
    130
    131	ice_for_each_txq(vsi, i) {
    132		struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
    133
    134		if (!tx_ring)
    135			continue;
    136		if (ice_ring_ch_enabled(tx_ring))
    137			continue;
    138
    139		if (tx_ring->desc) {
    140			/* If packet counter has not changed the queue is
    141			 * likely stalled, so force an interrupt for this
    142			 * queue.
    143			 *
    144			 * prev_pkt would be negative if there was no
    145			 * pending work.
    146			 */
    147			packets = tx_ring->stats.pkts & INT_MAX;
    148			if (tx_ring->tx_stats.prev_pkt == packets) {
    149				/* Trigger sw interrupt to revive the queue */
    150				ice_trigger_sw_intr(hw, tx_ring->q_vector);
    151				continue;
    152			}
    153
    154			/* Memory barrier between read of packet count and call
    155			 * to ice_get_tx_pending()
    156			 */
    157			smp_rmb();
    158			tx_ring->tx_stats.prev_pkt =
    159			    ice_get_tx_pending(tx_ring) ? packets : -1;
    160		}
    161	}
    162}
    163
    164/**
    165 * ice_init_mac_fltr - Set initial MAC filters
    166 * @pf: board private structure
    167 *
    168 * Set initial set of MAC filters for PF VSI; configure filters for permanent
    169 * address and broadcast address. If an error is encountered, netdevice will be
    170 * unregistered.
    171 */
    172static int ice_init_mac_fltr(struct ice_pf *pf)
    173{
    174	struct ice_vsi *vsi;
    175	u8 *perm_addr;
    176
    177	vsi = ice_get_main_vsi(pf);
    178	if (!vsi)
    179		return -EINVAL;
    180
    181	perm_addr = vsi->port_info->mac.perm_addr;
    182	return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
    183}
    184
    185/**
    186 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
    187 * @netdev: the net device on which the sync is happening
    188 * @addr: MAC address to sync
    189 *
    190 * This is a callback function which is called by the in kernel device sync
    191 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
    192 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
    193 * MAC filters from the hardware.
    194 */
    195static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
    196{
    197	struct ice_netdev_priv *np = netdev_priv(netdev);
    198	struct ice_vsi *vsi = np->vsi;
    199
    200	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
    201				     ICE_FWD_TO_VSI))
    202		return -EINVAL;
    203
    204	return 0;
    205}
    206
    207/**
    208 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
    209 * @netdev: the net device on which the unsync is happening
    210 * @addr: MAC address to unsync
    211 *
    212 * This is a callback function which is called by the in kernel device unsync
    213 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
    214 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
    215 * delete the MAC filters from the hardware.
    216 */
    217static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
    218{
    219	struct ice_netdev_priv *np = netdev_priv(netdev);
    220	struct ice_vsi *vsi = np->vsi;
    221
    222	/* Under some circumstances, we might receive a request to delete our
    223	 * own device address from our uc list. Because we store the device
    224	 * address in the VSI's MAC filter list, we need to ignore such
    225	 * requests and not delete our device address from this list.
    226	 */
    227	if (ether_addr_equal(addr, netdev->dev_addr))
    228		return 0;
    229
    230	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
    231				     ICE_FWD_TO_VSI))
    232		return -EINVAL;
    233
    234	return 0;
    235}
    236
    237/**
    238 * ice_vsi_fltr_changed - check if filter state changed
    239 * @vsi: VSI to be checked
    240 *
    241 * returns true if filter state has changed, false otherwise.
    242 */
    243static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
    244{
    245	return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
    246	       test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
    247}
    248
    249/**
    250 * ice_set_promisc - Enable promiscuous mode for a given PF
    251 * @vsi: the VSI being configured
    252 * @promisc_m: mask of promiscuous config bits
    253 *
    254 */
    255static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
    256{
    257	int status;
    258
    259	if (vsi->type != ICE_VSI_PF)
    260		return 0;
    261
    262	if (ice_vsi_has_non_zero_vlans(vsi)) {
    263		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
    264		status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
    265						       promisc_m);
    266	} else {
    267		status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
    268						  promisc_m, 0);
    269	}
    270
    271	return status;
    272}
    273
    274/**
    275 * ice_clear_promisc - Disable promiscuous mode for a given PF
    276 * @vsi: the VSI being configured
    277 * @promisc_m: mask of promiscuous config bits
    278 *
    279 */
    280static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
    281{
    282	int status;
    283
    284	if (vsi->type != ICE_VSI_PF)
    285		return 0;
    286
    287	if (ice_vsi_has_non_zero_vlans(vsi)) {
    288		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
    289		status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
    290							 promisc_m);
    291	} else {
    292		status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
    293						    promisc_m, 0);
    294	}
    295
    296	return status;
    297}
    298
    299/**
    300 * ice_get_devlink_port - Get devlink port from netdev
    301 * @netdev: the netdevice structure
    302 */
    303static struct devlink_port *ice_get_devlink_port(struct net_device *netdev)
    304{
    305	struct ice_pf *pf = ice_netdev_to_pf(netdev);
    306
    307	if (!ice_is_switchdev_running(pf))
    308		return NULL;
    309
    310	return &pf->devlink_port;
    311}
    312
    313/**
    314 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
    315 * @vsi: ptr to the VSI
    316 *
    317 * Push any outstanding VSI filter changes through the AdminQ.
    318 */
    319static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
    320{
    321	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
    322	struct device *dev = ice_pf_to_dev(vsi->back);
    323	struct net_device *netdev = vsi->netdev;
    324	bool promisc_forced_on = false;
    325	struct ice_pf *pf = vsi->back;
    326	struct ice_hw *hw = &pf->hw;
    327	u32 changed_flags = 0;
    328	int err;
    329
    330	if (!vsi->netdev)
    331		return -EINVAL;
    332
    333	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
    334		usleep_range(1000, 2000);
    335
    336	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
    337	vsi->current_netdev_flags = vsi->netdev->flags;
    338
    339	INIT_LIST_HEAD(&vsi->tmp_sync_list);
    340	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
    341
    342	if (ice_vsi_fltr_changed(vsi)) {
    343		clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
    344		clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
    345
    346		/* grab the netdev's addr_list_lock */
    347		netif_addr_lock_bh(netdev);
    348		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
    349			      ice_add_mac_to_unsync_list);
    350		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
    351			      ice_add_mac_to_unsync_list);
    352		/* our temp lists are populated. release lock */
    353		netif_addr_unlock_bh(netdev);
    354	}
    355
    356	/* Remove MAC addresses in the unsync list */
    357	err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
    358	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
    359	if (err) {
    360		netdev_err(netdev, "Failed to delete MAC filters\n");
    361		/* if we failed because of alloc failures, just bail */
    362		if (err == -ENOMEM)
    363			goto out;
    364	}
    365
    366	/* Add MAC addresses in the sync list */
    367	err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
    368	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
    369	/* If filter is added successfully or already exists, do not go into
    370	 * 'if' condition and report it as error. Instead continue processing
    371	 * rest of the function.
    372	 */
    373	if (err && err != -EEXIST) {
    374		netdev_err(netdev, "Failed to add MAC filters\n");
    375		/* If there is no more space for new umac filters, VSI
    376		 * should go into promiscuous mode. There should be some
    377		 * space reserved for promiscuous filters.
    378		 */
    379		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
    380		    !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
    381				      vsi->state)) {
    382			promisc_forced_on = true;
    383			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
    384				    vsi->vsi_num);
    385		} else {
    386			goto out;
    387		}
    388	}
    389	err = 0;
    390	/* check for changes in promiscuous modes */
    391	if (changed_flags & IFF_ALLMULTI) {
    392		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
    393			err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
    394			if (err) {
    395				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
    396				goto out_promisc;
    397			}
    398		} else {
    399			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
    400			err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
    401			if (err) {
    402				vsi->current_netdev_flags |= IFF_ALLMULTI;
    403				goto out_promisc;
    404			}
    405		}
    406	}
    407
    408	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
    409	    test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
    410		clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
    411		if (vsi->current_netdev_flags & IFF_PROMISC) {
    412			/* Apply Rx filter rule to get traffic from wire */
    413			if (!ice_is_dflt_vsi_in_use(pf->first_sw)) {
    414				err = ice_set_dflt_vsi(pf->first_sw, vsi);
    415				if (err && err != -EEXIST) {
    416					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
    417						   err, vsi->vsi_num);
    418					vsi->current_netdev_flags &=
    419						~IFF_PROMISC;
    420					goto out_promisc;
    421				}
    422				err = 0;
    423				vlan_ops->dis_rx_filtering(vsi);
    424			}
    425		} else {
    426			/* Clear Rx filter to remove traffic from wire */
    427			if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) {
    428				err = ice_clear_dflt_vsi(pf->first_sw);
    429				if (err) {
    430					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
    431						   err, vsi->vsi_num);
    432					vsi->current_netdev_flags |=
    433						IFF_PROMISC;
    434					goto out_promisc;
    435				}
    436				if (vsi->current_netdev_flags &
    437				    NETIF_F_HW_VLAN_CTAG_FILTER)
    438					vlan_ops->ena_rx_filtering(vsi);
    439			}
    440		}
    441	}
    442	goto exit;
    443
    444out_promisc:
    445	set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
    446	goto exit;
    447out:
    448	/* if something went wrong then set the changed flag so we try again */
    449	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
    450	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
    451exit:
    452	clear_bit(ICE_CFG_BUSY, vsi->state);
    453	return err;
    454}
    455
    456/**
    457 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
    458 * @pf: board private structure
    459 */
    460static void ice_sync_fltr_subtask(struct ice_pf *pf)
    461{
    462	int v;
    463
    464	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
    465		return;
    466
    467	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
    468
    469	ice_for_each_vsi(pf, v)
    470		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
    471		    ice_vsi_sync_fltr(pf->vsi[v])) {
    472			/* come back and try again later */
    473			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
    474			break;
    475		}
    476}
    477
    478/**
    479 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
    480 * @pf: the PF
    481 * @locked: is the rtnl_lock already held
    482 */
    483static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
    484{
    485	int node;
    486	int v;
    487
    488	ice_for_each_vsi(pf, v)
    489		if (pf->vsi[v])
    490			ice_dis_vsi(pf->vsi[v], locked);
    491
    492	for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
    493		pf->pf_agg_node[node].num_vsis = 0;
    494
    495	for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
    496		pf->vf_agg_node[node].num_vsis = 0;
    497}
    498
    499/**
    500 * ice_clear_sw_switch_recipes - clear switch recipes
    501 * @pf: board private structure
    502 *
    503 * Mark switch recipes as not created in sw structures. There are cases where
    504 * rules (especially advanced rules) need to be restored, either re-read from
    505 * hardware or added again. For example after the reset. 'recp_created' flag
    506 * prevents from doing that and need to be cleared upfront.
    507 */
    508static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
    509{
    510	struct ice_sw_recipe *recp;
    511	u8 i;
    512
    513	recp = pf->hw.switch_info->recp_list;
    514	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
    515		recp[i].recp_created = false;
    516}
    517
    518/**
    519 * ice_prepare_for_reset - prep for reset
    520 * @pf: board private structure
    521 * @reset_type: reset type requested
    522 *
    523 * Inform or close all dependent features in prep for reset.
    524 */
    525static void
    526ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
    527{
    528	struct ice_hw *hw = &pf->hw;
    529	struct ice_vsi *vsi;
    530	struct ice_vf *vf;
    531	unsigned int bkt;
    532
    533	dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
    534
    535	/* already prepared for reset */
    536	if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
    537		return;
    538
    539	ice_unplug_aux_dev(pf);
    540
    541	/* Notify VFs of impending reset */
    542	if (ice_check_sq_alive(hw, &hw->mailboxq))
    543		ice_vc_notify_reset(pf);
    544
    545	/* Disable VFs until reset is completed */
    546	mutex_lock(&pf->vfs.table_lock);
    547	ice_for_each_vf(pf, bkt, vf)
    548		ice_set_vf_state_qs_dis(vf);
    549	mutex_unlock(&pf->vfs.table_lock);
    550
    551	if (ice_is_eswitch_mode_switchdev(pf)) {
    552		if (reset_type != ICE_RESET_PFR)
    553			ice_clear_sw_switch_recipes(pf);
    554	}
    555
    556	/* release ADQ specific HW and SW resources */
    557	vsi = ice_get_main_vsi(pf);
    558	if (!vsi)
    559		goto skip;
    560
    561	/* to be on safe side, reset orig_rss_size so that normal flow
    562	 * of deciding rss_size can take precedence
    563	 */
    564	vsi->orig_rss_size = 0;
    565
    566	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
    567		if (reset_type == ICE_RESET_PFR) {
    568			vsi->old_ena_tc = vsi->all_enatc;
    569			vsi->old_numtc = vsi->all_numtc;
    570		} else {
    571			ice_remove_q_channels(vsi, true);
    572
    573			/* for other reset type, do not support channel rebuild
    574			 * hence reset needed info
    575			 */
    576			vsi->old_ena_tc = 0;
    577			vsi->all_enatc = 0;
    578			vsi->old_numtc = 0;
    579			vsi->all_numtc = 0;
    580			vsi->req_txq = 0;
    581			vsi->req_rxq = 0;
    582			clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
    583			memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
    584		}
    585	}
    586skip:
    587
    588	/* clear SW filtering DB */
    589	ice_clear_hw_tbls(hw);
    590	/* disable the VSIs and their queues that are not already DOWN */
    591	ice_pf_dis_all_vsi(pf, false);
    592
    593	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
    594		ice_ptp_prepare_for_reset(pf);
    595
    596	if (ice_is_feature_supported(pf, ICE_F_GNSS))
    597		ice_gnss_exit(pf);
    598
    599	if (hw->port_info)
    600		ice_sched_clear_port(hw->port_info);
    601
    602	ice_shutdown_all_ctrlq(hw);
    603
    604	set_bit(ICE_PREPARED_FOR_RESET, pf->state);
    605}
    606
    607/**
    608 * ice_do_reset - Initiate one of many types of resets
    609 * @pf: board private structure
    610 * @reset_type: reset type requested before this function was called.
    611 */
    612static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
    613{
    614	struct device *dev = ice_pf_to_dev(pf);
    615	struct ice_hw *hw = &pf->hw;
    616
    617	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
    618
    619	ice_prepare_for_reset(pf, reset_type);
    620
    621	/* trigger the reset */
    622	if (ice_reset(hw, reset_type)) {
    623		dev_err(dev, "reset %d failed\n", reset_type);
    624		set_bit(ICE_RESET_FAILED, pf->state);
    625		clear_bit(ICE_RESET_OICR_RECV, pf->state);
    626		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
    627		clear_bit(ICE_PFR_REQ, pf->state);
    628		clear_bit(ICE_CORER_REQ, pf->state);
    629		clear_bit(ICE_GLOBR_REQ, pf->state);
    630		wake_up(&pf->reset_wait_queue);
    631		return;
    632	}
    633
    634	/* PFR is a bit of a special case because it doesn't result in an OICR
    635	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
    636	 * associated state bits.
    637	 */
    638	if (reset_type == ICE_RESET_PFR) {
    639		pf->pfr_count++;
    640		ice_rebuild(pf, reset_type);
    641		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
    642		clear_bit(ICE_PFR_REQ, pf->state);
    643		wake_up(&pf->reset_wait_queue);
    644		ice_reset_all_vfs(pf);
    645	}
    646}
    647
    648/**
    649 * ice_reset_subtask - Set up for resetting the device and driver
    650 * @pf: board private structure
    651 */
    652static void ice_reset_subtask(struct ice_pf *pf)
    653{
    654	enum ice_reset_req reset_type = ICE_RESET_INVAL;
    655
    656	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
    657	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
    658	 * of reset is pending and sets bits in pf->state indicating the reset
    659	 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
    660	 * prepare for pending reset if not already (for PF software-initiated
    661	 * global resets the software should already be prepared for it as
    662	 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
    663	 * by firmware or software on other PFs, that bit is not set so prepare
    664	 * for the reset now), poll for reset done, rebuild and return.
    665	 */
    666	if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
    667		/* Perform the largest reset requested */
    668		if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
    669			reset_type = ICE_RESET_CORER;
    670		if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
    671			reset_type = ICE_RESET_GLOBR;
    672		if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
    673			reset_type = ICE_RESET_EMPR;
    674		/* return if no valid reset type requested */
    675		if (reset_type == ICE_RESET_INVAL)
    676			return;
    677		ice_prepare_for_reset(pf, reset_type);
    678
    679		/* make sure we are ready to rebuild */
    680		if (ice_check_reset(&pf->hw)) {
    681			set_bit(ICE_RESET_FAILED, pf->state);
    682		} else {
    683			/* done with reset. start rebuild */
    684			pf->hw.reset_ongoing = false;
    685			ice_rebuild(pf, reset_type);
    686			/* clear bit to resume normal operations, but
    687			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
    688			 */
    689			clear_bit(ICE_RESET_OICR_RECV, pf->state);
    690			clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
    691			clear_bit(ICE_PFR_REQ, pf->state);
    692			clear_bit(ICE_CORER_REQ, pf->state);
    693			clear_bit(ICE_GLOBR_REQ, pf->state);
    694			wake_up(&pf->reset_wait_queue);
    695			ice_reset_all_vfs(pf);
    696		}
    697
    698		return;
    699	}
    700
    701	/* No pending resets to finish processing. Check for new resets */
    702	if (test_bit(ICE_PFR_REQ, pf->state))
    703		reset_type = ICE_RESET_PFR;
    704	if (test_bit(ICE_CORER_REQ, pf->state))
    705		reset_type = ICE_RESET_CORER;
    706	if (test_bit(ICE_GLOBR_REQ, pf->state))
    707		reset_type = ICE_RESET_GLOBR;
    708	/* If no valid reset type requested just return */
    709	if (reset_type == ICE_RESET_INVAL)
    710		return;
    711
    712	/* reset if not already down or busy */
    713	if (!test_bit(ICE_DOWN, pf->state) &&
    714	    !test_bit(ICE_CFG_BUSY, pf->state)) {
    715		ice_do_reset(pf, reset_type);
    716	}
    717}
    718
    719/**
    720 * ice_print_topo_conflict - print topology conflict message
    721 * @vsi: the VSI whose topology status is being checked
    722 */
    723static void ice_print_topo_conflict(struct ice_vsi *vsi)
    724{
    725	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
    726	case ICE_AQ_LINK_TOPO_CONFLICT:
    727	case ICE_AQ_LINK_MEDIA_CONFLICT:
    728	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
    729	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
    730	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
    731		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");
    732		break;
    733	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
    734		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
    735			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");
    736		else
    737			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");
    738		break;
    739	default:
    740		break;
    741	}
    742}
    743
    744/**
    745 * ice_print_link_msg - print link up or down message
    746 * @vsi: the VSI whose link status is being queried
    747 * @isup: boolean for if the link is now up or down
    748 */
    749void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
    750{
    751	struct ice_aqc_get_phy_caps_data *caps;
    752	const char *an_advertised;
    753	const char *fec_req;
    754	const char *speed;
    755	const char *fec;
    756	const char *fc;
    757	const char *an;
    758	int status;
    759
    760	if (!vsi)
    761		return;
    762
    763	if (vsi->current_isup == isup)
    764		return;
    765
    766	vsi->current_isup = isup;
    767
    768	if (!isup) {
    769		netdev_info(vsi->netdev, "NIC Link is Down\n");
    770		return;
    771	}
    772
    773	switch (vsi->port_info->phy.link_info.link_speed) {
    774	case ICE_AQ_LINK_SPEED_100GB:
    775		speed = "100 G";
    776		break;
    777	case ICE_AQ_LINK_SPEED_50GB:
    778		speed = "50 G";
    779		break;
    780	case ICE_AQ_LINK_SPEED_40GB:
    781		speed = "40 G";
    782		break;
    783	case ICE_AQ_LINK_SPEED_25GB:
    784		speed = "25 G";
    785		break;
    786	case ICE_AQ_LINK_SPEED_20GB:
    787		speed = "20 G";
    788		break;
    789	case ICE_AQ_LINK_SPEED_10GB:
    790		speed = "10 G";
    791		break;
    792	case ICE_AQ_LINK_SPEED_5GB:
    793		speed = "5 G";
    794		break;
    795	case ICE_AQ_LINK_SPEED_2500MB:
    796		speed = "2.5 G";
    797		break;
    798	case ICE_AQ_LINK_SPEED_1000MB:
    799		speed = "1 G";
    800		break;
    801	case ICE_AQ_LINK_SPEED_100MB:
    802		speed = "100 M";
    803		break;
    804	default:
    805		speed = "Unknown ";
    806		break;
    807	}
    808
    809	switch (vsi->port_info->fc.current_mode) {
    810	case ICE_FC_FULL:
    811		fc = "Rx/Tx";
    812		break;
    813	case ICE_FC_TX_PAUSE:
    814		fc = "Tx";
    815		break;
    816	case ICE_FC_RX_PAUSE:
    817		fc = "Rx";
    818		break;
    819	case ICE_FC_NONE:
    820		fc = "None";
    821		break;
    822	default:
    823		fc = "Unknown";
    824		break;
    825	}
    826
    827	/* Get FEC mode based on negotiated link info */
    828	switch (vsi->port_info->phy.link_info.fec_info) {
    829	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
    830	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
    831		fec = "RS-FEC";
    832		break;
    833	case ICE_AQ_LINK_25G_KR_FEC_EN:
    834		fec = "FC-FEC/BASE-R";
    835		break;
    836	default:
    837		fec = "NONE";
    838		break;
    839	}
    840
    841	/* check if autoneg completed, might be false due to not supported */
    842	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
    843		an = "True";
    844	else
    845		an = "False";
    846
    847	/* Get FEC mode requested based on PHY caps last SW configuration */
    848	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
    849	if (!caps) {
    850		fec_req = "Unknown";
    851		an_advertised = "Unknown";
    852		goto done;
    853	}
    854
    855	status = ice_aq_get_phy_caps(vsi->port_info, false,
    856				     ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
    857	if (status)
    858		netdev_info(vsi->netdev, "Get phy capability failed.\n");
    859
    860	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
    861
    862	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
    863	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
    864		fec_req = "RS-FEC";
    865	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
    866		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
    867		fec_req = "FC-FEC/BASE-R";
    868	else
    869		fec_req = "NONE";
    870
    871	kfree(caps);
    872
    873done:
    874	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",
    875		    speed, fec_req, fec, an_advertised, an, fc);
    876	ice_print_topo_conflict(vsi);
    877}
    878
    879/**
    880 * ice_vsi_link_event - update the VSI's netdev
    881 * @vsi: the VSI on which the link event occurred
    882 * @link_up: whether or not the VSI needs to be set up or down
    883 */
    884static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
    885{
    886	if (!vsi)
    887		return;
    888
    889	if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
    890		return;
    891
    892	if (vsi->type == ICE_VSI_PF) {
    893		if (link_up == netif_carrier_ok(vsi->netdev))
    894			return;
    895
    896		if (link_up) {
    897			netif_carrier_on(vsi->netdev);
    898			netif_tx_wake_all_queues(vsi->netdev);
    899		} else {
    900			netif_carrier_off(vsi->netdev);
    901			netif_tx_stop_all_queues(vsi->netdev);
    902		}
    903	}
    904}
    905
    906/**
    907 * ice_set_dflt_mib - send a default config MIB to the FW
    908 * @pf: private PF struct
    909 *
    910 * This function sends a default configuration MIB to the FW.
    911 *
    912 * If this function errors out at any point, the driver is still able to
    913 * function.  The main impact is that LFC may not operate as expected.
    914 * Therefore an error state in this function should be treated with a DBG
    915 * message and continue on with driver rebuild/reenable.
    916 */
    917static void ice_set_dflt_mib(struct ice_pf *pf)
    918{
    919	struct device *dev = ice_pf_to_dev(pf);
    920	u8 mib_type, *buf, *lldpmib = NULL;
    921	u16 len, typelen, offset = 0;
    922	struct ice_lldp_org_tlv *tlv;
    923	struct ice_hw *hw = &pf->hw;
    924	u32 ouisubtype;
    925
    926	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
    927	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
    928	if (!lldpmib) {
    929		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
    930			__func__);
    931		return;
    932	}
    933
    934	/* Add ETS CFG TLV */
    935	tlv = (struct ice_lldp_org_tlv *)lldpmib;
    936	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
    937		   ICE_IEEE_ETS_TLV_LEN);
    938	tlv->typelen = htons(typelen);
    939	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
    940		      ICE_IEEE_SUBTYPE_ETS_CFG);
    941	tlv->ouisubtype = htonl(ouisubtype);
    942
    943	buf = tlv->tlvinfo;
    944	buf[0] = 0;
    945
    946	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
    947	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
    948	 * Octets 13 - 20 are TSA values - leave as zeros
    949	 */
    950	buf[5] = 0x64;
    951	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
    952	offset += len + 2;
    953	tlv = (struct ice_lldp_org_tlv *)
    954		((char *)tlv + sizeof(tlv->typelen) + len);
    955
    956	/* Add ETS REC TLV */
    957	buf = tlv->tlvinfo;
    958	tlv->typelen = htons(typelen);
    959
    960	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
    961		      ICE_IEEE_SUBTYPE_ETS_REC);
    962	tlv->ouisubtype = htonl(ouisubtype);
    963
    964	/* First octet of buf is reserved
    965	 * Octets 1 - 4 map UP to TC - all UPs map to zero
    966	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
    967	 * Octets 13 - 20 are TSA value - leave as zeros
    968	 */
    969	buf[5] = 0x64;
    970	offset += len + 2;
    971	tlv = (struct ice_lldp_org_tlv *)
    972		((char *)tlv + sizeof(tlv->typelen) + len);
    973
    974	/* Add PFC CFG TLV */
    975	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
    976		   ICE_IEEE_PFC_TLV_LEN);
    977	tlv->typelen = htons(typelen);
    978
    979	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
    980		      ICE_IEEE_SUBTYPE_PFC_CFG);
    981	tlv->ouisubtype = htonl(ouisubtype);
    982
    983	/* Octet 1 left as all zeros - PFC disabled */
    984	buf[0] = 0x08;
    985	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
    986	offset += len + 2;
    987
    988	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
    989		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
    990
    991	kfree(lldpmib);
    992}
    993
    994/**
    995 * ice_check_phy_fw_load - check if PHY FW load failed
    996 * @pf: pointer to PF struct
    997 * @link_cfg_err: bitmap from the link info structure
    998 *
    999 * check if external PHY FW load failed and print an error message if it did
   1000 */
   1001static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
   1002{
   1003	if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
   1004		clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
   1005		return;
   1006	}
   1007
   1008	if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
   1009		return;
   1010
   1011	if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
   1012		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");
   1013		set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
   1014	}
   1015}
   1016
   1017/**
   1018 * ice_check_module_power
   1019 * @pf: pointer to PF struct
   1020 * @link_cfg_err: bitmap from the link info structure
   1021 *
   1022 * check module power level returned by a previous call to aq_get_link_info
   1023 * and print error messages if module power level is not supported
   1024 */
   1025static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
   1026{
   1027	/* if module power level is supported, clear the flag */
   1028	if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
   1029			      ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
   1030		clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
   1031		return;
   1032	}
   1033
   1034	/* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
   1035	 * above block didn't clear this bit, there's nothing to do
   1036	 */
   1037	if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
   1038		return;
   1039
   1040	if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
   1041		dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
   1042		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
   1043	} else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
   1044		dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
   1045		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
   1046	}
   1047}
   1048
   1049/**
   1050 * ice_check_link_cfg_err - check if link configuration failed
   1051 * @pf: pointer to the PF struct
   1052 * @link_cfg_err: bitmap from the link info structure
   1053 *
   1054 * print if any link configuration failure happens due to the value in the
   1055 * link_cfg_err parameter in the link info structure
   1056 */
   1057static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
   1058{
   1059	ice_check_module_power(pf, link_cfg_err);
   1060	ice_check_phy_fw_load(pf, link_cfg_err);
   1061}
   1062
   1063/**
   1064 * ice_link_event - process the link event
   1065 * @pf: PF that the link event is associated with
   1066 * @pi: port_info for the port that the link event is associated with
   1067 * @link_up: true if the physical link is up and false if it is down
   1068 * @link_speed: current link speed received from the link event
   1069 *
   1070 * Returns 0 on success and negative on failure
   1071 */
   1072static int
   1073ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
   1074	       u16 link_speed)
   1075{
   1076	struct device *dev = ice_pf_to_dev(pf);
   1077	struct ice_phy_info *phy_info;
   1078	struct ice_vsi *vsi;
   1079	u16 old_link_speed;
   1080	bool old_link;
   1081	int status;
   1082
   1083	phy_info = &pi->phy;
   1084	phy_info->link_info_old = phy_info->link_info;
   1085
   1086	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
   1087	old_link_speed = phy_info->link_info_old.link_speed;
   1088
   1089	/* update the link info structures and re-enable link events,
   1090	 * don't bail on failure due to other book keeping needed
   1091	 */
   1092	status = ice_update_link_info(pi);
   1093	if (status)
   1094		dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
   1095			pi->lport, status,
   1096			ice_aq_str(pi->hw->adminq.sq_last_status));
   1097
   1098	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
   1099
   1100	/* Check if the link state is up after updating link info, and treat
   1101	 * this event as an UP event since the link is actually UP now.
   1102	 */
   1103	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
   1104		link_up = true;
   1105
   1106	vsi = ice_get_main_vsi(pf);
   1107	if (!vsi || !vsi->port_info)
   1108		return -EINVAL;
   1109
   1110	/* turn off PHY if media was removed */
   1111	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
   1112	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
   1113		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
   1114		ice_set_link(vsi, false);
   1115	}
   1116
   1117	/* if the old link up/down and speed is the same as the new */
   1118	if (link_up == old_link && link_speed == old_link_speed)
   1119		return 0;
   1120
   1121	if (!ice_is_e810(&pf->hw))
   1122		ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
   1123
   1124	if (ice_is_dcb_active(pf)) {
   1125		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
   1126			ice_dcb_rebuild(pf);
   1127	} else {
   1128		if (link_up)
   1129			ice_set_dflt_mib(pf);
   1130	}
   1131	ice_vsi_link_event(vsi, link_up);
   1132	ice_print_link_msg(vsi, link_up);
   1133
   1134	ice_vc_notify_link_state(pf);
   1135
   1136	return 0;
   1137}
   1138
   1139/**
   1140 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
   1141 * @pf: board private structure
   1142 */
   1143static void ice_watchdog_subtask(struct ice_pf *pf)
   1144{
   1145	int i;
   1146
   1147	/* if interface is down do nothing */
   1148	if (test_bit(ICE_DOWN, pf->state) ||
   1149	    test_bit(ICE_CFG_BUSY, pf->state))
   1150		return;
   1151
   1152	/* make sure we don't do these things too often */
   1153	if (time_before(jiffies,
   1154			pf->serv_tmr_prev + pf->serv_tmr_period))
   1155		return;
   1156
   1157	pf->serv_tmr_prev = jiffies;
   1158
   1159	/* Update the stats for active netdevs so the network stack
   1160	 * can look at updated numbers whenever it cares to
   1161	 */
   1162	ice_update_pf_stats(pf);
   1163	ice_for_each_vsi(pf, i)
   1164		if (pf->vsi[i] && pf->vsi[i]->netdev)
   1165			ice_update_vsi_stats(pf->vsi[i]);
   1166}
   1167
   1168/**
   1169 * ice_init_link_events - enable/initialize link events
   1170 * @pi: pointer to the port_info instance
   1171 *
   1172 * Returns -EIO on failure, 0 on success
   1173 */
   1174static int ice_init_link_events(struct ice_port_info *pi)
   1175{
   1176	u16 mask;
   1177
   1178	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
   1179		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
   1180		       ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
   1181
   1182	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
   1183		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
   1184			pi->lport);
   1185		return -EIO;
   1186	}
   1187
   1188	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
   1189		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
   1190			pi->lport);
   1191		return -EIO;
   1192	}
   1193
   1194	return 0;
   1195}
   1196
   1197/**
   1198 * ice_handle_link_event - handle link event via ARQ
   1199 * @pf: PF that the link event is associated with
   1200 * @event: event structure containing link status info
   1201 */
   1202static int
   1203ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
   1204{
   1205	struct ice_aqc_get_link_status_data *link_data;
   1206	struct ice_port_info *port_info;
   1207	int status;
   1208
   1209	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
   1210	port_info = pf->hw.port_info;
   1211	if (!port_info)
   1212		return -EINVAL;
   1213
   1214	status = ice_link_event(pf, port_info,
   1215				!!(link_data->link_info & ICE_AQ_LINK_UP),
   1216				le16_to_cpu(link_data->link_speed));
   1217	if (status)
   1218		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
   1219			status);
   1220
   1221	return status;
   1222}
   1223
   1224enum ice_aq_task_state {
   1225	ICE_AQ_TASK_WAITING = 0,
   1226	ICE_AQ_TASK_COMPLETE,
   1227	ICE_AQ_TASK_CANCELED,
   1228};
   1229
   1230struct ice_aq_task {
   1231	struct hlist_node entry;
   1232
   1233	u16 opcode;
   1234	struct ice_rq_event_info *event;
   1235	enum ice_aq_task_state state;
   1236};
   1237
   1238/**
   1239 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
   1240 * @pf: pointer to the PF private structure
   1241 * @opcode: the opcode to wait for
   1242 * @timeout: how long to wait, in jiffies
   1243 * @event: storage for the event info
   1244 *
   1245 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
   1246 * current thread will be put to sleep until the specified event occurs or
   1247 * until the given timeout is reached.
   1248 *
   1249 * To obtain only the descriptor contents, pass an event without an allocated
   1250 * msg_buf. If the complete data buffer is desired, allocate the
   1251 * event->msg_buf with enough space ahead of time.
   1252 *
   1253 * Returns: zero on success, or a negative error code on failure.
   1254 */
   1255int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
   1256			  struct ice_rq_event_info *event)
   1257{
   1258	struct device *dev = ice_pf_to_dev(pf);
   1259	struct ice_aq_task *task;
   1260	unsigned long start;
   1261	long ret;
   1262	int err;
   1263
   1264	task = kzalloc(sizeof(*task), GFP_KERNEL);
   1265	if (!task)
   1266		return -ENOMEM;
   1267
   1268	INIT_HLIST_NODE(&task->entry);
   1269	task->opcode = opcode;
   1270	task->event = event;
   1271	task->state = ICE_AQ_TASK_WAITING;
   1272
   1273	spin_lock_bh(&pf->aq_wait_lock);
   1274	hlist_add_head(&task->entry, &pf->aq_wait_list);
   1275	spin_unlock_bh(&pf->aq_wait_lock);
   1276
   1277	start = jiffies;
   1278
   1279	ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
   1280					       timeout);
   1281	switch (task->state) {
   1282	case ICE_AQ_TASK_WAITING:
   1283		err = ret < 0 ? ret : -ETIMEDOUT;
   1284		break;
   1285	case ICE_AQ_TASK_CANCELED:
   1286		err = ret < 0 ? ret : -ECANCELED;
   1287		break;
   1288	case ICE_AQ_TASK_COMPLETE:
   1289		err = ret < 0 ? ret : 0;
   1290		break;
   1291	default:
   1292		WARN(1, "Unexpected AdminQ wait task state %u", task->state);
   1293		err = -EINVAL;
   1294		break;
   1295	}
   1296
   1297	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
   1298		jiffies_to_msecs(jiffies - start),
   1299		jiffies_to_msecs(timeout),
   1300		opcode);
   1301
   1302	spin_lock_bh(&pf->aq_wait_lock);
   1303	hlist_del(&task->entry);
   1304	spin_unlock_bh(&pf->aq_wait_lock);
   1305	kfree(task);
   1306
   1307	return err;
   1308}
   1309
   1310/**
   1311 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
   1312 * @pf: pointer to the PF private structure
   1313 * @opcode: the opcode of the event
   1314 * @event: the event to check
   1315 *
   1316 * Loops over the current list of pending threads waiting for an AdminQ event.
   1317 * For each matching task, copy the contents of the event into the task
   1318 * structure and wake up the thread.
   1319 *
   1320 * If multiple threads wait for the same opcode, they will all be woken up.
   1321 *
   1322 * Note that event->msg_buf will only be duplicated if the event has a buffer
   1323 * with enough space already allocated. Otherwise, only the descriptor and
   1324 * message length will be copied.
   1325 *
   1326 * Returns: true if an event was found, false otherwise
   1327 */
   1328static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
   1329				struct ice_rq_event_info *event)
   1330{
   1331	struct ice_aq_task *task;
   1332	bool found = false;
   1333
   1334	spin_lock_bh(&pf->aq_wait_lock);
   1335	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
   1336		if (task->state || task->opcode != opcode)
   1337			continue;
   1338
   1339		memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
   1340		task->event->msg_len = event->msg_len;
   1341
   1342		/* Only copy the data buffer if a destination was set */
   1343		if (task->event->msg_buf &&
   1344		    task->event->buf_len > event->buf_len) {
   1345			memcpy(task->event->msg_buf, event->msg_buf,
   1346			       event->buf_len);
   1347			task->event->buf_len = event->buf_len;
   1348		}
   1349
   1350		task->state = ICE_AQ_TASK_COMPLETE;
   1351		found = true;
   1352	}
   1353	spin_unlock_bh(&pf->aq_wait_lock);
   1354
   1355	if (found)
   1356		wake_up(&pf->aq_wait_queue);
   1357}
   1358
   1359/**
   1360 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
   1361 * @pf: the PF private structure
   1362 *
   1363 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
   1364 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
   1365 */
   1366static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
   1367{
   1368	struct ice_aq_task *task;
   1369
   1370	spin_lock_bh(&pf->aq_wait_lock);
   1371	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
   1372		task->state = ICE_AQ_TASK_CANCELED;
   1373	spin_unlock_bh(&pf->aq_wait_lock);
   1374
   1375	wake_up(&pf->aq_wait_queue);
   1376}
   1377
   1378/**
   1379 * __ice_clean_ctrlq - helper function to clean controlq rings
   1380 * @pf: ptr to struct ice_pf
   1381 * @q_type: specific Control queue type
   1382 */
   1383static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
   1384{
   1385	struct device *dev = ice_pf_to_dev(pf);
   1386	struct ice_rq_event_info event;
   1387	struct ice_hw *hw = &pf->hw;
   1388	struct ice_ctl_q_info *cq;
   1389	u16 pending, i = 0;
   1390	const char *qtype;
   1391	u32 oldval, val;
   1392
   1393	/* Do not clean control queue if/when PF reset fails */
   1394	if (test_bit(ICE_RESET_FAILED, pf->state))
   1395		return 0;
   1396
   1397	switch (q_type) {
   1398	case ICE_CTL_Q_ADMIN:
   1399		cq = &hw->adminq;
   1400		qtype = "Admin";
   1401		break;
   1402	case ICE_CTL_Q_SB:
   1403		cq = &hw->sbq;
   1404		qtype = "Sideband";
   1405		break;
   1406	case ICE_CTL_Q_MAILBOX:
   1407		cq = &hw->mailboxq;
   1408		qtype = "Mailbox";
   1409		/* we are going to try to detect a malicious VF, so set the
   1410		 * state to begin detection
   1411		 */
   1412		hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
   1413		break;
   1414	default:
   1415		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
   1416		return 0;
   1417	}
   1418
   1419	/* check for error indications - PF_xx_AxQLEN register layout for
   1420	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
   1421	 */
   1422	val = rd32(hw, cq->rq.len);
   1423	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
   1424		   PF_FW_ARQLEN_ARQCRIT_M)) {
   1425		oldval = val;
   1426		if (val & PF_FW_ARQLEN_ARQVFE_M)
   1427			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
   1428				qtype);
   1429		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
   1430			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
   1431				qtype);
   1432		}
   1433		if (val & PF_FW_ARQLEN_ARQCRIT_M)
   1434			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
   1435				qtype);
   1436		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
   1437			 PF_FW_ARQLEN_ARQCRIT_M);
   1438		if (oldval != val)
   1439			wr32(hw, cq->rq.len, val);
   1440	}
   1441
   1442	val = rd32(hw, cq->sq.len);
   1443	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
   1444		   PF_FW_ATQLEN_ATQCRIT_M)) {
   1445		oldval = val;
   1446		if (val & PF_FW_ATQLEN_ATQVFE_M)
   1447			dev_dbg(dev, "%s Send Queue VF Error detected\n",
   1448				qtype);
   1449		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
   1450			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
   1451				qtype);
   1452		}
   1453		if (val & PF_FW_ATQLEN_ATQCRIT_M)
   1454			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
   1455				qtype);
   1456		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
   1457			 PF_FW_ATQLEN_ATQCRIT_M);
   1458		if (oldval != val)
   1459			wr32(hw, cq->sq.len, val);
   1460	}
   1461
   1462	event.buf_len = cq->rq_buf_size;
   1463	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
   1464	if (!event.msg_buf)
   1465		return 0;
   1466
   1467	do {
   1468		u16 opcode;
   1469		int ret;
   1470
   1471		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
   1472		if (ret == -EALREADY)
   1473			break;
   1474		if (ret) {
   1475			dev_err(dev, "%s Receive Queue event error %d\n", qtype,
   1476				ret);
   1477			break;
   1478		}
   1479
   1480		opcode = le16_to_cpu(event.desc.opcode);
   1481
   1482		/* Notify any thread that might be waiting for this event */
   1483		ice_aq_check_events(pf, opcode, &event);
   1484
   1485		switch (opcode) {
   1486		case ice_aqc_opc_get_link_status:
   1487			if (ice_handle_link_event(pf, &event))
   1488				dev_err(dev, "Could not handle link event\n");
   1489			break;
   1490		case ice_aqc_opc_event_lan_overflow:
   1491			ice_vf_lan_overflow_event(pf, &event);
   1492			break;
   1493		case ice_mbx_opc_send_msg_to_pf:
   1494			if (!ice_is_malicious_vf(pf, &event, i, pending))
   1495				ice_vc_process_vf_msg(pf, &event);
   1496			break;
   1497		case ice_aqc_opc_fw_logging:
   1498			ice_output_fw_log(hw, &event.desc, event.msg_buf);
   1499			break;
   1500		case ice_aqc_opc_lldp_set_mib_change:
   1501			ice_dcb_process_lldp_set_mib_change(pf, &event);
   1502			break;
   1503		default:
   1504			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
   1505				qtype, opcode);
   1506			break;
   1507		}
   1508	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
   1509
   1510	kfree(event.msg_buf);
   1511
   1512	return pending && (i == ICE_DFLT_IRQ_WORK);
   1513}
   1514
   1515/**
   1516 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
   1517 * @hw: pointer to hardware info
   1518 * @cq: control queue information
   1519 *
   1520 * returns true if there are pending messages in a queue, false if there aren't
   1521 */
   1522static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
   1523{
   1524	u16 ntu;
   1525
   1526	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
   1527	return cq->rq.next_to_clean != ntu;
   1528}
   1529
   1530/**
   1531 * ice_clean_adminq_subtask - clean the AdminQ rings
   1532 * @pf: board private structure
   1533 */
   1534static void ice_clean_adminq_subtask(struct ice_pf *pf)
   1535{
   1536	struct ice_hw *hw = &pf->hw;
   1537
   1538	if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
   1539		return;
   1540
   1541	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
   1542		return;
   1543
   1544	clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
   1545
   1546	/* There might be a situation where new messages arrive to a control
   1547	 * queue between processing the last message and clearing the
   1548	 * EVENT_PENDING bit. So before exiting, check queue head again (using
   1549	 * ice_ctrlq_pending) and process new messages if any.
   1550	 */
   1551	if (ice_ctrlq_pending(hw, &hw->adminq))
   1552		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
   1553
   1554	ice_flush(hw);
   1555}
   1556
   1557/**
   1558 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
   1559 * @pf: board private structure
   1560 */
   1561static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
   1562{
   1563	struct ice_hw *hw = &pf->hw;
   1564
   1565	if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
   1566		return;
   1567
   1568	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
   1569		return;
   1570
   1571	clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
   1572
   1573	if (ice_ctrlq_pending(hw, &hw->mailboxq))
   1574		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
   1575
   1576	ice_flush(hw);
   1577}
   1578
   1579/**
   1580 * ice_clean_sbq_subtask - clean the Sideband Queue rings
   1581 * @pf: board private structure
   1582 */
   1583static void ice_clean_sbq_subtask(struct ice_pf *pf)
   1584{
   1585	struct ice_hw *hw = &pf->hw;
   1586
   1587	/* Nothing to do here if sideband queue is not supported */
   1588	if (!ice_is_sbq_supported(hw)) {
   1589		clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
   1590		return;
   1591	}
   1592
   1593	if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
   1594		return;
   1595
   1596	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
   1597		return;
   1598
   1599	clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
   1600
   1601	if (ice_ctrlq_pending(hw, &hw->sbq))
   1602		__ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
   1603
   1604	ice_flush(hw);
   1605}
   1606
   1607/**
   1608 * ice_service_task_schedule - schedule the service task to wake up
   1609 * @pf: board private structure
   1610 *
   1611 * If not already scheduled, this puts the task into the work queue.
   1612 */
   1613void ice_service_task_schedule(struct ice_pf *pf)
   1614{
   1615	if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
   1616	    !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
   1617	    !test_bit(ICE_NEEDS_RESTART, pf->state))
   1618		queue_work(ice_wq, &pf->serv_task);
   1619}
   1620
   1621/**
   1622 * ice_service_task_complete - finish up the service task
   1623 * @pf: board private structure
   1624 */
   1625static void ice_service_task_complete(struct ice_pf *pf)
   1626{
   1627	WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
   1628
   1629	/* force memory (pf->state) to sync before next service task */
   1630	smp_mb__before_atomic();
   1631	clear_bit(ICE_SERVICE_SCHED, pf->state);
   1632}
   1633
   1634/**
   1635 * ice_service_task_stop - stop service task and cancel works
   1636 * @pf: board private structure
   1637 *
   1638 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
   1639 * 1 otherwise.
   1640 */
   1641static int ice_service_task_stop(struct ice_pf *pf)
   1642{
   1643	int ret;
   1644
   1645	ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
   1646
   1647	if (pf->serv_tmr.function)
   1648		del_timer_sync(&pf->serv_tmr);
   1649	if (pf->serv_task.func)
   1650		cancel_work_sync(&pf->serv_task);
   1651
   1652	clear_bit(ICE_SERVICE_SCHED, pf->state);
   1653	return ret;
   1654}
   1655
   1656/**
   1657 * ice_service_task_restart - restart service task and schedule works
   1658 * @pf: board private structure
   1659 *
   1660 * This function is needed for suspend and resume works (e.g WoL scenario)
   1661 */
   1662static void ice_service_task_restart(struct ice_pf *pf)
   1663{
   1664	clear_bit(ICE_SERVICE_DIS, pf->state);
   1665	ice_service_task_schedule(pf);
   1666}
   1667
   1668/**
   1669 * ice_service_timer - timer callback to schedule service task
   1670 * @t: pointer to timer_list
   1671 */
   1672static void ice_service_timer(struct timer_list *t)
   1673{
   1674	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
   1675
   1676	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
   1677	ice_service_task_schedule(pf);
   1678}
   1679
   1680/**
   1681 * ice_handle_mdd_event - handle malicious driver detect event
   1682 * @pf: pointer to the PF structure
   1683 *
   1684 * Called from service task. OICR interrupt handler indicates MDD event.
   1685 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
   1686 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
   1687 * disable the queue, the PF can be configured to reset the VF using ethtool
   1688 * private flag mdd-auto-reset-vf.
   1689 */
   1690static void ice_handle_mdd_event(struct ice_pf *pf)
   1691{
   1692	struct device *dev = ice_pf_to_dev(pf);
   1693	struct ice_hw *hw = &pf->hw;
   1694	struct ice_vf *vf;
   1695	unsigned int bkt;
   1696	u32 reg;
   1697
   1698	if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
   1699		/* Since the VF MDD event logging is rate limited, check if
   1700		 * there are pending MDD events.
   1701		 */
   1702		ice_print_vfs_mdd_events(pf);
   1703		return;
   1704	}
   1705
   1706	/* find what triggered an MDD event */
   1707	reg = rd32(hw, GL_MDET_TX_PQM);
   1708	if (reg & GL_MDET_TX_PQM_VALID_M) {
   1709		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
   1710				GL_MDET_TX_PQM_PF_NUM_S;
   1711		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
   1712				GL_MDET_TX_PQM_VF_NUM_S;
   1713		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
   1714				GL_MDET_TX_PQM_MAL_TYPE_S;
   1715		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
   1716				GL_MDET_TX_PQM_QNUM_S);
   1717
   1718		if (netif_msg_tx_err(pf))
   1719			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
   1720				 event, queue, pf_num, vf_num);
   1721		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
   1722	}
   1723
   1724	reg = rd32(hw, GL_MDET_TX_TCLAN);
   1725	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
   1726		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
   1727				GL_MDET_TX_TCLAN_PF_NUM_S;
   1728		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
   1729				GL_MDET_TX_TCLAN_VF_NUM_S;
   1730		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
   1731				GL_MDET_TX_TCLAN_MAL_TYPE_S;
   1732		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
   1733				GL_MDET_TX_TCLAN_QNUM_S);
   1734
   1735		if (netif_msg_tx_err(pf))
   1736			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
   1737				 event, queue, pf_num, vf_num);
   1738		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
   1739	}
   1740
   1741	reg = rd32(hw, GL_MDET_RX);
   1742	if (reg & GL_MDET_RX_VALID_M) {
   1743		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
   1744				GL_MDET_RX_PF_NUM_S;
   1745		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
   1746				GL_MDET_RX_VF_NUM_S;
   1747		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
   1748				GL_MDET_RX_MAL_TYPE_S;
   1749		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
   1750				GL_MDET_RX_QNUM_S);
   1751
   1752		if (netif_msg_rx_err(pf))
   1753			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
   1754				 event, queue, pf_num, vf_num);
   1755		wr32(hw, GL_MDET_RX, 0xffffffff);
   1756	}
   1757
   1758	/* check to see if this PF caused an MDD event */
   1759	reg = rd32(hw, PF_MDET_TX_PQM);
   1760	if (reg & PF_MDET_TX_PQM_VALID_M) {
   1761		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
   1762		if (netif_msg_tx_err(pf))
   1763			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
   1764	}
   1765
   1766	reg = rd32(hw, PF_MDET_TX_TCLAN);
   1767	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
   1768		wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
   1769		if (netif_msg_tx_err(pf))
   1770			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
   1771	}
   1772
   1773	reg = rd32(hw, PF_MDET_RX);
   1774	if (reg & PF_MDET_RX_VALID_M) {
   1775		wr32(hw, PF_MDET_RX, 0xFFFF);
   1776		if (netif_msg_rx_err(pf))
   1777			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
   1778	}
   1779
   1780	/* Check to see if one of the VFs caused an MDD event, and then
   1781	 * increment counters and set print pending
   1782	 */
   1783	mutex_lock(&pf->vfs.table_lock);
   1784	ice_for_each_vf(pf, bkt, vf) {
   1785		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
   1786		if (reg & VP_MDET_TX_PQM_VALID_M) {
   1787			wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
   1788			vf->mdd_tx_events.count++;
   1789			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
   1790			if (netif_msg_tx_err(pf))
   1791				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
   1792					 vf->vf_id);
   1793		}
   1794
   1795		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
   1796		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
   1797			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
   1798			vf->mdd_tx_events.count++;
   1799			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
   1800			if (netif_msg_tx_err(pf))
   1801				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
   1802					 vf->vf_id);
   1803		}
   1804
   1805		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
   1806		if (reg & VP_MDET_TX_TDPU_VALID_M) {
   1807			wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
   1808			vf->mdd_tx_events.count++;
   1809			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
   1810			if (netif_msg_tx_err(pf))
   1811				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
   1812					 vf->vf_id);
   1813		}
   1814
   1815		reg = rd32(hw, VP_MDET_RX(vf->vf_id));
   1816		if (reg & VP_MDET_RX_VALID_M) {
   1817			wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
   1818			vf->mdd_rx_events.count++;
   1819			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
   1820			if (netif_msg_rx_err(pf))
   1821				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
   1822					 vf->vf_id);
   1823
   1824			/* Since the queue is disabled on VF Rx MDD events, the
   1825			 * PF can be configured to reset the VF through ethtool
   1826			 * private flag mdd-auto-reset-vf.
   1827			 */
   1828			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
   1829				/* VF MDD event counters will be cleared by
   1830				 * reset, so print the event prior to reset.
   1831				 */
   1832				ice_print_vf_rx_mdd_event(vf);
   1833				ice_reset_vf(vf, ICE_VF_RESET_LOCK);
   1834			}
   1835		}
   1836	}
   1837	mutex_unlock(&pf->vfs.table_lock);
   1838
   1839	ice_print_vfs_mdd_events(pf);
   1840}
   1841
   1842/**
   1843 * ice_force_phys_link_state - Force the physical link state
   1844 * @vsi: VSI to force the physical link state to up/down
   1845 * @link_up: true/false indicates to set the physical link to up/down
   1846 *
   1847 * Force the physical link state by getting the current PHY capabilities from
   1848 * hardware and setting the PHY config based on the determined capabilities. If
   1849 * link changes a link event will be triggered because both the Enable Automatic
   1850 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
   1851 *
   1852 * Returns 0 on success, negative on failure
   1853 */
   1854static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
   1855{
   1856	struct ice_aqc_get_phy_caps_data *pcaps;
   1857	struct ice_aqc_set_phy_cfg_data *cfg;
   1858	struct ice_port_info *pi;
   1859	struct device *dev;
   1860	int retcode;
   1861
   1862	if (!vsi || !vsi->port_info || !vsi->back)
   1863		return -EINVAL;
   1864	if (vsi->type != ICE_VSI_PF)
   1865		return 0;
   1866
   1867	dev = ice_pf_to_dev(vsi->back);
   1868
   1869	pi = vsi->port_info;
   1870
   1871	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   1872	if (!pcaps)
   1873		return -ENOMEM;
   1874
   1875	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
   1876				      NULL);
   1877	if (retcode) {
   1878		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
   1879			vsi->vsi_num, retcode);
   1880		retcode = -EIO;
   1881		goto out;
   1882	}
   1883
   1884	/* No change in link */
   1885	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
   1886	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
   1887		goto out;
   1888
   1889	/* Use the current user PHY configuration. The current user PHY
   1890	 * configuration is initialized during probe from PHY capabilities
   1891	 * software mode, and updated on set PHY configuration.
   1892	 */
   1893	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
   1894	if (!cfg) {
   1895		retcode = -ENOMEM;
   1896		goto out;
   1897	}
   1898
   1899	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
   1900	if (link_up)
   1901		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
   1902	else
   1903		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
   1904
   1905	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
   1906	if (retcode) {
   1907		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
   1908			vsi->vsi_num, retcode);
   1909		retcode = -EIO;
   1910	}
   1911
   1912	kfree(cfg);
   1913out:
   1914	kfree(pcaps);
   1915	return retcode;
   1916}
   1917
   1918/**
   1919 * ice_init_nvm_phy_type - Initialize the NVM PHY type
   1920 * @pi: port info structure
   1921 *
   1922 * Initialize nvm_phy_type_[low|high] for link lenient mode support
   1923 */
   1924static int ice_init_nvm_phy_type(struct ice_port_info *pi)
   1925{
   1926	struct ice_aqc_get_phy_caps_data *pcaps;
   1927	struct ice_pf *pf = pi->hw->back;
   1928	int err;
   1929
   1930	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   1931	if (!pcaps)
   1932		return -ENOMEM;
   1933
   1934	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
   1935				  pcaps, NULL);
   1936
   1937	if (err) {
   1938		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
   1939		goto out;
   1940	}
   1941
   1942	pf->nvm_phy_type_hi = pcaps->phy_type_high;
   1943	pf->nvm_phy_type_lo = pcaps->phy_type_low;
   1944
   1945out:
   1946	kfree(pcaps);
   1947	return err;
   1948}
   1949
   1950/**
   1951 * ice_init_link_dflt_override - Initialize link default override
   1952 * @pi: port info structure
   1953 *
   1954 * Initialize link default override and PHY total port shutdown during probe
   1955 */
   1956static void ice_init_link_dflt_override(struct ice_port_info *pi)
   1957{
   1958	struct ice_link_default_override_tlv *ldo;
   1959	struct ice_pf *pf = pi->hw->back;
   1960
   1961	ldo = &pf->link_dflt_override;
   1962	if (ice_get_link_default_override(ldo, pi))
   1963		return;
   1964
   1965	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
   1966		return;
   1967
   1968	/* Enable Total Port Shutdown (override/replace link-down-on-close
   1969	 * ethtool private flag) for ports with Port Disable bit set.
   1970	 */
   1971	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
   1972	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
   1973}
   1974
   1975/**
   1976 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
   1977 * @pi: port info structure
   1978 *
   1979 * If default override is enabled, initialize the user PHY cfg speed and FEC
   1980 * settings using the default override mask from the NVM.
   1981 *
   1982 * The PHY should only be configured with the default override settings the
   1983 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
   1984 * is used to indicate that the user PHY cfg default override is initialized
   1985 * and the PHY has not been configured with the default override settings. The
   1986 * state is set here, and cleared in ice_configure_phy the first time the PHY is
   1987 * configured.
   1988 *
   1989 * This function should be called only if the FW doesn't support default
   1990 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
   1991 */
   1992static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
   1993{
   1994	struct ice_link_default_override_tlv *ldo;
   1995	struct ice_aqc_set_phy_cfg_data *cfg;
   1996	struct ice_phy_info *phy = &pi->phy;
   1997	struct ice_pf *pf = pi->hw->back;
   1998
   1999	ldo = &pf->link_dflt_override;
   2000
   2001	/* If link default override is enabled, use to mask NVM PHY capabilities
   2002	 * for speed and FEC default configuration.
   2003	 */
   2004	cfg = &phy->curr_user_phy_cfg;
   2005
   2006	if (ldo->phy_type_low || ldo->phy_type_high) {
   2007		cfg->phy_type_low = pf->nvm_phy_type_lo &
   2008				    cpu_to_le64(ldo->phy_type_low);
   2009		cfg->phy_type_high = pf->nvm_phy_type_hi &
   2010				     cpu_to_le64(ldo->phy_type_high);
   2011	}
   2012	cfg->link_fec_opt = ldo->fec_options;
   2013	phy->curr_user_fec_req = ICE_FEC_AUTO;
   2014
   2015	set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
   2016}
   2017
   2018/**
   2019 * ice_init_phy_user_cfg - Initialize the PHY user configuration
   2020 * @pi: port info structure
   2021 *
   2022 * Initialize the current user PHY configuration, speed, FEC, and FC requested
   2023 * mode to default. The PHY defaults are from get PHY capabilities topology
   2024 * with media so call when media is first available. An error is returned if
   2025 * called when media is not available. The PHY initialization completed state is
   2026 * set here.
   2027 *
   2028 * These configurations are used when setting PHY
   2029 * configuration. The user PHY configuration is updated on set PHY
   2030 * configuration. Returns 0 on success, negative on failure
   2031 */
   2032static int ice_init_phy_user_cfg(struct ice_port_info *pi)
   2033{
   2034	struct ice_aqc_get_phy_caps_data *pcaps;
   2035	struct ice_phy_info *phy = &pi->phy;
   2036	struct ice_pf *pf = pi->hw->back;
   2037	int err;
   2038
   2039	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
   2040		return -EIO;
   2041
   2042	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   2043	if (!pcaps)
   2044		return -ENOMEM;
   2045
   2046	if (ice_fw_supports_report_dflt_cfg(pi->hw))
   2047		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
   2048					  pcaps, NULL);
   2049	else
   2050		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
   2051					  pcaps, NULL);
   2052	if (err) {
   2053		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
   2054		goto err_out;
   2055	}
   2056
   2057	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
   2058
   2059	/* check if lenient mode is supported and enabled */
   2060	if (ice_fw_supports_link_override(pi->hw) &&
   2061	    !(pcaps->module_compliance_enforcement &
   2062	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
   2063		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
   2064
   2065		/* if the FW supports default PHY configuration mode, then the driver
   2066		 * does not have to apply link override settings. If not,
   2067		 * initialize user PHY configuration with link override values
   2068		 */
   2069		if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
   2070		    (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
   2071			ice_init_phy_cfg_dflt_override(pi);
   2072			goto out;
   2073		}
   2074	}
   2075
   2076	/* if link default override is not enabled, set user flow control and
   2077	 * FEC settings based on what get_phy_caps returned
   2078	 */
   2079	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
   2080						      pcaps->link_fec_options);
   2081	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
   2082
   2083out:
   2084	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
   2085	set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
   2086err_out:
   2087	kfree(pcaps);
   2088	return err;
   2089}
   2090
   2091/**
   2092 * ice_configure_phy - configure PHY
   2093 * @vsi: VSI of PHY
   2094 *
   2095 * Set the PHY configuration. If the current PHY configuration is the same as
   2096 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
   2097 * configure the based get PHY capabilities for topology with media.
   2098 */
   2099static int ice_configure_phy(struct ice_vsi *vsi)
   2100{
   2101	struct device *dev = ice_pf_to_dev(vsi->back);
   2102	struct ice_port_info *pi = vsi->port_info;
   2103	struct ice_aqc_get_phy_caps_data *pcaps;
   2104	struct ice_aqc_set_phy_cfg_data *cfg;
   2105	struct ice_phy_info *phy = &pi->phy;
   2106	struct ice_pf *pf = vsi->back;
   2107	int err;
   2108
   2109	/* Ensure we have media as we cannot configure a medialess port */
   2110	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
   2111		return -EPERM;
   2112
   2113	ice_print_topo_conflict(vsi);
   2114
   2115	if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
   2116	    phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
   2117		return -EPERM;
   2118
   2119	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
   2120		return ice_force_phys_link_state(vsi, true);
   2121
   2122	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   2123	if (!pcaps)
   2124		return -ENOMEM;
   2125
   2126	/* Get current PHY config */
   2127	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
   2128				  NULL);
   2129	if (err) {
   2130		dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
   2131			vsi->vsi_num, err);
   2132		goto done;
   2133	}
   2134
   2135	/* If PHY enable link is configured and configuration has not changed,
   2136	 * there's nothing to do
   2137	 */
   2138	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
   2139	    ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
   2140		goto done;
   2141
   2142	/* Use PHY topology as baseline for configuration */
   2143	memset(pcaps, 0, sizeof(*pcaps));
   2144	if (ice_fw_supports_report_dflt_cfg(pi->hw))
   2145		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
   2146					  pcaps, NULL);
   2147	else
   2148		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
   2149					  pcaps, NULL);
   2150	if (err) {
   2151		dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
   2152			vsi->vsi_num, err);
   2153		goto done;
   2154	}
   2155
   2156	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
   2157	if (!cfg) {
   2158		err = -ENOMEM;
   2159		goto done;
   2160	}
   2161
   2162	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
   2163
   2164	/* Speed - If default override pending, use curr_user_phy_cfg set in
   2165	 * ice_init_phy_user_cfg_ldo.
   2166	 */
   2167	if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
   2168			       vsi->back->state)) {
   2169		cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
   2170		cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
   2171	} else {
   2172		u64 phy_low = 0, phy_high = 0;
   2173
   2174		ice_update_phy_type(&phy_low, &phy_high,
   2175				    pi->phy.curr_user_speed_req);
   2176		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
   2177		cfg->phy_type_high = pcaps->phy_type_high &
   2178				     cpu_to_le64(phy_high);
   2179	}
   2180
   2181	/* Can't provide what was requested; use PHY capabilities */
   2182	if (!cfg->phy_type_low && !cfg->phy_type_high) {
   2183		cfg->phy_type_low = pcaps->phy_type_low;
   2184		cfg->phy_type_high = pcaps->phy_type_high;
   2185	}
   2186
   2187	/* FEC */
   2188	ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
   2189
   2190	/* Can't provide what was requested; use PHY capabilities */
   2191	if (cfg->link_fec_opt !=
   2192	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
   2193		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
   2194		cfg->link_fec_opt = pcaps->link_fec_options;
   2195	}
   2196
   2197	/* Flow Control - always supported; no need to check against
   2198	 * capabilities
   2199	 */
   2200	ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
   2201
   2202	/* Enable link and link update */
   2203	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
   2204
   2205	err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
   2206	if (err)
   2207		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
   2208			vsi->vsi_num, err);
   2209
   2210	kfree(cfg);
   2211done:
   2212	kfree(pcaps);
   2213	return err;
   2214}
   2215
   2216/**
   2217 * ice_check_media_subtask - Check for media
   2218 * @pf: pointer to PF struct
   2219 *
   2220 * If media is available, then initialize PHY user configuration if it is not
   2221 * been, and configure the PHY if the interface is up.
   2222 */
   2223static void ice_check_media_subtask(struct ice_pf *pf)
   2224{
   2225	struct ice_port_info *pi;
   2226	struct ice_vsi *vsi;
   2227	int err;
   2228
   2229	/* No need to check for media if it's already present */
   2230	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
   2231		return;
   2232
   2233	vsi = ice_get_main_vsi(pf);
   2234	if (!vsi)
   2235		return;
   2236
   2237	/* Refresh link info and check if media is present */
   2238	pi = vsi->port_info;
   2239	err = ice_update_link_info(pi);
   2240	if (err)
   2241		return;
   2242
   2243	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
   2244
   2245	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
   2246		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
   2247			ice_init_phy_user_cfg(pi);
   2248
   2249		/* PHY settings are reset on media insertion, reconfigure
   2250		 * PHY to preserve settings.
   2251		 */
   2252		if (test_bit(ICE_VSI_DOWN, vsi->state) &&
   2253		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
   2254			return;
   2255
   2256		err = ice_configure_phy(vsi);
   2257		if (!err)
   2258			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
   2259
   2260		/* A Link Status Event will be generated; the event handler
   2261		 * will complete bringing the interface up
   2262		 */
   2263	}
   2264}
   2265
   2266/**
   2267 * ice_service_task - manage and run subtasks
   2268 * @work: pointer to work_struct contained by the PF struct
   2269 */
   2270static void ice_service_task(struct work_struct *work)
   2271{
   2272	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
   2273	unsigned long start_time = jiffies;
   2274
   2275	/* subtasks */
   2276
   2277	/* process reset requests first */
   2278	ice_reset_subtask(pf);
   2279
   2280	/* bail if a reset/recovery cycle is pending or rebuild failed */
   2281	if (ice_is_reset_in_progress(pf->state) ||
   2282	    test_bit(ICE_SUSPENDED, pf->state) ||
   2283	    test_bit(ICE_NEEDS_RESTART, pf->state)) {
   2284		ice_service_task_complete(pf);
   2285		return;
   2286	}
   2287
   2288	if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
   2289		struct iidc_event *event;
   2290
   2291		event = kzalloc(sizeof(*event), GFP_KERNEL);
   2292		if (event) {
   2293			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
   2294			/* report the entire OICR value to AUX driver */
   2295			swap(event->reg, pf->oicr_err_reg);
   2296			ice_send_event_to_aux(pf, event);
   2297			kfree(event);
   2298		}
   2299	}
   2300
   2301	if (test_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags)) {
   2302		/* Plug aux device per request */
   2303		ice_plug_aux_dev(pf);
   2304
   2305		/* Mark plugging as done but check whether unplug was
   2306		 * requested during ice_plug_aux_dev() call
   2307		 * (e.g. from ice_clear_rdma_cap()) and if so then
   2308		 * plug aux device.
   2309		 */
   2310		if (!test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
   2311			ice_unplug_aux_dev(pf);
   2312	}
   2313
   2314	if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
   2315		struct iidc_event *event;
   2316
   2317		event = kzalloc(sizeof(*event), GFP_KERNEL);
   2318		if (event) {
   2319			set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
   2320			ice_send_event_to_aux(pf, event);
   2321			kfree(event);
   2322		}
   2323	}
   2324
   2325	ice_clean_adminq_subtask(pf);
   2326	ice_check_media_subtask(pf);
   2327	ice_check_for_hang_subtask(pf);
   2328	ice_sync_fltr_subtask(pf);
   2329	ice_handle_mdd_event(pf);
   2330	ice_watchdog_subtask(pf);
   2331
   2332	if (ice_is_safe_mode(pf)) {
   2333		ice_service_task_complete(pf);
   2334		return;
   2335	}
   2336
   2337	ice_process_vflr_event(pf);
   2338	ice_clean_mailboxq_subtask(pf);
   2339	ice_clean_sbq_subtask(pf);
   2340	ice_sync_arfs_fltrs(pf);
   2341	ice_flush_fdir_ctx(pf);
   2342
   2343	/* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
   2344	ice_service_task_complete(pf);
   2345
   2346	/* If the tasks have taken longer than one service timer period
   2347	 * or there is more work to be done, reset the service timer to
   2348	 * schedule the service task now.
   2349	 */
   2350	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
   2351	    test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
   2352	    test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
   2353	    test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
   2354	    test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
   2355	    test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
   2356	    test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
   2357		mod_timer(&pf->serv_tmr, jiffies);
   2358}
   2359
   2360/**
   2361 * ice_set_ctrlq_len - helper function to set controlq length
   2362 * @hw: pointer to the HW instance
   2363 */
   2364static void ice_set_ctrlq_len(struct ice_hw *hw)
   2365{
   2366	hw->adminq.num_rq_entries = ICE_AQ_LEN;
   2367	hw->adminq.num_sq_entries = ICE_AQ_LEN;
   2368	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
   2369	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
   2370	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
   2371	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
   2372	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
   2373	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
   2374	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
   2375	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
   2376	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
   2377	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
   2378}
   2379
   2380/**
   2381 * ice_schedule_reset - schedule a reset
   2382 * @pf: board private structure
   2383 * @reset: reset being requested
   2384 */
   2385int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
   2386{
   2387	struct device *dev = ice_pf_to_dev(pf);
   2388
   2389	/* bail out if earlier reset has failed */
   2390	if (test_bit(ICE_RESET_FAILED, pf->state)) {
   2391		dev_dbg(dev, "earlier reset has failed\n");
   2392		return -EIO;
   2393	}
   2394	/* bail if reset/recovery already in progress */
   2395	if (ice_is_reset_in_progress(pf->state)) {
   2396		dev_dbg(dev, "Reset already in progress\n");
   2397		return -EBUSY;
   2398	}
   2399
   2400	ice_unplug_aux_dev(pf);
   2401
   2402	switch (reset) {
   2403	case ICE_RESET_PFR:
   2404		set_bit(ICE_PFR_REQ, pf->state);
   2405		break;
   2406	case ICE_RESET_CORER:
   2407		set_bit(ICE_CORER_REQ, pf->state);
   2408		break;
   2409	case ICE_RESET_GLOBR:
   2410		set_bit(ICE_GLOBR_REQ, pf->state);
   2411		break;
   2412	default:
   2413		return -EINVAL;
   2414	}
   2415
   2416	ice_service_task_schedule(pf);
   2417	return 0;
   2418}
   2419
   2420/**
   2421 * ice_irq_affinity_notify - Callback for affinity changes
   2422 * @notify: context as to what irq was changed
   2423 * @mask: the new affinity mask
   2424 *
   2425 * This is a callback function used by the irq_set_affinity_notifier function
   2426 * so that we may register to receive changes to the irq affinity masks.
   2427 */
   2428static void
   2429ice_irq_affinity_notify(struct irq_affinity_notify *notify,
   2430			const cpumask_t *mask)
   2431{
   2432	struct ice_q_vector *q_vector =
   2433		container_of(notify, struct ice_q_vector, affinity_notify);
   2434
   2435	cpumask_copy(&q_vector->affinity_mask, mask);
   2436}
   2437
   2438/**
   2439 * ice_irq_affinity_release - Callback for affinity notifier release
   2440 * @ref: internal core kernel usage
   2441 *
   2442 * This is a callback function used by the irq_set_affinity_notifier function
   2443 * to inform the current notification subscriber that they will no longer
   2444 * receive notifications.
   2445 */
   2446static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
   2447
   2448/**
   2449 * ice_vsi_ena_irq - Enable IRQ for the given VSI
   2450 * @vsi: the VSI being configured
   2451 */
   2452static int ice_vsi_ena_irq(struct ice_vsi *vsi)
   2453{
   2454	struct ice_hw *hw = &vsi->back->hw;
   2455	int i;
   2456
   2457	ice_for_each_q_vector(vsi, i)
   2458		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
   2459
   2460	ice_flush(hw);
   2461	return 0;
   2462}
   2463
   2464/**
   2465 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
   2466 * @vsi: the VSI being configured
   2467 * @basename: name for the vector
   2468 */
   2469static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
   2470{
   2471	int q_vectors = vsi->num_q_vectors;
   2472	struct ice_pf *pf = vsi->back;
   2473	int base = vsi->base_vector;
   2474	struct device *dev;
   2475	int rx_int_idx = 0;
   2476	int tx_int_idx = 0;
   2477	int vector, err;
   2478	int irq_num;
   2479
   2480	dev = ice_pf_to_dev(pf);
   2481	for (vector = 0; vector < q_vectors; vector++) {
   2482		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
   2483
   2484		irq_num = pf->msix_entries[base + vector].vector;
   2485
   2486		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
   2487			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
   2488				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
   2489			tx_int_idx++;
   2490		} else if (q_vector->rx.rx_ring) {
   2491			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
   2492				 "%s-%s-%d", basename, "rx", rx_int_idx++);
   2493		} else if (q_vector->tx.tx_ring) {
   2494			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
   2495				 "%s-%s-%d", basename, "tx", tx_int_idx++);
   2496		} else {
   2497			/* skip this unused q_vector */
   2498			continue;
   2499		}
   2500		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
   2501			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
   2502					       IRQF_SHARED, q_vector->name,
   2503					       q_vector);
   2504		else
   2505			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
   2506					       0, q_vector->name, q_vector);
   2507		if (err) {
   2508			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
   2509				   err);
   2510			goto free_q_irqs;
   2511		}
   2512
   2513		/* register for affinity change notifications */
   2514		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
   2515			struct irq_affinity_notify *affinity_notify;
   2516
   2517			affinity_notify = &q_vector->affinity_notify;
   2518			affinity_notify->notify = ice_irq_affinity_notify;
   2519			affinity_notify->release = ice_irq_affinity_release;
   2520			irq_set_affinity_notifier(irq_num, affinity_notify);
   2521		}
   2522
   2523		/* assign the mask for this irq */
   2524		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
   2525	}
   2526
   2527	err = ice_set_cpu_rx_rmap(vsi);
   2528	if (err) {
   2529		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
   2530			   vsi->vsi_num, ERR_PTR(err));
   2531		goto free_q_irqs;
   2532	}
   2533
   2534	vsi->irqs_ready = true;
   2535	return 0;
   2536
   2537free_q_irqs:
   2538	while (vector) {
   2539		vector--;
   2540		irq_num = pf->msix_entries[base + vector].vector;
   2541		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
   2542			irq_set_affinity_notifier(irq_num, NULL);
   2543		irq_set_affinity_hint(irq_num, NULL);
   2544		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
   2545	}
   2546	return err;
   2547}
   2548
   2549/**
   2550 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
   2551 * @vsi: VSI to setup Tx rings used by XDP
   2552 *
   2553 * Return 0 on success and negative value on error
   2554 */
   2555static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
   2556{
   2557	struct device *dev = ice_pf_to_dev(vsi->back);
   2558	struct ice_tx_desc *tx_desc;
   2559	int i, j;
   2560
   2561	ice_for_each_xdp_txq(vsi, i) {
   2562		u16 xdp_q_idx = vsi->alloc_txq + i;
   2563		struct ice_tx_ring *xdp_ring;
   2564
   2565		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
   2566
   2567		if (!xdp_ring)
   2568			goto free_xdp_rings;
   2569
   2570		xdp_ring->q_index = xdp_q_idx;
   2571		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
   2572		xdp_ring->vsi = vsi;
   2573		xdp_ring->netdev = NULL;
   2574		xdp_ring->dev = dev;
   2575		xdp_ring->count = vsi->num_tx_desc;
   2576		xdp_ring->next_dd = ICE_RING_QUARTER(xdp_ring) - 1;
   2577		xdp_ring->next_rs = ICE_RING_QUARTER(xdp_ring) - 1;
   2578		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
   2579		if (ice_setup_tx_ring(xdp_ring))
   2580			goto free_xdp_rings;
   2581		ice_set_ring_xdp(xdp_ring);
   2582		xdp_ring->xsk_pool = ice_tx_xsk_pool(xdp_ring);
   2583		spin_lock_init(&xdp_ring->tx_lock);
   2584		for (j = 0; j < xdp_ring->count; j++) {
   2585			tx_desc = ICE_TX_DESC(xdp_ring, j);
   2586			tx_desc->cmd_type_offset_bsz = 0;
   2587		}
   2588	}
   2589
   2590	ice_for_each_rxq(vsi, i) {
   2591		if (static_key_enabled(&ice_xdp_locking_key))
   2592			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
   2593		else
   2594			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i];
   2595	}
   2596
   2597	return 0;
   2598
   2599free_xdp_rings:
   2600	for (; i >= 0; i--)
   2601		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
   2602			ice_free_tx_ring(vsi->xdp_rings[i]);
   2603	return -ENOMEM;
   2604}
   2605
   2606/**
   2607 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
   2608 * @vsi: VSI to set the bpf prog on
   2609 * @prog: the bpf prog pointer
   2610 */
   2611static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
   2612{
   2613	struct bpf_prog *old_prog;
   2614	int i;
   2615
   2616	old_prog = xchg(&vsi->xdp_prog, prog);
   2617	if (old_prog)
   2618		bpf_prog_put(old_prog);
   2619
   2620	ice_for_each_rxq(vsi, i)
   2621		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
   2622}
   2623
   2624/**
   2625 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
   2626 * @vsi: VSI to bring up Tx rings used by XDP
   2627 * @prog: bpf program that will be assigned to VSI
   2628 *
   2629 * Return 0 on success and negative value on error
   2630 */
   2631int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
   2632{
   2633	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
   2634	int xdp_rings_rem = vsi->num_xdp_txq;
   2635	struct ice_pf *pf = vsi->back;
   2636	struct ice_qs_cfg xdp_qs_cfg = {
   2637		.qs_mutex = &pf->avail_q_mutex,
   2638		.pf_map = pf->avail_txqs,
   2639		.pf_map_size = pf->max_pf_txqs,
   2640		.q_count = vsi->num_xdp_txq,
   2641		.scatter_count = ICE_MAX_SCATTER_TXQS,
   2642		.vsi_map = vsi->txq_map,
   2643		.vsi_map_offset = vsi->alloc_txq,
   2644		.mapping_mode = ICE_VSI_MAP_CONTIG
   2645	};
   2646	struct device *dev;
   2647	int i, v_idx;
   2648	int status;
   2649
   2650	dev = ice_pf_to_dev(pf);
   2651	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
   2652				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
   2653	if (!vsi->xdp_rings)
   2654		return -ENOMEM;
   2655
   2656	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
   2657	if (__ice_vsi_get_qs(&xdp_qs_cfg))
   2658		goto err_map_xdp;
   2659
   2660	if (static_key_enabled(&ice_xdp_locking_key))
   2661		netdev_warn(vsi->netdev,
   2662			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
   2663
   2664	if (ice_xdp_alloc_setup_rings(vsi))
   2665		goto clear_xdp_rings;
   2666
   2667	/* follow the logic from ice_vsi_map_rings_to_vectors */
   2668	ice_for_each_q_vector(vsi, v_idx) {
   2669		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
   2670		int xdp_rings_per_v, q_id, q_base;
   2671
   2672		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
   2673					       vsi->num_q_vectors - v_idx);
   2674		q_base = vsi->num_xdp_txq - xdp_rings_rem;
   2675
   2676		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
   2677			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
   2678
   2679			xdp_ring->q_vector = q_vector;
   2680			xdp_ring->next = q_vector->tx.tx_ring;
   2681			q_vector->tx.tx_ring = xdp_ring;
   2682		}
   2683		xdp_rings_rem -= xdp_rings_per_v;
   2684	}
   2685
   2686	/* omit the scheduler update if in reset path; XDP queues will be
   2687	 * taken into account at the end of ice_vsi_rebuild, where
   2688	 * ice_cfg_vsi_lan is being called
   2689	 */
   2690	if (ice_is_reset_in_progress(pf->state))
   2691		return 0;
   2692
   2693	/* tell the Tx scheduler that right now we have
   2694	 * additional queues
   2695	 */
   2696	for (i = 0; i < vsi->tc_cfg.numtc; i++)
   2697		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
   2698
   2699	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
   2700				 max_txqs);
   2701	if (status) {
   2702		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
   2703			status);
   2704		goto clear_xdp_rings;
   2705	}
   2706
   2707	/* assign the prog only when it's not already present on VSI;
   2708	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
   2709	 * VSI rebuild that happens under ethtool -L can expose us to
   2710	 * the bpf_prog refcount issues as we would be swapping same
   2711	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
   2712	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
   2713	 * this is not harmful as dev_xdp_install bumps the refcount
   2714	 * before calling the op exposed by the driver;
   2715	 */
   2716	if (!ice_is_xdp_ena_vsi(vsi))
   2717		ice_vsi_assign_bpf_prog(vsi, prog);
   2718
   2719	return 0;
   2720clear_xdp_rings:
   2721	ice_for_each_xdp_txq(vsi, i)
   2722		if (vsi->xdp_rings[i]) {
   2723			kfree_rcu(vsi->xdp_rings[i], rcu);
   2724			vsi->xdp_rings[i] = NULL;
   2725		}
   2726
   2727err_map_xdp:
   2728	mutex_lock(&pf->avail_q_mutex);
   2729	ice_for_each_xdp_txq(vsi, i) {
   2730		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
   2731		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
   2732	}
   2733	mutex_unlock(&pf->avail_q_mutex);
   2734
   2735	devm_kfree(dev, vsi->xdp_rings);
   2736	return -ENOMEM;
   2737}
   2738
   2739/**
   2740 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
   2741 * @vsi: VSI to remove XDP rings
   2742 *
   2743 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
   2744 * resources
   2745 */
   2746int ice_destroy_xdp_rings(struct ice_vsi *vsi)
   2747{
   2748	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
   2749	struct ice_pf *pf = vsi->back;
   2750	int i, v_idx;
   2751
   2752	/* q_vectors are freed in reset path so there's no point in detaching
   2753	 * rings; in case of rebuild being triggered not from reset bits
   2754	 * in pf->state won't be set, so additionally check first q_vector
   2755	 * against NULL
   2756	 */
   2757	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
   2758		goto free_qmap;
   2759
   2760	ice_for_each_q_vector(vsi, v_idx) {
   2761		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
   2762		struct ice_tx_ring *ring;
   2763
   2764		ice_for_each_tx_ring(ring, q_vector->tx)
   2765			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
   2766				break;
   2767
   2768		/* restore the value of last node prior to XDP setup */
   2769		q_vector->tx.tx_ring = ring;
   2770	}
   2771
   2772free_qmap:
   2773	mutex_lock(&pf->avail_q_mutex);
   2774	ice_for_each_xdp_txq(vsi, i) {
   2775		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
   2776		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
   2777	}
   2778	mutex_unlock(&pf->avail_q_mutex);
   2779
   2780	ice_for_each_xdp_txq(vsi, i)
   2781		if (vsi->xdp_rings[i]) {
   2782			if (vsi->xdp_rings[i]->desc) {
   2783				synchronize_rcu();
   2784				ice_free_tx_ring(vsi->xdp_rings[i]);
   2785			}
   2786			kfree_rcu(vsi->xdp_rings[i], rcu);
   2787			vsi->xdp_rings[i] = NULL;
   2788		}
   2789
   2790	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
   2791	vsi->xdp_rings = NULL;
   2792
   2793	if (static_key_enabled(&ice_xdp_locking_key))
   2794		static_branch_dec(&ice_xdp_locking_key);
   2795
   2796	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
   2797		return 0;
   2798
   2799	ice_vsi_assign_bpf_prog(vsi, NULL);
   2800
   2801	/* notify Tx scheduler that we destroyed XDP queues and bring
   2802	 * back the old number of child nodes
   2803	 */
   2804	for (i = 0; i < vsi->tc_cfg.numtc; i++)
   2805		max_txqs[i] = vsi->num_txq;
   2806
   2807	/* change number of XDP Tx queues to 0 */
   2808	vsi->num_xdp_txq = 0;
   2809
   2810	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
   2811			       max_txqs);
   2812}
   2813
   2814/**
   2815 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
   2816 * @vsi: VSI to schedule napi on
   2817 */
   2818static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
   2819{
   2820	int i;
   2821
   2822	ice_for_each_rxq(vsi, i) {
   2823		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
   2824
   2825		if (rx_ring->xsk_pool)
   2826			napi_schedule(&rx_ring->q_vector->napi);
   2827	}
   2828}
   2829
   2830/**
   2831 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
   2832 * @vsi: VSI to determine the count of XDP Tx qs
   2833 *
   2834 * returns 0 if Tx qs count is higher than at least half of CPU count,
   2835 * -ENOMEM otherwise
   2836 */
   2837int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
   2838{
   2839	u16 avail = ice_get_avail_txq_count(vsi->back);
   2840	u16 cpus = num_possible_cpus();
   2841
   2842	if (avail < cpus / 2)
   2843		return -ENOMEM;
   2844
   2845	vsi->num_xdp_txq = min_t(u16, avail, cpus);
   2846
   2847	if (vsi->num_xdp_txq < cpus)
   2848		static_branch_inc(&ice_xdp_locking_key);
   2849
   2850	return 0;
   2851}
   2852
   2853/**
   2854 * ice_xdp_setup_prog - Add or remove XDP eBPF program
   2855 * @vsi: VSI to setup XDP for
   2856 * @prog: XDP program
   2857 * @extack: netlink extended ack
   2858 */
   2859static int
   2860ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
   2861		   struct netlink_ext_ack *extack)
   2862{
   2863	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
   2864	bool if_running = netif_running(vsi->netdev);
   2865	int ret = 0, xdp_ring_err = 0;
   2866
   2867	if (frame_size > vsi->rx_buf_len) {
   2868		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
   2869		return -EOPNOTSUPP;
   2870	}
   2871
   2872	/* need to stop netdev while setting up the program for Rx rings */
   2873	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
   2874		ret = ice_down(vsi);
   2875		if (ret) {
   2876			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
   2877			return ret;
   2878		}
   2879	}
   2880
   2881	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
   2882		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
   2883		if (xdp_ring_err) {
   2884			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
   2885		} else {
   2886			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
   2887			if (xdp_ring_err)
   2888				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
   2889		}
   2890	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
   2891		xdp_ring_err = ice_destroy_xdp_rings(vsi);
   2892		if (xdp_ring_err)
   2893			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
   2894	} else {
   2895		/* safe to call even when prog == vsi->xdp_prog as
   2896		 * dev_xdp_install in net/core/dev.c incremented prog's
   2897		 * refcount so corresponding bpf_prog_put won't cause
   2898		 * underflow
   2899		 */
   2900		ice_vsi_assign_bpf_prog(vsi, prog);
   2901	}
   2902
   2903	if (if_running)
   2904		ret = ice_up(vsi);
   2905
   2906	if (!ret && prog)
   2907		ice_vsi_rx_napi_schedule(vsi);
   2908
   2909	return (ret || xdp_ring_err) ? -ENOMEM : 0;
   2910}
   2911
   2912/**
   2913 * ice_xdp_safe_mode - XDP handler for safe mode
   2914 * @dev: netdevice
   2915 * @xdp: XDP command
   2916 */
   2917static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
   2918			     struct netdev_bpf *xdp)
   2919{
   2920	NL_SET_ERR_MSG_MOD(xdp->extack,
   2921			   "Please provide working DDP firmware package in order to use XDP\n"
   2922			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
   2923	return -EOPNOTSUPP;
   2924}
   2925
   2926/**
   2927 * ice_xdp - implements XDP handler
   2928 * @dev: netdevice
   2929 * @xdp: XDP command
   2930 */
   2931static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
   2932{
   2933	struct ice_netdev_priv *np = netdev_priv(dev);
   2934	struct ice_vsi *vsi = np->vsi;
   2935
   2936	if (vsi->type != ICE_VSI_PF) {
   2937		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
   2938		return -EINVAL;
   2939	}
   2940
   2941	switch (xdp->command) {
   2942	case XDP_SETUP_PROG:
   2943		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
   2944	case XDP_SETUP_XSK_POOL:
   2945		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
   2946					  xdp->xsk.queue_id);
   2947	default:
   2948		return -EINVAL;
   2949	}
   2950}
   2951
   2952/**
   2953 * ice_ena_misc_vector - enable the non-queue interrupts
   2954 * @pf: board private structure
   2955 */
   2956static void ice_ena_misc_vector(struct ice_pf *pf)
   2957{
   2958	struct ice_hw *hw = &pf->hw;
   2959	u32 val;
   2960
   2961	/* Disable anti-spoof detection interrupt to prevent spurious event
   2962	 * interrupts during a function reset. Anti-spoof functionally is
   2963	 * still supported.
   2964	 */
   2965	val = rd32(hw, GL_MDCK_TX_TDPU);
   2966	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
   2967	wr32(hw, GL_MDCK_TX_TDPU, val);
   2968
   2969	/* clear things first */
   2970	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
   2971	rd32(hw, PFINT_OICR);		/* read to clear */
   2972
   2973	val = (PFINT_OICR_ECC_ERR_M |
   2974	       PFINT_OICR_MAL_DETECT_M |
   2975	       PFINT_OICR_GRST_M |
   2976	       PFINT_OICR_PCI_EXCEPTION_M |
   2977	       PFINT_OICR_VFLR_M |
   2978	       PFINT_OICR_HMC_ERR_M |
   2979	       PFINT_OICR_PE_PUSH_M |
   2980	       PFINT_OICR_PE_CRITERR_M);
   2981
   2982	wr32(hw, PFINT_OICR_ENA, val);
   2983
   2984	/* SW_ITR_IDX = 0, but don't change INTENA */
   2985	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
   2986	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
   2987}
   2988
   2989/**
   2990 * ice_misc_intr - misc interrupt handler
   2991 * @irq: interrupt number
   2992 * @data: pointer to a q_vector
   2993 */
   2994static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
   2995{
   2996	struct ice_pf *pf = (struct ice_pf *)data;
   2997	struct ice_hw *hw = &pf->hw;
   2998	irqreturn_t ret = IRQ_NONE;
   2999	struct device *dev;
   3000	u32 oicr, ena_mask;
   3001
   3002	dev = ice_pf_to_dev(pf);
   3003	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
   3004	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
   3005	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
   3006
   3007	oicr = rd32(hw, PFINT_OICR);
   3008	ena_mask = rd32(hw, PFINT_OICR_ENA);
   3009
   3010	if (oicr & PFINT_OICR_SWINT_M) {
   3011		ena_mask &= ~PFINT_OICR_SWINT_M;
   3012		pf->sw_int_count++;
   3013	}
   3014
   3015	if (oicr & PFINT_OICR_MAL_DETECT_M) {
   3016		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
   3017		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
   3018	}
   3019	if (oicr & PFINT_OICR_VFLR_M) {
   3020		/* disable any further VFLR event notifications */
   3021		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
   3022			u32 reg = rd32(hw, PFINT_OICR_ENA);
   3023
   3024			reg &= ~PFINT_OICR_VFLR_M;
   3025			wr32(hw, PFINT_OICR_ENA, reg);
   3026		} else {
   3027			ena_mask &= ~PFINT_OICR_VFLR_M;
   3028			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
   3029		}
   3030	}
   3031
   3032	if (oicr & PFINT_OICR_GRST_M) {
   3033		u32 reset;
   3034
   3035		/* we have a reset warning */
   3036		ena_mask &= ~PFINT_OICR_GRST_M;
   3037		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
   3038			GLGEN_RSTAT_RESET_TYPE_S;
   3039
   3040		if (reset == ICE_RESET_CORER)
   3041			pf->corer_count++;
   3042		else if (reset == ICE_RESET_GLOBR)
   3043			pf->globr_count++;
   3044		else if (reset == ICE_RESET_EMPR)
   3045			pf->empr_count++;
   3046		else
   3047			dev_dbg(dev, "Invalid reset type %d\n", reset);
   3048
   3049		/* If a reset cycle isn't already in progress, we set a bit in
   3050		 * pf->state so that the service task can start a reset/rebuild.
   3051		 */
   3052		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
   3053			if (reset == ICE_RESET_CORER)
   3054				set_bit(ICE_CORER_RECV, pf->state);
   3055			else if (reset == ICE_RESET_GLOBR)
   3056				set_bit(ICE_GLOBR_RECV, pf->state);
   3057			else
   3058				set_bit(ICE_EMPR_RECV, pf->state);
   3059
   3060			/* There are couple of different bits at play here.
   3061			 * hw->reset_ongoing indicates whether the hardware is
   3062			 * in reset. This is set to true when a reset interrupt
   3063			 * is received and set back to false after the driver
   3064			 * has determined that the hardware is out of reset.
   3065			 *
   3066			 * ICE_RESET_OICR_RECV in pf->state indicates
   3067			 * that a post reset rebuild is required before the
   3068			 * driver is operational again. This is set above.
   3069			 *
   3070			 * As this is the start of the reset/rebuild cycle, set
   3071			 * both to indicate that.
   3072			 */
   3073			hw->reset_ongoing = true;
   3074		}
   3075	}
   3076
   3077	if (oicr & PFINT_OICR_TSYN_TX_M) {
   3078		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
   3079		ice_ptp_process_ts(pf);
   3080	}
   3081
   3082	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
   3083		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
   3084		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
   3085
   3086		/* Save EVENTs from GTSYN register */
   3087		pf->ptp.ext_ts_irq |= gltsyn_stat & (GLTSYN_STAT_EVENT0_M |
   3088						     GLTSYN_STAT_EVENT1_M |
   3089						     GLTSYN_STAT_EVENT2_M);
   3090		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
   3091		kthread_queue_work(pf->ptp.kworker, &pf->ptp.extts_work);
   3092	}
   3093
   3094#define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
   3095	if (oicr & ICE_AUX_CRIT_ERR) {
   3096		pf->oicr_err_reg |= oicr;
   3097		set_bit(ICE_AUX_ERR_PENDING, pf->state);
   3098		ena_mask &= ~ICE_AUX_CRIT_ERR;
   3099	}
   3100
   3101	/* Report any remaining unexpected interrupts */
   3102	oicr &= ena_mask;
   3103	if (oicr) {
   3104		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
   3105		/* If a critical error is pending there is no choice but to
   3106		 * reset the device.
   3107		 */
   3108		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
   3109			    PFINT_OICR_ECC_ERR_M)) {
   3110			set_bit(ICE_PFR_REQ, pf->state);
   3111			ice_service_task_schedule(pf);
   3112		}
   3113	}
   3114	ret = IRQ_HANDLED;
   3115
   3116	ice_service_task_schedule(pf);
   3117	ice_irq_dynamic_ena(hw, NULL, NULL);
   3118
   3119	return ret;
   3120}
   3121
   3122/**
   3123 * ice_dis_ctrlq_interrupts - disable control queue interrupts
   3124 * @hw: pointer to HW structure
   3125 */
   3126static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
   3127{
   3128	/* disable Admin queue Interrupt causes */
   3129	wr32(hw, PFINT_FW_CTL,
   3130	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
   3131
   3132	/* disable Mailbox queue Interrupt causes */
   3133	wr32(hw, PFINT_MBX_CTL,
   3134	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
   3135
   3136	wr32(hw, PFINT_SB_CTL,
   3137	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
   3138
   3139	/* disable Control queue Interrupt causes */
   3140	wr32(hw, PFINT_OICR_CTL,
   3141	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
   3142
   3143	ice_flush(hw);
   3144}
   3145
   3146/**
   3147 * ice_free_irq_msix_misc - Unroll misc vector setup
   3148 * @pf: board private structure
   3149 */
   3150static void ice_free_irq_msix_misc(struct ice_pf *pf)
   3151{
   3152	struct ice_hw *hw = &pf->hw;
   3153
   3154	ice_dis_ctrlq_interrupts(hw);
   3155
   3156	/* disable OICR interrupt */
   3157	wr32(hw, PFINT_OICR_ENA, 0);
   3158	ice_flush(hw);
   3159
   3160	if (pf->msix_entries) {
   3161		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
   3162		devm_free_irq(ice_pf_to_dev(pf),
   3163			      pf->msix_entries[pf->oicr_idx].vector, pf);
   3164	}
   3165
   3166	pf->num_avail_sw_msix += 1;
   3167	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
   3168}
   3169
   3170/**
   3171 * ice_ena_ctrlq_interrupts - enable control queue interrupts
   3172 * @hw: pointer to HW structure
   3173 * @reg_idx: HW vector index to associate the control queue interrupts with
   3174 */
   3175static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
   3176{
   3177	u32 val;
   3178
   3179	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
   3180	       PFINT_OICR_CTL_CAUSE_ENA_M);
   3181	wr32(hw, PFINT_OICR_CTL, val);
   3182
   3183	/* enable Admin queue Interrupt causes */
   3184	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
   3185	       PFINT_FW_CTL_CAUSE_ENA_M);
   3186	wr32(hw, PFINT_FW_CTL, val);
   3187
   3188	/* enable Mailbox queue Interrupt causes */
   3189	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
   3190	       PFINT_MBX_CTL_CAUSE_ENA_M);
   3191	wr32(hw, PFINT_MBX_CTL, val);
   3192
   3193	/* This enables Sideband queue Interrupt causes */
   3194	val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
   3195	       PFINT_SB_CTL_CAUSE_ENA_M);
   3196	wr32(hw, PFINT_SB_CTL, val);
   3197
   3198	ice_flush(hw);
   3199}
   3200
   3201/**
   3202 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
   3203 * @pf: board private structure
   3204 *
   3205 * This sets up the handler for MSIX 0, which is used to manage the
   3206 * non-queue interrupts, e.g. AdminQ and errors. This is not used
   3207 * when in MSI or Legacy interrupt mode.
   3208 */
   3209static int ice_req_irq_msix_misc(struct ice_pf *pf)
   3210{
   3211	struct device *dev = ice_pf_to_dev(pf);
   3212	struct ice_hw *hw = &pf->hw;
   3213	int oicr_idx, err = 0;
   3214
   3215	if (!pf->int_name[0])
   3216		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
   3217			 dev_driver_string(dev), dev_name(dev));
   3218
   3219	/* Do not request IRQ but do enable OICR interrupt since settings are
   3220	 * lost during reset. Note that this function is called only during
   3221	 * rebuild path and not while reset is in progress.
   3222	 */
   3223	if (ice_is_reset_in_progress(pf->state))
   3224		goto skip_req_irq;
   3225
   3226	/* reserve one vector in irq_tracker for misc interrupts */
   3227	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
   3228	if (oicr_idx < 0)
   3229		return oicr_idx;
   3230
   3231	pf->num_avail_sw_msix -= 1;
   3232	pf->oicr_idx = (u16)oicr_idx;
   3233
   3234	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
   3235			       ice_misc_intr, 0, pf->int_name, pf);
   3236	if (err) {
   3237		dev_err(dev, "devm_request_irq for %s failed: %d\n",
   3238			pf->int_name, err);
   3239		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
   3240		pf->num_avail_sw_msix += 1;
   3241		return err;
   3242	}
   3243
   3244skip_req_irq:
   3245	ice_ena_misc_vector(pf);
   3246
   3247	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
   3248	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
   3249	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
   3250
   3251	ice_flush(hw);
   3252	ice_irq_dynamic_ena(hw, NULL, NULL);
   3253
   3254	return 0;
   3255}
   3256
   3257/**
   3258 * ice_napi_add - register NAPI handler for the VSI
   3259 * @vsi: VSI for which NAPI handler is to be registered
   3260 *
   3261 * This function is only called in the driver's load path. Registering the NAPI
   3262 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
   3263 * reset/rebuild, etc.)
   3264 */
   3265static void ice_napi_add(struct ice_vsi *vsi)
   3266{
   3267	int v_idx;
   3268
   3269	if (!vsi->netdev)
   3270		return;
   3271
   3272	ice_for_each_q_vector(vsi, v_idx)
   3273		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
   3274			       ice_napi_poll, NAPI_POLL_WEIGHT);
   3275}
   3276
   3277/**
   3278 * ice_set_ops - set netdev and ethtools ops for the given netdev
   3279 * @netdev: netdev instance
   3280 */
   3281static void ice_set_ops(struct net_device *netdev)
   3282{
   3283	struct ice_pf *pf = ice_netdev_to_pf(netdev);
   3284
   3285	if (ice_is_safe_mode(pf)) {
   3286		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
   3287		ice_set_ethtool_safe_mode_ops(netdev);
   3288		return;
   3289	}
   3290
   3291	netdev->netdev_ops = &ice_netdev_ops;
   3292	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
   3293	ice_set_ethtool_ops(netdev);
   3294}
   3295
   3296/**
   3297 * ice_set_netdev_features - set features for the given netdev
   3298 * @netdev: netdev instance
   3299 */
   3300static void ice_set_netdev_features(struct net_device *netdev)
   3301{
   3302	struct ice_pf *pf = ice_netdev_to_pf(netdev);
   3303	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
   3304	netdev_features_t csumo_features;
   3305	netdev_features_t vlano_features;
   3306	netdev_features_t dflt_features;
   3307	netdev_features_t tso_features;
   3308
   3309	if (ice_is_safe_mode(pf)) {
   3310		/* safe mode */
   3311		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
   3312		netdev->hw_features = netdev->features;
   3313		return;
   3314	}
   3315
   3316	dflt_features = NETIF_F_SG	|
   3317			NETIF_F_HIGHDMA	|
   3318			NETIF_F_NTUPLE	|
   3319			NETIF_F_RXHASH;
   3320
   3321	csumo_features = NETIF_F_RXCSUM	  |
   3322			 NETIF_F_IP_CSUM  |
   3323			 NETIF_F_SCTP_CRC |
   3324			 NETIF_F_IPV6_CSUM;
   3325
   3326	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
   3327			 NETIF_F_HW_VLAN_CTAG_TX     |
   3328			 NETIF_F_HW_VLAN_CTAG_RX;
   3329
   3330	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
   3331	if (is_dvm_ena)
   3332		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
   3333
   3334	tso_features = NETIF_F_TSO			|
   3335		       NETIF_F_TSO_ECN			|
   3336		       NETIF_F_TSO6			|
   3337		       NETIF_F_GSO_GRE			|
   3338		       NETIF_F_GSO_UDP_TUNNEL		|
   3339		       NETIF_F_GSO_GRE_CSUM		|
   3340		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
   3341		       NETIF_F_GSO_PARTIAL		|
   3342		       NETIF_F_GSO_IPXIP4		|
   3343		       NETIF_F_GSO_IPXIP6		|
   3344		       NETIF_F_GSO_UDP_L4;
   3345
   3346	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
   3347					NETIF_F_GSO_GRE_CSUM;
   3348	/* set features that user can change */
   3349	netdev->hw_features = dflt_features | csumo_features |
   3350			      vlano_features | tso_features;
   3351
   3352	/* add support for HW_CSUM on packets with MPLS header */
   3353	netdev->mpls_features =  NETIF_F_HW_CSUM |
   3354				 NETIF_F_TSO     |
   3355				 NETIF_F_TSO6;
   3356
   3357	/* enable features */
   3358	netdev->features |= netdev->hw_features;
   3359
   3360	netdev->hw_features |= NETIF_F_HW_TC;
   3361
   3362	/* encap and VLAN devices inherit default, csumo and tso features */
   3363	netdev->hw_enc_features |= dflt_features | csumo_features |
   3364				   tso_features;
   3365	netdev->vlan_features |= dflt_features | csumo_features |
   3366				 tso_features;
   3367
   3368	/* advertise support but don't enable by default since only one type of
   3369	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
   3370	 * type turns on the other has to be turned off. This is enforced by the
   3371	 * ice_fix_features() ndo callback.
   3372	 */
   3373	if (is_dvm_ena)
   3374		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
   3375			NETIF_F_HW_VLAN_STAG_TX;
   3376}
   3377
   3378/**
   3379 * ice_cfg_netdev - Allocate, configure and register a netdev
   3380 * @vsi: the VSI associated with the new netdev
   3381 *
   3382 * Returns 0 on success, negative value on failure
   3383 */
   3384static int ice_cfg_netdev(struct ice_vsi *vsi)
   3385{
   3386	struct ice_netdev_priv *np;
   3387	struct net_device *netdev;
   3388	u8 mac_addr[ETH_ALEN];
   3389
   3390	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
   3391				    vsi->alloc_rxq);
   3392	if (!netdev)
   3393		return -ENOMEM;
   3394
   3395	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
   3396	vsi->netdev = netdev;
   3397	np = netdev_priv(netdev);
   3398	np->vsi = vsi;
   3399
   3400	ice_set_netdev_features(netdev);
   3401
   3402	ice_set_ops(netdev);
   3403
   3404	if (vsi->type == ICE_VSI_PF) {
   3405		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
   3406		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
   3407		eth_hw_addr_set(netdev, mac_addr);
   3408		ether_addr_copy(netdev->perm_addr, mac_addr);
   3409	}
   3410
   3411	netdev->priv_flags |= IFF_UNICAST_FLT;
   3412
   3413	/* Setup netdev TC information */
   3414	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
   3415
   3416	/* setup watchdog timeout value to be 5 second */
   3417	netdev->watchdog_timeo = 5 * HZ;
   3418
   3419	netdev->min_mtu = ETH_MIN_MTU;
   3420	netdev->max_mtu = ICE_MAX_MTU;
   3421
   3422	return 0;
   3423}
   3424
   3425/**
   3426 * ice_fill_rss_lut - Fill the RSS lookup table with default values
   3427 * @lut: Lookup table
   3428 * @rss_table_size: Lookup table size
   3429 * @rss_size: Range of queue number for hashing
   3430 */
   3431void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
   3432{
   3433	u16 i;
   3434
   3435	for (i = 0; i < rss_table_size; i++)
   3436		lut[i] = i % rss_size;
   3437}
   3438
   3439/**
   3440 * ice_pf_vsi_setup - Set up a PF VSI
   3441 * @pf: board private structure
   3442 * @pi: pointer to the port_info instance
   3443 *
   3444 * Returns pointer to the successfully allocated VSI software struct
   3445 * on success, otherwise returns NULL on failure.
   3446 */
   3447static struct ice_vsi *
   3448ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
   3449{
   3450	return ice_vsi_setup(pf, pi, ICE_VSI_PF, NULL, NULL);
   3451}
   3452
   3453static struct ice_vsi *
   3454ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
   3455		   struct ice_channel *ch)
   3456{
   3457	return ice_vsi_setup(pf, pi, ICE_VSI_CHNL, NULL, ch);
   3458}
   3459
   3460/**
   3461 * ice_ctrl_vsi_setup - Set up a control VSI
   3462 * @pf: board private structure
   3463 * @pi: pointer to the port_info instance
   3464 *
   3465 * Returns pointer to the successfully allocated VSI software struct
   3466 * on success, otherwise returns NULL on failure.
   3467 */
   3468static struct ice_vsi *
   3469ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
   3470{
   3471	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, NULL, NULL);
   3472}
   3473
   3474/**
   3475 * ice_lb_vsi_setup - Set up a loopback VSI
   3476 * @pf: board private structure
   3477 * @pi: pointer to the port_info instance
   3478 *
   3479 * Returns pointer to the successfully allocated VSI software struct
   3480 * on success, otherwise returns NULL on failure.
   3481 */
   3482struct ice_vsi *
   3483ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
   3484{
   3485	return ice_vsi_setup(pf, pi, ICE_VSI_LB, NULL, NULL);
   3486}
   3487
   3488/**
   3489 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
   3490 * @netdev: network interface to be adjusted
   3491 * @proto: VLAN TPID
   3492 * @vid: VLAN ID to be added
   3493 *
   3494 * net_device_ops implementation for adding VLAN IDs
   3495 */
   3496static int
   3497ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
   3498{
   3499	struct ice_netdev_priv *np = netdev_priv(netdev);
   3500	struct ice_vsi_vlan_ops *vlan_ops;
   3501	struct ice_vsi *vsi = np->vsi;
   3502	struct ice_vlan vlan;
   3503	int ret;
   3504
   3505	/* VLAN 0 is added by default during load/reset */
   3506	if (!vid)
   3507		return 0;
   3508
   3509	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
   3510		usleep_range(1000, 2000);
   3511
   3512	/* Add multicast promisc rule for the VLAN ID to be added if
   3513	 * all-multicast is currently enabled.
   3514	 */
   3515	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
   3516		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
   3517					       ICE_MCAST_VLAN_PROMISC_BITS,
   3518					       vid);
   3519		if (ret)
   3520			goto finish;
   3521	}
   3522
   3523	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
   3524
   3525	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
   3526	 * packets aren't pruned by the device's internal switch on Rx
   3527	 */
   3528	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
   3529	ret = vlan_ops->add_vlan(vsi, &vlan);
   3530	if (ret)
   3531		goto finish;
   3532
   3533	/* If all-multicast is currently enabled and this VLAN ID is only one
   3534	 * besides VLAN-0 we have to update look-up type of multicast promisc
   3535	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
   3536	 */
   3537	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
   3538	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
   3539		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
   3540					   ICE_MCAST_PROMISC_BITS, 0);
   3541		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
   3542					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
   3543	}
   3544
   3545finish:
   3546	clear_bit(ICE_CFG_BUSY, vsi->state);
   3547
   3548	return ret;
   3549}
   3550
   3551/**
   3552 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
   3553 * @netdev: network interface to be adjusted
   3554 * @proto: VLAN TPID
   3555 * @vid: VLAN ID to be removed
   3556 *
   3557 * net_device_ops implementation for removing VLAN IDs
   3558 */
   3559static int
   3560ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
   3561{
   3562	struct ice_netdev_priv *np = netdev_priv(netdev);
   3563	struct ice_vsi_vlan_ops *vlan_ops;
   3564	struct ice_vsi *vsi = np->vsi;
   3565	struct ice_vlan vlan;
   3566	int ret;
   3567
   3568	/* don't allow removal of VLAN 0 */
   3569	if (!vid)
   3570		return 0;
   3571
   3572	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
   3573		usleep_range(1000, 2000);
   3574
   3575	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
   3576
   3577	/* Make sure VLAN delete is successful before updating VLAN
   3578	 * information
   3579	 */
   3580	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
   3581	ret = vlan_ops->del_vlan(vsi, &vlan);
   3582	if (ret)
   3583		goto finish;
   3584
   3585	/* Remove multicast promisc rule for the removed VLAN ID if
   3586	 * all-multicast is enabled.
   3587	 */
   3588	if (vsi->current_netdev_flags & IFF_ALLMULTI)
   3589		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
   3590					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
   3591
   3592	if (!ice_vsi_has_non_zero_vlans(vsi)) {
   3593		/* Update look-up type of multicast promisc rule for VLAN 0
   3594		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
   3595		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
   3596		 */
   3597		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
   3598			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
   3599						   ICE_MCAST_VLAN_PROMISC_BITS,
   3600						   0);
   3601			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
   3602						 ICE_MCAST_PROMISC_BITS, 0);
   3603		}
   3604	}
   3605
   3606finish:
   3607	clear_bit(ICE_CFG_BUSY, vsi->state);
   3608
   3609	return ret;
   3610}
   3611
   3612/**
   3613 * ice_rep_indr_tc_block_unbind
   3614 * @cb_priv: indirection block private data
   3615 */
   3616static void ice_rep_indr_tc_block_unbind(void *cb_priv)
   3617{
   3618	struct ice_indr_block_priv *indr_priv = cb_priv;
   3619
   3620	list_del(&indr_priv->list);
   3621	kfree(indr_priv);
   3622}
   3623
   3624/**
   3625 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
   3626 * @vsi: VSI struct which has the netdev
   3627 */
   3628static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
   3629{
   3630	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
   3631
   3632	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
   3633				 ice_rep_indr_tc_block_unbind);
   3634}
   3635
   3636/**
   3637 * ice_tc_indir_block_remove - clean indirect TC block notifications
   3638 * @pf: PF structure
   3639 */
   3640static void ice_tc_indir_block_remove(struct ice_pf *pf)
   3641{
   3642	struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
   3643
   3644	if (!pf_vsi)
   3645		return;
   3646
   3647	ice_tc_indir_block_unregister(pf_vsi);
   3648}
   3649
   3650/**
   3651 * ice_tc_indir_block_register - Register TC indirect block notifications
   3652 * @vsi: VSI struct which has the netdev
   3653 *
   3654 * Returns 0 on success, negative value on failure
   3655 */
   3656static int ice_tc_indir_block_register(struct ice_vsi *vsi)
   3657{
   3658	struct ice_netdev_priv *np;
   3659
   3660	if (!vsi || !vsi->netdev)
   3661		return -EINVAL;
   3662
   3663	np = netdev_priv(vsi->netdev);
   3664
   3665	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
   3666	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
   3667}
   3668
   3669/**
   3670 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
   3671 * @pf: board private structure
   3672 *
   3673 * Returns 0 on success, negative value on failure
   3674 */
   3675static int ice_setup_pf_sw(struct ice_pf *pf)
   3676{
   3677	struct device *dev = ice_pf_to_dev(pf);
   3678	bool dvm = ice_is_dvm_ena(&pf->hw);
   3679	struct ice_vsi *vsi;
   3680	int status;
   3681
   3682	if (ice_is_reset_in_progress(pf->state))
   3683		return -EBUSY;
   3684
   3685	status = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
   3686	if (status)
   3687		return -EIO;
   3688
   3689	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
   3690	if (!vsi)
   3691		return -ENOMEM;
   3692
   3693	/* init channel list */
   3694	INIT_LIST_HEAD(&vsi->ch_list);
   3695
   3696	status = ice_cfg_netdev(vsi);
   3697	if (status)
   3698		goto unroll_vsi_setup;
   3699	/* netdev has to be configured before setting frame size */
   3700	ice_vsi_cfg_frame_size(vsi);
   3701
   3702	/* init indirect block notifications */
   3703	status = ice_tc_indir_block_register(vsi);
   3704	if (status) {
   3705		dev_err(dev, "Failed to register netdev notifier\n");
   3706		goto unroll_cfg_netdev;
   3707	}
   3708
   3709	/* Setup DCB netlink interface */
   3710	ice_dcbnl_setup(vsi);
   3711
   3712	/* registering the NAPI handler requires both the queues and
   3713	 * netdev to be created, which are done in ice_pf_vsi_setup()
   3714	 * and ice_cfg_netdev() respectively
   3715	 */
   3716	ice_napi_add(vsi);
   3717
   3718	status = ice_init_mac_fltr(pf);
   3719	if (status)
   3720		goto unroll_napi_add;
   3721
   3722	return 0;
   3723
   3724unroll_napi_add:
   3725	ice_tc_indir_block_unregister(vsi);
   3726unroll_cfg_netdev:
   3727	if (vsi) {
   3728		ice_napi_del(vsi);
   3729		if (vsi->netdev) {
   3730			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
   3731			free_netdev(vsi->netdev);
   3732			vsi->netdev = NULL;
   3733		}
   3734	}
   3735
   3736unroll_vsi_setup:
   3737	ice_vsi_release(vsi);
   3738	return status;
   3739}
   3740
   3741/**
   3742 * ice_get_avail_q_count - Get count of queues in use
   3743 * @pf_qmap: bitmap to get queue use count from
   3744 * @lock: pointer to a mutex that protects access to pf_qmap
   3745 * @size: size of the bitmap
   3746 */
   3747static u16
   3748ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
   3749{
   3750	unsigned long bit;
   3751	u16 count = 0;
   3752
   3753	mutex_lock(lock);
   3754	for_each_clear_bit(bit, pf_qmap, size)
   3755		count++;
   3756	mutex_unlock(lock);
   3757
   3758	return count;
   3759}
   3760
   3761/**
   3762 * ice_get_avail_txq_count - Get count of Tx queues in use
   3763 * @pf: pointer to an ice_pf instance
   3764 */
   3765u16 ice_get_avail_txq_count(struct ice_pf *pf)
   3766{
   3767	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
   3768				     pf->max_pf_txqs);
   3769}
   3770
   3771/**
   3772 * ice_get_avail_rxq_count - Get count of Rx queues in use
   3773 * @pf: pointer to an ice_pf instance
   3774 */
   3775u16 ice_get_avail_rxq_count(struct ice_pf *pf)
   3776{
   3777	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
   3778				     pf->max_pf_rxqs);
   3779}
   3780
   3781/**
   3782 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
   3783 * @pf: board private structure to initialize
   3784 */
   3785static void ice_deinit_pf(struct ice_pf *pf)
   3786{
   3787	ice_service_task_stop(pf);
   3788	mutex_destroy(&pf->adev_mutex);
   3789	mutex_destroy(&pf->sw_mutex);
   3790	mutex_destroy(&pf->tc_mutex);
   3791	mutex_destroy(&pf->avail_q_mutex);
   3792	mutex_destroy(&pf->vfs.table_lock);
   3793
   3794	if (pf->avail_txqs) {
   3795		bitmap_free(pf->avail_txqs);
   3796		pf->avail_txqs = NULL;
   3797	}
   3798
   3799	if (pf->avail_rxqs) {
   3800		bitmap_free(pf->avail_rxqs);
   3801		pf->avail_rxqs = NULL;
   3802	}
   3803
   3804	if (pf->ptp.clock)
   3805		ptp_clock_unregister(pf->ptp.clock);
   3806}
   3807
   3808/**
   3809 * ice_set_pf_caps - set PFs capability flags
   3810 * @pf: pointer to the PF instance
   3811 */
   3812static void ice_set_pf_caps(struct ice_pf *pf)
   3813{
   3814	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
   3815
   3816	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
   3817	if (func_caps->common_cap.rdma)
   3818		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
   3819	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
   3820	if (func_caps->common_cap.dcb)
   3821		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
   3822	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
   3823	if (func_caps->common_cap.sr_iov_1_1) {
   3824		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
   3825		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
   3826					      ICE_MAX_SRIOV_VFS);
   3827	}
   3828	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
   3829	if (func_caps->common_cap.rss_table_size)
   3830		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
   3831
   3832	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
   3833	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
   3834		u16 unused;
   3835
   3836		/* ctrl_vsi_idx will be set to a valid value when flow director
   3837		 * is setup by ice_init_fdir
   3838		 */
   3839		pf->ctrl_vsi_idx = ICE_NO_VSI;
   3840		set_bit(ICE_FLAG_FD_ENA, pf->flags);
   3841		/* force guaranteed filter pool for PF */
   3842		ice_alloc_fd_guar_item(&pf->hw, &unused,
   3843				       func_caps->fd_fltr_guar);
   3844		/* force shared filter pool for PF */
   3845		ice_alloc_fd_shrd_item(&pf->hw, &unused,
   3846				       func_caps->fd_fltr_best_effort);
   3847	}
   3848
   3849	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
   3850	if (func_caps->common_cap.ieee_1588)
   3851		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
   3852
   3853	pf->max_pf_txqs = func_caps->common_cap.num_txq;
   3854	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
   3855}
   3856
   3857/**
   3858 * ice_init_pf - Initialize general software structures (struct ice_pf)
   3859 * @pf: board private structure to initialize
   3860 */
   3861static int ice_init_pf(struct ice_pf *pf)
   3862{
   3863	ice_set_pf_caps(pf);
   3864
   3865	mutex_init(&pf->sw_mutex);
   3866	mutex_init(&pf->tc_mutex);
   3867	mutex_init(&pf->adev_mutex);
   3868
   3869	INIT_HLIST_HEAD(&pf->aq_wait_list);
   3870	spin_lock_init(&pf->aq_wait_lock);
   3871	init_waitqueue_head(&pf->aq_wait_queue);
   3872
   3873	init_waitqueue_head(&pf->reset_wait_queue);
   3874
   3875	/* setup service timer and periodic service task */
   3876	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
   3877	pf->serv_tmr_period = HZ;
   3878	INIT_WORK(&pf->serv_task, ice_service_task);
   3879	clear_bit(ICE_SERVICE_SCHED, pf->state);
   3880
   3881	mutex_init(&pf->avail_q_mutex);
   3882	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
   3883	if (!pf->avail_txqs)
   3884		return -ENOMEM;
   3885
   3886	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
   3887	if (!pf->avail_rxqs) {
   3888		devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
   3889		pf->avail_txqs = NULL;
   3890		return -ENOMEM;
   3891	}
   3892
   3893	mutex_init(&pf->vfs.table_lock);
   3894	hash_init(pf->vfs.table);
   3895
   3896	return 0;
   3897}
   3898
   3899/**
   3900 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
   3901 * @pf: board private structure
   3902 *
   3903 * compute the number of MSIX vectors required (v_budget) and request from
   3904 * the OS. Return the number of vectors reserved or negative on failure
   3905 */
   3906static int ice_ena_msix_range(struct ice_pf *pf)
   3907{
   3908	int num_cpus, v_left, v_actual, v_other, v_budget = 0;
   3909	struct device *dev = ice_pf_to_dev(pf);
   3910	int needed, err, i;
   3911
   3912	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
   3913	num_cpus = num_online_cpus();
   3914
   3915	/* reserve for LAN miscellaneous handler */
   3916	needed = ICE_MIN_LAN_OICR_MSIX;
   3917	if (v_left < needed)
   3918		goto no_hw_vecs_left_err;
   3919	v_budget += needed;
   3920	v_left -= needed;
   3921
   3922	/* reserve for flow director */
   3923	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
   3924		needed = ICE_FDIR_MSIX;
   3925		if (v_left < needed)
   3926			goto no_hw_vecs_left_err;
   3927		v_budget += needed;
   3928		v_left -= needed;
   3929	}
   3930
   3931	/* reserve for switchdev */
   3932	needed = ICE_ESWITCH_MSIX;
   3933	if (v_left < needed)
   3934		goto no_hw_vecs_left_err;
   3935	v_budget += needed;
   3936	v_left -= needed;
   3937
   3938	/* total used for non-traffic vectors */
   3939	v_other = v_budget;
   3940
   3941	/* reserve vectors for LAN traffic */
   3942	needed = num_cpus;
   3943	if (v_left < needed)
   3944		goto no_hw_vecs_left_err;
   3945	pf->num_lan_msix = needed;
   3946	v_budget += needed;
   3947	v_left -= needed;
   3948
   3949	/* reserve vectors for RDMA auxiliary driver */
   3950	if (ice_is_rdma_ena(pf)) {
   3951		needed = num_cpus + ICE_RDMA_NUM_AEQ_MSIX;
   3952		if (v_left < needed)
   3953			goto no_hw_vecs_left_err;
   3954		pf->num_rdma_msix = needed;
   3955		v_budget += needed;
   3956		v_left -= needed;
   3957	}
   3958
   3959	pf->msix_entries = devm_kcalloc(dev, v_budget,
   3960					sizeof(*pf->msix_entries), GFP_KERNEL);
   3961	if (!pf->msix_entries) {
   3962		err = -ENOMEM;
   3963		goto exit_err;
   3964	}
   3965
   3966	for (i = 0; i < v_budget; i++)
   3967		pf->msix_entries[i].entry = i;
   3968
   3969	/* actually reserve the vectors */
   3970	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
   3971					 ICE_MIN_MSIX, v_budget);
   3972	if (v_actual < 0) {
   3973		dev_err(dev, "unable to reserve MSI-X vectors\n");
   3974		err = v_actual;
   3975		goto msix_err;
   3976	}
   3977
   3978	if (v_actual < v_budget) {
   3979		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
   3980			 v_budget, v_actual);
   3981
   3982		if (v_actual < ICE_MIN_MSIX) {
   3983			/* error if we can't get minimum vectors */
   3984			pci_disable_msix(pf->pdev);
   3985			err = -ERANGE;
   3986			goto msix_err;
   3987		} else {
   3988			int v_remain = v_actual - v_other;
   3989			int v_rdma = 0, v_min_rdma = 0;
   3990
   3991			if (ice_is_rdma_ena(pf)) {
   3992				/* Need at least 1 interrupt in addition to
   3993				 * AEQ MSIX
   3994				 */
   3995				v_rdma = ICE_RDMA_NUM_AEQ_MSIX + 1;
   3996				v_min_rdma = ICE_MIN_RDMA_MSIX;
   3997			}
   3998
   3999			if (v_actual == ICE_MIN_MSIX ||
   4000			    v_remain < ICE_MIN_LAN_TXRX_MSIX + v_min_rdma) {
   4001				dev_warn(dev, "Not enough MSI-X vectors to support RDMA.\n");
   4002				clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
   4003
   4004				pf->num_rdma_msix = 0;
   4005				pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
   4006			} else if ((v_remain < ICE_MIN_LAN_TXRX_MSIX + v_rdma) ||
   4007				   (v_remain - v_rdma < v_rdma)) {
   4008				/* Support minimum RDMA and give remaining
   4009				 * vectors to LAN MSIX
   4010				 */
   4011				pf->num_rdma_msix = v_min_rdma;
   4012				pf->num_lan_msix = v_remain - v_min_rdma;
   4013			} else {
   4014				/* Split remaining MSIX with RDMA after
   4015				 * accounting for AEQ MSIX
   4016				 */
   4017				pf->num_rdma_msix = (v_remain - ICE_RDMA_NUM_AEQ_MSIX) / 2 +
   4018						    ICE_RDMA_NUM_AEQ_MSIX;
   4019				pf->num_lan_msix = v_remain - pf->num_rdma_msix;
   4020			}
   4021
   4022			dev_notice(dev, "Enabled %d MSI-X vectors for LAN traffic.\n",
   4023				   pf->num_lan_msix);
   4024
   4025			if (ice_is_rdma_ena(pf))
   4026				dev_notice(dev, "Enabled %d MSI-X vectors for RDMA.\n",
   4027					   pf->num_rdma_msix);
   4028		}
   4029	}
   4030
   4031	return v_actual;
   4032
   4033msix_err:
   4034	devm_kfree(dev, pf->msix_entries);
   4035	goto exit_err;
   4036
   4037no_hw_vecs_left_err:
   4038	dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
   4039		needed, v_left);
   4040	err = -ERANGE;
   4041exit_err:
   4042	pf->num_rdma_msix = 0;
   4043	pf->num_lan_msix = 0;
   4044	return err;
   4045}
   4046
   4047/**
   4048 * ice_dis_msix - Disable MSI-X interrupt setup in OS
   4049 * @pf: board private structure
   4050 */
   4051static void ice_dis_msix(struct ice_pf *pf)
   4052{
   4053	pci_disable_msix(pf->pdev);
   4054	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
   4055	pf->msix_entries = NULL;
   4056}
   4057
   4058/**
   4059 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
   4060 * @pf: board private structure
   4061 */
   4062static void ice_clear_interrupt_scheme(struct ice_pf *pf)
   4063{
   4064	ice_dis_msix(pf);
   4065
   4066	if (pf->irq_tracker) {
   4067		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
   4068		pf->irq_tracker = NULL;
   4069	}
   4070}
   4071
   4072/**
   4073 * ice_init_interrupt_scheme - Determine proper interrupt scheme
   4074 * @pf: board private structure to initialize
   4075 */
   4076static int ice_init_interrupt_scheme(struct ice_pf *pf)
   4077{
   4078	int vectors;
   4079
   4080	vectors = ice_ena_msix_range(pf);
   4081
   4082	if (vectors < 0)
   4083		return vectors;
   4084
   4085	/* set up vector assignment tracking */
   4086	pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
   4087				       struct_size(pf->irq_tracker, list, vectors),
   4088				       GFP_KERNEL);
   4089	if (!pf->irq_tracker) {
   4090		ice_dis_msix(pf);
   4091		return -ENOMEM;
   4092	}
   4093
   4094	/* populate SW interrupts pool with number of OS granted IRQs. */
   4095	pf->num_avail_sw_msix = (u16)vectors;
   4096	pf->irq_tracker->num_entries = (u16)vectors;
   4097	pf->irq_tracker->end = pf->irq_tracker->num_entries;
   4098
   4099	return 0;
   4100}
   4101
   4102/**
   4103 * ice_is_wol_supported - check if WoL is supported
   4104 * @hw: pointer to hardware info
   4105 *
   4106 * Check if WoL is supported based on the HW configuration.
   4107 * Returns true if NVM supports and enables WoL for this port, false otherwise
   4108 */
   4109bool ice_is_wol_supported(struct ice_hw *hw)
   4110{
   4111	u16 wol_ctrl;
   4112
   4113	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
   4114	 * word) indicates WoL is not supported on the corresponding PF ID.
   4115	 */
   4116	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
   4117		return false;
   4118
   4119	return !(BIT(hw->port_info->lport) & wol_ctrl);
   4120}
   4121
   4122/**
   4123 * ice_vsi_recfg_qs - Change the number of queues on a VSI
   4124 * @vsi: VSI being changed
   4125 * @new_rx: new number of Rx queues
   4126 * @new_tx: new number of Tx queues
   4127 *
   4128 * Only change the number of queues if new_tx, or new_rx is non-0.
   4129 *
   4130 * Returns 0 on success.
   4131 */
   4132int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
   4133{
   4134	struct ice_pf *pf = vsi->back;
   4135	int err = 0, timeout = 50;
   4136
   4137	if (!new_rx && !new_tx)
   4138		return -EINVAL;
   4139
   4140	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
   4141		timeout--;
   4142		if (!timeout)
   4143			return -EBUSY;
   4144		usleep_range(1000, 2000);
   4145	}
   4146
   4147	if (new_tx)
   4148		vsi->req_txq = (u16)new_tx;
   4149	if (new_rx)
   4150		vsi->req_rxq = (u16)new_rx;
   4151
   4152	/* set for the next time the netdev is started */
   4153	if (!netif_running(vsi->netdev)) {
   4154		ice_vsi_rebuild(vsi, false);
   4155		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
   4156		goto done;
   4157	}
   4158
   4159	ice_vsi_close(vsi);
   4160	ice_vsi_rebuild(vsi, false);
   4161	ice_pf_dcb_recfg(pf);
   4162	ice_vsi_open(vsi);
   4163done:
   4164	clear_bit(ICE_CFG_BUSY, pf->state);
   4165	return err;
   4166}
   4167
   4168/**
   4169 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
   4170 * @pf: PF to configure
   4171 *
   4172 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
   4173 * VSI can still Tx/Rx VLAN tagged packets.
   4174 */
   4175static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
   4176{
   4177	struct ice_vsi *vsi = ice_get_main_vsi(pf);
   4178	struct ice_vsi_ctx *ctxt;
   4179	struct ice_hw *hw;
   4180	int status;
   4181
   4182	if (!vsi)
   4183		return;
   4184
   4185	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
   4186	if (!ctxt)
   4187		return;
   4188
   4189	hw = &pf->hw;
   4190	ctxt->info = vsi->info;
   4191
   4192	ctxt->info.valid_sections =
   4193		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
   4194			    ICE_AQ_VSI_PROP_SECURITY_VALID |
   4195			    ICE_AQ_VSI_PROP_SW_VALID);
   4196
   4197	/* disable VLAN anti-spoof */
   4198	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
   4199				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
   4200
   4201	/* disable VLAN pruning and keep all other settings */
   4202	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
   4203
   4204	/* allow all VLANs on Tx and don't strip on Rx */
   4205	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
   4206		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
   4207
   4208	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
   4209	if (status) {
   4210		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
   4211			status, ice_aq_str(hw->adminq.sq_last_status));
   4212	} else {
   4213		vsi->info.sec_flags = ctxt->info.sec_flags;
   4214		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
   4215		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
   4216	}
   4217
   4218	kfree(ctxt);
   4219}
   4220
   4221/**
   4222 * ice_log_pkg_init - log result of DDP package load
   4223 * @hw: pointer to hardware info
   4224 * @state: state of package load
   4225 */
   4226static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
   4227{
   4228	struct ice_pf *pf = hw->back;
   4229	struct device *dev;
   4230
   4231	dev = ice_pf_to_dev(pf);
   4232
   4233	switch (state) {
   4234	case ICE_DDP_PKG_SUCCESS:
   4235		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
   4236			 hw->active_pkg_name,
   4237			 hw->active_pkg_ver.major,
   4238			 hw->active_pkg_ver.minor,
   4239			 hw->active_pkg_ver.update,
   4240			 hw->active_pkg_ver.draft);
   4241		break;
   4242	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
   4243		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
   4244			 hw->active_pkg_name,
   4245			 hw->active_pkg_ver.major,
   4246			 hw->active_pkg_ver.minor,
   4247			 hw->active_pkg_ver.update,
   4248			 hw->active_pkg_ver.draft);
   4249		break;
   4250	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
   4251		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",
   4252			hw->active_pkg_name,
   4253			hw->active_pkg_ver.major,
   4254			hw->active_pkg_ver.minor,
   4255			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
   4256		break;
   4257	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
   4258		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",
   4259			 hw->active_pkg_name,
   4260			 hw->active_pkg_ver.major,
   4261			 hw->active_pkg_ver.minor,
   4262			 hw->active_pkg_ver.update,
   4263			 hw->active_pkg_ver.draft,
   4264			 hw->pkg_name,
   4265			 hw->pkg_ver.major,
   4266			 hw->pkg_ver.minor,
   4267			 hw->pkg_ver.update,
   4268			 hw->pkg_ver.draft);
   4269		break;
   4270	case ICE_DDP_PKG_FW_MISMATCH:
   4271		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");
   4272		break;
   4273	case ICE_DDP_PKG_INVALID_FILE:
   4274		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
   4275		break;
   4276	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
   4277		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
   4278		break;
   4279	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
   4280		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",
   4281			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
   4282		break;
   4283	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
   4284		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");
   4285		break;
   4286	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
   4287		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");
   4288		break;
   4289	case ICE_DDP_PKG_LOAD_ERROR:
   4290		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
   4291		/* poll for reset to complete */
   4292		if (ice_check_reset(hw))
   4293			dev_err(dev, "Error resetting device. Please reload the driver\n");
   4294		break;
   4295	case ICE_DDP_PKG_ERR:
   4296	default:
   4297		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
   4298		break;
   4299	}
   4300}
   4301
   4302/**
   4303 * ice_load_pkg - load/reload the DDP Package file
   4304 * @firmware: firmware structure when firmware requested or NULL for reload
   4305 * @pf: pointer to the PF instance
   4306 *
   4307 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
   4308 * initialize HW tables.
   4309 */
   4310static void
   4311ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
   4312{
   4313	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
   4314	struct device *dev = ice_pf_to_dev(pf);
   4315	struct ice_hw *hw = &pf->hw;
   4316
   4317	/* Load DDP Package */
   4318	if (firmware && !hw->pkg_copy) {
   4319		state = ice_copy_and_init_pkg(hw, firmware->data,
   4320					      firmware->size);
   4321		ice_log_pkg_init(hw, state);
   4322	} else if (!firmware && hw->pkg_copy) {
   4323		/* Reload package during rebuild after CORER/GLOBR reset */
   4324		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
   4325		ice_log_pkg_init(hw, state);
   4326	} else {
   4327		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
   4328	}
   4329
   4330	if (!ice_is_init_pkg_successful(state)) {
   4331		/* Safe Mode */
   4332		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
   4333		return;
   4334	}
   4335
   4336	/* Successful download package is the precondition for advanced
   4337	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
   4338	 */
   4339	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
   4340}
   4341
   4342/**
   4343 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
   4344 * @pf: pointer to the PF structure
   4345 *
   4346 * There is no error returned here because the driver should be able to handle
   4347 * 128 Byte cache lines, so we only print a warning in case issues are seen,
   4348 * specifically with Tx.
   4349 */
   4350static void ice_verify_cacheline_size(struct ice_pf *pf)
   4351{
   4352	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
   4353		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
   4354			 ICE_CACHE_LINE_BYTES);
   4355}
   4356
   4357/**
   4358 * ice_send_version - update firmware with driver version
   4359 * @pf: PF struct
   4360 *
   4361 * Returns 0 on success, else error code
   4362 */
   4363static int ice_send_version(struct ice_pf *pf)
   4364{
   4365	struct ice_driver_ver dv;
   4366
   4367	dv.major_ver = 0xff;
   4368	dv.minor_ver = 0xff;
   4369	dv.build_ver = 0xff;
   4370	dv.subbuild_ver = 0;
   4371	strscpy((char *)dv.driver_string, UTS_RELEASE,
   4372		sizeof(dv.driver_string));
   4373	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
   4374}
   4375
   4376/**
   4377 * ice_init_fdir - Initialize flow director VSI and configuration
   4378 * @pf: pointer to the PF instance
   4379 *
   4380 * returns 0 on success, negative on error
   4381 */
   4382static int ice_init_fdir(struct ice_pf *pf)
   4383{
   4384	struct device *dev = ice_pf_to_dev(pf);
   4385	struct ice_vsi *ctrl_vsi;
   4386	int err;
   4387
   4388	/* Side Band Flow Director needs to have a control VSI.
   4389	 * Allocate it and store it in the PF.
   4390	 */
   4391	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
   4392	if (!ctrl_vsi) {
   4393		dev_dbg(dev, "could not create control VSI\n");
   4394		return -ENOMEM;
   4395	}
   4396
   4397	err = ice_vsi_open_ctrl(ctrl_vsi);
   4398	if (err) {
   4399		dev_dbg(dev, "could not open control VSI\n");
   4400		goto err_vsi_open;
   4401	}
   4402
   4403	mutex_init(&pf->hw.fdir_fltr_lock);
   4404
   4405	err = ice_fdir_create_dflt_rules(pf);
   4406	if (err)
   4407		goto err_fdir_rule;
   4408
   4409	return 0;
   4410
   4411err_fdir_rule:
   4412	ice_fdir_release_flows(&pf->hw);
   4413	ice_vsi_close(ctrl_vsi);
   4414err_vsi_open:
   4415	ice_vsi_release(ctrl_vsi);
   4416	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
   4417		pf->vsi[pf->ctrl_vsi_idx] = NULL;
   4418		pf->ctrl_vsi_idx = ICE_NO_VSI;
   4419	}
   4420	return err;
   4421}
   4422
   4423/**
   4424 * ice_get_opt_fw_name - return optional firmware file name or NULL
   4425 * @pf: pointer to the PF instance
   4426 */
   4427static char *ice_get_opt_fw_name(struct ice_pf *pf)
   4428{
   4429	/* Optional firmware name same as default with additional dash
   4430	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
   4431	 */
   4432	struct pci_dev *pdev = pf->pdev;
   4433	char *opt_fw_filename;
   4434	u64 dsn;
   4435
   4436	/* Determine the name of the optional file using the DSN (two
   4437	 * dwords following the start of the DSN Capability).
   4438	 */
   4439	dsn = pci_get_dsn(pdev);
   4440	if (!dsn)
   4441		return NULL;
   4442
   4443	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
   4444	if (!opt_fw_filename)
   4445		return NULL;
   4446
   4447	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
   4448		 ICE_DDP_PKG_PATH, dsn);
   4449
   4450	return opt_fw_filename;
   4451}
   4452
   4453/**
   4454 * ice_request_fw - Device initialization routine
   4455 * @pf: pointer to the PF instance
   4456 */
   4457static void ice_request_fw(struct ice_pf *pf)
   4458{
   4459	char *opt_fw_filename = ice_get_opt_fw_name(pf);
   4460	const struct firmware *firmware = NULL;
   4461	struct device *dev = ice_pf_to_dev(pf);
   4462	int err = 0;
   4463
   4464	/* optional device-specific DDP (if present) overrides the default DDP
   4465	 * package file. kernel logs a debug message if the file doesn't exist,
   4466	 * and warning messages for other errors.
   4467	 */
   4468	if (opt_fw_filename) {
   4469		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
   4470		if (err) {
   4471			kfree(opt_fw_filename);
   4472			goto dflt_pkg_load;
   4473		}
   4474
   4475		/* request for firmware was successful. Download to device */
   4476		ice_load_pkg(firmware, pf);
   4477		kfree(opt_fw_filename);
   4478		release_firmware(firmware);
   4479		return;
   4480	}
   4481
   4482dflt_pkg_load:
   4483	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
   4484	if (err) {
   4485		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
   4486		return;
   4487	}
   4488
   4489	/* request for firmware was successful. Download to device */
   4490	ice_load_pkg(firmware, pf);
   4491	release_firmware(firmware);
   4492}
   4493
   4494/**
   4495 * ice_print_wake_reason - show the wake up cause in the log
   4496 * @pf: pointer to the PF struct
   4497 */
   4498static void ice_print_wake_reason(struct ice_pf *pf)
   4499{
   4500	u32 wus = pf->wakeup_reason;
   4501	const char *wake_str;
   4502
   4503	/* if no wake event, nothing to print */
   4504	if (!wus)
   4505		return;
   4506
   4507	if (wus & PFPM_WUS_LNKC_M)
   4508		wake_str = "Link\n";
   4509	else if (wus & PFPM_WUS_MAG_M)
   4510		wake_str = "Magic Packet\n";
   4511	else if (wus & PFPM_WUS_MNG_M)
   4512		wake_str = "Management\n";
   4513	else if (wus & PFPM_WUS_FW_RST_WK_M)
   4514		wake_str = "Firmware Reset\n";
   4515	else
   4516		wake_str = "Unknown\n";
   4517
   4518	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
   4519}
   4520
   4521/**
   4522 * ice_register_netdev - register netdev and devlink port
   4523 * @pf: pointer to the PF struct
   4524 */
   4525static int ice_register_netdev(struct ice_pf *pf)
   4526{
   4527	struct ice_vsi *vsi;
   4528	int err = 0;
   4529
   4530	vsi = ice_get_main_vsi(pf);
   4531	if (!vsi || !vsi->netdev)
   4532		return -EIO;
   4533
   4534	err = register_netdev(vsi->netdev);
   4535	if (err)
   4536		goto err_register_netdev;
   4537
   4538	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
   4539	netif_carrier_off(vsi->netdev);
   4540	netif_tx_stop_all_queues(vsi->netdev);
   4541	err = ice_devlink_create_pf_port(pf);
   4542	if (err)
   4543		goto err_devlink_create;
   4544
   4545	devlink_port_type_eth_set(&pf->devlink_port, vsi->netdev);
   4546
   4547	return 0;
   4548err_devlink_create:
   4549	unregister_netdev(vsi->netdev);
   4550	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
   4551err_register_netdev:
   4552	free_netdev(vsi->netdev);
   4553	vsi->netdev = NULL;
   4554	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
   4555	return err;
   4556}
   4557
   4558/**
   4559 * ice_probe - Device initialization routine
   4560 * @pdev: PCI device information struct
   4561 * @ent: entry in ice_pci_tbl
   4562 *
   4563 * Returns 0 on success, negative on failure
   4564 */
   4565static int
   4566ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
   4567{
   4568	struct device *dev = &pdev->dev;
   4569	struct ice_pf *pf;
   4570	struct ice_hw *hw;
   4571	int i, err;
   4572
   4573	if (pdev->is_virtfn) {
   4574		dev_err(dev, "can't probe a virtual function\n");
   4575		return -EINVAL;
   4576	}
   4577
   4578	/* this driver uses devres, see
   4579	 * Documentation/driver-api/driver-model/devres.rst
   4580	 */
   4581	err = pcim_enable_device(pdev);
   4582	if (err)
   4583		return err;
   4584
   4585	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
   4586	if (err) {
   4587		dev_err(dev, "BAR0 I/O map error %d\n", err);
   4588		return err;
   4589	}
   4590
   4591	pf = ice_allocate_pf(dev);
   4592	if (!pf)
   4593		return -ENOMEM;
   4594
   4595	/* initialize Auxiliary index to invalid value */
   4596	pf->aux_idx = -1;
   4597
   4598	/* set up for high or low DMA */
   4599	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
   4600	if (err) {
   4601		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
   4602		return err;
   4603	}
   4604
   4605	pci_enable_pcie_error_reporting(pdev);
   4606	pci_set_master(pdev);
   4607
   4608	pf->pdev = pdev;
   4609	pci_set_drvdata(pdev, pf);
   4610	set_bit(ICE_DOWN, pf->state);
   4611	/* Disable service task until DOWN bit is cleared */
   4612	set_bit(ICE_SERVICE_DIS, pf->state);
   4613
   4614	hw = &pf->hw;
   4615	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
   4616	pci_save_state(pdev);
   4617
   4618	hw->back = pf;
   4619	hw->vendor_id = pdev->vendor;
   4620	hw->device_id = pdev->device;
   4621	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
   4622	hw->subsystem_vendor_id = pdev->subsystem_vendor;
   4623	hw->subsystem_device_id = pdev->subsystem_device;
   4624	hw->bus.device = PCI_SLOT(pdev->devfn);
   4625	hw->bus.func = PCI_FUNC(pdev->devfn);
   4626	ice_set_ctrlq_len(hw);
   4627
   4628	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
   4629
   4630#ifndef CONFIG_DYNAMIC_DEBUG
   4631	if (debug < -1)
   4632		hw->debug_mask = debug;
   4633#endif
   4634
   4635	err = ice_init_hw(hw);
   4636	if (err) {
   4637		dev_err(dev, "ice_init_hw failed: %d\n", err);
   4638		err = -EIO;
   4639		goto err_exit_unroll;
   4640	}
   4641
   4642	ice_init_feature_support(pf);
   4643
   4644	ice_request_fw(pf);
   4645
   4646	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
   4647	 * set in pf->state, which will cause ice_is_safe_mode to return
   4648	 * true
   4649	 */
   4650	if (ice_is_safe_mode(pf)) {
   4651		/* we already got function/device capabilities but these don't
   4652		 * reflect what the driver needs to do in safe mode. Instead of
   4653		 * adding conditional logic everywhere to ignore these
   4654		 * device/function capabilities, override them.
   4655		 */
   4656		ice_set_safe_mode_caps(hw);
   4657	}
   4658
   4659	err = ice_init_pf(pf);
   4660	if (err) {
   4661		dev_err(dev, "ice_init_pf failed: %d\n", err);
   4662		goto err_init_pf_unroll;
   4663	}
   4664
   4665	ice_devlink_init_regions(pf);
   4666
   4667	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
   4668	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
   4669	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
   4670	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
   4671	i = 0;
   4672	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
   4673		pf->hw.udp_tunnel_nic.tables[i].n_entries =
   4674			pf->hw.tnl.valid_count[TNL_VXLAN];
   4675		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
   4676			UDP_TUNNEL_TYPE_VXLAN;
   4677		i++;
   4678	}
   4679	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
   4680		pf->hw.udp_tunnel_nic.tables[i].n_entries =
   4681			pf->hw.tnl.valid_count[TNL_GENEVE];
   4682		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
   4683			UDP_TUNNEL_TYPE_GENEVE;
   4684		i++;
   4685	}
   4686
   4687	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
   4688	if (!pf->num_alloc_vsi) {
   4689		err = -EIO;
   4690		goto err_init_pf_unroll;
   4691	}
   4692	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
   4693		dev_warn(&pf->pdev->dev,
   4694			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
   4695			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
   4696		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
   4697	}
   4698
   4699	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
   4700			       GFP_KERNEL);
   4701	if (!pf->vsi) {
   4702		err = -ENOMEM;
   4703		goto err_init_pf_unroll;
   4704	}
   4705
   4706	err = ice_init_interrupt_scheme(pf);
   4707	if (err) {
   4708		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
   4709		err = -EIO;
   4710		goto err_init_vsi_unroll;
   4711	}
   4712
   4713	/* In case of MSIX we are going to setup the misc vector right here
   4714	 * to handle admin queue events etc. In case of legacy and MSI
   4715	 * the misc functionality and queue processing is combined in
   4716	 * the same vector and that gets setup at open.
   4717	 */
   4718	err = ice_req_irq_msix_misc(pf);
   4719	if (err) {
   4720		dev_err(dev, "setup of misc vector failed: %d\n", err);
   4721		goto err_init_interrupt_unroll;
   4722	}
   4723
   4724	/* create switch struct for the switch element created by FW on boot */
   4725	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
   4726	if (!pf->first_sw) {
   4727		err = -ENOMEM;
   4728		goto err_msix_misc_unroll;
   4729	}
   4730
   4731	if (hw->evb_veb)
   4732		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
   4733	else
   4734		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
   4735
   4736	pf->first_sw->pf = pf;
   4737
   4738	/* record the sw_id available for later use */
   4739	pf->first_sw->sw_id = hw->port_info->sw_id;
   4740
   4741	err = ice_setup_pf_sw(pf);
   4742	if (err) {
   4743		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
   4744		goto err_alloc_sw_unroll;
   4745	}
   4746
   4747	clear_bit(ICE_SERVICE_DIS, pf->state);
   4748
   4749	/* tell the firmware we are up */
   4750	err = ice_send_version(pf);
   4751	if (err) {
   4752		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
   4753			UTS_RELEASE, err);
   4754		goto err_send_version_unroll;
   4755	}
   4756
   4757	/* since everything is good, start the service timer */
   4758	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
   4759
   4760	err = ice_init_link_events(pf->hw.port_info);
   4761	if (err) {
   4762		dev_err(dev, "ice_init_link_events failed: %d\n", err);
   4763		goto err_send_version_unroll;
   4764	}
   4765
   4766	/* not a fatal error if this fails */
   4767	err = ice_init_nvm_phy_type(pf->hw.port_info);
   4768	if (err)
   4769		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
   4770
   4771	/* not a fatal error if this fails */
   4772	err = ice_update_link_info(pf->hw.port_info);
   4773	if (err)
   4774		dev_err(dev, "ice_update_link_info failed: %d\n", err);
   4775
   4776	ice_init_link_dflt_override(pf->hw.port_info);
   4777
   4778	ice_check_link_cfg_err(pf,
   4779			       pf->hw.port_info->phy.link_info.link_cfg_err);
   4780
   4781	/* if media available, initialize PHY settings */
   4782	if (pf->hw.port_info->phy.link_info.link_info &
   4783	    ICE_AQ_MEDIA_AVAILABLE) {
   4784		/* not a fatal error if this fails */
   4785		err = ice_init_phy_user_cfg(pf->hw.port_info);
   4786		if (err)
   4787			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
   4788
   4789		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
   4790			struct ice_vsi *vsi = ice_get_main_vsi(pf);
   4791
   4792			if (vsi)
   4793				ice_configure_phy(vsi);
   4794		}
   4795	} else {
   4796		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
   4797	}
   4798
   4799	ice_verify_cacheline_size(pf);
   4800
   4801	/* Save wakeup reason register for later use */
   4802	pf->wakeup_reason = rd32(hw, PFPM_WUS);
   4803
   4804	/* check for a power management event */
   4805	ice_print_wake_reason(pf);
   4806
   4807	/* clear wake status, all bits */
   4808	wr32(hw, PFPM_WUS, U32_MAX);
   4809
   4810	/* Disable WoL at init, wait for user to enable */
   4811	device_set_wakeup_enable(dev, false);
   4812
   4813	if (ice_is_safe_mode(pf)) {
   4814		ice_set_safe_mode_vlan_cfg(pf);
   4815		goto probe_done;
   4816	}
   4817
   4818	/* initialize DDP driven features */
   4819	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
   4820		ice_ptp_init(pf);
   4821
   4822	if (ice_is_feature_supported(pf, ICE_F_GNSS))
   4823		ice_gnss_init(pf);
   4824
   4825	/* Note: Flow director init failure is non-fatal to load */
   4826	if (ice_init_fdir(pf))
   4827		dev_err(dev, "could not initialize flow director\n");
   4828
   4829	/* Note: DCB init failure is non-fatal to load */
   4830	if (ice_init_pf_dcb(pf, false)) {
   4831		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
   4832		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
   4833	} else {
   4834		ice_cfg_lldp_mib_change(&pf->hw, true);
   4835	}
   4836
   4837	if (ice_init_lag(pf))
   4838		dev_warn(dev, "Failed to init link aggregation support\n");
   4839
   4840	/* print PCI link speed and width */
   4841	pcie_print_link_status(pf->pdev);
   4842
   4843probe_done:
   4844	err = ice_register_netdev(pf);
   4845	if (err)
   4846		goto err_netdev_reg;
   4847
   4848	err = ice_devlink_register_params(pf);
   4849	if (err)
   4850		goto err_netdev_reg;
   4851
   4852	/* ready to go, so clear down state bit */
   4853	clear_bit(ICE_DOWN, pf->state);
   4854	if (ice_is_rdma_ena(pf)) {
   4855		pf->aux_idx = ida_alloc(&ice_aux_ida, GFP_KERNEL);
   4856		if (pf->aux_idx < 0) {
   4857			dev_err(dev, "Failed to allocate device ID for AUX driver\n");
   4858			err = -ENOMEM;
   4859			goto err_devlink_reg_param;
   4860		}
   4861
   4862		err = ice_init_rdma(pf);
   4863		if (err) {
   4864			dev_err(dev, "Failed to initialize RDMA: %d\n", err);
   4865			err = -EIO;
   4866			goto err_init_aux_unroll;
   4867		}
   4868	} else {
   4869		dev_warn(dev, "RDMA is not supported on this device\n");
   4870	}
   4871
   4872	ice_devlink_register(pf);
   4873	return 0;
   4874
   4875err_init_aux_unroll:
   4876	pf->adev = NULL;
   4877	ida_free(&ice_aux_ida, pf->aux_idx);
   4878err_devlink_reg_param:
   4879	ice_devlink_unregister_params(pf);
   4880err_netdev_reg:
   4881err_send_version_unroll:
   4882	ice_vsi_release_all(pf);
   4883err_alloc_sw_unroll:
   4884	set_bit(ICE_SERVICE_DIS, pf->state);
   4885	set_bit(ICE_DOWN, pf->state);
   4886	devm_kfree(dev, pf->first_sw);
   4887err_msix_misc_unroll:
   4888	ice_free_irq_msix_misc(pf);
   4889err_init_interrupt_unroll:
   4890	ice_clear_interrupt_scheme(pf);
   4891err_init_vsi_unroll:
   4892	devm_kfree(dev, pf->vsi);
   4893err_init_pf_unroll:
   4894	ice_deinit_pf(pf);
   4895	ice_devlink_destroy_regions(pf);
   4896	ice_deinit_hw(hw);
   4897err_exit_unroll:
   4898	pci_disable_pcie_error_reporting(pdev);
   4899	pci_disable_device(pdev);
   4900	return err;
   4901}
   4902
   4903/**
   4904 * ice_set_wake - enable or disable Wake on LAN
   4905 * @pf: pointer to the PF struct
   4906 *
   4907 * Simple helper for WoL control
   4908 */
   4909static void ice_set_wake(struct ice_pf *pf)
   4910{
   4911	struct ice_hw *hw = &pf->hw;
   4912	bool wol = pf->wol_ena;
   4913
   4914	/* clear wake state, otherwise new wake events won't fire */
   4915	wr32(hw, PFPM_WUS, U32_MAX);
   4916
   4917	/* enable / disable APM wake up, no RMW needed */
   4918	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
   4919
   4920	/* set magic packet filter enabled */
   4921	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
   4922}
   4923
   4924/**
   4925 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
   4926 * @pf: pointer to the PF struct
   4927 *
   4928 * Issue firmware command to enable multicast magic wake, making
   4929 * sure that any locally administered address (LAA) is used for
   4930 * wake, and that PF reset doesn't undo the LAA.
   4931 */
   4932static void ice_setup_mc_magic_wake(struct ice_pf *pf)
   4933{
   4934	struct device *dev = ice_pf_to_dev(pf);
   4935	struct ice_hw *hw = &pf->hw;
   4936	u8 mac_addr[ETH_ALEN];
   4937	struct ice_vsi *vsi;
   4938	int status;
   4939	u8 flags;
   4940
   4941	if (!pf->wol_ena)
   4942		return;
   4943
   4944	vsi = ice_get_main_vsi(pf);
   4945	if (!vsi)
   4946		return;
   4947
   4948	/* Get current MAC address in case it's an LAA */
   4949	if (vsi->netdev)
   4950		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
   4951	else
   4952		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
   4953
   4954	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
   4955		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
   4956		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
   4957
   4958	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
   4959	if (status)
   4960		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
   4961			status, ice_aq_str(hw->adminq.sq_last_status));
   4962}
   4963
   4964/**
   4965 * ice_remove - Device removal routine
   4966 * @pdev: PCI device information struct
   4967 */
   4968static void ice_remove(struct pci_dev *pdev)
   4969{
   4970	struct ice_pf *pf = pci_get_drvdata(pdev);
   4971	int i;
   4972
   4973	ice_devlink_unregister(pf);
   4974	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
   4975		if (!ice_is_reset_in_progress(pf->state))
   4976			break;
   4977		msleep(100);
   4978	}
   4979
   4980	ice_tc_indir_block_remove(pf);
   4981
   4982	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
   4983		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
   4984		ice_free_vfs(pf);
   4985	}
   4986
   4987	ice_service_task_stop(pf);
   4988
   4989	ice_aq_cancel_waiting_tasks(pf);
   4990	ice_unplug_aux_dev(pf);
   4991	if (pf->aux_idx >= 0)
   4992		ida_free(&ice_aux_ida, pf->aux_idx);
   4993	ice_devlink_unregister_params(pf);
   4994	set_bit(ICE_DOWN, pf->state);
   4995
   4996	ice_deinit_lag(pf);
   4997	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
   4998		ice_ptp_release(pf);
   4999	if (ice_is_feature_supported(pf, ICE_F_GNSS))
   5000		ice_gnss_exit(pf);
   5001	if (!ice_is_safe_mode(pf))
   5002		ice_remove_arfs(pf);
   5003	ice_setup_mc_magic_wake(pf);
   5004	ice_vsi_release_all(pf);
   5005	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
   5006	ice_set_wake(pf);
   5007	ice_free_irq_msix_misc(pf);
   5008	ice_for_each_vsi(pf, i) {
   5009		if (!pf->vsi[i])
   5010			continue;
   5011		ice_vsi_free_q_vectors(pf->vsi[i]);
   5012	}
   5013	ice_deinit_pf(pf);
   5014	ice_devlink_destroy_regions(pf);
   5015	ice_deinit_hw(&pf->hw);
   5016
   5017	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
   5018	 * do it via ice_schedule_reset() since there is no need to rebuild
   5019	 * and the service task is already stopped.
   5020	 */
   5021	ice_reset(&pf->hw, ICE_RESET_PFR);
   5022	pci_wait_for_pending_transaction(pdev);
   5023	ice_clear_interrupt_scheme(pf);
   5024	pci_disable_pcie_error_reporting(pdev);
   5025	pci_disable_device(pdev);
   5026}
   5027
   5028/**
   5029 * ice_shutdown - PCI callback for shutting down device
   5030 * @pdev: PCI device information struct
   5031 */
   5032static void ice_shutdown(struct pci_dev *pdev)
   5033{
   5034	struct ice_pf *pf = pci_get_drvdata(pdev);
   5035
   5036	ice_remove(pdev);
   5037
   5038	if (system_state == SYSTEM_POWER_OFF) {
   5039		pci_wake_from_d3(pdev, pf->wol_ena);
   5040		pci_set_power_state(pdev, PCI_D3hot);
   5041	}
   5042}
   5043
   5044#ifdef CONFIG_PM
   5045/**
   5046 * ice_prepare_for_shutdown - prep for PCI shutdown
   5047 * @pf: board private structure
   5048 *
   5049 * Inform or close all dependent features in prep for PCI device shutdown
   5050 */
   5051static void ice_prepare_for_shutdown(struct ice_pf *pf)
   5052{
   5053	struct ice_hw *hw = &pf->hw;
   5054	u32 v;
   5055
   5056	/* Notify VFs of impending reset */
   5057	if (ice_check_sq_alive(hw, &hw->mailboxq))
   5058		ice_vc_notify_reset(pf);
   5059
   5060	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
   5061
   5062	/* disable the VSIs and their queues that are not already DOWN */
   5063	ice_pf_dis_all_vsi(pf, false);
   5064
   5065	ice_for_each_vsi(pf, v)
   5066		if (pf->vsi[v])
   5067			pf->vsi[v]->vsi_num = 0;
   5068
   5069	ice_shutdown_all_ctrlq(hw);
   5070}
   5071
   5072/**
   5073 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
   5074 * @pf: board private structure to reinitialize
   5075 *
   5076 * This routine reinitialize interrupt scheme that was cleared during
   5077 * power management suspend callback.
   5078 *
   5079 * This should be called during resume routine to re-allocate the q_vectors
   5080 * and reacquire interrupts.
   5081 */
   5082static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
   5083{
   5084	struct device *dev = ice_pf_to_dev(pf);
   5085	int ret, v;
   5086
   5087	/* Since we clear MSIX flag during suspend, we need to
   5088	 * set it back during resume...
   5089	 */
   5090
   5091	ret = ice_init_interrupt_scheme(pf);
   5092	if (ret) {
   5093		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
   5094		return ret;
   5095	}
   5096
   5097	/* Remap vectors and rings, after successful re-init interrupts */
   5098	ice_for_each_vsi(pf, v) {
   5099		if (!pf->vsi[v])
   5100			continue;
   5101
   5102		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
   5103		if (ret)
   5104			goto err_reinit;
   5105		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
   5106	}
   5107
   5108	ret = ice_req_irq_msix_misc(pf);
   5109	if (ret) {
   5110		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
   5111			ret);
   5112		goto err_reinit;
   5113	}
   5114
   5115	return 0;
   5116
   5117err_reinit:
   5118	while (v--)
   5119		if (pf->vsi[v])
   5120			ice_vsi_free_q_vectors(pf->vsi[v]);
   5121
   5122	return ret;
   5123}
   5124
   5125/**
   5126 * ice_suspend
   5127 * @dev: generic device information structure
   5128 *
   5129 * Power Management callback to quiesce the device and prepare
   5130 * for D3 transition.
   5131 */
   5132static int __maybe_unused ice_suspend(struct device *dev)
   5133{
   5134	struct pci_dev *pdev = to_pci_dev(dev);
   5135	struct ice_pf *pf;
   5136	int disabled, v;
   5137
   5138	pf = pci_get_drvdata(pdev);
   5139
   5140	if (!ice_pf_state_is_nominal(pf)) {
   5141		dev_err(dev, "Device is not ready, no need to suspend it\n");
   5142		return -EBUSY;
   5143	}
   5144
   5145	/* Stop watchdog tasks until resume completion.
   5146	 * Even though it is most likely that the service task is
   5147	 * disabled if the device is suspended or down, the service task's
   5148	 * state is controlled by a different state bit, and we should
   5149	 * store and honor whatever state that bit is in at this point.
   5150	 */
   5151	disabled = ice_service_task_stop(pf);
   5152
   5153	ice_unplug_aux_dev(pf);
   5154
   5155	/* Already suspended?, then there is nothing to do */
   5156	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
   5157		if (!disabled)
   5158			ice_service_task_restart(pf);
   5159		return 0;
   5160	}
   5161
   5162	if (test_bit(ICE_DOWN, pf->state) ||
   5163	    ice_is_reset_in_progress(pf->state)) {
   5164		dev_err(dev, "can't suspend device in reset or already down\n");
   5165		if (!disabled)
   5166			ice_service_task_restart(pf);
   5167		return 0;
   5168	}
   5169
   5170	ice_setup_mc_magic_wake(pf);
   5171
   5172	ice_prepare_for_shutdown(pf);
   5173
   5174	ice_set_wake(pf);
   5175
   5176	/* Free vectors, clear the interrupt scheme and release IRQs
   5177	 * for proper hibernation, especially with large number of CPUs.
   5178	 * Otherwise hibernation might fail when mapping all the vectors back
   5179	 * to CPU0.
   5180	 */
   5181	ice_free_irq_msix_misc(pf);
   5182	ice_for_each_vsi(pf, v) {
   5183		if (!pf->vsi[v])
   5184			continue;
   5185		ice_vsi_free_q_vectors(pf->vsi[v]);
   5186	}
   5187	ice_clear_interrupt_scheme(pf);
   5188
   5189	pci_save_state(pdev);
   5190	pci_wake_from_d3(pdev, pf->wol_ena);
   5191	pci_set_power_state(pdev, PCI_D3hot);
   5192	return 0;
   5193}
   5194
   5195/**
   5196 * ice_resume - PM callback for waking up from D3
   5197 * @dev: generic device information structure
   5198 */
   5199static int __maybe_unused ice_resume(struct device *dev)
   5200{
   5201	struct pci_dev *pdev = to_pci_dev(dev);
   5202	enum ice_reset_req reset_type;
   5203	struct ice_pf *pf;
   5204	struct ice_hw *hw;
   5205	int ret;
   5206
   5207	pci_set_power_state(pdev, PCI_D0);
   5208	pci_restore_state(pdev);
   5209	pci_save_state(pdev);
   5210
   5211	if (!pci_device_is_present(pdev))
   5212		return -ENODEV;
   5213
   5214	ret = pci_enable_device_mem(pdev);
   5215	if (ret) {
   5216		dev_err(dev, "Cannot enable device after suspend\n");
   5217		return ret;
   5218	}
   5219
   5220	pf = pci_get_drvdata(pdev);
   5221	hw = &pf->hw;
   5222
   5223	pf->wakeup_reason = rd32(hw, PFPM_WUS);
   5224	ice_print_wake_reason(pf);
   5225
   5226	/* We cleared the interrupt scheme when we suspended, so we need to
   5227	 * restore it now to resume device functionality.
   5228	 */
   5229	ret = ice_reinit_interrupt_scheme(pf);
   5230	if (ret)
   5231		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
   5232
   5233	clear_bit(ICE_DOWN, pf->state);
   5234	/* Now perform PF reset and rebuild */
   5235	reset_type = ICE_RESET_PFR;
   5236	/* re-enable service task for reset, but allow reset to schedule it */
   5237	clear_bit(ICE_SERVICE_DIS, pf->state);
   5238
   5239	if (ice_schedule_reset(pf, reset_type))
   5240		dev_err(dev, "Reset during resume failed.\n");
   5241
   5242	clear_bit(ICE_SUSPENDED, pf->state);
   5243	ice_service_task_restart(pf);
   5244
   5245	/* Restart the service task */
   5246	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
   5247
   5248	return 0;
   5249}
   5250#endif /* CONFIG_PM */
   5251
   5252/**
   5253 * ice_pci_err_detected - warning that PCI error has been detected
   5254 * @pdev: PCI device information struct
   5255 * @err: the type of PCI error
   5256 *
   5257 * Called to warn that something happened on the PCI bus and the error handling
   5258 * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
   5259 */
   5260static pci_ers_result_t
   5261ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
   5262{
   5263	struct ice_pf *pf = pci_get_drvdata(pdev);
   5264
   5265	if (!pf) {
   5266		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
   5267			__func__, err);
   5268		return PCI_ERS_RESULT_DISCONNECT;
   5269	}
   5270
   5271	if (!test_bit(ICE_SUSPENDED, pf->state)) {
   5272		ice_service_task_stop(pf);
   5273
   5274		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
   5275			set_bit(ICE_PFR_REQ, pf->state);
   5276			ice_prepare_for_reset(pf, ICE_RESET_PFR);
   5277		}
   5278	}
   5279
   5280	return PCI_ERS_RESULT_NEED_RESET;
   5281}
   5282
   5283/**
   5284 * ice_pci_err_slot_reset - a PCI slot reset has just happened
   5285 * @pdev: PCI device information struct
   5286 *
   5287 * Called to determine if the driver can recover from the PCI slot reset by
   5288 * using a register read to determine if the device is recoverable.
   5289 */
   5290static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
   5291{
   5292	struct ice_pf *pf = pci_get_drvdata(pdev);
   5293	pci_ers_result_t result;
   5294	int err;
   5295	u32 reg;
   5296
   5297	err = pci_enable_device_mem(pdev);
   5298	if (err) {
   5299		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
   5300			err);
   5301		result = PCI_ERS_RESULT_DISCONNECT;
   5302	} else {
   5303		pci_set_master(pdev);
   5304		pci_restore_state(pdev);
   5305		pci_save_state(pdev);
   5306		pci_wake_from_d3(pdev, false);
   5307
   5308		/* Check for life */
   5309		reg = rd32(&pf->hw, GLGEN_RTRIG);
   5310		if (!reg)
   5311			result = PCI_ERS_RESULT_RECOVERED;
   5312		else
   5313			result = PCI_ERS_RESULT_DISCONNECT;
   5314	}
   5315
   5316	err = pci_aer_clear_nonfatal_status(pdev);
   5317	if (err)
   5318		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
   5319			err);
   5320		/* non-fatal, continue */
   5321
   5322	return result;
   5323}
   5324
   5325/**
   5326 * ice_pci_err_resume - restart operations after PCI error recovery
   5327 * @pdev: PCI device information struct
   5328 *
   5329 * Called to allow the driver to bring things back up after PCI error and/or
   5330 * reset recovery have finished
   5331 */
   5332static void ice_pci_err_resume(struct pci_dev *pdev)
   5333{
   5334	struct ice_pf *pf = pci_get_drvdata(pdev);
   5335
   5336	if (!pf) {
   5337		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
   5338			__func__);
   5339		return;
   5340	}
   5341
   5342	if (test_bit(ICE_SUSPENDED, pf->state)) {
   5343		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
   5344			__func__);
   5345		return;
   5346	}
   5347
   5348	ice_restore_all_vfs_msi_state(pdev);
   5349
   5350	ice_do_reset(pf, ICE_RESET_PFR);
   5351	ice_service_task_restart(pf);
   5352	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
   5353}
   5354
   5355/**
   5356 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
   5357 * @pdev: PCI device information struct
   5358 */
   5359static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
   5360{
   5361	struct ice_pf *pf = pci_get_drvdata(pdev);
   5362
   5363	if (!test_bit(ICE_SUSPENDED, pf->state)) {
   5364		ice_service_task_stop(pf);
   5365
   5366		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
   5367			set_bit(ICE_PFR_REQ, pf->state);
   5368			ice_prepare_for_reset(pf, ICE_RESET_PFR);
   5369		}
   5370	}
   5371}
   5372
   5373/**
   5374 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
   5375 * @pdev: PCI device information struct
   5376 */
   5377static void ice_pci_err_reset_done(struct pci_dev *pdev)
   5378{
   5379	ice_pci_err_resume(pdev);
   5380}
   5381
   5382/* ice_pci_tbl - PCI Device ID Table
   5383 *
   5384 * Wildcard entries (PCI_ANY_ID) should come last
   5385 * Last entry must be all 0s
   5386 *
   5387 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
   5388 *   Class, Class Mask, private data (not used) }
   5389 */
   5390static const struct pci_device_id ice_pci_tbl[] = {
   5391	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
   5392	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
   5393	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
   5394	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
   5395	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
   5396	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
   5397	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
   5398	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
   5399	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
   5400	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
   5401	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
   5402	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
   5403	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
   5404	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
   5405	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
   5406	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
   5407	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
   5408	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
   5409	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
   5410	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
   5411	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
   5412	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
   5413	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
   5414	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
   5415	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
   5416	/* required last entry */
   5417	{ 0, }
   5418};
   5419MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
   5420
   5421static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
   5422
   5423static const struct pci_error_handlers ice_pci_err_handler = {
   5424	.error_detected = ice_pci_err_detected,
   5425	.slot_reset = ice_pci_err_slot_reset,
   5426	.reset_prepare = ice_pci_err_reset_prepare,
   5427	.reset_done = ice_pci_err_reset_done,
   5428	.resume = ice_pci_err_resume
   5429};
   5430
   5431static struct pci_driver ice_driver = {
   5432	.name = KBUILD_MODNAME,
   5433	.id_table = ice_pci_tbl,
   5434	.probe = ice_probe,
   5435	.remove = ice_remove,
   5436#ifdef CONFIG_PM
   5437	.driver.pm = &ice_pm_ops,
   5438#endif /* CONFIG_PM */
   5439	.shutdown = ice_shutdown,
   5440	.sriov_configure = ice_sriov_configure,
   5441	.err_handler = &ice_pci_err_handler
   5442};
   5443
   5444/**
   5445 * ice_module_init - Driver registration routine
   5446 *
   5447 * ice_module_init is the first routine called when the driver is
   5448 * loaded. All it does is register with the PCI subsystem.
   5449 */
   5450static int __init ice_module_init(void)
   5451{
   5452	int status;
   5453
   5454	pr_info("%s\n", ice_driver_string);
   5455	pr_info("%s\n", ice_copyright);
   5456
   5457	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
   5458	if (!ice_wq) {
   5459		pr_err("Failed to create workqueue\n");
   5460		return -ENOMEM;
   5461	}
   5462
   5463	status = pci_register_driver(&ice_driver);
   5464	if (status) {
   5465		pr_err("failed to register PCI driver, err %d\n", status);
   5466		destroy_workqueue(ice_wq);
   5467	}
   5468
   5469	return status;
   5470}
   5471module_init(ice_module_init);
   5472
   5473/**
   5474 * ice_module_exit - Driver exit cleanup routine
   5475 *
   5476 * ice_module_exit is called just before the driver is removed
   5477 * from memory.
   5478 */
   5479static void __exit ice_module_exit(void)
   5480{
   5481	pci_unregister_driver(&ice_driver);
   5482	destroy_workqueue(ice_wq);
   5483	pr_info("module unloaded\n");
   5484}
   5485module_exit(ice_module_exit);
   5486
   5487/**
   5488 * ice_set_mac_address - NDO callback to set MAC address
   5489 * @netdev: network interface device structure
   5490 * @pi: pointer to an address structure
   5491 *
   5492 * Returns 0 on success, negative on failure
   5493 */
   5494static int ice_set_mac_address(struct net_device *netdev, void *pi)
   5495{
   5496	struct ice_netdev_priv *np = netdev_priv(netdev);
   5497	struct ice_vsi *vsi = np->vsi;
   5498	struct ice_pf *pf = vsi->back;
   5499	struct ice_hw *hw = &pf->hw;
   5500	struct sockaddr *addr = pi;
   5501	u8 old_mac[ETH_ALEN];
   5502	u8 flags = 0;
   5503	u8 *mac;
   5504	int err;
   5505
   5506	mac = (u8 *)addr->sa_data;
   5507
   5508	if (!is_valid_ether_addr(mac))
   5509		return -EADDRNOTAVAIL;
   5510
   5511	if (ether_addr_equal(netdev->dev_addr, mac)) {
   5512		netdev_dbg(netdev, "already using mac %pM\n", mac);
   5513		return 0;
   5514	}
   5515
   5516	if (test_bit(ICE_DOWN, pf->state) ||
   5517	    ice_is_reset_in_progress(pf->state)) {
   5518		netdev_err(netdev, "can't set mac %pM. device not ready\n",
   5519			   mac);
   5520		return -EBUSY;
   5521	}
   5522
   5523	if (ice_chnl_dmac_fltr_cnt(pf)) {
   5524		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
   5525			   mac);
   5526		return -EAGAIN;
   5527	}
   5528
   5529	netif_addr_lock_bh(netdev);
   5530	ether_addr_copy(old_mac, netdev->dev_addr);
   5531	/* change the netdev's MAC address */
   5532	eth_hw_addr_set(netdev, mac);
   5533	netif_addr_unlock_bh(netdev);
   5534
   5535	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
   5536	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
   5537	if (err && err != -ENOENT) {
   5538		err = -EADDRNOTAVAIL;
   5539		goto err_update_filters;
   5540	}
   5541
   5542	/* Add filter for new MAC. If filter exists, return success */
   5543	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
   5544	if (err == -EEXIST) {
   5545		/* Although this MAC filter is already present in hardware it's
   5546		 * possible in some cases (e.g. bonding) that dev_addr was
   5547		 * modified outside of the driver and needs to be restored back
   5548		 * to this value.
   5549		 */
   5550		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
   5551
   5552		return 0;
   5553	} else if (err) {
   5554		/* error if the new filter addition failed */
   5555		err = -EADDRNOTAVAIL;
   5556	}
   5557
   5558err_update_filters:
   5559	if (err) {
   5560		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
   5561			   mac);
   5562		netif_addr_lock_bh(netdev);
   5563		eth_hw_addr_set(netdev, old_mac);
   5564		netif_addr_unlock_bh(netdev);
   5565		return err;
   5566	}
   5567
   5568	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
   5569		   netdev->dev_addr);
   5570
   5571	/* write new MAC address to the firmware */
   5572	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
   5573	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
   5574	if (err) {
   5575		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
   5576			   mac, err);
   5577	}
   5578	return 0;
   5579}
   5580
   5581/**
   5582 * ice_set_rx_mode - NDO callback to set the netdev filters
   5583 * @netdev: network interface device structure
   5584 */
   5585static void ice_set_rx_mode(struct net_device *netdev)
   5586{
   5587	struct ice_netdev_priv *np = netdev_priv(netdev);
   5588	struct ice_vsi *vsi = np->vsi;
   5589
   5590	if (!vsi)
   5591		return;
   5592
   5593	/* Set the flags to synchronize filters
   5594	 * ndo_set_rx_mode may be triggered even without a change in netdev
   5595	 * flags
   5596	 */
   5597	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
   5598	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
   5599	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
   5600
   5601	/* schedule our worker thread which will take care of
   5602	 * applying the new filter changes
   5603	 */
   5604	ice_service_task_schedule(vsi->back);
   5605}
   5606
   5607/**
   5608 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
   5609 * @netdev: network interface device structure
   5610 * @queue_index: Queue ID
   5611 * @maxrate: maximum bandwidth in Mbps
   5612 */
   5613static int
   5614ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
   5615{
   5616	struct ice_netdev_priv *np = netdev_priv(netdev);
   5617	struct ice_vsi *vsi = np->vsi;
   5618	u16 q_handle;
   5619	int status;
   5620	u8 tc;
   5621
   5622	/* Validate maxrate requested is within permitted range */
   5623	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
   5624		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
   5625			   maxrate, queue_index);
   5626		return -EINVAL;
   5627	}
   5628
   5629	q_handle = vsi->tx_rings[queue_index]->q_handle;
   5630	tc = ice_dcb_get_tc(vsi, queue_index);
   5631
   5632	/* Set BW back to default, when user set maxrate to 0 */
   5633	if (!maxrate)
   5634		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
   5635					       q_handle, ICE_MAX_BW);
   5636	else
   5637		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
   5638					  q_handle, ICE_MAX_BW, maxrate * 1000);
   5639	if (status)
   5640		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
   5641			   status);
   5642
   5643	return status;
   5644}
   5645
   5646/**
   5647 * ice_fdb_add - add an entry to the hardware database
   5648 * @ndm: the input from the stack
   5649 * @tb: pointer to array of nladdr (unused)
   5650 * @dev: the net device pointer
   5651 * @addr: the MAC address entry being added
   5652 * @vid: VLAN ID
   5653 * @flags: instructions from stack about fdb operation
   5654 * @extack: netlink extended ack
   5655 */
   5656static int
   5657ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
   5658	    struct net_device *dev, const unsigned char *addr, u16 vid,
   5659	    u16 flags, struct netlink_ext_ack __always_unused *extack)
   5660{
   5661	int err;
   5662
   5663	if (vid) {
   5664		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
   5665		return -EINVAL;
   5666	}
   5667	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
   5668		netdev_err(dev, "FDB only supports static addresses\n");
   5669		return -EINVAL;
   5670	}
   5671
   5672	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
   5673		err = dev_uc_add_excl(dev, addr);
   5674	else if (is_multicast_ether_addr(addr))
   5675		err = dev_mc_add_excl(dev, addr);
   5676	else
   5677		err = -EINVAL;
   5678
   5679	/* Only return duplicate errors if NLM_F_EXCL is set */
   5680	if (err == -EEXIST && !(flags & NLM_F_EXCL))
   5681		err = 0;
   5682
   5683	return err;
   5684}
   5685
   5686/**
   5687 * ice_fdb_del - delete an entry from the hardware database
   5688 * @ndm: the input from the stack
   5689 * @tb: pointer to array of nladdr (unused)
   5690 * @dev: the net device pointer
   5691 * @addr: the MAC address entry being added
   5692 * @vid: VLAN ID
   5693 * @extack: netlink extended ack
   5694 */
   5695static int
   5696ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
   5697	    struct net_device *dev, const unsigned char *addr,
   5698	    __always_unused u16 vid, struct netlink_ext_ack *extack)
   5699{
   5700	int err;
   5701
   5702	if (ndm->ndm_state & NUD_PERMANENT) {
   5703		netdev_err(dev, "FDB only supports static addresses\n");
   5704		return -EINVAL;
   5705	}
   5706
   5707	if (is_unicast_ether_addr(addr))
   5708		err = dev_uc_del(dev, addr);
   5709	else if (is_multicast_ether_addr(addr))
   5710		err = dev_mc_del(dev, addr);
   5711	else
   5712		err = -EINVAL;
   5713
   5714	return err;
   5715}
   5716
   5717#define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
   5718					 NETIF_F_HW_VLAN_CTAG_TX | \
   5719					 NETIF_F_HW_VLAN_STAG_RX | \
   5720					 NETIF_F_HW_VLAN_STAG_TX)
   5721
   5722#define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
   5723					 NETIF_F_HW_VLAN_STAG_FILTER)
   5724
   5725/**
   5726 * ice_fix_features - fix the netdev features flags based on device limitations
   5727 * @netdev: ptr to the netdev that flags are being fixed on
   5728 * @features: features that need to be checked and possibly fixed
   5729 *
   5730 * Make sure any fixups are made to features in this callback. This enables the
   5731 * driver to not have to check unsupported configurations throughout the driver
   5732 * because that's the responsiblity of this callback.
   5733 *
   5734 * Single VLAN Mode (SVM) Supported Features:
   5735 *	NETIF_F_HW_VLAN_CTAG_FILTER
   5736 *	NETIF_F_HW_VLAN_CTAG_RX
   5737 *	NETIF_F_HW_VLAN_CTAG_TX
   5738 *
   5739 * Double VLAN Mode (DVM) Supported Features:
   5740 *	NETIF_F_HW_VLAN_CTAG_FILTER
   5741 *	NETIF_F_HW_VLAN_CTAG_RX
   5742 *	NETIF_F_HW_VLAN_CTAG_TX
   5743 *
   5744 *	NETIF_F_HW_VLAN_STAG_FILTER
   5745 *	NETIF_HW_VLAN_STAG_RX
   5746 *	NETIF_HW_VLAN_STAG_TX
   5747 *
   5748 * Features that need fixing:
   5749 *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
   5750 *	These are mutually exlusive as the VSI context cannot support multiple
   5751 *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
   5752 *	is not done, then default to clearing the requested STAG offload
   5753 *	settings.
   5754 *
   5755 *	All supported filtering has to be enabled or disabled together. For
   5756 *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
   5757 *	together. If this is not done, then default to VLAN filtering disabled.
   5758 *	These are mutually exclusive as there is currently no way to
   5759 *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
   5760 *	prune rules.
   5761 */
   5762static netdev_features_t
   5763ice_fix_features(struct net_device *netdev, netdev_features_t features)
   5764{
   5765	struct ice_netdev_priv *np = netdev_priv(netdev);
   5766	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
   5767	bool cur_ctag, cur_stag, req_ctag, req_stag;
   5768
   5769	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
   5770	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
   5771	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
   5772
   5773	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
   5774	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
   5775	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
   5776
   5777	if (req_vlan_fltr != cur_vlan_fltr) {
   5778		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
   5779			if (req_ctag && req_stag) {
   5780				features |= NETIF_VLAN_FILTERING_FEATURES;
   5781			} else if (!req_ctag && !req_stag) {
   5782				features &= ~NETIF_VLAN_FILTERING_FEATURES;
   5783			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
   5784				   (!cur_stag && req_stag && !cur_ctag)) {
   5785				features |= NETIF_VLAN_FILTERING_FEATURES;
   5786				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
   5787			} else if ((cur_ctag && !req_ctag && cur_stag) ||
   5788				   (cur_stag && !req_stag && cur_ctag)) {
   5789				features &= ~NETIF_VLAN_FILTERING_FEATURES;
   5790				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
   5791			}
   5792		} else {
   5793			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
   5794				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
   5795
   5796			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
   5797				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
   5798		}
   5799	}
   5800
   5801	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
   5802	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
   5803		netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
   5804		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
   5805			      NETIF_F_HW_VLAN_STAG_TX);
   5806	}
   5807
   5808	return features;
   5809}
   5810
   5811/**
   5812 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
   5813 * @vsi: PF's VSI
   5814 * @features: features used to determine VLAN offload settings
   5815 *
   5816 * First, determine the vlan_ethertype based on the VLAN offload bits in
   5817 * features. Then determine if stripping and insertion should be enabled or
   5818 * disabled. Finally enable or disable VLAN stripping and insertion.
   5819 */
   5820static int
   5821ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
   5822{
   5823	bool enable_stripping = true, enable_insertion = true;
   5824	struct ice_vsi_vlan_ops *vlan_ops;
   5825	int strip_err = 0, insert_err = 0;
   5826	u16 vlan_ethertype = 0;
   5827
   5828	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
   5829
   5830	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
   5831		vlan_ethertype = ETH_P_8021AD;
   5832	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
   5833		vlan_ethertype = ETH_P_8021Q;
   5834
   5835	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
   5836		enable_stripping = false;
   5837	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
   5838		enable_insertion = false;
   5839
   5840	if (enable_stripping)
   5841		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
   5842	else
   5843		strip_err = vlan_ops->dis_stripping(vsi);
   5844
   5845	if (enable_insertion)
   5846		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
   5847	else
   5848		insert_err = vlan_ops->dis_insertion(vsi);
   5849
   5850	if (strip_err || insert_err)
   5851		return -EIO;
   5852
   5853	return 0;
   5854}
   5855
   5856/**
   5857 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
   5858 * @vsi: PF's VSI
   5859 * @features: features used to determine VLAN filtering settings
   5860 *
   5861 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
   5862 * features.
   5863 */
   5864static int
   5865ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
   5866{
   5867	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
   5868	int err = 0;
   5869
   5870	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
   5871	 * if either bit is set
   5872	 */
   5873	if (features &
   5874	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
   5875		err = vlan_ops->ena_rx_filtering(vsi);
   5876	else
   5877		err = vlan_ops->dis_rx_filtering(vsi);
   5878
   5879	return err;
   5880}
   5881
   5882/**
   5883 * ice_set_vlan_features - set VLAN settings based on suggested feature set
   5884 * @netdev: ptr to the netdev being adjusted
   5885 * @features: the feature set that the stack is suggesting
   5886 *
   5887 * Only update VLAN settings if the requested_vlan_features are different than
   5888 * the current_vlan_features.
   5889 */
   5890static int
   5891ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
   5892{
   5893	netdev_features_t current_vlan_features, requested_vlan_features;
   5894	struct ice_netdev_priv *np = netdev_priv(netdev);
   5895	struct ice_vsi *vsi = np->vsi;
   5896	int err;
   5897
   5898	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
   5899	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
   5900	if (current_vlan_features ^ requested_vlan_features) {
   5901		err = ice_set_vlan_offload_features(vsi, features);
   5902		if (err)
   5903			return err;
   5904	}
   5905
   5906	current_vlan_features = netdev->features &
   5907		NETIF_VLAN_FILTERING_FEATURES;
   5908	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
   5909	if (current_vlan_features ^ requested_vlan_features) {
   5910		err = ice_set_vlan_filtering_features(vsi, features);
   5911		if (err)
   5912			return err;
   5913	}
   5914
   5915	return 0;
   5916}
   5917
   5918/**
   5919 * ice_set_features - set the netdev feature flags
   5920 * @netdev: ptr to the netdev being adjusted
   5921 * @features: the feature set that the stack is suggesting
   5922 */
   5923static int
   5924ice_set_features(struct net_device *netdev, netdev_features_t features)
   5925{
   5926	struct ice_netdev_priv *np = netdev_priv(netdev);
   5927	struct ice_vsi *vsi = np->vsi;
   5928	struct ice_pf *pf = vsi->back;
   5929	int ret = 0;
   5930
   5931	/* Don't set any netdev advanced features with device in Safe Mode */
   5932	if (ice_is_safe_mode(vsi->back)) {
   5933		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
   5934		return ret;
   5935	}
   5936
   5937	/* Do not change setting during reset */
   5938	if (ice_is_reset_in_progress(pf->state)) {
   5939		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
   5940		return -EBUSY;
   5941	}
   5942
   5943	/* Multiple features can be changed in one call so keep features in
   5944	 * separate if/else statements to guarantee each feature is checked
   5945	 */
   5946	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
   5947		ice_vsi_manage_rss_lut(vsi, true);
   5948	else if (!(features & NETIF_F_RXHASH) &&
   5949		 netdev->features & NETIF_F_RXHASH)
   5950		ice_vsi_manage_rss_lut(vsi, false);
   5951
   5952	ret = ice_set_vlan_features(netdev, features);
   5953	if (ret)
   5954		return ret;
   5955
   5956	if ((features & NETIF_F_NTUPLE) &&
   5957	    !(netdev->features & NETIF_F_NTUPLE)) {
   5958		ice_vsi_manage_fdir(vsi, true);
   5959		ice_init_arfs(vsi);
   5960	} else if (!(features & NETIF_F_NTUPLE) &&
   5961		 (netdev->features & NETIF_F_NTUPLE)) {
   5962		ice_vsi_manage_fdir(vsi, false);
   5963		ice_clear_arfs(vsi);
   5964	}
   5965
   5966	/* don't turn off hw_tc_offload when ADQ is already enabled */
   5967	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
   5968		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
   5969		return -EACCES;
   5970	}
   5971
   5972	if ((features & NETIF_F_HW_TC) &&
   5973	    !(netdev->features & NETIF_F_HW_TC))
   5974		set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
   5975	else
   5976		clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
   5977
   5978	return 0;
   5979}
   5980
   5981/**
   5982 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
   5983 * @vsi: VSI to setup VLAN properties for
   5984 */
   5985static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
   5986{
   5987	int err;
   5988
   5989	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
   5990	if (err)
   5991		return err;
   5992
   5993	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
   5994	if (err)
   5995		return err;
   5996
   5997	return ice_vsi_add_vlan_zero(vsi);
   5998}
   5999
   6000/**
   6001 * ice_vsi_cfg - Setup the VSI
   6002 * @vsi: the VSI being configured
   6003 *
   6004 * Return 0 on success and negative value on error
   6005 */
   6006int ice_vsi_cfg(struct ice_vsi *vsi)
   6007{
   6008	int err;
   6009
   6010	if (vsi->netdev) {
   6011		ice_set_rx_mode(vsi->netdev);
   6012
   6013		err = ice_vsi_vlan_setup(vsi);
   6014
   6015		if (err)
   6016			return err;
   6017	}
   6018	ice_vsi_cfg_dcb_rings(vsi);
   6019
   6020	err = ice_vsi_cfg_lan_txqs(vsi);
   6021	if (!err && ice_is_xdp_ena_vsi(vsi))
   6022		err = ice_vsi_cfg_xdp_txqs(vsi);
   6023	if (!err)
   6024		err = ice_vsi_cfg_rxqs(vsi);
   6025
   6026	return err;
   6027}
   6028
   6029/* THEORY OF MODERATION:
   6030 * The ice driver hardware works differently than the hardware that DIMLIB was
   6031 * originally made for. ice hardware doesn't have packet count limits that
   6032 * can trigger an interrupt, but it *does* have interrupt rate limit support,
   6033 * which is hard-coded to a limit of 250,000 ints/second.
   6034 * If not using dynamic moderation, the INTRL value can be modified
   6035 * by ethtool rx-usecs-high.
   6036 */
   6037struct ice_dim {
   6038	/* the throttle rate for interrupts, basically worst case delay before
   6039	 * an initial interrupt fires, value is stored in microseconds.
   6040	 */
   6041	u16 itr;
   6042};
   6043
   6044/* Make a different profile for Rx that doesn't allow quite so aggressive
   6045 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
   6046 * second.
   6047 */
   6048static const struct ice_dim rx_profile[] = {
   6049	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
   6050	{8},    /* 125,000 ints/s */
   6051	{16},   /*  62,500 ints/s */
   6052	{62},   /*  16,129 ints/s */
   6053	{126}   /*   7,936 ints/s */
   6054};
   6055
   6056/* The transmit profile, which has the same sorts of values
   6057 * as the previous struct
   6058 */
   6059static const struct ice_dim tx_profile[] = {
   6060	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
   6061	{8},    /* 125,000 ints/s */
   6062	{40},   /*  16,125 ints/s */
   6063	{128},  /*   7,812 ints/s */
   6064	{256}   /*   3,906 ints/s */
   6065};
   6066
   6067static void ice_tx_dim_work(struct work_struct *work)
   6068{
   6069	struct ice_ring_container *rc;
   6070	struct dim *dim;
   6071	u16 itr;
   6072
   6073	dim = container_of(work, struct dim, work);
   6074	rc = (struct ice_ring_container *)dim->priv;
   6075
   6076	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
   6077
   6078	/* look up the values in our local table */
   6079	itr = tx_profile[dim->profile_ix].itr;
   6080
   6081	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
   6082	ice_write_itr(rc, itr);
   6083
   6084	dim->state = DIM_START_MEASURE;
   6085}
   6086
   6087static void ice_rx_dim_work(struct work_struct *work)
   6088{
   6089	struct ice_ring_container *rc;
   6090	struct dim *dim;
   6091	u16 itr;
   6092
   6093	dim = container_of(work, struct dim, work);
   6094	rc = (struct ice_ring_container *)dim->priv;
   6095
   6096	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
   6097
   6098	/* look up the values in our local table */
   6099	itr = rx_profile[dim->profile_ix].itr;
   6100
   6101	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
   6102	ice_write_itr(rc, itr);
   6103
   6104	dim->state = DIM_START_MEASURE;
   6105}
   6106
   6107#define ICE_DIM_DEFAULT_PROFILE_IX 1
   6108
   6109/**
   6110 * ice_init_moderation - set up interrupt moderation
   6111 * @q_vector: the vector containing rings to be configured
   6112 *
   6113 * Set up interrupt moderation registers, with the intent to do the right thing
   6114 * when called from reset or from probe, and whether or not dynamic moderation
   6115 * is enabled or not. Take special care to write all the registers in both
   6116 * dynamic moderation mode or not in order to make sure hardware is in a known
   6117 * state.
   6118 */
   6119static void ice_init_moderation(struct ice_q_vector *q_vector)
   6120{
   6121	struct ice_ring_container *rc;
   6122	bool tx_dynamic, rx_dynamic;
   6123
   6124	rc = &q_vector->tx;
   6125	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
   6126	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
   6127	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
   6128	rc->dim.priv = rc;
   6129	tx_dynamic = ITR_IS_DYNAMIC(rc);
   6130
   6131	/* set the initial TX ITR to match the above */
   6132	ice_write_itr(rc, tx_dynamic ?
   6133		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
   6134
   6135	rc = &q_vector->rx;
   6136	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
   6137	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
   6138	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
   6139	rc->dim.priv = rc;
   6140	rx_dynamic = ITR_IS_DYNAMIC(rc);
   6141
   6142	/* set the initial RX ITR to match the above */
   6143	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
   6144				       rc->itr_setting);
   6145
   6146	ice_set_q_vector_intrl(q_vector);
   6147}
   6148
   6149/**
   6150 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
   6151 * @vsi: the VSI being configured
   6152 */
   6153static void ice_napi_enable_all(struct ice_vsi *vsi)
   6154{
   6155	int q_idx;
   6156
   6157	if (!vsi->netdev)
   6158		return;
   6159
   6160	ice_for_each_q_vector(vsi, q_idx) {
   6161		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
   6162
   6163		ice_init_moderation(q_vector);
   6164
   6165		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
   6166			napi_enable(&q_vector->napi);
   6167	}
   6168}
   6169
   6170/**
   6171 * ice_up_complete - Finish the last steps of bringing up a connection
   6172 * @vsi: The VSI being configured
   6173 *
   6174 * Return 0 on success and negative value on error
   6175 */
   6176static int ice_up_complete(struct ice_vsi *vsi)
   6177{
   6178	struct ice_pf *pf = vsi->back;
   6179	int err;
   6180
   6181	ice_vsi_cfg_msix(vsi);
   6182
   6183	/* Enable only Rx rings, Tx rings were enabled by the FW when the
   6184	 * Tx queue group list was configured and the context bits were
   6185	 * programmed using ice_vsi_cfg_txqs
   6186	 */
   6187	err = ice_vsi_start_all_rx_rings(vsi);
   6188	if (err)
   6189		return err;
   6190
   6191	clear_bit(ICE_VSI_DOWN, vsi->state);
   6192	ice_napi_enable_all(vsi);
   6193	ice_vsi_ena_irq(vsi);
   6194
   6195	if (vsi->port_info &&
   6196	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
   6197	    vsi->netdev) {
   6198		ice_print_link_msg(vsi, true);
   6199		netif_tx_start_all_queues(vsi->netdev);
   6200		netif_carrier_on(vsi->netdev);
   6201		if (!ice_is_e810(&pf->hw))
   6202			ice_ptp_link_change(pf, pf->hw.pf_id, true);
   6203	}
   6204
   6205	/* Perform an initial read of the statistics registers now to
   6206	 * set the baseline so counters are ready when interface is up
   6207	 */
   6208	ice_update_eth_stats(vsi);
   6209	ice_service_task_schedule(pf);
   6210
   6211	return 0;
   6212}
   6213
   6214/**
   6215 * ice_up - Bring the connection back up after being down
   6216 * @vsi: VSI being configured
   6217 */
   6218int ice_up(struct ice_vsi *vsi)
   6219{
   6220	int err;
   6221
   6222	err = ice_vsi_cfg(vsi);
   6223	if (!err)
   6224		err = ice_up_complete(vsi);
   6225
   6226	return err;
   6227}
   6228
   6229/**
   6230 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
   6231 * @syncp: pointer to u64_stats_sync
   6232 * @stats: stats that pkts and bytes count will be taken from
   6233 * @pkts: packets stats counter
   6234 * @bytes: bytes stats counter
   6235 *
   6236 * This function fetches stats from the ring considering the atomic operations
   6237 * that needs to be performed to read u64 values in 32 bit machine.
   6238 */
   6239void
   6240ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
   6241			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
   6242{
   6243	unsigned int start;
   6244
   6245	do {
   6246		start = u64_stats_fetch_begin_irq(syncp);
   6247		*pkts = stats.pkts;
   6248		*bytes = stats.bytes;
   6249	} while (u64_stats_fetch_retry_irq(syncp, start));
   6250}
   6251
   6252/**
   6253 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
   6254 * @vsi: the VSI to be updated
   6255 * @vsi_stats: the stats struct to be updated
   6256 * @rings: rings to work on
   6257 * @count: number of rings
   6258 */
   6259static void
   6260ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
   6261			     struct rtnl_link_stats64 *vsi_stats,
   6262			     struct ice_tx_ring **rings, u16 count)
   6263{
   6264	u16 i;
   6265
   6266	for (i = 0; i < count; i++) {
   6267		struct ice_tx_ring *ring;
   6268		u64 pkts = 0, bytes = 0;
   6269
   6270		ring = READ_ONCE(rings[i]);
   6271		if (!ring)
   6272			continue;
   6273		ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
   6274		vsi_stats->tx_packets += pkts;
   6275		vsi_stats->tx_bytes += bytes;
   6276		vsi->tx_restart += ring->tx_stats.restart_q;
   6277		vsi->tx_busy += ring->tx_stats.tx_busy;
   6278		vsi->tx_linearize += ring->tx_stats.tx_linearize;
   6279	}
   6280}
   6281
   6282/**
   6283 * ice_update_vsi_ring_stats - Update VSI stats counters
   6284 * @vsi: the VSI to be updated
   6285 */
   6286static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
   6287{
   6288	struct rtnl_link_stats64 *vsi_stats;
   6289	u64 pkts, bytes;
   6290	int i;
   6291
   6292	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
   6293	if (!vsi_stats)
   6294		return;
   6295
   6296	/* reset non-netdev (extended) stats */
   6297	vsi->tx_restart = 0;
   6298	vsi->tx_busy = 0;
   6299	vsi->tx_linearize = 0;
   6300	vsi->rx_buf_failed = 0;
   6301	vsi->rx_page_failed = 0;
   6302
   6303	rcu_read_lock();
   6304
   6305	/* update Tx rings counters */
   6306	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
   6307				     vsi->num_txq);
   6308
   6309	/* update Rx rings counters */
   6310	ice_for_each_rxq(vsi, i) {
   6311		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
   6312
   6313		ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
   6314		vsi_stats->rx_packets += pkts;
   6315		vsi_stats->rx_bytes += bytes;
   6316		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
   6317		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
   6318	}
   6319
   6320	/* update XDP Tx rings counters */
   6321	if (ice_is_xdp_ena_vsi(vsi))
   6322		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
   6323					     vsi->num_xdp_txq);
   6324
   6325	rcu_read_unlock();
   6326
   6327	vsi->net_stats.tx_packets = vsi_stats->tx_packets;
   6328	vsi->net_stats.tx_bytes = vsi_stats->tx_bytes;
   6329	vsi->net_stats.rx_packets = vsi_stats->rx_packets;
   6330	vsi->net_stats.rx_bytes = vsi_stats->rx_bytes;
   6331
   6332	kfree(vsi_stats);
   6333}
   6334
   6335/**
   6336 * ice_update_vsi_stats - Update VSI stats counters
   6337 * @vsi: the VSI to be updated
   6338 */
   6339void ice_update_vsi_stats(struct ice_vsi *vsi)
   6340{
   6341	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
   6342	struct ice_eth_stats *cur_es = &vsi->eth_stats;
   6343	struct ice_pf *pf = vsi->back;
   6344
   6345	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
   6346	    test_bit(ICE_CFG_BUSY, pf->state))
   6347		return;
   6348
   6349	/* get stats as recorded by Tx/Rx rings */
   6350	ice_update_vsi_ring_stats(vsi);
   6351
   6352	/* get VSI stats as recorded by the hardware */
   6353	ice_update_eth_stats(vsi);
   6354
   6355	cur_ns->tx_errors = cur_es->tx_errors;
   6356	cur_ns->rx_dropped = cur_es->rx_discards;
   6357	cur_ns->tx_dropped = cur_es->tx_discards;
   6358	cur_ns->multicast = cur_es->rx_multicast;
   6359
   6360	/* update some more netdev stats if this is main VSI */
   6361	if (vsi->type == ICE_VSI_PF) {
   6362		cur_ns->rx_crc_errors = pf->stats.crc_errors;
   6363		cur_ns->rx_errors = pf->stats.crc_errors +
   6364				    pf->stats.illegal_bytes +
   6365				    pf->stats.rx_len_errors +
   6366				    pf->stats.rx_undersize +
   6367				    pf->hw_csum_rx_error +
   6368				    pf->stats.rx_jabber +
   6369				    pf->stats.rx_fragments +
   6370				    pf->stats.rx_oversize;
   6371		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
   6372		/* record drops from the port level */
   6373		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
   6374	}
   6375}
   6376
   6377/**
   6378 * ice_update_pf_stats - Update PF port stats counters
   6379 * @pf: PF whose stats needs to be updated
   6380 */
   6381void ice_update_pf_stats(struct ice_pf *pf)
   6382{
   6383	struct ice_hw_port_stats *prev_ps, *cur_ps;
   6384	struct ice_hw *hw = &pf->hw;
   6385	u16 fd_ctr_base;
   6386	u8 port;
   6387
   6388	port = hw->port_info->lport;
   6389	prev_ps = &pf->stats_prev;
   6390	cur_ps = &pf->stats;
   6391
   6392	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
   6393			  &prev_ps->eth.rx_bytes,
   6394			  &cur_ps->eth.rx_bytes);
   6395
   6396	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
   6397			  &prev_ps->eth.rx_unicast,
   6398			  &cur_ps->eth.rx_unicast);
   6399
   6400	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
   6401			  &prev_ps->eth.rx_multicast,
   6402			  &cur_ps->eth.rx_multicast);
   6403
   6404	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
   6405			  &prev_ps->eth.rx_broadcast,
   6406			  &cur_ps->eth.rx_broadcast);
   6407
   6408	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
   6409			  &prev_ps->eth.rx_discards,
   6410			  &cur_ps->eth.rx_discards);
   6411
   6412	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
   6413			  &prev_ps->eth.tx_bytes,
   6414			  &cur_ps->eth.tx_bytes);
   6415
   6416	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
   6417			  &prev_ps->eth.tx_unicast,
   6418			  &cur_ps->eth.tx_unicast);
   6419
   6420	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
   6421			  &prev_ps->eth.tx_multicast,
   6422			  &cur_ps->eth.tx_multicast);
   6423
   6424	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
   6425			  &prev_ps->eth.tx_broadcast,
   6426			  &cur_ps->eth.tx_broadcast);
   6427
   6428	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
   6429			  &prev_ps->tx_dropped_link_down,
   6430			  &cur_ps->tx_dropped_link_down);
   6431
   6432	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
   6433			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
   6434
   6435	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
   6436			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
   6437
   6438	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
   6439			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
   6440
   6441	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
   6442			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
   6443
   6444	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
   6445			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
   6446
   6447	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
   6448			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
   6449
   6450	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
   6451			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
   6452
   6453	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
   6454			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
   6455
   6456	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
   6457			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
   6458
   6459	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
   6460			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
   6461
   6462	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
   6463			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
   6464
   6465	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
   6466			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
   6467
   6468	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
   6469			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
   6470
   6471	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
   6472			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
   6473
   6474	fd_ctr_base = hw->fd_ctr_base;
   6475
   6476	ice_stat_update40(hw,
   6477			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
   6478			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
   6479			  &cur_ps->fd_sb_match);
   6480	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
   6481			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
   6482
   6483	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
   6484			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
   6485
   6486	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
   6487			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
   6488
   6489	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
   6490			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
   6491
   6492	ice_update_dcb_stats(pf);
   6493
   6494	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
   6495			  &prev_ps->crc_errors, &cur_ps->crc_errors);
   6496
   6497	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
   6498			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
   6499
   6500	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
   6501			  &prev_ps->mac_local_faults,
   6502			  &cur_ps->mac_local_faults);
   6503
   6504	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
   6505			  &prev_ps->mac_remote_faults,
   6506			  &cur_ps->mac_remote_faults);
   6507
   6508	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
   6509			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
   6510
   6511	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
   6512			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
   6513
   6514	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
   6515			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
   6516
   6517	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
   6518			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
   6519
   6520	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
   6521			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
   6522
   6523	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
   6524
   6525	pf->stat_prev_loaded = true;
   6526}
   6527
   6528/**
   6529 * ice_get_stats64 - get statistics for network device structure
   6530 * @netdev: network interface device structure
   6531 * @stats: main device statistics structure
   6532 */
   6533static
   6534void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
   6535{
   6536	struct ice_netdev_priv *np = netdev_priv(netdev);
   6537	struct rtnl_link_stats64 *vsi_stats;
   6538	struct ice_vsi *vsi = np->vsi;
   6539
   6540	vsi_stats = &vsi->net_stats;
   6541
   6542	if (!vsi->num_txq || !vsi->num_rxq)
   6543		return;
   6544
   6545	/* netdev packet/byte stats come from ring counter. These are obtained
   6546	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
   6547	 * But, only call the update routine and read the registers if VSI is
   6548	 * not down.
   6549	 */
   6550	if (!test_bit(ICE_VSI_DOWN, vsi->state))
   6551		ice_update_vsi_ring_stats(vsi);
   6552	stats->tx_packets = vsi_stats->tx_packets;
   6553	stats->tx_bytes = vsi_stats->tx_bytes;
   6554	stats->rx_packets = vsi_stats->rx_packets;
   6555	stats->rx_bytes = vsi_stats->rx_bytes;
   6556
   6557	/* The rest of the stats can be read from the hardware but instead we
   6558	 * just return values that the watchdog task has already obtained from
   6559	 * the hardware.
   6560	 */
   6561	stats->multicast = vsi_stats->multicast;
   6562	stats->tx_errors = vsi_stats->tx_errors;
   6563	stats->tx_dropped = vsi_stats->tx_dropped;
   6564	stats->rx_errors = vsi_stats->rx_errors;
   6565	stats->rx_dropped = vsi_stats->rx_dropped;
   6566	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
   6567	stats->rx_length_errors = vsi_stats->rx_length_errors;
   6568}
   6569
   6570/**
   6571 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
   6572 * @vsi: VSI having NAPI disabled
   6573 */
   6574static void ice_napi_disable_all(struct ice_vsi *vsi)
   6575{
   6576	int q_idx;
   6577
   6578	if (!vsi->netdev)
   6579		return;
   6580
   6581	ice_for_each_q_vector(vsi, q_idx) {
   6582		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
   6583
   6584		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
   6585			napi_disable(&q_vector->napi);
   6586
   6587		cancel_work_sync(&q_vector->tx.dim.work);
   6588		cancel_work_sync(&q_vector->rx.dim.work);
   6589	}
   6590}
   6591
   6592/**
   6593 * ice_down - Shutdown the connection
   6594 * @vsi: The VSI being stopped
   6595 *
   6596 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
   6597 */
   6598int ice_down(struct ice_vsi *vsi)
   6599{
   6600	int i, tx_err, rx_err, link_err = 0, vlan_err = 0;
   6601
   6602	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
   6603
   6604	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
   6605		vlan_err = ice_vsi_del_vlan_zero(vsi);
   6606		if (!ice_is_e810(&vsi->back->hw))
   6607			ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
   6608		netif_carrier_off(vsi->netdev);
   6609		netif_tx_disable(vsi->netdev);
   6610	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
   6611		ice_eswitch_stop_all_tx_queues(vsi->back);
   6612	}
   6613
   6614	ice_vsi_dis_irq(vsi);
   6615
   6616	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
   6617	if (tx_err)
   6618		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
   6619			   vsi->vsi_num, tx_err);
   6620	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
   6621		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
   6622		if (tx_err)
   6623			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
   6624				   vsi->vsi_num, tx_err);
   6625	}
   6626
   6627	rx_err = ice_vsi_stop_all_rx_rings(vsi);
   6628	if (rx_err)
   6629		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
   6630			   vsi->vsi_num, rx_err);
   6631
   6632	ice_napi_disable_all(vsi);
   6633
   6634	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
   6635		link_err = ice_force_phys_link_state(vsi, false);
   6636		if (link_err)
   6637			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
   6638				   vsi->vsi_num, link_err);
   6639	}
   6640
   6641	ice_for_each_txq(vsi, i)
   6642		ice_clean_tx_ring(vsi->tx_rings[i]);
   6643
   6644	ice_for_each_rxq(vsi, i)
   6645		ice_clean_rx_ring(vsi->rx_rings[i]);
   6646
   6647	if (tx_err || rx_err || link_err || vlan_err) {
   6648		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
   6649			   vsi->vsi_num, vsi->vsw->sw_id);
   6650		return -EIO;
   6651	}
   6652
   6653	return 0;
   6654}
   6655
   6656/**
   6657 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
   6658 * @vsi: VSI having resources allocated
   6659 *
   6660 * Return 0 on success, negative on failure
   6661 */
   6662int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
   6663{
   6664	int i, err = 0;
   6665
   6666	if (!vsi->num_txq) {
   6667		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
   6668			vsi->vsi_num);
   6669		return -EINVAL;
   6670	}
   6671
   6672	ice_for_each_txq(vsi, i) {
   6673		struct ice_tx_ring *ring = vsi->tx_rings[i];
   6674
   6675		if (!ring)
   6676			return -EINVAL;
   6677
   6678		if (vsi->netdev)
   6679			ring->netdev = vsi->netdev;
   6680		err = ice_setup_tx_ring(ring);
   6681		if (err)
   6682			break;
   6683	}
   6684
   6685	return err;
   6686}
   6687
   6688/**
   6689 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
   6690 * @vsi: VSI having resources allocated
   6691 *
   6692 * Return 0 on success, negative on failure
   6693 */
   6694int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
   6695{
   6696	int i, err = 0;
   6697
   6698	if (!vsi->num_rxq) {
   6699		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
   6700			vsi->vsi_num);
   6701		return -EINVAL;
   6702	}
   6703
   6704	ice_for_each_rxq(vsi, i) {
   6705		struct ice_rx_ring *ring = vsi->rx_rings[i];
   6706
   6707		if (!ring)
   6708			return -EINVAL;
   6709
   6710		if (vsi->netdev)
   6711			ring->netdev = vsi->netdev;
   6712		err = ice_setup_rx_ring(ring);
   6713		if (err)
   6714			break;
   6715	}
   6716
   6717	return err;
   6718}
   6719
   6720/**
   6721 * ice_vsi_open_ctrl - open control VSI for use
   6722 * @vsi: the VSI to open
   6723 *
   6724 * Initialization of the Control VSI
   6725 *
   6726 * Returns 0 on success, negative value on error
   6727 */
   6728int ice_vsi_open_ctrl(struct ice_vsi *vsi)
   6729{
   6730	char int_name[ICE_INT_NAME_STR_LEN];
   6731	struct ice_pf *pf = vsi->back;
   6732	struct device *dev;
   6733	int err;
   6734
   6735	dev = ice_pf_to_dev(pf);
   6736	/* allocate descriptors */
   6737	err = ice_vsi_setup_tx_rings(vsi);
   6738	if (err)
   6739		goto err_setup_tx;
   6740
   6741	err = ice_vsi_setup_rx_rings(vsi);
   6742	if (err)
   6743		goto err_setup_rx;
   6744
   6745	err = ice_vsi_cfg(vsi);
   6746	if (err)
   6747		goto err_setup_rx;
   6748
   6749	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
   6750		 dev_driver_string(dev), dev_name(dev));
   6751	err = ice_vsi_req_irq_msix(vsi, int_name);
   6752	if (err)
   6753		goto err_setup_rx;
   6754
   6755	ice_vsi_cfg_msix(vsi);
   6756
   6757	err = ice_vsi_start_all_rx_rings(vsi);
   6758	if (err)
   6759		goto err_up_complete;
   6760
   6761	clear_bit(ICE_VSI_DOWN, vsi->state);
   6762	ice_vsi_ena_irq(vsi);
   6763
   6764	return 0;
   6765
   6766err_up_complete:
   6767	ice_down(vsi);
   6768err_setup_rx:
   6769	ice_vsi_free_rx_rings(vsi);
   6770err_setup_tx:
   6771	ice_vsi_free_tx_rings(vsi);
   6772
   6773	return err;
   6774}
   6775
   6776/**
   6777 * ice_vsi_open - Called when a network interface is made active
   6778 * @vsi: the VSI to open
   6779 *
   6780 * Initialization of the VSI
   6781 *
   6782 * Returns 0 on success, negative value on error
   6783 */
   6784int ice_vsi_open(struct ice_vsi *vsi)
   6785{
   6786	char int_name[ICE_INT_NAME_STR_LEN];
   6787	struct ice_pf *pf = vsi->back;
   6788	int err;
   6789
   6790	/* allocate descriptors */
   6791	err = ice_vsi_setup_tx_rings(vsi);
   6792	if (err)
   6793		goto err_setup_tx;
   6794
   6795	err = ice_vsi_setup_rx_rings(vsi);
   6796	if (err)
   6797		goto err_setup_rx;
   6798
   6799	err = ice_vsi_cfg(vsi);
   6800	if (err)
   6801		goto err_setup_rx;
   6802
   6803	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
   6804		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
   6805	err = ice_vsi_req_irq_msix(vsi, int_name);
   6806	if (err)
   6807		goto err_setup_rx;
   6808
   6809	if (vsi->type == ICE_VSI_PF) {
   6810		/* Notify the stack of the actual queue counts. */
   6811		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
   6812		if (err)
   6813			goto err_set_qs;
   6814
   6815		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
   6816		if (err)
   6817			goto err_set_qs;
   6818	}
   6819
   6820	err = ice_up_complete(vsi);
   6821	if (err)
   6822		goto err_up_complete;
   6823
   6824	return 0;
   6825
   6826err_up_complete:
   6827	ice_down(vsi);
   6828err_set_qs:
   6829	ice_vsi_free_irq(vsi);
   6830err_setup_rx:
   6831	ice_vsi_free_rx_rings(vsi);
   6832err_setup_tx:
   6833	ice_vsi_free_tx_rings(vsi);
   6834
   6835	return err;
   6836}
   6837
   6838/**
   6839 * ice_vsi_release_all - Delete all VSIs
   6840 * @pf: PF from which all VSIs are being removed
   6841 */
   6842static void ice_vsi_release_all(struct ice_pf *pf)
   6843{
   6844	int err, i;
   6845
   6846	if (!pf->vsi)
   6847		return;
   6848
   6849	ice_for_each_vsi(pf, i) {
   6850		if (!pf->vsi[i])
   6851			continue;
   6852
   6853		if (pf->vsi[i]->type == ICE_VSI_CHNL)
   6854			continue;
   6855
   6856		err = ice_vsi_release(pf->vsi[i]);
   6857		if (err)
   6858			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
   6859				i, err, pf->vsi[i]->vsi_num);
   6860	}
   6861}
   6862
   6863/**
   6864 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
   6865 * @pf: pointer to the PF instance
   6866 * @type: VSI type to rebuild
   6867 *
   6868 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
   6869 */
   6870static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
   6871{
   6872	struct device *dev = ice_pf_to_dev(pf);
   6873	int i, err;
   6874
   6875	ice_for_each_vsi(pf, i) {
   6876		struct ice_vsi *vsi = pf->vsi[i];
   6877
   6878		if (!vsi || vsi->type != type)
   6879			continue;
   6880
   6881		/* rebuild the VSI */
   6882		err = ice_vsi_rebuild(vsi, true);
   6883		if (err) {
   6884			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
   6885				err, vsi->idx, ice_vsi_type_str(type));
   6886			return err;
   6887		}
   6888
   6889		/* replay filters for the VSI */
   6890		err = ice_replay_vsi(&pf->hw, vsi->idx);
   6891		if (err) {
   6892			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
   6893				err, vsi->idx, ice_vsi_type_str(type));
   6894			return err;
   6895		}
   6896
   6897		/* Re-map HW VSI number, using VSI handle that has been
   6898		 * previously validated in ice_replay_vsi() call above
   6899		 */
   6900		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
   6901
   6902		/* enable the VSI */
   6903		err = ice_ena_vsi(vsi, false);
   6904		if (err) {
   6905			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
   6906				err, vsi->idx, ice_vsi_type_str(type));
   6907			return err;
   6908		}
   6909
   6910		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
   6911			 ice_vsi_type_str(type));
   6912	}
   6913
   6914	return 0;
   6915}
   6916
   6917/**
   6918 * ice_update_pf_netdev_link - Update PF netdev link status
   6919 * @pf: pointer to the PF instance
   6920 */
   6921static void ice_update_pf_netdev_link(struct ice_pf *pf)
   6922{
   6923	bool link_up;
   6924	int i;
   6925
   6926	ice_for_each_vsi(pf, i) {
   6927		struct ice_vsi *vsi = pf->vsi[i];
   6928
   6929		if (!vsi || vsi->type != ICE_VSI_PF)
   6930			return;
   6931
   6932		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
   6933		if (link_up) {
   6934			netif_carrier_on(pf->vsi[i]->netdev);
   6935			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
   6936		} else {
   6937			netif_carrier_off(pf->vsi[i]->netdev);
   6938			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
   6939		}
   6940	}
   6941}
   6942
   6943/**
   6944 * ice_rebuild - rebuild after reset
   6945 * @pf: PF to rebuild
   6946 * @reset_type: type of reset
   6947 *
   6948 * Do not rebuild VF VSI in this flow because that is already handled via
   6949 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
   6950 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
   6951 * to reset/rebuild all the VF VSI twice.
   6952 */
   6953static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
   6954{
   6955	struct device *dev = ice_pf_to_dev(pf);
   6956	struct ice_hw *hw = &pf->hw;
   6957	bool dvm;
   6958	int err;
   6959
   6960	if (test_bit(ICE_DOWN, pf->state))
   6961		goto clear_recovery;
   6962
   6963	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
   6964
   6965#define ICE_EMP_RESET_SLEEP_MS 5000
   6966	if (reset_type == ICE_RESET_EMPR) {
   6967		/* If an EMP reset has occurred, any previously pending flash
   6968		 * update will have completed. We no longer know whether or
   6969		 * not the NVM update EMP reset is restricted.
   6970		 */
   6971		pf->fw_emp_reset_disabled = false;
   6972
   6973		msleep(ICE_EMP_RESET_SLEEP_MS);
   6974	}
   6975
   6976	err = ice_init_all_ctrlq(hw);
   6977	if (err) {
   6978		dev_err(dev, "control queues init failed %d\n", err);
   6979		goto err_init_ctrlq;
   6980	}
   6981
   6982	/* if DDP was previously loaded successfully */
   6983	if (!ice_is_safe_mode(pf)) {
   6984		/* reload the SW DB of filter tables */
   6985		if (reset_type == ICE_RESET_PFR)
   6986			ice_fill_blk_tbls(hw);
   6987		else
   6988			/* Reload DDP Package after CORER/GLOBR reset */
   6989			ice_load_pkg(NULL, pf);
   6990	}
   6991
   6992	err = ice_clear_pf_cfg(hw);
   6993	if (err) {
   6994		dev_err(dev, "clear PF configuration failed %d\n", err);
   6995		goto err_init_ctrlq;
   6996	}
   6997
   6998	if (pf->first_sw->dflt_vsi_ena)
   6999		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
   7000	/* clear the default VSI configuration if it exists */
   7001	pf->first_sw->dflt_vsi = NULL;
   7002	pf->first_sw->dflt_vsi_ena = false;
   7003
   7004	ice_clear_pxe_mode(hw);
   7005
   7006	err = ice_init_nvm(hw);
   7007	if (err) {
   7008		dev_err(dev, "ice_init_nvm failed %d\n", err);
   7009		goto err_init_ctrlq;
   7010	}
   7011
   7012	err = ice_get_caps(hw);
   7013	if (err) {
   7014		dev_err(dev, "ice_get_caps failed %d\n", err);
   7015		goto err_init_ctrlq;
   7016	}
   7017
   7018	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
   7019	if (err) {
   7020		dev_err(dev, "set_mac_cfg failed %d\n", err);
   7021		goto err_init_ctrlq;
   7022	}
   7023
   7024	dvm = ice_is_dvm_ena(hw);
   7025
   7026	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
   7027	if (err)
   7028		goto err_init_ctrlq;
   7029
   7030	err = ice_sched_init_port(hw->port_info);
   7031	if (err)
   7032		goto err_sched_init_port;
   7033
   7034	/* start misc vector */
   7035	err = ice_req_irq_msix_misc(pf);
   7036	if (err) {
   7037		dev_err(dev, "misc vector setup failed: %d\n", err);
   7038		goto err_sched_init_port;
   7039	}
   7040
   7041	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
   7042		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
   7043		if (!rd32(hw, PFQF_FD_SIZE)) {
   7044			u16 unused, guar, b_effort;
   7045
   7046			guar = hw->func_caps.fd_fltr_guar;
   7047			b_effort = hw->func_caps.fd_fltr_best_effort;
   7048
   7049			/* force guaranteed filter pool for PF */
   7050			ice_alloc_fd_guar_item(hw, &unused, guar);
   7051			/* force shared filter pool for PF */
   7052			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
   7053		}
   7054	}
   7055
   7056	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
   7057		ice_dcb_rebuild(pf);
   7058
   7059	/* If the PF previously had enabled PTP, PTP init needs to happen before
   7060	 * the VSI rebuild. If not, this causes the PTP link status events to
   7061	 * fail.
   7062	 */
   7063	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
   7064		ice_ptp_reset(pf);
   7065
   7066	if (ice_is_feature_supported(pf, ICE_F_GNSS))
   7067		ice_gnss_init(pf);
   7068
   7069	/* rebuild PF VSI */
   7070	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
   7071	if (err) {
   7072		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
   7073		goto err_vsi_rebuild;
   7074	}
   7075
   7076	/* configure PTP timestamping after VSI rebuild */
   7077	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
   7078		ice_ptp_cfg_timestamp(pf, false);
   7079
   7080	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
   7081	if (err) {
   7082		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
   7083		goto err_vsi_rebuild;
   7084	}
   7085
   7086	if (reset_type == ICE_RESET_PFR) {
   7087		err = ice_rebuild_channels(pf);
   7088		if (err) {
   7089			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
   7090				err);
   7091			goto err_vsi_rebuild;
   7092		}
   7093	}
   7094
   7095	/* If Flow Director is active */
   7096	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
   7097		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
   7098		if (err) {
   7099			dev_err(dev, "control VSI rebuild failed: %d\n", err);
   7100			goto err_vsi_rebuild;
   7101		}
   7102
   7103		/* replay HW Flow Director recipes */
   7104		if (hw->fdir_prof)
   7105			ice_fdir_replay_flows(hw);
   7106
   7107		/* replay Flow Director filters */
   7108		ice_fdir_replay_fltrs(pf);
   7109
   7110		ice_rebuild_arfs(pf);
   7111	}
   7112
   7113	ice_update_pf_netdev_link(pf);
   7114
   7115	/* tell the firmware we are up */
   7116	err = ice_send_version(pf);
   7117	if (err) {
   7118		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
   7119			err);
   7120		goto err_vsi_rebuild;
   7121	}
   7122
   7123	ice_replay_post(hw);
   7124
   7125	/* if we get here, reset flow is successful */
   7126	clear_bit(ICE_RESET_FAILED, pf->state);
   7127
   7128	ice_plug_aux_dev(pf);
   7129	return;
   7130
   7131err_vsi_rebuild:
   7132err_sched_init_port:
   7133	ice_sched_cleanup_all(hw);
   7134err_init_ctrlq:
   7135	ice_shutdown_all_ctrlq(hw);
   7136	set_bit(ICE_RESET_FAILED, pf->state);
   7137clear_recovery:
   7138	/* set this bit in PF state to control service task scheduling */
   7139	set_bit(ICE_NEEDS_RESTART, pf->state);
   7140	dev_err(dev, "Rebuild failed, unload and reload driver\n");
   7141}
   7142
   7143/**
   7144 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
   7145 * @vsi: Pointer to VSI structure
   7146 */
   7147static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
   7148{
   7149	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
   7150		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
   7151	else
   7152		return ICE_RXBUF_3072;
   7153}
   7154
   7155/**
   7156 * ice_change_mtu - NDO callback to change the MTU
   7157 * @netdev: network interface device structure
   7158 * @new_mtu: new value for maximum frame size
   7159 *
   7160 * Returns 0 on success, negative on failure
   7161 */
   7162static int ice_change_mtu(struct net_device *netdev, int new_mtu)
   7163{
   7164	struct ice_netdev_priv *np = netdev_priv(netdev);
   7165	struct ice_vsi *vsi = np->vsi;
   7166	struct ice_pf *pf = vsi->back;
   7167	u8 count = 0;
   7168	int err = 0;
   7169
   7170	if (new_mtu == (int)netdev->mtu) {
   7171		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
   7172		return 0;
   7173	}
   7174
   7175	if (ice_is_xdp_ena_vsi(vsi)) {
   7176		int frame_size = ice_max_xdp_frame_size(vsi);
   7177
   7178		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
   7179			netdev_err(netdev, "max MTU for XDP usage is %d\n",
   7180				   frame_size - ICE_ETH_PKT_HDR_PAD);
   7181			return -EINVAL;
   7182		}
   7183	}
   7184
   7185	/* if a reset is in progress, wait for some time for it to complete */
   7186	do {
   7187		if (ice_is_reset_in_progress(pf->state)) {
   7188			count++;
   7189			usleep_range(1000, 2000);
   7190		} else {
   7191			break;
   7192		}
   7193
   7194	} while (count < 100);
   7195
   7196	if (count == 100) {
   7197		netdev_err(netdev, "can't change MTU. Device is busy\n");
   7198		return -EBUSY;
   7199	}
   7200
   7201	netdev->mtu = (unsigned int)new_mtu;
   7202
   7203	/* if VSI is up, bring it down and then back up */
   7204	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
   7205		err = ice_down(vsi);
   7206		if (err) {
   7207			netdev_err(netdev, "change MTU if_down err %d\n", err);
   7208			return err;
   7209		}
   7210
   7211		err = ice_up(vsi);
   7212		if (err) {
   7213			netdev_err(netdev, "change MTU if_up err %d\n", err);
   7214			return err;
   7215		}
   7216	}
   7217
   7218	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
   7219	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
   7220
   7221	return err;
   7222}
   7223
   7224/**
   7225 * ice_eth_ioctl - Access the hwtstamp interface
   7226 * @netdev: network interface device structure
   7227 * @ifr: interface request data
   7228 * @cmd: ioctl command
   7229 */
   7230static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
   7231{
   7232	struct ice_netdev_priv *np = netdev_priv(netdev);
   7233	struct ice_pf *pf = np->vsi->back;
   7234
   7235	switch (cmd) {
   7236	case SIOCGHWTSTAMP:
   7237		return ice_ptp_get_ts_config(pf, ifr);
   7238	case SIOCSHWTSTAMP:
   7239		return ice_ptp_set_ts_config(pf, ifr);
   7240	default:
   7241		return -EOPNOTSUPP;
   7242	}
   7243}
   7244
   7245/**
   7246 * ice_aq_str - convert AQ err code to a string
   7247 * @aq_err: the AQ error code to convert
   7248 */
   7249const char *ice_aq_str(enum ice_aq_err aq_err)
   7250{
   7251	switch (aq_err) {
   7252	case ICE_AQ_RC_OK:
   7253		return "OK";
   7254	case ICE_AQ_RC_EPERM:
   7255		return "ICE_AQ_RC_EPERM";
   7256	case ICE_AQ_RC_ENOENT:
   7257		return "ICE_AQ_RC_ENOENT";
   7258	case ICE_AQ_RC_ENOMEM:
   7259		return "ICE_AQ_RC_ENOMEM";
   7260	case ICE_AQ_RC_EBUSY:
   7261		return "ICE_AQ_RC_EBUSY";
   7262	case ICE_AQ_RC_EEXIST:
   7263		return "ICE_AQ_RC_EEXIST";
   7264	case ICE_AQ_RC_EINVAL:
   7265		return "ICE_AQ_RC_EINVAL";
   7266	case ICE_AQ_RC_ENOSPC:
   7267		return "ICE_AQ_RC_ENOSPC";
   7268	case ICE_AQ_RC_ENOSYS:
   7269		return "ICE_AQ_RC_ENOSYS";
   7270	case ICE_AQ_RC_EMODE:
   7271		return "ICE_AQ_RC_EMODE";
   7272	case ICE_AQ_RC_ENOSEC:
   7273		return "ICE_AQ_RC_ENOSEC";
   7274	case ICE_AQ_RC_EBADSIG:
   7275		return "ICE_AQ_RC_EBADSIG";
   7276	case ICE_AQ_RC_ESVN:
   7277		return "ICE_AQ_RC_ESVN";
   7278	case ICE_AQ_RC_EBADMAN:
   7279		return "ICE_AQ_RC_EBADMAN";
   7280	case ICE_AQ_RC_EBADBUF:
   7281		return "ICE_AQ_RC_EBADBUF";
   7282	}
   7283
   7284	return "ICE_AQ_RC_UNKNOWN";
   7285}
   7286
   7287/**
   7288 * ice_set_rss_lut - Set RSS LUT
   7289 * @vsi: Pointer to VSI structure
   7290 * @lut: Lookup table
   7291 * @lut_size: Lookup table size
   7292 *
   7293 * Returns 0 on success, negative on failure
   7294 */
   7295int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
   7296{
   7297	struct ice_aq_get_set_rss_lut_params params = {};
   7298	struct ice_hw *hw = &vsi->back->hw;
   7299	int status;
   7300
   7301	if (!lut)
   7302		return -EINVAL;
   7303
   7304	params.vsi_handle = vsi->idx;
   7305	params.lut_size = lut_size;
   7306	params.lut_type = vsi->rss_lut_type;
   7307	params.lut = lut;
   7308
   7309	status = ice_aq_set_rss_lut(hw, &params);
   7310	if (status)
   7311		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
   7312			status, ice_aq_str(hw->adminq.sq_last_status));
   7313
   7314	return status;
   7315}
   7316
   7317/**
   7318 * ice_set_rss_key - Set RSS key
   7319 * @vsi: Pointer to the VSI structure
   7320 * @seed: RSS hash seed
   7321 *
   7322 * Returns 0 on success, negative on failure
   7323 */
   7324int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
   7325{
   7326	struct ice_hw *hw = &vsi->back->hw;
   7327	int status;
   7328
   7329	if (!seed)
   7330		return -EINVAL;
   7331
   7332	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
   7333	if (status)
   7334		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
   7335			status, ice_aq_str(hw->adminq.sq_last_status));
   7336
   7337	return status;
   7338}
   7339
   7340/**
   7341 * ice_get_rss_lut - Get RSS LUT
   7342 * @vsi: Pointer to VSI structure
   7343 * @lut: Buffer to store the lookup table entries
   7344 * @lut_size: Size of buffer to store the lookup table entries
   7345 *
   7346 * Returns 0 on success, negative on failure
   7347 */
   7348int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
   7349{
   7350	struct ice_aq_get_set_rss_lut_params params = {};
   7351	struct ice_hw *hw = &vsi->back->hw;
   7352	int status;
   7353
   7354	if (!lut)
   7355		return -EINVAL;
   7356
   7357	params.vsi_handle = vsi->idx;
   7358	params.lut_size = lut_size;
   7359	params.lut_type = vsi->rss_lut_type;
   7360	params.lut = lut;
   7361
   7362	status = ice_aq_get_rss_lut(hw, &params);
   7363	if (status)
   7364		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
   7365			status, ice_aq_str(hw->adminq.sq_last_status));
   7366
   7367	return status;
   7368}
   7369
   7370/**
   7371 * ice_get_rss_key - Get RSS key
   7372 * @vsi: Pointer to VSI structure
   7373 * @seed: Buffer to store the key in
   7374 *
   7375 * Returns 0 on success, negative on failure
   7376 */
   7377int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
   7378{
   7379	struct ice_hw *hw = &vsi->back->hw;
   7380	int status;
   7381
   7382	if (!seed)
   7383		return -EINVAL;
   7384
   7385	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
   7386	if (status)
   7387		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
   7388			status, ice_aq_str(hw->adminq.sq_last_status));
   7389
   7390	return status;
   7391}
   7392
   7393/**
   7394 * ice_bridge_getlink - Get the hardware bridge mode
   7395 * @skb: skb buff
   7396 * @pid: process ID
   7397 * @seq: RTNL message seq
   7398 * @dev: the netdev being configured
   7399 * @filter_mask: filter mask passed in
   7400 * @nlflags: netlink flags passed in
   7401 *
   7402 * Return the bridge mode (VEB/VEPA)
   7403 */
   7404static int
   7405ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
   7406		   struct net_device *dev, u32 filter_mask, int nlflags)
   7407{
   7408	struct ice_netdev_priv *np = netdev_priv(dev);
   7409	struct ice_vsi *vsi = np->vsi;
   7410	struct ice_pf *pf = vsi->back;
   7411	u16 bmode;
   7412
   7413	bmode = pf->first_sw->bridge_mode;
   7414
   7415	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
   7416				       filter_mask, NULL);
   7417}
   7418
   7419/**
   7420 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
   7421 * @vsi: Pointer to VSI structure
   7422 * @bmode: Hardware bridge mode (VEB/VEPA)
   7423 *
   7424 * Returns 0 on success, negative on failure
   7425 */
   7426static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
   7427{
   7428	struct ice_aqc_vsi_props *vsi_props;
   7429	struct ice_hw *hw = &vsi->back->hw;
   7430	struct ice_vsi_ctx *ctxt;
   7431	int ret;
   7432
   7433	vsi_props = &vsi->info;
   7434
   7435	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
   7436	if (!ctxt)
   7437		return -ENOMEM;
   7438
   7439	ctxt->info = vsi->info;
   7440
   7441	if (bmode == BRIDGE_MODE_VEB)
   7442		/* change from VEPA to VEB mode */
   7443		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
   7444	else
   7445		/* change from VEB to VEPA mode */
   7446		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
   7447	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
   7448
   7449	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
   7450	if (ret) {
   7451		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
   7452			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
   7453		goto out;
   7454	}
   7455	/* Update sw flags for book keeping */
   7456	vsi_props->sw_flags = ctxt->info.sw_flags;
   7457
   7458out:
   7459	kfree(ctxt);
   7460	return ret;
   7461}
   7462
   7463/**
   7464 * ice_bridge_setlink - Set the hardware bridge mode
   7465 * @dev: the netdev being configured
   7466 * @nlh: RTNL message
   7467 * @flags: bridge setlink flags
   7468 * @extack: netlink extended ack
   7469 *
   7470 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
   7471 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
   7472 * not already set for all VSIs connected to this switch. And also update the
   7473 * unicast switch filter rules for the corresponding switch of the netdev.
   7474 */
   7475static int
   7476ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
   7477		   u16 __always_unused flags,
   7478		   struct netlink_ext_ack __always_unused *extack)
   7479{
   7480	struct ice_netdev_priv *np = netdev_priv(dev);
   7481	struct ice_pf *pf = np->vsi->back;
   7482	struct nlattr *attr, *br_spec;
   7483	struct ice_hw *hw = &pf->hw;
   7484	struct ice_sw *pf_sw;
   7485	int rem, v, err = 0;
   7486
   7487	pf_sw = pf->first_sw;
   7488	/* find the attribute in the netlink message */
   7489	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
   7490
   7491	nla_for_each_nested(attr, br_spec, rem) {
   7492		__u16 mode;
   7493
   7494		if (nla_type(attr) != IFLA_BRIDGE_MODE)
   7495			continue;
   7496		mode = nla_get_u16(attr);
   7497		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
   7498			return -EINVAL;
   7499		/* Continue  if bridge mode is not being flipped */
   7500		if (mode == pf_sw->bridge_mode)
   7501			continue;
   7502		/* Iterates through the PF VSI list and update the loopback
   7503		 * mode of the VSI
   7504		 */
   7505		ice_for_each_vsi(pf, v) {
   7506			if (!pf->vsi[v])
   7507				continue;
   7508			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
   7509			if (err)
   7510				return err;
   7511		}
   7512
   7513		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
   7514		/* Update the unicast switch filter rules for the corresponding
   7515		 * switch of the netdev
   7516		 */
   7517		err = ice_update_sw_rule_bridge_mode(hw);
   7518		if (err) {
   7519			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
   7520				   mode, err,
   7521				   ice_aq_str(hw->adminq.sq_last_status));
   7522			/* revert hw->evb_veb */
   7523			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
   7524			return err;
   7525		}
   7526
   7527		pf_sw->bridge_mode = mode;
   7528	}
   7529
   7530	return 0;
   7531}
   7532
   7533/**
   7534 * ice_tx_timeout - Respond to a Tx Hang
   7535 * @netdev: network interface device structure
   7536 * @txqueue: Tx queue
   7537 */
   7538static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
   7539{
   7540	struct ice_netdev_priv *np = netdev_priv(netdev);
   7541	struct ice_tx_ring *tx_ring = NULL;
   7542	struct ice_vsi *vsi = np->vsi;
   7543	struct ice_pf *pf = vsi->back;
   7544	u32 i;
   7545
   7546	pf->tx_timeout_count++;
   7547
   7548	/* Check if PFC is enabled for the TC to which the queue belongs
   7549	 * to. If yes then Tx timeout is not caused by a hung queue, no
   7550	 * need to reset and rebuild
   7551	 */
   7552	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
   7553		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
   7554			 txqueue);
   7555		return;
   7556	}
   7557
   7558	/* now that we have an index, find the tx_ring struct */
   7559	ice_for_each_txq(vsi, i)
   7560		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
   7561			if (txqueue == vsi->tx_rings[i]->q_index) {
   7562				tx_ring = vsi->tx_rings[i];
   7563				break;
   7564			}
   7565
   7566	/* Reset recovery level if enough time has elapsed after last timeout.
   7567	 * Also ensure no new reset action happens before next timeout period.
   7568	 */
   7569	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
   7570		pf->tx_timeout_recovery_level = 1;
   7571	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
   7572				       netdev->watchdog_timeo)))
   7573		return;
   7574
   7575	if (tx_ring) {
   7576		struct ice_hw *hw = &pf->hw;
   7577		u32 head, val = 0;
   7578
   7579		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
   7580			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
   7581		/* Read interrupt register */
   7582		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
   7583
   7584		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
   7585			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
   7586			    head, tx_ring->next_to_use, val);
   7587	}
   7588
   7589	pf->tx_timeout_last_recovery = jiffies;
   7590	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
   7591		    pf->tx_timeout_recovery_level, txqueue);
   7592
   7593	switch (pf->tx_timeout_recovery_level) {
   7594	case 1:
   7595		set_bit(ICE_PFR_REQ, pf->state);
   7596		break;
   7597	case 2:
   7598		set_bit(ICE_CORER_REQ, pf->state);
   7599		break;
   7600	case 3:
   7601		set_bit(ICE_GLOBR_REQ, pf->state);
   7602		break;
   7603	default:
   7604		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
   7605		set_bit(ICE_DOWN, pf->state);
   7606		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
   7607		set_bit(ICE_SERVICE_DIS, pf->state);
   7608		break;
   7609	}
   7610
   7611	ice_service_task_schedule(pf);
   7612	pf->tx_timeout_recovery_level++;
   7613}
   7614
   7615/**
   7616 * ice_setup_tc_cls_flower - flower classifier offloads
   7617 * @np: net device to configure
   7618 * @filter_dev: device on which filter is added
   7619 * @cls_flower: offload data
   7620 */
   7621static int
   7622ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
   7623			struct net_device *filter_dev,
   7624			struct flow_cls_offload *cls_flower)
   7625{
   7626	struct ice_vsi *vsi = np->vsi;
   7627
   7628	if (cls_flower->common.chain_index)
   7629		return -EOPNOTSUPP;
   7630
   7631	switch (cls_flower->command) {
   7632	case FLOW_CLS_REPLACE:
   7633		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
   7634	case FLOW_CLS_DESTROY:
   7635		return ice_del_cls_flower(vsi, cls_flower);
   7636	default:
   7637		return -EINVAL;
   7638	}
   7639}
   7640
   7641/**
   7642 * ice_setup_tc_block_cb - callback handler registered for TC block
   7643 * @type: TC SETUP type
   7644 * @type_data: TC flower offload data that contains user input
   7645 * @cb_priv: netdev private data
   7646 */
   7647static int
   7648ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
   7649{
   7650	struct ice_netdev_priv *np = cb_priv;
   7651
   7652	switch (type) {
   7653	case TC_SETUP_CLSFLOWER:
   7654		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
   7655					       type_data);
   7656	default:
   7657		return -EOPNOTSUPP;
   7658	}
   7659}
   7660
   7661/**
   7662 * ice_validate_mqprio_qopt - Validate TCF input parameters
   7663 * @vsi: Pointer to VSI
   7664 * @mqprio_qopt: input parameters for mqprio queue configuration
   7665 *
   7666 * This function validates MQPRIO params, such as qcount (power of 2 wherever
   7667 * needed), and make sure user doesn't specify qcount and BW rate limit
   7668 * for TCs, which are more than "num_tc"
   7669 */
   7670static int
   7671ice_validate_mqprio_qopt(struct ice_vsi *vsi,
   7672			 struct tc_mqprio_qopt_offload *mqprio_qopt)
   7673{
   7674	u64 sum_max_rate = 0, sum_min_rate = 0;
   7675	int non_power_of_2_qcount = 0;
   7676	struct ice_pf *pf = vsi->back;
   7677	int max_rss_q_cnt = 0;
   7678	struct device *dev;
   7679	int i, speed;
   7680	u8 num_tc;
   7681
   7682	if (vsi->type != ICE_VSI_PF)
   7683		return -EINVAL;
   7684
   7685	if (mqprio_qopt->qopt.offset[0] != 0 ||
   7686	    mqprio_qopt->qopt.num_tc < 1 ||
   7687	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
   7688		return -EINVAL;
   7689
   7690	dev = ice_pf_to_dev(pf);
   7691	vsi->ch_rss_size = 0;
   7692	num_tc = mqprio_qopt->qopt.num_tc;
   7693
   7694	for (i = 0; num_tc; i++) {
   7695		int qcount = mqprio_qopt->qopt.count[i];
   7696		u64 max_rate, min_rate, rem;
   7697
   7698		if (!qcount)
   7699			return -EINVAL;
   7700
   7701		if (is_power_of_2(qcount)) {
   7702			if (non_power_of_2_qcount &&
   7703			    qcount > non_power_of_2_qcount) {
   7704				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
   7705					qcount, non_power_of_2_qcount);
   7706				return -EINVAL;
   7707			}
   7708			if (qcount > max_rss_q_cnt)
   7709				max_rss_q_cnt = qcount;
   7710		} else {
   7711			if (non_power_of_2_qcount &&
   7712			    qcount != non_power_of_2_qcount) {
   7713				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
   7714					qcount, non_power_of_2_qcount);
   7715				return -EINVAL;
   7716			}
   7717			if (qcount < max_rss_q_cnt) {
   7718				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
   7719					qcount, max_rss_q_cnt);
   7720				return -EINVAL;
   7721			}
   7722			max_rss_q_cnt = qcount;
   7723			non_power_of_2_qcount = qcount;
   7724		}
   7725
   7726		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
   7727		 * converts the bandwidth rate limit into Bytes/s when
   7728		 * passing it down to the driver. So convert input bandwidth
   7729		 * from Bytes/s to Kbps
   7730		 */
   7731		max_rate = mqprio_qopt->max_rate[i];
   7732		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
   7733		sum_max_rate += max_rate;
   7734
   7735		/* min_rate is minimum guaranteed rate and it can't be zero */
   7736		min_rate = mqprio_qopt->min_rate[i];
   7737		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
   7738		sum_min_rate += min_rate;
   7739
   7740		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
   7741			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
   7742				min_rate, ICE_MIN_BW_LIMIT);
   7743			return -EINVAL;
   7744		}
   7745
   7746		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
   7747		if (rem) {
   7748			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
   7749				i, ICE_MIN_BW_LIMIT);
   7750			return -EINVAL;
   7751		}
   7752
   7753		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
   7754		if (rem) {
   7755			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
   7756				i, ICE_MIN_BW_LIMIT);
   7757			return -EINVAL;
   7758		}
   7759
   7760		/* min_rate can't be more than max_rate, except when max_rate
   7761		 * is zero (implies max_rate sought is max line rate). In such
   7762		 * a case min_rate can be more than max.
   7763		 */
   7764		if (max_rate && min_rate > max_rate) {
   7765			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
   7766				min_rate, max_rate);
   7767			return -EINVAL;
   7768		}
   7769
   7770		if (i >= mqprio_qopt->qopt.num_tc - 1)
   7771			break;
   7772		if (mqprio_qopt->qopt.offset[i + 1] !=
   7773		    (mqprio_qopt->qopt.offset[i] + qcount))
   7774			return -EINVAL;
   7775	}
   7776	if (vsi->num_rxq <
   7777	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
   7778		return -EINVAL;
   7779	if (vsi->num_txq <
   7780	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
   7781		return -EINVAL;
   7782
   7783	speed = ice_get_link_speed_kbps(vsi);
   7784	if (sum_max_rate && sum_max_rate > (u64)speed) {
   7785		dev_err(dev, "Invalid max Tx rate(%llu) Kbps > speed(%u) Kbps specified\n",
   7786			sum_max_rate, speed);
   7787		return -EINVAL;
   7788	}
   7789	if (sum_min_rate && sum_min_rate > (u64)speed) {
   7790		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
   7791			sum_min_rate, speed);
   7792		return -EINVAL;
   7793	}
   7794
   7795	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
   7796	vsi->ch_rss_size = max_rss_q_cnt;
   7797
   7798	return 0;
   7799}
   7800
   7801/**
   7802 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
   7803 * @pf: ptr to PF device
   7804 * @vsi: ptr to VSI
   7805 */
   7806static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
   7807{
   7808	struct device *dev = ice_pf_to_dev(pf);
   7809	bool added = false;
   7810	struct ice_hw *hw;
   7811	int flow;
   7812
   7813	if (!(vsi->num_gfltr || vsi->num_bfltr))
   7814		return -EINVAL;
   7815
   7816	hw = &pf->hw;
   7817	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
   7818		struct ice_fd_hw_prof *prof;
   7819		int tun, status;
   7820		u64 entry_h;
   7821
   7822		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
   7823		      hw->fdir_prof[flow]->cnt))
   7824			continue;
   7825
   7826		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
   7827			enum ice_flow_priority prio;
   7828			u64 prof_id;
   7829
   7830			/* add this VSI to FDir profile for this flow */
   7831			prio = ICE_FLOW_PRIO_NORMAL;
   7832			prof = hw->fdir_prof[flow];
   7833			prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
   7834			status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
   7835						    prof->vsi_h[0], vsi->idx,
   7836						    prio, prof->fdir_seg[tun],
   7837						    &entry_h);
   7838			if (status) {
   7839				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
   7840					vsi->idx, flow);
   7841				continue;
   7842			}
   7843
   7844			prof->entry_h[prof->cnt][tun] = entry_h;
   7845		}
   7846
   7847		/* store VSI for filter replay and delete */
   7848		prof->vsi_h[prof->cnt] = vsi->idx;
   7849		prof->cnt++;
   7850
   7851		added = true;
   7852		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
   7853			flow);
   7854	}
   7855
   7856	if (!added)
   7857		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
   7858
   7859	return 0;
   7860}
   7861
   7862/**
   7863 * ice_add_channel - add a channel by adding VSI
   7864 * @pf: ptr to PF device
   7865 * @sw_id: underlying HW switching element ID
   7866 * @ch: ptr to channel structure
   7867 *
   7868 * Add a channel (VSI) using add_vsi and queue_map
   7869 */
   7870static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
   7871{
   7872	struct device *dev = ice_pf_to_dev(pf);
   7873	struct ice_vsi *vsi;
   7874
   7875	if (ch->type != ICE_VSI_CHNL) {
   7876		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
   7877		return -EINVAL;
   7878	}
   7879
   7880	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
   7881	if (!vsi || vsi->type != ICE_VSI_CHNL) {
   7882		dev_err(dev, "create chnl VSI failure\n");
   7883		return -EINVAL;
   7884	}
   7885
   7886	ice_add_vsi_to_fdir(pf, vsi);
   7887
   7888	ch->sw_id = sw_id;
   7889	ch->vsi_num = vsi->vsi_num;
   7890	ch->info.mapping_flags = vsi->info.mapping_flags;
   7891	ch->ch_vsi = vsi;
   7892	/* set the back pointer of channel for newly created VSI */
   7893	vsi->ch = ch;
   7894
   7895	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
   7896	       sizeof(vsi->info.q_mapping));
   7897	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
   7898	       sizeof(vsi->info.tc_mapping));
   7899
   7900	return 0;
   7901}
   7902
   7903/**
   7904 * ice_chnl_cfg_res
   7905 * @vsi: the VSI being setup
   7906 * @ch: ptr to channel structure
   7907 *
   7908 * Configure channel specific resources such as rings, vector.
   7909 */
   7910static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
   7911{
   7912	int i;
   7913
   7914	for (i = 0; i < ch->num_txq; i++) {
   7915		struct ice_q_vector *tx_q_vector, *rx_q_vector;
   7916		struct ice_ring_container *rc;
   7917		struct ice_tx_ring *tx_ring;
   7918		struct ice_rx_ring *rx_ring;
   7919
   7920		tx_ring = vsi->tx_rings[ch->base_q + i];
   7921		rx_ring = vsi->rx_rings[ch->base_q + i];
   7922		if (!tx_ring || !rx_ring)
   7923			continue;
   7924
   7925		/* setup ring being channel enabled */
   7926		tx_ring->ch = ch;
   7927		rx_ring->ch = ch;
   7928
   7929		/* following code block sets up vector specific attributes */
   7930		tx_q_vector = tx_ring->q_vector;
   7931		rx_q_vector = rx_ring->q_vector;
   7932		if (!tx_q_vector && !rx_q_vector)
   7933			continue;
   7934
   7935		if (tx_q_vector) {
   7936			tx_q_vector->ch = ch;
   7937			/* setup Tx and Rx ITR setting if DIM is off */
   7938			rc = &tx_q_vector->tx;
   7939			if (!ITR_IS_DYNAMIC(rc))
   7940				ice_write_itr(rc, rc->itr_setting);
   7941		}
   7942		if (rx_q_vector) {
   7943			rx_q_vector->ch = ch;
   7944			/* setup Tx and Rx ITR setting if DIM is off */
   7945			rc = &rx_q_vector->rx;
   7946			if (!ITR_IS_DYNAMIC(rc))
   7947				ice_write_itr(rc, rc->itr_setting);
   7948		}
   7949	}
   7950
   7951	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
   7952	 * GLINT_ITR register would have written to perform in-context
   7953	 * update, hence perform flush
   7954	 */
   7955	if (ch->num_txq || ch->num_rxq)
   7956		ice_flush(&vsi->back->hw);
   7957}
   7958
   7959/**
   7960 * ice_cfg_chnl_all_res - configure channel resources
   7961 * @vsi: pte to main_vsi
   7962 * @ch: ptr to channel structure
   7963 *
   7964 * This function configures channel specific resources such as flow-director
   7965 * counter index, and other resources such as queues, vectors, ITR settings
   7966 */
   7967static void
   7968ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
   7969{
   7970	/* configure channel (aka ADQ) resources such as queues, vectors,
   7971	 * ITR settings for channel specific vectors and anything else
   7972	 */
   7973	ice_chnl_cfg_res(vsi, ch);
   7974}
   7975
   7976/**
   7977 * ice_setup_hw_channel - setup new channel
   7978 * @pf: ptr to PF device
   7979 * @vsi: the VSI being setup
   7980 * @ch: ptr to channel structure
   7981 * @sw_id: underlying HW switching element ID
   7982 * @type: type of channel to be created (VMDq2/VF)
   7983 *
   7984 * Setup new channel (VSI) based on specified type (VMDq2/VF)
   7985 * and configures Tx rings accordingly
   7986 */
   7987static int
   7988ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
   7989		     struct ice_channel *ch, u16 sw_id, u8 type)
   7990{
   7991	struct device *dev = ice_pf_to_dev(pf);
   7992	int ret;
   7993
   7994	ch->base_q = vsi->next_base_q;
   7995	ch->type = type;
   7996
   7997	ret = ice_add_channel(pf, sw_id, ch);
   7998	if (ret) {
   7999		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
   8000		return ret;
   8001	}
   8002
   8003	/* configure/setup ADQ specific resources */
   8004	ice_cfg_chnl_all_res(vsi, ch);
   8005
   8006	/* make sure to update the next_base_q so that subsequent channel's
   8007	 * (aka ADQ) VSI queue map is correct
   8008	 */
   8009	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
   8010	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
   8011		ch->num_rxq);
   8012
   8013	return 0;
   8014}
   8015
   8016/**
   8017 * ice_setup_channel - setup new channel using uplink element
   8018 * @pf: ptr to PF device
   8019 * @vsi: the VSI being setup
   8020 * @ch: ptr to channel structure
   8021 *
   8022 * Setup new channel (VSI) based on specified type (VMDq2/VF)
   8023 * and uplink switching element
   8024 */
   8025static bool
   8026ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
   8027		  struct ice_channel *ch)
   8028{
   8029	struct device *dev = ice_pf_to_dev(pf);
   8030	u16 sw_id;
   8031	int ret;
   8032
   8033	if (vsi->type != ICE_VSI_PF) {
   8034		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
   8035		return false;
   8036	}
   8037
   8038	sw_id = pf->first_sw->sw_id;
   8039
   8040	/* create channel (VSI) */
   8041	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
   8042	if (ret) {
   8043		dev_err(dev, "failed to setup hw_channel\n");
   8044		return false;
   8045	}
   8046	dev_dbg(dev, "successfully created channel()\n");
   8047
   8048	return ch->ch_vsi ? true : false;
   8049}
   8050
   8051/**
   8052 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
   8053 * @vsi: VSI to be configured
   8054 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
   8055 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
   8056 */
   8057static int
   8058ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
   8059{
   8060	int err;
   8061
   8062	err = ice_set_min_bw_limit(vsi, min_tx_rate);
   8063	if (err)
   8064		return err;
   8065
   8066	return ice_set_max_bw_limit(vsi, max_tx_rate);
   8067}
   8068
   8069/**
   8070 * ice_create_q_channel - function to create channel
   8071 * @vsi: VSI to be configured
   8072 * @ch: ptr to channel (it contains channel specific params)
   8073 *
   8074 * This function creates channel (VSI) using num_queues specified by user,
   8075 * reconfigs RSS if needed.
   8076 */
   8077static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
   8078{
   8079	struct ice_pf *pf = vsi->back;
   8080	struct device *dev;
   8081
   8082	if (!ch)
   8083		return -EINVAL;
   8084
   8085	dev = ice_pf_to_dev(pf);
   8086	if (!ch->num_txq || !ch->num_rxq) {
   8087		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
   8088		return -EINVAL;
   8089	}
   8090
   8091	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
   8092		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
   8093			vsi->cnt_q_avail, ch->num_txq);
   8094		return -EINVAL;
   8095	}
   8096
   8097	if (!ice_setup_channel(pf, vsi, ch)) {
   8098		dev_info(dev, "Failed to setup channel\n");
   8099		return -EINVAL;
   8100	}
   8101	/* configure BW rate limit */
   8102	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
   8103		int ret;
   8104
   8105		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
   8106				       ch->min_tx_rate);
   8107		if (ret)
   8108			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
   8109				ch->max_tx_rate, ch->ch_vsi->vsi_num);
   8110		else
   8111			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
   8112				ch->max_tx_rate, ch->ch_vsi->vsi_num);
   8113	}
   8114
   8115	vsi->cnt_q_avail -= ch->num_txq;
   8116
   8117	return 0;
   8118}
   8119
   8120/**
   8121 * ice_rem_all_chnl_fltrs - removes all channel filters
   8122 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
   8123 *
   8124 * Remove all advanced switch filters only if they are channel specific
   8125 * tc-flower based filter
   8126 */
   8127static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
   8128{
   8129	struct ice_tc_flower_fltr *fltr;
   8130	struct hlist_node *node;
   8131
   8132	/* to remove all channel filters, iterate an ordered list of filters */
   8133	hlist_for_each_entry_safe(fltr, node,
   8134				  &pf->tc_flower_fltr_list,
   8135				  tc_flower_node) {
   8136		struct ice_rule_query_data rule;
   8137		int status;
   8138
   8139		/* for now process only channel specific filters */
   8140		if (!ice_is_chnl_fltr(fltr))
   8141			continue;
   8142
   8143		rule.rid = fltr->rid;
   8144		rule.rule_id = fltr->rule_id;
   8145		rule.vsi_handle = fltr->dest_id;
   8146		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
   8147		if (status) {
   8148			if (status == -ENOENT)
   8149				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
   8150					rule.rule_id);
   8151			else
   8152				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
   8153					status);
   8154		} else if (fltr->dest_vsi) {
   8155			/* update advanced switch filter count */
   8156			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
   8157				u32 flags = fltr->flags;
   8158
   8159				fltr->dest_vsi->num_chnl_fltr--;
   8160				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
   8161					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
   8162					pf->num_dmac_chnl_fltrs--;
   8163			}
   8164		}
   8165
   8166		hlist_del(&fltr->tc_flower_node);
   8167		kfree(fltr);
   8168	}
   8169}
   8170
   8171/**
   8172 * ice_remove_q_channels - Remove queue channels for the TCs
   8173 * @vsi: VSI to be configured
   8174 * @rem_fltr: delete advanced switch filter or not
   8175 *
   8176 * Remove queue channels for the TCs
   8177 */
   8178static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
   8179{
   8180	struct ice_channel *ch, *ch_tmp;
   8181	struct ice_pf *pf = vsi->back;
   8182	int i;
   8183
   8184	/* remove all tc-flower based filter if they are channel filters only */
   8185	if (rem_fltr)
   8186		ice_rem_all_chnl_fltrs(pf);
   8187
   8188	/* remove ntuple filters since queue configuration is being changed */
   8189	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
   8190		struct ice_hw *hw = &pf->hw;
   8191
   8192		mutex_lock(&hw->fdir_fltr_lock);
   8193		ice_fdir_del_all_fltrs(vsi);
   8194		mutex_unlock(&hw->fdir_fltr_lock);
   8195	}
   8196
   8197	/* perform cleanup for channels if they exist */
   8198	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
   8199		struct ice_vsi *ch_vsi;
   8200
   8201		list_del(&ch->list);
   8202		ch_vsi = ch->ch_vsi;
   8203		if (!ch_vsi) {
   8204			kfree(ch);
   8205			continue;
   8206		}
   8207
   8208		/* Reset queue contexts */
   8209		for (i = 0; i < ch->num_rxq; i++) {
   8210			struct ice_tx_ring *tx_ring;
   8211			struct ice_rx_ring *rx_ring;
   8212
   8213			tx_ring = vsi->tx_rings[ch->base_q + i];
   8214			rx_ring = vsi->rx_rings[ch->base_q + i];
   8215			if (tx_ring) {
   8216				tx_ring->ch = NULL;
   8217				if (tx_ring->q_vector)
   8218					tx_ring->q_vector->ch = NULL;
   8219			}
   8220			if (rx_ring) {
   8221				rx_ring->ch = NULL;
   8222				if (rx_ring->q_vector)
   8223					rx_ring->q_vector->ch = NULL;
   8224			}
   8225		}
   8226
   8227		/* Release FD resources for the channel VSI */
   8228		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
   8229
   8230		/* clear the VSI from scheduler tree */
   8231		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
   8232
   8233		/* Delete VSI from FW */
   8234		ice_vsi_delete(ch->ch_vsi);
   8235
   8236		/* Delete VSI from PF and HW VSI arrays */
   8237		ice_vsi_clear(ch->ch_vsi);
   8238
   8239		/* free the channel */
   8240		kfree(ch);
   8241	}
   8242
   8243	/* clear the channel VSI map which is stored in main VSI */
   8244	ice_for_each_chnl_tc(i)
   8245		vsi->tc_map_vsi[i] = NULL;
   8246
   8247	/* reset main VSI's all TC information */
   8248	vsi->all_enatc = 0;
   8249	vsi->all_numtc = 0;
   8250}
   8251
   8252/**
   8253 * ice_rebuild_channels - rebuild channel
   8254 * @pf: ptr to PF
   8255 *
   8256 * Recreate channel VSIs and replay filters
   8257 */
   8258static int ice_rebuild_channels(struct ice_pf *pf)
   8259{
   8260	struct device *dev = ice_pf_to_dev(pf);
   8261	struct ice_vsi *main_vsi;
   8262	bool rem_adv_fltr = true;
   8263	struct ice_channel *ch;
   8264	struct ice_vsi *vsi;
   8265	int tc_idx = 1;
   8266	int i, err;
   8267
   8268	main_vsi = ice_get_main_vsi(pf);
   8269	if (!main_vsi)
   8270		return 0;
   8271
   8272	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
   8273	    main_vsi->old_numtc == 1)
   8274		return 0; /* nothing to be done */
   8275
   8276	/* reconfigure main VSI based on old value of TC and cached values
   8277	 * for MQPRIO opts
   8278	 */
   8279	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
   8280	if (err) {
   8281		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
   8282			main_vsi->old_ena_tc, main_vsi->vsi_num);
   8283		return err;
   8284	}
   8285
   8286	/* rebuild ADQ VSIs */
   8287	ice_for_each_vsi(pf, i) {
   8288		enum ice_vsi_type type;
   8289
   8290		vsi = pf->vsi[i];
   8291		if (!vsi || vsi->type != ICE_VSI_CHNL)
   8292			continue;
   8293
   8294		type = vsi->type;
   8295
   8296		/* rebuild ADQ VSI */
   8297		err = ice_vsi_rebuild(vsi, true);
   8298		if (err) {
   8299			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
   8300				ice_vsi_type_str(type), vsi->idx, err);
   8301			goto cleanup;
   8302		}
   8303
   8304		/* Re-map HW VSI number, using VSI handle that has been
   8305		 * previously validated in ice_replay_vsi() call above
   8306		 */
   8307		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
   8308
   8309		/* replay filters for the VSI */
   8310		err = ice_replay_vsi(&pf->hw, vsi->idx);
   8311		if (err) {
   8312			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
   8313				ice_vsi_type_str(type), err, vsi->idx);
   8314			rem_adv_fltr = false;
   8315			goto cleanup;
   8316		}
   8317		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
   8318			 ice_vsi_type_str(type), vsi->idx);
   8319
   8320		/* store ADQ VSI at correct TC index in main VSI's
   8321		 * map of TC to VSI
   8322		 */
   8323		main_vsi->tc_map_vsi[tc_idx++] = vsi;
   8324	}
   8325
   8326	/* ADQ VSI(s) has been rebuilt successfully, so setup
   8327	 * channel for main VSI's Tx and Rx rings
   8328	 */
   8329	list_for_each_entry(ch, &main_vsi->ch_list, list) {
   8330		struct ice_vsi *ch_vsi;
   8331
   8332		ch_vsi = ch->ch_vsi;
   8333		if (!ch_vsi)
   8334			continue;
   8335
   8336		/* reconfig channel resources */
   8337		ice_cfg_chnl_all_res(main_vsi, ch);
   8338
   8339		/* replay BW rate limit if it is non-zero */
   8340		if (!ch->max_tx_rate && !ch->min_tx_rate)
   8341			continue;
   8342
   8343		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
   8344				       ch->min_tx_rate);
   8345		if (err)
   8346			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",
   8347				err, ch->max_tx_rate, ch->min_tx_rate,
   8348				ch_vsi->vsi_num);
   8349		else
   8350			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
   8351				ch->max_tx_rate, ch->min_tx_rate,
   8352				ch_vsi->vsi_num);
   8353	}
   8354
   8355	/* reconfig RSS for main VSI */
   8356	if (main_vsi->ch_rss_size)
   8357		ice_vsi_cfg_rss_lut_key(main_vsi);
   8358
   8359	return 0;
   8360
   8361cleanup:
   8362	ice_remove_q_channels(main_vsi, rem_adv_fltr);
   8363	return err;
   8364}
   8365
   8366/**
   8367 * ice_create_q_channels - Add queue channel for the given TCs
   8368 * @vsi: VSI to be configured
   8369 *
   8370 * Configures queue channel mapping to the given TCs
   8371 */
   8372static int ice_create_q_channels(struct ice_vsi *vsi)
   8373{
   8374	struct ice_pf *pf = vsi->back;
   8375	struct ice_channel *ch;
   8376	int ret = 0, i;
   8377
   8378	ice_for_each_chnl_tc(i) {
   8379		if (!(vsi->all_enatc & BIT(i)))
   8380			continue;
   8381
   8382		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
   8383		if (!ch) {
   8384			ret = -ENOMEM;
   8385			goto err_free;
   8386		}
   8387		INIT_LIST_HEAD(&ch->list);
   8388		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
   8389		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
   8390		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
   8391		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
   8392		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
   8393
   8394		/* convert to Kbits/s */
   8395		if (ch->max_tx_rate)
   8396			ch->max_tx_rate = div_u64(ch->max_tx_rate,
   8397						  ICE_BW_KBPS_DIVISOR);
   8398		if (ch->min_tx_rate)
   8399			ch->min_tx_rate = div_u64(ch->min_tx_rate,
   8400						  ICE_BW_KBPS_DIVISOR);
   8401
   8402		ret = ice_create_q_channel(vsi, ch);
   8403		if (ret) {
   8404			dev_err(ice_pf_to_dev(pf),
   8405				"failed creating channel TC:%d\n", i);
   8406			kfree(ch);
   8407			goto err_free;
   8408		}
   8409		list_add_tail(&ch->list, &vsi->ch_list);
   8410		vsi->tc_map_vsi[i] = ch->ch_vsi;
   8411		dev_dbg(ice_pf_to_dev(pf),
   8412			"successfully created channel: VSI %pK\n", ch->ch_vsi);
   8413	}
   8414	return 0;
   8415
   8416err_free:
   8417	ice_remove_q_channels(vsi, false);
   8418
   8419	return ret;
   8420}
   8421
   8422/**
   8423 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
   8424 * @netdev: net device to configure
   8425 * @type_data: TC offload data
   8426 */
   8427static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
   8428{
   8429	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
   8430	struct ice_netdev_priv *np = netdev_priv(netdev);
   8431	struct ice_vsi *vsi = np->vsi;
   8432	struct ice_pf *pf = vsi->back;
   8433	u16 mode, ena_tc_qdisc = 0;
   8434	int cur_txq, cur_rxq;
   8435	u8 hw = 0, num_tcf;
   8436	struct device *dev;
   8437	int ret, i;
   8438
   8439	dev = ice_pf_to_dev(pf);
   8440	num_tcf = mqprio_qopt->qopt.num_tc;
   8441	hw = mqprio_qopt->qopt.hw;
   8442	mode = mqprio_qopt->mode;
   8443	if (!hw) {
   8444		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
   8445		vsi->ch_rss_size = 0;
   8446		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
   8447		goto config_tcf;
   8448	}
   8449
   8450	/* Generate queue region map for number of TCF requested */
   8451	for (i = 0; i < num_tcf; i++)
   8452		ena_tc_qdisc |= BIT(i);
   8453
   8454	switch (mode) {
   8455	case TC_MQPRIO_MODE_CHANNEL:
   8456
   8457		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
   8458		if (ret) {
   8459			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
   8460				   ret);
   8461			return ret;
   8462		}
   8463		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
   8464		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
   8465		/* don't assume state of hw_tc_offload during driver load
   8466		 * and set the flag for TC flower filter if hw_tc_offload
   8467		 * already ON
   8468		 */
   8469		if (vsi->netdev->features & NETIF_F_HW_TC)
   8470			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
   8471		break;
   8472	default:
   8473		return -EINVAL;
   8474	}
   8475
   8476config_tcf:
   8477
   8478	/* Requesting same TCF configuration as already enabled */
   8479	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
   8480	    mode != TC_MQPRIO_MODE_CHANNEL)
   8481		return 0;
   8482
   8483	/* Pause VSI queues */
   8484	ice_dis_vsi(vsi, true);
   8485
   8486	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
   8487		ice_remove_q_channels(vsi, true);
   8488
   8489	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
   8490		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
   8491				     num_online_cpus());
   8492		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
   8493				     num_online_cpus());
   8494	} else {
   8495		/* logic to rebuild VSI, same like ethtool -L */
   8496		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
   8497
   8498		for (i = 0; i < num_tcf; i++) {
   8499			if (!(ena_tc_qdisc & BIT(i)))
   8500				continue;
   8501
   8502			offset = vsi->mqprio_qopt.qopt.offset[i];
   8503			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
   8504			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
   8505		}
   8506		vsi->req_txq = offset + qcount_tx;
   8507		vsi->req_rxq = offset + qcount_rx;
   8508
   8509		/* store away original rss_size info, so that it gets reused
   8510		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
   8511		 * determine, what should be the rss_sizefor main VSI
   8512		 */
   8513		vsi->orig_rss_size = vsi->rss_size;
   8514	}
   8515
   8516	/* save current values of Tx and Rx queues before calling VSI rebuild
   8517	 * for fallback option
   8518	 */
   8519	cur_txq = vsi->num_txq;
   8520	cur_rxq = vsi->num_rxq;
   8521
   8522	/* proceed with rebuild main VSI using correct number of queues */
   8523	ret = ice_vsi_rebuild(vsi, false);
   8524	if (ret) {
   8525		/* fallback to current number of queues */
   8526		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
   8527		vsi->req_txq = cur_txq;
   8528		vsi->req_rxq = cur_rxq;
   8529		clear_bit(ICE_RESET_FAILED, pf->state);
   8530		if (ice_vsi_rebuild(vsi, false)) {
   8531			dev_err(dev, "Rebuild of main VSI failed again\n");
   8532			return ret;
   8533		}
   8534	}
   8535
   8536	vsi->all_numtc = num_tcf;
   8537	vsi->all_enatc = ena_tc_qdisc;
   8538	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
   8539	if (ret) {
   8540		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
   8541			   vsi->vsi_num);
   8542		goto exit;
   8543	}
   8544
   8545	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
   8546		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
   8547		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
   8548
   8549		/* set TC0 rate limit if specified */
   8550		if (max_tx_rate || min_tx_rate) {
   8551			/* convert to Kbits/s */
   8552			if (max_tx_rate)
   8553				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
   8554			if (min_tx_rate)
   8555				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
   8556
   8557			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
   8558			if (!ret) {
   8559				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
   8560					max_tx_rate, min_tx_rate, vsi->vsi_num);
   8561			} else {
   8562				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
   8563					max_tx_rate, min_tx_rate, vsi->vsi_num);
   8564				goto exit;
   8565			}
   8566		}
   8567		ret = ice_create_q_channels(vsi);
   8568		if (ret) {
   8569			netdev_err(netdev, "failed configuring queue channels\n");
   8570			goto exit;
   8571		} else {
   8572			netdev_dbg(netdev, "successfully configured channels\n");
   8573		}
   8574	}
   8575
   8576	if (vsi->ch_rss_size)
   8577		ice_vsi_cfg_rss_lut_key(vsi);
   8578
   8579exit:
   8580	/* if error, reset the all_numtc and all_enatc */
   8581	if (ret) {
   8582		vsi->all_numtc = 0;
   8583		vsi->all_enatc = 0;
   8584	}
   8585	/* resume VSI */
   8586	ice_ena_vsi(vsi, true);
   8587
   8588	return ret;
   8589}
   8590
   8591static LIST_HEAD(ice_block_cb_list);
   8592
   8593static int
   8594ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
   8595	     void *type_data)
   8596{
   8597	struct ice_netdev_priv *np = netdev_priv(netdev);
   8598	struct ice_pf *pf = np->vsi->back;
   8599	int err;
   8600
   8601	switch (type) {
   8602	case TC_SETUP_BLOCK:
   8603		return flow_block_cb_setup_simple(type_data,
   8604						  &ice_block_cb_list,
   8605						  ice_setup_tc_block_cb,
   8606						  np, np, true);
   8607	case TC_SETUP_QDISC_MQPRIO:
   8608		/* setup traffic classifier for receive side */
   8609		mutex_lock(&pf->tc_mutex);
   8610		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
   8611		mutex_unlock(&pf->tc_mutex);
   8612		return err;
   8613	default:
   8614		return -EOPNOTSUPP;
   8615	}
   8616	return -EOPNOTSUPP;
   8617}
   8618
   8619static struct ice_indr_block_priv *
   8620ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
   8621			   struct net_device *netdev)
   8622{
   8623	struct ice_indr_block_priv *cb_priv;
   8624
   8625	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
   8626		if (!cb_priv->netdev)
   8627			return NULL;
   8628		if (cb_priv->netdev == netdev)
   8629			return cb_priv;
   8630	}
   8631	return NULL;
   8632}
   8633
   8634static int
   8635ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
   8636			void *indr_priv)
   8637{
   8638	struct ice_indr_block_priv *priv = indr_priv;
   8639	struct ice_netdev_priv *np = priv->np;
   8640
   8641	switch (type) {
   8642	case TC_SETUP_CLSFLOWER:
   8643		return ice_setup_tc_cls_flower(np, priv->netdev,
   8644					       (struct flow_cls_offload *)
   8645					       type_data);
   8646	default:
   8647		return -EOPNOTSUPP;
   8648	}
   8649}
   8650
   8651static int
   8652ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
   8653			struct ice_netdev_priv *np,
   8654			struct flow_block_offload *f, void *data,
   8655			void (*cleanup)(struct flow_block_cb *block_cb))
   8656{
   8657	struct ice_indr_block_priv *indr_priv;
   8658	struct flow_block_cb *block_cb;
   8659
   8660	if (!ice_is_tunnel_supported(netdev) &&
   8661	    !(is_vlan_dev(netdev) &&
   8662	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
   8663		return -EOPNOTSUPP;
   8664
   8665	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
   8666		return -EOPNOTSUPP;
   8667
   8668	switch (f->command) {
   8669	case FLOW_BLOCK_BIND:
   8670		indr_priv = ice_indr_block_priv_lookup(np, netdev);
   8671		if (indr_priv)
   8672			return -EEXIST;
   8673
   8674		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
   8675		if (!indr_priv)
   8676			return -ENOMEM;
   8677
   8678		indr_priv->netdev = netdev;
   8679		indr_priv->np = np;
   8680		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
   8681
   8682		block_cb =
   8683			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
   8684						 indr_priv, indr_priv,
   8685						 ice_rep_indr_tc_block_unbind,
   8686						 f, netdev, sch, data, np,
   8687						 cleanup);
   8688
   8689		if (IS_ERR(block_cb)) {
   8690			list_del(&indr_priv->list);
   8691			kfree(indr_priv);
   8692			return PTR_ERR(block_cb);
   8693		}
   8694		flow_block_cb_add(block_cb, f);
   8695		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
   8696		break;
   8697	case FLOW_BLOCK_UNBIND:
   8698		indr_priv = ice_indr_block_priv_lookup(np, netdev);
   8699		if (!indr_priv)
   8700			return -ENOENT;
   8701
   8702		block_cb = flow_block_cb_lookup(f->block,
   8703						ice_indr_setup_block_cb,
   8704						indr_priv);
   8705		if (!block_cb)
   8706			return -ENOENT;
   8707
   8708		flow_indr_block_cb_remove(block_cb, f);
   8709
   8710		list_del(&block_cb->driver_list);
   8711		break;
   8712	default:
   8713		return -EOPNOTSUPP;
   8714	}
   8715	return 0;
   8716}
   8717
   8718static int
   8719ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
   8720		     void *cb_priv, enum tc_setup_type type, void *type_data,
   8721		     void *data,
   8722		     void (*cleanup)(struct flow_block_cb *block_cb))
   8723{
   8724	switch (type) {
   8725	case TC_SETUP_BLOCK:
   8726		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
   8727					       data, cleanup);
   8728
   8729	default:
   8730		return -EOPNOTSUPP;
   8731	}
   8732}
   8733
   8734/**
   8735 * ice_open - Called when a network interface becomes active
   8736 * @netdev: network interface device structure
   8737 *
   8738 * The open entry point is called when a network interface is made
   8739 * active by the system (IFF_UP). At this point all resources needed
   8740 * for transmit and receive operations are allocated, the interrupt
   8741 * handler is registered with the OS, the netdev watchdog is enabled,
   8742 * and the stack is notified that the interface is ready.
   8743 *
   8744 * Returns 0 on success, negative value on failure
   8745 */
   8746int ice_open(struct net_device *netdev)
   8747{
   8748	struct ice_netdev_priv *np = netdev_priv(netdev);
   8749	struct ice_pf *pf = np->vsi->back;
   8750
   8751	if (ice_is_reset_in_progress(pf->state)) {
   8752		netdev_err(netdev, "can't open net device while reset is in progress");
   8753		return -EBUSY;
   8754	}
   8755
   8756	return ice_open_internal(netdev);
   8757}
   8758
   8759/**
   8760 * ice_open_internal - Called when a network interface becomes active
   8761 * @netdev: network interface device structure
   8762 *
   8763 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
   8764 * handling routine
   8765 *
   8766 * Returns 0 on success, negative value on failure
   8767 */
   8768int ice_open_internal(struct net_device *netdev)
   8769{
   8770	struct ice_netdev_priv *np = netdev_priv(netdev);
   8771	struct ice_vsi *vsi = np->vsi;
   8772	struct ice_pf *pf = vsi->back;
   8773	struct ice_port_info *pi;
   8774	int err;
   8775
   8776	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
   8777		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
   8778		return -EIO;
   8779	}
   8780
   8781	netif_carrier_off(netdev);
   8782
   8783	pi = vsi->port_info;
   8784	err = ice_update_link_info(pi);
   8785	if (err) {
   8786		netdev_err(netdev, "Failed to get link info, error %d\n", err);
   8787		return err;
   8788	}
   8789
   8790	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
   8791
   8792	/* Set PHY if there is media, otherwise, turn off PHY */
   8793	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
   8794		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
   8795		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
   8796			err = ice_init_phy_user_cfg(pi);
   8797			if (err) {
   8798				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
   8799					   err);
   8800				return err;
   8801			}
   8802		}
   8803
   8804		err = ice_configure_phy(vsi);
   8805		if (err) {
   8806			netdev_err(netdev, "Failed to set physical link up, error %d\n",
   8807				   err);
   8808			return err;
   8809		}
   8810	} else {
   8811		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
   8812		ice_set_link(vsi, false);
   8813	}
   8814
   8815	err = ice_vsi_open(vsi);
   8816	if (err)
   8817		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
   8818			   vsi->vsi_num, vsi->vsw->sw_id);
   8819
   8820	/* Update existing tunnels information */
   8821	udp_tunnel_get_rx_info(netdev);
   8822
   8823	return err;
   8824}
   8825
   8826/**
   8827 * ice_stop - Disables a network interface
   8828 * @netdev: network interface device structure
   8829 *
   8830 * The stop entry point is called when an interface is de-activated by the OS,
   8831 * and the netdevice enters the DOWN state. The hardware is still under the
   8832 * driver's control, but the netdev interface is disabled.
   8833 *
   8834 * Returns success only - not allowed to fail
   8835 */
   8836int ice_stop(struct net_device *netdev)
   8837{
   8838	struct ice_netdev_priv *np = netdev_priv(netdev);
   8839	struct ice_vsi *vsi = np->vsi;
   8840	struct ice_pf *pf = vsi->back;
   8841
   8842	if (ice_is_reset_in_progress(pf->state)) {
   8843		netdev_err(netdev, "can't stop net device while reset is in progress");
   8844		return -EBUSY;
   8845	}
   8846
   8847	ice_vsi_close(vsi);
   8848
   8849	return 0;
   8850}
   8851
   8852/**
   8853 * ice_features_check - Validate encapsulated packet conforms to limits
   8854 * @skb: skb buffer
   8855 * @netdev: This port's netdev
   8856 * @features: Offload features that the stack believes apply
   8857 */
   8858static netdev_features_t
   8859ice_features_check(struct sk_buff *skb,
   8860		   struct net_device __always_unused *netdev,
   8861		   netdev_features_t features)
   8862{
   8863	bool gso = skb_is_gso(skb);
   8864	size_t len;
   8865
   8866	/* No point in doing any of this if neither checksum nor GSO are
   8867	 * being requested for this frame. We can rule out both by just
   8868	 * checking for CHECKSUM_PARTIAL
   8869	 */
   8870	if (skb->ip_summed != CHECKSUM_PARTIAL)
   8871		return features;
   8872
   8873	/* We cannot support GSO if the MSS is going to be less than
   8874	 * 64 bytes. If it is then we need to drop support for GSO.
   8875	 */
   8876	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
   8877		features &= ~NETIF_F_GSO_MASK;
   8878
   8879	len = skb_network_offset(skb);
   8880	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
   8881		goto out_rm_features;
   8882
   8883	len = skb_network_header_len(skb);
   8884	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
   8885		goto out_rm_features;
   8886
   8887	if (skb->encapsulation) {
   8888		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
   8889		 * the case of IPIP frames, the transport header pointer is
   8890		 * after the inner header! So check to make sure that this
   8891		 * is a GRE or UDP_TUNNEL frame before doing that math.
   8892		 */
   8893		if (gso && (skb_shinfo(skb)->gso_type &
   8894			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
   8895			len = skb_inner_network_header(skb) -
   8896			      skb_transport_header(skb);
   8897			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
   8898				goto out_rm_features;
   8899		}
   8900
   8901		len = skb_inner_network_header_len(skb);
   8902		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
   8903			goto out_rm_features;
   8904	}
   8905
   8906	return features;
   8907out_rm_features:
   8908	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
   8909}
   8910
   8911static const struct net_device_ops ice_netdev_safe_mode_ops = {
   8912	.ndo_open = ice_open,
   8913	.ndo_stop = ice_stop,
   8914	.ndo_start_xmit = ice_start_xmit,
   8915	.ndo_set_mac_address = ice_set_mac_address,
   8916	.ndo_validate_addr = eth_validate_addr,
   8917	.ndo_change_mtu = ice_change_mtu,
   8918	.ndo_get_stats64 = ice_get_stats64,
   8919	.ndo_tx_timeout = ice_tx_timeout,
   8920	.ndo_bpf = ice_xdp_safe_mode,
   8921};
   8922
   8923static const struct net_device_ops ice_netdev_ops = {
   8924	.ndo_open = ice_open,
   8925	.ndo_stop = ice_stop,
   8926	.ndo_start_xmit = ice_start_xmit,
   8927	.ndo_select_queue = ice_select_queue,
   8928	.ndo_features_check = ice_features_check,
   8929	.ndo_fix_features = ice_fix_features,
   8930	.ndo_set_rx_mode = ice_set_rx_mode,
   8931	.ndo_set_mac_address = ice_set_mac_address,
   8932	.ndo_validate_addr = eth_validate_addr,
   8933	.ndo_change_mtu = ice_change_mtu,
   8934	.ndo_get_stats64 = ice_get_stats64,
   8935	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
   8936	.ndo_eth_ioctl = ice_eth_ioctl,
   8937	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
   8938	.ndo_set_vf_mac = ice_set_vf_mac,
   8939	.ndo_get_vf_config = ice_get_vf_cfg,
   8940	.ndo_set_vf_trust = ice_set_vf_trust,
   8941	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
   8942	.ndo_set_vf_link_state = ice_set_vf_link_state,
   8943	.ndo_get_vf_stats = ice_get_vf_stats,
   8944	.ndo_set_vf_rate = ice_set_vf_bw,
   8945	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
   8946	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
   8947	.ndo_setup_tc = ice_setup_tc,
   8948	.ndo_set_features = ice_set_features,
   8949	.ndo_bridge_getlink = ice_bridge_getlink,
   8950	.ndo_bridge_setlink = ice_bridge_setlink,
   8951	.ndo_fdb_add = ice_fdb_add,
   8952	.ndo_fdb_del = ice_fdb_del,
   8953#ifdef CONFIG_RFS_ACCEL
   8954	.ndo_rx_flow_steer = ice_rx_flow_steer,
   8955#endif
   8956	.ndo_tx_timeout = ice_tx_timeout,
   8957	.ndo_bpf = ice_xdp,
   8958	.ndo_xdp_xmit = ice_xdp_xmit,
   8959	.ndo_xsk_wakeup = ice_xsk_wakeup,
   8960	.ndo_get_devlink_port = ice_get_devlink_port,
   8961};