File Index
Files and directories
Sign in
FILE

e1000_ethtool.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/intel/e1000/e1000_ethtool.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 1999 - 2006 Intel Corporation. */
   3
   4/* ethtool support for e1000 */
   5
   6#include "e1000.h"
   7#include <linux/jiffies.h>
   8#include <linux/uaccess.h>
   9
  10enum {NETDEV_STATS, E1000_STATS};
  11
  12struct e1000_stats {
  13	char stat_string[ETH_GSTRING_LEN];
  14	int type;
  15	int sizeof_stat;
  16	int stat_offset;
  17};
  18
  19#define E1000_STAT(m)		E1000_STATS, \
  20				sizeof(((struct e1000_adapter *)0)->m), \
  21				offsetof(struct e1000_adapter, m)
  22#define E1000_NETDEV_STAT(m)	NETDEV_STATS, \
  23				sizeof(((struct net_device *)0)->m), \
  24				offsetof(struct net_device, m)
  25
  26static const struct e1000_stats e1000_gstrings_stats[] = {
  27	{ "rx_packets", E1000_STAT(stats.gprc) },
  28	{ "tx_packets", E1000_STAT(stats.gptc) },
  29	{ "rx_bytes", E1000_STAT(stats.gorcl) },
  30	{ "tx_bytes", E1000_STAT(stats.gotcl) },
  31	{ "rx_broadcast", E1000_STAT(stats.bprc) },
  32	{ "tx_broadcast", E1000_STAT(stats.bptc) },
  33	{ "rx_multicast", E1000_STAT(stats.mprc) },
  34	{ "tx_multicast", E1000_STAT(stats.mptc) },
  35	{ "rx_errors", E1000_STAT(stats.rxerrc) },
  36	{ "tx_errors", E1000_STAT(stats.txerrc) },
  37	{ "tx_dropped", E1000_NETDEV_STAT(stats.tx_dropped) },
  38	{ "multicast", E1000_STAT(stats.mprc) },
  39	{ "collisions", E1000_STAT(stats.colc) },
  40	{ "rx_length_errors", E1000_STAT(stats.rlerrc) },
  41	{ "rx_over_errors", E1000_NETDEV_STAT(stats.rx_over_errors) },
  42	{ "rx_crc_errors", E1000_STAT(stats.crcerrs) },
  43	{ "rx_frame_errors", E1000_NETDEV_STAT(stats.rx_frame_errors) },
  44	{ "rx_no_buffer_count", E1000_STAT(stats.rnbc) },
  45	{ "rx_missed_errors", E1000_STAT(stats.mpc) },
  46	{ "tx_aborted_errors", E1000_STAT(stats.ecol) },
  47	{ "tx_carrier_errors", E1000_STAT(stats.tncrs) },
  48	{ "tx_fifo_errors", E1000_NETDEV_STAT(stats.tx_fifo_errors) },
  49	{ "tx_heartbeat_errors", E1000_NETDEV_STAT(stats.tx_heartbeat_errors) },
  50	{ "tx_window_errors", E1000_STAT(stats.latecol) },
  51	{ "tx_abort_late_coll", E1000_STAT(stats.latecol) },
  52	{ "tx_deferred_ok", E1000_STAT(stats.dc) },
  53	{ "tx_single_coll_ok", E1000_STAT(stats.scc) },
  54	{ "tx_multi_coll_ok", E1000_STAT(stats.mcc) },
  55	{ "tx_timeout_count", E1000_STAT(tx_timeout_count) },
  56	{ "tx_restart_queue", E1000_STAT(restart_queue) },
  57	{ "rx_long_length_errors", E1000_STAT(stats.roc) },
  58	{ "rx_short_length_errors", E1000_STAT(stats.ruc) },
  59	{ "rx_align_errors", E1000_STAT(stats.algnerrc) },
  60	{ "tx_tcp_seg_good", E1000_STAT(stats.tsctc) },
  61	{ "tx_tcp_seg_failed", E1000_STAT(stats.tsctfc) },
  62	{ "rx_flow_control_xon", E1000_STAT(stats.xonrxc) },
  63	{ "rx_flow_control_xoff", E1000_STAT(stats.xoffrxc) },
  64	{ "tx_flow_control_xon", E1000_STAT(stats.xontxc) },
  65	{ "tx_flow_control_xoff", E1000_STAT(stats.xofftxc) },
  66	{ "rx_long_byte_count", E1000_STAT(stats.gorcl) },
  67	{ "rx_csum_offload_good", E1000_STAT(hw_csum_good) },
  68	{ "rx_csum_offload_errors", E1000_STAT(hw_csum_err) },
  69	{ "alloc_rx_buff_failed", E1000_STAT(alloc_rx_buff_failed) },
  70	{ "tx_smbus", E1000_STAT(stats.mgptc) },
  71	{ "rx_smbus", E1000_STAT(stats.mgprc) },
  72	{ "dropped_smbus", E1000_STAT(stats.mgpdc) },
  73};
  74
  75#define E1000_QUEUE_STATS_LEN 0
  76#define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
  77#define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN + E1000_QUEUE_STATS_LEN)
  78static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
  79	"Register test  (offline)", "Eeprom test    (offline)",
  80	"Interrupt test (offline)", "Loopback test  (offline)",
  81	"Link test   (on/offline)"
  82};
  83
  84#define E1000_TEST_LEN	ARRAY_SIZE(e1000_gstrings_test)
  85
  86static int e1000_get_link_ksettings(struct net_device *netdev,
  87				    struct ethtool_link_ksettings *cmd)
  88{
  89	struct e1000_adapter *adapter = netdev_priv(netdev);
  90	struct e1000_hw *hw = &adapter->hw;
  91	u32 supported, advertising;
  92
  93	if (hw->media_type == e1000_media_type_copper) {
  94		supported = (SUPPORTED_10baseT_Half |
  95			     SUPPORTED_10baseT_Full |
  96			     SUPPORTED_100baseT_Half |
  97			     SUPPORTED_100baseT_Full |
  98			     SUPPORTED_1000baseT_Full|
  99			     SUPPORTED_Autoneg |
 100			     SUPPORTED_TP);
 101		advertising = ADVERTISED_TP;
 102
 103		if (hw->autoneg == 1) {
 104			advertising |= ADVERTISED_Autoneg;
 105			/* the e1000 autoneg seems to match ethtool nicely */
 106			advertising |= hw->autoneg_advertised;
 107		}
 108
 109		cmd->base.port = PORT_TP;
 110		cmd->base.phy_address = hw->phy_addr;
 111	} else {
 112		supported   = (SUPPORTED_1000baseT_Full |
 113			       SUPPORTED_FIBRE |
 114			       SUPPORTED_Autoneg);
 115
 116		advertising = (ADVERTISED_1000baseT_Full |
 117			       ADVERTISED_FIBRE |
 118			       ADVERTISED_Autoneg);
 119
 120		cmd->base.port = PORT_FIBRE;
 121	}
 122
 123	if (er32(STATUS) & E1000_STATUS_LU) {
 124		e1000_get_speed_and_duplex(hw, &adapter->link_speed,
 125					   &adapter->link_duplex);
 126		cmd->base.speed = adapter->link_speed;
 127
 128		/* unfortunately FULL_DUPLEX != DUPLEX_FULL
 129		 * and HALF_DUPLEX != DUPLEX_HALF
 130		 */
 131		if (adapter->link_duplex == FULL_DUPLEX)
 132			cmd->base.duplex = DUPLEX_FULL;
 133		else
 134			cmd->base.duplex = DUPLEX_HALF;
 135	} else {
 136		cmd->base.speed = SPEED_UNKNOWN;
 137		cmd->base.duplex = DUPLEX_UNKNOWN;
 138	}
 139
 140	cmd->base.autoneg = ((hw->media_type == e1000_media_type_fiber) ||
 141			 hw->autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
 142
 143	/* MDI-X => 1; MDI => 0 */
 144	if ((hw->media_type == e1000_media_type_copper) &&
 145	    netif_carrier_ok(netdev))
 146		cmd->base.eth_tp_mdix = (!!adapter->phy_info.mdix_mode ?
 147				     ETH_TP_MDI_X : ETH_TP_MDI);
 148	else
 149		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
 150
 151	if (hw->mdix == AUTO_ALL_MODES)
 152		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
 153	else
 154		cmd->base.eth_tp_mdix_ctrl = hw->mdix;
 155
 156	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
 157						supported);
 158	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
 159						advertising);
 160
 161	return 0;
 162}
 163
 164static int e1000_set_link_ksettings(struct net_device *netdev,
 165				    const struct ethtool_link_ksettings *cmd)
 166{
 167	struct e1000_adapter *adapter = netdev_priv(netdev);
 168	struct e1000_hw *hw = &adapter->hw;
 169	u32 advertising;
 170
 171	ethtool_convert_link_mode_to_legacy_u32(&advertising,
 172						cmd->link_modes.advertising);
 173
 174	/* MDI setting is only allowed when autoneg enabled because
 175	 * some hardware doesn't allow MDI setting when speed or
 176	 * duplex is forced.
 177	 */
 178	if (cmd->base.eth_tp_mdix_ctrl) {
 179		if (hw->media_type != e1000_media_type_copper)
 180			return -EOPNOTSUPP;
 181
 182		if ((cmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO) &&
 183		    (cmd->base.autoneg != AUTONEG_ENABLE)) {
 184			e_err(drv, "forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n");
 185			return -EINVAL;
 186		}
 187	}
 188
 189	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
 190		msleep(1);
 191
 192	if (cmd->base.autoneg == AUTONEG_ENABLE) {
 193		hw->autoneg = 1;
 194		if (hw->media_type == e1000_media_type_fiber)
 195			hw->autoneg_advertised = ADVERTISED_1000baseT_Full |
 196						 ADVERTISED_FIBRE |
 197						 ADVERTISED_Autoneg;
 198		else
 199			hw->autoneg_advertised = advertising |
 200						 ADVERTISED_TP |
 201						 ADVERTISED_Autoneg;
 202	} else {
 203		u32 speed = cmd->base.speed;
 204		/* calling this overrides forced MDI setting */
 205		if (e1000_set_spd_dplx(adapter, speed, cmd->base.duplex)) {
 206			clear_bit(__E1000_RESETTING, &adapter->flags);
 207			return -EINVAL;
 208		}
 209	}
 210
 211	/* MDI-X => 2; MDI => 1; Auto => 3 */
 212	if (cmd->base.eth_tp_mdix_ctrl) {
 213		if (cmd->base.eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
 214			hw->mdix = AUTO_ALL_MODES;
 215		else
 216			hw->mdix = cmd->base.eth_tp_mdix_ctrl;
 217	}
 218
 219	/* reset the link */
 220
 221	if (netif_running(adapter->netdev)) {
 222		e1000_down(adapter);
 223		e1000_up(adapter);
 224	} else {
 225		e1000_reset(adapter);
 226	}
 227	clear_bit(__E1000_RESETTING, &adapter->flags);
 228	return 0;
 229}
 230
 231static u32 e1000_get_link(struct net_device *netdev)
 232{
 233	struct e1000_adapter *adapter = netdev_priv(netdev);
 234
 235	/* If the link is not reported up to netdev, interrupts are disabled,
 236	 * and so the physical link state may have changed since we last
 237	 * looked. Set get_link_status to make sure that the true link
 238	 * state is interrogated, rather than pulling a cached and possibly
 239	 * stale link state from the driver.
 240	 */
 241	if (!netif_carrier_ok(netdev))
 242		adapter->hw.get_link_status = 1;
 243
 244	return e1000_has_link(adapter);
 245}
 246
 247static void e1000_get_pauseparam(struct net_device *netdev,
 248				 struct ethtool_pauseparam *pause)
 249{
 250	struct e1000_adapter *adapter = netdev_priv(netdev);
 251	struct e1000_hw *hw = &adapter->hw;
 252
 253	pause->autoneg =
 254		(adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
 255
 256	if (hw->fc == E1000_FC_RX_PAUSE) {
 257		pause->rx_pause = 1;
 258	} else if (hw->fc == E1000_FC_TX_PAUSE) {
 259		pause->tx_pause = 1;
 260	} else if (hw->fc == E1000_FC_FULL) {
 261		pause->rx_pause = 1;
 262		pause->tx_pause = 1;
 263	}
 264}
 265
 266static int e1000_set_pauseparam(struct net_device *netdev,
 267				struct ethtool_pauseparam *pause)
 268{
 269	struct e1000_adapter *adapter = netdev_priv(netdev);
 270	struct e1000_hw *hw = &adapter->hw;
 271	int retval = 0;
 272
 273	adapter->fc_autoneg = pause->autoneg;
 274
 275	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
 276		msleep(1);
 277
 278	if (pause->rx_pause && pause->tx_pause)
 279		hw->fc = E1000_FC_FULL;
 280	else if (pause->rx_pause && !pause->tx_pause)
 281		hw->fc = E1000_FC_RX_PAUSE;
 282	else if (!pause->rx_pause && pause->tx_pause)
 283		hw->fc = E1000_FC_TX_PAUSE;
 284	else if (!pause->rx_pause && !pause->tx_pause)
 285		hw->fc = E1000_FC_NONE;
 286
 287	hw->original_fc = hw->fc;
 288
 289	if (adapter->fc_autoneg == AUTONEG_ENABLE) {
 290		if (netif_running(adapter->netdev)) {
 291			e1000_down(adapter);
 292			e1000_up(adapter);
 293		} else {
 294			e1000_reset(adapter);
 295		}
 296	} else
 297		retval = ((hw->media_type == e1000_media_type_fiber) ?
 298			  e1000_setup_link(hw) : e1000_force_mac_fc(hw));
 299
 300	clear_bit(__E1000_RESETTING, &adapter->flags);
 301	return retval;
 302}
 303
 304static u32 e1000_get_msglevel(struct net_device *netdev)
 305{
 306	struct e1000_adapter *adapter = netdev_priv(netdev);
 307
 308	return adapter->msg_enable;
 309}
 310
 311static void e1000_set_msglevel(struct net_device *netdev, u32 data)
 312{
 313	struct e1000_adapter *adapter = netdev_priv(netdev);
 314
 315	adapter->msg_enable = data;
 316}
 317
 318static int e1000_get_regs_len(struct net_device *netdev)
 319{
 320#define E1000_REGS_LEN 32
 321	return E1000_REGS_LEN * sizeof(u32);
 322}
 323
 324static void e1000_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
 325			   void *p)
 326{
 327	struct e1000_adapter *adapter = netdev_priv(netdev);
 328	struct e1000_hw *hw = &adapter->hw;
 329	u32 *regs_buff = p;
 330	u16 phy_data;
 331
 332	memset(p, 0, E1000_REGS_LEN * sizeof(u32));
 333
 334	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
 335
 336	regs_buff[0]  = er32(CTRL);
 337	regs_buff[1]  = er32(STATUS);
 338
 339	regs_buff[2]  = er32(RCTL);
 340	regs_buff[3]  = er32(RDLEN);
 341	regs_buff[4]  = er32(RDH);
 342	regs_buff[5]  = er32(RDT);
 343	regs_buff[6]  = er32(RDTR);
 344
 345	regs_buff[7]  = er32(TCTL);
 346	regs_buff[8]  = er32(TDLEN);
 347	regs_buff[9]  = er32(TDH);
 348	regs_buff[10] = er32(TDT);
 349	regs_buff[11] = er32(TIDV);
 350
 351	regs_buff[12] = hw->phy_type;  /* PHY type (IGP=1, M88=0) */
 352	if (hw->phy_type == e1000_phy_igp) {
 353		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
 354				    IGP01E1000_PHY_AGC_A);
 355		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_A &
 356				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 357		regs_buff[13] = (u32)phy_data; /* cable length */
 358		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
 359				    IGP01E1000_PHY_AGC_B);
 360		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_B &
 361				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 362		regs_buff[14] = (u32)phy_data; /* cable length */
 363		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
 364				    IGP01E1000_PHY_AGC_C);
 365		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_C &
 366				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 367		regs_buff[15] = (u32)phy_data; /* cable length */
 368		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
 369				    IGP01E1000_PHY_AGC_D);
 370		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_D &
 371				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 372		regs_buff[16] = (u32)phy_data; /* cable length */
 373		regs_buff[17] = 0; /* extended 10bt distance (not needed) */
 374		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
 375		e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS &
 376				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 377		regs_buff[18] = (u32)phy_data; /* cable polarity */
 378		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
 379				    IGP01E1000_PHY_PCS_INIT_REG);
 380		e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG &
 381				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
 382		regs_buff[19] = (u32)phy_data; /* cable polarity */
 383		regs_buff[20] = 0; /* polarity correction enabled (always) */
 384		regs_buff[22] = 0; /* phy receive errors (unavailable) */
 385		regs_buff[23] = regs_buff[18]; /* mdix mode */
 386		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
 387	} else {
 388		e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
 389		regs_buff[13] = (u32)phy_data; /* cable length */
 390		regs_buff[14] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
 391		regs_buff[15] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
 392		regs_buff[16] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
 393		e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
 394		regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
 395		regs_buff[18] = regs_buff[13]; /* cable polarity */
 396		regs_buff[19] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
 397		regs_buff[20] = regs_buff[17]; /* polarity correction */
 398		/* phy receive errors */
 399		regs_buff[22] = adapter->phy_stats.receive_errors;
 400		regs_buff[23] = regs_buff[13]; /* mdix mode */
 401	}
 402	regs_buff[21] = adapter->phy_stats.idle_errors;  /* phy idle errors */
 403	e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
 404	regs_buff[24] = (u32)phy_data;  /* phy local receiver status */
 405	regs_buff[25] = regs_buff[24];  /* phy remote receiver status */
 406	if (hw->mac_type >= e1000_82540 &&
 407	    hw->media_type == e1000_media_type_copper) {
 408		regs_buff[26] = er32(MANC);
 409	}
 410}
 411
 412static int e1000_get_eeprom_len(struct net_device *netdev)
 413{
 414	struct e1000_adapter *adapter = netdev_priv(netdev);
 415	struct e1000_hw *hw = &adapter->hw;
 416
 417	return hw->eeprom.word_size * 2;
 418}
 419
 420static int e1000_get_eeprom(struct net_device *netdev,
 421			    struct ethtool_eeprom *eeprom, u8 *bytes)
 422{
 423	struct e1000_adapter *adapter = netdev_priv(netdev);
 424	struct e1000_hw *hw = &adapter->hw;
 425	u16 *eeprom_buff;
 426	int first_word, last_word;
 427	int ret_val = 0;
 428	u16 i;
 429
 430	if (eeprom->len == 0)
 431		return -EINVAL;
 432
 433	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
 434
 435	first_word = eeprom->offset >> 1;
 436	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
 437
 438	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
 439				    GFP_KERNEL);
 440	if (!eeprom_buff)
 441		return -ENOMEM;
 442
 443	if (hw->eeprom.type == e1000_eeprom_spi)
 444		ret_val = e1000_read_eeprom(hw, first_word,
 445					    last_word - first_word + 1,
 446					    eeprom_buff);
 447	else {
 448		for (i = 0; i < last_word - first_word + 1; i++) {
 449			ret_val = e1000_read_eeprom(hw, first_word + i, 1,
 450						    &eeprom_buff[i]);
 451			if (ret_val)
 452				break;
 453		}
 454	}
 455
 456	/* Device's eeprom is always little-endian, word addressable */
 457	for (i = 0; i < last_word - first_word + 1; i++)
 458		le16_to_cpus(&eeprom_buff[i]);
 459
 460	memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1),
 461	       eeprom->len);
 462	kfree(eeprom_buff);
 463
 464	return ret_val;
 465}
 466
 467static int e1000_set_eeprom(struct net_device *netdev,
 468			    struct ethtool_eeprom *eeprom, u8 *bytes)
 469{
 470	struct e1000_adapter *adapter = netdev_priv(netdev);
 471	struct e1000_hw *hw = &adapter->hw;
 472	u16 *eeprom_buff;
 473	void *ptr;
 474	int max_len, first_word, last_word, ret_val = 0;
 475	u16 i;
 476
 477	if (eeprom->len == 0)
 478		return -EOPNOTSUPP;
 479
 480	if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
 481		return -EFAULT;
 482
 483	max_len = hw->eeprom.word_size * 2;
 484
 485	first_word = eeprom->offset >> 1;
 486	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
 487	eeprom_buff = kmalloc(max_len, GFP_KERNEL);
 488	if (!eeprom_buff)
 489		return -ENOMEM;
 490
 491	ptr = (void *)eeprom_buff;
 492
 493	if (eeprom->offset & 1) {
 494		/* need read/modify/write of first changed EEPROM word
 495		 * only the second byte of the word is being modified
 496		 */
 497		ret_val = e1000_read_eeprom(hw, first_word, 1,
 498					    &eeprom_buff[0]);
 499		if (ret_val)
 500			goto out;
 501
 502		ptr++;
 503	}
 504	if ((eeprom->offset + eeprom->len) & 1) {
 505		/* need read/modify/write of last changed EEPROM word
 506		 * only the first byte of the word is being modified
 507		 */
 508		ret_val = e1000_read_eeprom(hw, last_word, 1,
 509					    &eeprom_buff[last_word - first_word]);
 510		if (ret_val)
 511			goto out;
 512	}
 513
 514	/* Device's eeprom is always little-endian, word addressable */
 515	for (i = 0; i < last_word - first_word + 1; i++)
 516		le16_to_cpus(&eeprom_buff[i]);
 517
 518	memcpy(ptr, bytes, eeprom->len);
 519
 520	for (i = 0; i < last_word - first_word + 1; i++)
 521		cpu_to_le16s(&eeprom_buff[i]);
 522
 523	ret_val = e1000_write_eeprom(hw, first_word,
 524				     last_word - first_word + 1, eeprom_buff);
 525
 526	/* Update the checksum over the first part of the EEPROM if needed */
 527	if ((ret_val == 0) && (first_word <= EEPROM_CHECKSUM_REG))
 528		e1000_update_eeprom_checksum(hw);
 529
 530out:
 531	kfree(eeprom_buff);
 532	return ret_val;
 533}
 534
 535static void e1000_get_drvinfo(struct net_device *netdev,
 536			      struct ethtool_drvinfo *drvinfo)
 537{
 538	struct e1000_adapter *adapter = netdev_priv(netdev);
 539
 540	strscpy(drvinfo->driver,  e1000_driver_name,
 541		sizeof(drvinfo->driver));
 542
 543	strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
 544		sizeof(drvinfo->bus_info));
 545}
 546
 547static void e1000_get_ringparam(struct net_device *netdev,
 548				struct ethtool_ringparam *ring,
 549				struct kernel_ethtool_ringparam *kernel_ring,
 550				struct netlink_ext_ack *extack)
 551{
 552	struct e1000_adapter *adapter = netdev_priv(netdev);
 553	struct e1000_hw *hw = &adapter->hw;
 554	e1000_mac_type mac_type = hw->mac_type;
 555	struct e1000_tx_ring *txdr = adapter->tx_ring;
 556	struct e1000_rx_ring *rxdr = adapter->rx_ring;
 557
 558	ring->rx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_RXD :
 559		E1000_MAX_82544_RXD;
 560	ring->tx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_TXD :
 561		E1000_MAX_82544_TXD;
 562	ring->rx_pending = rxdr->count;
 563	ring->tx_pending = txdr->count;
 564}
 565
 566static int e1000_set_ringparam(struct net_device *netdev,
 567			       struct ethtool_ringparam *ring,
 568			       struct kernel_ethtool_ringparam *kernel_ring,
 569			       struct netlink_ext_ack *extack)
 570{
 571	struct e1000_adapter *adapter = netdev_priv(netdev);
 572	struct e1000_hw *hw = &adapter->hw;
 573	e1000_mac_type mac_type = hw->mac_type;
 574	struct e1000_tx_ring *txdr, *tx_old;
 575	struct e1000_rx_ring *rxdr, *rx_old;
 576	int i, err;
 577
 578	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
 579		return -EINVAL;
 580
 581	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
 582		msleep(1);
 583
 584	if (netif_running(adapter->netdev))
 585		e1000_down(adapter);
 586
 587	tx_old = adapter->tx_ring;
 588	rx_old = adapter->rx_ring;
 589
 590	err = -ENOMEM;
 591	txdr = kcalloc(adapter->num_tx_queues, sizeof(struct e1000_tx_ring),
 592		       GFP_KERNEL);
 593	if (!txdr)
 594		goto err_alloc_tx;
 595
 596	rxdr = kcalloc(adapter->num_rx_queues, sizeof(struct e1000_rx_ring),
 597		       GFP_KERNEL);
 598	if (!rxdr)
 599		goto err_alloc_rx;
 600
 601	adapter->tx_ring = txdr;
 602	adapter->rx_ring = rxdr;
 603
 604	rxdr->count = max(ring->rx_pending, (u32)E1000_MIN_RXD);
 605	rxdr->count = min(rxdr->count, (u32)(mac_type < e1000_82544 ?
 606			  E1000_MAX_RXD : E1000_MAX_82544_RXD));
 607	rxdr->count = ALIGN(rxdr->count, REQ_RX_DESCRIPTOR_MULTIPLE);
 608	txdr->count = max(ring->tx_pending, (u32)E1000_MIN_TXD);
 609	txdr->count = min(txdr->count, (u32)(mac_type < e1000_82544 ?
 610			  E1000_MAX_TXD : E1000_MAX_82544_TXD));
 611	txdr->count = ALIGN(txdr->count, REQ_TX_DESCRIPTOR_MULTIPLE);
 612
 613	for (i = 0; i < adapter->num_tx_queues; i++)
 614		txdr[i].count = txdr->count;
 615	for (i = 0; i < adapter->num_rx_queues; i++)
 616		rxdr[i].count = rxdr->count;
 617
 618	err = 0;
 619	if (netif_running(adapter->netdev)) {
 620		/* Try to get new resources before deleting old */
 621		err = e1000_setup_all_rx_resources(adapter);
 622		if (err)
 623			goto err_setup_rx;
 624		err = e1000_setup_all_tx_resources(adapter);
 625		if (err)
 626			goto err_setup_tx;
 627
 628		/* save the new, restore the old in order to free it,
 629		 * then restore the new back again
 630		 */
 631
 632		adapter->rx_ring = rx_old;
 633		adapter->tx_ring = tx_old;
 634		e1000_free_all_rx_resources(adapter);
 635		e1000_free_all_tx_resources(adapter);
 636		adapter->rx_ring = rxdr;
 637		adapter->tx_ring = txdr;
 638		err = e1000_up(adapter);
 639	}
 640	kfree(tx_old);
 641	kfree(rx_old);
 642
 643	clear_bit(__E1000_RESETTING, &adapter->flags);
 644	return err;
 645
 646err_setup_tx:
 647	e1000_free_all_rx_resources(adapter);
 648err_setup_rx:
 649	adapter->rx_ring = rx_old;
 650	adapter->tx_ring = tx_old;
 651	kfree(rxdr);
 652err_alloc_rx:
 653	kfree(txdr);
 654err_alloc_tx:
 655	if (netif_running(adapter->netdev))
 656		e1000_up(adapter);
 657	clear_bit(__E1000_RESETTING, &adapter->flags);
 658	return err;
 659}
 660
 661static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data, int reg,
 662			     u32 mask, u32 write)
 663{
 664	struct e1000_hw *hw = &adapter->hw;
 665	static const u32 test[] = {
 666		0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
 667	};
 668	u8 __iomem *address = hw->hw_addr + reg;
 669	u32 read;
 670	int i;
 671
 672	for (i = 0; i < ARRAY_SIZE(test); i++) {
 673		writel(write & test[i], address);
 674		read = readl(address);
 675		if (read != (write & test[i] & mask)) {
 676			e_err(drv, "pattern test reg %04X failed: "
 677			      "got 0x%08X expected 0x%08X\n",
 678			      reg, read, (write & test[i] & mask));
 679			*data = reg;
 680			return true;
 681		}
 682	}
 683	return false;
 684}
 685
 686static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data, int reg,
 687			      u32 mask, u32 write)
 688{
 689	struct e1000_hw *hw = &adapter->hw;
 690	u8 __iomem *address = hw->hw_addr + reg;
 691	u32 read;
 692
 693	writel(write & mask, address);
 694	read = readl(address);
 695	if ((read & mask) != (write & mask)) {
 696		e_err(drv, "set/check reg %04X test failed: "
 697		      "got 0x%08X expected 0x%08X\n",
 698		      reg, (read & mask), (write & mask));
 699		*data = reg;
 700		return true;
 701	}
 702	return false;
 703}
 704
 705#define REG_PATTERN_TEST(reg, mask, write)			     \
 706	do {							     \
 707		if (reg_pattern_test(adapter, data,		     \
 708			     (hw->mac_type >= e1000_82543)   \
 709			     ? E1000_##reg : E1000_82542_##reg,	     \
 710			     mask, write))			     \
 711			return 1;				     \
 712	} while (0)
 713
 714#define REG_SET_AND_CHECK(reg, mask, write)			     \
 715	do {							     \
 716		if (reg_set_and_check(adapter, data,		     \
 717			      (hw->mac_type >= e1000_82543)  \
 718			      ? E1000_##reg : E1000_82542_##reg,     \
 719			      mask, write))			     \
 720			return 1;				     \
 721	} while (0)
 722
 723static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
 724{
 725	u32 value, before, after;
 726	u32 i, toggle;
 727	struct e1000_hw *hw = &adapter->hw;
 728
 729	/* The status register is Read Only, so a write should fail.
 730	 * Some bits that get toggled are ignored.
 731	 */
 732
 733	/* there are several bits on newer hardware that are r/w */
 734	toggle = 0xFFFFF833;
 735
 736	before = er32(STATUS);
 737	value = (er32(STATUS) & toggle);
 738	ew32(STATUS, toggle);
 739	after = er32(STATUS) & toggle;
 740	if (value != after) {
 741		e_err(drv, "failed STATUS register test got: "
 742		      "0x%08X expected: 0x%08X\n", after, value);
 743		*data = 1;
 744		return 1;
 745	}
 746	/* restore previous status */
 747	ew32(STATUS, before);
 748
 749	REG_PATTERN_TEST(FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
 750	REG_PATTERN_TEST(FCAH, 0x0000FFFF, 0xFFFFFFFF);
 751	REG_PATTERN_TEST(FCT, 0x0000FFFF, 0xFFFFFFFF);
 752	REG_PATTERN_TEST(VET, 0x0000FFFF, 0xFFFFFFFF);
 753
 754	REG_PATTERN_TEST(RDTR, 0x0000FFFF, 0xFFFFFFFF);
 755	REG_PATTERN_TEST(RDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
 756	REG_PATTERN_TEST(RDLEN, 0x000FFF80, 0x000FFFFF);
 757	REG_PATTERN_TEST(RDH, 0x0000FFFF, 0x0000FFFF);
 758	REG_PATTERN_TEST(RDT, 0x0000FFFF, 0x0000FFFF);
 759	REG_PATTERN_TEST(FCRTH, 0x0000FFF8, 0x0000FFF8);
 760	REG_PATTERN_TEST(FCTTV, 0x0000FFFF, 0x0000FFFF);
 761	REG_PATTERN_TEST(TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
 762	REG_PATTERN_TEST(TDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
 763	REG_PATTERN_TEST(TDLEN, 0x000FFF80, 0x000FFFFF);
 764
 765	REG_SET_AND_CHECK(RCTL, 0xFFFFFFFF, 0x00000000);
 766
 767	before = 0x06DFB3FE;
 768	REG_SET_AND_CHECK(RCTL, before, 0x003FFFFB);
 769	REG_SET_AND_CHECK(TCTL, 0xFFFFFFFF, 0x00000000);
 770
 771	if (hw->mac_type >= e1000_82543) {
 772		REG_SET_AND_CHECK(RCTL, before, 0xFFFFFFFF);
 773		REG_PATTERN_TEST(RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
 774		REG_PATTERN_TEST(TXCW, 0xC000FFFF, 0x0000FFFF);
 775		REG_PATTERN_TEST(TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
 776		REG_PATTERN_TEST(TIDV, 0x0000FFFF, 0x0000FFFF);
 777		value = E1000_RAR_ENTRIES;
 778		for (i = 0; i < value; i++) {
 779			REG_PATTERN_TEST(RA + (((i << 1) + 1) << 2),
 780					 0x8003FFFF, 0xFFFFFFFF);
 781		}
 782	} else {
 783		REG_SET_AND_CHECK(RCTL, 0xFFFFFFFF, 0x01FFFFFF);
 784		REG_PATTERN_TEST(RDBAL, 0xFFFFF000, 0xFFFFFFFF);
 785		REG_PATTERN_TEST(TXCW, 0x0000FFFF, 0x0000FFFF);
 786		REG_PATTERN_TEST(TDBAL, 0xFFFFF000, 0xFFFFFFFF);
 787	}
 788
 789	value = E1000_MC_TBL_SIZE;
 790	for (i = 0; i < value; i++)
 791		REG_PATTERN_TEST(MTA + (i << 2), 0xFFFFFFFF, 0xFFFFFFFF);
 792
 793	*data = 0;
 794	return 0;
 795}
 796
 797static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
 798{
 799	struct e1000_hw *hw = &adapter->hw;
 800	u16 temp;
 801	u16 checksum = 0;
 802	u16 i;
 803
 804	*data = 0;
 805	/* Read and add up the contents of the EEPROM */
 806	for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
 807		if ((e1000_read_eeprom(hw, i, 1, &temp)) < 0) {
 808			*data = 1;
 809			break;
 810		}
 811		checksum += temp;
 812	}
 813
 814	/* If Checksum is not Correct return error else test passed */
 815	if (checksum != EEPROM_SUM && !(*data))
 816		*data = 2;
 817
 818	return *data;
 819}
 820
 821static irqreturn_t e1000_test_intr(int irq, void *data)
 822{
 823	struct net_device *netdev = (struct net_device *)data;
 824	struct e1000_adapter *adapter = netdev_priv(netdev);
 825	struct e1000_hw *hw = &adapter->hw;
 826
 827	adapter->test_icr |= er32(ICR);
 828
 829	return IRQ_HANDLED;
 830}
 831
 832static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
 833{
 834	struct net_device *netdev = adapter->netdev;
 835	u32 mask, i = 0;
 836	bool shared_int = true;
 837	u32 irq = adapter->pdev->irq;
 838	struct e1000_hw *hw = &adapter->hw;
 839
 840	*data = 0;
 841
 842	/* NOTE: we don't test MSI interrupts here, yet
 843	 * Hook up test interrupt handler just for this test
 844	 */
 845	if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
 846			 netdev))
 847		shared_int = false;
 848	else if (request_irq(irq, e1000_test_intr, IRQF_SHARED,
 849			     netdev->name, netdev)) {
 850		*data = 1;
 851		return -1;
 852	}
 853	e_info(hw, "testing %s interrupt\n", (shared_int ?
 854	       "shared" : "unshared"));
 855
 856	/* Disable all the interrupts */
 857	ew32(IMC, 0xFFFFFFFF);
 858	E1000_WRITE_FLUSH();
 859	msleep(10);
 860
 861	/* Test each interrupt */
 862	for (; i < 10; i++) {
 863		/* Interrupt to test */
 864		mask = 1 << i;
 865
 866		if (!shared_int) {
 867			/* Disable the interrupt to be reported in
 868			 * the cause register and then force the same
 869			 * interrupt and see if one gets posted.  If
 870			 * an interrupt was posted to the bus, the
 871			 * test failed.
 872			 */
 873			adapter->test_icr = 0;
 874			ew32(IMC, mask);
 875			ew32(ICS, mask);
 876			E1000_WRITE_FLUSH();
 877			msleep(10);
 878
 879			if (adapter->test_icr & mask) {
 880				*data = 3;
 881				break;
 882			}
 883		}
 884
 885		/* Enable the interrupt to be reported in
 886		 * the cause register and then force the same
 887		 * interrupt and see if one gets posted.  If
 888		 * an interrupt was not posted to the bus, the
 889		 * test failed.
 890		 */
 891		adapter->test_icr = 0;
 892		ew32(IMS, mask);
 893		ew32(ICS, mask);
 894		E1000_WRITE_FLUSH();
 895		msleep(10);
 896
 897		if (!(adapter->test_icr & mask)) {
 898			*data = 4;
 899			break;
 900		}
 901
 902		if (!shared_int) {
 903			/* Disable the other interrupts to be reported in
 904			 * the cause register and then force the other
 905			 * interrupts and see if any get posted.  If
 906			 * an interrupt was posted to the bus, the
 907			 * test failed.
 908			 */
 909			adapter->test_icr = 0;
 910			ew32(IMC, ~mask & 0x00007FFF);
 911			ew32(ICS, ~mask & 0x00007FFF);
 912			E1000_WRITE_FLUSH();
 913			msleep(10);
 914
 915			if (adapter->test_icr) {
 916				*data = 5;
 917				break;
 918			}
 919		}
 920	}
 921
 922	/* Disable all the interrupts */
 923	ew32(IMC, 0xFFFFFFFF);
 924	E1000_WRITE_FLUSH();
 925	msleep(10);
 926
 927	/* Unhook test interrupt handler */
 928	free_irq(irq, netdev);
 929
 930	return *data;
 931}
 932
 933static void e1000_free_desc_rings(struct e1000_adapter *adapter)
 934{
 935	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
 936	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
 937	struct pci_dev *pdev = adapter->pdev;
 938	int i;
 939
 940	if (txdr->desc && txdr->buffer_info) {
 941		for (i = 0; i < txdr->count; i++) {
 942			if (txdr->buffer_info[i].dma)
 943				dma_unmap_single(&pdev->dev,
 944						 txdr->buffer_info[i].dma,
 945						 txdr->buffer_info[i].length,
 946						 DMA_TO_DEVICE);
 947			dev_kfree_skb(txdr->buffer_info[i].skb);
 948		}
 949	}
 950
 951	if (rxdr->desc && rxdr->buffer_info) {
 952		for (i = 0; i < rxdr->count; i++) {
 953			if (rxdr->buffer_info[i].dma)
 954				dma_unmap_single(&pdev->dev,
 955						 rxdr->buffer_info[i].dma,
 956						 E1000_RXBUFFER_2048,
 957						 DMA_FROM_DEVICE);
 958			kfree(rxdr->buffer_info[i].rxbuf.data);
 959		}
 960	}
 961
 962	if (txdr->desc) {
 963		dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
 964				  txdr->dma);
 965		txdr->desc = NULL;
 966	}
 967	if (rxdr->desc) {
 968		dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
 969				  rxdr->dma);
 970		rxdr->desc = NULL;
 971	}
 972
 973	kfree(txdr->buffer_info);
 974	txdr->buffer_info = NULL;
 975	kfree(rxdr->buffer_info);
 976	rxdr->buffer_info = NULL;
 977}
 978
 979static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
 980{
 981	struct e1000_hw *hw = &adapter->hw;
 982	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
 983	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
 984	struct pci_dev *pdev = adapter->pdev;
 985	u32 rctl;
 986	int i, ret_val;
 987
 988	/* Setup Tx descriptor ring and Tx buffers */
 989
 990	if (!txdr->count)
 991		txdr->count = E1000_DEFAULT_TXD;
 992
 993	txdr->buffer_info = kcalloc(txdr->count, sizeof(struct e1000_tx_buffer),
 994				    GFP_KERNEL);
 995	if (!txdr->buffer_info) {
 996		ret_val = 1;
 997		goto err_nomem;
 998	}
 999
1000	txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1001	txdr->size = ALIGN(txdr->size, 4096);
1002	txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1003					GFP_KERNEL);
1004	if (!txdr->desc) {
1005		ret_val = 2;
1006		goto err_nomem;
1007	}
1008	txdr->next_to_use = txdr->next_to_clean = 0;
1009
1010	ew32(TDBAL, ((u64)txdr->dma & 0x00000000FFFFFFFF));
1011	ew32(TDBAH, ((u64)txdr->dma >> 32));
1012	ew32(TDLEN, txdr->count * sizeof(struct e1000_tx_desc));
1013	ew32(TDH, 0);
1014	ew32(TDT, 0);
1015	ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN |
1016	     E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1017	     E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT);
1018
1019	for (i = 0; i < txdr->count; i++) {
1020		struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*txdr, i);
1021		struct sk_buff *skb;
1022		unsigned int size = 1024;
1023
1024		skb = alloc_skb(size, GFP_KERNEL);
1025		if (!skb) {
1026			ret_val = 3;
1027			goto err_nomem;
1028		}
1029		skb_put(skb, size);
1030		txdr->buffer_info[i].skb = skb;
1031		txdr->buffer_info[i].length = skb->len;
1032		txdr->buffer_info[i].dma =
1033			dma_map_single(&pdev->dev, skb->data, skb->len,
1034				       DMA_TO_DEVICE);
1035		if (dma_mapping_error(&pdev->dev, txdr->buffer_info[i].dma)) {
1036			ret_val = 4;
1037			goto err_nomem;
1038		}
1039		tx_desc->buffer_addr = cpu_to_le64(txdr->buffer_info[i].dma);
1040		tx_desc->lower.data = cpu_to_le32(skb->len);
1041		tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1042						   E1000_TXD_CMD_IFCS |
1043						   E1000_TXD_CMD_RPS);
1044		tx_desc->upper.data = 0;
1045	}
1046
1047	/* Setup Rx descriptor ring and Rx buffers */
1048
1049	if (!rxdr->count)
1050		rxdr->count = E1000_DEFAULT_RXD;
1051
1052	rxdr->buffer_info = kcalloc(rxdr->count, sizeof(struct e1000_rx_buffer),
1053				    GFP_KERNEL);
1054	if (!rxdr->buffer_info) {
1055		ret_val = 5;
1056		goto err_nomem;
1057	}
1058
1059	rxdr->size = rxdr->count * sizeof(struct e1000_rx_desc);
1060	rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1061					GFP_KERNEL);
1062	if (!rxdr->desc) {
1063		ret_val = 6;
1064		goto err_nomem;
1065	}
1066	rxdr->next_to_use = rxdr->next_to_clean = 0;
1067
1068	rctl = er32(RCTL);
1069	ew32(RCTL, rctl & ~E1000_RCTL_EN);
1070	ew32(RDBAL, ((u64)rxdr->dma & 0xFFFFFFFF));
1071	ew32(RDBAH, ((u64)rxdr->dma >> 32));
1072	ew32(RDLEN, rxdr->size);
1073	ew32(RDH, 0);
1074	ew32(RDT, 0);
1075	rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
1076		E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1077		(hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
1078	ew32(RCTL, rctl);
1079
1080	for (i = 0; i < rxdr->count; i++) {
1081		struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rxdr, i);
1082		u8 *buf;
1083
1084		buf = kzalloc(E1000_RXBUFFER_2048 + NET_SKB_PAD + NET_IP_ALIGN,
1085			      GFP_KERNEL);
1086		if (!buf) {
1087			ret_val = 7;
1088			goto err_nomem;
1089		}
1090		rxdr->buffer_info[i].rxbuf.data = buf;
1091
1092		rxdr->buffer_info[i].dma =
1093			dma_map_single(&pdev->dev,
1094				       buf + NET_SKB_PAD + NET_IP_ALIGN,
1095				       E1000_RXBUFFER_2048, DMA_FROM_DEVICE);
1096		if (dma_mapping_error(&pdev->dev, rxdr->buffer_info[i].dma)) {
1097			ret_val = 8;
1098			goto err_nomem;
1099		}
1100		rx_desc->buffer_addr = cpu_to_le64(rxdr->buffer_info[i].dma);
1101	}
1102
1103	return 0;
1104
1105err_nomem:
1106	e1000_free_desc_rings(adapter);
1107	return ret_val;
1108}
1109
1110static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1111{
1112	struct e1000_hw *hw = &adapter->hw;
1113
1114	/* Write out to PHY registers 29 and 30 to disable the Receiver. */
1115	e1000_write_phy_reg(hw, 29, 0x001F);
1116	e1000_write_phy_reg(hw, 30, 0x8FFC);
1117	e1000_write_phy_reg(hw, 29, 0x001A);
1118	e1000_write_phy_reg(hw, 30, 0x8FF0);
1119}
1120
1121static void e1000_phy_reset_clk_and_crs(struct e1000_adapter *adapter)
1122{
1123	struct e1000_hw *hw = &adapter->hw;
1124	u16 phy_reg;
1125
1126	/* Because we reset the PHY above, we need to re-force TX_CLK in the
1127	 * Extended PHY Specific Control Register to 25MHz clock.  This
1128	 * value defaults back to a 2.5MHz clock when the PHY is reset.
1129	 */
1130	e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_reg);
1131	phy_reg |= M88E1000_EPSCR_TX_CLK_25;
1132	e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_reg);
1133
1134	/* In addition, because of the s/w reset above, we need to enable
1135	 * CRS on TX.  This must be set for both full and half duplex
1136	 * operation.
1137	 */
1138	e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_reg);
1139	phy_reg |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1140	e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_reg);
1141}
1142
1143static int e1000_nonintegrated_phy_loopback(struct e1000_adapter *adapter)
1144{
1145	struct e1000_hw *hw = &adapter->hw;
1146	u32 ctrl_reg;
1147	u16 phy_reg;
1148
1149	/* Setup the Device Control Register for PHY loopback test. */
1150
1151	ctrl_reg = er32(CTRL);
1152	ctrl_reg |= (E1000_CTRL_ILOS |		/* Invert Loss-Of-Signal */
1153		     E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
1154		     E1000_CTRL_FRCDPX |	/* Set the Force Duplex Bit */
1155		     E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
1156		     E1000_CTRL_FD);		/* Force Duplex to FULL */
1157
1158	ew32(CTRL, ctrl_reg);
1159
1160	/* Read the PHY Specific Control Register (0x10) */
1161	e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_reg);
1162
1163	/* Clear Auto-Crossover bits in PHY Specific Control Register
1164	 * (bits 6:5).
1165	 */
1166	phy_reg &= ~M88E1000_PSCR_AUTO_X_MODE;
1167	e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_reg);
1168
1169	/* Perform software reset on the PHY */
1170	e1000_phy_reset(hw);
1171
1172	/* Have to setup TX_CLK and TX_CRS after software reset */
1173	e1000_phy_reset_clk_and_crs(adapter);
1174
1175	e1000_write_phy_reg(hw, PHY_CTRL, 0x8100);
1176
1177	/* Wait for reset to complete. */
1178	udelay(500);
1179
1180	/* Have to setup TX_CLK and TX_CRS after software reset */
1181	e1000_phy_reset_clk_and_crs(adapter);
1182
1183	/* Write out to PHY registers 29 and 30 to disable the Receiver. */
1184	e1000_phy_disable_receiver(adapter);
1185
1186	/* Set the loopback bit in the PHY control register. */
1187	e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1188	phy_reg |= MII_CR_LOOPBACK;
1189	e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1190
1191	/* Setup TX_CLK and TX_CRS one more time. */
1192	e1000_phy_reset_clk_and_crs(adapter);
1193
1194	/* Check Phy Configuration */
1195	e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1196	if (phy_reg != 0x4100)
1197		return 9;
1198
1199	e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_reg);
1200	if (phy_reg != 0x0070)
1201		return 10;
1202
1203	e1000_read_phy_reg(hw, 29, &phy_reg);
1204	if (phy_reg != 0x001A)
1205		return 11;
1206
1207	return 0;
1208}
1209
1210static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1211{
1212	struct e1000_hw *hw = &adapter->hw;
1213	u32 ctrl_reg = 0;
1214	u32 stat_reg = 0;
1215
1216	hw->autoneg = false;
1217
1218	if (hw->phy_type == e1000_phy_m88) {
1219		/* Auto-MDI/MDIX Off */
1220		e1000_write_phy_reg(hw,
1221				    M88E1000_PHY_SPEC_CTRL, 0x0808);
1222		/* reset to update Auto-MDI/MDIX */
1223		e1000_write_phy_reg(hw, PHY_CTRL, 0x9140);
1224		/* autoneg off */
1225		e1000_write_phy_reg(hw, PHY_CTRL, 0x8140);
1226	}
1227
1228	ctrl_reg = er32(CTRL);
1229
1230	/* force 1000, set loopback */
1231	e1000_write_phy_reg(hw, PHY_CTRL, 0x4140);
1232
1233	/* Now set up the MAC to the same speed/duplex as the PHY. */
1234	ctrl_reg = er32(CTRL);
1235	ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1236	ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1237			E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1238			E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1239			E1000_CTRL_FD); /* Force Duplex to FULL */
1240
1241	if (hw->media_type == e1000_media_type_copper &&
1242	    hw->phy_type == e1000_phy_m88)
1243		ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1244	else {
1245		/* Set the ILOS bit on the fiber Nic is half
1246		 * duplex link is detected.
1247		 */
1248		stat_reg = er32(STATUS);
1249		if ((stat_reg & E1000_STATUS_FD) == 0)
1250			ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1251	}
1252
1253	ew32(CTRL, ctrl_reg);
1254
1255	/* Disable the receiver on the PHY so when a cable is plugged in, the
1256	 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1257	 */
1258	if (hw->phy_type == e1000_phy_m88)
1259		e1000_phy_disable_receiver(adapter);
1260
1261	udelay(500);
1262
1263	return 0;
1264}
1265
1266static int e1000_set_phy_loopback(struct e1000_adapter *adapter)
1267{
1268	struct e1000_hw *hw = &adapter->hw;
1269	u16 phy_reg = 0;
1270	u16 count = 0;
1271
1272	switch (hw->mac_type) {
1273	case e1000_82543:
1274		if (hw->media_type == e1000_media_type_copper) {
1275			/* Attempt to setup Loopback mode on Non-integrated PHY.
1276			 * Some PHY registers get corrupted at random, so
1277			 * attempt this 10 times.
1278			 */
1279			while (e1000_nonintegrated_phy_loopback(adapter) &&
1280			       count++ < 10);
1281			if (count < 11)
1282				return 0;
1283		}
1284		break;
1285
1286	case e1000_82544:
1287	case e1000_82540:
1288	case e1000_82545:
1289	case e1000_82545_rev_3:
1290	case e1000_82546:
1291	case e1000_82546_rev_3:
1292	case e1000_82541:
1293	case e1000_82541_rev_2:
1294	case e1000_82547:
1295	case e1000_82547_rev_2:
1296		return e1000_integrated_phy_loopback(adapter);
1297	default:
1298		/* Default PHY loopback work is to read the MII
1299		 * control register and assert bit 14 (loopback mode).
1300		 */
1301		e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1302		phy_reg |= MII_CR_LOOPBACK;
1303		e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1304		return 0;
1305	}
1306
1307	return 8;
1308}
1309
1310static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1311{
1312	struct e1000_hw *hw = &adapter->hw;
1313	u32 rctl;
1314
1315	if (hw->media_type == e1000_media_type_fiber ||
1316	    hw->media_type == e1000_media_type_internal_serdes) {
1317		switch (hw->mac_type) {
1318		case e1000_82545:
1319		case e1000_82546:
1320		case e1000_82545_rev_3:
1321		case e1000_82546_rev_3:
1322			return e1000_set_phy_loopback(adapter);
1323		default:
1324			rctl = er32(RCTL);
1325			rctl |= E1000_RCTL_LBM_TCVR;
1326			ew32(RCTL, rctl);
1327			return 0;
1328		}
1329	} else if (hw->media_type == e1000_media_type_copper) {
1330		return e1000_set_phy_loopback(adapter);
1331	}
1332
1333	return 7;
1334}
1335
1336static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1337{
1338	struct e1000_hw *hw = &adapter->hw;
1339	u32 rctl;
1340	u16 phy_reg;
1341
1342	rctl = er32(RCTL);
1343	rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1344	ew32(RCTL, rctl);
1345
1346	switch (hw->mac_type) {
1347	case e1000_82545:
1348	case e1000_82546:
1349	case e1000_82545_rev_3:
1350	case e1000_82546_rev_3:
1351	default:
1352		hw->autoneg = true;
1353		e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1354		if (phy_reg & MII_CR_LOOPBACK) {
1355			phy_reg &= ~MII_CR_LOOPBACK;
1356			e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1357			e1000_phy_reset(hw);
1358		}
1359		break;
1360	}
1361}
1362
1363static void e1000_create_lbtest_frame(struct sk_buff *skb,
1364				      unsigned int frame_size)
1365{
1366	memset(skb->data, 0xFF, frame_size);
1367	frame_size &= ~1;
1368	memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1369	skb->data[frame_size / 2 + 10] = 0xBE;
1370	skb->data[frame_size / 2 + 12] = 0xAF;
1371}
1372
1373static int e1000_check_lbtest_frame(const unsigned char *data,
1374				    unsigned int frame_size)
1375{
1376	frame_size &= ~1;
1377	if (*(data + 3) == 0xFF) {
1378		if ((*(data + frame_size / 2 + 10) == 0xBE) &&
1379		    (*(data + frame_size / 2 + 12) == 0xAF)) {
1380			return 0;
1381		}
1382	}
1383	return 13;
1384}
1385
1386static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1387{
1388	struct e1000_hw *hw = &adapter->hw;
1389	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
1390	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
1391	struct pci_dev *pdev = adapter->pdev;
1392	int i, j, k, l, lc, good_cnt, ret_val = 0;
1393	unsigned long time;
1394
1395	ew32(RDT, rxdr->count - 1);
1396
1397	/* Calculate the loop count based on the largest descriptor ring
1398	 * The idea is to wrap the largest ring a number of times using 64
1399	 * send/receive pairs during each loop
1400	 */
1401
1402	if (rxdr->count <= txdr->count)
1403		lc = ((txdr->count / 64) * 2) + 1;
1404	else
1405		lc = ((rxdr->count / 64) * 2) + 1;
1406
1407	k = l = 0;
1408	for (j = 0; j <= lc; j++) { /* loop count loop */
1409		for (i = 0; i < 64; i++) { /* send the packets */
1410			e1000_create_lbtest_frame(txdr->buffer_info[i].skb,
1411						  1024);
1412			dma_sync_single_for_device(&pdev->dev,
1413						   txdr->buffer_info[k].dma,
1414						   txdr->buffer_info[k].length,
1415						   DMA_TO_DEVICE);
1416			if (unlikely(++k == txdr->count))
1417				k = 0;
1418		}
1419		ew32(TDT, k);
1420		E1000_WRITE_FLUSH();
1421		msleep(200);
1422		time = jiffies; /* set the start time for the receive */
1423		good_cnt = 0;
1424		do { /* receive the sent packets */
1425			dma_sync_single_for_cpu(&pdev->dev,
1426						rxdr->buffer_info[l].dma,
1427						E1000_RXBUFFER_2048,
1428						DMA_FROM_DEVICE);
1429
1430			ret_val = e1000_check_lbtest_frame(
1431					rxdr->buffer_info[l].rxbuf.data +
1432					NET_SKB_PAD + NET_IP_ALIGN,
1433					1024);
1434			if (!ret_val)
1435				good_cnt++;
1436			if (unlikely(++l == rxdr->count))
1437				l = 0;
1438			/* time + 20 msecs (200 msecs on 2.4) is more than
1439			 * enough time to complete the receives, if it's
1440			 * exceeded, break and error off
1441			 */
1442		} while (good_cnt < 64 && time_after(time + 20, jiffies));
1443
1444		if (good_cnt != 64) {
1445			ret_val = 13; /* ret_val is the same as mis-compare */
1446			break;
1447		}
1448		if (time_after_eq(jiffies, time + 2)) {
1449			ret_val = 14; /* error code for time out error */
1450			break;
1451		}
1452	} /* end loop count loop */
1453	return ret_val;
1454}
1455
1456static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1457{
1458	*data = e1000_setup_desc_rings(adapter);
1459	if (*data)
1460		goto out;
1461	*data = e1000_setup_loopback_test(adapter);
1462	if (*data)
1463		goto err_loopback;
1464	*data = e1000_run_loopback_test(adapter);
1465	e1000_loopback_cleanup(adapter);
1466
1467err_loopback:
1468	e1000_free_desc_rings(adapter);
1469out:
1470	return *data;
1471}
1472
1473static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1474{
1475	struct e1000_hw *hw = &adapter->hw;
1476	*data = 0;
1477	if (hw->media_type == e1000_media_type_internal_serdes) {
1478		int i = 0;
1479
1480		hw->serdes_has_link = false;
1481
1482		/* On some blade server designs, link establishment
1483		 * could take as long as 2-3 minutes
1484		 */
1485		do {
1486			e1000_check_for_link(hw);
1487			if (hw->serdes_has_link)
1488				return *data;
1489			msleep(20);
1490		} while (i++ < 3750);
1491
1492		*data = 1;
1493	} else {
1494		e1000_check_for_link(hw);
1495		if (hw->autoneg)  /* if auto_neg is set wait for it */
1496			msleep(4000);
1497
1498		if (!(er32(STATUS) & E1000_STATUS_LU))
1499			*data = 1;
1500	}
1501	return *data;
1502}
1503
1504static int e1000_get_sset_count(struct net_device *netdev, int sset)
1505{
1506	switch (sset) {
1507	case ETH_SS_TEST:
1508		return E1000_TEST_LEN;
1509	case ETH_SS_STATS:
1510		return E1000_STATS_LEN;
1511	default:
1512		return -EOPNOTSUPP;
1513	}
1514}
1515
1516static void e1000_diag_test(struct net_device *netdev,
1517			    struct ethtool_test *eth_test, u64 *data)
1518{
1519	struct e1000_adapter *adapter = netdev_priv(netdev);
1520	struct e1000_hw *hw = &adapter->hw;
1521	bool if_running = netif_running(netdev);
1522
1523	set_bit(__E1000_TESTING, &adapter->flags);
1524	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1525		/* Offline tests */
1526
1527		/* save speed, duplex, autoneg settings */
1528		u16 autoneg_advertised = hw->autoneg_advertised;
1529		u8 forced_speed_duplex = hw->forced_speed_duplex;
1530		u8 autoneg = hw->autoneg;
1531
1532		e_info(hw, "offline testing starting\n");
1533
1534		/* Link test performed before hardware reset so autoneg doesn't
1535		 * interfere with test result
1536		 */
1537		if (e1000_link_test(adapter, &data[4]))
1538			eth_test->flags |= ETH_TEST_FL_FAILED;
1539
1540		if (if_running)
1541			/* indicate we're in test mode */
1542			e1000_close(netdev);
1543		else
1544			e1000_reset(adapter);
1545
1546		if (e1000_reg_test(adapter, &data[0]))
1547			eth_test->flags |= ETH_TEST_FL_FAILED;
1548
1549		e1000_reset(adapter);
1550		if (e1000_eeprom_test(adapter, &data[1]))
1551			eth_test->flags |= ETH_TEST_FL_FAILED;
1552
1553		e1000_reset(adapter);
1554		if (e1000_intr_test(adapter, &data[2]))
1555			eth_test->flags |= ETH_TEST_FL_FAILED;
1556
1557		e1000_reset(adapter);
1558		/* make sure the phy is powered up */
1559		e1000_power_up_phy(adapter);
1560		if (e1000_loopback_test(adapter, &data[3]))
1561			eth_test->flags |= ETH_TEST_FL_FAILED;
1562
1563		/* restore speed, duplex, autoneg settings */
1564		hw->autoneg_advertised = autoneg_advertised;
1565		hw->forced_speed_duplex = forced_speed_duplex;
1566		hw->autoneg = autoneg;
1567
1568		e1000_reset(adapter);
1569		clear_bit(__E1000_TESTING, &adapter->flags);
1570		if (if_running)
1571			e1000_open(netdev);
1572	} else {
1573		e_info(hw, "online testing starting\n");
1574		/* Online tests */
1575		if (e1000_link_test(adapter, &data[4]))
1576			eth_test->flags |= ETH_TEST_FL_FAILED;
1577
1578		/* Online tests aren't run; pass by default */
1579		data[0] = 0;
1580		data[1] = 0;
1581		data[2] = 0;
1582		data[3] = 0;
1583
1584		clear_bit(__E1000_TESTING, &adapter->flags);
1585	}
1586	msleep_interruptible(4 * 1000);
1587}
1588
1589static int e1000_wol_exclusion(struct e1000_adapter *adapter,
1590			       struct ethtool_wolinfo *wol)
1591{
1592	struct e1000_hw *hw = &adapter->hw;
1593	int retval = 1; /* fail by default */
1594
1595	switch (hw->device_id) {
1596	case E1000_DEV_ID_82542:
1597	case E1000_DEV_ID_82543GC_FIBER:
1598	case E1000_DEV_ID_82543GC_COPPER:
1599	case E1000_DEV_ID_82544EI_FIBER:
1600	case E1000_DEV_ID_82546EB_QUAD_COPPER:
1601	case E1000_DEV_ID_82545EM_FIBER:
1602	case E1000_DEV_ID_82545EM_COPPER:
1603	case E1000_DEV_ID_82546GB_QUAD_COPPER:
1604	case E1000_DEV_ID_82546GB_PCIE:
1605		/* these don't support WoL at all */
1606		wol->supported = 0;
1607		break;
1608	case E1000_DEV_ID_82546EB_FIBER:
1609	case E1000_DEV_ID_82546GB_FIBER:
1610		/* Wake events not supported on port B */
1611		if (er32(STATUS) & E1000_STATUS_FUNC_1) {
1612			wol->supported = 0;
1613			break;
1614		}
1615		/* return success for non excluded adapter ports */
1616		retval = 0;
1617		break;
1618	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1619		/* quad port adapters only support WoL on port A */
1620		if (!adapter->quad_port_a) {
1621			wol->supported = 0;
1622			break;
1623		}
1624		/* return success for non excluded adapter ports */
1625		retval = 0;
1626		break;
1627	default:
1628		/* dual port cards only support WoL on port A from now on
1629		 * unless it was enabled in the eeprom for port B
1630		 * so exclude FUNC_1 ports from having WoL enabled
1631		 */
1632		if (er32(STATUS) & E1000_STATUS_FUNC_1 &&
1633		    !adapter->eeprom_wol) {
1634			wol->supported = 0;
1635			break;
1636		}
1637
1638		retval = 0;
1639	}
1640
1641	return retval;
1642}
1643
1644static void e1000_get_wol(struct net_device *netdev,
1645			  struct ethtool_wolinfo *wol)
1646{
1647	struct e1000_adapter *adapter = netdev_priv(netdev);
1648	struct e1000_hw *hw = &adapter->hw;
1649
1650	wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
1651	wol->wolopts = 0;
1652
1653	/* this function will set ->supported = 0 and return 1 if wol is not
1654	 * supported by this hardware
1655	 */
1656	if (e1000_wol_exclusion(adapter, wol) ||
1657	    !device_can_wakeup(&adapter->pdev->dev))
1658		return;
1659
1660	/* apply any specific unsupported masks here */
1661	switch (hw->device_id) {
1662	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1663		/* KSP3 does not support UCAST wake-ups */
1664		wol->supported &= ~WAKE_UCAST;
1665
1666		if (adapter->wol & E1000_WUFC_EX)
1667			e_err(drv, "Interface does not support directed "
1668			      "(unicast) frame wake-up packets\n");
1669		break;
1670	default:
1671		break;
1672	}
1673
1674	if (adapter->wol & E1000_WUFC_EX)
1675		wol->wolopts |= WAKE_UCAST;
1676	if (adapter->wol & E1000_WUFC_MC)
1677		wol->wolopts |= WAKE_MCAST;
1678	if (adapter->wol & E1000_WUFC_BC)
1679		wol->wolopts |= WAKE_BCAST;
1680	if (adapter->wol & E1000_WUFC_MAG)
1681		wol->wolopts |= WAKE_MAGIC;
1682}
1683
1684static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1685{
1686	struct e1000_adapter *adapter = netdev_priv(netdev);
1687	struct e1000_hw *hw = &adapter->hw;
1688
1689	if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
1690		return -EOPNOTSUPP;
1691
1692	if (e1000_wol_exclusion(adapter, wol) ||
1693	    !device_can_wakeup(&adapter->pdev->dev))
1694		return wol->wolopts ? -EOPNOTSUPP : 0;
1695
1696	switch (hw->device_id) {
1697	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1698		if (wol->wolopts & WAKE_UCAST) {
1699			e_err(drv, "Interface does not support directed "
1700			      "(unicast) frame wake-up packets\n");
1701			return -EOPNOTSUPP;
1702		}
1703		break;
1704	default:
1705		break;
1706	}
1707
1708	/* these settings will always override what we currently have */
1709	adapter->wol = 0;
1710
1711	if (wol->wolopts & WAKE_UCAST)
1712		adapter->wol |= E1000_WUFC_EX;
1713	if (wol->wolopts & WAKE_MCAST)
1714		adapter->wol |= E1000_WUFC_MC;
1715	if (wol->wolopts & WAKE_BCAST)
1716		adapter->wol |= E1000_WUFC_BC;
1717	if (wol->wolopts & WAKE_MAGIC)
1718		adapter->wol |= E1000_WUFC_MAG;
1719
1720	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1721
1722	return 0;
1723}
1724
1725static int e1000_set_phys_id(struct net_device *netdev,
1726			     enum ethtool_phys_id_state state)
1727{
1728	struct e1000_adapter *adapter = netdev_priv(netdev);
1729	struct e1000_hw *hw = &adapter->hw;
1730
1731	switch (state) {
1732	case ETHTOOL_ID_ACTIVE:
1733		e1000_setup_led(hw);
1734		return 2;
1735
1736	case ETHTOOL_ID_ON:
1737		e1000_led_on(hw);
1738		break;
1739
1740	case ETHTOOL_ID_OFF:
1741		e1000_led_off(hw);
1742		break;
1743
1744	case ETHTOOL_ID_INACTIVE:
1745		e1000_cleanup_led(hw);
1746	}
1747
1748	return 0;
1749}
1750
1751static int e1000_get_coalesce(struct net_device *netdev,
1752			      struct ethtool_coalesce *ec,
1753			      struct kernel_ethtool_coalesce *kernel_coal,
1754			      struct netlink_ext_ack *extack)
1755{
1756	struct e1000_adapter *adapter = netdev_priv(netdev);
1757
1758	if (adapter->hw.mac_type < e1000_82545)
1759		return -EOPNOTSUPP;
1760
1761	if (adapter->itr_setting <= 4)
1762		ec->rx_coalesce_usecs = adapter->itr_setting;
1763	else
1764		ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1765
1766	return 0;
1767}
1768
1769static int e1000_set_coalesce(struct net_device *netdev,
1770			      struct ethtool_coalesce *ec,
1771			      struct kernel_ethtool_coalesce *kernel_coal,
1772			      struct netlink_ext_ack *extack)
1773{
1774	struct e1000_adapter *adapter = netdev_priv(netdev);
1775	struct e1000_hw *hw = &adapter->hw;
1776
1777	if (hw->mac_type < e1000_82545)
1778		return -EOPNOTSUPP;
1779
1780	if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
1781	    ((ec->rx_coalesce_usecs > 4) &&
1782	     (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1783	    (ec->rx_coalesce_usecs == 2))
1784		return -EINVAL;
1785
1786	if (ec->rx_coalesce_usecs == 4) {
1787		adapter->itr = adapter->itr_setting = 4;
1788	} else if (ec->rx_coalesce_usecs <= 3) {
1789		adapter->itr = 20000;
1790		adapter->itr_setting = ec->rx_coalesce_usecs;
1791	} else {
1792		adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1793		adapter->itr_setting = adapter->itr & ~3;
1794	}
1795
1796	if (adapter->itr_setting != 0)
1797		ew32(ITR, 1000000000 / (adapter->itr * 256));
1798	else
1799		ew32(ITR, 0);
1800
1801	return 0;
1802}
1803
1804static int e1000_nway_reset(struct net_device *netdev)
1805{
1806	struct e1000_adapter *adapter = netdev_priv(netdev);
1807
1808	if (netif_running(netdev))
1809		e1000_reinit_locked(adapter);
1810	return 0;
1811}
1812
1813static void e1000_get_ethtool_stats(struct net_device *netdev,
1814				    struct ethtool_stats *stats, u64 *data)
1815{
1816	struct e1000_adapter *adapter = netdev_priv(netdev);
1817	int i;
1818	const struct e1000_stats *stat = e1000_gstrings_stats;
1819
1820	e1000_update_stats(adapter);
1821	for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++, stat++) {
1822		char *p;
1823
1824		switch (stat->type) {
1825		case NETDEV_STATS:
1826			p = (char *)netdev + stat->stat_offset;
1827			break;
1828		case E1000_STATS:
1829			p = (char *)adapter + stat->stat_offset;
1830			break;
1831		default:
1832			netdev_WARN_ONCE(netdev, "Invalid E1000 stat type: %u index %d\n",
1833					 stat->type, i);
1834			continue;
1835		}
1836
1837		if (stat->sizeof_stat == sizeof(u64))
1838			data[i] = *(u64 *)p;
1839		else
1840			data[i] = *(u32 *)p;
1841	}
1842/* BUG_ON(i != E1000_STATS_LEN); */
1843}
1844
1845static void e1000_get_strings(struct net_device *netdev, u32 stringset,
1846			      u8 *data)
1847{
1848	u8 *p = data;
1849	int i;
1850
1851	switch (stringset) {
1852	case ETH_SS_TEST:
1853		memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
1854		break;
1855	case ETH_SS_STATS:
1856		for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1857			memcpy(p, e1000_gstrings_stats[i].stat_string,
1858			       ETH_GSTRING_LEN);
1859			p += ETH_GSTRING_LEN;
1860		}
1861		/* BUG_ON(p - data != E1000_STATS_LEN * ETH_GSTRING_LEN); */
1862		break;
1863	}
1864}
1865
1866static const struct ethtool_ops e1000_ethtool_ops = {
1867	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1868	.get_drvinfo		= e1000_get_drvinfo,
1869	.get_regs_len		= e1000_get_regs_len,
1870	.get_regs		= e1000_get_regs,
1871	.get_wol		= e1000_get_wol,
1872	.set_wol		= e1000_set_wol,
1873	.get_msglevel		= e1000_get_msglevel,
1874	.set_msglevel		= e1000_set_msglevel,
1875	.nway_reset		= e1000_nway_reset,
1876	.get_link		= e1000_get_link,
1877	.get_eeprom_len		= e1000_get_eeprom_len,
1878	.get_eeprom		= e1000_get_eeprom,
1879	.set_eeprom		= e1000_set_eeprom,
1880	.get_ringparam		= e1000_get_ringparam,
1881	.set_ringparam		= e1000_set_ringparam,
1882	.get_pauseparam		= e1000_get_pauseparam,
1883	.set_pauseparam		= e1000_set_pauseparam,
1884	.self_test		= e1000_diag_test,
1885	.get_strings		= e1000_get_strings,
1886	.set_phys_id		= e1000_set_phys_id,
1887	.get_ethtool_stats	= e1000_get_ethtool_stats,
1888	.get_sset_count		= e1000_get_sset_count,
1889	.get_coalesce		= e1000_get_coalesce,
1890	.set_coalesce		= e1000_set_coalesce,
1891	.get_ts_info		= ethtool_op_get_ts_info,
1892	.get_link_ksettings	= e1000_get_link_ksettings,
1893	.set_link_ksettings	= e1000_set_link_ksettings,
1894};
1895
1896void e1000_set_ethtool_ops(struct net_device *netdev)
1897{
1898	netdev->ethtool_ops = &e1000_ethtool_ops;
1899}
~/ Read-only file preview.