rx.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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. spin_lock(&rba->lock);
  625. atomic_set(&rba->req_pending, 0);
  626. atomic_set(&rba->req_ready, 0);
  627. /* free all first - we might be reconfigured for a different size */
  628. iwl_pcie_rx_free_rba(trans);
  629. iwl_pcie_rx_init_rba(rba);
  630. spin_unlock(&rba->lock);
  631. spin_lock(&rxq->lock);
  632. /* free all first - we might be reconfigured for a different size */
  633. iwl_pcie_rxq_free_rbs(trans);
  634. iwl_pcie_rx_init_rxb_lists(rxq);
  635. for (i = 0; i < RX_QUEUE_SIZE; i++)
  636. rxq->queue[i] = NULL;
  637. /* Set us so that we have processed and used all buffers, but have
  638. * not restocked the Rx queue with fresh buffers */
  639. rxq->read = rxq->write = 0;
  640. rxq->write_actual = 0;
  641. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  642. spin_unlock(&rxq->lock);
  643. iwl_pcie_rx_replenish(trans);
  644. iwl_pcie_rx_hw_init(trans, rxq);
  645. spin_lock(&rxq->lock);
  646. iwl_pcie_rxq_inc_wr_ptr(trans);
  647. spin_unlock(&rxq->lock);
  648. return 0;
  649. }
  650. void iwl_pcie_rx_free(struct iwl_trans *trans)
  651. {
  652. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  653. struct iwl_rxq *rxq = &trans_pcie->rxq;
  654. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  655. /*if rxq->bd is NULL, it means that nothing has been allocated,
  656. * exit now */
  657. if (!rxq->bd) {
  658. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  659. return;
  660. }
  661. cancel_work_sync(&rba->rx_alloc);
  662. if (rba->alloc_wq) {
  663. destroy_workqueue(rba->alloc_wq);
  664. rba->alloc_wq = NULL;
  665. }
  666. spin_lock(&rba->lock);
  667. iwl_pcie_rx_free_rba(trans);
  668. spin_unlock(&rba->lock);
  669. spin_lock(&rxq->lock);
  670. iwl_pcie_rxq_free_rbs(trans);
  671. spin_unlock(&rxq->lock);
  672. dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  673. rxq->bd, rxq->bd_dma);
  674. rxq->bd_dma = 0;
  675. rxq->bd = NULL;
  676. if (rxq->rb_stts)
  677. dma_free_coherent(trans->dev,
  678. sizeof(struct iwl_rb_status),
  679. rxq->rb_stts, rxq->rb_stts_dma);
  680. else
  681. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  682. rxq->rb_stts_dma = 0;
  683. rxq->rb_stts = NULL;
  684. }
  685. /*
  686. * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
  687. *
  688. * Called when a RBD can be reused. The RBD is transferred to the allocator.
  689. * When there are 2 empty RBDs - a request for allocation is posted
  690. */
  691. static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
  692. struct iwl_rx_mem_buffer *rxb,
  693. struct iwl_rxq *rxq, bool emergency)
  694. {
  695. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  696. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  697. /* Move the RBD to the used list, will be moved to allocator in batches
  698. * before claiming or posting a request*/
  699. list_add_tail(&rxb->list, &rxq->rx_used);
  700. if (unlikely(emergency))
  701. return;
  702. /* Count the allocator owned RBDs */
  703. rxq->used_count++;
  704. /* If we have RX_POST_REQ_ALLOC new released rx buffers -
  705. * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
  706. * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
  707. * after but we still need to post another request.
  708. */
  709. if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
  710. /* Move the 2 RBDs to the allocator ownership.
  711. Allocator has another 6 from pool for the request completion*/
  712. spin_lock(&rba->lock);
  713. list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
  714. spin_unlock(&rba->lock);
  715. atomic_inc(&rba->req_pending);
  716. queue_work(rba->alloc_wq, &rba->rx_alloc);
  717. }
  718. }
  719. static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
  720. struct iwl_rx_mem_buffer *rxb,
  721. bool emergency)
  722. {
  723. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  724. struct iwl_rxq *rxq = &trans_pcie->rxq;
  725. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  726. bool page_stolen = false;
  727. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  728. u32 offset = 0;
  729. if (WARN_ON(!rxb))
  730. return;
  731. dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
  732. while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
  733. struct iwl_rx_packet *pkt;
  734. u16 sequence;
  735. bool reclaim;
  736. int index, cmd_index, len;
  737. struct iwl_rx_cmd_buffer rxcb = {
  738. ._offset = offset,
  739. ._rx_page_order = trans_pcie->rx_page_order,
  740. ._page = rxb->page,
  741. ._page_stolen = false,
  742. .truesize = max_len,
  743. };
  744. pkt = rxb_addr(&rxcb);
  745. if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
  746. break;
  747. IWL_DEBUG_RX(trans,
  748. "cmd at offset %d: %s (0x%.2x, seq 0x%x)\n",
  749. rxcb._offset,
  750. get_cmd_string(trans_pcie, pkt->hdr.cmd),
  751. pkt->hdr.cmd, le16_to_cpu(pkt->hdr.sequence));
  752. len = iwl_rx_packet_len(pkt);
  753. len += sizeof(u32); /* account for status word */
  754. trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
  755. trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
  756. /* Reclaim a command buffer only if this packet is a response
  757. * to a (driver-originated) command.
  758. * If the packet (e.g. Rx frame) originated from uCode,
  759. * there is no command buffer to reclaim.
  760. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  761. * but apparently a few don't get set; catch them here. */
  762. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
  763. if (reclaim) {
  764. int i;
  765. for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
  766. if (trans_pcie->no_reclaim_cmds[i] ==
  767. pkt->hdr.cmd) {
  768. reclaim = false;
  769. break;
  770. }
  771. }
  772. }
  773. sequence = le16_to_cpu(pkt->hdr.sequence);
  774. index = SEQ_TO_INDEX(sequence);
  775. cmd_index = get_cmd_index(&txq->q, index);
  776. iwl_op_mode_rx(trans->op_mode, &trans_pcie->napi, &rxcb);
  777. if (reclaim) {
  778. kzfree(txq->entries[cmd_index].free_buf);
  779. txq->entries[cmd_index].free_buf = NULL;
  780. }
  781. /*
  782. * After here, we should always check rxcb._page_stolen,
  783. * if it is true then one of the handlers took the page.
  784. */
  785. if (reclaim) {
  786. /* Invoke any callbacks, transfer the buffer to caller,
  787. * and fire off the (possibly) blocking
  788. * iwl_trans_send_cmd()
  789. * as we reclaim the driver command queue */
  790. if (!rxcb._page_stolen)
  791. iwl_pcie_hcmd_complete(trans, &rxcb);
  792. else
  793. IWL_WARN(trans, "Claim null rxb?\n");
  794. }
  795. page_stolen |= rxcb._page_stolen;
  796. offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
  797. }
  798. /* page was stolen from us -- free our reference */
  799. if (page_stolen) {
  800. __free_pages(rxb->page, trans_pcie->rx_page_order);
  801. rxb->page = NULL;
  802. }
  803. /* Reuse the page if possible. For notification packets and
  804. * SKBs that fail to Rx correctly, add them back into the
  805. * rx_free list for reuse later. */
  806. if (rxb->page != NULL) {
  807. rxb->page_dma =
  808. dma_map_page(trans->dev, rxb->page, 0,
  809. PAGE_SIZE << trans_pcie->rx_page_order,
  810. DMA_FROM_DEVICE);
  811. if (dma_mapping_error(trans->dev, rxb->page_dma)) {
  812. /*
  813. * free the page(s) as well to not break
  814. * the invariant that the items on the used
  815. * list have no page(s)
  816. */
  817. __free_pages(rxb->page, trans_pcie->rx_page_order);
  818. rxb->page = NULL;
  819. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  820. } else {
  821. list_add_tail(&rxb->list, &rxq->rx_free);
  822. rxq->free_count++;
  823. }
  824. } else
  825. iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
  826. }
  827. /*
  828. * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
  829. */
  830. static void iwl_pcie_rx_handle(struct iwl_trans *trans)
  831. {
  832. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  833. struct iwl_rxq *rxq = &trans_pcie->rxq;
  834. u32 r, i, j, count = 0;
  835. bool emergency = false;
  836. restart:
  837. spin_lock(&rxq->lock);
  838. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  839. * buffer that the driver may process (last buffer filled by ucode). */
  840. r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
  841. i = rxq->read;
  842. /* Rx interrupt, but nothing sent from uCode */
  843. if (i == r)
  844. IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
  845. while (i != r) {
  846. struct iwl_rx_mem_buffer *rxb;
  847. if (unlikely(rxq->used_count == RX_QUEUE_SIZE / 2))
  848. emergency = true;
  849. rxb = rxq->queue[i];
  850. rxq->queue[i] = NULL;
  851. IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d (%p)\n",
  852. r, i, rxb);
  853. iwl_pcie_rx_handle_rb(trans, rxb, emergency);
  854. i = (i + 1) & RX_QUEUE_MASK;
  855. /* If we have RX_CLAIM_REQ_ALLOC released rx buffers -
  856. * try to claim the pre-allocated buffers from the allocator */
  857. if (rxq->used_count >= RX_CLAIM_REQ_ALLOC) {
  858. struct iwl_rb_allocator *rba = &trans_pcie->rba;
  859. struct iwl_rx_mem_buffer *out[RX_CLAIM_REQ_ALLOC];
  860. if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 &&
  861. !emergency) {
  862. /* Add the remaining 6 empty RBDs
  863. * for allocator use
  864. */
  865. spin_lock(&rba->lock);
  866. list_splice_tail_init(&rxq->rx_used,
  867. &rba->rbd_empty);
  868. spin_unlock(&rba->lock);
  869. }
  870. /* If not ready - continue, will try to reclaim later.
  871. * No need to reschedule work - allocator exits only on
  872. * success */
  873. if (!iwl_pcie_rx_allocator_get(trans, out)) {
  874. /* If success - then RX_CLAIM_REQ_ALLOC
  875. * buffers were retrieved and should be added
  876. * to free list */
  877. rxq->used_count -= RX_CLAIM_REQ_ALLOC;
  878. for (j = 0; j < RX_CLAIM_REQ_ALLOC; j++) {
  879. list_add_tail(&out[j]->list,
  880. &rxq->rx_free);
  881. rxq->free_count++;
  882. }
  883. }
  884. }
  885. if (emergency) {
  886. count++;
  887. if (count == 8) {
  888. count = 0;
  889. if (rxq->used_count < RX_QUEUE_SIZE / 3)
  890. emergency = false;
  891. spin_unlock(&rxq->lock);
  892. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC);
  893. spin_lock(&rxq->lock);
  894. }
  895. }
  896. /* handle restock for three cases, can be all of them at once:
  897. * - we just pulled buffers from the allocator
  898. * - we have 8+ unstolen pages accumulated
  899. * - we are in emergency and allocated buffers
  900. */
  901. if (rxq->free_count >= RX_CLAIM_REQ_ALLOC) {
  902. rxq->read = i;
  903. spin_unlock(&rxq->lock);
  904. iwl_pcie_rxq_restock(trans);
  905. goto restart;
  906. }
  907. }
  908. /* Backtrack one entry */
  909. rxq->read = i;
  910. spin_unlock(&rxq->lock);
  911. /*
  912. * handle a case where in emergency there are some unallocated RBDs.
  913. * those RBDs are in the used list, but are not tracked by the queue's
  914. * used_count which counts allocator owned RBDs.
  915. * unallocated emergency RBDs must be allocated on exit, otherwise
  916. * when called again the function may not be in emergency mode and
  917. * they will be handed to the allocator with no tracking in the RBD
  918. * allocator counters, which will lead to them never being claimed back
  919. * by the queue.
  920. * by allocating them here, they are now in the queue free list, and
  921. * will be restocked by the next call of iwl_pcie_rxq_restock.
  922. */
  923. if (unlikely(emergency && count))
  924. iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC);
  925. if (trans_pcie->napi.poll)
  926. napi_gro_flush(&trans_pcie->napi, false);
  927. }
  928. /*
  929. * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
  930. */
  931. static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
  932. {
  933. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  934. int i;
  935. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  936. if (trans->cfg->internal_wimax_coex &&
  937. !trans->cfg->apmg_not_supported &&
  938. (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
  939. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  940. (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
  941. APMG_PS_CTRL_VAL_RESET_REQ))) {
  942. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  943. iwl_op_mode_wimax_active(trans->op_mode);
  944. wake_up(&trans_pcie->wait_command_queue);
  945. return;
  946. }
  947. iwl_pcie_dump_csr(trans);
  948. iwl_dump_fh(trans, NULL);
  949. local_bh_disable();
  950. /* The STATUS_FW_ERROR bit is set in this function. This must happen
  951. * before we wake up the command caller, to ensure a proper cleanup. */
  952. iwl_trans_fw_error(trans);
  953. local_bh_enable();
  954. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
  955. del_timer(&trans_pcie->txq[i].stuck_timer);
  956. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  957. wake_up(&trans_pcie->wait_command_queue);
  958. }
  959. static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
  960. {
  961. u32 inta;
  962. lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
  963. trace_iwlwifi_dev_irq(trans->dev);
  964. /* Discover which interrupts are active/pending */
  965. inta = iwl_read32(trans, CSR_INT);
  966. /* the thread will service interrupts and re-enable them */
  967. return inta;
  968. }
  969. /* a device (PCI-E) page is 4096 bytes long */
  970. #define ICT_SHIFT 12
  971. #define ICT_SIZE (1 << ICT_SHIFT)
  972. #define ICT_COUNT (ICT_SIZE / sizeof(u32))
  973. /* interrupt handler using ict table, with this interrupt driver will
  974. * stop using INTA register to get device's interrupt, reading this register
  975. * is expensive, device will write interrupts in ICT dram table, increment
  976. * index then will fire interrupt to driver, driver will OR all ICT table
  977. * entries from current index up to table entry with 0 value. the result is
  978. * the interrupt we need to service, driver will set the entries back to 0 and
  979. * set index.
  980. */
  981. static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
  982. {
  983. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  984. u32 inta;
  985. u32 val = 0;
  986. u32 read;
  987. trace_iwlwifi_dev_irq(trans->dev);
  988. /* Ignore interrupt if there's nothing in NIC to service.
  989. * This may be due to IRQ shared with another device,
  990. * or due to sporadic interrupts thrown from our NIC. */
  991. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  992. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
  993. if (!read)
  994. return 0;
  995. /*
  996. * Collect all entries up to the first 0, starting from ict_index;
  997. * note we already read at ict_index.
  998. */
  999. do {
  1000. val |= read;
  1001. IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
  1002. trans_pcie->ict_index, read);
  1003. trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
  1004. trans_pcie->ict_index =
  1005. ((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
  1006. read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
  1007. trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
  1008. read);
  1009. } while (read);
  1010. /* We should not get this value, just ignore it. */
  1011. if (val == 0xffffffff)
  1012. val = 0;
  1013. /*
  1014. * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
  1015. * (bit 15 before shifting it to 31) to clear when using interrupt
  1016. * coalescing. fortunately, bits 18 and 19 stay set when this happens
  1017. * so we use them to decide on the real state of the Rx bit.
  1018. * In order words, bit 15 is set if bit 18 or bit 19 are set.
  1019. */
  1020. if (val & 0xC0000)
  1021. val |= 0x8000;
  1022. inta = (0xff & val) | ((0xff00 & val) << 16);
  1023. return inta;
  1024. }
  1025. irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
  1026. {
  1027. struct iwl_trans *trans = dev_id;
  1028. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1029. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1030. u32 inta = 0;
  1031. u32 handled = 0;
  1032. lock_map_acquire(&trans->sync_cmd_lockdep_map);
  1033. spin_lock(&trans_pcie->irq_lock);
  1034. /* dram interrupt table not set yet,
  1035. * use legacy interrupt.
  1036. */
  1037. if (likely(trans_pcie->use_ict))
  1038. inta = iwl_pcie_int_cause_ict(trans);
  1039. else
  1040. inta = iwl_pcie_int_cause_non_ict(trans);
  1041. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1042. IWL_DEBUG_ISR(trans,
  1043. "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
  1044. inta, trans_pcie->inta_mask,
  1045. iwl_read32(trans, CSR_INT_MASK),
  1046. iwl_read32(trans, CSR_FH_INT_STATUS));
  1047. if (inta & (~trans_pcie->inta_mask))
  1048. IWL_DEBUG_ISR(trans,
  1049. "We got a masked interrupt (0x%08x)\n",
  1050. inta & (~trans_pcie->inta_mask));
  1051. }
  1052. inta &= trans_pcie->inta_mask;
  1053. /*
  1054. * Ignore interrupt if there's nothing in NIC to service.
  1055. * This may be due to IRQ shared with another device,
  1056. * or due to sporadic interrupts thrown from our NIC.
  1057. */
  1058. if (unlikely(!inta)) {
  1059. IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
  1060. /*
  1061. * Re-enable interrupts here since we don't
  1062. * have anything to service
  1063. */
  1064. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1065. iwl_enable_interrupts(trans);
  1066. spin_unlock(&trans_pcie->irq_lock);
  1067. lock_map_release(&trans->sync_cmd_lockdep_map);
  1068. return IRQ_NONE;
  1069. }
  1070. if (unlikely(inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1071. /*
  1072. * Hardware disappeared. It might have
  1073. * already raised an interrupt.
  1074. */
  1075. IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1076. spin_unlock(&trans_pcie->irq_lock);
  1077. goto out;
  1078. }
  1079. /* Ack/clear/reset pending uCode interrupts.
  1080. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1081. */
  1082. /* There is a hardware bug in the interrupt mask function that some
  1083. * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
  1084. * they are disabled in the CSR_INT_MASK register. Furthermore the
  1085. * ICT interrupt handling mechanism has another bug that might cause
  1086. * these unmasked interrupts fail to be detected. We workaround the
  1087. * hardware bugs here by ACKing all the possible interrupts so that
  1088. * interrupt coalescing can still be achieved.
  1089. */
  1090. iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
  1091. if (iwl_have_debug_level(IWL_DL_ISR))
  1092. IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
  1093. inta, iwl_read32(trans, CSR_INT_MASK));
  1094. spin_unlock(&trans_pcie->irq_lock);
  1095. /* Now service all interrupt bits discovered above. */
  1096. if (inta & CSR_INT_BIT_HW_ERR) {
  1097. IWL_ERR(trans, "Hardware error detected. Restarting.\n");
  1098. /* Tell the device to stop sending interrupts */
  1099. iwl_disable_interrupts(trans);
  1100. isr_stats->hw++;
  1101. iwl_pcie_irq_handle_error(trans);
  1102. handled |= CSR_INT_BIT_HW_ERR;
  1103. goto out;
  1104. }
  1105. if (iwl_have_debug_level(IWL_DL_ISR)) {
  1106. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1107. if (inta & CSR_INT_BIT_SCD) {
  1108. IWL_DEBUG_ISR(trans,
  1109. "Scheduler finished to transmit the frame/frames.\n");
  1110. isr_stats->sch++;
  1111. }
  1112. /* Alive notification via Rx interrupt will do the real work */
  1113. if (inta & CSR_INT_BIT_ALIVE) {
  1114. IWL_DEBUG_ISR(trans, "Alive interrupt\n");
  1115. isr_stats->alive++;
  1116. }
  1117. }
  1118. /* Safely ignore these bits for debug checks below */
  1119. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1120. /* HW RF KILL switch toggled */
  1121. if (inta & CSR_INT_BIT_RF_KILL) {
  1122. bool hw_rfkill;
  1123. hw_rfkill = iwl_is_rfkill_set(trans);
  1124. IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
  1125. hw_rfkill ? "disable radio" : "enable radio");
  1126. isr_stats->rfkill++;
  1127. mutex_lock(&trans_pcie->mutex);
  1128. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1129. mutex_unlock(&trans_pcie->mutex);
  1130. if (hw_rfkill) {
  1131. set_bit(STATUS_RFKILL, &trans->status);
  1132. if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
  1133. &trans->status))
  1134. IWL_DEBUG_RF_KILL(trans,
  1135. "Rfkill while SYNC HCMD in flight\n");
  1136. wake_up(&trans_pcie->wait_command_queue);
  1137. } else {
  1138. clear_bit(STATUS_RFKILL, &trans->status);
  1139. }
  1140. handled |= CSR_INT_BIT_RF_KILL;
  1141. }
  1142. /* Chip got too hot and stopped itself */
  1143. if (inta & CSR_INT_BIT_CT_KILL) {
  1144. IWL_ERR(trans, "Microcode CT kill error detected.\n");
  1145. isr_stats->ctkill++;
  1146. handled |= CSR_INT_BIT_CT_KILL;
  1147. }
  1148. /* Error detected by uCode */
  1149. if (inta & CSR_INT_BIT_SW_ERR) {
  1150. IWL_ERR(trans, "Microcode SW error detected. "
  1151. " Restarting 0x%X.\n", inta);
  1152. isr_stats->sw++;
  1153. iwl_pcie_irq_handle_error(trans);
  1154. handled |= CSR_INT_BIT_SW_ERR;
  1155. }
  1156. /* uCode wakes up after power-down sleep */
  1157. if (inta & CSR_INT_BIT_WAKEUP) {
  1158. IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
  1159. iwl_pcie_rxq_check_wrptr(trans);
  1160. iwl_pcie_txq_check_wrptrs(trans);
  1161. isr_stats->wakeup++;
  1162. handled |= CSR_INT_BIT_WAKEUP;
  1163. }
  1164. /* All uCode command responses, including Tx command responses,
  1165. * Rx "responses" (frame-received notification), and other
  1166. * notifications from uCode come through here*/
  1167. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
  1168. CSR_INT_BIT_RX_PERIODIC)) {
  1169. IWL_DEBUG_ISR(trans, "Rx interrupt\n");
  1170. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1171. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1172. iwl_write32(trans, CSR_FH_INT_STATUS,
  1173. CSR_FH_INT_RX_MASK);
  1174. }
  1175. if (inta & CSR_INT_BIT_RX_PERIODIC) {
  1176. handled |= CSR_INT_BIT_RX_PERIODIC;
  1177. iwl_write32(trans,
  1178. CSR_INT, CSR_INT_BIT_RX_PERIODIC);
  1179. }
  1180. /* Sending RX interrupt require many steps to be done in the
  1181. * the device:
  1182. * 1- write interrupt to current index in ICT table.
  1183. * 2- dma RX frame.
  1184. * 3- update RX shared data to indicate last write index.
  1185. * 4- send interrupt.
  1186. * This could lead to RX race, driver could receive RX interrupt
  1187. * but the shared data changes does not reflect this;
  1188. * periodic interrupt will detect any dangling Rx activity.
  1189. */
  1190. /* Disable periodic interrupt; we use it as just a one-shot. */
  1191. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1192. CSR_INT_PERIODIC_DIS);
  1193. /*
  1194. * Enable periodic interrupt in 8 msec only if we received
  1195. * real RX interrupt (instead of just periodic int), to catch
  1196. * any dangling Rx interrupt. If it was just the periodic
  1197. * interrupt, there was no dangling Rx activity, and no need
  1198. * to extend the periodic interrupt; one-shot is enough.
  1199. */
  1200. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
  1201. iwl_write8(trans, CSR_INT_PERIODIC_REG,
  1202. CSR_INT_PERIODIC_ENA);
  1203. isr_stats->rx++;
  1204. local_bh_disable();
  1205. iwl_pcie_rx_handle(trans);
  1206. local_bh_enable();
  1207. }
  1208. /* This "Tx" DMA channel is used only for loading uCode */
  1209. if (inta & CSR_INT_BIT_FH_TX) {
  1210. iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
  1211. IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
  1212. isr_stats->tx++;
  1213. handled |= CSR_INT_BIT_FH_TX;
  1214. /* Wake up uCode load routine, now that load is complete */
  1215. trans_pcie->ucode_write_complete = true;
  1216. wake_up(&trans_pcie->ucode_write_waitq);
  1217. }
  1218. if (inta & ~handled) {
  1219. IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1220. isr_stats->unhandled++;
  1221. }
  1222. if (inta & ~(trans_pcie->inta_mask)) {
  1223. IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
  1224. inta & ~trans_pcie->inta_mask);
  1225. }
  1226. /* Re-enable all interrupts */
  1227. /* only Re-enable if disabled by irq */
  1228. if (test_bit(STATUS_INT_ENABLED, &trans->status))
  1229. iwl_enable_interrupts(trans);
  1230. /* Re-enable RF_KILL if it occurred */
  1231. else if (handled & CSR_INT_BIT_RF_KILL)
  1232. iwl_enable_rfkill_int(trans);
  1233. out:
  1234. lock_map_release(&trans->sync_cmd_lockdep_map);
  1235. return IRQ_HANDLED;
  1236. }
  1237. /******************************************************************************
  1238. *
  1239. * ICT functions
  1240. *
  1241. ******************************************************************************/
  1242. /* Free dram table */
  1243. void iwl_pcie_free_ict(struct iwl_trans *trans)
  1244. {
  1245. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1246. if (trans_pcie->ict_tbl) {
  1247. dma_free_coherent(trans->dev, ICT_SIZE,
  1248. trans_pcie->ict_tbl,
  1249. trans_pcie->ict_tbl_dma);
  1250. trans_pcie->ict_tbl = NULL;
  1251. trans_pcie->ict_tbl_dma = 0;
  1252. }
  1253. }
  1254. /*
  1255. * allocate dram shared table, it is an aligned memory
  1256. * block of ICT_SIZE.
  1257. * also reset all data related to ICT table interrupt.
  1258. */
  1259. int iwl_pcie_alloc_ict(struct iwl_trans *trans)
  1260. {
  1261. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1262. trans_pcie->ict_tbl =
  1263. dma_zalloc_coherent(trans->dev, ICT_SIZE,
  1264. &trans_pcie->ict_tbl_dma,
  1265. GFP_KERNEL);
  1266. if (!trans_pcie->ict_tbl)
  1267. return -ENOMEM;
  1268. /* just an API sanity check ... it is guaranteed to be aligned */
  1269. if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
  1270. iwl_pcie_free_ict(trans);
  1271. return -EINVAL;
  1272. }
  1273. IWL_DEBUG_ISR(trans, "ict dma addr %Lx ict vir addr %p\n",
  1274. (unsigned long long)trans_pcie->ict_tbl_dma,
  1275. trans_pcie->ict_tbl);
  1276. return 0;
  1277. }
  1278. /* Device is going up inform it about using ICT interrupt table,
  1279. * also we need to tell the driver to start using ICT interrupt.
  1280. */
  1281. void iwl_pcie_reset_ict(struct iwl_trans *trans)
  1282. {
  1283. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1284. u32 val;
  1285. if (!trans_pcie->ict_tbl)
  1286. return;
  1287. spin_lock(&trans_pcie->irq_lock);
  1288. iwl_disable_interrupts(trans);
  1289. memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
  1290. val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
  1291. val |= CSR_DRAM_INT_TBL_ENABLE |
  1292. CSR_DRAM_INIT_TBL_WRAP_CHECK |
  1293. CSR_DRAM_INIT_TBL_WRITE_POINTER;
  1294. IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
  1295. iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
  1296. trans_pcie->use_ict = true;
  1297. trans_pcie->ict_index = 0;
  1298. iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
  1299. iwl_enable_interrupts(trans);
  1300. spin_unlock(&trans_pcie->irq_lock);
  1301. }
  1302. /* Device is going down disable ict interrupt usage */
  1303. void iwl_pcie_disable_ict(struct iwl_trans *trans)
  1304. {
  1305. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1306. spin_lock(&trans_pcie->irq_lock);
  1307. trans_pcie->use_ict = false;
  1308. spin_unlock(&trans_pcie->irq_lock);
  1309. }
  1310. irqreturn_t iwl_pcie_isr(int irq, void *data)
  1311. {
  1312. struct iwl_trans *trans = data;
  1313. if (!trans)
  1314. return IRQ_NONE;
  1315. /* Disable (but don't clear!) interrupts here to avoid
  1316. * back-to-back ISRs and sporadic interrupts from our NIC.
  1317. * If we have something to service, the tasklet will re-enable ints.
  1318. * If we *don't* have something, we'll re-enable before leaving here.
  1319. */
  1320. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  1321. return IRQ_WAKE_THREAD;
  1322. }