trans.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  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 - 2014 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 - 2014 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 "iwl-fw-error-dump.h"
  76. #include "internal.h"
  77. static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
  78. {
  79. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  80. ((reg & 0x0000ffff) | (2 << 28)));
  81. return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
  82. }
  83. static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
  84. {
  85. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
  86. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  87. ((reg & 0x0000ffff) | (3 << 28)));
  88. }
  89. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  90. {
  91. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  92. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  93. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  94. ~APMG_PS_CTRL_MSK_PWR_SRC);
  95. else
  96. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  97. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  98. ~APMG_PS_CTRL_MSK_PWR_SRC);
  99. }
  100. /* PCI registers */
  101. #define PCI_CFG_RETRY_TIMEOUT 0x041
  102. static void iwl_pcie_apm_config(struct iwl_trans *trans)
  103. {
  104. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  105. u16 lctl;
  106. /*
  107. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  108. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  109. * If so (likely), disable L0S, so device moves directly L0->L1;
  110. * costs negligible amount of power savings.
  111. * If not (unlikely), enable L0S, so there is at least some
  112. * power savings, even without L1.
  113. */
  114. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  115. if (lctl & PCI_EXP_LNKCTL_ASPM_L1) {
  116. /* L1-ASPM enabled; disable(!) L0S */
  117. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  118. dev_info(trans->dev, "L1 Enabled; Disabling L0S\n");
  119. } else {
  120. /* L1-ASPM disabled; enable(!) L0S */
  121. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  122. dev_info(trans->dev, "L1 Disabled; Enabling L0S\n");
  123. }
  124. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  125. }
  126. /*
  127. * Start up NIC's basic functionality after it has been reset
  128. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  129. * NOTE: This does not load uCode nor start the embedded processor
  130. */
  131. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  132. {
  133. int ret = 0;
  134. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  135. /*
  136. * Use "set_bit" below rather than "write", to preserve any hardware
  137. * bits already set by default after reset.
  138. */
  139. /* Disable L0S exit timer (platform NMI Work/Around) */
  140. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  141. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  142. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  143. /*
  144. * Disable L0s without affecting L1;
  145. * don't wait for ICH L0s (ICH bug W/A)
  146. */
  147. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  148. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  149. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  150. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  151. /*
  152. * Enable HAP INTA (interrupt from management bus) to
  153. * wake device's PCI Express link L1a -> L0s
  154. */
  155. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  156. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  157. iwl_pcie_apm_config(trans);
  158. /* Configure analog phase-lock-loop before activating to D0A */
  159. if (trans->cfg->base_params->pll_cfg_val)
  160. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  161. trans->cfg->base_params->pll_cfg_val);
  162. /*
  163. * Set "initialization complete" bit to move adapter from
  164. * D0U* --> D0A* (powered-up active) state.
  165. */
  166. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  167. /*
  168. * Wait for clock stabilization; once stabilized, access to
  169. * device-internal resources is supported, e.g. iwl_write_prph()
  170. * and accesses to uCode SRAM.
  171. */
  172. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  173. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  174. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  175. if (ret < 0) {
  176. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  177. goto out;
  178. }
  179. if (trans->cfg->host_interrupt_operation_mode) {
  180. /*
  181. * This is a bit of an abuse - This is needed for 7260 / 3160
  182. * only check host_interrupt_operation_mode even if this is
  183. * not related to host_interrupt_operation_mode.
  184. *
  185. * Enable the oscillator to count wake up time for L1 exit. This
  186. * consumes slightly more power (100uA) - but allows to be sure
  187. * that we wake up from L1 on time.
  188. *
  189. * This looks weird: read twice the same register, discard the
  190. * value, set a bit, and yet again, read that same register
  191. * just to discard the value. But that's the way the hardware
  192. * seems to like it.
  193. */
  194. iwl_read_prph(trans, OSC_CLK);
  195. iwl_read_prph(trans, OSC_CLK);
  196. iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
  197. iwl_read_prph(trans, OSC_CLK);
  198. iwl_read_prph(trans, OSC_CLK);
  199. }
  200. /*
  201. * Enable DMA clock and wait for it to stabilize.
  202. *
  203. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
  204. * bits do not disable clocks. This preserves any hardware
  205. * bits already set by default in "CLK_CTRL_REG" after reset.
  206. */
  207. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  208. iwl_write_prph(trans, APMG_CLK_EN_REG,
  209. APMG_CLK_VAL_DMA_CLK_RQT);
  210. udelay(20);
  211. /* Disable L1-Active */
  212. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  213. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  214. /* Clear the interrupt in APMG if the NIC is in RFKILL */
  215. iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
  216. APMG_RTC_INT_STT_RFKILL);
  217. }
  218. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  219. out:
  220. return ret;
  221. }
  222. /*
  223. * Enable LP XTAL to avoid HW bug where device may consume much power if
  224. * FW is not loaded after device reset. LP XTAL is disabled by default
  225. * after device HW reset. Do it only if XTAL is fed by internal source.
  226. * Configure device's "persistence" mode to avoid resetting XTAL again when
  227. * SHRD_HW_RST occurs in S3.
  228. */
  229. static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
  230. {
  231. int ret;
  232. u32 apmg_gp1_reg;
  233. u32 apmg_xtal_cfg_reg;
  234. u32 dl_cfg_reg;
  235. /* Force XTAL ON */
  236. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  237. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  238. /* Reset entire device - do controller reset (results in SHRD_HW_RST) */
  239. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  240. udelay(10);
  241. /*
  242. * Set "initialization complete" bit to move adapter from
  243. * D0U* --> D0A* (powered-up active) state.
  244. */
  245. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  246. /*
  247. * Wait for clock stabilization; once stabilized, access to
  248. * device-internal resources is possible.
  249. */
  250. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  251. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  252. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  253. 25000);
  254. if (WARN_ON(ret < 0)) {
  255. IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
  256. /* Release XTAL ON request */
  257. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  258. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  259. return;
  260. }
  261. /*
  262. * Clear "disable persistence" to avoid LP XTAL resetting when
  263. * SHRD_HW_RST is applied in S3.
  264. */
  265. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  266. APMG_PCIDEV_STT_VAL_PERSIST_DIS);
  267. /*
  268. * Force APMG XTAL to be active to prevent its disabling by HW
  269. * caused by APMG idle state.
  270. */
  271. apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
  272. SHR_APMG_XTAL_CFG_REG);
  273. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  274. apmg_xtal_cfg_reg |
  275. SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  276. /*
  277. * Reset entire device again - do controller reset (results in
  278. * SHRD_HW_RST). Turn MAC off before proceeding.
  279. */
  280. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  281. udelay(10);
  282. /* Enable LP XTAL by indirect access through CSR */
  283. apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
  284. iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
  285. SHR_APMG_GP1_WF_XTAL_LP_EN |
  286. SHR_APMG_GP1_CHICKEN_BIT_SELECT);
  287. /* Clear delay line clock power up */
  288. dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
  289. iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
  290. ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
  291. /*
  292. * Enable persistence mode to avoid LP XTAL resetting when
  293. * SHRD_HW_RST is applied in S3.
  294. */
  295. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  296. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  297. /*
  298. * Clear "initialization complete" bit to move adapter from
  299. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  300. */
  301. iwl_clear_bit(trans, CSR_GP_CNTRL,
  302. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  303. /* Activates XTAL resources monitor */
  304. __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
  305. CSR_MONITOR_XTAL_RESOURCES);
  306. /* Release XTAL ON request */
  307. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  308. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  309. udelay(10);
  310. /* Release APMG XTAL */
  311. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  312. apmg_xtal_cfg_reg &
  313. ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  314. }
  315. static int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  316. {
  317. int ret = 0;
  318. /* stop device's busmaster DMA activity */
  319. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  320. ret = iwl_poll_bit(trans, CSR_RESET,
  321. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  322. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  323. if (ret)
  324. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  325. IWL_DEBUG_INFO(trans, "stop master\n");
  326. return ret;
  327. }
  328. static void iwl_pcie_apm_stop(struct iwl_trans *trans)
  329. {
  330. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  331. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  332. /* Stop device's DMA activity */
  333. iwl_pcie_apm_stop_master(trans);
  334. if (trans->cfg->lp_xtal_workaround) {
  335. iwl_pcie_apm_lp_xtal_enable(trans);
  336. return;
  337. }
  338. /* Reset the entire device */
  339. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  340. udelay(10);
  341. /*
  342. * Clear "initialization complete" bit to move adapter from
  343. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  344. */
  345. iwl_clear_bit(trans, CSR_GP_CNTRL,
  346. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  347. }
  348. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  349. {
  350. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  351. /* nic_init */
  352. spin_lock(&trans_pcie->irq_lock);
  353. iwl_pcie_apm_init(trans);
  354. spin_unlock(&trans_pcie->irq_lock);
  355. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  356. iwl_pcie_set_pwr(trans, false);
  357. iwl_op_mode_nic_config(trans->op_mode);
  358. /* Allocate the RX queue, or reset if it is already allocated */
  359. iwl_pcie_rx_init(trans);
  360. /* Allocate or reset and init all Tx and Command queues */
  361. if (iwl_pcie_tx_init(trans))
  362. return -ENOMEM;
  363. if (trans->cfg->base_params->shadow_reg_enable) {
  364. /* enable shadow regs in HW */
  365. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  366. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  367. }
  368. return 0;
  369. }
  370. #define HW_READY_TIMEOUT (50)
  371. /* Note: returns poll_bit return value, which is >= 0 if success */
  372. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  373. {
  374. int ret;
  375. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  376. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  377. /* See if we got it */
  378. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  379. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  380. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  381. HW_READY_TIMEOUT);
  382. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  383. return ret;
  384. }
  385. /* Note: returns standard 0/-ERROR code */
  386. static int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  387. {
  388. int ret;
  389. int t = 0;
  390. int iter;
  391. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  392. ret = iwl_pcie_set_hw_ready(trans);
  393. /* If the card is ready, exit 0 */
  394. if (ret >= 0)
  395. return 0;
  396. for (iter = 0; iter < 10; iter++) {
  397. /* If HW is not ready, prepare the conditions to check again */
  398. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  399. CSR_HW_IF_CONFIG_REG_PREPARE);
  400. do {
  401. ret = iwl_pcie_set_hw_ready(trans);
  402. if (ret >= 0)
  403. return 0;
  404. usleep_range(200, 1000);
  405. t += 200;
  406. } while (t < 150000);
  407. msleep(25);
  408. }
  409. IWL_DEBUG_INFO(trans, "got NIC after %d iterations\n", iter);
  410. return ret;
  411. }
  412. /*
  413. * ucode
  414. */
  415. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, u32 dst_addr,
  416. dma_addr_t phy_addr, u32 byte_cnt)
  417. {
  418. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  419. int ret;
  420. trans_pcie->ucode_write_complete = false;
  421. iwl_write_direct32(trans,
  422. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  423. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  424. iwl_write_direct32(trans,
  425. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  426. dst_addr);
  427. iwl_write_direct32(trans,
  428. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  429. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  430. iwl_write_direct32(trans,
  431. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  432. (iwl_get_dma_hi_addr(phy_addr)
  433. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  434. iwl_write_direct32(trans,
  435. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  436. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  437. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  438. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  439. iwl_write_direct32(trans,
  440. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  441. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  442. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  443. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  444. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  445. trans_pcie->ucode_write_complete, 5 * HZ);
  446. if (!ret) {
  447. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  448. return -ETIMEDOUT;
  449. }
  450. return 0;
  451. }
  452. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  453. const struct fw_desc *section)
  454. {
  455. u8 *v_addr;
  456. dma_addr_t p_addr;
  457. u32 offset, chunk_sz = section->len;
  458. int ret = 0;
  459. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  460. section_num);
  461. v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
  462. GFP_KERNEL | __GFP_NOWARN);
  463. if (!v_addr) {
  464. IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
  465. chunk_sz = PAGE_SIZE;
  466. v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
  467. &p_addr, GFP_KERNEL);
  468. if (!v_addr)
  469. return -ENOMEM;
  470. }
  471. for (offset = 0; offset < section->len; offset += chunk_sz) {
  472. u32 copy_size;
  473. copy_size = min_t(u32, chunk_sz, section->len - offset);
  474. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  475. ret = iwl_pcie_load_firmware_chunk(trans,
  476. section->offset + offset,
  477. p_addr, copy_size);
  478. if (ret) {
  479. IWL_ERR(trans,
  480. "Could not load the [%d] uCode section\n",
  481. section_num);
  482. break;
  483. }
  484. }
  485. dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
  486. return ret;
  487. }
  488. static int iwl_pcie_load_cpu_secured_sections(struct iwl_trans *trans,
  489. const struct fw_img *image,
  490. int cpu,
  491. int *first_ucode_section)
  492. {
  493. int shift_param;
  494. int i, ret = 0;
  495. u32 last_read_idx = 0;
  496. if (cpu == 1) {
  497. shift_param = 0;
  498. *first_ucode_section = 0;
  499. } else {
  500. shift_param = 16;
  501. (*first_ucode_section)++;
  502. }
  503. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  504. last_read_idx = i;
  505. if (!image->sec[i].data ||
  506. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  507. IWL_DEBUG_FW(trans,
  508. "Break since Data not valid or Empty section, sec = %d\n",
  509. i);
  510. break;
  511. }
  512. if (i == (*first_ucode_section) + 1)
  513. /* set CPU to started */
  514. iwl_set_bits_prph(trans,
  515. CSR_UCODE_LOAD_STATUS_ADDR,
  516. LMPM_CPU_HDRS_LOADING_COMPLETED
  517. << shift_param);
  518. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  519. if (ret)
  520. return ret;
  521. }
  522. /* image loading complete */
  523. iwl_set_bits_prph(trans,
  524. CSR_UCODE_LOAD_STATUS_ADDR,
  525. LMPM_CPU_UCODE_LOADING_COMPLETED << shift_param);
  526. *first_ucode_section = last_read_idx;
  527. return 0;
  528. }
  529. static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
  530. const struct fw_img *image,
  531. int cpu,
  532. int *first_ucode_section)
  533. {
  534. int shift_param;
  535. int i, ret = 0;
  536. u32 last_read_idx = 0;
  537. if (cpu == 1) {
  538. shift_param = 0;
  539. *first_ucode_section = 0;
  540. } else {
  541. shift_param = 16;
  542. (*first_ucode_section)++;
  543. }
  544. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  545. last_read_idx = i;
  546. if (!image->sec[i].data ||
  547. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  548. IWL_DEBUG_FW(trans,
  549. "Break since Data not valid or Empty section, sec = %d\n",
  550. i);
  551. break;
  552. }
  553. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  554. if (ret)
  555. return ret;
  556. }
  557. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  558. iwl_set_bits_prph(trans,
  559. CSR_UCODE_LOAD_STATUS_ADDR,
  560. (LMPM_CPU_UCODE_LOADING_COMPLETED |
  561. LMPM_CPU_HDRS_LOADING_COMPLETED |
  562. LMPM_CPU_UCODE_LOADING_STARTED) <<
  563. shift_param);
  564. *first_ucode_section = last_read_idx;
  565. return 0;
  566. }
  567. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  568. const struct fw_img *image)
  569. {
  570. int ret = 0;
  571. int first_ucode_section;
  572. IWL_DEBUG_FW(trans,
  573. "working with %s image\n",
  574. image->is_secure ? "Secured" : "Non Secured");
  575. IWL_DEBUG_FW(trans,
  576. "working with %s CPU\n",
  577. image->is_dual_cpus ? "Dual" : "Single");
  578. /* configure the ucode to be ready to get the secured image */
  579. if (image->is_secure) {
  580. /* set secure boot inspector addresses */
  581. iwl_write_prph(trans,
  582. LMPM_SECURE_INSPECTOR_CODE_ADDR,
  583. LMPM_SECURE_INSPECTOR_CODE_MEM_SPACE);
  584. iwl_write_prph(trans,
  585. LMPM_SECURE_INSPECTOR_DATA_ADDR,
  586. LMPM_SECURE_INSPECTOR_DATA_MEM_SPACE);
  587. /* set CPU1 header address */
  588. iwl_write_prph(trans,
  589. LMPM_SECURE_UCODE_LOAD_CPU1_HDR_ADDR,
  590. LMPM_SECURE_CPU1_HDR_MEM_SPACE);
  591. /* load to FW the binary Secured sections of CPU1 */
  592. ret = iwl_pcie_load_cpu_secured_sections(trans, image, 1,
  593. &first_ucode_section);
  594. if (ret)
  595. return ret;
  596. } else {
  597. /* load to FW the binary Non secured sections of CPU1 */
  598. ret = iwl_pcie_load_cpu_sections(trans, image, 1,
  599. &first_ucode_section);
  600. if (ret)
  601. return ret;
  602. }
  603. if (image->is_dual_cpus) {
  604. /* set CPU2 header address */
  605. iwl_write_prph(trans,
  606. LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
  607. LMPM_SECURE_CPU2_HDR_MEM_SPACE);
  608. /* load to FW the binary sections of CPU2 */
  609. if (image->is_secure)
  610. ret = iwl_pcie_load_cpu_secured_sections(
  611. trans, image, 2,
  612. &first_ucode_section);
  613. else
  614. ret = iwl_pcie_load_cpu_sections(trans, image, 2,
  615. &first_ucode_section);
  616. if (ret)
  617. return ret;
  618. }
  619. /* release CPU reset */
  620. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  621. iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  622. else
  623. iwl_write32(trans, CSR_RESET, 0);
  624. if (image->is_secure) {
  625. /* wait for image verification to complete */
  626. ret = iwl_poll_prph_bit(trans,
  627. LMPM_SECURE_BOOT_CPU1_STATUS_ADDR,
  628. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  629. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  630. LMPM_SECURE_TIME_OUT);
  631. if (ret < 0) {
  632. IWL_ERR(trans, "Time out on secure boot process\n");
  633. return ret;
  634. }
  635. }
  636. return 0;
  637. }
  638. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  639. const struct fw_img *fw, bool run_in_rfkill)
  640. {
  641. int ret;
  642. bool hw_rfkill;
  643. /* This may fail if AMT took ownership of the device */
  644. if (iwl_pcie_prepare_card_hw(trans)) {
  645. IWL_WARN(trans, "Exit HW not ready\n");
  646. return -EIO;
  647. }
  648. iwl_enable_rfkill_int(trans);
  649. /* If platform's RF_KILL switch is NOT set to KILL */
  650. hw_rfkill = iwl_is_rfkill_set(trans);
  651. if (hw_rfkill)
  652. set_bit(STATUS_RFKILL, &trans->status);
  653. else
  654. clear_bit(STATUS_RFKILL, &trans->status);
  655. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  656. if (hw_rfkill && !run_in_rfkill)
  657. return -ERFKILL;
  658. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  659. ret = iwl_pcie_nic_init(trans);
  660. if (ret) {
  661. IWL_ERR(trans, "Unable to init nic\n");
  662. return ret;
  663. }
  664. /* make sure rfkill handshake bits are cleared */
  665. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  666. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  667. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  668. /* clear (again), then enable host interrupts */
  669. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  670. iwl_enable_interrupts(trans);
  671. /* really make sure rfkill handshake bits are cleared */
  672. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  673. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  674. /* Load the given image to the HW */
  675. return iwl_pcie_load_given_ucode(trans, fw);
  676. }
  677. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  678. {
  679. iwl_pcie_reset_ict(trans);
  680. iwl_pcie_tx_start(trans, scd_addr);
  681. }
  682. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  683. {
  684. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  685. bool hw_rfkill, was_hw_rfkill;
  686. was_hw_rfkill = iwl_is_rfkill_set(trans);
  687. /* tell the device to stop sending interrupts */
  688. spin_lock(&trans_pcie->irq_lock);
  689. iwl_disable_interrupts(trans);
  690. spin_unlock(&trans_pcie->irq_lock);
  691. /* device going down, Stop using ICT table */
  692. iwl_pcie_disable_ict(trans);
  693. /*
  694. * If a HW restart happens during firmware loading,
  695. * then the firmware loading might call this function
  696. * and later it might be called again due to the
  697. * restart. So don't process again if the device is
  698. * already dead.
  699. */
  700. if (test_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  701. iwl_pcie_tx_stop(trans);
  702. iwl_pcie_rx_stop(trans);
  703. /* Power-down device's busmaster DMA clocks */
  704. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  705. APMG_CLK_VAL_DMA_CLK_RQT);
  706. udelay(5);
  707. }
  708. /* Make sure (redundant) we've released our request to stay awake */
  709. iwl_clear_bit(trans, CSR_GP_CNTRL,
  710. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  711. /* Stop the device, and put it in low power state */
  712. iwl_pcie_apm_stop(trans);
  713. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  714. * Clean again the interrupt here
  715. */
  716. spin_lock(&trans_pcie->irq_lock);
  717. iwl_disable_interrupts(trans);
  718. spin_unlock(&trans_pcie->irq_lock);
  719. /* stop and reset the on-board processor */
  720. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  721. /* clear all status bits */
  722. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  723. clear_bit(STATUS_INT_ENABLED, &trans->status);
  724. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  725. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  726. clear_bit(STATUS_RFKILL, &trans->status);
  727. /*
  728. * Even if we stop the HW, we still want the RF kill
  729. * interrupt
  730. */
  731. iwl_enable_rfkill_int(trans);
  732. /*
  733. * Check again since the RF kill state may have changed while
  734. * all the interrupts were disabled, in this case we couldn't
  735. * receive the RF kill interrupt and update the state in the
  736. * op_mode.
  737. * Don't call the op_mode if the rkfill state hasn't changed.
  738. * This allows the op_mode to call stop_device from the rfkill
  739. * notification without endless recursion. Under very rare
  740. * circumstances, we might have a small recursion if the rfkill
  741. * state changed exactly now while we were called from stop_device.
  742. * This is very unlikely but can happen and is supported.
  743. */
  744. hw_rfkill = iwl_is_rfkill_set(trans);
  745. if (hw_rfkill)
  746. set_bit(STATUS_RFKILL, &trans->status);
  747. else
  748. clear_bit(STATUS_RFKILL, &trans->status);
  749. if (hw_rfkill != was_hw_rfkill)
  750. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  751. }
  752. void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
  753. {
  754. if (iwl_op_mode_hw_rf_kill(trans->op_mode, state))
  755. iwl_trans_pcie_stop_device(trans);
  756. }
  757. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test)
  758. {
  759. iwl_disable_interrupts(trans);
  760. /*
  761. * in testing mode, the host stays awake and the
  762. * hardware won't be reset (not even partially)
  763. */
  764. if (test)
  765. return;
  766. iwl_pcie_disable_ict(trans);
  767. iwl_clear_bit(trans, CSR_GP_CNTRL,
  768. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  769. iwl_clear_bit(trans, CSR_GP_CNTRL,
  770. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  771. /*
  772. * reset TX queues -- some of their registers reset during S3
  773. * so if we don't reset everything here the D3 image would try
  774. * to execute some invalid memory upon resume
  775. */
  776. iwl_trans_pcie_tx_reset(trans);
  777. iwl_pcie_set_pwr(trans, true);
  778. }
  779. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  780. enum iwl_d3_status *status,
  781. bool test)
  782. {
  783. u32 val;
  784. int ret;
  785. if (test) {
  786. iwl_enable_interrupts(trans);
  787. *status = IWL_D3_STATUS_ALIVE;
  788. return 0;
  789. }
  790. iwl_pcie_set_pwr(trans, false);
  791. val = iwl_read32(trans, CSR_RESET);
  792. if (val & CSR_RESET_REG_FLAG_NEVO_RESET) {
  793. *status = IWL_D3_STATUS_RESET;
  794. return 0;
  795. }
  796. /*
  797. * Also enables interrupts - none will happen as the device doesn't
  798. * know we're waking it up, only when the opmode actually tells it
  799. * after this call.
  800. */
  801. iwl_pcie_reset_ict(trans);
  802. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  803. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  804. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  805. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  806. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  807. 25000);
  808. if (ret) {
  809. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  810. return ret;
  811. }
  812. iwl_trans_pcie_tx_reset(trans);
  813. ret = iwl_pcie_rx_init(trans);
  814. if (ret) {
  815. IWL_ERR(trans, "Failed to resume the device (RX reset)\n");
  816. return ret;
  817. }
  818. *status = IWL_D3_STATUS_ALIVE;
  819. return 0;
  820. }
  821. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  822. {
  823. bool hw_rfkill;
  824. int err;
  825. err = iwl_pcie_prepare_card_hw(trans);
  826. if (err) {
  827. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  828. return err;
  829. }
  830. /* Reset the entire device */
  831. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  832. usleep_range(10, 15);
  833. iwl_pcie_apm_init(trans);
  834. /* From now on, the op_mode will be kept updated about RF kill state */
  835. iwl_enable_rfkill_int(trans);
  836. hw_rfkill = iwl_is_rfkill_set(trans);
  837. if (hw_rfkill)
  838. set_bit(STATUS_RFKILL, &trans->status);
  839. else
  840. clear_bit(STATUS_RFKILL, &trans->status);
  841. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  842. return 0;
  843. }
  844. static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
  845. {
  846. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  847. /* disable interrupts - don't enable HW RF kill interrupt */
  848. spin_lock(&trans_pcie->irq_lock);
  849. iwl_disable_interrupts(trans);
  850. spin_unlock(&trans_pcie->irq_lock);
  851. iwl_pcie_apm_stop(trans);
  852. spin_lock(&trans_pcie->irq_lock);
  853. iwl_disable_interrupts(trans);
  854. spin_unlock(&trans_pcie->irq_lock);
  855. iwl_pcie_disable_ict(trans);
  856. }
  857. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  858. {
  859. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  860. }
  861. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  862. {
  863. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  864. }
  865. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  866. {
  867. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  868. }
  869. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  870. {
  871. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
  872. ((reg & 0x000FFFFF) | (3 << 24)));
  873. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  874. }
  875. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  876. u32 val)
  877. {
  878. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  879. ((addr & 0x000FFFFF) | (3 << 24)));
  880. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  881. }
  882. static int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget)
  883. {
  884. WARN_ON(1);
  885. return 0;
  886. }
  887. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  888. const struct iwl_trans_config *trans_cfg)
  889. {
  890. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  891. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  892. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  893. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  894. trans_pcie->n_no_reclaim_cmds = 0;
  895. else
  896. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  897. if (trans_pcie->n_no_reclaim_cmds)
  898. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  899. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  900. trans_pcie->rx_buf_size_8k = trans_cfg->rx_buf_size_8k;
  901. if (trans_pcie->rx_buf_size_8k)
  902. trans_pcie->rx_page_order = get_order(8 * 1024);
  903. else
  904. trans_pcie->rx_page_order = get_order(4 * 1024);
  905. trans_pcie->wd_timeout =
  906. msecs_to_jiffies(trans_cfg->queue_watchdog_timeout);
  907. trans_pcie->command_names = trans_cfg->command_names;
  908. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  909. /* Initialize NAPI here - it should be before registering to mac80211
  910. * in the opmode but after the HW struct is allocated.
  911. * As this function may be called again in some corner cases don't
  912. * do anything if NAPI was already initialized.
  913. */
  914. if (!trans_pcie->napi.poll && trans->op_mode->ops->napi_add) {
  915. init_dummy_netdev(&trans_pcie->napi_dev);
  916. iwl_op_mode_napi_add(trans->op_mode, &trans_pcie->napi,
  917. &trans_pcie->napi_dev,
  918. iwl_pcie_dummy_napi_poll, 64);
  919. }
  920. }
  921. void iwl_trans_pcie_free(struct iwl_trans *trans)
  922. {
  923. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  924. synchronize_irq(trans_pcie->pci_dev->irq);
  925. iwl_pcie_tx_free(trans);
  926. iwl_pcie_rx_free(trans);
  927. free_irq(trans_pcie->pci_dev->irq, trans);
  928. iwl_pcie_free_ict(trans);
  929. pci_disable_msi(trans_pcie->pci_dev);
  930. iounmap(trans_pcie->hw_base);
  931. pci_release_regions(trans_pcie->pci_dev);
  932. pci_disable_device(trans_pcie->pci_dev);
  933. kmem_cache_destroy(trans->dev_cmd_pool);
  934. if (trans_pcie->napi.poll)
  935. netif_napi_del(&trans_pcie->napi);
  936. kfree(trans);
  937. }
  938. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  939. {
  940. if (state)
  941. set_bit(STATUS_TPOWER_PMI, &trans->status);
  942. else
  943. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  944. }
  945. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent,
  946. unsigned long *flags)
  947. {
  948. int ret;
  949. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  950. spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
  951. if (trans_pcie->cmd_in_flight)
  952. goto out;
  953. /* this bit wakes up the NIC */
  954. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  955. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  956. /*
  957. * These bits say the device is running, and should keep running for
  958. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  959. * but they do not indicate that embedded SRAM is restored yet;
  960. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  961. * to/from host DRAM when sleeping/waking for power-saving.
  962. * Each direction takes approximately 1/4 millisecond; with this
  963. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  964. * series of register accesses are expected (e.g. reading Event Log),
  965. * to keep device from sleeping.
  966. *
  967. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  968. * SRAM is okay/restored. We don't check that here because this call
  969. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  970. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  971. *
  972. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  973. * and do not save/restore SRAM when power cycling.
  974. */
  975. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  976. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  977. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  978. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  979. if (unlikely(ret < 0)) {
  980. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  981. if (!silent) {
  982. u32 val = iwl_read32(trans, CSR_GP_CNTRL);
  983. WARN_ONCE(1,
  984. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  985. val);
  986. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  987. return false;
  988. }
  989. }
  990. out:
  991. /*
  992. * Fool sparse by faking we release the lock - sparse will
  993. * track nic_access anyway.
  994. */
  995. __release(&trans_pcie->reg_lock);
  996. return true;
  997. }
  998. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
  999. unsigned long *flags)
  1000. {
  1001. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1002. lockdep_assert_held(&trans_pcie->reg_lock);
  1003. /*
  1004. * Fool sparse by faking we acquiring the lock - sparse will
  1005. * track nic_access anyway.
  1006. */
  1007. __acquire(&trans_pcie->reg_lock);
  1008. if (trans_pcie->cmd_in_flight)
  1009. goto out;
  1010. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  1011. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1012. /*
  1013. * Above we read the CSR_GP_CNTRL register, which will flush
  1014. * any previous writes, but we need the write that clears the
  1015. * MAC_ACCESS_REQ bit to be performed before any other writes
  1016. * scheduled on different CPUs (after we drop reg_lock).
  1017. */
  1018. mmiowb();
  1019. out:
  1020. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1021. }
  1022. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  1023. void *buf, int dwords)
  1024. {
  1025. unsigned long flags;
  1026. int offs, ret = 0;
  1027. u32 *vals = buf;
  1028. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1029. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  1030. for (offs = 0; offs < dwords; offs++)
  1031. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  1032. iwl_trans_release_nic_access(trans, &flags);
  1033. } else {
  1034. ret = -EBUSY;
  1035. }
  1036. return ret;
  1037. }
  1038. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  1039. const void *buf, int dwords)
  1040. {
  1041. unsigned long flags;
  1042. int offs, ret = 0;
  1043. const u32 *vals = buf;
  1044. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1045. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  1046. for (offs = 0; offs < dwords; offs++)
  1047. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  1048. vals ? vals[offs] : 0);
  1049. iwl_trans_release_nic_access(trans, &flags);
  1050. } else {
  1051. ret = -EBUSY;
  1052. }
  1053. return ret;
  1054. }
  1055. #define IWL_FLUSH_WAIT_MS 2000
  1056. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, u32 txq_bm)
  1057. {
  1058. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1059. struct iwl_txq *txq;
  1060. struct iwl_queue *q;
  1061. int cnt;
  1062. unsigned long now = jiffies;
  1063. u32 scd_sram_addr;
  1064. u8 buf[16];
  1065. int ret = 0;
  1066. /* waiting for all the tx frames complete might take a while */
  1067. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1068. u8 wr_ptr;
  1069. if (cnt == trans_pcie->cmd_queue)
  1070. continue;
  1071. if (!test_bit(cnt, trans_pcie->queue_used))
  1072. continue;
  1073. if (!(BIT(cnt) & txq_bm))
  1074. continue;
  1075. IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", cnt);
  1076. txq = &trans_pcie->txq[cnt];
  1077. q = &txq->q;
  1078. wr_ptr = ACCESS_ONCE(q->write_ptr);
  1079. while (q->read_ptr != ACCESS_ONCE(q->write_ptr) &&
  1080. !time_after(jiffies,
  1081. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
  1082. u8 write_ptr = ACCESS_ONCE(q->write_ptr);
  1083. if (WARN_ONCE(wr_ptr != write_ptr,
  1084. "WR pointer moved while flushing %d -> %d\n",
  1085. wr_ptr, write_ptr))
  1086. return -ETIMEDOUT;
  1087. msleep(1);
  1088. }
  1089. if (q->read_ptr != q->write_ptr) {
  1090. IWL_ERR(trans,
  1091. "fail to flush all tx fifo queues Q %d\n", cnt);
  1092. ret = -ETIMEDOUT;
  1093. break;
  1094. }
  1095. IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", cnt);
  1096. }
  1097. if (!ret)
  1098. return 0;
  1099. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1100. txq->q.read_ptr, txq->q.write_ptr);
  1101. scd_sram_addr = trans_pcie->scd_base_addr +
  1102. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  1103. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  1104. iwl_print_hex_error(trans, buf, sizeof(buf));
  1105. for (cnt = 0; cnt < FH_TCSR_CHNL_NUM; cnt++)
  1106. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", cnt,
  1107. iwl_read_direct32(trans, FH_TX_TRB_REG(cnt)));
  1108. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1109. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(cnt));
  1110. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  1111. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  1112. u32 tbl_dw =
  1113. iwl_trans_read_mem32(trans, trans_pcie->scd_base_addr +
  1114. SCD_TRANS_TBL_OFFSET_QUEUE(cnt));
  1115. if (cnt & 0x1)
  1116. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  1117. else
  1118. tbl_dw = tbl_dw & 0x0000FFFF;
  1119. IWL_ERR(trans,
  1120. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  1121. cnt, active ? "" : "in", fifo, tbl_dw,
  1122. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt)) &
  1123. (TFD_QUEUE_SIZE_MAX - 1),
  1124. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1125. }
  1126. return ret;
  1127. }
  1128. static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
  1129. u32 mask, u32 value)
  1130. {
  1131. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1132. unsigned long flags;
  1133. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1134. __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
  1135. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1136. }
  1137. static const char *get_csr_string(int cmd)
  1138. {
  1139. #define IWL_CMD(x) case x: return #x
  1140. switch (cmd) {
  1141. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1142. IWL_CMD(CSR_INT_COALESCING);
  1143. IWL_CMD(CSR_INT);
  1144. IWL_CMD(CSR_INT_MASK);
  1145. IWL_CMD(CSR_FH_INT_STATUS);
  1146. IWL_CMD(CSR_GPIO_IN);
  1147. IWL_CMD(CSR_RESET);
  1148. IWL_CMD(CSR_GP_CNTRL);
  1149. IWL_CMD(CSR_HW_REV);
  1150. IWL_CMD(CSR_EEPROM_REG);
  1151. IWL_CMD(CSR_EEPROM_GP);
  1152. IWL_CMD(CSR_OTP_GP_REG);
  1153. IWL_CMD(CSR_GIO_REG);
  1154. IWL_CMD(CSR_GP_UCODE_REG);
  1155. IWL_CMD(CSR_GP_DRIVER_REG);
  1156. IWL_CMD(CSR_UCODE_DRV_GP1);
  1157. IWL_CMD(CSR_UCODE_DRV_GP2);
  1158. IWL_CMD(CSR_LED_REG);
  1159. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1160. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1161. IWL_CMD(CSR_ANA_PLL_CFG);
  1162. IWL_CMD(CSR_HW_REV_WA_REG);
  1163. IWL_CMD(CSR_MONITOR_STATUS_REG);
  1164. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1165. default:
  1166. return "UNKNOWN";
  1167. }
  1168. #undef IWL_CMD
  1169. }
  1170. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  1171. {
  1172. int i;
  1173. static const u32 csr_tbl[] = {
  1174. CSR_HW_IF_CONFIG_REG,
  1175. CSR_INT_COALESCING,
  1176. CSR_INT,
  1177. CSR_INT_MASK,
  1178. CSR_FH_INT_STATUS,
  1179. CSR_GPIO_IN,
  1180. CSR_RESET,
  1181. CSR_GP_CNTRL,
  1182. CSR_HW_REV,
  1183. CSR_EEPROM_REG,
  1184. CSR_EEPROM_GP,
  1185. CSR_OTP_GP_REG,
  1186. CSR_GIO_REG,
  1187. CSR_GP_UCODE_REG,
  1188. CSR_GP_DRIVER_REG,
  1189. CSR_UCODE_DRV_GP1,
  1190. CSR_UCODE_DRV_GP2,
  1191. CSR_LED_REG,
  1192. CSR_DRAM_INT_TBL_REG,
  1193. CSR_GIO_CHICKEN_BITS,
  1194. CSR_ANA_PLL_CFG,
  1195. CSR_MONITOR_STATUS_REG,
  1196. CSR_HW_REV_WA_REG,
  1197. CSR_DBG_HPET_MEM_REG
  1198. };
  1199. IWL_ERR(trans, "CSR values:\n");
  1200. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1201. "CSR_INT_PERIODIC_REG)\n");
  1202. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1203. IWL_ERR(trans, " %25s: 0X%08x\n",
  1204. get_csr_string(csr_tbl[i]),
  1205. iwl_read32(trans, csr_tbl[i]));
  1206. }
  1207. }
  1208. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1209. /* create and remove of files */
  1210. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1211. if (!debugfs_create_file(#name, mode, parent, trans, \
  1212. &iwl_dbgfs_##name##_ops)) \
  1213. goto err; \
  1214. } while (0)
  1215. /* file operation */
  1216. #define DEBUGFS_READ_FILE_OPS(name) \
  1217. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1218. .read = iwl_dbgfs_##name##_read, \
  1219. .open = simple_open, \
  1220. .llseek = generic_file_llseek, \
  1221. };
  1222. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1223. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1224. .write = iwl_dbgfs_##name##_write, \
  1225. .open = simple_open, \
  1226. .llseek = generic_file_llseek, \
  1227. };
  1228. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1229. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1230. .write = iwl_dbgfs_##name##_write, \
  1231. .read = iwl_dbgfs_##name##_read, \
  1232. .open = simple_open, \
  1233. .llseek = generic_file_llseek, \
  1234. };
  1235. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1236. char __user *user_buf,
  1237. size_t count, loff_t *ppos)
  1238. {
  1239. struct iwl_trans *trans = file->private_data;
  1240. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1241. struct iwl_txq *txq;
  1242. struct iwl_queue *q;
  1243. char *buf;
  1244. int pos = 0;
  1245. int cnt;
  1246. int ret;
  1247. size_t bufsz;
  1248. bufsz = sizeof(char) * 64 * trans->cfg->base_params->num_of_queues;
  1249. if (!trans_pcie->txq)
  1250. return -EAGAIN;
  1251. buf = kzalloc(bufsz, GFP_KERNEL);
  1252. if (!buf)
  1253. return -ENOMEM;
  1254. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1255. txq = &trans_pcie->txq[cnt];
  1256. q = &txq->q;
  1257. pos += scnprintf(buf + pos, bufsz - pos,
  1258. "hwq %.2d: read=%u write=%u use=%d stop=%d\n",
  1259. cnt, q->read_ptr, q->write_ptr,
  1260. !!test_bit(cnt, trans_pcie->queue_used),
  1261. !!test_bit(cnt, trans_pcie->queue_stopped));
  1262. }
  1263. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1264. kfree(buf);
  1265. return ret;
  1266. }
  1267. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1268. char __user *user_buf,
  1269. size_t count, loff_t *ppos)
  1270. {
  1271. struct iwl_trans *trans = file->private_data;
  1272. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1273. struct iwl_rxq *rxq = &trans_pcie->rxq;
  1274. char buf[256];
  1275. int pos = 0;
  1276. const size_t bufsz = sizeof(buf);
  1277. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1278. rxq->read);
  1279. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1280. rxq->write);
  1281. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1282. rxq->free_count);
  1283. if (rxq->rb_stts) {
  1284. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1285. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1286. } else {
  1287. pos += scnprintf(buf + pos, bufsz - pos,
  1288. "closed_rb_num: Not Allocated\n");
  1289. }
  1290. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1291. }
  1292. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1293. char __user *user_buf,
  1294. size_t count, loff_t *ppos)
  1295. {
  1296. struct iwl_trans *trans = file->private_data;
  1297. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1298. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1299. int pos = 0;
  1300. char *buf;
  1301. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1302. ssize_t ret;
  1303. buf = kzalloc(bufsz, GFP_KERNEL);
  1304. if (!buf)
  1305. return -ENOMEM;
  1306. pos += scnprintf(buf + pos, bufsz - pos,
  1307. "Interrupt Statistics Report:\n");
  1308. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1309. isr_stats->hw);
  1310. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1311. isr_stats->sw);
  1312. if (isr_stats->sw || isr_stats->hw) {
  1313. pos += scnprintf(buf + pos, bufsz - pos,
  1314. "\tLast Restarting Code: 0x%X\n",
  1315. isr_stats->err_code);
  1316. }
  1317. #ifdef CONFIG_IWLWIFI_DEBUG
  1318. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1319. isr_stats->sch);
  1320. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1321. isr_stats->alive);
  1322. #endif
  1323. pos += scnprintf(buf + pos, bufsz - pos,
  1324. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1325. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1326. isr_stats->ctkill);
  1327. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1328. isr_stats->wakeup);
  1329. pos += scnprintf(buf + pos, bufsz - pos,
  1330. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1331. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1332. isr_stats->tx);
  1333. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1334. isr_stats->unhandled);
  1335. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1336. kfree(buf);
  1337. return ret;
  1338. }
  1339. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1340. const char __user *user_buf,
  1341. size_t count, loff_t *ppos)
  1342. {
  1343. struct iwl_trans *trans = file->private_data;
  1344. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1345. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1346. char buf[8];
  1347. int buf_size;
  1348. u32 reset_flag;
  1349. memset(buf, 0, sizeof(buf));
  1350. buf_size = min(count, sizeof(buf) - 1);
  1351. if (copy_from_user(buf, user_buf, buf_size))
  1352. return -EFAULT;
  1353. if (sscanf(buf, "%x", &reset_flag) != 1)
  1354. return -EFAULT;
  1355. if (reset_flag == 0)
  1356. memset(isr_stats, 0, sizeof(*isr_stats));
  1357. return count;
  1358. }
  1359. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1360. const char __user *user_buf,
  1361. size_t count, loff_t *ppos)
  1362. {
  1363. struct iwl_trans *trans = file->private_data;
  1364. char buf[8];
  1365. int buf_size;
  1366. int csr;
  1367. memset(buf, 0, sizeof(buf));
  1368. buf_size = min(count, sizeof(buf) - 1);
  1369. if (copy_from_user(buf, user_buf, buf_size))
  1370. return -EFAULT;
  1371. if (sscanf(buf, "%d", &csr) != 1)
  1372. return -EFAULT;
  1373. iwl_pcie_dump_csr(trans);
  1374. return count;
  1375. }
  1376. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1377. char __user *user_buf,
  1378. size_t count, loff_t *ppos)
  1379. {
  1380. struct iwl_trans *trans = file->private_data;
  1381. char *buf = NULL;
  1382. ssize_t ret;
  1383. ret = iwl_dump_fh(trans, &buf);
  1384. if (ret < 0)
  1385. return ret;
  1386. if (!buf)
  1387. return -EINVAL;
  1388. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1389. kfree(buf);
  1390. return ret;
  1391. }
  1392. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1393. DEBUGFS_READ_FILE_OPS(fh_reg);
  1394. DEBUGFS_READ_FILE_OPS(rx_queue);
  1395. DEBUGFS_READ_FILE_OPS(tx_queue);
  1396. DEBUGFS_WRITE_FILE_OPS(csr);
  1397. /*
  1398. * Create the debugfs files and directories
  1399. *
  1400. */
  1401. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1402. struct dentry *dir)
  1403. {
  1404. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1405. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1406. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1407. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1408. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1409. return 0;
  1410. err:
  1411. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  1412. return -ENOMEM;
  1413. }
  1414. static u32 iwl_trans_pcie_get_cmdlen(struct iwl_tfd *tfd)
  1415. {
  1416. u32 cmdlen = 0;
  1417. int i;
  1418. for (i = 0; i < IWL_NUM_OF_TBS; i++)
  1419. cmdlen += iwl_pcie_tfd_tb_get_len(tfd, i);
  1420. return cmdlen;
  1421. }
  1422. static u32 iwl_trans_pcie_dump_data(struct iwl_trans *trans,
  1423. void *buf, u32 buflen)
  1424. {
  1425. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1426. struct iwl_fw_error_dump_data *data;
  1427. struct iwl_txq *cmdq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1428. struct iwl_fw_error_dump_txcmd *txcmd;
  1429. u32 len;
  1430. int i, ptr;
  1431. if (!buf)
  1432. return sizeof(*data) +
  1433. cmdq->q.n_window * (sizeof(*txcmd) +
  1434. TFD_MAX_PAYLOAD_SIZE);
  1435. len = 0;
  1436. data = buf;
  1437. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
  1438. txcmd = (void *)data->data;
  1439. spin_lock_bh(&cmdq->lock);
  1440. ptr = cmdq->q.write_ptr;
  1441. for (i = 0; i < cmdq->q.n_window; i++) {
  1442. u8 idx = get_cmd_index(&cmdq->q, ptr);
  1443. u32 caplen, cmdlen;
  1444. cmdlen = iwl_trans_pcie_get_cmdlen(&cmdq->tfds[ptr]);
  1445. caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
  1446. if (cmdlen) {
  1447. len += sizeof(*txcmd) + caplen;
  1448. txcmd->cmdlen = cpu_to_le32(cmdlen);
  1449. txcmd->caplen = cpu_to_le32(caplen);
  1450. memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
  1451. txcmd = (void *)((u8 *)txcmd->data + caplen);
  1452. }
  1453. ptr = iwl_queue_dec_wrap(ptr);
  1454. }
  1455. spin_unlock_bh(&cmdq->lock);
  1456. data->len = cpu_to_le32(len);
  1457. return sizeof(*data) + len;
  1458. }
  1459. #else
  1460. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1461. struct dentry *dir)
  1462. {
  1463. return 0;
  1464. }
  1465. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1466. static const struct iwl_trans_ops trans_ops_pcie = {
  1467. .start_hw = iwl_trans_pcie_start_hw,
  1468. .op_mode_leave = iwl_trans_pcie_op_mode_leave,
  1469. .fw_alive = iwl_trans_pcie_fw_alive,
  1470. .start_fw = iwl_trans_pcie_start_fw,
  1471. .stop_device = iwl_trans_pcie_stop_device,
  1472. .d3_suspend = iwl_trans_pcie_d3_suspend,
  1473. .d3_resume = iwl_trans_pcie_d3_resume,
  1474. .send_cmd = iwl_trans_pcie_send_hcmd,
  1475. .tx = iwl_trans_pcie_tx,
  1476. .reclaim = iwl_trans_pcie_reclaim,
  1477. .txq_disable = iwl_trans_pcie_txq_disable,
  1478. .txq_enable = iwl_trans_pcie_txq_enable,
  1479. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1480. .wait_tx_queue_empty = iwl_trans_pcie_wait_txq_empty,
  1481. .write8 = iwl_trans_pcie_write8,
  1482. .write32 = iwl_trans_pcie_write32,
  1483. .read32 = iwl_trans_pcie_read32,
  1484. .read_prph = iwl_trans_pcie_read_prph,
  1485. .write_prph = iwl_trans_pcie_write_prph,
  1486. .read_mem = iwl_trans_pcie_read_mem,
  1487. .write_mem = iwl_trans_pcie_write_mem,
  1488. .configure = iwl_trans_pcie_configure,
  1489. .set_pmi = iwl_trans_pcie_set_pmi,
  1490. .grab_nic_access = iwl_trans_pcie_grab_nic_access,
  1491. .release_nic_access = iwl_trans_pcie_release_nic_access,
  1492. .set_bits_mask = iwl_trans_pcie_set_bits_mask,
  1493. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1494. .dump_data = iwl_trans_pcie_dump_data,
  1495. #endif
  1496. };
  1497. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  1498. const struct pci_device_id *ent,
  1499. const struct iwl_cfg *cfg)
  1500. {
  1501. struct iwl_trans_pcie *trans_pcie;
  1502. struct iwl_trans *trans;
  1503. u16 pci_cmd;
  1504. int err;
  1505. trans = kzalloc(sizeof(struct iwl_trans) +
  1506. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1507. if (!trans) {
  1508. err = -ENOMEM;
  1509. goto out;
  1510. }
  1511. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1512. trans->ops = &trans_ops_pcie;
  1513. trans->cfg = cfg;
  1514. trans_lockdep_init(trans);
  1515. trans_pcie->trans = trans;
  1516. spin_lock_init(&trans_pcie->irq_lock);
  1517. spin_lock_init(&trans_pcie->reg_lock);
  1518. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  1519. err = pci_enable_device(pdev);
  1520. if (err)
  1521. goto out_no_pci;
  1522. if (!cfg->base_params->pcie_l1_allowed) {
  1523. /*
  1524. * W/A - seems to solve weird behavior. We need to remove this
  1525. * if we don't want to stay in L1 all the time. This wastes a
  1526. * lot of power.
  1527. */
  1528. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  1529. PCIE_LINK_STATE_L1 |
  1530. PCIE_LINK_STATE_CLKPM);
  1531. }
  1532. pci_set_master(pdev);
  1533. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1534. if (!err)
  1535. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1536. if (err) {
  1537. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1538. if (!err)
  1539. err = pci_set_consistent_dma_mask(pdev,
  1540. DMA_BIT_MASK(32));
  1541. /* both attempts failed: */
  1542. if (err) {
  1543. dev_err(&pdev->dev, "No suitable DMA available\n");
  1544. goto out_pci_disable_device;
  1545. }
  1546. }
  1547. err = pci_request_regions(pdev, DRV_NAME);
  1548. if (err) {
  1549. dev_err(&pdev->dev, "pci_request_regions failed\n");
  1550. goto out_pci_disable_device;
  1551. }
  1552. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  1553. if (!trans_pcie->hw_base) {
  1554. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  1555. err = -ENODEV;
  1556. goto out_pci_release_regions;
  1557. }
  1558. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1559. * PCI Tx retries from interfering with C3 CPU state */
  1560. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1561. trans->dev = &pdev->dev;
  1562. trans_pcie->pci_dev = pdev;
  1563. iwl_disable_interrupts(trans);
  1564. err = pci_enable_msi(pdev);
  1565. if (err) {
  1566. dev_err(&pdev->dev, "pci_enable_msi failed(0X%x)\n", err);
  1567. /* enable rfkill interrupt: hw bug w/a */
  1568. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1569. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1570. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1571. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1572. }
  1573. }
  1574. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  1575. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  1576. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1577. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1578. /* Initialize the wait queue for commands */
  1579. init_waitqueue_head(&trans_pcie->wait_command_queue);
  1580. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  1581. "iwl_cmd_pool:%s", dev_name(trans->dev));
  1582. trans->dev_cmd_headroom = 0;
  1583. trans->dev_cmd_pool =
  1584. kmem_cache_create(trans->dev_cmd_pool_name,
  1585. sizeof(struct iwl_device_cmd)
  1586. + trans->dev_cmd_headroom,
  1587. sizeof(void *),
  1588. SLAB_HWCACHE_ALIGN,
  1589. NULL);
  1590. if (!trans->dev_cmd_pool) {
  1591. err = -ENOMEM;
  1592. goto out_pci_disable_msi;
  1593. }
  1594. if (iwl_pcie_alloc_ict(trans))
  1595. goto out_free_cmd_pool;
  1596. err = request_threaded_irq(pdev->irq, iwl_pcie_isr,
  1597. iwl_pcie_irq_handler,
  1598. IRQF_SHARED, DRV_NAME, trans);
  1599. if (err) {
  1600. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  1601. goto out_free_ict;
  1602. }
  1603. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1604. return trans;
  1605. out_free_ict:
  1606. iwl_pcie_free_ict(trans);
  1607. out_free_cmd_pool:
  1608. kmem_cache_destroy(trans->dev_cmd_pool);
  1609. out_pci_disable_msi:
  1610. pci_disable_msi(pdev);
  1611. out_pci_release_regions:
  1612. pci_release_regions(pdev);
  1613. out_pci_disable_device:
  1614. pci_disable_device(pdev);
  1615. out_no_pci:
  1616. kfree(trans);
  1617. out:
  1618. return ERR_PTR(err);
  1619. }