xref: /linux/drivers/net/ethernet/intel/e1000/e1000_ethtool.c (revision 18f65355e112dfc87d5e2e8a299119afd2e65e7e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2006 Intel Corporation. */
3 
4 /* ethtool support for e1000 */
5 
6 #include "e1000.h"
7 #include <linux/jiffies.h>
8 #include <linux/uaccess.h>
9 
10 enum {NETDEV_STATS, E1000_STATS};
11 
12 struct e1000_stats {
13 	char stat_string[ETH_GSTRING_LEN];
14 	int type;
15 	int sizeof_stat;
16 	int stat_offset;
17 };
18 
19 #define E1000_STAT(m)		E1000_STATS, \
20 				sizeof(((struct e1000_adapter *)0)->m), \
21 				offsetof(struct e1000_adapter, m)
22 #define E1000_NETDEV_STAT(m)	NETDEV_STATS, \
23 				sizeof(((struct net_device *)0)->m), \
24 				offsetof(struct net_device, m)
25 
26 static const struct e1000_stats e1000_gstrings_stats[] = {
27 	{ "rx_packets", E1000_STAT(stats.gprc) },
28 	{ "tx_packets", E1000_STAT(stats.gptc) },
29 	{ "rx_bytes", E1000_STAT(stats.gorcl) },
30 	{ "tx_bytes", E1000_STAT(stats.gotcl) },
31 	{ "rx_broadcast", E1000_STAT(stats.bprc) },
32 	{ "tx_broadcast", E1000_STAT(stats.bptc) },
33 	{ "rx_multicast", E1000_STAT(stats.mprc) },
34 	{ "tx_multicast", E1000_STAT(stats.mptc) },
35 	{ "rx_errors", E1000_STAT(stats.rxerrc) },
36 	{ "tx_errors", E1000_STAT(stats.txerrc) },
37 	{ "tx_dropped", E1000_NETDEV_STAT(stats.tx_dropped) },
38 	{ "multicast", E1000_STAT(stats.mprc) },
39 	{ "collisions", E1000_STAT(stats.colc) },
40 	{ "rx_length_errors", E1000_STAT(stats.rlerrc) },
41 	{ "rx_over_errors", E1000_NETDEV_STAT(stats.rx_over_errors) },
42 	{ "rx_crc_errors", E1000_STAT(stats.crcerrs) },
43 	{ "rx_frame_errors", E1000_NETDEV_STAT(stats.rx_frame_errors) },
44 	{ "rx_no_buffer_count", E1000_STAT(stats.rnbc) },
45 	{ "rx_missed_errors", E1000_STAT(stats.mpc) },
46 	{ "tx_aborted_errors", E1000_STAT(stats.ecol) },
47 	{ "tx_carrier_errors", E1000_STAT(stats.tncrs) },
48 	{ "tx_fifo_errors", E1000_NETDEV_STAT(stats.tx_fifo_errors) },
49 	{ "tx_heartbeat_errors", E1000_NETDEV_STAT(stats.tx_heartbeat_errors) },
50 	{ "tx_window_errors", E1000_STAT(stats.latecol) },
51 	{ "tx_abort_late_coll", E1000_STAT(stats.latecol) },
52 	{ "tx_deferred_ok", E1000_STAT(stats.dc) },
53 	{ "tx_single_coll_ok", E1000_STAT(stats.scc) },
54 	{ "tx_multi_coll_ok", E1000_STAT(stats.mcc) },
55 	{ "tx_timeout_count", E1000_STAT(tx_timeout_count) },
56 	{ "tx_restart_queue", E1000_STAT(restart_queue) },
57 	{ "rx_long_length_errors", E1000_STAT(stats.roc) },
58 	{ "rx_short_length_errors", E1000_STAT(stats.ruc) },
59 	{ "rx_align_errors", E1000_STAT(stats.algnerrc) },
60 	{ "tx_tcp_seg_good", E1000_STAT(stats.tsctc) },
61 	{ "tx_tcp_seg_failed", E1000_STAT(stats.tsctfc) },
62 	{ "rx_flow_control_xon", E1000_STAT(stats.xonrxc) },
63 	{ "rx_flow_control_xoff", E1000_STAT(stats.xoffrxc) },
64 	{ "tx_flow_control_xon", E1000_STAT(stats.xontxc) },
65 	{ "tx_flow_control_xoff", E1000_STAT(stats.xofftxc) },
66 	{ "rx_long_byte_count", E1000_STAT(stats.gorcl) },
67 	{ "rx_csum_offload_good", E1000_STAT(hw_csum_good) },
68 	{ "rx_csum_offload_errors", E1000_STAT(hw_csum_err) },
69 	{ "alloc_rx_buff_failed", E1000_STAT(alloc_rx_buff_failed) },
70 	{ "tx_smbus", E1000_STAT(stats.mgptc) },
71 	{ "rx_smbus", E1000_STAT(stats.mgprc) },
72 	{ "dropped_smbus", E1000_STAT(stats.mgpdc) },
73 };
74 
75 #define E1000_QUEUE_STATS_LEN 0
76 #define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats)
77 #define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN + E1000_QUEUE_STATS_LEN)
78 static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = {
79 	"Register test  (offline)", "Eeprom test    (offline)",
80 	"Interrupt test (offline)", "Loopback test  (offline)",
81 	"Link test   (on/offline)"
82 };
83 
84 #define E1000_TEST_LEN	ARRAY_SIZE(e1000_gstrings_test)
85 
86 static int e1000_get_link_ksettings(struct net_device *netdev,
87 				    struct ethtool_link_ksettings *cmd)
88 {
89 	struct e1000_adapter *adapter = netdev_priv(netdev);
90 	struct e1000_hw *hw = &adapter->hw;
91 	u32 supported, advertising;
92 
93 	if (hw->media_type == e1000_media_type_copper) {
94 		supported = (SUPPORTED_10baseT_Half |
95 			     SUPPORTED_10baseT_Full |
96 			     SUPPORTED_100baseT_Half |
97 			     SUPPORTED_100baseT_Full |
98 			     SUPPORTED_1000baseT_Full|
99 			     SUPPORTED_Autoneg |
100 			     SUPPORTED_TP);
101 		advertising = ADVERTISED_TP;
102 
103 		if (hw->autoneg == 1) {
104 			advertising |= ADVERTISED_Autoneg;
105 			/* the e1000 autoneg seems to match ethtool nicely */
106 			advertising |= hw->autoneg_advertised;
107 		}
108 
109 		cmd->base.port = PORT_TP;
110 		cmd->base.phy_address = hw->phy_addr;
111 	} else {
112 		supported   = (SUPPORTED_1000baseT_Full |
113 			       SUPPORTED_FIBRE |
114 			       SUPPORTED_Autoneg);
115 
116 		advertising = (ADVERTISED_1000baseT_Full |
117 			       ADVERTISED_FIBRE |
118 			       ADVERTISED_Autoneg);
119 
120 		cmd->base.port = PORT_FIBRE;
121 	}
122 
123 	if (er32(STATUS) & E1000_STATUS_LU) {
124 		e1000_get_speed_and_duplex(hw, &adapter->link_speed,
125 					   &adapter->link_duplex);
126 		cmd->base.speed = adapter->link_speed;
127 
128 		/* unfortunately FULL_DUPLEX != DUPLEX_FULL
129 		 * and HALF_DUPLEX != DUPLEX_HALF
130 		 */
131 		if (adapter->link_duplex == FULL_DUPLEX)
132 			cmd->base.duplex = DUPLEX_FULL;
133 		else
134 			cmd->base.duplex = DUPLEX_HALF;
135 	} else {
136 		cmd->base.speed = SPEED_UNKNOWN;
137 		cmd->base.duplex = DUPLEX_UNKNOWN;
138 	}
139 
140 	cmd->base.autoneg = ((hw->media_type == e1000_media_type_fiber) ||
141 			 hw->autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
142 
143 	/* MDI-X => 1; MDI => 0 */
144 	if ((hw->media_type == e1000_media_type_copper) &&
145 	    netif_carrier_ok(netdev))
146 		cmd->base.eth_tp_mdix = (!!adapter->phy_info.mdix_mode ?
147 				     ETH_TP_MDI_X : ETH_TP_MDI);
148 	else
149 		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
150 
151 	if (hw->mdix == AUTO_ALL_MODES)
152 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
153 	else
154 		cmd->base.eth_tp_mdix_ctrl = hw->mdix;
155 
156 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
157 						supported);
158 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
159 						advertising);
160 
161 	return 0;
162 }
163 
164 static int e1000_set_link_ksettings(struct net_device *netdev,
165 				    const struct ethtool_link_ksettings *cmd)
166 {
167 	struct e1000_adapter *adapter = netdev_priv(netdev);
168 	struct e1000_hw *hw = &adapter->hw;
169 	u32 advertising;
170 
171 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
172 						cmd->link_modes.advertising);
173 
174 	/* MDI setting is only allowed when autoneg enabled because
175 	 * some hardware doesn't allow MDI setting when speed or
176 	 * duplex is forced.
177 	 */
178 	if (cmd->base.eth_tp_mdix_ctrl) {
179 		if (hw->media_type != e1000_media_type_copper)
180 			return -EOPNOTSUPP;
181 
182 		if ((cmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO) &&
183 		    (cmd->base.autoneg != AUTONEG_ENABLE)) {
184 			e_err(drv, "forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n");
185 			return -EINVAL;
186 		}
187 	}
188 
189 	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
190 		msleep(1);
191 
192 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
193 		hw->autoneg = 1;
194 		if (hw->media_type == e1000_media_type_fiber)
195 			hw->autoneg_advertised = ADVERTISED_1000baseT_Full |
196 						 ADVERTISED_FIBRE |
197 						 ADVERTISED_Autoneg;
198 		else
199 			hw->autoneg_advertised = advertising |
200 						 ADVERTISED_TP |
201 						 ADVERTISED_Autoneg;
202 	} else {
203 		u32 speed = cmd->base.speed;
204 		/* calling this overrides forced MDI setting */
205 		if (e1000_set_spd_dplx(adapter, speed, cmd->base.duplex)) {
206 			clear_bit(__E1000_RESETTING, &adapter->flags);
207 			return -EINVAL;
208 		}
209 	}
210 
211 	/* MDI-X => 2; MDI => 1; Auto => 3 */
212 	if (cmd->base.eth_tp_mdix_ctrl) {
213 		if (cmd->base.eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
214 			hw->mdix = AUTO_ALL_MODES;
215 		else
216 			hw->mdix = cmd->base.eth_tp_mdix_ctrl;
217 	}
218 
219 	/* reset the link */
220 
221 	if (netif_running(adapter->netdev)) {
222 		e1000_down(adapter);
223 		e1000_up(adapter);
224 	} else {
225 		e1000_reset(adapter);
226 	}
227 	clear_bit(__E1000_RESETTING, &adapter->flags);
228 	return 0;
229 }
230 
231 static u32 e1000_get_link(struct net_device *netdev)
232 {
233 	struct e1000_adapter *adapter = netdev_priv(netdev);
234 
235 	/* If the link is not reported up to netdev, interrupts are disabled,
236 	 * and so the physical link state may have changed since we last
237 	 * looked. Set get_link_status to make sure that the true link
238 	 * state is interrogated, rather than pulling a cached and possibly
239 	 * stale link state from the driver.
240 	 */
241 	if (!netif_carrier_ok(netdev))
242 		adapter->hw.get_link_status = 1;
243 
244 	return e1000_has_link(adapter);
245 }
246 
247 static void e1000_get_pauseparam(struct net_device *netdev,
248 				 struct ethtool_pauseparam *pause)
249 {
250 	struct e1000_adapter *adapter = netdev_priv(netdev);
251 	struct e1000_hw *hw = &adapter->hw;
252 
253 	pause->autoneg =
254 		(adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
255 
256 	if (hw->fc == E1000_FC_RX_PAUSE) {
257 		pause->rx_pause = 1;
258 	} else if (hw->fc == E1000_FC_TX_PAUSE) {
259 		pause->tx_pause = 1;
260 	} else if (hw->fc == E1000_FC_FULL) {
261 		pause->rx_pause = 1;
262 		pause->tx_pause = 1;
263 	}
264 }
265 
266 static int e1000_set_pauseparam(struct net_device *netdev,
267 				struct ethtool_pauseparam *pause)
268 {
269 	struct e1000_adapter *adapter = netdev_priv(netdev);
270 	struct e1000_hw *hw = &adapter->hw;
271 	int retval = 0;
272 
273 	adapter->fc_autoneg = pause->autoneg;
274 
275 	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
276 		msleep(1);
277 
278 	if (pause->rx_pause && pause->tx_pause)
279 		hw->fc = E1000_FC_FULL;
280 	else if (pause->rx_pause && !pause->tx_pause)
281 		hw->fc = E1000_FC_RX_PAUSE;
282 	else if (!pause->rx_pause && pause->tx_pause)
283 		hw->fc = E1000_FC_TX_PAUSE;
284 	else if (!pause->rx_pause && !pause->tx_pause)
285 		hw->fc = E1000_FC_NONE;
286 
287 	hw->original_fc = hw->fc;
288 
289 	if (adapter->fc_autoneg == AUTONEG_ENABLE) {
290 		if (netif_running(adapter->netdev)) {
291 			e1000_down(adapter);
292 			e1000_up(adapter);
293 		} else {
294 			e1000_reset(adapter);
295 		}
296 	} else
297 		retval = ((hw->media_type == e1000_media_type_fiber) ?
298 			  e1000_setup_link(hw) : e1000_force_mac_fc(hw));
299 
300 	clear_bit(__E1000_RESETTING, &adapter->flags);
301 	return retval;
302 }
303 
304 static u32 e1000_get_msglevel(struct net_device *netdev)
305 {
306 	struct e1000_adapter *adapter = netdev_priv(netdev);
307 
308 	return adapter->msg_enable;
309 }
310 
311 static void e1000_set_msglevel(struct net_device *netdev, u32 data)
312 {
313 	struct e1000_adapter *adapter = netdev_priv(netdev);
314 
315 	adapter->msg_enable = data;
316 }
317 
318 static int e1000_get_regs_len(struct net_device *netdev)
319 {
320 #define E1000_REGS_LEN 32
321 	return E1000_REGS_LEN * sizeof(u32);
322 }
323 
324 static void e1000_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
325 			   void *p)
326 {
327 	struct e1000_adapter *adapter = netdev_priv(netdev);
328 	struct e1000_hw *hw = &adapter->hw;
329 	u32 *regs_buff = p;
330 	u16 phy_data;
331 
332 	memset(p, 0, E1000_REGS_LEN * sizeof(u32));
333 
334 	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
335 
336 	regs_buff[0]  = er32(CTRL);
337 	regs_buff[1]  = er32(STATUS);
338 
339 	regs_buff[2]  = er32(RCTL);
340 	regs_buff[3]  = er32(RDLEN);
341 	regs_buff[4]  = er32(RDH);
342 	regs_buff[5]  = er32(RDT);
343 	regs_buff[6]  = er32(RDTR);
344 
345 	regs_buff[7]  = er32(TCTL);
346 	regs_buff[8]  = er32(TDLEN);
347 	regs_buff[9]  = er32(TDH);
348 	regs_buff[10] = er32(TDT);
349 	regs_buff[11] = er32(TIDV);
350 
351 	regs_buff[12] = hw->phy_type;  /* PHY type (IGP=1, M88=0) */
352 	if (hw->phy_type == e1000_phy_igp) {
353 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
354 				    IGP01E1000_PHY_AGC_A);
355 		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_A &
356 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
357 		regs_buff[13] = (u32)phy_data; /* cable length */
358 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
359 				    IGP01E1000_PHY_AGC_B);
360 		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_B &
361 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
362 		regs_buff[14] = (u32)phy_data; /* cable length */
363 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
364 				    IGP01E1000_PHY_AGC_C);
365 		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_C &
366 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
367 		regs_buff[15] = (u32)phy_data; /* cable length */
368 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
369 				    IGP01E1000_PHY_AGC_D);
370 		e1000_read_phy_reg(hw, IGP01E1000_PHY_AGC_D &
371 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
372 		regs_buff[16] = (u32)phy_data; /* cable length */
373 		regs_buff[17] = 0; /* extended 10bt distance (not needed) */
374 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
375 		e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS &
376 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
377 		regs_buff[18] = (u32)phy_data; /* cable polarity */
378 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT,
379 				    IGP01E1000_PHY_PCS_INIT_REG);
380 		e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG &
381 				   IGP01E1000_PHY_PAGE_SELECT, &phy_data);
382 		regs_buff[19] = (u32)phy_data; /* cable polarity */
383 		regs_buff[20] = 0; /* polarity correction enabled (always) */
384 		regs_buff[22] = 0; /* phy receive errors (unavailable) */
385 		regs_buff[23] = regs_buff[18]; /* mdix mode */
386 		e1000_write_phy_reg(hw, IGP01E1000_PHY_PAGE_SELECT, 0x0);
387 	} else {
388 		e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
389 		regs_buff[13] = (u32)phy_data; /* cable length */
390 		regs_buff[14] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
391 		regs_buff[15] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
392 		regs_buff[16] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
393 		e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
394 		regs_buff[17] = (u32)phy_data; /* extended 10bt distance */
395 		regs_buff[18] = regs_buff[13]; /* cable polarity */
396 		regs_buff[19] = 0;  /* Dummy (to align w/ IGP phy reg dump) */
397 		regs_buff[20] = regs_buff[17]; /* polarity correction */
398 		/* phy receive errors */
399 		regs_buff[22] = adapter->phy_stats.receive_errors;
400 		regs_buff[23] = regs_buff[13]; /* mdix mode */
401 	}
402 	regs_buff[21] = adapter->phy_stats.idle_errors;  /* phy idle errors */
403 	e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
404 	regs_buff[24] = (u32)phy_data;  /* phy local receiver status */
405 	regs_buff[25] = regs_buff[24];  /* phy remote receiver status */
406 	if (hw->mac_type >= e1000_82540 &&
407 	    hw->media_type == e1000_media_type_copper) {
408 		regs_buff[26] = er32(MANC);
409 	}
410 }
411 
412 static int e1000_get_eeprom_len(struct net_device *netdev)
413 {
414 	struct e1000_adapter *adapter = netdev_priv(netdev);
415 	struct e1000_hw *hw = &adapter->hw;
416 
417 	return hw->eeprom.word_size * 2;
418 }
419 
420 static int e1000_get_eeprom(struct net_device *netdev,
421 			    struct ethtool_eeprom *eeprom, u8 *bytes)
422 {
423 	struct e1000_adapter *adapter = netdev_priv(netdev);
424 	struct e1000_hw *hw = &adapter->hw;
425 	u16 *eeprom_buff;
426 	int first_word, last_word;
427 	int ret_val = 0;
428 	u16 i;
429 
430 	if (eeprom->len == 0)
431 		return -EINVAL;
432 
433 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
434 
435 	first_word = eeprom->offset >> 1;
436 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
437 
438 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
439 				    GFP_KERNEL);
440 	if (!eeprom_buff)
441 		return -ENOMEM;
442 
443 	if (hw->eeprom.type == e1000_eeprom_spi)
444 		ret_val = e1000_read_eeprom(hw, first_word,
445 					    last_word - first_word + 1,
446 					    eeprom_buff);
447 	else {
448 		for (i = 0; i < last_word - first_word + 1; i++) {
449 			ret_val = e1000_read_eeprom(hw, first_word + i, 1,
450 						    &eeprom_buff[i]);
451 			if (ret_val)
452 				break;
453 		}
454 	}
455 
456 	/* Device's eeprom is always little-endian, word addressable */
457 	for (i = 0; i < last_word - first_word + 1; i++)
458 		le16_to_cpus(&eeprom_buff[i]);
459 
460 	memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1),
461 	       eeprom->len);
462 	kfree(eeprom_buff);
463 
464 	return ret_val;
465 }
466 
467 static int e1000_set_eeprom(struct net_device *netdev,
468 			    struct ethtool_eeprom *eeprom, u8 *bytes)
469 {
470 	struct e1000_adapter *adapter = netdev_priv(netdev);
471 	struct e1000_hw *hw = &adapter->hw;
472 	u16 *eeprom_buff;
473 	void *ptr;
474 	int max_len, first_word, last_word, ret_val = 0;
475 	u16 i;
476 
477 	if (eeprom->len == 0)
478 		return -EOPNOTSUPP;
479 
480 	if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
481 		return -EFAULT;
482 
483 	max_len = hw->eeprom.word_size * 2;
484 
485 	first_word = eeprom->offset >> 1;
486 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
487 	eeprom_buff = kmalloc(max_len, GFP_KERNEL);
488 	if (!eeprom_buff)
489 		return -ENOMEM;
490 
491 	ptr = (void *)eeprom_buff;
492 
493 	if (eeprom->offset & 1) {
494 		/* need read/modify/write of first changed EEPROM word
495 		 * only the second byte of the word is being modified
496 		 */
497 		ret_val = e1000_read_eeprom(hw, first_word, 1,
498 					    &eeprom_buff[0]);
499 		if (ret_val)
500 			goto out;
501 
502 		/* Device's eeprom is always little-endian, word addressable */
503 		le16_to_cpus(&eeprom_buff[0]);
504 
505 		ptr++;
506 	}
507 	if ((eeprom->offset + eeprom->len) & 1) {
508 		/* need read/modify/write of last changed EEPROM word
509 		 * only the first byte of the word is being modified
510 		 */
511 		ret_val = e1000_read_eeprom(hw, last_word, 1,
512 					    &eeprom_buff[last_word - first_word]);
513 		if (ret_val)
514 			goto out;
515 
516 		/* Device's eeprom is always little-endian, word addressable */
517 		le16_to_cpus(&eeprom_buff[last_word - first_word]);
518 	}
519 
520 	memcpy(ptr, bytes, eeprom->len);
521 
522 	for (i = 0; i < last_word - first_word + 1; i++)
523 		cpu_to_le16s(&eeprom_buff[i]);
524 
525 	ret_val = e1000_write_eeprom(hw, first_word,
526 				     last_word - first_word + 1, eeprom_buff);
527 
528 	/* Update the checksum over the first part of the EEPROM if needed */
529 	if ((ret_val == 0) && (first_word <= EEPROM_CHECKSUM_REG))
530 		e1000_update_eeprom_checksum(hw);
531 
532 out:
533 	kfree(eeprom_buff);
534 	return ret_val;
535 }
536 
537 static void e1000_get_drvinfo(struct net_device *netdev,
538 			      struct ethtool_drvinfo *drvinfo)
539 {
540 	struct e1000_adapter *adapter = netdev_priv(netdev);
541 
542 	strscpy(drvinfo->driver,  e1000_driver_name,
543 		sizeof(drvinfo->driver));
544 
545 	strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
546 		sizeof(drvinfo->bus_info));
547 }
548 
549 static void e1000_get_ringparam(struct net_device *netdev,
550 				struct ethtool_ringparam *ring,
551 				struct kernel_ethtool_ringparam *kernel_ring,
552 				struct netlink_ext_ack *extack)
553 {
554 	struct e1000_adapter *adapter = netdev_priv(netdev);
555 	struct e1000_hw *hw = &adapter->hw;
556 	e1000_mac_type mac_type = hw->mac_type;
557 	struct e1000_tx_ring *txdr = adapter->tx_ring;
558 	struct e1000_rx_ring *rxdr = adapter->rx_ring;
559 
560 	ring->rx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_RXD :
561 		E1000_MAX_82544_RXD;
562 	ring->tx_max_pending = (mac_type < e1000_82544) ? E1000_MAX_TXD :
563 		E1000_MAX_82544_TXD;
564 	ring->rx_pending = rxdr->count;
565 	ring->tx_pending = txdr->count;
566 }
567 
568 static int e1000_set_ringparam(struct net_device *netdev,
569 			       struct ethtool_ringparam *ring,
570 			       struct kernel_ethtool_ringparam *kernel_ring,
571 			       struct netlink_ext_ack *extack)
572 {
573 	struct e1000_adapter *adapter = netdev_priv(netdev);
574 	struct e1000_hw *hw = &adapter->hw;
575 	e1000_mac_type mac_type = hw->mac_type;
576 	struct e1000_tx_ring *txdr, *tx_old;
577 	struct e1000_rx_ring *rxdr, *rx_old;
578 	int i, err;
579 
580 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
581 		return -EINVAL;
582 
583 	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
584 		msleep(1);
585 
586 	if (netif_running(adapter->netdev))
587 		e1000_down(adapter);
588 
589 	tx_old = adapter->tx_ring;
590 	rx_old = adapter->rx_ring;
591 
592 	err = -ENOMEM;
593 	txdr = kzalloc_objs(struct e1000_tx_ring, adapter->num_tx_queues);
594 	if (!txdr)
595 		goto err_alloc_tx;
596 
597 	rxdr = kzalloc_objs(struct e1000_rx_ring, adapter->num_rx_queues);
598 	if (!rxdr)
599 		goto err_alloc_rx;
600 
601 	adapter->tx_ring = txdr;
602 	adapter->rx_ring = rxdr;
603 
604 	rxdr->count = max(ring->rx_pending, (u32)E1000_MIN_RXD);
605 	rxdr->count = min(rxdr->count, (u32)(mac_type < e1000_82544 ?
606 			  E1000_MAX_RXD : E1000_MAX_82544_RXD));
607 	rxdr->count = ALIGN(rxdr->count, REQ_RX_DESCRIPTOR_MULTIPLE);
608 	txdr->count = max(ring->tx_pending, (u32)E1000_MIN_TXD);
609 	txdr->count = min(txdr->count, (u32)(mac_type < e1000_82544 ?
610 			  E1000_MAX_TXD : E1000_MAX_82544_TXD));
611 	txdr->count = ALIGN(txdr->count, REQ_TX_DESCRIPTOR_MULTIPLE);
612 
613 	for (i = 0; i < adapter->num_tx_queues; i++)
614 		txdr[i].count = txdr->count;
615 	for (i = 0; i < adapter->num_rx_queues; i++)
616 		rxdr[i].count = rxdr->count;
617 
618 	err = 0;
619 	if (netif_running(adapter->netdev)) {
620 		/* Try to get new resources before deleting old */
621 		err = e1000_setup_all_rx_resources(adapter);
622 		if (err)
623 			goto err_setup_rx;
624 		err = e1000_setup_all_tx_resources(adapter);
625 		if (err)
626 			goto err_setup_tx;
627 
628 		/* save the new, restore the old in order to free it,
629 		 * then restore the new back again
630 		 */
631 
632 		adapter->rx_ring = rx_old;
633 		adapter->tx_ring = tx_old;
634 		e1000_free_all_rx_resources(adapter);
635 		e1000_free_all_tx_resources(adapter);
636 		adapter->rx_ring = rxdr;
637 		adapter->tx_ring = txdr;
638 		err = e1000_up(adapter);
639 	}
640 	kfree(tx_old);
641 	kfree(rx_old);
642 
643 	clear_bit(__E1000_RESETTING, &adapter->flags);
644 	return err;
645 
646 err_setup_tx:
647 	e1000_free_all_rx_resources(adapter);
648 err_setup_rx:
649 	adapter->rx_ring = rx_old;
650 	adapter->tx_ring = tx_old;
651 	kfree(rxdr);
652 err_alloc_rx:
653 	kfree(txdr);
654 err_alloc_tx:
655 	if (netif_running(adapter->netdev))
656 		e1000_up(adapter);
657 	clear_bit(__E1000_RESETTING, &adapter->flags);
658 	return err;
659 }
660 
661 static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data, int reg,
662 			     u32 mask, u32 write)
663 {
664 	struct e1000_hw *hw = &adapter->hw;
665 	static const u32 test[] = {
666 		0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
667 	};
668 	u8 __iomem *address = hw->hw_addr + reg;
669 	u32 read;
670 	int i;
671 
672 	for (i = 0; i < ARRAY_SIZE(test); i++) {
673 		writel(write & test[i], address);
674 		read = readl(address);
675 		if (read != (write & test[i] & mask)) {
676 			e_err(drv, "pattern test reg %04X failed: "
677 			      "got 0x%08X expected 0x%08X\n",
678 			      reg, read, (write & test[i] & mask));
679 			*data = reg;
680 			return true;
681 		}
682 	}
683 	return false;
684 }
685 
686 static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data, int reg,
687 			      u32 mask, u32 write)
688 {
689 	struct e1000_hw *hw = &adapter->hw;
690 	u8 __iomem *address = hw->hw_addr + reg;
691 	u32 read;
692 
693 	writel(write & mask, address);
694 	read = readl(address);
695 	if ((read & mask) != (write & mask)) {
696 		e_err(drv, "set/check reg %04X test failed: "
697 		      "got 0x%08X expected 0x%08X\n",
698 		      reg, (read & mask), (write & mask));
699 		*data = reg;
700 		return true;
701 	}
702 	return false;
703 }
704 
705 #define REG_PATTERN_TEST(reg, mask, write)			     \
706 	do {							     \
707 		if (reg_pattern_test(adapter, data,		     \
708 			     (hw->mac_type >= e1000_82543)   \
709 			     ? E1000_##reg : E1000_82542_##reg,	     \
710 			     mask, write))			     \
711 			return 1;				     \
712 	} while (0)
713 
714 #define REG_SET_AND_CHECK(reg, mask, write)			     \
715 	do {							     \
716 		if (reg_set_and_check(adapter, data,		     \
717 			      (hw->mac_type >= e1000_82543)  \
718 			      ? E1000_##reg : E1000_82542_##reg,     \
719 			      mask, write))			     \
720 			return 1;				     \
721 	} while (0)
722 
723 static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
724 {
725 	u32 value, before, after;
726 	u32 i, toggle;
727 	struct e1000_hw *hw = &adapter->hw;
728 
729 	/* The status register is Read Only, so a write should fail.
730 	 * Some bits that get toggled are ignored.
731 	 */
732 
733 	/* there are several bits on newer hardware that are r/w */
734 	toggle = 0xFFFFF833;
735 
736 	before = er32(STATUS);
737 	value = (er32(STATUS) & toggle);
738 	ew32(STATUS, toggle);
739 	after = er32(STATUS) & toggle;
740 	if (value != after) {
741 		e_err(drv, "failed STATUS register test got: "
742 		      "0x%08X expected: 0x%08X\n", after, value);
743 		*data = 1;
744 		return 1;
745 	}
746 	/* restore previous status */
747 	ew32(STATUS, before);
748 
749 	REG_PATTERN_TEST(FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
750 	REG_PATTERN_TEST(FCAH, 0x0000FFFF, 0xFFFFFFFF);
751 	REG_PATTERN_TEST(FCT, 0x0000FFFF, 0xFFFFFFFF);
752 	REG_PATTERN_TEST(VET, 0x0000FFFF, 0xFFFFFFFF);
753 
754 	REG_PATTERN_TEST(RDTR, 0x0000FFFF, 0xFFFFFFFF);
755 	REG_PATTERN_TEST(RDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
756 	REG_PATTERN_TEST(RDLEN, 0x000FFF80, 0x000FFFFF);
757 	REG_PATTERN_TEST(RDH, 0x0000FFFF, 0x0000FFFF);
758 	REG_PATTERN_TEST(RDT, 0x0000FFFF, 0x0000FFFF);
759 	REG_PATTERN_TEST(FCRTH, 0x0000FFF8, 0x0000FFF8);
760 	REG_PATTERN_TEST(FCTTV, 0x0000FFFF, 0x0000FFFF);
761 	REG_PATTERN_TEST(TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
762 	REG_PATTERN_TEST(TDBAH, 0xFFFFFFFF, 0xFFFFFFFF);
763 	REG_PATTERN_TEST(TDLEN, 0x000FFF80, 0x000FFFFF);
764 
765 	REG_SET_AND_CHECK(RCTL, 0xFFFFFFFF, 0x00000000);
766 
767 	before = 0x06DFB3FE;
768 	REG_SET_AND_CHECK(RCTL, before, 0x003FFFFB);
769 	REG_SET_AND_CHECK(TCTL, 0xFFFFFFFF, 0x00000000);
770 
771 	if (hw->mac_type >= e1000_82543) {
772 		REG_SET_AND_CHECK(RCTL, before, 0xFFFFFFFF);
773 		REG_PATTERN_TEST(RDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
774 		REG_PATTERN_TEST(TXCW, 0xC000FFFF, 0x0000FFFF);
775 		REG_PATTERN_TEST(TDBAL, 0xFFFFFFF0, 0xFFFFFFFF);
776 		REG_PATTERN_TEST(TIDV, 0x0000FFFF, 0x0000FFFF);
777 		value = E1000_RAR_ENTRIES;
778 		for (i = 0; i < value; i++) {
779 			REG_PATTERN_TEST(RA + (((i << 1) + 1) << 2),
780 					 0x8003FFFF, 0xFFFFFFFF);
781 		}
782 	} else {
783 		REG_SET_AND_CHECK(RCTL, 0xFFFFFFFF, 0x01FFFFFF);
784 		REG_PATTERN_TEST(RDBAL, 0xFFFFF000, 0xFFFFFFFF);
785 		REG_PATTERN_TEST(TXCW, 0x0000FFFF, 0x0000FFFF);
786 		REG_PATTERN_TEST(TDBAL, 0xFFFFF000, 0xFFFFFFFF);
787 	}
788 
789 	value = E1000_MC_TBL_SIZE;
790 	for (i = 0; i < value; i++)
791 		REG_PATTERN_TEST(MTA + (i << 2), 0xFFFFFFFF, 0xFFFFFFFF);
792 
793 	*data = 0;
794 	return 0;
795 }
796 
797 static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
798 {
799 	struct e1000_hw *hw = &adapter->hw;
800 	u16 temp;
801 	u16 checksum = 0;
802 	u16 i;
803 
804 	*data = 0;
805 	/* Read and add up the contents of the EEPROM */
806 	for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
807 		if ((e1000_read_eeprom(hw, i, 1, &temp)) < 0) {
808 			*data = 1;
809 			break;
810 		}
811 		checksum += temp;
812 	}
813 
814 	/* If Checksum is not Correct return error else test passed */
815 	if (checksum != EEPROM_SUM && !(*data))
816 		*data = 2;
817 
818 	return *data;
819 }
820 
821 static irqreturn_t e1000_test_intr(int irq, void *data)
822 {
823 	struct net_device *netdev = (struct net_device *)data;
824 	struct e1000_adapter *adapter = netdev_priv(netdev);
825 	struct e1000_hw *hw = &adapter->hw;
826 
827 	adapter->test_icr |= er32(ICR);
828 
829 	return IRQ_HANDLED;
830 }
831 
832 static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
833 {
834 	struct net_device *netdev = adapter->netdev;
835 	u32 mask, i = 0;
836 	bool shared_int = true;
837 	u32 irq = adapter->pdev->irq;
838 	struct e1000_hw *hw = &adapter->hw;
839 
840 	*data = 0;
841 
842 	/* NOTE: we don't test MSI interrupts here, yet
843 	 * Hook up test interrupt handler just for this test
844 	 */
845 	if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
846 			 netdev))
847 		shared_int = false;
848 	else if (request_irq(irq, e1000_test_intr, IRQF_SHARED,
849 			     netdev->name, netdev)) {
850 		*data = 1;
851 		return -1;
852 	}
853 	e_info(hw, "testing %s interrupt\n", (shared_int ?
854 	       "shared" : "unshared"));
855 
856 	/* Disable all the interrupts */
857 	ew32(IMC, 0xFFFFFFFF);
858 	E1000_WRITE_FLUSH();
859 	msleep(10);
860 
861 	/* Test each interrupt */
862 	for (; i < 10; i++) {
863 		/* Interrupt to test */
864 		mask = 1 << i;
865 
866 		if (!shared_int) {
867 			/* Disable the interrupt to be reported in
868 			 * the cause register and then force the same
869 			 * interrupt and see if one gets posted.  If
870 			 * an interrupt was posted to the bus, the
871 			 * test failed.
872 			 */
873 			adapter->test_icr = 0;
874 			ew32(IMC, mask);
875 			ew32(ICS, mask);
876 			E1000_WRITE_FLUSH();
877 			msleep(10);
878 
879 			if (adapter->test_icr & mask) {
880 				*data = 3;
881 				break;
882 			}
883 		}
884 
885 		/* Enable the interrupt to be reported in
886 		 * the cause register and then force the same
887 		 * interrupt and see if one gets posted.  If
888 		 * an interrupt was not posted to the bus, the
889 		 * test failed.
890 		 */
891 		adapter->test_icr = 0;
892 		ew32(IMS, mask);
893 		ew32(ICS, mask);
894 		E1000_WRITE_FLUSH();
895 		msleep(10);
896 
897 		if (!(adapter->test_icr & mask)) {
898 			*data = 4;
899 			break;
900 		}
901 
902 		if (!shared_int) {
903 			/* Disable the other interrupts to be reported in
904 			 * the cause register and then force the other
905 			 * interrupts and see if any get posted.  If
906 			 * an interrupt was posted to the bus, the
907 			 * test failed.
908 			 */
909 			adapter->test_icr = 0;
910 			ew32(IMC, ~mask & 0x00007FFF);
911 			ew32(ICS, ~mask & 0x00007FFF);
912 			E1000_WRITE_FLUSH();
913 			msleep(10);
914 
915 			if (adapter->test_icr) {
916 				*data = 5;
917 				break;
918 			}
919 		}
920 	}
921 
922 	/* Disable all the interrupts */
923 	ew32(IMC, 0xFFFFFFFF);
924 	E1000_WRITE_FLUSH();
925 	msleep(10);
926 
927 	/* Unhook test interrupt handler */
928 	free_irq(irq, netdev);
929 
930 	return *data;
931 }
932 
933 static void e1000_free_desc_rings(struct e1000_adapter *adapter)
934 {
935 	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
936 	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
937 	struct pci_dev *pdev = adapter->pdev;
938 	int i;
939 
940 	if (txdr->desc && txdr->buffer_info) {
941 		for (i = 0; i < txdr->count; i++) {
942 			if (txdr->buffer_info[i].dma)
943 				dma_unmap_single(&pdev->dev,
944 						 txdr->buffer_info[i].dma,
945 						 txdr->buffer_info[i].length,
946 						 DMA_TO_DEVICE);
947 			dev_kfree_skb(txdr->buffer_info[i].skb);
948 		}
949 	}
950 
951 	if (rxdr->desc && rxdr->buffer_info) {
952 		for (i = 0; i < rxdr->count; i++) {
953 			if (rxdr->buffer_info[i].dma)
954 				dma_unmap_single(&pdev->dev,
955 						 rxdr->buffer_info[i].dma,
956 						 E1000_RXBUFFER_2048,
957 						 DMA_FROM_DEVICE);
958 			kfree(rxdr->buffer_info[i].rxbuf.data);
959 		}
960 	}
961 
962 	if (txdr->desc) {
963 		dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
964 				  txdr->dma);
965 		txdr->desc = NULL;
966 	}
967 	if (rxdr->desc) {
968 		dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
969 				  rxdr->dma);
970 		rxdr->desc = NULL;
971 	}
972 
973 	kfree(txdr->buffer_info);
974 	txdr->buffer_info = NULL;
975 	kfree(rxdr->buffer_info);
976 	rxdr->buffer_info = NULL;
977 }
978 
979 static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
980 {
981 	struct e1000_hw *hw = &adapter->hw;
982 	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
983 	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
984 	struct pci_dev *pdev = adapter->pdev;
985 	u32 rctl;
986 	int i, ret_val;
987 
988 	/* Setup Tx descriptor ring and Tx buffers */
989 
990 	if (!txdr->count)
991 		txdr->count = E1000_DEFAULT_TXD;
992 
993 	txdr->buffer_info = kzalloc_objs(struct e1000_tx_buffer, txdr->count);
994 	if (!txdr->buffer_info) {
995 		ret_val = 1;
996 		goto err_nomem;
997 	}
998 
999 	txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1000 	txdr->size = ALIGN(txdr->size, 4096);
1001 	txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
1002 					GFP_KERNEL);
1003 	if (!txdr->desc) {
1004 		ret_val = 2;
1005 		goto err_nomem;
1006 	}
1007 	txdr->next_to_use = txdr->next_to_clean = 0;
1008 
1009 	ew32(TDBAL, ((u64)txdr->dma & 0x00000000FFFFFFFF));
1010 	ew32(TDBAH, ((u64)txdr->dma >> 32));
1011 	ew32(TDLEN, txdr->count * sizeof(struct e1000_tx_desc));
1012 	ew32(TDH, 0);
1013 	ew32(TDT, 0);
1014 	ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN |
1015 	     E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT |
1016 	     E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT);
1017 
1018 	for (i = 0; i < txdr->count; i++) {
1019 		struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*txdr, i);
1020 		struct sk_buff *skb;
1021 		unsigned int size = 1024;
1022 
1023 		skb = alloc_skb(size, GFP_KERNEL);
1024 		if (!skb) {
1025 			ret_val = 3;
1026 			goto err_nomem;
1027 		}
1028 		skb_put(skb, size);
1029 		txdr->buffer_info[i].skb = skb;
1030 		txdr->buffer_info[i].length = skb->len;
1031 		txdr->buffer_info[i].dma =
1032 			dma_map_single(&pdev->dev, skb->data, skb->len,
1033 				       DMA_TO_DEVICE);
1034 		if (dma_mapping_error(&pdev->dev, txdr->buffer_info[i].dma)) {
1035 			ret_val = 4;
1036 			goto err_nomem;
1037 		}
1038 		tx_desc->buffer_addr = cpu_to_le64(txdr->buffer_info[i].dma);
1039 		tx_desc->lower.data = cpu_to_le32(skb->len);
1040 		tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP |
1041 						   E1000_TXD_CMD_IFCS |
1042 						   E1000_TXD_CMD_RPS);
1043 		tx_desc->upper.data = 0;
1044 	}
1045 
1046 	/* Setup Rx descriptor ring and Rx buffers */
1047 
1048 	if (!rxdr->count)
1049 		rxdr->count = E1000_DEFAULT_RXD;
1050 
1051 	rxdr->buffer_info = kzalloc_objs(struct e1000_rx_buffer, rxdr->count);
1052 	if (!rxdr->buffer_info) {
1053 		ret_val = 5;
1054 		goto err_nomem;
1055 	}
1056 
1057 	rxdr->size = rxdr->count * sizeof(struct e1000_rx_desc);
1058 	rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
1059 					GFP_KERNEL);
1060 	if (!rxdr->desc) {
1061 		ret_val = 6;
1062 		goto err_nomem;
1063 	}
1064 	rxdr->next_to_use = rxdr->next_to_clean = 0;
1065 
1066 	rctl = er32(RCTL);
1067 	ew32(RCTL, rctl & ~E1000_RCTL_EN);
1068 	ew32(RDBAL, ((u64)rxdr->dma & 0xFFFFFFFF));
1069 	ew32(RDBAH, ((u64)rxdr->dma >> 32));
1070 	ew32(RDLEN, rxdr->size);
1071 	ew32(RDH, 0);
1072 	ew32(RDT, 0);
1073 	rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
1074 		E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1075 		(hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
1076 	ew32(RCTL, rctl);
1077 
1078 	for (i = 0; i < rxdr->count; i++) {
1079 		struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rxdr, i);
1080 		u8 *buf;
1081 
1082 		buf = kzalloc(E1000_RXBUFFER_2048 + NET_SKB_PAD + NET_IP_ALIGN,
1083 			      GFP_KERNEL);
1084 		if (!buf) {
1085 			ret_val = 7;
1086 			goto err_nomem;
1087 		}
1088 		rxdr->buffer_info[i].rxbuf.data = buf;
1089 
1090 		rxdr->buffer_info[i].dma =
1091 			dma_map_single(&pdev->dev,
1092 				       buf + NET_SKB_PAD + NET_IP_ALIGN,
1093 				       E1000_RXBUFFER_2048, DMA_FROM_DEVICE);
1094 		if (dma_mapping_error(&pdev->dev, rxdr->buffer_info[i].dma)) {
1095 			ret_val = 8;
1096 			goto err_nomem;
1097 		}
1098 		rx_desc->buffer_addr = cpu_to_le64(rxdr->buffer_info[i].dma);
1099 	}
1100 
1101 	return 0;
1102 
1103 err_nomem:
1104 	e1000_free_desc_rings(adapter);
1105 	return ret_val;
1106 }
1107 
1108 static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1109 {
1110 	struct e1000_hw *hw = &adapter->hw;
1111 
1112 	/* Write out to PHY registers 29 and 30 to disable the Receiver. */
1113 	e1000_write_phy_reg(hw, 29, 0x001F);
1114 	e1000_write_phy_reg(hw, 30, 0x8FFC);
1115 	e1000_write_phy_reg(hw, 29, 0x001A);
1116 	e1000_write_phy_reg(hw, 30, 0x8FF0);
1117 }
1118 
1119 static void e1000_phy_reset_clk_and_crs(struct e1000_adapter *adapter)
1120 {
1121 	struct e1000_hw *hw = &adapter->hw;
1122 	u16 phy_reg;
1123 
1124 	/* Because we reset the PHY above, we need to re-force TX_CLK in the
1125 	 * Extended PHY Specific Control Register to 25MHz clock.  This
1126 	 * value defaults back to a 2.5MHz clock when the PHY is reset.
1127 	 */
1128 	e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_reg);
1129 	phy_reg |= M88E1000_EPSCR_TX_CLK_25;
1130 	e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_reg);
1131 
1132 	/* In addition, because of the s/w reset above, we need to enable
1133 	 * CRS on TX.  This must be set for both full and half duplex
1134 	 * operation.
1135 	 */
1136 	e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_reg);
1137 	phy_reg |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1138 	e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_reg);
1139 }
1140 
1141 static int e1000_nonintegrated_phy_loopback(struct e1000_adapter *adapter)
1142 {
1143 	struct e1000_hw *hw = &adapter->hw;
1144 	u32 ctrl_reg;
1145 	u16 phy_reg;
1146 
1147 	/* Setup the Device Control Register for PHY loopback test. */
1148 
1149 	ctrl_reg = er32(CTRL);
1150 	ctrl_reg |= (E1000_CTRL_ILOS |		/* Invert Loss-Of-Signal */
1151 		     E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
1152 		     E1000_CTRL_FRCDPX |	/* Set the Force Duplex Bit */
1153 		     E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
1154 		     E1000_CTRL_FD);		/* Force Duplex to FULL */
1155 
1156 	ew32(CTRL, ctrl_reg);
1157 
1158 	/* Read the PHY Specific Control Register (0x10) */
1159 	e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_reg);
1160 
1161 	/* Clear Auto-Crossover bits in PHY Specific Control Register
1162 	 * (bits 6:5).
1163 	 */
1164 	phy_reg &= ~M88E1000_PSCR_AUTO_X_MODE;
1165 	e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_reg);
1166 
1167 	/* Perform software reset on the PHY */
1168 	e1000_phy_reset(hw);
1169 
1170 	/* Have to setup TX_CLK and TX_CRS after software reset */
1171 	e1000_phy_reset_clk_and_crs(adapter);
1172 
1173 	e1000_write_phy_reg(hw, PHY_CTRL, 0x8100);
1174 
1175 	/* Wait for reset to complete. */
1176 	udelay(500);
1177 
1178 	/* Have to setup TX_CLK and TX_CRS after software reset */
1179 	e1000_phy_reset_clk_and_crs(adapter);
1180 
1181 	/* Write out to PHY registers 29 and 30 to disable the Receiver. */
1182 	e1000_phy_disable_receiver(adapter);
1183 
1184 	/* Set the loopback bit in the PHY control register. */
1185 	e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1186 	phy_reg |= MII_CR_LOOPBACK;
1187 	e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1188 
1189 	/* Setup TX_CLK and TX_CRS one more time. */
1190 	e1000_phy_reset_clk_and_crs(adapter);
1191 
1192 	/* Check Phy Configuration */
1193 	e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1194 	if (phy_reg != 0x4100)
1195 		return 9;
1196 
1197 	e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_reg);
1198 	if (phy_reg != 0x0070)
1199 		return 10;
1200 
1201 	e1000_read_phy_reg(hw, 29, &phy_reg);
1202 	if (phy_reg != 0x001A)
1203 		return 11;
1204 
1205 	return 0;
1206 }
1207 
1208 static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1209 {
1210 	struct e1000_hw *hw = &adapter->hw;
1211 	u32 ctrl_reg = 0;
1212 	u32 stat_reg = 0;
1213 
1214 	hw->autoneg = false;
1215 
1216 	if (hw->phy_type == e1000_phy_m88) {
1217 		/* Auto-MDI/MDIX Off */
1218 		e1000_write_phy_reg(hw,
1219 				    M88E1000_PHY_SPEC_CTRL, 0x0808);
1220 		/* reset to update Auto-MDI/MDIX */
1221 		e1000_write_phy_reg(hw, PHY_CTRL, 0x9140);
1222 		/* autoneg off */
1223 		e1000_write_phy_reg(hw, PHY_CTRL, 0x8140);
1224 	}
1225 
1226 	ctrl_reg = er32(CTRL);
1227 
1228 	/* force 1000, set loopback */
1229 	e1000_write_phy_reg(hw, PHY_CTRL, 0x4140);
1230 
1231 	/* Now set up the MAC to the same speed/duplex as the PHY. */
1232 	ctrl_reg = er32(CTRL);
1233 	ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1234 	ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1235 			E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1236 			E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1237 			E1000_CTRL_FD); /* Force Duplex to FULL */
1238 
1239 	if (hw->media_type == e1000_media_type_copper &&
1240 	    hw->phy_type == e1000_phy_m88)
1241 		ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */
1242 	else {
1243 		/* Set the ILOS bit on the fiber Nic is half
1244 		 * duplex link is detected.
1245 		 */
1246 		stat_reg = er32(STATUS);
1247 		if ((stat_reg & E1000_STATUS_FD) == 0)
1248 			ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1249 	}
1250 
1251 	ew32(CTRL, ctrl_reg);
1252 
1253 	/* Disable the receiver on the PHY so when a cable is plugged in, the
1254 	 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1255 	 */
1256 	if (hw->phy_type == e1000_phy_m88)
1257 		e1000_phy_disable_receiver(adapter);
1258 
1259 	udelay(500);
1260 
1261 	return 0;
1262 }
1263 
1264 static int e1000_set_phy_loopback(struct e1000_adapter *adapter)
1265 {
1266 	struct e1000_hw *hw = &adapter->hw;
1267 	u16 phy_reg = 0;
1268 	u16 count = 0;
1269 
1270 	switch (hw->mac_type) {
1271 	case e1000_82543:
1272 		if (hw->media_type == e1000_media_type_copper) {
1273 			/* Attempt to setup Loopback mode on Non-integrated PHY.
1274 			 * Some PHY registers get corrupted at random, so
1275 			 * attempt this 10 times.
1276 			 */
1277 			while (e1000_nonintegrated_phy_loopback(adapter) &&
1278 			       count++ < 10);
1279 			if (count < 11)
1280 				return 0;
1281 		}
1282 		break;
1283 
1284 	case e1000_82544:
1285 	case e1000_82540:
1286 	case e1000_82545:
1287 	case e1000_82545_rev_3:
1288 	case e1000_82546:
1289 	case e1000_82546_rev_3:
1290 	case e1000_82541:
1291 	case e1000_82541_rev_2:
1292 	case e1000_82547:
1293 	case e1000_82547_rev_2:
1294 		return e1000_integrated_phy_loopback(adapter);
1295 	default:
1296 		/* Default PHY loopback work is to read the MII
1297 		 * control register and assert bit 14 (loopback mode).
1298 		 */
1299 		e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1300 		phy_reg |= MII_CR_LOOPBACK;
1301 		e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1302 		return 0;
1303 	}
1304 
1305 	return 8;
1306 }
1307 
1308 static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1309 {
1310 	struct e1000_hw *hw = &adapter->hw;
1311 	u32 rctl;
1312 
1313 	if (hw->media_type == e1000_media_type_fiber ||
1314 	    hw->media_type == e1000_media_type_internal_serdes) {
1315 		switch (hw->mac_type) {
1316 		case e1000_82545:
1317 		case e1000_82546:
1318 		case e1000_82545_rev_3:
1319 		case e1000_82546_rev_3:
1320 			return e1000_set_phy_loopback(adapter);
1321 		default:
1322 			rctl = er32(RCTL);
1323 			rctl |= E1000_RCTL_LBM_TCVR;
1324 			ew32(RCTL, rctl);
1325 			return 0;
1326 		}
1327 	} else if (hw->media_type == e1000_media_type_copper) {
1328 		return e1000_set_phy_loopback(adapter);
1329 	}
1330 
1331 	return 7;
1332 }
1333 
1334 static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1335 {
1336 	struct e1000_hw *hw = &adapter->hw;
1337 	u32 rctl;
1338 	u16 phy_reg;
1339 
1340 	rctl = er32(RCTL);
1341 	rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1342 	ew32(RCTL, rctl);
1343 
1344 	switch (hw->mac_type) {
1345 	case e1000_82545:
1346 	case e1000_82546:
1347 	case e1000_82545_rev_3:
1348 	case e1000_82546_rev_3:
1349 	default:
1350 		hw->autoneg = true;
1351 		e1000_read_phy_reg(hw, PHY_CTRL, &phy_reg);
1352 		if (phy_reg & MII_CR_LOOPBACK) {
1353 			phy_reg &= ~MII_CR_LOOPBACK;
1354 			e1000_write_phy_reg(hw, PHY_CTRL, phy_reg);
1355 			e1000_phy_reset(hw);
1356 		}
1357 		break;
1358 	}
1359 }
1360 
1361 static void e1000_create_lbtest_frame(struct sk_buff *skb,
1362 				      unsigned int frame_size)
1363 {
1364 	memset(skb->data, 0xFF, frame_size);
1365 	frame_size &= ~1;
1366 	memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1367 	skb->data[frame_size / 2 + 10] = 0xBE;
1368 	skb->data[frame_size / 2 + 12] = 0xAF;
1369 }
1370 
1371 static int e1000_check_lbtest_frame(const unsigned char *data,
1372 				    unsigned int frame_size)
1373 {
1374 	frame_size &= ~1;
1375 	if (*(data + 3) == 0xFF) {
1376 		if ((*(data + frame_size / 2 + 10) == 0xBE) &&
1377 		    (*(data + frame_size / 2 + 12) == 0xAF)) {
1378 			return 0;
1379 		}
1380 	}
1381 	return 13;
1382 }
1383 
1384 static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1385 {
1386 	struct e1000_hw *hw = &adapter->hw;
1387 	struct e1000_tx_ring *txdr = &adapter->test_tx_ring;
1388 	struct e1000_rx_ring *rxdr = &adapter->test_rx_ring;
1389 	struct pci_dev *pdev = adapter->pdev;
1390 	int i, j, k, l, lc, good_cnt, ret_val = 0;
1391 	unsigned long time;
1392 
1393 	ew32(RDT, rxdr->count - 1);
1394 
1395 	/* Calculate the loop count based on the largest descriptor ring
1396 	 * The idea is to wrap the largest ring a number of times using 64
1397 	 * send/receive pairs during each loop
1398 	 */
1399 
1400 	if (rxdr->count <= txdr->count)
1401 		lc = ((txdr->count / 64) * 2) + 1;
1402 	else
1403 		lc = ((rxdr->count / 64) * 2) + 1;
1404 
1405 	k = l = 0;
1406 	for (j = 0; j <= lc; j++) { /* loop count loop */
1407 		for (i = 0; i < 64; i++) { /* send the packets */
1408 			e1000_create_lbtest_frame(txdr->buffer_info[i].skb,
1409 						  1024);
1410 			dma_sync_single_for_device(&pdev->dev,
1411 						   txdr->buffer_info[k].dma,
1412 						   txdr->buffer_info[k].length,
1413 						   DMA_TO_DEVICE);
1414 			if (unlikely(++k == txdr->count))
1415 				k = 0;
1416 		}
1417 		ew32(TDT, k);
1418 		E1000_WRITE_FLUSH();
1419 		msleep(200);
1420 		time = jiffies; /* set the start time for the receive */
1421 		good_cnt = 0;
1422 		do { /* receive the sent packets */
1423 			dma_sync_single_for_cpu(&pdev->dev,
1424 						rxdr->buffer_info[l].dma,
1425 						E1000_RXBUFFER_2048,
1426 						DMA_FROM_DEVICE);
1427 
1428 			ret_val = e1000_check_lbtest_frame(
1429 					rxdr->buffer_info[l].rxbuf.data +
1430 					NET_SKB_PAD + NET_IP_ALIGN,
1431 					1024);
1432 			if (!ret_val)
1433 				good_cnt++;
1434 			if (unlikely(++l == rxdr->count))
1435 				l = 0;
1436 			/* time + 20 msecs (200 msecs on 2.4) is more than
1437 			 * enough time to complete the receives, if it's
1438 			 * exceeded, break and error off
1439 			 */
1440 		} while (good_cnt < 64 && time_after(time + 20, jiffies));
1441 
1442 		if (good_cnt != 64) {
1443 			ret_val = 13; /* ret_val is the same as mis-compare */
1444 			break;
1445 		}
1446 		if (time_after_eq(jiffies, time + 2)) {
1447 			ret_val = 14; /* error code for time out error */
1448 			break;
1449 		}
1450 	} /* end loop count loop */
1451 	return ret_val;
1452 }
1453 
1454 static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1455 {
1456 	*data = e1000_setup_desc_rings(adapter);
1457 	if (*data)
1458 		goto out;
1459 	*data = e1000_setup_loopback_test(adapter);
1460 	if (*data)
1461 		goto err_loopback;
1462 	*data = e1000_run_loopback_test(adapter);
1463 	e1000_loopback_cleanup(adapter);
1464 
1465 err_loopback:
1466 	e1000_free_desc_rings(adapter);
1467 out:
1468 	return *data;
1469 }
1470 
1471 static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1472 {
1473 	struct e1000_hw *hw = &adapter->hw;
1474 	*data = 0;
1475 	if (hw->media_type == e1000_media_type_internal_serdes) {
1476 		int i = 0;
1477 
1478 		hw->serdes_has_link = false;
1479 
1480 		/* On some blade server designs, link establishment
1481 		 * could take as long as 2-3 minutes
1482 		 */
1483 		do {
1484 			e1000_check_for_link(hw);
1485 			if (hw->serdes_has_link)
1486 				return *data;
1487 			msleep(20);
1488 		} while (i++ < 3750);
1489 
1490 		*data = 1;
1491 	} else {
1492 		e1000_check_for_link(hw);
1493 		if (hw->autoneg)  /* if auto_neg is set wait for it */
1494 			msleep(4000);
1495 
1496 		if (!(er32(STATUS) & E1000_STATUS_LU))
1497 			*data = 1;
1498 	}
1499 	return *data;
1500 }
1501 
1502 static int e1000_get_sset_count(struct net_device *netdev, int sset)
1503 {
1504 	switch (sset) {
1505 	case ETH_SS_TEST:
1506 		return E1000_TEST_LEN;
1507 	case ETH_SS_STATS:
1508 		return E1000_STATS_LEN;
1509 	default:
1510 		return -EOPNOTSUPP;
1511 	}
1512 }
1513 
1514 static void e1000_diag_test(struct net_device *netdev,
1515 			    struct ethtool_test *eth_test, u64 *data)
1516 {
1517 	struct e1000_adapter *adapter = netdev_priv(netdev);
1518 	struct e1000_hw *hw = &adapter->hw;
1519 	bool if_running = netif_running(netdev);
1520 
1521 	set_bit(__E1000_TESTING, &adapter->flags);
1522 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1523 		/* Offline tests */
1524 
1525 		/* save speed, duplex, autoneg settings */
1526 		u16 autoneg_advertised = hw->autoneg_advertised;
1527 		u8 forced_speed_duplex = hw->forced_speed_duplex;
1528 		u8 autoneg = hw->autoneg;
1529 
1530 		e_info(hw, "offline testing starting\n");
1531 
1532 		/* Link test performed before hardware reset so autoneg doesn't
1533 		 * interfere with test result
1534 		 */
1535 		if (e1000_link_test(adapter, &data[4]))
1536 			eth_test->flags |= ETH_TEST_FL_FAILED;
1537 
1538 		if (if_running)
1539 			/* indicate we're in test mode */
1540 			e1000_close(netdev);
1541 		else
1542 			e1000_reset(adapter);
1543 
1544 		if (e1000_reg_test(adapter, &data[0]))
1545 			eth_test->flags |= ETH_TEST_FL_FAILED;
1546 
1547 		e1000_reset(adapter);
1548 		if (e1000_eeprom_test(adapter, &data[1]))
1549 			eth_test->flags |= ETH_TEST_FL_FAILED;
1550 
1551 		e1000_reset(adapter);
1552 		if (e1000_intr_test(adapter, &data[2]))
1553 			eth_test->flags |= ETH_TEST_FL_FAILED;
1554 
1555 		e1000_reset(adapter);
1556 		/* make sure the phy is powered up */
1557 		e1000_power_up_phy(adapter);
1558 		if (e1000_loopback_test(adapter, &data[3]))
1559 			eth_test->flags |= ETH_TEST_FL_FAILED;
1560 
1561 		/* restore speed, duplex, autoneg settings */
1562 		hw->autoneg_advertised = autoneg_advertised;
1563 		hw->forced_speed_duplex = forced_speed_duplex;
1564 		hw->autoneg = autoneg;
1565 
1566 		e1000_reset(adapter);
1567 		clear_bit(__E1000_TESTING, &adapter->flags);
1568 		if (if_running)
1569 			e1000_open(netdev);
1570 	} else {
1571 		e_info(hw, "online testing starting\n");
1572 		/* Online tests */
1573 		if (e1000_link_test(adapter, &data[4]))
1574 			eth_test->flags |= ETH_TEST_FL_FAILED;
1575 
1576 		/* Online tests aren't run; pass by default */
1577 		data[0] = 0;
1578 		data[1] = 0;
1579 		data[2] = 0;
1580 		data[3] = 0;
1581 
1582 		clear_bit(__E1000_TESTING, &adapter->flags);
1583 	}
1584 	msleep_interruptible(4 * 1000);
1585 }
1586 
1587 static int e1000_wol_exclusion(struct e1000_adapter *adapter,
1588 			       struct ethtool_wolinfo *wol)
1589 {
1590 	struct e1000_hw *hw = &adapter->hw;
1591 	int retval = 1; /* fail by default */
1592 
1593 	switch (hw->device_id) {
1594 	case E1000_DEV_ID_82542:
1595 	case E1000_DEV_ID_82543GC_FIBER:
1596 	case E1000_DEV_ID_82543GC_COPPER:
1597 	case E1000_DEV_ID_82544EI_FIBER:
1598 	case E1000_DEV_ID_82546EB_QUAD_COPPER:
1599 	case E1000_DEV_ID_82545EM_FIBER:
1600 	case E1000_DEV_ID_82545EM_COPPER:
1601 	case E1000_DEV_ID_82546GB_QUAD_COPPER:
1602 	case E1000_DEV_ID_82546GB_PCIE:
1603 		/* these don't support WoL at all */
1604 		wol->supported = 0;
1605 		break;
1606 	case E1000_DEV_ID_82546EB_FIBER:
1607 	case E1000_DEV_ID_82546GB_FIBER:
1608 		/* Wake events not supported on port B */
1609 		if (er32(STATUS) & E1000_STATUS_FUNC_1) {
1610 			wol->supported = 0;
1611 			break;
1612 		}
1613 		/* return success for non excluded adapter ports */
1614 		retval = 0;
1615 		break;
1616 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1617 		/* quad port adapters only support WoL on port A */
1618 		if (!adapter->quad_port_a) {
1619 			wol->supported = 0;
1620 			break;
1621 		}
1622 		/* return success for non excluded adapter ports */
1623 		retval = 0;
1624 		break;
1625 	default:
1626 		/* dual port cards only support WoL on port A from now on
1627 		 * unless it was enabled in the eeprom for port B
1628 		 * so exclude FUNC_1 ports from having WoL enabled
1629 		 */
1630 		if (er32(STATUS) & E1000_STATUS_FUNC_1 &&
1631 		    !adapter->eeprom_wol) {
1632 			wol->supported = 0;
1633 			break;
1634 		}
1635 
1636 		retval = 0;
1637 	}
1638 
1639 	return retval;
1640 }
1641 
1642 static void e1000_get_wol(struct net_device *netdev,
1643 			  struct ethtool_wolinfo *wol)
1644 {
1645 	struct e1000_adapter *adapter = netdev_priv(netdev);
1646 	struct e1000_hw *hw = &adapter->hw;
1647 
1648 	wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
1649 	wol->wolopts = 0;
1650 
1651 	/* this function will set ->supported = 0 and return 1 if wol is not
1652 	 * supported by this hardware
1653 	 */
1654 	if (e1000_wol_exclusion(adapter, wol) ||
1655 	    !device_can_wakeup(&adapter->pdev->dev))
1656 		return;
1657 
1658 	/* apply any specific unsupported masks here */
1659 	switch (hw->device_id) {
1660 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1661 		/* KSP3 does not support UCAST wake-ups */
1662 		wol->supported &= ~WAKE_UCAST;
1663 
1664 		if (adapter->wol & E1000_WUFC_EX)
1665 			e_err(drv, "Interface does not support directed "
1666 			      "(unicast) frame wake-up packets\n");
1667 		break;
1668 	default:
1669 		break;
1670 	}
1671 
1672 	if (adapter->wol & E1000_WUFC_EX)
1673 		wol->wolopts |= WAKE_UCAST;
1674 	if (adapter->wol & E1000_WUFC_MC)
1675 		wol->wolopts |= WAKE_MCAST;
1676 	if (adapter->wol & E1000_WUFC_BC)
1677 		wol->wolopts |= WAKE_BCAST;
1678 	if (adapter->wol & E1000_WUFC_MAG)
1679 		wol->wolopts |= WAKE_MAGIC;
1680 }
1681 
1682 static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1683 {
1684 	struct e1000_adapter *adapter = netdev_priv(netdev);
1685 	struct e1000_hw *hw = &adapter->hw;
1686 
1687 	if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
1688 		return -EOPNOTSUPP;
1689 
1690 	if (e1000_wol_exclusion(adapter, wol) ||
1691 	    !device_can_wakeup(&adapter->pdev->dev))
1692 		return wol->wolopts ? -EOPNOTSUPP : 0;
1693 
1694 	switch (hw->device_id) {
1695 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1696 		if (wol->wolopts & WAKE_UCAST) {
1697 			e_err(drv, "Interface does not support directed "
1698 			      "(unicast) frame wake-up packets\n");
1699 			return -EOPNOTSUPP;
1700 		}
1701 		break;
1702 	default:
1703 		break;
1704 	}
1705 
1706 	/* these settings will always override what we currently have */
1707 	adapter->wol = 0;
1708 
1709 	if (wol->wolopts & WAKE_UCAST)
1710 		adapter->wol |= E1000_WUFC_EX;
1711 	if (wol->wolopts & WAKE_MCAST)
1712 		adapter->wol |= E1000_WUFC_MC;
1713 	if (wol->wolopts & WAKE_BCAST)
1714 		adapter->wol |= E1000_WUFC_BC;
1715 	if (wol->wolopts & WAKE_MAGIC)
1716 		adapter->wol |= E1000_WUFC_MAG;
1717 
1718 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1719 
1720 	return 0;
1721 }
1722 
1723 static int e1000_set_phys_id(struct net_device *netdev,
1724 			     enum ethtool_phys_id_state state)
1725 {
1726 	struct e1000_adapter *adapter = netdev_priv(netdev);
1727 	struct e1000_hw *hw = &adapter->hw;
1728 
1729 	switch (state) {
1730 	case ETHTOOL_ID_ACTIVE:
1731 		e1000_setup_led(hw);
1732 		return 2;
1733 
1734 	case ETHTOOL_ID_ON:
1735 		e1000_led_on(hw);
1736 		break;
1737 
1738 	case ETHTOOL_ID_OFF:
1739 		e1000_led_off(hw);
1740 		break;
1741 
1742 	case ETHTOOL_ID_INACTIVE:
1743 		e1000_cleanup_led(hw);
1744 	}
1745 
1746 	return 0;
1747 }
1748 
1749 static int e1000_get_coalesce(struct net_device *netdev,
1750 			      struct ethtool_coalesce *ec,
1751 			      struct kernel_ethtool_coalesce *kernel_coal,
1752 			      struct netlink_ext_ack *extack)
1753 {
1754 	struct e1000_adapter *adapter = netdev_priv(netdev);
1755 
1756 	if (adapter->hw.mac_type < e1000_82545)
1757 		return -EOPNOTSUPP;
1758 
1759 	if (adapter->itr_setting <= 4)
1760 		ec->rx_coalesce_usecs = adapter->itr_setting;
1761 	else
1762 		ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1763 
1764 	return 0;
1765 }
1766 
1767 static int e1000_set_coalesce(struct net_device *netdev,
1768 			      struct ethtool_coalesce *ec,
1769 			      struct kernel_ethtool_coalesce *kernel_coal,
1770 			      struct netlink_ext_ack *extack)
1771 {
1772 	struct e1000_adapter *adapter = netdev_priv(netdev);
1773 	struct e1000_hw *hw = &adapter->hw;
1774 
1775 	if (hw->mac_type < e1000_82545)
1776 		return -EOPNOTSUPP;
1777 
1778 	if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
1779 	    ((ec->rx_coalesce_usecs > 4) &&
1780 	     (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
1781 	    (ec->rx_coalesce_usecs == 2))
1782 		return -EINVAL;
1783 
1784 	if (ec->rx_coalesce_usecs == 4) {
1785 		adapter->itr = adapter->itr_setting = 4;
1786 	} else if (ec->rx_coalesce_usecs <= 3) {
1787 		adapter->itr = 20000;
1788 		adapter->itr_setting = ec->rx_coalesce_usecs;
1789 	} else {
1790 		adapter->itr = (1000000 / ec->rx_coalesce_usecs);
1791 		adapter->itr_setting = adapter->itr & ~3;
1792 	}
1793 
1794 	if (adapter->itr_setting != 0)
1795 		ew32(ITR, 1000000000 / (adapter->itr * 256));
1796 	else
1797 		ew32(ITR, 0);
1798 
1799 	return 0;
1800 }
1801 
1802 static int e1000_nway_reset(struct net_device *netdev)
1803 {
1804 	struct e1000_adapter *adapter = netdev_priv(netdev);
1805 
1806 	if (netif_running(netdev))
1807 		e1000_reinit_locked(adapter);
1808 	return 0;
1809 }
1810 
1811 static void e1000_get_ethtool_stats(struct net_device *netdev,
1812 				    struct ethtool_stats *stats, u64 *data)
1813 {
1814 	struct e1000_adapter *adapter = netdev_priv(netdev);
1815 	int i;
1816 	const struct e1000_stats *stat = e1000_gstrings_stats;
1817 
1818 	e1000_update_stats(adapter);
1819 	for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++, stat++) {
1820 		char *p;
1821 
1822 		switch (stat->type) {
1823 		case NETDEV_STATS:
1824 			p = (char *)netdev + stat->stat_offset;
1825 			break;
1826 		case E1000_STATS:
1827 			p = (char *)adapter + stat->stat_offset;
1828 			break;
1829 		default:
1830 			netdev_WARN_ONCE(netdev, "Invalid E1000 stat type: %u index %d\n",
1831 					 stat->type, i);
1832 			continue;
1833 		}
1834 
1835 		if (stat->sizeof_stat == sizeof(u64))
1836 			data[i] = *(u64 *)p;
1837 		else
1838 			data[i] = *(u32 *)p;
1839 	}
1840 /* BUG_ON(i != E1000_STATS_LEN); */
1841 }
1842 
1843 static void e1000_get_strings(struct net_device *netdev, u32 stringset,
1844 			      u8 *data)
1845 {
1846 	u8 *p = data;
1847 	int i;
1848 
1849 	switch (stringset) {
1850 	case ETH_SS_TEST:
1851 		memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
1852 		break;
1853 	case ETH_SS_STATS:
1854 		for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
1855 			memcpy(p, e1000_gstrings_stats[i].stat_string,
1856 			       ETH_GSTRING_LEN);
1857 			p += ETH_GSTRING_LEN;
1858 		}
1859 		/* BUG_ON(p - data != E1000_STATS_LEN * ETH_GSTRING_LEN); */
1860 		break;
1861 	}
1862 }
1863 
1864 static const struct ethtool_ops e1000_ethtool_ops = {
1865 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1866 	.get_drvinfo		= e1000_get_drvinfo,
1867 	.get_regs_len		= e1000_get_regs_len,
1868 	.get_regs		= e1000_get_regs,
1869 	.get_wol		= e1000_get_wol,
1870 	.set_wol		= e1000_set_wol,
1871 	.get_msglevel		= e1000_get_msglevel,
1872 	.set_msglevel		= e1000_set_msglevel,
1873 	.nway_reset		= e1000_nway_reset,
1874 	.get_link		= e1000_get_link,
1875 	.get_eeprom_len		= e1000_get_eeprom_len,
1876 	.get_eeprom		= e1000_get_eeprom,
1877 	.set_eeprom		= e1000_set_eeprom,
1878 	.get_ringparam		= e1000_get_ringparam,
1879 	.set_ringparam		= e1000_set_ringparam,
1880 	.get_pauseparam		= e1000_get_pauseparam,
1881 	.set_pauseparam		= e1000_set_pauseparam,
1882 	.self_test		= e1000_diag_test,
1883 	.get_strings		= e1000_get_strings,
1884 	.set_phys_id		= e1000_set_phys_id,
1885 	.get_ethtool_stats	= e1000_get_ethtool_stats,
1886 	.get_sset_count		= e1000_get_sset_count,
1887 	.get_coalesce		= e1000_get_coalesce,
1888 	.set_coalesce		= e1000_set_coalesce,
1889 	.get_ts_info		= ethtool_op_get_ts_info,
1890 	.get_link_ksettings	= e1000_get_link_ksettings,
1891 	.set_link_ksettings	= e1000_set_link_ksettings,
1892 };
1893 
1894 void e1000_set_ethtool_ops(struct net_device *netdev)
1895 {
1896 	netdev->ethtool_ops = &e1000_ethtool_ops;
1897 }
1898