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