selftest.c 23 KB

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