iwl-trans-pcie-rx.c 43 KB

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