blob: 817c7efc11e09b74bffb1f25e84b6e1035225f12 [file] [log] [blame]
Ben Hutchings3273c2e2008-05-07 13:36:19 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
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>
21#include <asm/io.h>
22#include "net_driver.h"
23#include "ethtool.h"
24#include "efx.h"
25#include "falcon.h"
26#include "selftest.h"
27#include "boards.h"
28#include "workarounds.h"
Ben Hutchings8c8661e2008-09-01 12:49:02 +010029#include "spi.h"
30#include "falcon_io.h"
31#include "mdio_10g.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010032
33/*
34 * Loopback test packet structure
35 *
36 * The self-test should stress every RSS vector, and unfortunately
37 * Falcon only performs RSS on TCP/UDP packets.
38 */
39struct efx_loopback_payload {
40 struct ethhdr header;
41 struct iphdr ip;
42 struct udphdr udp;
43 __be16 iteration;
44 const char msg[64];
45} __attribute__ ((packed));
46
47/* Loopback test source MAC address */
48static const unsigned char payload_source[ETH_ALEN] = {
49 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
50};
51
52static const char *payload_msg =
53 "Hello world! This is an Efx loopback test in progress!";
54
55/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010056 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010057 * @flush: Drop all packets in efx_loopback_rx_packet
58 * @packet_count: Number of packets being used in this test
59 * @skbs: An array of skbs transmitted
60 * @rx_good: RX good packet count
61 * @rx_bad: RX bad packet count
62 * @payload: Payload used in tests
63 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010064struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010065 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010066 int packet_count;
67 struct sk_buff **skbs;
Ben Hutchings60ac1062008-09-01 12:44:59 +010068
69 /* Checksums are being offloaded */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010070 bool offload_csum;
Ben Hutchings60ac1062008-09-01 12:44:59 +010071
Ben Hutchings3273c2e2008-05-07 13:36:19 +010072 atomic_t rx_good;
73 atomic_t rx_bad;
74 struct efx_loopback_payload payload;
75};
76
77/**************************************************************************
78 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010079 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010080 *
81 **************************************************************************/
82
Ben Hutchings68e7f452009-04-29 08:05:08 +000083static int efx_test_mdio(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010084{
85 int rc = 0;
Ben Hutchings68e7f452009-04-29 08:05:08 +000086 int devad = __ffs(efx->mdio.mmds);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010087 u16 physid1, physid2;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010088
89 if (efx->phy_type == PHY_TYPE_NONE)
90 return 0;
91
92 mutex_lock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +000093 tests->mdio = -1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010094
Ben Hutchings68e7f452009-04-29 08:05:08 +000095 physid1 = efx_mdio_read(efx, devad, MDIO_DEVID1);
96 physid2 = efx_mdio_read(efx, devad, MDIO_DEVID2);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010097
98 if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
99 (physid2 == 0x0000) || (physid2 == 0xffff)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +0000100 EFX_ERR(efx, "no MDIO PHY present with ID %d\n",
101 efx->mdio.prtad);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100102 rc = -EINVAL;
103 goto out;
104 }
105
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800106 if (EFX_IS10G(efx)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +0000107 rc = efx_mdio_check_mmds(efx, efx->phy_op->mmds, 0);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800108 if (rc)
109 goto out;
110 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100111
112out:
113 mutex_unlock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +0000114 tests->mdio = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100115 return rc;
116}
117
118static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
119{
120 int rc;
121
122 rc = falcon_read_nvram(efx, NULL);
123 tests->nvram = rc ? -1 : 1;
124 return rc;
125}
126
127static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
128{
129 int rc;
130
131 /* Not supported on A-series silicon */
132 if (falcon_rev(efx) < FALCON_REV_B0)
133 return 0;
134
135 rc = falcon_test_registers(efx);
136 tests->registers = rc ? -1 : 1;
137 return rc;
138}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100139
140/**************************************************************************
141 *
142 * Interrupt and event queue testing
143 *
144 **************************************************************************/
145
146/* Test generation and receipt of interrupts */
147static int efx_test_interrupts(struct efx_nic *efx,
148 struct efx_self_tests *tests)
149{
150 struct efx_channel *channel;
151
152 EFX_LOG(efx, "testing interrupts\n");
153 tests->interrupt = -1;
154
155 /* Reset interrupt flag */
156 efx->last_irq_cpu = -1;
157 smp_wmb();
158
159 /* ACK each interrupting event queue. Receiving an interrupt due to
160 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100161 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100162 if (channel->work_pending)
163 efx_process_channel_now(channel);
164 if (efx->last_irq_cpu >= 0)
165 goto success;
166 }
167
168 falcon_generate_interrupt(efx);
169
170 /* Wait for arrival of test interrupt. */
171 EFX_LOG(efx, "waiting for test interrupt\n");
172 schedule_timeout_uninterruptible(HZ / 10);
173 if (efx->last_irq_cpu >= 0)
174 goto success;
175
176 EFX_ERR(efx, "timed out waiting for interrupt\n");
177 return -ETIMEDOUT;
178
179 success:
180 EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
181 efx->interrupt_mode, efx->last_irq_cpu);
182 tests->interrupt = 1;
183 return 0;
184}
185
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100186/* Test generation and receipt of interrupting events */
187static int efx_test_eventq_irq(struct efx_channel *channel,
188 struct efx_self_tests *tests)
189{
190 unsigned int magic, count;
191
192 /* Channel specific code, limited to 20 bits */
193 magic = (0x00010150 + channel->channel);
194 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
195 channel->channel, magic);
196
197 tests->eventq_dma[channel->channel] = -1;
198 tests->eventq_int[channel->channel] = -1;
199 tests->eventq_poll[channel->channel] = -1;
200
201 /* Reset flag and zero magic word */
202 channel->efx->last_irq_cpu = -1;
203 channel->eventq_magic = 0;
204 smp_wmb();
205
206 falcon_generate_test_event(channel, magic);
207
208 /* Wait for arrival of interrupt */
209 count = 0;
210 do {
211 schedule_timeout_uninterruptible(HZ / 100);
212
213 if (channel->work_pending)
214 efx_process_channel_now(channel);
215
216 if (channel->eventq_magic == magic)
217 goto eventq_ok;
218 } while (++count < 2);
219
220 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
221 channel->channel);
222
223 /* See if interrupt arrived */
224 if (channel->efx->last_irq_cpu >= 0) {
225 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
226 "during event queue test\n", channel->channel,
227 raw_smp_processor_id());
228 tests->eventq_int[channel->channel] = 1;
229 }
230
231 /* Check to see if event was received even if interrupt wasn't */
232 efx_process_channel_now(channel);
233 if (channel->eventq_magic == magic) {
234 EFX_ERR(channel->efx, "channel %d event was generated, but "
235 "failed to trigger an interrupt\n", channel->channel);
236 tests->eventq_dma[channel->channel] = 1;
237 }
238
239 return -ETIMEDOUT;
240 eventq_ok:
241 EFX_LOG(channel->efx, "channel %d event queue passed\n",
242 channel->channel);
243 tests->eventq_dma[channel->channel] = 1;
244 tests->eventq_int[channel->channel] = 1;
245 tests->eventq_poll[channel->channel] = 1;
246 return 0;
247}
248
Ben Hutchings17967212008-12-26 13:47:25 -0800249static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
250 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100251{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100252 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100253
Ben Hutchings17967212008-12-26 13:47:25 -0800254 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100255 return 0;
256
Ben Hutchings17967212008-12-26 13:47:25 -0800257 EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
258 efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
259
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100260 mutex_lock(&efx->mac_lock);
Ben Hutchings17967212008-12-26 13:47:25 -0800261 rc = efx->phy_op->run_tests(efx, tests->phy, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100262 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100263 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100264}
265
266/**************************************************************************
267 *
268 * Loopback testing
269 * NB Only one loopback test can be executing concurrently.
270 *
271 **************************************************************************/
272
273/* Loopback test RX callback
274 * This is called for each received packet during loopback testing.
275 */
276void efx_loopback_rx_packet(struct efx_nic *efx,
277 const char *buf_ptr, int pkt_len)
278{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100279 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100280 struct efx_loopback_payload *received;
281 struct efx_loopback_payload *payload;
282
283 BUG_ON(!buf_ptr);
284
285 /* If we are just flushing, then drop the packet */
286 if ((state == NULL) || state->flush)
287 return;
288
289 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100290
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100291 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100292 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100293 if (state->offload_csum)
294 received->ip.check = payload->ip.check;
295
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100296 /* Check that header exists */
297 if (pkt_len < sizeof(received->header)) {
298 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
299 "test\n", pkt_len, LOOPBACK_MODE(efx));
300 goto err;
301 }
302
303 /* Check that the ethernet header exists */
304 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
305 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
306 LOOPBACK_MODE(efx));
307 goto err;
308 }
309
310 /* Check packet length */
311 if (pkt_len != sizeof(*payload)) {
312 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
313 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
314 LOOPBACK_MODE(efx));
315 goto err;
316 }
317
318 /* Check that IP header matches */
319 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
320 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
321 LOOPBACK_MODE(efx));
322 goto err;
323 }
324
325 /* Check that msg and padding matches */
326 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
327 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
328 LOOPBACK_MODE(efx));
329 goto err;
330 }
331
332 /* Check that iteration matches */
333 if (received->iteration != payload->iteration) {
334 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
335 "%s loopback test\n", ntohs(received->iteration),
336 ntohs(payload->iteration), LOOPBACK_MODE(efx));
337 goto err;
338 }
339
340 /* Increase correct RX count */
341 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
342 LOOPBACK_MODE(efx));
343
344 atomic_inc(&state->rx_good);
345 return;
346
347 err:
348#ifdef EFX_ENABLE_DEBUG
349 if (atomic_read(&state->rx_bad) == 0) {
350 EFX_ERR(efx, "received packet:\n");
351 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
352 buf_ptr, pkt_len, 0);
353 EFX_ERR(efx, "expected packet:\n");
354 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
355 &state->payload, sizeof(state->payload), 0);
356 }
357#endif
358 atomic_inc(&state->rx_bad);
359}
360
361/* Initialise an efx_selftest_state for a new iteration */
362static void efx_iterate_state(struct efx_nic *efx)
363{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100364 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100365 struct net_device *net_dev = efx->net_dev;
366 struct efx_loopback_payload *payload = &state->payload;
367
368 /* Initialise the layerII header */
369 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
370 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
371 payload->header.h_proto = htons(ETH_P_IP);
372
373 /* saddr set later and used as incrementing count */
374 payload->ip.daddr = htonl(INADDR_LOOPBACK);
375 payload->ip.ihl = 5;
376 payload->ip.check = htons(0xdead);
377 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
378 payload->ip.version = IPVERSION;
379 payload->ip.protocol = IPPROTO_UDP;
380
381 /* Initialise udp header */
382 payload->udp.source = 0;
383 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
384 sizeof(struct iphdr));
385 payload->udp.check = 0; /* checksum ignored */
386
387 /* Fill out payload */
388 payload->iteration = htons(ntohs(payload->iteration) + 1);
389 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
390
391 /* Fill out remaining state members */
392 atomic_set(&state->rx_good, 0);
393 atomic_set(&state->rx_bad, 0);
394 smp_wmb();
395}
396
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100397static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100398{
399 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100400 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100401 struct efx_loopback_payload *payload;
402 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000403 int i;
404 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100405
406 /* Transmit N copies of buffer */
407 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100408 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100409 * transmit completion counting */
410 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
411 if (!skb)
412 return -ENOMEM;
413 state->skbs[i] = skb;
414 skb_get(skb);
415
416 /* Copy the payload in, incrementing the source address to
417 * exercise the rss vectors */
418 payload = ((struct efx_loopback_payload *)
419 skb_put(skb, sizeof(state->payload)));
420 memcpy(payload, &state->payload, sizeof(state->payload));
421 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
422
423 /* Ensure everything we've written is visible to the
424 * interrupt handler. */
425 smp_wmb();
426
Ben Hutchings55668612008-05-16 21:16:10 +0100427 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100428 netif_tx_lock_bh(efx->net_dev);
429 rc = efx_xmit(efx, tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100430 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100431 netif_tx_unlock_bh(efx->net_dev);
432
433 if (rc != NETDEV_TX_OK) {
434 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
435 "of %d in %s loopback test\n", tx_queue->queue,
436 i + 1, state->packet_count, LOOPBACK_MODE(efx));
437
438 /* Defer cleaning up the other skbs for the caller */
439 kfree_skb(skb);
440 return -EPIPE;
441 }
Eric Dumazet28679752009-05-27 19:26:37 +0000442 efx->net_dev->trans_start = jiffies;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100443 }
444
445 return 0;
446}
447
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100448static int efx_poll_loopback(struct efx_nic *efx)
449{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100450 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100451 struct efx_channel *channel;
452
453 /* NAPI polling is not enabled, so process channels
454 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100455 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100456 if (channel->work_pending)
457 efx_process_channel_now(channel);
458 }
459 return atomic_read(&state->rx_good) == state->packet_count;
460}
461
462static int efx_end_loopback(struct efx_tx_queue *tx_queue,
463 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100464{
465 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100466 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100467 struct sk_buff *skb;
468 int tx_done = 0, rx_good, rx_bad;
469 int i, rc = 0;
470
Ben Hutchings55668612008-05-16 21:16:10 +0100471 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100472 netif_tx_lock_bh(efx->net_dev);
473
474 /* Count the number of tx completions, and decrement the refcnt. Any
475 * skbs not already completed will be free'd when the queue is flushed */
476 for (i=0; i < state->packet_count; i++) {
477 skb = state->skbs[i];
478 if (skb && !skb_shared(skb))
479 ++tx_done;
480 dev_kfree_skb_any(skb);
481 }
482
Ben Hutchings55668612008-05-16 21:16:10 +0100483 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100484 netif_tx_unlock_bh(efx->net_dev);
485
486 /* Check TX completion and received packet counts */
487 rx_good = atomic_read(&state->rx_good);
488 rx_bad = atomic_read(&state->rx_bad);
489 if (tx_done != state->packet_count) {
490 /* Don't free the skbs; they will be picked up on TX
491 * overflow or channel teardown.
492 */
493 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
494 "TX completion events in %s loopback test\n",
495 tx_queue->queue, tx_done, state->packet_count,
496 LOOPBACK_MODE(efx));
497 rc = -ETIMEDOUT;
498 /* Allow to fall through so we see the RX errors as well */
499 }
500
501 /* We may always be up to a flush away from our desired packet total */
502 if (rx_good != state->packet_count) {
503 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
504 "received packets in %s loopback test\n",
505 tx_queue->queue, rx_good, state->packet_count,
506 LOOPBACK_MODE(efx));
507 rc = -ETIMEDOUT;
508 /* Fall through */
509 }
510
511 /* Update loopback test structure */
512 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
513 lb_tests->tx_done[tx_queue->queue] += tx_done;
514 lb_tests->rx_good += rx_good;
515 lb_tests->rx_bad += rx_bad;
516
517 return rc;
518}
519
520static int
521efx_test_loopback(struct efx_tx_queue *tx_queue,
522 struct efx_loopback_self_tests *lb_tests)
523{
524 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100525 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100526 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100527
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100528 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100529 /* Determine how many packets to send */
530 state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
531 state->packet_count = min(1 << (i << 2), state->packet_count);
532 state->skbs = kzalloc(sizeof(state->skbs[0]) *
533 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100534 if (!state->skbs)
535 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100536 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100537
538 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
539 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
540 state->packet_count);
541
542 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100543 begin_rc = efx_begin_loopback(tx_queue);
544
545 /* This will normally complete very quickly, but be
546 * prepared to wait up to 100 ms. */
547 msleep(1);
548 if (!efx_poll_loopback(efx)) {
549 msleep(100);
550 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100551 }
552
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100553 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100554 kfree(state->skbs);
555
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100556 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100557 /* Wait a while to ensure there are no packets
558 * floating around after a failure. */
559 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100560 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100561 }
562 }
563
564 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
565 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
566 state->packet_count);
567
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100568 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100569}
570
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800571static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100572 unsigned int loopback_modes)
573{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100574 enum efx_loopback_mode mode;
575 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100576 struct efx_tx_queue *tx_queue;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100577 bool link_up;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100578 int count, rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100579
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100580 /* Set the port loopback_selftest member. From this point on
581 * all received packets will be dropped. Mark the state as
582 * "flushing" so all inflight packets are dropped */
583 state = kzalloc(sizeof(*state), GFP_KERNEL);
584 if (state == NULL)
585 return -ENOMEM;
586 BUG_ON(efx->loopback_selftest);
587 state->flush = true;
588 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100589
590 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100591 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100592 if (!(loopback_modes & (1 << mode)))
593 continue;
594
595 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100596 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100597 efx->loopback_mode = mode;
598 efx_reconfigure_port(efx);
599
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800600 /* Wait for the PHY to signal the link is up. Interrupts
601 * are enabled for PHY's using LASI, otherwise we poll()
602 * quickly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100603 count = 0;
604 do {
605 struct efx_channel *channel = &efx->channel[0];
606
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800607 efx->phy_op->poll(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100608 schedule_timeout_uninterruptible(HZ / 10);
609 if (channel->work_pending)
610 efx_process_channel_now(channel);
611 /* Wait for PHY events to be processed */
612 flush_workqueue(efx->workqueue);
613 rmb();
614
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800615 /* We need both the phy and xaui links to be ok.
616 * rather than relying on the falcon_xmac irq/poll
617 * regime, just poll xaui directly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100618 link_up = efx->link_up;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800619 if (link_up && EFX_IS10G(efx) &&
620 !falcon_xaui_link_ok(efx))
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100621 link_up = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100622
623 } while ((++count < 20) && !link_up);
624
625 /* The link should now be up. If it isn't, there is no point
626 * in attempting a loopback test */
627 if (!link_up) {
628 EFX_ERR(efx, "loopback %s never came up\n",
629 LOOPBACK_MODE(efx));
630 rc = -EIO;
631 goto out;
632 }
633
634 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
635 LOOPBACK_MODE(efx), count);
636
637 /* Test every TX queue */
638 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100639 state->offload_csum = (tx_queue->queue ==
640 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100641 rc = efx_test_loopback(tx_queue,
642 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100643 if (rc)
644 goto out;
645 }
646 }
647
648 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100649 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100650 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100651 efx->loopback_selftest = NULL;
652 wmb();
653 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100654
655 return rc;
656}
657
658/**************************************************************************
659 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800660 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100661 *
662 *************************************************************************/
663
Ben Hutchings2ef30682008-12-26 13:47:04 -0800664int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
665 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100666{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100667 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
668 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000669 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800670 struct ethtool_cmd ecmd;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800671 struct efx_channel *channel;
672 int rc_test = 0, rc_reset = 0, rc;
673
674 /* Online (i.e. non-disruptive) testing
675 * This checks interrupt generation, event delivery and PHY presence. */
676
Ben Hutchings68e7f452009-04-29 08:05:08 +0000677 rc = efx_test_mdio(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800678 if (rc && !rc_test)
679 rc_test = rc;
680
681 rc = efx_test_nvram(efx, tests);
682 if (rc && !rc_test)
683 rc_test = rc;
684
685 rc = efx_test_interrupts(efx, tests);
686 if (rc && !rc_test)
687 rc_test = rc;
688
689 efx_for_each_channel(channel, efx) {
690 rc = efx_test_eventq_irq(channel, tests);
691 if (rc && !rc_test)
692 rc_test = rc;
693 }
694
695 if (rc_test)
696 return rc_test;
697
698 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800699 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800700
701 /* Offline (i.e. disruptive) testing
702 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100703
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100704 /* force the carrier state off so the kernel doesn't transmit during
705 * the loopback test, and the watchdog timeout doesn't fire. Also put
706 * falcon into loopback for the register test.
707 */
708 mutex_lock(&efx->mac_lock);
709 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800710 if (efx->loopback_modes) {
711 /* We need the 312 clock from the PHY to test the XMAC
712 * registers, so move into XGMII loopback if available */
713 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
714 efx->loopback_mode = LOOPBACK_XGMII;
715 else
716 efx->loopback_mode = __ffs(efx->loopback_modes);
717 }
718
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100719 __efx_reconfigure_port(efx);
720 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100721
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100722 /* free up all consumers of SRAM (including all the queues) */
Steve Hodgson4b988282009-01-29 17:50:51 +0000723 efx_reset_down(efx, reset_method, &ecmd);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100724
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100725 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800726 if (rc && !rc_test)
727 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100728
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100729 /* reset the chip to recover from the register test */
Steve Hodgson4b988282009-01-29 17:50:51 +0000730 rc_reset = falcon_reset_hw(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100731
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800732 /* Ensure that the phy is powered and out of loopback
733 * for the bist and loopback tests */
734 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100735 efx->loopback_mode = LOOPBACK_NONE;
736
Steve Hodgson4b988282009-01-29 17:50:51 +0000737 rc = efx_reset_up(efx, reset_method, &ecmd, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800738 if (rc && !rc_reset)
739 rc_reset = rc;
740
741 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100742 EFX_ERR(efx, "Unable to recover from chip test\n");
743 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800744 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100745 }
746
Ben Hutchings17967212008-12-26 13:47:25 -0800747 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800748 if (rc && !rc_test)
749 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100750
Ben Hutchings2ef30682008-12-26 13:47:04 -0800751 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
752 if (rc && !rc_test)
753 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100754
755 /* restore the PHY to the previous state */
756 efx->loopback_mode = loopback_mode;
757 efx->phy_mode = phy_mode;
758 efx->port_inhibited = false;
759 efx_ethtool_set_settings(efx->net_dev, &ecmd);
760
Ben Hutchings2ef30682008-12-26 13:47:04 -0800761 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100762}
763