blob: 85f015f005d5c12afecdf1a1db7fe91fbdd8c3f1 [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.
Ben Hutchings906bb262009-11-29 15:16:19 +00004 * Copyright 2006-2009 Solarflare Communications Inc.
Ben Hutchings3273c2e2008-05-07 13:36:19 +01005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ben Hutchings3273c2e2008-05-07 13:36:19 +010022#include <asm/io.h>
23#include "net_driver.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010024#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000025#include "nic.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#include "selftest.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010027#include "workarounds.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010028
29/*
30 * Loopback test packet structure
31 *
32 * The self-test should stress every RSS vector, and unfortunately
33 * Falcon only performs RSS on TCP/UDP packets.
34 */
35struct efx_loopback_payload {
36 struct ethhdr header;
37 struct iphdr ip;
38 struct udphdr udp;
39 __be16 iteration;
40 const char msg[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +000041} __packed;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010042
43/* Loopback test source MAC address */
44static const unsigned char payload_source[ETH_ALEN] = {
45 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
46};
47
Julia Lawall94de8032009-12-13 01:41:29 +000048static const char payload_msg[] =
Ben Hutchings3273c2e2008-05-07 13:36:19 +010049 "Hello world! This is an Efx loopback test in progress!";
50
51/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010052 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010053 * @flush: Drop all packets in efx_loopback_rx_packet
54 * @packet_count: Number of packets being used in this test
55 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000056 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010057 * @rx_good: RX good packet count
58 * @rx_bad: RX bad packet count
59 * @payload: Payload used in tests
60 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010061struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010062 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010063 int packet_count;
64 struct sk_buff **skbs;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010065 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010066 atomic_t rx_good;
67 atomic_t rx_bad;
68 struct efx_loopback_payload payload;
69};
70
71/**************************************************************************
72 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010073 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010074 *
75 **************************************************************************/
76
Ben Hutchings4f16c072010-02-03 09:30:50 +000077static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010078{
79 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010080
Ben Hutchings4f16c072010-02-03 09:30:50 +000081 if (efx->phy_op->test_alive) {
82 rc = efx->phy_op->test_alive(efx);
83 tests->phy_alive = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010084 }
85
Ben Hutchings8c8661e2008-09-01 12:49:02 +010086 return rc;
87}
88
89static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
90{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000091 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010092
Ben Hutchings0aa3fba2009-11-29 03:43:33 +000093 if (efx->type->test_nvram) {
94 rc = efx->type->test_nvram(efx);
95 tests->nvram = rc ? -1 : 1;
96 }
97
Ben Hutchings8c8661e2008-09-01 12:49:02 +010098 return rc;
99}
100
101static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
102{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000103 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100104
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000105 /* Test register access */
106 if (efx->type->test_registers) {
107 rc = efx->type->test_registers(efx);
108 tests->registers = rc ? -1 : 1;
109 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100110
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100111 return rc;
112}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100113
114/**************************************************************************
115 *
116 * Interrupt and event queue testing
117 *
118 **************************************************************************/
119
120/* Test generation and receipt of interrupts */
121static int efx_test_interrupts(struct efx_nic *efx,
122 struct efx_self_tests *tests)
123{
124 struct efx_channel *channel;
125
Ben Hutchings62776d02010-06-23 11:30:07 +0000126 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100127 tests->interrupt = -1;
128
129 /* Reset interrupt flag */
130 efx->last_irq_cpu = -1;
131 smp_wmb();
132
133 /* ACK each interrupting event queue. Receiving an interrupt due to
134 * traffic before a test event is raised is considered a pass */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100135 efx_for_each_channel(channel, efx) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100136 if (channel->work_pending)
137 efx_process_channel_now(channel);
138 if (efx->last_irq_cpu >= 0)
139 goto success;
140 }
141
Ben Hutchings152b6a62009-11-29 03:43:56 +0000142 efx_nic_generate_interrupt(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100143
144 /* Wait for arrival of test interrupt. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000145 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100146 schedule_timeout_uninterruptible(HZ / 10);
147 if (efx->last_irq_cpu >= 0)
148 goto success;
149
Ben Hutchings62776d02010-06-23 11:30:07 +0000150 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100151 return -ETIMEDOUT;
152
153 success:
Ben Hutchings62776d02010-06-23 11:30:07 +0000154 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
155 INT_MODE(efx),
Ben Hutchingsc4593022009-11-23 16:08:17 +0000156 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100157 tests->interrupt = 1;
158 return 0;
159}
160
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100161/* Test generation and receipt of interrupting events */
162static int efx_test_eventq_irq(struct efx_channel *channel,
163 struct efx_self_tests *tests)
164{
Ben Hutchings62776d02010-06-23 11:30:07 +0000165 struct efx_nic *efx = channel->efx;
Steve Hodgsond730dc52010-06-01 11:19:09 +0000166 unsigned int magic_count, count;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100167
168 tests->eventq_dma[channel->channel] = -1;
169 tests->eventq_int[channel->channel] = -1;
170 tests->eventq_poll[channel->channel] = -1;
171
Steve Hodgsond730dc52010-06-01 11:19:09 +0000172 magic_count = channel->magic_count;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100173 channel->efx->last_irq_cpu = -1;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100174 smp_wmb();
175
Steve Hodgsond730dc52010-06-01 11:19:09 +0000176 efx_nic_generate_test_event(channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100177
178 /* Wait for arrival of interrupt */
179 count = 0;
180 do {
181 schedule_timeout_uninterruptible(HZ / 100);
182
183 if (channel->work_pending)
184 efx_process_channel_now(channel);
185
Steve Hodgsond730dc52010-06-01 11:19:09 +0000186 if (channel->magic_count != magic_count)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100187 goto eventq_ok;
188 } while (++count < 2);
189
Ben Hutchings62776d02010-06-23 11:30:07 +0000190 netif_err(efx, drv, efx->net_dev,
191 "channel %d timed out waiting for event queue\n",
192 channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100193
194 /* See if interrupt arrived */
195 if (channel->efx->last_irq_cpu >= 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000196 netif_err(efx, drv, efx->net_dev,
197 "channel %d saw interrupt on CPU%d "
198 "during event queue test\n", channel->channel,
199 raw_smp_processor_id());
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100200 tests->eventq_int[channel->channel] = 1;
201 }
202
203 /* Check to see if event was received even if interrupt wasn't */
204 efx_process_channel_now(channel);
Steve Hodgsond730dc52010-06-01 11:19:09 +0000205 if (channel->magic_count != magic_count) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000206 netif_err(efx, drv, efx->net_dev,
207 "channel %d event was generated, but "
208 "failed to trigger an interrupt\n", channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100209 tests->eventq_dma[channel->channel] = 1;
210 }
211
212 return -ETIMEDOUT;
213 eventq_ok:
Ben Hutchings62776d02010-06-23 11:30:07 +0000214 netif_dbg(efx, drv, efx->net_dev, "channel %d event queue passed\n",
215 channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100216 tests->eventq_dma[channel->channel] = 1;
217 tests->eventq_int[channel->channel] = 1;
218 tests->eventq_poll[channel->channel] = 1;
219 return 0;
220}
221
Ben Hutchings17967212008-12-26 13:47:25 -0800222static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
223 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100224{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100225 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100226
Ben Hutchings17967212008-12-26 13:47:25 -0800227 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100228 return 0;
229
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100230 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000231 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100232 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100233 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100234}
235
236/**************************************************************************
237 *
238 * Loopback testing
239 * NB Only one loopback test can be executing concurrently.
240 *
241 **************************************************************************/
242
243/* Loopback test RX callback
244 * This is called for each received packet during loopback testing.
245 */
246void efx_loopback_rx_packet(struct efx_nic *efx,
247 const char *buf_ptr, int pkt_len)
248{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100249 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100250 struct efx_loopback_payload *received;
251 struct efx_loopback_payload *payload;
252
253 BUG_ON(!buf_ptr);
254
255 /* If we are just flushing, then drop the packet */
256 if ((state == NULL) || state->flush)
257 return;
258
259 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100260
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100261 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100262 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100263 if (state->offload_csum)
264 received->ip.check = payload->ip.check;
265
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100266 /* Check that header exists */
267 if (pkt_len < sizeof(received->header)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000268 netif_err(efx, drv, efx->net_dev,
269 "saw runt RX packet (length %d) in %s loopback "
270 "test\n", pkt_len, LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100271 goto err;
272 }
273
274 /* Check that the ethernet header exists */
275 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000276 netif_err(efx, drv, efx->net_dev,
277 "saw non-loopback RX packet in %s loopback test\n",
278 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100279 goto err;
280 }
281
282 /* Check packet length */
283 if (pkt_len != sizeof(*payload)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000284 netif_err(efx, drv, efx->net_dev,
285 "saw incorrect RX packet length %d (wanted %d) in "
286 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
287 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100288 goto err;
289 }
290
291 /* Check that IP header matches */
292 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000293 netif_err(efx, drv, efx->net_dev,
294 "saw corrupted IP header in %s loopback test\n",
295 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100296 goto err;
297 }
298
299 /* Check that msg and padding matches */
300 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000301 netif_err(efx, drv, efx->net_dev,
302 "saw corrupted RX packet in %s loopback test\n",
303 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100304 goto err;
305 }
306
307 /* Check that iteration matches */
308 if (received->iteration != payload->iteration) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000309 netif_err(efx, drv, efx->net_dev,
310 "saw RX packet from iteration %d (wanted %d) in "
311 "%s loopback test\n", ntohs(received->iteration),
312 ntohs(payload->iteration), LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100313 goto err;
314 }
315
316 /* Increase correct RX count */
Ben Hutchings62776d02010-06-23 11:30:07 +0000317 netif_vdbg(efx, drv, efx->net_dev,
318 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100319
320 atomic_inc(&state->rx_good);
321 return;
322
323 err:
324#ifdef EFX_ENABLE_DEBUG
325 if (atomic_read(&state->rx_bad) == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000326 netif_err(efx, drv, efx->net_dev, "received packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100327 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
328 buf_ptr, pkt_len, 0);
Ben Hutchings62776d02010-06-23 11:30:07 +0000329 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100330 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
331 &state->payload, sizeof(state->payload), 0);
332 }
333#endif
334 atomic_inc(&state->rx_bad);
335}
336
337/* Initialise an efx_selftest_state for a new iteration */
338static void efx_iterate_state(struct efx_nic *efx)
339{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100340 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100341 struct net_device *net_dev = efx->net_dev;
342 struct efx_loopback_payload *payload = &state->payload;
343
344 /* Initialise the layerII header */
345 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
346 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
347 payload->header.h_proto = htons(ETH_P_IP);
348
349 /* saddr set later and used as incrementing count */
350 payload->ip.daddr = htonl(INADDR_LOOPBACK);
351 payload->ip.ihl = 5;
352 payload->ip.check = htons(0xdead);
353 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
354 payload->ip.version = IPVERSION;
355 payload->ip.protocol = IPPROTO_UDP;
356
357 /* Initialise udp header */
358 payload->udp.source = 0;
359 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
360 sizeof(struct iphdr));
361 payload->udp.check = 0; /* checksum ignored */
362
363 /* Fill out payload */
364 payload->iteration = htons(ntohs(payload->iteration) + 1);
365 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
366
367 /* Fill out remaining state members */
368 atomic_set(&state->rx_good, 0);
369 atomic_set(&state->rx_bad, 0);
370 smp_wmb();
371}
372
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100373static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100374{
375 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100376 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100377 struct efx_loopback_payload *payload;
378 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000379 int i;
380 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100381
382 /* Transmit N copies of buffer */
383 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100384 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100385 * transmit completion counting */
386 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
387 if (!skb)
388 return -ENOMEM;
389 state->skbs[i] = skb;
390 skb_get(skb);
391
392 /* Copy the payload in, incrementing the source address to
393 * exercise the rss vectors */
394 payload = ((struct efx_loopback_payload *)
395 skb_put(skb, sizeof(state->payload)));
396 memcpy(payload, &state->payload, sizeof(state->payload));
397 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
398
399 /* Ensure everything we've written is visible to the
400 * interrupt handler. */
401 smp_wmb();
402
Ben Hutchings55668612008-05-16 21:16:10 +0100403 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100404 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000405 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100406 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100407 netif_tx_unlock_bh(efx->net_dev);
408
409 if (rc != NETDEV_TX_OK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000410 netif_err(efx, drv, efx->net_dev,
411 "TX queue %d could not transmit packet %d of "
412 "%d in %s loopback test\n", tx_queue->queue,
413 i + 1, state->packet_count,
414 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100415
416 /* Defer cleaning up the other skbs for the caller */
417 kfree_skb(skb);
418 return -EPIPE;
419 }
420 }
421
422 return 0;
423}
424
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100425static int efx_poll_loopback(struct efx_nic *efx)
426{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100427 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100428 struct efx_channel *channel;
429
430 /* NAPI polling is not enabled, so process channels
431 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100432 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100433 if (channel->work_pending)
434 efx_process_channel_now(channel);
435 }
436 return atomic_read(&state->rx_good) == state->packet_count;
437}
438
439static int efx_end_loopback(struct efx_tx_queue *tx_queue,
440 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100441{
442 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100443 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100444 struct sk_buff *skb;
445 int tx_done = 0, rx_good, rx_bad;
446 int i, rc = 0;
447
Ben Hutchings55668612008-05-16 21:16:10 +0100448 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100449 netif_tx_lock_bh(efx->net_dev);
450
451 /* Count the number of tx completions, and decrement the refcnt. Any
452 * skbs not already completed will be free'd when the queue is flushed */
453 for (i=0; i < state->packet_count; i++) {
454 skb = state->skbs[i];
455 if (skb && !skb_shared(skb))
456 ++tx_done;
457 dev_kfree_skb_any(skb);
458 }
459
Ben Hutchings55668612008-05-16 21:16:10 +0100460 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100461 netif_tx_unlock_bh(efx->net_dev);
462
463 /* Check TX completion and received packet counts */
464 rx_good = atomic_read(&state->rx_good);
465 rx_bad = atomic_read(&state->rx_bad);
466 if (tx_done != state->packet_count) {
467 /* Don't free the skbs; they will be picked up on TX
468 * overflow or channel teardown.
469 */
Ben Hutchings62776d02010-06-23 11:30:07 +0000470 netif_err(efx, drv, efx->net_dev,
471 "TX queue %d saw only %d out of an expected %d "
472 "TX completion events in %s loopback test\n",
473 tx_queue->queue, tx_done, state->packet_count,
474 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100475 rc = -ETIMEDOUT;
476 /* Allow to fall through so we see the RX errors as well */
477 }
478
479 /* We may always be up to a flush away from our desired packet total */
480 if (rx_good != state->packet_count) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000481 netif_dbg(efx, drv, efx->net_dev,
482 "TX queue %d saw only %d out of an expected %d "
483 "received packets in %s loopback test\n",
484 tx_queue->queue, rx_good, state->packet_count,
485 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100486 rc = -ETIMEDOUT;
487 /* Fall through */
488 }
489
490 /* Update loopback test structure */
491 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
492 lb_tests->tx_done[tx_queue->queue] += tx_done;
493 lb_tests->rx_good += rx_good;
494 lb_tests->rx_bad += rx_bad;
495
496 return rc;
497}
498
499static int
500efx_test_loopback(struct efx_tx_queue *tx_queue,
501 struct efx_loopback_self_tests *lb_tests)
502{
503 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100504 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100505 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100506
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100507 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100508 /* Determine how many packets to send */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000509 state->packet_count = EFX_TXQ_SIZE / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100510 state->packet_count = min(1 << (i << 2), state->packet_count);
511 state->skbs = kzalloc(sizeof(state->skbs[0]) *
512 state->packet_count, GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100513 if (!state->skbs)
514 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100515 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100516
Ben Hutchings62776d02010-06-23 11:30:07 +0000517 netif_dbg(efx, drv, efx->net_dev,
518 "TX queue %d testing %s loopback with %d packets\n",
519 tx_queue->queue, LOOPBACK_MODE(efx),
520 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100521
522 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100523 begin_rc = efx_begin_loopback(tx_queue);
524
525 /* This will normally complete very quickly, but be
526 * prepared to wait up to 100 ms. */
527 msleep(1);
528 if (!efx_poll_loopback(efx)) {
529 msleep(100);
530 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100531 }
532
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100533 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100534 kfree(state->skbs);
535
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100536 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100537 /* Wait a while to ensure there are no packets
538 * floating around after a failure. */
539 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100540 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100541 }
542 }
543
Ben Hutchings62776d02010-06-23 11:30:07 +0000544 netif_dbg(efx, drv, efx->net_dev,
545 "TX queue %d passed %s loopback test with a burst length "
546 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
547 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100548
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100549 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100550}
551
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000552/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
553 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
554 * to delay and retry. Therefore, it's safer to just poll directly. Wait
555 * for link up and any faults to dissipate. */
556static int efx_wait_for_link(struct efx_nic *efx)
557{
558 struct efx_link_state *link_state = &efx->link_state;
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000559 int count, link_up_count = 0;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000560 bool link_up;
561
562 for (count = 0; count < 40; count++) {
563 schedule_timeout_uninterruptible(HZ / 10);
564
565 if (efx->type->monitor != NULL) {
566 mutex_lock(&efx->mac_lock);
567 efx->type->monitor(efx);
568 mutex_unlock(&efx->mac_lock);
569 } else {
570 struct efx_channel *channel = &efx->channel[0];
571 if (channel->work_pending)
572 efx_process_channel_now(channel);
573 }
574
575 mutex_lock(&efx->mac_lock);
576 link_up = link_state->up;
577 if (link_up)
578 link_up = !efx->mac_op->check_fault(efx);
579 mutex_unlock(&efx->mac_lock);
580
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000581 if (link_up) {
582 if (++link_up_count == 2)
583 return 0;
584 } else {
585 link_up_count = 0;
586 }
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000587 }
588
589 return -ETIMEDOUT;
590}
591
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800592static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100593 unsigned int loopback_modes)
594{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100595 enum efx_loopback_mode mode;
596 struct efx_loopback_state *state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100597 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000598 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100599
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100600 /* Set the port loopback_selftest member. From this point on
601 * all received packets will be dropped. Mark the state as
602 * "flushing" so all inflight packets are dropped */
603 state = kzalloc(sizeof(*state), GFP_KERNEL);
604 if (state == NULL)
605 return -ENOMEM;
606 BUG_ON(efx->loopback_selftest);
607 state->flush = true;
608 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100609
610 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100611 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100612 if (!(loopback_modes & (1 << mode)))
613 continue;
614
615 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100616 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000617 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100618 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000619 rc = __efx_reconfigure_port(efx);
620 mutex_unlock(&efx->mac_lock);
621 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000622 netif_err(efx, drv, efx->net_dev,
623 "unable to move into %s loopback\n",
624 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100625 goto out;
626 }
627
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000628 rc = efx_wait_for_link(efx);
629 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000630 netif_err(efx, drv, efx->net_dev,
631 "loopback %s never came up\n",
632 LOOPBACK_MODE(efx));
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000633 goto out;
634 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100635
Ben Hutchings5298c372010-04-28 09:30:30 +0000636 /* Test both types of TX queue */
637 efx_for_each_channel_tx_queue(tx_queue, &efx->channel[0]) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000638 state->offload_csum = (tx_queue->queue &
639 EFX_TXQ_TYPE_OFFLOAD);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100640 rc = efx_test_loopback(tx_queue,
641 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100642 if (rc)
643 goto out;
644 }
645 }
646
647 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100648 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100649 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100650 efx->loopback_selftest = NULL;
651 wmb();
652 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100653
654 return rc;
655}
656
657/**************************************************************************
658 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800659 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100660 *
661 *************************************************************************/
662
Ben Hutchings2ef30682008-12-26 13:47:04 -0800663int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
664 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100665{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100666 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
667 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000668 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
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 Hutchings4f16c072010-02-03 09:30:50 +0000675 rc = efx_test_phy_alive(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) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000721 efx_reset_down(efx, reset_method);
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 */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000728 rc_reset = efx->type->reset(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
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000735 rc = efx_reset_up(efx, reset_method, 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 Hutchings62776d02010-06-23 11:30:07 +0000740 netif_err(efx, drv, efx->net_dev,
741 "Unable to recover from chip test\n");
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100742 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800743 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100744 }
745
Ben Hutchings17967212008-12-26 13:47:25 -0800746 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800747 if (rc && !rc_test)
748 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100749
Ben Hutchings2ef30682008-12-26 13:47:04 -0800750 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
751 if (rc && !rc_test)
752 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100753
754 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000755 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100756 efx->phy_mode = phy_mode;
757 efx->port_inhibited = false;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000758 efx->loopback_mode = loopback_mode;
759 __efx_reconfigure_port(efx);
760 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100761
Ben Hutchings2ef30682008-12-26 13:47:04 -0800762 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100763}
764