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