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