selftest.c 21 KB

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