blob: 4f4f287f574f80d34e1513aec27fe48077e50172 [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"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010023#include "efx.h"
24#include "falcon.h"
25#include "selftest.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#include "workarounds.h"
Ben Hutchings8c8661e2008-09-01 12:49:02 +010027#include "spi.h"
Ben Hutchings12d00ca2009-10-23 08:30:46 +000028#include "io.h"
Ben Hutchings8c8661e2008-09-01 12:49:02 +010029#include "mdio_10g.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010030
31/*
32 * Loopback test packet structure
33 *
34 * The self-test should stress every RSS vector, and unfortunately
35 * Falcon only performs RSS on TCP/UDP packets.
36 */
37struct efx_loopback_payload {
38 struct ethhdr header;
39 struct iphdr ip;
40 struct udphdr udp;
41 __be16 iteration;
42 const char msg[64];
43} __attribute__ ((packed));
44
45/* Loopback test source MAC address */
46static const unsigned char payload_source[ETH_ALEN] = {
47 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
48};
49
50static const char *payload_msg =
51 "Hello world! This is an Efx loopback test in progress!";
52
53/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010054 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010055 * @flush: Drop all packets in efx_loopback_rx_packet
56 * @packet_count: Number of packets being used in this test
57 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000058 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010059 * @rx_good: RX good packet count
60 * @rx_bad: RX bad packet count
61 * @payload: Payload used in tests
62 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010063struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010064 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010065 int packet_count;
66 struct sk_buff **skbs;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010067 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010068 atomic_t rx_good;
69 atomic_t rx_bad;
70 struct efx_loopback_payload payload;
71};
72
73/**************************************************************************
74 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010075 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010076 *
77 **************************************************************************/
78
Ben Hutchings68e7f452009-04-29 08:05:08 +000079static int efx_test_mdio(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010080{
81 int rc = 0;
Ben Hutchings68e7f452009-04-29 08:05:08 +000082 int devad = __ffs(efx->mdio.mmds);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010083 u16 physid1, physid2;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010084
85 if (efx->phy_type == PHY_TYPE_NONE)
86 return 0;
87
88 mutex_lock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +000089 tests->mdio = -1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010090
Ben Hutchings68e7f452009-04-29 08:05:08 +000091 physid1 = efx_mdio_read(efx, devad, MDIO_DEVID1);
92 physid2 = efx_mdio_read(efx, devad, MDIO_DEVID2);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010093
94 if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
95 (physid2 == 0x0000) || (physid2 == 0xffff)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +000096 EFX_ERR(efx, "no MDIO PHY present with ID %d\n",
97 efx->mdio.prtad);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010098 rc = -EINVAL;
99 goto out;
100 }
101
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800102 if (EFX_IS10G(efx)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +0000103 rc = efx_mdio_check_mmds(efx, efx->phy_op->mmds, 0);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800104 if (rc)
105 goto out;
106 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100107
108out:
109 mutex_unlock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +0000110 tests->mdio = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100111 return rc;
112}
113
114static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
115{
116 int rc;
117
118 rc = falcon_read_nvram(efx, NULL);
119 tests->nvram = rc ? -1 : 1;
120 return rc;
121}
122
123static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
124{
125 int rc;
126
127 /* Not supported on A-series silicon */
128 if (falcon_rev(efx) < FALCON_REV_B0)
129 return 0;
130
131 rc = falcon_test_registers(efx);
132 tests->registers = rc ? -1 : 1;
133 return rc;
134}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100135
136/**************************************************************************
137 *
138 * Interrupt and event queue testing
139 *
140 **************************************************************************/
141
142/* Test generation and receipt of interrupts */
143static int efx_test_interrupts(struct efx_nic *efx,
144 struct efx_self_tests *tests)
145{
146 struct efx_channel *channel;
147
148 EFX_LOG(efx, "testing interrupts\n");
149 tests->interrupt = -1;
150
151 /* Reset interrupt flag */
152 efx->last_irq_cpu = -1;
153 smp_wmb();
154
155 /* ACK each interrupting event queue. Receiving an interrupt due to
156 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100157 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100158 if (channel->work_pending)
159 efx_process_channel_now(channel);
160 if (efx->last_irq_cpu >= 0)
161 goto success;
162 }
163
164 falcon_generate_interrupt(efx);
165
166 /* Wait for arrival of test interrupt. */
167 EFX_LOG(efx, "waiting for test interrupt\n");
168 schedule_timeout_uninterruptible(HZ / 10);
169 if (efx->last_irq_cpu >= 0)
170 goto success;
171
172 EFX_ERR(efx, "timed out waiting for interrupt\n");
173 return -ETIMEDOUT;
174
175 success:
Ben Hutchingsc4593022009-11-23 16:08:17 +0000176 EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
177 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100178 tests->interrupt = 1;
179 return 0;
180}
181
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100182/* Test generation and receipt of interrupting events */
183static int efx_test_eventq_irq(struct efx_channel *channel,
184 struct efx_self_tests *tests)
185{
186 unsigned int magic, count;
187
188 /* Channel specific code, limited to 20 bits */
189 magic = (0x00010150 + channel->channel);
190 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
191 channel->channel, magic);
192
193 tests->eventq_dma[channel->channel] = -1;
194 tests->eventq_int[channel->channel] = -1;
195 tests->eventq_poll[channel->channel] = -1;
196
197 /* Reset flag and zero magic word */
198 channel->efx->last_irq_cpu = -1;
199 channel->eventq_magic = 0;
200 smp_wmb();
201
202 falcon_generate_test_event(channel, magic);
203
204 /* Wait for arrival of interrupt */
205 count = 0;
206 do {
207 schedule_timeout_uninterruptible(HZ / 100);
208
209 if (channel->work_pending)
210 efx_process_channel_now(channel);
211
212 if (channel->eventq_magic == magic)
213 goto eventq_ok;
214 } while (++count < 2);
215
216 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
217 channel->channel);
218
219 /* See if interrupt arrived */
220 if (channel->efx->last_irq_cpu >= 0) {
221 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
222 "during event queue test\n", channel->channel,
223 raw_smp_processor_id());
224 tests->eventq_int[channel->channel] = 1;
225 }
226
227 /* Check to see if event was received even if interrupt wasn't */
228 efx_process_channel_now(channel);
229 if (channel->eventq_magic == magic) {
230 EFX_ERR(channel->efx, "channel %d event was generated, but "
231 "failed to trigger an interrupt\n", channel->channel);
232 tests->eventq_dma[channel->channel] = 1;
233 }
234
235 return -ETIMEDOUT;
236 eventq_ok:
237 EFX_LOG(channel->efx, "channel %d event queue passed\n",
238 channel->channel);
239 tests->eventq_dma[channel->channel] = 1;
240 tests->eventq_int[channel->channel] = 1;
241 tests->eventq_poll[channel->channel] = 1;
242 return 0;
243}
244
Ben Hutchings17967212008-12-26 13:47:25 -0800245static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
246 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100247{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100248 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100249
Ben Hutchings17967212008-12-26 13:47:25 -0800250 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100251 return 0;
252
Ben Hutchings17967212008-12-26 13:47:25 -0800253 EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
254 efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
255
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100256 mutex_lock(&efx->mac_lock);
Ben Hutchings17967212008-12-26 13:47:25 -0800257 rc = efx->phy_op->run_tests(efx, tests->phy, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100258 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100259 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100260}
261
262/**************************************************************************
263 *
264 * Loopback testing
265 * NB Only one loopback test can be executing concurrently.
266 *
267 **************************************************************************/
268
269/* Loopback test RX callback
270 * This is called for each received packet during loopback testing.
271 */
272void efx_loopback_rx_packet(struct efx_nic *efx,
273 const char *buf_ptr, int pkt_len)
274{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100275 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100276 struct efx_loopback_payload *received;
277 struct efx_loopback_payload *payload;
278
279 BUG_ON(!buf_ptr);
280
281 /* If we are just flushing, then drop the packet */
282 if ((state == NULL) || state->flush)
283 return;
284
285 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100286
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100287 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100288 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100289 if (state->offload_csum)
290 received->ip.check = payload->ip.check;
291
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100292 /* Check that header exists */
293 if (pkt_len < sizeof(received->header)) {
294 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
295 "test\n", pkt_len, LOOPBACK_MODE(efx));
296 goto err;
297 }
298
299 /* Check that the ethernet header exists */
300 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
301 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
302 LOOPBACK_MODE(efx));
303 goto err;
304 }
305
306 /* Check packet length */
307 if (pkt_len != sizeof(*payload)) {
308 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
309 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
310 LOOPBACK_MODE(efx));
311 goto err;
312 }
313
314 /* Check that IP header matches */
315 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
316 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
317 LOOPBACK_MODE(efx));
318 goto err;
319 }
320
321 /* Check that msg and padding matches */
322 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
323 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
324 LOOPBACK_MODE(efx));
325 goto err;
326 }
327
328 /* Check that iteration matches */
329 if (received->iteration != payload->iteration) {
330 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
331 "%s loopback test\n", ntohs(received->iteration),
332 ntohs(payload->iteration), LOOPBACK_MODE(efx));
333 goto err;
334 }
335
336 /* Increase correct RX count */
337 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
338 LOOPBACK_MODE(efx));
339
340 atomic_inc(&state->rx_good);
341 return;
342
343 err:
344#ifdef EFX_ENABLE_DEBUG
345 if (atomic_read(&state->rx_bad) == 0) {
346 EFX_ERR(efx, "received packet:\n");
347 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
348 buf_ptr, pkt_len, 0);
349 EFX_ERR(efx, "expected packet:\n");
350 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
351 &state->payload, sizeof(state->payload), 0);
352 }
353#endif
354 atomic_inc(&state->rx_bad);
355}
356
357/* Initialise an efx_selftest_state for a new iteration */
358static void efx_iterate_state(struct efx_nic *efx)
359{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100360 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100361 struct net_device *net_dev = efx->net_dev;
362 struct efx_loopback_payload *payload = &state->payload;
363
364 /* Initialise the layerII header */
365 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
366 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
367 payload->header.h_proto = htons(ETH_P_IP);
368
369 /* saddr set later and used as incrementing count */
370 payload->ip.daddr = htonl(INADDR_LOOPBACK);
371 payload->ip.ihl = 5;
372 payload->ip.check = htons(0xdead);
373 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
374 payload->ip.version = IPVERSION;
375 payload->ip.protocol = IPPROTO_UDP;
376
377 /* Initialise udp header */
378 payload->udp.source = 0;
379 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
380 sizeof(struct iphdr));
381 payload->udp.check = 0; /* checksum ignored */
382
383 /* Fill out payload */
384 payload->iteration = htons(ntohs(payload->iteration) + 1);
385 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
386
387 /* Fill out remaining state members */
388 atomic_set(&state->rx_good, 0);
389 atomic_set(&state->rx_bad, 0);
390 smp_wmb();
391}
392
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100393static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100394{
395 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100396 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100397 struct efx_loopback_payload *payload;
398 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000399 int i;
400 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100401
402 /* Transmit N copies of buffer */
403 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100404 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100405 * transmit completion counting */
406 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
407 if (!skb)
408 return -ENOMEM;
409 state->skbs[i] = skb;
410 skb_get(skb);
411
412 /* Copy the payload in, incrementing the source address to
413 * exercise the rss vectors */
414 payload = ((struct efx_loopback_payload *)
415 skb_put(skb, sizeof(state->payload)));
416 memcpy(payload, &state->payload, sizeof(state->payload));
417 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
418
419 /* Ensure everything we've written is visible to the
420 * interrupt handler. */
421 smp_wmb();
422
Ben Hutchings55668612008-05-16 21:16:10 +0100423 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100424 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000425 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100426 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100427 netif_tx_unlock_bh(efx->net_dev);
428
429 if (rc != NETDEV_TX_OK) {
430 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
431 "of %d in %s loopback test\n", tx_queue->queue,
432 i + 1, state->packet_count, LOOPBACK_MODE(efx));
433
434 /* Defer cleaning up the other skbs for the caller */
435 kfree_skb(skb);
436 return -EPIPE;
437 }
Eric Dumazet28679752009-05-27 19:26:37 +0000438 efx->net_dev->trans_start = jiffies;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100439 }
440
441 return 0;
442}
443
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100444static int efx_poll_loopback(struct efx_nic *efx)
445{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100446 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100447 struct efx_channel *channel;
448
449 /* NAPI polling is not enabled, so process channels
450 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100451 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100452 if (channel->work_pending)
453 efx_process_channel_now(channel);
454 }
455 return atomic_read(&state->rx_good) == state->packet_count;
456}
457
458static int efx_end_loopback(struct efx_tx_queue *tx_queue,
459 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100460{
461 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100462 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100463 struct sk_buff *skb;
464 int tx_done = 0, rx_good, rx_bad;
465 int i, rc = 0;
466
Ben Hutchings55668612008-05-16 21:16:10 +0100467 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100468 netif_tx_lock_bh(efx->net_dev);
469
470 /* Count the number of tx completions, and decrement the refcnt. Any
471 * skbs not already completed will be free'd when the queue is flushed */
472 for (i=0; i < state->packet_count; i++) {
473 skb = state->skbs[i];
474 if (skb && !skb_shared(skb))
475 ++tx_done;
476 dev_kfree_skb_any(skb);
477 }
478
Ben Hutchings55668612008-05-16 21:16:10 +0100479 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100480 netif_tx_unlock_bh(efx->net_dev);
481
482 /* Check TX completion and received packet counts */
483 rx_good = atomic_read(&state->rx_good);
484 rx_bad = atomic_read(&state->rx_bad);
485 if (tx_done != state->packet_count) {
486 /* Don't free the skbs; they will be picked up on TX
487 * overflow or channel teardown.
488 */
489 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
490 "TX completion events in %s loopback test\n",
491 tx_queue->queue, tx_done, state->packet_count,
492 LOOPBACK_MODE(efx));
493 rc = -ETIMEDOUT;
494 /* Allow to fall through so we see the RX errors as well */
495 }
496
497 /* We may always be up to a flush away from our desired packet total */
498 if (rx_good != state->packet_count) {
499 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
500 "received packets in %s loopback test\n",
501 tx_queue->queue, rx_good, state->packet_count,
502 LOOPBACK_MODE(efx));
503 rc = -ETIMEDOUT;
504 /* Fall through */
505 }
506
507 /* Update loopback test structure */
508 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
509 lb_tests->tx_done[tx_queue->queue] += tx_done;
510 lb_tests->rx_good += rx_good;
511 lb_tests->rx_bad += rx_bad;
512
513 return rc;
514}
515
516static int
517efx_test_loopback(struct efx_tx_queue *tx_queue,
518 struct efx_loopback_self_tests *lb_tests)
519{
520 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100521 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100522 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100523
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100524 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525 /* Determine how many packets to send */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000526 state->packet_count = EFX_TXQ_SIZE / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100527 state->packet_count = min(1 << (i << 2), state->packet_count);
528 state->skbs = kzalloc(sizeof(state->skbs[0]) *
529 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100530 if (!state->skbs)
531 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100532 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100533
534 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
535 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
536 state->packet_count);
537
538 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100539 begin_rc = efx_begin_loopback(tx_queue);
540
541 /* This will normally complete very quickly, but be
542 * prepared to wait up to 100 ms. */
543 msleep(1);
544 if (!efx_poll_loopback(efx)) {
545 msleep(100);
546 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100547 }
548
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100549 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100550 kfree(state->skbs);
551
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100552 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100553 /* Wait a while to ensure there are no packets
554 * floating around after a failure. */
555 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100556 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100557 }
558 }
559
560 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
561 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
562 state->packet_count);
563
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100564 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100565}
566
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800567static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100568 unsigned int loopback_modes)
569{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100570 enum efx_loopback_mode mode;
571 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100572 struct efx_tx_queue *tx_queue;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100573 bool link_up;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100574 int count, rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100575
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100576 /* Set the port loopback_selftest member. From this point on
577 * all received packets will be dropped. Mark the state as
578 * "flushing" so all inflight packets are dropped */
579 state = kzalloc(sizeof(*state), GFP_KERNEL);
580 if (state == NULL)
581 return -ENOMEM;
582 BUG_ON(efx->loopback_selftest);
583 state->flush = true;
584 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100585
586 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100587 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100588 if (!(loopback_modes & (1 << mode)))
589 continue;
590
591 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100592 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100593 efx->loopback_mode = mode;
594 efx_reconfigure_port(efx);
595
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800596 /* Wait for the PHY to signal the link is up. Interrupts
597 * are enabled for PHY's using LASI, otherwise we poll()
598 * quickly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100599 count = 0;
600 do {
601 struct efx_channel *channel = &efx->channel[0];
602
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800603 efx->phy_op->poll(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100604 schedule_timeout_uninterruptible(HZ / 10);
605 if (channel->work_pending)
606 efx_process_channel_now(channel);
607 /* Wait for PHY events to be processed */
608 flush_workqueue(efx->workqueue);
609 rmb();
610
Ben Hutchings9007b9f2009-11-25 16:12:01 +0000611 /* We need both the PHY and MAC-PHY links to be OK */
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000612 link_up = efx->link_state.up;
Ben Hutchings9007b9f2009-11-25 16:12:01 +0000613 if (link_up)
614 link_up = !efx->mac_op->check_fault(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100615
616 } while ((++count < 20) && !link_up);
617
618 /* The link should now be up. If it isn't, there is no point
619 * in attempting a loopback test */
620 if (!link_up) {
621 EFX_ERR(efx, "loopback %s never came up\n",
622 LOOPBACK_MODE(efx));
623 rc = -EIO;
624 goto out;
625 }
626
627 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
628 LOOPBACK_MODE(efx), count);
629
630 /* Test every TX queue */
631 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100632 state->offload_csum = (tx_queue->queue ==
633 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100634 rc = efx_test_loopback(tx_queue,
635 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100636 if (rc)
637 goto out;
638 }
639 }
640
641 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100642 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100643 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100644 efx->loopback_selftest = NULL;
645 wmb();
646 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100647
648 return rc;
649}
650
651/**************************************************************************
652 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800653 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100654 *
655 *************************************************************************/
656
Ben Hutchings2ef30682008-12-26 13:47:04 -0800657int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
658 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100659{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100660 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
661 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000662 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800663 struct ethtool_cmd ecmd;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800664 struct efx_channel *channel;
665 int rc_test = 0, rc_reset = 0, rc;
666
667 /* Online (i.e. non-disruptive) testing
668 * This checks interrupt generation, event delivery and PHY presence. */
669
Ben Hutchings68e7f452009-04-29 08:05:08 +0000670 rc = efx_test_mdio(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800671 if (rc && !rc_test)
672 rc_test = rc;
673
674 rc = efx_test_nvram(efx, tests);
675 if (rc && !rc_test)
676 rc_test = rc;
677
678 rc = efx_test_interrupts(efx, tests);
679 if (rc && !rc_test)
680 rc_test = rc;
681
682 efx_for_each_channel(channel, efx) {
683 rc = efx_test_eventq_irq(channel, tests);
684 if (rc && !rc_test)
685 rc_test = rc;
686 }
687
688 if (rc_test)
689 return rc_test;
690
691 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800692 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800693
694 /* Offline (i.e. disruptive) testing
695 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100696
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100697 /* force the carrier state off so the kernel doesn't transmit during
698 * the loopback test, and the watchdog timeout doesn't fire. Also put
699 * falcon into loopback for the register test.
700 */
701 mutex_lock(&efx->mac_lock);
702 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800703 if (efx->loopback_modes) {
704 /* We need the 312 clock from the PHY to test the XMAC
705 * registers, so move into XGMII loopback if available */
706 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
707 efx->loopback_mode = LOOPBACK_XGMII;
708 else
709 efx->loopback_mode = __ffs(efx->loopback_modes);
710 }
711
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100712 __efx_reconfigure_port(efx);
713 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100714
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100715 /* free up all consumers of SRAM (including all the queues) */
Steve Hodgson4b988282009-01-29 17:50:51 +0000716 efx_reset_down(efx, reset_method, &ecmd);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100717
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100718 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800719 if (rc && !rc_test)
720 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100721
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100722 /* reset the chip to recover from the register test */
Steve Hodgson4b988282009-01-29 17:50:51 +0000723 rc_reset = falcon_reset_hw(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100724
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800725 /* Ensure that the phy is powered and out of loopback
726 * for the bist and loopback tests */
727 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100728 efx->loopback_mode = LOOPBACK_NONE;
729
Steve Hodgson4b988282009-01-29 17:50:51 +0000730 rc = efx_reset_up(efx, reset_method, &ecmd, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800731 if (rc && !rc_reset)
732 rc_reset = rc;
733
734 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100735 EFX_ERR(efx, "Unable to recover from chip test\n");
736 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800737 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100738 }
739
Ben Hutchings17967212008-12-26 13:47:25 -0800740 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800741 if (rc && !rc_test)
742 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100743
Ben Hutchings2ef30682008-12-26 13:47:04 -0800744 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
745 if (rc && !rc_test)
746 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100747
748 /* restore the PHY to the previous state */
749 efx->loopback_mode = loopback_mode;
750 efx->phy_mode = phy_mode;
751 efx->port_inhibited = false;
752 efx_ethtool_set_settings(efx->net_dev, &ecmd);
753
Ben Hutchings2ef30682008-12-26 13:47:04 -0800754 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100755}
756