selftest.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 "efx_common.h"
  21. #include "efx_channels.h"
  22. #include "nic.h"
  23. #include "mcdi_port_common.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. * struct 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. rc = efx_mcdi_phy_test_alive(efx);
  93. tests->phy_alive = rc ? -1 : 1;
  94. return rc;
  95. }
  96. static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
  97. {
  98. int rc = 0;
  99. if (efx->type->test_nvram) {
  100. rc = efx->type->test_nvram(efx);
  101. if (rc == -EPERM)
  102. rc = 0;
  103. else
  104. tests->nvram = rc ? -1 : 1;
  105. }
  106. return rc;
  107. }
  108. /**************************************************************************
  109. *
  110. * Interrupt and event queue testing
  111. *
  112. **************************************************************************/
  113. /* Test generation and receipt of interrupts */
  114. static int efx_test_interrupts(struct efx_nic *efx,
  115. struct efx_self_tests *tests)
  116. {
  117. unsigned long timeout, wait;
  118. int cpu;
  119. int rc;
  120. netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
  121. tests->interrupt = -1;
  122. rc = efx_nic_irq_test_start(efx);
  123. if (rc == -ENOTSUPP) {
  124. netif_dbg(efx, drv, efx->net_dev,
  125. "direct interrupt testing not supported\n");
  126. tests->interrupt = 0;
  127. return 0;
  128. }
  129. timeout = jiffies + IRQ_TIMEOUT;
  130. wait = 1;
  131. /* Wait for arrival of test interrupt. */
  132. netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
  133. do {
  134. schedule_timeout_uninterruptible(wait);
  135. cpu = efx_nic_irq_test_irq_cpu(efx);
  136. if (cpu >= 0)
  137. goto success;
  138. wait *= 2;
  139. } while (time_before(jiffies, timeout));
  140. netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
  141. return -ETIMEDOUT;
  142. success:
  143. netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
  144. INT_MODE(efx), cpu);
  145. tests->interrupt = 1;
  146. return 0;
  147. }
  148. /* Test generation and receipt of interrupting events */
  149. static int efx_test_eventq_irq(struct efx_nic *efx,
  150. struct efx_self_tests *tests)
  151. {
  152. struct efx_channel *channel;
  153. unsigned int read_ptr[EFX_MAX_CHANNELS];
  154. unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
  155. unsigned long timeout, wait;
  156. BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
  157. efx_for_each_channel(channel, efx) {
  158. read_ptr[channel->channel] = channel->eventq_read_ptr;
  159. set_bit(channel->channel, &dma_pend);
  160. set_bit(channel->channel, &int_pend);
  161. efx_nic_event_test_start(channel);
  162. }
  163. timeout = jiffies + IRQ_TIMEOUT;
  164. wait = 1;
  165. /* Wait for arrival of interrupts. NAPI processing may or may
  166. * not complete in time, but we can cope in any case.
  167. */
  168. do {
  169. schedule_timeout_uninterruptible(wait);
  170. efx_for_each_channel(channel, efx) {
  171. efx_stop_eventq(channel);
  172. if (channel->eventq_read_ptr !=
  173. read_ptr[channel->channel]) {
  174. set_bit(channel->channel, &napi_ran);
  175. clear_bit(channel->channel, &dma_pend);
  176. clear_bit(channel->channel, &int_pend);
  177. } else {
  178. if (efx_nic_event_present(channel))
  179. clear_bit(channel->channel, &dma_pend);
  180. if (efx_nic_event_test_irq_cpu(channel) >= 0)
  181. clear_bit(channel->channel, &int_pend);
  182. }
  183. efx_start_eventq(channel);
  184. }
  185. wait *= 2;
  186. } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
  187. efx_for_each_channel(channel, efx) {
  188. bool dma_seen = !test_bit(channel->channel, &dma_pend);
  189. bool int_seen = !test_bit(channel->channel, &int_pend);
  190. tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
  191. tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
  192. if (dma_seen && int_seen) {
  193. netif_dbg(efx, drv, efx->net_dev,
  194. "channel %d event queue passed (with%s NAPI)\n",
  195. channel->channel,
  196. test_bit(channel->channel, &napi_ran) ?
  197. "" : "out");
  198. } else {
  199. /* Report failure and whether either interrupt or DMA
  200. * worked
  201. */
  202. netif_err(efx, drv, efx->net_dev,
  203. "channel %d timed out waiting for event queue\n",
  204. channel->channel);
  205. if (int_seen)
  206. netif_err(efx, drv, efx->net_dev,
  207. "channel %d saw interrupt "
  208. "during event queue test\n",
  209. channel->channel);
  210. if (dma_seen)
  211. netif_err(efx, drv, efx->net_dev,
  212. "channel %d event was generated, but "
  213. "failed to trigger an interrupt\n",
  214. channel->channel);
  215. }
  216. }
  217. return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
  218. }
  219. static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
  220. unsigned flags)
  221. {
  222. int rc;
  223. mutex_lock(&efx->mac_lock);
  224. rc = efx_mcdi_phy_run_tests(efx, tests->phy_ext, flags);
  225. mutex_unlock(&efx->mac_lock);
  226. if (rc == -EPERM)
  227. rc = 0;
  228. else
  229. netif_info(efx, drv, efx->net_dev,
  230. "%s phy selftest\n", rc ? "Failed" : "Passed");
  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. ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
  327. ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
  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 = (__force __sum16) 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 = skb_put(skb, sizeof(state->payload));
  369. memcpy(payload, &state->payload, sizeof(state->payload));
  370. payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
  371. /* Ensure everything we've written is visible to the
  372. * interrupt handler. */
  373. smp_wmb();
  374. netif_tx_lock_bh(efx->net_dev);
  375. rc = efx_enqueue_skb(tx_queue, skb);
  376. netif_tx_unlock_bh(efx->net_dev);
  377. if (rc != NETDEV_TX_OK) {
  378. netif_err(efx, drv, efx->net_dev,
  379. "TX queue %d could not transmit packet %d of "
  380. "%d in %s loopback test\n", tx_queue->label,
  381. i + 1, state->packet_count,
  382. LOOPBACK_MODE(efx));
  383. /* Defer cleaning up the other skbs for the caller */
  384. kfree_skb(skb);
  385. return -EPIPE;
  386. }
  387. }
  388. return 0;
  389. }
  390. static int efx_poll_loopback(struct efx_nic *efx)
  391. {
  392. struct efx_loopback_state *state = efx->loopback_selftest;
  393. return atomic_read(&state->rx_good) == state->packet_count;
  394. }
  395. static int efx_end_loopback(struct efx_tx_queue *tx_queue,
  396. struct efx_loopback_self_tests *lb_tests)
  397. {
  398. struct efx_nic *efx = tx_queue->efx;
  399. struct efx_loopback_state *state = efx->loopback_selftest;
  400. struct sk_buff *skb;
  401. int tx_done = 0, rx_good, rx_bad;
  402. int i, rc = 0;
  403. netif_tx_lock_bh(efx->net_dev);
  404. /* Count the number of tx completions, and decrement the refcnt. Any
  405. * skbs not already completed will be free'd when the queue is flushed */
  406. for (i = 0; i < state->packet_count; i++) {
  407. skb = state->skbs[i];
  408. if (skb && !skb_shared(skb))
  409. ++tx_done;
  410. dev_kfree_skb(skb);
  411. }
  412. netif_tx_unlock_bh(efx->net_dev);
  413. /* Check TX completion and received packet counts */
  414. rx_good = atomic_read(&state->rx_good);
  415. rx_bad = atomic_read(&state->rx_bad);
  416. if (tx_done != state->packet_count) {
  417. /* Don't free the skbs; they will be picked up on TX
  418. * overflow or channel teardown.
  419. */
  420. netif_err(efx, drv, efx->net_dev,
  421. "TX queue %d saw only %d out of an expected %d "
  422. "TX completion events in %s loopback test\n",
  423. tx_queue->label, tx_done, state->packet_count,
  424. LOOPBACK_MODE(efx));
  425. rc = -ETIMEDOUT;
  426. /* Allow to fall through so we see the RX errors as well */
  427. }
  428. /* We may always be up to a flush away from our desired packet total */
  429. if (rx_good != state->packet_count) {
  430. netif_dbg(efx, drv, efx->net_dev,
  431. "TX queue %d saw only %d out of an expected %d "
  432. "received packets in %s loopback test\n",
  433. tx_queue->label, rx_good, state->packet_count,
  434. LOOPBACK_MODE(efx));
  435. rc = -ETIMEDOUT;
  436. /* Fall through */
  437. }
  438. /* Update loopback test structure */
  439. lb_tests->tx_sent[tx_queue->label] += state->packet_count;
  440. lb_tests->tx_done[tx_queue->label] += tx_done;
  441. lb_tests->rx_good += rx_good;
  442. lb_tests->rx_bad += rx_bad;
  443. return rc;
  444. }
  445. static int
  446. efx_test_loopback(struct efx_tx_queue *tx_queue,
  447. struct efx_loopback_self_tests *lb_tests)
  448. {
  449. struct efx_nic *efx = tx_queue->efx;
  450. struct efx_loopback_state *state = efx->loopback_selftest;
  451. int i, begin_rc, end_rc;
  452. for (i = 0; i < 3; i++) {
  453. /* Determine how many packets to send */
  454. state->packet_count = efx->txq_entries / 3;
  455. state->packet_count = min(1 << (i << 2), state->packet_count);
  456. state->skbs = kcalloc(state->packet_count,
  457. sizeof(state->skbs[0]), GFP_KERNEL);
  458. if (!state->skbs)
  459. return -ENOMEM;
  460. state->flush = false;
  461. netif_dbg(efx, drv, efx->net_dev,
  462. "TX queue %d (hw %d) testing %s loopback with %d packets\n",
  463. tx_queue->label, tx_queue->queue, LOOPBACK_MODE(efx),
  464. state->packet_count);
  465. efx_iterate_state(efx);
  466. begin_rc = efx_begin_loopback(tx_queue);
  467. /* This will normally complete very quickly, but be
  468. * prepared to wait much longer. */
  469. msleep(1);
  470. if (!efx_poll_loopback(efx)) {
  471. msleep(LOOPBACK_TIMEOUT_MS);
  472. efx_poll_loopback(efx);
  473. }
  474. end_rc = efx_end_loopback(tx_queue, lb_tests);
  475. kfree(state->skbs);
  476. if (begin_rc || end_rc) {
  477. /* Wait a while to ensure there are no packets
  478. * floating around after a failure. */
  479. schedule_timeout_uninterruptible(HZ / 10);
  480. return begin_rc ? begin_rc : end_rc;
  481. }
  482. }
  483. netif_dbg(efx, drv, efx->net_dev,
  484. "TX queue %d passed %s loopback test with a burst length "
  485. "of %d packets\n", tx_queue->label, LOOPBACK_MODE(efx),
  486. state->packet_count);
  487. return 0;
  488. }
  489. /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
  490. * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
  491. * to delay and retry. Therefore, it's safer to just poll directly. Wait
  492. * for link up and any faults to dissipate. */
  493. static int efx_wait_for_link(struct efx_nic *efx)
  494. {
  495. struct efx_link_state *link_state = &efx->link_state;
  496. int count, link_up_count = 0;
  497. bool link_up;
  498. for (count = 0; count < 40; count++) {
  499. schedule_timeout_uninterruptible(HZ / 10);
  500. if (efx->type->monitor != NULL) {
  501. mutex_lock(&efx->mac_lock);
  502. efx->type->monitor(efx);
  503. mutex_unlock(&efx->mac_lock);
  504. }
  505. mutex_lock(&efx->mac_lock);
  506. link_up = link_state->up;
  507. if (link_up)
  508. link_up = !efx->type->check_mac_fault(efx);
  509. mutex_unlock(&efx->mac_lock);
  510. if (link_up) {
  511. if (++link_up_count == 2)
  512. return 0;
  513. } else {
  514. link_up_count = 0;
  515. }
  516. }
  517. return -ETIMEDOUT;
  518. }
  519. static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
  520. unsigned int loopback_modes)
  521. {
  522. enum efx_loopback_mode mode;
  523. struct efx_loopback_state *state;
  524. struct efx_channel *channel =
  525. efx_get_channel(efx, efx->tx_channel_offset);
  526. struct efx_tx_queue *tx_queue;
  527. int rc = 0;
  528. /* Set the port loopback_selftest member. From this point on
  529. * all received packets will be dropped. Mark the state as
  530. * "flushing" so all inflight packets are dropped */
  531. state = kzalloc(sizeof(*state), GFP_KERNEL);
  532. if (state == NULL)
  533. return -ENOMEM;
  534. BUG_ON(efx->loopback_selftest);
  535. state->flush = true;
  536. efx->loopback_selftest = state;
  537. /* Test all supported loopback modes */
  538. for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
  539. if (!(loopback_modes & (1 << mode)))
  540. continue;
  541. /* Move the port into the specified loopback mode. */
  542. state->flush = true;
  543. mutex_lock(&efx->mac_lock);
  544. efx->loopback_mode = mode;
  545. rc = __efx_reconfigure_port(efx);
  546. mutex_unlock(&efx->mac_lock);
  547. if (rc) {
  548. netif_err(efx, drv, efx->net_dev,
  549. "unable to move into %s loopback\n",
  550. LOOPBACK_MODE(efx));
  551. goto out;
  552. }
  553. rc = efx_wait_for_link(efx);
  554. if (rc) {
  555. netif_err(efx, drv, efx->net_dev,
  556. "loopback %s never came up\n",
  557. LOOPBACK_MODE(efx));
  558. goto out;
  559. }
  560. /* Test all enabled types of TX queue */
  561. efx_for_each_channel_tx_queue(tx_queue, channel) {
  562. state->offload_csum = (tx_queue->type &
  563. EFX_TXQ_TYPE_OUTER_CSUM);
  564. rc = efx_test_loopback(tx_queue,
  565. &tests->loopback[mode]);
  566. if (rc)
  567. goto out;
  568. }
  569. }
  570. out:
  571. /* Remove the flush. The caller will remove the loopback setting */
  572. state->flush = true;
  573. efx->loopback_selftest = NULL;
  574. wmb();
  575. kfree(state);
  576. if (rc == -EPERM)
  577. rc = 0;
  578. return rc;
  579. }
  580. /**************************************************************************
  581. *
  582. * Entry point
  583. *
  584. *************************************************************************/
  585. int efx_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
  586. unsigned flags)
  587. {
  588. enum efx_loopback_mode loopback_mode = efx->loopback_mode;
  589. int phy_mode = efx->phy_mode;
  590. int rc_test = 0, rc_reset, rc;
  591. efx_selftest_async_cancel(efx);
  592. /* Online (i.e. non-disruptive) testing
  593. * This checks interrupt generation, event delivery and PHY presence. */
  594. rc = efx_test_phy_alive(efx, tests);
  595. if (rc && !rc_test)
  596. rc_test = rc;
  597. rc = efx_test_nvram(efx, tests);
  598. if (rc && !rc_test)
  599. rc_test = rc;
  600. rc = efx_test_interrupts(efx, tests);
  601. if (rc && !rc_test)
  602. rc_test = rc;
  603. rc = efx_test_eventq_irq(efx, tests);
  604. if (rc && !rc_test)
  605. rc_test = rc;
  606. if (rc_test)
  607. return rc_test;
  608. if (!(flags & ETH_TEST_FL_OFFLINE))
  609. return efx_test_phy(efx, tests, flags);
  610. /* Offline (i.e. disruptive) testing
  611. * This checks MAC and PHY loopback on the specified port. */
  612. /* Detach the device so the kernel doesn't transmit during the
  613. * loopback test and the watchdog timeout doesn't fire.
  614. */
  615. efx_device_detach_sync(efx);
  616. if (efx->type->test_chip) {
  617. rc_reset = efx->type->test_chip(efx, tests);
  618. if (rc_reset) {
  619. netif_err(efx, hw, efx->net_dev,
  620. "Unable to recover from chip test\n");
  621. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  622. return rc_reset;
  623. }
  624. if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
  625. rc_test = -EIO;
  626. }
  627. /* Ensure that the phy is powered and out of loopback
  628. * for the bist and loopback tests */
  629. mutex_lock(&efx->mac_lock);
  630. efx->phy_mode &= ~PHY_MODE_LOW_POWER;
  631. efx->loopback_mode = LOOPBACK_NONE;
  632. __efx_reconfigure_port(efx);
  633. mutex_unlock(&efx->mac_lock);
  634. rc = efx_test_phy(efx, tests, flags);
  635. if (rc && !rc_test)
  636. rc_test = rc;
  637. rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
  638. if (rc && !rc_test)
  639. rc_test = rc;
  640. /* restore the PHY to the previous state */
  641. mutex_lock(&efx->mac_lock);
  642. efx->phy_mode = phy_mode;
  643. efx->loopback_mode = loopback_mode;
  644. __efx_reconfigure_port(efx);
  645. mutex_unlock(&efx->mac_lock);
  646. efx_device_attach_if_not_resetting(efx);
  647. return rc_test;
  648. }
  649. void efx_selftest_async_start(struct efx_nic *efx)
  650. {
  651. struct efx_channel *channel;
  652. efx_for_each_channel(channel, efx)
  653. efx_nic_event_test_start(channel);
  654. schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
  655. }
  656. void efx_selftest_async_cancel(struct efx_nic *efx)
  657. {
  658. cancel_delayed_work_sync(&efx->selftest_work);
  659. }
  660. static void efx_selftest_async_work(struct work_struct *data)
  661. {
  662. struct efx_nic *efx = container_of(data, struct efx_nic,
  663. selftest_work.work);
  664. struct efx_channel *channel;
  665. int cpu;
  666. efx_for_each_channel(channel, efx) {
  667. cpu = efx_nic_event_test_irq_cpu(channel);
  668. if (cpu < 0)
  669. netif_err(efx, ifup, efx->net_dev,
  670. "channel %d failed to trigger an interrupt\n",
  671. channel->channel);
  672. else
  673. netif_dbg(efx, ifup, efx->net_dev,
  674. "channel %d triggered interrupt on CPU %d\n",
  675. channel->channel, cpu);
  676. }
  677. }
  678. void efx_selftest_async_init(struct efx_nic *efx)
  679. {
  680. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  681. }