selftest.c 19 KB

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