selftest.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2012 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 <linux/slab.h>
  21. #include "net_driver.h"
  22. #include "efx.h"
  23. #include "nic.h"
  24. #include "selftest.h"
  25. #include "workarounds.h"
  26. /* IRQ latency can be enormous because:
  27. * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
  28. * slow serial console or an old IDE driver doing error recovery
  29. * - The PREEMPT_RT patches mostly deal with this, but also allow a
  30. * tasklet or normal task to be given higher priority than our IRQ
  31. * threads
  32. * Try to avoid blaming the hardware for this.
  33. */
  34. #define IRQ_TIMEOUT HZ
  35. /*
  36. * Loopback test packet structure
  37. *
  38. * The self-test should stress every RSS vector, and unfortunately
  39. * Falcon only performs RSS on TCP/UDP packets.
  40. */
  41. struct efx_loopback_payload {
  42. struct ethhdr header;
  43. struct iphdr ip;
  44. struct udphdr udp;
  45. __be16 iteration;
  46. char msg[64];
  47. } __packed;
  48. /* Loopback test source MAC address */
  49. static const u8 payload_source[ETH_ALEN] __aligned(2) = {
  50. 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
  51. };
  52. static const char payload_msg[] =
  53. "Hello world! This is an Efx loopback test in progress!";
  54. /* Interrupt mode names */
  55. static const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
  56. static const char *const efx_interrupt_mode_names[] = {
  57. [EFX_INT_MODE_MSIX] = "MSI-X",
  58. [EFX_INT_MODE_MSI] = "MSI",
  59. [EFX_INT_MODE_LEGACY] = "legacy",
  60. };
  61. #define INT_MODE(efx) \
  62. STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_interrupt_mode)
  63. /**
  64. * efx_loopback_state - persistent state during a loopback selftest
  65. * @flush: Drop all packets in efx_loopback_rx_packet
  66. * @packet_count: Number of packets being used in this test
  67. * @skbs: An array of skbs transmitted
  68. * @offload_csum: Checksums are being offloaded
  69. * @rx_good: RX good packet count
  70. * @rx_bad: RX bad packet count
  71. * @payload: Payload used in tests
  72. */
  73. struct efx_loopback_state {
  74. bool flush;
  75. int packet_count;
  76. struct sk_buff **skbs;
  77. bool offload_csum;
  78. atomic_t rx_good;
  79. atomic_t rx_bad;
  80. struct efx_loopback_payload payload;
  81. };
  82. /* How long to wait for all the packets to arrive (in ms) */
  83. #define LOOPBACK_TIMEOUT_MS 1000
  84. /**************************************************************************
  85. *
  86. * MII, NVRAM and register tests
  87. *
  88. **************************************************************************/
  89. static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
  90. {
  91. int rc = 0;
  92. if (efx->phy_op->test_alive) {
  93. rc = efx->phy_op->test_alive(efx);
  94. tests->phy_alive = rc ? -1 : 1;
  95. }
  96. return rc;
  97. }
  98. static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
  99. {
  100. int rc = 0;
  101. if (efx->type->test_nvram) {
  102. rc = efx->type->test_nvram(efx);
  103. tests->nvram = rc ? -1 : 1;
  104. }
  105. return rc;
  106. }
  107. /**************************************************************************
  108. *
  109. * Interrupt and event queue testing
  110. *
  111. **************************************************************************/
  112. /* Test generation and receipt of interrupts */
  113. static int efx_test_interrupts(struct efx_nic *efx,
  114. struct efx_self_tests *tests)
  115. {
  116. unsigned long timeout, wait;
  117. int cpu;
  118. netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
  119. tests->interrupt = -1;
  120. efx_nic_irq_test_start(efx);
  121. timeout = jiffies + IRQ_TIMEOUT;
  122. wait = 1;
  123. /* Wait for arrival of test interrupt. */
  124. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  125. do {
  126. schedule_timeout_uninterruptible(wait);
  127. cpu = efx_nic_irq_test_irq_cpu(efx);
  128. if (cpu >= 0)
  129. goto success;
  130. wait *= 2;
  131. } while (time_before(jiffies, timeout));
  132. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  133. return -ETIMEDOUT;
  134. success:
  135. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  136. INT_MODE(efx), cpu);
  137. tests->interrupt = 1;
  138. return 0;
  139. }
  140. /* Test generation and receipt of interrupting events */
  141. static int efx_test_eventq_irq(struct efx_nic *efx,
  142. struct efx_self_tests *tests)
  143. {
  144. struct efx_channel *channel;
  145. unsigned int read_ptr[EFX_MAX_CHANNELS];
  146. unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
  147. unsigned long timeout, wait;
  148. BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
  149. efx_for_each_channel(channel, efx) {
  150. read_ptr[channel->channel] = channel->eventq_read_ptr;
  151. set_bit(channel->channel, &dma_pend);
  152. set_bit(channel->channel, &int_pend);
  153. efx_nic_event_test_start(channel);
  154. }
  155. timeout = jiffies + IRQ_TIMEOUT;
  156. wait = 1;
  157. /* Wait for arrival of interrupts. NAPI processing may or may
  158. * not complete in time, but we can cope in any case.
  159. */
  160. do {
  161. schedule_timeout_uninterruptible(wait);
  162. efx_for_each_channel(channel, efx) {
  163. efx_stop_eventq(channel);
  164. if (channel->eventq_read_ptr !=
  165. read_ptr[channel->channel]) {
  166. set_bit(channel->channel, &napi_ran);
  167. clear_bit(channel->channel, &dma_pend);
  168. clear_bit(channel->channel, &int_pend);
  169. } else {
  170. if (efx_nic_event_present(channel))
  171. clear_bit(channel->channel, &dma_pend);
  172. if (efx_nic_event_test_irq_cpu(channel) >= 0)
  173. clear_bit(channel->channel, &int_pend);
  174. }
  175. efx_start_eventq(channel);
  176. }
  177. wait *= 2;
  178. } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
  179. efx_for_each_channel(channel, efx) {
  180. bool dma_seen = !test_bit(channel->channel, &dma_pend);
  181. bool int_seen = !test_bit(channel->channel, &int_pend);
  182. tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
  183. tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
  184. if (dma_seen && int_seen) {
  185. netif_dbg(efx, drv, efx->net_dev,
  186. "channel %d event queue passed (with%s NAPI)\n",
  187. channel->channel,
  188. test_bit(channel->channel, &napi_ran) ?
  189. "" : "out");
  190. } else {
  191. /* Report failure and whether either interrupt or DMA
  192. * worked
  193. */
  194. netif_err(efx, drv, efx->net_dev,
  195. "channel %d timed out waiting for event queue\n",
  196. channel->channel);
  197. if (int_seen)
  198. netif_err(efx, drv, efx->net_dev,
  199. "channel %d saw interrupt "
  200. "during event queue test\n",
  201. channel->channel);
  202. if (dma_seen)
  203. netif_err(efx, drv, efx->net_dev,
  204. "channel %d event was generated, but "
  205. "failed to trigger an interrupt\n",
  206. channel->channel);
  207. }
  208. }
  209. return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
  210. }
  211. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  212. unsigned flags)
  213. {
  214. int rc;
  215. if (!efx->phy_op->run_tests)
  216. return 0;
  217. mutex_lock(&efx->mac_lock);
  218. rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
  219. mutex_unlock(&efx->mac_lock);
  220. return rc;
  221. }
  222. /**************************************************************************
  223. *
  224. * Loopback testing
  225. * NB Only one loopback test can be executing concurrently.
  226. *
  227. **************************************************************************/
  228. /* Loopback test RX callback
  229. * This is called for each received packet during loopback testing.
  230. */
  231. void efx_loopback_rx_packet(struct efx_nic *efx,
  232. const char *buf_ptr, int pkt_len)
  233. {
  234. struct efx_loopback_state *state = efx->loopback_selftest;
  235. struct efx_loopback_payload *received;
  236. struct efx_loopback_payload *payload;
  237. BUG_ON(!buf_ptr);
  238. /* If we are just flushing, then drop the packet */
  239. if ((state == NULL) || state->flush)
  240. return;
  241. payload = &state->payload;
  242. received = (struct efx_loopback_payload *) buf_ptr;
  243. received->ip.saddr = payload->ip.saddr;
  244. if (state->offload_csum)
  245. received->ip.check = payload->ip.check;
  246. /* Check that header exists */
  247. if (pkt_len < sizeof(received->header)) {
  248. netif_err(efx, drv, efx->net_dev,
  249. "saw runt RX packet (length %d) in %s loopback "
  250. "test\n", pkt_len, LOOPBACK_MODE(efx));
  251. goto err;
  252. }
  253. /* Check that the ethernet header exists */
  254. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  255. netif_err(efx, drv, efx->net_dev,
  256. "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. netif_err(efx, drv, efx->net_dev,
  263. "saw incorrect RX packet length %d (wanted %d) in "
  264. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  265. LOOPBACK_MODE(efx));
  266. goto err;
  267. }
  268. /* Check that IP header matches */
  269. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  270. netif_err(efx, drv, efx->net_dev,
  271. "saw corrupted IP header in %s loopback test\n",
  272. LOOPBACK_MODE(efx));
  273. goto err;
  274. }
  275. /* Check that msg and padding matches */
  276. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  277. netif_err(efx, drv, efx->net_dev,
  278. "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. netif_err(efx, drv, efx->net_dev,
  285. "saw RX packet from iteration %d (wanted %d) in "
  286. "%s loopback test\n", ntohs(received->iteration),
  287. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  288. goto err;
  289. }
  290. /* Increase correct RX count */
  291. netif_vdbg(efx, drv, efx->net_dev,
  292. "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
  293. atomic_inc(&state->rx_good);
  294. return;
  295. err:
  296. #ifdef DEBUG
  297. if (atomic_read(&state->rx_bad) == 0) {
  298. netif_err(efx, drv, efx->net_dev, "received packet:\n");
  299. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  300. buf_ptr, pkt_len, 0);
  301. netif_err(efx, drv, efx->net_dev, "expected packet:\n");
  302. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  303. &state->payload, sizeof(state->payload), 0);
  304. }
  305. #endif
  306. atomic_inc(&state->rx_bad);
  307. }
  308. /* Initialise an efx_selftest_state for a new iteration */
  309. static void efx_iterate_state(struct efx_nic *efx)
  310. {
  311. struct efx_loopback_state *state = efx->loopback_selftest;
  312. struct net_device *net_dev = efx->net_dev;
  313. struct efx_loopback_payload *payload = &state->payload;
  314. /* Initialise the layerII header */
  315. ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
  316. ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
  317. payload->header.h_proto = htons(ETH_P_IP);
  318. /* saddr set later and used as incrementing count */
  319. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  320. payload->ip.ihl = 5;
  321. payload->ip.check = (__force __sum16) htons(0xdead);
  322. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  323. payload->ip.version = IPVERSION;
  324. payload->ip.protocol = IPPROTO_UDP;
  325. /* Initialise udp header */
  326. payload->udp.source = 0;
  327. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  328. sizeof(struct iphdr));
  329. payload->udp.check = 0; /* checksum ignored */
  330. /* Fill out payload */
  331. payload->iteration = htons(ntohs(payload->iteration) + 1);
  332. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  333. /* Fill out remaining state members */
  334. atomic_set(&state->rx_good, 0);
  335. atomic_set(&state->rx_bad, 0);
  336. smp_wmb();
  337. }
  338. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  339. {
  340. struct efx_nic *efx = tx_queue->efx;
  341. struct efx_loopback_state *state = efx->loopback_selftest;
  342. struct efx_loopback_payload *payload;
  343. struct sk_buff *skb;
  344. int i;
  345. netdev_tx_t rc;
  346. /* Transmit N copies of buffer */
  347. for (i = 0; i < state->packet_count; i++) {
  348. /* Allocate an skb, holding an extra reference for
  349. * transmit completion counting */
  350. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  351. if (!skb)
  352. return -ENOMEM;
  353. state->skbs[i] = skb;
  354. skb_get(skb);
  355. /* Copy the payload in, incrementing the source address to
  356. * exercise the rss vectors */
  357. payload = ((struct efx_loopback_payload *)
  358. skb_put(skb, sizeof(state->payload)));
  359. memcpy(payload, &state->payload, sizeof(state->payload));
  360. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  361. /* Ensure everything we've written is visible to the
  362. * interrupt handler. */
  363. smp_wmb();
  364. netif_tx_lock_bh(efx->net_dev);
  365. rc = efx_enqueue_skb(tx_queue, skb);
  366. netif_tx_unlock_bh(efx->net_dev);
  367. if (rc != NETDEV_TX_OK) {
  368. netif_err(efx, drv, efx->net_dev,
  369. "TX queue %d could not transmit packet %d of "
  370. "%d in %s loopback test\n", tx_queue->queue,
  371. i + 1, state->packet_count,
  372. LOOPBACK_MODE(efx));
  373. /* Defer cleaning up the other skbs for the caller */
  374. kfree_skb(skb);
  375. return -EPIPE;
  376. }
  377. }
  378. return 0;
  379. }
  380. static int efx_poll_loopback(struct efx_nic *efx)
  381. {
  382. struct efx_loopback_state *state = efx->loopback_selftest;
  383. return atomic_read(&state->rx_good) == state->packet_count;
  384. }
  385. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  386. struct efx_loopback_self_tests *lb_tests)
  387. {
  388. struct efx_nic *efx = tx_queue->efx;
  389. struct efx_loopback_state *state = efx->loopback_selftest;
  390. struct sk_buff *skb;
  391. int tx_done = 0, rx_good, rx_bad;
  392. int i, rc = 0;
  393. netif_tx_lock_bh(efx->net_dev);
  394. /* Count the number of tx completions, and decrement the refcnt. Any
  395. * skbs not already completed will be free'd when the queue is flushed */
  396. for (i = 0; i < state->packet_count; i++) {
  397. skb = state->skbs[i];
  398. if (skb && !skb_shared(skb))
  399. ++tx_done;
  400. dev_kfree_skb(skb);
  401. }
  402. netif_tx_unlock_bh(efx->net_dev);
  403. /* Check TX completion and received packet counts */
  404. rx_good = atomic_read(&state->rx_good);
  405. rx_bad = atomic_read(&state->rx_bad);
  406. if (tx_done != state->packet_count) {
  407. /* Don't free the skbs; they will be picked up on TX
  408. * overflow or channel teardown.
  409. */
  410. netif_err(efx, drv, efx->net_dev,
  411. "TX queue %d saw only %d out of an expected %d "
  412. "TX completion events in %s loopback test\n",
  413. tx_queue->queue, tx_done, state->packet_count,
  414. LOOPBACK_MODE(efx));
  415. rc = -ETIMEDOUT;
  416. /* Allow to fall through so we see the RX errors as well */
  417. }
  418. /* We may always be up to a flush away from our desired packet total */
  419. if (rx_good != state->packet_count) {
  420. netif_dbg(efx, drv, efx->net_dev,
  421. "TX queue %d saw only %d out of an expected %d "
  422. "received packets in %s loopback test\n",
  423. tx_queue->queue, rx_good, state->packet_count,
  424. LOOPBACK_MODE(efx));
  425. rc = -ETIMEDOUT;
  426. /* Fall through */
  427. }
  428. /* Update loopback test structure */
  429. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  430. lb_tests->tx_done[tx_queue->queue] += tx_done;
  431. lb_tests->rx_good += rx_good;
  432. lb_tests->rx_bad += rx_bad;
  433. return rc;
  434. }
  435. static int
  436. efx_test_loopback(struct efx_tx_queue *tx_queue,
  437. struct efx_loopback_self_tests *lb_tests)
  438. {
  439. struct efx_nic *efx = tx_queue->efx;
  440. struct efx_loopback_state *state = efx->loopback_selftest;
  441. int i, begin_rc, end_rc;
  442. for (i = 0; i < 3; i++) {
  443. /* Determine how many packets to send */
  444. state->packet_count = efx->txq_entries / 3;
  445. state->packet_count = min(1 << (i << 2), state->packet_count);
  446. state->skbs = kcalloc(state->packet_count,
  447. sizeof(state->skbs[0]), GFP_KERNEL);
  448. if (!state->skbs)
  449. return -ENOMEM;
  450. state->flush = false;
  451. netif_dbg(efx, drv, efx->net_dev,
  452. "TX queue %d testing %s loopback with %d packets\n",
  453. tx_queue->queue, LOOPBACK_MODE(efx),
  454. state->packet_count);
  455. efx_iterate_state(efx);
  456. begin_rc = efx_begin_loopback(tx_queue);
  457. /* This will normally complete very quickly, but be
  458. * prepared to wait much longer. */
  459. msleep(1);
  460. if (!efx_poll_loopback(efx)) {
  461. msleep(LOOPBACK_TIMEOUT_MS);
  462. efx_poll_loopback(efx);
  463. }
  464. end_rc = efx_end_loopback(tx_queue, lb_tests);
  465. kfree(state->skbs);
  466. if (begin_rc || end_rc) {
  467. /* Wait a while to ensure there are no packets
  468. * floating around after a failure. */
  469. schedule_timeout_uninterruptible(HZ / 10);
  470. return begin_rc ? begin_rc : end_rc;
  471. }
  472. }
  473. netif_dbg(efx, drv, efx->net_dev,
  474. "TX queue %d passed %s loopback test with a burst length "
  475. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  476. state->packet_count);
  477. return 0;
  478. }
  479. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  480. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  481. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  482. * for link up and any faults to dissipate. */
  483. static int efx_wait_for_link(struct efx_nic *efx)
  484. {
  485. struct efx_link_state *link_state = &efx->link_state;
  486. int count, link_up_count = 0;
  487. bool link_up;
  488. for (count = 0; count < 40; count++) {
  489. schedule_timeout_uninterruptible(HZ / 10);
  490. if (efx->type->monitor != NULL) {
  491. mutex_lock(&efx->mac_lock);
  492. efx->type->monitor(efx);
  493. mutex_unlock(&efx->mac_lock);
  494. }
  495. mutex_lock(&efx->mac_lock);
  496. link_up = link_state->up;
  497. if (link_up)
  498. link_up = !efx->type->check_mac_fault(efx);
  499. mutex_unlock(&efx->mac_lock);
  500. if (link_up) {
  501. if (++link_up_count == 2)
  502. return 0;
  503. } else {
  504. link_up_count = 0;
  505. }
  506. }
  507. return -ETIMEDOUT;
  508. }
  509. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  510. unsigned int loopback_modes)
  511. {
  512. enum efx_loopback_mode mode;
  513. struct efx_loopback_state *state;
  514. struct efx_channel *channel =
  515. efx_get_channel(efx, efx->tx_channel_offset);
  516. struct efx_tx_queue *tx_queue;
  517. int rc = 0;
  518. /* Set the port loopback_selftest member. From this point on
  519. * all received packets will be dropped. Mark the state as
  520. * "flushing" so all inflight packets are dropped */
  521. state = kzalloc(sizeof(*state), GFP_KERNEL);
  522. if (state == NULL)
  523. return -ENOMEM;
  524. BUG_ON(efx->loopback_selftest);
  525. state->flush = true;
  526. efx->loopback_selftest = state;
  527. /* Test all supported loopback modes */
  528. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  529. if (!(loopback_modes & (1 << mode)))
  530. continue;
  531. /* Move the port into the specified loopback mode. */
  532. state->flush = true;
  533. mutex_lock(&efx->mac_lock);
  534. efx->loopback_mode = mode;
  535. rc = __efx_reconfigure_port(efx);
  536. mutex_unlock(&efx->mac_lock);
  537. if (rc) {
  538. netif_err(efx, drv, efx->net_dev,
  539. "unable to move into %s loopback\n",
  540. LOOPBACK_MODE(efx));
  541. goto out;
  542. }
  543. rc = efx_wait_for_link(efx);
  544. if (rc) {
  545. netif_err(efx, drv, efx->net_dev,
  546. "loopback %s never came up\n",
  547. LOOPBACK_MODE(efx));
  548. goto out;
  549. }
  550. /* Test all enabled types of TX queue */
  551. efx_for_each_channel_tx_queue(tx_queue, channel) {
  552. state->offload_csum = (tx_queue->queue &
  553. EFX_TXQ_TYPE_OFFLOAD);
  554. rc = efx_test_loopback(tx_queue,
  555. &tests->loopback[mode]);
  556. if (rc)
  557. goto out;
  558. }
  559. }
  560. out:
  561. /* Remove the flush. The caller will remove the loopback setting */
  562. state->flush = true;
  563. efx->loopback_selftest = NULL;
  564. wmb();
  565. kfree(state);
  566. return rc;
  567. }
  568. /**************************************************************************
  569. *
  570. * Entry point
  571. *
  572. *************************************************************************/
  573. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  574. unsigned flags)
  575. {
  576. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  577. int phy_mode = efx->phy_mode;
  578. int rc_test = 0, rc_reset, rc;
  579. efx_selftest_async_cancel(efx);
  580. /* Online (i.e. non-disruptive) testing
  581. * This checks interrupt generation, event delivery and PHY presence. */
  582. rc = efx_test_phy_alive(efx, tests);
  583. if (rc && !rc_test)
  584. rc_test = rc;
  585. rc = efx_test_nvram(efx, tests);
  586. if (rc && !rc_test)
  587. rc_test = rc;
  588. rc = efx_test_interrupts(efx, tests);
  589. if (rc && !rc_test)
  590. rc_test = rc;
  591. rc = efx_test_eventq_irq(efx, tests);
  592. if (rc && !rc_test)
  593. rc_test = rc;
  594. if (rc_test)
  595. return rc_test;
  596. if (!(flags & ETH_TEST_FL_OFFLINE))
  597. return efx_test_phy(efx, tests, flags);
  598. /* Offline (i.e. disruptive) testing
  599. * This checks MAC and PHY loopback on the specified port. */
  600. /* Detach the device so the kernel doesn't transmit during the
  601. * loopback test and the watchdog timeout doesn't fire.
  602. */
  603. efx_device_detach_sync(efx);
  604. if (efx->type->test_chip) {
  605. rc_reset = efx->type->test_chip(efx, tests);
  606. if (rc_reset) {
  607. netif_err(efx, hw, efx->net_dev,
  608. "Unable to recover from chip test\n");
  609. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  610. return rc_reset;
  611. }
  612. if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
  613. rc_test = -EIO;
  614. }
  615. /* Ensure that the phy is powered and out of loopback
  616. * for the bist and loopback tests */
  617. mutex_lock(&efx->mac_lock);
  618. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  619. efx->loopback_mode = LOOPBACK_NONE;
  620. __efx_reconfigure_port(efx);
  621. mutex_unlock(&efx->mac_lock);
  622. rc = efx_test_phy(efx, tests, flags);
  623. if (rc && !rc_test)
  624. rc_test = rc;
  625. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  626. if (rc && !rc_test)
  627. rc_test = rc;
  628. /* restore the PHY to the previous state */
  629. mutex_lock(&efx->mac_lock);
  630. efx->phy_mode = phy_mode;
  631. efx->loopback_mode = loopback_mode;
  632. __efx_reconfigure_port(efx);
  633. mutex_unlock(&efx->mac_lock);
  634. netif_device_attach(efx->net_dev);
  635. return rc_test;
  636. }
  637. void efx_selftest_async_start(struct efx_nic *efx)
  638. {
  639. struct efx_channel *channel;
  640. efx_for_each_channel(channel, efx)
  641. efx_nic_event_test_start(channel);
  642. schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
  643. }
  644. void efx_selftest_async_cancel(struct efx_nic *efx)
  645. {
  646. cancel_delayed_work_sync(&efx->selftest_work);
  647. }
  648. void efx_selftest_async_work(struct work_struct *data)
  649. {
  650. struct efx_nic *efx = container_of(data, struct efx_nic,
  651. selftest_work.work);
  652. struct efx_channel *channel;
  653. int cpu;
  654. efx_for_each_channel(channel, efx) {
  655. cpu = efx_nic_event_test_irq_cpu(channel);
  656. if (cpu < 0)
  657. netif_err(efx, ifup, efx->net_dev,
  658. "channel %d failed to trigger an interrupt\n",
  659. channel->channel);
  660. else
  661. netif_dbg(efx, ifup, efx->net_dev,
  662. "channel %d triggered interrupt on CPU %d\n",
  663. channel->channel, cpu);
  664. }
  665. }