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