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