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