blob: 2e76f06720fd28fe7fbe2e701bc5b2915fc02813 [file] [log] [blame]
Auke Kokbc7f75f2007-09-17 12:30:59 -07001/*******************************************************************************
2
3 Intel PRO/1000 Linux driver
Bruce Allanf5e261e2012-01-01 16:00:03 +00004 Copyright(c) 1999 - 2012 Intel Corporation.
Auke Kokbc7f75f2007-09-17 12:30:59 -07005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
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
26
27*******************************************************************************/
28
29/* ethtool support for e1000 */
30
31#include <linux/netdevice.h>
Bruce Allan9fb7a5f2011-07-29 05:52:51 +000032#include <linux/interrupt.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070033#include <linux/ethtool.h>
34#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070036#include <linux/delay.h>
David S. Millerc85c21a2012-01-26 16:25:55 -050037#include <linux/vmalloc.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070038
39#include "e1000.h"
40
Ajit Khapardee0f36a92009-10-13 01:45:09 +000041enum {NETDEV_STATS, E1000_STATS};
42
Auke Kokbc7f75f2007-09-17 12:30:59 -070043struct e1000_stats {
44 char stat_string[ETH_GSTRING_LEN];
Ajit Khapardee0f36a92009-10-13 01:45:09 +000045 int type;
Auke Kokbc7f75f2007-09-17 12:30:59 -070046 int sizeof_stat;
47 int stat_offset;
48};
49
Bruce Allanf0f1a172010-12-11 05:53:32 +000050#define E1000_STAT(str, m) { \
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000051 .stat_string = str, \
52 .type = E1000_STATS, \
53 .sizeof_stat = sizeof(((struct e1000_adapter *)0)->m), \
54 .stat_offset = offsetof(struct e1000_adapter, m) }
Bruce Allanf0f1a172010-12-11 05:53:32 +000055#define E1000_NETDEV_STAT(str, m) { \
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000056 .stat_string = str, \
57 .type = NETDEV_STATS, \
58 .sizeof_stat = sizeof(((struct rtnl_link_stats64 *)0)->m), \
59 .stat_offset = offsetof(struct rtnl_link_stats64, m) }
Ajit Khapardee0f36a92009-10-13 01:45:09 +000060
Auke Kokbc7f75f2007-09-17 12:30:59 -070061static const struct e1000_stats e1000_gstrings_stats[] = {
Bruce Allanf0f1a172010-12-11 05:53:32 +000062 E1000_STAT("rx_packets", stats.gprc),
63 E1000_STAT("tx_packets", stats.gptc),
64 E1000_STAT("rx_bytes", stats.gorc),
65 E1000_STAT("tx_bytes", stats.gotc),
66 E1000_STAT("rx_broadcast", stats.bprc),
67 E1000_STAT("tx_broadcast", stats.bptc),
68 E1000_STAT("rx_multicast", stats.mprc),
69 E1000_STAT("tx_multicast", stats.mptc),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000070 E1000_NETDEV_STAT("rx_errors", rx_errors),
71 E1000_NETDEV_STAT("tx_errors", tx_errors),
72 E1000_NETDEV_STAT("tx_dropped", tx_dropped),
Bruce Allanf0f1a172010-12-11 05:53:32 +000073 E1000_STAT("multicast", stats.mprc),
74 E1000_STAT("collisions", stats.colc),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000075 E1000_NETDEV_STAT("rx_length_errors", rx_length_errors),
76 E1000_NETDEV_STAT("rx_over_errors", rx_over_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000077 E1000_STAT("rx_crc_errors", stats.crcerrs),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000078 E1000_NETDEV_STAT("rx_frame_errors", rx_frame_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000079 E1000_STAT("rx_no_buffer_count", stats.rnbc),
80 E1000_STAT("rx_missed_errors", stats.mpc),
81 E1000_STAT("tx_aborted_errors", stats.ecol),
82 E1000_STAT("tx_carrier_errors", stats.tncrs),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000083 E1000_NETDEV_STAT("tx_fifo_errors", tx_fifo_errors),
84 E1000_NETDEV_STAT("tx_heartbeat_errors", tx_heartbeat_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000085 E1000_STAT("tx_window_errors", stats.latecol),
86 E1000_STAT("tx_abort_late_coll", stats.latecol),
87 E1000_STAT("tx_deferred_ok", stats.dc),
88 E1000_STAT("tx_single_coll_ok", stats.scc),
89 E1000_STAT("tx_multi_coll_ok", stats.mcc),
90 E1000_STAT("tx_timeout_count", tx_timeout_count),
91 E1000_STAT("tx_restart_queue", restart_queue),
92 E1000_STAT("rx_long_length_errors", stats.roc),
93 E1000_STAT("rx_short_length_errors", stats.ruc),
94 E1000_STAT("rx_align_errors", stats.algnerrc),
95 E1000_STAT("tx_tcp_seg_good", stats.tsctc),
96 E1000_STAT("tx_tcp_seg_failed", stats.tsctfc),
97 E1000_STAT("rx_flow_control_xon", stats.xonrxc),
98 E1000_STAT("rx_flow_control_xoff", stats.xoffrxc),
99 E1000_STAT("tx_flow_control_xon", stats.xontxc),
100 E1000_STAT("tx_flow_control_xoff", stats.xofftxc),
101 E1000_STAT("rx_long_byte_count", stats.gorc),
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),
Auke Kokbc7f75f2007-09-17 12:30:59 -0700111};
112
Alejandro Martinez Ruizc00acf42007-10-18 10:16:33 +0200113#define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700114#define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN)
115static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
116 "Register test (offline)", "Eeprom test (offline)",
117 "Interrupt test (offline)", "Loopback test (offline)",
118 "Link test (on/offline)"
119};
Bruce Allanad680762008-03-28 09:15:03 -0700120#define E1000_TEST_LEN ARRAY_SIZE(e1000_gstrings_test)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700121
122static int e1000_get_settings(struct net_device *netdev,
123 struct ethtool_cmd *ecmd)
124{
125 struct e1000_adapter *adapter = netdev_priv(netdev);
126 struct e1000_hw *hw = &adapter->hw;
David Decotigny70739492011-04-27 18:32:40 +0000127 u32 speed;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700128
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700129 if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700130
131 ecmd->supported = (SUPPORTED_10baseT_Half |
132 SUPPORTED_10baseT_Full |
133 SUPPORTED_100baseT_Half |
134 SUPPORTED_100baseT_Full |
135 SUPPORTED_1000baseT_Full |
136 SUPPORTED_Autoneg |
137 SUPPORTED_TP);
138 if (hw->phy.type == e1000_phy_ife)
139 ecmd->supported &= ~SUPPORTED_1000baseT_Full;
140 ecmd->advertising = ADVERTISED_TP;
141
142 if (hw->mac.autoneg == 1) {
143 ecmd->advertising |= ADVERTISED_Autoneg;
144 /* the e1000 autoneg seems to match ethtool nicely */
145 ecmd->advertising |= hw->phy.autoneg_advertised;
146 }
147
148 ecmd->port = PORT_TP;
149 ecmd->phy_address = hw->phy.addr;
150 ecmd->transceiver = XCVR_INTERNAL;
151
152 } else {
153 ecmd->supported = (SUPPORTED_1000baseT_Full |
154 SUPPORTED_FIBRE |
155 SUPPORTED_Autoneg);
156
157 ecmd->advertising = (ADVERTISED_1000baseT_Full |
158 ADVERTISED_FIBRE |
159 ADVERTISED_Autoneg);
160
161 ecmd->port = PORT_FIBRE;
162 ecmd->transceiver = XCVR_EXTERNAL;
163 }
164
David Decotigny70739492011-04-27 18:32:40 +0000165 speed = -1;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000166 ecmd->duplex = -1;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700167
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000168 if (netif_running(netdev)) {
169 if (netif_carrier_ok(netdev)) {
David Decotigny70739492011-04-27 18:32:40 +0000170 speed = adapter->link_speed;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000171 ecmd->duplex = adapter->link_duplex - 1;
172 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700173 } else {
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000174 u32 status = er32(STATUS);
175 if (status & E1000_STATUS_LU) {
176 if (status & E1000_STATUS_SPEED_1000)
David Decotigny70739492011-04-27 18:32:40 +0000177 speed = SPEED_1000;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000178 else if (status & E1000_STATUS_SPEED_100)
David Decotigny70739492011-04-27 18:32:40 +0000179 speed = SPEED_100;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000180 else
David Decotigny70739492011-04-27 18:32:40 +0000181 speed = SPEED_10;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000182
183 if (status & E1000_STATUS_FD)
184 ecmd->duplex = DUPLEX_FULL;
185 else
186 ecmd->duplex = DUPLEX_HALF;
187 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700188 }
189
David Decotigny70739492011-04-27 18:32:40 +0000190 ethtool_cmd_speed_set(ecmd, speed);
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700191 ecmd->autoneg = ((hw->phy.media_type == e1000_media_type_fiber) ||
Auke Kokbc7f75f2007-09-17 12:30:59 -0700192 hw->mac.autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
Chaitanya Lala18760f12009-06-08 14:28:54 +0000193
194 /* MDI-X => 2; MDI =>1; Invalid =>0 */
195 if ((hw->phy.media_type == e1000_media_type_copper) &&
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000196 netif_carrier_ok(netdev))
Chaitanya Lala18760f12009-06-08 14:28:54 +0000197 ecmd->eth_tp_mdix = hw->phy.is_mdix ? ETH_TP_MDI_X :
198 ETH_TP_MDI;
199 else
200 ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
201
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000202 if (hw->phy.mdix == AUTO_ALL_MODES)
203 ecmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
204 else
205 ecmd->eth_tp_mdix_ctrl = hw->phy.mdix;
206
Auke Kokbc7f75f2007-09-17 12:30:59 -0700207 return 0;
208}
209
David Decotigny14ad2512011-04-27 18:32:43 +0000210static int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700211{
212 struct e1000_mac_info *mac = &adapter->hw.mac;
213
214 mac->autoneg = 0;
215
David Decotigny14ad2512011-04-27 18:32:43 +0000216 /* Make sure dplx is at most 1 bit and lsb of speed is not set
217 * for the switch() below to work */
218 if ((spd & 1) || (dplx & ~1))
219 goto err_inval;
220
Auke Kokbc7f75f2007-09-17 12:30:59 -0700221 /* Fiber NICs only allow 1000 gbps Full duplex */
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700222 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +0000223 spd != SPEED_1000 &&
224 dplx != DUPLEX_FULL) {
225 goto err_inval;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700226 }
227
David Decotigny14ad2512011-04-27 18:32:43 +0000228 switch (spd + dplx) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700229 case SPEED_10 + DUPLEX_HALF:
230 mac->forced_speed_duplex = ADVERTISE_10_HALF;
231 break;
232 case SPEED_10 + DUPLEX_FULL:
233 mac->forced_speed_duplex = ADVERTISE_10_FULL;
234 break;
235 case SPEED_100 + DUPLEX_HALF:
236 mac->forced_speed_duplex = ADVERTISE_100_HALF;
237 break;
238 case SPEED_100 + DUPLEX_FULL:
239 mac->forced_speed_duplex = ADVERTISE_100_FULL;
240 break;
241 case SPEED_1000 + DUPLEX_FULL:
242 mac->autoneg = 1;
243 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
244 break;
245 case SPEED_1000 + DUPLEX_HALF: /* not supported */
246 default:
David Decotigny14ad2512011-04-27 18:32:43 +0000247 goto err_inval;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700248 }
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000249
250 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
251 adapter->hw.phy.mdix = AUTO_ALL_MODES;
252
Auke Kokbc7f75f2007-09-17 12:30:59 -0700253 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +0000254
255err_inval:
256 e_err("Unsupported Speed/Duplex configuration\n");
257 return -EINVAL;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700258}
259
260static int e1000_set_settings(struct net_device *netdev,
261 struct ethtool_cmd *ecmd)
262{
263 struct e1000_adapter *adapter = netdev_priv(netdev);
264 struct e1000_hw *hw = &adapter->hw;
265
Bruce Allanad680762008-03-28 09:15:03 -0700266 /*
267 * When SoL/IDER sessions are active, autoneg/speed/duplex
268 * cannot be changed
269 */
Bruce Allan470a5422012-05-26 06:08:48 +0000270 if (hw->phy.ops.check_reset_block &&
271 hw->phy.ops.check_reset_block(hw)) {
Bruce Allan6ad65142012-04-12 05:47:09 +0000272 e_err("Cannot change link characteristics when SoL/IDER is active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700273 return -EINVAL;
274 }
275
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000276 /*
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.
280 */
281 if (ecmd->eth_tp_mdix_ctrl) {
282 if (hw->phy.media_type != e1000_media_type_copper)
283 return -EOPNOTSUPP;
284
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;
289 }
290 }
291
Auke Kokbc7f75f2007-09-17 12:30:59 -0700292 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000293 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700294
295 if (ecmd->autoneg == AUTONEG_ENABLE) {
296 hw->mac.autoneg = 1;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700297 if (hw->phy.media_type == e1000_media_type_fiber)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700298 hw->phy.autoneg_advertised = ADVERTISED_1000baseT_Full |
299 ADVERTISED_FIBRE |
300 ADVERTISED_Autoneg;
301 else
302 hw->phy.autoneg_advertised = ecmd->advertising |
303 ADVERTISED_TP |
304 ADVERTISED_Autoneg;
305 ecmd->advertising = hw->phy.autoneg_advertised;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700306 if (adapter->fc_autoneg)
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800307 hw->fc.requested_mode = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700308 } else {
David Decotigny25db0332011-04-27 18:32:39 +0000309 u32 speed = ethtool_cmd_speed(ecmd);
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000310 /* calling this overrides forced MDI setting */
David Decotigny14ad2512011-04-27 18:32:43 +0000311 if (e1000_set_spd_dplx(adapter, speed, ecmd->duplex)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700312 clear_bit(__E1000_RESETTING, &adapter->state);
313 return -EINVAL;
314 }
315 }
316
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000317 /* MDI-X => 2; MDI => 1; Auto => 3 */
318 if (ecmd->eth_tp_mdix_ctrl) {
319 /*
320 * fix up the value for auto (3 => 0) as zero is mapped
321 * internally to auto
322 */
323 if (ecmd->eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
324 hw->phy.mdix = AUTO_ALL_MODES;
325 else
326 hw->phy.mdix = ecmd->eth_tp_mdix_ctrl;
327 }
328
Auke Kokbc7f75f2007-09-17 12:30:59 -0700329 /* reset the link */
330
331 if (netif_running(adapter->netdev)) {
332 e1000e_down(adapter);
333 e1000e_up(adapter);
Jesse Brandeburg4e8186b2012-07-26 02:31:14 +0000334 } else
Auke Kokbc7f75f2007-09-17 12:30:59 -0700335 e1000e_reset(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700336
337 clear_bit(__E1000_RESETTING, &adapter->state);
338 return 0;
339}
340
341static void e1000_get_pauseparam(struct net_device *netdev,
342 struct ethtool_pauseparam *pause)
343{
344 struct e1000_adapter *adapter = netdev_priv(netdev);
345 struct e1000_hw *hw = &adapter->hw;
346
347 pause->autoneg =
348 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
349
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800350 if (hw->fc.current_mode == e1000_fc_rx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700351 pause->rx_pause = 1;
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800352 } else if (hw->fc.current_mode == e1000_fc_tx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700353 pause->tx_pause = 1;
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800354 } else if (hw->fc.current_mode == e1000_fc_full) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700355 pause->rx_pause = 1;
356 pause->tx_pause = 1;
357 }
358}
359
360static int e1000_set_pauseparam(struct net_device *netdev,
361 struct ethtool_pauseparam *pause)
362{
363 struct e1000_adapter *adapter = netdev_priv(netdev);
364 struct e1000_hw *hw = &adapter->hw;
365 int retval = 0;
366
367 adapter->fc_autoneg = pause->autoneg;
368
369 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000370 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700371
Auke Kokbc7f75f2007-09-17 12:30:59 -0700372 if (adapter->fc_autoneg == AUTONEG_ENABLE) {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800373 hw->fc.requested_mode = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700374 if (netif_running(adapter->netdev)) {
375 e1000e_down(adapter);
376 e1000e_up(adapter);
377 } else {
378 e1000e_reset(adapter);
379 }
380 } else {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800381 if (pause->rx_pause && pause->tx_pause)
382 hw->fc.requested_mode = e1000_fc_full;
383 else if (pause->rx_pause && !pause->tx_pause)
384 hw->fc.requested_mode = e1000_fc_rx_pause;
385 else if (!pause->rx_pause && pause->tx_pause)
386 hw->fc.requested_mode = e1000_fc_tx_pause;
387 else if (!pause->rx_pause && !pause->tx_pause)
388 hw->fc.requested_mode = e1000_fc_none;
389
390 hw->fc.current_mode = hw->fc.requested_mode;
391
Bruce Allan945eb312009-10-28 18:28:30 +0000392 if (hw->phy.media_type == e1000_media_type_fiber) {
393 retval = hw->mac.ops.setup_link(hw);
394 /* implicit goto out */
395 } else {
396 retval = e1000e_force_mac_fc(hw);
397 if (retval)
398 goto out;
399 e1000e_set_fc_watermarks(hw);
400 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700401 }
402
Bruce Allan945eb312009-10-28 18:28:30 +0000403out:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700404 clear_bit(__E1000_RESETTING, &adapter->state);
405 return retval;
406}
407
Auke Kokbc7f75f2007-09-17 12:30:59 -0700408static u32 e1000_get_msglevel(struct net_device *netdev)
409{
410 struct e1000_adapter *adapter = netdev_priv(netdev);
411 return adapter->msg_enable;
412}
413
414static void e1000_set_msglevel(struct net_device *netdev, u32 data)
415{
416 struct e1000_adapter *adapter = netdev_priv(netdev);
417 adapter->msg_enable = data;
418}
419
420static int e1000_get_regs_len(struct net_device *netdev)
421{
422#define E1000_REGS_LEN 32 /* overestimate */
423 return E1000_REGS_LEN * sizeof(u32);
424}
425
426static void e1000_get_regs(struct net_device *netdev,
427 struct ethtool_regs *regs, void *p)
428{
429 struct e1000_adapter *adapter = netdev_priv(netdev);
430 struct e1000_hw *hw = &adapter->hw;
431 u32 *regs_buff = p;
432 u16 phy_data;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700433
434 memset(p, 0, E1000_REGS_LEN * sizeof(u32));
435
Sergei Shtylyovff938e42011-02-28 11:57:33 -0800436 regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
437 adapter->pdev->device;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700438
439 regs_buff[0] = er32(CTRL);
440 regs_buff[1] = er32(STATUS);
441
442 regs_buff[2] = er32(RCTL);
Bruce Allan1e360522012-03-20 03:48:13 +0000443 regs_buff[3] = er32(RDLEN(0));
444 regs_buff[4] = er32(RDH(0));
445 regs_buff[5] = er32(RDT(0));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700446 regs_buff[6] = er32(RDTR);
447
448 regs_buff[7] = er32(TCTL);
Bruce Allan1e360522012-03-20 03:48:13 +0000449 regs_buff[8] = er32(TDLEN(0));
450 regs_buff[9] = er32(TDH(0));
451 regs_buff[10] = er32(TDT(0));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700452 regs_buff[11] = er32(TIDV);
453
454 regs_buff[12] = adapter->hw.phy.type; /* PHY type (IGP=1, M88=0) */
Jesse Brandeburg23033fa2008-10-02 16:33:30 -0700455
456 /* ethtool doesn't use anything past this point, so all this
457 * code is likely legacy junk for apps that may or may not
458 * exist */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700459 if (hw->phy.type == e1000_phy_m88) {
460 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
461 regs_buff[13] = (u32)phy_data; /* cable length */
462 regs_buff[14] = 0; /* Dummy (to align w/ IGP phy reg dump) */
463 regs_buff[15] = 0; /* Dummy (to align w/ IGP phy reg dump) */
464 regs_buff[16] = 0; /* Dummy (to align w/ IGP phy reg dump) */
465 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
466 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
467 regs_buff[18] = regs_buff[13]; /* cable polarity */
468 regs_buff[19] = 0; /* Dummy (to align w/ IGP phy reg dump) */
469 regs_buff[20] = regs_buff[17]; /* polarity correction */
470 /* phy receive errors */
471 regs_buff[22] = adapter->phy_stats.receive_errors;
472 regs_buff[23] = regs_buff[13]; /* mdix mode */
473 }
Jesse Brandeburg23033fa2008-10-02 16:33:30 -0700474 regs_buff[21] = 0; /* was idle_errors */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700475 e1e_rphy(hw, PHY_1000T_STATUS, &phy_data);
476 regs_buff[24] = (u32)phy_data; /* phy local receiver status */
477 regs_buff[25] = regs_buff[24]; /* phy remote receiver status */
478}
479
480static int e1000_get_eeprom_len(struct net_device *netdev)
481{
482 struct e1000_adapter *adapter = netdev_priv(netdev);
483 return adapter->hw.nvm.word_size * 2;
484}
485
486static int e1000_get_eeprom(struct net_device *netdev,
487 struct ethtool_eeprom *eeprom, u8 *bytes)
488{
489 struct e1000_adapter *adapter = netdev_priv(netdev);
490 struct e1000_hw *hw = &adapter->hw;
491 u16 *eeprom_buff;
492 int first_word;
493 int last_word;
494 int ret_val = 0;
495 u16 i;
496
497 if (eeprom->len == 0)
498 return -EINVAL;
499
500 eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16);
501
502 first_word = eeprom->offset >> 1;
503 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
504
505 eeprom_buff = kmalloc(sizeof(u16) *
506 (last_word - first_word + 1), GFP_KERNEL);
507 if (!eeprom_buff)
508 return -ENOMEM;
509
510 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
511 ret_val = e1000_read_nvm(hw, first_word,
512 last_word - first_word + 1,
513 eeprom_buff);
514 } else {
515 for (i = 0; i < last_word - first_word + 1; i++) {
516 ret_val = e1000_read_nvm(hw, first_word + i, 1,
517 &eeprom_buff[i]);
Bruce Allane2434552008-11-21 17:02:41 -0800518 if (ret_val)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700519 break;
520 }
521 }
522
Bruce Allane2434552008-11-21 17:02:41 -0800523 if (ret_val) {
524 /* a read error occurred, throw away the result */
Roel Kluin8528b012009-12-01 15:54:24 +0000525 memset(eeprom_buff, 0xff, sizeof(u16) *
526 (last_word - first_word + 1));
Bruce Allane2434552008-11-21 17:02:41 -0800527 } else {
528 /* Device's eeprom is always little-endian, word addressable */
529 for (i = 0; i < last_word - first_word + 1; i++)
530 le16_to_cpus(&eeprom_buff[i]);
531 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700532
533 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
534 kfree(eeprom_buff);
535
536 return ret_val;
537}
538
539static int e1000_set_eeprom(struct net_device *netdev,
540 struct ethtool_eeprom *eeprom, u8 *bytes)
541{
542 struct e1000_adapter *adapter = netdev_priv(netdev);
543 struct e1000_hw *hw = &adapter->hw;
544 u16 *eeprom_buff;
545 void *ptr;
546 int max_len;
547 int first_word;
548 int last_word;
549 int ret_val = 0;
550 u16 i;
551
552 if (eeprom->len == 0)
553 return -EOPNOTSUPP;
554
555 if (eeprom->magic != (adapter->pdev->vendor | (adapter->pdev->device << 16)))
556 return -EFAULT;
557
Bruce Allan4a770352008-10-01 17:18:35 -0700558 if (adapter->flags & FLAG_READ_ONLY_NVM)
559 return -EINVAL;
560
Auke Kokbc7f75f2007-09-17 12:30:59 -0700561 max_len = hw->nvm.word_size * 2;
562
563 first_word = eeprom->offset >> 1;
564 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
565 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
566 if (!eeprom_buff)
567 return -ENOMEM;
568
569 ptr = (void *)eeprom_buff;
570
571 if (eeprom->offset & 1) {
572 /* need read/modify/write of first changed EEPROM word */
573 /* only the second byte of the word is being modified */
574 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
575 ptr++;
576 }
Bruce Allan9e2d7652012-01-31 06:37:27 +0000577 if (((eeprom->offset + eeprom->len) & 1) && (!ret_val))
Auke Kokbc7f75f2007-09-17 12:30:59 -0700578 /* need read/modify/write of last changed EEPROM word */
579 /* only the first byte of the word is being modified */
580 ret_val = e1000_read_nvm(hw, last_word, 1,
581 &eeprom_buff[last_word - first_word]);
582
Bruce Allane2434552008-11-21 17:02:41 -0800583 if (ret_val)
584 goto out;
585
Auke Kokbc7f75f2007-09-17 12:30:59 -0700586 /* Device's eeprom is always little-endian, word addressable */
587 for (i = 0; i < last_word - first_word + 1; i++)
588 le16_to_cpus(&eeprom_buff[i]);
589
590 memcpy(ptr, bytes, eeprom->len);
591
592 for (i = 0; i < last_word - first_word + 1; i++)
Bruce Allane885d762012-01-31 06:37:32 +0000593 cpu_to_le16s(&eeprom_buff[i]);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700594
595 ret_val = e1000_write_nvm(hw, first_word,
596 last_word - first_word + 1, eeprom_buff);
597
Bruce Allane2434552008-11-21 17:02:41 -0800598 if (ret_val)
599 goto out;
600
Bruce Allanad680762008-03-28 09:15:03 -0700601 /*
602 * Update the checksum over the first part of the EEPROM if needed
Bruce Allane2434552008-11-21 17:02:41 -0800603 * and flush shadow RAM for applicable controllers
Bruce Allanad680762008-03-28 09:15:03 -0700604 */
Bruce Allane2434552008-11-21 17:02:41 -0800605 if ((first_word <= NVM_CHECKSUM_REG) ||
Bruce Allanf89271dd2009-11-20 23:22:20 +0000606 (hw->mac.type == e1000_82583) ||
607 (hw->mac.type == e1000_82574) ||
608 (hw->mac.type == e1000_82573))
Bruce Allane2434552008-11-21 17:02:41 -0800609 ret_val = e1000e_update_nvm_checksum(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700610
Bruce Allane2434552008-11-21 17:02:41 -0800611out:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700612 kfree(eeprom_buff);
613 return ret_val;
614}
615
616static void e1000_get_drvinfo(struct net_device *netdev,
617 struct ethtool_drvinfo *drvinfo)
618{
619 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700620
Rick Jones612a94d2011-11-14 08:13:25 +0000621 strlcpy(drvinfo->driver, e1000e_driver_name,
622 sizeof(drvinfo->driver));
Rick Jones33a5ba12011-11-15 14:59:53 +0000623 strlcpy(drvinfo->version, e1000e_driver_version,
Rick Jones612a94d2011-11-14 08:13:25 +0000624 sizeof(drvinfo->version));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700625
Bruce Allanad680762008-03-28 09:15:03 -0700626 /*
627 * EEPROM image version # is reported as firmware version # for
628 * PCI-E controllers
629 */
Rick Jones612a94d2011-11-14 08:13:25 +0000630 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
631 "%d.%d-%d",
Bruce Allan84527592008-11-21 17:00:22 -0800632 (adapter->eeprom_vers & 0xF000) >> 12,
633 (adapter->eeprom_vers & 0x0FF0) >> 4,
634 (adapter->eeprom_vers & 0x000F));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700635
Rick Jones612a94d2011-11-14 08:13:25 +0000636 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
637 sizeof(drvinfo->bus_info));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700638 drvinfo->regdump_len = e1000_get_regs_len(netdev);
639 drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
640}
641
642static void e1000_get_ringparam(struct net_device *netdev,
643 struct ethtool_ringparam *ring)
644{
645 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700646
647 ring->rx_max_pending = E1000_MAX_RXD;
648 ring->tx_max_pending = E1000_MAX_TXD;
Bruce Allan508da422011-12-16 00:45:51 +0000649 ring->rx_pending = adapter->rx_ring_count;
650 ring->tx_pending = adapter->tx_ring_count;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700651}
652
653static int e1000_set_ringparam(struct net_device *netdev,
654 struct ethtool_ringparam *ring)
655{
656 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan508da422011-12-16 00:45:51 +0000657 struct e1000_ring *temp_tx = NULL, *temp_rx = NULL;
658 int err = 0, size = sizeof(struct e1000_ring);
659 bool set_tx = false, set_rx = false;
660 u16 new_rx_count, new_tx_count;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700661
662 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
663 return -EINVAL;
664
Bruce Allan508da422011-12-16 00:45:51 +0000665 new_rx_count = clamp_t(u32, ring->rx_pending, E1000_MIN_RXD,
666 E1000_MAX_RXD);
667 new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
668
669 new_tx_count = clamp_t(u32, ring->tx_pending, E1000_MIN_TXD,
670 E1000_MAX_TXD);
671 new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
672
673 if ((new_tx_count == adapter->tx_ring_count) &&
674 (new_rx_count == adapter->rx_ring_count))
675 /* nothing to do */
676 return 0;
677
Auke Kokbc7f75f2007-09-17 12:30:59 -0700678 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000679 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700680
Bruce Allan508da422011-12-16 00:45:51 +0000681 if (!netif_running(adapter->netdev)) {
682 /* Set counts now and allocate resources during open() */
683 adapter->tx_ring->count = new_tx_count;
684 adapter->rx_ring->count = new_rx_count;
685 adapter->tx_ring_count = new_tx_count;
686 adapter->rx_ring_count = new_rx_count;
687 goto clear_reset;
688 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700689
Bruce Allan508da422011-12-16 00:45:51 +0000690 set_tx = (new_tx_count != adapter->tx_ring_count);
691 set_rx = (new_rx_count != adapter->rx_ring_count);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700692
Bruce Allan508da422011-12-16 00:45:51 +0000693 /* Allocate temporary storage for ring updates */
694 if (set_tx) {
695 temp_tx = vmalloc(size);
696 if (!temp_tx) {
697 err = -ENOMEM;
698 goto free_temp;
699 }
700 }
701 if (set_rx) {
702 temp_rx = vmalloc(size);
703 if (!temp_rx) {
704 err = -ENOMEM;
705 goto free_temp;
706 }
707 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700708
Bruce Allan508da422011-12-16 00:45:51 +0000709 e1000e_down(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700710
Bruce Allan508da422011-12-16 00:45:51 +0000711 /*
712 * We can't just free everything and then setup again, because the
713 * ISRs in MSI-X mode get passed pointers to the Tx and Rx ring
714 * structs. First, attempt to allocate new resources...
715 */
716 if (set_tx) {
717 memcpy(temp_tx, adapter->tx_ring, size);
718 temp_tx->count = new_tx_count;
719 err = e1000e_setup_tx_resources(temp_tx);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700720 if (err)
721 goto err_setup;
722 }
Bruce Allan508da422011-12-16 00:45:51 +0000723 if (set_rx) {
724 memcpy(temp_rx, adapter->rx_ring, size);
725 temp_rx->count = new_rx_count;
726 err = e1000e_setup_rx_resources(temp_rx);
727 if (err)
728 goto err_setup_rx;
729 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700730
Bruce Allan508da422011-12-16 00:45:51 +0000731 /* ...then free the old resources and copy back any new ring data */
732 if (set_tx) {
733 e1000e_free_tx_resources(adapter->tx_ring);
734 memcpy(adapter->tx_ring, temp_tx, size);
735 adapter->tx_ring_count = new_tx_count;
736 }
737 if (set_rx) {
738 e1000e_free_rx_resources(adapter->rx_ring);
739 memcpy(adapter->rx_ring, temp_rx, size);
740 adapter->rx_ring_count = new_rx_count;
741 }
742
Auke Kokbc7f75f2007-09-17 12:30:59 -0700743err_setup_rx:
Bruce Allan508da422011-12-16 00:45:51 +0000744 if (err && set_tx)
745 e1000e_free_tx_resources(temp_tx);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700746err_setup:
Bruce Allan508da422011-12-16 00:45:51 +0000747 e1000e_up(adapter);
748free_temp:
749 vfree(temp_tx);
750 vfree(temp_rx);
751clear_reset:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700752 clear_bit(__E1000_RESETTING, &adapter->state);
753 return err;
754}
755
Bruce Allancef8c792008-04-02 13:48:23 -0700756static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
757 int reg, int offset, u32 mask, u32 write)
Joe Perches2a887192007-11-13 20:53:51 -0800758{
Bruce Allancef8c792008-04-02 13:48:23 -0700759 u32 pat, val;
Bruce Allan64806412010-12-11 05:53:42 +0000760 static const u32 test[] = {
761 0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
Bruce Allancef8c792008-04-02 13:48:23 -0700762 for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
Joe Perches2a887192007-11-13 20:53:51 -0800763 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
Bruce Allancef8c792008-04-02 13:48:23 -0700764 (test[pat] & write));
765 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
766 if (val != (test[pat] & write & mask)) {
Bruce Allan6ad65142012-04-12 05:47:09 +0000767 e_err("pattern test reg %04X failed: got 0x%08X expected 0x%08X\n",
768 reg + offset, val, (test[pat] & write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800769 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700770 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800771 }
772 }
Bruce Allancef8c792008-04-02 13:48:23 -0700773 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700774}
775
Joe Perches2a887192007-11-13 20:53:51 -0800776static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
777 int reg, u32 mask, u32 write)
778{
Bruce Allancef8c792008-04-02 13:48:23 -0700779 u32 val;
Joe Perches2a887192007-11-13 20:53:51 -0800780 __ew32(&adapter->hw, reg, write & mask);
Bruce Allancef8c792008-04-02 13:48:23 -0700781 val = __er32(&adapter->hw, reg);
782 if ((write & mask) != (val & mask)) {
Bruce Allan6ad65142012-04-12 05:47:09 +0000783 e_err("set/check reg %04X test failed: got 0x%08X expected 0x%08X\n",
784 reg, (val & mask), (write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800785 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700786 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800787 }
Bruce Allancef8c792008-04-02 13:48:23 -0700788 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700789}
Bruce Allancef8c792008-04-02 13:48:23 -0700790#define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write) \
791 do { \
792 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
793 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800794 } while (0)
Bruce Allancef8c792008-04-02 13:48:23 -0700795#define REG_PATTERN_TEST(reg, mask, write) \
796 REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
Joe Perches2a887192007-11-13 20:53:51 -0800797
Bruce Allancef8c792008-04-02 13:48:23 -0700798#define REG_SET_AND_CHECK(reg, mask, write) \
799 do { \
800 if (reg_set_and_check(adapter, data, reg, mask, write)) \
801 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800802 } while (0)
803
Auke Kokbc7f75f2007-09-17 12:30:59 -0700804static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
805{
806 struct e1000_hw *hw = &adapter->hw;
807 struct e1000_mac_info *mac = &adapter->hw.mac;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700808 u32 value;
809 u32 before;
810 u32 after;
811 u32 i;
812 u32 toggle;
Bruce Allana4f58f52009-06-02 11:29:18 +0000813 u32 mask;
Bruce Allan2fbe4522012-04-19 03:21:47 +0000814 u32 wlock_mac = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700815
Bruce Allanad680762008-03-28 09:15:03 -0700816 /*
817 * The status register is Read Only, so a write should fail.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700818 * Some bits that get toggled are ignored.
819 */
820 switch (mac->type) {
821 /* there are several bits on newer hardware that are r/w */
822 case e1000_82571:
823 case e1000_82572:
824 case e1000_80003es2lan:
825 toggle = 0x7FFFF3FF;
826 break;
Bruce Allana4f58f52009-06-02 11:29:18 +0000827 default:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700828 toggle = 0x7FFFF033;
829 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700830 }
831
832 before = er32(STATUS);
833 value = (er32(STATUS) & toggle);
834 ew32(STATUS, toggle);
835 after = er32(STATUS) & toggle;
836 if (value != after) {
Bruce Allan6ad65142012-04-12 05:47:09 +0000837 e_err("failed STATUS register test got: 0x%08X expected: 0x%08X\n",
838 after, value);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700839 *data = 1;
840 return 1;
841 }
842 /* restore previous status */
843 ew32(STATUS, before);
844
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700845 if (!(adapter->flags & FLAG_IS_ICH)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700846 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
847 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
848 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
849 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
850 }
851
852 REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
Bruce Allan1e360522012-03-20 03:48:13 +0000853 REG_PATTERN_TEST(E1000_RDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
854 REG_PATTERN_TEST(E1000_RDLEN(0), 0x000FFF80, 0x000FFFFF);
855 REG_PATTERN_TEST(E1000_RDH(0), 0x0000FFFF, 0x0000FFFF);
856 REG_PATTERN_TEST(E1000_RDT(0), 0x0000FFFF, 0x0000FFFF);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700857 REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
858 REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
859 REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
Bruce Allan1e360522012-03-20 03:48:13 +0000860 REG_PATTERN_TEST(E1000_TDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
861 REG_PATTERN_TEST(E1000_TDLEN(0), 0x000FFF80, 0x000FFFFF);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700862
863 REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
864
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700865 before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700866 REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
867 REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
868
Auke Kok86582512007-10-04 15:00:08 -0700869 REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
Bruce Allan1e360522012-03-20 03:48:13 +0000870 REG_PATTERN_TEST(E1000_RDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700871 if (!(adapter->flags & FLAG_IS_ICH))
Auke Kok86582512007-10-04 15:00:08 -0700872 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
Bruce Allan1e360522012-03-20 03:48:13 +0000873 REG_PATTERN_TEST(E1000_TDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
Auke Kok86582512007-10-04 15:00:08 -0700874 REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
Bruce Allana4f58f52009-06-02 11:29:18 +0000875 mask = 0x8003FFFF;
876 switch (mac->type) {
877 case e1000_ich10lan:
878 case e1000_pchlan:
Bruce Alland3738bb2010-06-16 13:27:28 +0000879 case e1000_pch2lan:
Bruce Allan2fbe4522012-04-19 03:21:47 +0000880 case e1000_pch_lpt:
Bruce Allana4f58f52009-06-02 11:29:18 +0000881 mask |= (1 << 18);
882 break;
883 default:
884 break;
885 }
Bruce Allan2fbe4522012-04-19 03:21:47 +0000886
887 if (mac->type == e1000_pch_lpt)
888 wlock_mac = (er32(FWSM) & E1000_FWSM_WLOCK_MAC_MASK) >>
889 E1000_FWSM_WLOCK_MAC_SHIFT;
890
891 for (i = 0; i < mac->rar_entry_count; i++) {
892 /* Cannot test write-protected SHRAL[n] registers */
893 if ((wlock_mac == 1) || (wlock_mac && (i > wlock_mac)))
894 continue;
895
Auke Kok86582512007-10-04 15:00:08 -0700896 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1),
Bruce Allan2fbe4522012-04-19 03:21:47 +0000897 mask, 0xFFFFFFFF);
898 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700899
900 for (i = 0; i < mac->mta_reg_count; i++)
901 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
902
903 *data = 0;
Bruce Allan2fbe4522012-04-19 03:21:47 +0000904
Auke Kokbc7f75f2007-09-17 12:30:59 -0700905 return 0;
906}
907
908static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
909{
910 u16 temp;
911 u16 checksum = 0;
912 u16 i;
913
914 *data = 0;
915 /* Read and add up the contents of the EEPROM */
916 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
917 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
918 *data = 1;
Bruce Allane2434552008-11-21 17:02:41 -0800919 return *data;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700920 }
921 checksum += temp;
922 }
923
924 /* If Checksum is not Correct return error else test passed */
925 if ((checksum != (u16) NVM_SUM) && !(*data))
926 *data = 2;
927
928 return *data;
929}
930
931static irqreturn_t e1000_test_intr(int irq, void *data)
932{
933 struct net_device *netdev = (struct net_device *) data;
934 struct e1000_adapter *adapter = netdev_priv(netdev);
935 struct e1000_hw *hw = &adapter->hw;
936
937 adapter->test_icr |= er32(ICR);
938
939 return IRQ_HANDLED;
940}
941
942static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
943{
944 struct net_device *netdev = adapter->netdev;
945 struct e1000_hw *hw = &adapter->hw;
946 u32 mask;
947 u32 shared_int = 1;
948 u32 irq = adapter->pdev->irq;
949 int i;
Bruce Allan4662e822008-08-26 18:37:06 -0700950 int ret_val = 0;
951 int int_mode = E1000E_INT_MODE_LEGACY;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700952
953 *data = 0;
954
Bruce Allan4662e822008-08-26 18:37:06 -0700955 /* NOTE: we don't test MSI/MSI-X interrupts here, yet */
956 if (adapter->int_mode == E1000E_INT_MODE_MSIX) {
957 int_mode = adapter->int_mode;
958 e1000e_reset_interrupt_capability(adapter);
959 adapter->int_mode = E1000E_INT_MODE_LEGACY;
960 e1000e_set_interrupt_capability(adapter);
961 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700962 /* Hook up test interrupt handler just for this test */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800963 if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700964 netdev)) {
965 shared_int = 0;
Joe Perchesa0607fd2009-11-18 23:29:17 -0800966 } else if (request_irq(irq, e1000_test_intr, IRQF_SHARED,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700967 netdev->name, netdev)) {
968 *data = 1;
Bruce Allan4662e822008-08-26 18:37:06 -0700969 ret_val = -1;
970 goto out;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700971 }
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700972 e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700973
974 /* Disable all the interrupts */
975 ew32(IMC, 0xFFFFFFFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +0000976 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +0000977 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700978
979 /* Test each interrupt */
980 for (i = 0; i < 10; i++) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700981 /* Interrupt to test */
982 mask = 1 << i;
983
Bruce Allanf4187b52008-08-26 18:36:50 -0700984 if (adapter->flags & FLAG_IS_ICH) {
985 switch (mask) {
986 case E1000_ICR_RXSEQ:
987 continue;
988 case 0x00000100:
989 if (adapter->hw.mac.type == e1000_ich8lan ||
990 adapter->hw.mac.type == e1000_ich9lan)
991 continue;
992 break;
993 default:
994 break;
995 }
996 }
997
Auke Kokbc7f75f2007-09-17 12:30:59 -0700998 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -0700999 /*
1000 * Disable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -07001001 * the cause register and then force the same
1002 * interrupt and see if one gets posted. If
1003 * an interrupt was posted to the bus, the
1004 * test failed.
1005 */
1006 adapter->test_icr = 0;
1007 ew32(IMC, mask);
1008 ew32(ICS, mask);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001009 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001010 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001011
1012 if (adapter->test_icr & mask) {
1013 *data = 3;
1014 break;
1015 }
1016 }
1017
Bruce Allanad680762008-03-28 09:15:03 -07001018 /*
1019 * Enable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -07001020 * the cause register and then force the same
1021 * interrupt and see if one gets posted. If
1022 * an interrupt was not posted to the bus, the
1023 * test failed.
1024 */
1025 adapter->test_icr = 0;
1026 ew32(IMS, mask);
1027 ew32(ICS, mask);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001028 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001029 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001030
1031 if (!(adapter->test_icr & mask)) {
1032 *data = 4;
1033 break;
1034 }
1035
1036 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -07001037 /*
1038 * Disable the other interrupts to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -07001039 * 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.
1043 */
1044 adapter->test_icr = 0;
1045 ew32(IMC, ~mask & 0x00007FFF);
1046 ew32(ICS, ~mask & 0x00007FFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001047 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001048 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001049
1050 if (adapter->test_icr) {
1051 *data = 5;
1052 break;
1053 }
1054 }
1055 }
1056
1057 /* Disable all the interrupts */
1058 ew32(IMC, 0xFFFFFFFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001059 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001060 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001061
1062 /* Unhook test interrupt handler */
1063 free_irq(irq, netdev);
1064
Bruce Allan4662e822008-08-26 18:37:06 -07001065out:
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);
1070 }
1071
1072 return ret_val;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001073}
1074
1075static void e1000_free_desc_rings(struct e1000_adapter *adapter)
1076{
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;
1081
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)
Nick Nunley0be3f552010-04-27 13:09:05 +00001085 dma_unmap_single(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001086 tx_ring->buffer_info[i].dma,
1087 tx_ring->buffer_info[i].length,
Nick Nunley0be3f552010-04-27 13:09:05 +00001088 DMA_TO_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001089 if (tx_ring->buffer_info[i].skb)
1090 dev_kfree_skb(tx_ring->buffer_info[i].skb);
1091 }
1092 }
1093
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)
Nick Nunley0be3f552010-04-27 13:09:05 +00001097 dma_unmap_single(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001098 rx_ring->buffer_info[i].dma,
Nick Nunley0be3f552010-04-27 13:09:05 +00001099 2048, DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001100 if (rx_ring->buffer_info[i].skb)
1101 dev_kfree_skb(rx_ring->buffer_info[i].skb);
1102 }
1103 }
1104
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;
1109 }
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;
1114 }
1115
1116 kfree(tx_ring->buffer_info);
1117 tx_ring->buffer_info = NULL;
1118 kfree(rx_ring->buffer_info);
1119 rx_ring->buffer_info = NULL;
1120}
1121
1122static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1123{
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;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001129 int i;
1130 int ret_val;
1131
1132 /* Setup Tx descriptor ring and Tx buffers */
1133
1134 if (!tx_ring->count)
1135 tx_ring->count = E1000_DEFAULT_TXD;
1136
Bruce Allancef8c792008-04-02 13:48:23 -07001137 tx_ring->buffer_info = kcalloc(tx_ring->count,
1138 sizeof(struct e1000_buffer),
1139 GFP_KERNEL);
Bruce Allan668018d2012-01-31 07:02:56 +00001140 if (!tx_ring->buffer_info) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001141 ret_val = 1;
1142 goto err_nomem;
1143 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001144
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;
1152 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001153 tx_ring->next_to_use = 0;
1154 tx_ring->next_to_clean = 0;
1155
Bruce Allan1e360522012-03-20 03:48:13 +00001156 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);
Bruce Allancef8c792008-04-02 13:48:23 -07001161 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);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001164
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;
1169
1170 skb = alloc_skb(skb_size, GFP_KERNEL);
1171 if (!skb) {
1172 ret_val = 3;
1173 goto err_nomem;
1174 }
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 =
Nick Nunley0be3f552010-04-27 13:09:05 +00001179 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)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001183 ret_val = 4;
1184 goto err_nomem;
1185 }
Bruce Allancef8c792008-04-02 13:48:23 -07001186 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001187 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 |
Bruce Allancef8c792008-04-02 13:48:23 -07001190 E1000_TXD_CMD_RS);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001191 tx_desc->upper.data = 0;
1192 }
1193
1194 /* Setup Rx descriptor ring and Rx buffers */
1195
1196 if (!rx_ring->count)
1197 rx_ring->count = E1000_DEFAULT_RXD;
1198
Bruce Allancef8c792008-04-02 13:48:23 -07001199 rx_ring->buffer_info = kcalloc(rx_ring->count,
1200 sizeof(struct e1000_buffer),
1201 GFP_KERNEL);
Bruce Allan668018d2012-01-31 07:02:56 +00001202 if (!rx_ring->buffer_info) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001203 ret_val = 5;
1204 goto err_nomem;
1205 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001206
Bruce Allan5f450212011-07-22 06:21:46 +00001207 rx_ring->size = rx_ring->count * sizeof(union e1000_rx_desc_extended);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001208 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;
1213 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001214 rx_ring->next_to_use = 0;
1215 rx_ring->next_to_clean = 0;
1216
1217 rctl = er32(RCTL);
Bruce Allan7f99ae62011-07-22 06:21:35 +00001218 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
1219 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Bruce Allan1e360522012-03-20 03:48:13 +00001220 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);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001225 rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
Bruce Allancef8c792008-04-02 13:48:23 -07001226 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1227 E1000_RCTL_SBP | E1000_RCTL_SECRC |
Auke Kokbc7f75f2007-09-17 12:30:59 -07001228 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1229 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1230 ew32(RCTL, rctl);
1231
1232 for (i = 0; i < rx_ring->count; i++) {
Bruce Allan5f450212011-07-22 06:21:46 +00001233 union e1000_rx_desc_extended *rx_desc;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001234 struct sk_buff *skb;
1235
1236 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1237 if (!skb) {
1238 ret_val = 7;
1239 goto err_nomem;
1240 }
1241 skb_reserve(skb, NET_IP_ALIGN);
1242 rx_ring->buffer_info[i].skb = skb;
1243 rx_ring->buffer_info[i].dma =
Nick Nunley0be3f552010-04-27 13:09:05 +00001244 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)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001248 ret_val = 8;
1249 goto err_nomem;
1250 }
Bruce Allan5f450212011-07-22 06:21:46 +00001251 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);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001254 memset(skb->data, 0x00, skb->len);
1255 }
1256
1257 return 0;
1258
1259err_nomem:
1260 e1000_free_desc_rings(adapter);
1261 return ret_val;
1262}
1263
1264static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1265{
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);
1271}
1272
1273static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1274{
1275 struct e1000_hw *hw = &adapter->hw;
1276 u32 ctrl_reg = 0;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001277 u16 phy_reg = 0;
Bruce Allancbd006c2010-11-24 06:01:30 +00001278 s32 ret_val = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001279
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001280 hw->mac.autoneg = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001281
Bruce Allan3af50482010-06-16 13:25:55 +00001282 if (hw->phy.type == e1000_phy_ife) {
1283 /* force 100, set loopback */
1284 e1e_wphy(hw, PHY_CONTROL, 0x6100);
Bruce Allanbb436b22009-11-20 23:24:11 +00001285
Bruce Allan3af50482010-06-16 13:25:55 +00001286 /* 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 */
1293
1294 ew32(CTRL, ctrl_reg);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001295 e1e_flush();
Bruce Allan3af50482010-06-16 13:25:55 +00001296 udelay(500);
1297
1298 return 0;
1299 }
1300
1301 /* Specific PHY configuration for loopback */
1302 switch (hw->phy.type) {
1303 case e1000_phy_m88:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001304 /* Auto-MDI/MDIX Off */
1305 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1306 /* reset to update Auto-MDI/MDIX */
1307 e1e_wphy(hw, PHY_CONTROL, 0x9140);
1308 /* autoneg off */
1309 e1e_wphy(hw, PHY_CONTROL, 0x8140);
Bruce Allan3af50482010-06-16 13:25:55 +00001310 break;
1311 case e1000_phy_gg82563:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001312 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
Bruce Allancef8c792008-04-02 13:48:23 -07001313 break;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001314 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 e1000e_commit_phy(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);
Bruce Allan3af50482010-06-16 13:25:55 +00001335 break;
1336 case e1000_phy_82577:
1337 case e1000_phy_82578:
1338 /* Workaround: K1 must be disabled for stable 1Gbps operation */
Bruce Allancbd006c2010-11-24 06:01:30 +00001339 ret_val = hw->phy.ops.acquire(hw);
1340 if (ret_val) {
1341 e_err("Cannot setup 1Gbps loopback.\n");
1342 return ret_val;
1343 }
Bruce Allan3af50482010-06-16 13:25:55 +00001344 e1000_configure_k1_ich8lan(hw, false);
Bruce Allancbd006c2010-11-24 06:01:30 +00001345 hw->phy.ops.release(hw);
Bruce Allan3af50482010-06-16 13:25:55 +00001346 break;
Bruce Alland3738bb2010-06-16 13:27:28 +00001347 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#define I82577_PHY_LBK_CTRL 19
1356 e1e_wphy(hw, I82577_PHY_LBK_CTRL, 0x8001);
1357 break;
Bruce Allancef8c792008-04-02 13:48:23 -07001358 default:
Bruce Allan3af50482010-06-16 13:25:55 +00001359 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001360 }
1361
Bruce Allan3af50482010-06-16 13:25:55 +00001362 /* force 1000, set loopback */
1363 e1e_wphy(hw, PHY_CONTROL, 0x4140);
1364 mdelay(250);
1365
1366 /* Now set up the MAC to the same speed/duplex as the PHY. */
1367 ctrl_reg = er32(CTRL);
1368 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1369 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1370 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1371 E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1372 E1000_CTRL_FD); /* Force Duplex to FULL */
1373
1374 if (adapter->flags & FLAG_IS_ICH)
1375 ctrl_reg |= E1000_CTRL_SLU; /* Set Link Up */
1376
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001377 if (hw->phy.media_type == e1000_media_type_copper &&
1378 hw->phy.type == e1000_phy_m88) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001379 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1380 } else {
Bruce Allanad680762008-03-28 09:15:03 -07001381 /*
1382 * Set the ILOS bit on the fiber Nic if half duplex link is
1383 * detected.
1384 */
Bruce Allan90da0662011-01-06 07:02:53 +00001385 if ((er32(STATUS) & E1000_STATUS_FD) == 0)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001386 ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1387 }
1388
1389 ew32(CTRL, ctrl_reg);
1390
Bruce Allanad680762008-03-28 09:15:03 -07001391 /*
1392 * Disable the receiver on the PHY so when a cable is plugged in, the
Auke Kokbc7f75f2007-09-17 12:30:59 -07001393 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1394 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001395 if (hw->phy.type == e1000_phy_m88)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001396 e1000_phy_disable_receiver(adapter);
1397
1398 udelay(500);
1399
1400 return 0;
1401}
1402
1403static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1404{
1405 struct e1000_hw *hw = &adapter->hw;
1406 u32 ctrl = er32(CTRL);
1407 int link = 0;
1408
1409 /* special requirements for 82571/82572 fiber adapters */
1410
Bruce Allanad680762008-03-28 09:15:03 -07001411 /*
1412 * jump through hoops to make sure link is up because serdes
1413 * link is hardwired up
1414 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001415 ctrl |= E1000_CTRL_SLU;
1416 ew32(CTRL, ctrl);
1417
1418 /* disable autoneg */
1419 ctrl = er32(TXCW);
1420 ctrl &= ~(1 << 31);
1421 ew32(TXCW, ctrl);
1422
1423 link = (er32(STATUS) & E1000_STATUS_LU);
1424
1425 if (!link) {
1426 /* set invert loss of signal */
1427 ctrl = er32(CTRL);
1428 ctrl |= E1000_CTRL_ILOS;
1429 ew32(CTRL, ctrl);
1430 }
1431
Bruce Allanad680762008-03-28 09:15:03 -07001432 /*
1433 * special write to serdes control register to enable SerDes analog
1434 * loopback
1435 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001436#define E1000_SERDES_LB_ON 0x410
1437 ew32(SCTL, E1000_SERDES_LB_ON);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001438 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001439 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001440
1441 return 0;
1442}
1443
1444/* only call this for fiber/serdes connections to es2lan */
1445static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1446{
1447 struct e1000_hw *hw = &adapter->hw;
1448 u32 ctrlext = er32(CTRL_EXT);
1449 u32 ctrl = er32(CTRL);
1450
Bruce Allanad680762008-03-28 09:15:03 -07001451 /*
1452 * save CTRL_EXT to restore later, reuse an empty variable (unused
1453 * on mac_type 80003es2lan)
1454 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001455 adapter->tx_fifo_head = ctrlext;
1456
1457 /* clear the serdes mode bits, putting the device into mac loopback */
1458 ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1459 ew32(CTRL_EXT, ctrlext);
1460
1461 /* force speed to 1000/FD, link up */
1462 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1463 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1464 E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1465 ew32(CTRL, ctrl);
1466
1467 /* set mac loopback */
1468 ctrl = er32(RCTL);
1469 ctrl |= E1000_RCTL_LBM_MAC;
1470 ew32(RCTL, ctrl);
1471
1472 /* set testing mode parameters (no need to reset later) */
1473#define KMRNCTRLSTA_OPMODE (0x1F << 16)
1474#define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1475 ew32(KMRNCTRLSTA,
Bruce Allancef8c792008-04-02 13:48:23 -07001476 (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001477
1478 return 0;
1479}
1480
1481static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1482{
1483 struct e1000_hw *hw = &adapter->hw;
1484 u32 rctl;
1485
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001486 if (hw->phy.media_type == e1000_media_type_fiber ||
1487 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001488 switch (hw->mac.type) {
1489 case e1000_80003es2lan:
1490 return e1000_set_es2lan_mac_loopback(adapter);
1491 break;
1492 case e1000_82571:
1493 case e1000_82572:
1494 return e1000_set_82571_fiber_loopback(adapter);
1495 break;
1496 default:
1497 rctl = er32(RCTL);
1498 rctl |= E1000_RCTL_LBM_TCVR;
1499 ew32(RCTL, rctl);
1500 return 0;
1501 }
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001502 } else if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001503 return e1000_integrated_phy_loopback(adapter);
1504 }
1505
1506 return 7;
1507}
1508
1509static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1510{
1511 struct e1000_hw *hw = &adapter->hw;
1512 u32 rctl;
1513 u16 phy_reg;
1514
1515 rctl = er32(RCTL);
1516 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1517 ew32(RCTL, rctl);
1518
1519 switch (hw->mac.type) {
1520 case e1000_80003es2lan:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001521 if (hw->phy.media_type == e1000_media_type_fiber ||
1522 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001523 /* restore CTRL_EXT, stealing space from tx_fifo_head */
Bruce Allanad680762008-03-28 09:15:03 -07001524 ew32(CTRL_EXT, adapter->tx_fifo_head);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001525 adapter->tx_fifo_head = 0;
1526 }
1527 /* fall through */
1528 case e1000_82571:
1529 case e1000_82572:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001530 if (hw->phy.media_type == e1000_media_type_fiber ||
1531 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001532#define E1000_SERDES_LB_OFF 0x400
1533 ew32(SCTL, E1000_SERDES_LB_OFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001534 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001535 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001536 break;
1537 }
1538 /* Fall Through */
1539 default:
1540 hw->mac.autoneg = 1;
1541 if (hw->phy.type == e1000_phy_gg82563)
1542 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1543 e1e_rphy(hw, PHY_CONTROL, &phy_reg);
1544 if (phy_reg & MII_CR_LOOPBACK) {
1545 phy_reg &= ~MII_CR_LOOPBACK;
1546 e1e_wphy(hw, PHY_CONTROL, phy_reg);
1547 e1000e_commit_phy(hw);
1548 }
1549 break;
1550 }
1551}
1552
1553static void e1000_create_lbtest_frame(struct sk_buff *skb,
1554 unsigned int frame_size)
1555{
1556 memset(skb->data, 0xFF, frame_size);
1557 frame_size &= ~1;
1558 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1559 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
1560 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
1561}
1562
1563static int e1000_check_lbtest_frame(struct sk_buff *skb,
1564 unsigned int frame_size)
1565{
1566 frame_size &= ~1;
1567 if (*(skb->data + 3) == 0xFF)
1568 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1569 (*(skb->data + frame_size / 2 + 12) == 0xAF))
1570 return 0;
1571 return 13;
1572}
1573
1574static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1575{
1576 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1577 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1578 struct pci_dev *pdev = adapter->pdev;
1579 struct e1000_hw *hw = &adapter->hw;
1580 int i, j, k, l;
1581 int lc;
1582 int good_cnt;
1583 int ret_val = 0;
1584 unsigned long time;
1585
Bruce Allan1e360522012-03-20 03:48:13 +00001586 ew32(RDT(0), rx_ring->count - 1);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001587
Bruce Allanad680762008-03-28 09:15:03 -07001588 /*
1589 * Calculate the loop count based on the largest descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001590 * The idea is to wrap the largest ring a number of times using 64
1591 * send/receive pairs during each loop
1592 */
1593
1594 if (rx_ring->count <= tx_ring->count)
1595 lc = ((tx_ring->count / 64) * 2) + 1;
1596 else
1597 lc = ((rx_ring->count / 64) * 2) + 1;
1598
1599 k = 0;
1600 l = 0;
1601 for (j = 0; j <= lc; j++) { /* loop count loop */
1602 for (i = 0; i < 64; i++) { /* send the packets */
Bruce Allancef8c792008-04-02 13:48:23 -07001603 e1000_create_lbtest_frame(tx_ring->buffer_info[k].skb,
1604 1024);
Nick Nunley0be3f552010-04-27 13:09:05 +00001605 dma_sync_single_for_device(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001606 tx_ring->buffer_info[k].dma,
1607 tx_ring->buffer_info[k].length,
Nick Nunley0be3f552010-04-27 13:09:05 +00001608 DMA_TO_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001609 k++;
1610 if (k == tx_ring->count)
1611 k = 0;
1612 }
Bruce Allan1e360522012-03-20 03:48:13 +00001613 ew32(TDT(0), k);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001614 e1e_flush();
Auke Kokbc7f75f2007-09-17 12:30:59 -07001615 msleep(200);
1616 time = jiffies; /* set the start time for the receive */
1617 good_cnt = 0;
1618 do { /* receive the sent packets */
Nick Nunley0be3f552010-04-27 13:09:05 +00001619 dma_sync_single_for_cpu(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001620 rx_ring->buffer_info[l].dma, 2048,
Nick Nunley0be3f552010-04-27 13:09:05 +00001621 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001622
1623 ret_val = e1000_check_lbtest_frame(
1624 rx_ring->buffer_info[l].skb, 1024);
1625 if (!ret_val)
1626 good_cnt++;
1627 l++;
1628 if (l == rx_ring->count)
1629 l = 0;
Bruce Allanad680762008-03-28 09:15:03 -07001630 /*
1631 * time + 20 msecs (200 msecs on 2.4) is more than
Auke Kokbc7f75f2007-09-17 12:30:59 -07001632 * enough time to complete the receives, if it's
1633 * exceeded, break and error off
1634 */
1635 } while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1636 if (good_cnt != 64) {
1637 ret_val = 13; /* ret_val is the same as mis-compare */
1638 break;
1639 }
Bruce Allancef8c792008-04-02 13:48:23 -07001640 if (jiffies >= (time + 20)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001641 ret_val = 14; /* error code for time out error */
1642 break;
1643 }
1644 } /* end loop count loop */
1645 return ret_val;
1646}
1647
1648static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1649{
Bruce Allan44abd5c2012-02-22 09:02:37 +00001650 struct e1000_hw *hw = &adapter->hw;
1651
Bruce Allanad680762008-03-28 09:15:03 -07001652 /*
1653 * PHY loopback cannot be performed if SoL/IDER
1654 * sessions are active
1655 */
Bruce Allan470a5422012-05-26 06:08:48 +00001656 if (hw->phy.ops.check_reset_block &&
1657 hw->phy.ops.check_reset_block(hw)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001658 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001659 *data = 0;
1660 goto out;
1661 }
1662
1663 *data = e1000_setup_desc_rings(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001664 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001665 goto out;
1666
1667 *data = e1000_setup_loopback_test(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001668 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001669 goto err_loopback;
1670
1671 *data = e1000_run_loopback_test(adapter);
1672 e1000_loopback_cleanup(adapter);
1673
1674err_loopback:
1675 e1000_free_desc_rings(adapter);
1676out:
1677 return *data;
1678}
1679
1680static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1681{
1682 struct e1000_hw *hw = &adapter->hw;
1683
1684 *data = 0;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001685 if (hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001686 int i = 0;
Alex Chiang612e2442009-02-05 23:55:45 -08001687 hw->mac.serdes_has_link = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001688
Bruce Allanad680762008-03-28 09:15:03 -07001689 /*
1690 * On some blade server designs, link establishment
1691 * could take as long as 2-3 minutes
1692 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001693 do {
1694 hw->mac.ops.check_for_link(hw);
1695 if (hw->mac.serdes_has_link)
1696 return *data;
1697 msleep(20);
1698 } while (i++ < 3750);
1699
1700 *data = 1;
1701 } else {
1702 hw->mac.ops.check_for_link(hw);
1703 if (hw->mac.autoneg)
Bruce Allan5661aeb2011-02-25 06:36:25 +00001704 /*
1705 * On some Phy/switch combinations, link establishment
1706 * can take a few seconds more than expected.
1707 */
1708 msleep(5000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001709
Bruce Allan5661aeb2011-02-25 06:36:25 +00001710 if (!(er32(STATUS) & E1000_STATUS_LU))
Auke Kokbc7f75f2007-09-17 12:30:59 -07001711 *data = 1;
1712 }
1713 return *data;
1714}
1715
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001716static int e1000e_get_sset_count(struct net_device *netdev, int sset)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001717{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001718 switch (sset) {
1719 case ETH_SS_TEST:
1720 return E1000_TEST_LEN;
1721 case ETH_SS_STATS:
1722 return E1000_STATS_LEN;
1723 default:
1724 return -EOPNOTSUPP;
1725 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001726}
1727
1728static void e1000_diag_test(struct net_device *netdev,
1729 struct ethtool_test *eth_test, u64 *data)
1730{
1731 struct e1000_adapter *adapter = netdev_priv(netdev);
1732 u16 autoneg_advertised;
1733 u8 forced_speed_duplex;
1734 u8 autoneg;
1735 bool if_running = netif_running(netdev);
1736
1737 set_bit(__E1000_TESTING, &adapter->state);
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001738
1739 if (!if_running) {
1740 /* Get control of and reset hardware */
1741 if (adapter->flags & FLAG_HAS_AMT)
1742 e1000e_get_hw_control(adapter);
1743
1744 e1000e_power_up_phy(adapter);
1745
1746 adapter->hw.phy.autoneg_wait_to_complete = 1;
1747 e1000e_reset(adapter);
1748 adapter->hw.phy.autoneg_wait_to_complete = 0;
1749 }
1750
Auke Kokbc7f75f2007-09-17 12:30:59 -07001751 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1752 /* Offline tests */
1753
1754 /* save speed, duplex, autoneg settings */
1755 autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1756 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1757 autoneg = adapter->hw.mac.autoneg;
1758
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001759 e_info("offline testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001760
Auke Kokbc7f75f2007-09-17 12:30:59 -07001761 if (if_running)
1762 /* indicate we're in test mode */
1763 dev_close(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001764
1765 if (e1000_reg_test(adapter, &data[0]))
1766 eth_test->flags |= ETH_TEST_FL_FAILED;
1767
1768 e1000e_reset(adapter);
1769 if (e1000_eeprom_test(adapter, &data[1]))
1770 eth_test->flags |= ETH_TEST_FL_FAILED;
1771
1772 e1000e_reset(adapter);
1773 if (e1000_intr_test(adapter, &data[2]))
1774 eth_test->flags |= ETH_TEST_FL_FAILED;
1775
1776 e1000e_reset(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001777 if (e1000_loopback_test(adapter, &data[3]))
1778 eth_test->flags |= ETH_TEST_FL_FAILED;
1779
Auke Kokbc7f75f2007-09-17 12:30:59 -07001780 /* force this routine to wait until autoneg complete/timeout */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001781 adapter->hw.phy.autoneg_wait_to_complete = 1;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001782 e1000e_reset(adapter);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001783 adapter->hw.phy.autoneg_wait_to_complete = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001784
Carolyn Wybornyc6ce3852010-10-15 17:35:31 +00001785 if (e1000_link_test(adapter, &data[4]))
1786 eth_test->flags |= ETH_TEST_FL_FAILED;
1787
1788 /* restore speed, duplex, autoneg settings */
1789 adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1790 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1791 adapter->hw.mac.autoneg = autoneg;
1792 e1000e_reset(adapter);
1793
Auke Kokbc7f75f2007-09-17 12:30:59 -07001794 clear_bit(__E1000_TESTING, &adapter->state);
1795 if (if_running)
1796 dev_open(netdev);
1797 } else {
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001798 /* Online tests */
Bruce Allan11b08be2010-05-10 14:59:31 +00001799
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001800 e_info("online testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001801
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001802 /* register, eeprom, intr and loopback tests not run online */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001803 data[0] = 0;
1804 data[1] = 0;
1805 data[2] = 0;
1806 data[3] = 0;
1807
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001808 if (e1000_link_test(adapter, &data[4]))
1809 eth_test->flags |= ETH_TEST_FL_FAILED;
Bruce Allan11b08be2010-05-10 14:59:31 +00001810
Auke Kokbc7f75f2007-09-17 12:30:59 -07001811 clear_bit(__E1000_TESTING, &adapter->state);
1812 }
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001813
1814 if (!if_running) {
1815 e1000e_reset(adapter);
1816
1817 if (adapter->flags & FLAG_HAS_AMT)
1818 e1000e_release_hw_control(adapter);
1819 }
1820
Auke Kokbc7f75f2007-09-17 12:30:59 -07001821 msleep_interruptible(4 * 1000);
1822}
1823
1824static void e1000_get_wol(struct net_device *netdev,
1825 struct ethtool_wolinfo *wol)
1826{
1827 struct e1000_adapter *adapter = netdev_priv(netdev);
1828
1829 wol->supported = 0;
1830 wol->wolopts = 0;
1831
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001832 if (!(adapter->flags & FLAG_HAS_WOL) ||
1833 !device_can_wakeup(&adapter->pdev->dev))
Auke Kokbc7f75f2007-09-17 12:30:59 -07001834 return;
1835
1836 wol->supported = WAKE_UCAST | WAKE_MCAST |
Bruce Allan4a29e152011-03-04 09:07:01 +00001837 WAKE_BCAST | WAKE_MAGIC | WAKE_PHY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001838
1839 /* apply any specific unsupported masks here */
1840 if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1841 wol->supported &= ~WAKE_UCAST;
1842
1843 if (adapter->wol & E1000_WUFC_EX)
Bruce Allan6ad65142012-04-12 05:47:09 +00001844 e_err("Interface does not support directed (unicast) frame wake-up packets\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001845 }
1846
1847 if (adapter->wol & E1000_WUFC_EX)
1848 wol->wolopts |= WAKE_UCAST;
1849 if (adapter->wol & E1000_WUFC_MC)
1850 wol->wolopts |= WAKE_MCAST;
1851 if (adapter->wol & E1000_WUFC_BC)
1852 wol->wolopts |= WAKE_BCAST;
1853 if (adapter->wol & E1000_WUFC_MAG)
1854 wol->wolopts |= WAKE_MAGIC;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001855 if (adapter->wol & E1000_WUFC_LNKC)
1856 wol->wolopts |= WAKE_PHY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001857}
1858
Bruce Allan4a29e152011-03-04 09:07:01 +00001859static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001860{
1861 struct e1000_adapter *adapter = netdev_priv(netdev);
1862
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001863 if (!(adapter->flags & FLAG_HAS_WOL) ||
Bruce Allan1fbfca32009-11-20 23:22:01 +00001864 !device_can_wakeup(&adapter->pdev->dev) ||
1865 (wol->wolopts & ~(WAKE_UCAST | WAKE_MCAST | WAKE_BCAST |
Bruce Allan4a29e152011-03-04 09:07:01 +00001866 WAKE_MAGIC | WAKE_PHY)))
Bruce Allan1fbfca32009-11-20 23:22:01 +00001867 return -EOPNOTSUPP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001868
1869 /* these settings will always override what we currently have */
1870 adapter->wol = 0;
1871
1872 if (wol->wolopts & WAKE_UCAST)
1873 adapter->wol |= E1000_WUFC_EX;
1874 if (wol->wolopts & WAKE_MCAST)
1875 adapter->wol |= E1000_WUFC_MC;
1876 if (wol->wolopts & WAKE_BCAST)
1877 adapter->wol |= E1000_WUFC_BC;
1878 if (wol->wolopts & WAKE_MAGIC)
1879 adapter->wol |= E1000_WUFC_MAG;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001880 if (wol->wolopts & WAKE_PHY)
1881 adapter->wol |= E1000_WUFC_LNKC;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001882
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001883 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1884
Auke Kokbc7f75f2007-09-17 12:30:59 -07001885 return 0;
1886}
1887
Bruce Allandbf80dc2011-04-16 00:34:40 +00001888static int e1000_set_phys_id(struct net_device *netdev,
1889 enum ethtool_phys_id_state state)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001890{
1891 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan4662e822008-08-26 18:37:06 -07001892 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001893
Bruce Allandbf80dc2011-04-16 00:34:40 +00001894 switch (state) {
1895 case ETHTOOL_ID_ACTIVE:
1896 if (!hw->mac.ops.blink_led)
1897 return 2; /* cycle on/off twice per second */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001898
Bruce Allandbf80dc2011-04-16 00:34:40 +00001899 hw->mac.ops.blink_led(hw);
1900 break;
1901
1902 case ETHTOOL_ID_INACTIVE:
Bruce Allan4662e822008-08-26 18:37:06 -07001903 if (hw->phy.type == e1000_phy_ife)
1904 e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
Bruce Allandbf80dc2011-04-16 00:34:40 +00001905 hw->mac.ops.led_off(hw);
1906 hw->mac.ops.cleanup_led(hw);
1907 break;
1908
1909 case ETHTOOL_ID_ON:
Bruce Allanf23efdf2012-01-31 06:37:38 +00001910 hw->mac.ops.led_on(hw);
Bruce Allandbf80dc2011-04-16 00:34:40 +00001911 break;
1912
1913 case ETHTOOL_ID_OFF:
Bruce Allanf23efdf2012-01-31 06:37:38 +00001914 hw->mac.ops.led_off(hw);
Bruce Allandbf80dc2011-04-16 00:34:40 +00001915 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001916 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001917 return 0;
1918}
1919
Auke Kokde5b3072008-04-23 11:09:08 -07001920static int e1000_get_coalesce(struct net_device *netdev,
1921 struct ethtool_coalesce *ec)
1922{
1923 struct e1000_adapter *adapter = netdev_priv(netdev);
1924
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001925 if (adapter->itr_setting <= 4)
Auke Kokde5b3072008-04-23 11:09:08 -07001926 ec->rx_coalesce_usecs = adapter->itr_setting;
1927 else
1928 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1929
1930 return 0;
1931}
1932
1933static int e1000_set_coalesce(struct net_device *netdev,
1934 struct ethtool_coalesce *ec)
1935{
1936 struct e1000_adapter *adapter = netdev_priv(netdev);
Auke Kokde5b3072008-04-23 11:09:08 -07001937
1938 if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001939 ((ec->rx_coalesce_usecs > 4) &&
Auke Kokde5b3072008-04-23 11:09:08 -07001940 (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1941 (ec->rx_coalesce_usecs == 2))
1942 return -EINVAL;
1943
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001944 if (ec->rx_coalesce_usecs == 4) {
1945 adapter->itr = adapter->itr_setting = 4;
1946 } else if (ec->rx_coalesce_usecs <= 3) {
Auke Kokde5b3072008-04-23 11:09:08 -07001947 adapter->itr = 20000;
1948 adapter->itr_setting = ec->rx_coalesce_usecs;
1949 } else {
1950 adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1951 adapter->itr_setting = adapter->itr & ~3;
1952 }
1953
1954 if (adapter->itr_setting != 0)
Matthew Vick22a4cca2012-07-12 00:02:42 +00001955 e1000e_write_itr(adapter, adapter->itr);
Auke Kokde5b3072008-04-23 11:09:08 -07001956 else
Matthew Vick22a4cca2012-07-12 00:02:42 +00001957 e1000e_write_itr(adapter, 0);
Auke Kokde5b3072008-04-23 11:09:08 -07001958
1959 return 0;
1960}
1961
Auke Kokbc7f75f2007-09-17 12:30:59 -07001962static int e1000_nway_reset(struct net_device *netdev)
1963{
1964 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan5962bc22011-01-20 06:58:07 +00001965
1966 if (!netif_running(netdev))
1967 return -EAGAIN;
1968
1969 if (!adapter->hw.mac.autoneg)
1970 return -EINVAL;
1971
1972 e1000e_reinit_locked(adapter);
1973
Auke Kokbc7f75f2007-09-17 12:30:59 -07001974 return 0;
1975}
1976
Auke Kokbc7f75f2007-09-17 12:30:59 -07001977static void e1000_get_ethtool_stats(struct net_device *netdev,
1978 struct ethtool_stats *stats,
1979 u64 *data)
1980{
1981 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001982 struct rtnl_link_stats64 net_stats;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001983 int i;
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001984 char *p = NULL;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001985
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001986 e1000e_get_stats64(netdev, &net_stats);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001987 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001988 switch (e1000_gstrings_stats[i].type) {
1989 case NETDEV_STATS:
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001990 p = (char *) &net_stats +
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001991 e1000_gstrings_stats[i].stat_offset;
1992 break;
1993 case E1000_STATS:
1994 p = (char *) adapter +
1995 e1000_gstrings_stats[i].stat_offset;
1996 break;
Bruce Allan61c75812010-12-09 23:04:25 +00001997 default:
1998 data[i] = 0;
1999 continue;
Ajit Khapardee0f36a92009-10-13 01:45:09 +00002000 }
2001
Auke Kokbc7f75f2007-09-17 12:30:59 -07002002 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
2003 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
2004 }
2005}
2006
2007static void e1000_get_strings(struct net_device *netdev, u32 stringset,
2008 u8 *data)
2009{
2010 u8 *p = data;
2011 int i;
2012
2013 switch (stringset) {
2014 case ETH_SS_TEST:
Bruce Allan5c1bda02011-01-19 04:23:39 +00002015 memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
Auke Kokbc7f75f2007-09-17 12:30:59 -07002016 break;
2017 case ETH_SS_STATS:
2018 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
2019 memcpy(p, e1000_gstrings_stats[i].stat_string,
2020 ETH_GSTRING_LEN);
2021 p += ETH_GSTRING_LEN;
2022 }
2023 break;
2024 }
2025}
2026
Bruce Allan70495a52012-01-11 01:26:50 +00002027static int e1000_get_rxnfc(struct net_device *netdev,
2028 struct ethtool_rxnfc *info, u32 *rule_locs)
2029{
2030 info->data = 0;
2031
2032 switch (info->cmd) {
2033 case ETHTOOL_GRXFH: {
2034 struct e1000_adapter *adapter = netdev_priv(netdev);
2035 struct e1000_hw *hw = &adapter->hw;
2036 u32 mrqc = er32(MRQC);
2037
2038 if (!(mrqc & E1000_MRQC_RSS_FIELD_MASK))
2039 return 0;
2040
2041 switch (info->flow_type) {
2042 case TCP_V4_FLOW:
2043 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_TCP)
2044 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2045 /* fall through */
2046 case UDP_V4_FLOW:
2047 case SCTP_V4_FLOW:
2048 case AH_ESP_V4_FLOW:
2049 case IPV4_FLOW:
2050 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4)
2051 info->data |= RXH_IP_SRC | RXH_IP_DST;
2052 break;
2053 case TCP_V6_FLOW:
2054 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP)
2055 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2056 /* fall through */
2057 case UDP_V6_FLOW:
2058 case SCTP_V6_FLOW:
2059 case AH_ESP_V6_FLOW:
2060 case IPV6_FLOW:
2061 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6)
2062 info->data |= RXH_IP_SRC | RXH_IP_DST;
2063 break;
2064 default:
2065 break;
2066 }
2067 return 0;
2068 }
2069 default:
2070 return -EOPNOTSUPP;
2071 }
2072}
2073
Auke Kokbc7f75f2007-09-17 12:30:59 -07002074static const struct ethtool_ops e1000_ethtool_ops = {
2075 .get_settings = e1000_get_settings,
2076 .set_settings = e1000_set_settings,
2077 .get_drvinfo = e1000_get_drvinfo,
2078 .get_regs_len = e1000_get_regs_len,
2079 .get_regs = e1000_get_regs,
2080 .get_wol = e1000_get_wol,
2081 .set_wol = e1000_set_wol,
2082 .get_msglevel = e1000_get_msglevel,
2083 .set_msglevel = e1000_set_msglevel,
2084 .nway_reset = e1000_nway_reset,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +00002085 .get_link = ethtool_op_get_link,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002086 .get_eeprom_len = e1000_get_eeprom_len,
2087 .get_eeprom = e1000_get_eeprom,
2088 .set_eeprom = e1000_set_eeprom,
2089 .get_ringparam = e1000_get_ringparam,
2090 .set_ringparam = e1000_set_ringparam,
2091 .get_pauseparam = e1000_get_pauseparam,
2092 .set_pauseparam = e1000_set_pauseparam,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002093 .self_test = e1000_diag_test,
2094 .get_strings = e1000_get_strings,
Bruce Allandbf80dc2011-04-16 00:34:40 +00002095 .set_phys_id = e1000_set_phys_id,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002096 .get_ethtool_stats = e1000_get_ethtool_stats,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002097 .get_sset_count = e1000e_get_sset_count,
Auke Kokde5b3072008-04-23 11:09:08 -07002098 .get_coalesce = e1000_get_coalesce,
2099 .set_coalesce = e1000_set_coalesce,
Bruce Allan70495a52012-01-11 01:26:50 +00002100 .get_rxnfc = e1000_get_rxnfc,
Richard Cochran7b1115e2012-07-22 07:15:41 +00002101 .get_ts_info = ethtool_op_get_ts_info,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002102};
2103
2104void e1000e_set_ethtool_ops(struct net_device *netdev)
2105{
2106 SET_ETHTOOL_OPS(netdev, &e1000_ethtool_ops);
2107}