blob: 6a0526a59a8a38ce00e8c0aaeb9795db02d11d97 [file] [log] [blame]
Auke Kokbc7f75f2007-09-17 12:30:59 -07001/*******************************************************************************
2
3 Intel PRO/1000 Linux driver
Bruce Allan0d6057e2011-01-04 01:16:44 +00004 Copyright(c) 1999 - 2011 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>
37
38#include "e1000.h"
39
Ajit Khapardee0f36a92009-10-13 01:45:09 +000040enum {NETDEV_STATS, E1000_STATS};
41
Auke Kokbc7f75f2007-09-17 12:30:59 -070042struct e1000_stats {
43 char stat_string[ETH_GSTRING_LEN];
Ajit Khapardee0f36a92009-10-13 01:45:09 +000044 int type;
Auke Kokbc7f75f2007-09-17 12:30:59 -070045 int sizeof_stat;
46 int stat_offset;
47};
48
Bruce Allanf0f1a172010-12-11 05:53:32 +000049#define E1000_STAT(str, m) { \
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000050 .stat_string = str, \
51 .type = E1000_STATS, \
52 .sizeof_stat = sizeof(((struct e1000_adapter *)0)->m), \
53 .stat_offset = offsetof(struct e1000_adapter, m) }
Bruce Allanf0f1a172010-12-11 05:53:32 +000054#define E1000_NETDEV_STAT(str, m) { \
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000055 .stat_string = str, \
56 .type = NETDEV_STATS, \
57 .sizeof_stat = sizeof(((struct rtnl_link_stats64 *)0)->m), \
58 .stat_offset = offsetof(struct rtnl_link_stats64, m) }
Ajit Khapardee0f36a92009-10-13 01:45:09 +000059
Auke Kokbc7f75f2007-09-17 12:30:59 -070060static const struct e1000_stats e1000_gstrings_stats[] = {
Bruce Allanf0f1a172010-12-11 05:53:32 +000061 E1000_STAT("rx_packets", stats.gprc),
62 E1000_STAT("tx_packets", stats.gptc),
63 E1000_STAT("rx_bytes", stats.gorc),
64 E1000_STAT("tx_bytes", stats.gotc),
65 E1000_STAT("rx_broadcast", stats.bprc),
66 E1000_STAT("tx_broadcast", stats.bptc),
67 E1000_STAT("rx_multicast", stats.mprc),
68 E1000_STAT("tx_multicast", stats.mptc),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000069 E1000_NETDEV_STAT("rx_errors", rx_errors),
70 E1000_NETDEV_STAT("tx_errors", tx_errors),
71 E1000_NETDEV_STAT("tx_dropped", tx_dropped),
Bruce Allanf0f1a172010-12-11 05:53:32 +000072 E1000_STAT("multicast", stats.mprc),
73 E1000_STAT("collisions", stats.colc),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000074 E1000_NETDEV_STAT("rx_length_errors", rx_length_errors),
75 E1000_NETDEV_STAT("rx_over_errors", rx_over_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000076 E1000_STAT("rx_crc_errors", stats.crcerrs),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000077 E1000_NETDEV_STAT("rx_frame_errors", rx_frame_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000078 E1000_STAT("rx_no_buffer_count", stats.rnbc),
79 E1000_STAT("rx_missed_errors", stats.mpc),
80 E1000_STAT("tx_aborted_errors", stats.ecol),
81 E1000_STAT("tx_carrier_errors", stats.tncrs),
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +000082 E1000_NETDEV_STAT("tx_fifo_errors", tx_fifo_errors),
83 E1000_NETDEV_STAT("tx_heartbeat_errors", tx_heartbeat_errors),
Bruce Allanf0f1a172010-12-11 05:53:32 +000084 E1000_STAT("tx_window_errors", stats.latecol),
85 E1000_STAT("tx_abort_late_coll", stats.latecol),
86 E1000_STAT("tx_deferred_ok", stats.dc),
87 E1000_STAT("tx_single_coll_ok", stats.scc),
88 E1000_STAT("tx_multi_coll_ok", stats.mcc),
89 E1000_STAT("tx_timeout_count", tx_timeout_count),
90 E1000_STAT("tx_restart_queue", restart_queue),
91 E1000_STAT("rx_long_length_errors", stats.roc),
92 E1000_STAT("rx_short_length_errors", stats.ruc),
93 E1000_STAT("rx_align_errors", stats.algnerrc),
94 E1000_STAT("tx_tcp_seg_good", stats.tsctc),
95 E1000_STAT("tx_tcp_seg_failed", stats.tsctfc),
96 E1000_STAT("rx_flow_control_xon", stats.xonrxc),
97 E1000_STAT("rx_flow_control_xoff", stats.xoffrxc),
98 E1000_STAT("tx_flow_control_xon", stats.xontxc),
99 E1000_STAT("tx_flow_control_xoff", stats.xofftxc),
100 E1000_STAT("rx_long_byte_count", stats.gorc),
101 E1000_STAT("rx_csum_offload_good", hw_csum_good),
102 E1000_STAT("rx_csum_offload_errors", hw_csum_err),
103 E1000_STAT("rx_header_split", rx_hdr_split),
104 E1000_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
105 E1000_STAT("tx_smbus", stats.mgptc),
106 E1000_STAT("rx_smbus", stats.mgprc),
107 E1000_STAT("dropped_smbus", stats.mgpdc),
108 E1000_STAT("rx_dma_failed", rx_dma_failed),
109 E1000_STAT("tx_dma_failed", tx_dma_failed),
Auke Kokbc7f75f2007-09-17 12:30:59 -0700110};
111
Alejandro Martinez Ruizc00acf42007-10-18 10:16:33 +0200112#define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700113#define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN)
114static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
115 "Register test (offline)", "Eeprom test (offline)",
116 "Interrupt test (offline)", "Loopback test (offline)",
117 "Link test (on/offline)"
118};
Bruce Allanad680762008-03-28 09:15:03 -0700119#define E1000_TEST_LEN ARRAY_SIZE(e1000_gstrings_test)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700120
121static int e1000_get_settings(struct net_device *netdev,
122 struct ethtool_cmd *ecmd)
123{
124 struct e1000_adapter *adapter = netdev_priv(netdev);
125 struct e1000_hw *hw = &adapter->hw;
David Decotigny70739492011-04-27 18:32:40 +0000126 u32 speed;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700127
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700128 if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700129
130 ecmd->supported = (SUPPORTED_10baseT_Half |
131 SUPPORTED_10baseT_Full |
132 SUPPORTED_100baseT_Half |
133 SUPPORTED_100baseT_Full |
134 SUPPORTED_1000baseT_Full |
135 SUPPORTED_Autoneg |
136 SUPPORTED_TP);
137 if (hw->phy.type == e1000_phy_ife)
138 ecmd->supported &= ~SUPPORTED_1000baseT_Full;
139 ecmd->advertising = ADVERTISED_TP;
140
141 if (hw->mac.autoneg == 1) {
142 ecmd->advertising |= ADVERTISED_Autoneg;
143 /* the e1000 autoneg seems to match ethtool nicely */
144 ecmd->advertising |= hw->phy.autoneg_advertised;
145 }
146
147 ecmd->port = PORT_TP;
148 ecmd->phy_address = hw->phy.addr;
149 ecmd->transceiver = XCVR_INTERNAL;
150
151 } else {
152 ecmd->supported = (SUPPORTED_1000baseT_Full |
153 SUPPORTED_FIBRE |
154 SUPPORTED_Autoneg);
155
156 ecmd->advertising = (ADVERTISED_1000baseT_Full |
157 ADVERTISED_FIBRE |
158 ADVERTISED_Autoneg);
159
160 ecmd->port = PORT_FIBRE;
161 ecmd->transceiver = XCVR_EXTERNAL;
162 }
163
David Decotigny70739492011-04-27 18:32:40 +0000164 speed = -1;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000165 ecmd->duplex = -1;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700166
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000167 if (netif_running(netdev)) {
168 if (netif_carrier_ok(netdev)) {
David Decotigny70739492011-04-27 18:32:40 +0000169 speed = adapter->link_speed;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000170 ecmd->duplex = adapter->link_duplex - 1;
171 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700172 } else {
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000173 u32 status = er32(STATUS);
174 if (status & E1000_STATUS_LU) {
175 if (status & E1000_STATUS_SPEED_1000)
David Decotigny70739492011-04-27 18:32:40 +0000176 speed = SPEED_1000;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000177 else if (status & E1000_STATUS_SPEED_100)
David Decotigny70739492011-04-27 18:32:40 +0000178 speed = SPEED_100;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000179 else
David Decotigny70739492011-04-27 18:32:40 +0000180 speed = SPEED_10;
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000181
182 if (status & E1000_STATUS_FD)
183 ecmd->duplex = DUPLEX_FULL;
184 else
185 ecmd->duplex = DUPLEX_HALF;
186 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700187 }
188
David Decotigny70739492011-04-27 18:32:40 +0000189 ethtool_cmd_speed_set(ecmd, speed);
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700190 ecmd->autoneg = ((hw->phy.media_type == e1000_media_type_fiber) ||
Auke Kokbc7f75f2007-09-17 12:30:59 -0700191 hw->mac.autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
Chaitanya Lala18760f12009-06-08 14:28:54 +0000192
193 /* MDI-X => 2; MDI =>1; Invalid =>0 */
194 if ((hw->phy.media_type == e1000_media_type_copper) &&
Bruce Allan0c6bdb32010-06-17 18:58:43 +0000195 netif_carrier_ok(netdev))
Chaitanya Lala18760f12009-06-08 14:28:54 +0000196 ecmd->eth_tp_mdix = hw->phy.is_mdix ? ETH_TP_MDI_X :
197 ETH_TP_MDI;
198 else
199 ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
200
Auke Kokbc7f75f2007-09-17 12:30:59 -0700201 return 0;
202}
203
David Decotigny14ad2512011-04-27 18:32:43 +0000204static int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700205{
206 struct e1000_mac_info *mac = &adapter->hw.mac;
207
208 mac->autoneg = 0;
209
David Decotigny14ad2512011-04-27 18:32:43 +0000210 /* Make sure dplx is at most 1 bit and lsb of speed is not set
211 * for the switch() below to work */
212 if ((spd & 1) || (dplx & ~1))
213 goto err_inval;
214
Auke Kokbc7f75f2007-09-17 12:30:59 -0700215 /* Fiber NICs only allow 1000 gbps Full duplex */
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700216 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
David Decotigny14ad2512011-04-27 18:32:43 +0000217 spd != SPEED_1000 &&
218 dplx != DUPLEX_FULL) {
219 goto err_inval;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700220 }
221
David Decotigny14ad2512011-04-27 18:32:43 +0000222 switch (spd + dplx) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700223 case SPEED_10 + DUPLEX_HALF:
224 mac->forced_speed_duplex = ADVERTISE_10_HALF;
225 break;
226 case SPEED_10 + DUPLEX_FULL:
227 mac->forced_speed_duplex = ADVERTISE_10_FULL;
228 break;
229 case SPEED_100 + DUPLEX_HALF:
230 mac->forced_speed_duplex = ADVERTISE_100_HALF;
231 break;
232 case SPEED_100 + DUPLEX_FULL:
233 mac->forced_speed_duplex = ADVERTISE_100_FULL;
234 break;
235 case SPEED_1000 + DUPLEX_FULL:
236 mac->autoneg = 1;
237 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
238 break;
239 case SPEED_1000 + DUPLEX_HALF: /* not supported */
240 default:
David Decotigny14ad2512011-04-27 18:32:43 +0000241 goto err_inval;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700242 }
243 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +0000244
245err_inval:
246 e_err("Unsupported Speed/Duplex configuration\n");
247 return -EINVAL;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700248}
249
250static int e1000_set_settings(struct net_device *netdev,
251 struct ethtool_cmd *ecmd)
252{
253 struct e1000_adapter *adapter = netdev_priv(netdev);
254 struct e1000_hw *hw = &adapter->hw;
255
Bruce Allanad680762008-03-28 09:15:03 -0700256 /*
257 * When SoL/IDER sessions are active, autoneg/speed/duplex
258 * cannot be changed
259 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700260 if (e1000_check_reset_block(hw)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700261 e_err("Cannot change link characteristics when SoL/IDER is "
262 "active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700263 return -EINVAL;
264 }
265
266 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000267 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700268
269 if (ecmd->autoneg == AUTONEG_ENABLE) {
270 hw->mac.autoneg = 1;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700271 if (hw->phy.media_type == e1000_media_type_fiber)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700272 hw->phy.autoneg_advertised = ADVERTISED_1000baseT_Full |
273 ADVERTISED_FIBRE |
274 ADVERTISED_Autoneg;
275 else
276 hw->phy.autoneg_advertised = ecmd->advertising |
277 ADVERTISED_TP |
278 ADVERTISED_Autoneg;
279 ecmd->advertising = hw->phy.autoneg_advertised;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700280 if (adapter->fc_autoneg)
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800281 hw->fc.requested_mode = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700282 } else {
David Decotigny25db0332011-04-27 18:32:39 +0000283 u32 speed = ethtool_cmd_speed(ecmd);
David Decotigny14ad2512011-04-27 18:32:43 +0000284 if (e1000_set_spd_dplx(adapter, speed, ecmd->duplex)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700285 clear_bit(__E1000_RESETTING, &adapter->state);
286 return -EINVAL;
287 }
288 }
289
290 /* reset the link */
291
292 if (netif_running(adapter->netdev)) {
293 e1000e_down(adapter);
294 e1000e_up(adapter);
295 } else {
296 e1000e_reset(adapter);
297 }
298
299 clear_bit(__E1000_RESETTING, &adapter->state);
300 return 0;
301}
302
303static void e1000_get_pauseparam(struct net_device *netdev,
304 struct ethtool_pauseparam *pause)
305{
306 struct e1000_adapter *adapter = netdev_priv(netdev);
307 struct e1000_hw *hw = &adapter->hw;
308
309 pause->autoneg =
310 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
311
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800312 if (hw->fc.current_mode == e1000_fc_rx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700313 pause->rx_pause = 1;
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800314 } else if (hw->fc.current_mode == e1000_fc_tx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700315 pause->tx_pause = 1;
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800316 } else if (hw->fc.current_mode == e1000_fc_full) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700317 pause->rx_pause = 1;
318 pause->tx_pause = 1;
319 }
320}
321
322static int e1000_set_pauseparam(struct net_device *netdev,
323 struct ethtool_pauseparam *pause)
324{
325 struct e1000_adapter *adapter = netdev_priv(netdev);
326 struct e1000_hw *hw = &adapter->hw;
327 int retval = 0;
328
329 adapter->fc_autoneg = pause->autoneg;
330
331 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000332 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700333
Auke Kokbc7f75f2007-09-17 12:30:59 -0700334 if (adapter->fc_autoneg == AUTONEG_ENABLE) {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800335 hw->fc.requested_mode = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700336 if (netif_running(adapter->netdev)) {
337 e1000e_down(adapter);
338 e1000e_up(adapter);
339 } else {
340 e1000e_reset(adapter);
341 }
342 } else {
Bruce Allan5c48ef3e22008-11-21 16:57:36 -0800343 if (pause->rx_pause && pause->tx_pause)
344 hw->fc.requested_mode = e1000_fc_full;
345 else if (pause->rx_pause && !pause->tx_pause)
346 hw->fc.requested_mode = e1000_fc_rx_pause;
347 else if (!pause->rx_pause && pause->tx_pause)
348 hw->fc.requested_mode = e1000_fc_tx_pause;
349 else if (!pause->rx_pause && !pause->tx_pause)
350 hw->fc.requested_mode = e1000_fc_none;
351
352 hw->fc.current_mode = hw->fc.requested_mode;
353
Bruce Allan945eb312009-10-28 18:28:30 +0000354 if (hw->phy.media_type == e1000_media_type_fiber) {
355 retval = hw->mac.ops.setup_link(hw);
356 /* implicit goto out */
357 } else {
358 retval = e1000e_force_mac_fc(hw);
359 if (retval)
360 goto out;
361 e1000e_set_fc_watermarks(hw);
362 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700363 }
364
Bruce Allan945eb312009-10-28 18:28:30 +0000365out:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700366 clear_bit(__E1000_RESETTING, &adapter->state);
367 return retval;
368}
369
370static u32 e1000_get_rx_csum(struct net_device *netdev)
371{
372 struct e1000_adapter *adapter = netdev_priv(netdev);
Eric Dumazet807540b2010-09-23 05:40:09 +0000373 return adapter->flags & FLAG_RX_CSUM_ENABLED;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700374}
375
376static int e1000_set_rx_csum(struct net_device *netdev, u32 data)
377{
378 struct e1000_adapter *adapter = netdev_priv(netdev);
379
380 if (data)
381 adapter->flags |= FLAG_RX_CSUM_ENABLED;
382 else
383 adapter->flags &= ~FLAG_RX_CSUM_ENABLED;
384
385 if (netif_running(netdev))
386 e1000e_reinit_locked(adapter);
387 else
388 e1000e_reset(adapter);
389 return 0;
390}
391
392static u32 e1000_get_tx_csum(struct net_device *netdev)
393{
Eric Dumazet807540b2010-09-23 05:40:09 +0000394 return (netdev->features & NETIF_F_HW_CSUM) != 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700395}
396
397static int e1000_set_tx_csum(struct net_device *netdev, u32 data)
398{
399 if (data)
400 netdev->features |= NETIF_F_HW_CSUM;
401 else
402 netdev->features &= ~NETIF_F_HW_CSUM;
403
404 return 0;
405}
406
407static int e1000_set_tso(struct net_device *netdev, u32 data)
408{
409 struct e1000_adapter *adapter = netdev_priv(netdev);
410
411 if (data) {
412 netdev->features |= NETIF_F_TSO;
413 netdev->features |= NETIF_F_TSO6;
414 } else {
415 netdev->features &= ~NETIF_F_TSO;
416 netdev->features &= ~NETIF_F_TSO6;
417 }
418
Auke Kokbc7f75f2007-09-17 12:30:59 -0700419 adapter->flags |= FLAG_TSO_FORCE;
420 return 0;
421}
422
423static u32 e1000_get_msglevel(struct net_device *netdev)
424{
425 struct e1000_adapter *adapter = netdev_priv(netdev);
426 return adapter->msg_enable;
427}
428
429static void e1000_set_msglevel(struct net_device *netdev, u32 data)
430{
431 struct e1000_adapter *adapter = netdev_priv(netdev);
432 adapter->msg_enable = data;
433}
434
435static int e1000_get_regs_len(struct net_device *netdev)
436{
437#define E1000_REGS_LEN 32 /* overestimate */
438 return E1000_REGS_LEN * sizeof(u32);
439}
440
441static void e1000_get_regs(struct net_device *netdev,
442 struct ethtool_regs *regs, void *p)
443{
444 struct e1000_adapter *adapter = netdev_priv(netdev);
445 struct e1000_hw *hw = &adapter->hw;
446 u32 *regs_buff = p;
447 u16 phy_data;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700448
449 memset(p, 0, E1000_REGS_LEN * sizeof(u32));
450
Sergei Shtylyovff938e42011-02-28 11:57:33 -0800451 regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
452 adapter->pdev->device;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700453
454 regs_buff[0] = er32(CTRL);
455 regs_buff[1] = er32(STATUS);
456
457 regs_buff[2] = er32(RCTL);
458 regs_buff[3] = er32(RDLEN);
459 regs_buff[4] = er32(RDH);
460 regs_buff[5] = er32(RDT);
461 regs_buff[6] = er32(RDTR);
462
463 regs_buff[7] = er32(TCTL);
464 regs_buff[8] = er32(TDLEN);
465 regs_buff[9] = er32(TDH);
466 regs_buff[10] = er32(TDT);
467 regs_buff[11] = er32(TIDV);
468
469 regs_buff[12] = adapter->hw.phy.type; /* PHY type (IGP=1, M88=0) */
Jesse Brandeburg23033fa2008-10-02 16:33:30 -0700470
471 /* ethtool doesn't use anything past this point, so all this
472 * code is likely legacy junk for apps that may or may not
473 * exist */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700474 if (hw->phy.type == e1000_phy_m88) {
475 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
476 regs_buff[13] = (u32)phy_data; /* cable length */
477 regs_buff[14] = 0; /* Dummy (to align w/ IGP phy reg dump) */
478 regs_buff[15] = 0; /* Dummy (to align w/ IGP phy reg dump) */
479 regs_buff[16] = 0; /* Dummy (to align w/ IGP phy reg dump) */
480 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
481 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
482 regs_buff[18] = regs_buff[13]; /* cable polarity */
483 regs_buff[19] = 0; /* Dummy (to align w/ IGP phy reg dump) */
484 regs_buff[20] = regs_buff[17]; /* polarity correction */
485 /* phy receive errors */
486 regs_buff[22] = adapter->phy_stats.receive_errors;
487 regs_buff[23] = regs_buff[13]; /* mdix mode */
488 }
Jesse Brandeburg23033fa2008-10-02 16:33:30 -0700489 regs_buff[21] = 0; /* was idle_errors */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700490 e1e_rphy(hw, PHY_1000T_STATUS, &phy_data);
491 regs_buff[24] = (u32)phy_data; /* phy local receiver status */
492 regs_buff[25] = regs_buff[24]; /* phy remote receiver status */
493}
494
495static int e1000_get_eeprom_len(struct net_device *netdev)
496{
497 struct e1000_adapter *adapter = netdev_priv(netdev);
498 return adapter->hw.nvm.word_size * 2;
499}
500
501static int e1000_get_eeprom(struct net_device *netdev,
502 struct ethtool_eeprom *eeprom, u8 *bytes)
503{
504 struct e1000_adapter *adapter = netdev_priv(netdev);
505 struct e1000_hw *hw = &adapter->hw;
506 u16 *eeprom_buff;
507 int first_word;
508 int last_word;
509 int ret_val = 0;
510 u16 i;
511
512 if (eeprom->len == 0)
513 return -EINVAL;
514
515 eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16);
516
517 first_word = eeprom->offset >> 1;
518 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
519
520 eeprom_buff = kmalloc(sizeof(u16) *
521 (last_word - first_word + 1), GFP_KERNEL);
522 if (!eeprom_buff)
523 return -ENOMEM;
524
525 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
526 ret_val = e1000_read_nvm(hw, first_word,
527 last_word - first_word + 1,
528 eeprom_buff);
529 } else {
530 for (i = 0; i < last_word - first_word + 1; i++) {
531 ret_val = e1000_read_nvm(hw, first_word + i, 1,
532 &eeprom_buff[i]);
Bruce Allane2434552008-11-21 17:02:41 -0800533 if (ret_val)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700534 break;
535 }
536 }
537
Bruce Allane2434552008-11-21 17:02:41 -0800538 if (ret_val) {
539 /* a read error occurred, throw away the result */
Roel Kluin8528b012009-12-01 15:54:24 +0000540 memset(eeprom_buff, 0xff, sizeof(u16) *
541 (last_word - first_word + 1));
Bruce Allane2434552008-11-21 17:02:41 -0800542 } else {
543 /* Device's eeprom is always little-endian, word addressable */
544 for (i = 0; i < last_word - first_word + 1; i++)
545 le16_to_cpus(&eeprom_buff[i]);
546 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700547
548 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
549 kfree(eeprom_buff);
550
551 return ret_val;
552}
553
554static int e1000_set_eeprom(struct net_device *netdev,
555 struct ethtool_eeprom *eeprom, u8 *bytes)
556{
557 struct e1000_adapter *adapter = netdev_priv(netdev);
558 struct e1000_hw *hw = &adapter->hw;
559 u16 *eeprom_buff;
560 void *ptr;
561 int max_len;
562 int first_word;
563 int last_word;
564 int ret_val = 0;
565 u16 i;
566
567 if (eeprom->len == 0)
568 return -EOPNOTSUPP;
569
570 if (eeprom->magic != (adapter->pdev->vendor | (adapter->pdev->device << 16)))
571 return -EFAULT;
572
Bruce Allan4a770352008-10-01 17:18:35 -0700573 if (adapter->flags & FLAG_READ_ONLY_NVM)
574 return -EINVAL;
575
Auke Kokbc7f75f2007-09-17 12:30:59 -0700576 max_len = hw->nvm.word_size * 2;
577
578 first_word = eeprom->offset >> 1;
579 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
580 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
581 if (!eeprom_buff)
582 return -ENOMEM;
583
584 ptr = (void *)eeprom_buff;
585
586 if (eeprom->offset & 1) {
587 /* need read/modify/write of first changed EEPROM word */
588 /* only the second byte of the word is being modified */
589 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
590 ptr++;
591 }
592 if (((eeprom->offset + eeprom->len) & 1) && (ret_val == 0))
593 /* need read/modify/write of last changed EEPROM word */
594 /* only the first byte of the word is being modified */
595 ret_val = e1000_read_nvm(hw, last_word, 1,
596 &eeprom_buff[last_word - first_word]);
597
Bruce Allane2434552008-11-21 17:02:41 -0800598 if (ret_val)
599 goto out;
600
Auke Kokbc7f75f2007-09-17 12:30:59 -0700601 /* Device's eeprom is always little-endian, word addressable */
602 for (i = 0; i < last_word - first_word + 1; i++)
603 le16_to_cpus(&eeprom_buff[i]);
604
605 memcpy(ptr, bytes, eeprom->len);
606
607 for (i = 0; i < last_word - first_word + 1; i++)
608 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
609
610 ret_val = e1000_write_nvm(hw, first_word,
611 last_word - first_word + 1, eeprom_buff);
612
Bruce Allane2434552008-11-21 17:02:41 -0800613 if (ret_val)
614 goto out;
615
Bruce Allanad680762008-03-28 09:15:03 -0700616 /*
617 * Update the checksum over the first part of the EEPROM if needed
Bruce Allane2434552008-11-21 17:02:41 -0800618 * and flush shadow RAM for applicable controllers
Bruce Allanad680762008-03-28 09:15:03 -0700619 */
Bruce Allane2434552008-11-21 17:02:41 -0800620 if ((first_word <= NVM_CHECKSUM_REG) ||
Bruce Allanf89271dd2009-11-20 23:22:20 +0000621 (hw->mac.type == e1000_82583) ||
622 (hw->mac.type == e1000_82574) ||
623 (hw->mac.type == e1000_82573))
Bruce Allane2434552008-11-21 17:02:41 -0800624 ret_val = e1000e_update_nvm_checksum(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700625
Bruce Allane2434552008-11-21 17:02:41 -0800626out:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700627 kfree(eeprom_buff);
628 return ret_val;
629}
630
631static void e1000_get_drvinfo(struct net_device *netdev,
632 struct ethtool_drvinfo *drvinfo)
633{
634 struct e1000_adapter *adapter = netdev_priv(netdev);
635 char firmware_version[32];
Auke Kokbc7f75f2007-09-17 12:30:59 -0700636
Bruce Allane0dc4f12011-01-06 14:29:50 +0000637 strncpy(drvinfo->driver, e1000e_driver_name,
638 sizeof(drvinfo->driver) - 1);
639 strncpy(drvinfo->version, e1000e_driver_version,
640 sizeof(drvinfo->version) - 1);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700641
Bruce Allanad680762008-03-28 09:15:03 -0700642 /*
643 * EEPROM image version # is reported as firmware version # for
644 * PCI-E controllers
645 */
Bruce Allane0dc4f12011-01-06 14:29:50 +0000646 snprintf(firmware_version, sizeof(firmware_version), "%d.%d-%d",
Bruce Allan84527592008-11-21 17:00:22 -0800647 (adapter->eeprom_vers & 0xF000) >> 12,
648 (adapter->eeprom_vers & 0x0FF0) >> 4,
649 (adapter->eeprom_vers & 0x000F));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700650
Bruce Allane0dc4f12011-01-06 14:29:50 +0000651 strncpy(drvinfo->fw_version, firmware_version,
652 sizeof(drvinfo->fw_version) - 1);
653 strncpy(drvinfo->bus_info, pci_name(adapter->pdev),
654 sizeof(drvinfo->bus_info) - 1);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700655 drvinfo->regdump_len = e1000_get_regs_len(netdev);
656 drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
657}
658
659static void e1000_get_ringparam(struct net_device *netdev,
660 struct ethtool_ringparam *ring)
661{
662 struct e1000_adapter *adapter = netdev_priv(netdev);
663 struct e1000_ring *tx_ring = adapter->tx_ring;
664 struct e1000_ring *rx_ring = adapter->rx_ring;
665
666 ring->rx_max_pending = E1000_MAX_RXD;
667 ring->tx_max_pending = E1000_MAX_TXD;
668 ring->rx_mini_max_pending = 0;
669 ring->rx_jumbo_max_pending = 0;
670 ring->rx_pending = rx_ring->count;
671 ring->tx_pending = tx_ring->count;
672 ring->rx_mini_pending = 0;
673 ring->rx_jumbo_pending = 0;
674}
675
676static int e1000_set_ringparam(struct net_device *netdev,
677 struct ethtool_ringparam *ring)
678{
679 struct e1000_adapter *adapter = netdev_priv(netdev);
680 struct e1000_ring *tx_ring, *tx_old;
681 struct e1000_ring *rx_ring, *rx_old;
682 int err;
683
684 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
685 return -EINVAL;
686
687 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +0000688 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700689
690 if (netif_running(adapter->netdev))
691 e1000e_down(adapter);
692
693 tx_old = adapter->tx_ring;
694 rx_old = adapter->rx_ring;
695
696 err = -ENOMEM;
Bruce Allan05b93212011-01-05 07:10:38 +0000697 tx_ring = kmemdup(tx_old, sizeof(struct e1000_ring), GFP_KERNEL);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700698 if (!tx_ring)
699 goto err_alloc_tx;
700
Bruce Allan05b93212011-01-05 07:10:38 +0000701 rx_ring = kmemdup(rx_old, sizeof(struct e1000_ring), GFP_KERNEL);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700702 if (!rx_ring)
703 goto err_alloc_rx;
704
705 adapter->tx_ring = tx_ring;
706 adapter->rx_ring = rx_ring;
707
708 rx_ring->count = max(ring->rx_pending, (u32)E1000_MIN_RXD);
709 rx_ring->count = min(rx_ring->count, (u32)(E1000_MAX_RXD));
710 rx_ring->count = ALIGN(rx_ring->count, REQ_RX_DESCRIPTOR_MULTIPLE);
711
712 tx_ring->count = max(ring->tx_pending, (u32)E1000_MIN_TXD);
713 tx_ring->count = min(tx_ring->count, (u32)(E1000_MAX_TXD));
714 tx_ring->count = ALIGN(tx_ring->count, REQ_TX_DESCRIPTOR_MULTIPLE);
715
716 if (netif_running(adapter->netdev)) {
717 /* Try to get new resources before deleting old */
718 err = e1000e_setup_rx_resources(adapter);
719 if (err)
720 goto err_setup_rx;
721 err = e1000e_setup_tx_resources(adapter);
722 if (err)
723 goto err_setup_tx;
724
Bruce Allanad680762008-03-28 09:15:03 -0700725 /*
726 * restore the old in order to free it,
727 * then add in the new
728 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700729 adapter->rx_ring = rx_old;
730 adapter->tx_ring = tx_old;
731 e1000e_free_rx_resources(adapter);
732 e1000e_free_tx_resources(adapter);
733 kfree(tx_old);
734 kfree(rx_old);
735 adapter->rx_ring = rx_ring;
736 adapter->tx_ring = tx_ring;
737 err = e1000e_up(adapter);
738 if (err)
739 goto err_setup;
740 }
741
742 clear_bit(__E1000_RESETTING, &adapter->state);
743 return 0;
744err_setup_tx:
745 e1000e_free_rx_resources(adapter);
746err_setup_rx:
747 adapter->rx_ring = rx_old;
748 adapter->tx_ring = tx_old;
749 kfree(rx_ring);
750err_alloc_rx:
751 kfree(tx_ring);
752err_alloc_tx:
753 e1000e_up(adapter);
754err_setup:
755 clear_bit(__E1000_RESETTING, &adapter->state);
756 return err;
757}
758
Bruce Allancef8c792008-04-02 13:48:23 -0700759static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
760 int reg, int offset, u32 mask, u32 write)
Joe Perches2a887192007-11-13 20:53:51 -0800761{
Bruce Allancef8c792008-04-02 13:48:23 -0700762 u32 pat, val;
Bruce Allan64806412010-12-11 05:53:42 +0000763 static const u32 test[] = {
764 0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
Bruce Allancef8c792008-04-02 13:48:23 -0700765 for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
Joe Perches2a887192007-11-13 20:53:51 -0800766 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
Bruce Allancef8c792008-04-02 13:48:23 -0700767 (test[pat] & write));
768 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
769 if (val != (test[pat] & write & mask)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700770 e_err("pattern test reg %04X failed: got 0x%08X "
771 "expected 0x%08X\n", reg + offset, val,
772 (test[pat] & write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800773 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700774 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800775 }
776 }
Bruce Allancef8c792008-04-02 13:48:23 -0700777 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700778}
779
Joe Perches2a887192007-11-13 20:53:51 -0800780static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
781 int reg, u32 mask, u32 write)
782{
Bruce Allancef8c792008-04-02 13:48:23 -0700783 u32 val;
Joe Perches2a887192007-11-13 20:53:51 -0800784 __ew32(&adapter->hw, reg, write & mask);
Bruce Allancef8c792008-04-02 13:48:23 -0700785 val = __er32(&adapter->hw, reg);
786 if ((write & mask) != (val & mask)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700787 e_err("set/check reg %04X test failed: got 0x%08X "
788 "expected 0x%08X\n", reg, (val & mask), (write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800789 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700790 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800791 }
Bruce Allancef8c792008-04-02 13:48:23 -0700792 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700793}
Bruce Allancef8c792008-04-02 13:48:23 -0700794#define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write) \
795 do { \
796 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
797 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800798 } while (0)
Bruce Allancef8c792008-04-02 13:48:23 -0700799#define REG_PATTERN_TEST(reg, mask, write) \
800 REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
Joe Perches2a887192007-11-13 20:53:51 -0800801
Bruce Allancef8c792008-04-02 13:48:23 -0700802#define REG_SET_AND_CHECK(reg, mask, write) \
803 do { \
804 if (reg_set_and_check(adapter, data, reg, mask, write)) \
805 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800806 } while (0)
807
Auke Kokbc7f75f2007-09-17 12:30:59 -0700808static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
809{
810 struct e1000_hw *hw = &adapter->hw;
811 struct e1000_mac_info *mac = &adapter->hw.mac;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700812 u32 value;
813 u32 before;
814 u32 after;
815 u32 i;
816 u32 toggle;
Bruce Allana4f58f52009-06-02 11:29:18 +0000817 u32 mask;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700818
Bruce Allanad680762008-03-28 09:15:03 -0700819 /*
820 * The status register is Read Only, so a write should fail.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700821 * Some bits that get toggled are ignored.
822 */
823 switch (mac->type) {
824 /* there are several bits on newer hardware that are r/w */
825 case e1000_82571:
826 case e1000_82572:
827 case e1000_80003es2lan:
828 toggle = 0x7FFFF3FF;
829 break;
Bruce Allana4f58f52009-06-02 11:29:18 +0000830 default:
Auke Kokbc7f75f2007-09-17 12:30:59 -0700831 toggle = 0x7FFFF033;
832 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700833 }
834
835 before = er32(STATUS);
836 value = (er32(STATUS) & toggle);
837 ew32(STATUS, toggle);
838 after = er32(STATUS) & toggle;
839 if (value != after) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700840 e_err("failed STATUS register test got: 0x%08X expected: "
841 "0x%08X\n", after, value);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700842 *data = 1;
843 return 1;
844 }
845 /* restore previous status */
846 ew32(STATUS, before);
847
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700848 if (!(adapter->flags & FLAG_IS_ICH)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700849 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
850 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
851 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
852 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
853 }
854
855 REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
856 REG_PATTERN_TEST(E1000_RDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
857 REG_PATTERN_TEST(E1000_RDLEN, 0x000FFF80, 0x000FFFFF);
858 REG_PATTERN_TEST(E1000_RDH, 0x0000FFFF, 0x0000FFFF);
859 REG_PATTERN_TEST(E1000_RDT, 0x0000FFFF, 0x0000FFFF);
860 REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
861 REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
862 REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
863 REG_PATTERN_TEST(E1000_TDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
864 REG_PATTERN_TEST(E1000_TDLEN, 0x000FFF80, 0x000FFFFF);
865
866 REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
867
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700868 before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700869 REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
870 REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
871
Auke Kok86582512007-10-04 15:00:08 -0700872 REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
873 REG_PATTERN_TEST(E1000_RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700874 if (!(adapter->flags & FLAG_IS_ICH))
Auke Kok86582512007-10-04 15:00:08 -0700875 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
876 REG_PATTERN_TEST(E1000_TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
877 REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
Bruce Allana4f58f52009-06-02 11:29:18 +0000878 mask = 0x8003FFFF;
879 switch (mac->type) {
880 case e1000_ich10lan:
881 case e1000_pchlan:
Bruce Alland3738bb2010-06-16 13:27:28 +0000882 case e1000_pch2lan:
Bruce Allana4f58f52009-06-02 11:29:18 +0000883 mask |= (1 << 18);
884 break;
885 default:
886 break;
887 }
Auke Kok86582512007-10-04 15:00:08 -0700888 for (i = 0; i < mac->rar_entry_count; i++)
889 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1),
Bruce Allana4f58f52009-06-02 11:29:18 +0000890 mask, 0xFFFFFFFF);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700891
892 for (i = 0; i < mac->mta_reg_count; i++)
893 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
894
895 *data = 0;
896 return 0;
897}
898
899static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
900{
901 u16 temp;
902 u16 checksum = 0;
903 u16 i;
904
905 *data = 0;
906 /* Read and add up the contents of the EEPROM */
907 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
908 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
909 *data = 1;
Bruce Allane2434552008-11-21 17:02:41 -0800910 return *data;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700911 }
912 checksum += temp;
913 }
914
915 /* If Checksum is not Correct return error else test passed */
916 if ((checksum != (u16) NVM_SUM) && !(*data))
917 *data = 2;
918
919 return *data;
920}
921
922static irqreturn_t e1000_test_intr(int irq, void *data)
923{
924 struct net_device *netdev = (struct net_device *) data;
925 struct e1000_adapter *adapter = netdev_priv(netdev);
926 struct e1000_hw *hw = &adapter->hw;
927
928 adapter->test_icr |= er32(ICR);
929
930 return IRQ_HANDLED;
931}
932
933static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
934{
935 struct net_device *netdev = adapter->netdev;
936 struct e1000_hw *hw = &adapter->hw;
937 u32 mask;
938 u32 shared_int = 1;
939 u32 irq = adapter->pdev->irq;
940 int i;
Bruce Allan4662e822008-08-26 18:37:06 -0700941 int ret_val = 0;
942 int int_mode = E1000E_INT_MODE_LEGACY;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700943
944 *data = 0;
945
Bruce Allan4662e822008-08-26 18:37:06 -0700946 /* NOTE: we don't test MSI/MSI-X interrupts here, yet */
947 if (adapter->int_mode == E1000E_INT_MODE_MSIX) {
948 int_mode = adapter->int_mode;
949 e1000e_reset_interrupt_capability(adapter);
950 adapter->int_mode = E1000E_INT_MODE_LEGACY;
951 e1000e_set_interrupt_capability(adapter);
952 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700953 /* Hook up test interrupt handler just for this test */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800954 if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700955 netdev)) {
956 shared_int = 0;
Joe Perchesa0607fd2009-11-18 23:29:17 -0800957 } else if (request_irq(irq, e1000_test_intr, IRQF_SHARED,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700958 netdev->name, netdev)) {
959 *data = 1;
Bruce Allan4662e822008-08-26 18:37:06 -0700960 ret_val = -1;
961 goto out;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700962 }
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700963 e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700964
965 /* Disable all the interrupts */
966 ew32(IMC, 0xFFFFFFFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +0000967 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +0000968 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700969
970 /* Test each interrupt */
971 for (i = 0; i < 10; i++) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700972 /* Interrupt to test */
973 mask = 1 << i;
974
Bruce Allanf4187b52008-08-26 18:36:50 -0700975 if (adapter->flags & FLAG_IS_ICH) {
976 switch (mask) {
977 case E1000_ICR_RXSEQ:
978 continue;
979 case 0x00000100:
980 if (adapter->hw.mac.type == e1000_ich8lan ||
981 adapter->hw.mac.type == e1000_ich9lan)
982 continue;
983 break;
984 default:
985 break;
986 }
987 }
988
Auke Kokbc7f75f2007-09-17 12:30:59 -0700989 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -0700990 /*
991 * Disable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -0700992 * the cause register and then force the same
993 * interrupt and see if one gets posted. If
994 * an interrupt was posted to the bus, the
995 * test failed.
996 */
997 adapter->test_icr = 0;
998 ew32(IMC, mask);
999 ew32(ICS, mask);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001000 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001001 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001002
1003 if (adapter->test_icr & mask) {
1004 *data = 3;
1005 break;
1006 }
1007 }
1008
Bruce Allanad680762008-03-28 09:15:03 -07001009 /*
1010 * Enable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -07001011 * the cause register and then force the same
1012 * interrupt and see if one gets posted. If
1013 * an interrupt was not posted to the bus, the
1014 * test failed.
1015 */
1016 adapter->test_icr = 0;
1017 ew32(IMS, mask);
1018 ew32(ICS, mask);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001019 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001020 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001021
1022 if (!(adapter->test_icr & mask)) {
1023 *data = 4;
1024 break;
1025 }
1026
1027 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -07001028 /*
1029 * Disable the other interrupts to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -07001030 * the cause register and then force the other
1031 * interrupts and see if any get posted. If
1032 * an interrupt was posted to the bus, the
1033 * test failed.
1034 */
1035 adapter->test_icr = 0;
1036 ew32(IMC, ~mask & 0x00007FFF);
1037 ew32(ICS, ~mask & 0x00007FFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001038 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001039 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001040
1041 if (adapter->test_icr) {
1042 *data = 5;
1043 break;
1044 }
1045 }
1046 }
1047
1048 /* Disable all the interrupts */
1049 ew32(IMC, 0xFFFFFFFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001050 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001051 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001052
1053 /* Unhook test interrupt handler */
1054 free_irq(irq, netdev);
1055
Bruce Allan4662e822008-08-26 18:37:06 -07001056out:
1057 if (int_mode == E1000E_INT_MODE_MSIX) {
1058 e1000e_reset_interrupt_capability(adapter);
1059 adapter->int_mode = int_mode;
1060 e1000e_set_interrupt_capability(adapter);
1061 }
1062
1063 return ret_val;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001064}
1065
1066static void e1000_free_desc_rings(struct e1000_adapter *adapter)
1067{
1068 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1069 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1070 struct pci_dev *pdev = adapter->pdev;
1071 int i;
1072
1073 if (tx_ring->desc && tx_ring->buffer_info) {
1074 for (i = 0; i < tx_ring->count; i++) {
1075 if (tx_ring->buffer_info[i].dma)
Nick Nunley0be3f552010-04-27 13:09:05 +00001076 dma_unmap_single(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001077 tx_ring->buffer_info[i].dma,
1078 tx_ring->buffer_info[i].length,
Nick Nunley0be3f552010-04-27 13:09:05 +00001079 DMA_TO_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001080 if (tx_ring->buffer_info[i].skb)
1081 dev_kfree_skb(tx_ring->buffer_info[i].skb);
1082 }
1083 }
1084
1085 if (rx_ring->desc && rx_ring->buffer_info) {
1086 for (i = 0; i < rx_ring->count; i++) {
1087 if (rx_ring->buffer_info[i].dma)
Nick Nunley0be3f552010-04-27 13:09:05 +00001088 dma_unmap_single(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001089 rx_ring->buffer_info[i].dma,
Nick Nunley0be3f552010-04-27 13:09:05 +00001090 2048, DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001091 if (rx_ring->buffer_info[i].skb)
1092 dev_kfree_skb(rx_ring->buffer_info[i].skb);
1093 }
1094 }
1095
1096 if (tx_ring->desc) {
1097 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1098 tx_ring->dma);
1099 tx_ring->desc = NULL;
1100 }
1101 if (rx_ring->desc) {
1102 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1103 rx_ring->dma);
1104 rx_ring->desc = NULL;
1105 }
1106
1107 kfree(tx_ring->buffer_info);
1108 tx_ring->buffer_info = NULL;
1109 kfree(rx_ring->buffer_info);
1110 rx_ring->buffer_info = NULL;
1111}
1112
1113static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1114{
1115 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1116 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1117 struct pci_dev *pdev = adapter->pdev;
1118 struct e1000_hw *hw = &adapter->hw;
1119 u32 rctl;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001120 int i;
1121 int ret_val;
1122
1123 /* Setup Tx descriptor ring and Tx buffers */
1124
1125 if (!tx_ring->count)
1126 tx_ring->count = E1000_DEFAULT_TXD;
1127
Bruce Allancef8c792008-04-02 13:48:23 -07001128 tx_ring->buffer_info = kcalloc(tx_ring->count,
1129 sizeof(struct e1000_buffer),
1130 GFP_KERNEL);
1131 if (!(tx_ring->buffer_info)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001132 ret_val = 1;
1133 goto err_nomem;
1134 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001135
1136 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1137 tx_ring->size = ALIGN(tx_ring->size, 4096);
1138 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
1139 &tx_ring->dma, GFP_KERNEL);
1140 if (!tx_ring->desc) {
1141 ret_val = 2;
1142 goto err_nomem;
1143 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001144 tx_ring->next_to_use = 0;
1145 tx_ring->next_to_clean = 0;
1146
Bruce Allancef8c792008-04-02 13:48:23 -07001147 ew32(TDBAL, ((u64) tx_ring->dma & 0x00000000FFFFFFFF));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001148 ew32(TDBAH, ((u64) tx_ring->dma >> 32));
Bruce Allancef8c792008-04-02 13:48:23 -07001149 ew32(TDLEN, tx_ring->count * sizeof(struct e1000_tx_desc));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001150 ew32(TDH, 0);
1151 ew32(TDT, 0);
Bruce Allancef8c792008-04-02 13:48:23 -07001152 ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN | E1000_TCTL_MULR |
1153 E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1154 E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001155
1156 for (i = 0; i < tx_ring->count; i++) {
1157 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
1158 struct sk_buff *skb;
1159 unsigned int skb_size = 1024;
1160
1161 skb = alloc_skb(skb_size, GFP_KERNEL);
1162 if (!skb) {
1163 ret_val = 3;
1164 goto err_nomem;
1165 }
1166 skb_put(skb, skb_size);
1167 tx_ring->buffer_info[i].skb = skb;
1168 tx_ring->buffer_info[i].length = skb->len;
1169 tx_ring->buffer_info[i].dma =
Nick Nunley0be3f552010-04-27 13:09:05 +00001170 dma_map_single(&pdev->dev, skb->data, skb->len,
1171 DMA_TO_DEVICE);
1172 if (dma_mapping_error(&pdev->dev,
1173 tx_ring->buffer_info[i].dma)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001174 ret_val = 4;
1175 goto err_nomem;
1176 }
Bruce Allancef8c792008-04-02 13:48:23 -07001177 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001178 tx_desc->lower.data = cpu_to_le32(skb->len);
1179 tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1180 E1000_TXD_CMD_IFCS |
Bruce Allancef8c792008-04-02 13:48:23 -07001181 E1000_TXD_CMD_RS);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001182 tx_desc->upper.data = 0;
1183 }
1184
1185 /* Setup Rx descriptor ring and Rx buffers */
1186
1187 if (!rx_ring->count)
1188 rx_ring->count = E1000_DEFAULT_RXD;
1189
Bruce Allancef8c792008-04-02 13:48:23 -07001190 rx_ring->buffer_info = kcalloc(rx_ring->count,
1191 sizeof(struct e1000_buffer),
1192 GFP_KERNEL);
1193 if (!(rx_ring->buffer_info)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001194 ret_val = 5;
1195 goto err_nomem;
1196 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001197
1198 rx_ring->size = rx_ring->count * sizeof(struct e1000_rx_desc);
1199 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
1200 &rx_ring->dma, GFP_KERNEL);
1201 if (!rx_ring->desc) {
1202 ret_val = 6;
1203 goto err_nomem;
1204 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001205 rx_ring->next_to_use = 0;
1206 rx_ring->next_to_clean = 0;
1207
1208 rctl = er32(RCTL);
Bruce Allan7f99ae632011-07-22 06:21:35 +00001209 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
1210 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001211 ew32(RDBAL, ((u64) rx_ring->dma & 0xFFFFFFFF));
1212 ew32(RDBAH, ((u64) rx_ring->dma >> 32));
1213 ew32(RDLEN, rx_ring->size);
1214 ew32(RDH, 0);
1215 ew32(RDT, 0);
1216 rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
Bruce Allancef8c792008-04-02 13:48:23 -07001217 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1218 E1000_RCTL_SBP | E1000_RCTL_SECRC |
Auke Kokbc7f75f2007-09-17 12:30:59 -07001219 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1220 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1221 ew32(RCTL, rctl);
1222
1223 for (i = 0; i < rx_ring->count; i++) {
1224 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
1225 struct sk_buff *skb;
1226
1227 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1228 if (!skb) {
1229 ret_val = 7;
1230 goto err_nomem;
1231 }
1232 skb_reserve(skb, NET_IP_ALIGN);
1233 rx_ring->buffer_info[i].skb = skb;
1234 rx_ring->buffer_info[i].dma =
Nick Nunley0be3f552010-04-27 13:09:05 +00001235 dma_map_single(&pdev->dev, skb->data, 2048,
1236 DMA_FROM_DEVICE);
1237 if (dma_mapping_error(&pdev->dev,
1238 rx_ring->buffer_info[i].dma)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001239 ret_val = 8;
1240 goto err_nomem;
1241 }
1242 rx_desc->buffer_addr =
1243 cpu_to_le64(rx_ring->buffer_info[i].dma);
1244 memset(skb->data, 0x00, skb->len);
1245 }
1246
1247 return 0;
1248
1249err_nomem:
1250 e1000_free_desc_rings(adapter);
1251 return ret_val;
1252}
1253
1254static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1255{
1256 /* Write out to PHY registers 29 and 30 to disable the Receiver. */
1257 e1e_wphy(&adapter->hw, 29, 0x001F);
1258 e1e_wphy(&adapter->hw, 30, 0x8FFC);
1259 e1e_wphy(&adapter->hw, 29, 0x001A);
1260 e1e_wphy(&adapter->hw, 30, 0x8FF0);
1261}
1262
1263static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1264{
1265 struct e1000_hw *hw = &adapter->hw;
1266 u32 ctrl_reg = 0;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001267 u16 phy_reg = 0;
Bruce Allancbd006c2010-11-24 06:01:30 +00001268 s32 ret_val = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001269
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001270 hw->mac.autoneg = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001271
Bruce Allan3af50482010-06-16 13:25:55 +00001272 if (hw->phy.type == e1000_phy_ife) {
1273 /* force 100, set loopback */
1274 e1e_wphy(hw, PHY_CONTROL, 0x6100);
Bruce Allanbb436b22009-11-20 23:24:11 +00001275
Bruce Allan3af50482010-06-16 13:25:55 +00001276 /* Now set up the MAC to the same speed/duplex as the PHY. */
1277 ctrl_reg = er32(CTRL);
1278 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1279 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1280 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1281 E1000_CTRL_SPD_100 |/* Force Speed to 100 */
1282 E1000_CTRL_FD); /* Force Duplex to FULL */
1283
1284 ew32(CTRL, ctrl_reg);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001285 e1e_flush();
Bruce Allan3af50482010-06-16 13:25:55 +00001286 udelay(500);
1287
1288 return 0;
1289 }
1290
1291 /* Specific PHY configuration for loopback */
1292 switch (hw->phy.type) {
1293 case e1000_phy_m88:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001294 /* Auto-MDI/MDIX Off */
1295 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1296 /* reset to update Auto-MDI/MDIX */
1297 e1e_wphy(hw, PHY_CONTROL, 0x9140);
1298 /* autoneg off */
1299 e1e_wphy(hw, PHY_CONTROL, 0x8140);
Bruce Allan3af50482010-06-16 13:25:55 +00001300 break;
1301 case e1000_phy_gg82563:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001302 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
Bruce Allancef8c792008-04-02 13:48:23 -07001303 break;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001304 case e1000_phy_bm:
1305 /* Set Default MAC Interface speed to 1GB */
1306 e1e_rphy(hw, PHY_REG(2, 21), &phy_reg);
1307 phy_reg &= ~0x0007;
1308 phy_reg |= 0x006;
1309 e1e_wphy(hw, PHY_REG(2, 21), phy_reg);
1310 /* Assert SW reset for above settings to take effect */
1311 e1000e_commit_phy(hw);
1312 mdelay(1);
1313 /* Force Full Duplex */
1314 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1315 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C);
1316 /* Set Link Up (in force link) */
1317 e1e_rphy(hw, PHY_REG(776, 16), &phy_reg);
1318 e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040);
1319 /* Force Link */
1320 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1321 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040);
1322 /* Set Early Link Enable */
1323 e1e_rphy(hw, PHY_REG(769, 20), &phy_reg);
1324 e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400);
Bruce Allan3af50482010-06-16 13:25:55 +00001325 break;
1326 case e1000_phy_82577:
1327 case e1000_phy_82578:
1328 /* Workaround: K1 must be disabled for stable 1Gbps operation */
Bruce Allancbd006c2010-11-24 06:01:30 +00001329 ret_val = hw->phy.ops.acquire(hw);
1330 if (ret_val) {
1331 e_err("Cannot setup 1Gbps loopback.\n");
1332 return ret_val;
1333 }
Bruce Allan3af50482010-06-16 13:25:55 +00001334 e1000_configure_k1_ich8lan(hw, false);
Bruce Allancbd006c2010-11-24 06:01:30 +00001335 hw->phy.ops.release(hw);
Bruce Allan3af50482010-06-16 13:25:55 +00001336 break;
Bruce Alland3738bb2010-06-16 13:27:28 +00001337 case e1000_phy_82579:
1338 /* Disable PHY energy detect power down */
1339 e1e_rphy(hw, PHY_REG(0, 21), &phy_reg);
1340 e1e_wphy(hw, PHY_REG(0, 21), phy_reg & ~(1 << 3));
1341 /* Disable full chip energy detect */
1342 e1e_rphy(hw, PHY_REG(776, 18), &phy_reg);
1343 e1e_wphy(hw, PHY_REG(776, 18), phy_reg | 1);
1344 /* Enable loopback on the PHY */
1345#define I82577_PHY_LBK_CTRL 19
1346 e1e_wphy(hw, I82577_PHY_LBK_CTRL, 0x8001);
1347 break;
Bruce Allancef8c792008-04-02 13:48:23 -07001348 default:
Bruce Allan3af50482010-06-16 13:25:55 +00001349 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001350 }
1351
Bruce Allan3af50482010-06-16 13:25:55 +00001352 /* force 1000, set loopback */
1353 e1e_wphy(hw, PHY_CONTROL, 0x4140);
1354 mdelay(250);
1355
1356 /* Now set up the MAC to the same speed/duplex as the PHY. */
1357 ctrl_reg = er32(CTRL);
1358 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1359 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1360 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1361 E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1362 E1000_CTRL_FD); /* Force Duplex to FULL */
1363
1364 if (adapter->flags & FLAG_IS_ICH)
1365 ctrl_reg |= E1000_CTRL_SLU; /* Set Link Up */
1366
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001367 if (hw->phy.media_type == e1000_media_type_copper &&
1368 hw->phy.type == e1000_phy_m88) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001369 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1370 } else {
Bruce Allanad680762008-03-28 09:15:03 -07001371 /*
1372 * Set the ILOS bit on the fiber Nic if half duplex link is
1373 * detected.
1374 */
Bruce Allan90da0662011-01-06 07:02:53 +00001375 if ((er32(STATUS) & E1000_STATUS_FD) == 0)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001376 ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1377 }
1378
1379 ew32(CTRL, ctrl_reg);
1380
Bruce Allanad680762008-03-28 09:15:03 -07001381 /*
1382 * Disable the receiver on the PHY so when a cable is plugged in, the
Auke Kokbc7f75f2007-09-17 12:30:59 -07001383 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1384 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001385 if (hw->phy.type == e1000_phy_m88)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001386 e1000_phy_disable_receiver(adapter);
1387
1388 udelay(500);
1389
1390 return 0;
1391}
1392
1393static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1394{
1395 struct e1000_hw *hw = &adapter->hw;
1396 u32 ctrl = er32(CTRL);
1397 int link = 0;
1398
1399 /* special requirements for 82571/82572 fiber adapters */
1400
Bruce Allanad680762008-03-28 09:15:03 -07001401 /*
1402 * jump through hoops to make sure link is up because serdes
1403 * link is hardwired up
1404 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001405 ctrl |= E1000_CTRL_SLU;
1406 ew32(CTRL, ctrl);
1407
1408 /* disable autoneg */
1409 ctrl = er32(TXCW);
1410 ctrl &= ~(1 << 31);
1411 ew32(TXCW, ctrl);
1412
1413 link = (er32(STATUS) & E1000_STATUS_LU);
1414
1415 if (!link) {
1416 /* set invert loss of signal */
1417 ctrl = er32(CTRL);
1418 ctrl |= E1000_CTRL_ILOS;
1419 ew32(CTRL, ctrl);
1420 }
1421
Bruce Allanad680762008-03-28 09:15:03 -07001422 /*
1423 * special write to serdes control register to enable SerDes analog
1424 * loopback
1425 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001426#define E1000_SERDES_LB_ON 0x410
1427 ew32(SCTL, E1000_SERDES_LB_ON);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001428 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001429 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001430
1431 return 0;
1432}
1433
1434/* only call this for fiber/serdes connections to es2lan */
1435static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1436{
1437 struct e1000_hw *hw = &adapter->hw;
1438 u32 ctrlext = er32(CTRL_EXT);
1439 u32 ctrl = er32(CTRL);
1440
Bruce Allanad680762008-03-28 09:15:03 -07001441 /*
1442 * save CTRL_EXT to restore later, reuse an empty variable (unused
1443 * on mac_type 80003es2lan)
1444 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001445 adapter->tx_fifo_head = ctrlext;
1446
1447 /* clear the serdes mode bits, putting the device into mac loopback */
1448 ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1449 ew32(CTRL_EXT, ctrlext);
1450
1451 /* force speed to 1000/FD, link up */
1452 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1453 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1454 E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1455 ew32(CTRL, ctrl);
1456
1457 /* set mac loopback */
1458 ctrl = er32(RCTL);
1459 ctrl |= E1000_RCTL_LBM_MAC;
1460 ew32(RCTL, ctrl);
1461
1462 /* set testing mode parameters (no need to reset later) */
1463#define KMRNCTRLSTA_OPMODE (0x1F << 16)
1464#define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1465 ew32(KMRNCTRLSTA,
Bruce Allancef8c792008-04-02 13:48:23 -07001466 (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001467
1468 return 0;
1469}
1470
1471static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1472{
1473 struct e1000_hw *hw = &adapter->hw;
1474 u32 rctl;
1475
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001476 if (hw->phy.media_type == e1000_media_type_fiber ||
1477 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001478 switch (hw->mac.type) {
1479 case e1000_80003es2lan:
1480 return e1000_set_es2lan_mac_loopback(adapter);
1481 break;
1482 case e1000_82571:
1483 case e1000_82572:
1484 return e1000_set_82571_fiber_loopback(adapter);
1485 break;
1486 default:
1487 rctl = er32(RCTL);
1488 rctl |= E1000_RCTL_LBM_TCVR;
1489 ew32(RCTL, rctl);
1490 return 0;
1491 }
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001492 } else if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001493 return e1000_integrated_phy_loopback(adapter);
1494 }
1495
1496 return 7;
1497}
1498
1499static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1500{
1501 struct e1000_hw *hw = &adapter->hw;
1502 u32 rctl;
1503 u16 phy_reg;
1504
1505 rctl = er32(RCTL);
1506 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1507 ew32(RCTL, rctl);
1508
1509 switch (hw->mac.type) {
1510 case e1000_80003es2lan:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001511 if (hw->phy.media_type == e1000_media_type_fiber ||
1512 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001513 /* restore CTRL_EXT, stealing space from tx_fifo_head */
Bruce Allanad680762008-03-28 09:15:03 -07001514 ew32(CTRL_EXT, adapter->tx_fifo_head);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001515 adapter->tx_fifo_head = 0;
1516 }
1517 /* fall through */
1518 case e1000_82571:
1519 case e1000_82572:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001520 if (hw->phy.media_type == e1000_media_type_fiber ||
1521 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001522#define E1000_SERDES_LB_OFF 0x400
1523 ew32(SCTL, E1000_SERDES_LB_OFF);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001524 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00001525 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001526 break;
1527 }
1528 /* Fall Through */
1529 default:
1530 hw->mac.autoneg = 1;
1531 if (hw->phy.type == e1000_phy_gg82563)
1532 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1533 e1e_rphy(hw, PHY_CONTROL, &phy_reg);
1534 if (phy_reg & MII_CR_LOOPBACK) {
1535 phy_reg &= ~MII_CR_LOOPBACK;
1536 e1e_wphy(hw, PHY_CONTROL, phy_reg);
1537 e1000e_commit_phy(hw);
1538 }
1539 break;
1540 }
1541}
1542
1543static void e1000_create_lbtest_frame(struct sk_buff *skb,
1544 unsigned int frame_size)
1545{
1546 memset(skb->data, 0xFF, frame_size);
1547 frame_size &= ~1;
1548 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1549 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
1550 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
1551}
1552
1553static int e1000_check_lbtest_frame(struct sk_buff *skb,
1554 unsigned int frame_size)
1555{
1556 frame_size &= ~1;
1557 if (*(skb->data + 3) == 0xFF)
1558 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1559 (*(skb->data + frame_size / 2 + 12) == 0xAF))
1560 return 0;
1561 return 13;
1562}
1563
1564static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1565{
1566 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1567 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1568 struct pci_dev *pdev = adapter->pdev;
1569 struct e1000_hw *hw = &adapter->hw;
1570 int i, j, k, l;
1571 int lc;
1572 int good_cnt;
1573 int ret_val = 0;
1574 unsigned long time;
1575
1576 ew32(RDT, rx_ring->count - 1);
1577
Bruce Allanad680762008-03-28 09:15:03 -07001578 /*
1579 * Calculate the loop count based on the largest descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001580 * The idea is to wrap the largest ring a number of times using 64
1581 * send/receive pairs during each loop
1582 */
1583
1584 if (rx_ring->count <= tx_ring->count)
1585 lc = ((tx_ring->count / 64) * 2) + 1;
1586 else
1587 lc = ((rx_ring->count / 64) * 2) + 1;
1588
1589 k = 0;
1590 l = 0;
1591 for (j = 0; j <= lc; j++) { /* loop count loop */
1592 for (i = 0; i < 64; i++) { /* send the packets */
Bruce Allancef8c792008-04-02 13:48:23 -07001593 e1000_create_lbtest_frame(tx_ring->buffer_info[k].skb,
1594 1024);
Nick Nunley0be3f552010-04-27 13:09:05 +00001595 dma_sync_single_for_device(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001596 tx_ring->buffer_info[k].dma,
1597 tx_ring->buffer_info[k].length,
Nick Nunley0be3f552010-04-27 13:09:05 +00001598 DMA_TO_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001599 k++;
1600 if (k == tx_ring->count)
1601 k = 0;
1602 }
1603 ew32(TDT, k);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001604 e1e_flush();
Auke Kokbc7f75f2007-09-17 12:30:59 -07001605 msleep(200);
1606 time = jiffies; /* set the start time for the receive */
1607 good_cnt = 0;
1608 do { /* receive the sent packets */
Nick Nunley0be3f552010-04-27 13:09:05 +00001609 dma_sync_single_for_cpu(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001610 rx_ring->buffer_info[l].dma, 2048,
Nick Nunley0be3f552010-04-27 13:09:05 +00001611 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001612
1613 ret_val = e1000_check_lbtest_frame(
1614 rx_ring->buffer_info[l].skb, 1024);
1615 if (!ret_val)
1616 good_cnt++;
1617 l++;
1618 if (l == rx_ring->count)
1619 l = 0;
Bruce Allanad680762008-03-28 09:15:03 -07001620 /*
1621 * time + 20 msecs (200 msecs on 2.4) is more than
Auke Kokbc7f75f2007-09-17 12:30:59 -07001622 * enough time to complete the receives, if it's
1623 * exceeded, break and error off
1624 */
1625 } while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1626 if (good_cnt != 64) {
1627 ret_val = 13; /* ret_val is the same as mis-compare */
1628 break;
1629 }
Bruce Allancef8c792008-04-02 13:48:23 -07001630 if (jiffies >= (time + 20)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001631 ret_val = 14; /* error code for time out error */
1632 break;
1633 }
1634 } /* end loop count loop */
1635 return ret_val;
1636}
1637
1638static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1639{
Bruce Allanad680762008-03-28 09:15:03 -07001640 /*
1641 * PHY loopback cannot be performed if SoL/IDER
1642 * sessions are active
1643 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001644 if (e1000_check_reset_block(&adapter->hw)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001645 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001646 *data = 0;
1647 goto out;
1648 }
1649
1650 *data = e1000_setup_desc_rings(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001651 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001652 goto out;
1653
1654 *data = e1000_setup_loopback_test(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001655 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001656 goto err_loopback;
1657
1658 *data = e1000_run_loopback_test(adapter);
1659 e1000_loopback_cleanup(adapter);
1660
1661err_loopback:
1662 e1000_free_desc_rings(adapter);
1663out:
1664 return *data;
1665}
1666
1667static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1668{
1669 struct e1000_hw *hw = &adapter->hw;
1670
1671 *data = 0;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001672 if (hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001673 int i = 0;
Alex Chiang612e2442009-02-05 23:55:45 -08001674 hw->mac.serdes_has_link = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001675
Bruce Allanad680762008-03-28 09:15:03 -07001676 /*
1677 * On some blade server designs, link establishment
1678 * could take as long as 2-3 minutes
1679 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001680 do {
1681 hw->mac.ops.check_for_link(hw);
1682 if (hw->mac.serdes_has_link)
1683 return *data;
1684 msleep(20);
1685 } while (i++ < 3750);
1686
1687 *data = 1;
1688 } else {
1689 hw->mac.ops.check_for_link(hw);
1690 if (hw->mac.autoneg)
Bruce Allan5661aeb2011-02-25 06:36:25 +00001691 /*
1692 * On some Phy/switch combinations, link establishment
1693 * can take a few seconds more than expected.
1694 */
1695 msleep(5000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001696
Bruce Allan5661aeb2011-02-25 06:36:25 +00001697 if (!(er32(STATUS) & E1000_STATUS_LU))
Auke Kokbc7f75f2007-09-17 12:30:59 -07001698 *data = 1;
1699 }
1700 return *data;
1701}
1702
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001703static int e1000e_get_sset_count(struct net_device *netdev, int sset)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001704{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001705 switch (sset) {
1706 case ETH_SS_TEST:
1707 return E1000_TEST_LEN;
1708 case ETH_SS_STATS:
1709 return E1000_STATS_LEN;
1710 default:
1711 return -EOPNOTSUPP;
1712 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001713}
1714
1715static void e1000_diag_test(struct net_device *netdev,
1716 struct ethtool_test *eth_test, u64 *data)
1717{
1718 struct e1000_adapter *adapter = netdev_priv(netdev);
1719 u16 autoneg_advertised;
1720 u8 forced_speed_duplex;
1721 u8 autoneg;
1722 bool if_running = netif_running(netdev);
1723
1724 set_bit(__E1000_TESTING, &adapter->state);
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001725
1726 if (!if_running) {
1727 /* Get control of and reset hardware */
1728 if (adapter->flags & FLAG_HAS_AMT)
1729 e1000e_get_hw_control(adapter);
1730
1731 e1000e_power_up_phy(adapter);
1732
1733 adapter->hw.phy.autoneg_wait_to_complete = 1;
1734 e1000e_reset(adapter);
1735 adapter->hw.phy.autoneg_wait_to_complete = 0;
1736 }
1737
Auke Kokbc7f75f2007-09-17 12:30:59 -07001738 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1739 /* Offline tests */
1740
1741 /* save speed, duplex, autoneg settings */
1742 autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1743 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1744 autoneg = adapter->hw.mac.autoneg;
1745
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001746 e_info("offline testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001747
Auke Kokbc7f75f2007-09-17 12:30:59 -07001748 if (if_running)
1749 /* indicate we're in test mode */
1750 dev_close(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001751
1752 if (e1000_reg_test(adapter, &data[0]))
1753 eth_test->flags |= ETH_TEST_FL_FAILED;
1754
1755 e1000e_reset(adapter);
1756 if (e1000_eeprom_test(adapter, &data[1]))
1757 eth_test->flags |= ETH_TEST_FL_FAILED;
1758
1759 e1000e_reset(adapter);
1760 if (e1000_intr_test(adapter, &data[2]))
1761 eth_test->flags |= ETH_TEST_FL_FAILED;
1762
1763 e1000e_reset(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001764 if (e1000_loopback_test(adapter, &data[3]))
1765 eth_test->flags |= ETH_TEST_FL_FAILED;
1766
Auke Kokbc7f75f2007-09-17 12:30:59 -07001767 /* force this routine to wait until autoneg complete/timeout */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001768 adapter->hw.phy.autoneg_wait_to_complete = 1;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001769 e1000e_reset(adapter);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001770 adapter->hw.phy.autoneg_wait_to_complete = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001771
Carolyn Wybornyc6ce3852010-10-15 17:35:31 +00001772 if (e1000_link_test(adapter, &data[4]))
1773 eth_test->flags |= ETH_TEST_FL_FAILED;
1774
1775 /* restore speed, duplex, autoneg settings */
1776 adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1777 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1778 adapter->hw.mac.autoneg = autoneg;
1779 e1000e_reset(adapter);
1780
Auke Kokbc7f75f2007-09-17 12:30:59 -07001781 clear_bit(__E1000_TESTING, &adapter->state);
1782 if (if_running)
1783 dev_open(netdev);
1784 } else {
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001785 /* Online tests */
Bruce Allan11b08be2010-05-10 14:59:31 +00001786
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001787 e_info("online testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001788
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001789 /* register, eeprom, intr and loopback tests not run online */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001790 data[0] = 0;
1791 data[1] = 0;
1792 data[2] = 0;
1793 data[3] = 0;
1794
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001795 if (e1000_link_test(adapter, &data[4]))
1796 eth_test->flags |= ETH_TEST_FL_FAILED;
Bruce Allan11b08be2010-05-10 14:59:31 +00001797
Auke Kokbc7f75f2007-09-17 12:30:59 -07001798 clear_bit(__E1000_TESTING, &adapter->state);
1799 }
Bruce Allan31dbe5b2011-01-06 14:29:52 +00001800
1801 if (!if_running) {
1802 e1000e_reset(adapter);
1803
1804 if (adapter->flags & FLAG_HAS_AMT)
1805 e1000e_release_hw_control(adapter);
1806 }
1807
Auke Kokbc7f75f2007-09-17 12:30:59 -07001808 msleep_interruptible(4 * 1000);
1809}
1810
1811static void e1000_get_wol(struct net_device *netdev,
1812 struct ethtool_wolinfo *wol)
1813{
1814 struct e1000_adapter *adapter = netdev_priv(netdev);
1815
1816 wol->supported = 0;
1817 wol->wolopts = 0;
1818
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001819 if (!(adapter->flags & FLAG_HAS_WOL) ||
1820 !device_can_wakeup(&adapter->pdev->dev))
Auke Kokbc7f75f2007-09-17 12:30:59 -07001821 return;
1822
1823 wol->supported = WAKE_UCAST | WAKE_MCAST |
Bruce Allan4a29e152011-03-04 09:07:01 +00001824 WAKE_BCAST | WAKE_MAGIC | WAKE_PHY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001825
1826 /* apply any specific unsupported masks here */
1827 if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1828 wol->supported &= ~WAKE_UCAST;
1829
1830 if (adapter->wol & E1000_WUFC_EX)
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001831 e_err("Interface does not support directed (unicast) "
1832 "frame wake-up packets\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001833 }
1834
1835 if (adapter->wol & E1000_WUFC_EX)
1836 wol->wolopts |= WAKE_UCAST;
1837 if (adapter->wol & E1000_WUFC_MC)
1838 wol->wolopts |= WAKE_MCAST;
1839 if (adapter->wol & E1000_WUFC_BC)
1840 wol->wolopts |= WAKE_BCAST;
1841 if (adapter->wol & E1000_WUFC_MAG)
1842 wol->wolopts |= WAKE_MAGIC;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001843 if (adapter->wol & E1000_WUFC_LNKC)
1844 wol->wolopts |= WAKE_PHY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001845}
1846
Bruce Allan4a29e152011-03-04 09:07:01 +00001847static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001848{
1849 struct e1000_adapter *adapter = netdev_priv(netdev);
1850
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001851 if (!(adapter->flags & FLAG_HAS_WOL) ||
Bruce Allan1fbfca32009-11-20 23:22:01 +00001852 !device_can_wakeup(&adapter->pdev->dev) ||
1853 (wol->wolopts & ~(WAKE_UCAST | WAKE_MCAST | WAKE_BCAST |
Bruce Allan4a29e152011-03-04 09:07:01 +00001854 WAKE_MAGIC | WAKE_PHY)))
Bruce Allan1fbfca32009-11-20 23:22:01 +00001855 return -EOPNOTSUPP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001856
1857 /* these settings will always override what we currently have */
1858 adapter->wol = 0;
1859
1860 if (wol->wolopts & WAKE_UCAST)
1861 adapter->wol |= E1000_WUFC_EX;
1862 if (wol->wolopts & WAKE_MCAST)
1863 adapter->wol |= E1000_WUFC_MC;
1864 if (wol->wolopts & WAKE_BCAST)
1865 adapter->wol |= E1000_WUFC_BC;
1866 if (wol->wolopts & WAKE_MAGIC)
1867 adapter->wol |= E1000_WUFC_MAG;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001868 if (wol->wolopts & WAKE_PHY)
1869 adapter->wol |= E1000_WUFC_LNKC;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001870
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00001871 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1872
Auke Kokbc7f75f2007-09-17 12:30:59 -07001873 return 0;
1874}
1875
Bruce Allandbf80dc2011-04-16 00:34:40 +00001876static int e1000_set_phys_id(struct net_device *netdev,
1877 enum ethtool_phys_id_state state)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001878{
1879 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan4662e822008-08-26 18:37:06 -07001880 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001881
Bruce Allandbf80dc2011-04-16 00:34:40 +00001882 switch (state) {
1883 case ETHTOOL_ID_ACTIVE:
1884 if (!hw->mac.ops.blink_led)
1885 return 2; /* cycle on/off twice per second */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001886
Bruce Allandbf80dc2011-04-16 00:34:40 +00001887 hw->mac.ops.blink_led(hw);
1888 break;
1889
1890 case ETHTOOL_ID_INACTIVE:
Bruce Allan4662e822008-08-26 18:37:06 -07001891 if (hw->phy.type == e1000_phy_ife)
1892 e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
Bruce Allandbf80dc2011-04-16 00:34:40 +00001893 hw->mac.ops.led_off(hw);
1894 hw->mac.ops.cleanup_led(hw);
1895 break;
1896
1897 case ETHTOOL_ID_ON:
1898 adapter->hw.mac.ops.led_on(&adapter->hw);
1899 break;
1900
1901 case ETHTOOL_ID_OFF:
1902 adapter->hw.mac.ops.led_off(&adapter->hw);
1903 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001904 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001905 return 0;
1906}
1907
Auke Kokde5b3072008-04-23 11:09:08 -07001908static int e1000_get_coalesce(struct net_device *netdev,
1909 struct ethtool_coalesce *ec)
1910{
1911 struct e1000_adapter *adapter = netdev_priv(netdev);
1912
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001913 if (adapter->itr_setting <= 4)
Auke Kokde5b3072008-04-23 11:09:08 -07001914 ec->rx_coalesce_usecs = adapter->itr_setting;
1915 else
1916 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1917
1918 return 0;
1919}
1920
1921static int e1000_set_coalesce(struct net_device *netdev,
1922 struct ethtool_coalesce *ec)
1923{
1924 struct e1000_adapter *adapter = netdev_priv(netdev);
1925 struct e1000_hw *hw = &adapter->hw;
1926
1927 if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001928 ((ec->rx_coalesce_usecs > 4) &&
Auke Kokde5b3072008-04-23 11:09:08 -07001929 (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1930 (ec->rx_coalesce_usecs == 2))
1931 return -EINVAL;
1932
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00001933 if (ec->rx_coalesce_usecs == 4) {
1934 adapter->itr = adapter->itr_setting = 4;
1935 } else if (ec->rx_coalesce_usecs <= 3) {
Auke Kokde5b3072008-04-23 11:09:08 -07001936 adapter->itr = 20000;
1937 adapter->itr_setting = ec->rx_coalesce_usecs;
1938 } else {
1939 adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1940 adapter->itr_setting = adapter->itr & ~3;
1941 }
1942
1943 if (adapter->itr_setting != 0)
1944 ew32(ITR, 1000000000 / (adapter->itr * 256));
1945 else
1946 ew32(ITR, 0);
1947
1948 return 0;
1949}
1950
Auke Kokbc7f75f2007-09-17 12:30:59 -07001951static int e1000_nway_reset(struct net_device *netdev)
1952{
1953 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan5962bc22011-01-20 06:58:07 +00001954
1955 if (!netif_running(netdev))
1956 return -EAGAIN;
1957
1958 if (!adapter->hw.mac.autoneg)
1959 return -EINVAL;
1960
1961 e1000e_reinit_locked(adapter);
1962
Auke Kokbc7f75f2007-09-17 12:30:59 -07001963 return 0;
1964}
1965
Auke Kokbc7f75f2007-09-17 12:30:59 -07001966static void e1000_get_ethtool_stats(struct net_device *netdev,
1967 struct ethtool_stats *stats,
1968 u64 *data)
1969{
1970 struct e1000_adapter *adapter = netdev_priv(netdev);
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001971 struct rtnl_link_stats64 net_stats;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001972 int i;
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001973 char *p = NULL;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001974
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001975 e1000e_get_stats64(netdev, &net_stats);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001976 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001977 switch (e1000_gstrings_stats[i].type) {
1978 case NETDEV_STATS:
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00001979 p = (char *) &net_stats +
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001980 e1000_gstrings_stats[i].stat_offset;
1981 break;
1982 case E1000_STATS:
1983 p = (char *) adapter +
1984 e1000_gstrings_stats[i].stat_offset;
1985 break;
Bruce Allan61c75812010-12-09 23:04:25 +00001986 default:
1987 data[i] = 0;
1988 continue;
Ajit Khapardee0f36a92009-10-13 01:45:09 +00001989 }
1990
Auke Kokbc7f75f2007-09-17 12:30:59 -07001991 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
1992 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1993 }
1994}
1995
1996static void e1000_get_strings(struct net_device *netdev, u32 stringset,
1997 u8 *data)
1998{
1999 u8 *p = data;
2000 int i;
2001
2002 switch (stringset) {
2003 case ETH_SS_TEST:
Bruce Allan5c1bda02011-01-19 04:23:39 +00002004 memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
Auke Kokbc7f75f2007-09-17 12:30:59 -07002005 break;
2006 case ETH_SS_STATS:
2007 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
2008 memcpy(p, e1000_gstrings_stats[i].stat_string,
2009 ETH_GSTRING_LEN);
2010 p += ETH_GSTRING_LEN;
2011 }
2012 break;
2013 }
2014}
2015
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002016static int e1000e_set_flags(struct net_device *netdev, u32 data)
2017{
2018 struct e1000_adapter *adapter = netdev_priv(netdev);
2019 bool need_reset = false;
2020 int rc;
2021
2022 need_reset = (data & ETH_FLAG_RXVLAN) !=
2023 (netdev->features & NETIF_F_HW_VLAN_RX);
2024
2025 rc = ethtool_op_set_flags(netdev, data, ETH_FLAG_RXVLAN |
2026 ETH_FLAG_TXVLAN);
2027
2028 if (rc)
2029 return rc;
2030
2031 if (need_reset) {
2032 if (netif_running(netdev))
2033 e1000e_reinit_locked(adapter);
2034 else
2035 e1000e_reset(adapter);
2036 }
2037
2038 return 0;
2039}
2040
Auke Kokbc7f75f2007-09-17 12:30:59 -07002041static const struct ethtool_ops e1000_ethtool_ops = {
2042 .get_settings = e1000_get_settings,
2043 .set_settings = e1000_set_settings,
2044 .get_drvinfo = e1000_get_drvinfo,
2045 .get_regs_len = e1000_get_regs_len,
2046 .get_regs = e1000_get_regs,
2047 .get_wol = e1000_get_wol,
2048 .set_wol = e1000_set_wol,
2049 .get_msglevel = e1000_get_msglevel,
2050 .set_msglevel = e1000_set_msglevel,
2051 .nway_reset = e1000_nway_reset,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +00002052 .get_link = ethtool_op_get_link,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002053 .get_eeprom_len = e1000_get_eeprom_len,
2054 .get_eeprom = e1000_get_eeprom,
2055 .set_eeprom = e1000_set_eeprom,
2056 .get_ringparam = e1000_get_ringparam,
2057 .set_ringparam = e1000_set_ringparam,
2058 .get_pauseparam = e1000_get_pauseparam,
2059 .set_pauseparam = e1000_set_pauseparam,
2060 .get_rx_csum = e1000_get_rx_csum,
2061 .set_rx_csum = e1000_set_rx_csum,
2062 .get_tx_csum = e1000_get_tx_csum,
2063 .set_tx_csum = e1000_set_tx_csum,
2064 .get_sg = ethtool_op_get_sg,
2065 .set_sg = ethtool_op_set_sg,
2066 .get_tso = ethtool_op_get_tso,
2067 .set_tso = e1000_set_tso,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002068 .self_test = e1000_diag_test,
2069 .get_strings = e1000_get_strings,
Bruce Allandbf80dc2011-04-16 00:34:40 +00002070 .set_phys_id = e1000_set_phys_id,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002071 .get_ethtool_stats = e1000_get_ethtool_stats,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002072 .get_sset_count = e1000e_get_sset_count,
Auke Kokde5b3072008-04-23 11:09:08 -07002073 .get_coalesce = e1000_get_coalesce,
2074 .set_coalesce = e1000_set_coalesce,
Bruce Allane7d906f2009-11-20 23:22:57 +00002075 .get_flags = ethtool_op_get_flags,
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002076 .set_flags = e1000e_set_flags,
Auke Kokbc7f75f2007-09-17 12:30:59 -07002077};
2078
2079void e1000e_set_ethtool_ops(struct net_device *netdev)
2080{
2081 SET_ETHTOOL_OPS(netdev, &e1000_ethtool_ops);
2082}