iwl-trans-pcie-rx.c 43 KB

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