rx.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  5. *
  6. * Portions of this file are derived from the ipw3945 project, as well
  7. * as portions of the ieee80211 subsystem header files.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  21. *
  22. * The full GNU General Public License is included in this distribution in the
  23. * file called LICENSE.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <ilw@linux.intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. *****************************************************************************/
  30. #include <linux/sched.h>
  31. #include <linux/wait.h>
  32. #include <linux/gfp.h>
  33. #include "iwl-prph.h"
  34. #include "iwl-io.h"
  35. #include "internal.h"
  36. #include "iwl-op-mode.h"
  37. /******************************************************************************
  38. *
  39. * RX path functions
  40. *
  41. ******************************************************************************/
  42. /*
  43. * Rx theory of operation
  44. *
  45. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  46. * each of which point to Receive Buffers to be filled by the NIC. These get
  47. * used not only for Rx frames, but for any command response or notification
  48. * from the NIC. The driver and NIC manage the Rx buffers by means
  49. * of indexes into the circular buffer.
  50. *
  51. * Rx Queue Indexes
  52. * The host/firmware share two index registers for managing the Rx buffers.
  53. *
  54. * The READ index maps to the first position that the firmware may be writing
  55. * to -- the driver can read up to (but not including) this position and get
  56. * good data.
  57. * The READ index is managed by the firmware once the card is enabled.
  58. *
  59. * The WRITE index maps to the last position the driver has read from -- the
  60. * position preceding WRITE is the last slot the firmware can place a packet.
  61. *
  62. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  63. * WRITE = READ.
  64. *
  65. * During initialization, the host sets up the READ queue position to the first
  66. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  67. *
  68. * When the firmware places a packet in a buffer, it will advance the READ index
  69. * and fire the RX interrupt. The driver can then query the READ index and
  70. * process as many packets as possible, moving the WRITE index forward as it
  71. * resets the Rx queue buffers with new memory.
  72. *
  73. * The management in the driver is as follows:
  74. * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
  75. * When the interrupt handler is called, the request is processed.
  76. * The page is either stolen - transferred to the upper layer
  77. * or reused - added immediately to the iwl->rxq->rx_free list.
  78. * + When the page is stolen - the driver updates the matching queue's used
  79. * count, detaches the RBD and transfers it to the queue used list.
  80. * When there are two used RBDs - they are transferred to the allocator empty
  81. * list. Work is then scheduled for the allocator to start allocating
  82. * eight buffers.
  83. * When there are another 6 used RBDs - they are transferred to the allocator
  84. * empty list and the driver tries to claim the pre-allocated buffers and
  85. * add them to iwl->rxq->rx_free. If it fails - it continues to claim them
  86. * until ready.
  87. * When there are 8+ buffers in the free list - either from allocation or from
  88. * 8 reused unstolen pages - restock is called to update the FW and indexes.
  89. * + In order to make sure the allocator always has RBDs to use for allocation
  90. * the allocator has initial pool in the size of num_queues*(8-2) - the
  91. * maximum missing RBDs per allocation request (request posted with 2
  92. * empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
  93. * The queues supplies the recycle of the rest of the RBDs.
  94. * + A received packet is processed and handed to the kernel network stack,
  95. * detached from the iwl->rxq. The driver 'processed' index is updated.
  96. * + If there are no allocated buffers in iwl->rxq->rx_free,
  97. * the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
  98. * If there were enough free buffers and RX_STALLED is set it is cleared.
  99. *
  100. *
  101. * Driver sequence:
  102. *
  103. * iwl_rxq_alloc() Allocates rx_free
  104. * iwl_pcie_rx_replenish() Replenishes rx_free list from rx_used, and calls
  105. * iwl_pcie_rxq_restock.
  106. * Used only during initialization.
  107. * iwl_pcie_rxq_restock() Moves available buffers from rx_free into Rx
  108. * queue, updates firmware pointers, and updates
  109. * the WRITE index.
  110. * iwl_pcie_rx_allocator() Background work for allocating pages.
  111. *
  112. * -- enable interrupts --
  113. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  114. * READ INDEX, detaching the SKB from the pool.
  115. * Moves the packet buffer from queue to rx_used.
  116. * Posts and claims requests to the allocator.
  117. * Calls iwl_pcie_rxq_restock to refill any empty
  118. * slots.
  119. *
  120. * RBD life-cycle:
  121. *
  122. * Init:
  123. * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
  124. *
  125. * Regular Receive interrupt:
  126. * Page Stolen:
  127. * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
  128. * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
  129. * Page not Stolen:
  130. * rxq.queue -> rxq.rx_free -> rxq.queue
  131. * ...
  132. *
  133. */
  134. /*
  135. * iwl_rxq_space - Return number of free slots available in queue.
  136. */
  137. static int iwl_rxq_space(const struct iwl_rxq *rxq)
  138. {
  139. /* Make sure RX_QUEUE_SIZE is a power of 2 */
  140. BUILD_BUG_ON(RX_QUEUE_SIZE & (RX_QUEUE_SIZE - 1));
  141. /*
  142. * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
  143. * between empty and completely full queues.
  144. * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
  145. * defined for negative dividends.
  146. */
  147. return (rxq->read - rxq->write - 1) & (RX_QUEUE_SIZE - 1);
  148. }
  149. /*
  150. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  151. */
  152. static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
  153. {
  154. return cpu_to_le32((u32)(dma_addr >> 8));
  155. }
  156. /*
  157. * iwl_pcie_rx_stop - stops the Rx DMA
  158. */
  159. int iwl_pcie_rx_stop(struct iwl_trans *trans)
  160. {
  161. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  162. return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
  163. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  164. }
  165. /*
  166. * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
  167. */
  168. static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans)
  169. {
  170. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  171. struct iwl_rxq *rxq = &trans_pcie->rxq;
  172. u32 reg;
  173. lockdep_assert_held(&rxq->lock);
  174. /*
  175. * explicitly wake up the NIC if:
  176. * 1. shadow registers aren't enabled
  177. * 2. there is a chance that the NIC is asleep
  178. */
  179. if (!trans->cfg->base_params->shadow_reg_enable &&
  180. test_bit(STATUS_TPOWER_PMI, &trans->status)) {
  181. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  182. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  183. IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
  184. reg);
  185. iwl_set_bit(trans, CSR_GP_CNTRL,
  186. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  187. rxq->need_update = true;
  188. return;
  189. }
  190. }
  191. rxq->write_actual = round_down(rxq->write, 8);
  192. iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
  193. }
  194. static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
  195. {
  196. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  197. struct iwl_rxq *rxq = &trans_pcie->rxq;
  198. spin_lock(&rxq->lock);
  199. if (!rxq->need_update)
  200. goto exit_unlock;
  201. iwl_pcie_rxq_inc_wr_ptr(trans);
  202. rxq->need_update = false;
  203. exit_unlock:
  204. spin_unlock(&rxq->lock);
  205. }
  206. /*
  207. * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
  208. *
  209. * If there are slots in the RX queue that need to be restocked,
  210. * and we have free pre-allocated buffers, fill the ranks as much
  211. * as we can, pulling from rx_free.
  212. *
  213. * This moves the 'write' index forward to catch up with 'processed', and
  214. * also updates the memory address in the firmware to reference the new
  215. * target buffer.
  216. */
  217. static void iwl_pcie_rxq_restock(struct iwl_trans *trans)
  218. {
  219. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  220. struct iwl_rxq *rxq = &trans_pcie->rxq;
  221. struct iwl_rx_mem_buffer *rxb;
  222. /*
  223. * If the device isn't enabled - not need to try to add buffers...
  224. * This can happen when we stop the device and still have an interrupt
  225. * pending. We stop the APM before we sync the interrupts because we
  226. * have to (see comment there). On the other hand, since the APM is
  227. * stopped, we cannot access the HW (in particular not prph).
  228. * So don't try to restock if the APM has been already stopped.
  229. */
  230. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  231. return;
  232. spin_lock(&rxq->lock);
  233. while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
  234. /* The overwritten rxb must be a used one */
  235. rxb = rxq->queue[rxq->write];
  236. BUG_ON(rxb && rxb->page);
  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. /* Point to Rx buffer via next RBD in circular buffer */
  242. rxq->bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
  243. rxq->queue[rxq->write] = rxb;
  244. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  245. rxq->free_count--;
  246. }
  247. spin_unlock(&rxq->lock);
  248. /* If we've added more space for the firmware to place data, tell it.
  249. * Increment device's write pointer in multiples of 8. */
  250. if (rxq->write_actual != (rxq->write & ~0x7)) {
  251. spin_lock(&rxq->lock);
  252. iwl_pcie_rxq_inc_wr_ptr(trans);
  253. spin_unlock(&rxq->lock);
  254. }
  255. }
  256. /*
  257. * iwl_pcie_rx_alloc_page - allocates and returns a page.
  258. *
  259. */
  260. static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans,
  261. gfp_t priority)
  262. {
  263. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  264. struct iwl_rxq *rxq = &trans_pcie->rxq;
  265. struct page *page;
  266. gfp_t gfp_mask = priority;
  267. if (rxq->free_count > RX_LOW_WATERMARK)
  268. gfp_mask |= __GFP_NOWARN;
  269. if (trans_pcie->rx_page_order > 0)
  270. gfp_mask |= __GFP_COMP;
  271. /* Alloc a new receive buffer */
  272. page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
  273. if (!page) {
  274. if (net_ratelimit())
  275. IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
  276. trans_pcie->rx_page_order);
  277. /* Issue an error if the hardware has consumed more than half
  278. * of its free buffer list and we don't have enough
  279. * pre-allocated buffers.
  280. ` */
  281. if (rxq->free_count <= RX_LOW_WATERMARK &&
  282. iwl_rxq_space(rxq) > (RX_QUEUE_SIZE / 2) &&
  283. net_ratelimit())
  284. IWL_CRIT(trans,
  285. "Failed to alloc_pages with GFP_KERNEL. Only %u free buffers remaining.\n",
  286. rxq->free_count);
  287. return NULL;
  288. }
  289. return page;
  290. }
  291. /*
  292. * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
  293. *
  294. * A used RBD is an Rx buffer that has been given to the stack. To use it again
  295. * a page must be allocated and the RBD must point to the page. This function
  296. * doesn't change the HW pointer but handles the list of pages that is used by
  297. * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
  298. * allocated buffers.
  299. */
  300. static void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority)
  301. {
  302. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  303. struct iwl_rxq *rxq = &trans_pcie->rxq;
  304. struct iwl_rx_mem_buffer *rxb;
  305. struct page *page;
  306. while (1) {
  307. spin_lock(&rxq->lock);
  308. if (list_empty(&rxq->rx_used)) {
  309. spin_unlock(&rxq->lock);
  310. return;
  311. }
  312. spin_unlock(&rxq->lock);
  313. /* Alloc a new receive buffer */
  314. page = iwl_pcie_rx_alloc_page(trans, priority);
  315. if (!page)
  316. return;
  317. spin_lock(&rxq->lock);
  318. if (list_empty(&rxq->rx_used)) {
  319. spin_unlock(&rxq->lock);
  320. __free_pages(page, trans_pcie->rx_page_order);
  321. return;
  322. }
  323. rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
  324. list);
  325. list_del(&rxb->list);
  326. spin_unlock(&rxq->lock);
  327. BUG_ON(rxb->page);
  328. rxb->page = page;
  329. /* Get physical address of the RB */
  330. rxb->page_dma =
  331. dma_map_page(trans->dev, page, 0,
  332. PAGE_SIZE << trans_pcie->rx_page_order,
  333. DMA_FROM_DEVICE);
  334. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  335. rxb->page = NULL;
  336. spin_lock(&rxq->lock);
  337. list_add(&rxb->list, &rxq->rx_used);
  338. spin_unlock(&rxq->lock);
  339. __free_pages(page, trans_pcie->rx_page_order);
  340. return;
  341. }
  342. /* dma address must be no more than 36 bits */
  343. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  344. /* and also 256 byte aligned! */
  345. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  346. spin_lock(&rxq->lock);
  347. list_add_tail(&rxb->list, &rxq->rx_free);
  348. rxq->free_count++;
  349. spin_unlock(&rxq->lock);
  350. }
  351. }
  352. static void iwl_pcie_rxq_free_rbs(struct iwl_trans *trans)
  353. {
  354. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  355. struct iwl_rxq *rxq = &trans_pcie->rxq;
  356. int i;
  357. lockdep_assert_held(&rxq->lock);
  358. for (i = 0; i < RX_QUEUE_SIZE; i++) {
  359. if (!rxq->pool[i].page)
  360. continue;
  361. dma_unmap_page(trans->dev, rxq->pool[i].page_dma,
  362. PAGE_SIZE << trans_pcie->rx_page_order,
  363. DMA_FROM_DEVICE);
  364. __free_pages(rxq->pool[i].page, trans_pcie->rx_page_order);
  365. rxq->pool[i].page = NULL;
  366. }
  367. }
  368. /*
  369. * iwl_pcie_rx_replenish - Move all used buffers from rx_used to rx_free
  370. *
  371. * When moving to rx_free an page is allocated for the slot.
  372. *
  373. * Also restock the Rx queue via iwl_pcie_rxq_restock.
  374. * This is called only during initialization
  375. */
  376. static void iwl_pcie_rx_replenish(struct iwl_trans *trans)
  377. {
  378. iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL);
  379. iwl_pcie_rxq_restock(trans);
  380. }
  381. /*
  382. * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
  383. *
  384. * Allocates for each received request 8 pages
  385. * Called as a scheduled work item.
  386. */
  387. static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
  388. {
  389. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  390. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  391. struct list_head local_empty;
  392. int pending = atomic_xchg(&rba->req_pending, 0);
  393. IWL_DEBUG_RX(trans, "Pending allocation requests = %d\n", pending);
  394. /* If we were scheduled - there is at least one request */
  395. spin_lock(&rba->lock);
  396. /* swap out the rba->rbd_empty to a local list */
  397. list_replace_init(&rba->rbd_empty, &local_empty);
  398. spin_unlock(&rba->lock);
  399. while (pending) {
  400. int i;
  401. struct list_head local_allocated;
  402. INIT_LIST_HEAD(&local_allocated);
  403. for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
  404. struct iwl_rx_mem_buffer *rxb;
  405. struct page *page;
  406. /* List should never be empty - each reused RBD is
  407. * returned to the list, and initial pool covers any
  408. * possible gap between the time the page is allocated
  409. * to the time the RBD is added.
  410. */
  411. BUG_ON(list_empty(&local_empty));
  412. /* Get the first rxb from the rbd list */
  413. rxb = list_first_entry(&local_empty,
  414. struct iwl_rx_mem_buffer, list);
  415. BUG_ON(rxb->page);
  416. /* Alloc a new receive buffer */
  417. page = iwl_pcie_rx_alloc_page(trans, GFP_KERNEL);
  418. if (!page)
  419. continue;
  420. rxb->page = page;
  421. /* Get physical address of the RB */
  422. rxb->page_dma = dma_map_page(trans->dev, page, 0,
  423. PAGE_SIZE << trans_pcie->rx_page_order,
  424. DMA_FROM_DEVICE);
  425. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  426. rxb->page = NULL;
  427. __free_pages(page, trans_pcie->rx_page_order);
  428. continue;
  429. }
  430. /* dma address must be no more than 36 bits */
  431. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  432. /* and also 256 byte aligned! */
  433. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  434. /* move the allocated entry to the out list */
  435. list_move(&rxb->list, &local_allocated);
  436. i++;
  437. }
  438. pending--;
  439. if (!pending) {
  440. pending = atomic_xchg(&rba->req_pending, 0);
  441. IWL_DEBUG_RX(trans,
  442. "Pending allocation requests = %d\n",
  443. pending);
  444. }
  445. spin_lock(&rba->lock);
  446. /* add the allocated rbds to the allocator allocated list */
  447. list_splice_tail(&local_allocated, &rba->rbd_allocated);
  448. /* get more empty RBDs for current pending requests */
  449. list_splice_tail_init(&rba->rbd_empty, &local_empty);
  450. spin_unlock(&rba->lock);
  451. atomic_inc(&rba->req_ready);
  452. }
  453. spin_lock(&rba->lock);
  454. /* return unused rbds to the allocator empty list */
  455. list_splice_tail(&local_empty, &rba->rbd_empty);
  456. spin_unlock(&rba->lock);
  457. }
  458. /*
  459. * iwl_pcie_rx_allocator_get - Returns the pre-allocated pages
  460. .*
  461. .* Called by queue when the queue posted allocation request and
  462. * has freed 8 RBDs in order to restock itself.
  463. */
  464. static int iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
  465. struct iwl_rx_mem_buffer
  466. *out[RX_CLAIM_REQ_ALLOC])
  467. {
  468. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  469. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  470. int i;
  471. /*
  472. * atomic_dec_if_positive returns req_ready - 1 for any scenario.
  473. * If req_ready is 0 atomic_dec_if_positive will return -1 and this
  474. * function will return -ENOMEM, as there are no ready requests.
  475. * atomic_dec_if_positive will perofrm the *actual* decrement only if
  476. * req_ready > 0, i.e. - there are ready requests and the function
  477. * hands one request to the caller.
  478. */
  479. if (atomic_dec_if_positive(&rba->req_ready) < 0)
  480. return -ENOMEM;
  481. spin_lock(&rba->lock);
  482. for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
  483. /* Get next free Rx buffer, remove it from free list */
  484. out[i] = list_first_entry(&rba->rbd_allocated,
  485. struct iwl_rx_mem_buffer, list);
  486. list_del(&out[i]->list);
  487. }
  488. spin_unlock(&rba->lock);
  489. return 0;
  490. }
  491. static void iwl_pcie_rx_allocator_work(struct work_struct *data)
  492. {
  493. struct iwl_rb_allocator *rba_p =
  494. container_of(data, struct iwl_rb_allocator, rx_alloc);
  495. struct iwl_trans_pcie *trans_pcie =
  496. container_of(rba_p, struct iwl_trans_pcie, rba);
  497. iwl_pcie_rx_allocator(trans_pcie->trans);
  498. }
  499. static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
  500. {
  501. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  502. struct iwl_rxq *rxq = &trans_pcie->rxq;
  503. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  504. struct device *dev = trans->dev;
  505. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  506. spin_lock_init(&rxq->lock);
  507. spin_lock_init(&rba->lock);
  508. if (WARN_ON(rxq->bd || rxq->rb_stts))
  509. return -EINVAL;
  510. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  511. rxq->bd = dma_zalloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  512. &rxq->bd_dma, GFP_KERNEL);
  513. if (!rxq->bd)
  514. goto err_bd;
  515. /*Allocate the driver's pointer to receive buffer status */
  516. rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
  517. &rxq->rb_stts_dma, GFP_KERNEL);
  518. if (!rxq->rb_stts)
  519. goto err_rb_stts;
  520. return 0;
  521. err_rb_stts:
  522. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  523. rxq->bd, rxq->bd_dma);
  524. rxq->bd_dma = 0;
  525. rxq->bd = NULL;
  526. err_bd:
  527. return -ENOMEM;
  528. }
  529. static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
  530. {
  531. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  532. u32 rb_size;
  533. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  534. if (trans_pcie->rx_buf_size_8k)
  535. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  536. else
  537. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  538. /* Stop Rx DMA */
  539. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  540. /* reset and flush pointers */
  541. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
  542. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
  543. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
  544. /* Reset driver's Rx queue write index */
  545. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  546. /* Tell device where to find RBD circular buffer in DRAM */
  547. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  548. (u32)(rxq->bd_dma >> 8));
  549. /* Tell device where in DRAM to update its Rx status */
  550. iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  551. rxq->rb_stts_dma >> 4);
  552. /* Enable Rx DMA
  553. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  554. * the credit mechanism in 5000 HW RX FIFO
  555. * Direct rx interrupts to hosts
  556. * Rx buffer size 4 or 8k
  557. * RB timeout 0x10
  558. * 256 RBDs
  559. */
  560. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  561. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  562. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  563. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  564. rb_size|
  565. (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  566. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  567. /* Set interrupt coalescing timer to default (2048 usecs) */
  568. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  569. /* W/A for interrupt coalescing bug in 7260 and 3160 */
  570. if (trans->cfg->host_interrupt_operation_mode)
  571. iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
  572. }
  573. static void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
  574. {
  575. int i;
  576. lockdep_assert_held(&rxq->lock);
  577. INIT_LIST_HEAD(&rxq->rx_free);
  578. INIT_LIST_HEAD(&rxq->rx_used);
  579. rxq->free_count = 0;
  580. rxq->used_count = 0;
  581. for (i = 0; i < RX_QUEUE_SIZE; i++)
  582. list_add(&rxq->pool[i].list, &rxq->rx_used);
  583. }
  584. static void iwl_pcie_rx_init_rba(struct iwl_rb_allocator *rba)
  585. {
  586. int i;
  587. lockdep_assert_held(&rba->lock);
  588. INIT_LIST_HEAD(&rba->rbd_allocated);
  589. INIT_LIST_HEAD(&rba->rbd_empty);
  590. for (i = 0; i < RX_POOL_SIZE; i++)
  591. list_add(&rba->pool[i].list, &rba->rbd_empty);
  592. }
  593. static void iwl_pcie_rx_free_rba(struct iwl_trans *trans)
  594. {
  595. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  596. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  597. int i;
  598. lockdep_assert_held(&rba->lock);
  599. for (i = 0; i < RX_POOL_SIZE; i++) {
  600. if (!rba->pool[i].page)
  601. continue;
  602. dma_unmap_page(trans->dev, rba->pool[i].page_dma,
  603. PAGE_SIZE << trans_pcie->rx_page_order,
  604. DMA_FROM_DEVICE);
  605. __free_pages(rba->pool[i].page, trans_pcie->rx_page_order);
  606. rba->pool[i].page = NULL;
  607. }
  608. }
  609. int iwl_pcie_rx_init(struct iwl_trans *trans)
  610. {
  611. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  612. struct iwl_rxq *rxq = &trans_pcie->rxq;
  613. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  614. int i, err;
  615. if (!rxq->bd) {
  616. err = iwl_pcie_rx_alloc(trans);
  617. if (err)
  618. return err;
  619. }
  620. if (!rba->alloc_wq)
  621. rba->alloc_wq = alloc_workqueue("rb_allocator",
  622. WQ_HIGHPRI | WQ_UNBOUND, 1);
  623. INIT_WORK(&rba->rx_alloc, iwl_pcie_rx_allocator_work);
  624. cancel_work_sync(&rba->rx_alloc);
  625. spin_lock(&rba->lock);
  626. atomic_set(&rba->req_pending, 0);
  627. atomic_set(&rba->req_ready, 0);
  628. /* free all first - we might be reconfigured for a different size */
  629. iwl_pcie_rx_free_rba(trans);
  630. iwl_pcie_rx_init_rba(rba);
  631. spin_unlock(&rba->lock);
  632. spin_lock(&rxq->lock);
  633. /* free all first - we might be reconfigured for a different size */
  634. iwl_pcie_rxq_free_rbs(trans);
  635. iwl_pcie_rx_init_rxb_lists(rxq);
  636. for (i = 0; i < RX_QUEUE_SIZE; i++)
  637. rxq->queue[i] = NULL;
  638. /* Set us so that we have processed and used all buffers, but have
  639. * not restocked the Rx queue with fresh buffers */
  640. rxq->read = rxq->write = 0;
  641. rxq->write_actual = 0;
  642. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  643. spin_unlock(&rxq->lock);
  644. iwl_pcie_rx_replenish(trans);
  645. iwl_pcie_rx_hw_init(trans, rxq);
  646. spin_lock(&rxq->lock);
  647. iwl_pcie_rxq_inc_wr_ptr(trans);
  648. spin_unlock(&rxq->lock);
  649. return 0;
  650. }
  651. void iwl_pcie_rx_free(struct iwl_trans *trans)
  652. {
  653. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  654. struct iwl_rxq *rxq = &trans_pcie->rxq;
  655. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  656. /*if rxq->bd is NULL, it means that nothing has been allocated,
  657. * exit now */
  658. if (!rxq->bd) {
  659. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  660. return;
  661. }
  662. cancel_work_sync(&rba->rx_alloc);
  663. if (rba->alloc_wq) {
  664. destroy_workqueue(rba->alloc_wq);
  665. rba->alloc_wq = NULL;
  666. }
  667. spin_lock(&rba->lock);
  668. iwl_pcie_rx_free_rba(trans);
  669. spin_unlock(&rba->lock);
  670. spin_lock(&rxq->lock);
  671. iwl_pcie_rxq_free_rbs(trans);
  672. spin_unlock(&rxq->lock);
  673. dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  674. rxq->bd, rxq->bd_dma);
  675. rxq->bd_dma = 0;
  676. rxq->bd = NULL;
  677. if (rxq->rb_stts)
  678. dma_free_coherent(trans->dev,
  679. sizeof(struct iwl_rb_status),
  680. rxq->rb_stts, rxq->rb_stts_dma);
  681. else
  682. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  683. rxq->rb_stts_dma = 0;
  684. rxq->rb_stts = NULL;
  685. }
  686. /*
  687. * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
  688. *
  689. * Called when a RBD can be reused. The RBD is transferred to the allocator.
  690. * When there are 2 empty RBDs - a request for allocation is posted
  691. */
  692. static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
  693. struct iwl_rx_mem_buffer *rxb,
  694. struct iwl_rxq *rxq, bool emergency)
  695. {
  696. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  697. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  698. /* Move the RBD to the used list, will be moved to allocator in batches
  699. * before claiming or posting a request*/
  700. list_add_tail(&rxb->list, &rxq->rx_used);
  701. if (unlikely(emergency))
  702. return;
  703. /* Count the allocator owned RBDs */
  704. rxq->used_count++;
  705. /* If we have RX_POST_REQ_ALLOC new released rx buffers -
  706. * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
  707. * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
  708. * after but we still need to post another request.
  709. */
  710. if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
  711. /* Move the 2 RBDs to the allocator ownership.
  712. Allocator has another 6 from pool for the request completion*/
  713. spin_lock(&rba->lock);
  714. list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
  715. spin_unlock(&rba->lock);
  716. atomic_inc(&rba->req_pending);
  717. queue_work(rba->alloc_wq, &rba->rx_alloc);
  718. }
  719. }
  720. static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
  721. struct iwl_rx_mem_buffer *rxb,
  722. bool emergency)
  723. {
  724. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  725. struct iwl_rxq *rxq = &trans_pcie->rxq;
  726. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  727. bool page_stolen = false;
  728. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  729. u32 offset = 0;
  730. if (WARN_ON(!rxb))
  731. return;
  732. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  733. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  734. struct iwl_rx_packet *pkt;
  735. u16 sequence;
  736. bool reclaim;
  737. int index, cmd_index, len;
  738. struct iwl_rx_cmd_buffer rxcb = {
  739. ._offset = offset,
  740. ._rx_page_order = trans_pcie->rx_page_order,
  741. ._page = rxb->page,
  742. ._page_stolen = false,
  743. .truesize = max_len,
  744. };
  745. pkt = rxb_addr(&rxcb);
  746. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  747. break;
  748. IWL_DEBUG_RX(trans,
  749. "cmd at offset %d: %s (0x%.2x, seq 0x%x)\n",
  750. rxcb._offset,
  751. get_cmd_string(trans_pcie, pkt->hdr.cmd),
  752. pkt->hdr.cmd, le16_to_cpu(pkt->hdr.sequence));
  753. len = iwl_rx_packet_len(pkt);
  754. len += sizeof(u32); /* account for status word */
  755. trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
  756. trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
  757. /* Reclaim a command buffer only if this packet is a response
  758. * to a (driver-originated) command.
  759. * If the packet (e.g. Rx frame) originated from uCode,
  760. * there is no command buffer to reclaim.
  761. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  762. * but apparently a few don't get set; catch them here. */
  763. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  764. if (reclaim) {
  765. int i;
  766. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  767. if (trans_pcie->no_reclaim_cmds[i] ==
  768. pkt->hdr.cmd) {
  769. reclaim = false;
  770. break;
  771. }
  772. }
  773. }
  774. sequence = le16_to_cpu(pkt->hdr.sequence);
  775. index = SEQ_TO_INDEX(sequence);
  776. cmd_index = get_cmd_index(&txq->q, index);
  777. iwl_op_mode_rx(trans->op_mode, &trans_pcie->napi, &rxcb);
  778. if (reclaim) {
  779. kzfree(txq->entries[cmd_index].free_buf);
  780. txq->entries[cmd_index].free_buf = NULL;
  781. }
  782. /*
  783. * After here, we should always check rxcb._page_stolen,
  784. * if it is true then one of the handlers took the page.
  785. */
  786. if (reclaim) {
  787. /* Invoke any callbacks, transfer the buffer to caller,
  788. * and fire off the (possibly) blocking
  789. * iwl_trans_send_cmd()
  790. * as we reclaim the driver command queue */
  791. if (!rxcb._page_stolen)
  792. iwl_pcie_hcmd_complete(trans, &rxcb);
  793. else
  794. IWL_WARN(trans, "Claim null rxb?\n");
  795. }
  796. page_stolen |= rxcb._page_stolen;
  797. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  798. }
  799. /* page was stolen from us -- free our reference */
  800. if (page_stolen) {
  801. __free_pages(rxb->page, trans_pcie->rx_page_order);
  802. rxb->page = NULL;
  803. }
  804. /* Reuse the page if possible. For notification packets and
  805. * SKBs that fail to Rx correctly, add them back into the
  806. * rx_free list for reuse later. */
  807. if (rxb->page != NULL) {
  808. rxb->page_dma =
  809. dma_map_page(trans->dev, rxb->page, 0,
  810. PAGE_SIZE << trans_pcie->rx_page_order,
  811. DMA_FROM_DEVICE);
  812. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  813. /*
  814. * free the page(s) as well to not break
  815. * the invariant that the items on the used
  816. * list have no page(s)
  817. */
  818. __free_pages(rxb->page, trans_pcie->rx_page_order);
  819. rxb->page = NULL;
  820. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  821. } else {
  822. list_add_tail(&rxb->list, &rxq->rx_free);
  823. rxq->free_count++;
  824. }
  825. } else
  826. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  827. }
  828. /*
  829. * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
  830. */
  831. static void iwl_pcie_rx_handle(struct iwl_trans *trans)
  832. {
  833. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  834. struct iwl_rxq *rxq = &trans_pcie->rxq;
  835. u32 r, i, j, count = 0;
  836. bool emergency = false;
  837. restart:
  838. spin_lock(&rxq->lock);
  839. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  840. * buffer that the driver may process (last buffer filled by ucode). */
  841. r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
  842. i = rxq->read;
  843. /* Rx interrupt, but nothing sent from uCode */
  844. if (i == r)
  845. IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
  846. while (i != r) {
  847. struct iwl_rx_mem_buffer *rxb;
  848. if (unlikely(rxq->used_count == RX_QUEUE_SIZE / 2))
  849. emergency = true;
  850. rxb = rxq->queue[i];
  851. rxq->queue[i] = NULL;
  852. IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d (%p)\n",
  853. r, i, rxb);
  854. iwl_pcie_rx_handle_rb(trans, rxb, emergency);
  855. i = (i + 1) & RX_QUEUE_MASK;
  856. /* If we have RX_CLAIM_REQ_ALLOC released rx buffers -
  857. * try to claim the pre-allocated buffers from the allocator */
  858. if (rxq->used_count >= RX_CLAIM_REQ_ALLOC) {
  859. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  860. struct iwl_rx_mem_buffer *out[RX_CLAIM_REQ_ALLOC];
  861. if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 &&
  862. !emergency) {
  863. /* Add the remaining 6 empty RBDs
  864. * for allocator use
  865. */
  866. spin_lock(&rba->lock);
  867. list_splice_tail_init(&rxq->rx_used,
  868. &rba->rbd_empty);
  869. spin_unlock(&rba->lock);
  870. }
  871. /* If not ready - continue, will try to reclaim later.
  872. * No need to reschedule work - allocator exits only on
  873. * success */
  874. if (!iwl_pcie_rx_allocator_get(trans, out)) {
  875. /* If success - then RX_CLAIM_REQ_ALLOC
  876. * buffers were retrieved and should be added
  877. * to free list */
  878. rxq->used_count -= RX_CLAIM_REQ_ALLOC;
  879. for (j = 0; j < RX_CLAIM_REQ_ALLOC; j++) {
  880. list_add_tail(&out[j]->list,
  881. &rxq->rx_free);
  882. rxq->free_count++;
  883. }
  884. }
  885. }
  886. if (emergency) {
  887. count++;
  888. if (count == 8) {
  889. count = 0;
  890. if (rxq->used_count < RX_QUEUE_SIZE / 3)
  891. emergency = false;
  892. spin_unlock(&rxq->lock);
  893. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC);
  894. spin_lock(&rxq->lock);
  895. }
  896. }
  897. /* handle restock for three cases, can be all of them at once:
  898. * - we just pulled buffers from the allocator
  899. * - we have 8+ unstolen pages accumulated
  900. * - we are in emergency and allocated buffers
  901. */
  902. if (rxq->free_count >= RX_CLAIM_REQ_ALLOC) {
  903. rxq->read = i;
  904. spin_unlock(&rxq->lock);
  905. iwl_pcie_rxq_restock(trans);
  906. goto restart;
  907. }
  908. }
  909. /* Backtrack one entry */
  910. rxq->read = i;
  911. spin_unlock(&rxq->lock);
  912. /*
  913. * handle a case where in emergency there are some unallocated RBDs.
  914. * those RBDs are in the used list, but are not tracked by the queue's
  915. * used_count which counts allocator owned RBDs.
  916. * unallocated emergency RBDs must be allocated on exit, otherwise
  917. * when called again the function may not be in emergency mode and
  918. * they will be handed to the allocator with no tracking in the RBD
  919. * allocator counters, which will lead to them never being claimed back
  920. * by the queue.
  921. * by allocating them here, they are now in the queue free list, and
  922. * will be restocked by the next call of iwl_pcie_rxq_restock.
  923. */
  924. if (unlikely(emergency && count))
  925. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC);
  926. if (trans_pcie->napi.poll)
  927. napi_gro_flush(&trans_pcie->napi, false);
  928. }
  929. /*
  930. * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
  931. */
  932. static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
  933. {
  934. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  935. int i;
  936. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  937. if (trans->cfg->internal_wimax_coex &&
  938. !trans->cfg->apmg_not_supported &&
  939. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  940. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  941. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  942. APMG_PS_CTRL_VAL_RESET_REQ))) {
  943. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  944. iwl_op_mode_wimax_active(trans->op_mode);
  945. wake_up(&trans_pcie->wait_command_queue);
  946. return;
  947. }
  948. iwl_pcie_dump_csr(trans);
  949. iwl_dump_fh(trans, NULL);
  950. local_bh_disable();
  951. /* The STATUS_FW_ERROR bit is set in this function. This must happen
  952. * before we wake up the command caller, to ensure a proper cleanup. */
  953. iwl_trans_fw_error(trans);
  954. local_bh_enable();
  955. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  956. del_timer(&trans_pcie->txq[i].stuck_timer);
  957. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  958. wake_up(&trans_pcie->wait_command_queue);
  959. }
  960. static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
  961. {
  962. u32 inta;
  963. lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
  964. trace_iwlwifi_dev_irq(trans->dev);
  965. /* Discover which interrupts are active/pending */
  966. inta = iwl_read32(trans, CSR_INT);
  967. /* the thread will service interrupts and re-enable them */
  968. return inta;
  969. }
  970. /* a device (PCI-E) page is 4096 bytes long */
  971. #define ICT_SHIFT 12
  972. #define ICT_SIZE (1 << ICT_SHIFT)
  973. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  974. /* interrupt handler using ict table, with this interrupt driver will
  975. * stop using INTA register to get device's interrupt, reading this register
  976. * is expensive, device will write interrupts in ICT dram table, increment
  977. * index then will fire interrupt to driver, driver will OR all ICT table
  978. * entries from current index up to table entry with 0 value. the result is
  979. * the interrupt we need to service, driver will set the entries back to 0 and
  980. * set index.
  981. */
  982. static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
  983. {
  984. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  985. u32 inta;
  986. u32 val = 0;
  987. u32 read;
  988. trace_iwlwifi_dev_irq(trans->dev);
  989. /* Ignore interrupt if there's nothing in NIC to service.
  990. * This may be due to IRQ shared with another device,
  991. * or due to sporadic interrupts thrown from our NIC. */
  992. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  993. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  994. if (!read)
  995. return 0;
  996. /*
  997. * Collect all entries up to the first 0, starting from ict_index;
  998. * note we already read at ict_index.
  999. */
  1000. do {
  1001. val |= read;
  1002. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1003. trans_pcie->ict_index, read);
  1004. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1005. trans_pcie->ict_index =
  1006. ((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
  1007. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1008. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1009. read);
  1010. } while (read);
  1011. /* We should not get this value, just ignore it. */
  1012. if (val == 0xffffffff)
  1013. val = 0;
  1014. /*
  1015. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1016. * (bit 15 before shifting it to 31) to clear when using interrupt
  1017. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1018. * so we use them to decide on the real state of the Rx bit.
  1019. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1020. */
  1021. if (val & 0xC0000)
  1022. val |= 0x8000;
  1023. inta = (0xff & val) | ((0xff00 & val) << 16);
  1024. return inta;
  1025. }
  1026. irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
  1027. {
  1028. struct iwl_trans *trans = dev_id;
  1029. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1030. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1031. u32 inta = 0;
  1032. u32 handled = 0;
  1033. lock_map_acquire(&trans->sync_cmd_lockdep_map);
  1034. spin_lock(&trans_pcie->irq_lock);
  1035. /* dram interrupt table not set yet,
  1036. * use legacy interrupt.
  1037. */
  1038. if (likely(trans_pcie->use_ict))
  1039. inta = iwl_pcie_int_cause_ict(trans);
  1040. else
  1041. inta = iwl_pcie_int_cause_non_ict(trans);
  1042. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1043. IWL_DEBUG_ISR(trans,
  1044. "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
  1045. inta, trans_pcie->inta_mask,
  1046. iwl_read32(trans, CSR_INT_MASK),
  1047. iwl_read32(trans, CSR_FH_INT_STATUS));
  1048. if (inta & (~trans_pcie->inta_mask))
  1049. IWL_DEBUG_ISR(trans,
  1050. "We got a masked interrupt (0x%08x)\n",
  1051. inta & (~trans_pcie->inta_mask));
  1052. }
  1053. inta &= trans_pcie->inta_mask;
  1054. /*
  1055. * Ignore interrupt if there's nothing in NIC to service.
  1056. * This may be due to IRQ shared with another device,
  1057. * or due to sporadic interrupts thrown from our NIC.
  1058. */
  1059. if (unlikely(!inta)) {
  1060. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1061. /*
  1062. * Re-enable interrupts here since we don't
  1063. * have anything to service
  1064. */
  1065. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1066. iwl_enable_interrupts(trans);
  1067. spin_unlock(&trans_pcie->irq_lock);
  1068. lock_map_release(&trans->sync_cmd_lockdep_map);
  1069. return IRQ_NONE;
  1070. }
  1071. if (unlikely(inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1072. /*
  1073. * Hardware disappeared. It might have
  1074. * already raised an interrupt.
  1075. */
  1076. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1077. spin_unlock(&trans_pcie->irq_lock);
  1078. goto out;
  1079. }
  1080. /* Ack/clear/reset pending uCode interrupts.
  1081. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1082. */
  1083. /* There is a hardware bug in the interrupt mask function that some
  1084. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  1085. * they are disabled in the CSR_INT_MASK register. Furthermore the
  1086. * ICT interrupt handling mechanism has another bug that might cause
  1087. * these unmasked interrupts fail to be detected. We workaround the
  1088. * hardware bugs here by ACKing all the possible interrupts so that
  1089. * interrupt coalescing can still be achieved.
  1090. */
  1091. iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
  1092. if (iwl_have_debug_level(IWL_DL_ISR))
  1093. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  1094. inta, iwl_read32(trans, CSR_INT_MASK));
  1095. spin_unlock(&trans_pcie->irq_lock);
  1096. /* Now service all interrupt bits discovered above. */
  1097. if (inta & CSR_INT_BIT_HW_ERR) {
  1098. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  1099. /* Tell the device to stop sending interrupts */
  1100. iwl_disable_interrupts(trans);
  1101. isr_stats->hw++;
  1102. iwl_pcie_irq_handle_error(trans);
  1103. handled |= CSR_INT_BIT_HW_ERR;
  1104. goto out;
  1105. }
  1106. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1107. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1108. if (inta & CSR_INT_BIT_SCD) {
  1109. IWL_DEBUG_ISR(trans,
  1110. "Scheduler finished to transmit the frame/frames.\n");
  1111. isr_stats->sch++;
  1112. }
  1113. /* Alive notification via Rx interrupt will do the real work */
  1114. if (inta & CSR_INT_BIT_ALIVE) {
  1115. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  1116. isr_stats->alive++;
  1117. }
  1118. }
  1119. /* Safely ignore these bits for debug checks below */
  1120. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1121. /* HW RF KILL switch toggled */
  1122. if (inta & CSR_INT_BIT_RF_KILL) {
  1123. bool hw_rfkill;
  1124. hw_rfkill = iwl_is_rfkill_set(trans);
  1125. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  1126. hw_rfkill ? "disable radio" : "enable radio");
  1127. isr_stats->rfkill++;
  1128. mutex_lock(&trans_pcie->mutex);
  1129. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1130. mutex_unlock(&trans_pcie->mutex);
  1131. if (hw_rfkill) {
  1132. set_bit(STATUS_RFKILL, &trans->status);
  1133. if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
  1134. &trans->status))
  1135. IWL_DEBUG_RF_KILL(trans,
  1136. "Rfkill while SYNC HCMD in flight\n");
  1137. wake_up(&trans_pcie->wait_command_queue);
  1138. } else {
  1139. clear_bit(STATUS_RFKILL, &trans->status);
  1140. }
  1141. handled |= CSR_INT_BIT_RF_KILL;
  1142. }
  1143. /* Chip got too hot and stopped itself */
  1144. if (inta & CSR_INT_BIT_CT_KILL) {
  1145. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  1146. isr_stats->ctkill++;
  1147. handled |= CSR_INT_BIT_CT_KILL;
  1148. }
  1149. /* Error detected by uCode */
  1150. if (inta & CSR_INT_BIT_SW_ERR) {
  1151. IWL_ERR(trans, "Microcode SW error detected. "
  1152. " Restarting 0x%X.\n", inta);
  1153. isr_stats->sw++;
  1154. iwl_pcie_irq_handle_error(trans);
  1155. handled |= CSR_INT_BIT_SW_ERR;
  1156. }
  1157. /* uCode wakes up after power-down sleep */
  1158. if (inta & CSR_INT_BIT_WAKEUP) {
  1159. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  1160. iwl_pcie_rxq_check_wrptr(trans);
  1161. iwl_pcie_txq_check_wrptrs(trans);
  1162. isr_stats->wakeup++;
  1163. handled |= CSR_INT_BIT_WAKEUP;
  1164. }
  1165. /* All uCode command responses, including Tx command responses,
  1166. * Rx "responses" (frame-received notification), and other
  1167. * notifications from uCode come through here*/
  1168. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  1169. CSR_INT_BIT_RX_PERIODIC)) {
  1170. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  1171. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1172. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1173. iwl_write32(trans, CSR_FH_INT_STATUS,
  1174. CSR_FH_INT_RX_MASK);
  1175. }
  1176. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  1177. handled |= CSR_INT_BIT_RX_PERIODIC;
  1178. iwl_write32(trans,
  1179. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  1180. }
  1181. /* Sending RX interrupt require many steps to be done in the
  1182. * the device:
  1183. * 1- write interrupt to current index in ICT table.
  1184. * 2- dma RX frame.
  1185. * 3- update RX shared data to indicate last write index.
  1186. * 4- send interrupt.
  1187. * This could lead to RX race, driver could receive RX interrupt
  1188. * but the shared data changes does not reflect this;
  1189. * periodic interrupt will detect any dangling Rx activity.
  1190. */
  1191. /* Disable periodic interrupt; we use it as just a one-shot. */
  1192. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1193. CSR_INT_PERIODIC_DIS);
  1194. /*
  1195. * Enable periodic interrupt in 8 msec only if we received
  1196. * real RX interrupt (instead of just periodic int), to catch
  1197. * any dangling Rx interrupt. If it was just the periodic
  1198. * interrupt, there was no dangling Rx activity, and no need
  1199. * to extend the periodic interrupt; one-shot is enough.
  1200. */
  1201. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  1202. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1203. CSR_INT_PERIODIC_ENA);
  1204. isr_stats->rx++;
  1205. local_bh_disable();
  1206. iwl_pcie_rx_handle(trans);
  1207. local_bh_enable();
  1208. }
  1209. /* This "Tx" DMA channel is used only for loading uCode */
  1210. if (inta & CSR_INT_BIT_FH_TX) {
  1211. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  1212. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  1213. isr_stats->tx++;
  1214. handled |= CSR_INT_BIT_FH_TX;
  1215. /* Wake up uCode load routine, now that load is complete */
  1216. trans_pcie->ucode_write_complete = true;
  1217. wake_up(&trans_pcie->ucode_write_waitq);
  1218. }
  1219. if (inta & ~handled) {
  1220. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1221. isr_stats->unhandled++;
  1222. }
  1223. if (inta & ~(trans_pcie->inta_mask)) {
  1224. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  1225. inta & ~trans_pcie->inta_mask);
  1226. }
  1227. /* Re-enable all interrupts */
  1228. /* only Re-enable if disabled by irq */
  1229. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1230. iwl_enable_interrupts(trans);
  1231. /* Re-enable RF_KILL if it occurred */
  1232. else if (handled & CSR_INT_BIT_RF_KILL)
  1233. iwl_enable_rfkill_int(trans);
  1234. out:
  1235. lock_map_release(&trans->sync_cmd_lockdep_map);
  1236. return IRQ_HANDLED;
  1237. }
  1238. /******************************************************************************
  1239. *
  1240. * ICT functions
  1241. *
  1242. ******************************************************************************/
  1243. /* Free dram table */
  1244. void iwl_pcie_free_ict(struct iwl_trans *trans)
  1245. {
  1246. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1247. if (trans_pcie->ict_tbl) {
  1248. dma_free_coherent(trans->dev, ICT_SIZE,
  1249. trans_pcie->ict_tbl,
  1250. trans_pcie->ict_tbl_dma);
  1251. trans_pcie->ict_tbl = NULL;
  1252. trans_pcie->ict_tbl_dma = 0;
  1253. }
  1254. }
  1255. /*
  1256. * allocate dram shared table, it is an aligned memory
  1257. * block of ICT_SIZE.
  1258. * also reset all data related to ICT table interrupt.
  1259. */
  1260. int iwl_pcie_alloc_ict(struct iwl_trans *trans)
  1261. {
  1262. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1263. trans_pcie->ict_tbl =
  1264. dma_zalloc_coherent(trans->dev, ICT_SIZE,
  1265. &trans_pcie->ict_tbl_dma,
  1266. GFP_KERNEL);
  1267. if (!trans_pcie->ict_tbl)
  1268. return -ENOMEM;
  1269. /* just an API sanity check ... it is guaranteed to be aligned */
  1270. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  1271. iwl_pcie_free_ict(trans);
  1272. return -EINVAL;
  1273. }
  1274. IWL_DEBUG_ISR(trans, "ict dma addr %Lx ict vir addr %p\n",
  1275. (unsigned long long)trans_pcie->ict_tbl_dma,
  1276. trans_pcie->ict_tbl);
  1277. return 0;
  1278. }
  1279. /* Device is going up inform it about using ICT interrupt table,
  1280. * also we need to tell the driver to start using ICT interrupt.
  1281. */
  1282. void iwl_pcie_reset_ict(struct iwl_trans *trans)
  1283. {
  1284. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1285. u32 val;
  1286. if (!trans_pcie->ict_tbl)
  1287. return;
  1288. spin_lock(&trans_pcie->irq_lock);
  1289. iwl_disable_interrupts(trans);
  1290. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1291. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  1292. val |= CSR_DRAM_INT_TBL_ENABLE |
  1293. CSR_DRAM_INIT_TBL_WRAP_CHECK |
  1294. CSR_DRAM_INIT_TBL_WRITE_POINTER;
  1295. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  1296. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  1297. trans_pcie->use_ict = true;
  1298. trans_pcie->ict_index = 0;
  1299. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  1300. iwl_enable_interrupts(trans);
  1301. spin_unlock(&trans_pcie->irq_lock);
  1302. }
  1303. /* Device is going down disable ict interrupt usage */
  1304. void iwl_pcie_disable_ict(struct iwl_trans *trans)
  1305. {
  1306. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1307. spin_lock(&trans_pcie->irq_lock);
  1308. trans_pcie->use_ict = false;
  1309. spin_unlock(&trans_pcie->irq_lock);
  1310. }
  1311. irqreturn_t iwl_pcie_isr(int irq, void *data)
  1312. {
  1313. struct iwl_trans *trans = data;
  1314. if (!trans)
  1315. return IRQ_NONE;
  1316. /* Disable (but don't clear!) interrupts here to avoid
  1317. * back-to-back ISRs and sporadic interrupts from our NIC.
  1318. * If we have something to service, the tasklet will re-enable ints.
  1319. * If we *don't* have something, we'll re-enable before leaving here.
  1320. */
  1321. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1322. return IRQ_WAKE_THREAD;
  1323. }