sfc: Refactor link configuration
[safe/jmp/linux-2.6] / drivers / net / sfc / selftest.c
index cbda159..dddeb9d 100644 (file)
 #include <linux/rtnetlink.h>
 #include <asm/io.h>
 #include "net_driver.h"
-#include "ethtool.h"
 #include "efx.h"
 #include "falcon.h"
 #include "selftest.h"
-#include "boards.h"
 #include "workarounds.h"
-#include "mac.h"
+#include "spi.h"
+#include "io.h"
+#include "mdio_10g.h"
 
 /*
  * Loopback test packet structure
@@ -51,18 +51,20 @@ 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;
+       bool offload_csum;
        atomic_t rx_good;
        atomic_t rx_bad;
        struct efx_loopback_payload payload;
@@ -70,21 +72,66 @@ 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_mdio(struct efx_nic *efx, struct efx_self_tests *tests)
+{
+       int rc = 0;
+       int devad = __ffs(efx->mdio.mmds);
+       u16 physid1, physid2;
+
+       if (efx->phy_type == PHY_TYPE_NONE)
+               return 0;
+
+       mutex_lock(&efx->mac_lock);
+       tests->mdio = -1;
+
+       physid1 = efx_mdio_read(efx, devad, MDIO_DEVID1);
+       physid2 = efx_mdio_read(efx, devad, MDIO_DEVID2);
+
+       if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
+           (physid2 == 0x0000) || (physid2 == 0xffff)) {
+               EFX_ERR(efx, "no MDIO PHY present with ID %d\n",
+                       efx->mdio.prtad);
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (EFX_IS10G(efx)) {
+               rc = efx_mdio_check_mmds(efx, efx->phy_op->mmds, 0);
+               if (rc)
+                       goto out;
+       }
+
+out:
+       mutex_unlock(&efx->mac_lock);
+       tests->mdio = 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 (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
+               return 0;
+
+       rc = falcon_test_registers(efx);
+       tests->registers = rc ? -1 : 1;
+       return rc;
+}
 
 /**************************************************************************
  *
@@ -107,7 +154,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)
@@ -126,47 +173,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)
@@ -230,39 +242,21 @@ 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;
+       int rc;
 
-       if (efx->phy_type == PHY_TYPE_NONE)
+       if (!efx->phy_op->run_tests)
                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);
+       EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
+                           efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
 
-       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;
-               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, flags);
+       mutex_unlock(&efx->mac_lock);
+       return rc;
 }
 
 /**************************************************************************
@@ -278,7 +272,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;
 
@@ -289,11 +283,12 @@ void efx_loopback_rx_packet(struct efx_nic *efx,
                return;
 
        payload = &state->payload;
-       
-       received = (struct efx_loopback_payload *)(char *) buf_ptr;
+
+       received = (struct efx_loopback_payload *) buf_ptr;
        received->ip.saddr = payload->ip.saddr;
-       received->ip.check = payload->ip.check;
-       
+       if (state->offload_csum)
+               received->ip.check = payload->ip.check;
+
        /* Check that header exists */
        if (pkt_len < sizeof(received->header)) {
                EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
@@ -362,7 +357,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;
 
@@ -395,17 +390,18 @@ 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;
+       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)
@@ -424,10 +420,10 @@ static int efx_tx_loopback(struct efx_tx_queue *tx_queue)
                 * interrupt handler. */
                smp_wmb();
 
-               if (NET_DEV_REGISTERED(efx))
+               if (efx_dev_registered(efx))
                        netif_tx_lock_bh(efx->net_dev);
-               rc = efx_xmit(efx, tx_queue, skb);
-               if (NET_DEV_REGISTERED(efx))
+               rc = efx_enqueue_skb(tx_queue, skb);
+               if (efx_dev_registered(efx))
                        netif_tx_unlock_bh(efx->net_dev);
 
                if (rc != NETDEV_TX_OK) {
@@ -444,16 +440,30 @@ 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;
 
-       if (NET_DEV_REGISTERED(efx))
+       if (efx_dev_registered(efx))
                netif_tx_lock_bh(efx->net_dev);
 
        /* Count the number of tx completions, and decrement the refcnt. Any
@@ -465,7 +475,7 @@ static int efx_rx_loopback(struct efx_tx_queue *tx_queue,
                dev_kfree_skb_any(skb);
        }
 
-       if (NET_DEV_REGISTERED(efx))
+       if (efx_dev_registered(efx))
                netif_tx_unlock_bh(efx->net_dev);
 
        /* Check TX completion and received packet counts */
@@ -507,40 +517,42 @@ 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, rc = 0;
+       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);
-               state->flush = 0;
+               if (!state->skbs)
+                       return -ENOMEM;
+               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);
-               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);
                }
 
