selftest.c 20.8 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 15 16 17 18 19 20
 *
 * 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/module.h>
#include <linux/delay.h>
#include <linux/kernel_stat.h>
#include <linux/pci.h>
#include <linux/ethtool.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/udp.h>
#include <linux/rtnetlink.h>
21
#include <linux/slab.h>
22 23 24
#include <asm/io.h>
#include "net_driver.h"
#include "efx.h"
B
Ben Hutchings 已提交
25
#include "nic.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "selftest.h"
#include "workarounds.h"

/*
 * Loopback test packet structure
 *
 * The self-test should stress every RSS vector, and unfortunately
 * Falcon only performs RSS on TCP/UDP packets.
 */
struct efx_loopback_payload {
	struct ethhdr header;
	struct iphdr ip;
	struct udphdr udp;
	__be16 iteration;
	const char msg[64];
41
} __packed;
42 43 44 45 46 47

/* Loopback test source MAC address */
static const unsigned char payload_source[ETH_ALEN] = {
	0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
};

48
static const char payload_msg[] =
49 50 51
	"Hello world! This is an Efx loopback test in progress!";

/**
B
Ben Hutchings 已提交
52
 * efx_loopback_state - persistent state during a loopback selftest
53 54 55
 * @flush:		Drop all packets in efx_loopback_rx_packet
 * @packet_count:	Number of packets being used in this test
 * @skbs:		An array of skbs transmitted
56
 * @offload_csum:	Checksums are being offloaded
57 58 59 60
 * @rx_good:		RX good packet count
 * @rx_bad:		RX bad packet count
 * @payload:		Payload used in tests
 */
B
Ben Hutchings 已提交
61
struct efx_loopback_state {
62
	bool flush;
63 64
	int packet_count;
	struct sk_buff **skbs;
65
	bool offload_csum;
66 67 68 69 70 71 72
	atomic_t rx_good;
	atomic_t rx_bad;
	struct efx_loopback_payload payload;
};

/**************************************************************************
 *
B
Ben Hutchings 已提交
73
 * MII, NVRAM and register tests
74 75 76
 *
 **************************************************************************/

77
static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
B
Ben Hutchings 已提交
78 79 80
{
	int rc = 0;

81 82 83
	if (efx->phy_op->test_alive) {
		rc = efx->phy_op->test_alive(efx);
		tests->phy_alive = rc ? -1 : 1;
B
Ben Hutchings 已提交
84 85 86 87 88 89 90
	}

	return rc;
}

static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
{
91 92 93 94 95 96
	int rc = 0;

	if (efx->type->test_nvram) {
		rc = efx->type->test_nvram(efx);
		tests->nvram = rc ? -1 : 1;
	}
B
Ben Hutchings 已提交
97 98 99 100 101 102

	return rc;
}

static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
{
103
	int rc = 0;
B
Ben Hutchings 已提交
104

105 106 107 108 109
	/* Test register access */
	if (efx->type->test_registers) {
		rc = efx->type->test_registers(efx);
		tests->registers = rc ? -1 : 1;
	}
B
Ben Hutchings 已提交
110 111 112

	return rc;
}
113 114 115 116 117 118 119 120 121 122 123 124 125

/**************************************************************************
 *
 * Interrupt and event queue testing
 *
 **************************************************************************/

/* Test generation and receipt of interrupts */
static int efx_test_interrupts(struct efx_nic *efx,
			       struct efx_self_tests *tests)
{
	struct efx_channel *channel;

126
	netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
127 128 129 130 131 132 133 134
	tests->interrupt = -1;

	/* Reset interrupt flag */
	efx->last_irq_cpu = -1;
	smp_wmb();

	/* ACK each interrupting event queue. Receiving an interrupt due to
	 * traffic before a test event is raised is considered a pass */
135
	efx_for_each_channel(channel, efx) {
136 137 138 139 140 141
		if (channel->work_pending)
			efx_process_channel_now(channel);
		if (efx->last_irq_cpu >= 0)
			goto success;
	}

142
	efx_nic_generate_interrupt(efx);
143 144

