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"
26 #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_loopback_state - persistent state during a loopback 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 * @offload_csum: Checksums are being offloaded
59 * @rx_good: RX good packet count
60 * @rx_bad: RX bad packet count
61 * @payload: Payload used in tests
63 struct efx_loopback_state
{
66 struct sk_buff
**skbs
;
70 struct efx_loopback_payload payload
;
73 /**************************************************************************
75 * MII, NVRAM and register tests
77 **************************************************************************/
79 static int efx_test_mdio(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
82 int devad
= __ffs(efx
->mdio
.mmds
);
85 if (efx
->phy_type
== PHY_TYPE_NONE
)
88 mutex_lock(&efx
->mac_lock
);
91 physid1
= efx_mdio_read(efx
, devad
, MDIO_DEVID1
);
92 physid2
= efx_mdio_read(efx
, devad
, MDIO_DEVID2
);
94 if ((physid1
== 0x0000) || (physid1
== 0xffff) ||
95 (physid2
== 0x0000) || (physid2
== 0xffff)) {
96 EFX_ERR(efx
, "no MDIO PHY present with ID %d\n",
102 if (EFX_IS10G(efx
)) {
103 rc
= efx_mdio_check_mmds(efx
, efx
->phy_op
->mmds
, 0);
109 mutex_unlock(&efx
->mac_lock
);
110 tests
->mdio
= rc
? -1 : 1;
114 static int efx_test_nvram(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
118 rc
= falcon_read_nvram(efx
, NULL
);
119 tests
->nvram
= rc
? -1 : 1;
123 static int efx_test_chip(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
127 /* Not supported on A-series silicon */
128 if (efx_nic_rev(efx
) < EFX_REV_FALCON_B0
)
131 rc
= falcon_test_registers(efx
);
132 tests
->registers
= rc
? -1 : 1;
136 /**************************************************************************
138 * Interrupt and event queue testing
140 **************************************************************************/
142 /* Test generation and receipt of interrupts */
143 static int efx_test_interrupts(struct efx_nic
*efx
,
144 struct efx_self_tests
*tests
)
146 struct efx_channel
*channel
;
148 EFX_LOG(efx
, "testing interrupts\n");
149 tests
->interrupt
= -1;
151 /* Reset interrupt flag */
152 efx
->last_irq_cpu
= -1;
155 /* ACK each interrupting event queue. Receiving an interrupt due to
156 * traffic before a test event is raised is considered a pass */
157 efx_for_each_channel(channel
, efx
) {
158 if (channel
->work_pending
)
159 efx_process_channel_now(channel
);
160 if (efx
->last_irq_cpu
>= 0)
164 falcon_generate_interrupt(efx
);
166 /* Wait for arrival of test interrupt. */
167 EFX_LOG(efx
, "waiting for test interrupt\n");
168 schedule_timeout_uninterruptible(HZ
/ 10);
169 if (efx
->last_irq_cpu
>= 0)
172 EFX_ERR(efx
, "timed out waiting for interrupt\n");
176 EFX_LOG(efx
, "%s test interrupt seen on CPU%d\n", INT_MODE(efx
),
178 tests
->interrupt
= 1;
182 /* Test generation and receipt of interrupting events */
183 static int efx_test_eventq_irq(struct efx_channel
*channel
,
184 struct efx_self_tests
*tests
)
186 unsigned int magic
, count
;
188 /* Channel specific code, limited to 20 bits */
189 magic
= (0x00010150 + channel
->channel
);
190 EFX_LOG(channel
->efx
, "channel %d testing event queue with code %x\n",
191 channel
->channel
, magic
);
193 tests
->eventq_dma
[channel
->channel
] = -1;
194 tests
->eventq_int
[channel
->channel
] = -1;
195 tests
->eventq_poll
[channel
->channel
] = -1;
197 /* Reset flag and zero magic word */
198 channel
->efx
->last_irq_cpu
= -1;
199 channel
->eventq_magic
= 0;
202 falcon_generate_test_event(channel
, magic
);
204 /* Wait for arrival of interrupt */
207 schedule_timeout_uninterruptible(HZ
/ 100);
209 if (channel
->work_pending
)
210 efx_process_channel_now(channel
);
212 if (channel
->eventq_magic
== magic
)
214 } while (++count
< 2);
216 EFX_ERR(channel
->efx
, "channel %d timed out waiting for event queue\n",
219 /* See if interrupt arrived */
220 if (channel
->efx
->last_irq_cpu
>= 0) {
221 EFX_ERR(channel
->efx
, "channel %d saw interrupt on CPU%d "
222 "during event queue test\n", channel
->channel
,
223 raw_smp_processor_id());
224 tests
->eventq_int
[channel
->channel
] = 1;
227 /* Check to see if event was received even if interrupt wasn't */
228 efx_process_channel_now(channel
);
229 if (channel
->eventq_magic
== magic
) {
230 EFX_ERR(channel
->efx
, "channel %d event was generated, but "
231 "failed to trigger an interrupt\n", channel
->channel
);
232 tests
->eventq_dma
[channel
->channel
] = 1;
237 EFX_LOG(channel
->efx
, "channel %d event queue passed\n",
239 tests
->eventq_dma
[channel
->channel
] = 1;
240 tests
->eventq_int
[channel
->channel
] = 1;
241 tests
->eventq_poll
[channel
->channel
] = 1;
245 static int efx_test_phy(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
250 if (!efx
->phy_op
->run_tests
)
253 EFX_BUG_ON_PARANOID(efx
->phy_op
->num_tests
== 0 ||
254 efx
->phy_op
->num_tests
> EFX_MAX_PHY_TESTS
);
256 mutex_lock(&efx
->mac_lock
);
257 rc
= efx
->phy_op
->run_tests(efx
, tests
->phy
, flags
);
258 mutex_unlock(&efx
->mac_lock
);
262 /**************************************************************************
265 * NB Only one loopback test can be executing concurrently.
267 **************************************************************************/
269 /* Loopback test RX callback
270 * This is called for each received packet during loopback testing.
272 void efx_loopback_rx_packet(struct efx_nic
*efx
,
273 const char *buf_ptr
, int pkt_len
)
275 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
276 struct efx_loopback_payload
*received
;
277 struct efx_loopback_payload
*payload
;
281 /* If we are just flushing, then drop the packet */
282 if ((state
== NULL
) || state
->flush
)
285 payload
= &state
->payload
;
287 received
= (struct efx_loopback_payload
*) buf_ptr
;
288 received
->ip
.saddr
= payload
->ip
.saddr
;
289 if (state
->offload_csum
)
290 received
->ip
.check
= payload
->ip
.check
;
292 /* Check that header exists */
293 if (pkt_len
< sizeof(received
->header
)) {
294 EFX_ERR(efx
, "saw runt RX packet (length %d) in %s loopback "
295 "test\n", pkt_len
, LOOPBACK_MODE(efx
));
299 /* Check that the ethernet header exists */
300 if (memcmp(&received
->header
, &payload
->header
, ETH_HLEN
) != 0) {
301 EFX_ERR(efx
, "saw non-loopback RX packet in %s loopback test\n",
306 /* Check packet length */
307 if (pkt_len
!= sizeof(*payload
)) {
308 EFX_ERR(efx
, "saw incorrect RX packet length %d (wanted %d) in "
309 "%s loopback test\n", pkt_len
, (int)sizeof(*payload
),
314 /* Check that IP header matches */
315 if (memcmp(&received
->ip
, &payload
->ip
, sizeof(payload
->ip
)) != 0) {
316 EFX_ERR(efx
, "saw corrupted IP header in %s loopback test\n",
321 /* Check that msg and padding matches */
322 if (memcmp(&received
->msg
, &payload
->msg
, sizeof(received
->msg
)) != 0) {
323 EFX_ERR(efx
, "saw corrupted RX packet in %s loopback test\n",
328 /* Check that iteration matches */
329 if (received
->iteration
!= payload
->iteration
) {
330 EFX_ERR(efx
, "saw RX packet from iteration %d (wanted %d) in "
331 "%s loopback test\n", ntohs(received
->iteration
),
332 ntohs(payload
->iteration
), LOOPBACK_MODE(efx
));
336 /* Increase correct RX count */
337 EFX_TRACE(efx
, "got loopback RX in %s loopback test\n",
340 atomic_inc(&state
->rx_good
);
344 #ifdef EFX_ENABLE_DEBUG
345 if (atomic_read(&state
->rx_bad
) == 0) {
346 EFX_ERR(efx
, "received packet:\n");
347 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
348 buf_ptr
, pkt_len
, 0);
349 EFX_ERR(efx
, "expected packet:\n");
350 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
351 &state
->payload
, sizeof(state
->payload
), 0);
354 atomic_inc(&state
->rx_bad
);
357 /* Initialise an efx_selftest_state for a new iteration */
358 static void efx_iterate_state(struct efx_nic
*efx
)
360 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
361 struct net_device
*net_dev
= efx
->net_dev
;
362 struct efx_loopback_payload
*payload
= &state
->payload
;
364 /* Initialise the layerII header */
365 memcpy(&payload
->header
.h_dest
, net_dev
->dev_addr
, ETH_ALEN
);
366 memcpy(&payload
->header
.h_source
, &payload_source
, ETH_ALEN
);
367 payload
->header
.h_proto
= htons(ETH_P_IP
);
369 /* saddr set later and used as incrementing count */
370 payload
->ip
.daddr
= htonl(INADDR_LOOPBACK
);
372 payload
->ip
.check
= htons(0xdead);
373 payload
->ip
.tot_len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
));
374 payload
->ip
.version
= IPVERSION
;
375 payload
->ip
.protocol
= IPPROTO_UDP
;
377 /* Initialise udp header */
378 payload
->udp
.source
= 0;
379 payload
->udp
.len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
) -
380 sizeof(struct iphdr
));
381 payload
->udp
.check
= 0; /* checksum ignored */
383 /* Fill out payload */
384 payload
->iteration
= htons(ntohs(payload
->iteration
) + 1);
385 memcpy(&payload
->msg
, payload_msg
, sizeof(payload_msg
));
387 /* Fill out remaining state members */
388 atomic_set(&state
->rx_good
, 0);
389 atomic_set(&state
->rx_bad
, 0);
393 static int efx_begin_loopback(struct efx_tx_queue
*tx_queue
)
395 struct efx_nic
*efx
= tx_queue
->efx
;
396 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
397 struct efx_loopback_payload
*payload
;
402 /* Transmit N copies of buffer */
403 for (i
= 0; i
< state
->packet_count
; i
++) {
404 /* Allocate an skb, holding an extra reference for
405 * transmit completion counting */
406 skb
= alloc_skb(sizeof(state
->payload
), GFP_KERNEL
);
409 state
->skbs
[i
] = skb
;
412 /* Copy the payload in, incrementing the source address to
413 * exercise the rss vectors */
414 payload
= ((struct efx_loopback_payload
*)
415 skb_put(skb
, sizeof(state
->payload
)));
416 memcpy(payload
, &state
->payload
, sizeof(state
->payload
));
417 payload
->ip
.saddr
= htonl(INADDR_LOOPBACK
| (i
<< 2));
419 /* Ensure everything we've written is visible to the
420 * interrupt handler. */
423 if (efx_dev_registered(efx
))
424 netif_tx_lock_bh(efx
->net_dev
);
425 rc
= efx_enqueue_skb(tx_queue
, skb
);
426 if (efx_dev_registered(efx
))
427 netif_tx_unlock_bh(efx
->net_dev
);
429 if (rc
!= NETDEV_TX_OK
) {
430 EFX_ERR(efx
, "TX queue %d could not transmit packet %d "
431 "of %d in %s loopback test\n", tx_queue
->queue
,
432 i
+ 1, state
->packet_count
, LOOPBACK_MODE(efx
));
434 /* Defer cleaning up the other skbs for the caller */
443 static int efx_poll_loopback(struct efx_nic
*efx
)
445 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
446 struct efx_channel
*channel
;
448 /* NAPI polling is not enabled, so process channels
450 efx_for_each_channel(channel
, efx
) {
451 if (channel
->work_pending
)
452 efx_process_channel_now(channel
);
454 return atomic_read(&state
->rx_good
) == state
->packet_count
;
457 static int efx_end_loopback(struct efx_tx_queue
*tx_queue
,
458 struct efx_loopback_self_tests
*lb_tests
)
460 struct efx_nic
*efx
= tx_queue
->efx
;
461 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
463 int tx_done
= 0, rx_good
, rx_bad
;
466 if (efx_dev_registered(efx
))
467 netif_tx_lock_bh(efx
->net_dev
);
469 /* Count the number of tx completions, and decrement the refcnt. Any
470 * skbs not already completed will be free'd when the queue is flushed */
471 for (i
=0; i
< state
->packet_count
; i
++) {
472 skb
= state
->skbs
[i
];
473 if (skb
&& !skb_shared(skb
))
475 dev_kfree_skb_any(skb
);
478 if (efx_dev_registered(efx
))
479 netif_tx_unlock_bh(efx
->net_dev
);
481 /* Check TX completion and received packet counts */
482 rx_good
= atomic_read(&state
->rx_good
);
483 rx_bad
= atomic_read(&state
->rx_bad
);
484 if (tx_done
!= state
->packet_count
) {
485 /* Don't free the skbs; they will be picked up on TX
486 * overflow or channel teardown.
488 EFX_ERR(efx
, "TX queue %d saw only %d out of an expected %d "
489 "TX completion events in %s loopback test\n",
490 tx_queue
->queue
, tx_done
, state
->packet_count
,
493 /* Allow to fall through so we see the RX errors as well */
496 /* We may always be up to a flush away from our desired packet total */
497 if (rx_good
!= state
->packet_count
) {
498 EFX_LOG(efx
, "TX queue %d saw only %d out of an expected %d "
499 "received packets in %s loopback test\n",
500 tx_queue
->queue
, rx_good
, state
->packet_count
,
506 /* Update loopback test structure */
507 lb_tests
->tx_sent
[tx_queue
->queue
] += state
->packet_count
;
508 lb_tests
->tx_done
[tx_queue
->queue
] += tx_done
;
509 lb_tests
->rx_good
+= rx_good
;
510 lb_tests
->rx_bad
+= rx_bad
;
516 efx_test_loopback(struct efx_tx_queue
*tx_queue
,
517 struct efx_loopback_self_tests
*lb_tests
)
519 struct efx_nic
*efx
= tx_queue
->efx
;
520 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
521 int i
, begin_rc
, end_rc
;
523 for (i
= 0; i
< 3; i
++) {
524 /* Determine how many packets to send */
525 state
->packet_count
= EFX_TXQ_SIZE
/ 3;
526 state
->packet_count
= min(1 << (i
<< 2), state
->packet_count
);
527 state
->skbs
= kzalloc(sizeof(state
->skbs
[0]) *
528 state
->packet_count
, GFP_KERNEL
);
531 state
->flush
= false;
533 EFX_LOG(efx
, "TX queue %d testing %s loopback with %d "
534 "packets\n", tx_queue
->queue
, LOOPBACK_MODE(efx
),
535 state
->packet_count
);
537 efx_iterate_state(efx
);
538 begin_rc
= efx_begin_loopback(tx_queue
);
540 /* This will normally complete very quickly, but be
541 * prepared to wait up to 100 ms. */
543 if (!efx_poll_loopback(efx
)) {
545 efx_poll_loopback(efx
);
548 end_rc
= efx_end_loopback(tx_queue
, lb_tests
);
551 if (begin_rc
|| end_rc
) {
552 /* Wait a while to ensure there are no packets
553 * floating around after a failure. */
554 schedule_timeout_uninterruptible(HZ
/ 10);
555 return begin_rc
? begin_rc
: end_rc
;
559 EFX_LOG(efx
, "TX queue %d passed %s loopback test with a burst length "
560 "of %d packets\n", tx_queue
->queue
, LOOPBACK_MODE(efx
),
561 state
->packet_count
);
566 static int efx_test_loopbacks(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
567 unsigned int loopback_modes
)
569 enum efx_loopback_mode mode
;
570 struct efx_loopback_state
*state
;
571 struct efx_tx_queue
*tx_queue
;
575 /* Set the port loopback_selftest member. From this point on
576 * all received packets will be dropped. Mark the state as
577 * "flushing" so all inflight packets are dropped */
578 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
581 BUG_ON(efx
->loopback_selftest
);
583 efx
->loopback_selftest
= state
;
585 /* Test all supported loopback modes */
586 for (mode
= LOOPBACK_NONE
; mode
<= LOOPBACK_TEST_MAX
; mode
++) {
587 if (!(loopback_modes
& (1 << mode
)))
590 /* Move the port into the specified loopback mode. */
592 efx
->loopback_mode
= mode
;
593 efx_reconfigure_port(efx
);
595 /* Wait for the PHY to signal the link is up. Interrupts
596 * are enabled for PHY's using LASI, otherwise we poll()
600 struct efx_channel
*channel
= &efx
->channel
[0];
602 efx
->phy_op
->poll(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 /* We need both the PHY and MAC-PHY links to be OK */
611 link_up
= efx
->link_state
.up
;
613 link_up
= !efx
->mac_op
->check_fault(efx
);
615 } while ((++count
< 20) && !link_up
);
617 /* The link should now be up. If it isn't, there is no point
618 * in attempting a loopback test */
620 EFX_ERR(efx
, "loopback %s never came up\n",
626 EFX_LOG(efx
, "link came up in %s loopback in %d iterations\n",
627 LOOPBACK_MODE(efx
), count
);
629 /* Test every TX queue */
630 efx_for_each_tx_queue(tx_queue
, efx
) {
631 state
->offload_csum
= (tx_queue
->queue
==
632 EFX_TX_QUEUE_OFFLOAD_CSUM
);
633 rc
= efx_test_loopback(tx_queue
,
634 &tests
->loopback
[mode
]);
641 /* Remove the flush. The caller will remove the loopback setting */
643 efx
->loopback_selftest
= NULL
;
650 /**************************************************************************
654 *************************************************************************/
656 int efx_selftest(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
659 enum efx_loopback_mode loopback_mode
= efx
->loopback_mode
;
660 int phy_mode
= efx
->phy_mode
;
661 enum reset_type reset_method
= RESET_TYPE_INVISIBLE
;
662 struct ethtool_cmd ecmd
;
663 struct efx_channel
*channel
;
664 int rc_test
= 0, rc_reset
= 0, rc
;
666 /* Online (i.e. non-disruptive) testing
667 * This checks interrupt generation, event delivery and PHY presence. */
669 rc
= efx_test_mdio(efx
, tests
);
673 rc
= efx_test_nvram(efx
, tests
);
677 rc
= efx_test_interrupts(efx
, tests
);
681 efx_for_each_channel(channel
, efx
) {
682 rc
= efx_test_eventq_irq(channel
, tests
);
690 if (!(flags
& ETH_TEST_FL_OFFLINE
))
691 return efx_test_phy(efx
, tests
, flags
);
693 /* Offline (i.e. disruptive) testing
694 * This checks MAC and PHY loopback on the specified port. */
696 /* force the carrier state off so the kernel doesn't transmit during
697 * the loopback test, and the watchdog timeout doesn't fire. Also put
698 * falcon into loopback for the register test.
700 mutex_lock(&efx
->mac_lock
);
701 efx
->port_inhibited
= true;
702 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
;
708 efx
->loopback_mode
= __ffs(efx
->loopback_modes
);
711 __efx_reconfigure_port(efx
);
712 mutex_unlock(&efx
->mac_lock
);
714 /* free up all consumers of SRAM (including all the queues) */
715 efx_reset_down(efx
, reset_method
, &ecmd
);
717 rc
= efx_test_chip(efx
, tests
);
721 /* reset the chip to recover from the register test */
722 rc_reset
= efx
->type
->reset(efx
, reset_method
);
724 /* 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
;
727 efx
->loopback_mode
= LOOPBACK_NONE
;
729 rc
= efx_reset_up(efx
, reset_method
, &ecmd
, rc_reset
== 0);
734 EFX_ERR(efx
, "Unable to recover from chip test\n");
735 efx_schedule_reset(efx
, RESET_TYPE_DISABLE
);
739 rc
= efx_test_phy(efx
, tests
, flags
);
743 rc
= efx_test_loopbacks(efx
, tests
, efx
->loopback_modes
);
747 /* restore the PHY to the previous state */
748 efx
->loopback_mode
= loopback_mode
;
749 efx
->phy_mode
= phy_mode
;
750 efx
->port_inhibited
= false;
751 efx_ethtool_set_settings(efx
->net_dev
, &ecmd
);