selftest.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. } __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. netif_dbg(efx, drv, efx->net_dev, "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. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  124. schedule_timeout_uninterruptible(HZ / 10);
  125. if (efx->last_irq_cpu >= 0)
  126. goto success;
  127. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  128. return -ETIMEDOUT;
  129. success:
  130. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  131. 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. struct efx_nic *efx = channel->efx;
  141. unsigned int magic_count, count;
  142. tests->eventq_dma[channel->channel] = -1;
  143. tests->eventq_int[channel->channel] = -1;
  144. tests->eventq_poll[channel->channel] = -1;
  145. magic_count = channel->magic_count;
  146. channel->efx->last_irq_cpu = -1;
  147. smp_wmb();
  148. efx_nic_generate_test_event(channel);
  149. /* Wait for arrival of interrupt */
  150. count = 0;
  151. do {
  152. schedule_timeout_uninterruptible(HZ / 100);
  153. if (channel->work_pending)
  154. efx_process_channel_now(channel);
  155. if (channel->magic_count != magic_count)
  156. goto eventq_ok;
  157. } while (++count < 2);
  158. netif_err(efx, drv, efx->net_dev,
  159. "channel %d timed out waiting for event queue\n",
  160. channel->channel);
  161. /* See if interrupt arrived */
  162. if (channel->efx->last_irq_cpu >= 0) {
  163. netif_err(efx, drv, efx->net_dev,
  164. "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->magic_count != magic_count) {
  172. netif_err(efx, drv, efx->net_dev,
  173. "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. netif_dbg(efx, drv, efx->net_dev, "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. netif_err(efx, drv, efx->net_dev,
  224. "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. netif_err(efx, drv, efx->net_dev,
  231. "saw non-loopback RX packet in %s loopback test\n",
  232. LOOPBACK_MODE(efx));
  233. goto err;
  234. }
  235. /* Check packet length */
  236. if (pkt_len != sizeof(*payload)) {
  237. netif_err(efx, drv, efx->net_dev,
  238. "saw incorrect RX packet length %d (wanted %d) in "
  239. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  240. LOOPBACK_MODE(efx));
  241. goto err;
  242. }
  243. /* Check that IP header matches */
  244. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  245. netif_err(efx, drv, efx->net_dev,
  246. "saw corrupted IP header in %s loopback test\n",
  247. LOOPBACK_MODE(efx));
  248. goto err;
  249. }
  250. /* Check that msg and padding matches */
  251. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  252. netif_err(efx, drv, efx->net_dev,
  253. "saw corrupted RX packet in %s loopback test\n",
  254. LOOPBACK_MODE(efx));
  255. goto err;
  256. }
  257. /* Check that iteration matches */
  258. if (received->iteration != payload->iteration) {
  259. netif_err(efx, drv, efx->net_dev,
  260. "saw RX packet from iteration %d (wanted %d) in "
  261. "%s loopback test\n", ntohs(received->iteration),
  262. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  263. goto err;
  264. }
  265. /* Increase correct RX count */
  266. netif_vdbg(efx, drv, efx->net_dev,
  267. "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
  268. atomic_inc(&state->rx_good);
  269. return;
  270. err:
  271. #ifdef EFX_ENABLE_DEBUG
  272. if (atomic_read(&state->rx_bad) == 0) {
  273. netif_err(efx, drv, efx->net_dev, "received packet:\n");
  274. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  275. buf_ptr, pkt_len, 0);
  276. netif_err(efx, drv, efx->net_dev, "expected packet:\n");
  277. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  278. &state->payload, sizeof(state->payload), 0);
  279. }
  280. #endif
  281. atomic_inc(&state->rx_bad);
  282. }
  283. /* Initialise an efx_selftest_state for a new iteration */
  284. static void efx_iterate_state(struct efx_nic *efx)
  285. {
  286. struct efx_loopback_state *state = efx->loopback_selftest;
  287. struct net_device *net_dev = efx->net_dev;
  288. struct efx_loopback_payload *payload = &state->payload;
  289. /* Initialise the layerII header */
  290. memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
  291. memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
  292. payload->header.h_proto = htons(ETH_P_IP);
  293. /* saddr set later and used as incrementing count */
  294. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  295. payload->ip.ihl = 5;
  296. payload->ip.check = htons(0xdead);
  297. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  298. payload->ip.version = IPVERSION;
  299. payload->ip.protocol = IPPROTO_UDP;
  300. /* Initialise udp header */
  301. payload->udp.source = 0;
  302. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  303. sizeof(struct iphdr));
  304. payload->udp.check = 0; /* checksum ignored */
  305. /* Fill out payload */
  306. payload->iteration = htons(ntohs(payload->iteration) + 1);
  307. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  308. /* Fill out remaining state members */
  309. atomic_set(&state->rx_good, 0);
  310. atomic_set(&state->rx_bad, 0);
  311. smp_wmb();
  312. }
  313. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  314. {
  315. struct efx_nic *efx = tx_queue->efx;
  316. struct efx_loopback_state *state = efx->loopback_selftest;
  317. struct efx_loopback_payload *payload;
  318. struct sk_buff *skb;
  319. int i;
  320. netdev_tx_t rc;
  321. /* Transmit N copies of buffer */
  322. for (i = 0; i < state->packet_count; i++) {
  323. /* Allocate an skb, holding an extra reference for
  324. * transmit completion counting */
  325. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  326. if (!skb)
  327. return -ENOMEM;
  328. state->skbs[i] = skb;
  329. skb_get(skb);
  330. /* Copy the payload in, incrementing the source address to
  331. * exercise the rss vectors */
  332. payload = ((struct efx_loopback_payload *)
  333. skb_put(skb, sizeof(state->payload)));
  334. memcpy(payload, &state->payload, sizeof(state->payload));
  335. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  336. /* Ensure everything we've written is visible to the
  337. * interrupt handler. */
  338. smp_wmb();
  339. if (efx_dev_registered(efx))
  340. netif_tx_lock_bh(efx->net_dev);
  341. rc = efx_enqueue_skb(tx_queue, skb);
  342. if (efx_dev_registered(efx))
  343. netif_tx_unlock_bh(efx->net_dev);
  344. if (rc != NETDEV_TX_OK) {
  345. netif_err(efx, drv, efx->net_dev,
  346. "TX queue %d could not transmit packet %d of "
  347. "%d in %s loopback test\n", tx_queue->queue,
  348. i + 1, state->packet_count,
  349. LOOPBACK_MODE(efx));
  350. /* Defer cleaning up the other skbs for the caller */
  351. kfree_skb(skb);
  352. return -EPIPE;
  353. }
  354. }
  355. return 0;
  356. }
  357. static int efx_poll_loopback(struct efx_nic *efx)
  358. {
  359. struct efx_loopback_state *state = efx->loopback_selftest;
  360. struct efx_channel *channel;
  361. /* NAPI polling is not enabled, so process channels
  362. * synchronously */
  363. efx_for_each_channel(channel, efx) {
  364. if (channel->work_pending)
  365. efx_process_channel_now(channel);
  366. }
  367. return atomic_read(&state->rx_good) == state->packet_count;
  368. }
  369. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  370. struct efx_loopback_self_tests *lb_tests)
  371. {
  372. struct efx_nic *efx = tx_queue->efx;
  373. struct efx_loopback_state *state = efx->loopback_selftest;
  374. struct sk_buff *skb;
  375. int tx_done = 0, rx_good, rx_bad;
  376. int i, rc = 0;
  377. if (efx_dev_registered(efx))
  378. netif_tx_lock_bh(efx->net_dev);
  379. /* Count the number of tx completions, and decrement the refcnt. Any
  380. * skbs not already completed will be free'd when the queue is flushed */
  381. for (i=0; i < state->packet_count; i++) {
  382. skb = state->skbs[i];
  383. if (skb && !skb_shared(skb))
  384. ++tx_done;
  385. dev_kfree_skb_any(skb);
  386. }
  387. if (efx_dev_registered(efx))
  388. netif_tx_unlock_bh(efx->net_dev);
  389. /* Check TX completion and received packet counts */
  390. rx_good = atomic_read(&state->rx_good);
  391. rx_bad = atomic_read(&state->rx_bad);
  392. if (tx_done != state->packet_count) {
  393. /* Don't free the skbs; they will be picked up on TX
  394. * overflow or channel teardown.
  395. */
  396. netif_err(efx, drv, efx->net_dev,
  397. "TX queue %d saw only %d out of an expected %d "
  398. "TX completion events in %s loopback test\n",
  399. tx_queue->queue, tx_done, state->packet_count,
  400. LOOPBACK_MODE(efx));
  401. rc = -ETIMEDOUT;
  402. /* Allow to fall through so we see the RX errors as well */
  403. }
  404. /* We may always be up to a flush away from our desired packet total */
  405. if (rx_good != state->packet_count) {
  406. netif_dbg(efx, drv, efx->net_dev,
  407. "TX queue %d saw only %d out of an expected %d "
  408. "received packets in %s loopback test\n",
  409. tx_queue->queue, rx_good, state->packet_count,
  410. LOOPBACK_MODE(efx));
  411. rc = -ETIMEDOUT;
  412. /* Fall through */
  413. }
  414. /* Update loopback test structure */
  415. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  416. lb_tests->tx_done[tx_queue->queue] += tx_done;
  417. lb_tests->rx_good += rx_good;
  418. lb_tests->rx_bad += rx_bad;
  419. return rc;
  420. }
  421. static int
  422. efx_test_loopback(struct efx_tx_queue *tx_queue,
  423. struct efx_loopback_self_tests *lb_tests)
  424. {
  425. struct efx_nic *efx = tx_queue->efx;
  426. struct efx_loopback_state *state = efx->loopback_selftest;
  427. int i, begin_rc, end_rc;
  428. for (i = 0; i < 3; i++) {
  429. /* Determine how many packets to send */
  430. state->packet_count = EFX_TXQ_SIZE / 3;
  431. state->packet_count = min(1 << (i << 2), state->packet_count);
  432. state->skbs = kzalloc(sizeof(state->skbs[0]) *
  433. state->packet_count, GFP_KERNEL);
  434. if (!state->skbs)
  435. return -ENOMEM;
  436. state->flush = false;
  437. netif_dbg(efx, drv, efx->net_dev,
  438. "TX queue %d testing %s loopback with %d packets\n",
  439. tx_queue->queue, LOOPBACK_MODE(efx),
  440. state->packet_count);
  441. efx_iterate_state(efx);
  442. begin_rc = efx_begin_loopback(tx_queue);
  443. /* This will normally complete very quickly, but be
  444. * prepared to wait up to 100 ms. */
  445. msleep(1);
  446. if (!efx_poll_loopback(efx)) {
  447. msleep(100);
  448. efx_poll_loopback(efx);
  449. }
  450. end_rc = efx_end_loopback(tx_queue, lb_tests);
  451. kfree(state->skbs);
  452. if (begin_rc || end_rc) {
  453. /* Wait a while to ensure there are no packets
  454. * floating around after a failure. */
  455. schedule_timeout_uninterruptible(HZ / 10);
  456. return begin_rc ? begin_rc : end_rc;
  457. }
  458. }
  459. netif_dbg(efx, drv, efx->net_dev,
  460. "TX queue %d passed %s loopback test with a burst length "
  461. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  462. state->packet_count);
  463. return 0;
  464. }
  465. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  466. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  467. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  468. * for link up and any faults to dissipate. */
  469. static int efx_wait_for_link(struct efx_nic *efx)
  470. {
  471. struct efx_link_state *link_state = &efx->link_state;
  472. int count, link_up_count = 0;
  473. bool link_up;
  474. for (count = 0; count < 40; count++) {
  475. schedule_timeout_uninterruptible(HZ / 10);
  476. if (efx->type->monitor != NULL) {
  477. mutex_lock(&efx->mac_lock);
  478. efx->type->monitor(efx);
  479. mutex_unlock(&efx->mac_lock);
  480. } else {
  481. struct efx_channel *channel = &efx->channel[0];
  482. if (channel->work_pending)
  483. efx_process_channel_now(channel);
  484. }
  485. mutex_lock(&efx->mac_lock);
  486. link_up = link_state->up;
  487. if (link_up)
  488. link_up = !efx->mac_op->check_fault(efx);
  489. mutex_unlock(&efx->mac_lock);
  490. if (link_up) {
  491. if (++link_up_count == 2)
  492. return 0;
  493. } else {
  494. link_up_count = 0;
  495. }
  496. }
  497. return -ETIMEDOUT;
  498. }
  499. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  500. unsigned int loopback_modes)
  501. {
  502. enum efx_loopback_mode mode;
  503. struct efx_loopback_state *state;
  504. struct efx_tx_queue *tx_queue;
  505. int rc = 0;
  506. /* Set the port loopback_selftest member. From this point on
  507. * all received packets will be dropped. Mark the state as
  508. * "flushing" so all inflight packets are dropped */
  509. state = kzalloc(sizeof(*state), GFP_KERNEL);
  510. if (state == NULL)
  511. return -ENOMEM;
  512. BUG_ON(efx->loopback_selftest);
  513. state->flush = true;
  514. efx->loopback_selftest = state;
  515. /* Test all supported loopback modes */
  516. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  517. if (!(loopback_modes & (1 << mode)))
  518. continue;
  519. /* Move the port into the specified loopback mode. */
  520. state->flush = true;
  521. mutex_lock(&efx->mac_lock);
  522. efx->loopback_mode = mode;
  523. rc = __efx_reconfigure_port(efx);
  524. mutex_unlock(&efx->mac_lock);
  525. if (rc) {
  526. netif_err(efx, drv, efx->net_dev,
  527. "unable to move into %s loopback\n",
  528. LOOPBACK_MODE(efx));
  529. goto out;
  530. }
  531. rc = efx_wait_for_link(efx);
  532. if (rc) {
  533. netif_err(efx, drv, efx->net_dev,
  534. "loopback %s never came up\n",
  535. LOOPBACK_MODE(efx));
  536. goto out;
  537. }
  538. /* Test both types of TX queue */
  539. efx_for_each_channel_tx_queue(tx_queue, &efx->channel[0]) {
  540. state->offload_csum = (tx_queue->queue &
  541. EFX_TXQ_TYPE_OFFLOAD);
  542. rc = efx_test_loopback(tx_queue,
  543. &tests->loopback[mode]);
  544. if (rc)
  545. goto out;
  546. }
  547. }
  548. out:
  549. /* Remove the flush. The caller will remove the loopback setting */
  550. state->flush = true;
  551. efx->loopback_selftest = NULL;
  552. wmb();
  553. kfree(state);
  554. return rc;
  555. }
  556. /**************************************************************************
  557. *
  558. * Entry point
  559. *
  560. *************************************************************************/
  561. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  562. unsigned flags)
  563. {
  564. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  565. int phy_mode = efx->phy_mode;
  566. enum reset_type reset_method = RESET_TYPE_INVISIBLE;
  567. struct efx_channel *channel;
  568. int rc_test = 0, rc_reset = 0, rc;
  569. /* Online (i.e. non-disruptive) testing
  570. * This checks interrupt generation, event delivery and PHY presence. */
  571. rc = efx_test_phy_alive(efx, tests);
  572. if (rc && !rc_test)
  573. rc_test = rc;
  574. rc = efx_test_nvram(efx, tests);
  575. if (rc && !rc_test)
  576. rc_test = rc;
  577. rc = efx_test_interrupts(efx, tests);
  578. if (rc && !rc_test)
  579. rc_test = rc;
  580. efx_for_each_channel(channel, efx) {
  581. rc = efx_test_eventq_irq(channel, tests);
  582. if (rc && !rc_test)
  583. rc_test = rc;
  584. }
  585. if (rc_test)
  586. return rc_test;
  587. if (!(flags & ETH_TEST_FL_OFFLINE))
  588. return efx_test_phy(efx, tests, flags);
  589. /* Offline (i.e. disruptive) testing
  590. * This checks MAC and PHY loopback on the specified port. */
  591. /* force the carrier state off so the kernel doesn't transmit during
  592. * the loopback test, and the watchdog timeout doesn't fire. Also put
  593. * falcon into loopback for the register test.
  594. */
  595. mutex_lock(&efx->mac_lock);
  596. efx->port_inhibited = true;
  597. if (efx->loopback_modes) {
  598. /* We need the 312 clock from the PHY to test the XMAC
  599. * registers, so move into XGMII loopback if available */
  600. if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
  601. efx->loopback_mode = LOOPBACK_XGMII;
  602. else
  603. efx->loopback_mode = __ffs(efx->loopback_modes);
  604. }
  605. __efx_reconfigure_port(efx);
  606. mutex_unlock(&efx->mac_lock);
  607. /* free up all consumers of SRAM (including all the queues) */
  608. efx_reset_down(efx, reset_method);
  609. rc = efx_test_chip(efx, tests);
  610. if (rc && !rc_test)
  611. rc_test = rc;
  612. /* reset the chip to recover from the register test */
  613. rc_reset = efx->type->reset(efx, reset_method);
  614. /* Ensure that the phy is powered and out of loopback
  615. * for the bist and loopback tests */
  616. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  617. efx->loopback_mode = LOOPBACK_NONE;
  618. rc = efx_reset_up(efx, reset_method, rc_reset == 0);
  619. if (rc && !rc_reset)
  620. rc_reset = rc;
  621. if (rc_reset) {
  622. netif_err(efx, drv, efx->net_dev,
  623. "Unable to recover from chip test\n");
  624. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  625. return rc_reset;
  626. }
  627. rc = efx_test_phy(efx, tests, flags);
  628. if (rc && !rc_test)
  629. rc_test = rc;
  630. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  631. if (rc && !rc_test)
  632. rc_test = rc;
  633. /* restore the PHY to the previous state */
  634. mutex_lock(&efx->mac_lock);
  635. efx->phy_mode = phy_mode;
  636. efx->port_inhibited = false;
  637. efx->loopback_mode = loopback_mode;
  638. __efx_reconfigure_port(efx);
  639. mutex_unlock(&efx->mac_lock);
  640. return rc_test;
  641. }