iwl-trans-pcie.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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 - 2012 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 - 2012 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/pci.h>
  64. #include <linux/pci-aspm.h>
  65. #include <linux/interrupt.h>
  66. #include <linux/debugfs.h>
  67. #include <linux/bitops.h>
  68. #include <linux/gfp.h>
  69. #include "iwl-trans.h"
  70. #include "iwl-trans-pcie-int.h"
  71. #include "iwl-csr.h"
  72. #include "iwl-prph.h"
  73. #include "iwl-shared.h"
  74. #include "iwl-eeprom.h"
  75. #include "iwl-agn-hw.h"
  76. #include "iwl-core.h"
  77. static int iwl_trans_rx_alloc(struct iwl_trans *trans)
  78. {
  79. struct iwl_trans_pcie *trans_pcie =
  80. IWL_TRANS_GET_PCIE_TRANS(trans);
  81. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  82. struct device *dev = trans->dev;
  83. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  84. spin_lock_init(&rxq->lock);
  85. if (WARN_ON(rxq->bd || rxq->rb_stts))
  86. return -EINVAL;
  87. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  88. rxq->bd = dma_zalloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  89. &rxq->bd_dma, GFP_KERNEL);
  90. if (!rxq->bd)
  91. goto err_bd;
  92. /*Allocate the driver's pointer to receive buffer status */
  93. rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
  94. &rxq->rb_stts_dma, GFP_KERNEL);
  95. if (!rxq->rb_stts)
  96. goto err_rb_stts;
  97. return 0;
  98. err_rb_stts:
  99. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  100. rxq->bd, rxq->bd_dma);
  101. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  102. rxq->bd = NULL;
  103. err_bd:
  104. return -ENOMEM;
  105. }
  106. static void iwl_trans_rxq_free_rx_bufs(struct iwl_trans *trans)
  107. {
  108. struct iwl_trans_pcie *trans_pcie =
  109. IWL_TRANS_GET_PCIE_TRANS(trans);
  110. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  111. int i;
  112. /* Fill the rx_used queue with _all_ of the Rx buffers */
  113. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  114. /* In the reset function, these buffers may have been allocated
  115. * to an SKB, so we need to unmap and free potential storage */
  116. if (rxq->pool[i].page != NULL) {
  117. dma_unmap_page(trans->dev, rxq->pool[i].page_dma,
  118. PAGE_SIZE << hw_params(trans).rx_page_order,
  119. DMA_FROM_DEVICE);
  120. __free_pages(rxq->pool[i].page,
  121. hw_params(trans).rx_page_order);
  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_trans *trans,
  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 = RX_RB_TIMEOUT; /* FIXME: RX_RB_TIMEOUT for all devices? */
  133. if (iwlagn_mod_params.amsdu_size_8K)
  134. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  135. else
  136. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  137. /* Stop Rx DMA */
  138. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  139. /* Reset driver's Rx queue write index */
  140. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  141. /* Tell device where to find RBD circular buffer in DRAM */
  142. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  143. (u32)(rxq->bd_dma >> 8));
  144. /* Tell device where in DRAM to update its Rx status */
  145. iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  146. rxq->rb_stts_dma >> 4);
  147. /* Enable Rx DMA
  148. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  149. * the credit mechanism in 5000 HW RX FIFO
  150. * Direct rx interrupts to hosts
  151. * Rx buffer size 4 or 8k
  152. * RB timeout 0x10
  153. * 256 RBDs
  154. */
  155. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  156. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  157. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  158. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  159. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  160. rb_size|
  161. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  162. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  163. /* Set interrupt coalescing timer to default (2048 usecs) */
  164. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  165. }
  166. static int iwl_rx_init(struct iwl_trans *trans)
  167. {
  168. struct iwl_trans_pcie *trans_pcie =
  169. IWL_TRANS_GET_PCIE_TRANS(trans);
  170. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  171. int i, err;
  172. unsigned long flags;
  173. if (!rxq->bd) {
  174. err = iwl_trans_rx_alloc(trans);
  175. if (err)
  176. return err;
  177. }
  178. spin_lock_irqsave(&rxq->lock, flags);
  179. INIT_LIST_HEAD(&rxq->rx_free);
  180. INIT_LIST_HEAD(&rxq->rx_used);
  181. iwl_trans_rxq_free_rx_bufs(trans);
  182. for (i = 0; i < RX_QUEUE_SIZE; i++)
  183. rxq->queue[i] = NULL;
  184. /* Set us so that we have processed and used all buffers, but have
  185. * not restocked the Rx queue with fresh buffers */
  186. rxq->read = rxq->write = 0;
  187. rxq->write_actual = 0;
  188. rxq->free_count = 0;
  189. spin_unlock_irqrestore(&rxq->lock, flags);
  190. iwlagn_rx_replenish(trans);
  191. iwl_trans_rx_hw_init(trans, rxq);
  192. spin_lock_irqsave(&trans->shrd->lock, flags);
  193. rxq->need_update = 1;
  194. iwl_rx_queue_update_write_ptr(trans, rxq);
  195. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  196. return 0;
  197. }
  198. static void iwl_trans_pcie_rx_free(struct iwl_trans *trans)
  199. {
  200. struct iwl_trans_pcie *trans_pcie =
  201. IWL_TRANS_GET_PCIE_TRANS(trans);
  202. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  203. unsigned long flags;
  204. /*if rxq->bd is NULL, it means that nothing has been allocated,
  205. * exit now */
  206. if (!rxq->bd) {
  207. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  208. return;
  209. }
  210. spin_lock_irqsave(&rxq->lock, flags);
  211. iwl_trans_rxq_free_rx_bufs(trans);
  212. spin_unlock_irqrestore(&rxq->lock, flags);
  213. dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  214. rxq->bd, rxq->bd_dma);
  215. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  216. rxq->bd = NULL;
  217. if (rxq->rb_stts)
  218. dma_free_coherent(trans->dev,
  219. sizeof(struct iwl_rb_status),
  220. rxq->rb_stts, rxq->rb_stts_dma);
  221. else
  222. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  223. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  224. rxq->rb_stts = NULL;
  225. }
  226. static int iwl_trans_rx_stop(struct iwl_trans *trans)
  227. {
  228. /* stop Rx DMA */
  229. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  230. return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
  231. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  232. }
  233. static inline int iwlagn_alloc_dma_ptr(struct iwl_trans *trans,
  234. struct iwl_dma_ptr *ptr, size_t size)
  235. {
  236. if (WARN_ON(ptr->addr))
  237. return -EINVAL;
  238. ptr->addr = dma_alloc_coherent(trans->dev, size,
  239. &ptr->dma, GFP_KERNEL);
  240. if (!ptr->addr)
  241. return -ENOMEM;
  242. ptr->size = size;
  243. return 0;
  244. }
  245. static inline void iwlagn_free_dma_ptr(struct iwl_trans *trans,
  246. struct iwl_dma_ptr *ptr)
  247. {
  248. if (unlikely(!ptr->addr))
  249. return;
  250. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  251. memset(ptr, 0, sizeof(*ptr));
  252. }
  253. static int iwl_trans_txq_alloc(struct iwl_trans *trans,
  254. struct iwl_tx_queue *txq, int slots_num,
  255. u32 txq_id)
  256. {
  257. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  258. int i;
  259. if (WARN_ON(txq->meta || txq->cmd || txq->skbs || txq->tfds))
  260. return -EINVAL;
  261. txq->q.n_window = slots_num;
  262. txq->meta = kcalloc(slots_num, sizeof(txq->meta[0]), GFP_KERNEL);
  263. txq->cmd = kcalloc(slots_num, sizeof(txq->cmd[0]), GFP_KERNEL);
  264. if (!txq->meta || !txq->cmd)
  265. goto error;
  266. if (txq_id == trans->shrd->cmd_queue)
  267. for (i = 0; i < slots_num; i++) {
  268. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  269. GFP_KERNEL);
  270. if (!txq->cmd[i])
  271. goto error;
  272. }
  273. /* Alloc driver data array and TFD circular buffer */
  274. /* Driver private data, only for Tx (not command) queues,
  275. * not shared with device. */
  276. if (txq_id != trans->shrd->cmd_queue) {
  277. txq->skbs = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(txq->skbs[0]),
  278. GFP_KERNEL);
  279. if (!txq->skbs) {
  280. IWL_ERR(trans, "kmalloc for auxiliary BD "
  281. "structures failed\n");
  282. goto error;
  283. }
  284. } else {
  285. txq->skbs = NULL;
  286. }
  287. /* Circular buffer of transmit frame descriptors (TFDs),
  288. * shared with device */
  289. txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
  290. &txq->q.dma_addr, GFP_KERNEL);
  291. if (!txq->tfds) {
  292. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  293. goto error;
  294. }
  295. txq->q.id = txq_id;
  296. return 0;
  297. error:
  298. kfree(txq->skbs);
  299. txq->skbs = NULL;
  300. /* since txq->cmd has been zeroed,
  301. * all non allocated cmd[i] will be NULL */
  302. if (txq->cmd && txq_id == trans->shrd->cmd_queue)
  303. for (i = 0; i < slots_num; i++)
  304. kfree(txq->cmd[i]);
  305. kfree(txq->meta);
  306. kfree(txq->cmd);
  307. txq->meta = NULL;
  308. txq->cmd = NULL;
  309. return -ENOMEM;
  310. }
  311. static int iwl_trans_txq_init(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  312. int slots_num, u32 txq_id)
  313. {
  314. int ret;
  315. txq->need_update = 0;
  316. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  317. /*
  318. * For the default queues 0-3, set up the swq_id
  319. * already -- all others need to get one later
  320. * (if they need one at all).
  321. */
  322. if (txq_id < 4)
  323. iwl_set_swq_id(txq, txq_id, txq_id);
  324. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  325. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  326. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  327. /* Initialize queue's high/low-water marks, and head/tail indexes */
  328. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  329. txq_id);
  330. if (ret)
  331. return ret;
  332. /*
  333. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  334. * given Tx queue, and enable the DMA channel used for that queue.
  335. * Circular buffer (TFD queue in DRAM) physical base address */
  336. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  337. txq->q.dma_addr >> 8);
  338. return 0;
  339. }
  340. /**
  341. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  342. */
  343. static void iwl_tx_queue_unmap(struct iwl_trans *trans, int txq_id)
  344. {
  345. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  346. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  347. struct iwl_queue *q = &txq->q;
  348. enum dma_data_direction dma_dir;
  349. unsigned long flags;
  350. spinlock_t *lock;
  351. if (!q->n_bd)
  352. return;
  353. /* In the command queue, all the TBs are mapped as BIDI
  354. * so unmap them as such.
  355. */
  356. if (txq_id == trans->shrd->cmd_queue) {
  357. dma_dir = DMA_BIDIRECTIONAL;
  358. lock = &trans->hcmd_lock;
  359. } else {
  360. dma_dir = DMA_TO_DEVICE;
  361. lock = &trans->shrd->sta_lock;
  362. }
  363. spin_lock_irqsave(lock, flags);
  364. while (q->write_ptr != q->read_ptr) {
  365. /* The read_ptr needs to bound by q->n_window */
  366. iwlagn_txq_free_tfd(trans, txq, get_cmd_index(q, q->read_ptr),
  367. dma_dir);
  368. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  369. }
  370. spin_unlock_irqrestore(lock, flags);
  371. }
  372. /**
  373. * iwl_tx_queue_free - Deallocate DMA queue.
  374. * @txq: Transmit queue to deallocate.
  375. *
  376. * Empty queue by removing and destroying all BD's.
  377. * Free all buffers.
  378. * 0-fill, but do not free "txq" descriptor structure.
  379. */
  380. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  381. {
  382. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  383. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  384. struct device *dev = trans->dev;
  385. int i;
  386. if (WARN_ON(!txq))
  387. return;
  388. iwl_tx_queue_unmap(trans, txq_id);
  389. /* De-alloc array of command/tx buffers */
  390. if (txq_id == trans->shrd->cmd_queue)
  391. for (i = 0; i < txq->q.n_window; i++)
  392. kfree(txq->cmd[i]);
  393. /* De-alloc circular buffer of TFDs */
  394. if (txq->q.n_bd) {
  395. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  396. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  397. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  398. }
  399. /* De-alloc array of per-TFD driver data */
  400. kfree(txq->skbs);
  401. txq->skbs = NULL;
  402. /* deallocate arrays */
  403. kfree(txq->cmd);
  404. kfree(txq->meta);
  405. txq->cmd = NULL;
  406. txq->meta = NULL;
  407. /* 0-fill queue descriptor structure */
  408. memset(txq, 0, sizeof(*txq));
  409. }
  410. /**
  411. * iwl_trans_tx_free - Free TXQ Context
  412. *
  413. * Destroy all TX DMA queues and structures
  414. */
  415. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  416. {
  417. int txq_id;
  418. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  419. /* Tx queues */
  420. if (trans_pcie->txq) {
  421. for (txq_id = 0;
  422. txq_id < hw_params(trans).max_txq_num; txq_id++)
  423. iwl_tx_queue_free(trans, txq_id);
  424. }
  425. kfree(trans_pcie->txq);
  426. trans_pcie->txq = NULL;
  427. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  428. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  429. }
  430. /**
  431. * iwl_trans_tx_alloc - allocate TX context
  432. * Allocate all Tx DMA structures and initialize them
  433. *
  434. * @param priv
  435. * @return error code
  436. */
  437. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  438. {
  439. int ret;
  440. int txq_id, slots_num;
  441. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  442. u16 scd_bc_tbls_size = hw_params(trans).max_txq_num *
  443. sizeof(struct iwlagn_scd_bc_tbl);
  444. /*It is not allowed to alloc twice, so warn when this happens.
  445. * We cannot rely on the previous allocation, so free and fail */
  446. if (WARN_ON(trans_pcie->txq)) {
  447. ret = -EINVAL;
  448. goto error;
  449. }
  450. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  451. scd_bc_tbls_size);
  452. if (ret) {
  453. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  454. goto error;
  455. }
  456. /* Alloc keep-warm buffer */
  457. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  458. if (ret) {
  459. IWL_ERR(trans, "Keep Warm allocation failed\n");
  460. goto error;
  461. }
  462. trans_pcie->txq = kcalloc(hw_params(trans).max_txq_num,
  463. sizeof(struct iwl_tx_queue), GFP_KERNEL);
  464. if (!trans_pcie->txq) {
  465. IWL_ERR(trans, "Not enough memory for txq\n");
  466. ret = ENOMEM;
  467. goto error;
  468. }
  469. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  470. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  471. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  472. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  473. ret = iwl_trans_txq_alloc(trans, &trans_pcie->txq[txq_id],
  474. slots_num, txq_id);
  475. if (ret) {
  476. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  477. goto error;
  478. }
  479. }
  480. return 0;
  481. error:
  482. iwl_trans_pcie_tx_free(trans);
  483. return ret;
  484. }
  485. static int iwl_tx_init(struct iwl_trans *trans)
  486. {
  487. int ret;
  488. int txq_id, slots_num;
  489. unsigned long flags;
  490. bool alloc = false;
  491. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  492. if (!trans_pcie->txq) {
  493. ret = iwl_trans_tx_alloc(trans);
  494. if (ret)
  495. goto error;
  496. alloc = true;
  497. }
  498. spin_lock_irqsave(&trans->shrd->lock, flags);
  499. /* Turn off all Tx DMA fifos */
  500. iwl_write_prph(trans, SCD_TXFACT, 0);
  501. /* Tell NIC where to find the "keep warm" buffer */
  502. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  503. trans_pcie->kw.dma >> 4);
  504. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  505. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  506. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  507. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  508. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  509. ret = iwl_trans_txq_init(trans, &trans_pcie->txq[txq_id],
  510. slots_num, txq_id);
  511. if (ret) {
  512. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  513. goto error;
  514. }
  515. }
  516. return 0;
  517. error:
  518. /*Upon error, free only if we allocated something */
  519. if (alloc)
  520. iwl_trans_pcie_tx_free(trans);
  521. return ret;
  522. }
  523. static void iwl_set_pwr_vmain(struct iwl_trans *trans)
  524. {
  525. /*
  526. * (for documentation purposes)
  527. * to set power to V_AUX, do:
  528. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  529. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  530. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  531. ~APMG_PS_CTRL_MSK_PWR_SRC);
  532. */
  533. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  534. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  535. ~APMG_PS_CTRL_MSK_PWR_SRC);
  536. }
  537. /*
  538. * Start up NIC's basic functionality after it has been reset
  539. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  540. * NOTE: This does not load uCode nor start the embedded processor
  541. */
  542. static int iwl_apm_init(struct iwl_trans *trans)
  543. {
  544. int ret = 0;
  545. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  546. /*
  547. * Use "set_bit" below rather than "write", to preserve any hardware
  548. * bits already set by default after reset.
  549. */
  550. /* Disable L0S exit timer (platform NMI Work/Around) */
  551. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  552. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  553. /*
  554. * Disable L0s without affecting L1;
  555. * don't wait for ICH L0s (ICH bug W/A)
  556. */
  557. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  558. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  559. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  560. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  561. /*
  562. * Enable HAP INTA (interrupt from management bus) to
  563. * wake device's PCI Express link L1a -> L0s
  564. */
  565. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  566. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  567. bus_apm_config(bus(trans));
  568. /* Configure analog phase-lock-loop before activating to D0A */
  569. if (cfg(trans)->base_params->pll_cfg_val)
  570. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  571. cfg(trans)->base_params->pll_cfg_val);
  572. /*
  573. * Set "initialization complete" bit to move adapter from
  574. * D0U* --> D0A* (powered-up active) state.
  575. */
  576. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  577. /*
  578. * Wait for clock stabilization; once stabilized, access to
  579. * device-internal resources is supported, e.g. iwl_write_prph()
  580. * and accesses to uCode SRAM.
  581. */
  582. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  583. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  584. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  585. if (ret < 0) {
  586. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  587. goto out;
  588. }
  589. /*
  590. * Enable DMA clock and wait for it to stabilize.
  591. *
  592. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  593. * do not disable clocks. This preserves any hardware bits already
  594. * set by default in "CLK_CTRL_REG" after reset.
  595. */
  596. iwl_write_prph(trans, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  597. udelay(20);
  598. /* Disable L1-Active */
  599. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  600. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  601. set_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status);
  602. out:
  603. return ret;
  604. }
  605. static int iwl_apm_stop_master(struct iwl_trans *trans)
  606. {
  607. int ret = 0;
  608. /* stop device's busmaster DMA activity */
  609. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  610. ret = iwl_poll_bit(trans, CSR_RESET,
  611. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  612. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  613. if (ret)
  614. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  615. IWL_DEBUG_INFO(trans, "stop master\n");
  616. return ret;
  617. }
  618. static void iwl_apm_stop(struct iwl_trans *trans)
  619. {
  620. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  621. clear_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status);
  622. /* Stop device's DMA activity */
  623. iwl_apm_stop_master(trans);
  624. /* Reset the entire device */
  625. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  626. udelay(10);
  627. /*
  628. * Clear "initialization complete" bit to move adapter from
  629. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  630. */
  631. iwl_clear_bit(trans, CSR_GP_CNTRL,
  632. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  633. }
  634. static int iwl_nic_init(struct iwl_trans *trans)
  635. {
  636. unsigned long flags;
  637. /* nic_init */
  638. spin_lock_irqsave(&trans->shrd->lock, flags);
  639. iwl_apm_init(trans);
  640. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  641. iwl_write8(trans, CSR_INT_COALESCING,
  642. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  643. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  644. iwl_set_pwr_vmain(trans);
  645. iwl_nic_config(priv(trans));
  646. #ifndef CONFIG_IWLWIFI_IDI
  647. /* Allocate the RX queue, or reset if it is already allocated */
  648. iwl_rx_init(trans);
  649. #endif
  650. /* Allocate or reset and init all Tx and Command queues */
  651. if (iwl_tx_init(trans))
  652. return -ENOMEM;
  653. if (hw_params(trans).shadow_reg_enable) {
  654. /* enable shadow regs in HW */
  655. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL,
  656. 0x800FFFFF);
  657. }
  658. set_bit(STATUS_INIT, &trans->shrd->status);
  659. return 0;
  660. }
  661. #define HW_READY_TIMEOUT (50)
  662. /* Note: returns poll_bit return value, which is >= 0 if success */
  663. static int iwl_set_hw_ready(struct iwl_trans *trans)
  664. {
  665. int ret;
  666. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  667. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  668. /* See if we got it */
  669. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  670. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  671. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  672. HW_READY_TIMEOUT);
  673. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  674. return ret;
  675. }
  676. /* Note: returns standard 0/-ERROR code */
  677. static int iwl_prepare_card_hw(struct iwl_trans *trans)
  678. {
  679. int ret;
  680. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  681. ret = iwl_set_hw_ready(trans);
  682. /* If the card is ready, exit 0 */
  683. if (ret >= 0)
  684. return 0;
  685. /* If HW is not ready, prepare the conditions to check again */
  686. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  687. CSR_HW_IF_CONFIG_REG_PREPARE);
  688. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  689. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  690. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  691. if (ret < 0)
  692. return ret;
  693. /* HW should be ready by now, check again. */
  694. ret = iwl_set_hw_ready(trans);
  695. if (ret >= 0)
  696. return 0;
  697. return ret;
  698. }
  699. #define IWL_AC_UNSET -1
  700. struct queue_to_fifo_ac {
  701. s8 fifo, ac;
  702. };
  703. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  704. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  705. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  706. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  707. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  708. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  709. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  710. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  711. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  712. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  713. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  714. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  715. };
  716. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  717. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  718. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  719. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  720. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  721. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  722. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  723. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  724. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  725. { IWL_TX_FIFO_BE_IPAN, 2, },
  726. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  727. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  728. };
  729. static const u8 iwlagn_bss_ac_to_fifo[] = {
  730. IWL_TX_FIFO_VO,
  731. IWL_TX_FIFO_VI,
  732. IWL_TX_FIFO_BE,
  733. IWL_TX_FIFO_BK,
  734. };
  735. static const u8 iwlagn_bss_ac_to_queue[] = {
  736. 0, 1, 2, 3,
  737. };
  738. static const u8 iwlagn_pan_ac_to_fifo[] = {
  739. IWL_TX_FIFO_VO_IPAN,
  740. IWL_TX_FIFO_VI_IPAN,
  741. IWL_TX_FIFO_BE_IPAN,
  742. IWL_TX_FIFO_BK_IPAN,
  743. };
  744. static const u8 iwlagn_pan_ac_to_queue[] = {
  745. 7, 6, 5, 4,
  746. };
  747. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  748. {
  749. int ret;
  750. struct iwl_trans_pcie *trans_pcie =
  751. IWL_TRANS_GET_PCIE_TRANS(trans);
  752. trans->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  753. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  754. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  755. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  756. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  757. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  758. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  759. if ((hw_params(trans).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  760. iwl_prepare_card_hw(trans)) {
  761. IWL_WARN(trans, "Exit HW not ready\n");
  762. return -EIO;
  763. }
  764. /* If platform's RF_KILL switch is NOT set to KILL */
  765. if (iwl_read32(trans, CSR_GP_CNTRL) &
  766. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  767. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  768. else
  769. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  770. if (iwl_is_rfkill(trans->shrd)) {
  771. iwl_set_hw_rfkill_state(priv(trans), true);
  772. iwl_enable_interrupts(trans);
  773. return -ERFKILL;
  774. }
  775. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  776. ret = iwl_nic_init(trans);
  777. if (ret) {
  778. IWL_ERR(trans, "Unable to init nic\n");
  779. return ret;
  780. }
  781. /* make sure rfkill handshake bits are cleared */
  782. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  783. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  784. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  785. /* clear (again), then enable host interrupts */
  786. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  787. iwl_enable_interrupts(trans);
  788. /* really make sure rfkill handshake bits are cleared */
  789. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  790. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  791. return 0;
  792. }
  793. /*
  794. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  795. * must be called under priv->shrd->lock and mac access
  796. */
  797. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  798. {
  799. iwl_write_prph(trans, SCD_TXFACT, mask);
  800. }
  801. static void iwl_tx_start(struct iwl_trans *trans)
  802. {
  803. const struct queue_to_fifo_ac *queue_to_fifo;
  804. struct iwl_trans_pcie *trans_pcie =
  805. IWL_TRANS_GET_PCIE_TRANS(trans);
  806. u32 a;
  807. unsigned long flags;
  808. int i, chan;
  809. u32 reg_val;
  810. spin_lock_irqsave(&trans->shrd->lock, flags);
  811. trans_pcie->scd_base_addr =
  812. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  813. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  814. /* reset conext data memory */
  815. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  816. a += 4)
  817. iwl_write_targ_mem(trans, a, 0);
  818. /* reset tx status memory */
  819. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  820. a += 4)
  821. iwl_write_targ_mem(trans, a, 0);
  822. for (; a < trans_pcie->scd_base_addr +
  823. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(trans).max_txq_num);
  824. a += 4)
  825. iwl_write_targ_mem(trans, a, 0);
  826. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  827. trans_pcie->scd_bc_tbls.dma >> 10);
  828. /* Enable DMA channel */
  829. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  830. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  831. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  832. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  833. /* Update FH chicken bits */
  834. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  835. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  836. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  837. iwl_write_prph(trans, SCD_QUEUECHAIN_SEL,
  838. SCD_QUEUECHAIN_SEL_ALL(trans));
  839. iwl_write_prph(trans, SCD_AGGR_SEL, 0);
  840. /* initiate the queues */
  841. for (i = 0; i < hw_params(trans).max_txq_num; i++) {
  842. iwl_write_prph(trans, SCD_QUEUE_RDPTR(i), 0);
  843. iwl_write_direct32(trans, HBUS_TARG_WRPTR, 0 | (i << 8));
  844. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  845. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  846. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  847. SCD_CONTEXT_QUEUE_OFFSET(i) +
  848. sizeof(u32),
  849. ((SCD_WIN_SIZE <<
  850. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  851. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  852. ((SCD_FRAME_LIMIT <<
  853. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  854. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  855. }
  856. iwl_write_prph(trans, SCD_INTERRUPT_MASK,
  857. IWL_MASK(0, hw_params(trans).max_txq_num));
  858. /* Activate all Tx DMA/FIFO channels */
  859. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  860. /* map queues to FIFOs */
  861. if (trans->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  862. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  863. else
  864. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  865. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  866. /* make sure all queue are not stopped */
  867. memset(&trans_pcie->queue_stopped[0], 0,
  868. sizeof(trans_pcie->queue_stopped));
  869. for (i = 0; i < 4; i++)
  870. atomic_set(&trans_pcie->queue_stop_count[i], 0);
  871. /* reset to 0 to enable all the queue first */
  872. trans_pcie->txq_ctx_active_msk = 0;
  873. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  874. IWLAGN_FIRST_AMPDU_QUEUE);
  875. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  876. IWLAGN_FIRST_AMPDU_QUEUE);
  877. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  878. int fifo = queue_to_fifo[i].fifo;
  879. int ac = queue_to_fifo[i].ac;
  880. iwl_txq_ctx_activate(trans_pcie, i);
  881. if (fifo == IWL_TX_FIFO_UNUSED)
  882. continue;
  883. if (ac != IWL_AC_UNSET)
  884. iwl_set_swq_id(&trans_pcie->txq[i], ac, i);
  885. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[i],
  886. fifo, 0);
  887. }
  888. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  889. /* Enable L1-Active */
  890. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  891. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  892. }
  893. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans)
  894. {
  895. iwl_reset_ict(trans);
  896. iwl_tx_start(trans);
  897. }
  898. /**
  899. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  900. */
  901. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  902. {
  903. int ch, txq_id;
  904. unsigned long flags;
  905. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  906. /* Turn off all Tx DMA fifos */
  907. spin_lock_irqsave(&trans->shrd->lock, flags);
  908. iwl_trans_txq_set_sched(trans, 0);
  909. /* Stop each Tx DMA channel, and wait for it to be idle */
  910. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  911. iwl_write_direct32(trans,
  912. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  913. if (iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
  914. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  915. 1000))
  916. IWL_ERR(trans, "Failing on timeout while stopping"
  917. " DMA channel %d [0x%08x]", ch,
  918. iwl_read_direct32(trans,
  919. FH_TSSR_TX_STATUS_REG));
  920. }
  921. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  922. if (!trans_pcie->txq) {
  923. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  924. return 0;
  925. }
  926. /* Unmap DMA from host system and free skb's */
  927. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  928. iwl_tx_queue_unmap(trans, txq_id);
  929. return 0;
  930. }
  931. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  932. {
  933. unsigned long flags;
  934. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  935. /* tell the device to stop sending interrupts */
  936. spin_lock_irqsave(&trans->shrd->lock, flags);
  937. iwl_disable_interrupts(trans);
  938. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  939. /* device going down, Stop using ICT table */
  940. iwl_disable_ict(trans);
  941. /*
  942. * If a HW restart happens during firmware loading,
  943. * then the firmware loading might call this function
  944. * and later it might be called again due to the
  945. * restart. So don't process again if the device is
  946. * already dead.
  947. */
  948. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  949. iwl_trans_tx_stop(trans);
  950. #ifndef CONFIG_IWLWIFI_IDI
  951. iwl_trans_rx_stop(trans);
  952. #endif
  953. /* Power-down device's busmaster DMA clocks */
  954. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  955. APMG_CLK_VAL_DMA_CLK_RQT);
  956. udelay(5);
  957. }
  958. /* Make sure (redundant) we've released our request to stay awake */
  959. iwl_clear_bit(trans, CSR_GP_CNTRL,
  960. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  961. /* Stop the device, and put it in low power state */
  962. iwl_apm_stop(trans);
  963. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  964. * Clean again the interrupt here
  965. */
  966. spin_lock_irqsave(&trans->shrd->lock, flags);
  967. iwl_disable_interrupts(trans);
  968. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  969. /* wait to make sure we flush pending tasklet*/
  970. synchronize_irq(trans->irq);
  971. tasklet_kill(&trans_pcie->irq_tasklet);
  972. /* stop and reset the on-board processor */
  973. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  974. }
  975. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  976. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  977. u8 sta_id, u8 tid)
  978. {
  979. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  980. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  981. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  982. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  983. struct iwl_cmd_meta *out_meta;
  984. struct iwl_tx_queue *txq;
  985. struct iwl_queue *q;
  986. dma_addr_t phys_addr = 0;
  987. dma_addr_t txcmd_phys;
  988. dma_addr_t scratch_phys;
  989. u16 len, firstlen, secondlen;
  990. u8 wait_write_ptr = 0;
  991. u8 txq_id;
  992. bool is_agg = false;
  993. __le16 fc = hdr->frame_control;
  994. u8 hdr_len = ieee80211_hdrlen(fc);
  995. u16 __maybe_unused wifi_seq;
  996. /*
  997. * Send this frame after DTIM -- there's a special queue
  998. * reserved for this for contexts that support AP mode.
  999. */
  1000. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1001. txq_id = trans_pcie->mcast_queue[ctx];
  1002. /*
  1003. * The microcode will clear the more data
  1004. * bit in the last frame it transmits.
  1005. */
  1006. hdr->frame_control |=
  1007. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1008. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  1009. txq_id = IWL_AUX_QUEUE;
  1010. else
  1011. txq_id =
  1012. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  1013. /* aggregation is on for this <sta,tid> */
  1014. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1015. WARN_ON(tid >= IWL_MAX_TID_COUNT);
  1016. txq_id = trans_pcie->agg_txq[sta_id][tid];
  1017. is_agg = true;
  1018. }
  1019. txq = &trans_pcie->txq[txq_id];
  1020. q = &txq->q;
  1021. /* In AGG mode, the index in the ring must correspond to the WiFi
  1022. * sequence number. This is a HW requirements to help the SCD to parse
  1023. * the BA.
  1024. * Check here that the packets are in the right place on the ring.
  1025. */
  1026. #ifdef CONFIG_IWLWIFI_DEBUG
  1027. wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1028. WARN_ONCE(is_agg && ((wifi_seq & 0xff) != q->write_ptr),
  1029. "Q: %d WiFi Seq %d tfdNum %d",
  1030. txq_id, wifi_seq, q->write_ptr);
  1031. #endif
  1032. /* Set up driver data for this TFD */
  1033. txq->skbs[q->write_ptr] = skb;
  1034. txq->cmd[q->write_ptr] = dev_cmd;
  1035. dev_cmd->hdr.cmd = REPLY_TX;
  1036. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1037. INDEX_TO_SEQ(q->write_ptr)));
  1038. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1039. out_meta = &txq->meta[q->write_ptr];
  1040. /*
  1041. * Use the first empty entry in this queue's command buffer array
  1042. * to contain the Tx command and MAC header concatenated together
  1043. * (payload data will be in another buffer).
  1044. * Size of this varies, due to varying MAC header length.
  1045. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1046. * of the MAC header (device reads on dword boundaries).
  1047. * We'll tell device about this padding later.
  1048. */
  1049. len = sizeof(struct iwl_tx_cmd) +
  1050. sizeof(struct iwl_cmd_header) + hdr_len;
  1051. firstlen = (len + 3) & ~3;
  1052. /* Tell NIC about any 2-byte padding after MAC header */
  1053. if (firstlen != len)
  1054. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1055. /* Physical address of this Tx command's header (not MAC header!),
  1056. * within command buffer array. */
  1057. txcmd_phys = dma_map_single(trans->dev,
  1058. &dev_cmd->hdr, firstlen,
  1059. DMA_BIDIRECTIONAL);
  1060. if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
  1061. return -1;
  1062. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1063. dma_unmap_len_set(out_meta, len, firstlen);
  1064. if (!ieee80211_has_morefrags(fc)) {
  1065. txq->need_update = 1;
  1066. } else {
  1067. wait_write_ptr = 1;
  1068. txq->need_update = 0;
  1069. }
  1070. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1071. * if any (802.11 null frames have no payload). */
  1072. secondlen = skb->len - hdr_len;
  1073. if (secondlen > 0) {
  1074. phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
  1075. secondlen, DMA_TO_DEVICE);
  1076. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  1077. dma_unmap_single(trans->dev,
  1078. dma_unmap_addr(out_meta, mapping),
  1079. dma_unmap_len(out_meta, len),
  1080. DMA_BIDIRECTIONAL);
  1081. return -1;
  1082. }
  1083. }
  1084. /* Attach buffers to TFD */
  1085. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  1086. if (secondlen > 0)
  1087. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  1088. secondlen, 0);
  1089. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  1090. offsetof(struct iwl_tx_cmd, scratch);
  1091. /* take back ownership of DMA buffer to enable update */
  1092. dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
  1093. DMA_BIDIRECTIONAL);
  1094. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1095. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1096. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  1097. le16_to_cpu(dev_cmd->hdr.sequence));
  1098. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  1099. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  1100. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  1101. /* Set up entry for this TFD in Tx byte-count array */
  1102. iwl_trans_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1103. dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
  1104. DMA_BIDIRECTIONAL);
  1105. trace_iwlwifi_dev_tx(priv(trans),
  1106. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  1107. sizeof(struct iwl_tfd),
  1108. &dev_cmd->hdr, firstlen,
  1109. skb->data + hdr_len, secondlen);
  1110. /* Tell device the write index *just past* this latest filled TFD */
  1111. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1112. iwl_txq_update_write_ptr(trans, txq);
  1113. /*
  1114. * At this point the frame is "transmitted" successfully
  1115. * and we will get a TX status notification eventually,
  1116. * regardless of the value of ret. "ret" only indicates
  1117. * whether or not we should update the write pointer.
  1118. */
  1119. if (iwl_queue_space(q) < q->high_mark) {
  1120. if (wait_write_ptr) {
  1121. txq->need_update = 1;
  1122. iwl_txq_update_write_ptr(trans, txq);
  1123. } else {
  1124. iwl_stop_queue(trans, txq, "Queue is full");
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  1130. {
  1131. /* Remove all resets to allow NIC to operate */
  1132. iwl_write32(trans, CSR_RESET, 0);
  1133. }
  1134. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  1135. {
  1136. struct iwl_trans_pcie *trans_pcie =
  1137. IWL_TRANS_GET_PCIE_TRANS(trans);
  1138. int err;
  1139. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1140. if (!trans_pcie->irq_requested) {
  1141. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1142. iwl_irq_tasklet, (unsigned long)trans);
  1143. iwl_alloc_isr_ict(trans);
  1144. err = request_irq(trans->irq, iwl_isr_ict, IRQF_SHARED,
  1145. DRV_NAME, trans);
  1146. if (err) {
  1147. IWL_ERR(trans, "Error allocating IRQ %d\n",
  1148. trans->irq);
  1149. goto error;
  1150. }
  1151. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1152. trans_pcie->irq_requested = true;
  1153. }
  1154. err = iwl_prepare_card_hw(trans);
  1155. if (err) {
  1156. IWL_ERR(trans, "Error while preparing HW: %d", err);
  1157. goto error;
  1158. }
  1159. iwl_apm_init(trans);
  1160. return err;
  1161. error:
  1162. iwl_free_isr_ict(trans);
  1163. tasklet_kill(&trans_pcie->irq_tasklet);
  1164. return err;
  1165. }
  1166. static void iwl_trans_pcie_stop_hw(struct iwl_trans *trans)
  1167. {
  1168. iwl_apm_stop(trans);
  1169. /* Even if we stop the HW, we still want the RF kill interrupt */
  1170. IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
  1171. iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
  1172. }
  1173. static int iwl_trans_pcie_reclaim(struct iwl_trans *trans, int sta_id, int tid,
  1174. int txq_id, int ssn, u32 status,
  1175. struct sk_buff_head *skbs)
  1176. {
  1177. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1178. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1179. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1180. int tfd_num = ssn & (txq->q.n_bd - 1);
  1181. int freed = 0;
  1182. txq->time_stamp = jiffies;
  1183. if (unlikely(txq_id >= IWLAGN_FIRST_AMPDU_QUEUE &&
  1184. txq_id != trans_pcie->agg_txq[sta_id][tid])) {
  1185. /*
  1186. * FIXME: this is a uCode bug which need to be addressed,
  1187. * log the information and return for now.
  1188. * Since it is can possibly happen very often and in order
  1189. * not to fill the syslog, don't use IWL_ERR or IWL_WARN
  1190. */
  1191. IWL_DEBUG_TX_QUEUES(trans, "Bad queue mapping txq_id %d, "
  1192. "agg_txq[sta_id[tid] %d", txq_id,
  1193. trans_pcie->agg_txq[sta_id][tid]);
  1194. return 1;
  1195. }
  1196. if (txq->q.read_ptr != tfd_num) {
  1197. IWL_DEBUG_TX_REPLY(trans, "[Q %d | AC %d] %d -> %d (%d)\n",
  1198. txq_id, iwl_get_queue_ac(txq), txq->q.read_ptr,
  1199. tfd_num, ssn);
  1200. freed = iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1201. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  1202. (!txq->sched_retry ||
  1203. status != TX_STATUS_FAIL_PASSIVE_NO_RX))
  1204. iwl_wake_queue(trans, txq, "Packets reclaimed");
  1205. }
  1206. return 0;
  1207. }
  1208. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  1209. {
  1210. iowrite8(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1211. }
  1212. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  1213. {
  1214. iowrite32(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1215. }
  1216. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  1217. {
  1218. u32 val = ioread32(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1219. return val;
  1220. }
  1221. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1222. {
  1223. struct iwl_trans_pcie *trans_pcie =
  1224. IWL_TRANS_GET_PCIE_TRANS(trans);
  1225. iwl_calib_free_results(trans);
  1226. iwl_trans_pcie_tx_free(trans);
  1227. #ifndef CONFIG_IWLWIFI_IDI
  1228. iwl_trans_pcie_rx_free(trans);
  1229. #endif
  1230. if (trans_pcie->irq_requested == true) {
  1231. free_irq(trans->irq, trans);
  1232. iwl_free_isr_ict(trans);
  1233. }
  1234. pci_disable_msi(trans_pcie->pci_dev);
  1235. pci_iounmap(trans_pcie->pci_dev, trans_pcie->hw_base);
  1236. pci_release_regions(trans_pcie->pci_dev);
  1237. pci_disable_device(trans_pcie->pci_dev);
  1238. trans->shrd->trans = NULL;
  1239. kfree(trans);
  1240. }
  1241. #ifdef CONFIG_PM_SLEEP
  1242. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1243. {
  1244. /*
  1245. * This function is called when system goes into suspend state
  1246. * mac80211 will call iwlagn_mac_stop() from the mac80211 suspend
  1247. * function first but since iwlagn_mac_stop() has no knowledge of
  1248. * who the caller is,
  1249. * it will not call apm_ops.stop() to stop the DMA operation.
  1250. * Calling apm_ops.stop here to make sure we stop the DMA.
  1251. *
  1252. * But of course ... if we have configured WoWLAN then we did other
  1253. * things already :-)
  1254. */
  1255. if (!trans->shrd->wowlan) {
  1256. iwl_apm_stop(trans);
  1257. } else {
  1258. iwl_disable_interrupts(trans);
  1259. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1260. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1261. }
  1262. return 0;
  1263. }
  1264. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1265. {
  1266. bool hw_rfkill = false;
  1267. iwl_enable_interrupts(trans);
  1268. if (!(iwl_read32(trans, CSR_GP_CNTRL) &
  1269. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1270. hw_rfkill = true;
  1271. if (hw_rfkill)
  1272. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1273. else
  1274. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1275. iwl_set_hw_rfkill_state(priv(trans), hw_rfkill);
  1276. return 0;
  1277. }
  1278. #endif /* CONFIG_PM_SLEEP */
  1279. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1280. enum iwl_rxon_context_id ctx,
  1281. const char *msg)
  1282. {
  1283. u8 ac, txq_id;
  1284. struct iwl_trans_pcie *trans_pcie =
  1285. IWL_TRANS_GET_PCIE_TRANS(trans);
  1286. for (ac = 0; ac < AC_NUM; ac++) {
  1287. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1288. IWL_DEBUG_TX_QUEUES(trans, "Queue Status: Q[%d] %s\n",
  1289. ac,
  1290. (atomic_read(&trans_pcie->queue_stop_count[ac]) > 0)
  1291. ? "stopped" : "awake");
  1292. iwl_wake_queue(trans, &trans_pcie->txq[txq_id], msg);
  1293. }
  1294. }
  1295. static void iwl_trans_pcie_stop_queue(struct iwl_trans *trans, int txq_id,
  1296. const char *msg)
  1297. {
  1298. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1299. iwl_stop_queue(trans, &trans_pcie->txq[txq_id], msg);
  1300. }
  1301. #define IWL_FLUSH_WAIT_MS 2000
  1302. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1303. {
  1304. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1305. struct iwl_tx_queue *txq;
  1306. struct iwl_queue *q;
  1307. int cnt;
  1308. unsigned long now = jiffies;
  1309. int ret = 0;
  1310. /* waiting for all the tx frames complete might take a while */
  1311. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1312. if (cnt == trans->shrd->cmd_queue)
  1313. continue;
  1314. txq = &trans_pcie->txq[cnt];
  1315. q = &txq->q;
  1316. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1317. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1318. msleep(1);
  1319. if (q->read_ptr != q->write_ptr) {
  1320. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1321. ret = -ETIMEDOUT;
  1322. break;
  1323. }
  1324. }
  1325. return ret;
  1326. }
  1327. /*
  1328. * On every watchdog tick we check (latest) time stamp. If it does not
  1329. * change during timeout period and queue is not empty we reset firmware.
  1330. */
  1331. static int iwl_trans_pcie_check_stuck_queue(struct iwl_trans *trans, int cnt)
  1332. {
  1333. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1334. struct iwl_tx_queue *txq = &trans_pcie->txq[cnt];
  1335. struct iwl_queue *q = &txq->q;
  1336. unsigned long timeout;
  1337. if (q->read_ptr == q->write_ptr) {
  1338. txq->time_stamp = jiffies;
  1339. return 0;
  1340. }
  1341. timeout = txq->time_stamp +
  1342. msecs_to_jiffies(hw_params(trans).wd_timeout);
  1343. if (time_after(jiffies, timeout)) {
  1344. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", q->id,
  1345. hw_params(trans).wd_timeout);
  1346. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1347. q->read_ptr, q->write_ptr);
  1348. IWL_ERR(trans, "Current HW read_ptr %d write_ptr %d\n",
  1349. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt))
  1350. & (TFD_QUEUE_SIZE_MAX - 1),
  1351. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1352. return 1;
  1353. }
  1354. return 0;
  1355. }
  1356. static const char *get_fh_string(int cmd)
  1357. {
  1358. switch (cmd) {
  1359. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1360. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1361. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1362. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1363. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1364. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1365. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1366. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1367. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1368. default:
  1369. return "UNKNOWN";
  1370. }
  1371. }
  1372. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1373. {
  1374. int i;
  1375. #ifdef CONFIG_IWLWIFI_DEBUG
  1376. int pos = 0;
  1377. size_t bufsz = 0;
  1378. #endif
  1379. static const u32 fh_tbl[] = {
  1380. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1381. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1382. FH_RSCSR_CHNL0_WPTR,
  1383. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1384. FH_MEM_RSSR_SHARED_CTRL_REG,
  1385. FH_MEM_RSSR_RX_STATUS_REG,
  1386. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1387. FH_TSSR_TX_STATUS_REG,
  1388. FH_TSSR_TX_ERROR_REG
  1389. };
  1390. #ifdef CONFIG_IWLWIFI_DEBUG
  1391. if (display) {
  1392. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1393. *buf = kmalloc(bufsz, GFP_KERNEL);
  1394. if (!*buf)
  1395. return -ENOMEM;
  1396. pos += scnprintf(*buf + pos, bufsz - pos,
  1397. "FH register values:\n");
  1398. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1399. pos += scnprintf(*buf + pos, bufsz - pos,
  1400. " %34s: 0X%08x\n",
  1401. get_fh_string(fh_tbl[i]),
  1402. iwl_read_direct32(trans, fh_tbl[i]));
  1403. }
  1404. return pos;
  1405. }
  1406. #endif
  1407. IWL_ERR(trans, "FH register values:\n");
  1408. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1409. IWL_ERR(trans, " %34s: 0X%08x\n",
  1410. get_fh_string(fh_tbl[i]),
  1411. iwl_read_direct32(trans, fh_tbl[i]));
  1412. }
  1413. return 0;
  1414. }
  1415. static const char *get_csr_string(int cmd)
  1416. {
  1417. switch (cmd) {
  1418. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1419. IWL_CMD(CSR_INT_COALESCING);
  1420. IWL_CMD(CSR_INT);
  1421. IWL_CMD(CSR_INT_MASK);
  1422. IWL_CMD(CSR_FH_INT_STATUS);
  1423. IWL_CMD(CSR_GPIO_IN);
  1424. IWL_CMD(CSR_RESET);
  1425. IWL_CMD(CSR_GP_CNTRL);
  1426. IWL_CMD(CSR_HW_REV);
  1427. IWL_CMD(CSR_EEPROM_REG);
  1428. IWL_CMD(CSR_EEPROM_GP);
  1429. IWL_CMD(CSR_OTP_GP_REG);
  1430. IWL_CMD(CSR_GIO_REG);
  1431. IWL_CMD(CSR_GP_UCODE_REG);
  1432. IWL_CMD(CSR_GP_DRIVER_REG);
  1433. IWL_CMD(CSR_UCODE_DRV_GP1);
  1434. IWL_CMD(CSR_UCODE_DRV_GP2);
  1435. IWL_CMD(CSR_LED_REG);
  1436. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1437. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1438. IWL_CMD(CSR_ANA_PLL_CFG);
  1439. IWL_CMD(CSR_HW_REV_WA_REG);
  1440. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1441. default:
  1442. return "UNKNOWN";
  1443. }
  1444. }
  1445. void iwl_dump_csr(struct iwl_trans *trans)
  1446. {
  1447. int i;
  1448. static const u32 csr_tbl[] = {
  1449. CSR_HW_IF_CONFIG_REG,
  1450. CSR_INT_COALESCING,
  1451. CSR_INT,
  1452. CSR_INT_MASK,
  1453. CSR_FH_INT_STATUS,
  1454. CSR_GPIO_IN,
  1455. CSR_RESET,
  1456. CSR_GP_CNTRL,
  1457. CSR_HW_REV,
  1458. CSR_EEPROM_REG,
  1459. CSR_EEPROM_GP,
  1460. CSR_OTP_GP_REG,
  1461. CSR_GIO_REG,
  1462. CSR_GP_UCODE_REG,
  1463. CSR_GP_DRIVER_REG,
  1464. CSR_UCODE_DRV_GP1,
  1465. CSR_UCODE_DRV_GP2,
  1466. CSR_LED_REG,
  1467. CSR_DRAM_INT_TBL_REG,
  1468. CSR_GIO_CHICKEN_BITS,
  1469. CSR_ANA_PLL_CFG,
  1470. CSR_HW_REV_WA_REG,
  1471. CSR_DBG_HPET_MEM_REG
  1472. };
  1473. IWL_ERR(trans, "CSR values:\n");
  1474. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1475. "CSR_INT_PERIODIC_REG)\n");
  1476. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1477. IWL_ERR(trans, " %25s: 0X%08x\n",
  1478. get_csr_string(csr_tbl[i]),
  1479. iwl_read32(trans, csr_tbl[i]));
  1480. }
  1481. }
  1482. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1483. /* create and remove of files */
  1484. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1485. if (!debugfs_create_file(#name, mode, parent, trans, \
  1486. &iwl_dbgfs_##name##_ops)) \
  1487. return -ENOMEM; \
  1488. } while (0)
  1489. /* file operation */
  1490. #define DEBUGFS_READ_FUNC(name) \
  1491. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1492. char __user *user_buf, \
  1493. size_t count, loff_t *ppos);
  1494. #define DEBUGFS_WRITE_FUNC(name) \
  1495. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1496. const char __user *user_buf, \
  1497. size_t count, loff_t *ppos);
  1498. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1499. {
  1500. file->private_data = inode->i_private;
  1501. return 0;
  1502. }
  1503. #define DEBUGFS_READ_FILE_OPS(name) \
  1504. DEBUGFS_READ_FUNC(name); \
  1505. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1506. .read = iwl_dbgfs_##name##_read, \
  1507. .open = iwl_dbgfs_open_file_generic, \
  1508. .llseek = generic_file_llseek, \
  1509. };
  1510. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1511. DEBUGFS_WRITE_FUNC(name); \
  1512. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1513. .write = iwl_dbgfs_##name##_write, \
  1514. .open = iwl_dbgfs_open_file_generic, \
  1515. .llseek = generic_file_llseek, \
  1516. };
  1517. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1518. DEBUGFS_READ_FUNC(name); \
  1519. DEBUGFS_WRITE_FUNC(name); \
  1520. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1521. .write = iwl_dbgfs_##name##_write, \
  1522. .read = iwl_dbgfs_##name##_read, \
  1523. .open = iwl_dbgfs_open_file_generic, \
  1524. .llseek = generic_file_llseek, \
  1525. };
  1526. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1527. char __user *user_buf,
  1528. size_t count, loff_t *ppos)
  1529. {
  1530. struct iwl_trans *trans = file->private_data;
  1531. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1532. struct iwl_tx_queue *txq;
  1533. struct iwl_queue *q;
  1534. char *buf;
  1535. int pos = 0;
  1536. int cnt;
  1537. int ret;
  1538. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1539. if (!trans_pcie->txq) {
  1540. IWL_ERR(trans, "txq not ready\n");
  1541. return -EAGAIN;
  1542. }
  1543. buf = kzalloc(bufsz, GFP_KERNEL);
  1544. if (!buf)
  1545. return -ENOMEM;
  1546. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1547. txq = &trans_pcie->txq[cnt];
  1548. q = &txq->q;
  1549. pos += scnprintf(buf + pos, bufsz - pos,
  1550. "hwq %.2d: read=%u write=%u stop=%d"
  1551. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1552. cnt, q->read_ptr, q->write_ptr,
  1553. !!test_bit(cnt, trans_pcie->queue_stopped),
  1554. txq->swq_id, txq->swq_id & 3,
  1555. (txq->swq_id >> 2) & 0x1f);
  1556. if (cnt >= 4)
  1557. continue;
  1558. /* for the ACs, display the stop count too */
  1559. pos += scnprintf(buf + pos, bufsz - pos,
  1560. " stop-count: %d\n",
  1561. atomic_read(&trans_pcie->queue_stop_count[cnt]));
  1562. }
  1563. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1564. kfree(buf);
  1565. return ret;
  1566. }
  1567. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1568. char __user *user_buf,
  1569. size_t count, loff_t *ppos) {
  1570. struct iwl_trans *trans = file->private_data;
  1571. struct iwl_trans_pcie *trans_pcie =
  1572. IWL_TRANS_GET_PCIE_TRANS(trans);
  1573. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1574. char buf[256];
  1575. int pos = 0;
  1576. const size_t bufsz = sizeof(buf);
  1577. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1578. rxq->read);
  1579. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1580. rxq->write);
  1581. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1582. rxq->free_count);
  1583. if (rxq->rb_stts) {
  1584. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1585. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1586. } else {
  1587. pos += scnprintf(buf + pos, bufsz - pos,
  1588. "closed_rb_num: Not Allocated\n");
  1589. }
  1590. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1591. }
  1592. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1593. char __user *user_buf,
  1594. size_t count, loff_t *ppos)
  1595. {
  1596. struct iwl_trans *trans = file->private_data;
  1597. char *buf;
  1598. int pos = 0;
  1599. ssize_t ret = -ENOMEM;
  1600. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1601. if (buf) {
  1602. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1603. kfree(buf);
  1604. }
  1605. return ret;
  1606. }
  1607. static ssize_t iwl_dbgfs_log_event_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. u32 event_log_flag;
  1613. char buf[8];
  1614. int buf_size;
  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", &event_log_flag) != 1)
  1620. return -EFAULT;
  1621. if (event_log_flag == 1)
  1622. iwl_dump_nic_event_log(trans, true, NULL, false);
  1623. return count;
  1624. }
  1625. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1626. char __user *user_buf,
  1627. size_t count, loff_t *ppos) {
  1628. struct iwl_trans *trans = file->private_data;
  1629. struct iwl_trans_pcie *trans_pcie =
  1630. IWL_TRANS_GET_PCIE_TRANS(trans);
  1631. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1632. int pos = 0;
  1633. char *buf;
  1634. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1635. ssize_t ret;
  1636. buf = kzalloc(bufsz, GFP_KERNEL);
  1637. if (!buf) {
  1638. IWL_ERR(trans, "Can not allocate Buffer\n");
  1639. return -ENOMEM;
  1640. }
  1641. pos += scnprintf(buf + pos, bufsz - pos,
  1642. "Interrupt Statistics Report:\n");
  1643. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1644. isr_stats->hw);
  1645. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1646. isr_stats->sw);
  1647. if (isr_stats->sw || isr_stats->hw) {
  1648. pos += scnprintf(buf + pos, bufsz - pos,
  1649. "\tLast Restarting Code: 0x%X\n",
  1650. isr_stats->err_code);
  1651. }
  1652. #ifdef CONFIG_IWLWIFI_DEBUG
  1653. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1654. isr_stats->sch);
  1655. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1656. isr_stats->alive);
  1657. #endif
  1658. pos += scnprintf(buf + pos, bufsz - pos,
  1659. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1660. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1661. isr_stats->ctkill);
  1662. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1663. isr_stats->wakeup);
  1664. pos += scnprintf(buf + pos, bufsz - pos,
  1665. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1666. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1667. isr_stats->tx);
  1668. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1669. isr_stats->unhandled);
  1670. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1671. kfree(buf);
  1672. return ret;
  1673. }
  1674. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1675. const char __user *user_buf,
  1676. size_t count, loff_t *ppos)
  1677. {
  1678. struct iwl_trans *trans = file->private_data;
  1679. struct iwl_trans_pcie *trans_pcie =
  1680. IWL_TRANS_GET_PCIE_TRANS(trans);
  1681. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1682. char buf[8];
  1683. int buf_size;
  1684. u32 reset_flag;
  1685. memset(buf, 0, sizeof(buf));
  1686. buf_size = min(count, sizeof(buf) - 1);
  1687. if (copy_from_user(buf, user_buf, buf_size))
  1688. return -EFAULT;
  1689. if (sscanf(buf, "%x", &reset_flag) != 1)
  1690. return -EFAULT;
  1691. if (reset_flag == 0)
  1692. memset(isr_stats, 0, sizeof(*isr_stats));
  1693. return count;
  1694. }
  1695. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1696. const char __user *user_buf,
  1697. size_t count, loff_t *ppos)
  1698. {
  1699. struct iwl_trans *trans = file->private_data;
  1700. char buf[8];
  1701. int buf_size;
  1702. int csr;
  1703. memset(buf, 0, sizeof(buf));
  1704. buf_size = min(count, sizeof(buf) - 1);
  1705. if (copy_from_user(buf, user_buf, buf_size))
  1706. return -EFAULT;
  1707. if (sscanf(buf, "%d", &csr) != 1)
  1708. return -EFAULT;
  1709. iwl_dump_csr(trans);
  1710. return count;
  1711. }
  1712. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1713. char __user *user_buf,
  1714. size_t count, loff_t *ppos)
  1715. {
  1716. struct iwl_trans *trans = file->private_data;
  1717. char *buf;
  1718. int pos = 0;
  1719. ssize_t ret = -EFAULT;
  1720. ret = pos = iwl_dump_fh(trans, &buf, true);
  1721. if (buf) {
  1722. ret = simple_read_from_buffer(user_buf,
  1723. count, ppos, buf, pos);
  1724. kfree(buf);
  1725. }
  1726. return ret;
  1727. }
  1728. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1729. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1730. DEBUGFS_READ_FILE_OPS(fh_reg);
  1731. DEBUGFS_READ_FILE_OPS(rx_queue);
  1732. DEBUGFS_READ_FILE_OPS(tx_queue);
  1733. DEBUGFS_WRITE_FILE_OPS(csr);
  1734. /*
  1735. * Create the debugfs files and directories
  1736. *
  1737. */
  1738. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1739. struct dentry *dir)
  1740. {
  1741. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1742. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1743. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1744. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1745. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1746. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1747. return 0;
  1748. }
  1749. #else
  1750. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1751. struct dentry *dir)
  1752. { return 0; }
  1753. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1754. const struct iwl_trans_ops trans_ops_pcie = {
  1755. .start_hw = iwl_trans_pcie_start_hw,
  1756. .stop_hw = iwl_trans_pcie_stop_hw,
  1757. .fw_alive = iwl_trans_pcie_fw_alive,
  1758. .start_device = iwl_trans_pcie_start_device,
  1759. .stop_device = iwl_trans_pcie_stop_device,
  1760. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1761. .send_cmd = iwl_trans_pcie_send_cmd,
  1762. .tx = iwl_trans_pcie_tx,
  1763. .reclaim = iwl_trans_pcie_reclaim,
  1764. .tx_agg_disable = iwl_trans_pcie_tx_agg_disable,
  1765. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1766. .tx_agg_setup = iwl_trans_pcie_tx_agg_setup,
  1767. .kick_nic = iwl_trans_pcie_kick_nic,
  1768. .free = iwl_trans_pcie_free,
  1769. .stop_queue = iwl_trans_pcie_stop_queue,
  1770. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1771. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1772. .check_stuck_queue = iwl_trans_pcie_check_stuck_queue,
  1773. #ifdef CONFIG_PM_SLEEP
  1774. .suspend = iwl_trans_pcie_suspend,
  1775. .resume = iwl_trans_pcie_resume,
  1776. #endif
  1777. .write8 = iwl_trans_pcie_write8,
  1778. .write32 = iwl_trans_pcie_write32,
  1779. .read32 = iwl_trans_pcie_read32,
  1780. };
  1781. /* PCI registers */
  1782. #define PCI_CFG_RETRY_TIMEOUT 0x041
  1783. struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd,
  1784. struct pci_dev *pdev,
  1785. const struct pci_device_id *ent)
  1786. {
  1787. struct iwl_trans_pcie *trans_pcie;
  1788. struct iwl_trans *trans;
  1789. u16 pci_cmd;
  1790. int err;
  1791. trans = kzalloc(sizeof(struct iwl_trans) +
  1792. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1793. if (WARN_ON(!trans))
  1794. return NULL;
  1795. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1796. trans->ops = &trans_ops_pcie;
  1797. trans->shrd = shrd;
  1798. trans_pcie->trans = trans;
  1799. spin_lock_init(&trans->hcmd_lock);
  1800. /* W/A - seems to solve weird behavior. We need to remove this if we
  1801. * don't want to stay in L1 all the time. This wastes a lot of power */
  1802. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  1803. PCIE_LINK_STATE_CLKPM);
  1804. if (pci_enable_device(pdev)) {
  1805. err = -ENODEV;
  1806. goto out_no_pci;
  1807. }
  1808. pci_set_master(pdev);
  1809. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1810. if (!err)
  1811. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1812. if (err) {
  1813. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1814. if (!err)
  1815. err = pci_set_consistent_dma_mask(pdev,
  1816. DMA_BIT_MASK(32));
  1817. /* both attempts failed: */
  1818. if (err) {
  1819. dev_printk(KERN_ERR, &pdev->dev,
  1820. "No suitable DMA available.\n");
  1821. goto out_pci_disable_device;
  1822. }
  1823. }
  1824. err = pci_request_regions(pdev, DRV_NAME);
  1825. if (err) {
  1826. dev_printk(KERN_ERR, &pdev->dev, "pci_request_regions failed");
  1827. goto out_pci_disable_device;
  1828. }
  1829. trans_pcie->hw_base = pci_iomap(pdev, 0, 0);
  1830. if (!trans_pcie->hw_base) {
  1831. dev_printk(KERN_ERR, &pdev->dev, "pci_iomap failed");
  1832. err = -ENODEV;
  1833. goto out_pci_release_regions;
  1834. }
  1835. dev_printk(KERN_INFO, &pdev->dev,
  1836. "pci_resource_len = 0x%08llx\n",
  1837. (unsigned long long) pci_resource_len(pdev, 0));
  1838. dev_printk(KERN_INFO, &pdev->dev,
  1839. "pci_resource_base = %p\n", trans_pcie->hw_base);
  1840. dev_printk(KERN_INFO, &pdev->dev,
  1841. "HW Revision ID = 0x%X\n", pdev->revision);
  1842. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1843. * PCI Tx retries from interfering with C3 CPU state */
  1844. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1845. err = pci_enable_msi(pdev);
  1846. if (err)
  1847. dev_printk(KERN_ERR, &pdev->dev,
  1848. "pci_enable_msi failed(0X%x)", err);
  1849. trans->dev = &pdev->dev;
  1850. trans->irq = pdev->irq;
  1851. trans_pcie->pci_dev = pdev;
  1852. /* TODO: Move this away, not needed if not MSI */
  1853. /* enable rfkill interrupt: hw bug w/a */
  1854. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1855. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1856. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1857. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1858. }
  1859. return trans;
  1860. out_pci_release_regions:
  1861. pci_release_regions(pdev);
  1862. out_pci_disable_device:
  1863. pci_disable_device(pdev);
  1864. out_no_pci:
  1865. kfree(trans);
  1866. return NULL;
  1867. }