ethtool.c 24.7 KB
Newer Older
1 2 3
/****************************************************************************
 * Driver for Solarflare Solarstorm network controllers and boards
 * Copyright 2005-2006 Fen Systems Ltd.
4
 * Copyright 2006-2009 Solarflare Communications Inc.
5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, incorporated herein by reference.
 */

#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/rtnetlink.h>
#include "net_driver.h"
B
Ben Hutchings 已提交
15
#include "workarounds.h"
16
#include "selftest.h"
17
#include "efx.h"
B
Ben Hutchings 已提交
18
#include "nic.h"
19
#include "spi.h"
B
Ben Hutchings 已提交
20
#include "mdio_10g.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

struct ethtool_string {
	char name[ETH_GSTRING_LEN];
};

struct efx_ethtool_stat {
	const char *name;
	enum {
		EFX_ETHTOOL_STAT_SOURCE_mac_stats,
		EFX_ETHTOOL_STAT_SOURCE_nic,
		EFX_ETHTOOL_STAT_SOURCE_channel
	} source;
	unsigned offset;
	u64(*get_stat) (void *field); /* Reader function */
};

/* Initialiser for a struct #efx_ethtool_stat with type-checking */
#define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
				get_stat_function) {			\
	.name = #stat_name,						\
	.source = EFX_ETHTOOL_STAT_SOURCE_##source_name,		\
	.offset = ((((field_type *) 0) ==				\
		      &((struct efx_##source_name *)0)->field) ?	\
		    offsetof(struct efx_##source_name, field) :		\
		    offsetof(struct efx_##source_name, field)),		\
	.get_stat = get_stat_function,					\
}

static u64 efx_get_uint_stat(void *field)
{
	return *(unsigned int *)field;
}

static u64 efx_get_ulong_stat(void *field)
{
	return *(unsigned long *)field;
}

static u64 efx_get_u64_stat(void *field)
{
	return *(u64 *) field;
}

static u64 efx_get_atomic_stat(void *field)
{
	return atomic_read((atomic_t *) field);
}

#define EFX_ETHTOOL_ULONG_MAC_STAT(field)			\
	EFX_ETHTOOL_STAT(field, mac_stats, field, 		\
			  unsigned long, efx_get_ulong_stat)

#define EFX_ETHTOOL_U64_MAC_STAT(field)				\
	EFX_ETHTOOL_STAT(field, mac_stats, field, 		\
			  u64, efx_get_u64_stat)

#define EFX_ETHTOOL_UINT_NIC_STAT(name)				\
	EFX_ETHTOOL_STAT(name, nic, n_##name,			\
			 unsigned int, efx_get_uint_stat)

#define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)		\
	EFX_ETHTOOL_STAT(field, nic, field,			\
			 atomic_t, efx_get_atomic_stat)

#define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)			\
	EFX_ETHTOOL_STAT(field, channel, n_##field,		\
			 unsigned int, efx_get_uint_stat)

static struct efx_ethtool_stat efx_ethtool_stats[] = {
	EFX_ETHTOOL_U64_MAC_STAT(tx_bytes),
	EFX_ETHTOOL_U64_MAC_STAT(tx_good_bytes),
	EFX_ETHTOOL_U64_MAC_STAT(tx_bad_bytes),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_packets),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_bad),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_pause),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_control),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_unicast),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_multicast),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_broadcast),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_lt64),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_64),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_65_to_127),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_128_to_255),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_256_to_511),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_512_to_1023),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_1024_to_15xx),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_15xx_to_jumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_gtjumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_collision),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_single_collision),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_multiple_collision),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_collision),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_deferred),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_late_collision),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_deferred),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_non_tcpudp),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_mac_src_error),
	EFX_ETHTOOL_ULONG_MAC_STAT(tx_ip_src_error),
	EFX_ETHTOOL_U64_MAC_STAT(rx_bytes),
	EFX_ETHTOOL_U64_MAC_STAT(rx_good_bytes),
	EFX_ETHTOOL_U64_MAC_STAT(rx_bad_bytes),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_packets),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_good),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_pause),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_control),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_unicast),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_multicast),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_broadcast),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_lt64),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_64),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_65_to_127),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_128_to_255),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_256_to_511),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_512_to_1023),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_1024_to_15xx),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_15xx_to_jumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_gtjumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_lt64),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_64_to_15xx),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_15xx_to_jumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_gtjumbo),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_overflow),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_missed),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_false_carrier),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_symbol_error),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_align_error),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_length_error),
	EFX_ETHTOOL_ULONG_MAC_STAT(rx_internal_error),
	EFX_ETHTOOL_UINT_NIC_STAT(rx_nodesc_drop_cnt),
	EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
