rx.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  514. wake_up(&trans_pcie->wait_command_queue);
  515. iwl_op_mode_nic_error(trans->op_mode);
  516. }
  517. /* tasklet for iwlagn interrupt */
  518. void iwl_irq_tasklet(struct iwl_trans *trans)
  519. {
  520. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  521. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  522. u32 inta = 0;
  523. u32 handled = 0;
  524. unsigned long flags;
  525. u32 i;
  526. #ifdef CONFIG_IWLWIFI_DEBUG
  527. u32 inta_mask;
  528. #endif
  529. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  530. /* Ack/clear/reset pending uCode interrupts.
  531. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  532. */
  533. /* There is a hardware bug in the interrupt mask function that some
  534. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  535. * they are disabled in the CSR_INT_MASK register. Furthermore the
  536. * ICT interrupt handling mechanism has another bug that might cause
  537. * these unmasked interrupts fail to be detected. We workaround the
  538. * hardware bugs here by ACKing all the possible interrupts so that
  539. * interrupt coalescing can still be achieved.
  540. */
  541. iwl_write32(trans, CSR_INT,
  542. trans_pcie->inta | ~trans_pcie->inta_mask);
  543. inta = trans_pcie->inta;
  544. #ifdef CONFIG_IWLWIFI_DEBUG
  545. if (iwl_have_debug_level(IWL_DL_ISR)) {
  546. /* just for debug */
  547. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  548. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  549. inta, inta_mask);
  550. }
  551. #endif
  552. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  553. trans_pcie->inta = 0;
  554. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  555. /* Now service all interrupt bits discovered above. */
  556. if (inta & CSR_INT_BIT_HW_ERR) {
  557. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  558. /* Tell the device to stop sending interrupts */
  559. iwl_disable_interrupts(trans);
  560. isr_stats->hw++;
  561. iwl_irq_handle_error(trans);
  562. handled |= CSR_INT_BIT_HW_ERR;
  563. return;
  564. }
  565. #ifdef CONFIG_IWLWIFI_DEBUG
  566. if (iwl_have_debug_level(IWL_DL_ISR)) {
  567. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  568. if (inta & CSR_INT_BIT_SCD) {
  569. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  570. "the frame/frames.\n");
  571. isr_stats->sch++;
  572. }
  573. /* Alive notification via Rx interrupt will do the real work */
  574. if (inta & CSR_INT_BIT_ALIVE) {
  575. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  576. isr_stats->alive++;
  577. }
  578. }
  579. #endif
  580. /* Safely ignore these bits for debug checks below */
  581. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  582. /* HW RF KILL switch toggled */
  583. if (inta & CSR_INT_BIT_RF_KILL) {
  584. bool hw_rfkill;
  585. hw_rfkill = iwl_is_rfkill_set(trans);
  586. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  587. hw_rfkill ? "disable radio" : "enable radio");
  588. isr_stats->rfkill++;
  589. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  590. if (hw_rfkill) {
  591. set_bit(STATUS_RFKILL, &trans_pcie->status);
  592. if (test_and_clear_bit(STATUS_HCMD_ACTIVE,
  593. &trans_pcie->status))
  594. IWL_DEBUG_RF_KILL(trans,
  595. "Rfkill while SYNC HCMD in flight\n");
  596. wake_up(&trans_pcie->wait_command_queue);
  597. } else {
  598. clear_bit(STATUS_RFKILL, &trans_pcie->status);
  599. }
  600. handled |= CSR_INT_BIT_RF_KILL;
  601. }
  602. /* Chip got too hot and stopped itself */
  603. if (inta & CSR_INT_BIT_CT_KILL) {
  604. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  605. isr_stats->ctkill++;
  606. handled |= CSR_INT_BIT_CT_KILL;
  607. }
  608. /* Error detected by uCode */
  609. if (inta & CSR_INT_BIT_SW_ERR) {
  610. IWL_ERR(trans, "Microcode SW error detected. "
  611. " Restarting 0x%X.\n", inta);
  612. isr_stats->sw++;
  613. iwl_irq_handle_error(trans);
  614. handled |= CSR_INT_BIT_SW_ERR;
  615. }
  616. /* uCode wakes up after power-down sleep */
  617. if (inta & CSR_INT_BIT_WAKEUP) {
  618. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  619. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  620. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  621. iwl_txq_update_write_ptr(trans,
  622. &trans_pcie->txq[i]);
  623. isr_stats->wakeup++;
  624. handled |= CSR_INT_BIT_WAKEUP;
  625. }
  626. /* All uCode command responses, including Tx command responses,
  627. * Rx "responses" (frame-received notification), and other
  628. * notifications from uCode come through here*/
  629. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  630. CSR_INT_BIT_RX_PERIODIC)) {
  631. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  632. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  633. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  634. iwl_write32(trans, CSR_FH_INT_STATUS,
  635. CSR_FH_INT_RX_MASK);
  636. }
  637. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  638. handled |= CSR_INT_BIT_RX_PERIODIC;
  639. iwl_write32(trans,
  640. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  641. }
  642. /* Sending RX interrupt require many steps to be done in the
  643. * the device:
  644. * 1- write interrupt to current index in ICT table.
  645. * 2- dma RX frame.
  646. * 3- update RX shared data to indicate last write index.
  647. * 4- send interrupt.
  648. * This could lead to RX race, driver could receive RX interrupt
  649. * but the shared data changes does not reflect this;
  650. * periodic interrupt will detect any dangling Rx activity.
  651. */
  652. /* Disable periodic interrupt; we use it as just a one-shot. */
  653. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  654. CSR_INT_PERIODIC_DIS);
  655. iwl_rx_handle(trans);
  656. /*
  657. * Enable periodic interrupt in 8 msec only if we received
  658. * real RX interrupt (instead of just periodic int), to catch
  659. * any dangling Rx interrupt. If it was just the periodic
  660. * interrupt, there was no dangling Rx activity, and no need
  661. * to extend the periodic interrupt; one-shot is enough.
  662. */
  663. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  664. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  665. CSR_INT_PERIODIC_ENA);
  666. isr_stats->rx++;
  667. }
  668. /* This "Tx" DMA channel is used only for loading uCode */
  669. if (inta & CSR_INT_BIT_FH_TX) {
  670. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  671. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  672. isr_stats->tx++;
  673. handled |= CSR_INT_BIT_FH_TX;
  674. /* Wake up uCode load routine, now that load is complete */
  675. trans_pcie->ucode_write_complete = true;
  676. wake_up(&trans_pcie->ucode_write_waitq);
  677. }
  678. if (inta & ~handled) {
  679. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  680. isr_stats->unhandled++;
  681. }
  682. if (inta & ~(trans_pcie->inta_mask)) {
  683. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  684. inta & ~trans_pcie->inta_mask);
  685. }
  686. /* Re-enable all interrupts */
  687. /* only Re-enable if disabled by irq */
  688. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  689. iwl_enable_interrupts(trans);
  690. /* Re-enable RF_KILL if it occurred */
  691. else if (handled & CSR_INT_BIT_RF_KILL)
  692. iwl_enable_rfkill_int(trans);
  693. }
  694. /******************************************************************************
  695. *
  696. * ICT functions
  697. *
  698. ******************************************************************************/
  699. /* a device (PCI-E) page is 4096 bytes long */
  700. #define ICT_SHIFT 12
  701. #define ICT_SIZE (1 << ICT_SHIFT)
  702. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  703. /* Free dram table */
  704. void iwl_free_isr_ict(struct iwl_trans *trans)
  705. {
  706. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  707. if (trans_pcie->ict_tbl) {
  708. dma_free_coherent(trans->dev, ICT_SIZE,
  709. trans_pcie->ict_tbl,
  710. trans_pcie->ict_tbl_dma);
  711. trans_pcie->ict_tbl = NULL;
  712. trans_pcie->ict_tbl_dma = 0;
  713. }
  714. }
  715. /*
  716. * allocate dram shared table, it is an aligned memory
  717. * block of ICT_SIZE.
  718. * also reset all data related to ICT table interrupt.
  719. */
  720. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  721. {
  722. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  723. trans_pcie->ict_tbl =
  724. dma_alloc_coherent(trans->dev, ICT_SIZE,
  725. &trans_pcie->ict_tbl_dma,
  726. GFP_KERNEL);
  727. if (!trans_pcie->ict_tbl)
  728. return -ENOMEM;
  729. /* just an API sanity check ... it is guaranteed to be aligned */
  730. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  731. iwl_free_isr_ict(trans);
  732. return -EINVAL;
  733. }
  734. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  735. (unsigned long long)trans_pcie->ict_tbl_dma);
  736. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  737. /* reset table and index to all 0 */
  738. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  739. trans_pcie->ict_index = 0;
  740. /* add periodic RX interrupt */
  741. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  742. return 0;
  743. }
  744. /* Device is going up inform it about using ICT interrupt table,
  745. * also we need to tell the driver to start using ICT interrupt.
  746. */
  747. void iwl_reset_ict(struct iwl_trans *trans)
  748. {
  749. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  750. u32 val;
  751. unsigned long flags;
  752. if (!trans_pcie->ict_tbl)
  753. return;
  754. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  755. iwl_disable_interrupts(trans);
  756. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  757. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  758. val |= CSR_DRAM_INT_TBL_ENABLE;
  759. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  760. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  761. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  762. trans_pcie->use_ict = true;
  763. trans_pcie->ict_index = 0;
  764. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  765. iwl_enable_interrupts(trans);
  766. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  767. }
  768. /* Device is going down disable ict interrupt usage */
  769. void iwl_disable_ict(struct iwl_trans *trans)
  770. {
  771. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  772. unsigned long flags;
  773. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  774. trans_pcie->use_ict = false;
  775. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  776. }
  777. /* legacy (non-ICT) ISR. Assumes that trans_pcie->irq_lock is held */
  778. static irqreturn_t iwl_isr(int irq, void *data)
  779. {
  780. struct iwl_trans *trans = data;
  781. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  782. u32 inta, inta_mask;
  783. #ifdef CONFIG_IWLWIFI_DEBUG
  784. u32 inta_fh;
  785. #endif
  786. lockdep_assert_held(&trans_pcie->irq_lock);
  787. trace_iwlwifi_dev_irq(trans->dev);
  788. /* Disable (but don't clear!) interrupts here to avoid
  789. * back-to-back ISRs and sporadic interrupts from our NIC.
  790. * If we have something to service, the tasklet will re-enable ints.
  791. * If we *don't* have something, we'll re-enable before leaving here. */
  792. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  793. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  794. /* Discover which interrupts are active/pending */
  795. inta = iwl_read32(trans, CSR_INT);
  796. /* Ignore interrupt if there's nothing in NIC to service.
  797. * This may be due to IRQ shared with another device,
  798. * or due to sporadic interrupts thrown from our NIC. */
  799. if (!inta) {
  800. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  801. goto none;
  802. }
  803. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  804. /* Hardware disappeared. It might have already raised
  805. * an interrupt */
  806. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  807. return IRQ_HANDLED;
  808. }
  809. #ifdef CONFIG_IWLWIFI_DEBUG
  810. if (iwl_have_debug_level(IWL_DL_ISR)) {
  811. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  812. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  813. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  814. }
  815. #endif
  816. trans_pcie->inta |= inta;
  817. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  818. if (likely(inta))
  819. tasklet_schedule(&trans_pcie->irq_tasklet);
  820. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  821. !trans_pcie->inta)
  822. iwl_enable_interrupts(trans);
  823. none:
  824. /* re-enable interrupts here since we don't have anything to service. */
  825. /* only Re-enable if disabled by irq and no schedules tasklet. */
  826. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  827. !trans_pcie->inta)
  828. iwl_enable_interrupts(trans);
  829. return IRQ_NONE;
  830. }
  831. /* interrupt handler using ict table, with this interrupt driver will
  832. * stop using INTA register to get device's interrupt, reading this register
  833. * is expensive, device will write interrupts in ICT dram table, increment
  834. * index then will fire interrupt to driver, driver will OR all ICT table
  835. * entries from current index up to table entry with 0 value. the result is
  836. * the interrupt we need to service, driver will set the entries back to 0 and
  837. * set index.
  838. */
  839. irqreturn_t iwl_isr_ict(int irq, void *data)
  840. {
  841. struct iwl_trans *trans = data;
  842. struct iwl_trans_pcie *trans_pcie;
  843. u32 inta, inta_mask;
  844. u32 val = 0;
  845. u32 read;
  846. unsigned long flags;
  847. if (!trans)
  848. return IRQ_NONE;
  849. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  850. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  851. /* dram interrupt table not set yet,
  852. * use legacy interrupt.
  853. */
  854. if (unlikely(!trans_pcie->use_ict)) {
  855. irqreturn_t ret = iwl_isr(irq, data);
  856. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  857. return ret;
  858. }
  859. trace_iwlwifi_dev_irq(trans->dev);
  860. /* Disable (but don't clear!) interrupts here to avoid
  861. * back-to-back ISRs and sporadic interrupts from our NIC.
  862. * If we have something to service, the tasklet will re-enable ints.
  863. * If we *don't* have something, we'll re-enable before leaving here.
  864. */
  865. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  866. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  867. /* Ignore interrupt if there's nothing in NIC to service.
  868. * This may be due to IRQ shared with another device,
  869. * or due to sporadic interrupts thrown from our NIC. */
  870. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  871. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  872. if (!read) {
  873. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  874. goto none;
  875. }
  876. /*
  877. * Collect all entries up to the first 0, starting from ict_index;
  878. * note we already read at ict_index.
  879. */
  880. do {
  881. val |= read;
  882. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  883. trans_pcie->ict_index, read);
  884. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  885. trans_pcie->ict_index =
  886. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  887. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  888. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  889. read);
  890. } while (read);
  891. /* We should not get this value, just ignore it. */
  892. if (val == 0xffffffff)
  893. val = 0;
  894. /*
  895. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  896. * (bit 15 before shifting it to 31) to clear when using interrupt
  897. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  898. * so we use them to decide on the real state of the Rx bit.
  899. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  900. */
  901. if (val & 0xC0000)
  902. val |= 0x8000;
  903. inta = (0xff & val) | ((0xff00 & val) << 16);
  904. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  905. inta, inta_mask, val);
  906. inta &= trans_pcie->inta_mask;
  907. trans_pcie->inta |= inta;
  908. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  909. if (likely(inta))
  910. tasklet_schedule(&trans_pcie->irq_tasklet);
  911. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  912. !trans_pcie->inta) {
  913. /* Allow interrupt if was disabled by this handler and
  914. * no tasklet was schedules, We should not enable interrupt,
  915. * tasklet will enable it.
  916. */
  917. iwl_enable_interrupts(trans);
  918. }
  919. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  920. return IRQ_HANDLED;
  921. none:
  922. /* re-enable interrupts here since we don't have anything to service.
  923. * only Re-enable if disabled by irq.
  924. */
  925. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  926. !trans_pcie->inta)
  927. iwl_enable_interrupts(trans);
  928. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  929. return IRQ_NONE;
  930. }