sfc: Replace PHY MDIO test with an 'alive' test
[safe/jmp/linux-2.6] / drivers / net / sfc / selftest.c
index 53fa4ed..8a5a7b6 100644 (file)
@@ -1,7 +1,7 @@
 /****************************************************************************
  * Driver for Solarflare Solarstorm network controllers and boards
  * Copyright 2005-2006 Fen Systems Ltd.
- * Copyright 2006-2008 Solarflare Communications Inc.
+ * Copyright 2006-2009 Solarflare Communications Inc.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 as published
 #include <linux/rtnetlink.h>
 #include <asm/io.h>
 #include "net_driver.h"
-#include "ethtool.h"
 #include "efx.h"
-#include "falcon.h"
+#include "nic.h"
 #include "selftest.h"
-#include "boards.h"
 #include "workarounds.h"
-#include "mac.h"
+#include "spi.h"
+#include "io.h"
 
 /*
  * Loopback test packet structure
@@ -47,26 +46,24 @@ static const unsigned char payload_source[ETH_ALEN] = {
        0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
 };
 
-static const char *payload_msg =
+static const char payload_msg[] =
        "Hello world! This is an Efx loopback test in progress!";
 
 /**
- * efx_selftest_state - persistent state during a selftest
+ * efx_loopback_state - persistent state during a loopback selftest
  * @flush:             Drop all packets in efx_loopback_rx_packet
  * @packet_count:      Number of packets being used in this test
  * @skbs:              An array of skbs transmitted
+ * @offload_csum:      Checksums are being offloaded
  * @rx_good:           RX good packet count
  * @rx_bad:            RX bad packet count
  * @payload:           Payload used in tests
  */
-struct efx_selftest_state {
-       int flush;
+struct efx_loopback_state {
+       bool flush;
        int packet_count;
        struct sk_buff **skbs;
-
-       /* Checksums are being offloaded */
-       int offload_csum;
-
+       bool offload_csum;
        atomic_t rx_good;
        atomic_t rx_bad;
        struct efx_loopback_payload payload;
@@ -74,21 +71,46 @@ struct efx_selftest_state {
 
 /**************************************************************************
  *
- * Configurable values
+ * MII, NVRAM and register tests
  *
  **************************************************************************/
 
-/* Level of loopback testing
- *
- * The maximum packet burst length is 16**(n-1), i.e.
- *
- * - Level 0 : no packets
- * - Level 1 : 1 packet
- * - Level 2 : 17 packets (1 * 1 packet, 1 * 16 packets)
- * - Level 3 : 273 packets (1 * 1 packet, 1 * 16 packet, 1 * 256 packets)
- *
- */
-static unsigned int loopback_test_level = 3;
+static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc = 0;
+
+       if (efx->phy_op->test_alive) {
+               rc = efx->phy_op->test_alive(efx);
+               tests->phy_alive = rc ? -1 : 1;
+       }
+
+       return rc;
+}
+
+static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc = 0;
+
+       if (efx->type->test_nvram) {
+               rc = efx->type->test_nvram(efx);
+               tests->nvram = rc ? -1 : 1;
+       }
+
+       return rc;
+}
+
+static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc = 0;
+
+       /* Test register access */
+       if (efx->type->test_registers) {
+               rc = efx->type->test_registers(efx);
+               tests->registers = rc ? -1 : 1;
+       }
+
+       return rc;
+}
 
 /**************************************************************************
  *
@@ -111,14 +133,14 @@ static int efx_test_interrupts(struct efx_nic *efx,
 
        /* ACK each interrupting event queue. Receiving an interrupt due to
         * traffic before a test event is raised is considered a pass */
-       efx_for_each_channel_with_interrupt(channel, efx) {
+       efx_for_each_channel(channel, efx) {
                if (channel->work_pending)
                        efx_process_channel_now(channel);
                if (efx->last_irq_cpu >= 0)
                        goto success;
        }
 
-       falcon_generate_interrupt(efx);
+       efx_nic_generate_interrupt(efx);
 
        /* Wait for arrival of test interrupt. */
        EFX_LOG(efx, "waiting for test interrupt\n");
@@ -130,47 +152,12 @@ static int efx_test_interrupts(struct efx_nic *efx,
        return -ETIMEDOUT;
 
  success:
-       EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
-               efx->interrupt_mode, efx->last_irq_cpu);
+       EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
+               efx->last_irq_cpu);
        tests->interrupt = 1;
        return 0;
 }
 
