iwl-trans.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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. #define IWL_AC_UNSET -1
  597. struct queue_to_fifo_ac {
  598. s8 fifo, ac;
  599. };
  600. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  601. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  602. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  603. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  604. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  605. { IWLAGN_CMD_FIFO_NUM, 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. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  611. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  612. };
  613. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  614. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  615. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  616. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  617. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  618. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  619. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  620. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  621. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  622. { IWL_TX_FIFO_BE_IPAN, 2, },
  623. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  624. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  625. };
  626. static const u8 iwlagn_bss_ac_to_fifo[] = {
  627. IWL_TX_FIFO_VO,
  628. IWL_TX_FIFO_VI,
  629. IWL_TX_FIFO_BE,
  630. IWL_TX_FIFO_BK,
  631. };
  632. static const u8 iwlagn_bss_ac_to_queue[] = {
  633. 0, 1, 2, 3,
  634. };
  635. static const u8 iwlagn_pan_ac_to_fifo[] = {
  636. IWL_TX_FIFO_VO_IPAN,
  637. IWL_TX_FIFO_VI_IPAN,
  638. IWL_TX_FIFO_BE_IPAN,
  639. IWL_TX_FIFO_BK_IPAN,
  640. };
  641. static const u8 iwlagn_pan_ac_to_queue[] = {
  642. 7, 6, 5, 4,
  643. };
  644. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  645. {
  646. int ret;
  647. struct iwl_priv *priv = priv(trans);
  648. struct iwl_trans_pcie *trans_pcie =
  649. IWL_TRANS_GET_PCIE_TRANS(trans);
  650. priv->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  651. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  652. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  653. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  654. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  655. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  656. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  657. if ((hw_params(priv).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  658. iwl_trans_pcie_prepare_card_hw(trans)) {
  659. IWL_WARN(trans, "Exit HW not ready\n");
  660. return -EIO;
  661. }
  662. /* If platform's RF_KILL switch is NOT set to KILL */
  663. if (iwl_read32(bus(trans), CSR_GP_CNTRL) &
  664. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  665. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  666. else
  667. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  668. if (iwl_is_rfkill(trans->shrd)) {
  669. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  670. iwl_enable_interrupts(trans);
  671. return -ERFKILL;
  672. }
  673. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  674. ret = iwl_nic_init(trans);
  675. if (ret) {
  676. IWL_ERR(trans, "Unable to init nic\n");
  677. return ret;
  678. }
  679. /* make sure rfkill handshake bits are cleared */
  680. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  681. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR,
  682. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  683. /* clear (again), then enable host interrupts */
  684. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  685. iwl_enable_interrupts(trans);
  686. /* really make sure rfkill handshake bits are cleared */
  687. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  688. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  689. return 0;
  690. }
  691. /*
  692. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  693. * must be called under priv->shrd->lock and mac access
  694. */
  695. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  696. {
  697. iwl_write_prph(bus(trans), SCD_TXFACT, mask);
  698. }
  699. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  700. {
  701. const struct queue_to_fifo_ac *queue_to_fifo;
  702. struct iwl_rxon_context *ctx;
  703. struct iwl_priv *priv = priv(trans);
  704. struct iwl_trans_pcie *trans_pcie =
  705. IWL_TRANS_GET_PCIE_TRANS(trans);
  706. u32 a;
  707. unsigned long flags;
  708. int i, chan;
  709. u32 reg_val;
  710. spin_lock_irqsave(&trans->shrd->lock, flags);
  711. trans_pcie->scd_base_addr =
  712. iwl_read_prph(bus(trans), SCD_SRAM_BASE_ADDR);
  713. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  714. /* reset conext data memory */
  715. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  716. a += 4)
  717. iwl_write_targ_mem(bus(trans), a, 0);
  718. /* reset tx status memory */
  719. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  720. a += 4)
  721. iwl_write_targ_mem(bus(trans), a, 0);
  722. for (; a < trans_pcie->scd_base_addr +
  723. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(priv).max_txq_num);
  724. a += 4)
  725. iwl_write_targ_mem(bus(trans), a, 0);
  726. iwl_write_prph(bus(trans), SCD_DRAM_BASE_ADDR,
  727. trans_pcie->scd_bc_tbls.dma >> 10);
  728. /* Enable DMA channel */
  729. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  730. iwl_write_direct32(bus(trans), FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  731. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  732. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  733. /* Update FH chicken bits */
  734. reg_val = iwl_read_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG);
  735. iwl_write_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG,
  736. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  737. iwl_write_prph(bus(trans), SCD_QUEUECHAIN_SEL,
  738. SCD_QUEUECHAIN_SEL_ALL(priv));
  739. iwl_write_prph(bus(trans), SCD_AGGR_SEL, 0);
  740. /* initiate the queues */
  741. for (i = 0; i < hw_params(priv).max_txq_num; i++) {
  742. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(i), 0);
  743. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR, 0 | (i << 8));
  744. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  745. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  746. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  747. SCD_CONTEXT_QUEUE_OFFSET(i) +
  748. sizeof(u32),
  749. ((SCD_WIN_SIZE <<
  750. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  751. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  752. ((SCD_FRAME_LIMIT <<
  753. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  754. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  755. }
  756. iwl_write_prph(bus(trans), SCD_INTERRUPT_MASK,
  757. IWL_MASK(0, hw_params(trans).max_txq_num));
  758. /* Activate all Tx DMA/FIFO channels */
  759. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  760. /* map queues to FIFOs */
  761. if (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  762. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  763. else
  764. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  765. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  766. /* make sure all queue are not stopped */
  767. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  768. for (i = 0; i < 4; i++)
  769. atomic_set(&priv->queue_stop_count[i], 0);
  770. for_each_context(priv, ctx)
  771. ctx->last_tx_rejected = false;
  772. /* reset to 0 to enable all the queue first */
  773. priv->txq_ctx_active_msk = 0;
  774. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  775. IWLAGN_FIRST_AMPDU_QUEUE);
  776. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  777. IWLAGN_FIRST_AMPDU_QUEUE);
  778. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  779. int fifo = queue_to_fifo[i].fifo;
  780. int ac = queue_to_fifo[i].ac;
  781. iwl_txq_ctx_activate(priv, i);
  782. if (fifo == IWL_TX_FIFO_UNUSED)
  783. continue;
  784. if (ac != IWL_AC_UNSET)
  785. iwl_set_swq_id(&priv->txq[i], ac, i);
  786. iwl_trans_tx_queue_set_status(priv, &priv->txq[i], fifo, 0);
  787. }
  788. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  789. /* Enable L1-Active */
  790. iwl_clear_bits_prph(bus(trans), APMG_PCIDEV_STT_REG,
  791. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  792. }
  793. /**
  794. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  795. */
  796. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  797. {
  798. int ch, txq_id;
  799. unsigned long flags;
  800. struct iwl_priv *priv = priv(trans);
  801. /* Turn off all Tx DMA fifos */
  802. spin_lock_irqsave(&trans->shrd->lock, flags);
  803. iwl_trans_txq_set_sched(trans, 0);
  804. /* Stop each Tx DMA channel, and wait for it to be idle */
  805. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  806. iwl_write_direct32(bus(trans),
  807. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  808. if (iwl_poll_direct_bit(bus(trans), FH_TSSR_TX_STATUS_REG,
  809. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  810. 1000))
  811. IWL_ERR(trans, "Failing on timeout while stopping"
  812. " DMA channel %d [0x%08x]", ch,
  813. iwl_read_direct32(bus(trans),
  814. FH_TSSR_TX_STATUS_REG));
  815. }
  816. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  817. if (!priv->txq) {
  818. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  819. return 0;
  820. }
  821. /* Unmap DMA from host system and free skb's */
  822. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  823. iwl_tx_queue_unmap(trans, txq_id);
  824. return 0;
  825. }
  826. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  827. {
  828. unsigned long flags;
  829. struct iwl_trans_pcie *trans_pcie =
  830. IWL_TRANS_GET_PCIE_TRANS(trans);
  831. spin_lock_irqsave(&trans->shrd->lock, flags);
  832. iwl_disable_interrupts(trans);
  833. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  834. /* wait to make sure we flush pending tasklet*/
  835. synchronize_irq(bus(trans)->irq);
  836. tasklet_kill(&trans_pcie->irq_tasklet);
  837. }
  838. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  839. {
  840. /* stop and reset the on-board processor */
  841. iwl_write32(bus(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  842. /* tell the device to stop sending interrupts */
  843. iwl_trans_pcie_disable_sync_irq(trans);
  844. /* device going down, Stop using ICT table */
  845. iwl_disable_ict(trans);
  846. /*
  847. * If a HW restart happens during firmware loading,
  848. * then the firmware loading might call this function
  849. * and later it might be called again due to the
  850. * restart. So don't process again if the device is
  851. * already dead.
  852. */
  853. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  854. iwl_trans_tx_stop(trans);
  855. iwl_trans_rx_stop(trans);
  856. /* Power-down device's busmaster DMA clocks */
  857. iwl_write_prph(bus(trans), APMG_CLK_DIS_REG,
  858. APMG_CLK_VAL_DMA_CLK_RQT);
  859. udelay(5);
  860. }
  861. /* Make sure (redundant) we've released our request to stay awake */
  862. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  863. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  864. /* Stop the device, and put it in low power state */
  865. iwl_apm_stop(priv(trans));
  866. }
  867. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  868. struct iwl_device_cmd *dev_cmd, u8 ctx, u8 sta_id)
  869. {
  870. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  871. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  872. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  873. struct iwl_tx_cmd *tx_cmd = &dev_cmd->cmd.tx;
  874. struct iwl_cmd_meta *out_meta;
  875. struct iwl_tx_queue *txq;
  876. struct iwl_queue *q;
  877. dma_addr_t phys_addr = 0;
  878. dma_addr_t txcmd_phys;
  879. dma_addr_t scratch_phys;
  880. u16 len, firstlen, secondlen;
  881. u16 seq_number = 0;
  882. u8 wait_write_ptr = 0;
  883. u8 txq_id;
  884. u8 tid = 0;
  885. bool is_agg = false;
  886. __le16 fc = hdr->frame_control;
  887. u8 hdr_len = ieee80211_hdrlen(fc);
  888. /*
  889. * Send this frame after DTIM -- there's a special queue
  890. * reserved for this for contexts that support AP mode.
  891. */
  892. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  893. txq_id = trans_pcie->mcast_queue[ctx];
  894. /*
  895. * The microcode will clear the more data
  896. * bit in the last frame it transmits.
  897. */
  898. hdr->frame_control |=
  899. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  900. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  901. txq_id = IWL_AUX_QUEUE;
  902. else
  903. txq_id =
  904. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  905. if (ieee80211_is_data_qos(fc)) {
  906. u8 *qc = NULL;
  907. struct iwl_tid_data *tid_data;
  908. qc = ieee80211_get_qos_ctl(hdr);
  909. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  910. tid_data = &trans->shrd->tid_data[sta_id][tid];
  911. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  912. return -1;
  913. seq_number = tid_data->seq_number;
  914. seq_number &= IEEE80211_SCTL_SEQ;
  915. hdr->seq_ctrl = hdr->seq_ctrl &
  916. cpu_to_le16(IEEE80211_SCTL_FRAG);
  917. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  918. seq_number += 0x10;
  919. /* aggregation is on for this <sta,tid> */
  920. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  921. tid_data->agg.state == IWL_AGG_ON) {
  922. txq_id = tid_data->agg.txq_id;
  923. is_agg = true;
  924. }
  925. }
  926. txq = &priv(trans)->txq[txq_id];
  927. q = &txq->q;
  928. /* Set up driver data for this TFD */
  929. txq->skbs[q->write_ptr] = skb;
  930. txq->cmd[q->write_ptr] = dev_cmd;
  931. dev_cmd->hdr.cmd = REPLY_TX;
  932. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  933. INDEX_TO_SEQ(q->write_ptr)));
  934. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  935. out_meta = &txq->meta[q->write_ptr];
  936. /*
  937. * Use the first empty entry in this queue's command buffer array
  938. * to contain the Tx command and MAC header concatenated together
  939. * (payload data will be in another buffer).
  940. * Size of this varies, due to varying MAC header length.
  941. * If end is not dword aligned, we'll have 2 extra bytes at the end
  942. * of the MAC header (device reads on dword boundaries).
  943. * We'll tell device about this padding later.
  944. */
  945. len = sizeof(struct iwl_tx_cmd) +
  946. sizeof(struct iwl_cmd_header) + hdr_len;
  947. firstlen = (len + 3) & ~3;
  948. /* Tell NIC about any 2-byte padding after MAC header */
  949. if (firstlen != len)
  950. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  951. /* Physical address of this Tx command's header (not MAC header!),
  952. * within command buffer array. */
  953. txcmd_phys = dma_map_single(bus(trans)->dev,
  954. &dev_cmd->hdr, firstlen,
  955. DMA_BIDIRECTIONAL);
  956. if (unlikely(dma_mapping_error(bus(trans)->dev, txcmd_phys)))
  957. return -1;
  958. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  959. dma_unmap_len_set(out_meta, len, firstlen);
  960. if (!ieee80211_has_morefrags(fc)) {
  961. txq->need_update = 1;
  962. } else {
  963. wait_write_ptr = 1;
  964. txq->need_update = 0;
  965. }
  966. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  967. * if any (802.11 null frames have no payload). */
  968. secondlen = skb->len - hdr_len;
  969. if (secondlen > 0) {
  970. phys_addr = dma_map_single(bus(trans)->dev, skb->data + hdr_len,
  971. secondlen, DMA_TO_DEVICE);
  972. if (unlikely(dma_mapping_error(bus(trans)->dev, phys_addr))) {
  973. dma_unmap_single(bus(trans)->dev,
  974. dma_unmap_addr(out_meta, mapping),
  975. dma_unmap_len(out_meta, len),
  976. DMA_BIDIRECTIONAL);
  977. return -1;
  978. }
  979. }
  980. /* Attach buffers to TFD */
  981. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  982. if (secondlen > 0)
  983. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  984. secondlen, 0);
  985. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  986. offsetof(struct iwl_tx_cmd, scratch);
  987. /* take back ownership of DMA buffer to enable update */
  988. dma_sync_single_for_cpu(bus(trans)->dev, txcmd_phys, firstlen,
  989. DMA_BIDIRECTIONAL);
  990. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  991. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  992. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  993. le16_to_cpu(dev_cmd->hdr.sequence));
  994. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  995. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  996. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  997. /* Set up entry for this TFD in Tx byte-count array */
  998. if (is_agg)
  999. iwl_trans_txq_update_byte_cnt_tbl(trans, txq,
  1000. le16_to_cpu(tx_cmd->len));
  1001. dma_sync_single_for_device(bus(trans)->dev, txcmd_phys, firstlen,
  1002. DMA_BIDIRECTIONAL);
  1003. trace_iwlwifi_dev_tx(priv(trans),
  1004. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  1005. sizeof(struct iwl_tfd),
  1006. &dev_cmd->hdr, firstlen,
  1007. skb->data + hdr_len, secondlen);
  1008. /* Tell device the write index *just past* this latest filled TFD */
  1009. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1010. iwl_txq_update_write_ptr(trans, txq);
  1011. if (ieee80211_is_data_qos(fc)) {
  1012. trans->shrd->tid_data[sta_id][tid].tfds_in_queue++;
  1013. if (!ieee80211_has_morefrags(fc))
  1014. trans->shrd->tid_data[sta_id][tid].seq_number =
  1015. seq_number;
  1016. }
  1017. /*
  1018. * At this point the frame is "transmitted" successfully
  1019. * and we will get a TX status notification eventually,
  1020. * regardless of the value of ret. "ret" only indicates
  1021. * whether or not we should update the write pointer.
  1022. */
  1023. if (iwl_queue_space(q) < q->high_mark) {
  1024. if (wait_write_ptr) {
  1025. txq->need_update = 1;
  1026. iwl_txq_update_write_ptr(trans, txq);
  1027. } else {
  1028. iwl_stop_queue(priv(trans), txq);
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  1034. {
  1035. /* Remove all resets to allow NIC to operate */
  1036. iwl_write32(bus(trans), CSR_RESET, 0);
  1037. }
  1038. static int iwl_trans_pcie_request_irq(struct iwl_trans *trans)
  1039. {
  1040. struct iwl_trans_pcie *trans_pcie =
  1041. IWL_TRANS_GET_PCIE_TRANS(trans);
  1042. int err;
  1043. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1044. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1045. iwl_irq_tasklet, (unsigned long)trans);
  1046. iwl_alloc_isr_ict(trans);
  1047. err = request_irq(bus(trans)->irq, iwl_isr_ict, IRQF_SHARED,
  1048. DRV_NAME, trans);
  1049. if (err) {
  1050. IWL_ERR(trans, "Error allocating IRQ %d\n", bus(trans)->irq);
  1051. iwl_free_isr_ict(trans);
  1052. return err;
  1053. }
  1054. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1055. return 0;
  1056. }
  1057. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id,
  1058. int ssn, u32 status, struct sk_buff_head *skbs)
  1059. {
  1060. struct iwl_priv *priv = priv(trans);
  1061. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1062. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1063. int tfd_num = ssn & (txq->q.n_bd - 1);
  1064. u8 agg_state;
  1065. bool cond;
  1066. if (txq->sched_retry) {
  1067. agg_state =
  1068. priv->shrd->tid_data[txq->sta_id][txq->tid].agg.state;
  1069. cond = (agg_state != IWL_EMPTYING_HW_QUEUE_DELBA);
  1070. } else {
  1071. cond = (status != TX_STATUS_FAIL_PASSIVE_NO_RX);
  1072. }
  1073. if (txq->q.read_ptr != tfd_num) {
  1074. IWL_DEBUG_TX_REPLY(trans, "Retry scheduler reclaim "
  1075. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  1076. ssn , tfd_num, txq_id, txq->swq_id);
  1077. iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1078. if (iwl_queue_space(&txq->q) > txq->q.low_mark && cond)
  1079. iwl_wake_queue(priv, txq);
  1080. }
  1081. }
  1082. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1083. {
  1084. iwl_trans_pcie_tx_free(trans);
  1085. iwl_trans_pcie_rx_free(trans);
  1086. free_irq(bus(trans)->irq, trans);
  1087. iwl_free_isr_ict(trans);
  1088. trans->shrd->trans = NULL;
  1089. kfree(trans);
  1090. }
  1091. #ifdef CONFIG_PM
  1092. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1093. {
  1094. /*
  1095. * This function is called when system goes into suspend state
  1096. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  1097. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  1098. * it will not call apm_ops.stop() to stop the DMA operation.
  1099. * Calling apm_ops.stop here to make sure we stop the DMA.
  1100. *
  1101. * But of course ... if we have configured WoWLAN then we did other
  1102. * things already :-)
  1103. */
  1104. if (!trans->shrd->wowlan)
  1105. iwl_apm_stop(priv(trans));
  1106. return 0;
  1107. }
  1108. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1109. {
  1110. bool hw_rfkill = false;
  1111. iwl_enable_interrupts(trans);
  1112. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  1113. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1114. hw_rfkill = true;
  1115. if (hw_rfkill)
  1116. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1117. else
  1118. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1119. wiphy_rfkill_set_hw_state(priv(trans)->hw->wiphy, hw_rfkill);
  1120. return 0;
  1121. }
  1122. #else /* CONFIG_PM */
  1123. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1124. { return 0; }
  1125. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1126. { return 0; }
  1127. #endif /* CONFIG_PM */
  1128. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1129. u8 ctx)
  1130. {
  1131. u8 ac, txq_id;
  1132. struct iwl_trans_pcie *trans_pcie =
  1133. IWL_TRANS_GET_PCIE_TRANS(trans);
  1134. for (ac = 0; ac < AC_NUM; ac++) {
  1135. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1136. IWL_DEBUG_INFO(trans, "Queue Status: Q[%d] %s\n",
  1137. ac,
  1138. (atomic_read(&priv(trans)->queue_stop_count[ac]) > 0)
  1139. ? "stopped" : "awake");
  1140. iwl_wake_queue(priv(trans), &priv(trans)->txq[txq_id]);
  1141. }
  1142. }
  1143. const struct iwl_trans_ops trans_ops_pcie;
  1144. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1145. {
  1146. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1147. sizeof(struct iwl_trans_pcie),
  1148. GFP_KERNEL);
  1149. if (iwl_trans) {
  1150. struct iwl_trans_pcie *trans_pcie =
  1151. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1152. iwl_trans->ops = &trans_ops_pcie;
  1153. iwl_trans->shrd = shrd;
  1154. trans_pcie->trans = iwl_trans;
  1155. spin_lock_init(&iwl_trans->hcmd_lock);
  1156. }
  1157. return iwl_trans;
  1158. }
  1159. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1160. /* create and remove of files */
  1161. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1162. if (!debugfs_create_file(#name, mode, parent, trans, \
  1163. &iwl_dbgfs_##name##_ops)) \
  1164. return -ENOMEM; \
  1165. } while (0)
  1166. /* file operation */
  1167. #define DEBUGFS_READ_FUNC(name) \
  1168. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1169. char __user *user_buf, \
  1170. size_t count, loff_t *ppos);
  1171. #define DEBUGFS_WRITE_FUNC(name) \
  1172. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1173. const char __user *user_buf, \
  1174. size_t count, loff_t *ppos);
  1175. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1176. {
  1177. file->private_data = inode->i_private;
  1178. return 0;
  1179. }
  1180. #define DEBUGFS_READ_FILE_OPS(name) \
  1181. DEBUGFS_READ_FUNC(name); \
  1182. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1183. .read = iwl_dbgfs_##name##_read, \
  1184. .open = iwl_dbgfs_open_file_generic, \
  1185. .llseek = generic_file_llseek, \
  1186. };
  1187. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1188. DEBUGFS_WRITE_FUNC(name); \
  1189. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1190. .write = iwl_dbgfs_##name##_write, \
  1191. .open = iwl_dbgfs_open_file_generic, \
  1192. .llseek = generic_file_llseek, \
  1193. };
  1194. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1195. DEBUGFS_READ_FUNC(name); \
  1196. DEBUGFS_WRITE_FUNC(name); \
  1197. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1198. .write = iwl_dbgfs_##name##_write, \
  1199. .read = iwl_dbgfs_##name##_read, \
  1200. .open = iwl_dbgfs_open_file_generic, \
  1201. .llseek = generic_file_llseek, \
  1202. };
  1203. static ssize_t iwl_dbgfs_traffic_log_read(struct file *file,
  1204. char __user *user_buf,
  1205. size_t count, loff_t *ppos)
  1206. {
  1207. struct iwl_trans *trans = file->private_data;
  1208. struct iwl_priv *priv = priv(trans);
  1209. int pos = 0, ofs = 0;
  1210. int cnt = 0, entry;
  1211. struct iwl_trans_pcie *trans_pcie =
  1212. IWL_TRANS_GET_PCIE_TRANS(trans);
  1213. struct iwl_tx_queue *txq;
  1214. struct iwl_queue *q;
  1215. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1216. char *buf;
  1217. int bufsz = ((IWL_TRAFFIC_ENTRIES * IWL_TRAFFIC_ENTRY_SIZE * 64) * 2) +
  1218. (hw_params(trans).max_txq_num * 32 * 8) + 400;
  1219. const u8 *ptr;
  1220. ssize_t ret;
  1221. if (!priv->txq) {
  1222. IWL_ERR(trans, "txq not ready\n");
  1223. return -EAGAIN;
  1224. }
  1225. buf = kzalloc(bufsz, GFP_KERNEL);
  1226. if (!buf) {
  1227. IWL_ERR(trans, "Can not allocate buffer\n");
  1228. return -ENOMEM;
  1229. }
  1230. pos += scnprintf(buf + pos, bufsz - pos, "Tx Queue\n");
  1231. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1232. txq = &priv->txq[cnt];
  1233. q = &txq->q;
  1234. pos += scnprintf(buf + pos, bufsz - pos,
  1235. "q[%d]: read_ptr: %u, write_ptr: %u\n",
  1236. cnt, q->read_ptr, q->write_ptr);
  1237. }
  1238. if (priv->tx_traffic &&
  1239. (iwl_get_debug_level(trans->shrd) & IWL_DL_TX)) {
  1240. ptr = priv->tx_traffic;
  1241. pos += scnprintf(buf + pos, bufsz - pos,
  1242. "Tx Traffic idx: %u\n", priv->tx_traffic_idx);
  1243. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1244. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1245. entry++, ofs += 16) {
  1246. pos += scnprintf(buf + pos, bufsz - pos,
  1247. "0x%.4x ", ofs);
  1248. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1249. buf + pos, bufsz - pos, 0);
  1250. pos += strlen(buf + pos);
  1251. if (bufsz - pos > 0)
  1252. buf[pos++] = '\n';
  1253. }
  1254. }
  1255. }
  1256. pos += scnprintf(buf + pos, bufsz - pos, "Rx Queue\n");
  1257. pos += scnprintf(buf + pos, bufsz - pos,
  1258. "read: %u, write: %u\n",
  1259. rxq->read, rxq->write);
  1260. if (priv->rx_traffic &&
  1261. (iwl_get_debug_level(trans->shrd) & IWL_DL_RX)) {
  1262. ptr = priv->rx_traffic;
  1263. pos += scnprintf(buf + pos, bufsz - pos,
  1264. "Rx Traffic idx: %u\n", priv->rx_traffic_idx);
  1265. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1266. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1267. entry++, ofs += 16) {
  1268. pos += scnprintf(buf + pos, bufsz - pos,
  1269. "0x%.4x ", ofs);
  1270. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1271. buf + pos, bufsz - pos, 0);
  1272. pos += strlen(buf + pos);
  1273. if (bufsz - pos > 0)
  1274. buf[pos++] = '\n';
  1275. }
  1276. }
  1277. }
  1278. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1279. kfree(buf);
  1280. return ret;
  1281. }
  1282. static ssize_t iwl_dbgfs_traffic_log_write(struct file *file,
  1283. const char __user *user_buf,
  1284. size_t count, loff_t *ppos)
  1285. {
  1286. struct iwl_trans *trans = file->private_data;
  1287. char buf[8];
  1288. int buf_size;
  1289. int traffic_log;
  1290. memset(buf, 0, sizeof(buf));
  1291. buf_size = min(count, sizeof(buf) - 1);
  1292. if (copy_from_user(buf, user_buf, buf_size))
  1293. return -EFAULT;
  1294. if (sscanf(buf, "%d", &traffic_log) != 1)
  1295. return -EFAULT;
  1296. if (traffic_log == 0)
  1297. iwl_reset_traffic_log(priv(trans));
  1298. return count;
  1299. }
  1300. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1301. char __user *user_buf,
  1302. size_t count, loff_t *ppos) {
  1303. struct iwl_trans *trans = file->private_data;
  1304. struct iwl_priv *priv = priv(trans);
  1305. struct iwl_tx_queue *txq;
  1306. struct iwl_queue *q;
  1307. char *buf;
  1308. int pos = 0;
  1309. int cnt;
  1310. int ret;
  1311. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1312. if (!priv->txq) {
  1313. IWL_ERR(priv, "txq not ready\n");
  1314. return -EAGAIN;
  1315. }
  1316. buf = kzalloc(bufsz, GFP_KERNEL);
  1317. if (!buf)
  1318. return -ENOMEM;
  1319. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1320. txq = &priv->txq[cnt];
  1321. q = &txq->q;
  1322. pos += scnprintf(buf + pos, bufsz - pos,
  1323. "hwq %.2d: read=%u write=%u stop=%d"
  1324. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1325. cnt, q->read_ptr, q->write_ptr,
  1326. !!test_bit(cnt, priv->queue_stopped),
  1327. txq->swq_id, txq->swq_id & 3,
  1328. (txq->swq_id >> 2) & 0x1f);
  1329. if (cnt >= 4)
  1330. continue;
  1331. /* for the ACs, display the stop count too */
  1332. pos += scnprintf(buf + pos, bufsz - pos,
  1333. " stop-count: %d\n",
  1334. atomic_read(&priv->queue_stop_count[cnt]));
  1335. }
  1336. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1337. kfree(buf);
  1338. return ret;
  1339. }
  1340. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1341. char __user *user_buf,
  1342. size_t count, loff_t *ppos) {
  1343. struct iwl_trans *trans = file->private_data;
  1344. struct iwl_trans_pcie *trans_pcie =
  1345. IWL_TRANS_GET_PCIE_TRANS(trans);
  1346. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1347. char buf[256];
  1348. int pos = 0;
  1349. const size_t bufsz = sizeof(buf);
  1350. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1351. rxq->read);
  1352. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1353. rxq->write);
  1354. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1355. rxq->free_count);
  1356. if (rxq->rb_stts) {
  1357. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1358. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1359. } else {
  1360. pos += scnprintf(buf + pos, bufsz - pos,
  1361. "closed_rb_num: Not Allocated\n");
  1362. }
  1363. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1364. }
  1365. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1366. char __user *user_buf,
  1367. size_t count, loff_t *ppos)
  1368. {
  1369. struct iwl_trans *trans = file->private_data;
  1370. char *buf;
  1371. int pos = 0;
  1372. ssize_t ret = -ENOMEM;
  1373. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1374. if (buf) {
  1375. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1376. kfree(buf);
  1377. }
  1378. return ret;
  1379. }
  1380. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1381. const char __user *user_buf,
  1382. size_t count, loff_t *ppos)
  1383. {
  1384. struct iwl_trans *trans = file->private_data;
  1385. u32 event_log_flag;
  1386. char buf[8];
  1387. int buf_size;
  1388. memset(buf, 0, sizeof(buf));
  1389. buf_size = min(count, sizeof(buf) - 1);
  1390. if (copy_from_user(buf, user_buf, buf_size))
  1391. return -EFAULT;
  1392. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1393. return -EFAULT;
  1394. if (event_log_flag == 1)
  1395. iwl_dump_nic_event_log(trans, true, NULL, false);
  1396. return count;
  1397. }
  1398. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1399. char __user *user_buf,
  1400. size_t count, loff_t *ppos) {
  1401. struct iwl_trans *trans = file->private_data;
  1402. struct iwl_trans_pcie *trans_pcie =
  1403. IWL_TRANS_GET_PCIE_TRANS(trans);
  1404. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1405. int pos = 0;
  1406. char *buf;
  1407. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1408. ssize_t ret;
  1409. buf = kzalloc(bufsz, GFP_KERNEL);
  1410. if (!buf) {
  1411. IWL_ERR(trans, "Can not allocate Buffer\n");
  1412. return -ENOMEM;
  1413. }
  1414. pos += scnprintf(buf + pos, bufsz - pos,
  1415. "Interrupt Statistics Report:\n");
  1416. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1417. isr_stats->hw);
  1418. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1419. isr_stats->sw);
  1420. if (isr_stats->sw || isr_stats->hw) {
  1421. pos += scnprintf(buf + pos, bufsz - pos,
  1422. "\tLast Restarting Code: 0x%X\n",
  1423. isr_stats->err_code);
  1424. }
  1425. #ifdef CONFIG_IWLWIFI_DEBUG
  1426. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1427. isr_stats->sch);
  1428. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1429. isr_stats->alive);
  1430. #endif
  1431. pos += scnprintf(buf + pos, bufsz - pos,
  1432. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1433. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1434. isr_stats->ctkill);
  1435. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1436. isr_stats->wakeup);
  1437. pos += scnprintf(buf + pos, bufsz - pos,
  1438. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1439. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1440. isr_stats->tx);
  1441. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1442. isr_stats->unhandled);
  1443. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1444. kfree(buf);
  1445. return ret;
  1446. }
  1447. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1448. const char __user *user_buf,
  1449. size_t count, loff_t *ppos)
  1450. {
  1451. struct iwl_trans *trans = file->private_data;
  1452. struct iwl_trans_pcie *trans_pcie =
  1453. IWL_TRANS_GET_PCIE_TRANS(trans);
  1454. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1455. char buf[8];
  1456. int buf_size;
  1457. u32 reset_flag;
  1458. memset(buf, 0, sizeof(buf));
  1459. buf_size = min(count, sizeof(buf) - 1);
  1460. if (copy_from_user(buf, user_buf, buf_size))
  1461. return -EFAULT;
  1462. if (sscanf(buf, "%x", &reset_flag) != 1)
  1463. return -EFAULT;
  1464. if (reset_flag == 0)
  1465. memset(isr_stats, 0, sizeof(*isr_stats));
  1466. return count;
  1467. }
  1468. static const char *get_csr_string(int cmd)
  1469. {
  1470. switch (cmd) {
  1471. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1472. IWL_CMD(CSR_INT_COALESCING);
  1473. IWL_CMD(CSR_INT);
  1474. IWL_CMD(CSR_INT_MASK);
  1475. IWL_CMD(CSR_FH_INT_STATUS);
  1476. IWL_CMD(CSR_GPIO_IN);
  1477. IWL_CMD(CSR_RESET);
  1478. IWL_CMD(CSR_GP_CNTRL);
  1479. IWL_CMD(CSR_HW_REV);
  1480. IWL_CMD(CSR_EEPROM_REG);
  1481. IWL_CMD(CSR_EEPROM_GP);
  1482. IWL_CMD(CSR_OTP_GP_REG);
  1483. IWL_CMD(CSR_GIO_REG);
  1484. IWL_CMD(CSR_GP_UCODE_REG);
  1485. IWL_CMD(CSR_GP_DRIVER_REG);
  1486. IWL_CMD(CSR_UCODE_DRV_GP1);
  1487. IWL_CMD(CSR_UCODE_DRV_GP2);
  1488. IWL_CMD(CSR_LED_REG);
  1489. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1490. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1491. IWL_CMD(CSR_ANA_PLL_CFG);
  1492. IWL_CMD(CSR_HW_REV_WA_REG);
  1493. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1494. default:
  1495. return "UNKNOWN";
  1496. }
  1497. }
  1498. void iwl_dump_csr(struct iwl_trans *trans)
  1499. {
  1500. int i;
  1501. static const u32 csr_tbl[] = {
  1502. CSR_HW_IF_CONFIG_REG,
  1503. CSR_INT_COALESCING,
  1504. CSR_INT,
  1505. CSR_INT_MASK,
  1506. CSR_FH_INT_STATUS,
  1507. CSR_GPIO_IN,
  1508. CSR_RESET,
  1509. CSR_GP_CNTRL,
  1510. CSR_HW_REV,
  1511. CSR_EEPROM_REG,
  1512. CSR_EEPROM_GP,
  1513. CSR_OTP_GP_REG,
  1514. CSR_GIO_REG,
  1515. CSR_GP_UCODE_REG,
  1516. CSR_GP_DRIVER_REG,
  1517. CSR_UCODE_DRV_GP1,
  1518. CSR_UCODE_DRV_GP2,
  1519. CSR_LED_REG,
  1520. CSR_DRAM_INT_TBL_REG,
  1521. CSR_GIO_CHICKEN_BITS,
  1522. CSR_ANA_PLL_CFG,
  1523. CSR_HW_REV_WA_REG,
  1524. CSR_DBG_HPET_MEM_REG
  1525. };
  1526. IWL_ERR(trans, "CSR values:\n");
  1527. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1528. "CSR_INT_PERIODIC_REG)\n");
  1529. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1530. IWL_ERR(trans, " %25s: 0X%08x\n",
  1531. get_csr_string(csr_tbl[i]),
  1532. iwl_read32(bus(trans), csr_tbl[i]));
  1533. }
  1534. }
  1535. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1536. const char __user *user_buf,
  1537. size_t count, loff_t *ppos)
  1538. {
  1539. struct iwl_trans *trans = file->private_data;
  1540. char buf[8];
  1541. int buf_size;
  1542. int csr;
  1543. memset(buf, 0, sizeof(buf));
  1544. buf_size = min(count, sizeof(buf) - 1);
  1545. if (copy_from_user(buf, user_buf, buf_size))
  1546. return -EFAULT;
  1547. if (sscanf(buf, "%d", &csr) != 1)
  1548. return -EFAULT;
  1549. iwl_dump_csr(trans);
  1550. return count;
  1551. }
  1552. static const char *get_fh_string(int cmd)
  1553. {
  1554. switch (cmd) {
  1555. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1556. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1557. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1558. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1559. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1560. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1561. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1562. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1563. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1564. default:
  1565. return "UNKNOWN";
  1566. }
  1567. }
  1568. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1569. {
  1570. int i;
  1571. #ifdef CONFIG_IWLWIFI_DEBUG
  1572. int pos = 0;
  1573. size_t bufsz = 0;
  1574. #endif
  1575. static const u32 fh_tbl[] = {
  1576. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1577. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1578. FH_RSCSR_CHNL0_WPTR,
  1579. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1580. FH_MEM_RSSR_SHARED_CTRL_REG,
  1581. FH_MEM_RSSR_RX_STATUS_REG,
  1582. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1583. FH_TSSR_TX_STATUS_REG,
  1584. FH_TSSR_TX_ERROR_REG
  1585. };
  1586. #ifdef CONFIG_IWLWIFI_DEBUG
  1587. if (display) {
  1588. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1589. *buf = kmalloc(bufsz, GFP_KERNEL);
  1590. if (!*buf)
  1591. return -ENOMEM;
  1592. pos += scnprintf(*buf + pos, bufsz - pos,
  1593. "FH register values:\n");
  1594. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1595. pos += scnprintf(*buf + pos, bufsz - pos,
  1596. " %34s: 0X%08x\n",
  1597. get_fh_string(fh_tbl[i]),
  1598. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1599. }
  1600. return pos;
  1601. }
  1602. #endif
  1603. IWL_ERR(trans, "FH register values:\n");
  1604. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1605. IWL_ERR(trans, " %34s: 0X%08x\n",
  1606. get_fh_string(fh_tbl[i]),
  1607. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1608. }
  1609. return 0;
  1610. }
  1611. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1612. char __user *user_buf,
  1613. size_t count, loff_t *ppos)
  1614. {
  1615. struct iwl_trans *trans = file->private_data;
  1616. char *buf;
  1617. int pos = 0;
  1618. ssize_t ret = -EFAULT;
  1619. ret = pos = iwl_dump_fh(trans, &buf, true);
  1620. if (buf) {
  1621. ret = simple_read_from_buffer(user_buf,
  1622. count, ppos, buf, pos);
  1623. kfree(buf);
  1624. }
  1625. return ret;
  1626. }
  1627. DEBUGFS_READ_WRITE_FILE_OPS(traffic_log);
  1628. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1629. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1630. DEBUGFS_READ_FILE_OPS(fh_reg);
  1631. DEBUGFS_READ_FILE_OPS(rx_queue);
  1632. DEBUGFS_READ_FILE_OPS(tx_queue);
  1633. DEBUGFS_WRITE_FILE_OPS(csr);
  1634. /*
  1635. * Create the debugfs files and directories
  1636. *
  1637. */
  1638. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1639. struct dentry *dir)
  1640. {
  1641. DEBUGFS_ADD_FILE(traffic_log, dir, S_IWUSR | S_IRUSR);
  1642. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1643. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1644. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1645. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1646. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1647. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1648. return 0;
  1649. }
  1650. #else
  1651. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1652. struct dentry *dir)
  1653. { return 0; }
  1654. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1655. const struct iwl_trans_ops trans_ops_pcie = {
  1656. .alloc = iwl_trans_pcie_alloc,
  1657. .request_irq = iwl_trans_pcie_request_irq,
  1658. .start_device = iwl_trans_pcie_start_device,
  1659. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1660. .stop_device = iwl_trans_pcie_stop_device,
  1661. .tx_start = iwl_trans_pcie_tx_start,
  1662. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1663. .send_cmd = iwl_trans_pcie_send_cmd,
  1664. .send_cmd_pdu = iwl_trans_pcie_send_cmd_pdu,
  1665. .tx = iwl_trans_pcie_tx,
  1666. .reclaim = iwl_trans_pcie_reclaim,
  1667. .txq_agg_disable = iwl_trans_pcie_txq_agg_disable,
  1668. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1669. .txq_agg_setup = iwl_trans_pcie_txq_agg_setup,
  1670. .kick_nic = iwl_trans_pcie_kick_nic,
  1671. .free = iwl_trans_pcie_free,
  1672. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1673. .suspend = iwl_trans_pcie_suspend,
  1674. .resume = iwl_trans_pcie_resume,
  1675. };