	/* Wait for arrival of test interrupt. */
145
	netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
146 147 148 149
	schedule_timeout_uninterruptible(HZ / 10);
	if (efx->last_irq_cpu >= 0)
		goto success;

150
	netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
151 152 153
	return -ETIMEDOUT;

 success:
154 155
	netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
		  INT_MODE(efx),
156
		efx->last_irq_cpu);
157 158 159 160 161 162 163 164
	tests->interrupt = 1;
	return 0;
}

/* Test generation and receipt of interrupting events */
static int efx_test_eventq_irq(struct efx_channel *channel,
			       struct efx_self_tests *tests)
{
165
	struct efx_nic *efx = channel->efx;
166
	unsigned int magic_count, count;
167 168 169 170 171

	tests->eventq_dma[channel->channel] = -1;
	tests->eventq_int[channel->channel] = -1;
	tests->eventq_poll[channel->channel] = -1;

172
	magic_count = channel->magic_count;
173 174 175
	channel->efx->last_irq_cpu = -1;
	smp_wmb();

176
	efx_nic_generate_test_event(channel);
177 178 179 180 181 182 183 184 185

	/* Wait for arrival of interrupt */
	count = 0;
	do {
		schedule_timeout_uninterruptible(HZ / 100);

		if (channel->work_pending)
			efx_process_channel_now(channel);

186
		if (channel->magic_count != magic_count)
187 188 189
			goto eventq_ok;
	} while (++count < 2);

190 191 192
	netif_err(efx, drv, efx->net_dev,
		  "channel %d timed out waiting for event queue\n",
		  channel->channel);
193 194 195

	/* See if interrupt arrived */
	if (channel->efx->last_irq_cpu >= 0) {
196 197 198 199
		netif_err(efx, drv, efx->net_dev,
			  "channel %d saw interrupt on CPU%d "
			  "during event queue test\n", channel->channel,
			  raw_smp_processor_id());
200 201 202 203 204
		tests->eventq_int[channel->channel] = 1;
	}

	/* Check to see if event was received even if interrupt wasn't */
	efx_process_channel_now(channel);
205
	if (channel->magic_count != magic_count) {
206 207 208
		netif_err(efx, drv, efx->net_dev,
			  "channel %d event was generated, but "
			  "failed to trigger an interrupt\n", channel->channel);
209 210 211 212 213
		tests->eventq_dma[channel->channel] = 1;
	}

	return -ETIMEDOUT;
 eventq_ok:
214 215
	netif_dbg(efx, drv, efx->net_dev, "channel %d event queue passed\n",
		  channel->channel);
216 217 218 219 220 221
	tests->eventq_dma[channel->channel] = 1;
	tests->eventq_int[channel->channel] = 1;
	tests->eventq_poll[channel->channel] = 1;
	return 0;
}

222 223
static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
			unsigned flags)
