selftest.c 21.8 KB
Newer Older
1
/****************************************************************************
B
Ben Hutchings 已提交
2
 * Driver for Solarflare network controllers and boards
3
 * Copyright 2005-2006 Fen Systems Ltd.
B
Ben Hutchings 已提交
4
 * Copyright 2006-2012 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
#include "net_driver.h"
#include "efx.h"
B
Ben Hutchings 已提交
24
#include "nic.h"
25 26 27
#include "selftest.h"
#include "workarounds.h"

B
Ben Hutchings 已提交
28 29 30 31 32 33 34 35 36 37
/* IRQ latency can be enormous because:
 * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
 *   slow serial console or an old IDE driver doing error recovery
 * - The PREEMPT_RT patches mostly deal with this, but also allow a
 *   tasklet or normal task to be given higher priority than our IRQ
 *   threads
 * Try to avoid blaming the hardware for this.
 */
#define IRQ_TIMEOUT HZ

38 39 40 41 42 43 44 45 46 47 48
/*
 * 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;
49
	char msg[64];
50
} __packed;
51 52

/* Loopback test source MAC address */
53
static const u8 payload_source[ETH_ALEN] __aligned(2) = {
54 55 56
	0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
};

57
static const char payload_msg[] =
58 59
	"Hello world! This is an Efx loopback test in progress!";

S
stephen hemminger 已提交
60 61
/* Interrupt mode names */
static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
62
static const char *const efx_interrupt_mode_names[] = {
S
stephen hemminger 已提交
63 64 65 66 67 68 69
	[EFX_INT_MODE_MSIX]   = "MSI-X",
	[EFX_INT_MODE_MSI]    = "MSI",
	[EFX_INT_MODE_LEGACY] = "legacy",
};
#define INT_MODE(efx) \
	STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)

70
/**
B
Ben Hutchings 已提交
71
 * efx_loopback_state - persistent state during a loopback selftest
72 73 74
 * @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
75
 * @offload_csum:	Checksums are being offloaded
76 77 78 79
 * @rx_good:		RX good packet count
 * @rx_bad:		RX bad packet count
 * @payload:		Payload used in tests
 */
B
Ben Hutchings 已提交
80
struct efx_loopback_state {
81
	bool flush;
82 83
	int packet_count;
	struct sk_buff **skbs;
84
	bool offload_csum;
85 86 87 88 89
	atomic_t rx_good;
	atomic_t rx_bad;
	struct efx_loopback_payload payload;
};

B
Ben Hutchings 已提交
90 91 92
/* How long to wait for all the packets to arrive (in ms) */
#define LOOPBACK_TIMEOUT_MS 1000

93 94
/**************************************************************************
 *
B
Ben Hutchings 已提交
95
 * MII, NVRAM and register tests
96 97 98
 *
 **************************************************************************/

99
static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
B
Ben Hutchings 已提交
100 101 102
{
	int rc = 0;

103 104 105
	if (efx->phy_op->test_alive) {
		rc = efx->phy_op->test_alive(efx);
		tests->phy_alive = rc ? -1 : 1;
B
Ben Hutchings 已提交
106 107 108 109 110 111 112
	}

	return rc;
}

static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
{
113 114 115 116
	int rc = 0;

	if (efx->type->test_nvram) {
		rc = efx->type->test_nvram(efx);
117 118 119 120
		if (rc == -EPERM)
			rc = 0;
		else
			tests->nvram = rc ? -1 : 1;
121
	}
B
Ben Hutchings 已提交
122 123 124 125

	return rc;
}

126 127 128 129 130 131 132 133 134 135
/**************************************************************************
 *
 * 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)
{
B
Ben Hutchings 已提交
136
	unsigned long timeout, wait;
137 138
	int cpu;

139
	netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
140 141
	tests->interrupt = -1;

142
	efx_nic_irq_test_start(efx);
B
Ben Hutchings 已提交
143 144
	timeout = jiffies + IRQ_TIMEOUT;
	wait = 1;
145 146

	/* Wait for arrival of test interrupt. */
147
	netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
B
Ben Hutchings 已提交
148 149
	do {
		schedule_timeout_uninterruptible(wait);
150
		cpu = efx_nic_irq_test_irq_cpu(efx);
B
Ben Hutchings 已提交
151 152 153 154
		if (cpu >= 0)
			goto success;
		wait *= 2;
	} while (time_before(jiffies, timeout));
