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