iwl-trans-pcie.c 65 KB

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