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