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