sfc: falcon: use padding to fix alignment in loopback test
[linux-2.6-block.git] / drivers / net / ethernet / sfc / falcon / selftest.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2006 Fen Systems Ltd.
5  * Copyright 2006-2012 Solarflare Communications Inc.
6  */
7
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/pci.h>
13 #include <linux/ethtool.h>
14 #include <linux/ip.h>
15 #include <linux/in.h>
16 #include <linux/udp.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include "net_driver.h"
20 #include "efx.h"
21 #include "nic.h"
22 #include "selftest.h"
23 #include "workarounds.h"
24
25 /* IRQ latency can be enormous because:
26  * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
27  *   slow serial console or an old IDE driver doing error recovery
28  * - The PREEMPT_RT patches mostly deal with this, but also allow a
29  *   tasklet or normal task to be given higher priority than our IRQ
30  *   threads
31  * Try to avoid blaming the hardware for this.
32  */
33 #define IRQ_TIMEOUT HZ
34
35 /*
36  * Loopback test packet structure
37  *
38  * The self-test should stress every RSS vector, and unfortunately
39  * Falcon only performs RSS on TCP/UDP packets.
40  */
41 struct ef4_loopback_payload {
42         char pad[2]; /* Ensures ip is 4-byte aligned */
43         struct ethhdr header;
44         struct iphdr ip;
45         struct udphdr udp;
46         __be16 iteration;
47         char msg[64];
48 } __packed __aligned(4);
49 #define EF4_LOOPBACK_PAYLOAD_LEN        (sizeof(struct ef4_loopback_payload) - \
50                                          offsetof(struct ef4_loopback_payload, \
51                                                   header))
52
53 /* Loopback test source MAC address */
54 static const u8 payload_source[ETH_ALEN] __aligned(2) = {
55         0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
56 };
57
58 static const char payload_msg[] =
59         "Hello world! This is an Efx loopback test in progress!";
60
61 /* Interrupt mode names */
62 static const unsigned int ef4_interrupt_mode_max = EF4_INT_MODE_MAX;
63 static const char *const ef4_interrupt_mode_names[] = {
64         [EF4_INT_MODE_MSIX]   = "MSI-X",
65         [EF4_INT_MODE_MSI]    = "MSI",
66         [EF4_INT_MODE_LEGACY] = "legacy",
67 };
68 #define INT_MODE(efx) \
69         STRING_TABLE_LOOKUP(efx->interrupt_mode, ef4_interrupt_mode)
70
71 /**
72  * struct ef4_loopback_state - persistent state during a loopback selftest
73  * @flush:              Drop all packets in ef4_loopback_rx_packet
74  * @packet_count:       Number of packets being used in this test
75  * @skbs:               An array of skbs transmitted
76  * @offload_csum:       Checksums are being offloaded
77  * @rx_good:            RX good packet count
78  * @rx_bad:             RX bad packet count
79  * @payload:            Payload used in tests
80  */
81 struct ef4_loopback_state {
82         bool flush;
83         int packet_count;
84         struct sk_buff **skbs;
85         bool offload_csum;
86         atomic_t rx_good;
87         atomic_t rx_bad;
88         struct ef4_loopback_payload payload;
89 };
90
91 /* How long to wait for all the packets to arrive (in ms) */
92 #define LOOPBACK_TIMEOUT_MS 1000
93
94 /**************************************************************************
95  *
96  * MII, NVRAM and register tests
97  *
98  **************************************************************************/
99
100 static int ef4_test_phy_alive(struct ef4_nic *efx, struct ef4_self_tests *tests)
101 {
102         int rc = 0;
103
104         if (efx->phy_op->test_alive) {
105                 rc = efx->phy_op->test_alive(efx);
106                 tests->phy_alive = rc ? -1 : 1;
107         }
108
109         return rc;
110 }
111
112 static int ef4_test_nvram(struct ef4_nic *efx, struct ef4_self_tests *tests)
113 {
114         int rc = 0;
115
116         if (efx->type->test_nvram) {
117                 rc = efx->type->test_nvram(efx);
118                 if (rc == -EPERM)
119                         rc = 0;
120                 else
121                         tests->nvram = rc ? -1 : 1;
122         }
123
124         return rc;
125 }
126
127 /**************************************************************************
128  *
129  * Interrupt and event queue testing
130  *
131  **************************************************************************/
132
133 /* Test generation and receipt of interrupts */
134 static int ef4_test_interrupts(struct ef4_nic *efx,
135                                struct ef4_self_tests *tests)
136 {
137         unsigned long timeout, wait;
138         int cpu;
139         int rc;
140
141         netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
142         tests->interrupt = -1;
143
144         rc = ef4_nic_irq_test_start(efx);
145         if (rc == -ENOTSUPP) {
146                 netif_dbg(efx, drv, efx->net_dev,
147                           "direct interrupt testing not supported\n");
148                 tests->interrupt = 0;
149                 return 0;
150         }
151
152         timeout = jiffies + IRQ_TIMEOUT;
153         wait = 1;
154
155         /* Wait for arrival of test interrupt. */
156         netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
157         do {
158                 schedule_timeout_uninterruptible(wait);
159                 cpu = ef4_nic_irq_test_irq_cpu(efx);
160                 if (cpu >= 0)
161                         goto success;
162                 wait *= 2;
163         } while (time_before(jiffies, timeout));
164
165         netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
166         return -ETIMEDOUT;
167
168  success:
169         netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
170                   INT_MODE(efx), cpu);
171         tests->interrupt = 1;
172         return 0;
173 }
174
175 /* Test generation and receipt of interrupting events */
176 static int ef4_test_eventq_irq(struct ef4_nic *efx,
177                                struct ef4_self_tests *tests)
178 {
179         struct ef4_channel *channel;
180         unsigned int read_ptr[EF4_MAX_CHANNELS];
181         unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
182         unsigned long timeout, wait;
183
184         BUILD_BUG_ON(EF4_MAX_CHANNELS > BITS_PER_LONG);
185
186         ef4_for_each_channel(channel, efx) {
187                 read_ptr[channel->channel] = channel->eventq_read_ptr;
188                 set_bit(channel->channel, &dma_pend);
189                 set_bit(channel->channel, &int_pend);
190                 ef4_nic_event_test_start(channel);
191         }
192
193         timeout = jiffies + IRQ_TIMEOUT;
194         wait = 1;
195
196         /* Wait for arrival of interrupts.  NAPI processing may or may
197          * not complete in time, but we can cope in any case.
198          */
199         do {
200                 schedule_timeout_uninterruptible(wait);
201
202                 ef4_for_each_channel(channel, efx) {
203                         ef4_stop_eventq(channel);
204                         if (channel->eventq_read_ptr !=
205                             read_ptr[channel->channel]) {
206                                 set_bit(channel->channel, &napi_ran);
207                                 clear_bit(channel->channel, &dma_pend);
208                                 clear_bit(channel->channel, &int_pend);
209                         } else {
210                                 if (ef4_nic_event_present(channel))
211                                         clear_bit(channel->channel, &dma_pend);
212                                 if (ef4_nic_event_test_irq_cpu(channel) >= 0)
213                                         clear_bit(channel->channel, &int_pend);
214                         }
215                         ef4_start_eventq(channel);
216                 }
217
218                 wait *= 2;
219         } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
220
221         ef4_for_each_channel(channel, efx) {
222                 bool dma_seen = !test_bit(channel->channel, &dma_pend);
223                 bool int_seen = !test_bit(channel->channel, &int_pend);
224
225                 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
226                 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
227
228                 if (dma_seen && int_seen) {
229                         netif_dbg(efx, drv, efx->net_dev,
230                                   "channel %d event queue passed (with%s NAPI)\n",
231                                   channel->channel,
232                                   test_bit(channel->channel, &napi_ran) ?
233                                   "" : "out");
234                 } else {
235                         /* Report failure and whether either interrupt or DMA
236                          * worked
237                          */
238                         netif_err(efx, drv, efx->net_dev,
239                                   "channel %d timed out waiting for event queue\n",
240                                   channel->channel);
241                         if (int_seen)
242                                 netif_err(efx, drv, efx->net_dev,
243                                           "channel %d saw interrupt "
244                                           "during event queue test\n",
245                                           channel->channel);
246                         if (dma_seen)
247                                 netif_err(efx, drv, efx->net_dev,
248                                           "channel %d event was generated, but "
249                                           "failed to trigger an interrupt\n",
250                                           channel->channel);
251                 }
252         }
253
254         return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
255 }
256
257 static int ef4_test_phy(struct ef4_nic *efx, struct ef4_self_tests *tests,
258                         unsigned flags)
259 {
260         int rc;
261
262         if (!efx->phy_op->run_tests)
263                 return 0;
264
265         mutex_lock(&efx->mac_lock);
266         rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
267         mutex_unlock(&efx->mac_lock);
268         if (rc == -EPERM)
269                 rc = 0;
270         else
271                 netif_info(efx, drv, efx->net_dev,
272                            "%s phy selftest\n", rc ? "Failed" : "Passed");
273
274         return rc;
275 }
276
277 /**************************************************************************
278  *
279  * Loopback testing
280  * NB Only one loopback test can be executing concurrently.
281  *
282  **************************************************************************/
283
284 /* Loopback test RX callback
285  * This is called for each received packet during loopback testing.
286  */
287 void ef4_loopback_rx_packet(struct ef4_nic *efx,
288                             const char *buf_ptr, int pkt_len)
289 {
290         struct ef4_loopback_state *state = efx->loopback_selftest;
291         struct ef4_loopback_payload received;
292         struct ef4_loopback_payload *payload;
293
294         BUG_ON(!buf_ptr);
295
296         /* If we are just flushing, then drop the packet */
297         if ((state == NULL) || state->flush)
298                 return;
299
300         payload = &state->payload;
301
302         memcpy(&received.header, buf_ptr,
303                min_t(int, pkt_len, EF4_LOOPBACK_PAYLOAD_LEN));
304         received.ip.saddr = payload->ip.saddr;
305         if (state->offload_csum)
306                 received.ip.check = payload->ip.check;
307
308         /* Check that header exists */
309         if (pkt_len < sizeof(received.header)) {
310                 netif_err(efx, drv, efx->net_dev,
311                           "saw runt RX packet (length %d) in %s loopback "
312                           "test\n", pkt_len, LOOPBACK_MODE(efx));
313                 goto err;
314         }
315
316         /* Check that the ethernet header exists */
317         if (memcmp(&received.header, &payload->header, ETH_HLEN) != 0) {
318                 netif_err(efx, drv, efx->net_dev,
319                           "saw non-loopback RX packet in %s loopback test\n",
320                           LOOPBACK_MODE(efx));
321                 goto err;
322         }
323
324         /* Check packet length */
325         if (pkt_len != EF4_LOOPBACK_PAYLOAD_LEN) {
326                 netif_err(efx, drv, efx->net_dev,
327                           "saw incorrect RX packet length %d (wanted %d) in "
328                           "%s loopback test\n", pkt_len,
329                           (int)EF4_LOOPBACK_PAYLOAD_LEN, LOOPBACK_MODE(efx));
330                 goto err;
331         }
332
333         /* Check that IP header matches */
334         if (memcmp(&received.ip, &payload->ip, sizeof(payload->ip)) != 0) {
335                 netif_err(efx, drv, efx->net_dev,
336                           "saw corrupted IP header in %s loopback test\n",
337                           LOOPBACK_MODE(efx));
338                 goto err;
339         }
340
341         /* Check that msg and padding matches */
342         if (memcmp(&received.msg, &payload->msg, sizeof(received.msg)) != 0) {
343                 netif_err(efx, drv, efx->net_dev,
344                           "saw corrupted RX packet in %s loopback test\n",
345                           LOOPBACK_MODE(efx));
346                 goto err;
347         }
348
349         /* Check that iteration matches */
350         if (received.iteration != payload->iteration) {
351                 netif_err(efx, drv, efx->net_dev,
352                           "saw RX packet from iteration %d (wanted %d) in "
353                           "%s loopback test\n", ntohs(received.iteration),
354                           ntohs(payload->iteration), LOOPBACK_MODE(efx));
355                 goto err;
356         }
357
358         /* Increase correct RX count */
359         netif_vdbg(efx, drv, efx->net_dev,
360                    "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
361
362         atomic_inc(&state->rx_good);
363         return;
364
365  err:
366 #ifdef DEBUG
367         if (atomic_read(&state->rx_bad) == 0) {
368                 netif_err(efx, drv, efx->net_dev, "received packet:\n");
369                 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
370                                buf_ptr, pkt_len, 0);
371                 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
372                 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
373                                &state->payload.header, EF4_LOOPBACK_PAYLOAD_LEN,
374                                0);
375         }
376 #endif
377         atomic_inc(&state->rx_bad);
378 }
379
380 /* Initialise an ef4_selftest_state for a new iteration */
381 static void ef4_iterate_state(struct ef4_nic *efx)
382 {
383         struct ef4_loopback_state *state = efx->loopback_selftest;
384         struct net_device *net_dev = efx->net_dev;
385         struct ef4_loopback_payload *payload = &state->payload;
386
387         /* Initialise the layerII header */
388         ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
389         ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
390         payload->header.h_proto = htons(ETH_P_IP);
391
392         /* saddr set later and used as incrementing count */
393         payload->ip.daddr = htonl(INADDR_LOOPBACK);
394         payload->ip.ihl = 5;
395         payload->ip.check = (__force __sum16) htons(0xdead);
396         payload->ip.tot_len = htons(sizeof(*payload) -
397                                     offsetof(struct ef4_loopback_payload, ip));
398         payload->ip.version = IPVERSION;
399         payload->ip.protocol = IPPROTO_UDP;
400
401         /* Initialise udp header */
402         payload->udp.source = 0;
403         payload->udp.len = htons(sizeof(*payload) -
404                                  offsetof(struct ef4_loopback_payload, udp));
405         payload->udp.check = 0; /* checksum ignored */
406
407         /* Fill out payload */
408         payload->iteration = htons(ntohs(payload->iteration) + 1);
409         memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
410
411         /* Fill out remaining state members */
412         atomic_set(&state->rx_good, 0);
413         atomic_set(&state->rx_bad, 0);
414         smp_wmb();
415 }
416
417 static int ef4_begin_loopback(struct ef4_tx_queue *tx_queue)
418 {
419         struct ef4_nic *efx = tx_queue->efx;
420         struct ef4_loopback_state *state = efx->loopback_selftest;
421         struct ef4_loopback_payload *payload;
422         struct sk_buff *skb;
423         int i;
424         netdev_tx_t rc;
425
426         /* Transmit N copies of buffer */
427         for (i = 0; i < state->packet_count; i++) {
428                 /* Allocate an skb, holding an extra reference for
429                  * transmit completion counting */
430                 skb = alloc_skb(EF4_LOOPBACK_PAYLOAD_LEN, GFP_KERNEL);
431                 if (!skb)
432                         return -ENOMEM;
433                 state->skbs[i] = skb;
434                 skb_get(skb);
435
436                 /* Copy the payload in, incrementing the source address to
437                  * exercise the rss vectors */
438                 payload = skb_put(skb, sizeof(state->payload));
439                 memcpy(payload, &state->payload, sizeof(state->payload));
440                 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
441                 /* Strip off the leading padding */
442                 skb_pull(skb, offsetof(struct ef4_loopback_payload, header));
443
444                 /* Ensure everything we've written is visible to the
445                  * interrupt handler. */
446                 smp_wmb();
447
448                 netif_tx_lock_bh(efx->net_dev);
449                 rc = ef4_enqueue_skb(tx_queue, skb);
450                 netif_tx_unlock_bh(efx->net_dev);
451
452                 if (rc != NETDEV_TX_OK) {
453                         netif_err(efx, drv, efx->net_dev,
454                                   "TX queue %d could not transmit packet %d of "
455                                   "%d in %s loopback test\n", tx_queue->queue,
456                                   i + 1, state->packet_count,
457                                   LOOPBACK_MODE(efx));
458
459                         /* Defer cleaning up the other skbs for the caller */
460                         kfree_skb(skb);
461                         return -EPIPE;
462                 }
463         }
464
465         return 0;
466 }
467
468 static int ef4_poll_loopback(struct ef4_nic *efx)
469 {
470         struct ef4_loopback_state *state = efx->loopback_selftest;
471
472         return atomic_read(&state->rx_good) == state->packet_count;
473 }
474
475 static int ef4_end_loopback(struct ef4_tx_queue *tx_queue,
476                             struct ef4_loopback_self_tests *lb_tests)
477 {
478         struct ef4_nic *efx = tx_queue->efx;
479         struct ef4_loopback_state *state = efx->loopback_selftest;
480         struct sk_buff *skb;
481         int tx_done = 0, rx_good, rx_bad;
482         int i, rc = 0;
483
484         netif_tx_lock_bh(efx->net_dev);
485
486         /* Count the number of tx completions, and decrement the refcnt. Any
487          * skbs not already completed will be free'd when the queue is flushed */
488         for (i = 0; i < state->packet_count; i++) {
489                 skb = state->skbs[i];
490                 if (skb && !skb_shared(skb))
491                         ++tx_done;
492                 dev_kfree_skb(skb);
493         }
494
495         netif_tx_unlock_bh(efx->net_dev);
496
497         /* Check TX completion and received packet counts */
498         rx_good = atomic_read(&state->rx_good);
499         rx_bad = atomic_read(&state->rx_bad);
500         if (tx_done != state->packet_count) {
501                 /* Don't free the skbs; they will be picked up on TX
502                  * overflow or channel teardown.
503                  */
504                 netif_err(efx, drv, efx->net_dev,
505                           "TX queue %d saw only %d out of an expected %d "
506                           "TX completion events in %s loopback test\n",
507                           tx_queue->queue, tx_done, state->packet_count,
508                           LOOPBACK_MODE(efx));
509                 rc = -ETIMEDOUT;
510                 /* Allow to fall through so we see the RX errors as well */
511         }
512
513         /* We may always be up to a flush away from our desired packet total */
514         if (rx_good != state->packet_count) {
515                 netif_dbg(efx, drv, efx->net_dev,
516                           "TX queue %d saw only %d out of an expected %d "
517                           "received packets in %s loopback test\n",
518                           tx_queue->queue, rx_good, state->packet_count,
519                           LOOPBACK_MODE(efx));
520                 rc = -ETIMEDOUT;
521                 /* Fall through */
522         }
523
524         /* Update loopback test structure */
525         lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
526         lb_tests->tx_done[tx_queue->queue] += tx_done;
527         lb_tests->rx_good += rx_good;
528         lb_tests->rx_bad += rx_bad;
529
530         return rc;
531 }
532
533 static int
534 ef4_test_loopback(struct ef4_tx_queue *tx_queue,
535                   struct ef4_loopback_self_tests *lb_tests)
536 {
537         struct ef4_nic *efx = tx_queue->efx;
538         struct ef4_loopback_state *state = efx->loopback_selftest;
539         int i, begin_rc, end_rc;
540
541         for (i = 0; i < 3; i++) {
542                 /* Determine how many packets to send */
543                 state->packet_count = efx->txq_entries / 3;
544                 state->packet_count = min(1 << (i << 2), state->packet_count);
545                 state->skbs = kcalloc(state->packet_count,
546                                       sizeof(state->skbs[0]), GFP_KERNEL);
547                 if (!state->skbs)
548                         return -ENOMEM;
549                 state->flush = false;
550
551                 netif_dbg(efx, drv, efx->net_dev,
552                           "TX queue %d testing %s loopback with %d packets\n",
553                           tx_queue->queue, LOOPBACK_MODE(efx),
554                           state->packet_count);
555
556                 ef4_iterate_state(efx);
557                 begin_rc = ef4_begin_loopback(tx_queue);
558
559                 /* This will normally complete very quickly, but be
560                  * prepared to wait much longer. */
561                 msleep(1);
562                 if (!ef4_poll_loopback(efx)) {
563                         msleep(LOOPBACK_TIMEOUT_MS);
564                         ef4_poll_loopback(efx);
565                 }
566
567                 end_rc = ef4_end_loopback(tx_queue, lb_tests);
568                 kfree(state->skbs);
569
570                 if (begin_rc || end_rc) {
571                         /* Wait a while to ensure there are no packets
572                          * floating around after a failure. */
573                         schedule_timeout_uninterruptible(HZ / 10);
574                         return begin_rc ? begin_rc : end_rc;
575                 }
576         }
577
578         netif_dbg(efx, drv, efx->net_dev,
579                   "TX queue %d passed %s loopback test with a burst length "
580                   "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
581                   state->packet_count);
582
583         return 0;
584 }
585
586 /* Wait for link up. On Falcon, we would prefer to rely on ef4_monitor, but
587  * any contention on the mac lock (via e.g. ef4_mac_mcast_work) causes it
588  * to delay and retry. Therefore, it's safer to just poll directly. Wait
589  * for link up and any faults to dissipate. */
590 static int ef4_wait_for_link(struct ef4_nic *efx)
591 {
592         struct ef4_link_state *link_state = &efx->link_state;
593         int count, link_up_count = 0;
594         bool link_up;
595
596         for (count = 0; count < 40; count++) {
597                 schedule_timeout_uninterruptible(HZ / 10);
598
599                 if (efx->type->monitor != NULL) {
600                         mutex_lock(&efx->mac_lock);
601                         efx->type->monitor(efx);
602                         mutex_unlock(&efx->mac_lock);
603                 }
604
605                 mutex_lock(&efx->mac_lock);
606                 link_up = link_state->up;
607                 if (link_up)
608                         link_up = !efx->type->check_mac_fault(efx);
609                 mutex_unlock(&efx->mac_lock);
610
611                 if (link_up) {
612                         if (++link_up_count == 2)
613                                 return 0;
614                 } else {
615                         link_up_count = 0;
616                 }
617         }
618
619         return -ETIMEDOUT;
620 }
621
622 static int ef4_test_loopbacks(struct ef4_nic *efx, struct ef4_self_tests *tests,
623                               unsigned int loopback_modes)
624 {
625         enum ef4_loopback_mode mode;
626         struct ef4_loopback_state *state;
627         struct ef4_channel *channel =
628                 ef4_get_channel(efx, efx->tx_channel_offset);
629         struct ef4_tx_queue *tx_queue;
630         int rc = 0;
631
632         /* Set the port loopback_selftest member. From this point on
633          * all received packets will be dropped. Mark the state as
634          * "flushing" so all inflight packets are dropped */
635         state = kzalloc(sizeof(*state), GFP_KERNEL);
636         if (state == NULL)
637                 return -ENOMEM;
638         BUG_ON(efx->loopback_selftest);
639         state->flush = true;
640         efx->loopback_selftest = state;
641
642         /* Test all supported loopback modes */
643         for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
644                 if (!(loopback_modes & (1 << mode)))
645                         continue;
646
647                 /* Move the port into the specified loopback mode. */
648                 state->flush = true;
649                 mutex_lock(&efx->mac_lock);
650                 efx->loopback_mode = mode;
651                 rc = __ef4_reconfigure_port(efx);
652                 mutex_unlock(&efx->mac_lock);
653                 if (rc) {
654                         netif_err(efx, drv, efx->net_dev,
655                                   "unable to move into %s loopback\n",
656                                   LOOPBACK_MODE(efx));
657                         goto out;
658                 }
659
660                 rc = ef4_wait_for_link(efx);
661                 if (rc) {
662                         netif_err(efx, drv, efx->net_dev,
663                                   "loopback %s never came up\n",
664                                   LOOPBACK_MODE(efx));
665                         goto out;
666                 }
667
668                 /* Test all enabled types of TX queue */
669                 ef4_for_each_channel_tx_queue(tx_queue, channel) {
670                         state->offload_csum = (tx_queue->queue &
671                                                EF4_TXQ_TYPE_OFFLOAD);
672                         rc = ef4_test_loopback(tx_queue,
673                                                &tests->loopback[mode]);
674                         if (rc)
675                                 goto out;
676                 }
677         }
678
679  out:
680         /* Remove the flush. The caller will remove the loopback setting */
681         state->flush = true;
682         efx->loopback_selftest = NULL;
683         wmb();
684         kfree(state);
685
686         if (rc == -EPERM)
687                 rc = 0;
688
689         return rc;
690 }
691
692 /**************************************************************************
693  *
694  * Entry point
695  *
696  *************************************************************************/
697
698 int ef4_selftest(struct ef4_nic *efx, struct ef4_self_tests *tests,
699                  unsigned flags)
700 {
701         enum ef4_loopback_mode loopback_mode = efx->loopback_mode;
702         int phy_mode = efx->phy_mode;
703         int rc_test = 0, rc_reset, rc;
704
705         ef4_selftest_async_cancel(efx);
706
707         /* Online (i.e. non-disruptive) testing
708          * This checks interrupt generation, event delivery and PHY presence. */
709
710         rc = ef4_test_phy_alive(efx, tests);
711         if (rc && !rc_test)
712                 rc_test = rc;
713
714         rc = ef4_test_nvram(efx, tests);
715         if (rc && !rc_test)
716                 rc_test = rc;
717
718         rc = ef4_test_interrupts(efx, tests);
719         if (rc && !rc_test)
720                 rc_test = rc;
721
722         rc = ef4_test_eventq_irq(efx, tests);
723         if (rc && !rc_test)
724                 rc_test = rc;
725
726         if (rc_test)
727                 return rc_test;
728
729         if (!(flags & ETH_TEST_FL_OFFLINE))
730                 return ef4_test_phy(efx, tests, flags);
731
732         /* Offline (i.e. disruptive) testing
733          * This checks MAC and PHY loopback on the specified port. */
734
735         /* Detach the device so the kernel doesn't transmit during the
736          * loopback test and the watchdog timeout doesn't fire.
737          */
738         ef4_device_detach_sync(efx);
739
740         if (efx->type->test_chip) {
741                 rc_reset = efx->type->test_chip(efx, tests);
742                 if (rc_reset) {
743                         netif_err(efx, hw, efx->net_dev,
744                                   "Unable to recover from chip test\n");
745                         ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
746                         return rc_reset;
747                 }
748
749                 if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
750                         rc_test = -EIO;
751         }
752
753         /* Ensure that the phy is powered and out of loopback
754          * for the bist and loopback tests */
755         mutex_lock(&efx->mac_lock);
756         efx->phy_mode &= ~PHY_MODE_LOW_POWER;
757         efx->loopback_mode = LOOPBACK_NONE;
758         __ef4_reconfigure_port(efx);
759         mutex_unlock(&efx->mac_lock);
760
761         rc = ef4_test_phy(efx, tests, flags);
762         if (rc && !rc_test)
763                 rc_test = rc;
764
765         rc = ef4_test_loopbacks(efx, tests, efx->loopback_modes);
766         if (rc && !rc_test)
767                 rc_test = rc;
768
769         /* restore the PHY to the previous state */
770         mutex_lock(&efx->mac_lock);
771         efx->phy_mode = phy_mode;
772         efx->loopback_mode = loopback_mode;
773         __ef4_reconfigure_port(efx);
774         mutex_unlock(&efx->mac_lock);
775
776         netif_device_attach(efx->net_dev);
777
778         return rc_test;
779 }
780
781 void ef4_selftest_async_start(struct ef4_nic *efx)
782 {
783         struct ef4_channel *channel;
784
785         ef4_for_each_channel(channel, efx)
786                 ef4_nic_event_test_start(channel);
787         schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
788 }
789
790 void ef4_selftest_async_cancel(struct ef4_nic *efx)
791 {
792         cancel_delayed_work_sync(&efx->selftest_work);
793 }
794
795 void ef4_selftest_async_work(struct work_struct *data)
796 {
797         struct ef4_nic *efx = container_of(data, struct ef4_nic,
798                                            selftest_work.work);
799         struct ef4_channel *channel;
800         int cpu;
801
802         ef4_for_each_channel(channel, efx) {
803                 cpu = ef4_nic_event_test_irq_cpu(channel);
804                 if (cpu < 0)
805                         netif_err(efx, ifup, efx->net_dev,
806                                   "channel %d failed to trigger an interrupt\n",
807                                   channel->channel);
808                 else
809                         netif_dbg(efx, ifup, efx->net_dev,
810                                   "channel %d triggered interrupt on CPU %d\n",
811                                   channel->channel, cpu);
812         }
813 }