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