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