blob: 5d2e186e67106e527d3c1a98cfcf523ac69f8320 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/netdevice.h>
12#include <linux/ethtool.h>
Ben Hutchings68e7f452009-04-29 08:05:08 +000013#include <linux/mdio.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010014#include <linux/rtnetlink.h>
15#include "net_driver.h"
Ben Hutchings04cc8ca2008-12-12 21:50:46 -080016#include "workarounds.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010017#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010018#include "efx.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010019#include "falcon.h"
Ben Hutchings4a5b5042008-09-01 12:47:16 +010020#include "spi.h"
Ben Hutchings04cc8ca2008-12-12 21:50:46 -080021#include "mdio_10g.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010022
Ben Hutchings8ceee662008-04-27 12:55:59 +010023struct ethtool_string {
24 char name[ETH_GSTRING_LEN];
25};
26
27struct efx_ethtool_stat {
28 const char *name;
29 enum {
30 EFX_ETHTOOL_STAT_SOURCE_mac_stats,
31 EFX_ETHTOOL_STAT_SOURCE_nic,
32 EFX_ETHTOOL_STAT_SOURCE_channel
33 } source;
34 unsigned offset;
35 u64(*get_stat) (void *field); /* Reader function */
36};
37
38/* Initialiser for a struct #efx_ethtool_stat with type-checking */
39#define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
40 get_stat_function) { \
41 .name = #stat_name, \
42 .source = EFX_ETHTOOL_STAT_SOURCE_##source_name, \
43 .offset = ((((field_type *) 0) == \
44 &((struct efx_##source_name *)0)->field) ? \
45 offsetof(struct efx_##source_name, field) : \
46 offsetof(struct efx_##source_name, field)), \
47 .get_stat = get_stat_function, \
48}
49
50static u64 efx_get_uint_stat(void *field)
51{
52 return *(unsigned int *)field;
53}
54
55static u64 efx_get_ulong_stat(void *field)
56{
57 return *(unsigned long *)field;
58}
59
60static u64 efx_get_u64_stat(void *field)
61{
62 return *(u64 *) field;
63}
64
65static u64 efx_get_atomic_stat(void *field)
66{
67 return atomic_read((atomic_t *) field);
68}
69
70#define EFX_ETHTOOL_ULONG_MAC_STAT(field) \
71 EFX_ETHTOOL_STAT(field, mac_stats, field, \
72 unsigned long, efx_get_ulong_stat)
73
74#define EFX_ETHTOOL_U64_MAC_STAT(field) \
75 EFX_ETHTOOL_STAT(field, mac_stats, field, \
76 u64, efx_get_u64_stat)
77
78#define EFX_ETHTOOL_UINT_NIC_STAT(name) \
79 EFX_ETHTOOL_STAT(name, nic, n_##name, \
80 unsigned int, efx_get_uint_stat)
81
82#define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field) \
83 EFX_ETHTOOL_STAT(field, nic, field, \
84 atomic_t, efx_get_atomic_stat)
85
86#define EFX_ETHTOOL_UINT_CHANNEL_STAT(field) \
87 EFX_ETHTOOL_STAT(field, channel, n_##field, \
88 unsigned int, efx_get_uint_stat)
89
90static struct efx_ethtool_stat efx_ethtool_stats[] = {
91 EFX_ETHTOOL_U64_MAC_STAT(tx_bytes),
92 EFX_ETHTOOL_U64_MAC_STAT(tx_good_bytes),
93 EFX_ETHTOOL_U64_MAC_STAT(tx_bad_bytes),
94 EFX_ETHTOOL_ULONG_MAC_STAT(tx_packets),
95 EFX_ETHTOOL_ULONG_MAC_STAT(tx_bad),
96 EFX_ETHTOOL_ULONG_MAC_STAT(tx_pause),
97 EFX_ETHTOOL_ULONG_MAC_STAT(tx_control),
98 EFX_ETHTOOL_ULONG_MAC_STAT(tx_unicast),
99 EFX_ETHTOOL_ULONG_MAC_STAT(tx_multicast),
100 EFX_ETHTOOL_ULONG_MAC_STAT(tx_broadcast),
101 EFX_ETHTOOL_ULONG_MAC_STAT(tx_lt64),
102 EFX_ETHTOOL_ULONG_MAC_STAT(tx_64),
103 EFX_ETHTOOL_ULONG_MAC_STAT(tx_65_to_127),
104 EFX_ETHTOOL_ULONG_MAC_STAT(tx_128_to_255),
105 EFX_ETHTOOL_ULONG_MAC_STAT(tx_256_to_511),
106 EFX_ETHTOOL_ULONG_MAC_STAT(tx_512_to_1023),
107 EFX_ETHTOOL_ULONG_MAC_STAT(tx_1024_to_15xx),
108 EFX_ETHTOOL_ULONG_MAC_STAT(tx_15xx_to_jumbo),
109 EFX_ETHTOOL_ULONG_MAC_STAT(tx_gtjumbo),
110 EFX_ETHTOOL_ULONG_MAC_STAT(tx_collision),
111 EFX_ETHTOOL_ULONG_MAC_STAT(tx_single_collision),
112 EFX_ETHTOOL_ULONG_MAC_STAT(tx_multiple_collision),
113 EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_collision),
114 EFX_ETHTOOL_ULONG_MAC_STAT(tx_deferred),
115 EFX_ETHTOOL_ULONG_MAC_STAT(tx_late_collision),
116 EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_deferred),
117 EFX_ETHTOOL_ULONG_MAC_STAT(tx_non_tcpudp),
118 EFX_ETHTOOL_ULONG_MAC_STAT(tx_mac_src_error),
119 EFX_ETHTOOL_ULONG_MAC_STAT(tx_ip_src_error),
120 EFX_ETHTOOL_U64_MAC_STAT(rx_bytes),
121 EFX_ETHTOOL_U64_MAC_STAT(rx_good_bytes),
122 EFX_ETHTOOL_U64_MAC_STAT(rx_bad_bytes),
123 EFX_ETHTOOL_ULONG_MAC_STAT(rx_packets),
124 EFX_ETHTOOL_ULONG_MAC_STAT(rx_good),
125 EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad),
126 EFX_ETHTOOL_ULONG_MAC_STAT(rx_pause),
127 EFX_ETHTOOL_ULONG_MAC_STAT(rx_control),
128 EFX_ETHTOOL_ULONG_MAC_STAT(rx_unicast),
129 EFX_ETHTOOL_ULONG_MAC_STAT(rx_multicast),
130 EFX_ETHTOOL_ULONG_MAC_STAT(rx_broadcast),
131 EFX_ETHTOOL_ULONG_MAC_STAT(rx_lt64),
132 EFX_ETHTOOL_ULONG_MAC_STAT(rx_64),
133 EFX_ETHTOOL_ULONG_MAC_STAT(rx_65_to_127),
134 EFX_ETHTOOL_ULONG_MAC_STAT(rx_128_to_255),
135 EFX_ETHTOOL_ULONG_MAC_STAT(rx_256_to_511),
136 EFX_ETHTOOL_ULONG_MAC_STAT(rx_512_to_1023),
137 EFX_ETHTOOL_ULONG_MAC_STAT(rx_1024_to_15xx),
138 EFX_ETHTOOL_ULONG_MAC_STAT(rx_15xx_to_jumbo),
139 EFX_ETHTOOL_ULONG_MAC_STAT(rx_gtjumbo),
140 EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_lt64),
141 EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_64_to_15xx),
142 EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_15xx_to_jumbo),
143 EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_gtjumbo),
144 EFX_ETHTOOL_ULONG_MAC_STAT(rx_overflow),
145 EFX_ETHTOOL_ULONG_MAC_STAT(rx_missed),
146 EFX_ETHTOOL_ULONG_MAC_STAT(rx_false_carrier),
147 EFX_ETHTOOL_ULONG_MAC_STAT(rx_symbol_error),
148 EFX_ETHTOOL_ULONG_MAC_STAT(rx_align_error),
149 EFX_ETHTOOL_ULONG_MAC_STAT(rx_length_error),
150 EFX_ETHTOOL_ULONG_MAC_STAT(rx_internal_error),
151 EFX_ETHTOOL_UINT_NIC_STAT(rx_nodesc_drop_cnt),
152 EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
153 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
154 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
155 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
156 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
157};
158
159/* Number of ethtool statistics */
160#define EFX_ETHTOOL_NUM_STATS ARRAY_SIZE(efx_ethtool_stats)
161
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100162#define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100163
Ben Hutchings8ceee662008-04-27 12:55:59 +0100164/**************************************************************************
165 *
166 * Ethtool operations
167 *
168 **************************************************************************
169 */
170
171/* Identify device by flashing LEDs */
Ben Hutchings65f667f2008-12-12 21:32:10 -0800172static int efx_ethtool_phys_id(struct net_device *net_dev, u32 count)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100173{
Ben Hutchings767e4682008-09-01 12:43:14 +0100174 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100175
Ben Hutchings398468e2009-11-23 16:03:45 +0000176 do {
Ben Hutchings44838a42009-11-25 16:09:41 +0000177 falcon_board(efx)->type->set_id_led(efx, EFX_LED_ON);
Ben Hutchings398468e2009-11-23 16:03:45 +0000178 schedule_timeout_interruptible(HZ / 2);
179
Ben Hutchings44838a42009-11-25 16:09:41 +0000180 falcon_board(efx)->type->set_id_led(efx, EFX_LED_OFF);
Ben Hutchings398468e2009-11-23 16:03:45 +0000181 schedule_timeout_interruptible(HZ / 2);
182 } while (!signal_pending(current) && --count != 0);
183
Ben Hutchings44838a42009-11-25 16:09:41 +0000184 falcon_board(efx)->type->set_id_led(efx, EFX_LED_DEFAULT);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100185 return 0;
186}
187
188/* This must be called with rtnl_lock held. */
189int efx_ethtool_get_settings(struct net_device *net_dev,
190 struct ethtool_cmd *ecmd)
191{
Ben Hutchings767e4682008-09-01 12:43:14 +0100192 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100193
194 mutex_lock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800195 efx->phy_op->get_settings(efx, ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100196 mutex_unlock(&efx->mac_lock);
197
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800198 /* Falcon GMAC does not support 1000Mbps HD */
199 ecmd->supported &= ~SUPPORTED_1000baseT_Half;
200
201 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202}
203
204/* This must be called with rtnl_lock held. */
205int efx_ethtool_set_settings(struct net_device *net_dev,
206 struct ethtool_cmd *ecmd)
207{
Ben Hutchings767e4682008-09-01 12:43:14 +0100208 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209 int rc;
210
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800211 /* Falcon GMAC does not support 1000Mbps HD */
212 if (ecmd->speed == SPEED_1000 && ecmd->duplex != DUPLEX_FULL) {
213 EFX_LOG(efx, "rejecting unsupported 1000Mbps HD"
214 " setting\n");
215 return -EINVAL;
216 }
217
Ben Hutchings8ceee662008-04-27 12:55:59 +0100218 mutex_lock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800219 rc = efx->phy_op->set_settings(efx, ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100220 mutex_unlock(&efx->mac_lock);
221 if (!rc)
222 efx_reconfigure_port(efx);
223
224 return rc;
225}
226
227static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
228 struct ethtool_drvinfo *info)
229{
Ben Hutchings767e4682008-09-01 12:43:14 +0100230 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100231
232 strlcpy(info->driver, EFX_DRIVER_NAME, sizeof(info->driver));
233 strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
234 strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
235}
236
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100237/**
238 * efx_fill_test - fill in an individual self-test entry
239 * @test_index: Index of the test
240 * @strings: Ethtool strings, or %NULL
241 * @data: Ethtool test results, or %NULL
242 * @test: Pointer to test result (used only if data != %NULL)
Ben Hutchings740ced92008-12-12 21:41:55 -0800243 * @unit_format: Unit name format (e.g. "chan\%d")
244 * @unit_id: Unit id (e.g. 0 for "chan0")
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100245 * @test_format: Test name format (e.g. "loopback.\%s.tx.sent")
Ben Hutchings740ced92008-12-12 21:41:55 -0800246 * @test_id: Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100247 *
248 * Fill in an individual self-test entry.
249 */
250static void efx_fill_test(unsigned int test_index,
251 struct ethtool_string *strings, u64 *data,
252 int *test, const char *unit_format, int unit_id,
253 const char *test_format, const char *test_id)
254{
255 struct ethtool_string unit_str, test_str;
256
257 /* Fill data value, if applicable */
258 if (data)
259 data[test_index] = *test;
260
261 /* Fill string, if applicable */
262 if (strings) {
Ben Hutchings11e66962008-12-12 22:05:48 -0800263 if (strchr(unit_format, '%'))
264 snprintf(unit_str.name, sizeof(unit_str.name),
265 unit_format, unit_id);
266 else
267 strcpy(unit_str.name, unit_format);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100268 snprintf(test_str.name, sizeof(test_str.name),
269 test_format, test_id);
270 snprintf(strings[test_index].name,
271 sizeof(strings[test_index].name),
Ben Hutchings740ced92008-12-12 21:41:55 -0800272 "%-6s %-24s", unit_str.name, test_str.name);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100273 }
274}
275
Ben Hutchings740ced92008-12-12 21:41:55 -0800276#define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100277#define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
278#define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
279#define EFX_LOOPBACK_NAME(_mode, _counter) \
Ben Hutchingsc4593022009-11-23 16:08:17 +0000280 "loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100281
282/**
283 * efx_fill_loopback_test - fill in a block of loopback self-test entries
284 * @efx: Efx NIC
285 * @lb_tests: Efx loopback self-test results structure
286 * @mode: Loopback test mode
287 * @test_index: Starting index of the test
288 * @strings: Ethtool strings, or %NULL
289 * @data: Ethtool test results, or %NULL
290 */
291static int efx_fill_loopback_test(struct efx_nic *efx,
292 struct efx_loopback_self_tests *lb_tests,
293 enum efx_loopback_mode mode,
294 unsigned int test_index,
295 struct ethtool_string *strings, u64 *data)
296{
297 struct efx_tx_queue *tx_queue;
298
299 efx_for_each_tx_queue(tx_queue, efx) {
300 efx_fill_test(test_index++, strings, data,
301 &lb_tests->tx_sent[tx_queue->queue],
302 EFX_TX_QUEUE_NAME(tx_queue),
303 EFX_LOOPBACK_NAME(mode, "tx_sent"));
304 efx_fill_test(test_index++, strings, data,
305 &lb_tests->tx_done[tx_queue->queue],
306 EFX_TX_QUEUE_NAME(tx_queue),
307 EFX_LOOPBACK_NAME(mode, "tx_done"));
308 }
309 efx_fill_test(test_index++, strings, data,
310 &lb_tests->rx_good,
Ben Hutchings11e66962008-12-12 22:05:48 -0800311 "rx", 0,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100312 EFX_LOOPBACK_NAME(mode, "rx_good"));
313 efx_fill_test(test_index++, strings, data,
314 &lb_tests->rx_bad,
Ben Hutchings11e66962008-12-12 22:05:48 -0800315 "rx", 0,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100316 EFX_LOOPBACK_NAME(mode, "rx_bad"));
317
318 return test_index;
319}
320
321/**
322 * efx_ethtool_fill_self_tests - get self-test details
323 * @efx: Efx NIC
324 * @tests: Efx self-test results structure, or %NULL
325 * @strings: Ethtool strings, or %NULL
326 * @data: Ethtool test results, or %NULL
327 */
328static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
329 struct efx_self_tests *tests,
330 struct ethtool_string *strings,
331 u64 *data)
332{
333 struct efx_channel *channel;
Ben Hutchings17967212008-12-26 13:47:25 -0800334 unsigned int n = 0, i;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100335 enum efx_loopback_mode mode;
336
Ben Hutchings68e7f452009-04-29 08:05:08 +0000337 efx_fill_test(n++, strings, data, &tests->mdio,
338 "core", 0, "mdio", NULL);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100339 efx_fill_test(n++, strings, data, &tests->nvram,
340 "core", 0, "nvram", NULL);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100341 efx_fill_test(n++, strings, data, &tests->interrupt,
342 "core", 0, "interrupt", NULL);
343
344 /* Event queues */
345 efx_for_each_channel(channel, efx) {
346 efx_fill_test(n++, strings, data,
347 &tests->eventq_dma[channel->channel],
348 EFX_CHANNEL_NAME(channel),
349 "eventq.dma", NULL);
350 efx_fill_test(n++, strings, data,
351 &tests->eventq_int[channel->channel],
352 EFX_CHANNEL_NAME(channel),
353 "eventq.int", NULL);
354 efx_fill_test(n++, strings, data,
355 &tests->eventq_poll[channel->channel],
356 EFX_CHANNEL_NAME(channel),
357 "eventq.poll", NULL);
358 }
359
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100360 efx_fill_test(n++, strings, data, &tests->registers,
361 "core", 0, "registers", NULL);
Ben Hutchings17967212008-12-26 13:47:25 -0800362
363 for (i = 0; i < efx->phy_op->num_tests; i++)
364 efx_fill_test(n++, strings, data, &tests->phy[i],
365 "phy", 0, efx->phy_op->test_names[i], NULL);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100366
367 /* Loopback tests */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100368 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100369 if (!(efx->loopback_modes & (1 << mode)))
370 continue;
371 n = efx_fill_loopback_test(efx,
372 &tests->loopback[mode], mode, n,
373 strings, data);
374 }
375
376 return n;
377}
378
Ben Hutchings3594e132008-09-01 12:48:12 +0100379static int efx_ethtool_get_sset_count(struct net_device *net_dev,
380 int string_set)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381{
Ben Hutchings3594e132008-09-01 12:48:12 +0100382 switch (string_set) {
383 case ETH_SS_STATS:
384 return EFX_ETHTOOL_NUM_STATS;
385 case ETH_SS_TEST:
386 return efx_ethtool_fill_self_tests(netdev_priv(net_dev),
387 NULL, NULL, NULL);
388 default:
389 return -EINVAL;
390 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100391}
392
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393static void efx_ethtool_get_strings(struct net_device *net_dev,
394 u32 string_set, u8 *strings)
395{
Ben Hutchings767e4682008-09-01 12:43:14 +0100396 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100397 struct ethtool_string *ethtool_strings =
398 (struct ethtool_string *)strings;
399 int i;
400
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100401 switch (string_set) {
402 case ETH_SS_STATS:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100403 for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++)
404 strncpy(ethtool_strings[i].name,
405 efx_ethtool_stats[i].name,
406 sizeof(ethtool_strings[i].name));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100407 break;
408 case ETH_SS_TEST:
409 efx_ethtool_fill_self_tests(efx, NULL,
410 ethtool_strings, NULL);
411 break;
412 default:
413 /* No other string sets */
414 break;
415 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100416}
417
418static void efx_ethtool_get_stats(struct net_device *net_dev,
419 struct ethtool_stats *stats,
420 u64 *data)
421{
Ben Hutchings767e4682008-09-01 12:43:14 +0100422 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423 struct efx_mac_stats *mac_stats = &efx->mac_stats;
424 struct efx_ethtool_stat *stat;
425 struct efx_channel *channel;
426 int i;
427
428 EFX_BUG_ON_PARANOID(stats->n_stats != EFX_ETHTOOL_NUM_STATS);
429
430 /* Update MAC and NIC statistics */
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800431 dev_get_stats(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432
433 /* Fill detailed statistics buffer */
434 for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++) {
435 stat = &efx_ethtool_stats[i];
436 switch (stat->source) {
437 case EFX_ETHTOOL_STAT_SOURCE_mac_stats:
438 data[i] = stat->get_stat((void *)mac_stats +
439 stat->offset);
440 break;
441 case EFX_ETHTOOL_STAT_SOURCE_nic:
442 data[i] = stat->get_stat((void *)efx + stat->offset);
443 break;
444 case EFX_ETHTOOL_STAT_SOURCE_channel:
445 data[i] = 0;
446 efx_for_each_channel(channel, efx)
447 data[i] += stat->get_stat((void *)channel +
448 stat->offset);
449 break;
450 }
451 }
452}
453
Ben Hutchings8ceee662008-04-27 12:55:59 +0100454static int efx_ethtool_set_rx_csum(struct net_device *net_dev, u32 enable)
455{
Ben Hutchings767e4682008-09-01 12:43:14 +0100456 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100457
458 /* No way to stop the hardware doing the checks; we just
459 * ignore the result.
460 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100461 efx->rx_checksum_enabled = !!enable;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462
463 return 0;
464}
465
466static u32 efx_ethtool_get_rx_csum(struct net_device *net_dev)
467{
Ben Hutchings767e4682008-09-01 12:43:14 +0100468 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100469
470 return efx->rx_checksum_enabled;
471}
472
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100473static void efx_ethtool_self_test(struct net_device *net_dev,
474 struct ethtool_test *test, u64 *data)
475{
Ben Hutchings767e4682008-09-01 12:43:14 +0100476 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100477 struct efx_self_tests efx_tests;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800478 int already_up;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100479 int rc;
480
481 ASSERT_RTNL();
482 if (efx->state != STATE_RUNNING) {
483 rc = -EIO;
484 goto fail1;
485 }
486
487 /* We need rx buffers and interrupts. */
488 already_up = (efx->net_dev->flags & IFF_UP);
489 if (!already_up) {
490 rc = dev_open(efx->net_dev);
491 if (rc) {
492 EFX_ERR(efx, "failed opening device.\n");
493 goto fail2;
494 }
495 }
496
497 memset(&efx_tests, 0, sizeof(efx_tests));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100498
Ben Hutchings2ef30682008-12-26 13:47:04 -0800499 rc = efx_selftest(efx, &efx_tests, test->flags);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100500
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100501 if (!already_up)
502 dev_close(efx->net_dev);
503
Ben Hutchings2ef30682008-12-26 13:47:04 -0800504 EFX_LOG(efx, "%s %sline self-tests\n",
505 rc == 0 ? "passed" : "failed",
506 (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100507
508 fail2:
509 fail1:
510 /* Fill ethtool results structures */
511 efx_ethtool_fill_self_tests(efx, &efx_tests, NULL, data);
512 if (rc)
513 test->flags |= ETH_TEST_FL_FAILED;
514}
515
Ben Hutchings8ceee662008-04-27 12:55:59 +0100516/* Restart autonegotiation */
517static int efx_ethtool_nway_reset(struct net_device *net_dev)
518{
Ben Hutchings767e4682008-09-01 12:43:14 +0100519 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100520
Ben Hutchings68e7f452009-04-29 08:05:08 +0000521 return mdio45_nway_restart(&efx->mdio);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100522}
523
524static u32 efx_ethtool_get_link(struct net_device *net_dev)
525{
Ben Hutchings767e4682008-09-01 12:43:14 +0100526 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100527
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000528 return efx->link_state.up;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100529}
530
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100531static int efx_ethtool_get_eeprom_len(struct net_device *net_dev)
532{
533 struct efx_nic *efx = netdev_priv(net_dev);
534 struct efx_spi_device *spi = efx->spi_eeprom;
535
536 if (!spi)
537 return 0;
Ben Hutchings0a95f562008-11-04 20:33:11 +0000538 return min(spi->size, EFX_EEPROM_BOOTCONFIG_END) -
539 min(spi->size, EFX_EEPROM_BOOTCONFIG_START);
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100540}
541
542static int efx_ethtool_get_eeprom(struct net_device *net_dev,
543 struct ethtool_eeprom *eeprom, u8 *buf)
544{
545 struct efx_nic *efx = netdev_priv(net_dev);
546 struct efx_spi_device *spi = efx->spi_eeprom;
547 size_t len;
548 int rc;
549
Ben Hutchingsca54a9f2008-12-12 22:06:24 -0800550 rc = mutex_lock_interruptible(&efx->spi_lock);
551 if (rc)
552 return rc;
Ben Hutchings0a95f562008-11-04 20:33:11 +0000553 rc = falcon_spi_read(spi, eeprom->offset + EFX_EEPROM_BOOTCONFIG_START,
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100554 eeprom->len, &len, buf);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000555 mutex_unlock(&efx->spi_lock);
Ben Hutchingsca54a9f2008-12-12 22:06:24 -0800556
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100557 eeprom->magic = EFX_ETHTOOL_EEPROM_MAGIC;
558 eeprom->len = len;
559 return rc;
560}
561
562static int efx_ethtool_set_eeprom(struct net_device *net_dev,
563 struct ethtool_eeprom *eeprom, u8 *buf)
564{
565 struct efx_nic *efx = netdev_priv(net_dev);
566 struct efx_spi_device *spi = efx->spi_eeprom;
567 size_t len;
568 int rc;
569
570 if (eeprom->magic != EFX_ETHTOOL_EEPROM_MAGIC)
571 return -EINVAL;
572
Ben Hutchingsca54a9f2008-12-12 22:06:24 -0800573 rc = mutex_lock_interruptible(&efx->spi_lock);
574 if (rc)
575 return rc;
Ben Hutchings0a95f562008-11-04 20:33:11 +0000576 rc = falcon_spi_write(spi, eeprom->offset + EFX_EEPROM_BOOTCONFIG_START,
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100577 eeprom->len, &len, buf);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000578 mutex_unlock(&efx->spi_lock);
Ben Hutchingsca54a9f2008-12-12 22:06:24 -0800579
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100580 eeprom->len = len;
581 return rc;
582}
583
Ben Hutchings8ceee662008-04-27 12:55:59 +0100584static int efx_ethtool_get_coalesce(struct net_device *net_dev,
585 struct ethtool_coalesce *coalesce)
586{
Ben Hutchings767e4682008-09-01 12:43:14 +0100587 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100588 struct efx_tx_queue *tx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100589 struct efx_channel *channel;
590
591 memset(coalesce, 0, sizeof(*coalesce));
592
593 /* Find lowest IRQ moderation across all used TX queues */
594 coalesce->tx_coalesce_usecs_irq = ~((u32) 0);
595 efx_for_each_tx_queue(tx_queue, efx) {
596 channel = tx_queue->channel;
597 if (channel->irq_moderation < coalesce->tx_coalesce_usecs_irq) {
598 if (channel->used_flags != EFX_USED_BY_RX_TX)
599 coalesce->tx_coalesce_usecs_irq =
600 channel->irq_moderation;
601 else
602 coalesce->tx_coalesce_usecs_irq = 0;
603 }
604 }
605
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000606 coalesce->use_adaptive_rx_coalesce = efx->irq_rx_adaptive;
607 coalesce->rx_coalesce_usecs_irq = efx->irq_rx_moderation;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100608
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000609 coalesce->tx_coalesce_usecs_irq *= FALCON_IRQ_MOD_RESOLUTION;
610 coalesce->rx_coalesce_usecs_irq *= FALCON_IRQ_MOD_RESOLUTION;
611
Ben Hutchings8ceee662008-04-27 12:55:59 +0100612 return 0;
613}
614
615/* Set coalescing parameters
616 * The difficulties occur for shared channels
617 */
618static int efx_ethtool_set_coalesce(struct net_device *net_dev,
619 struct ethtool_coalesce *coalesce)
620{
Ben Hutchings767e4682008-09-01 12:43:14 +0100621 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622 struct efx_channel *channel;
623 struct efx_tx_queue *tx_queue;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000624 unsigned tx_usecs, rx_usecs, adaptive;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000626 if (coalesce->use_adaptive_tx_coalesce)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100627 return -EOPNOTSUPP;
628
629 if (coalesce->rx_coalesce_usecs || coalesce->tx_coalesce_usecs) {
630 EFX_ERR(efx, "invalid coalescing setting. "
631 "Only rx/tx_coalesce_usecs_irq are supported\n");
632 return -EOPNOTSUPP;
633 }
634
635 rx_usecs = coalesce->rx_coalesce_usecs_irq;
636 tx_usecs = coalesce->tx_coalesce_usecs_irq;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000637 adaptive = coalesce->use_adaptive_rx_coalesce;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100638
639 /* If the channel is shared only allow RX parameters to be set */
640 efx_for_each_tx_queue(tx_queue, efx) {
641 if ((tx_queue->channel->used_flags == EFX_USED_BY_RX_TX) &&
642 tx_usecs) {
643 EFX_ERR(efx, "Channel is shared. "
644 "Only RX coalescing may be set\n");
645 return -EOPNOTSUPP;
646 }
647 }
648
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000649 efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100650 efx_for_each_channel(channel, efx)
651 falcon_set_int_moderation(channel);
652
653 return 0;
654}
655
656static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
657 struct ethtool_pauseparam *pause)
658{
Ben Hutchings767e4682008-09-01 12:43:14 +0100659 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800660 enum efx_fc_type wanted_fc;
661 bool reset;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800663 wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
664 (pause->tx_pause ? EFX_FC_TX : 0) |
665 (pause->autoneg ? EFX_FC_AUTO : 0));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100666
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800667 if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
668 EFX_LOG(efx, "Flow control unsupported: tx ON rx OFF\n");
669 return -EINVAL;
670 }
671
Ben Hutchings68e7f452009-04-29 08:05:08 +0000672 if (!(efx->phy_op->mmds & MDIO_DEVS_AN) &&
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800673 (wanted_fc & EFX_FC_AUTO)) {
674 EFX_LOG(efx, "PHY does not support flow control "
675 "autonegotiation\n");
676 return -EINVAL;
677 }
678
679 /* TX flow control may automatically turn itself off if the
680 * link partner (intermittently) stops responding to pause
681 * frames. There isn't any indication that this has happened,
682 * so the best we do is leave it up to the user to spot this
683 * and fix it be cycling transmit flow control on this end. */
684 reset = (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX);
685 if (EFX_WORKAROUND_11482(efx) && reset) {
Ben Hutchingsdaeda632009-11-28 05:36:04 +0000686 if (efx_nic_rev(efx) == EFX_REV_FALCON_B0) {
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800687 /* Recover by resetting the EM block */
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000688 if (efx->link_state.up)
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800689 falcon_drain_tx_fifo(efx);
690 } else {
691 /* Schedule a reset to recover */
692 efx_schedule_reset(efx, RESET_TYPE_INVISIBLE);
693 }
694 }
695
696 /* Try to push the pause parameters */
697 mutex_lock(&efx->mac_lock);
698
699 efx->wanted_fc = wanted_fc;
Ben Hutchings3f926da2009-04-29 08:20:37 +0000700 if (efx->phy_op->mmds & MDIO_DEVS_AN)
701 mdio45_ethtool_spauseparam_an(&efx->mdio, pause);
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800702 __efx_reconfigure_port(efx);
703
704 mutex_unlock(&efx->mac_lock);
705
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800706 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707}
708
709static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
710 struct ethtool_pauseparam *pause)
711{
Ben Hutchings767e4682008-09-01 12:43:14 +0100712 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800714 pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
715 pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
716 pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717}
718
719
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700720const struct ethtool_ops efx_ethtool_ops = {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721 .get_settings = efx_ethtool_get_settings,
722 .set_settings = efx_ethtool_set_settings,
723 .get_drvinfo = efx_ethtool_get_drvinfo,
724 .nway_reset = efx_ethtool_nway_reset,
725 .get_link = efx_ethtool_get_link,
Ben Hutchings4a5b5042008-09-01 12:47:16 +0100726 .get_eeprom_len = efx_ethtool_get_eeprom_len,
727 .get_eeprom = efx_ethtool_get_eeprom,
728 .set_eeprom = efx_ethtool_set_eeprom,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100729 .get_coalesce = efx_ethtool_get_coalesce,
730 .set_coalesce = efx_ethtool_set_coalesce,
731 .get_pauseparam = efx_ethtool_get_pauseparam,
732 .set_pauseparam = efx_ethtool_set_pauseparam,
733 .get_rx_csum = efx_ethtool_get_rx_csum,
734 .set_rx_csum = efx_ethtool_set_rx_csum,
735 .get_tx_csum = ethtool_op_get_tx_csum,
Ben Hutchings60ac1062008-09-01 12:44:59 +0100736 .set_tx_csum = ethtool_op_set_tx_csum,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737 .get_sg = ethtool_op_get_sg,
738 .set_sg = ethtool_op_set_sg,
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100739 .get_tso = ethtool_op_get_tso,
Ben Hutchings60ac1062008-09-01 12:44:59 +0100740 .set_tso = ethtool_op_set_tso,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741 .get_flags = ethtool_op_get_flags,
742 .set_flags = ethtool_op_set_flags,
Ben Hutchings3594e132008-09-01 12:48:12 +0100743 .get_sset_count = efx_ethtool_get_sset_count,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100744 .self_test = efx_ethtool_self_test,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100745 .get_strings = efx_ethtool_get_strings,
746 .phys_id = efx_ethtool_phys_id,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100747 .get_ethtool_stats = efx_ethtool_get_stats,
748};