iwl-trans.c 48 KB

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