155

156
	netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
157 158 159
	return -ETIMEDOUT;

 success:
160
	netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
161
		  INT_MODE(efx), cpu);
162 163 164 165 166
	tests->interrupt = 1;
	return 0;
}

/* Test generation and receipt of interrupting events */
167
static int efx_test_eventq_irq(struct efx_nic *efx,
168 169
			       struct efx_self_tests *tests)
{
170 171 172
	struct efx_channel *channel;
	unsigned int read_ptr[EFX_MAX_CHANNELS];
	unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
B
Ben Hutchings 已提交
173
	unsigned long timeout, wait;
174

175 176 177 178 179 180
	BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);

	efx_for_each_channel(channel, efx) {
		read_ptr[channel->channel] = channel->eventq_read_ptr;
		set_bit(channel->channel, &dma_pend);
		set_bit(channel->channel, &int_pend);
181
		efx_nic_event_test_start(channel);
182
	}
183

B
Ben Hutchings 已提交
184 185
	timeout = jiffies + IRQ_TIMEOUT;
	wait = 1;
186

187
	/* Wait for arrival of interrupts.  NAPI processing may or may
188 189
	 * not complete in time, but we can cope in any case.
	 */
B
Ben Hutchings 已提交
190 191 192
	do {
		schedule_timeout_uninterruptible(wait);

193
		efx_for_each_channel(channel, efx) {
194
			efx_stop_eventq(channel);
195 196 197 198 199 200 201 202
			if (channel->eventq_read_ptr !=
			    read_ptr[channel->channel]) {
				set_bit(channel->channel, &napi_ran);
				clear_bit(channel->channel, &dma_pend);
				clear_bit(channel->channel, &int_pend);
			} else {
				if (efx_nic_event_present(channel))
					clear_bit(channel->channel, &dma_pend);
203
				if (efx_nic_event_test_irq_cpu(channel) >= 0)
204 205
					clear_bit(channel->channel, &int_pend);
			}
206
			efx_start_eventq(channel);
B
Ben Hutchings 已提交
207 208 209
		}

		wait *= 2;
210
	} while ((dma_pend || int_pend) && time_before(jiffies, timeout));
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	efx_for_each_channel(channel, efx) {
		bool dma_seen = !test_bit(channel->channel, &dma_pend);
		bool int_seen = !test_bit(channel->channel, &int_pend);

		tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
		tests->eventq_int[channel->channel] = int_seen ? 1 : -1;

		if (dma_seen && int_seen) {
			netif_dbg(efx, drv, efx->net_dev,
				  "channel %d event queue passed (with%s NAPI)\n",
				  channel->channel,
				  test_bit(channel->channel, &napi_ran) ?
				  "" : "out");
		} else {
			/* Report failure and whether either interrupt or DMA
			 * worked
			 */
229
			netif_err(efx, drv, efx->net_dev,
230
				  "channel %d timed out waiting for event queue\n",
231
				  channel->channel);
232 233 234 235 236 237 238 239 240 241 242
			if (int_seen)
				netif_err(efx, drv, efx->net_dev,
					  "channel %d saw interrupt "
					  "during event queue test\n",
					  channel->channel);
			if (dma_seen)
				netif_err(efx, drv, efx->net_dev,
					  "channel %d event was generated, but "
					  "failed to trigger an interrupt\n",
					  channel->channel);
		}
243
	}
244 245

	return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
246 247
}

248 249
static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
			unsigned flags)
250
{
B
Ben Hutchings 已提交
251
	int rc;
252

253
	if (!efx->phy_op->run_tests)
254 255
		return 0;

B
Ben Hutchings 已提交
256
	mutex_lock(&efx->mac_lock);
257
	rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
B
Ben Hutchings 已提交
258
	mutex_unlock(&efx->mac_lock);
259 260 261 262 263 264
	if (rc == -EPERM)
		rc = 0;
	else
		netif_info(efx, drv, efx->net_dev,
			   "%s phy selftest\n", rc ? "Failed" : "Passed");

B
Ben Hutchings 已提交
265
	return rc;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
}

