interrupt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/interrupt.h>
  17. #include "wil6210.h"
  18. #include "trace.h"
  19. /**
  20. * Theory of operation:
  21. *
  22. * There is ISR pseudo-cause register,
  23. * dma_rgf->DMA_RGF.PSEUDO_CAUSE.PSEUDO_CAUSE
  24. * Its bits represents OR'ed bits from 3 real ISR registers:
  25. * TX, RX, and MISC.
  26. *
  27. * Registers may be configured to either "write 1 to clear" or
  28. * "clear on read" mode
  29. *
  30. * When handling interrupt, one have to mask/unmask interrupts for the
  31. * real ISR registers, or hardware may malfunction.
  32. *
  33. */
  34. #define WIL6210_IRQ_DISABLE (0xFFFFFFFFUL)
  35. #define WIL6210_IMC_RX BIT_DMA_EP_RX_ICR_RX_DONE
  36. #define WIL6210_IMC_TX (BIT_DMA_EP_TX_ICR_TX_DONE | \
  37. BIT_DMA_EP_TX_ICR_TX_DONE_N(0))
  38. #define WIL6210_IMC_MISC (ISR_MISC_FW_READY | \
  39. ISR_MISC_MBOX_EVT | \
  40. ISR_MISC_FW_ERROR)
  41. #define WIL6210_IRQ_PSEUDO_MASK (u32)(~(BIT_DMA_PSEUDO_CAUSE_RX | \
  42. BIT_DMA_PSEUDO_CAUSE_TX | \
  43. BIT_DMA_PSEUDO_CAUSE_MISC))
  44. #if defined(CONFIG_WIL6210_ISR_COR)
  45. /* configure to Clear-On-Read mode */
  46. #define WIL_ICR_ICC_VALUE (0xFFFFFFFFUL)
  47. static inline void wil_icr_clear(u32 x, void __iomem *addr)
  48. {
  49. }
  50. #else /* defined(CONFIG_WIL6210_ISR_COR) */
  51. /* configure to Write-1-to-Clear mode */
  52. #define WIL_ICR_ICC_VALUE (0UL)
  53. static inline void wil_icr_clear(u32 x, void __iomem *addr)
  54. {
  55. iowrite32(x, addr);
  56. }
  57. #endif /* defined(CONFIG_WIL6210_ISR_COR) */
  58. static inline u32 wil_ioread32_and_clear(void __iomem *addr)
  59. {
  60. u32 x = ioread32(addr);
  61. wil_icr_clear(x, addr);
  62. return x;
  63. }
  64. static void wil6210_mask_irq_tx(struct wil6210_priv *wil)
  65. {
  66. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  67. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  68. offsetof(struct RGF_ICR, IMS));
  69. }
  70. static void wil6210_mask_irq_rx(struct wil6210_priv *wil)
  71. {
  72. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  73. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  74. offsetof(struct RGF_ICR, IMS));
  75. }
  76. static void wil6210_mask_irq_misc(struct wil6210_priv *wil)
  77. {
  78. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  79. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  80. offsetof(struct RGF_ICR, IMS));
  81. }
  82. static void wil6210_mask_irq_pseudo(struct wil6210_priv *wil)
  83. {
  84. wil_dbg_irq(wil, "%s()\n", __func__);
  85. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  86. HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW));
  87. clear_bit(wil_status_irqen, &wil->status);
  88. }
  89. void wil6210_unmask_irq_tx(struct wil6210_priv *wil)
  90. {
  91. iowrite32(WIL6210_IMC_TX, wil->csr +
  92. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  93. offsetof(struct RGF_ICR, IMC));
  94. }
  95. void wil6210_unmask_irq_rx(struct wil6210_priv *wil)
  96. {
  97. iowrite32(WIL6210_IMC_RX, wil->csr +
  98. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  99. offsetof(struct RGF_ICR, IMC));
  100. }
  101. static void wil6210_unmask_irq_misc(struct wil6210_priv *wil)
  102. {
  103. iowrite32(WIL6210_IMC_MISC, wil->csr +
  104. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  105. offsetof(struct RGF_ICR, IMC));
  106. }
  107. static void wil6210_unmask_irq_pseudo(struct wil6210_priv *wil)
  108. {
  109. wil_dbg_irq(wil, "%s()\n", __func__);
  110. set_bit(wil_status_irqen, &wil->status);
  111. iowrite32(WIL6210_IRQ_PSEUDO_MASK, wil->csr +
  112. HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW));
  113. }
  114. void wil_mask_irq(struct wil6210_priv *wil)
  115. {
  116. wil_dbg_irq(wil, "%s()\n", __func__);
  117. wil6210_mask_irq_tx(wil);
  118. wil6210_mask_irq_rx(wil);
  119. wil6210_mask_irq_misc(wil);
  120. wil6210_mask_irq_pseudo(wil);
  121. }
  122. void wil_unmask_irq(struct wil6210_priv *wil)
  123. {
  124. wil_dbg_irq(wil, "%s()\n", __func__);
  125. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_RX_ICR) +
  126. offsetof(struct RGF_ICR, ICC));
  127. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_TX_ICR) +
  128. offsetof(struct RGF_ICR, ICC));
  129. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  130. offsetof(struct RGF_ICR, ICC));
  131. /* interrupt moderation parameters */
  132. wil_set_itr_trsh(wil);
  133. wil6210_unmask_irq_pseudo(wil);
  134. wil6210_unmask_irq_tx(wil);
  135. wil6210_unmask_irq_rx(wil);
  136. wil6210_unmask_irq_misc(wil);
  137. }
  138. static irqreturn_t wil6210_irq_rx(int irq, void *cookie)
  139. {
  140. struct wil6210_priv *wil = cookie;
  141. u32 isr = wil_ioread32_and_clear(wil->csr +
  142. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  143. offsetof(struct RGF_ICR, ICR));
  144. trace_wil6210_irq_rx(isr);
  145. wil_dbg_irq(wil, "ISR RX 0x%08x\n", isr);
  146. if (!isr) {
  147. wil_err(wil, "spurious IRQ: RX\n");
  148. return IRQ_NONE;
  149. }
  150. wil6210_mask_irq_rx(wil);
  151. if (isr & BIT_DMA_EP_RX_ICR_RX_DONE) {
  152. wil_dbg_irq(wil, "RX done\n");
  153. isr &= ~BIT_DMA_EP_RX_ICR_RX_DONE;
  154. if (test_bit(wil_status_reset_done, &wil->status)) {
  155. if (test_bit(wil_status_napi_en, &wil->status)) {
  156. wil_dbg_txrx(wil, "NAPI(Rx) schedule\n");
  157. napi_schedule(&wil->napi_rx);
  158. } else {
  159. wil_err(wil, "Got Rx interrupt while "
  160. "stopping interface\n");
  161. }
  162. } else {
  163. wil_err(wil, "Got Rx interrupt while in reset\n");
  164. }
  165. }
  166. if (isr)
  167. wil_err(wil, "un-handled RX ISR bits 0x%08x\n", isr);
  168. /* Rx IRQ will be enabled when NAPI processing finished */
  169. atomic_inc(&wil->isr_count_rx);
  170. return IRQ_HANDLED;
  171. }
  172. static irqreturn_t wil6210_irq_tx(int irq, void *cookie)
  173. {
  174. struct wil6210_priv *wil = cookie;
  175. u32 isr = wil_ioread32_and_clear(wil->csr +
  176. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  177. offsetof(struct RGF_ICR, ICR));
  178. trace_wil6210_irq_tx(isr);
  179. wil_dbg_irq(wil, "ISR TX 0x%08x\n", isr);
  180. if (!isr) {
  181. wil_err(wil, "spurious IRQ: TX\n");
  182. return IRQ_NONE;
  183. }
  184. wil6210_mask_irq_tx(wil);
  185. if (isr & BIT_DMA_EP_TX_ICR_TX_DONE) {
  186. wil_dbg_irq(wil, "TX done\n");
  187. isr &= ~BIT_DMA_EP_TX_ICR_TX_DONE;
  188. /* clear also all VRING interrupts */
  189. isr &= ~(BIT(25) - 1UL);
  190. if (test_bit(wil_status_reset_done, &wil->status)) {
  191. wil_dbg_txrx(wil, "NAPI(Tx) schedule\n");
  192. napi_schedule(&wil->napi_tx);
  193. } else {
  194. wil_err(wil, "Got Tx interrupt while in reset\n");
  195. }
  196. }
  197. if (isr)
  198. wil_err(wil, "un-handled TX ISR bits 0x%08x\n", isr);
  199. /* Tx IRQ will be enabled when NAPI processing finished */
  200. atomic_inc(&wil->isr_count_tx);
  201. return IRQ_HANDLED;
  202. }
  203. static void wil_notify_fw_error(struct wil6210_priv *wil)
  204. {
  205. struct device *dev = &wil_to_ndev(wil)->dev;
  206. char *envp[3] = {
  207. [0] = "SOURCE=wil6210",
  208. [1] = "EVENT=FW_ERROR",
  209. [2] = NULL,
  210. };
  211. wil_err(wil, "Notify about firmware error\n");
  212. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  213. }
  214. static void wil_cache_mbox_regs(struct wil6210_priv *wil)
  215. {
  216. /* make shadow copy of registers that should not change on run time */
  217. wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
  218. sizeof(struct wil6210_mbox_ctl));
  219. wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
  220. wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
  221. }
  222. static irqreturn_t wil6210_irq_misc(int irq, void *cookie)
  223. {
  224. struct wil6210_priv *wil = cookie;
  225. u32 isr = wil_ioread32_and_clear(wil->csr +
  226. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  227. offsetof(struct RGF_ICR, ICR));
  228. trace_wil6210_irq_misc(isr);
  229. wil_dbg_irq(wil, "ISR MISC 0x%08x\n", isr);
  230. if (!isr) {
  231. wil_err(wil, "spurious IRQ: MISC\n");
  232. return IRQ_NONE;
  233. }
  234. wil6210_mask_irq_misc(wil);
  235. if (isr & ISR_MISC_FW_ERROR) {
  236. wil_err(wil, "Firmware error detected\n");
  237. clear_bit(wil_status_fwready, &wil->status);
  238. /*
  239. * do not clear @isr here - we do 2-nd part in thread
  240. * there, user space get notified, and it should be done
  241. * in non-atomic context
  242. */
  243. }
  244. if (isr & ISR_MISC_FW_READY) {
  245. wil_dbg_irq(wil, "IRQ: FW ready\n");
  246. wil_cache_mbox_regs(wil);
  247. set_bit(wil_status_reset_done, &wil->status);
  248. /**
  249. * Actual FW ready indicated by the
  250. * WMI_FW_READY_EVENTID
  251. */
  252. isr &= ~ISR_MISC_FW_READY;
  253. }
  254. wil->isr_misc = isr;
  255. if (isr) {
  256. return IRQ_WAKE_THREAD;
  257. } else {
  258. wil6210_unmask_irq_misc(wil);
  259. return IRQ_HANDLED;
  260. }
  261. }
  262. static irqreturn_t wil6210_irq_misc_thread(int irq, void *cookie)
  263. {
  264. struct wil6210_priv *wil = cookie;
  265. u32 isr = wil->isr_misc;
  266. trace_wil6210_irq_misc_thread(isr);
  267. wil_dbg_irq(wil, "Thread ISR MISC 0x%08x\n", isr);
  268. if (isr & ISR_MISC_FW_ERROR) {
  269. wil_notify_fw_error(wil);
  270. isr &= ~ISR_MISC_FW_ERROR;
  271. wil_fw_error_recovery(wil);
  272. }
  273. if (isr & ISR_MISC_MBOX_EVT) {
  274. wil_dbg_irq(wil, "MBOX event\n");
  275. wmi_recv_cmd(wil);
  276. isr &= ~ISR_MISC_MBOX_EVT;
  277. }
  278. if (isr)
  279. wil_dbg_irq(wil, "un-handled MISC ISR bits 0x%08x\n", isr);
  280. wil->isr_misc = 0;
  281. wil6210_unmask_irq_misc(wil);
  282. return IRQ_HANDLED;
  283. }
  284. /**
  285. * thread IRQ handler
  286. */
  287. static irqreturn_t wil6210_thread_irq(int irq, void *cookie)
  288. {
  289. struct wil6210_priv *wil = cookie;
  290. wil_dbg_irq(wil, "Thread IRQ\n");
  291. /* Discover real IRQ cause */
  292. if (wil->isr_misc)
  293. wil6210_irq_misc_thread(irq, cookie);
  294. wil6210_unmask_irq_pseudo(wil);
  295. return IRQ_HANDLED;
  296. }
  297. /* DEBUG
  298. * There is subtle bug in hardware that causes IRQ to raise when it should be
  299. * masked. It is quite rare and hard to debug.
  300. *
  301. * Catch irq issue if it happens and print all I can.
  302. */
  303. static int wil6210_debug_irq_mask(struct wil6210_priv *wil, u32 pseudo_cause)
  304. {
  305. if (!test_bit(wil_status_irqen, &wil->status)) {
  306. u32 icm_rx = wil_ioread32_and_clear(wil->csr +
  307. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  308. offsetof(struct RGF_ICR, ICM));
  309. u32 icr_rx = wil_ioread32_and_clear(wil->csr +
  310. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  311. offsetof(struct RGF_ICR, ICR));
  312. u32 imv_rx = ioread32(wil->csr +
  313. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  314. offsetof(struct RGF_ICR, IMV));
  315. u32 icm_tx = wil_ioread32_and_clear(wil->csr +
  316. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  317. offsetof(struct RGF_ICR, ICM));
  318. u32 icr_tx = wil_ioread32_and_clear(wil->csr +
  319. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  320. offsetof(struct RGF_ICR, ICR));
  321. u32 imv_tx = ioread32(wil->csr +
  322. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  323. offsetof(struct RGF_ICR, IMV));
  324. u32 icm_misc = wil_ioread32_and_clear(wil->csr +
  325. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  326. offsetof(struct RGF_ICR, ICM));
  327. u32 icr_misc = wil_ioread32_and_clear(wil->csr +
  328. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  329. offsetof(struct RGF_ICR, ICR));
  330. u32 imv_misc = ioread32(wil->csr +
  331. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  332. offsetof(struct RGF_ICR, IMV));
  333. wil_err(wil, "IRQ when it should be masked: pseudo 0x%08x\n"
  334. "Rx icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
  335. "Tx icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
  336. "Misc icm:icr:imv 0x%08x 0x%08x 0x%08x\n",
  337. pseudo_cause,
  338. icm_rx, icr_rx, imv_rx,
  339. icm_tx, icr_tx, imv_tx,
  340. icm_misc, icr_misc, imv_misc);
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. static irqreturn_t wil6210_hardirq(int irq, void *cookie)
  346. {
  347. irqreturn_t rc = IRQ_HANDLED;
  348. struct wil6210_priv *wil = cookie;
  349. u32 pseudo_cause = ioread32(wil->csr + HOSTADDR(RGF_DMA_PSEUDO_CAUSE));
  350. /**
  351. * pseudo_cause is Clear-On-Read, no need to ACK
  352. */
  353. if ((pseudo_cause == 0) || ((pseudo_cause & 0xff) == 0xff))
  354. return IRQ_NONE;
  355. /* FIXME: IRQ mask debug */
  356. if (wil6210_debug_irq_mask(wil, pseudo_cause))
  357. return IRQ_NONE;
  358. trace_wil6210_irq_pseudo(pseudo_cause);
  359. wil_dbg_irq(wil, "Pseudo IRQ 0x%08x\n", pseudo_cause);
  360. wil6210_mask_irq_pseudo(wil);
  361. /* Discover real IRQ cause
  362. * There are 2 possible phases for every IRQ:
  363. * - hard IRQ handler called right here
  364. * - threaded handler called later
  365. *
  366. * Hard IRQ handler reads and clears ISR.
  367. *
  368. * If threaded handler requested, hard IRQ handler
  369. * returns IRQ_WAKE_THREAD and saves ISR register value
  370. * for the threaded handler use.
  371. *
  372. * voting for wake thread - need at least 1 vote
  373. */
  374. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_RX) &&
  375. (wil6210_irq_rx(irq, cookie) == IRQ_WAKE_THREAD))
  376. rc = IRQ_WAKE_THREAD;
  377. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_TX) &&
  378. (wil6210_irq_tx(irq, cookie) == IRQ_WAKE_THREAD))
  379. rc = IRQ_WAKE_THREAD;
  380. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_MISC) &&
  381. (wil6210_irq_misc(irq, cookie) == IRQ_WAKE_THREAD))
  382. rc = IRQ_WAKE_THREAD;
  383. /* if thread is requested, it will unmask IRQ */
  384. if (rc != IRQ_WAKE_THREAD)
  385. wil6210_unmask_irq_pseudo(wil);
  386. return rc;
  387. }
  388. static int wil6210_request_3msi(struct wil6210_priv *wil, int irq)
  389. {
  390. int rc;
  391. /*
  392. * IRQ's are in the following order:
  393. * - Tx
  394. * - Rx
  395. * - Misc
  396. */
  397. rc = request_irq(irq, wil6210_irq_tx, IRQF_SHARED,
  398. WIL_NAME"_tx", wil);
  399. if (rc)
  400. return rc;
  401. rc = request_irq(irq + 1, wil6210_irq_rx, IRQF_SHARED,
  402. WIL_NAME"_rx", wil);
  403. if (rc)
  404. goto free0;
  405. rc = request_threaded_irq(irq + 2, wil6210_irq_misc,
  406. wil6210_irq_misc_thread,
  407. IRQF_SHARED, WIL_NAME"_misc", wil);
  408. if (rc)
  409. goto free1;
  410. return 0;
  411. /* error branch */
  412. free1:
  413. free_irq(irq + 1, wil);
  414. free0:
  415. free_irq(irq, wil);
  416. return rc;
  417. }
  418. /* can't use wil_ioread32_and_clear because ICC value is not set yet */
  419. static inline void wil_clear32(void __iomem *addr)
  420. {
  421. u32 x = ioread32(addr);
  422. iowrite32(x, addr);
  423. }
  424. void wil6210_clear_irq(struct wil6210_priv *wil)
  425. {
  426. wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_RX_ICR) +
  427. offsetof(struct RGF_ICR, ICR));
  428. wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_TX_ICR) +
  429. offsetof(struct RGF_ICR, ICR));
  430. wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  431. offsetof(struct RGF_ICR, ICR));
  432. wmb(); /* make sure write completed */
  433. }
  434. int wil6210_init_irq(struct wil6210_priv *wil, int irq)
  435. {
  436. int rc;
  437. wil_dbg_misc(wil, "%s() n_msi=%d\n", __func__, wil->n_msi);
  438. if (wil->n_msi == 3)
  439. rc = wil6210_request_3msi(wil, irq);
  440. else
  441. rc = request_threaded_irq(irq, wil6210_hardirq,
  442. wil6210_thread_irq,
  443. wil->n_msi ? 0 : IRQF_SHARED,
  444. WIL_NAME, wil);
  445. return rc;
  446. }
  447. void wil6210_fini_irq(struct wil6210_priv *wil, int irq)
  448. {
  449. wil_dbg_misc(wil, "%s()\n", __func__);
  450. wil_mask_irq(wil);
  451. free_irq(irq, wil);
  452. if (wil->n_msi == 3) {
  453. free_irq(irq + 1, wil);
  454. free_irq(irq + 2, wil);
  455. }
  456. }