1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
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.
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>
19 #include <linux/udp.h>
20 #include <linux/rtnetlink.h>
22 #include "net_driver.h"
28 #include "workarounds.h"
32 * Loopback test packet structure
34 * The self-test should stress every RSS vector, and unfortunately
35 * Falcon only performs RSS on TCP/UDP packets.
37 struct efx_loopback_payload
{
43 } __attribute__ ((packed
));
45 /* Loopback test source MAC address */
46 static const unsigned char payload_source
[ETH_ALEN
] = {
47 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
50 static const char *payload_msg
=
51 "Hello world! This is an Efx loopback test in progress!";
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
62 struct efx_selftest_state
{
65 struct sk_buff
**skbs
;
68 struct efx_loopback_payload payload
;
71 /**************************************************************************
75 **************************************************************************/
77 /* Level of loopback testing
79 * The maximum packet burst length is 16**(n-1), i.e.
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)
87 static unsigned int loopback_test_level
= 3;
89 /**************************************************************************
91 * Interrupt and event queue testing
93 **************************************************************************/
95 /* Test generation and receipt of interrupts */
96 static int efx_test_interrupts(struct efx_nic
*efx
,
97 struct efx_self_tests
*tests
)
99 struct efx_channel
*channel
;
101 EFX_LOG(efx
, "testing interrupts\n");
102 tests
->interrupt
= -1;
104 /* Reset interrupt flag */
105 efx
->last_irq_cpu
= -1;
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)
117 falcon_generate_interrupt(efx
);
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)
125 EFX_ERR(efx
, "timed out waiting for interrupt\n");
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;
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
)
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
);
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 */
150 /* Reset flag and zero magic word */
151 channel
->efx
->last_irq_cpu
= -1;
152 channel
->eventq_magic
= 0;
155 falcon_generate_test_event(channel
, magic
);
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",
164 tests
->eventq_dma
[channel
->channel
] = 1;
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
)
174 unsigned int magic
, count
;
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
);
181 tests
->eventq_dma
[channel
->channel
] = -1;
182 tests
->eventq_int
[channel
->channel
] = -1;
183 tests
->eventq_poll
[channel
->channel
] = -1;
185 /* Reset flag and zero magic word */
186 channel
->efx
->last_irq_cpu
= -1;
187 channel
->eventq_magic
= 0;
190 falcon_generate_test_event(channel
, magic
);
192 /* Wait for arrival of interrupt */
195 schedule_timeout_uninterruptible(HZ
/ 100);
197 if (channel
->work_pending
)
198 efx_process_channel_now(channel
);
200 if (channel
->eventq_magic
== magic
)
202 } while (++count
< 2);
204 EFX_ERR(channel
->efx
, "channel %d timed out waiting for event queue\n",
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;
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;
225 EFX_LOG(channel
->efx
, "channel %d event queue passed\n",
227 tests
->eventq_dma
[channel
->channel
] = 1;
228 tests
->eventq_int
[channel
->channel
] = 1;
229 tests
->eventq_poll
[channel
->channel
] = 1;
233 /**************************************************************************
237 **************************************************************************/
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
)
243 u16 physid1
, physid2
;
244 struct mii_if_info
*mii
= &efx
->mii
;
245 struct net_device
*net_dev
= efx
->net_dev
;
247 if (efx
->phy_type
== PHY_TYPE_NONE
)
250 EFX_LOG(efx
, "testing PHY presence\n");
253 physid1
= mii
->mdio_read(net_dev
, mii
->phy_id
, MII_PHYSID1
);
254 physid2
= mii
->mdio_read(net_dev
, mii
->phy_id
, MII_PHYSID2
);
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
);
264 EFX_ERR(efx
, "no MII PHY present with ID %d\n", mii
->phy_id
);
268 /**************************************************************************
271 * NB Only one loopback test can be executing concurrently.
273 **************************************************************************/
275 /* Loopback test RX callback
276 * This is called for each received packet during loopback testing.
278 void efx_loopback_rx_packet(struct efx_nic
*efx
,
279 const char *buf_ptr
, int pkt_len
)
281 struct efx_selftest_state
*state
= efx
->loopback_selftest
;
282 struct efx_loopback_payload
*received
;
283 struct efx_loopback_payload
*payload
;
287 /* If we are just flushing, then drop the packet */
288 if ((state
== NULL
) || state
->flush
)
291 payload
= &state
->payload
;
293 received
= (struct efx_loopback_payload
*)(char *) buf_ptr
;
294 received
->ip
.saddr
= payload
->ip
.saddr
;
295 received
->ip
.check
= payload
->ip
.check
;
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
));
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",
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
),
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",
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",
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
));
341 /* Increase correct RX count */
342 EFX_TRACE(efx
, "got loopback RX in %s loopback test\n",
345 atomic_inc(&state
->rx_good
);
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);
359 atomic_inc(&state
->rx_bad
);
362 /* Initialise an efx_selftest_state for a new iteration */
363 static void efx_iterate_state(struct efx_nic
*efx
)
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
;
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
);
374 /* saddr set later and used as incrementing count */
375 payload
->ip
.daddr
= htonl(INADDR_LOOPBACK
);
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
;
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 */
388 /* Fill out payload */
389 payload
->iteration
= htons(ntohs(payload
->iteration
) + 1);
390 memcpy(&payload
->msg
, payload_msg
, sizeof(payload_msg
));
392 /* Fill out remaining state members */
393 atomic_set(&state
->rx_good
, 0);
394 atomic_set(&state
->rx_bad
, 0);
398 static int efx_tx_loopback(struct efx_tx_queue
*tx_queue
)
400 struct efx_nic
*efx
= tx_queue
->efx
;
401 struct efx_selftest_state
*state
= efx
->loopback_selftest
;
402 struct efx_loopback_payload
*payload
;
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
);
413 state
->skbs
[i
] = skb
;
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));
423 /* Ensure everything we've written is visible to the
424 * interrupt handler. */
427 if (NET_DEV_REGISTERED(efx
))
428 netif_tx_lock_bh(efx
->net_dev
);
429 rc
= efx_xmit(efx
, tx_queue
, skb
);
430 if (NET_DEV_REGISTERED(efx
))
431 netif_tx_unlock_bh(efx
->net_dev
);
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
));
438 /* Defer cleaning up the other skbs for the caller */
447 static int efx_rx_loopback(struct efx_tx_queue
*tx_queue
,
448 struct efx_loopback_self_tests
*lb_tests
)
450 struct efx_nic
*efx
= tx_queue
->efx
;
451 struct efx_selftest_state
*state
= efx
->loopback_selftest
;
453 int tx_done
= 0, rx_good
, rx_bad
;
456 if (NET_DEV_REGISTERED(efx
))
457 netif_tx_lock_bh(efx
->net_dev
);
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
))
465 dev_kfree_skb_any(skb
);
468 if (NET_DEV_REGISTERED(efx
))
469 netif_tx_unlock_bh(efx
->net_dev
);
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.
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
,
483 /* Allow to fall through so we see the RX errors as well */
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
,
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
;
506 efx_test_loopback(struct efx_tx_queue
*tx_queue
,
507 struct efx_loopback_self_tests
*lb_tests
)
509 struct efx_nic
*efx
= tx_queue
->efx
;
510 struct efx_selftest_state
*state
= efx
->loopback_selftest
;
511 struct efx_channel
*channel
;
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
);
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
);
526 efx_iterate_state(efx
);
527 rc
= efx_tx_loopback(tx_queue
);
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
);
536 rc
|= efx_rx_loopback(tx_queue
, lb_tests
);
540 /* Wait a while to ensure there are no packets
541 * floating around after a failure. */
542 schedule_timeout_uninterruptible(HZ
/ 10);
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
);
554 static int efx_test_loopbacks(struct efx_nic
*efx
,
555 struct efx_self_tests
*tests
,
556 unsigned int loopback_modes
)
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
;
564 rc
= efx_ethtool_get_settings(efx
->net_dev
, &ecmd
);
566 EFX_ERR(efx
, "could not get GMII settings\n");
569 old_mode
= efx
->loopback_mode
;
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
;
579 rc
= efx_ethtool_set_settings(efx
->net_dev
, &ecmd_loopback
);
581 EFX_ERR(efx
, "could not disable autonegotiation\n");
584 tests
->loopback_speed
= ecmd_loopback
.speed
;
585 tests
->loopback_full_duplex
= ecmd_loopback
.duplex
;
587 /* Test all supported loopback modes */
588 for (mode
= LOOPBACK_NONE
; mode
< LOOPBACK_TEST_MAX
; mode
++) {
589 if (!(loopback_modes
& (1 << mode
)))
592 /* Move the port into the specified loopback mode. */
594 efx
->loopback_mode
= mode
;
595 efx_reconfigure_port(efx
);
597 /* Wait for the PHY to signal the link is up */
600 struct efx_channel
*channel
= &efx
->channel
[0];
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
);
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.
615 link_up
= efx
->link_up
;
616 if (!falcon_xaui_link_ok(efx
))
619 } while ((++count
< 20) && !link_up
);
621 /* The link should now be up. If it isn't, there is no point
622 * in attempting a loopback test */
624 EFX_ERR(efx
, "loopback %s never came up\n",
630 EFX_LOG(efx
, "link came up in %s loopback in %d iterations\n",
631 LOOPBACK_MODE(efx
), count
);
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
]);
643 /* Take out of loopback and restore PHY settings */
645 efx
->loopback_mode
= old_mode
;
646 efx_ethtool_set_settings(efx
->net_dev
, &ecmd
);
651 /**************************************************************************
655 *************************************************************************/
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
)
661 struct efx_channel
*channel
;
664 EFX_LOG(efx
, "performing online self-tests\n");
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
);
671 rc
|= efx_test_eventq(channel
, tests
);
673 rc
|= efx_test_phy(efx
, tests
);
676 EFX_ERR(efx
, "failed online self-tests\n");
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
)
686 struct efx_selftest_state
*state
;
689 EFX_LOG(efx
, "performing offline self-tests\n");
691 /* Create a selftest_state structure to hold state for the test */
692 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
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
);
703 efx
->loopback_selftest
= (void *)state
;
705 rc
= efx_test_loopbacks(efx
, tests
, loopback_modes
);
707 efx
->loopback_selftest
= NULL
;
713 EFX_ERR(efx
, "failed offline self-tests\n");