rx.c 32 KB

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