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 * @rx_good: RX good packet count
59 * @rx_bad: RX bad packet count
60 * @payload: Payload used in tests
62 struct efx_loopback_state
{
65 struct sk_buff
**skbs
;
67 /* Checksums are being offloaded */
72 struct efx_loopback_payload payload
;
75 /**************************************************************************
77 * MII, NVRAM and register tests
79 **************************************************************************/
81 static int efx_test_mdio(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
84 int devad
= __ffs(efx
->mdio
.mmds
);
87 if (efx
->phy_type
== PHY_TYPE_NONE
)
90 mutex_lock(&efx
->mac_lock
);
93 physid1
= efx_mdio_read(efx
, devad
, MDIO_DEVID1
);
94 physid2
= efx_mdio_read(efx
, devad
, MDIO_DEVID2
);
96 if ((physid1
== 0x0000) || (physid1
== 0xffff) ||
97 (physid2
== 0x0000) || (physid2
== 0xffff)) {
98 EFX_ERR(efx
, "no MDIO PHY present with ID %d\n",
104 if (EFX_IS10G(efx
)) {
105 rc
= efx_mdio_check_mmds(efx
, efx
->phy_op
->mmds
, 0);
111 mutex_unlock(&efx
->mac_lock
);
112 tests
->mdio
= rc
? -1 : 1;
116 static int efx_test_nvram(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
120 rc
= falcon_read_nvram(efx
, NULL
);
121 tests
->nvram
= rc
? -1 : 1;
125 static int efx_test_chip(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
129 /* Not supported on A-series silicon */
130 if (falcon_rev(efx
) < FALCON_REV_B0
)
133 rc
= falcon_test_registers(efx
);
134 tests
->registers
= rc
? -1 : 1;
138 /**************************************************************************
140 * Interrupt and event queue testing
142 **************************************************************************/
144 /* Test generation and receipt of interrupts */
145 static int efx_test_interrupts(struct efx_nic
*efx
,
146 struct efx_self_tests
*tests
)
148 struct efx_channel
*channel
;
150 EFX_LOG(efx
, "testing interrupts\n");
151 tests
->interrupt
= -1;
153 /* Reset interrupt flag */
154 efx
->last_irq_cpu
= -1;
157 /* ACK each interrupting event queue. Receiving an interrupt due to
158 * traffic before a test event is raised is considered a pass */
159 efx_for_each_channel(channel
, efx
) {
160 if (channel
->work_pending
)
161 efx_process_channel_now(channel
);
162 if (efx
->last_irq_cpu
>= 0)
166 falcon_generate_interrupt(efx
);
168 /* Wait for arrival of test interrupt. */
169 EFX_LOG(efx
, "waiting for test interrupt\n");
170 schedule_timeout_uninterruptible(HZ
/ 10);
171 if (efx
->last_irq_cpu
>= 0)
174 EFX_ERR(efx
, "timed out waiting for interrupt\n");
178 EFX_LOG(efx
, "%s test interrupt seen on CPU%d\n", INT_MODE(efx
),
180 tests
->interrupt
= 1;
184 /* Test generation and receipt of interrupting events */
185 static int efx_test_eventq_irq(struct efx_channel
*channel
,
186 struct efx_self_tests
*tests
)
188 unsigned int magic
, count
;
190 /* Channel specific code, limited to 20 bits */
191 magic
= (0x00010150 + channel
->channel
);
192 EFX_LOG(channel
->efx
, "channel %d testing event queue with code %x\n",
193 channel
->channel
, magic
);
195 tests
->eventq_dma
[channel
->channel
] = -1;
196 tests
->eventq_int
[channel
->channel
] = -1;
197 tests
->eventq_poll
[channel
->channel
] = -1;
199 /* Reset flag and zero magic word */
200 channel
->efx
->last_irq_cpu
= -1;
201 channel
->eventq_magic
= 0;
204 falcon_generate_test_event(channel
, magic
);
206 /* Wait for arrival of interrupt */
209 schedule_timeout_uninterruptible(HZ
/ 100);
211 if (channel
->work_pending
)
212 efx_process_channel_now(channel
);
214 if (channel
->eventq_magic
== magic
)
216 } while (++count
< 2);
218 EFX_ERR(channel
->efx
, "channel %d timed out waiting for event queue\n",
221 /* See if interrupt arrived */
222 if (channel
->efx
->last_irq_cpu
>= 0) {
223 EFX_ERR(channel
->efx
, "channel %d saw interrupt on CPU%d "
224 "during event queue test\n", channel
->channel
,
225 raw_smp_processor_id());
226 tests
->eventq_int
[channel
->channel
] = 1;
229 /* Check to see if event was received even if interrupt wasn't */
230 efx_process_channel_now(channel
);
231 if (channel
->eventq_magic
== magic
) {
232 EFX_ERR(channel
->efx
, "channel %d event was generated, but "
233 "failed to trigger an interrupt\n", channel
->channel
);
234 tests
->eventq_dma
[channel
->channel
] = 1;
239 EFX_LOG(channel
->efx
, "channel %d event queue passed\n",
241 tests
->eventq_dma
[channel
->channel
] = 1;
242 tests
->eventq_int
[channel
->channel
] = 1;
243 tests
->eventq_poll
[channel
->channel
] = 1;
247 static int efx_test_phy(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
252 if (!efx
->phy_op
->run_tests
)
255 EFX_BUG_ON_PARANOID(efx
->phy_op
->num_tests
== 0 ||
256 efx
->phy_op
->num_tests
> EFX_MAX_PHY_TESTS
);
258 mutex_lock(&efx
->mac_lock
);
259 rc
= efx
->phy_op
->run_tests(efx
, tests
->phy
, flags
);
260 mutex_unlock(&efx
->mac_lock
);
264 /**************************************************************************
267 * NB Only one loopback test can be executing concurrently.
269 **************************************************************************/
271 /* Loopback test RX callback
272 * This is called for each received packet during loopback testing.
274 void efx_loopback_rx_packet(struct efx_nic
*efx
,
275 const char *buf_ptr
, int pkt_len
)
277 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
278 struct efx_loopback_payload
*received
;
279 struct efx_loopback_payload
*payload
;
283 /* If we are just flushing, then drop the packet */
284 if ((state
== NULL
) || state
->flush
)
287 payload
= &state
->payload
;
289 received
= (struct efx_loopback_payload
*) buf_ptr
;
290 received
->ip
.saddr
= payload
->ip
.saddr
;
291 if (state
->offload_csum
)
292 received
->ip
.check
= payload
->ip
.check
;
294 /* Check that header exists */
295 if (pkt_len
< sizeof(received
->header
)) {
296 EFX_ERR(efx
, "saw runt RX packet (length %d) in %s loopback "
297 "test\n", pkt_len
, LOOPBACK_MODE(efx
));
301 /* Check that the ethernet header exists */
302 if (memcmp(&received
->header
, &payload
->header
, ETH_HLEN
) != 0) {
303 EFX_ERR(efx
, "saw non-loopback RX packet in %s loopback test\n",
308 /* Check packet length */
309 if (pkt_len
!= sizeof(*payload
)) {
310 EFX_ERR(efx
, "saw incorrect RX packet length %d (wanted %d) in "
311 "%s loopback test\n", pkt_len
, (int)sizeof(*payload
),
316 /* Check that IP header matches */
317 if (memcmp(&received
->ip
, &payload
->ip
, sizeof(payload
->ip
)) != 0) {
318 EFX_ERR(efx
, "saw corrupted IP header in %s loopback test\n",
323 /* Check that msg and padding matches */
324 if (memcmp(&received
->msg
, &payload
->msg
, sizeof(received
->msg
)) != 0) {
325 EFX_ERR(efx
, "saw corrupted RX packet in %s loopback test\n",
330 /* Check that iteration matches */
331 if (received
->iteration
!= payload
->iteration
) {
332 EFX_ERR(efx
, "saw RX packet from iteration %d (wanted %d) in "
333 "%s loopback test\n", ntohs(received
->iteration
),
334 ntohs(payload
->iteration
), LOOPBACK_MODE(efx
));
338 /* Increase correct RX count */
339 EFX_TRACE(efx
, "got loopback RX in %s loopback test\n",
342 atomic_inc(&state
->rx_good
);
346 #ifdef EFX_ENABLE_DEBUG
347 if (atomic_read(&state
->rx_bad
) == 0) {
348 EFX_ERR(efx
, "received packet:\n");
349 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
350 buf_ptr
, pkt_len
, 0);
351 EFX_ERR(efx
, "expected packet:\n");
352 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
353 &state
->payload
, sizeof(state
->payload
), 0);
356 atomic_inc(&state
->rx_bad
);
359 /* Initialise an efx_selftest_state for a new iteration */
360 static void efx_iterate_state(struct efx_nic
*efx
)
362 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
363 struct net_device
*net_dev
= efx
->net_dev
;
364 struct efx_loopback_payload
*payload
= &state
->payload
;
366 /* Initialise the layerII header */
367 memcpy(&payload
->header
.h_dest
, net_dev
->dev_addr
, ETH_ALEN
);
368 memcpy(&payload
->header
.h_source
, &payload_source
, ETH_ALEN
);
369 payload
->header
.h_proto
= htons(ETH_P_IP
);
371 /* saddr set later and used as incrementing count */
372 payload
->ip
.daddr
= htonl(INADDR_LOOPBACK
);
374 payload
->ip
.check
= htons(0xdead);
375 payload
->ip
.tot_len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
));
376 payload
->ip
.version
= IPVERSION
;
377 payload
->ip
.protocol
= IPPROTO_UDP
;
379 /* Initialise udp header */
380 payload
->udp
.source
= 0;
381 payload
->udp
.len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
) -
382 sizeof(struct iphdr
));
383 payload
->udp
.check
= 0; /* checksum ignored */
385 /* Fill out payload */
386 payload
->iteration
= htons(ntohs(payload
->iteration
) + 1);
387 memcpy(&payload
->msg
, payload_msg
, sizeof(payload_msg
));
389 /* Fill out remaining state members */
390 atomic_set(&state
->rx_good
, 0);
391 atomic_set(&state
->rx_bad
, 0);
395 static int efx_begin_loopback(struct efx_tx_queue
*tx_queue
)
397 struct efx_nic
*efx
= tx_queue
->efx
;
398 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
399 struct efx_loopback_payload
*payload
;
404 /* Transmit N copies of buffer */
405 for (i
= 0; i
< state
->packet_count
; i
++) {
406 /* Allocate an skb, holding an extra reference for
407 * transmit completion counting */
408 skb
= alloc_skb(sizeof(state
->payload
), GFP_KERNEL
);
411 state
->skbs
[i
] = skb
;
414 /* Copy the payload in, incrementing the source address to
415 * exercise the rss vectors */
416 payload
= ((struct efx_loopback_payload
*)
417 skb_put(skb
, sizeof(state
->payload
)));
418 memcpy(payload
, &state
->payload
, sizeof(state
->payload
));
419 payload
->ip
.saddr
= htonl(INADDR_LOOPBACK
| (i
<< 2));
421 /* Ensure everything we've written is visible to the
422 * interrupt handler. */
425 if (efx_dev_registered(efx
))
426 netif_tx_lock_bh(efx
->net_dev
);
427 rc
= efx_enqueue_skb(tx_queue
, skb
);
428 if (efx_dev_registered(efx
))
429 netif_tx_unlock_bh(efx
->net_dev
);
431 if (rc
!= NETDEV_TX_OK
) {
432 EFX_ERR(efx
, "TX queue %d could not transmit packet %d "
433 "of %d in %s loopback test\n", tx_queue
->queue
,
434 i
+ 1, state
->packet_count
, LOOPBACK_MODE(efx
));
436 /* Defer cleaning up the other skbs for the caller */
440 efx
->net_dev
->trans_start
= jiffies
;
446 static int efx_poll_loopback(struct efx_nic
*efx
)
448 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
449 struct efx_channel
*channel
;
451 /* NAPI polling is not enabled, so process channels
453 efx_for_each_channel(channel
, efx
) {
454 if (channel
->work_pending
)
455 efx_process_channel_now(channel
);
457 return atomic_read(&state
->rx_good
) == state
->packet_count
;
460 static int efx_end_loopback(struct efx_tx_queue
*tx_queue
,
461 struct efx_loopback_self_tests
*lb_tests
)
463 struct efx_nic
*efx
= tx_queue
->efx
;
464 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
466 int tx_done
= 0, rx_good
, rx_bad
;
469 if (efx_dev_registered(efx
))
470 netif_tx_lock_bh(efx
->net_dev
);
472 /* Count the number of tx completions, and decrement the refcnt. Any
473 * skbs not already completed will be free'd when the queue is flushed */
474 for (i
=0; i
< state
->packet_count
; i
++) {
475 skb
= state
->skbs
[i
];
476 if (skb
&& !skb_shared(skb
))
478 dev_kfree_skb_any(skb
);
481 if (efx_dev_registered(efx
))
482 netif_tx_unlock_bh(efx
->net_dev
);
484 /* Check TX completion and received packet counts */
485 rx_good
= atomic_read(&state
->rx_good
);
486 rx_bad
= atomic_read(&state
->rx_bad
);
487 if (tx_done
!= state
->packet_count
) {
488 /* Don't free the skbs; they will be picked up on TX
489 * overflow or channel teardown.
491 EFX_ERR(efx
, "TX queue %d saw only %d out of an expected %d "
492 "TX completion events in %s loopback test\n",
493 tx_queue
->queue
, tx_done
, state
->packet_count
,
496 /* Allow to fall through so we see the RX errors as well */
499 /* We may always be up to a flush away from our desired packet total */
500 if (rx_good
!= state
->packet_count
) {
501 EFX_LOG(efx
, "TX queue %d saw only %d out of an expected %d "
502 "received packets in %s loopback test\n",
503 tx_queue
->queue
, rx_good
, state
->packet_count
,
509 /* Update loopback test structure */
510 lb_tests
->tx_sent
[tx_queue
->queue
] += state
->packet_count
;
511 lb_tests
->tx_done
[tx_queue
->queue
] += tx_done
;
512 lb_tests
->rx_good
+= rx_good
;
513 lb_tests
->rx_bad
+= rx_bad
;
519 efx_test_loopback(struct efx_tx_queue
*tx_queue
,
520 struct efx_loopback_self_tests
*lb_tests
)
522 struct efx_nic
*efx
= tx_queue
->efx
;
523 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
524 int i
, begin_rc
, end_rc
;
526 for (i
= 0; i
< 3; i
++) {
527 /* Determine how many packets to send */
528 state
->packet_count
= EFX_TXQ_SIZE
/ 3;
529 state
->packet_count
= min(1 << (i
<< 2), state
->packet_count
);
530 state
->skbs
= kzalloc(sizeof(state
->skbs
[0]) *
531 state
->packet_count
, GFP_KERNEL
);
534 state
->flush
= false;
536 EFX_LOG(efx
, "TX queue %d testing %s loopback with %d "
537 "packets\n", tx_queue
->queue
, LOOPBACK_MODE(efx
),
538 state
->packet_count
);
540 efx_iterate_state(efx
);
541 begin_rc
= efx_begin_loopback(tx_queue
);
543 /* This will normally complete very quickly, but be
544 * prepared to wait up to 100 ms. */
546 if (!efx_poll_loopback(efx
)) {
548 efx_poll_loopback(efx
);
551 end_rc
= efx_end_loopback(tx_queue
, lb_tests
);
554 if (begin_rc
|| end_rc
) {
555 /* Wait a while to ensure there are no packets
556 * floating around after a failure. */
557 schedule_timeout_uninterruptible(HZ
/ 10);
558 return begin_rc
? begin_rc
: end_rc
;
562 EFX_LOG(efx
, "TX queue %d passed %s loopback test with a burst length "
563 "of %d packets\n", tx_queue
->queue
, LOOPBACK_MODE(efx
),
564 state
->packet_count
);
569 static int efx_test_loopbacks(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
570 unsigned int loopback_modes
)
572 enum efx_loopback_mode mode
;
573 struct efx_loopback_state
*state
;
574 struct efx_tx_queue
*tx_queue
;
578 /* Set the port loopback_selftest member. From this point on
579 * all received packets will be dropped. Mark the state as
580 * "flushing" so all inflight packets are dropped */
581 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
584 BUG_ON(efx
->loopback_selftest
);
586 efx
->loopback_selftest
= state
;
588 /* Test all supported loopback modes */
589 for (mode
= LOOPBACK_NONE
; mode
<= LOOPBACK_TEST_MAX
; mode
++) {
590 if (!(loopback_modes
& (1 << mode
)))
593 /* Move the port into the specified loopback mode. */
595 efx
->loopback_mode
= mode
;
596 efx_reconfigure_port(efx
);
598 /* Wait for the PHY to signal the link is up. Interrupts
599 * are enabled for PHY's using LASI, otherwise we poll()
603 struct efx_channel
*channel
= &efx
->channel
[0];
605 efx
->phy_op
->poll(efx
);
606 schedule_timeout_uninterruptible(HZ
/ 10);
607 if (channel
->work_pending
)
608 efx_process_channel_now(channel
);
609 /* Wait for PHY events to be processed */
610 flush_workqueue(efx
->workqueue
);
613 /* We need both the PHY and MAC-PHY links to be OK */
614 link_up
= efx
->link_state
.up
;
616 link_up
= !efx
->mac_op
->check_fault(efx
);
618 } while ((++count
< 20) && !link_up
);
620 /* The link should now be up. If it isn't, there is no point
621 * in attempting a loopback test */
623 EFX_ERR(efx
, "loopback %s never came up\n",
629 EFX_LOG(efx
, "link came up in %s loopback in %d iterations\n",
630 LOOPBACK_MODE(efx
), count
);
632 /* Test every TX queue */
633 efx_for_each_tx_queue(tx_queue
, efx
) {
634 state
->offload_csum
= (tx_queue
->queue
==
635 EFX_TX_QUEUE_OFFLOAD_CSUM
);
636 rc
= efx_test_loopback(tx_queue
,
637 &tests
->loopback
[mode
]);
644 /* Remove the flush. The caller will remove the loopback setting */
646 efx
->loopback_selftest
= NULL
;
653 /**************************************************************************
657 *************************************************************************/
659 int efx_selftest(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
662 enum efx_loopback_mode loopback_mode
= efx
->loopback_mode
;
663 int phy_mode
= efx
->phy_mode
;
664 enum reset_type reset_method
= RESET_TYPE_INVISIBLE
;
665 struct ethtool_cmd ecmd
;
666 struct efx_channel
*channel
;
667 int rc_test
= 0, rc_reset
= 0, rc
;
669 /* Online (i.e. non-disruptive) testing
670 * This checks interrupt generation, event delivery and PHY presence. */
672 rc
= efx_test_mdio(efx
, tests
);
676 rc
= efx_test_nvram(efx
, tests
);
680 rc
= efx_test_interrupts(efx
, tests
);
684 efx_for_each_channel(channel
, efx
) {
685 rc
= efx_test_eventq_irq(channel
, tests
);
693 if (!(flags
& ETH_TEST_FL_OFFLINE
))
694 return efx_test_phy(efx
, tests
, flags
);
696 /* Offline (i.e. disruptive) testing
697 * This checks MAC and PHY loopback on the specified port. */
699 /* force the carrier state off so the kernel doesn't transmit during
700 * the loopback test, and the watchdog timeout doesn't fire. Also put
701 * falcon into loopback for the register test.
703 mutex_lock(&efx
->mac_lock
);
704 efx
->port_inhibited
= true;
705 if (efx
->loopback_modes
) {
706 /* We need the 312 clock from the PHY to test the XMAC
707 * registers, so move into XGMII loopback if available */
708 if (efx
->loopback_modes
& (1 << LOOPBACK_XGMII
))
709 efx
->loopback_mode
= LOOPBACK_XGMII
;
711 efx
->loopback_mode
= __ffs(efx
->loopback_modes
);
714 __efx_reconfigure_port(efx
);
715 mutex_unlock(&efx
->mac_lock
);
717 /* free up all consumers of SRAM (including all the queues) */
718 efx_reset_down(efx
, reset_method
, &ecmd
);
720 rc
= efx_test_chip(efx
, tests
);
724 /* reset the chip to recover from the register test */
725 rc_reset
= falcon_reset_hw(efx
, reset_method
);
727 /* Ensure that the phy is powered and out of loopback
728 * for the bist and loopback tests */
729 efx
->phy_mode
&= ~PHY_MODE_LOW_POWER
;
730 efx
->loopback_mode
= LOOPBACK_NONE
;
732 rc
= efx_reset_up(efx
, reset_method
, &ecmd
, rc_reset
== 0);
737 EFX_ERR(efx
, "Unable to recover from chip test\n");
738 efx_schedule_reset(efx
, RESET_TYPE_DISABLE
);
742 rc
= efx_test_phy(efx
, tests
, flags
);
746 rc
= efx_test_loopbacks(efx
, tests
, efx
->loopback_modes
);
750 /* restore the PHY to the previous state */
751 efx
->loopback_mode
= loopback_mode
;
752 efx
->phy_mode
= phy_mode
;
753 efx
->port_inhibited
= false;
754 efx_ethtool_set_settings(efx
->net_dev
, &ecmd
);