iwl-trans.c 47 KB

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