selftest.c 22 KB

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