selftest.c 21 KB

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