trans.c 61 KB

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