/drivers/net/ethernet/sfc/selftest.c

http://github.com/mirrors/linux · C · 811 lines · 560 code · 127 blank · 124 comment · 95 complexity · 7eb9feaa9469a86d0fd9d290223b662e MD5 · raw file

  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.
  6. */
  7. #include <linux/netdevice.h>
  8. #include <linux/module.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel_stat.h>
  11. #include <linux/pci.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/ip.h>
  14. #include <linux/in.h>
  15. #include <linux/udp.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/slab.h>
  18. #include "net_driver.h"
  19. #include "efx.h"
  20. #include "efx_common.h"
  21. #include "efx_channels.h"
  22. #include "nic.h"
  23. #include "selftest.h"
  24. #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
  30. * threads
  31. * Try to avoid blaming the hardware for this.
  32. */
  33. #define IRQ_TIMEOUT HZ
  34. /*
  35. * Loopback test packet structure
  36. *
  37. * The self-test should stress every RSS vector, and unfortunately
  38. * Falcon only performs RSS on TCP/UDP packets.
  39. */
  40. struct efx_loopback_payload {
  41. struct ethhdr header;
  42. struct iphdr ip;
  43. struct udphdr udp;
  44. __be16 iteration;
  45. char msg[64];
  46. } __packed;
  47. /* Loopback test source MAC address */
  48. static const u8 payload_source[ETH_ALEN] __aligned(2) = {
  49. 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
  50. };
  51. static const char payload_msg[] =
  52. "Hello world! This is an Efx loopback test in progress!";
  53. /* Interrupt mode names */
  54. static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
  55. static const char *const efx_interrupt_mode_names[] = {
  56. [EFX_INT_MODE_MSIX] = "MSI-X",
  57. [EFX_INT_MODE_MSI] = "MSI",
  58. [EFX_INT_MODE_LEGACY] = "legacy",
  59. };
  60. #define INT_MODE(efx) \
  61. STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
  62. /**
  63. * efx_loopback_state - persistent state during a loopback selftest
  64. * @flush: Drop all packets in efx_loopback_rx_packet
  65. * @packet_count: Number of packets being used in this test
  66. * @skbs: An array of skbs transmitted
  67. * @offload_csum: Checksums are being offloaded
  68. * @rx_good: RX good packet count
  69. * @rx_bad: RX bad packet count
  70. * @payload: Payload used in tests
  71. */
  72. struct efx_loopback_state {
  73. bool flush;
  74. int packet_count;
  75. struct sk_buff **skbs;
  76. bool offload_csum;
  77. atomic_t rx_good;
  78. atomic_t rx_bad;
  79. struct efx_loopback_payload payload;
  80. };
  81. /* How long to wait for all the packets to arrive (in ms) */
  82. #define LOOPBACK_TIMEOUT_MS 1000
  83. /**************************************************************************
  84. *
  85. * MII, NVRAM and register tests
  86. *
  87. **************************************************************************/
  88. static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
  89. {
  90. int rc = 0;
  91. if (efx->phy_op->test_alive) {
  92. rc = efx->phy_op->test_alive(efx);
  93. tests->phy_alive = rc ? -1 : 1;
  94. }
  95. return rc;
  96. }
  97. static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
  98. {
  99. int rc = 0;
  100. if (efx->type->test_nvram) {
  101. rc = efx->type->test_nvram(efx);
  102. if (rc == -EPERM)
  103. rc = 0;
  104. else
  105. tests->nvram = rc ? -1 : 1;
  106. }
  107. return rc;
  108. }
  109. /**************************************************************************
  110. *
  111. * Interrupt and event queue testing
  112. *
  113. **************************************************************************/
  114. /* Test generation and receipt of interrupts */
  115. static int efx_test_interrupts(struct efx_nic *efx,
  116. struct efx_self_tests *tests)
  117. {
  118. unsigned long timeout, wait;
  119. int cpu;
  120. int rc;
  121. netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
  122. tests->interrupt = -1;
  123. rc = efx_nic_irq_test_start(efx);
  124. if (rc == -ENOTSUPP) {
  125. netif_dbg(efx, drv, efx->net_dev,
  126. "direct interrupt testing not supported\n");
  127. tests->interrupt = 0;
  128. return 0;
  129. }
  130. timeout = jiffies + IRQ_TIMEOUT;
  131. wait = 1;
  132. /* Wait for arrival of test interrupt. */
  133. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  134. do {
  135. schedule_timeout_uninterruptible(wait);
  136. cpu = efx_nic_irq_test_irq_cpu(efx);
  137. if (cpu >= 0)
  138. goto success;
  139. wait *= 2;
  140. } while (time_before(jiffies, timeout));
  141. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  142. return -ETIMEDOUT;
  143. success:
  144. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  145. INT_MODE(efx), cpu);
  146. tests->interrupt = 1;
  147. return 0;
  148. }
  149. /* Test generation and receipt of interrupting events */
  150. static int efx_test_eventq_irq(struct efx_nic *efx,
  151. struct efx_self_tests *tests)
  152. {
  153. struct efx_channel *channel;
  154. unsigned int read_ptr[EFX_MAX_CHANNELS];
  155. unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
  156. unsigned long timeout, wait;
  157. BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
  158. efx_for_each_channel(channel, efx) {
  159. read_ptr[channel->channel] = channel->eventq_read_ptr;
  160. set_bit(channel->channel, &dma_pend);
  161. set_bit(channel->channel, &int_pend);
  162. efx_nic_event_test_start(channel);
  163. }
  164. timeout = jiffies + IRQ_TIMEOUT;
  165. wait = 1;
  166. /* Wait for arrival of interrupts. NAPI processing may or may
  167. * not complete in time, but we can cope in any case.
  168. */
  169. do {
  170. schedule_timeout_uninterruptible(wait);
  171. efx_for_each_channel(channel, efx) {
  172. efx_stop_eventq(channel);
  173. if (channel->eventq_read_ptr !=
  174. read_ptr[channel->channel]) {
  175. set_bit(channel->channel, &napi_ran);
  176. clear_bit(channel->channel, &dma_pend);
  177. clear_bit(channel->channel, &int_pend);
  178. } else {
  179. if (efx_nic_event_present(channel))
  180. clear_bit(channel->channel, &dma_pend);
  181. if (efx_nic_event_test_irq_cpu(channel) >= 0)
  182. clear_bit(channel->channel, &int_pend);
  183. }
  184. efx_start_eventq(channel);
  185. }
  186. wait *= 2;
  187. } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
  188. efx_for_each_channel(channel, efx) {
  189. bool dma_seen = !test_bit(channel->channel, &dma_pend);
  190. bool int_seen = !test_bit(channel->channel, &int_pend);
  191. tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
  192. tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
  193. if (dma_seen && int_seen) {
  194. netif_dbg(efx, drv, efx->net_dev,
  195. "channel %d event queue passed (with%s NAPI)\n",
  196. channel->channel,
  197. test_bit(channel->channel, &napi_ran) ?
  198. "" : "out");
  199. } else {
  200. /* Report failure and whether either interrupt or DMA
  201. * worked
  202. */
  203. netif_err(efx, drv, efx->net_dev,
  204. "channel %d timed out waiting for event queue\n",
  205. channel->channel);
  206. if (int_seen)
  207. netif_err(efx, drv, efx->net_dev,
  208. "channel %d saw interrupt "
  209. "during event queue test\n",
  210. channel->channel);
  211. if (dma_seen)
  212. netif_err(efx, drv, efx->net_dev,
  213. "channel %d event was generated, but "
  214. "failed to trigger an interrupt\n",
  215. channel->channel);
  216. }
  217. }
  218. return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
  219. }
  220. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  221. unsigned flags)
  222. {
  223. int rc;
  224. if (!efx->phy_op->run_tests)
  225. return 0;
  226. mutex_lock(&efx->mac_lock);
  227. rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
  228. mutex_unlock(&efx->mac_lock);
  229. if (rc == -EPERM)
  230. rc = 0;
  231. else
  232. netif_info(efx, drv, efx->net_dev,
  233. "%s phy selftest\n", rc ? "Failed" : "Passed");
  234. return rc;
  235. }
  236. /**************************************************************************
  237. *
  238. * Loopback testing
  239. * NB Only one loopback test can be executing concurrently.
  240. *
  241. **************************************************************************/
  242. /* Loopback test RX callback
  243. * This is called for each received packet during loopback testing.
  244. */
  245. void efx_loopback_rx_packet(struct efx_nic *efx,
  246. const char *buf_ptr, int pkt_len)
  247. {
  248. struct efx_loopback_state *state = efx->loopback_selftest;
  249. struct efx_loopback_payload *received;
  250. struct efx_loopback_payload *payload;
  251. BUG_ON(!buf_ptr);
  252. /* If we are just flushing, then drop the packet */
  253. if ((state == NULL) || state->flush)
  254. return;
  255. payload = &state->payload;
  256. received = (struct efx_loopback_payload *) buf_ptr;
  257. received->ip.saddr = payload->ip.saddr;
  258. if (state->offload_csum)
  259. received->ip.check = payload->ip.check;
  260. /* Check that header exists */
  261. if (pkt_len < sizeof(received->header)) {
  262. netif_err(efx, drv, efx->net_dev,
  263. "saw runt RX packet (length %d) in %s loopback "
  264. "test\n", pkt_len, LOOPBACK_MODE(efx));
  265. goto err;
  266. }
  267. /* Check that the ethernet header exists */
  268. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  269. netif_err(efx, drv, efx->net_dev,
  270. "saw non-loopback RX packet in %s loopback test\n",
  271. LOOPBACK_MODE(efx));
  272. goto err;
  273. }
  274. /* Check packet length */
  275. if (pkt_len != sizeof(*payload)) {
  276. netif_err(efx, drv, efx->net_dev,
  277. "saw incorrect RX packet length %d (wanted %d) in "
  278. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  279. LOOPBACK_MODE(efx));
  280. goto err;
  281. }
  282. /* Check that IP header matches */
  283. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  284. netif_err(efx, drv, efx->net_dev,
  285. "saw corrupted IP header in %s loopback test\n",
  286. LOOPBACK_MODE(efx));
  287. goto err;
  288. }
  289. /* Check that msg and padding matches */
  290. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  291. netif_err(efx, drv, efx->net_dev,
  292. "saw corrupted RX packet in %s loopback test\n",
  293. LOOPBACK_MODE(efx));
  294. goto err;
  295. }
  296. /* Check that iteration matches */
  297. if (received->iteration != payload->iteration) {
  298. netif_err(efx, drv, efx->net_dev,
  299. "saw RX packet from iteration %d (wanted %d) in "
  300. "%s loopback test\n", ntohs(received->iteration),
  301. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  302. goto err;
  303. }
  304. /* Increase correct RX count */
  305. netif_vdbg(efx, drv, efx->net_dev,
  306. "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
  307. atomic_inc(&state->rx_good);
  308. return;
  309. err:
  310. #ifdef DEBUG
  311. if (atomic_read(&state->rx_bad) == 0) {
  312. netif_err(efx, drv, efx->net_dev, "received packet:\n");
  313. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  314. buf_ptr, pkt_len, 0);
  315. netif_err(efx, drv, efx->net_dev, "expected packet:\n");
  316. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  317. &state->payload, sizeof(state->payload), 0);
  318. }
  319. #endif
  320. atomic_inc(&state->rx_bad);
  321. }
  322. /* Initialise an efx_selftest_state for a new iteration */
  323. static void efx_iterate_state(struct efx_nic *efx)
  324. {
  325. struct efx_loopback_state *state = efx->loopback_selftest;
  326. struct net_device *net_dev = efx->net_dev;
  327. struct efx_loopback_payload *payload = &state->payload;
  328. /* Initialise the layerII header */
  329. ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
  330. ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
  331. payload->header.h_proto = htons(ETH_P_IP);
  332. /* saddr set later and used as incrementing count */
  333. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  334. payload->ip.ihl = 5;
  335. payload->ip.check = (__force __sum16) htons(0xdead);
  336. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  337. payload->ip.version = IPVERSION;
  338. payload->ip.protocol = IPPROTO_UDP;
  339. /* Initialise udp header */
  340. payload->udp.source = 0;
  341. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  342. sizeof(struct iphdr));
  343. payload->udp.check = 0; /* checksum ignored */
  344. /* Fill out payload */
  345. payload->iteration = htons(ntohs(payload->iteration) + 1);
  346. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  347. /* Fill out remaining state members */
  348. atomic_set(&state->rx_good, 0);
  349. atomic_set(&state->rx_bad, 0);
  350. smp_wmb();
  351. }
  352. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  353. {
  354. struct efx_nic *efx = tx_queue->efx;
  355. struct efx_loopback_state *state = efx->loopback_selftest;
  356. struct efx_loopback_payload *payload;
  357. struct sk_buff *skb;
  358. int i;
  359. netdev_tx_t rc;
  360. /* Transmit N copies of buffer */
  361. for (i = 0; i < state->packet_count; i++) {
  362. /* Allocate an skb, holding an extra reference for
  363. * transmit completion counting */
  364. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  365. if (!skb)
  366. return -ENOMEM;
  367. state->skbs[i] = skb;
  368. skb_get(skb);
  369. /* Copy the payload in, incrementing the source address to
  370. * exercise the rss vectors */
  371. payload = skb_put(skb, sizeof(state->payload));
  372. memcpy(payload, &state->payload, sizeof(state->payload));
  373. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  374. /* Ensure everything we've written is visible to the
  375. * interrupt handler. */
  376. smp_wmb();
  377. netif_tx_lock_bh(efx->net_dev);
  378. rc = efx_enqueue_skb(tx_queue, skb);
  379. netif_tx_unlock_bh(efx->net_dev);
  380. if (rc != NETDEV_TX_OK) {
  381. netif_err(efx, drv, efx->net_dev,
  382. "TX queue %d could not transmit packet %d of "
  383. "%d in %s loopback test\n", tx_queue->queue,
  384. i + 1, state->packet_count,
  385. LOOPBACK_MODE(efx));
  386. /* Defer cleaning up the other skbs for the caller */
  387. kfree_skb(skb);
  388. return -EPIPE;
  389. }
  390. }
  391. return 0;
  392. }
  393. static int efx_poll_loopback(struct efx_nic *efx)
  394. {
  395. struct efx_loopback_state *state = efx->loopback_selftest;
  396. return atomic_read(&state->rx_good) == state->packet_count;
  397. }
  398. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  399. struct efx_loopback_self_tests *lb_tests)
  400. {
  401. struct efx_nic *efx = tx_queue->efx;
  402. struct efx_loopback_state *state = efx->loopback_selftest;
  403. struct sk_buff *skb;
  404. int tx_done = 0, rx_good, rx_bad;
  405. int i, rc = 0;
  406. netif_tx_lock_bh(efx->net_dev);
  407. /* Count the number of tx completions, and decrement the refcnt. Any
  408. * skbs not already completed will be free'd when the queue is flushed */
  409. for (i = 0; i < state->packet_count; i++) {
  410. skb = state->skbs[i];
  411. if (skb && !skb_shared(skb))
  412. ++tx_done;
  413. dev_kfree_skb(skb);
  414. }
  415. netif_tx_unlock_bh(efx->net_dev);
  416. /* Check TX completion and received packet counts */
  417. rx_good = atomic_read(&state->rx_good);
  418. rx_bad = atomic_read(&state->rx_bad);
  419. if (tx_done != state->packet_count) {
  420. /* Don't free the skbs; they will be picked up on TX
  421. * overflow or channel teardown.
  422. */
  423. netif_err(efx, drv, efx->net_dev,
  424. "TX queue %d saw only %d out of an expected %d "
  425. "TX completion events in %s loopback test\n",
  426. tx_queue->queue, tx_done, state->packet_count,
  427. LOOPBACK_MODE(efx));
  428. rc = -ETIMEDOUT;
  429. /* Allow to fall through so we see the RX errors as well */
  430. }
  431. /* We may always be up to a flush away from our desired packet total */
  432. if (rx_good != state->packet_count) {
  433. netif_dbg(efx, drv, efx->net_dev,
  434. "TX queue %d saw only %d out of an expected %d "
  435. "received packets in %s loopback test\n",
  436. tx_queue->queue, rx_good, state->packet_count,
  437. LOOPBACK_MODE(efx));
  438. rc = -ETIMEDOUT;
  439. /* Fall through */
  440. }
  441. /* Update loopback test structure */
  442. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  443. lb_tests->tx_done[tx_queue->queue] += tx_done;
  444. lb_tests->rx_good += rx_good;
  445. lb_tests->rx_bad += rx_bad;
  446. return rc;
  447. }
  448. static int
  449. efx_test_loopback(struct efx_tx_queue *tx_queue,
  450. struct efx_loopback_self_tests *lb_tests)
  451. {
  452. struct efx_nic *efx = tx_queue->efx;
  453. struct efx_loopback_state *state = efx->loopback_selftest;
  454. int i, begin_rc, end_rc;
  455. for (i = 0; i < 3; i++) {
  456. /* Determine how many packets to send */
  457. state->packet_count = efx->txq_entries / 3;
  458. state->packet_count = min(1 << (i << 2), state->packet_count);
  459. state->skbs = kcalloc(state->packet_count,
  460. sizeof(state->skbs[0]), GFP_KERNEL);
  461. if (!state->skbs)
  462. return -ENOMEM;
  463. state->flush = false;
  464. netif_dbg(efx, drv, efx->net_dev,
  465. "TX queue %d testing %s loopback with %d packets\n",
  466. tx_queue->queue, LOOPBACK_MODE(efx),
  467. state->packet_count);
  468. efx_iterate_state(efx);
  469. begin_rc = efx_begin_loopback(tx_queue);
  470. /* This will normally complete very quickly, but be
  471. * prepared to wait much longer. */
  472. msleep(1);
  473. if (!efx_poll_loopback(efx)) {
  474. msleep(LOOPBACK_TIMEOUT_MS);
  475. efx_poll_loopback(efx);
  476. }
  477. end_rc = efx_end_loopback(tx_queue, lb_tests);
  478. kfree(state->skbs);
  479. if (begin_rc || end_rc) {
  480. /* Wait a while to ensure there are no packets
  481. * floating around after a failure. */
  482. schedule_timeout_uninterruptible(HZ / 10);
  483. return begin_rc ? begin_rc : end_rc;
  484. }
  485. }
  486. netif_dbg(efx, drv, efx->net_dev,
  487. "TX queue %d passed %s loopback test with a burst length "
  488. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  489. state->packet_count);
  490. return 0;
  491. }
  492. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  493. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  494. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  495. * for link up and any faults to dissipate. */
  496. static int efx_wait_for_link(struct efx_nic *efx)
  497. {
  498. struct efx_link_state *link_state = &efx->link_state;
  499. int count, link_up_count = 0;
  500. bool link_up;
  501. for (count = 0; count < 40; count++) {
  502. schedule_timeout_uninterruptible(HZ / 10);
  503. if (efx->type->monitor != NULL) {
  504. mutex_lock(&efx->mac_lock);
  505. efx->type->monitor(efx);
  506. mutex_unlock(&efx->mac_lock);
  507. }
  508. mutex_lock(&efx->mac_lock);
  509. link_up = link_state->up;
  510. if (link_up)
  511. link_up = !efx->type->check_mac_fault(efx);
  512. mutex_unlock(&efx->mac_lock);
  513. if (link_up) {
  514. if (++link_up_count == 2)
  515. return 0;
  516. } else {
  517. link_up_count = 0;
  518. }
  519. }
  520. return -ETIMEDOUT;
  521. }
  522. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  523. unsigned int loopback_modes)
  524. {
  525. enum efx_loopback_mode mode;
  526. struct efx_loopback_state *state;
  527. struct efx_channel *channel =
  528. efx_get_channel(efx, efx->tx_channel_offset);
  529. struct efx_tx_queue *tx_queue;
  530. int rc = 0;
  531. /* Set the port loopback_selftest member. From this point on
  532. * all received packets will be dropped. Mark the state as
  533. * "flushing" so all inflight packets are dropped */
  534. state = kzalloc(sizeof(*state), GFP_KERNEL);
  535. if (state == NULL)
  536. return -ENOMEM;
  537. BUG_ON(efx->loopback_selftest);
  538. state->flush = true;
  539. efx->loopback_selftest = state;
  540. /* Test all supported loopback modes */
  541. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  542. if (!(loopback_modes & (1 << mode)))
  543. continue;
  544. /* Move the port into the specified loopback mode. */
  545. state->flush = true;
  546. mutex_lock(&efx->mac_lock);
  547. efx->loopback_mode = mode;
  548. rc = __efx_reconfigure_port(efx);
  549. mutex_unlock(&efx->mac_lock);
  550. if (rc) {
  551. netif_err(efx, drv, efx->net_dev,
  552. "unable to move into %s loopback\n",
  553. LOOPBACK_MODE(efx));
  554. goto out;
  555. }
  556. rc = efx_wait_for_link(efx);
  557. if (rc) {
  558. netif_err(efx, drv, efx->net_dev,
  559. "loopback %s never came up\n",
  560. LOOPBACK_MODE(efx));
  561. goto out;
  562. }
  563. /* Test all enabled types of TX queue */
  564. efx_for_each_channel_tx_queue(tx_queue, channel) {
  565. state->offload_csum = (tx_queue->queue &
  566. EFX_TXQ_TYPE_OFFLOAD);
  567. rc = efx_test_loopback(tx_queue,
  568. &tests->loopback[mode]);
  569. if (rc)
  570. goto out;
  571. }
  572. }
  573. out:
  574. /* Remove the flush. The caller will remove the loopback setting */
  575. state->flush = true;
  576. efx->loopback_selftest = NULL;
  577. wmb();
  578. kfree(state);
  579. if (rc == -EPERM)
  580. rc = 0;
  581. return rc;
  582. }
  583. /**************************************************************************
  584. *
  585. * Entry point
  586. *
  587. *************************************************************************/
  588. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  589. unsigned flags)
  590. {
  591. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  592. int phy_mode = efx->phy_mode;
  593. int rc_test = 0, rc_reset, rc;
  594. efx_selftest_async_cancel(efx);
  595. /* Online (i.e. non-disruptive) testing
  596. * This checks interrupt generation, event delivery and PHY presence. */
  597. rc = efx_test_phy_alive(efx, tests);
  598. if (rc && !rc_test)
  599. rc_test = rc;
  600. rc = efx_test_nvram(efx, tests);
  601. if (rc && !rc_test)
  602. rc_test = rc;
  603. rc = efx_test_interrupts(efx, tests);
  604. if (rc && !rc_test)
  605. rc_test = rc;
  606. rc = efx_test_eventq_irq(efx, tests);
  607. if (rc && !rc_test)
  608. rc_test = rc;
  609. if (rc_test)
  610. return rc_test;
  611. if (!(flags & ETH_TEST_FL_OFFLINE))
  612. return efx_test_phy(efx, tests, flags);
  613. /* Offline (i.e. disruptive) testing
  614. * This checks MAC and PHY loopback on the specified port. */
  615. /* Detach the device so the kernel doesn't transmit during the
  616. * loopback test and the watchdog timeout doesn't fire.
  617. */
  618. efx_device_detach_sync(efx);
  619. if (efx->type->test_chip) {
  620. rc_reset = efx->type->test_chip(efx, tests);
  621. if (rc_reset) {
  622. netif_err(efx, hw, efx->net_dev,
  623. "Unable to recover from chip test\n");
  624. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  625. return rc_reset;
  626. }
  627. if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
  628. rc_test = -EIO;
  629. }
  630. /* Ensure that the phy is powered and out of loopback
  631. * for the bist and loopback tests */
  632. mutex_lock(&efx->mac_lock);
  633. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  634. efx->loopback_mode = LOOPBACK_NONE;
  635. __efx_reconfigure_port(efx);
  636. mutex_unlock(&efx->mac_lock);
  637. rc = efx_test_phy(efx, tests, flags);
  638. if (rc && !rc_test)
  639. rc_test = rc;
  640. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  641. if (rc && !rc_test)
  642. rc_test = rc;
  643. /* restore the PHY to the previous state */
  644. mutex_lock(&efx->mac_lock);
  645. efx->phy_mode = phy_mode;
  646. efx->loopback_mode = loopback_mode;
  647. __efx_reconfigure_port(efx);
  648. mutex_unlock(&efx->mac_lock);
  649. efx_device_attach_if_not_resetting(efx);
  650. return rc_test;
  651. }
  652. void efx_selftest_async_start(struct efx_nic *efx)
  653. {
  654. struct efx_channel *channel;
  655. efx_for_each_channel(channel, efx)
  656. efx_nic_event_test_start(channel);
  657. schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
  658. }
  659. void efx_selftest_async_cancel(struct efx_nic *efx)
  660. {
  661. cancel_delayed_work_sync(&efx->selftest_work);
  662. }
  663. static void efx_selftest_async_work(struct work_struct *data)
  664. {
  665. struct efx_nic *efx = container_of(data, struct efx_nic,
  666. selftest_work.work);
  667. struct efx_channel *channel;
  668. int cpu;
  669. efx_for_each_channel(channel, efx) {
  670. cpu = efx_nic_event_test_irq_cpu(channel);
  671. if (cpu < 0)
  672. netif_err(efx, ifup, efx->net_dev,
  673. "channel %d failed to trigger an interrupt\n",
  674. channel->channel);
  675. else
  676. netif_dbg(efx, ifup, efx->net_dev,
  677. "channel %d triggered interrupt on CPU %d\n",
  678. channel->channel, cpu);
  679. }
  680. }
  681. void efx_selftest_async_init(struct efx_nic *efx)
  682. {
  683. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  684. }