iwl-trans-pcie-rx.c 43 KB

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