B
Ben Hutchings 已提交
155
	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
156 157 158 159 160 161
	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
};

/* Number of ethtool statistics */
#define EFX_ETHTOOL_NUM_STATS ARRAY_SIZE(efx_ethtool_stats)

162 163
#define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB

164 165 166 167 168 169 170 171
/**************************************************************************
 *
 * Ethtool operations
 *
 **************************************************************************
 */

/* Identify device by flashing LEDs */
172
static int efx_ethtool_phys_id(struct net_device *net_dev, u32 count)
173
{
174
	struct efx_nic *efx = netdev_priv(net_dev);
175

176
	do {
177
		efx->type->set_id_led(efx, EFX_LED_ON);
178 179
		schedule_timeout_interruptible(HZ / 2);

180
		efx->type->set_id_led(efx, EFX_LED_OFF);
181 182 183
		schedule_timeout_interruptible(HZ / 2);
	} while (!signal_pending(current) && --count != 0);

184
	efx->type->set_id_led(efx, EFX_LED_DEFAULT);
185 186 187 188 189 190 191
	return 0;
}

/* This must be called with rtnl_lock held. */
int efx_ethtool_get_settings(struct net_device *net_dev,
			     struct ethtool_cmd *ecmd)
{
192
	struct efx_nic *efx = netdev_priv(net_dev);
B
Ben Hutchings 已提交
193
	struct efx_link_state *link_state = &efx->link_state;
194 195

	mutex_lock(&efx->mac_lock);
196
	efx->phy_op->get_settings(efx, ecmd);
197 198
	mutex_unlock(&efx->mac_lock);

199
	/* GMAC does not support 1000Mbps HD */
200
	ecmd->supported &= ~SUPPORTED_1000baseT_Half;
B
Ben Hutchings 已提交
201 202 203 204 205 206 207
	/* Both MACs support pause frames (bidirectional and respond-only) */
	ecmd->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;

	if (LOOPBACK_INTERNAL(efx)) {
		ecmd->speed = link_state->speed;
		ecmd->duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
	}
208 209

	return 0;
210 211 212 213 214 215
}

/* This must be called with rtnl_lock held. */
int efx_ethtool_set_settings(struct net_device *net_dev,
			     struct ethtool_cmd *ecmd)
{
216
	struct efx_nic *efx = netdev_priv(net_dev);
217 218
	int rc;

219
	/* GMAC does not support 1000Mbps HD */
220 221 222 223 224 225
	if (ecmd->speed == SPEED_1000 && ecmd->duplex != DUPLEX_FULL) {
		EFX_LOG(efx, "rejecting unsupported 1000Mbps HD"
			" setting\n");
		return -EINVAL;
	}

226
	mutex_lock(&efx->mac_lock);
227
	rc = efx->phy_op->set_settings(efx, ecmd);
228 229 230 231 232 233 234
	mutex_unlock(&efx->mac_lock);
	return rc;
}

static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
				    struct ethtool_drvinfo *info)
{
235
	struct efx_nic *efx = netdev_priv(net_dev);
236 237 238

	strlcpy(info->driver, EFX_DRIVER_NAME, sizeof(info->driver));
	strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
239 240 241
	if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
		siena_print_fwver(efx, info->fw_version,
				  sizeof(info->fw_version));
242 243 244
	strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
}