-/* Test generation and receipt of non-interrupting events */
-static int efx_test_eventq(struct efx_channel *channel,
-                          struct efx_self_tests *tests)
-{
-       unsigned int magic;
-
-       /* Channel specific code, limited to 20 bits */
-       magic = (0x00010150 + channel->channel);
-       EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
-               channel->channel, magic);
-
-       tests->eventq_dma[channel->channel] = -1;
-       tests->eventq_int[channel->channel] = 1;        /* fake pass */
-       tests->eventq_poll[channel->channel] = 1;       /* fake pass */
-
-       /* Reset flag and zero magic word */
-       channel->efx->last_irq_cpu = -1;
-       channel->eventq_magic = 0;
-       smp_wmb();
-
-       falcon_generate_test_event(channel, magic);
-       udelay(1);
-
-       efx_process_channel_now(channel);
-       if (channel->eventq_magic != magic) {
-               EFX_ERR(channel->efx, "channel %d  failed to see test event\n",
-                       channel->channel);
-               return -ETIMEDOUT;
-       } else {
-               tests->eventq_dma[channel->channel] = 1;
-       }
-
-       return 0;
-}
-
 /* Test generation and receipt of interrupting events */
 static int efx_test_eventq_irq(struct efx_channel *channel,
                               struct efx_self_tests *tests)
