iwl-trans-rx-pcie.c 42 KB

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