blob: 2f7def657679ba58789ea5294053d04eef8b9311 [file] [log] [blame]
Ben Hutchings3273c2e2008-05-07 13:36:19 +01001/****************************************************************************
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"
23#include "ethtool.h"
24#include "efx.h"
25#include "falcon.h"
26#include "selftest.h"
27#include "boards.h"
28#include "workarounds.h"
29#include "mac.h"
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/**
54 * efx_selftest_state - persistent state during a selftest
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
58 * @rx_good: RX good packet count
59 * @rx_bad: RX bad packet count
60 * @payload: Payload used in tests
61 */
62struct efx_selftest_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010063 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010064 int packet_count;
65 struct sk_buff **skbs;
Ben Hutchings60ac1062008-09-01 12:44:59 +010066
67 /* Checksums are being offloaded */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010068 bool offload_csum;
Ben Hutchings60ac1062008-09-01 12:44:59 +010069
Ben Hutchings3273c2e2008-05-07 13:36:19 +010070 atomic_t rx_good;
71 atomic_t rx_bad;
72 struct efx_loopback_payload payload;
73};
74
75/**************************************************************************
76 *
77 * Configurable values
78 *
79 **************************************************************************/
80
81/* Level of loopback testing
82 *
83 * The maximum packet burst length is 16**(n-1), i.e.
84 *
85 * - Level 0 : no packets
86 * - Level 1 : 1 packet
87 * - Level 2 : 17 packets (1 * 1 packet, 1 * 16 packets)
88 * - Level 3 : 273 packets (1 * 1 packet, 1 * 16 packet, 1 * 256 packets)
89 *
90 */
91static unsigned int loopback_test_level = 3;
92
93/**************************************************************************
94 *
95 * Interrupt and event queue testing
96 *
97 **************************************************************************/
98
99/* Test generation and receipt of interrupts */
100static int efx_test_interrupts(struct efx_nic *efx,
101 struct efx_self_tests *tests)
102{
103 struct efx_channel *channel;
104
105 EFX_LOG(efx, "testing interrupts\n");
106 tests->interrupt = -1;
107
108 /* Reset interrupt flag */
109 efx->last_irq_cpu = -1;
110 smp_wmb();
111
112 /* ACK each interrupting event queue. Receiving an interrupt due to
113 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100114 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100115 if (channel->work_pending)
116 efx_process_channel_now(channel);
117 if (efx->last_irq_cpu >= 0)
118 goto success;
119 }
120
121 falcon_generate_interrupt(efx);
122
123 /* Wait for arrival of test interrupt. */
124 EFX_LOG(efx, "waiting for test interrupt\n");
125 schedule_timeout_uninterruptible(HZ / 10);
126 if (efx->last_irq_cpu >= 0)
127 goto success;
128
129 EFX_ERR(efx, "timed out waiting for interrupt\n");
130 return -ETIMEDOUT;
131
132 success:
133 EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
134 efx->interrupt_mode, efx->last_irq_cpu);
135 tests->interrupt = 1;
136 return 0;
137}
138
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100139/* Test generation and receipt of interrupting events */
140static int efx_test_eventq_irq(struct efx_channel *channel,
141 struct efx_self_tests *tests)
142{
143 unsigned int magic, count;
144
145 /* Channel specific code, limited to 20 bits */
146 magic = (0x00010150 + channel->channel);
147 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
148 channel->channel, magic);
149
150 tests->eventq_dma[channel->channel] = -1;
151 tests->eventq_int[channel->channel] = -1;
152 tests->eventq_poll[channel->channel] = -1;
153
154 /* Reset flag and zero magic word */
155 channel->efx->last_irq_cpu = -1;
156 channel->eventq_magic = 0;
157 smp_wmb();
158
159 falcon_generate_test_event(channel, magic);
160
161 /* Wait for arrival of interrupt */
162 count = 0;
163 do {
164 schedule_timeout_uninterruptible(HZ / 100);
165
166 if (channel->work_pending)
167 efx_process_channel_now(channel);
168
169 if (channel->eventq_magic == magic)
170 goto eventq_ok;
171 } while (++count < 2);
172
173 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
174 channel->channel);
175
176 /* See if interrupt arrived */
177 if (channel->efx->last_irq_cpu >= 0) {
178 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
179 "during event queue test\n", channel->channel,
180 raw_smp_processor_id());
181 tests->eventq_int[channel->channel] = 1;
182 }
183
184 /* Check to see if event was received even if interrupt wasn't */
185 efx_process_channel_now(channel);
186 if (channel->eventq_magic == magic) {
187 EFX_ERR(channel->efx, "channel %d event was generated, but "
188 "failed to trigger an interrupt\n", channel->channel);
189 tests->eventq_dma[channel->channel] = 1;
190 }
191
192 return -ETIMEDOUT;
193 eventq_ok:
194 EFX_LOG(channel->efx, "channel %d event queue passed\n",
195 channel->channel);
196 tests->eventq_dma[channel->channel] = 1;
197 tests->eventq_int[channel->channel] = 1;
198 tests->eventq_poll[channel->channel] = 1;
199 return 0;
200}
201
202/**************************************************************************
203 *
204 * PHY testing
205 *
206 **************************************************************************/
207
208/* Check PHY presence by reading the PHY ID registers */
209static int efx_test_phy(struct efx_nic *efx,
210 struct efx_self_tests *tests)
211{
212 u16 physid1, physid2;
213 struct mii_if_info *mii = &efx->mii;
214 struct net_device *net_dev = efx->net_dev;
215
216 if (efx->phy_type == PHY_TYPE_NONE)
217 return 0;
218
219 EFX_LOG(efx, "testing PHY presence\n");
220 tests->phy_ok = -1;
221
222 physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
223 physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
224
225 if ((physid1 != 0x0000) && (physid1 != 0xffff) &&
226 (physid2 != 0x0000) && (physid2 != 0xffff)) {
227 EFX_LOG(efx, "found MII PHY %d ID 0x%x:%x\n",
228 mii->phy_id, physid1, physid2);
229 tests->phy_ok = 1;
230 return 0;
231 }
232
233 EFX_ERR(efx, "no MII PHY present with ID %d\n", mii->phy_id);
234 return -ENODEV;
235}
236
237/**************************************************************************
238 *
239 * Loopback testing
240 * NB Only one loopback test can be executing concurrently.
241 *
242 **************************************************************************/
243
244/* Loopback test RX callback
245 * This is called for each received packet during loopback testing.
246 */
247void efx_loopback_rx_packet(struct efx_nic *efx,
248 const char *buf_ptr, int pkt_len)
249{
250 struct efx_selftest_state *state = efx->loopback_selftest;
251 struct efx_loopback_payload *received;
252 struct efx_loopback_payload *payload;
253
254 BUG_ON(!buf_ptr);
255
256 /* If we are just flushing, then drop the packet */
257 if ((state == NULL) || state->flush)
258 return;
259
260 payload = &state->payload;
261
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100262 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100263 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100264 if (state->offload_csum)
265 received->ip.check = payload->ip.check;
266
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100267 /* Check that header exists */
268 if (pkt_len < sizeof(received->header)) {
269 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
270 "test\n", pkt_len, LOOPBACK_MODE(efx));
271 goto err;
272 }
273
274 /* Check that the ethernet header exists */
275 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
276 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
277 LOOPBACK_MODE(efx));
278 goto err;
279 }
280
281 /* Check packet length */
282 if (pkt_len != sizeof(*payload)) {
283 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
284 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
285 LOOPBACK_MODE(efx));
286 goto err;
287 }
288
289 /* Check that IP header matches */
290 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
291 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
292 LOOPBACK_MODE(efx));
293 goto err;
294 }
295
296 /* Check that msg and padding matches */
297 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
298 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
299 LOOPBACK_MODE(efx));
300 goto err;
301 }
302
303 /* Check that iteration matches */
304 if (received->iteration != payload->iteration) {
305 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
306 "%s loopback test\n", ntohs(received->iteration),
307 ntohs(payload->iteration), LOOPBACK_MODE(efx));
308 goto err;
309 }
310
311 /* Increase correct RX count */
312 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
313 LOOPBACK_MODE(efx));
314
315 atomic_inc(&state->rx_good);
316 return;
317
318 err:
319#ifdef EFX_ENABLE_DEBUG
320 if (atomic_read(&state->rx_bad) == 0) {
321 EFX_ERR(efx, "received packet:\n");
322 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
323 buf_ptr, pkt_len, 0);
324 EFX_ERR(efx, "expected packet:\n");
325 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
326 &state->payload, sizeof(state->payload), 0);
327 }
328#endif
329 atomic_inc(&state->rx_bad);
330}
331
332/* Initialise an efx_selftest_state for a new iteration */
333static void efx_iterate_state(struct efx_nic *efx)
334{
335 struct efx_selftest_state *state = efx->loopback_selftest;
336 struct net_device *net_dev = efx->net_dev;
337 struct efx_loopback_payload *payload = &state->payload;
338
339 /* Initialise the layerII header */
340 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
341 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
342 payload->header.h_proto = htons(ETH_P_IP);
343
344 /* saddr set later and used as incrementing count */
345 payload->ip.daddr = htonl(INADDR_LOOPBACK);
346 payload->ip.ihl = 5;
347 payload->ip.check = htons(0xdead);
348 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
349 payload->ip.version = IPVERSION;
350 payload->ip.protocol = IPPROTO_UDP;
351
352 /* Initialise udp header */
353 payload->udp.source = 0;
354 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
355 sizeof(struct iphdr));
356 payload->udp.check = 0; /* checksum ignored */
357
358 /* Fill out payload */
359 payload->iteration = htons(ntohs(payload->iteration) + 1);
360 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
361
362 /* Fill out remaining state members */
363 atomic_set(&state->rx_good, 0);
364 atomic_set(&state->rx_bad, 0);
365 smp_wmb();
366}
367
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100368static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100369{
370 struct efx_nic *efx = tx_queue->efx;
371 struct efx_selftest_state *state = efx->loopback_selftest;
372 struct efx_loopback_payload *payload;
373 struct sk_buff *skb;
374 int i, rc;
375
376 /* Transmit N copies of buffer */
377 for (i = 0; i < state->packet_count; i++) {
378 /* Allocate an skb, holding an extra reference for
379 * transmit completion counting */
380 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
381 if (!skb)
382 return -ENOMEM;
383 state->skbs[i] = skb;
384 skb_get(skb);
385
386 /* Copy the payload in, incrementing the source address to
387 * exercise the rss vectors */
388 payload = ((struct efx_loopback_payload *)
389 skb_put(skb, sizeof(state->payload)));
390 memcpy(payload, &state->payload, sizeof(state->payload));
391 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
392
393 /* Ensure everything we've written is visible to the
394 * interrupt handler. */
395 smp_wmb();
396
Ben Hutchings55668612008-05-16 21:16:10 +0100397 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100398 netif_tx_lock_bh(efx->net_dev);
399 rc = efx_xmit(efx, tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100400 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100401 netif_tx_unlock_bh(efx->net_dev);
402
403 if (rc != NETDEV_TX_OK) {
404 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
405 "of %d in %s loopback test\n", tx_queue->queue,
406 i + 1, state->packet_count, LOOPBACK_MODE(efx));
407
408 /* Defer cleaning up the other skbs for the caller */
409 kfree_skb(skb);
410 return -EPIPE;
411 }
412 }
413
414 return 0;
415}
416
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100417static int efx_poll_loopback(struct efx_nic *efx)
418{
419 struct efx_selftest_state *state = efx->loopback_selftest;
420 struct efx_channel *channel;
421
422 /* NAPI polling is not enabled, so process channels
423 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100424 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100425 if (channel->work_pending)
426 efx_process_channel_now(channel);
427 }
428 return atomic_read(&state->rx_good) == state->packet_count;
429}
430
431static int efx_end_loopback(struct efx_tx_queue *tx_queue,
432 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100433{
434 struct efx_nic *efx = tx_queue->efx;
435 struct efx_selftest_state *state = efx->loopback_selftest;
436 struct sk_buff *skb;
437 int tx_done = 0, rx_good, rx_bad;
438 int i, rc = 0;
439
Ben Hutchings55668612008-05-16 21:16:10 +0100440 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100441 netif_tx_lock_bh(efx->net_dev);
442
443 /* Count the number of tx completions, and decrement the refcnt. Any
444 * skbs not already completed will be free'd when the queue is flushed */
445 for (i=0; i < state->packet_count; i++) {
446 skb = state->skbs[i];
447 if (skb && !skb_shared(skb))
448 ++tx_done;
449 dev_kfree_skb_any(skb);
450 }
451
Ben Hutchings55668612008-05-16 21:16:10 +0100452 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100453 netif_tx_unlock_bh(efx->net_dev);
454
455 /* Check TX completion and received packet counts */
456 rx_good = atomic_read(&state->rx_good);
457 rx_bad = atomic_read(&state->rx_bad);
458 if (tx_done != state->packet_count) {
459 /* Don't free the skbs; they will be picked up on TX
460 * overflow or channel teardown.
461 */
462 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
463 "TX completion events in %s loopback test\n",
464 tx_queue->queue, tx_done, state->packet_count,
465 LOOPBACK_MODE(efx));
466 rc = -ETIMEDOUT;
467 /* Allow to fall through so we see the RX errors as well */
468 }
469
470 /* We may always be up to a flush away from our desired packet total */
471 if (rx_good != state->packet_count) {
472 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
473 "received packets in %s loopback test\n",
474 tx_queue->queue, rx_good, state->packet_count,
475 LOOPBACK_MODE(efx));
476 rc = -ETIMEDOUT;
477 /* Fall through */
478 }
479
480 /* Update loopback test structure */
481 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
482 lb_tests->tx_done[tx_queue->queue] += tx_done;
483 lb_tests->rx_good += rx_good;
484 lb_tests->rx_bad += rx_bad;
485
486 return rc;
487}
488
489static int
490efx_test_loopback(struct efx_tx_queue *tx_queue,
491 struct efx_loopback_self_tests *lb_tests)
492{
493 struct efx_nic *efx = tx_queue->efx;
494 struct efx_selftest_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100495 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100496
497 for (i = 0; i < loopback_test_level; i++) {
498 /* Determine how many packets to send */
499 state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
500 state->packet_count = min(1 << (i << 2), state->packet_count);
501 state->skbs = kzalloc(sizeof(state->skbs[0]) *
502 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100503 if (!state->skbs)
504 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100505 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100506
507 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
508 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
509 state->packet_count);
510
511 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100512 begin_rc = efx_begin_loopback(tx_queue);
513
514 /* This will normally complete very quickly, but be
515 * prepared to wait up to 100 ms. */
516 msleep(1);
517 if (!efx_poll_loopback(efx)) {
518 msleep(100);
519 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100520 }
521
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100522 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100523 kfree(state->skbs);
524
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100525 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100526 /* Wait a while to ensure there are no packets
527 * floating around after a failure. */
528 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100529 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100530 }
531 }
532
533 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
534 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
535 state->packet_count);
536
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100537 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100538}
539
540static int efx_test_loopbacks(struct efx_nic *efx,
541 struct efx_self_tests *tests,
542 unsigned int loopback_modes)
543{
544 struct efx_selftest_state *state = efx->loopback_selftest;
545 struct ethtool_cmd ecmd, ecmd_loopback;
546 struct efx_tx_queue *tx_queue;
547 enum efx_loopback_mode old_mode, mode;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100548 bool link_up;
549 int count, rc;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100550
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100551 rc = efx_ethtool_get_settings(efx->net_dev, &ecmd);
552 if (rc) {
553 EFX_ERR(efx, "could not get GMII settings\n");
554 return rc;
555 }
556 old_mode = efx->loopback_mode;
557
558 /* Disable autonegotiation for the purposes of loopback */
559 memcpy(&ecmd_loopback, &ecmd, sizeof(ecmd_loopback));
560 if (ecmd_loopback.autoneg == AUTONEG_ENABLE) {
561 ecmd_loopback.autoneg = AUTONEG_DISABLE;
562 ecmd_loopback.duplex = DUPLEX_FULL;
563 ecmd_loopback.speed = SPEED_10000;
564 }
565
566 rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback);
567 if (rc) {
568 EFX_ERR(efx, "could not disable autonegotiation\n");
569 goto out;
570 }
571 tests->loopback_speed = ecmd_loopback.speed;
572 tests->loopback_full_duplex = ecmd_loopback.duplex;
573
574 /* Test all supported loopback modes */
575 for (mode = LOOPBACK_NONE; mode < LOOPBACK_TEST_MAX; mode++) {
576 if (!(loopback_modes & (1 << mode)))
577 continue;
578
579 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100580 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100581 efx->loopback_mode = mode;
582 efx_reconfigure_port(efx);
583
584 /* Wait for the PHY to signal the link is up */
585 count = 0;
586 do {
587 struct efx_channel *channel = &efx->channel[0];
588
589 falcon_check_xmac(efx);
590 schedule_timeout_uninterruptible(HZ / 10);
591 if (channel->work_pending)
592 efx_process_channel_now(channel);
593 /* Wait for PHY events to be processed */
594 flush_workqueue(efx->workqueue);
595 rmb();
596
597 /* efx->link_up can be 1 even if the XAUI link is down,
598 * (bug5762). Usually, it's not worth bothering with the
599 * difference, but for selftests, we need that extra
600 * guarantee that the link is really, really, up.
601 */
602 link_up = efx->link_up;
603 if (!falcon_xaui_link_ok(efx))
604 link_up = 0;
605
606 } while ((++count < 20) && !link_up);
607
608 /* The link should now be up. If it isn't, there is no point
609 * in attempting a loopback test */
610 if (!link_up) {
611 EFX_ERR(efx, "loopback %s never came up\n",
612 LOOPBACK_MODE(efx));
613 rc = -EIO;
614 goto out;
615 }
616
617 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
618 LOOPBACK_MODE(efx), count);
619
620 /* Test every TX queue */
621 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100622 state->offload_csum = (tx_queue->queue ==
623 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100624 rc = efx_test_loopback(tx_queue,
625 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100626 if (rc)
627 goto out;
628 }
629 }
630
631 out:
632 /* Take out of loopback and restore PHY settings */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100633 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100634 efx->loopback_mode = old_mode;
635 efx_ethtool_set_settings(efx->net_dev, &ecmd);
636
637 return rc;
638}
639
640/**************************************************************************
641 *
642 * Entry points
643 *
644 *************************************************************************/
645
646/* Online (i.e. non-disruptive) testing
647 * This checks interrupt generation, event delivery and PHY presence. */
648int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
649{
650 struct efx_channel *channel;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100651 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100652
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100653 rc = efx_test_interrupts(efx, tests);
654 if (rc)
655 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100656 efx_for_each_channel(channel, efx) {
Ben Hutchings64ee3122008-09-01 12:47:38 +0100657 rc = efx_test_eventq_irq(channel, tests);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100658 if (rc)
659 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100660 }
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100661 rc = efx_test_phy(efx, tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100662 return rc;
663}
664
665/* Offline (i.e. disruptive) testing
666 * This checks MAC and PHY loopback on the specified port. */
667int efx_offline_test(struct efx_nic *efx,
668 struct efx_self_tests *tests, unsigned int loopback_modes)
669{
670 struct efx_selftest_state *state;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100671 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100672
673 /* Create a selftest_state structure to hold state for the test */
674 state = kzalloc(sizeof(*state), GFP_KERNEL);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100675 if (state == NULL)
676 return -ENOMEM;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100677
678 /* Set the port loopback_selftest member. From this point on
679 * all received packets will be dropped. Mark the state as
680 * "flushing" so all inflight packets are dropped */
681 BUG_ON(efx->loopback_selftest);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100682 state->flush = true;
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100683 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100684
685 rc = efx_test_loopbacks(efx, tests, loopback_modes);
686
687 efx->loopback_selftest = NULL;
688 wmb();
689 kfree(state);
690
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100691 return rc;
692}
693