selftest.c 21 KB

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