rx.c 60 KB

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