e1000e: cleanup LONG_LINE checkpatch warnings
[linux-2.6/btrfs-unstable.git] / drivers / net / ethernet / intel / e1000e / ethtool.c
blob6e9d433f122b1c6947b4748920b5aae3ccb4e470
1 /*******************************************************************************
3 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2013 Intel Corporation.
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.
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.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Contact Information:
23 Linux NICS <linux.nics@intel.com>
24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *******************************************************************************/
29 /* ethtool support for e1000 */
31 #include <linux/netdevice.h>
32 #include <linux/interrupt.h>
33 #include <linux/ethtool.h>
34 #include <linux/pci.h>
35 #include <linux/slab.h>
36 #include <linux/delay.h>
37 #include <linux/vmalloc.h>
38 #include <linux/mdio.h>
40 #include "e1000.h"
42 enum { NETDEV_STATS, E1000_STATS };
44 struct e1000_stats {
45 char stat_string[ETH_GSTRING_LEN];
46 int type;
47 int sizeof_stat;
48 int stat_offset;
51 #define E1000_STAT(str, m) { \
52 .stat_string = str, \
53 .type = E1000_STATS, \
54 .sizeof_stat = sizeof(((struct e1000_adapter *)0)->m), \
55 .stat_offset = offsetof(struct e1000_adapter, m) }
56 #define E1000_NETDEV_STAT(str, m) { \
57 .stat_string = str, \
58 .type = NETDEV_STATS, \
59 .sizeof_stat = sizeof(((struct rtnl_link_stats64 *)0)->m), \
60 .stat_offset = offsetof(struct rtnl_link_stats64, m) }
62 static const struct e1000_stats e1000_gstrings_stats[] = {
63 E1000_STAT("rx_packets", stats.gprc),
64 E1000_STAT("tx_packets", stats.gptc),
65 E1000_STAT("rx_bytes", stats.gorc),
66 E1000_STAT("tx_bytes", stats.gotc),
67 E1000_STAT("rx_broadcast", stats.bprc),
68 E1000_STAT("tx_broadcast", stats.bptc),
69 E1000_STAT("rx_multicast", stats.mprc),
70 E1000_STAT("tx_multicast", stats.mptc),
71 E1000_NETDEV_STAT("rx_errors", rx_errors),
72 E1000_NETDEV_STAT("tx_errors", tx_errors),
73 E1000_NETDEV_STAT("tx_dropped", tx_dropped),
74 E1000_STAT("multicast", stats.mprc),
75 E1000_STAT("collisions", stats.colc),
76 E1000_NETDEV_STAT("rx_length_errors", rx_length_errors),
77 E1000_NETDEV_STAT("rx_over_errors", rx_over_errors),
78 E1000_STAT("rx_crc_errors", stats.crcerrs),
79 E1000_NETDEV_STAT("rx_frame_errors", rx_frame_errors),
80 E1000_STAT("rx_no_buffer_count", stats.rnbc),
81 E1000_STAT("rx_missed_errors", stats.mpc),
82 E1000_STAT("tx_aborted_errors", stats.ecol),
83 E1000_STAT("tx_carrier_errors", stats.tncrs),
84 E1000_NETDEV_STAT("tx_fifo_errors", tx_fifo_errors),
85 E1000_NETDEV_STAT("tx_heartbeat_errors", tx_heartbeat_errors),
86 E1000_STAT("tx_window_errors", stats.latecol),
87 E1000_STAT("tx_abort_late_coll", stats.latecol),
88 E1000_STAT("tx_deferred_ok", stats.dc),
89 E1000_STAT("tx_single_coll_ok", stats.scc),
90 E1000_STAT("tx_multi_coll_ok", stats.mcc),
91 E1000_STAT("tx_timeout_count", tx_timeout_count),
92 E1000_STAT("tx_restart_queue", restart_queue),
93 E1000_STAT("rx_long_length_errors", stats.roc),
94 E1000_STAT("rx_short_length_errors", stats.ruc),
95 E1000_STAT("rx_align_errors", stats.algnerrc),
96 E1000_STAT("tx_tcp_seg_good", stats.tsctc),
97 E1000_STAT("tx_tcp_seg_failed", stats.tsctfc),
98 E1000_STAT("rx_flow_control_xon", stats.xonrxc),
99 E1000_STAT("rx_flow_control_xoff", stats.xoffrxc),
100 E1000_STAT("tx_flow_control_xon", stats.xontxc),
101 E1000_STAT("tx_flow_control_xoff", stats.xofftxc),
102 E1000_STAT("rx_csum_offload_good", hw_csum_good),
103 E1000_STAT("rx_csum_offload_errors", hw_csum_err),
104 E1000_STAT("rx_header_split", rx_hdr_split),
105 E1000_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
106 E1000_STAT("tx_smbus", stats.mgptc),
107 E1000_STAT("rx_smbus", stats.mgprc),
108 E1000_STAT("dropped_smbus", stats.mgpdc),
109 E1000_STAT("rx_dma_failed", rx_dma_failed),
110 E1000_STAT("tx_dma_failed", tx_dma_failed),
111 E1000_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
112 E1000_STAT("uncorr_ecc_errors", uncorr_errors),
113 E1000_STAT("corr_ecc_errors", corr_errors),
116 #define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
117 #define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN)
118 static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
119 "Register test (offline)", "Eeprom test (offline)",
120 "Interrupt test (offline)", "Loopback test (offline)",
121 "Link test (on/offline)"
123 #define E1000_TEST_LEN ARRAY_SIZE(e1000_gstrings_test)
125 static int e1000_get_settings(struct net_device *netdev,
126 struct ethtool_cmd *ecmd)
128 struct e1000_adapter *adapter = netdev_priv(netdev);
129 struct e1000_hw *hw = &adapter->hw;
130 u32 speed;
132 if (hw->phy.media_type == e1000_media_type_copper) {
133 ecmd->supported = (SUPPORTED_10baseT_Half |
134 SUPPORTED_10baseT_Full |
135 SUPPORTED_100baseT_Half |
136 SUPPORTED_100baseT_Full |
137 SUPPORTED_1000baseT_Full |
138 SUPPORTED_Autoneg |
139 SUPPORTED_TP);
140 if (hw->phy.type == e1000_phy_ife)
141 ecmd->supported &= ~SUPPORTED_1000baseT_Full;
142 ecmd->advertising = ADVERTISED_TP;
144 if (hw->mac.autoneg == 1) {
145 ecmd->advertising |= ADVERTISED_Autoneg;
146 /* the e1000 autoneg seems to match ethtool nicely */
147 ecmd->advertising |= hw->phy.autoneg_advertised;
150 ecmd->port = PORT_TP;
151 ecmd->phy_address = hw->phy.addr;
152 ecmd->transceiver = XCVR_INTERNAL;
154 } else {
155 ecmd->supported = (SUPPORTED_1000baseT_Full |
156 SUPPORTED_FIBRE |
157 SUPPORTED_Autoneg);
159 ecmd->advertising = (ADVERTISED_1000baseT_Full |
160 ADVERTISED_FIBRE |
161 ADVERTISED_Autoneg);
163 ecmd->port = PORT_FIBRE;
164 ecmd->transceiver = XCVR_EXTERNAL;
167 speed = -1;
168 ecmd->duplex = -1;
170 if (netif_running(netdev)) {
171 if (netif_carrier_ok(netdev)) {
172 speed = adapter->link_speed;
173 ecmd->duplex = adapter->link_duplex - 1;
175 } else {
176 u32 status = er32(STATUS);
177 if (status & E1000_STATUS_LU) {
178 if (status & E1000_STATUS_SPEED_1000)
179 speed = SPEED_1000;
180 else if (status & E1000_STATUS_SPEED_100)
181 speed = SPEED_100;
182 else
183 speed = SPEED_10;
185 if (status & E1000_STATUS_FD)
186 ecmd->duplex = DUPLEX_FULL;
187 else
188 ecmd->duplex = DUPLEX_HALF;
192 ethtool_cmd_speed_set(ecmd, speed);
193 ecmd->autoneg = ((hw->phy.media_type == e1000_media_type_fiber) ||
194 hw->mac.autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
196 /* MDI-X => 2; MDI =>1; Invalid =>0 */
197 if ((hw->phy.media_type == e1000_media_type_copper) &&
198 netif_carrier_ok(netdev))
199 ecmd->eth_tp_mdix = hw->phy.is_mdix ? ETH_TP_MDI_X : ETH_TP_MDI;
200 else
201 ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
203 if (hw->phy.mdix == AUTO_ALL_MODES)
204 ecmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
205 else
206 ecmd->eth_tp_mdix_ctrl = hw->phy.mdix;
208 return 0;
211 static int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
213 struct e1000_mac_info *mac = &adapter->hw.mac;
215 mac->autoneg = 0;
217 /* Make sure dplx is at most 1 bit and lsb of speed is not set
218 * for the switch() below to work
220 if ((spd & 1) || (dplx & ~1))
221 goto err_inval;
223 /* Fiber NICs only allow 1000 gbps Full duplex */
224 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
225 spd != SPEED_1000 &&
226 dplx != DUPLEX_FULL) {
227 goto err_inval;
230 switch (spd + dplx) {
231 case SPEED_10 + DUPLEX_HALF:
232 mac->forced_speed_duplex = ADVERTISE_10_HALF;
233 break;
234 case SPEED_10 + DUPLEX_FULL:
235 mac->forced_speed_duplex = ADVERTISE_10_FULL;
236 break;
237 case SPEED_100 + DUPLEX_HALF:
238 mac->forced_speed_duplex = ADVERTISE_100_HALF;
239 break;
240 case SPEED_100 + DUPLEX_FULL:
241 mac->forced_speed_duplex = ADVERTISE_100_FULL;
242 break;
243 case SPEED_1000 + DUPLEX_FULL:
244 mac->autoneg = 1;
245 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
246 break;
247 case SPEED_1000 + DUPLEX_HALF: /* not supported */
248 default:
249 goto err_inval;
252 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
253 adapter->hw.phy.mdix = AUTO_ALL_MODES;
255 return 0;
257 err_inval:
258 e_err("Unsupported Speed/Duplex configuration\n");
259 return -EINVAL;
262 static int e1000_set_settings(struct net_device *netdev,
263 struct ethtool_cmd *ecmd)
265 struct e1000_adapter *adapter = netdev_priv(netdev);
266 struct e1000_hw *hw = &adapter->hw;
268 /* When SoL/IDER sessions are active, autoneg/speed/duplex
269 * cannot be changed
271 if (hw->phy.ops.check_reset_block &&
272 hw->phy.ops.check_reset_block(hw)) {
273 e_err("Cannot change link characteristics when SoL/IDER is active.\n");
274 return -EINVAL;
277 /* MDI setting is only allowed when autoneg enabled because
278 * some hardware doesn't allow MDI setting when speed or
279 * duplex is forced.
281 if (ecmd->eth_tp_mdix_ctrl) {
282 if (hw->phy.media_type != e1000_media_type_copper)
283 return -EOPNOTSUPP;
285 if ((ecmd->eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO) &&
286 (ecmd->autoneg != AUTONEG_ENABLE)) {
287 e_err("forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n");
288 return -EINVAL;
292 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
293 usleep_range(1000, 2000);
295 if (ecmd->autoneg == AUTONEG_ENABLE) {
296 hw->mac.autoneg = 1;
297 if (hw->phy.media_type == e1000_media_type_fiber)
298 hw->phy.autoneg_advertised = ADVERTISED_1000baseT_Full |
299 ADVERTISED_FIBRE | ADVERTISED_Autoneg;
300 else
301 hw->phy.autoneg_advertised = ecmd->advertising |
302 ADVERTISED_TP | ADVERTISED_Autoneg;
303 ecmd->advertising = hw->phy.autoneg_advertised;
304 if (adapter->fc_autoneg)
305 hw->fc.requested_mode = e1000_fc_default;
306 } else {
307 u32 speed = ethtool_cmd_speed(ecmd);
308 /* calling this overrides forced MDI setting */
309 if (e1000_set_spd_dplx(adapter, speed, ecmd->duplex)) {
310 clear_bit(__E1000_RESETTING, &adapter->state);
311 return -EINVAL;
315 /* MDI-X => 2; MDI => 1; Auto => 3 */
316 if (ecmd->eth_tp_mdix_ctrl) {
317 /* fix up the value for auto (3 => 0) as zero is mapped
318 * internally to auto
320 if (ecmd->eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
321 hw->phy.mdix = AUTO_ALL_MODES;
322 else
323 hw->phy.mdix = ecmd->eth_tp_mdix_ctrl;
326 /* reset the link */
327 if (netif_running(adapter->netdev)) {
328 e1000e_down(adapter);
329 e1000e_up(adapter);
330 } else {
331 e1000e_reset(adapter);
334 clear_bit(__E1000_RESETTING, &adapter->state);
335 return 0;
338 static void e1000_get_pauseparam(struct net_device *netdev,
339 struct ethtool_pauseparam *pause)
341 struct e1000_adapter *adapter = netdev_priv(netdev);
342 struct e1000_hw *hw = &adapter->hw;
344 pause->autoneg =
345 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
347 if (hw->fc.current_mode == e1000_fc_rx_pause) {
348 pause->rx_pause = 1;
349 } else if (hw->fc.current_mode == e1000_fc_tx_pause) {
350 pause->tx_pause = 1;
351 } else if (hw->fc.current_mode == e1000_fc_full) {
352 pause->rx_pause = 1;
353 pause->tx_pause = 1;
357 static int e1000_set_pauseparam(struct net_device *netdev,
358 struct ethtool_pauseparam *pause)
360 struct e1000_adapter *adapter = netdev_priv(netdev);
361 struct e1000_hw *hw = &adapter->hw;
362 int retval = 0;
364 adapter->fc_autoneg = pause->autoneg;
366 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
367 usleep_range(1000, 2000);
369 if (adapter->fc_autoneg == AUTONEG_ENABLE) {
370 hw->fc.requested_mode = e1000_fc_default;
371 if (netif_running(adapter->netdev)) {
372 e1000e_down(adapter);
373 e1000e_up(adapter);
374 } else {
375 e1000e_reset(adapter);
377 } else {
378 if (pause->rx_pause && pause->tx_pause)
379 hw->fc.requested_mode = e1000_fc_full;
380 else if (pause->rx_pause && !pause->tx_pause)
381 hw->fc.requested_mode = e1000_fc_rx_pause;
382 else if (!pause->rx_pause && pause->tx_pause)
383 hw->fc.requested_mode = e1000_fc_tx_pause;
384 else if (!pause->rx_pause && !pause->tx_pause)
385 hw->fc.requested_mode = e1000_fc_none;
387 hw->fc.current_mode = hw->fc.requested_mode;
389 if (hw->phy.media_type == e1000_media_type_fiber) {
390 retval = hw->mac.ops.setup_link(hw);
391 /* implicit goto out */
392 } else {
393 retval = e1000e_force_mac_fc(hw);
394 if (retval)
395 goto out;
396 e1000e_set_fc_watermarks(hw);
400 out:
401 clear_bit(__E1000_RESETTING, &adapter->state);
402 return retval;
405 static u32 e1000_get_msglevel(struct net_device *netdev)
407 struct e1000_adapter *adapter = netdev_priv(netdev);
408 return adapter->msg_enable;
411 static void e1000_set_msglevel(struct net_device *netdev, u32 data)
413 struct e1000_adapter *adapter = netdev_priv(netdev);
414 adapter->msg_enable = data;
417 static int e1000_get_regs_len(struct net_device __always_unused *netdev)
419 #define E1000_REGS_LEN 32 /* overestimate */
420 return E1000_REGS_LEN * sizeof(u32);
423 static void e1000_get_regs(struct net_device *netdev,
424 struct ethtool_regs *regs, void *p)
426 struct e1000_adapter *adapter = netdev_priv(netdev);
427 struct e1000_hw *hw = &adapter->hw;
428 u32 *regs_buff = p;
429 u16 phy_data;
431 memset(p, 0, E1000_REGS_LEN * sizeof(u32));
433 regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
434 adapter->pdev->device;
436 regs_buff[0] = er32(CTRL);
437 regs_buff[1] = er32(STATUS);
439 regs_buff[2] = er32(RCTL);
440 regs_buff[3] = er32(RDLEN(0));
441 regs_buff[4] = er32(RDH(0));
442 regs_buff[5] = er32(RDT(0));
443 regs_buff[6] = er32(RDTR);
445 regs_buff[7] = er32(TCTL);
446 regs_buff[8] = er32(TDLEN(0));
447 regs_buff[9] = er32(TDH(0));
448 regs_buff[10] = er32(TDT(0));
449 regs_buff[11] = er32(TIDV);
451 regs_buff[12] = adapter->hw.phy.type; /* PHY type (IGP=1, M88=0) */
453 /* ethtool doesn't use anything past this point, so all this
454 * code is likely legacy junk for apps that may or may not exist
456 if (hw->phy.type == e1000_phy_m88) {
457 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
458 regs_buff[13] = (u32)phy_data; /* cable length */
459 regs_buff[14] = 0; /* Dummy (to align w/ IGP phy reg dump) */
460 regs_buff[15] = 0; /* Dummy (to align w/ IGP phy reg dump) */
461 regs_buff[16] = 0; /* Dummy (to align w/ IGP phy reg dump) */
462 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
463 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
464 regs_buff[18] = regs_buff[13]; /* cable polarity */
465 regs_buff[19] = 0; /* Dummy (to align w/ IGP phy reg dump) */
466 regs_buff[20] = regs_buff[17]; /* polarity correction */
467 /* phy receive errors */
468 regs_buff[22] = adapter->phy_stats.receive_errors;
469 regs_buff[23] = regs_buff[13]; /* mdix mode */
471 regs_buff[21] = 0; /* was idle_errors */
472 e1e_rphy(hw, MII_STAT1000, &phy_data);
473 regs_buff[24] = (u32)phy_data; /* phy local receiver status */
474 regs_buff[25] = regs_buff[24]; /* phy remote receiver status */
477 static int e1000_get_eeprom_len(struct net_device *netdev)
479 struct e1000_adapter *adapter = netdev_priv(netdev);
480 return adapter->hw.nvm.word_size * 2;
483 static int e1000_get_eeprom(struct net_device *netdev,
484 struct ethtool_eeprom *eeprom, u8 *bytes)
486 struct e1000_adapter *adapter = netdev_priv(netdev);
487 struct e1000_hw *hw = &adapter->hw;
488 u16 *eeprom_buff;
489 int first_word;
490 int last_word;
491 int ret_val = 0;
492 u16 i;
494 if (eeprom->len == 0)
495 return -EINVAL;
497 eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16);
499 first_word = eeprom->offset >> 1;
500 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
502 eeprom_buff = kmalloc(sizeof(u16) *
503 (last_word - first_word + 1), GFP_KERNEL);
504 if (!eeprom_buff)
505 return -ENOMEM;
507 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
508 ret_val = e1000_read_nvm(hw, first_word,
509 last_word - first_word + 1,
510 eeprom_buff);
511 } else {
512 for (i = 0; i < last_word - first_word + 1; i++) {
513 ret_val = e1000_read_nvm(hw, first_word + i, 1,
514 &eeprom_buff[i]);
515 if (ret_val)
516 break;
520 if (ret_val) {
521 /* a read error occurred, throw away the result */
522 memset(eeprom_buff, 0xff, sizeof(u16) *
523 (last_word - first_word + 1));
524 } else {
525 /* Device's eeprom is always little-endian, word addressable */
526 for (i = 0; i < last_word - first_word + 1; i++)
527 le16_to_cpus(&eeprom_buff[i]);
530 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
531 kfree(eeprom_buff);
533 return ret_val;
536 static int e1000_set_eeprom(struct net_device *netdev,
537 struct ethtool_eeprom *eeprom, u8 *bytes)
539 struct e1000_adapter *adapter = netdev_priv(netdev);
540 struct e1000_hw *hw = &adapter->hw;
541 u16 *eeprom_buff;
542 void *ptr;
543 int max_len;
544 int first_word;
545 int last_word;
546 int ret_val = 0;
547 u16 i;
549 if (eeprom->len == 0)
550 return -EOPNOTSUPP;
552 if (eeprom->magic !=
553 (adapter->pdev->vendor | (adapter->pdev->device << 16)))
554 return -EFAULT;
556 if (adapter->flags & FLAG_READ_ONLY_NVM)
557 return -EINVAL;
559 max_len = hw->nvm.word_size * 2;
561 first_word = eeprom->offset >> 1;
562 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
563 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
564 if (!eeprom_buff)
565 return -ENOMEM;
567 ptr = (void *)eeprom_buff;
569 if (eeprom->offset & 1) {
570 /* need read/modify/write of first changed EEPROM word */
571 /* only the second byte of the word is being modified */
572 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
573 ptr++;
575 if (((eeprom->offset + eeprom->len) & 1) && (!ret_val))
576 /* need read/modify/write of last changed EEPROM word */
577 /* only the first byte of the word is being modified */
578 ret_val = e1000_read_nvm(hw, last_word, 1,
579 &eeprom_buff[last_word - first_word]);
581 if (ret_val)
582 goto out;
584 /* Device's eeprom is always little-endian, word addressable */
585 for (i = 0; i < last_word - first_word + 1; i++)
586 le16_to_cpus(&eeprom_buff[i]);
588 memcpy(ptr, bytes, eeprom->len);
590 for (i = 0; i < last_word - first_word + 1; i++)
591 cpu_to_le16s(&eeprom_buff[i]);
593 ret_val = e1000_write_nvm(hw, first_word,
594 last_word - first_word + 1, eeprom_buff);
596 if (ret_val)
597 goto out;
599 /* Update the checksum over the first part of the EEPROM if needed
600 * and flush shadow RAM for applicable controllers
602 if ((first_word <= NVM_CHECKSUM_REG) ||
603 (hw->mac.type == e1000_82583) ||
604 (hw->mac.type == e1000_82574) ||
605 (hw->mac.type == e1000_82573))
606 ret_val = e1000e_update_nvm_checksum(hw);
608 out:
609 kfree(eeprom_buff);
610 return ret_val;
613 static void e1000_get_drvinfo(struct net_device *netdev,
614 struct ethtool_drvinfo *drvinfo)
616 struct e1000_adapter *adapter = netdev_priv(netdev);
618 strlcpy(drvinfo->driver, e1000e_driver_name,
619 sizeof(drvinfo->driver));
620 strlcpy(drvinfo->version, e1000e_driver_version,
621 sizeof(drvinfo->version));
623 /* EEPROM image version # is reported as firmware version # for
624 * PCI-E controllers
626 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
627 "%d.%d-%d",
628 (adapter->eeprom_vers & 0xF000) >> 12,
629 (adapter->eeprom_vers & 0x0FF0) >> 4,
630 (adapter->eeprom_vers & 0x000F));
632 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
633 sizeof(drvinfo->bus_info));
634 drvinfo->regdump_len = e1000_get_regs_len(netdev);
635 drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
638 static void e1000_get_ringparam(struct net_device *netdev,
639 struct ethtool_ringparam *ring)
641 struct e1000_adapter *adapter = netdev_priv(netdev);
643 ring->rx_max_pending = E1000_MAX_RXD;
644 ring->tx_max_pending = E1000_MAX_TXD;
645 ring->rx_pending = adapter->rx_ring_count;
646 ring->tx_pending = adapter->tx_ring_count;
649 static int e1000_set_ringparam(struct net_device *netdev,
650 struct ethtool_ringparam *ring)
652 struct e1000_adapter *adapter = netdev_priv(netdev);
653 struct e1000_ring *temp_tx = NULL, *temp_rx = NULL;
654 int err = 0, size = sizeof(struct e1000_ring);
655 bool set_tx = false, set_rx = false;
656 u16 new_rx_count, new_tx_count;
658 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
659 return -EINVAL;
661 new_rx_count = clamp_t(u32, ring->rx_pending, E1000_MIN_RXD,
662 E1000_MAX_RXD);
663 new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
665 new_tx_count = clamp_t(u32, ring->tx_pending, E1000_MIN_TXD,
666 E1000_MAX_TXD);
667 new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
669 if ((new_tx_count == adapter->tx_ring_count) &&
670 (new_rx_count == adapter->rx_ring_count))
671 /* nothing to do */
672 return 0;
674 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
675 usleep_range(1000, 2000);
677 if (!netif_running(adapter->netdev)) {
678 /* Set counts now and allocate resources during open() */
679 adapter->tx_ring->count = new_tx_count;
680 adapter->rx_ring->count = new_rx_count;
681 adapter->tx_ring_count = new_tx_count;
682 adapter->rx_ring_count = new_rx_count;
683 goto clear_reset;
686 set_tx = (new_tx_count != adapter->tx_ring_count);
687 set_rx = (new_rx_count != adapter->rx_ring_count);
689 /* Allocate temporary storage for ring updates */
690 if (set_tx) {
691 temp_tx = vmalloc(size);
692 if (!temp_tx) {
693 err = -ENOMEM;
694 goto free_temp;
697 if (set_rx) {
698 temp_rx = vmalloc(size);
699 if (!temp_rx) {
700 err = -ENOMEM;
701 goto free_temp;
705 e1000e_down(adapter);
707 /* We can't just free everything and then setup again, because the
708 * ISRs in MSI-X mode get passed pointers to the Tx and Rx ring
709 * structs. First, attempt to allocate new resources...
711 if (set_tx) {
712 memcpy(temp_tx, adapter->tx_ring, size);
713 temp_tx->count = new_tx_count;
714 err = e1000e_setup_tx_resources(temp_tx);
715 if (err)
716 goto err_setup;
718 if (set_rx) {
719 memcpy(temp_rx, adapter->rx_ring, size);
720 temp_rx->count = new_rx_count;
721 err = e1000e_setup_rx_resources(temp_rx);
722 if (err)
723 goto err_setup_rx;
726 /* ...then free the old resources and copy back any new ring data */
727 if (set_tx) {
728 e1000e_free_tx_resources(adapter->tx_ring);
729 memcpy(adapter->tx_ring, temp_tx, size);
730 adapter->tx_ring_count = new_tx_count;
732 if (set_rx) {
733 e1000e_free_rx_resources(adapter->rx_ring);
734 memcpy(adapter->rx_ring, temp_rx, size);
735 adapter->rx_ring_count = new_rx_count;
738 err_setup_rx:
739 if (err && set_tx)
740 e1000e_free_tx_resources(temp_tx);
741 err_setup:
742 e1000e_up(adapter);
743 free_temp:
744 vfree(temp_tx);
745 vfree(temp_rx);
746 clear_reset:
747 clear_bit(__E1000_RESETTING, &adapter->state);
748 return err;
751 static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
752 int reg, int offset, u32 mask, u32 write)
754 u32 pat, val;
755 static const u32 test[] = {
756 0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
757 for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
758 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
759 (test[pat] & write));
760 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
761 if (val != (test[pat] & write & mask)) {
762 e_err("pattern test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n",
763 reg + (offset << 2), val,
764 (test[pat] & write & mask));
765 *data = reg;
766 return 1;
769 return 0;
772 static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
773 int reg, u32 mask, u32 write)
775 u32 val;
776 __ew32(&adapter->hw, reg, write & mask);
777 val = __er32(&adapter->hw, reg);
778 if ((write & mask) != (val & mask)) {
779 e_err("set/check test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n",
780 reg, (val & mask), (write & mask));
781 *data = reg;
782 return 1;
784 return 0;
786 #define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write) \
787 do { \
788 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
789 return 1; \
790 } while (0)
791 #define REG_PATTERN_TEST(reg, mask, write) \
792 REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
794 #define REG_SET_AND_CHECK(reg, mask, write) \
795 do { \
796 if (reg_set_and_check(adapter, data, reg, mask, write)) \
797 return 1; \
798 } while (0)
800 static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
802 struct e1000_hw *hw = &adapter->hw;
803 struct e1000_mac_info *mac = &adapter->hw.mac;
804 u32 value;
805 u32 before;
806 u32 after;
807 u32 i;
808 u32 toggle;
809 u32 mask;
810 u32 wlock_mac = 0;
812 /* The status register is Read Only, so a write should fail.
813 * Some bits that get toggled are ignored.
815 switch (mac->type) {
816 /* there are several bits on newer hardware that are r/w */
817 case e1000_82571:
818 case e1000_82572:
819 case e1000_80003es2lan:
820 toggle = 0x7FFFF3FF;
821 break;
822 default:
823 toggle = 0x7FFFF033;
824 break;
827 before = er32(STATUS);
828 value = (er32(STATUS) & toggle);
829 ew32(STATUS, toggle);
830 after = er32(STATUS) & toggle;
831 if (value != after) {
832 e_err("failed STATUS register test got: 0x%08X expected: 0x%08X\n",
833 after, value);
834 *data = 1;
835 return 1;
837 /* restore previous status */
838 ew32(STATUS, before);
840 if (!(adapter->flags & FLAG_IS_ICH)) {
841 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
842 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
843 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
844 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
847 REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
848 REG_PATTERN_TEST(E1000_RDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
849 REG_PATTERN_TEST(E1000_RDLEN(0), 0x000FFF80, 0x000FFFFF);
850 REG_PATTERN_TEST(E1000_RDH(0), 0x0000FFFF, 0x0000FFFF);
851 REG_PATTERN_TEST(E1000_RDT(0), 0x0000FFFF, 0x0000FFFF);
852 REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
853 REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
854 REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
855 REG_PATTERN_TEST(E1000_TDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
856 REG_PATTERN_TEST(E1000_TDLEN(0), 0x000FFF80, 0x000FFFFF);
858 REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
860 before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
861 REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
862 REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
864 REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
865 REG_PATTERN_TEST(E1000_RDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
866 if (!(adapter->flags & FLAG_IS_ICH))
867 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
868 REG_PATTERN_TEST(E1000_TDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
869 REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
870 mask = 0x8003FFFF;
871 switch (mac->type) {
872 case e1000_ich10lan:
873 case e1000_pchlan:
874 case e1000_pch2lan:
875 case e1000_pch_lpt:
876 mask |= (1 << 18);
877 break;
878 default:
879 break;
882 if (mac->type == e1000_pch_lpt)
883 wlock_mac = (er32(FWSM) & E1000_FWSM_WLOCK_MAC_MASK) >>
884 E1000_FWSM_WLOCK_MAC_SHIFT;
886 for (i = 0; i < mac->rar_entry_count; i++) {
887 if (mac->type == e1000_pch_lpt) {
888 /* Cannot test write-protected SHRAL[n] registers */
889 if ((wlock_mac == 1) || (wlock_mac && (i > wlock_mac)))
890 continue;
892 /* SHRAH[9] different than the others */
893 if (i == 10)
894 mask |= (1 << 30);
895 else
896 mask &= ~(1 << 30);
899 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1), mask,
900 0xFFFFFFFF);
903 for (i = 0; i < mac->mta_reg_count; i++)
904 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
906 *data = 0;
908 return 0;
911 static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
913 u16 temp;
914 u16 checksum = 0;
915 u16 i;
917 *data = 0;
918 /* Read and add up the contents of the EEPROM */
919 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
920 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
921 *data = 1;
922 return *data;
924 checksum += temp;
927 /* If Checksum is not Correct return error else test passed */
928 if ((checksum != (u16) NVM_SUM) && !(*data))
929 *data = 2;
931 return *data;
934 static irqreturn_t e1000_test_intr(int __always_unused irq, void *data)
936 struct net_device *netdev = (struct net_device *) data;
937 struct e1000_adapter *adapter = netdev_priv(netdev);
938 struct e1000_hw *hw = &adapter->hw;
940 adapter->test_icr |= er32(ICR);
942 return IRQ_HANDLED;
945 static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
947 struct net_device *netdev = adapter->netdev;
948 struct e1000_hw *hw = &adapter->hw;
949 u32 mask;
950 u32 shared_int = 1;
951 u32 irq = adapter->pdev->irq;
952 int i;
953 int ret_val = 0;
954 int int_mode = E1000E_INT_MODE_LEGACY;
956 *data = 0;
958 /* NOTE: we don't test MSI/MSI-X interrupts here, yet */
959 if (adapter->int_mode == E1000E_INT_MODE_MSIX) {
960 int_mode = adapter->int_mode;
961 e1000e_reset_interrupt_capability(adapter);
962 adapter->int_mode = E1000E_INT_MODE_LEGACY;
963 e1000e_set_interrupt_capability(adapter);
965 /* Hook up test interrupt handler just for this test */
966 if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
967 netdev)) {
968 shared_int = 0;
969 } else if (request_irq(irq, e1000_test_intr, IRQF_SHARED,
970 netdev->name, netdev)) {
971 *data = 1;
972 ret_val = -1;
973 goto out;
975 e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
977 /* Disable all the interrupts */
978 ew32(IMC, 0xFFFFFFFF);
979 e1e_flush();
980 usleep_range(10000, 20000);
982 /* Test each interrupt */
983 for (i = 0; i < 10; i++) {
984 /* Interrupt to test */
985 mask = 1 << i;
987 if (adapter->flags & FLAG_IS_ICH) {
988 switch (mask) {
989 case E1000_ICR_RXSEQ:
990 continue;
991 case 0x00000100:
992 if (adapter->hw.mac.type == e1000_ich8lan ||
993 adapter->hw.mac.type == e1000_ich9lan)
994 continue;
995 break;
996 default:
997 break;
1001 if (!shared_int) {
1002 /* Disable the interrupt to be reported in
1003 * the cause register and then force the same
1004 * interrupt and see if one gets posted. If
1005 * an interrupt was posted to the bus, the
1006 * test failed.
1008 adapter->test_icr = 0;
1009 ew32(IMC, mask);
1010 ew32(ICS, mask);
1011 e1e_flush();
1012 usleep_range(10000, 20000);
1014 if (adapter->test_icr & mask) {
1015 *data = 3;
1016 break;
1020 /* Enable the interrupt to be reported in
1021 * the cause register and then force the same
1022 * interrupt and see if one gets posted. If
1023 * an interrupt was not posted to the bus, the
1024 * test failed.
1026 adapter->test_icr = 0;
1027 ew32(IMS, mask);
1028 ew32(ICS, mask);
1029 e1e_flush();
1030 usleep_range(10000, 20000);
1032 if (!(adapter->test_icr & mask)) {
1033 *data = 4;
1034 break;
1037 if (!shared_int) {
1038 /* Disable the other interrupts to be reported in
1039 * the cause register and then force the other
1040 * interrupts and see if any get posted. If
1041 * an interrupt was posted to the bus, the
1042 * test failed.
1044 adapter->test_icr = 0;
1045 ew32(IMC, ~mask & 0x00007FFF);
1046 ew32(ICS, ~mask & 0x00007FFF);
1047 e1e_flush();
1048 usleep_range(10000, 20000);
1050 if (adapter->test_icr) {
1051 *data = 5;
1052 break;
1057 /* Disable all the interrupts */
1058 ew32(IMC, 0xFFFFFFFF);
1059 e1e_flush();
1060 usleep_range(10000, 20000);
1062 /* Unhook test interrupt handler */
1063 free_irq(irq, netdev);
1065 out:
1066 if (int_mode == E1000E_INT_MODE_MSIX) {
1067 e1000e_reset_interrupt_capability(adapter);
1068 adapter->int_mode = int_mode;
1069 e1000e_set_interrupt_capability(adapter);
1072 return ret_val;
1075 static void e1000_free_desc_rings(struct e1000_adapter *adapter)
1077 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1078 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1079 struct pci_dev *pdev = adapter->pdev;
1080 int i;
1082 if (tx_ring->desc && tx_ring->buffer_info) {
1083 for (i = 0; i < tx_ring->count; i++) {
1084 if (tx_ring->buffer_info[i].dma)
1085 dma_unmap_single(&pdev->dev,
1086 tx_ring->buffer_info[i].dma,
1087 tx_ring->buffer_info[i].length,
1088 DMA_TO_DEVICE);
1089 if (tx_ring->buffer_info[i].skb)
1090 dev_kfree_skb(tx_ring->buffer_info[i].skb);
1094 if (rx_ring->desc && rx_ring->buffer_info) {
1095 for (i = 0; i < rx_ring->count; i++) {
1096 if (rx_ring->buffer_info[i].dma)
1097 dma_unmap_single(&pdev->dev,
1098 rx_ring->buffer_info[i].dma,
1099 2048, DMA_FROM_DEVICE);
1100 if (rx_ring->buffer_info[i].skb)
1101 dev_kfree_skb(rx_ring->buffer_info[i].skb);
1105 if (tx_ring->desc) {
1106 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1107 tx_ring->dma);
1108 tx_ring->desc = NULL;
1110 if (rx_ring->desc) {
1111 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1112 rx_ring->dma);
1113 rx_ring->desc = NULL;
1116 kfree(tx_ring->buffer_info);
1117 tx_ring->buffer_info = NULL;
1118 kfree(rx_ring->buffer_info);
1119 rx_ring->buffer_info = NULL;
1122 static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1124 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1125 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1126 struct pci_dev *pdev = adapter->pdev;
1127 struct e1000_hw *hw = &adapter->hw;
1128 u32 rctl;
1129 int i;
1130 int ret_val;
1132 /* Setup Tx descriptor ring and Tx buffers */
1134 if (!tx_ring->count)
1135 tx_ring->count = E1000_DEFAULT_TXD;
1137 tx_ring->buffer_info = kcalloc(tx_ring->count,
1138 sizeof(struct e1000_buffer),
1139 GFP_KERNEL);
1140 if (!tx_ring->buffer_info) {
1141 ret_val = 1;
1142 goto err_nomem;
1145 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1146 tx_ring->size = ALIGN(tx_ring->size, 4096);
1147 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
1148 &tx_ring->dma, GFP_KERNEL);
1149 if (!tx_ring->desc) {
1150 ret_val = 2;
1151 goto err_nomem;
1153 tx_ring->next_to_use = 0;
1154 tx_ring->next_to_clean = 0;
1156 ew32(TDBAL(0), ((u64) tx_ring->dma & 0x00000000FFFFFFFF));
1157 ew32(TDBAH(0), ((u64) tx_ring->dma >> 32));
1158 ew32(TDLEN(0), tx_ring->count * sizeof(struct e1000_tx_desc));
1159 ew32(TDH(0), 0);
1160 ew32(TDT(0), 0);
1161 ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN | E1000_TCTL_MULR |
1162 E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1163 E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT);
1165 for (i = 0; i < tx_ring->count; i++) {
1166 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
1167 struct sk_buff *skb;
1168 unsigned int skb_size = 1024;
1170 skb = alloc_skb(skb_size, GFP_KERNEL);
1171 if (!skb) {
1172 ret_val = 3;
1173 goto err_nomem;
1175 skb_put(skb, skb_size);
1176 tx_ring->buffer_info[i].skb = skb;
1177 tx_ring->buffer_info[i].length = skb->len;
1178 tx_ring->buffer_info[i].dma =
1179 dma_map_single(&pdev->dev, skb->data, skb->len,
1180 DMA_TO_DEVICE);
1181 if (dma_mapping_error(&pdev->dev,
1182 tx_ring->buffer_info[i].dma)) {
1183 ret_val = 4;
1184 goto err_nomem;
1186 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma);
1187 tx_desc->lower.data = cpu_to_le32(skb->len);
1188 tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1189 E1000_TXD_CMD_IFCS |
1190 E1000_TXD_CMD_RS);
1191 tx_desc->upper.data = 0;
1194 /* Setup Rx descriptor ring and Rx buffers */
1196 if (!rx_ring->count)
1197 rx_ring->count = E1000_DEFAULT_RXD;
1199 rx_ring->buffer_info = kcalloc(rx_ring->count,
1200 sizeof(struct e1000_buffer),
1201 GFP_KERNEL);
1202 if (!rx_ring->buffer_info) {
1203 ret_val = 5;
1204 goto err_nomem;
1207 rx_ring->size = rx_ring->count * sizeof(union e1000_rx_desc_extended);
1208 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
1209 &rx_ring->dma, GFP_KERNEL);
1210 if (!rx_ring->desc) {
1211 ret_val = 6;
1212 goto err_nomem;
1214 rx_ring->next_to_use = 0;
1215 rx_ring->next_to_clean = 0;
1217 rctl = er32(RCTL);
1218 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
1219 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1220 ew32(RDBAL(0), ((u64) rx_ring->dma & 0xFFFFFFFF));
1221 ew32(RDBAH(0), ((u64) rx_ring->dma >> 32));
1222 ew32(RDLEN(0), rx_ring->size);
1223 ew32(RDH(0), 0);
1224 ew32(RDT(0), 0);
1225 rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
1226 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1227 E1000_RCTL_SBP | E1000_RCTL_SECRC |
1228 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1229 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1230 ew32(RCTL, rctl);
1232 for (i = 0; i < rx_ring->count; i++) {
1233 union e1000_rx_desc_extended *rx_desc;
1234 struct sk_buff *skb;
1236 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1237 if (!skb) {
1238 ret_val = 7;
1239 goto err_nomem;
1241 skb_reserve(skb, NET_IP_ALIGN);
1242 rx_ring->buffer_info[i].skb = skb;
1243 rx_ring->buffer_info[i].dma =
1244 dma_map_single(&pdev->dev, skb->data, 2048,
1245 DMA_FROM_DEVICE);
1246 if (dma_mapping_error(&pdev->dev,
1247 rx_ring->buffer_info[i].dma)) {
1248 ret_val = 8;
1249 goto err_nomem;
1251 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
1252 rx_desc->read.buffer_addr =
1253 cpu_to_le64(rx_ring->buffer_info[i].dma);
1254 memset(skb->data, 0x00, skb->len);
1257 return 0;
1259 err_nomem:
1260 e1000_free_desc_rings(adapter);
1261 return ret_val;
1264 static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1266 /* Write out to PHY registers 29 and 30 to disable the Receiver. */
1267 e1e_wphy(&adapter->hw, 29, 0x001F);
1268 e1e_wphy(&adapter->hw, 30, 0x8FFC);
1269 e1e_wphy(&adapter->hw, 29, 0x001A);
1270 e1e_wphy(&adapter->hw, 30, 0x8FF0);
1273 static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1275 struct e1000_hw *hw = &adapter->hw;
1276 u32 ctrl_reg = 0;
1277 u16 phy_reg = 0;
1278 s32 ret_val = 0;
1280 hw->mac.autoneg = 0;
1282 if (hw->phy.type == e1000_phy_ife) {
1283 /* force 100, set loopback */
1284 e1e_wphy(hw, MII_BMCR, 0x6100);
1286 /* Now set up the MAC to the same speed/duplex as the PHY. */
1287 ctrl_reg = er32(CTRL);
1288 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1289 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1290 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1291 E1000_CTRL_SPD_100 |/* Force Speed to 100 */
1292 E1000_CTRL_FD); /* Force Duplex to FULL */
1294 ew32(CTRL, ctrl_reg);
1295 e1e_flush();
1296 udelay(500);
1298 return 0;
1301 /* Specific PHY configuration for loopback */
1302 switch (hw->phy.type) {
1303 case e1000_phy_m88:
1304 /* Auto-MDI/MDIX Off */
1305 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1306 /* reset to update Auto-MDI/MDIX */
1307 e1e_wphy(hw, MII_BMCR, 0x9140);
1308 /* autoneg off */
1309 e1e_wphy(hw, MII_BMCR, 0x8140);
1310 break;
1311 case e1000_phy_gg82563:
1312 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
1313 break;
1314 case e1000_phy_bm:
1315 /* Set Default MAC Interface speed to 1GB */
1316 e1e_rphy(hw, PHY_REG(2, 21), &phy_reg);
1317 phy_reg &= ~0x0007;
1318 phy_reg |= 0x006;
1319 e1e_wphy(hw, PHY_REG(2, 21), phy_reg);
1320 /* Assert SW reset for above settings to take effect */
1321 hw->phy.ops.commit(hw);
1322 mdelay(1);
1323 /* Force Full Duplex */
1324 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1325 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C);
1326 /* Set Link Up (in force link) */
1327 e1e_rphy(hw, PHY_REG(776, 16), &phy_reg);
1328 e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040);
1329 /* Force Link */
1330 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1331 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040);
1332 /* Set Early Link Enable */
1333 e1e_rphy(hw, PHY_REG(769, 20), &phy_reg);
1334 e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400);
1335 break;
1336 case e1000_phy_82577:
1337 case e1000_phy_82578:
1338 /* Workaround: K1 must be disabled for stable 1Gbps operation */
1339 ret_val = hw->phy.ops.acquire(hw);
1340 if (ret_val) {
1341 e_err("Cannot setup 1Gbps loopback.\n");
1342 return ret_val;
1344 e1000_configure_k1_ich8lan(hw, false);
1345 hw->phy.ops.release(hw);
1346 break;
1347 case e1000_phy_82579:
1348 /* Disable PHY energy detect power down */
1349 e1e_rphy(hw, PHY_REG(0, 21), &phy_reg);
1350 e1e_wphy(hw, PHY_REG(0, 21), phy_reg & ~(1 << 3));
1351 /* Disable full chip energy detect */
1352 e1e_rphy(hw, PHY_REG(776, 18), &phy_reg);
1353 e1e_wphy(hw, PHY_REG(776, 18), phy_reg | 1);
1354 /* Enable loopback on the PHY */
1355 e1e_wphy(hw, I82577_PHY_LBK_CTRL, 0x8001);
1356 break;
1357 default:
1358 break;
1361 /* force 1000, set loopback */
1362 e1e_wphy(hw, MII_BMCR, 0x4140);
1363 mdelay(250);
1365 /* Now set up the MAC to the same speed/duplex as the PHY. */
1366 ctrl_reg = er32(CTRL);
1367 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1368 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1369 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1370 E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1371 E1000_CTRL_FD); /* Force Duplex to FULL */
1373 if (adapter->flags & FLAG_IS_ICH)
1374 ctrl_reg |= E1000_CTRL_SLU; /* Set Link Up */
1376 if (hw->phy.media_type == e1000_media_type_copper &&
1377 hw->phy.type == e1000_phy_m88) {
1378 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1379 } else {
1380 /* Set the ILOS bit on the fiber Nic if half duplex link is
1381 * detected.
1383 if ((er32(STATUS) & E1000_STATUS_FD) == 0)
1384 ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1387 ew32(CTRL, ctrl_reg);
1389 /* Disable the receiver on the PHY so when a cable is plugged in, the
1390 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1392 if (hw->phy.type == e1000_phy_m88)
1393 e1000_phy_disable_receiver(adapter);
1395 udelay(500);
1397 return 0;
1400 static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1402 struct e1000_hw *hw = &adapter->hw;
1403 u32 ctrl = er32(CTRL);
1404 int link;
1406 /* special requirements for 82571/82572 fiber adapters */
1408 /* jump through hoops to make sure link is up because serdes
1409 * link is hardwired up
1411 ctrl |= E1000_CTRL_SLU;
1412 ew32(CTRL, ctrl);
1414 /* disable autoneg */
1415 ctrl = er32(TXCW);
1416 ctrl &= ~(1 << 31);
1417 ew32(TXCW, ctrl);
1419 link = (er32(STATUS) & E1000_STATUS_LU);
1421 if (!link) {
1422 /* set invert loss of signal */
1423 ctrl = er32(CTRL);
1424 ctrl |= E1000_CTRL_ILOS;
1425 ew32(CTRL, ctrl);
1428 /* special write to serdes control register to enable SerDes analog
1429 * loopback
1431 #define E1000_SERDES_LB_ON 0x410
1432 ew32(SCTL, E1000_SERDES_LB_ON);
1433 e1e_flush();
1434 usleep_range(10000, 20000);
1436 return 0;
1439 /* only call this for fiber/serdes connections to es2lan */
1440 static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1442 struct e1000_hw *hw = &adapter->hw;
1443 u32 ctrlext = er32(CTRL_EXT);
1444 u32 ctrl = er32(CTRL);
1446 /* save CTRL_EXT to restore later, reuse an empty variable (unused
1447 * on mac_type 80003es2lan)
1449 adapter->tx_fifo_head = ctrlext;
1451 /* clear the serdes mode bits, putting the device into mac loopback */
1452 ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1453 ew32(CTRL_EXT, ctrlext);
1455 /* force speed to 1000/FD, link up */
1456 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1457 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1458 E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1459 ew32(CTRL, ctrl);
1461 /* set mac loopback */
1462 ctrl = er32(RCTL);
1463 ctrl |= E1000_RCTL_LBM_MAC;
1464 ew32(RCTL, ctrl);
1466 /* set testing mode parameters (no need to reset later) */
1467 #define KMRNCTRLSTA_OPMODE (0x1F << 16)
1468 #define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1469 ew32(KMRNCTRLSTA,
1470 (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
1472 return 0;
1475 static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1477 struct e1000_hw *hw = &adapter->hw;
1478 u32 rctl;
1480 if (hw->phy.media_type == e1000_media_type_fiber ||
1481 hw->phy.media_type == e1000_media_type_internal_serdes) {
1482 switch (hw->mac.type) {
1483 case e1000_80003es2lan:
1484 return e1000_set_es2lan_mac_loopback(adapter);
1485 break;
1486 case e1000_82571:
1487 case e1000_82572:
1488 return e1000_set_82571_fiber_loopback(adapter);
1489 break;
1490 default:
1491 rctl = er32(RCTL);
1492 rctl |= E1000_RCTL_LBM_TCVR;
1493 ew32(RCTL, rctl);
1494 return 0;
1496 } else if (hw->phy.media_type == e1000_media_type_copper) {
1497 return e1000_integrated_phy_loopback(adapter);
1500 return 7;
1503 static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1505 struct e1000_hw *hw = &adapter->hw;
1506 u32 rctl;
1507 u16 phy_reg;
1509 rctl = er32(RCTL);
1510 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1511 ew32(RCTL, rctl);
1513 switch (hw->mac.type) {
1514 case e1000_80003es2lan:
1515 if (hw->phy.media_type == e1000_media_type_fiber ||
1516 hw->phy.media_type == e1000_media_type_internal_serdes) {
1517 /* restore CTRL_EXT, stealing space from tx_fifo_head */
1518 ew32(CTRL_EXT, adapter->tx_fifo_head);
1519 adapter->tx_fifo_head = 0;
1521 /* fall through */
1522 case e1000_82571:
1523 case e1000_82572:
1524 if (hw->phy.media_type == e1000_media_type_fiber ||
1525 hw->phy.media_type == e1000_media_type_internal_serdes) {
1526 #define E1000_SERDES_LB_OFF 0x400
1527 ew32(SCTL, E1000_SERDES_LB_OFF);
1528 e1e_flush();
1529 usleep_range(10000, 20000);
1530 break;
1532 /* Fall Through */
1533 default:
1534 hw->mac.autoneg = 1;
1535 if (hw->phy.type == e1000_phy_gg82563)
1536 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1537 e1e_rphy(hw, MII_BMCR, &phy_reg);
1538 if (phy_reg & BMCR_LOOPBACK) {
1539 phy_reg &= ~BMCR_LOOPBACK;
1540 e1e_wphy(hw, MII_BMCR, phy_reg);
1541 if (hw->phy.ops.commit)
1542 hw->phy.ops.commit(hw);
1544 break;
1548 static void e1000_create_lbtest_frame(struct sk_buff *skb,
1549 unsigned int frame_size)
1551 memset(skb->data, 0xFF, frame_size);
1552 frame_size &= ~1;
1553 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1554 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
1555 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
1558 static int e1000_check_lbtest_frame(struct sk_buff *skb,
1559 unsigned int frame_size)
1561 frame_size &= ~1;
1562 if (*(skb->data + 3) == 0xFF)
1563 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1564 (*(skb->data + frame_size / 2 + 12) == 0xAF))
1565 return 0;
1566 return 13;
1569 static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1571 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1572 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1573 struct pci_dev *pdev = adapter->pdev;
1574 struct e1000_hw *hw = &adapter->hw;
1575 int i, j, k, l;
1576 int lc;
1577 int good_cnt;
1578 int ret_val = 0;
1579 unsigned long time;
1581 ew32(RDT(0), rx_ring->count - 1);
1583 /* Calculate the loop count based on the largest descriptor ring
1584 * The idea is to wrap the largest ring a number of times using 64
1585 * send/receive pairs during each loop
1588 if (rx_ring->count <= tx_ring->count)
1589 lc = ((tx_ring->count / 64) * 2) + 1;
1590 else
1591 lc = ((rx_ring->count / 64) * 2) + 1;
1593 k = 0;
1594 l = 0;
1595 for (j = 0; j <= lc; j++) { /* loop count loop */
1596 for (i = 0; i < 64; i++) { /* send the packets */
1597 e1000_create_lbtest_frame(tx_ring->buffer_info[k].skb,
1598 1024);
1599 dma_sync_single_for_device(&pdev->dev,
1600 tx_ring->buffer_info[k].dma,
1601 tx_ring->buffer_info[k].length,
1602 DMA_TO_DEVICE);
1603 k++;
1604 if (k == tx_ring->count)
1605 k = 0;
1607 ew32(TDT(0), k);
1608 e1e_flush();
1609 msleep(200);
1610 time = jiffies; /* set the start time for the receive */
1611 good_cnt = 0;
1612 do { /* receive the sent packets */
1613 dma_sync_single_for_cpu(&pdev->dev,
1614 rx_ring->buffer_info[l].dma, 2048,
1615 DMA_FROM_DEVICE);
1617 ret_val = e1000_check_lbtest_frame(
1618 rx_ring->buffer_info[l].skb, 1024);
1619 if (!ret_val)
1620 good_cnt++;
1621 l++;
1622 if (l == rx_ring->count)
1623 l = 0;
1624 /* time + 20 msecs (200 msecs on 2.4) is more than
1625 * enough time to complete the receives, if it's
1626 * exceeded, break and error off
1628 } while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1629 if (good_cnt != 64) {
1630 ret_val = 13; /* ret_val is the same as mis-compare */
1631 break;
1633 if (jiffies >= (time + 20)) {
1634 ret_val = 14; /* error code for time out error */
1635 break;
1637 } /* end loop count loop */
1638 return ret_val;
1641 static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1643 struct e1000_hw *hw = &adapter->hw;
1645 /* PHY loopback cannot be performed if SoL/IDER sessions are active */
1646 if (hw->phy.ops.check_reset_block &&
1647 hw->phy.ops.check_reset_block(hw)) {
1648 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
1649 *data = 0;
1650 goto out;
1653 *data = e1000_setup_desc_rings(adapter);
1654 if (*data)
1655 goto out;
1657 *data = e1000_setup_loopback_test(adapter);
1658 if (*data)
1659 goto err_loopback;
1661 *data = e1000_run_loopback_test(adapter);
1662 e1000_loopback_cleanup(adapter);
1664 err_loopback:
1665 e1000_free_desc_rings(adapter);
1666 out:
1667 return *data;
1670 static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1672 struct e1000_hw *hw = &adapter->hw;
1674 *data = 0;
1675 if (hw->phy.media_type == e1000_media_type_internal_serdes) {
1676 int i = 0;
1677 hw->mac.serdes_has_link = false;
1679 /* On some blade server designs, link establishment
1680 * could take as long as 2-3 minutes
1682 do {
1683 hw->mac.ops.check_for_link(hw);
1684 if (hw->mac.serdes_has_link)
1685 return *data;
1686 msleep(20);
1687 } while (i++ < 3750);
1689 *data = 1;
1690 } else {
1691 hw->mac.ops.check_for_link(hw);
1692 if (hw->mac.autoneg)
1693 /* On some Phy/switch combinations, link establishment
1694 * can take a few seconds more than expected.
1696 msleep(5000);
1698 if (!(er32(STATUS) & E1000_STATUS_LU))
1699 *data = 1;
1701 return *data;
1704 static int e1000e_get_sset_count(struct net_device __always_unused *netdev,
1705 int sset)
1707 switch (sset) {
1708 case ETH_SS_TEST:
1709 return E1000_TEST_LEN;
1710 case ETH_SS_STATS:
1711 return E1000_STATS_LEN;
1712 default:
1713 return -EOPNOTSUPP;
1717 static void e1000_diag_test(struct net_device *netdev,
1718 struct ethtool_test *eth_test, u64 *data)
1720 struct e1000_adapter *adapter = netdev_priv(netdev);
1721 u16 autoneg_advertised;
1722 u8 forced_speed_duplex;
1723 u8 autoneg;
1724 bool if_running = netif_running(netdev);
1726 set_bit(__E1000_TESTING, &adapter->state);
1728 if (!if_running) {
1729 /* Get control of and reset hardware */
1730 if (adapter->flags & FLAG_HAS_AMT)
1731 e1000e_get_hw_control(adapter);
1733 e1000e_power_up_phy(adapter);
1735 adapter->hw.phy.autoneg_wait_to_complete = 1;
1736 e1000e_reset(adapter);
1737 adapter->hw.phy.autoneg_wait_to_complete = 0;
1740 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1741 /* Offline tests */
1743 /* save speed, duplex, autoneg settings */
1744 autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1745 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1746 autoneg = adapter->hw.mac.autoneg;
1748 e_info("offline testing starting\n");
1750 if (if_running)
1751 /* indicate we're in test mode */
1752 dev_close(netdev);
1754 if (e1000_reg_test(adapter, &data[0]))
1755 eth_test->flags |= ETH_TEST_FL_FAILED;
1757 e1000e_reset(adapter);
1758 if (e1000_eeprom_test(adapter, &data[1]))
1759 eth_test->flags |= ETH_TEST_FL_FAILED;
1761 e1000e_reset(adapter);
1762 if (e1000_intr_test(adapter, &data[2]))
1763 eth_test->flags |= ETH_TEST_FL_FAILED;
1765 e1000e_reset(adapter);
1766 if (e1000_loopback_test(adapter, &data[3]))
1767 eth_test->flags |= ETH_TEST_FL_FAILED;
1769 /* force this routine to wait until autoneg complete/timeout */
1770 adapter->hw.phy.autoneg_wait_to_complete = 1;
1771 e1000e_reset(adapter);
1772 adapter->hw.phy.autoneg_wait_to_complete = 0;
1774 if (e1000_link_test(adapter, &data[4]))
1775 eth_test->flags |= ETH_TEST_FL_FAILED;
1777 /* restore speed, duplex, autoneg settings */
1778 adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1779 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1780 adapter->hw.mac.autoneg = autoneg;
1781 e1000e_reset(adapter);
1783 clear_bit(__E1000_TESTING, &adapter->state);
1784 if (if_running)
1785 dev_open(netdev);
1786 } else {
1787 /* Online tests */
1789 e_info("online testing starting\n");
1791 /* register, eeprom, intr and loopback tests not run online */
1792 data[0] = 0;
1793 data[1] = 0;
1794 data[2] = 0;
1795 data[3] = 0;
1797 if (e1000_link_test(adapter, &data[4]))
1798 eth_test->flags |= ETH_TEST_FL_FAILED;
1800 clear_bit(__E1000_TESTING, &adapter->state);
1803 if (!if_running) {
1804 e1000e_reset(adapter);
1806 if (adapter->flags & FLAG_HAS_AMT)
1807 e1000e_release_hw_control(adapter);
1810 msleep_interruptible(4 * 1000);
1813 static void e1000_get_wol(struct net_device *netdev,
1814 struct ethtool_wolinfo *wol)
1816 struct e1000_adapter *adapter = netdev_priv(netdev);
1818 wol->supported = 0;
1819 wol->wolopts = 0;
1821 if (!(adapter->flags & FLAG_HAS_WOL) ||
1822 !device_can_wakeup(&adapter->pdev->dev))
1823 return;
1825 wol->supported = WAKE_UCAST | WAKE_MCAST |
1826 WAKE_BCAST | WAKE_MAGIC | WAKE_PHY;
1828 /* apply any specific unsupported masks here */
1829 if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1830 wol->supported &= ~WAKE_UCAST;
1832 if (adapter->wol & E1000_WUFC_EX)
1833 e_err("Interface does not support directed (unicast) frame wake-up packets\n");
1836 if (adapter->wol & E1000_WUFC_EX)
1837 wol->wolopts |= WAKE_UCAST;
1838 if (adapter->wol & E1000_WUFC_MC)
1839 wol->wolopts |= WAKE_MCAST;
1840 if (adapter->wol & E1000_WUFC_BC)
1841 wol->wolopts |= WAKE_BCAST;
1842 if (adapter->wol & E1000_WUFC_MAG)
1843 wol->wolopts |= WAKE_MAGIC;
1844 if (adapter->wol & E1000_WUFC_LNKC)
1845 wol->wolopts |= WAKE_PHY;
1848 static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1850 struct e1000_adapter *adapter = netdev_priv(netdev);
1852 if (!(adapter->flags & FLAG_HAS_WOL) ||
1853 !device_can_wakeup(&adapter->pdev->dev) ||
1854 (wol->wolopts & ~(WAKE_UCAST | WAKE_MCAST | WAKE_BCAST |
1855 WAKE_MAGIC | WAKE_PHY)))
1856 return -EOPNOTSUPP;
1858 /* these settings will always override what we currently have */
1859 adapter->wol = 0;
1861 if (wol->wolopts & WAKE_UCAST)
1862 adapter->wol |= E1000_WUFC_EX;
1863 if (wol->wolopts & WAKE_MCAST)
1864 adapter->wol |= E1000_WUFC_MC;
1865 if (wol->wolopts & WAKE_BCAST)
1866 adapter->wol |= E1000_WUFC_BC;
1867 if (wol->wolopts & WAKE_MAGIC)
1868 adapter->wol |= E1000_WUFC_MAG;
1869 if (wol->wolopts & WAKE_PHY)
1870 adapter->wol |= E1000_WUFC_LNKC;
1872 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1874 return 0;
1877 static int e1000_set_phys_id(struct net_device *netdev,
1878 enum ethtool_phys_id_state state)
1880 struct e1000_adapter *adapter = netdev_priv(netdev);
1881 struct e1000_hw *hw = &adapter->hw;
1883 switch (state) {
1884 case ETHTOOL_ID_ACTIVE:
1885 if (!hw->mac.ops.blink_led)
1886 return 2; /* cycle on/off twice per second */
1888 hw->mac.ops.blink_led(hw);
1889 break;
1891 case ETHTOOL_ID_INACTIVE:
1892 if (hw->phy.type == e1000_phy_ife)
1893 e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
1894 hw->mac.ops.led_off(hw);
1895 hw->mac.ops.cleanup_led(hw);
1896 break;
1898 case ETHTOOL_ID_ON:
1899 hw->mac.ops.led_on(hw);
1900 break;
1902 case ETHTOOL_ID_OFF:
1903 hw->mac.ops.led_off(hw);
1904 break;
1906 return 0;
1909 static int e1000_get_coalesce(struct net_device *netdev,
1910 struct ethtool_coalesce *ec)
1912 struct e1000_adapter *adapter = netdev_priv(netdev);
1914 if (adapter->itr_setting <= 4)
1915 ec->rx_coalesce_usecs = adapter->itr_setting;
1916 else
1917 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1919 return 0;
1922 static int e1000_set_coalesce(struct net_device *netdev,
1923 struct ethtool_coalesce *ec)
1925 struct e1000_adapter *adapter = netdev_priv(netdev);
1927 if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
1928 ((ec->rx_coalesce_usecs > 4) &&
1929 (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1930 (ec->rx_coalesce_usecs == 2))
1931 return -EINVAL;
1933 if (ec->rx_coalesce_usecs == 4) {
1934 adapter->itr_setting = 4;
1935 adapter->itr = adapter->itr_setting;
1936 } else if (ec->rx_coalesce_usecs <= 3) {
1937 adapter->itr = 20000;
1938 adapter->itr_setting = ec->rx_coalesce_usecs;
1939 } else {
1940 adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1941 adapter->itr_setting = adapter->itr & ~3;
1944 if (adapter->itr_setting != 0)
1945 e1000e_write_itr(adapter, adapter->itr);
1946 else
1947 e1000e_write_itr(adapter, 0);
1949 return 0;
1952 static int e1000_nway_reset(struct net_device *netdev)
1954 struct e1000_adapter *adapter = netdev_priv(netdev);
1956 if (!netif_running(netdev))
1957 return -EAGAIN;
1959 if (!adapter->hw.mac.autoneg)
1960 return -EINVAL;
1962 e1000e_reinit_locked(adapter);
1964 return 0;
1967 static void e1000_get_ethtool_stats(struct net_device *netdev,
1968 struct ethtool_stats __always_unused *stats,
1969 u64 *data)
1971 struct e1000_adapter *adapter = netdev_priv(netdev);
1972 struct rtnl_link_stats64 net_stats;
1973 int i;
1974 char *p = NULL;
1976 e1000e_get_stats64(netdev, &net_stats);
1977 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1978 switch (e1000_gstrings_stats[i].type) {
1979 case NETDEV_STATS:
1980 p = (char *) &net_stats +
1981 e1000_gstrings_stats[i].stat_offset;
1982 break;
1983 case E1000_STATS:
1984 p = (char *) adapter +
1985 e1000_gstrings_stats[i].stat_offset;
1986 break;
1987 default:
1988 data[i] = 0;
1989 continue;
1992 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
1993 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1997 static void e1000_get_strings(struct net_device __always_unused *netdev,
1998 u32 stringset, u8 *data)
2000 u8 *p = data;
2001 int i;
2003 switch (stringset) {
2004 case ETH_SS_TEST:
2005 memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
2006 break;
2007 case ETH_SS_STATS:
2008 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
2009 memcpy(p, e1000_gstrings_stats[i].stat_string,
2010 ETH_GSTRING_LEN);
2011 p += ETH_GSTRING_LEN;
2013 break;
2017 static int e1000_get_rxnfc(struct net_device *netdev,
2018 struct ethtool_rxnfc *info,
2019 u32 __always_unused *rule_locs)
2021 info->data = 0;
2023 switch (info->cmd) {
2024 case ETHTOOL_GRXFH: {
2025 struct e1000_adapter *adapter = netdev_priv(netdev);
2026 struct e1000_hw *hw = &adapter->hw;
2027 u32 mrqc = er32(MRQC);
2029 if (!(mrqc & E1000_MRQC_RSS_FIELD_MASK))
2030 return 0;
2032 switch (info->flow_type) {
2033 case TCP_V4_FLOW:
2034 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_TCP)
2035 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2036 /* fall through */
2037 case UDP_V4_FLOW:
2038 case SCTP_V4_FLOW:
2039 case AH_ESP_V4_FLOW:
2040 case IPV4_FLOW:
2041 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4)
2042 info->data |= RXH_IP_SRC | RXH_IP_DST;
2043 break;
2044 case TCP_V6_FLOW:
2045 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP)
2046 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2047 /* fall through */
2048 case UDP_V6_FLOW:
2049 case SCTP_V6_FLOW:
2050 case AH_ESP_V6_FLOW:
2051 case IPV6_FLOW:
2052 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6)
2053 info->data |= RXH_IP_SRC | RXH_IP_DST;
2054 break;
2055 default:
2056 break;
2058 return 0;
2060 default:
2061 return -EOPNOTSUPP;
2065 static int e1000e_get_eee(struct net_device *netdev, struct ethtool_eee *edata)
2067 struct e1000_adapter *adapter = netdev_priv(netdev);
2068 struct e1000_hw *hw = &adapter->hw;
2069 u16 cap_addr, adv_addr, lpa_addr, pcs_stat_addr, phy_data, lpi_ctrl;
2070 u32 status, ret_val;
2072 if (!(adapter->flags & FLAG_IS_ICH) ||
2073 !(adapter->flags2 & FLAG2_HAS_EEE))
2074 return -EOPNOTSUPP;
2076 switch (hw->phy.type) {
2077 case e1000_phy_82579:
2078 cap_addr = I82579_EEE_CAPABILITY;
2079 adv_addr = I82579_EEE_ADVERTISEMENT;
2080 lpa_addr = I82579_EEE_LP_ABILITY;
2081 pcs_stat_addr = I82579_EEE_PCS_STATUS;
2082 break;
2083 case e1000_phy_i217:
2084 cap_addr = I217_EEE_CAPABILITY;
2085 adv_addr = I217_EEE_ADVERTISEMENT;
2086 lpa_addr = I217_EEE_LP_ABILITY;
2087 pcs_stat_addr = I217_EEE_PCS_STATUS;
2088 break;
2089 default:
2090 return -EOPNOTSUPP;
2093 ret_val = hw->phy.ops.acquire(hw);
2094 if (ret_val)
2095 return -EBUSY;
2097 /* EEE Capability */
2098 ret_val = e1000_read_emi_reg_locked(hw, cap_addr, &phy_data);
2099 if (ret_val)
2100 goto release;
2101 edata->supported = mmd_eee_cap_to_ethtool_sup_t(phy_data);
2103 /* EEE Advertised */
2104 ret_val = e1000_read_emi_reg_locked(hw, adv_addr, &phy_data);
2105 if (ret_val)
2106 goto release;
2107 edata->advertised = mmd_eee_adv_to_ethtool_adv_t(phy_data);
2109 /* EEE Link Partner Advertised */
2110 ret_val = e1000_read_emi_reg_locked(hw, lpa_addr, &phy_data);
2111 if (ret_val)
2112 goto release;
2113 edata->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(phy_data);
2115 /* EEE PCS Status */
2116 ret_val = e1000_read_emi_reg_locked(hw, pcs_stat_addr, &phy_data);
2117 if (hw->phy.type == e1000_phy_82579)
2118 phy_data <<= 8;
2120 release:
2121 hw->phy.ops.release(hw);
2122 if (ret_val)
2123 return -ENODATA;
2125 e1e_rphy(hw, I82579_LPI_CTRL, &lpi_ctrl);
2126 status = er32(STATUS);
2128 /* Result of the EEE auto negotiation - there is no register that
2129 * has the status of the EEE negotiation so do a best-guess based
2130 * on whether both Tx and Rx LPI indications have been received or
2131 * base it on the link speed, the EEE advertised speeds on both ends
2132 * and the speeds on which EEE is enabled locally.
2134 if (((phy_data & E1000_EEE_TX_LPI_RCVD) &&
2135 (phy_data & E1000_EEE_RX_LPI_RCVD)) ||
2136 ((status & E1000_STATUS_SPEED_100) &&
2137 (edata->advertised & ADVERTISED_100baseT_Full) &&
2138 (edata->lp_advertised & ADVERTISED_100baseT_Full) &&
2139 (lpi_ctrl & I82579_LPI_CTRL_100_ENABLE)) ||
2140 ((status & E1000_STATUS_SPEED_1000) &&
2141 (edata->advertised & ADVERTISED_1000baseT_Full) &&
2142 (edata->lp_advertised & ADVERTISED_1000baseT_Full) &&
2143 (lpi_ctrl & I82579_LPI_CTRL_1000_ENABLE)))
2144 edata->eee_active = true;
2146 edata->eee_enabled = !hw->dev_spec.ich8lan.eee_disable;
2147 edata->tx_lpi_enabled = true;
2148 edata->tx_lpi_timer = er32(LPIC) >> E1000_LPIC_LPIET_SHIFT;
2150 return 0;
2153 static int e1000e_set_eee(struct net_device *netdev, struct ethtool_eee *edata)
2155 struct e1000_adapter *adapter = netdev_priv(netdev);
2156 struct e1000_hw *hw = &adapter->hw;
2157 struct ethtool_eee eee_curr;
2158 s32 ret_val;
2160 if (!(adapter->flags & FLAG_IS_ICH) ||
2161 !(adapter->flags2 & FLAG2_HAS_EEE))
2162 return -EOPNOTSUPP;
2164 ret_val = e1000e_get_eee(netdev, &eee_curr);
2165 if (ret_val)
2166 return ret_val;
2168 if (eee_curr.advertised != edata->advertised) {
2169 e_err("Setting EEE advertisement is not supported\n");
2170 return -EINVAL;
2173 if (eee_curr.tx_lpi_enabled != edata->tx_lpi_enabled) {
2174 e_err("Setting EEE tx-lpi is not supported\n");
2175 return -EINVAL;
2178 if (eee_curr.tx_lpi_timer != edata->tx_lpi_timer) {
2179 e_err("Setting EEE Tx LPI timer is not supported\n");
2180 return -EINVAL;
2183 if (hw->dev_spec.ich8lan.eee_disable != !edata->eee_enabled) {
2184 hw->dev_spec.ich8lan.eee_disable = !edata->eee_enabled;
2186 /* reset the link */
2187 if (netif_running(netdev))
2188 e1000e_reinit_locked(adapter);
2189 else
2190 e1000e_reset(adapter);
2193 return 0;
2196 static int e1000e_get_ts_info(struct net_device *netdev,
2197 struct ethtool_ts_info *info)
2199 struct e1000_adapter *adapter = netdev_priv(netdev);
2201 ethtool_op_get_ts_info(netdev, info);
2203 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
2204 return 0;
2206 info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
2207 SOF_TIMESTAMPING_RX_HARDWARE |
2208 SOF_TIMESTAMPING_RAW_HARDWARE);
2210 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
2212 info->rx_filters = ((1 << HWTSTAMP_FILTER_NONE) |
2213 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
2214 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
2215 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
2216 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
2217 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
2218 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
2219 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
2220 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
2221 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
2222 (1 << HWTSTAMP_FILTER_ALL));
2224 if (adapter->ptp_clock)
2225 info->phc_index = ptp_clock_index(adapter->ptp_clock);
2227 return 0;
2230 static const struct ethtool_ops e1000_ethtool_ops = {
2231 .get_settings = e1000_get_settings,
2232 .set_settings = e1000_set_settings,
2233 .get_drvinfo = e1000_get_drvinfo,
2234 .get_regs_len = e1000_get_regs_len,
2235 .get_regs = e1000_get_regs,
2236 .get_wol = e1000_get_wol,
2237 .set_wol = e1000_set_wol,
2238 .get_msglevel = e1000_get_msglevel,
2239 .set_msglevel = e1000_set_msglevel,
2240 .nway_reset = e1000_nway_reset,
2241 .get_link = ethtool_op_get_link,
2242 .get_eeprom_len = e1000_get_eeprom_len,
2243 .get_eeprom = e1000_get_eeprom,
2244 .set_eeprom = e1000_set_eeprom,
2245 .get_ringparam = e1000_get_ringparam,
2246 .set_ringparam = e1000_set_ringparam,
2247 .get_pauseparam = e1000_get_pauseparam,
2248 .set_pauseparam = e1000_set_pauseparam,
2249 .self_test = e1000_diag_test,
2250 .get_strings = e1000_get_strings,
2251 .set_phys_id = e1000_set_phys_id,
2252 .get_ethtool_stats = e1000_get_ethtool_stats,
2253 .get_sset_count = e1000e_get_sset_count,
2254 .get_coalesce = e1000_get_coalesce,
2255 .set_coalesce = e1000_set_coalesce,
2256 .get_rxnfc = e1000_get_rxnfc,
2257 .get_ts_info = e1000e_get_ts_info,
2258 .get_eee = e1000e_get_eee,
2259 .set_eee = e1000e_set_eee,
2262 void e1000e_set_ethtool_ops(struct net_device *netdev)
2264 SET_ETHTOOL_OPS(netdev, &e1000_ethtool_ops);