selftest.c 21 KB

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