224
{
B
Ben Hutchings 已提交
225
	int rc;
226

227
	if (!efx->phy_op->run_tests)
228 229
		return 0;

B
Ben Hutchings 已提交
230
	mutex_lock(&efx->mac_lock);
231
	rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
B
Ben Hutchings 已提交
232 233
	mutex_unlock(&efx->mac_lock);
	return rc;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

/**************************************************************************
 *
 * Loopback testing
 * NB Only one loopback test can be executing concurrently.
 *
 **************************************************************************/

/* Loopback test RX callback
 * This is called for each received packet during loopback testing.
 */
void efx_loopback_rx_packet(struct efx_nic *efx,
			    const char *buf_ptr, int pkt_len)
{
B
Ben Hutchings 已提交
249
	struct efx_loopback_state *state = efx->loopback_selftest;
250 251 252 253 254 255 256 257 258 259
	struct efx_loopback_payload *received;
	struct efx_loopback_payload *payload;

	BUG_ON(!buf_ptr);

	/* If we are just flushing, then drop the packet */
	if ((state == NULL) || state->flush)
		return;

	payload = &state->payload;
B
Ben Hutchings 已提交
260

261 262 263
	buf_ptr += efx->type->rx_buffer_hash_size;
	pkt_len -= efx->type->rx_buffer_hash_size;

264
	received = (struct efx_loopback_payload *) buf_ptr;
265
	received->ip.saddr = payload->ip.saddr;
266 267 268
	if (state->offload_csum)
		received->ip.check = payload->ip.check;

269 270
	/* Check that header exists */
	if (pkt_len < sizeof(received->header)) {
271 272 273
		netif_err(efx, drv, efx->net_dev,
			  "saw runt RX packet (length %d) in %s loopback "
			  "test\n", pkt_len, LOOPBACK_MODE(efx));
274 275 276 277 278
		goto err;
	}

	/* Check that the ethernet header exists */
	if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
279 280 281
		netif_err(efx, drv, efx->net_dev,
			  "saw non-loopback RX packet in %s loopback test\n",
			  LOOPBACK_MODE(efx));
282 283 284 285 286
		goto err;
	}

	/* Check packet length */
	if (pkt_len != sizeof(*payload)) {
287 288 289 290
		netif_err(efx, drv, efx->net_dev,
			  "saw incorrect RX packet length %d (wanted %d) in "
			  "%s loopback test\n", pkt_len, (int)sizeof(*payload),
			  LOOPBACK_MODE(efx));
291 292 293 294 295
		goto err;
	}

	/* Check that IP header matches */
	if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
296 297 298
		netif_err(efx, drv, efx->net_dev,
			  "saw corrupted IP header in %s loopback test\n",
			  LOOPBACK_MODE(efx));
299 300 301 302 303
		goto err;
	}

	/* Check that msg and padding matches */
	if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
304 305 306
		netif_err(efx, drv, efx->net_dev,
			  "saw corrupted RX packet in %s loopback test\n",
			  LOOPBACK_MODE(efx));
307 308 309 310 311
		goto err;
	}

	/* Check that iteration matches */
	if (received->iteration != payload->iteration) {
312 313 314 315
		netif_err(efx, drv, efx->net_dev,
			  "saw RX packet from iteration %d (wanted %d) in "
			  "%s loopback test\n", ntohs(received->iteration),
			  ntohs(payload->iteration), LOOPBACK_MODE(efx));
316 317 318 319
		goto err;
	}

	/* Increase correct RX count */
320 321
	netif_vdbg(efx, drv, efx->net_dev,
		   "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
322 323 324 325 326 327 328

	atomic_inc(&state->rx_good);
	return;

 err:
#ifdef EFX_ENABLE_DEBUG
	if (atomic_read(&state->rx_bad) == 0) {
329
		netif_err(efx, drv, efx->net_dev, "received packet:\n");
330 331
		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
			       buf_ptr, pkt_len, 0);
332
		netif_err(efx, drv, efx->net_dev, "expected packet:\n");
333 334 335 336 337 338 339 340 341 342
		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
			       &state->payload, sizeof(state->payload), 0);
	}
#endif
	atomic_inc(&state->rx_bad);
}

/* Initialise an efx_selftest_state for a new iteration */
static void efx_iterate_state(struct efx_nic *efx)
{
B
Ben Hutchings 已提交
343
	struct efx_loopback_state *state = efx->loopback_selftest;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	struct net_device *net_dev = efx->net_dev;
	struct efx_loopback_payload *payload = &state->payload;

	/* Initialise the layerII header */
	memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
	memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
	payload->header.h_proto = htons(ETH_P_IP);

	/* saddr set later and used as incrementing count */
	payload->ip.daddr = htonl(INADDR_LOOPBACK);
	payload->ip.ihl = 5;
	payload->ip.check = htons(0xdead);
	payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
	payload->ip.version = IPVERSION;
	payload->ip.protocol = IPPROTO_UDP;

	/* Initialise udp header */
	payload->udp.source = 0;
	payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
				 sizeof(struct iphdr));
	payload->udp.check = 0;	/* checksum ignored */

	/* Fill out payload */
	payload->iteration = htons(ntohs(payload->iteration) + 1);
	memcpy(&payload->msg, payload_msg, sizeof(payload_msg));

	/* Fill out remaining state members */
	atomic_set(&state->rx_good, 0);
	atomic_set(&state->rx_bad, 0);
	smp_wmb();
}

