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