245 246 247 248 249 250
/**
 * efx_fill_test - fill in an individual self-test entry
 * @test_index:		Index of the test
 * @strings:		Ethtool strings, or %NULL
 * @data:		Ethtool test results, or %NULL
 * @test:		Pointer to test result (used only if data != %NULL)
251 252
 * @unit_format:	Unit name format (e.g. "chan\%d")
 * @unit_id:		Unit id (e.g. 0 for "chan0")
253
 * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
254
 * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
 *
 * Fill in an individual self-test entry.
 */
static void efx_fill_test(unsigned int test_index,
			  struct ethtool_string *strings, u64 *data,
			  int *test, const char *unit_format, int unit_id,
			  const char *test_format, const char *test_id)
{
	struct ethtool_string unit_str, test_str;

	/* Fill data value, if applicable */
	if (data)
		data[test_index] = *test;

	/* Fill string, if applicable */
	if (strings) {
271 272 273 274 275
		if (strchr(unit_format, '%'))
			snprintf(unit_str.name, sizeof(unit_str.name),
				 unit_format, unit_id);
		else
			strcpy(unit_str.name, unit_format);
276 277 278 279
		snprintf(test_str.name, sizeof(test_str.name),
			 test_format, test_id);
		snprintf(strings[test_index].name,
			 sizeof(strings[test_index].name),
280
			 "%-6s %-24s", unit_str.name, test_str.name);
281 282 283
	}
}

284
#define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
285 286 287
#define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
#define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
#define EFX_LOOPBACK_NAME(_mode, _counter)			\
288
	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

/**
 * efx_fill_loopback_test - fill in a block of loopback self-test entries
 * @efx:		Efx NIC
 * @lb_tests:		Efx loopback self-test results structure
 * @mode:		Loopback test mode
 * @test_index:		Starting index of the test
 * @strings:		Ethtool strings, or %NULL
 * @data:		Ethtool test results, or %NULL
 */
static int efx_fill_loopback_test(struct efx_nic *efx,
				  struct efx_loopback_self_tests *lb_tests,
				  enum efx_loopback_mode mode,
				  unsigned int test_index,
				  struct ethtool_string *strings, u64 *data)
{
	struct efx_tx_queue *tx_queue;

307
	efx_for_each_channel_tx_queue(tx_queue, &efx->channel[0]) {
308 309 310 311 312 313 314 315 316 317 318
		efx_fill_test(test_index++, strings, data,
			      &lb_tests->tx_sent[tx_queue->queue],
			      EFX_TX_QUEUE_NAME(tx_queue),
			      EFX_LOOPBACK_NAME(mode, "tx_sent"));
		efx_fill_test(test_index++, strings, data,
			      &lb_tests->tx_done[tx_queue->queue],
			      EFX_TX_QUEUE_NAME(tx_queue),
			      EFX_LOOPBACK_NAME(mode, "tx_done"));
	}
	efx_fill_test(test_index++, strings, data,
		      &lb_tests->rx_good,
319
		      "rx", 0,
320 321 322
		      EFX_LOOPBACK_NAME(mode, "rx_good"));
	efx_fill_test(test_index++, strings, data,
		      &lb_tests->rx_bad,
323
		      "rx", 0,
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
		      EFX_LOOPBACK_NAME(mode, "rx_bad"));

	return test_index;
}

/**
 * efx_ethtool_fill_self_tests - get self-test details
 * @efx:		Efx NIC
 * @tests:		Efx self-test results structure, or %NULL
 * @strings:		Ethtool strings, or %NULL
 * @data:		Ethtool test results, or %NULL
 */
