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