blob: febe2a9e62117f560e43d87f675cdc5f38cc6204 [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 Hutchings1646a6f32012-01-05 20:14:10 +0000133 int cpu;
134
Ben Hutchings62776d02010-06-23 11:30:07 +0000135 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100136 tests->interrupt = -1;
137
138 /* Reset interrupt flag */
139 efx->last_irq_cpu = -1;
140 smp_wmb();
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);
Ben Hutchings1646a6f32012-01-05 20:14:10 +0000147 cpu = ACCESS_ONCE(efx->last_irq_cpu);
148 if (cpu >= 0)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100149 goto success;
150
Ben Hutchings62776d02010-06-23 11:30:07 +0000151 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100152 return -ETIMEDOUT;
153
154 success:
Ben Hutchings62776d02010-06-23 11:30:07 +0000155 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
Ben Hutchings1646a6f32012-01-05 20:14:10 +0000156 INT_MODE(efx), 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;
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000166 unsigned int read_ptr;
167 bool napi_ran, dma_seen, int_seen;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100168
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100169 read_ptr = channel->eventq_read_ptr;
Ben Hutchings1646a6f32012-01-05 20:14:10 +0000170 channel->last_irq_cpu = -1;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100171 smp_wmb();
172
Steve Hodgsond730dc52010-06-01 11:19:09 +0000173 efx_nic_generate_test_event(channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100174
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000175 /* Wait for arrival of interrupt. NAPI processing may or may
176 * not complete in time, but we can cope in any case.
177 */
178 msleep(10);
179 napi_disable(&channel->napi_str);
180 if (channel->eventq_read_ptr != read_ptr) {
181 napi_ran = true;
182 dma_seen = true;
183 int_seen = true;
184 } else {
185 napi_ran = false;
186 dma_seen = efx_nic_event_present(channel);
Ben Hutchings1646a6f32012-01-05 20:14:10 +0000187 int_seen = ACCESS_ONCE(channel->last_irq_cpu) >= 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100188 }
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000189 napi_enable(&channel->napi_str);
190 efx_nic_eventq_read_ack(channel);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100191
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000192 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
193 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
194
195 if (dma_seen && int_seen) {
196 netif_dbg(efx, drv, efx->net_dev,
197 "channel %d event queue passed (with%s NAPI)\n",
198 channel->channel, napi_ran ? "" : "out");
199 return 0;
200 } else {
201 /* Report failure and whether either interrupt or DMA worked */
Ben Hutchings62776d02010-06-23 11:30:07 +0000202 netif_err(efx, drv, efx->net_dev,
Ben Hutchings0fb53fa2011-11-04 23:06:04 +0000203 "channel %d timed out waiting for event queue\n",
204 channel->channel);
205 if (int_seen)
206 netif_err(efx, drv, efx->net_dev,
207 "channel %d saw interrupt "
208 "during event queue test\n",
209 channel->channel);
210 if (dma_seen)
211 netif_err(efx, drv, efx->net_dev,
212 "channel %d event was generated, but "
213 "failed to trigger an interrupt\n",
214 channel->channel);
215 return -ETIMEDOUT;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100216 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100217}
218
Ben Hutchings17967212008-12-26 13:47:25 -0800219static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
220 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100221{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100222 int rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100223
Ben Hutchings17967212008-12-26 13:47:25 -0800224 if (!efx->phy_op->run_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100225 return 0;
226
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100227 mutex_lock(&efx->mac_lock);
Ben Hutchings4f16c072010-02-03 09:30:50 +0000228 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100229 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100230 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100231}
232
233/**************************************************************************
234 *
235 * Loopback testing
236 * NB Only one loopback test can be executing concurrently.
237 *
238 **************************************************************************/
239
240/* Loopback test RX callback
241 * This is called for each received packet during loopback testing.
242 */
243void efx_loopback_rx_packet(struct efx_nic *efx,
244 const char *buf_ptr, int pkt_len)
245{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100246 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100247 struct efx_loopback_payload *received;
248 struct efx_loopback_payload *payload;
249
250 BUG_ON(!buf_ptr);
251
252 /* If we are just flushing, then drop the packet */
253 if ((state == NULL) || state->flush)
254 return;
255
256 payload = &state->payload;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100257
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100258 received = (struct efx_loopback_payload *) buf_ptr;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100259 received->ip.saddr = payload->ip.saddr;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100260 if (state->offload_csum)
261 received->ip.check = payload->ip.check;
262
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100263 /* Check that header exists */
264 if (pkt_len < sizeof(received->header)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000265 netif_err(efx, drv, efx->net_dev,
266 "saw runt RX packet (length %d) in %s loopback "
267 "test\n", pkt_len, LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100268 goto err;
269 }
270
271 /* Check that the ethernet header exists */
272 if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000273 netif_err(efx, drv, efx->net_dev,
274 "saw non-loopback RX packet in %s loopback test\n",
275 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100276 goto err;
277 }
278
279 /* Check packet length */
280 if (pkt_len != sizeof(*payload)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000281 netif_err(efx, drv, efx->net_dev,
282 "saw incorrect RX packet length %d (wanted %d) in "
283 "%s loopback test\n", pkt_len, (int)sizeof(*payload),
284 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100285 goto err;
286 }
287
288 /* Check that IP header matches */
289 if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000290 netif_err(efx, drv, efx->net_dev,
291 "saw corrupted IP header in %s loopback test\n",
292 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100293 goto err;
294 }
295
296 /* Check that msg and padding matches */
297 if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000298 netif_err(efx, drv, efx->net_dev,
299 "saw corrupted RX packet in %s loopback test\n",
300 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100301 goto err;
302 }
303
304 /* Check that iteration matches */
305 if (received->iteration != payload->iteration) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000306 netif_err(efx, drv, efx->net_dev,
307 "saw RX packet from iteration %d (wanted %d) in "
308 "%s loopback test\n", ntohs(received->iteration),
309 ntohs(payload->iteration), LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100310 goto err;
311 }
312
313 /* Increase correct RX count */
Ben Hutchings62776d02010-06-23 11:30:07 +0000314 netif_vdbg(efx, drv, efx->net_dev,
315 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100316
317 atomic_inc(&state->rx_good);
318 return;
319
320 err:
Ben Hutchings5f3f9d62011-11-04 22:29:14 +0000321#ifdef DEBUG
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100322 if (atomic_read(&state->rx_bad) == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000323 netif_err(efx, drv, efx->net_dev, "received packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100324 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
325 buf_ptr, pkt_len, 0);
Ben Hutchings62776d02010-06-23 11:30:07 +0000326 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100327 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
328 &state->payload, sizeof(state->payload), 0);
329 }
330#endif
331 atomic_inc(&state->rx_bad);
332}
333
334/* Initialise an efx_selftest_state for a new iteration */
335static void efx_iterate_state(struct efx_nic *efx)
336{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100337 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100338 struct net_device *net_dev = efx->net_dev;
339 struct efx_loopback_payload *payload = &state->payload;
340
341 /* Initialise the layerII header */
342 memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
343 memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
344 payload->header.h_proto = htons(ETH_P_IP);
345
346 /* saddr set later and used as incrementing count */
347 payload->ip.daddr = htonl(INADDR_LOOPBACK);
348 payload->ip.ihl = 5;
349 payload->ip.check = htons(0xdead);
350 payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
351 payload->ip.version = IPVERSION;
352 payload->ip.protocol = IPPROTO_UDP;
353
354 /* Initialise udp header */
355 payload->udp.source = 0;
356 payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
357 sizeof(struct iphdr));
358 payload->udp.check = 0; /* checksum ignored */
359
360 /* Fill out payload */
361 payload->iteration = htons(ntohs(payload->iteration) + 1);
362 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
363
364 /* Fill out remaining state members */
365 atomic_set(&state->rx_good, 0);
366 atomic_set(&state->rx_bad, 0);
367 smp_wmb();
368}
369
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100370static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100371{
372 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100373 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100374 struct efx_loopback_payload *payload;
375 struct sk_buff *skb;
Stephen Hemminger613573252009-08-31 19:50:58 +0000376 int i;
377 netdev_tx_t rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100378
379 /* Transmit N copies of buffer */
380 for (i = 0; i < state->packet_count; i++) {
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100381 /* Allocate an skb, holding an extra reference for
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100382 * transmit completion counting */
383 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
384 if (!skb)
385 return -ENOMEM;
386 state->skbs[i] = skb;
387 skb_get(skb);
388
389 /* Copy the payload in, incrementing the source address to
390 * exercise the rss vectors */
391 payload = ((struct efx_loopback_payload *)
392 skb_put(skb, sizeof(state->payload)));
393 memcpy(payload, &state->payload, sizeof(state->payload));
394 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
395
396 /* Ensure everything we've written is visible to the
397 * interrupt handler. */
398 smp_wmb();
399
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000400 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000401 rc = efx_enqueue_skb(tx_queue, skb);
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000402 netif_tx_unlock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100403
404 if (rc != NETDEV_TX_OK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000405 netif_err(efx, drv, efx->net_dev,
406 "TX queue %d could not transmit packet %d of "
407 "%d in %s loopback test\n", tx_queue->queue,
408 i + 1, state->packet_count,
409 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100410
411 /* Defer cleaning up the other skbs for the caller */
412 kfree_skb(skb);
413 return -EPIPE;
414 }
415 }
416
417 return 0;
418}
419
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100420static int efx_poll_loopback(struct efx_nic *efx)
421{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100422 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100423 struct efx_channel *channel;
424
425 /* NAPI polling is not enabled, so process channels
426 * synchronously */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100427 efx_for_each_channel(channel, efx) {
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100428 if (channel->work_pending)
429 efx_process_channel_now(channel);
430 }
431 return atomic_read(&state->rx_good) == state->packet_count;
432}
433
434static int efx_end_loopback(struct efx_tx_queue *tx_queue,
435 struct efx_loopback_self_tests *lb_tests)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100436{
437 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100438 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100439 struct sk_buff *skb;
440 int tx_done = 0, rx_good, rx_bad;
441 int i, rc = 0;
442
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000443 netif_tx_lock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100444
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 Hutchings73ba7b62012-01-09 19:47:08 +0000454 netif_tx_unlock_bh(efx->net_dev);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100455
456 /* Check TX completion and received packet counts */
457 rx_good = atomic_read(&state->rx_good);
458 rx_bad = atomic_read(&state->rx_bad);
459 if (tx_done != state->packet_count) {
460 /* Don't free the skbs; they will be picked up on TX
461 * overflow or channel teardown.
462 */
Ben Hutchings62776d02010-06-23 11:30:07 +0000463 netif_err(efx, drv, efx->net_dev,
464 "TX queue %d saw only %d out of an expected %d "
465 "TX completion events in %s loopback test\n",
466 tx_queue->queue, tx_done, state->packet_count,
467 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100468 rc = -ETIMEDOUT;
469 /* Allow to fall through so we see the RX errors as well */
470 }
471
472 /* We may always be up to a flush away from our desired packet total */
473 if (rx_good != state->packet_count) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000474 netif_dbg(efx, drv, efx->net_dev,
475 "TX queue %d saw only %d out of an expected %d "
476 "received packets in %s loopback test\n",
477 tx_queue->queue, rx_good, state->packet_count,
478 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100479 rc = -ETIMEDOUT;
480 /* Fall through */
481 }
482
483 /* Update loopback test structure */
484 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
485 lb_tests->tx_done[tx_queue->queue] += tx_done;
486 lb_tests->rx_good += rx_good;
487 lb_tests->rx_bad += rx_bad;
488
489 return rc;
490}
491
492static int
493efx_test_loopback(struct efx_tx_queue *tx_queue,
494 struct efx_loopback_self_tests *lb_tests)
495{
496 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100497 struct efx_loopback_state *state = efx->loopback_selftest;
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100498 int i, begin_rc, end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100499
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100500 for (i = 0; i < 3; i++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100501 /* Determine how many packets to send */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000502 state->packet_count = efx->txq_entries / 3;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100503 state->packet_count = min(1 << (i << 2), state->packet_count);
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000504 state->skbs = kcalloc(state->packet_count,
505 sizeof(state->skbs[0]), GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100506 if (!state->skbs)
507 return -ENOMEM;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100508 state->flush = false;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100509
Ben Hutchings62776d02010-06-23 11:30:07 +0000510 netif_dbg(efx, drv, efx->net_dev,
511 "TX queue %d testing %s loopback with %d packets\n",
512 tx_queue->queue, LOOPBACK_MODE(efx),
513 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100514
515 efx_iterate_state(efx);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100516 begin_rc = efx_begin_loopback(tx_queue);
517
518 /* This will normally complete very quickly, but be
519 * prepared to wait up to 100 ms. */
520 msleep(1);
521 if (!efx_poll_loopback(efx)) {
522 msleep(100);
523 efx_poll_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100524 }
525
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100526 end_rc = efx_end_loopback(tx_queue, lb_tests);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100527 kfree(state->skbs);
528
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100529 if (begin_rc || end_rc) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100530 /* Wait a while to ensure there are no packets
531 * floating around after a failure. */
532 schedule_timeout_uninterruptible(HZ / 10);
Ben Hutchingsb9aafb02008-09-01 12:46:33 +0100533 return begin_rc ? begin_rc : end_rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100534 }
535 }
536
Ben Hutchings62776d02010-06-23 11:30:07 +0000537 netif_dbg(efx, drv, efx->net_dev,
538 "TX queue %d passed %s loopback test with a burst length "
539 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
540 state->packet_count);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100541
Ben Hutchingsa0c2c192008-09-01 12:45:08 +0100542 return 0;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100543}
544
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000545/* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
546 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
547 * to delay and retry. Therefore, it's safer to just poll directly. Wait
548 * for link up and any faults to dissipate. */
549static int efx_wait_for_link(struct efx_nic *efx)
550{
551 struct efx_link_state *link_state = &efx->link_state;
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000552 int count, link_up_count = 0;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000553 bool link_up;
554
555 for (count = 0; count < 40; count++) {
556 schedule_timeout_uninterruptible(HZ / 10);
557
558 if (efx->type->monitor != NULL) {
559 mutex_lock(&efx->mac_lock);
560 efx->type->monitor(efx);
561 mutex_unlock(&efx->mac_lock);
562 } else {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000563 struct efx_channel *channel = efx_get_channel(efx, 0);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000564 if (channel->work_pending)
565 efx_process_channel_now(channel);
566 }
567
568 mutex_lock(&efx->mac_lock);
569 link_up = link_state->up;
570 if (link_up)
Ben Hutchings710b2082011-09-03 00:15:00 +0100571 link_up = !efx->type->check_mac_fault(efx);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000572 mutex_unlock(&efx->mac_lock);
573
Steve Hodgson901d3fe2010-06-01 11:18:28 +0000574 if (link_up) {
575 if (++link_up_count == 2)
576 return 0;
577 } else {
578 link_up_count = 0;
579 }
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000580 }
581
582 return -ETIMEDOUT;
583}
584
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800585static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100586 unsigned int loopback_modes)
587{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100588 enum efx_loopback_mode mode;
589 struct efx_loopback_state *state;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000590 struct efx_channel *channel = efx_get_channel(efx, 0);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100591 struct efx_tx_queue *tx_queue;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000592 int rc = 0;
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100593
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100594 /* Set the port loopback_selftest member. From this point on
595 * all received packets will be dropped. Mark the state as
596 * "flushing" so all inflight packets are dropped */
597 state = kzalloc(sizeof(*state), GFP_KERNEL);
598 if (state == NULL)
599 return -ENOMEM;
600 BUG_ON(efx->loopback_selftest);
601 state->flush = true;
602 efx->loopback_selftest = state;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100603
604 /* Test all supported loopback modes */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100605 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100606 if (!(loopback_modes & (1 << mode)))
607 continue;
608
609 /* Move the port into the specified loopback mode. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100610 state->flush = true;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000611 mutex_lock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100612 efx->loopback_mode = mode;
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000613 rc = __efx_reconfigure_port(efx);
614 mutex_unlock(&efx->mac_lock);
615 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000616 netif_err(efx, drv, efx->net_dev,
617 "unable to move into %s loopback\n",
618 LOOPBACK_MODE(efx));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100619 goto out;
620 }
621
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000622 rc = efx_wait_for_link(efx);
623 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000624 netif_err(efx, drv, efx->net_dev,
625 "loopback %s never came up\n",
626 LOOPBACK_MODE(efx));
Steve Hodgson78c1f0a2009-11-29 03:43:00 +0000627 goto out;
628 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100629
Ben Hutchings94b274b2011-01-10 21:18:20 +0000630 /* Test all enabled types of TX queue */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000631 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000632 state->offload_csum = (tx_queue->queue &
633 EFX_TXQ_TYPE_OFFLOAD);
Ben Hutchingsf8ea0b62008-09-01 12:46:28 +0100634 rc = efx_test_loopback(tx_queue,
635 &tests->loopback[mode]);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100636 if (rc)
637 goto out;
638 }
639 }
640
641 out:
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100642 /* Remove the flush. The caller will remove the loopback setting */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100643 state->flush = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100644 efx->loopback_selftest = NULL;
645 wmb();
646 kfree(state);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100647
648 return rc;
649}
650
651/**************************************************************************
652 *
Ben Hutchings2ef30682008-12-26 13:47:04 -0800653 * Entry point
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100654 *
655 *************************************************************************/
656
Ben Hutchings2ef30682008-12-26 13:47:04 -0800657int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
658 unsigned flags)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100659{
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100660 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
661 int phy_mode = efx->phy_mode;
Steve Hodgson4b988282009-01-29 17:50:51 +0000662 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
Ben Hutchings2ef30682008-12-26 13:47:04 -0800663 struct efx_channel *channel;
664 int rc_test = 0, rc_reset = 0, rc;
665
666 /* Online (i.e. non-disruptive) testing
667 * This checks interrupt generation, event delivery and PHY presence. */
668
Ben Hutchings4f16c072010-02-03 09:30:50 +0000669 rc = efx_test_phy_alive(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800670 if (rc && !rc_test)
671 rc_test = rc;
672
673 rc = efx_test_nvram(efx, tests);
674 if (rc && !rc_test)
675 rc_test = rc;
676
677 rc = efx_test_interrupts(efx, tests);
678 if (rc && !rc_test)
679 rc_test = rc;
680
681 efx_for_each_channel(channel, efx) {
682 rc = efx_test_eventq_irq(channel, tests);
683 if (rc && !rc_test)
684 rc_test = rc;
685 }
686
687 if (rc_test)
688 return rc_test;
689
690 if (!(flags & ETH_TEST_FL_OFFLINE))
Ben Hutchings17967212008-12-26 13:47:25 -0800691 return efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800692
693 /* Offline (i.e. disruptive) testing
694 * This checks MAC and PHY loopback on the specified port. */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100695
Ben Hutchingse4abce82011-05-16 18:51:24 +0100696 /* Detach the device so the kernel doesn't transmit during the
697 * loopback test and the watchdog timeout doesn't fire.
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100698 */
Ben Hutchingse4abce82011-05-16 18:51:24 +0100699 netif_device_detach(efx->net_dev);
700
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100701 mutex_lock(&efx->mac_lock);
Ben Hutchings6f158d52008-12-12 22:00:49 -0800702 if (efx->loopback_modes) {
703 /* We need the 312 clock from the PHY to test the XMAC
704 * registers, so move into XGMII loopback if available */
705 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
706 efx->loopback_mode = LOOPBACK_XGMII;
707 else
708 efx->loopback_mode = __ffs(efx->loopback_modes);
709 }
710
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100711 __efx_reconfigure_port(efx);
712 mutex_unlock(&efx->mac_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100713
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100714 /* free up all consumers of SRAM (including all the queues) */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000715 efx_reset_down(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100716
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100717 rc = efx_test_chip(efx, tests);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800718 if (rc && !rc_test)
719 rc_test = rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100720
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100721 /* reset the chip to recover from the register test */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000722 rc_reset = efx->type->reset(efx, reset_method);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100723
Ben Hutchingsa5692e42008-12-26 13:46:38 -0800724 /* Ensure that the phy is powered and out of loopback
725 * for the bist and loopback tests */
726 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100727 efx->loopback_mode = LOOPBACK_NONE;
728
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000729 rc = efx_reset_up(efx, reset_method, rc_reset == 0);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800730 if (rc && !rc_reset)
731 rc_reset = rc;
732
733 if (rc_reset) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000734 netif_err(efx, drv, efx->net_dev,
735 "Unable to recover from chip test\n");
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100736 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800737 return rc_reset;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100738 }
739
Ben Hutchings17967212008-12-26 13:47:25 -0800740 rc = efx_test_phy(efx, tests, flags);
Ben Hutchings2ef30682008-12-26 13:47:04 -0800741 if (rc && !rc_test)
742 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100743
Ben Hutchings2ef30682008-12-26 13:47:04 -0800744 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
745 if (rc && !rc_test)
746 rc_test = rc;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100747
748 /* restore the PHY to the previous state */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000749 mutex_lock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100750 efx->phy_mode = phy_mode;
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000751 efx->loopback_mode = loopback_mode;
752 __efx_reconfigure_port(efx);
753 mutex_unlock(&efx->mac_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100754
Ben Hutchingse4abce82011-05-16 18:51:24 +0100755 netif_device_attach(efx->net_dev);
Neil Turton9d1aea62011-04-04 13:46:23 +0100756
Ben Hutchings2ef30682008-12-26 13:47:04 -0800757 return rc_test;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100758}
759