-               rc |= efx_rx_loopback(tx_queue, lb_tests);
+               end_rc = efx_end_loopback(tx_queue, lb_tests);
                kfree(state->skbs);
 
-               if (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 rc;
+                       return begin_rc ? begin_rc : end_rc;
                }
        }
 
@@ -548,58 +560,46 @@ efx_test_loopback(struct efx_tx_queue *tx_queue,
                "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
                state->packet_count);
 
-       return rc;
+       return 0;
 }
 
-static int efx_test_loopbacks(struct efx_nic *efx,
-                             struct efx_self_tests *tests,
+static int efx_test_loopbacks(struct efx_nic *efx, 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 = 0, link_up;
-       
-       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;
-       }
+       bool link_up;
+       int count, rc = 0;
 
-       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);
@@ -607,14 +607,10 @@ 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.
-                        */
-                       link_up = efx->link_up;
-                       if (!falcon_xaui_link_ok(efx))
-                               link_up = 0;
+                       /* We need both the PHY and MAC-PHY links to be OK */
+                       link_up = efx->link_state.up;
+                       if (link_up)
+                               link_up = !efx->mac_op->check_fault(efx);
 
                } while ((++count < 20) && !link_up);
 
@@ -632,86 +628,129 @@ static int efx_test_loopbacks(struct efx_nic *efx,
 
                /* Test every TX queue */
                efx_for_each_tx_queue(tx_queue, efx) {
-                       rc |= efx_test_loopback(tx_queue,
-                                               &tests->loopback[mode]);
+                       state->offload_csum = (tx_queue->queue ==
+                                              EFX_TX_QUEUE_OFFLOAD_CSUM);
+                       rc = efx_test_loopback(tx_queue,
+                                              &tests->loopback[mode]);
                        if (rc)
                                goto out;
                }
        }
 
  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 = 0;
+       int rc_test = 0, rc_reset = 0, rc;
 
-       EFX_LOG(efx, "performing online self-tests\n");
+       /* Online (i.e. non-disruptive) testing
+        * This checks interrupt generation, event delivery and PHY presence. */
+
+       rc = efx_test_mdio(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 && !rc_test)
+               rc_test = rc;
 
-       rc |= efx_test_interrupts(efx, tests);
        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);
+                       efx->loopback_mode = __ffs(efx->loopback_modes);
        }
-       rc |= efx_test_phy(efx, tests);
 
-       if (rc)
-               EFX_ERR(efx, "failed online self-tests\n");
+       __efx_reconfigure_port(efx);
+       mutex_unlock(&efx->mac_lock);
 
-       return rc;
-}
+       /* free up all consumers of SRAM (including all the queues) */
+       efx_reset_down(efx, reset_method);
 
-/* 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 = 0;
+       rc = efx_test_chip(efx, tests);
+       if (rc && !rc_test)
+               rc_test = rc;
 
-       EFX_LOG(efx, "performing offline self-tests\n");
+       /* reset the chip to recover from the register test */
+       rc_reset = efx->type->reset(efx, reset_method);
 
-       /* Create a selftest_state structure to hold state for the test */
-       state = kzalloc(sizeof(*state), GFP_KERNEL);
-       if (state == NULL) {
-               rc = -ENOMEM;
-               goto out;
-       }
+       /* 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;
 
-       /* 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 = (void *)state;
+       rc = efx_reset_up(efx, reset_method, rc_reset == 0);
+       if (rc && !rc_reset)
+               rc_reset = rc;
 
-       rc = efx_test_loopbacks(efx, tests, loopback_modes);
+       if (rc_reset) {
+               EFX_ERR(efx, "Unable to recover from chip test\n");
+               efx_schedule_reset(efx, RESET_TYPE_DISABLE);
+               return rc_reset;
+       }
 
-       efx->loopback_selftest = NULL;
-       wmb();
-       kfree(state);
+       rc = efx_test_phy(efx, tests, flags);
+       if (rc && !rc_test)
+               rc_test = rc;
 
- out:
-       if (rc)
-               EFX_ERR(efx, "failed offline self-tests\n");
+       rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
+       if (rc && !rc_test)
+               rc_test = rc;
 
-       return 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;
 }