selftest.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2010 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. const char msg[64];
  47. } __packed;
  48. /* Loopback test source MAC address */
  49. static const unsigned char payload_source[ETH_ALEN] = {
  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. tests->nvram = rc ? -1 : 1;
  104. }
  105. return rc;
  106. }
  107. /**************************************************************************
  108. *
  109. * Interrupt and event queue testing
  110. *
  111. **************************************************************************/
  112. /* Test generation and receipt of interrupts */
  113. static int efx_test_interrupts(struct efx_nic *efx,
  114. struct efx_self_tests *tests)
  115. {
  116. unsigned long timeout, wait;
  117. int cpu;
  118. netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
  119. tests->interrupt = -1;
  120. efx_nic_irq_test_start(efx);
  121. timeout = jiffies + IRQ_TIMEOUT;
  122. wait = 1;
  123. /* Wait for arrival of test interrupt. */
  124. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  125. do {
  126. schedule_timeout_uninterruptible(wait);
  127. cpu = efx_nic_irq_test_irq_cpu(efx);
  128. if (cpu >= 0)
  129. goto success;
  130. wait *= 2;
  131. } while (time_before(jiffies, timeout));
  132. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  133. return -ETIMEDOUT;
  134. success:
  135. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  136. INT_MODE(efx), cpu);
  137. tests->interrupt = 1;
  138. return 0;
  139. }
  140. /* Test generation and receipt of interrupting events */
  141. static int efx_test_eventq_irq(struct efx_nic *efx,
  142. struct efx_self_tests *tests)
  143. {
  144. struct efx_channel *channel;
  145. unsigned int read_ptr[EFX_MAX_CHANNELS];
  146. unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
  147. unsigned long timeout, wait;
  148. BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
  149. efx_for_each_channel(channel, efx) {
  150. read_ptr[channel->channel] = channel->eventq_read_ptr;
  151. set_bit(channel->channel, &dma_pend);
  152. set_bit(channel->channel, &int_pend);
  153. efx_nic_event_test_start(channel);
  154. }
  155. timeout = jiffies + IRQ_TIMEOUT;
  156. wait = 1;
  157. /* Wait for arrival of interrupts. NAPI processing may or may
  158. * not complete in time, but we can cope in any case.
  159. */
  160. do {
  161. schedule_timeout_uninterruptible(wait);
  162. efx_for_each_channel(channel, efx) {
  163. napi_disable(&channel->napi_str);
  164. if (channel->eventq_read_ptr !=
  165. read_ptr[channel->channel]) {
  166. set_bit(channel->channel, &napi_ran);
  167. clear_bit(channel->channel, &dma_pend);
  168. clear_bit(channel->channel, &int_pend);
  169. } else {
  170. if (efx_nic_event_present(channel))
  171. clear_bit(channel->channel, &dma_pend);
  172. if (efx_nic_event_test_irq_cpu(channel) >= 0)
  173. clear_bit(channel->channel, &int_pend);
  174. }
  175. napi_enable(&channel->napi_str);
  176. efx_nic_eventq_read_ack(channel);
  177. }
  178. wait *= 2;
  179. } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
  180. efx_for_each_channel(channel, efx) {
  181. bool dma_seen = !test_bit(channel->channel, &dma_pend);
  182. bool int_seen = !test_bit(channel->channel, &int_pend);
  183. tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
  184. tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
  185. if (dma_seen && int_seen) {
  186. netif_dbg(efx, drv, efx->net_dev,
  187. "channel %d event queue passed (with%s NAPI)\n",
  188. channel->channel,
  189. test_bit(channel->channel, &napi_ran) ?
  190. "" : "out");
  191. } else {
  192. /* Report failure and whether either interrupt or DMA
  193. * worked
  194. */
  195. netif_err(efx, drv, efx->net_dev,
  196. "channel %d timed out waiting for event queue\n",
  197. channel->channel);
  198. if (int_seen)
  199. netif_err(efx, drv, efx->net_dev,
  200. "channel %d saw interrupt "
  201. "during event queue test\n",
  202. channel->channel);
  203. if (dma_seen)
  204. netif_err(efx, drv, efx->net_dev,
  205. "channel %d event was generated, but "
  206. "failed to trigger an interrupt\n",
  207. channel->channel);
  208. }
  209. }
  210. return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
  211. }
  212. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  213. unsigned flags)
  214. {
  215. int rc;
  216. if (!efx->phy_op->run_tests)
  217. return 0;
  218. mutex_lock(&efx->mac_lock);
  219. rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
  220. mutex_unlock(&efx->mac_lock);
  221. return rc;
  222. }
  223. /**************************************************************************
  224. *
  225. * Loopback testing
  226. * NB Only one loopback test can be executing concurrently.
  227. *
  228. **************************************************************************/
  229. /* Loopback test RX callback
  230. * This is called for each received packet during loopback testing.
  231. */
  232. void efx_loopback_rx_packet(struct efx_nic *efx,
  233. const char *buf_ptr, int pkt_len)
  234. {
  235. struct efx_loopback_state *state = efx->loopback_selftest;
  236. struct efx_loopback_payload *received;
  237. struct efx_loopback_payload *payload;
  238. BUG_ON(!buf_ptr);
  239. /* If we are just flushing, then drop the packet */
  240. if ((state == NULL) || state->flush)
  241. return;
  242. payload = &state->payload;
  243. received = (struct efx_loopback_payload *) buf_ptr;
  244. received->ip.saddr = payload->ip.saddr;
  245. if (state->offload_csum)
  246. received->ip.check = payload->ip.check;
  247. /* Check that header exists */
  248. if (pkt_len < sizeof(received->header)) {
  249. netif_err(efx, drv, efx->net_dev,
  250. "saw runt RX packet (length %d) in %s loopback "
  251. "test\n", pkt_len, LOOPBACK_MODE(efx));
  252. goto err;
  253. }
  254. /* Check that the ethernet header exists */
  255. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  256. netif_err(efx, drv, efx->net_dev,
  257. "saw non-loopback RX packet in %s loopback test\n",
  258. LOOPBACK_MODE(efx));
  259. goto err;
  260. }
  261. /* Check packet length */
  262. if (pkt_len != sizeof(*payload)) {
  263. netif_err(efx, drv, efx->net_dev,
  264. "saw incorrect RX packet length %d (wanted %d) in "
  265. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  266. LOOPBACK_MODE(efx));
  267. goto err;
  268. }
  269. /* Check that IP header matches */
  270. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  271. netif_err(efx, drv, efx->net_dev,
  272. "saw corrupted IP header in %s loopback test\n",
  273. LOOPBACK_MODE(efx));
  274. goto err;
  275. }
  276. /* Check that msg and padding matches */
  277. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  278. netif_err(efx, drv, efx->net_dev,
  279. "saw corrupted RX packet in %s loopback test\n",
  280. LOOPBACK_MODE(efx));
  281. goto err;
  282. }
  283. /* Check that iteration matches */
  284. if (received->iteration != payload->iteration) {
  285. netif_err(efx, drv, efx->net_dev,
  286. "saw RX packet from iteration %d (wanted %d) in "
  287. "%s loopback test\n", ntohs(received->iteration),
  288. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  289. goto err;
  290. }
  291. /* Increase correct RX count */
  292. netif_vdbg(efx, drv, efx->net_dev,
  293. "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
  294. atomic_inc(&state->rx_good);
  295. return;
  296. err:
  297. #ifdef DEBUG
  298. if (atomic_read(&state->rx_bad) == 0) {
  299. netif_err(efx, drv, efx->net_dev, "received packet:\n");
  300. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  301. buf_ptr, pkt_len, 0);
  302. netif_err(efx, drv, efx->net_dev, "expected packet:\n");
  303. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  304. &state->payload, sizeof(state->payload), 0);
  305. }
  306. #endif
  307. atomic_inc(&state->rx_bad);
  308. }
  309. /* Initialise an efx_selftest_state for a new iteration */
  310. static void efx_iterate_state(struct efx_nic *efx)
  311. {
  312. struct efx_loopback_state *state = efx->loopback_selftest;
  313. struct net_device *net_dev = efx->net_dev;
  314. struct efx_loopback_payload *payload = &state->payload;
  315. /* Initialise the layerII header */
  316. memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
  317. memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
  318. payload->header.h_proto = htons(ETH_P_IP);
  319. /* saddr set later and used as incrementing count */
  320. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  321. payload->ip.ihl = 5;
  322. payload->ip.check = htons(0xdead);
  323. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  324. payload->ip.version = IPVERSION;
  325. payload->ip.protocol = IPPROTO_UDP;
  326. /* Initialise udp header */
  327. payload->udp.source = 0;
  328. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  329. sizeof(struct iphdr));
  330. payload->udp.check = 0; /* checksum ignored */
  331. /* Fill out payload */
  332. payload->iteration = htons(ntohs(payload->iteration) + 1);
  333. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  334. /* Fill out remaining state members */
  335. atomic_set(&state->rx_good, 0);
  336. atomic_set(&state->rx_bad, 0);
  337. smp_wmb();
  338. }
  339. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  340. {
  341. struct efx_nic *efx = tx_queue->efx;
  342. struct efx_loopback_state *state = efx->loopback_selftest;
  343. struct efx_loopback_payload *payload;
  344. struct sk_buff *skb;
  345. int i;
  346. netdev_tx_t rc;
  347. /* Transmit N copies of buffer */
  348. for (i = 0; i < state->packet_count; i++) {
  349. /* Allocate an skb, holding an extra reference for
  350. * transmit completion counting */
  351. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  352. if (!skb)
  353. return -ENOMEM;
  354. state->skbs[i] = skb;
  355. skb_get(skb);
  356. /* Copy the payload in, incrementing the source address to
  357. * exercise the rss vectors */
  358. payload = ((struct efx_loopback_payload *)
  359. skb_put(skb, sizeof(state->payload)));
  360. memcpy(payload, &state->payload, sizeof(state->payload));
  361. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  362. /* Ensure everything we've written is visible to the
  363. * interrupt handler. */
  364. smp_wmb();
  365. netif_tx_lock_bh(efx->net_dev);
  366. rc = efx_enqueue_skb(tx_queue, skb);
  367. netif_tx_unlock_bh(efx->net_dev);
  368. if (rc != NETDEV_TX_OK) {
  369. netif_err(efx, drv, efx->net_dev,
  370. "TX queue %d could not transmit packet %d of "
  371. "%d in %s loopback test\n", tx_queue->queue,
  372. i + 1, state->packet_count,
  373. LOOPBACK_MODE(efx));
  374. /* Defer cleaning up the other skbs for the caller */
  375. kfree_skb(skb);
  376. return -EPIPE;
  377. }
  378. }
  379. return 0;
  380. }
  381. static int efx_poll_loopback(struct efx_nic *efx)
  382. {
  383. struct efx_loopback_state *state = efx->loopback_selftest;
  384. struct efx_channel *channel;
  385. /* NAPI polling is not enabled, so process channels
  386. * synchronously */
  387. efx_for_each_channel(channel, efx) {
  388. if (channel->work_pending)
  389. efx_process_channel_now(channel);
  390. }
  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_any(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. } else {
  503. struct efx_channel *channel = efx_get_channel(efx, 0);
  504. if (channel->work_pending)
  505. efx_process_channel_now(channel);
  506. }
  507. mutex_lock(&efx->mac_lock);
  508. link_up = link_state->up;
  509. if (link_up)
  510. link_up = !efx->type->check_mac_fault(efx);
  511. mutex_unlock(&efx->mac_lock);
  512. if (link_up) {
  513. if (++link_up_count == 2)
  514. return 0;
  515. } else {
  516. link_up_count = 0;
  517. }
  518. }
  519. return -ETIMEDOUT;
  520. }
  521. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  522. unsigned int loopback_modes)
  523. {
  524. enum efx_loopback_mode mode;
  525. struct efx_loopback_state *state;
  526. struct efx_channel *channel = efx_get_channel(efx, 0);
  527. struct efx_tx_queue *tx_queue;
  528. int rc = 0;
  529. /* Set the port loopback_selftest member. From this point on
  530. * all received packets will be dropped. Mark the state as
  531. * "flushing" so all inflight packets are dropped */
  532. state = kzalloc(sizeof(*state), GFP_KERNEL);
  533. if (state == NULL)
  534. return -ENOMEM;
  535. BUG_ON(efx->loopback_selftest);
  536. state->flush = true;
  537. efx->loopback_selftest = state;
  538. /* Test all supported loopback modes */
  539. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  540. if (!(loopback_modes & (1 << mode)))
  541. continue;
  542. /* Move the port into the specified loopback mode. */
  543. state->flush = true;
  544. mutex_lock(&efx->mac_lock);
  545. efx->loopback_mode = mode;
  546. rc = __efx_reconfigure_port(efx);
  547. mutex_unlock(&efx->mac_lock);
  548. if (rc) {
  549. netif_err(efx, drv, efx->net_dev,
  550. "unable to move into %s loopback\n",
  551. LOOPBACK_MODE(efx));
  552. goto out;
  553. }
  554. rc = efx_wait_for_link(efx);
  555. if (rc) {
  556. netif_err(efx, drv, efx->net_dev,
  557. "loopback %s never came up\n",
  558. LOOPBACK_MODE(efx));
  559. goto out;
  560. }
  561. /* Test all enabled types of TX queue */
  562. efx_for_each_channel_tx_queue(tx_queue, channel) {
  563. state->offload_csum = (tx_queue->queue &
  564. EFX_TXQ_TYPE_OFFLOAD);
  565. rc = efx_test_loopback(tx_queue,
  566. &tests->loopback[mode]);
  567. if (rc)
  568. goto out;
  569. }
  570. }
  571. out:
  572. /* Remove the flush. The caller will remove the loopback setting */
  573. state->flush = true;
  574. efx->loopback_selftest = NULL;
  575. wmb();
  576. kfree(state);
  577. return rc;
  578. }
  579. /**************************************************************************
  580. *
  581. * Entry point
  582. *
  583. *************************************************************************/
  584. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  585. unsigned flags)
  586. {
  587. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  588. int phy_mode = efx->phy_mode;
  589. int rc_test = 0, rc_reset, rc;
  590. efx_selftest_async_cancel(efx);
  591. /* Online (i.e. non-disruptive) testing
  592. * This checks interrupt generation, event delivery and PHY presence. */
  593. rc = efx_test_phy_alive(efx, tests);
  594. if (rc && !rc_test)
  595. rc_test = rc;
  596. rc = efx_test_nvram(efx, tests);
  597. if (rc && !rc_test)
  598. rc_test = rc;
  599. rc = efx_test_interrupts(efx, tests);
  600. if (rc && !rc_test)
  601. rc_test = rc;
  602. rc = efx_test_eventq_irq(efx, tests);
  603. if (rc && !rc_test)
  604. rc_test = rc;
  605. if (rc_test)
  606. return rc_test;
  607. if (!(flags & ETH_TEST_FL_OFFLINE))
  608. return efx_test_phy(efx, tests, flags);
  609. /* Offline (i.e. disruptive) testing
  610. * This checks MAC and PHY loopback on the specified port. */
  611. /* Detach the device so the kernel doesn't transmit during the
  612. * loopback test and the watchdog timeout doesn't fire.
  613. */
  614. efx_device_detach_sync(efx);
  615. if (efx->type->test_chip) {
  616. rc_reset = efx->type->test_chip(efx, tests);
  617. if (rc_reset) {
  618. netif_err(efx, hw, efx->net_dev,
  619. "Unable to recover from chip test\n");
  620. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  621. return rc_reset;
  622. }
  623. if ((tests->registers < 0) && !rc_test)
  624. rc_test = -EIO;
  625. }
  626. /* Ensure that the phy is powered and out of loopback
  627. * for the bist and loopback tests */
  628. mutex_lock(&efx->mac_lock);
  629. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  630. efx->loopback_mode = LOOPBACK_NONE;
  631. __efx_reconfigure_port(efx);
  632. mutex_unlock(&efx->mac_lock);
  633. rc = efx_test_phy(efx, tests, flags);
  634. if (rc && !rc_test)
  635. rc_test = rc;
  636. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  637. if (rc && !rc_test)
  638. rc_test = rc;
  639. /* restore the PHY to the previous state */
  640. mutex_lock(&efx->mac_lock);
  641. efx->phy_mode = phy_mode;
  642. efx->loopback_mode = loopback_mode;
  643. __efx_reconfigure_port(efx);
  644. mutex_unlock(&efx->mac_lock);
  645. netif_device_attach(efx->net_dev);
  646. return rc_test;
  647. }
  648. void efx_selftest_async_start(struct efx_nic *efx)
  649. {
  650. struct efx_channel *channel;
  651. efx_for_each_channel(channel, efx)
  652. efx_nic_event_test_start(channel);
  653. schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
  654. }
  655. void efx_selftest_async_cancel(struct efx_nic *efx)
  656. {
  657. cancel_delayed_work_sync(&efx->selftest_work);
  658. }
  659. void efx_selftest_async_work(struct work_struct *data)
  660. {
  661. struct efx_nic *efx = container_of(data, struct efx_nic,
  662. selftest_work.work);
  663. struct efx_channel *channel;
  664. int cpu;
  665. efx_for_each_channel(channel, efx) {
  666. cpu = efx_nic_event_test_irq_cpu(channel);
  667. if (cpu < 0)
  668. netif_err(efx, ifup, efx->net_dev,
  669. "channel %d failed to trigger an interrupt\n",
  670. channel->channel);
  671. else
  672. netif_dbg(efx, ifup, efx->net_dev,
  673. "channel %d triggered interrupt on CPU %d\n",
  674. channel->channel, cpu);
  675. }
  676. }