Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[linux-2.6] / 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         { "tx_restart_queue", E1000_STAT(restart_queue) },
89         { "rx_long_length_errors", E1000_STAT(stats.roc) },
90         { "rx_short_length_errors", E1000_STAT(stats.ruc) },
91         { "rx_align_errors", E1000_STAT(stats.algnerrc) },
92         { "tx_tcp_seg_good", E1000_STAT(stats.tsctc) },
93         { "tx_tcp_seg_failed", E1000_STAT(stats.tsctfc) },
94         { "rx_flow_control_xon", E1000_STAT(stats.xonrxc) },
95         { "rx_flow_control_xoff", E1000_STAT(stats.xoffrxc) },
96         { "tx_flow_control_xon", E1000_STAT(stats.xontxc) },
97         { "tx_flow_control_xoff", E1000_STAT(stats.xofftxc) },
98         { "rx_long_byte_count", E1000_STAT(stats.gorcl) },
99         { "rx_csum_offload_good", E1000_STAT(hw_csum_good) },
100         { "rx_csum_offload_errors", E1000_STAT(hw_csum_err) },
101         { "rx_header_split", E1000_STAT(rx_hdr_split) },
102         { "alloc_rx_buff_failed", E1000_STAT(alloc_rx_buff_failed) },
103         { "tx_smbus", E1000_STAT(stats.mgptc) },
104         { "rx_smbus", E1000_STAT(stats.mgprc) },
105         { "dropped_smbus", E1000_STAT(stats.mgpdc) },
106 };
107
108 #define E1000_QUEUE_STATS_LEN 0
109 #define E1000_GLOBAL_STATS_LEN  \
110         sizeof(e1000_gstrings_stats) / sizeof(struct e1000_stats)
111 #define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN + E1000_QUEUE_STATS_LEN)
112 static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
113         "Register test  (offline)", "Eeprom test    (offline)",
114         "Interrupt test (offline)", "Loopback test  (offline)",
115         "Link test   (on/offline)"
116 };
117 #define E1000_TEST_LEN sizeof(e1000_gstrings_test) / ETH_GSTRING_LEN
118
119 static int
120 e1000_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
121 {
122         struct e1000_adapter *adapter = netdev_priv(netdev);
123         struct e1000_hw *hw = &adapter->hw;
124
125         if (hw->media_type == e1000_media_type_copper) {
126
127                 ecmd->supported = (SUPPORTED_10baseT_Half |
128                                    SUPPORTED_10baseT_Full |
129                                    SUPPORTED_100baseT_Half |
130                                    SUPPORTED_100baseT_Full |
131                                    SUPPORTED_1000baseT_Full|
132                                    SUPPORTED_Autoneg |
133                                    SUPPORTED_TP);
134                 if (hw->phy_type == e1000_phy_ife)
135                         ecmd->supported &= ~SUPPORTED_1000baseT_Full;
136                 ecmd->advertising = ADVERTISED_TP;
137
138                 if (hw->autoneg == 1) {
139                         ecmd->advertising |= ADVERTISED_Autoneg;
140                         /* the e1000 autoneg seems to match ethtool nicely */
141                         ecmd->advertising |= hw->autoneg_advertised;
142                 }
143
144                 ecmd->port = PORT_TP;
145                 ecmd->phy_address = hw->phy_addr;
146
147                 if (hw->mac_type == e1000_82543)
148                         ecmd->transceiver = XCVR_EXTERNAL;
149                 else
150                         ecmd->transceiver = XCVR_INTERNAL;
151
152         } else {
153                 ecmd->supported   = (SUPPORTED_1000baseT_Full |
154                                      SUPPORTED_FIBRE |
155                                      SUPPORTED_Autoneg);
156
157                 ecmd->advertising = (ADVERTISED_1000baseT_Full |
158                                      ADVERTISED_FIBRE |
159                                      ADVERTISED_Autoneg);
160
161                 ecmd->port = PORT_FIBRE;
162
163                 if (hw->mac_type >= e1000_82545)
164                         ecmd->transceiver = XCVR_INTERNAL;
165                 else
166                         ecmd->transceiver = XCVR_EXTERNAL;
167         }
168
169         if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
170
171                 e1000_get_speed_and_duplex(hw, &adapter->link_speed,
172                                                    &adapter->link_duplex);
173                 ecmd->speed = adapter->link_speed;
174
175                 /* unfortunatly FULL_DUPLEX != DUPLEX_FULL
176                  *          and HALF_DUPLEX != DUPLEX_HALF */
177
178                 if (adapter->link_duplex == FULL_DUPLEX)
179                         ecmd->duplex = DUPLEX_FULL;
180                 else
181                         ecmd->duplex = DUPLEX_HALF;
182         } else {
183                 ecmd->speed = -1;
184                 ecmd->duplex = -1;
185         }
186
187         ecmd->autoneg = ((hw->media_type == e1000_media_type_fiber) ||
188                          hw->autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
189         return 0;
190 }
191
192 static int
193 e1000_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
194 {
195         struct e1000_adapter *adapter = netdev_priv(netdev);
196         struct e1000_hw *hw = &adapter->hw;
197
198         /* When SoL/IDER sessions are active, autoneg/speed/duplex
199          * cannot be changed */
200         if (e1000_check_phy_reset_block(hw)) {
201                 DPRINTK(DRV, ERR, "Cannot change link characteristics "
202                         "when SoL/IDER is active.\n");
203                 return -EINVAL;
204         }
205
206         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
207                 msleep(1);
208
209         if (ecmd->autoneg == AUTONEG_ENABLE) {
210                 hw->autoneg = 1;
211                 if (hw->media_type == e1000_media_type_fiber)
212                         hw->autoneg_advertised = ADVERTISED_1000baseT_Full |
213                                      ADVERTISED_FIBRE |
214                                      ADVERTISED_Autoneg;
215                 else
216                         hw->autoneg_advertised = ecmd->advertising |
217                                                  ADVERTISED_TP |
218                                                  ADVERTISED_Autoneg;
219                 ecmd->advertising = hw->autoneg_advertised;
220         } else
221                 if (e1000_set_spd_dplx(adapter, ecmd->speed + ecmd->duplex)) {
222                         clear_bit(__E1000_RESETTING, &adapter->flags);
223                         return -EINVAL;
224                 }
225
226         /* reset the link */
227
228         if (netif_running(adapter->netdev)) {
229                 e1000_down(adapter);
230                 e1000_up(adapter);
231         } else
232                 e1000_reset(adapter);
233
234         clear_bit(__E1000_RESETTING, &adapter->flags);
235         return 0;
236 }
237
238 static void
239 e1000_get_pauseparam(struct net_device *netdev,
240                      struct ethtool_pauseparam *pause)
241 {
242         struct e1000_adapter *adapter = netdev_priv(netdev);
243         struct e1000_hw *hw = &adapter->hw;
244
245         pause->autoneg =
246                 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
247
248         if (hw->fc == E1000_FC_RX_PAUSE)
249                 pause->rx_pause = 1;
250         else if (hw->fc == E1000_FC_TX_PAUSE)
251                 pause->tx_pause = 1;
252         else if (hw->fc == E1000_FC_FULL) {
253                 pause->rx_pause = 1;
254                 pause->tx_pause = 1;
255         }
256 }
257
258 static int
259 e1000_set_pauseparam(struct net_device *netdev,
260                      struct ethtool_pauseparam *pause)
261 {
262         struct e1000_adapter *adapter = netdev_priv(netdev);
263         struct e1000_hw *hw = &adapter->hw;
264         int retval = 0;
265
266         adapter->fc_autoneg = pause->autoneg;
267
268         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
269                 msleep(1);
270
271         if (pause->rx_pause && pause->tx_pause)
272                 hw->fc = E1000_FC_FULL;
273         else if (pause->rx_pause && !pause->tx_pause)
274                 hw->fc = E1000_FC_RX_PAUSE;
275         else if (!pause->rx_pause && pause->tx_pause)
276                 hw->fc = E1000_FC_TX_PAUSE;
277         else if (!pause->rx_pause && !pause->tx_pause)
278                 hw->fc = E1000_FC_NONE;
279
280         hw->original_fc = hw->fc;
281
282         if (adapter->fc_autoneg == AUTONEG_ENABLE) {
283                 if (netif_running(adapter->netdev)) {
284                         e1000_down(adapter);
285                         e1000_up(adapter);
286                 } else
287                         e1000_reset(adapter);
288         } else
289                 retval = ((hw->media_type == e1000_media_type_fiber) ?
290                           e1000_setup_link(hw) : e1000_force_mac_fc(hw));
291
292         clear_bit(__E1000_RESETTING, &adapter->flags);
293         return retval;
294 }
295
296 static uint32_t
297 e1000_get_rx_csum(struct net_device *netdev)
298 {
299         struct e1000_adapter *adapter = netdev_priv(netdev);
300         return adapter->rx_csum;
301 }
302
303 static int
304 e1000_set_rx_csum(struct net_device *netdev, uint32_t data)
305 {
306         struct e1000_adapter *adapter = netdev_priv(netdev);
307         adapter->rx_csum = data;
308
309         if (netif_running(netdev))
310                 e1000_reinit_locked(adapter);
311         else
312                 e1000_reset(adapter);
313         return 0;
314 }
315
316 static uint32_t
317 e1000_get_tx_csum(struct net_device *netdev)
318 {
319         return (netdev->features & NETIF_F_HW_CSUM) != 0;
320 }
321
322 static int
323 e1000_set_tx_csum(struct net_device *netdev, uint32_t data)
324 {
325         struct e1000_adapter *adapter = netdev_priv(netdev);
326
327         if (adapter->hw.mac_type < e1000_82543) {
328                 if (!data)
329                         return -EINVAL;
330                 return 0;
331         }
332
333         if (data)
334                 netdev->features |= NETIF_F_HW_CSUM;
335         else
336                 netdev->features &= ~NETIF_F_HW_CSUM;
337
338         return 0;
339 }
340
341 static int
342 e1000_set_tso(struct net_device *netdev, uint32_t data)
343 {
344         struct e1000_adapter *adapter = netdev_priv(netdev);
345         if ((adapter->hw.mac_type < e1000_82544) ||
346             (adapter->hw.mac_type == e1000_82547))
347                 return data ? -EINVAL : 0;
348
349         if (data)
350                 netdev->features |= NETIF_F_TSO;
351         else
352                 netdev->features &= ~NETIF_F_TSO;
353
354         if (data)
355                 netdev->features |= NETIF_F_TSO6;
356         else
357                 netdev->features &= ~NETIF_F_TSO6;
358
359         DPRINTK(PROBE, INFO, "TSO is %s\n", data ? "Enabled" : "Disabled");
360         adapter->tso_force = TRUE;
361         return 0;
362 }
363
364 static uint32_t
365 e1000_get_msglevel(struct net_device *netdev)
366 {
367         struct e1000_adapter *adapter = netdev_priv(netdev);
368         return adapter->msg_enable;
369 }
370
371 static void
372 e1000_set_msglevel(struct net_device *netdev, uint32_t data)
373 {
374         struct e1000_adapter *adapter = netdev_priv(netdev);
375         adapter->msg_enable = data;
376 }
377
378 static int
379 e1000_get_regs_len(struct net_device *netdev)
380 {
381 #define E1000_REGS_LEN 32
382         return E1000_REGS_LEN * sizeof(uint32_t);
383 }
384
385 static void
386 e1000_get_regs(struct net_device *netdev,
387                struct ethtool_regs *regs, void *p)
388 {
389         struct e1000_adapter *adapter = netdev_priv(netdev);
390         struct e1000_hw *hw = &adapter->hw;
391         uint32_t *regs_buff = p;
392         uint16_t phy_data;
393
394         memset(p, 0, E1000_REGS_LEN * sizeof(uint32_t));
395
396         regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
397
398         regs_buff[0]  = E1000_READ_REG(hw, CTRL);
399         regs_buff[1]  = E1000_READ_REG(hw, STATUS);
400
401         regs_buff[2]  = E1000_READ_REG(hw, RCTL);
402         regs_buff[3]  = E1000_READ_REG(hw, RDLEN);
403         regs_buff[4]  = E1000_READ_REG(hw, RDH);
404         regs_buff[5]  = E1000_READ_REG(hw, RDT);
405         regs_buff[6]  = E1000_READ_REG(hw, RDTR);
406
407         regs_buff[7]  = E1000_READ_REG(hw, TCTL);
408         regs_buff[8]  = E1000_READ_REG(hw, TDLEN);
409         regs_buff[9]  = E1000_READ_REG(hw, TDH);
410         regs_buff[10] = E1000_READ_REG(hw, TDT);
411         regs_buff[11] = E1000_READ_REG(hw, TIDV);
412
413         regs_buff[12] = adapter->hw.phy_type;  /* PHY type (IGP=1, M88=0) */
414         if (hw->phy_type == e1000_phy_igp) {
415                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
416                                     IGP01E1000_PHY_AGC_A);
417                 e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_A &
418                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
419                 regs_buff[13] = (uint32_t)phy_data; /* cable length */
420                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
421                                     IGP01E1000_PHY_AGC_B);
422                 e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_B &
423                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
424                 regs_buff[14] = (uint32_t)phy_data; /* cable length */
425                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
426                                     IGP01E1000_PHY_AGC_C);
427                 e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_C &
428                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
429                 regs_buff[15] = (uint32_t)phy_data; /* cable length */
430                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
431                                     IGP01E1000_PHY_AGC_D);
432                 e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_D &
433                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
434                 regs_buff[16] = (uint32_t)phy_data; /* cable length */
435                 regs_buff[17] = 0; /* extended 10bt distance (not needed) */
436                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
437                 e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS &
438                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
439                 regs_buff[18] = (uint32_t)phy_data; /* cable polarity */
440                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
441                                     IGP01E1000_PHY_PCS_INIT_REG);
442                 e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG &
443                                    IGP01E1000_PHY_PAGE_SELECT, &phy_data);
444                 regs_buff[19] = (uint32_t)phy_data; /* cable polarity */
445                 regs_buff[20] = 0; /* polarity correction enabled (always) */
446                 regs_buff[22] = 0; /* phy receive errors (unavailable) */
447                 regs_buff[23] = regs_buff[18]; /* mdix mode */
448                 e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
449         } else {
450                 e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
451                 regs_buff[13] = (uint32_t)phy_data; /* cable length */
452                 regs_buff[14] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
453                 regs_buff[15] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
454                 regs_buff[16] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
455                 e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
456                 regs_buff[17] = (uint32_t)phy_data; /* extended 10bt distance */
457                 regs_buff[18] = regs_buff[13]; /* cable polarity */
458                 regs_buff[19] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
459                 regs_buff[20] = regs_buff[17]; /* polarity correction */
460                 /* phy receive errors */
461                 regs_buff[22] = adapter->phy_stats.receive_errors;
462                 regs_buff[23] = regs_buff[13]; /* mdix mode */
463         }
464         regs_buff[21] = adapter->phy_stats.idle_errors;  /* phy idle errors */
465         e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
466         regs_buff[24] = (uint32_t)phy_data;  /* phy local receiver status */
467         regs_buff[25] = regs_buff[24];  /* phy remote receiver status */
468         if (hw->mac_type >= e1000_82540 &&
469             hw->mac_type < e1000_82571 &&
470             hw->media_type == e1000_media_type_copper) {
471                 regs_buff[26] = E1000_READ_REG(hw, MANC);
472         }
473 }
474
475 static int
476 e1000_get_eeprom_len(struct net_device *netdev)
477 {
478         struct e1000_adapter *adapter = netdev_priv(netdev);
479         return adapter->hw.eeprom.word_size * 2;
480 }
481
482 static int
483 e1000_get_eeprom(struct net_device *netdev,
484                       struct ethtool_eeprom *eeprom, uint8_t *bytes)
485 {
486         struct e1000_adapter *adapter = netdev_priv(netdev);
487         struct e1000_hw *hw = &adapter->hw;
488         uint16_t *eeprom_buff;
489         int first_word, last_word;
490         int ret_val = 0;
491         uint16_t i;
492
493         if (eeprom->len == 0)
494                 return -EINVAL;
495
496         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
497
498         first_word = eeprom->offset >> 1;
499         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
500
501         eeprom_buff = kmalloc(sizeof(uint16_t) *
502                         (last_word - first_word + 1), GFP_KERNEL);
503         if (!eeprom_buff)
504                 return -ENOMEM;
505
506         if (hw->eeprom.type == e1000_eeprom_spi)
507                 ret_val = e1000_read_eeprom(hw, first_word,
508                                             last_word - first_word + 1,
509                                             eeprom_buff);
510         else {
511                 for (i = 0; i < last_word - first_word + 1; i++)
512                         if ((ret_val = e1000_read_eeprom(hw, first_word + i, 1,
513                                                         &eeprom_buff[i])))
514                                 break;
515         }
516
517         /* Device's eeprom is always little-endian, word addressable */
518         for (i = 0; i < last_word - first_word + 1; i++)
519                 le16_to_cpus(&eeprom_buff[i]);
520
521         memcpy(bytes, (uint8_t *)eeprom_buff + (eeprom->offset & 1),
522                         eeprom->len);
523         kfree(eeprom_buff);
524
525         return ret_val;
526 }
527
528 static int
529 e1000_set_eeprom(struct net_device *netdev,
530                       struct ethtool_eeprom *eeprom, uint8_t *bytes)
531 {
532         struct e1000_adapter *adapter = netdev_priv(netdev);
533         struct e1000_hw *hw = &adapter->hw;
534         uint16_t *eeprom_buff;
535         void *ptr;
536         int max_len, first_word, last_word, ret_val = 0;
537         uint16_t i;
538
539         if (eeprom->len == 0)
540                 return -EOPNOTSUPP;
541
542         if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
543                 return -EFAULT;
544
545         max_len = hw->eeprom.word_size * 2;
546
547         first_word = eeprom->offset >> 1;
548         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
549         eeprom_buff = kmalloc(max_len, GFP_KERNEL);
550         if (!eeprom_buff)
551                 return -ENOMEM;
552
553         ptr = (void *)eeprom_buff;
554
555         if (eeprom->offset & 1) {
556                 /* need read/modify/write of first changed EEPROM word */
557                 /* only the second byte of the word is being modified */
558                 ret_val = e1000_read_eeprom(hw, first_word, 1,
559                                             &eeprom_buff[0]);
560                 ptr++;
561         }
562         if (((eeprom->offset + eeprom->len) & 1) && (ret_val == 0)) {
563                 /* need read/modify/write of last changed EEPROM word */
564                 /* only the first byte of the word is being modified */
565                 ret_val = e1000_read_eeprom(hw, last_word, 1,
566                                   &eeprom_buff[last_word - first_word]);
567         }
568
569         /* Device's eeprom is always little-endian, word addressable */
570         for (i = 0; i < last_word - first_word + 1; i++)
571                 le16_to_cpus(&eeprom_buff[i]);
572
573         memcpy(ptr, bytes, eeprom->len);
574
575         for (i = 0; i < last_word - first_word + 1; i++)
576                 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
577
578         ret_val = e1000_write_eeprom(hw, first_word,
579                                      last_word - first_word + 1, eeprom_buff);
580
581         /* Update the checksum over the first part of the EEPROM if needed
582          * and flush shadow RAM for 82573 conrollers */
583         if ((ret_val == 0) && ((first_word <= EEPROM_CHECKSUM_REG) ||
584                                 (hw->mac_type == e1000_82573)))
585                 e1000_update_eeprom_checksum(hw);
586
587         kfree(eeprom_buff);
588         return ret_val;
589 }
590
591 static void
592 e1000_get_drvinfo(struct net_device *netdev,
593                        struct ethtool_drvinfo *drvinfo)
594 {
595         struct e1000_adapter *adapter = netdev_priv(netdev);
596         char firmware_version[32];
597         uint16_t eeprom_data;
598
599         strncpy(drvinfo->driver,  e1000_driver_name, 32);
600         strncpy(drvinfo->version, e1000_driver_version, 32);
601
602         /* EEPROM image version # is reported as firmware version # for
603          * 8257{1|2|3} controllers */
604         e1000_read_eeprom(&adapter->hw, 5, 1, &eeprom_data);
605         switch (adapter->hw.mac_type) {
606         case e1000_82571:
607         case e1000_82572:
608         case e1000_82573:
609         case e1000_80003es2lan:
610         case e1000_ich8lan:
611                 sprintf(firmware_version, "%d.%d-%d",
612                         (eeprom_data & 0xF000) >> 12,
613                         (eeprom_data & 0x0FF0) >> 4,
614                         eeprom_data & 0x000F);
615                 break;
616         default:
617                 sprintf(firmware_version, "N/A");
618         }
619
620         strncpy(drvinfo->fw_version, firmware_version, 32);
621         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
622         drvinfo->n_stats = E1000_STATS_LEN;
623         drvinfo->testinfo_len = E1000_TEST_LEN;
624         drvinfo->regdump_len = e1000_get_regs_len(netdev);
625         drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
626 }
627
628 static void
629 e1000_get_ringparam(struct net_device *netdev,
630                     struct ethtool_ringparam *ring)
631 {
632         struct e1000_adapter *adapter = netdev_priv(netdev);
633         e1000_mac_type mac_type = adapter->hw.mac_type;
634         struct e1000_tx_ring *txdr = adapter->tx_ring;
635         struct e1000_rx_ring *rxdr = adapter->rx_ring;
636
637         ring->rx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_RXD :
638                 E1000_MAX_82544_RXD;
639         ring->tx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_TXD :
640                 E1000_MAX_82544_TXD;
641         ring->rx_mini_max_pending = 0;
642         ring->rx_jumbo_max_pending = 0;
643         ring->rx_pending = rxdr->count;
644         ring->tx_pending = txdr->count;
645         ring->rx_mini_pending = 0;
646         ring->rx_jumbo_pending = 0;
647 }
648
649 static int
650 e1000_set_ringparam(struct net_device *netdev,
651                     struct ethtool_ringparam *ring)
652 {
653         struct e1000_adapter *adapter = netdev_priv(netdev);
654         e1000_mac_type mac_type = adapter->hw.mac_type;
655         struct e1000_tx_ring *txdr, *tx_old;
656         struct e1000_rx_ring *rxdr, *rx_old;
657         int i, err, tx_ring_size, rx_ring_size;
658
659         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
660                 return -EINVAL;
661
662         tx_ring_size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
663         rx_ring_size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
664
665         while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
666                 msleep(1);
667
668         if (netif_running(adapter->netdev))
669                 e1000_down(adapter);
670
671         tx_old = adapter->tx_ring;
672         rx_old = adapter->rx_ring;
673
674         err = -ENOMEM;
675         txdr = kzalloc(tx_ring_size, GFP_KERNEL);
676         if (!txdr)
677                 goto err_alloc_tx;
678
679         rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
680         if (!rxdr)
681                 goto err_alloc_rx;
682
683         adapter->tx_ring = txdr;
684         adapter->rx_ring = rxdr;
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                 adapter->rx_ring = rx_old;
712                 adapter->tx_ring = tx_old;
713                 e1000_free_all_rx_resources(adapter);
714                 e1000_free_all_tx_resources(adapter);
715                 kfree(tx_old);
716                 kfree(rx_old);
717                 adapter->rx_ring = rxdr;
718                 adapter->tx_ring = txdr;
719                 if ((err = e1000_up(adapter)))
720                         goto err_setup;
721         }
722
723         clear_bit(__E1000_RESETTING, &adapter->flags);
724         return 0;
725 err_setup_tx:
726         e1000_free_all_rx_resources(adapter);
727 err_setup_rx:
728         adapter->rx_ring = rx_old;
729         adapter->tx_ring = tx_old;
730         kfree(rxdr);
731 err_alloc_rx:
732         kfree(txdr);
733 err_alloc_tx:
734         e1000_up(adapter);
735 err_setup:
736         clear_bit(__E1000_RESETTING, &adapter->flags);
737         return err;
738 }
739
740 #define REG_PATTERN_TEST(R, M, W)                                              \
741 {                                                                              \
742         uint32_t pat, value;                                                   \
743         uint32_t test[] =                                                      \
744                 {0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};              \
745         for (pat = 0; pat < sizeof(test)/sizeof(test[0]); pat++) {              \
746                 E1000_WRITE_REG(&adapter->hw, R, (test[pat] & W));             \
747                 value = E1000_READ_REG(&adapter->hw, R);                       \
748                 if (value != (test[pat] & W & M)) {                             \
749                         DPRINTK(DRV, ERR, "pattern test reg %04X failed: got " \
750                                 "0x%08X expected 0x%08X\n",                    \
751                                 E1000_##R, value, (test[pat] & W & M));        \
752                         *data = (adapter->hw.mac_type < e1000_82543) ?         \
753                                 E1000_82542_##R : E1000_##R;                   \
754                         return 1;                                              \
755                 }                                                              \
756         }                                                                      \
757 }
758
759 #define REG_SET_AND_CHECK(R, M, W)                                             \
760 {                                                                              \
761         uint32_t value;                                                        \
762         E1000_WRITE_REG(&adapter->hw, R, W & M);                               \
763         value = E1000_READ_REG(&adapter->hw, R);                               \
764         if ((W & M) != (value & M)) {                                          \
765                 DPRINTK(DRV, ERR, "set/check reg %04X test failed: got 0x%08X "\
766                         "expected 0x%08X\n", E1000_##R, (value & M), (W & M)); \
767                 *data = (adapter->hw.mac_type < e1000_82543) ?                 \
768                         E1000_82542_##R : E1000_##R;                           \
769                 return 1;                                                      \
770         }                                                                      \
771 }
772
773 static int
774 e1000_reg_test(struct e1000_adapter *adapter, uint64_t *data)
775 {
776         uint32_t value, before, after;
777         uint32_t i, toggle;
778
779         /* The status register is Read Only, so a write should fail.
780          * Some bits that get toggled are ignored.
781          */
782         switch (adapter->hw.mac_type) {
783         /* there are several bits on newer hardware that are r/w */
784         case e1000_82571:
785         case e1000_82572:
786         case e1000_80003es2lan:
787                 toggle = 0x7FFFF3FF;
788                 break;
789         case e1000_82573:
790         case e1000_ich8lan:
791                 toggle = 0x7FFFF033;
792                 break;
793         default:
794                 toggle = 0xFFFFF833;
795                 break;
796         }
797
798         before = E1000_READ_REG(&adapter->hw, STATUS);
799         value = (E1000_READ_REG(&adapter->hw, STATUS) & toggle);
800         E1000_WRITE_REG(&adapter->hw, STATUS, toggle);
801         after = E1000_READ_REG(&adapter->hw, STATUS) & toggle;
802         if (value != after) {
803                 DPRINTK(DRV, ERR, "failed STATUS register test got: "
804                         "0x%08X expected: 0x%08X\n", after, value);
805                 *data = 1;
806                 return 1;
807         }
808         /* restore previous status */
809         E1000_WRITE_REG(&adapter->hw, STATUS, before);
810
811         if (adapter->hw.mac_type != e1000_ich8lan) {
812                 REG_PATTERN_TEST(FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
813                 REG_PATTERN_TEST(FCAH, 0x0000FFFF, 0xFFFFFFFF);
814                 REG_PATTERN_TEST(FCT, 0x0000FFFF, 0xFFFFFFFF);
815                 REG_PATTERN_TEST(VET, 0x0000FFFF, 0xFFFFFFFF);
816         }
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
831         before = (adapter->hw.mac_type == e1000_ich8lan ?
832                   0x06C3B33E : 0x06DFB3FE);
833         REG_SET_AND_CHECK(RCTL, before, 0x003FFFFB);
834         REG_SET_AND_CHECK(TCTL, 0xFFFFFFFF, 0x00000000);
835
836         if (adapter->hw.mac_type >= e1000_82543) {
837
838                 REG_SET_AND_CHECK(RCTL, before, 0xFFFFFFFF);
839                 REG_PATTERN_TEST(RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
840                 if (adapter->hw.mac_type != e1000_ich8lan)
841                         REG_PATTERN_TEST(TXCW, 0xC000FFFF, 0x0000FFFF);
842                 REG_PATTERN_TEST(TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
843                 REG_PATTERN_TEST(TIDV, 0x0000FFFF, 0x0000FFFF);
844                 value = (adapter->hw.mac_type == e1000_ich8lan ?
845                          E1000_RAR_ENTRIES_ICH8LAN : E1000_RAR_ENTRIES);
846                 for (i = 0; i < value; i++) {
847                         REG_PATTERN_TEST(RA + (((i << 1) + 1) << 2), 0x8003FFFF,
848                                          0xFFFFFFFF);
849                 }
850
851         } else {
852
853                 REG_SET_AND_CHECK(RCTL, 0xFFFFFFFF, 0x01FFFFFF);
854                 REG_PATTERN_TEST(RDBAL, 0xFFFFF000, 0xFFFFFFFF);
855                 REG_PATTERN_TEST(TXCW, 0x0000FFFF, 0x0000FFFF);
856                 REG_PATTERN_TEST(TDBAL, 0xFFFFF000, 0xFFFFFFFF);
857
858         }
859
860         value = (adapter->hw.mac_type == e1000_ich8lan ?
861                         E1000_MC_TBL_SIZE_ICH8LAN : E1000_MC_TBL_SIZE);
862         for (i = 0; i < value; i++)
863                 REG_PATTERN_TEST(MTA + (i << 2), 0xFFFFFFFF, 0xFFFFFFFF);
864
865         *data = 0;
866         return 0;
867 }
868
869 static int
870 e1000_eeprom_test(struct e1000_adapter *adapter, uint64_t *data)
871 {
872         uint16_t temp;
873         uint16_t checksum = 0;
874         uint16_t i;
875
876         *data = 0;
877         /* Read and add up the contents of the EEPROM */
878         for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
879                 if ((e1000_read_eeprom(&adapter->hw, i, 1, &temp)) < 0) {
880                         *data = 1;
881                         break;
882                 }
883                 checksum += temp;
884         }
885
886         /* If Checksum is not Correct return error else test passed */
887         if ((checksum != (uint16_t) EEPROM_SUM) && !(*data))
888                 *data = 2;
889
890         return *data;
891 }
892
893 static irqreturn_t
894 e1000_test_intr(int irq, void *data)
895 {
896         struct net_device *netdev = (struct net_device *) data;
897         struct e1000_adapter *adapter = netdev_priv(netdev);
898
899         adapter->test_icr |= E1000_READ_REG(&adapter->hw, ICR);
900
901         return IRQ_HANDLED;
902 }
903
904 static int
905 e1000_intr_test(struct e1000_adapter *adapter, uint64_t *data)
906 {
907         struct net_device *netdev = adapter->netdev;
908         uint32_t mask, i=0, shared_int = TRUE;
909         uint32_t irq = adapter->pdev->irq;
910
911         *data = 0;
912
913         /* NOTE: we don't test MSI interrupts here, yet */
914         /* Hook up test interrupt handler just for this test */
915         if (!request_irq(irq, &e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
916                          netdev))
917                 shared_int = FALSE;
918         else if (request_irq(irq, &e1000_test_intr, IRQF_SHARED,
919                  netdev->name, netdev)) {
920                 *data = 1;
921                 return -1;
922         }
923         DPRINTK(HW, INFO, "testing %s interrupt\n",
924                 (shared_int ? "shared" : "unshared"));
925
926         /* Disable all the interrupts */
927         E1000_WRITE_REG(&adapter->hw, IMC, 0xFFFFFFFF);
928         msleep(10);
929
930         /* Test each interrupt */
931         for (; i < 10; i++) {
932
933                 if (adapter->hw.mac_type == e1000_ich8lan && i == 8)
934                         continue;
935
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_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_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                 /* Online 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_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_82542:
1703         case E1000_DEV_ID_82543GC_FIBER:
1704         case E1000_DEV_ID_82543GC_COPPER:
1705         case E1000_DEV_ID_82544EI_FIBER:
1706         case E1000_DEV_ID_82546EB_QUAD_COPPER:
1707         case E1000_DEV_ID_82545EM_FIBER:
1708         case E1000_DEV_ID_82545EM_COPPER:
1709         case E1000_DEV_ID_82546GB_QUAD_COPPER:
1710         case E1000_DEV_ID_82546GB_PCIE:
1711                 /* these don't support WoL at all */
1712                 wol->supported = 0;
1713                 break;
1714         case E1000_DEV_ID_82546EB_FIBER:
1715         case E1000_DEV_ID_82546GB_FIBER:
1716         case E1000_DEV_ID_82571EB_FIBER:
1717         case E1000_DEV_ID_82571EB_SERDES:
1718         case E1000_DEV_ID_82571EB_COPPER:
1719                 /* Wake events not supported on port B */
1720                 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1) {
1721                         wol->supported = 0;
1722                         break;
1723                 }
1724                 /* return success for non excluded adapter ports */
1725                 retval = 0;
1726                 break;
1727         case E1000_DEV_ID_82571EB_QUAD_COPPER:
1728         case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1729         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1730                 /* quad port adapters only support WoL on port A */
1731                 if (!adapter->quad_port_a) {
1732                         wol->supported = 0;
1733                         break;
1734                 }
1735                 /* return success for non excluded adapter ports */
1736                 retval = 0;
1737                 break;
1738         default:
1739                 /* dual port cards only support WoL on port A from now on
1740                  * unless it was enabled in the eeprom for port B
1741                  * so exclude FUNC_1 ports from having WoL enabled */
1742                 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1 &&
1743                     !adapter->eeprom_wol) {
1744                         wol->supported = 0;
1745                         break;
1746                 }
1747
1748                 retval = 0;
1749         }
1750
1751         return retval;
1752 }
1753
1754 static void
1755 e1000_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1756 {
1757         struct e1000_adapter *adapter = netdev_priv(netdev);
1758
1759         wol->supported = WAKE_UCAST | WAKE_MCAST |
1760                          WAKE_BCAST | WAKE_MAGIC;
1761         wol->wolopts = 0;
1762
1763         /* this function will set ->supported = 0 and return 1 if wol is not
1764          * supported by this hardware */
1765         if (e1000_wol_exclusion(adapter, wol))
1766                 return;
1767
1768         /* apply any specific unsupported masks here */
1769         switch (adapter->hw.device_id) {
1770         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1771                 /* KSP3 does not suppport UCAST wake-ups */
1772                 wol->supported &= ~WAKE_UCAST;
1773
1774                 if (adapter->wol & E1000_WUFC_EX)
1775                         DPRINTK(DRV, ERR, "Interface does not support "
1776                         "directed (unicast) frame wake-up packets\n");
1777                 break;
1778         default:
1779                 break;
1780         }
1781
1782         if (adapter->wol & E1000_WUFC_EX)
1783                 wol->wolopts |= WAKE_UCAST;
1784         if (adapter->wol & E1000_WUFC_MC)
1785                 wol->wolopts |= WAKE_MCAST;
1786         if (adapter->wol & E1000_WUFC_BC)
1787                 wol->wolopts |= WAKE_BCAST;
1788         if (adapter->wol & E1000_WUFC_MAG)
1789                 wol->wolopts |= WAKE_MAGIC;
1790
1791         return;
1792 }
1793
1794 static int
1795 e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1796 {
1797         struct e1000_adapter *adapter = netdev_priv(netdev);
1798         struct e1000_hw *hw = &adapter->hw;
1799
1800         if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
1801                 return -EOPNOTSUPP;
1802
1803         if (e1000_wol_exclusion(adapter, wol))
1804                 return wol->wolopts ? -EOPNOTSUPP : 0;
1805
1806         switch (hw->device_id) {
1807         case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1808                 if (wol->wolopts & WAKE_UCAST) {
1809                         DPRINTK(DRV, ERR, "Interface does not support "
1810                         "directed (unicast) frame wake-up packets\n");
1811                         return -EOPNOTSUPP;
1812                 }
1813                 break;
1814         default:
1815                 break;
1816         }
1817
1818         /* these settings will always override what we currently have */
1819         adapter->wol = 0;
1820
1821         if (wol->wolopts & WAKE_UCAST)
1822                 adapter->wol |= E1000_WUFC_EX;
1823         if (wol->wolopts & WAKE_MCAST)
1824                 adapter->wol |= E1000_WUFC_MC;
1825         if (wol->wolopts & WAKE_BCAST)
1826                 adapter->wol |= E1000_WUFC_BC;
1827         if (wol->wolopts & WAKE_MAGIC)
1828                 adapter->wol |= E1000_WUFC_MAG;
1829
1830         return 0;
1831 }
1832
1833 /* toggle LED 4 times per second = 2 "blinks" per second */
1834 #define E1000_ID_INTERVAL       (HZ/4)
1835
1836 /* bit defines for adapter->led_status */
1837 #define E1000_LED_ON            0
1838
1839 static void
1840 e1000_led_blink_callback(unsigned long data)
1841 {
1842         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1843
1844         if (test_and_change_bit(E1000_LED_ON, &adapter->led_status))
1845                 e1000_led_off(&adapter->hw);
1846         else
1847                 e1000_led_on(&adapter->hw);
1848
1849         mod_timer(&adapter->blink_timer, jiffies + E1000_ID_INTERVAL);
1850 }
1851
1852 static int
1853 e1000_phys_id(struct net_device *netdev, uint32_t data)
1854 {
1855         struct e1000_adapter *adapter = netdev_priv(netdev);
1856
1857         if (!data || data > (uint32_t)(MAX_SCHEDULE_TIMEOUT / HZ))
1858                 data = (uint32_t)(MAX_SCHEDULE_TIMEOUT / HZ);
1859
1860         if (adapter->hw.mac_type < e1000_82571) {
1861                 if (!adapter->blink_timer.function) {
1862                         init_timer(&adapter->blink_timer);
1863                         adapter->blink_timer.function = e1000_led_blink_callback;
1864                         adapter->blink_timer.data = (unsigned long) adapter;
1865                 }
1866                 e1000_setup_led(&adapter->hw);
1867                 mod_timer(&adapter->blink_timer, jiffies);
1868                 msleep_interruptible(data * 1000);
1869                 del_timer_sync(&adapter->blink_timer);
1870         } else if (adapter->hw.phy_type == e1000_phy_ife) {
1871                 if (!adapter->blink_timer.function) {
1872                         init_timer(&adapter->blink_timer);
1873                         adapter->blink_timer.function = e1000_led_blink_callback;
1874                         adapter->blink_timer.data = (unsigned long) adapter;
1875                 }
1876                 mod_timer(&adapter->blink_timer, jiffies);
1877                 msleep_interruptible(data * 1000);
1878                 del_timer_sync(&adapter->blink_timer);
1879                 e1000_write_phy_reg(&(adapter->hw), IFE_PHY_SPECIAL_CONTROL_LED, 0);
1880         } else {
1881                 e1000_blink_led_start(&adapter->hw);
1882                 msleep_interruptible(data * 1000);
1883         }
1884
1885         e1000_led_off(&adapter->hw);
1886         clear_bit(E1000_LED_ON, &adapter->led_status);
1887         e1000_cleanup_led(&adapter->hw);
1888
1889         return 0;
1890 }
1891
1892 static int
1893 e1000_nway_reset(struct net_device *netdev)
1894 {
1895         struct e1000_adapter *adapter = netdev_priv(netdev);
1896         if (netif_running(netdev))
1897                 e1000_reinit_locked(adapter);
1898         return 0;
1899 }
1900
1901 static int
1902 e1000_get_stats_count(struct net_device *netdev)
1903 {
1904         return E1000_STATS_LEN;
1905 }
1906
1907 static void
1908 e1000_get_ethtool_stats(struct net_device *netdev,
1909                 struct ethtool_stats *stats, uint64_t *data)
1910 {
1911         struct e1000_adapter *adapter = netdev_priv(netdev);
1912         int i;
1913
1914         e1000_update_stats(adapter);
1915         for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1916                 char *p = (char *)adapter+e1000_gstrings_stats[i].stat_offset;
1917                 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
1918                         sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1919         }
1920 /*      BUG_ON(i != E1000_STATS_LEN); */
1921 }
1922
1923 static void
1924 e1000_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data)
1925 {
1926         uint8_t *p = data;
1927         int i;
1928
1929         switch (stringset) {
1930         case ETH_SS_TEST:
1931                 memcpy(data, *e1000_gstrings_test,
1932                         E1000_TEST_LEN*ETH_GSTRING_LEN);
1933                 break;
1934         case ETH_SS_STATS:
1935                 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1936                         memcpy(p, e1000_gstrings_stats[i].stat_string,
1937                                ETH_GSTRING_LEN);
1938                         p += ETH_GSTRING_LEN;
1939                 }
1940 /*              BUG_ON(p - data != E1000_STATS_LEN * ETH_GSTRING_LEN); */
1941                 break;
1942         }
1943 }
1944
1945 static const struct ethtool_ops e1000_ethtool_ops = {
1946         .get_settings           = e1000_get_settings,
1947         .set_settings           = e1000_set_settings,
1948         .get_drvinfo            = e1000_get_drvinfo,
1949         .get_regs_len           = e1000_get_regs_len,
1950         .get_regs               = e1000_get_regs,
1951         .get_wol                = e1000_get_wol,
1952         .set_wol                = e1000_set_wol,
1953         .get_msglevel           = e1000_get_msglevel,
1954         .set_msglevel           = e1000_set_msglevel,
1955         .nway_reset             = e1000_nway_reset,
1956         .get_link               = ethtool_op_get_link,
1957         .get_eeprom_len         = e1000_get_eeprom_len,
1958         .get_eeprom             = e1000_get_eeprom,
1959         .set_eeprom             = e1000_set_eeprom,
1960         .get_ringparam          = e1000_get_ringparam,
1961         .set_ringparam          = e1000_set_ringparam,
1962         .get_pauseparam         = e1000_get_pauseparam,
1963         .set_pauseparam         = e1000_set_pauseparam,
1964         .get_rx_csum            = e1000_get_rx_csum,
1965         .set_rx_csum            = e1000_set_rx_csum,
1966         .get_tx_csum            = e1000_get_tx_csum,
1967         .set_tx_csum            = e1000_set_tx_csum,
1968         .get_sg                 = ethtool_op_get_sg,
1969         .set_sg                 = ethtool_op_set_sg,
1970         .get_tso                = ethtool_op_get_tso,
1971         .set_tso                = e1000_set_tso,
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 }