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_ethtool.c (123539B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (c) 2018, Intel Corporation. */
      3
      4/* ethtool support for ice */
      5
      6#include "ice.h"
      7#include "ice_flow.h"
      8#include "ice_fltr.h"
      9#include "ice_lib.h"
     10#include "ice_dcb_lib.h"
     11#include <net/dcbnl.h>
     12
     13struct ice_stats {
     14	char stat_string[ETH_GSTRING_LEN];
     15	int sizeof_stat;
     16	int stat_offset;
     17};
     18
     19#define ICE_STAT(_type, _name, _stat) { \
     20	.stat_string = _name, \
     21	.sizeof_stat = sizeof_field(_type, _stat), \
     22	.stat_offset = offsetof(_type, _stat) \
     23}
     24
     25#define ICE_VSI_STAT(_name, _stat) \
     26		ICE_STAT(struct ice_vsi, _name, _stat)
     27#define ICE_PF_STAT(_name, _stat) \
     28		ICE_STAT(struct ice_pf, _name, _stat)
     29
     30static int ice_q_stats_len(struct net_device *netdev)
     31{
     32	struct ice_netdev_priv *np = netdev_priv(netdev);
     33
     34	return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
     35		(sizeof(struct ice_q_stats) / sizeof(u64)));
     36}
     37
     38#define ICE_PF_STATS_LEN	ARRAY_SIZE(ice_gstrings_pf_stats)
     39#define ICE_VSI_STATS_LEN	ARRAY_SIZE(ice_gstrings_vsi_stats)
     40
     41#define ICE_PFC_STATS_LEN ( \
     42		(sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \
     43		 sizeof_field(struct ice_pf, stats.priority_xon_rx) + \
     44		 sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \
     45		 sizeof_field(struct ice_pf, stats.priority_xon_tx)) \
     46		 / sizeof(u64))
     47#define ICE_ALL_STATS_LEN(n)	(ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
     48				 ICE_VSI_STATS_LEN + ice_q_stats_len(n))
     49
     50static const struct ice_stats ice_gstrings_vsi_stats[] = {
     51	ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
     52	ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
     53	ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
     54	ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
     55	ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
     56	ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
     57	ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
     58	ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
     59	ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
     60	ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
     61	ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
     62	ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
     63	ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
     64	ICE_VSI_STAT("tx_linearize", tx_linearize),
     65	ICE_VSI_STAT("tx_busy", tx_busy),
     66	ICE_VSI_STAT("tx_restart", tx_restart),
     67};
     68
     69enum ice_ethtool_test_id {
     70	ICE_ETH_TEST_REG = 0,
     71	ICE_ETH_TEST_EEPROM,
     72	ICE_ETH_TEST_INTR,
     73	ICE_ETH_TEST_LOOP,
     74	ICE_ETH_TEST_LINK,
     75};
     76
     77static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
     78	"Register test  (offline)",
     79	"EEPROM test    (offline)",
     80	"Interrupt test (offline)",
     81	"Loopback test  (offline)",
     82	"Link test   (on/offline)",
     83};
     84
     85#define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
     86
     87/* These PF_STATs might look like duplicates of some NETDEV_STATs,
     88 * but they aren't. This device is capable of supporting multiple
     89 * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
     90 * netdevs whereas the PF_STATs are for the physical function that's
     91 * hosting these netdevs.
     92 *
     93 * The PF_STATs are appended to the netdev stats only when ethtool -S
     94 * is queried on the base PF netdev.
     95 */
     96static const struct ice_stats ice_gstrings_pf_stats[] = {
     97	ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
     98	ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
     99	ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
    100	ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
    101	ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
    102	ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
    103	ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
    104	ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
    105	ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
    106	ICE_PF_STAT("tx_timeout.nic", tx_timeout_count),
    107	ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
    108	ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
    109	ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
    110	ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
    111	ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
    112	ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
    113	ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
    114	ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
    115	ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
    116	ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
    117	ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
    118	ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
    119	ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
    120	ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
    121	ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
    122	ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
    123	ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
    124	ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
    125	ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
    126	ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
    127	ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
    128	ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
    129	ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
    130	ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
    131	ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
    132	ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
    133	ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
    134	ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
    135	ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
    136	ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
    137	ICE_PF_STAT("fdir_sb_match.nic", stats.fd_sb_match),
    138	ICE_PF_STAT("fdir_sb_status.nic", stats.fd_sb_status),
    139};
    140
    141static const u32 ice_regs_dump_list[] = {
    142	PFGEN_STATE,
    143	PRTGEN_STATUS,
    144	QRX_CTRL(0),
    145	QINT_TQCTL(0),
    146	QINT_RQCTL(0),
    147	PFINT_OICR_ENA,
    148	QRX_ITR(0),
    149};
    150
    151struct ice_priv_flag {
    152	char name[ETH_GSTRING_LEN];
    153	u32 bitno;			/* bit position in pf->flags */
    154};
    155
    156#define ICE_PRIV_FLAG(_name, _bitno) { \
    157	.name = _name, \
    158	.bitno = _bitno, \
    159}
    160
    161static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
    162	ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
    163	ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
    164	ICE_PRIV_FLAG("vf-true-promisc-support",
    165		      ICE_FLAG_VF_TRUE_PROMISC_ENA),
    166	ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF),
    167	ICE_PRIV_FLAG("vf-vlan-pruning", ICE_FLAG_VF_VLAN_PRUNING),
    168	ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX),
    169};
    170
    171#define ICE_PRIV_FLAG_ARRAY_SIZE	ARRAY_SIZE(ice_gstrings_priv_flags)
    172
    173static void
    174__ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo,
    175		  struct ice_vsi *vsi)
    176{
    177	struct ice_pf *pf = vsi->back;
    178	struct ice_hw *hw = &pf->hw;
    179	struct ice_orom_info *orom;
    180	struct ice_nvm_info *nvm;
    181
    182	nvm = &hw->flash.nvm;
    183	orom = &hw->flash.orom;
    184
    185	strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
    186
    187	/* Display NVM version (from which the firmware version can be
    188	 * determined) which contains more pertinent information.
    189	 */
    190	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
    191		 "%x.%02x 0x%x %d.%d.%d", nvm->major, nvm->minor,
    192		 nvm->eetrack, orom->major, orom->build, orom->patch);
    193
    194	strscpy(drvinfo->bus_info, pci_name(pf->pdev),
    195		sizeof(drvinfo->bus_info));
    196}
    197
    198static void
    199ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
    200{
    201	struct ice_netdev_priv *np = netdev_priv(netdev);
    202
    203	__ice_get_drvinfo(netdev, drvinfo, np->vsi);
    204	drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
    205}
    206
    207static int ice_get_regs_len(struct net_device __always_unused *netdev)
    208{
    209	return sizeof(ice_regs_dump_list);
    210}
    211
    212static void
    213ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
    214{
    215	struct ice_netdev_priv *np = netdev_priv(netdev);
    216	struct ice_pf *pf = np->vsi->back;
    217	struct ice_hw *hw = &pf->hw;
    218	u32 *regs_buf = (u32 *)p;
    219	unsigned int i;
    220
    221	regs->version = 1;
    222
    223	for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
    224		regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
    225}
    226
    227static u32 ice_get_msglevel(struct net_device *netdev)
    228{
    229	struct ice_netdev_priv *np = netdev_priv(netdev);
    230	struct ice_pf *pf = np->vsi->back;
    231
    232#ifndef CONFIG_DYNAMIC_DEBUG
    233	if (pf->hw.debug_mask)
    234		netdev_info(netdev, "hw debug_mask: 0x%llX\n",
    235			    pf->hw.debug_mask);
    236#endif /* !CONFIG_DYNAMIC_DEBUG */
    237
    238	return pf->msg_enable;
    239}
    240
    241static void ice_set_msglevel(struct net_device *netdev, u32 data)
    242{
    243	struct ice_netdev_priv *np = netdev_priv(netdev);
    244	struct ice_pf *pf = np->vsi->back;
    245
    246#ifndef CONFIG_DYNAMIC_DEBUG
    247	if (ICE_DBG_USER & data)
    248		pf->hw.debug_mask = data;
    249	else
    250		pf->msg_enable = data;
    251#else
    252	pf->msg_enable = data;
    253#endif /* !CONFIG_DYNAMIC_DEBUG */
    254}
    255
    256static int ice_get_eeprom_len(struct net_device *netdev)
    257{
    258	struct ice_netdev_priv *np = netdev_priv(netdev);
    259	struct ice_pf *pf = np->vsi->back;
    260
    261	return (int)pf->hw.flash.flash_size;
    262}
    263
    264static int
    265ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
    266	       u8 *bytes)
    267{
    268	struct ice_netdev_priv *np = netdev_priv(netdev);
    269	struct ice_vsi *vsi = np->vsi;
    270	struct ice_pf *pf = vsi->back;
    271	struct ice_hw *hw = &pf->hw;
    272	struct device *dev;
    273	int ret;
    274	u8 *buf;
    275
    276	dev = ice_pf_to_dev(pf);
    277
    278	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
    279	netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n",
    280		   eeprom->cmd, eeprom->offset, eeprom->len);
    281
    282	buf = kzalloc(eeprom->len, GFP_KERNEL);
    283	if (!buf)
    284		return -ENOMEM;
    285
    286	ret = ice_acquire_nvm(hw, ICE_RES_READ);
    287	if (ret) {
    288		dev_err(dev, "ice_acquire_nvm failed, err %d aq_err %s\n",
    289			ret, ice_aq_str(hw->adminq.sq_last_status));
    290		goto out;
    291	}
    292
    293	ret = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf,
    294				false);
    295	if (ret) {
    296		dev_err(dev, "ice_read_flat_nvm failed, err %d aq_err %s\n",
    297			ret, ice_aq_str(hw->adminq.sq_last_status));
    298		goto release;
    299	}
    300
    301	memcpy(bytes, buf, eeprom->len);
    302release:
    303	ice_release_nvm(hw);
    304out:
    305	kfree(buf);
    306	return ret;
    307}
    308
    309/**
    310 * ice_active_vfs - check if there are any active VFs
    311 * @pf: board private structure
    312 *
    313 * Returns true if an active VF is found, otherwise returns false
    314 */
    315static bool ice_active_vfs(struct ice_pf *pf)
    316{
    317	bool active = false;
    318	struct ice_vf *vf;
    319	unsigned int bkt;
    320
    321	rcu_read_lock();
    322	ice_for_each_vf_rcu(pf, bkt, vf) {
    323		if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
    324			active = true;
    325			break;
    326		}
    327	}
    328	rcu_read_unlock();
    329
    330	return active;
    331}
    332
    333/**
    334 * ice_link_test - perform a link test on a given net_device
    335 * @netdev: network interface device structure
    336 *
    337 * This function performs one of the self-tests required by ethtool.
    338 * Returns 0 on success, non-zero on failure.
    339 */
    340static u64 ice_link_test(struct net_device *netdev)
    341{
    342	struct ice_netdev_priv *np = netdev_priv(netdev);
    343	bool link_up = false;
    344	int status;
    345
    346	netdev_info(netdev, "link test\n");
    347	status = ice_get_link_status(np->vsi->port_info, &link_up);
    348	if (status) {
    349		netdev_err(netdev, "link query error, status = %d\n",
    350			   status);
    351		return 1;
    352	}
    353
    354	if (!link_up)
    355		return 2;
    356
    357	return 0;
    358}
    359
    360/**
    361 * ice_eeprom_test - perform an EEPROM test on a given net_device
    362 * @netdev: network interface device structure
    363 *
    364 * This function performs one of the self-tests required by ethtool.
    365 * Returns 0 on success, non-zero on failure.
    366 */
    367static u64 ice_eeprom_test(struct net_device *netdev)
    368{
    369	struct ice_netdev_priv *np = netdev_priv(netdev);
    370	struct ice_pf *pf = np->vsi->back;
    371
    372	netdev_info(netdev, "EEPROM test\n");
    373	return !!(ice_nvm_validate_checksum(&pf->hw));
    374}
    375
    376/**
    377 * ice_reg_pattern_test
    378 * @hw: pointer to the HW struct
    379 * @reg: reg to be tested
    380 * @mask: bits to be touched
    381 */
    382static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
    383{
    384	struct ice_pf *pf = (struct ice_pf *)hw->back;
    385	struct device *dev = ice_pf_to_dev(pf);
    386	static const u32 patterns[] = {
    387		0x5A5A5A5A, 0xA5A5A5A5,
    388		0x00000000, 0xFFFFFFFF
    389	};
    390	u32 val, orig_val;
    391	unsigned int i;
    392
    393	orig_val = rd32(hw, reg);
    394	for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
    395		u32 pattern = patterns[i] & mask;
    396
    397		wr32(hw, reg, pattern);
    398		val = rd32(hw, reg);
    399		if (val == pattern)
    400			continue;
    401		dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
    402			, __func__, reg, pattern, val);
    403		return 1;
    404	}
    405
    406	wr32(hw, reg, orig_val);
    407	val = rd32(hw, reg);
    408	if (val != orig_val) {
    409		dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
    410			, __func__, reg, orig_val, val);
    411		return 1;
    412	}
    413
    414	return 0;
    415}
    416
    417/**
    418 * ice_reg_test - perform a register test on a given net_device
    419 * @netdev: network interface device structure
    420 *
    421 * This function performs one of the self-tests required by ethtool.
    422 * Returns 0 on success, non-zero on failure.
    423 */
    424static u64 ice_reg_test(struct net_device *netdev)
    425{
    426	struct ice_netdev_priv *np = netdev_priv(netdev);
    427	struct ice_hw *hw = np->vsi->port_info->hw;
    428	u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
    429		hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
    430	struct ice_diag_reg_test_info {
    431		u32 address;
    432		u32 mask;
    433		u32 elem_num;
    434		u32 elem_size;
    435	} ice_reg_list[] = {
    436		{GLINT_ITR(0, 0), 0x00000fff, int_elements,
    437			GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
    438		{GLINT_ITR(1, 0), 0x00000fff, int_elements,
    439			GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
    440		{GLINT_ITR(0, 0), 0x00000fff, int_elements,
    441			GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
    442		{GLINT_CTL, 0xffff0001, 1, 0}
    443	};
    444	unsigned int i;
    445
    446	netdev_dbg(netdev, "Register test\n");
    447	for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
    448		u32 j;
    449
    450		for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
    451			u32 mask = ice_reg_list[i].mask;
    452			u32 reg = ice_reg_list[i].address +
    453				(j * ice_reg_list[i].elem_size);
    454
    455			/* bail on failure (non-zero return) */
    456			if (ice_reg_pattern_test(hw, reg, mask))
    457				return 1;
    458		}
    459	}
    460
    461	return 0;
    462}
    463
    464/**
    465 * ice_lbtest_prepare_rings - configure Tx/Rx test rings
    466 * @vsi: pointer to the VSI structure
    467 *
    468 * Function configures rings of a VSI for loopback test without
    469 * enabling interrupts or informing the kernel about new queues.
    470 *
    471 * Returns 0 on success, negative on failure.
    472 */
    473static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
    474{
    475	int status;
    476
    477	status = ice_vsi_setup_tx_rings(vsi);
    478	if (status)
    479		goto err_setup_tx_ring;
    480
    481	status = ice_vsi_setup_rx_rings(vsi);
    482	if (status)
    483		goto err_setup_rx_ring;
    484
    485	status = ice_vsi_cfg(vsi);
    486	if (status)
    487		goto err_setup_rx_ring;
    488
    489	status = ice_vsi_start_all_rx_rings(vsi);
    490	if (status)
    491		goto err_start_rx_ring;
    492
    493	return status;
    494
    495err_start_rx_ring:
    496	ice_vsi_free_rx_rings(vsi);
    497err_setup_rx_ring:
    498	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
    499err_setup_tx_ring:
    500	ice_vsi_free_tx_rings(vsi);
    501
    502	return status;
    503}
    504
    505/**
    506 * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
    507 * @vsi: pointer to the VSI structure
    508 *
    509 * Function stops and frees VSI rings after a loopback test.
    510 * Returns 0 on success, negative on failure.
    511 */
    512static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
    513{
    514	int status;
    515
    516	status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
    517	if (status)
    518		netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
    519			   vsi->vsi_num, status);
    520
    521	status = ice_vsi_stop_all_rx_rings(vsi);
    522	if (status)
    523		netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
    524			   vsi->vsi_num, status);
    525
    526	ice_vsi_free_tx_rings(vsi);
    527	ice_vsi_free_rx_rings(vsi);
    528
    529	return status;
    530}
    531
    532/**
    533 * ice_lbtest_create_frame - create test packet
    534 * @pf: pointer to the PF structure
    535 * @ret_data: allocated frame buffer
    536 * @size: size of the packet data
    537 *
    538 * Function allocates a frame with a test pattern on specific offsets.
    539 * Returns 0 on success, non-zero on failure.
    540 */
    541static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
    542{
    543	u8 *data;
    544
    545	if (!pf)
    546		return -EINVAL;
    547
    548	data = devm_kzalloc(ice_pf_to_dev(pf), size, GFP_KERNEL);
    549	if (!data)
    550		return -ENOMEM;
    551
    552	/* Since the ethernet test frame should always be at least
    553	 * 64 bytes long, fill some octets in the payload with test data.
    554	 */
    555	memset(data, 0xFF, size);
    556	data[32] = 0xDE;
    557	data[42] = 0xAD;
    558	data[44] = 0xBE;
    559	data[46] = 0xEF;
    560
    561	*ret_data = data;
    562
    563	return 0;
    564}
    565
    566/**
    567 * ice_lbtest_check_frame - verify received loopback frame
    568 * @frame: pointer to the raw packet data
    569 *
    570 * Function verifies received test frame with a pattern.
    571 * Returns true if frame matches the pattern, false otherwise.
    572 */
    573static bool ice_lbtest_check_frame(u8 *frame)
    574{
    575	/* Validate bytes of a frame under offsets chosen earlier */
    576	if (frame[32] == 0xDE &&
    577	    frame[42] == 0xAD &&
    578	    frame[44] == 0xBE &&
    579	    frame[46] == 0xEF &&
    580	    frame[48] == 0xFF)
    581		return true;
    582
    583	return false;
    584}
    585
    586/**
    587 * ice_diag_send - send test frames to the test ring
    588 * @tx_ring: pointer to the transmit ring
    589 * @data: pointer to the raw packet data
    590 * @size: size of the packet to send
    591 *
    592 * Function sends loopback packets on a test Tx ring.
    593 */
    594static int ice_diag_send(struct ice_tx_ring *tx_ring, u8 *data, u16 size)
    595{
    596	struct ice_tx_desc *tx_desc;
    597	struct ice_tx_buf *tx_buf;
    598	dma_addr_t dma;
    599	u64 td_cmd;
    600
    601	tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
    602	tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
    603
    604	dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
    605	if (dma_mapping_error(tx_ring->dev, dma))
    606		return -EINVAL;
    607
    608	tx_desc->buf_addr = cpu_to_le64(dma);
    609
    610	/* These flags are required for a descriptor to be pushed out */
    611	td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
    612	tx_desc->cmd_type_offset_bsz =
    613		cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
    614			    (td_cmd << ICE_TXD_QW1_CMD_S) |
    615			    ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
    616			    ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
    617			    ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
    618
    619	tx_buf->next_to_watch = tx_desc;
    620
    621	/* Force memory write to complete before letting h/w know
    622	 * there are new descriptors to fetch.
    623	 */
    624	wmb();
    625
    626	tx_ring->next_to_use++;
    627	if (tx_ring->next_to_use >= tx_ring->count)
    628		tx_ring->next_to_use = 0;
    629
    630	writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
    631
    632	/* Wait until the packets get transmitted to the receive queue. */
    633	usleep_range(1000, 2000);
    634	dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
    635
    636	return 0;
    637}
    638
    639#define ICE_LB_FRAME_SIZE 64
    640/**
    641 * ice_lbtest_receive_frames - receive and verify test frames
    642 * @rx_ring: pointer to the receive ring
    643 *
    644 * Function receives loopback packets and verify their correctness.
    645 * Returns number of received valid frames.
    646 */
    647static int ice_lbtest_receive_frames(struct ice_rx_ring *rx_ring)
    648{
    649	struct ice_rx_buf *rx_buf;
    650	int valid_frames, i;
    651	u8 *received_buf;
    652
    653	valid_frames = 0;
    654
    655	for (i = 0; i < rx_ring->count; i++) {
    656		union ice_32b_rx_flex_desc *rx_desc;
    657
    658		rx_desc = ICE_RX_DESC(rx_ring, i);
    659
    660		if (!(rx_desc->wb.status_error0 &
    661		    cpu_to_le16(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS)))
    662			continue;
    663
    664		rx_buf = &rx_ring->rx_buf[i];
    665		received_buf = page_address(rx_buf->page) + rx_buf->page_offset;
    666
    667		if (ice_lbtest_check_frame(received_buf))
    668			valid_frames++;
    669	}
    670
    671	return valid_frames;
    672}
    673
    674/**
    675 * ice_loopback_test - perform a loopback test on a given net_device
    676 * @netdev: network interface device structure
    677 *
    678 * This function performs one of the self-tests required by ethtool.
    679 * Returns 0 on success, non-zero on failure.
    680 */
    681static u64 ice_loopback_test(struct net_device *netdev)
    682{
    683	struct ice_netdev_priv *np = netdev_priv(netdev);
    684	struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
    685	struct ice_pf *pf = orig_vsi->back;
    686	u8 broadcast[ETH_ALEN], ret = 0;
    687	int num_frames, valid_frames;
    688	struct ice_tx_ring *tx_ring;
    689	struct ice_rx_ring *rx_ring;
    690	struct device *dev;
    691	u8 *tx_frame;
    692	int i;
    693
    694	dev = ice_pf_to_dev(pf);
    695	netdev_info(netdev, "loopback test\n");
    696
    697	test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
    698	if (!test_vsi) {
    699		netdev_err(netdev, "Failed to create a VSI for the loopback test\n");
    700		return 1;
    701	}
    702
    703	test_vsi->netdev = netdev;
    704	tx_ring = test_vsi->tx_rings[0];
    705	rx_ring = test_vsi->rx_rings[0];
    706
    707	if (ice_lbtest_prepare_rings(test_vsi)) {
    708		ret = 2;
    709		goto lbtest_vsi_close;
    710	}
    711
    712	if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
    713		ret = 3;
    714		goto lbtest_rings_dis;
    715	}
    716
    717	/* Enable MAC loopback in firmware */
    718	if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
    719		ret = 4;
    720		goto lbtest_mac_dis;
    721	}
    722
    723	/* Test VSI needs to receive broadcast packets */
    724	eth_broadcast_addr(broadcast);
    725	if (ice_fltr_add_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) {
    726		ret = 5;
    727		goto lbtest_mac_dis;
    728	}
    729
    730	if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
    731		ret = 7;
    732		goto remove_mac_filters;
    733	}
    734
    735	num_frames = min_t(int, tx_ring->count, 32);
    736	for (i = 0; i < num_frames; i++) {
    737		if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
    738			ret = 8;
    739			goto lbtest_free_frame;
    740		}
    741	}
    742
    743	valid_frames = ice_lbtest_receive_frames(rx_ring);
    744	if (!valid_frames)
    745		ret = 9;
    746	else if (valid_frames != num_frames)
    747		ret = 10;
    748
    749lbtest_free_frame:
    750	devm_kfree(dev, tx_frame);
    751remove_mac_filters:
    752	if (ice_fltr_remove_mac(test_vsi, broadcast, ICE_FWD_TO_VSI))
    753		netdev_err(netdev, "Could not remove MAC filter for the test VSI\n");
    754lbtest_mac_dis:
    755	/* Disable MAC loopback after the test is completed. */
    756	if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
    757		netdev_err(netdev, "Could not disable MAC loopback\n");
    758lbtest_rings_dis:
    759	if (ice_lbtest_disable_rings(test_vsi))
    760		netdev_err(netdev, "Could not disable test rings\n");
    761lbtest_vsi_close:
    762	test_vsi->netdev = NULL;
    763	if (ice_vsi_release(test_vsi))
    764		netdev_err(netdev, "Failed to remove the test VSI\n");
    765
    766	return ret;
    767}
    768
    769/**
    770 * ice_intr_test - perform an interrupt test on a given net_device
    771 * @netdev: network interface device structure
    772 *
    773 * This function performs one of the self-tests required by ethtool.
    774 * Returns 0 on success, non-zero on failure.
    775 */
    776static u64 ice_intr_test(struct net_device *netdev)
    777{
    778	struct ice_netdev_priv *np = netdev_priv(netdev);
    779	struct ice_pf *pf = np->vsi->back;
    780	u16 swic_old = pf->sw_int_count;
    781
    782	netdev_info(netdev, "interrupt test\n");
    783
    784	wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
    785	     GLINT_DYN_CTL_SW_ITR_INDX_M |
    786	     GLINT_DYN_CTL_INTENA_MSK_M |
    787	     GLINT_DYN_CTL_SWINT_TRIG_M);
    788
    789	usleep_range(1000, 2000);
    790	return (swic_old == pf->sw_int_count);
    791}
    792
    793/**
    794 * ice_self_test - handler function for performing a self-test by ethtool
    795 * @netdev: network interface device structure
    796 * @eth_test: ethtool_test structure
    797 * @data: required by ethtool.self_test
    798 *
    799 * This function is called after invoking 'ethtool -t devname' command where
    800 * devname is the name of the network device on which ethtool should operate.
    801 * It performs a set of self-tests to check if a device works properly.
    802 */
    803static void
    804ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
    805	      u64 *data)
    806{
    807	struct ice_netdev_priv *np = netdev_priv(netdev);
    808	bool if_running = netif_running(netdev);
    809	struct ice_pf *pf = np->vsi->back;
    810	struct device *dev;
    811
    812	dev = ice_pf_to_dev(pf);
    813
    814	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
    815		netdev_info(netdev, "offline testing starting\n");
    816
    817		set_bit(ICE_TESTING, pf->state);
    818
    819		if (ice_active_vfs(pf)) {
    820			dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
    821			data[ICE_ETH_TEST_REG] = 1;
    822			data[ICE_ETH_TEST_EEPROM] = 1;
    823			data[ICE_ETH_TEST_INTR] = 1;
    824			data[ICE_ETH_TEST_LOOP] = 1;
    825			data[ICE_ETH_TEST_LINK] = 1;
    826			eth_test->flags |= ETH_TEST_FL_FAILED;
    827			clear_bit(ICE_TESTING, pf->state);
    828			goto skip_ol_tests;
    829		}
    830		/* If the device is online then take it offline */
    831		if (if_running)
    832			/* indicate we're in test mode */
    833			ice_stop(netdev);
    834
    835		data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
    836		data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
    837		data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
    838		data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
    839		data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
    840
    841		if (data[ICE_ETH_TEST_LINK] ||
    842		    data[ICE_ETH_TEST_EEPROM] ||
    843		    data[ICE_ETH_TEST_LOOP] ||
    844		    data[ICE_ETH_TEST_INTR] ||
    845		    data[ICE_ETH_TEST_REG])
    846			eth_test->flags |= ETH_TEST_FL_FAILED;
    847
    848		clear_bit(ICE_TESTING, pf->state);
    849
    850		if (if_running) {
    851			int status = ice_open(netdev);
    852
    853			if (status) {
    854				dev_err(dev, "Could not open device %s, err %d\n",
    855					pf->int_name, status);
    856			}
    857		}
    858	} else {
    859		/* Online tests */
    860		netdev_info(netdev, "online testing starting\n");
    861
    862		data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
    863		if (data[ICE_ETH_TEST_LINK])
    864			eth_test->flags |= ETH_TEST_FL_FAILED;
    865
    866		/* Offline only tests, not run in online; pass by default */
    867		data[ICE_ETH_TEST_REG] = 0;
    868		data[ICE_ETH_TEST_EEPROM] = 0;
    869		data[ICE_ETH_TEST_INTR] = 0;
    870		data[ICE_ETH_TEST_LOOP] = 0;
    871	}
    872
    873skip_ol_tests:
    874	netdev_info(netdev, "testing finished\n");
    875}
    876
    877static void
    878__ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data,
    879		  struct ice_vsi *vsi)
    880{
    881	unsigned int i;
    882	u8 *p = data;
    883
    884	switch (stringset) {
    885	case ETH_SS_STATS:
    886		for (i = 0; i < ICE_VSI_STATS_LEN; i++)
    887			ethtool_sprintf(&p,
    888					ice_gstrings_vsi_stats[i].stat_string);
    889
    890		if (ice_is_port_repr_netdev(netdev))
    891			return;
    892
    893		ice_for_each_alloc_txq(vsi, i) {
    894			ethtool_sprintf(&p, "tx_queue_%u_packets", i);
    895			ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
    896		}
    897
    898		ice_for_each_alloc_rxq(vsi, i) {
    899			ethtool_sprintf(&p, "rx_queue_%u_packets", i);
    900			ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
    901		}
    902
    903		if (vsi->type != ICE_VSI_PF)
    904			return;
    905
    906		for (i = 0; i < ICE_PF_STATS_LEN; i++)
    907			ethtool_sprintf(&p,
    908					ice_gstrings_pf_stats[i].stat_string);
    909
    910		for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
    911			ethtool_sprintf(&p, "tx_priority_%u_xon.nic", i);
    912			ethtool_sprintf(&p, "tx_priority_%u_xoff.nic", i);
    913		}
    914		for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
    915			ethtool_sprintf(&p, "rx_priority_%u_xon.nic", i);
    916			ethtool_sprintf(&p, "rx_priority_%u_xoff.nic", i);
    917		}
    918		break;
    919	case ETH_SS_TEST:
    920		memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
    921		break;
    922	case ETH_SS_PRIV_FLAGS:
    923		for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++)
    924			ethtool_sprintf(&p, ice_gstrings_priv_flags[i].name);
    925		break;
    926	default:
    927		break;
    928	}
    929}
    930
    931static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
    932{
    933	struct ice_netdev_priv *np = netdev_priv(netdev);
    934
    935	__ice_get_strings(netdev, stringset, data, np->vsi);
    936}
    937
    938static int
    939ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
    940{
    941	struct ice_netdev_priv *np = netdev_priv(netdev);
    942	bool led_active;
    943
    944	switch (state) {
    945	case ETHTOOL_ID_ACTIVE:
    946		led_active = true;
    947		break;
    948	case ETHTOOL_ID_INACTIVE:
    949		led_active = false;
    950		break;
    951	default:
    952		return -EINVAL;
    953	}
    954
    955	if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
    956		return -EIO;
    957
    958	return 0;
    959}
    960
    961/**
    962 * ice_set_fec_cfg - Set link FEC options
    963 * @netdev: network interface device structure
    964 * @req_fec: FEC mode to configure
    965 */
    966static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
    967{
    968	struct ice_netdev_priv *np = netdev_priv(netdev);
    969	struct ice_aqc_set_phy_cfg_data config = { 0 };
    970	struct ice_vsi *vsi = np->vsi;
    971	struct ice_port_info *pi;
    972
    973	pi = vsi->port_info;
    974	if (!pi)
    975		return -EOPNOTSUPP;
    976
    977	/* Changing the FEC parameters is not supported if not the PF VSI */
    978	if (vsi->type != ICE_VSI_PF) {
    979		netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
    980		return -EOPNOTSUPP;
    981	}
    982
    983	/* Proceed only if requesting different FEC mode */
    984	if (pi->phy.curr_user_fec_req == req_fec)
    985		return 0;
    986
    987	/* Copy the current user PHY configuration. The current user PHY
    988	 * configuration is initialized during probe from PHY capabilities
    989	 * software mode, and updated on set PHY configuration.
    990	 */
    991	memcpy(&config, &pi->phy.curr_user_phy_cfg, sizeof(config));
    992
    993	ice_cfg_phy_fec(pi, &config, req_fec);
    994	config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
    995
    996	if (ice_aq_set_phy_cfg(pi->hw, pi, &config, NULL))
    997		return -EAGAIN;
    998
    999	/* Save requested FEC config */
   1000	pi->phy.curr_user_fec_req = req_fec;
   1001
   1002	return 0;
   1003}
   1004
   1005/**
   1006 * ice_set_fecparam - Set FEC link options
   1007 * @netdev: network interface device structure
   1008 * @fecparam: Ethtool structure to retrieve FEC parameters
   1009 */
   1010static int
   1011ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
   1012{
   1013	struct ice_netdev_priv *np = netdev_priv(netdev);
   1014	struct ice_vsi *vsi = np->vsi;
   1015	enum ice_fec_mode fec;
   1016
   1017	switch (fecparam->fec) {
   1018	case ETHTOOL_FEC_AUTO:
   1019		fec = ICE_FEC_AUTO;
   1020		break;
   1021	case ETHTOOL_FEC_RS:
   1022		fec = ICE_FEC_RS;
   1023		break;
   1024	case ETHTOOL_FEC_BASER:
   1025		fec = ICE_FEC_BASER;
   1026		break;
   1027	case ETHTOOL_FEC_OFF:
   1028	case ETHTOOL_FEC_NONE:
   1029		fec = ICE_FEC_NONE;
   1030		break;
   1031	default:
   1032		dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n",
   1033			 fecparam->fec);
   1034		return -EINVAL;
   1035	}
   1036
   1037	return ice_set_fec_cfg(netdev, fec);
   1038}
   1039
   1040/**
   1041 * ice_get_fecparam - Get link FEC options
   1042 * @netdev: network interface device structure
   1043 * @fecparam: Ethtool structure to retrieve FEC parameters
   1044 */
   1045static int
   1046ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
   1047{
   1048	struct ice_netdev_priv *np = netdev_priv(netdev);
   1049	struct ice_aqc_get_phy_caps_data *caps;
   1050	struct ice_link_status *link_info;
   1051	struct ice_vsi *vsi = np->vsi;
   1052	struct ice_port_info *pi;
   1053	int err;
   1054
   1055	pi = vsi->port_info;
   1056
   1057	if (!pi)
   1058		return -EOPNOTSUPP;
   1059	link_info = &pi->phy.link_info;
   1060
   1061	/* Set FEC mode based on negotiated link info */
   1062	switch (link_info->fec_info) {
   1063	case ICE_AQ_LINK_25G_KR_FEC_EN:
   1064		fecparam->active_fec = ETHTOOL_FEC_BASER;
   1065		break;
   1066	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
   1067	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
   1068		fecparam->active_fec = ETHTOOL_FEC_RS;
   1069		break;
   1070	default:
   1071		fecparam->active_fec = ETHTOOL_FEC_OFF;
   1072		break;
   1073	}
   1074
   1075	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
   1076	if (!caps)
   1077		return -ENOMEM;
   1078
   1079	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
   1080				  caps, NULL);
   1081	if (err)
   1082		goto done;
   1083
   1084	/* Set supported/configured FEC modes based on PHY capability */
   1085	if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
   1086		fecparam->fec |= ETHTOOL_FEC_AUTO;
   1087	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
   1088	    caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
   1089	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
   1090	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
   1091		fecparam->fec |= ETHTOOL_FEC_BASER;
   1092	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
   1093	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
   1094	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
   1095		fecparam->fec |= ETHTOOL_FEC_RS;
   1096	if (caps->link_fec_options == 0)
   1097		fecparam->fec |= ETHTOOL_FEC_OFF;
   1098
   1099done:
   1100	kfree(caps);
   1101	return err;
   1102}
   1103
   1104/**
   1105 * ice_nway_reset - restart autonegotiation
   1106 * @netdev: network interface device structure
   1107 */
   1108static int ice_nway_reset(struct net_device *netdev)
   1109{
   1110	struct ice_netdev_priv *np = netdev_priv(netdev);
   1111	struct ice_vsi *vsi = np->vsi;
   1112	int err;
   1113
   1114	/* If VSI state is up, then restart autoneg with link up */
   1115	if (!test_bit(ICE_DOWN, vsi->back->state))
   1116		err = ice_set_link(vsi, true);
   1117	else
   1118		err = ice_set_link(vsi, false);
   1119
   1120	return err;
   1121}
   1122
   1123/**
   1124 * ice_get_priv_flags - report device private flags
   1125 * @netdev: network interface device structure
   1126 *
   1127 * The get string set count and the string set should be matched for each
   1128 * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
   1129 * array.
   1130 *
   1131 * Returns a u32 bitmap of flags.
   1132 */
   1133static u32 ice_get_priv_flags(struct net_device *netdev)
   1134{
   1135	struct ice_netdev_priv *np = netdev_priv(netdev);
   1136	struct ice_vsi *vsi = np->vsi;
   1137	struct ice_pf *pf = vsi->back;
   1138	u32 i, ret_flags = 0;
   1139
   1140	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
   1141		const struct ice_priv_flag *priv_flag;
   1142
   1143		priv_flag = &ice_gstrings_priv_flags[i];
   1144
   1145		if (test_bit(priv_flag->bitno, pf->flags))
   1146			ret_flags |= BIT(i);
   1147	}
   1148
   1149	return ret_flags;
   1150}
   1151
   1152/**
   1153 * ice_set_priv_flags - set private flags
   1154 * @netdev: network interface device structure
   1155 * @flags: bit flags to be set
   1156 */
   1157static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
   1158{
   1159	struct ice_netdev_priv *np = netdev_priv(netdev);
   1160	DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
   1161	DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
   1162	struct ice_vsi *vsi = np->vsi;
   1163	struct ice_pf *pf = vsi->back;
   1164	struct device *dev;
   1165	int ret = 0;
   1166	u32 i;
   1167
   1168	if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
   1169		return -EINVAL;
   1170
   1171	dev = ice_pf_to_dev(pf);
   1172	set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
   1173
   1174	bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
   1175	for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
   1176		const struct ice_priv_flag *priv_flag;
   1177
   1178		priv_flag = &ice_gstrings_priv_flags[i];
   1179
   1180		if (flags & BIT(i))
   1181			set_bit(priv_flag->bitno, pf->flags);
   1182		else
   1183			clear_bit(priv_flag->bitno, pf->flags);
   1184	}
   1185
   1186	bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
   1187
   1188	/* Do not allow change to link-down-on-close when Total Port Shutdown
   1189	 * is enabled.
   1190	 */
   1191	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, change_flags) &&
   1192	    test_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
   1193		dev_err(dev, "Setting link-down-on-close not supported on this port\n");
   1194		set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
   1195		ret = -EINVAL;
   1196		goto ethtool_exit;
   1197	}
   1198
   1199	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
   1200		if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
   1201			int status;
   1202
   1203			/* Disable FW LLDP engine */
   1204			status = ice_cfg_lldp_mib_change(&pf->hw, false);
   1205
   1206			/* If unregistering for LLDP events fails, this is
   1207			 * not an error state, as there shouldn't be any
   1208			 * events to respond to.
   1209			 */
   1210			if (status)
   1211				dev_info(dev, "Failed to unreg for LLDP events\n");
   1212
   1213			/* The AQ call to stop the FW LLDP agent will generate
   1214			 * an error if the agent is already stopped.
   1215			 */
   1216			status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
   1217			if (status)
   1218				dev_warn(dev, "Fail to stop LLDP agent\n");
   1219			/* Use case for having the FW LLDP agent stopped
   1220			 * will likely not need DCB, so failure to init is
   1221			 * not a concern of ethtool
   1222			 */
   1223			status = ice_init_pf_dcb(pf, true);
   1224			if (status)
   1225				dev_warn(dev, "Fail to init DCB\n");
   1226
   1227			pf->dcbx_cap &= ~DCB_CAP_DCBX_LLD_MANAGED;
   1228			pf->dcbx_cap |= DCB_CAP_DCBX_HOST;
   1229		} else {
   1230			bool dcbx_agent_status;
   1231			int status;
   1232
   1233			if (ice_get_pfc_mode(pf) == ICE_QOS_MODE_DSCP) {
   1234				clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
   1235				dev_err(dev, "QoS in L3 DSCP mode, FW Agent not allowed to start\n");
   1236				ret = -EOPNOTSUPP;
   1237				goto ethtool_exit;
   1238			}
   1239
   1240			/* Remove rule to direct LLDP packets to default VSI.
   1241			 * The FW LLDP engine will now be consuming them.
   1242			 */
   1243			ice_cfg_sw_lldp(vsi, false, false);
   1244
   1245			/* AQ command to start FW LLDP agent will return an
   1246			 * error if the agent is already started
   1247			 */
   1248			status = ice_aq_start_lldp(&pf->hw, true, NULL);
   1249			if (status)
   1250				dev_warn(dev, "Fail to start LLDP Agent\n");
   1251
   1252			/* AQ command to start FW DCBX agent will fail if
   1253			 * the agent is already started
   1254			 */
   1255			status = ice_aq_start_stop_dcbx(&pf->hw, true,
   1256							&dcbx_agent_status,
   1257							NULL);
   1258			if (status)
   1259				dev_dbg(dev, "Failed to start FW DCBX\n");
   1260
   1261			dev_info(dev, "FW DCBX agent is %s\n",
   1262				 dcbx_agent_status ? "ACTIVE" : "DISABLED");
   1263
   1264			/* Failure to configure MIB change or init DCB is not
   1265			 * relevant to ethtool.  Print notification that
   1266			 * registration/init failed but do not return error
   1267			 * state to ethtool
   1268			 */
   1269			status = ice_init_pf_dcb(pf, true);
   1270			if (status)
   1271				dev_dbg(dev, "Fail to init DCB\n");
   1272
   1273			/* Register for MIB change events */
   1274			status = ice_cfg_lldp_mib_change(&pf->hw, true);
   1275			if (status)
   1276				dev_dbg(dev, "Fail to enable MIB change events\n");
   1277
   1278			pf->dcbx_cap &= ~DCB_CAP_DCBX_HOST;
   1279			pf->dcbx_cap |= DCB_CAP_DCBX_LLD_MANAGED;
   1280
   1281			ice_nway_reset(netdev);
   1282		}
   1283	}
   1284	if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) {
   1285		/* down and up VSI so that changes of Rx cfg are reflected. */
   1286		if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
   1287			ice_down(vsi);
   1288			ice_up(vsi);
   1289		}
   1290	}
   1291	/* don't allow modification of this flag when a single VF is in
   1292	 * promiscuous mode because it's not supported
   1293	 */
   1294	if (test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, change_flags) &&
   1295	    ice_is_any_vf_in_promisc(pf)) {
   1296		dev_err(dev, "Changing vf-true-promisc-support flag while VF(s) are in promiscuous mode not supported\n");
   1297		/* toggle bit back to previous state */
   1298		change_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags);
   1299		ret = -EAGAIN;
   1300	}
   1301
   1302	if (test_bit(ICE_FLAG_VF_VLAN_PRUNING, change_flags) &&
   1303	    ice_has_vfs(pf)) {
   1304		dev_err(dev, "vf-vlan-pruning: VLAN pruning cannot be changed while VFs are active.\n");
   1305		/* toggle bit back to previous state */
   1306		change_bit(ICE_FLAG_VF_VLAN_PRUNING, pf->flags);
   1307		ret = -EOPNOTSUPP;
   1308	}
   1309ethtool_exit:
   1310	clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
   1311	return ret;
   1312}
   1313
   1314static int ice_get_sset_count(struct net_device *netdev, int sset)
   1315{
   1316	switch (sset) {
   1317	case ETH_SS_STATS:
   1318		/* The number (and order) of strings reported *must* remain
   1319		 * constant for a given netdevice. This function must not
   1320		 * report a different number based on run time parameters
   1321		 * (such as the number of queues in use, or the setting of
   1322		 * a private ethtool flag). This is due to the nature of the
   1323		 * ethtool stats API.
   1324		 *
   1325		 * Userspace programs such as ethtool must make 3 separate
   1326		 * ioctl requests, one for size, one for the strings, and
   1327		 * finally one for the stats. Since these cross into
   1328		 * userspace, changes to the number or size could result in
   1329		 * undefined memory access or incorrect string<->value
   1330		 * correlations for statistics.
   1331		 *
   1332		 * Even if it appears to be safe, changes to the size or
   1333		 * order of strings will suffer from race conditions and are
   1334		 * not safe.
   1335		 */
   1336		return ICE_ALL_STATS_LEN(netdev);
   1337	case ETH_SS_TEST:
   1338		return ICE_TEST_LEN;
   1339	case ETH_SS_PRIV_FLAGS:
   1340		return ICE_PRIV_FLAG_ARRAY_SIZE;
   1341	default:
   1342		return -EOPNOTSUPP;
   1343	}
   1344}
   1345
   1346static void
   1347__ice_get_ethtool_stats(struct net_device *netdev,
   1348			struct ethtool_stats __always_unused *stats, u64 *data,
   1349			struct ice_vsi *vsi)
   1350{
   1351	struct ice_pf *pf = vsi->back;
   1352	struct ice_tx_ring *tx_ring;
   1353	struct ice_rx_ring *rx_ring;
   1354	unsigned int j;
   1355	int i = 0;
   1356	char *p;
   1357
   1358	ice_update_pf_stats(pf);
   1359	ice_update_vsi_stats(vsi);
   1360
   1361	for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
   1362		p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
   1363		data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
   1364			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
   1365	}
   1366
   1367	if (ice_is_port_repr_netdev(netdev))
   1368		return;
   1369
   1370	/* populate per queue stats */
   1371	rcu_read_lock();
   1372
   1373	ice_for_each_alloc_txq(vsi, j) {
   1374		tx_ring = READ_ONCE(vsi->tx_rings[j]);
   1375		if (tx_ring) {
   1376			data[i++] = tx_ring->stats.pkts;
   1377			data[i++] = tx_ring->stats.bytes;
   1378		} else {
   1379			data[i++] = 0;
   1380			data[i++] = 0;
   1381		}
   1382	}
   1383
   1384	ice_for_each_alloc_rxq(vsi, j) {
   1385		rx_ring = READ_ONCE(vsi->rx_rings[j]);
   1386		if (rx_ring) {
   1387			data[i++] = rx_ring->stats.pkts;
   1388			data[i++] = rx_ring->stats.bytes;
   1389		} else {
   1390			data[i++] = 0;
   1391			data[i++] = 0;
   1392		}
   1393	}
   1394
   1395	rcu_read_unlock();
   1396
   1397	if (vsi->type != ICE_VSI_PF)
   1398		return;
   1399
   1400	for (j = 0; j < ICE_PF_STATS_LEN; j++) {
   1401		p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
   1402		data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
   1403			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
   1404	}
   1405
   1406	for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
   1407		data[i++] = pf->stats.priority_xon_tx[j];
   1408		data[i++] = pf->stats.priority_xoff_tx[j];
   1409	}
   1410
   1411	for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
   1412		data[i++] = pf->stats.priority_xon_rx[j];
   1413		data[i++] = pf->stats.priority_xoff_rx[j];
   1414	}
   1415}
   1416
   1417static void
   1418ice_get_ethtool_stats(struct net_device *netdev,
   1419		      struct ethtool_stats __always_unused *stats, u64 *data)
   1420{
   1421	struct ice_netdev_priv *np = netdev_priv(netdev);
   1422
   1423	__ice_get_ethtool_stats(netdev, stats, data, np->vsi);
   1424}
   1425
   1426#define ICE_PHY_TYPE_LOW_MASK_MIN_1G	(ICE_PHY_TYPE_LOW_100BASE_TX | \
   1427					 ICE_PHY_TYPE_LOW_100M_SGMII)
   1428
   1429#define ICE_PHY_TYPE_LOW_MASK_MIN_25G	(ICE_PHY_TYPE_LOW_MASK_MIN_1G | \
   1430					 ICE_PHY_TYPE_LOW_1000BASE_T | \
   1431					 ICE_PHY_TYPE_LOW_1000BASE_SX | \
   1432					 ICE_PHY_TYPE_LOW_1000BASE_LX | \
   1433					 ICE_PHY_TYPE_LOW_1000BASE_KX | \
   1434					 ICE_PHY_TYPE_LOW_1G_SGMII | \
   1435					 ICE_PHY_TYPE_LOW_2500BASE_T | \
   1436					 ICE_PHY_TYPE_LOW_2500BASE_X | \
   1437					 ICE_PHY_TYPE_LOW_2500BASE_KX | \
   1438					 ICE_PHY_TYPE_LOW_5GBASE_T | \
   1439					 ICE_PHY_TYPE_LOW_5GBASE_KR | \
   1440					 ICE_PHY_TYPE_LOW_10GBASE_T | \
   1441					 ICE_PHY_TYPE_LOW_10G_SFI_DA | \
   1442					 ICE_PHY_TYPE_LOW_10GBASE_SR | \
   1443					 ICE_PHY_TYPE_LOW_10GBASE_LR | \
   1444					 ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 | \
   1445					 ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC | \
   1446					 ICE_PHY_TYPE_LOW_10G_SFI_C2C)
   1447
   1448#define ICE_PHY_TYPE_LOW_MASK_100G	(ICE_PHY_TYPE_LOW_100GBASE_CR4 | \
   1449					 ICE_PHY_TYPE_LOW_100GBASE_SR4 | \
   1450					 ICE_PHY_TYPE_LOW_100GBASE_LR4 | \
   1451					 ICE_PHY_TYPE_LOW_100GBASE_KR4 | \
   1452					 ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC | \
   1453					 ICE_PHY_TYPE_LOW_100G_CAUI4 | \
   1454					 ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC | \
   1455					 ICE_PHY_TYPE_LOW_100G_AUI4 | \
   1456					 ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 | \
   1457					 ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 | \
   1458					 ICE_PHY_TYPE_LOW_100GBASE_CP2 | \
   1459					 ICE_PHY_TYPE_LOW_100GBASE_SR2 | \
   1460					 ICE_PHY_TYPE_LOW_100GBASE_DR)
   1461
   1462#define ICE_PHY_TYPE_HIGH_MASK_100G	(ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4 | \
   1463					 ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |\
   1464					 ICE_PHY_TYPE_HIGH_100G_CAUI2 | \
   1465					 ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC | \
   1466					 ICE_PHY_TYPE_HIGH_100G_AUI2)
   1467
   1468/**
   1469 * ice_mask_min_supported_speeds
   1470 * @phy_types_high: PHY type high
   1471 * @phy_types_low: PHY type low to apply minimum supported speeds mask
   1472 *
   1473 * Apply minimum supported speeds mask to PHY type low. These are the speeds
   1474 * for ethtool supported link mode.
   1475 */
   1476static
   1477void ice_mask_min_supported_speeds(u64 phy_types_high, u64 *phy_types_low)
   1478{
   1479	/* if QSFP connection with 100G speed, minimum supported speed is 25G */
   1480	if (*phy_types_low & ICE_PHY_TYPE_LOW_MASK_100G ||
   1481	    phy_types_high & ICE_PHY_TYPE_HIGH_MASK_100G)
   1482		*phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_25G;
   1483	else
   1484		*phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_1G;
   1485}
   1486
   1487#define ice_ethtool_advertise_link_mode(aq_link_speed, ethtool_link_mode)    \
   1488	do {								     \
   1489		if (req_speeds & (aq_link_speed) ||			     \
   1490		    (!req_speeds &&					     \
   1491		     (advert_phy_type_lo & phy_type_mask_lo ||		     \
   1492		      advert_phy_type_hi & phy_type_mask_hi)))		     \
   1493			ethtool_link_ksettings_add_link_mode(ks, advertising,\
   1494							ethtool_link_mode);  \
   1495	} while (0)
   1496
   1497/**
   1498 * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
   1499 * @netdev: network interface device structure
   1500 * @ks: ethtool link ksettings struct to fill out
   1501 */
   1502static void
   1503ice_phy_type_to_ethtool(struct net_device *netdev,
   1504			struct ethtool_link_ksettings *ks)
   1505{
   1506	struct ice_netdev_priv *np = netdev_priv(netdev);
   1507	struct ice_vsi *vsi = np->vsi;
   1508	struct ice_pf *pf = vsi->back;
   1509	u64 advert_phy_type_lo = 0;
   1510	u64 advert_phy_type_hi = 0;
   1511	u64 phy_type_mask_lo = 0;
   1512	u64 phy_type_mask_hi = 0;
   1513	u64 phy_types_high = 0;
   1514	u64 phy_types_low = 0;
   1515	u16 req_speeds;
   1516
   1517	req_speeds = vsi->port_info->phy.link_info.req_speeds;
   1518
   1519	/* Check if lenient mode is supported and enabled, or in strict mode.
   1520	 *
   1521	 * In lenient mode the Supported link modes are the PHY types without
   1522	 * media. The Advertising link mode is either 1. the user requested
   1523	 * speed, 2. the override PHY mask, or 3. the PHY types with media.
   1524	 *
   1525	 * In strict mode Supported link mode are the PHY type with media,
   1526	 * and Advertising link modes are the media PHY type or the speed
   1527	 * requested by user.
   1528	 */
   1529	if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
   1530		phy_types_low = le64_to_cpu(pf->nvm_phy_type_lo);
   1531		phy_types_high = le64_to_cpu(pf->nvm_phy_type_hi);
   1532
   1533		ice_mask_min_supported_speeds(phy_types_high, &phy_types_low);
   1534		/* determine advertised modes based on link override only
   1535		 * if it's supported and if the FW doesn't abstract the
   1536		 * driver from having to account for link overrides
   1537		 */
   1538		if (ice_fw_supports_link_override(&pf->hw) &&
   1539		    !ice_fw_supports_report_dflt_cfg(&pf->hw)) {
   1540			struct ice_link_default_override_tlv *ldo;
   1541
   1542			ldo = &pf->link_dflt_override;
   1543			/* If override enabled and PHY mask set, then
   1544			 * Advertising link mode is the intersection of the PHY
   1545			 * types without media and the override PHY mask.
   1546			 */
   1547			if (ldo->options & ICE_LINK_OVERRIDE_EN &&
   1548			    (ldo->phy_type_low || ldo->phy_type_high)) {
   1549				advert_phy_type_lo =
   1550					le64_to_cpu(pf->nvm_phy_type_lo) &
   1551					ldo->phy_type_low;
   1552				advert_phy_type_hi =
   1553					le64_to_cpu(pf->nvm_phy_type_hi) &
   1554					ldo->phy_type_high;
   1555			}
   1556		}
   1557	} else {
   1558		/* strict mode */
   1559		phy_types_low = vsi->port_info->phy.phy_type_low;
   1560		phy_types_high = vsi->port_info->phy.phy_type_high;
   1561	}
   1562
   1563	/* If Advertising link mode PHY type is not using override PHY type,
   1564	 * then use PHY type with media.
   1565	 */
   1566	if (!advert_phy_type_lo && !advert_phy_type_hi) {
   1567		advert_phy_type_lo = vsi->port_info->phy.phy_type_low;
   1568		advert_phy_type_hi = vsi->port_info->phy.phy_type_high;
   1569	}
   1570
   1571	ethtool_link_ksettings_zero_link_mode(ks, supported);
   1572	ethtool_link_ksettings_zero_link_mode(ks, advertising);
   1573
   1574	phy_type_mask_lo = ICE_PHY_TYPE_LOW_100BASE_TX |
   1575			   ICE_PHY_TYPE_LOW_100M_SGMII;
   1576	if (phy_types_low & phy_type_mask_lo) {
   1577		ethtool_link_ksettings_add_link_mode(ks, supported,
   1578						     100baseT_Full);
   1579
   1580		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100MB,
   1581						100baseT_Full);
   1582	}
   1583
   1584	phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_T |
   1585			   ICE_PHY_TYPE_LOW_1G_SGMII;
   1586	if (phy_types_low & phy_type_mask_lo) {
   1587		ethtool_link_ksettings_add_link_mode(ks, supported,
   1588						     1000baseT_Full);
   1589		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
   1590						1000baseT_Full);
   1591	}
   1592
   1593	phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_KX;
   1594	if (phy_types_low & phy_type_mask_lo) {
   1595		ethtool_link_ksettings_add_link_mode(ks, supported,
   1596						     1000baseKX_Full);
   1597		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
   1598						1000baseKX_Full);
   1599	}
   1600
   1601	phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_SX |
   1602			   ICE_PHY_TYPE_LOW_1000BASE_LX;
   1603	if (phy_types_low & phy_type_mask_lo) {
   1604		ethtool_link_ksettings_add_link_mode(ks, supported,
   1605						     1000baseX_Full);
   1606		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
   1607						1000baseX_Full);
   1608	}
   1609
   1610	phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_T;
   1611	if (phy_types_low & phy_type_mask_lo) {
   1612		ethtool_link_ksettings_add_link_mode(ks, supported,
   1613						     2500baseT_Full);
   1614		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
   1615						2500baseT_Full);
   1616	}
   1617
   1618	phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_X |
   1619			   ICE_PHY_TYPE_LOW_2500BASE_KX;
   1620	if (phy_types_low & phy_type_mask_lo) {
   1621		ethtool_link_ksettings_add_link_mode(ks, supported,
   1622						     2500baseX_Full);
   1623		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
   1624						2500baseX_Full);
   1625	}
   1626
   1627	phy_type_mask_lo = ICE_PHY_TYPE_LOW_5GBASE_T |
   1628			   ICE_PHY_TYPE_LOW_5GBASE_KR;
   1629	if (phy_types_low & phy_type_mask_lo) {
   1630		ethtool_link_ksettings_add_link_mode(ks, supported,
   1631						     5000baseT_Full);
   1632		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_5GB,
   1633						5000baseT_Full);
   1634	}
   1635
   1636	phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_T |
   1637			   ICE_PHY_TYPE_LOW_10G_SFI_DA |
   1638			   ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC |
   1639			   ICE_PHY_TYPE_LOW_10G_SFI_C2C;
   1640	if (phy_types_low & phy_type_mask_lo) {
   1641		ethtool_link_ksettings_add_link_mode(ks, supported,
   1642						     10000baseT_Full);
   1643		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
   1644						10000baseT_Full);
   1645	}
   1646
   1647	phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_KR_CR1;
   1648	if (phy_types_low & phy_type_mask_lo) {
   1649		ethtool_link_ksettings_add_link_mode(ks, supported,
   1650						     10000baseKR_Full);
   1651		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
   1652						10000baseKR_Full);
   1653	}
   1654
   1655	phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_SR;
   1656	if (phy_types_low & phy_type_mask_lo) {
   1657		ethtool_link_ksettings_add_link_mode(ks, supported,
   1658						     10000baseSR_Full);
   1659		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
   1660						10000baseSR_Full);
   1661	}
   1662
   1663	phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_LR;
   1664	if (phy_types_low & phy_type_mask_lo) {
   1665		ethtool_link_ksettings_add_link_mode(ks, supported,
   1666						     10000baseLR_Full);
   1667		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
   1668						10000baseLR_Full);
   1669	}
   1670
   1671	phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_T |
   1672			   ICE_PHY_TYPE_LOW_25GBASE_CR |
   1673			   ICE_PHY_TYPE_LOW_25GBASE_CR_S |
   1674			   ICE_PHY_TYPE_LOW_25GBASE_CR1 |
   1675			   ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC |
   1676			   ICE_PHY_TYPE_LOW_25G_AUI_C2C;
   1677	if (phy_types_low & phy_type_mask_lo) {
   1678		ethtool_link_ksettings_add_link_mode(ks, supported,
   1679						     25000baseCR_Full);
   1680		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
   1681						25000baseCR_Full);
   1682	}
   1683
   1684	phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_SR |
   1685			   ICE_PHY_TYPE_LOW_25GBASE_LR;
   1686	if (phy_types_low & phy_type_mask_lo) {
   1687		ethtool_link_ksettings_add_link_mode(ks, supported,
   1688						     25000baseSR_Full);
   1689		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
   1690						25000baseSR_Full);
   1691	}
   1692
   1693	phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_KR |
   1694			   ICE_PHY_TYPE_LOW_25GBASE_KR_S |
   1695			   ICE_PHY_TYPE_LOW_25GBASE_KR1;
   1696	if (phy_types_low & phy_type_mask_lo) {
   1697		ethtool_link_ksettings_add_link_mode(ks, supported,
   1698						     25000baseKR_Full);
   1699		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
   1700						25000baseKR_Full);
   1701	}
   1702
   1703	phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_KR4;
   1704	if (phy_types_low & phy_type_mask_lo) {
   1705		ethtool_link_ksettings_add_link_mode(ks, supported,
   1706						     40000baseKR4_Full);
   1707		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
   1708						40000baseKR4_Full);
   1709	}
   1710
   1711	phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_CR4 |
   1712			   ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC |
   1713			   ICE_PHY_TYPE_LOW_40G_XLAUI;
   1714	if (phy_types_low & phy_type_mask_lo) {
   1715		ethtool_link_ksettings_add_link_mode(ks, supported,
   1716						     40000baseCR4_Full);
   1717		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
   1718						40000baseCR4_Full);
   1719	}
   1720
   1721	phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_SR4;
   1722	if (phy_types_low & phy_type_mask_lo) {
   1723		ethtool_link_ksettings_add_link_mode(ks, supported,
   1724						     40000baseSR4_Full);
   1725		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
   1726						40000baseSR4_Full);
   1727	}
   1728
   1729	phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_LR4;
   1730	if (phy_types_low & phy_type_mask_lo) {
   1731		ethtool_link_ksettings_add_link_mode(ks, supported,
   1732						     40000baseLR4_Full);
   1733		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
   1734						40000baseLR4_Full);
   1735	}
   1736
   1737	phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_CR2 |
   1738			   ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC |
   1739			   ICE_PHY_TYPE_LOW_50G_LAUI2 |
   1740			   ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC |
   1741			   ICE_PHY_TYPE_LOW_50G_AUI2 |
   1742			   ICE_PHY_TYPE_LOW_50GBASE_CP |
   1743			   ICE_PHY_TYPE_LOW_50GBASE_SR |
   1744			   ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC |
   1745			   ICE_PHY_TYPE_LOW_50G_AUI1;
   1746	if (phy_types_low & phy_type_mask_lo) {
   1747		ethtool_link_ksettings_add_link_mode(ks, supported,
   1748						     50000baseCR2_Full);
   1749		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
   1750						50000baseCR2_Full);
   1751	}
   1752
   1753	phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_KR2 |
   1754			   ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4;
   1755	if (phy_types_low & phy_type_mask_lo) {
   1756		ethtool_link_ksettings_add_link_mode(ks, supported,
   1757						     50000baseKR2_Full);
   1758		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
   1759						50000baseKR2_Full);
   1760	}
   1761
   1762	phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_SR2 |
   1763			   ICE_PHY_TYPE_LOW_50GBASE_LR2 |
   1764			   ICE_PHY_TYPE_LOW_50GBASE_FR |
   1765			   ICE_PHY_TYPE_LOW_50GBASE_LR;
   1766	if (phy_types_low & phy_type_mask_lo) {
   1767		ethtool_link_ksettings_add_link_mode(ks, supported,
   1768						     50000baseSR2_Full);
   1769		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
   1770						50000baseSR2_Full);
   1771	}
   1772
   1773	phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_CR4 |
   1774			   ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC |
   1775			   ICE_PHY_TYPE_LOW_100G_CAUI4 |
   1776			   ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC |
   1777			   ICE_PHY_TYPE_LOW_100G_AUI4 |
   1778			   ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 |
   1779			   ICE_PHY_TYPE_LOW_100GBASE_CP2;
   1780	phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |
   1781			   ICE_PHY_TYPE_HIGH_100G_CAUI2 |
   1782			   ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC |
   1783			   ICE_PHY_TYPE_HIGH_100G_AUI2;
   1784	if (phy_types_low & phy_type_mask_lo ||
   1785	    phy_types_high & phy_type_mask_hi) {
   1786		ethtool_link_ksettings_add_link_mode(ks, supported,
   1787						     100000baseCR4_Full);
   1788		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
   1789						100000baseCR4_Full);
   1790	}
   1791
   1792	phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_SR4 |
   1793			   ICE_PHY_TYPE_LOW_100GBASE_SR2;
   1794	if (phy_types_low & phy_type_mask_lo) {
   1795		ethtool_link_ksettings_add_link_mode(ks, supported,
   1796						     100000baseSR4_Full);
   1797		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
   1798						100000baseSR4_Full);
   1799	}
   1800
   1801	phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_LR4 |
   1802			   ICE_PHY_TYPE_LOW_100GBASE_DR;
   1803	if (phy_types_low & phy_type_mask_lo) {
   1804		ethtool_link_ksettings_add_link_mode(ks, supported,
   1805						     100000baseLR4_ER4_Full);
   1806		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
   1807						100000baseLR4_ER4_Full);
   1808	}
   1809
   1810	phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_KR4 |
   1811			   ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4;
   1812	phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4;
   1813	if (phy_types_low & phy_type_mask_lo ||
   1814	    phy_types_high & phy_type_mask_hi) {
   1815		ethtool_link_ksettings_add_link_mode(ks, supported,
   1816						     100000baseKR4_Full);
   1817		ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
   1818						100000baseKR4_Full);
   1819	}
   1820}
   1821
   1822#define TEST_SET_BITS_TIMEOUT	50
   1823#define TEST_SET_BITS_SLEEP_MAX	2000
   1824#define TEST_SET_BITS_SLEEP_MIN	1000
   1825
   1826/**
   1827 * ice_get_settings_link_up - Get Link settings for when link is up
   1828 * @ks: ethtool ksettings to fill in
   1829 * @netdev: network interface device structure
   1830 */
   1831static void
   1832ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
   1833			 struct net_device *netdev)
   1834{
   1835	struct ice_netdev_priv *np = netdev_priv(netdev);
   1836	struct ice_port_info *pi = np->vsi->port_info;
   1837	struct ice_link_status *link_info;
   1838	struct ice_vsi *vsi = np->vsi;
   1839
   1840	link_info = &vsi->port_info->phy.link_info;
   1841
   1842	/* Get supported and advertised settings from PHY ability with media */
   1843	ice_phy_type_to_ethtool(netdev, ks);
   1844
   1845	switch (link_info->link_speed) {
   1846	case ICE_AQ_LINK_SPEED_100GB:
   1847		ks->base.speed = SPEED_100000;
   1848		break;
   1849	case ICE_AQ_LINK_SPEED_50GB:
   1850		ks->base.speed = SPEED_50000;
   1851		break;
   1852	case ICE_AQ_LINK_SPEED_40GB:
   1853		ks->base.speed = SPEED_40000;
   1854		break;
   1855	case ICE_AQ_LINK_SPEED_25GB:
   1856		ks->base.speed = SPEED_25000;
   1857		break;
   1858	case ICE_AQ_LINK_SPEED_20GB:
   1859		ks->base.speed = SPEED_20000;
   1860		break;
   1861	case ICE_AQ_LINK_SPEED_10GB:
   1862		ks->base.speed = SPEED_10000;
   1863		break;
   1864	case ICE_AQ_LINK_SPEED_5GB:
   1865		ks->base.speed = SPEED_5000;
   1866		break;
   1867	case ICE_AQ_LINK_SPEED_2500MB:
   1868		ks->base.speed = SPEED_2500;
   1869		break;
   1870	case ICE_AQ_LINK_SPEED_1000MB:
   1871		ks->base.speed = SPEED_1000;
   1872		break;
   1873	case ICE_AQ_LINK_SPEED_100MB:
   1874		ks->base.speed = SPEED_100;
   1875		break;
   1876	default:
   1877		netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n",
   1878			    link_info->link_speed);
   1879		break;
   1880	}
   1881	ks->base.duplex = DUPLEX_FULL;
   1882
   1883	if (link_info->an_info & ICE_AQ_AN_COMPLETED)
   1884		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
   1885						     Autoneg);
   1886
   1887	/* Set flow control negotiated Rx/Tx pause */
   1888	switch (pi->fc.current_mode) {
   1889	case ICE_FC_FULL:
   1890		ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
   1891		break;
   1892	case ICE_FC_TX_PAUSE:
   1893		ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
   1894		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
   1895						     Asym_Pause);
   1896		break;
   1897	case ICE_FC_RX_PAUSE:
   1898		ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
   1899						     Asym_Pause);
   1900		break;
   1901	case ICE_FC_PFC:
   1902	default:
   1903		ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
   1904		ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
   1905						     Asym_Pause);
   1906		break;
   1907	}
   1908}
   1909
   1910/**
   1911 * ice_get_settings_link_down - Get the Link settings when link is down
   1912 * @ks: ethtool ksettings to fill in
   1913 * @netdev: network interface device structure
   1914 *
   1915 * Reports link settings that can be determined when link is down
   1916 */
   1917static void
   1918ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
   1919			   struct net_device *netdev)
   1920{
   1921	/* link is down and the driver needs to fall back on
   1922	 * supported PHY types to figure out what info to display
   1923	 */
   1924	ice_phy_type_to_ethtool(netdev, ks);
   1925
   1926	/* With no link, speed and duplex are unknown */
   1927	ks->base.speed = SPEED_UNKNOWN;
   1928	ks->base.duplex = DUPLEX_UNKNOWN;
   1929}
   1930
   1931/**
   1932 * ice_get_link_ksettings - Get Link Speed and Duplex settings
   1933 * @netdev: network interface device structure
   1934 * @ks: ethtool ksettings
   1935 *
   1936 * Reports speed/duplex settings based on media_type
   1937 */
   1938static int
   1939ice_get_link_ksettings(struct net_device *netdev,
   1940		       struct ethtool_link_ksettings *ks)
   1941{
   1942	struct ice_netdev_priv *np = netdev_priv(netdev);
   1943	struct ice_aqc_get_phy_caps_data *caps;
   1944	struct ice_link_status *hw_link_info;
   1945	struct ice_vsi *vsi = np->vsi;
   1946	int err;
   1947
   1948	ethtool_link_ksettings_zero_link_mode(ks, supported);
   1949	ethtool_link_ksettings_zero_link_mode(ks, advertising);
   1950	ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
   1951	hw_link_info = &vsi->port_info->phy.link_info;
   1952
   1953	/* set speed and duplex */
   1954	if (hw_link_info->link_info & ICE_AQ_LINK_UP)
   1955		ice_get_settings_link_up(ks, netdev);
   1956	else
   1957		ice_get_settings_link_down(ks, netdev);
   1958
   1959	/* set autoneg settings */
   1960	ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
   1961		AUTONEG_ENABLE : AUTONEG_DISABLE;
   1962
   1963	/* set media type settings */
   1964	switch (vsi->port_info->phy.media_type) {
   1965	case ICE_MEDIA_FIBER:
   1966		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
   1967		ks->base.port = PORT_FIBRE;
   1968		break;
   1969	case ICE_MEDIA_BASET:
   1970		ethtool_link_ksettings_add_link_mode(ks, supported, TP);
   1971		ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
   1972		ks->base.port = PORT_TP;
   1973		break;
   1974	case ICE_MEDIA_BACKPLANE:
   1975		ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
   1976		ethtool_link_ksettings_add_link_mode(ks, advertising,
   1977						     Backplane);
   1978		ks->base.port = PORT_NONE;
   1979		break;
   1980	case ICE_MEDIA_DA:
   1981		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
   1982		ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
   1983		ks->base.port = PORT_DA;
   1984		break;
   1985	default:
   1986		ks->base.port = PORT_OTHER;
   1987		break;
   1988	}
   1989
   1990	/* flow control is symmetric and always supported */
   1991	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
   1992
   1993	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
   1994	if (!caps)
   1995		return -ENOMEM;
   1996
   1997	err = ice_aq_get_phy_caps(vsi->port_info, false,
   1998				  ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
   1999	if (err)
   2000		goto done;
   2001
   2002	/* Set the advertised flow control based on the PHY capability */
   2003	if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
   2004	    (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
   2005		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
   2006		ethtool_link_ksettings_add_link_mode(ks, advertising,
   2007						     Asym_Pause);
   2008	} else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
   2009		ethtool_link_ksettings_add_link_mode(ks, advertising,
   2010						     Asym_Pause);
   2011	} else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
   2012		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
   2013		ethtool_link_ksettings_add_link_mode(ks, advertising,
   2014						     Asym_Pause);
   2015	} else {
   2016		ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
   2017		ethtool_link_ksettings_del_link_mode(ks, advertising,
   2018						     Asym_Pause);
   2019	}
   2020
   2021	/* Set advertised FEC modes based on PHY capability */
   2022	ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
   2023
   2024	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
   2025	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
   2026		ethtool_link_ksettings_add_link_mode(ks, advertising,
   2027						     FEC_BASER);
   2028	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
   2029	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
   2030		ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
   2031
   2032	err = ice_aq_get_phy_caps(vsi->port_info, false,
   2033				  ICE_AQC_REPORT_TOPO_CAP_MEDIA, caps, NULL);
   2034	if (err)
   2035		goto done;
   2036
   2037	/* Set supported FEC modes based on PHY capability */
   2038	ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
   2039
   2040	if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
   2041	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
   2042		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
   2043	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
   2044		ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
   2045
   2046	/* Set supported and advertised autoneg */
   2047	if (ice_is_phy_caps_an_enabled(caps)) {
   2048		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
   2049		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
   2050	}
   2051
   2052done:
   2053	kfree(caps);
   2054	return err;
   2055}
   2056
   2057/**
   2058 * ice_ksettings_find_adv_link_speed - Find advertising link speed
   2059 * @ks: ethtool ksettings
   2060 */
   2061static u16
   2062ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
   2063{
   2064	u16 adv_link_speed = 0;
   2065
   2066	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2067						  100baseT_Full))
   2068		adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
   2069	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2070						  1000baseX_Full))
   2071		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
   2072	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2073						  1000baseT_Full) ||
   2074	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2075						  1000baseKX_Full))
   2076		adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
   2077	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2078						  2500baseT_Full))
   2079		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
   2080	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2081						  2500baseX_Full))
   2082		adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
   2083	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2084						  5000baseT_Full))
   2085		adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
   2086	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2087						  10000baseT_Full) ||
   2088	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2089						  10000baseKR_Full))
   2090		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
   2091	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2092						  10000baseSR_Full) ||
   2093	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2094						  10000baseLR_Full))
   2095		adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
   2096	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2097						  25000baseCR_Full) ||
   2098	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2099						  25000baseSR_Full) ||
   2100	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2101						  25000baseKR_Full))
   2102		adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
   2103	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2104						  40000baseCR4_Full) ||
   2105	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2106						  40000baseSR4_Full) ||
   2107	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2108						  40000baseLR4_Full) ||
   2109	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2110						  40000baseKR4_Full))
   2111		adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
   2112	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2113						  50000baseCR2_Full) ||
   2114	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2115						  50000baseKR2_Full))
   2116		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
   2117	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2118						  50000baseSR2_Full))
   2119		adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
   2120	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2121						  100000baseCR4_Full) ||
   2122	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2123						  100000baseSR4_Full) ||
   2124	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2125						  100000baseLR4_ER4_Full) ||
   2126	    ethtool_link_ksettings_test_link_mode(ks, advertising,
   2127						  100000baseKR4_Full))
   2128		adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
   2129
   2130	return adv_link_speed;
   2131}
   2132
   2133/**
   2134 * ice_setup_autoneg
   2135 * @p: port info
   2136 * @ks: ethtool_link_ksettings
   2137 * @config: configuration that will be sent down to FW
   2138 * @autoneg_enabled: autonegotiation is enabled or not
   2139 * @autoneg_changed: will there a change in autonegotiation
   2140 * @netdev: network interface device structure
   2141 *
   2142 * Setup PHY autonegotiation feature
   2143 */
   2144static int
   2145ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
   2146		  struct ice_aqc_set_phy_cfg_data *config,
   2147		  u8 autoneg_enabled, u8 *autoneg_changed,
   2148		  struct net_device *netdev)
   2149{
   2150	int err = 0;
   2151
   2152	*autoneg_changed = 0;
   2153
   2154	/* Check autoneg */
   2155	if (autoneg_enabled == AUTONEG_ENABLE) {
   2156		/* If autoneg was not already enabled */
   2157		if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
   2158			/* If autoneg is not supported, return error */
   2159			if (!ethtool_link_ksettings_test_link_mode(ks,
   2160								   supported,
   2161								   Autoneg)) {
   2162				netdev_info(netdev, "Autoneg not supported on this phy.\n");
   2163				err = -EINVAL;
   2164			} else {
   2165				/* Autoneg is allowed to change */
   2166				config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
   2167				*autoneg_changed = 1;
   2168			}
   2169		}
   2170	} else {
   2171		/* If autoneg is currently enabled */
   2172		if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
   2173			/* If autoneg is supported 10GBASE_T is the only PHY
   2174			 * that can disable it, so otherwise return error
   2175			 */
   2176			if (ethtool_link_ksettings_test_link_mode(ks,
   2177								  supported,
   2178								  Autoneg)) {
   2179				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
   2180				err = -EINVAL;
   2181			} else {
   2182				/* Autoneg is allowed to change */
   2183				config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
   2184				*autoneg_changed = 1;
   2185			}
   2186		}
   2187	}
   2188
   2189	return err;
   2190}
   2191
   2192/**
   2193 * ice_set_phy_type_from_speed - set phy_types based on speeds
   2194 * and advertised modes
   2195 * @ks: ethtool link ksettings struct
   2196 * @phy_type_low: pointer to the lower part of phy_type
   2197 * @phy_type_high: pointer to the higher part of phy_type
   2198 * @adv_link_speed: targeted link speeds bitmap
   2199 */
   2200static void
   2201ice_set_phy_type_from_speed(const struct ethtool_link_ksettings *ks,
   2202			    u64 *phy_type_low, u64 *phy_type_high,
   2203			    u16 adv_link_speed)
   2204{
   2205	/* Handle 1000M speed in a special way because ice_update_phy_type
   2206	 * enables all link modes, but having mixed copper and optical
   2207	 * standards is not supported.
   2208	 */
   2209	adv_link_speed &= ~ICE_AQ_LINK_SPEED_1000MB;
   2210
   2211	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2212						  1000baseT_Full))
   2213		*phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_T |
   2214				 ICE_PHY_TYPE_LOW_1G_SGMII;
   2215
   2216	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2217						  1000baseKX_Full))
   2218		*phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_KX;
   2219
   2220	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
   2221						  1000baseX_Full))
   2222		*phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_SX |
   2223				 ICE_PHY_TYPE_LOW_1000BASE_LX;
   2224
   2225	ice_update_phy_type(phy_type_low, phy_type_high, adv_link_speed);
   2226}
   2227
   2228/**
   2229 * ice_set_link_ksettings - Set Speed and Duplex
   2230 * @netdev: network interface device structure
   2231 * @ks: ethtool ksettings
   2232 *
   2233 * Set speed/duplex per media_types advertised/forced
   2234 */
   2235static int
   2236ice_set_link_ksettings(struct net_device *netdev,
   2237		       const struct ethtool_link_ksettings *ks)
   2238{
   2239	struct ice_netdev_priv *np = netdev_priv(netdev);
   2240	u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT;
   2241	struct ethtool_link_ksettings copy_ks = *ks;
   2242	struct ethtool_link_ksettings safe_ks = {};
   2243	struct ice_aqc_get_phy_caps_data *phy_caps;
   2244	struct ice_aqc_set_phy_cfg_data config;
   2245	u16 adv_link_speed, curr_link_speed;
   2246	struct ice_pf *pf = np->vsi->back;
   2247	struct ice_port_info *pi;
   2248	u8 autoneg_changed = 0;
   2249	u64 phy_type_high = 0;
   2250	u64 phy_type_low = 0;
   2251	bool linkup;
   2252	int err;
   2253
   2254	pi = np->vsi->port_info;
   2255
   2256	if (!pi)
   2257		return -EIO;
   2258
   2259	if (pi->phy.media_type != ICE_MEDIA_BASET &&
   2260	    pi->phy.media_type != ICE_MEDIA_FIBER &&
   2261	    pi->phy.media_type != ICE_MEDIA_BACKPLANE &&
   2262	    pi->phy.media_type != ICE_MEDIA_DA &&
   2263	    pi->phy.link_info.link_info & ICE_AQ_LINK_UP)
   2264		return -EOPNOTSUPP;
   2265
   2266	phy_caps = kzalloc(sizeof(*phy_caps), GFP_KERNEL);
   2267	if (!phy_caps)
   2268		return -ENOMEM;
   2269
   2270	/* Get the PHY capabilities based on media */
   2271	if (ice_fw_supports_report_dflt_cfg(pi->hw))
   2272		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
   2273					  phy_caps, NULL);
   2274	else
   2275		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
   2276					  phy_caps, NULL);
   2277	if (err)
   2278		goto done;
   2279
   2280	/* save autoneg out of ksettings */
   2281	autoneg = copy_ks.base.autoneg;
   2282
   2283	/* Get link modes supported by hardware.*/
   2284	ice_phy_type_to_ethtool(netdev, &safe_ks);
   2285
   2286	/* and check against modes requested by user.
   2287	 * Return an error if unsupported mode was set.
   2288	 */
   2289	if (!bitmap_subset(copy_ks.link_modes.advertising,
   2290			   safe_ks.link_modes.supported,
   2291			   __ETHTOOL_LINK_MODE_MASK_NBITS)) {
   2292		if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags))
   2293			netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
   2294		err = -EOPNOTSUPP;
   2295		goto done;
   2296	}
   2297
   2298	/* get our own copy of the bits to check against */
   2299	memset(&safe_ks, 0, sizeof(safe_ks));
   2300	safe_ks.base.cmd = copy_ks.base.cmd;
   2301	safe_ks.base.link_mode_masks_nwords =
   2302		copy_ks.base.link_mode_masks_nwords;
   2303	ice_get_link_ksettings(netdev, &safe_ks);
   2304
   2305	/* set autoneg back to what it currently is */
   2306	copy_ks.base.autoneg = safe_ks.base.autoneg;
   2307	/* we don't compare the speed */
   2308	copy_ks.base.speed = safe_ks.base.speed;
   2309
   2310	/* If copy_ks.base and safe_ks.base are not the same now, then they are
   2311	 * trying to set something that we do not support.
   2312	 */
   2313	if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base))) {
   2314		err = -EOPNOTSUPP;
   2315		goto done;
   2316	}
   2317
   2318	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
   2319		timeout--;
   2320		if (!timeout) {
   2321			err = -EBUSY;
   2322			goto done;
   2323		}
   2324		usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
   2325	}
   2326
   2327	/* Copy the current user PHY configuration. The current user PHY
   2328	 * configuration is initialized during probe from PHY capabilities
   2329	 * software mode, and updated on set PHY configuration.
   2330	 */
   2331	config = pi->phy.curr_user_phy_cfg;
   2332
   2333	config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
   2334
   2335	/* Check autoneg */
   2336	err = ice_setup_autoneg(pi, &safe_ks, &config, autoneg, &autoneg_changed,
   2337				netdev);
   2338
   2339	if (err)
   2340		goto done;
   2341
   2342	/* Call to get the current link speed */
   2343	pi->phy.get_link_info = true;
   2344	err = ice_get_link_status(pi, &linkup);
   2345	if (err)
   2346		goto done;
   2347
   2348	curr_link_speed = pi->phy.curr_user_speed_req;
   2349	adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
   2350
   2351	/* If speed didn't get set, set it to what it currently is.
   2352	 * This is needed because if advertise is 0 (as it is when autoneg
   2353	 * is disabled) then speed won't get set.
   2354	 */
   2355	if (!adv_link_speed)
   2356		adv_link_speed = curr_link_speed;
   2357
   2358	/* Convert the advertise link speeds to their corresponded PHY_TYPE */
   2359	ice_set_phy_type_from_speed(ks, &phy_type_low, &phy_type_high,
   2360				    adv_link_speed);
   2361
   2362	if (!autoneg_changed && adv_link_speed == curr_link_speed) {
   2363		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
   2364		goto done;
   2365	}
   2366
   2367	/* save the requested speeds */
   2368	pi->phy.link_info.req_speeds = adv_link_speed;
   2369
   2370	/* set link and auto negotiation so changes take effect */
   2371	config.caps |= ICE_AQ_PHY_ENA_LINK;
   2372
   2373	/* check if there is a PHY type for the requested advertised speed */
   2374	if (!(phy_type_low || phy_type_high)) {
   2375		netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
   2376		err = -EOPNOTSUPP;
   2377		goto done;
   2378	}
   2379
   2380	/* intersect requested advertised speed PHY types with media PHY types
   2381	 * for set PHY configuration
   2382	 */
   2383	config.phy_type_high = cpu_to_le64(phy_type_high) &
   2384			phy_caps->phy_type_high;
   2385	config.phy_type_low = cpu_to_le64(phy_type_low) &
   2386			phy_caps->phy_type_low;
   2387
   2388	if (!(config.phy_type_high || config.phy_type_low)) {
   2389		/* If there is no intersection and lenient mode is enabled, then
   2390		 * intersect the requested advertised speed with NVM media type
   2391		 * PHY types.
   2392		 */
   2393		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
   2394			config.phy_type_high = cpu_to_le64(phy_type_high) &
   2395					       pf->nvm_phy_type_hi;
   2396			config.phy_type_low = cpu_to_le64(phy_type_low) &
   2397					      pf->nvm_phy_type_lo;
   2398		} else {
   2399			netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
   2400			err = -EOPNOTSUPP;
   2401			goto done;
   2402		}
   2403	}
   2404
   2405	/* If link is up put link down */
   2406	if (pi->phy.link_info.link_info & ICE_AQ_LINK_UP) {
   2407		/* Tell the OS link is going down, the link will go
   2408		 * back up when fw says it is ready asynchronously
   2409		 */
   2410		ice_print_link_msg(np->vsi, false);
   2411		netif_carrier_off(netdev);
   2412		netif_tx_stop_all_queues(netdev);
   2413	}
   2414
   2415	/* make the aq call */
   2416	err = ice_aq_set_phy_cfg(&pf->hw, pi, &config, NULL);
   2417	if (err) {
   2418		netdev_info(netdev, "Set phy config failed,\n");
   2419		goto done;
   2420	}
   2421
   2422	/* Save speed request */
   2423	pi->phy.curr_user_speed_req = adv_link_speed;
   2424done:
   2425	kfree(phy_caps);
   2426	clear_bit(ICE_CFG_BUSY, pf->state);
   2427
   2428	return err;
   2429}
   2430
   2431/**
   2432 * ice_parse_hdrs - parses headers from RSS hash input
   2433 * @nfc: ethtool rxnfc command
   2434 *
   2435 * This function parses the rxnfc command and returns intended
   2436 * header types for RSS configuration
   2437 */
   2438static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc)
   2439{
   2440	u32 hdrs = ICE_FLOW_SEG_HDR_NONE;
   2441
   2442	switch (nfc->flow_type) {
   2443	case TCP_V4_FLOW:
   2444		hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4;
   2445		break;
   2446	case UDP_V4_FLOW:
   2447		hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4;
   2448		break;
   2449	case SCTP_V4_FLOW:
   2450		hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4;
   2451		break;
   2452	case TCP_V6_FLOW:
   2453		hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6;
   2454		break;
   2455	case UDP_V6_FLOW:
   2456		hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6;
   2457		break;
   2458	case SCTP_V6_FLOW:
   2459		hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6;
   2460		break;
   2461	default:
   2462		break;
   2463	}
   2464	return hdrs;
   2465}
   2466
   2467#define ICE_FLOW_HASH_FLD_IPV4_SA	BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)
   2468#define ICE_FLOW_HASH_FLD_IPV6_SA	BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)
   2469#define ICE_FLOW_HASH_FLD_IPV4_DA	BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)
   2470#define ICE_FLOW_HASH_FLD_IPV6_DA	BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)
   2471#define ICE_FLOW_HASH_FLD_TCP_SRC_PORT	BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)
   2472#define ICE_FLOW_HASH_FLD_TCP_DST_PORT	BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)
   2473#define ICE_FLOW_HASH_FLD_UDP_SRC_PORT	BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)
   2474#define ICE_FLOW_HASH_FLD_UDP_DST_PORT	BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)
   2475#define ICE_FLOW_HASH_FLD_SCTP_SRC_PORT	\
   2476	BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)
   2477#define ICE_FLOW_HASH_FLD_SCTP_DST_PORT	\
   2478	BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)
   2479
   2480/**
   2481 * ice_parse_hash_flds - parses hash fields from RSS hash input
   2482 * @nfc: ethtool rxnfc command
   2483 *
   2484 * This function parses the rxnfc command and returns intended
   2485 * hash fields for RSS configuration
   2486 */
   2487static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc)
   2488{
   2489	u64 hfld = ICE_HASH_INVALID;
   2490
   2491	if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) {
   2492		switch (nfc->flow_type) {
   2493		case TCP_V4_FLOW:
   2494		case UDP_V4_FLOW:
   2495		case SCTP_V4_FLOW:
   2496			if (nfc->data & RXH_IP_SRC)
   2497				hfld |= ICE_FLOW_HASH_FLD_IPV4_SA;
   2498			if (nfc->data & RXH_IP_DST)
   2499				hfld |= ICE_FLOW_HASH_FLD_IPV4_DA;
   2500			break;
   2501		case TCP_V6_FLOW:
   2502		case UDP_V6_FLOW:
   2503		case SCTP_V6_FLOW:
   2504			if (nfc->data & RXH_IP_SRC)
   2505				hfld |= ICE_FLOW_HASH_FLD_IPV6_SA;
   2506			if (nfc->data & RXH_IP_DST)
   2507				hfld |= ICE_FLOW_HASH_FLD_IPV6_DA;
   2508			break;
   2509		default:
   2510			break;
   2511		}
   2512	}
   2513
   2514	if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) {
   2515		switch (nfc->flow_type) {
   2516		case TCP_V4_FLOW:
   2517		case TCP_V6_FLOW:
   2518			if (nfc->data & RXH_L4_B_0_1)
   2519				hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT;
   2520			if (nfc->data & RXH_L4_B_2_3)
   2521				hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT;
   2522			break;
   2523		case UDP_V4_FLOW:
   2524		case UDP_V6_FLOW:
   2525			if (nfc->data & RXH_L4_B_0_1)
   2526				hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT;
   2527			if (nfc->data & RXH_L4_B_2_3)
   2528				hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT;
   2529			break;
   2530		case SCTP_V4_FLOW:
   2531		case SCTP_V6_FLOW:
   2532			if (nfc->data & RXH_L4_B_0_1)
   2533				hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT;
   2534			if (nfc->data & RXH_L4_B_2_3)
   2535				hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT;
   2536			break;
   2537		default:
   2538			break;
   2539		}
   2540	}
   2541
   2542	return hfld;
   2543}
   2544
   2545/**
   2546 * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash
   2547 * @vsi: the VSI being configured
   2548 * @nfc: ethtool rxnfc command
   2549 *
   2550 * Returns Success if the flow input set is supported.
   2551 */
   2552static int
   2553ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
   2554{
   2555	struct ice_pf *pf = vsi->back;
   2556	struct device *dev;
   2557	u64 hashed_flds;
   2558	int status;
   2559	u32 hdrs;
   2560
   2561	dev = ice_pf_to_dev(pf);
   2562	if (ice_is_safe_mode(pf)) {
   2563		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
   2564			vsi->vsi_num);
   2565		return -EINVAL;
   2566	}
   2567
   2568	hashed_flds = ice_parse_hash_flds(nfc);
   2569	if (hashed_flds == ICE_HASH_INVALID) {
   2570		dev_dbg(dev, "Invalid hash fields, vsi num = %d\n",
   2571			vsi->vsi_num);
   2572		return -EINVAL;
   2573	}
   2574
   2575	hdrs = ice_parse_hdrs(nfc);
   2576	if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
   2577		dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
   2578			vsi->vsi_num);
   2579		return -EINVAL;
   2580	}
   2581
   2582	status = ice_add_rss_cfg(&pf->hw, vsi->idx, hashed_flds, hdrs);
   2583	if (status) {
   2584		dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %d\n",
   2585			vsi->vsi_num, status);
   2586		return status;
   2587	}
   2588
   2589	return 0;
   2590}
   2591
   2592/**
   2593 * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type
   2594 * @vsi: the VSI being configured
   2595 * @nfc: ethtool rxnfc command
   2596 */
   2597static void
   2598ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
   2599{
   2600	struct ice_pf *pf = vsi->back;
   2601	struct device *dev;
   2602	u64 hash_flds;
   2603	u32 hdrs;
   2604
   2605	dev = ice_pf_to_dev(pf);
   2606
   2607	nfc->data = 0;
   2608	if (ice_is_safe_mode(pf)) {
   2609		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
   2610			vsi->vsi_num);
   2611		return;
   2612	}
   2613
   2614	hdrs = ice_parse_hdrs(nfc);
   2615	if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
   2616		dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
   2617			vsi->vsi_num);
   2618		return;
   2619	}
   2620
   2621	hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs);
   2622	if (hash_flds == ICE_HASH_INVALID) {
   2623		dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n",
   2624			vsi->vsi_num);
   2625		return;
   2626	}
   2627
   2628	if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA ||
   2629	    hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA)
   2630		nfc->data |= (u64)RXH_IP_SRC;
   2631
   2632	if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA ||
   2633	    hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA)
   2634		nfc->data |= (u64)RXH_IP_DST;
   2635
   2636	if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT ||
   2637	    hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT ||
   2638	    hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT)
   2639		nfc->data |= (u64)RXH_L4_B_0_1;
   2640
   2641	if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT ||
   2642	    hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT ||
   2643	    hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)
   2644		nfc->data |= (u64)RXH_L4_B_2_3;
   2645}
   2646
   2647/**
   2648 * ice_set_rxnfc - command to set Rx flow rules.
   2649 * @netdev: network interface device structure
   2650 * @cmd: ethtool rxnfc command
   2651 *
   2652 * Returns 0 for success and negative values for errors
   2653 */
   2654static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
   2655{
   2656	struct ice_netdev_priv *np = netdev_priv(netdev);
   2657	struct ice_vsi *vsi = np->vsi;
   2658
   2659	switch (cmd->cmd) {
   2660	case ETHTOOL_SRXCLSRLINS:
   2661		return ice_add_fdir_ethtool(vsi, cmd);
   2662	case ETHTOOL_SRXCLSRLDEL:
   2663		return ice_del_fdir_ethtool(vsi, cmd);
   2664	case ETHTOOL_SRXFH:
   2665		return ice_set_rss_hash_opt(vsi, cmd);
   2666	default:
   2667		break;
   2668	}
   2669	return -EOPNOTSUPP;
   2670}
   2671
   2672/**
   2673 * ice_get_rxnfc - command to get Rx flow classification rules
   2674 * @netdev: network interface device structure
   2675 * @cmd: ethtool rxnfc command
   2676 * @rule_locs: buffer to rturn Rx flow classification rules
   2677 *
   2678 * Returns Success if the command is supported.
   2679 */
   2680static int
   2681ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
   2682	      u32 __always_unused *rule_locs)
   2683{
   2684	struct ice_netdev_priv *np = netdev_priv(netdev);
   2685	struct ice_vsi *vsi = np->vsi;
   2686	int ret = -EOPNOTSUPP;
   2687	struct ice_hw *hw;
   2688
   2689	hw = &vsi->back->hw;
   2690
   2691	switch (cmd->cmd) {
   2692	case ETHTOOL_GRXRINGS:
   2693		cmd->data = vsi->rss_size;
   2694		ret = 0;
   2695		break;
   2696	case ETHTOOL_GRXCLSRLCNT:
   2697		cmd->rule_cnt = hw->fdir_active_fltr;
   2698		/* report total rule count */
   2699		cmd->data = ice_get_fdir_cnt_all(hw);
   2700		ret = 0;
   2701		break;
   2702	case ETHTOOL_GRXCLSRULE:
   2703		ret = ice_get_ethtool_fdir_entry(hw, cmd);
   2704		break;
   2705	case ETHTOOL_GRXCLSRLALL:
   2706		ret = ice_get_fdir_fltr_ids(hw, cmd, (u32 *)rule_locs);
   2707		break;
   2708	case ETHTOOL_GRXFH:
   2709		ice_get_rss_hash_opt(vsi, cmd);
   2710		ret = 0;
   2711		break;
   2712	default:
   2713		break;
   2714	}
   2715
   2716	return ret;
   2717}
   2718
   2719static void
   2720ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
   2721		  struct kernel_ethtool_ringparam *kernel_ring,
   2722		  struct netlink_ext_ack *extack)
   2723{
   2724	struct ice_netdev_priv *np = netdev_priv(netdev);
   2725	struct ice_vsi *vsi = np->vsi;
   2726
   2727	ring->rx_max_pending = ICE_MAX_NUM_DESC;
   2728	ring->tx_max_pending = ICE_MAX_NUM_DESC;
   2729	ring->rx_pending = vsi->rx_rings[0]->count;
   2730	ring->tx_pending = vsi->tx_rings[0]->count;
   2731
   2732	/* Rx mini and jumbo rings are not supported */
   2733	ring->rx_mini_max_pending = 0;
   2734	ring->rx_jumbo_max_pending = 0;
   2735	ring->rx_mini_pending = 0;
   2736	ring->rx_jumbo_pending = 0;
   2737}
   2738
   2739static int
   2740ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
   2741		  struct kernel_ethtool_ringparam *kernel_ring,
   2742		  struct netlink_ext_ack *extack)
   2743{
   2744	struct ice_netdev_priv *np = netdev_priv(netdev);
   2745	struct ice_tx_ring *xdp_rings = NULL;
   2746	struct ice_tx_ring *tx_rings = NULL;
   2747	struct ice_rx_ring *rx_rings = NULL;
   2748	struct ice_vsi *vsi = np->vsi;
   2749	struct ice_pf *pf = vsi->back;
   2750	int i, timeout = 50, err = 0;
   2751	u16 new_rx_cnt, new_tx_cnt;
   2752
   2753	if (ring->tx_pending > ICE_MAX_NUM_DESC ||
   2754	    ring->tx_pending < ICE_MIN_NUM_DESC ||
   2755	    ring->rx_pending > ICE_MAX_NUM_DESC ||
   2756	    ring->rx_pending < ICE_MIN_NUM_DESC) {
   2757		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
   2758			   ring->tx_pending, ring->rx_pending,
   2759			   ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
   2760			   ICE_REQ_DESC_MULTIPLE);
   2761		return -EINVAL;
   2762	}
   2763
   2764	new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
   2765	if (new_tx_cnt != ring->tx_pending)
   2766		netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
   2767			    new_tx_cnt);
   2768	new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
   2769	if (new_rx_cnt != ring->rx_pending)
   2770		netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
   2771			    new_rx_cnt);
   2772
   2773	/* if nothing to do return success */
   2774	if (new_tx_cnt == vsi->tx_rings[0]->count &&
   2775	    new_rx_cnt == vsi->rx_rings[0]->count) {
   2776		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
   2777		return 0;
   2778	}
   2779
   2780	/* If there is a AF_XDP UMEM attached to any of Rx rings,
   2781	 * disallow changing the number of descriptors -- regardless
   2782	 * if the netdev is running or not.
   2783	 */
   2784	if (ice_xsk_any_rx_ring_ena(vsi))
   2785		return -EBUSY;
   2786
   2787	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
   2788		timeout--;
   2789		if (!timeout)
   2790			return -EBUSY;
   2791		usleep_range(1000, 2000);
   2792	}
   2793
   2794	/* set for the next time the netdev is started */
   2795	if (!netif_running(vsi->netdev)) {
   2796		ice_for_each_alloc_txq(vsi, i)
   2797			vsi->tx_rings[i]->count = new_tx_cnt;
   2798		ice_for_each_alloc_rxq(vsi, i)
   2799			vsi->rx_rings[i]->count = new_rx_cnt;
   2800		if (ice_is_xdp_ena_vsi(vsi))
   2801			ice_for_each_xdp_txq(vsi, i)
   2802				vsi->xdp_rings[i]->count = new_tx_cnt;
   2803		vsi->num_tx_desc = (u16)new_tx_cnt;
   2804		vsi->num_rx_desc = (u16)new_rx_cnt;
   2805		netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
   2806		goto done;
   2807	}
   2808
   2809	if (new_tx_cnt == vsi->tx_rings[0]->count)
   2810		goto process_rx;
   2811
   2812	/* alloc updated Tx resources */
   2813	netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
   2814		    vsi->tx_rings[0]->count, new_tx_cnt);
   2815
   2816	tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
   2817	if (!tx_rings) {
   2818		err = -ENOMEM;
   2819		goto done;
   2820	}
   2821
   2822	ice_for_each_txq(vsi, i) {
   2823		/* clone ring and setup updated count */
   2824		tx_rings[i] = *vsi->tx_rings[i];
   2825		tx_rings[i].count = new_tx_cnt;
   2826		tx_rings[i].desc = NULL;
   2827		tx_rings[i].tx_buf = NULL;
   2828		err = ice_setup_tx_ring(&tx_rings[i]);
   2829		if (err) {
   2830			while (i--)
   2831				ice_clean_tx_ring(&tx_rings[i]);
   2832			kfree(tx_rings);
   2833			goto done;
   2834		}
   2835	}
   2836
   2837	if (!ice_is_xdp_ena_vsi(vsi))
   2838		goto process_rx;
   2839
   2840	/* alloc updated XDP resources */
   2841	netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
   2842		    vsi->xdp_rings[0]->count, new_tx_cnt);
   2843
   2844	xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
   2845	if (!xdp_rings) {
   2846		err = -ENOMEM;
   2847		goto free_tx;
   2848	}
   2849
   2850	ice_for_each_xdp_txq(vsi, i) {
   2851		/* clone ring and setup updated count */
   2852		xdp_rings[i] = *vsi->xdp_rings[i];
   2853		xdp_rings[i].count = new_tx_cnt;
   2854		xdp_rings[i].next_dd = ICE_RING_QUARTER(&xdp_rings[i]) - 1;
   2855		xdp_rings[i].next_rs = ICE_RING_QUARTER(&xdp_rings[i]) - 1;
   2856		xdp_rings[i].desc = NULL;
   2857		xdp_rings[i].tx_buf = NULL;
   2858		err = ice_setup_tx_ring(&xdp_rings[i]);
   2859		if (err) {
   2860			while (i--)
   2861				ice_clean_tx_ring(&xdp_rings[i]);
   2862			kfree(xdp_rings);
   2863			goto free_tx;
   2864		}
   2865		ice_set_ring_xdp(&xdp_rings[i]);
   2866	}
   2867
   2868process_rx:
   2869	if (new_rx_cnt == vsi->rx_rings[0]->count)
   2870		goto process_link;
   2871
   2872	/* alloc updated Rx resources */
   2873	netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
   2874		    vsi->rx_rings[0]->count, new_rx_cnt);
   2875
   2876	rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
   2877	if (!rx_rings) {
   2878		err = -ENOMEM;
   2879		goto done;
   2880	}
   2881
   2882	ice_for_each_rxq(vsi, i) {
   2883		/* clone ring and setup updated count */
   2884		rx_rings[i] = *vsi->rx_rings[i];
   2885		rx_rings[i].count = new_rx_cnt;
   2886		rx_rings[i].desc = NULL;
   2887		rx_rings[i].rx_buf = NULL;
   2888		/* this is to allow wr32 to have something to write to
   2889		 * during early allocation of Rx buffers
   2890		 */
   2891		rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
   2892
   2893		err = ice_setup_rx_ring(&rx_rings[i]);
   2894		if (err)
   2895			goto rx_unwind;
   2896
   2897		/* allocate Rx buffers */
   2898		err = ice_alloc_rx_bufs(&rx_rings[i],
   2899					ICE_DESC_UNUSED(&rx_rings[i]));
   2900rx_unwind:
   2901		if (err) {
   2902			while (i) {
   2903				i--;
   2904				ice_free_rx_ring(&rx_rings[i]);
   2905			}
   2906			kfree(rx_rings);
   2907			err = -ENOMEM;
   2908			goto free_tx;
   2909		}
   2910	}
   2911
   2912process_link:
   2913	/* Bring interface down, copy in the new ring info, then restore the
   2914	 * interface. if VSI is up, bring it down and then back up
   2915	 */
   2916	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
   2917		ice_down(vsi);
   2918
   2919		if (tx_rings) {
   2920			ice_for_each_txq(vsi, i) {
   2921				ice_free_tx_ring(vsi->tx_rings[i]);
   2922				*vsi->tx_rings[i] = tx_rings[i];
   2923			}
   2924			kfree(tx_rings);
   2925		}
   2926
   2927		if (rx_rings) {
   2928			ice_for_each_rxq(vsi, i) {
   2929				ice_free_rx_ring(vsi->rx_rings[i]);
   2930				/* copy the real tail offset */
   2931				rx_rings[i].tail = vsi->rx_rings[i]->tail;
   2932				/* this is to fake out the allocation routine
   2933				 * into thinking it has to realloc everything
   2934				 * but the recycling logic will let us re-use
   2935				 * the buffers allocated above
   2936				 */
   2937				rx_rings[i].next_to_use = 0;
   2938				rx_rings[i].next_to_clean = 0;
   2939				rx_rings[i].next_to_alloc = 0;
   2940				*vsi->rx_rings[i] = rx_rings[i];
   2941			}
   2942			kfree(rx_rings);
   2943		}
   2944
   2945		if (xdp_rings) {
   2946			ice_for_each_xdp_txq(vsi, i) {
   2947				ice_free_tx_ring(vsi->xdp_rings[i]);
   2948				*vsi->xdp_rings[i] = xdp_rings[i];
   2949			}
   2950			kfree(xdp_rings);
   2951		}
   2952
   2953		vsi->num_tx_desc = new_tx_cnt;
   2954		vsi->num_rx_desc = new_rx_cnt;
   2955		ice_up(vsi);
   2956	}
   2957	goto done;
   2958
   2959free_tx:
   2960	/* error cleanup if the Rx allocations failed after getting Tx */
   2961	if (tx_rings) {
   2962		ice_for_each_txq(vsi, i)
   2963			ice_free_tx_ring(&tx_rings[i]);
   2964		kfree(tx_rings);
   2965	}
   2966
   2967done:
   2968	clear_bit(ICE_CFG_BUSY, pf->state);
   2969	return err;
   2970}
   2971
   2972/**
   2973 * ice_get_pauseparam - Get Flow Control status
   2974 * @netdev: network interface device structure
   2975 * @pause: ethernet pause (flow control) parameters
   2976 *
   2977 * Get requested flow control status from PHY capability.
   2978 * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
   2979 * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
   2980 * the negotiated Rx/Tx pause via lp_advertising.
   2981 */
   2982static void
   2983ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
   2984{
   2985	struct ice_netdev_priv *np = netdev_priv(netdev);
   2986	struct ice_port_info *pi = np->vsi->port_info;
   2987	struct ice_aqc_get_phy_caps_data *pcaps;
   2988	struct ice_dcbx_cfg *dcbx_cfg;
   2989	int status;
   2990
   2991	/* Initialize pause params */
   2992	pause->rx_pause = 0;
   2993	pause->tx_pause = 0;
   2994
   2995	dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
   2996
   2997	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   2998	if (!pcaps)
   2999		return;
   3000
   3001	/* Get current PHY config */
   3002	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
   3003				     NULL);
   3004	if (status)
   3005		goto out;
   3006
   3007	pause->autoneg = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
   3008							     AUTONEG_DISABLE;
   3009
   3010	if (dcbx_cfg->pfc.pfcena)
   3011		/* PFC enabled so report LFC as off */
   3012		goto out;
   3013
   3014	if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
   3015		pause->tx_pause = 1;
   3016	if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
   3017		pause->rx_pause = 1;
   3018
   3019out:
   3020	kfree(pcaps);
   3021}
   3022
   3023/**
   3024 * ice_set_pauseparam - Set Flow Control parameter
   3025 * @netdev: network interface device structure
   3026 * @pause: return Tx/Rx flow control status
   3027 */
   3028static int
   3029ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
   3030{
   3031	struct ice_netdev_priv *np = netdev_priv(netdev);
   3032	struct ice_aqc_get_phy_caps_data *pcaps;
   3033	struct ice_link_status *hw_link_info;
   3034	struct ice_pf *pf = np->vsi->back;
   3035	struct ice_dcbx_cfg *dcbx_cfg;
   3036	struct ice_vsi *vsi = np->vsi;
   3037	struct ice_hw *hw = &pf->hw;
   3038	struct ice_port_info *pi;
   3039	u8 aq_failures;
   3040	bool link_up;
   3041	u32 is_an;
   3042	int err;
   3043
   3044	pi = vsi->port_info;
   3045	hw_link_info = &pi->phy.link_info;
   3046	dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
   3047	link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
   3048
   3049	/* Changing the port's flow control is not supported if this isn't the
   3050	 * PF VSI
   3051	 */
   3052	if (vsi->type != ICE_VSI_PF) {
   3053		netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
   3054		return -EOPNOTSUPP;
   3055	}
   3056
   3057	/* Get pause param reports configured and negotiated flow control pause
   3058	 * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
   3059	 * defined get pause param pause->autoneg reports SW configured setting,
   3060	 * so compare pause->autoneg with SW configured to prevent the user from
   3061	 * using set pause param to chance autoneg.
   3062	 */
   3063	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
   3064	if (!pcaps)
   3065		return -ENOMEM;
   3066
   3067	/* Get current PHY config */
   3068	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
   3069				  NULL);
   3070	if (err) {
   3071		kfree(pcaps);
   3072		return err;
   3073	}
   3074
   3075	is_an = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
   3076						    AUTONEG_DISABLE;
   3077
   3078	kfree(pcaps);
   3079
   3080	if (pause->autoneg != is_an) {
   3081		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
   3082		return -EOPNOTSUPP;
   3083	}
   3084
   3085	/* If we have link and don't have autoneg */
   3086	if (!test_bit(ICE_DOWN, pf->state) &&
   3087	    !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
   3088		/* Send message that it might not necessarily work*/
   3089		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
   3090	}
   3091
   3092	if (dcbx_cfg->pfc.pfcena) {
   3093		netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
   3094		return -EOPNOTSUPP;
   3095	}
   3096	if (pause->rx_pause && pause->tx_pause)
   3097		pi->fc.req_mode = ICE_FC_FULL;
   3098	else if (pause->rx_pause && !pause->tx_pause)
   3099		pi->fc.req_mode = ICE_FC_RX_PAUSE;
   3100	else if (!pause->rx_pause && pause->tx_pause)
   3101		pi->fc.req_mode = ICE_FC_TX_PAUSE;
   3102	else if (!pause->rx_pause && !pause->tx_pause)
   3103		pi->fc.req_mode = ICE_FC_NONE;
   3104	else
   3105		return -EINVAL;
   3106
   3107	/* Set the FC mode and only restart AN if link is up */
   3108	err = ice_set_fc(pi, &aq_failures, link_up);
   3109
   3110	if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
   3111		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %s\n",
   3112			    err, ice_aq_str(hw->adminq.sq_last_status));
   3113		err = -EAGAIN;
   3114	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
   3115		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %s\n",
   3116			    err, ice_aq_str(hw->adminq.sq_last_status));
   3117		err = -EAGAIN;
   3118	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
   3119		netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %s\n",
   3120			    err, ice_aq_str(hw->adminq.sq_last_status));
   3121		err = -EAGAIN;
   3122	}
   3123
   3124	return err;
   3125}
   3126
   3127/**
   3128 * ice_get_rxfh_key_size - get the RSS hash key size
   3129 * @netdev: network interface device structure
   3130 *
   3131 * Returns the table size.
   3132 */
   3133static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
   3134{
   3135	return ICE_VSIQF_HKEY_ARRAY_SIZE;
   3136}
   3137
   3138/**
   3139 * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
   3140 * @netdev: network interface device structure
   3141 *
   3142 * Returns the table size.
   3143 */
   3144static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
   3145{
   3146	struct ice_netdev_priv *np = netdev_priv(netdev);
   3147
   3148	return np->vsi->rss_table_size;
   3149}
   3150
   3151static int
   3152ice_get_rxfh_context(struct net_device *netdev, u32 *indir,
   3153		     u8 *key, u8 *hfunc, u32 rss_context)
   3154{
   3155	struct ice_netdev_priv *np = netdev_priv(netdev);
   3156	struct ice_vsi *vsi = np->vsi;
   3157	struct ice_pf *pf = vsi->back;
   3158	u16 qcount, offset;
   3159	int err, num_tc, i;
   3160	u8 *lut;
   3161
   3162	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
   3163		netdev_warn(netdev, "RSS is not supported on this VSI!\n");
   3164		return -EOPNOTSUPP;
   3165	}
   3166
   3167	if (rss_context && !ice_is_adq_active(pf)) {
   3168		netdev_err(netdev, "RSS context cannot be non-zero when ADQ is not configured.\n");
   3169		return -EINVAL;
   3170	}
   3171
   3172	qcount = vsi->mqprio_qopt.qopt.count[rss_context];
   3173	offset = vsi->mqprio_qopt.qopt.offset[rss_context];
   3174
   3175	if (rss_context && ice_is_adq_active(pf)) {
   3176		num_tc = vsi->mqprio_qopt.qopt.num_tc;
   3177		if (rss_context >= num_tc) {
   3178			netdev_err(netdev, "RSS context:%d  > num_tc:%d\n",
   3179				   rss_context, num_tc);
   3180			return -EINVAL;
   3181		}
   3182		/* Use channel VSI of given TC */
   3183		vsi = vsi->tc_map_vsi[rss_context];
   3184	}
   3185
   3186	if (hfunc)
   3187		*hfunc = ETH_RSS_HASH_TOP;
   3188
   3189	if (!indir)
   3190		return 0;
   3191
   3192	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
   3193	if (!lut)
   3194		return -ENOMEM;
   3195
   3196	err = ice_get_rss_key(vsi, key);
   3197	if (err)
   3198		goto out;
   3199
   3200	err = ice_get_rss_lut(vsi, lut, vsi->rss_table_size);
   3201	if (err)
   3202		goto out;
   3203
   3204	if (ice_is_adq_active(pf)) {
   3205		for (i = 0; i < vsi->rss_table_size; i++)
   3206			indir[i] = offset + lut[i] % qcount;
   3207		goto out;
   3208	}
   3209
   3210	for (i = 0; i < vsi->rss_table_size; i++)
   3211		indir[i] = lut[i];
   3212
   3213out:
   3214	kfree(lut);
   3215	return err;
   3216}
   3217
   3218/**
   3219 * ice_get_rxfh - get the Rx flow hash indirection table
   3220 * @netdev: network interface device structure
   3221 * @indir: indirection table
   3222 * @key: hash key
   3223 * @hfunc: hash function
   3224 *
   3225 * Reads the indirection table directly from the hardware.
   3226 */
   3227static int
   3228ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
   3229{
   3230	return ice_get_rxfh_context(netdev, indir, key, hfunc, 0);
   3231}
   3232
   3233/**
   3234 * ice_set_rxfh - set the Rx flow hash indirection table
   3235 * @netdev: network interface device structure
   3236 * @indir: indirection table
   3237 * @key: hash key
   3238 * @hfunc: hash function
   3239 *
   3240 * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
   3241 * returns 0 after programming the table.
   3242 */
   3243static int
   3244ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
   3245	     const u8 hfunc)
   3246{
   3247	struct ice_netdev_priv *np = netdev_priv(netdev);
   3248	struct ice_vsi *vsi = np->vsi;
   3249	struct ice_pf *pf = vsi->back;
   3250	struct device *dev;
   3251	int err;
   3252
   3253	dev = ice_pf_to_dev(pf);
   3254	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
   3255		return -EOPNOTSUPP;
   3256
   3257	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
   3258		/* RSS not supported return error here */
   3259		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
   3260		return -EIO;
   3261	}
   3262
   3263	if (ice_is_adq_active(pf)) {
   3264		netdev_err(netdev, "Cannot change RSS params with ADQ configured.\n");
   3265		return -EOPNOTSUPP;
   3266	}
   3267
   3268	if (key) {
   3269		if (!vsi->rss_hkey_user) {
   3270			vsi->rss_hkey_user =
   3271				devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
   3272					     GFP_KERNEL);
   3273			if (!vsi->rss_hkey_user)
   3274				return -ENOMEM;
   3275		}
   3276		memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
   3277
   3278		err = ice_set_rss_key(vsi, vsi->rss_hkey_user);
   3279		if (err)
   3280			return err;
   3281	}
   3282
   3283	if (!vsi->rss_lut_user) {
   3284		vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
   3285						 GFP_KERNEL);
   3286		if (!vsi->rss_lut_user)
   3287			return -ENOMEM;
   3288	}
   3289
   3290	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
   3291	if (indir) {
   3292		int i;
   3293
   3294		for (i = 0; i < vsi->rss_table_size; i++)
   3295			vsi->rss_lut_user[i] = (u8)(indir[i]);
   3296	} else {
   3297		ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
   3298				 vsi->rss_size);
   3299	}
   3300
   3301	err = ice_set_rss_lut(vsi, vsi->rss_lut_user, vsi->rss_table_size);
   3302	if (err)
   3303		return err;
   3304
   3305	return 0;
   3306}
   3307
   3308static int
   3309ice_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
   3310{
   3311	struct ice_pf *pf = ice_netdev_to_pf(dev);
   3312
   3313	/* only report timestamping if PTP is enabled */
   3314	if (!test_bit(ICE_FLAG_PTP, pf->flags))
   3315		return ethtool_op_get_ts_info(dev, info);
   3316
   3317	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
   3318				SOF_TIMESTAMPING_RX_SOFTWARE |
   3319				SOF_TIMESTAMPING_SOFTWARE |
   3320				SOF_TIMESTAMPING_TX_HARDWARE |
   3321				SOF_TIMESTAMPING_RX_HARDWARE |
   3322				SOF_TIMESTAMPING_RAW_HARDWARE;
   3323
   3324	info->phc_index = ice_get_ptp_clock_index(pf);
   3325
   3326	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
   3327
   3328	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
   3329
   3330	return 0;
   3331}
   3332
   3333/**
   3334 * ice_get_max_txq - return the maximum number of Tx queues for in a PF
   3335 * @pf: PF structure
   3336 */
   3337static int ice_get_max_txq(struct ice_pf *pf)
   3338{
   3339	return min3(pf->num_lan_msix, (u16)num_online_cpus(),
   3340		    (u16)pf->hw.func_caps.common_cap.num_txq);
   3341}
   3342
   3343/**
   3344 * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
   3345 * @pf: PF structure
   3346 */
   3347static int ice_get_max_rxq(struct ice_pf *pf)
   3348{
   3349	return min3(pf->num_lan_msix, (u16)num_online_cpus(),
   3350		    (u16)pf->hw.func_caps.common_cap.num_rxq);
   3351}
   3352
   3353/**
   3354 * ice_get_combined_cnt - return the current number of combined channels
   3355 * @vsi: PF VSI pointer
   3356 *
   3357 * Go through all queue vectors and count ones that have both Rx and Tx ring
   3358 * attached
   3359 */
   3360static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
   3361{
   3362	u32 combined = 0;
   3363	int q_idx;
   3364
   3365	ice_for_each_q_vector(vsi, q_idx) {
   3366		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
   3367
   3368		if (q_vector->rx.rx_ring && q_vector->tx.tx_ring)
   3369			combined++;
   3370	}
   3371
   3372	return combined;
   3373}
   3374
   3375/**
   3376 * ice_get_channels - get the current and max supported channels
   3377 * @dev: network interface device structure
   3378 * @ch: ethtool channel data structure
   3379 */
   3380static void
   3381ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
   3382{
   3383	struct ice_netdev_priv *np = netdev_priv(dev);
   3384	struct ice_vsi *vsi = np->vsi;
   3385	struct ice_pf *pf = vsi->back;
   3386
   3387	/* report maximum channels */
   3388	ch->max_rx = ice_get_max_rxq(pf);
   3389	ch->max_tx = ice_get_max_txq(pf);
   3390	ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
   3391
   3392	/* report current channels */
   3393	ch->combined_count = ice_get_combined_cnt(vsi);
   3394	ch->rx_count = vsi->num_rxq - ch->combined_count;
   3395	ch->tx_count = vsi->num_txq - ch->combined_count;
   3396
   3397	/* report other queues */
   3398	ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
   3399	ch->max_other = ch->other_count;
   3400}
   3401
   3402/**
   3403 * ice_get_valid_rss_size - return valid number of RSS queues
   3404 * @hw: pointer to the HW structure
   3405 * @new_size: requested RSS queues
   3406 */
   3407static int ice_get_valid_rss_size(struct ice_hw *hw, int new_size)
   3408{
   3409	struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
   3410
   3411	return min_t(int, new_size, BIT(caps->rss_table_entry_width));
   3412}
   3413
   3414/**
   3415 * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
   3416 * @vsi: VSI to reconfigure RSS LUT on
   3417 * @req_rss_size: requested range of queue numbers for hashing
   3418 *
   3419 * Set the VSI's RSS parameters, configure the RSS LUT based on these.
   3420 */
   3421static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
   3422{
   3423	struct ice_pf *pf = vsi->back;
   3424	struct device *dev;
   3425	struct ice_hw *hw;
   3426	int err;
   3427	u8 *lut;
   3428
   3429	dev = ice_pf_to_dev(pf);
   3430	hw = &pf->hw;
   3431
   3432	if (!req_rss_size)
   3433		return -EINVAL;
   3434
   3435	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
   3436	if (!lut)
   3437		return -ENOMEM;
   3438
   3439	/* set RSS LUT parameters */
   3440	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
   3441		vsi->rss_size = 1;
   3442	else
   3443		vsi->rss_size = ice_get_valid_rss_size(hw, req_rss_size);
   3444
   3445	/* create/set RSS LUT */
   3446	ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
   3447	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
   3448	if (err)
   3449		dev_err(dev, "Cannot set RSS lut, err %d aq_err %s\n", err,
   3450			ice_aq_str(hw->adminq.sq_last_status));
   3451
   3452	kfree(lut);
   3453	return err;
   3454}
   3455
   3456/**
   3457 * ice_set_channels - set the number channels
   3458 * @dev: network interface device structure
   3459 * @ch: ethtool channel data structure
   3460 */
   3461static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
   3462{
   3463	struct ice_netdev_priv *np = netdev_priv(dev);
   3464	struct ice_vsi *vsi = np->vsi;
   3465	struct ice_pf *pf = vsi->back;
   3466	int new_rx = 0, new_tx = 0;
   3467	u32 curr_combined;
   3468
   3469	/* do not support changing channels in Safe Mode */
   3470	if (ice_is_safe_mode(pf)) {
   3471		netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
   3472		return -EOPNOTSUPP;
   3473	}
   3474	/* do not support changing other_count */
   3475	if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U))
   3476		return -EINVAL;
   3477
   3478	if (ice_is_adq_active(pf)) {
   3479		netdev_err(dev, "Cannot set channels with ADQ configured.\n");
   3480		return -EOPNOTSUPP;
   3481	}
   3482
   3483	if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) {
   3484		netdev_err(dev, "Cannot set channels when Flow Director filters are active\n");
   3485		return -EOPNOTSUPP;
   3486	}
   3487
   3488	curr_combined = ice_get_combined_cnt(vsi);
   3489
   3490	/* these checks are for cases where user didn't specify a particular
   3491	 * value on cmd line but we get non-zero value anyway via
   3492	 * get_channels(); look at ethtool.c in ethtool repository (the user
   3493	 * space part), particularly, do_schannels() routine
   3494	 */
   3495	if (ch->rx_count == vsi->num_rxq - curr_combined)
   3496		ch->rx_count = 0;
   3497	if (ch->tx_count == vsi->num_txq - curr_combined)
   3498		ch->tx_count = 0;
   3499	if (ch->combined_count == curr_combined)
   3500		ch->combined_count = 0;
   3501
   3502	if (!(ch->combined_count || (ch->rx_count && ch->tx_count))) {
   3503		netdev_err(dev, "Please specify at least 1 Rx and 1 Tx channel\n");
   3504		return -EINVAL;
   3505	}
   3506
   3507	new_rx = ch->combined_count + ch->rx_count;
   3508	new_tx = ch->combined_count + ch->tx_count;
   3509
   3510	if (new_rx < vsi->tc_cfg.numtc) {
   3511		netdev_err(dev, "Cannot set less Rx channels, than Traffic Classes you have (%u)\n",
   3512			   vsi->tc_cfg.numtc);
   3513		return -EINVAL;
   3514	}
   3515	if (new_tx < vsi->tc_cfg.numtc) {
   3516		netdev_err(dev, "Cannot set less Tx channels, than Traffic Classes you have (%u)\n",
   3517			   vsi->tc_cfg.numtc);
   3518		return -EINVAL;
   3519	}
   3520	if (new_rx > ice_get_max_rxq(pf)) {
   3521		netdev_err(dev, "Maximum allowed Rx channels is %d\n",
   3522			   ice_get_max_rxq(pf));
   3523		return -EINVAL;
   3524	}
   3525	if (new_tx > ice_get_max_txq(pf)) {
   3526		netdev_err(dev, "Maximum allowed Tx channels is %d\n",
   3527			   ice_get_max_txq(pf));
   3528		return -EINVAL;
   3529	}
   3530
   3531	ice_vsi_recfg_qs(vsi, new_rx, new_tx);
   3532
   3533	if (!netif_is_rxfh_configured(dev))
   3534		return ice_vsi_set_dflt_rss_lut(vsi, new_rx);
   3535
   3536	/* Update rss_size due to change in Rx queues */
   3537	vsi->rss_size = ice_get_valid_rss_size(&pf->hw, new_rx);
   3538
   3539	return 0;
   3540}
   3541
   3542/**
   3543 * ice_get_wol - get current Wake on LAN configuration
   3544 * @netdev: network interface device structure
   3545 * @wol: Ethtool structure to retrieve WoL settings
   3546 */
   3547static void ice_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
   3548{
   3549	struct ice_netdev_priv *np = netdev_priv(netdev);
   3550	struct ice_pf *pf = np->vsi->back;
   3551
   3552	if (np->vsi->type != ICE_VSI_PF)
   3553		netdev_warn(netdev, "Wake on LAN is not supported on this interface!\n");
   3554
   3555	/* Get WoL settings based on the HW capability */
   3556	if (ice_is_wol_supported(&pf->hw)) {
   3557		wol->supported = WAKE_MAGIC;
   3558		wol->wolopts = pf->wol_ena ? WAKE_MAGIC : 0;
   3559	} else {
   3560		wol->supported = 0;
   3561		wol->wolopts = 0;
   3562	}
   3563}
   3564
   3565/**
   3566 * ice_set_wol - set Wake on LAN on supported device
   3567 * @netdev: network interface device structure
   3568 * @wol: Ethtool structure to set WoL
   3569 */
   3570static int ice_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
   3571{
   3572	struct ice_netdev_priv *np = netdev_priv(netdev);
   3573	struct ice_vsi *vsi = np->vsi;
   3574	struct ice_pf *pf = vsi->back;
   3575
   3576	if (vsi->type != ICE_VSI_PF || !ice_is_wol_supported(&pf->hw))
   3577		return -EOPNOTSUPP;
   3578
   3579	/* only magic packet is supported */
   3580	if (wol->wolopts && wol->wolopts != WAKE_MAGIC)
   3581		return -EOPNOTSUPP;
   3582
   3583	/* Set WoL only if there is a new value */
   3584	if (pf->wol_ena != !!wol->wolopts) {
   3585		pf->wol_ena = !!wol->wolopts;
   3586		device_set_wakeup_enable(ice_pf_to_dev(pf), pf->wol_ena);
   3587		netdev_dbg(netdev, "WoL magic packet %sabled\n",
   3588			   pf->wol_ena ? "en" : "dis");
   3589	}
   3590
   3591	return 0;
   3592}
   3593
   3594/**
   3595 * ice_get_rc_coalesce - get ITR values for specific ring container
   3596 * @ec: ethtool structure to fill with driver's coalesce settings
   3597 * @rc: ring container that the ITR values will come from
   3598 *
   3599 * Query the device for ice_ring_container specific ITR values. This is
   3600 * done per ice_ring_container because each q_vector can have 1 or more rings
   3601 * and all of said ring(s) will have the same ITR values.
   3602 *
   3603 * Returns 0 on success, negative otherwise.
   3604 */
   3605static int
   3606ice_get_rc_coalesce(struct ethtool_coalesce *ec, struct ice_ring_container *rc)
   3607{
   3608	if (!rc->rx_ring)
   3609		return -EINVAL;
   3610
   3611	switch (rc->type) {
   3612	case ICE_RX_CONTAINER:
   3613		ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc);
   3614		ec->rx_coalesce_usecs = rc->itr_setting;
   3615		ec->rx_coalesce_usecs_high = rc->rx_ring->q_vector->intrl;
   3616		break;
   3617	case ICE_TX_CONTAINER:
   3618		ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc);
   3619		ec->tx_coalesce_usecs = rc->itr_setting;
   3620		break;
   3621	default:
   3622		dev_dbg(ice_pf_to_dev(rc->rx_ring->vsi->back), "Invalid c_type %d\n", rc->type);
   3623		return -EINVAL;
   3624	}
   3625
   3626	return 0;
   3627}
   3628
   3629/**
   3630 * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
   3631 * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
   3632 * @ec: coalesce settings to program the device with
   3633 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
   3634 *
   3635 * Return 0 on success, and negative under the following conditions:
   3636 * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
   3637 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
   3638 */
   3639static int
   3640ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
   3641{
   3642	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
   3643		if (ice_get_rc_coalesce(ec,
   3644					&vsi->rx_rings[q_num]->q_vector->rx))
   3645			return -EINVAL;
   3646		if (ice_get_rc_coalesce(ec,
   3647					&vsi->tx_rings[q_num]->q_vector->tx))
   3648			return -EINVAL;
   3649	} else if (q_num < vsi->num_rxq) {
   3650		if (ice_get_rc_coalesce(ec,
   3651					&vsi->rx_rings[q_num]->q_vector->rx))
   3652			return -EINVAL;
   3653	} else if (q_num < vsi->num_txq) {
   3654		if (ice_get_rc_coalesce(ec,
   3655					&vsi->tx_rings[q_num]->q_vector->tx))
   3656			return -EINVAL;
   3657	} else {
   3658		return -EINVAL;
   3659	}
   3660
   3661	return 0;
   3662}
   3663
   3664/**
   3665 * __ice_get_coalesce - get ITR/INTRL values for the device
   3666 * @netdev: pointer to the netdev associated with this query
   3667 * @ec: ethtool structure to fill with driver's coalesce settings
   3668 * @q_num: queue number to get the coalesce settings for
   3669 *
   3670 * If the caller passes in a negative q_num then we return coalesce settings
   3671 * based on queue number 0, else use the actual q_num passed in.
   3672 */
   3673static int
   3674__ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
   3675		   int q_num)
   3676{
   3677	struct ice_netdev_priv *np = netdev_priv(netdev);
   3678	struct ice_vsi *vsi = np->vsi;
   3679
   3680	if (q_num < 0)
   3681		q_num = 0;
   3682
   3683	if (ice_get_q_coalesce(vsi, ec, q_num))
   3684		return -EINVAL;
   3685
   3686	return 0;
   3687}
   3688
   3689static int ice_get_coalesce(struct net_device *netdev,
   3690			    struct ethtool_coalesce *ec,
   3691			    struct kernel_ethtool_coalesce *kernel_coal,
   3692			    struct netlink_ext_ack *extack)
   3693{
   3694	return __ice_get_coalesce(netdev, ec, -1);
   3695}
   3696
   3697static int
   3698ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
   3699		       struct ethtool_coalesce *ec)
   3700{
   3701	return __ice_get_coalesce(netdev, ec, q_num);
   3702}
   3703
   3704/**
   3705 * ice_set_rc_coalesce - set ITR values for specific ring container
   3706 * @ec: ethtool structure from user to update ITR settings
   3707 * @rc: ring container that the ITR values will come from
   3708 * @vsi: VSI associated to the ring container
   3709 *
   3710 * Set specific ITR values. This is done per ice_ring_container because each
   3711 * q_vector can have 1 or more rings and all of said ring(s) will have the same
   3712 * ITR values.
   3713 *
   3714 * Returns 0 on success, negative otherwise.
   3715 */
   3716static int
   3717ice_set_rc_coalesce(struct ethtool_coalesce *ec,
   3718		    struct ice_ring_container *rc, struct ice_vsi *vsi)
   3719{
   3720	const char *c_type_str = (rc->type == ICE_RX_CONTAINER) ? "rx" : "tx";
   3721	u32 use_adaptive_coalesce, coalesce_usecs;
   3722	struct ice_pf *pf = vsi->back;
   3723	u16 itr_setting;
   3724
   3725	if (!rc->rx_ring)
   3726		return -EINVAL;
   3727
   3728	switch (rc->type) {
   3729	case ICE_RX_CONTAINER:
   3730	{
   3731		struct ice_q_vector *q_vector = rc->rx_ring->q_vector;
   3732
   3733		if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
   3734		    (ec->rx_coalesce_usecs_high &&
   3735		     ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
   3736			netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
   3737				    c_type_str, pf->hw.intrl_gran,
   3738				    ICE_MAX_INTRL);
   3739			return -EINVAL;
   3740		}
   3741		if (ec->rx_coalesce_usecs_high != q_vector->intrl &&
   3742		    (ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce)) {
   3743			netdev_info(vsi->netdev, "Invalid value, %s-usecs-high cannot be changed if adaptive-tx or adaptive-rx is enabled\n",
   3744				    c_type_str);
   3745			return -EINVAL;
   3746		}
   3747		if (ec->rx_coalesce_usecs_high != q_vector->intrl)
   3748			q_vector->intrl = ec->rx_coalesce_usecs_high;
   3749
   3750		use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
   3751		coalesce_usecs = ec->rx_coalesce_usecs;
   3752
   3753		break;
   3754	}
   3755	case ICE_TX_CONTAINER:
   3756		use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
   3757		coalesce_usecs = ec->tx_coalesce_usecs;
   3758
   3759		break;
   3760	default:
   3761		dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
   3762			rc->type);
   3763		return -EINVAL;
   3764	}
   3765
   3766	itr_setting = rc->itr_setting;
   3767	if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
   3768		netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
   3769			    c_type_str, c_type_str);
   3770		return -EINVAL;
   3771	}
   3772
   3773	if (coalesce_usecs > ICE_ITR_MAX) {
   3774		netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
   3775			    c_type_str, ICE_ITR_MAX);
   3776		return -EINVAL;
   3777	}
   3778
   3779	if (use_adaptive_coalesce) {
   3780		rc->itr_mode = ITR_DYNAMIC;
   3781	} else {
   3782		rc->itr_mode = ITR_STATIC;
   3783		/* store user facing value how it was set */
   3784		rc->itr_setting = coalesce_usecs;
   3785		/* write the change to the register */
   3786		ice_write_itr(rc, coalesce_usecs);
   3787		/* force writes to take effect immediately, the flush shouldn't
   3788		 * be done in the functions above because the intent is for
   3789		 * them to do lazy writes.
   3790		 */
   3791		ice_flush(&pf->hw);
   3792	}
   3793
   3794	return 0;
   3795}
   3796
   3797/**
   3798 * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
   3799 * @vsi: VSI associated to the queue that need updating
   3800 * @ec: coalesce settings to program the device with
   3801 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
   3802 *
   3803 * Return 0 on success, and negative under the following conditions:
   3804 * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
   3805 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
   3806 */
   3807static int
   3808ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
   3809{
   3810	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
   3811		if (ice_set_rc_coalesce(ec,
   3812					&vsi->rx_rings[q_num]->q_vector->rx,
   3813					vsi))
   3814			return -EINVAL;
   3815
   3816		if (ice_set_rc_coalesce(ec,
   3817					&vsi->tx_rings[q_num]->q_vector->tx,
   3818					vsi))
   3819			return -EINVAL;
   3820	} else if (q_num < vsi->num_rxq) {
   3821		if (ice_set_rc_coalesce(ec,
   3822					&vsi->rx_rings[q_num]->q_vector->rx,
   3823					vsi))
   3824			return -EINVAL;
   3825	} else if (q_num < vsi->num_txq) {
   3826		if (ice_set_rc_coalesce(ec,
   3827					&vsi->tx_rings[q_num]->q_vector->tx,
   3828					vsi))
   3829			return -EINVAL;
   3830	} else {
   3831		return -EINVAL;
   3832	}
   3833
   3834	return 0;
   3835}
   3836
   3837/**
   3838 * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
   3839 * @netdev: netdev used for print
   3840 * @itr_setting: previous user setting
   3841 * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
   3842 * @coalesce_usecs: requested value of [tx|rx]-usecs
   3843 * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
   3844 */
   3845static void
   3846ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
   3847		       u32 use_adaptive_coalesce, u32 coalesce_usecs,
   3848		       const char *c_type_str)
   3849{
   3850	if (use_adaptive_coalesce)
   3851		return;
   3852
   3853	if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
   3854		netdev_info(netdev, "User set %s-usecs to %d, device only supports even values. Rounding down and attempting to set %s-usecs to %d\n",
   3855			    c_type_str, coalesce_usecs, c_type_str,
   3856			    ITR_REG_ALIGN(coalesce_usecs));
   3857}
   3858
   3859/**
   3860 * __ice_set_coalesce - set ITR/INTRL values for the device
   3861 * @netdev: pointer to the netdev associated with this query
   3862 * @ec: ethtool structure to fill with driver's coalesce settings
   3863 * @q_num: queue number to get the coalesce settings for
   3864 *
   3865 * If the caller passes in a negative q_num then we set the coalesce settings
   3866 * for all Tx/Rx queues, else use the actual q_num passed in.
   3867 */
   3868static int
   3869__ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
   3870		   int q_num)
   3871{
   3872	struct ice_netdev_priv *np = netdev_priv(netdev);
   3873	struct ice_vsi *vsi = np->vsi;
   3874
   3875	if (q_num < 0) {
   3876		struct ice_q_vector *q_vector = vsi->q_vectors[0];
   3877		int v_idx;
   3878
   3879		if (q_vector) {
   3880			ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
   3881					       ec->use_adaptive_rx_coalesce,
   3882					       ec->rx_coalesce_usecs, "rx");
   3883
   3884			ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
   3885					       ec->use_adaptive_tx_coalesce,
   3886					       ec->tx_coalesce_usecs, "tx");
   3887		}
   3888
   3889		ice_for_each_q_vector(vsi, v_idx) {
   3890			/* In some cases if DCB is configured the num_[rx|tx]q
   3891			 * can be less than vsi->num_q_vectors. This check
   3892			 * accounts for that so we don't report a false failure
   3893			 */
   3894			if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
   3895				goto set_complete;
   3896
   3897			if (ice_set_q_coalesce(vsi, ec, v_idx))
   3898				return -EINVAL;
   3899
   3900			ice_set_q_vector_intrl(vsi->q_vectors[v_idx]);
   3901		}
   3902		goto set_complete;
   3903	}
   3904
   3905	if (ice_set_q_coalesce(vsi, ec, q_num))
   3906		return -EINVAL;
   3907
   3908	ice_set_q_vector_intrl(vsi->q_vectors[q_num]);
   3909
   3910set_complete:
   3911	return 0;
   3912}
   3913
   3914static int ice_set_coalesce(struct net_device *netdev,
   3915			    struct ethtool_coalesce *ec,
   3916			    struct kernel_ethtool_coalesce *kernel_coal,
   3917			    struct netlink_ext_ack *extack)
   3918{
   3919	return __ice_set_coalesce(netdev, ec, -1);
   3920}
   3921
   3922static int
   3923ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
   3924		       struct ethtool_coalesce *ec)
   3925{
   3926	return __ice_set_coalesce(netdev, ec, q_num);
   3927}
   3928
   3929static void
   3930ice_repr_get_drvinfo(struct net_device *netdev,
   3931		     struct ethtool_drvinfo *drvinfo)
   3932{
   3933	struct ice_repr *repr = ice_netdev_to_repr(netdev);
   3934
   3935	if (ice_check_vf_ready_for_cfg(repr->vf))
   3936		return;
   3937
   3938	__ice_get_drvinfo(netdev, drvinfo, repr->src_vsi);
   3939}
   3940
   3941static void
   3942ice_repr_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
   3943{
   3944	struct ice_repr *repr = ice_netdev_to_repr(netdev);
   3945
   3946	/* for port representors only ETH_SS_STATS is supported */
   3947	if (ice_check_vf_ready_for_cfg(repr->vf) ||
   3948	    stringset != ETH_SS_STATS)
   3949		return;
   3950
   3951	__ice_get_strings(netdev, stringset, data, repr->src_vsi);
   3952}
   3953
   3954static void
   3955ice_repr_get_ethtool_stats(struct net_device *netdev,
   3956			   struct ethtool_stats __always_unused *stats,
   3957			   u64 *data)
   3958{
   3959	struct ice_repr *repr = ice_netdev_to_repr(netdev);
   3960
   3961	if (ice_check_vf_ready_for_cfg(repr->vf))
   3962		return;
   3963
   3964	__ice_get_ethtool_stats(netdev, stats, data, repr->src_vsi);
   3965}
   3966
   3967static int ice_repr_get_sset_count(struct net_device *netdev, int sset)
   3968{
   3969	switch (sset) {
   3970	case ETH_SS_STATS:
   3971		return ICE_VSI_STATS_LEN;
   3972	default:
   3973		return -EOPNOTSUPP;
   3974	}
   3975}
   3976
   3977#define ICE_I2C_EEPROM_DEV_ADDR		0xA0
   3978#define ICE_I2C_EEPROM_DEV_ADDR2	0xA2
   3979#define ICE_MODULE_TYPE_SFP		0x03
   3980#define ICE_MODULE_TYPE_QSFP_PLUS	0x0D
   3981#define ICE_MODULE_TYPE_QSFP28		0x11
   3982#define ICE_MODULE_SFF_ADDR_MODE	0x04
   3983#define ICE_MODULE_SFF_DIAG_CAPAB	0x40
   3984#define ICE_MODULE_REVISION_ADDR	0x01
   3985#define ICE_MODULE_SFF_8472_COMP	0x5E
   3986#define ICE_MODULE_SFF_8472_SWAP	0x5C
   3987#define ICE_MODULE_QSFP_MAX_LEN		640
   3988
   3989/**
   3990 * ice_get_module_info - get SFF module type and revision information
   3991 * @netdev: network interface device structure
   3992 * @modinfo: module EEPROM size and layout information structure
   3993 */
   3994static int
   3995ice_get_module_info(struct net_device *netdev,
   3996		    struct ethtool_modinfo *modinfo)
   3997{
   3998	struct ice_netdev_priv *np = netdev_priv(netdev);
   3999	struct ice_vsi *vsi = np->vsi;
   4000	struct ice_pf *pf = vsi->back;
   4001	struct ice_hw *hw = &pf->hw;
   4002	u8 sff8472_comp = 0;
   4003	u8 sff8472_swap = 0;
   4004	u8 sff8636_rev = 0;
   4005	u8 value = 0;
   4006	int status;
   4007
   4008	status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
   4009				   0, &value, 1, 0, NULL);
   4010	if (status)
   4011		return status;
   4012
   4013	switch (value) {
   4014	case ICE_MODULE_TYPE_SFP:
   4015		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
   4016					   ICE_MODULE_SFF_8472_COMP, 0x00, 0,
   4017					   &sff8472_comp, 1, 0, NULL);
   4018		if (status)
   4019			return status;
   4020		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
   4021					   ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
   4022					   &sff8472_swap, 1, 0, NULL);
   4023		if (status)
   4024			return status;
   4025
   4026		if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
   4027			modinfo->type = ETH_MODULE_SFF_8079;
   4028			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
   4029		} else if (sff8472_comp &&
   4030			   (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
   4031			modinfo->type = ETH_MODULE_SFF_8472;
   4032			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
   4033		} else {
   4034			modinfo->type = ETH_MODULE_SFF_8079;
   4035			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
   4036		}
   4037		break;
   4038	case ICE_MODULE_TYPE_QSFP_PLUS:
   4039	case ICE_MODULE_TYPE_QSFP28:
   4040		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
   4041					   ICE_MODULE_REVISION_ADDR, 0x00, 0,
   4042					   &sff8636_rev, 1, 0, NULL);
   4043		if (status)
   4044			return status;
   4045		/* Check revision compliance */
   4046		if (sff8636_rev > 0x02) {
   4047			/* Module is SFF-8636 compliant */
   4048			modinfo->type = ETH_MODULE_SFF_8636;
   4049			modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
   4050		} else {
   4051			modinfo->type = ETH_MODULE_SFF_8436;
   4052			modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
   4053		}
   4054		break;
   4055	default:
   4056		netdev_warn(netdev, "SFF Module Type not recognized.\n");
   4057		return -EINVAL;
   4058	}
   4059	return 0;
   4060}
   4061
   4062/**
   4063 * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
   4064 * @netdev: network interface device structure
   4065 * @ee: EEPROM dump request structure
   4066 * @data: buffer to be filled with EEPROM contents
   4067 */
   4068static int
   4069ice_get_module_eeprom(struct net_device *netdev,
   4070		      struct ethtool_eeprom *ee, u8 *data)
   4071{
   4072	struct ice_netdev_priv *np = netdev_priv(netdev);
   4073#define SFF_READ_BLOCK_SIZE 8
   4074	u8 value[SFF_READ_BLOCK_SIZE] = { 0 };
   4075	u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
   4076	struct ice_vsi *vsi = np->vsi;
   4077	struct ice_pf *pf = vsi->back;
   4078	struct ice_hw *hw = &pf->hw;
   4079	bool is_sfp = false;
   4080	unsigned int i, j;
   4081	u16 offset = 0;
   4082	u8 page = 0;
   4083	int status;
   4084
   4085	if (!ee || !ee->len || !data)
   4086		return -EINVAL;
   4087
   4088	status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, value, 1, 0,
   4089				   NULL);
   4090	if (status)
   4091		return status;
   4092
   4093	if (value[0] == ICE_MODULE_TYPE_SFP)
   4094		is_sfp = true;
   4095
   4096	memset(data, 0, ee->len);
   4097	for (i = 0; i < ee->len; i += SFF_READ_BLOCK_SIZE) {
   4098		offset = i + ee->offset;
   4099		page = 0;
   4100
   4101		/* Check if we need to access the other memory page */
   4102		if (is_sfp) {
   4103			if (offset >= ETH_MODULE_SFF_8079_LEN) {
   4104				offset -= ETH_MODULE_SFF_8079_LEN;
   4105				addr = ICE_I2C_EEPROM_DEV_ADDR2;
   4106			}
   4107		} else {
   4108			while (offset >= ETH_MODULE_SFF_8436_LEN) {
   4109				/* Compute memory page number and offset. */
   4110				offset -= ETH_MODULE_SFF_8436_LEN / 2;
   4111				page++;
   4112			}
   4113		}
   4114
   4115		/* Bit 2 of EEPROM address 0x02 declares upper
   4116		 * pages are disabled on QSFP modules.
   4117		 * SFP modules only ever use page 0.
   4118		 */
   4119		if (page == 0 || !(data[0x2] & 0x4)) {
   4120			/* If i2c bus is busy due to slow page change or
   4121			 * link management access, call can fail. This is normal.
   4122			 * So we retry this a few times.
   4123			 */
   4124			for (j = 0; j < 4; j++) {
   4125				status = ice_aq_sff_eeprom(hw, 0, addr, offset, page,
   4126							   !is_sfp, value,
   4127							   SFF_READ_BLOCK_SIZE,
   4128							   0, NULL);
   4129				netdev_dbg(netdev, "SFF %02X %02X %02X %X = %02X%02X%02X%02X.%02X%02X%02X%02X (%X)\n",
   4130					   addr, offset, page, is_sfp,
   4131					   value[0], value[1], value[2], value[3],
   4132					   value[4], value[5], value[6], value[7],
   4133					   status);
   4134				if (status) {
   4135					usleep_range(1500, 2500);
   4136					memset(value, 0, SFF_READ_BLOCK_SIZE);
   4137					continue;
   4138				}
   4139				break;
   4140			}
   4141
   4142			/* Make sure we have enough room for the new block */
   4143			if ((i + SFF_READ_BLOCK_SIZE) < ee->len)
   4144				memcpy(data + i, value, SFF_READ_BLOCK_SIZE);
   4145		}
   4146	}
   4147	return 0;
   4148}
   4149
   4150static const struct ethtool_ops ice_ethtool_ops = {
   4151	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
   4152				     ETHTOOL_COALESCE_USE_ADAPTIVE |
   4153				     ETHTOOL_COALESCE_RX_USECS_HIGH,
   4154	.get_link_ksettings	= ice_get_link_ksettings,
   4155	.set_link_ksettings	= ice_set_link_ksettings,
   4156	.get_drvinfo		= ice_get_drvinfo,
   4157	.get_regs_len		= ice_get_regs_len,
   4158	.get_regs		= ice_get_regs,
   4159	.get_wol		= ice_get_wol,
   4160	.set_wol		= ice_set_wol,
   4161	.get_msglevel		= ice_get_msglevel,
   4162	.set_msglevel		= ice_set_msglevel,
   4163	.self_test		= ice_self_test,
   4164	.get_link		= ethtool_op_get_link,
   4165	.get_eeprom_len		= ice_get_eeprom_len,
   4166	.get_eeprom		= ice_get_eeprom,
   4167	.get_coalesce		= ice_get_coalesce,
   4168	.set_coalesce		= ice_set_coalesce,
   4169	.get_strings		= ice_get_strings,
   4170	.set_phys_id		= ice_set_phys_id,
   4171	.get_ethtool_stats      = ice_get_ethtool_stats,
   4172	.get_priv_flags		= ice_get_priv_flags,
   4173	.set_priv_flags		= ice_set_priv_flags,
   4174	.get_sset_count		= ice_get_sset_count,
   4175	.get_rxnfc		= ice_get_rxnfc,
   4176	.set_rxnfc		= ice_set_rxnfc,
   4177	.get_ringparam		= ice_get_ringparam,
   4178	.set_ringparam		= ice_set_ringparam,
   4179	.nway_reset		= ice_nway_reset,
   4180	.get_pauseparam		= ice_get_pauseparam,
   4181	.set_pauseparam		= ice_set_pauseparam,
   4182	.get_rxfh_key_size	= ice_get_rxfh_key_size,
   4183	.get_rxfh_indir_size	= ice_get_rxfh_indir_size,
   4184	.get_rxfh_context	= ice_get_rxfh_context,
   4185	.get_rxfh		= ice_get_rxfh,
   4186	.set_rxfh		= ice_set_rxfh,
   4187	.get_channels		= ice_get_channels,
   4188	.set_channels		= ice_set_channels,
   4189	.get_ts_info		= ice_get_ts_info,
   4190	.get_per_queue_coalesce	= ice_get_per_q_coalesce,
   4191	.set_per_queue_coalesce	= ice_set_per_q_coalesce,
   4192	.get_fecparam		= ice_get_fecparam,
   4193	.set_fecparam		= ice_set_fecparam,
   4194	.get_module_info	= ice_get_module_info,
   4195	.get_module_eeprom	= ice_get_module_eeprom,
   4196};
   4197
   4198static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
   4199	.get_link_ksettings	= ice_get_link_ksettings,
   4200	.set_link_ksettings	= ice_set_link_ksettings,
   4201	.get_drvinfo		= ice_get_drvinfo,
   4202	.get_regs_len		= ice_get_regs_len,
   4203	.get_regs		= ice_get_regs,
   4204	.get_wol		= ice_get_wol,
   4205	.set_wol		= ice_set_wol,
   4206	.get_msglevel		= ice_get_msglevel,
   4207	.set_msglevel		= ice_set_msglevel,
   4208	.get_link		= ethtool_op_get_link,
   4209	.get_eeprom_len		= ice_get_eeprom_len,
   4210	.get_eeprom		= ice_get_eeprom,
   4211	.get_strings		= ice_get_strings,
   4212	.get_ethtool_stats	= ice_get_ethtool_stats,
   4213	.get_sset_count		= ice_get_sset_count,
   4214	.get_ringparam		= ice_get_ringparam,
   4215	.set_ringparam		= ice_set_ringparam,
   4216	.nway_reset		= ice_nway_reset,
   4217	.get_channels		= ice_get_channels,
   4218};
   4219
   4220/**
   4221 * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
   4222 * @netdev: network interface device structure
   4223 */
   4224void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
   4225{
   4226	netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
   4227}
   4228
   4229static const struct ethtool_ops ice_ethtool_repr_ops = {
   4230	.get_drvinfo		= ice_repr_get_drvinfo,
   4231	.get_link		= ethtool_op_get_link,
   4232	.get_strings		= ice_repr_get_strings,
   4233	.get_ethtool_stats      = ice_repr_get_ethtool_stats,
   4234	.get_sset_count		= ice_repr_get_sset_count,
   4235};
   4236
   4237/**
   4238 * ice_set_ethtool_repr_ops - setup VF's port representor ethtool ops
   4239 * @netdev: network interface device structure
   4240 */
   4241void ice_set_ethtool_repr_ops(struct net_device *netdev)
   4242{
   4243	netdev->ethtool_ops = &ice_ethtool_repr_ops;
   4244}
   4245
   4246/**
   4247 * ice_set_ethtool_ops - setup netdev ethtool ops
   4248 * @netdev: network interface device structure
   4249 *
   4250 * setup netdev ethtool ops with ice specific ops
   4251 */
   4252void ice_set_ethtool_ops(struct net_device *netdev)
   4253{
   4254	netdev->ethtool_ops = &ice_ethtool_ops;
   4255}