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