iwl-trans-rx-pcie.c 42 KB

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