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"
30 #include "falcon_io.h"
34 * Loopback test packet structure
36 * The self-test should stress every RSS vector, and unfortunately
37 * Falcon only performs RSS on TCP/UDP packets.
39 struct efx_loopback_payload {
45 } __attribute__ ((packed));
47 /* Loopback test source MAC address */
48 static const unsigned char payload_source[ETH_ALEN] = {
49 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
52 static const char *payload_msg =
53 "Hello world! This is an Efx loopback test in progress!";
56 * efx_loopback_state - persistent state during a loopback selftest
57 * @flush: Drop all packets in efx_loopback_rx_packet
58 * @packet_count: Number of packets being used in this test
59 * @skbs: An array of skbs transmitted
60 * @rx_good: RX good packet count
61 * @rx_bad: RX bad packet count
62 * @payload: Payload used in tests
64 struct efx_loopback_state {
67 struct sk_buff **skbs;
69 /* Checksums are being offloaded */
74 struct efx_loopback_payload payload;
77 /**************************************************************************
79 * MII, NVRAM and register tests
81 **************************************************************************/
83 static int efx_test_mii(struct efx_nic *efx, struct efx_self_tests *tests)
87 struct mii_if_info *mii = &efx->mii;
88 struct net_device *net_dev = efx->net_dev;
90 if (efx->phy_type == PHY_TYPE_NONE)
93 mutex_lock(&efx->mac_lock);
96 physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
97 physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
99 if ((physid1 == 0x0000) || (physid1 == 0xffff) ||
100 (physid2 == 0x0000) || (physid2 == 0xffff)) {
101 EFX_ERR(efx, "no MII PHY present with ID %d\n",
107 if (EFX_IS10G(efx)) {
108 rc = mdio_clause45_check_mmds(efx, efx->phy_op->mmds, 0);
114 mutex_unlock(&efx->mac_lock);
115 tests->mii = rc ? -1 : 1;
119 static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
123 rc = falcon_read_nvram(efx, NULL);
124 tests->nvram = rc ? -1 : 1;
128 static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
132 /* Not supported on A-series silicon */
133 if (falcon_rev(efx) < FALCON_REV_B0)
136 rc = falcon_test_registers(efx);
137 tests->registers = rc ? -1 : 1;
141 /**************************************************************************
143 * Interrupt and event queue testing
145 **************************************************************************/
147 /* Test generation and receipt of interrupts */
148 static int efx_test_interrupts(struct efx_nic *efx,
149 struct efx_self_tests *tests)
151 struct efx_channel *channel;
153 EFX_LOG(efx, "testing interrupts\n");
154 tests->interrupt = -1;
156 /* Reset interrupt flag */
157 efx->last_irq_cpu = -1;
160 /* ACK each interrupting event queue. Receiving an interrupt due to
161 * traffic before a test event is raised is considered a pass */
162 efx_for_each_channel(channel, efx) {
163 if (channel->work_pending)
164 efx_process_channel_now(channel);
165 if (efx->last_irq_cpu >= 0)
169 falcon_generate_interrupt(efx);
171 /* Wait for arrival of test interrupt. */
172 EFX_LOG(efx, "waiting for test interrupt\n");
173 schedule_timeout_uninterruptible(HZ / 10);
174 if (efx->last_irq_cpu >= 0)
177 EFX_ERR(efx, "timed out waiting for interrupt\n");
181 EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
182 efx->interrupt_mode, efx->last_irq_cpu);
183 tests->interrupt = 1;
187 /* Test generation and receipt of interrupting events */
188 static int efx_test_eventq_irq(struct efx_channel *channel,
189 struct efx_self_tests *tests)
191 unsigned int magic, count;
193 /* Channel specific code, limited to 20 bits */
194 magic = (0x00010150 + channel->channel);
195 EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
196 channel->channel, magic);
198 tests->eventq_dma[channel->channel] = -1;
199 tests->eventq_int[channel->channel] = -1;
200 tests->eventq_poll[channel->channel] = -1;
202 /* Reset flag and zero magic word */
203 channel->efx->last_irq_cpu = -1;
204 channel->eventq_magic = 0;
207 falcon_generate_test_event(channel, magic);
209 /* Wait for arrival of interrupt */
212 schedule_timeout_uninterruptible(HZ / 100);
214 if (channel->work_pending)
215 efx_process_channel_now(channel);
217 if (channel->eventq_magic == magic)
219 } while (++count < 2);
221 EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
224 /* See if interrupt arrived */
225 if (channel->efx->last_irq_cpu >= 0) {
226 EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
227 "during event queue test\n", channel->channel,
228 raw_smp_processor_id());
229 tests->eventq_int[channel->channel] = 1;
232 /* Check to see if event was received even if interrupt wasn't */
233 efx_process_channel_now(channel);
234 if (channel->eventq_magic == magic) {
235 EFX_ERR(channel->efx, "channel %d event was generated, but "
236 "failed to trigger an interrupt\n", channel->channel);
237 tests->eventq_dma[channel->channel] = 1;
242 EFX_LOG(channel->efx, "channel %d event queue passed\n",
244 tests->eventq_dma[channel->channel] = 1;
245 tests->eventq_int[channel->channel] = 1;
246 tests->eventq_poll[channel->channel] = 1;
250 static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
255 if (!efx->phy_op->run_tests)
258 EFX_BUG_ON_PARANOID(efx->phy_op->num_tests == 0 ||
259 efx->phy_op->num_tests > EFX_MAX_PHY_TESTS);
261 mutex_lock(&efx->mac_lock);
262 rc = efx->phy_op->run_tests(efx, tests->phy, flags);
263 mutex_unlock(&efx->mac_lock);
267 /**************************************************************************
270 * NB Only one loopback test can be executing concurrently.
272 **************************************************************************/
274 /* Loopback test RX callback
275 * This is called for each received packet during loopback testing.
277 void efx_loopback_rx_packet(struct efx_nic *efx,
278 const char *buf_ptr, int pkt_len)
280 struct efx_loopback_state *state = efx->loopback_selftest;
281 struct efx_loopback_payload *received;
282 struct efx_loopback_payload *payload;
286 /* If we are just flushing, then drop the packet */
287 if ((state == NULL) || state->flush)
290 payload = &state->payload;
292 received = (struct efx_loopback_payload *) buf_ptr;
293 received->ip.saddr = payload->ip.saddr;
294 if (state->offload_csum)
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_loopback_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_begin_loopback(struct efx_tx_queue *tx_queue)
400 struct efx_nic *efx = tx_queue->efx;
401 struct efx_loopback_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 (efx_dev_registered(efx))
428 netif_tx_lock_bh(efx->net_dev);
429 rc = efx_xmit(efx, tx_queue, skb);
430 if (efx_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_poll_loopback(struct efx_nic *efx)
449 struct efx_loopback_state *state = efx->loopback_selftest;
450 struct efx_channel *channel;
452 /* NAPI polling is not enabled, so process channels
454 efx_for_each_channel(channel, efx) {
455 if (channel->work_pending)
456 efx_process_channel_now(channel);
458 return atomic_read(&state->rx_good) == state->packet_count;
461 static int efx_end_loopback(struct efx_tx_queue *tx_queue,
462 struct efx_loopback_self_tests *lb_tests)
464 struct efx_nic *efx = tx_queue->efx;
465 struct efx_loopback_state *state = efx->loopback_selftest;
467 int tx_done = 0, rx_good, rx_bad;
470 if (efx_dev_registered(efx))
471 netif_tx_lock_bh(efx->net_dev);
473 /* Count the number of tx completions, and decrement the refcnt. Any
474 * skbs not already completed will be free'd when the queue is flushed */
475 for (i=0; i < state->packet_count; i++) {
476 skb = state->skbs[i];
477 if (skb && !skb_shared(skb))
479 dev_kfree_skb_any(skb);
482 if (efx_dev_registered(efx))
483 netif_tx_unlock_bh(efx->net_dev);
485 /* Check TX completion and received packet counts */
486 rx_good = atomic_read(&state->rx_good);
487 rx_bad = atomic_read(&state->rx_bad);
488 if (tx_done != state->packet_count) {
489 /* Don't free the skbs; they will be picked up on TX
490 * overflow or channel teardown.
492 EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
493 "TX completion events in %s loopback test\n",
494 tx_queue->queue, tx_done, state->packet_count,
497 /* Allow to fall through so we see the RX errors as well */
500 /* We may always be up to a flush away from our desired packet total */
501 if (rx_good != state->packet_count) {
502 EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
503 "received packets in %s loopback test\n",
504 tx_queue->queue, rx_good, state->packet_count,
510 /* Update loopback test structure */
511 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
512 lb_tests->tx_done[tx_queue->queue] += tx_done;
513 lb_tests->rx_good += rx_good;
514 lb_tests->rx_bad += rx_bad;
520 efx_test_loopback(struct efx_tx_queue *tx_queue,
521 struct efx_loopback_self_tests *lb_tests)
523 struct efx_nic *efx = tx_queue->efx;
524 struct efx_loopback_state *state = efx->loopback_selftest;
525 int i, begin_rc, end_rc;
527 for (i = 0; i < 3; i++) {
528 /* Determine how many packets to send */
529 state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
530 state->packet_count = min(1 << (i << 2), state->packet_count);
531 state->skbs = kzalloc(sizeof(state->skbs[0]) *
532 state->packet_count, GFP_KERNEL);
535 state->flush = false;
537 EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
538 "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
539 state->packet_count);
541 efx_iterate_state(efx);
542 begin_rc = efx_begin_loopback(tx_queue);
544 /* This will normally complete very quickly, but be
545 * prepared to wait up to 100 ms. */
547 if (!efx_poll_loopback(efx)) {
549 efx_poll_loopback(efx);
552 end_rc = efx_end_loopback(tx_queue, lb_tests);
555 if (begin_rc || end_rc) {
556 /* Wait a while to ensure there are no packets
557 * floating around after a failure. */
558 schedule_timeout_uninterruptible(HZ / 10);
559 return begin_rc ? begin_rc : end_rc;
563 EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
564 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
565 state->packet_count);
570 static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
571 unsigned int loopback_modes)
573 enum efx_loopback_mode mode;
574 struct efx_loopback_state *state;
575 struct efx_tx_queue *tx_queue;
579 /* Set the port loopback_selftest member. From this point on
580 * all received packets will be dropped. Mark the state as
581 * "flushing" so all inflight packets are dropped */
582 state = kzalloc(sizeof(*state), GFP_KERNEL);
585 BUG_ON(efx->loopback_selftest);
587 efx->loopback_selftest = state;
589 /* Test all supported loopback modes */
590 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
591 if (!(loopback_modes & (1 << mode)))
594 /* Move the port into the specified loopback mode. */
596 efx->loopback_mode = mode;
597 efx_reconfigure_port(efx);
599 /* Wait for the PHY to signal the link is up. Interrupts
600 * are enabled for PHY's using LASI, otherwise we poll()
604 struct efx_channel *channel = &efx->channel[0];
606 efx->phy_op->poll(efx);
607 schedule_timeout_uninterruptible(HZ / 10);
608 if (channel->work_pending)
609 efx_process_channel_now(channel);
610 /* Wait for PHY events to be processed */
611 flush_workqueue(efx->workqueue);
614 /* We need both the phy and xaui links to be ok.
615 * rather than relying on the falcon_xmac irq/poll
616 * regime, just poll xaui directly */
617 link_up = efx->link_up;
618 if (link_up && EFX_IS10G(efx) &&
619 !falcon_xaui_link_ok(efx))
622 } while ((++count < 20) && !link_up);
624 /* The link should now be up. If it isn't, there is no point
625 * in attempting a loopback test */
627 EFX_ERR(efx, "loopback %s never came up\n",
633 EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
634 LOOPBACK_MODE(efx), count);
636 /* Test every TX queue */
637 efx_for_each_tx_queue(tx_queue, efx) {
638 state->offload_csum = (tx_queue->queue ==
639 EFX_TX_QUEUE_OFFLOAD_CSUM);
640 rc = efx_test_loopback(tx_queue,
641 &tests->loopback[mode]);
648 /* Remove the flush. The caller will remove the loopback setting */
650 efx->loopback_selftest = NULL;
657 /**************************************************************************
661 *************************************************************************/
663 int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
666 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
667 int phy_mode = efx->phy_mode;
668 enum reset_type reset_method = RESET_TYPE_INVISIBLE;
669 struct ethtool_cmd ecmd;
670 struct efx_channel *channel;
671 int rc_test = 0, rc_reset = 0, rc;
673 /* Online (i.e. non-disruptive) testing
674 * This checks interrupt generation, event delivery and PHY presence. */
676 rc = efx_test_mii(efx, tests);
680 rc = efx_test_nvram(efx, tests);
684 rc = efx_test_interrupts(efx, tests);
688 efx_for_each_channel(channel, efx) {
689 rc = efx_test_eventq_irq(channel, tests);
697 if (!(flags & ETH_TEST_FL_OFFLINE))
698 return efx_test_phy(efx, tests, flags);
700 /* Offline (i.e. disruptive) testing
701 * This checks MAC and PHY loopback on the specified port. */
703 /* force the carrier state off so the kernel doesn't transmit during
704 * the loopback test, and the watchdog timeout doesn't fire. Also put
705 * falcon into loopback for the register test.
707 mutex_lock(&efx->mac_lock);
708 efx->port_inhibited = true;
709 if (efx->loopback_modes) {
710 /* We need the 312 clock from the PHY to test the XMAC
711 * registers, so move into XGMII loopback if available */
712 if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
713 efx->loopback_mode = LOOPBACK_XGMII;
715 efx->loopback_mode = __ffs(efx->loopback_modes);
718 __efx_reconfigure_port(efx);
719 mutex_unlock(&efx->mac_lock);
721 /* free up all consumers of SRAM (including all the queues) */
722 efx_reset_down(efx, reset_method, &ecmd);
724 rc = efx_test_chip(efx, tests);
728 /* reset the chip to recover from the register test */
729 rc_reset = falcon_reset_hw(efx, reset_method);
731 /* Ensure that the phy is powered and out of loopback
732 * for the bist and loopback tests */
733 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
734 efx->loopback_mode = LOOPBACK_NONE;
736 rc = efx_reset_up(efx, reset_method, &ecmd, rc_reset == 0);
741 EFX_ERR(efx, "Unable to recover from chip test\n");
742 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
746 rc = efx_test_phy(efx, tests, flags);
750 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
754 /* restore the PHY to the previous state */
755 efx->loopback_mode = loopback_mode;
756 efx->phy_mode = phy_mode;
757 efx->port_inhibited = false;
758 efx_ethtool_set_settings(efx->net_dev, &ecmd);