static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
				       struct efx_self_tests *tests,
				       struct ethtool_string *strings,
				       u64 *data)
{
	struct efx_channel *channel;
342
	unsigned int n = 0, i;
343 344
	enum efx_loopback_mode mode;

345 346
	efx_fill_test(n++, strings, data, &tests->phy_alive,
		      "phy", 0, "alive", NULL);
B
Ben Hutchings 已提交
347 348
	efx_fill_test(n++, strings, data, &tests->nvram,
		      "core", 0, "nvram", NULL);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	efx_fill_test(n++, strings, data, &tests->interrupt,
		      "core", 0, "interrupt", NULL);

	/* Event queues */
	efx_for_each_channel(channel, efx) {
		efx_fill_test(n++, strings, data,
			      &tests->eventq_dma[channel->channel],
			      EFX_CHANNEL_NAME(channel),
			      "eventq.dma", NULL);
		efx_fill_test(n++, strings, data,
			      &tests->eventq_int[channel->channel],
			      EFX_CHANNEL_NAME(channel),
			      "eventq.int", NULL);
		efx_fill_test(n++, strings, data,
			      &tests->eventq_poll[channel->channel],
			      EFX_CHANNEL_NAME(channel),
			      "eventq.poll", NULL);
	}

B
Ben Hutchings 已提交
368 369
	efx_fill_test(n++, strings, data, &tests->registers,
		      "core", 0, "registers", NULL);
370

371 372 373 374 375 376 377 378 379 380 381
	if (efx->phy_op->run_tests != NULL) {
		EFX_BUG_ON_PARANOID(efx->phy_op->test_name == NULL);

		for (i = 0; true; ++i) {
			const char *name;

			EFX_BUG_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
			name = efx->phy_op->test_name(efx, i);
			if (name == NULL)
				break;

382
			efx_fill_test(n++, strings, data, &tests->phy_ext[i],
383 384 385
				      "phy", 0, name, NULL);
		}
	}
386 387

	/* Loopback tests */
B
Ben Hutchings 已提交
388
	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
389 390 391 392 393 394 395 396 397 398
		if (!(efx->loopback_modes & (1 << mode)))
			continue;
		n = efx_fill_loopback_test(efx,
					   &tests->loopback[mode], mode, n,
					   strings, data);
	}

	return n;
}

399 400
static int efx_ethtool_get_sset_count(struct net_device *net_dev,
				      int string_set)
401
{
402 403 404 405 406 407 408 409 410
	switch (string_set) {
	case ETH_SS_STATS:
		return EFX_ETHTOOL_NUM_STATS;
	case ETH_SS_TEST:
		return efx_ethtool_fill_self_tests(netdev_priv(net_dev),
						   NULL, NULL, NULL);
	default:
		return -EINVAL;
	}
411 412
}

413 414 415
static void efx_ethtool_get_strings(struct net_device *net_dev,
				    u32 string_set, u8 *strings)
{
416
	struct efx_nic *efx = netdev_priv(net_dev);
417 418 419 420
	struct ethtool_string *ethtool_strings =
		(struct ethtool_string *)strings;
	int i;

421 422
	switch (string_set) {
	case ETH_SS_STATS:
423 424 425 426
		for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++)
			strncpy(ethtool_strings[i].name,
				efx_ethtool_stats[i].name,
				sizeof(ethtool_strings[i].name));
427 428 429 430 431 432 433 434 435
		break;
	case ETH_SS_TEST:
		efx_ethtool_fill_self_tests(efx, NULL,
					    ethtool_strings, NULL);
		break;
	default:
		/* No other string sets */
		break;
	}
436 437 438 439 440 441
}

static void efx_ethtool_get_stats(struct net_device *net_dev,
				  struct ethtool_stats *stats,
				  u64 *data)
{
442
	struct efx_nic *efx = netdev_priv(net_dev);
443 444 445 446 447 448 449 450
	struct efx_mac_stats *mac_stats = &efx->mac_stats;
	struct efx_ethtool_stat *stat;
	struct efx_channel *channel;
	int i;

	EFX_BUG_ON_PARANOID(stats->n_stats != EFX_ETHTOOL_NUM_STATS);

	/* Update MAC and NIC statistics */
451
	dev_get_stats(net_dev);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	/* Fill detailed statistics buffer */
	for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++) {
		stat = &efx_ethtool_stats[i];
		switch (stat->source) {
		case EFX_ETHTOOL_STAT_SOURCE_mac_stats:
			data[i] = stat->get_stat((void *)mac_stats +
						 stat->offset);
			break;
		case EFX_ETHTOOL_STAT_SOURCE_nic:
			data[i] = stat->get_stat((void *)efx + stat->offset);
			break;
		case EFX_ETHTOOL_STAT_SOURCE_channel:
			data[i] = 0;
			efx_for_each_channel(channel, efx)
				data[i] += stat->get_stat((void *)channel +
							  stat->offset);
			break;
		}
	}
}

