iwl-trans.c 52 KB

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