blob: 3a16e06122671e503322ce72d8f7890b94f571d5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ben Hutchings3273c2e2008-05-07 13:36:19 +010022#include <asm/io.h>
23#include "net_driver.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010024#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000025#include "nic.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#include "selftest.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010027#include "workarounds.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010028
29/*
30 * Loopback test packet structure
31 *
32 * The self-test should stress every RSS vector, and unfortunately
33 * Falcon only performs RSS on TCP/UDP packets.
34 */
35struct efx_loopback_payload {
36 struct ethhdr header;
37 struct iphdr ip;
38 struct udphdr udp;
39 __be16 iteration;
40 const char msg[64];
41} __attribute__ ((packed));
42
43/* Loopback test source MAC address */
44static const unsigned char payload_source[ETH_ALEN] = {
45 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
46};
47
Julia Lawall94de8032009-12-13 01:41:29 +000048static const char payload_msg[] =
Ben Hutchings3273c2e2008-05-07 13:36:19 +010049 "Hello world! This is an Efx loopback test in progress!";
50
51/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010052 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010053 * @flush: Drop all packets in efx_loopback_rx_packet
54 * @packet_count: Number of packets being used in this test
55 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000056 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010057 * @rx_good: RX good packet count
58 * @rx_bad: RX bad packet count
59 * @payload: Payload used in tests
60 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010061struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010062 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010063 int packet_count;
64 struct sk_buff **skbs;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010065 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010066 atomic_t rx_good;
67 atomic_t rx_bad;
68 struct efx_loopback_payload payload;
69};
70
71/**************************************************************************
72 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010073 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010074 *
75 **************************************************************************/
76
Ben Hutchings4f16c072010-02-03 09:30:50 +000077static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010078{
79 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010080
Ben Hutchings4f16c072010-02-03 09:30:50 +000081 if (efx->phy_op->test_alive) {
82 rc = efx->phy_op->test_alive(efx);
83 tests->phy_alive = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010084 }
85
Ben Hutchings8c8661e2008-09-01 12:49:02 +010086 return rc;
87}
88
89static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
90{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000091 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010092
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000093 if (efx->type->test_nvram) {
94 rc = efx->type->test_nvram(efx);
95 tests->nvram = rc ? -1 : 1;
96 }
97
Ben Hutchings8c8661e2008-09-01 12:49:02 +010098 return rc;
99}
100
101static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
102{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000103 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100104
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000105 /* Test register access */
106 if (efx->type->test_registers) {
107 rc = efx->type->test_registers(efx);
108 tests->registers = rc ? -1 : 1;
109 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100110
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100111 return rc;
112}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100113
114/**************************************************************************
115 *
116 * Interrupt and event queue testing
117 *
118 **************************************************************************/
119
120/* Test generation and receipt of interrupts */
121static int efx_test_interrupts(struct efx_nic *efx,
122 struct efx_self_tests *tests)
123{
124 struct efx_channel *channel;
125
126 EFX_LOG(efx, "testing interrupts\n");
127 tests->interrupt = -1;
128
129 /* Reset interrupt flag */
130 efx->last_irq_cpu = -1;
131 smp_wmb();
132
133 /* ACK each interrupting event queue. Receiving an interrupt due to
134 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100135 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100136 if (channel->work_pending)
137 efx_process_channel_now(channel);
138 if (efx->last_irq_cpu >= 0)
139 goto success;
140 }
141
Ben Hutchings152b6a62009-11-29 03:43:56 +0000142 efx_nic_generate_interrupt(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100143
144 /* Wait for arrival of test interrupt. */
145 EFX_LOG(efx, "waiting for test interrupt\n");
146 schedule_timeout_uninterruptible(HZ / 10);
147 if (efx->last_irq_cpu >= 0)
148 goto success;
149
150 EFX_ERR(efx, "timed out waiting for interrupt\n");
151 return -ETIMEDOUT;
152
153 success:
Ben Hutchingsc4593022009-11-23 16:08:17 +0000154 EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
155 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100156 tests->interrupt = 1;
157 return 0;
158}
159
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100160/* Test generation and receipt of interrupting events */
161static int efx_test_eventq_irq(struct efx_channel *channel,
162 struct efx_self_tests *tests)
163{
164 unsigned int magic, count;
165
166 /* Channel specific code, limited to 20 bits */
167 magic = (0x00010150 + channel->channel);
168 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
169 channel->channel, magic);
170
171 tests->eventq_dma[channel->channel] = -1;
172 tests->eventq_int[channel->channel] = -1;
173 tests->eventq_poll[channel->channel] = -1;
174
175 /* Reset flag and zero magic word */
176 channel->efx->last_irq_cpu = -1;
177 channel->eventq_magic = 0;
178 smp_wmb();
179
Ben Hutchings152b6a62009-11-29 03:43:56 +0000180 efx_nic_generate_test_event(channel, magic);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100181
182 /* Wait for arrival of interrupt */
183 count = 0;
184 do {
185 schedule_timeout_uninterruptible(HZ / 100);
186
187 if (channel->work_pending)
188 efx_process_channel_now(channel);
189
190 if (channel->eventq_magic == magic)
191 goto eventq_ok;
192 } while (++count < 2);
193
194 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
195 channel->channel);
196
197 /* See if interrupt arrived */
198 if (channel->efx->last_irq_cpu >= 0) {
199 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
200 "during event queue test\n", channel->channel,
201 raw_smp_processor_id());
202 tests->eventq_int[channel->channel] = 1;
203 }
204
205 /* Check to see if event was received even if interrupt wasn't */
206 efx_process_channel_now(channel);
207 if (channel->eventq_magic == magic) {
208 EFX_ERR(channel->efx, "channel %d event was generated, but "
209 "failed to trigger an interrupt\n", channel->channel);
210 tests->eventq_dma[channel->channel] = 1;
211 }
212
213 return -ETIMEDOUT;
214 eventq_ok:
215 EFX_LOG(channel->efx, "channel %d event queue passed\n",
216 channel->channel);
217 tests->eventq_dma[channel->channel] = 1;
218 tests->eventq_int[channel->channel] = 1;
219 tests->eventq_poll[channel->channel] = 1;
220 return 0;
221}
222
Ben Hutchings17967212008-12-26 13:47:25 -0800223static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
224 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100225{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100226 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100227
Ben Hutchings17967212008-12-26 13:47:25 -0800228 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100229 return 0;
230
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100231 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000232 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100233 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100234 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100235}
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{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100250 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100251 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;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100261
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{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100335 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100336 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;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100371 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100372 struct efx_loopback_payload *payload;
373 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000374 int i;
375 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100376
377 /* Transmit N copies of buffer */
378 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100379 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100380 * transmit completion counting */
381 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
382 if (!skb)
383 return -ENOMEM;
384 state->skbs[i] = skb;
385 skb_get(skb);
386
387 /* Copy the payload in, incrementing the source address to
388 * exercise the rss vectors */
389 payload = ((struct efx_loopback_payload *)
390 skb_put(skb, sizeof(state->payload)));
391 memcpy(payload, &state->payload, sizeof(state->payload));
392 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
393
394 /* Ensure everything we've written is visible to the
395 * interrupt handler. */
396 smp_wmb();
397
Ben Hutchings55668612008-05-16 21:16:10 +0100398 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100399 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000400 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100401 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100402 netif_tx_unlock_bh(efx->net_dev);
403
404 if (rc != NETDEV_TX_OK) {
405 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
406 "of %d in %s loopback test\n", tx_queue->queue,
407 i + 1, state->packet_count, LOOPBACK_MODE(efx));
408
409 /* Defer cleaning up the other skbs for the caller */
410 kfree_skb(skb);
411 return -EPIPE;
412 }
413 }
414
415 return 0;
416}
417
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100418static int efx_poll_loopback(struct efx_nic *efx)
419{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100420 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100421 struct efx_channel *channel;
422
423 /* NAPI polling is not enabled, so process channels
424 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100425 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100426 if (channel->work_pending)
427 efx_process_channel_now(channel);
428 }
429 return atomic_read(&state->rx_good) == state->packet_count;
430}
431
432static int efx_end_loopback(struct efx_tx_queue *tx_queue,
433 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100434{
435 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100436 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100437 struct sk_buff *skb;
438 int tx_done = 0, rx_good, rx_bad;
439 int i, rc = 0;
440
Ben Hutchings55668612008-05-16 21:16:10 +0100441 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100442 netif_tx_lock_bh(efx->net_dev);
443
444 /* Count the number of tx completions, and decrement the refcnt. Any
445 * skbs not already completed will be free'd when the queue is flushed */
446 for (i=0; i < state->packet_count; i++) {
447 skb = state->skbs[i];
448 if (skb && !skb_shared(skb))
449 ++tx_done;
450 dev_kfree_skb_any(skb);
451 }
452
Ben Hutchings55668612008-05-16 21:16:10 +0100453 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100454 netif_tx_unlock_bh(efx->net_dev);
455
456 /* Check TX completion and received packet counts */
457 rx_good = atomic_read(&state->rx_good);
458 rx_bad = atomic_read(&state->rx_bad);
459 if (tx_done != state->packet_count) {
460 /* Don't free the skbs; they will be picked up on TX
461 * overflow or channel teardown.
462 */
463 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
464 "TX completion events in %s loopback test\n",
465 tx_queue->queue, tx_done, state->packet_count,
466 LOOPBACK_MODE(efx));
467 rc = -ETIMEDOUT;
468 /* Allow to fall through so we see the RX errors as well */
469 }
470
471 /* We may always be up to a flush away from our desired packet total */
472 if (rx_good != state->packet_count) {
473 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
474 "received packets in %s loopback test\n",
475 tx_queue->queue, rx_good, state->packet_count,
476 LOOPBACK_MODE(efx));
477 rc = -ETIMEDOUT;
478 /* Fall through */
479 }
480
481 /* Update loopback test structure */
482 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
483 lb_tests->tx_done[tx_queue->queue] += tx_done;
484 lb_tests->rx_good += rx_good;
485 lb_tests->rx_bad += rx_bad;
486
487 return rc;
488}
489
490static int
491efx_test_loopback(struct efx_tx_queue *tx_queue,
492 struct efx_loopback_self_tests *lb_tests)
493{
494 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100495 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100496 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100497
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100498 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100499 /* Determine how many packets to send */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000500 state->packet_count = EFX_TXQ_SIZE / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100501 state->packet_count = min(1 << (i << 2), state->packet_count);
502 state->skbs = kzalloc(sizeof(state->skbs[0]) *
503 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100504 if (!state->skbs)
505 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100506 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100507
508 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
509 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
510 state->packet_count);
511
512 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100513 begin_rc = efx_begin_loopback(tx_queue);
514
515 /* This will normally complete very quickly, but be
516 * prepared to wait up to 100 ms. */
517 msleep(1);
518 if (!efx_poll_loopback(efx)) {
519 msleep(100);
520 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100521 }
522
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100523 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100524 kfree(state->skbs);
525
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100526 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100527 /* Wait a while to ensure there are no packets
528 * floating around after a failure. */
529 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100530 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100531 }
532 }
533
534 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
535 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
536 state->packet_count);
537
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100538 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100539}
540
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000541/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
542 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
543 * to delay and retry. Therefore, it's safer to just poll directly. Wait
544 * for link up and any faults to dissipate. */
545static int efx_wait_for_link(struct efx_nic *efx)
546{
547 struct efx_link_state *link_state = &efx->link_state;
548 int count;
549 bool link_up;
550
551 for (count = 0; count < 40; count++) {
552 schedule_timeout_uninterruptible(HZ / 10);
553
554 if (efx->type->monitor != NULL) {
555 mutex_lock(&efx->mac_lock);
556 efx->type->monitor(efx);
557 mutex_unlock(&efx->mac_lock);
558 } else {
559 struct efx_channel *channel = &efx->channel[0];
560 if (channel->work_pending)
561 efx_process_channel_now(channel);
562 }
563
564 mutex_lock(&efx->mac_lock);
565 link_up = link_state->up;
566 if (link_up)
567 link_up = !efx->mac_op->check_fault(efx);
568 mutex_unlock(&efx->mac_lock);
569
570 if (link_up)
571 return 0;
572 }
573
574 return -ETIMEDOUT;
575}
576
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800577static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100578 unsigned int loopback_modes)
579{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100580 enum efx_loopback_mode mode;
581 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100582 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000583 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100584
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100585 /* Set the port loopback_selftest member. From this point on
586 * all received packets will be dropped. Mark the state as
587 * "flushing" so all inflight packets are dropped */
588 state = kzalloc(sizeof(*state), GFP_KERNEL);
589 if (state == NULL)
590 return -ENOMEM;
591 BUG_ON(efx->loopback_selftest);
592 state->flush = true;
593 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100594
595 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100596 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100597 if (!(loopback_modes & (1 << mode)))
598 continue;
599
600 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100601 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000602 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100603 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000604 rc = __efx_reconfigure_port(efx);
605 mutex_unlock(&efx->mac_lock);
606 if (rc) {
607 EFX_ERR(efx, "unable to move into %s loopback\n",
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100608 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100609 goto out;
610 }
611
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000612 rc = efx_wait_for_link(efx);
613 if (rc) {
614 EFX_ERR(efx, "loopback %s never came up\n",
615 LOOPBACK_MODE(efx));
616 goto out;
617 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100618
Ben Hutchings5298c372010-04-28 09:30:30 +0000619 /* Test both types of TX queue */
620 efx_for_each_channel_tx_queue(tx_queue, &efx->channel[0]) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100621 state->offload_csum = (tx_queue->queue ==
622 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100623 rc = efx_test_loopback(tx_queue,
624 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100625 if (rc)
626 goto out;
627 }
628 }
629
630 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100631 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100632 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100633 efx->loopback_selftest = NULL;
634 wmb();
635 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100636
637 return rc;
638}
639
640/**************************************************************************
641 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800642 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100643 *
644 *************************************************************************/
645
Ben Hutchings2ef30682008-12-26 13:47:04 -0800646int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
647 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100648{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100649 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
650 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000651 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800652 struct efx_channel *channel;
653 int rc_test = 0, rc_reset = 0, rc;
654
655 /* Online (i.e. non-disruptive) testing
656 * This checks interrupt generation, event delivery and PHY presence. */
657
Ben Hutchings4f16c072010-02-03 09:30:50 +0000658 rc = efx_test_phy_alive(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800659 if (rc && !rc_test)
660 rc_test = rc;
661
662 rc = efx_test_nvram(efx, tests);
663 if (rc && !rc_test)
664 rc_test = rc;
665
666 rc = efx_test_interrupts(efx, tests);
667 if (rc && !rc_test)
668 rc_test = rc;
669
670 efx_for_each_channel(channel, efx) {
671 rc = efx_test_eventq_irq(channel, tests);
672 if (rc && !rc_test)
673 rc_test = rc;
674 }
675
676 if (rc_test)
677 return rc_test;
678
679 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800680 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800681
682 /* Offline (i.e. disruptive) testing
683 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100684
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100685 /* force the carrier state off so the kernel doesn't transmit during
686 * the loopback test, and the watchdog timeout doesn't fire. Also put
687 * falcon into loopback for the register test.
688 */
689 mutex_lock(&efx->mac_lock);
690 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800691 if (efx->loopback_modes) {
692 /* We need the 312 clock from the PHY to test the XMAC
693 * registers, so move into XGMII loopback if available */
694 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
695 efx->loopback_mode = LOOPBACK_XGMII;
696 else
697 efx->loopback_mode = __ffs(efx->loopback_modes);
698 }
699
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100700 __efx_reconfigure_port(efx);
701 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100702
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100703 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000704 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100705
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100706 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800707 if (rc && !rc_test)
708 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100709
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100710 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000711 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100712
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800713 /* Ensure that the phy is powered and out of loopback
714 * for the bist and loopback tests */
715 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100716 efx->loopback_mode = LOOPBACK_NONE;
717
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000718 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800719 if (rc && !rc_reset)
720 rc_reset = rc;
721
722 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100723 EFX_ERR(efx, "Unable to recover from chip test\n");
724 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800725 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100726 }
727
Ben Hutchings17967212008-12-26 13:47:25 -0800728 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800729 if (rc && !rc_test)
730 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100731
Ben Hutchings2ef30682008-12-26 13:47:04 -0800732 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
733 if (rc && !rc_test)
734 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100735
736 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000737 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100738 efx->phy_mode = phy_mode;
739 efx->port_inhibited = false;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000740 efx->loopback_mode = loopback_mode;
741 __efx_reconfigure_port(efx);
742 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100743
Ben Hutchings2ef30682008-12-26 13:47:04 -0800744 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100745}
746