blob: e4b4c8a3abc669802a61680d5ce6d5a33e0a7664 [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 Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2006-2010 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 "net_driver.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010023#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000024#include "nic.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010025#include "selftest.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#include "workarounds.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010027
Ben Hutchings93e5dfa2012-02-28 20:40:53 +000028/* IRQ latency can be enormous because:
29 * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
30 * slow serial console or an old IDE driver doing error recovery
31 * - The PREEMPT_RT patches mostly deal with this, but also allow a
32 * tasklet or normal task to be given higher priority than our IRQ
33 * threads
34 * Try to avoid blaming the hardware for this.
35 */
36#define IRQ_TIMEOUT HZ
37
Ben Hutchings3273c2e2008-05-07 13:36:19 +010038/*
39 * Loopback test packet structure
40 *
41 * The self-test should stress every RSS vector, and unfortunately
42 * Falcon only performs RSS on TCP/UDP packets.
43 */
44struct efx_loopback_payload {
45 struct ethhdr header;
46 struct iphdr ip;
47 struct udphdr udp;
48 __be16 iteration;
49 const char msg[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +000050} __packed;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010051
52/* Loopback test source MAC address */
53static const unsigned char payload_source[ETH_ALEN] = {
54 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
55};
56
Julia Lawall94de8032009-12-13 01:41:29 +000057static const char payload_msg[] =
Ben Hutchings3273c2e2008-05-07 13:36:19 +010058 "Hello world! This is an Efx loopback test in progress!";
59
stephen hemmingerd2156972010-10-18 05:27:31 +000060/* Interrupt mode names */
61static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000062static const char *const efx_interrupt_mode_names[] = {
stephen hemmingerd2156972010-10-18 05:27:31 +000063 [EFX_INT_MODE_MSIX] = "MSI-X",
64 [EFX_INT_MODE_MSI] = "MSI",
65 [EFX_INT_MODE_LEGACY] = "legacy",
66};
67#define INT_MODE(efx) \
68 STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
69
Ben Hutchings3273c2e2008-05-07 13:36:19 +010070/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010071 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010072 * @flush: Drop all packets in efx_loopback_rx_packet
73 * @packet_count: Number of packets being used in this test
74 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000075 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010076 * @rx_good: RX good packet count
77 * @rx_bad: RX bad packet count
78 * @payload: Payload used in tests
79 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010080struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010081 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010082 int packet_count;
83 struct sk_buff **skbs;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010084 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010085 atomic_t rx_good;
86 atomic_t rx_bad;
87 struct efx_loopback_payload payload;
88};
89
Ben Hutchings93e5dfa2012-02-28 20:40:53 +000090/* How long to wait for all the packets to arrive (in ms) */
91#define LOOPBACK_TIMEOUT_MS 1000
92
Ben Hutchings3273c2e2008-05-07 13:36:19 +010093/**************************************************************************
94 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010095 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010096 *
97 **************************************************************************/
98
Ben Hutchings4f16c072010-02-03 09:30:50 +000099static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100100{
101 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100102
Ben Hutchings4f16c072010-02-03 09:30:50 +0000103 if (efx->phy_op->test_alive) {
104 rc = efx->phy_op->test_alive(efx);
105 tests->phy_alive = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100106 }
107
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100108 return rc;
109}
110
111static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
112{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000113 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100114
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000115 if (efx->type->test_nvram) {
116 rc = efx->type->test_nvram(efx);
117 tests->nvram = rc ? -1 : 1;
118 }
119
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100120 return rc;
121}
122
123static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
124{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000125 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100126
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000127 /* Test register access */
128 if (efx->type->test_registers) {
129 rc = efx->type->test_registers(efx);
130 tests->registers = rc ? -1 : 1;
131 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100132
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100133 return rc;
134}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100135
136/**************************************************************************
137 *
138 * Interrupt and event queue testing
139 *
140 **************************************************************************/
141
142/* Test generation and receipt of interrupts */
143static int efx_test_interrupts(struct efx_nic *efx,
144 struct efx_self_tests *tests)
145{
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000146 unsigned long timeout, wait;
Ben Hutchings1646a6f2012-01-05 20:14:10 +0000147 int cpu;
148
Ben Hutchings62776d02010-06-23 11:30:07 +0000149 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100150 tests->interrupt = -1;
151
152 /* Reset interrupt flag */
153 efx->last_irq_cpu = -1;
154 smp_wmb();
155
Ben Hutchings152b6a62009-11-29 03:43:56 +0000156 efx_nic_generate_interrupt(efx);
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000157 timeout = jiffies + IRQ_TIMEOUT;
158 wait = 1;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100159
160 /* Wait for arrival of test interrupt. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000161 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000162 do {
163 schedule_timeout_uninterruptible(wait);
164 cpu = ACCESS_ONCE(efx->last_irq_cpu);
165 if (cpu >= 0)
166 goto success;
167 wait *= 2;
168 } while (time_before(jiffies, timeout));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100169
Ben Hutchings62776d02010-06-23 11:30:07 +0000170 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100171 return -ETIMEDOUT;
172
173 success:
Ben Hutchings62776d02010-06-23 11:30:07 +0000174 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
Ben Hutchings1646a6f2012-01-05 20:14:10 +0000175 INT_MODE(efx), cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100176 tests->interrupt = 1;
177 return 0;
178}
179
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100180/* Test generation and receipt of interrupting events */
Ben Hutchingsed74f482012-02-28 20:40:54 +0000181static int efx_test_eventq_irq(struct efx_nic *efx,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100182 struct efx_self_tests *tests)
183{
Ben Hutchingsed74f482012-02-28 20:40:54 +0000184 struct efx_channel *channel;
185 unsigned int read_ptr[EFX_MAX_CHANNELS];
186 unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000187 unsigned long timeout, wait;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100188
Ben Hutchingsed74f482012-02-28 20:40:54 +0000189 BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100190
Ben Hutchingsed74f482012-02-28 20:40:54 +0000191 efx_for_each_channel(channel, efx) {
192 read_ptr[channel->channel] = channel->eventq_read_ptr;
193 set_bit(channel->channel, &dma_pend);
194 set_bit(channel->channel, &int_pend);
195 channel->last_irq_cpu = -1;
196 smp_wmb();
197 efx_nic_generate_test_event(channel);
198 }
199
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000200 timeout = jiffies + IRQ_TIMEOUT;
201 wait = 1;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100202
Ben Hutchingsed74f482012-02-28 20:40:54 +0000203 /* Wait for arrival of interrupts. NAPI processing may or may
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000204 * not complete in time, but we can cope in any case.
205 */
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000206 do {
207 schedule_timeout_uninterruptible(wait);
208
Ben Hutchingsed74f482012-02-28 20:40:54 +0000209 efx_for_each_channel(channel, efx) {
210 napi_disable(&channel->napi_str);
211 if (channel->eventq_read_ptr !=
212 read_ptr[channel->channel]) {
213 set_bit(channel->channel, &napi_ran);
214 clear_bit(channel->channel, &dma_pend);
215 clear_bit(channel->channel, &int_pend);
216 } else {
217 if (efx_nic_event_present(channel))
218 clear_bit(channel->channel, &dma_pend);
219 if (ACCESS_ONCE(channel->last_irq_cpu) >= 0)
220 clear_bit(channel->channel, &int_pend);
221 }
222 napi_enable(&channel->napi_str);
223 efx_nic_eventq_read_ack(channel);
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000224 }
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000225
226 wait *= 2;
Ben Hutchingsed74f482012-02-28 20:40:54 +0000227 } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100228
Ben Hutchingsed74f482012-02-28 20:40:54 +0000229 efx_for_each_channel(channel, efx) {
230 bool dma_seen = !test_bit(channel->channel, &dma_pend);
231 bool int_seen = !test_bit(channel->channel, &int_pend);
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000232
Ben Hutchingsed74f482012-02-28 20:40:54 +0000233 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
234 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
235
236 if (dma_seen && int_seen) {
237 netif_dbg(efx, drv, efx->net_dev,
238 "channel %d event queue passed (with%s NAPI)\n",
239 channel->channel,
240 test_bit(channel->channel, &napi_ran) ?
241 "" : "out");
242 } else {
243 /* Report failure and whether either interrupt or DMA
244 * worked
245 */
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000246 netif_err(efx, drv, efx->net_dev,
Ben Hutchingsed74f482012-02-28 20:40:54 +0000247 "channel %d timed out waiting for event queue\n",
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000248 channel->channel);
Ben Hutchingsed74f482012-02-28 20:40:54 +0000249 if (int_seen)
250 netif_err(efx, drv, efx->net_dev,
251 "channel %d saw interrupt "
252 "during event queue test\n",
253 channel->channel);
254 if (dma_seen)
255 netif_err(efx, drv, efx->net_dev,
256 "channel %d event was generated, but "
257 "failed to trigger an interrupt\n",
258 channel->channel);
259 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100260 }
Ben Hutchingsed74f482012-02-28 20:40:54 +0000261
262 return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100263}
264
Ben Hutchings17967212008-12-26 13:47:25 -0800265static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
266 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100267{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100268 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100269
Ben Hutchings17967212008-12-26 13:47:25 -0800270 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100271 return 0;
272
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100273 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000274 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100275 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100276 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100277}
278
279/**************************************************************************
280 *
281 * Loopback testing
282 * NB Only one loopback test can be executing concurrently.
283 *
284 **************************************************************************/
285
286/* Loopback test RX callback
287 * This is called for each received packet during loopback testing.
288 */
289void efx_loopback_rx_packet(struct efx_nic *efx,
290 const char *buf_ptr, int pkt_len)
291{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100292 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100293 struct efx_loopback_payload *received;
294 struct efx_loopback_payload *payload;
295
296 BUG_ON(!buf_ptr);
297
298 /* If we are just flushing, then drop the packet */
299 if ((state == NULL) || state->flush)
300 return;
301
302 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100303
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100304 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100305 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100306 if (state->offload_csum)
307 received->ip.check = payload->ip.check;
308
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100309 /* Check that header exists */
310 if (pkt_len < sizeof(received->header)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000311 netif_err(efx, drv, efx->net_dev,
312 "saw runt RX packet (length %d) in %s loopback "
313 "test\n", pkt_len, LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100314 goto err;
315 }
316
317 /* Check that the ethernet header exists */
318 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000319 netif_err(efx, drv, efx->net_dev,
320 "saw non-loopback RX packet in %s loopback test\n",
321 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100322 goto err;
323 }
324
325 /* Check packet length */
326 if (pkt_len != sizeof(*payload)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000327 netif_err(efx, drv, efx->net_dev,
328 "saw incorrect RX packet length %d (wanted %d) in "
329 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
330 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100331 goto err;
332 }
333
334 /* Check that IP header matches */
335 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000336 netif_err(efx, drv, efx->net_dev,
337 "saw corrupted IP header in %s loopback test\n",
338 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100339 goto err;
340 }
341
342 /* Check that msg and padding matches */
343 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000344 netif_err(efx, drv, efx->net_dev,
345 "saw corrupted RX packet in %s loopback test\n",
346 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100347 goto err;
348 }
349
350 /* Check that iteration matches */
351 if (received->iteration != payload->iteration) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000352 netif_err(efx, drv, efx->net_dev,
353 "saw RX packet from iteration %d (wanted %d) in "
354 "%s loopback test\n", ntohs(received->iteration),
355 ntohs(payload->iteration), LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100356 goto err;
357 }
358
359 /* Increase correct RX count */
Ben Hutchings62776d02010-06-23 11:30:07 +0000360 netif_vdbg(efx, drv, efx->net_dev,
361 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100362
363 atomic_inc(&state->rx_good);
364 return;
365
366 err:
Ben Hutchings5f3f9d62011-11-04 22:29:14 +0000367#ifdef DEBUG
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100368 if (atomic_read(&state->rx_bad) == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000369 netif_err(efx, drv, efx->net_dev, "received packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100370 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
371 buf_ptr, pkt_len, 0);
Ben Hutchings62776d02010-06-23 11:30:07 +0000372 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100373 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
374 &state->payload, sizeof(state->payload), 0);
375 }
376#endif
377 atomic_inc(&state->rx_bad);
378}
379
380/* Initialise an efx_selftest_state for a new iteration */
381static void efx_iterate_state(struct efx_nic *efx)
382{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100383 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100384 struct net_device *net_dev = efx->net_dev;
385 struct efx_loopback_payload *payload = &state->payload;
386
387 /* Initialise the layerII header */
388 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
389 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
390 payload->header.h_proto = htons(ETH_P_IP);
391
392 /* saddr set later and used as incrementing count */
393 payload->ip.daddr = htonl(INADDR_LOOPBACK);
394 payload->ip.ihl = 5;
395 payload->ip.check = htons(0xdead);
396 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
397 payload->ip.version = IPVERSION;
398 payload->ip.protocol = IPPROTO_UDP;
399
400 /* Initialise udp header */
401 payload->udp.source = 0;
402 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
403 sizeof(struct iphdr));
404 payload->udp.check = 0; /* checksum ignored */
405
406 /* Fill out payload */
407 payload->iteration = htons(ntohs(payload->iteration) + 1);
408 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
409
410 /* Fill out remaining state members */
411 atomic_set(&state->rx_good, 0);
412 atomic_set(&state->rx_bad, 0);
413 smp_wmb();
414}
415
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100416static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100417{
418 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100419 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100420 struct efx_loopback_payload *payload;
421 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000422 int i;
423 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100424
425 /* Transmit N copies of buffer */
426 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100427 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100428 * transmit completion counting */
429 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
430 if (!skb)
431 return -ENOMEM;
432 state->skbs[i] = skb;
433 skb_get(skb);
434
435 /* Copy the payload in, incrementing the source address to
436 * exercise the rss vectors */
437 payload = ((struct efx_loopback_payload *)
438 skb_put(skb, sizeof(state->payload)));
439 memcpy(payload, &state->payload, sizeof(state->payload));
440 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
441
442 /* Ensure everything we've written is visible to the
443 * interrupt handler. */
444 smp_wmb();
445
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000446 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000447 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000448 netif_tx_unlock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100449
450 if (rc != NETDEV_TX_OK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000451 netif_err(efx, drv, efx->net_dev,
452 "TX queue %d could not transmit packet %d of "
453 "%d in %s loopback test\n", tx_queue->queue,
454 i + 1, state->packet_count,
455 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100456
457 /* Defer cleaning up the other skbs for the caller */
458 kfree_skb(skb);
459 return -EPIPE;
460 }
461 }
462
463 return 0;
464}
465
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100466static int efx_poll_loopback(struct efx_nic *efx)
467{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100468 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100469 struct efx_channel *channel;
470
471 /* NAPI polling is not enabled, so process channels
472 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100473 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100474 if (channel->work_pending)
475 efx_process_channel_now(channel);
476 }
477 return atomic_read(&state->rx_good) == state->packet_count;
478}
479
480static int efx_end_loopback(struct efx_tx_queue *tx_queue,
481 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100482{
483 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100484 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100485 struct sk_buff *skb;
486 int tx_done = 0, rx_good, rx_bad;
487 int i, rc = 0;
488
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000489 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100490
491 /* Count the number of tx completions, and decrement the refcnt. Any
492 * skbs not already completed will be free'd when the queue is flushed */
Ben Hutchings9c636ba2012-01-05 17:19:45 +0000493 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100494 skb = state->skbs[i];
495 if (skb && !skb_shared(skb))
496 ++tx_done;
497 dev_kfree_skb_any(skb);
498 }
499
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000500 netif_tx_unlock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100501
502 /* Check TX completion and received packet counts */
503 rx_good = atomic_read(&state->rx_good);
504 rx_bad = atomic_read(&state->rx_bad);
505 if (tx_done != state->packet_count) {
506 /* Don't free the skbs; they will be picked up on TX
507 * overflow or channel teardown.
508 */
Ben Hutchings62776d02010-06-23 11:30:07 +0000509 netif_err(efx, drv, efx->net_dev,
510 "TX queue %d saw only %d out of an expected %d "
511 "TX completion events in %s loopback test\n",
512 tx_queue->queue, tx_done, state->packet_count,
513 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100514 rc = -ETIMEDOUT;
515 /* Allow to fall through so we see the RX errors as well */
516 }
517
518 /* We may always be up to a flush away from our desired packet total */
519 if (rx_good != state->packet_count) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000520 netif_dbg(efx, drv, efx->net_dev,
521 "TX queue %d saw only %d out of an expected %d "
522 "received packets in %s loopback test\n",
523 tx_queue->queue, rx_good, state->packet_count,
524 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525 rc = -ETIMEDOUT;
526 /* Fall through */
527 }
528
529 /* Update loopback test structure */
530 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
531 lb_tests->tx_done[tx_queue->queue] += tx_done;
532 lb_tests->rx_good += rx_good;
533 lb_tests->rx_bad += rx_bad;
534
535 return rc;
536}
537
538static int
539efx_test_loopback(struct efx_tx_queue *tx_queue,
540 struct efx_loopback_self_tests *lb_tests)
541{
542 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100543 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100544 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100545
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100546 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100547 /* Determine how many packets to send */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000548 state->packet_count = efx->txq_entries / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100549 state->packet_count = min(1 << (i << 2), state->packet_count);
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000550 state->skbs = kcalloc(state->packet_count,
551 sizeof(state->skbs[0]), GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100552 if (!state->skbs)
553 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100554 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100555
Ben Hutchings62776d02010-06-23 11:30:07 +0000556 netif_dbg(efx, drv, efx->net_dev,
557 "TX queue %d testing %s loopback with %d packets\n",
558 tx_queue->queue, LOOPBACK_MODE(efx),
559 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100560
561 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100562 begin_rc = efx_begin_loopback(tx_queue);
563
564 /* This will normally complete very quickly, but be
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000565 * prepared to wait much longer. */
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100566 msleep(1);
567 if (!efx_poll_loopback(efx)) {
Ben Hutchings93e5dfa2012-02-28 20:40:53 +0000568 msleep(LOOPBACK_TIMEOUT_MS);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100569 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100570 }
571
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100572 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100573 kfree(state->skbs);
574
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100575 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100576 /* Wait a while to ensure there are no packets
577 * floating around after a failure. */
578 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100579 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100580 }
581 }
582
Ben Hutchings62776d02010-06-23 11:30:07 +0000583 netif_dbg(efx, drv, efx->net_dev,
584 "TX queue %d passed %s loopback test with a burst length "
585 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
586 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100587
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100588 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100589}
590
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000591/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
592 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
593 * to delay and retry. Therefore, it's safer to just poll directly. Wait
594 * for link up and any faults to dissipate. */
595static int efx_wait_for_link(struct efx_nic *efx)
596{
597 struct efx_link_state *link_state = &efx->link_state;
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000598 int count, link_up_count = 0;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000599 bool link_up;
600
601 for (count = 0; count < 40; count++) {
602 schedule_timeout_uninterruptible(HZ / 10);
603
604 if (efx->type->monitor != NULL) {
605 mutex_lock(&efx->mac_lock);
606 efx->type->monitor(efx);
607 mutex_unlock(&efx->mac_lock);
608 } else {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000609 struct efx_channel *channel = efx_get_channel(efx, 0);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000610 if (channel->work_pending)
611 efx_process_channel_now(channel);
612 }
613
614 mutex_lock(&efx->mac_lock);
615 link_up = link_state->up;
616 if (link_up)
Ben Hutchings710b2082011-09-03 00:15:00 +0100617 link_up = !efx->type->check_mac_fault(efx);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000618 mutex_unlock(&efx->mac_lock);
619
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000620 if (link_up) {
621 if (++link_up_count == 2)
622 return 0;
623 } else {
624 link_up_count = 0;
625 }
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000626 }
627
628 return -ETIMEDOUT;
629}
630
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800631static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100632 unsigned int loopback_modes)
633{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100634 enum efx_loopback_mode mode;
635 struct efx_loopback_state *state;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000636 struct efx_channel *channel = efx_get_channel(efx, 0);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100637 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000638 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100639
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100640 /* Set the port loopback_selftest member. From this point on
641 * all received packets will be dropped. Mark the state as
642 * "flushing" so all inflight packets are dropped */
643 state = kzalloc(sizeof(*state), GFP_KERNEL);
644 if (state == NULL)
645 return -ENOMEM;
646 BUG_ON(efx->loopback_selftest);
647 state->flush = true;
648 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100649
650 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100651 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100652 if (!(loopback_modes & (1 << mode)))
653 continue;
654
655 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100656 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000657 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100658 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000659 rc = __efx_reconfigure_port(efx);
660 mutex_unlock(&efx->mac_lock);
661 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000662 netif_err(efx, drv, efx->net_dev,
663 "unable to move into %s loopback\n",
664 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100665 goto out;
666 }
667
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000668 rc = efx_wait_for_link(efx);
669 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000670 netif_err(efx, drv, efx->net_dev,
671 "loopback %s never came up\n",
672 LOOPBACK_MODE(efx));
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000673 goto out;
674 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100675
Ben Hutchings94b274b2011-01-10 21:18:20 +0000676 /* Test all enabled types of TX queue */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000677 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000678 state->offload_csum = (tx_queue->queue &
679 EFX_TXQ_TYPE_OFFLOAD);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100680 rc = efx_test_loopback(tx_queue,
681 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100682 if (rc)
683 goto out;
684 }
685 }
686
687 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100688 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100689 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100690 efx->loopback_selftest = NULL;
691 wmb();
692 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100693
694 return rc;
695}
696
697/**************************************************************************
698 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800699 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100700 *
701 *************************************************************************/
702
Ben Hutchings2ef30682008-12-26 13:47:04 -0800703int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
704 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100705{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100706 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
707 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000708 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800709 int rc_test = 0, rc_reset = 0, rc;
710
711 /* Online (i.e. non-disruptive) testing
712 * This checks interrupt generation, event delivery and PHY presence. */
713
Ben Hutchings4f16c072010-02-03 09:30:50 +0000714 rc = efx_test_phy_alive(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800715 if (rc && !rc_test)
716 rc_test = rc;
717
718 rc = efx_test_nvram(efx, tests);
719 if (rc && !rc_test)
720 rc_test = rc;
721
722 rc = efx_test_interrupts(efx, tests);
723 if (rc && !rc_test)
724 rc_test = rc;
725
Ben Hutchingsed74f482012-02-28 20:40:54 +0000726 rc = efx_test_eventq_irq(efx, tests);
727 if (rc && !rc_test)
728 rc_test = rc;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800729
730 if (rc_test)
731 return rc_test;
732
733 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800734 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800735
736 /* Offline (i.e. disruptive) testing
737 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100738
Ben Hutchingse4abce82011-05-16 18:51:24 +0100739 /* Detach the device so the kernel doesn't transmit during the
740 * loopback test and the watchdog timeout doesn't fire.
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100741 */
Ben Hutchingse4abce82011-05-16 18:51:24 +0100742 netif_device_detach(efx->net_dev);
743
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100744 mutex_lock(&efx->mac_lock);
Ben Hutchings6f158d52008-12-12 22:00:49 -0800745 if (efx->loopback_modes) {
746 /* We need the 312 clock from the PHY to test the XMAC
747 * registers, so move into XGMII loopback if available */
748 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
749 efx->loopback_mode = LOOPBACK_XGMII;
750 else
751 efx->loopback_mode = __ffs(efx->loopback_modes);
752 }
753
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100754 __efx_reconfigure_port(efx);
755 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100756
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100757 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000758 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100759
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100760 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800761 if (rc && !rc_test)
762 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100763
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100764 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000765 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100766
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800767 /* Ensure that the phy is powered and out of loopback
768 * for the bist and loopback tests */
769 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100770 efx->loopback_mode = LOOPBACK_NONE;
771
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000772 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800773 if (rc && !rc_reset)
774 rc_reset = rc;
775
776 if (rc_reset) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000777 netif_err(efx, drv, efx->net_dev,
778 "Unable to recover from chip test\n");
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100779 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800780 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100781 }
782
Ben Hutchings17967212008-12-26 13:47:25 -0800783 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800784 if (rc && !rc_test)
785 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100786
Ben Hutchings2ef30682008-12-26 13:47:04 -0800787 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
788 if (rc && !rc_test)
789 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100790
791 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000792 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100793 efx->phy_mode = phy_mode;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000794 efx->loopback_mode = loopback_mode;
795 __efx_reconfigure_port(efx);
796 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100797
Ben Hutchingse4abce82011-05-16 18:51:24 +0100798 netif_device_attach(efx->net_dev);
Neil Turton9d1aea62011-04-04 13:46:23 +0100799
Ben Hutchings2ef30682008-12-26 13:47:04 -0800800 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100801}
802