blob: 5ed8e13d5fed0bd9c3ee6cc93a9d263f42ac591b [file] [log] [blame]
Auke Kokbc7f75f2007-09-17 12:30:59 -07001/*******************************************************************************
2
3 Intel PRO/1000 Linux driver
Bruce Allanad680762008-03-28 09:15:03 -07004 Copyright(c) 1999 - 2008 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>
32#include <linux/ethtool.h>
33#include <linux/pci.h>
34#include <linux/delay.h>
35
36#include "e1000.h"
37
38struct e1000_stats {
39 char stat_string[ETH_GSTRING_LEN];
40 int sizeof_stat;
41 int stat_offset;
42};
43
44#define E1000_STAT(m) sizeof(((struct e1000_adapter *)0)->m), \
45 offsetof(struct e1000_adapter, m)
46static const struct e1000_stats e1000_gstrings_stats[] = {
47 { "rx_packets", E1000_STAT(stats.gprc) },
48 { "tx_packets", E1000_STAT(stats.gptc) },
Bruce Allan7c257692008-04-23 11:09:00 -070049 { "rx_bytes", E1000_STAT(stats.gorc) },
50 { "tx_bytes", E1000_STAT(stats.gotc) },
Auke Kokbc7f75f2007-09-17 12:30:59 -070051 { "rx_broadcast", E1000_STAT(stats.bprc) },
52 { "tx_broadcast", E1000_STAT(stats.bptc) },
53 { "rx_multicast", E1000_STAT(stats.mprc) },
54 { "tx_multicast", E1000_STAT(stats.mptc) },
55 { "rx_errors", E1000_STAT(net_stats.rx_errors) },
56 { "tx_errors", E1000_STAT(net_stats.tx_errors) },
57 { "tx_dropped", E1000_STAT(net_stats.tx_dropped) },
58 { "multicast", E1000_STAT(stats.mprc) },
59 { "collisions", E1000_STAT(stats.colc) },
60 { "rx_length_errors", E1000_STAT(net_stats.rx_length_errors) },
61 { "rx_over_errors", E1000_STAT(net_stats.rx_over_errors) },
62 { "rx_crc_errors", E1000_STAT(stats.crcerrs) },
63 { "rx_frame_errors", E1000_STAT(net_stats.rx_frame_errors) },
64 { "rx_no_buffer_count", E1000_STAT(stats.rnbc) },
65 { "rx_missed_errors", E1000_STAT(stats.mpc) },
66 { "tx_aborted_errors", E1000_STAT(stats.ecol) },
67 { "tx_carrier_errors", E1000_STAT(stats.tncrs) },
68 { "tx_fifo_errors", E1000_STAT(net_stats.tx_fifo_errors) },
69 { "tx_heartbeat_errors", E1000_STAT(net_stats.tx_heartbeat_errors) },
70 { "tx_window_errors", E1000_STAT(stats.latecol) },
71 { "tx_abort_late_coll", E1000_STAT(stats.latecol) },
72 { "tx_deferred_ok", E1000_STAT(stats.dc) },
73 { "tx_single_coll_ok", E1000_STAT(stats.scc) },
74 { "tx_multi_coll_ok", E1000_STAT(stats.mcc) },
75 { "tx_timeout_count", E1000_STAT(tx_timeout_count) },
76 { "tx_restart_queue", E1000_STAT(restart_queue) },
77 { "rx_long_length_errors", E1000_STAT(stats.roc) },
78 { "rx_short_length_errors", E1000_STAT(stats.ruc) },
79 { "rx_align_errors", E1000_STAT(stats.algnerrc) },
80 { "tx_tcp_seg_good", E1000_STAT(stats.tsctc) },
81 { "tx_tcp_seg_failed", E1000_STAT(stats.tsctfc) },
82 { "rx_flow_control_xon", E1000_STAT(stats.xonrxc) },
83 { "rx_flow_control_xoff", E1000_STAT(stats.xoffrxc) },
84 { "tx_flow_control_xon", E1000_STAT(stats.xontxc) },
85 { "tx_flow_control_xoff", E1000_STAT(stats.xofftxc) },
Bruce Allan7c257692008-04-23 11:09:00 -070086 { "rx_long_byte_count", E1000_STAT(stats.gorc) },
Auke Kokbc7f75f2007-09-17 12:30:59 -070087 { "rx_csum_offload_good", E1000_STAT(hw_csum_good) },
88 { "rx_csum_offload_errors", E1000_STAT(hw_csum_err) },
89 { "rx_header_split", E1000_STAT(rx_hdr_split) },
90 { "alloc_rx_buff_failed", E1000_STAT(alloc_rx_buff_failed) },
91 { "tx_smbus", E1000_STAT(stats.mgptc) },
92 { "rx_smbus", E1000_STAT(stats.mgprc) },
93 { "dropped_smbus", E1000_STAT(stats.mgpdc) },
94 { "rx_dma_failed", E1000_STAT(rx_dma_failed) },
95 { "tx_dma_failed", E1000_STAT(tx_dma_failed) },
96};
97
Alejandro Martinez Ruizc00acf42007-10-18 10:16:33 +020098#define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
Auke Kokbc7f75f2007-09-17 12:30:59 -070099#define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN)
100static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
101 "Register test (offline)", "Eeprom test (offline)",
102 "Interrupt test (offline)", "Loopback test (offline)",
103 "Link test (on/offline)"
104};
Bruce Allanad680762008-03-28 09:15:03 -0700105#define E1000_TEST_LEN ARRAY_SIZE(e1000_gstrings_test)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700106
107static int e1000_get_settings(struct net_device *netdev,
108 struct ethtool_cmd *ecmd)
109{
110 struct e1000_adapter *adapter = netdev_priv(netdev);
111 struct e1000_hw *hw = &adapter->hw;
Auke Kok369d7422007-10-15 14:30:59 -0700112 u32 status;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700113
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700114 if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700115
116 ecmd->supported = (SUPPORTED_10baseT_Half |
117 SUPPORTED_10baseT_Full |
118 SUPPORTED_100baseT_Half |
119 SUPPORTED_100baseT_Full |
120 SUPPORTED_1000baseT_Full |
121 SUPPORTED_Autoneg |
122 SUPPORTED_TP);
123 if (hw->phy.type == e1000_phy_ife)
124 ecmd->supported &= ~SUPPORTED_1000baseT_Full;
125 ecmd->advertising = ADVERTISED_TP;
126
127 if (hw->mac.autoneg == 1) {
128 ecmd->advertising |= ADVERTISED_Autoneg;
129 /* the e1000 autoneg seems to match ethtool nicely */
130 ecmd->advertising |= hw->phy.autoneg_advertised;
131 }
132
133 ecmd->port = PORT_TP;
134 ecmd->phy_address = hw->phy.addr;
135 ecmd->transceiver = XCVR_INTERNAL;
136
137 } else {
138 ecmd->supported = (SUPPORTED_1000baseT_Full |
139 SUPPORTED_FIBRE |
140 SUPPORTED_Autoneg);
141
142 ecmd->advertising = (ADVERTISED_1000baseT_Full |
143 ADVERTISED_FIBRE |
144 ADVERTISED_Autoneg);
145
146 ecmd->port = PORT_FIBRE;
147 ecmd->transceiver = XCVR_EXTERNAL;
148 }
149
Auke Kok369d7422007-10-15 14:30:59 -0700150 status = er32(STATUS);
151 if (status & E1000_STATUS_LU) {
152 if (status & E1000_STATUS_SPEED_1000)
153 ecmd->speed = 1000;
154 else if (status & E1000_STATUS_SPEED_100)
155 ecmd->speed = 100;
156 else
157 ecmd->speed = 10;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700158
Auke Kok369d7422007-10-15 14:30:59 -0700159 if (status & E1000_STATUS_FD)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700160 ecmd->duplex = DUPLEX_FULL;
161 else
162 ecmd->duplex = DUPLEX_HALF;
163 } else {
164 ecmd->speed = -1;
165 ecmd->duplex = -1;
166 }
167
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700168 ecmd->autoneg = ((hw->phy.media_type == e1000_media_type_fiber) ||
Auke Kokbc7f75f2007-09-17 12:30:59 -0700169 hw->mac.autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
170 return 0;
171}
172
Auke Kok369d7422007-10-15 14:30:59 -0700173static u32 e1000_get_link(struct net_device *netdev)
174{
175 struct e1000_adapter *adapter = netdev_priv(netdev);
176 struct e1000_hw *hw = &adapter->hw;
177 u32 status;
178
179 status = er32(STATUS);
Bruce Allan56e1f822008-08-08 18:35:44 -0700180 return (status & E1000_STATUS_LU) ? 1 : 0;
Auke Kok369d7422007-10-15 14:30:59 -0700181}
182
Auke Kokbc7f75f2007-09-17 12:30:59 -0700183static int e1000_set_spd_dplx(struct e1000_adapter *adapter, u16 spddplx)
184{
185 struct e1000_mac_info *mac = &adapter->hw.mac;
186
187 mac->autoneg = 0;
188
189 /* Fiber NICs only allow 1000 gbps Full duplex */
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700190 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
Auke Kokbc7f75f2007-09-17 12:30:59 -0700191 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700192 e_err("Unsupported Speed/Duplex configuration\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700193 return -EINVAL;
194 }
195
196 switch (spddplx) {
197 case SPEED_10 + DUPLEX_HALF:
198 mac->forced_speed_duplex = ADVERTISE_10_HALF;
199 break;
200 case SPEED_10 + DUPLEX_FULL:
201 mac->forced_speed_duplex = ADVERTISE_10_FULL;
202 break;
203 case SPEED_100 + DUPLEX_HALF:
204 mac->forced_speed_duplex = ADVERTISE_100_HALF;
205 break;
206 case SPEED_100 + DUPLEX_FULL:
207 mac->forced_speed_duplex = ADVERTISE_100_FULL;
208 break;
209 case SPEED_1000 + DUPLEX_FULL:
210 mac->autoneg = 1;
211 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
212 break;
213 case SPEED_1000 + DUPLEX_HALF: /* not supported */
214 default:
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700215 e_err("Unsupported Speed/Duplex configuration\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700216 return -EINVAL;
217 }
218 return 0;
219}
220
221static int e1000_set_settings(struct net_device *netdev,
222 struct ethtool_cmd *ecmd)
223{
224 struct e1000_adapter *adapter = netdev_priv(netdev);
225 struct e1000_hw *hw = &adapter->hw;
226
Bruce Allanad680762008-03-28 09:15:03 -0700227 /*
228 * When SoL/IDER sessions are active, autoneg/speed/duplex
229 * cannot be changed
230 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700231 if (e1000_check_reset_block(hw)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700232 e_err("Cannot change link characteristics when SoL/IDER is "
233 "active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700234 return -EINVAL;
235 }
236
237 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
238 msleep(1);
239
240 if (ecmd->autoneg == AUTONEG_ENABLE) {
241 hw->mac.autoneg = 1;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700242 if (hw->phy.media_type == e1000_media_type_fiber)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700243 hw->phy.autoneg_advertised = ADVERTISED_1000baseT_Full |
244 ADVERTISED_FIBRE |
245 ADVERTISED_Autoneg;
246 else
247 hw->phy.autoneg_advertised = ecmd->advertising |
248 ADVERTISED_TP |
249 ADVERTISED_Autoneg;
250 ecmd->advertising = hw->phy.autoneg_advertised;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700251 if (adapter->fc_autoneg)
252 hw->fc.original_type = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700253 } else {
254 if (e1000_set_spd_dplx(adapter, ecmd->speed + ecmd->duplex)) {
255 clear_bit(__E1000_RESETTING, &adapter->state);
256 return -EINVAL;
257 }
258 }
259
260 /* reset the link */
261
262 if (netif_running(adapter->netdev)) {
263 e1000e_down(adapter);
264 e1000e_up(adapter);
265 } else {
266 e1000e_reset(adapter);
267 }
268
269 clear_bit(__E1000_RESETTING, &adapter->state);
270 return 0;
271}
272
273static void e1000_get_pauseparam(struct net_device *netdev,
274 struct ethtool_pauseparam *pause)
275{
276 struct e1000_adapter *adapter = netdev_priv(netdev);
277 struct e1000_hw *hw = &adapter->hw;
278
279 pause->autoneg =
280 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
281
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700282 if (hw->fc.type == e1000_fc_rx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700283 pause->rx_pause = 1;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700284 } else if (hw->fc.type == e1000_fc_tx_pause) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700285 pause->tx_pause = 1;
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700286 } else if (hw->fc.type == e1000_fc_full) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700287 pause->rx_pause = 1;
288 pause->tx_pause = 1;
289 }
290}
291
292static int e1000_set_pauseparam(struct net_device *netdev,
293 struct ethtool_pauseparam *pause)
294{
295 struct e1000_adapter *adapter = netdev_priv(netdev);
296 struct e1000_hw *hw = &adapter->hw;
297 int retval = 0;
298
299 adapter->fc_autoneg = pause->autoneg;
300
301 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
302 msleep(1);
303
304 if (pause->rx_pause && pause->tx_pause)
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700305 hw->fc.type = e1000_fc_full;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700306 else if (pause->rx_pause && !pause->tx_pause)
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700307 hw->fc.type = e1000_fc_rx_pause;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700308 else if (!pause->rx_pause && pause->tx_pause)
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700309 hw->fc.type = e1000_fc_tx_pause;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700310 else if (!pause->rx_pause && !pause->tx_pause)
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700311 hw->fc.type = e1000_fc_none;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700312
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700313 hw->fc.original_type = hw->fc.type;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700314
315 if (adapter->fc_autoneg == AUTONEG_ENABLE) {
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700316 hw->fc.type = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700317 if (netif_running(adapter->netdev)) {
318 e1000e_down(adapter);
319 e1000e_up(adapter);
320 } else {
321 e1000e_reset(adapter);
322 }
323 } else {
Jeff Kirsher318a94d2008-03-28 09:15:16 -0700324 retval = ((hw->phy.media_type == e1000_media_type_fiber) ?
Auke Kokbc7f75f2007-09-17 12:30:59 -0700325 hw->mac.ops.setup_link(hw) : e1000e_force_mac_fc(hw));
326 }
327
328 clear_bit(__E1000_RESETTING, &adapter->state);
329 return retval;
330}
331
332static u32 e1000_get_rx_csum(struct net_device *netdev)
333{
334 struct e1000_adapter *adapter = netdev_priv(netdev);
335 return (adapter->flags & FLAG_RX_CSUM_ENABLED);
336}
337
338static int e1000_set_rx_csum(struct net_device *netdev, u32 data)
339{
340 struct e1000_adapter *adapter = netdev_priv(netdev);
341
342 if (data)
343 adapter->flags |= FLAG_RX_CSUM_ENABLED;
344 else
345 adapter->flags &= ~FLAG_RX_CSUM_ENABLED;
346
347 if (netif_running(netdev))
348 e1000e_reinit_locked(adapter);
349 else
350 e1000e_reset(adapter);
351 return 0;
352}
353
354static u32 e1000_get_tx_csum(struct net_device *netdev)
355{
356 return ((netdev->features & NETIF_F_HW_CSUM) != 0);
357}
358
359static int e1000_set_tx_csum(struct net_device *netdev, u32 data)
360{
361 if (data)
362 netdev->features |= NETIF_F_HW_CSUM;
363 else
364 netdev->features &= ~NETIF_F_HW_CSUM;
365
366 return 0;
367}
368
369static int e1000_set_tso(struct net_device *netdev, u32 data)
370{
371 struct e1000_adapter *adapter = netdev_priv(netdev);
372
373 if (data) {
374 netdev->features |= NETIF_F_TSO;
375 netdev->features |= NETIF_F_TSO6;
376 } else {
377 netdev->features &= ~NETIF_F_TSO;
378 netdev->features &= ~NETIF_F_TSO6;
379 }
380
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700381 e_info("TSO is %s\n", data ? "Enabled" : "Disabled");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700382 adapter->flags |= FLAG_TSO_FORCE;
383 return 0;
384}
385
386static u32 e1000_get_msglevel(struct net_device *netdev)
387{
388 struct e1000_adapter *adapter = netdev_priv(netdev);
389 return adapter->msg_enable;
390}
391
392static void e1000_set_msglevel(struct net_device *netdev, u32 data)
393{
394 struct e1000_adapter *adapter = netdev_priv(netdev);
395 adapter->msg_enable = data;
396}
397
398static int e1000_get_regs_len(struct net_device *netdev)
399{
400#define E1000_REGS_LEN 32 /* overestimate */
401 return E1000_REGS_LEN * sizeof(u32);
402}
403
404static void e1000_get_regs(struct net_device *netdev,
405 struct ethtool_regs *regs, void *p)
406{
407 struct e1000_adapter *adapter = netdev_priv(netdev);
408 struct e1000_hw *hw = &adapter->hw;
409 u32 *regs_buff = p;
410 u16 phy_data;
411 u8 revision_id;
412
413 memset(p, 0, E1000_REGS_LEN * sizeof(u32));
414
415 pci_read_config_byte(adapter->pdev, PCI_REVISION_ID, &revision_id);
416
417 regs->version = (1 << 24) | (revision_id << 16) | adapter->pdev->device;
418
419 regs_buff[0] = er32(CTRL);
420 regs_buff[1] = er32(STATUS);
421
422 regs_buff[2] = er32(RCTL);
423 regs_buff[3] = er32(RDLEN);
424 regs_buff[4] = er32(RDH);
425 regs_buff[5] = er32(RDT);
426 regs_buff[6] = er32(RDTR);
427
428 regs_buff[7] = er32(TCTL);
429 regs_buff[8] = er32(TDLEN);
430 regs_buff[9] = er32(TDH);
431 regs_buff[10] = er32(TDT);
432 regs_buff[11] = er32(TIDV);
433
434 regs_buff[12] = adapter->hw.phy.type; /* PHY type (IGP=1, M88=0) */
435 if (hw->phy.type == e1000_phy_m88) {
436 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
437 regs_buff[13] = (u32)phy_data; /* cable length */
438 regs_buff[14] = 0; /* Dummy (to align w/ IGP phy reg dump) */
439 regs_buff[15] = 0; /* Dummy (to align w/ IGP phy reg dump) */
440 regs_buff[16] = 0; /* Dummy (to align w/ IGP phy reg dump) */
441 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
442 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
443 regs_buff[18] = regs_buff[13]; /* cable polarity */
444 regs_buff[19] = 0; /* Dummy (to align w/ IGP phy reg dump) */
445 regs_buff[20] = regs_buff[17]; /* polarity correction */
446 /* phy receive errors */
447 regs_buff[22] = adapter->phy_stats.receive_errors;
448 regs_buff[23] = regs_buff[13]; /* mdix mode */
449 }
450 regs_buff[21] = adapter->phy_stats.idle_errors; /* phy idle errors */
451 e1e_rphy(hw, PHY_1000T_STATUS, &phy_data);
452 regs_buff[24] = (u32)phy_data; /* phy local receiver status */
453 regs_buff[25] = regs_buff[24]; /* phy remote receiver status */
454}
455
456static int e1000_get_eeprom_len(struct net_device *netdev)
457{
458 struct e1000_adapter *adapter = netdev_priv(netdev);
459 return adapter->hw.nvm.word_size * 2;
460}
461
462static int e1000_get_eeprom(struct net_device *netdev,
463 struct ethtool_eeprom *eeprom, u8 *bytes)
464{
465 struct e1000_adapter *adapter = netdev_priv(netdev);
466 struct e1000_hw *hw = &adapter->hw;
467 u16 *eeprom_buff;
468 int first_word;
469 int last_word;
470 int ret_val = 0;
471 u16 i;
472
473 if (eeprom->len == 0)
474 return -EINVAL;
475
476 eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16);
477
478 first_word = eeprom->offset >> 1;
479 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
480
481 eeprom_buff = kmalloc(sizeof(u16) *
482 (last_word - first_word + 1), GFP_KERNEL);
483 if (!eeprom_buff)
484 return -ENOMEM;
485
486 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
487 ret_val = e1000_read_nvm(hw, first_word,
488 last_word - first_word + 1,
489 eeprom_buff);
490 } else {
491 for (i = 0; i < last_word - first_word + 1; i++) {
492 ret_val = e1000_read_nvm(hw, first_word + i, 1,
493 &eeprom_buff[i]);
Kok, Aukef227ec32008-04-29 11:18:55 -0700494 if (ret_val) {
495 /* a read error occurred, throw away the
496 * result */
497 memset(eeprom_buff, 0xff, sizeof(eeprom_buff));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700498 break;
Kok, Aukef227ec32008-04-29 11:18:55 -0700499 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700500 }
501 }
502
503 /* Device's eeprom is always little-endian, word addressable */
504 for (i = 0; i < last_word - first_word + 1; i++)
505 le16_to_cpus(&eeprom_buff[i]);
506
507 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
508 kfree(eeprom_buff);
509
510 return ret_val;
511}
512
513static int e1000_set_eeprom(struct net_device *netdev,
514 struct ethtool_eeprom *eeprom, u8 *bytes)
515{
516 struct e1000_adapter *adapter = netdev_priv(netdev);
517 struct e1000_hw *hw = &adapter->hw;
518 u16 *eeprom_buff;
519 void *ptr;
520 int max_len;
521 int first_word;
522 int last_word;
523 int ret_val = 0;
524 u16 i;
525
526 if (eeprom->len == 0)
527 return -EOPNOTSUPP;
528
529 if (eeprom->magic != (adapter->pdev->vendor | (adapter->pdev->device << 16)))
530 return -EFAULT;
531
Bruce Allan4a770352008-10-01 17:18:35 -0700532 if (adapter->flags & FLAG_READ_ONLY_NVM)
533 return -EINVAL;
534
Auke Kokbc7f75f2007-09-17 12:30:59 -0700535 max_len = hw->nvm.word_size * 2;
536
537 first_word = eeprom->offset >> 1;
538 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
539 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
540 if (!eeprom_buff)
541 return -ENOMEM;
542
543 ptr = (void *)eeprom_buff;
544
545 if (eeprom->offset & 1) {
546 /* need read/modify/write of first changed EEPROM word */
547 /* only the second byte of the word is being modified */
548 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
549 ptr++;
550 }
551 if (((eeprom->offset + eeprom->len) & 1) && (ret_val == 0))
552 /* need read/modify/write of last changed EEPROM word */
553 /* only the first byte of the word is being modified */
554 ret_val = e1000_read_nvm(hw, last_word, 1,
555 &eeprom_buff[last_word - first_word]);
556
557 /* Device's eeprom is always little-endian, word addressable */
558 for (i = 0; i < last_word - first_word + 1; i++)
559 le16_to_cpus(&eeprom_buff[i]);
560
561 memcpy(ptr, bytes, eeprom->len);
562
563 for (i = 0; i < last_word - first_word + 1; i++)
564 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
565
566 ret_val = e1000_write_nvm(hw, first_word,
567 last_word - first_word + 1, eeprom_buff);
568
Bruce Allanad680762008-03-28 09:15:03 -0700569 /*
570 * Update the checksum over the first part of the EEPROM if needed
571 * and flush shadow RAM for 82573 controllers
572 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700573 if ((ret_val == 0) && ((first_word <= NVM_CHECKSUM_REG) ||
574 (hw->mac.type == e1000_82573)))
575 e1000e_update_nvm_checksum(hw);
576
577 kfree(eeprom_buff);
578 return ret_val;
579}
580
581static void e1000_get_drvinfo(struct net_device *netdev,
582 struct ethtool_drvinfo *drvinfo)
583{
584 struct e1000_adapter *adapter = netdev_priv(netdev);
585 char firmware_version[32];
586 u16 eeprom_data;
587
588 strncpy(drvinfo->driver, e1000e_driver_name, 32);
589 strncpy(drvinfo->version, e1000e_driver_version, 32);
590
Bruce Allanad680762008-03-28 09:15:03 -0700591 /*
592 * EEPROM image version # is reported as firmware version # for
593 * PCI-E controllers
594 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700595 e1000_read_nvm(&adapter->hw, 5, 1, &eeprom_data);
596 sprintf(firmware_version, "%d.%d-%d",
597 (eeprom_data & 0xF000) >> 12,
598 (eeprom_data & 0x0FF0) >> 4,
599 eeprom_data & 0x000F);
600
601 strncpy(drvinfo->fw_version, firmware_version, 32);
602 strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700603 drvinfo->regdump_len = e1000_get_regs_len(netdev);
604 drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
605}
606
607static void e1000_get_ringparam(struct net_device *netdev,
608 struct ethtool_ringparam *ring)
609{
610 struct e1000_adapter *adapter = netdev_priv(netdev);
611 struct e1000_ring *tx_ring = adapter->tx_ring;
612 struct e1000_ring *rx_ring = adapter->rx_ring;
613
614 ring->rx_max_pending = E1000_MAX_RXD;
615 ring->tx_max_pending = E1000_MAX_TXD;
616 ring->rx_mini_max_pending = 0;
617 ring->rx_jumbo_max_pending = 0;
618 ring->rx_pending = rx_ring->count;
619 ring->tx_pending = tx_ring->count;
620 ring->rx_mini_pending = 0;
621 ring->rx_jumbo_pending = 0;
622}
623
624static int e1000_set_ringparam(struct net_device *netdev,
625 struct ethtool_ringparam *ring)
626{
627 struct e1000_adapter *adapter = netdev_priv(netdev);
628 struct e1000_ring *tx_ring, *tx_old;
629 struct e1000_ring *rx_ring, *rx_old;
630 int err;
631
632 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
633 return -EINVAL;
634
635 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
636 msleep(1);
637
638 if (netif_running(adapter->netdev))
639 e1000e_down(adapter);
640
641 tx_old = adapter->tx_ring;
642 rx_old = adapter->rx_ring;
643
644 err = -ENOMEM;
645 tx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
646 if (!tx_ring)
647 goto err_alloc_tx;
Bruce Allancef8c792008-04-02 13:48:23 -0700648 /*
649 * use a memcpy to save any previously configured
650 * items like napi structs from having to be
651 * reinitialized
652 */
653 memcpy(tx_ring, tx_old, sizeof(struct e1000_ring));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700654
655 rx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
656 if (!rx_ring)
657 goto err_alloc_rx;
Bruce Allancef8c792008-04-02 13:48:23 -0700658 memcpy(rx_ring, rx_old, sizeof(struct e1000_ring));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700659
660 adapter->tx_ring = tx_ring;
661 adapter->rx_ring = rx_ring;
662
663 rx_ring->count = max(ring->rx_pending, (u32)E1000_MIN_RXD);
664 rx_ring->count = min(rx_ring->count, (u32)(E1000_MAX_RXD));
665 rx_ring->count = ALIGN(rx_ring->count, REQ_RX_DESCRIPTOR_MULTIPLE);
666
667 tx_ring->count = max(ring->tx_pending, (u32)E1000_MIN_TXD);
668 tx_ring->count = min(tx_ring->count, (u32)(E1000_MAX_TXD));
669 tx_ring->count = ALIGN(tx_ring->count, REQ_TX_DESCRIPTOR_MULTIPLE);
670
671 if (netif_running(adapter->netdev)) {
672 /* Try to get new resources before deleting old */
673 err = e1000e_setup_rx_resources(adapter);
674 if (err)
675 goto err_setup_rx;
676 err = e1000e_setup_tx_resources(adapter);
677 if (err)
678 goto err_setup_tx;
679
Bruce Allanad680762008-03-28 09:15:03 -0700680 /*
681 * restore the old in order to free it,
682 * then add in the new
683 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700684 adapter->rx_ring = rx_old;
685 adapter->tx_ring = tx_old;
686 e1000e_free_rx_resources(adapter);
687 e1000e_free_tx_resources(adapter);
688 kfree(tx_old);
689 kfree(rx_old);
690 adapter->rx_ring = rx_ring;
691 adapter->tx_ring = tx_ring;
692 err = e1000e_up(adapter);
693 if (err)
694 goto err_setup;
695 }
696
697 clear_bit(__E1000_RESETTING, &adapter->state);
698 return 0;
699err_setup_tx:
700 e1000e_free_rx_resources(adapter);
701err_setup_rx:
702 adapter->rx_ring = rx_old;
703 adapter->tx_ring = tx_old;
704 kfree(rx_ring);
705err_alloc_rx:
706 kfree(tx_ring);
707err_alloc_tx:
708 e1000e_up(adapter);
709err_setup:
710 clear_bit(__E1000_RESETTING, &adapter->state);
711 return err;
712}
713
Bruce Allancef8c792008-04-02 13:48:23 -0700714static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
715 int reg, int offset, u32 mask, u32 write)
Joe Perches2a887192007-11-13 20:53:51 -0800716{
Bruce Allancef8c792008-04-02 13:48:23 -0700717 u32 pat, val;
Joe Perches2a887192007-11-13 20:53:51 -0800718 static const u32 test[] =
719 {0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
Bruce Allancef8c792008-04-02 13:48:23 -0700720 for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
Joe Perches2a887192007-11-13 20:53:51 -0800721 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
Bruce Allancef8c792008-04-02 13:48:23 -0700722 (test[pat] & write));
723 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
724 if (val != (test[pat] & write & mask)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700725 e_err("pattern test reg %04X failed: got 0x%08X "
726 "expected 0x%08X\n", reg + offset, val,
727 (test[pat] & write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800728 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700729 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800730 }
731 }
Bruce Allancef8c792008-04-02 13:48:23 -0700732 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700733}
734
Joe Perches2a887192007-11-13 20:53:51 -0800735static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
736 int reg, u32 mask, u32 write)
737{
Bruce Allancef8c792008-04-02 13:48:23 -0700738 u32 val;
Joe Perches2a887192007-11-13 20:53:51 -0800739 __ew32(&adapter->hw, reg, write & mask);
Bruce Allancef8c792008-04-02 13:48:23 -0700740 val = __er32(&adapter->hw, reg);
741 if ((write & mask) != (val & mask)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700742 e_err("set/check reg %04X test failed: got 0x%08X "
743 "expected 0x%08X\n", reg, (val & mask), (write & mask));
Joe Perches2a887192007-11-13 20:53:51 -0800744 *data = reg;
Bruce Allancef8c792008-04-02 13:48:23 -0700745 return 1;
Joe Perches2a887192007-11-13 20:53:51 -0800746 }
Bruce Allancef8c792008-04-02 13:48:23 -0700747 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700748}
Bruce Allancef8c792008-04-02 13:48:23 -0700749#define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write) \
750 do { \
751 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
752 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800753 } while (0)
Bruce Allancef8c792008-04-02 13:48:23 -0700754#define REG_PATTERN_TEST(reg, mask, write) \
755 REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
Joe Perches2a887192007-11-13 20:53:51 -0800756
Bruce Allancef8c792008-04-02 13:48:23 -0700757#define REG_SET_AND_CHECK(reg, mask, write) \
758 do { \
759 if (reg_set_and_check(adapter, data, reg, mask, write)) \
760 return 1; \
Joe Perches2a887192007-11-13 20:53:51 -0800761 } while (0)
762
Auke Kokbc7f75f2007-09-17 12:30:59 -0700763static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
764{
765 struct e1000_hw *hw = &adapter->hw;
766 struct e1000_mac_info *mac = &adapter->hw.mac;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700767 u32 value;
768 u32 before;
769 u32 after;
770 u32 i;
771 u32 toggle;
772
Bruce Allanad680762008-03-28 09:15:03 -0700773 /*
774 * The status register is Read Only, so a write should fail.
Auke Kokbc7f75f2007-09-17 12:30:59 -0700775 * Some bits that get toggled are ignored.
776 */
777 switch (mac->type) {
778 /* there are several bits on newer hardware that are r/w */
779 case e1000_82571:
780 case e1000_82572:
781 case e1000_80003es2lan:
782 toggle = 0x7FFFF3FF;
783 break;
784 case e1000_82573:
785 case e1000_ich8lan:
786 case e1000_ich9lan:
787 toggle = 0x7FFFF033;
788 break;
789 default:
790 toggle = 0xFFFFF833;
791 break;
792 }
793
794 before = er32(STATUS);
795 value = (er32(STATUS) & toggle);
796 ew32(STATUS, toggle);
797 after = er32(STATUS) & toggle;
798 if (value != after) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700799 e_err("failed STATUS register test got: 0x%08X expected: "
800 "0x%08X\n", after, value);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700801 *data = 1;
802 return 1;
803 }
804 /* restore previous status */
805 ew32(STATUS, before);
806
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700807 if (!(adapter->flags & FLAG_IS_ICH)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700808 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
809 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
810 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
811 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
812 }
813
814 REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
815 REG_PATTERN_TEST(E1000_RDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
816 REG_PATTERN_TEST(E1000_RDLEN, 0x000FFF80, 0x000FFFFF);
817 REG_PATTERN_TEST(E1000_RDH, 0x0000FFFF, 0x0000FFFF);
818 REG_PATTERN_TEST(E1000_RDT, 0x0000FFFF, 0x0000FFFF);
819 REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
820 REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
821 REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
822 REG_PATTERN_TEST(E1000_TDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
823 REG_PATTERN_TEST(E1000_TDLEN, 0x000FFF80, 0x000FFFFF);
824
825 REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
826
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700827 before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700828 REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
829 REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
830
Auke Kok86582512007-10-04 15:00:08 -0700831 REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
832 REG_PATTERN_TEST(E1000_RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700833 if (!(adapter->flags & FLAG_IS_ICH))
Auke Kok86582512007-10-04 15:00:08 -0700834 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
835 REG_PATTERN_TEST(E1000_TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
836 REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
837 for (i = 0; i < mac->rar_entry_count; i++)
838 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1),
839 0x8003FFFF, 0xFFFFFFFF);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700840
841 for (i = 0; i < mac->mta_reg_count; i++)
842 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
843
844 *data = 0;
845 return 0;
846}
847
848static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
849{
850 u16 temp;
851 u16 checksum = 0;
852 u16 i;
853
854 *data = 0;
855 /* Read and add up the contents of the EEPROM */
856 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
857 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
858 *data = 1;
859 break;
860 }
861 checksum += temp;
862 }
863
864 /* If Checksum is not Correct return error else test passed */
865 if ((checksum != (u16) NVM_SUM) && !(*data))
866 *data = 2;
867
868 return *data;
869}
870
871static irqreturn_t e1000_test_intr(int irq, void *data)
872{
873 struct net_device *netdev = (struct net_device *) data;
874 struct e1000_adapter *adapter = netdev_priv(netdev);
875 struct e1000_hw *hw = &adapter->hw;
876
877 adapter->test_icr |= er32(ICR);
878
879 return IRQ_HANDLED;
880}
881
882static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
883{
884 struct net_device *netdev = adapter->netdev;
885 struct e1000_hw *hw = &adapter->hw;
886 u32 mask;
887 u32 shared_int = 1;
888 u32 irq = adapter->pdev->irq;
889 int i;
890
891 *data = 0;
892
893 /* NOTE: we don't test MSI interrupts here, yet */
894 /* Hook up test interrupt handler just for this test */
895 if (!request_irq(irq, &e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
896 netdev)) {
897 shared_int = 0;
898 } else if (request_irq(irq, &e1000_test_intr, IRQF_SHARED,
899 netdev->name, netdev)) {
900 *data = 1;
901 return -1;
902 }
Jeff Kirsher44defeb2008-08-04 17:20:41 -0700903 e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700904
905 /* Disable all the interrupts */
906 ew32(IMC, 0xFFFFFFFF);
907 msleep(10);
908
909 /* Test each interrupt */
910 for (i = 0; i < 10; i++) {
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700911 if ((adapter->flags & FLAG_IS_ICH) && (i == 8))
Auke Kokbc7f75f2007-09-17 12:30:59 -0700912 continue;
913
914 /* Interrupt to test */
915 mask = 1 << i;
916
917 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -0700918 /*
919 * Disable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -0700920 * the cause register and then force the same
921 * interrupt and see if one gets posted. If
922 * an interrupt was posted to the bus, the
923 * test failed.
924 */
925 adapter->test_icr = 0;
926 ew32(IMC, mask);
927 ew32(ICS, mask);
928 msleep(10);
929
930 if (adapter->test_icr & mask) {
931 *data = 3;
932 break;
933 }
934 }
935
Bruce Allanad680762008-03-28 09:15:03 -0700936 /*
937 * Enable the interrupt to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -0700938 * the cause register and then force the same
939 * interrupt and see if one gets posted. If
940 * an interrupt was not posted to the bus, the
941 * test failed.
942 */
943 adapter->test_icr = 0;
944 ew32(IMS, mask);
945 ew32(ICS, mask);
946 msleep(10);
947
948 if (!(adapter->test_icr & mask)) {
949 *data = 4;
950 break;
951 }
952
953 if (!shared_int) {
Bruce Allanad680762008-03-28 09:15:03 -0700954 /*
955 * Disable the other interrupts to be reported in
Auke Kokbc7f75f2007-09-17 12:30:59 -0700956 * the cause register and then force the other
957 * interrupts and see if any get posted. If
958 * an interrupt was posted to the bus, the
959 * test failed.
960 */
961 adapter->test_icr = 0;
962 ew32(IMC, ~mask & 0x00007FFF);
963 ew32(ICS, ~mask & 0x00007FFF);
964 msleep(10);
965
966 if (adapter->test_icr) {
967 *data = 5;
968 break;
969 }
970 }
971 }
972
973 /* Disable all the interrupts */
974 ew32(IMC, 0xFFFFFFFF);
975 msleep(10);
976
977 /* Unhook test interrupt handler */
978 free_irq(irq, netdev);
979
980 return *data;
981}
982
983static void e1000_free_desc_rings(struct e1000_adapter *adapter)
984{
985 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
986 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
987 struct pci_dev *pdev = adapter->pdev;
988 int i;
989
990 if (tx_ring->desc && tx_ring->buffer_info) {
991 for (i = 0; i < tx_ring->count; i++) {
992 if (tx_ring->buffer_info[i].dma)
993 pci_unmap_single(pdev,
994 tx_ring->buffer_info[i].dma,
995 tx_ring->buffer_info[i].length,
996 PCI_DMA_TODEVICE);
997 if (tx_ring->buffer_info[i].skb)
998 dev_kfree_skb(tx_ring->buffer_info[i].skb);
999 }
1000 }
1001
1002 if (rx_ring->desc && rx_ring->buffer_info) {
1003 for (i = 0; i < rx_ring->count; i++) {
1004 if (rx_ring->buffer_info[i].dma)
1005 pci_unmap_single(pdev,
1006 rx_ring->buffer_info[i].dma,
1007 2048, PCI_DMA_FROMDEVICE);
1008 if (rx_ring->buffer_info[i].skb)
1009 dev_kfree_skb(rx_ring->buffer_info[i].skb);
1010 }
1011 }
1012
1013 if (tx_ring->desc) {
1014 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1015 tx_ring->dma);
1016 tx_ring->desc = NULL;
1017 }
1018 if (rx_ring->desc) {
1019 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1020 rx_ring->dma);
1021 rx_ring->desc = NULL;
1022 }
1023
1024 kfree(tx_ring->buffer_info);
1025 tx_ring->buffer_info = NULL;
1026 kfree(rx_ring->buffer_info);
1027 rx_ring->buffer_info = NULL;
1028}
1029
1030static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1031{
1032 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1033 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1034 struct pci_dev *pdev = adapter->pdev;
1035 struct e1000_hw *hw = &adapter->hw;
1036 u32 rctl;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001037 int i;
1038 int ret_val;
1039
1040 /* Setup Tx descriptor ring and Tx buffers */
1041
1042 if (!tx_ring->count)
1043 tx_ring->count = E1000_DEFAULT_TXD;
1044
Bruce Allancef8c792008-04-02 13:48:23 -07001045 tx_ring->buffer_info = kcalloc(tx_ring->count,
1046 sizeof(struct e1000_buffer),
1047 GFP_KERNEL);
1048 if (!(tx_ring->buffer_info)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001049 ret_val = 1;
1050 goto err_nomem;
1051 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001052
1053 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1054 tx_ring->size = ALIGN(tx_ring->size, 4096);
1055 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
1056 &tx_ring->dma, GFP_KERNEL);
1057 if (!tx_ring->desc) {
1058 ret_val = 2;
1059 goto err_nomem;
1060 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001061 tx_ring->next_to_use = 0;
1062 tx_ring->next_to_clean = 0;
1063
Bruce Allancef8c792008-04-02 13:48:23 -07001064 ew32(TDBAL, ((u64) tx_ring->dma & 0x00000000FFFFFFFF));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001065 ew32(TDBAH, ((u64) tx_ring->dma >> 32));
Bruce Allancef8c792008-04-02 13:48:23 -07001066 ew32(TDLEN, tx_ring->count * sizeof(struct e1000_tx_desc));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001067 ew32(TDH, 0);
1068 ew32(TDT, 0);
Bruce Allancef8c792008-04-02 13:48:23 -07001069 ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN | E1000_TCTL_MULR |
1070 E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1071 E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001072
1073 for (i = 0; i < tx_ring->count; i++) {
1074 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
1075 struct sk_buff *skb;
1076 unsigned int skb_size = 1024;
1077
1078 skb = alloc_skb(skb_size, GFP_KERNEL);
1079 if (!skb) {
1080 ret_val = 3;
1081 goto err_nomem;
1082 }
1083 skb_put(skb, skb_size);
1084 tx_ring->buffer_info[i].skb = skb;
1085 tx_ring->buffer_info[i].length = skb->len;
1086 tx_ring->buffer_info[i].dma =
1087 pci_map_single(pdev, skb->data, skb->len,
1088 PCI_DMA_TODEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001089 if (pci_dma_mapping_error(pdev, tx_ring->buffer_info[i].dma)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001090 ret_val = 4;
1091 goto err_nomem;
1092 }
Bruce Allancef8c792008-04-02 13:48:23 -07001093 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001094 tx_desc->lower.data = cpu_to_le32(skb->len);
1095 tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1096 E1000_TXD_CMD_IFCS |
Bruce Allancef8c792008-04-02 13:48:23 -07001097 E1000_TXD_CMD_RS);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001098 tx_desc->upper.data = 0;
1099 }
1100
1101 /* Setup Rx descriptor ring and Rx buffers */
1102
1103 if (!rx_ring->count)
1104 rx_ring->count = E1000_DEFAULT_RXD;
1105
Bruce Allancef8c792008-04-02 13:48:23 -07001106 rx_ring->buffer_info = kcalloc(rx_ring->count,
1107 sizeof(struct e1000_buffer),
1108 GFP_KERNEL);
1109 if (!(rx_ring->buffer_info)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001110 ret_val = 5;
1111 goto err_nomem;
1112 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001113
1114 rx_ring->size = rx_ring->count * sizeof(struct e1000_rx_desc);
1115 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
1116 &rx_ring->dma, GFP_KERNEL);
1117 if (!rx_ring->desc) {
1118 ret_val = 6;
1119 goto err_nomem;
1120 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001121 rx_ring->next_to_use = 0;
1122 rx_ring->next_to_clean = 0;
1123
1124 rctl = er32(RCTL);
1125 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1126 ew32(RDBAL, ((u64) rx_ring->dma & 0xFFFFFFFF));
1127 ew32(RDBAH, ((u64) rx_ring->dma >> 32));
1128 ew32(RDLEN, rx_ring->size);
1129 ew32(RDH, 0);
1130 ew32(RDT, 0);
1131 rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
Bruce Allancef8c792008-04-02 13:48:23 -07001132 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1133 E1000_RCTL_SBP | E1000_RCTL_SECRC |
Auke Kokbc7f75f2007-09-17 12:30:59 -07001134 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1135 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1136 ew32(RCTL, rctl);
1137
1138 for (i = 0; i < rx_ring->count; i++) {
1139 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
1140 struct sk_buff *skb;
1141
1142 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1143 if (!skb) {
1144 ret_val = 7;
1145 goto err_nomem;
1146 }
1147 skb_reserve(skb, NET_IP_ALIGN);
1148 rx_ring->buffer_info[i].skb = skb;
1149 rx_ring->buffer_info[i].dma =
1150 pci_map_single(pdev, skb->data, 2048,
1151 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001152 if (pci_dma_mapping_error(pdev, rx_ring->buffer_info[i].dma)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001153 ret_val = 8;
1154 goto err_nomem;
1155 }
1156 rx_desc->buffer_addr =
1157 cpu_to_le64(rx_ring->buffer_info[i].dma);
1158 memset(skb->data, 0x00, skb->len);
1159 }
1160
1161 return 0;
1162
1163err_nomem:
1164 e1000_free_desc_rings(adapter);
1165 return ret_val;
1166}
1167
1168static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1169{
1170 /* Write out to PHY registers 29 and 30 to disable the Receiver. */
1171 e1e_wphy(&adapter->hw, 29, 0x001F);
1172 e1e_wphy(&adapter->hw, 30, 0x8FFC);
1173 e1e_wphy(&adapter->hw, 29, 0x001A);
1174 e1e_wphy(&adapter->hw, 30, 0x8FF0);
1175}
1176
1177static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1178{
1179 struct e1000_hw *hw = &adapter->hw;
1180 u32 ctrl_reg = 0;
1181 u32 stat_reg = 0;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001182 u16 phy_reg = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001183
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001184 hw->mac.autoneg = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001185
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001186 if (hw->phy.type == e1000_phy_m88) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001187 /* Auto-MDI/MDIX Off */
1188 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1189 /* reset to update Auto-MDI/MDIX */
1190 e1e_wphy(hw, PHY_CONTROL, 0x9140);
1191 /* autoneg off */
1192 e1e_wphy(hw, PHY_CONTROL, 0x8140);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001193 } else if (hw->phy.type == e1000_phy_gg82563)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001194 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
1195
1196 ctrl_reg = er32(CTRL);
1197
Bruce Allancef8c792008-04-02 13:48:23 -07001198 switch (hw->phy.type) {
1199 case e1000_phy_ife:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001200 /* force 100, set loopback */
1201 e1e_wphy(hw, PHY_CONTROL, 0x6100);
1202
1203 /* Now set up the MAC to the same speed/duplex as the PHY. */
1204 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1205 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1206 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1207 E1000_CTRL_SPD_100 |/* Force Speed to 100 */
1208 E1000_CTRL_FD); /* Force Duplex to FULL */
Bruce Allancef8c792008-04-02 13:48:23 -07001209 break;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001210 case e1000_phy_bm:
1211 /* Set Default MAC Interface speed to 1GB */
1212 e1e_rphy(hw, PHY_REG(2, 21), &phy_reg);
1213 phy_reg &= ~0x0007;
1214 phy_reg |= 0x006;
1215 e1e_wphy(hw, PHY_REG(2, 21), phy_reg);
1216 /* Assert SW reset for above settings to take effect */
1217 e1000e_commit_phy(hw);
1218 mdelay(1);
1219 /* Force Full Duplex */
1220 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1221 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C);
1222 /* Set Link Up (in force link) */
1223 e1e_rphy(hw, PHY_REG(776, 16), &phy_reg);
1224 e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040);
1225 /* Force Link */
1226 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1227 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040);
1228 /* Set Early Link Enable */
1229 e1e_rphy(hw, PHY_REG(769, 20), &phy_reg);
1230 e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400);
1231 /* fall through */
Bruce Allancef8c792008-04-02 13:48:23 -07001232 default:
Auke Kokbc7f75f2007-09-17 12:30:59 -07001233 /* force 1000, set loopback */
1234 e1e_wphy(hw, PHY_CONTROL, 0x4140);
Bruce Allancef8c792008-04-02 13:48:23 -07001235 mdelay(250);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001236
1237 /* Now set up the MAC to the same speed/duplex as the PHY. */
1238 ctrl_reg = er32(CTRL);
1239 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1240 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1241 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1242 E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1243 E1000_CTRL_FD); /* Force Duplex to FULL */
Bruce Allancef8c792008-04-02 13:48:23 -07001244
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001245 if (adapter->flags & FLAG_IS_ICH)
Bruce Allancef8c792008-04-02 13:48:23 -07001246 ctrl_reg |= E1000_CTRL_SLU; /* Set Link Up */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001247 }
1248
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001249 if (hw->phy.media_type == e1000_media_type_copper &&
1250 hw->phy.type == e1000_phy_m88) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001251 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1252 } else {
Bruce Allanad680762008-03-28 09:15:03 -07001253 /*
1254 * Set the ILOS bit on the fiber Nic if half duplex link is
1255 * detected.
1256 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001257 stat_reg = er32(STATUS);
1258 if ((stat_reg & E1000_STATUS_FD) == 0)
1259 ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1260 }
1261
1262 ew32(CTRL, ctrl_reg);
1263
Bruce Allanad680762008-03-28 09:15:03 -07001264 /*
1265 * Disable the receiver on the PHY so when a cable is plugged in, the
Auke Kokbc7f75f2007-09-17 12:30:59 -07001266 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1267 */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001268 if (hw->phy.type == e1000_phy_m88)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001269 e1000_phy_disable_receiver(adapter);
1270
1271 udelay(500);
1272
1273 return 0;
1274}
1275
1276static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1277{
1278 struct e1000_hw *hw = &adapter->hw;
1279 u32 ctrl = er32(CTRL);
1280 int link = 0;
1281
1282 /* special requirements for 82571/82572 fiber adapters */
1283
Bruce Allanad680762008-03-28 09:15:03 -07001284 /*
1285 * jump through hoops to make sure link is up because serdes
1286 * link is hardwired up
1287 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001288 ctrl |= E1000_CTRL_SLU;
1289 ew32(CTRL, ctrl);
1290
1291 /* disable autoneg */
1292 ctrl = er32(TXCW);
1293 ctrl &= ~(1 << 31);
1294 ew32(TXCW, ctrl);
1295
1296 link = (er32(STATUS) & E1000_STATUS_LU);
1297
1298 if (!link) {
1299 /* set invert loss of signal */
1300 ctrl = er32(CTRL);
1301 ctrl |= E1000_CTRL_ILOS;
1302 ew32(CTRL, ctrl);
1303 }
1304
Bruce Allanad680762008-03-28 09:15:03 -07001305 /*
1306 * special write to serdes control register to enable SerDes analog
1307 * loopback
1308 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001309#define E1000_SERDES_LB_ON 0x410
1310 ew32(SCTL, E1000_SERDES_LB_ON);
1311 msleep(10);
1312
1313 return 0;
1314}
1315
1316/* only call this for fiber/serdes connections to es2lan */
1317static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1318{
1319 struct e1000_hw *hw = &adapter->hw;
1320 u32 ctrlext = er32(CTRL_EXT);
1321 u32 ctrl = er32(CTRL);
1322
Bruce Allanad680762008-03-28 09:15:03 -07001323 /*
1324 * save CTRL_EXT to restore later, reuse an empty variable (unused
1325 * on mac_type 80003es2lan)
1326 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001327 adapter->tx_fifo_head = ctrlext;
1328
1329 /* clear the serdes mode bits, putting the device into mac loopback */
1330 ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1331 ew32(CTRL_EXT, ctrlext);
1332
1333 /* force speed to 1000/FD, link up */
1334 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1335 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1336 E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1337 ew32(CTRL, ctrl);
1338
1339 /* set mac loopback */
1340 ctrl = er32(RCTL);
1341 ctrl |= E1000_RCTL_LBM_MAC;
1342 ew32(RCTL, ctrl);
1343
1344 /* set testing mode parameters (no need to reset later) */
1345#define KMRNCTRLSTA_OPMODE (0x1F << 16)
1346#define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1347 ew32(KMRNCTRLSTA,
Bruce Allancef8c792008-04-02 13:48:23 -07001348 (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001349
1350 return 0;
1351}
1352
1353static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1354{
1355 struct e1000_hw *hw = &adapter->hw;
1356 u32 rctl;
1357
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001358 if (hw->phy.media_type == e1000_media_type_fiber ||
1359 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001360 switch (hw->mac.type) {
1361 case e1000_80003es2lan:
1362 return e1000_set_es2lan_mac_loopback(adapter);
1363 break;
1364 case e1000_82571:
1365 case e1000_82572:
1366 return e1000_set_82571_fiber_loopback(adapter);
1367 break;
1368 default:
1369 rctl = er32(RCTL);
1370 rctl |= E1000_RCTL_LBM_TCVR;
1371 ew32(RCTL, rctl);
1372 return 0;
1373 }
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001374 } else if (hw->phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001375 return e1000_integrated_phy_loopback(adapter);
1376 }
1377
1378 return 7;
1379}
1380
1381static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1382{
1383 struct e1000_hw *hw = &adapter->hw;
1384 u32 rctl;
1385 u16 phy_reg;
1386
1387 rctl = er32(RCTL);
1388 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1389 ew32(RCTL, rctl);
1390
1391 switch (hw->mac.type) {
1392 case e1000_80003es2lan:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001393 if (hw->phy.media_type == e1000_media_type_fiber ||
1394 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001395 /* restore CTRL_EXT, stealing space from tx_fifo_head */
Bruce Allanad680762008-03-28 09:15:03 -07001396 ew32(CTRL_EXT, adapter->tx_fifo_head);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001397 adapter->tx_fifo_head = 0;
1398 }
1399 /* fall through */
1400 case e1000_82571:
1401 case e1000_82572:
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001402 if (hw->phy.media_type == e1000_media_type_fiber ||
1403 hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001404#define E1000_SERDES_LB_OFF 0x400
1405 ew32(SCTL, E1000_SERDES_LB_OFF);
1406 msleep(10);
1407 break;
1408 }
1409 /* Fall Through */
1410 default:
1411 hw->mac.autoneg = 1;
1412 if (hw->phy.type == e1000_phy_gg82563)
1413 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1414 e1e_rphy(hw, PHY_CONTROL, &phy_reg);
1415 if (phy_reg & MII_CR_LOOPBACK) {
1416 phy_reg &= ~MII_CR_LOOPBACK;
1417 e1e_wphy(hw, PHY_CONTROL, phy_reg);
1418 e1000e_commit_phy(hw);
1419 }
1420 break;
1421 }
1422}
1423
1424static void e1000_create_lbtest_frame(struct sk_buff *skb,
1425 unsigned int frame_size)
1426{
1427 memset(skb->data, 0xFF, frame_size);
1428 frame_size &= ~1;
1429 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1430 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
1431 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
1432}
1433
1434static int e1000_check_lbtest_frame(struct sk_buff *skb,
1435 unsigned int frame_size)
1436{
1437 frame_size &= ~1;
1438 if (*(skb->data + 3) == 0xFF)
1439 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1440 (*(skb->data + frame_size / 2 + 12) == 0xAF))
1441 return 0;
1442 return 13;
1443}
1444
1445static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1446{
1447 struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1448 struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1449 struct pci_dev *pdev = adapter->pdev;
1450 struct e1000_hw *hw = &adapter->hw;
1451 int i, j, k, l;
1452 int lc;
1453 int good_cnt;
1454 int ret_val = 0;
1455 unsigned long time;
1456
1457 ew32(RDT, rx_ring->count - 1);
1458
Bruce Allanad680762008-03-28 09:15:03 -07001459 /*
1460 * Calculate the loop count based on the largest descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001461 * The idea is to wrap the largest ring a number of times using 64
1462 * send/receive pairs during each loop
1463 */
1464
1465 if (rx_ring->count <= tx_ring->count)
1466 lc = ((tx_ring->count / 64) * 2) + 1;
1467 else
1468 lc = ((rx_ring->count / 64) * 2) + 1;
1469
1470 k = 0;
1471 l = 0;
1472 for (j = 0; j <= lc; j++) { /* loop count loop */
1473 for (i = 0; i < 64; i++) { /* send the packets */
Bruce Allancef8c792008-04-02 13:48:23 -07001474 e1000_create_lbtest_frame(tx_ring->buffer_info[k].skb,
1475 1024);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001476 pci_dma_sync_single_for_device(pdev,
1477 tx_ring->buffer_info[k].dma,
1478 tx_ring->buffer_info[k].length,
1479 PCI_DMA_TODEVICE);
1480 k++;
1481 if (k == tx_ring->count)
1482 k = 0;
1483 }
1484 ew32(TDT, k);
1485 msleep(200);
1486 time = jiffies; /* set the start time for the receive */
1487 good_cnt = 0;
1488 do { /* receive the sent packets */
1489 pci_dma_sync_single_for_cpu(pdev,
1490 rx_ring->buffer_info[l].dma, 2048,
1491 PCI_DMA_FROMDEVICE);
1492
1493 ret_val = e1000_check_lbtest_frame(
1494 rx_ring->buffer_info[l].skb, 1024);
1495 if (!ret_val)
1496 good_cnt++;
1497 l++;
1498 if (l == rx_ring->count)
1499 l = 0;
Bruce Allanad680762008-03-28 09:15:03 -07001500 /*
1501 * time + 20 msecs (200 msecs on 2.4) is more than
Auke Kokbc7f75f2007-09-17 12:30:59 -07001502 * enough time to complete the receives, if it's
1503 * exceeded, break and error off
1504 */
1505 } while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1506 if (good_cnt != 64) {
1507 ret_val = 13; /* ret_val is the same as mis-compare */
1508 break;
1509 }
Bruce Allancef8c792008-04-02 13:48:23 -07001510 if (jiffies >= (time + 20)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001511 ret_val = 14; /* error code for time out error */
1512 break;
1513 }
1514 } /* end loop count loop */
1515 return ret_val;
1516}
1517
1518static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1519{
Bruce Allanad680762008-03-28 09:15:03 -07001520 /*
1521 * PHY loopback cannot be performed if SoL/IDER
1522 * sessions are active
1523 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001524 if (e1000_check_reset_block(&adapter->hw)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001525 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001526 *data = 0;
1527 goto out;
1528 }
1529
1530 *data = e1000_setup_desc_rings(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001531 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001532 goto out;
1533
1534 *data = e1000_setup_loopback_test(adapter);
Adrian Bunke2655222007-10-15 14:02:21 -07001535 if (*data)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001536 goto err_loopback;
1537
1538 *data = e1000_run_loopback_test(adapter);
1539 e1000_loopback_cleanup(adapter);
1540
1541err_loopback:
1542 e1000_free_desc_rings(adapter);
1543out:
1544 return *data;
1545}
1546
1547static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1548{
1549 struct e1000_hw *hw = &adapter->hw;
1550
1551 *data = 0;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001552 if (hw->phy.media_type == e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001553 int i = 0;
1554 hw->mac.serdes_has_link = 0;
1555
Bruce Allanad680762008-03-28 09:15:03 -07001556 /*
1557 * On some blade server designs, link establishment
1558 * could take as long as 2-3 minutes
1559 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001560 do {
1561 hw->mac.ops.check_for_link(hw);
1562 if (hw->mac.serdes_has_link)
1563 return *data;
1564 msleep(20);
1565 } while (i++ < 3750);
1566
1567 *data = 1;
1568 } else {
1569 hw->mac.ops.check_for_link(hw);
1570 if (hw->mac.autoneg)
1571 msleep(4000);
1572
1573 if (!(er32(STATUS) &
1574 E1000_STATUS_LU))
1575 *data = 1;
1576 }
1577 return *data;
1578}
1579
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001580static int e1000e_get_sset_count(struct net_device *netdev, int sset)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001581{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001582 switch (sset) {
1583 case ETH_SS_TEST:
1584 return E1000_TEST_LEN;
1585 case ETH_SS_STATS:
1586 return E1000_STATS_LEN;
1587 default:
1588 return -EOPNOTSUPP;
1589 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001590}
1591
1592static void e1000_diag_test(struct net_device *netdev,
1593 struct ethtool_test *eth_test, u64 *data)
1594{
1595 struct e1000_adapter *adapter = netdev_priv(netdev);
1596 u16 autoneg_advertised;
1597 u8 forced_speed_duplex;
1598 u8 autoneg;
1599 bool if_running = netif_running(netdev);
1600
1601 set_bit(__E1000_TESTING, &adapter->state);
1602 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1603 /* Offline tests */
1604
1605 /* save speed, duplex, autoneg settings */
1606 autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1607 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1608 autoneg = adapter->hw.mac.autoneg;
1609
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001610 e_info("offline testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001611
Bruce Allanad680762008-03-28 09:15:03 -07001612 /*
1613 * Link test performed before hardware reset so autoneg doesn't
1614 * interfere with test result
1615 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001616 if (e1000_link_test(adapter, &data[4]))
1617 eth_test->flags |= ETH_TEST_FL_FAILED;
1618
1619 if (if_running)
1620 /* indicate we're in test mode */
1621 dev_close(netdev);
1622 else
1623 e1000e_reset(adapter);
1624
1625 if (e1000_reg_test(adapter, &data[0]))
1626 eth_test->flags |= ETH_TEST_FL_FAILED;
1627
1628 e1000e_reset(adapter);
1629 if (e1000_eeprom_test(adapter, &data[1]))
1630 eth_test->flags |= ETH_TEST_FL_FAILED;
1631
1632 e1000e_reset(adapter);
1633 if (e1000_intr_test(adapter, &data[2]))
1634 eth_test->flags |= ETH_TEST_FL_FAILED;
1635
1636 e1000e_reset(adapter);
1637 /* make sure the phy is powered up */
1638 e1000e_power_up_phy(adapter);
1639 if (e1000_loopback_test(adapter, &data[3]))
1640 eth_test->flags |= ETH_TEST_FL_FAILED;
1641
1642 /* restore speed, duplex, autoneg settings */
1643 adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1644 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1645 adapter->hw.mac.autoneg = autoneg;
1646
1647 /* force this routine to wait until autoneg complete/timeout */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001648 adapter->hw.phy.autoneg_wait_to_complete = 1;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001649 e1000e_reset(adapter);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001650 adapter->hw.phy.autoneg_wait_to_complete = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001651
1652 clear_bit(__E1000_TESTING, &adapter->state);
1653 if (if_running)
1654 dev_open(netdev);
1655 } else {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001656 e_info("online testing starting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001657 /* Online tests */
1658 if (e1000_link_test(adapter, &data[4]))
1659 eth_test->flags |= ETH_TEST_FL_FAILED;
1660
1661 /* Online tests aren't run; pass by default */
1662 data[0] = 0;
1663 data[1] = 0;
1664 data[2] = 0;
1665 data[3] = 0;
1666
1667 clear_bit(__E1000_TESTING, &adapter->state);
1668 }
1669 msleep_interruptible(4 * 1000);
1670}
1671
1672static void e1000_get_wol(struct net_device *netdev,
1673 struct ethtool_wolinfo *wol)
1674{
1675 struct e1000_adapter *adapter = netdev_priv(netdev);
1676
1677 wol->supported = 0;
1678 wol->wolopts = 0;
1679
1680 if (!(adapter->flags & FLAG_HAS_WOL))
1681 return;
1682
1683 wol->supported = WAKE_UCAST | WAKE_MCAST |
Mitch Williamsefb90e42008-01-29 12:43:02 -08001684 WAKE_BCAST | WAKE_MAGIC |
1685 WAKE_PHY | WAKE_ARP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001686
1687 /* apply any specific unsupported masks here */
1688 if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1689 wol->supported &= ~WAKE_UCAST;
1690
1691 if (adapter->wol & E1000_WUFC_EX)
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001692 e_err("Interface does not support directed (unicast) "
1693 "frame wake-up packets\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001694 }
1695
1696 if (adapter->wol & E1000_WUFC_EX)
1697 wol->wolopts |= WAKE_UCAST;
1698 if (adapter->wol & E1000_WUFC_MC)
1699 wol->wolopts |= WAKE_MCAST;
1700 if (adapter->wol & E1000_WUFC_BC)
1701 wol->wolopts |= WAKE_BCAST;
1702 if (adapter->wol & E1000_WUFC_MAG)
1703 wol->wolopts |= WAKE_MAGIC;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001704 if (adapter->wol & E1000_WUFC_LNKC)
1705 wol->wolopts |= WAKE_PHY;
1706 if (adapter->wol & E1000_WUFC_ARP)
1707 wol->wolopts |= WAKE_ARP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001708}
1709
1710static int e1000_set_wol(struct net_device *netdev,
1711 struct ethtool_wolinfo *wol)
1712{
1713 struct e1000_adapter *adapter = netdev_priv(netdev);
1714
Mitch Williamsefb90e42008-01-29 12:43:02 -08001715 if (wol->wolopts & WAKE_MAGICSECURE)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001716 return -EOPNOTSUPP;
1717
1718 if (!(adapter->flags & FLAG_HAS_WOL))
1719 return wol->wolopts ? -EOPNOTSUPP : 0;
1720
1721 /* these settings will always override what we currently have */
1722 adapter->wol = 0;
1723
1724 if (wol->wolopts & WAKE_UCAST)
1725 adapter->wol |= E1000_WUFC_EX;
1726 if (wol->wolopts & WAKE_MCAST)
1727 adapter->wol |= E1000_WUFC_MC;
1728 if (wol->wolopts & WAKE_BCAST)
1729 adapter->wol |= E1000_WUFC_BC;
1730 if (wol->wolopts & WAKE_MAGIC)
1731 adapter->wol |= E1000_WUFC_MAG;
Mitch Williamsefb90e42008-01-29 12:43:02 -08001732 if (wol->wolopts & WAKE_PHY)
1733 adapter->wol |= E1000_WUFC_LNKC;
1734 if (wol->wolopts & WAKE_ARP)
1735 adapter->wol |= E1000_WUFC_ARP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001736
1737 return 0;
1738}
1739
1740/* toggle LED 4 times per second = 2 "blinks" per second */
1741#define E1000_ID_INTERVAL (HZ/4)
1742
1743/* bit defines for adapter->led_status */
1744#define E1000_LED_ON 0
1745
1746static void e1000_led_blink_callback(unsigned long data)
1747{
1748 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1749
1750 if (test_and_change_bit(E1000_LED_ON, &adapter->led_status))
1751 adapter->hw.mac.ops.led_off(&adapter->hw);
1752 else
1753 adapter->hw.mac.ops.led_on(&adapter->hw);
1754
1755 mod_timer(&adapter->blink_timer, jiffies + E1000_ID_INTERVAL);
1756}
1757
1758static int e1000_phys_id(struct net_device *netdev, u32 data)
1759{
1760 struct e1000_adapter *adapter = netdev_priv(netdev);
1761
Stephen Hemminger5a9147b2007-10-29 10:46:05 -07001762 if (!data)
1763 data = INT_MAX;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001764
1765 if (adapter->hw.phy.type == e1000_phy_ife) {
1766 if (!adapter->blink_timer.function) {
1767 init_timer(&adapter->blink_timer);
1768 adapter->blink_timer.function =
1769 e1000_led_blink_callback;
1770 adapter->blink_timer.data = (unsigned long) adapter;
1771 }
1772 mod_timer(&adapter->blink_timer, jiffies);
1773 msleep_interruptible(data * 1000);
1774 del_timer_sync(&adapter->blink_timer);
1775 e1e_wphy(&adapter->hw,
1776 IFE_PHY_SPECIAL_CONTROL_LED, 0);
1777 } else {
1778 e1000e_blink_led(&adapter->hw);
1779 msleep_interruptible(data * 1000);
1780 }
1781
1782 adapter->hw.mac.ops.led_off(&adapter->hw);
1783 clear_bit(E1000_LED_ON, &adapter->led_status);
1784 adapter->hw.mac.ops.cleanup_led(&adapter->hw);
1785
1786 return 0;
1787}
1788
Auke Kokde5b3072008-04-23 11:09:08 -07001789static int e1000_get_coalesce(struct net_device *netdev,
1790 struct ethtool_coalesce *ec)
1791{
1792 struct e1000_adapter *adapter = netdev_priv(netdev);
1793
1794 if (adapter->itr_setting <= 3)
1795 ec->rx_coalesce_usecs = adapter->itr_setting;
1796 else
1797 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1798
1799 return 0;
1800}
1801
1802static int e1000_set_coalesce(struct net_device *netdev,
1803 struct ethtool_coalesce *ec)
1804{
1805 struct e1000_adapter *adapter = netdev_priv(netdev);
1806 struct e1000_hw *hw = &adapter->hw;
1807
1808 if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
1809 ((ec->rx_coalesce_usecs > 3) &&
1810 (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1811 (ec->rx_coalesce_usecs == 2))
1812 return -EINVAL;
1813
1814 if (ec->rx_coalesce_usecs <= 3) {
1815 adapter->itr = 20000;
1816 adapter->itr_setting = ec->rx_coalesce_usecs;
1817 } else {
1818 adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1819 adapter->itr_setting = adapter->itr & ~3;
1820 }
1821
1822 if (adapter->itr_setting != 0)
1823 ew32(ITR, 1000000000 / (adapter->itr * 256));
1824 else
1825 ew32(ITR, 0);
1826
1827 return 0;
1828}
1829
Auke Kokbc7f75f2007-09-17 12:30:59 -07001830static int e1000_nway_reset(struct net_device *netdev)
1831{
1832 struct e1000_adapter *adapter = netdev_priv(netdev);
1833 if (netif_running(netdev))
1834 e1000e_reinit_locked(adapter);
1835 return 0;
1836}
1837
Auke Kokbc7f75f2007-09-17 12:30:59 -07001838static void e1000_get_ethtool_stats(struct net_device *netdev,
1839 struct ethtool_stats *stats,
1840 u64 *data)
1841{
1842 struct e1000_adapter *adapter = netdev_priv(netdev);
1843 int i;
1844
1845 e1000e_update_stats(adapter);
1846 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1847 char *p = (char *)adapter+e1000_gstrings_stats[i].stat_offset;
1848 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
1849 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1850 }
1851}
1852
1853static void e1000_get_strings(struct net_device *netdev, u32 stringset,
1854 u8 *data)
1855{
1856 u8 *p = data;
1857 int i;
1858
1859 switch (stringset) {
1860 case ETH_SS_TEST:
Bruce Allanad680762008-03-28 09:15:03 -07001861 memcpy(data, *e1000_gstrings_test, sizeof(e1000_gstrings_test));
Auke Kokbc7f75f2007-09-17 12:30:59 -07001862 break;
1863 case ETH_SS_STATS:
1864 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1865 memcpy(p, e1000_gstrings_stats[i].stat_string,
1866 ETH_GSTRING_LEN);
1867 p += ETH_GSTRING_LEN;
1868 }
1869 break;
1870 }
1871}
1872
1873static const struct ethtool_ops e1000_ethtool_ops = {
1874 .get_settings = e1000_get_settings,
1875 .set_settings = e1000_set_settings,
1876 .get_drvinfo = e1000_get_drvinfo,
1877 .get_regs_len = e1000_get_regs_len,
1878 .get_regs = e1000_get_regs,
1879 .get_wol = e1000_get_wol,
1880 .set_wol = e1000_set_wol,
1881 .get_msglevel = e1000_get_msglevel,
1882 .set_msglevel = e1000_set_msglevel,
1883 .nway_reset = e1000_nway_reset,
Auke Kok369d7422007-10-15 14:30:59 -07001884 .get_link = e1000_get_link,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001885 .get_eeprom_len = e1000_get_eeprom_len,
1886 .get_eeprom = e1000_get_eeprom,
1887 .set_eeprom = e1000_set_eeprom,
1888 .get_ringparam = e1000_get_ringparam,
1889 .set_ringparam = e1000_set_ringparam,
1890 .get_pauseparam = e1000_get_pauseparam,
1891 .set_pauseparam = e1000_set_pauseparam,
1892 .get_rx_csum = e1000_get_rx_csum,
1893 .set_rx_csum = e1000_set_rx_csum,
1894 .get_tx_csum = e1000_get_tx_csum,
1895 .set_tx_csum = e1000_set_tx_csum,
1896 .get_sg = ethtool_op_get_sg,
1897 .set_sg = ethtool_op_set_sg,
1898 .get_tso = ethtool_op_get_tso,
1899 .set_tso = e1000_set_tso,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001900 .self_test = e1000_diag_test,
1901 .get_strings = e1000_get_strings,
1902 .phys_id = e1000_phys_id,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001903 .get_ethtool_stats = e1000_get_ethtool_stats,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001904 .get_sset_count = e1000e_get_sset_count,
Auke Kokde5b3072008-04-23 11:09:08 -07001905 .get_coalesce = e1000_get_coalesce,
1906 .set_coalesce = e1000_set_coalesce,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001907};
1908
1909void e1000e_set_ethtool_ops(struct net_device *netdev)
1910{
1911 SET_ETHTOOL_OPS(netdev, &e1000_ethtool_ops);
1912}