iwl-trans-pcie-rx.c 43 KB

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