selftest.c 20 KB

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