trans.c 42 KB

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