rx.c 59 KB

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