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