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