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