rx.c 57 KB

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