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