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 
8 #include "igc.h"
9 #include "igc_diag.h"
10 
11 /* forward declaration */
12 struct igc_stats {
13 	char stat_string[ETH_GSTRING_LEN];
14 	int sizeof_stat;
15 	int stat_offset;
16 };
17 
18 #define IGC_STAT(_name, _stat) { \
19 	.stat_string = _name, \
20 	.sizeof_stat = sizeof_field(struct igc_adapter, _stat), \
21 	.stat_offset = offsetof(struct igc_adapter, _stat) \
22 }
23 
24 static const struct igc_stats igc_gstrings_stats[] = {
25 	IGC_STAT("rx_packets", stats.gprc),
26 	IGC_STAT("tx_packets", stats.gptc),
27 	IGC_STAT("rx_bytes", stats.gorc),
28 	IGC_STAT("tx_bytes", stats.gotc),
29 	IGC_STAT("rx_broadcast", stats.bprc),
30 	IGC_STAT("tx_broadcast", stats.bptc),
31 	IGC_STAT("rx_multicast", stats.mprc),
32 	IGC_STAT("tx_multicast", stats.mptc),
33 	IGC_STAT("multicast", stats.mprc),
34 	IGC_STAT("collisions", stats.colc),
35 	IGC_STAT("rx_crc_errors", stats.crcerrs),
36 	IGC_STAT("rx_no_buffer_count", stats.rnbc),
37 	IGC_STAT("rx_missed_errors", stats.mpc),
38 	IGC_STAT("tx_aborted_errors", stats.ecol),
39 	IGC_STAT("tx_carrier_errors", stats.tncrs),
40 	IGC_STAT("tx_window_errors", stats.latecol),
41 	IGC_STAT("tx_abort_late_coll", stats.latecol),
42 	IGC_STAT("tx_deferred_ok", stats.dc),
43 	IGC_STAT("tx_single_coll_ok", stats.scc),
44 	IGC_STAT("tx_multi_coll_ok", stats.mcc),
45 	IGC_STAT("tx_timeout_count", tx_timeout_count),
46 	IGC_STAT("rx_long_length_errors", stats.roc),
47 	IGC_STAT("rx_short_length_errors", stats.ruc),
48 	IGC_STAT("rx_align_errors", stats.algnerrc),
49 	IGC_STAT("tx_tcp_seg_good", stats.tsctc),
50 	IGC_STAT("tx_tcp_seg_failed", stats.tsctfc),
51 	IGC_STAT("rx_flow_control_xon", stats.xonrxc),
52 	IGC_STAT("rx_flow_control_xoff", stats.xoffrxc),
53 	IGC_STAT("tx_flow_control_xon", stats.xontxc),
54 	IGC_STAT("tx_flow_control_xoff", stats.xofftxc),
55 	IGC_STAT("rx_long_byte_count", stats.gorc),
56 	IGC_STAT("tx_dma_out_of_sync", stats.doosync),
57 	IGC_STAT("tx_smbus", stats.mgptc),
58 	IGC_STAT("rx_smbus", stats.mgprc),
59 	IGC_STAT("dropped_smbus", stats.mgpdc),
60 	IGC_STAT("os2bmc_rx_by_bmc", stats.o2bgptc),
61 	IGC_STAT("os2bmc_tx_by_bmc", stats.b2ospc),
62 	IGC_STAT("os2bmc_tx_by_host", stats.o2bspc),
63 	IGC_STAT("os2bmc_rx_by_host", stats.b2ogprc),
64 	IGC_STAT("tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
65 	IGC_STAT("tx_hwtstamp_skipped", tx_hwtstamp_skipped),
66 	IGC_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
67 };
68 
69 #define IGC_NETDEV_STAT(_net_stat) { \
70 	.stat_string = __stringify(_net_stat), \
71 	.sizeof_stat = sizeof_field(struct rtnl_link_stats64, _net_stat), \
72 	.stat_offset = offsetof(struct rtnl_link_stats64, _net_stat) \
73 }
74 
75 static const struct igc_stats igc_gstrings_net_stats[] = {
76 	IGC_NETDEV_STAT(rx_errors),
77 	IGC_NETDEV_STAT(tx_errors),
78 	IGC_NETDEV_STAT(tx_dropped),
79 	IGC_NETDEV_STAT(rx_length_errors),
80 	IGC_NETDEV_STAT(rx_over_errors),
81 	IGC_NETDEV_STAT(rx_frame_errors),
82 	IGC_NETDEV_STAT(rx_fifo_errors),
83 	IGC_NETDEV_STAT(tx_fifo_errors),
84 	IGC_NETDEV_STAT(tx_heartbeat_errors)
85 };
86 
87 enum igc_diagnostics_results {
88 	TEST_REG = 0,
89 	TEST_EEP,
90 	TEST_IRQ,
91 	TEST_LOOP,
92 	TEST_LINK
93 };
94 
95 static const char igc_gstrings_test[][ETH_GSTRING_LEN] = {
96 	[TEST_REG]  = "Register test  (offline)",
97 	[TEST_EEP]  = "Eeprom test    (offline)",
98 	[TEST_IRQ]  = "Interrupt test (offline)",
99 	[TEST_LOOP] = "Loopback test  (offline)",
100 	[TEST_LINK] = "Link test   (on/offline)"
101 };
102 
103 #define IGC_TEST_LEN (sizeof(igc_gstrings_test) / ETH_GSTRING_LEN)
104 
105 #define IGC_GLOBAL_STATS_LEN	\
106 	(sizeof(igc_gstrings_stats) / sizeof(struct igc_stats))
107 #define IGC_NETDEV_STATS_LEN	\
108 	(sizeof(igc_gstrings_net_stats) / sizeof(struct igc_stats))
109 #define IGC_RX_QUEUE_STATS_LEN \
110 	(sizeof(struct igc_rx_queue_stats) / sizeof(u64))
111 #define IGC_TX_QUEUE_STATS_LEN 3 /* packets, bytes, restart_queue */
112 #define IGC_QUEUE_STATS_LEN \
113 	((((struct igc_adapter *)netdev_priv(netdev))->num_rx_queues * \
114 	  IGC_RX_QUEUE_STATS_LEN) + \
115 	 (((struct igc_adapter *)netdev_priv(netdev))->num_tx_queues * \
116 	  IGC_TX_QUEUE_STATS_LEN))
117 #define IGC_STATS_LEN \
118 	(IGC_GLOBAL_STATS_LEN + IGC_NETDEV_STATS_LEN + IGC_QUEUE_STATS_LEN)
119 
120 static const char igc_priv_flags_strings[][ETH_GSTRING_LEN] = {
121 #define IGC_PRIV_FLAGS_LEGACY_RX	BIT(0)
122 	"legacy-rx",
123 };
124 
125 #define IGC_PRIV_FLAGS_STR_LEN ARRAY_SIZE(igc_priv_flags_strings)
126 
127 static void igc_ethtool_get_drvinfo(struct net_device *netdev,
128 				    struct ethtool_drvinfo *drvinfo)
129 {
130 	struct igc_adapter *adapter = netdev_priv(netdev);
131 
132 	strlcpy(drvinfo->driver,  igc_driver_name, sizeof(drvinfo->driver));
133 	strlcpy(drvinfo->version, igc_driver_version, sizeof(drvinfo->version));
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 		/* Fall through */
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 		/* Fall through */
1023 	case SCTP_V4_FLOW:
1024 		/* Fall through */
1025 	case AH_ESP_V4_FLOW:
1026 		/* Fall through */
1027 	case AH_V4_FLOW:
1028 		/* Fall through */
1029 	case ESP_V4_FLOW:
1030 		/* Fall through */
1031 	case IPV4_FLOW:
1032 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1033 		break;
1034 	case TCP_V6_FLOW:
1035 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1036 		/* Fall through */
1037 	case UDP_V6_FLOW:
1038 		if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
1039 			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1040 		/* Fall through */
1041 	case SCTP_V6_FLOW:
1042 		/* Fall through */
1043 	case AH_ESP_V6_FLOW:
1044 		/* Fall through */
1045 	case AH_V6_FLOW:
1046 		/* Fall through */
1047 	case ESP_V6_FLOW:
1048 		/* Fall through */
1049 	case IPV6_FLOW:
1050 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1051 		break;
1052 	default:
1053 		return -EINVAL;
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 static int igc_ethtool_get_rxnfc(struct net_device *dev,
1060 				 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1061 {
1062 	struct igc_adapter *adapter = netdev_priv(dev);
1063 
1064 	switch (cmd->cmd) {
1065 	case ETHTOOL_GRXRINGS:
1066 		cmd->data = adapter->num_rx_queues;
1067 		return 0;
1068 	case ETHTOOL_GRXCLSRLCNT:
1069 		cmd->rule_cnt = adapter->nfc_rule_count;
1070 		return 0;
1071 	case ETHTOOL_GRXCLSRULE:
1072 		return igc_ethtool_get_nfc_rule(adapter, cmd);
1073 	case ETHTOOL_GRXCLSRLALL:
1074 		return igc_ethtool_get_nfc_rules(adapter, cmd, rule_locs);
1075 	case ETHTOOL_GRXFH:
1076 		return igc_ethtool_get_rss_hash_opts(adapter, cmd);
1077 	default:
1078 		return -EOPNOTSUPP;
1079 	}
1080 }
1081 
1082 #define UDP_RSS_FLAGS (IGC_FLAG_RSS_FIELD_IPV4_UDP | \
1083 		       IGC_FLAG_RSS_FIELD_IPV6_UDP)
1084 static int igc_ethtool_set_rss_hash_opt(struct igc_adapter *adapter,
1085 					struct ethtool_rxnfc *nfc)
1086 {
1087 	u32 flags = adapter->flags;
1088 
1089 	/* RSS does not support anything other than hashing
1090 	 * to queues on src and dst IPs and ports
1091 	 */
1092 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1093 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
1094 		return -EINVAL;
1095 
1096 	switch (nfc->flow_type) {
1097 	case TCP_V4_FLOW:
1098 	case TCP_V6_FLOW:
1099 		if (!(nfc->data & RXH_IP_SRC) ||
1100 		    !(nfc->data & RXH_IP_DST) ||
1101 		    !(nfc->data & RXH_L4_B_0_1) ||
1102 		    !(nfc->data & RXH_L4_B_2_3))
1103 			return -EINVAL;
1104 		break;
1105 	case UDP_V4_FLOW:
1106 		if (!(nfc->data & RXH_IP_SRC) ||
1107 		    !(nfc->data & RXH_IP_DST))
1108 			return -EINVAL;
1109 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1110 		case 0:
1111 			flags &= ~IGC_FLAG_RSS_FIELD_IPV4_UDP;
1112 			break;
1113 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1114 			flags |= IGC_FLAG_RSS_FIELD_IPV4_UDP;
1115 			break;
1116 		default:
1117 			return -EINVAL;
1118 		}
1119 		break;
1120 	case UDP_V6_FLOW:
1121 		if (!(nfc->data & RXH_IP_SRC) ||
1122 		    !(nfc->data & RXH_IP_DST))
1123 			return -EINVAL;
1124 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1125 		case 0:
1126 			flags &= ~IGC_FLAG_RSS_FIELD_IPV6_UDP;
1127 			break;
1128 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1129 			flags |= IGC_FLAG_RSS_FIELD_IPV6_UDP;
1130 			break;
1131 		default:
1132 			return -EINVAL;
1133 		}
1134 		break;
1135 	case AH_ESP_V4_FLOW:
1136 	case AH_V4_FLOW:
1137 	case ESP_V4_FLOW:
1138 	case SCTP_V4_FLOW:
1139 	case AH_ESP_V6_FLOW:
1140 	case AH_V6_FLOW:
1141 	case ESP_V6_FLOW:
1142 	case SCTP_V6_FLOW:
1143 		if (!(nfc->data & RXH_IP_SRC) ||
1144 		    !(nfc->data & RXH_IP_DST) ||
1145 		    (nfc->data & RXH_L4_B_0_1) ||
1146 		    (nfc->data & RXH_L4_B_2_3))
1147 			return -EINVAL;
1148 		break;
1149 	default:
1150 		return -EINVAL;
1151 	}
1152 
1153 	/* if we changed something we need to update flags */
1154 	if (flags != adapter->flags) {
1155 		struct igc_hw *hw = &adapter->hw;
1156 		u32 mrqc = rd32(IGC_MRQC);
1157 
1158 		if ((flags & UDP_RSS_FLAGS) &&
1159 		    !(adapter->flags & UDP_RSS_FLAGS))
1160 			netdev_err(adapter->netdev,
1161 				   "Enabling UDP RSS: fragmented packets may arrive out of order to the stack above\n");
1162 
1163 		adapter->flags = flags;
1164 
1165 		/* Perform hash on these packet types */
1166 		mrqc |= IGC_MRQC_RSS_FIELD_IPV4 |
1167 			IGC_MRQC_RSS_FIELD_IPV4_TCP |
1168 			IGC_MRQC_RSS_FIELD_IPV6 |
1169 			IGC_MRQC_RSS_FIELD_IPV6_TCP;
1170 
1171 		mrqc &= ~(IGC_MRQC_RSS_FIELD_IPV4_UDP |
1172 			  IGC_MRQC_RSS_FIELD_IPV6_UDP);
1173 
1174 		if (flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
1175 			mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
1176 
1177 		if (flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
1178 			mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
1179 
1180 		wr32(IGC_MRQC, mrqc);
1181 	}
1182 
1183 	return 0;
1184 }
1185 
1186 static void igc_ethtool_init_nfc_rule(struct igc_nfc_rule *rule,
1187 				      const struct ethtool_rx_flow_spec *fsp)
1188 {
1189 	INIT_LIST_HEAD(&rule->list);
1190 
1191 	rule->action = fsp->ring_cookie;
1192 	rule->location = fsp->location;
1193 
1194 	if ((fsp->flow_type & FLOW_EXT) && fsp->m_ext.vlan_tci) {
1195 		rule->filter.vlan_tci = ntohs(fsp->h_ext.vlan_tci);
1196 		rule->filter.match_flags |= IGC_FILTER_FLAG_VLAN_TCI;
1197 	}
1198 
1199 	if (fsp->m_u.ether_spec.h_proto == ETHER_TYPE_FULL_MASK) {
1200 		rule->filter.etype = ntohs(fsp->h_u.ether_spec.h_proto);
1201 		rule->filter.match_flags = IGC_FILTER_FLAG_ETHER_TYPE;
1202 	}
1203 
1204 	/* Both source and destination address filters only support the full
1205 	 * mask.
1206 	 */
1207 	if (is_broadcast_ether_addr(fsp->m_u.ether_spec.h_source)) {
1208 		rule->filter.match_flags |= IGC_FILTER_FLAG_SRC_MAC_ADDR;
1209 		ether_addr_copy(rule->filter.src_addr,
1210 				fsp->h_u.ether_spec.h_source);
1211 	}
1212 
1213 	if (is_broadcast_ether_addr(fsp->m_u.ether_spec.h_dest)) {
1214 		rule->filter.match_flags |= IGC_FILTER_FLAG_DST_MAC_ADDR;
1215 		ether_addr_copy(rule->filter.dst_addr,
1216 				fsp->h_u.ether_spec.h_dest);
1217 	}
1218 }
1219 
1220 /**
1221  * igc_ethtool_check_nfc_rule() - Check if NFC rule is valid
1222  * @adapter: Pointer to adapter
1223  * @rule: Rule under evaluation
1224  *
1225  * The driver doesn't support rules with multiple matches so if more than
1226  * one bit in filter flags is set, @rule is considered invalid.
1227  *
1228  * Also, if there is already another rule with the same filter in a different
1229  * location, @rule is considered invalid.
1230  *
1231  * Context: Expects adapter->nfc_rule_lock to be held by caller.
1232  *
1233  * Return: 0 in case of success, negative errno code otherwise.
1234  */
1235 static int igc_ethtool_check_nfc_rule(struct igc_adapter *adapter,
1236 				      struct igc_nfc_rule *rule)
1237 {
1238 	struct net_device *dev = adapter->netdev;
1239 	u8 flags = rule->filter.match_flags;
1240 	struct igc_nfc_rule *tmp;
1241 
1242 	if (!flags) {
1243 		netdev_dbg(dev, "Rule with no match\n");
1244 		return -EINVAL;
1245 	}
1246 
1247 	if (flags & (flags - 1)) {
1248 		netdev_dbg(dev, "Rule with multiple matches not supported\n");
1249 		return -EOPNOTSUPP;
1250 	}
1251 
1252 	list_for_each_entry(tmp, &adapter->nfc_rule_list, list) {
1253 		if (!memcmp(&rule->filter, &tmp->filter,
1254 			    sizeof(rule->filter)) &&
1255 		    tmp->location != rule->location) {
1256 			netdev_dbg(dev, "Rule already exists\n");
1257 			return -EEXIST;
1258 		}
1259 	}
1260 
1261 	return 0;
1262 }
1263 
1264 static int igc_ethtool_add_nfc_rule(struct igc_adapter *adapter,
1265 				    struct ethtool_rxnfc *cmd)
1266 {
1267 	struct net_device *netdev = adapter->netdev;
1268 	struct ethtool_rx_flow_spec *fsp =
1269 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1270 	struct igc_nfc_rule *rule, *old_rule;
1271 	int err;
1272 
1273 	if (!(netdev->hw_features & NETIF_F_NTUPLE)) {
1274 		netdev_dbg(netdev, "N-tuple filters disabled\n");
1275 		return -EOPNOTSUPP;
1276 	}
1277 
1278 	if ((fsp->flow_type & ~FLOW_EXT) != ETHER_FLOW) {
1279 		netdev_dbg(netdev, "Only ethernet flow type is supported\n");
1280 		return -EOPNOTSUPP;
1281 	}
1282 
1283 	if ((fsp->flow_type & FLOW_EXT) &&
1284 	    fsp->m_ext.vlan_tci != htons(VLAN_PRIO_MASK)) {
1285 		netdev_dbg(netdev, "VLAN mask not supported\n");
1286 		return -EOPNOTSUPP;
1287 	}
1288 
1289 	if (fsp->ring_cookie >= adapter->num_rx_queues) {
1290 		netdev_dbg(netdev, "Invalid action\n");
1291 		return -EINVAL;
1292 	}
1293 
1294 	if (fsp->location >= IGC_MAX_RXNFC_RULES) {
1295 		netdev_dbg(netdev, "Invalid location\n");
1296 		return -EINVAL;
1297 	}
1298 
1299 	rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1300 	if (!rule)
1301 		return -ENOMEM;
1302 
1303 	igc_ethtool_init_nfc_rule(rule, fsp);
1304 
1305 	mutex_lock(&adapter->nfc_rule_lock);
1306 
1307 	err = igc_ethtool_check_nfc_rule(adapter, rule);
1308 	if (err)
1309 		goto err;
1310 
1311 	old_rule = igc_get_nfc_rule(adapter, fsp->location);
1312 	if (old_rule)
1313 		igc_del_nfc_rule(adapter, old_rule);
1314 
1315 	err = igc_add_nfc_rule(adapter, rule);
1316 	if (err)
1317 		goto err;
1318 
1319 	mutex_unlock(&adapter->nfc_rule_lock);
1320 	return 0;
1321 
1322 err:
1323 	mutex_unlock(&adapter->nfc_rule_lock);
1324 	kfree(rule);
1325 	return err;
1326 }
1327 
1328 static int igc_ethtool_del_nfc_rule(struct igc_adapter *adapter,
1329 				    struct ethtool_rxnfc *cmd)
1330 {
1331 	struct ethtool_rx_flow_spec *fsp =
1332 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1333 	struct igc_nfc_rule *rule;
1334 
1335 	mutex_lock(&adapter->nfc_rule_lock);
1336 
1337 	rule = igc_get_nfc_rule(adapter, fsp->location);
1338 	if (!rule) {
1339 		mutex_unlock(&adapter->nfc_rule_lock);
1340 		return -EINVAL;
1341 	}
1342 
1343 	igc_del_nfc_rule(adapter, rule);
1344 
1345 	mutex_unlock(&adapter->nfc_rule_lock);
1346 	return 0;
1347 }
1348 
1349 static int igc_ethtool_set_rxnfc(struct net_device *dev,
1350 				 struct ethtool_rxnfc *cmd)
1351 {
1352 	struct igc_adapter *adapter = netdev_priv(dev);
1353 
1354 	switch (cmd->cmd) {
1355 	case ETHTOOL_SRXFH:
1356 		return igc_ethtool_set_rss_hash_opt(adapter, cmd);
1357 	case ETHTOOL_SRXCLSRLINS:
1358 		return igc_ethtool_add_nfc_rule(adapter, cmd);
1359 	case ETHTOOL_SRXCLSRLDEL:
1360 		return igc_ethtool_del_nfc_rule(adapter, cmd);
1361 	default:
1362 		return -EOPNOTSUPP;
1363 	}
1364 }
1365 
1366 void igc_write_rss_indir_tbl(struct igc_adapter *adapter)
1367 {
1368 	struct igc_hw *hw = &adapter->hw;
1369 	u32 reg = IGC_RETA(0);
1370 	u32 shift = 0;
1371 	int i = 0;
1372 
1373 	while (i < IGC_RETA_SIZE) {
1374 		u32 val = 0;
1375 		int j;
1376 
1377 		for (j = 3; j >= 0; j--) {
1378 			val <<= 8;
1379 			val |= adapter->rss_indir_tbl[i + j];
1380 		}
1381 
1382 		wr32(reg, val << shift);
1383 		reg += 4;
1384 		i += 4;
1385 	}
1386 }
1387 
1388 static u32 igc_ethtool_get_rxfh_indir_size(struct net_device *netdev)
1389 {
1390 	return IGC_RETA_SIZE;
1391 }
1392 
1393 static int igc_ethtool_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1394 				u8 *hfunc)
1395 {
1396 	struct igc_adapter *adapter = netdev_priv(netdev);
1397 	int i;
1398 
1399 	if (hfunc)
1400 		*hfunc = ETH_RSS_HASH_TOP;
1401 	if (!indir)
1402 		return 0;
1403 	for (i = 0; i < IGC_RETA_SIZE; i++)
1404 		indir[i] = adapter->rss_indir_tbl[i];
1405 
1406 	return 0;
1407 }
1408 
1409 static int igc_ethtool_set_rxfh(struct net_device *netdev, const u32 *indir,
1410 				const u8 *key, const u8 hfunc)
1411 {
1412 	struct igc_adapter *adapter = netdev_priv(netdev);
1413 	u32 num_queues;
1414 	int i;
1415 
1416 	/* We do not allow change in unsupported parameters */
1417 	if (key ||
1418 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1419 		return -EOPNOTSUPP;
1420 	if (!indir)
1421 		return 0;
1422 
1423 	num_queues = adapter->rss_queues;
1424 
1425 	/* Verify user input. */
1426 	for (i = 0; i < IGC_RETA_SIZE; i++)
1427 		if (indir[i] >= num_queues)
1428 			return -EINVAL;
1429 
1430 	for (i = 0; i < IGC_RETA_SIZE; i++)
1431 		adapter->rss_indir_tbl[i] = indir[i];
1432 
1433 	igc_write_rss_indir_tbl(adapter);
1434 
1435 	return 0;
1436 }
1437 
1438 static void igc_ethtool_get_channels(struct net_device *netdev,
1439 				     struct ethtool_channels *ch)
1440 {
1441 	struct igc_adapter *adapter = netdev_priv(netdev);
1442 
1443 	/* Report maximum channels */
1444 	ch->max_combined = igc_get_max_rss_queues(adapter);
1445 
1446 	/* Report info for other vector */
1447 	if (adapter->flags & IGC_FLAG_HAS_MSIX) {
1448 		ch->max_other = NON_Q_VECTORS;
1449 		ch->other_count = NON_Q_VECTORS;
1450 	}
1451 
1452 	ch->combined_count = adapter->rss_queues;
1453 }
1454 
1455 static int igc_ethtool_set_channels(struct net_device *netdev,
1456 				    struct ethtool_channels *ch)
1457 {
1458 	struct igc_adapter *adapter = netdev_priv(netdev);
1459 	unsigned int count = ch->combined_count;
1460 	unsigned int max_combined = 0;
1461 
1462 	/* Verify they are not requesting separate vectors */
1463 	if (!count || ch->rx_count || ch->tx_count)
1464 		return -EINVAL;
1465 
1466 	/* Verify other_count is valid and has not been changed */
1467 	if (ch->other_count != NON_Q_VECTORS)
1468 		return -EINVAL;
1469 
1470 	/* Verify the number of channels doesn't exceed hw limits */
1471 	max_combined = igc_get_max_rss_queues(adapter);
1472 	if (count > max_combined)
1473 		return -EINVAL;
1474 
1475 	if (count != adapter->rss_queues) {
1476 		adapter->rss_queues = count;
1477 		igc_set_flag_queue_pairs(adapter, max_combined);
1478 
1479 		/* Hardware has to reinitialize queues and interrupts to
1480 		 * match the new configuration.
1481 		 */
1482 		return igc_reinit_queues(adapter);
1483 	}
1484 
1485 	return 0;
1486 }
1487 
1488 static int igc_ethtool_get_ts_info(struct net_device *dev,
1489 				   struct ethtool_ts_info *info)
1490 {
1491 	struct igc_adapter *adapter = netdev_priv(dev);
1492 
1493 	if (adapter->ptp_clock)
1494 		info->phc_index = ptp_clock_index(adapter->ptp_clock);
1495 	else
1496 		info->phc_index = -1;
1497 
1498 	switch (adapter->hw.mac.type) {
1499 	case igc_i225:
1500 		info->so_timestamping =
1501 			SOF_TIMESTAMPING_TX_SOFTWARE |
1502 			SOF_TIMESTAMPING_RX_SOFTWARE |
1503 			SOF_TIMESTAMPING_SOFTWARE |
1504 			SOF_TIMESTAMPING_TX_HARDWARE |
1505 			SOF_TIMESTAMPING_RX_HARDWARE |
1506 			SOF_TIMESTAMPING_RAW_HARDWARE;
1507 
1508 		info->tx_types =
1509 			BIT(HWTSTAMP_TX_OFF) |
1510 			BIT(HWTSTAMP_TX_ON);
1511 
1512 		info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
1513 		info->rx_filters |= BIT(HWTSTAMP_FILTER_ALL);
1514 
1515 		return 0;
1516 	default:
1517 		return -EOPNOTSUPP;
1518 	}
1519 }
1520 
1521 static u32 igc_ethtool_get_priv_flags(struct net_device *netdev)
1522 {
1523 	struct igc_adapter *adapter = netdev_priv(netdev);
1524 	u32 priv_flags = 0;
1525 
1526 	if (adapter->flags & IGC_FLAG_RX_LEGACY)
1527 		priv_flags |= IGC_PRIV_FLAGS_LEGACY_RX;
1528 
1529 	return priv_flags;
1530 }
1531 
1532 static int igc_ethtool_set_priv_flags(struct net_device *netdev, u32 priv_flags)
1533 {
1534 	struct igc_adapter *adapter = netdev_priv(netdev);
1535 	unsigned int flags = adapter->flags;
1536 
1537 	flags &= ~IGC_FLAG_RX_LEGACY;
1538 	if (priv_flags & IGC_PRIV_FLAGS_LEGACY_RX)
1539 		flags |= IGC_FLAG_RX_LEGACY;
1540 
1541 	if (flags != adapter->flags) {
1542 		adapter->flags = flags;
1543 
1544 		/* reset interface to repopulate queues */
1545 		if (netif_running(netdev))
1546 			igc_reinit_locked(adapter);
1547 	}
1548 
1549 	return 0;
1550 }
1551 
1552 static int igc_ethtool_begin(struct net_device *netdev)
1553 {
1554 	struct igc_adapter *adapter = netdev_priv(netdev);
1555 
1556 	pm_runtime_get_sync(&adapter->pdev->dev);
1557 	return 0;
1558 }
1559 
1560 static void igc_ethtool_complete(struct net_device *netdev)
1561 {
1562 	struct igc_adapter *adapter = netdev_priv(netdev);
1563 
1564 	pm_runtime_put(&adapter->pdev->dev);
1565 }
1566 
1567 static int igc_ethtool_get_link_ksettings(struct net_device *netdev,
1568 					  struct ethtool_link_ksettings *cmd)
1569 {
1570 	struct igc_adapter *adapter = netdev_priv(netdev);
1571 	struct igc_hw *hw = &adapter->hw;
1572 	u32 status;
1573 	u32 speed;
1574 
1575 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
1576 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
1577 
1578 	/* supported link modes */
1579 	ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Half);
1580 	ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Full);
1581 	ethtool_link_ksettings_add_link_mode(cmd, supported, 100baseT_Half);
1582 	ethtool_link_ksettings_add_link_mode(cmd, supported, 100baseT_Full);
1583 	ethtool_link_ksettings_add_link_mode(cmd, supported, 1000baseT_Full);
1584 	ethtool_link_ksettings_add_link_mode(cmd, supported, 2500baseT_Full);
1585 
1586 	/* twisted pair */
1587 	cmd->base.port = PORT_TP;
1588 	cmd->base.phy_address = hw->phy.addr;
1589 
1590 	/* advertising link modes */
1591 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Half);
1592 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Full);
1593 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 100baseT_Half);
1594 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 100baseT_Full);
1595 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 1000baseT_Full);
1596 	ethtool_link_ksettings_add_link_mode(cmd, advertising, 2500baseT_Full);
1597 
1598 	/* set autoneg settings */
1599 	if (hw->mac.autoneg == 1) {
1600 		ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
1601 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1602 						     Autoneg);
1603 	}
1604 
1605 	switch (hw->fc.requested_mode) {
1606 	case igc_fc_full:
1607 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
1608 		break;
1609 	case igc_fc_rx_pause:
1610 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
1611 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1612 						     Asym_Pause);
1613 		break;
1614 	case igc_fc_tx_pause:
1615 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1616 						     Asym_Pause);
1617 		break;
1618 	default:
1619 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
1620 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
1621 						     Asym_Pause);
1622 	}
1623 
1624 	status = rd32(IGC_STATUS);
1625 
1626 	if (status & IGC_STATUS_LU) {
1627 		if (status & IGC_STATUS_SPEED_1000) {
1628 			/* For I225, STATUS will indicate 1G speed in both
1629 			 * 1 Gbps and 2.5 Gbps link modes.
1630 			 * An additional bit is used
1631 			 * to differentiate between 1 Gbps and 2.5 Gbps.
1632 			 */
1633 			if (hw->mac.type == igc_i225 &&
1634 			    (status & IGC_STATUS_SPEED_2500)) {
1635 				speed = SPEED_2500;
1636 			} else {
1637 				speed = SPEED_1000;
1638 			}
1639 		} else if (status & IGC_STATUS_SPEED_100) {
1640 			speed = SPEED_100;
1641 		} else {
1642 			speed = SPEED_10;
1643 		}
1644 		if ((status & IGC_STATUS_FD) ||
1645 		    hw->phy.media_type != igc_media_type_copper)
1646 			cmd->base.duplex = DUPLEX_FULL;
1647 		else
1648 			cmd->base.duplex = DUPLEX_HALF;
1649 	} else {
1650 		speed = SPEED_UNKNOWN;
1651 		cmd->base.duplex = DUPLEX_UNKNOWN;
1652 	}
1653 	cmd->base.speed = speed;
1654 	if (hw->mac.autoneg)
1655 		cmd->base.autoneg = AUTONEG_ENABLE;
1656 	else
1657 		cmd->base.autoneg = AUTONEG_DISABLE;
1658 
1659 	/* MDI-X => 2; MDI =>1; Invalid =>0 */
1660 	if (hw->phy.media_type == igc_media_type_copper)
1661 		cmd->base.eth_tp_mdix = hw->phy.is_mdix ? ETH_TP_MDI_X :
1662 						      ETH_TP_MDI;
1663 	else
1664 		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
1665 
1666 	if (hw->phy.mdix == AUTO_ALL_MODES)
1667 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
1668 	else
1669 		cmd->base.eth_tp_mdix_ctrl = hw->phy.mdix;
1670 
1671 	return 0;
1672 }
1673 
1674 static int
1675 igc_ethtool_set_link_ksettings(struct net_device *netdev,
1676 			       const struct ethtool_link_ksettings *cmd)
1677 {
1678 	struct igc_adapter *adapter = netdev_priv(netdev);
1679 	struct net_device *dev = adapter->netdev;
1680 	struct igc_hw *hw = &adapter->hw;
1681 	u32 advertising;
1682 
1683 	/* When adapter in resetting mode, autoneg/speed/duplex
1684 	 * cannot be changed
1685 	 */
1686 	if (igc_check_reset_block(hw)) {
1687 		netdev_err(dev, "Cannot change link characteristics when reset is active\n");
1688 		return -EINVAL;
1689 	}
1690 
1691 	/* MDI setting is only allowed when autoneg enabled because
1692 	 * some hardware doesn't allow MDI setting when speed or
1693 	 * duplex is forced.
1694 	 */
1695 	if (cmd->base.eth_tp_mdix_ctrl) {
1696 		if (cmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO &&
1697 		    cmd->base.autoneg != AUTONEG_ENABLE) {
1698 			netdev_err(dev, "Forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n");
1699 			return -EINVAL;
1700 		}
1701 	}
1702 
1703 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
1704 		usleep_range(1000, 2000);
1705 
1706 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
1707 						cmd->link_modes.advertising);
1708 
1709 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1710 		hw->mac.autoneg = 1;
1711 		hw->phy.autoneg_advertised = advertising;
1712 		if (adapter->fc_autoneg)
1713 			hw->fc.requested_mode = igc_fc_default;
1714 	} else {
1715 		netdev_info(dev, "Force mode currently not supported\n");
1716 	}
1717 
1718 	/* MDI-X => 2; MDI => 1; Auto => 3 */
1719 	if (cmd->base.eth_tp_mdix_ctrl) {
1720 		/* fix up the value for auto (3 => 0) as zero is mapped
1721 		 * internally to auto
1722 		 */
1723 		if (cmd->base.eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO)
1724 			hw->phy.mdix = AUTO_ALL_MODES;
1725 		else
1726 			hw->phy.mdix = cmd->base.eth_tp_mdix_ctrl;
1727 	}
1728 
1729 	/* reset the link */
1730 	if (netif_running(adapter->netdev)) {
1731 		igc_down(adapter);
1732 		igc_up(adapter);
1733 	} else {
1734 		igc_reset(adapter);
1735 	}
1736 
1737 	clear_bit(__IGC_RESETTING, &adapter->state);
1738 
1739 	return 0;
1740 }
1741 
1742 static void igc_ethtool_diag_test(struct net_device *netdev,
1743 				  struct ethtool_test *eth_test, u64 *data)
1744 {
1745 	struct igc_adapter *adapter = netdev_priv(netdev);
1746 	bool if_running = netif_running(netdev);
1747 
1748 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1749 		netdev_info(adapter->netdev, "Offline testing starting");
1750 		set_bit(__IGC_TESTING, &adapter->state);
1751 
1752 		/* Link test performed before hardware reset so autoneg doesn't
1753 		 * interfere with test result
1754 		 */
1755 		if (!igc_link_test(adapter, &data[TEST_LINK]))
1756 			eth_test->flags |= ETH_TEST_FL_FAILED;
1757 
1758 		if (if_running)
1759 			igc_close(netdev);
1760 		else
1761 			igc_reset(adapter);
1762 
1763 		netdev_info(adapter->netdev, "Register testing starting");
1764 		if (!igc_reg_test(adapter, &data[TEST_REG]))
1765 			eth_test->flags |= ETH_TEST_FL_FAILED;
1766 
1767 		igc_reset(adapter);
1768 
1769 		netdev_info(adapter->netdev, "EEPROM testing starting");
1770 		if (!igc_eeprom_test(adapter, &data[TEST_EEP]))
1771 			eth_test->flags |= ETH_TEST_FL_FAILED;
1772 
1773 		igc_reset(adapter);
1774 
1775 		/* loopback and interrupt tests
1776 		 * will be implemented in the future
1777 		 */
1778 		data[TEST_LOOP] = 0;
1779 		data[TEST_IRQ] = 0;
1780 
1781 		clear_bit(__IGC_TESTING, &adapter->state);
1782 		if (if_running)
1783 			igc_open(netdev);
1784 	} else {
1785 		netdev_info(adapter->netdev, "Online testing starting");
1786 
1787 		/* register, eeprom, intr and loopback tests not run online */
1788 		data[TEST_REG] = 0;
1789 		data[TEST_EEP] = 0;
1790 		data[TEST_IRQ] = 0;
1791 		data[TEST_LOOP] = 0;
1792 
1793 		if (!igc_link_test(adapter, &data[TEST_LINK]))
1794 			eth_test->flags |= ETH_TEST_FL_FAILED;
1795 	}
1796 
1797 	msleep_interruptible(4 * 1000);
1798 }
1799 
1800 static const struct ethtool_ops igc_ethtool_ops = {
1801 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
1802 	.get_drvinfo		= igc_ethtool_get_drvinfo,
1803 	.get_regs_len		= igc_ethtool_get_regs_len,
1804 	.get_regs		= igc_ethtool_get_regs,
1805 	.get_wol		= igc_ethtool_get_wol,
1806 	.set_wol		= igc_ethtool_set_wol,
1807 	.get_msglevel		= igc_ethtool_get_msglevel,
1808 	.set_msglevel		= igc_ethtool_set_msglevel,
1809 	.nway_reset		= igc_ethtool_nway_reset,
1810 	.get_link		= igc_ethtool_get_link,
1811 	.get_eeprom_len		= igc_ethtool_get_eeprom_len,
1812 	.get_eeprom		= igc_ethtool_get_eeprom,
1813 	.set_eeprom		= igc_ethtool_set_eeprom,
1814 	.get_ringparam		= igc_ethtool_get_ringparam,
1815 	.set_ringparam		= igc_ethtool_set_ringparam,
1816 	.get_pauseparam		= igc_ethtool_get_pauseparam,
1817 	.set_pauseparam		= igc_ethtool_set_pauseparam,
1818 	.get_strings		= igc_ethtool_get_strings,
1819 	.get_sset_count		= igc_ethtool_get_sset_count,
1820 	.get_ethtool_stats	= igc_ethtool_get_stats,
1821 	.get_coalesce		= igc_ethtool_get_coalesce,
1822 	.set_coalesce		= igc_ethtool_set_coalesce,
1823 	.get_rxnfc		= igc_ethtool_get_rxnfc,
1824 	.set_rxnfc		= igc_ethtool_set_rxnfc,
1825 	.get_rxfh_indir_size	= igc_ethtool_get_rxfh_indir_size,
1826 	.get_rxfh		= igc_ethtool_get_rxfh,
1827 	.set_rxfh		= igc_ethtool_set_rxfh,
1828 	.get_ts_info		= igc_ethtool_get_ts_info,
1829 	.get_channels		= igc_ethtool_get_channels,
1830 	.set_channels		= igc_ethtool_set_channels,
1831 	.get_priv_flags		= igc_ethtool_get_priv_flags,
1832 	.set_priv_flags		= igc_ethtool_set_priv_flags,
1833 	.begin			= igc_ethtool_begin,
1834 	.complete		= igc_ethtool_complete,
1835 	.get_link_ksettings	= igc_ethtool_get_link_ksettings,
1836 	.set_link_ksettings	= igc_ethtool_set_link_ksettings,
1837 	.self_test		= igc_ethtool_diag_test,
1838 };
1839 
1840 void igc_ethtool_set_ops(struct net_device *netdev)
1841 {
1842 	netdev->ethtool_ops = &igc_ethtool_ops;
1843 }
1844