iwl-trans-pcie-rx.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/sched.h>
  30. #include <linux/wait.h>
  31. #include <linux/gfp.h>
  32. #include "iwl-prph.h"
  33. #include "iwl-io.h"
  34. #include "iwl-trans-pcie-int.h"
  35. #include "iwl-op-mode.h"
  36. #ifdef CONFIG_IWLWIFI_IDI
  37. #include "iwl-amfh.h"
  38. #endif
  39. /******************************************************************************
  40. *
  41. * RX path functions
  42. *
  43. ******************************************************************************/
  44. /*
  45. * Rx theory of operation
  46. *
  47. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  48. * each of which point to Receive Buffers to be filled by the NIC. These get
  49. * used not only for Rx frames, but for any command response or notification
  50. * from the NIC. The driver and NIC manage the Rx buffers by means
  51. * of indexes into the circular buffer.
  52. *
  53. * Rx Queue Indexes
  54. * The host/firmware share two index registers for managing the Rx buffers.
  55. *
  56. * The READ index maps to the first position that the firmware may be writing
  57. * to -- the driver can read up to (but not including) this position and get
  58. * good data.
  59. * The READ index is managed by the firmware once the card is enabled.
  60. *
  61. * The WRITE index maps to the last position the driver has read from -- the
  62. * position preceding WRITE is the last slot the firmware can place a packet.
  63. *
  64. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  65. * WRITE = READ.
  66. *
  67. * During initialization, the host sets up the READ queue position to the first
  68. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  69. *
  70. * When the firmware places a packet in a buffer, it will advance the READ index
  71. * and fire the RX interrupt. The driver can then query the READ index and
  72. * process as many packets as possible, moving the WRITE index forward as it
  73. * resets the Rx queue buffers with new memory.
  74. *
  75. * The management in the driver is as follows:
  76. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  77. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  78. * to replenish the iwl->rxq->rx_free.
  79. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  80. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  81. * 'processed' and 'read' driver indexes as well)
  82. * + A received packet is processed and handed to the kernel network stack,
  83. * detached from the iwl->rxq. The driver 'processed' index is updated.
  84. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  85. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  86. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  87. * were enough free buffers and RX_STALLED is set it is cleared.
  88. *
  89. *
  90. * Driver sequence:
  91. *
  92. * iwl_rx_queue_alloc() Allocates rx_free
  93. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  94. * iwl_rx_queue_restock
  95. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  96. * queue, updates firmware pointers, and updates
  97. * the WRITE index. If insufficient rx_free buffers
  98. * are available, schedules iwl_rx_replenish
  99. *
  100. * -- enable interrupts --
  101. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  102. * READ INDEX, detaching the SKB from the pool.
  103. * Moves the packet buffer from queue to rx_used.
  104. * Calls iwl_rx_queue_restock to refill any empty
  105. * slots.
  106. * ...
  107. *
  108. */
  109. /**
  110. * iwl_rx_queue_space - Return number of free slots available in queue.
  111. */
  112. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  113. {
  114. int s = q->read - q->write;
  115. if (s <= 0)
  116. s += RX_QUEUE_SIZE;
  117. /* keep some buffer to not confuse full and empty queue */
  118. s -= 2;
  119. if (s < 0)
  120. s = 0;
  121. return s;
  122. }
  123. /**
  124. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  125. */
  126. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  127. struct iwl_rx_queue *q)
  128. {
  129. unsigned long flags;
  130. u32 reg;
  131. spin_lock_irqsave(&q->lock, flags);
  132. if (q->need_update == 0)
  133. goto exit_unlock;
  134. if (trans->cfg->base_params->shadow_reg_enable) {
  135. /* shadow register enabled */
  136. /* Device expects a multiple of 8 */
  137. q->write_actual = (q->write & ~0x7);
  138. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
  139. } else {
  140. struct iwl_trans_pcie *trans_pcie =
  141. IWL_TRANS_GET_PCIE_TRANS(trans);
  142. /* If power-saving is in use, make sure device is awake */
  143. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
  144. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  145. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  146. IWL_DEBUG_INFO(trans,
  147. "Rx queue requesting wakeup,"
  148. " GP1 = 0x%x\n", reg);
  149. iwl_set_bit(trans, CSR_GP_CNTRL,
  150. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  151. goto exit_unlock;
  152. }
  153. q->write_actual = (q->write & ~0x7);
  154. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  155. q->write_actual);
  156. /* Else device is assumed to be awake */
  157. } else {
  158. /* Device expects a multiple of 8 */
  159. q->write_actual = (q->write & ~0x7);
  160. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  161. q->write_actual);
  162. }
  163. }
  164. q->need_update = 0;
  165. exit_unlock:
  166. spin_unlock_irqrestore(&q->lock, flags);
  167. }
  168. /**
  169. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  170. */
  171. static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  172. {
  173. return cpu_to_le32((u32)(dma_addr >> 8));
  174. }
  175. /**
  176. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  177. *
  178. * If there are slots in the RX queue that need to be restocked,
  179. * and we have free pre-allocated buffers, fill the ranks as much
  180. * as we can, pulling from rx_free.
  181. *
  182. * This moves the 'write' index forward to catch up with 'processed', and
  183. * also updates the memory address in the firmware to reference the new
  184. * target buffer.
  185. */
  186. static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
  187. {
  188. struct iwl_trans_pcie *trans_pcie =
  189. IWL_TRANS_GET_PCIE_TRANS(trans);
  190. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  191. struct list_head *element;
  192. struct iwl_rx_mem_buffer *rxb;
  193. unsigned long flags;
  194. spin_lock_irqsave(&rxq->lock, flags);
  195. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  196. /* The overwritten rxb must be a used one */
  197. rxb = rxq->queue[rxq->write];
  198. BUG_ON(rxb && rxb->page);
  199. /* Get next free Rx buffer, remove from free list */
  200. element = rxq->rx_free.next;
  201. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  202. list_del(element);
  203. /* Point to Rx buffer via next RBD in circular buffer */
  204. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
  205. rxq->queue[rxq->write] = rxb;
  206. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  207. rxq->free_count--;
  208. }
  209. spin_unlock_irqrestore(&rxq->lock, flags);
  210. /* If the pre-allocated buffer pool is dropping low, schedule to
  211. * refill it */
  212. if (rxq->free_count <= RX_LOW_WATERMARK)
  213. schedule_work(&trans_pcie->rx_replenish);
  214. /* If we've added more space for the firmware to place data, tell it.
  215. * Increment device's write pointer in multiples of 8. */
  216. if (rxq->write_actual != (rxq->write & ~0x7)) {
  217. spin_lock_irqsave(&rxq->lock, flags);
  218. rxq->need_update = 1;
  219. spin_unlock_irqrestore(&rxq->lock, flags);
  220. iwl_rx_queue_update_write_ptr(trans, rxq);
  221. }
  222. }
  223. /**
  224. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  225. *
  226. * When moving to rx_free an SKB is allocated for the slot.
  227. *
  228. * Also restock the Rx queue via iwl_rx_queue_restock.
  229. * This is called as a scheduled work item (except for during initialization)
  230. */
  231. static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
  232. {
  233. struct iwl_trans_pcie *trans_pcie =
  234. IWL_TRANS_GET_PCIE_TRANS(trans);
  235. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  236. struct list_head *element;
  237. struct iwl_rx_mem_buffer *rxb;
  238. struct page *page;
  239. unsigned long flags;
  240. gfp_t gfp_mask = priority;
  241. while (1) {
  242. spin_lock_irqsave(&rxq->lock, flags);
  243. if (list_empty(&rxq->rx_used)) {
  244. spin_unlock_irqrestore(&rxq->lock, flags);
  245. return;
  246. }
  247. spin_unlock_irqrestore(&rxq->lock, flags);
  248. if (rxq->free_count > RX_LOW_WATERMARK)
  249. gfp_mask |= __GFP_NOWARN;
  250. if (trans_pcie->rx_page_order > 0)
  251. gfp_mask |= __GFP_COMP;
  252. /* Alloc a new receive buffer */
  253. page = alloc_pages(gfp_mask,
  254. trans_pcie->rx_page_order);
  255. if (!page) {
  256. if (net_ratelimit())
  257. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  258. "order: %d\n",
  259. trans_pcie->rx_page_order);
  260. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  261. net_ratelimit())
  262. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  263. "Only %u free buffers remaining.\n",
  264. priority == GFP_ATOMIC ?
  265. "GFP_ATOMIC" : "GFP_KERNEL",
  266. rxq->free_count);
  267. /* We don't reschedule replenish work here -- we will
  268. * call the restock method and if it still needs
  269. * more buffers it will schedule replenish */
  270. return;
  271. }
  272. spin_lock_irqsave(&rxq->lock, flags);
  273. if (list_empty(&rxq->rx_used)) {
  274. spin_unlock_irqrestore(&rxq->lock, flags);
  275. __free_pages(page, trans_pcie->rx_page_order);
  276. return;
  277. }
  278. element = rxq->rx_used.next;
  279. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  280. list_del(element);
  281. spin_unlock_irqrestore(&rxq->lock, flags);
  282. BUG_ON(rxb->page);
  283. rxb->page = page;
  284. /* Get physical address of the RB */
  285. rxb->page_dma = dma_map_page(trans->dev, page, 0,
  286. PAGE_SIZE << trans_pcie->rx_page_order,
  287. DMA_FROM_DEVICE);
  288. /* dma address must be no more than 36 bits */
  289. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  290. /* and also 256 byte aligned! */
  291. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  292. spin_lock_irqsave(&rxq->lock, flags);
  293. list_add_tail(&rxb->list, &rxq->rx_free);
  294. rxq->free_count++;
  295. spin_unlock_irqrestore(&rxq->lock, flags);
  296. }
  297. }
  298. void iwlagn_rx_replenish(struct iwl_trans *trans)
  299. {
  300. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  301. unsigned long flags;
  302. iwlagn_rx_allocate(trans, GFP_KERNEL);
  303. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  304. iwlagn_rx_queue_restock(trans);
  305. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  306. }
  307. static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
  308. {
  309. iwlagn_rx_allocate(trans, GFP_ATOMIC);
  310. iwlagn_rx_queue_restock(trans);
  311. }
  312. void iwl_bg_rx_replenish(struct work_struct *data)
  313. {
  314. struct iwl_trans_pcie *trans_pcie =
  315. container_of(data, struct iwl_trans_pcie, rx_replenish);
  316. iwlagn_rx_replenish(trans_pcie->trans);
  317. }
  318. static void iwl_rx_handle_rxbuf(struct iwl_trans *trans,
  319. struct iwl_rx_mem_buffer *rxb)
  320. {
  321. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  322. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  323. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  324. unsigned long flags;
  325. bool page_stolen = false;
  326. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  327. u32 offset = 0;
  328. if (WARN_ON(!rxb))
  329. return;
  330. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  331. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  332. struct iwl_rx_packet *pkt;
  333. struct iwl_device_cmd *cmd;
  334. u16 sequence;
  335. bool reclaim;
  336. int index, cmd_index, err, len;
  337. struct iwl_rx_cmd_buffer rxcb = {
  338. ._offset = offset,
  339. ._page = rxb->page,
  340. ._page_stolen = false,
  341. .truesize = max_len,
  342. };
  343. pkt = rxb_addr(&rxcb);
  344. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  345. break;
  346. IWL_DEBUG_RX(trans, "cmd at offset %d: %s (0x%.2x)\n",
  347. rxcb._offset,
  348. trans_pcie_get_cmd_string(trans_pcie, pkt->hdr.cmd),
  349. pkt->hdr.cmd);
  350. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  351. len += sizeof(u32); /* account for status word */
  352. trace_iwlwifi_dev_rx(trans->dev, pkt, len);
  353. /* Reclaim a command buffer only if this packet is a response
  354. * to a (driver-originated) command.
  355. * If the packet (e.g. Rx frame) originated from uCode,
  356. * there is no command buffer to reclaim.
  357. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  358. * but apparently a few don't get set; catch them here. */
  359. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  360. if (reclaim) {
  361. int i;
  362. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  363. if (trans_pcie->no_reclaim_cmds[i] ==
  364. pkt->hdr.cmd) {
  365. reclaim = false;
  366. break;
  367. }
  368. }
  369. }
  370. sequence = le16_to_cpu(pkt->hdr.sequence);
  371. index = SEQ_TO_INDEX(sequence);
  372. cmd_index = get_cmd_index(&txq->q, index);
  373. if (reclaim)
  374. cmd = txq->entries[cmd_index].cmd;
  375. else
  376. cmd = NULL;
  377. err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
  378. /*
  379. * After here, we should always check rxcb._page_stolen,
  380. * if it is true then one of the handlers took the page.
  381. */
  382. if (reclaim) {
  383. /* Invoke any callbacks, transfer the buffer to caller,
  384. * and fire off the (possibly) blocking
  385. * iwl_trans_send_cmd()
  386. * as we reclaim the driver command queue */
  387. if (!rxcb._page_stolen)
  388. iwl_tx_cmd_complete(trans, &rxcb, err);
  389. else
  390. IWL_WARN(trans, "Claim null rxb?\n");
  391. }
  392. page_stolen |= rxcb._page_stolen;
  393. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  394. }
  395. /* page was stolen from us -- free our reference */
  396. if (page_stolen) {
  397. __free_pages(rxb->page, trans_pcie->rx_page_order);
  398. rxb->page = NULL;
  399. }
  400. /* Reuse the page if possible. For notification packets and
  401. * SKBs that fail to Rx correctly, add them back into the
  402. * rx_free list for reuse later. */
  403. spin_lock_irqsave(&rxq->lock, flags);
  404. if (rxb->page != NULL) {
  405. rxb->page_dma =
  406. dma_map_page(trans->dev, rxb->page, 0,
  407. PAGE_SIZE << trans_pcie->rx_page_order,
  408. DMA_FROM_DEVICE);
  409. list_add_tail(&rxb->list, &rxq->rx_free);
  410. rxq->free_count++;
  411. } else
  412. list_add_tail(&rxb->list, &rxq->rx_used);
  413. spin_unlock_irqrestore(&rxq->lock, flags);
  414. }
  415. /**
  416. * iwl_rx_handle - Main entry function for receiving responses from uCode
  417. *
  418. * Uses the priv->rx_handlers callback function array to invoke
  419. * the appropriate handlers, including command responses,
  420. * frame-received notifications, and other notifications.
  421. */
  422. static void iwl_rx_handle(struct iwl_trans *trans)
  423. {
  424. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  425. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  426. u32 r, i;
  427. u8 fill_rx = 0;
  428. u32 count = 8;
  429. int total_empty;
  430. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  431. * buffer that the driver may process (last buffer filled by ucode). */
  432. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  433. i = rxq->read;
  434. /* Rx interrupt, but nothing sent from uCode */
  435. if (i == r)
  436. IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
  437. /* calculate total frames need to be restock after handling RX */
  438. total_empty = r - rxq->write_actual;
  439. if (total_empty < 0)
  440. total_empty += RX_QUEUE_SIZE;
  441. if (total_empty > (RX_QUEUE_SIZE / 2))
  442. fill_rx = 1;
  443. while (i != r) {
  444. struct iwl_rx_mem_buffer *rxb;
  445. rxb = rxq->queue[i];
  446. rxq->queue[i] = NULL;
  447. IWL_DEBUG_RX(trans, "rxbuf: r = %d, i = %d (%p)\n", rxb);
  448. iwl_rx_handle_rxbuf(trans, rxb);
  449. i = (i + 1) & RX_QUEUE_MASK;
  450. /* If there are a lot of unused frames,
  451. * restock the Rx queue so ucode wont assert. */
  452. if (fill_rx) {
  453. count++;
  454. if (count >= 8) {
  455. rxq->read = i;
  456. iwlagn_rx_replenish_now(trans);
  457. count = 0;
  458. }
  459. }
  460. }
  461. /* Backtrack one entry */
  462. rxq->read = i;
  463. if (fill_rx)
  464. iwlagn_rx_replenish_now(trans);
  465. else
  466. iwlagn_rx_queue_restock(trans);
  467. }
  468. /**
  469. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  470. */
  471. static void iwl_irq_handle_error(struct iwl_trans *trans)
  472. {
  473. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  474. if (trans->cfg->internal_wimax_coex &&
  475. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  476. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  477. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  478. APMG_PS_CTRL_VAL_RESET_REQ))) {
  479. struct iwl_trans_pcie *trans_pcie;
  480. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  481. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  482. iwl_op_mode_wimax_active(trans->op_mode);
  483. wake_up(&trans->wait_command_queue);
  484. return;
  485. }
  486. iwl_dump_csr(trans);
  487. iwl_dump_fh(trans, NULL, false);
  488. iwl_op_mode_nic_error(trans->op_mode);
  489. }
  490. /* tasklet for iwlagn interrupt */
  491. void iwl_irq_tasklet(struct iwl_trans *trans)
  492. {
  493. u32 inta = 0;
  494. u32 handled = 0;
  495. unsigned long flags;
  496. u32 i;
  497. #ifdef CONFIG_IWLWIFI_DEBUG
  498. u32 inta_mask;
  499. #endif
  500. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  501. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  502. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  503. /* Ack/clear/reset pending uCode interrupts.
  504. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  505. */
  506. /* There is a hardware bug in the interrupt mask function that some
  507. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  508. * they are disabled in the CSR_INT_MASK register. Furthermore the
  509. * ICT interrupt handling mechanism has another bug that might cause
  510. * these unmasked interrupts fail to be detected. We workaround the
  511. * hardware bugs here by ACKing all the possible interrupts so that
  512. * interrupt coalescing can still be achieved.
  513. */
  514. iwl_write32(trans, CSR_INT,
  515. trans_pcie->inta | ~trans_pcie->inta_mask);
  516. inta = trans_pcie->inta;
  517. #ifdef CONFIG_IWLWIFI_DEBUG
  518. if (iwl_have_debug_level(IWL_DL_ISR)) {
  519. /* just for debug */
  520. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  521. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  522. inta, inta_mask);
  523. }
  524. #endif
  525. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  526. trans_pcie->inta = 0;
  527. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  528. /* Now service all interrupt bits discovered above. */
  529. if (inta & CSR_INT_BIT_HW_ERR) {
  530. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  531. /* Tell the device to stop sending interrupts */
  532. iwl_disable_interrupts(trans);
  533. isr_stats->hw++;
  534. iwl_irq_handle_error(trans);
  535. handled |= CSR_INT_BIT_HW_ERR;
  536. return;
  537. }
  538. #ifdef CONFIG_IWLWIFI_DEBUG
  539. if (iwl_have_debug_level(IWL_DL_ISR)) {
  540. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  541. if (inta & CSR_INT_BIT_SCD) {
  542. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  543. "the frame/frames.\n");
  544. isr_stats->sch++;
  545. }
  546. /* Alive notification via Rx interrupt will do the real work */
  547. if (inta & CSR_INT_BIT_ALIVE) {
  548. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  549. isr_stats->alive++;
  550. }
  551. }
  552. #endif
  553. /* Safely ignore these bits for debug checks below */
  554. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  555. /* HW RF KILL switch toggled */
  556. if (inta & CSR_INT_BIT_RF_KILL) {
  557. bool hw_rfkill;
  558. hw_rfkill = iwl_is_rfkill_set(trans);
  559. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  560. hw_rfkill ? "disable radio" : "enable radio");
  561. isr_stats->rfkill++;
  562. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  563. handled |= CSR_INT_BIT_RF_KILL;
  564. }
  565. /* Chip got too hot and stopped itself */
  566. if (inta & CSR_INT_BIT_CT_KILL) {
  567. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  568. isr_stats->ctkill++;
  569. handled |= CSR_INT_BIT_CT_KILL;
  570. }
  571. /* Error detected by uCode */
  572. if (inta & CSR_INT_BIT_SW_ERR) {
  573. IWL_ERR(trans, "Microcode SW error detected. "
  574. " Restarting 0x%X.\n", inta);
  575. isr_stats->sw++;
  576. iwl_irq_handle_error(trans);
  577. handled |= CSR_INT_BIT_SW_ERR;
  578. }
  579. /* uCode wakes up after power-down sleep */
  580. if (inta & CSR_INT_BIT_WAKEUP) {
  581. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  582. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  583. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  584. iwl_txq_update_write_ptr(trans,
  585. &trans_pcie->txq[i]);
  586. isr_stats->wakeup++;
  587. handled |= CSR_INT_BIT_WAKEUP;
  588. }
  589. /* All uCode command responses, including Tx command responses,
  590. * Rx "responses" (frame-received notification), and other
  591. * notifications from uCode come through here*/
  592. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  593. CSR_INT_BIT_RX_PERIODIC)) {
  594. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  595. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  596. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  597. iwl_write32(trans, CSR_FH_INT_STATUS,
  598. CSR_FH_INT_RX_MASK);
  599. }
  600. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  601. handled |= CSR_INT_BIT_RX_PERIODIC;
  602. iwl_write32(trans,
  603. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  604. }
  605. /* Sending RX interrupt require many steps to be done in the
  606. * the device:
  607. * 1- write interrupt to current index in ICT table.
  608. * 2- dma RX frame.
  609. * 3- update RX shared data to indicate last write index.
  610. * 4- send interrupt.
  611. * This could lead to RX race, driver could receive RX interrupt
  612. * but the shared data changes does not reflect this;
  613. * periodic interrupt will detect any dangling Rx activity.
  614. */
  615. /* Disable periodic interrupt; we use it as just a one-shot. */
  616. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  617. CSR_INT_PERIODIC_DIS);
  618. #ifdef CONFIG_IWLWIFI_IDI
  619. iwl_amfh_rx_handler();
  620. #else
  621. iwl_rx_handle(trans);
  622. #endif
  623. /*
  624. * Enable periodic interrupt in 8 msec only if we received
  625. * real RX interrupt (instead of just periodic int), to catch
  626. * any dangling Rx interrupt. If it was just the periodic
  627. * interrupt, there was no dangling Rx activity, and no need
  628. * to extend the periodic interrupt; one-shot is enough.
  629. */
  630. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  631. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  632. CSR_INT_PERIODIC_ENA);
  633. isr_stats->rx++;
  634. }
  635. /* This "Tx" DMA channel is used only for loading uCode */
  636. if (inta & CSR_INT_BIT_FH_TX) {
  637. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  638. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  639. isr_stats->tx++;
  640. handled |= CSR_INT_BIT_FH_TX;
  641. /* Wake up uCode load routine, now that load is complete */
  642. trans_pcie->ucode_write_complete = true;
  643. wake_up(&trans_pcie->ucode_write_waitq);
  644. }
  645. if (inta & ~handled) {
  646. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  647. isr_stats->unhandled++;
  648. }
  649. if (inta & ~(trans_pcie->inta_mask)) {
  650. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  651. inta & ~trans_pcie->inta_mask);
  652. }
  653. /* Re-enable all interrupts */
  654. /* only Re-enable if disabled by irq */
  655. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  656. iwl_enable_interrupts(trans);
  657. /* Re-enable RF_KILL if it occurred */
  658. else if (handled & CSR_INT_BIT_RF_KILL)
  659. iwl_enable_rfkill_int(trans);
  660. }
  661. /******************************************************************************
  662. *
  663. * ICT functions
  664. *
  665. ******************************************************************************/
  666. /* a device (PCI-E) page is 4096 bytes long */
  667. #define ICT_SHIFT 12
  668. #define ICT_SIZE (1 << ICT_SHIFT)
  669. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  670. /* Free dram table */
  671. void iwl_free_isr_ict(struct iwl_trans *trans)
  672. {
  673. struct iwl_trans_pcie *trans_pcie =
  674. IWL_TRANS_GET_PCIE_TRANS(trans);
  675. if (trans_pcie->ict_tbl) {
  676. dma_free_coherent(trans->dev, ICT_SIZE,
  677. trans_pcie->ict_tbl,
  678. trans_pcie->ict_tbl_dma);
  679. trans_pcie->ict_tbl = NULL;
  680. trans_pcie->ict_tbl_dma = 0;
  681. }
  682. }
  683. /*
  684. * allocate dram shared table, it is an aligned memory
  685. * block of ICT_SIZE.
  686. * also reset all data related to ICT table interrupt.
  687. */
  688. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  689. {
  690. struct iwl_trans_pcie *trans_pcie =
  691. IWL_TRANS_GET_PCIE_TRANS(trans);
  692. trans_pcie->ict_tbl =
  693. dma_alloc_coherent(trans->dev, ICT_SIZE,
  694. &trans_pcie->ict_tbl_dma,
  695. GFP_KERNEL);
  696. if (!trans_pcie->ict_tbl)
  697. return -ENOMEM;
  698. /* just an API sanity check ... it is guaranteed to be aligned */
  699. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  700. iwl_free_isr_ict(trans);
  701. return -EINVAL;
  702. }
  703. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  704. (unsigned long long)trans_pcie->ict_tbl_dma);
  705. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  706. /* reset table and index to all 0 */
  707. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  708. trans_pcie->ict_index = 0;
  709. /* add periodic RX interrupt */
  710. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  711. return 0;
  712. }
  713. /* Device is going up inform it about using ICT interrupt table,
  714. * also we need to tell the driver to start using ICT interrupt.
  715. */
  716. void iwl_reset_ict(struct iwl_trans *trans)
  717. {
  718. u32 val;
  719. unsigned long flags;
  720. struct iwl_trans_pcie *trans_pcie =
  721. IWL_TRANS_GET_PCIE_TRANS(trans);
  722. if (!trans_pcie->ict_tbl)
  723. return;
  724. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  725. iwl_disable_interrupts(trans);
  726. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  727. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  728. val |= CSR_DRAM_INT_TBL_ENABLE;
  729. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  730. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  731. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  732. trans_pcie->use_ict = true;
  733. trans_pcie->ict_index = 0;
  734. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  735. iwl_enable_interrupts(trans);
  736. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  737. }
  738. /* Device is going down disable ict interrupt usage */
  739. void iwl_disable_ict(struct iwl_trans *trans)
  740. {
  741. struct iwl_trans_pcie *trans_pcie =
  742. IWL_TRANS_GET_PCIE_TRANS(trans);
  743. unsigned long flags;
  744. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  745. trans_pcie->use_ict = false;
  746. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  747. }
  748. static irqreturn_t iwl_isr(int irq, void *data)
  749. {
  750. struct iwl_trans *trans = data;
  751. struct iwl_trans_pcie *trans_pcie;
  752. u32 inta, inta_mask;
  753. unsigned long flags;
  754. #ifdef CONFIG_IWLWIFI_DEBUG
  755. u32 inta_fh;
  756. #endif
  757. if (!trans)
  758. return IRQ_NONE;
  759. trace_iwlwifi_dev_irq(trans->dev);
  760. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  761. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  762. /* Disable (but don't clear!) interrupts here to avoid
  763. * back-to-back ISRs and sporadic interrupts from our NIC.
  764. * If we have something to service, the tasklet will re-enable ints.
  765. * If we *don't* have something, we'll re-enable before leaving here. */
  766. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  767. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  768. /* Discover which interrupts are active/pending */
  769. inta = iwl_read32(trans, CSR_INT);
  770. /* Ignore interrupt if there's nothing in NIC to service.
  771. * This may be due to IRQ shared with another device,
  772. * or due to sporadic interrupts thrown from our NIC. */
  773. if (!inta) {
  774. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  775. goto none;
  776. }
  777. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  778. /* Hardware disappeared. It might have already raised
  779. * an interrupt */
  780. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  781. goto unplugged;
  782. }
  783. #ifdef CONFIG_IWLWIFI_DEBUG
  784. if (iwl_have_debug_level(IWL_DL_ISR)) {
  785. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  786. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  787. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  788. }
  789. #endif
  790. trans_pcie->inta |= inta;
  791. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  792. if (likely(inta))
  793. tasklet_schedule(&trans_pcie->irq_tasklet);
  794. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  795. !trans_pcie->inta)
  796. iwl_enable_interrupts(trans);
  797. unplugged:
  798. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  799. return IRQ_HANDLED;
  800. none:
  801. /* re-enable interrupts here since we don't have anything to service. */
  802. /* only Re-enable if disabled by irq and no schedules tasklet. */
  803. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  804. !trans_pcie->inta)
  805. iwl_enable_interrupts(trans);
  806. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  807. return IRQ_NONE;
  808. }
  809. /* interrupt handler using ict table, with this interrupt driver will
  810. * stop using INTA register to get device's interrupt, reading this register
  811. * is expensive, device will write interrupts in ICT dram table, increment
  812. * index then will fire interrupt to driver, driver will OR all ICT table
  813. * entries from current index up to table entry with 0 value. the result is
  814. * the interrupt we need to service, driver will set the entries back to 0 and
  815. * set index.
  816. */
  817. irqreturn_t iwl_isr_ict(int irq, void *data)
  818. {
  819. struct iwl_trans *trans = data;
  820. struct iwl_trans_pcie *trans_pcie;
  821. u32 inta, inta_mask;
  822. u32 val = 0;
  823. u32 read;
  824. unsigned long flags;
  825. if (!trans)
  826. return IRQ_NONE;
  827. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  828. /* dram interrupt table not set yet,
  829. * use legacy interrupt.
  830. */
  831. if (!trans_pcie->use_ict)
  832. return iwl_isr(irq, data);
  833. trace_iwlwifi_dev_irq(trans->dev);
  834. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  835. /* Disable (but don't clear!) interrupts here to avoid
  836. * back-to-back ISRs and sporadic interrupts from our NIC.
  837. * If we have something to service, the tasklet will re-enable ints.
  838. * If we *don't* have something, we'll re-enable before leaving here.
  839. */
  840. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  841. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  842. /* Ignore interrupt if there's nothing in NIC to service.
  843. * This may be due to IRQ shared with another device,
  844. * or due to sporadic interrupts thrown from our NIC. */
  845. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  846. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  847. if (!read) {
  848. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  849. goto none;
  850. }
  851. /*
  852. * Collect all entries up to the first 0, starting from ict_index;
  853. * note we already read at ict_index.
  854. */
  855. do {
  856. val |= read;
  857. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  858. trans_pcie->ict_index, read);
  859. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  860. trans_pcie->ict_index =
  861. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  862. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  863. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  864. read);
  865. } while (read);
  866. /* We should not get this value, just ignore it. */
  867. if (val == 0xffffffff)
  868. val = 0;
  869. /*
  870. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  871. * (bit 15 before shifting it to 31) to clear when using interrupt
  872. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  873. * so we use them to decide on the real state of the Rx bit.
  874. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  875. */
  876. if (val & 0xC0000)
  877. val |= 0x8000;
  878. inta = (0xff & val) | ((0xff00 & val) << 16);
  879. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  880. inta, inta_mask, val);
  881. inta &= trans_pcie->inta_mask;
  882. trans_pcie->inta |= inta;
  883. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  884. if (likely(inta))
  885. tasklet_schedule(&trans_pcie->irq_tasklet);
  886. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  887. !trans_pcie->inta) {
  888. /* Allow interrupt if was disabled by this handler and
  889. * no tasklet was schedules, We should not enable interrupt,
  890. * tasklet will enable it.
  891. */
  892. iwl_enable_interrupts(trans);
  893. }
  894. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  895. return IRQ_HANDLED;
  896. none:
  897. /* re-enable interrupts here since we don't have anything to service.
  898. * only Re-enable if disabled by irq.
  899. */
  900. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  901. !trans_pcie->inta)
  902. iwl_enable_interrupts(trans);
  903. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  904. return IRQ_NONE;
  905. }