B
Ben Hutchings 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
static int efx_ethtool_set_tso(struct net_device *net_dev, u32 enable)
{
	struct efx_nic *efx __attribute__ ((unused)) = netdev_priv(net_dev);
	unsigned long features;

	features = NETIF_F_TSO;
	if (efx->type->offload_features & NETIF_F_V6_CSUM)
		features |= NETIF_F_TSO6;

	if (enable)
		net_dev->features |= features;
	else
		net_dev->features &= ~features;

	return 0;
}

491 492 493 494 495 496 497 498 499 500 501 502 503
static int efx_ethtool_set_tx_csum(struct net_device *net_dev, u32 enable)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	unsigned long features = efx->type->offload_features & NETIF_F_ALL_CSUM;

	if (enable)
		net_dev->features |= features;
	else
		net_dev->features &= ~features;

	return 0;
}

504 505
static int efx_ethtool_set_rx_csum(struct net_device *net_dev, u32 enable)
{
506
	struct efx_nic *efx = netdev_priv(net_dev);
507 508 509 510

	/* No way to stop the hardware doing the checks; we just
	 * ignore the result.
	 */
511
	efx->rx_checksum_enabled = !!enable;
512 513 514 515 516 517

	return 0;
}

static u32 efx_ethtool_get_rx_csum(struct net_device *net_dev)
{
518
	struct efx_nic *efx = netdev_priv(net_dev);
519 520 521 522

	return efx->rx_checksum_enabled;
}

523 524 525
static void efx_ethtool_self_test(struct net_device *net_dev,
				  struct ethtool_test *test, u64 *data)
{
526
	struct efx_nic *efx = netdev_priv(net_dev);
527
	struct efx_self_tests efx_tests;
528
	int already_up;
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
	int rc;

	ASSERT_RTNL();
	if (efx->state != STATE_RUNNING) {
		rc = -EIO;
		goto fail1;
	}

	/* We need rx buffers and interrupts. */
	already_up = (efx->net_dev->flags & IFF_UP);
	if (!already_up) {
		rc = dev_open(efx->net_dev);
		if (rc) {
			EFX_ERR(efx, "failed opening device.\n");
			goto fail2;
		}
	}

	memset(&efx_tests, 0, sizeof(efx_tests));

549
	rc = efx_selftest(efx, &efx_tests, test->flags);
550 551 552 553

	if (!already_up)
		dev_close(efx->net_dev);

554 555 556
	EFX_LOG(efx, "%s %sline self-tests\n",
		rc == 0 ? "passed" : "failed",
		(test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
557 558 559 560 561 562 563 564 565

 fail2:
 fail1:
	/* Fill ethtool results structures */
	efx_ethtool_fill_self_tests(efx, &efx_tests, NULL, data);
	if (rc)
		test->flags |= ETH_TEST_FL_FAILED;
}

566 567 568
/* Restart autonegotiation */
static int efx_ethtool_nway_reset(struct net_device *net_dev)
{
569
	struct efx_nic *efx = netdev_priv(net_dev);
570

571
	return mdio45_nway_restart(&efx->mdio);
572 573 574 575
}

static u32 efx_ethtool_get_link(struct net_device *net_dev)
{
576
	struct efx_nic *efx = netdev_priv(net_dev);
577

578
	return efx->link_state.up;
579 580
}

581 582 583 584 585 586 587
static int efx_ethtool_get_eeprom_len(struct net_device *net_dev)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	struct efx_spi_device *spi = efx->spi_eeprom;

	if (!spi)
		return 0;
588 589
	return min(spi->size, EFX_EEPROM_BOOTCONFIG_END) -
		min(spi->size, EFX_EEPROM_BOOTCONFIG_START);
590 591 592 593 594 595 596 597 598 599
}

static int efx_ethtool_get_eeprom(struct net_device *net_dev,
				  struct ethtool_eeprom *eeprom, u8 *buf)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	struct efx_spi_device *spi = efx->spi_eeprom;
	size_t len;
	int rc;

600 601 602
	rc = mutex_lock_interruptible(&efx->spi_lock);
	if (rc)
		return rc;
603 604
	rc = falcon_spi_read(efx, spi,
			     eeprom->offset + EFX_EEPROM_BOOTCONFIG_START,
605
			     eeprom->len, &len, buf);
606
	mutex_unlock(&efx->spi_lock);
607

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	eeprom->magic = EFX_ETHTOOL_EEPROM_MAGIC;
	eeprom->len = len;
	return rc;
}

