iwl-trans-pcie.c 60 KB

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