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