xref: /linux/drivers/net/ethernet/intel/e1000e/ethtool.c (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
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 	u16 *eeprom_buff;
555 	int ret_val = 0;
556 	size_t max_len;
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 	first_word = eeprom->offset >> 1;
575 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
576 	eeprom_buff = kmalloc(max_len, GFP_KERNEL);
577 	if (!eeprom_buff)
578 		return -ENOMEM;
579 
580 	ptr = (void *)eeprom_buff;
581 
582 	if (eeprom->offset & 1) {
583 		/* need read/modify/write of first changed EEPROM word */
584 		/* only the second byte of the word is being modified */
585 		ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]);
586 		if (ret_val)
587 			goto out;
588 
589 		/* Device's eeprom is always little-endian, word addressable */
590 		le16_to_cpus(&eeprom_buff[0]);
591 
592 		ptr++;
593 	}
594 	if ((eeprom->offset + eeprom->len) & 1) {
595 		/* need read/modify/write of last changed EEPROM word */
596 		/* only the first byte of the word is being modified */
597 		ret_val = e1000_read_nvm(hw, last_word, 1,
598 					 &eeprom_buff[last_word - first_word]);
599 		if (ret_val)
600 			goto out;
601 
602 		/* Device's eeprom is always little-endian, word addressable */
603 		le16_to_cpus(&eeprom_buff[last_word - first_word]);
604 	}
605 
606 	memcpy(ptr, bytes, eeprom->len);
607 
608 	for (i = 0; i < last_word - first_word + 1; i++)
609 		cpu_to_le16s(&eeprom_buff[i]);
610 
611 	ret_val = e1000_write_nvm(hw, first_word,
612 				  last_word - first_word + 1, eeprom_buff);
613 
614 	if (ret_val)
615 		goto out;
616 
617 	/* Update the checksum over the first part of the EEPROM if needed
618 	 * and flush shadow RAM for applicable controllers
619 	 */
620 	if ((first_word <= NVM_CHECKSUM_REG) ||
621 	    (hw->mac.type == e1000_82583) ||
622 	    (hw->mac.type == e1000_82574) ||
623 	    (hw->mac.type == e1000_82573))
624 		ret_val = e1000e_update_nvm_checksum(hw);
625 
626 out:
627 	kfree(eeprom_buff);
628 	return ret_val;
629 }
630 
631 static void e1000_get_drvinfo(struct net_device *netdev,
632 			      struct ethtool_drvinfo *drvinfo)
633 {
634 	struct e1000_adapter *adapter = netdev_priv(netdev);
635 
636 	strscpy(drvinfo->driver, e1000e_driver_name, sizeof(drvinfo->driver));
637 
638 	/* EEPROM image version # is reported as firmware version # for
639 	 * PCI-E controllers
640 	 */
641 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
642 		 "%d.%d-%d",
643 		 FIELD_GET(0xF000, adapter->eeprom_vers),
644 		 FIELD_GET(0x0FF0, adapter->eeprom_vers),
645 		 (adapter->eeprom_vers & 0x000F));
646 
647 	strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
648 		sizeof(drvinfo->bus_info));
649 }
650 
651 static void e1000_get_ringparam(struct net_device *netdev,
652 				struct ethtool_ringparam *ring,
653 				struct kernel_ethtool_ringparam *kernel_ring,
654 				struct netlink_ext_ack *extack)
655 {
656 	struct e1000_adapter *adapter = netdev_priv(netdev);
657 
658 	ring->rx_max_pending = E1000_MAX_RXD;
659 	ring->tx_max_pending = E1000_MAX_TXD;
660 	ring->rx_pending = adapter->rx_ring_count;
661 	ring->tx_pending = adapter->tx_ring_count;
662 }
663 
664 static int e1000_set_ringparam(struct net_device *netdev,
665 			       struct ethtool_ringparam *ring,
666 			       struct kernel_ethtool_ringparam *kernel_ring,
667 			       struct netlink_ext_ack *extack)
668 {
669 	struct e1000_adapter *adapter = netdev_priv(netdev);
670 	struct e1000_ring *temp_tx = NULL, *temp_rx = NULL;
671 	int err = 0, size = sizeof(struct e1000_ring);
672 	bool set_tx = false, set_rx = false;
673 	u16 new_rx_count, new_tx_count;
674 
675 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
676 		return -EINVAL;
677 
678 	new_rx_count = clamp_t(u32, ring->rx_pending, E1000_MIN_RXD,
679 			       E1000_MAX_RXD);
680 	new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
681 
682 	new_tx_count = clamp_t(u32, ring->tx_pending, E1000_MIN_TXD,
683 			       E1000_MAX_TXD);
684 	new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
685 
686 	if ((new_tx_count == adapter->tx_ring_count) &&
687 	    (new_rx_count == adapter->rx_ring_count))
688 		/* nothing to do */
689 		return 0;
690 
691 	while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
692 		usleep_range(1000, 2000);
693 
694 	if (!netif_running(adapter->netdev)) {
695 		/* Set counts now and allocate resources during open() */
696 		adapter->tx_ring->count = new_tx_count;
697 		adapter->rx_ring->count = new_rx_count;
698 		adapter->tx_ring_count = new_tx_count;
699 		adapter->rx_ring_count = new_rx_count;
700 		goto clear_reset;
701 	}
702 
703 	set_tx = (new_tx_count != adapter->tx_ring_count);
704 	set_rx = (new_rx_count != adapter->rx_ring_count);
705 
706 	/* Allocate temporary storage for ring updates */
707 	if (set_tx) {
708 		temp_tx = vmalloc(size);
709 		if (!temp_tx) {
710 			err = -ENOMEM;
711 			goto free_temp;
712 		}
713 	}
714 	if (set_rx) {
715 		temp_rx = vmalloc(size);
716 		if (!temp_rx) {
717 			err = -ENOMEM;
718 			goto free_temp;
719 		}
720 	}
721 
722 	e1000e_down(adapter, true);
723 
724 	/* We can't just free everything and then setup again, because the
725 	 * ISRs in MSI-X mode get passed pointers to the Tx and Rx ring
726 	 * structs.  First, attempt to allocate new resources...
727 	 */
728 	if (set_tx) {
729 		memcpy(temp_tx, adapter->tx_ring, size);
730 		temp_tx->count = new_tx_count;
731 		err = e1000e_setup_tx_resources(temp_tx);
732 		if (err)
733 			goto err_setup;
734 	}
735 	if (set_rx) {
736 		memcpy(temp_rx, adapter->rx_ring, size);
737 		temp_rx->count = new_rx_count;
738 		err = e1000e_setup_rx_resources(temp_rx);
739 		if (err)
740 			goto err_setup_rx;
741 	}
742 
743 	/* ...then free the old resources and copy back any new ring data */
744 	if (set_tx) {
745 		e1000e_free_tx_resources(adapter->tx_ring);
746 		memcpy(adapter->tx_ring, temp_tx, size);
747 		adapter->tx_ring_count = new_tx_count;
748 	}
749 	if (set_rx) {
750 		e1000e_free_rx_resources(adapter->rx_ring);
751 		memcpy(adapter->rx_ring, temp_rx, size);
752 		adapter->rx_ring_count = new_rx_count;
753 	}
754 
755 err_setup_rx:
756 	if (err && set_tx)
757 		e1000e_free_tx_resources(temp_tx);
758 err_setup:
759 	e1000e_up(adapter);
760 free_temp:
761 	vfree(temp_tx);
762 	vfree(temp_rx);
763 clear_reset:
764 	clear_bit(__E1000_RESETTING, &adapter->state);
765 	return err;
766 }
767 
768 static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data,
769 			     int reg, int offset, u32 mask, u32 write)
770 {
771 	u32 pat, val;
772 	static const u32 test[] = {
773 		0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
774 	};
775 	for (pat = 0; pat < ARRAY_SIZE(test); pat++) {
776 		E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset,
777 				      (test[pat] & write));
778 		val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset);
779 		if (val != (test[pat] & write & mask)) {
780 			e_err("pattern test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n",
781 			      reg + (offset << 2), val,
782 			      (test[pat] & write & mask));
783 			*data = reg;
784 			return true;
785 		}
786 	}
787 	return false;
788 }
789 
790 static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data,
791 			      int reg, u32 mask, u32 write)
792 {
793 	u32 val;
794 
795 	__ew32(&adapter->hw, reg, write & mask);
796 	val = __er32(&adapter->hw, reg);
797 	if ((write & mask) != (val & mask)) {
798 		e_err("set/check test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n",
799 		      reg, (val & mask), (write & mask));
800 		*data = reg;
801 		return true;
802 	}
803 	return false;
804 }
805 
806 #define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write)                       \
807 	do {                                                                   \
808 		if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \
809 			return 1;                                              \
810 	} while (0)
811 #define REG_PATTERN_TEST(reg, mask, write)                                     \
812 	REG_PATTERN_TEST_ARRAY(reg, 0, mask, write)
813 
814 #define REG_SET_AND_CHECK(reg, mask, write)                                    \
815 	do {                                                                   \
816 		if (reg_set_and_check(adapter, data, reg, mask, write))        \
817 			return 1;                                              \
818 	} while (0)
819 
820 static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data)
821 {
822 	struct e1000_hw *hw = &adapter->hw;
823 	struct e1000_mac_info *mac = &adapter->hw.mac;
824 	u32 value;
825 	u32 before;
826 	u32 after;
827 	u32 i;
828 	u32 toggle;
829 	u32 mask;
830 	u32 wlock_mac = 0;
831 
832 	/* The status register is Read Only, so a write should fail.
833 	 * Some bits that get toggled are ignored.  There are several bits
834 	 * on newer hardware that are r/w.
835 	 */
836 	switch (mac->type) {
837 	case e1000_82571:
838 	case e1000_82572:
839 	case e1000_80003es2lan:
840 		toggle = 0x7FFFF3FF;
841 		break;
842 	default:
843 		toggle = 0x7FFFF033;
844 		break;
845 	}
846 
847 	before = er32(STATUS);
848 	value = (er32(STATUS) & toggle);
849 	ew32(STATUS, toggle);
850 	after = er32(STATUS) & toggle;
851 	if (value != after) {
852 		e_err("failed STATUS register test got: 0x%08X expected: 0x%08X\n",
853 		      after, value);
854 		*data = 1;
855 		return 1;
856 	}
857 	/* restore previous status */
858 	ew32(STATUS, before);
859 
860 	if (!(adapter->flags & FLAG_IS_ICH)) {
861 		REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF);
862 		REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF);
863 		REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF);
864 		REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF);
865 	}
866 
867 	REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF);
868 	REG_PATTERN_TEST(E1000_RDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
869 	REG_PATTERN_TEST(E1000_RDLEN(0), 0x000FFF80, 0x000FFFFF);
870 	REG_PATTERN_TEST(E1000_RDH(0), 0x0000FFFF, 0x0000FFFF);
871 	REG_PATTERN_TEST(E1000_RDT(0), 0x0000FFFF, 0x0000FFFF);
872 	REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8);
873 	REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF);
874 	REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF);
875 	REG_PATTERN_TEST(E1000_TDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF);
876 	REG_PATTERN_TEST(E1000_TDLEN(0), 0x000FFF80, 0x000FFFFF);
877 
878 	REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000);
879 
880 	before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE);
881 	REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB);
882 	REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000);
883 
884 	REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF);
885 	REG_PATTERN_TEST(E1000_RDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
886 	if (!(adapter->flags & FLAG_IS_ICH))
887 		REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF);
888 	REG_PATTERN_TEST(E1000_TDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF);
889 	REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF);
890 	mask = 0x8003FFFF;
891 	switch (mac->type) {
892 	case e1000_ich10lan:
893 	case e1000_pchlan:
894 	case e1000_pch2lan:
895 	case e1000_pch_lpt:
896 	case e1000_pch_spt:
897 	case e1000_pch_cnp:
898 	case e1000_pch_tgp:
899 	case e1000_pch_adp:
900 	case e1000_pch_mtp:
901 	case e1000_pch_lnp:
902 	case e1000_pch_ptp:
903 	case e1000_pch_nvp:
904 		mask |= BIT(18);
905 		break;
906 	default:
907 		break;
908 	}
909 
910 	if (mac->type >= e1000_pch_lpt)
911 		wlock_mac = FIELD_GET(E1000_FWSM_WLOCK_MAC_MASK, er32(FWSM));
912 
913 	for (i = 0; i < mac->rar_entry_count; i++) {
914 		if (mac->type >= e1000_pch_lpt) {
915 			/* Cannot test write-protected SHRAL[n] registers */
916 			if ((wlock_mac == 1) || (wlock_mac && (i > wlock_mac)))
917 				continue;
918 
919 			/* SHRAH[9] different than the others */
920 			if (i == 10)
921 				mask |= BIT(30);
922 			else
923 				mask &= ~BIT(30);
924 		}
925 		if (mac->type == e1000_pch2lan) {
926 			/* SHRAH[0,1,2] different than previous */
927 			if (i == 1)
928 				mask &= 0xFFF4FFFF;
929 			/* SHRAH[3] different than SHRAH[0,1,2] */
930 			if (i == 4)
931 				mask |= BIT(30);
932 			/* RAR[1-6] owned by management engine - skipping */
933 			if (i > 0)
934 				i += 6;
935 		}
936 
937 		REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1), mask,
938 				       0xFFFFFFFF);
939 		/* reset index to actual value */
940 		if ((mac->type == e1000_pch2lan) && (i > 6))
941 			i -= 6;
942 	}
943 
944 	for (i = 0; i < mac->mta_reg_count; i++)
945 		REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF);
946 
947 	*data = 0;
948 
949 	return 0;
950 }
951 
952 static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data)
953 {
954 	u16 temp;
955 	u16 checksum = 0;
956 	u16 i;
957 
958 	*data = 0;
959 	/* Read and add up the contents of the EEPROM */
960 	for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
961 		if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) {
962 			*data = 1;
963 			return *data;
964 		}
965 		checksum += temp;
966 	}
967 
968 	/* If Checksum is not Correct return error else test passed */
969 	if (checksum != NVM_SUM && !(*data))
970 		*data = 2;
971 
972 	return *data;
973 }
974 
975 static irqreturn_t e1000_test_intr(int __always_unused irq, void *data)
976 {
977 	struct net_device *netdev = (struct net_device *)data;
978 	struct e1000_adapter *adapter = netdev_priv(netdev);
979 	struct e1000_hw *hw = &adapter->hw;
980 
981 	adapter->test_icr |= er32(ICR);
982 
983 	return IRQ_HANDLED;
984 }
985 
986 static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data)
987 {
988 	struct net_device *netdev = adapter->netdev;
989 	struct e1000_hw *hw = &adapter->hw;
990 	u32 mask;
991 	u32 shared_int = 1;
992 	u32 irq = adapter->pdev->irq;
993 	int i;
994 	int ret_val = 0;
995 	int int_mode = E1000E_INT_MODE_LEGACY;
996 
997 	*data = 0;
998 
999 	/* NOTE: we don't test MSI/MSI-X interrupts here, yet */
1000 	if (adapter->int_mode == E1000E_INT_MODE_MSIX) {
1001 		int_mode = adapter->int_mode;
1002 		e1000e_reset_interrupt_capability(adapter);
1003 		adapter->int_mode = E1000E_INT_MODE_LEGACY;
1004 		e1000e_set_interrupt_capability(adapter);
1005 	}
1006 	/* Hook up test interrupt handler just for this test */
1007 	if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name,
1008 			 netdev)) {
1009 		shared_int = 0;
1010 	} else if (request_irq(irq, e1000_test_intr, IRQF_SHARED, netdev->name,
1011 			       netdev)) {
1012 		*data = 1;
1013 		ret_val = -1;
1014 		goto out;
1015 	}
1016 	e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared"));
1017 
1018 	/* Disable all the interrupts */
1019 	ew32(IMC, 0xFFFFFFFF);
1020 	e1e_flush();
1021 	usleep_range(10000, 11000);
1022 
1023 	/* Test each interrupt */
1024 	for (i = 0; i < 10; i++) {
1025 		/* Interrupt to test */
1026 		mask = BIT(i);
1027 
1028 		if (adapter->flags & FLAG_IS_ICH) {
1029 			switch (mask) {
1030 			case E1000_ICR_RXSEQ:
1031 				continue;
1032 			case 0x00000100:
1033 				if (adapter->hw.mac.type == e1000_ich8lan ||
1034 				    adapter->hw.mac.type == e1000_ich9lan)
1035 					continue;
1036 				break;
1037 			default:
1038 				break;
1039 			}
1040 		}
1041 
1042 		if (!shared_int) {
1043 			/* Disable the interrupt to be reported in
1044 			 * the cause register and then force the same
1045 			 * interrupt and see if one gets posted.  If
1046 			 * an interrupt was posted to the bus, the
1047 			 * test failed.
1048 			 */
1049 			adapter->test_icr = 0;
1050 			ew32(IMC, mask);
1051 			ew32(ICS, mask);
1052 			e1e_flush();
1053 			usleep_range(10000, 11000);
1054 
1055 			if (adapter->test_icr & mask) {
1056 				*data = 3;
1057 				break;
1058 			}
1059 		}
1060 
1061 		/* Enable the interrupt to be reported in
1062 		 * the cause register and then force the same
1063 		 * interrupt and see if one gets posted.  If
1064 		 * an interrupt was not posted to the bus, the
1065 		 * test failed.
1066 		 */
1067 		adapter->test_icr = 0;
1068 		ew32(IMS, mask);
1069 		ew32(ICS, mask);
1070 		e1e_flush();
1071 		usleep_range(10000, 11000);
1072 
1073 		if (!(adapter->test_icr & mask)) {
1074 			*data = 4;
1075 			break;
1076 		}
1077 
1078 		if (!shared_int) {
1079 			/* Disable the other interrupts to be reported in
1080 			 * the cause register and then force the other
1081 			 * interrupts and see if any get posted.  If
1082 			 * an interrupt was posted to the bus, the
1083 			 * test failed.
1084 			 */
1085 			adapter->test_icr = 0;
1086 			ew32(IMC, ~mask & 0x00007FFF);
1087 			ew32(ICS, ~mask & 0x00007FFF);
1088 			e1e_flush();
1089 			usleep_range(10000, 11000);
1090 
1091 			if (adapter->test_icr) {
1092 				*data = 5;
1093 				break;
1094 			}
1095 		}
1096 	}
1097 
1098 	/* Disable all the interrupts */
1099 	ew32(IMC, 0xFFFFFFFF);
1100 	e1e_flush();
1101 	usleep_range(10000, 11000);
1102 
1103 	/* Unhook test interrupt handler */
1104 	free_irq(irq, netdev);
1105 
1106 out:
1107 	if (int_mode == E1000E_INT_MODE_MSIX) {
1108 		e1000e_reset_interrupt_capability(adapter);
1109 		adapter->int_mode = int_mode;
1110 		e1000e_set_interrupt_capability(adapter);
1111 	}
1112 
1113 	return ret_val;
1114 }
1115 
1116 static void e1000_free_desc_rings(struct e1000_adapter *adapter)
1117 {
1118 	struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1119 	struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1120 	struct pci_dev *pdev = adapter->pdev;
1121 	struct e1000_buffer *buffer_info;
1122 	int i;
1123 
1124 	if (tx_ring->desc && tx_ring->buffer_info) {
1125 		for (i = 0; i < tx_ring->count; i++) {
1126 			buffer_info = &tx_ring->buffer_info[i];
1127 
1128 			if (buffer_info->dma)
1129 				dma_unmap_single(&pdev->dev,
1130 						 buffer_info->dma,
1131 						 buffer_info->length,
1132 						 DMA_TO_DEVICE);
1133 			dev_kfree_skb(buffer_info->skb);
1134 		}
1135 	}
1136 
1137 	if (rx_ring->desc && rx_ring->buffer_info) {
1138 		for (i = 0; i < rx_ring->count; i++) {
1139 			buffer_info = &rx_ring->buffer_info[i];
1140 
1141 			if (buffer_info->dma)
1142 				dma_unmap_single(&pdev->dev,
1143 						 buffer_info->dma,
1144 						 2048, DMA_FROM_DEVICE);
1145 			dev_kfree_skb(buffer_info->skb);
1146 		}
1147 	}
1148 
1149 	if (tx_ring->desc) {
1150 		dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1151 				  tx_ring->dma);
1152 		tx_ring->desc = NULL;
1153 	}
1154 	if (rx_ring->desc) {
1155 		dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1156 				  rx_ring->dma);
1157 		rx_ring->desc = NULL;
1158 	}
1159 
1160 	kfree(tx_ring->buffer_info);
1161 	tx_ring->buffer_info = NULL;
1162 	kfree(rx_ring->buffer_info);
1163 	rx_ring->buffer_info = NULL;
1164 }
1165 
1166 static int e1000_setup_desc_rings(struct e1000_adapter *adapter)
1167 {
1168 	struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1169 	struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1170 	struct pci_dev *pdev = adapter->pdev;
1171 	struct e1000_hw *hw = &adapter->hw;
1172 	u32 rctl;
1173 	int i;
1174 	int ret_val;
1175 
1176 	/* Setup Tx descriptor ring and Tx buffers */
1177 
1178 	if (!tx_ring->count)
1179 		tx_ring->count = E1000_DEFAULT_TXD;
1180 
1181 	tx_ring->buffer_info = kzalloc_objs(struct e1000_buffer, tx_ring->count);
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 = kzalloc_objs(struct e1000_buffer, rx_ring->count);
1242 	if (!rx_ring->buffer_info) {
1243 		ret_val = 5;
1244 		goto err_nomem;
1245 	}
1246 
1247 	rx_ring->size = rx_ring->count * sizeof(union e1000_rx_desc_extended);
1248 	rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
1249 					   &rx_ring->dma, GFP_KERNEL);
1250 	if (!rx_ring->desc) {
1251 		ret_val = 6;
1252 		goto err_nomem;
1253 	}
1254 	rx_ring->next_to_use = 0;
1255 	rx_ring->next_to_clean = 0;
1256 
1257 	rctl = er32(RCTL);
1258 	if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
1259 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
1260 	ew32(RDBAL(0), ((u64)rx_ring->dma & 0xFFFFFFFF));
1261 	ew32(RDBAH(0), ((u64)rx_ring->dma >> 32));
1262 	ew32(RDLEN(0), rx_ring->size);
1263 	ew32(RDH(0), 0);
1264 	ew32(RDT(0), 0);
1265 	rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
1266 	    E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE |
1267 	    E1000_RCTL_SBP | E1000_RCTL_SECRC |
1268 	    E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1269 	    (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1270 	ew32(RCTL, rctl);
1271 
1272 	for (i = 0; i < rx_ring->count; i++) {
1273 		union e1000_rx_desc_extended *rx_desc;
1274 		struct sk_buff *skb;
1275 
1276 		skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL);
1277 		if (!skb) {
1278 			ret_val = 7;
1279 			goto err_nomem;
1280 		}
1281 		skb_reserve(skb, NET_IP_ALIGN);
1282 		rx_ring->buffer_info[i].skb = skb;
1283 		rx_ring->buffer_info[i].dma =
1284 		    dma_map_single(&pdev->dev, skb->data, 2048,
1285 				   DMA_FROM_DEVICE);
1286 		if (dma_mapping_error(&pdev->dev,
1287 				      rx_ring->buffer_info[i].dma)) {
1288 			ret_val = 8;
1289 			goto err_nomem;
1290 		}
1291 		rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
1292 		rx_desc->read.buffer_addr =
1293 		    cpu_to_le64(rx_ring->buffer_info[i].dma);
1294 		memset(skb->data, 0x00, skb->len);
1295 	}
1296 
1297 	return 0;
1298 
1299 err_nomem:
1300 	e1000_free_desc_rings(adapter);
1301 	return ret_val;
1302 }
1303 
1304 static void e1000_phy_disable_receiver(struct e1000_adapter *adapter)
1305 {
1306 	/* Write out to PHY registers 29 and 30 to disable the Receiver. */
1307 	e1e_wphy(&adapter->hw, 29, 0x001F);
1308 	e1e_wphy(&adapter->hw, 30, 0x8FFC);
1309 	e1e_wphy(&adapter->hw, 29, 0x001A);
1310 	e1e_wphy(&adapter->hw, 30, 0x8FF0);
1311 }
1312 
1313 static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter)
1314 {
1315 	struct e1000_hw *hw = &adapter->hw;
1316 	u32 ctrl_reg = 0;
1317 	u16 phy_reg = 0;
1318 	s32 ret_val = 0;
1319 
1320 	hw->mac.autoneg = 0;
1321 
1322 	if (hw->phy.type == e1000_phy_ife) {
1323 		/* force 100, set loopback */
1324 		e1e_wphy(hw, MII_BMCR, 0x6100);
1325 
1326 		/* Now set up the MAC to the same speed/duplex as the PHY. */
1327 		ctrl_reg = er32(CTRL);
1328 		ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1329 		ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1330 			     E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1331 			     E1000_CTRL_SPD_100 |/* Force Speed to 100 */
1332 			     E1000_CTRL_FD);	 /* Force Duplex to FULL */
1333 
1334 		ew32(CTRL, ctrl_reg);
1335 		e1e_flush();
1336 		usleep_range(500, 1000);
1337 
1338 		return 0;
1339 	}
1340 
1341 	/* Specific PHY configuration for loopback */
1342 	switch (hw->phy.type) {
1343 	case e1000_phy_m88:
1344 		/* Auto-MDI/MDIX Off */
1345 		e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
1346 		/* reset to update Auto-MDI/MDIX */
1347 		e1e_wphy(hw, MII_BMCR, 0x9140);
1348 		/* autoneg off */
1349 		e1e_wphy(hw, MII_BMCR, 0x8140);
1350 		break;
1351 	case e1000_phy_gg82563:
1352 		e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC);
1353 		break;
1354 	case e1000_phy_bm:
1355 		/* Set Default MAC Interface speed to 1GB */
1356 		e1e_rphy(hw, PHY_REG(2, 21), &phy_reg);
1357 		phy_reg &= ~0x0007;
1358 		phy_reg |= 0x006;
1359 		e1e_wphy(hw, PHY_REG(2, 21), phy_reg);
1360 		/* Assert SW reset for above settings to take effect */
1361 		hw->phy.ops.commit(hw);
1362 		usleep_range(1000, 2000);
1363 		/* Force Full Duplex */
1364 		e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1365 		e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C);
1366 		/* Set Link Up (in force link) */
1367 		e1e_rphy(hw, PHY_REG(776, 16), &phy_reg);
1368 		e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040);
1369 		/* Force Link */
1370 		e1e_rphy(hw, PHY_REG(769, 16), &phy_reg);
1371 		e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040);
1372 		/* Set Early Link Enable */
1373 		e1e_rphy(hw, PHY_REG(769, 20), &phy_reg);
1374 		e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400);
1375 		break;
1376 	case e1000_phy_82577:
1377 	case e1000_phy_82578:
1378 		/* Workaround: K1 must be disabled for stable 1Gbps operation */
1379 		ret_val = hw->phy.ops.acquire(hw);
1380 		if (ret_val) {
1381 			e_err("Cannot setup 1Gbps loopback.\n");
1382 			return ret_val;
1383 		}
1384 		e1000_configure_k1_ich8lan(hw, false);
1385 		hw->phy.ops.release(hw);
1386 		break;
1387 	case e1000_phy_82579:
1388 		/* Disable PHY energy detect power down */
1389 		e1e_rphy(hw, PHY_REG(0, 21), &phy_reg);
1390 		e1e_wphy(hw, PHY_REG(0, 21), phy_reg & ~BIT(3));
1391 		/* Disable full chip energy detect */
1392 		e1e_rphy(hw, PHY_REG(776, 18), &phy_reg);
1393 		e1e_wphy(hw, PHY_REG(776, 18), phy_reg | 1);
1394 		/* Enable loopback on the PHY */
1395 		e1e_wphy(hw, I82577_PHY_LBK_CTRL, 0x8001);
1396 		break;
1397 	default:
1398 		break;
1399 	}
1400 
1401 	/* force 1000, set loopback */
1402 	e1e_wphy(hw, MII_BMCR, 0x4140);
1403 	msleep(250);
1404 
1405 	/* Now set up the MAC to the same speed/duplex as the PHY. */
1406 	ctrl_reg = er32(CTRL);
1407 	ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */
1408 	ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */
1409 		     E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */
1410 		     E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */
1411 		     E1000_CTRL_FD);	 /* Force Duplex to FULL */
1412 
1413 	if (adapter->flags & FLAG_IS_ICH)
1414 		ctrl_reg |= E1000_CTRL_SLU;	/* Set Link Up */
1415 
1416 	if (hw->phy.media_type == e1000_media_type_copper &&
1417 	    hw->phy.type == e1000_phy_m88) {
1418 		ctrl_reg |= E1000_CTRL_ILOS;	/* Invert Loss of Signal */
1419 	} else {
1420 		/* Set the ILOS bit on the fiber Nic if half duplex link is
1421 		 * detected.
1422 		 */
1423 		if ((er32(STATUS) & E1000_STATUS_FD) == 0)
1424 			ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU);
1425 	}
1426 
1427 	ew32(CTRL, ctrl_reg);
1428 
1429 	/* Disable the receiver on the PHY so when a cable is plugged in, the
1430 	 * PHY does not begin to autoneg when a cable is reconnected to the NIC.
1431 	 */
1432 	if (hw->phy.type == e1000_phy_m88)
1433 		e1000_phy_disable_receiver(adapter);
1434 
1435 	usleep_range(500, 1000);
1436 
1437 	return 0;
1438 }
1439 
1440 static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter)
1441 {
1442 	struct e1000_hw *hw = &adapter->hw;
1443 	u32 ctrl = er32(CTRL);
1444 	int link;
1445 
1446 	/* special requirements for 82571/82572 fiber adapters */
1447 
1448 	/* jump through hoops to make sure link is up because serdes
1449 	 * link is hardwired up
1450 	 */
1451 	ctrl |= E1000_CTRL_SLU;
1452 	ew32(CTRL, ctrl);
1453 
1454 	/* disable autoneg */
1455 	ctrl = er32(TXCW);
1456 	ctrl &= ~BIT(31);
1457 	ew32(TXCW, ctrl);
1458 
1459 	link = (er32(STATUS) & E1000_STATUS_LU);
1460 
1461 	if (!link) {
1462 		/* set invert loss of signal */
1463 		ctrl = er32(CTRL);
1464 		ctrl |= E1000_CTRL_ILOS;
1465 		ew32(CTRL, ctrl);
1466 	}
1467 
1468 	/* special write to serdes control register to enable SerDes analog
1469 	 * loopback
1470 	 */
1471 	ew32(SCTL, E1000_SCTL_ENABLE_SERDES_LOOPBACK);
1472 	e1e_flush();
1473 	usleep_range(10000, 11000);
1474 
1475 	return 0;
1476 }
1477 
1478 /* only call this for fiber/serdes connections to es2lan */
1479 static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter)
1480 {
1481 	struct e1000_hw *hw = &adapter->hw;
1482 	u32 ctrlext = er32(CTRL_EXT);
1483 	u32 ctrl = er32(CTRL);
1484 
1485 	/* save CTRL_EXT to restore later, reuse an empty variable (unused
1486 	 * on mac_type 80003es2lan)
1487 	 */
1488 	adapter->tx_fifo_head = ctrlext;
1489 
1490 	/* clear the serdes mode bits, putting the device into mac loopback */
1491 	ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
1492 	ew32(CTRL_EXT, ctrlext);
1493 
1494 	/* force speed to 1000/FD, link up */
1495 	ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1496 	ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX |
1497 		 E1000_CTRL_SPD_1000 | E1000_CTRL_FD);
1498 	ew32(CTRL, ctrl);
1499 
1500 	/* set mac loopback */
1501 	ctrl = er32(RCTL);
1502 	ctrl |= E1000_RCTL_LBM_MAC;
1503 	ew32(RCTL, ctrl);
1504 
1505 	/* set testing mode parameters (no need to reset later) */
1506 #define KMRNCTRLSTA_OPMODE (0x1F << 16)
1507 #define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582
1508 	ew32(KMRNCTRLSTA,
1509 	     (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII));
1510 
1511 	return 0;
1512 }
1513 
1514 static int e1000_setup_loopback_test(struct e1000_adapter *adapter)
1515 {
1516 	struct e1000_hw *hw = &adapter->hw;
1517 	u32 rctl, fext_nvm11, tarc0;
1518 
1519 	if (hw->mac.type >= e1000_pch_spt) {
1520 		fext_nvm11 = er32(FEXTNVM11);
1521 		fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
1522 		ew32(FEXTNVM11, fext_nvm11);
1523 		tarc0 = er32(TARC(0));
1524 		/* clear bits 28 & 29 (control of MULR concurrent requests) */
1525 		tarc0 &= 0xcfffffff;
1526 		/* set bit 29 (value of MULR requests is now 2) */
1527 		tarc0 |= 0x20000000;
1528 		ew32(TARC(0), tarc0);
1529 	}
1530 	if (hw->phy.media_type == e1000_media_type_fiber ||
1531 	    hw->phy.media_type == e1000_media_type_internal_serdes) {
1532 		switch (hw->mac.type) {
1533 		case e1000_80003es2lan:
1534 			return e1000_set_es2lan_mac_loopback(adapter);
1535 		case e1000_82571:
1536 		case e1000_82572:
1537 			return e1000_set_82571_fiber_loopback(adapter);
1538 		default:
1539 			rctl = er32(RCTL);
1540 			rctl |= E1000_RCTL_LBM_TCVR;
1541 			ew32(RCTL, rctl);
1542 			return 0;
1543 		}
1544 	} else if (hw->phy.media_type == e1000_media_type_copper) {
1545 		return e1000_integrated_phy_loopback(adapter);
1546 	}
1547 
1548 	return 7;
1549 }
1550 
1551 static void e1000_loopback_cleanup(struct e1000_adapter *adapter)
1552 {
1553 	struct e1000_hw *hw = &adapter->hw;
1554 	u32 rctl, fext_nvm11, tarc0;
1555 	u16 phy_reg;
1556 
1557 	rctl = er32(RCTL);
1558 	rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
1559 	ew32(RCTL, rctl);
1560 
1561 	switch (hw->mac.type) {
1562 	case e1000_pch_spt:
1563 	case e1000_pch_cnp:
1564 	case e1000_pch_tgp:
1565 	case e1000_pch_adp:
1566 	case e1000_pch_mtp:
1567 	case e1000_pch_lnp:
1568 	case e1000_pch_ptp:
1569 	case e1000_pch_nvp:
1570 		fext_nvm11 = er32(FEXTNVM11);
1571 		fext_nvm11 &= ~E1000_FEXTNVM11_DISABLE_MULR_FIX;
1572 		ew32(FEXTNVM11, fext_nvm11);
1573 		tarc0 = er32(TARC(0));
1574 		/* clear bits 28 & 29 (control of MULR concurrent requests) */
1575 		/* set bit 29 (value of MULR requests is now 0) */
1576 		tarc0 &= 0xcfffffff;
1577 		ew32(TARC(0), tarc0);
1578 		fallthrough;
1579 	case e1000_80003es2lan:
1580 		if (hw->phy.media_type == e1000_media_type_fiber ||
1581 		    hw->phy.media_type == e1000_media_type_internal_serdes) {
1582 			/* restore CTRL_EXT, stealing space from tx_fifo_head */
1583 			ew32(CTRL_EXT, adapter->tx_fifo_head);
1584 			adapter->tx_fifo_head = 0;
1585 		}
1586 		fallthrough;
1587 	case e1000_82571:
1588 	case e1000_82572:
1589 		if (hw->phy.media_type == e1000_media_type_fiber ||
1590 		    hw->phy.media_type == e1000_media_type_internal_serdes) {
1591 			ew32(SCTL, E1000_SCTL_DISABLE_SERDES_LOOPBACK);
1592 			e1e_flush();
1593 			usleep_range(10000, 11000);
1594 			break;
1595 		}
1596 		fallthrough;
1597 	default:
1598 		hw->mac.autoneg = 1;
1599 		if (hw->phy.type == e1000_phy_gg82563)
1600 			e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180);
1601 		e1e_rphy(hw, MII_BMCR, &phy_reg);
1602 		if (phy_reg & BMCR_LOOPBACK) {
1603 			phy_reg &= ~BMCR_LOOPBACK;
1604 			e1e_wphy(hw, MII_BMCR, phy_reg);
1605 			if (hw->phy.ops.commit)
1606 				hw->phy.ops.commit(hw);
1607 		}
1608 		break;
1609 	}
1610 }
1611 
1612 static void e1000_create_lbtest_frame(struct sk_buff *skb,
1613 				      unsigned int frame_size)
1614 {
1615 	memset(skb->data, 0xFF, frame_size);
1616 	frame_size &= ~1;
1617 	memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
1618 	skb->data[frame_size / 2 + 10] = 0xBE;
1619 	skb->data[frame_size / 2 + 12] = 0xAF;
1620 }
1621 
1622 static int e1000_check_lbtest_frame(struct sk_buff *skb,
1623 				    unsigned int frame_size)
1624 {
1625 	frame_size &= ~1;
1626 	if (*(skb->data + 3) == 0xFF)
1627 		if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
1628 		    (*(skb->data + frame_size / 2 + 12) == 0xAF))
1629 			return 0;
1630 	return 13;
1631 }
1632 
1633 static int e1000_run_loopback_test(struct e1000_adapter *adapter)
1634 {
1635 	struct e1000_ring *tx_ring = &adapter->test_tx_ring;
1636 	struct e1000_ring *rx_ring = &adapter->test_rx_ring;
1637 	struct pci_dev *pdev = adapter->pdev;
1638 	struct e1000_hw *hw = &adapter->hw;
1639 	struct e1000_buffer *buffer_info;
1640 	int i, j, k, l;
1641 	int lc;
1642 	int good_cnt;
1643 	int ret_val = 0;
1644 	unsigned long time;
1645 
1646 	ew32(RDT(0), rx_ring->count - 1);
1647 
1648 	/* Calculate the loop count based on the largest descriptor ring
1649 	 * The idea is to wrap the largest ring a number of times using 64
1650 	 * send/receive pairs during each loop
1651 	 */
1652 
1653 	if (rx_ring->count <= tx_ring->count)
1654 		lc = ((tx_ring->count / 64) * 2) + 1;
1655 	else
1656 		lc = ((rx_ring->count / 64) * 2) + 1;
1657 
1658 	k = 0;
1659 	l = 0;
1660 	/* loop count loop */
1661 	for (j = 0; j <= lc; j++) {
1662 		/* send the packets */
1663 		for (i = 0; i < 64; i++) {
1664 			buffer_info = &tx_ring->buffer_info[k];
1665 
1666 			e1000_create_lbtest_frame(buffer_info->skb, 1024);
1667 			dma_sync_single_for_device(&pdev->dev,
1668 						   buffer_info->dma,
1669 						   buffer_info->length,
1670 						   DMA_TO_DEVICE);
1671 			k++;
1672 			if (k == tx_ring->count)
1673 				k = 0;
1674 		}
1675 		ew32(TDT(0), k);
1676 		e1e_flush();
1677 		msleep(200);
1678 		time = jiffies;	/* set the start time for the receive */
1679 		good_cnt = 0;
1680 		/* receive the sent packets */
1681 		do {
1682 			buffer_info = &rx_ring->buffer_info[l];
1683 
1684 			dma_sync_single_for_cpu(&pdev->dev,
1685 						buffer_info->dma, 2048,
1686 						DMA_FROM_DEVICE);
1687 
1688 			ret_val = e1000_check_lbtest_frame(buffer_info->skb,
1689 							   1024);
1690 			if (!ret_val)
1691 				good_cnt++;
1692 			l++;
1693 			if (l == rx_ring->count)
1694 				l = 0;
1695 			/* time + 20 msecs (200 msecs on 2.4) is more than
1696 			 * enough time to complete the receives, if it's
1697 			 * exceeded, break and error off
1698 			 */
1699 		} while ((good_cnt < 64) && !time_after(jiffies, time + 20));
1700 		if (good_cnt != 64) {
1701 			ret_val = 13;	/* ret_val is the same as mis-compare */
1702 			break;
1703 		}
1704 		if (time_after(jiffies, time + 20)) {
1705 			ret_val = 14;	/* error code for time out error */
1706 			break;
1707 		}
1708 	}
1709 	return ret_val;
1710 }
1711 
1712 static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data)
1713 {
1714 	struct e1000_hw *hw = &adapter->hw;
1715 
1716 	/* PHY loopback cannot be performed if SoL/IDER sessions are active */
1717 	if (hw->phy.ops.check_reset_block &&
1718 	    hw->phy.ops.check_reset_block(hw)) {
1719 		e_err("Cannot do PHY loopback test when SoL/IDER is active.\n");
1720 		*data = 0;
1721 		goto out;
1722 	}
1723 
1724 	*data = e1000_setup_desc_rings(adapter);
1725 	if (*data)
1726 		goto out;
1727 
1728 	*data = e1000_setup_loopback_test(adapter);
1729 	if (*data)
1730 		goto err_loopback;
1731 
1732 	*data = e1000_run_loopback_test(adapter);
1733 	e1000_loopback_cleanup(adapter);
1734 
1735 err_loopback:
1736 	e1000_free_desc_rings(adapter);
1737 out:
1738 	return *data;
1739 }
1740 
1741 static int e1000_link_test(struct e1000_adapter *adapter, u64 *data)
1742 {
1743 	struct e1000_hw *hw = &adapter->hw;
1744 
1745 	*data = 0;
1746 	if (hw->phy.media_type == e1000_media_type_internal_serdes) {
1747 		int i = 0;
1748 
1749 		hw->mac.serdes_has_link = false;
1750 
1751 		/* On some blade server designs, link establishment
1752 		 * could take as long as 2-3 minutes
1753 		 */
1754 		do {
1755 			hw->mac.ops.check_for_link(hw);
1756 			if (hw->mac.serdes_has_link)
1757 				return *data;
1758 			msleep(20);
1759 		} while (i++ < 3750);
1760 
1761 		*data = 1;
1762 	} else {
1763 		hw->mac.ops.check_for_link(hw);
1764 		if (hw->mac.autoneg)
1765 			/* On some Phy/switch combinations, link establishment
1766 			 * can take a few seconds more than expected.
1767 			 */
1768 			msleep_interruptible(5000);
1769 
1770 		if (!(er32(STATUS) & E1000_STATUS_LU))
1771 			*data = 1;
1772 	}
1773 	return *data;
1774 }
1775 
1776 static int e1000e_get_sset_count(struct net_device __always_unused *netdev,
1777 				 int sset)
1778 {
1779 	switch (sset) {
1780 	case ETH_SS_TEST:
1781 		return E1000_TEST_LEN;
1782 	case ETH_SS_STATS:
1783 		return E1000_STATS_LEN;
1784 	case ETH_SS_PRIV_FLAGS:
1785 		return E1000E_PRIV_FLAGS_STR_LEN;
1786 	default:
1787 		return -EOPNOTSUPP;
1788 	}
1789 }
1790 
1791 static void e1000_diag_test(struct net_device *netdev,
1792 			    struct ethtool_test *eth_test, u64 *data)
1793 {
1794 	struct e1000_adapter *adapter = netdev_priv(netdev);
1795 	u16 autoneg_advertised;
1796 	u8 forced_speed_duplex;
1797 	u8 autoneg;
1798 	bool if_running = netif_running(netdev);
1799 
1800 	set_bit(__E1000_TESTING, &adapter->state);
1801 
1802 	if (!if_running) {
1803 		/* Get control of and reset hardware */
1804 		if (adapter->flags & FLAG_HAS_AMT)
1805 			e1000e_get_hw_control(adapter);
1806 
1807 		e1000e_power_up_phy(adapter);
1808 
1809 		adapter->hw.phy.autoneg_wait_to_complete = 1;
1810 		e1000e_reset(adapter);
1811 		adapter->hw.phy.autoneg_wait_to_complete = 0;
1812 	}
1813 
1814 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1815 		/* Offline tests */
1816 
1817 		/* save speed, duplex, autoneg settings */
1818 		autoneg_advertised = adapter->hw.phy.autoneg_advertised;
1819 		forced_speed_duplex = adapter->hw.mac.forced_speed_duplex;
1820 		autoneg = adapter->hw.mac.autoneg;
1821 
1822 		e_info("offline testing starting\n");
1823 
1824 		if (if_running)
1825 			/* indicate we're in test mode */
1826 			e1000e_close(netdev);
1827 
1828 		if (e1000_reg_test(adapter, &data[0]))
1829 			eth_test->flags |= ETH_TEST_FL_FAILED;
1830 
1831 		e1000e_reset(adapter);
1832 		if (e1000_eeprom_test(adapter, &data[1]))
1833 			eth_test->flags |= ETH_TEST_FL_FAILED;
1834 
1835 		e1000e_reset(adapter);
1836 		if (e1000_intr_test(adapter, &data[2]))
1837 			eth_test->flags |= ETH_TEST_FL_FAILED;
1838 
1839 		e1000e_reset(adapter);
1840 		if (e1000_loopback_test(adapter, &data[3]))
1841 			eth_test->flags |= ETH_TEST_FL_FAILED;
1842 
1843 		/* force this routine to wait until autoneg complete/timeout */
1844 		adapter->hw.phy.autoneg_wait_to_complete = 1;
1845 		e1000e_reset(adapter);
1846 		adapter->hw.phy.autoneg_wait_to_complete = 0;
1847 
1848 		if (e1000_link_test(adapter, &data[4]))
1849 			eth_test->flags |= ETH_TEST_FL_FAILED;
1850 
1851 		/* restore speed, duplex, autoneg settings */
1852 		adapter->hw.phy.autoneg_advertised = autoneg_advertised;
1853 		adapter->hw.mac.forced_speed_duplex = forced_speed_duplex;
1854 		adapter->hw.mac.autoneg = autoneg;
1855 		e1000e_reset(adapter);
1856 
1857 		clear_bit(__E1000_TESTING, &adapter->state);
1858 		if (if_running)
1859 			e1000e_open(netdev);
1860 	} else {
1861 		/* Online tests */
1862 
1863 		e_info("online testing starting\n");
1864 
1865 		/* register, eeprom, intr and loopback tests not run online */
1866 		data[0] = 0;
1867 		data[1] = 0;
1868 		data[2] = 0;
1869 		data[3] = 0;
1870 
1871 		if (e1000_link_test(adapter, &data[4]))
1872 			eth_test->flags |= ETH_TEST_FL_FAILED;
1873 
1874 		clear_bit(__E1000_TESTING, &adapter->state);
1875 	}
1876 
1877 	if (!if_running) {
1878 		e1000e_reset(adapter);
1879 
1880 		if (adapter->flags & FLAG_HAS_AMT)
1881 			e1000e_release_hw_control(adapter);
1882 	}
1883 
1884 	msleep_interruptible(4 * 1000);
1885 }
1886 
1887 static void e1000_get_wol(struct net_device *netdev,
1888 			  struct ethtool_wolinfo *wol)
1889 {
1890 	struct e1000_adapter *adapter = netdev_priv(netdev);
1891 
1892 	wol->supported = 0;
1893 	wol->wolopts = 0;
1894 
1895 	if (!(adapter->flags & FLAG_HAS_WOL) ||
1896 	    !device_can_wakeup(&adapter->pdev->dev))
1897 		return;
1898 
1899 	wol->supported = WAKE_UCAST | WAKE_MCAST |
1900 	    WAKE_BCAST | WAKE_MAGIC | WAKE_PHY;
1901 
1902 	/* apply any specific unsupported masks here */
1903 	if (adapter->flags & FLAG_NO_WAKE_UCAST) {
1904 		wol->supported &= ~WAKE_UCAST;
1905 
1906 		if (adapter->wol & E1000_WUFC_EX)
1907 			e_err("Interface does not support directed (unicast) frame wake-up packets\n");
1908 	}
1909 
1910 	if (adapter->wol & E1000_WUFC_EX)
1911 		wol->wolopts |= WAKE_UCAST;
1912 	if (adapter->wol & E1000_WUFC_MC)
1913 		wol->wolopts |= WAKE_MCAST;
1914 	if (adapter->wol & E1000_WUFC_BC)
1915 		wol->wolopts |= WAKE_BCAST;
1916 	if (adapter->wol & E1000_WUFC_MAG)
1917 		wol->wolopts |= WAKE_MAGIC;
1918 	if (adapter->wol & E1000_WUFC_LNKC)
1919 		wol->wolopts |= WAKE_PHY;
1920 }
1921 
1922 static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1923 {
1924 	struct e1000_adapter *adapter = netdev_priv(netdev);
1925 
1926 	if (!(adapter->flags & FLAG_HAS_WOL) ||
1927 	    !device_can_wakeup(&adapter->pdev->dev) ||
1928 	    (wol->wolopts & ~(WAKE_UCAST | WAKE_MCAST | WAKE_BCAST |
1929 			      WAKE_MAGIC | WAKE_PHY)))
1930 		return -EOPNOTSUPP;
1931 
1932 	/* these settings will always override what we currently have */
1933 	adapter->wol = 0;
1934 
1935 	if (wol->wolopts & WAKE_UCAST)
1936 		adapter->wol |= E1000_WUFC_EX;
1937 	if (wol->wolopts & WAKE_MCAST)
1938 		adapter->wol |= E1000_WUFC_MC;
1939 	if (wol->wolopts & WAKE_BCAST)
1940 		adapter->wol |= E1000_WUFC_BC;
1941 	if (wol->wolopts & WAKE_MAGIC)
1942 		adapter->wol |= E1000_WUFC_MAG;
1943 	if (wol->wolopts & WAKE_PHY)
1944 		adapter->wol |= E1000_WUFC_LNKC;
1945 
1946 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
1947 
1948 	return 0;
1949 }
1950 
1951 static int e1000_set_phys_id(struct net_device *netdev,
1952 			     enum ethtool_phys_id_state state)
1953 {
1954 	struct e1000_adapter *adapter = netdev_priv(netdev);
1955 	struct e1000_hw *hw = &adapter->hw;
1956 
1957 	switch (state) {
1958 	case ETHTOOL_ID_ACTIVE:
1959 		pm_runtime_get_sync(netdev->dev.parent);
1960 
1961 		if (!hw->mac.ops.blink_led)
1962 			return 2;	/* cycle on/off twice per second */
1963 
1964 		hw->mac.ops.blink_led(hw);
1965 		break;
1966 
1967 	case ETHTOOL_ID_INACTIVE:
1968 		if (hw->phy.type == e1000_phy_ife)
1969 			e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
1970 		hw->mac.ops.led_off(hw);
1971 		hw->mac.ops.cleanup_led(hw);
1972 		pm_runtime_put_sync(netdev->dev.parent);
1973 		break;
1974 
1975 	case ETHTOOL_ID_ON:
1976 		hw->mac.ops.led_on(hw);
1977 		break;
1978 
1979 	case ETHTOOL_ID_OFF:
1980 		hw->mac.ops.led_off(hw);
1981 		break;
1982 	}
1983 
1984 	return 0;
1985 }
1986 
1987 static int e1000_get_coalesce(struct net_device *netdev,
1988 			      struct ethtool_coalesce *ec,
1989 			      struct kernel_ethtool_coalesce *kernel_coal,
1990 			      struct netlink_ext_ack *extack)
1991 {
1992 	struct e1000_adapter *adapter = netdev_priv(netdev);
1993 
1994 	if (adapter->itr_setting <= 4)
1995 		ec->rx_coalesce_usecs = adapter->itr_setting;
1996 	else
1997 		ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting;
1998 
1999 	return 0;
2000 }
2001 
2002 static int e1000_set_coalesce(struct net_device *netdev,
2003 			      struct ethtool_coalesce *ec,
2004 			      struct kernel_ethtool_coalesce *kernel_coal,
2005 			      struct netlink_ext_ack *extack)
2006 {
2007 	struct e1000_adapter *adapter = netdev_priv(netdev);
2008 
2009 	if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) ||
2010 	    ((ec->rx_coalesce_usecs > 4) &&
2011 	     (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) ||
2012 	    (ec->rx_coalesce_usecs == 2))
2013 		return -EINVAL;
2014 
2015 	if (ec->rx_coalesce_usecs == 4) {
2016 		adapter->itr_setting = 4;
2017 		adapter->itr = adapter->itr_setting;
2018 	} else if (ec->rx_coalesce_usecs <= 3) {
2019 		adapter->itr = 20000;
2020 		adapter->itr_setting = ec->rx_coalesce_usecs;
2021 	} else {
2022 		adapter->itr = (1000000 / ec->rx_coalesce_usecs);
2023 		adapter->itr_setting = adapter->itr & ~3;
2024 	}
2025 
2026 	if (adapter->itr_setting != 0)
2027 		e1000e_write_itr(adapter, adapter->itr);
2028 	else
2029 		e1000e_write_itr(adapter, 0);
2030 
2031 	return 0;
2032 }
2033 
2034 static int e1000_nway_reset(struct net_device *netdev)
2035 {
2036 	struct e1000_adapter *adapter = netdev_priv(netdev);
2037 
2038 	if (!netif_running(netdev))
2039 		return -EAGAIN;
2040 
2041 	if (!adapter->hw.mac.autoneg)
2042 		return -EINVAL;
2043 
2044 	e1000e_reinit_locked(adapter);
2045 
2046 	return 0;
2047 }
2048 
2049 static void e1000_get_ethtool_stats(struct net_device *netdev,
2050 				    struct ethtool_stats __always_unused *stats,
2051 				    u64 *data)
2052 {
2053 	struct e1000_adapter *adapter = netdev_priv(netdev);
2054 	struct rtnl_link_stats64 net_stats;
2055 	int i;
2056 	char *p = NULL;
2057 
2058 	dev_get_stats(netdev, &net_stats);
2059 
2060 	for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
2061 		switch (e1000_gstrings_stats[i].type) {
2062 		case NETDEV_STATS:
2063 			p = (char *)&net_stats +
2064 			    e1000_gstrings_stats[i].stat_offset;
2065 			break;
2066 		case E1000_STATS:
2067 			p = (char *)adapter +
2068 			    e1000_gstrings_stats[i].stat_offset;
2069 			break;
2070 		default:
2071 			data[i] = 0;
2072 			continue;
2073 		}
2074 
2075 		data[i] = (e1000_gstrings_stats[i].sizeof_stat ==
2076 			   sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
2077 	}
2078 }
2079 
2080 static void e1000_get_strings(struct net_device __always_unused *netdev,
2081 			      u32 stringset, u8 *data)
2082 {
2083 	u8 *p = data;
2084 	int i;
2085 
2086 	switch (stringset) {
2087 	case ETH_SS_TEST:
2088 		memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test));
2089 		break;
2090 	case ETH_SS_STATS:
2091 		for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) {
2092 			memcpy(p, e1000_gstrings_stats[i].stat_string,
2093 			       ETH_GSTRING_LEN);
2094 			p += ETH_GSTRING_LEN;
2095 		}
2096 		break;
2097 	case ETH_SS_PRIV_FLAGS:
2098 		memcpy(data, e1000e_priv_flags_strings,
2099 		       E1000E_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
2100 		break;
2101 	}
2102 }
2103 
2104 static int e1000_get_rxfh_fields(struct net_device *netdev,
2105 				 struct ethtool_rxfh_fields *info)
2106 {
2107 	struct e1000_adapter *adapter = netdev_priv(netdev);
2108 	struct e1000_hw *hw = &adapter->hw;
2109 	u32 mrqc;
2110 
2111 	info->data = 0;
2112 
2113 	mrqc = er32(MRQC);
2114 
2115 	if (!(mrqc & E1000_MRQC_RSS_FIELD_MASK))
2116 		return 0;
2117 
2118 	switch (info->flow_type) {
2119 	case TCP_V4_FLOW:
2120 		if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_TCP)
2121 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2122 		fallthrough;
2123 	case UDP_V4_FLOW:
2124 	case SCTP_V4_FLOW:
2125 	case AH_ESP_V4_FLOW:
2126 	case IPV4_FLOW:
2127 		if (mrqc & E1000_MRQC_RSS_FIELD_IPV4)
2128 			info->data |= RXH_IP_SRC | RXH_IP_DST;
2129 		break;
2130 	case TCP_V6_FLOW:
2131 		if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP)
2132 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
2133 		fallthrough;
2134 	case UDP_V6_FLOW:
2135 	case SCTP_V6_FLOW:
2136 	case AH_ESP_V6_FLOW:
2137 	case IPV6_FLOW:
2138 		if (mrqc & E1000_MRQC_RSS_FIELD_IPV6)
2139 			info->data |= RXH_IP_SRC | RXH_IP_DST;
2140 		break;
2141 	default:
2142 		break;
2143 	}
2144 	return 0;
2145 }
2146 
2147 static int e1000e_get_eee(struct net_device *netdev, struct ethtool_keee *edata)
2148 {
2149 	struct e1000_adapter *adapter = netdev_priv(netdev);
2150 	struct e1000_hw *hw = &adapter->hw;
2151 	u16 cap_addr, lpa_addr, pcs_stat_addr, phy_data;
2152 	u32 ret_val;
2153 
2154 	if (!(adapter->flags2 & FLAG2_HAS_EEE))
2155 		return -EOPNOTSUPP;
2156 
2157 	switch (hw->phy.type) {
2158 	case e1000_phy_82579:
2159 		cap_addr = I82579_EEE_CAPABILITY;
2160 		lpa_addr = I82579_EEE_LP_ABILITY;
2161 		pcs_stat_addr = I82579_EEE_PCS_STATUS;
2162 		break;
2163 	case e1000_phy_i217:
2164 		cap_addr = I217_EEE_CAPABILITY;
2165 		lpa_addr = I217_EEE_LP_ABILITY;
2166 		pcs_stat_addr = I217_EEE_PCS_STATUS;
2167 		break;
2168 	default:
2169 		return -EOPNOTSUPP;
2170 	}
2171 
2172 	ret_val = hw->phy.ops.acquire(hw);
2173 	if (ret_val)
2174 		return -EBUSY;
2175 
2176 	/* EEE Capability */
2177 	ret_val = e1000_read_emi_reg_locked(hw, cap_addr, &phy_data);
2178 	if (ret_val)
2179 		goto release;
2180 	mii_eee_cap1_mod_linkmode_t(edata->supported, phy_data);
2181 
2182 	/* EEE Advertised */
2183 	mii_eee_cap1_mod_linkmode_t(edata->advertised, adapter->eee_advert);
2184 
2185 	/* EEE Link Partner Advertised */
2186 	ret_val = e1000_read_emi_reg_locked(hw, lpa_addr, &phy_data);
2187 	if (ret_val)
2188 		goto release;
2189 	mii_eee_cap1_mod_linkmode_t(edata->lp_advertised, phy_data);
2190 
2191 	/* EEE PCS Status */
2192 	ret_val = e1000_read_emi_reg_locked(hw, pcs_stat_addr, &phy_data);
2193 	if (ret_val)
2194 		goto release;
2195 	if (hw->phy.type == e1000_phy_82579)
2196 		phy_data <<= 8;
2197 
2198 	/* Result of the EEE auto negotiation - there is no register that
2199 	 * has the status of the EEE negotiation so do a best-guess based
2200 	 * on whether Tx or Rx LPI indications have been received.
2201 	 */
2202 	if (phy_data & (E1000_EEE_TX_LPI_RCVD | E1000_EEE_RX_LPI_RCVD))
2203 		edata->eee_active = true;
2204 
2205 	edata->eee_enabled = !hw->dev_spec.ich8lan.eee_disable;
2206 	edata->tx_lpi_enabled = true;
2207 	edata->tx_lpi_timer = er32(LPIC) >> E1000_LPIC_LPIET_SHIFT;
2208 
2209 release:
2210 	hw->phy.ops.release(hw);
2211 	if (ret_val)
2212 		ret_val = -ENODATA;
2213 
2214 	return ret_val;
2215 }
2216 
2217 static int e1000e_set_eee(struct net_device *netdev, struct ethtool_keee *edata)
2218 {
2219 	struct e1000_adapter *adapter = netdev_priv(netdev);
2220 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = {};
2221 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = {};
2222 	struct e1000_hw *hw = &adapter->hw;
2223 	struct ethtool_keee eee_curr;
2224 	s32 ret_val;
2225 
2226 	ret_val = e1000e_get_eee(netdev, &eee_curr);
2227 	if (ret_val)
2228 		return ret_val;
2229 
2230 	if (eee_curr.tx_lpi_enabled != edata->tx_lpi_enabled) {
2231 		e_err("Setting EEE tx-lpi is not supported\n");
2232 		return -EINVAL;
2233 	}
2234 
2235 	if (eee_curr.tx_lpi_timer != edata->tx_lpi_timer) {
2236 		e_err("Setting EEE Tx LPI timer is not supported\n");
2237 		return -EINVAL;
2238 	}
2239 
2240 	linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2241 			 supported);
2242 	linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2243 			 supported);
2244 
2245 	if (linkmode_andnot(tmp, edata->advertised, supported)) {
2246 		e_err("EEE advertisement supports only 100TX and/or 1000T full-duplex\n");
2247 		return -EINVAL;
2248 	}
2249 
2250 	adapter->eee_advert = linkmode_to_mii_eee_cap1_t(edata->advertised);
2251 
2252 	hw->dev_spec.ich8lan.eee_disable = !edata->eee_enabled;
2253 
2254 	/* reset the link */
2255 	if (netif_running(netdev))
2256 		e1000e_reinit_locked(adapter);
2257 	else
2258 		e1000e_reset(adapter);
2259 
2260 	return 0;
2261 }
2262 
2263 static int e1000e_get_ts_info(struct net_device *netdev,
2264 			      struct kernel_ethtool_ts_info *info)
2265 {
2266 	struct e1000_adapter *adapter = netdev_priv(netdev);
2267 
2268 	ethtool_op_get_ts_info(netdev, info);
2269 
2270 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
2271 		return 0;
2272 
2273 	info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
2274 				  SOF_TIMESTAMPING_RX_HARDWARE |
2275 				  SOF_TIMESTAMPING_RAW_HARDWARE);
2276 
2277 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
2278 
2279 	info->rx_filters = (BIT(HWTSTAMP_FILTER_NONE) |
2280 			    BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
2281 			    BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
2282 			    BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
2283 			    BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
2284 			    BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
2285 			    BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
2286 			    BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
2287 			    BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
2288 			    BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
2289 			    BIT(HWTSTAMP_FILTER_ALL));
2290 
2291 	if (adapter->ptp_clock)
2292 		info->phc_index = ptp_clock_index(adapter->ptp_clock);
2293 
2294 	return 0;
2295 }
2296 
2297 static u32 e1000e_get_priv_flags(struct net_device *netdev)
2298 {
2299 	struct e1000_adapter *adapter = netdev_priv(netdev);
2300 	u32 priv_flags = 0;
2301 
2302 	if (adapter->flags2 & FLAG2_ENABLE_S0IX_FLOWS)
2303 		priv_flags |= E1000E_PRIV_FLAGS_S0IX_ENABLED;
2304 
2305 	if (adapter->flags2 & FLAG2_DISABLE_K1)
2306 		priv_flags |= E1000E_PRIV_FLAGS_DISABLE_K1;
2307 
2308 	return priv_flags;
2309 }
2310 
2311 static int e1000e_set_priv_flags(struct net_device *netdev, u32 priv_flags)
2312 {
2313 	struct e1000_adapter *adapter = netdev_priv(netdev);
2314 	struct e1000_hw *hw = &adapter->hw;
2315 	unsigned int flags2 = adapter->flags2;
2316 	unsigned int changed;
2317 
2318 	flags2 &= ~(FLAG2_ENABLE_S0IX_FLOWS | FLAG2_DISABLE_K1);
2319 
2320 	if (priv_flags & E1000E_PRIV_FLAGS_S0IX_ENABLED) {
2321 		if (hw->mac.type < e1000_pch_cnp) {
2322 			e_err("S0ix is not supported on this device\n");
2323 			return -EINVAL;
2324 		}
2325 
2326 		flags2 |= FLAG2_ENABLE_S0IX_FLOWS;
2327 	}
2328 
2329 	if (priv_flags & E1000E_PRIV_FLAGS_DISABLE_K1) {
2330 		if (hw->mac.type < e1000_ich8lan) {
2331 			e_err("Disabling K1 is not supported on this device\n");
2332 			return -EINVAL;
2333 		}
2334 
2335 		flags2 |= FLAG2_DISABLE_K1;
2336 	}
2337 
2338 	changed = adapter->flags2 ^ flags2;
2339 	if (changed)
2340 		adapter->flags2 = flags2;
2341 
2342 	if (changed & FLAG2_DISABLE_K1) {
2343 		/* reset the hardware to apply the changes */
2344 		while (test_and_set_bit(__E1000_RESETTING,
2345 					&adapter->state))
2346 			usleep_range(1000, 2000);
2347 
2348 		if (netif_running(adapter->netdev)) {
2349 			e1000e_down(adapter, true);
2350 			e1000e_up(adapter);
2351 		} else {
2352 			e1000e_reset(adapter);
2353 		}
2354 
2355 		clear_bit(__E1000_RESETTING, &adapter->state);
2356 	}
2357 
2358 	return 0;
2359 }
2360 
2361 static const struct ethtool_ops e1000_ethtool_ops = {
2362 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
2363 	.get_drvinfo		= e1000_get_drvinfo,
2364 	.get_regs_len		= e1000_get_regs_len,
2365 	.get_regs		= e1000_get_regs,
2366 	.get_wol		= e1000_get_wol,
2367 	.set_wol		= e1000_set_wol,
2368 	.get_msglevel		= e1000_get_msglevel,
2369 	.set_msglevel		= e1000_set_msglevel,
2370 	.nway_reset		= e1000_nway_reset,
2371 	.get_link		= ethtool_op_get_link,
2372 	.get_eeprom_len		= e1000_get_eeprom_len,
2373 	.get_eeprom		= e1000_get_eeprom,
2374 	.set_eeprom		= e1000_set_eeprom,
2375 	.get_ringparam		= e1000_get_ringparam,
2376 	.set_ringparam		= e1000_set_ringparam,
2377 	.get_pauseparam		= e1000_get_pauseparam,
2378 	.set_pauseparam		= e1000_set_pauseparam,
2379 	.self_test		= e1000_diag_test,
2380 	.get_strings		= e1000_get_strings,
2381 	.set_phys_id		= e1000_set_phys_id,
2382 	.get_ethtool_stats	= e1000_get_ethtool_stats,
2383 	.get_sset_count		= e1000e_get_sset_count,
2384 	.get_coalesce		= e1000_get_coalesce,
2385 	.set_coalesce		= e1000_set_coalesce,
2386 	.get_rxfh_fields	= e1000_get_rxfh_fields,
2387 	.get_ts_info		= e1000e_get_ts_info,
2388 	.get_eee		= e1000e_get_eee,
2389 	.set_eee		= e1000e_set_eee,
2390 	.get_link_ksettings	= e1000_get_link_ksettings,
2391 	.set_link_ksettings	= e1000_set_link_ksettings,
2392 	.get_priv_flags		= e1000e_get_priv_flags,
2393 	.set_priv_flags		= e1000e_set_priv_flags,
2394 };
2395 
2396 void e1000e_set_ethtool_ops(struct net_device *netdev)
2397 {
2398 	netdev->ethtool_ops = &e1000_ethtool_ops;
2399 }
2400