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