sfc: Add support for SFN4111T
[firefly-linux-kernel-4.4.55.git] / drivers / net / sfc / selftest.c
index 8ad517c619468f2df034b20ba517f456ee978851..6bb09f263b333b3ee88dc7a444fecd3a3eb72255 100644 (file)
@@ -26,7 +26,9 @@
 #include "selftest.h"
 #include "boards.h"
 #include "workarounds.h"
-#include "mac.h"
+#include "spi.h"
+#include "falcon_io.h"
+#include "mdio_10g.h"
 
 /*
  * Loopback test packet structure
@@ -51,7 +53,7 @@ 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
@@ -59,13 +61,13 @@ static const char *payload_msg =
  * @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;
@@ -74,21 +76,67 @@ 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_mii(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc = 0;
+       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;
+
+       mutex_lock(&efx->mac_lock);
+       tests->mii = -1;
+
+       physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
+       physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
+
+       if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
+           (physid2 == 0x0000) || (physid2 == 0xffff)) {
+               EFX_ERR(efx, "no MII PHY present with ID %d\n",
+                       mii->phy_id);
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (EFX_IS10G(efx)) {
+               rc = mdio_clause45_check_mmds(efx, efx->phy_op->mmds, 0);
+               if (rc)
+                       goto out;
+       }
+
+out:
+       mutex_unlock(&efx->mac_lock);
+       tests->mii = rc ? -1 : 1;
+       return rc;
+}
+
+static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc;
+
+       rc = falcon_read_nvram(efx, NULL);
+       tests->nvram = rc ? -1 : 1;
+       return rc;
+}
+
+static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc;
+
+       /* Not supported on A-series silicon */
+       if (falcon_rev(efx) < FALCON_REV_B0)
+               return 0;
+
+       rc = falcon_test_registers(efx);
+       tests->registers = rc ? -1 : 1;
+       return rc;
+}
 
 /**************************************************************************
  *
@@ -111,7 +159,7 @@ 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)
@@ -136,41 +184,6 @@ static int efx_test_interrupts(struct efx_nic *efx,
        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)
@@ -234,39 +247,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)
 {
-       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->test)
                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->test(efx);
+       mutex_unlock(&efx->mac_lock);
+       tests->phy = rc ? -1 : 1;
+       return rc;
 }
 
 /**************************************************************************
@@ -282,7 +274,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 +285,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 +359,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;
 
@@ -400,17 +392,17 @@ static void efx_iterate_state(struct efx_nic *efx)
        smp_wmb();
 }
 
-static int efx_tx_loopback(struct efx_tx_queue *tx_queue)
+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;
 
        /* 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)
@@ -449,11 +441,25 @@ static int efx_tx_loopback(struct efx_tx_queue *tx_queue)
        return 0;
 }
 
-static int efx_rx_loopback(struct efx_tx_queue *tx_queue,
-                          struct efx_loopback_self_tests *lb_tests)
+static int efx_poll_loopback(struct efx_nic *efx)
+{
+       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(channel, efx) {
+               if (channel->work_pending)
+                       efx_process_channel_now(channel);
+       }
+       return atomic_read(&state->rx_good) == state->packet_count;
+}
+
+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;
@@ -512,11 +518,10 @@ 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_channel *channel;
-       int i, tx_rc, rx_rc;
+       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 = min(1 << (i << 2), state->packet_count);
@@ -524,30 +529,31 @@ efx_test_loopback(struct efx_tx_queue *tx_queue,
                                      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),
                        state->packet_count);
 
                efx_iterate_state(efx);
-               tx_rc = efx_tx_loopback(tx_queue);
-               
-               /* NAPI polling is not enabled, so process channels synchronously */
-               schedule_timeout_uninterruptible(HZ / 50);
-               efx_for_each_channel_with_interrupt(channel, efx) {
-                       if (channel->work_pending)
-                               efx_process_channel_now(channel);
+               begin_rc = efx_begin_loopback(tx_queue);
+
+               /* This will normally complete very quickly, but be
+                * prepared to wait up to 100 ms. */
+               msleep(1);
+               if (!efx_poll_loopback(efx)) {
+                       msleep(100);
+                       efx_poll_loopback(efx);
                }
 
-               rx_rc = efx_rx_loopback(tx_queue, lb_tests);
+               end_rc = efx_end_loopback(tx_queue, lb_tests);
                kfree(state->skbs);
 
-               if (tx_rc || rx_rc) {
+               if (begin_rc || end_rc) {
                        /* Wait a while to ensure there are no packets
                         * floating around after a failure. */
                        schedule_timeout_uninterruptible(HZ / 10);
-                       return tx_rc ? tx_rc : rx_rc;
+                       return begin_rc ? begin_rc : end_rc;
                }
        }
 
@@ -558,55 +564,44 @@ efx_test_loopback(struct efx_tx_queue *tx_queue,
        return 0;
 }
 
-static int efx_test_loopbacks(struct efx_nic *efx,
+static int efx_test_loopbacks(struct efx_nic *efx, struct ethtool_cmd ecmd,
                              struct efx_self_tests *tests,
                              unsigned int loopback_modes)
 {
-       struct efx_selftest_state *state = efx->loopback_selftest;
-       struct ethtool_cmd ecmd, ecmd_loopback;
+       enum efx_loopback_mode mode;
+       struct efx_loopback_state *state;
        struct efx_tx_queue *tx_queue;
-       enum efx_loopback_mode old_mode, mode;
-       int count, rc, link_up;
+       bool link_up;
+       int count, rc = 0;
 
-       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;
-       }
-
-       rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback);
-       if (rc) {
-               EFX_ERR(efx, "could not disable autonegotiation\n");
-               goto out;
-       }
-       tests->loopback_speed = ecmd_loopback.speed;
-       tests->loopback_full_duplex = ecmd_loopback.duplex;
+       /* 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;
                efx->loopback_mode = mode;
                efx_reconfigure_port(efx);
 
-               /* Wait for the PHY to signal the link is up */
+               /* Wait for the PHY to signal the link is up. Interrupts
+                * are enabled for PHY's using LASI, otherwise we poll()
+                * quickly */
                count = 0;
                do {
                        struct efx_channel *channel = &efx->channel[0];
 
-                       falcon_check_xmac(efx);
+                       efx->phy_op->poll(efx);
                        schedule_timeout_uninterruptible(HZ / 10);
                        if (channel->work_pending)
                                efx_process_channel_now(channel);
@@ -614,14 +609,13 @@ static int efx_test_loopbacks(struct efx_nic *efx,
                        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.
-                        */
+                       /* We need both the phy and xaui links to be ok.
+                        * rather than relying on the falcon_xmac irq/poll
+                        * regime, just poll xaui directly */
                        link_up = efx->link_up;
-                       if (!falcon_xaui_link_ok(efx))
-                               link_up = 0;
+                       if (link_up && EFX_IS10G(efx) &&
+                           !falcon_xaui_link_ok(efx))
+                               link_up = false;
 
                } while ((++count < 20) && !link_up);
 
@@ -649,10 +643,11 @@ 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;
 }
@@ -668,21 +663,27 @@ static int efx_test_loopbacks(struct efx_nic *efx,
 int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
 {
        struct efx_channel *channel;
-       int rc;
+       int rc, rc2 = 0;
+
+       rc = efx_test_mii(efx, tests);
+       if (rc && !rc2)
+               rc2 = rc;
+
+       rc = efx_test_nvram(efx, tests);
+       if (rc && !rc2)
+               rc2 = rc;
 
        rc = efx_test_interrupts(efx, tests);
-       if (rc)
-               return rc;
+       if (rc && !rc2)
+               rc2 = rc;
+
        efx_for_each_channel(channel, efx) {
-               if (channel->has_interrupt)
-                       rc = efx_test_eventq_irq(channel, tests);
-               else
-                       rc = efx_test_eventq(channel, tests);
-               if (rc)
-                       return rc;
+               rc = efx_test_eventq_irq(channel, tests);
+               if (rc && !rc2)
+                       rc2 = rc;
        }
-       rc = efx_test_phy(efx, tests);
-       return rc;
+
+       return rc2;
 }
 
 /* Offline (i.e. disruptive) testing
@@ -690,27 +691,69 @@ int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
 int efx_offline_test(struct efx_nic *efx,
                     struct efx_self_tests *tests, unsigned int loopback_modes)
 {
-       struct efx_selftest_state *state;
-       int rc;
+       enum efx_loopback_mode loopback_mode = efx->loopback_mode;
+       int phy_mode = efx->phy_mode;
+       struct ethtool_cmd ecmd, ecmd_test;
+       int rc, rc2 = 0;
+
+       /* 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
+                       efx->loopback_mode = __ffs(efx->loopback_modes);
+       }
 
-       /* Create a selftest_state structure to hold state for the test */
-       state = kzalloc(sizeof(*state), GFP_KERNEL);
-       if (state == NULL)
-               return -ENOMEM;
+       __efx_reconfigure_port(efx);
+       mutex_unlock(&efx->mac_lock);
 
-       /* 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;
+       /* free up all consumers of SRAM (including all the queues) */
+       efx_reset_down(efx, &ecmd);
 
-       rc = efx_test_loopbacks(efx, tests, loopback_modes);
+       rc = efx_test_chip(efx, tests);
+       if (rc && !rc2)
+               rc2 = rc;
 
-       efx->loopback_selftest = NULL;
-       wmb();
-       kfree(state);
+       /* reset the chip to recover from the register test */
+       rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
 
-       return rc;
+       /* Modify the saved ecmd so that when efx_reset_up() restores the phy
+        * state, AN is disabled, and the phy is powered, and out of loopback */
+       memcpy(&ecmd_test, &ecmd, sizeof(ecmd_test));
+       if (ecmd_test.autoneg == AUTONEG_ENABLE) {
+               ecmd_test.autoneg = AUTONEG_DISABLE;
+               ecmd_test.duplex = DUPLEX_FULL;
+       }
+       efx->loopback_mode = LOOPBACK_NONE;
+
+       rc = efx_reset_up(efx, &ecmd_test, rc == 0);
+       if (rc) {
+               EFX_ERR(efx, "Unable to recover from chip test\n");
+               efx_schedule_reset(efx, RESET_TYPE_DISABLE);
+               return rc;
+       }
+
+       rc = efx_test_phy(efx, tests);
+       if (rc && !rc2)
+               rc2 = rc;
+
+       rc = efx_test_loopbacks(efx, ecmd_test, tests, loopback_modes);
+       if (rc && !rc2)
+               rc2 = rc;
+
+       /* restore the PHY to the previous state */
+       efx->loopback_mode = loopback_mode;
+       efx->phy_mode = phy_mode;
+       efx->port_inhibited = false;
+       efx_ethtool_set_settings(efx->net_dev, &ecmd);
+
+       return rc2;
 }