rx.c 32 KB

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