Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2005-2006 Fen Systems Ltd.
0005  * Copyright 2006-2012 Solarflare Communications Inc.
0006  */
0007 
0008 #include <linux/netdevice.h>
0009 #include <linux/module.h>
0010 #include <linux/delay.h>
0011 #include <linux/kernel_stat.h>
0012 #include <linux/pci.h>
0013 #include <linux/ethtool.h>
0014 #include <linux/ip.h>
0015 #include <linux/in.h>
0016 #include <linux/udp.h>
0017 #include <linux/rtnetlink.h>
0018 #include <linux/slab.h>
0019 #include "net_driver.h"
0020 #include "efx.h"
0021 #include "nic.h"
0022 #include "selftest.h"
0023 #include "workarounds.h"
0024 
0025 /* IRQ latency can be enormous because:
0026  * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
0027  *   slow serial console or an old IDE driver doing error recovery
0028  * - The PREEMPT_RT patches mostly deal with this, but also allow a
0029  *   tasklet or normal task to be given higher priority than our IRQ
0030  *   threads
0031  * Try to avoid blaming the hardware for this.
0032  */
0033 #define IRQ_TIMEOUT HZ
0034 
0035 /*
0036  * Loopback test packet structure
0037  *
0038  * The self-test should stress every RSS vector, and unfortunately
0039  * Falcon only performs RSS on TCP/UDP packets.
0040  */
0041 struct ef4_loopback_payload {
0042     struct ethhdr header;
0043     struct iphdr ip;
0044     struct udphdr udp;
0045     __be16 iteration;
0046     char msg[64];
0047 } __packed;
0048 
0049 /* Loopback test source MAC address */
0050 static const u8 payload_source[ETH_ALEN] __aligned(2) = {
0051     0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
0052 };
0053 
0054 static const char payload_msg[] =
0055     "Hello world! This is an Efx loopback test in progress!";
0056 
0057 /* Interrupt mode names */
0058 static const unsigned int ef4_interrupt_mode_max = EF4_INT_MODE_MAX;
0059 static const char *const ef4_interrupt_mode_names[] = {
0060     [EF4_INT_MODE_MSIX]   = "MSI-X",
0061     [EF4_INT_MODE_MSI]    = "MSI",
0062     [EF4_INT_MODE_LEGACY] = "legacy",
0063 };
0064 #define INT_MODE(efx) \
0065     STRING_TABLE_LOOKUP(efx->interrupt_mode, ef4_interrupt_mode)
0066 
0067 /**
0068  * struct ef4_loopback_state - persistent state during a loopback selftest
0069  * @flush:      Drop all packets in ef4_loopback_rx_packet
0070  * @packet_count:   Number of packets being used in this test
0071  * @skbs:       An array of skbs transmitted
0072  * @offload_csum:   Checksums are being offloaded
0073  * @rx_good:        RX good packet count
0074  * @rx_bad:     RX bad packet count
0075  * @payload:        Payload used in tests
0076  */
0077 struct ef4_loopback_state {
0078     bool flush;
0079     int packet_count;
0080     struct sk_buff **skbs;
0081     bool offload_csum;
0082     atomic_t rx_good;
0083     atomic_t rx_bad;
0084     struct ef4_loopback_payload payload;
0085 };
0086 
0087 /* How long to wait for all the packets to arrive (in ms) */
0088 #define LOOPBACK_TIMEOUT_MS 1000
0089 
0090 /**************************************************************************
0091  *
0092  * MII, NVRAM and register tests
0093  *
0094  **************************************************************************/
0095 
0096 static int ef4_test_phy_alive(struct ef4_nic *efx, struct ef4_self_tests *tests)
0097 {
0098     int rc = 0;
0099 
0100     if (efx->phy_op->test_alive) {
0101         rc = efx->phy_op->test_alive(efx);
0102         tests->phy_alive = rc ? -1 : 1;
0103     }
0104 
0105     return rc;
0106 }
0107 
0108 static int ef4_test_nvram(struct ef4_nic *efx, struct ef4_self_tests *tests)
0109 {
0110     int rc = 0;
0111 
0112     if (efx->type->test_nvram) {
0113         rc = efx->type->test_nvram(efx);
0114         if (rc == -EPERM)
0115             rc = 0;
0116         else
0117             tests->nvram = rc ? -1 : 1;
0118     }
0119 
0120     return rc;
0121 }
0122 
0123 /**************************************************************************
0124  *
0125  * Interrupt and event queue testing
0126  *
0127  **************************************************************************/
0128 
0129 /* Test generation and receipt of interrupts */
0130 static int ef4_test_interrupts(struct ef4_nic *efx,
0131                    struct ef4_self_tests *tests)
0132 {
0133     unsigned long timeout, wait;
0134     int cpu;
0135     int rc;
0136 
0137     netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
0138     tests->interrupt = -1;
0139 
0140     rc = ef4_nic_irq_test_start(efx);
0141     if (rc == -ENOTSUPP) {
0142         netif_dbg(efx, drv, efx->net_dev,
0143               "direct interrupt testing not supported\n");
0144         tests->interrupt = 0;
0145         return 0;
0146     }
0147 
0148     timeout = jiffies + IRQ_TIMEOUT;
0149     wait = 1;
0150 
0151     /* Wait for arrival of test interrupt. */
0152     netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
0153     do {
0154         schedule_timeout_uninterruptible(wait);
0155         cpu = ef4_nic_irq_test_irq_cpu(efx);
0156         if (cpu >= 0)
0157             goto success;
0158         wait *= 2;
0159     } while (time_before(jiffies, timeout));
0160 
0161     netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
0162     return -ETIMEDOUT;
0163 
0164  success:
0165     netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
0166           INT_MODE(efx), cpu);
0167     tests->interrupt = 1;
0168     return 0;
0169 }
0170 
0171 /* Test generation and receipt of interrupting events */
0172 static int ef4_test_eventq_irq(struct ef4_nic *efx,
0173                    struct ef4_self_tests *tests)
0174 {
0175     struct ef4_channel *channel;
0176     unsigned int read_ptr[EF4_MAX_CHANNELS];
0177     unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
0178     unsigned long timeout, wait;
0179 
0180     BUILD_BUG_ON(EF4_MAX_CHANNELS > BITS_PER_LONG);
0181 
0182     ef4_for_each_channel(channel, efx) {
0183         read_ptr[channel->channel] = channel->eventq_read_ptr;
0184         set_bit(channel->channel, &dma_pend);
0185         set_bit(channel->channel, &int_pend);
0186         ef4_nic_event_test_start(channel);
0187     }
0188 
0189     timeout = jiffies + IRQ_TIMEOUT;
0190     wait = 1;
0191 
0192     /* Wait for arrival of interrupts.  NAPI processing may or may
0193      * not complete in time, but we can cope in any case.
0194      */
0195     do {
0196         schedule_timeout_uninterruptible(wait);
0197 
0198         ef4_for_each_channel(channel, efx) {
0199             ef4_stop_eventq(channel);
0200             if (channel->eventq_read_ptr !=
0201                 read_ptr[channel->channel]) {
0202                 set_bit(channel->channel, &napi_ran);
0203                 clear_bit(channel->channel, &dma_pend);
0204                 clear_bit(channel->channel, &int_pend);
0205             } else {
0206                 if (ef4_nic_event_present(channel))
0207                     clear_bit(channel->channel, &dma_pend);
0208                 if (ef4_nic_event_test_irq_cpu(channel) >= 0)
0209                     clear_bit(channel->channel, &int_pend);
0210             }
0211             ef4_start_eventq(channel);
0212         }
0213 
0214         wait *= 2;
0215     } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
0216 
0217     ef4_for_each_channel(channel, efx) {
0218         bool dma_seen = !test_bit(channel->channel, &dma_pend);
0219         bool int_seen = !test_bit(channel->channel, &int_pend);
0220 
0221         tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
0222         tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
0223 
0224         if (dma_seen && int_seen) {
0225             netif_dbg(efx, drv, efx->net_dev,
0226                   "channel %d event queue passed (with%s NAPI)\n",
0227                   channel->channel,
0228                   test_bit(channel->channel, &napi_ran) ?
0229                   "" : "out");
0230         } else {
0231             /* Report failure and whether either interrupt or DMA
0232              * worked
0233              */
0234             netif_err(efx, drv, efx->net_dev,
0235                   "channel %d timed out waiting for event queue\n",
0236                   channel->channel);
0237             if (int_seen)
0238                 netif_err(efx, drv, efx->net_dev,
0239                       "channel %d saw interrupt "
0240                       "during event queue test\n",
0241                       channel->channel);
0242             if (dma_seen)
0243                 netif_err(efx, drv, efx->net_dev,
0244                       "channel %d event was generated, but "
0245                       "failed to trigger an interrupt\n",
0246                       channel->channel);
0247         }
0248     }
0249 
0250     return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
0251 }
0252 
0253 static int ef4_test_phy(struct ef4_nic *efx, struct ef4_self_tests *tests,
0254             unsigned flags)
0255 {
0256     int rc;
0257 
0258     if (!efx->phy_op->run_tests)
0259         return 0;
0260 
0261     mutex_lock(&efx->mac_lock);
0262     rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
0263     mutex_unlock(&efx->mac_lock);
0264     if (rc == -EPERM)
0265         rc = 0;
0266     else
0267         netif_info(efx, drv, efx->net_dev,
0268                "%s phy selftest\n", rc ? "Failed" : "Passed");
0269 
0270     return rc;
0271 }
0272 
0273 /**************************************************************************
0274  *
0275  * Loopback testing
0276  * NB Only one loopback test can be executing concurrently.
0277  *
0278  **************************************************************************/
0279 
0280 /* Loopback test RX callback
0281  * This is called for each received packet during loopback testing.
0282  */
0283 void ef4_loopback_rx_packet(struct ef4_nic *efx,
0284                 const char *buf_ptr, int pkt_len)
0285 {
0286     struct ef4_loopback_state *state = efx->loopback_selftest;
0287     struct ef4_loopback_payload *received;
0288     struct ef4_loopback_payload *payload;
0289 
0290     BUG_ON(!buf_ptr);
0291 
0292     /* If we are just flushing, then drop the packet */
0293     if ((state == NULL) || state->flush)
0294         return;
0295 
0296     payload = &state->payload;
0297 
0298     received = (struct ef4_loopback_payload *) buf_ptr;
0299     received->ip.saddr = payload->ip.saddr;
0300     if (state->offload_csum)
0301         received->ip.check = payload->ip.check;
0302 
0303     /* Check that header exists */
0304     if (pkt_len < sizeof(received->header)) {
0305         netif_err(efx, drv, efx->net_dev,
0306               "saw runt RX packet (length %d) in %s loopback "
0307               "test\n", pkt_len, LOOPBACK_MODE(efx));
0308         goto err;
0309     }
0310 
0311     /* Check that the ethernet header exists */
0312     if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
0313         netif_err(efx, drv, efx->net_dev,
0314               "saw non-loopback RX packet in %s loopback test\n",
0315               LOOPBACK_MODE(efx));
0316         goto err;
0317     }
0318 
0319     /* Check packet length */
0320     if (pkt_len != sizeof(*payload)) {
0321         netif_err(efx, drv, efx->net_dev,
0322               "saw incorrect RX packet length %d (wanted %d) in "
0323               "%s loopback test\n", pkt_len, (int)sizeof(*payload),
0324               LOOPBACK_MODE(efx));
0325         goto err;
0326     }
0327 
0328     /* Check that IP header matches */
0329     if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
0330         netif_err(efx, drv, efx->net_dev,
0331               "saw corrupted IP header in %s loopback test\n",
0332               LOOPBACK_MODE(efx));
0333         goto err;
0334     }
0335 
0336     /* Check that msg and padding matches */
0337     if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
0338         netif_err(efx, drv, efx->net_dev,
0339               "saw corrupted RX packet in %s loopback test\n",
0340               LOOPBACK_MODE(efx));
0341         goto err;
0342     }
0343 
0344     /* Check that iteration matches */
0345     if (received->iteration != payload->iteration) {
0346         netif_err(efx, drv, efx->net_dev,
0347               "saw RX packet from iteration %d (wanted %d) in "
0348               "%s loopback test\n", ntohs(received->iteration),
0349               ntohs(payload->iteration), LOOPBACK_MODE(efx));
0350         goto err;
0351     }
0352 
0353     /* Increase correct RX count */
0354     netif_vdbg(efx, drv, efx->net_dev,
0355            "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
0356 
0357     atomic_inc(&state->rx_good);
0358     return;
0359 
0360  err:
0361 #ifdef DEBUG
0362     if (atomic_read(&state->rx_bad) == 0) {
0363         netif_err(efx, drv, efx->net_dev, "received packet:\n");
0364         print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
0365                    buf_ptr, pkt_len, 0);
0366         netif_err(efx, drv, efx->net_dev, "expected packet:\n");
0367         print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
0368                    &state->payload, sizeof(state->payload), 0);
0369     }
0370 #endif
0371     atomic_inc(&state->rx_bad);
0372 }
0373 
0374 /* Initialise an ef4_selftest_state for a new iteration */
0375 static void ef4_iterate_state(struct ef4_nic *efx)
0376 {
0377     struct ef4_loopback_state *state = efx->loopback_selftest;
0378     struct net_device *net_dev = efx->net_dev;
0379     struct ef4_loopback_payload *payload = &state->payload;
0380 
0381     /* Initialise the layerII header */
0382     ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
0383     ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
0384     payload->header.h_proto = htons(ETH_P_IP);
0385 
0386     /* saddr set later and used as incrementing count */
0387     payload->ip.daddr = htonl(INADDR_LOOPBACK);
0388     payload->ip.ihl = 5;
0389     payload->ip.check = (__force __sum16) htons(0xdead);
0390     payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
0391     payload->ip.version = IPVERSION;
0392     payload->ip.protocol = IPPROTO_UDP;
0393 
0394     /* Initialise udp header */
0395     payload->udp.source = 0;
0396     payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
0397                  sizeof(struct iphdr));
0398     payload->udp.check = 0; /* checksum ignored */
0399 
0400     /* Fill out payload */
0401     payload->iteration = htons(ntohs(payload->iteration) + 1);
0402     memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
0403 
0404     /* Fill out remaining state members */
0405     atomic_set(&state->rx_good, 0);
0406     atomic_set(&state->rx_bad, 0);
0407     smp_wmb();
0408 }
0409 
0410 static int ef4_begin_loopback(struct ef4_tx_queue *tx_queue)
0411 {
0412     struct ef4_nic *efx = tx_queue->efx;
0413     struct ef4_loopback_state *state = efx->loopback_selftest;
0414     struct ef4_loopback_payload *payload;
0415     struct sk_buff *skb;
0416     int i;
0417     netdev_tx_t rc;
0418 
0419     /* Transmit N copies of buffer */
0420     for (i = 0; i < state->packet_count; i++) {
0421         /* Allocate an skb, holding an extra reference for
0422          * transmit completion counting */
0423         skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
0424         if (!skb)
0425             return -ENOMEM;
0426         state->skbs[i] = skb;
0427         skb_get(skb);
0428 
0429         /* Copy the payload in, incrementing the source address to
0430          * exercise the rss vectors */
0431         payload = skb_put(skb, sizeof(state->payload));
0432         memcpy(payload, &state->payload, sizeof(state->payload));
0433         payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
0434 
0435         /* Ensure everything we've written is visible to the
0436          * interrupt handler. */
0437         smp_wmb();
0438 
0439         netif_tx_lock_bh(efx->net_dev);
0440         rc = ef4_enqueue_skb(tx_queue, skb);
0441         netif_tx_unlock_bh(efx->net_dev);
0442 
0443         if (rc != NETDEV_TX_OK) {
0444             netif_err(efx, drv, efx->net_dev,
0445                   "TX queue %d could not transmit packet %d of "
0446                   "%d in %s loopback test\n", tx_queue->queue,
0447                   i + 1, state->packet_count,
0448                   LOOPBACK_MODE(efx));
0449 
0450             /* Defer cleaning up the other skbs for the caller */
0451             kfree_skb(skb);
0452             return -EPIPE;
0453         }
0454     }
0455 
0456     return 0;
0457 }
0458 
0459 static int ef4_poll_loopback(struct ef4_nic *efx)
0460 {
0461     struct ef4_loopback_state *state = efx->loopback_selftest;
0462 
0463     return atomic_read(&state->rx_good) == state->packet_count;
0464 }
0465 
0466 static int ef4_end_loopback(struct ef4_tx_queue *tx_queue,
0467                 struct ef4_loopback_self_tests *lb_tests)
0468 {
0469     struct ef4_nic *efx = tx_queue->efx;
0470     struct ef4_loopback_state *state = efx->loopback_selftest;
0471     struct sk_buff *skb;
0472     int tx_done = 0, rx_good, rx_bad;
0473     int i, rc = 0;
0474 
0475     netif_tx_lock_bh(efx->net_dev);
0476 
0477     /* Count the number of tx completions, and decrement the refcnt. Any
0478      * skbs not already completed will be free'd when the queue is flushed */
0479     for (i = 0; i < state->packet_count; i++) {
0480         skb = state->skbs[i];
0481         if (skb && !skb_shared(skb))
0482             ++tx_done;
0483         dev_kfree_skb(skb);
0484     }
0485 
0486     netif_tx_unlock_bh(efx->net_dev);
0487 
0488     /* Check TX completion and received packet counts */
0489     rx_good = atomic_read(&state->rx_good);
0490     rx_bad = atomic_read(&state->rx_bad);
0491     if (tx_done != state->packet_count) {
0492         /* Don't free the skbs; they will be picked up on TX
0493          * overflow or channel teardown.
0494          */
0495         netif_err(efx, drv, efx->net_dev,
0496               "TX queue %d saw only %d out of an expected %d "
0497               "TX completion events in %s loopback test\n",
0498               tx_queue->queue, tx_done, state->packet_count,
0499               LOOPBACK_MODE(efx));
0500         rc = -ETIMEDOUT;
0501         /* Allow to fall through so we see the RX errors as well */
0502     }
0503 
0504     /* We may always be up to a flush away from our desired packet total */
0505     if (rx_good != state->packet_count) {
0506         netif_dbg(efx, drv, efx->net_dev,
0507               "TX queue %d saw only %d out of an expected %d "
0508               "received packets in %s loopback test\n",
0509               tx_queue->queue, rx_good, state->packet_count,
0510               LOOPBACK_MODE(efx));
0511         rc = -ETIMEDOUT;
0512         /* Fall through */
0513     }
0514 
0515     /* Update loopback test structure */
0516     lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
0517     lb_tests->tx_done[tx_queue->queue] += tx_done;
0518     lb_tests->rx_good += rx_good;
0519     lb_tests->rx_bad += rx_bad;
0520 
0521     return rc;
0522 }
0523 
0524 static int
0525 ef4_test_loopback(struct ef4_tx_queue *tx_queue,
0526           struct ef4_loopback_self_tests *lb_tests)
0527 {
0528     struct ef4_nic *efx = tx_queue->efx;
0529     struct ef4_loopback_state *state = efx->loopback_selftest;
0530     int i, begin_rc, end_rc;
0531 
0532     for (i = 0; i < 3; i++) {
0533         /* Determine how many packets to send */
0534         state->packet_count = efx->txq_entries / 3;
0535         state->packet_count = min(1 << (i << 2), state->packet_count);
0536         state->skbs = kcalloc(state->packet_count,
0537                       sizeof(state->skbs[0]), GFP_KERNEL);
0538         if (!state->skbs)
0539             return -ENOMEM;
0540         state->flush = false;
0541 
0542         netif_dbg(efx, drv, efx->net_dev,
0543               "TX queue %d testing %s loopback with %d packets\n",
0544               tx_queue->queue, LOOPBACK_MODE(efx),
0545               state->packet_count);
0546 
0547         ef4_iterate_state(efx);
0548         begin_rc = ef4_begin_loopback(tx_queue);
0549 
0550         /* This will normally complete very quickly, but be
0551          * prepared to wait much longer. */
0552         msleep(1);
0553         if (!ef4_poll_loopback(efx)) {
0554             msleep(LOOPBACK_TIMEOUT_MS);
0555             ef4_poll_loopback(efx);
0556         }
0557 
0558         end_rc = ef4_end_loopback(tx_queue, lb_tests);
0559         kfree(state->skbs);
0560 
0561         if (begin_rc || end_rc) {
0562             /* Wait a while to ensure there are no packets
0563              * floating around after a failure. */
0564             schedule_timeout_uninterruptible(HZ / 10);
0565             return begin_rc ? begin_rc : end_rc;
0566         }
0567     }
0568 
0569     netif_dbg(efx, drv, efx->net_dev,
0570           "TX queue %d passed %s loopback test with a burst length "
0571           "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
0572           state->packet_count);
0573 
0574     return 0;
0575 }
0576 
0577 /* Wait for link up. On Falcon, we would prefer to rely on ef4_monitor, but
0578  * any contention on the mac lock (via e.g. ef4_mac_mcast_work) causes it
0579  * to delay and retry. Therefore, it's safer to just poll directly. Wait
0580  * for link up and any faults to dissipate. */
0581 static int ef4_wait_for_link(struct ef4_nic *efx)
0582 {
0583     struct ef4_link_state *link_state = &efx->link_state;
0584     int count, link_up_count = 0;
0585     bool link_up;
0586 
0587     for (count = 0; count < 40; count++) {
0588         schedule_timeout_uninterruptible(HZ / 10);
0589 
0590         if (efx->type->monitor != NULL) {
0591             mutex_lock(&efx->mac_lock);
0592             efx->type->monitor(efx);
0593             mutex_unlock(&efx->mac_lock);
0594         }
0595 
0596         mutex_lock(&efx->mac_lock);
0597         link_up = link_state->up;
0598         if (link_up)
0599             link_up = !efx->type->check_mac_fault(efx);
0600         mutex_unlock(&efx->mac_lock);
0601 
0602         if (link_up) {
0603             if (++link_up_count == 2)
0604                 return 0;
0605         } else {
0606             link_up_count = 0;
0607         }
0608     }
0609 
0610     return -ETIMEDOUT;
0611 }
0612 
0613 static int ef4_test_loopbacks(struct ef4_nic *efx, struct ef4_self_tests *tests,
0614                   unsigned int loopback_modes)
0615 {
0616     enum ef4_loopback_mode mode;
0617     struct ef4_loopback_state *state;
0618     struct ef4_channel *channel =
0619         ef4_get_channel(efx, efx->tx_channel_offset);
0620     struct ef4_tx_queue *tx_queue;
0621     int rc = 0;
0622 
0623     /* Set the port loopback_selftest member. From this point on
0624      * all received packets will be dropped. Mark the state as
0625      * "flushing" so all inflight packets are dropped */
0626     state = kzalloc(sizeof(*state), GFP_KERNEL);
0627     if (state == NULL)
0628         return -ENOMEM;
0629     BUG_ON(efx->loopback_selftest);
0630     state->flush = true;
0631     efx->loopback_selftest = state;
0632 
0633     /* Test all supported loopback modes */
0634     for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
0635         if (!(loopback_modes & (1 << mode)))
0636             continue;
0637 
0638         /* Move the port into the specified loopback mode. */
0639         state->flush = true;
0640         mutex_lock(&efx->mac_lock);
0641         efx->loopback_mode = mode;
0642         rc = __ef4_reconfigure_port(efx);
0643         mutex_unlock(&efx->mac_lock);
0644         if (rc) {
0645             netif_err(efx, drv, efx->net_dev,
0646                   "unable to move into %s loopback\n",
0647                   LOOPBACK_MODE(efx));
0648             goto out;
0649         }
0650 
0651         rc = ef4_wait_for_link(efx);
0652         if (rc) {
0653             netif_err(efx, drv, efx->net_dev,
0654                   "loopback %s never came up\n",
0655                   LOOPBACK_MODE(efx));
0656             goto out;
0657         }
0658 
0659         /* Test all enabled types of TX queue */
0660         ef4_for_each_channel_tx_queue(tx_queue, channel) {
0661             state->offload_csum = (tx_queue->queue &
0662                            EF4_TXQ_TYPE_OFFLOAD);
0663             rc = ef4_test_loopback(tx_queue,
0664                            &tests->loopback[mode]);
0665             if (rc)
0666                 goto out;
0667         }
0668     }
0669 
0670  out:
0671     /* Remove the flush. The caller will remove the loopback setting */
0672     state->flush = true;
0673     efx->loopback_selftest = NULL;
0674     wmb();
0675     kfree(state);
0676 
0677     if (rc == -EPERM)
0678         rc = 0;
0679 
0680     return rc;
0681 }
0682 
0683 /**************************************************************************
0684  *
0685  * Entry point
0686  *
0687  *************************************************************************/
0688 
0689 int ef4_selftest(struct ef4_nic *efx, struct ef4_self_tests *tests,
0690          unsigned flags)
0691 {
0692     enum ef4_loopback_mode loopback_mode = efx->loopback_mode;
0693     int phy_mode = efx->phy_mode;
0694     int rc_test = 0, rc_reset, rc;
0695 
0696     ef4_selftest_async_cancel(efx);
0697 
0698     /* Online (i.e. non-disruptive) testing
0699      * This checks interrupt generation, event delivery and PHY presence. */
0700 
0701     rc = ef4_test_phy_alive(efx, tests);
0702     if (rc && !rc_test)
0703         rc_test = rc;
0704 
0705     rc = ef4_test_nvram(efx, tests);
0706     if (rc && !rc_test)
0707         rc_test = rc;
0708 
0709     rc = ef4_test_interrupts(efx, tests);
0710     if (rc && !rc_test)
0711         rc_test = rc;
0712 
0713     rc = ef4_test_eventq_irq(efx, tests);
0714     if (rc && !rc_test)
0715         rc_test = rc;
0716 
0717     if (rc_test)
0718         return rc_test;
0719 
0720     if (!(flags & ETH_TEST_FL_OFFLINE))
0721         return ef4_test_phy(efx, tests, flags);
0722 
0723     /* Offline (i.e. disruptive) testing
0724      * This checks MAC and PHY loopback on the specified port. */
0725 
0726     /* Detach the device so the kernel doesn't transmit during the
0727      * loopback test and the watchdog timeout doesn't fire.
0728      */
0729     ef4_device_detach_sync(efx);
0730 
0731     if (efx->type->test_chip) {
0732         rc_reset = efx->type->test_chip(efx, tests);
0733         if (rc_reset) {
0734             netif_err(efx, hw, efx->net_dev,
0735                   "Unable to recover from chip test\n");
0736             ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
0737             return rc_reset;
0738         }
0739 
0740         if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
0741             rc_test = -EIO;
0742     }
0743 
0744     /* Ensure that the phy is powered and out of loopback
0745      * for the bist and loopback tests */
0746     mutex_lock(&efx->mac_lock);
0747     efx->phy_mode &= ~PHY_MODE_LOW_POWER;
0748     efx->loopback_mode = LOOPBACK_NONE;
0749     __ef4_reconfigure_port(efx);
0750     mutex_unlock(&efx->mac_lock);
0751 
0752     rc = ef4_test_phy(efx, tests, flags);
0753     if (rc && !rc_test)
0754         rc_test = rc;
0755 
0756     rc = ef4_test_loopbacks(efx, tests, efx->loopback_modes);
0757     if (rc && !rc_test)
0758         rc_test = rc;
0759 
0760     /* restore the PHY to the previous state */
0761     mutex_lock(&efx->mac_lock);
0762     efx->phy_mode = phy_mode;
0763     efx->loopback_mode = loopback_mode;
0764     __ef4_reconfigure_port(efx);
0765     mutex_unlock(&efx->mac_lock);
0766 
0767     netif_device_attach(efx->net_dev);
0768 
0769     return rc_test;
0770 }
0771 
0772 void ef4_selftest_async_start(struct ef4_nic *efx)
0773 {
0774     struct ef4_channel *channel;
0775 
0776     ef4_for_each_channel(channel, efx)
0777         ef4_nic_event_test_start(channel);
0778     schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
0779 }
0780 
0781 void ef4_selftest_async_cancel(struct ef4_nic *efx)
0782 {
0783     cancel_delayed_work_sync(&efx->selftest_work);
0784 }
0785 
0786 void ef4_selftest_async_work(struct work_struct *data)
0787 {
0788     struct ef4_nic *efx = container_of(data, struct ef4_nic,
0789                        selftest_work.work);
0790     struct ef4_channel *channel;
0791     int cpu;
0792 
0793     ef4_for_each_channel(channel, efx) {
0794         cpu = ef4_nic_event_test_irq_cpu(channel);
0795         if (cpu < 0)
0796             netif_err(efx, ifup, efx->net_dev,
0797                   "channel %d failed to trigger an interrupt\n",
0798                   channel->channel);
0799         else
0800             netif_dbg(efx, ifup, efx->net_dev,
0801                   "channel %d triggered interrupt on CPU %d\n",
0802                   channel->channel, cpu);
0803     }
0804 }