selftest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2009 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 <asm/io.h>
  21. #include "net_driver.h"
  22. #include "efx.h"
  23. #include "nic.h"
  24. #include "selftest.h"
  25. #include "workarounds.h"
  26. #include "spi.h"
  27. #include "io.h"
  28. /*
  29. * Loopback test packet structure
  30. *
  31. * The self-test should stress every RSS vector, and unfortunately
  32. * Falcon only performs RSS on TCP/UDP packets.
  33. */
  34. struct efx_loopback_payload {
  35. struct ethhdr header;
  36. struct iphdr ip;
  37. struct udphdr udp;
  38. __be16 iteration;
  39. const char msg[64];
  40. } __attribute__ ((packed));
  41. /* Loopback test source MAC address */
  42. static const unsigned char payload_source[ETH_ALEN] = {
  43. 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
  44. };
  45. static const char payload_msg[] =
  46. "Hello world! This is an Efx loopback test in progress!";
  47. /**
  48. * efx_loopback_state - persistent state during a loopback selftest
  49. * @flush: Drop all packets in efx_loopback_rx_packet
  50. * @packet_count: Number of packets being used in this test
  51. * @skbs: An array of skbs transmitted
  52. * @offload_csum: Checksums are being offloaded
  53. * @rx_good: RX good packet count
  54. * @rx_bad: RX bad packet count
  55. * @payload: Payload used in tests
  56. */
  57. struct efx_loopback_state {
  58. bool flush;
  59. int packet_count;
  60. struct sk_buff **skbs;
  61. bool offload_csum;
  62. atomic_t rx_good;
  63. atomic_t rx_bad;
  64. struct efx_loopback_payload payload;
  65. };
  66. /**************************************************************************
  67. *
  68. * MII, NVRAM and register tests
  69. *
  70. **************************************************************************/
  71. static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
  72. {
  73. int rc = 0;
  74. if (efx->phy_op->test_alive) {
  75. rc = efx->phy_op->test_alive(efx);
  76. tests->phy_alive = rc ? -1 : 1;
  77. }
  78. return rc;
  79. }
  80. static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
  81. {
  82. int rc = 0;
  83. if (efx->type->test_nvram) {
  84. rc = efx->type->test_nvram(efx);
  85. tests->nvram = rc ? -1 : 1;
  86. }
  87. return rc;
  88. }
  89. static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
  90. {
  91. int rc = 0;
  92. /* Test register access */
  93. if (efx->type->test_registers) {
  94. rc = efx->type->test_registers(efx);
  95. tests->registers = rc ? -1 : 1;
  96. }
  97. return rc;
  98. }
  99. /**************************************************************************
  100. *
  101. * Interrupt and event queue testing
  102. *
  103. **************************************************************************/
  104. /* Test generation and receipt of interrupts */
  105. static int efx_test_interrupts(struct efx_nic *efx,
  106. struct efx_self_tests *tests)
  107. {
  108. struct efx_channel *channel;
  109. EFX_LOG(efx, "testing interrupts\n");
  110. tests->interrupt = -1;
  111. /* Reset interrupt flag */
  112. efx->last_irq_cpu = -1;
  113. smp_wmb();
  114. /* ACK each interrupting event queue. Receiving an interrupt due to
  115. * traffic before a test event is raised is considered a pass */
  116. efx_for_each_channel(channel, efx) {
  117. if (channel->work_pending)
  118. efx_process_channel_now(channel);
  119. if (efx->last_irq_cpu >= 0)
  120. goto success;
  121. }
  122. efx_nic_generate_interrupt(efx);
  123. /* Wait for arrival of test interrupt. */
  124. EFX_LOG(efx, "waiting for test interrupt\n");
  125. schedule_timeout_uninterruptible(HZ / 10);
  126. if (efx->last_irq_cpu >= 0)
  127. goto success;
  128. EFX_ERR(efx, "timed out waiting for interrupt\n");
  129. return -ETIMEDOUT;
  130. success:
  131. EFX_LOG(efx, "%s test interrupt seen on CPU%d\n", INT_MODE(efx),
  132. efx->last_irq_cpu);
  133. tests->interrupt = 1;
  134. return 0;
  135. }
  136. /* Test generation and receipt of interrupting events */
  137. static int efx_test_eventq_irq(struct efx_channel *channel,
  138. struct efx_self_tests *tests)
  139. {
  140. unsigned int magic, count;
  141. /* Channel specific code, limited to 20 bits */
  142. magic = (0x00010150 + channel->channel);
  143. EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
  144. channel->channel, magic);
  145. tests->eventq_dma[channel->channel] = -1;
  146. tests->eventq_int[channel->channel] = -1;
  147. tests->eventq_poll[channel->channel] = -1;
  148. /* Reset flag and zero magic word */
  149. channel->efx->last_irq_cpu = -1;
  150. channel->eventq_magic = 0;
  151. smp_wmb();
  152. efx_nic_generate_test_event(channel, magic);
  153. /* Wait for arrival of interrupt */
  154. count = 0;
  155. do {
  156. schedule_timeout_uninterruptible(HZ / 100);
  157. if (channel->work_pending)
  158. efx_process_channel_now(channel);
  159. if (channel->eventq_magic == magic)
  160. goto eventq_ok;
  161. } while (++count < 2);
  162. EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
  163. channel->channel);
  164. /* See if interrupt arrived */
  165. if (channel->efx->last_irq_cpu >= 0) {
  166. EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
  167. "during event queue test\n", channel->channel,
  168. raw_smp_processor_id());
  169. tests->eventq_int[channel->channel] = 1;
  170. }
  171. /* Check to see if event was received even if interrupt wasn't */
  172. efx_process_channel_now(channel);
  173. if (channel->eventq_magic == magic) {
  174. EFX_ERR(channel->efx, "channel %d event was generated, but "
  175. "failed to trigger an interrupt\n", channel->channel);
  176. tests->eventq_dma[channel->channel] = 1;
  177. }
  178. return -ETIMEDOUT;
  179. eventq_ok:
  180. EFX_LOG(channel->efx, "channel %d event queue passed\n",
  181. channel->channel);
  182. tests->eventq_dma[channel->channel] = 1;
  183. tests->eventq_int[channel->channel] = 1;
  184. tests->eventq_poll[channel->channel] = 1;
  185. return 0;
  186. }
  187. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  188. unsigned flags)
  189. {
  190. int rc;
  191. if (!efx->phy_op->run_tests)
  192. return 0;
  193. mutex_lock(&efx->mac_lock);
  194. rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
  195. mutex_unlock(&efx->mac_lock);
  196. return rc;
  197. }
  198. /**************************************************************************
  199. *
  200. * Loopback testing
  201. * NB Only one loopback test can be executing concurrently.
  202. *
  203. **************************************************************************/
  204. /* Loopback test RX callback
  205. * This is called for each received packet during loopback testing.
  206. */
  207. void efx_loopback_rx_packet(struct efx_nic *efx,
  208. const char *buf_ptr, int pkt_len)
  209. {
  210. struct efx_loopback_state *state = efx->loopback_selftest;
  211. struct efx_loopback_payload *received;
  212. struct efx_loopback_payload *payload;
  213. BUG_ON(!buf_ptr);
  214. /* If we are just flushing, then drop the packet */
  215. if ((state == NULL) || state->flush)
  216. return;
  217. payload = &state->payload;
  218. received = (struct efx_loopback_payload *) buf_ptr;
  219. received->ip.saddr = payload->ip.saddr;
  220. if (state->offload_csum)
  221. received->ip.check = payload->ip.check;
  222. /* Check that header exists */
  223. if (pkt_len < sizeof(received->header)) {
  224. EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
  225. "test\n", pkt_len, LOOPBACK_MODE(efx));
  226. goto err;
  227. }
  228. /* Check that the ethernet header exists */
  229. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  230. EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
  231. LOOPBACK_MODE(efx));
  232. goto err;
  233. }
  234. /* Check packet length */
  235. if (pkt_len != sizeof(*payload)) {
  236. EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
  237. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  238. LOOPBACK_MODE(efx));
  239. goto err;
  240. }
  241. /* Check that IP header matches */
  242. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  243. EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
  244. LOOPBACK_MODE(efx));
  245. goto err;
  246. }
  247. /* Check that msg and padding matches */
  248. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  249. EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
  250. LOOPBACK_MODE(efx));
  251. goto err;
  252. }
  253. /* Check that iteration matches */
  254. if (received->iteration != payload->iteration) {
  255. EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
  256. "%s loopback test\n", ntohs(received->iteration),
  257. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  258. goto err;
  259. }
  260. /* Increase correct RX count */
  261. EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
  262. LOOPBACK_MODE(efx));
  263. atomic_inc(&state->rx_good);
  264. return;
  265. err:
  266. #ifdef EFX_ENABLE_DEBUG
  267. if (atomic_read(&state->rx_bad) == 0) {
  268. EFX_ERR(efx, "received packet:\n");
  269. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  270. buf_ptr, pkt_len, 0);
  271. EFX_ERR(efx, "expected packet:\n");
  272. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  273. &state->payload, sizeof(state->payload), 0);
  274. }
  275. #endif
  276. atomic_inc(&state->rx_bad);
  277. }
  278. /* Initialise an efx_selftest_state for a new iteration */
  279. static void efx_iterate_state(struct efx_nic *efx)
  280. {
  281. struct efx_loopback_state *state = efx->loopback_selftest;
  282. struct net_device *net_dev = efx->net_dev;
  283. struct efx_loopback_payload *payload = &state->payload;
  284. /* Initialise the layerII header */
  285. memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
  286. memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
  287. payload->header.h_proto = htons(ETH_P_IP);
  288. /* saddr set later and used as incrementing count */
  289. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  290. payload->ip.ihl = 5;
  291. payload->ip.check = htons(0xdead);
  292. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  293. payload->ip.version = IPVERSION;
  294. payload->ip.protocol = IPPROTO_UDP;
  295. /* Initialise udp header */
  296. payload->udp.source = 0;
  297. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  298. sizeof(struct iphdr));
  299. payload->udp.check = 0; /* checksum ignored */
  300. /* Fill out payload */
  301. payload->iteration = htons(ntohs(payload->iteration) + 1);
  302. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  303. /* Fill out remaining state members */
  304. atomic_set(&state->rx_good, 0);
  305. atomic_set(&state->rx_bad, 0);
  306. smp_wmb();
  307. }
  308. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  309. {
  310. struct efx_nic *efx = tx_queue->efx;
  311. struct efx_loopback_state *state = efx->loopback_selftest;
  312. struct efx_loopback_payload *payload;
  313. struct sk_buff *skb;
  314. int i;
  315. netdev_tx_t rc;
  316. /* Transmit N copies of buffer */
  317. for (i = 0; i < state->packet_count; i++) {
  318. /* Allocate an skb, holding an extra reference for
  319. * transmit completion counting */
  320. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  321. if (!skb)
  322. return -ENOMEM;
  323. state->skbs[i] = skb;
  324. skb_get(skb);
  325. /* Copy the payload in, incrementing the source address to
  326. * exercise the rss vectors */
  327. payload = ((struct efx_loopback_payload *)
  328. skb_put(skb, sizeof(state->payload)));
  329. memcpy(payload, &state->payload, sizeof(state->payload));
  330. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  331. /* Ensure everything we've written is visible to the
  332. * interrupt handler. */
  333. smp_wmb();
  334. if (efx_dev_registered(efx))
  335. netif_tx_lock_bh(efx->net_dev);
  336. rc = efx_enqueue_skb(tx_queue, skb);
  337. if (efx_dev_registered(efx))
  338. netif_tx_unlock_bh(efx->net_dev);
  339. if (rc != NETDEV_TX_OK) {
  340. EFX_ERR(efx, "TX queue %d could not transmit packet %d "
  341. "of %d in %s loopback test\n", tx_queue->queue,
  342. i + 1, state->packet_count, LOOPBACK_MODE(efx));
  343. /* Defer cleaning up the other skbs for the caller */
  344. kfree_skb(skb);
  345. return -EPIPE;
  346. }
  347. }
  348. return 0;
  349. }
  350. static int efx_poll_loopback(struct efx_nic *efx)
  351. {
  352. struct efx_loopback_state *state = efx->loopback_selftest;
  353. struct efx_channel *channel;
  354. /* NAPI polling is not enabled, so process channels
  355. * synchronously */
  356. efx_for_each_channel(channel, efx) {
  357. if (channel->work_pending)
  358. efx_process_channel_now(channel);
  359. }
  360. return atomic_read(&state->rx_good) == state->packet_count;
  361. }
  362. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  363. struct efx_loopback_self_tests *lb_tests)
  364. {
  365. struct efx_nic *efx = tx_queue->efx;
  366. struct efx_loopback_state *state = efx->loopback_selftest;
  367. struct sk_buff *skb;
  368. int tx_done = 0, rx_good, rx_bad;
  369. int i, rc = 0;
  370. if (efx_dev_registered(efx))
  371. netif_tx_lock_bh(efx->net_dev);
  372. /* Count the number of tx completions, and decrement the refcnt. Any
  373. * skbs not already completed will be free'd when the queue is flushed */
  374. for (i=0; i < state->packet_count; i++) {
  375. skb = state->skbs[i];
  376. if (skb && !skb_shared(skb))
  377. ++tx_done;
  378. dev_kfree_skb_any(skb);
  379. }
  380. if (efx_dev_registered(efx))
  381. netif_tx_unlock_bh(efx->net_dev);
  382. /* Check TX completion and received packet counts */
  383. rx_good = atomic_read(&state->rx_good);
  384. rx_bad = atomic_read(&state->rx_bad);
  385. if (tx_done != state->packet_count) {
  386. /* Don't free the skbs; they will be picked up on TX
  387. * overflow or channel teardown.
  388. */
  389. EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
  390. "TX completion events in %s loopback test\n",
  391. tx_queue->queue, tx_done, state->packet_count,
  392. LOOPBACK_MODE(efx));
  393. rc = -ETIMEDOUT;
  394. /* Allow to fall through so we see the RX errors as well */
  395. }
  396. /* We may always be up to a flush away from our desired packet total */
  397. if (rx_good != state->packet_count) {
  398. EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
  399. "received packets in %s loopback test\n",
  400. tx_queue->queue, rx_good, state->packet_count,
  401. LOOPBACK_MODE(efx));
  402. rc = -ETIMEDOUT;
  403. /* Fall through */
  404. }
  405. /* Update loopback test structure */
  406. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  407. lb_tests->tx_done[tx_queue->queue] += tx_done;
  408. lb_tests->rx_good += rx_good;
  409. lb_tests->rx_bad += rx_bad;
  410. return rc;
  411. }
  412. static int
  413. efx_test_loopback(struct efx_tx_queue *tx_queue,
  414. struct efx_loopback_self_tests *lb_tests)
  415. {
  416. struct efx_nic *efx = tx_queue->efx;
  417. struct efx_loopback_state *state = efx->loopback_selftest;
  418. int i, begin_rc, end_rc;
  419. for (i = 0; i < 3; i++) {
  420. /* Determine how many packets to send */
  421. state->packet_count = EFX_TXQ_SIZE / 3;
  422. state->packet_count = min(1 << (i << 2), state->packet_count);
  423. state->skbs = kzalloc(sizeof(state->skbs[0]) *
  424. state->packet_count, GFP_KERNEL);
  425. if (!state->skbs)
  426. return -ENOMEM;
  427. state->flush = false;
  428. EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
  429. "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  430. state->packet_count);
  431. efx_iterate_state(efx);
  432. begin_rc = efx_begin_loopback(tx_queue);
  433. /* This will normally complete very quickly, but be
  434. * prepared to wait up to 100 ms. */
  435. msleep(1);
  436. if (!efx_poll_loopback(efx)) {
  437. msleep(100);
  438. efx_poll_loopback(efx);
  439. }
  440. end_rc = efx_end_loopback(tx_queue, lb_tests);
  441. kfree(state->skbs);
  442. if (begin_rc || end_rc) {
  443. /* Wait a while to ensure there are no packets
  444. * floating around after a failure. */
  445. schedule_timeout_uninterruptible(HZ / 10);
  446. return begin_rc ? begin_rc : end_rc;
  447. }
  448. }
  449. EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
  450. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  451. state->packet_count);
  452. return 0;
  453. }
  454. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  455. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  456. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  457. * for link up and any faults to dissipate. */
  458. static int efx_wait_for_link(struct efx_nic *efx)
  459. {
  460. struct efx_link_state *link_state = &efx->link_state;
  461. int count;
  462. bool link_up;
  463. for (count = 0; count < 40; count++) {
  464. schedule_timeout_uninterruptible(HZ / 10);
  465. if (efx->type->monitor != NULL) {
  466. mutex_lock(&efx->mac_lock);
  467. efx->type->monitor(efx);
  468. mutex_unlock(&efx->mac_lock);
  469. } else {
  470. struct efx_channel *channel = &efx->channel[0];
  471. if (channel->work_pending)
  472. efx_process_channel_now(channel);
  473. }
  474. mutex_lock(&efx->mac_lock);
  475. link_up = link_state->up;
  476. if (link_up)
  477. link_up = !efx->mac_op->check_fault(efx);
  478. mutex_unlock(&efx->mac_lock);
  479. if (link_up)
  480. return 0;
  481. }
  482. return -ETIMEDOUT;
  483. }
  484. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  485. unsigned int loopback_modes)
  486. {
  487. enum efx_loopback_mode mode;
  488. struct efx_loopback_state *state;
  489. struct efx_tx_queue *tx_queue;
  490. int rc = 0;
  491. /* Set the port loopback_selftest member. From this point on
  492. * all received packets will be dropped. Mark the state as
  493. * "flushing" so all inflight packets are dropped */
  494. state = kzalloc(sizeof(*state), GFP_KERNEL);
  495. if (state == NULL)
  496. return -ENOMEM;
  497. BUG_ON(efx->loopback_selftest);
  498. state->flush = true;
  499. efx->loopback_selftest = state;
  500. /* Test all supported loopback modes */
  501. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  502. if (!(loopback_modes & (1 << mode)))
  503. continue;
  504. /* Move the port into the specified loopback mode. */
  505. state->flush = true;
  506. mutex_lock(&efx->mac_lock);
  507. efx->loopback_mode = mode;
  508. rc = __efx_reconfigure_port(efx);
  509. mutex_unlock(&efx->mac_lock);
  510. if (rc) {
  511. EFX_ERR(efx, "unable to move into %s loopback\n",
  512. LOOPBACK_MODE(efx));
  513. goto out;
  514. }
  515. rc = efx_wait_for_link(efx);
  516. if (rc) {
  517. EFX_ERR(efx, "loopback %s never came up\n",
  518. LOOPBACK_MODE(efx));
  519. goto out;
  520. }
  521. /* Test every TX queue */
  522. efx_for_each_tx_queue(tx_queue, efx) {
  523. state->offload_csum = (tx_queue->queue ==
  524. EFX_TX_QUEUE_OFFLOAD_CSUM);
  525. rc = efx_test_loopback(tx_queue,
  526. &tests->loopback[mode]);
  527. if (rc)
  528. goto out;
  529. }
  530. }
  531. out:
  532. /* Remove the flush. The caller will remove the loopback setting */
  533. state->flush = true;
  534. efx->loopback_selftest = NULL;
  535. wmb();
  536. kfree(state);
  537. return rc;
  538. }
  539. /**************************************************************************
  540. *
  541. * Entry point
  542. *
  543. *************************************************************************/
  544. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  545. unsigned flags)
  546. {
  547. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  548. int phy_mode = efx->phy_mode;
  549. enum reset_type reset_method = RESET_TYPE_INVISIBLE;
  550. struct efx_channel *channel;
  551. int rc_test = 0, rc_reset = 0, rc;
  552. /* Online (i.e. non-disruptive) testing
  553. * This checks interrupt generation, event delivery and PHY presence. */
  554. rc = efx_test_phy_alive(efx, tests);
  555. if (rc && !rc_test)
  556. rc_test = rc;
  557. rc = efx_test_nvram(efx, tests);
  558. if (rc && !rc_test)
  559. rc_test = rc;
  560. rc = efx_test_interrupts(efx, tests);
  561. if (rc && !rc_test)
  562. rc_test = rc;
  563. efx_for_each_channel(channel, efx) {
  564. rc = efx_test_eventq_irq(channel, tests);
  565. if (rc && !rc_test)
  566. rc_test = rc;
  567. }
  568. if (rc_test)
  569. return rc_test;
  570. if (!(flags & ETH_TEST_FL_OFFLINE))
  571. return efx_test_phy(efx, tests, flags);
  572. /* Offline (i.e. disruptive) testing
  573. * This checks MAC and PHY loopback on the specified port. */
  574. /* force the carrier state off so the kernel doesn't transmit during
  575. * the loopback test, and the watchdog timeout doesn't fire. Also put
  576. * falcon into loopback for the register test.
  577. */
  578. mutex_lock(&efx->mac_lock);
  579. efx->port_inhibited = true;
  580. if (efx->loopback_modes) {
  581. /* We need the 312 clock from the PHY to test the XMAC
  582. * registers, so move into XGMII loopback if available */
  583. if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
  584. efx->loopback_mode = LOOPBACK_XGMII;
  585. else
  586. efx->loopback_mode = __ffs(efx->loopback_modes);
  587. }
  588. __efx_reconfigure_port(efx);
  589. mutex_unlock(&efx->mac_lock);
  590. /* free up all consumers of SRAM (including all the queues) */
  591. efx_reset_down(efx, reset_method);
  592. rc = efx_test_chip(efx, tests);
  593. if (rc && !rc_test)
  594. rc_test = rc;
  595. /* reset the chip to recover from the register test */
  596. rc_reset = efx->type->reset(efx, reset_method);
  597. /* Ensure that the phy is powered and out of loopback
  598. * for the bist and loopback tests */
  599. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  600. efx->loopback_mode = LOOPBACK_NONE;
  601. rc = efx_reset_up(efx, reset_method, rc_reset == 0);
  602. if (rc && !rc_reset)
  603. rc_reset = rc;
  604. if (rc_reset) {
  605. EFX_ERR(efx, "Unable to recover from chip test\n");
  606. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  607. return rc_reset;
  608. }
  609. rc = efx_test_phy(efx, tests, flags);
  610. if (rc && !rc_test)
  611. rc_test = rc;
  612. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  613. if (rc && !rc_test)
  614. rc_test = rc;
  615. /* restore the PHY to the previous state */
  616. mutex_lock(&efx->mac_lock);
  617. efx->phy_mode = phy_mode;
  618. efx->port_inhibited = false;
  619. efx->loopback_mode = loopback_mode;
  620. __efx_reconfigure_port(efx);
  621. mutex_unlock(&efx->mac_lock);
  622. return rc_test;
  623. }