rx.c 33 KB

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