]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/sfc/selftest.c
sfc: make functions static
[net-next-2.6.git] / drivers / net / sfc / selftest.c
CommitLineData
3273c2e8
BH
1/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
906bb26c 4 * Copyright 2006-2009 Solarflare Communications Inc.
3273c2e8
BH
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/module.h>
13#include <linux/delay.h>
14#include <linux/kernel_stat.h>
15#include <linux/pci.h>
16#include <linux/ethtool.h>
17#include <linux/ip.h>
18#include <linux/in.h>
19#include <linux/udp.h>
20#include <linux/rtnetlink.h>
5a0e3ad6 21#include <linux/slab.h>
3273c2e8
BH
22#include <asm/io.h>
23#include "net_driver.h"
3273c2e8 24#include "efx.h"
744093c9 25#include "nic.h"
3273c2e8 26#include "selftest.h"
3273c2e8 27#include "workarounds.h"
3273c2e8
BH
28
29/*
30 * Loopback test packet structure
31 *
32 * The self-test should stress every RSS vector, and unfortunately
33 * Falcon only performs RSS on TCP/UDP packets.
34 */
35struct efx_loopback_payload {
36 struct ethhdr header;
37 struct iphdr ip;
38 struct udphdr udp;
39 __be16 iteration;
40 const char msg[64];
ba2d3587 41} __packed;
3273c2e8
BH
42
43/* Loopback test source MAC address */
44static const unsigned char payload_source[ETH_ALEN] = {
45 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
46};
47
94de803d 48static const char payload_msg[] =
3273c2e8
BH
49 "Hello world! This is an Efx loopback test in progress!";
50
d215697f 51/* Interrupt mode names */
52static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
53static const char *efx_interrupt_mode_names[] = {
54 [EFX_INT_MODE_MSIX] = "MSI-X",
55 [EFX_INT_MODE_MSI] = "MSI",
56 [EFX_INT_MODE_LEGACY] = "legacy",
57};
58#define INT_MODE(efx) \
59 STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
60
3273c2e8 61/**
8c8661e4 62 * efx_loopback_state - persistent state during a loopback selftest
3273c2e8
BH
63 * @flush: Drop all packets in efx_loopback_rx_packet
64 * @packet_count: Number of packets being used in this test
65 * @skbs: An array of skbs transmitted
f30eb23e 66 * @offload_csum: Checksums are being offloaded
3273c2e8
BH
67 * @rx_good: RX good packet count
68 * @rx_bad: RX bad packet count
69 * @payload: Payload used in tests
70 */
8c8661e4 71struct efx_loopback_state {
dc8cfa55 72 bool flush;
3273c2e8
BH
73 int packet_count;
74 struct sk_buff **skbs;
dc8cfa55 75 bool offload_csum;
3273c2e8
BH
76 atomic_t rx_good;
77 atomic_t rx_bad;
78 struct efx_loopback_payload payload;
79};
80
81/**************************************************************************
82 *
8c8661e4 83 * MII, NVRAM and register tests
3273c2e8
BH
84 *
85 **************************************************************************/
86
4f16c073 87static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
8c8661e4
BH
88{
89 int rc = 0;
8c8661e4 90
4f16c073
BH
91 if (efx->phy_op->test_alive) {
92 rc = efx->phy_op->test_alive(efx);
93 tests->phy_alive = rc ? -1 : 1;
8c8661e4
BH
94 }
95
8c8661e4
BH
96 return rc;
97}
98
99static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
100{
0aa3fbaa
BH
101 int rc = 0;
102
103 if (efx->type->test_nvram) {
104 rc = efx->type->test_nvram(efx);
105 tests->nvram = rc ? -1 : 1;
106 }
8c8661e4 107
8c8661e4
BH
108 return rc;
109}
110
111static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
112{
9bfc4bb1 113 int rc = 0;
8c8661e4 114
9bfc4bb1
BH
115 /* Test register access */
116 if (efx->type->test_registers) {
117 rc = efx->type->test_registers(efx);
118 tests->registers = rc ? -1 : 1;
119 }
8c8661e4 120
8c8661e4
BH
121 return rc;
122}
3273c2e8
BH
123
124/**************************************************************************
125 *
126 * Interrupt and event queue testing
127 *
128 **************************************************************************/
129
130/* Test generation and receipt of interrupts */
131static int efx_test_interrupts(struct efx_nic *efx,
132 struct efx_self_tests *tests)
133{
134 struct efx_channel *channel;
135
62776d03 136 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
3273c2e8
BH
137 tests->interrupt = -1;
138
139 /* Reset interrupt flag */
140 efx->last_irq_cpu = -1;
141 smp_wmb();
142
143 /* ACK each interrupting event queue. Receiving an interrupt due to
144 * traffic before a test event is raised is considered a pass */
64ee3120 145 efx_for_each_channel(channel, efx) {
3273c2e8
BH
146 if (channel->work_pending)
147 efx_process_channel_now(channel);
148 if (efx->last_irq_cpu >= 0)
149 goto success;
150 }
151
152b6a62 152 efx_nic_generate_interrupt(efx);
3273c2e8
BH
153
154 /* Wait for arrival of test interrupt. */
62776d03 155 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
3273c2e8
BH
156 schedule_timeout_uninterruptible(HZ / 10);
157 if (efx->last_irq_cpu >= 0)
158 goto success;
159
62776d03 160 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
3273c2e8
BH
161 return -ETIMEDOUT;
162
163 success:
62776d03
BH
164 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
165 INT_MODE(efx),
c459302d 166 efx->last_irq_cpu);
3273c2e8
BH
167 tests->interrupt = 1;
168 return 0;
169}
170
3273c2e8
BH
171/* Test generation and receipt of interrupting events */
172static int efx_test_eventq_irq(struct efx_channel *channel,
173 struct efx_self_tests *tests)
174{
62776d03 175 struct efx_nic *efx = channel->efx;
d730dc52 176 unsigned int magic_count, count;
3273c2e8
BH
177
178 tests->eventq_dma[channel->channel] = -1;
179 tests->eventq_int[channel->channel] = -1;
180 tests->eventq_poll[channel->channel] = -1;
181
d730dc52 182 magic_count = channel->magic_count;
3273c2e8 183 channel->efx->last_irq_cpu = -1;
3273c2e8
BH
184 smp_wmb();
185
d730dc52 186 efx_nic_generate_test_event(channel);
3273c2e8
BH
187
188 /* Wait for arrival of interrupt */
189 count = 0;
190 do {
191 schedule_timeout_uninterruptible(HZ / 100);
192
193 if (channel->work_pending)
194 efx_process_channel_now(channel);
195
d730dc52 196 if (channel->magic_count != magic_count)
3273c2e8
BH
197 goto eventq_ok;
198 } while (++count < 2);
199
62776d03
BH
200 netif_err(efx, drv, efx->net_dev,
201 "channel %d timed out waiting for event queue\n",
202 channel->channel);
3273c2e8
BH
203
204 /* See if interrupt arrived */
205 if (channel->efx->last_irq_cpu >= 0) {
62776d03
BH
206 netif_err(efx, drv, efx->net_dev,
207 "channel %d saw interrupt on CPU%d "
208 "during event queue test\n", channel->channel,
209 raw_smp_processor_id());
3273c2e8
BH
210 tests->eventq_int[channel->channel] = 1;
211 }
212
213 /* Check to see if event was received even if interrupt wasn't */
214 efx_process_channel_now(channel);
d730dc52 215 if (channel->magic_count != magic_count) {
62776d03
BH
216 netif_err(efx, drv, efx->net_dev,
217 "channel %d event was generated, but "
218 "failed to trigger an interrupt\n", channel->channel);
3273c2e8
BH
219 tests->eventq_dma[channel->channel] = 1;
220 }
221
222 return -ETIMEDOUT;
223 eventq_ok:
62776d03
BH
224 netif_dbg(efx, drv, efx->net_dev, "channel %d event queue passed\n",
225 channel->channel);
3273c2e8
BH
226 tests->eventq_dma[channel->channel] = 1;
227 tests->eventq_int[channel->channel] = 1;
228 tests->eventq_poll[channel->channel] = 1;
229 return 0;
230}
231
1796721a
BH
232static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
233 unsigned flags)
3273c2e8 234{
8c8661e4 235 int rc;
3273c2e8 236
1796721a 237 if (!efx->phy_op->run_tests)
3273c2e8 238 return 0;
3273c2e8 239
8c8661e4 240 mutex_lock(&efx->mac_lock);
4f16c073 241 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
8c8661e4 242 mutex_unlock(&efx->mac_lock);
8c8661e4 243 return rc;
3273c2e8
BH
244}
245
246/**************************************************************************
247 *
248 * Loopback testing
249 * NB Only one loopback test can be executing concurrently.
250 *
251 **************************************************************************/
252
253/* Loopback test RX callback
254 * This is called for each received packet during loopback testing.
255 */
256void efx_loopback_rx_packet(struct efx_nic *efx,
257 const char *buf_ptr, int pkt_len)
258{
8c8661e4 259 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
260 struct efx_loopback_payload *received;
261 struct efx_loopback_payload *payload;
262
263 BUG_ON(!buf_ptr);
264
265 /* If we are just flushing, then drop the packet */
266 if ((state == NULL) || state->flush)
267 return;
268
269 payload = &state->payload;
8c8661e4 270
d3208b5e 271 received = (struct efx_loopback_payload *) buf_ptr;
3273c2e8 272 received->ip.saddr = payload->ip.saddr;
60ac1065
BH
273 if (state->offload_csum)
274 received->ip.check = payload->ip.check;
275
3273c2e8
BH
276 /* Check that header exists */
277 if (pkt_len < sizeof(received->header)) {
62776d03
BH
278 netif_err(efx, drv, efx->net_dev,
279 "saw runt RX packet (length %d) in %s loopback "
280 "test\n", pkt_len, LOOPBACK_MODE(efx));
3273c2e8
BH
281 goto err;
282 }
283
284 /* Check that the ethernet header exists */
285 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
62776d03
BH
286 netif_err(efx, drv, efx->net_dev,
287 "saw non-loopback RX packet in %s loopback test\n",
288 LOOPBACK_MODE(efx));
3273c2e8
BH
289 goto err;
290 }
291
292 /* Check packet length */
293 if (pkt_len != sizeof(*payload)) {
62776d03
BH
294 netif_err(efx, drv, efx->net_dev,
295 "saw incorrect RX packet length %d (wanted %d) in "
296 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
297 LOOPBACK_MODE(efx));
3273c2e8
BH
298 goto err;
299 }
300
301 /* Check that IP header matches */
302 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
62776d03
BH
303 netif_err(efx, drv, efx->net_dev,
304 "saw corrupted IP header in %s loopback test\n",
305 LOOPBACK_MODE(efx));
3273c2e8
BH
306 goto err;
307 }
308
309 /* Check that msg and padding matches */
310 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
62776d03
BH
311 netif_err(efx, drv, efx->net_dev,
312 "saw corrupted RX packet in %s loopback test\n",
313 LOOPBACK_MODE(efx));
3273c2e8
BH
314 goto err;
315 }
316
317 /* Check that iteration matches */
318 if (received->iteration != payload->iteration) {
62776d03
BH
319 netif_err(efx, drv, efx->net_dev,
320 "saw RX packet from iteration %d (wanted %d) in "
321 "%s loopback test\n", ntohs(received->iteration),
322 ntohs(payload->iteration), LOOPBACK_MODE(efx));
3273c2e8
BH
323 goto err;
324 }
325
326 /* Increase correct RX count */
62776d03
BH
327 netif_vdbg(efx, drv, efx->net_dev,
328 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
3273c2e8
BH
329
330 atomic_inc(&state->rx_good);
331 return;
332
333 err:
334#ifdef EFX_ENABLE_DEBUG
335 if (atomic_read(&state->rx_bad) == 0) {
62776d03 336 netif_err(efx, drv, efx->net_dev, "received packet:\n");
3273c2e8
BH
337 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
338 buf_ptr, pkt_len, 0);
62776d03 339 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
3273c2e8
BH
340 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
341 &state->payload, sizeof(state->payload), 0);
342 }
343#endif
344 atomic_inc(&state->rx_bad);
345}
346
347/* Initialise an efx_selftest_state for a new iteration */
348static void efx_iterate_state(struct efx_nic *efx)
349{
8c8661e4 350 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
351 struct net_device *net_dev = efx->net_dev;
352 struct efx_loopback_payload *payload = &state->payload;
353
354 /* Initialise the layerII header */
355 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
356 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
357 payload->header.h_proto = htons(ETH_P_IP);
358
359 /* saddr set later and used as incrementing count */
360 payload->ip.daddr = htonl(INADDR_LOOPBACK);
361 payload->ip.ihl = 5;
362 payload->ip.check = htons(0xdead);
363 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
364 payload->ip.version = IPVERSION;
365 payload->ip.protocol = IPPROTO_UDP;
366
367 /* Initialise udp header */
368 payload->udp.source = 0;
369 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
370 sizeof(struct iphdr));
371 payload->udp.check = 0; /* checksum ignored */
372
373 /* Fill out payload */
374 payload->iteration = htons(ntohs(payload->iteration) + 1);
375 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
376
377 /* Fill out remaining state members */
378 atomic_set(&state->rx_good, 0);
379 atomic_set(&state->rx_bad, 0);
380 smp_wmb();
381}
382
b9aafb0e 383static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
3273c2e8
BH
384{
385 struct efx_nic *efx = tx_queue->efx;
8c8661e4 386 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
387 struct efx_loopback_payload *payload;
388 struct sk_buff *skb;
61357325
SH
389 int i;
390 netdev_tx_t rc;
3273c2e8
BH
391
392 /* Transmit N copies of buffer */
393 for (i = 0; i < state->packet_count; i++) {
8c8661e4 394 /* Allocate an skb, holding an extra reference for
3273c2e8
BH
395 * transmit completion counting */
396 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
397 if (!skb)
398 return -ENOMEM;
399 state->skbs[i] = skb;
400 skb_get(skb);
401
402 /* Copy the payload in, incrementing the source address to
403 * exercise the rss vectors */
404 payload = ((struct efx_loopback_payload *)
405 skb_put(skb, sizeof(state->payload)));
406 memcpy(payload, &state->payload, sizeof(state->payload));
407 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
408
409 /* Ensure everything we've written is visible to the
410 * interrupt handler. */
411 smp_wmb();
412
55668611 413 if (efx_dev_registered(efx))
3273c2e8 414 netif_tx_lock_bh(efx->net_dev);
497f5ba3 415 rc = efx_enqueue_skb(tx_queue, skb);
55668611 416 if (efx_dev_registered(efx))
3273c2e8
BH
417 netif_tx_unlock_bh(efx->net_dev);
418
419 if (rc != NETDEV_TX_OK) {
62776d03
BH
420 netif_err(efx, drv, efx->net_dev,
421 "TX queue %d could not transmit packet %d of "
422 "%d in %s loopback test\n", tx_queue->queue,
423 i + 1, state->packet_count,
424 LOOPBACK_MODE(efx));
3273c2e8
BH
425
426 /* Defer cleaning up the other skbs for the caller */
427 kfree_skb(skb);
428 return -EPIPE;
429 }
430 }
431
432 return 0;
433}
434
b9aafb0e
BH
435static int efx_poll_loopback(struct efx_nic *efx)
436{
8c8661e4 437 struct efx_loopback_state *state = efx->loopback_selftest;
b9aafb0e
BH
438 struct efx_channel *channel;
439
440 /* NAPI polling is not enabled, so process channels
441 * synchronously */
64ee3120 442 efx_for_each_channel(channel, efx) {
b9aafb0e
BH
443 if (channel->work_pending)
444 efx_process_channel_now(channel);
445 }
446 return atomic_read(&state->rx_good) == state->packet_count;
447}
448
449static int efx_end_loopback(struct efx_tx_queue *tx_queue,
450 struct efx_loopback_self_tests *lb_tests)
3273c2e8
BH
451{
452 struct efx_nic *efx = tx_queue->efx;
8c8661e4 453 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
454 struct sk_buff *skb;
455 int tx_done = 0, rx_good, rx_bad;
456 int i, rc = 0;
457
55668611 458 if (efx_dev_registered(efx))
3273c2e8
BH
459 netif_tx_lock_bh(efx->net_dev);
460
461 /* Count the number of tx completions, and decrement the refcnt. Any
462 * skbs not already completed will be free'd when the queue is flushed */
463 for (i=0; i < state->packet_count; i++) {
464 skb = state->skbs[i];
465 if (skb && !skb_shared(skb))
466 ++tx_done;
467 dev_kfree_skb_any(skb);
468 }
469
55668611 470 if (efx_dev_registered(efx))
3273c2e8
BH
471 netif_tx_unlock_bh(efx->net_dev);
472
473 /* Check TX completion and received packet counts */
474 rx_good = atomic_read(&state->rx_good);
475 rx_bad = atomic_read(&state->rx_bad);
476 if (tx_done != state->packet_count) {
477 /* Don't free the skbs; they will be picked up on TX
478 * overflow or channel teardown.
479 */
62776d03
BH
480 netif_err(efx, drv, efx->net_dev,
481 "TX queue %d saw only %d out of an expected %d "
482 "TX completion events in %s loopback test\n",
483 tx_queue->queue, tx_done, state->packet_count,
484 LOOPBACK_MODE(efx));
3273c2e8
BH
485 rc = -ETIMEDOUT;
486 /* Allow to fall through so we see the RX errors as well */
487 }
488
489 /* We may always be up to a flush away from our desired packet total */
490 if (rx_good != state->packet_count) {
62776d03
BH
491 netif_dbg(efx, drv, efx->net_dev,
492 "TX queue %d saw only %d out of an expected %d "
493 "received packets in %s loopback test\n",
494 tx_queue->queue, rx_good, state->packet_count,
495 LOOPBACK_MODE(efx));
3273c2e8
BH
496 rc = -ETIMEDOUT;
497 /* Fall through */
498 }
499
500 /* Update loopback test structure */
501 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
502 lb_tests->tx_done[tx_queue->queue] += tx_done;
503 lb_tests->rx_good += rx_good;
504 lb_tests->rx_bad += rx_bad;
505
506 return rc;
507}
508
509static int
510efx_test_loopback(struct efx_tx_queue *tx_queue,
511 struct efx_loopback_self_tests *lb_tests)
512{
513 struct efx_nic *efx = tx_queue->efx;
8c8661e4 514 struct efx_loopback_state *state = efx->loopback_selftest;
b9aafb0e 515 int i, begin_rc, end_rc;
3273c2e8 516
8c8661e4 517 for (i = 0; i < 3; i++) {
3273c2e8 518 /* Determine how many packets to send */
ecc910f5 519 state->packet_count = efx->txq_entries / 3;
3273c2e8
BH
520 state->packet_count = min(1 << (i << 2), state->packet_count);
521 state->skbs = kzalloc(sizeof(state->skbs[0]) *
522 state->packet_count, GFP_KERNEL);
9b7bfc4c
BH
523 if (!state->skbs)
524 return -ENOMEM;
dc8cfa55 525 state->flush = false;
3273c2e8 526
62776d03
BH
527 netif_dbg(efx, drv, efx->net_dev,
528 "TX queue %d testing %s loopback with %d packets\n",
529 tx_queue->queue, LOOPBACK_MODE(efx),
530 state->packet_count);
3273c2e8
BH
531
532 efx_iterate_state(efx);
b9aafb0e
BH
533 begin_rc = efx_begin_loopback(tx_queue);
534
535 /* This will normally complete very quickly, but be
536 * prepared to wait up to 100 ms. */
537 msleep(1);
538 if (!efx_poll_loopback(efx)) {
539 msleep(100);
540 efx_poll_loopback(efx);
3273c2e8
BH
541 }
542
b9aafb0e 543 end_rc = efx_end_loopback(tx_queue, lb_tests);
3273c2e8
BH
544 kfree(state->skbs);
545
b9aafb0e 546 if (begin_rc || end_rc) {
3273c2e8
BH
547 /* Wait a while to ensure there are no packets
548 * floating around after a failure. */
549 schedule_timeout_uninterruptible(HZ / 10);
b9aafb0e 550 return begin_rc ? begin_rc : end_rc;
3273c2e8
BH
551 }
552 }
553
62776d03
BH
554 netif_dbg(efx, drv, efx->net_dev,
555 "TX queue %d passed %s loopback test with a burst length "
556 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
557 state->packet_count);
3273c2e8 558
a0c2c190 559 return 0;
3273c2e8
BH
560}
561
78c1f0a0
SH
562/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
563 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
564 * to delay and retry. Therefore, it's safer to just poll directly. Wait
565 * for link up and any faults to dissipate. */
566static int efx_wait_for_link(struct efx_nic *efx)
567{
568 struct efx_link_state *link_state = &efx->link_state;
901d3fe8 569 int count, link_up_count = 0;
78c1f0a0
SH
570 bool link_up;
571
572 for (count = 0; count < 40; count++) {
573 schedule_timeout_uninterruptible(HZ / 10);
574
575 if (efx->type->monitor != NULL) {
576 mutex_lock(&efx->mac_lock);
577 efx->type->monitor(efx);
578 mutex_unlock(&efx->mac_lock);
579 } else {
f7d12cdc 580 struct efx_channel *channel = efx_get_channel(efx, 0);
78c1f0a0
SH
581 if (channel->work_pending)
582 efx_process_channel_now(channel);
583 }
584
585 mutex_lock(&efx->mac_lock);
586 link_up = link_state->up;
587 if (link_up)
588 link_up = !efx->mac_op->check_fault(efx);
589 mutex_unlock(&efx->mac_lock);
590
901d3fe8
SH
591 if (link_up) {
592 if (++link_up_count == 2)
593 return 0;
594 } else {
595 link_up_count = 0;
596 }
78c1f0a0
SH
597 }
598
599 return -ETIMEDOUT;
600}
601
a5692e49 602static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
3273c2e8
BH
603 unsigned int loopback_modes)
604{
8c8661e4
BH
605 enum efx_loopback_mode mode;
606 struct efx_loopback_state *state;
f7d12cdc 607 struct efx_channel *channel = efx_get_channel(efx, 0);
3273c2e8 608 struct efx_tx_queue *tx_queue;
78c1f0a0 609 int rc = 0;
3273c2e8 610
8c8661e4
BH
611 /* Set the port loopback_selftest member. From this point on
612 * all received packets will be dropped. Mark the state as
613 * "flushing" so all inflight packets are dropped */
614 state = kzalloc(sizeof(*state), GFP_KERNEL);
615 if (state == NULL)
616 return -ENOMEM;
617 BUG_ON(efx->loopback_selftest);
618 state->flush = true;
619 efx->loopback_selftest = state;
3273c2e8
BH
620
621 /* Test all supported loopback modes */
8c8661e4 622 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
3273c2e8
BH
623 if (!(loopback_modes & (1 << mode)))
624 continue;
625
626 /* Move the port into the specified loopback mode. */
dc8cfa55 627 state->flush = true;
78c1f0a0 628 mutex_lock(&efx->mac_lock);
3273c2e8 629 efx->loopback_mode = mode;
78c1f0a0
SH
630 rc = __efx_reconfigure_port(efx);
631 mutex_unlock(&efx->mac_lock);
632 if (rc) {
62776d03
BH
633 netif_err(efx, drv, efx->net_dev,
634 "unable to move into %s loopback\n",
635 LOOPBACK_MODE(efx));
78c1f0a0
SH
636 goto out;
637 }
3273c2e8 638
78c1f0a0
SH
639 rc = efx_wait_for_link(efx);
640 if (rc) {
62776d03
BH
641 netif_err(efx, drv, efx->net_dev,
642 "loopback %s never came up\n",
643 LOOPBACK_MODE(efx));
3273c2e8
BH
644 goto out;
645 }
646
5298c37f 647 /* Test both types of TX queue */
f7d12cdc 648 efx_for_each_channel_tx_queue(tx_queue, channel) {
a4900ac9
BH
649 state->offload_csum = (tx_queue->queue &
650 EFX_TXQ_TYPE_OFFLOAD);
f8ea0b67
BH
651 rc = efx_test_loopback(tx_queue,
652 &tests->loopback[mode]);
3273c2e8
BH
653 if (rc)
654 goto out;
655 }
656 }
657
658 out:
8c8661e4 659 /* Remove the flush. The caller will remove the loopback setting */
dc8cfa55 660 state->flush = true;
8c8661e4
BH
661 efx->loopback_selftest = NULL;
662 wmb();
663 kfree(state);
3273c2e8
BH
664
665 return rc;
666}
667
668/**************************************************************************
669 *
2ef3068e 670 * Entry point
3273c2e8
BH
671 *
672 *************************************************************************/
673
2ef3068e
BH
674int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
675 unsigned flags)
3273c2e8 676{
2ef3068e
BH
677 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
678 int phy_mode = efx->phy_mode;
4b988280 679 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
3273c2e8 680 struct efx_channel *channel;
2ef3068e
BH
681 int rc_test = 0, rc_reset = 0, rc;
682
683 /* Online (i.e. non-disruptive) testing
684 * This checks interrupt generation, event delivery and PHY presence. */
8c8661e4 685
4f16c073 686 rc = efx_test_phy_alive(efx, tests);
2ef3068e
BH
687 if (rc && !rc_test)
688 rc_test = rc;
8c8661e4
BH
689
690 rc = efx_test_nvram(efx, tests);
2ef3068e
BH
691 if (rc && !rc_test)
692 rc_test = rc;
3273c2e8 693
f8ea0b67 694 rc = efx_test_interrupts(efx, tests);
2ef3068e
BH
695 if (rc && !rc_test)
696 rc_test = rc;
8c8661e4 697
3273c2e8 698 efx_for_each_channel(channel, efx) {
64ee3120 699 rc = efx_test_eventq_irq(channel, tests);
2ef3068e
BH
700 if (rc && !rc_test)
701 rc_test = rc;
3273c2e8 702 }
8c8661e4 703
2ef3068e
BH
704 if (rc_test)
705 return rc_test;
3273c2e8 706
2ef3068e 707 if (!(flags & ETH_TEST_FL_OFFLINE))
1796721a 708 return efx_test_phy(efx, tests, flags);
2ef3068e
BH
709
710 /* Offline (i.e. disruptive) testing
711 * This checks MAC and PHY loopback on the specified port. */
8c8661e4
BH
712
713 /* force the carrier state off so the kernel doesn't transmit during
714 * the loopback test, and the watchdog timeout doesn't fire. Also put
715 * falcon into loopback for the register test.
716 */
717 mutex_lock(&efx->mac_lock);
718 efx->port_inhibited = true;
6f158d5f
BH
719 if (efx->loopback_modes) {
720 /* We need the 312 clock from the PHY to test the XMAC
721 * registers, so move into XGMII loopback if available */
722 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
723 efx->loopback_mode = LOOPBACK_XGMII;
724 else
725 efx->loopback_mode = __ffs(efx->loopback_modes);
726 }
727
8c8661e4
BH
728 __efx_reconfigure_port(efx);
729 mutex_unlock(&efx->mac_lock);
730
731 /* free up all consumers of SRAM (including all the queues) */
d3245b28 732 efx_reset_down(efx, reset_method);
8c8661e4
BH
733
734 rc = efx_test_chip(efx, tests);
2ef3068e
BH
735 if (rc && !rc_test)
736 rc_test = rc;
8c8661e4
BH
737
738 /* reset the chip to recover from the register test */
ef2b90ee 739 rc_reset = efx->type->reset(efx, reset_method);
8c8661e4 740
a5692e49
BH
741 /* Ensure that the phy is powered and out of loopback
742 * for the bist and loopback tests */
743 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
8c8661e4 744 efx->loopback_mode = LOOPBACK_NONE;
3273c2e8 745
d3245b28 746 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
2ef3068e
BH
747 if (rc && !rc_reset)
748 rc_reset = rc;
749
750 if (rc_reset) {
62776d03
BH
751 netif_err(efx, drv, efx->net_dev,
752 "Unable to recover from chip test\n");
8c8661e4 753 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
2ef3068e 754 return rc_reset;
8c8661e4 755 }
3273c2e8 756
1796721a 757 rc = efx_test_phy(efx, tests, flags);
2ef3068e
BH
758 if (rc && !rc_test)
759 rc_test = rc;
3273c2e8 760
2ef3068e
BH
761 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
762 if (rc && !rc_test)
763 rc_test = rc;
3273c2e8 764
8c8661e4 765 /* restore the PHY to the previous state */
d3245b28 766 mutex_lock(&efx->mac_lock);
8c8661e4
BH
767 efx->phy_mode = phy_mode;
768 efx->port_inhibited = false;
d3245b28
BH
769 efx->loopback_mode = loopback_mode;
770 __efx_reconfigure_port(efx);
771 mutex_unlock(&efx->mac_lock);
8c8661e4 772
2ef3068e 773 return rc_test;
3273c2e8
BH
774}
775