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