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