static int efx_ethtool_set_eeprom(struct net_device *net_dev,
				  struct ethtool_eeprom *eeprom, u8 *buf)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	struct efx_spi_device *spi = efx->spi_eeprom;
	size_t len;
	int rc;

	if (eeprom->magic != EFX_ETHTOOL_EEPROM_MAGIC)
		return -EINVAL;

624 625 626
	rc = mutex_lock_interruptible(&efx->spi_lock);
	if (rc)
		return rc;
627 628
	rc = falcon_spi_write(efx, spi,
			      eeprom->offset + EFX_EEPROM_BOOTCONFIG_START,
629
			      eeprom->len, &len, buf);
630
	mutex_unlock(&efx->spi_lock);
631

632 633 634 635
	eeprom->len = len;
	return rc;
}

636 637 638
static int efx_ethtool_get_coalesce(struct net_device *net_dev,
				    struct ethtool_coalesce *coalesce)
{
639
	struct efx_nic *efx = netdev_priv(net_dev);
640 641 642 643 644 645 646 647 648 649
	struct efx_tx_queue *tx_queue;
	struct efx_channel *channel;

	memset(coalesce, 0, sizeof(*coalesce));

	/* Find lowest IRQ moderation across all used TX queues */
	coalesce->tx_coalesce_usecs_irq = ~((u32) 0);
	efx_for_each_tx_queue(tx_queue, efx) {
		channel = tx_queue->channel;
		if (channel->irq_moderation < coalesce->tx_coalesce_usecs_irq) {
B
Ben Hutchings 已提交
650
			if (channel->channel < efx->n_rx_channels)
651 652 653 654 655 656 657
				coalesce->tx_coalesce_usecs_irq =
					channel->irq_moderation;
			else
				coalesce->tx_coalesce_usecs_irq = 0;
		}
	}

658 659
	coalesce->use_adaptive_rx_coalesce = efx->irq_rx_adaptive;
	coalesce->rx_coalesce_usecs_irq = efx->irq_rx_moderation;
660

661 662
	coalesce->tx_coalesce_usecs_irq *= EFX_IRQ_MOD_RESOLUTION;
	coalesce->rx_coalesce_usecs_irq *= EFX_IRQ_MOD_RESOLUTION;
663

664 665 666 667 668 669 670 671 672
	return 0;
}

/* Set coalescing parameters
 * The difficulties occur for shared channels
 */
static int efx_ethtool_set_coalesce(struct net_device *net_dev,
				    struct ethtool_coalesce *coalesce)
{
673
	struct efx_nic *efx = netdev_priv(net_dev);
674 675
	struct efx_channel *channel;
	struct efx_tx_queue *tx_queue;
676
	unsigned tx_usecs, rx_usecs, adaptive;
677

678
	if (coalesce->use_adaptive_tx_coalesce)
679 680 681 682 683 684 685 686 687 688
		return -EOPNOTSUPP;

	if (coalesce->rx_coalesce_usecs || coalesce->tx_coalesce_usecs) {
		EFX_ERR(efx, "invalid coalescing setting. "
			"Only rx/tx_coalesce_usecs_irq are supported\n");
		return -EOPNOTSUPP;
	}

