rx.c 37 KB

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