blob: 8a5a7b6d042d7d557b80294138d3202c82c103b5 [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.
Ben Hutchings906bb262009-11-29 15:16:19 +00004 * Copyright 2006-2009 Solarflare Communications Inc.
Ben Hutchings3273c2e2008-05-07 13:36:19 +01005 *
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"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010023#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000024#include "nic.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010025#include "selftest.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#include "workarounds.h"
Ben Hutchings8c8661e2008-09-01 12:49:02 +010027#include "spi.h"
Ben Hutchings12d00ca2009-10-23 08:30:46 +000028#include "io.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010029
30/*
31 * Loopback test packet structure
32 *
33 * The self-test should stress every RSS vector, and unfortunately
34 * Falcon only performs RSS on TCP/UDP packets.
35 */
36struct efx_loopback_payload {
37 struct ethhdr header;
38 struct iphdr ip;
39 struct udphdr udp;
40 __be16 iteration;
41 const char msg[64];
42} __attribute__ ((packed));
43
44/* Loopback test source MAC address */
45static const unsigned char payload_source[ETH_ALEN] = {
46 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
47};
48
Julia Lawall94de8032009-12-13 01:41:29 +000049static const char payload_msg[] =
Ben Hutchings3273c2e2008-05-07 13:36:19 +010050 "Hello world! This is an Efx loopback test in progress!";
51
52/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010053 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010054 * @flush: Drop all packets in efx_loopback_rx_packet
55 * @packet_count: Number of packets being used in this test
56 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000057 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010058 * @rx_good: RX good packet count
59 * @rx_bad: RX bad packet count
60 * @payload: Payload used in tests
61 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010062struct efx_loopback_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 Hutchingsdc8cfa52008-09-01 12:46:50 +010066 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010067 atomic_t rx_good;
68 atomic_t rx_bad;
69 struct efx_loopback_payload payload;
70};
71
72/**************************************************************************
73 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010074 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010075 *
76 **************************************************************************/
77
Ben Hutchings4f16c072010-02-03 09:30:50 +000078static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010079{
80 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010081
Ben Hutchings4f16c072010-02-03 09:30:50 +000082 if (efx->phy_op->test_alive) {
83 rc = efx->phy_op->test_alive(efx);
84 tests->phy_alive = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010085 }
86
Ben Hutchings8c8661e2008-09-01 12:49:02 +010087 return rc;
88}
89
90static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
91{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000092 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010093
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000094 if (efx->type->test_nvram) {
95 rc = efx->type->test_nvram(efx);
96 tests->nvram = rc ? -1 : 1;
97 }
98
Ben Hutchings8c8661e2008-09-01 12:49:02 +010099 return rc;
100}
101
102static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
103{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000104 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100105
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000106 /* Test register access */
107 if (efx->type->test_registers) {
108 rc = efx->type->test_registers(efx);
109 tests->registers = rc ? -1 : 1;
110 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100111
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100112 return rc;
113}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100114
115/**************************************************************************
116 *
117 * Interrupt and event queue testing
118 *
119 **************************************************************************/
120
121/* Test generation and receipt of interrupts */
122static int efx_test_interrupts(struct efx_nic *efx,
123 struct efx_self_tests *tests)
124{
125 struct efx_channel *channel;
126
127 EFX_LOG(efx, "testing interrupts\n");
128 tests->interrupt = -1;
129
130 /* Reset interrupt flag */
131 efx->last_irq_cpu = -1;
132 smp_wmb();
133
134 /* ACK each interrupting event queue. Receiving an interrupt due to
135 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100136 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100137 if (channel->work_pending)
138 efx_process_channel_now(channel);
139 if (efx->last_irq_cpu >= 0)
140 goto success;
141 }
142
Ben Hutchings152b6a62009-11-29 03:43:56 +0000143 efx_nic_generate_interrupt(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100144
145 /* Wait for arrival of test interrupt. */
146 EFX_LOG(efx, "waiting for test interrupt\n");
147 schedule_timeout_uninterruptible(HZ / 10);
148 if (efx->last_irq_cpu >= 0)
149 goto success;
150
151 EFX_ERR(efx, "timed out waiting for interrupt\n");
152 return -ETIMEDOUT;
153
154 success:
Ben Hutchingsc4593022009-11-23 16:08:17 +0000155 EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
156 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100157 tests->interrupt = 1;
158 return 0;
159}
160
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100161/* Test generation and receipt of interrupting events */
162static int efx_test_eventq_irq(struct efx_channel *channel,
163 struct efx_self_tests *tests)
164{
165 unsigned int magic, count;
166
167 /* Channel specific code, limited to 20 bits */
168 magic = (0x00010150 + channel->channel);
169 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
170 channel->channel, magic);
171
172 tests->eventq_dma[channel->channel] = -1;
173 tests->eventq_int[channel->channel] = -1;
174 tests->eventq_poll[channel->channel] = -1;
175
176 /* Reset flag and zero magic word */
177 channel->efx->last_irq_cpu = -1;
178 channel->eventq_magic = 0;
179 smp_wmb();
180
Ben Hutchings152b6a62009-11-29 03:43:56 +0000181 efx_nic_generate_test_event(channel, magic);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100182
183 /* Wait for arrival of interrupt */
184 count = 0;
185 do {
186 schedule_timeout_uninterruptible(HZ / 100);
187
188 if (channel->work_pending)
189 efx_process_channel_now(channel);
190
191 if (channel->eventq_magic == magic)
192 goto eventq_ok;
193 } while (++count < 2);
194
195 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
196 channel->channel);
197
198 /* See if interrupt arrived */
199 if (channel->efx->last_irq_cpu >= 0) {
200 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
201 "during event queue test\n", channel->channel,
202 raw_smp_processor_id());
203 tests->eventq_int[channel->channel] = 1;
204 }
205
206 /* Check to see if event was received even if interrupt wasn't */
207 efx_process_channel_now(channel);
208 if (channel->eventq_magic == magic) {
209 EFX_ERR(channel->efx, "channel %d event was generated, but "
210 "failed to trigger an interrupt\n", channel->channel);
211 tests->eventq_dma[channel->channel] = 1;
212 }
213
214 return -ETIMEDOUT;
215 eventq_ok:
216 EFX_LOG(channel->efx, "channel %d event queue passed\n",
217 channel->channel);
218 tests->eventq_dma[channel->channel] = 1;
219 tests->eventq_int[channel->channel] = 1;
220 tests->eventq_poll[channel->channel] = 1;
221 return 0;
222}
223
Ben Hutchings17967212008-12-26 13:47:25 -0800224static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
225 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100226{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100227 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100228
Ben Hutchings17967212008-12-26 13:47:25 -0800229 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100230 return 0;
231
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100232 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000233 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100234 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100235 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100236}
237
238/**************************************************************************
239 *
240 * Loopback testing
241 * NB Only one loopback test can be executing concurrently.
242 *
243 **************************************************************************/
244
245/* Loopback test RX callback
246 * This is called for each received packet during loopback testing.
247 */
248void efx_loopback_rx_packet(struct efx_nic *efx,
249 const char *buf_ptr, int pkt_len)
250{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100251 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100252 struct efx_loopback_payload *received;
253 struct efx_loopback_payload *payload;
254
255 BUG_ON(!buf_ptr);
256
257 /* If we are just flushing, then drop the packet */
258 if ((state == NULL) || state->flush)
259 return;
260
261 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100262
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100263 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100264 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100265 if (state->offload_csum)
266 received->ip.check = payload->ip.check;
267
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100268 /* Check that header exists */
269 if (pkt_len < sizeof(received->header)) {
270 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
271 "test\n", pkt_len, LOOPBACK_MODE(efx));
272 goto err;
273 }
274
275 /* Check that the ethernet header exists */
276 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
277 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
278 LOOPBACK_MODE(efx));
279 goto err;
280 }
281
282 /* Check packet length */
283 if (pkt_len != sizeof(*payload)) {
284 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
285 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
286 LOOPBACK_MODE(efx));
287 goto err;
288 }
289
290 /* Check that IP header matches */
291 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
292 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
293 LOOPBACK_MODE(efx));
294 goto err;
295 }
296
297 /* Check that msg and padding matches */
298 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
299 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
300 LOOPBACK_MODE(efx));
301 goto err;
302 }
303
304 /* Check that iteration matches */
305 if (received->iteration != payload->iteration) {
306 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
307 "%s loopback test\n", ntohs(received->iteration),
308 ntohs(payload->iteration), LOOPBACK_MODE(efx));
309 goto err;
310 }
311
312 /* Increase correct RX count */
313 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
314 LOOPBACK_MODE(efx));
315
316 atomic_inc(&state->rx_good);
317 return;
318
319 err:
320#ifdef EFX_ENABLE_DEBUG
321 if (atomic_read(&state->rx_bad) == 0) {
322 EFX_ERR(efx, "received packet:\n");
323 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
324 buf_ptr, pkt_len, 0);
325 EFX_ERR(efx, "expected packet:\n");
326 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
327 &state->payload, sizeof(state->payload), 0);
328 }
329#endif
330 atomic_inc(&state->rx_bad);
331}
332
333/* Initialise an efx_selftest_state for a new iteration */
334static void efx_iterate_state(struct efx_nic *efx)
335{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100336 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100337 struct net_device *net_dev = efx->net_dev;
338 struct efx_loopback_payload *payload = &state->payload;
339
340 /* Initialise the layerII header */
341 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
342 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
343 payload->header.h_proto = htons(ETH_P_IP);
344
345 /* saddr set later and used as incrementing count */
346 payload->ip.daddr = htonl(INADDR_LOOPBACK);
347 payload->ip.ihl = 5;
348 payload->ip.check = htons(0xdead);
349 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
350 payload->ip.version = IPVERSION;
351 payload->ip.protocol = IPPROTO_UDP;
352
353 /* Initialise udp header */
354 payload->udp.source = 0;
355 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
356 sizeof(struct iphdr));
357 payload->udp.check = 0; /* checksum ignored */
358
359 /* Fill out payload */
360 payload->iteration = htons(ntohs(payload->iteration) + 1);
361 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
362
363 /* Fill out remaining state members */
364 atomic_set(&state->rx_good, 0);
365 atomic_set(&state->rx_bad, 0);
366 smp_wmb();
367}
368
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100369static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100370{
371 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100372 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100373 struct efx_loopback_payload *payload;
374 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000375 int i;
376 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100377
378 /* Transmit N copies of buffer */
379 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100380 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100381 * transmit completion counting */
382 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
383 if (!skb)
384 return -ENOMEM;
385 state->skbs[i] = skb;
386 skb_get(skb);
387
388 /* Copy the payload in, incrementing the source address to
389 * exercise the rss vectors */
390 payload = ((struct efx_loopback_payload *)
391 skb_put(skb, sizeof(state->payload)));
392 memcpy(payload, &state->payload, sizeof(state->payload));
393 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
394
395 /* Ensure everything we've written is visible to the
396 * interrupt handler. */
397 smp_wmb();
398
Ben Hutchings55668612008-05-16 21:16:10 +0100399 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100400 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000401 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100402 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100403 netif_tx_unlock_bh(efx->net_dev);
404
405 if (rc != NETDEV_TX_OK) {
406 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
407 "of %d in %s loopback test\n", tx_queue->queue,
408 i + 1, state->packet_count, LOOPBACK_MODE(efx));
409
410 /* Defer cleaning up the other skbs for the caller */
411 kfree_skb(skb);
412 return -EPIPE;
413 }
414 }
415
416 return 0;
417}
418
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100419static int efx_poll_loopback(struct efx_nic *efx)
420{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100421 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100422 struct efx_channel *channel;
423
424 /* NAPI polling is not enabled, so process channels
425 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100426 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100427 if (channel->work_pending)
428 efx_process_channel_now(channel);
429 }
430 return atomic_read(&state->rx_good) == state->packet_count;
431}
432
433static int efx_end_loopback(struct efx_tx_queue *tx_queue,
434 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100435{
436 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100437 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100438 struct sk_buff *skb;
439 int tx_done = 0, rx_good, rx_bad;
440 int i, rc = 0;
441
Ben Hutchings55668612008-05-16 21:16:10 +0100442 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100443 netif_tx_lock_bh(efx->net_dev);
444
445 /* Count the number of tx completions, and decrement the refcnt. Any
446 * skbs not already completed will be free'd when the queue is flushed */
447 for (i=0; i < state->packet_count; i++) {
448 skb = state->skbs[i];
449 if (skb && !skb_shared(skb))
450 ++tx_done;
451 dev_kfree_skb_any(skb);
452 }
453
Ben Hutchings55668612008-05-16 21:16:10 +0100454 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100455 netif_tx_unlock_bh(efx->net_dev);
456
457 /* Check TX completion and received packet counts */
458 rx_good = atomic_read(&state->rx_good);
459 rx_bad = atomic_read(&state->rx_bad);
460 if (tx_done != state->packet_count) {
461 /* Don't free the skbs; they will be picked up on TX
462 * overflow or channel teardown.
463 */
464 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
465 "TX completion events in %s loopback test\n",
466 tx_queue->queue, tx_done, state->packet_count,
467 LOOPBACK_MODE(efx));
468 rc = -ETIMEDOUT;
469 /* Allow to fall through so we see the RX errors as well */
470 }
471
472 /* We may always be up to a flush away from our desired packet total */
473 if (rx_good != state->packet_count) {
474 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
475 "received packets in %s loopback test\n",
476 tx_queue->queue, rx_good, state->packet_count,
477 LOOPBACK_MODE(efx));
478 rc = -ETIMEDOUT;
479 /* Fall through */
480 }
481
482 /* Update loopback test structure */
483 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
484 lb_tests->tx_done[tx_queue->queue] += tx_done;
485 lb_tests->rx_good += rx_good;
486 lb_tests->rx_bad += rx_bad;
487
488 return rc;
489}
490
491static int
492efx_test_loopback(struct efx_tx_queue *tx_queue,
493 struct efx_loopback_self_tests *lb_tests)
494{
495 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100496 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100497 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100498
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100499 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100500 /* Determine how many packets to send */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000501 state->packet_count = EFX_TXQ_SIZE / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100502 state->packet_count = min(1 << (i << 2), state->packet_count);
503 state->skbs = kzalloc(sizeof(state->skbs[0]) *
504 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100505 if (!state->skbs)
506 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100507 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100508
509 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
510 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
511 state->packet_count);
512
513 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100514 begin_rc = efx_begin_loopback(tx_queue);
515
516 /* This will normally complete very quickly, but be
517 * prepared to wait up to 100 ms. */
518 msleep(1);
519 if (!efx_poll_loopback(efx)) {
520 msleep(100);
521 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100522 }
523
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100524 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525 kfree(state->skbs);
526
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100527 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100528 /* Wait a while to ensure there are no packets
529 * floating around after a failure. */
530 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100531 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100532 }
533 }
534
535 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
536 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
537 state->packet_count);
538
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100539 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100540}
541
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000542/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
543 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
544 * to delay and retry. Therefore, it's safer to just poll directly. Wait
545 * for link up and any faults to dissipate. */
546static int efx_wait_for_link(struct efx_nic *efx)
547{
548 struct efx_link_state *link_state = &efx->link_state;
549 int count;
550 bool link_up;
551
552 for (count = 0; count < 40; count++) {
553 schedule_timeout_uninterruptible(HZ / 10);
554
555 if (efx->type->monitor != NULL) {
556 mutex_lock(&efx->mac_lock);
557 efx->type->monitor(efx);
558 mutex_unlock(&efx->mac_lock);
559 } else {
560 struct efx_channel *channel = &efx->channel[0];
561 if (channel->work_pending)
562 efx_process_channel_now(channel);
563 }
564
565 mutex_lock(&efx->mac_lock);
566 link_up = link_state->up;
567 if (link_up)
568 link_up = !efx->mac_op->check_fault(efx);
569 mutex_unlock(&efx->mac_lock);
570
571 if (link_up)
572 return 0;
573 }
574
575 return -ETIMEDOUT;
576}
577
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800578static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100579 unsigned int loopback_modes)
580{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100581 enum efx_loopback_mode mode;
582 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100583 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000584 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100585
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100586 /* Set the port loopback_selftest member. From this point on
587 * all received packets will be dropped. Mark the state as
588 * "flushing" so all inflight packets are dropped */
589 state = kzalloc(sizeof(*state), GFP_KERNEL);
590 if (state == NULL)
591 return -ENOMEM;
592 BUG_ON(efx->loopback_selftest);
593 state->flush = true;
594 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100595
596 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100597 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100598 if (!(loopback_modes & (1 << mode)))
599 continue;
600
601 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100602 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000603 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100604 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000605 rc = __efx_reconfigure_port(efx);
606 mutex_unlock(&efx->mac_lock);
607 if (rc) {
608 EFX_ERR(efx, "unable to move into %s loopback\n",
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100609 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100610 goto out;
611 }
612
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000613 rc = efx_wait_for_link(efx);
614 if (rc) {
615 EFX_ERR(efx, "loopback %s never came up\n",
616 LOOPBACK_MODE(efx));
617 goto out;
618 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100619
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:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100632 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100633 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100634 efx->loopback_selftest = NULL;
635 wmb();
636 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100637
638 return rc;
639}
640
641/**************************************************************************
642 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800643 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100644 *
645 *************************************************************************/
646
Ben Hutchings2ef30682008-12-26 13:47:04 -0800647int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
648 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100649{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100650 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
651 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000652 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800653 struct efx_channel *channel;
654 int rc_test = 0, rc_reset = 0, rc;
655
656 /* Online (i.e. non-disruptive) testing
657 * This checks interrupt generation, event delivery and PHY presence. */
658
Ben Hutchings4f16c072010-02-03 09:30:50 +0000659 rc = efx_test_phy_alive(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800660 if (rc && !rc_test)
661 rc_test = rc;
662
663 rc = efx_test_nvram(efx, tests);
664 if (rc && !rc_test)
665 rc_test = rc;
666
667 rc = efx_test_interrupts(efx, tests);
668 if (rc && !rc_test)
669 rc_test = rc;
670
671 efx_for_each_channel(channel, efx) {
672 rc = efx_test_eventq_irq(channel, tests);
673 if (rc && !rc_test)
674 rc_test = rc;
675 }
676
677 if (rc_test)
678 return rc_test;
679
680 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800681 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800682
683 /* Offline (i.e. disruptive) testing
684 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100685
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100686 /* force the carrier state off so the kernel doesn't transmit during
687 * the loopback test, and the watchdog timeout doesn't fire. Also put
688 * falcon into loopback for the register test.
689 */
690 mutex_lock(&efx->mac_lock);
691 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800692 if (efx->loopback_modes) {
693 /* We need the 312 clock from the PHY to test the XMAC
694 * registers, so move into XGMII loopback if available */
695 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
696 efx->loopback_mode = LOOPBACK_XGMII;
697 else
698 efx->loopback_mode = __ffs(efx->loopback_modes);
699 }
700
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100701 __efx_reconfigure_port(efx);
702 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100703
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100704 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000705 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100706
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100707 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800708 if (rc && !rc_test)
709 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100710
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100711 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000712 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100713
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800714 /* Ensure that the phy is powered and out of loopback
715 * for the bist and loopback tests */
716 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100717 efx->loopback_mode = LOOPBACK_NONE;
718
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000719 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800720 if (rc && !rc_reset)
721 rc_reset = rc;
722
723 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100724 EFX_ERR(efx, "Unable to recover from chip test\n");
725 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800726 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100727 }
728
Ben Hutchings17967212008-12-26 13:47:25 -0800729 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800730 if (rc && !rc_test)
731 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100732
Ben Hutchings2ef30682008-12-26 13:47:04 -0800733 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
734 if (rc && !rc_test)
735 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100736
737 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000738 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100739 efx->phy_mode = phy_mode;
740 efx->port_inhibited = false;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000741 efx->loopback_mode = loopback_mode;
742 __efx_reconfigure_port(efx);
743 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100744
Ben Hutchings2ef30682008-12-26 13:47:04 -0800745 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100746}
747