trans.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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 - 2015 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2015 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/pci.h>
  66. #include <linux/pci-aspm.h>
  67. #include <linux/interrupt.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/sched.h>
  70. #include <linux/bitops.h>
  71. #include <linux/gfp.h>
  72. #include <linux/vmalloc.h>
  73. #include "iwl-drv.h"
  74. #include "iwl-trans.h"
  75. #include "iwl-csr.h"
  76. #include "iwl-prph.h"
  77. #include "iwl-agn-hw.h"
  78. #include "iwl-fw-error-dump.h"
  79. #include "internal.h"
  80. static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
  81. {
  82. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  83. if (!trans_pcie->fw_mon_page)
  84. return;
  85. dma_unmap_page(trans->dev, trans_pcie->fw_mon_phys,
  86. trans_pcie->fw_mon_size, DMA_FROM_DEVICE);
  87. __free_pages(trans_pcie->fw_mon_page,
  88. get_order(trans_pcie->fw_mon_size));
  89. trans_pcie->fw_mon_page = NULL;
  90. trans_pcie->fw_mon_phys = 0;
  91. trans_pcie->fw_mon_size = 0;
  92. }
  93. static void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans)
  94. {
  95. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  96. struct page *page = NULL;
  97. dma_addr_t phys;
  98. u32 size;
  99. u8 power;
  100. if (trans_pcie->fw_mon_page) {
  101. dma_sync_single_for_device(trans->dev, trans_pcie->fw_mon_phys,
  102. trans_pcie->fw_mon_size,
  103. DMA_FROM_DEVICE);
  104. return;
  105. }
  106. phys = 0;
  107. for (power = 26; power >= 11; power--) {
  108. int order;
  109. size = BIT(power);
  110. order = get_order(size);
  111. page = alloc_pages(__GFP_COMP | __GFP_NOWARN | __GFP_ZERO,
  112. order);
  113. if (!page)
  114. continue;
  115. phys = dma_map_page(trans->dev, page, 0, PAGE_SIZE << order,
  116. DMA_FROM_DEVICE);
  117. if (dma_mapping_error(trans->dev, phys)) {
  118. __free_pages(page, order);
  119. page = NULL;
  120. continue;
  121. }
  122. IWL_INFO(trans,
  123. "Allocated 0x%08x bytes (order %d) for firmware monitor.\n",
  124. size, order);
  125. break;
  126. }
  127. if (!page)
  128. return;
  129. trans_pcie->fw_mon_page = page;
  130. trans_pcie->fw_mon_phys = phys;
  131. trans_pcie->fw_mon_size = size;
  132. }
  133. static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
  134. {
  135. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  136. ((reg & 0x0000ffff) | (2 << 28)));
  137. return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
  138. }
  139. static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
  140. {
  141. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
  142. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  143. ((reg & 0x0000ffff) | (3 << 28)));
  144. }
  145. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  146. {
  147. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  148. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  149. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  150. ~APMG_PS_CTRL_MSK_PWR_SRC);
  151. else
  152. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  153. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  154. ~APMG_PS_CTRL_MSK_PWR_SRC);
  155. }
  156. /* PCI registers */
  157. #define PCI_CFG_RETRY_TIMEOUT 0x041
  158. static void iwl_pcie_apm_config(struct iwl_trans *trans)
  159. {
  160. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  161. u16 lctl;
  162. u16 cap;
  163. /*
  164. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  165. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  166. * If so (likely), disable L0S, so device moves directly L0->L1;
  167. * costs negligible amount of power savings.
  168. * If not (unlikely), enable L0S, so there is at least some
  169. * power savings, even without L1.
  170. */
  171. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  172. if (lctl & PCI_EXP_LNKCTL_ASPM_L1)
  173. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  174. else
  175. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  176. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  177. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
  178. trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
  179. dev_info(trans->dev, "L1 %sabled - LTR %sabled\n",
  180. (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
  181. trans->ltr_enabled ? "En" : "Dis");
  182. }
  183. /*
  184. * Start up NIC's basic functionality after it has been reset
  185. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  186. * NOTE: This does not load uCode nor start the embedded processor
  187. */
  188. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  189. {
  190. int ret = 0;
  191. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  192. /*
  193. * Use "set_bit" below rather than "write", to preserve any hardware
  194. * bits already set by default after reset.
  195. */
  196. /* Disable L0S exit timer (platform NMI Work/Around) */
  197. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  198. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  199. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  200. /*
  201. * Disable L0s without affecting L1;
  202. * don't wait for ICH L0s (ICH bug W/A)
  203. */
  204. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  205. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  206. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  207. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  208. /*
  209. * Enable HAP INTA (interrupt from management bus) to
  210. * wake device's PCI Express link L1a -> L0s
  211. */
  212. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  213. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  214. iwl_pcie_apm_config(trans);
  215. /* Configure analog phase-lock-loop before activating to D0A */
  216. if (trans->cfg->base_params->pll_cfg_val)
  217. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  218. trans->cfg->base_params->pll_cfg_val);
  219. /*
  220. * Set "initialization complete" bit to move adapter from
  221. * D0U* --> D0A* (powered-up active) state.
  222. */
  223. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  224. /*
  225. * Wait for clock stabilization; once stabilized, access to
  226. * device-internal resources is supported, e.g. iwl_write_prph()
  227. * and accesses to uCode SRAM.
  228. */
  229. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  230. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  231. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  232. if (ret < 0) {
  233. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  234. goto out;
  235. }
  236. if (trans->cfg->host_interrupt_operation_mode) {
  237. /*
  238. * This is a bit of an abuse - This is needed for 7260 / 3160
  239. * only check host_interrupt_operation_mode even if this is
  240. * not related to host_interrupt_operation_mode.
  241. *
  242. * Enable the oscillator to count wake up time for L1 exit. This
  243. * consumes slightly more power (100uA) - but allows to be sure
  244. * that we wake up from L1 on time.
  245. *
  246. * This looks weird: read twice the same register, discard the
  247. * value, set a bit, and yet again, read that same register
  248. * just to discard the value. But that's the way the hardware
  249. * seems to like it.
  250. */
  251. iwl_read_prph(trans, OSC_CLK);
  252. iwl_read_prph(trans, OSC_CLK);
  253. iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
  254. iwl_read_prph(trans, OSC_CLK);
  255. iwl_read_prph(trans, OSC_CLK);
  256. }
  257. /*
  258. * Enable DMA clock and wait for it to stabilize.
  259. *
  260. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
  261. * bits do not disable clocks. This preserves any hardware
  262. * bits already set by default in "CLK_CTRL_REG" after reset.
  263. */
  264. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  265. iwl_write_prph(trans, APMG_CLK_EN_REG,
  266. APMG_CLK_VAL_DMA_CLK_RQT);
  267. udelay(20);
  268. /* Disable L1-Active */
  269. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  270. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  271. /* Clear the interrupt in APMG if the NIC is in RFKILL */
  272. iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
  273. APMG_RTC_INT_STT_RFKILL);
  274. }
  275. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  276. out:
  277. return ret;
  278. }
  279. /*
  280. * Enable LP XTAL to avoid HW bug where device may consume much power if
  281. * FW is not loaded after device reset. LP XTAL is disabled by default
  282. * after device HW reset. Do it only if XTAL is fed by internal source.
  283. * Configure device's "persistence" mode to avoid resetting XTAL again when
  284. * SHRD_HW_RST occurs in S3.
  285. */
  286. static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
  287. {
  288. int ret;
  289. u32 apmg_gp1_reg;
  290. u32 apmg_xtal_cfg_reg;
  291. u32 dl_cfg_reg;
  292. /* Force XTAL ON */
  293. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  294. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  295. /* Reset entire device - do controller reset (results in SHRD_HW_RST) */
  296. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  297. udelay(10);
  298. /*
  299. * Set "initialization complete" bit to move adapter from
  300. * D0U* --> D0A* (powered-up active) state.
  301. */
  302. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  303. /*
  304. * Wait for clock stabilization; once stabilized, access to
  305. * device-internal resources is possible.
  306. */
  307. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  308. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  309. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  310. 25000);
  311. if (WARN_ON(ret < 0)) {
  312. IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
  313. /* Release XTAL ON request */
  314. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  315. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  316. return;
  317. }
  318. /*
  319. * Clear "disable persistence" to avoid LP XTAL resetting when
  320. * SHRD_HW_RST is applied in S3.
  321. */
  322. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  323. APMG_PCIDEV_STT_VAL_PERSIST_DIS);
  324. /*
  325. * Force APMG XTAL to be active to prevent its disabling by HW
  326. * caused by APMG idle state.
  327. */
  328. apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
  329. SHR_APMG_XTAL_CFG_REG);
  330. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  331. apmg_xtal_cfg_reg |
  332. SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  333. /*
  334. * Reset entire device again - do controller reset (results in
  335. * SHRD_HW_RST). Turn MAC off before proceeding.
  336. */
  337. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  338. udelay(10);
  339. /* Enable LP XTAL by indirect access through CSR */
  340. apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
  341. iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
  342. SHR_APMG_GP1_WF_XTAL_LP_EN |
  343. SHR_APMG_GP1_CHICKEN_BIT_SELECT);
  344. /* Clear delay line clock power up */
  345. dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
  346. iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
  347. ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
  348. /*
  349. * Enable persistence mode to avoid LP XTAL resetting when
  350. * SHRD_HW_RST is applied in S3.
  351. */
  352. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  353. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  354. /*
  355. * Clear "initialization complete" bit to move adapter from
  356. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  357. */
  358. iwl_clear_bit(trans, CSR_GP_CNTRL,
  359. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  360. /* Activates XTAL resources monitor */
  361. __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
  362. CSR_MONITOR_XTAL_RESOURCES);
  363. /* Release XTAL ON request */
  364. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  365. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  366. udelay(10);
  367. /* Release APMG XTAL */
  368. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  369. apmg_xtal_cfg_reg &
  370. ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  371. }
  372. static int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  373. {
  374. int ret = 0;
  375. /* stop device's busmaster DMA activity */
  376. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  377. ret = iwl_poll_bit(trans, CSR_RESET,
  378. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  379. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  380. if (ret < 0)
  381. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  382. IWL_DEBUG_INFO(trans, "stop master\n");
  383. return ret;
  384. }
  385. static void iwl_pcie_apm_stop(struct iwl_trans *trans)
  386. {
  387. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  388. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  389. /* Stop device's DMA activity */
  390. iwl_pcie_apm_stop_master(trans);
  391. if (trans->cfg->lp_xtal_workaround) {
  392. iwl_pcie_apm_lp_xtal_enable(trans);
  393. return;
  394. }
  395. /* Reset the entire device */
  396. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  397. udelay(10);
  398. /*
  399. * Clear "initialization complete" bit to move adapter from
  400. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  401. */
  402. iwl_clear_bit(trans, CSR_GP_CNTRL,
  403. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  404. }
  405. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  406. {
  407. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  408. /* nic_init */
  409. spin_lock(&trans_pcie->irq_lock);
  410. iwl_pcie_apm_init(trans);
  411. spin_unlock(&trans_pcie->irq_lock);
  412. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  413. iwl_pcie_set_pwr(trans, false);
  414. iwl_op_mode_nic_config(trans->op_mode);
  415. /* Allocate the RX queue, or reset if it is already allocated */
  416. iwl_pcie_rx_init(trans);
  417. /* Allocate or reset and init all Tx and Command queues */
  418. if (iwl_pcie_tx_init(trans))
  419. return -ENOMEM;
  420. if (trans->cfg->base_params->shadow_reg_enable) {
  421. /* enable shadow regs in HW */
  422. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  423. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  424. }
  425. return 0;
  426. }
  427. #define HW_READY_TIMEOUT (50)
  428. /* Note: returns poll_bit return value, which is >= 0 if success */
  429. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  430. {
  431. int ret;
  432. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  433. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  434. /* See if we got it */
  435. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  436. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  437. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  438. HW_READY_TIMEOUT);
  439. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  440. return ret;
  441. }
  442. /* Note: returns standard 0/-ERROR code */
  443. static int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  444. {
  445. int ret;
  446. int t = 0;
  447. int iter;
  448. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  449. ret = iwl_pcie_set_hw_ready(trans);
  450. /* If the card is ready, exit 0 */
  451. if (ret >= 0)
  452. return 0;
  453. for (iter = 0; iter < 10; iter++) {
  454. /* If HW is not ready, prepare the conditions to check again */
  455. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  456. CSR_HW_IF_CONFIG_REG_PREPARE);
  457. do {
  458. ret = iwl_pcie_set_hw_ready(trans);
  459. if (ret >= 0)
  460. return 0;
  461. usleep_range(200, 1000);
  462. t += 200;
  463. } while (t < 150000);
  464. msleep(25);
  465. }
  466. IWL_ERR(trans, "Couldn't prepare the card\n");
  467. return ret;
  468. }
  469. /*
  470. * ucode
  471. */
  472. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, u32 dst_addr,
  473. dma_addr_t phy_addr, u32 byte_cnt)
  474. {
  475. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  476. int ret;
  477. trans_pcie->ucode_write_complete = false;
  478. iwl_write_direct32(trans,
  479. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  480. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  481. iwl_write_direct32(trans,
  482. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  483. dst_addr);
  484. iwl_write_direct32(trans,
  485. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  486. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  487. iwl_write_direct32(trans,
  488. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  489. (iwl_get_dma_hi_addr(phy_addr)
  490. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  491. iwl_write_direct32(trans,
  492. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  493. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  494. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  495. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  496. iwl_write_direct32(trans,
  497. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  498. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  499. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  500. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  501. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  502. trans_pcie->ucode_write_complete, 5 * HZ);
  503. if (!ret) {
  504. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  505. return -ETIMEDOUT;
  506. }
  507. return 0;
  508. }
  509. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  510. const struct fw_desc *section)
  511. {
  512. u8 *v_addr;
  513. dma_addr_t p_addr;
  514. u32 offset, chunk_sz = section->len;
  515. int ret = 0;
  516. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  517. section_num);
  518. v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
  519. GFP_KERNEL | __GFP_NOWARN);
  520. if (!v_addr) {
  521. IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
  522. chunk_sz = PAGE_SIZE;
  523. v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
  524. &p_addr, GFP_KERNEL);
  525. if (!v_addr)
  526. return -ENOMEM;
  527. }
  528. for (offset = 0; offset < section->len; offset += chunk_sz) {
  529. u32 copy_size;
  530. copy_size = min_t(u32, chunk_sz, section->len - offset);
  531. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  532. ret = iwl_pcie_load_firmware_chunk(trans,
  533. section->offset + offset,
  534. p_addr, copy_size);
  535. if (ret) {
  536. IWL_ERR(trans,
  537. "Could not load the [%d] uCode section\n",
  538. section_num);
  539. break;
  540. }
  541. }
  542. dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
  543. return ret;
  544. }
  545. static int iwl_pcie_load_cpu_secured_sections(struct iwl_trans *trans,
  546. const struct fw_img *image,
  547. int cpu,
  548. int *first_ucode_section)
  549. {
  550. int shift_param;
  551. int i, ret = 0;
  552. u32 last_read_idx = 0;
  553. if (cpu == 1) {
  554. shift_param = 0;
  555. *first_ucode_section = 0;
  556. } else {
  557. shift_param = 16;
  558. (*first_ucode_section)++;
  559. }
  560. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  561. last_read_idx = i;
  562. if (!image->sec[i].data ||
  563. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  564. IWL_DEBUG_FW(trans,
  565. "Break since Data not valid or Empty section, sec = %d\n",
  566. i);
  567. break;
  568. }
  569. if (i == (*first_ucode_section) + 1)
  570. /* set CPU to started */
  571. iwl_set_bits_prph(trans,
  572. CSR_UCODE_LOAD_STATUS_ADDR,
  573. LMPM_CPU_HDRS_LOADING_COMPLETED
  574. << shift_param);
  575. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  576. if (ret)
  577. return ret;
  578. }
  579. /* image loading complete */
  580. iwl_set_bits_prph(trans,
  581. CSR_UCODE_LOAD_STATUS_ADDR,
  582. LMPM_CPU_UCODE_LOADING_COMPLETED << shift_param);
  583. *first_ucode_section = last_read_idx;
  584. return 0;
  585. }
  586. static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
  587. const struct fw_img *image,
  588. int cpu,
  589. int *first_ucode_section)
  590. {
  591. int shift_param;
  592. int i, ret = 0;
  593. u32 last_read_idx = 0;
  594. if (cpu == 1) {
  595. shift_param = 0;
  596. *first_ucode_section = 0;
  597. } else {
  598. shift_param = 16;
  599. (*first_ucode_section)++;
  600. }
  601. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  602. last_read_idx = i;
  603. if (!image->sec[i].data ||
  604. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  605. IWL_DEBUG_FW(trans,
  606. "Break since Data not valid or Empty section, sec = %d\n",
  607. i);
  608. break;
  609. }
  610. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  611. if (ret)
  612. return ret;
  613. }
  614. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  615. iwl_set_bits_prph(trans,
  616. CSR_UCODE_LOAD_STATUS_ADDR,
  617. (LMPM_CPU_UCODE_LOADING_COMPLETED |
  618. LMPM_CPU_HDRS_LOADING_COMPLETED |
  619. LMPM_CPU_UCODE_LOADING_STARTED) <<
  620. shift_param);
  621. *first_ucode_section = last_read_idx;
  622. return 0;
  623. }
  624. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  625. const struct fw_img *image)
  626. {
  627. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  628. int ret = 0;
  629. int first_ucode_section;
  630. IWL_DEBUG_FW(trans,
  631. "working with %s image\n",
  632. image->is_secure ? "Secured" : "Non Secured");
  633. IWL_DEBUG_FW(trans,
  634. "working with %s CPU\n",
  635. image->is_dual_cpus ? "Dual" : "Single");
  636. /* configure the ucode to be ready to get the secured image */
  637. if (image->is_secure) {
  638. /* set secure boot inspector addresses */
  639. iwl_write_prph(trans,
  640. LMPM_SECURE_INSPECTOR_CODE_ADDR,
  641. LMPM_SECURE_INSPECTOR_CODE_MEM_SPACE);
  642. iwl_write_prph(trans,
  643. LMPM_SECURE_INSPECTOR_DATA_ADDR,
  644. LMPM_SECURE_INSPECTOR_DATA_MEM_SPACE);
  645. /* set CPU1 header address */
  646. iwl_write_prph(trans,
  647. LMPM_SECURE_UCODE_LOAD_CPU1_HDR_ADDR,
  648. LMPM_SECURE_CPU1_HDR_MEM_SPACE);
  649. /* load to FW the binary Secured sections of CPU1 */
  650. ret = iwl_pcie_load_cpu_secured_sections(trans, image, 1,
  651. &first_ucode_section);
  652. if (ret)
  653. return ret;
  654. } else {
  655. /* load to FW the binary Non secured sections of CPU1 */
  656. ret = iwl_pcie_load_cpu_sections(trans, image, 1,
  657. &first_ucode_section);
  658. if (ret)
  659. return ret;
  660. }
  661. if (image->is_dual_cpus) {
  662. /* set CPU2 header address */
  663. iwl_write_prph(trans,
  664. LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
  665. LMPM_SECURE_CPU2_HDR_MEM_SPACE);
  666. /* load to FW the binary sections of CPU2 */
  667. if (image->is_secure)
  668. ret = iwl_pcie_load_cpu_secured_sections(
  669. trans, image, 2,
  670. &first_ucode_section);
  671. else
  672. ret = iwl_pcie_load_cpu_sections(trans, image, 2,
  673. &first_ucode_section);
  674. if (ret)
  675. return ret;
  676. }
  677. /* supported for 7000 only for the moment */
  678. if (iwlwifi_mod_params.fw_monitor &&
  679. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  680. iwl_pcie_alloc_fw_monitor(trans);
  681. if (trans_pcie->fw_mon_size) {
  682. iwl_write_prph(trans, MON_BUFF_BASE_ADDR,
  683. trans_pcie->fw_mon_phys >> 4);
  684. iwl_write_prph(trans, MON_BUFF_END_ADDR,
  685. (trans_pcie->fw_mon_phys +
  686. trans_pcie->fw_mon_size) >> 4);
  687. }
  688. }
  689. /* release CPU reset */
  690. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  691. iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  692. else
  693. iwl_write32(trans, CSR_RESET, 0);
  694. if (image->is_secure) {
  695. /* wait for image verification to complete */
  696. ret = iwl_poll_prph_bit(trans,
  697. LMPM_SECURE_BOOT_CPU1_STATUS_ADDR,
  698. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  699. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  700. LMPM_SECURE_TIME_OUT);
  701. if (ret < 0) {
  702. IWL_ERR(trans, "Time out on secure boot process\n");
  703. return ret;
  704. }
  705. }
  706. return 0;
  707. }
  708. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  709. const struct fw_img *fw, bool run_in_rfkill)
  710. {
  711. int ret;
  712. bool hw_rfkill;
  713. /* This may fail if AMT took ownership of the device */
  714. if (iwl_pcie_prepare_card_hw(trans)) {
  715. IWL_WARN(trans, "Exit HW not ready\n");
  716. return -EIO;
  717. }
  718. iwl_enable_rfkill_int(trans);
  719. /* If platform's RF_KILL switch is NOT set to KILL */
  720. hw_rfkill = iwl_is_rfkill_set(trans);
  721. if (hw_rfkill)
  722. set_bit(STATUS_RFKILL, &trans->status);
  723. else
  724. clear_bit(STATUS_RFKILL, &trans->status);
  725. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  726. if (hw_rfkill && !run_in_rfkill)
  727. return -ERFKILL;
  728. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  729. ret = iwl_pcie_nic_init(trans);
  730. if (ret) {
  731. IWL_ERR(trans, "Unable to init nic\n");
  732. return ret;
  733. }
  734. /* make sure rfkill handshake bits are cleared */
  735. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  736. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  737. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  738. /* clear (again), then enable host interrupts */
  739. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  740. iwl_enable_interrupts(trans);
  741. /* really make sure rfkill handshake bits are cleared */
  742. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  743. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  744. /* Load the given image to the HW */
  745. return iwl_pcie_load_given_ucode(trans, fw);
  746. }
  747. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  748. {
  749. iwl_pcie_reset_ict(trans);
  750. iwl_pcie_tx_start(trans, scd_addr);
  751. }
  752. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  753. {
  754. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  755. bool hw_rfkill, was_hw_rfkill;
  756. was_hw_rfkill = iwl_is_rfkill_set(trans);
  757. /* tell the device to stop sending interrupts */
  758. spin_lock(&trans_pcie->irq_lock);
  759. iwl_disable_interrupts(trans);
  760. spin_unlock(&trans_pcie->irq_lock);
  761. /* device going down, Stop using ICT table */
  762. iwl_pcie_disable_ict(trans);
  763. /*
  764. * If a HW restart happens during firmware loading,
  765. * then the firmware loading might call this function
  766. * and later it might be called again due to the
  767. * restart. So don't process again if the device is
  768. * already dead.
  769. */
  770. if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  771. IWL_DEBUG_INFO(trans, "DEVICE_ENABLED bit was set and is now cleared\n");
  772. iwl_pcie_tx_stop(trans);
  773. iwl_pcie_rx_stop(trans);
  774. /* Power-down device's busmaster DMA clocks */
  775. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  776. APMG_CLK_VAL_DMA_CLK_RQT);
  777. udelay(5);
  778. }
  779. /* Make sure (redundant) we've released our request to stay awake */
  780. iwl_clear_bit(trans, CSR_GP_CNTRL,
  781. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  782. /* Stop the device, and put it in low power state */
  783. iwl_pcie_apm_stop(trans);
  784. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  785. * Clean again the interrupt here
  786. */
  787. spin_lock(&trans_pcie->irq_lock);
  788. iwl_disable_interrupts(trans);
  789. spin_unlock(&trans_pcie->irq_lock);
  790. /* stop and reset the on-board processor */
  791. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  792. /* clear all status bits */
  793. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  794. clear_bit(STATUS_INT_ENABLED, &trans->status);
  795. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  796. clear_bit(STATUS_RFKILL, &trans->status);
  797. /*
  798. * Even if we stop the HW, we still want the RF kill
  799. * interrupt
  800. */
  801. iwl_enable_rfkill_int(trans);
  802. /*
  803. * Check again since the RF kill state may have changed while
  804. * all the interrupts were disabled, in this case we couldn't
  805. * receive the RF kill interrupt and update the state in the
  806. * op_mode.
  807. * Don't call the op_mode if the rkfill state hasn't changed.
  808. * This allows the op_mode to call stop_device from the rfkill
  809. * notification without endless recursion. Under very rare
  810. * circumstances, we might have a small recursion if the rfkill
  811. * state changed exactly now while we were called from stop_device.
  812. * This is very unlikely but can happen and is supported.
  813. */
  814. hw_rfkill = iwl_is_rfkill_set(trans);
  815. if (hw_rfkill)
  816. set_bit(STATUS_RFKILL, &trans->status);
  817. else
  818. clear_bit(STATUS_RFKILL, &trans->status);
  819. if (hw_rfkill != was_hw_rfkill)
  820. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  821. }
  822. void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
  823. {
  824. if (iwl_op_mode_hw_rf_kill(trans->op_mode, state))
  825. iwl_trans_pcie_stop_device(trans);
  826. }
  827. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test)
  828. {
  829. iwl_disable_interrupts(trans);
  830. /*
  831. * in testing mode, the host stays awake and the
  832. * hardware won't be reset (not even partially)
  833. */
  834. if (test)
  835. return;
  836. iwl_pcie_disable_ict(trans);
  837. iwl_clear_bit(trans, CSR_GP_CNTRL,
  838. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  839. iwl_clear_bit(trans, CSR_GP_CNTRL,
  840. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  841. /*
  842. * reset TX queues -- some of their registers reset during S3
  843. * so if we don't reset everything here the D3 image would try
  844. * to execute some invalid memory upon resume
  845. */
  846. iwl_trans_pcie_tx_reset(trans);
  847. iwl_pcie_set_pwr(trans, true);
  848. }
  849. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  850. enum iwl_d3_status *status,
  851. bool test)
  852. {
  853. u32 val;
  854. int ret;
  855. if (test) {
  856. iwl_enable_interrupts(trans);
  857. *status = IWL_D3_STATUS_ALIVE;
  858. return 0;
  859. }
  860. iwl_pcie_set_pwr(trans, false);
  861. val = iwl_read32(trans, CSR_RESET);
  862. if (val & CSR_RESET_REG_FLAG_NEVO_RESET) {
  863. *status = IWL_D3_STATUS_RESET;
  864. return 0;
  865. }
  866. /*
  867. * Also enables interrupts - none will happen as the device doesn't
  868. * know we're waking it up, only when the opmode actually tells it
  869. * after this call.
  870. */
  871. iwl_pcie_reset_ict(trans);
  872. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  873. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  874. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  875. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  876. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  877. 25000);
  878. if (ret < 0) {
  879. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  880. return ret;
  881. }
  882. iwl_trans_pcie_tx_reset(trans);
  883. ret = iwl_pcie_rx_init(trans);
  884. if (ret) {
  885. IWL_ERR(trans, "Failed to resume the device (RX reset)\n");
  886. return ret;
  887. }
  888. *status = IWL_D3_STATUS_ALIVE;
  889. return 0;
  890. }
  891. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  892. {
  893. bool hw_rfkill;
  894. int err;
  895. err = iwl_pcie_prepare_card_hw(trans);
  896. if (err) {
  897. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  898. return err;
  899. }
  900. /* Reset the entire device */
  901. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  902. usleep_range(10, 15);
  903. iwl_pcie_apm_init(trans);
  904. /* From now on, the op_mode will be kept updated about RF kill state */
  905. iwl_enable_rfkill_int(trans);
  906. hw_rfkill = iwl_is_rfkill_set(trans);
  907. if (hw_rfkill)
  908. set_bit(STATUS_RFKILL, &trans->status);
  909. else
  910. clear_bit(STATUS_RFKILL, &trans->status);
  911. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  912. return 0;
  913. }
  914. static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
  915. {
  916. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  917. /* disable interrupts - don't enable HW RF kill interrupt */
  918. spin_lock(&trans_pcie->irq_lock);
  919. iwl_disable_interrupts(trans);
  920. spin_unlock(&trans_pcie->irq_lock);
  921. iwl_pcie_apm_stop(trans);
  922. spin_lock(&trans_pcie->irq_lock);
  923. iwl_disable_interrupts(trans);
  924. spin_unlock(&trans_pcie->irq_lock);
  925. iwl_pcie_disable_ict(trans);
  926. }
  927. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  928. {
  929. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  930. }
  931. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  932. {
  933. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  934. }
  935. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  936. {
  937. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  938. }
  939. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  940. {
  941. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
  942. ((reg & 0x000FFFFF) | (3 << 24)));
  943. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  944. }
  945. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  946. u32 val)
  947. {
  948. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  949. ((addr & 0x000FFFFF) | (3 << 24)));
  950. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  951. }
  952. static int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget)
  953. {
  954. WARN_ON(1);
  955. return 0;
  956. }
  957. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  958. const struct iwl_trans_config *trans_cfg)
  959. {
  960. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  961. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  962. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  963. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  964. trans_pcie->n_no_reclaim_cmds = 0;
  965. else
  966. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  967. if (trans_pcie->n_no_reclaim_cmds)
  968. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  969. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  970. trans_pcie->rx_buf_size_8k = trans_cfg->rx_buf_size_8k;
  971. if (trans_pcie->rx_buf_size_8k)
  972. trans_pcie->rx_page_order = get_order(8 * 1024);
  973. else
  974. trans_pcie->rx_page_order = get_order(4 * 1024);
  975. trans_pcie->wd_timeout =
  976. msecs_to_jiffies(trans_cfg->queue_watchdog_timeout);
  977. trans_pcie->command_names = trans_cfg->command_names;
  978. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  979. trans_pcie->scd_set_active = trans_cfg->scd_set_active;
  980. /* Initialize NAPI here - it should be before registering to mac80211
  981. * in the opmode but after the HW struct is allocated.
  982. * As this function may be called again in some corner cases don't
  983. * do anything if NAPI was already initialized.
  984. */
  985. if (!trans_pcie->napi.poll && trans->op_mode->ops->napi_add) {
  986. init_dummy_netdev(&trans_pcie->napi_dev);
  987. iwl_op_mode_napi_add(trans->op_mode, &trans_pcie->napi,
  988. &trans_pcie->napi_dev,
  989. iwl_pcie_dummy_napi_poll, 64);
  990. }
  991. }
  992. void iwl_trans_pcie_free(struct iwl_trans *trans)
  993. {
  994. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  995. synchronize_irq(trans_pcie->pci_dev->irq);
  996. iwl_pcie_tx_free(trans);
  997. iwl_pcie_rx_free(trans);
  998. free_irq(trans_pcie->pci_dev->irq, trans);
  999. iwl_pcie_free_ict(trans);
  1000. pci_disable_msi(trans_pcie->pci_dev);
  1001. iounmap(trans_pcie->hw_base);
  1002. pci_release_regions(trans_pcie->pci_dev);
  1003. pci_disable_device(trans_pcie->pci_dev);
  1004. kmem_cache_destroy(trans->dev_cmd_pool);
  1005. if (trans_pcie->napi.poll)
  1006. netif_napi_del(&trans_pcie->napi);
  1007. iwl_pcie_free_fw_monitor(trans);
  1008. kfree(trans);
  1009. }
  1010. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  1011. {
  1012. if (state)
  1013. set_bit(STATUS_TPOWER_PMI, &trans->status);
  1014. else
  1015. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  1016. }
  1017. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent,
  1018. unsigned long *flags)
  1019. {
  1020. int ret;
  1021. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1022. spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
  1023. if (trans_pcie->cmd_in_flight)
  1024. goto out;
  1025. /* this bit wakes up the NIC */
  1026. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  1027. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1028. /*
  1029. * These bits say the device is running, and should keep running for
  1030. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  1031. * but they do not indicate that embedded SRAM is restored yet;
  1032. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  1033. * to/from host DRAM when sleeping/waking for power-saving.
  1034. * Each direction takes approximately 1/4 millisecond; with this
  1035. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  1036. * series of register accesses are expected (e.g. reading Event Log),
  1037. * to keep device from sleeping.
  1038. *
  1039. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  1040. * SRAM is okay/restored. We don't check that here because this call
  1041. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  1042. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  1043. *
  1044. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  1045. * and do not save/restore SRAM when power cycling.
  1046. */
  1047. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1048. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  1049. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  1050. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  1051. if (unlikely(ret < 0)) {
  1052. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  1053. if (!silent) {
  1054. u32 val = iwl_read32(trans, CSR_GP_CNTRL);
  1055. WARN_ONCE(1,
  1056. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  1057. val);
  1058. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1059. return false;
  1060. }
  1061. }
  1062. out:
  1063. /*
  1064. * Fool sparse by faking we release the lock - sparse will
  1065. * track nic_access anyway.
  1066. */
  1067. __release(&trans_pcie->reg_lock);
  1068. return true;
  1069. }
  1070. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
  1071. unsigned long *flags)
  1072. {
  1073. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1074. lockdep_assert_held(&trans_pcie->reg_lock);
  1075. /*
  1076. * Fool sparse by faking we acquiring the lock - sparse will
  1077. * track nic_access anyway.
  1078. */
  1079. __acquire(&trans_pcie->reg_lock);
  1080. if (trans_pcie->cmd_in_flight)
  1081. goto out;
  1082. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  1083. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1084. /*
  1085. * Above we read the CSR_GP_CNTRL register, which will flush
  1086. * any previous writes, but we need the write that clears the
  1087. * MAC_ACCESS_REQ bit to be performed before any other writes
  1088. * scheduled on different CPUs (after we drop reg_lock).
  1089. */
  1090. mmiowb();
  1091. out:
  1092. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1093. }
  1094. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  1095. void *buf, int dwords)
  1096. {
  1097. unsigned long flags;
  1098. int offs, ret = 0;
  1099. u32 *vals = buf;
  1100. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1101. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  1102. for (offs = 0; offs < dwords; offs++)
  1103. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  1104. iwl_trans_release_nic_access(trans, &flags);
  1105. } else {
  1106. ret = -EBUSY;
  1107. }
  1108. return ret;
  1109. }
  1110. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  1111. const void *buf, int dwords)
  1112. {
  1113. unsigned long flags;
  1114. int offs, ret = 0;
  1115. const u32 *vals = buf;
  1116. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1117. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  1118. for (offs = 0; offs < dwords; offs++)
  1119. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  1120. vals ? vals[offs] : 0);
  1121. iwl_trans_release_nic_access(trans, &flags);
  1122. } else {
  1123. ret = -EBUSY;
  1124. }
  1125. return ret;
  1126. }
  1127. #define IWL_FLUSH_WAIT_MS 2000
  1128. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, u32 txq_bm)
  1129. {
  1130. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1131. struct iwl_txq *txq;
  1132. struct iwl_queue *q;
  1133. int cnt;
  1134. unsigned long now = jiffies;
  1135. u32 scd_sram_addr;
  1136. u8 buf[16];
  1137. int ret = 0;
  1138. /* waiting for all the tx frames complete might take a while */
  1139. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1140. u8 wr_ptr;
  1141. if (cnt == trans_pcie->cmd_queue)
  1142. continue;
  1143. if (!test_bit(cnt, trans_pcie->queue_used))
  1144. continue;
  1145. if (!(BIT(cnt) & txq_bm))
  1146. continue;
  1147. IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", cnt);
  1148. txq = &trans_pcie->txq[cnt];
  1149. q = &txq->q;
  1150. wr_ptr = ACCESS_ONCE(q->write_ptr);
  1151. while (q->read_ptr != ACCESS_ONCE(q->write_ptr) &&
  1152. !time_after(jiffies,
  1153. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
  1154. u8 write_ptr = ACCESS_ONCE(q->write_ptr);
  1155. if (WARN_ONCE(wr_ptr != write_ptr,
  1156. "WR pointer moved while flushing %d -> %d\n",
  1157. wr_ptr, write_ptr))
  1158. return -ETIMEDOUT;
  1159. msleep(1);
  1160. }
  1161. if (q->read_ptr != q->write_ptr) {
  1162. IWL_ERR(trans,
  1163. "fail to flush all tx fifo queues Q %d\n", cnt);
  1164. ret = -ETIMEDOUT;
  1165. break;
  1166. }
  1167. IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", cnt);
  1168. }
  1169. if (!ret)
  1170. return 0;
  1171. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1172. txq->q.read_ptr, txq->q.write_ptr);
  1173. scd_sram_addr = trans_pcie->scd_base_addr +
  1174. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  1175. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  1176. iwl_print_hex_error(trans, buf, sizeof(buf));
  1177. for (cnt = 0; cnt < FH_TCSR_CHNL_NUM; cnt++)
  1178. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", cnt,
  1179. iwl_read_direct32(trans, FH_TX_TRB_REG(cnt)));
  1180. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1181. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(cnt));
  1182. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  1183. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  1184. u32 tbl_dw =
  1185. iwl_trans_read_mem32(trans, trans_pcie->scd_base_addr +
  1186. SCD_TRANS_TBL_OFFSET_QUEUE(cnt));
  1187. if (cnt & 0x1)
  1188. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  1189. else
  1190. tbl_dw = tbl_dw & 0x0000FFFF;
  1191. IWL_ERR(trans,
  1192. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  1193. cnt, active ? "" : "in", fifo, tbl_dw,
  1194. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt)) &
  1195. (TFD_QUEUE_SIZE_MAX - 1),
  1196. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1197. }
  1198. return ret;
  1199. }
  1200. static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
  1201. u32 mask, u32 value)
  1202. {
  1203. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1204. unsigned long flags;
  1205. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1206. __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
  1207. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1208. }
  1209. static const char *get_csr_string(int cmd)
  1210. {
  1211. #define IWL_CMD(x) case x: return #x
  1212. switch (cmd) {
  1213. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1214. IWL_CMD(CSR_INT_COALESCING);
  1215. IWL_CMD(CSR_INT);
  1216. IWL_CMD(CSR_INT_MASK);
  1217. IWL_CMD(CSR_FH_INT_STATUS);
  1218. IWL_CMD(CSR_GPIO_IN);
  1219. IWL_CMD(CSR_RESET);
  1220. IWL_CMD(CSR_GP_CNTRL);
  1221. IWL_CMD(CSR_HW_REV);
  1222. IWL_CMD(CSR_EEPROM_REG);
  1223. IWL_CMD(CSR_EEPROM_GP);
  1224. IWL_CMD(CSR_OTP_GP_REG);
  1225. IWL_CMD(CSR_GIO_REG);
  1226. IWL_CMD(CSR_GP_UCODE_REG);
  1227. IWL_CMD(CSR_GP_DRIVER_REG);
  1228. IWL_CMD(CSR_UCODE_DRV_GP1);
  1229. IWL_CMD(CSR_UCODE_DRV_GP2);
  1230. IWL_CMD(CSR_LED_REG);
  1231. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1232. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1233. IWL_CMD(CSR_ANA_PLL_CFG);
  1234. IWL_CMD(CSR_HW_REV_WA_REG);
  1235. IWL_CMD(CSR_MONITOR_STATUS_REG);
  1236. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1237. default:
  1238. return "UNKNOWN";
  1239. }
  1240. #undef IWL_CMD
  1241. }
  1242. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  1243. {
  1244. int i;
  1245. static const u32 csr_tbl[] = {
  1246. CSR_HW_IF_CONFIG_REG,
  1247. CSR_INT_COALESCING,
  1248. CSR_INT,
  1249. CSR_INT_MASK,
  1250. CSR_FH_INT_STATUS,
  1251. CSR_GPIO_IN,
  1252. CSR_RESET,
  1253. CSR_GP_CNTRL,
  1254. CSR_HW_REV,
  1255. CSR_EEPROM_REG,
  1256. CSR_EEPROM_GP,
  1257. CSR_OTP_GP_REG,
  1258. CSR_GIO_REG,
  1259. CSR_GP_UCODE_REG,
  1260. CSR_GP_DRIVER_REG,
  1261. CSR_UCODE_DRV_GP1,
  1262. CSR_UCODE_DRV_GP2,
  1263. CSR_LED_REG,
  1264. CSR_DRAM_INT_TBL_REG,
  1265. CSR_GIO_CHICKEN_BITS,
  1266. CSR_ANA_PLL_CFG,
  1267. CSR_MONITOR_STATUS_REG,
  1268. CSR_HW_REV_WA_REG,
  1269. CSR_DBG_HPET_MEM_REG
  1270. };
  1271. IWL_ERR(trans, "CSR values:\n");
  1272. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1273. "CSR_INT_PERIODIC_REG)\n");
  1274. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1275. IWL_ERR(trans, " %25s: 0X%08x\n",
  1276. get_csr_string(csr_tbl[i]),
  1277. iwl_read32(trans, csr_tbl[i]));
  1278. }
  1279. }
  1280. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1281. /* create and remove of files */
  1282. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1283. if (!debugfs_create_file(#name, mode, parent, trans, \
  1284. &iwl_dbgfs_##name##_ops)) \
  1285. goto err; \
  1286. } while (0)
  1287. /* file operation */
  1288. #define DEBUGFS_READ_FILE_OPS(name) \
  1289. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1290. .read = iwl_dbgfs_##name##_read, \
  1291. .open = simple_open, \
  1292. .llseek = generic_file_llseek, \
  1293. };
  1294. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1295. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1296. .write = iwl_dbgfs_##name##_write, \
  1297. .open = simple_open, \
  1298. .llseek = generic_file_llseek, \
  1299. };
  1300. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1301. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1302. .write = iwl_dbgfs_##name##_write, \
  1303. .read = iwl_dbgfs_##name##_read, \
  1304. .open = simple_open, \
  1305. .llseek = generic_file_llseek, \
  1306. };
  1307. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1308. char __user *user_buf,
  1309. size_t count, loff_t *ppos)
  1310. {
  1311. struct iwl_trans *trans = file->private_data;
  1312. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1313. struct iwl_txq *txq;
  1314. struct iwl_queue *q;
  1315. char *buf;
  1316. int pos = 0;
  1317. int cnt;
  1318. int ret;
  1319. size_t bufsz;
  1320. bufsz = sizeof(char) * 64 * trans->cfg->base_params->num_of_queues;
  1321. if (!trans_pcie->txq)
  1322. return -EAGAIN;
  1323. buf = kzalloc(bufsz, GFP_KERNEL);
  1324. if (!buf)
  1325. return -ENOMEM;
  1326. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1327. txq = &trans_pcie->txq[cnt];
  1328. q = &txq->q;
  1329. pos += scnprintf(buf + pos, bufsz - pos,
  1330. "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d%s\n",
  1331. cnt, q->read_ptr, q->write_ptr,
  1332. !!test_bit(cnt, trans_pcie->queue_used),
  1333. !!test_bit(cnt, trans_pcie->queue_stopped),
  1334. txq->need_update,
  1335. (cnt == trans_pcie->cmd_queue ? " HCMD" : ""));
  1336. }
  1337. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1338. kfree(buf);
  1339. return ret;
  1340. }
  1341. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1342. char __user *user_buf,
  1343. size_t count, loff_t *ppos)
  1344. {
  1345. struct iwl_trans *trans = file->private_data;
  1346. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1347. struct iwl_rxq *rxq = &trans_pcie->rxq;
  1348. char buf[256];
  1349. int pos = 0;
  1350. const size_t bufsz = sizeof(buf);
  1351. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1352. rxq->read);
  1353. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1354. rxq->write);
  1355. pos += scnprintf(buf + pos, bufsz - pos, "write_actual: %u\n",
  1356. rxq->write_actual);
  1357. pos += scnprintf(buf + pos, bufsz - pos, "need_update: %d\n",
  1358. rxq->need_update);
  1359. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1360. rxq->free_count);
  1361. if (rxq->rb_stts) {
  1362. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1363. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1364. } else {
  1365. pos += scnprintf(buf + pos, bufsz - pos,
  1366. "closed_rb_num: Not Allocated\n");
  1367. }
  1368. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1369. }
  1370. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1371. char __user *user_buf,
  1372. size_t count, loff_t *ppos)
  1373. {
  1374. struct iwl_trans *trans = file->private_data;
  1375. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1376. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1377. int pos = 0;
  1378. char *buf;
  1379. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1380. ssize_t ret;
  1381. buf = kzalloc(bufsz, GFP_KERNEL);
  1382. if (!buf)
  1383. return -ENOMEM;
  1384. pos += scnprintf(buf + pos, bufsz - pos,
  1385. "Interrupt Statistics Report:\n");
  1386. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1387. isr_stats->hw);
  1388. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1389. isr_stats->sw);
  1390. if (isr_stats->sw || isr_stats->hw) {
  1391. pos += scnprintf(buf + pos, bufsz - pos,
  1392. "\tLast Restarting Code: 0x%X\n",
  1393. isr_stats->err_code);
  1394. }
  1395. #ifdef CONFIG_IWLWIFI_DEBUG
  1396. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1397. isr_stats->sch);
  1398. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1399. isr_stats->alive);
  1400. #endif
  1401. pos += scnprintf(buf + pos, bufsz - pos,
  1402. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1403. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1404. isr_stats->ctkill);
  1405. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1406. isr_stats->wakeup);
  1407. pos += scnprintf(buf + pos, bufsz - pos,
  1408. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1409. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1410. isr_stats->tx);
  1411. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1412. isr_stats->unhandled);
  1413. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1414. kfree(buf);
  1415. return ret;
  1416. }
  1417. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1418. const char __user *user_buf,
  1419. size_t count, loff_t *ppos)
  1420. {
  1421. struct iwl_trans *trans = file->private_data;
  1422. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1423. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1424. char buf[8];
  1425. int buf_size;
  1426. u32 reset_flag;
  1427. memset(buf, 0, sizeof(buf));
  1428. buf_size = min(count, sizeof(buf) - 1);
  1429. if (copy_from_user(buf, user_buf, buf_size))
  1430. return -EFAULT;
  1431. if (sscanf(buf, "%x", &reset_flag) != 1)
  1432. return -EFAULT;
  1433. if (reset_flag == 0)
  1434. memset(isr_stats, 0, sizeof(*isr_stats));
  1435. return count;
  1436. }
  1437. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1438. const char __user *user_buf,
  1439. size_t count, loff_t *ppos)
  1440. {
  1441. struct iwl_trans *trans = file->private_data;
  1442. char buf[8];
  1443. int buf_size;
  1444. int csr;
  1445. memset(buf, 0, sizeof(buf));
  1446. buf_size = min(count, sizeof(buf) - 1);
  1447. if (copy_from_user(buf, user_buf, buf_size))
  1448. return -EFAULT;
  1449. if (sscanf(buf, "%d", &csr) != 1)
  1450. return -EFAULT;
  1451. iwl_pcie_dump_csr(trans);
  1452. return count;
  1453. }
  1454. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1455. char __user *user_buf,
  1456. size_t count, loff_t *ppos)
  1457. {
  1458. struct iwl_trans *trans = file->private_data;
  1459. char *buf = NULL;
  1460. ssize_t ret;
  1461. ret = iwl_dump_fh(trans, &buf);
  1462. if (ret < 0)
  1463. return ret;
  1464. if (!buf)
  1465. return -EINVAL;
  1466. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1467. kfree(buf);
  1468. return ret;
  1469. }
  1470. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1471. DEBUGFS_READ_FILE_OPS(fh_reg);
  1472. DEBUGFS_READ_FILE_OPS(rx_queue);
  1473. DEBUGFS_READ_FILE_OPS(tx_queue);
  1474. DEBUGFS_WRITE_FILE_OPS(csr);
  1475. /*
  1476. * Create the debugfs files and directories
  1477. *
  1478. */
  1479. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1480. struct dentry *dir)
  1481. {
  1482. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1483. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1484. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1485. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1486. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1487. return 0;
  1488. err:
  1489. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  1490. return -ENOMEM;
  1491. }
  1492. static u32 iwl_trans_pcie_get_cmdlen(struct iwl_tfd *tfd)
  1493. {
  1494. u32 cmdlen = 0;
  1495. int i;
  1496. for (i = 0; i < IWL_NUM_OF_TBS; i++)
  1497. cmdlen += iwl_pcie_tfd_tb_get_len(tfd, i);
  1498. return cmdlen;
  1499. }
  1500. static const struct {
  1501. u32 start, end;
  1502. } iwl_prph_dump_addr[] = {
  1503. { .start = 0x00a00000, .end = 0x00a00000 },
  1504. { .start = 0x00a0000c, .end = 0x00a00024 },
  1505. { .start = 0x00a0002c, .end = 0x00a0003c },
  1506. { .start = 0x00a00410, .end = 0x00a00418 },
  1507. { .start = 0x00a00420, .end = 0x00a00420 },
  1508. { .start = 0x00a00428, .end = 0x00a00428 },
  1509. { .start = 0x00a00430, .end = 0x00a0043c },
  1510. { .start = 0x00a00444, .end = 0x00a00444 },
  1511. { .start = 0x00a004c0, .end = 0x00a004cc },
  1512. { .start = 0x00a004d8, .end = 0x00a004d8 },
  1513. { .start = 0x00a004e0, .end = 0x00a004f0 },
  1514. { .start = 0x00a00840, .end = 0x00a00840 },
  1515. { .start = 0x00a00850, .end = 0x00a00858 },
  1516. { .start = 0x00a01004, .end = 0x00a01008 },
  1517. { .start = 0x00a01010, .end = 0x00a01010 },
  1518. { .start = 0x00a01018, .end = 0x00a01018 },
  1519. { .start = 0x00a01024, .end = 0x00a01024 },
  1520. { .start = 0x00a0102c, .end = 0x00a01034 },
  1521. { .start = 0x00a0103c, .end = 0x00a01040 },
  1522. { .start = 0x00a01048, .end = 0x00a01094 },
  1523. { .start = 0x00a01c00, .end = 0x00a01c20 },
  1524. { .start = 0x00a01c58, .end = 0x00a01c58 },
  1525. { .start = 0x00a01c7c, .end = 0x00a01c7c },
  1526. { .start = 0x00a01c28, .end = 0x00a01c54 },
  1527. { .start = 0x00a01c5c, .end = 0x00a01c5c },
  1528. { .start = 0x00a01c84, .end = 0x00a01c84 },
  1529. { .start = 0x00a01ce0, .end = 0x00a01d0c },
  1530. { .start = 0x00a01d18, .end = 0x00a01d20 },
  1531. { .start = 0x00a01d2c, .end = 0x00a01d30 },
  1532. { .start = 0x00a01d40, .end = 0x00a01d5c },
  1533. { .start = 0x00a01d80, .end = 0x00a01d80 },
  1534. { .start = 0x00a01d98, .end = 0x00a01d98 },
  1535. { .start = 0x00a01dc0, .end = 0x00a01dfc },
  1536. { .start = 0x00a01e00, .end = 0x00a01e2c },
  1537. { .start = 0x00a01e40, .end = 0x00a01e60 },
  1538. { .start = 0x00a01e84, .end = 0x00a01e90 },
  1539. { .start = 0x00a01e9c, .end = 0x00a01ec4 },
  1540. { .start = 0x00a01ed0, .end = 0x00a01ed0 },
  1541. { .start = 0x00a01f00, .end = 0x00a01f14 },
  1542. { .start = 0x00a01f44, .end = 0x00a01f58 },
  1543. { .start = 0x00a01f80, .end = 0x00a01fa8 },
  1544. { .start = 0x00a01fb0, .end = 0x00a01fbc },
  1545. { .start = 0x00a01ff8, .end = 0x00a01ffc },
  1546. { .start = 0x00a02000, .end = 0x00a02048 },
  1547. { .start = 0x00a02068, .end = 0x00a020f0 },
  1548. { .start = 0x00a02100, .end = 0x00a02118 },
  1549. { .start = 0x00a02140, .end = 0x00a0214c },
  1550. { .start = 0x00a02168, .end = 0x00a0218c },
  1551. { .start = 0x00a021c0, .end = 0x00a021c0 },
  1552. { .start = 0x00a02400, .end = 0x00a02410 },
  1553. { .start = 0x00a02418, .end = 0x00a02420 },
  1554. { .start = 0x00a02428, .end = 0x00a0242c },
  1555. { .start = 0x00a02434, .end = 0x00a02434 },
  1556. { .start = 0x00a02440, .end = 0x00a02460 },
  1557. { .start = 0x00a02468, .end = 0x00a024b0 },
  1558. { .start = 0x00a024c8, .end = 0x00a024cc },
  1559. { .start = 0x00a02500, .end = 0x00a02504 },
  1560. { .start = 0x00a0250c, .end = 0x00a02510 },
  1561. { .start = 0x00a02540, .end = 0x00a02554 },
  1562. { .start = 0x00a02580, .end = 0x00a025f4 },
  1563. { .start = 0x00a02600, .end = 0x00a0260c },
  1564. { .start = 0x00a02648, .end = 0x00a02650 },
  1565. { .start = 0x00a02680, .end = 0x00a02680 },
  1566. { .start = 0x00a026c0, .end = 0x00a026d0 },
  1567. { .start = 0x00a02700, .end = 0x00a0270c },
  1568. { .start = 0x00a02804, .end = 0x00a02804 },
  1569. { .start = 0x00a02818, .end = 0x00a0281c },
  1570. { .start = 0x00a02c00, .end = 0x00a02db4 },
  1571. { .start = 0x00a02df4, .end = 0x00a02fb0 },
  1572. { .start = 0x00a03000, .end = 0x00a03014 },
  1573. { .start = 0x00a0301c, .end = 0x00a0302c },
  1574. { .start = 0x00a03034, .end = 0x00a03038 },
  1575. { .start = 0x00a03040, .end = 0x00a03048 },
  1576. { .start = 0x00a03060, .end = 0x00a03068 },
  1577. { .start = 0x00a03070, .end = 0x00a03074 },
  1578. { .start = 0x00a0307c, .end = 0x00a0307c },
  1579. { .start = 0x00a03080, .end = 0x00a03084 },
  1580. { .start = 0x00a0308c, .end = 0x00a03090 },
  1581. { .start = 0x00a03098, .end = 0x00a03098 },
  1582. { .start = 0x00a030a0, .end = 0x00a030a0 },
  1583. { .start = 0x00a030a8, .end = 0x00a030b4 },
  1584. { .start = 0x00a030bc, .end = 0x00a030bc },
  1585. { .start = 0x00a030c0, .end = 0x00a0312c },
  1586. { .start = 0x00a03c00, .end = 0x00a03c5c },
  1587. { .start = 0x00a04400, .end = 0x00a04454 },
  1588. { .start = 0x00a04460, .end = 0x00a04474 },
  1589. { .start = 0x00a044c0, .end = 0x00a044ec },
  1590. { .start = 0x00a04500, .end = 0x00a04504 },
  1591. { .start = 0x00a04510, .end = 0x00a04538 },
  1592. { .start = 0x00a04540, .end = 0x00a04548 },
  1593. { .start = 0x00a04560, .end = 0x00a0457c },
  1594. { .start = 0x00a04590, .end = 0x00a04598 },
  1595. { .start = 0x00a045c0, .end = 0x00a045f4 },
  1596. };
  1597. static u32 iwl_trans_pcie_dump_prph(struct iwl_trans *trans,
  1598. struct iwl_fw_error_dump_data **data)
  1599. {
  1600. struct iwl_fw_error_dump_prph *prph;
  1601. unsigned long flags;
  1602. u32 prph_len = 0, i;
  1603. if (!iwl_trans_grab_nic_access(trans, false, &flags))
  1604. return 0;
  1605. for (i = 0; i < ARRAY_SIZE(iwl_prph_dump_addr); i++) {
  1606. /* The range includes both boundaries */
  1607. int num_bytes_in_chunk = iwl_prph_dump_addr[i].end -
  1608. iwl_prph_dump_addr[i].start + 4;
  1609. int reg;
  1610. __le32 *val;
  1611. prph_len += sizeof(**data) + sizeof(*prph) + num_bytes_in_chunk;
  1612. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PRPH);
  1613. (*data)->len = cpu_to_le32(sizeof(*prph) +
  1614. num_bytes_in_chunk);
  1615. prph = (void *)(*data)->data;
  1616. prph->prph_start = cpu_to_le32(iwl_prph_dump_addr[i].start);
  1617. val = (void *)prph->data;
  1618. for (reg = iwl_prph_dump_addr[i].start;
  1619. reg <= iwl_prph_dump_addr[i].end;
  1620. reg += 4)
  1621. *val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans,
  1622. reg));
  1623. *data = iwl_fw_error_next_data(*data);
  1624. }
  1625. iwl_trans_release_nic_access(trans, &flags);
  1626. return prph_len;
  1627. }
  1628. #define IWL_CSR_TO_DUMP (0x250)
  1629. static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
  1630. struct iwl_fw_error_dump_data **data)
  1631. {
  1632. u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
  1633. __le32 *val;
  1634. int i;
  1635. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
  1636. (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
  1637. val = (void *)(*data)->data;
  1638. for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
  1639. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  1640. *data = iwl_fw_error_next_data(*data);
  1641. return csr_len;
  1642. }
  1643. static
  1644. struct iwl_trans_dump_data *iwl_trans_pcie_dump_data(struct iwl_trans *trans)
  1645. {
  1646. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1647. struct iwl_fw_error_dump_data *data;
  1648. struct iwl_txq *cmdq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1649. struct iwl_fw_error_dump_txcmd *txcmd;
  1650. struct iwl_trans_dump_data *dump_data;
  1651. u32 len;
  1652. int i, ptr;
  1653. /* transport dump header */
  1654. len = sizeof(*dump_data);
  1655. /* host commands */
  1656. len += sizeof(*data) +
  1657. cmdq->q.n_window * (sizeof(*txcmd) + TFD_MAX_PAYLOAD_SIZE);
  1658. /* CSR registers */
  1659. len += sizeof(*data) + IWL_CSR_TO_DUMP;
  1660. /* PRPH registers */
  1661. for (i = 0; i < ARRAY_SIZE(iwl_prph_dump_addr); i++) {
  1662. /* The range includes both boundaries */
  1663. int num_bytes_in_chunk = iwl_prph_dump_addr[i].end -
  1664. iwl_prph_dump_addr[i].start + 4;
  1665. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_prph) +
  1666. num_bytes_in_chunk;
  1667. }
  1668. /* FW monitor */
  1669. if (trans_pcie->fw_mon_page)
  1670. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  1671. trans_pcie->fw_mon_size;
  1672. dump_data = vzalloc(len);
  1673. if (!dump_data)
  1674. return NULL;
  1675. len = 0;
  1676. data = (void *)dump_data->data;
  1677. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
  1678. txcmd = (void *)data->data;
  1679. spin_lock_bh(&cmdq->lock);
  1680. ptr = cmdq->q.write_ptr;
  1681. for (i = 0; i < cmdq->q.n_window; i++) {
  1682. u8 idx = get_cmd_index(&cmdq->q, ptr);
  1683. u32 caplen, cmdlen;
  1684. cmdlen = iwl_trans_pcie_get_cmdlen(&cmdq->tfds[ptr]);
  1685. caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
  1686. if (cmdlen) {
  1687. len += sizeof(*txcmd) + caplen;
  1688. txcmd->cmdlen = cpu_to_le32(cmdlen);
  1689. txcmd->caplen = cpu_to_le32(caplen);
  1690. memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
  1691. txcmd = (void *)((u8 *)txcmd->data + caplen);
  1692. }
  1693. ptr = iwl_queue_dec_wrap(ptr);
  1694. }
  1695. spin_unlock_bh(&cmdq->lock);
  1696. data->len = cpu_to_le32(len);
  1697. len += sizeof(*data);
  1698. data = iwl_fw_error_next_data(data);
  1699. len += iwl_trans_pcie_dump_prph(trans, &data);
  1700. len += iwl_trans_pcie_dump_csr(trans, &data);
  1701. /* data is already pointing to the next section */
  1702. if (trans_pcie->fw_mon_page) {
  1703. struct iwl_fw_error_dump_fw_mon *fw_mon_data;
  1704. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
  1705. data->len = cpu_to_le32(trans_pcie->fw_mon_size +
  1706. sizeof(*fw_mon_data));
  1707. fw_mon_data = (void *)data->data;
  1708. fw_mon_data->fw_mon_wr_ptr =
  1709. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_WRPTR));
  1710. fw_mon_data->fw_mon_cycle_cnt =
  1711. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_CYCLE_CNT));
  1712. fw_mon_data->fw_mon_base_ptr =
  1713. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_BASE_ADDR));
  1714. /*
  1715. * The firmware is now asserted, it won't write anything to
  1716. * the buffer. CPU can take ownership to fetch the data.
  1717. * The buffer will be handed back to the device before the
  1718. * firmware will be restarted.
  1719. */
  1720. dma_sync_single_for_cpu(trans->dev, trans_pcie->fw_mon_phys,
  1721. trans_pcie->fw_mon_size,
  1722. DMA_FROM_DEVICE);
  1723. memcpy(fw_mon_data->data, page_address(trans_pcie->fw_mon_page),
  1724. trans_pcie->fw_mon_size);
  1725. len += sizeof(*data) + sizeof(*fw_mon_data) +
  1726. trans_pcie->fw_mon_size;
  1727. }
  1728. dump_data->len = len;
  1729. return dump_data;
  1730. }
  1731. #else
  1732. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1733. struct dentry *dir)
  1734. {
  1735. return 0;
  1736. }
  1737. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1738. static const struct iwl_trans_ops trans_ops_pcie = {
  1739. .start_hw = iwl_trans_pcie_start_hw,
  1740. .op_mode_leave = iwl_trans_pcie_op_mode_leave,
  1741. .fw_alive = iwl_trans_pcie_fw_alive,
  1742. .start_fw = iwl_trans_pcie_start_fw,
  1743. .stop_device = iwl_trans_pcie_stop_device,
  1744. .d3_suspend = iwl_trans_pcie_d3_suspend,
  1745. .d3_resume = iwl_trans_pcie_d3_resume,
  1746. .send_cmd = iwl_trans_pcie_send_hcmd,
  1747. .tx = iwl_trans_pcie_tx,
  1748. .reclaim = iwl_trans_pcie_reclaim,
  1749. .txq_disable = iwl_trans_pcie_txq_disable,
  1750. .txq_enable = iwl_trans_pcie_txq_enable,
  1751. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1752. .wait_tx_queue_empty = iwl_trans_pcie_wait_txq_empty,
  1753. .write8 = iwl_trans_pcie_write8,
  1754. .write32 = iwl_trans_pcie_write32,
  1755. .read32 = iwl_trans_pcie_read32,
  1756. .read_prph = iwl_trans_pcie_read_prph,
  1757. .write_prph = iwl_trans_pcie_write_prph,
  1758. .read_mem = iwl_trans_pcie_read_mem,
  1759. .write_mem = iwl_trans_pcie_write_mem,
  1760. .configure = iwl_trans_pcie_configure,
  1761. .set_pmi = iwl_trans_pcie_set_pmi,
  1762. .grab_nic_access = iwl_trans_pcie_grab_nic_access,
  1763. .release_nic_access = iwl_trans_pcie_release_nic_access,
  1764. .set_bits_mask = iwl_trans_pcie_set_bits_mask,
  1765. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1766. .dump_data = iwl_trans_pcie_dump_data,
  1767. #endif
  1768. };
  1769. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  1770. const struct pci_device_id *ent,
  1771. const struct iwl_cfg *cfg)
  1772. {
  1773. struct iwl_trans_pcie *trans_pcie;
  1774. struct iwl_trans *trans;
  1775. u16 pci_cmd;
  1776. int err;
  1777. trans = kzalloc(sizeof(struct iwl_trans) +
  1778. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1779. if (!trans) {
  1780. err = -ENOMEM;
  1781. goto out;
  1782. }
  1783. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1784. trans->ops = &trans_ops_pcie;
  1785. trans->cfg = cfg;
  1786. trans_lockdep_init(trans);
  1787. trans_pcie->trans = trans;
  1788. spin_lock_init(&trans_pcie->irq_lock);
  1789. spin_lock_init(&trans_pcie->reg_lock);
  1790. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  1791. err = pci_enable_device(pdev);
  1792. if (err)
  1793. goto out_no_pci;
  1794. if (!cfg->base_params->pcie_l1_allowed) {
  1795. /*
  1796. * W/A - seems to solve weird behavior. We need to remove this
  1797. * if we don't want to stay in L1 all the time. This wastes a
  1798. * lot of power.
  1799. */
  1800. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  1801. PCIE_LINK_STATE_L1 |
  1802. PCIE_LINK_STATE_CLKPM);
  1803. }
  1804. pci_set_master(pdev);
  1805. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1806. if (!err)
  1807. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1808. if (err) {
  1809. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1810. if (!err)
  1811. err = pci_set_consistent_dma_mask(pdev,
  1812. DMA_BIT_MASK(32));
  1813. /* both attempts failed: */
  1814. if (err) {
  1815. dev_err(&pdev->dev, "No suitable DMA available\n");
  1816. goto out_pci_disable_device;
  1817. }
  1818. }
  1819. err = pci_request_regions(pdev, DRV_NAME);
  1820. if (err) {
  1821. dev_err(&pdev->dev, "pci_request_regions failed\n");
  1822. goto out_pci_disable_device;
  1823. }
  1824. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  1825. if (!trans_pcie->hw_base) {
  1826. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  1827. err = -ENODEV;
  1828. goto out_pci_release_regions;
  1829. }
  1830. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1831. * PCI Tx retries from interfering with C3 CPU state */
  1832. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1833. trans->dev = &pdev->dev;
  1834. trans_pcie->pci_dev = pdev;
  1835. iwl_disable_interrupts(trans);
  1836. err = pci_enable_msi(pdev);
  1837. if (err) {
  1838. dev_err(&pdev->dev, "pci_enable_msi failed(0X%x)\n", err);
  1839. /* enable rfkill interrupt: hw bug w/a */
  1840. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1841. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1842. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1843. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1844. }
  1845. }
  1846. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  1847. /*
  1848. * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
  1849. * changed, and now the revision step also includes bit 0-1 (no more
  1850. * "dash" value). To keep hw_rev backwards compatible - we'll store it
  1851. * in the old format.
  1852. */
  1853. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1854. trans->hw_rev = (trans->hw_rev & 0xfff0) |
  1855. (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2);
  1856. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  1857. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1858. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1859. /* Initialize the wait queue for commands */
  1860. init_waitqueue_head(&trans_pcie->wait_command_queue);
  1861. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  1862. "iwl_cmd_pool:%s", dev_name(trans->dev));
  1863. trans->dev_cmd_headroom = 0;
  1864. trans->dev_cmd_pool =
  1865. kmem_cache_create(trans->dev_cmd_pool_name,
  1866. sizeof(struct iwl_device_cmd)
  1867. + trans->dev_cmd_headroom,
  1868. sizeof(void *),
  1869. SLAB_HWCACHE_ALIGN,
  1870. NULL);
  1871. if (!trans->dev_cmd_pool) {
  1872. err = -ENOMEM;
  1873. goto out_pci_disable_msi;
  1874. }
  1875. if (iwl_pcie_alloc_ict(trans))
  1876. goto out_free_cmd_pool;
  1877. err = request_threaded_irq(pdev->irq, iwl_pcie_isr,
  1878. iwl_pcie_irq_handler,
  1879. IRQF_SHARED, DRV_NAME, trans);
  1880. if (err) {
  1881. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  1882. goto out_free_ict;
  1883. }
  1884. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1885. return trans;
  1886. out_free_ict:
  1887. iwl_pcie_free_ict(trans);
  1888. out_free_cmd_pool:
  1889. kmem_cache_destroy(trans->dev_cmd_pool);
  1890. out_pci_disable_msi:
  1891. pci_disable_msi(pdev);
  1892. out_pci_release_regions:
  1893. pci_release_regions(pdev);
  1894. out_pci_disable_device:
  1895. pci_disable_device(pdev);
  1896. out_no_pci:
  1897. kfree(trans);
  1898. out:
  1899. return ERR_PTR(err);
  1900. }