/**************************************************************************
 *
 * 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 已提交
281
	struct efx_loopback_state *state = efx->loopback_selftest;
282 283 284 285 286 287 288 289 290 291
	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 已提交
292

293
	received = (struct efx_loopback_payload *) buf_ptr;
294
	received->ip.saddr = payload->ip.saddr;
295 296 297
	if (state->offload_csum)
		received->ip.check = payload->ip.check;

298 299
	/* Check that header exists */
	if (pkt_len < sizeof(received->header)) {
300 301 302
		netif_err(efx, drv, efx->net_dev,
			  "saw runt RX packet (length %d) in %s loopback "
			  "test\n", pkt_len, LOOPBACK_MODE(efx));
303 304 305 306 307
		goto err;
	}

	/* Check that the ethernet header exists */
	if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
308 309 310
		netif_err(efx, drv, efx->net_dev,
			  "saw non-loopback RX packet in %s loopback test\n",
			  LOOPBACK_MODE(efx));
311 312 313 314 315
		goto err;
	}

	/* Check packet length */
	if (pkt_len != sizeof(*payload)) {
316 317 318 319
		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));
320 321 322 323 324
		goto err;
	}

	/* Check that IP header matches */
	if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
325 326 327
		netif_err(efx, drv, efx->net_dev,
			  "saw corrupted IP header in %s loopback test\n",
			  LOOPBACK_MODE(efx));
328 329 330 331 332
		goto err;
	}

	/* Check that msg and padding matches */
	if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
333 334 335
		netif_err(efx, drv, efx->net_dev,
			  "saw corrupted RX packet in %s loopback test\n",
			  LOOPBACK_MODE(efx));
336 337 338 339 340
		goto err;
	}

	/* Check that iteration matches */
	if (received->iteration != payload->iteration) {
341 342 343 344
		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));
345 346 347 348
		goto err;
	}

	/* Increase correct RX count */
349 350
	netif_vdbg(efx, drv, efx->net_dev,
		   "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
351 352 353 354 355

	atomic_inc(&state->rx_good);
	return;

 err:
356
#ifdef DEBUG
357
	if (atomic_read(&state->rx_bad) == 0) {
358
		netif_err(efx, drv, efx->net_dev, "received packet:\n");
359 360
		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
			       buf_ptr, pkt_len, 0);
361
		netif_err(efx, drv, efx->net_dev, "expected packet:\n");
362 363 364 365 366 367 368 369 370 371
		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 已提交
372
	struct efx_loopback_state *state = efx->loopback_selftest;
373 374 375 376
	struct net_device *net_dev = efx->net_dev;
	struct efx_loopback_payload *payload = &state->payload;

	/* Initialise the layerII header */
377 378
	ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
	ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
379 380 381 382 383
	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;
384
	payload->ip.check = (__force __sum16) htons(0xdead);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	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 已提交
405
static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
406 407
{
	struct efx_nic *efx = tx_queue->efx;
B
Ben Hutchings 已提交
408
	struct efx_loopback_state *state = efx->loopback_selftest;
409 410
	struct efx_loopback_payload *payload;
	struct sk_buff *skb;
411 412
	int i;
	netdev_tx_t rc;
413 414 415

	/* Transmit N copies of buffer */
	for (i = 0; i < state->packet_count; i++) {
B
Ben Hutchings 已提交
416
		/* Allocate an skb, holding an extra reference for
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		 * 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();

435
		netif_tx_lock_bh(efx->net_dev);
436
		rc = efx_enqueue_skb(tx_queue, skb);
437
		netif_tx_unlock_bh(efx->net_dev);
438 439

		if (rc != NETDEV_TX_OK) {
440 441 442 443 444
			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));
445 446 447 448 449 450 451 452 453 454

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

	return 0;
}

B
Ben Hutchings 已提交
455 456
static int efx_poll_loopback(struct efx_nic *efx)
{
B
Ben Hutchings 已提交
457
	struct efx_loopback_state *state = efx->loopback_selftest;
B
Ben Hutchings 已提交
458 459 460 461 462 463

	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)
464 465
{
	struct efx_nic *efx = tx_queue->efx;
B
Ben Hutchings 已提交
466
	struct efx_loopback_state *state = efx->loopback_selftest;
467 468 469 470
	struct sk_buff *skb;
	int tx_done = 0, rx_good, rx_bad;
	int i, rc = 0;

471
	netif_tx_lock_bh(efx->net_dev);
472 473 474

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

482
	netif_tx_unlock_bh(efx->net_dev);
483 484 485 486 487 488 489 490

	/* 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.
		 */
491 492 493 494 495
		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));
496 497 498 499 500 501
		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) {
502 503 504 505 506
		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));
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		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 已提交
525
	struct efx_loopback_state *state = efx->loopback_selftest;
