rx.c 40 KB

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