@@ -191,7 +178,7 @@ static int efx_test_eventq_irq(struct efx_channel *channel,
        channel->eventq_magic = 0;
        smp_wmb();
 
-       falcon_generate_test_event(channel, magic);
+       efx_nic_generate_test_event(channel, magic);
 
        /* Wait for arrival of interrupt */
        count = 0;
@@ -234,39 +221,18 @@ static int efx_test_eventq_irq(struct efx_channel *channel,
        return 0;
 }
 
-/**************************************************************************
- *
- * PHY testing
- *
- **************************************************************************/
-
-/* Check PHY presence by reading the PHY ID registers */
-static int efx_test_phy(struct efx_nic *efx,
-                       struct efx_self_tests *tests)
+static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
+                       unsigned flags)
 {
-       u16 physid1, physid2;
-       struct mii_if_info *mii = &efx->mii;
-       struct net_device *net_dev = efx->net_dev;
-
-       if (efx->phy_type == PHY_TYPE_NONE)
-               return 0;
-
-       EFX_LOG(efx, "testing PHY presence\n");
-       tests->phy_ok = -1;
-
-       physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
-       physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
+       int rc;
 
-       if ((physid1 != 0x0000) && (physid1 != 0xffff) &&
-           (physid2 != 0x0000) && (physid2 != 0xffff)) {
-               EFX_LOG(efx, "found MII PHY %d ID 0x%x:%x\n",
-                       mii->phy_id, physid1, physid2);
-               tests->phy_ok = 1;
+       if (!efx->phy_op->run_tests)
                return 0;
-       }
 
-       EFX_ERR(efx, "no MII PHY present with ID %d\n", mii->phy_id);
-       return -ENODEV;
+       mutex_lock(&efx->mac_lock);
+       rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
+       mutex_unlock(&efx->mac_lock);
+       return rc;
 }
 
 /**************************************************************************
@@ -282,7 +248,7 @@ static int efx_test_phy(struct efx_nic *efx,
 void efx_loopback_rx_packet(struct efx_nic *efx,
                            const char *buf_ptr, int pkt_len)
 {
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        struct efx_loopback_payload *received;
        struct efx_loopback_payload *payload;
 
@@ -293,7 +259,7 @@ void efx_loopback_rx_packet(struct efx_nic *efx,
                return;
 
        payload = &state->payload;
-       
+
        received = (struct efx_loopback_payload *) buf_ptr;
        received->ip.saddr = payload->ip.saddr;
        if (state->offload_csum)
@@ -367,7 +333,7 @@ void efx_loopback_rx_packet(struct efx_nic *efx,
 /* Initialise an efx_selftest_state for a new iteration */
 static void efx_iterate_state(struct efx_nic *efx)
 {
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        struct net_device *net_dev = efx->net_dev;
        struct efx_loopback_payload *payload = &state->payload;
 
@@ -403,14 +369,15 @@ static void efx_iterate_state(struct efx_nic *efx)
 static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
 {
        struct efx_nic *efx = tx_queue->efx;
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        struct efx_loopback_payload *payload;
        struct sk_buff *skb;
-       int i, rc;
+       int i;
+       netdev_tx_t rc;
 
        /* Transmit N copies of buffer */
        for (i = 0; i < state->packet_count; i++) {
-               /* Allocate an skb, holding an extra reference for 
+               /* Allocate an skb, holding an extra reference for
                 * transmit completion counting */
                skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
                if (!skb)
@@ -431,7 +398,7 @@ static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
 
                if (efx_dev_registered(efx))
                        netif_tx_lock_bh(efx->net_dev);
-               rc = efx_xmit(efx, tx_queue, skb);
+               rc = efx_enqueue_skb(tx_queue, skb);
                if (efx_dev_registered(efx))
                        netif_tx_unlock_bh(efx->net_dev);
 
@@ -451,12 +418,12 @@ static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
 
 static int efx_poll_loopback(struct efx_nic *efx)
 {
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        struct efx_channel *channel;
 
        /* NAPI polling is not enabled, so process channels
         * synchronously */
-       efx_for_each_channel_with_interrupt(channel, efx) {
+       efx_for_each_channel(channel, efx) {
                if (channel->work_pending)
                        efx_process_channel_now(channel);
        }
@@ -467,7 +434,7 @@ static int efx_end_loopback(struct efx_tx_queue *tx_queue,
                            struct efx_loopback_self_tests *lb_tests)
 {
        struct efx_nic *efx = tx_queue->efx;
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        struct sk_buff *skb;
        int tx_done = 0, rx_good, rx_bad;
        int i, rc = 0;
@@ -526,18 +493,18 @@ efx_test_loopback(struct efx_tx_queue *tx_queue,
                  struct efx_loopback_self_tests *lb_tests)
 {
        struct efx_nic *efx = tx_queue->efx;
-       struct efx_selftest_state *state = efx->loopback_selftest;
+       struct efx_loopback_state *state = efx->loopback_selftest;
        int i, begin_rc, end_rc;
 
-       for (i = 0; i < loopback_test_level; i++) {
+       for (i = 0; i < 3; i++) {
                /* Determine how many packets to send */
-               state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
+               state->packet_count = EFX_TXQ_SIZE / 3;
                state->packet_count = min(1 << (i << 2), state->packet_count);
                state->skbs = kzalloc(sizeof(state->skbs[0]) *
                                      state->packet_count, GFP_KERNEL);
                if (!state->skbs)
                        return -ENOMEM;
-               state->flush = 0;
+               state->flush = false;
 
                EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
                        "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
@@ -572,85 +539,84 @@ efx_test_loopback(struct efx_tx_queue *tx_queue,
        return 0;
 }
 
-static int efx_test_loopbacks(struct efx_nic *efx,
-                             struct efx_self_tests *tests,
-                             unsigned int loopback_modes)
+/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
+ * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
+ * to delay and retry. Therefore, it's safer to just poll directly. Wait
+ * for link up and any faults to dissipate. */
+static int efx_wait_for_link(struct efx_nic *efx)
 {
-       struct efx_selftest_state *state = efx->loopback_selftest;
-       struct ethtool_cmd ecmd, ecmd_loopback;
-       struct efx_tx_queue *tx_queue;
-       enum efx_loopback_mode old_mode, mode;
-       int count, rc, link_up;
+       struct efx_link_state *link_state = &efx->link_state;
+       int count;
+       bool link_up;
+
+       for (count = 0; count < 40; count++) {
+               schedule_timeout_uninterruptible(HZ / 10);
+
+               if (efx->type->monitor != NULL) {
+                       mutex_lock(&efx->mac_lock);
+                       efx->type->monitor(efx);
+                       mutex_unlock(&efx->mac_lock);
+               } else {
+                       struct efx_channel *channel = &efx->channel[0];
+                       if (channel->work_pending)
+                               efx_process_channel_now(channel);
+               }
 
-       rc = efx_ethtool_get_settings(efx->net_dev, &ecmd);
-       if (rc) {
-               EFX_ERR(efx, "could not get GMII settings\n");
-               return rc;
-       }
-       old_mode = efx->loopback_mode;
-
-       /* Disable autonegotiation for the purposes of loopback */
-       memcpy(&ecmd_loopback, &ecmd, sizeof(ecmd_loopback));
-       if (ecmd_loopback.autoneg == AUTONEG_ENABLE) {
-               ecmd_loopback.autoneg = AUTONEG_DISABLE;
-               ecmd_loopback.duplex = DUPLEX_FULL;
-               ecmd_loopback.speed = SPEED_10000;
-       }
+               mutex_lock(&efx->mac_lock);
+               link_up = link_state->up;
+               if (link_up)
+                       link_up = !efx->mac_op->check_fault(efx);
+               mutex_unlock(&efx->mac_lock);
 
-       rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback);
-       if (rc) {
-               EFX_ERR(efx, "could not disable autonegotiation\n");
-               goto out;
+               if (link_up)
+                       return 0;
        }
-       tests->loopback_speed = ecmd_loopback.speed;
-       tests->loopback_full_duplex = ecmd_loopback.duplex;
+
+       return -ETIMEDOUT;
+}
+
+static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
+                             unsigned int loopback_modes)
+{
+       enum efx_loopback_mode mode;
+       struct efx_loopback_state *state;
+       struct efx_tx_queue *tx_queue;
+       int rc = 0;
+
+       /* Set the port loopback_selftest member. From this point on
+        * all received packets will be dropped. Mark the state as
+        * "flushing" so all inflight packets are dropped */
+       state = kzalloc(sizeof(*state), GFP_KERNEL);
+       if (state == NULL)
+               return -ENOMEM;
+       BUG_ON(efx->loopback_selftest);
+       state->flush = true;
+       efx->loopback_selftest = state;
 
        /* Test all supported loopback modes */
-       for (mode = LOOPBACK_NONE; mode < LOOPBACK_TEST_MAX; mode++) {
+       for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
                if (!(loopback_modes & (1 << mode)))
                        continue;
 
                /* Move the port into the specified loopback mode. */
-               state->flush = 1;
+               state->flush = true;
+               mutex_lock(&efx->mac_lock);
                efx->loopback_mode = mode;
-               efx_reconfigure_port(efx);
-
-               /* Wait for the PHY to signal the link is up */
-               count = 0;
-               do {
-                       struct efx_channel *channel = &efx->channel[0];
+               rc = __efx_reconfigure_port(efx);
+               mutex_unlock(&efx->mac_lock);
+               if (rc) {
+                       EFX_ERR(efx, "unable to move into %s loopback\n",
+                               LOOPBACK_MODE(efx));
+                       goto out;
+               }
 
-                       falcon_check_xmac(efx);
-                       schedule_timeout_uninterruptible(HZ / 10);
-                       if (channel->work_pending)
-                               efx_process_channel_now(channel);
-                       /* Wait for PHY events to be processed */
-                       flush_workqueue(efx->workqueue);
-                       rmb();
-
-                       /* efx->link_up can be 1 even if the XAUI link is down,
-                        * (bug5762). Usually, it's not worth bothering with the
-                        * difference, but for selftests, we need that extra
-                        * guarantee that the link is really, really, up.
-                        */
-                       link_up = efx->link_up;
-                       if (!falcon_xaui_link_ok(efx))
-                               link_up = 0;
-
-               } while ((++count < 20) && !link_up);
-
-               /* The link should now be up. If it isn't, there is no point
-                * in attempting a loopback test */
-               if (!link_up) {
+               rc = efx_wait_for_link(efx);
+               if (rc) {
                        EFX_ERR(efx, "loopback %s never came up\n",
                                LOOPBACK_MODE(efx));
-                       rc = -EIO;
                        goto out;
                }
 
-               EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
-                       LOOPBACK_MODE(efx), count);
-
                /* Test every TX queue */
                efx_for_each_tx_queue(tx_queue, efx) {
                        state->offload_csum = (tx_queue->queue ==
@@ -663,68 +629,119 @@ static int efx_test_loopbacks(struct efx_nic *efx,
        }
 
  out:
-       /* Take out of loopback and restore PHY settings */
-       state->flush = 1;
-       efx->loopback_mode = old_mode;
-       efx_ethtool_set_settings(efx->net_dev, &ecmd);
+       /* Remove the flush. The caller will remove the loopback setting */
+       state->flush = true;
+       efx->loopback_selftest = NULL;
+       wmb();
+       kfree(state);
 
        return rc;
 }
 
 /**************************************************************************
  *
- * Entry points
+ * Entry point
  *
  *************************************************************************/
 
-/* Online (i.e. non-disruptive) testing
- * This checks interrupt generation, event delivery and PHY presence. */
-int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
+int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
+                unsigned flags)
 {
+       enum efx_loopback_mode loopback_mode = efx->loopback_mode;
+       int phy_mode = efx->phy_mode;
+       enum reset_type reset_method = RESET_TYPE_INVISIBLE;
        struct efx_channel *channel;
-       int rc;
+       int rc_test = 0, rc_reset = 0, rc;
+
+       /* Online (i.e. non-disruptive) testing
+        * This checks interrupt generation, event delivery and PHY presence. */
+
+       rc = efx_test_phy_alive(efx, tests);
+       if (rc && !rc_test)
+               rc_test = rc;
+
+       rc = efx_test_nvram(efx, tests);
+       if (rc && !rc_test)
+               rc_test = rc;
 
        rc = efx_test_interrupts(efx, tests);
-       if (rc)
-               return rc;
+       if (rc && !rc_test)
+               rc_test = rc;
+
        efx_for_each_channel(channel, efx) {
-               if (channel->has_interrupt)
-                       rc = efx_test_eventq_irq(channel, tests);
+               rc = efx_test_eventq_irq(channel, tests);
+               if (rc && !rc_test)
+                       rc_test = rc;
+       }
+
+       if (rc_test)
+               return rc_test;
+
+       if (!(flags & ETH_TEST_FL_OFFLINE))
+               return efx_test_phy(efx, tests, flags);
+
+       /* Offline (i.e. disruptive) testing
+        * This checks MAC and PHY loopback on the specified port. */
+
+       /* force the carrier state off so the kernel doesn't transmit during
+        * the loopback test, and the watchdog timeout doesn't fire. Also put
+        * falcon into loopback for the register test.
+        */
+       mutex_lock(&efx->mac_lock);
+       efx->port_inhibited = true;
+       if (efx->loopback_modes) {
+               /* We need the 312 clock from the PHY to test the XMAC
+                * registers, so move into XGMII loopback if available */
+               if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
+                       efx->loopback_mode = LOOPBACK_XGMII;
                else
-                       rc = efx_test_eventq(channel, tests);
-               if (rc)
-                       return rc;
+                       efx->loopback_mode = __ffs(efx->loopback_modes);
        }
-       rc = efx_test_phy(efx, tests);
-       return rc;
-}
 
-/* Offline (i.e. disruptive) testing
- * This checks MAC and PHY loopback on the specified port. */
-int efx_offline_test(struct efx_nic *efx,
-                    struct efx_self_tests *tests, unsigned int loopback_modes)
-{
-       struct efx_selftest_state *state;
-       int rc;
+       __efx_reconfigure_port(efx);
+       mutex_unlock(&efx->mac_lock);
 
-       /* Create a selftest_state structure to hold state for the test */
-       state = kzalloc(sizeof(*state), GFP_KERNEL);
-       if (state == NULL)
-               return -ENOMEM;
+       /* free up all consumers of SRAM (including all the queues) */
+       efx_reset_down(efx, reset_method);
 
-       /* Set the port loopback_selftest member. From this point on
-        * all received packets will be dropped. Mark the state as
-        * "flushing" so all inflight packets are dropped */
-       BUG_ON(efx->loopback_selftest);
-       state->flush = 1;
-       efx->loopback_selftest = state;
+       rc = efx_test_chip(efx, tests);
+       if (rc && !rc_test)
+               rc_test = rc;
 
-       rc = efx_test_loopbacks(efx, tests, loopback_modes);
+       /* reset the chip to recover from the register test */
+       rc_reset = efx->type->reset(efx, reset_method);
 
-       efx->loopback_selftest = NULL;
-       wmb();
-       kfree(state);
+       /* Ensure that the phy is powered and out of loopback
+        * for the bist and loopback tests */
+       efx->phy_mode &= ~PHY_MODE_LOW_POWER;
+       efx->loopback_mode = LOOPBACK_NONE;
 
-       return rc;
+       rc = efx_reset_up(efx, reset_method, rc_reset == 0);
+       if (rc && !rc_reset)
+               rc_reset = rc;
+
+       if (rc_reset) {
+               EFX_ERR(efx, "Unable to recover from chip test\n");
+               efx_schedule_reset(efx, RESET_TYPE_DISABLE);
+               return rc_reset;
+       }
+
+       rc = efx_test_phy(efx, tests, flags);
+       if (rc && !rc_test)
+               rc_test = rc;
+
+       rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
+       if (rc && !rc_test)
+               rc_test = rc;
+
+       /* restore the PHY to the previous state */
+       mutex_lock(&efx->mac_lock);
+       efx->phy_mode = phy_mode;
+       efx->port_inhibited = false;
+       efx->loopback_mode = loopback_mode;
+       __efx_reconfigure_port(efx);
+       mutex_unlock(&efx->mac_lock);
+
+       return rc_test;
 }