rx.c 39 KB

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