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