iwl-trans-rx-pcie.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 (hw_params(priv).rx_page_order > 0)
  246. gfp_mask |= __GFP_COMP;
  247. /* Alloc a new receive buffer */
  248. page = alloc_pages(gfp_mask,
  249. hw_params(priv).rx_page_order);
  250. if (!page) {
  251. if (net_ratelimit())
  252. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  253. "order: %d\n",
  254. hw_params(priv).rx_page_order);
  255. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  256. net_ratelimit())
  257. IWL_CRIT(priv, "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(priv).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(priv->bus->dev, page, 0,
  281. PAGE_SIZE << hw_params(priv).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_priv *priv)
  294. {
  295. unsigned long flags;
  296. iwlagn_rx_allocate(priv, GFP_KERNEL);
  297. spin_lock_irqsave(&priv->lock, flags);
  298. iwlagn_rx_queue_restock(priv);
  299. spin_unlock_irqrestore(&priv->lock, flags);
  300. }
  301. static void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  302. {
  303. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  304. iwlagn_rx_queue_restock(priv);
  305. }
  306. void iwl_bg_rx_replenish(struct work_struct *data)
  307. {
  308. struct iwl_priv *priv =
  309. container_of(data, struct iwl_priv, rx_replenish);
  310. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  311. return;
  312. mutex_lock(&priv->mutex);
  313. iwlagn_rx_replenish(priv);
  314. mutex_unlock(&priv->mutex);
  315. }
  316. /**
  317. * iwl_rx_handle - Main entry function for receiving responses from uCode
  318. *
  319. * Uses the priv->rx_handlers callback function array to invoke
  320. * the appropriate handlers, including command responses,
  321. * frame-received notifications, and other notifications.
  322. */
  323. static void iwl_rx_handle(struct iwl_priv *priv)
  324. {
  325. struct iwl_rx_mem_buffer *rxb;
  326. struct iwl_rx_packet *pkt;
  327. struct iwl_rx_queue *rxq = &priv->rxq;
  328. u32 r, i;
  329. int reclaim;
  330. unsigned long flags;
  331. u8 fill_rx = 0;
  332. u32 count = 8;
  333. int total_empty;
  334. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  335. * buffer that the driver may process (last buffer filled by ucode). */
  336. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  337. i = rxq->read;
  338. /* Rx interrupt, but nothing sent from uCode */
  339. if (i == r)
  340. IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
  341. /* calculate total frames need to be restock after handling RX */
  342. total_empty = r - rxq->write_actual;
  343. if (total_empty < 0)
  344. total_empty += RX_QUEUE_SIZE;
  345. if (total_empty > (RX_QUEUE_SIZE / 2))
  346. fill_rx = 1;
  347. while (i != r) {
  348. int len;
  349. rxb = rxq->queue[i];
  350. /* If an RXB doesn't have a Rx queue slot associated with it,
  351. * then a bug has been introduced in the queue refilling
  352. * routines -- catch it here */
  353. if (WARN_ON(rxb == NULL)) {
  354. i = (i + 1) & RX_QUEUE_MASK;
  355. continue;
  356. }
  357. rxq->queue[i] = NULL;
  358. dma_unmap_page(priv->bus->dev, rxb->page_dma,
  359. PAGE_SIZE << hw_params(priv).rx_page_order,
  360. DMA_FROM_DEVICE);
  361. pkt = rxb_addr(rxb);
  362. IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
  363. i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  364. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  365. len += sizeof(u32); /* account for status word */
  366. trace_iwlwifi_dev_rx(priv, pkt, len);
  367. /* Reclaim a command buffer only if this packet is a response
  368. * to a (driver-originated) command.
  369. * If the packet (e.g. Rx frame) originated from uCode,
  370. * there is no command buffer to reclaim.
  371. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  372. * but apparently a few don't get set; catch them here. */
  373. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  374. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  375. (pkt->hdr.cmd != REPLY_RX) &&
  376. (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
  377. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  378. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  379. (pkt->hdr.cmd != REPLY_TX);
  380. iwl_rx_dispatch(priv, rxb);
  381. /*
  382. * XXX: After here, we should always check rxb->page
  383. * against NULL before touching it or its virtual
  384. * memory (pkt). Because some rx_handler might have
  385. * already taken or freed the pages.
  386. */
  387. if (reclaim) {
  388. /* Invoke any callbacks, transfer the buffer to caller,
  389. * and fire off the (possibly) blocking
  390. * trans_send_cmd()
  391. * as we reclaim the driver command queue */
  392. if (rxb->page)
  393. iwl_tx_cmd_complete(priv, rxb);
  394. else
  395. IWL_WARN(priv, "Claim null rxb?\n");
  396. }
  397. /* Reuse the page if possible. For notification packets and
  398. * SKBs that fail to Rx correctly, add them back into the
  399. * rx_free list for reuse later. */
  400. spin_lock_irqsave(&rxq->lock, flags);
  401. if (rxb->page != NULL) {
  402. rxb->page_dma = dma_map_page(priv->bus->dev, rxb->page,
  403. 0, PAGE_SIZE <<
  404. hw_params(priv).rx_page_order,
  405. DMA_FROM_DEVICE);
  406. list_add_tail(&rxb->list, &rxq->rx_free);
  407. rxq->free_count++;
  408. } else
  409. list_add_tail(&rxb->list, &rxq->rx_used);
  410. spin_unlock_irqrestore(&rxq->lock, flags);
  411. i = (i + 1) & RX_QUEUE_MASK;
  412. /* If there are a lot of unused frames,
  413. * restock the Rx queue so ucode wont assert. */
  414. if (fill_rx) {
  415. count++;
  416. if (count >= 8) {
  417. rxq->read = i;
  418. iwlagn_rx_replenish_now(priv);
  419. count = 0;
  420. }
  421. }
  422. }
  423. /* Backtrack one entry */
  424. rxq->read = i;
  425. if (fill_rx)
  426. iwlagn_rx_replenish_now(priv);
  427. else
  428. iwlagn_rx_queue_restock(priv);
  429. }
  430. /* tasklet for iwlagn interrupt */
  431. void iwl_irq_tasklet(struct iwl_priv *priv)
  432. {
  433. u32 inta = 0;
  434. u32 handled = 0;
  435. unsigned long flags;
  436. u32 i;
  437. #ifdef CONFIG_IWLWIFI_DEBUG
  438. u32 inta_mask;
  439. #endif
  440. spin_lock_irqsave(&priv->lock, flags);
  441. /* Ack/clear/reset pending uCode interrupts.
  442. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  443. */
  444. /* There is a hardware bug in the interrupt mask function that some
  445. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  446. * they are disabled in the CSR_INT_MASK register. Furthermore the
  447. * ICT interrupt handling mechanism has another bug that might cause
  448. * these unmasked interrupts fail to be detected. We workaround the
  449. * hardware bugs here by ACKing all the possible interrupts so that
  450. * interrupt coalescing can still be achieved.
  451. */
  452. iwl_write32(priv, CSR_INT, priv->inta | ~priv->inta_mask);
  453. inta = priv->inta;
  454. #ifdef CONFIG_IWLWIFI_DEBUG
  455. if (iwl_get_debug_level(priv->shrd) & IWL_DL_ISR) {
  456. /* just for debug */
  457. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  458. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x\n ",
  459. inta, inta_mask);
  460. }
  461. #endif
  462. spin_unlock_irqrestore(&priv->lock, flags);
  463. /* saved interrupt in inta variable now we can reset priv->inta */
  464. priv->inta = 0;
  465. /* Now service all interrupt bits discovered above. */
  466. if (inta & CSR_INT_BIT_HW_ERR) {
  467. IWL_ERR(priv, "Hardware error detected. Restarting.\n");
  468. /* Tell the device to stop sending interrupts */
  469. iwl_disable_interrupts(priv);
  470. priv->isr_stats.hw++;
  471. iwl_irq_handle_error(priv);
  472. handled |= CSR_INT_BIT_HW_ERR;
  473. return;
  474. }
  475. #ifdef CONFIG_IWLWIFI_DEBUG
  476. if (iwl_get_debug_level(priv->shrd) & (IWL_DL_ISR)) {
  477. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  478. if (inta & CSR_INT_BIT_SCD) {
  479. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  480. "the frame/frames.\n");
  481. priv->isr_stats.sch++;
  482. }
  483. /* Alive notification via Rx interrupt will do the real work */
  484. if (inta & CSR_INT_BIT_ALIVE) {
  485. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  486. priv->isr_stats.alive++;
  487. }
  488. }
  489. #endif
  490. /* Safely ignore these bits for debug checks below */
  491. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  492. /* HW RF KILL switch toggled */
  493. if (inta & CSR_INT_BIT_RF_KILL) {
  494. int hw_rf_kill = 0;
  495. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  496. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  497. hw_rf_kill = 1;
  498. IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
  499. hw_rf_kill ? "disable radio" : "enable radio");
  500. priv->isr_stats.rfkill++;
  501. /* driver only loads ucode once setting the interface up.
  502. * the driver allows loading the ucode even if the radio
  503. * is killed. Hence update the killswitch state here. The
  504. * rfkill handler will care about restarting if needed.
  505. */
  506. if (!test_bit(STATUS_ALIVE, &priv->status)) {
  507. if (hw_rf_kill)
  508. set_bit(STATUS_RF_KILL_HW, &priv->status);
  509. else
  510. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  511. wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
  512. }
  513. handled |= CSR_INT_BIT_RF_KILL;
  514. }
  515. /* Chip got too hot and stopped itself */
  516. if (inta & CSR_INT_BIT_CT_KILL) {
  517. IWL_ERR(priv, "Microcode CT kill error detected.\n");
  518. priv->isr_stats.ctkill++;
  519. handled |= CSR_INT_BIT_CT_KILL;
  520. }
  521. /* Error detected by uCode */
  522. if (inta & CSR_INT_BIT_SW_ERR) {
  523. IWL_ERR(priv, "Microcode SW error detected. "
  524. " Restarting 0x%X.\n", inta);
  525. priv->isr_stats.sw++;
  526. iwl_irq_handle_error(priv);
  527. handled |= CSR_INT_BIT_SW_ERR;
  528. }
  529. /* uCode wakes up after power-down sleep */
  530. if (inta & CSR_INT_BIT_WAKEUP) {
  531. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  532. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  533. for (i = 0; i < hw_params(priv).max_txq_num; i++)
  534. iwl_txq_update_write_ptr(priv, &priv->txq[i]);
  535. priv->isr_stats.wakeup++;
  536. handled |= CSR_INT_BIT_WAKEUP;
  537. }
  538. /* All uCode command responses, including Tx command responses,
  539. * Rx "responses" (frame-received notification), and other
  540. * notifications from uCode come through here*/
  541. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  542. CSR_INT_BIT_RX_PERIODIC)) {
  543. IWL_DEBUG_ISR(priv, "Rx interrupt\n");
  544. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  545. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  546. iwl_write32(priv, CSR_FH_INT_STATUS,
  547. CSR_FH_INT_RX_MASK);
  548. }
  549. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  550. handled |= CSR_INT_BIT_RX_PERIODIC;
  551. iwl_write32(priv, CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  552. }
  553. /* Sending RX interrupt require many steps to be done in the
  554. * the device:
  555. * 1- write interrupt to current index in ICT table.
  556. * 2- dma RX frame.
  557. * 3- update RX shared data to indicate last write index.
  558. * 4- send interrupt.
  559. * This could lead to RX race, driver could receive RX interrupt
  560. * but the shared data changes does not reflect this;
  561. * periodic interrupt will detect any dangling Rx activity.
  562. */
  563. /* Disable periodic interrupt; we use it as just a one-shot. */
  564. iwl_write8(priv, CSR_INT_PERIODIC_REG,
  565. CSR_INT_PERIODIC_DIS);
  566. iwl_rx_handle(priv);
  567. /*
  568. * Enable periodic interrupt in 8 msec only if we received
  569. * real RX interrupt (instead of just periodic int), to catch
  570. * any dangling Rx interrupt. If it was just the periodic
  571. * interrupt, there was no dangling Rx activity, and no need
  572. * to extend the periodic interrupt; one-shot is enough.
  573. */
  574. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  575. iwl_write8(priv, CSR_INT_PERIODIC_REG,
  576. CSR_INT_PERIODIC_ENA);
  577. priv->isr_stats.rx++;
  578. }
  579. /* This "Tx" DMA channel is used only for loading uCode */
  580. if (inta & CSR_INT_BIT_FH_TX) {
  581. iwl_write32(priv, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  582. IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
  583. priv->isr_stats.tx++;
  584. handled |= CSR_INT_BIT_FH_TX;
  585. /* Wake up uCode load routine, now that load is complete */
  586. priv->ucode_write_complete = 1;
  587. wake_up_interruptible(&priv->wait_command_queue);
  588. }
  589. if (inta & ~handled) {
  590. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  591. priv->isr_stats.unhandled++;
  592. }
  593. if (inta & ~(priv->inta_mask)) {
  594. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  595. inta & ~priv->inta_mask);
  596. }
  597. /* Re-enable all interrupts */
  598. /* only Re-enable if disabled by irq */
  599. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  600. iwl_enable_interrupts(priv);
  601. /* Re-enable RF_KILL if it occurred */
  602. else if (handled & CSR_INT_BIT_RF_KILL)
  603. iwl_enable_rfkill_int(priv);
  604. }
  605. /******************************************************************************
  606. *
  607. * ICT functions
  608. *
  609. ******************************************************************************/
  610. #define ICT_COUNT (PAGE_SIZE/sizeof(u32))
  611. /* Free dram table */
  612. void iwl_free_isr_ict(struct iwl_priv *priv)
  613. {
  614. if (priv->ict_tbl_vir) {
  615. dma_free_coherent(priv->bus->dev,
  616. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  617. priv->ict_tbl_vir,
  618. priv->ict_tbl_dma);
  619. priv->ict_tbl_vir = NULL;
  620. memset(&priv->ict_tbl_dma, 0,
  621. sizeof(priv->ict_tbl_dma));
  622. memset(&priv->aligned_ict_tbl_dma, 0,
  623. sizeof(priv->aligned_ict_tbl_dma));
  624. }
  625. }
  626. /* allocate dram shared table it is a PAGE_SIZE aligned
  627. * also reset all data related to ICT table interrupt.
  628. */
  629. int iwl_alloc_isr_ict(struct iwl_priv *priv)
  630. {
  631. /* allocate shrared data table */
  632. priv->ict_tbl_vir =
  633. dma_alloc_coherent(priv->bus->dev,
  634. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  635. &priv->ict_tbl_dma, GFP_KERNEL);
  636. if (!priv->ict_tbl_vir)
  637. return -ENOMEM;
  638. /* align table to PAGE_SIZE boundary */
  639. priv->aligned_ict_tbl_dma =
  640. ALIGN(priv->ict_tbl_dma, PAGE_SIZE);
  641. IWL_DEBUG_ISR(priv, "ict dma addr %Lx dma aligned %Lx diff %d\n",
  642. (unsigned long long)priv->ict_tbl_dma,
  643. (unsigned long long)priv->aligned_ict_tbl_dma,
  644. (int)(priv->aligned_ict_tbl_dma -
  645. priv->ict_tbl_dma));
  646. priv->ict_tbl = priv->ict_tbl_vir +
  647. (priv->aligned_ict_tbl_dma -
  648. priv->ict_tbl_dma);
  649. IWL_DEBUG_ISR(priv, "ict vir addr %p vir aligned %p diff %d\n",
  650. priv->ict_tbl, priv->ict_tbl_vir,
  651. (int)(priv->aligned_ict_tbl_dma -
  652. priv->ict_tbl_dma));
  653. /* reset table and index to all 0 */
  654. memset(priv->ict_tbl_vir, 0,
  655. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE);
  656. priv->ict_index = 0;
  657. /* add periodic RX interrupt */
  658. priv->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  659. return 0;
  660. }
  661. /* Device is going up inform it about using ICT interrupt table,
  662. * also we need to tell the driver to start using ICT interrupt.
  663. */
  664. int iwl_reset_ict(struct iwl_priv *priv)
  665. {
  666. u32 val;
  667. unsigned long flags;
  668. if (!priv->ict_tbl_vir)
  669. return 0;
  670. spin_lock_irqsave(&priv->lock, flags);
  671. iwl_disable_interrupts(priv);
  672. memset(&priv->ict_tbl[0], 0, sizeof(u32) * ICT_COUNT);
  673. val = priv->aligned_ict_tbl_dma >> PAGE_SHIFT;
  674. val |= CSR_DRAM_INT_TBL_ENABLE;
  675. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  676. IWL_DEBUG_ISR(priv, "CSR_DRAM_INT_TBL_REG =0x%X "
  677. "aligned dma address %Lx\n",
  678. val,
  679. (unsigned long long)priv->aligned_ict_tbl_dma);
  680. iwl_write32(priv, CSR_DRAM_INT_TBL_REG, val);
  681. priv->use_ict = true;
  682. priv->ict_index = 0;
  683. iwl_write32(priv, CSR_INT, priv->inta_mask);
  684. iwl_enable_interrupts(priv);
  685. spin_unlock_irqrestore(&priv->lock, flags);
  686. return 0;
  687. }
  688. /* Device is going down disable ict interrupt usage */
  689. void iwl_disable_ict(struct iwl_priv *priv)
  690. {
  691. unsigned long flags;
  692. spin_lock_irqsave(&priv->lock, flags);
  693. priv->use_ict = false;
  694. spin_unlock_irqrestore(&priv->lock, flags);
  695. }
  696. static irqreturn_t iwl_isr(int irq, void *data)
  697. {
  698. struct iwl_priv *priv = data;
  699. u32 inta, inta_mask;
  700. unsigned long flags;
  701. #ifdef CONFIG_IWLWIFI_DEBUG
  702. u32 inta_fh;
  703. #endif
  704. if (!priv)
  705. return IRQ_NONE;
  706. spin_lock_irqsave(&priv->lock, flags);
  707. /* Disable (but don't clear!) interrupts here to avoid
  708. * back-to-back ISRs and sporadic interrupts from our NIC.
  709. * If we have something to service, the tasklet will re-enable ints.
  710. * If we *don't* have something, we'll re-enable before leaving here. */
  711. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  712. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  713. /* Discover which interrupts are active/pending */
  714. inta = iwl_read32(priv, CSR_INT);
  715. /* Ignore interrupt if there's nothing in NIC to service.
  716. * This may be due to IRQ shared with another device,
  717. * or due to sporadic interrupts thrown from our NIC. */
  718. if (!inta) {
  719. IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0\n");
  720. goto none;
  721. }
  722. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  723. /* Hardware disappeared. It might have already raised
  724. * an interrupt */
  725. IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  726. goto unplugged;
  727. }
  728. #ifdef CONFIG_IWLWIFI_DEBUG
  729. if (iwl_get_debug_level(priv->shrd) & (IWL_DL_ISR)) {
  730. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  731. IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, "
  732. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  733. }
  734. #endif
  735. priv->inta |= inta;
  736. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  737. if (likely(inta))
  738. tasklet_schedule(&priv->irq_tasklet);
  739. else if (test_bit(STATUS_INT_ENABLED, &priv->status) &&
  740. !priv->inta)
  741. iwl_enable_interrupts(priv);
  742. unplugged:
  743. spin_unlock_irqrestore(&priv->lock, flags);
  744. return IRQ_HANDLED;
  745. none:
  746. /* re-enable interrupts here since we don't have anything to service. */
  747. /* only Re-enable if disabled by irq and no schedules tasklet. */
  748. if (test_bit(STATUS_INT_ENABLED, &priv->status) && !priv->inta)
  749. iwl_enable_interrupts(priv);
  750. spin_unlock_irqrestore(&priv->lock, flags);
  751. return IRQ_NONE;
  752. }
  753. /* interrupt handler using ict table, with this interrupt driver will
  754. * stop using INTA register to get device's interrupt, reading this register
  755. * is expensive, device will write interrupts in ICT dram table, increment
  756. * index then will fire interrupt to driver, driver will OR all ICT table
  757. * entries from current index up to table entry with 0 value. the result is
  758. * the interrupt we need to service, driver will set the entries back to 0 and
  759. * set index.
  760. */
  761. irqreturn_t iwl_isr_ict(int irq, void *data)
  762. {
  763. struct iwl_priv *priv = data;
  764. u32 inta, inta_mask;
  765. u32 val = 0;
  766. unsigned long flags;
  767. if (!priv)
  768. return IRQ_NONE;
  769. /* dram interrupt table not set yet,
  770. * use legacy interrupt.
  771. */
  772. if (!priv->use_ict)
  773. return iwl_isr(irq, data);
  774. spin_lock_irqsave(&priv->lock, flags);
  775. /* Disable (but don't clear!) interrupts here to avoid
  776. * back-to-back ISRs and sporadic interrupts from our NIC.
  777. * If we have something to service, the tasklet will re-enable ints.
  778. * If we *don't* have something, we'll re-enable before leaving here.
  779. */
  780. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  781. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  782. /* Ignore interrupt if there's nothing in NIC to service.
  783. * This may be due to IRQ shared with another device,
  784. * or due to sporadic interrupts thrown from our NIC. */
  785. if (!priv->ict_tbl[priv->ict_index]) {
  786. IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0\n");
  787. goto none;
  788. }
  789. /* read all entries that not 0 start with ict_index */
  790. while (priv->ict_tbl[priv->ict_index]) {
  791. val |= le32_to_cpu(priv->ict_tbl[priv->ict_index]);
  792. IWL_DEBUG_ISR(priv, "ICT index %d value 0x%08X\n",
  793. priv->ict_index,
  794. le32_to_cpu(
  795. priv->ict_tbl[priv->ict_index]));
  796. priv->ict_tbl[priv->ict_index] = 0;
  797. priv->ict_index = iwl_queue_inc_wrap(priv->ict_index,
  798. ICT_COUNT);
  799. }
  800. /* We should not get this value, just ignore it. */
  801. if (val == 0xffffffff)
  802. val = 0;
  803. /*
  804. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  805. * (bit 15 before shifting it to 31) to clear when using interrupt
  806. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  807. * so we use them to decide on the real state of the Rx bit.
  808. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  809. */
  810. if (val & 0xC0000)
  811. val |= 0x8000;
  812. inta = (0xff & val) | ((0xff00 & val) << 16);
  813. IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  814. inta, inta_mask, val);
  815. inta &= priv->inta_mask;
  816. priv->inta |= inta;
  817. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  818. if (likely(inta))
  819. tasklet_schedule(&priv->irq_tasklet);
  820. else if (test_bit(STATUS_INT_ENABLED, &priv->status) &&
  821. !priv->inta) {
  822. /* Allow interrupt if was disabled by this handler and
  823. * no tasklet was schedules, We should not enable interrupt,
  824. * tasklet will enable it.
  825. */
  826. iwl_enable_interrupts(priv);
  827. }
  828. spin_unlock_irqrestore(&priv->lock, flags);
  829. return IRQ_HANDLED;
  830. none:
  831. /* re-enable interrupts here since we don't have anything to service.
  832. * only Re-enable if disabled by irq.
  833. */
  834. if (test_bit(STATUS_INT_ENABLED, &priv->status) && !priv->inta)
  835. iwl_enable_interrupts(priv);
  836. spin_unlock_irqrestore(&priv->lock, flags);
  837. return IRQ_NONE;
  838. }