B
Ben Hutchings 已提交
376
static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
377 378
{
	struct efx_nic *efx = tx_queue->efx;
B
Ben Hutchings 已提交
379
	struct efx_loopback_state *state = efx->loopback_selftest;
380 381
	struct efx_loopback_payload *payload;
	struct sk_buff *skb;
382 383
	int i;
	netdev_tx_t rc;
384 385 386

	/* Transmit N copies of buffer */
	for (i = 0; i < state->packet_count; i++) {
B
Ben Hutchings 已提交
387
		/* Allocate an skb, holding an extra reference for
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
		 * transmit completion counting */
		skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
		if (!skb)
			return -ENOMEM;
		state->skbs[i] = skb;
		skb_get(skb);

		/* Copy the payload in, incrementing the source address to
		 * exercise the rss vectors */
		payload = ((struct efx_loopback_payload *)
			   skb_put(skb, sizeof(state->payload)));
		memcpy(payload, &state->payload, sizeof(state->payload));
		payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));

		/* Ensure everything we've written is visible to the
		 * interrupt handler. */
		smp_wmb();

406
		if (efx_dev_registered(efx))
407
			netif_tx_lock_bh(efx->net_dev);
408
		rc = efx_enqueue_skb(tx_queue, skb);
409
		if (efx_dev_registered(efx))
410 411 412
			netif_tx_unlock_bh(efx->net_dev);

		if (rc != NETDEV_TX_OK) {
413 414 415 416 417
			netif_err(efx, drv, efx->net_dev,
				  "TX queue %d could not transmit packet %d of "
				  "%d in %s loopback test\n", tx_queue->queue,
				  i + 1, state->packet_count,
				  LOOPBACK_MODE(efx));
418 419 420 421 422 423 424 425 426 427

			/* Defer cleaning up the other skbs for the caller */
			kfree_skb(skb);
			return -EPIPE;
		}
	}

	return 0;
}

B
Ben Hutchings 已提交
428 429
static int efx_poll_loopback(struct efx_nic *efx)
{
B
Ben Hutchings 已提交
430
	struct efx_loopback_state *state = efx->loopback_selftest;
B
Ben Hutchings 已提交
431 432 433 434
	struct efx_channel *channel;

	/* NAPI polling is not enabled, so process channels
	 * synchronously */
435
	efx_for_each_channel(channel, efx) {
B
Ben Hutchings 已提交
436 437 438 439 440 441 442 443
		if (channel->work_pending)
			efx_process_channel_now(channel);
	}
	return atomic_read(&state->rx_good) == state->packet_count;
}

static int efx_end_loopback(struct efx_tx_queue *tx_queue,
			    struct efx_loopback_self_tests *lb_tests)
