selftest.c 22 KB

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