selftest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 "mac.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_selftest_state - persistent state during a 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. * @rx_good: RX good packet count
  54. * @rx_bad: RX bad packet count
  55. * @payload: Payload used in tests
  56. */
  57. struct efx_selftest_state {
  58. int flush;
  59. int packet_count;
  60. struct sk_buff **skbs;
  61. atomic_t rx_good;
  62. atomic_t rx_bad;
  63. struct efx_loopback_payload payload;
  64. };
  65. /**************************************************************************
  66. *
  67. * Configurable values
  68. *
  69. **************************************************************************/
  70. /* Level of loopback testing
  71. *
  72. * The maximum packet burst length is 16**(n-1), i.e.
  73. *
  74. * - Level 0 : no packets
  75. * - Level 1 : 1 packet
  76. * - Level 2 : 17 packets (1 * 1 packet, 1 * 16 packets)
  77. * - Level 3 : 273 packets (1 * 1 packet, 1 * 16 packet, 1 * 256 packets)
  78. *
  79. */
  80. static unsigned int loopback_test_level = 3;
  81. /**************************************************************************
  82. *
  83. * Interrupt and event queue testing
  84. *
  85. **************************************************************************/
  86. /* Test generation and receipt of interrupts */
  87. static int efx_test_interrupts(struct efx_nic *efx,
  88. struct efx_self_tests *tests)
  89. {
  90. struct efx_channel *channel;
  91. EFX_LOG(efx, "testing interrupts\n");
  92. tests->interrupt = -1;
  93. /* Reset interrupt flag */
  94. efx->last_irq_cpu = -1;
  95. smp_wmb();
  96. /* ACK each interrupting event queue. Receiving an interrupt due to
  97. * traffic before a test event is raised is considered a pass */
  98. efx_for_each_channel_with_interrupt(channel, efx) {
  99. if (channel->work_pending)
  100. efx_process_channel_now(channel);
  101. if (efx->last_irq_cpu >= 0)
  102. goto success;
  103. }
  104. falcon_generate_interrupt(efx);
  105. /* Wait for arrival of test interrupt. */
  106. EFX_LOG(efx, "waiting for test interrupt\n");
  107. schedule_timeout_uninterruptible(HZ / 10);
  108. if (efx->last_irq_cpu >= 0)
  109. goto success;
  110. EFX_ERR(efx, "timed out waiting for interrupt\n");
  111. return -ETIMEDOUT;
  112. success:
  113. EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n",
  114. efx->interrupt_mode, efx->last_irq_cpu);
  115. tests->interrupt = 1;
  116. return 0;
  117. }
  118. /* Test generation and receipt of non-interrupting events */
  119. static int efx_test_eventq(struct efx_channel *channel,
  120. struct efx_self_tests *tests)
  121. {
  122. unsigned int magic;
  123. /* Channel specific code, limited to 20 bits */
  124. magic = (0x00010150 + channel->channel);
  125. EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
  126. channel->channel, magic);
  127. tests->eventq_dma[channel->channel] = -1;
  128. tests->eventq_int[channel->channel] = 1; /* fake pass */
  129. tests->eventq_poll[channel->channel] = 1; /* fake pass */
  130. /* Reset flag and zero magic word */
  131. channel->efx->last_irq_cpu = -1;
  132. channel->eventq_magic = 0;
  133. smp_wmb();
  134. falcon_generate_test_event(channel, magic);
  135. udelay(1);
  136. efx_process_channel_now(channel);
  137. if (channel->eventq_magic != magic) {
  138. EFX_ERR(channel->efx, "channel %d failed to see test event\n",
  139. channel->channel);
  140. return -ETIMEDOUT;
  141. } else {
  142. tests->eventq_dma[channel->channel] = 1;
  143. }
  144. return 0;
  145. }
  146. /* Test generation and receipt of interrupting events */
  147. static int efx_test_eventq_irq(struct efx_channel *channel,
  148. struct efx_self_tests *tests)
  149. {
  150. unsigned int magic, count;
  151. /* Channel specific code, limited to 20 bits */
  152. magic = (0x00010150 + channel->channel);
  153. EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n",
  154. channel->channel, magic);
  155. tests->eventq_dma[channel->channel] = -1;
  156. tests->eventq_int[channel->channel] = -1;
  157. tests->eventq_poll[channel->channel] = -1;
  158. /* Reset flag and zero magic word */
  159. channel->efx->last_irq_cpu = -1;
  160. channel->eventq_magic = 0;
  161. smp_wmb();
  162. falcon_generate_test_event(channel, magic);
  163. /* Wait for arrival of interrupt */
  164. count = 0;
  165. do {
  166. schedule_timeout_uninterruptible(HZ / 100);
  167. if (channel->work_pending)
  168. efx_process_channel_now(channel);
  169. if (channel->eventq_magic == magic)
  170. goto eventq_ok;
  171. } while (++count < 2);
  172. EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n",
  173. channel->channel);
  174. /* See if interrupt arrived */
  175. if (channel->efx->last_irq_cpu >= 0) {
  176. EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d "
  177. "during event queue test\n", channel->channel,
  178. raw_smp_processor_id());
  179. tests->eventq_int[channel->channel] = 1;
  180. }
  181. /* Check to see if event was received even if interrupt wasn't */
  182. efx_process_channel_now(channel);
  183. if (channel->eventq_magic == magic) {
  184. EFX_ERR(channel->efx, "channel %d event was generated, but "
  185. "failed to trigger an interrupt\n", channel->channel);
  186. tests->eventq_dma[channel->channel] = 1;
  187. }
  188. return -ETIMEDOUT;
  189. eventq_ok:
  190. EFX_LOG(channel->efx, "channel %d event queue passed\n",
  191. channel->channel);
  192. tests->eventq_dma[channel->channel] = 1;
  193. tests->eventq_int[channel->channel] = 1;
  194. tests->eventq_poll[channel->channel] = 1;
  195. return 0;
  196. }
  197. /**************************************************************************
  198. *
  199. * PHY testing
  200. *
  201. **************************************************************************/
  202. /* Check PHY presence by reading the PHY ID registers */
  203. static int efx_test_phy(struct efx_nic *efx,
  204. struct efx_self_tests *tests)
  205. {
  206. u16 physid1, physid2;
  207. struct mii_if_info *mii = &efx->mii;
  208. struct net_device *net_dev = efx->net_dev;
  209. if (efx->phy_type == PHY_TYPE_NONE)
  210. return 0;
  211. EFX_LOG(efx, "testing PHY presence\n");
  212. tests->phy_ok = -1;
  213. physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1);
  214. physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2);
  215. if ((physid1 != 0x0000) && (physid1 != 0xffff) &&
  216. (physid2 != 0x0000) && (physid2 != 0xffff)) {
  217. EFX_LOG(efx, "found MII PHY %d ID 0x%x:%x\n",
  218. mii->phy_id, physid1, physid2);
  219. tests->phy_ok = 1;
  220. return 0;
  221. }
  222. EFX_ERR(efx, "no MII PHY present with ID %d\n", mii->phy_id);
  223. return -ENODEV;
  224. }
  225. /**************************************************************************
  226. *
  227. * Loopback testing
  228. * NB Only one loopback test can be executing concurrently.
  229. *
  230. **************************************************************************/
  231. /* Loopback test RX callback
  232. * This is called for each received packet during loopback testing.
  233. */
  234. void efx_loopback_rx_packet(struct efx_nic *efx,
  235. const char *buf_ptr, int pkt_len)
  236. {
  237. struct efx_selftest_state *state = efx->loopback_selftest;
  238. struct efx_loopback_payload *received;
  239. struct efx_loopback_payload *payload;
  240. BUG_ON(!buf_ptr);
  241. /* If we are just flushing, then drop the packet */
  242. if ((state == NULL) || state->flush)
  243. return;
  244. payload = &state->payload;
  245. received = (struct efx_loopback_payload *) buf_ptr;
  246. received->ip.saddr = payload->ip.saddr;
  247. received->ip.check = payload->ip.check;
  248. /* Check that header exists */
  249. if (pkt_len < sizeof(received->header)) {
  250. EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback "
  251. "test\n", pkt_len, LOOPBACK_MODE(efx));
  252. goto err;
  253. }
  254. /* Check that the ethernet header exists */
  255. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  256. EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n",
  257. LOOPBACK_MODE(efx));
  258. goto err;
  259. }
  260. /* Check packet length */
  261. if (pkt_len != sizeof(*payload)) {
  262. EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in "
  263. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  264. LOOPBACK_MODE(efx));
  265. goto err;
  266. }
  267. /* Check that IP header matches */
  268. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  269. EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n",
  270. LOOPBACK_MODE(efx));
  271. goto err;
  272. }
  273. /* Check that msg and padding matches */
  274. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  275. EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n",
  276. LOOPBACK_MODE(efx));
  277. goto err;
  278. }
  279. /* Check that iteration matches */
  280. if (received->iteration != payload->iteration) {
  281. EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in "
  282. "%s loopback test\n", ntohs(received->iteration),
  283. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  284. goto err;
  285. }
  286. /* Increase correct RX count */
  287. EFX_TRACE(efx, "got loopback RX in %s loopback test\n",
  288. LOOPBACK_MODE(efx));
  289. atomic_inc(&state->rx_good);
  290. return;
  291. err:
  292. #ifdef EFX_ENABLE_DEBUG
  293. if (atomic_read(&state->rx_bad) == 0) {
  294. EFX_ERR(efx, "received packet:\n");
  295. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  296. buf_ptr, pkt_len, 0);
  297. EFX_ERR(efx, "expected packet:\n");
  298. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  299. &state->payload, sizeof(state->payload), 0);
  300. }
  301. #endif
  302. atomic_inc(&state->rx_bad);
  303. }
  304. /* Initialise an efx_selftest_state for a new iteration */
  305. static void efx_iterate_state(struct efx_nic *efx)
  306. {
  307. struct efx_selftest_state *state = efx->loopback_selftest;
  308. struct net_device *net_dev = efx->net_dev;
  309. struct efx_loopback_payload *payload = &state->payload;
  310. /* Initialise the layerII header */
  311. memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
  312. memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
  313. payload->header.h_proto = htons(ETH_P_IP);
  314. /* saddr set later and used as incrementing count */
  315. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  316. payload->ip.ihl = 5;
  317. payload->ip.check = htons(0xdead);
  318. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  319. payload->ip.version = IPVERSION;
  320. payload->ip.protocol = IPPROTO_UDP;
  321. /* Initialise udp header */
  322. payload->udp.source = 0;
  323. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  324. sizeof(struct iphdr));
  325. payload->udp.check = 0; /* checksum ignored */
  326. /* Fill out payload */
  327. payload->iteration = htons(ntohs(payload->iteration) + 1);
  328. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  329. /* Fill out remaining state members */
  330. atomic_set(&state->rx_good, 0);
  331. atomic_set(&state->rx_bad, 0);
  332. smp_wmb();
  333. }
  334. static int efx_tx_loopback(struct efx_tx_queue *tx_queue)
  335. {
  336. struct efx_nic *efx = tx_queue->efx;
  337. struct efx_selftest_state *state = efx->loopback_selftest;
  338. struct efx_loopback_payload *payload;
  339. struct sk_buff *skb;
  340. int i, rc;
  341. /* Transmit N copies of buffer */
  342. for (i = 0; i < state->packet_count; i++) {
  343. /* Allocate an skb, holding an extra reference for
  344. * transmit completion counting */
  345. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  346. if (!skb)
  347. return -ENOMEM;
  348. state->skbs[i] = skb;
  349. skb_get(skb);
  350. /* Copy the payload in, incrementing the source address to
  351. * exercise the rss vectors */
  352. payload = ((struct efx_loopback_payload *)
  353. skb_put(skb, sizeof(state->payload)));
  354. memcpy(payload, &state->payload, sizeof(state->payload));
  355. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  356. /* Ensure everything we've written is visible to the
  357. * interrupt handler. */
  358. smp_wmb();
  359. if (efx_dev_registered(efx))
  360. netif_tx_lock_bh(efx->net_dev);
  361. rc = efx_xmit(efx, tx_queue, skb);
  362. if (efx_dev_registered(efx))
  363. netif_tx_unlock_bh(efx->net_dev);
  364. if (rc != NETDEV_TX_OK) {
  365. EFX_ERR(efx, "TX queue %d could not transmit packet %d "
  366. "of %d in %s loopback test\n", tx_queue->queue,
  367. i + 1, state->packet_count, LOOPBACK_MODE(efx));
  368. /* Defer cleaning up the other skbs for the caller */
  369. kfree_skb(skb);
  370. return -EPIPE;
  371. }
  372. }
  373. return 0;
  374. }
  375. static int efx_rx_loopback(struct efx_tx_queue *tx_queue,
  376. struct efx_loopback_self_tests *lb_tests)
  377. {
  378. struct efx_nic *efx = tx_queue->efx;
  379. struct efx_selftest_state *state = efx->loopback_selftest;
  380. struct sk_buff *skb;
  381. int tx_done = 0, rx_good, rx_bad;
  382. int i, rc = 0;
  383. if (efx_dev_registered(efx))
  384. netif_tx_lock_bh(efx->net_dev);
  385. /* Count the number of tx completions, and decrement the refcnt. Any
  386. * skbs not already completed will be free'd when the queue is flushed */
  387. for (i=0; i < state->packet_count; i++) {
  388. skb = state->skbs[i];
  389. if (skb && !skb_shared(skb))
  390. ++tx_done;
  391. dev_kfree_skb_any(skb);
  392. }
  393. if (efx_dev_registered(efx))
  394. netif_tx_unlock_bh(efx->net_dev);
  395. /* Check TX completion and received packet counts */
  396. rx_good = atomic_read(&state->rx_good);
  397. rx_bad = atomic_read(&state->rx_bad);
  398. if (tx_done != state->packet_count) {
  399. /* Don't free the skbs; they will be picked up on TX
  400. * overflow or channel teardown.
  401. */
  402. EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d "
  403. "TX completion events in %s loopback test\n",
  404. tx_queue->queue, tx_done, state->packet_count,
  405. LOOPBACK_MODE(efx));
  406. rc = -ETIMEDOUT;
  407. /* Allow to fall through so we see the RX errors as well */
  408. }
  409. /* We may always be up to a flush away from our desired packet total */
  410. if (rx_good != state->packet_count) {
  411. EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d "
  412. "received packets in %s loopback test\n",
  413. tx_queue->queue, rx_good, state->packet_count,
  414. LOOPBACK_MODE(efx));
  415. rc = -ETIMEDOUT;
  416. /* Fall through */
  417. }
  418. /* Update loopback test structure */
  419. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  420. lb_tests->tx_done[tx_queue->queue] += tx_done;
  421. lb_tests->rx_good += rx_good;
  422. lb_tests->rx_bad += rx_bad;
  423. return rc;
  424. }
  425. static int
  426. efx_test_loopback(struct efx_tx_queue *tx_queue,
  427. struct efx_loopback_self_tests *lb_tests)
  428. {
  429. struct efx_nic *efx = tx_queue->efx;
  430. struct efx_selftest_state *state = efx->loopback_selftest;
  431. struct efx_channel *channel;
  432. int i, rc = 0;
  433. for (i = 0; i < loopback_test_level; i++) {
  434. /* Determine how many packets to send */
  435. state->packet_count = (efx->type->txd_ring_mask + 1) / 3;
  436. state->packet_count = min(1 << (i << 2), state->packet_count);
  437. state->skbs = kzalloc(sizeof(state->skbs[0]) *
  438. state->packet_count, GFP_KERNEL);
  439. if (!state->skbs)
  440. return -ENOMEM;
  441. state->flush = 0;
  442. EFX_LOG(efx, "TX queue %d testing %s loopback with %d "
  443. "packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  444. state->packet_count);
  445. efx_iterate_state(efx);
  446. rc = efx_tx_loopback(tx_queue);
  447. /* NAPI polling is not enabled, so process channels synchronously */
  448. schedule_timeout_uninterruptible(HZ / 50);
  449. efx_for_each_channel_with_interrupt(channel, efx) {
  450. if (channel->work_pending)
  451. efx_process_channel_now(channel);
  452. }
  453. rc |= efx_rx_loopback(tx_queue, lb_tests);
  454. kfree(state->skbs);
  455. if (rc) {
  456. /* Wait a while to ensure there are no packets
  457. * floating around after a failure. */
  458. schedule_timeout_uninterruptible(HZ / 10);
  459. return rc;
  460. }
  461. }
  462. EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length "
  463. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  464. state->packet_count);
  465. return rc;
  466. }
  467. static int efx_test_loopbacks(struct efx_nic *efx,
  468. struct efx_self_tests *tests,
  469. unsigned int loopback_modes)
  470. {
  471. struct efx_selftest_state *state = efx->loopback_selftest;
  472. struct ethtool_cmd ecmd, ecmd_loopback;
  473. struct efx_tx_queue *tx_queue;
  474. enum efx_loopback_mode old_mode, mode;
  475. int count, rc = 0, link_up;
  476. rc = efx_ethtool_get_settings(efx->net_dev, &ecmd);
  477. if (rc) {
  478. EFX_ERR(efx, "could not get GMII settings\n");
  479. return rc;
  480. }
  481. old_mode = efx->loopback_mode;
  482. /* Disable autonegotiation for the purposes of loopback */
  483. memcpy(&ecmd_loopback, &ecmd, sizeof(ecmd_loopback));
  484. if (ecmd_loopback.autoneg == AUTONEG_ENABLE) {
  485. ecmd_loopback.autoneg = AUTONEG_DISABLE;
  486. ecmd_loopback.duplex = DUPLEX_FULL;
  487. ecmd_loopback.speed = SPEED_10000;
  488. }
  489. rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback);
  490. if (rc) {
  491. EFX_ERR(efx, "could not disable autonegotiation\n");
  492. goto out;
  493. }
  494. tests->loopback_speed = ecmd_loopback.speed;
  495. tests->loopback_full_duplex = ecmd_loopback.duplex;
  496. /* Test all supported loopback modes */
  497. for (mode = LOOPBACK_NONE; mode < LOOPBACK_TEST_MAX; mode++) {
  498. if (!(loopback_modes & (1 << mode)))
  499. continue;
  500. /* Move the port into the specified loopback mode. */
  501. state->flush = 1;
  502. efx->loopback_mode = mode;
  503. efx_reconfigure_port(efx);
  504. /* Wait for the PHY to signal the link is up */
  505. count = 0;
  506. do {
  507. struct efx_channel *channel = &efx->channel[0];
  508. falcon_check_xmac(efx);
  509. schedule_timeout_uninterruptible(HZ / 10);
  510. if (channel->work_pending)
  511. efx_process_channel_now(channel);
  512. /* Wait for PHY events to be processed */
  513. flush_workqueue(efx->workqueue);
  514. rmb();
  515. /* efx->link_up can be 1 even if the XAUI link is down,
  516. * (bug5762). Usually, it's not worth bothering with the
  517. * difference, but for selftests, we need that extra
  518. * guarantee that the link is really, really, up.
  519. */
  520. link_up = efx->link_up;
  521. if (!falcon_xaui_link_ok(efx))
  522. link_up = 0;
  523. } while ((++count < 20) && !link_up);
  524. /* The link should now be up. If it isn't, there is no point
  525. * in attempting a loopback test */
  526. if (!link_up) {
  527. EFX_ERR(efx, "loopback %s never came up\n",
  528. LOOPBACK_MODE(efx));
  529. rc = -EIO;
  530. goto out;
  531. }
  532. EFX_LOG(efx, "link came up in %s loopback in %d iterations\n",
  533. LOOPBACK_MODE(efx), count);
  534. /* Test every TX queue */
  535. efx_for_each_tx_queue(tx_queue, efx) {
  536. rc |= efx_test_loopback(tx_queue,
  537. &tests->loopback[mode]);
  538. if (rc)
  539. goto out;
  540. }
  541. }
  542. out:
  543. /* Take out of loopback and restore PHY settings */
  544. state->flush = 1;
  545. efx->loopback_mode = old_mode;
  546. efx_ethtool_set_settings(efx->net_dev, &ecmd);
  547. return rc;
  548. }
  549. /**************************************************************************
  550. *
  551. * Entry points
  552. *
  553. *************************************************************************/
  554. /* Online (i.e. non-disruptive) testing
  555. * This checks interrupt generation, event delivery and PHY presence. */
  556. int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests)
  557. {
  558. struct efx_channel *channel;
  559. int rc = 0;
  560. EFX_LOG(efx, "performing online self-tests\n");
  561. rc |= efx_test_interrupts(efx, tests);
  562. efx_for_each_channel(channel, efx) {
  563. if (channel->has_interrupt)
  564. rc |= efx_test_eventq_irq(channel, tests);
  565. else
  566. rc |= efx_test_eventq(channel, tests);
  567. }
  568. rc |= efx_test_phy(efx, tests);
  569. if (rc)
  570. EFX_ERR(efx, "failed online self-tests\n");
  571. return rc;
  572. }
  573. /* Offline (i.e. disruptive) testing
  574. * This checks MAC and PHY loopback on the specified port. */
  575. int efx_offline_test(struct efx_nic *efx,
  576. struct efx_self_tests *tests, unsigned int loopback_modes)
  577. {
  578. struct efx_selftest_state *state;
  579. int rc = 0;
  580. EFX_LOG(efx, "performing offline self-tests\n");
  581. /* Create a selftest_state structure to hold state for the test */
  582. state = kzalloc(sizeof(*state), GFP_KERNEL);
  583. if (state == NULL) {
  584. rc = -ENOMEM;
  585. goto out;
  586. }
  587. /* Set the port loopback_selftest member. From this point on
  588. * all received packets will be dropped. Mark the state as
  589. * "flushing" so all inflight packets are dropped */
  590. BUG_ON(efx->loopback_selftest);
  591. state->flush = 1;
  592. efx->loopback_selftest = state;
  593. rc = efx_test_loopbacks(efx, tests, loopback_modes);
  594. efx->loopback_selftest = NULL;
  595. wmb();
  596. kfree(state);
  597. out:
  598. if (rc)
  599. EFX_ERR(efx, "failed offline self-tests\n");
  600. return rc;
  601. }