iwl-trans-pcie-rx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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. /*TODO: Remove include to iwl-core.h*/
  33. #include "iwl-core.h"
  34. #include "iwl-io.h"
  35. #include "iwl-helpers.h"
  36. #include "iwl-trans-pcie-int.h"
  37. /******************************************************************************
  38. *
  39. * RX path functions
  40. *
  41. ******************************************************************************/
  42. /*
  43. * Rx theory of operation
  44. *
  45. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  46. * each of which point to Receive Buffers to be filled by the NIC. These get
  47. * used not only for Rx frames, but for any command response or notification
  48. * from the NIC. The driver and NIC manage the Rx buffers by means
  49. * of indexes into the circular buffer.
  50. *
  51. * Rx Queue Indexes
  52. * The host/firmware share two index registers for managing the Rx buffers.
  53. *
  54. * The READ index maps to the first position that the firmware may be writing
  55. * to -- the driver can read up to (but not including) this position and get
  56. * good data.
  57. * The READ index is managed by the firmware once the card is enabled.
  58. *
  59. * The WRITE index maps to the last position the driver has read from -- the
  60. * position preceding WRITE is the last slot the firmware can place a packet.
  61. *
  62. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  63. * WRITE = READ.
  64. *
  65. * During initialization, the host sets up the READ queue position to the first
  66. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  67. *
  68. * When the firmware places a packet in a buffer, it will advance the READ index
  69. * and fire the RX interrupt. The driver can then query the READ index and
  70. * process as many packets as possible, moving the WRITE index forward as it
  71. * resets the Rx queue buffers with new memory.
  72. *
  73. * The management in the driver is as follows:
  74. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  75. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  76. * to replenish the iwl->rxq->rx_free.
  77. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  78. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  79. * 'processed' and 'read' driver indexes as well)
  80. * + A received packet is processed and handed to the kernel network stack,
  81. * detached from the iwl->rxq. The driver 'processed' index is updated.
  82. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  83. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  84. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  85. * were enough free buffers and RX_STALLED is set it is cleared.
  86. *
  87. *
  88. * Driver sequence:
  89. *
  90. * iwl_rx_queue_alloc() Allocates rx_free
  91. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  92. * iwl_rx_queue_restock
  93. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  94. * queue, updates firmware pointers, and updates
  95. * the WRITE index. If insufficient rx_free buffers
  96. * are available, schedules iwl_rx_replenish
  97. *
  98. * -- enable interrupts --
  99. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  100. * READ INDEX, detaching the SKB from the pool.
  101. * Moves the packet buffer from queue to rx_used.
  102. * Calls iwl_rx_queue_restock to refill any empty
  103. * slots.
  104. * ...
  105. *
  106. */
  107. /**
  108. * iwl_rx_queue_space - Return number of free slots available in queue.
  109. */
  110. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  111. {
  112. int s = q->read - q->write;
  113. if (s <= 0)
  114. s += RX_QUEUE_SIZE;
  115. /* keep some buffer to not confuse full and empty queue */
  116. s -= 2;
  117. if (s < 0)
  118. s = 0;
  119. return s;
  120. }
  121. /**
  122. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  123. */
  124. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  125. struct iwl_rx_queue *q)
  126. {
  127. unsigned long flags;
  128. u32 reg;
  129. spin_lock_irqsave(&q->lock, flags);
  130. if (q->need_update == 0)
  131. goto exit_unlock;
  132. if (hw_params(trans).shadow_reg_enable) {
  133. /* shadow register enabled */
  134. /* Device expects a multiple of 8 */
  135. q->write_actual = (q->write & ~0x7);
  136. iwl_write32(bus(trans), FH_RSCSR_CHNL0_WPTR, q->write_actual);
  137. } else {
  138. /* If power-saving is in use, make sure device is awake */
  139. if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
  140. reg = iwl_read32(bus(trans), CSR_UCODE_DRV_GP1);
  141. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  142. IWL_DEBUG_INFO(trans,
  143. "Rx queue requesting wakeup,"
  144. " GP1 = 0x%x\n", reg);
  145. iwl_set_bit(bus(trans), CSR_GP_CNTRL,
  146. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  147. goto exit_unlock;
  148. }
  149. q->write_actual = (q->write & ~0x7);
  150. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_WPTR,
  151. q->write_actual);
  152. /* Else device is assumed to be awake */
  153. } else {
  154. /* Device expects a multiple of 8 */
  155. q->write_actual = (q->write & ~0x7);
  156. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_WPTR,
  157. q->write_actual);
  158. }
  159. }
  160. q->need_update = 0;
  161. exit_unlock:
  162. spin_unlock_irqrestore(&q->lock, flags);
  163. }
  164. /**
  165. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  166. */
  167. static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  168. {
  169. return cpu_to_le32((u32)(dma_addr >> 8));
  170. }
  171. /**
  172. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  173. *
  174. * If there are slots in the RX queue that need to be restocked,
  175. * and we have free pre-allocated buffers, fill the ranks as much
  176. * as we can, pulling from rx_free.
  177. *
  178. * This moves the 'write' index forward to catch up with 'processed', and
  179. * also updates the memory address in the firmware to reference the new
  180. * target buffer.
  181. */
  182. static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
  183. {
  184. struct iwl_trans_pcie *trans_pcie =
  185. IWL_TRANS_GET_PCIE_TRANS(trans);
  186. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  187. struct list_head *element;
  188. struct iwl_rx_mem_buffer *rxb;
  189. unsigned long flags;
  190. spin_lock_irqsave(&rxq->lock, flags);
  191. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  192. /* The overwritten rxb must be a used one */
  193. rxb = rxq->queue[rxq->write];
  194. BUG_ON(rxb && rxb->page);
  195. /* Get next free Rx buffer, remove from free list */
  196. element = rxq->rx_free.next;
  197. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  198. list_del(element);
  199. /* Point to Rx buffer via next RBD in circular buffer */
  200. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
  201. rxq->queue[rxq->write] = rxb;
  202. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  203. rxq->free_count--;
  204. }
  205. spin_unlock_irqrestore(&rxq->lock, flags);
  206. /* If the pre-allocated buffer pool is dropping low, schedule to
  207. * refill it */
  208. if (rxq->free_count <= RX_LOW_WATERMARK)
  209. queue_work(trans->shrd->workqueue, &trans_pcie->rx_replenish);
  210. /* If we've added more space for the firmware to place data, tell it.
  211. * Increment device's write pointer in multiples of 8. */
  212. if (rxq->write_actual != (rxq->write & ~0x7)) {
  213. spin_lock_irqsave(&rxq->lock, flags);
  214. rxq->need_update = 1;
  215. spin_unlock_irqrestore(&rxq->lock, flags);
  216. iwl_rx_queue_update_write_ptr(trans, rxq);
  217. }
  218. }
  219. /**
  220. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  221. *
  222. * When moving to rx_free an SKB is allocated for the slot.
  223. *
  224. * Also restock the Rx queue via iwl_rx_queue_restock.
  225. * This is called as a scheduled work item (except for during initialization)
  226. */
  227. static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
  228. {
  229. struct iwl_trans_pcie *trans_pcie =
  230. IWL_TRANS_GET_PCIE_TRANS(trans);
  231. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  232. struct list_head *element;
  233. struct iwl_rx_mem_buffer *rxb;
  234. struct page *page;
  235. unsigned long flags;
  236. gfp_t gfp_mask = priority;
  237. while (1) {
  238. spin_lock_irqsave(&rxq->lock, flags);
  239. if (list_empty(&rxq->rx_used)) {
  240. spin_unlock_irqrestore(&rxq->lock, flags);
  241. return;
  242. }
  243. spin_unlock_irqrestore(&rxq->lock, flags);
  244. if (rxq->free_count > RX_LOW_WATERMARK)
  245. gfp_mask |= __GFP_NOWARN;
  246. if (hw_params(trans).rx_page_order > 0)
  247. gfp_mask |= __GFP_COMP;
  248. /* Alloc a new receive buffer */
  249. page = alloc_pages(gfp_mask,
  250. hw_params(trans).rx_page_order);
  251. if (!page) {
  252. if (net_ratelimit())
  253. IWL_DEBUG_INFO(trans, "alloc_pages failed, "
  254. "order: %d\n",
  255. hw_params(trans).rx_page_order);
  256. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  257. net_ratelimit())
  258. IWL_CRIT(trans, "Failed to alloc_pages with %s."
  259. "Only %u free buffers remaining.\n",
  260. priority == GFP_ATOMIC ?
  261. "GFP_ATOMIC" : "GFP_KERNEL",
  262. rxq->free_count);
  263. /* We don't reschedule replenish work here -- we will
  264. * call the restock method and if it still needs
  265. * more buffers it will schedule replenish */
  266. return;
  267. }
  268. spin_lock_irqsave(&rxq->lock, flags);
  269. if (list_empty(&rxq->rx_used)) {
  270. spin_unlock_irqrestore(&rxq->lock, flags);
  271. __free_pages(page, hw_params(trans).rx_page_order);
  272. return;
  273. }
  274. element = rxq->rx_used.next;
  275. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  276. list_del(element);
  277. spin_unlock_irqrestore(&rxq->lock, flags);
  278. BUG_ON(rxb->page);
  279. rxb->page = page;
  280. /* Get physical address of the RB */
  281. rxb->page_dma = dma_map_page(bus(trans)->dev, page, 0,
  282. PAGE_SIZE << hw_params(trans).rx_page_order,
  283. DMA_FROM_DEVICE);
  284. /* dma address must be no more than 36 bits */
  285. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  286. /* and also 256 byte aligned! */
  287. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  288. spin_lock_irqsave(&rxq->lock, flags);
  289. list_add_tail(&rxb->list, &rxq->rx_free);
  290. rxq->free_count++;
  291. spin_unlock_irqrestore(&rxq->lock, flags);
  292. }
  293. }
  294. void iwlagn_rx_replenish(struct iwl_trans *trans)
  295. {
  296. unsigned long flags;
  297. iwlagn_rx_allocate(trans, GFP_KERNEL);
  298. spin_lock_irqsave(&trans->shrd->lock, flags);
  299. iwlagn_rx_queue_restock(trans);
  300. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  301. }
  302. static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
  303. {
  304. iwlagn_rx_allocate(trans, GFP_ATOMIC);
  305. iwlagn_rx_queue_restock(trans);
  306. }
  307. void iwl_bg_rx_replenish(struct work_struct *data)
  308. {
  309. struct iwl_trans_pcie *trans_pcie =
  310. container_of(data, struct iwl_trans_pcie, rx_replenish);
  311. struct iwl_trans *trans = trans_pcie->trans;
  312. if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
  313. return;
  314. mutex_lock(&trans->shrd->mutex);
  315. iwlagn_rx_replenish(trans);
  316. mutex_unlock(&trans->shrd->mutex);
  317. }
  318. /**
  319. * iwl_rx_handle - Main entry function for receiving responses from uCode
  320. *
  321. * Uses the priv->rx_handlers callback function array to invoke
  322. * the appropriate handlers, including command responses,
  323. * frame-received notifications, and other notifications.
  324. */
  325. static void iwl_rx_handle(struct iwl_trans *trans)
  326. {
  327. struct iwl_rx_mem_buffer *rxb;
  328. struct iwl_rx_packet *pkt;
  329. struct iwl_trans_pcie *trans_pcie =
  330. IWL_TRANS_GET_PCIE_TRANS(trans);
  331. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  332. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  333. struct iwl_device_cmd *cmd;
  334. u32 r, i;
  335. int reclaim;
  336. unsigned long flags;
  337. u8 fill_rx = 0;
  338. u32 count = 8;
  339. int total_empty;
  340. int index, cmd_index;
  341. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  342. * buffer that the driver may process (last buffer filled by ucode). */
  343. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  344. i = rxq->read;
  345. /* Rx interrupt, but nothing sent from uCode */
  346. if (i == r)
  347. IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
  348. /* calculate total frames need to be restock after handling RX */
  349. total_empty = r - rxq->write_actual;
  350. if (total_empty < 0)
  351. total_empty += RX_QUEUE_SIZE;
  352. if (total_empty > (RX_QUEUE_SIZE / 2))
  353. fill_rx = 1;
  354. while (i != r) {
  355. int len, err;
  356. u16 sequence;
  357. rxb = rxq->queue[i];
  358. /* If an RXB doesn't have a Rx queue slot associated with it,
  359. * then a bug has been introduced in the queue refilling
  360. * routines -- catch it here */
  361. if (WARN_ON(rxb == NULL)) {
  362. i = (i + 1) & RX_QUEUE_MASK;
  363. continue;
  364. }
  365. rxq->queue[i] = NULL;
  366. dma_unmap_page(bus(trans)->dev, rxb->page_dma,
  367. PAGE_SIZE << hw_params(trans).rx_page_order,
  368. DMA_FROM_DEVICE);
  369. pkt = rxb_addr(rxb);
  370. IWL_DEBUG_RX(trans, "r = %d, i = %d, %s, 0x%02x\n", r,
  371. i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  372. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  373. len += sizeof(u32); /* account for status word */
  374. trace_iwlwifi_dev_rx(priv(trans), pkt, len);
  375. /* Reclaim a command buffer only if this packet is a response
  376. * to a (driver-originated) command.
  377. * If the packet (e.g. Rx frame) originated from uCode,
  378. * there is no command buffer to reclaim.
  379. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  380. * but apparently a few don't get set; catch them here. */
  381. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  382. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  383. (pkt->hdr.cmd != REPLY_RX) &&
  384. (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
  385. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  386. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  387. (pkt->hdr.cmd != REPLY_TX);
  388. sequence = le16_to_cpu(pkt->hdr.sequence);
  389. index = SEQ_TO_INDEX(sequence);
  390. cmd_index = get_cmd_index(&txq->q, index);
  391. if (reclaim)
  392. cmd = txq->cmd[cmd_index];
  393. else
  394. cmd = NULL;
  395. /* warn if this is cmd response / notification and the uCode
  396. * didn't set the SEQ_RX_FRAME for a frame that is
  397. * uCode-originated
  398. * If you saw this code after the second half of 2012, then
  399. * please remove it
  400. */
  401. WARN(pkt->hdr.cmd != REPLY_TX && reclaim == false &&
  402. (!(pkt->hdr.sequence & SEQ_RX_FRAME)),
  403. "reclaim is false, SEQ_RX_FRAME unset: %s\n",
  404. get_cmd_string(pkt->hdr.cmd));
  405. err = iwl_rx_dispatch(priv(trans), rxb, cmd);
  406. /*
  407. * XXX: After here, we should always check rxb->page
  408. * against NULL before touching it or its virtual
  409. * memory (pkt). Because some rx_handler might have
  410. * already taken or freed the pages.
  411. */
  412. if (reclaim) {
  413. /* Invoke any callbacks, transfer the buffer to caller,
  414. * and fire off the (possibly) blocking
  415. * iwl_trans_send_cmd()
  416. * as we reclaim the driver command queue */
  417. if (rxb->page)
  418. iwl_tx_cmd_complete(trans, rxb, err);
  419. else
  420. IWL_WARN(trans, "Claim null rxb?\n");
  421. }
  422. /* Reuse the page if possible. For notification packets and
  423. * SKBs that fail to Rx correctly, add them back into the
  424. * rx_free list for reuse later. */
  425. spin_lock_irqsave(&rxq->lock, flags);
  426. if (rxb->page != NULL) {
  427. rxb->page_dma = dma_map_page(bus(trans)->dev, rxb->page,
  428. 0, PAGE_SIZE <<
  429. hw_params(trans).rx_page_order,
  430. DMA_FROM_DEVICE);
  431. list_add_tail(&rxb->list, &rxq->rx_free);
  432. rxq->free_count++;
  433. } else
  434. list_add_tail(&rxb->list, &rxq->rx_used);
  435. spin_unlock_irqrestore(&rxq->lock, flags);
  436. i = (i + 1) & RX_QUEUE_MASK;
  437. /* If there are a lot of unused frames,
  438. * restock the Rx queue so ucode wont assert. */
  439. if (fill_rx) {
  440. count++;
  441. if (count >= 8) {
  442. rxq->read = i;
  443. iwlagn_rx_replenish_now(trans);
  444. count = 0;
  445. }
  446. }
  447. }
  448. /* Backtrack one entry */
  449. rxq->read = i;
  450. if (fill_rx)
  451. iwlagn_rx_replenish_now(trans);
  452. else
  453. iwlagn_rx_queue_restock(trans);
  454. }
  455. static const char * const desc_lookup_text[] = {
  456. "OK",
  457. "FAIL",
  458. "BAD_PARAM",
  459. "BAD_CHECKSUM",
  460. "NMI_INTERRUPT_WDG",
  461. "SYSASSERT",
  462. "FATAL_ERROR",
  463. "BAD_COMMAND",
  464. "HW_ERROR_TUNE_LOCK",
  465. "HW_ERROR_TEMPERATURE",
  466. "ILLEGAL_CHAN_FREQ",
  467. "VCC_NOT_STABLE",
  468. "FH_ERROR",
  469. "NMI_INTERRUPT_HOST",
  470. "NMI_INTERRUPT_ACTION_PT",
  471. "NMI_INTERRUPT_UNKNOWN",
  472. "UCODE_VERSION_MISMATCH",
  473. "HW_ERROR_ABS_LOCK",
  474. "HW_ERROR_CAL_LOCK_FAIL",
  475. "NMI_INTERRUPT_INST_ACTION_PT",
  476. "NMI_INTERRUPT_DATA_ACTION_PT",
  477. "NMI_TRM_HW_ER",
  478. "NMI_INTERRUPT_TRM",
  479. "NMI_INTERRUPT_BREAK_POINT",
  480. "DEBUG_0",
  481. "DEBUG_1",
  482. "DEBUG_2",
  483. "DEBUG_3",
  484. };
  485. static struct { char *name; u8 num; } advanced_lookup[] = {
  486. { "NMI_INTERRUPT_WDG", 0x34 },
  487. { "SYSASSERT", 0x35 },
  488. { "UCODE_VERSION_MISMATCH", 0x37 },
  489. { "BAD_COMMAND", 0x38 },
  490. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  491. { "FATAL_ERROR", 0x3D },
  492. { "NMI_TRM_HW_ERR", 0x46 },
  493. { "NMI_INTERRUPT_TRM", 0x4C },
  494. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  495. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  496. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  497. { "NMI_INTERRUPT_HOST", 0x66 },
  498. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  499. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  500. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  501. { "ADVANCED_SYSASSERT", 0 },
  502. };
  503. static const char *desc_lookup(u32 num)
  504. {
  505. int i;
  506. int max = ARRAY_SIZE(desc_lookup_text);
  507. if (num < max)
  508. return desc_lookup_text[num];
  509. max = ARRAY_SIZE(advanced_lookup) - 1;
  510. for (i = 0; i < max; i++) {
  511. if (advanced_lookup[i].num == num)
  512. break;
  513. }
  514. return advanced_lookup[i].name;
  515. }
  516. #define ERROR_START_OFFSET (1 * sizeof(u32))
  517. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  518. static void iwl_dump_nic_error_log(struct iwl_trans *trans)
  519. {
  520. u32 base;
  521. struct iwl_error_event_table table;
  522. struct iwl_priv *priv = priv(trans);
  523. struct iwl_trans_pcie *trans_pcie =
  524. IWL_TRANS_GET_PCIE_TRANS(trans);
  525. base = priv->device_pointers.error_event_table;
  526. if (priv->ucode_type == IWL_UCODE_INIT) {
  527. if (!base)
  528. base = priv->init_errlog_ptr;
  529. } else {
  530. if (!base)
  531. base = priv->inst_errlog_ptr;
  532. }
  533. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  534. IWL_ERR(trans,
  535. "Not valid error log pointer 0x%08X for %s uCode\n",
  536. base,
  537. (priv->ucode_type == IWL_UCODE_INIT)
  538. ? "Init" : "RT");
  539. return;
  540. }
  541. iwl_read_targ_mem_words(bus(priv), base, &table, sizeof(table));
  542. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  543. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  544. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  545. trans->shrd->status, table.valid);
  546. }
  547. trans_pcie->isr_stats.err_code = table.error_id;
  548. trace_iwlwifi_dev_ucode_error(priv, table.error_id, table.tsf_low,
  549. table.data1, table.data2, table.line,
  550. table.blink1, table.blink2, table.ilink1,
  551. table.ilink2, table.bcon_time, table.gp1,
  552. table.gp2, table.gp3, table.ucode_ver,
  553. table.hw_ver, table.brd_ver);
  554. IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
  555. desc_lookup(table.error_id));
  556. IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
  557. IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
  558. IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
  559. IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
  560. IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
  561. IWL_ERR(trans, "0x%08X | data1\n", table.data1);
  562. IWL_ERR(trans, "0x%08X | data2\n", table.data2);
  563. IWL_ERR(trans, "0x%08X | line\n", table.line);
  564. IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
  565. IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
  566. IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
  567. IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
  568. IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
  569. IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
  570. IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
  571. IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
  572. IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
  573. IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
  574. }
  575. /**
  576. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  577. */
  578. static void iwl_irq_handle_error(struct iwl_trans *trans)
  579. {
  580. struct iwl_priv *priv = priv(trans);
  581. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  582. if (priv->cfg->internal_wimax_coex &&
  583. (!(iwl_read_prph(bus(trans), APMG_CLK_CTRL_REG) &
  584. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  585. (iwl_read_prph(bus(trans), APMG_PS_CTRL_REG) &
  586. APMG_PS_CTRL_VAL_RESET_REQ))) {
  587. /*
  588. * Keep the restart process from trying to send host
  589. * commands by clearing the ready bit.
  590. */
  591. clear_bit(STATUS_READY, &trans->shrd->status);
  592. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  593. wake_up(&priv->shrd->wait_command_queue);
  594. IWL_ERR(trans, "RF is used by WiMAX\n");
  595. return;
  596. }
  597. IWL_ERR(trans, "Loaded firmware version: %s\n",
  598. priv->hw->wiphy->fw_version);
  599. iwl_dump_nic_error_log(trans);
  600. iwl_dump_csr(trans);
  601. iwl_dump_fh(trans, NULL, false);
  602. iwl_dump_nic_event_log(trans, false, NULL, false);
  603. #ifdef CONFIG_IWLWIFI_DEBUG
  604. if (iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS)
  605. iwl_print_rx_config_cmd(priv(trans), IWL_RXON_CTX_BSS);
  606. #endif
  607. iwlagn_fw_error(priv, false);
  608. }
  609. #define EVENT_START_OFFSET (4 * sizeof(u32))
  610. /**
  611. * iwl_print_event_log - Dump error event log to syslog
  612. *
  613. */
  614. static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
  615. u32 num_events, u32 mode,
  616. int pos, char **buf, size_t bufsz)
  617. {
  618. u32 i;
  619. u32 base; /* SRAM byte address of event log header */
  620. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  621. u32 ptr; /* SRAM byte address of log data */
  622. u32 ev, time, data; /* event log data */
  623. unsigned long reg_flags;
  624. struct iwl_priv *priv = priv(trans);
  625. if (num_events == 0)
  626. return pos;
  627. base = priv->device_pointers.log_event_table;
  628. if (priv->ucode_type == IWL_UCODE_INIT) {
  629. if (!base)
  630. base = priv->init_evtlog_ptr;
  631. } else {
  632. if (!base)
  633. base = priv->inst_evtlog_ptr;
  634. }
  635. if (mode == 0)
  636. event_size = 2 * sizeof(u32);
  637. else
  638. event_size = 3 * sizeof(u32);
  639. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  640. /* Make sure device is powered up for SRAM reads */
  641. spin_lock_irqsave(&bus(trans)->reg_lock, reg_flags);
  642. iwl_grab_nic_access(bus(trans));
  643. /* Set starting address; reads will auto-increment */
  644. iwl_write32(bus(trans), HBUS_TARG_MEM_RADDR, ptr);
  645. rmb();
  646. /* "time" is actually "data" for mode 0 (no timestamp).
  647. * place event id # at far right for easier visual parsing. */
  648. for (i = 0; i < num_events; i++) {
  649. ev = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
  650. time = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
  651. if (mode == 0) {
  652. /* data, ev */
  653. if (bufsz) {
  654. pos += scnprintf(*buf + pos, bufsz - pos,
  655. "EVT_LOG:0x%08x:%04u\n",
  656. time, ev);
  657. } else {
  658. trace_iwlwifi_dev_ucode_event(priv, 0,
  659. time, ev);
  660. IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
  661. time, ev);
  662. }
  663. } else {
  664. data = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
  665. if (bufsz) {
  666. pos += scnprintf(*buf + pos, bufsz - pos,
  667. "EVT_LOGT:%010u:0x%08x:%04u\n",
  668. time, data, ev);
  669. } else {
  670. IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
  671. time, data, ev);
  672. trace_iwlwifi_dev_ucode_event(priv, time,
  673. data, ev);
  674. }
  675. }
  676. }
  677. /* Allow device to power down */
  678. iwl_release_nic_access(bus(trans));
  679. spin_unlock_irqrestore(&bus(trans)->reg_lock, reg_flags);
  680. return pos;
  681. }
  682. /**
  683. * iwl_print_last_event_logs - Dump the newest # of event log to syslog
  684. */
  685. static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
  686. u32 num_wraps, u32 next_entry,
  687. u32 size, u32 mode,
  688. int pos, char **buf, size_t bufsz)
  689. {
  690. /*
  691. * display the newest DEFAULT_LOG_ENTRIES entries
  692. * i.e the entries just before the next ont that uCode would fill.
  693. */
  694. if (num_wraps) {
  695. if (next_entry < size) {
  696. pos = iwl_print_event_log(trans,
  697. capacity - (size - next_entry),
  698. size - next_entry, mode,
  699. pos, buf, bufsz);
  700. pos = iwl_print_event_log(trans, 0,
  701. next_entry, mode,
  702. pos, buf, bufsz);
  703. } else
  704. pos = iwl_print_event_log(trans, next_entry - size,
  705. size, mode, pos, buf, bufsz);
  706. } else {
  707. if (next_entry < size) {
  708. pos = iwl_print_event_log(trans, 0, next_entry,
  709. mode, pos, buf, bufsz);
  710. } else {
  711. pos = iwl_print_event_log(trans, next_entry - size,
  712. size, mode, pos, buf, bufsz);
  713. }
  714. }
  715. return pos;
  716. }
  717. #define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
  718. int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
  719. char **buf, bool display)
  720. {
  721. u32 base; /* SRAM byte address of event log header */
  722. u32 capacity; /* event log capacity in # entries */
  723. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  724. u32 num_wraps; /* # times uCode wrapped to top of log */
  725. u32 next_entry; /* index of next entry to be written by uCode */
  726. u32 size; /* # entries that we'll print */
  727. u32 logsize;
  728. int pos = 0;
  729. size_t bufsz = 0;
  730. struct iwl_priv *priv = priv(trans);
  731. base = priv->device_pointers.log_event_table;
  732. if (priv->ucode_type == IWL_UCODE_INIT) {
  733. logsize = priv->init_evtlog_size;
  734. if (!base)
  735. base = priv->init_evtlog_ptr;
  736. } else {
  737. logsize = priv->inst_evtlog_size;
  738. if (!base)
  739. base = priv->inst_evtlog_ptr;
  740. }
  741. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  742. IWL_ERR(trans,
  743. "Invalid event log pointer 0x%08X for %s uCode\n",
  744. base,
  745. (priv->ucode_type == IWL_UCODE_INIT)
  746. ? "Init" : "RT");
  747. return -EINVAL;
  748. }
  749. /* event log header */
  750. capacity = iwl_read_targ_mem(bus(trans), base);
  751. mode = iwl_read_targ_mem(bus(trans), base + (1 * sizeof(u32)));
  752. num_wraps = iwl_read_targ_mem(bus(trans), base + (2 * sizeof(u32)));
  753. next_entry = iwl_read_targ_mem(bus(trans), base + (3 * sizeof(u32)));
  754. if (capacity > logsize) {
  755. IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
  756. "entries\n", capacity, logsize);
  757. capacity = logsize;
  758. }
  759. if (next_entry > logsize) {
  760. IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
  761. next_entry, logsize);
  762. next_entry = logsize;
  763. }
  764. size = num_wraps ? capacity : next_entry;
  765. /* bail out if nothing in log */
  766. if (size == 0) {
  767. IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
  768. return pos;
  769. }
  770. #ifdef CONFIG_IWLWIFI_DEBUG
  771. if (!(iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) && !full_log)
  772. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  773. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  774. #else
  775. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  776. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  777. #endif
  778. IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
  779. size);
  780. #ifdef CONFIG_IWLWIFI_DEBUG
  781. if (display) {
  782. if (full_log)
  783. bufsz = capacity * 48;
  784. else
  785. bufsz = size * 48;
  786. *buf = kmalloc(bufsz, GFP_KERNEL);
  787. if (!*buf)
  788. return -ENOMEM;
  789. }
  790. if ((iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) || full_log) {
  791. /*
  792. * if uCode has wrapped back to top of log,
  793. * start at the oldest entry,
  794. * i.e the next one that uCode would fill.
  795. */
  796. if (num_wraps)
  797. pos = iwl_print_event_log(trans, next_entry,
  798. capacity - next_entry, mode,
  799. pos, buf, bufsz);
  800. /* (then/else) start at top of log */
  801. pos = iwl_print_event_log(trans, 0,
  802. next_entry, mode, pos, buf, bufsz);
  803. } else
  804. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  805. next_entry, size, mode,
  806. pos, buf, bufsz);
  807. #else
  808. pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
  809. next_entry, size, mode,
  810. pos, buf, bufsz);
  811. #endif
  812. return pos;
  813. }
  814. /* tasklet for iwlagn interrupt */
  815. void iwl_irq_tasklet(struct iwl_trans *trans)
  816. {
  817. u32 inta = 0;
  818. u32 handled = 0;
  819. unsigned long flags;
  820. u32 i;
  821. #ifdef CONFIG_IWLWIFI_DEBUG
  822. u32 inta_mask;
  823. #endif
  824. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  825. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  826. spin_lock_irqsave(&trans->shrd->lock, flags);
  827. /* Ack/clear/reset pending uCode interrupts.
  828. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  829. */
  830. /* There is a hardware bug in the interrupt mask function that some
  831. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  832. * they are disabled in the CSR_INT_MASK register. Furthermore the
  833. * ICT interrupt handling mechanism has another bug that might cause
  834. * these unmasked interrupts fail to be detected. We workaround the
  835. * hardware bugs here by ACKing all the possible interrupts so that
  836. * interrupt coalescing can still be achieved.
  837. */
  838. iwl_write32(bus(trans), CSR_INT,
  839. trans_pcie->inta | ~trans_pcie->inta_mask);
  840. inta = trans_pcie->inta;
  841. #ifdef CONFIG_IWLWIFI_DEBUG
  842. if (iwl_get_debug_level(trans->shrd) & IWL_DL_ISR) {
  843. /* just for debug */
  844. inta_mask = iwl_read32(bus(trans), CSR_INT_MASK);
  845. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
  846. inta, inta_mask);
  847. }
  848. #endif
  849. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  850. /* saved interrupt in inta variable now we can reset trans_pcie->inta */
  851. trans_pcie->inta = 0;
  852. /* Now service all interrupt bits discovered above. */
  853. if (inta & CSR_INT_BIT_HW_ERR) {
  854. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  855. /* Tell the device to stop sending interrupts */
  856. iwl_disable_interrupts(trans);
  857. isr_stats->hw++;
  858. iwl_irq_handle_error(trans);
  859. handled |= CSR_INT_BIT_HW_ERR;
  860. return;
  861. }
  862. #ifdef CONFIG_IWLWIFI_DEBUG
  863. if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
  864. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  865. if (inta & CSR_INT_BIT_SCD) {
  866. IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
  867. "the frame/frames.\n");
  868. isr_stats->sch++;
  869. }
  870. /* Alive notification via Rx interrupt will do the real work */
  871. if (inta & CSR_INT_BIT_ALIVE) {
  872. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  873. isr_stats->alive++;
  874. }
  875. }
  876. #endif
  877. /* Safely ignore these bits for debug checks below */
  878. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  879. /* HW RF KILL switch toggled */
  880. if (inta & CSR_INT_BIT_RF_KILL) {
  881. int hw_rf_kill = 0;
  882. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  883. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  884. hw_rf_kill = 1;
  885. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  886. hw_rf_kill ? "disable radio" : "enable radio");
  887. isr_stats->rfkill++;
  888. /* driver only loads ucode once setting the interface up.
  889. * the driver allows loading the ucode even if the radio
  890. * is killed. Hence update the killswitch state here. The
  891. * rfkill handler will care about restarting if needed.
  892. */
  893. if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
  894. if (hw_rf_kill)
  895. set_bit(STATUS_RF_KILL_HW,
  896. &trans->shrd->status);
  897. else
  898. clear_bit(STATUS_RF_KILL_HW,
  899. &trans->shrd->status);
  900. iwl_set_hw_rfkill_state(priv(trans), hw_rf_kill);
  901. }
  902. handled |= CSR_INT_BIT_RF_KILL;
  903. }
  904. /* Chip got too hot and stopped itself */
  905. if (inta & CSR_INT_BIT_CT_KILL) {
  906. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  907. isr_stats->ctkill++;
  908. handled |= CSR_INT_BIT_CT_KILL;
  909. }
  910. /* Error detected by uCode */
  911. if (inta & CSR_INT_BIT_SW_ERR) {
  912. IWL_ERR(trans, "Microcode SW error detected. "
  913. " Restarting 0x%X.\n", inta);
  914. isr_stats->sw++;
  915. iwl_irq_handle_error(trans);
  916. handled |= CSR_INT_BIT_SW_ERR;
  917. }
  918. /* uCode wakes up after power-down sleep */
  919. if (inta & CSR_INT_BIT_WAKEUP) {
  920. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  921. iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
  922. for (i = 0; i < hw_params(trans).max_txq_num; i++)
  923. iwl_txq_update_write_ptr(trans,
  924. &trans_pcie->txq[i]);
  925. isr_stats->wakeup++;
  926. handled |= CSR_INT_BIT_WAKEUP;
  927. }
  928. /* All uCode command responses, including Tx command responses,
  929. * Rx "responses" (frame-received notification), and other
  930. * notifications from uCode come through here*/
  931. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  932. CSR_INT_BIT_RX_PERIODIC)) {
  933. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  934. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  935. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  936. iwl_write32(bus(trans), CSR_FH_INT_STATUS,
  937. CSR_FH_INT_RX_MASK);
  938. }
  939. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  940. handled |= CSR_INT_BIT_RX_PERIODIC;
  941. iwl_write32(bus(trans),
  942. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  943. }
  944. /* Sending RX interrupt require many steps to be done in the
  945. * the device:
  946. * 1- write interrupt to current index in ICT table.
  947. * 2- dma RX frame.
  948. * 3- update RX shared data to indicate last write index.
  949. * 4- send interrupt.
  950. * This could lead to RX race, driver could receive RX interrupt
  951. * but the shared data changes does not reflect this;
  952. * periodic interrupt will detect any dangling Rx activity.
  953. */
  954. /* Disable periodic interrupt; we use it as just a one-shot. */
  955. iwl_write8(bus(trans), CSR_INT_PERIODIC_REG,
  956. CSR_INT_PERIODIC_DIS);
  957. iwl_rx_handle(trans);
  958. /*
  959. * Enable periodic interrupt in 8 msec only if we received
  960. * real RX interrupt (instead of just periodic int), to catch
  961. * any dangling Rx interrupt. If it was just the periodic
  962. * interrupt, there was no dangling Rx activity, and no need
  963. * to extend the periodic interrupt; one-shot is enough.
  964. */
  965. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  966. iwl_write8(bus(trans), CSR_INT_PERIODIC_REG,
  967. CSR_INT_PERIODIC_ENA);
  968. isr_stats->rx++;
  969. }
  970. /* This "Tx" DMA channel is used only for loading uCode */
  971. if (inta & CSR_INT_BIT_FH_TX) {
  972. iwl_write32(bus(trans), CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  973. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  974. isr_stats->tx++;
  975. handled |= CSR_INT_BIT_FH_TX;
  976. /* Wake up uCode load routine, now that load is complete */
  977. priv(trans)->ucode_write_complete = 1;
  978. wake_up(&trans->shrd->wait_command_queue);
  979. }
  980. if (inta & ~handled) {
  981. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  982. isr_stats->unhandled++;
  983. }
  984. if (inta & ~(trans_pcie->inta_mask)) {
  985. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  986. inta & ~trans_pcie->inta_mask);
  987. }
  988. /* Re-enable all interrupts */
  989. /* only Re-enable if disabled by irq */
  990. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
  991. iwl_enable_interrupts(trans);
  992. /* Re-enable RF_KILL if it occurred */
  993. else if (handled & CSR_INT_BIT_RF_KILL)
  994. iwl_enable_rfkill_int(priv(trans));
  995. }
  996. /******************************************************************************
  997. *
  998. * ICT functions
  999. *
  1000. ******************************************************************************/
  1001. #define ICT_COUNT (PAGE_SIZE/sizeof(u32))
  1002. /* Free dram table */
  1003. void iwl_free_isr_ict(struct iwl_trans *trans)
  1004. {
  1005. struct iwl_trans_pcie *trans_pcie =
  1006. IWL_TRANS_GET_PCIE_TRANS(trans);
  1007. if (trans_pcie->ict_tbl_vir) {
  1008. dma_free_coherent(bus(trans)->dev,
  1009. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  1010. trans_pcie->ict_tbl_vir,
  1011. trans_pcie->ict_tbl_dma);
  1012. trans_pcie->ict_tbl_vir = NULL;
  1013. memset(&trans_pcie->ict_tbl_dma, 0,
  1014. sizeof(trans_pcie->ict_tbl_dma));
  1015. memset(&trans_pcie->aligned_ict_tbl_dma, 0,
  1016. sizeof(trans_pcie->aligned_ict_tbl_dma));
  1017. }
  1018. }
  1019. /* allocate dram shared table it is a PAGE_SIZE aligned
  1020. * also reset all data related to ICT table interrupt.
  1021. */
  1022. int iwl_alloc_isr_ict(struct iwl_trans *trans)
  1023. {
  1024. struct iwl_trans_pcie *trans_pcie =
  1025. IWL_TRANS_GET_PCIE_TRANS(trans);
  1026. /* allocate shrared data table */
  1027. trans_pcie->ict_tbl_vir =
  1028. dma_alloc_coherent(bus(trans)->dev,
  1029. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
  1030. &trans_pcie->ict_tbl_dma, GFP_KERNEL);
  1031. if (!trans_pcie->ict_tbl_vir)
  1032. return -ENOMEM;
  1033. /* align table to PAGE_SIZE boundary */
  1034. trans_pcie->aligned_ict_tbl_dma =
  1035. ALIGN(trans_pcie->ict_tbl_dma, PAGE_SIZE);
  1036. IWL_DEBUG_ISR(trans, "ict dma addr %Lx dma aligned %Lx diff %d\n",
  1037. (unsigned long long)trans_pcie->ict_tbl_dma,
  1038. (unsigned long long)trans_pcie->aligned_ict_tbl_dma,
  1039. (int)(trans_pcie->aligned_ict_tbl_dma -
  1040. trans_pcie->ict_tbl_dma));
  1041. trans_pcie->ict_tbl = trans_pcie->ict_tbl_vir +
  1042. (trans_pcie->aligned_ict_tbl_dma -
  1043. trans_pcie->ict_tbl_dma);
  1044. IWL_DEBUG_ISR(trans, "ict vir addr %p vir aligned %p diff %d\n",
  1045. trans_pcie->ict_tbl, trans_pcie->ict_tbl_vir,
  1046. (int)(trans_pcie->aligned_ict_tbl_dma -
  1047. trans_pcie->ict_tbl_dma));
  1048. /* reset table and index to all 0 */
  1049. memset(trans_pcie->ict_tbl_vir, 0,
  1050. (sizeof(u32) * ICT_COUNT) + PAGE_SIZE);
  1051. trans_pcie->ict_index = 0;
  1052. /* add periodic RX interrupt */
  1053. trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
  1054. return 0;
  1055. }
  1056. /* Device is going up inform it about using ICT interrupt table,
  1057. * also we need to tell the driver to start using ICT interrupt.
  1058. */
  1059. int iwl_reset_ict(struct iwl_trans *trans)
  1060. {
  1061. u32 val;
  1062. unsigned long flags;
  1063. struct iwl_trans_pcie *trans_pcie =
  1064. IWL_TRANS_GET_PCIE_TRANS(trans);
  1065. if (!trans_pcie->ict_tbl_vir)
  1066. return 0;
  1067. spin_lock_irqsave(&trans->shrd->lock, flags);
  1068. iwl_disable_interrupts(trans);
  1069. memset(&trans_pcie->ict_tbl[0], 0, sizeof(u32) * ICT_COUNT);
  1070. val = trans_pcie->aligned_ict_tbl_dma >> PAGE_SHIFT;
  1071. val |= CSR_DRAM_INT_TBL_ENABLE;
  1072. val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
  1073. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%X "
  1074. "aligned dma address %Lx\n",
  1075. val,
  1076. (unsigned long long)trans_pcie->aligned_ict_tbl_dma);
  1077. iwl_write32(bus(trans), CSR_DRAM_INT_TBL_REG, val);
  1078. trans_pcie->use_ict = true;
  1079. trans_pcie->ict_index = 0;
  1080. iwl_write32(bus(trans), CSR_INT, trans_pcie->inta_mask);
  1081. iwl_enable_interrupts(trans);
  1082. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1083. return 0;
  1084. }
  1085. /* Device is going down disable ict interrupt usage */
  1086. void iwl_disable_ict(struct iwl_trans *trans)
  1087. {
  1088. struct iwl_trans_pcie *trans_pcie =
  1089. IWL_TRANS_GET_PCIE_TRANS(trans);
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&trans->shrd->lock, flags);
  1092. trans_pcie->use_ict = false;
  1093. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1094. }
  1095. static irqreturn_t iwl_isr(int irq, void *data)
  1096. {
  1097. struct iwl_trans *trans = data;
  1098. struct iwl_trans_pcie *trans_pcie;
  1099. u32 inta, inta_mask;
  1100. unsigned long flags;
  1101. #ifdef CONFIG_IWLWIFI_DEBUG
  1102. u32 inta_fh;
  1103. #endif
  1104. if (!trans)
  1105. return IRQ_NONE;
  1106. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1107. spin_lock_irqsave(&trans->shrd->lock, flags);
  1108. /* Disable (but don't clear!) interrupts here to avoid
  1109. * back-to-back ISRs and sporadic interrupts from our NIC.
  1110. * If we have something to service, the tasklet will re-enable ints.
  1111. * If we *don't* have something, we'll re-enable before leaving here. */
  1112. inta_mask = iwl_read32(bus(trans), CSR_INT_MASK); /* just for debug */
  1113. iwl_write32(bus(trans), CSR_INT_MASK, 0x00000000);
  1114. /* Discover which interrupts are active/pending */
  1115. inta = iwl_read32(bus(trans), CSR_INT);
  1116. /* Ignore interrupt if there's nothing in NIC to service.
  1117. * This may be due to IRQ shared with another device,
  1118. * or due to sporadic interrupts thrown from our NIC. */
  1119. if (!inta) {
  1120. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1121. goto none;
  1122. }
  1123. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1124. /* Hardware disappeared. It might have already raised
  1125. * an interrupt */
  1126. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1127. goto unplugged;
  1128. }
  1129. #ifdef CONFIG_IWLWIFI_DEBUG
  1130. if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
  1131. inta_fh = iwl_read32(bus(trans), CSR_FH_INT_STATUS);
  1132. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
  1133. "fh 0x%08x\n", inta, inta_mask, inta_fh);
  1134. }
  1135. #endif
  1136. trans_pcie->inta |= inta;
  1137. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1138. if (likely(inta))
  1139. tasklet_schedule(&trans_pcie->irq_tasklet);
  1140. else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  1141. !trans_pcie->inta)
  1142. iwl_enable_interrupts(trans);
  1143. unplugged:
  1144. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1145. return IRQ_HANDLED;
  1146. none:
  1147. /* re-enable interrupts here since we don't have anything to service. */
  1148. /* only Re-enable if disabled by irq and no schedules tasklet. */
  1149. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  1150. !trans_pcie->inta)
  1151. iwl_enable_interrupts(trans);
  1152. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1153. return IRQ_NONE;
  1154. }
  1155. /* interrupt handler using ict table, with this interrupt driver will
  1156. * stop using INTA register to get device's interrupt, reading this register
  1157. * is expensive, device will write interrupts in ICT dram table, increment
  1158. * index then will fire interrupt to driver, driver will OR all ICT table
  1159. * entries from current index up to table entry with 0 value. the result is
  1160. * the interrupt we need to service, driver will set the entries back to 0 and
  1161. * set index.
  1162. */
  1163. irqreturn_t iwl_isr_ict(int irq, void *data)
  1164. {
  1165. struct iwl_trans *trans = data;
  1166. struct iwl_trans_pcie *trans_pcie;
  1167. u32 inta, inta_mask;
  1168. u32 val = 0;
  1169. unsigned long flags;
  1170. if (!trans)
  1171. return IRQ_NONE;
  1172. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1173. /* dram interrupt table not set yet,
  1174. * use legacy interrupt.
  1175. */
  1176. if (!trans_pcie->use_ict)
  1177. return iwl_isr(irq, data);
  1178. spin_lock_irqsave(&trans->shrd->lock, flags);
  1179. /* Disable (but don't clear!) interrupts here to avoid
  1180. * back-to-back ISRs and sporadic interrupts from our NIC.
  1181. * If we have something to service, the tasklet will re-enable ints.
  1182. * If we *don't* have something, we'll re-enable before leaving here.
  1183. */
  1184. inta_mask = iwl_read32(bus(trans), CSR_INT_MASK); /* just for debug */
  1185. iwl_write32(bus(trans), CSR_INT_MASK, 0x00000000);
  1186. /* Ignore interrupt if there's nothing in NIC to service.
  1187. * This may be due to IRQ shared with another device,
  1188. * or due to sporadic interrupts thrown from our NIC. */
  1189. if (!trans_pcie->ict_tbl[trans_pcie->ict_index]) {
  1190. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1191. goto none;
  1192. }
  1193. /* read all entries that not 0 start with ict_index */
  1194. while (trans_pcie->ict_tbl[trans_pcie->ict_index]) {
  1195. val |= le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1196. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1197. trans_pcie->ict_index,
  1198. le32_to_cpu(
  1199. trans_pcie->ict_tbl[trans_pcie->ict_index]));
  1200. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1201. trans_pcie->ict_index =
  1202. iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
  1203. }
  1204. /* We should not get this value, just ignore it. */
  1205. if (val == 0xffffffff)
  1206. val = 0;
  1207. /*
  1208. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1209. * (bit 15 before shifting it to 31) to clear when using interrupt
  1210. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1211. * so we use them to decide on the real state of the Rx bit.
  1212. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1213. */
  1214. if (val & 0xC0000)
  1215. val |= 0x8000;
  1216. inta = (0xff & val) | ((0xff00 & val) << 16);
  1217. IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
  1218. inta, inta_mask, val);
  1219. inta &= trans_pcie->inta_mask;
  1220. trans_pcie->inta |= inta;
  1221. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1222. if (likely(inta))
  1223. tasklet_schedule(&trans_pcie->irq_tasklet);
  1224. else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  1225. !trans_pcie->inta) {
  1226. /* Allow interrupt if was disabled by this handler and
  1227. * no tasklet was schedules, We should not enable interrupt,
  1228. * tasklet will enable it.
  1229. */
  1230. iwl_enable_interrupts(trans);
  1231. }
  1232. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1233. return IRQ_HANDLED;
  1234. none:
  1235. /* re-enable interrupts here since we don't have anything to service.
  1236. * only Re-enable if disabled by irq.
  1237. */
  1238. if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
  1239. !trans_pcie->inta)
  1240. iwl_enable_interrupts(trans);
  1241. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1242. return IRQ_NONE;
  1243. }