444 445
{
	struct efx_nic *efx = tx_queue->efx;
B
Ben Hutchings 已提交
446
	struct efx_loopback_state *state = efx->loopback_selftest;
447 448 449 450
	struct sk_buff *skb;
	int tx_done = 0, rx_good, rx_bad;
	int i, rc = 0;

451
	if (efx_dev_registered(efx))
452 453 454 455 456 457 458 459 460 461 462
		netif_tx_lock_bh(efx->net_dev);

	/* Count the number of tx completions, and decrement the refcnt. Any
	 * skbs not already completed will be free'd when the queue is flushed */
	for (i=0; i < state->packet_count; i++) {
		skb = state->skbs[i];
		if (skb && !skb_shared(skb))
			++tx_done;
		dev_kfree_skb_any(skb);
	}

463
	if (efx_dev_registered(efx))
464 465 466 467 468 469 470 471 472
		netif_tx_unlock_bh(efx->net_dev);

	/* Check TX completion and received packet counts */
	rx_good = atomic_read(&state->rx_good);
	rx_bad = atomic_read(&state->rx_bad);
	if (tx_done != state->packet_count) {
		/* Don't free the skbs; they will be picked up on TX
		 * overflow or channel teardown.
		 */
473 474 475 476 477
		netif_err(efx, drv, efx->net_dev,
			  "TX queue %d saw only %d out of an expected %d "
			  "TX completion events in %s loopback test\n",
			  tx_queue->queue, tx_done, state->packet_count,
			  LOOPBACK_MODE(efx));
478 479 480 481 482 483
		rc = -ETIMEDOUT;
		/* Allow to fall through so we see the RX errors as well */
	}

	/* We may always be up to a flush away from our desired packet total */
	if (rx_good != state->packet_count) {
484 485 486 487 488
		netif_dbg(efx, drv, efx->net_dev,
			  "TX queue %d saw only %d out of an expected %d "
			  "received packets in %s loopback test\n",
			  tx_queue->queue, rx_good, state->packet_count,
			  LOOPBACK_MODE(efx));
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
		rc = -ETIMEDOUT;
		/* Fall through */
	}

	/* Update loopback test structure */
	lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
	lb_tests->tx_done[tx_queue->queue] += tx_done;
	lb_tests->rx_good += rx_good;
	lb_tests->rx_bad += rx_bad;

	return rc;
}

static int
efx_test_loopback(struct efx_tx_queue *tx_queue,
		  struct efx_loopback_self_tests *lb_tests)
{
	struct efx_nic *efx = tx_queue->efx;
B
Ben Hutchings 已提交
507
	struct efx_loopback_state *state = efx->loopback_selftest;
B
Ben Hutchings 已提交
508
	int i, begin_rc, end_rc;
509

B
Ben Hutchings 已提交
510
	for (i = 0; i < 3; i++) {
511
		/* Determine how many packets to send */
512
		state->packet_count = EFX_TXQ_SIZE / 3;
513 514 515
		state->packet_count = min(1 << (i << 2), state->packet_count);
		state->skbs = kzalloc(sizeof(state->skbs[0]) *
				      state->packet_count, GFP_KERNEL);
516 517
		if (!state->skbs)
			return -ENOMEM;
518
		state->flush = false;
519

520 521 522 523
		netif_dbg(efx, drv, efx->net_dev,
			  "TX queue %d testing %s loopback with %d packets\n",
			  tx_queue->queue, LOOPBACK_MODE(efx),
			  state->packet_count);
524 525

		efx_iterate_state(efx);
B
Ben Hutchings 已提交
526 527 528 529 530 531 532 533
		begin_rc = efx_begin_loopback(tx_queue);

		/* This will normally complete very quickly, but be
		 * prepared to wait up to 100 ms. */
		msleep(1);
		if (!efx_poll_loopback(efx)) {
			msleep(100);
			efx_poll_loopback(efx);
534 535
		}

B
Ben Hutchings 已提交
536
		end_rc = efx_end_loopback(tx_queue, lb_tests);
537 538
		kfree(state->skbs);

B
Ben Hutchings 已提交
539
		if (begin_rc || end_rc) {
540 541 542
			/* Wait a while to ensure there are no packets
			 * floating around after a failure. */
			schedule_timeout_uninterruptible(HZ / 10);
B
Ben Hutchings 已提交
543
			return begin_rc ? begin_rc : end_rc;
544 545 546
		}
	}

547 548 549 550
	netif_dbg(efx, drv, efx->net_dev,
		  "TX queue %d passed %s loopback test with a burst length "
		  "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
		  state->packet_count);
551

552
	return 0;
553 554
}

555 556 557 558 559 560 561
/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
 * to delay and retry. Therefore, it's safer to just poll directly. Wait
 * for link up and any faults to dissipate. */
