selftest.c 22 KB

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