Commit | Line | Data |
---|---|---|
3273c2e8 BH |
1 | /**************************************************************************** |
2 | * Driver for Solarflare Solarstorm network controllers and boards | |
3 | * Copyright 2005-2006 Fen Systems Ltd. | |
4 | * Copyright 2006-2008 Solarflare Communications Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published | |
8 | * by the Free Software Foundation, incorporated herein by reference. | |
9 | */ | |
10 | ||
11 | #include <linux/netdevice.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/delay.h> | |
14 | #include <linux/kernel_stat.h> | |
15 | #include <linux/pci.h> | |
16 | #include <linux/ethtool.h> | |
17 | #include <linux/ip.h> | |
18 | #include <linux/in.h> | |
19 | #include <linux/udp.h> | |
20 | #include <linux/rtnetlink.h> | |
21 | #include <asm/io.h> | |
22 | #include "net_driver.h" | |
23 | #include "ethtool.h" | |
24 | #include "efx.h" | |
25 | #include "falcon.h" | |
26 | #include "selftest.h" | |
27 | #include "boards.h" | |
28 | #include "workarounds.h" | |
29 | #include "mac.h" | |
30 | ||
31 | /* | |
32 | * Loopback test packet structure | |
33 | * | |
34 | * The self-test should stress every RSS vector, and unfortunately | |
35 | * Falcon only performs RSS on TCP/UDP packets. | |
36 | */ | |
37 | struct efx_loopback_payload { | |
38 | struct ethhdr header; | |
39 | struct iphdr ip; | |
40 | struct udphdr udp; | |
41 | __be16 iteration; | |
42 | const char msg[64]; | |
43 | } __attribute__ ((packed)); | |
44 | ||
45 | /* Loopback test source MAC address */ | |
46 | static const unsigned char payload_source[ETH_ALEN] = { | |
47 | 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b, | |
48 | }; | |
49 | ||
50 | static const char *payload_msg = | |
51 | "Hello world! This is an Efx loopback test in progress!"; | |
52 | ||
53 | /** | |
54 | * efx_selftest_state - persistent state during a selftest | |
55 | * @flush: Drop all packets in efx_loopback_rx_packet | |
56 | * @packet_count: Number of packets being used in this test | |
57 | * @skbs: An array of skbs transmitted | |
58 | * @rx_good: RX good packet count | |
59 | * @rx_bad: RX bad packet count | |
60 | * @payload: Payload used in tests | |
61 | */ | |
62 | struct efx_selftest_state { | |
63 | int flush; | |
64 | int packet_count; | |
65 | struct sk_buff **skbs; | |
66 | atomic_t rx_good; | |
67 | atomic_t rx_bad; | |
68 | struct efx_loopback_payload payload; | |
69 | }; | |
70 | ||
71 | /************************************************************************** | |
72 | * | |
73 | * Configurable values | |
74 | * | |
75 | **************************************************************************/ | |
76 | ||
77 | /* Level of loopback testing | |
78 | * | |
79 | * The maximum packet burst length is 16**(n-1), i.e. | |
80 | * | |
81 | * - Level 0 : no packets | |
82 | * - Level 1 : 1 packet | |
83 | * - Level 2 : 17 packets (1 * 1 packet, 1 * 16 packets) | |
84 | * - Level 3 : 273 packets (1 * 1 packet, 1 * 16 packet, 1 * 256 packets) | |
85 | * | |
86 | */ | |
87 | static unsigned int loopback_test_level = 3; | |
88 | ||
89 | /************************************************************************** | |
90 | * | |
91 | * Interrupt and event queue testing | |
92 | * | |
93 | **************************************************************************/ | |
94 | ||
95 | /* Test generation and receipt of interrupts */ | |
96 | static int efx_test_interrupts(struct efx_nic *efx, | |
97 | struct efx_self_tests *tests) | |
98 | { | |
99 | struct efx_channel *channel; | |
100 | ||
101 | EFX_LOG(efx, "testing interrupts\n"); | |
102 | tests->interrupt = -1; | |
103 | ||
104 | /* Reset interrupt flag */ | |
105 | efx->last_irq_cpu = -1; | |
106 | smp_wmb(); | |
107 | ||
108 | /* ACK each interrupting event queue. Receiving an interrupt due to | |
109 | * traffic before a test event is raised is considered a pass */ | |
110 | efx_for_each_channel_with_interrupt(channel, efx) { | |
111 | if (channel->work_pending) | |
112 | efx_process_channel_now(channel); | |
113 | if (efx->last_irq_cpu >= 0) | |
114 | goto success; | |
115 | } | |
116 | ||
117 | falcon_generate_interrupt(efx); | |
118 | ||
119 | /* Wait for arrival of test interrupt. */ | |
120 | EFX_LOG(efx, "waiting for test interrupt\n"); | |
121 | schedule_timeout_uninterruptible(HZ / 10); | |
122 | if (efx->last_irq_cpu >= 0) | |
123 | goto success; | |
124 | ||
125 | EFX_ERR(efx, "timed out waiting for interrupt\n"); | |
126 | return -ETIMEDOUT; | |
127 | ||
128 | success: | |
129 | EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n", | |
130 | efx->interrupt_mode, efx->last_irq_cpu); | |
131 | tests->interrupt = 1; | |
132 | return 0; | |
133 | } | |
134 | ||
135 | /* Test generation and receipt of non-interrupting events */ | |
136 | static int efx_test_eventq(struct efx_channel *channel, | |
137 | struct efx_self_tests *tests) | |
138 | { | |
139 | unsigned int magic; | |
140 | ||
141 | /* Channel specific code, limited to 20 bits */ | |
142 | magic = (0x00010150 + channel->channel); | |
143 | EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n", | |
144 | channel->channel, magic); | |
145 | ||
146 | tests->eventq_dma[channel->channel] = -1; | |
147 | tests->eventq_int[channel->channel] = 1; /* fake pass */ | |
148 | tests->eventq_poll[channel->channel] = 1; /* fake pass */ | |
149 | ||
150 | /* Reset flag and zero magic word */ | |
151 | channel->efx->last_irq_cpu = -1; | |
152 | channel->eventq_magic = 0; | |
153 | smp_wmb(); | |
154 | ||
155 | falcon_generate_test_event(channel, magic); | |
156 | udelay(1); | |
157 | ||
158 | efx_process_channel_now(channel); | |
159 | if (channel->eventq_magic != magic) { | |
160 | EFX_ERR(channel->efx, "channel %d failed to see test event\n", | |
161 | channel->channel); | |
162 | return -ETIMEDOUT; | |
163 | } else { | |
164 | tests->eventq_dma[channel->channel] = 1; | |
165 | } | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | /* Test generation and receipt of interrupting events */ | |
171 | static int efx_test_eventq_irq(struct efx_channel *channel, | |
172 | struct efx_self_tests *tests) | |
173 | { | |
174 | unsigned int magic, count; | |
175 | ||
176 | /* Channel specific code, limited to 20 bits */ | |
177 | magic = (0x00010150 + channel->channel); | |
178 | EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n", | |
179 | channel->channel, magic); | |
180 | ||
181 | tests->eventq_dma[channel->channel] = -1; | |
182 | tests->eventq_int[channel->channel] = -1; | |
183 | tests->eventq_poll[channel->channel] = -1; | |
184 | ||
185 | /* Reset flag and zero magic word */ | |
186 | channel->efx->last_irq_cpu = -1; | |
187 | channel->eventq_magic = 0; | |
188 | smp_wmb(); | |
189 | ||
190 | falcon_generate_test_event(channel, magic); | |
191 | ||
192 | /* Wait for arrival of interrupt */ | |
193 | count = 0; | |
194 | do { | |
195 | schedule_timeout_uninterruptible(HZ / 100); | |
196 | ||
197 | if (channel->work_pending) | |
198 | efx_process_channel_now(channel); | |
199 | ||
200 | if (channel->eventq_magic == magic) | |
201 | goto eventq_ok; | |
202 | } while (++count < 2); | |
203 | ||
204 | EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n", | |
205 | channel->channel); | |
206 | ||
207 | /* See if interrupt arrived */ | |
208 | if (channel->efx->last_irq_cpu >= 0) { | |
209 | EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d " | |
210 | "during event queue test\n", channel->channel, | |
211 | raw_smp_processor_id()); | |
212 | tests->eventq_int[channel->channel] = 1; | |
213 | } | |
214 | ||
215 | /* Check to see if event was received even if interrupt wasn't */ | |
216 | efx_process_channel_now(channel); | |
217 | if (channel->eventq_magic == magic) { | |
218 | EFX_ERR(channel->efx, "channel %d event was generated, but " | |
219 | "failed to trigger an interrupt\n", channel->channel); | |
220 | tests->eventq_dma[channel->channel] = 1; | |
221 | } | |
222 | ||
223 | return -ETIMEDOUT; | |
224 | eventq_ok: | |
225 | EFX_LOG(channel->efx, "channel %d event queue passed\n", | |
226 | channel->channel); | |
227 | tests->eventq_dma[channel->channel] = 1; | |
228 | tests->eventq_int[channel->channel] = 1; | |
229 | tests->eventq_poll[channel->channel] = 1; | |
230 | return 0; | |
231 | } | |
232 | ||
233 | /************************************************************************** | |
234 | * | |
235 | * PHY testing | |
236 | * | |
237 | **************************************************************************/ | |
238 | ||
239 | /* Check PHY presence by reading the PHY ID registers */ | |
240 | static int efx_test_phy(struct efx_nic *efx, | |
241 | struct efx_self_tests *tests) | |
242 | { | |
243 | u16 physid1, physid2; | |
244 | struct mii_if_info *mii = &efx->mii; | |
245 | struct net_device *net_dev = efx->net_dev; | |
246 | ||
247 | if (efx->phy_type == PHY_TYPE_NONE) | |
248 | return 0; | |
249 | ||
250 | EFX_LOG(efx, "testing PHY presence\n"); | |
251 | tests->phy_ok = -1; | |
252 | ||
253 | physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1); | |
254 | physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2); | |
255 | ||
256 | if ((physid1 != 0x0000) && (physid1 != 0xffff) && | |
257 | (physid2 != 0x0000) && (physid2 != 0xffff)) { | |
258 | EFX_LOG(efx, "found MII PHY %d ID 0x%x:%x\n", | |
259 | mii->phy_id, physid1, physid2); | |
260 | tests->phy_ok = 1; | |
261 | return 0; | |
262 | } | |
263 | ||
264 | EFX_ERR(efx, "no MII PHY present with ID %d\n", mii->phy_id); | |
265 | return -ENODEV; | |
266 | } | |
267 | ||
268 | /************************************************************************** | |
269 | * | |
270 | * Loopback testing | |
271 | * NB Only one loopback test can be executing concurrently. | |
272 | * | |
273 | **************************************************************************/ | |
274 | ||
275 | /* Loopback test RX callback | |
276 | * This is called for each received packet during loopback testing. | |
277 | */ | |
278 | void efx_loopback_rx_packet(struct efx_nic *efx, | |
279 | const char *buf_ptr, int pkt_len) | |
280 | { | |
281 | struct efx_selftest_state *state = efx->loopback_selftest; | |
282 | struct efx_loopback_payload *received; | |
283 | struct efx_loopback_payload *payload; | |
284 | ||
285 | BUG_ON(!buf_ptr); | |
286 | ||
287 | /* If we are just flushing, then drop the packet */ | |
288 | if ((state == NULL) || state->flush) | |
289 | return; | |
290 | ||
291 | payload = &state->payload; | |
292 | ||
d3208b5e | 293 | received = (struct efx_loopback_payload *) buf_ptr; |
3273c2e8 BH |
294 | received->ip.saddr = payload->ip.saddr; |
295 | received->ip.check = payload->ip.check; | |
296 | ||
297 | /* Check that header exists */ | |
298 | if (pkt_len < sizeof(received->header)) { | |
299 | EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback " | |
300 | "test\n", pkt_len, LOOPBACK_MODE(efx)); | |
301 | goto err; | |
302 | } | |
303 | ||
304 | /* Check that the ethernet header exists */ | |
305 | if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) { | |
306 | EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n", | |
307 | LOOPBACK_MODE(efx)); | |
308 | goto err; | |
309 | } | |
310 | ||
311 | /* Check packet length */ | |
312 | if (pkt_len != sizeof(*payload)) { | |
313 | EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in " | |
314 | "%s loopback test\n", pkt_len, (int)sizeof(*payload), | |
315 | LOOPBACK_MODE(efx)); | |
316 | goto err; | |
317 | } | |
318 | ||
319 | /* Check that IP header matches */ | |
320 | if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) { | |
321 | EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n", | |
322 | LOOPBACK_MODE(efx)); | |
323 | goto err; | |
324 | } | |
325 | ||
326 | /* Check that msg and padding matches */ | |
327 | if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) { | |
328 | EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n", | |
329 | LOOPBACK_MODE(efx)); | |
330 | goto err; | |
331 | } | |
332 | ||
333 | /* Check that iteration matches */ | |
334 | if (received->iteration != payload->iteration) { | |
335 | EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in " | |
336 | "%s loopback test\n", ntohs(received->iteration), | |
337 | ntohs(payload->iteration), LOOPBACK_MODE(efx)); | |
338 | goto err; | |
339 | } | |
340 | ||
341 | /* Increase correct RX count */ | |
342 | EFX_TRACE(efx, "got loopback RX in %s loopback test\n", | |
343 | LOOPBACK_MODE(efx)); | |
344 | ||
345 | atomic_inc(&state->rx_good); | |
346 | return; | |
347 | ||
348 | err: | |
349 | #ifdef EFX_ENABLE_DEBUG | |
350 | if (atomic_read(&state->rx_bad) == 0) { | |
351 | EFX_ERR(efx, "received packet:\n"); | |
352 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1, | |
353 | buf_ptr, pkt_len, 0); | |
354 | EFX_ERR(efx, "expected packet:\n"); | |
355 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1, | |
356 | &state->payload, sizeof(state->payload), 0); | |
357 | } | |
358 | #endif | |
359 | atomic_inc(&state->rx_bad); | |
360 | } | |
361 | ||
362 | /* Initialise an efx_selftest_state for a new iteration */ | |
363 | static void efx_iterate_state(struct efx_nic *efx) | |
364 | { | |
365 | struct efx_selftest_state *state = efx->loopback_selftest; | |
366 | struct net_device *net_dev = efx->net_dev; | |
367 | struct efx_loopback_payload *payload = &state->payload; | |
368 | ||
369 | /* Initialise the layerII header */ | |
370 | memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN); | |
371 | memcpy(&payload->header.h_source, &payload_source, ETH_ALEN); | |
372 | payload->header.h_proto = htons(ETH_P_IP); | |
373 | ||
374 | /* saddr set later and used as incrementing count */ | |
375 | payload->ip.daddr = htonl(INADDR_LOOPBACK); | |
376 | payload->ip.ihl = 5; | |
377 | payload->ip.check = htons(0xdead); | |
378 | payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr)); | |
379 | payload->ip.version = IPVERSION; | |
380 | payload->ip.protocol = IPPROTO_UDP; | |
381 | ||
382 | /* Initialise udp header */ | |
383 | payload->udp.source = 0; | |
384 | payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) - | |
385 | sizeof(struct iphdr)); | |
386 | payload->udp.check = 0; /* checksum ignored */ | |
387 | ||
388 | /* Fill out payload */ | |
389 | payload->iteration = htons(ntohs(payload->iteration) + 1); | |
390 | memcpy(&payload->msg, payload_msg, sizeof(payload_msg)); | |
391 | ||
392 | /* Fill out remaining state members */ | |
393 | atomic_set(&state->rx_good, 0); | |
394 | atomic_set(&state->rx_bad, 0); | |
395 | smp_wmb(); | |
396 | } | |
397 | ||
398 | static int efx_tx_loopback(struct efx_tx_queue *tx_queue) | |
399 | { | |
400 | struct efx_nic *efx = tx_queue->efx; | |
401 | struct efx_selftest_state *state = efx->loopback_selftest; | |
402 | struct efx_loopback_payload *payload; | |
403 | struct sk_buff *skb; | |
404 | int i, rc; | |
405 | ||
406 | /* Transmit N copies of buffer */ | |
407 | for (i = 0; i < state->packet_count; i++) { | |
408 | /* Allocate an skb, holding an extra reference for | |
409 | * transmit completion counting */ | |
410 | skb = alloc_skb(sizeof(state->payload), GFP_KERNEL); | |
411 | if (!skb) | |
412 | return -ENOMEM; | |
413 | state->skbs[i] = skb; | |
414 | skb_get(skb); | |
415 | ||
416 | /* Copy the payload in, incrementing the source address to | |
417 | * exercise the rss vectors */ | |
418 | payload = ((struct efx_loopback_payload *) | |
419 | skb_put(skb, sizeof(state->payload))); | |
420 | memcpy(payload, &state->payload, sizeof(state->payload)); | |
421 | payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2)); | |
422 | ||
423 | /* Ensure everything we've written is visible to the | |
424 | * interrupt handler. */ | |
425 | smp_wmb(); | |
426 | ||
55668611 | 427 | if (efx_dev_registered(efx)) |
3273c2e8 BH |
428 | netif_tx_lock_bh(efx->net_dev); |
429 | rc = efx_xmit(efx, tx_queue, skb); | |
55668611 | 430 | if (efx_dev_registered(efx)) |
3273c2e8 BH |
431 | netif_tx_unlock_bh(efx->net_dev); |
432 | ||
433 | if (rc != NETDEV_TX_OK) { | |
434 | EFX_ERR(efx, "TX queue %d could not transmit packet %d " | |
435 | "of %d in %s loopback test\n", tx_queue->queue, | |
436 | i + 1, state->packet_count, LOOPBACK_MODE(efx)); | |
437 | ||
438 | /* Defer cleaning up the other skbs for the caller */ | |
439 | kfree_skb(skb); | |
440 | return -EPIPE; | |
441 | } | |
442 | } | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
447 | static int efx_rx_loopback(struct efx_tx_queue *tx_queue, | |
448 | struct efx_loopback_self_tests *lb_tests) | |
449 | { | |
450 | struct efx_nic *efx = tx_queue->efx; | |
451 | struct efx_selftest_state *state = efx->loopback_selftest; | |
452 | struct sk_buff *skb; | |
453 | int tx_done = 0, rx_good, rx_bad; | |
454 | int i, rc = 0; | |
455 | ||
55668611 | 456 | if (efx_dev_registered(efx)) |
3273c2e8 BH |
457 | netif_tx_lock_bh(efx->net_dev); |
458 | ||
459 | /* Count the number of tx completions, and decrement the refcnt. Any | |
460 | * skbs not already completed will be free'd when the queue is flushed */ | |
461 | for (i=0; i < state->packet_count; i++) { | |
462 | skb = state->skbs[i]; | |
463 | if (skb && !skb_shared(skb)) | |
464 | ++tx_done; | |
465 | dev_kfree_skb_any(skb); | |
466 | } | |
467 | ||
55668611 | 468 | if (efx_dev_registered(efx)) |
3273c2e8 BH |
469 | netif_tx_unlock_bh(efx->net_dev); |
470 | ||
471 | /* Check TX completion and received packet counts */ | |
472 | rx_good = atomic_read(&state->rx_good); | |
473 | rx_bad = atomic_read(&state->rx_bad); | |
474 | if (tx_done != state->packet_count) { | |
475 | /* Don't free the skbs; they will be picked up on TX | |
476 | * overflow or channel teardown. | |
477 | */ | |
478 | EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d " | |
479 | "TX completion events in %s loopback test\n", | |
480 | tx_queue->queue, tx_done, state->packet_count, | |
481 | LOOPBACK_MODE(efx)); | |
482 | rc = -ETIMEDOUT; | |
483 | /* Allow to fall through so we see the RX errors as well */ | |
484 | } | |
485 | ||
486 | /* We may always be up to a flush away from our desired packet total */ | |
487 | if (rx_good != state->packet_count) { | |
488 | EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d " | |
489 | "received packets in %s loopback test\n", | |
490 | tx_queue->queue, rx_good, state->packet_count, | |
491 | LOOPBACK_MODE(efx)); | |
492 | rc = -ETIMEDOUT; | |
493 | /* Fall through */ | |
494 | } | |
495 | ||
496 | /* Update loopback test structure */ | |
497 | lb_tests->tx_sent[tx_queue->queue] += state->packet_count; | |
498 | lb_tests->tx_done[tx_queue->queue] += tx_done; | |
499 | lb_tests->rx_good += rx_good; | |
500 | lb_tests->rx_bad += rx_bad; | |
501 | ||
502 | return rc; | |
503 | } | |
504 | ||
505 | static int | |
506 | efx_test_loopback(struct efx_tx_queue *tx_queue, | |
507 | struct efx_loopback_self_tests *lb_tests) | |
508 | { | |
509 | struct efx_nic *efx = tx_queue->efx; | |
510 | struct efx_selftest_state *state = efx->loopback_selftest; | |
511 | struct efx_channel *channel; | |
512 | int i, rc = 0; | |
513 | ||
514 | for (i = 0; i < loopback_test_level; i++) { | |
515 | /* Determine how many packets to send */ | |
516 | state->packet_count = (efx->type->txd_ring_mask + 1) / 3; | |
517 | state->packet_count = min(1 << (i << 2), state->packet_count); | |
518 | state->skbs = kzalloc(sizeof(state->skbs[0]) * | |
519 | state->packet_count, GFP_KERNEL); | |
520 | state->flush = 0; | |
521 | ||
522 | EFX_LOG(efx, "TX queue %d testing %s loopback with %d " | |
523 | "packets\n", tx_queue->queue, LOOPBACK_MODE(efx), | |
524 | state->packet_count); | |
525 | ||
526 | efx_iterate_state(efx); | |
527 | rc = efx_tx_loopback(tx_queue); | |
528 | ||
529 | /* NAPI polling is not enabled, so process channels synchronously */ | |
530 | schedule_timeout_uninterruptible(HZ / 50); | |
531 | efx_for_each_channel_with_interrupt(channel, efx) { | |
532 | if (channel->work_pending) | |
533 | efx_process_channel_now(channel); | |
534 | } | |
535 | ||
536 | rc |= efx_rx_loopback(tx_queue, lb_tests); | |
537 | kfree(state->skbs); | |
538 | ||
539 | if (rc) { | |
540 | /* Wait a while to ensure there are no packets | |
541 | * floating around after a failure. */ | |
542 | schedule_timeout_uninterruptible(HZ / 10); | |
543 | return rc; | |
544 | } | |
545 | } | |
546 | ||
547 | EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length " | |
548 | "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx), | |
549 | state->packet_count); | |
550 | ||
551 | return rc; | |
552 | } | |
553 | ||
554 | static int efx_test_loopbacks(struct efx_nic *efx, | |
555 | struct efx_self_tests *tests, | |
556 | unsigned int loopback_modes) | |
557 | { | |
558 | struct efx_selftest_state *state = efx->loopback_selftest; | |
559 | struct ethtool_cmd ecmd, ecmd_loopback; | |
560 | struct efx_tx_queue *tx_queue; | |
561 | enum efx_loopback_mode old_mode, mode; | |
562 | int count, rc = 0, link_up; | |
563 | ||
564 | rc = efx_ethtool_get_settings(efx->net_dev, &ecmd); | |
565 | if (rc) { | |
566 | EFX_ERR(efx, "could not get GMII settings\n"); | |
567 | return rc; | |
568 | } | |
569 | old_mode = efx->loopback_mode; | |
570 | ||
571 | /* Disable autonegotiation for the purposes of loopback */ | |
572 | memcpy(&ecmd_loopback, &ecmd, sizeof(ecmd_loopback)); | |
573 | if (ecmd_loopback.autoneg == AUTONEG_ENABLE) { | |
574 | ecmd_loopback.autoneg = AUTONEG_DISABLE; | |
575 | ecmd_loopback.duplex = DUPLEX_FULL; | |
576 | ecmd_loopback.speed = SPEED_10000; | |
577 | } | |
578 | ||
579 | rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback); | |
580 | if (rc) { | |
581 | EFX_ERR(efx, "could not disable autonegotiation\n"); | |
582 | goto out; | |
583 | } | |
584 | tests->loopback_speed = ecmd_loopback.speed; | |
585 | tests->loopback_full_duplex = ecmd_loopback.duplex; | |
586 | ||
587 | /* Test all supported loopback modes */ | |
588 | for (mode = LOOPBACK_NONE; mode < LOOPBACK_TEST_MAX; mode++) { | |
589 | if (!(loopback_modes & (1 << mode))) | |
590 | continue; | |
591 | ||
592 | /* Move the port into the specified loopback mode. */ | |
593 | state->flush = 1; | |
594 | efx->loopback_mode = mode; | |
595 | efx_reconfigure_port(efx); | |
596 | ||
597 | /* Wait for the PHY to signal the link is up */ | |
598 | count = 0; | |
599 | do { | |
600 | struct efx_channel *channel = &efx->channel[0]; | |
601 | ||
602 | falcon_check_xmac(efx); | |
603 | schedule_timeout_uninterruptible(HZ / 10); | |
604 | if (channel->work_pending) | |
605 | efx_process_channel_now(channel); | |
606 | /* Wait for PHY events to be processed */ | |
607 | flush_workqueue(efx->workqueue); | |
608 | rmb(); | |
609 | ||
610 | /* efx->link_up can be 1 even if the XAUI link is down, | |
611 | * (bug5762). Usually, it's not worth bothering with the | |
612 | * difference, but for selftests, we need that extra | |
613 | * guarantee that the link is really, really, up. | |
614 | */ | |
615 | link_up = efx->link_up; | |
616 | if (!falcon_xaui_link_ok(efx)) | |
617 | link_up = 0; | |
618 | ||
619 | } while ((++count < 20) && !link_up); | |
620 | ||
621 | /* The link should now be up. If it isn't, there is no point | |
622 | * in attempting a loopback test */ | |
623 | if (!link_up) { | |
624 | EFX_ERR(efx, "loopback %s never came up\n", | |
625 | LOOPBACK_MODE(efx)); | |
626 | rc = -EIO; | |
627 | goto out; | |
628 | } | |
629 | ||
630 | EFX_LOG(efx, "link came up in %s loopback in %d iterations\n", | |
631 | LOOPBACK_MODE(efx), count); | |
632 | ||
633 | /* Test every TX queue */ | |
634 | efx_for_each_tx_queue(tx_queue, efx) { | |
635 | rc |= efx_test_loopback(tx_queue, | |
636 | &tests->loopback[mode]); | |
637 | if (rc) | |
638 | goto out; | |
639 | } | |
640 | } | |
641 | ||
642 | out: | |
643 | /* Take out of loopback and restore PHY settings */ | |
644 | state->flush = 1; | |
645 | efx->loopback_mode = old_mode; | |
646 | efx_ethtool_set_settings(efx->net_dev, &ecmd); | |
647 | ||
648 | return rc; | |
649 | } | |
650 | ||
651 | /************************************************************************** | |
652 | * | |
653 | * Entry points | |
654 | * | |
655 | *************************************************************************/ | |
656 | ||
657 | /* Online (i.e. non-disruptive) testing | |
658 | * This checks interrupt generation, event delivery and PHY presence. */ | |
659 | int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests) | |
660 | { | |
661 | struct efx_channel *channel; | |
662 | int rc = 0; | |
663 | ||
664 | EFX_LOG(efx, "performing online self-tests\n"); | |
665 | ||
666 | rc |= efx_test_interrupts(efx, tests); | |
667 | efx_for_each_channel(channel, efx) { | |
668 | if (channel->has_interrupt) | |
669 | rc |= efx_test_eventq_irq(channel, tests); | |
670 | else | |
671 | rc |= efx_test_eventq(channel, tests); | |
672 | } | |
673 | rc |= efx_test_phy(efx, tests); | |
674 | ||
675 | if (rc) | |
676 | EFX_ERR(efx, "failed online self-tests\n"); | |
677 | ||
678 | return rc; | |
679 | } | |
680 | ||
681 | /* Offline (i.e. disruptive) testing | |
682 | * This checks MAC and PHY loopback on the specified port. */ | |
683 | int efx_offline_test(struct efx_nic *efx, | |
684 | struct efx_self_tests *tests, unsigned int loopback_modes) | |
685 | { | |
686 | struct efx_selftest_state *state; | |
687 | int rc = 0; | |
688 | ||
689 | EFX_LOG(efx, "performing offline self-tests\n"); | |
690 | ||
691 | /* Create a selftest_state structure to hold state for the test */ | |
692 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
693 | if (state == NULL) { | |
694 | rc = -ENOMEM; | |
695 | goto out; | |
696 | } | |
697 | ||
698 | /* Set the port loopback_selftest member. From this point on | |
699 | * all received packets will be dropped. Mark the state as | |
700 | * "flushing" so all inflight packets are dropped */ | |
701 | BUG_ON(efx->loopback_selftest); | |
702 | state->flush = 1; | |
d3208b5e | 703 | efx->loopback_selftest = state; |
3273c2e8 BH |
704 | |
705 | rc = efx_test_loopbacks(efx, tests, loopback_modes); | |
706 | ||
707 | efx->loopback_selftest = NULL; | |
708 | wmb(); | |
709 | kfree(state); | |
710 | ||
711 | out: | |
712 | if (rc) | |
713 | EFX_ERR(efx, "failed offline self-tests\n"); | |
714 | ||
715 | return rc; | |
716 | } | |
717 |