Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1/*******************************************************************************
   2 *
   3 * Intel Ethernet Controller XL710 Family Linux Driver
   4 * Copyright(c) 2013 - 2016 Intel Corporation.
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms and conditions of the GNU General Public License,
   8 * version 2, as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13 * more details.
  14 *
  15 * You should have received a copy of the GNU General Public License along
  16 * with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 *
  18 * The full GNU General Public License is included in this distribution in
  19 * the file called "COPYING".
  20 *
  21 * Contact Information:
  22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24 *
  25 ******************************************************************************/
  26
  27/* ethtool support for i40e */
  28
  29#include "i40e.h"
  30#include "i40e_diag.h"
  31
  32struct i40e_stats {
  33	char stat_string[ETH_GSTRING_LEN];
  34	int sizeof_stat;
  35	int stat_offset;
  36};
  37
  38#define I40E_STAT(_type, _name, _stat) { \
  39	.stat_string = _name, \
  40	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
  41	.stat_offset = offsetof(_type, _stat) \
  42}
  43
  44#define I40E_NETDEV_STAT(_net_stat) \
  45		I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
  46#define I40E_PF_STAT(_name, _stat) \
  47		I40E_STAT(struct i40e_pf, _name, _stat)
  48#define I40E_VSI_STAT(_name, _stat) \
  49		I40E_STAT(struct i40e_vsi, _name, _stat)
  50#define I40E_VEB_STAT(_name, _stat) \
  51		I40E_STAT(struct i40e_veb, _name, _stat)
  52
  53static const struct i40e_stats i40e_gstrings_net_stats[] = {
  54	I40E_NETDEV_STAT(rx_packets),
  55	I40E_NETDEV_STAT(tx_packets),
  56	I40E_NETDEV_STAT(rx_bytes),
  57	I40E_NETDEV_STAT(tx_bytes),
  58	I40E_NETDEV_STAT(rx_errors),
  59	I40E_NETDEV_STAT(tx_errors),
  60	I40E_NETDEV_STAT(rx_dropped),
  61	I40E_NETDEV_STAT(tx_dropped),
  62	I40E_NETDEV_STAT(collisions),
  63	I40E_NETDEV_STAT(rx_length_errors),
  64	I40E_NETDEV_STAT(rx_crc_errors),
  65};
  66
  67static const struct i40e_stats i40e_gstrings_veb_stats[] = {
  68	I40E_VEB_STAT("rx_bytes", stats.rx_bytes),
  69	I40E_VEB_STAT("tx_bytes", stats.tx_bytes),
  70	I40E_VEB_STAT("rx_unicast", stats.rx_unicast),
  71	I40E_VEB_STAT("tx_unicast", stats.tx_unicast),
  72	I40E_VEB_STAT("rx_multicast", stats.rx_multicast),
  73	I40E_VEB_STAT("tx_multicast", stats.tx_multicast),
  74	I40E_VEB_STAT("rx_broadcast", stats.rx_broadcast),
  75	I40E_VEB_STAT("tx_broadcast", stats.tx_broadcast),
  76	I40E_VEB_STAT("rx_discards", stats.rx_discards),
  77	I40E_VEB_STAT("tx_discards", stats.tx_discards),
  78	I40E_VEB_STAT("tx_errors", stats.tx_errors),
  79	I40E_VEB_STAT("rx_unknown_protocol", stats.rx_unknown_protocol),
  80};
  81
  82static const struct i40e_stats i40e_gstrings_misc_stats[] = {
  83	I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
  84	I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
  85	I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
  86	I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
  87	I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
  88	I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
  89	I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
  90	I40E_VSI_STAT("tx_linearize", tx_linearize),
  91	I40E_VSI_STAT("tx_force_wb", tx_force_wb),
  92	I40E_VSI_STAT("tx_lost_interrupt", tx_lost_interrupt),
  93	I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
  94	I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
  95};
  96
  97/* These PF_STATs might look like duplicates of some NETDEV_STATs,
  98 * but they are separate.  This device supports Virtualization, and
  99 * as such might have several netdevs supporting VMDq and FCoE going
 100 * through a single port.  The NETDEV_STATs are for individual netdevs
 101 * seen at the top of the stack, and the PF_STATs are for the physical
 102 * function at the bottom of the stack hosting those netdevs.
 103 *
 104 * The PF_STATs are appended to the netdev stats only when ethtool -S
 105 * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
 106 */
 107static struct i40e_stats i40e_gstrings_stats[] = {
 108	I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
 109	I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
 110	I40E_PF_STAT("rx_unicast", stats.eth.rx_unicast),
 111	I40E_PF_STAT("tx_unicast", stats.eth.tx_unicast),
 112	I40E_PF_STAT("rx_multicast", stats.eth.rx_multicast),
 113	I40E_PF_STAT("tx_multicast", stats.eth.tx_multicast),
 114	I40E_PF_STAT("rx_broadcast", stats.eth.rx_broadcast),
 115	I40E_PF_STAT("tx_broadcast", stats.eth.tx_broadcast),
 116	I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
 117	I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
 118	I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
 119	I40E_PF_STAT("rx_crc_errors", stats.crc_errors),
 120	I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
 121	I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
 122	I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
 123	I40E_PF_STAT("tx_timeout", tx_timeout_count),
 124	I40E_PF_STAT("rx_csum_bad", hw_csum_rx_error),
 125	I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
 126	I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
 127	I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
 128	I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
 129	I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
 130	I40E_PF_STAT("rx_size_64", stats.rx_size_64),
 131	I40E_PF_STAT("rx_size_127", stats.rx_size_127),
 132	I40E_PF_STAT("rx_size_255", stats.rx_size_255),
 133	I40E_PF_STAT("rx_size_511", stats.rx_size_511),
 134	I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
 135	I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
 136	I40E_PF_STAT("rx_size_big", stats.rx_size_big),
 137	I40E_PF_STAT("tx_size_64", stats.tx_size_64),
 138	I40E_PF_STAT("tx_size_127", stats.tx_size_127),
 139	I40E_PF_STAT("tx_size_255", stats.tx_size_255),
 140	I40E_PF_STAT("tx_size_511", stats.tx_size_511),
 141	I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
 142	I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
 143	I40E_PF_STAT("tx_size_big", stats.tx_size_big),
 144	I40E_PF_STAT("rx_undersize", stats.rx_undersize),
 145	I40E_PF_STAT("rx_fragments", stats.rx_fragments),
 146	I40E_PF_STAT("rx_oversize", stats.rx_oversize),
 147	I40E_PF_STAT("rx_jabber", stats.rx_jabber),
 148	I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
 149	I40E_PF_STAT("arq_overflows", arq_overflows),
 150	I40E_PF_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
 151	I40E_PF_STAT("fdir_flush_cnt", fd_flush_cnt),
 152	I40E_PF_STAT("fdir_atr_match", stats.fd_atr_match),
 153	I40E_PF_STAT("fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
 154	I40E_PF_STAT("fdir_atr_status", stats.fd_atr_status),
 155	I40E_PF_STAT("fdir_sb_match", stats.fd_sb_match),
 156	I40E_PF_STAT("fdir_sb_status", stats.fd_sb_status),
 157
 158	/* LPI stats */
 159	I40E_PF_STAT("tx_lpi_status", stats.tx_lpi_status),
 160	I40E_PF_STAT("rx_lpi_status", stats.rx_lpi_status),
 161	I40E_PF_STAT("tx_lpi_count", stats.tx_lpi_count),
 162	I40E_PF_STAT("rx_lpi_count", stats.rx_lpi_count),
 163};
 164
 165#ifdef I40E_FCOE
 166static const struct i40e_stats i40e_gstrings_fcoe_stats[] = {
 167	I40E_VSI_STAT("fcoe_bad_fccrc", fcoe_stats.fcoe_bad_fccrc),
 168	I40E_VSI_STAT("rx_fcoe_dropped", fcoe_stats.rx_fcoe_dropped),
 169	I40E_VSI_STAT("rx_fcoe_packets", fcoe_stats.rx_fcoe_packets),
 170	I40E_VSI_STAT("rx_fcoe_dwords", fcoe_stats.rx_fcoe_dwords),
 171	I40E_VSI_STAT("fcoe_ddp_count", fcoe_stats.fcoe_ddp_count),
 172	I40E_VSI_STAT("fcoe_last_error", fcoe_stats.fcoe_last_error),
 173	I40E_VSI_STAT("tx_fcoe_packets", fcoe_stats.tx_fcoe_packets),
 174	I40E_VSI_STAT("tx_fcoe_dwords", fcoe_stats.tx_fcoe_dwords),
 175};
 176
 177#endif /* I40E_FCOE */
 178#define I40E_QUEUE_STATS_LEN(n) \
 179	(((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs \
 180	    * 2 /* Tx and Rx together */                                     \
 181	    * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
 182#define I40E_GLOBAL_STATS_LEN	ARRAY_SIZE(i40e_gstrings_stats)
 183#define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
 184#define I40E_MISC_STATS_LEN	ARRAY_SIZE(i40e_gstrings_misc_stats)
 185#ifdef I40E_FCOE
 186#define I40E_FCOE_STATS_LEN	ARRAY_SIZE(i40e_gstrings_fcoe_stats)
 187#define I40E_VSI_STATS_LEN(n)	(I40E_NETDEV_STATS_LEN + \
 188				 I40E_FCOE_STATS_LEN + \
 189				 I40E_MISC_STATS_LEN + \
 190				 I40E_QUEUE_STATS_LEN((n)))
 191#else
 192#define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
 193				 I40E_MISC_STATS_LEN + \
 194				 I40E_QUEUE_STATS_LEN((n)))
 195#endif /* I40E_FCOE */
 196#define I40E_PFC_STATS_LEN ( \
 197		(FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
 198		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
 199		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
 200		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
 201		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
 202		 / sizeof(u64))
 203#define I40E_VEB_TC_STATS_LEN ( \
 204		(FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_rx_packets) + \
 205		 FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_rx_bytes) + \
 206		 FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_tx_packets) + \
 207		 FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_tx_bytes)) \
 208		 / sizeof(u64))
 209#define I40E_VEB_STATS_LEN	ARRAY_SIZE(i40e_gstrings_veb_stats)
 210#define I40E_VEB_STATS_TOTAL	(I40E_VEB_STATS_LEN + I40E_VEB_TC_STATS_LEN)
 211#define I40E_PF_STATS_LEN(n)	(I40E_GLOBAL_STATS_LEN + \
 212				 I40E_PFC_STATS_LEN + \
 213				 I40E_VSI_STATS_LEN((n)))
 214
 215enum i40e_ethtool_test_id {
 216	I40E_ETH_TEST_REG = 0,
 217	I40E_ETH_TEST_EEPROM,
 218	I40E_ETH_TEST_INTR,
 219	I40E_ETH_TEST_LOOPBACK,
 220	I40E_ETH_TEST_LINK,
 221};
 222
 223static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
 224	"Register test  (offline)",
 225	"Eeprom test    (offline)",
 226	"Interrupt test (offline)",
 227	"Loopback test  (offline)",
 228	"Link test   (on/offline)"
 229};
 230
 231#define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
 232
 233static const char i40e_priv_flags_strings[][ETH_GSTRING_LEN] = {
 234	"NPAR",
 235	"LinkPolling",
 236	"flow-director-atr",
 237	"veb-stats",
 238	"packet-split",
 239	"hw-atr-eviction",
 240};
 241
 242#define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_priv_flags_strings)
 243
 244/**
 245 * i40e_partition_setting_complaint - generic complaint for MFP restriction
 246 * @pf: the PF struct
 247 **/
 248static void i40e_partition_setting_complaint(struct i40e_pf *pf)
 249{
 250	dev_info(&pf->pdev->dev,
 251		 "The link settings are allowed to be changed only from the first partition of a given port. Please switch to the first partition in order to change the setting.\n");
 252}
 253
 254/**
 255 * i40e_get_settings_link_up - Get the Link settings for when link is up
 256 * @hw: hw structure
 257 * @ecmd: ethtool command to fill in
 258 * @netdev: network interface device structure
 259 *
 260 **/
 261static void i40e_get_settings_link_up(struct i40e_hw *hw,
 262				      struct ethtool_cmd *ecmd,
 263				      struct net_device *netdev,
 264				      struct i40e_pf *pf)
 265{
 266	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
 267	u32 link_speed = hw_link_info->link_speed;
 268
 269	/* Initialize supported and advertised settings based on phy settings */
 270	switch (hw_link_info->phy_type) {
 271	case I40E_PHY_TYPE_40GBASE_CR4:
 272	case I40E_PHY_TYPE_40GBASE_CR4_CU:
 273		ecmd->supported = SUPPORTED_Autoneg |
 274				  SUPPORTED_40000baseCR4_Full;
 275		ecmd->advertising = ADVERTISED_Autoneg |
 276				    ADVERTISED_40000baseCR4_Full;
 277		break;
 278	case I40E_PHY_TYPE_XLAUI:
 279	case I40E_PHY_TYPE_XLPPI:
 280	case I40E_PHY_TYPE_40GBASE_AOC:
 281		ecmd->supported = SUPPORTED_40000baseCR4_Full;
 282		break;
 283	case I40E_PHY_TYPE_40GBASE_SR4:
 284		ecmd->supported = SUPPORTED_40000baseSR4_Full;
 285		break;
 286	case I40E_PHY_TYPE_40GBASE_LR4:
 287		ecmd->supported = SUPPORTED_40000baseLR4_Full;
 288		break;
 289	case I40E_PHY_TYPE_10GBASE_SR:
 290	case I40E_PHY_TYPE_10GBASE_LR:
 291	case I40E_PHY_TYPE_1000BASE_SX:
 292	case I40E_PHY_TYPE_1000BASE_LX:
 293		ecmd->supported = SUPPORTED_10000baseT_Full;
 294		if (hw_link_info->module_type[2] &
 295		    I40E_MODULE_TYPE_1000BASE_SX ||
 296		    hw_link_info->module_type[2] &
 297		    I40E_MODULE_TYPE_1000BASE_LX) {
 298			ecmd->supported |= SUPPORTED_1000baseT_Full;
 299			if (hw_link_info->requested_speeds &
 300			    I40E_LINK_SPEED_1GB)
 301				ecmd->advertising |= ADVERTISED_1000baseT_Full;
 302		}
 303		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
 304			ecmd->advertising |= ADVERTISED_10000baseT_Full;
 305		break;
 306	case I40E_PHY_TYPE_10GBASE_T:
 307	case I40E_PHY_TYPE_1000BASE_T:
 308		ecmd->supported = SUPPORTED_Autoneg |
 309				  SUPPORTED_10000baseT_Full |
 310				  SUPPORTED_1000baseT_Full;
 311		ecmd->advertising = ADVERTISED_Autoneg;
 312		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
 313			ecmd->advertising |= ADVERTISED_10000baseT_Full;
 314		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
 315			ecmd->advertising |= ADVERTISED_1000baseT_Full;
 316		break;
 317	case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
 318		ecmd->supported = SUPPORTED_Autoneg |
 319				  SUPPORTED_1000baseT_Full;
 320		ecmd->advertising = ADVERTISED_Autoneg |
 321				    ADVERTISED_1000baseT_Full;
 322		break;
 323	case I40E_PHY_TYPE_100BASE_TX:
 324		ecmd->supported = SUPPORTED_Autoneg |
 325				  SUPPORTED_100baseT_Full;
 326		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
 327			ecmd->advertising |= ADVERTISED_100baseT_Full;
 328		break;
 329	case I40E_PHY_TYPE_10GBASE_CR1_CU:
 330	case I40E_PHY_TYPE_10GBASE_CR1:
 331		ecmd->supported = SUPPORTED_Autoneg |
 332				  SUPPORTED_10000baseT_Full;
 333		ecmd->advertising = ADVERTISED_Autoneg |
 334				    ADVERTISED_10000baseT_Full;
 335		break;
 336	case I40E_PHY_TYPE_XAUI:
 337	case I40E_PHY_TYPE_XFI:
 338	case I40E_PHY_TYPE_SFI:
 339	case I40E_PHY_TYPE_10GBASE_SFPP_CU:
 340	case I40E_PHY_TYPE_10GBASE_AOC:
 341		ecmd->supported = SUPPORTED_10000baseT_Full;
 342		break;
 343	case I40E_PHY_TYPE_SGMII:
 344		ecmd->supported = SUPPORTED_Autoneg |
 345				  SUPPORTED_1000baseT_Full;
 346		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
 347			ecmd->advertising |= ADVERTISED_1000baseT_Full;
 348		if (pf->flags & I40E_FLAG_100M_SGMII_CAPABLE) {
 349			ecmd->supported |= SUPPORTED_100baseT_Full;
 350			if (hw_link_info->requested_speeds &
 351			    I40E_LINK_SPEED_100MB)
 352				ecmd->advertising |= ADVERTISED_100baseT_Full;
 353		}
 354		break;
 355	/* Backplane is set based on supported phy types in get_settings
 356	 * so don't set anything here but don't warn either
 357	 */
 358	case I40E_PHY_TYPE_40GBASE_KR4:
 359	case I40E_PHY_TYPE_20GBASE_KR2:
 360	case I40E_PHY_TYPE_10GBASE_KR:
 361	case I40E_PHY_TYPE_10GBASE_KX4:
 362	case I40E_PHY_TYPE_1000BASE_KX:
 363		break;
 364	default:
 365		/* if we got here and link is up something bad is afoot */
 366		netdev_info(netdev, "WARNING: Link is up but PHY type 0x%x is not recognized.\n",
 367			    hw_link_info->phy_type);
 368	}
 369
 370	/* Set speed and duplex */
 371	switch (link_speed) {
 372	case I40E_LINK_SPEED_40GB:
 373		ethtool_cmd_speed_set(ecmd, SPEED_40000);
 374		break;
 375	case I40E_LINK_SPEED_20GB:
 376		ethtool_cmd_speed_set(ecmd, SPEED_20000);
 377		break;
 378	case I40E_LINK_SPEED_10GB:
 379		ethtool_cmd_speed_set(ecmd, SPEED_10000);
 380		break;
 381	case I40E_LINK_SPEED_1GB:
 382		ethtool_cmd_speed_set(ecmd, SPEED_1000);
 383		break;
 384	case I40E_LINK_SPEED_100MB:
 385		ethtool_cmd_speed_set(ecmd, SPEED_100);
 386		break;
 387	default:
 388		break;
 389	}
 390	ecmd->duplex = DUPLEX_FULL;
 391}
 392
 393/**
 394 * i40e_get_settings_link_down - Get the Link settings for when link is down
 395 * @hw: hw structure
 396 * @ecmd: ethtool command to fill in
 397 *
 398 * Reports link settings that can be determined when link is down
 399 **/
 400static void i40e_get_settings_link_down(struct i40e_hw *hw,
 401					struct ethtool_cmd *ecmd,
 402					struct i40e_pf *pf)
 403{
 404	enum i40e_aq_capabilities_phy_type phy_types = hw->phy.phy_types;
 405
 406	/* link is down and the driver needs to fall back on
 407	 * supported phy types to figure out what info to display
 408	 */
 409	ecmd->supported = 0x0;
 410	ecmd->advertising = 0x0;
 411	if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
 412		ecmd->supported |= SUPPORTED_Autoneg |
 413				   SUPPORTED_1000baseT_Full;
 414		ecmd->advertising |= ADVERTISED_Autoneg |
 415				     ADVERTISED_1000baseT_Full;
 416		if (pf->hw.mac.type == I40E_MAC_X722) {
 417			ecmd->supported |= SUPPORTED_100baseT_Full;
 418			ecmd->advertising |= ADVERTISED_100baseT_Full;
 419			if (pf->flags & I40E_FLAG_100M_SGMII_CAPABLE) {
 420				ecmd->supported |= SUPPORTED_100baseT_Full;
 421				ecmd->advertising |= ADVERTISED_100baseT_Full;
 422			}
 423		}
 424	}
 425	if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
 426	    phy_types & I40E_CAP_PHY_TYPE_XFI ||
 427	    phy_types & I40E_CAP_PHY_TYPE_SFI ||
 428	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
 429	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC)
 430		ecmd->supported |= SUPPORTED_10000baseT_Full;
 431	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
 432	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
 433	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
 434	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
 435	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
 436		ecmd->supported |= SUPPORTED_Autoneg |
 437				   SUPPORTED_10000baseT_Full;
 438		ecmd->advertising |= ADVERTISED_Autoneg |
 439				     ADVERTISED_10000baseT_Full;
 440	}
 441	if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
 442	    phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
 443	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
 444		ecmd->supported |= SUPPORTED_40000baseCR4_Full;
 445	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
 446	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
 447		ecmd->supported |= SUPPORTED_Autoneg |
 448				  SUPPORTED_40000baseCR4_Full;
 449		ecmd->advertising |= ADVERTISED_Autoneg |
 450				    ADVERTISED_40000baseCR4_Full;
 451	}
 452	if ((phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) &&
 453	    !(phy_types & I40E_CAP_PHY_TYPE_1000BASE_T)) {
 454		ecmd->supported |= SUPPORTED_Autoneg |
 455				   SUPPORTED_100baseT_Full;
 456		ecmd->advertising |= ADVERTISED_Autoneg |
 457				     ADVERTISED_100baseT_Full;
 458	}
 459	if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
 460	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
 461	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
 462	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
 463		ecmd->supported |= SUPPORTED_Autoneg |
 464				   SUPPORTED_1000baseT_Full;
 465		ecmd->advertising |= ADVERTISED_Autoneg |
 466				     ADVERTISED_1000baseT_Full;
 467	}
 468	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4)
 469		ecmd->supported |= SUPPORTED_40000baseSR4_Full;
 470	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4)
 471		ecmd->supported |= SUPPORTED_40000baseLR4_Full;
 472
 473	/* With no link speed and duplex are unknown */
 474	ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
 475	ecmd->duplex = DUPLEX_UNKNOWN;
 476}
 477
 478/**
 479 * i40e_get_settings - Get Link Speed and Duplex settings
 480 * @netdev: network interface device structure
 481 * @ecmd: ethtool command
 482 *
 483 * Reports speed/duplex settings based on media_type
 484 **/
 485static int i40e_get_settings(struct net_device *netdev,
 486			     struct ethtool_cmd *ecmd)
 487{
 488	struct i40e_netdev_priv *np = netdev_priv(netdev);
 489	struct i40e_pf *pf = np->vsi->back;
 490	struct i40e_hw *hw = &pf->hw;
 491	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
 492	bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
 493
 494	if (link_up)
 495		i40e_get_settings_link_up(hw, ecmd, netdev, pf);
 496	else
 497		i40e_get_settings_link_down(hw, ecmd, pf);
 498
 499	/* Now set the settings that don't rely on link being up/down */
 500
 501	/* For backplane, supported and advertised are only reliant on the
 502	 * phy types the NVM specifies are supported.
 503	 */
 504	if (hw->device_id == I40E_DEV_ID_KX_B ||
 505	    hw->device_id == I40E_DEV_ID_KX_C ||
 506	    hw->device_id == I40E_DEV_ID_20G_KR2 ||
 507	    hw->device_id ==  I40E_DEV_ID_20G_KR2_A) {
 508		ecmd->supported = SUPPORTED_Autoneg;
 509		ecmd->advertising = ADVERTISED_Autoneg;
 510		if (hw->phy.phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
 511			ecmd->supported |= SUPPORTED_40000baseKR4_Full;
 512			ecmd->advertising |= ADVERTISED_40000baseKR4_Full;
 513		}
 514		if (hw->phy.phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
 515			ecmd->supported |= SUPPORTED_20000baseKR2_Full;
 516			ecmd->advertising |= ADVERTISED_20000baseKR2_Full;
 517		}
 518		if (hw->phy.phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR) {
 519			ecmd->supported |= SUPPORTED_10000baseKR_Full;
 520			ecmd->advertising |= ADVERTISED_10000baseKR_Full;
 521		}
 522		if (hw->phy.phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
 523			ecmd->supported |= SUPPORTED_10000baseKX4_Full;
 524			ecmd->advertising |= ADVERTISED_10000baseKX4_Full;
 525		}
 526		if (hw->phy.phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX) {
 527			ecmd->supported |= SUPPORTED_1000baseKX_Full;
 528			ecmd->advertising |= ADVERTISED_1000baseKX_Full;
 529		}
 530	}
 531
 532	/* Set autoneg settings */
 533	ecmd->autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
 534			  AUTONEG_ENABLE : AUTONEG_DISABLE);
 535
 536	switch (hw->phy.media_type) {
 537	case I40E_MEDIA_TYPE_BACKPLANE:
 538		ecmd->supported |= SUPPORTED_Autoneg |
 539				   SUPPORTED_Backplane;
 540		ecmd->advertising |= ADVERTISED_Autoneg |
 541				     ADVERTISED_Backplane;
 542		ecmd->port = PORT_NONE;
 543		break;
 544	case I40E_MEDIA_TYPE_BASET:
 545		ecmd->supported |= SUPPORTED_TP;
 546		ecmd->advertising |= ADVERTISED_TP;
 547		ecmd->port = PORT_TP;
 548		break;
 549	case I40E_MEDIA_TYPE_DA:
 550	case I40E_MEDIA_TYPE_CX4:
 551		ecmd->supported |= SUPPORTED_FIBRE;
 552		ecmd->advertising |= ADVERTISED_FIBRE;
 553		ecmd->port = PORT_DA;
 554		break;
 555	case I40E_MEDIA_TYPE_FIBER:
 556		ecmd->supported |= SUPPORTED_FIBRE;
 557		ecmd->port = PORT_FIBRE;
 558		break;
 559	case I40E_MEDIA_TYPE_UNKNOWN:
 560	default:
 561		ecmd->port = PORT_OTHER;
 562		break;
 563	}
 564
 565	/* Set transceiver */
 566	ecmd->transceiver = XCVR_EXTERNAL;
 567
 568	/* Set flow control settings */
 569	ecmd->supported |= SUPPORTED_Pause;
 570
 571	switch (hw->fc.requested_mode) {
 572	case I40E_FC_FULL:
 573		ecmd->advertising |= ADVERTISED_Pause;
 574		break;
 575	case I40E_FC_TX_PAUSE:
 576		ecmd->advertising |= ADVERTISED_Asym_Pause;
 577		break;
 578	case I40E_FC_RX_PAUSE:
 579		ecmd->advertising |= (ADVERTISED_Pause |
 580				      ADVERTISED_Asym_Pause);
 581		break;
 582	default:
 583		ecmd->advertising &= ~(ADVERTISED_Pause |
 584				       ADVERTISED_Asym_Pause);
 585		break;
 586	}
 587
 588	return 0;
 589}
 590
 591/**
 592 * i40e_set_settings - Set Speed and Duplex
 593 * @netdev: network interface device structure
 594 * @ecmd: ethtool command
 595 *
 596 * Set speed/duplex per media_types advertised/forced
 597 **/
 598static int i40e_set_settings(struct net_device *netdev,
 599			     struct ethtool_cmd *ecmd)
 600{
 601	struct i40e_netdev_priv *np = netdev_priv(netdev);
 602	struct i40e_aq_get_phy_abilities_resp abilities;
 603	struct i40e_aq_set_phy_config config;
 604	struct i40e_pf *pf = np->vsi->back;
 605	struct i40e_vsi *vsi = np->vsi;
 606	struct i40e_hw *hw = &pf->hw;
 607	struct ethtool_cmd safe_ecmd;
 608	i40e_status status = 0;
 609	bool change = false;
 610	int err = 0;
 611	u8 autoneg;
 612	u32 advertise;
 613
 614	/* Changing port settings is not supported if this isn't the
 615	 * port's controlling PF
 616	 */
 617	if (hw->partition_id != 1) {
 618		i40e_partition_setting_complaint(pf);
 619		return -EOPNOTSUPP;
 620	}
 621
 622	if (vsi != pf->vsi[pf->lan_vsi])
 623		return -EOPNOTSUPP;
 624
 625	if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
 626	    hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
 627	    hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
 628	    hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
 629		return -EOPNOTSUPP;
 630
 631	if (hw->device_id == I40E_DEV_ID_KX_B ||
 632	    hw->device_id == I40E_DEV_ID_KX_C ||
 633	    hw->device_id == I40E_DEV_ID_20G_KR2 ||
 634	    hw->device_id == I40E_DEV_ID_20G_KR2_A) {
 635		netdev_info(netdev, "Changing settings is not supported on backplane.\n");
 636		return -EOPNOTSUPP;
 637	}
 638
 639	/* get our own copy of the bits to check against */
 640	memset(&safe_ecmd, 0, sizeof(struct ethtool_cmd));
 641	i40e_get_settings(netdev, &safe_ecmd);
 642
 643	/* save autoneg and speed out of ecmd */
 644	autoneg = ecmd->autoneg;
 645	advertise = ecmd->advertising;
 646
 647	/* set autoneg and speed back to what they currently are */
 648	ecmd->autoneg = safe_ecmd.autoneg;
 649	ecmd->advertising = safe_ecmd.advertising;
 650
 651	ecmd->cmd = safe_ecmd.cmd;
 652	/* If ecmd and safe_ecmd are not the same now, then they are
 653	 * trying to set something that we do not support
 654	 */
 655	if (memcmp(ecmd, &safe_ecmd, sizeof(struct ethtool_cmd)))
 656		return -EOPNOTSUPP;
 657
 658	while (test_bit(__I40E_CONFIG_BUSY, &vsi->state))
 659		usleep_range(1000, 2000);
 660
 661	/* Get the current phy config */
 662	status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
 663					      NULL);
 664	if (status)
 665		return -EAGAIN;
 666
 667	/* Copy abilities to config in case autoneg is not
 668	 * set below
 669	 */
 670	memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
 671	config.abilities = abilities.abilities;
 672
 673	/* Check autoneg */
 674	if (autoneg == AUTONEG_ENABLE) {
 675		/* If autoneg was not already enabled */
 676		if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
 677			/* If autoneg is not supported, return error */
 678			if (!(safe_ecmd.supported & SUPPORTED_Autoneg)) {
 679				netdev_info(netdev, "Autoneg not supported on this phy\n");
 680				return -EINVAL;
 681			}
 682			/* Autoneg is allowed to change */
 683			config.abilities = abilities.abilities |
 684					   I40E_AQ_PHY_ENABLE_AN;
 685			change = true;
 686		}
 687	} else {
 688		/* If autoneg is currently enabled */
 689		if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
 690			/* If autoneg is supported 10GBASE_T is the only PHY
 691			 * that can disable it, so otherwise return error
 692			 */
 693			if (safe_ecmd.supported & SUPPORTED_Autoneg &&
 694			    hw->phy.link_info.phy_type !=
 695			    I40E_PHY_TYPE_10GBASE_T) {
 696				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
 697				return -EINVAL;
 698			}
 699			/* Autoneg is allowed to change */
 700			config.abilities = abilities.abilities &
 701					   ~I40E_AQ_PHY_ENABLE_AN;
 702			change = true;
 703		}
 704	}
 705
 706	if (advertise & ~safe_ecmd.supported)
 707		return -EINVAL;
 708
 709	if (advertise & ADVERTISED_100baseT_Full)
 710		config.link_speed |= I40E_LINK_SPEED_100MB;
 711	if (advertise & ADVERTISED_1000baseT_Full ||
 712	    advertise & ADVERTISED_1000baseKX_Full)
 713		config.link_speed |= I40E_LINK_SPEED_1GB;
 714	if (advertise & ADVERTISED_10000baseT_Full ||
 715	    advertise & ADVERTISED_10000baseKX4_Full ||
 716	    advertise & ADVERTISED_10000baseKR_Full)
 717		config.link_speed |= I40E_LINK_SPEED_10GB;
 718	if (advertise & ADVERTISED_20000baseKR2_Full)
 719		config.link_speed |= I40E_LINK_SPEED_20GB;
 720	if (advertise & ADVERTISED_40000baseKR4_Full ||
 721	    advertise & ADVERTISED_40000baseCR4_Full ||
 722	    advertise & ADVERTISED_40000baseSR4_Full ||
 723	    advertise & ADVERTISED_40000baseLR4_Full)
 724		config.link_speed |= I40E_LINK_SPEED_40GB;
 725
 726	/* If speed didn't get set, set it to what it currently is.
 727	 * This is needed because if advertise is 0 (as it is when autoneg
 728	 * is disabled) then speed won't get set.
 729	 */
 730	if (!config.link_speed)
 731		config.link_speed = abilities.link_speed;
 732
 733	if (change || (abilities.link_speed != config.link_speed)) {
 734		/* copy over the rest of the abilities */
 735		config.phy_type = abilities.phy_type;
 736		config.eee_capability = abilities.eee_capability;
 737		config.eeer = abilities.eeer_val;
 738		config.low_power_ctrl = abilities.d3_lpan;
 739
 740		/* save the requested speeds */
 741		hw->phy.link_info.requested_speeds = config.link_speed;
 742		/* set link and auto negotiation so changes take effect */
 743		config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
 744		/* If link is up put link down */
 745		if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
 746			/* Tell the OS link is going down, the link will go
 747			 * back up when fw says it is ready asynchronously
 748			 */
 749			i40e_print_link_message(vsi, false);
 750			netif_carrier_off(netdev);
 751			netif_tx_stop_all_queues(netdev);
 752		}
 753
 754		/* make the aq call */
 755		status = i40e_aq_set_phy_config(hw, &config, NULL);
 756		if (status) {
 757			netdev_info(netdev, "Set phy config failed, err %s aq_err %s\n",
 758				    i40e_stat_str(hw, status),
 759				    i40e_aq_str(hw, hw->aq.asq_last_status));
 760			return -EAGAIN;
 761		}
 762
 763		status = i40e_update_link_info(hw);
 764		if (status)
 765			netdev_dbg(netdev, "Updating link info failed with err %s aq_err %s\n",
 766				   i40e_stat_str(hw, status),
 767				   i40e_aq_str(hw, hw->aq.asq_last_status));
 768
 769	} else {
 770		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
 771	}
 772
 773	return err;
 774}
 775
 776static int i40e_nway_reset(struct net_device *netdev)
 777{
 778	/* restart autonegotiation */
 779	struct i40e_netdev_priv *np = netdev_priv(netdev);
 780	struct i40e_pf *pf = np->vsi->back;
 781	struct i40e_hw *hw = &pf->hw;
 782	bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
 783	i40e_status ret = 0;
 784
 785	ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
 786	if (ret) {
 787		netdev_info(netdev, "link restart failed, err %s aq_err %s\n",
 788			    i40e_stat_str(hw, ret),
 789			    i40e_aq_str(hw, hw->aq.asq_last_status));
 790		return -EIO;
 791	}
 792
 793	return 0;
 794}
 795
 796/**
 797 * i40e_get_pauseparam -  Get Flow Control status
 798 * Return tx/rx-pause status
 799 **/
 800static void i40e_get_pauseparam(struct net_device *netdev,
 801				struct ethtool_pauseparam *pause)
 802{
 803	struct i40e_netdev_priv *np = netdev_priv(netdev);
 804	struct i40e_pf *pf = np->vsi->back;
 805	struct i40e_hw *hw = &pf->hw;
 806	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
 807	struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
 808
 809	pause->autoneg =
 810		((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
 811		  AUTONEG_ENABLE : AUTONEG_DISABLE);
 812
 813	/* PFC enabled so report LFC as off */
 814	if (dcbx_cfg->pfc.pfcenable) {
 815		pause->rx_pause = 0;
 816		pause->tx_pause = 0;
 817		return;
 818	}
 819
 820	if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
 821		pause->rx_pause = 1;
 822	} else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
 823		pause->tx_pause = 1;
 824	} else if (hw->fc.current_mode == I40E_FC_FULL) {
 825		pause->rx_pause = 1;
 826		pause->tx_pause = 1;
 827	}
 828}
 829
 830/**
 831 * i40e_set_pauseparam - Set Flow Control parameter
 832 * @netdev: network interface device structure
 833 * @pause: return tx/rx flow control status
 834 **/
 835static int i40e_set_pauseparam(struct net_device *netdev,
 836			       struct ethtool_pauseparam *pause)
 837{
 838	struct i40e_netdev_priv *np = netdev_priv(netdev);
 839	struct i40e_pf *pf = np->vsi->back;
 840	struct i40e_vsi *vsi = np->vsi;
 841	struct i40e_hw *hw = &pf->hw;
 842	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
 843	struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
 844	bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
 845	i40e_status status;
 846	u8 aq_failures;
 847	int err = 0;
 848
 849	/* Changing the port's flow control is not supported if this isn't the
 850	 * port's controlling PF
 851	 */
 852	if (hw->partition_id != 1) {
 853		i40e_partition_setting_complaint(pf);
 854		return -EOPNOTSUPP;
 855	}
 856
 857	if (vsi != pf->vsi[pf->lan_vsi])
 858		return -EOPNOTSUPP;
 859
 860	if (pause->autoneg != ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
 861	    AUTONEG_ENABLE : AUTONEG_DISABLE)) {
 862		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
 863		return -EOPNOTSUPP;
 864	}
 865
 866	/* If we have link and don't have autoneg */
 867	if (!test_bit(__I40E_DOWN, &pf->state) &&
 868	    !(hw_link_info->an_info & I40E_AQ_AN_COMPLETED)) {
 869		/* Send message that it might not necessarily work*/
 870		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
 871	}
 872
 873	if (dcbx_cfg->pfc.pfcenable) {
 874		netdev_info(netdev,
 875			    "Priority flow control enabled. Cannot set link flow control.\n");
 876		return -EOPNOTSUPP;
 877	}
 878
 879	if (pause->rx_pause && pause->tx_pause)
 880		hw->fc.requested_mode = I40E_FC_FULL;
 881	else if (pause->rx_pause && !pause->tx_pause)
 882		hw->fc.requested_mode = I40E_FC_RX_PAUSE;
 883	else if (!pause->rx_pause && pause->tx_pause)
 884		hw->fc.requested_mode = I40E_FC_TX_PAUSE;
 885	else if (!pause->rx_pause && !pause->tx_pause)
 886		hw->fc.requested_mode = I40E_FC_NONE;
 887	else
 888		 return -EINVAL;
 889
 890	/* Tell the OS link is going down, the link will go back up when fw
 891	 * says it is ready asynchronously
 892	 */
 893	i40e_print_link_message(vsi, false);
 894	netif_carrier_off(netdev);
 895	netif_tx_stop_all_queues(netdev);
 896
 897	/* Set the fc mode and only restart an if link is up*/
 898	status = i40e_set_fc(hw, &aq_failures, link_up);
 899
 900	if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
 901		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
 902			    i40e_stat_str(hw, status),
 903			    i40e_aq_str(hw, hw->aq.asq_last_status));
 904		err = -EAGAIN;
 905	}
 906	if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
 907		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
 908			    i40e_stat_str(hw, status),
 909			    i40e_aq_str(hw, hw->aq.asq_last_status));
 910		err = -EAGAIN;
 911	}
 912	if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
 913		netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
 914			    i40e_stat_str(hw, status),
 915			    i40e_aq_str(hw, hw->aq.asq_last_status));
 916		err = -EAGAIN;
 917	}
 918
 919	if (!test_bit(__I40E_DOWN, &pf->state)) {
 920		/* Give it a little more time to try to come back */
 921		msleep(75);
 922		if (!test_bit(__I40E_DOWN, &pf->state))
 923			return i40e_nway_reset(netdev);
 924	}
 925
 926	return err;
 927}
 928
 929static u32 i40e_get_msglevel(struct net_device *netdev)
 930{
 931	struct i40e_netdev_priv *np = netdev_priv(netdev);
 932	struct i40e_pf *pf = np->vsi->back;
 933
 934	return pf->msg_enable;
 935}
 936
 937static void i40e_set_msglevel(struct net_device *netdev, u32 data)
 938{
 939	struct i40e_netdev_priv *np = netdev_priv(netdev);
 940	struct i40e_pf *pf = np->vsi->back;
 941
 942	if (I40E_DEBUG_USER & data)
 943		pf->hw.debug_mask = data;
 944	pf->msg_enable = data;
 945}
 946
 947static int i40e_get_regs_len(struct net_device *netdev)
 948{
 949	int reg_count = 0;
 950	int i;
 951
 952	for (i = 0; i40e_reg_list[i].offset != 0; i++)
 953		reg_count += i40e_reg_list[i].elements;
 954
 955	return reg_count * sizeof(u32);
 956}
 957
 958static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
 959			  void *p)
 960{
 961	struct i40e_netdev_priv *np = netdev_priv(netdev);
 962	struct i40e_pf *pf = np->vsi->back;
 963	struct i40e_hw *hw = &pf->hw;
 964	u32 *reg_buf = p;
 965	int i, j, ri;
 966	u32 reg;
 967
 968	/* Tell ethtool which driver-version-specific regs output we have.
 969	 *
 970	 * At some point, if we have ethtool doing special formatting of
 971	 * this data, it will rely on this version number to know how to
 972	 * interpret things.  Hence, this needs to be updated if/when the
 973	 * diags register table is changed.
 974	 */
 975	regs->version = 1;
 976
 977	/* loop through the diags reg table for what to print */
 978	ri = 0;
 979	for (i = 0; i40e_reg_list[i].offset != 0; i++) {
 980		for (j = 0; j < i40e_reg_list[i].elements; j++) {
 981			reg = i40e_reg_list[i].offset
 982				+ (j * i40e_reg_list[i].stride);
 983			reg_buf[ri++] = rd32(hw, reg);
 984		}
 985	}
 986
 987}
 988
 989static int i40e_get_eeprom(struct net_device *netdev,
 990			   struct ethtool_eeprom *eeprom, u8 *bytes)
 991{
 992	struct i40e_netdev_priv *np = netdev_priv(netdev);
 993	struct i40e_hw *hw = &np->vsi->back->hw;
 994	struct i40e_pf *pf = np->vsi->back;
 995	int ret_val = 0, len, offset;
 996	u8 *eeprom_buff;
 997	u16 i, sectors;
 998	bool last;
 999	u32 magic;
1000
1001#define I40E_NVM_SECTOR_SIZE  4096
1002	if (eeprom->len == 0)
1003		return -EINVAL;
1004
1005	/* check for NVMUpdate access method */
1006	magic = hw->vendor_id | (hw->device_id << 16);
1007	if (eeprom->magic && eeprom->magic != magic) {
1008		struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1009		int errno = 0;
1010
1011		/* make sure it is the right magic for NVMUpdate */
1012		if ((eeprom->magic >> 16) != hw->device_id)
1013			errno = -EINVAL;
1014		else if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
1015			 test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
1016			errno = -EBUSY;
1017		else
1018			ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1019
1020		if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1021			dev_info(&pf->pdev->dev,
1022				 "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1023				 ret_val, hw->aq.asq_last_status, errno,
1024				 (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1025				 cmd->offset, cmd->data_size);
1026
1027		return errno;
1028	}
1029
1030	/* normal ethtool get_eeprom support */
1031	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1032
1033	eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
1034	if (!eeprom_buff)
1035		return -ENOMEM;
1036
1037	ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1038	if (ret_val) {
1039		dev_info(&pf->pdev->dev,
1040			 "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
1041			 ret_val, hw->aq.asq_last_status);
1042		goto free_buff;
1043	}
1044
1045	sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
1046	sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
1047	len = I40E_NVM_SECTOR_SIZE;
1048	last = false;
1049	for (i = 0; i < sectors; i++) {
1050		if (i == (sectors - 1)) {
1051			len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
1052			last = true;
1053		}
1054		offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
1055		ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
1056				(u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
1057				last, NULL);
1058		if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
1059			dev_info(&pf->pdev->dev,
1060				 "read NVM failed, invalid offset 0x%x\n",
1061				 offset);
1062			break;
1063		} else if (ret_val &&
1064			   hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
1065			dev_info(&pf->pdev->dev,
1066				 "read NVM failed, access, offset 0x%x\n",
1067				 offset);
1068			break;
1069		} else if (ret_val) {
1070			dev_info(&pf->pdev->dev,
1071				 "read NVM failed offset %d err=%d status=0x%x\n",
1072				 offset, ret_val, hw->aq.asq_last_status);
1073			break;
1074		}
1075	}
1076
1077	i40e_release_nvm(hw);
1078	memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
1079free_buff:
1080	kfree(eeprom_buff);
1081	return ret_val;
1082}
1083
1084static int i40e_get_eeprom_len(struct net_device *netdev)
1085{
1086	struct i40e_netdev_priv *np = netdev_priv(netdev);
1087	struct i40e_hw *hw = &np->vsi->back->hw;
1088	u32 val;
1089
1090	val = (rd32(hw, I40E_GLPCI_LBARCTRL)
1091		& I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
1092		>> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
1093	/* register returns value in power of 2, 64Kbyte chunks. */
1094	val = (64 * 1024) * BIT(val);
1095	return val;
1096}
1097
1098static int i40e_set_eeprom(struct net_device *netdev,
1099			   struct ethtool_eeprom *eeprom, u8 *bytes)
1100{
1101	struct i40e_netdev_priv *np = netdev_priv(netdev);
1102	struct i40e_hw *hw = &np->vsi->back->hw;
1103	struct i40e_pf *pf = np->vsi->back;
1104	struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1105	int ret_val = 0;
1106	int errno = 0;
1107	u32 magic;
1108
1109	/* normal ethtool set_eeprom is not supported */
1110	magic = hw->vendor_id | (hw->device_id << 16);
1111	if (eeprom->magic == magic)
1112		errno = -EOPNOTSUPP;
1113	/* check for NVMUpdate access method */
1114	else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
1115		errno = -EINVAL;
1116	else if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
1117		 test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
1118		errno = -EBUSY;
1119	else
1120		ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1121
1122	if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1123		dev_info(&pf->pdev->dev,
1124			 "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1125			 ret_val, hw->aq.asq_last_status, errno,
1126			 (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1127			 cmd->offset, cmd->data_size);
1128
1129	return errno;
1130}
1131
1132static void i40e_get_drvinfo(struct net_device *netdev,
1133			     struct ethtool_drvinfo *drvinfo)
1134{
1135	struct i40e_netdev_priv *np = netdev_priv(netdev);
1136	struct i40e_vsi *vsi = np->vsi;
1137	struct i40e_pf *pf = vsi->back;
1138
1139	strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
1140	strlcpy(drvinfo->version, i40e_driver_version_str,
1141		sizeof(drvinfo->version));
1142	strlcpy(drvinfo->fw_version, i40e_nvm_version_str(&pf->hw),
1143		sizeof(drvinfo->fw_version));
1144	strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
1145		sizeof(drvinfo->bus_info));
1146}
1147
1148static void i40e_get_ringparam(struct net_device *netdev,
1149			       struct ethtool_ringparam *ring)
1150{
1151	struct i40e_netdev_priv *np = netdev_priv(netdev);
1152	struct i40e_pf *pf = np->vsi->back;
1153	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
1154
1155	ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1156	ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1157	ring->rx_mini_max_pending = 0;
1158	ring->rx_jumbo_max_pending = 0;
1159	ring->rx_pending = vsi->rx_rings[0]->count;
1160	ring->tx_pending = vsi->tx_rings[0]->count;
1161	ring->rx_mini_pending = 0;
1162	ring->rx_jumbo_pending = 0;
1163}
1164
1165static int i40e_set_ringparam(struct net_device *netdev,
1166			      struct ethtool_ringparam *ring)
1167{
1168	struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
1169	struct i40e_netdev_priv *np = netdev_priv(netdev);
1170	struct i40e_vsi *vsi = np->vsi;
1171	struct i40e_pf *pf = vsi->back;
1172	u32 new_rx_count, new_tx_count;
1173	int i, err = 0;
1174
1175	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
1176		return -EINVAL;
1177
1178	if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1179	    ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
1180	    ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1181	    ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
1182		netdev_info(netdev,
1183			    "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
1184			    ring->tx_pending, ring->rx_pending,
1185			    I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
1186		return -EINVAL;
1187	}
1188
1189	new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1190	new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1191
1192	/* if nothing to do return success */
1193	if ((new_tx_count == vsi->tx_rings[0]->count) &&
1194	    (new_rx_count == vsi->rx_rings[0]->count))
1195		return 0;
1196
1197	while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
1198		usleep_range(1000, 2000);
1199
1200	if (!netif_running(vsi->netdev)) {
1201		/* simple case - set for the next time the netdev is started */
1202		for (i = 0; i < vsi->num_queue_pairs; i++) {
1203			vsi->tx_rings[i]->count = new_tx_count;
1204			vsi->rx_rings[i]->count = new_rx_count;
1205		}
1206		goto done;
1207	}
1208
1209	/* We can't just free everything and then setup again,
1210	 * because the ISRs in MSI-X mode get passed pointers
1211	 * to the Tx and Rx ring structs.
1212	 */
1213
1214	/* alloc updated Tx resources */
1215	if (new_tx_count != vsi->tx_rings[0]->count) {
1216		netdev_info(netdev,
1217			    "Changing Tx descriptor count from %d to %d.\n",
1218			    vsi->tx_rings[0]->count, new_tx_count);
1219		tx_rings = kcalloc(vsi->alloc_queue_pairs,
1220				   sizeof(struct i40e_ring), GFP_KERNEL);
1221		if (!tx_rings) {
1222			err = -ENOMEM;
1223			goto done;
1224		}
1225
1226		for (i = 0; i < vsi->num_queue_pairs; i++) {
1227			/* clone ring and setup updated count */
1228			tx_rings[i] = *vsi->tx_rings[i];
1229			tx_rings[i].count = new_tx_count;
1230			/* the desc and bi pointers will be reallocated in the
1231			 * setup call
1232			 */
1233			tx_rings[i].desc = NULL;
1234			tx_rings[i].rx_bi = NULL;
1235			err = i40e_setup_tx_descriptors(&tx_rings[i]);
1236			if (err) {
1237				while (i) {
1238					i--;
1239					i40e_free_tx_resources(&tx_rings[i]);
1240				}
1241				kfree(tx_rings);
1242				tx_rings = NULL;
1243
1244				goto done;
1245			}
1246		}
1247	}
1248
1249	/* alloc updated Rx resources */
1250	if (new_rx_count != vsi->rx_rings[0]->count) {
1251		netdev_info(netdev,
1252			    "Changing Rx descriptor count from %d to %d\n",
1253			    vsi->rx_rings[0]->count, new_rx_count);
1254		rx_rings = kcalloc(vsi->alloc_queue_pairs,
1255				   sizeof(struct i40e_ring), GFP_KERNEL);
1256		if (!rx_rings) {
1257			err = -ENOMEM;
1258			goto free_tx;
1259		}
1260
1261		for (i = 0; i < vsi->num_queue_pairs; i++) {
1262			/* clone ring and setup updated count */
1263			rx_rings[i] = *vsi->rx_rings[i];
1264			rx_rings[i].count = new_rx_count;
1265			/* the desc and bi pointers will be reallocated in the
1266			 * setup call
1267			 */
1268			rx_rings[i].desc = NULL;
1269			rx_rings[i].rx_bi = NULL;
1270			err = i40e_setup_rx_descriptors(&rx_rings[i]);
1271			if (err) {
1272				while (i) {
1273					i--;
1274					i40e_free_rx_resources(&rx_rings[i]);
1275				}
1276				kfree(rx_rings);
1277				rx_rings = NULL;
1278
1279				goto free_tx;
1280			}
1281		}
1282	}
1283
1284	/* Bring interface down, copy in the new ring info,
1285	 * then restore the interface
1286	 */
1287	i40e_down(vsi);
1288
1289	if (tx_rings) {
1290		for (i = 0; i < vsi->num_queue_pairs; i++) {
1291			i40e_free_tx_resources(vsi->tx_rings[i]);
1292			*vsi->tx_rings[i] = tx_rings[i];
1293		}
1294		kfree(tx_rings);
1295		tx_rings = NULL;
1296	}
1297
1298	if (rx_rings) {
1299		for (i = 0; i < vsi->num_queue_pairs; i++) {
1300			i40e_free_rx_resources(vsi->rx_rings[i]);
1301			*vsi->rx_rings[i] = rx_rings[i];
1302		}
1303		kfree(rx_rings);
1304		rx_rings = NULL;
1305	}
1306
1307	i40e_up(vsi);
1308
1309free_tx:
1310	/* error cleanup if the Rx allocations failed after getting Tx */
1311	if (tx_rings) {
1312		for (i = 0; i < vsi->num_queue_pairs; i++)
1313			i40e_free_tx_resources(&tx_rings[i]);
1314		kfree(tx_rings);
1315		tx_rings = NULL;
1316	}
1317
1318done:
1319	clear_bit(__I40E_CONFIG_BUSY, &pf->state);
1320
1321	return err;
1322}
1323
1324static int i40e_get_sset_count(struct net_device *netdev, int sset)
1325{
1326	struct i40e_netdev_priv *np = netdev_priv(netdev);
1327	struct i40e_vsi *vsi = np->vsi;
1328	struct i40e_pf *pf = vsi->back;
1329
1330	switch (sset) {
1331	case ETH_SS_TEST:
1332		return I40E_TEST_LEN;
1333	case ETH_SS_STATS:
1334		if (vsi == pf->vsi[pf->lan_vsi] && pf->hw.partition_id == 1) {
1335			int len = I40E_PF_STATS_LEN(netdev);
1336
1337			if ((pf->lan_veb != I40E_NO_VEB) &&
1338			    (pf->flags & I40E_FLAG_VEB_STATS_ENABLED))
1339				len += I40E_VEB_STATS_TOTAL;
1340			return len;
1341		} else {
1342			return I40E_VSI_STATS_LEN(netdev);
1343		}
1344	case ETH_SS_PRIV_FLAGS:
1345		return I40E_PRIV_FLAGS_STR_LEN;
1346	default:
1347		return -EOPNOTSUPP;
1348	}
1349}
1350
1351static void i40e_get_ethtool_stats(struct net_device *netdev,
1352				   struct ethtool_stats *stats, u64 *data)
1353{
1354	struct i40e_netdev_priv *np = netdev_priv(netdev);
1355	struct i40e_ring *tx_ring, *rx_ring;
1356	struct i40e_vsi *vsi = np->vsi;
1357	struct i40e_pf *pf = vsi->back;
1358	int i = 0;
1359	char *p;
1360	int j;
1361	struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
1362	unsigned int start;
1363
1364	i40e_update_stats(vsi);
1365
1366	for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
1367		p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
1368		data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
1369			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1370	}
1371	for (j = 0; j < I40E_MISC_STATS_LEN; j++) {
1372		p = (char *)vsi + i40e_gstrings_misc_stats[j].stat_offset;
1373		data[i++] = (i40e_gstrings_misc_stats[j].sizeof_stat ==
1374			    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1375	}
1376#ifdef I40E_FCOE
1377	for (j = 0; j < I40E_FCOE_STATS_LEN; j++) {
1378		p = (char *)vsi + i40e_gstrings_fcoe_stats[j].stat_offset;
1379		data[i++] = (i40e_gstrings_fcoe_stats[j].sizeof_stat ==
1380			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1381	}
1382#endif
1383	rcu_read_lock();
1384	for (j = 0; j < vsi->num_queue_pairs; j++) {
1385		tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
1386
1387		if (!tx_ring)
1388			continue;
1389
1390		/* process Tx ring statistics */
1391		do {
1392			start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
1393			data[i] = tx_ring->stats.packets;
1394			data[i + 1] = tx_ring->stats.bytes;
1395		} while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
1396		i += 2;
1397
1398		/* Rx ring is the 2nd half of the queue pair */
1399		rx_ring = &tx_ring[1];
1400		do {
1401			start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
1402			data[i] = rx_ring->stats.packets;
1403			data[i + 1] = rx_ring->stats.bytes;
1404		} while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
1405		i += 2;
1406	}
1407	rcu_read_unlock();
1408	if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
1409		return;
1410
1411	if ((pf->lan_veb != I40E_NO_VEB) &&
1412	    (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
1413		struct i40e_veb *veb = pf->veb[pf->lan_veb];
1414
1415		for (j = 0; j < I40E_VEB_STATS_LEN; j++) {
1416			p = (char *)veb;
1417			p += i40e_gstrings_veb_stats[j].stat_offset;
1418			data[i++] = (i40e_gstrings_veb_stats[j].sizeof_stat ==
1419				     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1420		}
1421		for (j = 0; j < I40E_MAX_TRAFFIC_CLASS; j++) {
1422			data[i++] = veb->tc_stats.tc_tx_packets[j];
1423			data[i++] = veb->tc_stats.tc_tx_bytes[j];
1424			data[i++] = veb->tc_stats.tc_rx_packets[j];
1425			data[i++] = veb->tc_stats.tc_rx_bytes[j];
1426		}
1427	}
1428	for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
1429		p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
1430		data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
1431			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1432	}
1433	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1434		data[i++] = pf->stats.priority_xon_tx[j];
1435		data[i++] = pf->stats.priority_xoff_tx[j];
1436	}
1437	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1438		data[i++] = pf->stats.priority_xon_rx[j];
1439		data[i++] = pf->stats.priority_xoff_rx[j];
1440	}
1441	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
1442		data[i++] = pf->stats.priority_xon_2_xoff[j];
1443}
1444
1445static void i40e_get_strings(struct net_device *netdev, u32 stringset,
1446			     u8 *data)
1447{
1448	struct i40e_netdev_priv *np = netdev_priv(netdev);
1449	struct i40e_vsi *vsi = np->vsi;
1450	struct i40e_pf *pf = vsi->back;
1451	char *p = (char *)data;
1452	int i;
1453
1454	switch (stringset) {
1455	case ETH_SS_TEST:
1456		for (i = 0; i < I40E_TEST_LEN; i++) {
1457			memcpy(data, i40e_gstrings_test[i], ETH_GSTRING_LEN);
1458			data += ETH_GSTRING_LEN;
1459		}
1460		break;
1461	case ETH_SS_STATS:
1462		for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
1463			snprintf(p, ETH_GSTRING_LEN, "%s",
1464				 i40e_gstrings_net_stats[i].stat_string);
1465			p += ETH_GSTRING_LEN;
1466		}
1467		for (i = 0; i < I40E_MISC_STATS_LEN; i++) {
1468			snprintf(p, ETH_GSTRING_LEN, "%s",
1469				 i40e_gstrings_misc_stats[i].stat_string);
1470			p += ETH_GSTRING_LEN;
1471		}
1472#ifdef I40E_FCOE
1473		for (i = 0; i < I40E_FCOE_STATS_LEN; i++) {
1474			snprintf(p, ETH_GSTRING_LEN, "%s",
1475				 i40e_gstrings_fcoe_stats[i].stat_string);
1476			p += ETH_GSTRING_LEN;
1477		}
1478#endif
1479		for (i = 0; i < vsi->num_queue_pairs; i++) {
1480			snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
1481			p += ETH_GSTRING_LEN;
1482			snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
1483			p += ETH_GSTRING_LEN;
1484			snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
1485			p += ETH_GSTRING_LEN;
1486			snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
1487			p += ETH_GSTRING_LEN;
1488		}
1489		if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
1490			return;
1491
1492		if ((pf->lan_veb != I40E_NO_VEB) &&
1493		    (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
1494			for (i = 0; i < I40E_VEB_STATS_LEN; i++) {
1495				snprintf(p, ETH_GSTRING_LEN, "veb.%s",
1496					i40e_gstrings_veb_stats[i].stat_string);
1497				p += ETH_GSTRING_LEN;
1498			}
1499			for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1500				snprintf(p, ETH_GSTRING_LEN,
1501					 "veb.tc_%u_tx_packets", i);
1502				p += ETH_GSTRING_LEN;
1503				snprintf(p, ETH_GSTRING_LEN,
1504					 "veb.tc_%u_tx_bytes", i);
1505				p += ETH_GSTRING_LEN;
1506				snprintf(p, ETH_GSTRING_LEN,
1507					 "veb.tc_%u_rx_packets", i);
1508				p += ETH_GSTRING_LEN;
1509				snprintf(p, ETH_GSTRING_LEN,
1510					 "veb.tc_%u_rx_bytes", i);
1511				p += ETH_GSTRING_LEN;
1512			}
1513		}
1514		for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
1515			snprintf(p, ETH_GSTRING_LEN, "port.%s",
1516				 i40e_gstrings_stats[i].stat_string);
1517			p += ETH_GSTRING_LEN;
1518		}
1519		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1520			snprintf(p, ETH_GSTRING_LEN,
1521				 "port.tx_priority_%u_xon", i);
1522			p += ETH_GSTRING_LEN;
1523			snprintf(p, ETH_GSTRING_LEN,
1524				 "port.tx_priority_%u_xoff", i);
1525			p += ETH_GSTRING_LEN;
1526		}
1527		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1528			snprintf(p, ETH_GSTRING_LEN,
1529				 "port.rx_priority_%u_xon", i);
1530			p += ETH_GSTRING_LEN;
1531			snprintf(p, ETH_GSTRING_LEN,
1532				 "port.rx_priority_%u_xoff", i);
1533			p += ETH_GSTRING_LEN;
1534		}
1535		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1536			snprintf(p, ETH_GSTRING_LEN,
1537				 "port.rx_priority_%u_xon_2_xoff", i);
1538			p += ETH_GSTRING_LEN;
1539		}
1540		/* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
1541		break;
1542	case ETH_SS_PRIV_FLAGS:
1543		for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
1544			memcpy(data, i40e_priv_flags_strings[i],
1545			       ETH_GSTRING_LEN);
1546			data += ETH_GSTRING_LEN;
1547		}
1548		break;
1549	default:
1550		break;
1551	}
1552}
1553
1554static int i40e_get_ts_info(struct net_device *dev,
1555			    struct ethtool_ts_info *info)
1556{
1557	struct i40e_pf *pf = i40e_netdev_to_pf(dev);
1558
1559	/* only report HW timestamping if PTP is enabled */
1560	if (!(pf->flags & I40E_FLAG_PTP))
1561		return ethtool_op_get_ts_info(dev, info);
1562
1563	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1564				SOF_TIMESTAMPING_RX_SOFTWARE |
1565				SOF_TIMESTAMPING_SOFTWARE |
1566				SOF_TIMESTAMPING_TX_HARDWARE |
1567				SOF_TIMESTAMPING_RX_HARDWARE |
1568				SOF_TIMESTAMPING_RAW_HARDWARE;
1569
1570	if (pf->ptp_clock)
1571		info->phc_index = ptp_clock_index(pf->ptp_clock);
1572	else
1573		info->phc_index = -1;
1574
1575	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
1576
1577	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1578			   BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1579			   BIT(HWTSTAMP_FILTER_PTP_V2_EVENT);
1580
1581	return 0;
1582}
1583
1584static int i40e_link_test(struct net_device *netdev, u64 *data)
1585{
1586	struct i40e_netdev_priv *np = netdev_priv(netdev);
1587	struct i40e_pf *pf = np->vsi->back;
1588	i40e_status status;
1589	bool link_up = false;
1590
1591	netif_info(pf, hw, netdev, "link test\n");
1592	status = i40e_get_link_status(&pf->hw, &link_up);
1593	if (status) {
1594		netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
1595		*data = 1;
1596		return *data;
1597	}
1598
1599	if (link_up)
1600		*data = 0;
1601	else
1602		*data = 1;
1603
1604	return *data;
1605}
1606
1607static int i40e_reg_test(struct net_device *netdev, u64 *data)
1608{
1609	struct i40e_netdev_priv *np = netdev_priv(netdev);
1610	struct i40e_pf *pf = np->vsi->back;
1611
1612	netif_info(pf, hw, netdev, "register test\n");
1613	*data = i40e_diag_reg_test(&pf->hw);
1614
1615	return *data;
1616}
1617
1618static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
1619{
1620	struct i40e_netdev_priv *np = netdev_priv(netdev);
1621	struct i40e_pf *pf = np->vsi->back;
1622
1623	netif_info(pf, hw, netdev, "eeprom test\n");
1624	*data = i40e_diag_eeprom_test(&pf->hw);
1625
1626	/* forcebly clear the NVM Update state machine */
1627	pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
1628
1629	return *data;
1630}
1631
1632static int i40e_intr_test(struct net_device *netdev, u64 *data)
1633{
1634	struct i40e_netdev_priv *np = netdev_priv(netdev);
1635	struct i40e_pf *pf = np->vsi->back;
1636	u16 swc_old = pf->sw_int_count;
1637
1638	netif_info(pf, hw, netdev, "interrupt test\n");
1639	wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
1640	     (I40E_PFINT_DYN_CTL0_INTENA_MASK |
1641	      I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
1642	      I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
1643	      I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
1644	      I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
1645	usleep_range(1000, 2000);
1646	*data = (swc_old == pf->sw_int_count);
1647
1648	return *data;
1649}
1650
1651static int i40e_loopback_test(struct net_device *netdev, u64 *data)
1652{
1653	struct i40e_netdev_priv *np = netdev_priv(netdev);
1654	struct i40e_pf *pf = np->vsi->back;
1655
1656	netif_info(pf, hw, netdev, "loopback test not implemented\n");
1657	*data = 0;
1658
1659	return *data;
1660}
1661
1662static inline bool i40e_active_vfs(struct i40e_pf *pf)
1663{
1664	struct i40e_vf *vfs = pf->vf;
1665	int i;
1666
1667	for (i = 0; i < pf->num_alloc_vfs; i++)
1668		if (test_bit(I40E_VF_STAT_ACTIVE, &vfs[i].vf_states))
1669			return true;
1670	return false;
1671}
1672
1673static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
1674{
1675	struct i40e_vsi **vsi = pf->vsi;
1676	int i;
1677
1678	for (i = 0; i < pf->num_alloc_vsi; i++) {
1679		if (!vsi[i])
1680			continue;
1681		if (vsi[i]->type == I40E_VSI_VMDQ2)
1682			return true;
1683	}
1684
1685	return false;
1686}
1687
1688static void i40e_diag_test(struct net_device *netdev,
1689			   struct ethtool_test *eth_test, u64 *data)
1690{
1691	struct i40e_netdev_priv *np = netdev_priv(netdev);
1692	bool if_running = netif_running(netdev);
1693	struct i40e_pf *pf = np->vsi->back;
1694
1695	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1696		/* Offline tests */
1697		netif_info(pf, drv, netdev, "offline testing starting\n");
1698
1699		set_bit(__I40E_TESTING, &pf->state);
1700
1701		if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
1702			dev_warn(&pf->pdev->dev,
1703				 "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
1704			data[I40E_ETH_TEST_REG]		= 1;
1705			data[I40E_ETH_TEST_EEPROM]	= 1;
1706			data[I40E_ETH_TEST_INTR]	= 1;
1707			data[I40E_ETH_TEST_LOOPBACK]	= 1;
1708			data[I40E_ETH_TEST_LINK]	= 1;
1709			eth_test->flags |= ETH_TEST_FL_FAILED;
1710			clear_bit(__I40E_TESTING, &pf->state);
1711			goto skip_ol_tests;
1712		}
1713
1714		/* If the device is online then take it offline */
1715		if (if_running)
1716			/* indicate we're in test mode */
1717			dev_close(netdev);
1718		else
1719			/* This reset does not affect link - if it is
1720			 * changed to a type of reset that does affect
1721			 * link then the following link test would have
1722			 * to be moved to before the reset
1723			 */
1724			i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
1725
1726		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1727			eth_test->flags |= ETH_TEST_FL_FAILED;
1728
1729		if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
1730			eth_test->flags |= ETH_TEST_FL_FAILED;
1731
1732		if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
1733			eth_test->flags |= ETH_TEST_FL_FAILED;
1734
1735		if (i40e_loopback_test(netdev, &data[I40E_ETH_TEST_LOOPBACK]))
1736			eth_test->flags |= ETH_TEST_FL_FAILED;
1737
1738		/* run reg test last, a reset is required after it */
1739		if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
1740			eth_test->flags |= ETH_TEST_FL_FAILED;
1741
1742		clear_bit(__I40E_TESTING, &pf->state);
1743		i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
1744
1745		if (if_running)
1746			dev_open(netdev);
1747	} else {
1748		/* Online tests */
1749		netif_info(pf, drv, netdev, "online testing starting\n");
1750
1751		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1752			eth_test->flags |= ETH_TEST_FL_FAILED;
1753
1754		/* Offline only tests, not run in online; pass by default */
1755		data[I40E_ETH_TEST_REG] = 0;
1756		data[I40E_ETH_TEST_EEPROM] = 0;
1757		data[I40E_ETH_TEST_INTR] = 0;
1758		data[I40E_ETH_TEST_LOOPBACK] = 0;
1759	}
1760
1761skip_ol_tests:
1762
1763	netif_info(pf, drv, netdev, "testing finished\n");
1764}
1765
1766static void i40e_get_wol(struct net_device *netdev,
1767			 struct ethtool_wolinfo *wol)
1768{
1769	struct i40e_netdev_priv *np = netdev_priv(netdev);
1770	struct i40e_pf *pf = np->vsi->back;
1771	struct i40e_hw *hw = &pf->hw;
1772	u16 wol_nvm_bits;
1773
1774	/* NVM bit on means WoL disabled for the port */
1775	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1776	if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
1777		wol->supported = 0;
1778		wol->wolopts = 0;
1779	} else {
1780		wol->supported = WAKE_MAGIC;
1781		wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
1782	}
1783}
1784
1785/**
1786 * i40e_set_wol - set the WakeOnLAN configuration
1787 * @netdev: the netdev in question
1788 * @wol: the ethtool WoL setting data
1789 **/
1790static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1791{
1792	struct i40e_netdev_priv *np = netdev_priv(netdev);
1793	struct i40e_pf *pf = np->vsi->back;
1794	struct i40e_vsi *vsi = np->vsi;
1795	struct i40e_hw *hw = &pf->hw;
1796	u16 wol_nvm_bits;
1797
1798	/* WoL not supported if this isn't the controlling PF on the port */
1799	if (hw->partition_id != 1) {
1800		i40e_partition_setting_complaint(pf);
1801		return -EOPNOTSUPP;
1802	}
1803
1804	if (vsi != pf->vsi[pf->lan_vsi])
1805		return -EOPNOTSUPP;
1806
1807	/* NVM bit on means WoL disabled for the port */
1808	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1809	if (BIT(hw->port) & wol_nvm_bits)
1810		return -EOPNOTSUPP;
1811
1812	/* only magic packet is supported */
1813	if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
1814		return -EOPNOTSUPP;
1815
1816	/* is this a new value? */
1817	if (pf->wol_en != !!wol->wolopts) {
1818		pf->wol_en = !!wol->wolopts;
1819		device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
1820	}
1821
1822	return 0;
1823}
1824
1825static int i40e_set_phys_id(struct net_device *netdev,
1826			    enum ethtool_phys_id_state state)
1827{
1828	struct i40e_netdev_priv *np = netdev_priv(netdev);
1829	i40e_status ret = 0;
1830	struct i40e_pf *pf = np->vsi->back;
1831	struct i40e_hw *hw = &pf->hw;
1832	int blink_freq = 2;
1833	u16 temp_status;
1834
1835	switch (state) {
1836	case ETHTOOL_ID_ACTIVE:
1837		if (!(pf->flags & I40E_FLAG_HAVE_10GBASET_PHY)) {
1838			pf->led_status = i40e_led_get(hw);
1839		} else {
1840			i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_PORT, NULL);
1841			ret = i40e_led_get_phy(hw, &temp_status,
1842					       &pf->phy_led_val);
1843			pf->led_status = temp_status;
1844		}
1845		return blink_freq;
1846	case ETHTOOL_ID_ON:
1847		if (!(pf->flags & I40E_FLAG_HAVE_10GBASET_PHY))
1848			i40e_led_set(hw, 0xf, false);
1849		else
1850			ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
1851		break;
1852	case ETHTOOL_ID_OFF:
1853		if (!(pf->flags & I40E_FLAG_HAVE_10GBASET_PHY))
1854			i40e_led_set(hw, 0x0, false);
1855		else
1856			ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
1857		break;
1858	case ETHTOOL_ID_INACTIVE:
1859		if (!(pf->flags & I40E_FLAG_HAVE_10GBASET_PHY)) {
1860			i40e_led_set(hw, false, pf->led_status);
1861		} else {
1862			ret = i40e_led_set_phy(hw, false, pf->led_status,
1863					       (pf->phy_led_val |
1864					       I40E_PHY_LED_MODE_ORIG));
1865			i40e_aq_set_phy_debug(hw, 0, NULL);
1866		}
1867		break;
1868	default:
1869		break;
1870	}
1871		if (ret)
1872			return -ENOENT;
1873		else
1874			return 0;
1875}
1876
1877/* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
1878 * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
1879 * 125us (8000 interrupts per second) == ITR(62)
1880 */
1881
1882static int __i40e_get_coalesce(struct net_device *netdev,
1883			       struct ethtool_coalesce *ec,
1884			       int queue)
1885{
1886	struct i40e_netdev_priv *np = netdev_priv(netdev);
1887	struct i40e_vsi *vsi = np->vsi;
1888
1889	ec->tx_max_coalesced_frames_irq = vsi->work_limit;
1890	ec->rx_max_coalesced_frames_irq = vsi->work_limit;
1891
1892	/* rx and tx usecs has per queue value. If user doesn't specify the queue,
1893	 * return queue 0's value to represent.
1894	 */
1895	if (queue < 0) {
1896		queue = 0;
1897	} else if (queue >= vsi->num_queue_pairs) {
1898		return -EINVAL;
1899	}
1900
1901	if (ITR_IS_DYNAMIC(vsi->rx_rings[queue]->rx_itr_setting))
1902		ec->use_adaptive_rx_coalesce = 1;
1903
1904	if (ITR_IS_DYNAMIC(vsi->tx_rings[queue]->tx_itr_setting))
1905		ec->use_adaptive_tx_coalesce = 1;
1906
1907	ec->rx_coalesce_usecs = vsi->rx_rings[queue]->rx_itr_setting & ~I40E_ITR_DYNAMIC;
1908	ec->tx_coalesce_usecs = vsi->tx_rings[queue]->tx_itr_setting & ~I40E_ITR_DYNAMIC;
1909
1910	/* we use the _usecs_high to store/set the interrupt rate limit
1911	 * that the hardware supports, that almost but not quite
1912	 * fits the original intent of the ethtool variable,
1913	 * the rx_coalesce_usecs_high limits total interrupts
1914	 * per second from both tx/rx sources.
1915	 */
1916	ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
1917	ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
1918
1919	return 0;
1920}
1921
1922static int i40e_get_coalesce(struct net_device *netdev,
1923			     struct ethtool_coalesce *ec)
1924{
1925	return __i40e_get_coalesce(netdev, ec, -1);
1926}
1927
1928static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
1929				       struct ethtool_coalesce *ec)
1930{
1931	return __i40e_get_coalesce(netdev, ec, queue);
1932}
1933
1934static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
1935				   struct ethtool_coalesce *ec,
1936				   int queue)
1937{
1938	struct i40e_pf *pf = vsi->back;
1939	struct i40e_hw *hw = &pf->hw;
1940	struct i40e_q_vector *q_vector;
1941	u16 vector, intrl;
1942
1943	intrl = INTRL_USEC_TO_REG(vsi->int_rate_limit);
1944
1945	vsi->rx_rings[queue]->rx_itr_setting = ec->rx_coalesce_usecs;
1946	vsi->tx_rings[queue]->tx_itr_setting = ec->tx_coalesce_usecs;
1947
1948	if (ec->use_adaptive_rx_coalesce)
1949		vsi->rx_rings[queue]->rx_itr_setting |= I40E_ITR_DYNAMIC;
1950	else
1951		vsi->rx_rings[queue]->rx_itr_setting &= ~I40E_ITR_DYNAMIC;
1952
1953	if (ec->use_adaptive_tx_coalesce)
1954		vsi->tx_rings[queue]->tx_itr_setting |= I40E_ITR_DYNAMIC;
1955	else
1956		vsi->tx_rings[queue]->tx_itr_setting &= ~I40E_ITR_DYNAMIC;
1957
1958	q_vector = vsi->rx_rings[queue]->q_vector;
1959	q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[queue]->rx_itr_setting);
1960	vector = vsi->base_vector + q_vector->v_idx;
1961	wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1), q_vector->rx.itr);
1962
1963	q_vector = vsi->tx_rings[queue]->q_vector;
1964	q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[queue]->tx_itr_setting);
1965	vector = vsi->base_vector + q_vector->v_idx;
1966	wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1), q_vector->tx.itr);
1967
1968	wr32(hw, I40E_PFINT_RATEN(vector - 1), intrl);
1969	i40e_flush(hw);
1970}
1971
1972static int __i40e_set_coalesce(struct net_device *netdev,
1973			       struct ethtool_coalesce *ec,
1974			       int queue)
1975{
1976	struct i40e_netdev_priv *np = netdev_priv(netdev);
1977	struct i40e_vsi *vsi = np->vsi;
1978	struct i40e_pf *pf = vsi->back;
1979	int i;
1980
1981	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
1982		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
1983
1984	/* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
1985	if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
1986		netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
1987		return -EINVAL;
1988	}
1989
1990	if (ec->rx_coalesce_usecs_high >= INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
1991		netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-235\n");
1992		return -EINVAL;
1993	}
1994
1995	if (ec->rx_coalesce_usecs == 0) {
1996		if (ec->use_adaptive_rx_coalesce)
1997			netif_info(pf, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
1998	} else if ((ec->rx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
1999		   (ec->rx_coalesce_usecs > (I40E_MAX_ITR << 1))) {
2000			netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
2001			return -EINVAL;
2002	}
2003
2004	vsi->int_rate_limit = ec->rx_coalesce_usecs_high;
2005
2006	if (ec->tx_coalesce_usecs == 0) {
2007		if (ec->use_adaptive_tx_coalesce)
2008			netif_info(pf, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
2009	} else if ((ec->tx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
2010		   (ec->tx_coalesce_usecs > (I40E_MAX_ITR << 1))) {
2011			netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
2012			return -EINVAL;
2013	}
2014
2015	/* rx and tx usecs has per queue value. If user doesn't specify the queue,
2016	 * apply to all queues.
2017	 */
2018	if (queue < 0) {
2019		for (i = 0; i < vsi->num_queue_pairs; i++)
2020			i40e_set_itr_per_queue(vsi, ec, i);
2021	} else if (queue < vsi->num_queue_pairs) {
2022		i40e_set_itr_per_queue(vsi, ec, queue);
2023	} else {
2024		netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
2025			   vsi->num_queue_pairs - 1);
2026		return -EINVAL;
2027	}
2028
2029	return 0;
2030}
2031
2032static int i40e_set_coalesce(struct net_device *netdev,
2033			     struct ethtool_coalesce *ec)
2034{
2035	return __i40e_set_coalesce(netdev, ec, -1);
2036}
2037
2038static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
2039				       struct ethtool_coalesce *ec)
2040{
2041	return __i40e_set_coalesce(netdev, ec, queue);
2042}
2043
2044/**
2045 * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
2046 * @pf: pointer to the physical function struct
2047 * @cmd: ethtool rxnfc command
2048 *
2049 * Returns Success if the flow is supported, else Invalid Input.
2050 **/
2051static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
2052{
2053	cmd->data = 0;
2054
2055	if (pf->vsi[pf->lan_vsi]->rxnfc.data != 0) {
2056		cmd->data = pf->vsi[pf->lan_vsi]->rxnfc.data;
2057		cmd->flow_type = pf->vsi[pf->lan_vsi]->rxnfc.flow_type;
2058		return 0;
2059	}
2060	/* Report default options for RSS on i40e */
2061	switch (cmd->flow_type) {
2062	case TCP_V4_FLOW:
2063	case UDP_V4_FLOW:
2064		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2065	/* fall through to add IP fields */
2066	case SCTP_V4_FLOW:
2067	case AH_ESP_V4_FLOW:
2068	case AH_V4_FLOW:
2069	case ESP_V4_FLOW:
2070	case IPV4_FLOW:
2071		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
2072		break;
2073	case TCP_V6_FLOW:
2074	case UDP_V6_FLOW:
2075		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2076	/* fall through to add IP fields */
2077	case SCTP_V6_FLOW:
2078	case AH_ESP_V6_FLOW:
2079	case AH_V6_FLOW:
2080	case ESP_V6_FLOW:
2081	case IPV6_FLOW:
2082		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
2083		break;
2084	default:
2085		return -EINVAL;
2086	}
2087
2088	return 0;
2089}
2090
2091/**
2092 * i40e_get_ethtool_fdir_all - Populates the rule count of a command
2093 * @pf: Pointer to the physical function struct
2094 * @cmd: The command to get or set Rx flow classification rules
2095 * @rule_locs: Array of used rule locations
2096 *
2097 * This function populates both the total and actual rule count of
2098 * the ethtool flow classification command
2099 *
2100 * Returns 0 on success or -EMSGSIZE if entry not found
2101 **/
2102static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
2103				     struct ethtool_rxnfc *cmd,
2104				     u32 *rule_locs)
2105{
2106	struct i40e_fdir_filter *rule;
2107	struct hlist_node *node2;
2108	int cnt = 0;
2109
2110	/* report total rule count */
2111	cmd->data = i40e_get_fd_cnt_all(pf);
2112
2113	hlist_for_each_entry_safe(rule, node2,
2114				  &pf->fdir_filter_list, fdir_node) {
2115		if (cnt == cmd->rule_cnt)
2116			return -EMSGSIZE;
2117
2118		rule_locs[cnt] = rule->fd_id;
2119		cnt++;
2120	}
2121
2122	cmd->rule_cnt = cnt;
2123
2124	return 0;
2125}
2126
2127/**
2128 * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
2129 * @pf: Pointer to the physical function struct
2130 * @cmd: The command to get or set Rx flow classification rules
2131 *
2132 * This function looks up a filter based on the Rx flow classification
2133 * command and fills the flow spec info for it if found
2134 *
2135 * Returns 0 on success or -EINVAL if filter not found
2136 **/
2137static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
2138				       struct ethtool_rxnfc *cmd)
2139{
2140	struct ethtool_rx_flow_spec *fsp =
2141			(struct ethtool_rx_flow_spec *)&cmd->fs;
2142	struct i40e_fdir_filter *rule = NULL;
2143	struct hlist_node *node2;
2144
2145	hlist_for_each_entry_safe(rule, node2,
2146				  &pf->fdir_filter_list, fdir_node) {
2147		if (fsp->location <= rule->fd_id)
2148			break;
2149	}
2150
2151	if (!rule || fsp->location != rule->fd_id)
2152		return -EINVAL;
2153
2154	fsp->flow_type = rule->flow_type;
2155	if (fsp->flow_type == IP_USER_FLOW) {
2156		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
2157		fsp->h_u.usr_ip4_spec.proto = 0;
2158		fsp->m_u.usr_ip4_spec.proto = 0;
2159	}
2160
2161	/* Reverse the src and dest notion, since the HW views them from
2162	 * Tx perspective where as the user expects it from Rx filter view.
2163	 */
2164	fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
2165	fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
2166	fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip[0];
2167	fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip[0];
2168
2169	if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
2170		fsp->ring_cookie = RX_CLS_FLOW_DISC;
2171	else
2172		fsp->ring_cookie = rule->q_index;
2173
2174	if (rule->dest_vsi != pf->vsi[pf->lan_vsi]->id) {
2175		struct i40e_vsi *vsi;
2176
2177		vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
2178		if (vsi && vsi->type == I40E_VSI_SRIOV) {
2179			fsp->h_ext.data[1] = htonl(vsi->vf_id);
2180			fsp->m_ext.data[1] = htonl(0x1);
2181		}
2182	}
2183
2184	return 0;
2185}
2186
2187/**
2188 * i40e_get_rxnfc - command to get RX flow classification rules
2189 * @netdev: network interface device structure
2190 * @cmd: ethtool rxnfc command
2191 *
2192 * Returns Success if the command is supported.
2193 **/
2194static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2195			  u32 *rule_locs)
2196{
2197	struct i40e_netdev_priv *np = netdev_priv(netdev);
2198	struct i40e_vsi *vsi = np->vsi;
2199	struct i40e_pf *pf = vsi->back;
2200	int ret = -EOPNOTSUPP;
2201
2202	switch (cmd->cmd) {
2203	case ETHTOOL_GRXRINGS:
2204		cmd->data = vsi->num_queue_pairs;
2205		ret = 0;
2206		break;
2207	case ETHTOOL_GRXFH:
2208		ret = i40e_get_rss_hash_opts(pf, cmd);
2209		break;
2210	case ETHTOOL_GRXCLSRLCNT:
2211		cmd->rule_cnt = pf->fdir_pf_active_filters;
2212		/* report total rule count */
2213		cmd->data = i40e_get_fd_cnt_all(pf);
2214		ret = 0;
2215		break;
2216	case ETHTOOL_GRXCLSRULE:
2217		ret = i40e_get_ethtool_fdir_entry(pf, cmd);
2218		break;
2219	case ETHTOOL_GRXCLSRLALL:
2220		ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
2221		break;
2222	default:
2223		break;
2224	}
2225
2226	return ret;
2227}
2228
2229/**
2230 * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2231 * @pf: pointer to the physical function struct
2232 * @cmd: ethtool rxnfc command
2233 *
2234 * Returns Success if the flow input set is supported.
2235 **/
2236static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
2237{
2238	struct i40e_hw *hw = &pf->hw;
2239	u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
2240		   ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
2241
2242	/* RSS does not support anything other than hashing
2243	 * to queues on src and dst IPs and ports
2244	 */
2245	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
2246			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
2247		return -EINVAL;
2248
2249	/* We need at least the IP SRC and DEST fields for hashing */
2250	if (!(nfc->data & RXH_IP_SRC) ||
2251	    !(nfc->data & RXH_IP_DST))
2252		return -EINVAL;
2253
2254	switch (nfc->flow_type) {
2255	case TCP_V4_FLOW:
2256		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
2257		case 0:
2258			return -EINVAL;
2259		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
2260			if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2261				hena |=
2262			   BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
2263
2264			hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
2265			break;
2266		default:
2267			return -EINVAL;
2268		}
2269		break;
2270	case TCP_V6_FLOW:
2271		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
2272		case 0:
2273			return -EINVAL;
2274		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
2275			if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2276				hena |=
2277			   BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK);
2278
2279			hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
2280			break;
2281		default:
2282			return -EINVAL;
2283		}
2284		break;
2285	case UDP_V4_FLOW:
2286		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
2287		case 0:
2288			return -EINVAL;
2289		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
2290			if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2291				hena |=
2292			    BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
2293			    BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP);
2294
2295			hena |= (BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
2296				 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4));
2297			break;
2298		default:
2299			return -EINVAL;
2300		}
2301		break;
2302	case UDP_V6_FLOW:
2303		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
2304		case 0:
2305			return -EINVAL;
2306		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
2307			if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2308				hena |=
2309			    BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
2310			    BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP);
2311
2312			hena |= (BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
2313				 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6));
2314			break;
2315		default:
2316			return -EINVAL;
2317		}
2318		break;
2319	case AH_ESP_V4_FLOW:
2320	case AH_V4_FLOW:
2321	case ESP_V4_FLOW:
2322	case SCTP_V4_FLOW:
2323		if ((nfc->data & RXH_L4_B_0_1) ||
2324		    (nfc->data & RXH_L4_B_2_3))
2325			return -EINVAL;
2326		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
2327		break;
2328	case AH_ESP_V6_FLOW:
2329	case AH_V6_FLOW:
2330	case ESP_V6_FLOW:
2331	case SCTP_V6_FLOW:
2332		if ((nfc->data & RXH_L4_B_0_1) ||
2333		    (nfc->data & RXH_L4_B_2_3))
2334			return -EINVAL;
2335		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
2336		break;
2337	case IPV4_FLOW:
2338		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
2339			BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
2340		break;
2341	case IPV6_FLOW:
2342		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
2343			BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
2344		break;
2345	default:
2346		return -EINVAL;
2347	}
2348
2349	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
2350	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
2351	i40e_flush(hw);
2352
2353	/* Save setting for future output/update */
2354	pf->vsi[pf->lan_vsi]->rxnfc = *nfc;
2355
2356	return 0;
2357}
2358
2359/**
2360 * i40e_match_fdir_input_set - Match a new filter against an existing one
2361 * @rule: The filter already added
2362 * @input: The new filter to comapre against
2363 *
2364 * Returns true if the two input set match
2365 **/
2366static bool i40e_match_fdir_input_set(struct i40e_fdir_filter *rule,
2367				      struct i40e_fdir_filter *input)
2368{
2369	if ((rule->dst_ip[0] != input->dst_ip[0]) ||
2370	    (rule->src_ip[0] != input->src_ip[0]) ||
2371	    (rule->dst_port != input->dst_port) ||
2372	    (rule->src_port != input->src_port))
2373		return false;
2374	return true;
2375}
2376
2377/**
2378 * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
2379 * @vsi: Pointer to the targeted VSI
2380 * @input: The filter to update or NULL to indicate deletion
2381 * @sw_idx: Software index to the filter
2382 * @cmd: The command to get or set Rx flow classification rules
2383 *
2384 * This function updates (or deletes) a Flow Director entry from
2385 * the hlist of the corresponding PF
2386 *
2387 * Returns 0 on success
2388 **/
2389static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
2390					  struct i40e_fdir_filter *input,
2391					  u16 sw_idx,
2392					  struct ethtool_rxnfc *cmd)
2393{
2394	struct i40e_fdir_filter *rule, *parent;
2395	struct i40e_pf *pf = vsi->back;
2396	struct hlist_node *node2;
2397	int err = -EINVAL;
2398
2399	parent = NULL;
2400	rule = NULL;
2401
2402	hlist_for_each_entry_safe(rule, node2,
2403				  &pf->fdir_filter_list, fdir_node) {
2404		/* hash found, or no matching entry */
2405		if (rule->fd_id >= sw_idx)
2406			break;
2407		parent = rule;
2408	}
2409
2410	/* if there is an old rule occupying our place remove it */
2411	if (rule && (rule->fd_id == sw_idx)) {
2412		if (input && !i40e_match_fdir_input_set(rule, input))
2413			err = i40e_add_del_fdir(vsi, rule, false);
2414		else if (!input)
2415			err = i40e_add_del_fdir(vsi, rule, false);
2416		hlist_del(&rule->fdir_node);
2417		kfree(rule);
2418		pf->fdir_pf_active_filters--;
2419	}
2420
2421	/* If no input this was a delete, err should be 0 if a rule was
2422	 * successfully found and removed from the list else -EINVAL
2423	 */
2424	if (!input)
2425		return err;
2426
2427	/* initialize node and set software index */
2428	INIT_HLIST_NODE(&input->fdir_node);
2429
2430	/* add filter to the list */
2431	if (parent)
2432		hlist_add_behind(&input->fdir_node, &parent->fdir_node);
2433	else
2434		hlist_add_head(&input->fdir_node,
2435			       &pf->fdir_filter_list);
2436
2437	/* update counts */
2438	pf->fdir_pf_active_filters++;
2439
2440	return 0;
2441}
2442
2443/**
2444 * i40e_del_fdir_entry - Deletes a Flow Director filter entry
2445 * @vsi: Pointer to the targeted VSI
2446 * @cmd: The command to get or set Rx flow classification rules
2447 *
2448 * The function removes a Flow Director filter entry from the
2449 * hlist of the corresponding PF
2450 *
2451 * Returns 0 on success
2452 */
2453static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
2454			       struct ethtool_rxnfc *cmd)
2455{
2456	struct ethtool_rx_flow_spec *fsp =
2457		(struct ethtool_rx_flow_spec *)&cmd->fs;
2458	struct i40e_pf *pf = vsi->back;
2459	int ret = 0;
2460
2461	if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
2462	    test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
2463		return -EBUSY;
2464
2465	if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
2466		return -EBUSY;
2467
2468	ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
2469
2470	i40e_fdir_check_and_reenable(pf);
2471	return ret;
2472}
2473
2474/**
2475 * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
2476 * @vsi: pointer to the targeted VSI
2477 * @cmd: command to get or set RX flow classification rules
2478 *
2479 * Add Flow Director filters for a specific flow spec based on their
2480 * protocol.  Returns 0 if the filters were successfully added.
2481 **/
2482static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
2483				 struct ethtool_rxnfc *cmd)
2484{
2485	struct ethtool_rx_flow_spec *fsp;
2486	struct i40e_fdir_filter *input;
2487	struct i40e_pf *pf;
2488	int ret = -EINVAL;
2489	u16 vf_id;
2490
2491	if (!vsi)
2492		return -EINVAL;
2493
2494	pf = vsi->back;
2495
2496	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2497		return -EOPNOTSUPP;
2498
2499	if (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)
2500		return -ENOSPC;
2501
2502	if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
2503	    test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
2504		return -EBUSY;
2505
2506	if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
2507		return -EBUSY;
2508
2509	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
2510
2511	if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
2512			      pf->hw.func_caps.fd_filters_guaranteed)) {
2513		return -EINVAL;
2514	}
2515
2516	if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
2517	    (fsp->ring_cookie >= vsi->num_queue_pairs))
2518		return -EINVAL;
2519
2520	input = kzalloc(sizeof(*input), GFP_KERNEL);
2521
2522	if (!input)
2523		return -ENOMEM;
2524
2525	input->fd_id = fsp->location;
2526
2527	if (fsp->ring_cookie == RX_CLS_FLOW_DISC)
2528		input->dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
2529	else
2530		input->dest_ctl =
2531			     I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
2532
2533	input->q_index = fsp->ring_cookie;
2534	input->flex_off = 0;
2535	input->pctype = 0;
2536	input->dest_vsi = vsi->id;
2537	input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
2538	input->cnt_index  = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
2539	input->flow_type = fsp->flow_type;
2540	input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
2541
2542	/* Reverse the src and dest notion, since the HW expects them to be from
2543	 * Tx perspective where as the input from user is from Rx filter view.
2544	 */
2545	input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
2546	input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
2547	input->dst_ip[0] = fsp->h_u.tcp_ip4_spec.ip4src;
2548	input->src_ip[0] = fsp->h_u.tcp_ip4_spec.ip4dst;
2549
2550	if (ntohl(fsp->m_ext.data[1])) {
2551		if (ntohl(fsp->h_ext.data[1]) >= pf->num_alloc_vfs) {
2552			netif_info(pf, drv, vsi->netdev, "Invalid VF id\n");
2553			goto free_input;
2554		}
2555		vf_id = ntohl(fsp->h_ext.data[1]);
2556		/* Find vsi id from vf id and override dest vsi */
2557		input->dest_vsi = pf->vf[vf_id].lan_vsi_id;
2558		if (input->q_index >= pf->vf[vf_id].num_queue_pairs) {
2559			netif_info(pf, drv, vsi->netdev, "Invalid queue id\n");
2560			goto free_input;
2561		}
2562	}
2563
2564	ret = i40e_add_del_fdir(vsi, input, true);
2565free_input:
2566	if (ret)
2567		kfree(input);
2568	else
2569		i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
2570
2571	return ret;
2572}
2573
2574/**
2575 * i40e_set_rxnfc - command to set RX flow classification rules
2576 * @netdev: network interface device structure
2577 * @cmd: ethtool rxnfc command
2578 *
2579 * Returns Success if the command is supported.
2580 **/
2581static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2582{
2583	struct i40e_netdev_priv *np = netdev_priv(netdev);
2584	struct i40e_vsi *vsi = np->vsi;
2585	struct i40e_pf *pf = vsi->back;
2586	int ret = -EOPNOTSUPP;
2587
2588	switch (cmd->cmd) {
2589	case ETHTOOL_SRXFH:
2590		ret = i40e_set_rss_hash_opt(pf, cmd);
2591		break;
2592	case ETHTOOL_SRXCLSRLINS:
2593		ret = i40e_add_fdir_ethtool(vsi, cmd);
2594		break;
2595	case ETHTOOL_SRXCLSRLDEL:
2596		ret = i40e_del_fdir_entry(vsi, cmd);
2597		break;
2598	default:
2599		break;
2600	}
2601
2602	return ret;
2603}
2604
2605/**
2606 * i40e_max_channels - get Max number of combined channels supported
2607 * @vsi: vsi pointer
2608 **/
2609static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
2610{
2611	/* TODO: This code assumes DCB and FD is disabled for now. */
2612	return vsi->alloc_queue_pairs;
2613}
2614
2615/**
2616 * i40e_get_channels - Get the current channels enabled and max supported etc.
2617 * @netdev: network interface device structure
2618 * @ch: ethtool channels structure
2619 *
2620 * We don't support separate tx and rx queues as channels. The other count
2621 * represents how many queues are being used for control. max_combined counts
2622 * how many queue pairs we can support. They may not be mapped 1 to 1 with
2623 * q_vectors since we support a lot more queue pairs than q_vectors.
2624 **/
2625static void i40e_get_channels(struct net_device *dev,
2626			       struct ethtool_channels *ch)
2627{
2628	struct i40e_netdev_priv *np = netdev_priv(dev);
2629	struct i40e_vsi *vsi = np->vsi;
2630	struct i40e_pf *pf = vsi->back;
2631
2632	/* report maximum channels */
2633	ch->max_combined = i40e_max_channels(vsi);
2634
2635	/* report info for other vector */
2636	ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
2637	ch->max_other = ch->other_count;
2638
2639	/* Note: This code assumes DCB is disabled for now. */
2640	ch->combined_count = vsi->num_queue_pairs;
2641}
2642
2643/**
2644 * i40e_set_channels - Set the new channels count.
2645 * @netdev: network interface device structure
2646 * @ch: ethtool channels structure
2647 *
2648 * The new channels count may not be the same as requested by the user
2649 * since it gets rounded down to a power of 2 value.
2650 **/
2651static int i40e_set_channels(struct net_device *dev,
2652			      struct ethtool_channels *ch)
2653{
2654	struct i40e_netdev_priv *np = netdev_priv(dev);
2655	unsigned int count = ch->combined_count;
2656	struct i40e_vsi *vsi = np->vsi;
2657	struct i40e_pf *pf = vsi->back;
2658	int new_count;
2659
2660	/* We do not support setting channels for any other VSI at present */
2661	if (vsi->type != I40E_VSI_MAIN)
2662		return -EINVAL;
2663
2664	/* verify they are not requesting separate vectors */
2665	if (!count || ch->rx_count || ch->tx_count)
2666		return -EINVAL;
2667
2668	/* verify other_count has not changed */
2669	if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
2670		return -EINVAL;
2671
2672	/* verify the number of channels does not exceed hardware limits */
2673	if (count > i40e_max_channels(vsi))
2674		return -EINVAL;
2675
2676	/* update feature limits from largest to smallest supported values */
2677	/* TODO: Flow director limit, DCB etc */
2678
2679	/* use rss_reconfig to rebuild with new queue count and update traffic
2680	 * class queue mapping
2681	 */
2682	new_count = i40e_reconfig_rss_queues(pf, count);
2683	if (new_count > 0)
2684		return 0;
2685	else
2686		return -EINVAL;
2687}
2688
2689/**
2690 * i40e_get_rxfh_key_size - get the RSS hash key size
2691 * @netdev: network interface device structure
2692 *
2693 * Returns the table size.
2694 **/
2695static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
2696{
2697	return I40E_HKEY_ARRAY_SIZE;
2698}
2699
2700/**
2701 * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
2702 * @netdev: network interface device structure
2703 *
2704 * Returns the table size.
2705 **/
2706static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
2707{
2708	return I40E_HLUT_ARRAY_SIZE;
2709}
2710
2711static int i40e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
2712			 u8 *hfunc)
2713{
2714	struct i40e_netdev_priv *np = netdev_priv(netdev);
2715	struct i40e_vsi *vsi = np->vsi;
2716	u8 *lut, *seed = NULL;
2717	int ret;
2718	u16 i;
2719
2720	if (hfunc)
2721		*hfunc = ETH_RSS_HASH_TOP;
2722
2723	if (!indir)
2724		return 0;
2725
2726	seed = key;
2727	lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
2728	if (!lut)
2729		return -ENOMEM;
2730	ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
2731	if (ret)
2732		goto out;
2733	for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
2734		indir[i] = (u32)(lut[i]);
2735
2736out:
2737	kfree(lut);
2738
2739	return ret;
2740}
2741
2742/**
2743 * i40e_set_rxfh - set the rx flow hash indirection table
2744 * @netdev: network interface device structure
2745 * @indir: indirection table
2746 * @key: hash key
2747 *
2748 * Returns -EINVAL if the table specifies an invalid queue id, otherwise
2749 * returns 0 after programming the table.
2750 **/
2751static int i40e_set_rxfh(struct net_device *netdev, const u32 *indir,
2752			 const u8 *key, const u8 hfunc)
2753{
2754	struct i40e_netdev_priv *np = netdev_priv(netdev);
2755	struct i40e_vsi *vsi = np->vsi;
2756	u8 *seed = NULL;
2757	u16 i;
2758
2759	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2760		return -EOPNOTSUPP;
2761
2762	if (!indir)
2763		return 0;
2764
2765	if (key) {
2766		if (!vsi->rss_hkey_user) {
2767			vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
2768						     GFP_KERNEL);
2769			if (!vsi->rss_hkey_user)
2770				return -ENOMEM;
2771		}
2772		memcpy(vsi->rss_hkey_user, key, I40E_HKEY_ARRAY_SIZE);
2773		seed = vsi->rss_hkey_user;
2774	}
2775	if (!vsi->rss_lut_user) {
2776		vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
2777		if (!vsi->rss_lut_user)
2778			return -ENOMEM;
2779	}
2780
2781	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
2782	for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
2783		vsi->rss_lut_user[i] = (u8)(indir[i]);
2784
2785	return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
2786			       I40E_HLUT_ARRAY_SIZE);
2787}
2788
2789/**
2790 * i40e_get_priv_flags - report device private flags
2791 * @dev: network interface device structure
2792 *
2793 * The get string set count and the string set should be matched for each
2794 * flag returned.  Add new strings for each flag to the i40e_priv_flags_strings
2795 * array.
2796 *
2797 * Returns a u32 bitmap of flags.
2798 **/
2799static u32 i40e_get_priv_flags(struct net_device *dev)
2800{
2801	struct i40e_netdev_priv *np = netdev_priv(dev);
2802	struct i40e_vsi *vsi = np->vsi;
2803	struct i40e_pf *pf = vsi->back;
2804	u32 ret_flags = 0;
2805
2806	ret_flags |= pf->hw.func_caps.npar_enable ?
2807		I40E_PRIV_FLAGS_NPAR_FLAG : 0;
2808	ret_flags |= pf->flags & I40E_FLAG_LINK_POLLING_ENABLED ?
2809		I40E_PRIV_FLAGS_LINKPOLL_FLAG : 0;
2810	ret_flags |= pf->flags & I40E_FLAG_FD_ATR_ENABLED ?
2811		I40E_PRIV_FLAGS_FD_ATR : 0;
2812	ret_flags |= pf->flags & I40E_FLAG_VEB_STATS_ENABLED ?
2813		I40E_PRIV_FLAGS_VEB_STATS : 0;
2814	ret_flags |= pf->flags & I40E_FLAG_RX_PS_ENABLED ?
2815		I40E_PRIV_FLAGS_PS : 0;
2816	ret_flags |= pf->auto_disable_flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE ?
2817		0 : I40E_PRIV_FLAGS_HW_ATR_EVICT;
2818
2819	return ret_flags;
2820}
2821
2822/**
2823 * i40e_set_priv_flags - set private flags
2824 * @dev: network interface device structure
2825 * @flags: bit flags to be set
2826 **/
2827static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
2828{
2829	struct i40e_netdev_priv *np = netdev_priv(dev);
2830	struct i40e_vsi *vsi = np->vsi;
2831	struct i40e_pf *pf = vsi->back;
2832	bool reset_required = false;
2833
2834	/* NOTE: MFP is not settable */
2835
2836	/* allow the user to control the method of receive
2837	 * buffer DMA, whether the packet is split at header
2838	 * boundaries into two separate buffers.  In some cases
2839	 * one routine or the other will perform better.
2840	 */
2841	if ((flags & I40E_PRIV_FLAGS_PS) &&
2842	    !(pf->flags & I40E_FLAG_RX_PS_ENABLED)) {
2843		pf->flags |= I40E_FLAG_RX_PS_ENABLED;
2844		pf->flags &= ~I40E_FLAG_RX_1BUF_ENABLED;
2845		reset_required = true;
2846	} else if (!(flags & I40E_PRIV_FLAGS_PS) &&
2847		   (pf->flags & I40E_FLAG_RX_PS_ENABLED)) {
2848		pf->flags &= ~I40E_FLAG_RX_PS_ENABLED;
2849		pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
2850		reset_required = true;
2851	}
2852
2853	if (flags & I40E_PRIV_FLAGS_LINKPOLL_FLAG)
2854		pf->flags |= I40E_FLAG_LINK_POLLING_ENABLED;
2855	else
2856		pf->flags &= ~I40E_FLAG_LINK_POLLING_ENABLED;
2857
2858	/* allow the user to control the state of the Flow
2859	 * Director ATR (Application Targeted Routing) feature
2860	 * of the driver
2861	 */
2862	if (flags & I40E_PRIV_FLAGS_FD_ATR) {
2863		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
2864	} else {
2865		pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
2866		pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
2867	}
2868
2869	if ((flags & I40E_PRIV_FLAGS_VEB_STATS) &&
2870	    !(pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
2871		pf->flags |= I40E_FLAG_VEB_STATS_ENABLED;
2872		reset_required = true;
2873	} else if (!(flags & I40E_PRIV_FLAGS_VEB_STATS) &&
2874		   (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
2875		pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
2876		reset_required = true;
2877	}
2878
2879	if ((flags & I40E_PRIV_FLAGS_HW_ATR_EVICT) &&
2880	    (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE))
2881		pf->auto_disable_flags &= ~I40E_FLAG_HW_ATR_EVICT_CAPABLE;
2882	else
2883		pf->auto_disable_flags |= I40E_FLAG_HW_ATR_EVICT_CAPABLE;
2884
2885	/* if needed, issue reset to cause things to take effect */
2886	if (reset_required)
2887		i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
2888
2889	return 0;
2890}
2891
2892static const struct ethtool_ops i40e_ethtool_ops = {
2893	.get_settings		= i40e_get_settings,
2894	.set_settings		= i40e_set_settings,
2895	.get_drvinfo		= i40e_get_drvinfo,
2896	.get_regs_len		= i40e_get_regs_len,
2897	.get_regs		= i40e_get_regs,
2898	.nway_reset		= i40e_nway_reset,
2899	.get_link		= ethtool_op_get_link,
2900	.get_wol		= i40e_get_wol,
2901	.set_wol		= i40e_set_wol,
2902	.set_eeprom		= i40e_set_eeprom,
2903	.get_eeprom_len		= i40e_get_eeprom_len,
2904	.get_eeprom		= i40e_get_eeprom,
2905	.get_ringparam		= i40e_get_ringparam,
2906	.set_ringparam		= i40e_set_ringparam,
2907	.get_pauseparam		= i40e_get_pauseparam,
2908	.set_pauseparam		= i40e_set_pauseparam,
2909	.get_msglevel		= i40e_get_msglevel,
2910	.set_msglevel		= i40e_set_msglevel,
2911	.get_rxnfc		= i40e_get_rxnfc,
2912	.set_rxnfc		= i40e_set_rxnfc,
2913	.self_test		= i40e_diag_test,
2914	.get_strings		= i40e_get_strings,
2915	.set_phys_id		= i40e_set_phys_id,
2916	.get_sset_count		= i40e_get_sset_count,
2917	.get_ethtool_stats	= i40e_get_ethtool_stats,
2918	.get_coalesce		= i40e_get_coalesce,
2919	.set_coalesce		= i40e_set_coalesce,
2920	.get_rxfh_key_size	= i40e_get_rxfh_key_size,
2921	.get_rxfh_indir_size	= i40e_get_rxfh_indir_size,
2922	.get_rxfh		= i40e_get_rxfh,
2923	.set_rxfh		= i40e_set_rxfh,
2924	.get_channels		= i40e_get_channels,
2925	.set_channels		= i40e_set_channels,
2926	.get_ts_info		= i40e_get_ts_info,
2927	.get_priv_flags		= i40e_get_priv_flags,
2928	.set_priv_flags		= i40e_set_priv_flags,
2929	.get_per_queue_coalesce	= i40e_get_per_queue_coalesce,
2930	.set_per_queue_coalesce	= i40e_set_per_queue_coalesce,
2931};
2932
2933void i40e_set_ethtool_ops(struct net_device *netdev)
2934{
2935	netdev->ethtool_ops = &i40e_ethtool_ops;
2936}