selftest.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2010 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. const char msg[64];
  47. } __packed;
  48. /* Loopback test source MAC address */
  49. static const unsigned char payload_source[ETH_ALEN] = {
  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. static int efx_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
  108. {
  109. int rc = 0;
  110. /* Test register access */
  111. if (efx->type->test_registers) {
  112. rc = efx->type->test_registers(efx);
  113. tests->registers = rc ? -1 : 1;
  114. }
  115. return rc;
  116. }
  117. /**************************************************************************
  118. *
  119. * Interrupt and event queue testing
  120. *
  121. **************************************************************************/
  122. /* Test generation and receipt of interrupts */
  123. static int efx_test_interrupts(struct efx_nic *efx,
  124. struct efx_self_tests *tests)
  125. {
  126. unsigned long timeout, wait;
  127. int cpu;
  128. netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
  129. tests->interrupt = -1;
  130. efx_nic_irq_test_start(efx);
  131. timeout = jiffies + IRQ_TIMEOUT;
  132. wait = 1;
  133. /* Wait for arrival of test interrupt. */
  134. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  135. do {
  136. schedule_timeout_uninterruptible(wait);
  137. cpu = efx_nic_irq_test_irq_cpu(efx);
  138. if (cpu >= 0)
  139. goto success;
  140. wait *= 2;
  141. } while (time_before(jiffies, timeout));
  142. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  143. return -ETIMEDOUT;
  144. success:
  145. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  146. INT_MODE(efx), cpu);
  147. tests->interrupt = 1;
  148. return 0;
  149. }
  150. /* Test generation and receipt of interrupting events */
  151. static int efx_test_eventq_irq(struct efx_nic *efx,
  152. struct efx_self_tests *tests)
  153. {
  154. struct efx_channel *channel;
  155. unsigned int read_ptr[EFX_MAX_CHANNELS];
  156. unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
  157. unsigned long timeout, wait;
  158. BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
  159. efx_for_each_channel(channel, efx) {
  160. read_ptr[channel->channel] = channel->eventq_read_ptr;
  161. set_bit(channel->channel, &dma_pend);
  162. set_bit(channel->channel, &int_pend);
  163. efx_nic_event_test_start(channel);
  164. }
  165. timeout = jiffies + IRQ_TIMEOUT;
  166. wait = 1;
  167. /* Wait for arrival of interrupts. NAPI processing may or may
  168. * not complete in time, but we can cope in any case.
  169. */
  170. do {
  171. schedule_timeout_uninterruptible(wait);
  172. efx_for_each_channel(channel, efx) {
  173. napi_disable(&channel->napi_str);
  174. if (channel->eventq_read_ptr !=
  175. read_ptr[channel->channel]) {
  176. set_bit(channel->channel, &napi_ran);
  177. clear_bit(channel->channel, &dma_pend);
  178. clear_bit(channel->channel, &int_pend);
  179. } else {
  180. if (efx_nic_event_present(channel))
  181. clear_bit(channel->channel, &dma_pend);
  182. if (efx_nic_event_test_irq_cpu(channel) >= 0)
  183. clear_bit(channel->channel, &int_pend);
  184. }
  185. napi_enable(&channel->napi_str);
  186. efx_nic_eventq_read_ack(channel);
  187. }
  188. wait *= 2;
  189. } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
  190. efx_for_each_channel(channel, efx) {
  191. bool dma_seen = !test_bit(channel->channel, &dma_pend);
  192. bool int_seen = !test_bit(channel->channel, &int_pend);
  193. tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
  194. tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
  195. if (dma_seen && int_seen) {
  196. netif_dbg(efx, drv, efx->net_dev,
  197. "channel %d event queue passed (with%s NAPI)\n",
  198. channel->channel,
  199. test_bit(channel->channel, &napi_ran) ?
  200. "" : "out");
  201. } else {
  202. /* Report failure and whether either interrupt or DMA
  203. * worked
  204. */
  205. netif_err(efx, drv, efx->net_dev,
  206. "channel %d timed out waiting for event queue\n",
  207. channel->channel);
  208. if (int_seen)
  209. netif_err(efx, drv, efx->net_dev,
  210. "channel %d saw interrupt "
  211. "during event queue test\n",
  212. channel->channel);
  213. if (dma_seen)
  214. netif_err(efx, drv, efx->net_dev,
  215. "channel %d event was generated, but "
  216. "failed to trigger an interrupt\n",
  217. channel->channel);
  218. }
  219. }
  220. return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
  221. }
  222. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  223. unsigned flags)
  224. {
  225. int rc;
  226. if (!efx->phy_op->run_tests)
  227. return 0;
  228. mutex_lock(&efx->mac_lock);
  229. rc = efx->phy_op->run_tests(efx, tests->phy_ext, flags);
  230. mutex_unlock(&efx->mac_lock);
  231. return rc;
  232. }
  233. /**************************************************************************
  234. *
  235. * Loopback testing
  236. * NB Only one loopback test can be executing concurrently.
  237. *
  238. **************************************************************************/
  239. /* Loopback test RX callback
  240. * This is called for each received packet during loopback testing.
  241. */
  242. void efx_loopback_rx_packet(struct efx_nic *efx,
  243. const char *buf_ptr, int pkt_len)
  244. {
  245. struct efx_loopback_state *state = efx->loopback_selftest;
  246. struct efx_loopback_payload *received;
  247. struct efx_loopback_payload *payload;
  248. BUG_ON(!buf_ptr);
  249. /* If we are just flushing, then drop the packet */
  250. if ((state == NULL) || state->flush)
  251. return;
  252. payload = &state->payload;
  253. received = (struct efx_loopback_payload *) buf_ptr;
  254. received->ip.saddr = payload->ip.saddr;
  255. if (state->offload_csum)
  256. received->ip.check = payload->ip.check;
  257. /* Check that header exists */
  258. if (pkt_len < sizeof(received->header)) {
  259. netif_err(efx, drv, efx->net_dev,
  260. "saw runt RX packet (length %d) in %s loopback "
  261. "test\n", pkt_len, LOOPBACK_MODE(efx));
  262. goto err;
  263. }
  264. /* Check that the ethernet header exists */
  265. if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) {
  266. netif_err(efx, drv, efx->net_dev,
  267. "saw non-loopback RX packet in %s loopback test\n",
  268. LOOPBACK_MODE(efx));
  269. goto err;
  270. }
  271. /* Check packet length */
  272. if (pkt_len != sizeof(*payload)) {
  273. netif_err(efx, drv, efx->net_dev,
  274. "saw incorrect RX packet length %d (wanted %d) in "
  275. "%s loopback test\n", pkt_len, (int)sizeof(*payload),
  276. LOOPBACK_MODE(efx));
  277. goto err;
  278. }
  279. /* Check that IP header matches */
  280. if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) {
  281. netif_err(efx, drv, efx->net_dev,
  282. "saw corrupted IP header in %s loopback test\n",
  283. LOOPBACK_MODE(efx));
  284. goto err;
  285. }
  286. /* Check that msg and padding matches */
  287. if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) {
  288. netif_err(efx, drv, efx->net_dev,
  289. "saw corrupted RX packet in %s loopback test\n",
  290. LOOPBACK_MODE(efx));
  291. goto err;
  292. }
  293. /* Check that iteration matches */
  294. if (received->iteration != payload->iteration) {
  295. netif_err(efx, drv, efx->net_dev,
  296. "saw RX packet from iteration %d (wanted %d) in "
  297. "%s loopback test\n", ntohs(received->iteration),
  298. ntohs(payload->iteration), LOOPBACK_MODE(efx));
  299. goto err;
  300. }
  301. /* Increase correct RX count */
  302. netif_vdbg(efx, drv, efx->net_dev,
  303. "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
  304. atomic_inc(&state->rx_good);
  305. return;
  306. err:
  307. #ifdef DEBUG
  308. if (atomic_read(&state->rx_bad) == 0) {
  309. netif_err(efx, drv, efx->net_dev, "received packet:\n");
  310. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  311. buf_ptr, pkt_len, 0);
  312. netif_err(efx, drv, efx->net_dev, "expected packet:\n");
  313. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
  314. &state->payload, sizeof(state->payload), 0);
  315. }
  316. #endif
  317. atomic_inc(&state->rx_bad);
  318. }
  319. /* Initialise an efx_selftest_state for a new iteration */
  320. static void efx_iterate_state(struct efx_nic *efx)
  321. {
  322. struct efx_loopback_state *state = efx->loopback_selftest;
  323. struct net_device *net_dev = efx->net_dev;
  324. struct efx_loopback_payload *payload = &state->payload;
  325. /* Initialise the layerII header */
  326. memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN);
  327. memcpy(&payload->header.h_source, &payload_source, ETH_ALEN);
  328. payload->header.h_proto = htons(ETH_P_IP);
  329. /* saddr set later and used as incrementing count */
  330. payload->ip.daddr = htonl(INADDR_LOOPBACK);
  331. payload->ip.ihl = 5;
  332. payload->ip.check = htons(0xdead);
  333. payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr));
  334. payload->ip.version = IPVERSION;
  335. payload->ip.protocol = IPPROTO_UDP;
  336. /* Initialise udp header */
  337. payload->udp.source = 0;
  338. payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) -
  339. sizeof(struct iphdr));
  340. payload->udp.check = 0; /* checksum ignored */
  341. /* Fill out payload */
  342. payload->iteration = htons(ntohs(payload->iteration) + 1);
  343. memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
  344. /* Fill out remaining state members */
  345. atomic_set(&state->rx_good, 0);
  346. atomic_set(&state->rx_bad, 0);
  347. smp_wmb();
  348. }
  349. static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
  350. {
  351. struct efx_nic *efx = tx_queue->efx;
  352. struct efx_loopback_state *state = efx->loopback_selftest;
  353. struct efx_loopback_payload *payload;
  354. struct sk_buff *skb;
  355. int i;
  356. netdev_tx_t rc;
  357. /* Transmit N copies of buffer */
  358. for (i = 0; i < state->packet_count; i++) {
  359. /* Allocate an skb, holding an extra reference for
  360. * transmit completion counting */
  361. skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
  362. if (!skb)
  363. return -ENOMEM;
  364. state->skbs[i] = skb;
  365. skb_get(skb);
  366. /* Copy the payload in, incrementing the source address to
  367. * exercise the rss vectors */
  368. payload = ((struct efx_loopback_payload *)
  369. skb_put(skb, sizeof(state->payload)));
  370. memcpy(payload, &state->payload, sizeof(state->payload));
  371. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  372. /* Ensure everything we've written is visible to the
  373. * interrupt handler. */
  374. smp_wmb();
  375. netif_tx_lock_bh(efx->net_dev);
  376. rc = efx_enqueue_skb(tx_queue, skb);
  377. netif_tx_unlock_bh(efx->net_dev);
  378. if (rc != NETDEV_TX_OK) {
  379. netif_err(efx, drv, efx->net_dev,
  380. "TX queue %d could not transmit packet %d of "
  381. "%d in %s loopback test\n", tx_queue->queue,
  382. i + 1, state->packet_count,
  383. LOOPBACK_MODE(efx));
  384. /* Defer cleaning up the other skbs for the caller */
  385. kfree_skb(skb);
  386. return -EPIPE;
  387. }
  388. }
  389. return 0;
  390. }
  391. static int efx_poll_loopback(struct efx_nic *efx)
  392. {
  393. struct efx_loopback_state *state = efx->loopback_selftest;
  394. struct efx_channel *channel;
  395. /* NAPI polling is not enabled, so process channels
  396. * synchronously */
  397. efx_for_each_channel(channel, efx) {
  398. if (channel->work_pending)
  399. efx_process_channel_now(channel);
  400. }
  401. return atomic_read(&state->rx_good) == state->packet_count;
  402. }
  403. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  404. struct efx_loopback_self_tests *lb_tests)
  405. {
  406. struct efx_nic *efx = tx_queue->efx;
  407. struct efx_loopback_state *state = efx->loopback_selftest;
  408. struct sk_buff *skb;
  409. int tx_done = 0, rx_good, rx_bad;
  410. int i, rc = 0;
  411. netif_tx_lock_bh(efx->net_dev);
  412. /* Count the number of tx completions, and decrement the refcnt. Any
  413. * skbs not already completed will be free'd when the queue is flushed */
  414. for (i = 0; i < state->packet_count; i++) {
  415. skb = state->skbs[i];
  416. if (skb && !skb_shared(skb))
  417. ++tx_done;
  418. dev_kfree_skb_any(skb);
  419. }
  420. netif_tx_unlock_bh(efx->net_dev);
  421. /* Check TX completion and received packet counts */
  422. rx_good = atomic_read(&state->rx_good);
  423. rx_bad = atomic_read(&state->rx_bad);
  424. if (tx_done != state->packet_count) {
  425. /* Don't free the skbs; they will be picked up on TX
  426. * overflow or channel teardown.
  427. */
  428. netif_err(efx, drv, efx->net_dev,
  429. "TX queue %d saw only %d out of an expected %d "
  430. "TX completion events in %s loopback test\n",
  431. tx_queue->queue, tx_done, state->packet_count,
  432. LOOPBACK_MODE(efx));
  433. rc = -ETIMEDOUT;
  434. /* Allow to fall through so we see the RX errors as well */
  435. }
  436. /* We may always be up to a flush away from our desired packet total */
  437. if (rx_good != state->packet_count) {
  438. netif_dbg(efx, drv, efx->net_dev,
  439. "TX queue %d saw only %d out of an expected %d "
  440. "received packets in %s loopback test\n",
  441. tx_queue->queue, rx_good, state->packet_count,
  442. LOOPBACK_MODE(efx));
  443. rc = -ETIMEDOUT;
  444. /* Fall through */
  445. }
  446. /* Update loopback test structure */
  447. lb_tests->tx_sent[tx_queue->queue] += state->packet_count;
  448. lb_tests->tx_done[tx_queue->queue] += tx_done;
  449. lb_tests->rx_good += rx_good;
  450. lb_tests->rx_bad += rx_bad;
  451. return rc;
  452. }
  453. static int
  454. efx_test_loopback(struct efx_tx_queue *tx_queue,
  455. struct efx_loopback_self_tests *lb_tests)
  456. {
  457. struct efx_nic *efx = tx_queue->efx;
  458. struct efx_loopback_state *state = efx->loopback_selftest;
  459. int i, begin_rc, end_rc;
  460. for (i = 0; i < 3; i++) {
  461. /* Determine how many packets to send */
  462. state->packet_count = efx->txq_entries / 3;
  463. state->packet_count = min(1 << (i << 2), state->packet_count);
  464. state->skbs = kcalloc(state->packet_count,
  465. sizeof(state->skbs[0]), GFP_KERNEL);
  466. if (!state->skbs)
  467. return -ENOMEM;
  468. state->flush = false;
  469. netif_dbg(efx, drv, efx->net_dev,
  470. "TX queue %d testing %s loopback with %d packets\n",
  471. tx_queue->queue, LOOPBACK_MODE(efx),
  472. state->packet_count);
  473. efx_iterate_state(efx);
  474. begin_rc = efx_begin_loopback(tx_queue);
  475. /* This will normally complete very quickly, but be
  476. * prepared to wait much longer. */
  477. msleep(1);
  478. if (!efx_poll_loopback(efx)) {
  479. msleep(LOOPBACK_TIMEOUT_MS);
  480. efx_poll_loopback(efx);
  481. }
  482. end_rc = efx_end_loopback(tx_queue, lb_tests);
  483. kfree(state->skbs);
  484. if (begin_rc || end_rc) {
  485. /* Wait a while to ensure there are no packets
  486. * floating around after a failure. */
  487. schedule_timeout_uninterruptible(HZ / 10);
  488. return begin_rc ? begin_rc : end_rc;
  489. }
  490. }
  491. netif_dbg(efx, drv, efx->net_dev,
  492. "TX queue %d passed %s loopback test with a burst length "
  493. "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx),
  494. state->packet_count);
  495. return 0;
  496. }
  497. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  498. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  499. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  500. * for link up and any faults to dissipate. */
  501. static int efx_wait_for_link(struct efx_nic *efx)
  502. {
  503. struct efx_link_state *link_state = &efx->link_state;
  504. int count, link_up_count = 0;
  505. bool link_up;
  506. for (count = 0; count < 40; count++) {
  507. schedule_timeout_uninterruptible(HZ / 10);
  508. if (efx->type->monitor != NULL) {
  509. mutex_lock(&efx->mac_lock);
  510. efx->type->monitor(efx);
  511. mutex_unlock(&efx->mac_lock);
  512. } else {
  513. struct efx_channel *channel = efx_get_channel(efx, 0);
  514. if (channel->work_pending)
  515. efx_process_channel_now(channel);
  516. }
  517. mutex_lock(&efx->mac_lock);
  518. link_up = link_state->up;
  519. if (link_up)
  520. link_up = !efx->type->check_mac_fault(efx);
  521. mutex_unlock(&efx->mac_lock);
  522. if (link_up) {
  523. if (++link_up_count == 2)
  524. return 0;
  525. } else {
  526. link_up_count = 0;
  527. }
  528. }
  529. return -ETIMEDOUT;
  530. }
  531. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  532. unsigned int loopback_modes)
  533. {
  534. enum efx_loopback_mode mode;
  535. struct efx_loopback_state *state;
  536. struct efx_channel *channel = efx_get_channel(efx, 0);
  537. struct efx_tx_queue *tx_queue;
  538. int rc = 0;
  539. /* Set the port loopback_selftest member. From this point on
  540. * all received packets will be dropped. Mark the state as
  541. * "flushing" so all inflight packets are dropped */
  542. state = kzalloc(sizeof(*state), GFP_KERNEL);
  543. if (state == NULL)
  544. return -ENOMEM;
  545. BUG_ON(efx->loopback_selftest);
  546. state->flush = true;
  547. efx->loopback_selftest = state;
  548. /* Test all supported loopback modes */
  549. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  550. if (!(loopback_modes & (1 << mode)))
  551. continue;
  552. /* Move the port into the specified loopback mode. */
  553. state->flush = true;
  554. mutex_lock(&efx->mac_lock);
  555. efx->loopback_mode = mode;
  556. rc = __efx_reconfigure_port(efx);
  557. mutex_unlock(&efx->mac_lock);
  558. if (rc) {
  559. netif_err(efx, drv, efx->net_dev,
  560. "unable to move into %s loopback\n",
  561. LOOPBACK_MODE(efx));
  562. goto out;
  563. }
  564. rc = efx_wait_for_link(efx);
  565. if (rc) {
  566. netif_err(efx, drv, efx->net_dev,
  567. "loopback %s never came up\n",
  568. LOOPBACK_MODE(efx));
  569. goto out;
  570. }
  571. /* Test all enabled types of TX queue */
  572. efx_for_each_channel_tx_queue(tx_queue, channel) {
  573. state->offload_csum = (tx_queue->queue &
  574. EFX_TXQ_TYPE_OFFLOAD);
  575. rc = efx_test_loopback(tx_queue,
  576. &tests->loopback[mode]);
  577. if (rc)
  578. goto out;
  579. }
  580. }
  581. out:
  582. /* Remove the flush. The caller will remove the loopback setting */
  583. state->flush = true;
  584. efx->loopback_selftest = NULL;
  585. wmb();
  586. kfree(state);
  587. return rc;
  588. }
  589. /**************************************************************************
  590. *
  591. * Entry point
  592. *
  593. *************************************************************************/
  594. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  595. unsigned flags)
  596. {
  597. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  598. int phy_mode = efx->phy_mode;
  599. enum reset_type reset_method = RESET_TYPE_INVISIBLE;
  600. int rc_test = 0, rc_reset = 0, rc;
  601. efx_selftest_async_cancel(efx);
  602. /* Online (i.e. non-disruptive) testing
  603. * This checks interrupt generation, event delivery and PHY presence. */
  604. rc = efx_test_phy_alive(efx, tests);
  605. if (rc && !rc_test)
  606. rc_test = rc;
  607. rc = efx_test_nvram(efx, tests);
  608. if (rc && !rc_test)
  609. rc_test = rc;
  610. rc = efx_test_interrupts(efx, tests);
  611. if (rc && !rc_test)
  612. rc_test = rc;
  613. rc = efx_test_eventq_irq(efx, tests);
  614. if (rc && !rc_test)
  615. rc_test = rc;
  616. if (rc_test)
  617. return rc_test;
  618. if (!(flags & ETH_TEST_FL_OFFLINE))
  619. return efx_test_phy(efx, tests, flags);
  620. /* Offline (i.e. disruptive) testing
  621. * This checks MAC and PHY loopback on the specified port. */
  622. /* Detach the device so the kernel doesn't transmit during the
  623. * loopback test and the watchdog timeout doesn't fire.
  624. */
  625. netif_device_detach(efx->net_dev);
  626. mutex_lock(&efx->mac_lock);
  627. if (efx->loopback_modes) {
  628. /* We need the 312 clock from the PHY to test the XMAC
  629. * registers, so move into XGMII loopback if available */
  630. if (efx->loopback_modes & (1 << LOOPBACK_XGMII))
  631. efx->loopback_mode = LOOPBACK_XGMII;
  632. else
  633. efx->loopback_mode = __ffs(efx->loopback_modes);
  634. }
  635. __efx_reconfigure_port(efx);
  636. mutex_unlock(&efx->mac_lock);
  637. /* free up all consumers of SRAM (including all the queues) */
  638. efx_reset_down(efx, reset_method);
  639. rc = efx_test_chip(efx, tests);
  640. if (rc && !rc_test)
  641. rc_test = rc;
  642. /* reset the chip to recover from the register test */
  643. rc_reset = efx->type->reset(efx, reset_method);
  644. /* Ensure that the phy is powered and out of loopback
  645. * for the bist and loopback tests */
  646. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  647. efx->loopback_mode = LOOPBACK_NONE;
  648. rc = efx_reset_up(efx, reset_method, rc_reset == 0);
  649. if (rc && !rc_reset)
  650. rc_reset = rc;
  651. if (rc_reset) {
  652. netif_err(efx, drv, efx->net_dev,
  653. "Unable to recover from chip test\n");
  654. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  655. return rc_reset;
  656. }
  657. rc = efx_test_phy(efx, tests, flags);
  658. if (rc && !rc_test)
  659. rc_test = rc;
  660. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  661. if (rc && !rc_test)
  662. rc_test = rc;
  663. /* restore the PHY to the previous state */
  664. mutex_lock(&efx->mac_lock);
  665. efx->phy_mode = phy_mode;
  666. efx->loopback_mode = loopback_mode;
  667. __efx_reconfigure_port(efx);
  668. mutex_unlock(&efx->mac_lock);
  669. netif_device_attach(efx->net_dev);
  670. return rc_test;
  671. }
  672. void efx_selftest_async_start(struct efx_nic *efx)
  673. {
  674. struct efx_channel *channel;
  675. efx_for_each_channel(channel, efx)
  676. efx_nic_event_test_start(channel);
  677. schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
  678. }
  679. void efx_selftest_async_cancel(struct efx_nic *efx)
  680. {
  681. cancel_delayed_work_sync(&efx->selftest_work);
  682. }
  683. void efx_selftest_async_work(struct work_struct *data)
  684. {
  685. struct efx_nic *efx = container_of(data, struct efx_nic,
  686. selftest_work.work);
  687. struct efx_channel *channel;
  688. int cpu;
  689. efx_for_each_channel(channel, efx) {
  690. cpu = efx_nic_event_test_irq_cpu(channel);
  691. if (cpu < 0)
  692. netif_err(efx, ifup, efx->net_dev,
  693. "channel %d failed to trigger an interrupt\n",
  694. channel->channel);
  695. else
  696. netif_dbg(efx, ifup, efx->net_dev,
  697. "channel %d triggered interrupt on CPU %d\n",
  698. channel->channel, cpu);
  699. }
  700. }