trans.c 40 KB

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