1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2006-2012 Solarflare Communications Inc.
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/pci.h>
13 #include <linux/ethtool.h>
16 #include <linux/udp.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include "net_driver.h"
23 #include "workarounds.h"
25 /* IRQ latency can be enormous because:
26 * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
27 * slow serial console or an old IDE driver doing error recovery
28 * - The PREEMPT_RT patches mostly deal with this, but also allow a
29 * tasklet or normal task to be given higher priority than our IRQ
31 * Try to avoid blaming the hardware for this.
33 #define IRQ_TIMEOUT HZ
36 * Loopback test packet structure
38 * The self-test should stress every RSS vector, and unfortunately
39 * Falcon only performs RSS on TCP/UDP packets.
41 struct ef4_loopback_payload {
42 char pad[2]; /* Ensures ip is 4-byte aligned */
43 struct_group_attr(packet, __packed,
50 } __packed __aligned(4);
51 #define EF4_LOOPBACK_PAYLOAD_LEN \
52 sizeof_field(struct ef4_loopback_payload, packet)
54 /* Loopback test source MAC address */
55 static const u8 payload_source[ETH_ALEN] __aligned(2) = {
56 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
59 static const char payload_msg[] =
60 "Hello world! This is an Efx loopback test in progress!";
62 /* Interrupt mode names */
63 static const unsigned int ef4_interrupt_mode_max = EF4_INT_MODE_MAX;
64 static const char *const ef4_interrupt_mode_names[] = {
65 [EF4_INT_MODE_MSIX] = "MSI-X",
66 [EF4_INT_MODE_MSI] = "MSI",
67 [EF4_INT_MODE_LEGACY] = "legacy",
69 #define INT_MODE(efx) \
70 STRING_TABLE_LOOKUP(efx->interrupt_mode, ef4_interrupt_mode)
73 * struct ef4_loopback_state - persistent state during a loopback selftest
74 * @flush: Drop all packets in ef4_loopback_rx_packet
75 * @packet_count: Number of packets being used in this test
76 * @skbs: An array of skbs transmitted
77 * @offload_csum: Checksums are being offloaded
78 * @rx_good: RX good packet count
79 * @rx_bad: RX bad packet count
80 * @payload: Payload used in tests
82 struct ef4_loopback_state {
85 struct sk_buff **skbs;
89 struct ef4_loopback_payload payload;
92 /* How long to wait for all the packets to arrive (in ms) */
93 #define LOOPBACK_TIMEOUT_MS 1000
95 /**************************************************************************
97 * MII, NVRAM and register tests
99 **************************************************************************/
101 static int ef4_test_phy_alive(struct ef4_nic *efx, struct ef4_self_tests *tests)
105 if (efx->phy_op->test_alive) {
106 rc = efx->phy_op->test_alive(efx);
107 tests->phy_alive = rc ? -1 : 1;
113 static int ef4_test_nvram(struct ef4_nic *efx, struct ef4_self_tests *tests)
117 if (efx->type->test_nvram) {
118 rc = efx->type->test_nvram(efx);
122 tests->nvram = rc ? -1 : 1;
128 /**************************************************************************
130 * Interrupt and event queue testing
132 **************************************************************************/
134 /* Test generation and receipt of interrupts */
135 static int ef4_test_interrupts(struct ef4_nic *efx,
136 struct ef4_self_tests *tests)
138 unsigned long timeout, wait;
142 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
143 tests->interrupt = -1;
145 rc = ef4_nic_irq_test_start(efx);
146 if (rc == -ENOTSUPP) {
147 netif_dbg(efx, drv, efx->net_dev,
148 "direct interrupt testing not supported\n");
149 tests->interrupt = 0;
153 timeout = jiffies + IRQ_TIMEOUT;
156 /* Wait for arrival of test interrupt. */
157 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
159 schedule_timeout_uninterruptible(wait);
160 cpu = ef4_nic_irq_test_irq_cpu(efx);
164 } while (time_before(jiffies, timeout));
166 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
170 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
172 tests->interrupt = 1;
176 /* Test generation and receipt of interrupting events */
177 static int ef4_test_eventq_irq(struct ef4_nic *efx,
178 struct ef4_self_tests *tests)
180 struct ef4_channel *channel;
181 unsigned int read_ptr[EF4_MAX_CHANNELS];
182 unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
183 unsigned long timeout, wait;
185 BUILD_BUG_ON(EF4_MAX_CHANNELS > BITS_PER_LONG);
187 ef4_for_each_channel(channel, efx) {
188 read_ptr[channel->channel] = channel->eventq_read_ptr;
189 set_bit(channel->channel, &dma_pend);
190 set_bit(channel->channel, &int_pend);
191 ef4_nic_event_test_start(channel);
194 timeout = jiffies + IRQ_TIMEOUT;
197 /* Wait for arrival of interrupts. NAPI processing may or may
198 * not complete in time, but we can cope in any case.
201 schedule_timeout_uninterruptible(wait);
203 ef4_for_each_channel(channel, efx) {
204 ef4_stop_eventq(channel);
205 if (channel->eventq_read_ptr !=
206 read_ptr[channel->channel]) {
207 set_bit(channel->channel, &napi_ran);
208 clear_bit(channel->channel, &dma_pend);
209 clear_bit(channel->channel, &int_pend);
211 if (ef4_nic_event_present(channel))
212 clear_bit(channel->channel, &dma_pend);
213 if (ef4_nic_event_test_irq_cpu(channel) >= 0)
214 clear_bit(channel->channel, &int_pend);
216 ef4_start_eventq(channel);
220 } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
222 ef4_for_each_channel(channel, efx) {
223 bool dma_seen = !test_bit(channel->channel, &dma_pend);
224 bool int_seen = !test_bit(channel->channel, &int_pend);
226 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
227 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
229 if (dma_seen && int_seen) {
230 netif_dbg(efx, drv, efx->net_dev,
231 "channel %d event queue passed (with%s NAPI)\n",
233 test_bit(channel->channel, &napi_ran) ?
236 /* Report failure and whether either interrupt or DMA
239 netif_err(efx, drv, efx->net_dev,
240 "channel %d timed out waiting for event queue\n",
243 netif_err(efx, drv, efx->net_dev,
244 "channel %d saw interrupt "
245 "during event queue test\n",
248 netif_err(efx, drv, efx->net_dev,
249 "channel %d event was generated, but "
250 "failed to trigger an interrupt\n",
255 return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
258 static int ef4_test_phy(struct ef4_nic *efx, struct ef4_self_tests *tests,
263 if (!efx->phy_op->run_tests)
266 mutex_lock(&efx->mac_lock);
267 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
268 mutex_unlock(&efx->mac_lock);
272 netif_info(efx, drv, efx->net_dev,
273 "%s phy selftest\n", rc ? "Failed" : "Passed");
278 /**************************************************************************
281 * NB Only one loopback test can be executing concurrently.
283 **************************************************************************/
285 /* Loopback test RX callback
286 * This is called for each received packet during loopback testing.
288 void ef4_loopback_rx_packet(struct ef4_nic *efx,
289 const char *buf_ptr, int pkt_len)
291 struct ef4_loopback_state *state = efx->loopback_selftest;
292 struct ef4_loopback_payload received;
293 struct ef4_loopback_payload *payload;
297 /* If we are just flushing, then drop the packet */
298 if ((state == NULL) || state->flush)
301 payload = &state->payload;
303 memcpy(&received.packet, buf_ptr,
304 min_t(int, pkt_len, EF4_LOOPBACK_PAYLOAD_LEN));
305 received.ip.saddr = payload->ip.saddr;
306 if (state->offload_csum)
307 received.ip.check = payload->ip.check;
309 /* Check that header exists */
310 if (pkt_len < sizeof(received.header)) {
311 netif_err(efx, drv, efx->net_dev,
312 "saw runt RX packet (length %d) in %s loopback "
313 "test\n", pkt_len, LOOPBACK_MODE(efx));
317 /* Check that the ethernet header exists */
318 if (memcmp(&received.header, &payload->header, ETH_HLEN) != 0) {
319 netif_err(efx, drv, efx->net_dev,
320 "saw non-loopback RX packet in %s loopback test\n",
325 /* Check packet length */
326 if (pkt_len != EF4_LOOPBACK_PAYLOAD_LEN) {
327 netif_err(efx, drv, efx->net_dev,
328 "saw incorrect RX packet length %d (wanted %d) in "
329 "%s loopback test\n", pkt_len,
330 (int)EF4_LOOPBACK_PAYLOAD_LEN, LOOPBACK_MODE(efx));
334 /* Check that IP header matches */
335 if (memcmp(&received.ip, &payload->ip, sizeof(payload->ip)) != 0) {
336 netif_err(efx, drv, efx->net_dev,
337 "saw corrupted IP header in %s loopback test\n",
342 /* Check that msg and padding matches */
343 if (memcmp(&received.msg, &payload->msg, sizeof(received.msg)) != 0) {
344 netif_err(efx, drv, efx->net_dev,
345 "saw corrupted RX packet in %s loopback test\n",
350 /* Check that iteration matches */
351 if (received.iteration != payload->iteration) {
352 netif_err(efx, drv, efx->net_dev,
353 "saw RX packet from iteration %d (wanted %d) in "
354 "%s loopback test\n", ntohs(received.iteration),
355 ntohs(payload->iteration), LOOPBACK_MODE(efx));
359 /* Increase correct RX count */
360 netif_vdbg(efx, drv, efx->net_dev,
361 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
363 atomic_inc(&state->rx_good);
368 if (atomic_read(&state->rx_bad) == 0) {
369 netif_err(efx, drv, efx->net_dev, "received packet:\n");
370 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
371 buf_ptr, pkt_len, 0);
372 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
373 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
374 &state->payload.packet, EF4_LOOPBACK_PAYLOAD_LEN,
378 atomic_inc(&state->rx_bad);
381 /* Initialise an ef4_selftest_state for a new iteration */
382 static void ef4_iterate_state(struct ef4_nic *efx)
384 struct ef4_loopback_state *state = efx->loopback_selftest;
385 struct net_device *net_dev = efx->net_dev;
386 struct ef4_loopback_payload *payload = &state->payload;
388 /* Initialise the layerII header */
389 ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
390 ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
391 payload->header.h_proto = htons(ETH_P_IP);
393 /* saddr set later and used as incrementing count */
394 payload->ip.daddr = htonl(INADDR_LOOPBACK);
396 payload->ip.check = (__force __sum16) htons(0xdead);
397 payload->ip.tot_len = htons(sizeof(*payload) -
398 offsetof(struct ef4_loopback_payload, ip));
399 payload->ip.version = IPVERSION;
400 payload->ip.protocol = IPPROTO_UDP;
402 /* Initialise udp header */
403 payload->udp.source = 0;
404 payload->udp.len = htons(sizeof(*payload) -
405 offsetof(struct ef4_loopback_payload, udp));
406 payload->udp.check = 0; /* checksum ignored */
408 /* Fill out payload */
409 payload->iteration = htons(ntohs(payload->iteration) + 1);
410 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
412 /* Fill out remaining state members */
413 atomic_set(&state->rx_good, 0);
414 atomic_set(&state->rx_bad, 0);
418 static int ef4_begin_loopback(struct ef4_tx_queue *tx_queue)
420 struct ef4_nic *efx = tx_queue->efx;
421 struct ef4_loopback_state *state = efx->loopback_selftest;
422 struct ef4_loopback_payload *payload;
427 /* Transmit N copies of buffer */
428 for (i = 0; i < state->packet_count; i++) {
429 /* Allocate an skb, holding an extra reference for
430 * transmit completion counting */
431 skb = alloc_skb(EF4_LOOPBACK_PAYLOAD_LEN, GFP_KERNEL);
434 state->skbs[i] = skb;
437 /* Copy the payload in, incrementing the source address to
438 * exercise the rss vectors */
439 payload = skb_put(skb, sizeof(state->payload));
440 memcpy(payload, &state->payload, sizeof(state->payload));
441 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
442 /* Strip off the leading padding */
443 skb_pull(skb, offsetof(struct ef4_loopback_payload, header));
444 /* Strip off the trailing padding */
445 skb_trim(skb, EF4_LOOPBACK_PAYLOAD_LEN);
447 /* Ensure everything we've written is visible to the
448 * interrupt handler. */
451 netif_tx_lock_bh(efx->net_dev);
452 rc = ef4_enqueue_skb(tx_queue, skb);
453 netif_tx_unlock_bh(efx->net_dev);
455 if (rc != NETDEV_TX_OK) {
456 netif_err(efx, drv, efx->net_dev,
457 "TX queue %d could not transmit packet %d of "
458 "%d in %s loopback test\n", tx_queue->queue,
459 i + 1, state->packet_count,
462 /* Defer cleaning up the other skbs for the caller */
471 static int ef4_poll_loopback(struct ef4_nic *efx)
473 struct ef4_loopback_state *state = efx->loopback_selftest;
475 return atomic_read(&state->rx_good) == state->packet_count;
478 static int ef4_end_loopback(struct ef4_tx_queue *tx_queue,
479 struct ef4_loopback_self_tests *lb_tests)
481 struct ef4_nic *efx = tx_queue->efx;
482 struct ef4_loopback_state *state = efx->loopback_selftest;
484 int tx_done = 0, rx_good, rx_bad;
487 netif_tx_lock_bh(efx->net_dev);
489 /* Count the number of tx completions, and decrement the refcnt. Any
490 * skbs not already completed will be free'd when the queue is flushed */
491 for (i = 0; i < state->packet_count; i++) {
492 skb = state->skbs[i];
493 if (skb && !skb_shared(skb))
498 netif_tx_unlock_bh(efx->net_dev);
500 /* Check TX completion and received packet counts */
501 rx_good = atomic_read(&state->rx_good);
502 rx_bad = atomic_read(&state->rx_bad);
503 if (tx_done != state->packet_count) {
504 /* Don't free the skbs; they will be picked up on TX
505 * overflow or channel teardown.
507 netif_err(efx, drv, efx->net_dev,
508 "TX queue %d saw only %d out of an expected %d "
509 "TX completion events in %s loopback test\n",
510 tx_queue->queue, tx_done, state->packet_count,
513 /* Allow to fall through so we see the RX errors as well */
516 /* We may always be up to a flush away from our desired packet total */
517 if (rx_good != state->packet_count) {
518 netif_dbg(efx, drv, efx->net_dev,
519 "TX queue %d saw only %d out of an expected %d "
520 "received packets in %s loopback test\n",
521 tx_queue->queue, rx_good, state->packet_count,
527 /* Update loopback test structure */
528 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
529 lb_tests->tx_done[tx_queue->queue] += tx_done;
530 lb_tests->rx_good += rx_good;
531 lb_tests->rx_bad += rx_bad;
537 ef4_test_loopback(struct ef4_tx_queue *tx_queue,
538 struct ef4_loopback_self_tests *lb_tests)
540 struct ef4_nic *efx = tx_queue->efx;
541 struct ef4_loopback_state *state = efx->loopback_selftest;
542 int i, begin_rc, end_rc;
544 for (i = 0; i < 3; i++) {
545 /* Determine how many packets to send */
546 state->packet_count = efx->txq_entries / 3;
547 state->packet_count = min(1 << (i << 2), state->packet_count);
548 state->skbs = kcalloc(state->packet_count,
549 sizeof(state->skbs[0]), GFP_KERNEL);
552 state->flush = false;
554 netif_dbg(efx, drv, efx->net_dev,
555 "TX queue %d testing %s loopback with %d packets\n",
556 tx_queue->queue, LOOPBACK_MODE(efx),
557 state->packet_count);
559 ef4_iterate_state(efx);
560 begin_rc = ef4_begin_loopback(tx_queue);
562 /* This will normally complete very quickly, but be
563 * prepared to wait much longer. */
565 if (!ef4_poll_loopback(efx)) {
566 msleep(LOOPBACK_TIMEOUT_MS);
567 ef4_poll_loopback(efx);
570 end_rc = ef4_end_loopback(tx_queue, lb_tests);
573 if (begin_rc || end_rc) {
574 /* Wait a while to ensure there are no packets
575 * floating around after a failure. */
576 schedule_timeout_uninterruptible(HZ / 10);
577 return begin_rc ? begin_rc : end_rc;
581 netif_dbg(efx, drv, efx->net_dev,
582 "TX queue %d passed %s loopback test with a burst length "
583 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
584 state->packet_count);
589 /* Wait for link up. On Falcon, we would prefer to rely on ef4_monitor, but
590 * any contention on the mac lock (via e.g. ef4_mac_mcast_work) causes it
591 * to delay and retry. Therefore, it's safer to just poll directly. Wait
592 * for link up and any faults to dissipate. */
593 static int ef4_wait_for_link(struct ef4_nic *efx)
595 struct ef4_link_state *link_state = &efx->link_state;
596 int count, link_up_count = 0;
599 for (count = 0; count < 40; count++) {
600 schedule_timeout_uninterruptible(HZ / 10);
602 if (efx->type->monitor != NULL) {
603 mutex_lock(&efx->mac_lock);
604 efx->type->monitor(efx);
605 mutex_unlock(&efx->mac_lock);
608 mutex_lock(&efx->mac_lock);
609 link_up = link_state->up;
611 link_up = !efx->type->check_mac_fault(efx);
612 mutex_unlock(&efx->mac_lock);
615 if (++link_up_count == 2)
625 static int ef4_test_loopbacks(struct ef4_nic *efx, struct ef4_self_tests *tests,
626 unsigned int loopback_modes)
628 enum ef4_loopback_mode mode;
629 struct ef4_loopback_state *state;
630 struct ef4_channel *channel =
631 ef4_get_channel(efx, efx->tx_channel_offset);
632 struct ef4_tx_queue *tx_queue;
635 /* Set the port loopback_selftest member. From this point on
636 * all received packets will be dropped. Mark the state as
637 * "flushing" so all inflight packets are dropped */
638 state = kzalloc(sizeof(*state), GFP_KERNEL);
641 BUG_ON(efx->loopback_selftest);
643 efx->loopback_selftest = state;
645 /* Test all supported loopback modes */
646 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
647 if (!(loopback_modes & (1 << mode)))
650 /* Move the port into the specified loopback mode. */
652 mutex_lock(&efx->mac_lock);
653 efx->loopback_mode = mode;
654 rc = __ef4_reconfigure_port(efx);
655 mutex_unlock(&efx->mac_lock);
657 netif_err(efx, drv, efx->net_dev,
658 "unable to move into %s loopback\n",
663 rc = ef4_wait_for_link(efx);
665 netif_err(efx, drv, efx->net_dev,
666 "loopback %s never came up\n",
671 /* Test all enabled types of TX queue */
672 ef4_for_each_channel_tx_queue(tx_queue, channel) {
673 state->offload_csum = (tx_queue->queue &
674 EF4_TXQ_TYPE_OFFLOAD);
675 rc = ef4_test_loopback(tx_queue,
676 &tests->loopback[mode]);
683 /* Remove the flush. The caller will remove the loopback setting */
685 efx->loopback_selftest = NULL;
695 /**************************************************************************
699 *************************************************************************/
701 int ef4_selftest(struct ef4_nic *efx, struct ef4_self_tests *tests,
704 enum ef4_loopback_mode loopback_mode = efx->loopback_mode;
705 int phy_mode = efx->phy_mode;
706 int rc_test = 0, rc_reset, rc;
708 ef4_selftest_async_cancel(efx);
710 /* Online (i.e. non-disruptive) testing
711 * This checks interrupt generation, event delivery and PHY presence. */
713 rc = ef4_test_phy_alive(efx, tests);
717 rc = ef4_test_nvram(efx, tests);
721 rc = ef4_test_interrupts(efx, tests);
725 rc = ef4_test_eventq_irq(efx, tests);
732 if (!(flags & ETH_TEST_FL_OFFLINE))
733 return ef4_test_phy(efx, tests, flags);
735 /* Offline (i.e. disruptive) testing
736 * This checks MAC and PHY loopback on the specified port. */
738 /* Detach the device so the kernel doesn't transmit during the
739 * loopback test and the watchdog timeout doesn't fire.
741 ef4_device_detach_sync(efx);
743 if (efx->type->test_chip) {
744 rc_reset = efx->type->test_chip(efx, tests);
746 netif_err(efx, hw, efx->net_dev,
747 "Unable to recover from chip test\n");
748 ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
752 if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
756 /* Ensure that the phy is powered and out of loopback
757 * for the bist and loopback tests */
758 mutex_lock(&efx->mac_lock);
759 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
760 efx->loopback_mode = LOOPBACK_NONE;
761 __ef4_reconfigure_port(efx);
762 mutex_unlock(&efx->mac_lock);
764 rc = ef4_test_phy(efx, tests, flags);
768 rc = ef4_test_loopbacks(efx, tests, efx->loopback_modes);
772 /* restore the PHY to the previous state */
773 mutex_lock(&efx->mac_lock);
774 efx->phy_mode = phy_mode;
775 efx->loopback_mode = loopback_mode;
776 __ef4_reconfigure_port(efx);
777 mutex_unlock(&efx->mac_lock);
779 netif_device_attach(efx->net_dev);
784 void ef4_selftest_async_start(struct ef4_nic *efx)
786 struct ef4_channel *channel;
788 ef4_for_each_channel(channel, efx)
789 ef4_nic_event_test_start(channel);
790 schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
793 void ef4_selftest_async_cancel(struct ef4_nic *efx)
795 cancel_delayed_work_sync(&efx->selftest_work);
798 void ef4_selftest_async_work(struct work_struct *data)
800 struct ef4_nic *efx = container_of(data, struct ef4_nic,
802 struct ef4_channel *channel;
805 ef4_for_each_channel(channel, efx) {
806 cpu = ef4_nic_event_test_irq_cpu(channel);
808 netif_err(efx, ifup, efx->net_dev,
809 "channel %d failed to trigger an interrupt\n",
812 netif_dbg(efx, ifup, efx->net_dev,
813 "channel %d triggered interrupt on CPU %d\n",
814 channel->channel, cpu);