xref: /linux/drivers/net/ethernet/intel/igc/igc_ethtool.c (revision c4101e55974cc7d835fbd2d8e01553a3f61e9e75)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c)  2018 Intel Corporation */
3 
4 /* ethtool support for igc */
5 #include <linux/if_vlan.h>
6 #include <linux/pm_runtime.h>
7 #include <linux/mdio.h>
8 
9 #include "igc.h"
10 #include "igc_diag.h"
11 
12 /* forward declaration */
13 struct igc_stats {
14 	char stat_string[ETH_GSTRING_LEN];
15 	int sizeof_stat;
16 	int stat_offset;
17 };
18 
19 #define IGC_STAT(_name, _stat) { \
20 	.stat_string = _name, \
21 	.sizeof_stat = sizeof_field(struct igc_adapter, _stat), \
22 	.stat_offset = offsetof(struct igc_adapter, _stat) \
23 }
24 
25 static const struct igc_stats igc_gstrings_stats[] = {
26 	IGC_STAT("rx_packets", stats.gprc),
27 	IGC_STAT("tx_packets", stats.gptc),
28 	IGC_STAT("rx_bytes", stats.gorc),
29 	IGC_STAT("tx_bytes", stats.gotc),
30 	IGC_STAT("rx_broadcast", stats.bprc),
31 	IGC_STAT("tx_broadcast", stats.bptc),
32 	IGC_STAT("rx_multicast", stats.mprc),
33 	IGC_STAT("tx_multicast", stats.mptc),
34 	IGC_STAT("multicast", stats.mprc),
35 	IGC_STAT("collisions", stats.colc),
36 	IGC_STAT("rx_crc_errors", stats.crcerrs),
37 	IGC_STAT("rx_no_buffer_count", stats.rnbc),
38 	IGC_STAT("rx_missed_errors", stats.mpc),
39 	IGC_STAT("tx_aborted_errors", stats.ecol),
40 	IGC_STAT("tx_carrier_errors", stats.tncrs),
41 	IGC_STAT("tx_window_errors", stats.latecol),
42 	IGC_STAT("tx_abort_late_coll", stats.latecol),
43 	IGC_STAT("tx_deferred_ok", stats.dc),
44 	IGC_STAT("tx_single_coll_ok", stats.scc),
45 	IGC_STAT("tx_multi_coll_ok", stats.mcc),
46 	IGC_STAT("tx_timeout_count", tx_timeout_count),
47 	IGC_STAT("rx_long_length_errors", stats.roc),
48 	IGC_STAT("rx_short_length_errors", stats.ruc),
49 	IGC_STAT("rx_align_errors", stats.algnerrc),
50 	IGC_STAT("tx_tcp_seg_good", stats.tsctc),
51 	IGC_STAT("tx_tcp_seg_failed", stats.tsctfc),
52 	IGC_STAT("rx_flow_control_xon", stats.xonrxc),
53 	IGC_STAT("rx_flow_control_xoff", stats.xoffrxc),
54 	IGC_STAT("tx_flow_control_xon", stats.xontxc),
55 	IGC_STAT("tx_flow_control_xoff", stats.xofftxc),
56 	IGC_STAT("rx_long_byte_count", stats.gorc),
57 	IGC_STAT("tx_dma_out_of_sync", stats.doosync),
58 	IGC_STAT("tx_smbus", stats.mgptc),
59 	IGC_STAT("rx_smbus", stats.mgprc),
60 	IGC_STAT("dropped_smbus", stats.mgpdc),
61 	IGC_STAT("os2bmc_rx_by_bmc", stats.o2bgptc),
62 	IGC_STAT("os2bmc_tx_by_bmc", stats.b2ospc),
63 	IGC_STAT("os2bmc_tx_by_host", stats.o2bspc),
64 	IGC_STAT("os2bmc_rx_by_host", stats.b2ogprc),
65 	IGC_STAT("tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
66 	IGC_STAT("tx_hwtstamp_skipped", tx_hwtstamp_skipped),
67 	IGC_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
68 	IGC_STAT("tx_lpi_counter", stats.tlpic),
69 	IGC_STAT("rx_lpi_counter", stats.rlpic),
70 	IGC_STAT("qbv_config_change_errors", qbv_config_change_errors),
71 };
72 
73 #define IGC_NETDEV_STAT(_net_stat) { \
74 	.stat_string = __stringify(_net_stat), \
75 	.sizeof_stat = sizeof_field(struct rtnl_link_stats64, _net_stat), \
76 	.stat_offset = offsetof(struct rtnl_link_stats64, _net_stat) \
77 }
78 
79 static const struct igc_stats igc_gstrings_net_stats[] = {
80 	IGC_NETDEV_STAT(rx_errors),
81 	IGC_NETDEV_STAT(tx_errors),
82 	IGC_NETDEV_STAT(tx_dropped),
83 	IGC_NETDEV_STAT(rx_length_errors),
84 	IGC_NETDEV_STAT(rx_over_errors),
85 	IGC_NETDEV_STAT(rx_frame_errors),
86 	IGC_NETDEV_STAT(rx_fifo_errors),
87 	IGC_NETDEV_STAT(tx_fifo_errors),
88 	IGC_NETDEV_STAT(tx_heartbeat_errors)
89 };
90 
91 enum igc_diagnostics_results {
92 	TEST_REG = 0,
93 	TEST_EEP,
94 	TEST_IRQ,
95 	TEST_LOOP,
96 	TEST_LINK
97 };
98 
99 static const char igc_gstrings_test[][ETH_GSTRING_LEN] = {
100 	[TEST_REG]  = "Register test  (offline)",
101 	[TEST_EEP]  = "Eeprom test    (offline)",
102 	[TEST_IRQ]  = "Interrupt test (offline)",
103 	[TEST_LOOP] = "Loopback test  (offline)",
104 	[TEST_LINK] = "Link test   (on/offline)"
105 };
106 
107 #define IGC_TEST_LEN (sizeof(igc_gstrings_test) / ETH_GSTRING_LEN)
108 
109 #define IGC_GLOBAL_STATS_LEN	\
110 	(sizeof(igc_gstrings_stats) / sizeof(struct igc_stats))
111 #define IGC_NETDEV_STATS_LEN	\
112 	(sizeof(igc_gstrings_net_stats) / sizeof(struct igc_stats))
113 #define IGC_RX_QUEUE_STATS_LEN \
114 	(sizeof(struct igc_rx_queue_stats) / sizeof(u64))
115 #define IGC_TX_QUEUE_STATS_LEN 3 /* packets, bytes, restart_queue */
116 #define IGC_QUEUE_STATS_LEN \
117 	((((struct igc_adapter *)netdev_priv(netdev))->num_rx_queues * \
118 	  IGC_RX_QUEUE_STATS_LEN) + \
119 	 (((struct igc_adapter *)netdev_priv(netdev))->num_tx_queues * \
120 	  IGC_TX_QUEUE_STATS_LEN))
121 #define IGC_STATS_LEN \
122 	(IGC_GLOBAL_STATS_LEN + IGC_NETDEV_STATS_LEN + IGC_QUEUE_STATS_LEN)
123 
124 static const char igc_priv_flags_strings[][ETH_GSTRING_LEN] = {
125 #define IGC_PRIV_FLAGS_LEGACY_RX	BIT(0)
126 	"legacy-rx",
127 };
128 
129 #define IGC_PRIV_FLAGS_STR_LEN ARRAY_SIZE(igc_priv_flags_strings)
130 
131 static void igc_ethtool_get_drvinfo(struct net_device *netdev,
132 				    struct ethtool_drvinfo *drvinfo)
133 {
134 	struct igc_adapter *adapter = netdev_priv(netdev);
135 	struct igc_hw *hw = &adapter->hw;
136 	u16 nvm_version = 0;
137 	u16 gphy_version;
138 
139 	strscpy(drvinfo->driver, igc_driver_name, sizeof(drvinfo->driver));
140 
141 	/* NVM image version is reported as firmware version for i225 device */
142 	hw->nvm.ops.read(hw, IGC_NVM_DEV_STARTER, 1, &nvm_version);
143 
144 	/* gPHY firmware version is reported as PHY FW version */
145 	gphy_version = igc_read_phy_fw_version(hw);
146 
147 	scnprintf(adapter->fw_version,
148 		  sizeof(adapter->fw_version),
149 		  "%x:%x",
150 		  nvm_version,
151 		  gphy_version);
152 
153 	strscpy(drvinfo->fw_version, adapter->fw_version,
154 		sizeof(drvinfo->fw_version));
155 
156 	strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
157 		sizeof(drvinfo->bus_info));
158 
159 	drvinfo->n_priv_flags = IGC_PRIV_FLAGS_STR_LEN;
160 }
161 
162 static int igc_ethtool_get_regs_len(struct net_device *netdev)
163 {
164 	return IGC_REGS_LEN * sizeof(u32);
165 }
166 
167 static void igc_ethtool_get_regs(struct net_device *netdev,
168 				 struct ethtool_regs *regs, void *p)
169 {
170 	struct igc_adapter *adapter = netdev_priv(netdev);
171 	struct igc_hw *hw = &adapter->hw;
172 	u32 *regs_buff = p;
173 	u8 i;
174 
175 	memset(p, 0, IGC_REGS_LEN * sizeof(u32));
176 
177 	regs->version = (2u << 24) | (hw->revision_id << 16) | hw->device_id;
178 
179 	/* General Registers */
180 	regs_buff[0] = rd32(IGC_CTRL);
181 	regs_buff[1] = rd32(IGC_STATUS);
182 	regs_buff[2] = rd32(IGC_CTRL_EXT);
183 	regs_buff[3] = rd32(IGC_MDIC);
184 	regs_buff[4] = rd32(IGC_CONNSW);
185 
186 	/* NVM Register */
187 	regs_buff[5] = rd32(IGC_EECD);
188 
189 	/* Interrupt */
190 	/* Reading EICS for EICR because they read the
191 	 * same but EICS does not clear on read
192 	 */
193 	regs_buff[6] = rd32(IGC_EICS);
194 	regs_buff[7] = rd32(IGC_EICS);
195 	regs_buff[8] = rd32(IGC_EIMS);
196 	regs_buff[9] = rd32(IGC_EIMC);
197 	regs_buff[10] = rd32(IGC_EIAC);
198 	regs_buff[11] = rd32(IGC_EIAM);
199 	/* Reading ICS for ICR because they read the
200 	 * same but ICS does not clear on read
201 	 */
202 	regs_buff[12] = rd32(IGC_ICS);
203 	regs_buff[13] = rd32(IGC_ICS);
204 	regs_buff[14] = rd32(IGC_IMS);
205 	regs_buff[15] = rd32(IGC_IMC);
206 	regs_buff[16] = rd32(IGC_IAC);
207 	regs_buff[17] = rd32(IGC_IAM);
208 
209 	/* Flow Control */
210 	regs_buff[18] = rd32(IGC_FCAL);
211 	regs_buff[19] = rd32(IGC_FCAH);
212 	regs_buff[20] = rd32(IGC_FCTTV);
213 	regs_buff[21] = rd32(IGC_FCRTL);
214 	regs_buff[22] = rd32(IGC_FCRTH);
215 	regs_buff[23] = rd32(IGC_FCRTV);
216 
217 	/* Receive */
218 	regs_buff[24] = rd32(IGC_RCTL);
219 	regs_buff[25] = rd32(IGC_RXCSUM);
220 	regs_buff[26] = rd32(IGC_RLPML);
221 	regs_buff[27] = rd32(IGC_RFCTL);
222 
223 	/* Transmit */
224 	regs_buff[28] = rd32(IGC_TCTL);
225 	regs_buff[29] = rd32(IGC_TIPG);
226 
227 	/* Wake Up */
228 
229 	/* MAC */
230 
231 	/* Statistics */
232 	regs_buff[30] = adapter->stats.crcerrs;
233 	regs_buff[31] = adapter->stats.algnerrc;
234 	regs_buff[32] = adapter->stats.symerrs;
235 	regs_buff[33] = adapter->stats.rxerrc;
236 	regs_buff[34] = adapter->stats.mpc;
237 	regs_buff[35] = adapter->stats.scc;
238 	regs_buff[36] = adapter->stats.ecol;
239 	regs_buff[37] = adapter->stats.mcc;
240 	regs_buff[38] = adapter->stats.latecol;
241 	regs_buff[39] = adapter->stats.colc;
242 	regs_buff[40] = adapter->stats.dc;
243 	regs_buff[41] = adapter->stats.tncrs;
244 	regs_buff[42] = adapter->stats.sec;
245 	regs_buff[43] = adapter->stats.htdpmc;
246 	regs_buff[44] = adapter->stats.rlec;
247 	regs_buff[45] = adapter->stats.xonrxc;
248 	regs_buff[46] = adapter->stats.xontxc;
249 	regs_buff[47] = adapter->stats.xoffrxc;
250 	regs_buff[48] = adapter->stats.xofftxc;
251 	regs_buff[49] = adapter->stats.fcruc;
252 	regs_buff[50] = adapter->stats.prc64;
253 	regs_buff[51] = adapter->stats.prc127;
254 	regs_buff[52] = adapter->stats.prc255;
255 	regs_buff[53] = adapter->stats.prc511;
256 	regs_buff[54] = adapter->stats.prc1023;
257 	regs_buff[55] = adapter->stats.prc1522;
258 	regs_buff[56] = adapter->stats.gprc;
259 	regs_buff[57] = adapter->stats.bprc;
260 	regs_buff[58] = adapter->stats.mprc;
261 	regs_buff[59] = adapter->stats.gptc;
262 	regs_buff[60] = adapter->stats.gorc;
263 	regs_buff[61] = adapter->stats.gotc;
264 	regs_buff[62] = adapter->stats.rnbc;
265 	regs_buff[63] = adapter->stats.ruc;
266 	regs_buff[64] = adapter->stats.rfc;
267 	regs_buff[65] = adapter->stats.roc;
268 	regs_buff[66] = adapter->stats.rjc;
269 	regs_buff[67] = adapter->stats.mgprc;
270 	regs_buff[68] = adapter->stats.mgpdc;
271 	regs_buff[69] = adapter->stats.mgptc;
272 	regs_buff[70] = adapter->stats.tor;
273 	regs_buff[71] = adapter->stats.tot;
274 	regs_buff[72] = adapter->stats.tpr;
275 	regs_buff[73] = adapter->stats.tpt;
276 	regs_buff[74] = adapter->stats.ptc64;
277 	regs_buff[75] = adapter->stats.ptc127;
278 	regs_buff[76] = adapter->stats.ptc255;
279 	regs_buff[77] = adapter->stats.ptc511;
280 	regs_buff[78] = adapter->stats.ptc1023;
281 	regs_buff[79] = adapter->stats.ptc1522;
282 	regs_buff[80] = adapter->stats.mptc;
283 	regs_buff[81] = adapter->stats.bptc;
284 	regs_buff[82] = adapter->stats.tsctc;
285 	regs_buff[83] = adapter->stats.iac;
286 	regs_buff[84] = adapter->stats.rpthc;
287 	regs_buff[85] = adapter->stats.hgptc;
288 	regs_buff[86] = adapter->stats.hgorc;
289 	regs_buff[87] = adapter->stats.hgotc;
290 	regs_buff[88] = adapter->stats.lenerrs;
291 	regs_buff[89] = adapter->stats.scvpc;
292 	regs_buff[90] = adapter->stats.hrmpc;
293 
294 	for (i = 0; i < 4; i++)
295 		regs_buff[91 + i] = rd32(IGC_SRRCTL(i));
296 	for (i = 0; i < 4; i++)
297 		regs_buff[95 + i] = rd32(IGC_PSRTYPE(i));
298 	for (i = 0; i < 4; i++)
299 		regs_buff[99 + i] = rd32(IGC_RDBAL(i));
300 	for (i = 0; i < 4; i++)
301 		regs_buff[103 + i] = rd32(IGC_RDBAH(i));
302 	for (i = 0; i < 4; i++)
303 		regs_buff[107 + i] = rd32(IGC_RDLEN(i));
304 	for (i = 0; i < 4; i++)
305 		regs_buff[111 + i] = rd32(IGC_RDH(i));
306 	for (i = 0; i < 4; i++)
307 		regs_buff[115 + i] = rd32(IGC_RDT(i));
308 	for (i = 0; i < 4; i++)
309 		regs_buff[119 + i] = rd32(IGC_RXDCTL(i));
310 
311 	for (i = 0; i < 10; i++)
312 		regs_buff[123 + i] = rd32(IGC_EITR(i));
313 	for (i = 0; i < 16; i++)
314 		regs_buff[139 + i] = rd32(IGC_RAL(i));
315 	for (i = 0; i < 16; i++)
316 		regs_buff[145 + i] = rd32(IGC_RAH(i));
317 
318 	for (i = 0; i < 4; i++)
319 		regs_buff[149 + i] = rd32(IGC_TDBAL(i));
320 	for (i = 0; i < 4; i++)
321 		regs_buff[152 + i] = rd32(IGC_TDBAH(i));
322 	for (i = 0; i < 4; i++)
323 		regs_buff[156 + i] = rd32(IGC_TDLEN(i));
324 	for (i = 0; i < 4; i++)
325 		regs_buff[160 + i] = rd32(IGC_TDH(i));
326 	for (i = 0; i < 4; i++)
327 		regs_buff[164 + i] = rd32(IGC_TDT(i));
328 	for (i = 0; i < 4; i++)
329 		regs_buff[168 + i] = rd32(IGC_TXDCTL(i));
330 
331 	/* XXX: Due to a bug few lines above, RAL and RAH registers are
332 	 * overwritten. To preserve the ABI, we write these registers again in
333 	 * regs_buff.
334 	 */
335 	for (i = 0; i < 16; i++)
336 		regs_buff[172 + i] = rd32(IGC_RAL(i));
337 	for (i = 0; i < 16; i++)
338 		regs_buff[188 + i] = rd32(IGC_RAH(i));
339 
340 	regs_buff[204] = rd32(IGC_VLANPQF);
341 
342 	for (i = 0; i < 8; i++)
343 		regs_buff[205 + i] = rd32(IGC_ETQF(i));
344 
345 	regs_buff[213] = adapter->stats.tlpic;
346 	regs_buff[214] = adapter->stats.rlpic;
347 }
348 
349 static void igc_ethtool_get_wol(struct net_device *netdev,
350 				struct ethtool_wolinfo *wol)
351 {
352 	struct igc_adapter *adapter = netdev_priv(netdev);
353 
354 	wol->wolopts = 0;
355 
356 	if (!(adapter->flags & IGC_FLAG_WOL_SUPPORTED))
357 		return;
358 
359 	wol->supported = WAKE_UCAST | WAKE_MCAST |
360 			 WAKE_BCAST | WAKE_MAGIC |
361 			 WAKE_PHY;
362 
363 	/* apply any specific unsupported masks here */
364 	switch (adapter->hw.device_id) {
365 	default:
366 		break;
367 	}
368 
369 	if (adapter->wol & IGC_WUFC_EX)
370 		wol->wolopts |= WAKE_UCAST;
371 	if (adapter->wol & IGC_WUFC_MC)
372 		wol->wolopts |= WAKE_MCAST;
373 	if (adapter->wol & IGC_WUFC_BC)
374 		wol->wolopts |= WAKE_BCAST;
375 	if (adapter->wol & IGC_WUFC_MAG)
376 		wol->wolopts |= WAKE_MAGIC;
377 	if (adapter->wol & IGC_WUFC_LNKC)
378 		wol->wolopts |= WAKE_PHY;
379 }
380 
381 static int igc_ethtool_set_wol(struct net_device *netdev,
382 			       struct ethtool_wolinfo *wol)
383 {
384 	struct igc_adapter *adapter = netdev_priv(netdev);
385 
386 	if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE | WAKE_FILTER))
387 		return -EOPNOTSUPP;
388 
389 	if (!(adapter->flags & IGC_FLAG_WOL_SUPPORTED))
390 		return wol->wolopts ? -EOPNOTSUPP : 0;
391 
392 	/* these settings will always override what we currently have */
393 	adapter->wol = 0;
394 
395 	if (wol->wolopts & WAKE_UCAST)
396 		adapter->wol |= IGC_WUFC_EX;
397 	if (wol->wolopts & WAKE_MCAST)
398 		adapter->wol |= IGC_WUFC_MC;
399 	if (wol->wolopts & WAKE_BCAST)
400 		adapter->wol |= IGC_WUFC_BC;
401 	if (wol->wolopts & WAKE_MAGIC)
402 		adapter->wol |= IGC_WUFC_MAG;
403 	if (wol->wolopts & WAKE_PHY)
404 		adapter->wol |= IGC_WUFC_LNKC;
405 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
406 
407 	return 0;
408 }
409 
410 static u32 igc_ethtool_get_msglevel(struct net_device *netdev)
411 {
412 	struct igc_adapter *adapter = netdev_priv(netdev);
413 
414 	return adapter->msg_enable;
415 }
416 
417 static void igc_ethtool_set_msglevel(struct net_device *netdev, u32 data)
418 {
419 	struct igc_adapter *adapter = netdev_priv(netdev);
420 
421 	adapter->msg_enable = data;
422 }
423 
424 static int igc_ethtool_nway_reset(struct net_device *netdev)
425 {
426 	struct igc_adapter *adapter = netdev_priv(netdev);
427 
428 	if (netif_running(netdev))
429 		igc_reinit_locked(adapter);
430 	return 0;
431 }
432 
433 static u32 igc_ethtool_get_link(struct net_device *netdev)
434 {
435 	struct igc_adapter *adapter = netdev_priv(netdev);
436 	struct igc_mac_info *mac = &adapter->hw.mac;
437 
438 	/* If the link is not reported up to netdev, interrupts are disabled,
439 	 * and so the physical link state may have changed since we last
440 	 * looked. Set get_link_status to make sure that the true link
441 	 * state is interrogated, rather than pulling a cached and possibly
442 	 * stale link state from the driver.
443 	 */
444 	if (!netif_carrier_ok(netdev))
445 		mac->get_link_status = 1;
446 
447 	return igc_has_link(adapter);
448 }
449 
450 static int igc_ethtool_get_eeprom_len(struct net_device *netdev)
451 {
452 	struct igc_adapter *adapter = netdev_priv(netdev);
453 
454 	return adapter->hw.nvm.word_size * 2;
455 }
456 
457 static int igc_ethtool_get_eeprom(struct net_device *netdev,
458 				  struct ethtool_eeprom *eeprom, u8 *bytes)
459 {
460 	struct igc_adapter *adapter = netdev_priv(netdev);
461 	struct igc_hw *hw = &adapter->hw;
462 	int first_word, last_word;
463 	u16 *eeprom_buff;
464 	int ret_val = 0;
465 	u16 i;
466 
467 	if (eeprom->len == 0)
468 		return -EINVAL;
469 
470 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
471 
472 	first_word = eeprom->offset >> 1;
473 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
474 
475 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
476 				    GFP_KERNEL);
477 	if (!eeprom_buff)
478 		return -ENOMEM;
479 
480 	if (hw->nvm.type == igc_nvm_eeprom_spi) {
481 		ret_val = hw->nvm.ops.read(hw, first_word,
482 					   last_word - first_word + 1,
483 					   eeprom_buff);
484 	} else {
485 		for (i = 0; i < last_word - first_word + 1; i++) {
486 			ret_val = hw->nvm.ops.read(hw, first_word + i, 1,
487 						   &eeprom_buff[i]);
488 			if (ret_val)
489 				break;
490 		}
491 	}
492 
493 	/* Device's eeprom is always little-endian, word addressable */
494 	for (i = 0; i < last_word - first_word + 1; i++)
495 		le16_to_cpus(&eeprom_buff[i]);
496 
497 	memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1),
498 	       eeprom->len);
499 	kfree(eeprom_buff);
500 
501 	return ret_val;
502 }
503 
504 static int igc_ethtool_set_eeprom(struct net_device *netdev,
505 				  struct ethtool_eeprom *eeprom, u8 *bytes)
506 {
507 	struct igc_adapter *adapter = netdev_priv(netdev);
508 	struct igc_hw *hw = &adapter->hw;
509 	int max_len, first_word, last_word, ret_val = 0;
510 	u16 *eeprom_buff;
511 	void *ptr;
512 	u16 i;
513 
514 	if (eeprom->len == 0)
515 		return -EOPNOTSUPP;
516 
517 	if (hw->mac.type >= igc_i225 &&
518 	    !igc_get_flash_presence_i225(hw)) {
519 		return -EOPNOTSUPP;
520 	}
521 
522 	if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
523 		return -EFAULT;
524 
525 	max_len = hw->nvm.word_size * 2;
526 
527 	first_word = eeprom->offset >> 1;
528 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
529 	eeprom_buff = kmalloc(max_len, GFP_KERNEL);
530 	if (!eeprom_buff)
531 		return -ENOMEM;
532 
533 	ptr = (void *)eeprom_buff;
534 
535 	if (eeprom->offset & 1) {
536 		/* need read/modify/write of first changed EEPROM word
537 		 * only the second byte of the word is being modified
538 		 */
539 		ret_val = hw->nvm.ops.read(hw, first_word, 1,
540 					    &eeprom_buff[0]);
541 		ptr++;
542 	}
543 	if (((eeprom->offset + eeprom->len) & 1) && ret_val == 0) {
544 		/* need read/modify/write of last changed EEPROM word
545 		 * only the first byte of the word is being modified
546 		 */
547 		ret_val = hw->nvm.ops.read(hw, last_word, 1,
548 				   &eeprom_buff[last_word - first_word]);
549 	}
550 
551 	/* Device's eeprom is always little-endian, word addressable */
552 	for (i = 0; i < last_word - first_word + 1; i++)
553 		le16_to_cpus(&eeprom_buff[i]);
554 
555 	memcpy(ptr, bytes, eeprom->len);
556 
557 	for (i = 0; i < last_word - first_word + 1; i++)
558 		cpu_to_le16s(&eeprom_buff[i]);
559 
560 	ret_val = hw->nvm.ops.write(hw, first_word,
561 				    last_word - first_word + 1, eeprom_buff);
562 
563 	/* Update the checksum if nvm write succeeded */
564 	if (ret_val == 0)
565 		hw->nvm.ops.update(hw);
566 
567 	kfree(eeprom_buff);
568 	return ret_val;
569 }
570 
571 static void
572 igc_ethtool_get_ringparam(struct net_device *netdev,
573 			  struct ethtool_ringparam *ring,
574 			  struct kernel_ethtool_ringparam *kernel_ering,
575 			  struct netlink_ext_ack *extack)
576 {
577 	struct igc_adapter *adapter = netdev_priv(netdev);
578 
579 	ring->rx_max_pending = IGC_MAX_RXD;
580 	ring->tx_max_pending = IGC_MAX_TXD;
581 	ring->rx_pending = adapter->rx_ring_count;
582 	ring->tx_pending = adapter->tx_ring_count;
583 }
584 
585 static int
586 igc_ethtool_set_ringparam(struct net_device *netdev,
587 			  struct ethtool_ringparam *ring,
588 			  struct kernel_ethtool_ringparam *kernel_ering,
589 			  struct netlink_ext_ack *extack)
590 {
591 	struct igc_adapter *adapter = netdev_priv(netdev);
592 	struct igc_ring *temp_ring;
593 	u16 new_rx_count, new_tx_count;
594 	int i, err = 0;
595 
596 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
597 		return -EINVAL;
598 
599 	new_rx_count = min_t(u32, ring->rx_pending, IGC_MAX_RXD);
600 	new_rx_count = max_t(u16, new_rx_count, IGC_MIN_RXD);
601 	new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
602 
603 	new_tx_count = min_t(u32, ring->tx_pending, IGC_MAX_TXD);
604 	new_tx_count = max_t(u16, new_tx_count, IGC_MIN_TXD);
605 	new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
606 
607 	if (new_tx_count == adapter->tx_ring_count &&
608 	    new_rx_count == adapter->rx_ring_count) {
609 		/* nothing to do */
610 		return 0;
611 	}
612 
613 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
614 		usleep_range(1000, 2000);
615 
616 	if (!netif_running(adapter->netdev)) {
617 		for (i = 0; i < adapter->num_tx_queues; i++)
618 			adapter->tx_ring[i]->count = new_tx_count;
619 		for (i = 0; i < adapter->num_rx_queues; i++)
620 			adapter->rx_ring[i]->count = new_rx_count;
621 		adapter->tx_ring_count = new_tx_count;
622 		adapter->rx_ring_count = new_rx_count;
623 		goto clear_reset;
624 	}
625 
626 	if (adapter->num_tx_queues > adapter->num_rx_queues)
627 		temp_ring = vmalloc(array_size(sizeof(struct igc_ring),
628 					       adapter->num_tx_queues));
629 	else
630 		temp_ring = vmalloc(array_size(sizeof(struct igc_ring),
631 					       adapter->num_rx_queues));
632 
633 	if (!temp_ring) {
634 		err = -ENOMEM;
635 		goto clear_reset;
636 	}
637 
638 	igc_down(adapter);
639 
640 	/* We can't just free everything and then setup again,
641 	 * because the ISRs in MSI-X mode get passed pointers
642 	 * to the Tx and Rx ring structs.
643 	 */
644 	if (new_tx_count != adapter->tx_ring_count) {
645 		for (i = 0; i < adapter->num_tx_queues; i++) {
646 			memcpy(&temp_ring[i], adapter->tx_ring[i],
647 			       sizeof(struct igc_ring));
648 
649 			temp_ring[i].count = new_tx_count;
650 			err = igc_setup_tx_resources(&temp_ring[i]);
651 			if (err) {
652 				while (i) {
653 					i--;
654 					igc_free_tx_resources(&temp_ring[i]);
655 				}
656 				goto err_setup;
657 			}
658 		}
659 
660 		for (i = 0; i < adapter->num_tx_queues; i++) {
661 			igc_free_tx_resources(adapter->tx_ring[i]);
662 
663 			memcpy(adapter->tx_ring[i], &temp_ring[i],
664 			       sizeof(struct igc_ring));
665 		}
666 
667 		adapter->tx_ring_count = new_tx_count;
668 	}
669 
670 	if (new_rx_count != adapter->rx_ring_count) {
671 		for (i = 0; i < adapter->num_rx_queues; i++) {
672 			memcpy(&temp_ring[i], adapter->rx_ring[i],
673 			       sizeof(struct igc_ring));
674 
675 			temp_ring[i].count = new_rx_count;
676 			err = igc_setup_rx_resources(&temp_ring[i]);
677 			if (err) {
678 				while (i) {
679 					i--;
680 					igc_free_rx_resources(&temp_ring[i]);
681 				}
682 				goto err_setup;
683 			}
684 		}
685 
686 		for (i = 0; i < adapter->num_rx_queues; i++) {
687 			igc_free_rx_resources(adapter->rx_ring[i]);
688 
689 			memcpy(adapter->rx_ring[i], &temp_ring[i],
690 			       sizeof(struct igc_ring));
691 		}
692 
693 		adapter->rx_ring_count = new_rx_count;
694 	}
695 err_setup:
696 	igc_up(adapter);
697 	vfree(temp_ring);
698 clear_reset:
699 	clear_bit(__IGC_RESETTING, &adapter->state);
700 	return err;
701 }
702 
703 static void igc_ethtool_get_pauseparam(struct net_device *netdev,
704 				       struct ethtool_pauseparam *pause)
705 {
706 	struct igc_adapter *adapter = netdev_priv(netdev);
707 	struct igc_hw *hw = &adapter->hw;
708 
709 	pause->autoneg =
710 		(adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
711 
712 	if (hw->fc.current_mode == igc_fc_rx_pause) {
713 		pause->rx_pause = 1;
714 	} else if (hw->fc.current_mode == igc_fc_tx_pause) {
715 		pause->tx_pause = 1;
716 	} else if (hw->fc.current_mode == igc_fc_full) {
717 		pause->rx_pause = 1;
718 		pause->tx_pause = 1;
719 	}
720 }
721 
722 static int igc_ethtool_set_pauseparam(struct net_device *netdev,
723 				      struct ethtool_pauseparam *pause)
724 {
725 	struct igc_adapter *adapter = netdev_priv(netdev);
726 	struct igc_hw *hw = &adapter->hw;
727 	int retval = 0;
728 
729 	adapter->fc_autoneg = pause->autoneg;
730 
731 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
732 		usleep_range(1000, 2000);
733 
734 	if (adapter->fc_autoneg == AUTONEG_ENABLE) {
735 		hw->fc.requested_mode = igc_fc_default;
736 		if (netif_running(adapter->netdev)) {
737 			igc_down(adapter);
738 			igc_up(adapter);
739 		} else {
740 			igc_reset(adapter);
741 		}
742 	} else {
743 		if (pause->rx_pause && pause->tx_pause)
744 			hw->fc.requested_mode = igc_fc_full;
745 		else if (pause->rx_pause && !pause->tx_pause)
746 			hw->fc.requested_mode = igc_fc_rx_pause;
747 		else if (!pause->rx_pause && pause->tx_pause)
748 			hw->fc.requested_mode = igc_fc_tx_pause;
749 		else if (!pause->rx_pause && !pause->tx_pause)
750 			hw->fc.requested_mode = igc_fc_none;
751 
752 		hw->fc.current_mode = hw->fc.requested_mode;
753 
754 		retval = ((hw->phy.media_type == igc_media_type_copper) ?
755 			  igc_force_mac_fc(hw) : igc_setup_link(hw));
756 	}
757 
758 	clear_bit(__IGC_RESETTING, &adapter->state);
759 	return retval;
760 }
761 
762 static void igc_ethtool_get_strings(struct net_device *netdev, u32 stringset,
763 				    u8 *data)
764 {
765 	struct igc_adapter *adapter = netdev_priv(netdev);
766 	u8 *p = data;
767 	int i;
768 
769 	switch (stringset) {
770 	case ETH_SS_TEST:
771 		memcpy(data, *igc_gstrings_test,
772 		       IGC_TEST_LEN * ETH_GSTRING_LEN);
773 		break;
774 	case ETH_SS_STATS:
775 		for (i = 0; i < IGC_GLOBAL_STATS_LEN; i++)
776 			ethtool_puts(&p, igc_gstrings_stats[i].stat_string);
777 		for (i = 0; i < IGC_NETDEV_STATS_LEN; i++)
778 			ethtool_puts(&p, igc_gstrings_net_stats[i].stat_string);
779 		for (i = 0; i < adapter->num_tx_queues; i++) {
780 			ethtool_sprintf(&p, "tx_queue_%u_packets", i);
781 			ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
782 			ethtool_sprintf(&p, "tx_queue_%u_restart", i);
783 		}
784 		for (i = 0; i < adapter->num_rx_queues; i++) {
785 			ethtool_sprintf(&p, "rx_queue_%u_packets", i);
786 			ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
787 			ethtool_sprintf(&p, "rx_queue_%u_drops", i);
788 			ethtool_sprintf(&p, "rx_queue_%u_csum_err", i);
789 			ethtool_sprintf(&p, "rx_queue_%u_alloc_failed", i);
790 		}
791 		/* BUG_ON(p - data != IGC_STATS_LEN * ETH_GSTRING_LEN); */
792 		break;
793 	case ETH_SS_PRIV_FLAGS:
794 		memcpy(data, igc_priv_flags_strings,
795 		       IGC_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
796 		break;
797 	}
798 }
799 
800 static int igc_ethtool_get_sset_count(struct net_device *netdev, int sset)
801 {
802 	switch (sset) {
803 	case ETH_SS_STATS:
804 		return IGC_STATS_LEN;
805 	case ETH_SS_TEST:
806 		return IGC_TEST_LEN;
807 	case ETH_SS_PRIV_FLAGS:
808 		return IGC_PRIV_FLAGS_STR_LEN;
809 	default:
810 		return -ENOTSUPP;
811 	}
812 }
813 
814 static void igc_ethtool_get_stats(struct net_device *netdev,
815 				  struct ethtool_stats *stats, u64 *data)
816 {
817 	struct igc_adapter *adapter = netdev_priv(netdev);
818 	struct rtnl_link_stats64 *net_stats = &adapter->stats64;
819 	unsigned int start;
820 	struct igc_ring *ring;
821 	int i, j;
822 	char *p;
823 
824 	spin_lock(&adapter->stats64_lock);
825 	igc_update_stats(adapter);
826 
827 	for (i = 0; i < IGC_GLOBAL_STATS_LEN; i++) {
828 		p = (char *)adapter + igc_gstrings_stats[i].stat_offset;
829 		data[i] = (igc_gstrings_stats[i].sizeof_stat ==
830 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
831 	}
832 	for (j = 0; j < IGC_NETDEV_STATS_LEN; j++, i++) {
833 		p = (char *)net_stats + igc_gstrings_net_stats[j].stat_offset;
834 		data[i] = (igc_gstrings_net_stats[j].sizeof_stat ==
835 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
836 	}
837 	for (j = 0; j < adapter->num_tx_queues; j++) {
838 		u64	restart2;
839 
840 		ring = adapter->tx_ring[j];
841 		do {
842 			start = u64_stats_fetch_begin(&ring->tx_syncp);
843 			data[i]   = ring->tx_stats.packets;
844 			data[i + 1] = ring->tx_stats.bytes;
845 			data[i + 2] = ring->tx_stats.restart_queue;
846 		} while (u64_stats_fetch_retry(&ring->tx_syncp, start));
847 		do {
848 			start = u64_stats_fetch_begin(&ring->tx_syncp2);
849 			restart2  = ring->tx_stats.restart_queue2;
850 		} while (u64_stats_fetch_retry(&ring->tx_syncp2, start));
851 		data[i + 2] += restart2;
852 
853 		i += IGC_TX_QUEUE_STATS_LEN;
854 	}
855 	for (j = 0; j < adapter->num_rx_queues; j++) {
856 		ring = adapter->rx_ring[j];
857 		do {
858 			start = u64_stats_fetch_begin(&ring->rx_syncp);
859 			data[i]   = ring->rx_stats.packets;
860 			data[i + 1] = ring->rx_stats.bytes;
861 			data[i + 2] = ring->rx_stats.drops;
862 			data[i + 3] = ring->rx_stats.csum_err;
863 			data[i + 4] = ring->rx_stats.alloc_failed;
864 		} while (u64_stats_fetch_retry(&ring->rx_syncp, start));
865 		i += IGC_RX_QUEUE_STATS_LEN;
866 	}
867 	spin_unlock(&adapter->stats64_lock);
868 }
869 
870 static int igc_ethtool_get_previous_rx_coalesce(struct igc_adapter *adapter)
871 {
872 	return (adapter->rx_itr_setting <= 3) ?
873 		adapter->rx_itr_setting : adapter->rx_itr_setting >> 2;
874 }
875 
876 static int igc_ethtool_get_previous_tx_coalesce(struct igc_adapter *adapter)
877 {
878 	return (adapter->tx_itr_setting <= 3) ?
879 		adapter->tx_itr_setting : adapter->tx_itr_setting >> 2;
880 }
881 
882 static int igc_ethtool_get_coalesce(struct net_device *netdev,
883 				    struct ethtool_coalesce *ec,
884 				    struct kernel_ethtool_coalesce *kernel_coal,
885 				    struct netlink_ext_ack *extack)
886 {
887 	struct igc_adapter *adapter = netdev_priv(netdev);
888 
889 	ec->rx_coalesce_usecs = igc_ethtool_get_previous_rx_coalesce(adapter);
890 	ec->tx_coalesce_usecs = igc_ethtool_get_previous_tx_coalesce(adapter);
891 
892 	return 0;
893 }
894 
895 static int igc_ethtool_set_coalesce(struct net_device *netdev,
896 				    struct ethtool_coalesce *ec,
897 				    struct kernel_ethtool_coalesce *kernel_coal,
898 				    struct netlink_ext_ack *extack)
899 {
900 	struct igc_adapter *adapter = netdev_priv(netdev);
901 	int i;
902 
903 	if (ec->rx_coalesce_usecs > IGC_MAX_ITR_USECS ||
904 	    (ec->rx_coalesce_usecs > 3 &&
905 	     ec->rx_coalesce_usecs < IGC_MIN_ITR_USECS) ||
906 	    ec->rx_coalesce_usecs == 2)
907 		return -EINVAL;
908 
909 	if (ec->tx_coalesce_usecs > IGC_MAX_ITR_USECS ||
910 	    (ec->tx_coalesce_usecs > 3 &&
911 	     ec->tx_coalesce_usecs < IGC_MIN_ITR_USECS) ||
912 	    ec->tx_coalesce_usecs == 2)
913 		return -EINVAL;
914 
915 	if ((adapter->flags & IGC_FLAG_QUEUE_PAIRS) &&
916 	    ec->tx_coalesce_usecs != igc_ethtool_get_previous_tx_coalesce(adapter)) {
917 		NL_SET_ERR_MSG_MOD(extack,
918 				   "Queue Pair mode enabled, both Rx and Tx coalescing controlled by rx-usecs");
919 		return -EINVAL;
920 	}
921 
922 	/* If ITR is disabled, disable DMAC */
923 	if (ec->rx_coalesce_usecs == 0) {
924 		if (adapter->flags & IGC_FLAG_DMAC)
925 			adapter->flags &= ~IGC_FLAG_DMAC;
926 	}
927 
928 	/* convert to rate of irq's per second */
929 	if (ec->rx_coalesce_usecs && ec->rx_coalesce_usecs <= 3)
930 		adapter->rx_itr_setting = ec->rx_coalesce_usecs;
931 	else
932 		adapter->rx_itr_setting = ec->rx_coalesce_usecs << 2;
933 
934 	/* convert to rate of irq's per second */
935 	if (adapter->flags & IGC_FLAG_QUEUE_PAIRS)
936 		adapter->tx_itr_setting = adapter->rx_itr_setting;
937 	else if (ec->tx_coalesce_usecs && ec->tx_coalesce_usecs <= 3)
938 		adapter->tx_itr_setting = ec->tx_coalesce_usecs;
939 	else
940 		adapter->tx_itr_setting = ec->tx_coalesce_usecs << 2;
941 
942 	for (i = 0; i < adapter->num_q_vectors; i++) {
943 		struct igc_q_vector *q_vector = adapter->q_vector[i];
944 
945 		q_vector->tx.work_limit = adapter->tx_work_limit;
946 		if (q_vector->rx.ring)
947 			q_vector->itr_val = adapter->rx_itr_setting;
948 		else
949 			q_vector->itr_val = adapter->tx_itr_setting;
950 		if (q_vector->itr_val && q_vector->itr_val <= 3)
951 			q_vector->itr_val = IGC_START_ITR;
952 		q_vector->set_itr = 1;
953 	}
954 
955 	return 0;
956 }
957 
958 #define ETHER_TYPE_FULL_MASK ((__force __be16)~0)
959 #define VLAN_TCI_FULL_MASK ((__force __be16)~0)
960 static int igc_ethtool_get_nfc_rule(struct igc_adapter *adapter,
961 				    struct ethtool_rxnfc *cmd)
962 {
963 	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
964 	struct igc_nfc_rule *rule = NULL;
965 
966 	cmd->data = IGC_MAX_RXNFC_RULES;
967 
968 	mutex_lock(&adapter->nfc_rule_lock);
969 
970 	rule = igc_get_nfc_rule(adapter, fsp->location);
971 	if (!rule)
972 		goto out;
973 
974 	fsp->flow_type = ETHER_FLOW;
975 	fsp->ring_cookie = rule->action;
976 
977 	if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
978 		fsp->h_u.ether_spec.h_proto = htons(rule->filter.etype);
979 		fsp->m_u.ether_spec.h_proto = ETHER_TYPE_FULL_MASK;
980 	}
981 
982 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE) {
983 		fsp->flow_type |= FLOW_EXT;
984 		fsp->h_ext.vlan_etype = rule->filter.vlan_etype;
985 		fsp->m_ext.vlan_etype = ETHER_TYPE_FULL_MASK;
986 	}
987 
988 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
989 		fsp->flow_type |= FLOW_EXT;
990 		fsp->h_ext.vlan_tci = htons(rule->filter.vlan_tci);
991 		fsp->m_ext.vlan_tci = htons(rule->filter.vlan_tci_mask);
992 	}
993 
994 	if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) {
995 		ether_addr_copy(fsp->h_u.ether_spec.h_dest,
996 				rule->filter.dst_addr);
997 		eth_broadcast_addr(fsp->m_u.ether_spec.h_dest);
998 	}
999 
1000 	if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) {
1001 		ether_addr_copy(fsp->h_u.ether_spec.h_source,
1002 				rule->filter.src_addr);
1003 		eth_broadcast_addr(fsp->m_u.ether_spec.h_source);
1004 	}
1005 
1006 	if (rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA) {
1007 		fsp->flow_type |= FLOW_EXT;
1008 		memcpy(fsp->h_ext.data, rule->filter.user_data, sizeof(fsp->h_ext.data));
1009 		memcpy(fsp->m_ext.data, rule->filter.user_mask, sizeof(fsp->m_ext.data));
1010 	}
1011 
1012 	mutex_unlock(&adapter->nfc_rule_lock);
1013 	return 0;
1014 
1015 out:
1016 	mutex_unlock(&adapter->nfc_rule_lock);
1017 	return -EINVAL;
1018 }
1019 
1020 static int igc_ethtool_get_nfc_rules(struct igc_adapter *adapter,
1021 				     struct ethtool_rxnfc *cmd,
1022 				     u32 *rule_locs)
1023 {
1024 	struct igc_nfc_rule *rule;
1025 	int cnt = 0;
1026 
1027 	cmd->data = IGC_MAX_RXNFC_RULES;
1028 
1029 	mutex_lock(&adapter->nfc_rule_lock);
1030 
1031 	list_for_each_entry(rule, &adapter->nfc_rule_list, list) {
1032 		if (cnt == cmd->rule_cnt) {
1033 			mutex_unlock(&adapter->nfc_rule_lock);
1034 			return -EMSGSIZE;
1035 		}
1036 		rule_locs[cnt] = rule->location;
1037 		cnt++;
1038 	}
1039 
1040 	mutex_unlock(&adapter->nfc_rule_lock);
1041 
1042 	cmd->rule_cnt = cnt;
1043 
1044 	return 0;
1045 }
1046 
1047 static int igc_ethtool_get_rss_hash_opts(struct igc_adapter *adapter,
1048 					 struct ethtool_rxnfc *cmd)
1049 {
1050 	cmd->data = 0;
1051 
1052 	/* Report default options for RSS on igc */
1053 	switch (cmd->flow_type) {
1054 	case TCP_V4_FLOW:
1055 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1056 		fallthrough;
1057 	case UDP_V4_FLOW:
1058 		if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
1059 			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1060 		fallthrough;
1061 	case SCTP_V4_FLOW:
1062 	case AH_ESP_V4_FLOW:
1063 	case AH_V4_FLOW:
1064 	case ESP_V4_FLOW:
1065 	case IPV4_FLOW:
1066 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1067 		break;
1068 	case TCP_V6_FLOW:
1069 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1070 		fallthrough;
1071 	case UDP_V6_FLOW:
1072 		if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
1073 			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1074 		fallthrough;
1075 	case SCTP_V6_FLOW:
1076 	case AH_ESP_V6_FLOW:
1077 	case AH_V6_FLOW:
1078 	case ESP_V6_FLOW:
1079 	case IPV6_FLOW:
1080 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1081 		break;
1082 	default:
1083 		return -EINVAL;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
1089 static int igc_ethtool_get_rxnfc(struct net_device *dev,
1090 				 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1091 {
1092 	struct igc_adapter *adapter = netdev_priv(dev);
1093 
1094 	switch (cmd->cmd) {
1095 	case ETHTOOL_GRXRINGS:
1096 		cmd->data = adapter->num_rx_queues;
1097 		return 0;
1098 	case ETHTOOL_GRXCLSRLCNT:
1099 		cmd->rule_cnt = adapter->nfc_rule_count;
1100 		return 0;
1101 	case ETHTOOL_GRXCLSRULE:
1102 		return igc_ethtool_get_nfc_rule(adapter, cmd);
1103 	case ETHTOOL_GRXCLSRLALL:
1104 		return igc_ethtool_get_nfc_rules(adapter, cmd, rule_locs);
1105 	case ETHTOOL_GRXFH:
1106 		return igc_ethtool_get_rss_hash_opts(adapter, cmd);
1107 	default:
1108 		return -EOPNOTSUPP;
1109 	}
1110 }
1111 
1112 #define UDP_RSS_FLAGS (IGC_FLAG_RSS_FIELD_IPV4_UDP | \
1113 		       IGC_FLAG_RSS_FIELD_IPV6_UDP)
1114 static int igc_ethtool_set_rss_hash_opt(struct igc_adapter *adapter,
1115 					struct ethtool_rxnfc *nfc)
1116 {
1117 	u32 flags = adapter->flags;
1118 
1119 	/* RSS does not support anything other than hashing
1120 	 * to queues on src and dst IPs and ports
1121 	 */
1122 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1123 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
1124 		return -EINVAL;
1125 
1126 	switch (nfc->flow_type) {
1127 	case TCP_V4_FLOW:
1128 	case TCP_V6_FLOW:
1129 		if (!(nfc->data & RXH_IP_SRC) ||
1130 		    !(nfc->data & RXH_IP_DST) ||
1131 		    !(nfc->data & RXH_L4_B_0_1) ||
1132 		    !(nfc->data & RXH_L4_B_2_3))
1133 			return -EINVAL;
1134 		break;
1135 	case UDP_V4_FLOW:
1136 		if (!(nfc->data & RXH_IP_SRC) ||
1137 		    !(nfc->data & RXH_IP_DST))
1138 			return -EINVAL;
1139 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1140 		case 0:
1141 			flags &= ~IGC_FLAG_RSS_FIELD_IPV4_UDP;
1142 			break;
1143 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1144 			flags |= IGC_FLAG_RSS_FIELD_IPV4_UDP;
1145 			break;
1146 		default:
1147 			return -EINVAL;
1148 		}
1149 		break;
1150 	case UDP_V6_FLOW:
1151 		if (!(nfc->data & RXH_IP_SRC) ||
1152 		    !(nfc->data & RXH_IP_DST))
1153 			return -EINVAL;
1154 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1155 		case 0:
1156 			flags &= ~IGC_FLAG_RSS_FIELD_IPV6_UDP;
1157 			break;
1158 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1159 			flags |= IGC_FLAG_RSS_FIELD_IPV6_UDP;
1160 			break;
1161 		default:
1162 			return -EINVAL;
1163 		}
1164 		break;
1165 	case AH_ESP_V4_FLOW:
1166 	case AH_V4_FLOW:
1167 	case ESP_V4_FLOW:
1168 	case SCTP_V4_FLOW:
1169 	case AH_ESP_V6_FLOW:
1170 	case AH_V6_FLOW:
1171 	case ESP_V6_FLOW:
1172 	case SCTP_V6_FLOW:
1173 		if (!(nfc->data & RXH_IP_SRC) ||
1174 		    !(nfc->data & RXH_IP_DST) ||
1175 		    (nfc->data & RXH_L4_B_0_1) ||
1176 		    (nfc->data & RXH_L4_B_2_3))
1177 			return -EINVAL;
1178 		break;
1179 	default:
1180 		return -EINVAL;
1181 	}
1182 
1183 	/* if we changed something we need to update flags */
1184 	if (flags != adapter->flags) {
1185 		struct igc_hw *hw = &adapter->hw;
1186 		u32 mrqc = rd32(IGC_MRQC);
1187 
1188 		if ((flags & UDP_RSS_FLAGS) &&
1189 		    !(adapter->flags & UDP_RSS_FLAGS))
1190 			netdev_err(adapter->netdev,
1191 				   "Enabling UDP RSS: fragmented packets may arrive out of order to the stack above\n");
1192 
1193 		adapter->flags = flags;
1194 
1195 		/* Perform hash on these packet types */
1196 		mrqc |= IGC_MRQC_RSS_FIELD_IPV4 |
1197 			IGC_MRQC_RSS_FIELD_IPV4_TCP |
1198 			IGC_MRQC_RSS_FIELD_IPV6 |
1199 			IGC_MRQC_RSS_FIELD_IPV6_TCP;
1200 
1201 		mrqc &= ~(IGC_MRQC_RSS_FIELD_IPV4_UDP |
1202 			  IGC_MRQC_RSS_FIELD_IPV6_UDP);
1203 
1204 		if (flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
1205 			mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
1206 
1207 		if (flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
1208 			mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
1209 
1210 		wr32(IGC_MRQC, mrqc);
1211 	}
1212 
1213 	return 0;
1214 }
1215 
1216 static void igc_ethtool_init_nfc_rule(struct igc_nfc_rule *rule,
1217 				      const struct ethtool_rx_flow_spec *fsp)
1218 {
1219 	INIT_LIST_HEAD(&rule->list);
1220 
1221 	rule->action = fsp->ring_cookie;
1222 	rule->location = fsp->location;
1223 
1224 	if ((fsp->flow_type & FLOW_EXT) && fsp->m_ext.vlan_tci) {
1225 		rule->filter.vlan_tci = ntohs(fsp->h_ext.vlan_tci);
1226 		rule->filter.vlan_tci_mask = ntohs(fsp->m_ext.vlan_tci);
1227 		rule->filter.match_flags |= IGC_FILTER_FLAG_VLAN_TCI;
1228 	}
1229 
1230 	if (fsp->m_u.ether_spec.h_proto == ETHER_TYPE_FULL_MASK) {
1231 		rule->filter.etype = ntohs(fsp->h_u.ether_spec.h_proto);
1232 		rule->filter.match_flags = IGC_FILTER_FLAG_ETHER_TYPE;
1233 	}
1234 
1235 	/* Both source and destination address filters only support the full
1236 	 * mask.
1237 	 */
1238 	if (is_broadcast_ether_addr(fsp->m_u.ether_spec.h_source)) {
1239 		rule->filter.match_flags |= IGC_FILTER_FLAG_SRC_MAC_ADDR;
1240 		ether_addr_copy(rule->filter.src_addr,
1241 				fsp->h_u.ether_spec.h_source);
1242 	}
1243 
1244 	if (is_broadcast_ether_addr(fsp->m_u.ether_spec.h_dest)) {
1245 		rule->filter.match_flags |= IGC_FILTER_FLAG_DST_MAC_ADDR;
1246 		ether_addr_copy(rule->filter.dst_addr,
1247 				fsp->h_u.ether_spec.h_dest);
1248 	}
1249 
1250 	/* VLAN etype matching */
1251 	if ((fsp->flow_type & FLOW_EXT) && fsp->h_ext.vlan_etype) {
1252 		rule->filter.vlan_etype = fsp->h_ext.vlan_etype;
1253 		rule->filter.match_flags |= IGC_FILTER_FLAG_VLAN_ETYPE;
1254 	}
1255 
1256 	/* Check for user defined data */
1257 	if ((fsp->flow_type & FLOW_EXT) &&
1258 	    (fsp->h_ext.data[0] || fsp->h_ext.data[1])) {
1259 		rule->filter.match_flags |= IGC_FILTER_FLAG_USER_DATA;
1260 		memcpy(rule->filter.user_data, fsp->h_ext.data, sizeof(fsp->h_ext.data));
1261 		memcpy(rule->filter.user_mask, fsp->m_ext.data, sizeof(fsp->m_ext.data));
1262 	}
1263 
1264 	/* The i225/i226 has various different filters. Flex filters provide a
1265 	 * way to match up to the first 128 bytes of a packet. Use them for:
1266 	 *   a) For specific user data
1267 	 *   b) For VLAN EtherType
1268 	 *   c) For full TCI match
1269 	 *   d) Or in case multiple filter criteria are set
1270 	 *
1271 	 * Otherwise, use the simple MAC, VLAN PRIO or EtherType filters.
1272 	 */
1273 	if ((rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA) ||
1274 	    (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE) ||
1275 	    ((rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) &&
1276 	     rule->filter.vlan_tci_mask == ntohs(VLAN_TCI_FULL_MASK)) ||
1277 	    (rule->filter.match_flags & (rule->filter.match_flags - 1)))
1278 		rule->flex = true;
1279 	else
1280 		rule->flex = false;
1281 }
1282 
1283 /**
1284  * igc_ethtool_check_nfc_rule() - Check if NFC rule is valid
1285  * @adapter: Pointer to adapter
1286  * @rule: Rule under evaluation
1287  *
1288  * The driver doesn't support rules with multiple matches so if more than
1289  * one bit in filter flags is set, @rule is considered invalid.
1290  *
1291  * Also, if there is already another rule with the same filter in a different
1292  * location, @rule is considered invalid.
1293  *
1294  * Context: Expects adapter->nfc_rule_lock to be held by caller.
1295  *
1296  * Return: 0 in case of success, negative errno code otherwise.
1297  */
1298 static int igc_ethtool_check_nfc_rule(struct igc_adapter *adapter,
1299 				      struct igc_nfc_rule *rule)
1300 {
1301 	struct net_device *dev = adapter->netdev;
1302 	u8 flags = rule->filter.match_flags;
1303 	struct igc_nfc_rule *tmp;
1304 
1305 	if (!flags) {
1306 		netdev_dbg(dev, "Rule with no match\n");
1307 		return -EINVAL;
1308 	}
1309 
1310 	list_for_each_entry(tmp, &adapter->nfc_rule_list, list) {
1311 		if (!memcmp(&rule->filter, &tmp->filter,
1312 			    sizeof(rule->filter)) &&
1313 		    tmp->location != rule->location) {
1314 			netdev_dbg(dev, "Rule already exists\n");
1315 			return -EEXIST;
1316 		}
1317 	}
1318 
1319 	return 0;
1320 }
1321 
1322 static int igc_ethtool_add_nfc_rule(struct igc_adapter *adapter,
1323 				    struct ethtool_rxnfc *cmd)
1324 {
1325 	struct net_device *netdev = adapter->netdev;
1326 	struct ethtool_rx_flow_spec *fsp =
1327 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1328 	struct igc_nfc_rule *rule, *old_rule;
1329 	int err;
1330 
1331 	if (!(netdev->hw_features & NETIF_F_NTUPLE)) {
1332 		netdev_dbg(netdev, "N-tuple filters disabled\n");
1333 		return -EOPNOTSUPP;
1334 	}
1335 
1336 	if ((fsp->flow_type & ~FLOW_EXT) != ETHER_FLOW) {
1337 		netdev_dbg(netdev, "Only ethernet flow type is supported\n");
1338 		return -EOPNOTSUPP;
1339 	}
1340 
1341 	if (fsp->ring_cookie >= adapter->num_rx_queues) {
1342 		netdev_dbg(netdev, "Invalid action\n");
1343 		return -EINVAL;
1344 	}
1345 
1346 	/* There are two ways to match the VLAN TCI:
1347 	 *  1. Match on PCP field and use vlan prio filter for it
1348 	 *  2. Match on complete TCI field and use flex filter for it
1349 	 */
1350 	if ((fsp->flow_type & FLOW_EXT) &&
1351 	    fsp->m_ext.vlan_tci &&
1352 	    fsp->m_ext.vlan_tci != htons(VLAN_PRIO_MASK) &&
1353 	    fsp->m_ext.vlan_tci != VLAN_TCI_FULL_MASK) {
1354 		netdev_dbg(netdev, "VLAN mask not supported\n");
1355 		return -EOPNOTSUPP;
1356 	}
1357 
1358 	/* VLAN EtherType can only be matched by full mask. */
1359 	if ((fsp->flow_type & FLOW_EXT) &&
1360 	    fsp->m_ext.vlan_etype &&
1361 	    fsp->m_ext.vlan_etype != ETHER_TYPE_FULL_MASK) {
1362 		netdev_dbg(netdev, "VLAN EtherType mask not supported\n");
1363 		return -EOPNOTSUPP;
1364 	}
1365 
1366 	if (fsp->location >= IGC_MAX_RXNFC_RULES) {
1367 		netdev_dbg(netdev, "Invalid location\n");
1368 		return -EINVAL;
1369 	}
1370 
1371 	rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1372 	if (!rule)
1373 		return -ENOMEM;
1374 
1375 	igc_ethtool_init_nfc_rule(rule, fsp);
1376 
1377 	mutex_lock(&adapter->nfc_rule_lock);
1378 
1379 	err = igc_ethtool_check_nfc_rule(adapter, rule);
1380 	if (err)
1381 		goto err;
1382 
1383 	old_rule = igc_get_nfc_rule(adapter, fsp->location);
1384 	if (old_rule)
1385 		igc_del_nfc_rule(adapter, old_rule);
1386 
1387 	err = igc_add_nfc_rule(adapter, rule);
1388 	if (err)
1389 		goto err;
1390 
1391 	mutex_unlock(&adapter->nfc_rule_lock);
1392 	return 0;
1393 
1394 err:
1395 	mutex_unlock(&adapter->nfc_rule_lock);
1396 	kfree(rule);
1397 	return err;
1398 }
1399 
1400 static int igc_ethtool_del_nfc_rule(struct igc_adapter *adapter,
1401 				    struct ethtool_rxnfc *cmd)
1402 {
1403 	struct ethtool_rx_flow_spec *fsp =
1404 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1405 	struct igc_nfc_rule *rule;
1406 
1407 	mutex_lock(&adapter->nfc_rule_lock);
1408 
1409 	rule = igc_get_nfc_rule(adapter, fsp->location);
1410 	if (!rule) {
1411 		mutex_unlock(&adapter->nfc_rule_lock);
1412 		return -EINVAL;
1413 	}
1414 
1415 	igc_del_nfc_rule(adapter, rule);
1416 
1417 	mutex_unlock(&adapter->nfc_rule_lock);
1418 	return 0;
1419 }
1420 
1421 static int igc_ethtool_set_rxnfc(struct net_device *dev,
1422 				 struct ethtool_rxnfc *cmd)
1423 {
1424 	struct igc_adapter *adapter = netdev_priv(dev);
1425 
1426 	switch (cmd->cmd) {
1427 	case ETHTOOL_SRXFH:
1428 		return igc_ethtool_set_rss_hash_opt(adapter, cmd);
1429 	case ETHTOOL_SRXCLSRLINS:
1430 		return igc_ethtool_add_nfc_rule(adapter, cmd);
1431 	case ETHTOOL_SRXCLSRLDEL:
1432 		return igc_ethtool_del_nfc_rule(adapter, cmd);
1433 	default:
1434 		return -EOPNOTSUPP;
1435 	}
1436 }
1437 
1438 void igc_write_rss_indir_tbl(struct igc_adapter *adapter)
1439 {
1440 	struct igc_hw *hw = &adapter->hw;
1441 	u32 reg = IGC_RETA(0);
1442 	u32 shift = 0;
1443 	int i = 0;
1444 
1445 	while (i < IGC_RETA_SIZE) {
1446 		u32 val = 0;
1447 		int j;
1448 
1449 		for (j = 3; j >= 0; j--) {
1450 			val <<= 8;
1451 			val |= adapter->rss_indir_tbl[i + j];
1452 		}
1453 
1454 		wr32(reg, val << shift);
1455 		reg += 4;
1456 		i += 4;
1457 	}
1458 }
1459 
1460 static u32 igc_ethtool_get_rxfh_indir_size(struct net_device *netdev)
1461 {
1462 	return IGC_RETA_SIZE;
1463 }
1464 
1465 static int igc_ethtool_get_rxfh(struct net_device *netdev,
1466 				struct ethtool_rxfh_param *rxfh)
1467 {
1468 	struct igc_adapter *adapter = netdev_priv(netdev);
1469 	int i;
1470 
1471 	rxfh->hfunc = ETH_RSS_HASH_TOP;
1472 	if (!rxfh->indir)
1473 		return 0;
1474 	for (i = 0; i < IGC_RETA_SIZE; i++)
1475 		rxfh->indir[i] = adapter->rss_indir_tbl[i];
1476 
1477 	return 0;
1478 }
1479 
1480 static int igc_ethtool_set_rxfh(struct net_device *netdev,
1481 				struct ethtool_rxfh_param *rxfh,
1482 				struct netlink_ext_ack *extack)
1483 {
1484 	struct igc_adapter *adapter = netdev_priv(netdev);
1485 	u32 num_queues;
1486 	int i;
1487 
1488 	/* We do not allow change in unsupported parameters */
1489 	if (rxfh->key ||
1490 	    (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
1491 	     rxfh->hfunc != ETH_RSS_HASH_TOP))
1492 		return -EOPNOTSUPP;
1493 	if (!rxfh->indir)
1494 		return 0;
1495 
1496 	num_queues = adapter->rss_queues;
1497 
1498 	/* Verify user input. */
1499 	for (i = 0; i < IGC_RETA_SIZE; i++)
1500 		if (rxfh->indir[i] >= num_queues)
1501 			return -EINVAL;
1502 
1503 	for (i = 0; i < IGC_RETA_SIZE; i++)
1504 		adapter->rss_indir_tbl[i] = rxfh->indir[i];
1505 
1506 	igc_write_rss_indir_tbl(adapter);
1507 
1508 	return 0;
1509 }
1510 
1511 static void igc_ethtool_get_channels(struct net_device *netdev,
1512 				     struct ethtool_channels *ch)
1513 {
1514 	struct igc_adapter *adapter = netdev_priv(netdev);
1515 
1516 	/* Report maximum channels */
1517 	ch->max_combined = igc_get_max_rss_queues(adapter);
1518 
1519 	/* Report info for other vector */
1520 	if (adapter->flags & IGC_FLAG_HAS_MSIX) {
1521 		ch->max_other = NON_Q_VECTORS;
1522 		ch->other_count = NON_Q_VECTORS;
1523 	}
1524 
1525 	ch->combined_count = adapter->rss_queues;
1526 }
1527 
1528 static int igc_ethtool_set_channels(struct net_device *netdev,
1529 				    struct ethtool_channels *ch)
1530 {
1531 	struct igc_adapter *adapter = netdev_priv(netdev);
1532 	unsigned int count = ch->combined_count;
1533 	unsigned int max_combined = 0;
1534 
1535 	/* Verify they are not requesting separate vectors */
1536 	if (!count || ch->rx_count || ch->tx_count)
1537 		return -EINVAL;
1538 
1539 	/* Verify other_count is valid and has not been changed */
1540 	if (ch->other_count != NON_Q_VECTORS)
1541 		return -EINVAL;
1542 
1543 	/* Verify the number of channels doesn't exceed hw limits */
1544 	max_combined = igc_get_max_rss_queues(adapter);
1545 	if (count > max_combined)
1546 		return -EINVAL;
1547 
1548 	if (count != adapter->rss_queues) {
1549 		adapter->rss_queues = count;
1550 		igc_set_flag_queue_pairs(adapter, max_combined);
1551 
1552 		/* Hardware has to reinitialize queues and interrupts to
1553 		 * match the new configuration.
1554 		 */
1555 		return igc_reinit_queues(adapter);
1556 	}
1557 
1558 	return 0;
1559 }
1560 
1561 static int igc_ethtool_get_ts_info(struct net_device *dev,
1562 				   struct ethtool_ts_info *info)
1563 {
1564 	struct igc_adapter *adapter = netdev_priv(dev);
1565 
1566 	if (adapter->ptp_clock)
1567 		info->phc_index = ptp_clock_index(adapter->ptp_clock);
1568 	else
1569 		info->phc_index = -1;
1570 
1571 	switch (adapter->hw.mac.type) {
1572 	case igc_i225:
1573 		info->so_timestamping =
1574 			SOF_TIMESTAMPING_TX_SOFTWARE |
1575 			SOF_TIMESTAMPING_RX_SOFTWARE |
1576 			SOF_TIMESTAMPING_SOFTWARE |
1577 			SOF_TIMESTAMPING_TX_HARDWARE |
1578 			SOF_TIMESTAMPING_RX_HARDWARE |
1579 			SOF_TIMESTAMPING_RAW_HARDWARE;
1580 
1581 		info->tx_types =
1582 			BIT(HWTSTAMP_TX_OFF) |
1583 			BIT(HWTSTAMP_TX_ON);
1584 
1585 		info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
1586 		info->rx_filters |= BIT(HWTSTAMP_FILTER_ALL);
1587 
1588 		return 0;
1589 	default:
1590 		return -EOPNOTSUPP;
1591 	}
1592 }
1593 
1594 static u32 igc_ethtool_get_priv_flags(struct net_device *netdev)
1595 {
1596 	struct igc_adapter *adapter = netdev_priv(netdev);
1597 	u32 priv_flags = 0;
1598 
1599 	if (adapter->flags & IGC_FLAG_RX_LEGACY)
1600 		priv_flags |= IGC_PRIV_FLAGS_LEGACY_RX;
1601 
1602 	return priv_flags;
1603 }
1604 
1605 static int igc_ethtool_set_priv_flags(struct net_device *netdev, u32 priv_flags)
1606 {
1607 	struct igc_adapter *adapter = netdev_priv(netdev);
1608 	unsigned int flags = adapter->flags;
1609 
1610 	flags &= ~IGC_FLAG_RX_LEGACY;
1611 	if (priv_flags & IGC_PRIV_FLAGS_LEGACY_RX)
1612 		flags |= IGC_FLAG_RX_LEGACY;
1613 
1614 	if (flags != adapter->flags) {
1615 		adapter->flags = flags;
1616 
1617 		/* reset interface to repopulate queues */
1618 		if (netif_running(netdev))
1619 			igc_reinit_locked(adapter);
1620 	}
1621 
1622 	return 0;
1623 }
1624 
1625 static int igc_ethtool_get_eee(struct net_device *netdev,
1626 			       struct ethtool_eee *edata)
1627 {
1628 	struct igc_adapter *adapter = netdev_priv(netdev);
1629 	struct igc_hw *hw = &adapter->hw;
1630 	u32 eeer;
1631 
1632 	if (hw->dev_spec._base.eee_enable)
1633 		edata->advertised =
1634 			mmd_eee_adv_to_ethtool_adv_t(adapter->eee_advert);
1635 
1636 	*edata = adapter->eee;
1637 	edata->supported = SUPPORTED_Autoneg;
1638 
1639 	eeer = rd32(IGC_EEER);
1640 
1641 	/* EEE status on negotiated link */
1642 	if (eeer & IGC_EEER_EEE_NEG)
1643 		edata->eee_active = true;
1644 
1645 	if (eeer & IGC_EEER_TX_LPI_EN)
1646 		edata->tx_lpi_enabled = true;
1647 
1648 	edata->eee_enabled = hw->dev_spec._base.eee_enable;
1649 
1650 	edata->advertised = SUPPORTED_Autoneg;
1651 	edata->lp_advertised = SUPPORTED_Autoneg;
1652 
1653 	/* Report correct negotiated EEE status for devices that
1654 	 * wrongly report EEE at half-duplex
1655 	 */
1656 	if (adapter->link_duplex == HALF_DUPLEX) {
1657 		edata->eee_enabled = false;
1658 		edata->eee_active = false;
1659 		edata->tx_lpi_enabled = false;
1660 		edata->advertised &= ~edata->advertised;
1661 	}
1662 
1663 	return 0;
1664 }
1665 
1666 static int igc_ethtool_set_eee(struct net_device *netdev,
1667 			       struct ethtool_eee *edata)
1668 {
1669 	struct igc_adapter *adapter = netdev_priv(netdev);
1670 	struct igc_hw *hw = &adapter->hw;
1671 	struct ethtool_eee eee_curr;
1672 	s32 ret_val;
1673 
1674 	memset(&eee_curr, 0, sizeof(struct ethtool_eee));
1675 
1676 	ret_val = igc_ethtool_get_eee(netdev, &eee_curr);
1677 	if (ret_val) {
1678 		netdev_err(netdev,
1679 			   "Problem setting EEE advertisement options\n");
1680 		return -EINVAL;
1681 	}
1682 
1683 	if (eee_curr.eee_enabled) {
1684 		if (eee_curr.tx_lpi_enabled != edata->tx_lpi_enabled) {
1685 			netdev_err(netdev,
1686 				   "Setting EEE tx-lpi is not supported\n");
1687 			return -EINVAL;
1688 		}
1689 
1690 		/* Tx LPI timer is not implemented currently */
1691 		if (edata->tx_lpi_timer) {
1692 			netdev_err(netdev,
1693 				   "Setting EEE Tx LPI timer is not supported\n");
1694 			return -EINVAL;
1695 		}
1696 	} else if (!edata->eee_enabled) {
1697 		netdev_err(netdev,
1698 			   "Setting EEE options are not supported with EEE disabled\n");
1699 		return -EINVAL;
1700 	}
1701 
1702 	adapter->eee_advert = ethtool_adv_to_mmd_eee_adv_t(edata->advertised);
1703 	if (hw->dev_spec._base.eee_enable != edata->eee_enabled) {
1704 		hw->dev_spec._base.eee_enable = edata->eee_enabled;
1705 		adapter->flags |= IGC_FLAG_EEE;
1706 
1707 		/* reset link */
1708 		if (netif_running(netdev))
1709 			igc_reinit_locked(adapter);
1710 		else
1711 			igc_reset(adapter);
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 static int igc_ethtool_begin(struct net_device *netdev)
1718 {
1719 	struct igc_adapter *adapter = netdev_priv(netdev);
1720 
1721 	pm_runtime_get_sync(&adapter->pdev->dev);
1722 	return 0;
1723 }
1724 
1725 static void igc_ethtool_complete(struct net_device *netdev)
1726 {
1727 	struct igc_adapter *adapter = netdev_priv(netdev);
1728 
1729 	pm_runtime_put(&adapter->pdev->dev);
1730 }
1731 
1732 static int igc_ethtool_get_link_ksettings(struct net_device *netdev,
1733 					  struct ethtool_link_ksettings *cmd)
1734 {
1735 	struct igc_adapter *adapter = netdev_priv(netdev);
1736 	struct igc_hw *hw = &adapter->hw;
1737 	u32 status;
1738 	u32 speed;
1739 
1740 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
1741 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
1742 
1743 	/* supported link modes */
1744 	ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Half);
1745 	ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Full);
1746 	ethtool_link_ksettings_add_link_mode(cmd, supported, 100baseT_Half);
1747 	ethtool_link_ksettings_add_link_mode(cmd, supported, 100baseT_Full);
1748 	ethtool_link_ksettings_add_link_mode(cmd, supported, 1000baseT_Full);
1749 	ethtool_link_ksettings_add_link_mode(cmd, supported, 2500baseT_Full);
1750 
1751 	/* twisted pair */
1752 	cmd->base.port = PORT_TP;
1753 	cmd->base.phy_address = hw->phy.addr;
1754 	ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
1755 	ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
1756 
1757 	/* advertising link modes */
1758 	if (hw->phy.autoneg_advertised & ADVERTISE_10_HALF)
1759 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Half);
1760 	if (hw->phy.autoneg_advertised & ADVERTISE_10_FULL)
1761 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Full);
1762 	if (hw->phy.autoneg_advertised & ADVERTISE_100_HALF)
1763 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 100baseT_Half);
1764 	if (hw->phy.autoneg_advertised & ADVERTISE_100_FULL)
1765 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 100baseT_Full);
1766 	if (hw->phy.autoneg_advertised & ADVERTISE_1000_FULL)
1767 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 1000baseT_Full);
1768 	if (hw->phy.autoneg_advertised & ADVERTISE_2500_FULL)
1769 		ethtool_link_ksettings_add_link_mode(cmd, advertising, 2500baseT_Full);
1770 
1771 	/* set autoneg settings */
1772 	if (hw->mac.autoneg == 1) {
1773 		ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
1774 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1775 						     Autoneg);
1776 	}
1777 
1778 	/* Set pause flow control settings */
1779 	ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
1780 
1781 	switch (hw->fc.requested_mode) {
1782 	case igc_fc_full:
1783 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
1784 		break;
1785 	case igc_fc_rx_pause:
1786 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
1787 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1788 						     Asym_Pause);
1789 		break;
1790 	case igc_fc_tx_pause:
1791 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1792 						     Asym_Pause);
1793 		break;
1794 	default:
1795 		break;
1796 	}
1797 
1798 	status = pm_runtime_suspended(&adapter->pdev->dev) ?
1799 		 0 : rd32(IGC_STATUS);
1800 
1801 	if (status & IGC_STATUS_LU) {
1802 		if (status & IGC_STATUS_SPEED_1000) {
1803 			/* For I225, STATUS will indicate 1G speed in both
1804 			 * 1 Gbps and 2.5 Gbps link modes.
1805 			 * An additional bit is used
1806 			 * to differentiate between 1 Gbps and 2.5 Gbps.
1807 			 */
1808 			if (hw->mac.type == igc_i225 &&
1809 			    (status & IGC_STATUS_SPEED_2500)) {
1810 				speed = SPEED_2500;
1811 			} else {
1812 				speed = SPEED_1000;
1813 			}
1814 		} else if (status & IGC_STATUS_SPEED_100) {
1815 			speed = SPEED_100;
1816 		} else {
1817 			speed = SPEED_10;
1818 		}
1819 		if ((status & IGC_STATUS_FD) ||
1820 		    hw->phy.media_type != igc_media_type_copper)
1821 			cmd->base.duplex = DUPLEX_FULL;
1822 		else
1823 			cmd->base.duplex = DUPLEX_HALF;
1824 	} else {
1825 		speed = SPEED_UNKNOWN;
1826 		cmd->base.duplex = DUPLEX_UNKNOWN;
1827 	}
1828 	cmd->base.speed = speed;
1829 	if (hw->mac.autoneg)
1830 		cmd->base.autoneg = AUTONEG_ENABLE;
1831 	else
1832 		cmd->base.autoneg = AUTONEG_DISABLE;
1833 
1834 	/* MDI-X => 2; MDI =>1; Invalid =>0 */
1835 	if (hw->phy.media_type == igc_media_type_copper)
1836 		cmd->base.eth_tp_mdix = hw->phy.is_mdix ? ETH_TP_MDI_X :
1837 						      ETH_TP_MDI;
1838 	else
1839 		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
1840 
1841 	if (hw->phy.mdix == AUTO_ALL_MODES)
1842 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
1843 	else
1844 		cmd->base.eth_tp_mdix_ctrl = hw->phy.mdix;
1845 
1846 	return 0;
1847 }
1848 
1849 static int
1850 igc_ethtool_set_link_ksettings(struct net_device *netdev,
1851 			       const struct ethtool_link_ksettings *cmd)
1852 {
1853 	struct igc_adapter *adapter = netdev_priv(netdev);
1854 	struct net_device *dev = adapter->netdev;
1855 	struct igc_hw *hw = &adapter->hw;
1856 	u16 advertised = 0;
1857 
1858 	/* When adapter in resetting mode, autoneg/speed/duplex
1859 	 * cannot be changed
1860 	 */
1861 	if (igc_check_reset_block(hw)) {
1862 		netdev_err(dev, "Cannot change link characteristics when reset is active\n");
1863 		return -EINVAL;
1864 	}
1865 
1866 	/* MDI setting is only allowed when autoneg enabled because
1867 	 * some hardware doesn't allow MDI setting when speed or
1868 	 * duplex is forced.
1869 	 */
1870 	if (cmd->base.eth_tp_mdix_ctrl) {
1871 		if (cmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO &&
1872 		    cmd->base.autoneg != AUTONEG_ENABLE) {
1873 			netdev_err(dev, "Forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n");
1874 			return -EINVAL;
1875 		}
1876 	}
1877 
1878 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
1879 		usleep_range(1000, 2000);
1880 
1881 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1882 						  2500baseT_Full))
1883 		advertised |= ADVERTISE_2500_FULL;
1884 
1885 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1886 						  1000baseT_Full))
1887 		advertised |= ADVERTISE_1000_FULL;
1888 
1889 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1890 						  100baseT_Full))
1891 		advertised |= ADVERTISE_100_FULL;
1892 
1893 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1894 						  100baseT_Half))
1895 		advertised |= ADVERTISE_100_HALF;
1896 
1897 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1898 						  10baseT_Full))
1899 		advertised |= ADVERTISE_10_FULL;
1900 
1901 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
1902 						  10baseT_Half))
1903 		advertised |= ADVERTISE_10_HALF;
1904 
1905 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1906 		hw->mac.autoneg = 1;
1907 		hw->phy.autoneg_advertised = advertised;
1908 		if (adapter->fc_autoneg)
1909 			hw->fc.requested_mode = igc_fc_default;
1910 	} else {
1911 		netdev_info(dev, "Force mode currently not supported\n");
1912 	}
1913 
1914 	/* MDI-X => 2; MDI => 1; Auto => 3 */
1915 	if (cmd->base.eth_tp_mdix_ctrl) {
1916 		/* fix up the value for auto (3 => 0) as zero is mapped
1917 		 * internally to auto
1918 		 */
1919 		if (cmd->base.eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
1920 			hw->phy.mdix = AUTO_ALL_MODES;
1921 		else
1922 			hw->phy.mdix = cmd->base.eth_tp_mdix_ctrl;
1923 	}
1924 
1925 	/* reset the link */
1926 	if (netif_running(adapter->netdev)) {
1927 		igc_down(adapter);
1928 		igc_up(adapter);
1929 	} else {
1930 		igc_reset(adapter);
1931 	}
1932 
1933 	clear_bit(__IGC_RESETTING, &adapter->state);
1934 
1935 	return 0;
1936 }
1937 
1938 static void igc_ethtool_diag_test(struct net_device *netdev,
1939 				  struct ethtool_test *eth_test, u64 *data)
1940 {
1941 	struct igc_adapter *adapter = netdev_priv(netdev);
1942 	bool if_running = netif_running(netdev);
1943 
1944 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1945 		netdev_info(adapter->netdev, "Offline testing starting");
1946 		set_bit(__IGC_TESTING, &adapter->state);
1947 
1948 		/* Link test performed before hardware reset so autoneg doesn't
1949 		 * interfere with test result
1950 		 */
1951 		if (!igc_link_test(adapter, &data[TEST_LINK]))
1952 			eth_test->flags |= ETH_TEST_FL_FAILED;
1953 
1954 		if (if_running)
1955 			igc_close(netdev);
1956 		else
1957 			igc_reset(adapter);
1958 
1959 		netdev_info(adapter->netdev, "Register testing starting");
1960 		if (!igc_reg_test(adapter, &data[TEST_REG]))
1961 			eth_test->flags |= ETH_TEST_FL_FAILED;
1962 
1963 		igc_reset(adapter);
1964 
1965 		netdev_info(adapter->netdev, "EEPROM testing starting");
1966 		if (!igc_eeprom_test(adapter, &data[TEST_EEP]))
1967 			eth_test->flags |= ETH_TEST_FL_FAILED;
1968 
1969 		igc_reset(adapter);
1970 
1971 		/* loopback and interrupt tests
1972 		 * will be implemented in the future
1973 		 */
1974 		data[TEST_LOOP] = 0;
1975 		data[TEST_IRQ] = 0;
1976 
1977 		clear_bit(__IGC_TESTING, &adapter->state);
1978 		if (if_running)
1979 			igc_open(netdev);
1980 	} else {
1981 		netdev_info(adapter->netdev, "Online testing starting");
1982 
1983 		/* register, eeprom, intr and loopback tests not run online */
1984 		data[TEST_REG] = 0;
1985 		data[TEST_EEP] = 0;
1986 		data[TEST_IRQ] = 0;
1987 		data[TEST_LOOP] = 0;
1988 
1989 		if (!igc_link_test(adapter, &data[TEST_LINK]))
1990 			eth_test->flags |= ETH_TEST_FL_FAILED;
1991 	}
1992 
1993 	msleep_interruptible(4 * 1000);
1994 }
1995 
1996 static const struct ethtool_ops igc_ethtool_ops = {
1997 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
1998 	.get_drvinfo		= igc_ethtool_get_drvinfo,
1999 	.get_regs_len		= igc_ethtool_get_regs_len,
2000 	.get_regs		= igc_ethtool_get_regs,
2001 	.get_wol		= igc_ethtool_get_wol,
2002 	.set_wol		= igc_ethtool_set_wol,
2003 	.get_msglevel		= igc_ethtool_get_msglevel,
2004 	.set_msglevel		= igc_ethtool_set_msglevel,
2005 	.nway_reset		= igc_ethtool_nway_reset,
2006 	.get_link		= igc_ethtool_get_link,
2007 	.get_eeprom_len		= igc_ethtool_get_eeprom_len,
2008 	.get_eeprom		= igc_ethtool_get_eeprom,
2009 	.set_eeprom		= igc_ethtool_set_eeprom,
2010 	.get_ringparam		= igc_ethtool_get_ringparam,
2011 	.set_ringparam		= igc_ethtool_set_ringparam,
2012 	.get_pauseparam		= igc_ethtool_get_pauseparam,
2013 	.set_pauseparam		= igc_ethtool_set_pauseparam,
2014 	.get_strings		= igc_ethtool_get_strings,
2015 	.get_sset_count		= igc_ethtool_get_sset_count,
2016 	.get_ethtool_stats	= igc_ethtool_get_stats,
2017 	.get_coalesce		= igc_ethtool_get_coalesce,
2018 	.set_coalesce		= igc_ethtool_set_coalesce,
2019 	.get_rxnfc		= igc_ethtool_get_rxnfc,
2020 	.set_rxnfc		= igc_ethtool_set_rxnfc,
2021 	.get_rxfh_indir_size	= igc_ethtool_get_rxfh_indir_size,
2022 	.get_rxfh		= igc_ethtool_get_rxfh,
2023 	.set_rxfh		= igc_ethtool_set_rxfh,
2024 	.get_ts_info		= igc_ethtool_get_ts_info,
2025 	.get_channels		= igc_ethtool_get_channels,
2026 	.set_channels		= igc_ethtool_set_channels,
2027 	.get_priv_flags		= igc_ethtool_get_priv_flags,
2028 	.set_priv_flags		= igc_ethtool_set_priv_flags,
2029 	.get_eee		= igc_ethtool_get_eee,
2030 	.set_eee		= igc_ethtool_set_eee,
2031 	.begin			= igc_ethtool_begin,
2032 	.complete		= igc_ethtool_complete,
2033 	.get_link_ksettings	= igc_ethtool_get_link_ksettings,
2034 	.set_link_ksettings	= igc_ethtool_set_link_ksettings,
2035 	.self_test		= igc_ethtool_diag_test,
2036 };
2037 
2038 void igc_ethtool_set_ops(struct net_device *netdev)
2039 {
2040 	netdev->ethtool_ops = &igc_ethtool_ops;
2041 }
2042