static int efx_wait_for_link(struct efx_nic *efx)
{
	struct efx_link_state *link_state = &efx->link_state;
562
	int count, link_up_count = 0;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	bool link_up;

	for (count = 0; count < 40; count++) {
		schedule_timeout_uninterruptible(HZ / 10);

		if (efx->type->monitor != NULL) {
			mutex_lock(&efx->mac_lock);
			efx->type->monitor(efx);
			mutex_unlock(&efx->mac_lock);
		} else {
			struct efx_channel *channel = &efx->channel[0];
			if (channel->work_pending)
				efx_process_channel_now(channel);
		}

		mutex_lock(&efx->mac_lock);
		link_up = link_state->up;
		if (link_up)
			link_up = !efx->mac_op->check_fault(efx);
		mutex_unlock(&efx->mac_lock);

584 585 586 587 588 589
		if (link_up) {
			if (++link_up_count == 2)
				return 0;
		} else {
			link_up_count = 0;
		}
590 591 592 593 594
	}

	return -ETIMEDOUT;
}

595
static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
596 597
			      unsigned int loopback_modes)
{
B
Ben Hutchings 已提交
598 599
	enum efx_loopback_mode mode;
	struct efx_loopback_state *state;
600
	struct efx_tx_queue *tx_queue;
601
	int rc = 0;
602

B
Ben Hutchings 已提交
603 604 605 606 607 608 609 610 611
	/* Set the port loopback_selftest member. From this point on
	 * all received packets will be dropped. Mark the state as
	 * "flushing" so all inflight packets are dropped */
	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (state == NULL)
		return -ENOMEM;
	BUG_ON(efx->loopback_selftest);
	state->flush = true;
	efx->loopback_selftest = state;
612 613

	/* Test all supported loopback modes */
B
Ben Hutchings 已提交
614
	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
615 616 617 618
		if (!(loopback_modes & (1 << mode)))
			continue;

		/* Move the port into the specified loopback mode. */
619
		state->flush = true;
620
		mutex_lock(&efx->mac_lock);
621
		efx->loopback_mode = mode;
622 623 624
		rc = __efx_reconfigure_port(efx);
		mutex_unlock(&efx->mac_lock);
		if (rc) {
625 626 627
			netif_err(efx, drv, efx->net_dev,
				  "unable to move into %s loopback\n",
				  LOOPBACK_MODE(efx));
628 629
			goto out;
		}
630

631 632
		rc = efx_wait_for_link(efx);
		if (rc) {
633 634 635
			netif_err(efx, drv, efx->net_dev,
				  "loopback %s never came up\n",
				  LOOPBACK_MODE(efx));
636 637 638
			goto out;
		}

639 640
		/* Test both types of TX queue */
		efx_for_each_channel_tx_queue(tx_queue, &efx->channel[0]) {
B
Ben Hutchings 已提交
641 642
			state->offload_csum = (tx_queue->queue &
					       EFX_TXQ_TYPE_OFFLOAD);
B
Ben Hutchings 已提交
643 644
			rc = efx_test_loopback(tx_queue,
					       &tests->loopback[mode]);
645 646 647 648 649 650
			if (rc)
				goto out;
		}
	}

 out:
B
Ben Hutchings 已提交
651
	/* Remove the flush. The caller will remove the loopback setting */
652
	state->flush = true;
B
Ben Hutchings 已提交
653 654 655
	efx->loopback_selftest = NULL;
	wmb();
	kfree(state);
656 657 658 659 660 661

	return rc;
}

/**************************************************************************
 *
662
 * Entry point
663 664 665
 *
 *************************************************************************/

666 667
int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
		 unsigned flags)
