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