selftest.c 21 KB

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