668
{
669 670
	enum efx_loopback_mode loopback_mode = efx->loopback_mode;
	int phy_mode = efx->phy_mode;
671
	enum reset_type reset_method = RESET_TYPE_INVISIBLE;
672
	struct efx_channel *channel;
673 674 675 676
	int rc_test = 0, rc_reset = 0, rc;

	/* Online (i.e. non-disruptive) testing
	 * This checks interrupt generation, event delivery and PHY presence. */
B
Ben Hutchings 已提交
677

678
	rc = efx_test_phy_alive(efx, tests);
679 680
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
681 682

	rc = efx_test_nvram(efx, tests);
683 684
	if (rc && !rc_test)
		rc_test = rc;
685

B
Ben Hutchings 已提交
686
	rc = efx_test_interrupts(efx, tests);
687 688
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
689

690
	efx_for_each_channel(channel, efx) {
691
		rc = efx_test_eventq_irq(channel, tests);
692 693
		if (rc && !rc_test)
			rc_test = rc;
694
	}
B
Ben Hutchings 已提交
695

696 697
	if (rc_test)
		return rc_test;
698

699
	if (!(flags & ETH_TEST_FL_OFFLINE))
700
		return efx_test_phy(efx, tests, flags);
701 702 703

	/* Offline (i.e. disruptive) testing
	 * This checks MAC and PHY loopback on the specified port. */
B
Ben Hutchings 已提交
704 705 706 707 708 709 710

	/* force the carrier state off so the kernel doesn't transmit during
	 * the loopback test, and the watchdog timeout doesn't fire. Also put
	 * falcon into loopback for the register test.
	 */
	mutex_lock(&efx->mac_lock);
	efx->port_inhibited = true;
B
Ben Hutchings 已提交
711 712 713 714 715 716 717 718 719
	if (efx->loopback_modes) {
		/* We need the 312 clock from the PHY to test the XMAC
		 * registers, so move into XGMII loopback if available */
		if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
			efx->loopback_mode = LOOPBACK_XGMII;
		else
			efx->loopback_mode = __ffs(efx->loopback_modes);
	}

B
Ben Hutchings 已提交
720 721 722 723
	__efx_reconfigure_port(efx);
	mutex_unlock(&efx->mac_lock);

	/* free up all consumers of SRAM (including all the queues) */
B
Ben Hutchings 已提交
724
	efx_reset_down(efx, reset_method);
B
Ben Hutchings 已提交
725 726

	rc = efx_test_chip(efx, tests);
727 728
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
729 730

	/* reset the chip to recover from the register test */
731
	rc_reset = efx->type->reset(efx, reset_method);
B
Ben Hutchings 已提交
732

733 734 735
	/* Ensure that the phy is powered and out of loopback
	 * for the bist and loopback tests */
	efx->phy_mode &= ~PHY_MODE_LOW_POWER;
B
Ben Hutchings 已提交
736
	efx->loopback_mode = LOOPBACK_NONE;
737

B
Ben Hutchings 已提交
738
	rc = efx_reset_up(efx, reset_method, rc_reset == 0);
739 740 741 742
	if (rc && !rc_reset)
		rc_reset = rc;

	if (rc_reset) {
743 744
		netif_err(efx, drv, efx->net_dev,
			  "Unable to recover from chip test\n");
B
Ben Hutchings 已提交
745
		efx_schedule_reset(efx, RESET_TYPE_DISABLE);
746
		return rc_reset;
B
Ben Hutchings 已提交
747
	}
748

749
	rc = efx_test_phy(efx, tests, flags);
750 751
	if (rc && !rc_test)
		rc_test = rc;
752

753 754 755
	rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
	if (rc && !rc_test)
		rc_test = rc;
756

B
Ben Hutchings 已提交
757
	/* restore the PHY to the previous state */
B
Ben Hutchings 已提交
758
	mutex_lock(&efx->mac_lock);
B
Ben Hutchings 已提交
759 760
	efx->phy_mode = phy_mode;
	efx->port_inhibited = false;
B
Ben Hutchings 已提交
761 762 763
	efx->loopback_mode = loopback_mode;
	__efx_reconfigure_port(efx);
	mutex_unlock(&efx->mac_lock);
B
Ben Hutchings 已提交
764

765
	return rc_test;
766 767
}