	rx_usecs = coalesce->rx_coalesce_usecs_irq;
	tx_usecs = coalesce->tx_coalesce_usecs_irq;
689
	adaptive = coalesce->use_adaptive_rx_coalesce;
690 691 692

	/* If the channel is shared only allow RX parameters to be set */
	efx_for_each_tx_queue(tx_queue, efx) {
B
Ben Hutchings 已提交
693
		if ((tx_queue->channel->channel < efx->n_rx_channels) &&
694 695 696 697 698 699 700
		    tx_usecs) {
			EFX_ERR(efx, "Channel is shared. "
				"Only RX coalescing may be set\n");
			return -EOPNOTSUPP;
		}
	}

701
	efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive);
702
	efx_for_each_channel(channel, efx)
703
		efx->type->push_irq_moderation(channel);
704 705 706 707 708 709 710

	return 0;
}

static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
				      struct ethtool_pauseparam *pause)
{
711
	struct efx_nic *efx = netdev_priv(net_dev);
B
Ben Hutchings 已提交
712 713
	enum efx_fc_type wanted_fc, old_fc;
	u32 old_adv;
B
Ben Hutchings 已提交
714
	bool reset;
B
Ben Hutchings 已提交
715 716 717
	int rc = 0;

	mutex_lock(&efx->mac_lock);
718

B
Ben Hutchings 已提交
719 720 721 722 723 724
	wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
		     (pause->tx_pause ? EFX_FC_TX : 0) |
		     (pause->autoneg ? EFX_FC_AUTO : 0));

	if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
		EFX_LOG(efx, "Flow control unsupported: tx ON rx OFF\n");
B
Ben Hutchings 已提交
725 726
		rc = -EINVAL;
		goto out;
B
Ben Hutchings 已提交
727 728
	}

B
Ben Hutchings 已提交
729 730 731 732
	if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising) {
		EFX_LOG(efx, "Autonegotiation is disabled\n");
		rc = -EINVAL;
		goto out;
B
Ben Hutchings 已提交
733 734 735 736 737 738 739 740 741
	}

	/* TX flow control may automatically turn itself off if the
	 * link partner (intermittently) stops responding to pause
	 * frames. There isn't any indication that this has happened,
	 * so the best we do is leave it up to the user to spot this
	 * and fix it be cycling transmit flow control on this end. */
	reset = (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX);
	if (EFX_WORKAROUND_11482(efx) && reset) {
742
		if (efx_nic_rev(efx) == EFX_REV_FALCON_B0) {
B
Ben Hutchings 已提交
743
			/* Recover by resetting the EM block */
B
Ben Hutchings 已提交
744 745 746 747
			falcon_stop_nic_stats(efx);
			falcon_drain_tx_fifo(efx);
			efx->mac_op->reconfigure(efx);
			falcon_start_nic_stats(efx);
B
Ben Hutchings 已提交
748 749 750 751 752 753
		} else {
			/* Schedule a reset to recover */
			efx_schedule_reset(efx, RESET_TYPE_INVISIBLE);
		}
	}

B
Ben Hutchings 已提交
754 755 756 757 758 759 760 761 762 763 764 765
	old_adv = efx->link_advertising;
	old_fc = efx->wanted_fc;
	efx_link_set_wanted_fc(efx, wanted_fc);
	if (efx->link_advertising != old_adv ||
	    (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
		rc = efx->phy_op->reconfigure(efx);
		if (rc) {
			EFX_ERR(efx, "Unable to advertise requested flow "
				"control setting\n");
			goto out;
		}
	}
B
Ben Hutchings 已提交
766

B
Ben Hutchings 已提交
767 768 769 770
	/* Reconfigure the MAC. The PHY *may* generate a link state change event
	 * if the user just changed the advertised capabilities, but there's no
	 * harm doing this twice */
	efx->mac_op->reconfigure(efx);
B
Ben Hutchings 已提交
771

B
Ben Hutchings 已提交
772
out:
B
Ben Hutchings 已提交
773
	mutex_unlock(&efx->mac_lock);
774

B
Ben Hutchings 已提交
775
	return rc;
776 777 778 779 780
}

