1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2010 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>
21 #include <linux/slab.h>
23 #include "net_driver.h"
27 #include "workarounds.h"
30 * Loopback test packet structure
32 * The self-test should stress every RSS vector, and unfortunately
33 * Falcon only performs RSS on TCP/UDP packets.
35 struct efx_loopback_payload
{
43 /* Loopback test source MAC address */
44 static const unsigned char payload_source
[ETH_ALEN
] = {
45 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
48 static const char payload_msg
[] =
49 "Hello world! This is an Efx loopback test in progress!";
51 /* Interrupt mode names */
52 static const unsigned int efx_interrupt_mode_max
= EFX_INT_MODE_MAX
;
53 static const char *efx_interrupt_mode_names
[] = {
54 [EFX_INT_MODE_MSIX
] = "MSI-X",
55 [EFX_INT_MODE_MSI
] = "MSI",
56 [EFX_INT_MODE_LEGACY
] = "legacy",
58 #define INT_MODE(efx) \
59 STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
62 * efx_loopback_state - persistent state during a loopback selftest
63 * @flush: Drop all packets in efx_loopback_rx_packet
64 * @packet_count: Number of packets being used in this test
65 * @skbs: An array of skbs transmitted
66 * @offload_csum: Checksums are being offloaded
67 * @rx_good: RX good packet count
68 * @rx_bad: RX bad packet count
69 * @payload: Payload used in tests
71 struct efx_loopback_state
{
74 struct sk_buff
**skbs
;
78 struct efx_loopback_payload payload
;
81 /**************************************************************************
83 * MII, NVRAM and register tests
85 **************************************************************************/
87 static int efx_test_phy_alive(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
91 if (efx
->phy_op
->test_alive
) {
92 rc
= efx
->phy_op
->test_alive(efx
);
93 tests
->phy_alive
= rc
? -1 : 1;
99 static int efx_test_nvram(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
103 if (efx
->type
->test_nvram
) {
104 rc
= efx
->type
->test_nvram(efx
);
105 tests
->nvram
= rc
? -1 : 1;
111 static int efx_test_chip(struct efx_nic
*efx
, struct efx_self_tests
*tests
)
115 /* Test register access */
116 if (efx
->type
->test_registers
) {
117 rc
= efx
->type
->test_registers(efx
);
118 tests
->registers
= rc
? -1 : 1;
124 /**************************************************************************
126 * Interrupt and event queue testing
128 **************************************************************************/
130 /* Test generation and receipt of interrupts */
131 static int efx_test_interrupts(struct efx_nic
*efx
,
132 struct efx_self_tests
*tests
)
134 struct efx_channel
*channel
;
136 netif_dbg(efx
, drv
, efx
->net_dev
, "testing interrupts\n");
137 tests
->interrupt
= -1;
139 /* Reset interrupt flag */
140 efx
->last_irq_cpu
= -1;
143 /* ACK each interrupting event queue. Receiving an interrupt due to
144 * traffic before a test event is raised is considered a pass */
145 efx_for_each_channel(channel
, efx
) {
146 if (channel
->work_pending
)
147 efx_process_channel_now(channel
);
148 if (efx
->last_irq_cpu
>= 0)
152 efx_nic_generate_interrupt(efx
);
154 /* Wait for arrival of test interrupt. */
155 netif_dbg(efx
, drv
, efx
->net_dev
, "waiting for test interrupt\n");
156 schedule_timeout_uninterruptible(HZ
/ 10);
157 if (efx
->last_irq_cpu
>= 0)
160 netif_err(efx
, drv
, efx
->net_dev
, "timed out waiting for interrupt\n");
164 netif_dbg(efx
, drv
, efx
->net_dev
, "%s test interrupt seen on CPU%d\n",
167 tests
->interrupt
= 1;
171 /* Test generation and receipt of interrupting events */
172 static int efx_test_eventq_irq(struct efx_channel
*channel
,
173 struct efx_self_tests
*tests
)
175 struct efx_nic
*efx
= channel
->efx
;
176 unsigned int magic_count
, count
;
178 tests
->eventq_dma
[channel
->channel
] = -1;
179 tests
->eventq_int
[channel
->channel
] = -1;
180 tests
->eventq_poll
[channel
->channel
] = -1;
182 magic_count
= channel
->magic_count
;
183 channel
->efx
->last_irq_cpu
= -1;
186 efx_nic_generate_test_event(channel
);
188 /* Wait for arrival of interrupt */
191 schedule_timeout_uninterruptible(HZ
/ 100);
193 if (channel
->work_pending
)
194 efx_process_channel_now(channel
);
196 if (channel
->magic_count
!= magic_count
)
198 } while (++count
< 2);
200 netif_err(efx
, drv
, efx
->net_dev
,
201 "channel %d timed out waiting for event queue\n",
204 /* See if interrupt arrived */
205 if (channel
->efx
->last_irq_cpu
>= 0) {
206 netif_err(efx
, drv
, efx
->net_dev
,
207 "channel %d saw interrupt on CPU%d "
208 "during event queue test\n", channel
->channel
,
209 raw_smp_processor_id());
210 tests
->eventq_int
[channel
->channel
] = 1;
213 /* Check to see if event was received even if interrupt wasn't */
214 efx_process_channel_now(channel
);
215 if (channel
->magic_count
!= magic_count
) {
216 netif_err(efx
, drv
, efx
->net_dev
,
217 "channel %d event was generated, but "
218 "failed to trigger an interrupt\n", channel
->channel
);
219 tests
->eventq_dma
[channel
->channel
] = 1;
224 netif_dbg(efx
, drv
, efx
->net_dev
, "channel %d event queue passed\n",
226 tests
->eventq_dma
[channel
->channel
] = 1;
227 tests
->eventq_int
[channel
->channel
] = 1;
228 tests
->eventq_poll
[channel
->channel
] = 1;
232 static int efx_test_phy(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
237 if (!efx
->phy_op
->run_tests
)
240 mutex_lock(&efx
->mac_lock
);
241 rc
= efx
->phy_op
->run_tests(efx
, tests
->phy_ext
, flags
);
242 mutex_unlock(&efx
->mac_lock
);
246 /**************************************************************************
249 * NB Only one loopback test can be executing concurrently.
251 **************************************************************************/
253 /* Loopback test RX callback
254 * This is called for each received packet during loopback testing.
256 void efx_loopback_rx_packet(struct efx_nic
*efx
,
257 const char *buf_ptr
, int pkt_len
)
259 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
260 struct efx_loopback_payload
*received
;
261 struct efx_loopback_payload
*payload
;
265 /* If we are just flushing, then drop the packet */
266 if ((state
== NULL
) || state
->flush
)
269 payload
= &state
->payload
;
271 received
= (struct efx_loopback_payload
*) buf_ptr
;
272 received
->ip
.saddr
= payload
->ip
.saddr
;
273 if (state
->offload_csum
)
274 received
->ip
.check
= payload
->ip
.check
;
276 /* Check that header exists */
277 if (pkt_len
< sizeof(received
->header
)) {
278 netif_err(efx
, drv
, efx
->net_dev
,
279 "saw runt RX packet (length %d) in %s loopback "
280 "test\n", pkt_len
, LOOPBACK_MODE(efx
));
284 /* Check that the ethernet header exists */
285 if (memcmp(&received
->header
, &payload
->header
, ETH_HLEN
) != 0) {
286 netif_err(efx
, drv
, efx
->net_dev
,
287 "saw non-loopback RX packet in %s loopback test\n",
292 /* Check packet length */
293 if (pkt_len
!= sizeof(*payload
)) {
294 netif_err(efx
, drv
, efx
->net_dev
,
295 "saw incorrect RX packet length %d (wanted %d) in "
296 "%s loopback test\n", pkt_len
, (int)sizeof(*payload
),
301 /* Check that IP header matches */
302 if (memcmp(&received
->ip
, &payload
->ip
, sizeof(payload
->ip
)) != 0) {
303 netif_err(efx
, drv
, efx
->net_dev
,
304 "saw corrupted IP header in %s loopback test\n",
309 /* Check that msg and padding matches */
310 if (memcmp(&received
->msg
, &payload
->msg
, sizeof(received
->msg
)) != 0) {
311 netif_err(efx
, drv
, efx
->net_dev
,
312 "saw corrupted RX packet in %s loopback test\n",
317 /* Check that iteration matches */
318 if (received
->iteration
!= payload
->iteration
) {
319 netif_err(efx
, drv
, efx
->net_dev
,
320 "saw RX packet from iteration %d (wanted %d) in "
321 "%s loopback test\n", ntohs(received
->iteration
),
322 ntohs(payload
->iteration
), LOOPBACK_MODE(efx
));
326 /* Increase correct RX count */
327 netif_vdbg(efx
, drv
, efx
->net_dev
,
328 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx
));
330 atomic_inc(&state
->rx_good
);
334 #ifdef EFX_ENABLE_DEBUG
335 if (atomic_read(&state
->rx_bad
) == 0) {
336 netif_err(efx
, drv
, efx
->net_dev
, "received packet:\n");
337 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
338 buf_ptr
, pkt_len
, 0);
339 netif_err(efx
, drv
, efx
->net_dev
, "expected packet:\n");
340 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 0x10, 1,
341 &state
->payload
, sizeof(state
->payload
), 0);
344 atomic_inc(&state
->rx_bad
);
347 /* Initialise an efx_selftest_state for a new iteration */
348 static void efx_iterate_state(struct efx_nic
*efx
)
350 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
351 struct net_device
*net_dev
= efx
->net_dev
;
352 struct efx_loopback_payload
*payload
= &state
->payload
;
354 /* Initialise the layerII header */
355 memcpy(&payload
->header
.h_dest
, net_dev
->dev_addr
, ETH_ALEN
);
356 memcpy(&payload
->header
.h_source
, &payload_source
, ETH_ALEN
);
357 payload
->header
.h_proto
= htons(ETH_P_IP
);
359 /* saddr set later and used as incrementing count */
360 payload
->ip
.daddr
= htonl(INADDR_LOOPBACK
);
362 payload
->ip
.check
= htons(0xdead);
363 payload
->ip
.tot_len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
));
364 payload
->ip
.version
= IPVERSION
;
365 payload
->ip
.protocol
= IPPROTO_UDP
;
367 /* Initialise udp header */
368 payload
->udp
.source
= 0;
369 payload
->udp
.len
= htons(sizeof(*payload
) - sizeof(struct ethhdr
) -
370 sizeof(struct iphdr
));
371 payload
->udp
.check
= 0; /* checksum ignored */
373 /* Fill out payload */
374 payload
->iteration
= htons(ntohs(payload
->iteration
) + 1);
375 memcpy(&payload
->msg
, payload_msg
, sizeof(payload_msg
));
377 /* Fill out remaining state members */
378 atomic_set(&state
->rx_good
, 0);
379 atomic_set(&state
->rx_bad
, 0);
383 static int efx_begin_loopback(struct efx_tx_queue
*tx_queue
)
385 struct efx_nic
*efx
= tx_queue
->efx
;
386 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
387 struct efx_loopback_payload
*payload
;
392 /* Transmit N copies of buffer */
393 for (i
= 0; i
< state
->packet_count
; i
++) {
394 /* Allocate an skb, holding an extra reference for
395 * transmit completion counting */
396 skb
= alloc_skb(sizeof(state
->payload
), GFP_KERNEL
);
399 state
->skbs
[i
] = skb
;
402 /* Copy the payload in, incrementing the source address to
403 * exercise the rss vectors */
404 payload
= ((struct efx_loopback_payload
*)
405 skb_put(skb
, sizeof(state
->payload
)));
406 memcpy(payload
, &state
->payload
, sizeof(state
->payload
));
407 payload
->ip
.saddr
= htonl(INADDR_LOOPBACK
| (i
<< 2));
409 /* Ensure everything we've written is visible to the
410 * interrupt handler. */
413 if (efx_dev_registered(efx
))
414 netif_tx_lock_bh(efx
->net_dev
);
415 rc
= efx_enqueue_skb(tx_queue
, skb
);
416 if (efx_dev_registered(efx
))
417 netif_tx_unlock_bh(efx
->net_dev
);
419 if (rc
!= NETDEV_TX_OK
) {
420 netif_err(efx
, drv
, efx
->net_dev
,
421 "TX queue %d could not transmit packet %d of "
422 "%d in %s loopback test\n", tx_queue
->queue
,
423 i
+ 1, state
->packet_count
,
426 /* Defer cleaning up the other skbs for the caller */
435 static int efx_poll_loopback(struct efx_nic
*efx
)
437 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
438 struct efx_channel
*channel
;
440 /* NAPI polling is not enabled, so process channels
442 efx_for_each_channel(channel
, efx
) {
443 if (channel
->work_pending
)
444 efx_process_channel_now(channel
);
446 return atomic_read(&state
->rx_good
) == state
->packet_count
;
449 static int efx_end_loopback(struct efx_tx_queue
*tx_queue
,
450 struct efx_loopback_self_tests
*lb_tests
)
452 struct efx_nic
*efx
= tx_queue
->efx
;
453 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
455 int tx_done
= 0, rx_good
, rx_bad
;
458 if (efx_dev_registered(efx
))
459 netif_tx_lock_bh(efx
->net_dev
);
461 /* Count the number of tx completions, and decrement the refcnt. Any
462 * skbs not already completed will be free'd when the queue is flushed */
463 for (i
=0; i
< state
->packet_count
; i
++) {
464 skb
= state
->skbs
[i
];
465 if (skb
&& !skb_shared(skb
))
467 dev_kfree_skb_any(skb
);
470 if (efx_dev_registered(efx
))
471 netif_tx_unlock_bh(efx
->net_dev
);
473 /* Check TX completion and received packet counts */
474 rx_good
= atomic_read(&state
->rx_good
);
475 rx_bad
= atomic_read(&state
->rx_bad
);
476 if (tx_done
!= state
->packet_count
) {
477 /* Don't free the skbs; they will be picked up on TX
478 * overflow or channel teardown.
480 netif_err(efx
, drv
, efx
->net_dev
,
481 "TX queue %d saw only %d out of an expected %d "
482 "TX completion events in %s loopback test\n",
483 tx_queue
->queue
, tx_done
, state
->packet_count
,
486 /* Allow to fall through so we see the RX errors as well */
489 /* We may always be up to a flush away from our desired packet total */
490 if (rx_good
!= state
->packet_count
) {
491 netif_dbg(efx
, drv
, efx
->net_dev
,
492 "TX queue %d saw only %d out of an expected %d "
493 "received packets in %s loopback test\n",
494 tx_queue
->queue
, rx_good
, state
->packet_count
,
500 /* Update loopback test structure */
501 lb_tests
->tx_sent
[tx_queue
->queue
] += state
->packet_count
;
502 lb_tests
->tx_done
[tx_queue
->queue
] += tx_done
;
503 lb_tests
->rx_good
+= rx_good
;
504 lb_tests
->rx_bad
+= rx_bad
;
510 efx_test_loopback(struct efx_tx_queue
*tx_queue
,
511 struct efx_loopback_self_tests
*lb_tests
)
513 struct efx_nic
*efx
= tx_queue
->efx
;
514 struct efx_loopback_state
*state
= efx
->loopback_selftest
;
515 int i
, begin_rc
, end_rc
;
517 for (i
= 0; i
< 3; i
++) {
518 /* Determine how many packets to send */
519 state
->packet_count
= efx
->txq_entries
/ 3;
520 state
->packet_count
= min(1 << (i
<< 2), state
->packet_count
);
521 state
->skbs
= kzalloc(sizeof(state
->skbs
[0]) *
522 state
->packet_count
, GFP_KERNEL
);
525 state
->flush
= false;
527 netif_dbg(efx
, drv
, efx
->net_dev
,
528 "TX queue %d testing %s loopback with %d packets\n",
529 tx_queue
->queue
, LOOPBACK_MODE(efx
),
530 state
->packet_count
);
532 efx_iterate_state(efx
);
533 begin_rc
= efx_begin_loopback(tx_queue
);
535 /* This will normally complete very quickly, but be
536 * prepared to wait up to 100 ms. */
538 if (!efx_poll_loopback(efx
)) {
540 efx_poll_loopback(efx
);
543 end_rc
= efx_end_loopback(tx_queue
, lb_tests
);
546 if (begin_rc
|| end_rc
) {
547 /* Wait a while to ensure there are no packets
548 * floating around after a failure. */
549 schedule_timeout_uninterruptible(HZ
/ 10);
550 return begin_rc
? begin_rc
: end_rc
;
554 netif_dbg(efx
, drv
, efx
->net_dev
,
555 "TX queue %d passed %s loopback test with a burst length "
556 "of %d packets\n", tx_queue
->queue
, LOOPBACK_MODE(efx
),
557 state
->packet_count
);
562 /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
563 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
564 * to delay and retry. Therefore, it's safer to just poll directly. Wait
565 * for link up and any faults to dissipate. */
566 static int efx_wait_for_link(struct efx_nic
*efx
)
568 struct efx_link_state
*link_state
= &efx
->link_state
;
569 int count
, link_up_count
= 0;
572 for (count
= 0; count
< 40; count
++) {
573 schedule_timeout_uninterruptible(HZ
/ 10);
575 if (efx
->type
->monitor
!= NULL
) {
576 mutex_lock(&efx
->mac_lock
);
577 efx
->type
->monitor(efx
);
578 mutex_unlock(&efx
->mac_lock
);
580 struct efx_channel
*channel
= efx_get_channel(efx
, 0);
581 if (channel
->work_pending
)
582 efx_process_channel_now(channel
);
585 mutex_lock(&efx
->mac_lock
);
586 link_up
= link_state
->up
;
588 link_up
= !efx
->mac_op
->check_fault(efx
);
589 mutex_unlock(&efx
->mac_lock
);
592 if (++link_up_count
== 2)
602 static int efx_test_loopbacks(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
603 unsigned int loopback_modes
)
605 enum efx_loopback_mode mode
;
606 struct efx_loopback_state
*state
;
607 struct efx_channel
*channel
= efx_get_channel(efx
, 0);
608 struct efx_tx_queue
*tx_queue
;
611 /* Set the port loopback_selftest member. From this point on
612 * all received packets will be dropped. Mark the state as
613 * "flushing" so all inflight packets are dropped */
614 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
617 BUG_ON(efx
->loopback_selftest
);
619 efx
->loopback_selftest
= state
;
621 /* Test all supported loopback modes */
622 for (mode
= LOOPBACK_NONE
; mode
<= LOOPBACK_TEST_MAX
; mode
++) {
623 if (!(loopback_modes
& (1 << mode
)))
626 /* Move the port into the specified loopback mode. */
628 mutex_lock(&efx
->mac_lock
);
629 efx
->loopback_mode
= mode
;
630 rc
= __efx_reconfigure_port(efx
);
631 mutex_unlock(&efx
->mac_lock
);
633 netif_err(efx
, drv
, efx
->net_dev
,
634 "unable to move into %s loopback\n",
639 rc
= efx_wait_for_link(efx
);
641 netif_err(efx
, drv
, efx
->net_dev
,
642 "loopback %s never came up\n",
647 /* Test all enabled types of TX queue */
648 efx_for_each_channel_tx_queue(tx_queue
, channel
) {
649 state
->offload_csum
= (tx_queue
->queue
&
650 EFX_TXQ_TYPE_OFFLOAD
);
651 rc
= efx_test_loopback(tx_queue
,
652 &tests
->loopback
[mode
]);
659 /* Remove the flush. The caller will remove the loopback setting */
661 efx
->loopback_selftest
= NULL
;
668 /**************************************************************************
672 *************************************************************************/
674 int efx_selftest(struct efx_nic
*efx
, struct efx_self_tests
*tests
,
677 enum efx_loopback_mode loopback_mode
= efx
->loopback_mode
;
678 int phy_mode
= efx
->phy_mode
;
679 enum reset_type reset_method
= RESET_TYPE_INVISIBLE
;
680 struct efx_channel
*channel
;
681 int rc_test
= 0, rc_reset
= 0, rc
;
683 /* Online (i.e. non-disruptive) testing
684 * This checks interrupt generation, event delivery and PHY presence. */
686 rc
= efx_test_phy_alive(efx
, tests
);
690 rc
= efx_test_nvram(efx
, tests
);
694 rc
= efx_test_interrupts(efx
, tests
);
698 efx_for_each_channel(channel
, efx
) {
699 rc
= efx_test_eventq_irq(channel
, tests
);
707 if (!(flags
& ETH_TEST_FL_OFFLINE
))
708 return efx_test_phy(efx
, tests
, flags
);
710 /* Offline (i.e. disruptive) testing
711 * This checks MAC and PHY loopback on the specified port. */
713 /* force the carrier state off so the kernel doesn't transmit during
714 * the loopback test, and the watchdog timeout doesn't fire. Also put
715 * falcon into loopback for the register test.
717 mutex_lock(&efx
->mac_lock
);
718 efx
->port_inhibited
= true;
719 if (efx
->loopback_modes
) {
720 /* We need the 312 clock from the PHY to test the XMAC
721 * registers, so move into XGMII loopback if available */
722 if (efx
->loopback_modes
& (1 << LOOPBACK_XGMII
))
723 efx
->loopback_mode
= LOOPBACK_XGMII
;
725 efx
->loopback_mode
= __ffs(efx
->loopback_modes
);
728 __efx_reconfigure_port(efx
);
729 mutex_unlock(&efx
->mac_lock
);
731 /* free up all consumers of SRAM (including all the queues) */
732 efx_reset_down(efx
, reset_method
);
734 rc
= efx_test_chip(efx
, tests
);
738 /* reset the chip to recover from the register test */
739 rc_reset
= efx
->type
->reset(efx
, reset_method
);
741 /* Ensure that the phy is powered and out of loopback
742 * for the bist and loopback tests */
743 efx
->phy_mode
&= ~PHY_MODE_LOW_POWER
;
744 efx
->loopback_mode
= LOOPBACK_NONE
;
746 rc
= efx_reset_up(efx
, reset_method
, rc_reset
== 0);
751 netif_err(efx
, drv
, efx
->net_dev
,
752 "Unable to recover from chip test\n");
753 efx_schedule_reset(efx
, RESET_TYPE_DISABLE
);
757 rc
= efx_test_phy(efx
, tests
, flags
);
761 rc
= efx_test_loopbacks(efx
, tests
, efx
->loopback_modes
);
765 /* restore the PHY to the previous state */
766 mutex_lock(&efx
->mac_lock
);
767 efx
->phy_mode
= phy_mode
;
768 efx
->port_inhibited
= false;
769 efx
->loopback_mode
= loopback_mode
;
770 __efx_reconfigure_port(efx
);
771 mutex_unlock(&efx
->mac_lock
);