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