selftest.c 20 KB

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