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