iwl-trans-pcie.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/interrupt.h>
  64. #include <linux/debugfs.h>
  65. #include <linux/bitops.h>
  66. #include <linux/gfp.h>
  67. #include "iwl-trans.h"
  68. #include "iwl-trans-pcie-int.h"
  69. #include "iwl-csr.h"
  70. #include "iwl-prph.h"
  71. #include "iwl-shared.h"
  72. #include "iwl-eeprom.h"
  73. #include "iwl-agn-hw.h"
  74. static int iwl_trans_rx_alloc(struct iwl_trans *trans)
  75. {
  76. struct iwl_trans_pcie *trans_pcie =
  77. IWL_TRANS_GET_PCIE_TRANS(trans);
  78. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  79. struct device *dev = bus(trans)->dev;
  80. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  81. spin_lock_init(&rxq->lock);
  82. if (WARN_ON(rxq->bd || rxq->rb_stts))
  83. return -EINVAL;
  84. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  85. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  86. &rxq->bd_dma, GFP_KERNEL);
  87. if (!rxq->bd)
  88. goto err_bd;
  89. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  90. /*Allocate the driver's pointer to receive buffer status */
  91. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  92. &rxq->rb_stts_dma, GFP_KERNEL);
  93. if (!rxq->rb_stts)
  94. goto err_rb_stts;
  95. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  96. return 0;
  97. err_rb_stts:
  98. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  99. rxq->bd, rxq->bd_dma);
  100. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  101. rxq->bd = NULL;
  102. err_bd:
  103. return -ENOMEM;
  104. }
  105. static void iwl_trans_rxq_free_rx_bufs(struct iwl_trans *trans)
  106. {
  107. struct iwl_trans_pcie *trans_pcie =
  108. IWL_TRANS_GET_PCIE_TRANS(trans);
  109. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  110. int i;
  111. /* Fill the rx_used queue with _all_ of the Rx buffers */
  112. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  113. /* In the reset function, these buffers may have been allocated
  114. * to an SKB, so we need to unmap and free potential storage */
  115. if (rxq->pool[i].page != NULL) {
  116. dma_unmap_page(bus(trans)->dev, rxq->pool[i].page_dma,
  117. PAGE_SIZE << hw_params(trans).rx_page_order,
  118. DMA_FROM_DEVICE);
  119. __free_pages(rxq->pool[i].page,
  120. hw_params(trans).rx_page_order);
  121. rxq->pool[i].page = NULL;
  122. }
  123. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  124. }
  125. }
  126. static void iwl_trans_rx_hw_init(struct iwl_trans *trans,
  127. struct iwl_rx_queue *rxq)
  128. {
  129. u32 rb_size;
  130. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  131. u32 rb_timeout = RX_RB_TIMEOUT; /* FIXME: RX_RB_TIMEOUT for all devices? */
  132. if (iwlagn_mod_params.amsdu_size_8K)
  133. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  134. else
  135. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  136. /* Stop Rx DMA */
  137. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  138. /* Reset driver's Rx queue write index */
  139. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  140. /* Tell device where to find RBD circular buffer in DRAM */
  141. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  142. (u32)(rxq->bd_dma >> 8));
  143. /* Tell device where in DRAM to update its Rx status */
  144. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_STTS_WPTR_REG,
  145. rxq->rb_stts_dma >> 4);
  146. /* Enable Rx DMA
  147. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  148. * the credit mechanism in 5000 HW RX FIFO
  149. * Direct rx interrupts to hosts
  150. * Rx buffer size 4 or 8k
  151. * RB timeout 0x10
  152. * 256 RBDs
  153. */
  154. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG,
  155. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  156. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  157. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  158. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  159. rb_size|
  160. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  161. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  162. /* Set interrupt coalescing timer to default (2048 usecs) */
  163. iwl_write8(bus(trans), CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  164. }
  165. static int iwl_rx_init(struct iwl_trans *trans)
  166. {
  167. struct iwl_trans_pcie *trans_pcie =
  168. IWL_TRANS_GET_PCIE_TRANS(trans);
  169. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  170. int i, err;
  171. unsigned long flags;
  172. if (!rxq->bd) {
  173. err = iwl_trans_rx_alloc(trans);
  174. if (err)
  175. return err;
  176. }
  177. spin_lock_irqsave(&rxq->lock, flags);
  178. INIT_LIST_HEAD(&rxq->rx_free);
  179. INIT_LIST_HEAD(&rxq->rx_used);
  180. iwl_trans_rxq_free_rx_bufs(trans);
  181. for (i = 0; i < RX_QUEUE_SIZE; i++)
  182. rxq->queue[i] = NULL;
  183. /* Set us so that we have processed and used all buffers, but have
  184. * not restocked the Rx queue with fresh buffers */
  185. rxq->read = rxq->write = 0;
  186. rxq->write_actual = 0;
  187. rxq->free_count = 0;
  188. spin_unlock_irqrestore(&rxq->lock, flags);
  189. iwlagn_rx_replenish(trans);
  190. iwl_trans_rx_hw_init(trans, rxq);
  191. spin_lock_irqsave(&trans->shrd->lock, flags);
  192. rxq->need_update = 1;
  193. iwl_rx_queue_update_write_ptr(trans, rxq);
  194. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  195. return 0;
  196. }
  197. static void iwl_trans_pcie_rx_free(struct iwl_trans *trans)
  198. {
  199. struct iwl_trans_pcie *trans_pcie =
  200. IWL_TRANS_GET_PCIE_TRANS(trans);
  201. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  202. unsigned long flags;
  203. /*if rxq->bd is NULL, it means that nothing has been allocated,
  204. * exit now */
  205. if (!rxq->bd) {
  206. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  207. return;
  208. }
  209. spin_lock_irqsave(&rxq->lock, flags);
  210. iwl_trans_rxq_free_rx_bufs(trans);
  211. spin_unlock_irqrestore(&rxq->lock, flags);
  212. dma_free_coherent(bus(trans)->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  213. rxq->bd, rxq->bd_dma);
  214. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  215. rxq->bd = NULL;
  216. if (rxq->rb_stts)
  217. dma_free_coherent(bus(trans)->dev,
  218. sizeof(struct iwl_rb_status),
  219. rxq->rb_stts, rxq->rb_stts_dma);
  220. else
  221. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  222. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  223. rxq->rb_stts = NULL;
  224. }
  225. static int iwl_trans_rx_stop(struct iwl_trans *trans)
  226. {
  227. /* stop Rx DMA */
  228. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  229. return iwl_poll_direct_bit(bus(trans), FH_MEM_RSSR_RX_STATUS_REG,
  230. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  231. }
  232. static inline int iwlagn_alloc_dma_ptr(struct iwl_trans *trans,
  233. struct iwl_dma_ptr *ptr, size_t size)
  234. {
  235. if (WARN_ON(ptr->addr))
  236. return -EINVAL;
  237. ptr->addr = dma_alloc_coherent(bus(trans)->dev, size,
  238. &ptr->dma, GFP_KERNEL);
  239. if (!ptr->addr)
  240. return -ENOMEM;
  241. ptr->size = size;
  242. return 0;
  243. }
  244. static inline void iwlagn_free_dma_ptr(struct iwl_trans *trans,
  245. struct iwl_dma_ptr *ptr)
  246. {
  247. if (unlikely(!ptr->addr))
  248. return;
  249. dma_free_coherent(bus(trans)->dev, ptr->size, ptr->addr, ptr->dma);
  250. memset(ptr, 0, sizeof(*ptr));
  251. }
  252. static int iwl_trans_txq_alloc(struct iwl_trans *trans,
  253. struct iwl_tx_queue *txq, int slots_num,
  254. u32 txq_id)
  255. {
  256. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  257. int i;
  258. if (WARN_ON(txq->meta || txq->cmd || txq->skbs || txq->tfds))
  259. return -EINVAL;
  260. txq->q.n_window = slots_num;
  261. txq->meta = kcalloc(slots_num, sizeof(txq->meta[0]), GFP_KERNEL);
  262. txq->cmd = kcalloc(slots_num, sizeof(txq->cmd[0]), GFP_KERNEL);
  263. if (!txq->meta || !txq->cmd)
  264. goto error;
  265. if (txq_id == trans->shrd->cmd_queue)
  266. for (i = 0; i < slots_num; i++) {
  267. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  268. GFP_KERNEL);
  269. if (!txq->cmd[i])
  270. goto error;
  271. }
  272. /* Alloc driver data array and TFD circular buffer */
  273. /* Driver private data, only for Tx (not command) queues,
  274. * not shared with device. */
  275. if (txq_id != trans->shrd->cmd_queue) {
  276. txq->skbs = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(txq->skbs[0]),
  277. GFP_KERNEL);
  278. if (!txq->skbs) {
  279. IWL_ERR(trans, "kmalloc for auxiliary BD "
  280. "structures failed\n");
  281. goto error;
  282. }
  283. } else {
  284. txq->skbs = NULL;
  285. }
  286. /* Circular buffer of transmit frame descriptors (TFDs),
  287. * shared with device */
  288. txq->tfds = dma_alloc_coherent(bus(trans)->dev, tfd_sz,
  289. &txq->q.dma_addr, GFP_KERNEL);
  290. if (!txq->tfds) {
  291. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  292. goto error;
  293. }
  294. txq->q.id = txq_id;
  295. return 0;
  296. error:
  297. kfree(txq->skbs);
  298. txq->skbs = NULL;
  299. /* since txq->cmd has been zeroed,
  300. * all non allocated cmd[i] will be NULL */
  301. if (txq->cmd && txq_id == trans->shrd->cmd_queue)
  302. for (i = 0; i < slots_num; i++)
  303. kfree(txq->cmd[i]);
  304. kfree(txq->meta);
  305. kfree(txq->cmd);
  306. txq->meta = NULL;
  307. txq->cmd = NULL;
  308. return -ENOMEM;
  309. }
  310. static int iwl_trans_txq_init(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  311. int slots_num, u32 txq_id)
  312. {
  313. int ret;
  314. txq->need_update = 0;
  315. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  316. /*
  317. * For the default queues 0-3, set up the swq_id
  318. * already -- all others need to get one later
  319. * (if they need one at all).
  320. */
  321. if (txq_id < 4)
  322. iwl_set_swq_id(txq, txq_id, txq_id);
  323. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  324. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  325. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  326. /* Initialize queue's high/low-water marks, and head/tail indexes */
  327. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  328. txq_id);
  329. if (ret)
  330. return ret;
  331. /*
  332. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  333. * given Tx queue, and enable the DMA channel used for that queue.
  334. * Circular buffer (TFD queue in DRAM) physical base address */
  335. iwl_write_direct32(bus(trans), FH_MEM_CBBC_QUEUE(txq_id),
  336. txq->q.dma_addr >> 8);
  337. return 0;
  338. }
  339. /**
  340. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  341. */
  342. static void iwl_tx_queue_unmap(struct iwl_trans *trans, int txq_id)
  343. {
  344. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  345. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  346. struct iwl_queue *q = &txq->q;
  347. enum dma_data_direction dma_dir;
  348. unsigned long flags;
  349. if (!q->n_bd)
  350. return;
  351. /* In the command queue, all the TBs are mapped as BIDI
  352. * so unmap them as such.
  353. */
  354. if (txq_id == trans->shrd->cmd_queue)
  355. dma_dir = DMA_BIDIRECTIONAL;
  356. else
  357. dma_dir = DMA_TO_DEVICE;
  358. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  359. while (q->write_ptr != q->read_ptr) {
  360. /* The read_ptr needs to bound by q->n_window */
  361. iwlagn_txq_free_tfd(trans, txq, get_cmd_index(q, q->read_ptr),
  362. dma_dir);
  363. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  364. }
  365. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  366. }
  367. /**
  368. * iwl_tx_queue_free - Deallocate DMA queue.
  369. * @txq: Transmit queue to deallocate.
  370. *
  371. * Empty queue by removing and destroying all BD's.
  372. * Free all buffers.
  373. * 0-fill, but do not free "txq" descriptor structure.
  374. */
  375. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  376. {
  377. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  378. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  379. struct device *dev = bus(trans)->dev;
  380. int i;
  381. if (WARN_ON(!txq))
  382. return;
  383. iwl_tx_queue_unmap(trans, txq_id);
  384. /* De-alloc array of command/tx buffers */
  385. if (txq_id == trans->shrd->cmd_queue)
  386. for (i = 0; i < txq->q.n_window; i++)
  387. kfree(txq->cmd[i]);
  388. /* De-alloc circular buffer of TFDs */
  389. if (txq->q.n_bd) {
  390. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  391. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  392. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  393. }
  394. /* De-alloc array of per-TFD driver data */
  395. kfree(txq->skbs);
  396. txq->skbs = NULL;
  397. /* deallocate arrays */
  398. kfree(txq->cmd);
  399. kfree(txq->meta);
  400. txq->cmd = NULL;
  401. txq->meta = NULL;
  402. /* 0-fill queue descriptor structure */
  403. memset(txq, 0, sizeof(*txq));
  404. }
  405. /**
  406. * iwl_trans_tx_free - Free TXQ Context
  407. *
  408. * Destroy all TX DMA queues and structures
  409. */
  410. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  411. {
  412. int txq_id;
  413. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  414. /* Tx queues */
  415. if (trans_pcie->txq) {
  416. for (txq_id = 0;
  417. txq_id < hw_params(trans).max_txq_num; txq_id++)
  418. iwl_tx_queue_free(trans, txq_id);
  419. }
  420. kfree(trans_pcie->txq);
  421. trans_pcie->txq = NULL;
  422. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  423. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  424. }
  425. /**
  426. * iwl_trans_tx_alloc - allocate TX context
  427. * Allocate all Tx DMA structures and initialize them
  428. *
  429. * @param priv
  430. * @return error code
  431. */
  432. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  433. {
  434. int ret;
  435. int txq_id, slots_num;
  436. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  437. u16 scd_bc_tbls_size = hw_params(trans).max_txq_num *
  438. sizeof(struct iwlagn_scd_bc_tbl);
  439. /*It is not allowed to alloc twice, so warn when this happens.
  440. * We cannot rely on the previous allocation, so free and fail */
  441. if (WARN_ON(trans_pcie->txq)) {
  442. ret = -EINVAL;
  443. goto error;
  444. }
  445. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  446. scd_bc_tbls_size);
  447. if (ret) {
  448. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  449. goto error;
  450. }
  451. /* Alloc keep-warm buffer */
  452. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  453. if (ret) {
  454. IWL_ERR(trans, "Keep Warm allocation failed\n");
  455. goto error;
  456. }
  457. trans_pcie->txq = kcalloc(hw_params(trans).max_txq_num,
  458. sizeof(struct iwl_tx_queue), GFP_KERNEL);
  459. if (!trans_pcie->txq) {
  460. IWL_ERR(trans, "Not enough memory for txq\n");
  461. ret = ENOMEM;
  462. goto error;
  463. }
  464. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  465. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  466. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  467. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  468. ret = iwl_trans_txq_alloc(trans, &trans_pcie->txq[txq_id],
  469. slots_num, txq_id);
  470. if (ret) {
  471. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  472. goto error;
  473. }
  474. }
  475. return 0;
  476. error:
  477. iwl_trans_pcie_tx_free(trans);
  478. return ret;
  479. }
  480. static int iwl_tx_init(struct iwl_trans *trans)
  481. {
  482. int ret;
  483. int txq_id, slots_num;
  484. unsigned long flags;
  485. bool alloc = false;
  486. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  487. if (!trans_pcie->txq) {
  488. ret = iwl_trans_tx_alloc(trans);
  489. if (ret)
  490. goto error;
  491. alloc = true;
  492. }
  493. spin_lock_irqsave(&trans->shrd->lock, flags);
  494. /* Turn off all Tx DMA fifos */
  495. iwl_write_prph(bus(trans), SCD_TXFACT, 0);
  496. /* Tell NIC where to find the "keep warm" buffer */
  497. iwl_write_direct32(bus(trans), FH_KW_MEM_ADDR_REG,
  498. trans_pcie->kw.dma >> 4);
  499. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  500. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  501. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  502. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  503. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  504. ret = iwl_trans_txq_init(trans, &trans_pcie->txq[txq_id],
  505. slots_num, txq_id);
  506. if (ret) {
  507. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  508. goto error;
  509. }
  510. }
  511. return 0;
  512. error:
  513. /*Upon error, free only if we allocated something */
  514. if (alloc)
  515. iwl_trans_pcie_tx_free(trans);
  516. return ret;
  517. }
  518. static void iwl_set_pwr_vmain(struct iwl_trans *trans)
  519. {
  520. /*
  521. * (for documentation purposes)
  522. * to set power to V_AUX, do:
  523. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  524. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  525. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  526. ~APMG_PS_CTRL_MSK_PWR_SRC);
  527. */
  528. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  529. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  530. ~APMG_PS_CTRL_MSK_PWR_SRC);
  531. }
  532. static int iwl_nic_init(struct iwl_trans *trans)
  533. {
  534. unsigned long flags;
  535. /* nic_init */
  536. spin_lock_irqsave(&trans->shrd->lock, flags);
  537. iwl_apm_init(priv(trans));
  538. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  539. iwl_write8(bus(trans), CSR_INT_COALESCING,
  540. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  541. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  542. iwl_set_pwr_vmain(trans);
  543. iwl_nic_config(priv(trans));
  544. /* Allocate the RX queue, or reset if it is already allocated */
  545. iwl_rx_init(trans);
  546. /* Allocate or reset and init all Tx and Command queues */
  547. if (iwl_tx_init(trans))
  548. return -ENOMEM;
  549. if (hw_params(trans).shadow_reg_enable) {
  550. /* enable shadow regs in HW */
  551. iwl_set_bit(bus(trans), CSR_MAC_SHADOW_REG_CTRL,
  552. 0x800FFFFF);
  553. }
  554. set_bit(STATUS_INIT, &trans->shrd->status);
  555. return 0;
  556. }
  557. #define HW_READY_TIMEOUT (50)
  558. /* Note: returns poll_bit return value, which is >= 0 if success */
  559. static int iwl_set_hw_ready(struct iwl_trans *trans)
  560. {
  561. int ret;
  562. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  563. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  564. /* See if we got it */
  565. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  566. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  567. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  568. HW_READY_TIMEOUT);
  569. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  570. return ret;
  571. }
  572. /* Note: returns standard 0/-ERROR code */
  573. static int iwl_trans_pcie_prepare_card_hw(struct iwl_trans *trans)
  574. {
  575. int ret;
  576. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  577. ret = iwl_set_hw_ready(trans);
  578. if (ret >= 0)
  579. return 0;
  580. /* If HW is not ready, prepare the conditions to check again */
  581. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  582. CSR_HW_IF_CONFIG_REG_PREPARE);
  583. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  584. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  585. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  586. if (ret < 0)
  587. return ret;
  588. /* HW should be ready by now, check again. */
  589. ret = iwl_set_hw_ready(trans);
  590. if (ret >= 0)
  591. return 0;
  592. return ret;
  593. }
  594. #define IWL_AC_UNSET -1
  595. struct queue_to_fifo_ac {
  596. s8 fifo, ac;
  597. };
  598. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  599. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  600. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  601. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  602. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  603. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  604. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  605. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  606. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  607. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  608. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  609. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  610. };
  611. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  612. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  613. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  614. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  615. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  616. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  617. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  618. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  619. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  620. { IWL_TX_FIFO_BE_IPAN, 2, },
  621. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  622. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  623. };
  624. static const u8 iwlagn_bss_ac_to_fifo[] = {
  625. IWL_TX_FIFO_VO,
  626. IWL_TX_FIFO_VI,
  627. IWL_TX_FIFO_BE,
  628. IWL_TX_FIFO_BK,
  629. };
  630. static const u8 iwlagn_bss_ac_to_queue[] = {
  631. 0, 1, 2, 3,
  632. };
  633. static const u8 iwlagn_pan_ac_to_fifo[] = {
  634. IWL_TX_FIFO_VO_IPAN,
  635. IWL_TX_FIFO_VI_IPAN,
  636. IWL_TX_FIFO_BE_IPAN,
  637. IWL_TX_FIFO_BK_IPAN,
  638. };
  639. static const u8 iwlagn_pan_ac_to_queue[] = {
  640. 7, 6, 5, 4,
  641. };
  642. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  643. {
  644. int ret;
  645. struct iwl_trans_pcie *trans_pcie =
  646. IWL_TRANS_GET_PCIE_TRANS(trans);
  647. trans->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  648. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  649. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  650. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  651. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  652. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  653. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  654. if ((hw_params(trans).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  655. iwl_trans_pcie_prepare_card_hw(trans)) {
  656. IWL_WARN(trans, "Exit HW not ready\n");
  657. return -EIO;
  658. }
  659. /* If platform's RF_KILL switch is NOT set to KILL */
  660. if (iwl_read32(bus(trans), CSR_GP_CNTRL) &
  661. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  662. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  663. else
  664. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  665. if (iwl_is_rfkill(trans->shrd)) {
  666. iwl_set_hw_rfkill_state(priv(trans), true);
  667. iwl_enable_interrupts(trans);
  668. return -ERFKILL;
  669. }
  670. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  671. ret = iwl_nic_init(trans);
  672. if (ret) {
  673. IWL_ERR(trans, "Unable to init nic\n");
  674. return ret;
  675. }
  676. /* make sure rfkill handshake bits are cleared */
  677. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  678. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR,
  679. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  680. /* clear (again), then enable host interrupts */
  681. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  682. iwl_enable_interrupts(trans);
  683. /* really make sure rfkill handshake bits are cleared */
  684. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  685. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  686. return 0;
  687. }
  688. /*
  689. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  690. * must be called under priv->shrd->lock and mac access
  691. */
  692. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  693. {
  694. iwl_write_prph(bus(trans), SCD_TXFACT, mask);
  695. }
  696. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  697. {
  698. const struct queue_to_fifo_ac *queue_to_fifo;
  699. struct iwl_trans_pcie *trans_pcie =
  700. IWL_TRANS_GET_PCIE_TRANS(trans);
  701. u32 a;
  702. unsigned long flags;
  703. int i, chan;
  704. u32 reg_val;
  705. spin_lock_irqsave(&trans->shrd->lock, flags);
  706. trans_pcie->scd_base_addr =
  707. iwl_read_prph(bus(trans), SCD_SRAM_BASE_ADDR);
  708. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  709. /* reset conext data memory */
  710. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  711. a += 4)
  712. iwl_write_targ_mem(bus(trans), a, 0);
  713. /* reset tx status memory */
  714. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  715. a += 4)
  716. iwl_write_targ_mem(bus(trans), a, 0);
  717. for (; a < trans_pcie->scd_base_addr +
  718. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(trans).max_txq_num);
  719. a += 4)
  720. iwl_write_targ_mem(bus(trans), a, 0);
  721. iwl_write_prph(bus(trans), SCD_DRAM_BASE_ADDR,
  722. trans_pcie->scd_bc_tbls.dma >> 10);
  723. /* Enable DMA channel */
  724. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  725. iwl_write_direct32(bus(trans), FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  726. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  727. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  728. /* Update FH chicken bits */
  729. reg_val = iwl_read_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG);
  730. iwl_write_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG,
  731. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  732. iwl_write_prph(bus(trans), SCD_QUEUECHAIN_SEL,
  733. SCD_QUEUECHAIN_SEL_ALL(trans));
  734. iwl_write_prph(bus(trans), SCD_AGGR_SEL, 0);
  735. /* initiate the queues */
  736. for (i = 0; i < hw_params(trans).max_txq_num; i++) {
  737. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(i), 0);
  738. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR, 0 | (i << 8));
  739. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  740. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  741. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  742. SCD_CONTEXT_QUEUE_OFFSET(i) +
  743. sizeof(u32),
  744. ((SCD_WIN_SIZE <<
  745. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  746. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  747. ((SCD_FRAME_LIMIT <<
  748. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  749. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  750. }
  751. iwl_write_prph(bus(trans), SCD_INTERRUPT_MASK,
  752. IWL_MASK(0, hw_params(trans).max_txq_num));
  753. /* Activate all Tx DMA/FIFO channels */
  754. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  755. /* map queues to FIFOs */
  756. if (trans->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  757. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  758. else
  759. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  760. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  761. /* make sure all queue are not stopped */
  762. memset(&trans_pcie->queue_stopped[0], 0,
  763. sizeof(trans_pcie->queue_stopped));
  764. for (i = 0; i < 4; i++)
  765. atomic_set(&trans_pcie->queue_stop_count[i], 0);
  766. /* reset to 0 to enable all the queue first */
  767. trans_pcie->txq_ctx_active_msk = 0;
  768. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  769. IWLAGN_FIRST_AMPDU_QUEUE);
  770. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  771. IWLAGN_FIRST_AMPDU_QUEUE);
  772. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  773. int fifo = queue_to_fifo[i].fifo;
  774. int ac = queue_to_fifo[i].ac;
  775. iwl_txq_ctx_activate(trans_pcie, i);
  776. if (fifo == IWL_TX_FIFO_UNUSED)
  777. continue;
  778. if (ac != IWL_AC_UNSET)
  779. iwl_set_swq_id(&trans_pcie->txq[i], ac, i);
  780. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[i],
  781. fifo, 0);
  782. }
  783. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  784. /* Enable L1-Active */
  785. iwl_clear_bits_prph(bus(trans), APMG_PCIDEV_STT_REG,
  786. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  787. }
  788. /**
  789. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  790. */
  791. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  792. {
  793. int ch, txq_id;
  794. unsigned long flags;
  795. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  796. /* Turn off all Tx DMA fifos */
  797. spin_lock_irqsave(&trans->shrd->lock, flags);
  798. iwl_trans_txq_set_sched(trans, 0);
  799. /* Stop each Tx DMA channel, and wait for it to be idle */
  800. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  801. iwl_write_direct32(bus(trans),
  802. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  803. if (iwl_poll_direct_bit(bus(trans), FH_TSSR_TX_STATUS_REG,
  804. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  805. 1000))
  806. IWL_ERR(trans, "Failing on timeout while stopping"
  807. " DMA channel %d [0x%08x]", ch,
  808. iwl_read_direct32(bus(trans),
  809. FH_TSSR_TX_STATUS_REG));
  810. }
  811. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  812. if (!trans_pcie->txq) {
  813. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  814. return 0;
  815. }
  816. /* Unmap DMA from host system and free skb's */
  817. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  818. iwl_tx_queue_unmap(trans, txq_id);
  819. return 0;
  820. }
  821. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  822. {
  823. unsigned long flags;
  824. struct iwl_trans_pcie *trans_pcie =
  825. IWL_TRANS_GET_PCIE_TRANS(trans);
  826. spin_lock_irqsave(&trans->shrd->lock, flags);
  827. iwl_disable_interrupts(trans);
  828. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  829. /* wait to make sure we flush pending tasklet*/
  830. synchronize_irq(bus(trans)->irq);
  831. tasklet_kill(&trans_pcie->irq_tasklet);
  832. }
  833. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  834. {
  835. /* stop and reset the on-board processor */
  836. iwl_write32(bus(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  837. /* tell the device to stop sending interrupts */
  838. iwl_trans_pcie_disable_sync_irq(trans);
  839. /* device going down, Stop using ICT table */
  840. iwl_disable_ict(trans);
  841. /*
  842. * If a HW restart happens during firmware loading,
  843. * then the firmware loading might call this function
  844. * and later it might be called again due to the
  845. * restart. So don't process again if the device is
  846. * already dead.
  847. */
  848. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  849. iwl_trans_tx_stop(trans);
  850. iwl_trans_rx_stop(trans);
  851. /* Power-down device's busmaster DMA clocks */
  852. iwl_write_prph(bus(trans), APMG_CLK_DIS_REG,
  853. APMG_CLK_VAL_DMA_CLK_RQT);
  854. udelay(5);
  855. }
  856. /* Make sure (redundant) we've released our request to stay awake */
  857. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  858. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  859. /* Stop the device, and put it in low power state */
  860. iwl_apm_stop(priv(trans));
  861. }
  862. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  863. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  864. u8 sta_id)
  865. {
  866. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  867. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  868. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  869. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  870. struct iwl_cmd_meta *out_meta;
  871. struct iwl_tx_queue *txq;
  872. struct iwl_queue *q;
  873. dma_addr_t phys_addr = 0;
  874. dma_addr_t txcmd_phys;
  875. dma_addr_t scratch_phys;
  876. u16 len, firstlen, secondlen;
  877. u16 seq_number = 0;
  878. u8 wait_write_ptr = 0;
  879. u8 txq_id;
  880. u8 tid = 0;
  881. bool is_agg = false;
  882. __le16 fc = hdr->frame_control;
  883. u8 hdr_len = ieee80211_hdrlen(fc);
  884. /*
  885. * Send this frame after DTIM -- there's a special queue
  886. * reserved for this for contexts that support AP mode.
  887. */
  888. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  889. txq_id = trans_pcie->mcast_queue[ctx];
  890. /*
  891. * The microcode will clear the more data
  892. * bit in the last frame it transmits.
  893. */
  894. hdr->frame_control |=
  895. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  896. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  897. txq_id = IWL_AUX_QUEUE;
  898. else
  899. txq_id =
  900. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  901. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  902. u8 *qc = NULL;
  903. struct iwl_tid_data *tid_data;
  904. qc = ieee80211_get_qos_ctl(hdr);
  905. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  906. tid_data = &trans->shrd->tid_data[sta_id][tid];
  907. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  908. return -1;
  909. seq_number = tid_data->seq_number;
  910. seq_number &= IEEE80211_SCTL_SEQ;
  911. hdr->seq_ctrl = hdr->seq_ctrl &
  912. cpu_to_le16(IEEE80211_SCTL_FRAG);
  913. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  914. seq_number += 0x10;
  915. /* aggregation is on for this <sta,tid> */
  916. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  917. WARN_ON_ONCE(tid_data->agg.state != IWL_AGG_ON);
  918. txq_id = tid_data->agg.txq_id;
  919. is_agg = true;
  920. }
  921. }
  922. /* Copy MAC header from skb into command buffer */
  923. memcpy(tx_cmd->hdr, hdr, hdr_len);
  924. txq = &trans_pcie->txq[txq_id];
  925. q = &txq->q;
  926. /* Set up driver data for this TFD */
  927. txq->skbs[q->write_ptr] = skb;
  928. txq->cmd[q->write_ptr] = dev_cmd;
  929. dev_cmd->hdr.cmd = REPLY_TX;
  930. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  931. INDEX_TO_SEQ(q->write_ptr)));
  932. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  933. out_meta = &txq->meta[q->write_ptr];
  934. /*
  935. * Use the first empty entry in this queue's command buffer array
  936. * to contain the Tx command and MAC header concatenated together
  937. * (payload data will be in another buffer).
  938. * Size of this varies, due to varying MAC header length.
  939. * If end is not dword aligned, we'll have 2 extra bytes at the end
  940. * of the MAC header (device reads on dword boundaries).
  941. * We'll tell device about this padding later.
  942. */
  943. len = sizeof(struct iwl_tx_cmd) +
  944. sizeof(struct iwl_cmd_header) + hdr_len;
  945. firstlen = (len + 3) & ~3;
  946. /* Tell NIC about any 2-byte padding after MAC header */
  947. if (firstlen != len)
  948. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  949. /* Physical address of this Tx command's header (not MAC header!),
  950. * within command buffer array. */
  951. txcmd_phys = dma_map_single(bus(trans)->dev,
  952. &dev_cmd->hdr, firstlen,
  953. DMA_BIDIRECTIONAL);
  954. if (unlikely(dma_mapping_error(bus(trans)->dev, txcmd_phys)))
  955. return -1;
  956. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  957. dma_unmap_len_set(out_meta, len, firstlen);
  958. if (!ieee80211_has_morefrags(fc)) {
  959. txq->need_update = 1;
  960. } else {
  961. wait_write_ptr = 1;
  962. txq->need_update = 0;
  963. }
  964. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  965. * if any (802.11 null frames have no payload). */
  966. secondlen = skb->len - hdr_len;
  967. if (secondlen > 0) {
  968. phys_addr = dma_map_single(bus(trans)->dev, skb->data + hdr_len,
  969. secondlen, DMA_TO_DEVICE);
  970. if (unlikely(dma_mapping_error(bus(trans)->dev, phys_addr))) {
  971. dma_unmap_single(bus(trans)->dev,
  972. dma_unmap_addr(out_meta, mapping),
  973. dma_unmap_len(out_meta, len),
  974. DMA_BIDIRECTIONAL);
  975. return -1;
  976. }
  977. }
  978. /* Attach buffers to TFD */
  979. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  980. if (secondlen > 0)
  981. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  982. secondlen, 0);
  983. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  984. offsetof(struct iwl_tx_cmd, scratch);
  985. /* take back ownership of DMA buffer to enable update */
  986. dma_sync_single_for_cpu(bus(trans)->dev, txcmd_phys, firstlen,
  987. DMA_BIDIRECTIONAL);
  988. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  989. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  990. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  991. le16_to_cpu(dev_cmd->hdr.sequence));
  992. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  993. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  994. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  995. /* Set up entry for this TFD in Tx byte-count array */
  996. if (is_agg)
  997. iwl_trans_txq_update_byte_cnt_tbl(trans, txq,
  998. le16_to_cpu(tx_cmd->len));
  999. dma_sync_single_for_device(bus(trans)->dev, txcmd_phys, firstlen,
  1000. DMA_BIDIRECTIONAL);
  1001. trace_iwlwifi_dev_tx(priv(trans),
  1002. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  1003. sizeof(struct iwl_tfd),
  1004. &dev_cmd->hdr, firstlen,
  1005. skb->data + hdr_len, secondlen);
  1006. /* Tell device the write index *just past* this latest filled TFD */
  1007. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1008. iwl_txq_update_write_ptr(trans, txq);
  1009. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  1010. trans->shrd->tid_data[sta_id][tid].tfds_in_queue++;
  1011. if (!ieee80211_has_morefrags(fc))
  1012. trans->shrd->tid_data[sta_id][tid].seq_number =
  1013. seq_number;
  1014. }
  1015. /*
  1016. * At this point the frame is "transmitted" successfully
  1017. * and we will get a TX status notification eventually,
  1018. * regardless of the value of ret. "ret" only indicates
  1019. * whether or not we should update the write pointer.
  1020. */
  1021. if (iwl_queue_space(q) < q->high_mark) {
  1022. if (wait_write_ptr) {
  1023. txq->need_update = 1;
  1024. iwl_txq_update_write_ptr(trans, txq);
  1025. } else {
  1026. iwl_stop_queue(trans, txq);
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  1032. {
  1033. /* Remove all resets to allow NIC to operate */
  1034. iwl_write32(bus(trans), CSR_RESET, 0);
  1035. }
  1036. static int iwl_trans_pcie_request_irq(struct iwl_trans *trans)
  1037. {
  1038. struct iwl_trans_pcie *trans_pcie =
  1039. IWL_TRANS_GET_PCIE_TRANS(trans);
  1040. int err;
  1041. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1042. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1043. iwl_irq_tasklet, (unsigned long)trans);
  1044. iwl_alloc_isr_ict(trans);
  1045. err = request_irq(bus(trans)->irq, iwl_isr_ict, IRQF_SHARED,
  1046. DRV_NAME, trans);
  1047. if (err) {
  1048. IWL_ERR(trans, "Error allocating IRQ %d\n", bus(trans)->irq);
  1049. iwl_free_isr_ict(trans);
  1050. return err;
  1051. }
  1052. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1053. return 0;
  1054. }
  1055. static int iwlagn_txq_check_empty(struct iwl_trans *trans,
  1056. int sta_id, u8 tid, int txq_id)
  1057. {
  1058. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1059. struct iwl_queue *q = &trans_pcie->txq[txq_id].q;
  1060. struct iwl_tid_data *tid_data = &trans->shrd->tid_data[sta_id][tid];
  1061. lockdep_assert_held(&trans->shrd->sta_lock);
  1062. switch (trans->shrd->tid_data[sta_id][tid].agg.state) {
  1063. case IWL_EMPTYING_HW_QUEUE_DELBA:
  1064. /* We are reclaiming the last packet of the */
  1065. /* aggregated HW queue */
  1066. if ((txq_id == tid_data->agg.txq_id) &&
  1067. (q->read_ptr == q->write_ptr)) {
  1068. IWL_DEBUG_HT(trans,
  1069. "HW queue empty: continue DELBA flow\n");
  1070. iwl_trans_pcie_txq_agg_disable(trans, txq_id);
  1071. tid_data->agg.state = IWL_AGG_OFF;
  1072. iwl_stop_tx_ba_trans_ready(priv(trans),
  1073. NUM_IWL_RXON_CTX,
  1074. sta_id, tid);
  1075. iwl_wake_queue(trans, &trans_pcie->txq[txq_id]);
  1076. }
  1077. break;
  1078. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1079. /* We are reclaiming the last packet of the queue */
  1080. if (tid_data->tfds_in_queue == 0) {
  1081. IWL_DEBUG_HT(trans,
  1082. "HW queue empty: continue ADDBA flow\n");
  1083. tid_data->agg.state = IWL_AGG_ON;
  1084. iwl_start_tx_ba_trans_ready(priv(trans),
  1085. NUM_IWL_RXON_CTX,
  1086. sta_id, tid);
  1087. }
  1088. break;
  1089. default:
  1090. break;
  1091. }
  1092. return 0;
  1093. }
  1094. static void iwl_free_tfds_in_queue(struct iwl_trans *trans,
  1095. int sta_id, int tid, int freed)
  1096. {
  1097. lockdep_assert_held(&trans->shrd->sta_lock);
  1098. if (trans->shrd->tid_data[sta_id][tid].tfds_in_queue >= freed)
  1099. trans->shrd->tid_data[sta_id][tid].tfds_in_queue -= freed;
  1100. else {
  1101. IWL_DEBUG_TX(trans, "free more than tfds_in_queue (%u:%d)\n",
  1102. trans->shrd->tid_data[sta_id][tid].tfds_in_queue,
  1103. freed);
  1104. trans->shrd->tid_data[sta_id][tid].tfds_in_queue = 0;
  1105. }
  1106. }
  1107. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int sta_id, int tid,
  1108. int txq_id, int ssn, u32 status,
  1109. struct sk_buff_head *skbs)
  1110. {
  1111. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1112. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1113. enum iwl_agg_state agg_state;
  1114. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1115. int tfd_num = ssn & (txq->q.n_bd - 1);
  1116. int freed = 0;
  1117. bool cond;
  1118. txq->time_stamp = jiffies;
  1119. if (txq->sched_retry) {
  1120. agg_state =
  1121. trans->shrd->tid_data[txq->sta_id][txq->tid].agg.state;
  1122. cond = (agg_state != IWL_EMPTYING_HW_QUEUE_DELBA);
  1123. } else {
  1124. cond = (status != TX_STATUS_FAIL_PASSIVE_NO_RX);
  1125. }
  1126. if (txq->q.read_ptr != tfd_num) {
  1127. IWL_DEBUG_TX_REPLY(trans, "Retry scheduler reclaim "
  1128. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  1129. ssn , tfd_num, txq_id, txq->swq_id);
  1130. freed = iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1131. if (iwl_queue_space(&txq->q) > txq->q.low_mark && cond)
  1132. iwl_wake_queue(trans, txq);
  1133. }
  1134. iwl_free_tfds_in_queue(trans, sta_id, tid, freed);
  1135. iwlagn_txq_check_empty(trans, sta_id, tid, txq_id);
  1136. }
  1137. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1138. {
  1139. iwl_trans_pcie_tx_free(trans);
  1140. iwl_trans_pcie_rx_free(trans);
  1141. free_irq(bus(trans)->irq, trans);
  1142. iwl_free_isr_ict(trans);
  1143. trans->shrd->trans = NULL;
  1144. kfree(trans);
  1145. }
  1146. #ifdef CONFIG_PM_SLEEP
  1147. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1148. {
  1149. /*
  1150. * This function is called when system goes into suspend state
  1151. * mac80211 will call iwlagn_mac_stop() from the mac80211 suspend
  1152. * function first but since iwlagn_mac_stop() has no knowledge of
  1153. * who the caller is,
  1154. * it will not call apm_ops.stop() to stop the DMA operation.
  1155. * Calling apm_ops.stop here to make sure we stop the DMA.
  1156. *
  1157. * But of course ... if we have configured WoWLAN then we did other
  1158. * things already :-)
  1159. */
  1160. if (!trans->shrd->wowlan) {
  1161. iwl_apm_stop(priv(trans));
  1162. } else {
  1163. iwl_disable_interrupts(trans);
  1164. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  1165. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1166. }
  1167. return 0;
  1168. }
  1169. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1170. {
  1171. bool hw_rfkill = false;
  1172. iwl_enable_interrupts(trans);
  1173. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  1174. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1175. hw_rfkill = true;
  1176. if (hw_rfkill)
  1177. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1178. else
  1179. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1180. iwl_set_hw_rfkill_state(priv(trans), hw_rfkill);
  1181. return 0;
  1182. }
  1183. #endif /* CONFIG_PM_SLEEP */
  1184. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1185. enum iwl_rxon_context_id ctx)
  1186. {
  1187. u8 ac, txq_id;
  1188. struct iwl_trans_pcie *trans_pcie =
  1189. IWL_TRANS_GET_PCIE_TRANS(trans);
  1190. for (ac = 0; ac < AC_NUM; ac++) {
  1191. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1192. IWL_DEBUG_INFO(trans, "Queue Status: Q[%d] %s\n",
  1193. ac,
  1194. (atomic_read(&trans_pcie->queue_stop_count[ac]) > 0)
  1195. ? "stopped" : "awake");
  1196. iwl_wake_queue(trans, &trans_pcie->txq[txq_id]);
  1197. }
  1198. }
  1199. const struct iwl_trans_ops trans_ops_pcie;
  1200. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1201. {
  1202. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1203. sizeof(struct iwl_trans_pcie),
  1204. GFP_KERNEL);
  1205. if (iwl_trans) {
  1206. struct iwl_trans_pcie *trans_pcie =
  1207. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1208. iwl_trans->ops = &trans_ops_pcie;
  1209. iwl_trans->shrd = shrd;
  1210. trans_pcie->trans = iwl_trans;
  1211. spin_lock_init(&iwl_trans->hcmd_lock);
  1212. }
  1213. return iwl_trans;
  1214. }
  1215. static void iwl_trans_pcie_stop_queue(struct iwl_trans *trans, int txq_id)
  1216. {
  1217. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1218. iwl_stop_queue(trans, &trans_pcie->txq[txq_id]);
  1219. }
  1220. #define IWL_FLUSH_WAIT_MS 2000
  1221. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1222. {
  1223. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1224. struct iwl_tx_queue *txq;
  1225. struct iwl_queue *q;
  1226. int cnt;
  1227. unsigned long now = jiffies;
  1228. int ret = 0;
  1229. /* waiting for all the tx frames complete might take a while */
  1230. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1231. if (cnt == trans->shrd->cmd_queue)
  1232. continue;
  1233. txq = &trans_pcie->txq[cnt];
  1234. q = &txq->q;
  1235. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1236. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1237. msleep(1);
  1238. if (q->read_ptr != q->write_ptr) {
  1239. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1240. ret = -ETIMEDOUT;
  1241. break;
  1242. }
  1243. }
  1244. return ret;
  1245. }
  1246. /*
  1247. * On every watchdog tick we check (latest) time stamp. If it does not
  1248. * change during timeout period and queue is not empty we reset firmware.
  1249. */
  1250. static int iwl_trans_pcie_check_stuck_queue(struct iwl_trans *trans, int cnt)
  1251. {
  1252. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1253. struct iwl_tx_queue *txq = &trans_pcie->txq[cnt];
  1254. struct iwl_queue *q = &txq->q;
  1255. unsigned long timeout;
  1256. if (q->read_ptr == q->write_ptr) {
  1257. txq->time_stamp = jiffies;
  1258. return 0;
  1259. }
  1260. timeout = txq->time_stamp +
  1261. msecs_to_jiffies(hw_params(trans).wd_timeout);
  1262. if (time_after(jiffies, timeout)) {
  1263. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", q->id,
  1264. hw_params(trans).wd_timeout);
  1265. IWL_ERR(trans, "Current read_ptr %d write_ptr %d\n",
  1266. q->read_ptr, q->write_ptr);
  1267. return 1;
  1268. }
  1269. return 0;
  1270. }
  1271. static const char *get_fh_string(int cmd)
  1272. {
  1273. switch (cmd) {
  1274. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1275. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1276. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1277. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1278. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1279. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1280. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1281. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1282. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1283. default:
  1284. return "UNKNOWN";
  1285. }
  1286. }
  1287. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1288. {
  1289. int i;
  1290. #ifdef CONFIG_IWLWIFI_DEBUG
  1291. int pos = 0;
  1292. size_t bufsz = 0;
  1293. #endif
  1294. static const u32 fh_tbl[] = {
  1295. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1296. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1297. FH_RSCSR_CHNL0_WPTR,
  1298. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1299. FH_MEM_RSSR_SHARED_CTRL_REG,
  1300. FH_MEM_RSSR_RX_STATUS_REG,
  1301. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1302. FH_TSSR_TX_STATUS_REG,
  1303. FH_TSSR_TX_ERROR_REG
  1304. };
  1305. #ifdef CONFIG_IWLWIFI_DEBUG
  1306. if (display) {
  1307. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1308. *buf = kmalloc(bufsz, GFP_KERNEL);
  1309. if (!*buf)
  1310. return -ENOMEM;
  1311. pos += scnprintf(*buf + pos, bufsz - pos,
  1312. "FH register values:\n");
  1313. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1314. pos += scnprintf(*buf + pos, bufsz - pos,
  1315. " %34s: 0X%08x\n",
  1316. get_fh_string(fh_tbl[i]),
  1317. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1318. }
  1319. return pos;
  1320. }
  1321. #endif
  1322. IWL_ERR(trans, "FH register values:\n");
  1323. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1324. IWL_ERR(trans, " %34s: 0X%08x\n",
  1325. get_fh_string(fh_tbl[i]),
  1326. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1327. }
  1328. return 0;
  1329. }
  1330. static const char *get_csr_string(int cmd)
  1331. {
  1332. switch (cmd) {
  1333. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1334. IWL_CMD(CSR_INT_COALESCING);
  1335. IWL_CMD(CSR_INT);
  1336. IWL_CMD(CSR_INT_MASK);
  1337. IWL_CMD(CSR_FH_INT_STATUS);
  1338. IWL_CMD(CSR_GPIO_IN);
  1339. IWL_CMD(CSR_RESET);
  1340. IWL_CMD(CSR_GP_CNTRL);
  1341. IWL_CMD(CSR_HW_REV);
  1342. IWL_CMD(CSR_EEPROM_REG);
  1343. IWL_CMD(CSR_EEPROM_GP);
  1344. IWL_CMD(CSR_OTP_GP_REG);
  1345. IWL_CMD(CSR_GIO_REG);
  1346. IWL_CMD(CSR_GP_UCODE_REG);
  1347. IWL_CMD(CSR_GP_DRIVER_REG);
  1348. IWL_CMD(CSR_UCODE_DRV_GP1);
  1349. IWL_CMD(CSR_UCODE_DRV_GP2);
  1350. IWL_CMD(CSR_LED_REG);
  1351. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1352. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1353. IWL_CMD(CSR_ANA_PLL_CFG);
  1354. IWL_CMD(CSR_HW_REV_WA_REG);
  1355. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1356. default:
  1357. return "UNKNOWN";
  1358. }
  1359. }
  1360. void iwl_dump_csr(struct iwl_trans *trans)
  1361. {
  1362. int i;
  1363. static const u32 csr_tbl[] = {
  1364. CSR_HW_IF_CONFIG_REG,
  1365. CSR_INT_COALESCING,
  1366. CSR_INT,
  1367. CSR_INT_MASK,
  1368. CSR_FH_INT_STATUS,
  1369. CSR_GPIO_IN,
  1370. CSR_RESET,
  1371. CSR_GP_CNTRL,
  1372. CSR_HW_REV,
  1373. CSR_EEPROM_REG,
  1374. CSR_EEPROM_GP,
  1375. CSR_OTP_GP_REG,
  1376. CSR_GIO_REG,
  1377. CSR_GP_UCODE_REG,
  1378. CSR_GP_DRIVER_REG,
  1379. CSR_UCODE_DRV_GP1,
  1380. CSR_UCODE_DRV_GP2,
  1381. CSR_LED_REG,
  1382. CSR_DRAM_INT_TBL_REG,
  1383. CSR_GIO_CHICKEN_BITS,
  1384. CSR_ANA_PLL_CFG,
  1385. CSR_HW_REV_WA_REG,
  1386. CSR_DBG_HPET_MEM_REG
  1387. };
  1388. IWL_ERR(trans, "CSR values:\n");
  1389. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1390. "CSR_INT_PERIODIC_REG)\n");
  1391. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1392. IWL_ERR(trans, " %25s: 0X%08x\n",
  1393. get_csr_string(csr_tbl[i]),
  1394. iwl_read32(bus(trans), csr_tbl[i]));
  1395. }
  1396. }
  1397. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1398. /* create and remove of files */
  1399. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1400. if (!debugfs_create_file(#name, mode, parent, trans, \
  1401. &iwl_dbgfs_##name##_ops)) \
  1402. return -ENOMEM; \
  1403. } while (0)
  1404. /* file operation */
  1405. #define DEBUGFS_READ_FUNC(name) \
  1406. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1407. char __user *user_buf, \
  1408. size_t count, loff_t *ppos);
  1409. #define DEBUGFS_WRITE_FUNC(name) \
  1410. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1411. const char __user *user_buf, \
  1412. size_t count, loff_t *ppos);
  1413. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1414. {
  1415. file->private_data = inode->i_private;
  1416. return 0;
  1417. }
  1418. #define DEBUGFS_READ_FILE_OPS(name) \
  1419. DEBUGFS_READ_FUNC(name); \
  1420. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1421. .read = iwl_dbgfs_##name##_read, \
  1422. .open = iwl_dbgfs_open_file_generic, \
  1423. .llseek = generic_file_llseek, \
  1424. };
  1425. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1426. DEBUGFS_WRITE_FUNC(name); \
  1427. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1428. .write = iwl_dbgfs_##name##_write, \
  1429. .open = iwl_dbgfs_open_file_generic, \
  1430. .llseek = generic_file_llseek, \
  1431. };
  1432. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1433. DEBUGFS_READ_FUNC(name); \
  1434. DEBUGFS_WRITE_FUNC(name); \
  1435. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1436. .write = iwl_dbgfs_##name##_write, \
  1437. .read = iwl_dbgfs_##name##_read, \
  1438. .open = iwl_dbgfs_open_file_generic, \
  1439. .llseek = generic_file_llseek, \
  1440. };
  1441. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1442. char __user *user_buf,
  1443. size_t count, loff_t *ppos)
  1444. {
  1445. struct iwl_trans *trans = file->private_data;
  1446. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1447. struct iwl_tx_queue *txq;
  1448. struct iwl_queue *q;
  1449. char *buf;
  1450. int pos = 0;
  1451. int cnt;
  1452. int ret;
  1453. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1454. if (!trans_pcie->txq) {
  1455. IWL_ERR(trans, "txq not ready\n");
  1456. return -EAGAIN;
  1457. }
  1458. buf = kzalloc(bufsz, GFP_KERNEL);
  1459. if (!buf)
  1460. return -ENOMEM;
  1461. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1462. txq = &trans_pcie->txq[cnt];
  1463. q = &txq->q;
  1464. pos += scnprintf(buf + pos, bufsz - pos,
  1465. "hwq %.2d: read=%u write=%u stop=%d"
  1466. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1467. cnt, q->read_ptr, q->write_ptr,
  1468. !!test_bit(cnt, trans_pcie->queue_stopped),
  1469. txq->swq_id, txq->swq_id & 3,
  1470. (txq->swq_id >> 2) & 0x1f);
  1471. if (cnt >= 4)
  1472. continue;
  1473. /* for the ACs, display the stop count too */
  1474. pos += scnprintf(buf + pos, bufsz - pos,
  1475. " stop-count: %d\n",
  1476. atomic_read(&trans_pcie->queue_stop_count[cnt]));
  1477. }
  1478. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1479. kfree(buf);
  1480. return ret;
  1481. }
  1482. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1483. char __user *user_buf,
  1484. size_t count, loff_t *ppos) {
  1485. struct iwl_trans *trans = file->private_data;
  1486. struct iwl_trans_pcie *trans_pcie =
  1487. IWL_TRANS_GET_PCIE_TRANS(trans);
  1488. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1489. char buf[256];
  1490. int pos = 0;
  1491. const size_t bufsz = sizeof(buf);
  1492. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1493. rxq->read);
  1494. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1495. rxq->write);
  1496. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1497. rxq->free_count);
  1498. if (rxq->rb_stts) {
  1499. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1500. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1501. } else {
  1502. pos += scnprintf(buf + pos, bufsz - pos,
  1503. "closed_rb_num: Not Allocated\n");
  1504. }
  1505. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1506. }
  1507. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1508. char __user *user_buf,
  1509. size_t count, loff_t *ppos)
  1510. {
  1511. struct iwl_trans *trans = file->private_data;
  1512. char *buf;
  1513. int pos = 0;
  1514. ssize_t ret = -ENOMEM;
  1515. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1516. if (buf) {
  1517. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1518. kfree(buf);
  1519. }
  1520. return ret;
  1521. }
  1522. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1523. const char __user *user_buf,
  1524. size_t count, loff_t *ppos)
  1525. {
  1526. struct iwl_trans *trans = file->private_data;
  1527. u32 event_log_flag;
  1528. char buf[8];
  1529. int buf_size;
  1530. memset(buf, 0, sizeof(buf));
  1531. buf_size = min(count, sizeof(buf) - 1);
  1532. if (copy_from_user(buf, user_buf, buf_size))
  1533. return -EFAULT;
  1534. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1535. return -EFAULT;
  1536. if (event_log_flag == 1)
  1537. iwl_dump_nic_event_log(trans, true, NULL, false);
  1538. return count;
  1539. }
  1540. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1541. char __user *user_buf,
  1542. size_t count, loff_t *ppos) {
  1543. struct iwl_trans *trans = file->private_data;
  1544. struct iwl_trans_pcie *trans_pcie =
  1545. IWL_TRANS_GET_PCIE_TRANS(trans);
  1546. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1547. int pos = 0;
  1548. char *buf;
  1549. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1550. ssize_t ret;
  1551. buf = kzalloc(bufsz, GFP_KERNEL);
  1552. if (!buf) {
  1553. IWL_ERR(trans, "Can not allocate Buffer\n");
  1554. return -ENOMEM;
  1555. }
  1556. pos += scnprintf(buf + pos, bufsz - pos,
  1557. "Interrupt Statistics Report:\n");
  1558. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1559. isr_stats->hw);
  1560. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1561. isr_stats->sw);
  1562. if (isr_stats->sw || isr_stats->hw) {
  1563. pos += scnprintf(buf + pos, bufsz - pos,
  1564. "\tLast Restarting Code: 0x%X\n",
  1565. isr_stats->err_code);
  1566. }
  1567. #ifdef CONFIG_IWLWIFI_DEBUG
  1568. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1569. isr_stats->sch);
  1570. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1571. isr_stats->alive);
  1572. #endif
  1573. pos += scnprintf(buf + pos, bufsz - pos,
  1574. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1575. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1576. isr_stats->ctkill);
  1577. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1578. isr_stats->wakeup);
  1579. pos += scnprintf(buf + pos, bufsz - pos,
  1580. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1581. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1582. isr_stats->tx);
  1583. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1584. isr_stats->unhandled);
  1585. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1586. kfree(buf);
  1587. return ret;
  1588. }
  1589. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1590. const char __user *user_buf,
  1591. size_t count, loff_t *ppos)
  1592. {
  1593. struct iwl_trans *trans = file->private_data;
  1594. struct iwl_trans_pcie *trans_pcie =
  1595. IWL_TRANS_GET_PCIE_TRANS(trans);
  1596. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1597. char buf[8];
  1598. int buf_size;
  1599. u32 reset_flag;
  1600. memset(buf, 0, sizeof(buf));
  1601. buf_size = min(count, sizeof(buf) - 1);
  1602. if (copy_from_user(buf, user_buf, buf_size))
  1603. return -EFAULT;
  1604. if (sscanf(buf, "%x", &reset_flag) != 1)
  1605. return -EFAULT;
  1606. if (reset_flag == 0)
  1607. memset(isr_stats, 0, sizeof(*isr_stats));
  1608. return count;
  1609. }
  1610. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1611. const char __user *user_buf,
  1612. size_t count, loff_t *ppos)
  1613. {
  1614. struct iwl_trans *trans = file->private_data;
  1615. char buf[8];
  1616. int buf_size;
  1617. int csr;
  1618. memset(buf, 0, sizeof(buf));
  1619. buf_size = min(count, sizeof(buf) - 1);
  1620. if (copy_from_user(buf, user_buf, buf_size))
  1621. return -EFAULT;
  1622. if (sscanf(buf, "%d", &csr) != 1)
  1623. return -EFAULT;
  1624. iwl_dump_csr(trans);
  1625. return count;
  1626. }
  1627. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1628. char __user *user_buf,
  1629. size_t count, loff_t *ppos)
  1630. {
  1631. struct iwl_trans *trans = file->private_data;
  1632. char *buf;
  1633. int pos = 0;
  1634. ssize_t ret = -EFAULT;
  1635. ret = pos = iwl_dump_fh(trans, &buf, true);
  1636. if (buf) {
  1637. ret = simple_read_from_buffer(user_buf,
  1638. count, ppos, buf, pos);
  1639. kfree(buf);
  1640. }
  1641. return ret;
  1642. }
  1643. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1644. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1645. DEBUGFS_READ_FILE_OPS(fh_reg);
  1646. DEBUGFS_READ_FILE_OPS(rx_queue);
  1647. DEBUGFS_READ_FILE_OPS(tx_queue);
  1648. DEBUGFS_WRITE_FILE_OPS(csr);
  1649. /*
  1650. * Create the debugfs files and directories
  1651. *
  1652. */
  1653. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1654. struct dentry *dir)
  1655. {
  1656. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1657. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1658. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1659. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1660. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1661. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1662. return 0;
  1663. }
  1664. #else
  1665. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1666. struct dentry *dir)
  1667. { return 0; }
  1668. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1669. const struct iwl_trans_ops trans_ops_pcie = {
  1670. .alloc = iwl_trans_pcie_alloc,
  1671. .request_irq = iwl_trans_pcie_request_irq,
  1672. .start_device = iwl_trans_pcie_start_device,
  1673. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1674. .stop_device = iwl_trans_pcie_stop_device,
  1675. .tx_start = iwl_trans_pcie_tx_start,
  1676. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1677. .send_cmd = iwl_trans_pcie_send_cmd,
  1678. .tx = iwl_trans_pcie_tx,
  1679. .reclaim = iwl_trans_pcie_reclaim,
  1680. .tx_agg_disable = iwl_trans_pcie_tx_agg_disable,
  1681. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1682. .tx_agg_setup = iwl_trans_pcie_tx_agg_setup,
  1683. .kick_nic = iwl_trans_pcie_kick_nic,
  1684. .free = iwl_trans_pcie_free,
  1685. .stop_queue = iwl_trans_pcie_stop_queue,
  1686. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1687. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1688. .check_stuck_queue = iwl_trans_pcie_check_stuck_queue,
  1689. #ifdef CONFIG_PM_SLEEP
  1690. .suspend = iwl_trans_pcie_suspend,
  1691. .resume = iwl_trans_pcie_resume,
  1692. #endif
  1693. };