B
Ben Hutchings 已提交
526
	int i, begin_rc, end_rc;
527

B
Ben Hutchings 已提交
528
	for (i = 0; i < 3; i++) {
529
		/* Determine how many packets to send */
530
		state->packet_count = efx->txq_entries / 3;
531
		state->packet_count = min(1 << (i << 2), state->packet_count);
532 533
		state->skbs = kcalloc(state->packet_count,
				      sizeof(state->skbs[0]), GFP_KERNEL);
534 535
		if (!state->skbs)
			return -ENOMEM;
536
		state->flush = false;
537

538 539 540 541
		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);
542 543

		efx_iterate_state(efx);
B
Ben Hutchings 已提交
544 545 546
		begin_rc = efx_begin_loopback(tx_queue);

		/* This will normally complete very quickly, but be
B
Ben Hutchings 已提交
547
		 * prepared to wait much longer. */
B
Ben Hutchings 已提交
548 549
		msleep(1);
		if (!efx_poll_loopback(efx)) {
B
Ben Hutchings 已提交
550
			msleep(LOOPBACK_TIMEOUT_MS);
B
Ben Hutchings 已提交
551
			efx_poll_loopback(efx);
552 553
		}

B
Ben Hutchings 已提交
554
		end_rc = efx_end_loopback(tx_queue, lb_tests);
555 556
		kfree(state->skbs);

B
Ben Hutchings 已提交
557
		if (begin_rc || end_rc) {
558 559 560
			/* Wait a while to ensure there are no packets
			 * floating around after a failure. */
			schedule_timeout_uninterruptible(HZ / 10);
B
Ben Hutchings 已提交
561
			return begin_rc ? begin_rc : end_rc;
562 563 564
		}
	}

565 566 567 568
	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);
569

570
	return 0;
571 572
}

573 574 575 576 577 578 579
/* 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;
580
	int count, link_up_count = 0;
581 582 583 584 585 586 587 588 589 590 591 592 593 594
	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);
		}

		mutex_lock(&efx->mac_lock);
		link_up = link_state->up;
		if (link_up)
595
			link_up = !efx->type->check_mac_fault(efx);
596 597
		mutex_unlock(&efx->mac_lock);

598 599 600 601 602 603
		if (link_up) {
			if (++link_up_count == 2)
				return 0;
		} else {
			link_up_count = 0;
		}
604 605 606 607 608
	}

	return -ETIMEDOUT;
}

609
static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
610 611
			      unsigned int loopback_modes)
{
B
Ben Hutchings 已提交
612 613
	enum efx_loopback_mode mode;
	struct efx_loopback_state *state;
614 615
	struct efx_channel *channel =
		efx_get_channel(efx, efx->tx_channel_offset);
616
	struct efx_tx_queue *tx_queue;
617
	int rc = 0;
618

B
Ben Hutchings 已提交
619 620 621 622 623 624 625 626 627
	/* 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;
628 629

	/* Test all supported loopback modes */
B
Ben Hutchings 已提交
630
	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
631 632 633 634
		if (!(loopback_modes & (1 << mode)))
			continue;

		/* Move the port into the specified loopback mode. */
635
		state->flush = true;
636
		mutex_lock(&efx->mac_lock);
637
		efx->loopback_mode = mode;
638 639 640
		rc = __efx_reconfigure_port(efx);
		mutex_unlock(&efx->mac_lock);
		if (rc) {
641 642 643
			netif_err(efx, drv, efx->net_dev,
				  "unable to move into %s loopback\n",
				  LOOPBACK_MODE(efx));
644 645
			goto out;
		}
646

647 648
		rc = efx_wait_for_link(efx);
		if (rc) {
649 650 651
			netif_err(efx, drv, efx->net_dev,
				  "loopback %s never came up\n",
				  LOOPBACK_MODE(efx));
652 653 654
			goto out;
		}

655
		/* Test all enabled types of TX queue */
