blob: fa56e2e8e9c59aed0a230ee05e97bc6fa486f7d6 [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{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000116 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100117
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000118 if (efx->type->test_nvram) {
119 rc = efx->type->test_nvram(efx);
120 tests->nvram = rc ? -1 : 1;
121 }
122
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100123 return rc;
124}
125
126static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
127{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000128 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100129
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000130 /* Test register access */
131 if (efx->type->test_registers) {
132 rc = efx->type->test_registers(efx);
133 tests->registers = rc ? -1 : 1;
134 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100135
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100136 return rc;
137}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100138
139/**************************************************************************
140 *
141 * Interrupt and event queue testing
142 *
143 **************************************************************************/
144
145/* Test generation and receipt of interrupts */
146static int efx_test_interrupts(struct efx_nic *efx,
147 struct efx_self_tests *tests)
148{
149 struct efx_channel *channel;
150
151 EFX_LOG(efx, "testing interrupts\n");
152 tests->interrupt = -1;
153
154 /* Reset interrupt flag */
155 efx->last_irq_cpu = -1;
156 smp_wmb();
157
158 /* ACK each interrupting event queue. Receiving an interrupt due to
159 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100160 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100161 if (channel->work_pending)
162 efx_process_channel_now(channel);
163 if (efx->last_irq_cpu >= 0)
164 goto success;
165 }
166
167 falcon_generate_interrupt(efx);
168
169 /* Wait for arrival of test interrupt. */
170 EFX_LOG(efx, "waiting for test interrupt\n");
171 schedule_timeout_uninterruptible(HZ / 10);
172 if (efx->last_irq_cpu >= 0)
173 goto success;
174
175 EFX_ERR(efx, "timed out waiting for interrupt\n");
176 return -ETIMEDOUT;
177
178 success:
Ben Hutchingsc4593022009-11-23 16:08:17 +0000179 EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
180 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100181 tests->interrupt = 1;
182 return 0;
183}
184
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100185/* Test generation and receipt of interrupting events */
186static int efx_test_eventq_irq(struct efx_channel *channel,
187 struct efx_self_tests *tests)
188{
189 unsigned int magic, count;
190
191 /* Channel specific code, limited to 20 bits */
192 magic = (0x00010150 + channel->channel);
193 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
194 channel->channel, magic);
195
196 tests->eventq_dma[channel->channel] = -1;
197 tests->eventq_int[channel->channel] = -1;
198 tests->eventq_poll[channel->channel] = -1;
199
200 /* Reset flag and zero magic word */
201 channel->efx->last_irq_cpu = -1;
202 channel->eventq_magic = 0;
203 smp_wmb();
204
205 falcon_generate_test_event(channel, magic);
206
207 /* Wait for arrival of interrupt */
208 count = 0;
209 do {
210 schedule_timeout_uninterruptible(HZ / 100);
211
212 if (channel->work_pending)
213 efx_process_channel_now(channel);
214
215 if (channel->eventq_magic == magic)
216 goto eventq_ok;
217 } while (++count < 2);
218
219 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
220 channel->channel);
221
222 /* See if interrupt arrived */
223 if (channel->efx->last_irq_cpu >= 0) {
224 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
225 "during event queue test\n", channel->channel,
226 raw_smp_processor_id());
227 tests->eventq_int[channel->channel] = 1;
228 }
229
230 /* Check to see if event was received even if interrupt wasn't */
231 efx_process_channel_now(channel);
232 if (channel->eventq_magic == magic) {
233 EFX_ERR(channel->efx, "channel %d event was generated, but "
234 "failed to trigger an interrupt\n", channel->channel);
235 tests->eventq_dma[channel->channel] = 1;
236 }
237
238 return -ETIMEDOUT;
239 eventq_ok:
240 EFX_LOG(channel->efx, "channel %d event queue passed\n",
241 channel->channel);
242 tests->eventq_dma[channel->channel] = 1;
243 tests->eventq_int[channel->channel] = 1;
244 tests->eventq_poll[channel->channel] = 1;
245 return 0;
246}
247
Ben Hutchings17967212008-12-26 13:47:25 -0800248static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
249 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100250{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100251 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100252
Ben Hutchings17967212008-12-26 13:47:25 -0800253 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100254 return 0;
255
Ben Hutchings17967212008-12-26 13:47:25 -0800256 EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
257 efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
258
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100259 mutex_lock(&efx->mac_lock);
Ben Hutchings17967212008-12-26 13:47:25 -0800260 rc = efx->phy_op->run_tests(efx, tests->phy, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100261 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100262 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100263}
264
265/**************************************************************************
266 *
267 * Loopback testing
268 * NB Only one loopback test can be executing concurrently.
269 *
270 **************************************************************************/
271
272/* Loopback test RX callback
273 * This is called for each received packet during loopback testing.
274 */
275void efx_loopback_rx_packet(struct efx_nic *efx,
276 const char *buf_ptr, int pkt_len)
277{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100278 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100279 struct efx_loopback_payload *received;
280 struct efx_loopback_payload *payload;
281
282 BUG_ON(!buf_ptr);
283
284 /* If we are just flushing, then drop the packet */
285 if ((state == NULL) || state->flush)
286 return;
287
288 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100289
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100290 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100291 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100292 if (state->offload_csum)
293 received->ip.check = payload->ip.check;
294
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100295 /* Check that header exists */
296 if (pkt_len < sizeof(received->header)) {
297 EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
298 "test\n", pkt_len, LOOPBACK_MODE(efx));
299 goto err;
300 }
301
302 /* Check that the ethernet header exists */
303 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
304 EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
305 LOOPBACK_MODE(efx));
306 goto err;
307 }
308
309 /* Check packet length */
310 if (pkt_len != sizeof(*payload)) {
311 EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
312 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
313 LOOPBACK_MODE(efx));
314 goto err;
315 }
316
317 /* Check that IP header matches */
318 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
319 EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
320 LOOPBACK_MODE(efx));
321 goto err;
322 }
323
324 /* Check that msg and padding matches */
325 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
326 EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
327 LOOPBACK_MODE(efx));
328 goto err;
329 }
330
331 /* Check that iteration matches */
332 if (received->iteration != payload->iteration) {
333 EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
334 "%s loopback test\n", ntohs(received->iteration),
335 ntohs(payload->iteration), LOOPBACK_MODE(efx));
336 goto err;
337 }
338
339 /* Increase correct RX count */
340 EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
341 LOOPBACK_MODE(efx));
342
343 atomic_inc(&state->rx_good);
344 return;
345
346 err:
347#ifdef EFX_ENABLE_DEBUG
348 if (atomic_read(&state->rx_bad) == 0) {
349 EFX_ERR(efx, "received packet:\n");
350 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
351 buf_ptr, pkt_len, 0);
352 EFX_ERR(efx, "expected packet:\n");
353 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
354 &state->payload, sizeof(state->payload), 0);
355 }
356#endif
357 atomic_inc(&state->rx_bad);
358}
359
360/* Initialise an efx_selftest_state for a new iteration */
361static void efx_iterate_state(struct efx_nic *efx)
362{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100363 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100364 struct net_device *net_dev = efx->net_dev;
365 struct efx_loopback_payload *payload = &state->payload;
366
367 /* Initialise the layerII header */
368 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
369 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
370 payload->header.h_proto = htons(ETH_P_IP);
371
372 /* saddr set later and used as incrementing count */
373 payload->ip.daddr = htonl(INADDR_LOOPBACK);
374 payload->ip.ihl = 5;
375 payload->ip.check = htons(0xdead);
376 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
377 payload->ip.version = IPVERSION;
378 payload->ip.protocol = IPPROTO_UDP;
379
380 /* Initialise udp header */
381 payload->udp.source = 0;
382 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
383 sizeof(struct iphdr));
384 payload->udp.check = 0; /* checksum ignored */
385
386 /* Fill out payload */
387 payload->iteration = htons(ntohs(payload->iteration) + 1);
388 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
389
390 /* Fill out remaining state members */
391 atomic_set(&state->rx_good, 0);
392 atomic_set(&state->rx_bad, 0);
393 smp_wmb();
394}
395
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100396static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100397{
398 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100399 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100400 struct efx_loopback_payload *payload;
401 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000402 int i;
403 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100404
405 /* Transmit N copies of buffer */
406 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100407 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100408 * transmit completion counting */
409 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
410 if (!skb)
411 return -ENOMEM;
412 state->skbs[i] = skb;
413 skb_get(skb);
414
415 /* Copy the payload in, incrementing the source address to
416 * exercise the rss vectors */
417 payload = ((struct efx_loopback_payload *)
418 skb_put(skb, sizeof(state->payload)));
419 memcpy(payload, &state->payload, sizeof(state->payload));
420 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
421
422 /* Ensure everything we've written is visible to the
423 * interrupt handler. */
424 smp_wmb();
425
Ben Hutchings55668612008-05-16 21:16:10 +0100426 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100427 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000428 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100429 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100430 netif_tx_unlock_bh(efx->net_dev);
431
432 if (rc != NETDEV_TX_OK) {
433 EFX_ERR(efx, "TX queue %d could not transmit packet %d "
434 "of %d in %s loopback test\n", tx_queue->queue,
435 i + 1, state->packet_count, LOOPBACK_MODE(efx));
436
437 /* Defer cleaning up the other skbs for the caller */
438 kfree_skb(skb);
439 return -EPIPE;
440 }
441 }
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
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000569/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
570 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
571 * to delay and retry. Therefore, it's safer to just poll directly. Wait
572 * for link up and any faults to dissipate. */
573static int efx_wait_for_link(struct efx_nic *efx)
574{
575 struct efx_link_state *link_state = &efx->link_state;
576 int count;
577 bool link_up;
578
579 for (count = 0; count < 40; count++) {
580 schedule_timeout_uninterruptible(HZ / 10);
581
582 if (efx->type->monitor != NULL) {
583 mutex_lock(&efx->mac_lock);
584 efx->type->monitor(efx);
585 mutex_unlock(&efx->mac_lock);
586 } else {
587 struct efx_channel *channel = &efx->channel[0];
588 if (channel->work_pending)
589 efx_process_channel_now(channel);
590 }
591
592 mutex_lock(&efx->mac_lock);
593 link_up = link_state->up;
594 if (link_up)
595 link_up = !efx->mac_op->check_fault(efx);
596 mutex_unlock(&efx->mac_lock);
597
598 if (link_up)
599 return 0;
600 }
601
602 return -ETIMEDOUT;
603}
604
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800605static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100606 unsigned int loopback_modes)
607{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100608 enum efx_loopback_mode mode;
609 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100610 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000611 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100612
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100613 /* Set the port loopback_selftest member. From this point on
614 * all received packets will be dropped. Mark the state as
615 * "flushing" so all inflight packets are dropped */
616 state = kzalloc(sizeof(*state), GFP_KERNEL);
617 if (state == NULL)
618 return -ENOMEM;
619 BUG_ON(efx->loopback_selftest);
620 state->flush = true;
621 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100622
623 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100624 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100625 if (!(loopback_modes & (1 << mode)))
626 continue;
627
628 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100629 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000630 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100631 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000632 rc = __efx_reconfigure_port(efx);
633 mutex_unlock(&efx->mac_lock);
634 if (rc) {
635 EFX_ERR(efx, "unable to move into %s loopback\n",
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100636 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100637 goto out;
638 }
639
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000640 rc = efx_wait_for_link(efx);
641 if (rc) {
642 EFX_ERR(efx, "loopback %s never came up\n",
643 LOOPBACK_MODE(efx));
644 goto out;
645 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100646
647 /* Test every TX queue */
648 efx_for_each_tx_queue(tx_queue, efx) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100649 state->offload_csum = (tx_queue->queue ==
650 EFX_TX_QUEUE_OFFLOAD_CSUM);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100651 rc = efx_test_loopback(tx_queue,
652 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100653 if (rc)
654 goto out;
655 }
656 }
657
658 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100659 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100660 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100661 efx->loopback_selftest = NULL;
662 wmb();
663 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100664
665 return rc;
666}
667
668/**************************************************************************
669 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800670 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100671 *
672 *************************************************************************/
673
Ben Hutchings2ef30682008-12-26 13:47:04 -0800674int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
675 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100676{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100677 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
678 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000679 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800680 struct efx_channel *channel;
681 int rc_test = 0, rc_reset = 0, rc;
682
683 /* Online (i.e. non-disruptive) testing
684 * This checks interrupt generation, event delivery and PHY presence. */
685
Ben Hutchings68e7f452009-04-29 08:05:08 +0000686 rc = efx_test_mdio(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800687 if (rc && !rc_test)
688 rc_test = rc;
689
690 rc = efx_test_nvram(efx, tests);
691 if (rc && !rc_test)
692 rc_test = rc;
693
694 rc = efx_test_interrupts(efx, tests);
695 if (rc && !rc_test)
696 rc_test = rc;
697
698 efx_for_each_channel(channel, efx) {
699 rc = efx_test_eventq_irq(channel, tests);
700 if (rc && !rc_test)
701 rc_test = rc;
702 }
703
704 if (rc_test)
705 return rc_test;
706
707 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800708 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800709
710 /* Offline (i.e. disruptive) testing
711 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100712
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100713 /* force the carrier state off so the kernel doesn't transmit during
714 * the loopback test, and the watchdog timeout doesn't fire. Also put
715 * falcon into loopback for the register test.
716 */
717 mutex_lock(&efx->mac_lock);
718 efx->port_inhibited = true;
Ben Hutchings6f158d52008-12-12 22:00:49 -0800719 if (efx->loopback_modes) {
720 /* We need the 312 clock from the PHY to test the XMAC
721 * registers, so move into XGMII loopback if available */
722 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
723 efx->loopback_mode = LOOPBACK_XGMII;
724 else
725 efx->loopback_mode = __ffs(efx->loopback_modes);
726 }
727
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100728 __efx_reconfigure_port(efx);
729 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100730
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100731 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000732 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100733
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100734 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800735 if (rc && !rc_test)
736 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100737
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100738 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000739 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100740
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800741 /* Ensure that the phy is powered and out of loopback
742 * for the bist and loopback tests */
743 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100744 efx->loopback_mode = LOOPBACK_NONE;
745
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000746 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800747 if (rc && !rc_reset)
748 rc_reset = rc;
749
750 if (rc_reset) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100751 EFX_ERR(efx, "Unable to recover from chip test\n");
752 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800753 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100754 }
755
Ben Hutchings17967212008-12-26 13:47:25 -0800756 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800757 if (rc && !rc_test)
758 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100759
Ben Hutchings2ef30682008-12-26 13:47:04 -0800760 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
761 if (rc && !rc_test)
762 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100763
764 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000765 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100766 efx->phy_mode = phy_mode;
767 efx->port_inhibited = false;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000768 efx->loopback_mode = loopback_mode;
769 __efx_reconfigure_port(efx);
770 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100771
Ben Hutchings2ef30682008-12-26 13:47:04 -0800772 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100773}
774