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