selftest.c 20 KB

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