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