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