656
		efx_for_each_channel_tx_queue(tx_queue, channel) {
B
Ben Hutchings 已提交
657 658
			state->offload_csum = (tx_queue->queue &
					       EFX_TXQ_TYPE_OFFLOAD);
B
Ben Hutchings 已提交
659 660
			rc = efx_test_loopback(tx_queue,
					       &tests->loopback[mode]);
661 662 663 664 665 666
			if (rc)
				goto out;
		}
	}

 out:
B
Ben Hutchings 已提交
667
	/* Remove the flush. The caller will remove the loopback setting */
668
	state->flush = true;
B
Ben Hutchings 已提交
669 670 671
	efx->loopback_selftest = NULL;
	wmb();
	kfree(state);
672

673 674 675
	if (rc == -EPERM)
		rc = 0;

676 677 678 679 680
	return rc;
}

/**************************************************************************
 *
681
 * Entry point
682 683 684
 *
 *************************************************************************/

685 686
int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
		 unsigned flags)
687
{
688 689
	enum efx_loopback_mode loopback_mode = efx->loopback_mode;
	int phy_mode = efx->phy_mode;
690
	int rc_test = 0, rc_reset, rc;
691

692 693
	efx_selftest_async_cancel(efx);

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

697
	rc = efx_test_phy_alive(efx, tests);
698 699
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
700 701

	rc = efx_test_nvram(efx, tests);
702 703
	if (rc && !rc_test)
		rc_test = rc;
704

B
Ben Hutchings 已提交
705
	rc = efx_test_interrupts(efx, tests);
706 707
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
708

709 710 711
	rc = efx_test_eventq_irq(efx, tests);
	if (rc && !rc_test)
		rc_test = rc;
B
Ben Hutchings 已提交
712

713 714
	if (rc_test)
		return rc_test;
715

716
	if (!(flags & ETH_TEST_FL_OFFLINE))
717
		return efx_test_phy(efx, tests, flags);
718 719 720

	/* Offline (i.e. disruptive) testing
	 * This checks MAC and PHY loopback on the specified port. */
B
Ben Hutchings 已提交
721

722 723
	/* Detach the device so the kernel doesn't transmit during the
	 * loopback test and the watchdog timeout doesn't fire.
B
Ben Hutchings 已提交
724
	 */
725
	efx_device_detach_sync(efx);
726

727 728 729 730 731 732 733 734
	if (efx->type->test_chip) {
		rc_reset = efx->type->test_chip(efx, tests);
		if (rc_reset) {
			netif_err(efx, hw, efx->net_dev,
				  "Unable to recover from chip test\n");
			efx_schedule_reset(efx, RESET_TYPE_DISABLE);
			return rc_reset;
		}
B
Ben Hutchings 已提交
735

736
		if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
737 738
			rc_test = -EIO;
	}
B
Ben Hutchings 已提交
739

740 741
	/* Ensure that the phy is powered and out of loopback
	 * for the bist and loopback tests */
742
	mutex_lock(&efx->mac_lock);
743
	efx->phy_mode &= ~PHY_MODE_LOW_POWER;
B
Ben Hutchings 已提交
744
	efx->loopback_mode = LOOPBACK_NONE;
745 746
	__efx_reconfigure_port(efx);
	mutex_unlock(&efx->mac_lock);
747

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

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

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

763
	netif_device_attach(efx->net_dev);
764

765
	return rc_test;
766 767
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
void efx_selftest_async_start(struct efx_nic *efx)
{
	struct efx_channel *channel;

	efx_for_each_channel(channel, efx)
		efx_nic_event_test_start(channel);
	schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
}

void efx_selftest_async_cancel(struct efx_nic *efx)
{
	cancel_delayed_work_sync(&efx->selftest_work);
}

void efx_selftest_async_work(struct work_struct *data)
{
	struct efx_nic *efx = container_of(data, struct efx_nic,
					   selftest_work.work);
	struct efx_channel *channel;
	int cpu;

	efx_for_each_channel(channel, efx) {
		cpu = efx_nic_event_test_irq_cpu(channel);
		if (cpu < 0)
			netif_err(efx, ifup, efx->net_dev,
				  "channel %d failed to trigger an interrupt\n",
				  channel->channel);
		else
			netif_dbg(efx, ifup, efx->net_dev,
				  "channel %d triggered interrupt on CPU %d\n",
				  channel->channel, cpu);
	}
}