blob: afac1cc6bd2f27d186ee4a7f888895bd2f2be82b [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
58 * @rx_good: RX good packet count
59 * @rx_bad: RX bad packet count
60 * @payload: Payload used in tests
61 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010062struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010063 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010064 int packet_count;
65 struct sk_buff **skbs;
Ben Hutchings60ac1062008-09-01 12:44:59 +010066
67 /* Checksums are being offloaded */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010068 bool offload_csum;
Ben Hutchings60ac1062008-09-01 12:44:59 +010069
Ben Hutchings3273c2e2008-05-07 13:36:19 +010070 atomic_t rx_good;
71 atomic_t rx_bad;
72 struct efx_loopback_payload payload;
73};
74
75/**************************************************************************
76 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010077 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010078 *
79 **************************************************************************/
80
Ben Hutchings68e7f452009-04-29 08:05:08 +000081static int efx_test_mdio(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010082{
83 int rc = 0;
Ben Hutchings68e7f452009-04-29 08:05:08 +000084 int devad = __ffs(efx->mdio.mmds);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010085 u16 physid1, physid2;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010086
87 if (efx->phy_type == PHY_TYPE_NONE)
88 return 0;
89
90 mutex_lock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +000091 tests->mdio = -1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010092
Ben Hutchings68e7f452009-04-29 08:05:08 +000093 physid1 = efx_mdio_read(efx, devad, MDIO_DEVID1);
94 physid2 = efx_mdio_read(efx, devad, MDIO_DEVID2);
Ben Hutchings8c8661e2008-09-01 12:49:02 +010095
96 if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
97 (physid2 == 0x0000) || (physid2 == 0xffff)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +000098 EFX_ERR(efx, "no MDIO PHY present with ID %d\n",
99 efx->mdio.prtad);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100100 rc = -EINVAL;
101 goto out;
102 }
103
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800104 if (EFX_IS10G(efx)) {
Ben Hutchings68e7f452009-04-29 08:05:08 +0000105 rc = efx_mdio_check_mmds(efx, efx->phy_op->mmds, 0);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800106 if (rc)
107 goto out;
108 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100109
110out:
111 mutex_unlock(&efx->mac_lock);
Ben Hutchings68e7f452009-04-29 08:05:08 +0000112 tests->mdio = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100113 return rc;
114}
115
116static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
117{
118 int rc;
119
120 rc = falcon_read_nvram(efx, NULL);
121 tests->nvram = rc ? -1 : 1;
122 return rc;
123}
124
125static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
126{
127 int rc;
128
129 /* Not supported on A-series silicon */
130 if (falcon_rev(efx) < FALCON_REV_B0)
131 return 0;
132
133 rc = falcon_test_registers(efx);
134 tests->registers = rc ? -1 : 1;
135 return rc;
136}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100137
138/**************************************************************************
139 *
140 * Interrupt and event queue testing
141 *
142 **************************************************************************/
143
144/* Test generation and receipt of interrupts */
145static int efx_test_interrupts(struct efx_nic *efx,
146 struct efx_self_tests *tests)
147{
148 struct efx_channel *channel;
149
150 EFX_LOG(efx, "testing interrupts\n");
151 tests->interrupt = -1;
152
153 /* Reset interrupt flag */
154 efx->last_irq_cpu = -1;
155 smp_wmb();
156
157 /* ACK each interrupting event queue. Receiving an interrupt due to
158 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100159 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100160 if (channel->work_pending)
161 efx_process_channel_now(channel);
162 if (efx->last_irq_cpu >= 0)
163 goto success;
164 }
165
166 falcon_generate_interrupt(efx);
167
168 /* Wait for arrival of test interrupt. */
169 EFX_LOG(efx, "waiting for test interrupt\n");
170 schedule_timeout_uninterruptible(HZ / 10);
171 if (efx->last_irq_cpu >= 0)
172 goto success;
173
174 EFX_ERR(efx, "timed out waiting for interrupt\n");
175 return -ETIMEDOUT;
176
177 success:
178 EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
179 efx->interrupt_mode, efx->last_irq_cpu);
180 tests->interrupt = 1;
181 return 0;
182}
183
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100184/* Test generation and receipt of interrupting events */
185static int efx_test_eventq_irq(struct efx_channel *channel,
186 struct efx_self_tests *tests)
187{
188 unsigned int magic, count;
189
190 /* Channel specific code, limited to 20 bits */
191 magic = (0x00010150 + channel->channel);
192 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
193 channel->channel, magic);
194
195 tests->eventq_dma[channel->channel] = -1;
196 tests->eventq_int[channel->channel] = -1;
197 tests->eventq_poll[channel->channel] = -1;
198
199 /* Reset flag and zero magic word */
200 channel->efx->last_irq_cpu = -1;
201 channel->eventq_magic = 0;
202 smp_wmb();
203
204 falcon_generate_test_event(channel, magic);
205
206 /* Wait for arrival of interrupt */
207 count = 0;
208 do {
209 schedule_timeout_uninterruptible(HZ / 100);
210
211 if (channel->work_pending)
212 efx_process_channel_now(channel);
213
214 if (channel->eventq_magic == magic)
215 goto eventq_ok;
216 } while (++count < 2);
217
218 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
219 channel->channel);
220
221 /* See if interrupt arrived */
222 if (channel->efx->last_irq_cpu >= 0) {
223 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
224 "during event queue test\n", channel->channel,
225 raw_smp_processor_id());
226 tests->eventq_int[channel->channel] = 1;
227 }
228
229 /* Check to see if event was received even if interrupt wasn't */
230 efx_process_channel_now(channel);
231 if (channel->eventq_magic == magic) {
232 EFX_ERR(channel->efx, "channel %d event was generated, but "
233 "failed to trigger an interrupt\n", channel->channel);
234 tests->eventq_dma[channel->channel] = 1;
235 }
236
237 return -ETIMEDOUT;
238 eventq_ok:
239 EFX_LOG(channel->efx, "channel %d event queue passed\n",
240 channel->channel);
241 tests->eventq_dma[channel->channel] = 1;
242 tests->eventq_int[channel->channel] = 1;
243 tests->eventq_poll[channel->channel] = 1;
244 return 0;
245}
246
Ben Hutchings17967212008-12-26 13:47:25 -0800247static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
248 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100249{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100250 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100251
Ben Hutchings17967212008-12-26 13:47:25 -0800252 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100253 return 0;
254
Ben Hutchings17967212008-12-26 13:47:25 -0800255 EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
256 efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
257
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100258 mutex_lock(&efx->mac_lock);
Ben Hutchings17967212008-12-26 13:47:25 -0800259 rc = efx->phy_op->run_tests(efx, tests->phy, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100260 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100261 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100262}
263
264/**************************************************************************
265 *
266 * Loopback testing
267 * NB Only one loopback test can be executing concurrently.
268 *
269 **************************************************************************/
270
271/* Loopback test RX callback
272 * This is called for each received packet during loopback testing.
273 */
274void efx_loopback_rx_packet(struct efx_nic *efx,
275 const char *buf_ptr, int pkt_len)
276{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100277 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100278 struct efx_loopback_payload *received;
279 struct efx_loopback_payload *payload;
280
281 BUG_ON(!buf_ptr);
282
283 /* If we are just flushing, then drop the packet */
284 if ((state == NULL) || state->flush)
285 return;
286
287 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100288
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100289 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100290 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100291 if (state->offload_csum)
292 received->ip.check = payload->ip.check;
293
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100294 /* Check that header exists */
295 if (pkt_len < sizeof(received->header)) {
296 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
297 "test\n", pkt_len, LOOPBACK_MODE(efx));
298 goto err;
299 }
300
301 /* Check that the ethernet header exists */
302 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
303 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
304 LOOPBACK_MODE(efx));
305 goto err;
306 }
307
308 /* Check packet length */
309 if (pkt_len != sizeof(*payload)) {
310 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
311 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
312 LOOPBACK_MODE(efx));
313 goto err;
314 }
315
316 /* Check that IP header matches */
317 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
318 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
319 LOOPBACK_MODE(efx));
320 goto err;
321 }
322
323 /* Check that msg and padding matches */
324 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
325 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
326 LOOPBACK_MODE(efx));
327 goto err;
328 }
329
330 /* Check that iteration matches */
331 if (received->iteration != payload->iteration) {
332 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
333 "%s loopback test\n", ntohs(received->iteration),
334 ntohs(payload->iteration), LOOPBACK_MODE(efx));
335 goto err;
336 }
337
338 /* Increase correct RX count */
339 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
340 LOOPBACK_MODE(efx));
341
342 atomic_inc(&state->rx_good);
343 return;
344
345 err:
346#ifdef EFX_ENABLE_DEBUG
347 if (atomic_read(&state->rx_bad) == 0) {
348 EFX_ERR(efx, "received packet:\n");
349 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
350 buf_ptr, pkt_len, 0);
351 EFX_ERR(efx, "expected packet:\n");
352 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
353 &state->payload, sizeof(state->payload), 0);
354 }
355#endif
356 atomic_inc(&state->rx_bad);
357}
358
359/* Initialise an efx_selftest_state for a new iteration */
360static void efx_iterate_state(struct efx_nic *efx)
361{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100362 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100363 struct net_device *net_dev = efx->net_dev;
364 struct efx_loopback_payload *payload = &state->payload;
365
366 /* Initialise the layerII header */
367 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
368 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
369 payload->header.h_proto = htons(ETH_P_IP);
370
371 /* saddr set later and used as incrementing count */
372 payload->ip.daddr = htonl(INADDR_LOOPBACK);
373 payload->ip.ihl = 5;
374 payload->ip.check = htons(0xdead);
375 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
376 payload->ip.version = IPVERSION;
377 payload->ip.protocol = IPPROTO_UDP;
378
379 /* Initialise udp header */
380 payload->udp.source = 0;
381 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
382 sizeof(struct iphdr));
383 payload->udp.check = 0; /* checksum ignored */
384
385 /* Fill out payload */
386 payload->iteration = htons(ntohs(payload->iteration) + 1);
387 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
388
389 /* Fill out remaining state members */
390 atomic_set(&state->rx_good, 0);
391 atomic_set(&state->rx_bad, 0);
392 smp_wmb();
393}
394
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100395static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100396{
397 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100398 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100399 struct efx_loopback_payload *payload;
400 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000401 int i;
402 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100403
404 /* Transmit N copies of buffer */
405 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100406 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100407 * transmit completion counting */
408 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
409 if (!skb)
410 return -ENOMEM;
411 state->skbs[i] = skb;
412 skb_get(skb);
413
414 /* Copy the payload in, incrementing the source address to
415 * exercise the rss vectors */
416 payload = ((struct efx_loopback_payload *)
417 skb_put(skb, sizeof(state->payload)));
418 memcpy(payload, &state->payload, sizeof(state->payload));
419 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
420
421 /* Ensure everything we've written is visible to the
422 * interrupt handler. */
423 smp_wmb();
424
Ben Hutchings55668612008-05-16 21:16:10 +0100425 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100426 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000427 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100428 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100429 netif_tx_unlock_bh(efx->net_dev);
430
431 if (rc != NETDEV_TX_OK) {
432 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
433 "of %d in %s loopback test\n", tx_queue->queue,
434 i + 1, state->packet_count, LOOPBACK_MODE(efx));
435
436 /* Defer cleaning up the other skbs for the caller */
437 kfree_skb(skb);
438 return -EPIPE;
439 }
Eric Dumazet28679752009-05-27 19:26:37 +0000440 efx->net_dev->trans_start = jiffies;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100441 }
442
443 return 0;
444}
445
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100446static int efx_poll_loopback(struct efx_nic *efx)
447{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100448 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100449 struct efx_channel *channel;
450
451 /* NAPI polling is not enabled, so process channels
452 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100453 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100454 if (channel->work_pending)
455 efx_process_channel_now(channel);
456 }
457 return atomic_read(&state->rx_good) == state->packet_count;
458}
459
460static int efx_end_loopback(struct efx_tx_queue *tx_queue,
461 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100462{
463 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100464 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100465 struct sk_buff *skb;
466 int tx_done = 0, rx_good, rx_bad;
467 int i, rc = 0;
468
Ben Hutchings55668612008-05-16 21:16:10 +0100469 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100470 netif_tx_lock_bh(efx->net_dev);
471
472 /* Count the number of tx completions, and decrement the refcnt. Any
473 * skbs not already completed will be free'd when the queue is flushed */
474 for (i=0; i < state->packet_count; i++) {
475 skb = state->skbs[i];
476 if (skb && !skb_shared(skb))
477 ++tx_done;
478 dev_kfree_skb_any(skb);
479 }
480
Ben Hutchings55668612008-05-16 21:16:10 +0100481 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100482 netif_tx_unlock_bh(efx->net_dev);
483
484 /* Check TX completion and received packet counts */
485 rx_good = atomic_read(&state->rx_good);
486 rx_bad = atomic_read(&state->rx_bad);
487 if (tx_done != state->packet_count) {
488 /* Don't free the skbs; they will be picked up on TX
489 * overflow or channel teardown.
490 */
491 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
492 "TX completion events in %s loopback test\n",
493 tx_queue->queue, tx_done, state->packet_count,
494 LOOPBACK_MODE(efx));
495 rc = -ETIMEDOUT;
496 /* Allow to fall through so we see the RX errors as well */
497 }
498
499 /* We may always be up to a flush away from our desired packet total */
500 if (rx_good != state->packet_count) {
501 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
502 "received packets in %s loopback test\n",
503 tx_queue->queue, rx_good, state->packet_count,
504 LOOPBACK_MODE(efx));
505 rc = -ETIMEDOUT;
506 /* Fall through */
507 }
508
509 /* Update loopback test structure */
510 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
511 lb_tests->tx_done[tx_queue->queue] += tx_done;
512 lb_tests->rx_good += rx_good;
513 lb_tests->rx_bad += rx_bad;
514
515 return rc;
516}
517
518static int
519efx_test_loopback(struct efx_tx_queue *tx_queue,
520 struct efx_loopback_self_tests *lb_tests)
521{
522 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100523 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100524 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100526 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100527 /* Determine how many packets to send */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000528 state->packet_count = EFX_TXQ_SIZE / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100529 state->packet_count = min(1 << (i << 2), state->packet_count);
530 state->skbs = kzalloc(sizeof(state->skbs[0]) *
531 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100532 if (!state->skbs)
533 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100534 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100535
536 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
537 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
538 state->packet_count);
539
540 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100541 begin_rc = efx_begin_loopback(tx_queue);
542
543 /* This will normally complete very quickly, but be
544 * prepared to wait up to 100 ms. */
545 msleep(1);
546 if (!efx_poll_loopback(efx)) {
547 msleep(100);
548 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100549 }
550
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100551 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100552 kfree(state->skbs);
553
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100554 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100555 /* Wait a while to ensure there are no packets
556 * floating around after a failure. */
557 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100558 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100559 }
560 }
561
562 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
563 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
564 state->packet_count);
565
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100566 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100567}
568
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800569static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100570 unsigned int loopback_modes)
571{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100572 enum efx_loopback_mode mode;
573 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100574 struct efx_tx_queue *tx_queue;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100575 bool link_up;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100576 int count, rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100577
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100578 /* Set the port loopback_selftest member. From this point on
579 * all received packets will be dropped. Mark the state as
580 * "flushing" so all inflight packets are dropped */
581 state = kzalloc(sizeof(*state), GFP_KERNEL);
582 if (state == NULL)
583 return -ENOMEM;
584 BUG_ON(efx->loopback_selftest);
585 state->flush = true;
586 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100587
588 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100589 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100590 if (!(loopback_modes & (1 << mode)))
591 continue;
592
593 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100594 state->flush = true;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100595 efx->loopback_mode = mode;
596 efx_reconfigure_port(efx);
597
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800598 /* Wait for the PHY to signal the link is up. Interrupts
599 * are enabled for PHY's using LASI, otherwise we poll()
600 * quickly */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100601 count = 0;
602 do {
603 struct efx_channel *channel = &efx->channel[0];
604
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800605 efx->phy_op->poll(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100606 schedule_timeout_uninterruptible(HZ / 10);
607 if (channel->work_pending)
608 efx_process_channel_now(channel);
609 /* Wait for PHY events to be processed */
610 flush_workqueue(efx->workqueue);
611 rmb();
612
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800613 /* We need both the phy and xaui links to be ok.
614 * rather than relying on the falcon_xmac irq/poll
615 * regime, just poll xaui directly */
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000616 link_up = efx->link_state.up;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800617 if (link_up && EFX_IS10G(efx) &&
618 !falcon_xaui_link_ok(efx))
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100619 link_up = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100620
621 } while ((++count < 20) && !link_up);
622
623 /* The link should now be up. If it isn't, there is no point
624 * in attempting a loopback test */
625 if (!link_up) {
626 EFX_ERR(efx, "loopback %s never came up\n",
627 LOOPBACK_MODE(efx));
628 rc = -EIO;
629 goto out;
630 }
631
632 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
633 LOOPBACK_MODE(efx), count);
634
635 /* Test every TX queue */
636 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100637 state->offload_csum = (tx_queue->queue ==
638 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100639 rc = efx_test_loopback(tx_queue,
640 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100641 if (rc)
642 goto out;
643 }
644 }
645
646 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100647 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100648 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100649 efx->loopback_selftest = NULL;
650 wmb();
651 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100652
653 return rc;
654}
655
656/**************************************************************************
657 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800658 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100659 *
660 *************************************************************************/
661
Ben Hutchings2ef30682008-12-26 13:47:04 -0800662int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
663 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100664{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100665 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
666 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000667 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800668 struct ethtool_cmd ecmd;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800669 struct efx_channel *channel;
670 int rc_test = 0, rc_reset = 0, rc;
671
672 /* Online (i.e. non-disruptive) testing
673 * This checks interrupt generation, event delivery and PHY presence. */
674
Ben Hutchings68e7f452009-04-29 08:05:08 +0000675 rc = efx_test_mdio(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800676 if (rc && !rc_test)
677 rc_test = rc;
678
679 rc = efx_test_nvram(efx, tests);
680 if (rc && !rc_test)
681 rc_test = rc;
682
683 rc = efx_test_interrupts(efx, tests);
684 if (rc && !rc_test)
685 rc_test = rc;
686
687 efx_for_each_channel(channel, efx) {
688 rc = efx_test_eventq_irq(channel, tests);
689 if (rc && !rc_test)
690 rc_test = rc;
691 }
692
693 if (rc_test)
694 return rc_test;
695
696 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800697 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800698
699 /* Offline (i.e. disruptive) testing
700 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100701
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100702 /* force the carrier state off so the kernel doesn't transmit during
703 * the loopback test, and the watchdog timeout doesn't fire. Also put
704 * falcon into loopback for the register test.
705 */
706 mutex_lock(&efx->mac_lock);
707 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800708 if (efx->loopback_modes) {
709 /* We need the 312 clock from the PHY to test the XMAC
710 * registers, so move into XGMII loopback if available */
711 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
712 efx->loopback_mode = LOOPBACK_XGMII;
713 else
714 efx->loopback_mode = __ffs(efx->loopback_modes);
715 }
716
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100717 __efx_reconfigure_port(efx);
718 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100719
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100720 /* free up all consumers of SRAM (including all the queues) */
Steve Hodgson4b988282009-01-29 17:50:51 +0000721 efx_reset_down(efx, reset_method, &ecmd);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100722
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100723 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800724 if (rc && !rc_test)
725 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100726
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100727 /* reset the chip to recover from the register test */
Steve Hodgson4b988282009-01-29 17:50:51 +0000728 rc_reset = falcon_reset_hw(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100729
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800730 /* Ensure that the phy is powered and out of loopback
731 * for the bist and loopback tests */
732 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100733 efx->loopback_mode = LOOPBACK_NONE;
734
Steve Hodgson4b988282009-01-29 17:50:51 +0000735 rc = efx_reset_up(efx, reset_method, &ecmd, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800736 if (rc && !rc_reset)
737 rc_reset = rc;
738
739 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100740 EFX_ERR(efx, "Unable to recover from chip test\n");
741 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800742 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100743 }
744
Ben Hutchings17967212008-12-26 13:47:25 -0800745 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800746 if (rc && !rc_test)
747 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100748
Ben Hutchings2ef30682008-12-26 13:47:04 -0800749 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
750 if (rc && !rc_test)
751 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100752
753 /* restore the PHY to the previous state */
754 efx->loopback_mode = loopback_mode;
755 efx->phy_mode = phy_mode;
756 efx->port_inhibited = false;
757 efx_ethtool_set_settings(efx->net_dev, &ecmd);
758
Ben Hutchings2ef30682008-12-26 13:47:04 -0800759 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100760}
761