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