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