rx.c 37 KB

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