iwl-trans-rx-pcie.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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_trans *trans)
  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. struct iwl_trans_pcie *trans_pcie =
  447. IWL_TRANS_GET_PCIE_TRANS(trans);
  448. spin_lock_irqsave(&trans->shrd->lock, flags);
  449. /* Ack/clear/reset pending uCode interrupts.
  450. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  451. */
  452. /* There is a hardware bug in the interrupt mask function that some
  453. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  454. * they are disabled in the CSR_INT_MASK register. Furthermore the
  455. * ICT interrupt handling mechanism has another bug that might cause
  456. * these unmasked interrupts fail to be detected. We workaround the
  457. * hardware bugs here by ACKing all the possible interrupts so that
  458. * interrupt coalescing can still be achieved.
  459. */
  460. iwl_write32(priv(trans), CSR_INT,
  461. trans_pcie->inta | ~trans_pcie->inta_mask);
  462. inta = trans_pcie->inta;
  463. #ifdef CONFIG_IWLWIFI_DEBUG
  464. if (iwl_get_debug_level(trans->shrd) & IWL_DL_ISR) {
  465. /* just for debug */
  466. inta_mask = iwl_read32(priv(trans), CSR_INT_MASK);
  467. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
  468. inta, inta_mask);
  469. }
  470. #endif
  471. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  472. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  473. trans_pcie->inta = 0;
  474. /* Now service all interrupt bits discovered above. */
  475. if (inta & CSR_INT_BIT_HW_ERR) {
  476. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  477. /* Tell the device to stop sending interrupts */
  478. iwl_disable_interrupts(trans);
  479. priv(trans)->isr_stats.hw++;
  480. iwl_irq_handle_error(priv(trans));
  481. handled |= CSR_INT_BIT_HW_ERR;
  482. return;
  483. }
  484. #ifdef CONFIG_IWLWIFI_DEBUG
  485. if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
  486. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  487. if (inta & CSR_INT_BIT_SCD) {
  488. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  489. "the frame/frames.\n");
  490. priv(trans)->isr_stats.sch++;
  491. }
  492. /* Alive notification via Rx interrupt will do the real work */
  493. if (inta & CSR_INT_BIT_ALIVE) {
  494. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  495. priv(trans)->isr_stats.alive++;
  496. }
  497. }
  498. #endif
  499. /* Safely ignore these bits for debug checks below */
  500. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  501. /* HW RF KILL switch toggled */
  502. if (inta & CSR_INT_BIT_RF_KILL) {
  503. int hw_rf_kill = 0;
  504. if (!(iwl_read32(priv(trans), CSR_GP_CNTRL) &
  505. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  506. hw_rf_kill = 1;
  507. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  508. hw_rf_kill ? "disable radio" : "enable radio");
  509. priv(trans)->isr_stats.rfkill++;
  510. /* driver only loads ucode once setting the interface up.
  511. * the driver allows loading the ucode even if the radio
  512. * is killed. Hence update the killswitch state here. The
  513. * rfkill handler will care about restarting if needed.
  514. */
  515. if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
  516. if (hw_rf_kill)
  517. set_bit(STATUS_RF_KILL_HW,
  518. &trans->shrd->status);
  519. else
  520. clear_bit(STATUS_RF_KILL_HW,
  521. &trans->shrd->status);
  522. wiphy_rfkill_set_hw_state(priv(trans)->hw->wiphy,
  523. hw_rf_kill);
  524. }
  525. handled |= CSR_INT_BIT_RF_KILL;
  526. }
  527. /* Chip got too hot and stopped itself */
  528. if (inta & CSR_INT_BIT_CT_KILL) {
  529. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  530. priv(trans)->isr_stats.ctkill++;
  531. handled |= CSR_INT_BIT_CT_KILL;
  532. }
  533. /* Error detected by uCode */
  534. if (inta & CSR_INT_BIT_SW_ERR) {
  535. IWL_ERR(trans, "Microcode SW error detected. "
  536. " Restarting 0x%X.\n", inta);
  537. priv(trans)->isr_stats.sw++;
  538. iwl_irq_handle_error(priv(trans));
  539. handled |= CSR_INT_BIT_SW_ERR;
  540. }
  541. /* uCode wakes up after power-down sleep */
  542. if (inta & CSR_INT_BIT_WAKEUP) {
  543. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  544. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  545. for (i = 0; i < hw_params(trans).max_txq_num; i++)
  546. iwl_txq_update_write_ptr(priv(trans),
  547. &priv(trans)->txq[i]);
  548. priv(trans)->isr_stats.wakeup++;
  549. handled |= CSR_INT_BIT_WAKEUP;
  550. }
  551. /* All uCode command responses, including Tx command responses,
  552. * Rx "responses" (frame-received notification), and other
  553. * notifications from uCode come through here*/
  554. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  555. CSR_INT_BIT_RX_PERIODIC)) {
  556. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  557. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  558. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  559. iwl_write32(priv(trans), CSR_FH_INT_STATUS,
  560. CSR_FH_INT_RX_MASK);
  561. }
  562. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  563. handled |= CSR_INT_BIT_RX_PERIODIC;
  564. iwl_write32(priv(trans),
  565. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  566. }
  567. /* Sending RX interrupt require many steps to be done in the
  568. * the device:
  569. * 1- write interrupt to current index in ICT table.
  570. * 2- dma RX frame.
  571. * 3- update RX shared data to indicate last write index.
  572. * 4- send interrupt.
  573. * This could lead to RX race, driver could receive RX interrupt
  574. * but the shared data changes does not reflect this;
  575. * periodic interrupt will detect any dangling Rx activity.
  576. */
  577. /* Disable periodic interrupt; we use it as just a one-shot. */
  578. iwl_write8(priv(trans), CSR_INT_PERIODIC_REG,
  579. CSR_INT_PERIODIC_DIS);
  580. iwl_rx_handle(trans);
  581. /*
  582. * Enable periodic interrupt in 8 msec only if we received
  583. * real RX interrupt (instead of just periodic int), to catch
  584. * any dangling Rx interrupt. If it was just the periodic
  585. * interrupt, there was no dangling Rx activity, and no need
  586. * to extend the periodic interrupt; one-shot is enough.
  587. */
  588. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  589. iwl_write8(priv(trans), CSR_INT_PERIODIC_REG,
  590. CSR_INT_PERIODIC_ENA);
  591. priv(trans)->isr_stats.rx++;
  592. }
  593. /* This "Tx" DMA channel is used only for loading uCode */
  594. if (inta & CSR_INT_BIT_FH_TX) {
  595. iwl_write32(priv(trans), CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  596. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  597. priv(trans)->isr_stats.tx++;
  598. handled |= CSR_INT_BIT_FH_TX;
  599. /* Wake up uCode load routine, now that load is complete */
  600. priv(trans)->ucode_write_complete = 1;
  601. wake_up_interruptible(&priv(trans)->wait_command_queue);
  602. }
  603. if (inta & ~handled) {
  604. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  605. priv(trans)->isr_stats.unhandled++;
  606. }
  607. if (inta & ~(trans_pcie->inta_mask)) {
  608. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  609. inta & ~trans_pcie->inta_mask);
  610. }
  611. /* Re-enable all interrupts */
  612. /* only Re-enable if disabled by irq */
  613. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
  614. iwl_enable_interrupts(trans);
  615. /* Re-enable RF_KILL if it occurred */
  616. else if (handled & CSR_INT_BIT_RF_KILL)
  617. iwl_enable_rfkill_int(priv(trans));
  618. }
  619. /******************************************************************************
  620. *
  621. * ICT functions
  622. *
  623. ******************************************************************************/
  624. #define ICT_COUNT (PAGE_SIZE/sizeof(u32))
  625. /* Free dram table */
  626. void iwl_free_isr_ict(struct iwl_trans *trans)
  627. {
  628. struct iwl_trans_pcie *trans_pcie =
  629. IWL_TRANS_GET_PCIE_TRANS(trans);
  630. if (trans_pcie->ict_tbl_vir) {
  631. dma_free_coherent(bus(trans)->dev,
  632. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  633. trans_pcie->ict_tbl_vir,
  634. trans_pcie->ict_tbl_dma);
  635. trans_pcie->ict_tbl_vir = NULL;
  636. memset(&trans_pcie->ict_tbl_dma, 0,
  637. sizeof(trans_pcie->ict_tbl_dma));
  638. memset(&trans_pcie->aligned_ict_tbl_dma, 0,
  639. sizeof(trans_pcie->aligned_ict_tbl_dma));
  640. }
  641. }
  642. /* allocate dram shared table it is a PAGE_SIZE aligned
  643. * also reset all data related to ICT table interrupt.
  644. */
  645. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  646. {
  647. struct iwl_trans_pcie *trans_pcie =
  648. IWL_TRANS_GET_PCIE_TRANS(trans);
  649. /* allocate shrared data table */
  650. trans_pcie->ict_tbl_vir =
  651. dma_alloc_coherent(bus(trans)->dev,
  652. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  653. &trans_pcie->ict_tbl_dma, GFP_KERNEL);
  654. if (!trans_pcie->ict_tbl_vir)
  655. return -ENOMEM;
  656. /* align table to PAGE_SIZE boundary */
  657. trans_pcie->aligned_ict_tbl_dma =
  658. ALIGN(trans_pcie->ict_tbl_dma, PAGE_SIZE);
  659. IWL_DEBUG_ISR(trans, "ict dma addr %Lx dma aligned %Lx diff %d\n",
  660. (unsigned long long)trans_pcie->ict_tbl_dma,
  661. (unsigned long long)trans_pcie->aligned_ict_tbl_dma,
  662. (int)(trans_pcie->aligned_ict_tbl_dma -
  663. trans_pcie->ict_tbl_dma));
  664. trans_pcie->ict_tbl = trans_pcie->ict_tbl_vir +
  665. (trans_pcie->aligned_ict_tbl_dma -
  666. trans_pcie->ict_tbl_dma);
  667. IWL_DEBUG_ISR(trans, "ict vir addr %p vir aligned %p diff %d\n",
  668. trans_pcie->ict_tbl, trans_pcie->ict_tbl_vir,
  669. (int)(trans_pcie->aligned_ict_tbl_dma -
  670. trans_pcie->ict_tbl_dma));
  671. /* reset table and index to all 0 */
  672. memset(trans_pcie->ict_tbl_vir, 0,
  673. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE);
  674. trans_pcie->ict_index = 0;
  675. /* add periodic RX interrupt */
  676. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  677. return 0;
  678. }
  679. /* Device is going up inform it about using ICT interrupt table,
  680. * also we need to tell the driver to start using ICT interrupt.
  681. */
  682. int iwl_reset_ict(struct iwl_priv *priv)
  683. {
  684. u32 val;
  685. unsigned long flags;
  686. struct iwl_trans *trans = trans(priv);
  687. struct iwl_trans_pcie *trans_pcie =
  688. IWL_TRANS_GET_PCIE_TRANS(trans);
  689. if (!trans_pcie->ict_tbl_vir)
  690. return 0;
  691. spin_lock_irqsave(&trans->shrd->lock, flags);
  692. iwl_disable_interrupts(trans);
  693. memset(&trans_pcie->ict_tbl[0], 0, sizeof(u32) * ICT_COUNT);
  694. val = trans_pcie->aligned_ict_tbl_dma >> PAGE_SHIFT;
  695. val |= CSR_DRAM_INT_TBL_ENABLE;
  696. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  697. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%X "
  698. "aligned dma address %Lx\n",
  699. val,
  700. (unsigned long long)trans_pcie->aligned_ict_tbl_dma);
  701. iwl_write32(priv(trans), CSR_DRAM_INT_TBL_REG, val);
  702. trans_pcie->use_ict = true;
  703. trans_pcie->ict_index = 0;
  704. iwl_write32(priv(trans), CSR_INT, trans_pcie->inta_mask);
  705. iwl_enable_interrupts(trans);
  706. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  707. return 0;
  708. }
  709. /* Device is going down disable ict interrupt usage */
  710. void iwl_disable_ict(struct iwl_trans *trans)
  711. {
  712. struct iwl_trans_pcie *trans_pcie =
  713. IWL_TRANS_GET_PCIE_TRANS(trans);
  714. unsigned long flags;
  715. spin_lock_irqsave(&trans->shrd->lock, flags);
  716. trans_pcie->use_ict = false;
  717. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  718. }
  719. static irqreturn_t iwl_isr(int irq, void *data)
  720. {
  721. struct iwl_trans *trans = data;
  722. struct iwl_trans_pcie *trans_pcie;
  723. u32 inta, inta_mask;
  724. unsigned long flags;
  725. #ifdef CONFIG_IWLWIFI_DEBUG
  726. u32 inta_fh;
  727. #endif
  728. if (!trans)
  729. return IRQ_NONE;
  730. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  731. spin_lock_irqsave(&trans->shrd->lock, flags);
  732. /* Disable (but don't clear!) interrupts here to avoid
  733. * back-to-back ISRs and sporadic interrupts from our NIC.
  734. * If we have something to service, the tasklet will re-enable ints.
  735. * If we *don't* have something, we'll re-enable before leaving here. */
  736. inta_mask = iwl_read32(priv(trans), CSR_INT_MASK); /* just for debug */
  737. iwl_write32(priv(trans), CSR_INT_MASK, 0x00000000);
  738. /* Discover which interrupts are active/pending */
  739. inta = iwl_read32(priv(trans), CSR_INT);
  740. /* Ignore interrupt if there's nothing in NIC to service.
  741. * This may be due to IRQ shared with another device,
  742. * or due to sporadic interrupts thrown from our NIC. */
  743. if (!inta) {
  744. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  745. goto none;
  746. }
  747. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  748. /* Hardware disappeared. It might have already raised
  749. * an interrupt */
  750. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  751. goto unplugged;
  752. }
  753. #ifdef CONFIG_IWLWIFI_DEBUG
  754. if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
  755. inta_fh = iwl_read32(priv(trans), CSR_FH_INT_STATUS);
  756. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  757. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  758. }
  759. #endif
  760. trans_pcie->inta |= inta;
  761. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  762. if (likely(inta))
  763. tasklet_schedule(&trans_pcie->irq_tasklet);
  764. else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  765. !trans_pcie->inta)
  766. iwl_enable_interrupts(trans);
  767. unplugged:
  768. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  769. return IRQ_HANDLED;
  770. none:
  771. /* re-enable interrupts here since we don't have anything to service. */
  772. /* only Re-enable if disabled by irq and no schedules tasklet. */
  773. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  774. !trans_pcie->inta)
  775. iwl_enable_interrupts(trans);
  776. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  777. return IRQ_NONE;
  778. }
  779. /* interrupt handler using ict table, with this interrupt driver will
  780. * stop using INTA register to get device's interrupt, reading this register
  781. * is expensive, device will write interrupts in ICT dram table, increment
  782. * index then will fire interrupt to driver, driver will OR all ICT table
  783. * entries from current index up to table entry with 0 value. the result is
  784. * the interrupt we need to service, driver will set the entries back to 0 and
  785. * set index.
  786. */
  787. irqreturn_t iwl_isr_ict(int irq, void *data)
  788. {
  789. struct iwl_trans *trans = data;
  790. struct iwl_trans_pcie *trans_pcie;
  791. u32 inta, inta_mask;
  792. u32 val = 0;
  793. unsigned long flags;
  794. if (!trans)
  795. return IRQ_NONE;
  796. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  797. /* dram interrupt table not set yet,
  798. * use legacy interrupt.
  799. */
  800. if (!trans_pcie->use_ict)
  801. return iwl_isr(irq, data);
  802. spin_lock_irqsave(&trans->shrd->lock, flags);
  803. /* Disable (but don't clear!) interrupts here to avoid
  804. * back-to-back ISRs and sporadic interrupts from our NIC.
  805. * If we have something to service, the tasklet will re-enable ints.
  806. * If we *don't* have something, we'll re-enable before leaving here.
  807. */
  808. inta_mask = iwl_read32(priv(trans), CSR_INT_MASK); /* just for debug */
  809. iwl_write32(priv(trans), CSR_INT_MASK, 0x00000000);
  810. /* Ignore interrupt if there's nothing in NIC to service.
  811. * This may be due to IRQ shared with another device,
  812. * or due to sporadic interrupts thrown from our NIC. */
  813. if (!trans_pcie->ict_tbl[trans_pcie->ict_index]) {
  814. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  815. goto none;
  816. }
  817. /* read all entries that not 0 start with ict_index */
  818. while (trans_pcie->ict_tbl[trans_pcie->ict_index]) {
  819. val |= le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  820. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  821. trans_pcie->ict_index,
  822. le32_to_cpu(
  823. trans_pcie->ict_tbl[trans_pcie->ict_index]));
  824. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  825. trans_pcie->ict_index =
  826. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  827. }
  828. /* We should not get this value, just ignore it. */
  829. if (val == 0xffffffff)
  830. val = 0;
  831. /*
  832. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  833. * (bit 15 before shifting it to 31) to clear when using interrupt
  834. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  835. * so we use them to decide on the real state of the Rx bit.
  836. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  837. */
  838. if (val & 0xC0000)
  839. val |= 0x8000;
  840. inta = (0xff & val) | ((0xff00 & val) << 16);
  841. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  842. inta, inta_mask, val);
  843. inta &= trans_pcie->inta_mask;
  844. trans_pcie->inta |= inta;
  845. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  846. if (likely(inta))
  847. tasklet_schedule(&trans_pcie->irq_tasklet);
  848. else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  849. !trans_pcie->inta) {
  850. /* Allow interrupt if was disabled by this handler and
  851. * no tasklet was schedules, We should not enable interrupt,
  852. * tasklet will enable it.
  853. */
  854. iwl_enable_interrupts(trans);
  855. }
  856. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  857. return IRQ_HANDLED;
  858. none:
  859. /* re-enable interrupts here since we don't have anything to service.
  860. * only Re-enable if disabled by irq.
  861. */
  862. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  863. !trans_pcie->inta)
  864. iwl_enable_interrupts(trans);
  865. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  866. return IRQ_NONE;
  867. }