rx.c 33 KB

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