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