blob: 6bb09f263b333b3ee88dc7a444fecd3a3eb72255 [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"
Ben Hutchings8c8661e2008-09-01 12:49:02 +010029#include "spi.h"
30#include "falcon_io.h"
31#include "mdio_10g.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010032
33/*
34 * Loopback test packet structure
35 *
36 * The self-test should stress every RSS vector, and unfortunately
37 * Falcon only performs RSS on TCP/UDP packets.
38 */
39struct efx_loopback_payload {
40 struct ethhdr header;
41 struct iphdr ip;
42 struct udphdr udp;
43 __be16 iteration;
44 const char msg[64];
45} __attribute__ ((packed));
46
47/* Loopback test source MAC address */
48static const unsigned char payload_source[ETH_ALEN] = {
49 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
50};
51
52static const char *payload_msg =
53 "Hello world! This is an Efx loopback test in progress!";
54
55/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010056 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010057 * @flush: Drop all packets in efx_loopback_rx_packet
58 * @packet_count: Number of packets being used in this test
59 * @skbs: An array of skbs transmitted
60 * @rx_good: RX good packet count
61 * @rx_bad: RX bad packet count
62 * @payload: Payload used in tests
63 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010064struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010065 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010066 int packet_count;
67 struct sk_buff **skbs;
Ben Hutchings60ac1062008-09-01 12:44:59 +010068
69 /* Checksums are being offloaded */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010070 bool offload_csum;
Ben Hutchings60ac1062008-09-01 12:44:59 +010071
Ben Hutchings3273c2e2008-05-07 13:36:19 +010072 atomic_t rx_good;
73 atomic_t rx_bad;
74 struct efx_loopback_payload payload;
75};
76
77/**************************************************************************
78 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010079 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010080 *
81 **************************************************************************/
82
Ben Hutchings8c8661e2008-09-01 12:49:02 +010083static int efx_test_mii(struct efx_nic *efx, struct efx_self_tests *tests)
84{
85 int rc = 0;
86 u16 physid1, physid2;
87 struct mii_if_info *mii = &efx->mii;
88 struct net_device *net_dev = efx->net_dev;
89
90 if (efx->phy_type == PHY_TYPE_NONE)
91 return 0;
92
93 mutex_lock(&efx->mac_lock);
94 tests->mii = -1;
95
96 physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
97 physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
98
99 if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
100 (physid2 == 0x0000) || (physid2 == 0xffff)) {
101 EFX_ERR(efx, "no MII PHY present with ID %d\n",
102 mii->phy_id);
103 rc = -EINVAL;
104 goto out;
105 }
106
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800107 if (EFX_IS10G(efx)) {
108 rc = mdio_clause45_check_mmds(efx, efx->phy_op->mmds, 0);
109 if (rc)
110 goto out;
111 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100112
113out:
114 mutex_unlock(&efx->mac_lock);
115 tests->mii = rc ? -1 : 1;
116 return rc;
117}
118
119static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
120{
121 int rc;
122
123 rc = falcon_read_nvram(efx, NULL);
124 tests->nvram = rc ? -1 : 1;
125 return rc;
126}
127
128static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
129{
130 int rc;
131
132 /* Not supported on A-series silicon */
133 if (falcon_rev(efx) < FALCON_REV_B0)
134 return 0;
135
136 rc = falcon_test_registers(efx);
137 tests->registers = rc ? -1 : 1;
138 return rc;
139}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100140
141/**************************************************************************
142 *
143 * Interrupt and event queue testing
144 *
145 **************************************************************************/
146
147/* Test generation and receipt of interrupts */
148static int efx_test_interrupts(struct efx_nic *efx,
149 struct efx_self_tests *tests)
150{
151 struct efx_channel *channel;
152
153 EFX_LOG(efx, "testing interrupts\n");
154 tests->interrupt = -1;
155
156 /* Reset interrupt flag */
157 efx->last_irq_cpu = -1;
158 smp_wmb();
159
160 /* ACK each interrupting event queue. Receiving an interrupt due to
161 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100162 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100163 if (channel->work_pending)
164 efx_process_channel_now(channel);
165 if (efx->last_irq_cpu >= 0)
166 goto success;
167 }
168
169 falcon_generate_interrupt(efx);
170
171 /* Wait for arrival of test interrupt. */
172 EFX_LOG(efx, "waiting for test interrupt\n");
173 schedule_timeout_uninterruptible(HZ / 10);
174 if (efx->last_irq_cpu >= 0)
175 goto success;
176
177 EFX_ERR(efx, "timed out waiting for interrupt\n");
178 return -ETIMEDOUT;
179
180 success:
181 EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
182 efx->interrupt_mode, efx->last_irq_cpu);
183 tests->interrupt = 1;
184 return 0;
185}
186
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100187/* Test generation and receipt of interrupting events */
188static int efx_test_eventq_irq(struct efx_channel *channel,
189 struct efx_self_tests *tests)
190{
191 unsigned int magic, count;
192
193 /* Channel specific code, limited to 20 bits */
194 magic = (0x00010150 + channel->channel);
195 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
196 channel->channel, magic);
197
198 tests->eventq_dma[channel->channel] = -1;
199 tests->eventq_int[channel->channel] = -1;
200 tests->eventq_poll[channel->channel] = -1;
201
202 /* Reset flag and zero magic word */
203 channel->efx->last_irq_cpu = -1;
204 channel->eventq_magic = 0;
205 smp_wmb();
206
207 falcon_generate_test_event(channel, magic);
208
209 /* Wait for arrival of interrupt */
210 count = 0;
211 do {
212 schedule_timeout_uninterruptible(HZ / 100);
213
214 if (channel->work_pending)
215 efx_process_channel_now(channel);
216
217 if (channel->eventq_magic == magic)
218 goto eventq_ok;
219 } while (++count < 2);
220
221 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
222 channel->channel);
223
224 /* See if interrupt arrived */
225 if (channel->efx->last_irq_cpu >= 0) {
226 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
227 "during event queue test\n", channel->channel,
228 raw_smp_processor_id());
229 tests->eventq_int[channel->channel] = 1;
230 }
231
232 /* Check to see if event was received even if interrupt wasn't */
233 efx_process_channel_now(channel);
234 if (channel->eventq_magic == magic) {
235 EFX_ERR(channel->efx, "channel %d event was generated, but "
236 "failed to trigger an interrupt\n", channel->channel);
237 tests->eventq_dma[channel->channel] = 1;
238 }
239
240 return -ETIMEDOUT;
241 eventq_ok:
242 EFX_LOG(channel->efx, "channel %d event queue passed\n",
243 channel->channel);
244 tests->eventq_dma[channel->channel] = 1;
245 tests->eventq_int[channel->channel] = 1;
246 tests->eventq_poll[channel->channel] = 1;
247 return 0;
248}
249
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100250static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100251{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100252 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100253
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100254 if (!efx->phy_op->test)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100255 return 0;
256
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100257 mutex_lock(&efx->mac_lock);
258 rc = efx->phy_op->test(efx);
259 mutex_unlock(&efx->mac_lock);
260 tests->phy = rc ? -1 : 1;
261 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100262}
263
264/**************************************************************************
265 *
266 * Loopback testing
267 * NB Only one loopback test can be executing concurrently.
268 *
269 **************************************************************************/
270
271/* Loopback test RX callback
272 * This is called for each received packet during loopback testing.
273 */
274void efx_loopback_rx_packet(struct efx_nic *efx,
275 const char *buf_ptr, int pkt_len)
276{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100277 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100278 struct efx_loopback_payload *received;
279 struct efx_loopback_payload *payload;
280
281 BUG_ON(!buf_ptr);
282
283 /* If we are just flushing, then drop the packet */
284 if ((state == NULL) || state->flush)
285 return;
286
287 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100288
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100289 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100290 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100291 if (state->offload_csum)
292 received->ip.check = payload->ip.check;
293
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100294 /* Check that header exists */
295 if (pkt_len < sizeof(received->header)) {
296 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
297 "test\n", pkt_len, LOOPBACK_MODE(efx));
298 goto err;
299 }
300
301 /* Check that the ethernet header exists */
302 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
303 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
304 LOOPBACK_MODE(efx));
305 goto err;
306 }
307
308 /* Check packet length */
309 if (pkt_len != sizeof(*payload)) {
310 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
311 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
312 LOOPBACK_MODE(efx));
313 goto err;
314 }
315
316 /* Check that IP header matches */
317 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
318 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
319 LOOPBACK_MODE(efx));
320 goto err;
321 }
322
323 /* Check that msg and padding matches */
324 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
325 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
326 LOOPBACK_MODE(efx));
327 goto err;
328 }
329
330 /* Check that iteration matches */
331 if (received->iteration != payload->iteration) {
332 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
333 "%s loopback test\n", ntohs(received->iteration),
334 ntohs(payload->iteration), LOOPBACK_MODE(efx));
335 goto err;
336 }
337
338 /* Increase correct RX count */
339 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
340 LOOPBACK_MODE(efx));
341
342 atomic_inc(&state->rx_good);
343 return;
344
345 err:
346#ifdef EFX_ENABLE_DEBUG
347 if (atomic_read(&state->rx_bad) == 0) {
348 EFX_ERR(efx, "received packet:\n");
349 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
350 buf_ptr, pkt_len, 0);
351 EFX_ERR(efx, "expected packet:\n");
352 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
353 &state->payload, sizeof(state->payload), 0);
354 }
355#endif
356 atomic_inc(&state->rx_bad);
357}
358
359/* Initialise an efx_selftest_state for a new iteration */
360static void efx_iterate_state(struct efx_nic *efx)
361{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100362 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100363 struct net_device *net_dev = efx->net_dev;
364 struct efx_loopback_payload *payload = &state->payload;
365
366 /* Initialise the layerII header */
367 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
368 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
369 payload->header.h_proto = htons(ETH_P_IP);
370
371 /* saddr set later and used as incrementing count */
372 payload->ip.daddr = htonl(INADDR_LOOPBACK);
373 payload->ip.ihl = 5;
374 payload->ip.check = htons(0xdead);
375 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
376 payload->ip.version = IPVERSION;
377 payload->ip.protocol = IPPROTO_UDP;
378
379 /* Initialise udp header */
380 payload->udp.source = 0;
381 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
382 sizeof(struct iphdr));
383 payload->udp.check = 0; /* checksum ignored */
384
385 /* Fill out payload */
386 payload->iteration = htons(ntohs(payload->iteration) + 1);
387 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
388
389 /* Fill out remaining state members */
390 atomic_set(&state->rx_good, 0);
391 atomic_set(&state->rx_bad, 0);
392 smp_wmb();
393}
394
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100395static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100396{
397 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100398 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100399 struct efx_loopback_payload *payload;
400 struct sk_buff *skb;
401 int i, rc;
402
403 /* Transmit N copies of buffer */
404 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100405 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100406 * transmit completion counting */
407 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
408 if (!skb)
409 return -ENOMEM;
410 state->skbs[i] = skb;
411 skb_get(skb);
412
413 /* Copy the payload in, incrementing the source address to
414 * exercise the rss vectors */
415 payload = ((struct efx_loopback_payload *)
416 skb_put(skb, sizeof(state->payload)));
417 memcpy(payload, &state->payload, sizeof(state->payload));
418 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
419
420 /* Ensure everything we've written is visible to the
421 * interrupt handler. */
422 smp_wmb();
423
Ben Hutchings55668612008-05-16 21:16:10 +0100424 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100425 netif_tx_lock_bh(efx->net_dev);
426 rc = efx_xmit(efx, tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100427 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100428 netif_tx_unlock_bh(efx->net_dev);
429
430 if (rc != NETDEV_TX_OK) {
431 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
432 "of %d in %s loopback test\n", tx_queue->queue,
433 i + 1, state->packet_count, LOOPBACK_MODE(efx));
434
435 /* Defer cleaning up the other skbs for the caller */
436 kfree_skb(skb);
437 return -EPIPE;
438 }
439 }
440
441 return 0;
442}
443
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100444static int efx_poll_loopback(struct efx_nic *efx)
445{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100446 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100447 struct efx_channel *channel;
448
449 /* NAPI polling is not enabled, so process channels
450 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100451 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100452 if (channel->work_pending)
453 efx_process_channel_now(channel);
454 }
455 return atomic_read(&state->rx_good) == state->packet_count;
456}
457
458static int efx_end_loopback(struct efx_tx_queue *tx_queue,
459 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100460{
461 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100462 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100463 struct sk_buff *skb;
464 int tx_done = 0, rx_good, rx_bad;
465 int i, rc = 0;
466
Ben Hutchings55668612008-05-16 21:16:10 +0100467 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100468 netif_tx_lock_bh(efx->net_dev);
469
470 /* Count the number of tx completions, and decrement the refcnt. Any
471 * skbs not already completed will be free'd when the queue is flushed */
472 for (i=0; i < state->packet_count; i++) {
473 skb = state->skbs[i];
474 if (skb && !skb_shared(skb))
475 ++tx_done;
476 dev_kfree_skb_any(skb);
477 }
478
Ben Hutchings55668612008-05-16 21:16:10 +0100479 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100480 netif_tx_unlock_bh(efx->net_dev);
481
482 /* Check TX completion and received packet counts */
483 rx_good = atomic_read(&state->rx_good);
484 rx_bad = atomic_read(&state->rx_bad);
485 if (tx_done != state->packet_count) {
486 /* Don't free the skbs; they will be picked up on TX
487 * overflow or channel teardown.
488 */
489 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
490 "TX completion events in %s loopback test\n",
491 tx_queue->queue, tx_done, state->packet_count,
492 LOOPBACK_MODE(efx));
493 rc = -ETIMEDOUT;
494 /* Allow to fall through so we see the RX errors as well */
495 }
496
497 /* We may always be up to a flush away from our desired packet total */
498 if (rx_good != state->packet_count) {
499 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
500 "received packets in %s loopback test\n",
501 tx_queue->queue, rx_good, state->packet_count,
502 LOOPBACK_MODE(efx));
503 rc = -ETIMEDOUT;
504 /* Fall through */
505 }
506
507 /* Update loopback test structure */
508 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
509 lb_tests->tx_done[tx_queue->queue] += tx_done;
510 lb_tests->rx_good += rx_good;
511 lb_tests->rx_bad += rx_bad;
512
513 return rc;
514}
515
516static int
517efx_test_loopback(struct efx_tx_queue *tx_queue,
518 struct efx_loopback_self_tests *lb_tests)
519{
520 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100521 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100522 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100523
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100524 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525 /* Determine how many packets to send */
526 state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
527 state->packet_count = min(1 << (i << 2), state->packet_count);
528 state->skbs = kzalloc(sizeof(state->skbs[0]) *
529 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100530 if (!state->skbs)
531 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100532 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100533
534 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
535 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
536 state->packet_count);
537
538 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100539 begin_rc = efx_begin_loopback(tx_queue);
540
541 /* This will normally complete very quickly, but be
542 * prepared to wait up to 100 ms. */
543 msleep(1);
544 if (!efx_poll_loopback(efx)) {
545 msleep(100);
546 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100547 }
548
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100549 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100550 kfree(state->skbs);
551
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100552 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100553 /* Wait a while to ensure there are no packets
554 * floating around after a failure. */
555 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100556 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100557 }
558 }
559
560 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
561 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
562 state->packet_count);
563
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100564 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100565}
566
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100567static int efx_test_loopbacks(struct efx_nic *efx, struct ethtool_cmd ecmd,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100568 struct efx_self_tests *tests,
569 unsigned int loopback_modes)
570{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100571 enum efx_loopback_mode mode;
572 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100573 struct efx_tx_queue *tx_queue;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100574 bool link_up;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100575 int count, rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100576
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100577 /* Set the port loopback_selftest member. From this point on
578 * all received packets will be dropped. Mark the state as
579 * "flushing" so all inflight packets are dropped */
580 state = kzalloc(sizeof(*state), GFP_KERNEL);
581 if (state == NULL)
582 return -ENOMEM;
583 BUG_ON(efx->loopback_selftest);
584 state->flush = true;
585 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100586
587 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100588 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100589 if (!(loopback_modes & (1 << mode)))
590 continue;
591
592 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100593 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100594 efx->loopback_mode = mode;
595 efx_reconfigure_port(efx);
596
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800597 /* Wait for the PHY to signal the link is up. Interrupts
598 * are enabled for PHY's using LASI, otherwise we poll()
599 * quickly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100600 count = 0;
601 do {
602 struct efx_channel *channel = &efx->channel[0];
603
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800604 efx->phy_op->poll(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100605 schedule_timeout_uninterruptible(HZ / 10);
606 if (channel->work_pending)
607 efx_process_channel_now(channel);
608 /* Wait for PHY events to be processed */
609 flush_workqueue(efx->workqueue);
610 rmb();
611
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800612 /* We need both the phy and xaui links to be ok.
613 * rather than relying on the falcon_xmac irq/poll
614 * regime, just poll xaui directly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100615 link_up = efx->link_up;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800616 if (link_up && EFX_IS10G(efx) &&
617 !falcon_xaui_link_ok(efx))
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100618 link_up = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100619
620 } while ((++count < 20) && !link_up);
621
622 /* The link should now be up. If it isn't, there is no point
623 * in attempting a loopback test */
624 if (!link_up) {
625 EFX_ERR(efx, "loopback %s never came up\n",
626 LOOPBACK_MODE(efx));
627 rc = -EIO;
628 goto out;
629 }
630
631 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
632 LOOPBACK_MODE(efx), count);
633
634 /* Test every TX queue */
635 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100636 state->offload_csum = (tx_queue->queue ==
637 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100638 rc = efx_test_loopback(tx_queue,
639 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100640 if (rc)
641 goto out;
642 }
643 }
644
645 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100646 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100647 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100648 efx->loopback_selftest = NULL;
649 wmb();
650 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100651
652 return rc;
653}
654
655/**************************************************************************
656 *
657 * Entry points
658 *
659 *************************************************************************/
660
661/* Online (i.e. non-disruptive) testing
662 * This checks interrupt generation, event delivery and PHY presence. */
663int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
664{
665 struct efx_channel *channel;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100666 int rc, rc2 = 0;
667
668 rc = efx_test_mii(efx, tests);
669 if (rc && !rc2)
670 rc2 = rc;
671
672 rc = efx_test_nvram(efx, tests);
673 if (rc && !rc2)
674 rc2 = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100675
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100676 rc = efx_test_interrupts(efx, tests);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100677 if (rc && !rc2)
678 rc2 = rc;
679
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100680 efx_for_each_channel(channel, efx) {
Ben Hutchings64ee3122008-09-01 12:47:38 +0100681 rc = efx_test_eventq_irq(channel, tests);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100682 if (rc && !rc2)
683 rc2 = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100684 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100685
686 return rc2;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100687}
688
689/* Offline (i.e. disruptive) testing
690 * This checks MAC and PHY loopback on the specified port. */
691int efx_offline_test(struct efx_nic *efx,
692 struct efx_self_tests *tests, unsigned int loopback_modes)
693{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100694 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
695 int phy_mode = efx->phy_mode;
696 struct ethtool_cmd ecmd, ecmd_test;
697 int rc, rc2 = 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100698
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100699 /* force the carrier state off so the kernel doesn't transmit during
700 * the loopback test, and the watchdog timeout doesn't fire. Also put
701 * falcon into loopback for the register test.
702 */
703 mutex_lock(&efx->mac_lock);
704 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800705 if (efx->loopback_modes) {
706 /* We need the 312 clock from the PHY to test the XMAC
707 * registers, so move into XGMII loopback if available */
708 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
709 efx->loopback_mode = LOOPBACK_XGMII;
710 else
711 efx->loopback_mode = __ffs(efx->loopback_modes);
712 }
713
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100714 __efx_reconfigure_port(efx);
715 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100716
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100717 /* free up all consumers of SRAM (including all the queues) */
718 efx_reset_down(efx, &ecmd);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100719
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100720 rc = efx_test_chip(efx, tests);
721 if (rc && !rc2)
722 rc2 = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100723
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100724 /* reset the chip to recover from the register test */
725 rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100726
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100727 /* Modify the saved ecmd so that when efx_reset_up() restores the phy
728 * state, AN is disabled, and the phy is powered, and out of loopback */
729 memcpy(&ecmd_test, &ecmd, sizeof(ecmd_test));
730 if (ecmd_test.autoneg == AUTONEG_ENABLE) {
731 ecmd_test.autoneg = AUTONEG_DISABLE;
732 ecmd_test.duplex = DUPLEX_FULL;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100733 }
734 efx->loopback_mode = LOOPBACK_NONE;
735
736 rc = efx_reset_up(efx, &ecmd_test, rc == 0);
737 if (rc) {
738 EFX_ERR(efx, "Unable to recover from chip test\n");
739 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
740 return rc;
741 }
742
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100743 rc = efx_test_phy(efx, tests);
744 if (rc && !rc2)
745 rc2 = rc;
746
747 rc = efx_test_loopbacks(efx, ecmd_test, tests, loopback_modes);
748 if (rc && !rc2)
749 rc2 = rc;
750
751 /* restore the PHY to the previous state */
752 efx->loopback_mode = loopback_mode;
753 efx->phy_mode = phy_mode;
754 efx->port_inhibited = false;
755 efx_ethtool_set_settings(efx->net_dev, &ecmd);
756
757 return rc2;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100758}
759