Merge branch 'topic/pcsp-fix' into topic/misc
[linux-2.6] / 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
436         /* ethtool doesn't use anything past this point, so all this
437          * code is likely legacy junk for apps that may or may not
438          * exist */
439         if (hw->phy.type == e1000_phy_m88) {
440                 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
441                 regs_buff[13] = (u32)phy_data; /* cable length */
442                 regs_buff[14] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
443                 regs_buff[15] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
444                 regs_buff[16] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
445                 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
446                 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
447                 regs_buff[18] = regs_buff[13]; /* cable polarity */
448                 regs_buff[19] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
449                 regs_buff[20] = regs_buff[17]; /* polarity correction */
450                 /* phy receive errors */
451                 regs_buff[22] = adapter->phy_stats.receive_errors;
452                 regs_buff[23] = regs_buff[13]; /* mdix mode */
453         }
454         regs_buff[21] = 0; /* was idle_errors */
455         e1e_rphy(hw, PHY_1000T_STATUS, &phy_data);
456         regs_buff[24] = (u32)phy_data;  /* phy local receiver status */
457         regs_buff[25] = regs_buff[24];  /* phy remote receiver status */
458 }
459
460 static int e1000_get_eeprom_len(struct net_device *netdev)
461 {
462         struct e1000_adapter *adapter = netdev_priv(netdev);
463         return adapter->hw.nvm.word_size * 2;
464 }
465
466 static int e1000_get_eeprom(struct net_device *netdev,
467                             struct ethtool_eeprom *eeprom, u8 *bytes)
468 {
469         struct e1000_adapter *adapter = netdev_priv(netdev);
470         struct e1000_hw *hw = &adapter->hw;
471         u16 *eeprom_buff;
472         int first_word;
473         int last_word;
474         int ret_val = 0;
475         u16 i;
476
477         if (eeprom->len == 0)
478                 return -EINVAL;
479
480         eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16);
481
482         first_word = eeprom->offset >> 1;
483         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
484
485         eeprom_buff = kmalloc(sizeof(u16) *
486                         (last_word - first_word + 1), GFP_KERNEL);
487         if (!eeprom_buff)
488                 return -ENOMEM;
489
490         if (hw->nvm.type == e1000_nvm_eeprom_spi) {
491                 ret_val = e1000_read_nvm(hw, first_word,
492                                          last_word - first_word + 1,
493                                          eeprom_buff);
494         } else {
495                 for (i = 0; i < last_word - first_word + 1; i++) {
496                         ret_val = e1000_read_nvm(hw, first_word + i, 1,
497                                                       &eeprom_buff[i]);
498                         if (ret_val) {
499                                 /* a read error occurred, throw away the
500                                  * result */
501                                 memset(eeprom_buff, 0xff, sizeof(eeprom_buff));
502                                 break;
503                         }
504                 }
505         }
506
507         /* Device's eeprom is always little-endian, word addressable */
508         for (i = 0; i < last_word - first_word + 1; i++)
509                 le16_to_cpus(&eeprom_buff[i]);
510
511         memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
512         kfree(eeprom_buff);
513
514         return ret_val;
515 }
516
517 static int e1000_set_eeprom(struct net_device *netdev,
518                             struct ethtool_eeprom *eeprom, u8 *bytes)
519 {
520         struct e1000_adapter *adapter = netdev_priv(netdev);
521         struct e1000_hw *hw = &adapter->hw;
522         u16 *eeprom_buff;
523         void *ptr;
524         int max_len;
525         int first_word;
526         int last_word;
527         int ret_val = 0;
528         u16 i;
529
530         if (eeprom->len == 0)
531                 return -EOPNOTSUPP;
532
533         if (eeprom->magic != (adapter->pdev->vendor | (adapter->pdev->device << 16)))
534                 return -EFAULT;
535
536         if (adapter->flags & FLAG_READ_ONLY_NVM)
537                 return -EINVAL;
538
539         max_len = hw->nvm.word_size * 2;
540
541         first_word = eeprom->offset >> 1;
542         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
543         eeprom_buff = kmalloc(max_len, GFP_KERNEL);
544         if (!eeprom_buff)
545                 return -ENOMEM;
546
547         ptr = (void *)eeprom_buff;
548
549         if (eeprom->offset & 1) {
550                 /* need read/modify/write of first changed EEPROM word */
551                 /* only the second byte of the word is being modified */
552                 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
553                 ptr++;
554         }
555         if (((eeprom->offset + eeprom->len) & 1) && (ret_val == 0))
556                 /* need read/modify/write of last changed EEPROM word */
557                 /* only the first byte of the word is being modified */
558                 ret_val = e1000_read_nvm(hw, last_word, 1,
559                                   &eeprom_buff[last_word - first_word]);
560
561         /* Device's eeprom is always little-endian, word addressable */
562         for (i = 0; i < last_word - first_word + 1; i++)
563                 le16_to_cpus(&eeprom_buff[i]);
564
565         memcpy(ptr, bytes, eeprom->len);
566
567         for (i = 0; i < last_word - first_word + 1; i++)
568                 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
569
570         ret_val = e1000_write_nvm(hw, first_word,
571                                   last_word - first_word + 1, eeprom_buff);
572
573         /*
574          * Update the checksum over the first part of the EEPROM if needed
575          * and flush shadow RAM for 82573 controllers
576          */
577         if ((ret_val == 0) && ((first_word <= NVM_CHECKSUM_REG) ||
578                                (hw->mac.type == e1000_82574) ||
579                                (hw->mac.type == e1000_82573)))
580                 e1000e_update_nvm_checksum(hw);
581
582         kfree(eeprom_buff);
583         return ret_val;
584 }
585
586 static void e1000_get_drvinfo(struct net_device *netdev,
587                               struct ethtool_drvinfo *drvinfo)
588 {
589         struct e1000_adapter *adapter = netdev_priv(netdev);
590         char firmware_version[32];
591         u16 eeprom_data;
592
593         strncpy(drvinfo->driver,  e1000e_driver_name, 32);
594         strncpy(drvinfo->version, e1000e_driver_version, 32);
595
596         /*
597          * EEPROM image version # is reported as firmware version # for
598          * PCI-E controllers
599          */
600         e1000_read_nvm(&adapter->hw, 5, 1, &eeprom_data);
601         sprintf(firmware_version, "%d.%d-%d",
602                 (eeprom_data & 0xF000) >> 12,
603                 (eeprom_data & 0x0FF0) >> 4,
604                 eeprom_data & 0x000F);
605
606         strncpy(drvinfo->fw_version, firmware_version, 32);
607         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
608         drvinfo->regdump_len = e1000_get_regs_len(netdev);
609         drvinfo->eedump_len = e1000_get_eeprom_len(netdev);
610 }
611
612 static void e1000_get_ringparam(struct net_device *netdev,
613                                 struct ethtool_ringparam *ring)
614 {
615         struct e1000_adapter *adapter = netdev_priv(netdev);
616         struct e1000_ring *tx_ring = adapter->tx_ring;
617         struct e1000_ring *rx_ring = adapter->rx_ring;
618
619         ring->rx_max_pending = E1000_MAX_RXD;
620         ring->tx_max_pending = E1000_MAX_TXD;
621         ring->rx_mini_max_pending = 0;
622         ring->rx_jumbo_max_pending = 0;
623         ring->rx_pending = rx_ring->count;
624         ring->tx_pending = tx_ring->count;
625         ring->rx_mini_pending = 0;
626         ring->rx_jumbo_pending = 0;
627 }
628
629 static int e1000_set_ringparam(struct net_device *netdev,
630                                struct ethtool_ringparam *ring)
631 {
632         struct e1000_adapter *adapter = netdev_priv(netdev);
633         struct e1000_ring *tx_ring, *tx_old;
634         struct e1000_ring *rx_ring, *rx_old;
635         int err;
636
637         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
638                 return -EINVAL;
639
640         while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
641                 msleep(1);
642
643         if (netif_running(adapter->netdev))
644                 e1000e_down(adapter);
645
646         tx_old = adapter->tx_ring;
647         rx_old = adapter->rx_ring;
648
649         err = -ENOMEM;
650         tx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
651         if (!tx_ring)
652                 goto err_alloc_tx;
653         /*
654          * use a memcpy to save any previously configured
655          * items like napi structs from having to be
656          * reinitialized
657          */
658         memcpy(tx_ring, tx_old, sizeof(struct e1000_ring));
659
660         rx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
661         if (!rx_ring)
662                 goto err_alloc_rx;
663         memcpy(rx_ring, rx_old, sizeof(struct e1000_ring));
664
665         adapter->tx_ring = tx_ring;
666         adapter->rx_ring = rx_ring;
667
668         rx_ring->count = max(ring->rx_pending, (u32)E1000_MIN_RXD);
669         rx_ring->count = min(rx_ring->count, (u32)(E1000_MAX_RXD));
670         rx_ring->count = ALIGN(rx_ring->count, REQ_RX_DESCRIPTOR_MULTIPLE);
671
672         tx_ring->count = max(ring->tx_pending, (u32)E1000_MIN_TXD);
673         tx_ring->count = min(tx_ring->count, (u32)(E1000_MAX_TXD));
674         tx_ring->count = ALIGN(tx_ring->count, REQ_TX_DESCRIPTOR_MULTIPLE);
675
676         if (netif_running(adapter->netdev)) {
677                 /* Try to get new resources before deleting old */
678                 err = e1000e_setup_rx_resources(adapter);
679                 if (err)
680                         goto err_setup_rx;
681                 err = e1000e_setup_tx_resources(adapter);
682                 if (err)
683                         goto err_setup_tx;
684
685                 /*
686                  * restore the old in order to free it,
687                  * then add in the new
688                  */
689                 adapter->rx_ring = rx_old;
690                 adapter->tx_ring = tx_old;
691                 e1000e_free_rx_resources(adapter);
692                 e1000e_free_tx_resources(adapter);
693                 kfree(tx_old);
694                 kfree(rx_old);
695                 adapter->rx_ring = rx_ring;
696                 adapter->tx_ring = tx_ring;
697                 err = e1000e_up(adapter);
698                 if (err)
699                         goto err_setup;
700         }
701
702         clear_bit(__E1000_RESETTING, &adapter->state);
703         return 0;
704 err_setup_tx:
705         e1000e_free_rx_resources(adapter);
706 err_setup_rx:
707         adapter->rx_ring = rx_old;
708         adapter->tx_ring = tx_old;
709         kfree(rx_ring);
710 err_alloc_rx:
711         kfree(tx_ring);
712 err_alloc_tx:
713         e1000e_up(adapter);
714 err_setup:
715         clear_bit(__E1000_RESETTING, &adapter->state);
716         return err;
717 }
718
719 static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
720                              int reg, int offset, u32 mask, u32 write)
721 {
722         u32 pat, val;
723         static const u32 test[] =
724                 {0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
725         for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
726                 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
727                                       (test[pat] & write));
728                 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
729                 if (val != (test[pat] & write & mask)) {
730                         e_err("pattern test reg %04X failed: got 0x%08X "
731                               "expected 0x%08X\n", reg + offset, val,
732                               (test[pat] & write & mask));
733                         *data = reg;
734                         return 1;
735                 }
736         }
737         return 0;
738 }
739
740 static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
741                               int reg, u32 mask, u32 write)
742 {
743         u32 val;
744         __ew32(&adapter->hw, reg, write & mask);
745         val = __er32(&adapter->hw, reg);
746         if ((write & mask) != (val & mask)) {
747                 e_err("set/check reg %04X test failed: got 0x%08X "
748                       "expected 0x%08X\n", reg, (val & mask), (write & mask));
749                 *data = reg;
750                 return 1;
751         }
752         return 0;
753 }
754 #define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write)                       \
755         do {                                                                   \
756                 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
757                         return 1;                                              \
758         } while (0)
759 #define REG_PATTERN_TEST(reg, mask, write)                                     \
760         REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
761
762 #define REG_SET_AND_CHECK(reg, mask, write)                                    \
763         do {                                                                   \
764                 if (reg_set_and_check(adapter, data, reg, mask, write))        \
765                         return 1;                                              \
766         } while (0)
767
768 static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
769 {
770         struct e1000_hw *hw = &adapter->hw;
771         struct e1000_mac_info *mac = &adapter->hw.mac;
772         u32 value;
773         u32 before;
774         u32 after;
775         u32 i;
776         u32 toggle;
777
778         /*
779          * The status register is Read Only, so a write should fail.
780          * Some bits that get toggled are ignored.
781          */
782         switch (mac->type) {
783         /* there are several bits on newer hardware that are r/w */
784         case e1000_82571:
785         case e1000_82572:
786         case e1000_80003es2lan:
787                 toggle = 0x7FFFF3FF;
788                 break;
789         case e1000_82573:
790         case e1000_82574:
791         case e1000_ich8lan:
792         case e1000_ich9lan:
793         case e1000_ich10lan:
794                 toggle = 0x7FFFF033;
795                 break;
796         default:
797                 toggle = 0xFFFFF833;
798                 break;
799         }
800
801         before = er32(STATUS);
802         value = (er32(STATUS) & toggle);
803         ew32(STATUS, toggle);
804         after = er32(STATUS) & toggle;
805         if (value != after) {
806                 e_err("failed STATUS register test got: 0x%08X expected: "
807                       "0x%08X\n", after, value);
808                 *data = 1;
809                 return 1;
810         }
811         /* restore previous status */
812         ew32(STATUS, before);
813
814         if (!(adapter->flags & FLAG_IS_ICH)) {
815                 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
816                 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
817                 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
818                 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
819         }
820
821         REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
822         REG_PATTERN_TEST(E1000_RDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
823         REG_PATTERN_TEST(E1000_RDLEN, 0x000FFF80, 0x000FFFFF);
824         REG_PATTERN_TEST(E1000_RDH, 0x0000FFFF, 0x0000FFFF);
825         REG_PATTERN_TEST(E1000_RDT, 0x0000FFFF, 0x0000FFFF);
826         REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
827         REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
828         REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
829         REG_PATTERN_TEST(E1000_TDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
830         REG_PATTERN_TEST(E1000_TDLEN, 0x000FFF80, 0x000FFFFF);
831
832         REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
833
834         before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
835         REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
836         REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
837
838         REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
839         REG_PATTERN_TEST(E1000_RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
840         if (!(adapter->flags & FLAG_IS_ICH))
841                 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
842         REG_PATTERN_TEST(E1000_TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
843         REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
844         for (i = 0; i < mac->rar_entry_count; i++)
845                 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1),
846                                        ((mac->type == e1000_ich10lan) ?
847                                            0x8007FFFF : 0x8003FFFF),
848                                        0xFFFFFFFF);
849
850         for (i = 0; i < mac->mta_reg_count; i++)
851                 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
852
853         *data = 0;
854         return 0;
855 }
856
857 static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
858 {
859         u16 temp;
860         u16 checksum = 0;
861         u16 i;
862
863         *data = 0;
864         /* Read and add up the contents of the EEPROM */
865         for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
866                 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
867                         *data = 1;
868                         break;
869                 }
870                 checksum += temp;
871         }
872
873         /* If Checksum is not Correct return error else test passed */
874         if ((checksum != (u16) NVM_SUM) && !(*data))
875                 *data = 2;
876
877         return *data;
878 }
879
880 static irqreturn_t e1000_test_intr(int irq, void *data)
881 {
882         struct net_device *netdev = (struct net_device *) data;
883         struct e1000_adapter *adapter = netdev_priv(netdev);
884         struct e1000_hw *hw = &adapter->hw;
885
886         adapter->test_icr |= er32(ICR);
887
888         return IRQ_HANDLED;
889 }
890
891 static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
892 {
893         struct net_device *netdev = adapter->netdev;
894         struct e1000_hw *hw = &adapter->hw;
895         u32 mask;
896         u32 shared_int = 1;
897         u32 irq = adapter->pdev->irq;
898         int i;
899         int ret_val = 0;
900         int int_mode = E1000E_INT_MODE_LEGACY;
901
902         *data = 0;
903
904         /* NOTE: we don't test MSI/MSI-X interrupts here, yet */
905         if (adapter->int_mode == E1000E_INT_MODE_MSIX) {
906                 int_mode = adapter->int_mode;
907                 e1000e_reset_interrupt_capability(adapter);
908                 adapter->int_mode = E1000E_INT_MODE_LEGACY;
909                 e1000e_set_interrupt_capability(adapter);
910         }
911         /* Hook up test interrupt handler just for this test */
912         if (!request_irq(irq, &e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
913                          netdev)) {
914                 shared_int = 0;
915         } else if (request_irq(irq, &e1000_test_intr, IRQF_SHARED,
916                  netdev->name, netdev)) {
917                 *data = 1;
918                 ret_val = -1;
919                 goto out;
920         }
921         e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
922
923         /* Disable all the interrupts */
924         ew32(IMC, 0xFFFFFFFF);
925         msleep(10);
926
927         /* Test each interrupt */
928         for (i = 0; i < 10; i++) {
929                 /* Interrupt to test */
930                 mask = 1 << i;
931
932                 if (adapter->flags & FLAG_IS_ICH) {
933                         switch (mask) {
934                         case E1000_ICR_RXSEQ:
935                                 continue;
936                         case 0x00000100:
937                                 if (adapter->hw.mac.type == e1000_ich8lan ||
938                                     adapter->hw.mac.type == e1000_ich9lan)
939                                         continue;
940                                 break;
941                         default:
942                                 break;
943                         }
944                 }
945
946                 if (!shared_int) {
947                         /*
948                          * Disable 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 posted to the bus, the
952                          * test failed.
953                          */
954                         adapter->test_icr = 0;
955                         ew32(IMC, mask);
956                         ew32(ICS, mask);
957                         msleep(10);
958
959                         if (adapter->test_icr & mask) {
960                                 *data = 3;
961                                 break;
962                         }
963                 }
964
965                 /*
966                  * Enable the interrupt to be reported in
967                  * the cause register and then force the same
968                  * interrupt and see if one gets posted.  If
969                  * an interrupt was not posted to the bus, the
970                  * test failed.
971                  */
972                 adapter->test_icr = 0;
973                 ew32(IMS, mask);
974                 ew32(ICS, mask);
975                 msleep(10);
976
977                 if (!(adapter->test_icr & mask)) {
978                         *data = 4;
979                         break;
980                 }
981
982                 if (!shared_int) {
983                         /*
984                          * Disable the other interrupts to be reported in
985                          * the cause register and then force the other
986                          * interrupts and see if any get posted.  If
987                          * an interrupt was posted to the bus, the
988                          * test failed.
989                          */
990                         adapter->test_icr = 0;
991                         ew32(IMC, ~mask & 0x00007FFF);
992                         ew32(ICS, ~mask & 0x00007FFF);
993                         msleep(10);
994
995                         if (adapter->test_icr) {
996                                 *data = 5;
997                                 break;
998                         }
999                 }
1000         }
1001
1002         /* Disable all the interrupts */
1003         ew32(IMC, 0xFFFFFFFF);
1004         msleep(10);
1005
1006         /* Unhook test interrupt handler */
1007         free_irq(irq, netdev);
1008
1009 out:
1010         if (int_mode == E1000E_INT_MODE_MSIX) {
1011                 e1000e_reset_interrupt_capability(adapter);
1012                 adapter->int_mode = int_mode;
1013                 e1000e_set_interrupt_capability(adapter);
1014         }
1015
1016         return ret_val;
1017 }
1018
1019 static void e1000_free_desc_rings(struct e1000_adapter *adapter)
1020 {
1021         struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1022         struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1023         struct pci_dev *pdev = adapter->pdev;
1024         int i;
1025
1026         if (tx_ring->desc && tx_ring->buffer_info) {
1027                 for (i = 0; i < tx_ring->count; i++) {
1028                         if (tx_ring->buffer_info[i].dma)
1029                                 pci_unmap_single(pdev,
1030                                         tx_ring->buffer_info[i].dma,
1031                                         tx_ring->buffer_info[i].length,
1032                                         PCI_DMA_TODEVICE);
1033                         if (tx_ring->buffer_info[i].skb)
1034                                 dev_kfree_skb(tx_ring->buffer_info[i].skb);
1035                 }
1036         }
1037
1038         if (rx_ring->desc && rx_ring->buffer_info) {
1039                 for (i = 0; i < rx_ring->count; i++) {
1040                         if (rx_ring->buffer_info[i].dma)
1041                                 pci_unmap_single(pdev,
1042                                         rx_ring->buffer_info[i].dma,
1043                                         2048, PCI_DMA_FROMDEVICE);
1044                         if (rx_ring->buffer_info[i].skb)
1045                                 dev_kfree_skb(rx_ring->buffer_info[i].skb);
1046                 }
1047         }
1048
1049         if (tx_ring->desc) {
1050                 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1051                                   tx_ring->dma);
1052                 tx_ring->desc = NULL;
1053         }
1054         if (rx_ring->desc) {
1055                 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1056                                   rx_ring->dma);
1057                 rx_ring->desc = NULL;
1058         }
1059
1060         kfree(tx_ring->buffer_info);
1061         tx_ring->buffer_info = NULL;
1062         kfree(rx_ring->buffer_info);
1063         rx_ring->buffer_info = NULL;
1064 }
1065
1066 static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1067 {
1068         struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1069         struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1070         struct pci_dev *pdev = adapter->pdev;
1071         struct e1000_hw *hw = &adapter->hw;
1072         u32 rctl;
1073         int i;
1074         int ret_val;
1075
1076         /* Setup Tx descriptor ring and Tx buffers */
1077
1078         if (!tx_ring->count)
1079                 tx_ring->count = E1000_DEFAULT_TXD;
1080
1081         tx_ring->buffer_info = kcalloc(tx_ring->count,
1082                                        sizeof(struct e1000_buffer),
1083                                        GFP_KERNEL);
1084         if (!(tx_ring->buffer_info)) {
1085                 ret_val = 1;
1086                 goto err_nomem;
1087         }
1088
1089         tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1090         tx_ring->size = ALIGN(tx_ring->size, 4096);
1091         tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
1092                                            &tx_ring->dma, GFP_KERNEL);
1093         if (!tx_ring->desc) {
1094                 ret_val = 2;
1095                 goto err_nomem;
1096         }
1097         tx_ring->next_to_use = 0;
1098         tx_ring->next_to_clean = 0;
1099
1100         ew32(TDBAL, ((u64) tx_ring->dma & 0x00000000FFFFFFFF));
1101         ew32(TDBAH, ((u64) tx_ring->dma >> 32));
1102         ew32(TDLEN, tx_ring->count * sizeof(struct e1000_tx_desc));
1103         ew32(TDH, 0);
1104         ew32(TDT, 0);
1105         ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN | E1000_TCTL_MULR |
1106              E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1107              E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT);
1108
1109         for (i = 0; i < tx_ring->count; i++) {
1110                 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
1111                 struct sk_buff *skb;
1112                 unsigned int skb_size = 1024;
1113
1114                 skb = alloc_skb(skb_size, GFP_KERNEL);
1115                 if (!skb) {
1116                         ret_val = 3;
1117                         goto err_nomem;
1118                 }
1119                 skb_put(skb, skb_size);
1120                 tx_ring->buffer_info[i].skb = skb;
1121                 tx_ring->buffer_info[i].length = skb->len;
1122                 tx_ring->buffer_info[i].dma =
1123                         pci_map_single(pdev, skb->data, skb->len,
1124                                        PCI_DMA_TODEVICE);
1125                 if (pci_dma_mapping_error(pdev, tx_ring->buffer_info[i].dma)) {
1126                         ret_val = 4;
1127                         goto err_nomem;
1128                 }
1129                 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma);
1130                 tx_desc->lower.data = cpu_to_le32(skb->len);
1131                 tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1132                                                    E1000_TXD_CMD_IFCS |
1133                                                    E1000_TXD_CMD_RS);
1134                 tx_desc->upper.data = 0;
1135         }
1136
1137         /* Setup Rx descriptor ring and Rx buffers */
1138
1139         if (!rx_ring->count)
1140                 rx_ring->count = E1000_DEFAULT_RXD;
1141
1142         rx_ring->buffer_info = kcalloc(rx_ring->count,
1143                                        sizeof(struct e1000_buffer),
1144                                        GFP_KERNEL);
1145         if (!(rx_ring->buffer_info)) {
1146                 ret_val = 5;
1147                 goto err_nomem;
1148         }
1149
1150         rx_ring->size = rx_ring->count * sizeof(struct e1000_rx_desc);
1151         rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
1152                                            &rx_ring->dma, GFP_KERNEL);
1153         if (!rx_ring->desc) {
1154                 ret_val = 6;
1155                 goto err_nomem;
1156         }
1157         rx_ring->next_to_use = 0;
1158         rx_ring->next_to_clean = 0;
1159
1160         rctl = er32(RCTL);
1161         ew32(RCTL, rctl & ~E1000_RCTL_EN);
1162         ew32(RDBAL, ((u64) rx_ring->dma & 0xFFFFFFFF));
1163         ew32(RDBAH, ((u64) rx_ring->dma >> 32));
1164         ew32(RDLEN, rx_ring->size);
1165         ew32(RDH, 0);
1166         ew32(RDT, 0);
1167         rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
1168                 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1169                 E1000_RCTL_SBP | E1000_RCTL_SECRC |
1170                 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1171                 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1172         ew32(RCTL, rctl);
1173
1174         for (i = 0; i < rx_ring->count; i++) {
1175                 struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
1176                 struct sk_buff *skb;
1177
1178                 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1179                 if (!skb) {
1180                         ret_val = 7;
1181                         goto err_nomem;
1182                 }
1183                 skb_reserve(skb, NET_IP_ALIGN);
1184                 rx_ring->buffer_info[i].skb = skb;
1185                 rx_ring->buffer_info[i].dma =
1186                         pci_map_single(pdev, skb->data, 2048,
1187                                        PCI_DMA_FROMDEVICE);
1188                 if (pci_dma_mapping_error(pdev, rx_ring->buffer_info[i].dma)) {
1189                         ret_val = 8;
1190                         goto err_nomem;
1191                 }
1192                 rx_desc->buffer_addr =
1193                         cpu_to_le64(rx_ring->buffer_info[i].dma);
1194                 memset(skb->data, 0x00, skb->len);
1195         }
1196
1197         return 0;
1198
1199 err_nomem:
1200         e1000_free_desc_rings(adapter);
1201         return ret_val;
1202 }
1203
1204 static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1205 {
1206         /* Write out to PHY registers 29 and 30 to disable the Receiver. */
1207         e1e_wphy(&adapter->hw, 29, 0x001F);
1208         e1e_wphy(&adapter->hw, 30, 0x8FFC);
1209         e1e_wphy(&adapter->hw, 29, 0x001A);
1210         e1e_wphy(&adapter->hw, 30, 0x8FF0);
1211 }
1212
1213 static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1214 {
1215         struct e1000_hw *hw = &adapter->hw;
1216         u32 ctrl_reg = 0;
1217         u32 stat_reg = 0;
1218         u16 phy_reg = 0;
1219
1220         hw->mac.autoneg = 0;
1221
1222         if (hw->phy.type == e1000_phy_m88) {
1223                 /* Auto-MDI/MDIX Off */
1224                 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1225                 /* reset to update Auto-MDI/MDIX */
1226                 e1e_wphy(hw, PHY_CONTROL, 0x9140);
1227                 /* autoneg off */
1228                 e1e_wphy(hw, PHY_CONTROL, 0x8140);
1229         } else if (hw->phy.type == e1000_phy_gg82563)
1230                 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
1231
1232         ctrl_reg = er32(CTRL);
1233
1234         switch (hw->phy.type) {
1235         case e1000_phy_ife:
1236                 /* force 100, set loopback */
1237                 e1e_wphy(hw, PHY_CONTROL, 0x6100);
1238
1239                 /* Now set up the MAC to the same speed/duplex as the PHY. */
1240                 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1241                 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1242                              E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1243                              E1000_CTRL_SPD_100 |/* Force Speed to 100 */
1244                              E1000_CTRL_FD);     /* Force Duplex to FULL */
1245                 break;
1246         case e1000_phy_bm:
1247                 /* Set Default MAC Interface speed to 1GB */
1248                 e1e_rphy(hw, PHY_REG(2, 21), &phy_reg);
1249                 phy_reg &= ~0x0007;
1250                 phy_reg |= 0x006;
1251                 e1e_wphy(hw, PHY_REG(2, 21), phy_reg);
1252                 /* Assert SW reset for above settings to take effect */
1253                 e1000e_commit_phy(hw);
1254                 mdelay(1);
1255                 /* Force Full Duplex */
1256                 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1257                 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C);
1258                 /* Set Link Up (in force link) */
1259                 e1e_rphy(hw, PHY_REG(776, 16), &phy_reg);
1260                 e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040);
1261                 /* Force Link */
1262                 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1263                 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040);
1264                 /* Set Early Link Enable */
1265                 e1e_rphy(hw, PHY_REG(769, 20), &phy_reg);
1266                 e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400);
1267                 /* fall through */
1268         default:
1269                 /* force 1000, set loopback */
1270                 e1e_wphy(hw, PHY_CONTROL, 0x4140);
1271                 mdelay(250);
1272
1273                 /* Now set up the MAC to the same speed/duplex as the PHY. */
1274                 ctrl_reg = er32(CTRL);
1275                 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1276                 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1277                              E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1278                              E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1279                              E1000_CTRL_FD);     /* Force Duplex to FULL */
1280
1281                 if (adapter->flags & FLAG_IS_ICH)
1282                         ctrl_reg |= E1000_CTRL_SLU;     /* Set Link Up */
1283         }
1284
1285         if (hw->phy.media_type == e1000_media_type_copper &&
1286             hw->phy.type == e1000_phy_m88) {
1287                 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1288         } else {
1289                 /*
1290                  * Set the ILOS bit on the fiber Nic if half duplex link is
1291                  * detected.
1292                  */
1293                 stat_reg = er32(STATUS);
1294                 if ((stat_reg & E1000_STATUS_FD) == 0)
1295                         ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1296         }
1297
1298         ew32(CTRL, ctrl_reg);
1299
1300         /*
1301          * Disable the receiver on the PHY so when a cable is plugged in, the
1302          * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1303          */
1304         if (hw->phy.type == e1000_phy_m88)
1305                 e1000_phy_disable_receiver(adapter);
1306
1307         udelay(500);
1308
1309         return 0;
1310 }
1311
1312 static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1313 {
1314         struct e1000_hw *hw = &adapter->hw;
1315         u32 ctrl = er32(CTRL);
1316         int link = 0;
1317
1318         /* special requirements for 82571/82572 fiber adapters */
1319
1320         /*
1321          * jump through hoops to make sure link is up because serdes
1322          * link is hardwired up
1323          */
1324         ctrl |= E1000_CTRL_SLU;
1325         ew32(CTRL, ctrl);
1326
1327         /* disable autoneg */
1328         ctrl = er32(TXCW);
1329         ctrl &= ~(1 << 31);
1330         ew32(TXCW, ctrl);
1331
1332         link = (er32(STATUS) & E1000_STATUS_LU);
1333
1334         if (!link) {
1335                 /* set invert loss of signal */
1336                 ctrl = er32(CTRL);
1337                 ctrl |= E1000_CTRL_ILOS;
1338                 ew32(CTRL, ctrl);
1339         }
1340
1341         /*
1342          * special write to serdes control register to enable SerDes analog
1343          * loopback
1344          */
1345 #define E1000_SERDES_LB_ON 0x410
1346         ew32(SCTL, E1000_SERDES_LB_ON);
1347         msleep(10);
1348
1349         return 0;
1350 }
1351
1352 /* only call this for fiber/serdes connections to es2lan */
1353 static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1354 {
1355         struct e1000_hw *hw = &adapter->hw;
1356         u32 ctrlext = er32(CTRL_EXT);
1357         u32 ctrl = er32(CTRL);
1358
1359         /*
1360          * save CTRL_EXT to restore later, reuse an empty variable (unused
1361          * on mac_type 80003es2lan)
1362          */
1363         adapter->tx_fifo_head = ctrlext;
1364
1365         /* clear the serdes mode bits, putting the device into mac loopback */
1366         ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1367         ew32(CTRL_EXT, ctrlext);
1368
1369         /* force speed to 1000/FD, link up */
1370         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1371         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1372                  E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1373         ew32(CTRL, ctrl);
1374
1375         /* set mac loopback */
1376         ctrl = er32(RCTL);
1377         ctrl |= E1000_RCTL_LBM_MAC;
1378         ew32(RCTL, ctrl);
1379
1380         /* set testing mode parameters (no need to reset later) */
1381 #define KMRNCTRLSTA_OPMODE (0x1F << 16)
1382 #define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1383         ew32(KMRNCTRLSTA,
1384              (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
1385
1386         return 0;
1387 }
1388
1389 static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1390 {
1391         struct e1000_hw *hw = &adapter->hw;
1392         u32 rctl;
1393
1394         if (hw->phy.media_type == e1000_media_type_fiber ||
1395             hw->phy.media_type == e1000_media_type_internal_serdes) {
1396                 switch (hw->mac.type) {
1397                 case e1000_80003es2lan:
1398                         return e1000_set_es2lan_mac_loopback(adapter);
1399                         break;
1400                 case e1000_82571:
1401                 case e1000_82572:
1402                         return e1000_set_82571_fiber_loopback(adapter);
1403                         break;
1404                 default:
1405                         rctl = er32(RCTL);
1406                         rctl |= E1000_RCTL_LBM_TCVR;
1407                         ew32(RCTL, rctl);
1408                         return 0;
1409                 }
1410         } else if (hw->phy.media_type == e1000_media_type_copper) {
1411                 return e1000_integrated_phy_loopback(adapter);
1412         }
1413
1414         return 7;
1415 }
1416
1417 static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1418 {
1419         struct e1000_hw *hw = &adapter->hw;
1420         u32 rctl;
1421         u16 phy_reg;
1422
1423         rctl = er32(RCTL);
1424         rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1425         ew32(RCTL, rctl);
1426
1427         switch (hw->mac.type) {
1428         case e1000_80003es2lan:
1429                 if (hw->phy.media_type == e1000_media_type_fiber ||
1430                     hw->phy.media_type == e1000_media_type_internal_serdes) {
1431                         /* restore CTRL_EXT, stealing space from tx_fifo_head */
1432                         ew32(CTRL_EXT, adapter->tx_fifo_head);
1433                         adapter->tx_fifo_head = 0;
1434                 }
1435                 /* fall through */
1436         case e1000_82571:
1437         case e1000_82572:
1438                 if (hw->phy.media_type == e1000_media_type_fiber ||
1439                     hw->phy.media_type == e1000_media_type_internal_serdes) {
1440 #define E1000_SERDES_LB_OFF 0x400
1441                         ew32(SCTL, E1000_SERDES_LB_OFF);
1442                         msleep(10);
1443                         break;
1444                 }
1445                 /* Fall Through */
1446         default:
1447                 hw->mac.autoneg = 1;
1448                 if (hw->phy.type == e1000_phy_gg82563)
1449                         e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1450                 e1e_rphy(hw, PHY_CONTROL, &phy_reg);
1451                 if (phy_reg & MII_CR_LOOPBACK) {
1452                         phy_reg &= ~MII_CR_LOOPBACK;
1453                         e1e_wphy(hw, PHY_CONTROL, phy_reg);
1454                         e1000e_commit_phy(hw);
1455                 }
1456                 break;
1457         }
1458 }
1459
1460 static void e1000_create_lbtest_frame(struct sk_buff *skb,
1461                                       unsigned int frame_size)
1462 {
1463         memset(skb->data, 0xFF, frame_size);
1464         frame_size &= ~1;
1465         memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1466         memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
1467         memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
1468 }
1469
1470 static int e1000_check_lbtest_frame(struct sk_buff *skb,
1471                                     unsigned int frame_size)
1472 {
1473         frame_size &= ~1;
1474         if (*(skb->data + 3) == 0xFF)
1475                 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1476                    (*(skb->data + frame_size / 2 + 12) == 0xAF))
1477                         return 0;
1478         return 13;
1479 }
1480
1481 static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1482 {
1483         struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1484         struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1485         struct pci_dev *pdev = adapter->pdev;
1486         struct e1000_hw *hw = &adapter->hw;
1487         int i, j, k, l;
1488         int lc;
1489         int good_cnt;
1490         int ret_val = 0;
1491         unsigned long time;
1492
1493         ew32(RDT, rx_ring->count - 1);
1494
1495         /*
1496          * Calculate the loop count based on the largest descriptor ring
1497          * The idea is to wrap the largest ring a number of times using 64
1498          * send/receive pairs during each loop
1499          */
1500
1501         if (rx_ring->count <= tx_ring->count)
1502                 lc = ((tx_ring->count / 64) * 2) + 1;
1503         else
1504                 lc = ((rx_ring->count / 64) * 2) + 1;
1505
1506         k = 0;
1507         l = 0;
1508         for (j = 0; j <= lc; j++) { /* loop count loop */
1509                 for (i = 0; i < 64; i++) { /* send the packets */
1510                         e1000_create_lbtest_frame(tx_ring->buffer_info[k].skb,
1511                                                   1024);
1512                         pci_dma_sync_single_for_device(pdev,
1513                                         tx_ring->buffer_info[k].dma,
1514                                         tx_ring->buffer_info[k].length,
1515                                         PCI_DMA_TODEVICE);
1516                         k++;
1517                         if (k == tx_ring->count)
1518                                 k = 0;
1519                 }
1520                 ew32(TDT, k);
1521                 msleep(200);
1522                 time = jiffies; /* set the start time for the receive */
1523                 good_cnt = 0;
1524                 do { /* receive the sent packets */
1525                         pci_dma_sync_single_for_cpu(pdev,
1526                                         rx_ring->buffer_info[l].dma, 2048,
1527                                         PCI_DMA_FROMDEVICE);
1528
1529                         ret_val = e1000_check_lbtest_frame(
1530                                         rx_ring->buffer_info[l].skb, 1024);
1531                         if (!ret_val)
1532                                 good_cnt++;
1533                         l++;
1534                         if (l == rx_ring->count)
1535                                 l = 0;
1536                         /*
1537                          * time + 20 msecs (200 msecs on 2.4) is more than
1538                          * enough time to complete the receives, if it's
1539                          * exceeded, break and error off
1540                          */
1541                 } while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1542                 if (good_cnt != 64) {
1543                         ret_val = 13; /* ret_val is the same as mis-compare */
1544                         break;
1545                 }
1546                 if (jiffies >= (time + 20)) {
1547                         ret_val = 14; /* error code for time out error */
1548                         break;
1549                 }
1550         } /* end loop count loop */
1551         return ret_val;
1552 }
1553
1554 static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1555 {
1556         /*
1557          * PHY loopback cannot be performed if SoL/IDER
1558          * sessions are active
1559          */
1560         if (e1000_check_reset_block(&adapter->hw)) {
1561                 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
1562                 *data = 0;
1563                 goto out;
1564         }
1565
1566         *data = e1000_setup_desc_rings(adapter);
1567         if (*data)
1568                 goto out;
1569
1570         *data = e1000_setup_loopback_test(adapter);
1571         if (*data)
1572                 goto err_loopback;
1573
1574         *data = e1000_run_loopback_test(adapter);
1575         e1000_loopback_cleanup(adapter);
1576
1577 err_loopback:
1578         e1000_free_desc_rings(adapter);
1579 out:
1580         return *data;
1581 }
1582
1583 static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1584 {
1585         struct e1000_hw *hw = &adapter->hw;
1586
1587         *data = 0;
1588         if (hw->phy.media_type == e1000_media_type_internal_serdes) {
1589                 int i = 0;
1590                 hw->mac.serdes_has_link = 0;
1591
1592                 /*
1593                  * On some blade server designs, link establishment
1594                  * could take as long as 2-3 minutes
1595                  */
1596                 do {
1597                         hw->mac.ops.check_for_link(hw);
1598                         if (hw->mac.serdes_has_link)
1599                                 return *data;
1600                         msleep(20);
1601                 } while (i++ < 3750);
1602
1603                 *data = 1;
1604         } else {
1605                 hw->mac.ops.check_for_link(hw);
1606                 if (hw->mac.autoneg)
1607                         msleep(4000);
1608
1609                 if (!(er32(STATUS) &
1610                       E1000_STATUS_LU))
1611                         *data = 1;
1612         }
1613         return *data;
1614 }
1615
1616 static int e1000e_get_sset_count(struct net_device *netdev, int sset)
1617 {
1618         switch (sset) {
1619         case ETH_SS_TEST:
1620                 return E1000_TEST_LEN;
1621         case ETH_SS_STATS:
1622                 return E1000_STATS_LEN;
1623         default:
1624                 return -EOPNOTSUPP;
1625         }
1626 }
1627
1628 static void e1000_diag_test(struct net_device *netdev,
1629                             struct ethtool_test *eth_test, u64 *data)
1630 {
1631         struct e1000_adapter *adapter = netdev_priv(netdev);
1632         u16 autoneg_advertised;
1633         u8 forced_speed_duplex;
1634         u8 autoneg;
1635         bool if_running = netif_running(netdev);
1636
1637         set_bit(__E1000_TESTING, &adapter->state);
1638         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1639                 /* Offline tests */
1640
1641                 /* save speed, duplex, autoneg settings */
1642                 autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1643                 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1644                 autoneg = adapter->hw.mac.autoneg;
1645
1646                 e_info("offline testing starting\n");
1647
1648                 /*
1649                  * Link test performed before hardware reset so autoneg doesn't
1650                  * interfere with test result
1651                  */
1652                 if (e1000_link_test(adapter, &data[4]))
1653                         eth_test->flags |= ETH_TEST_FL_FAILED;
1654
1655                 if (if_running)
1656                         /* indicate we're in test mode */
1657                         dev_close(netdev);
1658                 else
1659                         e1000e_reset(adapter);
1660
1661                 if (e1000_reg_test(adapter, &data[0]))
1662                         eth_test->flags |= ETH_TEST_FL_FAILED;
1663
1664                 e1000e_reset(adapter);
1665                 if (e1000_eeprom_test(adapter, &data[1]))
1666                         eth_test->flags |= ETH_TEST_FL_FAILED;
1667
1668                 e1000e_reset(adapter);
1669                 if (e1000_intr_test(adapter, &data[2]))
1670                         eth_test->flags |= ETH_TEST_FL_FAILED;
1671
1672                 e1000e_reset(adapter);
1673                 /* make sure the phy is powered up */
1674                 e1000e_power_up_phy(adapter);
1675                 if (e1000_loopback_test(adapter, &data[3]))
1676                         eth_test->flags |= ETH_TEST_FL_FAILED;
1677
1678                 /* restore speed, duplex, autoneg settings */
1679                 adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1680                 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1681                 adapter->hw.mac.autoneg = autoneg;
1682
1683                 /* force this routine to wait until autoneg complete/timeout */
1684                 adapter->hw.phy.autoneg_wait_to_complete = 1;
1685                 e1000e_reset(adapter);
1686                 adapter->hw.phy.autoneg_wait_to_complete = 0;
1687
1688                 clear_bit(__E1000_TESTING, &adapter->state);
1689                 if (if_running)
1690                         dev_open(netdev);
1691         } else {
1692                 e_info("online testing starting\n");
1693                 /* Online tests */
1694                 if (e1000_link_test(adapter, &data[4]))
1695                         eth_test->flags |= ETH_TEST_FL_FAILED;
1696
1697                 /* Online tests aren't run; pass by default */
1698                 data[0] = 0;
1699                 data[1] = 0;
1700                 data[2] = 0;
1701                 data[3] = 0;
1702
1703                 clear_bit(__E1000_TESTING, &adapter->state);
1704         }
1705         msleep_interruptible(4 * 1000);
1706 }
1707
1708 static void e1000_get_wol(struct net_device *netdev,
1709                           struct ethtool_wolinfo *wol)
1710 {
1711         struct e1000_adapter *adapter = netdev_priv(netdev);
1712
1713         wol->supported = 0;
1714         wol->wolopts = 0;
1715
1716         if (!(adapter->flags & FLAG_HAS_WOL) ||
1717             !device_can_wakeup(&adapter->pdev->dev))
1718                 return;
1719
1720         wol->supported = WAKE_UCAST | WAKE_MCAST |
1721                          WAKE_BCAST | WAKE_MAGIC |
1722                          WAKE_PHY | WAKE_ARP;
1723
1724         /* apply any specific unsupported masks here */
1725         if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1726                 wol->supported &= ~WAKE_UCAST;
1727
1728                 if (adapter->wol & E1000_WUFC_EX)
1729                         e_err("Interface does not support directed (unicast) "
1730                               "frame wake-up packets\n");
1731         }
1732
1733         if (adapter->wol & E1000_WUFC_EX)
1734                 wol->wolopts |= WAKE_UCAST;
1735         if (adapter->wol & E1000_WUFC_MC)
1736                 wol->wolopts |= WAKE_MCAST;
1737         if (adapter->wol & E1000_WUFC_BC)
1738                 wol->wolopts |= WAKE_BCAST;
1739         if (adapter->wol & E1000_WUFC_MAG)
1740                 wol->wolopts |= WAKE_MAGIC;
1741         if (adapter->wol & E1000_WUFC_LNKC)
1742                 wol->wolopts |= WAKE_PHY;
1743         if (adapter->wol & E1000_WUFC_ARP)
1744                 wol->wolopts |= WAKE_ARP;
1745 }
1746
1747 static int e1000_set_wol(struct net_device *netdev,
1748                          struct ethtool_wolinfo *wol)
1749 {
1750         struct e1000_adapter *adapter = netdev_priv(netdev);
1751
1752         if (wol->wolopts & WAKE_MAGICSECURE)
1753                 return -EOPNOTSUPP;
1754
1755         if (!(adapter->flags & FLAG_HAS_WOL) ||
1756             !device_can_wakeup(&adapter->pdev->dev))
1757                 return wol->wolopts ? -EOPNOTSUPP : 0;
1758
1759         /* these settings will always override what we currently have */
1760         adapter->wol = 0;
1761
1762         if (wol->wolopts & WAKE_UCAST)
1763                 adapter->wol |= E1000_WUFC_EX;
1764         if (wol->wolopts & WAKE_MCAST)
1765                 adapter->wol |= E1000_WUFC_MC;
1766         if (wol->wolopts & WAKE_BCAST)
1767                 adapter->wol |= E1000_WUFC_BC;
1768         if (wol->wolopts & WAKE_MAGIC)
1769                 adapter->wol |= E1000_WUFC_MAG;
1770         if (wol->wolopts & WAKE_PHY)
1771                 adapter->wol |= E1000_WUFC_LNKC;
1772         if (wol->wolopts & WAKE_ARP)
1773                 adapter->wol |= E1000_WUFC_ARP;
1774
1775         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1776
1777         return 0;
1778 }
1779
1780 /* toggle LED 4 times per second = 2 "blinks" per second */
1781 #define E1000_ID_INTERVAL       (HZ/4)
1782
1783 /* bit defines for adapter->led_status */
1784 #define E1000_LED_ON            0
1785
1786 static void e1000_led_blink_callback(unsigned long data)
1787 {
1788         struct e1000_adapter *adapter = (struct e1000_adapter *) data;
1789
1790         if (test_and_change_bit(E1000_LED_ON, &adapter->led_status))
1791                 adapter->hw.mac.ops.led_off(&adapter->hw);
1792         else
1793                 adapter->hw.mac.ops.led_on(&adapter->hw);
1794
1795         mod_timer(&adapter->blink_timer, jiffies + E1000_ID_INTERVAL);
1796 }
1797
1798 static int e1000_phys_id(struct net_device *netdev, u32 data)
1799 {
1800         struct e1000_adapter *adapter = netdev_priv(netdev);
1801         struct e1000_hw *hw = &adapter->hw;
1802
1803         if (!data)
1804                 data = INT_MAX;
1805
1806         if ((hw->phy.type == e1000_phy_ife) ||
1807             (hw->mac.type == e1000_82574)) {
1808                 if (!adapter->blink_timer.function) {
1809                         init_timer(&adapter->blink_timer);
1810                         adapter->blink_timer.function =
1811                                 e1000_led_blink_callback;
1812                         adapter->blink_timer.data = (unsigned long) adapter;
1813                 }
1814                 mod_timer(&adapter->blink_timer, jiffies);
1815                 msleep_interruptible(data * 1000);
1816                 del_timer_sync(&adapter->blink_timer);
1817                 if (hw->phy.type == e1000_phy_ife)
1818                         e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
1819         } else {
1820                 e1000e_blink_led(hw);
1821                 msleep_interruptible(data * 1000);
1822         }
1823
1824         hw->mac.ops.led_off(hw);
1825         clear_bit(E1000_LED_ON, &adapter->led_status);
1826         hw->mac.ops.cleanup_led(hw);
1827
1828         return 0;
1829 }
1830
1831 static int e1000_get_coalesce(struct net_device *netdev,
1832                               struct ethtool_coalesce *ec)
1833 {
1834         struct e1000_adapter *adapter = netdev_priv(netdev);
1835
1836         if (adapter->itr_setting <= 3)
1837                 ec->rx_coalesce_usecs = adapter->itr_setting;
1838         else
1839                 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1840
1841         return 0;
1842 }
1843
1844 static int e1000_set_coalesce(struct net_device *netdev,
1845                               struct ethtool_coalesce *ec)
1846 {
1847         struct e1000_adapter *adapter = netdev_priv(netdev);
1848         struct e1000_hw *hw = &adapter->hw;
1849
1850         if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
1851             ((ec->rx_coalesce_usecs > 3) &&
1852              (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1853             (ec->rx_coalesce_usecs == 2))
1854                 return -EINVAL;
1855
1856         if (ec->rx_coalesce_usecs <= 3) {
1857                 adapter->itr = 20000;
1858                 adapter->itr_setting = ec->rx_coalesce_usecs;
1859         } else {
1860                 adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1861                 adapter->itr_setting = adapter->itr & ~3;
1862         }
1863
1864         if (adapter->itr_setting != 0)
1865                 ew32(ITR, 1000000000 / (adapter->itr * 256));
1866         else
1867                 ew32(ITR, 0);
1868
1869         return 0;
1870 }
1871
1872 static int e1000_nway_reset(struct net_device *netdev)
1873 {
1874         struct e1000_adapter *adapter = netdev_priv(netdev);
1875         if (netif_running(netdev))
1876                 e1000e_reinit_locked(adapter);
1877         return 0;
1878 }
1879
1880 static void e1000_get_ethtool_stats(struct net_device *netdev,
1881                                     struct ethtool_stats *stats,
1882                                     u64 *data)
1883 {
1884         struct e1000_adapter *adapter = netdev_priv(netdev);
1885         int i;
1886
1887         e1000e_update_stats(adapter);
1888         for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1889                 char *p = (char *)adapter+e1000_gstrings_stats[i].stat_offset;
1890                 data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
1891                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1892         }
1893 }
1894
1895 static void e1000_get_strings(struct net_device *netdev, u32 stringset,
1896                               u8 *data)
1897 {
1898         u8 *p = data;
1899         int i;
1900
1901         switch (stringset) {
1902         case ETH_SS_TEST:
1903                 memcpy(data, *e1000_gstrings_test, sizeof(e1000_gstrings_test));
1904                 break;
1905         case ETH_SS_STATS:
1906                 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1907                         memcpy(p, e1000_gstrings_stats[i].stat_string,
1908                                ETH_GSTRING_LEN);
1909                         p += ETH_GSTRING_LEN;
1910                 }
1911                 break;
1912         }
1913 }
1914
1915 static const struct ethtool_ops e1000_ethtool_ops = {
1916         .get_settings           = e1000_get_settings,
1917         .set_settings           = e1000_set_settings,
1918         .get_drvinfo            = e1000_get_drvinfo,
1919         .get_regs_len           = e1000_get_regs_len,
1920         .get_regs               = e1000_get_regs,
1921         .get_wol                = e1000_get_wol,
1922         .set_wol                = e1000_set_wol,
1923         .get_msglevel           = e1000_get_msglevel,
1924         .set_msglevel           = e1000_set_msglevel,
1925         .nway_reset             = e1000_nway_reset,
1926         .get_link               = e1000_get_link,
1927         .get_eeprom_len         = e1000_get_eeprom_len,
1928         .get_eeprom             = e1000_get_eeprom,
1929         .set_eeprom             = e1000_set_eeprom,
1930         .get_ringparam          = e1000_get_ringparam,
1931         .set_ringparam          = e1000_set_ringparam,
1932         .get_pauseparam         = e1000_get_pauseparam,
1933         .set_pauseparam         = e1000_set_pauseparam,
1934         .get_rx_csum            = e1000_get_rx_csum,
1935         .set_rx_csum            = e1000_set_rx_csum,
1936         .get_tx_csum            = e1000_get_tx_csum,
1937         .set_tx_csum            = e1000_set_tx_csum,
1938         .get_sg                 = ethtool_op_get_sg,
1939         .set_sg                 = ethtool_op_set_sg,
1940         .get_tso                = ethtool_op_get_tso,
1941         .set_tso                = e1000_set_tso,
1942         .self_test              = e1000_diag_test,
1943         .get_strings            = e1000_get_strings,
1944         .phys_id                = e1000_phys_id,
1945         .get_ethtool_stats      = e1000_get_ethtool_stats,
1946         .get_sset_count         = e1000e_get_sset_count,
1947         .get_coalesce           = e1000_get_coalesce,
1948         .set_coalesce           = e1000_set_coalesce,
1949 };
1950
1951 void e1000e_set_ethtool_ops(struct net_device *netdev)
1952 {
1953         SET_ETHTOOL_OPS(netdev, &e1000_ethtool_ops);
1954 }