iwl-trans-pcie-rx.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 "iwl-trans-pcie-int.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 (cfg(trans)->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_POWER_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 =
  189. IWL_TRANS_GET_PCIE_TRANS(trans);
  190. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  191. struct list_head *element;
  192. struct iwl_rx_mem_buffer *rxb;
  193. unsigned long flags;
  194. spin_lock_irqsave(&rxq->lock, flags);
  195. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  196. /* The overwritten rxb must be a used one */
  197. rxb = rxq->queue[rxq->write];
  198. BUG_ON(rxb && rxb->page);
  199. /* Get next free Rx buffer, remove from free list */
  200. element = rxq->rx_free.next;
  201. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  202. list_del(element);
  203. /* Point to Rx buffer via next RBD in circular buffer */
  204. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
  205. rxq->queue[rxq->write] = rxb;
  206. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  207. rxq->free_count--;
  208. }
  209. spin_unlock_irqrestore(&rxq->lock, flags);
  210. /* If the pre-allocated buffer pool is dropping low, schedule to
  211. * refill it */
  212. if (rxq->free_count <= RX_LOW_WATERMARK)
  213. schedule_work(&trans_pcie->rx_replenish);
  214. /* If we've added more space for the firmware to place data, tell it.
  215. * Increment device's write pointer in multiples of 8. */
  216. if (rxq->write_actual != (rxq->write & ~0x7)) {
  217. spin_lock_irqsave(&rxq->lock, flags);
  218. rxq->need_update = 1;
  219. spin_unlock_irqrestore(&rxq->lock, flags);
  220. iwl_rx_queue_update_write_ptr(trans, rxq);
  221. }
  222. }
  223. /**
  224. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  225. *
  226. * When moving to rx_free an SKB is allocated for the slot.
  227. *
  228. * Also restock the Rx queue via iwl_rx_queue_restock.
  229. * This is called as a scheduled work item (except for during initialization)
  230. */
  231. static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
  232. {
  233. struct iwl_trans_pcie *trans_pcie =
  234. IWL_TRANS_GET_PCIE_TRANS(trans);
  235. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  236. struct list_head *element;
  237. struct iwl_rx_mem_buffer *rxb;
  238. struct page *page;
  239. unsigned long flags;
  240. gfp_t gfp_mask = priority;
  241. while (1) {
  242. spin_lock_irqsave(&rxq->lock, flags);
  243. if (list_empty(&rxq->rx_used)) {
  244. spin_unlock_irqrestore(&rxq->lock, flags);
  245. return;
  246. }
  247. spin_unlock_irqrestore(&rxq->lock, flags);
  248. if (rxq->free_count > RX_LOW_WATERMARK)
  249. gfp_mask |= __GFP_NOWARN;
  250. if (hw_params(trans).rx_page_order > 0)
  251. gfp_mask |= __GFP_COMP;
  252. /* Alloc a new receive buffer */
  253. page = alloc_pages(gfp_mask,
  254. hw_params(trans).rx_page_order);
  255. if (!page) {
  256. if (net_ratelimit())
  257. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  258. "order: %d\n",
  259. hw_params(trans).rx_page_order);
  260. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  261. net_ratelimit())
  262. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  263. "Only %u free buffers remaining.\n",
  264. priority == GFP_ATOMIC ?
  265. "GFP_ATOMIC" : "GFP_KERNEL",
  266. rxq->free_count);
  267. /* We don't reschedule replenish work here -- we will
  268. * call the restock method and if it still needs
  269. * more buffers it will schedule replenish */
  270. return;
  271. }
  272. spin_lock_irqsave(&rxq->lock, flags);
  273. if (list_empty(&rxq->rx_used)) {
  274. spin_unlock_irqrestore(&rxq->lock, flags);
  275. __free_pages(page, hw_params(trans).rx_page_order);
  276. return;
  277. }
  278. element = rxq->rx_used.next;
  279. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  280. list_del(element);
  281. spin_unlock_irqrestore(&rxq->lock, flags);
  282. BUG_ON(rxb->page);
  283. rxb->page = page;
  284. /* Get physical address of the RB */
  285. rxb->page_dma = dma_map_page(trans->dev, page, 0,
  286. PAGE_SIZE << hw_params(trans).rx_page_order,
  287. DMA_FROM_DEVICE);
  288. /* dma address must be no more than 36 bits */
  289. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  290. /* and also 256 byte aligned! */
  291. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  292. spin_lock_irqsave(&rxq->lock, flags);
  293. list_add_tail(&rxb->list, &rxq->rx_free);
  294. rxq->free_count++;
  295. spin_unlock_irqrestore(&rxq->lock, flags);
  296. }
  297. }
  298. void iwlagn_rx_replenish(struct iwl_trans *trans)
  299. {
  300. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  301. unsigned long flags;
  302. iwlagn_rx_allocate(trans, GFP_KERNEL);
  303. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  304. iwlagn_rx_queue_restock(trans);
  305. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  306. }
  307. static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
  308. {
  309. iwlagn_rx_allocate(trans, GFP_ATOMIC);
  310. iwlagn_rx_queue_restock(trans);
  311. }
  312. void iwl_bg_rx_replenish(struct work_struct *data)
  313. {
  314. struct iwl_trans_pcie *trans_pcie =
  315. container_of(data, struct iwl_trans_pcie, rx_replenish);
  316. iwlagn_rx_replenish(trans_pcie->trans);
  317. }
  318. static void iwl_rx_handle_rxbuf(struct iwl_trans *trans,
  319. struct iwl_rx_mem_buffer *rxb)
  320. {
  321. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  322. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  323. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  324. unsigned long flags;
  325. bool page_stolen = false;
  326. int max_len = PAGE_SIZE << hw_params(trans).rx_page_order;
  327. u32 offset = 0;
  328. if (WARN_ON(!rxb))
  329. return;
  330. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  331. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  332. struct iwl_rx_packet *pkt;
  333. struct iwl_device_cmd *cmd;
  334. u16 sequence;
  335. bool reclaim;
  336. int index, cmd_index, err, len;
  337. struct iwl_rx_cmd_buffer rxcb = {
  338. ._offset = offset,
  339. ._page = rxb->page,
  340. ._page_stolen = false,
  341. };
  342. pkt = rxb_addr(&rxcb);
  343. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  344. break;
  345. IWL_DEBUG_RX(trans, "cmd at offset %d: %s (0x%.2x)\n",
  346. rxcb._offset, get_cmd_string(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->cmd[cmd_index];
  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, hw_params(trans).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 << hw_params(trans).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, "r = %d, i = %d\n", r, i);
  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: r = %d, i = %d (%p)\n", rxb);
  446. iwl_rx_handle_rxbuf(trans, rxb);
  447. i = (i + 1) & RX_QUEUE_MASK;
  448. /* If there are a lot of unused frames,
  449. * restock the Rx queue so ucode wont assert. */
  450. if (fill_rx) {
  451. count++;
  452. if (count >= 8) {
  453. rxq->read = i;
  454. iwlagn_rx_replenish_now(trans);
  455. count = 0;
  456. }
  457. }
  458. }
  459. /* Backtrack one entry */
  460. rxq->read = i;
  461. if (fill_rx)
  462. iwlagn_rx_replenish_now(trans);
  463. else
  464. iwlagn_rx_queue_restock(trans);
  465. }
  466. /**
  467. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  468. */
  469. static void iwl_irq_handle_error(struct iwl_trans *trans)
  470. {
  471. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  472. if (cfg(trans)->internal_wimax_coex &&
  473. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  474. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  475. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  476. APMG_PS_CTRL_VAL_RESET_REQ))) {
  477. /*
  478. * Keep the restart process from trying to send host
  479. * commands by clearing the ready bit.
  480. */
  481. clear_bit(STATUS_READY, &trans->shrd->status);
  482. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  483. wake_up(&trans->wait_command_queue);
  484. IWL_ERR(trans, "RF is used by WiMAX\n");
  485. return;
  486. }
  487. iwl_dump_csr(trans);
  488. iwl_dump_fh(trans, NULL, false);
  489. iwl_op_mode_nic_error(trans->op_mode);
  490. }
  491. /* tasklet for iwlagn interrupt */
  492. void iwl_irq_tasklet(struct iwl_trans *trans)
  493. {
  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. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  502. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  503. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  504. /* Ack/clear/reset pending uCode interrupts.
  505. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  506. */
  507. /* There is a hardware bug in the interrupt mask function that some
  508. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  509. * they are disabled in the CSR_INT_MASK register. Furthermore the
  510. * ICT interrupt handling mechanism has another bug that might cause
  511. * these unmasked interrupts fail to be detected. We workaround the
  512. * hardware bugs here by ACKing all the possible interrupts so that
  513. * interrupt coalescing can still be achieved.
  514. */
  515. iwl_write32(trans, CSR_INT,
  516. trans_pcie->inta | ~trans_pcie->inta_mask);
  517. inta = trans_pcie->inta;
  518. #ifdef CONFIG_IWLWIFI_DEBUG
  519. if (iwl_have_debug_level(IWL_DL_ISR)) {
  520. /* just for debug */
  521. inta_mask = iwl_read32(trans, CSR_INT_MASK);
  522. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  523. inta, inta_mask);
  524. }
  525. #endif
  526. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  527. trans_pcie->inta = 0;
  528. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  529. /* Now service all interrupt bits discovered above. */
  530. if (inta & CSR_INT_BIT_HW_ERR) {
  531. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  532. /* Tell the device to stop sending interrupts */
  533. iwl_disable_interrupts(trans);
  534. isr_stats->hw++;
  535. iwl_irq_handle_error(trans);
  536. handled |= CSR_INT_BIT_HW_ERR;
  537. return;
  538. }
  539. #ifdef CONFIG_IWLWIFI_DEBUG
  540. if (iwl_have_debug_level(IWL_DL_ISR)) {
  541. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  542. if (inta & CSR_INT_BIT_SCD) {
  543. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  544. "the frame/frames.\n");
  545. isr_stats->sch++;
  546. }
  547. /* Alive notification via Rx interrupt will do the real work */
  548. if (inta & CSR_INT_BIT_ALIVE) {
  549. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  550. isr_stats->alive++;
  551. }
  552. }
  553. #endif
  554. /* Safely ignore these bits for debug checks below */
  555. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  556. /* HW RF KILL switch toggled */
  557. if (inta & CSR_INT_BIT_RF_KILL) {
  558. bool hw_rfkill;
  559. hw_rfkill = !(iwl_read32(trans, CSR_GP_CNTRL) &
  560. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  561. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  562. hw_rfkill ? "disable radio" : "enable radio");
  563. isr_stats->rfkill++;
  564. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  565. handled |= CSR_INT_BIT_RF_KILL;
  566. }
  567. /* Chip got too hot and stopped itself */
  568. if (inta & CSR_INT_BIT_CT_KILL) {
  569. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  570. isr_stats->ctkill++;
  571. handled |= CSR_INT_BIT_CT_KILL;
  572. }
  573. /* Error detected by uCode */
  574. if (inta & CSR_INT_BIT_SW_ERR) {
  575. IWL_ERR(trans, "Microcode SW error detected. "
  576. " Restarting 0x%X.\n", inta);
  577. isr_stats->sw++;
  578. iwl_irq_handle_error(trans);
  579. handled |= CSR_INT_BIT_SW_ERR;
  580. }
  581. /* uCode wakes up after power-down sleep */
  582. if (inta & CSR_INT_BIT_WAKEUP) {
  583. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  584. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  585. for (i = 0; i < cfg(trans)->base_params->num_of_queues; i++)
  586. iwl_txq_update_write_ptr(trans,
  587. &trans_pcie->txq[i]);
  588. isr_stats->wakeup++;
  589. handled |= CSR_INT_BIT_WAKEUP;
  590. }
  591. /* All uCode command responses, including Tx command responses,
  592. * Rx "responses" (frame-received notification), and other
  593. * notifications from uCode come through here*/
  594. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  595. CSR_INT_BIT_RX_PERIODIC)) {
  596. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  597. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  598. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  599. iwl_write32(trans, CSR_FH_INT_STATUS,
  600. CSR_FH_INT_RX_MASK);
  601. }
  602. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  603. handled |= CSR_INT_BIT_RX_PERIODIC;
  604. iwl_write32(trans,
  605. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  606. }
  607. /* Sending RX interrupt require many steps to be done in the
  608. * the device:
  609. * 1- write interrupt to current index in ICT table.
  610. * 2- dma RX frame.
  611. * 3- update RX shared data to indicate last write index.
  612. * 4- send interrupt.
  613. * This could lead to RX race, driver could receive RX interrupt
  614. * but the shared data changes does not reflect this;
  615. * periodic interrupt will detect any dangling Rx activity.
  616. */
  617. /* Disable periodic interrupt; we use it as just a one-shot. */
  618. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  619. CSR_INT_PERIODIC_DIS);
  620. #ifdef CONFIG_IWLWIFI_IDI
  621. iwl_amfh_rx_handler();
  622. #else
  623. iwl_rx_handle(trans);
  624. #endif
  625. /*
  626. * Enable periodic interrupt in 8 msec only if we received
  627. * real RX interrupt (instead of just periodic int), to catch
  628. * any dangling Rx interrupt. If it was just the periodic
  629. * interrupt, there was no dangling Rx activity, and no need
  630. * to extend the periodic interrupt; one-shot is enough.
  631. */
  632. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  633. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  634. CSR_INT_PERIODIC_ENA);
  635. isr_stats->rx++;
  636. }
  637. /* This "Tx" DMA channel is used only for loading uCode */
  638. if (inta & CSR_INT_BIT_FH_TX) {
  639. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  640. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  641. isr_stats->tx++;
  642. handled |= CSR_INT_BIT_FH_TX;
  643. /* Wake up uCode load routine, now that load is complete */
  644. trans_pcie->ucode_write_complete = true;
  645. wake_up(&trans_pcie->ucode_write_waitq);
  646. }
  647. if (inta & ~handled) {
  648. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  649. isr_stats->unhandled++;
  650. }
  651. if (inta & ~(trans_pcie->inta_mask)) {
  652. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  653. inta & ~trans_pcie->inta_mask);
  654. }
  655. /* Re-enable all interrupts */
  656. /* only Re-enable if disabled by irq */
  657. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
  658. iwl_enable_interrupts(trans);
  659. /* Re-enable RF_KILL if it occurred */
  660. else if (handled & CSR_INT_BIT_RF_KILL)
  661. iwl_enable_rfkill_int(trans);
  662. }
  663. /******************************************************************************
  664. *
  665. * ICT functions
  666. *
  667. ******************************************************************************/
  668. /* a device (PCI-E) page is 4096 bytes long */
  669. #define ICT_SHIFT 12
  670. #define ICT_SIZE (1 << ICT_SHIFT)
  671. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  672. /* Free dram table */
  673. void iwl_free_isr_ict(struct iwl_trans *trans)
  674. {
  675. struct iwl_trans_pcie *trans_pcie =
  676. IWL_TRANS_GET_PCIE_TRANS(trans);
  677. if (trans_pcie->ict_tbl) {
  678. dma_free_coherent(trans->dev, ICT_SIZE,
  679. trans_pcie->ict_tbl,
  680. trans_pcie->ict_tbl_dma);
  681. trans_pcie->ict_tbl = NULL;
  682. trans_pcie->ict_tbl_dma = 0;
  683. }
  684. }
  685. /*
  686. * allocate dram shared table, it is an aligned memory
  687. * block of ICT_SIZE.
  688. * also reset all data related to ICT table interrupt.
  689. */
  690. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  691. {
  692. struct iwl_trans_pcie *trans_pcie =
  693. IWL_TRANS_GET_PCIE_TRANS(trans);
  694. trans_pcie->ict_tbl =
  695. dma_alloc_coherent(trans->dev, ICT_SIZE,
  696. &trans_pcie->ict_tbl_dma,
  697. GFP_KERNEL);
  698. if (!trans_pcie->ict_tbl)
  699. return -ENOMEM;
  700. /* just an API sanity check ... it is guaranteed to be aligned */
  701. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  702. iwl_free_isr_ict(trans);
  703. return -EINVAL;
  704. }
  705. IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
  706. (unsigned long long)trans_pcie->ict_tbl_dma);
  707. IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
  708. /* reset table and index to all 0 */
  709. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  710. trans_pcie->ict_index = 0;
  711. /* add periodic RX interrupt */
  712. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  713. return 0;
  714. }
  715. /* Device is going up inform it about using ICT interrupt table,
  716. * also we need to tell the driver to start using ICT interrupt.
  717. */
  718. void iwl_reset_ict(struct iwl_trans *trans)
  719. {
  720. u32 val;
  721. unsigned long flags;
  722. struct iwl_trans_pcie *trans_pcie =
  723. IWL_TRANS_GET_PCIE_TRANS(trans);
  724. if (!trans_pcie->ict_tbl)
  725. return;
  726. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  727. iwl_disable_interrupts(trans);
  728. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  729. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  730. val |= CSR_DRAM_INT_TBL_ENABLE;
  731. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  732. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  733. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  734. trans_pcie->use_ict = true;
  735. trans_pcie->ict_index = 0;
  736. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  737. iwl_enable_interrupts(trans);
  738. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  739. }
  740. /* Device is going down disable ict interrupt usage */
  741. void iwl_disable_ict(struct iwl_trans *trans)
  742. {
  743. struct iwl_trans_pcie *trans_pcie =
  744. IWL_TRANS_GET_PCIE_TRANS(trans);
  745. unsigned long flags;
  746. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  747. trans_pcie->use_ict = false;
  748. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  749. }
  750. static irqreturn_t iwl_isr(int irq, void *data)
  751. {
  752. struct iwl_trans *trans = data;
  753. struct iwl_trans_pcie *trans_pcie;
  754. u32 inta, inta_mask;
  755. unsigned long flags;
  756. #ifdef CONFIG_IWLWIFI_DEBUG
  757. u32 inta_fh;
  758. #endif
  759. if (!trans)
  760. return IRQ_NONE;
  761. trace_iwlwifi_dev_irq(trans->dev);
  762. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  763. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  764. /* Disable (but don't clear!) interrupts here to avoid
  765. * back-to-back ISRs and sporadic interrupts from our NIC.
  766. * If we have something to service, the tasklet will re-enable ints.
  767. * If we *don't* have something, we'll re-enable before leaving here. */
  768. inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
  769. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  770. /* Discover which interrupts are active/pending */
  771. inta = iwl_read32(trans, CSR_INT);
  772. /* Ignore interrupt if there's nothing in NIC to service.
  773. * This may be due to IRQ shared with another device,
  774. * or due to sporadic interrupts thrown from our NIC. */
  775. if (!inta) {
  776. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  777. goto none;
  778. }
  779. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  780. /* Hardware disappeared. It might have already raised
  781. * an interrupt */
  782. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  783. goto unplugged;
  784. }
  785. #ifdef CONFIG_IWLWIFI_DEBUG
  786. if (iwl_have_debug_level(IWL_DL_ISR)) {
  787. inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
  788. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  789. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  790. }
  791. #endif
  792. trans_pcie->inta |= inta;
  793. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  794. if (likely(inta))
  795. tasklet_schedule(&trans_pcie->irq_tasklet);
  796. else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  797. !trans_pcie->inta)
  798. iwl_enable_interrupts(trans);
  799. unplugged:
  800. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  801. return IRQ_HANDLED;
  802. none:
  803. /* re-enable interrupts here since we don't have anything to service. */
  804. /* only Re-enable if disabled by irq and no schedules tasklet. */
  805. if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
  806. !trans_pcie->inta)
  807. iwl_enable_interrupts(trans);
  808. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  809. return IRQ_NONE;
  810. }
  811. /* interrupt handler using ict table, with this interrupt driver will
  812. * stop using INTA register to get device's interrupt, reading this register
  813. * is expensive, device will write interrupts in ICT dram table, increment
  814. * index then will fire interrupt to driver, driver will OR all ICT table
  815. * entries from current index up to table entry with 0 value. the result is
  816. * the interrupt we need to service, driver will set the entries back to 0 and
  817. * set index.
  818. */
  819. irqreturn_t iwl_isr_ict(int irq, void *data)
  820. {
  821. struct iwl_trans *trans = data;
  822. struct iwl_trans_pcie *trans_pcie;
  823. u32 inta, inta_mask;
  824. u32 val = 0;
  825. u32 read;
  826. unsigned long flags;
  827. if (!trans)
  828. return IRQ_NONE;
  829. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  830. /* dram interrupt table not set yet,
  831. * use legacy interrupt.
  832. */
  833. if (!trans_pcie->use_ict)
  834. return iwl_isr(irq, data);
  835. trace_iwlwifi_dev_irq(trans->dev);
  836. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  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. }