Merge tag 'dma-mapping-6.5-2023-06-28' of git://git.infradead.org/users/hch/dma-mapping
[linux-block.git] / drivers / net / ethernet / sfc / selftest.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
3273c2e8 2/****************************************************************************
f7a6d2c4 3 * Driver for Solarflare network controllers and boards
3273c2e8 4 * Copyright 2005-2006 Fen Systems Ltd.
f7a6d2c4 5 * Copyright 2006-2012 Solarflare Communications Inc.
3273c2e8
BH
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>
5a0e3ad6 18#include <linux/slab.h>
3273c2e8 19#include "net_driver.h"
3273c2e8 20#include "efx.h"
e1253f39
AM
21#include "efx_common.h"
22#include "efx_channels.h"
744093c9 23#include "nic.h"
c77289b4 24#include "mcdi_port_common.h"
3273c2e8 25#include "selftest.h"
3273c2e8 26#include "workarounds.h"
3273c2e8 27
93e5dfa5
BH
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
3273c2e8
BH
38/*
39 * Loopback test packet structure
40 *
41 * The self-test should stress every RSS vector, and unfortunately
42 * Falcon only performs RSS on TCP/UDP packets.
43 */
44struct efx_loopback_payload {
cf60ed46 45 char pad[2]; /* Ensures ip is 4-byte aligned */
3273c2e8
BH
46 struct ethhdr header;
47 struct iphdr ip;
48 struct udphdr udp;
49 __be16 iteration;
1d20a160 50 char msg[64];
cf60ed46
EC
51} __packed __aligned(4);
52#define EFX_LOOPBACK_PAYLOAD_LEN (sizeof(struct efx_loopback_payload) - \
53 offsetof(struct efx_loopback_payload, \
54 header))
3273c2e8
BH
55
56/* Loopback test source MAC address */
cd84ff4d 57static const u8 payload_source[ETH_ALEN] __aligned(2) = {
3273c2e8
BH
58 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
59};
60
94de803d 61static const char payload_msg[] =
3273c2e8
BH
62 "Hello world! This is an Efx loopback test in progress!";
63
d215697f 64/* Interrupt mode names */
65static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
18e83e4c 66static const char *const efx_interrupt_mode_names[] = {
d215697f 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
3273c2e8 74/**
b1d11fdb 75 * struct efx_loopback_state - persistent state during a loopback selftest
3273c2e8
BH
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
f30eb23e 79 * @offload_csum: Checksums are being offloaded
3273c2e8
BH
80 * @rx_good: RX good packet count
81 * @rx_bad: RX bad packet count
82 * @payload: Payload used in tests
83 */
8c8661e4 84struct efx_loopback_state {
dc8cfa55 85 bool flush;
3273c2e8
BH
86 int packet_count;
87 struct sk_buff **skbs;
dc8cfa55 88 bool offload_csum;
3273c2e8
BH
89 atomic_t rx_good;
90 atomic_t rx_bad;
91 struct efx_loopback_payload payload;
92};
93
93e5dfa5
BH
94/* How long to wait for all the packets to arrive (in ms) */
95#define LOOPBACK_TIMEOUT_MS 1000
96
3273c2e8
BH
97/**************************************************************************
98 *
8c8661e4 99 * MII, NVRAM and register tests
3273c2e8
BH
100 *
101 **************************************************************************/
102
4f16c073 103static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
8c8661e4
BH
104{
105 int rc = 0;
8c8661e4 106
c77289b4
EC
107 rc = efx_mcdi_phy_test_alive(efx);
108 tests->phy_alive = rc ? -1 : 1;
8c8661e4 109
8c8661e4
BH
110 return rc;
111}
112
113static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
114{
0aa3fbaa
BH
115 int rc = 0;
116
117 if (efx->type->test_nvram) {
118 rc = efx->type->test_nvram(efx);
27324820
DP
119 if (rc == -EPERM)
120 rc = 0;
121 else
122 tests->nvram = rc ? -1 : 1;
0aa3fbaa 123 }
8c8661e4 124
8c8661e4
BH
125 return rc;
126}
127
3273c2e8
BH
128/**************************************************************************
129 *
130 * Interrupt and event queue testing
131 *
132 **************************************************************************/
133
134/* Test generation and receipt of interrupts */
135static int efx_test_interrupts(struct efx_nic *efx,
136 struct efx_self_tests *tests)
137{
93e5dfa5 138 unsigned long timeout, wait;
1646a6f3 139 int cpu;
942e298e 140 int rc;
1646a6f3 141
62776d03 142 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
3273c2e8
BH
143 tests->interrupt = -1;
144
942e298e
JC
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
93e5dfa5
BH
153 timeout = jiffies + IRQ_TIMEOUT;
154 wait = 1;
3273c2e8
BH
155
156 /* Wait for arrival of test interrupt. */
62776d03 157 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
93e5dfa5
BH
158 do {
159 schedule_timeout_uninterruptible(wait);
eee6f6a9 160 cpu = efx_nic_irq_test_irq_cpu(efx);
93e5dfa5
BH
161 if (cpu >= 0)
162 goto success;
163 wait *= 2;
164 } while (time_before(jiffies, timeout));
3273c2e8 165
62776d03 166 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
3273c2e8
BH
167 return -ETIMEDOUT;
168
169 success:
62776d03 170 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
1646a6f3 171 INT_MODE(efx), cpu);
3273c2e8
BH
172 tests->interrupt = 1;
173 return 0;
174}
175
3273c2e8 176/* Test generation and receipt of interrupting events */
ed74f480 177static int efx_test_eventq_irq(struct efx_nic *efx,
3273c2e8
BH
178 struct efx_self_tests *tests)
179{
ed74f480
BH
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;
93e5dfa5 183 unsigned long timeout, wait;
3273c2e8 184
ed74f480
BH
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);
eee6f6a9 191 efx_nic_event_test_start(channel);
ed74f480 192 }
3273c2e8 193
93e5dfa5
BH
194 timeout = jiffies + IRQ_TIMEOUT;
195 wait = 1;
3273c2e8 196
ed74f480 197 /* Wait for arrival of interrupts. NAPI processing may or may
0fb53faa
BH
198 * not complete in time, but we can cope in any case.
199 */
93e5dfa5
BH
200 do {
201 schedule_timeout_uninterruptible(wait);
202
ed74f480 203 efx_for_each_channel(channel, efx) {
36763266 204 efx_stop_eventq(channel);
ed74f480
BH
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);
eee6f6a9 213 if (efx_nic_event_test_irq_cpu(channel) >= 0)
ed74f480
BH
214 clear_bit(channel->channel, &int_pend);
215 }
36763266 216 efx_start_eventq(channel);
93e5dfa5 217 }
93e5dfa5
BH
218
219 wait *= 2;
ed74f480 220 } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
0fb53faa 221
ed74f480
BH
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 */
0fb53faa 239 netif_err(efx, drv, efx->net_dev,
ed74f480 240 "channel %d timed out waiting for event queue\n",
0fb53faa 241 channel->channel);
ed74f480
BH
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 }
3273c2e8 253 }
ed74f480
BH
254
255 return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
3273c2e8
BH
256}
257
1796721a
BH
258static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
259 unsigned flags)
3273c2e8 260{
8c8661e4 261 int rc;
3273c2e8 262
8c8661e4 263 mutex_lock(&efx->mac_lock);
c77289b4 264 rc = efx_mcdi_phy_run_tests(efx, tests->phy_ext, flags);
8c8661e4 265 mutex_unlock(&efx->mac_lock);
27324820
DP
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
8c8661e4 272 return rc;
3273c2e8
BH
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 */
285void efx_loopback_rx_packet(struct efx_nic *efx,
286 const char *buf_ptr, int pkt_len)
287{
8c8661e4 288 struct efx_loopback_state *state = efx->loopback_selftest;
cf60ed46 289 struct efx_loopback_payload received;
3273c2e8
BH
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;
8c8661e4 299
cf60ed46
EC
300 memcpy(&received.header, buf_ptr,
301 min_t(int, pkt_len, EFX_LOOPBACK_PAYLOAD_LEN));
302 received.ip.saddr = payload->ip.saddr;
60ac1065 303 if (state->offload_csum)
cf60ed46 304 received.ip.check = payload->ip.check;
60ac1065 305
3273c2e8 306 /* Check that header exists */
cf60ed46 307 if (pkt_len < sizeof(received.header)) {
62776d03
BH
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));
3273c2e8
BH
311 goto err;
312 }
313
314 /* Check that the ethernet header exists */
cf60ed46 315 if (memcmp(&received.header, &payload->header, ETH_HLEN) != 0) {
62776d03
BH
316 netif_err(efx, drv, efx->net_dev,
317 "saw non-loopback RX packet in %s loopback test\n",
318 LOOPBACK_MODE(efx));
3273c2e8
BH
319 goto err;
320 }
321
322 /* Check packet length */
cf60ed46 323 if (pkt_len != EFX_LOOPBACK_PAYLOAD_LEN) {
62776d03
BH
324 netif_err(efx, drv, efx->net_dev,
325 "saw incorrect RX packet length %d (wanted %d) in "
cf60ed46
EC
326 "%s loopback test\n", pkt_len,
327 (int)EFX_LOOPBACK_PAYLOAD_LEN, LOOPBACK_MODE(efx));
3273c2e8
BH
328 goto err;
329 }
330
331 /* Check that IP header matches */
cf60ed46 332 if (memcmp(&received.ip, &payload->ip, sizeof(payload->ip)) != 0) {
62776d03
BH
333 netif_err(efx, drv, efx->net_dev,
334 "saw corrupted IP header in %s loopback test\n",
335 LOOPBACK_MODE(efx));
3273c2e8
BH
336 goto err;
337 }
338
339 /* Check that msg and padding matches */
cf60ed46 340 if (memcmp(&received.msg, &payload->msg, sizeof(received.msg)) != 0) {
62776d03
BH
341 netif_err(efx, drv, efx->net_dev,
342 "saw corrupted RX packet in %s loopback test\n",
343 LOOPBACK_MODE(efx));
3273c2e8
BH
344 goto err;
345 }
346
347 /* Check that iteration matches */
cf60ed46 348 if (received.iteration != payload->iteration) {
62776d03
BH
349 netif_err(efx, drv, efx->net_dev,
350 "saw RX packet from iteration %d (wanted %d) in "
cf60ed46 351 "%s loopback test\n", ntohs(received.iteration),
62776d03 352 ntohs(payload->iteration), LOOPBACK_MODE(efx));
3273c2e8
BH
353 goto err;
354 }
355
356 /* Increase correct RX count */
62776d03
BH
357 netif_vdbg(efx, drv, efx->net_dev,
358 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
3273c2e8
BH
359
360 atomic_inc(&state->rx_good);
361 return;
362
363 err:
5f3f9d6c 364#ifdef DEBUG
3273c2e8 365 if (atomic_read(&state->rx_bad) == 0) {
62776d03 366 netif_err(efx, drv, efx->net_dev, "received packet:\n");
3273c2e8
BH
367 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
368 buf_ptr, pkt_len, 0);
62776d03 369 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
3273c2e8 370 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
cf60ed46
EC
371 &state->payload.header, EFX_LOOPBACK_PAYLOAD_LEN,
372 0);
3273c2e8
BH
373 }
374#endif
375 atomic_inc(&state->rx_bad);
376}
377
378/* Initialise an efx_selftest_state for a new iteration */
379static void efx_iterate_state(struct efx_nic *efx)
380{
8c8661e4 381 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
382 struct net_device *net_dev = efx->net_dev;
383 struct efx_loopback_payload *payload = &state->payload;
384
385 /* Initialise the layerII header */
cd84ff4d
EC
386 ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
387 ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
3273c2e8
BH
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;
3f978ef3 393 payload->ip.check = (__force __sum16) htons(0xdead);
cf60ed46
EC
394 payload->ip.tot_len = htons(sizeof(*payload) -
395 offsetof(struct efx_loopback_payload, ip));
3273c2e8
BH
396 payload->ip.version = IPVERSION;
397 payload->ip.protocol = IPPROTO_UDP;
398
399 /* Initialise udp header */
400 payload->udp.source = 0;
cf60ed46
EC
401 payload->udp.len = htons(sizeof(*payload) -
402 offsetof(struct efx_loopback_payload, udp));
3273c2e8
BH
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
b9aafb0e 415static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
3273c2e8
BH
416{
417 struct efx_nic *efx = tx_queue->efx;
8c8661e4 418 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
419 struct efx_loopback_payload *payload;
420 struct sk_buff *skb;
61357325
SH
421 int i;
422 netdev_tx_t rc;
3273c2e8
BH
423
424 /* Transmit N copies of buffer */
425 for (i = 0; i < state->packet_count; i++) {
8c8661e4 426 /* Allocate an skb, holding an extra reference for
3273c2e8 427 * transmit completion counting */
cf60ed46 428 skb = alloc_skb(EFX_LOOPBACK_PAYLOAD_LEN, GFP_KERNEL);
3273c2e8
BH
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 */
4df864c1 436 payload = skb_put(skb, sizeof(state->payload));
3273c2e8
BH
437 memcpy(payload, &state->payload, sizeof(state->payload));
438 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
cf60ed46
EC
439 /* Strip off the leading padding */
440 skb_pull(skb, offsetof(struct efx_loopback_payload, header));
3273c2e8
BH
441
442 /* Ensure everything we've written is visible to the
443 * interrupt handler. */
444 smp_wmb();
445
73ba7b68 446 netif_tx_lock_bh(efx->net_dev);
497f5ba3 447 rc = efx_enqueue_skb(tx_queue, skb);
73ba7b68 448 netif_tx_unlock_bh(efx->net_dev);
3273c2e8
BH
449
450 if (rc != NETDEV_TX_OK) {
62776d03
BH
451 netif_err(efx, drv, efx->net_dev,
452 "TX queue %d could not transmit packet %d of "
a81dcd85 453 "%d in %s loopback test\n", tx_queue->label,
62776d03
BH
454 i + 1, state->packet_count,
455 LOOPBACK_MODE(efx));
3273c2e8
BH
456
457 /* Defer cleaning up the other skbs for the caller */
458 kfree_skb(skb);
459 return -EPIPE;
460 }
461 }
462
463 return 0;
464}
465
b9aafb0e
BH
466static int efx_poll_loopback(struct efx_nic *efx)
467{
8c8661e4 468 struct efx_loopback_state *state = efx->loopback_selftest;
b9aafb0e 469
b9aafb0e
BH
470 return atomic_read(&state->rx_good) == state->packet_count;
471}
472
473static int efx_end_loopback(struct efx_tx_queue *tx_queue,
474 struct efx_loopback_self_tests *lb_tests)
3273c2e8
BH
475{
476 struct efx_nic *efx = tx_queue->efx;
8c8661e4 477 struct efx_loopback_state *state = efx->loopback_selftest;
3273c2e8
BH
478 struct sk_buff *skb;
479 int tx_done = 0, rx_good, rx_bad;
480 int i, rc = 0;
481
73ba7b68 482 netif_tx_lock_bh(efx->net_dev);
3273c2e8
BH
483
484 /* Count the number of tx completions, and decrement the refcnt. Any
485 * skbs not already completed will be free'd when the queue is flushed */
9c636baf 486 for (i = 0; i < state->packet_count; i++) {
3273c2e8
BH
487 skb = state->skbs[i];
488 if (skb && !skb_shared(skb))
489 ++tx_done;
e3ed2bdf 490 dev_kfree_skb(skb);
3273c2e8
BH
491 }
492
73ba7b68 493 netif_tx_unlock_bh(efx->net_dev);
3273c2e8
BH
494
495 /* Check TX completion and received packet counts */
496 rx_good = atomic_read(&state->rx_good);
497 rx_bad = atomic_read(&state->rx_bad);
498 if (tx_done != state->packet_count) {
499 /* Don't free the skbs; they will be picked up on TX
500 * overflow or channel teardown.
501 */
62776d03
BH
502 netif_err(efx, drv, efx->net_dev,
503 "TX queue %d saw only %d out of an expected %d "
504 "TX completion events in %s loopback test\n",
a81dcd85 505 tx_queue->label, tx_done, state->packet_count,
62776d03 506 LOOPBACK_MODE(efx));
3273c2e8
BH
507 rc = -ETIMEDOUT;
508 /* Allow to fall through so we see the RX errors as well */
509 }
510
511 /* We may always be up to a flush away from our desired packet total */
512 if (rx_good != state->packet_count) {
62776d03
BH
513 netif_dbg(efx, drv, efx->net_dev,
514 "TX queue %d saw only %d out of an expected %d "
515 "received packets in %s loopback test\n",
a81dcd85 516 tx_queue->label, rx_good, state->packet_count,
62776d03 517 LOOPBACK_MODE(efx));
3273c2e8
BH
518 rc = -ETIMEDOUT;
519 /* Fall through */
520 }
521
522 /* Update loopback test structure */
a81dcd85
EC
523 lb_tests->tx_sent[tx_queue->label] += state->packet_count;
524 lb_tests->tx_done[tx_queue->label] += tx_done;
3273c2e8
BH
525 lb_tests->rx_good += rx_good;
526 lb_tests->rx_bad += rx_bad;
527
528 return rc;
529}
530
531static int
532efx_test_loopback(struct efx_tx_queue *tx_queue,
533 struct efx_loopback_self_tests *lb_tests)
534{
535 struct efx_nic *efx = tx_queue->efx;
8c8661e4 536 struct efx_loopback_state *state = efx->loopback_selftest;
b9aafb0e 537 int i, begin_rc, end_rc;
3273c2e8 538
8c8661e4 539 for (i = 0; i < 3; i++) {
3273c2e8 540 /* Determine how many packets to send */
ecc910f5 541 state->packet_count = efx->txq_entries / 3;
3273c2e8 542 state->packet_count = min(1 << (i << 2), state->packet_count);
c2e4e25a
TM
543 state->skbs = kcalloc(state->packet_count,
544 sizeof(state->skbs[0]), GFP_KERNEL);
9b7bfc4c
BH
545 if (!state->skbs)
546 return -ENOMEM;
dc8cfa55 547 state->flush = false;
3273c2e8 548
62776d03 549 netif_dbg(efx, drv, efx->net_dev,
a81dcd85
EC
550 "TX queue %d (hw %d) testing %s loopback with %d packets\n",
551 tx_queue->label, tx_queue->queue, LOOPBACK_MODE(efx),
62776d03 552 state->packet_count);
3273c2e8
BH
553
554 efx_iterate_state(efx);
b9aafb0e
BH
555 begin_rc = efx_begin_loopback(tx_queue);
556
557 /* This will normally complete very quickly, but be
93e5dfa5 558 * prepared to wait much longer. */
b9aafb0e
BH
559 msleep(1);
560 if (!efx_poll_loopback(efx)) {
93e5dfa5 561 msleep(LOOPBACK_TIMEOUT_MS);
b9aafb0e 562 efx_poll_loopback(efx);
3273c2e8
BH
563 }
564
b9aafb0e 565 end_rc = efx_end_loopback(tx_queue, lb_tests);
3273c2e8
BH
566 kfree(state->skbs);
567
b9aafb0e 568 if (begin_rc || end_rc) {
3273c2e8
BH
569 /* Wait a while to ensure there are no packets
570 * floating around after a failure. */
571 schedule_timeout_uninterruptible(HZ / 10);
b9aafb0e 572 return begin_rc ? begin_rc : end_rc;
3273c2e8
BH
573 }
574 }
575
62776d03
BH
576 netif_dbg(efx, drv, efx->net_dev,
577 "TX queue %d passed %s loopback test with a burst length "
a81dcd85 578 "of %d packets\n", tx_queue->label, LOOPBACK_MODE(efx),
62776d03 579 state->packet_count);
3273c2e8 580
a0c2c190 581 return 0;
3273c2e8
BH
582}
583
78c1f0a0
SH
584/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
585 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
586 * to delay and retry. Therefore, it's safer to just poll directly. Wait
587 * for link up and any faults to dissipate. */
588static int efx_wait_for_link(struct efx_nic *efx)
589{
590 struct efx_link_state *link_state = &efx->link_state;
901d3fe8 591 int count, link_up_count = 0;
78c1f0a0
SH
592 bool link_up;
593
594 for (count = 0; count < 40; count++) {
595 schedule_timeout_uninterruptible(HZ / 10);
596
597 if (efx->type->monitor != NULL) {
598 mutex_lock(&efx->mac_lock);
599 efx->type->monitor(efx);
600 mutex_unlock(&efx->mac_lock);
78c1f0a0
SH
601 }
602
603 mutex_lock(&efx->mac_lock);
604 link_up = link_state->up;
605 if (link_up)
710b208d 606 link_up = !efx->type->check_mac_fault(efx);
78c1f0a0
SH
607 mutex_unlock(&efx->mac_lock);
608
901d3fe8
SH
609 if (link_up) {
610 if (++link_up_count == 2)
611 return 0;
612 } else {
613 link_up_count = 0;
614 }
78c1f0a0
SH
615 }
616
617 return -ETIMEDOUT;
618}
619
a5692e49 620static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
3273c2e8
BH
621 unsigned int loopback_modes)
622{
8c8661e4
BH
623 enum efx_loopback_mode mode;
624 struct efx_loopback_state *state;
1ac0226e
BH
625 struct efx_channel *channel =
626 efx_get_channel(efx, efx->tx_channel_offset);
3273c2e8 627 struct efx_tx_queue *tx_queue;
78c1f0a0 628 int rc = 0;
3273c2e8 629
8c8661e4
BH
630 /* Set the port loopback_selftest member. From this point on
631 * all received packets will be dropped. Mark the state as
632 * "flushing" so all inflight packets are dropped */
633 state = kzalloc(sizeof(*state), GFP_KERNEL);
634 if (state == NULL)
635 return -ENOMEM;
636 BUG_ON(efx->loopback_selftest);
637 state->flush = true;
638 efx->loopback_selftest = state;
3273c2e8
BH
639
640 /* Test all supported loopback modes */
8c8661e4 641 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
3273c2e8
BH
642 if (!(loopback_modes & (1 << mode)))
643 continue;
644
645 /* Move the port into the specified loopback mode. */
dc8cfa55 646 state->flush = true;
78c1f0a0 647 mutex_lock(&efx->mac_lock);
3273c2e8 648 efx->loopback_mode = mode;
78c1f0a0
SH
649 rc = __efx_reconfigure_port(efx);
650 mutex_unlock(&efx->mac_lock);
651 if (rc) {
62776d03
BH
652 netif_err(efx, drv, efx->net_dev,
653 "unable to move into %s loopback\n",
654 LOOPBACK_MODE(efx));
78c1f0a0
SH
655 goto out;
656 }
3273c2e8 657
78c1f0a0
SH
658 rc = efx_wait_for_link(efx);
659 if (rc) {
62776d03
BH
660 netif_err(efx, drv, efx->net_dev,
661 "loopback %s never came up\n",
662 LOOPBACK_MODE(efx));
3273c2e8
BH
663 goto out;
664 }
665
94b274bf 666 /* Test all enabled types of TX queue */
f7d12cdc 667 efx_for_each_channel_tx_queue(tx_queue, channel) {
12804793 668 state->offload_csum = (tx_queue->type &
044588b9 669 EFX_TXQ_TYPE_OUTER_CSUM);
f8ea0b67
BH
670 rc = efx_test_loopback(tx_queue,
671 &tests->loopback[mode]);
3273c2e8
BH
672 if (rc)
673 goto out;
674 }
675 }
676
677 out:
8c8661e4 678 /* Remove the flush. The caller will remove the loopback setting */
dc8cfa55 679 state->flush = true;
8c8661e4
BH
680 efx->loopback_selftest = NULL;
681 wmb();
682 kfree(state);
3273c2e8 683
27324820
DP
684 if (rc == -EPERM)
685 rc = 0;
686
3273c2e8
BH
687 return rc;
688}
689
690/**************************************************************************
691 *
2ef3068e 692 * Entry point
3273c2e8
BH
693 *
694 *************************************************************************/
695
2ef3068e
BH
696int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
697 unsigned flags)
3273c2e8 698{
2ef3068e
BH
699 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
700 int phy_mode = efx->phy_mode;
d4f2cecc 701 int rc_test = 0, rc_reset, rc;
2ef3068e 702
dd40781e
BH
703 efx_selftest_async_cancel(efx);
704
2ef3068e
BH
705 /* Online (i.e. non-disruptive) testing
706 * This checks interrupt generation, event delivery and PHY presence. */
8c8661e4 707
4f16c073 708 rc = efx_test_phy_alive(efx, tests);
2ef3068e
BH
709 if (rc && !rc_test)
710 rc_test = rc;
8c8661e4
BH
711
712 rc = efx_test_nvram(efx, tests);
2ef3068e
BH
713 if (rc && !rc_test)
714 rc_test = rc;
3273c2e8 715
f8ea0b67 716 rc = efx_test_interrupts(efx, tests);
2ef3068e
BH
717 if (rc && !rc_test)
718 rc_test = rc;
8c8661e4 719
ed74f480
BH
720 rc = efx_test_eventq_irq(efx, tests);
721 if (rc && !rc_test)
722 rc_test = rc;
8c8661e4 723
2ef3068e
BH
724 if (rc_test)
725 return rc_test;
3273c2e8 726
2ef3068e 727 if (!(flags & ETH_TEST_FL_OFFLINE))
1796721a 728 return efx_test_phy(efx, tests, flags);
2ef3068e
BH
729
730 /* Offline (i.e. disruptive) testing
731 * This checks MAC and PHY loopback on the specified port. */
8c8661e4 732
e4abce85
BH
733 /* Detach the device so the kernel doesn't transmit during the
734 * loopback test and the watchdog timeout doesn't fire.
8c8661e4 735 */
c2f3b8e3 736 efx_device_detach_sync(efx);
e4abce85 737
d4f2cecc
BH
738 if (efx->type->test_chip) {
739 rc_reset = efx->type->test_chip(efx, tests);
740 if (rc_reset) {
741 netif_err(efx, hw, efx->net_dev,
742 "Unable to recover from chip test\n");
743 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
744 return rc_reset;
745 }
8c8661e4 746
74cd60a4 747 if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
d4f2cecc
BH
748 rc_test = -EIO;
749 }
8c8661e4 750
a5692e49
BH
751 /* Ensure that the phy is powered and out of loopback
752 * for the bist and loopback tests */
d4f2cecc 753 mutex_lock(&efx->mac_lock);
a5692e49 754 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
8c8661e4 755 efx->loopback_mode = LOOPBACK_NONE;
d4f2cecc
BH
756 __efx_reconfigure_port(efx);
757 mutex_unlock(&efx->mac_lock);
3273c2e8 758
1796721a 759 rc = efx_test_phy(efx, tests, flags);
2ef3068e
BH
760 if (rc && !rc_test)
761 rc_test = rc;
3273c2e8 762
2ef3068e
BH
763 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
764 if (rc && !rc_test)
765 rc_test = rc;
3273c2e8 766
8c8661e4 767 /* restore the PHY to the previous state */
d3245b28 768 mutex_lock(&efx->mac_lock);
8c8661e4 769 efx->phy_mode = phy_mode;
d3245b28
BH
770 efx->loopback_mode = loopback_mode;
771 __efx_reconfigure_port(efx);
772 mutex_unlock(&efx->mac_lock);
8c8661e4 773
9c568fd8 774 efx_device_attach_if_not_resetting(efx);
9d1aea62 775
2ef3068e 776 return rc_test;
3273c2e8
BH
777}
778
dd40781e
BH
779void efx_selftest_async_start(struct efx_nic *efx)
780{
781 struct efx_channel *channel;
782
783 efx_for_each_channel(channel, efx)
784 efx_nic_event_test_start(channel);
785 schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
786}
787
788void efx_selftest_async_cancel(struct efx_nic *efx)
789{
790 cancel_delayed_work_sync(&efx->selftest_work);
791}
792
86de7ced 793static void efx_selftest_async_work(struct work_struct *data)
dd40781e
BH
794{
795 struct efx_nic *efx = container_of(data, struct efx_nic,
796 selftest_work.work);
797 struct efx_channel *channel;
798 int cpu;
799
800 efx_for_each_channel(channel, efx) {
801 cpu = efx_nic_event_test_irq_cpu(channel);
802 if (cpu < 0)
803 netif_err(efx, ifup, efx->net_dev,
804 "channel %d failed to trigger an interrupt\n",
805 channel->channel);
806 else
807 netif_dbg(efx, ifup, efx->net_dev,
808 "channel %d triggered interrupt on CPU %d\n",
809 channel->channel, cpu);
810 }
811}
86de7ced
AM
812
813void efx_selftest_async_init(struct efx_nic *efx)
814{
815 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
816}