rx.c 32 KB

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