iwl-trans-rx-pcie.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/sched.h>
  30. #include <linux/wait.h>
  31. #include <linux/gfp.h>
  32. #include "iwl-dev.h"
  33. #include "iwl-agn.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. #include "iwl-trans-int-pcie.h"
  38. /******************************************************************************
  39. *
  40. * RX path functions
  41. *
  42. ******************************************************************************/
  43. /*
  44. * Rx theory of operation
  45. *
  46. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  47. * each of which point to Receive Buffers to be filled by the NIC. These get
  48. * used not only for Rx frames, but for any command response or notification
  49. * from the NIC. The driver and NIC manage the Rx buffers by means
  50. * of indexes into the circular buffer.
  51. *
  52. * Rx Queue Indexes
  53. * The host/firmware share two index registers for managing the Rx buffers.
  54. *
  55. * The READ index maps to the first position that the firmware may be writing
  56. * to -- the driver can read up to (but not including) this position and get
  57. * good data.
  58. * The READ index is managed by the firmware once the card is enabled.
  59. *
  60. * The WRITE index maps to the last position the driver has read from -- the
  61. * position preceding WRITE is the last slot the firmware can place a packet.
  62. *
  63. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  64. * WRITE = READ.
  65. *
  66. * During initialization, the host sets up the READ queue position to the first
  67. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  68. *
  69. * When the firmware places a packet in a buffer, it will advance the READ index
  70. * and fire the RX interrupt. The driver can then query the READ index and
  71. * process as many packets as possible, moving the WRITE index forward as it
  72. * resets the Rx queue buffers with new memory.
  73. *
  74. * The management in the driver is as follows:
  75. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  76. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  77. * to replenish the iwl->rxq->rx_free.
  78. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  79. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  80. * 'processed' and 'read' driver indexes as well)
  81. * + A received packet is processed and handed to the kernel network stack,
  82. * detached from the iwl->rxq. The driver 'processed' index is updated.
  83. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  84. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  85. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  86. * were enough free buffers and RX_STALLED is set it is cleared.
  87. *
  88. *
  89. * Driver sequence:
  90. *
  91. * iwl_rx_queue_alloc() Allocates rx_free
  92. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  93. * iwl_rx_queue_restock
  94. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  95. * queue, updates firmware pointers, and updates
  96. * the WRITE index. If insufficient rx_free buffers
  97. * are available, schedules iwl_rx_replenish
  98. *
  99. * -- enable interrupts --
  100. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  101. * READ INDEX, detaching the SKB from the pool.
  102. * Moves the packet buffer from queue to rx_used.
  103. * Calls iwl_rx_queue_restock to refill any empty
  104. * slots.
  105. * ...
  106. *
  107. */
  108. /**
  109. * iwl_rx_queue_space - Return number of free slots available in queue.
  110. */
  111. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  112. {
  113. int s = q->read - q->write;
  114. if (s <= 0)
  115. s += RX_QUEUE_SIZE;
  116. /* keep some buffer to not confuse full and empty queue */
  117. s -= 2;
  118. if (s < 0)
  119. s = 0;
  120. return s;
  121. }
  122. /**
  123. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  124. */
  125. void iwl_rx_queue_update_write_ptr(struct iwl_priv *priv,
  126. struct iwl_rx_queue *q)
  127. {
  128. unsigned long flags;
  129. u32 reg;
  130. spin_lock_irqsave(&q->lock, flags);
  131. if (q->need_update == 0)
  132. goto exit_unlock;
  133. if (priv->cfg->base_params->shadow_reg_enable) {
  134. /* shadow register enabled */
  135. /* Device expects a multiple of 8 */
  136. q->write_actual = (q->write & ~0x7);
  137. iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write_actual);
  138. } else {
  139. /* If power-saving is in use, make sure device is awake */
  140. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  141. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  142. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  143. IWL_DEBUG_INFO(priv,
  144. "Rx queue requesting wakeup,"
  145. " GP1 = 0x%x\n", reg);
  146. iwl_set_bit(priv, CSR_GP_CNTRL,
  147. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  148. goto exit_unlock;
  149. }
  150. q->write_actual = (q->write & ~0x7);
  151. iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  152. q->write_actual);
  153. /* Else device is assumed to be awake */
  154. } else {
  155. /* Device expects a multiple of 8 */
  156. q->write_actual = (q->write & ~0x7);
  157. iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  158. q->write_actual);
  159. }
  160. }
  161. q->need_update = 0;
  162. exit_unlock:
  163. spin_unlock_irqrestore(&q->lock, flags);
  164. }
  165. /**
  166. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  167. */
  168. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  169. 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_priv *priv)
  185. {
  186. struct iwl_rx_queue *rxq = &priv->rxq;
  187. struct list_head *element;
  188. struct iwl_rx_mem_buffer *rxb;
  189. unsigned long flags;
  190. spin_lock_irqsave(&rxq->lock, flags);
  191. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  192. /* The overwritten rxb must be a used one */
  193. rxb = rxq->queue[rxq->write];
  194. BUG_ON(rxb && rxb->page);
  195. /* Get next free Rx buffer, remove from free list */
  196. element = rxq->rx_free.next;
  197. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  198. list_del(element);
  199. /* Point to Rx buffer via next RBD in circular buffer */
  200. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  201. rxb->page_dma);
  202. rxq->queue[rxq->write] = rxb;
  203. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  204. rxq->free_count--;
  205. }
  206. spin_unlock_irqrestore(&rxq->lock, flags);
  207. /* If the pre-allocated buffer pool is dropping low, schedule to
  208. * refill it */
  209. if (rxq->free_count <= RX_LOW_WATERMARK)
  210. queue_work(priv->workqueue, &priv->rx_replenish);
  211. /* If we've added more space for the firmware to place data, tell it.
  212. * Increment device's write pointer in multiples of 8. */
  213. if (rxq->write_actual != (rxq->write & ~0x7)) {
  214. spin_lock_irqsave(&rxq->lock, flags);
  215. rxq->need_update = 1;
  216. spin_unlock_irqrestore(&rxq->lock, flags);
  217. iwl_rx_queue_update_write_ptr(priv, rxq);
  218. }
  219. }
  220. /**
  221. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  222. *
  223. * When moving to rx_free an SKB is allocated for the slot.
  224. *
  225. * Also restock the Rx queue via iwl_rx_queue_restock.
  226. * This is called as a scheduled work item (except for during initialization)
  227. */
  228. static void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  229. {
  230. struct iwl_rx_queue *rxq = &priv->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 (priv->hw_params.rx_page_order > 0)
  246. gfp_mask |= __GFP_COMP;
  247. /* Alloc a new receive buffer */
  248. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  249. if (!page) {
  250. if (net_ratelimit())
  251. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  252. "order: %d\n",
  253. priv->hw_params.rx_page_order);
  254. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  255. net_ratelimit())
  256. IWL_CRIT(priv, "Failed to alloc_pages with %s."
  257. "Only %u free buffers remaining.\n",
  258. priority == GFP_ATOMIC ?
  259. "GFP_ATOMIC" : "GFP_KERNEL",
  260. rxq->free_count);
  261. /* We don't reschedule replenish work here -- we will
  262. * call the restock method and if it still needs
  263. * more buffers it will schedule replenish */
  264. return;
  265. }
  266. spin_lock_irqsave(&rxq->lock, flags);
  267. if (list_empty(&rxq->rx_used)) {
  268. spin_unlock_irqrestore(&rxq->lock, flags);
  269. __free_pages(page, priv->hw_params.rx_page_order);
  270. return;
  271. }
  272. element = rxq->rx_used.next;
  273. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  274. list_del(element);
  275. spin_unlock_irqrestore(&rxq->lock, flags);
  276. BUG_ON(rxb->page);
  277. rxb->page = page;
  278. /* Get physical address of the RB */
  279. rxb->page_dma = dma_map_page(priv->bus->dev, page, 0,
  280. PAGE_SIZE << priv->hw_params.rx_page_order,
  281. DMA_FROM_DEVICE);
  282. /* dma address must be no more than 36 bits */
  283. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  284. /* and also 256 byte aligned! */
  285. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  286. spin_lock_irqsave(&rxq->lock, flags);
  287. list_add_tail(&rxb->list, &rxq->rx_free);
  288. rxq->free_count++;
  289. spin_unlock_irqrestore(&rxq->lock, flags);
  290. }
  291. }
  292. void iwlagn_rx_replenish(struct iwl_priv *priv)
  293. {
  294. unsigned long flags;
  295. iwlagn_rx_allocate(priv, GFP_KERNEL);
  296. spin_lock_irqsave(&priv->lock, flags);
  297. iwlagn_rx_queue_restock(priv);
  298. spin_unlock_irqrestore(&priv->lock, flags);
  299. }
  300. static void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  301. {
  302. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  303. iwlagn_rx_queue_restock(priv);
  304. }
  305. void iwl_bg_rx_replenish(struct work_struct *data)
  306. {
  307. struct iwl_priv *priv =
  308. container_of(data, struct iwl_priv, rx_replenish);
  309. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  310. return;
  311. mutex_lock(&priv->mutex);
  312. iwlagn_rx_replenish(priv);
  313. mutex_unlock(&priv->mutex);
  314. }
  315. /**
  316. * iwl_rx_handle - Main entry function for receiving responses from uCode
  317. *
  318. * Uses the priv->rx_handlers callback function array to invoke
  319. * the appropriate handlers, including command responses,
  320. * frame-received notifications, and other notifications.
  321. */
  322. static void iwl_rx_handle(struct iwl_priv *priv)
  323. {
  324. struct iwl_rx_mem_buffer *rxb;
  325. struct iwl_rx_packet *pkt;
  326. struct iwl_rx_queue *rxq = &priv->rxq;
  327. u32 r, i;
  328. int reclaim;
  329. unsigned long flags;
  330. u8 fill_rx = 0;
  331. u32 count = 8;
  332. int total_empty;
  333. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  334. * buffer that the driver may process (last buffer filled by ucode). */
  335. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  336. i = rxq->read;
  337. /* Rx interrupt, but nothing sent from uCode */
  338. if (i == r)
  339. IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
  340. /* calculate total frames need to be restock after handling RX */
  341. total_empty = r - rxq->write_actual;
  342. if (total_empty < 0)
  343. total_empty += RX_QUEUE_SIZE;
  344. if (total_empty > (RX_QUEUE_SIZE / 2))
  345. fill_rx = 1;
  346. while (i != r) {
  347. int len;
  348. rxb = rxq->queue[i];
  349. /* If an RXB doesn't have a Rx queue slot associated with it,
  350. * then a bug has been introduced in the queue refilling
  351. * routines -- catch it here */
  352. if (WARN_ON(rxb == NULL)) {
  353. i = (i + 1) & RX_QUEUE_MASK;
  354. continue;
  355. }
  356. rxq->queue[i] = NULL;
  357. dma_unmap_page(priv->bus->dev, rxb->page_dma,
  358. PAGE_SIZE << priv->hw_params.rx_page_order,
  359. DMA_FROM_DEVICE);
  360. pkt = rxb_addr(rxb);
  361. IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
  362. i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  363. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  364. len += sizeof(u32); /* account for status word */
  365. trace_iwlwifi_dev_rx(priv, pkt, len);
  366. /* Reclaim a command buffer only if this packet is a response
  367. * to a (driver-originated) command.
  368. * If the packet (e.g. Rx frame) originated from uCode,
  369. * there is no command buffer to reclaim.
  370. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  371. * but apparently a few don't get set; catch them here. */
  372. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  373. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  374. (pkt->hdr.cmd != REPLY_RX) &&
  375. (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
  376. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  377. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  378. (pkt->hdr.cmd != REPLY_TX);
  379. iwl_rx_dispatch(priv, rxb);
  380. /*
  381. * XXX: After here, we should always check rxb->page
  382. * against NULL before touching it or its virtual
  383. * memory (pkt). Because some rx_handler might have
  384. * already taken or freed the pages.
  385. */
  386. if (reclaim) {
  387. /* Invoke any callbacks, transfer the buffer to caller,
  388. * and fire off the (possibly) blocking
  389. * trans_send_cmd()
  390. * as we reclaim the driver command queue */
  391. if (rxb->page)
  392. iwl_tx_cmd_complete(priv, rxb);
  393. else
  394. IWL_WARN(priv, "Claim null rxb?\n");
  395. }
  396. /* Reuse the page if possible. For notification packets and
  397. * SKBs that fail to Rx correctly, add them back into the
  398. * rx_free list for reuse later. */
  399. spin_lock_irqsave(&rxq->lock, flags);
  400. if (rxb->page != NULL) {
  401. rxb->page_dma = dma_map_page(priv->bus->dev, rxb->page,
  402. 0, PAGE_SIZE << priv->hw_params.rx_page_order,
  403. DMA_FROM_DEVICE);
  404. list_add_tail(&rxb->list, &rxq->rx_free);
  405. rxq->free_count++;
  406. } else
  407. list_add_tail(&rxb->list, &rxq->rx_used);
  408. spin_unlock_irqrestore(&rxq->lock, flags);
  409. i = (i + 1) & RX_QUEUE_MASK;
  410. /* If there are a lot of unused frames,
  411. * restock the Rx queue so ucode wont assert. */
  412. if (fill_rx) {
  413. count++;
  414. if (count >= 8) {
  415. rxq->read = i;
  416. iwlagn_rx_replenish_now(priv);
  417. count = 0;
  418. }
  419. }
  420. }
  421. /* Backtrack one entry */
  422. rxq->read = i;
  423. if (fill_rx)
  424. iwlagn_rx_replenish_now(priv);
  425. else
  426. iwlagn_rx_queue_restock(priv);
  427. }
  428. /* tasklet for iwlagn interrupt */
  429. void iwl_irq_tasklet(struct iwl_priv *priv)
  430. {
  431. u32 inta = 0;
  432. u32 handled = 0;
  433. unsigned long flags;
  434. u32 i;
  435. #ifdef CONFIG_IWLWIFI_DEBUG
  436. u32 inta_mask;
  437. #endif
  438. spin_lock_irqsave(&priv->lock, flags);
  439. /* Ack/clear/reset pending uCode interrupts.
  440. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  441. */
  442. /* There is a hardware bug in the interrupt mask function that some
  443. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  444. * they are disabled in the CSR_INT_MASK register. Furthermore the
  445. * ICT interrupt handling mechanism has another bug that might cause
  446. * these unmasked interrupts fail to be detected. We workaround the
  447. * hardware bugs here by ACKing all the possible interrupts so that
  448. * interrupt coalescing can still be achieved.
  449. */
  450. iwl_write32(priv, CSR_INT, priv->inta | ~priv->inta_mask);
  451. inta = priv->inta;
  452. #ifdef CONFIG_IWLWIFI_DEBUG
  453. if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
  454. /* just for debug */
  455. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  456. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x\n ",
  457. inta, inta_mask);
  458. }
  459. #endif
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. /* saved interrupt in inta variable now we can reset priv->inta */
  462. priv->inta = 0;
  463. /* Now service all interrupt bits discovered above. */
  464. if (inta & CSR_INT_BIT_HW_ERR) {
  465. IWL_ERR(priv, "Hardware error detected. Restarting.\n");
  466. /* Tell the device to stop sending interrupts */
  467. iwl_disable_interrupts(priv);
  468. priv->isr_stats.hw++;
  469. iwl_irq_handle_error(priv);
  470. handled |= CSR_INT_BIT_HW_ERR;
  471. return;
  472. }
  473. #ifdef CONFIG_IWLWIFI_DEBUG
  474. if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
  475. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  476. if (inta & CSR_INT_BIT_SCD) {
  477. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  478. "the frame/frames.\n");
  479. priv->isr_stats.sch++;
  480. }
  481. /* Alive notification via Rx interrupt will do the real work */
  482. if (inta & CSR_INT_BIT_ALIVE) {
  483. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  484. priv->isr_stats.alive++;
  485. }
  486. }
  487. #endif
  488. /* Safely ignore these bits for debug checks below */
  489. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  490. /* HW RF KILL switch toggled */
  491. if (inta & CSR_INT_BIT_RF_KILL) {
  492. int hw_rf_kill = 0;
  493. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  494. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  495. hw_rf_kill = 1;
  496. IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
  497. hw_rf_kill ? "disable radio" : "enable radio");
  498. priv->isr_stats.rfkill++;
  499. /* driver only loads ucode once setting the interface up.
  500. * the driver allows loading the ucode even if the radio
  501. * is killed. Hence update the killswitch state here. The
  502. * rfkill handler will care about restarting if needed.
  503. */
  504. if (!test_bit(STATUS_ALIVE, &priv->status)) {
  505. if (hw_rf_kill)
  506. set_bit(STATUS_RF_KILL_HW, &priv->status);
  507. else
  508. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  509. wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
  510. }
  511. handled |= CSR_INT_BIT_RF_KILL;
  512. }
  513. /* Chip got too hot and stopped itself */
  514. if (inta & CSR_INT_BIT_CT_KILL) {
  515. IWL_ERR(priv, "Microcode CT kill error detected.\n");
  516. priv->isr_stats.ctkill++;
  517. handled |= CSR_INT_BIT_CT_KILL;
  518. }
  519. /* Error detected by uCode */
  520. if (inta & CSR_INT_BIT_SW_ERR) {
  521. IWL_ERR(priv, "Microcode SW error detected. "
  522. " Restarting 0x%X.\n", inta);
  523. priv->isr_stats.sw++;
  524. iwl_irq_handle_error(priv);
  525. handled |= CSR_INT_BIT_SW_ERR;
  526. }
  527. /* uCode wakes up after power-down sleep */
  528. if (inta & CSR_INT_BIT_WAKEUP) {
  529. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  530. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  531. for (i = 0; i < priv->hw_params.max_txq_num; i++)
  532. iwl_txq_update_write_ptr(priv, &priv->txq[i]);
  533. priv->isr_stats.wakeup++;
  534. handled |= CSR_INT_BIT_WAKEUP;
  535. }
  536. /* All uCode command responses, including Tx command responses,
  537. * Rx "responses" (frame-received notification), and other
  538. * notifications from uCode come through here*/
  539. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  540. CSR_INT_BIT_RX_PERIODIC)) {
  541. IWL_DEBUG_ISR(priv, "Rx interrupt\n");
  542. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  543. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  544. iwl_write32(priv, CSR_FH_INT_STATUS,
  545. CSR_FH_INT_RX_MASK);
  546. }
  547. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  548. handled |= CSR_INT_BIT_RX_PERIODIC;
  549. iwl_write32(priv, CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  550. }
  551. /* Sending RX interrupt require many steps to be done in the
  552. * the device:
  553. * 1- write interrupt to current index in ICT table.
  554. * 2- dma RX frame.
  555. * 3- update RX shared data to indicate last write index.
  556. * 4- send interrupt.
  557. * This could lead to RX race, driver could receive RX interrupt
  558. * but the shared data changes does not reflect this;
  559. * periodic interrupt will detect any dangling Rx activity.
  560. */
  561. /* Disable periodic interrupt; we use it as just a one-shot. */
  562. iwl_write8(priv, CSR_INT_PERIODIC_REG,
  563. CSR_INT_PERIODIC_DIS);
  564. iwl_rx_handle(priv);
  565. /*
  566. * Enable periodic interrupt in 8 msec only if we received
  567. * real RX interrupt (instead of just periodic int), to catch
  568. * any dangling Rx interrupt. If it was just the periodic
  569. * interrupt, there was no dangling Rx activity, and no need
  570. * to extend the periodic interrupt; one-shot is enough.
  571. */
  572. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  573. iwl_write8(priv, CSR_INT_PERIODIC_REG,
  574. CSR_INT_PERIODIC_ENA);
  575. priv->isr_stats.rx++;
  576. }
  577. /* This "Tx" DMA channel is used only for loading uCode */
  578. if (inta & CSR_INT_BIT_FH_TX) {
  579. iwl_write32(priv, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  580. IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
  581. priv->isr_stats.tx++;
  582. handled |= CSR_INT_BIT_FH_TX;
  583. /* Wake up uCode load routine, now that load is complete */
  584. priv->ucode_write_complete = 1;
  585. wake_up_interruptible(&priv->wait_command_queue);
  586. }
  587. if (inta & ~handled) {
  588. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  589. priv->isr_stats.unhandled++;
  590. }
  591. if (inta & ~(priv->inta_mask)) {
  592. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  593. inta & ~priv->inta_mask);
  594. }
  595. /* Re-enable all interrupts */
  596. /* only Re-enable if disabled by irq */
  597. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  598. iwl_enable_interrupts(priv);
  599. /* Re-enable RF_KILL if it occurred */
  600. else if (handled & CSR_INT_BIT_RF_KILL)
  601. iwl_enable_rfkill_int(priv);
  602. }
  603. /******************************************************************************
  604. *
  605. * ICT functions
  606. *
  607. ******************************************************************************/
  608. #define ICT_COUNT (PAGE_SIZE/sizeof(u32))
  609. /* Free dram table */
  610. void iwl_free_isr_ict(struct iwl_priv *priv)
  611. {
  612. if (priv->ict_tbl_vir) {
  613. dma_free_coherent(priv->bus->dev,
  614. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  615. priv->ict_tbl_vir,
  616. priv->ict_tbl_dma);
  617. priv->ict_tbl_vir = NULL;
  618. memset(&priv->ict_tbl_dma, 0,
  619. sizeof(priv->ict_tbl_dma));
  620. memset(&priv->aligned_ict_tbl_dma, 0,
  621. sizeof(priv->aligned_ict_tbl_dma));
  622. }
  623. }
  624. /* allocate dram shared table it is a PAGE_SIZE aligned
  625. * also reset all data related to ICT table interrupt.
  626. */
  627. int iwl_alloc_isr_ict(struct iwl_priv *priv)
  628. {
  629. /* allocate shrared data table */
  630. priv->ict_tbl_vir =
  631. dma_alloc_coherent(priv->bus->dev,
  632. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  633. &priv->ict_tbl_dma, GFP_KERNEL);
  634. if (!priv->ict_tbl_vir)
  635. return -ENOMEM;
  636. /* align table to PAGE_SIZE boundary */
  637. priv->aligned_ict_tbl_dma =
  638. ALIGN(priv->ict_tbl_dma, PAGE_SIZE);
  639. IWL_DEBUG_ISR(priv, "ict dma addr %Lx dma aligned %Lx diff %d\n",
  640. (unsigned long long)priv->ict_tbl_dma,
  641. (unsigned long long)priv->aligned_ict_tbl_dma,
  642. (int)(priv->aligned_ict_tbl_dma -
  643. priv->ict_tbl_dma));
  644. priv->ict_tbl = priv->ict_tbl_vir +
  645. (priv->aligned_ict_tbl_dma -
  646. priv->ict_tbl_dma);
  647. IWL_DEBUG_ISR(priv, "ict vir addr %p vir aligned %p diff %d\n",
  648. priv->ict_tbl, priv->ict_tbl_vir,
  649. (int)(priv->aligned_ict_tbl_dma -
  650. priv->ict_tbl_dma));
  651. /* reset table and index to all 0 */
  652. memset(priv->ict_tbl_vir, 0,
  653. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE);
  654. priv->ict_index = 0;
  655. /* add periodic RX interrupt */
  656. priv->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  657. return 0;
  658. }
  659. /* Device is going up inform it about using ICT interrupt table,
  660. * also we need to tell the driver to start using ICT interrupt.
  661. */
  662. int iwl_reset_ict(struct iwl_priv *priv)
  663. {
  664. u32 val;
  665. unsigned long flags;
  666. if (!priv->ict_tbl_vir)
  667. return 0;
  668. spin_lock_irqsave(&priv->lock, flags);
  669. iwl_disable_interrupts(priv);
  670. memset(&priv->ict_tbl[0], 0, sizeof(u32) * ICT_COUNT);
  671. val = priv->aligned_ict_tbl_dma >> PAGE_SHIFT;
  672. val |= CSR_DRAM_INT_TBL_ENABLE;
  673. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  674. IWL_DEBUG_ISR(priv, "CSR_DRAM_INT_TBL_REG =0x%X "
  675. "aligned dma address %Lx\n",
  676. val,
  677. (unsigned long long)priv->aligned_ict_tbl_dma);
  678. iwl_write32(priv, CSR_DRAM_INT_TBL_REG, val);
  679. priv->use_ict = true;
  680. priv->ict_index = 0;
  681. iwl_write32(priv, CSR_INT, priv->inta_mask);
  682. iwl_enable_interrupts(priv);
  683. spin_unlock_irqrestore(&priv->lock, flags);
  684. return 0;
  685. }
  686. /* Device is going down disable ict interrupt usage */
  687. void iwl_disable_ict(struct iwl_priv *priv)
  688. {
  689. unsigned long flags;
  690. spin_lock_irqsave(&priv->lock, flags);
  691. priv->use_ict = false;
  692. spin_unlock_irqrestore(&priv->lock, flags);
  693. }
  694. static irqreturn_t iwl_isr(int irq, void *data)
  695. {
  696. struct iwl_priv *priv = data;
  697. u32 inta, inta_mask;
  698. unsigned long flags;
  699. #ifdef CONFIG_IWLWIFI_DEBUG
  700. u32 inta_fh;
  701. #endif
  702. if (!priv)
  703. return IRQ_NONE;
  704. spin_lock_irqsave(&priv->lock, flags);
  705. /* Disable (but don't clear!) interrupts here to avoid
  706. * back-to-back ISRs and sporadic interrupts from our NIC.
  707. * If we have something to service, the tasklet will re-enable ints.
  708. * If we *don't* have something, we'll re-enable before leaving here. */
  709. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  710. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  711. /* Discover which interrupts are active/pending */
  712. inta = iwl_read32(priv, CSR_INT);
  713. /* Ignore interrupt if there's nothing in NIC to service.
  714. * This may be due to IRQ shared with another device,
  715. * or due to sporadic interrupts thrown from our NIC. */
  716. if (!inta) {
  717. IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0\n");
  718. goto none;
  719. }
  720. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  721. /* Hardware disappeared. It might have already raised
  722. * an interrupt */
  723. IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  724. goto unplugged;
  725. }
  726. #ifdef CONFIG_IWLWIFI_DEBUG
  727. if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
  728. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  729. IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, "
  730. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  731. }
  732. #endif
  733. priv->inta |= inta;
  734. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  735. if (likely(inta))
  736. tasklet_schedule(&priv->irq_tasklet);
  737. else if (test_bit(STATUS_INT_ENABLED, &priv->status) &&
  738. !priv->inta)
  739. iwl_enable_interrupts(priv);
  740. unplugged:
  741. spin_unlock_irqrestore(&priv->lock, flags);
  742. return IRQ_HANDLED;
  743. none:
  744. /* re-enable interrupts here since we don't have anything to service. */
  745. /* only Re-enable if disabled by irq and no schedules tasklet. */
  746. if (test_bit(STATUS_INT_ENABLED, &priv->status) && !priv->inta)
  747. iwl_enable_interrupts(priv);
  748. spin_unlock_irqrestore(&priv->lock, flags);
  749. return IRQ_NONE;
  750. }
  751. /* interrupt handler using ict table, with this interrupt driver will
  752. * stop using INTA register to get device's interrupt, reading this register
  753. * is expensive, device will write interrupts in ICT dram table, increment
  754. * index then will fire interrupt to driver, driver will OR all ICT table
  755. * entries from current index up to table entry with 0 value. the result is
  756. * the interrupt we need to service, driver will set the entries back to 0 and
  757. * set index.
  758. */
  759. irqreturn_t iwl_isr_ict(int irq, void *data)
  760. {
  761. struct iwl_priv *priv = data;
  762. u32 inta, inta_mask;
  763. u32 val = 0;
  764. unsigned long flags;
  765. if (!priv)
  766. return IRQ_NONE;
  767. /* dram interrupt table not set yet,
  768. * use legacy interrupt.
  769. */
  770. if (!priv->use_ict)
  771. return iwl_isr(irq, data);
  772. spin_lock_irqsave(&priv->lock, flags);
  773. /* Disable (but don't clear!) interrupts here to avoid
  774. * back-to-back ISRs and sporadic interrupts from our NIC.
  775. * If we have something to service, the tasklet will re-enable ints.
  776. * If we *don't* have something, we'll re-enable before leaving here.
  777. */
  778. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  779. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  780. /* Ignore interrupt if there's nothing in NIC to service.
  781. * This may be due to IRQ shared with another device,
  782. * or due to sporadic interrupts thrown from our NIC. */
  783. if (!priv->ict_tbl[priv->ict_index]) {
  784. IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0\n");
  785. goto none;
  786. }
  787. /* read all entries that not 0 start with ict_index */
  788. while (priv->ict_tbl[priv->ict_index]) {
  789. val |= le32_to_cpu(priv->ict_tbl[priv->ict_index]);
  790. IWL_DEBUG_ISR(priv, "ICT index %d value 0x%08X\n",
  791. priv->ict_index,
  792. le32_to_cpu(
  793. priv->ict_tbl[priv->ict_index]));
  794. priv->ict_tbl[priv->ict_index] = 0;
  795. priv->ict_index = iwl_queue_inc_wrap(priv->ict_index,
  796. ICT_COUNT);
  797. }
  798. /* We should not get this value, just ignore it. */
  799. if (val == 0xffffffff)
  800. val = 0;
  801. /*
  802. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  803. * (bit 15 before shifting it to 31) to clear when using interrupt
  804. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  805. * so we use them to decide on the real state of the Rx bit.
  806. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  807. */
  808. if (val & 0xC0000)
  809. val |= 0x8000;
  810. inta = (0xff & val) | ((0xff00 & val) << 16);
  811. IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  812. inta, inta_mask, val);
  813. inta &= priv->inta_mask;
  814. priv->inta |= inta;
  815. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  816. if (likely(inta))
  817. tasklet_schedule(&priv->irq_tasklet);
  818. else if (test_bit(STATUS_INT_ENABLED, &priv->status) &&
  819. !priv->inta) {
  820. /* Allow interrupt if was disabled by this handler and
  821. * no tasklet was schedules, We should not enable interrupt,
  822. * tasklet will enable it.
  823. */
  824. iwl_enable_interrupts(priv);
  825. }
  826. spin_unlock_irqrestore(&priv->lock, flags);
  827. return IRQ_HANDLED;
  828. none:
  829. /* re-enable interrupts here since we don't have anything to service.
  830. * only Re-enable if disabled by irq.
  831. */
  832. if (test_bit(STATUS_INT_ENABLED, &priv->status) && !priv->inta)
  833. iwl_enable_interrupts(priv);
  834. spin_unlock_irqrestore(&priv->lock, flags);
  835. return IRQ_NONE;
  836. }