selftest.c 20 KB

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