selftest.c 22 KB

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