iwl-trans-pcie-rx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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 "iwl-trans-pcie-int.h"
  35. #include "iwl-op-mode.h"
  36. #ifdef CONFIG_IWLWIFI_IDI
  37. #include "iwl-amfh.h"
  38. #endif
  39. /******************************************************************************
  40. *
  41. * RX path functions
  42. *
  43. ******************************************************************************/
  44. /*
  45. * Rx theory of operation
  46. *
  47. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  48. * each of which point to Receive Buffers to be filled by the NIC. These get
  49. * used not only for Rx frames, but for any command response or notification
  50. * from the NIC. The driver and NIC manage the Rx buffers by means
  51. * of indexes into the circular buffer.
  52. *
  53. * Rx Queue Indexes
  54. * The host/firmware share two index registers for managing the Rx buffers.
  55. *
  56. * The READ index maps to the first position that the firmware may be writing
  57. * to -- the driver can read up to (but not including) this position and get
  58. * good data.
  59. * The READ index is managed by the firmware once the card is enabled.
  60. *
  61. * The WRITE index maps to the last position the driver has read from -- the
  62. * position preceding WRITE is the last slot the firmware can place a packet.
  63. *
  64. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  65. * WRITE = READ.
  66. *
  67. * During initialization, the host sets up the READ queue position to the first
  68. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  69. *
  70. * When the firmware places a packet in a buffer, it will advance the READ index
  71. * and fire the RX interrupt. The driver can then query the READ index and
  72. * process as many packets as possible, moving the WRITE index forward as it
  73. * resets the Rx queue buffers with new memory.
  74. *
  75. * The management in the driver is as follows:
  76. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  77. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  78. * to replenish the iwl->rxq->rx_free.
  79. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  80. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  81. * 'processed' and 'read' driver indexes as well)
  82. * + A received packet is processed and handed to the kernel network stack,
  83. * detached from the iwl->rxq. The driver 'processed' index is updated.
  84. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  85. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  86. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  87. * were enough free buffers and RX_STALLED is set it is cleared.
  88. *
  89. *
  90. * Driver sequence:
  91. *
  92. * iwl_rx_queue_alloc() Allocates rx_free
  93. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  94. * iwl_rx_queue_restock
  95. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  96. * queue, updates firmware pointers, and updates
  97. * the WRITE index. If insufficient rx_free buffers
  98. * are available, schedules iwl_rx_replenish
  99. *
  100. * -- enable interrupts --
  101. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  102. * READ INDEX, detaching the SKB from the pool.
  103. * Moves the packet buffer from queue to rx_used.
  104. * Calls iwl_rx_queue_restock to refill any empty
  105. * slots.
  106. * ...
  107. *
  108. */
  109. /**
  110. * iwl_rx_queue_space - Return number of free slots available in queue.
  111. */
  112. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  113. {
  114. int s = q->read - q->write;
  115. if (s <= 0)
  116. s += RX_QUEUE_SIZE;
  117. /* keep some buffer to not confuse full and empty queue */
  118. s -= 2;
  119. if (s < 0)
  120. s = 0;
  121. return s;
  122. }
  123. /**
  124. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  125. */
  126. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  127. struct iwl_rx_queue *q)
  128. {
  129. unsigned long flags;
  130. u32 reg;
  131. spin_lock_irqsave(&q->lock, flags);
  132. if (q->need_update == 0)
  133. goto exit_unlock;
  134. if (cfg(trans)->base_params->shadow_reg_enable) {
  135. /* shadow register enabled */
  136. /* Device expects a multiple of 8 */
  137. q->write_actual = (q->write & ~0x7);
  138. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
  139. } else {
  140. /* If power-saving is in use, make sure device is awake */
  141. if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
  142. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  143. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  144. IWL_DEBUG_INFO(trans,
  145. "Rx queue requesting wakeup,"
  146. " GP1 = 0x%x\n", reg);
  147. iwl_set_bit(trans, CSR_GP_CNTRL,
  148. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  149. goto exit_unlock;
  150. }
  151. q->write_actual = (q->write & ~0x7);
  152. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  153. q->write_actual);
  154. /* Else device is assumed to be awake */
  155. } else {
  156. /* Device expects a multiple of 8 */
  157. q->write_actual = (q->write & ~0x7);
  158. iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
  159. q->write_actual);
  160. }
  161. }
  162. q->need_update = 0;
  163. exit_unlock:
  164. spin_unlock_irqrestore(&q->lock, flags);
  165. }
  166. /**
  167. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  168. */
  169. static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  170. {
  171. return cpu_to_le32((u32)(dma_addr >> 8));
  172. }
  173. /**
  174. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  175. *
  176. * If there are slots in the RX queue that need to be restocked,
  177. * and we have free pre-allocated buffers, fill the ranks as much
  178. * as we can, pulling from rx_free.
  179. *
  180. * This moves the 'write' index forward to catch up with 'processed', and
  181. * also updates the memory address in the firmware to reference the new
  182. * target buffer.
  183. */
  184. static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
  185. {
  186. struct iwl_trans_pcie *trans_pcie =
  187. IWL_TRANS_GET_PCIE_TRANS(trans);
  188. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  189. struct list_head *element;
  190. struct iwl_rx_mem_buffer *rxb;
  191. unsigned long flags;
  192. spin_lock_irqsave(&rxq->lock, flags);
  193. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  194. /* The overwritten rxb must be a used one */
  195. rxb = rxq->queue[rxq->write];
  196. BUG_ON(rxb && rxb->page);
  197. /* Get next free Rx buffer, remove from free list */
  198. element = rxq->rx_free.next;
  199. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  200. list_del(element);
  201. /* Point to Rx buffer via next RBD in circular buffer */
  202. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
  203. rxq->queue[rxq->write] = rxb;
  204. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  205. rxq->free_count--;
  206. }
  207. spin_unlock_irqrestore(&rxq->lock, flags);
  208. /* If the pre-allocated buffer pool is dropping low, schedule to
  209. * refill it */
  210. if (rxq->free_count <= RX_LOW_WATERMARK)
  211. schedule_work(&trans_pcie->rx_replenish);
  212. /* If we've added more space for the firmware to place data, tell it.
  213. * Increment device's write pointer in multiples of 8. */
  214. if (rxq->write_actual != (rxq->write & ~0x7)) {
  215. spin_lock_irqsave(&rxq->lock, flags);
  216. rxq->need_update = 1;
  217. spin_unlock_irqrestore(&rxq->lock, flags);
  218. iwl_rx_queue_update_write_ptr(trans, rxq);
  219. }
  220. }
  221. /**
  222. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  223. *
  224. * When moving to rx_free an SKB is allocated for the slot.
  225. *
  226. * Also restock the Rx queue via iwl_rx_queue_restock.
  227. * This is called as a scheduled work item (except for during initialization)
  228. */
  229. static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
  230. {
  231. struct iwl_trans_pcie *trans_pcie =
  232. IWL_TRANS_GET_PCIE_TRANS(trans);
  233. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  234. struct list_head *element;
  235. struct iwl_rx_mem_buffer *rxb;
  236. struct page *page;
  237. unsigned long flags;
  238. gfp_t gfp_mask = priority;
  239. while (1) {
  240. spin_lock_irqsave(&rxq->lock, flags);
  241. if (list_empty(&rxq->rx_used)) {
  242. spin_unlock_irqrestore(&rxq->lock, flags);
  243. return;
  244. }
  245. spin_unlock_irqrestore(&rxq->lock, flags);
  246. if (rxq->free_count > RX_LOW_WATERMARK)
  247. gfp_mask |= __GFP_NOWARN;
  248. if (hw_params(trans).rx_page_order > 0)
  249. gfp_mask |= __GFP_COMP;
  250. /* Alloc a new receive buffer */
  251. page = alloc_pages(gfp_mask,
  252. hw_params(trans).rx_page_order);
  253. if (!page) {
  254. if (net_ratelimit())
  255. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  256. "order: %d\n",
  257. hw_params(trans).rx_page_order);
  258. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  259. net_ratelimit())
  260. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  261. "Only %u free buffers remaining.\n",
  262. priority == GFP_ATOMIC ?
  263. "GFP_ATOMIC" : "GFP_KERNEL",
  264. rxq->free_count);
  265. /* We don't reschedule replenish work here -- we will
  266. * call the restock method and if it still needs
  267. * more buffers it will schedule replenish */
  268. return;
  269. }
  270. spin_lock_irqsave(&rxq->lock, flags);
  271. if (list_empty(&rxq->rx_used)) {
  272. spin_unlock_irqrestore(&rxq->lock, flags);
  273. __free_pages(page, hw_params(trans).rx_page_order);
  274. return;
  275. }
  276. element = rxq->rx_used.next;
  277. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  278. list_del(element);
  279. spin_unlock_irqrestore(&rxq->lock, flags);
  280. BUG_ON(rxb->page);
  281. rxb->page = page;
  282. /* Get physical address of the RB */
  283. rxb->page_dma = dma_map_page(trans->dev, page, 0,
  284. PAGE_SIZE << hw_params(trans).rx_page_order,
  285. DMA_FROM_DEVICE);
  286. /* dma address must be no more than 36 bits */
  287. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  288. /* and also 256 byte aligned! */
  289. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  290. spin_lock_irqsave(&rxq->lock, flags);
  291. list_add_tail(&rxb->list, &rxq->rx_free);
  292. rxq->free_count++;
  293. spin_unlock_irqrestore(&rxq->lock, flags);
  294. }
  295. }
  296. void iwlagn_rx_replenish(struct iwl_trans *trans)
  297. {
  298. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  299. unsigned long flags;
  300. iwlagn_rx_allocate(trans, GFP_KERNEL);
  301. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  302. iwlagn_rx_queue_restock(trans);
  303. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  304. }
  305. static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
  306. {
  307. iwlagn_rx_allocate(trans, GFP_ATOMIC);
  308. iwlagn_rx_queue_restock(trans);
  309. }
  310. void iwl_bg_rx_replenish(struct work_struct *data)
  311. {
  312. struct iwl_trans_pcie *trans_pcie =
  313. container_of(data, struct iwl_trans_pcie, rx_replenish);
  314. iwlagn_rx_replenish(trans_pcie->trans);
  315. }
  316. static void iwl_rx_handle_rxbuf(struct iwl_trans *trans,
  317. struct iwl_rx_mem_buffer *rxb)
  318. {
  319. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  320. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  321. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  322. unsigned long flags;
  323. bool page_stolen = false;
  324. int max_len = PAGE_SIZE << hw_params(trans).rx_page_order;
  325. u32 offset = 0;
  326. if (WARN_ON(!rxb))
  327. return;
  328. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  329. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  330. struct iwl_rx_packet *pkt;
  331. struct iwl_device_cmd *cmd;
  332. u16 sequence;
  333. bool reclaim;
  334. int index, cmd_index, err, len;
  335. struct iwl_rx_cmd_buffer rxcb = {
  336. ._offset = offset,
  337. ._page = rxb->page,
  338. ._page_stolen = false,
  339. };
  340. pkt = rxb_addr(&rxcb);
  341. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  342. break;
  343. IWL_DEBUG_RX(trans, "cmd at offset %d: %s (0x%.2x)\n",
  344. rxcb._offset, get_cmd_string(pkt->hdr.cmd),
  345. pkt->hdr.cmd);
  346. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  347. len += sizeof(u32); /* account for status word */
  348. trace_iwlwifi_dev_rx(trans->dev, pkt, len);
  349. /* Reclaim a command buffer only if this packet is a response
  350. * to a (driver-originated) command.
  351. * If the packet (e.g. Rx frame) originated from uCode,
  352. * there is no command buffer to reclaim.
  353. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  354. * but apparently a few don't get set; catch them here. */
  355. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  356. if (reclaim) {
  357. int i;
  358. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  359. if (trans_pcie->no_reclaim_cmds[i] ==
  360. pkt->hdr.cmd) {
  361. reclaim = false;
  362. break;
  363. }
  364. }
  365. }
  366. sequence = le16_to_cpu(pkt->hdr.sequence);
  367. index = SEQ_TO_INDEX(sequence);
  368. cmd_index = get_cmd_index(&txq->q, index);
  369. if (reclaim)
  370. cmd = txq->cmd[cmd_index];
  371. else
  372. cmd = NULL;
  373. err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
  374. /*
  375. * After here, we should always check rxcb._page_stolen,
  376. * if it is true then one of the handlers took the page.
  377. */
  378. if (reclaim) {
  379. /* Invoke any callbacks, transfer the buffer to caller,
  380. * and fire off the (possibly) blocking
  381. * iwl_trans_send_cmd()
  382. * as we reclaim the driver command queue */
  383. if (!rxcb._page_stolen)
  384. iwl_tx_cmd_complete(trans, &rxcb, err);
  385. else
  386. IWL_WARN(trans, "Claim null rxb?\n");
  387. }
  388. page_stolen |= rxcb._page_stolen;
  389. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  390. }
  391. /* page was stolen from us -- free our reference */
  392. if (page_stolen) {
  393. __free_pages(rxb->page, hw_params(trans).rx_page_order);
  394. rxb->page = NULL;
  395. }
  396. /* Reuse the page if possible. For notification packets and
  397. * SKBs that fail to Rx correctly, add them back into the
  398. * rx_free list for reuse later. */
  399. spin_lock_irqsave(&rxq->lock, flags);
  400. if (rxb->page != NULL) {
  401. rxb->page_dma =
  402. dma_map_page(trans->dev, rxb->page, 0,
  403. PAGE_SIZE << hw_params(trans).rx_page_order,
  404. DMA_FROM_DEVICE);
  405. list_add_tail(&rxb->list, &rxq->rx_free);
  406. rxq->free_count++;
  407. } else
  408. list_add_tail(&rxb->list, &rxq->rx_used);
  409. spin_unlock_irqrestore(&rxq->lock, flags);
  410. }
  411. /**
  412. * iwl_rx_handle - Main entry function for receiving responses from uCode
  413. *
  414. * Uses the priv->rx_handlers callback function array to invoke
  415. * the appropriate handlers, including command responses,
  416. * frame-received notifications, and other notifications.
  417. */
  418. static void iwl_rx_handle(struct iwl_trans *trans)
  419. {
  420. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  421. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  422. u32 r, i;
  423. u8 fill_rx = 0;
  424. u32 count = 8;
  425. int total_empty;
  426. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  427. * buffer that the driver may process (last buffer filled by ucode). */
  428. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  429. i = rxq->read;
  430. /* Rx interrupt, but nothing sent from uCode */
  431. if (i == r)
  432. IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
  433. /* calculate total frames need to be restock after handling RX */
  434. total_empty = r - rxq->write_actual;
  435. if (total_empty < 0)
  436. total_empty += RX_QUEUE_SIZE;
  437. if (total_empty > (RX_QUEUE_SIZE / 2))
  438. fill_rx = 1;
  439. while (i != r) {
  440. struct iwl_rx_mem_buffer *rxb;
  441. rxb = rxq->queue[i];
  442. rxq->queue[i] = NULL;
  443. IWL_DEBUG_RX(trans, "rxbuf: r = %d, i = %d (%p)\n", rxb);
  444. iwl_rx_handle_rxbuf(trans, rxb);
  445. i = (i + 1) & RX_QUEUE_MASK;
  446. /* If there are a lot of unused frames,
  447. * restock the Rx queue so ucode wont assert. */
  448. if (fill_rx) {
  449. count++;
  450. if (count >= 8) {
  451. rxq->read = i;
  452. iwlagn_rx_replenish_now(trans);
  453. count = 0;
  454. }
  455. }
  456. }
  457. /* Backtrack one entry */
  458. rxq->read = i;
  459. if (fill_rx)
  460. iwlagn_rx_replenish_now(trans);
  461. else
  462. iwlagn_rx_queue_restock(trans);
  463. }
  464. static const char * const desc_lookup_text[] = {
  465. "OK",
  466. "FAIL",
  467. "BAD_PARAM",
  468. "BAD_CHECKSUM",
  469. "NMI_INTERRUPT_WDG",
  470. "SYSASSERT",
  471. "FATAL_ERROR",
  472. "BAD_COMMAND",
  473. "HW_ERROR_TUNE_LOCK",
  474. "HW_ERROR_TEMPERATURE",
  475. "ILLEGAL_CHAN_FREQ",
  476. "VCC_NOT_STABLE",
  477. "FH_ERROR",
  478. "NMI_INTERRUPT_HOST",
  479. "NMI_INTERRUPT_ACTION_PT",
  480. "NMI_INTERRUPT_UNKNOWN",
  481. "UCODE_VERSION_MISMATCH",
  482. "HW_ERROR_ABS_LOCK",
  483. "HW_ERROR_CAL_LOCK_FAIL",
  484. "NMI_INTERRUPT_INST_ACTION_PT",
  485. "NMI_INTERRUPT_DATA_ACTION_PT",
  486. "NMI_TRM_HW_ER",
  487. "NMI_INTERRUPT_TRM",
  488. "NMI_INTERRUPT_BREAK_POINT",
  489. "DEBUG_0",
  490. "DEBUG_1",
  491. "DEBUG_2",
  492. "DEBUG_3",
  493. };
  494. static struct { char *name; u8 num; } advanced_lookup[] = {
  495. { "NMI_INTERRUPT_WDG", 0x34 },
  496. { "SYSASSERT", 0x35 },
  497. { "UCODE_VERSION_MISMATCH", 0x37 },
  498. { "BAD_COMMAND", 0x38 },
  499. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  500. { "FATAL_ERROR", 0x3D },
  501. { "NMI_TRM_HW_ERR", 0x46 },
  502. { "NMI_INTERRUPT_TRM", 0x4C },
  503. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  504. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  505. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  506. { "NMI_INTERRUPT_HOST", 0x66 },
  507. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  508. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  509. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  510. { "ADVANCED_SYSASSERT", 0 },
  511. };
  512. static const char *desc_lookup(u32 num)
  513. {
  514. int i;
  515. int max = ARRAY_SIZE(desc_lookup_text);
  516. if (num < max)
  517. return desc_lookup_text[num];
  518. max = ARRAY_SIZE(advanced_lookup) - 1;
  519. for (i = 0; i < max; i++) {
  520. if (advanced_lookup[i].num == num)
  521. break;
  522. }
  523. return advanced_lookup[i].name;
  524. }
  525. #define ERROR_START_OFFSET (1 * sizeof(u32))
  526. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  527. static void iwl_dump_nic_error_log(struct iwl_trans *trans)
  528. {
  529. u32 base;
  530. struct iwl_error_event_table table;
  531. struct iwl_trans_pcie *trans_pcie =
  532. IWL_TRANS_GET_PCIE_TRANS(trans);
  533. base = trans->shrd->device_pointers.error_event_table;
  534. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  535. if (!base)
  536. base = trans->shrd->fw->init_errlog_ptr;
  537. } else {
  538. if (!base)
  539. base = trans->shrd->fw->inst_errlog_ptr;
  540. }
  541. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  542. IWL_ERR(trans,
  543. "Not valid error log pointer 0x%08X for %s uCode\n",
  544. base,
  545. (trans->shrd->ucode_type == IWL_UCODE_INIT)
  546. ? "Init" : "RT");
  547. return;
  548. }
  549. iwl_read_targ_mem_words(trans, base, &table, sizeof(table));
  550. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  551. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  552. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  553. trans->shrd->status, table.valid);
  554. }
  555. trans_pcie->isr_stats.err_code = table.error_id;
  556. trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
  557. table.data1, table.data2, table.line,
  558. table.blink1, table.blink2, table.ilink1,
  559. table.ilink2, table.bcon_time, table.gp1,
  560. table.gp2, table.gp3, table.ucode_ver,
  561. table.hw_ver, table.brd_ver);
  562. IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
  563. desc_lookup(table.error_id));
  564. IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
  565. IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
  566. IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
  567. IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
  568. IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
  569. IWL_ERR(trans, "0x%08X | data1\n", table.data1);
  570. IWL_ERR(trans, "0x%08X | data2\n", table.data2);
  571. IWL_ERR(trans, "0x%08X | line\n", table.line);
  572. IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
  573. IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
  574. IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
  575. IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
  576. IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
  577. IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
  578. IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
  579. IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
  580. IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
  581. IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
  582. IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
  583. IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
  584. IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
  585. IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
  586. IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
  587. IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
  588. IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
  589. IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
  590. IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
  591. IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
  592. IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
  593. IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
  594. IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
  595. IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
  596. }
  597. /**
  598. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  599. */
  600. static void iwl_irq_handle_error(struct iwl_trans *trans)
  601. {
  602. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  603. if (cfg(trans)->internal_wimax_coex &&
  604. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  605. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  606. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  607. APMG_PS_CTRL_VAL_RESET_REQ))) {
  608. /*
  609. * Keep the restart process from trying to send host
  610. * commands by clearing the ready bit.
  611. */
  612. clear_bit(STATUS_READY, &trans->shrd->status);
  613. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  614. wake_up(&trans->wait_command_queue);
  615. IWL_ERR(trans, "RF is used by WiMAX\n");
  616. return;
  617. }
  618. IWL_ERR(trans, "Loaded firmware version: %s\n",
  619. trans->shrd->fw->fw_version);
  620. iwl_dump_nic_error_log(trans);
  621. iwl_dump_csr(trans);
  622. iwl_dump_fh(trans, NULL, false);
  623. iwl_dump_nic_event_log(trans, false, NULL, false);
  624. iwl_op_mode_nic_error(trans->op_mode);
  625. }
  626. #define EVENT_START_OFFSET (4 * sizeof(u32))
  627. /**
  628. * iwl_print_event_log - Dump error event log to syslog
  629. *
  630. */
  631. static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
  632. u32 num_events, u32 mode,
  633. int pos, char **buf, size_t bufsz)
  634. {
  635. u32 i;
  636. u32 base; /* SRAM byte address of event log header */
  637. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  638. u32 ptr; /* SRAM byte address of log data */
  639. u32 ev, time, data; /* event log data */
  640. unsigned long reg_flags;
  641. if (num_events == 0)
  642. return pos;
  643. base = trans->shrd->device_pointers.log_event_table;
  644. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  645. if (!base)
  646. base = trans->shrd->fw->init_evtlog_ptr;
  647. } else {
  648. if (!base)
  649. base = trans->shrd->fw->inst_evtlog_ptr;
  650. }
  651. if (mode == 0)
  652. event_size = 2 * sizeof(u32);
  653. else
  654. event_size = 3 * sizeof(u32);
  655. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  656. /* Make sure device is powered up for SRAM reads */
  657. spin_lock_irqsave(&trans->reg_lock, reg_flags);
  658. if (unlikely(!iwl_grab_nic_access(trans)))
  659. goto out_unlock;
  660. /* Set starting address; reads will auto-increment */
  661. iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
  662. /* "time" is actually "data" for mode 0 (no timestamp).
  663. * place event id # at far right for easier visual parsing. */
  664. for (i = 0; i < num_events; i++) {
  665. ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  666. time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  667. if (mode == 0) {
  668. /* data, ev */
  669. if (bufsz) {
  670. pos += scnprintf(*buf + pos, bufsz - pos,
  671. "EVT_LOG:0x%08x:%04u\n",
  672. time, ev);
  673. } else {
  674. trace_iwlwifi_dev_ucode_event(trans->dev, 0,
  675. time, ev);
  676. IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
  677. time, ev);
  678. }
  679. } else {
  680. data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  681. if (bufsz) {
  682. pos += scnprintf(*buf + pos, bufsz - pos,
  683. "EVT_LOGT:%010u:0x%08x:%04u\n",
  684. time, data, ev);
  685. } else {
  686. IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
  687. time, data, ev);
  688. trace_iwlwifi_dev_ucode_event(trans->dev, time,
  689. data, ev);
  690. }
  691. }
  692. }
  693. /* Allow device to power down */
  694. iwl_release_nic_access(trans);
  695. out_unlock:
  696. spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
  697. return pos;
  698. }
  699. /**
  700. * iwl_print_last_event_logs - Dump the newest # of event log to syslog
  701. */
  702. static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
  703. u32 num_wraps, u32 next_entry,
  704. u32 size, u32 mode,
  705. int pos, char **buf, size_t bufsz)
  706. {
  707. /*
  708. * display the newest DEFAULT_LOG_ENTRIES entries
  709. * i.e the entries just before the next ont that uCode would fill.
  710. */
  711. if (num_wraps) {
  712. if (next_entry < size) {
  713. pos = iwl_print_event_log(trans,
  714. capacity - (size - next_entry),
  715. size - next_entry, mode,
  716. pos, buf, bufsz);
  717. pos = iwl_print_event_log(trans, 0,
  718. next_entry, mode,
  719. pos, buf, bufsz);
  720. } else
  721. pos = iwl_print_event_log(trans, next_entry - size,
  722. size, mode, pos, buf, bufsz);
  723. } else {
  724. if (next_entry < size) {
  725. pos = iwl_print_event_log(trans, 0, next_entry,
  726. mode, pos, buf, bufsz);
  727. } else {
  728. pos = iwl_print_event_log(trans, next_entry - size,
  729. size, mode, pos, buf, bufsz);
  730. }
  731. }
  732. return pos;
  733. }
  734. #define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
  735. int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
  736. char **buf, bool display)
  737. {
  738. u32 base; /* SRAM byte address of event log header */
  739. u32 capacity; /* event log capacity in # entries */
  740. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  741. u32 num_wraps; /* # times uCode wrapped to top of log */
  742. u32 next_entry; /* index of next entry to be written by uCode */
  743. u32 size; /* # entries that we'll print */
  744. u32 logsize;
  745. int pos = 0;
  746. size_t bufsz = 0;
  747. base = trans->shrd->device_pointers.log_event_table;
  748. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  749. logsize = trans->shrd->fw->init_evtlog_size;
  750. if (!base)
  751. base = trans->shrd->fw->init_evtlog_ptr;
  752. } else {
  753. logsize = trans->shrd->fw->inst_evtlog_size;
  754. if (!base)
  755. base = trans->shrd->fw->inst_evtlog_ptr;
  756. }
  757. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  758. IWL_ERR(trans,
  759. "Invalid event log pointer 0x%08X for %s uCode\n",
  760. base,
  761. (trans->shrd->ucode_type == IWL_UCODE_INIT)
  762. ? "Init" : "RT");
  763. return -EINVAL;
  764. }
  765. /* event log header */
  766. capacity = iwl_read_targ_mem(trans, base);
  767. mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
  768. num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
  769. next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
  770. if (capacity > logsize) {
  771. IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
  772. "entries\n", capacity, logsize);
  773. capacity = logsize;
  774. }
  775. if (next_entry > logsize) {
  776. IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
  777. next_entry, logsize);
  778. next_entry = logsize;
  779. }
  780. size = num_wraps ? capacity : next_entry;
  781. /* bail out if nothing in log */
  782. if (size == 0) {
  783. IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
  784. return pos;
  785. }
  786. #ifdef CONFIG_IWLWIFI_DEBUG
  787. if (!(iwl_have_debug_level(IWL_DL_FW_ERRORS)) && !full_log)
  788. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  789. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  790. #else
  791. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  792. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  793. #endif
  794. IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
  795. size);
  796. #ifdef CONFIG_IWLWIFI_DEBUG
  797. if (display) {
  798. if (full_log)
  799. bufsz = capacity * 48;
  800. else
  801. bufsz = size * 48;
  802. *buf = kmalloc(bufsz, GFP_KERNEL);
  803. if (!*buf)
  804. return -ENOMEM;
  805. }
  806. if (iwl_have_debug_level(IWL_DL_FW_ERRORS) || full_log) {
  807. /*
  808. * if uCode has wrapped back to top of log,
  809. * start at the oldest entry,
  810. * i.e the next one that uCode would fill.
  811. */
  812. if (num_wraps)
  813. pos = iwl_print_event_log(trans, next_entry,
  814. capacity - next_entry, mode,
  815. pos, buf, bufsz);
  816. /* (then/else) start at top of log */
  817. pos = iwl_print_event_log(trans, 0,
  818. next_entry, mode, pos, buf, bufsz);
  819. } else
  820. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  821. next_entry, size, mode,
  822. pos, buf, bufsz);
  823. #else
  824. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  825. next_entry, size, mode,
  826. pos, buf, bufsz);
  827. #endif
  828. return pos;
  829. }
  830. /* tasklet for iwlagn interrupt */
  831. void iwl_irq_tasklet(struct iwl_trans *trans)
  832. {
  833. u32 inta = 0;
  834. u32 handled = 0;
  835. unsigned long flags;
  836. u32 i;
  837. #ifdef CONFIG_IWLWIFI_DEBUG
  838. u32 inta_mask;
  839. #endif
  840. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  841. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  842. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  843. /* Ack/clear/reset pending uCode interrupts.
  844. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  845. */
  846. /* There is a hardware bug in the interrupt mask function that some
  847. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  848. * they are disabled in the CSR_INT_MASK register. Furthermore the
  849. * ICT interrupt handling mechanism has another bug that might cause
  850. * these unmasked interrupts fail to be detected. We workaround the
  851. * hardware bugs here by ACKing all the possible interrupts so that
  852. * interrupt coalescing can still be achieved.
  853. */
  854. iwl_write32(trans, CSR_INT,
  855. trans_pcie->inta | ~trans_pcie->inta_mask);
  856. inta = trans_pcie->inta;
  857. #ifdef CONFIG_IWLWIFI_DEBUG
  858. if (iwl_have_debug_level(IWL_DL_ISR)) {
  859. /* just for debug */
  860. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  861. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
  862. inta, inta_mask);
  863. }
  864. #endif
  865. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  866. trans_pcie->inta = 0;
  867. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  868. /* Now service all interrupt bits discovered above. */
  869. if (inta & CSR_INT_BIT_HW_ERR) {
  870. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  871. /* Tell the device to stop sending interrupts */
  872. iwl_disable_interrupts(trans);
  873. isr_stats->hw++;
  874. iwl_irq_handle_error(trans);
  875. handled |= CSR_INT_BIT_HW_ERR;
  876. return;
  877. }
  878. #ifdef CONFIG_IWLWIFI_DEBUG
  879. if (iwl_have_debug_level(IWL_DL_ISR)) {
  880. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  881. if (inta & CSR_INT_BIT_SCD) {
  882. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  883. "the frame/frames.\n");
  884. isr_stats->sch++;
  885. }
  886. /* Alive notification via Rx interrupt will do the real work */
  887. if (inta & CSR_INT_BIT_ALIVE) {
  888. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  889. isr_stats->alive++;
  890. }
  891. }
  892. #endif
  893. /* Safely ignore these bits for debug checks below */
  894. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  895. /* HW RF KILL switch toggled */
  896. if (inta & CSR_INT_BIT_RF_KILL) {
  897. bool hw_rfkill;
  898. hw_rfkill = !(iwl_read32(trans, CSR_GP_CNTRL) &
  899. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  900. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  901. hw_rfkill ? "disable radio" : "enable radio");
  902. isr_stats->rfkill++;
  903. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  904. handled |= CSR_INT_BIT_RF_KILL;
  905. }
  906. /* Chip got too hot and stopped itself */
  907. if (inta & CSR_INT_BIT_CT_KILL) {
  908. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  909. isr_stats->ctkill++;
  910. handled |= CSR_INT_BIT_CT_KILL;
  911. }
  912. /* Error detected by uCode */
  913. if (inta & CSR_INT_BIT_SW_ERR) {
  914. IWL_ERR(trans, "Microcode SW error detected. "
  915. " Restarting 0x%X.\n", inta);
  916. isr_stats->sw++;
  917. iwl_irq_handle_error(trans);
  918. handled |= CSR_INT_BIT_SW_ERR;
  919. }
  920. /* uCode wakes up after power-down sleep */
  921. if (inta & CSR_INT_BIT_WAKEUP) {
  922. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  923. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  924. for (i = 0; i < cfg(trans)->base_params->num_of_queues; i++)
  925. iwl_txq_update_write_ptr(trans,
  926. &trans_pcie->txq[i]);
  927. isr_stats->wakeup++;
  928. handled |= CSR_INT_BIT_WAKEUP;
  929. }
  930. /* All uCode command responses, including Tx command responses,
  931. * Rx "responses" (frame-received notification), and other
  932. * notifications from uCode come through here*/
  933. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  934. CSR_INT_BIT_RX_PERIODIC)) {
  935. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  936. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  937. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  938. iwl_write32(trans, CSR_FH_INT_STATUS,
  939. CSR_FH_INT_RX_MASK);
  940. }
  941. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  942. handled |= CSR_INT_BIT_RX_PERIODIC;
  943. iwl_write32(trans,
  944. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  945. }
  946. /* Sending RX interrupt require many steps to be done in the
  947. * the device:
  948. * 1- write interrupt to current index in ICT table.
  949. * 2- dma RX frame.
  950. * 3- update RX shared data to indicate last write index.
  951. * 4- send interrupt.
  952. * This could lead to RX race, driver could receive RX interrupt
  953. * but the shared data changes does not reflect this;
  954. * periodic interrupt will detect any dangling Rx activity.
  955. */
  956. /* Disable periodic interrupt; we use it as just a one-shot. */
  957. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  958. CSR_INT_PERIODIC_DIS);
  959. #ifdef CONFIG_IWLWIFI_IDI
  960. iwl_amfh_rx_handler();
  961. #else
  962. iwl_rx_handle(trans);
  963. #endif
  964. /*
  965. * Enable periodic interrupt in 8 msec only if we received
  966. * real RX interrupt (instead of just periodic int), to catch
  967. * any dangling Rx interrupt. If it was just the periodic
  968. * interrupt, there was no dangling Rx activity, and no need
  969. * to extend the periodic interrupt; one-shot is enough.
  970. */
  971. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  972. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  973. CSR_INT_PERIODIC_ENA);
  974. isr_stats->rx++;
  975. }
  976. /* This "Tx" DMA channel is used only for loading uCode */
  977. if (inta & CSR_INT_BIT_FH_TX) {
  978. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  979. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  980. isr_stats->tx++;
  981. handled |= CSR_INT_BIT_FH_TX;
  982. /* Wake up uCode load routine, now that load is complete */
  983. trans_pcie->ucode_write_complete = true;
  984. wake_up(&trans_pcie->ucode_write_waitq);
  985. }
  986. if (inta & ~handled) {
  987. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  988. isr_stats->unhandled++;
  989. }
  990. if (inta & ~(trans_pcie->inta_mask)) {
  991. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  992. inta & ~trans_pcie->inta_mask);
  993. }
  994. /* Re-enable all interrupts */
  995. /* only Re-enable if disabled by irq */
  996. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  997. iwl_enable_interrupts(trans);
  998. /* Re-enable RF_KILL if it occurred */
  999. else if (handled & CSR_INT_BIT_RF_KILL)
  1000. iwl_enable_rfkill_int(trans);
  1001. }
  1002. /******************************************************************************
  1003. *
  1004. * ICT functions
  1005. *
  1006. ******************************************************************************/
  1007. /* a device (PCI-E) page is 4096 bytes long */
  1008. #define ICT_SHIFT 12
  1009. #define ICT_SIZE (1 << ICT_SHIFT)
  1010. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  1011. /* Free dram table */
  1012. void iwl_free_isr_ict(struct iwl_trans *trans)
  1013. {
  1014. struct iwl_trans_pcie *trans_pcie =
  1015. IWL_TRANS_GET_PCIE_TRANS(trans);
  1016. if (trans_pcie->ict_tbl) {
  1017. dma_free_coherent(trans->dev, ICT_SIZE,
  1018. trans_pcie->ict_tbl,
  1019. trans_pcie->ict_tbl_dma);
  1020. trans_pcie->ict_tbl = NULL;
  1021. trans_pcie->ict_tbl_dma = 0;
  1022. }
  1023. }
  1024. /*
  1025. * allocate dram shared table, it is an aligned memory
  1026. * block of ICT_SIZE.
  1027. * also reset all data related to ICT table interrupt.
  1028. */
  1029. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  1030. {
  1031. struct iwl_trans_pcie *trans_pcie =
  1032. IWL_TRANS_GET_PCIE_TRANS(trans);
  1033. trans_pcie->ict_tbl =
  1034. dma_alloc_coherent(trans->dev, ICT_SIZE,
  1035. &trans_pcie->ict_tbl_dma,
  1036. GFP_KERNEL);
  1037. if (!trans_pcie->ict_tbl)
  1038. return -ENOMEM;
  1039. /* just an API sanity check ... it is guaranteed to be aligned */
  1040. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  1041. iwl_free_isr_ict(trans);
  1042. return -EINVAL;
  1043. }
  1044. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  1045. (unsigned long long)trans_pcie->ict_tbl_dma);
  1046. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  1047. /* reset table and index to all 0 */
  1048. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1049. trans_pcie->ict_index = 0;
  1050. /* add periodic RX interrupt */
  1051. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  1052. return 0;
  1053. }
  1054. /* Device is going up inform it about using ICT interrupt table,
  1055. * also we need to tell the driver to start using ICT interrupt.
  1056. */
  1057. void iwl_reset_ict(struct iwl_trans *trans)
  1058. {
  1059. u32 val;
  1060. unsigned long flags;
  1061. struct iwl_trans_pcie *trans_pcie =
  1062. IWL_TRANS_GET_PCIE_TRANS(trans);
  1063. if (!trans_pcie->ict_tbl)
  1064. return;
  1065. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1066. iwl_disable_interrupts(trans);
  1067. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1068. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  1069. val |= CSR_DRAM_INT_TBL_ENABLE;
  1070. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  1071. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  1072. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  1073. trans_pcie->use_ict = true;
  1074. trans_pcie->ict_index = 0;
  1075. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  1076. iwl_enable_interrupts(trans);
  1077. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1078. }
  1079. /* Device is going down disable ict interrupt usage */
  1080. void iwl_disable_ict(struct iwl_trans *trans)
  1081. {
  1082. struct iwl_trans_pcie *trans_pcie =
  1083. IWL_TRANS_GET_PCIE_TRANS(trans);
  1084. unsigned long flags;
  1085. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1086. trans_pcie->use_ict = false;
  1087. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1088. }
  1089. static irqreturn_t iwl_isr(int irq, void *data)
  1090. {
  1091. struct iwl_trans *trans = data;
  1092. struct iwl_trans_pcie *trans_pcie;
  1093. u32 inta, inta_mask;
  1094. unsigned long flags;
  1095. #ifdef CONFIG_IWLWIFI_DEBUG
  1096. u32 inta_fh;
  1097. #endif
  1098. if (!trans)
  1099. return IRQ_NONE;
  1100. trace_iwlwifi_dev_irq(trans->dev);
  1101. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1102. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1103. /* Disable (but don't clear!) interrupts here to avoid
  1104. * back-to-back ISRs and sporadic interrupts from our NIC.
  1105. * If we have something to service, the tasklet will re-enable ints.
  1106. * If we *don't* have something, we'll re-enable before leaving here. */
  1107. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  1108. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1109. /* Discover which interrupts are active/pending */
  1110. inta = iwl_read32(trans, CSR_INT);
  1111. /* Ignore interrupt if there's nothing in NIC to service.
  1112. * This may be due to IRQ shared with another device,
  1113. * or due to sporadic interrupts thrown from our NIC. */
  1114. if (!inta) {
  1115. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1116. goto none;
  1117. }
  1118. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1119. /* Hardware disappeared. It might have already raised
  1120. * an interrupt */
  1121. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1122. goto unplugged;
  1123. }
  1124. #ifdef CONFIG_IWLWIFI_DEBUG
  1125. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1126. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  1127. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  1128. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  1129. }
  1130. #endif
  1131. trans_pcie->inta |= inta;
  1132. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1133. if (likely(inta))
  1134. tasklet_schedule(&trans_pcie->irq_tasklet);
  1135. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1136. !trans_pcie->inta)
  1137. iwl_enable_interrupts(trans);
  1138. unplugged:
  1139. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1140. return IRQ_HANDLED;
  1141. none:
  1142. /* re-enable interrupts here since we don't have anything to service. */
  1143. /* only Re-enable if disabled by irq and no schedules tasklet. */
  1144. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1145. !trans_pcie->inta)
  1146. iwl_enable_interrupts(trans);
  1147. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1148. return IRQ_NONE;
  1149. }
  1150. /* interrupt handler using ict table, with this interrupt driver will
  1151. * stop using INTA register to get device's interrupt, reading this register
  1152. * is expensive, device will write interrupts in ICT dram table, increment
  1153. * index then will fire interrupt to driver, driver will OR all ICT table
  1154. * entries from current index up to table entry with 0 value. the result is
  1155. * the interrupt we need to service, driver will set the entries back to 0 and
  1156. * set index.
  1157. */
  1158. irqreturn_t iwl_isr_ict(int irq, void *data)
  1159. {
  1160. struct iwl_trans *trans = data;
  1161. struct iwl_trans_pcie *trans_pcie;
  1162. u32 inta, inta_mask;
  1163. u32 val = 0;
  1164. u32 read;
  1165. unsigned long flags;
  1166. if (!trans)
  1167. return IRQ_NONE;
  1168. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1169. /* dram interrupt table not set yet,
  1170. * use legacy interrupt.
  1171. */
  1172. if (!trans_pcie->use_ict)
  1173. return iwl_isr(irq, data);
  1174. trace_iwlwifi_dev_irq(trans->dev);
  1175. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1176. /* Disable (but don't clear!) interrupts here to avoid
  1177. * back-to-back ISRs and sporadic interrupts from our NIC.
  1178. * If we have something to service, the tasklet will re-enable ints.
  1179. * If we *don't* have something, we'll re-enable before leaving here.
  1180. */
  1181. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  1182. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1183. /* Ignore interrupt if there's nothing in NIC to service.
  1184. * This may be due to IRQ shared with another device,
  1185. * or due to sporadic interrupts thrown from our NIC. */
  1186. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1187. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  1188. if (!read) {
  1189. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1190. goto none;
  1191. }
  1192. /*
  1193. * Collect all entries up to the first 0, starting from ict_index;
  1194. * note we already read at ict_index.
  1195. */
  1196. do {
  1197. val |= read;
  1198. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1199. trans_pcie->ict_index, read);
  1200. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1201. trans_pcie->ict_index =
  1202. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  1203. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1204. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1205. read);
  1206. } while (read);
  1207. /* We should not get this value, just ignore it. */
  1208. if (val == 0xffffffff)
  1209. val = 0;
  1210. /*
  1211. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1212. * (bit 15 before shifting it to 31) to clear when using interrupt
  1213. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1214. * so we use them to decide on the real state of the Rx bit.
  1215. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1216. */
  1217. if (val & 0xC0000)
  1218. val |= 0x8000;
  1219. inta = (0xff & val) | ((0xff00 & val) << 16);
  1220. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  1221. inta, inta_mask, val);
  1222. inta &= trans_pcie->inta_mask;
  1223. trans_pcie->inta |= inta;
  1224. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1225. if (likely(inta))
  1226. tasklet_schedule(&trans_pcie->irq_tasklet);
  1227. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1228. !trans_pcie->inta) {
  1229. /* Allow interrupt if was disabled by this handler and
  1230. * no tasklet was schedules, We should not enable interrupt,
  1231. * tasklet will enable it.
  1232. */
  1233. iwl_enable_interrupts(trans);
  1234. }
  1235. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1236. return IRQ_HANDLED;
  1237. none:
  1238. /* re-enable interrupts here since we don't have anything to service.
  1239. * only Re-enable if disabled by irq.
  1240. */
  1241. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1242. !trans_pcie->inta)
  1243. iwl_enable_interrupts(trans);
  1244. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1245. return IRQ_NONE;
  1246. }