trans.c 56 KB

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