trans.c 69 KB

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