iwl-trans-rx-pcie.c 30 KB

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