trans.c 37 KB

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