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