rx.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  5. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  6. * Copyright(c) 2018 Intel Corporation
  7. *
  8. * Portions of this file are derived from the ipw3945 project, as well
  9. * as portions of the ieee80211 subsystem header files.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program.
  22. *
  23. * The full GNU General Public License is included in this distribution in the
  24. * file called LICENSE.
  25. *
  26. * Contact Information:
  27. * Intel Linux Wireless <linuxwifi@intel.com>
  28. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  29. *
  30. *****************************************************************************/
  31. #include <linux/sched.h>
  32. #include <linux/wait.h>
  33. #include <linux/gfp.h>
  34. #include "iwl-prph.h"
  35. #include "iwl-io.h"
  36. #include "internal.h"
  37. #include "iwl-op-mode.h"
  38. #include "iwl-context-info-gen3.h"
  39. /******************************************************************************
  40. *
  41. * RX path functions
  42. *
  43. ******************************************************************************/
  44. /*
  45. * Rx theory of operation
  46. *
  47. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  48. * each of which point to Receive Buffers to be filled by the NIC. These get
  49. * used not only for Rx frames, but for any command response or notification
  50. * from the NIC. The driver and NIC manage the Rx buffers by means
  51. * of indexes into the circular buffer.
  52. *
  53. * Rx Queue Indexes
  54. * The host/firmware share two index registers for managing the Rx buffers.
  55. *
  56. * The READ index maps to the first position that the firmware may be writing
  57. * to -- the driver can read up to (but not including) this position and get
  58. * good data.
  59. * The READ index is managed by the firmware once the card is enabled.
  60. *
  61. * The WRITE index maps to the last position the driver has read from -- the
  62. * position preceding WRITE is the last slot the firmware can place a packet.
  63. *
  64. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  65. * WRITE = READ.
  66. *
  67. * During initialization, the host sets up the READ queue position to the first
  68. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  69. *
  70. * When the firmware places a packet in a buffer, it will advance the READ index
  71. * and fire the RX interrupt. The driver can then query the READ index and
  72. * process as many packets as possible, moving the WRITE index forward as it
  73. * resets the Rx queue buffers with new memory.
  74. *
  75. * The management in the driver is as follows:
  76. * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
  77. * When the interrupt handler is called, the request is processed.
  78. * The page is either stolen - transferred to the upper layer
  79. * or reused - added immediately to the iwl->rxq->rx_free list.
  80. * + When the page is stolen - the driver updates the matching queue's used
  81. * count, detaches the RBD and transfers it to the queue used list.
  82. * When there are two used RBDs - they are transferred to the allocator empty
  83. * list. Work is then scheduled for the allocator to start allocating
  84. * eight buffers.
  85. * When there are another 6 used RBDs - they are transferred to the allocator
  86. * empty list and the driver tries to claim the pre-allocated buffers and
  87. * add them to iwl->rxq->rx_free. If it fails - it continues to claim them
  88. * until ready.
  89. * When there are 8+ buffers in the free list - either from allocation or from
  90. * 8 reused unstolen pages - restock is called to update the FW and indexes.
  91. * + In order to make sure the allocator always has RBDs to use for allocation
  92. * the allocator has initial pool in the size of num_queues*(8-2) - the
  93. * maximum missing RBDs per allocation request (request posted with 2
  94. * empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
  95. * The queues supplies the recycle of the rest of the RBDs.
  96. * + A received packet is processed and handed to the kernel network stack,
  97. * detached from the iwl->rxq. The driver 'processed' index is updated.
  98. * + If there are no allocated buffers in iwl->rxq->rx_free,
  99. * the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
  100. * If there were enough free buffers and RX_STALLED is set it is cleared.
  101. *
  102. *
  103. * Driver sequence:
  104. *
  105. * iwl_rxq_alloc() Allocates rx_free
  106. * iwl_pcie_rx_replenish() Replenishes rx_free list from rx_used, and calls
  107. * iwl_pcie_rxq_restock.
  108. * Used only during initialization.
  109. * iwl_pcie_rxq_restock() Moves available buffers from rx_free into Rx
  110. * queue, updates firmware pointers, and updates
  111. * the WRITE index.
  112. * iwl_pcie_rx_allocator() Background work for allocating pages.
  113. *
  114. * -- enable interrupts --
  115. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  116. * READ INDEX, detaching the SKB from the pool.
  117. * Moves the packet buffer from queue to rx_used.
  118. * Posts and claims requests to the allocator.
  119. * Calls iwl_pcie_rxq_restock to refill any empty
  120. * slots.
  121. *
  122. * RBD life-cycle:
  123. *
  124. * Init:
  125. * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
  126. *
  127. * Regular Receive interrupt:
  128. * Page Stolen:
  129. * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
  130. * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
  131. * Page not Stolen:
  132. * rxq.queue -> rxq.rx_free -> rxq.queue
  133. * ...
  134. *
  135. */
  136. /*
  137. * iwl_rxq_space - Return number of free slots available in queue.
  138. */
  139. static int iwl_rxq_space(const struct iwl_rxq *rxq)
  140. {
  141. /* Make sure rx queue size is a power of 2 */
  142. WARN_ON(rxq->queue_size & (rxq->queue_size - 1));
  143. /*
  144. * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
  145. * between empty and completely full queues.
  146. * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
  147. * defined for negative dividends.
  148. */
  149. return (rxq->read - rxq->write - 1) & (rxq->queue_size - 1);
  150. }
  151. /*
  152. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  153. */
  154. static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  155. {
  156. return cpu_to_le32((u32)(dma_addr >> 8));
  157. }
  158. /*
  159. * iwl_pcie_rx_stop - stops the Rx DMA
  160. */
  161. int iwl_pcie_rx_stop(struct iwl_trans *trans)
  162. {
  163. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) {
  164. /* TODO: remove this for 22560 once fw does it */
  165. iwl_write_prph(trans, RFH_RXF_DMA_CFG_GEN3, 0);
  166. return iwl_poll_prph_bit(trans, RFH_GEN_STATUS_GEN3,
  167. RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
  168. } else if (trans->cfg->mq_rx_supported) {
  169. iwl_write_prph(trans, RFH_RXF_DMA_CFG, 0);
  170. return iwl_poll_prph_bit(trans, RFH_GEN_STATUS,
  171. RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
  172. } else {
  173. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  174. return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
  175. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
  176. 1000);
  177. }
  178. }
  179. /*
  180. * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
  181. */
  182. static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans,
  183. struct iwl_rxq *rxq)
  184. {
  185. u32 reg;
  186. lockdep_assert_held(&rxq->lock);
  187. /*
  188. * explicitly wake up the NIC if:
  189. * 1. shadow registers aren't enabled
  190. * 2. there is a chance that the NIC is asleep
  191. */
  192. if (!trans->cfg->base_params->shadow_reg_enable &&
  193. test_bit(STATUS_TPOWER_PMI, &trans->status)) {
  194. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  195. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  196. IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
  197. reg);
  198. iwl_set_bit(trans, CSR_GP_CNTRL,
  199. BIT(trans->cfg->csr->flag_mac_access_req));
  200. rxq->need_update = true;
  201. return;
  202. }
  203. }
  204. rxq->write_actual = round_down(rxq->write, 8);
  205. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  206. iwl_write32(trans, HBUS_TARG_WRPTR,
  207. (rxq->write_actual |
  208. ((FIRST_RX_QUEUE + rxq->id) << 16)));
  209. else if (trans->cfg->mq_rx_supported)
  210. iwl_write32(trans, RFH_Q_FRBDCB_WIDX_TRG(rxq->id),
  211. rxq->write_actual);
  212. else
  213. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
  214. }
  215. static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
  216. {
  217. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  218. int i;
  219. for (i = 0; i < trans->num_rx_queues; i++) {
  220. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  221. if (!rxq->need_update)
  222. continue;
  223. spin_lock(&rxq->lock);
  224. iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
  225. rxq->need_update = false;
  226. spin_unlock(&rxq->lock);
  227. }
  228. }
  229. static void iwl_pcie_restock_bd(struct iwl_trans *trans,
  230. struct iwl_rxq *rxq,
  231. struct iwl_rx_mem_buffer *rxb)
  232. {
  233. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) {
  234. struct iwl_rx_transfer_desc *bd = rxq->bd;
  235. bd[rxq->write].type_n_size =
  236. cpu_to_le32((IWL_RX_TD_TYPE & IWL_RX_TD_TYPE_MSK) |
  237. ((IWL_RX_TD_SIZE_2K >> 8) & IWL_RX_TD_SIZE_MSK));
  238. bd[rxq->write].addr = cpu_to_le64(rxb->page_dma);
  239. bd[rxq->write].rbid = cpu_to_le16(rxb->vid);
  240. } else {
  241. __le64 *bd = rxq->bd;
  242. bd[rxq->write] = cpu_to_le64(rxb->page_dma | rxb->vid);
  243. }
  244. }
  245. /*
  246. * iwl_pcie_rxmq_restock - restock implementation for multi-queue rx
  247. */
  248. static void iwl_pcie_rxmq_restock(struct iwl_trans *trans,
  249. struct iwl_rxq *rxq)
  250. {
  251. struct iwl_rx_mem_buffer *rxb;
  252. /*
  253. * If the device isn't enabled - no need to try to add buffers...
  254. * This can happen when we stop the device and still have an interrupt
  255. * pending. We stop the APM before we sync the interrupts because we
  256. * have to (see comment there). On the other hand, since the APM is
  257. * stopped, we cannot access the HW (in particular not prph).
  258. * So don't try to restock if the APM has been already stopped.
  259. */
  260. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  261. return;
  262. spin_lock(&rxq->lock);
  263. while (rxq->free_count) {
  264. /* Get next free Rx buffer, remove from free list */
  265. rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
  266. list);
  267. list_del(&rxb->list);
  268. rxb->invalid = false;
  269. /* 12 first bits are expected to be empty */
  270. WARN_ON(rxb->page_dma & DMA_BIT_MASK(12));
  271. /* Point to Rx buffer via next RBD in circular buffer */
  272. iwl_pcie_restock_bd(trans, rxq, rxb);
  273. rxq->write = (rxq->write + 1) & MQ_RX_TABLE_MASK;
  274. rxq->free_count--;
  275. }
  276. spin_unlock(&rxq->lock);
  277. /*
  278. * If we've added more space for the firmware to place data, tell it.
  279. * Increment device's write pointer in multiples of 8.
  280. */
  281. if (rxq->write_actual != (rxq->write & ~0x7)) {
  282. spin_lock(&rxq->lock);
  283. iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
  284. spin_unlock(&rxq->lock);
  285. }
  286. }
  287. /*
  288. * iwl_pcie_rxsq_restock - restock implementation for single queue rx
  289. */
  290. static void iwl_pcie_rxsq_restock(struct iwl_trans *trans,
  291. struct iwl_rxq *rxq)
  292. {
  293. struct iwl_rx_mem_buffer *rxb;
  294. /*
  295. * If the device isn't enabled - not need to try to add buffers...
  296. * This can happen when we stop the device and still have an interrupt
  297. * pending. We stop the APM before we sync the interrupts because we
  298. * have to (see comment there). On the other hand, since the APM is
  299. * stopped, we cannot access the HW (in particular not prph).
  300. * So don't try to restock if the APM has been already stopped.
  301. */
  302. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  303. return;
  304. spin_lock(&rxq->lock);
  305. while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
  306. __le32 *bd = (__le32 *)rxq->bd;
  307. /* The overwritten rxb must be a used one */
  308. rxb = rxq->queue[rxq->write];
  309. BUG_ON(rxb && rxb->page);
  310. /* Get next free Rx buffer, remove from free list */
  311. rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
  312. list);
  313. list_del(&rxb->list);
  314. rxb->invalid = false;
  315. /* Point to Rx buffer via next RBD in circular buffer */
  316. bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
  317. rxq->queue[rxq->write] = rxb;
  318. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  319. rxq->free_count--;
  320. }
  321. spin_unlock(&rxq->lock);
  322. /* If we've added more space for the firmware to place data, tell it.
  323. * Increment device's write pointer in multiples of 8. */
  324. if (rxq->write_actual != (rxq->write & ~0x7)) {
  325. spin_lock(&rxq->lock);
  326. iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
  327. spin_unlock(&rxq->lock);
  328. }
  329. }
  330. /*
  331. * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
  332. *
  333. * If there are slots in the RX queue that need to be restocked,
  334. * and we have free pre-allocated buffers, fill the ranks as much
  335. * as we can, pulling from rx_free.
  336. *
  337. * This moves the 'write' index forward to catch up with 'processed', and
  338. * also updates the memory address in the firmware to reference the new
  339. * target buffer.
  340. */
  341. static
  342. void iwl_pcie_rxq_restock(struct iwl_trans *trans, struct iwl_rxq *rxq)
  343. {
  344. if (trans->cfg->mq_rx_supported)
  345. iwl_pcie_rxmq_restock(trans, rxq);
  346. else
  347. iwl_pcie_rxsq_restock(trans, rxq);
  348. }
  349. /*
  350. * iwl_pcie_rx_alloc_page - allocates and returns a page.
  351. *
  352. */
  353. static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans,
  354. gfp_t priority)
  355. {
  356. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  357. struct page *page;
  358. gfp_t gfp_mask = priority;
  359. if (trans_pcie->rx_page_order > 0)
  360. gfp_mask |= __GFP_COMP;
  361. /* Alloc a new receive buffer */
  362. page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
  363. if (!page) {
  364. if (net_ratelimit())
  365. IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
  366. trans_pcie->rx_page_order);
  367. /*
  368. * Issue an error if we don't have enough pre-allocated
  369. * buffers.
  370. ` */
  371. if (!(gfp_mask & __GFP_NOWARN) && net_ratelimit())
  372. IWL_CRIT(trans,
  373. "Failed to alloc_pages\n");
  374. return NULL;
  375. }
  376. return page;
  377. }
  378. /*
  379. * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
  380. *
  381. * A used RBD is an Rx buffer that has been given to the stack. To use it again
  382. * a page must be allocated and the RBD must point to the page. This function
  383. * doesn't change the HW pointer but handles the list of pages that is used by
  384. * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
  385. * allocated buffers.
  386. */
  387. void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
  388. struct iwl_rxq *rxq)
  389. {
  390. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  391. struct iwl_rx_mem_buffer *rxb;
  392. struct page *page;
  393. while (1) {
  394. spin_lock(&rxq->lock);
  395. if (list_empty(&rxq->rx_used)) {
  396. spin_unlock(&rxq->lock);
  397. return;
  398. }
  399. spin_unlock(&rxq->lock);
  400. /* Alloc a new receive buffer */
  401. page = iwl_pcie_rx_alloc_page(trans, priority);
  402. if (!page)
  403. return;
  404. spin_lock(&rxq->lock);
  405. if (list_empty(&rxq->rx_used)) {
  406. spin_unlock(&rxq->lock);
  407. __free_pages(page, trans_pcie->rx_page_order);
  408. return;
  409. }
  410. rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
  411. list);
  412. list_del(&rxb->list);
  413. spin_unlock(&rxq->lock);
  414. BUG_ON(rxb->page);
  415. rxb->page = page;
  416. /* Get physical address of the RB */
  417. rxb->page_dma =
  418. dma_map_page(trans->dev, page, 0,
  419. PAGE_SIZE << trans_pcie->rx_page_order,
  420. DMA_FROM_DEVICE);
  421. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  422. rxb->page = NULL;
  423. spin_lock(&rxq->lock);
  424. list_add(&rxb->list, &rxq->rx_used);
  425. spin_unlock(&rxq->lock);
  426. __free_pages(page, trans_pcie->rx_page_order);
  427. return;
  428. }
  429. spin_lock(&rxq->lock);
  430. list_add_tail(&rxb->list, &rxq->rx_free);
  431. rxq->free_count++;
  432. spin_unlock(&rxq->lock);
  433. }
  434. }
  435. void iwl_pcie_free_rbs_pool(struct iwl_trans *trans)
  436. {
  437. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  438. int i;
  439. for (i = 0; i < RX_POOL_SIZE; i++) {
  440. if (!trans_pcie->rx_pool[i].page)
  441. continue;
  442. dma_unmap_page(trans->dev, trans_pcie->rx_pool[i].page_dma,
  443. PAGE_SIZE << trans_pcie->rx_page_order,
  444. DMA_FROM_DEVICE);
  445. __free_pages(trans_pcie->rx_pool[i].page,
  446. trans_pcie->rx_page_order);
  447. trans_pcie->rx_pool[i].page = NULL;
  448. }
  449. }
  450. /*
  451. * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
  452. *
  453. * Allocates for each received request 8 pages
  454. * Called as a scheduled work item.
  455. */
  456. static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
  457. {
  458. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  459. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  460. struct list_head local_empty;
  461. int pending = atomic_read(&rba->req_pending);
  462. IWL_DEBUG_RX(trans, "Pending allocation requests = %d\n", pending);
  463. /* If we were scheduled - there is at least one request */
  464. spin_lock(&rba->lock);
  465. /* swap out the rba->rbd_empty to a local list */
  466. list_replace_init(&rba->rbd_empty, &local_empty);
  467. spin_unlock(&rba->lock);
  468. while (pending) {
  469. int i;
  470. LIST_HEAD(local_allocated);
  471. gfp_t gfp_mask = GFP_KERNEL;
  472. /* Do not post a warning if there are only a few requests */
  473. if (pending < RX_PENDING_WATERMARK)
  474. gfp_mask |= __GFP_NOWARN;
  475. for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
  476. struct iwl_rx_mem_buffer *rxb;
  477. struct page *page;
  478. /* List should never be empty - each reused RBD is
  479. * returned to the list, and initial pool covers any
  480. * possible gap between the time the page is allocated
  481. * to the time the RBD is added.
  482. */
  483. BUG_ON(list_empty(&local_empty));
  484. /* Get the first rxb from the rbd list */
  485. rxb = list_first_entry(&local_empty,
  486. struct iwl_rx_mem_buffer, list);
  487. BUG_ON(rxb->page);
  488. /* Alloc a new receive buffer */
  489. page = iwl_pcie_rx_alloc_page(trans, gfp_mask);
  490. if (!page)
  491. continue;
  492. rxb->page = page;
  493. /* Get physical address of the RB */
  494. rxb->page_dma = dma_map_page(trans->dev, page, 0,
  495. PAGE_SIZE << trans_pcie->rx_page_order,
  496. DMA_FROM_DEVICE);
  497. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  498. rxb->page = NULL;
  499. __free_pages(page, trans_pcie->rx_page_order);
  500. continue;
  501. }
  502. /* move the allocated entry to the out list */
  503. list_move(&rxb->list, &local_allocated);
  504. i++;
  505. }
  506. atomic_dec(&rba->req_pending);
  507. pending--;
  508. if (!pending) {
  509. pending = atomic_read(&rba->req_pending);
  510. IWL_DEBUG_RX(trans,
  511. "Got more pending allocation requests = %d\n",
  512. pending);
  513. }
  514. spin_lock(&rba->lock);
  515. /* add the allocated rbds to the allocator allocated list */
  516. list_splice_tail(&local_allocated, &rba->rbd_allocated);
  517. /* get more empty RBDs for current pending requests */
  518. list_splice_tail_init(&rba->rbd_empty, &local_empty);
  519. spin_unlock(&rba->lock);
  520. atomic_inc(&rba->req_ready);
  521. }
  522. spin_lock(&rba->lock);
  523. /* return unused rbds to the allocator empty list */
  524. list_splice_tail(&local_empty, &rba->rbd_empty);
  525. spin_unlock(&rba->lock);
  526. IWL_DEBUG_RX(trans, "%s, exit.\n", __func__);
  527. }
  528. /*
  529. * iwl_pcie_rx_allocator_get - returns the pre-allocated pages
  530. .*
  531. .* Called by queue when the queue posted allocation request and
  532. * has freed 8 RBDs in order to restock itself.
  533. * This function directly moves the allocated RBs to the queue's ownership
  534. * and updates the relevant counters.
  535. */
  536. static void iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
  537. struct iwl_rxq *rxq)
  538. {
  539. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  540. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  541. int i;
  542. lockdep_assert_held(&rxq->lock);
  543. /*
  544. * atomic_dec_if_positive returns req_ready - 1 for any scenario.
  545. * If req_ready is 0 atomic_dec_if_positive will return -1 and this
  546. * function will return early, as there are no ready requests.
  547. * atomic_dec_if_positive will perofrm the *actual* decrement only if
  548. * req_ready > 0, i.e. - there are ready requests and the function
  549. * hands one request to the caller.
  550. */
  551. if (atomic_dec_if_positive(&rba->req_ready) < 0)
  552. return;
  553. spin_lock(&rba->lock);
  554. for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
  555. /* Get next free Rx buffer, remove it from free list */
  556. struct iwl_rx_mem_buffer *rxb =
  557. list_first_entry(&rba->rbd_allocated,
  558. struct iwl_rx_mem_buffer, list);
  559. list_move(&rxb->list, &rxq->rx_free);
  560. }
  561. spin_unlock(&rba->lock);
  562. rxq->used_count -= RX_CLAIM_REQ_ALLOC;
  563. rxq->free_count += RX_CLAIM_REQ_ALLOC;
  564. }
  565. void iwl_pcie_rx_allocator_work(struct work_struct *data)
  566. {
  567. struct iwl_rb_allocator *rba_p =
  568. container_of(data, struct iwl_rb_allocator, rx_alloc);
  569. struct iwl_trans_pcie *trans_pcie =
  570. container_of(rba_p, struct iwl_trans_pcie, rba);
  571. iwl_pcie_rx_allocator(trans_pcie->trans);
  572. }
  573. static int iwl_pcie_free_bd_size(struct iwl_trans *trans, bool use_rx_td)
  574. {
  575. struct iwl_rx_transfer_desc *rx_td;
  576. if (use_rx_td)
  577. return sizeof(*rx_td);
  578. else
  579. return trans->cfg->mq_rx_supported ? sizeof(__le64) :
  580. sizeof(__le32);
  581. }
  582. static void iwl_pcie_free_rxq_dma(struct iwl_trans *trans,
  583. struct iwl_rxq *rxq)
  584. {
  585. struct device *dev = trans->dev;
  586. bool use_rx_td = (trans->cfg->device_family >=
  587. IWL_DEVICE_FAMILY_22560);
  588. int free_size = iwl_pcie_free_bd_size(trans, use_rx_td);
  589. if (rxq->bd)
  590. dma_free_coherent(trans->dev,
  591. free_size * rxq->queue_size,
  592. rxq->bd, rxq->bd_dma);
  593. rxq->bd_dma = 0;
  594. rxq->bd = NULL;
  595. if (rxq->rb_stts)
  596. dma_free_coherent(trans->dev,
  597. use_rx_td ? sizeof(__le16) :
  598. sizeof(struct iwl_rb_status),
  599. rxq->rb_stts, rxq->rb_stts_dma);
  600. rxq->rb_stts_dma = 0;
  601. rxq->rb_stts = NULL;
  602. if (rxq->used_bd)
  603. dma_free_coherent(trans->dev,
  604. (use_rx_td ? sizeof(*rxq->cd) :
  605. sizeof(__le32)) * rxq->queue_size,
  606. rxq->used_bd, rxq->used_bd_dma);
  607. rxq->used_bd_dma = 0;
  608. rxq->used_bd = NULL;
  609. if (trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
  610. return;
  611. if (rxq->tr_tail)
  612. dma_free_coherent(dev, sizeof(__le16),
  613. rxq->tr_tail, rxq->tr_tail_dma);
  614. rxq->tr_tail_dma = 0;
  615. rxq->tr_tail = NULL;
  616. if (rxq->cr_tail)
  617. dma_free_coherent(dev, sizeof(__le16),
  618. rxq->cr_tail, rxq->cr_tail_dma);
  619. rxq->cr_tail_dma = 0;
  620. rxq->cr_tail = NULL;
  621. }
  622. static int iwl_pcie_alloc_rxq_dma(struct iwl_trans *trans,
  623. struct iwl_rxq *rxq)
  624. {
  625. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  626. struct device *dev = trans->dev;
  627. int i;
  628. int free_size;
  629. bool use_rx_td = (trans->cfg->device_family >=
  630. IWL_DEVICE_FAMILY_22560);
  631. spin_lock_init(&rxq->lock);
  632. if (trans->cfg->mq_rx_supported)
  633. rxq->queue_size = MQ_RX_TABLE_SIZE;
  634. else
  635. rxq->queue_size = RX_QUEUE_SIZE;
  636. free_size = iwl_pcie_free_bd_size(trans, use_rx_td);
  637. /*
  638. * Allocate the circular buffer of Read Buffer Descriptors
  639. * (RBDs)
  640. */
  641. rxq->bd = dma_zalloc_coherent(dev,
  642. free_size * rxq->queue_size,
  643. &rxq->bd_dma, GFP_KERNEL);
  644. if (!rxq->bd)
  645. goto err;
  646. if (trans->cfg->mq_rx_supported) {
  647. rxq->used_bd = dma_zalloc_coherent(dev,
  648. (use_rx_td ?
  649. sizeof(*rxq->cd) :
  650. sizeof(__le32)) *
  651. rxq->queue_size,
  652. &rxq->used_bd_dma,
  653. GFP_KERNEL);
  654. if (!rxq->used_bd)
  655. goto err;
  656. }
  657. /* Allocate the driver's pointer to receive buffer status */
  658. rxq->rb_stts = dma_zalloc_coherent(dev, use_rx_td ?
  659. sizeof(__le16) :
  660. sizeof(struct iwl_rb_status),
  661. &rxq->rb_stts_dma,
  662. GFP_KERNEL);
  663. if (!rxq->rb_stts)
  664. goto err;
  665. if (!use_rx_td)
  666. return 0;
  667. /* Allocate the driver's pointer to TR tail */
  668. rxq->tr_tail = dma_zalloc_coherent(dev, sizeof(__le16),
  669. &rxq->tr_tail_dma,
  670. GFP_KERNEL);
  671. if (!rxq->tr_tail)
  672. goto err;
  673. /* Allocate the driver's pointer to CR tail */
  674. rxq->cr_tail = dma_zalloc_coherent(dev, sizeof(__le16),
  675. &rxq->cr_tail_dma,
  676. GFP_KERNEL);
  677. if (!rxq->cr_tail)
  678. goto err;
  679. /*
  680. * W/A 22560 device step Z0 must be non zero bug
  681. * TODO: remove this when stop supporting Z0
  682. */
  683. *rxq->cr_tail = cpu_to_le16(500);
  684. return 0;
  685. err:
  686. for (i = 0; i < trans->num_rx_queues; i++) {
  687. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  688. iwl_pcie_free_rxq_dma(trans, rxq);
  689. }
  690. kfree(trans_pcie->rxq);
  691. return -ENOMEM;
  692. }
  693. static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
  694. {
  695. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  696. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  697. int i, ret;
  698. if (WARN_ON(trans_pcie->rxq))
  699. return -EINVAL;
  700. trans_pcie->rxq = kcalloc(trans->num_rx_queues, sizeof(struct iwl_rxq),
  701. GFP_KERNEL);
  702. if (!trans_pcie->rxq)
  703. return -EINVAL;
  704. spin_lock_init(&rba->lock);
  705. for (i = 0; i < trans->num_rx_queues; i++) {
  706. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  707. ret = iwl_pcie_alloc_rxq_dma(trans, rxq);
  708. if (ret)
  709. return ret;
  710. }
  711. return 0;
  712. }
  713. static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
  714. {
  715. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  716. u32 rb_size;
  717. unsigned long flags;
  718. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  719. switch (trans_pcie->rx_buf_size) {
  720. case IWL_AMSDU_4K:
  721. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  722. break;
  723. case IWL_AMSDU_8K:
  724. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  725. break;
  726. case IWL_AMSDU_12K:
  727. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_12K;
  728. break;
  729. default:
  730. WARN_ON(1);
  731. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  732. }
  733. if (!iwl_trans_grab_nic_access(trans, &flags))
  734. return;
  735. /* Stop Rx DMA */
  736. iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  737. /* reset and flush pointers */
  738. iwl_write32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
  739. iwl_write32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
  740. iwl_write32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
  741. /* Reset driver's Rx queue write index */
  742. iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  743. /* Tell device where to find RBD circular buffer in DRAM */
  744. iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  745. (u32)(rxq->bd_dma >> 8));
  746. /* Tell device where in DRAM to update its Rx status */
  747. iwl_write32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  748. rxq->rb_stts_dma >> 4);
  749. /* Enable Rx DMA
  750. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  751. * the credit mechanism in 5000 HW RX FIFO
  752. * Direct rx interrupts to hosts
  753. * Rx buffer size 4 or 8k or 12k
  754. * RB timeout 0x10
  755. * 256 RBDs
  756. */
  757. iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  758. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  759. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  760. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  761. rb_size |
  762. (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
  763. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  764. iwl_trans_release_nic_access(trans, &flags);
  765. /* Set interrupt coalescing timer to default (2048 usecs) */
  766. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  767. /* W/A for interrupt coalescing bug in 7260 and 3160 */
  768. if (trans->cfg->host_interrupt_operation_mode)
  769. iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
  770. }
  771. void iwl_pcie_enable_rx_wake(struct iwl_trans *trans, bool enable)
  772. {
  773. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_9000)
  774. return;
  775. if (CSR_HW_REV_STEP(trans->hw_rev) != SILICON_A_STEP)
  776. return;
  777. if (!trans->cfg->integrated)
  778. return;
  779. /*
  780. * Turn on the chicken-bits that cause MAC wakeup for RX-related
  781. * values.
  782. * This costs some power, but needed for W/A 9000 integrated A-step
  783. * bug where shadow registers are not in the retention list and their
  784. * value is lost when NIC powers down
  785. */
  786. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL,
  787. CSR_MAC_SHADOW_REG_CTRL_RX_WAKE);
  788. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTL2,
  789. CSR_MAC_SHADOW_REG_CTL2_RX_WAKE);
  790. }
  791. static void iwl_pcie_rx_mq_hw_init(struct iwl_trans *trans)
  792. {
  793. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  794. u32 rb_size, enabled = 0;
  795. unsigned long flags;
  796. int i;
  797. switch (trans_pcie->rx_buf_size) {
  798. case IWL_AMSDU_2K:
  799. rb_size = RFH_RXF_DMA_RB_SIZE_2K;
  800. break;
  801. case IWL_AMSDU_4K:
  802. rb_size = RFH_RXF_DMA_RB_SIZE_4K;
  803. break;
  804. case IWL_AMSDU_8K:
  805. rb_size = RFH_RXF_DMA_RB_SIZE_8K;
  806. break;
  807. case IWL_AMSDU_12K:
  808. rb_size = RFH_RXF_DMA_RB_SIZE_12K;
  809. break;
  810. default:
  811. WARN_ON(1);
  812. rb_size = RFH_RXF_DMA_RB_SIZE_4K;
  813. }
  814. if (!iwl_trans_grab_nic_access(trans, &flags))
  815. return;
  816. /* Stop Rx DMA */
  817. iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG, 0);
  818. /* disable free amd used rx queue operation */
  819. iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, 0);
  820. for (i = 0; i < trans->num_rx_queues; i++) {
  821. /* Tell device where to find RBD free table in DRAM */
  822. iwl_write_prph64_no_grab(trans,
  823. RFH_Q_FRBDCB_BA_LSB(i),
  824. trans_pcie->rxq[i].bd_dma);
  825. /* Tell device where to find RBD used table in DRAM */
  826. iwl_write_prph64_no_grab(trans,
  827. RFH_Q_URBDCB_BA_LSB(i),
  828. trans_pcie->rxq[i].used_bd_dma);
  829. /* Tell device where in DRAM to update its Rx status */
  830. iwl_write_prph64_no_grab(trans,
  831. RFH_Q_URBD_STTS_WPTR_LSB(i),
  832. trans_pcie->rxq[i].rb_stts_dma);
  833. /* Reset device indice tables */
  834. iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_WIDX(i), 0);
  835. iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_RIDX(i), 0);
  836. iwl_write_prph_no_grab(trans, RFH_Q_URBDCB_WIDX(i), 0);
  837. enabled |= BIT(i) | BIT(i + 16);
  838. }
  839. /*
  840. * Enable Rx DMA
  841. * Rx buffer size 4 or 8k or 12k
  842. * Min RB size 4 or 8
  843. * Drop frames that exceed RB size
  844. * 512 RBDs
  845. */
  846. iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG,
  847. RFH_DMA_EN_ENABLE_VAL | rb_size |
  848. RFH_RXF_DMA_MIN_RB_4_8 |
  849. RFH_RXF_DMA_DROP_TOO_LARGE_MASK |
  850. RFH_RXF_DMA_RBDCB_SIZE_512);
  851. /*
  852. * Activate DMA snooping.
  853. * Set RX DMA chunk size to 64B for IOSF and 128B for PCIe
  854. * Default queue is 0
  855. */
  856. iwl_write_prph_no_grab(trans, RFH_GEN_CFG,
  857. RFH_GEN_CFG_RFH_DMA_SNOOP |
  858. RFH_GEN_CFG_VAL(DEFAULT_RXQ_NUM, 0) |
  859. RFH_GEN_CFG_SERVICE_DMA_SNOOP |
  860. RFH_GEN_CFG_VAL(RB_CHUNK_SIZE,
  861. trans->cfg->integrated ?
  862. RFH_GEN_CFG_RB_CHUNK_SIZE_64 :
  863. RFH_GEN_CFG_RB_CHUNK_SIZE_128));
  864. /* Enable the relevant rx queues */
  865. iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, enabled);
  866. iwl_trans_release_nic_access(trans, &flags);
  867. /* Set interrupt coalescing timer to default (2048 usecs) */
  868. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  869. iwl_pcie_enable_rx_wake(trans, true);
  870. }
  871. void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
  872. {
  873. lockdep_assert_held(&rxq->lock);
  874. INIT_LIST_HEAD(&rxq->rx_free);
  875. INIT_LIST_HEAD(&rxq->rx_used);
  876. rxq->free_count = 0;
  877. rxq->used_count = 0;
  878. }
  879. int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget)
  880. {
  881. WARN_ON(1);
  882. return 0;
  883. }
  884. static int _iwl_pcie_rx_init(struct iwl_trans *trans)
  885. {
  886. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  887. struct iwl_rxq *def_rxq;
  888. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  889. int i, err, queue_size, allocator_pool_size, num_alloc;
  890. if (!trans_pcie->rxq) {
  891. err = iwl_pcie_rx_alloc(trans);
  892. if (err)
  893. return err;
  894. }
  895. def_rxq = trans_pcie->rxq;
  896. cancel_work_sync(&rba->rx_alloc);
  897. spin_lock(&rba->lock);
  898. atomic_set(&rba->req_pending, 0);
  899. atomic_set(&rba->req_ready, 0);
  900. INIT_LIST_HEAD(&rba->rbd_allocated);
  901. INIT_LIST_HEAD(&rba->rbd_empty);
  902. spin_unlock(&rba->lock);
  903. /* free all first - we might be reconfigured for a different size */
  904. iwl_pcie_free_rbs_pool(trans);
  905. for (i = 0; i < RX_QUEUE_SIZE; i++)
  906. def_rxq->queue[i] = NULL;
  907. for (i = 0; i < trans->num_rx_queues; i++) {
  908. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  909. rxq->id = i;
  910. spin_lock(&rxq->lock);
  911. /*
  912. * Set read write pointer to reflect that we have processed
  913. * and used all buffers, but have not restocked the Rx queue
  914. * with fresh buffers
  915. */
  916. rxq->read = 0;
  917. rxq->write = 0;
  918. rxq->write_actual = 0;
  919. memset(rxq->rb_stts, 0,
  920. (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) ?
  921. sizeof(__le16) : sizeof(struct iwl_rb_status));
  922. iwl_pcie_rx_init_rxb_lists(rxq);
  923. if (!rxq->napi.poll)
  924. netif_napi_add(&trans_pcie->napi_dev, &rxq->napi,
  925. iwl_pcie_dummy_napi_poll, 64);
  926. spin_unlock(&rxq->lock);
  927. }
  928. /* move the pool to the default queue and allocator ownerships */
  929. queue_size = trans->cfg->mq_rx_supported ?
  930. MQ_RX_NUM_RBDS : RX_QUEUE_SIZE;
  931. allocator_pool_size = trans->num_rx_queues *
  932. (RX_CLAIM_REQ_ALLOC - RX_POST_REQ_ALLOC);
  933. num_alloc = queue_size + allocator_pool_size;
  934. BUILD_BUG_ON(ARRAY_SIZE(trans_pcie->global_table) !=
  935. ARRAY_SIZE(trans_pcie->rx_pool));
  936. for (i = 0; i < num_alloc; i++) {
  937. struct iwl_rx_mem_buffer *rxb = &trans_pcie->rx_pool[i];
  938. if (i < allocator_pool_size)
  939. list_add(&rxb->list, &rba->rbd_empty);
  940. else
  941. list_add(&rxb->list, &def_rxq->rx_used);
  942. trans_pcie->global_table[i] = rxb;
  943. rxb->vid = (u16)(i + 1);
  944. rxb->invalid = true;
  945. }
  946. iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL, def_rxq);
  947. return 0;
  948. }
  949. int iwl_pcie_rx_init(struct iwl_trans *trans)
  950. {
  951. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  952. int ret = _iwl_pcie_rx_init(trans);
  953. if (ret)
  954. return ret;
  955. if (trans->cfg->mq_rx_supported)
  956. iwl_pcie_rx_mq_hw_init(trans);
  957. else
  958. iwl_pcie_rx_hw_init(trans, trans_pcie->rxq);
  959. iwl_pcie_rxq_restock(trans, trans_pcie->rxq);
  960. spin_lock(&trans_pcie->rxq->lock);
  961. iwl_pcie_rxq_inc_wr_ptr(trans, trans_pcie->rxq);
  962. spin_unlock(&trans_pcie->rxq->lock);
  963. return 0;
  964. }
  965. int iwl_pcie_gen2_rx_init(struct iwl_trans *trans)
  966. {
  967. /*
  968. * We don't configure the RFH.
  969. * Restock will be done at alive, after firmware configured the RFH.
  970. */
  971. return _iwl_pcie_rx_init(trans);
  972. }
  973. void iwl_pcie_rx_free(struct iwl_trans *trans)
  974. {
  975. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  976. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  977. int i;
  978. /*
  979. * if rxq is NULL, it means that nothing has been allocated,
  980. * exit now
  981. */
  982. if (!trans_pcie->rxq) {
  983. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  984. return;
  985. }
  986. cancel_work_sync(&rba->rx_alloc);
  987. iwl_pcie_free_rbs_pool(trans);
  988. for (i = 0; i < trans->num_rx_queues; i++) {
  989. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  990. iwl_pcie_free_rxq_dma(trans, rxq);
  991. if (rxq->napi.poll)
  992. netif_napi_del(&rxq->napi);
  993. }
  994. kfree(trans_pcie->rxq);
  995. }
  996. static void iwl_pcie_rx_move_to_allocator(struct iwl_rxq *rxq,
  997. struct iwl_rb_allocator *rba)
  998. {
  999. spin_lock(&rba->lock);
  1000. list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
  1001. spin_unlock(&rba->lock);
  1002. }
  1003. /*
  1004. * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
  1005. *
  1006. * Called when a RBD can be reused. The RBD is transferred to the allocator.
  1007. * When there are 2 empty RBDs - a request for allocation is posted
  1008. */
  1009. static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
  1010. struct iwl_rx_mem_buffer *rxb,
  1011. struct iwl_rxq *rxq, bool emergency)
  1012. {
  1013. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1014. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  1015. /* Move the RBD to the used list, will be moved to allocator in batches
  1016. * before claiming or posting a request*/
  1017. list_add_tail(&rxb->list, &rxq->rx_used);
  1018. if (unlikely(emergency))
  1019. return;
  1020. /* Count the allocator owned RBDs */
  1021. rxq->used_count++;
  1022. /* If we have RX_POST_REQ_ALLOC new released rx buffers -
  1023. * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
  1024. * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
  1025. * after but we still need to post another request.
  1026. */
  1027. if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
  1028. /* Move the 2 RBDs to the allocator ownership.
  1029. Allocator has another 6 from pool for the request completion*/
  1030. iwl_pcie_rx_move_to_allocator(rxq, rba);
  1031. atomic_inc(&rba->req_pending);
  1032. queue_work(rba->alloc_wq, &rba->rx_alloc);
  1033. }
  1034. }
  1035. static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
  1036. struct iwl_rxq *rxq,
  1037. struct iwl_rx_mem_buffer *rxb,
  1038. bool emergency,
  1039. int i)
  1040. {
  1041. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1042. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  1043. bool page_stolen = false;
  1044. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  1045. u32 offset = 0;
  1046. if (WARN_ON(!rxb))
  1047. return;
  1048. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  1049. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  1050. struct iwl_rx_packet *pkt;
  1051. u16 sequence;
  1052. bool reclaim;
  1053. int index, cmd_index, len;
  1054. struct iwl_rx_cmd_buffer rxcb = {
  1055. ._offset = offset,
  1056. ._rx_page_order = trans_pcie->rx_page_order,
  1057. ._page = rxb->page,
  1058. ._page_stolen = false,
  1059. .truesize = max_len,
  1060. };
  1061. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  1062. rxcb.status = rxq->cd[i].status;
  1063. pkt = rxb_addr(&rxcb);
  1064. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID)) {
  1065. IWL_DEBUG_RX(trans,
  1066. "Q %d: RB end marker at offset %d\n",
  1067. rxq->id, offset);
  1068. break;
  1069. }
  1070. WARN((le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
  1071. FH_RSCSR_RXQ_POS != rxq->id,
  1072. "frame on invalid queue - is on %d and indicates %d\n",
  1073. rxq->id,
  1074. (le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
  1075. FH_RSCSR_RXQ_POS);
  1076. IWL_DEBUG_RX(trans,
  1077. "Q %d: cmd at offset %d: %s (%.2x.%2x, seq 0x%x)\n",
  1078. rxq->id, offset,
  1079. iwl_get_cmd_string(trans,
  1080. iwl_cmd_id(pkt->hdr.cmd,
  1081. pkt->hdr.group_id,
  1082. 0)),
  1083. pkt->hdr.group_id, pkt->hdr.cmd,
  1084. le16_to_cpu(pkt->hdr.sequence));
  1085. len = iwl_rx_packet_len(pkt);
  1086. len += sizeof(u32); /* account for status word */
  1087. trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
  1088. trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
  1089. /* Reclaim a command buffer only if this packet is a response
  1090. * to a (driver-originated) command.
  1091. * If the packet (e.g. Rx frame) originated from uCode,
  1092. * there is no command buffer to reclaim.
  1093. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1094. * but apparently a few don't get set; catch them here. */
  1095. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  1096. if (reclaim && !pkt->hdr.group_id) {
  1097. int i;
  1098. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  1099. if (trans_pcie->no_reclaim_cmds[i] ==
  1100. pkt->hdr.cmd) {
  1101. reclaim = false;
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. sequence = le16_to_cpu(pkt->hdr.sequence);
  1107. index = SEQ_TO_INDEX(sequence);
  1108. cmd_index = iwl_pcie_get_cmd_index(txq, index);
  1109. if (rxq->id == 0)
  1110. iwl_op_mode_rx(trans->op_mode, &rxq->napi,
  1111. &rxcb);
  1112. else
  1113. iwl_op_mode_rx_rss(trans->op_mode, &rxq->napi,
  1114. &rxcb, rxq->id);
  1115. if (reclaim) {
  1116. kzfree(txq->entries[cmd_index].free_buf);
  1117. txq->entries[cmd_index].free_buf = NULL;
  1118. }
  1119. /*
  1120. * After here, we should always check rxcb._page_stolen,
  1121. * if it is true then one of the handlers took the page.
  1122. */
  1123. if (reclaim) {
  1124. /* Invoke any callbacks, transfer the buffer to caller,
  1125. * and fire off the (possibly) blocking
  1126. * iwl_trans_send_cmd()
  1127. * as we reclaim the driver command queue */
  1128. if (!rxcb._page_stolen)
  1129. iwl_pcie_hcmd_complete(trans, &rxcb);
  1130. else
  1131. IWL_WARN(trans, "Claim null rxb?\n");
  1132. }
  1133. page_stolen |= rxcb._page_stolen;
  1134. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  1135. break;
  1136. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  1137. }
  1138. /* page was stolen from us -- free our reference */
  1139. if (page_stolen) {
  1140. __free_pages(rxb->page, trans_pcie->rx_page_order);
  1141. rxb->page = NULL;
  1142. }
  1143. /* Reuse the page if possible. For notification packets and
  1144. * SKBs that fail to Rx correctly, add them back into the
  1145. * rx_free list for reuse later. */
  1146. if (rxb->page != NULL) {
  1147. rxb->page_dma =
  1148. dma_map_page(trans->dev, rxb->page, 0,
  1149. PAGE_SIZE << trans_pcie->rx_page_order,
  1150. DMA_FROM_DEVICE);
  1151. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  1152. /*
  1153. * free the page(s) as well to not break
  1154. * the invariant that the items on the used
  1155. * list have no page(s)
  1156. */
  1157. __free_pages(rxb->page, trans_pcie->rx_page_order);
  1158. rxb->page = NULL;
  1159. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  1160. } else {
  1161. list_add_tail(&rxb->list, &rxq->rx_free);
  1162. rxq->free_count++;
  1163. }
  1164. } else
  1165. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  1166. }
  1167. static struct iwl_rx_mem_buffer *iwl_pcie_get_rxb(struct iwl_trans *trans,
  1168. struct iwl_rxq *rxq, int i)
  1169. {
  1170. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1171. struct iwl_rx_mem_buffer *rxb;
  1172. u16 vid;
  1173. if (!trans->cfg->mq_rx_supported) {
  1174. rxb = rxq->queue[i];
  1175. rxq->queue[i] = NULL;
  1176. return rxb;
  1177. }
  1178. /* used_bd is a 32/16 bit but only 12 are used to retrieve the vid */
  1179. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  1180. vid = le16_to_cpu(rxq->cd[i].rbid) & 0x0FFF;
  1181. else
  1182. vid = le32_to_cpu(rxq->bd_32[i]) & 0x0FFF;
  1183. if (!vid || vid > ARRAY_SIZE(trans_pcie->global_table))
  1184. goto out_err;
  1185. rxb = trans_pcie->global_table[vid - 1];
  1186. if (rxb->invalid)
  1187. goto out_err;
  1188. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  1189. rxb->size = le32_to_cpu(rxq->cd[i].size) & IWL_RX_CD_SIZE;
  1190. rxb->invalid = true;
  1191. return rxb;
  1192. out_err:
  1193. WARN(1, "Invalid rxb from HW %u\n", (u32)vid);
  1194. iwl_force_nmi(trans);
  1195. return NULL;
  1196. }
  1197. /*
  1198. * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
  1199. */
  1200. static void iwl_pcie_rx_handle(struct iwl_trans *trans, int queue)
  1201. {
  1202. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1203. struct iwl_rxq *rxq;
  1204. u32 r, i, count = 0;
  1205. bool emergency = false;
  1206. if (WARN_ON_ONCE(!trans_pcie->rxq || !trans_pcie->rxq[queue].bd))
  1207. return;
  1208. rxq = &trans_pcie->rxq[queue];
  1209. restart:
  1210. spin_lock(&rxq->lock);
  1211. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1212. * buffer that the driver may process (last buffer filled by ucode). */
  1213. r = le16_to_cpu(iwl_get_closed_rb_stts(trans, rxq)) & 0x0FFF;
  1214. i = rxq->read;
  1215. /* W/A 9000 device step A0 wrap-around bug */
  1216. r &= (rxq->queue_size - 1);
  1217. /* Rx interrupt, but nothing sent from uCode */
  1218. if (i == r)
  1219. IWL_DEBUG_RX(trans, "Q %d: HW = SW = %d\n", rxq->id, r);
  1220. while (i != r) {
  1221. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  1222. struct iwl_rx_mem_buffer *rxb;
  1223. /* number of RBDs still waiting for page allocation */
  1224. u32 rb_pending_alloc =
  1225. atomic_read(&trans_pcie->rba.req_pending) *
  1226. RX_CLAIM_REQ_ALLOC;
  1227. if (unlikely(rb_pending_alloc >= rxq->queue_size / 2 &&
  1228. !emergency)) {
  1229. iwl_pcie_rx_move_to_allocator(rxq, rba);
  1230. emergency = true;
  1231. }
  1232. rxb = iwl_pcie_get_rxb(trans, rxq, i);
  1233. if (!rxb)
  1234. goto out;
  1235. IWL_DEBUG_RX(trans, "Q %d: HW = %d, SW = %d\n", rxq->id, r, i);
  1236. iwl_pcie_rx_handle_rb(trans, rxq, rxb, emergency, i);
  1237. i = (i + 1) & (rxq->queue_size - 1);
  1238. /*
  1239. * If we have RX_CLAIM_REQ_ALLOC released rx buffers -
  1240. * try to claim the pre-allocated buffers from the allocator.
  1241. * If not ready - will try to reclaim next time.
  1242. * There is no need to reschedule work - allocator exits only
  1243. * on success
  1244. */
  1245. if (rxq->used_count >= RX_CLAIM_REQ_ALLOC)
  1246. iwl_pcie_rx_allocator_get(trans, rxq);
  1247. if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 && !emergency) {
  1248. /* Add the remaining empty RBDs for allocator use */
  1249. iwl_pcie_rx_move_to_allocator(rxq, rba);
  1250. } else if (emergency) {
  1251. count++;
  1252. if (count == 8) {
  1253. count = 0;
  1254. if (rb_pending_alloc < rxq->queue_size / 3)
  1255. emergency = false;
  1256. rxq->read = i;
  1257. spin_unlock(&rxq->lock);
  1258. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
  1259. iwl_pcie_rxq_restock(trans, rxq);
  1260. goto restart;
  1261. }
  1262. }
  1263. }
  1264. out:
  1265. /* Backtrack one entry */
  1266. rxq->read = i;
  1267. /* update cr tail with the rxq read pointer */
  1268. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  1269. *rxq->cr_tail = cpu_to_le16(r);
  1270. spin_unlock(&rxq->lock);
  1271. /*
  1272. * handle a case where in emergency there are some unallocated RBDs.
  1273. * those RBDs are in the used list, but are not tracked by the queue's
  1274. * used_count which counts allocator owned RBDs.
  1275. * unallocated emergency RBDs must be allocated on exit, otherwise
  1276. * when called again the function may not be in emergency mode and
  1277. * they will be handed to the allocator with no tracking in the RBD
  1278. * allocator counters, which will lead to them never being claimed back
  1279. * by the queue.
  1280. * by allocating them here, they are now in the queue free list, and
  1281. * will be restocked by the next call of iwl_pcie_rxq_restock.
  1282. */
  1283. if (unlikely(emergency && count))
  1284. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
  1285. if (rxq->napi.poll)
  1286. napi_gro_flush(&rxq->napi, false);
  1287. iwl_pcie_rxq_restock(trans, rxq);
  1288. }
  1289. static struct iwl_trans_pcie *iwl_pcie_get_trans_pcie(struct msix_entry *entry)
  1290. {
  1291. u8 queue = entry->entry;
  1292. struct msix_entry *entries = entry - queue;
  1293. return container_of(entries, struct iwl_trans_pcie, msix_entries[0]);
  1294. }
  1295. /*
  1296. * iwl_pcie_rx_msix_handle - Main entry function for receiving responses from fw
  1297. * This interrupt handler should be used with RSS queue only.
  1298. */
  1299. irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id)
  1300. {
  1301. struct msix_entry *entry = dev_id;
  1302. struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
  1303. struct iwl_trans *trans = trans_pcie->trans;
  1304. trace_iwlwifi_dev_irq_msix(trans->dev, entry, false, 0, 0);
  1305. if (WARN_ON(entry->entry >= trans->num_rx_queues))
  1306. return IRQ_NONE;
  1307. lock_map_acquire(&trans->sync_cmd_lockdep_map);
  1308. local_bh_disable();
  1309. iwl_pcie_rx_handle(trans, entry->entry);
  1310. local_bh_enable();
  1311. iwl_pcie_clear_irq(trans, entry);
  1312. lock_map_release(&trans->sync_cmd_lockdep_map);
  1313. return IRQ_HANDLED;
  1314. }
  1315. /*
  1316. * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
  1317. */
  1318. static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
  1319. {
  1320. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1321. int i;
  1322. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  1323. if (trans->cfg->internal_wimax_coex &&
  1324. !trans->cfg->apmg_not_supported &&
  1325. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  1326. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  1327. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  1328. APMG_PS_CTRL_VAL_RESET_REQ))) {
  1329. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1330. iwl_op_mode_wimax_active(trans->op_mode);
  1331. wake_up(&trans_pcie->wait_command_queue);
  1332. return;
  1333. }
  1334. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  1335. if (!trans_pcie->txq[i])
  1336. continue;
  1337. del_timer(&trans_pcie->txq[i]->stuck_timer);
  1338. }
  1339. /* The STATUS_FW_ERROR bit is set in this function. This must happen
  1340. * before we wake up the command caller, to ensure a proper cleanup. */
  1341. iwl_trans_fw_error(trans);
  1342. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1343. wake_up(&trans_pcie->wait_command_queue);
  1344. }
  1345. static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
  1346. {
  1347. u32 inta;
  1348. lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
  1349. trace_iwlwifi_dev_irq(trans->dev);
  1350. /* Discover which interrupts are active/pending */
  1351. inta = iwl_read32(trans, CSR_INT);
  1352. /* the thread will service interrupts and re-enable them */
  1353. return inta;
  1354. }
  1355. /* a device (PCI-E) page is 4096 bytes long */
  1356. #define ICT_SHIFT 12
  1357. #define ICT_SIZE (1 << ICT_SHIFT)
  1358. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  1359. /* interrupt handler using ict table, with this interrupt driver will
  1360. * stop using INTA register to get device's interrupt, reading this register
  1361. * is expensive, device will write interrupts in ICT dram table, increment
  1362. * index then will fire interrupt to driver, driver will OR all ICT table
  1363. * entries from current index up to table entry with 0 value. the result is
  1364. * the interrupt we need to service, driver will set the entries back to 0 and
  1365. * set index.
  1366. */
  1367. static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
  1368. {
  1369. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1370. u32 inta;
  1371. u32 val = 0;
  1372. u32 read;
  1373. trace_iwlwifi_dev_irq(trans->dev);
  1374. /* Ignore interrupt if there's nothing in NIC to service.
  1375. * This may be due to IRQ shared with another device,
  1376. * or due to sporadic interrupts thrown from our NIC. */
  1377. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1378. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  1379. if (!read)
  1380. return 0;
  1381. /*
  1382. * Collect all entries up to the first 0, starting from ict_index;
  1383. * note we already read at ict_index.
  1384. */
  1385. do {
  1386. val |= read;
  1387. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1388. trans_pcie->ict_index, read);
  1389. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1390. trans_pcie->ict_index =
  1391. ((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
  1392. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1393. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1394. read);
  1395. } while (read);
  1396. /* We should not get this value, just ignore it. */
  1397. if (val == 0xffffffff)
  1398. val = 0;
  1399. /*
  1400. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1401. * (bit 15 before shifting it to 31) to clear when using interrupt
  1402. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1403. * so we use them to decide on the real state of the Rx bit.
  1404. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1405. */
  1406. if (val & 0xC0000)
  1407. val |= 0x8000;
  1408. inta = (0xff & val) | ((0xff00 & val) << 16);
  1409. return inta;
  1410. }
  1411. void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans)
  1412. {
  1413. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1414. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1415. bool hw_rfkill, prev, report;
  1416. mutex_lock(&trans_pcie->mutex);
  1417. prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
  1418. hw_rfkill = iwl_is_rfkill_set(trans);
  1419. if (hw_rfkill) {
  1420. set_bit(STATUS_RFKILL_OPMODE, &trans->status);
  1421. set_bit(STATUS_RFKILL_HW, &trans->status);
  1422. }
  1423. if (trans_pcie->opmode_down)
  1424. report = hw_rfkill;
  1425. else
  1426. report = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
  1427. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  1428. hw_rfkill ? "disable radio" : "enable radio");
  1429. isr_stats->rfkill++;
  1430. if (prev != report)
  1431. iwl_trans_pcie_rf_kill(trans, report);
  1432. mutex_unlock(&trans_pcie->mutex);
  1433. if (hw_rfkill) {
  1434. if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
  1435. &trans->status))
  1436. IWL_DEBUG_RF_KILL(trans,
  1437. "Rfkill while SYNC HCMD in flight\n");
  1438. wake_up(&trans_pcie->wait_command_queue);
  1439. } else {
  1440. clear_bit(STATUS_RFKILL_HW, &trans->status);
  1441. if (trans_pcie->opmode_down)
  1442. clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
  1443. }
  1444. }
  1445. irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
  1446. {
  1447. struct iwl_trans *trans = dev_id;
  1448. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1449. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1450. u32 inta = 0;
  1451. u32 handled = 0;
  1452. lock_map_acquire(&trans->sync_cmd_lockdep_map);
  1453. spin_lock(&trans_pcie->irq_lock);
  1454. /* dram interrupt table not set yet,
  1455. * use legacy interrupt.
  1456. */
  1457. if (likely(trans_pcie->use_ict))
  1458. inta = iwl_pcie_int_cause_ict(trans);
  1459. else
  1460. inta = iwl_pcie_int_cause_non_ict(trans);
  1461. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1462. IWL_DEBUG_ISR(trans,
  1463. "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
  1464. inta, trans_pcie->inta_mask,
  1465. iwl_read32(trans, CSR_INT_MASK),
  1466. iwl_read32(trans, CSR_FH_INT_STATUS));
  1467. if (inta & (~trans_pcie->inta_mask))
  1468. IWL_DEBUG_ISR(trans,
  1469. "We got a masked interrupt (0x%08x)\n",
  1470. inta & (~trans_pcie->inta_mask));
  1471. }
  1472. inta &= trans_pcie->inta_mask;
  1473. /*
  1474. * Ignore interrupt if there's nothing in NIC to service.
  1475. * This may be due to IRQ shared with another device,
  1476. * or due to sporadic interrupts thrown from our NIC.
  1477. */
  1478. if (unlikely(!inta)) {
  1479. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1480. /*
  1481. * Re-enable interrupts here since we don't
  1482. * have anything to service
  1483. */
  1484. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1485. _iwl_enable_interrupts(trans);
  1486. spin_unlock(&trans_pcie->irq_lock);
  1487. lock_map_release(&trans->sync_cmd_lockdep_map);
  1488. return IRQ_NONE;
  1489. }
  1490. if (unlikely(inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1491. /*
  1492. * Hardware disappeared. It might have
  1493. * already raised an interrupt.
  1494. */
  1495. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1496. spin_unlock(&trans_pcie->irq_lock);
  1497. goto out;
  1498. }
  1499. /* Ack/clear/reset pending uCode interrupts.
  1500. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1501. */
  1502. /* There is a hardware bug in the interrupt mask function that some
  1503. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  1504. * they are disabled in the CSR_INT_MASK register. Furthermore the
  1505. * ICT interrupt handling mechanism has another bug that might cause
  1506. * these unmasked interrupts fail to be detected. We workaround the
  1507. * hardware bugs here by ACKing all the possible interrupts so that
  1508. * interrupt coalescing can still be achieved.
  1509. */
  1510. iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
  1511. if (iwl_have_debug_level(IWL_DL_ISR))
  1512. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  1513. inta, iwl_read32(trans, CSR_INT_MASK));
  1514. spin_unlock(&trans_pcie->irq_lock);
  1515. /* Now service all interrupt bits discovered above. */
  1516. if (inta & CSR_INT_BIT_HW_ERR) {
  1517. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  1518. /* Tell the device to stop sending interrupts */
  1519. iwl_disable_interrupts(trans);
  1520. isr_stats->hw++;
  1521. iwl_pcie_irq_handle_error(trans);
  1522. handled |= CSR_INT_BIT_HW_ERR;
  1523. goto out;
  1524. }
  1525. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1526. if (inta & CSR_INT_BIT_SCD) {
  1527. IWL_DEBUG_ISR(trans,
  1528. "Scheduler finished to transmit the frame/frames.\n");
  1529. isr_stats->sch++;
  1530. }
  1531. /* Alive notification via Rx interrupt will do the real work */
  1532. if (inta & CSR_INT_BIT_ALIVE) {
  1533. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  1534. isr_stats->alive++;
  1535. if (trans->cfg->gen2) {
  1536. /*
  1537. * We can restock, since firmware configured
  1538. * the RFH
  1539. */
  1540. iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
  1541. }
  1542. handled |= CSR_INT_BIT_ALIVE;
  1543. }
  1544. /* Safely ignore these bits for debug checks below */
  1545. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1546. /* HW RF KILL switch toggled */
  1547. if (inta & CSR_INT_BIT_RF_KILL) {
  1548. iwl_pcie_handle_rfkill_irq(trans);
  1549. handled |= CSR_INT_BIT_RF_KILL;
  1550. }
  1551. /* Chip got too hot and stopped itself */
  1552. if (inta & CSR_INT_BIT_CT_KILL) {
  1553. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  1554. isr_stats->ctkill++;
  1555. handled |= CSR_INT_BIT_CT_KILL;
  1556. }
  1557. /* Error detected by uCode */
  1558. if (inta & CSR_INT_BIT_SW_ERR) {
  1559. IWL_ERR(trans, "Microcode SW error detected. "
  1560. " Restarting 0x%X.\n", inta);
  1561. isr_stats->sw++;
  1562. iwl_pcie_irq_handle_error(trans);
  1563. handled |= CSR_INT_BIT_SW_ERR;
  1564. }
  1565. /* uCode wakes up after power-down sleep */
  1566. if (inta & CSR_INT_BIT_WAKEUP) {
  1567. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  1568. iwl_pcie_rxq_check_wrptr(trans);
  1569. iwl_pcie_txq_check_wrptrs(trans);
  1570. isr_stats->wakeup++;
  1571. handled |= CSR_INT_BIT_WAKEUP;
  1572. }
  1573. /* All uCode command responses, including Tx command responses,
  1574. * Rx "responses" (frame-received notification), and other
  1575. * notifications from uCode come through here*/
  1576. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  1577. CSR_INT_BIT_RX_PERIODIC)) {
  1578. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  1579. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1580. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1581. iwl_write32(trans, CSR_FH_INT_STATUS,
  1582. CSR_FH_INT_RX_MASK);
  1583. }
  1584. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  1585. handled |= CSR_INT_BIT_RX_PERIODIC;
  1586. iwl_write32(trans,
  1587. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  1588. }
  1589. /* Sending RX interrupt require many steps to be done in the
  1590. * the device:
  1591. * 1- write interrupt to current index in ICT table.
  1592. * 2- dma RX frame.
  1593. * 3- update RX shared data to indicate last write index.
  1594. * 4- send interrupt.
  1595. * This could lead to RX race, driver could receive RX interrupt
  1596. * but the shared data changes does not reflect this;
  1597. * periodic interrupt will detect any dangling Rx activity.
  1598. */
  1599. /* Disable periodic interrupt; we use it as just a one-shot. */
  1600. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1601. CSR_INT_PERIODIC_DIS);
  1602. /*
  1603. * Enable periodic interrupt in 8 msec only if we received
  1604. * real RX interrupt (instead of just periodic int), to catch
  1605. * any dangling Rx interrupt. If it was just the periodic
  1606. * interrupt, there was no dangling Rx activity, and no need
  1607. * to extend the periodic interrupt; one-shot is enough.
  1608. */
  1609. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  1610. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1611. CSR_INT_PERIODIC_ENA);
  1612. isr_stats->rx++;
  1613. local_bh_disable();
  1614. iwl_pcie_rx_handle(trans, 0);
  1615. local_bh_enable();
  1616. }
  1617. /* This "Tx" DMA channel is used only for loading uCode */
  1618. if (inta & CSR_INT_BIT_FH_TX) {
  1619. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  1620. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  1621. isr_stats->tx++;
  1622. handled |= CSR_INT_BIT_FH_TX;
  1623. /* Wake up uCode load routine, now that load is complete */
  1624. trans_pcie->ucode_write_complete = true;
  1625. wake_up(&trans_pcie->ucode_write_waitq);
  1626. }
  1627. if (inta & ~handled) {
  1628. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1629. isr_stats->unhandled++;
  1630. }
  1631. if (inta & ~(trans_pcie->inta_mask)) {
  1632. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  1633. inta & ~trans_pcie->inta_mask);
  1634. }
  1635. spin_lock(&trans_pcie->irq_lock);
  1636. /* only Re-enable all interrupt if disabled by irq */
  1637. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1638. _iwl_enable_interrupts(trans);
  1639. /* we are loading the firmware, enable FH_TX interrupt only */
  1640. else if (handled & CSR_INT_BIT_FH_TX)
  1641. iwl_enable_fw_load_int(trans);
  1642. /* Re-enable RF_KILL if it occurred */
  1643. else if (handled & CSR_INT_BIT_RF_KILL)
  1644. iwl_enable_rfkill_int(trans);
  1645. /* Re-enable the ALIVE / Rx interrupt if it occurred */
  1646. else if (handled & (CSR_INT_BIT_ALIVE | CSR_INT_BIT_FH_RX))
  1647. iwl_enable_fw_load_int_ctx_info(trans);
  1648. spin_unlock(&trans_pcie->irq_lock);
  1649. out:
  1650. lock_map_release(&trans->sync_cmd_lockdep_map);
  1651. return IRQ_HANDLED;
  1652. }
  1653. /******************************************************************************
  1654. *
  1655. * ICT functions
  1656. *
  1657. ******************************************************************************/
  1658. /* Free dram table */
  1659. void iwl_pcie_free_ict(struct iwl_trans *trans)
  1660. {
  1661. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1662. if (trans_pcie->ict_tbl) {
  1663. dma_free_coherent(trans->dev, ICT_SIZE,
  1664. trans_pcie->ict_tbl,
  1665. trans_pcie->ict_tbl_dma);
  1666. trans_pcie->ict_tbl = NULL;
  1667. trans_pcie->ict_tbl_dma = 0;
  1668. }
  1669. }
  1670. /*
  1671. * allocate dram shared table, it is an aligned memory
  1672. * block of ICT_SIZE.
  1673. * also reset all data related to ICT table interrupt.
  1674. */
  1675. int iwl_pcie_alloc_ict(struct iwl_trans *trans)
  1676. {
  1677. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1678. trans_pcie->ict_tbl =
  1679. dma_zalloc_coherent(trans->dev, ICT_SIZE,
  1680. &trans_pcie->ict_tbl_dma,
  1681. GFP_KERNEL);
  1682. if (!trans_pcie->ict_tbl)
  1683. return -ENOMEM;
  1684. /* just an API sanity check ... it is guaranteed to be aligned */
  1685. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  1686. iwl_pcie_free_ict(trans);
  1687. return -EINVAL;
  1688. }
  1689. return 0;
  1690. }
  1691. /* Device is going up inform it about using ICT interrupt table,
  1692. * also we need to tell the driver to start using ICT interrupt.
  1693. */
  1694. void iwl_pcie_reset_ict(struct iwl_trans *trans)
  1695. {
  1696. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1697. u32 val;
  1698. if (!trans_pcie->ict_tbl)
  1699. return;
  1700. spin_lock(&trans_pcie->irq_lock);
  1701. _iwl_disable_interrupts(trans);
  1702. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1703. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  1704. val |= CSR_DRAM_INT_TBL_ENABLE |
  1705. CSR_DRAM_INIT_TBL_WRAP_CHECK |
  1706. CSR_DRAM_INIT_TBL_WRITE_POINTER;
  1707. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  1708. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  1709. trans_pcie->use_ict = true;
  1710. trans_pcie->ict_index = 0;
  1711. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  1712. _iwl_enable_interrupts(trans);
  1713. spin_unlock(&trans_pcie->irq_lock);
  1714. }
  1715. /* Device is going down disable ict interrupt usage */
  1716. void iwl_pcie_disable_ict(struct iwl_trans *trans)
  1717. {
  1718. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1719. spin_lock(&trans_pcie->irq_lock);
  1720. trans_pcie->use_ict = false;
  1721. spin_unlock(&trans_pcie->irq_lock);
  1722. }
  1723. irqreturn_t iwl_pcie_isr(int irq, void *data)
  1724. {
  1725. struct iwl_trans *trans = data;
  1726. if (!trans)
  1727. return IRQ_NONE;
  1728. /* Disable (but don't clear!) interrupts here to avoid
  1729. * back-to-back ISRs and sporadic interrupts from our NIC.
  1730. * If we have something to service, the tasklet will re-enable ints.
  1731. * If we *don't* have something, we'll re-enable before leaving here.
  1732. */
  1733. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1734. return IRQ_WAKE_THREAD;
  1735. }
  1736. irqreturn_t iwl_pcie_msix_isr(int irq, void *data)
  1737. {
  1738. return IRQ_WAKE_THREAD;
  1739. }
  1740. irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id)
  1741. {
  1742. struct msix_entry *entry = dev_id;
  1743. struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
  1744. struct iwl_trans *trans = trans_pcie->trans;
  1745. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1746. u32 inta_fh, inta_hw;
  1747. lock_map_acquire(&trans->sync_cmd_lockdep_map);
  1748. spin_lock(&trans_pcie->irq_lock);
  1749. inta_fh = iwl_read32(trans, CSR_MSIX_FH_INT_CAUSES_AD);
  1750. inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD);
  1751. /*
  1752. * Clear causes registers to avoid being handling the same cause.
  1753. */
  1754. iwl_write32(trans, CSR_MSIX_FH_INT_CAUSES_AD, inta_fh);
  1755. iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD, inta_hw);
  1756. spin_unlock(&trans_pcie->irq_lock);
  1757. trace_iwlwifi_dev_irq_msix(trans->dev, entry, true, inta_fh, inta_hw);
  1758. if (unlikely(!(inta_fh | inta_hw))) {
  1759. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1760. lock_map_release(&trans->sync_cmd_lockdep_map);
  1761. return IRQ_NONE;
  1762. }
  1763. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1764. IWL_DEBUG_ISR(trans,
  1765. "ISR inta_fh 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
  1766. inta_fh, trans_pcie->fh_mask,
  1767. iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD));
  1768. if (inta_fh & ~trans_pcie->fh_mask)
  1769. IWL_DEBUG_ISR(trans,
  1770. "We got a masked interrupt (0x%08x)\n",
  1771. inta_fh & ~trans_pcie->fh_mask);
  1772. }
  1773. inta_fh &= trans_pcie->fh_mask;
  1774. if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX) &&
  1775. inta_fh & MSIX_FH_INT_CAUSES_Q0) {
  1776. local_bh_disable();
  1777. iwl_pcie_rx_handle(trans, 0);
  1778. local_bh_enable();
  1779. }
  1780. if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS) &&
  1781. inta_fh & MSIX_FH_INT_CAUSES_Q1) {
  1782. local_bh_disable();
  1783. iwl_pcie_rx_handle(trans, 1);
  1784. local_bh_enable();
  1785. }
  1786. /* This "Tx" DMA channel is used only for loading uCode */
  1787. if (inta_fh & MSIX_FH_INT_CAUSES_D2S_CH0_NUM) {
  1788. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  1789. isr_stats->tx++;
  1790. /*
  1791. * Wake up uCode load routine,
  1792. * now that load is complete
  1793. */
  1794. trans_pcie->ucode_write_complete = true;
  1795. wake_up(&trans_pcie->ucode_write_waitq);
  1796. }
  1797. /* Error detected by uCode */
  1798. if ((inta_fh & MSIX_FH_INT_CAUSES_FH_ERR) ||
  1799. (inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR) ||
  1800. (inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR_V2)) {
  1801. IWL_ERR(trans,
  1802. "Microcode SW error detected. Restarting 0x%X.\n",
  1803. inta_fh);
  1804. isr_stats->sw++;
  1805. iwl_pcie_irq_handle_error(trans);
  1806. }
  1807. /* After checking FH register check HW register */
  1808. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1809. IWL_DEBUG_ISR(trans,
  1810. "ISR inta_hw 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
  1811. inta_hw, trans_pcie->hw_mask,
  1812. iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD));
  1813. if (inta_hw & ~trans_pcie->hw_mask)
  1814. IWL_DEBUG_ISR(trans,
  1815. "We got a masked interrupt 0x%08x\n",
  1816. inta_hw & ~trans_pcie->hw_mask);
  1817. }
  1818. inta_hw &= trans_pcie->hw_mask;
  1819. /* Alive notification via Rx interrupt will do the real work */
  1820. if (inta_hw & MSIX_HW_INT_CAUSES_REG_ALIVE) {
  1821. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  1822. isr_stats->alive++;
  1823. if (trans->cfg->gen2) {
  1824. /* We can restock, since firmware configured the RFH */
  1825. iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
  1826. }
  1827. }
  1828. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560 &&
  1829. inta_hw & MSIX_HW_INT_CAUSES_REG_IPC) {
  1830. /* Reflect IML transfer status */
  1831. int res = iwl_read32(trans, CSR_IML_RESP_ADDR);
  1832. IWL_DEBUG_ISR(trans, "IML transfer status: %d\n", res);
  1833. if (res == IWL_IMAGE_RESP_FAIL) {
  1834. isr_stats->sw++;
  1835. iwl_pcie_irq_handle_error(trans);
  1836. }
  1837. } else if (inta_hw & MSIX_HW_INT_CAUSES_REG_WAKEUP) {
  1838. /* uCode wakes up after power-down sleep */
  1839. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  1840. iwl_pcie_rxq_check_wrptr(trans);
  1841. iwl_pcie_txq_check_wrptrs(trans);
  1842. isr_stats->wakeup++;
  1843. }
  1844. /* Chip got too hot and stopped itself */
  1845. if (inta_hw & MSIX_HW_INT_CAUSES_REG_CT_KILL) {
  1846. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  1847. isr_stats->ctkill++;
  1848. }
  1849. /* HW RF KILL switch toggled */
  1850. if (inta_hw & MSIX_HW_INT_CAUSES_REG_RF_KILL)
  1851. iwl_pcie_handle_rfkill_irq(trans);
  1852. if (inta_hw & MSIX_HW_INT_CAUSES_REG_HW_ERR) {
  1853. IWL_ERR(trans,
  1854. "Hardware error detected. Restarting.\n");
  1855. isr_stats->hw++;
  1856. iwl_pcie_irq_handle_error(trans);
  1857. }
  1858. iwl_pcie_clear_irq(trans, entry);
  1859. lock_map_release(&trans->sync_cmd_lockdep_map);
  1860. return IRQ_HANDLED;
  1861. }