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