static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
				       struct ethtool_pauseparam *pause)
{
781
	struct efx_nic *efx = netdev_priv(net_dev);
782

B
Ben Hutchings 已提交
783 784 785
	pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
	pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
	pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
786 787 788
}


789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static void efx_ethtool_get_wol(struct net_device *net_dev,
				struct ethtool_wolinfo *wol)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	return efx->type->get_wol(efx, wol);
}


static int efx_ethtool_set_wol(struct net_device *net_dev,
			       struct ethtool_wolinfo *wol)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	return efx->type->set_wol(efx, wol->wolopts);
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
extern int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
{
	struct efx_nic *efx = netdev_priv(net_dev);
	enum reset_type method;
	enum {
		ETH_RESET_EFX_INVISIBLE = (ETH_RESET_DMA | ETH_RESET_FILTER |
					   ETH_RESET_OFFLOAD | ETH_RESET_MAC)
	};

	/* Check for minimal reset flags */
	if ((*flags & ETH_RESET_EFX_INVISIBLE) != ETH_RESET_EFX_INVISIBLE)
		return -EINVAL;
	*flags ^= ETH_RESET_EFX_INVISIBLE;
	method = RESET_TYPE_INVISIBLE;

	if (*flags & ETH_RESET_PHY) {
		*flags ^= ETH_RESET_PHY;
		method = RESET_TYPE_ALL;
	}

	if ((*flags & efx->type->reset_world_flags) ==
	    efx->type->reset_world_flags) {
		*flags ^= efx->type->reset_world_flags;
		method = RESET_TYPE_WORLD;
	}

	return efx_reset(efx, method);
}

833
const struct ethtool_ops efx_ethtool_ops = {
834 835 836 837 838
	.get_settings		= efx_ethtool_get_settings,
	.set_settings		= efx_ethtool_set_settings,
	.get_drvinfo		= efx_ethtool_get_drvinfo,
	.nway_reset		= efx_ethtool_nway_reset,
	.get_link		= efx_ethtool_get_link,
839 840 841
	.get_eeprom_len		= efx_ethtool_get_eeprom_len,
	.get_eeprom		= efx_ethtool_get_eeprom,
	.set_eeprom		= efx_ethtool_set_eeprom,
842 843 844 845 846 847 848
	.get_coalesce		= efx_ethtool_get_coalesce,
	.set_coalesce		= efx_ethtool_set_coalesce,
	.get_pauseparam         = efx_ethtool_get_pauseparam,
	.set_pauseparam         = efx_ethtool_set_pauseparam,
	.get_rx_csum		= efx_ethtool_get_rx_csum,
	.set_rx_csum		= efx_ethtool_set_rx_csum,
	.get_tx_csum		= ethtool_op_get_tx_csum,
849 850
	/* Need to enable/disable IPv6 too */
	.set_tx_csum		= efx_ethtool_set_tx_csum,
851 852
	.get_sg			= ethtool_op_get_sg,
	.set_sg			= ethtool_op_set_sg,
B
Ben Hutchings 已提交
853
	.get_tso		= ethtool_op_get_tso,
B
Ben Hutchings 已提交
854 855
	/* Need to enable/disable TSO-IPv6 too */
	.set_tso		= efx_ethtool_set_tso,
856 857
	.get_flags		= ethtool_op_get_flags,
	.set_flags		= ethtool_op_set_flags,
858
	.get_sset_count		= efx_ethtool_get_sset_count,
859
	.self_test		= efx_ethtool_self_test,
860 861 862
	.get_strings		= efx_ethtool_get_strings,
	.phys_id		= efx_ethtool_phys_id,
	.get_ethtool_stats	= efx_ethtool_get_stats,
863 864
	.get_wol                = efx_ethtool_get_wol,
	.set_wol                = efx_ethtool_set_wol,
865
	.reset			= efx_ethtool_reset,
866
};