trans.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  77. {
  78. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  79. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  80. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  81. ~APMG_PS_CTRL_MSK_PWR_SRC);
  82. else
  83. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  84. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  85. ~APMG_PS_CTRL_MSK_PWR_SRC);
  86. }
  87. /* PCI registers */
  88. #define PCI_CFG_RETRY_TIMEOUT 0x041
  89. static void iwl_pcie_apm_config(struct iwl_trans *trans)
  90. {
  91. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  92. u16 lctl;
  93. /*
  94. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  95. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  96. * If so (likely), disable L0S, so device moves directly L0->L1;
  97. * costs negligible amount of power savings.
  98. * If not (unlikely), enable L0S, so there is at least some
  99. * power savings, even without L1.
  100. */
  101. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  102. if (lctl & PCI_EXP_LNKCTL_ASPM_L1) {
  103. /* L1-ASPM enabled; disable(!) L0S */
  104. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  105. dev_info(trans->dev, "L1 Enabled; Disabling L0S\n");
  106. } else {
  107. /* L1-ASPM disabled; enable(!) L0S */
  108. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  109. dev_info(trans->dev, "L1 Disabled; Enabling L0S\n");
  110. }
  111. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  112. }
  113. /*
  114. * Start up NIC's basic functionality after it has been reset
  115. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  116. * NOTE: This does not load uCode nor start the embedded processor
  117. */
  118. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  119. {
  120. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  121. int ret = 0;
  122. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  123. /*
  124. * Use "set_bit" below rather than "write", to preserve any hardware
  125. * bits already set by default after reset.
  126. */
  127. /* Disable L0S exit timer (platform NMI Work/Around) */
  128. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  129. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  130. /*
  131. * Disable L0s without affecting L1;
  132. * don't wait for ICH L0s (ICH bug W/A)
  133. */
  134. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  135. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  136. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  137. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  138. /*
  139. * Enable HAP INTA (interrupt from management bus) to
  140. * wake device's PCI Express link L1a -> L0s
  141. */
  142. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  143. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  144. iwl_pcie_apm_config(trans);
  145. /* Configure analog phase-lock-loop before activating to D0A */
  146. if (trans->cfg->base_params->pll_cfg_val)
  147. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  148. trans->cfg->base_params->pll_cfg_val);
  149. /*
  150. * Set "initialization complete" bit to move adapter from
  151. * D0U* --> D0A* (powered-up active) state.
  152. */
  153. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  154. /*
  155. * Wait for clock stabilization; once stabilized, access to
  156. * device-internal resources is supported, e.g. iwl_write_prph()
  157. * and accesses to uCode SRAM.
  158. */
  159. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  160. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  161. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  162. if (ret < 0) {
  163. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  164. goto out;
  165. }
  166. /*
  167. * Enable DMA clock and wait for it to stabilize.
  168. *
  169. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  170. * do not disable clocks. This preserves any hardware bits already
  171. * set by default in "CLK_CTRL_REG" after reset.
  172. */
  173. iwl_write_prph(trans, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  174. udelay(20);
  175. /* Disable L1-Active */
  176. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  177. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  178. set_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  179. out:
  180. return ret;
  181. }
  182. static int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  183. {
  184. int ret = 0;
  185. /* stop device's busmaster DMA activity */
  186. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  187. ret = iwl_poll_bit(trans, CSR_RESET,
  188. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  189. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  190. if (ret)
  191. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  192. IWL_DEBUG_INFO(trans, "stop master\n");
  193. return ret;
  194. }
  195. static void iwl_pcie_apm_stop(struct iwl_trans *trans)
  196. {
  197. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  198. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  199. clear_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  200. /* Stop device's DMA activity */
  201. iwl_pcie_apm_stop_master(trans);
  202. /* Reset the entire device */
  203. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  204. udelay(10);
  205. /*
  206. * Clear "initialization complete" bit to move adapter from
  207. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  208. */
  209. iwl_clear_bit(trans, CSR_GP_CNTRL,
  210. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  211. }
  212. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  213. {
  214. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  215. unsigned long flags;
  216. /* nic_init */
  217. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  218. iwl_pcie_apm_init(trans);
  219. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  220. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  221. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  222. iwl_pcie_set_pwr(trans, false);
  223. iwl_op_mode_nic_config(trans->op_mode);
  224. /* Allocate the RX queue, or reset if it is already allocated */
  225. iwl_pcie_rx_init(trans);
  226. /* Allocate or reset and init all Tx and Command queues */
  227. if (iwl_pcie_tx_init(trans))
  228. return -ENOMEM;
  229. if (trans->cfg->base_params->shadow_reg_enable) {
  230. /* enable shadow regs in HW */
  231. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  232. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  233. }
  234. return 0;
  235. }
  236. #define HW_READY_TIMEOUT (50)
  237. /* Note: returns poll_bit return value, which is >= 0 if success */
  238. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  239. {
  240. int ret;
  241. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  242. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  243. /* See if we got it */
  244. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  245. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  246. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  247. HW_READY_TIMEOUT);
  248. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  249. return ret;
  250. }
  251. /* Note: returns standard 0/-ERROR code */
  252. static int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  253. {
  254. int ret;
  255. int t = 0;
  256. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  257. ret = iwl_pcie_set_hw_ready(trans);
  258. /* If the card is ready, exit 0 */
  259. if (ret >= 0)
  260. return 0;
  261. /* If HW is not ready, prepare the conditions to check again */
  262. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  263. CSR_HW_IF_CONFIG_REG_PREPARE);
  264. do {
  265. ret = iwl_pcie_set_hw_ready(trans);
  266. if (ret >= 0)
  267. return 0;
  268. usleep_range(200, 1000);
  269. t += 200;
  270. } while (t < 150000);
  271. return ret;
  272. }
  273. /*
  274. * ucode
  275. */
  276. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, u32 dst_addr,
  277. dma_addr_t phy_addr, u32 byte_cnt)
  278. {
  279. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  280. int ret;
  281. trans_pcie->ucode_write_complete = false;
  282. iwl_write_direct32(trans,
  283. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  284. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  285. iwl_write_direct32(trans,
  286. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  287. dst_addr);
  288. iwl_write_direct32(trans,
  289. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  290. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  291. iwl_write_direct32(trans,
  292. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  293. (iwl_get_dma_hi_addr(phy_addr)
  294. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  295. iwl_write_direct32(trans,
  296. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  297. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  298. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  299. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  300. iwl_write_direct32(trans,
  301. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  302. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  303. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  304. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  305. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  306. trans_pcie->ucode_write_complete, 5 * HZ);
  307. if (!ret) {
  308. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  309. return -ETIMEDOUT;
  310. }
  311. return 0;
  312. }
  313. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  314. const struct fw_desc *section)
  315. {
  316. u8 *v_addr;
  317. dma_addr_t p_addr;
  318. u32 offset;
  319. int ret = 0;
  320. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  321. section_num);
  322. v_addr = dma_alloc_coherent(trans->dev, PAGE_SIZE, &p_addr, GFP_KERNEL);
  323. if (!v_addr)
  324. return -ENOMEM;
  325. for (offset = 0; offset < section->len; offset += PAGE_SIZE) {
  326. u32 copy_size;
  327. copy_size = min_t(u32, PAGE_SIZE, section->len - offset);
  328. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  329. ret = iwl_pcie_load_firmware_chunk(trans,
  330. section->offset + offset,
  331. p_addr, copy_size);
  332. if (ret) {
  333. IWL_ERR(trans,
  334. "Could not load the [%d] uCode section\n",
  335. section_num);
  336. break;
  337. }
  338. }
  339. dma_free_coherent(trans->dev, PAGE_SIZE, v_addr, p_addr);
  340. return ret;
  341. }
  342. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  343. const struct fw_img *image)
  344. {
  345. int i, ret = 0;
  346. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++) {
  347. if (!image->sec[i].data)
  348. break;
  349. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  350. if (ret)
  351. return ret;
  352. }
  353. /* Remove all resets to allow NIC to operate */
  354. iwl_write32(trans, CSR_RESET, 0);
  355. return 0;
  356. }
  357. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  358. const struct fw_img *fw, bool run_in_rfkill)
  359. {
  360. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  361. int ret;
  362. bool hw_rfkill;
  363. /* This may fail if AMT took ownership of the device */
  364. if (iwl_pcie_prepare_card_hw(trans)) {
  365. IWL_WARN(trans, "Exit HW not ready\n");
  366. return -EIO;
  367. }
  368. clear_bit(STATUS_FW_ERROR, &trans_pcie->status);
  369. iwl_enable_rfkill_int(trans);
  370. /* If platform's RF_KILL switch is NOT set to KILL */
  371. hw_rfkill = iwl_is_rfkill_set(trans);
  372. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  373. if (hw_rfkill && !run_in_rfkill)
  374. return -ERFKILL;
  375. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  376. ret = iwl_pcie_nic_init(trans);
  377. if (ret) {
  378. IWL_ERR(trans, "Unable to init nic\n");
  379. return ret;
  380. }
  381. /* make sure rfkill handshake bits are cleared */
  382. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  383. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  384. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  385. /* clear (again), then enable host interrupts */
  386. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  387. iwl_enable_interrupts(trans);
  388. /* really make sure rfkill handshake bits are cleared */
  389. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  390. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  391. /* Load the given image to the HW */
  392. return iwl_pcie_load_given_ucode(trans, fw);
  393. }
  394. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  395. {
  396. iwl_pcie_reset_ict(trans);
  397. iwl_pcie_tx_start(trans, scd_addr);
  398. }
  399. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  400. {
  401. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  402. unsigned long flags;
  403. /* tell the device to stop sending interrupts */
  404. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  405. iwl_disable_interrupts(trans);
  406. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  407. /* device going down, Stop using ICT table */
  408. iwl_pcie_disable_ict(trans);
  409. /*
  410. * If a HW restart happens during firmware loading,
  411. * then the firmware loading might call this function
  412. * and later it might be called again due to the
  413. * restart. So don't process again if the device is
  414. * already dead.
  415. */
  416. if (test_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status)) {
  417. iwl_pcie_tx_stop(trans);
  418. iwl_pcie_rx_stop(trans);
  419. /* Power-down device's busmaster DMA clocks */
  420. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  421. APMG_CLK_VAL_DMA_CLK_RQT);
  422. udelay(5);
  423. }
  424. /* Make sure (redundant) we've released our request to stay awake */
  425. iwl_clear_bit(trans, CSR_GP_CNTRL,
  426. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  427. /* Stop the device, and put it in low power state */
  428. iwl_pcie_apm_stop(trans);
  429. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  430. * Clean again the interrupt here
  431. */
  432. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  433. iwl_disable_interrupts(trans);
  434. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  435. iwl_enable_rfkill_int(trans);
  436. /* stop and reset the on-board processor */
  437. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  438. /* clear all status bits */
  439. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  440. clear_bit(STATUS_INT_ENABLED, &trans_pcie->status);
  441. clear_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  442. clear_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  443. clear_bit(STATUS_RFKILL, &trans_pcie->status);
  444. }
  445. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans)
  446. {
  447. /* let the ucode operate on its own */
  448. iwl_write32(trans, CSR_UCODE_DRV_GP1_SET,
  449. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  450. iwl_disable_interrupts(trans);
  451. iwl_pcie_disable_ict(trans);
  452. iwl_clear_bit(trans, CSR_GP_CNTRL,
  453. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  454. iwl_clear_bit(trans, CSR_GP_CNTRL,
  455. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  456. /*
  457. * reset TX queues -- some of their registers reset during S3
  458. * so if we don't reset everything here the D3 image would try
  459. * to execute some invalid memory upon resume
  460. */
  461. iwl_trans_pcie_tx_reset(trans);
  462. iwl_pcie_set_pwr(trans, true);
  463. }
  464. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  465. enum iwl_d3_status *status)
  466. {
  467. u32 val;
  468. int ret;
  469. iwl_pcie_set_pwr(trans, false);
  470. val = iwl_read32(trans, CSR_RESET);
  471. if (val & CSR_RESET_REG_FLAG_NEVO_RESET) {
  472. *status = IWL_D3_STATUS_RESET;
  473. return 0;
  474. }
  475. /*
  476. * Also enables interrupts - none will happen as the device doesn't
  477. * know we're waking it up, only when the opmode actually tells it
  478. * after this call.
  479. */
  480. iwl_pcie_reset_ict(trans);
  481. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  482. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  483. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  484. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  485. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  486. 25000);
  487. if (ret) {
  488. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  489. return ret;
  490. }
  491. iwl_trans_pcie_tx_reset(trans);
  492. ret = iwl_pcie_rx_init(trans);
  493. if (ret) {
  494. IWL_ERR(trans, "Failed to resume the device (RX reset)\n");
  495. return ret;
  496. }
  497. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  498. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  499. *status = IWL_D3_STATUS_ALIVE;
  500. return 0;
  501. }
  502. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  503. {
  504. bool hw_rfkill;
  505. int err;
  506. err = iwl_pcie_prepare_card_hw(trans);
  507. if (err) {
  508. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  509. return err;
  510. }
  511. iwl_pcie_apm_init(trans);
  512. /* From now on, the op_mode will be kept updated about RF kill state */
  513. iwl_enable_rfkill_int(trans);
  514. hw_rfkill = iwl_is_rfkill_set(trans);
  515. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  516. return 0;
  517. }
  518. static void iwl_trans_pcie_stop_hw(struct iwl_trans *trans,
  519. bool op_mode_leaving)
  520. {
  521. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  522. bool hw_rfkill;
  523. unsigned long flags;
  524. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  525. iwl_disable_interrupts(trans);
  526. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  527. iwl_pcie_apm_stop(trans);
  528. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  529. iwl_disable_interrupts(trans);
  530. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  531. iwl_pcie_disable_ict(trans);
  532. if (!op_mode_leaving) {
  533. /*
  534. * Even if we stop the HW, we still want the RF kill
  535. * interrupt
  536. */
  537. iwl_enable_rfkill_int(trans);
  538. /*
  539. * Check again since the RF kill state may have changed while
  540. * all the interrupts were disabled, in this case we couldn't
  541. * receive the RF kill interrupt and update the state in the
  542. * op_mode.
  543. */
  544. hw_rfkill = iwl_is_rfkill_set(trans);
  545. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  546. }
  547. }
  548. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  549. {
  550. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  551. }
  552. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  553. {
  554. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  555. }
  556. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  557. {
  558. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  559. }
  560. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  561. {
  562. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  563. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  564. }
  565. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  566. u32 val)
  567. {
  568. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  569. ((addr & 0x0000FFFF) | (3 << 24)));
  570. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  571. }
  572. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  573. const struct iwl_trans_config *trans_cfg)
  574. {
  575. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  576. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  577. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  578. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  579. trans_pcie->n_no_reclaim_cmds = 0;
  580. else
  581. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  582. if (trans_pcie->n_no_reclaim_cmds)
  583. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  584. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  585. trans_pcie->rx_buf_size_8k = trans_cfg->rx_buf_size_8k;
  586. if (trans_pcie->rx_buf_size_8k)
  587. trans_pcie->rx_page_order = get_order(8 * 1024);
  588. else
  589. trans_pcie->rx_page_order = get_order(4 * 1024);
  590. trans_pcie->wd_timeout =
  591. msecs_to_jiffies(trans_cfg->queue_watchdog_timeout);
  592. trans_pcie->command_names = trans_cfg->command_names;
  593. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  594. }
  595. void iwl_trans_pcie_free(struct iwl_trans *trans)
  596. {
  597. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  598. synchronize_irq(trans_pcie->pci_dev->irq);
  599. tasklet_kill(&trans_pcie->irq_tasklet);
  600. iwl_pcie_tx_free(trans);
  601. iwl_pcie_rx_free(trans);
  602. free_irq(trans_pcie->pci_dev->irq, trans);
  603. iwl_pcie_free_ict(trans);
  604. pci_disable_msi(trans_pcie->pci_dev);
  605. iounmap(trans_pcie->hw_base);
  606. pci_release_regions(trans_pcie->pci_dev);
  607. pci_disable_device(trans_pcie->pci_dev);
  608. kmem_cache_destroy(trans->dev_cmd_pool);
  609. kfree(trans);
  610. }
  611. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  612. {
  613. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  614. if (state)
  615. set_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  616. else
  617. clear_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  618. }
  619. #ifdef CONFIG_PM_SLEEP
  620. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  621. {
  622. return 0;
  623. }
  624. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  625. {
  626. bool hw_rfkill;
  627. iwl_enable_rfkill_int(trans);
  628. hw_rfkill = iwl_is_rfkill_set(trans);
  629. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  630. return 0;
  631. }
  632. #endif /* CONFIG_PM_SLEEP */
  633. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent)
  634. {
  635. int ret;
  636. lockdep_assert_held(&trans->reg_lock);
  637. /* this bit wakes up the NIC */
  638. __iwl_set_bit(trans, CSR_GP_CNTRL,
  639. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  640. /*
  641. * These bits say the device is running, and should keep running for
  642. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  643. * but they do not indicate that embedded SRAM is restored yet;
  644. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  645. * to/from host DRAM when sleeping/waking for power-saving.
  646. * Each direction takes approximately 1/4 millisecond; with this
  647. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  648. * series of register accesses are expected (e.g. reading Event Log),
  649. * to keep device from sleeping.
  650. *
  651. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  652. * SRAM is okay/restored. We don't check that here because this call
  653. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  654. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  655. *
  656. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  657. * and do not save/restore SRAM when power cycling.
  658. */
  659. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  660. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  661. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  662. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  663. if (unlikely(ret < 0)) {
  664. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  665. if (!silent) {
  666. u32 val = iwl_read32(trans, CSR_GP_CNTRL);
  667. WARN_ONCE(1,
  668. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  669. val);
  670. return false;
  671. }
  672. }
  673. return true;
  674. }
  675. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans)
  676. {
  677. lockdep_assert_held(&trans->reg_lock);
  678. __iwl_clear_bit(trans, CSR_GP_CNTRL,
  679. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  680. /*
  681. * Above we read the CSR_GP_CNTRL register, which will flush
  682. * any previous writes, but we need the write that clears the
  683. * MAC_ACCESS_REQ bit to be performed before any other writes
  684. * scheduled on different CPUs (after we drop reg_lock).
  685. */
  686. mmiowb();
  687. }
  688. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  689. void *buf, int dwords)
  690. {
  691. unsigned long flags;
  692. int offs, ret = 0;
  693. u32 *vals = buf;
  694. spin_lock_irqsave(&trans->reg_lock, flags);
  695. if (iwl_trans_grab_nic_access(trans, false)) {
  696. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  697. for (offs = 0; offs < dwords; offs++)
  698. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  699. iwl_trans_release_nic_access(trans);
  700. } else {
  701. ret = -EBUSY;
  702. }
  703. spin_unlock_irqrestore(&trans->reg_lock, flags);
  704. return ret;
  705. }
  706. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  707. void *buf, int dwords)
  708. {
  709. unsigned long flags;
  710. int offs, ret = 0;
  711. u32 *vals = buf;
  712. spin_lock_irqsave(&trans->reg_lock, flags);
  713. if (iwl_trans_grab_nic_access(trans, false)) {
  714. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  715. for (offs = 0; offs < dwords; offs++)
  716. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  717. vals ? vals[offs] : 0);
  718. iwl_trans_release_nic_access(trans);
  719. } else {
  720. ret = -EBUSY;
  721. }
  722. spin_unlock_irqrestore(&trans->reg_lock, flags);
  723. return ret;
  724. }
  725. #define IWL_FLUSH_WAIT_MS 2000
  726. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans)
  727. {
  728. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  729. struct iwl_txq *txq;
  730. struct iwl_queue *q;
  731. int cnt;
  732. unsigned long now = jiffies;
  733. u32 scd_sram_addr;
  734. u8 buf[16];
  735. int ret = 0;
  736. /* waiting for all the tx frames complete might take a while */
  737. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  738. if (cnt == trans_pcie->cmd_queue)
  739. continue;
  740. txq = &trans_pcie->txq[cnt];
  741. q = &txq->q;
  742. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  743. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  744. msleep(1);
  745. if (q->read_ptr != q->write_ptr) {
  746. IWL_ERR(trans,
  747. "fail to flush all tx fifo queues Q %d\n", cnt);
  748. ret = -ETIMEDOUT;
  749. break;
  750. }
  751. }
  752. if (!ret)
  753. return 0;
  754. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  755. txq->q.read_ptr, txq->q.write_ptr);
  756. scd_sram_addr = trans_pcie->scd_base_addr +
  757. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  758. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  759. iwl_print_hex_error(trans, buf, sizeof(buf));
  760. for (cnt = 0; cnt < FH_TCSR_CHNL_NUM; cnt++)
  761. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", cnt,
  762. iwl_read_direct32(trans, FH_TX_TRB_REG(cnt)));
  763. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  764. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(cnt));
  765. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  766. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  767. u32 tbl_dw =
  768. iwl_trans_read_mem32(trans, trans_pcie->scd_base_addr +
  769. SCD_TRANS_TBL_OFFSET_QUEUE(cnt));
  770. if (cnt & 0x1)
  771. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  772. else
  773. tbl_dw = tbl_dw & 0x0000FFFF;
  774. IWL_ERR(trans,
  775. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  776. cnt, active ? "" : "in", fifo, tbl_dw,
  777. iwl_read_prph(trans,
  778. SCD_QUEUE_RDPTR(cnt)) & (txq->q.n_bd - 1),
  779. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  780. }
  781. return ret;
  782. }
  783. static const char *get_fh_string(int cmd)
  784. {
  785. #define IWL_CMD(x) case x: return #x
  786. switch (cmd) {
  787. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  788. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  789. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  790. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  791. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  792. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  793. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  794. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  795. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  796. default:
  797. return "UNKNOWN";
  798. }
  799. #undef IWL_CMD
  800. }
  801. int iwl_pcie_dump_fh(struct iwl_trans *trans, char **buf)
  802. {
  803. int i;
  804. static const u32 fh_tbl[] = {
  805. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  806. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  807. FH_RSCSR_CHNL0_WPTR,
  808. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  809. FH_MEM_RSSR_SHARED_CTRL_REG,
  810. FH_MEM_RSSR_RX_STATUS_REG,
  811. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  812. FH_TSSR_TX_STATUS_REG,
  813. FH_TSSR_TX_ERROR_REG
  814. };
  815. #ifdef CONFIG_IWLWIFI_DEBUGFS
  816. if (buf) {
  817. int pos = 0;
  818. size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  819. *buf = kmalloc(bufsz, GFP_KERNEL);
  820. if (!*buf)
  821. return -ENOMEM;
  822. pos += scnprintf(*buf + pos, bufsz - pos,
  823. "FH register values:\n");
  824. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  825. pos += scnprintf(*buf + pos, bufsz - pos,
  826. " %34s: 0X%08x\n",
  827. get_fh_string(fh_tbl[i]),
  828. iwl_read_direct32(trans, fh_tbl[i]));
  829. return pos;
  830. }
  831. #endif
  832. IWL_ERR(trans, "FH register values:\n");
  833. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  834. IWL_ERR(trans, " %34s: 0X%08x\n",
  835. get_fh_string(fh_tbl[i]),
  836. iwl_read_direct32(trans, fh_tbl[i]));
  837. return 0;
  838. }
  839. static const char *get_csr_string(int cmd)
  840. {
  841. #define IWL_CMD(x) case x: return #x
  842. switch (cmd) {
  843. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  844. IWL_CMD(CSR_INT_COALESCING);
  845. IWL_CMD(CSR_INT);
  846. IWL_CMD(CSR_INT_MASK);
  847. IWL_CMD(CSR_FH_INT_STATUS);
  848. IWL_CMD(CSR_GPIO_IN);
  849. IWL_CMD(CSR_RESET);
  850. IWL_CMD(CSR_GP_CNTRL);
  851. IWL_CMD(CSR_HW_REV);
  852. IWL_CMD(CSR_EEPROM_REG);
  853. IWL_CMD(CSR_EEPROM_GP);
  854. IWL_CMD(CSR_OTP_GP_REG);
  855. IWL_CMD(CSR_GIO_REG);
  856. IWL_CMD(CSR_GP_UCODE_REG);
  857. IWL_CMD(CSR_GP_DRIVER_REG);
  858. IWL_CMD(CSR_UCODE_DRV_GP1);
  859. IWL_CMD(CSR_UCODE_DRV_GP2);
  860. IWL_CMD(CSR_LED_REG);
  861. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  862. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  863. IWL_CMD(CSR_ANA_PLL_CFG);
  864. IWL_CMD(CSR_HW_REV_WA_REG);
  865. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  866. default:
  867. return "UNKNOWN";
  868. }
  869. #undef IWL_CMD
  870. }
  871. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  872. {
  873. int i;
  874. static const u32 csr_tbl[] = {
  875. CSR_HW_IF_CONFIG_REG,
  876. CSR_INT_COALESCING,
  877. CSR_INT,
  878. CSR_INT_MASK,
  879. CSR_FH_INT_STATUS,
  880. CSR_GPIO_IN,
  881. CSR_RESET,
  882. CSR_GP_CNTRL,
  883. CSR_HW_REV,
  884. CSR_EEPROM_REG,
  885. CSR_EEPROM_GP,
  886. CSR_OTP_GP_REG,
  887. CSR_GIO_REG,
  888. CSR_GP_UCODE_REG,
  889. CSR_GP_DRIVER_REG,
  890. CSR_UCODE_DRV_GP1,
  891. CSR_UCODE_DRV_GP2,
  892. CSR_LED_REG,
  893. CSR_DRAM_INT_TBL_REG,
  894. CSR_GIO_CHICKEN_BITS,
  895. CSR_ANA_PLL_CFG,
  896. CSR_HW_REV_WA_REG,
  897. CSR_DBG_HPET_MEM_REG
  898. };
  899. IWL_ERR(trans, "CSR values:\n");
  900. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  901. "CSR_INT_PERIODIC_REG)\n");
  902. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  903. IWL_ERR(trans, " %25s: 0X%08x\n",
  904. get_csr_string(csr_tbl[i]),
  905. iwl_read32(trans, csr_tbl[i]));
  906. }
  907. }
  908. #ifdef CONFIG_IWLWIFI_DEBUGFS
  909. /* create and remove of files */
  910. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  911. if (!debugfs_create_file(#name, mode, parent, trans, \
  912. &iwl_dbgfs_##name##_ops)) \
  913. goto err; \
  914. } while (0)
  915. /* file operation */
  916. #define DEBUGFS_READ_FUNC(name) \
  917. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  918. char __user *user_buf, \
  919. size_t count, loff_t *ppos);
  920. #define DEBUGFS_WRITE_FUNC(name) \
  921. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  922. const char __user *user_buf, \
  923. size_t count, loff_t *ppos);
  924. #define DEBUGFS_READ_FILE_OPS(name) \
  925. DEBUGFS_READ_FUNC(name); \
  926. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  927. .read = iwl_dbgfs_##name##_read, \
  928. .open = simple_open, \
  929. .llseek = generic_file_llseek, \
  930. };
  931. #define DEBUGFS_WRITE_FILE_OPS(name) \
  932. DEBUGFS_WRITE_FUNC(name); \
  933. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  934. .write = iwl_dbgfs_##name##_write, \
  935. .open = simple_open, \
  936. .llseek = generic_file_llseek, \
  937. };
  938. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  939. DEBUGFS_READ_FUNC(name); \
  940. DEBUGFS_WRITE_FUNC(name); \
  941. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  942. .write = iwl_dbgfs_##name##_write, \
  943. .read = iwl_dbgfs_##name##_read, \
  944. .open = simple_open, \
  945. .llseek = generic_file_llseek, \
  946. };
  947. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  948. char __user *user_buf,
  949. size_t count, loff_t *ppos)
  950. {
  951. struct iwl_trans *trans = file->private_data;
  952. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  953. struct iwl_txq *txq;
  954. struct iwl_queue *q;
  955. char *buf;
  956. int pos = 0;
  957. int cnt;
  958. int ret;
  959. size_t bufsz;
  960. bufsz = sizeof(char) * 64 * trans->cfg->base_params->num_of_queues;
  961. if (!trans_pcie->txq)
  962. return -EAGAIN;
  963. buf = kzalloc(bufsz, GFP_KERNEL);
  964. if (!buf)
  965. return -ENOMEM;
  966. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  967. txq = &trans_pcie->txq[cnt];
  968. q = &txq->q;
  969. pos += scnprintf(buf + pos, bufsz - pos,
  970. "hwq %.2d: read=%u write=%u use=%d stop=%d\n",
  971. cnt, q->read_ptr, q->write_ptr,
  972. !!test_bit(cnt, trans_pcie->queue_used),
  973. !!test_bit(cnt, trans_pcie->queue_stopped));
  974. }
  975. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  976. kfree(buf);
  977. return ret;
  978. }
  979. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  980. char __user *user_buf,
  981. size_t count, loff_t *ppos)
  982. {
  983. struct iwl_trans *trans = file->private_data;
  984. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  985. struct iwl_rxq *rxq = &trans_pcie->rxq;
  986. char buf[256];
  987. int pos = 0;
  988. const size_t bufsz = sizeof(buf);
  989. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  990. rxq->read);
  991. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  992. rxq->write);
  993. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  994. rxq->free_count);
  995. if (rxq->rb_stts) {
  996. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  997. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  998. } else {
  999. pos += scnprintf(buf + pos, bufsz - pos,
  1000. "closed_rb_num: Not Allocated\n");
  1001. }
  1002. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1003. }
  1004. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1005. char __user *user_buf,
  1006. size_t count, loff_t *ppos)
  1007. {
  1008. struct iwl_trans *trans = file->private_data;
  1009. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1010. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1011. int pos = 0;
  1012. char *buf;
  1013. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1014. ssize_t ret;
  1015. buf = kzalloc(bufsz, GFP_KERNEL);
  1016. if (!buf)
  1017. return -ENOMEM;
  1018. pos += scnprintf(buf + pos, bufsz - pos,
  1019. "Interrupt Statistics Report:\n");
  1020. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1021. isr_stats->hw);
  1022. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1023. isr_stats->sw);
  1024. if (isr_stats->sw || isr_stats->hw) {
  1025. pos += scnprintf(buf + pos, bufsz - pos,
  1026. "\tLast Restarting Code: 0x%X\n",
  1027. isr_stats->err_code);
  1028. }
  1029. #ifdef CONFIG_IWLWIFI_DEBUG
  1030. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1031. isr_stats->sch);
  1032. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1033. isr_stats->alive);
  1034. #endif
  1035. pos += scnprintf(buf + pos, bufsz - pos,
  1036. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1037. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1038. isr_stats->ctkill);
  1039. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1040. isr_stats->wakeup);
  1041. pos += scnprintf(buf + pos, bufsz - pos,
  1042. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1043. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1044. isr_stats->tx);
  1045. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1046. isr_stats->unhandled);
  1047. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1048. kfree(buf);
  1049. return ret;
  1050. }
  1051. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1052. const char __user *user_buf,
  1053. size_t count, loff_t *ppos)
  1054. {
  1055. struct iwl_trans *trans = file->private_data;
  1056. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1057. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1058. char buf[8];
  1059. int buf_size;
  1060. u32 reset_flag;
  1061. memset(buf, 0, sizeof(buf));
  1062. buf_size = min(count, sizeof(buf) - 1);
  1063. if (copy_from_user(buf, user_buf, buf_size))
  1064. return -EFAULT;
  1065. if (sscanf(buf, "%x", &reset_flag) != 1)
  1066. return -EFAULT;
  1067. if (reset_flag == 0)
  1068. memset(isr_stats, 0, sizeof(*isr_stats));
  1069. return count;
  1070. }
  1071. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1072. const char __user *user_buf,
  1073. size_t count, loff_t *ppos)
  1074. {
  1075. struct iwl_trans *trans = file->private_data;
  1076. char buf[8];
  1077. int buf_size;
  1078. int csr;
  1079. memset(buf, 0, sizeof(buf));
  1080. buf_size = min(count, sizeof(buf) - 1);
  1081. if (copy_from_user(buf, user_buf, buf_size))
  1082. return -EFAULT;
  1083. if (sscanf(buf, "%d", &csr) != 1)
  1084. return -EFAULT;
  1085. iwl_pcie_dump_csr(trans);
  1086. return count;
  1087. }
  1088. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1089. char __user *user_buf,
  1090. size_t count, loff_t *ppos)
  1091. {
  1092. struct iwl_trans *trans = file->private_data;
  1093. char *buf = NULL;
  1094. int pos = 0;
  1095. ssize_t ret = -EFAULT;
  1096. ret = pos = iwl_pcie_dump_fh(trans, &buf);
  1097. if (buf) {
  1098. ret = simple_read_from_buffer(user_buf,
  1099. count, ppos, buf, pos);
  1100. kfree(buf);
  1101. }
  1102. return ret;
  1103. }
  1104. static ssize_t iwl_dbgfs_fw_restart_write(struct file *file,
  1105. const char __user *user_buf,
  1106. size_t count, loff_t *ppos)
  1107. {
  1108. struct iwl_trans *trans = file->private_data;
  1109. if (!trans->op_mode)
  1110. return -EAGAIN;
  1111. local_bh_disable();
  1112. iwl_op_mode_nic_error(trans->op_mode);
  1113. local_bh_enable();
  1114. return count;
  1115. }
  1116. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1117. DEBUGFS_READ_FILE_OPS(fh_reg);
  1118. DEBUGFS_READ_FILE_OPS(rx_queue);
  1119. DEBUGFS_READ_FILE_OPS(tx_queue);
  1120. DEBUGFS_WRITE_FILE_OPS(csr);
  1121. DEBUGFS_WRITE_FILE_OPS(fw_restart);
  1122. /*
  1123. * Create the debugfs files and directories
  1124. *
  1125. */
  1126. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1127. struct dentry *dir)
  1128. {
  1129. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1130. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1131. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1132. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1133. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1134. DEBUGFS_ADD_FILE(fw_restart, dir, S_IWUSR);
  1135. return 0;
  1136. err:
  1137. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  1138. return -ENOMEM;
  1139. }
  1140. #else
  1141. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1142. struct dentry *dir)
  1143. {
  1144. return 0;
  1145. }
  1146. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1147. static const struct iwl_trans_ops trans_ops_pcie = {
  1148. .start_hw = iwl_trans_pcie_start_hw,
  1149. .stop_hw = iwl_trans_pcie_stop_hw,
  1150. .fw_alive = iwl_trans_pcie_fw_alive,
  1151. .start_fw = iwl_trans_pcie_start_fw,
  1152. .stop_device = iwl_trans_pcie_stop_device,
  1153. .d3_suspend = iwl_trans_pcie_d3_suspend,
  1154. .d3_resume = iwl_trans_pcie_d3_resume,
  1155. .send_cmd = iwl_trans_pcie_send_hcmd,
  1156. .tx = iwl_trans_pcie_tx,
  1157. .reclaim = iwl_trans_pcie_reclaim,
  1158. .txq_disable = iwl_trans_pcie_txq_disable,
  1159. .txq_enable = iwl_trans_pcie_txq_enable,
  1160. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1161. .wait_tx_queue_empty = iwl_trans_pcie_wait_txq_empty,
  1162. #ifdef CONFIG_PM_SLEEP
  1163. .suspend = iwl_trans_pcie_suspend,
  1164. .resume = iwl_trans_pcie_resume,
  1165. #endif
  1166. .write8 = iwl_trans_pcie_write8,
  1167. .write32 = iwl_trans_pcie_write32,
  1168. .read32 = iwl_trans_pcie_read32,
  1169. .read_prph = iwl_trans_pcie_read_prph,
  1170. .write_prph = iwl_trans_pcie_write_prph,
  1171. .read_mem = iwl_trans_pcie_read_mem,
  1172. .write_mem = iwl_trans_pcie_write_mem,
  1173. .configure = iwl_trans_pcie_configure,
  1174. .set_pmi = iwl_trans_pcie_set_pmi,
  1175. .grab_nic_access = iwl_trans_pcie_grab_nic_access,
  1176. .release_nic_access = iwl_trans_pcie_release_nic_access
  1177. };
  1178. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  1179. const struct pci_device_id *ent,
  1180. const struct iwl_cfg *cfg)
  1181. {
  1182. struct iwl_trans_pcie *trans_pcie;
  1183. struct iwl_trans *trans;
  1184. u16 pci_cmd;
  1185. int err;
  1186. trans = kzalloc(sizeof(struct iwl_trans) +
  1187. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1188. if (!trans)
  1189. return NULL;
  1190. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1191. trans->ops = &trans_ops_pcie;
  1192. trans->cfg = cfg;
  1193. trans_pcie->trans = trans;
  1194. spin_lock_init(&trans_pcie->irq_lock);
  1195. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  1196. /* W/A - seems to solve weird behavior. We need to remove this if we
  1197. * don't want to stay in L1 all the time. This wastes a lot of power */
  1198. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  1199. PCIE_LINK_STATE_CLKPM);
  1200. if (pci_enable_device(pdev)) {
  1201. err = -ENODEV;
  1202. goto out_no_pci;
  1203. }
  1204. pci_set_master(pdev);
  1205. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1206. if (!err)
  1207. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1208. if (err) {
  1209. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1210. if (!err)
  1211. err = pci_set_consistent_dma_mask(pdev,
  1212. DMA_BIT_MASK(32));
  1213. /* both attempts failed: */
  1214. if (err) {
  1215. dev_err(&pdev->dev, "No suitable DMA available\n");
  1216. goto out_pci_disable_device;
  1217. }
  1218. }
  1219. err = pci_request_regions(pdev, DRV_NAME);
  1220. if (err) {
  1221. dev_err(&pdev->dev, "pci_request_regions failed\n");
  1222. goto out_pci_disable_device;
  1223. }
  1224. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  1225. if (!trans_pcie->hw_base) {
  1226. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  1227. err = -ENODEV;
  1228. goto out_pci_release_regions;
  1229. }
  1230. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1231. * PCI Tx retries from interfering with C3 CPU state */
  1232. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1233. err = pci_enable_msi(pdev);
  1234. if (err) {
  1235. dev_err(&pdev->dev, "pci_enable_msi failed(0X%x)\n", err);
  1236. /* enable rfkill interrupt: hw bug w/a */
  1237. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1238. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1239. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1240. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1241. }
  1242. }
  1243. trans->dev = &pdev->dev;
  1244. trans_pcie->pci_dev = pdev;
  1245. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  1246. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  1247. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1248. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1249. /* Initialize the wait queue for commands */
  1250. init_waitqueue_head(&trans_pcie->wait_command_queue);
  1251. spin_lock_init(&trans->reg_lock);
  1252. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  1253. "iwl_cmd_pool:%s", dev_name(trans->dev));
  1254. trans->dev_cmd_headroom = 0;
  1255. trans->dev_cmd_pool =
  1256. kmem_cache_create(trans->dev_cmd_pool_name,
  1257. sizeof(struct iwl_device_cmd)
  1258. + trans->dev_cmd_headroom,
  1259. sizeof(void *),
  1260. SLAB_HWCACHE_ALIGN,
  1261. NULL);
  1262. if (!trans->dev_cmd_pool)
  1263. goto out_pci_disable_msi;
  1264. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1265. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1266. iwl_pcie_tasklet, (unsigned long)trans);
  1267. if (iwl_pcie_alloc_ict(trans))
  1268. goto out_free_cmd_pool;
  1269. err = request_irq(pdev->irq, iwl_pcie_isr_ict,
  1270. IRQF_SHARED, DRV_NAME, trans);
  1271. if (err) {
  1272. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  1273. goto out_free_ict;
  1274. }
  1275. return trans;
  1276. out_free_ict:
  1277. iwl_pcie_free_ict(trans);
  1278. out_free_cmd_pool:
  1279. kmem_cache_destroy(trans->dev_cmd_pool);
  1280. out_pci_disable_msi:
  1281. pci_disable_msi(pdev);
  1282. out_pci_release_regions:
  1283. pci_release_regions(pdev);
  1284. out_pci_disable_device:
  1285. pci_disable_device(pdev);
  1286. out_no_pci:
  1287. kfree(trans);
  1288. return NULL;
  1289. }