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