rx.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 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-prph.h"
  33. #include "iwl-io.h"
  34. #include "internal.h"
  35. #include "iwl-op-mode.h"
  36. /******************************************************************************
  37. *
  38. * RX path functions
  39. *
  40. ******************************************************************************/
  41. /*
  42. * Rx theory of operation
  43. *
  44. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  45. * each of which point to Receive Buffers to be filled by the NIC. These get
  46. * used not only for Rx frames, but for any command response or notification
  47. * from the NIC. The driver and NIC manage the Rx buffers by means
  48. * of indexes into the circular buffer.
  49. *
  50. * Rx Queue Indexes
  51. * The host/firmware share two index registers for managing the Rx buffers.
  52. *
  53. * The READ index maps to the first position that the firmware may be writing
  54. * to -- the driver can read up to (but not including) this position and get
  55. * good data.
  56. * The READ index is managed by the firmware once the card is enabled.
  57. *
  58. * The WRITE index maps to the last position the driver has read from -- the
  59. * position preceding WRITE is the last slot the firmware can place a packet.
  60. *
  61. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  62. * WRITE = READ.
  63. *
  64. * During initialization, the host sets up the READ queue position to the first
  65. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  66. *
  67. * When the firmware places a packet in a buffer, it will advance the READ index
  68. * and fire the RX interrupt. The driver can then query the READ index and
  69. * process as many packets as possible, moving the WRITE index forward as it
  70. * resets the Rx queue buffers with new memory.
  71. *
  72. * The management in the driver is as follows:
  73. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  74. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  75. * to replenish the iwl->rxq->rx_free.
  76. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  77. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  78. * 'processed' and 'read' driver indexes as well)
  79. * + A received packet is processed and handed to the kernel network stack,
  80. * detached from the iwl->rxq. The driver 'processed' index is updated.
  81. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  82. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  83. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  84. * were enough free buffers and RX_STALLED is set it is cleared.
  85. *
  86. *
  87. * Driver sequence:
  88. *
  89. * iwl_rx_queue_alloc() Allocates rx_free
  90. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  91. * iwl_rx_queue_restock
  92. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  93. * queue, updates firmware pointers, and updates
  94. * the WRITE index. If insufficient rx_free buffers
  95. * are available, schedules iwl_rx_replenish
  96. *
  97. * -- enable interrupts --
  98. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  99. * READ INDEX, detaching the SKB from the pool.
  100. * Moves the packet buffer from queue to rx_used.
  101. * Calls iwl_rx_queue_restock to refill any empty
  102. * slots.
  103. * ...
  104. *
  105. */
  106. /**
  107. * iwl_rx_queue_space - Return number of free slots available in queue.
  108. */
  109. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  110. {
  111. int s = q->read - q->write;
  112. if (s <= 0)
  113. s += RX_QUEUE_SIZE;
  114. /* keep some buffer to not confuse full and empty queue */
  115. s -= 2;
  116. if (s < 0)
  117. s = 0;
  118. return s;
  119. }
  120. /**
  121. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  122. */
  123. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  124. struct iwl_rx_queue *q)
  125. {
  126. unsigned long flags;
  127. u32 reg;
  128. spin_lock_irqsave(&q->lock, flags);
  129. if (q->need_update == 0)
  130. goto exit_unlock;
  131. if (trans->cfg->base_params->shadow_reg_enable) {
  132. /* shadow register enabled */
  133. /* Device expects a multiple of 8 */
  134. q->write_actual = (q->write & ~0x7);
  135. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
  136. } else {
  137. struct iwl_trans_pcie *trans_pcie =
  138. IWL_TRANS_GET_PCIE_TRANS(trans);
  139. /* If power-saving is in use, make sure device is awake */
  140. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
  141. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  142. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  143. IWL_DEBUG_INFO(trans,
  144. "Rx queue requesting wakeup,"
  145. " GP1 = 0x%x\n", reg);
  146. iwl_set_bit(trans, 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(trans, 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(trans, 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. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  167. */
  168. static inline __le32 iwl_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  169. {
  170. return cpu_to_le32((u32)(dma_addr >> 8));
  171. }
  172. /**
  173. * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
  174. *
  175. * If there are slots in the RX queue that need to be restocked,
  176. * and we have free pre-allocated buffers, fill the ranks as much
  177. * as we can, pulling from rx_free.
  178. *
  179. * This moves the 'write' index forward to catch up with 'processed', and
  180. * also updates the memory address in the firmware to reference the new
  181. * target buffer.
  182. */
  183. static void iwl_rx_queue_restock(struct iwl_trans *trans)
  184. {
  185. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  186. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  187. struct iwl_rx_mem_buffer *rxb;
  188. unsigned long flags;
  189. /*
  190. * If the device isn't enabled - not need to try to add buffers...
  191. * This can happen when we stop the device and still have an interrupt
  192. * pending. We stop the APM before we sync the interrupts / tasklets
  193. * because we have to (see comment there). On the other hand, since
  194. * the APM is stopped, we cannot access the HW (in particular not prph).
  195. * So don't try to restock if the APM has been already stopped.
  196. */
  197. if (!test_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status))
  198. return;
  199. spin_lock_irqsave(&rxq->lock, flags);
  200. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  201. /* The overwritten rxb must be a used one */
  202. rxb = rxq->queue[rxq->write];
  203. BUG_ON(rxb && rxb->page);
  204. /* Get next free Rx buffer, remove from free list */
  205. rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
  206. list);
  207. list_del(&rxb->list);
  208. /* Point to Rx buffer via next RBD in circular buffer */
  209. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(rxb->page_dma);
  210. rxq->queue[rxq->write] = rxb;
  211. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  212. rxq->free_count--;
  213. }
  214. spin_unlock_irqrestore(&rxq->lock, flags);
  215. /* If the pre-allocated buffer pool is dropping low, schedule to
  216. * refill it */
  217. if (rxq->free_count <= RX_LOW_WATERMARK)
  218. schedule_work(&trans_pcie->rx_replenish);
  219. /* If we've added more space for the firmware to place data, tell it.
  220. * Increment device's write pointer in multiples of 8. */
  221. if (rxq->write_actual != (rxq->write & ~0x7)) {
  222. spin_lock_irqsave(&rxq->lock, flags);
  223. rxq->need_update = 1;
  224. spin_unlock_irqrestore(&rxq->lock, flags);
  225. iwl_rx_queue_update_write_ptr(trans, rxq);
  226. }
  227. }
  228. /*
  229. * iwl_rx_allocate - allocate a page for each used RBD
  230. *
  231. * A used RBD is an Rx buffer that has been given to the stack. To use it again
  232. * a page must be allocated and the RBD must point to the page. This function
  233. * doesn't change the HW pointer but handles the list of pages that is used by
  234. * iwl_rx_queue_restock. The latter function will update the HW to use the newly
  235. * allocated buffers.
  236. */
  237. static void iwl_rx_allocate(struct iwl_trans *trans, gfp_t priority)
  238. {
  239. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  240. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  241. struct iwl_rx_mem_buffer *rxb;
  242. struct page *page;
  243. unsigned long flags;
  244. gfp_t gfp_mask = priority;
  245. while (1) {
  246. spin_lock_irqsave(&rxq->lock, flags);
  247. if (list_empty(&rxq->rx_used)) {
  248. spin_unlock_irqrestore(&rxq->lock, flags);
  249. return;
  250. }
  251. spin_unlock_irqrestore(&rxq->lock, flags);
  252. if (rxq->free_count > RX_LOW_WATERMARK)
  253. gfp_mask |= __GFP_NOWARN;
  254. if (trans_pcie->rx_page_order > 0)
  255. gfp_mask |= __GFP_COMP;
  256. /* Alloc a new receive buffer */
  257. page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
  258. if (!page) {
  259. if (net_ratelimit())
  260. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  261. "order: %d\n",
  262. trans_pcie->rx_page_order);
  263. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  264. net_ratelimit())
  265. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  266. "Only %u free buffers remaining.\n",
  267. priority == GFP_ATOMIC ?
  268. "GFP_ATOMIC" : "GFP_KERNEL",
  269. rxq->free_count);
  270. /* We don't reschedule replenish work here -- we will
  271. * call the restock method and if it still needs
  272. * more buffers it will schedule replenish */
  273. return;
  274. }
  275. spin_lock_irqsave(&rxq->lock, flags);
  276. if (list_empty(&rxq->rx_used)) {
  277. spin_unlock_irqrestore(&rxq->lock, flags);
  278. __free_pages(page, trans_pcie->rx_page_order);
  279. return;
  280. }
  281. rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
  282. list);
  283. list_del(&rxb->list);
  284. spin_unlock_irqrestore(&rxq->lock, flags);
  285. BUG_ON(rxb->page);
  286. rxb->page = page;
  287. /* Get physical address of the RB */
  288. rxb->page_dma =
  289. dma_map_page(trans->dev, page, 0,
  290. PAGE_SIZE << trans_pcie->rx_page_order,
  291. DMA_FROM_DEVICE);
  292. /* dma address must be no more than 36 bits */
  293. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  294. /* and also 256 byte aligned! */
  295. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  296. spin_lock_irqsave(&rxq->lock, flags);
  297. list_add_tail(&rxb->list, &rxq->rx_free);
  298. rxq->free_count++;
  299. spin_unlock_irqrestore(&rxq->lock, flags);
  300. }
  301. }
  302. /*
  303. * iwl_rx_replenish - Move all used buffers from rx_used to rx_free
  304. *
  305. * When moving to rx_free an page is allocated for the slot.
  306. *
  307. * Also restock the Rx queue via iwl_rx_queue_restock.
  308. * This is called as a scheduled work item (except for during initialization)
  309. */
  310. void iwl_rx_replenish(struct iwl_trans *trans)
  311. {
  312. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  313. unsigned long flags;
  314. iwl_rx_allocate(trans, GFP_KERNEL);
  315. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  316. iwl_rx_queue_restock(trans);
  317. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  318. }
  319. static void iwl_rx_replenish_now(struct iwl_trans *trans)
  320. {
  321. iwl_rx_allocate(trans, GFP_ATOMIC);
  322. iwl_rx_queue_restock(trans);
  323. }
  324. void iwl_bg_rx_replenish(struct work_struct *data)
  325. {
  326. struct iwl_trans_pcie *trans_pcie =
  327. container_of(data, struct iwl_trans_pcie, rx_replenish);
  328. iwl_rx_replenish(trans_pcie->trans);
  329. }
  330. static void iwl_rx_handle_rxbuf(struct iwl_trans *trans,
  331. struct iwl_rx_mem_buffer *rxb)
  332. {
  333. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  334. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  335. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  336. unsigned long flags;
  337. bool page_stolen = false;
  338. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  339. u32 offset = 0;
  340. if (WARN_ON(!rxb))
  341. return;
  342. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  343. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  344. struct iwl_rx_packet *pkt;
  345. struct iwl_device_cmd *cmd;
  346. u16 sequence;
  347. bool reclaim;
  348. int index, cmd_index, err, len;
  349. struct iwl_rx_cmd_buffer rxcb = {
  350. ._offset = offset,
  351. ._page = rxb->page,
  352. ._page_stolen = false,
  353. .truesize = max_len,
  354. };
  355. pkt = rxb_addr(&rxcb);
  356. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  357. break;
  358. IWL_DEBUG_RX(trans, "cmd at offset %d: %s (0x%.2x)\n",
  359. rxcb._offset,
  360. trans_pcie_get_cmd_string(trans_pcie, pkt->hdr.cmd),
  361. pkt->hdr.cmd);
  362. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  363. len += sizeof(u32); /* account for status word */
  364. trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
  365. trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
  366. /* Reclaim a command buffer only if this packet is a response
  367. * to a (driver-originated) command.
  368. * If the packet (e.g. Rx frame) originated from uCode,
  369. * there is no command buffer to reclaim.
  370. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  371. * but apparently a few don't get set; catch them here. */
  372. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  373. if (reclaim) {
  374. int i;
  375. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  376. if (trans_pcie->no_reclaim_cmds[i] ==
  377. pkt->hdr.cmd) {
  378. reclaim = false;
  379. break;
  380. }
  381. }
  382. }
  383. sequence = le16_to_cpu(pkt->hdr.sequence);
  384. index = SEQ_TO_INDEX(sequence);
  385. cmd_index = get_cmd_index(&txq->q, index);
  386. if (reclaim) {
  387. struct iwl_pcie_tx_queue_entry *ent;
  388. ent = &txq->entries[cmd_index];
  389. cmd = ent->copy_cmd;
  390. WARN_ON_ONCE(!cmd && ent->meta.flags & CMD_WANT_HCMD);
  391. } else {
  392. cmd = NULL;
  393. }
  394. err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
  395. if (reclaim) {
  396. /* The original command isn't needed any more */
  397. kfree(txq->entries[cmd_index].copy_cmd);
  398. txq->entries[cmd_index].copy_cmd = NULL;
  399. /* nor is the duplicated part of the command */
  400. kfree(txq->entries[cmd_index].free_buf);
  401. txq->entries[cmd_index].free_buf = NULL;
  402. }
  403. /*
  404. * After here, we should always check rxcb._page_stolen,
  405. * if it is true then one of the handlers took the page.
  406. */
  407. if (reclaim) {
  408. /* Invoke any callbacks, transfer the buffer to caller,
  409. * and fire off the (possibly) blocking
  410. * iwl_trans_send_cmd()
  411. * as we reclaim the driver command queue */
  412. if (!rxcb._page_stolen)
  413. iwl_tx_cmd_complete(trans, &rxcb, err);
  414. else
  415. IWL_WARN(trans, "Claim null rxb?\n");
  416. }
  417. page_stolen |= rxcb._page_stolen;
  418. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  419. }
  420. /* page was stolen from us -- free our reference */
  421. if (page_stolen) {
  422. __free_pages(rxb->page, trans_pcie->rx_page_order);
  423. rxb->page = NULL;
  424. }
  425. /* Reuse the page if possible. For notification packets and
  426. * SKBs that fail to Rx correctly, add them back into the
  427. * rx_free list for reuse later. */
  428. spin_lock_irqsave(&rxq->lock, flags);
  429. if (rxb->page != NULL) {
  430. rxb->page_dma =
  431. dma_map_page(trans->dev, rxb->page, 0,
  432. PAGE_SIZE << trans_pcie->rx_page_order,
  433. DMA_FROM_DEVICE);
  434. list_add_tail(&rxb->list, &rxq->rx_free);
  435. rxq->free_count++;
  436. } else
  437. list_add_tail(&rxb->list, &rxq->rx_used);
  438. spin_unlock_irqrestore(&rxq->lock, flags);
  439. }
  440. /**
  441. * iwl_rx_handle - Main entry function for receiving responses from uCode
  442. *
  443. * Uses the priv->rx_handlers callback function array to invoke
  444. * the appropriate handlers, including command responses,
  445. * frame-received notifications, and other notifications.
  446. */
  447. static void iwl_rx_handle(struct iwl_trans *trans)
  448. {
  449. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  450. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  451. u32 r, i;
  452. u8 fill_rx = 0;
  453. u32 count = 8;
  454. int total_empty;
  455. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  456. * buffer that the driver may process (last buffer filled by ucode). */
  457. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  458. i = rxq->read;
  459. /* Rx interrupt, but nothing sent from uCode */
  460. if (i == r)
  461. IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
  462. /* calculate total frames need to be restock after handling RX */
  463. total_empty = r - rxq->write_actual;
  464. if (total_empty < 0)
  465. total_empty += RX_QUEUE_SIZE;
  466. if (total_empty > (RX_QUEUE_SIZE / 2))
  467. fill_rx = 1;
  468. while (i != r) {
  469. struct iwl_rx_mem_buffer *rxb;
  470. rxb = rxq->queue[i];
  471. rxq->queue[i] = NULL;
  472. IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d (%p)\n",
  473. r, i, rxb);
  474. iwl_rx_handle_rxbuf(trans, rxb);
  475. i = (i + 1) & RX_QUEUE_MASK;
  476. /* If there are a lot of unused frames,
  477. * restock the Rx queue so ucode wont assert. */
  478. if (fill_rx) {
  479. count++;
  480. if (count >= 8) {
  481. rxq->read = i;
  482. iwl_rx_replenish_now(trans);
  483. count = 0;
  484. }
  485. }
  486. }
  487. /* Backtrack one entry */
  488. rxq->read = i;
  489. if (fill_rx)
  490. iwl_rx_replenish_now(trans);
  491. else
  492. iwl_rx_queue_restock(trans);
  493. }
  494. /**
  495. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  496. */
  497. static void iwl_irq_handle_error(struct iwl_trans *trans)
  498. {
  499. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  500. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  501. if (trans->cfg->internal_wimax_coex &&
  502. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  503. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  504. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  505. APMG_PS_CTRL_VAL_RESET_REQ))) {
  506. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  507. iwl_op_mode_wimax_active(trans->op_mode);
  508. wake_up(&trans_pcie->wait_command_queue);
  509. return;
  510. }
  511. iwl_dump_csr(trans);
  512. iwl_dump_fh(trans, NULL);
  513. set_bit(STATUS_FW_ERROR, &trans_pcie->status);
  514. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  515. wake_up(&trans_pcie->wait_command_queue);
  516. iwl_op_mode_nic_error(trans->op_mode);
  517. }
  518. /* tasklet for iwlagn interrupt */
  519. void iwl_irq_tasklet(struct iwl_trans *trans)
  520. {
  521. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  522. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  523. u32 inta = 0;
  524. u32 handled = 0;
  525. unsigned long flags;
  526. u32 i;
  527. #ifdef CONFIG_IWLWIFI_DEBUG
  528. u32 inta_mask;
  529. #endif
  530. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  531. /* Ack/clear/reset pending uCode interrupts.
  532. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  533. */
  534. /* There is a hardware bug in the interrupt mask function that some
  535. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  536. * they are disabled in the CSR_INT_MASK register. Furthermore the
  537. * ICT interrupt handling mechanism has another bug that might cause
  538. * these unmasked interrupts fail to be detected. We workaround the
  539. * hardware bugs here by ACKing all the possible interrupts so that
  540. * interrupt coalescing can still be achieved.
  541. */
  542. iwl_write32(trans, CSR_INT,
  543. trans_pcie->inta | ~trans_pcie->inta_mask);
  544. inta = trans_pcie->inta;
  545. #ifdef CONFIG_IWLWIFI_DEBUG
  546. if (iwl_have_debug_level(IWL_DL_ISR)) {
  547. /* just for debug */
  548. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  549. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  550. inta, inta_mask);
  551. }
  552. #endif
  553. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  554. trans_pcie->inta = 0;
  555. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  556. /* Now service all interrupt bits discovered above. */
  557. if (inta & CSR_INT_BIT_HW_ERR) {
  558. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  559. /* Tell the device to stop sending interrupts */
  560. iwl_disable_interrupts(trans);
  561. isr_stats->hw++;
  562. iwl_irq_handle_error(trans);
  563. handled |= CSR_INT_BIT_HW_ERR;
  564. return;
  565. }
  566. #ifdef CONFIG_IWLWIFI_DEBUG
  567. if (iwl_have_debug_level(IWL_DL_ISR)) {
  568. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  569. if (inta & CSR_INT_BIT_SCD) {
  570. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  571. "the frame/frames.\n");
  572. isr_stats->sch++;
  573. }
  574. /* Alive notification via Rx interrupt will do the real work */
  575. if (inta & CSR_INT_BIT_ALIVE) {
  576. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  577. isr_stats->alive++;
  578. }
  579. }
  580. #endif
  581. /* Safely ignore these bits for debug checks below */
  582. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  583. /* HW RF KILL switch toggled */
  584. if (inta & CSR_INT_BIT_RF_KILL) {
  585. bool hw_rfkill;
  586. hw_rfkill = iwl_is_rfkill_set(trans);
  587. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  588. hw_rfkill ? "disable radio" : "enable radio");
  589. isr_stats->rfkill++;
  590. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  591. if (hw_rfkill) {
  592. set_bit(STATUS_RFKILL, &trans_pcie->status);
  593. if (test_and_clear_bit(STATUS_HCMD_ACTIVE,
  594. &trans_pcie->status))
  595. IWL_DEBUG_RF_KILL(trans,
  596. "Rfkill while SYNC HCMD in flight\n");
  597. wake_up(&trans_pcie->wait_command_queue);
  598. } else {
  599. clear_bit(STATUS_RFKILL, &trans_pcie->status);
  600. }
  601. handled |= CSR_INT_BIT_RF_KILL;
  602. }
  603. /* Chip got too hot and stopped itself */
  604. if (inta & CSR_INT_BIT_CT_KILL) {
  605. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  606. isr_stats->ctkill++;
  607. handled |= CSR_INT_BIT_CT_KILL;
  608. }
  609. /* Error detected by uCode */
  610. if (inta & CSR_INT_BIT_SW_ERR) {
  611. IWL_ERR(trans, "Microcode SW error detected. "
  612. " Restarting 0x%X.\n", inta);
  613. isr_stats->sw++;
  614. iwl_irq_handle_error(trans);
  615. handled |= CSR_INT_BIT_SW_ERR;
  616. }
  617. /* uCode wakes up after power-down sleep */
  618. if (inta & CSR_INT_BIT_WAKEUP) {
  619. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  620. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  621. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  622. iwl_txq_update_write_ptr(trans,
  623. &trans_pcie->txq[i]);
  624. isr_stats->wakeup++;
  625. handled |= CSR_INT_BIT_WAKEUP;
  626. }
  627. /* All uCode command responses, including Tx command responses,
  628. * Rx "responses" (frame-received notification), and other
  629. * notifications from uCode come through here*/
  630. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  631. CSR_INT_BIT_RX_PERIODIC)) {
  632. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  633. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  634. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  635. iwl_write32(trans, CSR_FH_INT_STATUS,
  636. CSR_FH_INT_RX_MASK);
  637. }
  638. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  639. handled |= CSR_INT_BIT_RX_PERIODIC;
  640. iwl_write32(trans,
  641. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  642. }
  643. /* Sending RX interrupt require many steps to be done in the
  644. * the device:
  645. * 1- write interrupt to current index in ICT table.
  646. * 2- dma RX frame.
  647. * 3- update RX shared data to indicate last write index.
  648. * 4- send interrupt.
  649. * This could lead to RX race, driver could receive RX interrupt
  650. * but the shared data changes does not reflect this;
  651. * periodic interrupt will detect any dangling Rx activity.
  652. */
  653. /* Disable periodic interrupt; we use it as just a one-shot. */
  654. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  655. CSR_INT_PERIODIC_DIS);
  656. iwl_rx_handle(trans);
  657. /*
  658. * Enable periodic interrupt in 8 msec only if we received
  659. * real RX interrupt (instead of just periodic int), to catch
  660. * any dangling Rx interrupt. If it was just the periodic
  661. * interrupt, there was no dangling Rx activity, and no need
  662. * to extend the periodic interrupt; one-shot is enough.
  663. */
  664. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  665. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  666. CSR_INT_PERIODIC_ENA);
  667. isr_stats->rx++;
  668. }
  669. /* This "Tx" DMA channel is used only for loading uCode */
  670. if (inta & CSR_INT_BIT_FH_TX) {
  671. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  672. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  673. isr_stats->tx++;
  674. handled |= CSR_INT_BIT_FH_TX;
  675. /* Wake up uCode load routine, now that load is complete */
  676. trans_pcie->ucode_write_complete = true;
  677. wake_up(&trans_pcie->ucode_write_waitq);
  678. }
  679. if (inta & ~handled) {
  680. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  681. isr_stats->unhandled++;
  682. }
  683. if (inta & ~(trans_pcie->inta_mask)) {
  684. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  685. inta & ~trans_pcie->inta_mask);
  686. }
  687. /* Re-enable all interrupts */
  688. /* only Re-enable if disabled by irq */
  689. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  690. iwl_enable_interrupts(trans);
  691. /* Re-enable RF_KILL if it occurred */
  692. else if (handled & CSR_INT_BIT_RF_KILL)
  693. iwl_enable_rfkill_int(trans);
  694. }
  695. /******************************************************************************
  696. *
  697. * ICT functions
  698. *
  699. ******************************************************************************/
  700. /* a device (PCI-E) page is 4096 bytes long */
  701. #define ICT_SHIFT 12
  702. #define ICT_SIZE (1 << ICT_SHIFT)
  703. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  704. /* Free dram table */
  705. void iwl_free_isr_ict(struct iwl_trans *trans)
  706. {
  707. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  708. if (trans_pcie->ict_tbl) {
  709. dma_free_coherent(trans->dev, ICT_SIZE,
  710. trans_pcie->ict_tbl,
  711. trans_pcie->ict_tbl_dma);
  712. trans_pcie->ict_tbl = NULL;
  713. trans_pcie->ict_tbl_dma = 0;
  714. }
  715. }
  716. /*
  717. * allocate dram shared table, it is an aligned memory
  718. * block of ICT_SIZE.
  719. * also reset all data related to ICT table interrupt.
  720. */
  721. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  722. {
  723. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  724. trans_pcie->ict_tbl =
  725. dma_alloc_coherent(trans->dev, ICT_SIZE,
  726. &trans_pcie->ict_tbl_dma,
  727. GFP_KERNEL);
  728. if (!trans_pcie->ict_tbl)
  729. return -ENOMEM;
  730. /* just an API sanity check ... it is guaranteed to be aligned */
  731. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  732. iwl_free_isr_ict(trans);
  733. return -EINVAL;
  734. }
  735. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  736. (unsigned long long)trans_pcie->ict_tbl_dma);
  737. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  738. /* reset table and index to all 0 */
  739. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  740. trans_pcie->ict_index = 0;
  741. /* add periodic RX interrupt */
  742. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  743. return 0;
  744. }
  745. /* Device is going up inform it about using ICT interrupt table,
  746. * also we need to tell the driver to start using ICT interrupt.
  747. */
  748. void iwl_reset_ict(struct iwl_trans *trans)
  749. {
  750. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  751. u32 val;
  752. unsigned long flags;
  753. if (!trans_pcie->ict_tbl)
  754. return;
  755. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  756. iwl_disable_interrupts(trans);
  757. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  758. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  759. val |= CSR_DRAM_INT_TBL_ENABLE;
  760. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  761. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  762. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  763. trans_pcie->use_ict = true;
  764. trans_pcie->ict_index = 0;
  765. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  766. iwl_enable_interrupts(trans);
  767. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  768. }
  769. /* Device is going down disable ict interrupt usage */
  770. void iwl_disable_ict(struct iwl_trans *trans)
  771. {
  772. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  773. unsigned long flags;
  774. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  775. trans_pcie->use_ict = false;
  776. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  777. }
  778. /* legacy (non-ICT) ISR. Assumes that trans_pcie->irq_lock is held */
  779. static irqreturn_t iwl_isr(int irq, void *data)
  780. {
  781. struct iwl_trans *trans = data;
  782. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  783. u32 inta, inta_mask;
  784. #ifdef CONFIG_IWLWIFI_DEBUG
  785. u32 inta_fh;
  786. #endif
  787. lockdep_assert_held(&trans_pcie->irq_lock);
  788. trace_iwlwifi_dev_irq(trans->dev);
  789. /* Disable (but don't clear!) interrupts here to avoid
  790. * back-to-back ISRs and sporadic interrupts from our NIC.
  791. * If we have something to service, the tasklet will re-enable ints.
  792. * If we *don't* have something, we'll re-enable before leaving here. */
  793. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  794. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  795. /* Discover which interrupts are active/pending */
  796. inta = iwl_read32(trans, CSR_INT);
  797. /* Ignore interrupt if there's nothing in NIC to service.
  798. * This may be due to IRQ shared with another device,
  799. * or due to sporadic interrupts thrown from our NIC. */
  800. if (!inta) {
  801. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  802. goto none;
  803. }
  804. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  805. /* Hardware disappeared. It might have already raised
  806. * an interrupt */
  807. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  808. return IRQ_HANDLED;
  809. }
  810. #ifdef CONFIG_IWLWIFI_DEBUG
  811. if (iwl_have_debug_level(IWL_DL_ISR)) {
  812. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  813. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  814. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  815. }
  816. #endif
  817. trans_pcie->inta |= inta;
  818. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  819. if (likely(inta))
  820. tasklet_schedule(&trans_pcie->irq_tasklet);
  821. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  822. !trans_pcie->inta)
  823. iwl_enable_interrupts(trans);
  824. none:
  825. /* re-enable interrupts here since we don't have anything to service. */
  826. /* only Re-enable if disabled by irq and no schedules tasklet. */
  827. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  828. !trans_pcie->inta)
  829. iwl_enable_interrupts(trans);
  830. return IRQ_NONE;
  831. }
  832. /* interrupt handler using ict table, with this interrupt driver will
  833. * stop using INTA register to get device's interrupt, reading this register
  834. * is expensive, device will write interrupts in ICT dram table, increment
  835. * index then will fire interrupt to driver, driver will OR all ICT table
  836. * entries from current index up to table entry with 0 value. the result is
  837. * the interrupt we need to service, driver will set the entries back to 0 and
  838. * set index.
  839. */
  840. irqreturn_t iwl_isr_ict(int irq, void *data)
  841. {
  842. struct iwl_trans *trans = data;
  843. struct iwl_trans_pcie *trans_pcie;
  844. u32 inta, inta_mask;
  845. u32 val = 0;
  846. u32 read;
  847. unsigned long flags;
  848. if (!trans)
  849. return IRQ_NONE;
  850. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  851. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  852. /* dram interrupt table not set yet,
  853. * use legacy interrupt.
  854. */
  855. if (unlikely(!trans_pcie->use_ict)) {
  856. irqreturn_t ret = iwl_isr(irq, data);
  857. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  858. return ret;
  859. }
  860. trace_iwlwifi_dev_irq(trans->dev);
  861. /* Disable (but don't clear!) interrupts here to avoid
  862. * back-to-back ISRs and sporadic interrupts from our NIC.
  863. * If we have something to service, the tasklet will re-enable ints.
  864. * If we *don't* have something, we'll re-enable before leaving here.
  865. */
  866. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  867. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  868. /* Ignore interrupt if there's nothing in NIC to service.
  869. * This may be due to IRQ shared with another device,
  870. * or due to sporadic interrupts thrown from our NIC. */
  871. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  872. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  873. if (!read) {
  874. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  875. goto none;
  876. }
  877. /*
  878. * Collect all entries up to the first 0, starting from ict_index;
  879. * note we already read at ict_index.
  880. */
  881. do {
  882. val |= read;
  883. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  884. trans_pcie->ict_index, read);
  885. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  886. trans_pcie->ict_index =
  887. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  888. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  889. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  890. read);
  891. } while (read);
  892. /* We should not get this value, just ignore it. */
  893. if (val == 0xffffffff)
  894. val = 0;
  895. /*
  896. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  897. * (bit 15 before shifting it to 31) to clear when using interrupt
  898. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  899. * so we use them to decide on the real state of the Rx bit.
  900. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  901. */
  902. if (val & 0xC0000)
  903. val |= 0x8000;
  904. inta = (0xff & val) | ((0xff00 & val) << 16);
  905. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  906. inta, inta_mask, val);
  907. inta &= trans_pcie->inta_mask;
  908. trans_pcie->inta |= inta;
  909. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  910. if (likely(inta))
  911. tasklet_schedule(&trans_pcie->irq_tasklet);
  912. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  913. !trans_pcie->inta) {
  914. /* Allow interrupt if was disabled by this handler and
  915. * no tasklet was schedules, We should not enable interrupt,
  916. * tasklet will enable it.
  917. */
  918. iwl_enable_interrupts(trans);
  919. }
  920. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  921. return IRQ_HANDLED;
  922. none:
  923. /* re-enable interrupts here since we don't have anything to service.
  924. * only Re-enable if disabled by irq.
  925. */
  926. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  927. !trans_pcie->inta)
  928. iwl_enable_interrupts(trans);
  929. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  930. return IRQ_NONE;
  931. }