trans.c 37 KB

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