iwl-trans-pcie-rx.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. struct iwl_device_cmd *cmd;
  323. unsigned long flags;
  324. int len, err;
  325. u16 sequence;
  326. struct iwl_rx_cmd_buffer rxcb;
  327. struct iwl_rx_packet *pkt;
  328. bool reclaim;
  329. int index, cmd_index;
  330. if (WARN_ON(!rxb))
  331. return;
  332. rxcb.truesize = PAGE_SIZE << hw_params(trans).rx_page_order;
  333. dma_unmap_page(trans->dev, rxb->page_dma,
  334. rxcb.truesize,
  335. DMA_FROM_DEVICE);
  336. rxcb._page = rxb->page;
  337. pkt = rxb_addr(&rxcb);
  338. IWL_DEBUG_RX(trans, "%s, 0x%02x\n",
  339. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  340. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  341. len += sizeof(u32); /* account for status word */
  342. trace_iwlwifi_dev_rx(trans->dev, pkt, len);
  343. /* Reclaim a command buffer only if this packet is a response
  344. * to a (driver-originated) command.
  345. * If the packet (e.g. Rx frame) originated from uCode,
  346. * there is no command buffer to reclaim.
  347. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  348. * but apparently a few don't get set; catch them here. */
  349. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  350. if (reclaim) {
  351. int i;
  352. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  353. if (trans_pcie->no_reclaim_cmds[i] == pkt->hdr.cmd) {
  354. reclaim = false;
  355. break;
  356. }
  357. }
  358. }
  359. sequence = le16_to_cpu(pkt->hdr.sequence);
  360. index = SEQ_TO_INDEX(sequence);
  361. cmd_index = get_cmd_index(&txq->q, index);
  362. if (reclaim)
  363. cmd = txq->cmd[cmd_index];
  364. else
  365. cmd = NULL;
  366. err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
  367. /*
  368. * XXX: After here, we should always check rxcb._page
  369. * against NULL before touching it or its virtual
  370. * memory (pkt). Because some rx_handler might have
  371. * already taken or freed the pages.
  372. */
  373. if (reclaim) {
  374. /* Invoke any callbacks, transfer the buffer to caller,
  375. * and fire off the (possibly) blocking
  376. * iwl_trans_send_cmd()
  377. * as we reclaim the driver command queue */
  378. if (rxcb._page)
  379. iwl_tx_cmd_complete(trans, &rxcb, err);
  380. else
  381. IWL_WARN(trans, "Claim null rxb?\n");
  382. }
  383. /* page was stolen from us */
  384. if (rxcb._page == NULL)
  385. rxb->page = NULL;
  386. /* Reuse the page if possible. For notification packets and
  387. * SKBs that fail to Rx correctly, add them back into the
  388. * rx_free list for reuse later. */
  389. spin_lock_irqsave(&rxq->lock, flags);
  390. if (rxb->page != NULL) {
  391. rxb->page_dma =
  392. dma_map_page(trans->dev, rxb->page, 0,
  393. PAGE_SIZE << hw_params(trans).rx_page_order,
  394. DMA_FROM_DEVICE);
  395. list_add_tail(&rxb->list, &rxq->rx_free);
  396. rxq->free_count++;
  397. } else
  398. list_add_tail(&rxb->list, &rxq->rx_used);
  399. spin_unlock_irqrestore(&rxq->lock, flags);
  400. }
  401. /**
  402. * iwl_rx_handle - Main entry function for receiving responses from uCode
  403. *
  404. * Uses the priv->rx_handlers callback function array to invoke
  405. * the appropriate handlers, including command responses,
  406. * frame-received notifications, and other notifications.
  407. */
  408. static void iwl_rx_handle(struct iwl_trans *trans)
  409. {
  410. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  411. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  412. u32 r, i;
  413. u8 fill_rx = 0;
  414. u32 count = 8;
  415. int total_empty;
  416. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  417. * buffer that the driver may process (last buffer filled by ucode). */
  418. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  419. i = rxq->read;
  420. /* Rx interrupt, but nothing sent from uCode */
  421. if (i == r)
  422. IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
  423. /* calculate total frames need to be restock after handling RX */
  424. total_empty = r - rxq->write_actual;
  425. if (total_empty < 0)
  426. total_empty += RX_QUEUE_SIZE;
  427. if (total_empty > (RX_QUEUE_SIZE / 2))
  428. fill_rx = 1;
  429. while (i != r) {
  430. struct iwl_rx_mem_buffer *rxb;
  431. rxb = rxq->queue[i];
  432. rxq->queue[i] = NULL;
  433. IWL_DEBUG_RX(trans, "rxbuf: r = %d, i = %d (%p)\n", rxb);
  434. iwl_rx_handle_rxbuf(trans, rxb);
  435. i = (i + 1) & RX_QUEUE_MASK;
  436. /* If there are a lot of unused frames,
  437. * restock the Rx queue so ucode wont assert. */
  438. if (fill_rx) {
  439. count++;
  440. if (count >= 8) {
  441. rxq->read = i;
  442. iwlagn_rx_replenish_now(trans);
  443. count = 0;
  444. }
  445. }
  446. }
  447. /* Backtrack one entry */
  448. rxq->read = i;
  449. if (fill_rx)
  450. iwlagn_rx_replenish_now(trans);
  451. else
  452. iwlagn_rx_queue_restock(trans);
  453. }
  454. static const char * const desc_lookup_text[] = {
  455. "OK",
  456. "FAIL",
  457. "BAD_PARAM",
  458. "BAD_CHECKSUM",
  459. "NMI_INTERRUPT_WDG",
  460. "SYSASSERT",
  461. "FATAL_ERROR",
  462. "BAD_COMMAND",
  463. "HW_ERROR_TUNE_LOCK",
  464. "HW_ERROR_TEMPERATURE",
  465. "ILLEGAL_CHAN_FREQ",
  466. "VCC_NOT_STABLE",
  467. "FH_ERROR",
  468. "NMI_INTERRUPT_HOST",
  469. "NMI_INTERRUPT_ACTION_PT",
  470. "NMI_INTERRUPT_UNKNOWN",
  471. "UCODE_VERSION_MISMATCH",
  472. "HW_ERROR_ABS_LOCK",
  473. "HW_ERROR_CAL_LOCK_FAIL",
  474. "NMI_INTERRUPT_INST_ACTION_PT",
  475. "NMI_INTERRUPT_DATA_ACTION_PT",
  476. "NMI_TRM_HW_ER",
  477. "NMI_INTERRUPT_TRM",
  478. "NMI_INTERRUPT_BREAK_POINT",
  479. "DEBUG_0",
  480. "DEBUG_1",
  481. "DEBUG_2",
  482. "DEBUG_3",
  483. };
  484. static struct { char *name; u8 num; } advanced_lookup[] = {
  485. { "NMI_INTERRUPT_WDG", 0x34 },
  486. { "SYSASSERT", 0x35 },
  487. { "UCODE_VERSION_MISMATCH", 0x37 },
  488. { "BAD_COMMAND", 0x38 },
  489. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  490. { "FATAL_ERROR", 0x3D },
  491. { "NMI_TRM_HW_ERR", 0x46 },
  492. { "NMI_INTERRUPT_TRM", 0x4C },
  493. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  494. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  495. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  496. { "NMI_INTERRUPT_HOST", 0x66 },
  497. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  498. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  499. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  500. { "ADVANCED_SYSASSERT", 0 },
  501. };
  502. static const char *desc_lookup(u32 num)
  503. {
  504. int i;
  505. int max = ARRAY_SIZE(desc_lookup_text);
  506. if (num < max)
  507. return desc_lookup_text[num];
  508. max = ARRAY_SIZE(advanced_lookup) - 1;
  509. for (i = 0; i < max; i++) {
  510. if (advanced_lookup[i].num == num)
  511. break;
  512. }
  513. return advanced_lookup[i].name;
  514. }
  515. #define ERROR_START_OFFSET (1 * sizeof(u32))
  516. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  517. static void iwl_dump_nic_error_log(struct iwl_trans *trans)
  518. {
  519. u32 base;
  520. struct iwl_error_event_table table;
  521. struct iwl_trans_pcie *trans_pcie =
  522. IWL_TRANS_GET_PCIE_TRANS(trans);
  523. base = trans->shrd->device_pointers.error_event_table;
  524. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  525. if (!base)
  526. base = trans->shrd->fw->init_errlog_ptr;
  527. } else {
  528. if (!base)
  529. base = trans->shrd->fw->inst_errlog_ptr;
  530. }
  531. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  532. IWL_ERR(trans,
  533. "Not valid error log pointer 0x%08X for %s uCode\n",
  534. base,
  535. (trans->shrd->ucode_type == IWL_UCODE_INIT)
  536. ? "Init" : "RT");
  537. return;
  538. }
  539. iwl_read_targ_mem_words(trans, base, &table, sizeof(table));
  540. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  541. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  542. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  543. trans->shrd->status, table.valid);
  544. }
  545. trans_pcie->isr_stats.err_code = table.error_id;
  546. trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
  547. table.data1, table.data2, table.line,
  548. table.blink1, table.blink2, table.ilink1,
  549. table.ilink2, table.bcon_time, table.gp1,
  550. table.gp2, table.gp3, table.ucode_ver,
  551. table.hw_ver, table.brd_ver);
  552. IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
  553. desc_lookup(table.error_id));
  554. IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
  555. IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
  556. IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
  557. IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
  558. IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
  559. IWL_ERR(trans, "0x%08X | data1\n", table.data1);
  560. IWL_ERR(trans, "0x%08X | data2\n", table.data2);
  561. IWL_ERR(trans, "0x%08X | line\n", table.line);
  562. IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
  563. IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
  564. IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
  565. IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
  566. IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
  567. IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
  568. IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
  569. IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
  570. IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
  571. IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
  572. IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
  573. IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
  574. IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
  575. IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
  576. IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
  577. IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
  578. IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
  579. IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
  580. IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
  581. IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
  582. IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
  583. IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
  584. IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
  585. IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
  586. }
  587. /**
  588. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  589. */
  590. static void iwl_irq_handle_error(struct iwl_trans *trans)
  591. {
  592. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  593. if (cfg(trans)->internal_wimax_coex &&
  594. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  595. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  596. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  597. APMG_PS_CTRL_VAL_RESET_REQ))) {
  598. /*
  599. * Keep the restart process from trying to send host
  600. * commands by clearing the ready bit.
  601. */
  602. clear_bit(STATUS_READY, &trans->shrd->status);
  603. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  604. wake_up(&trans->wait_command_queue);
  605. IWL_ERR(trans, "RF is used by WiMAX\n");
  606. return;
  607. }
  608. IWL_ERR(trans, "Loaded firmware version: %s\n",
  609. trans->shrd->fw->fw_version);
  610. iwl_dump_nic_error_log(trans);
  611. iwl_dump_csr(trans);
  612. iwl_dump_fh(trans, NULL, false);
  613. iwl_dump_nic_event_log(trans, false, NULL, false);
  614. iwl_op_mode_nic_error(trans->op_mode);
  615. }
  616. #define EVENT_START_OFFSET (4 * sizeof(u32))
  617. /**
  618. * iwl_print_event_log - Dump error event log to syslog
  619. *
  620. */
  621. static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
  622. u32 num_events, u32 mode,
  623. int pos, char **buf, size_t bufsz)
  624. {
  625. u32 i;
  626. u32 base; /* SRAM byte address of event log header */
  627. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  628. u32 ptr; /* SRAM byte address of log data */
  629. u32 ev, time, data; /* event log data */
  630. unsigned long reg_flags;
  631. if (num_events == 0)
  632. return pos;
  633. base = trans->shrd->device_pointers.log_event_table;
  634. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  635. if (!base)
  636. base = trans->shrd->fw->init_evtlog_ptr;
  637. } else {
  638. if (!base)
  639. base = trans->shrd->fw->inst_evtlog_ptr;
  640. }
  641. if (mode == 0)
  642. event_size = 2 * sizeof(u32);
  643. else
  644. event_size = 3 * sizeof(u32);
  645. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  646. /* Make sure device is powered up for SRAM reads */
  647. spin_lock_irqsave(&trans->reg_lock, reg_flags);
  648. if (unlikely(!iwl_grab_nic_access(trans)))
  649. goto out_unlock;
  650. /* Set starting address; reads will auto-increment */
  651. iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
  652. /* "time" is actually "data" for mode 0 (no timestamp).
  653. * place event id # at far right for easier visual parsing. */
  654. for (i = 0; i < num_events; i++) {
  655. ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  656. time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  657. if (mode == 0) {
  658. /* data, ev */
  659. if (bufsz) {
  660. pos += scnprintf(*buf + pos, bufsz - pos,
  661. "EVT_LOG:0x%08x:%04u\n",
  662. time, ev);
  663. } else {
  664. trace_iwlwifi_dev_ucode_event(trans->dev, 0,
  665. time, ev);
  666. IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
  667. time, ev);
  668. }
  669. } else {
  670. data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  671. if (bufsz) {
  672. pos += scnprintf(*buf + pos, bufsz - pos,
  673. "EVT_LOGT:%010u:0x%08x:%04u\n",
  674. time, data, ev);
  675. } else {
  676. IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
  677. time, data, ev);
  678. trace_iwlwifi_dev_ucode_event(trans->dev, time,
  679. data, ev);
  680. }
  681. }
  682. }
  683. /* Allow device to power down */
  684. iwl_release_nic_access(trans);
  685. out_unlock:
  686. spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
  687. return pos;
  688. }
  689. /**
  690. * iwl_print_last_event_logs - Dump the newest # of event log to syslog
  691. */
  692. static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
  693. u32 num_wraps, u32 next_entry,
  694. u32 size, u32 mode,
  695. int pos, char **buf, size_t bufsz)
  696. {
  697. /*
  698. * display the newest DEFAULT_LOG_ENTRIES entries
  699. * i.e the entries just before the next ont that uCode would fill.
  700. */
  701. if (num_wraps) {
  702. if (next_entry < size) {
  703. pos = iwl_print_event_log(trans,
  704. capacity - (size - next_entry),
  705. size - next_entry, mode,
  706. pos, buf, bufsz);
  707. pos = iwl_print_event_log(trans, 0,
  708. next_entry, mode,
  709. pos, buf, bufsz);
  710. } else
  711. pos = iwl_print_event_log(trans, next_entry - size,
  712. size, mode, pos, buf, bufsz);
  713. } else {
  714. if (next_entry < size) {
  715. pos = iwl_print_event_log(trans, 0, next_entry,
  716. mode, pos, buf, bufsz);
  717. } else {
  718. pos = iwl_print_event_log(trans, next_entry - size,
  719. size, mode, pos, buf, bufsz);
  720. }
  721. }
  722. return pos;
  723. }
  724. #define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
  725. int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
  726. char **buf, bool display)
  727. {
  728. u32 base; /* SRAM byte address of event log header */
  729. u32 capacity; /* event log capacity in # entries */
  730. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  731. u32 num_wraps; /* # times uCode wrapped to top of log */
  732. u32 next_entry; /* index of next entry to be written by uCode */
  733. u32 size; /* # entries that we'll print */
  734. u32 logsize;
  735. int pos = 0;
  736. size_t bufsz = 0;
  737. base = trans->shrd->device_pointers.log_event_table;
  738. if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
  739. logsize = trans->shrd->fw->init_evtlog_size;
  740. if (!base)
  741. base = trans->shrd->fw->init_evtlog_ptr;
  742. } else {
  743. logsize = trans->shrd->fw->inst_evtlog_size;
  744. if (!base)
  745. base = trans->shrd->fw->inst_evtlog_ptr;
  746. }
  747. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  748. IWL_ERR(trans,
  749. "Invalid event log pointer 0x%08X for %s uCode\n",
  750. base,
  751. (trans->shrd->ucode_type == IWL_UCODE_INIT)
  752. ? "Init" : "RT");
  753. return -EINVAL;
  754. }
  755. /* event log header */
  756. capacity = iwl_read_targ_mem(trans, base);
  757. mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
  758. num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
  759. next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
  760. if (capacity > logsize) {
  761. IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
  762. "entries\n", capacity, logsize);
  763. capacity = logsize;
  764. }
  765. if (next_entry > logsize) {
  766. IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
  767. next_entry, logsize);
  768. next_entry = logsize;
  769. }
  770. size = num_wraps ? capacity : next_entry;
  771. /* bail out if nothing in log */
  772. if (size == 0) {
  773. IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
  774. return pos;
  775. }
  776. #ifdef CONFIG_IWLWIFI_DEBUG
  777. if (!(iwl_have_debug_level(IWL_DL_FW_ERRORS)) && !full_log)
  778. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  779. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  780. #else
  781. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  782. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  783. #endif
  784. IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
  785. size);
  786. #ifdef CONFIG_IWLWIFI_DEBUG
  787. if (display) {
  788. if (full_log)
  789. bufsz = capacity * 48;
  790. else
  791. bufsz = size * 48;
  792. *buf = kmalloc(bufsz, GFP_KERNEL);
  793. if (!*buf)
  794. return -ENOMEM;
  795. }
  796. if (iwl_have_debug_level(IWL_DL_FW_ERRORS) || full_log) {
  797. /*
  798. * if uCode has wrapped back to top of log,
  799. * start at the oldest entry,
  800. * i.e the next one that uCode would fill.
  801. */
  802. if (num_wraps)
  803. pos = iwl_print_event_log(trans, next_entry,
  804. capacity - next_entry, mode,
  805. pos, buf, bufsz);
  806. /* (then/else) start at top of log */
  807. pos = iwl_print_event_log(trans, 0,
  808. next_entry, mode, pos, buf, bufsz);
  809. } else
  810. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  811. next_entry, size, mode,
  812. pos, buf, bufsz);
  813. #else
  814. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  815. next_entry, size, mode,
  816. pos, buf, bufsz);
  817. #endif
  818. return pos;
  819. }
  820. /* tasklet for iwlagn interrupt */
  821. void iwl_irq_tasklet(struct iwl_trans *trans)
  822. {
  823. u32 inta = 0;
  824. u32 handled = 0;
  825. unsigned long flags;
  826. u32 i;
  827. #ifdef CONFIG_IWLWIFI_DEBUG
  828. u32 inta_mask;
  829. #endif
  830. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  831. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  832. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  833. /* Ack/clear/reset pending uCode interrupts.
  834. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  835. */
  836. /* There is a hardware bug in the interrupt mask function that some
  837. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  838. * they are disabled in the CSR_INT_MASK register. Furthermore the
  839. * ICT interrupt handling mechanism has another bug that might cause
  840. * these unmasked interrupts fail to be detected. We workaround the
  841. * hardware bugs here by ACKing all the possible interrupts so that
  842. * interrupt coalescing can still be achieved.
  843. */
  844. iwl_write32(trans, CSR_INT,
  845. trans_pcie->inta | ~trans_pcie->inta_mask);
  846. inta = trans_pcie->inta;
  847. #ifdef CONFIG_IWLWIFI_DEBUG
  848. if (iwl_have_debug_level(IWL_DL_ISR)) {
  849. /* just for debug */
  850. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  851. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
  852. inta, inta_mask);
  853. }
  854. #endif
  855. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  856. trans_pcie->inta = 0;
  857. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  858. /* Now service all interrupt bits discovered above. */
  859. if (inta & CSR_INT_BIT_HW_ERR) {
  860. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  861. /* Tell the device to stop sending interrupts */
  862. iwl_disable_interrupts(trans);
  863. isr_stats->hw++;
  864. iwl_irq_handle_error(trans);
  865. handled |= CSR_INT_BIT_HW_ERR;
  866. return;
  867. }
  868. #ifdef CONFIG_IWLWIFI_DEBUG
  869. if (iwl_have_debug_level(IWL_DL_ISR)) {
  870. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  871. if (inta & CSR_INT_BIT_SCD) {
  872. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  873. "the frame/frames.\n");
  874. isr_stats->sch++;
  875. }
  876. /* Alive notification via Rx interrupt will do the real work */
  877. if (inta & CSR_INT_BIT_ALIVE) {
  878. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  879. isr_stats->alive++;
  880. }
  881. }
  882. #endif
  883. /* Safely ignore these bits for debug checks below */
  884. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  885. /* HW RF KILL switch toggled */
  886. if (inta & CSR_INT_BIT_RF_KILL) {
  887. bool hw_rfkill;
  888. hw_rfkill = !(iwl_read32(trans, CSR_GP_CNTRL) &
  889. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  890. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  891. hw_rfkill ? "disable radio" : "enable radio");
  892. isr_stats->rfkill++;
  893. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  894. handled |= CSR_INT_BIT_RF_KILL;
  895. }
  896. /* Chip got too hot and stopped itself */
  897. if (inta & CSR_INT_BIT_CT_KILL) {
  898. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  899. isr_stats->ctkill++;
  900. handled |= CSR_INT_BIT_CT_KILL;
  901. }
  902. /* Error detected by uCode */
  903. if (inta & CSR_INT_BIT_SW_ERR) {
  904. IWL_ERR(trans, "Microcode SW error detected. "
  905. " Restarting 0x%X.\n", inta);
  906. isr_stats->sw++;
  907. iwl_irq_handle_error(trans);
  908. handled |= CSR_INT_BIT_SW_ERR;
  909. }
  910. /* uCode wakes up after power-down sleep */
  911. if (inta & CSR_INT_BIT_WAKEUP) {
  912. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  913. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  914. for (i = 0; i < cfg(trans)->base_params->num_of_queues; i++)
  915. iwl_txq_update_write_ptr(trans,
  916. &trans_pcie->txq[i]);
  917. isr_stats->wakeup++;
  918. handled |= CSR_INT_BIT_WAKEUP;
  919. }
  920. /* All uCode command responses, including Tx command responses,
  921. * Rx "responses" (frame-received notification), and other
  922. * notifications from uCode come through here*/
  923. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  924. CSR_INT_BIT_RX_PERIODIC)) {
  925. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  926. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  927. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  928. iwl_write32(trans, CSR_FH_INT_STATUS,
  929. CSR_FH_INT_RX_MASK);
  930. }
  931. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  932. handled |= CSR_INT_BIT_RX_PERIODIC;
  933. iwl_write32(trans,
  934. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  935. }
  936. /* Sending RX interrupt require many steps to be done in the
  937. * the device:
  938. * 1- write interrupt to current index in ICT table.
  939. * 2- dma RX frame.
  940. * 3- update RX shared data to indicate last write index.
  941. * 4- send interrupt.
  942. * This could lead to RX race, driver could receive RX interrupt
  943. * but the shared data changes does not reflect this;
  944. * periodic interrupt will detect any dangling Rx activity.
  945. */
  946. /* Disable periodic interrupt; we use it as just a one-shot. */
  947. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  948. CSR_INT_PERIODIC_DIS);
  949. #ifdef CONFIG_IWLWIFI_IDI
  950. iwl_amfh_rx_handler();
  951. #else
  952. iwl_rx_handle(trans);
  953. #endif
  954. /*
  955. * Enable periodic interrupt in 8 msec only if we received
  956. * real RX interrupt (instead of just periodic int), to catch
  957. * any dangling Rx interrupt. If it was just the periodic
  958. * interrupt, there was no dangling Rx activity, and no need
  959. * to extend the periodic interrupt; one-shot is enough.
  960. */
  961. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  962. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  963. CSR_INT_PERIODIC_ENA);
  964. isr_stats->rx++;
  965. }
  966. /* This "Tx" DMA channel is used only for loading uCode */
  967. if (inta & CSR_INT_BIT_FH_TX) {
  968. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  969. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  970. isr_stats->tx++;
  971. handled |= CSR_INT_BIT_FH_TX;
  972. /* Wake up uCode load routine, now that load is complete */
  973. trans_pcie->ucode_write_complete = true;
  974. wake_up(&trans_pcie->ucode_write_waitq);
  975. }
  976. if (inta & ~handled) {
  977. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  978. isr_stats->unhandled++;
  979. }
  980. if (inta & ~(trans_pcie->inta_mask)) {
  981. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  982. inta & ~trans_pcie->inta_mask);
  983. }
  984. /* Re-enable all interrupts */
  985. /* only Re-enable if disabled by irq */
  986. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  987. iwl_enable_interrupts(trans);
  988. /* Re-enable RF_KILL if it occurred */
  989. else if (handled & CSR_INT_BIT_RF_KILL)
  990. iwl_enable_rfkill_int(trans);
  991. }
  992. /******************************************************************************
  993. *
  994. * ICT functions
  995. *
  996. ******************************************************************************/
  997. /* a device (PCI-E) page is 4096 bytes long */
  998. #define ICT_SHIFT 12
  999. #define ICT_SIZE (1 << ICT_SHIFT)
  1000. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  1001. /* Free dram table */
  1002. void iwl_free_isr_ict(struct iwl_trans *trans)
  1003. {
  1004. struct iwl_trans_pcie *trans_pcie =
  1005. IWL_TRANS_GET_PCIE_TRANS(trans);
  1006. if (trans_pcie->ict_tbl) {
  1007. dma_free_coherent(trans->dev, ICT_SIZE,
  1008. trans_pcie->ict_tbl,
  1009. trans_pcie->ict_tbl_dma);
  1010. trans_pcie->ict_tbl = NULL;
  1011. trans_pcie->ict_tbl_dma = 0;
  1012. }
  1013. }
  1014. /*
  1015. * allocate dram shared table, it is an aligned memory
  1016. * block of ICT_SIZE.
  1017. * also reset all data related to ICT table interrupt.
  1018. */
  1019. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  1020. {
  1021. struct iwl_trans_pcie *trans_pcie =
  1022. IWL_TRANS_GET_PCIE_TRANS(trans);
  1023. trans_pcie->ict_tbl =
  1024. dma_alloc_coherent(trans->dev, ICT_SIZE,
  1025. &trans_pcie->ict_tbl_dma,
  1026. GFP_KERNEL);
  1027. if (!trans_pcie->ict_tbl)
  1028. return -ENOMEM;
  1029. /* just an API sanity check ... it is guaranteed to be aligned */
  1030. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  1031. iwl_free_isr_ict(trans);
  1032. return -EINVAL;
  1033. }
  1034. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  1035. (unsigned long long)trans_pcie->ict_tbl_dma);
  1036. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  1037. /* reset table and index to all 0 */
  1038. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1039. trans_pcie->ict_index = 0;
  1040. /* add periodic RX interrupt */
  1041. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  1042. return 0;
  1043. }
  1044. /* Device is going up inform it about using ICT interrupt table,
  1045. * also we need to tell the driver to start using ICT interrupt.
  1046. */
  1047. void iwl_reset_ict(struct iwl_trans *trans)
  1048. {
  1049. u32 val;
  1050. unsigned long flags;
  1051. struct iwl_trans_pcie *trans_pcie =
  1052. IWL_TRANS_GET_PCIE_TRANS(trans);
  1053. if (!trans_pcie->ict_tbl)
  1054. return;
  1055. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1056. iwl_disable_interrupts(trans);
  1057. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1058. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  1059. val |= CSR_DRAM_INT_TBL_ENABLE;
  1060. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  1061. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  1062. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  1063. trans_pcie->use_ict = true;
  1064. trans_pcie->ict_index = 0;
  1065. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  1066. iwl_enable_interrupts(trans);
  1067. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1068. }
  1069. /* Device is going down disable ict interrupt usage */
  1070. void iwl_disable_ict(struct iwl_trans *trans)
  1071. {
  1072. struct iwl_trans_pcie *trans_pcie =
  1073. IWL_TRANS_GET_PCIE_TRANS(trans);
  1074. unsigned long flags;
  1075. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1076. trans_pcie->use_ict = false;
  1077. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1078. }
  1079. static irqreturn_t iwl_isr(int irq, void *data)
  1080. {
  1081. struct iwl_trans *trans = data;
  1082. struct iwl_trans_pcie *trans_pcie;
  1083. u32 inta, inta_mask;
  1084. unsigned long flags;
  1085. #ifdef CONFIG_IWLWIFI_DEBUG
  1086. u32 inta_fh;
  1087. #endif
  1088. if (!trans)
  1089. return IRQ_NONE;
  1090. trace_iwlwifi_dev_irq(trans->dev);
  1091. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1092. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1093. /* Disable (but don't clear!) interrupts here to avoid
  1094. * back-to-back ISRs and sporadic interrupts from our NIC.
  1095. * If we have something to service, the tasklet will re-enable ints.
  1096. * If we *don't* have something, we'll re-enable before leaving here. */
  1097. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  1098. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1099. /* Discover which interrupts are active/pending */
  1100. inta = iwl_read32(trans, CSR_INT);
  1101. /* Ignore interrupt if there's nothing in NIC to service.
  1102. * This may be due to IRQ shared with another device,
  1103. * or due to sporadic interrupts thrown from our NIC. */
  1104. if (!inta) {
  1105. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1106. goto none;
  1107. }
  1108. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1109. /* Hardware disappeared. It might have already raised
  1110. * an interrupt */
  1111. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1112. goto unplugged;
  1113. }
  1114. #ifdef CONFIG_IWLWIFI_DEBUG
  1115. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1116. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  1117. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  1118. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  1119. }
  1120. #endif
  1121. trans_pcie->inta |= inta;
  1122. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1123. if (likely(inta))
  1124. tasklet_schedule(&trans_pcie->irq_tasklet);
  1125. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1126. !trans_pcie->inta)
  1127. iwl_enable_interrupts(trans);
  1128. unplugged:
  1129. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1130. return IRQ_HANDLED;
  1131. none:
  1132. /* re-enable interrupts here since we don't have anything to service. */
  1133. /* only Re-enable if disabled by irq and no schedules tasklet. */
  1134. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1135. !trans_pcie->inta)
  1136. iwl_enable_interrupts(trans);
  1137. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1138. return IRQ_NONE;
  1139. }
  1140. /* interrupt handler using ict table, with this interrupt driver will
  1141. * stop using INTA register to get device's interrupt, reading this register
  1142. * is expensive, device will write interrupts in ICT dram table, increment
  1143. * index then will fire interrupt to driver, driver will OR all ICT table
  1144. * entries from current index up to table entry with 0 value. the result is
  1145. * the interrupt we need to service, driver will set the entries back to 0 and
  1146. * set index.
  1147. */
  1148. irqreturn_t iwl_isr_ict(int irq, void *data)
  1149. {
  1150. struct iwl_trans *trans = data;
  1151. struct iwl_trans_pcie *trans_pcie;
  1152. u32 inta, inta_mask;
  1153. u32 val = 0;
  1154. u32 read;
  1155. unsigned long flags;
  1156. if (!trans)
  1157. return IRQ_NONE;
  1158. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1159. /* dram interrupt table not set yet,
  1160. * use legacy interrupt.
  1161. */
  1162. if (!trans_pcie->use_ict)
  1163. return iwl_isr(irq, data);
  1164. trace_iwlwifi_dev_irq(trans->dev);
  1165. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1166. /* Disable (but don't clear!) interrupts here to avoid
  1167. * back-to-back ISRs and sporadic interrupts from our NIC.
  1168. * If we have something to service, the tasklet will re-enable ints.
  1169. * If we *don't* have something, we'll re-enable before leaving here.
  1170. */
  1171. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  1172. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1173. /* Ignore interrupt if there's nothing in NIC to service.
  1174. * This may be due to IRQ shared with another device,
  1175. * or due to sporadic interrupts thrown from our NIC. */
  1176. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1177. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  1178. if (!read) {
  1179. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1180. goto none;
  1181. }
  1182. /*
  1183. * Collect all entries up to the first 0, starting from ict_index;
  1184. * note we already read at ict_index.
  1185. */
  1186. do {
  1187. val |= read;
  1188. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1189. trans_pcie->ict_index, read);
  1190. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1191. trans_pcie->ict_index =
  1192. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  1193. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1194. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1195. read);
  1196. } while (read);
  1197. /* We should not get this value, just ignore it. */
  1198. if (val == 0xffffffff)
  1199. val = 0;
  1200. /*
  1201. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1202. * (bit 15 before shifting it to 31) to clear when using interrupt
  1203. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1204. * so we use them to decide on the real state of the Rx bit.
  1205. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1206. */
  1207. if (val & 0xC0000)
  1208. val |= 0x8000;
  1209. inta = (0xff & val) | ((0xff00 & val) << 16);
  1210. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  1211. inta, inta_mask, val);
  1212. inta &= trans_pcie->inta_mask;
  1213. trans_pcie->inta |= inta;
  1214. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1215. if (likely(inta))
  1216. tasklet_schedule(&trans_pcie->irq_tasklet);
  1217. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1218. !trans_pcie->inta) {
  1219. /* Allow interrupt if was disabled by this handler and
  1220. * no tasklet was schedules, We should not enable interrupt,
  1221. * tasklet will enable it.
  1222. */
  1223. iwl_enable_interrupts(trans);
  1224. }
  1225. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1226. return IRQ_HANDLED;
  1227. none:
  1228. /* re-enable interrupts here since we don't have anything to service.
  1229. * only Re-enable if disabled by irq.
  1230. */
  1231. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  1232. !trans_pcie->inta)
  1233. iwl_enable_interrupts(trans);
  1234. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1235. return IRQ_NONE;
  1236. }