iwl-trans-rx-pcie.c 43 KB

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