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 */
48 } __packed __aligned(4);
49 #define EF4_LOOPBACK_PAYLOAD_LEN (sizeof(struct ef4_loopback_payload) - \
50 offsetof(struct ef4_loopback_payload, \
53 /* Loopback test source MAC address */
54 static const u8 payload_source[ETH_ALEN] __aligned(2) = {
55 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
58 static const char payload_msg[] =
59 "Hello world! This is an Efx loopback test in progress!";
61 /* Interrupt mode names */
62 static const unsigned int ef4_interrupt_mode_max = EF4_INT_MODE_MAX;
63 static const char *const ef4_interrupt_mode_names[] = {
64 [EF4_INT_MODE_MSIX] = "MSI-X",
65 [EF4_INT_MODE_MSI] = "MSI",
66 [EF4_INT_MODE_LEGACY] = "legacy",
68 #define INT_MODE(efx) \
69 STRING_TABLE_LOOKUP(efx->interrupt_mode, ef4_interrupt_mode)
72 * struct ef4_loopback_state - persistent state during a loopback selftest
73 * @flush: Drop all packets in ef4_loopback_rx_packet
74 * @packet_count: Number of packets being used in this test
75 * @skbs: An array of skbs transmitted
76 * @offload_csum: Checksums are being offloaded
77 * @rx_good: RX good packet count
78 * @rx_bad: RX bad packet count
79 * @payload: Payload used in tests
81 struct ef4_loopback_state {
84 struct sk_buff **skbs;
88 struct ef4_loopback_payload payload;
91 /* How long to wait for all the packets to arrive (in ms) */
92 #define LOOPBACK_TIMEOUT_MS 1000
94 /**************************************************************************
96 * MII, NVRAM and register tests
98 **************************************************************************/
100 static int ef4_test_phy_alive(struct ef4_nic *efx, struct ef4_self_tests *tests)
104 if (efx->phy_op->test_alive) {
105 rc = efx->phy_op->test_alive(efx);
106 tests->phy_alive = rc ? -1 : 1;
112 static int ef4_test_nvram(struct ef4_nic *efx, struct ef4_self_tests *tests)
116 if (efx->type->test_nvram) {
117 rc = efx->type->test_nvram(efx);
121 tests->nvram = rc ? -1 : 1;
127 /**************************************************************************
129 * Interrupt and event queue testing
131 **************************************************************************/
133 /* Test generation and receipt of interrupts */
134 static int ef4_test_interrupts(struct ef4_nic *efx,
135 struct ef4_self_tests *tests)
137 unsigned long timeout, wait;
141 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
142 tests->interrupt = -1;
144 rc = ef4_nic_irq_test_start(efx);
145 if (rc == -ENOTSUPP) {
146 netif_dbg(efx, drv, efx->net_dev,
147 "direct interrupt testing not supported\n");
148 tests->interrupt = 0;
152 timeout = jiffies + IRQ_TIMEOUT;
155 /* Wait for arrival of test interrupt. */
156 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
158 schedule_timeout_uninterruptible(wait);
159 cpu = ef4_nic_irq_test_irq_cpu(efx);
163 } while (time_before(jiffies, timeout));
165 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
169 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
171 tests->interrupt = 1;
175 /* Test generation and receipt of interrupting events */
176 static int ef4_test_eventq_irq(struct ef4_nic *efx,
177 struct ef4_self_tests *tests)
179 struct ef4_channel *channel;
180 unsigned int read_ptr[EF4_MAX_CHANNELS];
181 unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
182 unsigned long timeout, wait;
184 BUILD_BUG_ON(EF4_MAX_CHANNELS > BITS_PER_LONG);
186 ef4_for_each_channel(channel, efx) {
187 read_ptr[channel->channel] = channel->eventq_read_ptr;
188 set_bit(channel->channel, &dma_pend);
189 set_bit(channel->channel, &int_pend);
190 ef4_nic_event_test_start(channel);
193 timeout = jiffies + IRQ_TIMEOUT;
196 /* Wait for arrival of interrupts. NAPI processing may or may
197 * not complete in time, but we can cope in any case.
200 schedule_timeout_uninterruptible(wait);
202 ef4_for_each_channel(channel, efx) {
203 ef4_stop_eventq(channel);
204 if (channel->eventq_read_ptr !=
205 read_ptr[channel->channel]) {
206 set_bit(channel->channel, &napi_ran);
207 clear_bit(channel->channel, &dma_pend);
208 clear_bit(channel->channel, &int_pend);
210 if (ef4_nic_event_present(channel))
211 clear_bit(channel->channel, &dma_pend);
212 if (ef4_nic_event_test_irq_cpu(channel) >= 0)
213 clear_bit(channel->channel, &int_pend);
215 ef4_start_eventq(channel);
219 } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
221 ef4_for_each_channel(channel, efx) {
222 bool dma_seen = !test_bit(channel->channel, &dma_pend);
223 bool int_seen = !test_bit(channel->channel, &int_pend);
225 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
226 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
228 if (dma_seen && int_seen) {
229 netif_dbg(efx, drv, efx->net_dev,
230 "channel %d event queue passed (with%s NAPI)\n",
232 test_bit(channel->channel, &napi_ran) ?
235 /* Report failure and whether either interrupt or DMA
238 netif_err(efx, drv, efx->net_dev,
239 "channel %d timed out waiting for event queue\n",
242 netif_err(efx, drv, efx->net_dev,
243 "channel %d saw interrupt "
244 "during event queue test\n",
247 netif_err(efx, drv, efx->net_dev,
248 "channel %d event was generated, but "
249 "failed to trigger an interrupt\n",
254 return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
257 static int ef4_test_phy(struct ef4_nic *efx, struct ef4_self_tests *tests,
262 if (!efx->phy_op->run_tests)
265 mutex_lock(&efx->mac_lock);
266 rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
267 mutex_unlock(&efx->mac_lock);
271 netif_info(efx, drv, efx->net_dev,
272 "%s phy selftest\n", rc ? "Failed" : "Passed");
277 /**************************************************************************
280 * NB Only one loopback test can be executing concurrently.
282 **************************************************************************/
284 /* Loopback test RX callback
285 * This is called for each received packet during loopback testing.
287 void ef4_loopback_rx_packet(struct ef4_nic *efx,
288 const char *buf_ptr, int pkt_len)
290 struct ef4_loopback_state *state = efx->loopback_selftest;
291 struct ef4_loopback_payload received;
292 struct ef4_loopback_payload *payload;
296 /* If we are just flushing, then drop the packet */
297 if ((state == NULL) || state->flush)
300 payload = &state->payload;
302 memcpy(&received.header, buf_ptr,
303 min_t(int, pkt_len, EF4_LOOPBACK_PAYLOAD_LEN));
304 received.ip.saddr = payload->ip.saddr;
305 if (state->offload_csum)
306 received.ip.check = payload->ip.check;
308 /* Check that header exists */
309 if (pkt_len < sizeof(received.header)) {
310 netif_err(efx, drv, efx->net_dev,
311 "saw runt RX packet (length %d) in %s loopback "
312 "test\n", pkt_len, LOOPBACK_MODE(efx));
316 /* Check that the ethernet header exists */
317 if (memcmp(&received.header, &payload->header, ETH_HLEN) != 0) {
318 netif_err(efx, drv, efx->net_dev,
319 "saw non-loopback RX packet in %s loopback test\n",
324 /* Check packet length */
325 if (pkt_len != EF4_LOOPBACK_PAYLOAD_LEN) {
326 netif_err(efx, drv, efx->net_dev,
327 "saw incorrect RX packet length %d (wanted %d) in "
328 "%s loopback test\n", pkt_len,
329 (int)EF4_LOOPBACK_PAYLOAD_LEN, LOOPBACK_MODE(efx));
333 /* Check that IP header matches */
334 if (memcmp(&received.ip, &payload->ip, sizeof(payload->ip)) != 0) {
335 netif_err(efx, drv, efx->net_dev,
336 "saw corrupted IP header in %s loopback test\n",
341 /* Check that msg and padding matches */
342 if (memcmp(&received.msg, &payload->msg, sizeof(received.msg)) != 0) {
343 netif_err(efx, drv, efx->net_dev,
344 "saw corrupted RX packet in %s loopback test\n",
349 /* Check that iteration matches */
350 if (received.iteration != payload->iteration) {
351 netif_err(efx, drv, efx->net_dev,
352 "saw RX packet from iteration %d (wanted %d) in "
353 "%s loopback test\n", ntohs(received.iteration),
354 ntohs(payload->iteration), LOOPBACK_MODE(efx));
358 /* Increase correct RX count */
359 netif_vdbg(efx, drv, efx->net_dev,
360 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
362 atomic_inc(&state->rx_good);
367 if (atomic_read(&state->rx_bad) == 0) {
368 netif_err(efx, drv, efx->net_dev, "received packet:\n");
369 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
370 buf_ptr, pkt_len, 0);
371 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
372 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
373 &state->payload.header, EF4_LOOPBACK_PAYLOAD_LEN,
377 atomic_inc(&state->rx_bad);
380 /* Initialise an ef4_selftest_state for a new iteration */
381 static void ef4_iterate_state(struct ef4_nic *efx)
383 struct ef4_loopback_state *state = efx->loopback_selftest;
384 struct net_device *net_dev = efx->net_dev;
385 struct ef4_loopback_payload *payload = &state->payload;
387 /* Initialise the layerII header */
388 ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
389 ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
390 payload->header.h_proto = htons(ETH_P_IP);
392 /* saddr set later and used as incrementing count */
393 payload->ip.daddr = htonl(INADDR_LOOPBACK);
395 payload->ip.check = (__force __sum16) htons(0xdead);
396 payload->ip.tot_len = htons(sizeof(*payload) -
397 offsetof(struct ef4_loopback_payload, ip));
398 payload->ip.version = IPVERSION;
399 payload->ip.protocol = IPPROTO_UDP;
401 /* Initialise udp header */
402 payload->udp.source = 0;
403 payload->udp.len = htons(sizeof(*payload) -
404 offsetof(struct ef4_loopback_payload, udp));
405 payload->udp.check = 0; /* checksum ignored */
407 /* Fill out payload */
408 payload->iteration = htons(ntohs(payload->iteration) + 1);
409 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
411 /* Fill out remaining state members */
412 atomic_set(&state->rx_good, 0);
413 atomic_set(&state->rx_bad, 0);
417 static int ef4_begin_loopback(struct ef4_tx_queue *tx_queue)
419 struct ef4_nic *efx = tx_queue->efx;
420 struct ef4_loopback_state *state = efx->loopback_selftest;
421 struct ef4_loopback_payload *payload;
426 /* Transmit N copies of buffer */
427 for (i = 0; i < state->packet_count; i++) {
428 /* Allocate an skb, holding an extra reference for
429 * transmit completion counting */
430 skb = alloc_skb(EF4_LOOPBACK_PAYLOAD_LEN, GFP_KERNEL);
433 state->skbs[i] = skb;
436 /* Copy the payload in, incrementing the source address to
437 * exercise the rss vectors */
438 payload = skb_put(skb, sizeof(state->payload));
439 memcpy(payload, &state->payload, sizeof(state->payload));
440 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
441 /* Strip off the leading padding */
442 skb_pull(skb, offsetof(struct ef4_loopback_payload, header));
444 /* Ensure everything we've written is visible to the
445 * interrupt handler. */
448 netif_tx_lock_bh(efx->net_dev);
449 rc = ef4_enqueue_skb(tx_queue, skb);
450 netif_tx_unlock_bh(efx->net_dev);
452 if (rc != NETDEV_TX_OK) {
453 netif_err(efx, drv, efx->net_dev,
454 "TX queue %d could not transmit packet %d of "
455 "%d in %s loopback test\n", tx_queue->queue,
456 i + 1, state->packet_count,
459 /* Defer cleaning up the other skbs for the caller */
468 static int ef4_poll_loopback(struct ef4_nic *efx)
470 struct ef4_loopback_state *state = efx->loopback_selftest;
472 return atomic_read(&state->rx_good) == state->packet_count;
475 static int ef4_end_loopback(struct ef4_tx_queue *tx_queue,
476 struct ef4_loopback_self_tests *lb_tests)
478 struct ef4_nic *efx = tx_queue->efx;
479 struct ef4_loopback_state *state = efx->loopback_selftest;
481 int tx_done = 0, rx_good, rx_bad;
484 netif_tx_lock_bh(efx->net_dev);
486 /* Count the number of tx completions, and decrement the refcnt. Any
487 * skbs not already completed will be free'd when the queue is flushed */
488 for (i = 0; i < state->packet_count; i++) {
489 skb = state->skbs[i];
490 if (skb && !skb_shared(skb))
495 netif_tx_unlock_bh(efx->net_dev);
497 /* Check TX completion and received packet counts */
498 rx_good = atomic_read(&state->rx_good);
499 rx_bad = atomic_read(&state->rx_bad);
500 if (tx_done != state->packet_count) {
501 /* Don't free the skbs; they will be picked up on TX
502 * overflow or channel teardown.
504 netif_err(efx, drv, efx->net_dev,
505 "TX queue %d saw only %d out of an expected %d "
506 "TX completion events in %s loopback test\n",
507 tx_queue->queue, tx_done, state->packet_count,
510 /* Allow to fall through so we see the RX errors as well */
513 /* We may always be up to a flush away from our desired packet total */
514 if (rx_good != state->packet_count) {
515 netif_dbg(efx, drv, efx->net_dev,
516 "TX queue %d saw only %d out of an expected %d "
517 "received packets in %s loopback test\n",
518 tx_queue->queue, rx_good, state->packet_count,
524 /* Update loopback test structure */
525 lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
526 lb_tests->tx_done[tx_queue->queue] += tx_done;
527 lb_tests->rx_good += rx_good;
528 lb_tests->rx_bad += rx_bad;
534 ef4_test_loopback(struct ef4_tx_queue *tx_queue,
535 struct ef4_loopback_self_tests *lb_tests)
537 struct ef4_nic *efx = tx_queue->efx;
538 struct ef4_loopback_state *state = efx->loopback_selftest;
539 int i, begin_rc, end_rc;
541 for (i = 0; i < 3; i++) {
542 /* Determine how many packets to send */
543 state->packet_count = efx->txq_entries / 3;
544 state->packet_count = min(1 << (i << 2), state->packet_count);
545 state->skbs = kcalloc(state->packet_count,
546 sizeof(state->skbs[0]), GFP_KERNEL);
549 state->flush = false;
551 netif_dbg(efx, drv, efx->net_dev,
552 "TX queue %d testing %s loopback with %d packets\n",
553 tx_queue->queue, LOOPBACK_MODE(efx),
554 state->packet_count);
556 ef4_iterate_state(efx);
557 begin_rc = ef4_begin_loopback(tx_queue);
559 /* This will normally complete very quickly, but be
560 * prepared to wait much longer. */
562 if (!ef4_poll_loopback(efx)) {
563 msleep(LOOPBACK_TIMEOUT_MS);
564 ef4_poll_loopback(efx);
567 end_rc = ef4_end_loopback(tx_queue, lb_tests);
570 if (begin_rc || end_rc) {
571 /* Wait a while to ensure there are no packets
572 * floating around after a failure. */
573 schedule_timeout_uninterruptible(HZ / 10);
574 return begin_rc ? begin_rc : end_rc;
578 netif_dbg(efx, drv, efx->net_dev,
579 "TX queue %d passed %s loopback test with a burst length "
580 "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
581 state->packet_count);
586 /* Wait for link up. On Falcon, we would prefer to rely on ef4_monitor, but
587 * any contention on the mac lock (via e.g. ef4_mac_mcast_work) causes it
588 * to delay and retry. Therefore, it's safer to just poll directly. Wait
589 * for link up and any faults to dissipate. */
590 static int ef4_wait_for_link(struct ef4_nic *efx)
592 struct ef4_link_state *link_state = &efx->link_state;
593 int count, link_up_count = 0;
596 for (count = 0; count < 40; count++) {
597 schedule_timeout_uninterruptible(HZ / 10);
599 if (efx->type->monitor != NULL) {
600 mutex_lock(&efx->mac_lock);
601 efx->type->monitor(efx);
602 mutex_unlock(&efx->mac_lock);
605 mutex_lock(&efx->mac_lock);
606 link_up = link_state->up;
608 link_up = !efx->type->check_mac_fault(efx);
609 mutex_unlock(&efx->mac_lock);
612 if (++link_up_count == 2)
622 static int ef4_test_loopbacks(struct ef4_nic *efx, struct ef4_self_tests *tests,
623 unsigned int loopback_modes)
625 enum ef4_loopback_mode mode;
626 struct ef4_loopback_state *state;
627 struct ef4_channel *channel =
628 ef4_get_channel(efx, efx->tx_channel_offset);
629 struct ef4_tx_queue *tx_queue;
632 /* Set the port loopback_selftest member. From this point on
633 * all received packets will be dropped. Mark the state as
634 * "flushing" so all inflight packets are dropped */
635 state = kzalloc(sizeof(*state), GFP_KERNEL);
638 BUG_ON(efx->loopback_selftest);
640 efx->loopback_selftest = state;
642 /* Test all supported loopback modes */
643 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
644 if (!(loopback_modes & (1 << mode)))
647 /* Move the port into the specified loopback mode. */
649 mutex_lock(&efx->mac_lock);
650 efx->loopback_mode = mode;
651 rc = __ef4_reconfigure_port(efx);
652 mutex_unlock(&efx->mac_lock);
654 netif_err(efx, drv, efx->net_dev,
655 "unable to move into %s loopback\n",
660 rc = ef4_wait_for_link(efx);
662 netif_err(efx, drv, efx->net_dev,
663 "loopback %s never came up\n",
668 /* Test all enabled types of TX queue */
669 ef4_for_each_channel_tx_queue(tx_queue, channel) {
670 state->offload_csum = (tx_queue->queue &
671 EF4_TXQ_TYPE_OFFLOAD);
672 rc = ef4_test_loopback(tx_queue,
673 &tests->loopback[mode]);
680 /* Remove the flush. The caller will remove the loopback setting */
682 efx->loopback_selftest = NULL;
692 /**************************************************************************
696 *************************************************************************/
698 int ef4_selftest(struct ef4_nic *efx, struct ef4_self_tests *tests,
701 enum ef4_loopback_mode loopback_mode = efx->loopback_mode;
702 int phy_mode = efx->phy_mode;
703 int rc_test = 0, rc_reset, rc;
705 ef4_selftest_async_cancel(efx);
707 /* Online (i.e. non-disruptive) testing
708 * This checks interrupt generation, event delivery and PHY presence. */
710 rc = ef4_test_phy_alive(efx, tests);
714 rc = ef4_test_nvram(efx, tests);
718 rc = ef4_test_interrupts(efx, tests);
722 rc = ef4_test_eventq_irq(efx, tests);
729 if (!(flags & ETH_TEST_FL_OFFLINE))
730 return ef4_test_phy(efx, tests, flags);
732 /* Offline (i.e. disruptive) testing
733 * This checks MAC and PHY loopback on the specified port. */
735 /* Detach the device so the kernel doesn't transmit during the
736 * loopback test and the watchdog timeout doesn't fire.
738 ef4_device_detach_sync(efx);
740 if (efx->type->test_chip) {
741 rc_reset = efx->type->test_chip(efx, tests);
743 netif_err(efx, hw, efx->net_dev,
744 "Unable to recover from chip test\n");
745 ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
749 if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
753 /* Ensure that the phy is powered and out of loopback
754 * for the bist and loopback tests */
755 mutex_lock(&efx->mac_lock);
756 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
757 efx->loopback_mode = LOOPBACK_NONE;
758 __ef4_reconfigure_port(efx);
759 mutex_unlock(&efx->mac_lock);
761 rc = ef4_test_phy(efx, tests, flags);
765 rc = ef4_test_loopbacks(efx, tests, efx->loopback_modes);
769 /* restore the PHY to the previous state */
770 mutex_lock(&efx->mac_lock);
771 efx->phy_mode = phy_mode;
772 efx->loopback_mode = loopback_mode;
773 __ef4_reconfigure_port(efx);
774 mutex_unlock(&efx->mac_lock);
776 netif_device_attach(efx->net_dev);
781 void ef4_selftest_async_start(struct ef4_nic *efx)
783 struct ef4_channel *channel;
785 ef4_for_each_channel(channel, efx)
786 ef4_nic_event_test_start(channel);
787 schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
790 void ef4_selftest_async_cancel(struct ef4_nic *efx)
792 cancel_delayed_work_sync(&efx->selftest_work);
795 void ef4_selftest_async_work(struct work_struct *data)
797 struct ef4_nic *efx = container_of(data, struct ef4_nic,
799 struct ef4_channel *channel;
802 ef4_for_each_channel(channel, efx) {
803 cpu = ef4_nic_event_test_irq_cpu(channel);
805 netif_err(efx, ifup, efx->net_dev,
806 "channel %d failed to trigger an interrupt\n",
809 netif_dbg(efx, ifup, efx->net_dev,
810 "channel %d triggered interrupt on CPU %d\n",
811 channel->channel, cpu);