rx.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 "internal.h"
  35. #include "iwl-op-mode.h"
  36. /******************************************************************************
  37. *
  38. * RX path functions
  39. *
  40. ******************************************************************************/
  41. /*
  42. * Rx theory of operation
  43. *
  44. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  45. * each of which point to Receive Buffers to be filled by the NIC. These get
  46. * used not only for Rx frames, but for any command response or notification
  47. * from the NIC. The driver and NIC manage the Rx buffers by means
  48. * of indexes into the circular buffer.
  49. *
  50. * Rx Queue Indexes
  51. * The host/firmware share two index registers for managing the Rx buffers.
  52. *
  53. * The READ index maps to the first position that the firmware may be writing
  54. * to -- the driver can read up to (but not including) this position and get
  55. * good data.
  56. * The READ index is managed by the firmware once the card is enabled.
  57. *
  58. * The WRITE index maps to the last position the driver has read from -- the
  59. * position preceding WRITE is the last slot the firmware can place a packet.
  60. *
  61. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  62. * WRITE = READ.
  63. *
  64. * During initialization, the host sets up the READ queue position to the first
  65. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  66. *
  67. * When the firmware places a packet in a buffer, it will advance the READ index
  68. * and fire the RX interrupt. The driver can then query the READ index and
  69. * process as many packets as possible, moving the WRITE index forward as it
  70. * resets the Rx queue buffers with new memory.
  71. *
  72. * The management in the driver is as follows:
  73. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  74. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  75. * to replenish the iwl->rxq->rx_free.
  76. * + In iwl_pcie_rx_replenish (scheduled) if 'processed' != 'read' then the
  77. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  78. * 'processed' and 'read' driver indexes as well)
  79. * + A received packet is processed and handed to the kernel network stack,
  80. * detached from the iwl->rxq. The driver 'processed' index is updated.
  81. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  82. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  83. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  84. * were enough free buffers and RX_STALLED is set it is cleared.
  85. *
  86. *
  87. * Driver sequence:
  88. *
  89. * iwl_rxq_alloc() Allocates rx_free
  90. * iwl_pcie_rx_replenish() Replenishes rx_free list from rx_used, and calls
  91. * iwl_pcie_rxq_restock
  92. * iwl_pcie_rxq_restock() Moves available buffers from rx_free into Rx
  93. * queue, updates firmware pointers, and updates
  94. * the WRITE index. If insufficient rx_free buffers
  95. * are available, schedules iwl_pcie_rx_replenish
  96. *
  97. * -- enable interrupts --
  98. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  99. * READ INDEX, detaching the SKB from the pool.
  100. * Moves the packet buffer from queue to rx_used.
  101. * Calls iwl_pcie_rxq_restock to refill any empty
  102. * slots.
  103. * ...
  104. *
  105. */
  106. /*
  107. * iwl_rxq_space - Return number of free slots available in queue.
  108. */
  109. static int iwl_rxq_space(const struct iwl_rxq *q)
  110. {
  111. int s = q->read - q->write;
  112. if (s <= 0)
  113. s += RX_QUEUE_SIZE;
  114. /* keep some buffer to not confuse full and empty queue */
  115. s -= 2;
  116. if (s < 0)
  117. s = 0;
  118. return s;
  119. }
  120. /*
  121. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  122. */
  123. static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  124. {
  125. return cpu_to_le32((u32)(dma_addr >> 8));
  126. }
  127. int iwl_pcie_rx_stop(struct iwl_trans *trans)
  128. {
  129. /* stop Rx DMA */
  130. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  131. return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
  132. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  133. }
  134. /*
  135. * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
  136. * TODO - could be made static
  137. */
  138. void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_rxq *q)
  139. {
  140. unsigned long flags;
  141. u32 reg;
  142. spin_lock_irqsave(&q->lock, flags);
  143. if (q->need_update == 0)
  144. goto exit_unlock;
  145. if (trans->cfg->base_params->shadow_reg_enable) {
  146. /* shadow register enabled */
  147. /* Device expects a multiple of 8 */
  148. q->write_actual = (q->write & ~0x7);
  149. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
  150. } else {
  151. struct iwl_trans_pcie *trans_pcie =
  152. IWL_TRANS_GET_PCIE_TRANS(trans);
  153. /* If power-saving is in use, make sure device is awake */
  154. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
  155. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  156. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  157. IWL_DEBUG_INFO(trans,
  158. "Rx queue requesting wakeup,"
  159. " GP1 = 0x%x\n", reg);
  160. iwl_set_bit(trans, CSR_GP_CNTRL,
  161. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  162. goto exit_unlock;
  163. }
  164. q->write_actual = (q->write & ~0x7);
  165. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  166. q->write_actual);
  167. /* Else device is assumed to be awake */
  168. } else {
  169. /* Device expects a multiple of 8 */
  170. q->write_actual = (q->write & ~0x7);
  171. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  172. q->write_actual);
  173. }
  174. }
  175. q->need_update = 0;
  176. exit_unlock:
  177. spin_unlock_irqrestore(&q->lock, flags);
  178. }
  179. /*
  180. * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
  181. *
  182. * If there are slots in the RX queue that need to be restocked,
  183. * and we have free pre-allocated buffers, fill the ranks as much
  184. * as we can, pulling from rx_free.
  185. *
  186. * This moves the 'write' index forward to catch up with 'processed', and
  187. * also updates the memory address in the firmware to reference the new
  188. * target buffer.
  189. */
  190. static void iwl_pcie_rxq_restock(struct iwl_trans *trans)
  191. {
  192. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  193. struct iwl_rxq *rxq = &trans_pcie->rxq;
  194. struct iwl_rx_mem_buffer *rxb;
  195. unsigned long flags;
  196. /*
  197. * If the device isn't enabled - not need to try to add buffers...
  198. * This can happen when we stop the device and still have an interrupt
  199. * pending. We stop the APM before we sync the interrupts / tasklets
  200. * because we have to (see comment there). On the other hand, since
  201. * the APM is stopped, we cannot access the HW (in particular not prph).
  202. * So don't try to restock if the APM has been already stopped.
  203. */
  204. if (!test_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status))
  205. return;
  206. spin_lock_irqsave(&rxq->lock, flags);
  207. while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
  208. /* The overwritten rxb must be a used one */
  209. rxb = rxq->queue[rxq->write];
  210. BUG_ON(rxb && rxb->page);
  211. /* Get next free Rx buffer, remove from free list */
  212. rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
  213. list);
  214. list_del(&rxb->list);
  215. /* Point to Rx buffer via next RBD in circular buffer */
  216. rxq->bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
  217. rxq->queue[rxq->write] = rxb;
  218. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  219. rxq->free_count--;
  220. }
  221. spin_unlock_irqrestore(&rxq->lock, flags);
  222. /* If the pre-allocated buffer pool is dropping low, schedule to
  223. * refill it */
  224. if (rxq->free_count <= RX_LOW_WATERMARK)
  225. schedule_work(&trans_pcie->rx_replenish);
  226. /* If we've added more space for the firmware to place data, tell it.
  227. * Increment device's write pointer in multiples of 8. */
  228. if (rxq->write_actual != (rxq->write & ~0x7)) {
  229. spin_lock_irqsave(&rxq->lock, flags);
  230. rxq->need_update = 1;
  231. spin_unlock_irqrestore(&rxq->lock, flags);
  232. iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
  233. }
  234. }
  235. /*
  236. * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
  237. *
  238. * A used RBD is an Rx buffer that has been given to the stack. To use it again
  239. * a page must be allocated and the RBD must point to the page. This function
  240. * doesn't change the HW pointer but handles the list of pages that is used by
  241. * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
  242. * allocated buffers.
  243. */
  244. static void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority)
  245. {
  246. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  247. struct iwl_rxq *rxq = &trans_pcie->rxq;
  248. struct iwl_rx_mem_buffer *rxb;
  249. struct page *page;
  250. unsigned long flags;
  251. gfp_t gfp_mask = priority;
  252. while (1) {
  253. spin_lock_irqsave(&rxq->lock, flags);
  254. if (list_empty(&rxq->rx_used)) {
  255. spin_unlock_irqrestore(&rxq->lock, flags);
  256. return;
  257. }
  258. spin_unlock_irqrestore(&rxq->lock, flags);
  259. if (rxq->free_count > RX_LOW_WATERMARK)
  260. gfp_mask |= __GFP_NOWARN;
  261. if (trans_pcie->rx_page_order > 0)
  262. gfp_mask |= __GFP_COMP;
  263. /* Alloc a new receive buffer */
  264. page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
  265. if (!page) {
  266. if (net_ratelimit())
  267. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  268. "order: %d\n",
  269. trans_pcie->rx_page_order);
  270. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  271. net_ratelimit())
  272. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  273. "Only %u free buffers remaining.\n",
  274. priority == GFP_ATOMIC ?
  275. "GFP_ATOMIC" : "GFP_KERNEL",
  276. rxq->free_count);
  277. /* We don't reschedule replenish work here -- we will
  278. * call the restock method and if it still needs
  279. * more buffers it will schedule replenish */
  280. return;
  281. }
  282. spin_lock_irqsave(&rxq->lock, flags);
  283. if (list_empty(&rxq->rx_used)) {
  284. spin_unlock_irqrestore(&rxq->lock, flags);
  285. __free_pages(page, trans_pcie->rx_page_order);
  286. return;
  287. }
  288. rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
  289. list);
  290. list_del(&rxb->list);
  291. spin_unlock_irqrestore(&rxq->lock, flags);
  292. BUG_ON(rxb->page);
  293. rxb->page = page;
  294. /* Get physical address of the RB */
  295. rxb->page_dma =
  296. dma_map_page(trans->dev, page, 0,
  297. PAGE_SIZE << trans_pcie->rx_page_order,
  298. DMA_FROM_DEVICE);
  299. /* dma address must be no more than 36 bits */
  300. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  301. /* and also 256 byte aligned! */
  302. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  303. spin_lock_irqsave(&rxq->lock, flags);
  304. list_add_tail(&rxb->list, &rxq->rx_free);
  305. rxq->free_count++;
  306. spin_unlock_irqrestore(&rxq->lock, flags);
  307. }
  308. }
  309. static void iwl_pcie_rxq_free_rbs(struct iwl_trans *trans)
  310. {
  311. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  312. struct iwl_rxq *rxq = &trans_pcie->rxq;
  313. int i;
  314. /* Fill the rx_used queue with _all_ of the Rx buffers */
  315. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  316. /* In the reset function, these buffers may have been allocated
  317. * to an SKB, so we need to unmap and free potential storage */
  318. if (rxq->pool[i].page != NULL) {
  319. dma_unmap_page(trans->dev, rxq->pool[i].page_dma,
  320. PAGE_SIZE << trans_pcie->rx_page_order,
  321. DMA_FROM_DEVICE);
  322. __free_pages(rxq->pool[i].page,
  323. trans_pcie->rx_page_order);
  324. rxq->pool[i].page = NULL;
  325. }
  326. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  327. }
  328. }
  329. /*
  330. * iwl_pcie_rx_replenish - Move all used buffers from rx_used to rx_free
  331. *
  332. * When moving to rx_free an page is allocated for the slot.
  333. *
  334. * Also restock the Rx queue via iwl_pcie_rxq_restock.
  335. * This is called as a scheduled work item (except for during initialization)
  336. */
  337. static void iwl_pcie_rx_replenish(struct iwl_trans *trans)
  338. {
  339. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  340. unsigned long flags;
  341. iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL);
  342. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  343. iwl_pcie_rxq_restock(trans);
  344. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  345. }
  346. static void iwl_pcie_rx_replenish_now(struct iwl_trans *trans)
  347. {
  348. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC);
  349. iwl_pcie_rxq_restock(trans);
  350. }
  351. static void iwl_pcie_rx_replenish_work(struct work_struct *data)
  352. {
  353. struct iwl_trans_pcie *trans_pcie =
  354. container_of(data, struct iwl_trans_pcie, rx_replenish);
  355. iwl_pcie_rx_replenish(trans_pcie->trans);
  356. }
  357. static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
  358. {
  359. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  360. struct iwl_rxq *rxq = &trans_pcie->rxq;
  361. struct device *dev = trans->dev;
  362. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  363. spin_lock_init(&rxq->lock);
  364. if (WARN_ON(rxq->bd || rxq->rb_stts))
  365. return -EINVAL;
  366. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  367. rxq->bd = dma_zalloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  368. &rxq->bd_dma, GFP_KERNEL);
  369. if (!rxq->bd)
  370. goto err_bd;
  371. /*Allocate the driver's pointer to receive buffer status */
  372. rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
  373. &rxq->rb_stts_dma, GFP_KERNEL);
  374. if (!rxq->rb_stts)
  375. goto err_rb_stts;
  376. return 0;
  377. err_rb_stts:
  378. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  379. rxq->bd, rxq->bd_dma);
  380. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  381. rxq->bd = NULL;
  382. err_bd:
  383. return -ENOMEM;
  384. }
  385. static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
  386. {
  387. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  388. u32 rb_size;
  389. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  390. /* FIXME: RX_RB_TIMEOUT for all devices? */
  391. u32 rb_timeout = RX_RB_TIMEOUT;
  392. if (trans_pcie->rx_buf_size_8k)
  393. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  394. else
  395. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  396. /* Stop Rx DMA */
  397. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  398. /* Reset driver's Rx queue write index */
  399. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  400. /* Tell device where to find RBD circular buffer in DRAM */
  401. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  402. (u32)(rxq->bd_dma >> 8));
  403. /* Tell device where in DRAM to update its Rx status */
  404. iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  405. rxq->rb_stts_dma >> 4);
  406. /* Enable Rx DMA
  407. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  408. * the credit mechanism in 5000 HW RX FIFO
  409. * Direct rx interrupts to hosts
  410. * Rx buffer size 4 or 8k
  411. * RB timeout 0x10
  412. * 256 RBDs
  413. */
  414. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  415. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  416. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  417. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  418. rb_size|
  419. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  420. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  421. /* Set interrupt coalescing timer to default (2048 usecs) */
  422. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  423. }
  424. int iwl_pcie_rx_init(struct iwl_trans *trans)
  425. {
  426. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  427. struct iwl_rxq *rxq = &trans_pcie->rxq;
  428. int i, err;
  429. unsigned long flags;
  430. if (!rxq->bd) {
  431. err = iwl_pcie_rx_alloc(trans);
  432. if (err)
  433. return err;
  434. }
  435. spin_lock_irqsave(&rxq->lock, flags);
  436. INIT_LIST_HEAD(&rxq->rx_free);
  437. INIT_LIST_HEAD(&rxq->rx_used);
  438. INIT_WORK(&trans_pcie->rx_replenish,
  439. iwl_pcie_rx_replenish_work);
  440. iwl_pcie_rxq_free_rbs(trans);
  441. for (i = 0; i < RX_QUEUE_SIZE; i++)
  442. rxq->queue[i] = NULL;
  443. /* Set us so that we have processed and used all buffers, but have
  444. * not restocked the Rx queue with fresh buffers */
  445. rxq->read = rxq->write = 0;
  446. rxq->write_actual = 0;
  447. rxq->free_count = 0;
  448. spin_unlock_irqrestore(&rxq->lock, flags);
  449. iwl_pcie_rx_replenish(trans);
  450. iwl_pcie_rx_hw_init(trans, rxq);
  451. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  452. rxq->need_update = 1;
  453. iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
  454. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  455. return 0;
  456. }
  457. void iwl_pcie_rx_free(struct iwl_trans *trans)
  458. {
  459. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  460. struct iwl_rxq *rxq = &trans_pcie->rxq;
  461. unsigned long flags;
  462. /*if rxq->bd is NULL, it means that nothing has been allocated,
  463. * exit now */
  464. if (!rxq->bd) {
  465. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  466. return;
  467. }
  468. spin_lock_irqsave(&rxq->lock, flags);
  469. iwl_pcie_rxq_free_rbs(trans);
  470. spin_unlock_irqrestore(&rxq->lock, flags);
  471. dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  472. rxq->bd, rxq->bd_dma);
  473. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  474. rxq->bd = NULL;
  475. if (rxq->rb_stts)
  476. dma_free_coherent(trans->dev,
  477. sizeof(struct iwl_rb_status),
  478. rxq->rb_stts, rxq->rb_stts_dma);
  479. else
  480. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  481. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  482. rxq->rb_stts = NULL;
  483. }
  484. static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
  485. struct iwl_rx_mem_buffer *rxb)
  486. {
  487. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  488. struct iwl_rxq *rxq = &trans_pcie->rxq;
  489. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  490. unsigned long flags;
  491. bool page_stolen = false;
  492. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  493. u32 offset = 0;
  494. if (WARN_ON(!rxb))
  495. return;
  496. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  497. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  498. struct iwl_rx_packet *pkt;
  499. struct iwl_device_cmd *cmd;
  500. u16 sequence;
  501. bool reclaim;
  502. int index, cmd_index, err, len;
  503. struct iwl_rx_cmd_buffer rxcb = {
  504. ._offset = offset,
  505. ._page = rxb->page,
  506. ._page_stolen = false,
  507. .truesize = max_len,
  508. };
  509. pkt = rxb_addr(&rxcb);
  510. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  511. break;
  512. IWL_DEBUG_RX(trans, "cmd at offset %d: %s (0x%.2x)\n",
  513. rxcb._offset, get_cmd_string(trans_pcie, pkt->hdr.cmd),
  514. pkt->hdr.cmd);
  515. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  516. len += sizeof(u32); /* account for status word */
  517. trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
  518. trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
  519. /* Reclaim a command buffer only if this packet is a response
  520. * to a (driver-originated) command.
  521. * If the packet (e.g. Rx frame) originated from uCode,
  522. * there is no command buffer to reclaim.
  523. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  524. * but apparently a few don't get set; catch them here. */
  525. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  526. if (reclaim) {
  527. int i;
  528. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  529. if (trans_pcie->no_reclaim_cmds[i] ==
  530. pkt->hdr.cmd) {
  531. reclaim = false;
  532. break;
  533. }
  534. }
  535. }
  536. sequence = le16_to_cpu(pkt->hdr.sequence);
  537. index = SEQ_TO_INDEX(sequence);
  538. cmd_index = get_cmd_index(&txq->q, index);
  539. if (reclaim) {
  540. struct iwl_pcie_txq_entry *ent;
  541. ent = &txq->entries[cmd_index];
  542. cmd = ent->copy_cmd;
  543. WARN_ON_ONCE(!cmd && ent->meta.flags & CMD_WANT_HCMD);
  544. } else {
  545. cmd = NULL;
  546. }
  547. err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
  548. if (reclaim) {
  549. /* The original command isn't needed any more */
  550. kfree(txq->entries[cmd_index].copy_cmd);
  551. txq->entries[cmd_index].copy_cmd = NULL;
  552. /* nor is the duplicated part of the command */
  553. kfree(txq->entries[cmd_index].free_buf);
  554. txq->entries[cmd_index].free_buf = NULL;
  555. }
  556. /*
  557. * After here, we should always check rxcb._page_stolen,
  558. * if it is true then one of the handlers took the page.
  559. */
  560. if (reclaim) {
  561. /* Invoke any callbacks, transfer the buffer to caller,
  562. * and fire off the (possibly) blocking
  563. * iwl_trans_send_cmd()
  564. * as we reclaim the driver command queue */
  565. if (!rxcb._page_stolen)
  566. iwl_pcie_hcmd_complete(trans, &rxcb, err);
  567. else
  568. IWL_WARN(trans, "Claim null rxb?\n");
  569. }
  570. page_stolen |= rxcb._page_stolen;
  571. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  572. }
  573. /* page was stolen from us -- free our reference */
  574. if (page_stolen) {
  575. __free_pages(rxb->page, trans_pcie->rx_page_order);
  576. rxb->page = NULL;
  577. }
  578. /* Reuse the page if possible. For notification packets and
  579. * SKBs that fail to Rx correctly, add them back into the
  580. * rx_free list for reuse later. */
  581. spin_lock_irqsave(&rxq->lock, flags);
  582. if (rxb->page != NULL) {
  583. rxb->page_dma =
  584. dma_map_page(trans->dev, rxb->page, 0,
  585. PAGE_SIZE << trans_pcie->rx_page_order,
  586. DMA_FROM_DEVICE);
  587. list_add_tail(&rxb->list, &rxq->rx_free);
  588. rxq->free_count++;
  589. } else
  590. list_add_tail(&rxb->list, &rxq->rx_used);
  591. spin_unlock_irqrestore(&rxq->lock, flags);
  592. }
  593. /*
  594. * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
  595. */
  596. static void iwl_pcie_rx_handle(struct iwl_trans *trans)
  597. {
  598. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  599. struct iwl_rxq *rxq = &trans_pcie->rxq;
  600. u32 r, i;
  601. u8 fill_rx = 0;
  602. u32 count = 8;
  603. int total_empty;
  604. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  605. * buffer that the driver may process (last buffer filled by ucode). */
  606. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  607. i = rxq->read;
  608. /* Rx interrupt, but nothing sent from uCode */
  609. if (i == r)
  610. IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
  611. /* calculate total frames need to be restock after handling RX */
  612. total_empty = r - rxq->write_actual;
  613. if (total_empty < 0)
  614. total_empty += RX_QUEUE_SIZE;
  615. if (total_empty > (RX_QUEUE_SIZE / 2))
  616. fill_rx = 1;
  617. while (i != r) {
  618. struct iwl_rx_mem_buffer *rxb;
  619. rxb = rxq->queue[i];
  620. rxq->queue[i] = NULL;
  621. IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d (%p)\n",
  622. r, i, rxb);
  623. iwl_pcie_rx_handle_rb(trans, rxb);
  624. i = (i + 1) & RX_QUEUE_MASK;
  625. /* If there are a lot of unused frames,
  626. * restock the Rx queue so ucode wont assert. */
  627. if (fill_rx) {
  628. count++;
  629. if (count >= 8) {
  630. rxq->read = i;
  631. iwl_pcie_rx_replenish_now(trans);
  632. count = 0;
  633. }
  634. }
  635. }
  636. /* Backtrack one entry */
  637. rxq->read = i;
  638. if (fill_rx)
  639. iwl_pcie_rx_replenish_now(trans);
  640. else
  641. iwl_pcie_rxq_restock(trans);
  642. }
  643. /*
  644. * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
  645. */
  646. static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
  647. {
  648. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  649. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  650. if (trans->cfg->internal_wimax_coex &&
  651. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  652. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  653. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  654. APMG_PS_CTRL_VAL_RESET_REQ))) {
  655. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  656. iwl_op_mode_wimax_active(trans->op_mode);
  657. wake_up(&trans_pcie->wait_command_queue);
  658. return;
  659. }
  660. iwl_pcie_dump_csr(trans);
  661. iwl_pcie_dump_fh(trans, NULL);
  662. set_bit(STATUS_FW_ERROR, &trans_pcie->status);
  663. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  664. wake_up(&trans_pcie->wait_command_queue);
  665. iwl_op_mode_nic_error(trans->op_mode);
  666. }
  667. /* tasklet for iwlagn interrupt */
  668. void iwl_pcie_tasklet(struct iwl_trans *trans)
  669. {
  670. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  671. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  672. u32 inta = 0;
  673. u32 handled = 0;
  674. unsigned long flags;
  675. u32 i;
  676. #ifdef CONFIG_IWLWIFI_DEBUG
  677. u32 inta_mask;
  678. #endif
  679. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  680. /* Ack/clear/reset pending uCode interrupts.
  681. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  682. */
  683. /* There is a hardware bug in the interrupt mask function that some
  684. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  685. * they are disabled in the CSR_INT_MASK register. Furthermore the
  686. * ICT interrupt handling mechanism has another bug that might cause
  687. * these unmasked interrupts fail to be detected. We workaround the
  688. * hardware bugs here by ACKing all the possible interrupts so that
  689. * interrupt coalescing can still be achieved.
  690. */
  691. iwl_write32(trans, CSR_INT,
  692. trans_pcie->inta | ~trans_pcie->inta_mask);
  693. inta = trans_pcie->inta;
  694. #ifdef CONFIG_IWLWIFI_DEBUG
  695. if (iwl_have_debug_level(IWL_DL_ISR)) {
  696. /* just for debug */
  697. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  698. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  699. inta, inta_mask);
  700. }
  701. #endif
  702. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  703. trans_pcie->inta = 0;
  704. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  705. /* Now service all interrupt bits discovered above. */
  706. if (inta & CSR_INT_BIT_HW_ERR) {
  707. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  708. /* Tell the device to stop sending interrupts */
  709. iwl_disable_interrupts(trans);
  710. isr_stats->hw++;
  711. iwl_pcie_irq_handle_error(trans);
  712. handled |= CSR_INT_BIT_HW_ERR;
  713. return;
  714. }
  715. #ifdef CONFIG_IWLWIFI_DEBUG
  716. if (iwl_have_debug_level(IWL_DL_ISR)) {
  717. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  718. if (inta & CSR_INT_BIT_SCD) {
  719. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  720. "the frame/frames.\n");
  721. isr_stats->sch++;
  722. }
  723. /* Alive notification via Rx interrupt will do the real work */
  724. if (inta & CSR_INT_BIT_ALIVE) {
  725. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  726. isr_stats->alive++;
  727. }
  728. }
  729. #endif
  730. /* Safely ignore these bits for debug checks below */
  731. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  732. /* HW RF KILL switch toggled */
  733. if (inta & CSR_INT_BIT_RF_KILL) {
  734. bool hw_rfkill;
  735. hw_rfkill = iwl_is_rfkill_set(trans);
  736. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  737. hw_rfkill ? "disable radio" : "enable radio");
  738. isr_stats->rfkill++;
  739. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  740. if (hw_rfkill) {
  741. set_bit(STATUS_RFKILL, &trans_pcie->status);
  742. if (test_and_clear_bit(STATUS_HCMD_ACTIVE,
  743. &trans_pcie->status))
  744. IWL_DEBUG_RF_KILL(trans,
  745. "Rfkill while SYNC HCMD in flight\n");
  746. wake_up(&trans_pcie->wait_command_queue);
  747. } else {
  748. clear_bit(STATUS_RFKILL, &trans_pcie->status);
  749. }
  750. handled |= CSR_INT_BIT_RF_KILL;
  751. }
  752. /* Chip got too hot and stopped itself */
  753. if (inta & CSR_INT_BIT_CT_KILL) {
  754. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  755. isr_stats->ctkill++;
  756. handled |= CSR_INT_BIT_CT_KILL;
  757. }
  758. /* Error detected by uCode */
  759. if (inta & CSR_INT_BIT_SW_ERR) {
  760. IWL_ERR(trans, "Microcode SW error detected. "
  761. " Restarting 0x%X.\n", inta);
  762. isr_stats->sw++;
  763. iwl_pcie_irq_handle_error(trans);
  764. handled |= CSR_INT_BIT_SW_ERR;
  765. }
  766. /* uCode wakes up after power-down sleep */
  767. if (inta & CSR_INT_BIT_WAKEUP) {
  768. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  769. iwl_pcie_rxq_inc_wr_ptr(trans, &trans_pcie->rxq);
  770. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  771. iwl_pcie_txq_inc_wr_ptr(trans, &trans_pcie->txq[i]);
  772. isr_stats->wakeup++;
  773. handled |= CSR_INT_BIT_WAKEUP;
  774. }
  775. /* All uCode command responses, including Tx command responses,
  776. * Rx "responses" (frame-received notification), and other
  777. * notifications from uCode come through here*/
  778. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  779. CSR_INT_BIT_RX_PERIODIC)) {
  780. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  781. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  782. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  783. iwl_write32(trans, CSR_FH_INT_STATUS,
  784. CSR_FH_INT_RX_MASK);
  785. }
  786. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  787. handled |= CSR_INT_BIT_RX_PERIODIC;
  788. iwl_write32(trans,
  789. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  790. }
  791. /* Sending RX interrupt require many steps to be done in the
  792. * the device:
  793. * 1- write interrupt to current index in ICT table.
  794. * 2- dma RX frame.
  795. * 3- update RX shared data to indicate last write index.
  796. * 4- send interrupt.
  797. * This could lead to RX race, driver could receive RX interrupt
  798. * but the shared data changes does not reflect this;
  799. * periodic interrupt will detect any dangling Rx activity.
  800. */
  801. /* Disable periodic interrupt; we use it as just a one-shot. */
  802. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  803. CSR_INT_PERIODIC_DIS);
  804. iwl_pcie_rx_handle(trans);
  805. /*
  806. * Enable periodic interrupt in 8 msec only if we received
  807. * real RX interrupt (instead of just periodic int), to catch
  808. * any dangling Rx interrupt. If it was just the periodic
  809. * interrupt, there was no dangling Rx activity, and no need
  810. * to extend the periodic interrupt; one-shot is enough.
  811. */
  812. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  813. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  814. CSR_INT_PERIODIC_ENA);
  815. isr_stats->rx++;
  816. }
  817. /* This "Tx" DMA channel is used only for loading uCode */
  818. if (inta & CSR_INT_BIT_FH_TX) {
  819. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  820. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  821. isr_stats->tx++;
  822. handled |= CSR_INT_BIT_FH_TX;
  823. /* Wake up uCode load routine, now that load is complete */
  824. trans_pcie->ucode_write_complete = true;
  825. wake_up(&trans_pcie->ucode_write_waitq);
  826. }
  827. if (inta & ~handled) {
  828. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  829. isr_stats->unhandled++;
  830. }
  831. if (inta & ~(trans_pcie->inta_mask)) {
  832. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  833. inta & ~trans_pcie->inta_mask);
  834. }
  835. /* Re-enable all interrupts */
  836. /* only Re-enable if disabled by irq */
  837. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  838. iwl_enable_interrupts(trans);
  839. /* Re-enable RF_KILL if it occurred */
  840. else if (handled & CSR_INT_BIT_RF_KILL)
  841. iwl_enable_rfkill_int(trans);
  842. }
  843. /******************************************************************************
  844. *
  845. * ICT functions
  846. *
  847. ******************************************************************************/
  848. /* a device (PCI-E) page is 4096 bytes long */
  849. #define ICT_SHIFT 12
  850. #define ICT_SIZE (1 << ICT_SHIFT)
  851. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  852. /* Free dram table */
  853. void iwl_pcie_free_ict(struct iwl_trans *trans)
  854. {
  855. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  856. if (trans_pcie->ict_tbl) {
  857. dma_free_coherent(trans->dev, ICT_SIZE,
  858. trans_pcie->ict_tbl,
  859. trans_pcie->ict_tbl_dma);
  860. trans_pcie->ict_tbl = NULL;
  861. trans_pcie->ict_tbl_dma = 0;
  862. }
  863. }
  864. /*
  865. * allocate dram shared table, it is an aligned memory
  866. * block of ICT_SIZE.
  867. * also reset all data related to ICT table interrupt.
  868. */
  869. int iwl_pcie_alloc_ict(struct iwl_trans *trans)
  870. {
  871. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  872. trans_pcie->ict_tbl =
  873. dma_alloc_coherent(trans->dev, ICT_SIZE,
  874. &trans_pcie->ict_tbl_dma,
  875. GFP_KERNEL);
  876. if (!trans_pcie->ict_tbl)
  877. return -ENOMEM;
  878. /* just an API sanity check ... it is guaranteed to be aligned */
  879. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  880. iwl_pcie_free_ict(trans);
  881. return -EINVAL;
  882. }
  883. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  884. (unsigned long long)trans_pcie->ict_tbl_dma);
  885. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  886. /* reset table and index to all 0 */
  887. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  888. trans_pcie->ict_index = 0;
  889. /* add periodic RX interrupt */
  890. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  891. return 0;
  892. }
  893. /* Device is going up inform it about using ICT interrupt table,
  894. * also we need to tell the driver to start using ICT interrupt.
  895. */
  896. void iwl_pcie_reset_ict(struct iwl_trans *trans)
  897. {
  898. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  899. u32 val;
  900. unsigned long flags;
  901. if (!trans_pcie->ict_tbl)
  902. return;
  903. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  904. iwl_disable_interrupts(trans);
  905. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  906. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  907. val |= CSR_DRAM_INT_TBL_ENABLE;
  908. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  909. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  910. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  911. trans_pcie->use_ict = true;
  912. trans_pcie->ict_index = 0;
  913. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  914. iwl_enable_interrupts(trans);
  915. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  916. }
  917. /* Device is going down disable ict interrupt usage */
  918. void iwl_pcie_disable_ict(struct iwl_trans *trans)
  919. {
  920. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  921. unsigned long flags;
  922. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  923. trans_pcie->use_ict = false;
  924. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  925. }
  926. /* legacy (non-ICT) ISR. Assumes that trans_pcie->irq_lock is held */
  927. static irqreturn_t iwl_pcie_isr(int irq, void *data)
  928. {
  929. struct iwl_trans *trans = data;
  930. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  931. u32 inta, inta_mask;
  932. #ifdef CONFIG_IWLWIFI_DEBUG
  933. u32 inta_fh;
  934. #endif
  935. lockdep_assert_held(&trans_pcie->irq_lock);
  936. trace_iwlwifi_dev_irq(trans->dev);
  937. /* Disable (but don't clear!) interrupts here to avoid
  938. * back-to-back ISRs and sporadic interrupts from our NIC.
  939. * If we have something to service, the tasklet will re-enable ints.
  940. * If we *don't* have something, we'll re-enable before leaving here. */
  941. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  942. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  943. /* Discover which interrupts are active/pending */
  944. inta = iwl_read32(trans, CSR_INT);
  945. /* Ignore interrupt if there's nothing in NIC to service.
  946. * This may be due to IRQ shared with another device,
  947. * or due to sporadic interrupts thrown from our NIC. */
  948. if (!inta) {
  949. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  950. goto none;
  951. }
  952. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  953. /* Hardware disappeared. It might have already raised
  954. * an interrupt */
  955. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  956. return IRQ_HANDLED;
  957. }
  958. #ifdef CONFIG_IWLWIFI_DEBUG
  959. if (iwl_have_debug_level(IWL_DL_ISR)) {
  960. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  961. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  962. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  963. }
  964. #endif
  965. trans_pcie->inta |= inta;
  966. /* iwl_pcie_tasklet() will service interrupts and re-enable them */
  967. if (likely(inta))
  968. tasklet_schedule(&trans_pcie->irq_tasklet);
  969. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  970. !trans_pcie->inta)
  971. iwl_enable_interrupts(trans);
  972. none:
  973. /* re-enable interrupts here since we don't have anything to service. */
  974. /* only Re-enable if disabled by irq and no schedules tasklet. */
  975. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  976. !trans_pcie->inta)
  977. iwl_enable_interrupts(trans);
  978. return IRQ_NONE;
  979. }
  980. /* interrupt handler using ict table, with this interrupt driver will
  981. * stop using INTA register to get device's interrupt, reading this register
  982. * is expensive, device will write interrupts in ICT dram table, increment
  983. * index then will fire interrupt to driver, driver will OR all ICT table
  984. * entries from current index up to table entry with 0 value. the result is
  985. * the interrupt we need to service, driver will set the entries back to 0 and
  986. * set index.
  987. */
  988. irqreturn_t iwl_pcie_isr_ict(int irq, void *data)
  989. {
  990. struct iwl_trans *trans = data;
  991. struct iwl_trans_pcie *trans_pcie;
  992. u32 inta, inta_mask;
  993. u32 val = 0;
  994. u32 read;
  995. unsigned long flags;
  996. if (!trans)
  997. return IRQ_NONE;
  998. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  999. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1000. /* dram interrupt table not set yet,
  1001. * use legacy interrupt.
  1002. */
  1003. if (unlikely(!trans_pcie->use_ict)) {
  1004. irqreturn_t ret = iwl_pcie_isr(irq, data);
  1005. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1006. return ret;
  1007. }
  1008. trace_iwlwifi_dev_irq(trans->dev);
  1009. /* Disable (but don't clear!) interrupts here to avoid
  1010. * back-to-back ISRs and sporadic interrupts from our NIC.
  1011. * If we have something to service, the tasklet will re-enable ints.
  1012. * If we *don't* have something, we'll re-enable before leaving here.
  1013. */
  1014. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  1015. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1016. /* Ignore interrupt if there's nothing in NIC to service.
  1017. * This may be due to IRQ shared with another device,
  1018. * or due to sporadic interrupts thrown from our NIC. */
  1019. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1020. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  1021. if (!read) {
  1022. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1023. goto none;
  1024. }
  1025. /*
  1026. * Collect all entries up to the first 0, starting from ict_index;
  1027. * note we already read at ict_index.
  1028. */
  1029. do {
  1030. val |= read;
  1031. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1032. trans_pcie->ict_index, read);
  1033. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1034. trans_pcie->ict_index =
  1035. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  1036. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1037. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1038. read);
  1039. } while (read);
  1040. /* We should not get this value, just ignore it. */
  1041. if (val == 0xffffffff)
  1042. val = 0;
  1043. /*
  1044. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1045. * (bit 15 before shifting it to 31) to clear when using interrupt
  1046. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1047. * so we use them to decide on the real state of the Rx bit.
  1048. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1049. */
  1050. if (val & 0xC0000)
  1051. val |= 0x8000;
  1052. inta = (0xff & val) | ((0xff00 & val) << 16);
  1053. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  1054. inta, inta_mask, val);
  1055. inta &= trans_pcie->inta_mask;
  1056. trans_pcie->inta |= inta;
  1057. /* iwl_pcie_tasklet() will service interrupts and re-enable them */
  1058. if (likely(inta))
  1059. tasklet_schedule(&trans_pcie->irq_tasklet);
  1060. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1061. !trans_pcie->inta) {
  1062. /* Allow interrupt if was disabled by this handler and
  1063. * no tasklet was schedules, We should not enable interrupt,
  1064. * tasklet will enable it.
  1065. */
  1066. iwl_enable_interrupts(trans);
  1067. }
  1068. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1069. return IRQ_HANDLED;
  1070. none:
  1071. /* re-enable interrupts here since we don't have anything to service.
  1072. * only Re-enable if disabled by irq.
  1073. */
  1074. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1075. !trans_pcie->inta)
  1076. iwl_enable_interrupts(trans);
  1077. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1078. return IRQ_NONE;
  1079. }