blob: 7718287ad0bc2353e75172e574ecf752a796883c [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
28/*
29 * Loopback test packet structure
30 *
31 * The self-test should stress every RSS vector, and unfortunately
32 * Falcon only performs RSS on TCP/UDP packets.
33 */
34struct efx_loopback_payload {
35 struct ethhdr header;
36 struct iphdr ip;
37 struct udphdr udp;
38 __be16 iteration;
39 const char msg[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +000040} __packed;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010041
42/* Loopback test source MAC address */
43static const unsigned char payload_source[ETH_ALEN] = {
44 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
45};
46
Julia Lawall94de8032009-12-13 01:41:29 +000047static const char payload_msg[] =
Ben Hutchings3273c2e2008-05-07 13:36:19 +010048 "Hello world! This is an Efx loopback test in progress!";
49
stephen hemmingerd2156972010-10-18 05:27:31 +000050/* Interrupt mode names */
51static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000052static const char *const efx_interrupt_mode_names[] = {
stephen hemmingerd2156972010-10-18 05:27:31 +000053 [EFX_INT_MODE_MSIX] = "MSI-X",
54 [EFX_INT_MODE_MSI] = "MSI",
55 [EFX_INT_MODE_LEGACY] = "legacy",
56};
57#define INT_MODE(efx) \
58 STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
59
Ben Hutchings3273c2e2008-05-07 13:36:19 +010060/**
Ben Hutchings8c8661e2008-09-01 12:49:02 +010061 * efx_loopback_state - persistent state during a loopback selftest
Ben Hutchings3273c2e2008-05-07 13:36:19 +010062 * @flush: Drop all packets in efx_loopback_rx_packet
63 * @packet_count: Number of packets being used in this test
64 * @skbs: An array of skbs transmitted
Ben Hutchingsf30eb232009-11-25 16:12:24 +000065 * @offload_csum: Checksums are being offloaded
Ben Hutchings3273c2e2008-05-07 13:36:19 +010066 * @rx_good: RX good packet count
67 * @rx_bad: RX bad packet count
68 * @payload: Payload used in tests
69 */
Ben Hutchings8c8661e2008-09-01 12:49:02 +010070struct efx_loopback_state {
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010071 bool flush;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010072 int packet_count;
73 struct sk_buff **skbs;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010074 bool offload_csum;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010075 atomic_t rx_good;
76 atomic_t rx_bad;
77 struct efx_loopback_payload payload;
78};
79
80/**************************************************************************
81 *
Ben Hutchings8c8661e2008-09-01 12:49:02 +010082 * MII, NVRAM and register tests
Ben Hutchings3273c2e2008-05-07 13:36:19 +010083 *
84 **************************************************************************/
85
Ben Hutchings4f16c072010-02-03 09:30:50 +000086static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
Ben Hutchings8c8661e2008-09-01 12:49:02 +010087{
88 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010089
Ben Hutchings4f16c072010-02-03 09:30:50 +000090 if (efx->phy_op->test_alive) {
91 rc = efx->phy_op->test_alive(efx);
92 tests->phy_alive = rc ? -1 : 1;
Ben Hutchings8c8661e2008-09-01 12:49:02 +010093 }
94
Ben Hutchings8c8661e2008-09-01 12:49:02 +010095 return rc;
96}
97
98static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
99{
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000100 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100101
Ben Hutchings0aa3fba2009-11-29 03:43:33 +0000102 if (efx->type->test_nvram) {
103 rc = efx->type->test_nvram(efx);
104 tests->nvram = rc ? -1 : 1;
105 }
106
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100107 return rc;
108}
109
110static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
111{
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000112 int rc = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100113
Ben Hutchings9bfc4bb2009-11-29 03:43:23 +0000114 /* Test register access */
115 if (efx->type->test_registers) {
116 rc = efx->type->test_registers(efx);
117 tests->registers = rc ? -1 : 1;
118 }
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100119
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100120 return rc;
121}
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100122
123/**************************************************************************
124 *
125 * Interrupt and event queue testing
126 *
127 **************************************************************************/
128
129/* Test generation and receipt of interrupts */
130static int efx_test_interrupts(struct efx_nic *efx,
131 struct efx_self_tests *tests)
132{
Ben Hutchings62776d02010-06-23 11:30:07 +0000133 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100134 tests->interrupt = -1;
135
136 /* Reset interrupt flag */
137 efx->last_irq_cpu = -1;
138 smp_wmb();
139
Ben Hutchings152b6a62009-11-29 03:43:56 +0000140 efx_nic_generate_interrupt(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100141
142 /* Wait for arrival of test interrupt. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000143 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100144 schedule_timeout_uninterruptible(HZ / 10);
145 if (efx->last_irq_cpu >= 0)
146 goto success;
147
Ben Hutchings62776d02010-06-23 11:30:07 +0000148 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100149 return -ETIMEDOUT;
150
151 success:
Ben Hutchings62776d02010-06-23 11:30:07 +0000152 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
153 INT_MODE(efx),
Ben Hutchingsc4593022009-11-23 16:08:17 +0000154 efx->last_irq_cpu);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100155 tests->interrupt = 1;
156 return 0;
157}
158
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100159/* Test generation and receipt of interrupting events */
160static int efx_test_eventq_irq(struct efx_channel *channel,
161 struct efx_self_tests *tests)
162{
Ben Hutchings62776d02010-06-23 11:30:07 +0000163 struct efx_nic *efx = channel->efx;
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100164 unsigned int read_ptr, count;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100165
166 tests->eventq_dma[channel->channel] = -1;
167 tests->eventq_int[channel->channel] = -1;
168 tests->eventq_poll[channel->channel] = -1;
169
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100170 read_ptr = channel->eventq_read_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100171 channel->efx->last_irq_cpu = -1;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100172 smp_wmb();
173
Steve Hodgsond730dc52010-06-01 11:19:09 +0000174 efx_nic_generate_test_event(channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100175
176 /* Wait for arrival of interrupt */
177 count = 0;
178 do {
179 schedule_timeout_uninterruptible(HZ / 100);
180
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100181 if (ACCESS_ONCE(channel->eventq_read_ptr) != read_ptr)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100182 goto eventq_ok;
183 } while (++count < 2);
184
Ben Hutchings62776d02010-06-23 11:30:07 +0000185 netif_err(efx, drv, efx->net_dev,
186 "channel %d timed out waiting for event queue\n",
187 channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100188
189 /* See if interrupt arrived */
190 if (channel->efx->last_irq_cpu >= 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000191 netif_err(efx, drv, efx->net_dev,
192 "channel %d saw interrupt on CPU%d "
193 "during event queue test\n", channel->channel,
194 raw_smp_processor_id());
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100195 tests->eventq_int[channel->channel] = 1;
196 }
197
198 /* Check to see if event was received even if interrupt wasn't */
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100199 if (efx_nic_event_present(channel)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000200 netif_err(efx, drv, efx->net_dev,
201 "channel %d event was generated, but "
202 "failed to trigger an interrupt\n", channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100203 tests->eventq_dma[channel->channel] = 1;
204 }
205
206 return -ETIMEDOUT;
207 eventq_ok:
Ben Hutchings62776d02010-06-23 11:30:07 +0000208 netif_dbg(efx, drv, efx->net_dev, "channel %d event queue passed\n",
209 channel->channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100210 tests->eventq_dma[channel->channel] = 1;
211 tests->eventq_int[channel->channel] = 1;
212 tests->eventq_poll[channel->channel] = 1;
213 return 0;
214}
215
Ben Hutchings17967212008-12-26 13:47:25 -0800216static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
217 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100218{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100219 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100220
Ben Hutchings17967212008-12-26 13:47:25 -0800221 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100222 return 0;
223
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100224 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000225 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100226 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100227 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100228}
229
230/**************************************************************************
231 *
232 * Loopback testing
233 * NB Only one loopback test can be executing concurrently.
234 *
235 **************************************************************************/
236
237/* Loopback test RX callback
238 * This is called for each received packet during loopback testing.
239 */
240void efx_loopback_rx_packet(struct efx_nic *efx,
241 const char *buf_ptr, int pkt_len)
242{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100243 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100244 struct efx_loopback_payload *received;
245 struct efx_loopback_payload *payload;
246
247 BUG_ON(!buf_ptr);
248
249 /* If we are just flushing, then drop the packet */
250 if ((state == NULL) || state->flush)
251 return;
252
253 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100254
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100255 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100256 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100257 if (state->offload_csum)
258 received->ip.check = payload->ip.check;
259
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100260 /* Check that header exists */
261 if (pkt_len < sizeof(received->header)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000262 netif_err(efx, drv, efx->net_dev,
263 "saw runt RX packet (length %d) in %s loopback "
264 "test\n", pkt_len, LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100265 goto err;
266 }
267
268 /* Check that the ethernet header exists */
269 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000270 netif_err(efx, drv, efx->net_dev,
271 "saw non-loopback RX packet in %s loopback test\n",
272 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100273 goto err;
274 }
275
276 /* Check packet length */
277 if (pkt_len != sizeof(*payload)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000278 netif_err(efx, drv, efx->net_dev,
279 "saw incorrect RX packet length %d (wanted %d) in "
280 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
281 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100282 goto err;
283 }
284
285 /* Check that IP header matches */
286 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000287 netif_err(efx, drv, efx->net_dev,
288 "saw corrupted IP header in %s loopback test\n",
289 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100290 goto err;
291 }
292
293 /* Check that msg and padding matches */
294 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000295 netif_err(efx, drv, efx->net_dev,
296 "saw corrupted RX packet in %s loopback test\n",
297 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100298 goto err;
299 }
300
301 /* Check that iteration matches */
302 if (received->iteration != payload->iteration) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000303 netif_err(efx, drv, efx->net_dev,
304 "saw RX packet from iteration %d (wanted %d) in "
305 "%s loopback test\n", ntohs(received->iteration),
306 ntohs(payload->iteration), LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100307 goto err;
308 }
309
310 /* Increase correct RX count */
Ben Hutchings62776d02010-06-23 11:30:07 +0000311 netif_vdbg(efx, drv, efx->net_dev,
312 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100313
314 atomic_inc(&state->rx_good);
315 return;
316
317 err:
Ben Hutchings5f3f9d62011-11-04 22:29:14 +0000318#ifdef DEBUG
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100319 if (atomic_read(&state->rx_bad) == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000320 netif_err(efx, drv, efx->net_dev, "received packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100321 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
322 buf_ptr, pkt_len, 0);
Ben Hutchings62776d02010-06-23 11:30:07 +0000323 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100324 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
325 &state->payload, sizeof(state->payload), 0);
326 }
327#endif
328 atomic_inc(&state->rx_bad);
329}
330
331/* Initialise an efx_selftest_state for a new iteration */
332static void efx_iterate_state(struct efx_nic *efx)
333{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100334 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100335 struct net_device *net_dev = efx->net_dev;
336 struct efx_loopback_payload *payload = &state->payload;
337
338 /* Initialise the layerII header */
339 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
340 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
341 payload->header.h_proto = htons(ETH_P_IP);
342
343 /* saddr set later and used as incrementing count */
344 payload->ip.daddr = htonl(INADDR_LOOPBACK);
345 payload->ip.ihl = 5;
346 payload->ip.check = htons(0xdead);
347 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
348 payload->ip.version = IPVERSION;
349 payload->ip.protocol = IPPROTO_UDP;
350
351 /* Initialise udp header */
352 payload->udp.source = 0;
353 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
354 sizeof(struct iphdr));
355 payload->udp.check = 0; /* checksum ignored */
356
357 /* Fill out payload */
358 payload->iteration = htons(ntohs(payload->iteration) + 1);
359 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
360
361 /* Fill out remaining state members */
362 atomic_set(&state->rx_good, 0);
363 atomic_set(&state->rx_bad, 0);
364 smp_wmb();
365}
366
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100367static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100368{
369 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100370 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100371 struct efx_loopback_payload *payload;
372 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000373 int i;
374 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100375
376 /* Transmit N copies of buffer */
377 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100378 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100379 * transmit completion counting */
380 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
381 if (!skb)
382 return -ENOMEM;
383 state->skbs[i] = skb;
384 skb_get(skb);
385
386 /* Copy the payload in, incrementing the source address to
387 * exercise the rss vectors */
388 payload = ((struct efx_loopback_payload *)
389 skb_put(skb, sizeof(state->payload)));
390 memcpy(payload, &state->payload, sizeof(state->payload));
391 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
392
393 /* Ensure everything we've written is visible to the
394 * interrupt handler. */
395 smp_wmb();
396
Ben Hutchings55668612008-05-16 21:16:10 +0100397 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100398 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000399 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings55668612008-05-16 21:16:10 +0100400 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100401 netif_tx_unlock_bh(efx->net_dev);
402
403 if (rc != NETDEV_TX_OK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000404 netif_err(efx, drv, efx->net_dev,
405 "TX queue %d could not transmit packet %d of "
406 "%d in %s loopback test\n", tx_queue->queue,
407 i + 1, state->packet_count,
408 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100409
410 /* Defer cleaning up the other skbs for the caller */
411 kfree_skb(skb);
412 return -EPIPE;
413 }
414 }
415
416 return 0;
417}
418
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100419static int efx_poll_loopback(struct efx_nic *efx)
420{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100421 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100422 struct efx_channel *channel;
423
424 /* NAPI polling is not enabled, so process channels
425 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100426 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100427 if (channel->work_pending)
428 efx_process_channel_now(channel);
429 }
430 return atomic_read(&state->rx_good) == state->packet_count;
431}
432
433static int efx_end_loopback(struct efx_tx_queue *tx_queue,
434 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100435{
436 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100437 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100438 struct sk_buff *skb;
439 int tx_done = 0, rx_good, rx_bad;
440 int i, rc = 0;
441
Ben Hutchings55668612008-05-16 21:16:10 +0100442 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100443 netif_tx_lock_bh(efx->net_dev);
444
445 /* Count the number of tx completions, and decrement the refcnt. Any
446 * skbs not already completed will be free'd when the queue is flushed */
Ben Hutchings9c636ba2012-01-05 17:19:45 +0000447 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100448 skb = state->skbs[i];
449 if (skb && !skb_shared(skb))
450 ++tx_done;
451 dev_kfree_skb_any(skb);
452 }
453
Ben Hutchings55668612008-05-16 21:16:10 +0100454 if (efx_dev_registered(efx))
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100455 netif_tx_unlock_bh(efx->net_dev);
456
457 /* Check TX completion and received packet counts */
458 rx_good = atomic_read(&state->rx_good);
459 rx_bad = atomic_read(&state->rx_bad);
460 if (tx_done != state->packet_count) {
461 /* Don't free the skbs; they will be picked up on TX
462 * overflow or channel teardown.
463 */
Ben Hutchings62776d02010-06-23 11:30:07 +0000464 netif_err(efx, drv, efx->net_dev,
465 "TX queue %d saw only %d out of an expected %d "
466 "TX completion events in %s loopback test\n",
467 tx_queue->queue, tx_done, state->packet_count,
468 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100469 rc = -ETIMEDOUT;
470 /* Allow to fall through so we see the RX errors as well */
471 }
472
473 /* We may always be up to a flush away from our desired packet total */
474 if (rx_good != state->packet_count) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000475 netif_dbg(efx, drv, efx->net_dev,
476 "TX queue %d saw only %d out of an expected %d "
477 "received packets in %s loopback test\n",
478 tx_queue->queue, rx_good, state->packet_count,
479 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100480 rc = -ETIMEDOUT;
481 /* Fall through */
482 }
483
484 /* Update loopback test structure */
485 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
486 lb_tests->tx_done[tx_queue->queue] += tx_done;
487 lb_tests->rx_good += rx_good;
488 lb_tests->rx_bad += rx_bad;
489
490 return rc;
491}
492
493static int
494efx_test_loopback(struct efx_tx_queue *tx_queue,
495 struct efx_loopback_self_tests *lb_tests)
496{
497 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100498 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100499 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100500
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100501 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100502 /* Determine how many packets to send */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000503 state->packet_count = efx->txq_entries / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100504 state->packet_count = min(1 << (i << 2), state->packet_count);
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000505 state->skbs = kcalloc(state->packet_count,
506 sizeof(state->skbs[0]), GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100507 if (!state->skbs)
508 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100509 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100510
Ben Hutchings62776d02010-06-23 11:30:07 +0000511 netif_dbg(efx, drv, efx->net_dev,
512 "TX queue %d testing %s loopback with %d packets\n",
513 tx_queue->queue, LOOPBACK_MODE(efx),
514 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100515
516 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100517 begin_rc = efx_begin_loopback(tx_queue);
518
519 /* This will normally complete very quickly, but be
520 * prepared to wait up to 100 ms. */
521 msleep(1);
522 if (!efx_poll_loopback(efx)) {
523 msleep(100);
524 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100525 }
526
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100527 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100528 kfree(state->skbs);
529
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100530 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100531 /* Wait a while to ensure there are no packets
532 * floating around after a failure. */
533 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100534 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100535 }
536 }
537
Ben Hutchings62776d02010-06-23 11:30:07 +0000538 netif_dbg(efx, drv, efx->net_dev,
539 "TX queue %d passed %s loopback test with a burst length "
540 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
541 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100542
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100543 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100544}
545
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000546/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
547 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
548 * to delay and retry. Therefore, it's safer to just poll directly. Wait
549 * for link up and any faults to dissipate. */
550static int efx_wait_for_link(struct efx_nic *efx)
551{
552 struct efx_link_state *link_state = &efx->link_state;
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000553 int count, link_up_count = 0;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000554 bool link_up;
555
556 for (count = 0; count < 40; count++) {
557 schedule_timeout_uninterruptible(HZ / 10);
558
559 if (efx->type->monitor != NULL) {
560 mutex_lock(&efx->mac_lock);
561 efx->type->monitor(efx);
562 mutex_unlock(&efx->mac_lock);
563 } else {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000564 struct efx_channel *channel = efx_get_channel(efx, 0);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000565 if (channel->work_pending)
566 efx_process_channel_now(channel);
567 }
568
569 mutex_lock(&efx->mac_lock);
570 link_up = link_state->up;
571 if (link_up)
Ben Hutchings710b2082011-09-03 00:15:00 +0100572 link_up = !efx->type->check_mac_fault(efx);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000573 mutex_unlock(&efx->mac_lock);
574
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000575 if (link_up) {
576 if (++link_up_count == 2)
577 return 0;
578 } else {
579 link_up_count = 0;
580 }
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000581 }
582
583 return -ETIMEDOUT;
584}
585
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800586static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100587 unsigned int loopback_modes)
588{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100589 enum efx_loopback_mode mode;
590 struct efx_loopback_state *state;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000591 struct efx_channel *channel = efx_get_channel(efx, 0);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100592 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000593 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100594
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100595 /* Set the port loopback_selftest member. From this point on
596 * all received packets will be dropped. Mark the state as
597 * "flushing" so all inflight packets are dropped */
598 state = kzalloc(sizeof(*state), GFP_KERNEL);
599 if (state == NULL)
600 return -ENOMEM;
601 BUG_ON(efx->loopback_selftest);
602 state->flush = true;
603 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100604
605 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100606 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100607 if (!(loopback_modes & (1 << mode)))
608 continue;
609
610 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100611 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000612 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100613 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000614 rc = __efx_reconfigure_port(efx);
615 mutex_unlock(&efx->mac_lock);
616 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000617 netif_err(efx, drv, efx->net_dev,
618 "unable to move into %s loopback\n",
619 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100620 goto out;
621 }
622
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000623 rc = efx_wait_for_link(efx);
624 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000625 netif_err(efx, drv, efx->net_dev,
626 "loopback %s never came up\n",
627 LOOPBACK_MODE(efx));
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000628 goto out;
629 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100630
Ben Hutchings94b274b2011-01-10 21:18:20 +0000631 /* Test all enabled types of TX queue */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000632 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000633 state->offload_csum = (tx_queue->queue &
634 EFX_TXQ_TYPE_OFFLOAD);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100635 rc = efx_test_loopback(tx_queue,
636 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100637 if (rc)
638 goto out;
639 }
640 }
641
642 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100643 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100644 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100645 efx->loopback_selftest = NULL;
646 wmb();
647 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100648
649 return rc;
650}
651
652/**************************************************************************
653 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800654 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100655 *
656 *************************************************************************/
657
Ben Hutchings2ef30682008-12-26 13:47:04 -0800658int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
659 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100660{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100661 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
662 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000663 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800664 struct efx_channel *channel;
665 int rc_test = 0, rc_reset = 0, rc;
666
667 /* Online (i.e. non-disruptive) testing
668 * This checks interrupt generation, event delivery and PHY presence. */
669
Ben Hutchings4f16c072010-02-03 09:30:50 +0000670 rc = efx_test_phy_alive(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800671 if (rc && !rc_test)
672 rc_test = rc;
673
674 rc = efx_test_nvram(efx, tests);
675 if (rc && !rc_test)
676 rc_test = rc;
677
678 rc = efx_test_interrupts(efx, tests);
679 if (rc && !rc_test)
680 rc_test = rc;
681
682 efx_for_each_channel(channel, efx) {
683 rc = efx_test_eventq_irq(channel, tests);
684 if (rc && !rc_test)
685 rc_test = rc;
686 }
687
688 if (rc_test)
689 return rc_test;
690
691 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800692 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800693
694 /* Offline (i.e. disruptive) testing
695 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100696
Ben Hutchingse4abce82011-05-16 18:51:24 +0100697 /* Detach the device so the kernel doesn't transmit during the
698 * loopback test and the watchdog timeout doesn't fire.
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100699 */
Ben Hutchingse4abce82011-05-16 18:51:24 +0100700 netif_device_detach(efx->net_dev);
701
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100702 mutex_lock(&efx->mac_lock);
Ben Hutchings6f158d52008-12-12 22:00:49 -0800703 if (efx->loopback_modes) {
704 /* We need the 312 clock from the PHY to test the XMAC
705 * registers, so move into XGMII loopback if available */
706 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
707 efx->loopback_mode = LOOPBACK_XGMII;
708 else
709 efx->loopback_mode = __ffs(efx->loopback_modes);
710 }
711
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100712 __efx_reconfigure_port(efx);
713 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100714
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100715 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000716 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100717
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100718 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800719 if (rc && !rc_test)
720 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100721
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100722 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000723 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100724
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800725 /* Ensure that the phy is powered and out of loopback
726 * for the bist and loopback tests */
727 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100728 efx->loopback_mode = LOOPBACK_NONE;
729
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000730 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800731 if (rc && !rc_reset)
732 rc_reset = rc;
733
734 if (rc_reset) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000735 netif_err(efx, drv, efx->net_dev,
736 "Unable to recover from chip test\n");
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100737 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800738 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100739 }
740
Ben Hutchings17967212008-12-26 13:47:25 -0800741 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800742 if (rc && !rc_test)
743 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100744
Ben Hutchings2ef30682008-12-26 13:47:04 -0800745 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
746 if (rc && !rc_test)
747 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100748
749 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000750 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100751 efx->phy_mode = phy_mode;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000752 efx->loopback_mode = loopback_mode;
753 __efx_reconfigure_port(efx);
754 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100755
Ben Hutchingse4abce82011-05-16 18:51:24 +0100756 netif_device_attach(efx->net_dev);
Neil Turton9d1aea62011-04-04 13:46:23 +0100757
Ben Hutchings2ef30682008-12-26 13:47:04 -0800758 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100759}
760