selftest.c 21 KB

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