iwl-trans.c 56 KB

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